@@ -6493,18 +6493,148 @@ def Shelve.fresh.u(r: M.Role, +t: String, +th: String) -> Bool:
64936493 case _:
64946494 False{}
64956495
6496- # whether the step brings the thread new input: a message for it (not the
6497- # agent's own) or one queued for it (Lifecycle.wake.fresh)
6498- def Shelve.fresh(cs: List<&2, M.Change>, +th: String) -> Bool:
6499- match cs:
6500- case Nil{}:
6501- False{}
6502- case M.MessagePosted{+t, _, r, _, _} <> rest:
6503- Bool.or(Shelve.fresh.u(r, t, th), Shelve.fresh(rest, th))
6504- case M.TurnQueued{+t, _, _, _} <> rest:
6505- Bool.or(String.eq(t, th), Shelve.fresh(rest, th))
6496+ # These local identity scans return at the first match. The tail calls
6497+ # stay bounded in stack even when a manually settled thread has a long log.
6498+ def Shelve.rows.has.go(xs: List<&2, String>, +id: String, found: Bool) -> Bool:
6499+ match xs:
6500+ case Nil{}: found
6501+ case +l <> rest:
6502+ match found:
6503+ case True{}: True{}
6504+ case False{}: Shelve.rows.has.go(rest, id, M.Bg.is(l, id))
6505+
6506+ def Shelve.rows.has(xs: List<&2, String>, +id: String) -> Bool:
6507+ Shelve.rows.has.go(xs, id, False{})
6508+
6509+ # Snapshot updates count as fresh work only when they introduce a live
6510+ # identity. Progress, launch acknowledgments and completions keep the same
6511+ # identity and do not undo a manual settle, even if the busy count changes.
6512+ def Shelve.rows.fresh.go(xs: List<&2, String>, +old: List<&2, String>, found: Bool) -> Bool:
6513+ match xs:
6514+ case Nil{}: found
6515+ case +l <> rest:
6516+ match found:
6517+ case True{}: True{}
6518+ case False{}:
6519+ Shelve.rows.fresh.go(rest, old, Bool.and(Bool.not(M.Bg.ended(l)), Bool.and(Bool.not(String.is_empty(M.Bg.field(l, 0n))), Bool.not(Shelve.rows.has(old, M.Bg.field(l, 0n))))))
6520+
6521+ def Shelve.rows.fresh(xs: List<&2, String>, +old: List<&2, String>) -> Bool:
6522+ Shelve.rows.fresh.go(xs, old, False{})
6523+
6524+ def Shelve.queue.has.go(qs: List<&2, M.QItem>, +msg: String, found: Bool) -> Bool:
6525+ match qs:
6526+ case Nil{}: found
6527+ case M.QItem{+id, _} <> rest:
6528+ match found:
6529+ case True{}: True{}
6530+ case False{}: Shelve.queue.has.go(rest, msg, String.eq(id, msg))
6531+
6532+ def Shelve.queue.has(qs: List<&2, M.QItem>, +msg: String) -> Bool:
6533+ Shelve.queue.has.go(qs, msg, False{})
6534+
6535+ def Shelve.input.seen.go(es: List<&2, M.Entry>, +msg: String, found: Bool) -> Bool:
6536+ match es:
6537+ case Nil{}: found
6538+ case M.Msg{+id, _, _, _, _} <> rest:
6539+ match found:
6540+ case True{}: True{}
6541+ case False{}: Shelve.input.seen.go(rest, msg, String.eq(id, msg))
65066542 case _ <> rest:
6507- Shelve.fresh(rest, th)
6543+ match found:
6544+ case True{}: True{}
6545+ case False{}: Shelve.input.seen.go(rest, msg, False{})
6546+
6547+ def Shelve.input.seen(es: List<&2, M.Entry>, +msg: String) -> Bool:
6548+ Shelve.input.seen.go(es, msg, False{})
6549+
6550+ # Check the small queue before consulting the thread's history.
6551+ def Shelve.input.new.k(queued: Bool, +s: M.State, +th: String, +msg: String) -> Bool:
6552+ match queued:
6553+ case True{}: False{}
6554+ case False{}: Bool.not(Shelve.input.seen(M.State.mine(s, th), msg))
6555+
6556+ def Shelve.input.new(+s: M.State, +th: String, +msg: String) -> Bool:
6557+ Shelve.input.new.k(Shelve.queue.has(M.Queue.items(s, th), msg), s, th, msg)
6558+
6559+ # Real activity is accepted at its event boundary, against the state just
6560+ # before it. Older callbacks cannot supersede a later manual settlement.
6561+ def Shelve.recent.k(o: M.Override, +sa: Nat, +at: Nat) -> Bool:
6562+ match o:
6563+ case M.ForcedSettled{}: Nat.is_ge(at, sa)
6564+ case _: True{}
6565+
6566+ def Shelve.recent(+s: M.State, +th: String, +at: Nat) -> Bool:
6567+ Shelve.recent.k(M.Lifecycle.override(M.State.thread.get(s, th)), M.Lifecycle.settled_at(M.State.thread.get(s, th)), at)
6568+
6569+ # Only a recent user message for this thread may read its history.
6570+ def Shelve.input.recent(recent: Bool, +s: M.State, +th: String, +msg: String) -> Bool:
6571+ match recent:
6572+ case False{}: False{}
6573+ case True{}: Shelve.input.new(s, th, msg)
6574+
6575+ def Shelve.input.for(hit: Bool, +s: M.State, +th: String, +msg: String, +at: Nat) -> Bool:
6576+ match hit:
6577+ case False{}: False{}
6578+ case True{}: Shelve.input.recent(Shelve.recent(s, th, at), s, th, msg)
6579+
6580+ def Shelve.input.role(r: M.Role, +s: M.State, +t: String, +th: String, +msg: String, +at: Nat) -> Bool:
6581+ match r:
6582+ case M.User{}: Shelve.input.for(String.eq(t, th), s, th, msg, at)
6583+ case _: False{}
6584+
6585+ def Shelve.fresh.event(+s: M.State, c: M.Change, +th: String) -> Bool:
6586+ match c:
6587+ case M.MessagePosted{+t, +msg, r, _, +at}:
6588+ Shelve.input.role(r, s, t, th, msg, at)
6589+ case M.TurnQueued{+t, +msg, _, +at}:
6590+ Bool.and(String.eq(t, th), Bool.and(Shelve.recent(s, th, at), Bool.not(Shelve.queue.has(M.Queue.items(s, th), msg))))
6591+ case M.WorkSet{+t, +kind, rows, +at}:
6592+ Bool.and(String.eq(t, th), Bool.and(Shelve.recent(s, th, at), Bool.and(Bool.or(String.eq(kind, "bg"), String.eq(kind, "watch")),
6593+ Shelve.rows.fresh(M.Bg.lines(rows), M.Bg.lines(M.Setting.get(s, kind ++ "." ++ th, ""))))))
6594+ case M.TaskDelegated{+id, +parent, _, _, _, _, +at}:
6595+ Bool.and(String.eq(parent, th), Bool.and(Shelve.recent(s, th, at), String.is_empty(M.Lifecycle.task.parent(s, id))))
6596+ case M.TurnChanged{+t, M.Running{}, +at}:
6597+ Bool.and(String.eq(t, th), Bool.and(Shelve.recent(s, th, at), Bool.not(M.State.running.k(M.State.thread.get(s, th)))))
6598+ case _: False{}
6599+
6600+ # A settle later in the same transaction wins over the work before it;
6601+ # work accepted after that settle can wake it, including in the same second.
6602+ def Shelve.hold(c: M.Change, +th: String) -> Bool:
6603+ match c:
6604+ case M.ThreadSettled{+t, _, _}: String.eq(t, th)
6605+ case M.ThreadMerged{+t, _, _}: String.eq(t, th)
6606+ case _: False{}
6607+
6608+ def Shelve.held.go(cs: List<&2, M.Change>, +th: String, found: Bool) -> Bool:
6609+ match cs:
6610+ case Nil{}: found
6611+ case +c <> rest:
6612+ match found:
6613+ case True{}: True{}
6614+ case False{}: Shelve.held.go(rest, th, Shelve.hold(c, th))
6615+
6616+ def Shelve.held(cs: List<&2, M.Change>, +th: String) -> Bool:
6617+ Shelve.held.go(cs, th, False{})
6618+
6619+ def Shelve.fresh.next(hold: Bool, fresh: Bool, +s: M.State, c: M.Change, +th: String) -> Bool:
6620+ match hold:
6621+ case True{}: False{}
6622+ case False{}:
6623+ match fresh:
6624+ case True{}: True{}
6625+ case False{}: Shelve.fresh.event(s, c, th)
6626+
6627+ def Shelve.fresh.go(cs: List<&2, M.Change>, +s: M.State, +th: String, fresh: Bool) -> Bool:
6628+ match cs:
6629+ case Nil{}: fresh
6630+ case +c <> rest:
6631+ Shelve.fresh.go(rest, M.State.apply.go(s, c), th, Shelve.fresh.next(Shelve.hold(c, th), fresh, s, c, th))
6632+
6633+ # Active streaming threads skip the event scan entirely.
6634+ def Shelve.fresh.on(sh: M.Shelf, cs: List<&2, M.Change>, +pre: M.State, +th: String) -> Bool:
6635+ match sh:
6636+ case M.Settled{}: Shelve.fresh.go(cs, pre, th, False{})
6637+ case _: False{}
65086638
65096639def Shelve.one.k(wake: Bool, done: Bool, +th: String, +at: Nat, +t: M.Thread, rest: List<&2, M.Change>) -> List<&2, M.Change>:
65106640 match wake done:
@@ -6524,7 +6654,8 @@ def Shelve.wake(+pre: M.State, +post: M.State, +k0: Map<&2, Nat>, +k1: Map<&2, N
65246654 Shelve.wake.k(M.Lifecycle.working(k0, pre, M.State.thread.get(pre, th)), M.Lifecycle.working(k1, post, M.State.thread.get(post, th)), fresh, post, th)
65256655
65266656def Shelve.one(+cs: List<&2, M.Change>, +pre: M.State, +post: M.State, +k0: Map<&2, Nat>, +k1: Map<&2, Nat>, +navs: Map<&2, M.Nav>, +th: String, +at: Nat, rest: List<&2, M.Change>) -> List<&2, M.Change>:
6527- Shelve.one.k(Shelve.wake(pre, post, k0, k1, th, Shelve.fresh(cs, th)), M.Lifecycle.done(k1, post, M.State.thread.get(post, th), M.Snooze.on(at, M.Nav.until(M.Nav.get(navs, th)))),
6657+ Shelve.one.k(Bool.or(Bool.and(Bool.and(Bool.not(Shelve.held(cs, th)), Shelve.recent(pre, th, at)), Shelve.wake(pre, post, k0, k1, th, False{})),
6658+ M.Lifecycle.wake.fresh(Shelve.fresh.on(M.Shelf.of(M.State.thread.get(post, th)), cs, pre, th), M.Lifecycle.working(k1, post, M.State.thread.get(post, th)), M.Shelf.of(M.State.thread.get(post, th)))), M.Lifecycle.done(k1, post, M.State.thread.get(post, th), M.Snooze.on(at, M.Nav.until(M.Nav.get(navs, th)))),
65286659 th, at, M.State.thread.get(post, th), rest)
65296660
65306661def Shelve.go(xs: List<&2, SvAt>, +cs: List<&2, M.Change>, +pre: M.State, +post: M.State, +k0: Map<&2, Nat>, +k1: Map<&2, Nat>, +navs: Map<&2, M.Nav>) -> List<&2, M.Change>:
0 commit comments