Repository navigation
fix(studio): tab.attached can land past run end, and a quit racing open() can orphan a running run - #486
Merged
KnockOutEZ merged 3 commits intoAug 26, 2026
Conversation
applyAttach was the one lifecycle write on the view-model with no terminal-run guard — its three siblings refuse there, and the store checks only that the run row exists. So an attach on a run that had already ended committed, and the append-only log came out saying a cancelled run owns a page. The pre-append guard cannot close the whole window on its own: the attach is ordered per TAB and the terminal event per RUN, so an append still on the wire had already passed a projection run.cancelled was about to move. Joining the two lanes deadlocks, because applyEndRun holds the run lane while awaiting the per-tab lanes its detaches take, so the attach publishes its in-flight append and applyEndRun waits on that one-directionally before its membership read. endRun is idempotent against the terminal event for the same reason from the other side: a quit that cancels a run whose open() is still in flight leaves that open's rollback writing run.cancelled on top of run.cancelled.
open() publishes the session context at status live BEFORE the run exists,
because the run records the session id and the tab it is standing up. Everything
after that is a check-then-act across a broker round-trip, and shutdown() is the
writer on the other side of it — so a user quitting inside that window was read
two ways, both wrong. Either shutdown() found the live context, cancelled the
run, and open() resumed to attach a tab to it; or shutdown() arrived before
createRun resolved, found no run for the session, and left the run open() then
created at running forever with nothing to reap it. Either way the agent got
{ session_id } for a session contexts.clear() had already destroyed, so its every
next call failed wrong_session with nothing saying why.
Each await now re-checks the context when it resumes and unwinds what it built —
including the profile load and the gated navigation, which sit outside the try on
purpose and so cannot be caught there. The refusal is named host_closing rather
than folded into run_unavailable: a window that closed under the agent and a run
store that is down are the same retry decision only if you cannot tell them
apart.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes
KnockOutEZ/wigolo-studio-run#135(SD1 exit-16, MED).What was wrong
applyAttachwas the one lifecycle write onRunViewModelwith no terminal-run guard — its threesiblings (
applyVisibility,applyRequestDecision,applyResolveDecision) all refuse past theterminal event, and the store checks only that the run row exists. The quit path exploited that two
ways, because
open()publishes its session context at statuslivebefore the run exists andnever re-checked it when its awaits resumed.
open()'s awaits.shutdown()finds the live context, ends therun
cancelled, andopen()then resumes and attaches a tab to it: the durable log reads['run.created','run.cancelled','tab.attached'], a cancelled run owning a page in an append-onlylog with no prune path. The agent's half was worse —
{ session_id }for a sessioncontexts.clear()had already destroyed, so every next call failedwrong_session.createRunresolves.runForSessionfinds nothing,shutdown()returns without ending anything, and the run
open()then creates sitsrunningforever: countedby the tray, returned by
GET /v1/runs?status=running. Exactly the orphanopen()'s own rollbackcomment exists to prevent.
What changed
applyAttachrefuses on a terminal run, and on a run this process is not holding, withoutappending — a throw (
RunNotOpenError) rather than the siblings' silent return, because attach isthe one of the four whose caller acts on the answer.
and the terminal event per run, so an append still on the wire has already passed a projection
run.cancelledis about to move. Joining the lanes deadlocks (applyEndRunholds the run lanewhile awaiting the per-tab lanes its detaches take) and taking the run lane outside the tab lane
breaks the pinned "serialises per TAB, not globally" property. So
applyAttachpublishes itsin-flight append and
applyEndRunwaits on that set — one-directionally — before its membershipread.
endRunis idempotent against the terminal event, for the same defect from the other side: a quitthat cancels a run whose
open()is in flight leaves that open's rollback writingrun.cancelledon top of
run.cancelled.open()re-checks the context after every await and unwinds what it built — including after theprofile load and the gated navigation, which sit outside the try/catch on purpose and so cannot be
caught there. The refusal is a new
error_reason,host_closing.Rationale and reversal conditions:
DECISIONS-AUTO.md→ SD1wigolo-studio-run#135.Demo — both forced interleaves and their refusals
The interleaves are forced from inside the run-spine call
open()is parked on, which is where abroker round-trip puts them in the real app. N clean opens reach neither window.
Every guard mutated red, each reversed after
applyAttachterminal refusalrefuses to attach a tab to a run that has already endedapplyAttachmissing-projection refusalrefuses to attach a tab to a run this process is not holdingapplyEndRunterminal idempotencerun.cancelledpin plus 3 quit-race armsapplyEndRunin-flight-attach barriernever lets an attach on the wire commit behind the terminal event—expected [ Array(3) ] to deeply equal [ 'run.created', 'tab.attached', …(2) ], i.e. the exact reported logopen()re-check aftercreateRunopen()re-check afterattachTabnever navigates a session the quit destroyed while its attach was on the wireopen()re-check after the gated navreports a structured failure when the quit lands during the gated startUrl navigationThe post-attach re-check was green on its first flip — it was redundant with the final one for
the no-startUrl path. The arm above was added to make it load-bearing: with a startUrl, removing it
lets the host drive a torn-down tab (
expected [ 'https://example.com/' ] to deeply equal []), andthat arm carries a positive control proving the fixture does navigate when no quit intervenes.
Suites
npm testapps/studionpm testRUN_STUDIO_E2E=1 npm run test:e2enpx tsc --noEmitnpm run typecheck:studionpm run gate:studionpm run lint -w apps/studioTwo first-run reds, both classified as contention and both untouched by this diff:
tests/unit/extraction/schema-truth-perf.test.ts(a wall-clock sanity bound — 66.9 ms against 50 ms,red only while four static gates ran concurrently, 2/2 alone) and
apps/studio/tests/e2e/capture.spec.ts(15 s timeout under the full e2e lane, 3/3 alone, lane rerunclean). Both recorded in
known-issues.md.Territory
apps/studio/src/main/**(lanestudio-main) +apps/studio/tests/**(shared). Nolane-extra:.Non-goals honoured
No lane redesign — both lanes still order exactly what they ordered; the barrier is a directional
wait, not a lane. No store-level terminal guard — the view-model owns this. No change to
graceful-quit budgets — the repair is done by the resumed
open(), not byshutdown()awaitingin-flight opens, precisely so the constant deadline is untouched.
Re-verified after merging
origin/studio-handoff(e17eba10)studio-handoffmoved ahead by 3 merges while this was in review (#482–#485, none touching thesefiles). Merged in, rebuilt, and every gate re-run on the merged tip: root 12 067 passed / exit 0,
studio 658/658,
npx tsc --noEmit,typecheck:studio,gate:studio,lint -w apps/studioallexit 0 — clean on the first pass this time, no contention reds.
One CI red, classified and re-run green
full test suite (windows)failed once on the merge commit —tests/unit/cache/db-exit-hook.test.ts:17(
Test timed out in 20000ms) andtests/unit/studio/run-store-restart.test.ts:173(a spawned brokerchild reaped as
{code: null, signal: null}instead of{code: 0}). Both are the recurringWindows-runner starvation class already recorded in
known-issues.md— the same 1000×-gap-to-the-ceiling shape as the
migrations-runner.test.ts:173row.Not this diff: neither failing test imports anything from
apps/studioat all(
run-store-restart→src/studio/run-store.js+src/studio/broker-stop.js;db-exit-hook→src/cache/db.js), and this diff touches only two files underapps/studio/src/main/. Every othercheck passed, including
full test suite (ubuntu)and(macos).Re-running the failed job alone, with no code change, went green: run
33009021632conclusionsuccess, 25/25 checks pass. That is the flake reading confirmed rather than assumed, and it isthe cheapest falsifier for "the diff did it" since the diff was byte-identical across both runs. The
new instance is recorded in
known-issues.mdas a third datapoint for that class.