Skip to content

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 into
studio-handofffrom
sd-135-fix-studio-tab-attached-can-land
Aug 26, 2026
Merged

KnockOutEZ merged 3 commits into
studio-handofffrom
sd-135-fix-studio-tab-attached-can-land

Conversation

@KnockOutEZ

@KnockOutEZ KnockOutEZ commented Aug 26, 2026 •

Copy link
Copy Markdown
Owner

Closes KnockOutEZ/wigolo-studio-run#135 (SD1 exit-16, MED).

What was wrong

applyAttach was the one lifecycle write on RunViewModel with no terminal-run guard — its three
siblings (applyVisibility, applyRequestDecision, applyResolveDecision) all refuse past the
terminal 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 status live before the run exists and
never re-checked it when its awaits resumed.

  • Arm 1 — the quit lands between open()'s awaits. shutdown() finds the live context, ends the
    run cancelled, and open() 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-only
    log with no prune path. The agent's half was worse — { session_id } for a session
    contexts.clear() had already destroyed, so every next call failed wrong_session.
  • Arm 2 — the quit lands before createRun resolves. runForSession finds nothing, shutdown()
    returns without ending anything, and the run open() then creates sits running forever: counted
    by the tray, returned by GET /v1/runs?status=running. Exactly the orphan open()'s own rollback
    comment exists to prevent.

What changed

  1. applyAttach refuses on a terminal run, and on a run this process is not holding, without
    appending — a throw (RunNotOpenError) rather than the siblings' silent return, because attach is
    the one of the four whose caller acts on the answer.
  2. 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 has already passed a projection
    run.cancelled is about to move. Joining the lanes deadlocks (applyEndRun holds the run lane
    while 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 applyAttach publishes its
    in-flight append and applyEndRun waits on that set — one-directionally — before its membership
    read.
  3. endRun is idempotent against the terminal event, for the same defect from the other side: a quit
    that cancels a run whose open() is in flight leaves that open's rollback writing run.cancelled
    on top of run.cancelled.
  4. open() re-checks the context after every await and unwinds what it built — including after the
    profile 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 → SD1 wigolo-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 a
broker round-trip puts them in the real app. N clean opens reach neither window.

✓ studio host — a quit racing an in-flight open (SD1 exit-16) > reports a structured failure instead of a session id when the quit lands between open’s awaits
✓ studio host — a quit racing an in-flight open (SD1 exit-16) > leaves no run stuck running when the quit lands before createRun resolves
✓ studio host — a quit racing an in-flight open (SD1 exit-16) > never lets an attach on the wire commit behind the terminal event a quit writes
✓ studio host — a quit racing an in-flight open (SD1 exit-16) > never navigates a session the quit destroyed while its attach was on the wire
✓ studio host — a quit racing an in-flight open (SD1 exit-16) > reports a structured failure when the quit lands during the gated startUrl navigation
✓ studio host — a quit racing an in-flight open (SD1 exit-16) > leaves neither run running when one quit races two opens in different interleaves
✓ RunViewModel — tab↔run ownership is the run log … > refuses to attach a tab to a run that has already ended, and writes no event
✓ RunViewModel — tab↔run ownership is the run log … > refuses to attach a tab to a run this process is not holding
✓ RunViewModel — tab↔run ownership is the run log … > writes nothing when a run that has already ended is ended again

Every guard mutated red, each reversed after

Guard removed Red
applyAttach terminal refusal refuses to attach a tab to a run that has already ended
applyAttach missing-projection refusal refuses to attach a tab to a run this process is not holding
applyEndRun terminal idempotence 4 red — the double-run.cancelled pin plus 3 quit-race arms
applyEndRun in-flight-attach barrier never 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 log
open() re-check after createRun both arm-1 and arm-2 rows
open() re-check after attachTab never navigates a session the quit destroyed while its attach was on the wire
open() re-check after the gated nav reports a structured failure when the quit lands during the gated startUrl navigation

The 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 []), and
that arm carries a positive control proving the fixture does navigate when no quit intervenes.

Suites

Gate Result
root npm test 12 002 passed / 20 skipped / 7 todo, exit 0
apps/studio npm test 658 / 658, exit 0
RUN_STUDIO_E2E=1 npm run test:e2e 117 passed / 21 opt-skip, exit 0
npx tsc --noEmit exit 0
npm run typecheck:studio exit 0
npm run gate:studio exit 0
npm run lint -w apps/studio exit 0

Two 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 rerun
clean). Both recorded in known-issues.md.

Territory

apps/studio/src/main/** (lane studio-main) + apps/studio/tests/** (shared). No lane-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 by shutdown() awaiting
in-flight opens, precisely so the constant deadline is untouched.

Re-verified after merging origin/studio-handoff (e17eba10)

studio-handoff moved ahead by 3 merges while this was in review (#482–#485, none touching these
files). 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/studio all
exit 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) and tests/unit/studio/run-store-restart.test.ts:173 (a spawned broker
child reaped as {code: null, signal: null} instead of {code: 0}). Both are the recurring
Windows-runner starvation class already recorded in known-issues.md — the same 1000×-gap-to-the-
ceiling shape as the migrations-runner.test.ts:173 row.

Not this diff: neither failing test imports anything from apps/studio at 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 under apps/studio/src/main/. Every other
check passed, including full test suite (ubuntu) and (macos).

Re-running the failed job alone, with no code change, went green: run 33009021632 conclusion
success, 25/25 checks pass. That is the flake reading confirmed rather than assumed, and it is
the cheapest falsifier for "the diff did it" since the diff was byte-identical across both runs. The
new instance is recorded in known-issues.md as a third datapoint for that class.

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.
@coderabbitai

coderabbitai Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2a0133ca-fe63-49b3-a150-bad048dea1ab

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@KnockOutEZ
KnockOutEZ merged commit 216fc59 into studio-handoff Aug 26, 2026
46 of 47 checks passed
@KnockOutEZ
KnockOutEZ deleted the sd-135-fix-studio-tab-attached-can-land branch August 26, 2026 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant