Repository navigation
Studio: give back the continuation lease when the user stops the preflight - #9560
Conversation
…light A tab that starts an automatic continuation takes a hold on that message's cross-tab lease and renews it for as long as its own run is alive. A hold that has not started streaming yet is renewed rather than timed out on purpose: preflight has no upper bound, and an arming deadline dropped holds in the middle of a model load, lapsing leases under continuations that were about to stream. That leaves one way out, and it does not cover Stop. The adapter wrapper skips its per-thread failure notice when the run's signal is already aborted, because the abort is what was asked for rather than a fault to report, and the stream flag never moved in either direction because no token was ever on its way. So a preflight the user stopped left a hold with nothing to arm on and nothing to settle on: it renewed the lease for the life of the tab, and every other tab read that lease as live and refused the message for just as long. Hand the keeper the runtime's own view of the run, counted off the runStart and runEnd events assistant-ui emits around startRun. The pair spans the whole call, settings pairing and model load included, and closes however the run ends, cancellation among them, so "this side watched a run start here and then watched it end, and the stream never began" is a fact rather than an elapsed time. A preflight that is merely long raises neither event and keeps its hold and its renewals, which is what the absence of an arming deadline is for. Not read off the runtime's running flag, which looks like the answer and is a trap: that flag is the status of the message at the head of the current branch, so clicking the branch picker back to the truncated partial mid-preflight reads as the run having ended and drops a hold whose continuation goes on to stream. An unreadable runtime is reported as unknown rather than as idle. getById throws for any id the thread list cannot resolve to a mounted runtime -- an alias hydration retired, or an ordinary background thread the user has navigated away from -- so both spellings of the thread are tried, and a hold is only ever discarded on an answer this side actually got. Discarded, never released: the lease lapses on its own TTL and no done marker claims a message that produced not one token.
… signal The first cut read whether a run was outstanding off the thread, first from assistant-ui's running flag and then by counting its runStart and runEnd events. Both are per-thread, and per-thread cannot say WHICH run ended. That matters because the next round is claimed while the previous one is still winding down. The adapter clears runningByThreadId from its own finally, strictly before the runtime announces that run ending, so the successor's hold is taken, its run is issued, and only then does the predecessor report itself over. Counted per thread, the predecessor's ending settles the successor's hold in the middle of its preflight, the renewals stop, and the lease lapses under a continuation that is still coming. Reproduced against a real LocalThreadRuntimeCore with the claim on a microtask off the predecessor's last chunk: held 1 during preflight, 0 once the predecessor ended. startRun hands back the roundtrip's promise. It is pending for the whole preflight however long that takes, it settles when THAT run ends however it ends, and it is the only handle here identified with one run rather than with a thread. So the bar passes what startRun returned to the keeper, and an unarmed hold whose own run has settled is discarded exactly as a failed preflight is: forgotten, never released, its lease left to lapse on its own TTL. Nothing reads a clock, and a preflight that is merely long leaves the promise pending, which settles nothing. The type says startRun returns void while every hop from the store proxy down to LocalThreadRuntimeCore delegates with return, and that core is async. The value is therefore passed through untyped and its shape checked once: anything that is not thenable is assistant-ui no longer handing the run back, and the honest answer is no signal at all, leaving the hold kept and renewed as before rather than released early. This also drops the thread-id lookups the previous approach needed. There is no getById here any more, so no retired alias to fall back past and no listener to unsubscribe.
…continuation-hold
|
Confirmed this hits the unarmed-hold path in studio/frontend/src/features/chat/utils/continuation.ts, where a preflight stopped by the user has nothing to settle on and keeps renewing its lease, and the run-promise version you pushed in f8a1d6f addresses the predecessor interleaving as well. Will get this reviewed. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ef20c737cf
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| function observe(): void { | ||
| const at = now(); | ||
| for (const [id, hold] of [...holds]) { | ||
| if (hold.settled && !hold.armed) { |
There was a problem hiding this comment.
Prevent replacement runs from arming stopped preflights
When a user-started replacement run flips this thread's signal to running before the aborted continuation's promise settles, the earlier signal notification has already set the continuation hold's armed flag. This guard then deliberately excludes that hold, so the replacement run's eventual end calls release() and records the truncated message as successfully continued even though its continuation emitted nothing. Fresh evidence after the prior fix is the still-uncovered ordering hold while idle → replacement starts → issued promise settles → replacement ends; the added test instead starts with the thread already busy, which never arms the hold.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The ordering is real and the reading of the test is right: it starts with the thread busy, so the hold never arms and it does not cover this. I am not changing it here, because it is not this PR's behavior.
Verified rather than argued. Running that exact sequence against the keeper as it stands on the merge base, with none of this PR applied:
hold while idle -> replacement streams -> replacement ends
released: ["m1"]
Same result at this head. The hold arms at the store notification for the replacement's stream, before the aborted run's promise settles, and settled is only ever consulted for a hold that never armed. So the false marker predates the change and the change neither introduces nor widens it.
Closing it needs the arming side, not the settling side: a thread-wide flag cannot say whose run it is, so the hold would have to arm on run identity, which the owner list in runOwnerByThreadId could carry. That reverses a choice #9425 made on purpose, and this PR exists because adding a signal to that machinery mid-review is what produced the arming-timeout regression the first time. The last round in this thread is the same lesson: widening the settle predicate to cover this case cost an armed hold its marker whenever a second owner held the key. It gets its own change and its own tests.
The narrower claim this PR does make is unaffected: a hold whose own run ended without ever streaming is discarded rather than renewed for the life of the tab.
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Checked the new settled-before-armed branch in studio/frontend/src/features/chat/utils/continuation.ts against the keeper on main, which still has no way to end an unarmed hold outside |
…nswer The settled check sits ahead of the arming check on purpose, but moving it below the arming check failed no test: the case claimed for it read a hold whose own run ended while the thread was idle, where the two positions agree. They only disagree in one pass: the hold owns an idle key, its own run ended without streaming, and the key already reads busy by the time the keeper looks, because the notification for the run that replaced this one has not been delivered and the settle callback observes on its own. Below the arming check that pass arms the hold on somebody else's run, and the end of that run releases it, writing a done marker for a message that produced not one token. Also corrects the ownsTheKey comment, which said such a hold can never arm. It cannot arm off its own run, which is what the guard is about, but it can still arm off a run that starts after the key has been seen idle. That is the thread-wide mis-arm #9425 declined to close and this change does not move it. npm test 7086 passing, npm run typecheck clean on both projects.
…equence The signal the keeper is wired to notifies synchronously on every store write, so the app never reaches the pass the previous commit covers: the key turning busy is always observed, and observed while the hold is still unsettled, which arms it. That is the superseded-run case this change does not close. The keeper takes its signal as a parameter and promises nothing about delivery, so the ordering is still worth pinning. Claiming a user hits it was not.
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
185 comment lines added to studio/frontend, down to 98 against 718 added non-blank lines. Step narration in the tests goes entirely, since the assertion messages already say what each line does, and the paragraphs collapse to the one fact each carries: why the settled check is first, why an armed hold is exempt, why ownsTheKey leaves the busy-key case alone, why the callback captures the hold rather than looking it up again. Comments, docstrings and whitespace only, AST-verified against d1f1ab2. npm test 97 passing in this file, npm run typecheck clean.
…n one spelling Two gaps found by reverting each new guard in turn and re-running the case meant to catch it. Both are test-only; no behaviour changes. Emptying watchAutoContinueRun's body, leaving its call site and the issuedRunFrom shape check in place so both source-text matches still hold, kept all 97 cases green. The keeper's own decisions are covered against a keeper built with fakes, and the call site is pinned as text, but nothing executed the one function joining them, which is the only thing that ends a hold whose preflight the user stopped. So it is exercised against the real module-scope keeper, in its own file because that module reads runningByThreadId off the chat runtime store and needs the store stub resolver. The observable is the renewal timer: holdAutoContinueRun starts it and the keeper's tick clears it as soon as nothing is held, so "the timer stopped" is "the hold was given up" with no clock to advance. The timer is replaced outright rather than mocked, because the keeper captured Date.now as a default argument at import and mocking Date desynchronises its clock from the lease's, at which point renewals stop landing and the test passes for the wrong reason. The second gap is the arming deadline this change exists to avoid. The assertion read doesNotMatch(/setTimeout\(/), so a deadline counting its own elapsed time under setInterval passed unnoticed. It now compares the full list of timers either file schedules against an allowlist, and names the renewal interval rather than counting it, so a second setInterval cannot pose as it. npm test 7087 passing, npm run typecheck clean.
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…light (unslothai#9560) * Studio: give back the continuation lease when the user stops the preflight A tab that starts an automatic continuation takes a hold on that message's cross-tab lease and renews it for as long as its own run is alive. A hold that has not started streaming yet is renewed rather than timed out on purpose: preflight has no upper bound, and an arming deadline dropped holds in the middle of a model load, lapsing leases under continuations that were about to stream. That leaves one way out, and it does not cover Stop. The adapter wrapper skips its per-thread failure notice when the run's signal is already aborted, because the abort is what was asked for rather than a fault to report, and the stream flag never moved in either direction because no token was ever on its way. So a preflight the user stopped left a hold with nothing to arm on and nothing to settle on: it renewed the lease for the life of the tab, and every other tab read that lease as live and refused the message for just as long. Hand the keeper the runtime's own view of the run, counted off the runStart and runEnd events assistant-ui emits around startRun. The pair spans the whole call, settings pairing and model load included, and closes however the run ends, cancellation among them, so "this side watched a run start here and then watched it end, and the stream never began" is a fact rather than an elapsed time. A preflight that is merely long raises neither event and keeps its hold and its renewals, which is what the absence of an arming deadline is for. Not read off the runtime's running flag, which looks like the answer and is a trap: that flag is the status of the message at the head of the current branch, so clicking the branch picker back to the truncated partial mid-preflight reads as the run having ended and drops a hold whose continuation goes on to stream. An unreadable runtime is reported as unknown rather than as idle. getById throws for any id the thread list cannot resolve to a mounted runtime -- an alias hydration retired, or an ordinary background thread the user has navigated away from -- so both spellings of the thread are tried, and a hold is only ever discarded on an answer this side actually got. Discarded, never released: the lease lapses on its own TTL and no done marker claims a message that produced not one token. * Studio: settle the hold on the run's own promise, not on a per-thread signal The first cut read whether a run was outstanding off the thread, first from assistant-ui's running flag and then by counting its runStart and runEnd events. Both are per-thread, and per-thread cannot say WHICH run ended. That matters because the next round is claimed while the previous one is still winding down. The adapter clears runningByThreadId from its own finally, strictly before the runtime announces that run ending, so the successor's hold is taken, its run is issued, and only then does the predecessor report itself over. Counted per thread, the predecessor's ending settles the successor's hold in the middle of its preflight, the renewals stop, and the lease lapses under a continuation that is still coming. Reproduced against a real LocalThreadRuntimeCore with the claim on a microtask off the predecessor's last chunk: held 1 during preflight, 0 once the predecessor ended. startRun hands back the roundtrip's promise. It is pending for the whole preflight however long that takes, it settles when THAT run ends however it ends, and it is the only handle here identified with one run rather than with a thread. So the bar passes what startRun returned to the keeper, and an unarmed hold whose own run has settled is discarded exactly as a failed preflight is: forgotten, never released, its lease left to lapse on its own TTL. Nothing reads a clock, and a preflight that is merely long leaves the promise pending, which settles nothing. The type says startRun returns void while every hop from the store proxy down to LocalThreadRuntimeCore delegates with return, and that core is async. The value is therefore passed through untyped and its shape checked once: anything that is not thenable is assistant-ui no longer handing the run back, and the honest answer is no signal at all, leaving the hold kept and renewed as before rather than released early. This also drops the thread-id lookups the previous approach needed. There is no getById here any more, so no retired alias to fall back past and no listener to unsubscribe. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Discard a continuation hold the run on its thread cannot own * Keep an armed hold when a second owner holds the thread key * Trim the continuation hold comments * Drop a test counter nothing asserts * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Keep a hold whose key was already busy when it was taken * Cover the one pass where the settled check being first is the whole answer The settled check sits ahead of the arming check on purpose, but moving it below the arming check failed no test: the case claimed for it read a hold whose own run ended while the thread was idle, where the two positions agree. They only disagree in one pass: the hold owns an idle key, its own run ended without streaming, and the key already reads busy by the time the keeper looks, because the notification for the run that replaced this one has not been delivered and the settle callback observes on its own. Below the arming check that pass arms the hold on somebody else's run, and the end of that run releases it, writing a done marker for a message that produced not one token. Also corrects the ownsTheKey comment, which said such a hold can never arm. It cannot arm off its own run, which is what the guard is about, but it can still arm off a run that starts after the key has been seen idle. That is the thread-wide mis-arm unslothai#9425 declined to close and this change does not move it. npm test 7086 passing, npm run typecheck clean on both projects. * Say plainly that the ordering case is an invariant, not a reachable sequence The signal the keeper is wired to notifies synchronously on every store write, so the app never reaches the pass the previous commit covers: the key turning busy is always observed, and observed while the hold is still unsettled, which arms it. That is the superseded-run case this change does not close. The keeper takes its signal as a parameter and promises nothing about delivery, so the ordering is still worth pinning. Claiming a user hits it was not. * Cut this change's comments roughly in half 185 comment lines added to studio/frontend, down to 98 against 718 added non-blank lines. Step narration in the tests goes entirely, since the assertion messages already say what each line does, and the paragraphs collapse to the one fact each carries: why the settled check is first, why an armed hold is exempt, why ownsTheKey leaves the busy-key case alone, why the callback captures the hold rather than looking it up again. Comments, docstrings and whitespace only, AST-verified against d1f1ab2. npm test 97 passing in this file, npm run typecheck clean. * Cover the wire from the bar to the keeper, and every timer rather than one spelling Two gaps found by reverting each new guard in turn and re-running the case meant to catch it. Both are test-only; no behaviour changes. Emptying watchAutoContinueRun's body, leaving its call site and the issuedRunFrom shape check in place so both source-text matches still hold, kept all 97 cases green. The keeper's own decisions are covered against a keeper built with fakes, and the call site is pinned as text, but nothing executed the one function joining them, which is the only thing that ends a hold whose preflight the user stopped. So it is exercised against the real module-scope keeper, in its own file because that module reads runningByThreadId off the chat runtime store and needs the store stub resolver. The observable is the renewal timer: holdAutoContinueRun starts it and the keeper's tick clears it as soon as nothing is held, so "the timer stopped" is "the hold was given up" with no clock to advance. The timer is replaced outright rather than mocked, because the keeper captured Date.now as a default argument at import and mocking Date desynchronises its clock from the lease's, at which point renewals stop landing and the test passes for the wrong reason. The second gap is the arming deadline this change exists to avoid. The assertion read doesNotMatch(/setTimeout\(/), so a deadline counting its own elapsed time under setInterval passed unnoticed. It now compares the full list of timers either file schedules against an allowlist, and names the renewal interval rather than counting it, so a second setInterval cannot pose as it. npm test 7087 passing, npm run typecheck clean. * Cut the comments the follow-up added back to where the first sweep left them --------- Co-authored-by: oobabooga <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Follow-up to #9425, which deliberately left this case out. Adding a signal on the abort path mid-review was how the arming-timeout regression got in, so it gets its own change and its own tests.
The hole
#9425 gave every automatic continuation a cross-tab hold on its message's lease. A hold arms when that thread starts streaming and is released when it stops. A hold that has not armed yet is renewed rather than timed out, on purpose: preflight has no upper bound (this chat's settings pairing, then
waitForModelReadypolling while a large local GGUF loads), and the fixed arming timeout that was tried first dropped holds in the middle of that, lapsing leases under continuations that were about to stream.That leaves exactly one way to end an unarmed hold,
keeper.failed, and it ridesunsloth:prompt-queue-run-failed. That event does not fire on abort:The skip is correct on its own terms: the abort is what the user asked for, not a fault to report. But
runningByThreadIdnever moved either, because no token was ever on its way. So a preflight the user stopped left a hold with nothing to arm on and nothing to settle on. It renewed the lease for the life of the tab, and every other tab read that lease as live and refused the message for just as long. Reproducing it against a realLocalThreadRuntimeCore: hold count 1 during preflight, still 1 after Stop, renewed forever.The fix
Hand the keeper the run's own promise.
ThreadRuntime.startRunis declared to returnvoidand in fact returns the roundtrip's promise: every hop from the store proxy down toLocalThreadRuntimeCore.startRundelegates withreturn, and that core isasync. The bar passes what it got back, untyped, towatchAutoContinueRun, andissuedRunFromchecks the shape rather than assuming it. That promise settles when THAT run ends, however it ends: finished, failed, or cancelled in the middle of a model load.A hold whose own run has ended without ever streaming is discarded, exactly as a failed preflight is: forgotten, never released. The lease lapses on its own TTL and the message comes back to whoever is still looking at it, and no
donemarker claims a message that produced not one token.That check sits ahead of the arming branch, not after it: a hold whose own run is over must not arm on whatever is on the thread now. Only ahead of it, though, and only for a hold that never armed.
runningByThreadIdis a flag with a LIST of owners behind it andsetThreadRunningclears by owner, so the key can still read busy after this run's ownfinallyhas cleared it:scheduleGenerationRecoveryfollows a durable server run from outside the adapter and holds the same key. An armed hold settling under that is owed itsdonemarker, and dropping it would hand a continuation that did stream back to the next tab to pay for again.What this does not close: a run that supersedes the continuation (sending a new message aborts the run in flight before starting its own) can arm the abandoned hold before that hold's own promise settles, and a thread-wide signal cannot tell that run from this hold's own. Closing it needs per-run arming, which #9425 declined on purpose. It is the behaviour on main today and this change does not move it.
Why this is event-driven and not a deadline
Nothing here reads a clock, and nothing compares an elapsed time to a threshold. A hold ends only on a fact reported by something that knows, and there are now two of them, which are the two ways a preflight can end without a stream:
A preflight that is merely long raises neither. Its promise is still pending, so it keeps its hold and its renewals for as long as it takes, which is the whole reason arming has no deadline. The test asserting neither file contains
setTimeout(still holds, and now covers the new file too.Two traps worth naming
A thread-wide notice cannot say which run ended. The next round is claimed while the previous one is still winding down, so the predecessor's ending would settle the successor's hold mid-preflight and lapse the lease under a live continuation, which is precisely the failure the missing arming deadline exists to avoid, reached without a clock. The promise is the only handle here identified with one run, and the callback is scoped to the exact hold object it was registered for rather than looked up again by key when it settles.
A value that is not thenable is not something to work around. It is assistant-ui no longer handing the run back, and the honest answer is no signal at all: the hold is kept and renewed exactly as every unarmed hold is, which is the behaviour this change replaced, never an early release.
issuedRunFromlives in its own file importing nothing but a type, because the keeper's wiring reaches the chat store and through it most of the app, which a node test cannot load.Tests
Ten new cases in
studio/frontend/tests/chat-continuation.test.ts:settleOnassumesholdsucceededsetTimeout(The four cases #9425 paid for are untouched and still pass: a hold keeps its lease while its run is still in preflight; a hold whose run never starts holds its lease for the life of the tab; a hold waits for its own run, not the one already in flight; and no
setTimeout(in the keeper's wiring.npm test6734 passing,npm run typecheckclean on both projects. Only the new lines were formatted; the pre-existing drift in these files is left alone.