Repository navigation
fix(chat): roll back the dock tab a failed run-in-terminal dispatch minted - #10859
Conversation
|
Intent: A failed Run-in-terminal dispatch must not leave anything behind — the tab it minted (and its PTY) is rolled back unless someone else has taken ownership of it, so retries can never ratchet the dock toward the tab cap. |
UX Review (Fable 5) — ✅ PASSUX-level review of UX-Verdict: PASS Verify once live that the rollback notice lands as the dock tab disappears — this failure path shipped with unit coverage only, no manual run. [UX-REVIEWED] a73362b |
Design Review (Fable 5) — ✅ PASSDesign-level review of Design-Verdict: PASS Confirm follow-up #10977 is actually filed — existing leaked tabs (including #10822's reporter's five) get no relief until hydrate-time reconciliation lands. [DESIGN-REVIEWED] a73362b |
First Principles Review (Fable 5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Verify the 2 s What this change shipsInventory (10 items) — 10 justifiedIntent: FIX — stop failed Run-in-terminal dispatches from permanently leaking dock tabs that ratchet toward the 8-tab cap (#10822).
[FIRST-PRINCIPLES-REVIEWED] a73362b |
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Opus 4.8 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
59ba2aa to
827351c
Compare
827351c to
8c1ca7e
Compare
self-added: yes -- the probe and its catch arrived with this PR. |
self-added: yes -- the probe arrived with this PR. |
self-added: yes -- the absent-session branch arrived with this PR. |
self-added: yes -- the 17 s figure was. |
self-added: yes -- the rollback arrived with this PR. |
self-added: no -- the readiness path predates this PR. |
self-added: yes -- the finding lands in code this PR introduces. |
self-added: yes -- the finding lands in code this PR introduces. |
self-added: yes -- the finding lands in code this PR introduces. |
self-added: yes -- the finding lands in code this PR introduces. |
self-added: yes -- the finding lands in code this PR introduces. |
…inted A run-in-terminal dispatch mints a persisted dock terminal tab, then waits for the PTY's ready frame. When ready never arrived, the dispatch reported failure but left the tab behind: every retry ratcheted the persisted store toward MAX_TERMINALS, where addTab refuses and every later dispatch silently fails (#10822). The dispatch now cleans up after itself on the never-ready path, but only after asking the terminal sessions route whether the shell is still running: a shell profile can replace the readiness hook on a live shell, so a missing ready frame alone does not prove the dispatch died. A running shell keeps its tab and scrollback; a dead or absent one is torn down with the same teardown and order as the tab-close paths. The 6 s deadline becomes a shared 15 s constant tolerant of slow login-shell profile chains, and the code-block button's fallback flash is coupled strictly after it. Fixes #10822
cecef5d to
a73362b
Compare
Adopted the lane's suggested English verbatim: "The command wasn't sent to the new terminal, The existing pin only covered self-added: yes - this string was introduced by this PR (round 6). |
Not taken here because it changes Recorded where the follow-up will read it, since the lane's own suggestion is "fits #10977": self-added: yes - the grace + confirm probe are this PR's (round 5). |
Doing it in this PR would collide head-on with work already in flight. Open That overlap was declared before any code was written (coordination note on #10822) and the self-added: partly - the PR grew the block from ~33 to ~120 lines; the handler predates it. |
iamwhatever
left a comment
There was a problem hiding this comment.
Approved after a full read of the diff on the current head: the change matches the linked issue and body, every code claim was spot-checked against main, tests pin the behaviour, all review lanes are clean and nothing is left undisposed.
`loadPersisted` restored the dock terminal tab list without asking the backend which PTY sessions still exist. A tab leaked by a "Run in terminal" dispatch whose page closed before its rollback deadline (#10822), or one whose shell the orphan reaper has since killed, therefore came back on every reload — holding a slot in the per-panel tab cap and, the moment its view reconnected, minting a fresh shell nobody asked for (the WS route spawns a PTY for an id it does not know). The store now marks the restored set as pending at module init and rules on it against `GET /api/terminal/sessions`, which App already fetches at boot. The ruling takes two looks, the protocol #10859 gave the dispatch-deadline probe: the route skips the placeholder a session holds until its shell spawns, so a session another window is opening reads like one that never existed, and this store is shared across windows. The first answer only names suspects (absent or `alive: false`); an uncached second answer after the opening grace drops the ones still missing and keeps the ones now live. The trimmed list is persisted and focus moves to a survivor. An answer that does not rule — request failure, unrecognized shape, or the feature-disabled reply, which returns an empty list without consulting the registry — keeps every tab, since removing a live shell's scrollback cannot be undone while a kept dead tab is user-closable and reaped server-side. Tabs minted after boot are never candidates. `TerminalTabsView` draws nothing while the ruling is pending. Effect order made that necessary rather than cosmetic: a CliPanel mounted for a restored tab opens its socket before the App-level probe fires, so the probe would find the very shell it was asked about and keep the tab. Closes #10977
`loadPersisted` restored the dock terminal tab list without asking the backend which PTY sessions still exist. A tab leaked by a "Run in terminal" dispatch whose page closed before its rollback deadline (#10822), or one whose shell the orphan reaper has since killed, therefore came back on every reload — holding a slot in the per-panel tab cap and, the moment its view reconnected, minting a fresh shell nobody asked for (the WS route spawns a PTY for an id it does not know). The store now marks the restored set as pending at module init and rules on it against `GET /api/terminal/sessions`, which App already fetches at boot. The ruling takes two looks, the protocol #10859 gave the dispatch-deadline probe: the route skips the placeholder a session holds until its shell spawns, so a session another window is opening reads like one that never existed, and this store is shared across windows. The first answer only names suspects (absent or `alive: false`); an uncached second answer after the opening grace drops the ones still missing and keeps the ones now live. The trimmed list is persisted and focus moves to a survivor. An answer that does not rule — request failure, unrecognized shape, or the feature-disabled reply, which returns an empty list without consulting the registry — keeps every tab, since removing a live shell's scrollback cannot be undone while a kept dead tab is user-closable and reaped server-side. Tabs minted after boot are never candidates. Both looks run under a deadline: the panel stays gated until they settle, so a socket that never answers must be made to answer, and past the bound it reads as a failed probe that keeps every tab. `TerminalTabsView` draws nothing while the ruling is pending. Effect order made that necessary rather than cosmetic: a CliPanel mounted for a restored tab opens its socket before the App-level probe fires, so the probe would find the very shell it was asked about and keep the tab. Closes #10977
…te (#11133) `loadPersisted` restored the dock terminal tab list without asking the backend which PTY sessions still exist. A tab leaked by a "Run in terminal" dispatch whose page closed before its rollback deadline (#10822), or one whose shell the orphan reaper has since killed, therefore came back on every reload — holding a slot in the per-panel tab cap and, the moment its view reconnected, minting a fresh shell nobody asked for (the WS route spawns a PTY for an id it does not know). The store now marks the restored set as pending at module init and rules on it against `GET /api/terminal/sessions`, which App already fetches at boot. The ruling takes two looks, the protocol #10859 gave the dispatch-deadline probe: the route skips the placeholder a session holds until its shell spawns, so a session another window is opening reads like one that never existed, and this store is shared across windows. The first answer only names suspects (absent or `alive: false`); an uncached second answer after the opening grace drops the ones still missing and keeps the ones now live. The trimmed list is persisted and focus moves to a survivor. An answer that does not rule — request failure, unrecognized shape, or the feature-disabled reply, which returns an empty list without consulting the registry — keeps every tab, since removing a live shell's scrollback cannot be undone while a kept dead tab is user-closable and reaped server-side. Tabs minted after boot are never candidates. Both looks run under a deadline: the panel stays gated until they settle, so a socket that never answers must be made to answer, and past the bound it reads as a failed probe that keeps every tab. `TerminalTabsView` draws nothing while the ruling is pending. Effect order made that necessary rather than cosmetic: a CliPanel mounted for a restored tab opens its socket before the App-level probe fires, so the probe would find the very shell it was asked about and keep the tab. Closes #10977
…inted (kirodotdev#10859) A run-in-terminal dispatch mints a persisted dock terminal tab, then waits for the PTY's ready frame. When ready never arrived, the dispatch reported failure but left the tab behind: every retry ratcheted the persisted store toward MAX_TERMINALS, where addTab refuses and every later dispatch silently fails (kirodotdev#10822). The dispatch now cleans up after itself on the never-ready path, but only after asking the terminal sessions route whether the shell is still running: a shell profile can replace the readiness hook on a live shell, so a missing ready frame alone does not prove the dispatch died. A running shell keeps its tab and scrollback; a dead or absent one is torn down with the same teardown and order as the tab-close paths. The 6 s deadline becomes a shared 15 s constant tolerant of slow login-shell profile chains, and the code-block button's fallback flash is coupled strictly after it. Fixes kirodotdev#10822 Co-authored-by: Patrick Gao <patrigao@amazon.com>
…te (kirodotdev#11133) `loadPersisted` restored the dock terminal tab list without asking the backend which PTY sessions still exist. A tab leaked by a "Run in terminal" dispatch whose page closed before its rollback deadline (kirodotdev#10822), or one whose shell the orphan reaper has since killed, therefore came back on every reload — holding a slot in the per-panel tab cap and, the moment its view reconnected, minting a fresh shell nobody asked for (the WS route spawns a PTY for an id it does not know). The store now marks the restored set as pending at module init and rules on it against `GET /api/terminal/sessions`, which App already fetches at boot. The ruling takes two looks, the protocol kirodotdev#10859 gave the dispatch-deadline probe: the route skips the placeholder a session holds until its shell spawns, so a session another window is opening reads like one that never existed, and this store is shared across windows. The first answer only names suspects (absent or `alive: false`); an uncached second answer after the opening grace drops the ones still missing and keeps the ones now live. The trimmed list is persisted and focus moves to a survivor. An answer that does not rule — request failure, unrecognized shape, or the feature-disabled reply, which returns an empty list without consulting the registry — keeps every tab, since removing a live shell's scrollback cannot be undone while a kept dead tab is user-closable and reaped server-side. Tabs minted after boot are never candidates. Both looks run under a deadline: the panel stays gated until they settle, so a socket that never answers must be made to answer, and past the bound it reads as a failed probe that keeps every tab. `TerminalTabsView` draws nothing while the ruling is pending. Effect order made that necessary rather than cosmetic: a CliPanel mounted for a restored tab opens its socket before the App-level probe fires, so the probe would find the very shell it was asked about and keep the tab. Closes kirodotdev#10977
Problem / Motivation
Clicking Run in terminal on a shell code block, while dispatches were failing, filled the bottom dock with terminal tabs that never ran anything. The tabs persist in
localStorage, so they come back on every reload. At 8 tabs (MAX_TERMINALS) the store refuses new tabs, and from then on every Run-in-terminal click fails silently — a 13 px icon flashes for 2 seconds and nothing explains why. A user reported this as "run in terminal opens multiple terminals but doesn't run anything" (#10822; observed live with five leaked tabs, three of them holding dead shells).Why it matters
Two known conditions make failed dispatches routine: a second dashboard window fighting over the PTY sockets (#7638), and a bash profile that assigns
PROMPT_COMMAND, which means the ready frame never arrives at all (#7657). Anyone in either state ratchets toward the cap, and once there the feature is dead until they hand-close the zombie tabs.What changed (motivation → approach → change)
A dispatch mints a dock tab, then waits for the PTY's ready frame. On the never-ready path the old code reported failure and walked away. The tab it had just created stayed in the persisted store forever.
The fix makes the dispatch clean up after itself, but only after it learns whether the shell is actually dead. A missing ready frame does not prove that: the frame rides a
PROMPT_COMMANDhook, and a profile that assigns that variable drops the hook while the shell runs fine. So at the deadline the dispatch reports failure for the button, then asksGET /api/terminal/sessions— which already reports each session'salivefrom the real child-process state — whether this shell is still running. A running shell keeps its tab and its scrollback. A dead or absent one is rolled back with the same teardown, in the same order, as the tab-close paths: delete the backend PTY (best-effort, the orphan reaper backstops it), drop the local socket and cached xterm, remove the store entry. If the probe itself cannot answer, the tab stays, because removing a possibly-live shell is irreversible while a leftover tab is user-closable.Rollback also runs only while the dispatch still owns the tab, checked before the probe and again after it. A tab the user closed is left alone (its close path already deleted the PTY, and a second DELETE would raise a false "close failed" notice), and a panel popped out to its own window is left alone (the popout owns the live session). The deadline keeps its value and moves from a magic
6000to a shared constant,RUN_IN_TERMINAL_READY_DEADLINE_MSinwebsite/src/utils/fenceShell.ts; the code-block button's fallback flash derives from it (RUN_IN_TERMINAL_RESULT_FALLBACK_MS) so the button can never declare failure before the dispatcher has ruled. Waiting longer was considered and rejected: reaching the deadline now only asks whether the shell is alive, and a slow-but-alive one keeps its tab on that answer, so a longer wait would buy nothing but dead air.Four details of the probe itself. It goes through the query cache under a
['terminal-sessions']key, so two dispatches that reach their deadlines together share one request instead of each issuing an uncachedfetch; the reuse window is far shorter than the deadline, so no probe can read a snapshot taken before its own session existed. A session the probe does not list is already gone from the backend registry, so its rollback skips theDELETEthat would 404 and raise a spurious close failure. A probe that cannot answer reports itself to the user throughErrorNotice(showActionError), because a kept tab whose command never ran is otherwise unexplained -- and so does a rollback, because closing a tab the user watched open is the ROUTINE outcome here and must not be the quiet one.And absent is not the same as gone. The backend reserves a session's slot with a null placeholder from
ws.prepare()until the shell is spawned, andGET /api/terminal/sessionsskips placeholders (if sess is None: continue), so a shell still opening reads exactly like one that never existed. Rolling that back would pull the tab out from under a shell about to come up and leave it orphaned until the reaper collects it. The dispatch therefore CONFIRMS an absent session: one uncached re-probe after a bounded grace (RUN_IN_TERMINAL_OPENING_GRACE_MS), and only absent twice counts as gone.flowchart LR subgraph Before A1[dispatch mints tab]:::ctx --> B1[wait 6 s for ready]:::ctx --> C1[report failure]:::ctx --> D1[tab persists forever]:::removed end subgraph After A2[dispatch mints tab]:::ctx --> B2[wait 6 s for ready]:::ctx --> C2{still owns tab?}:::added C2 -->|no: closed or popped out| F2[report failure]:::ctx C2 -->|yes| G2{shell still alive?}:::added G2 -->|yes| H2[keep tab and scrollback]:::added --> F2 G2 -->|cannot tell| I2[keep tab + ErrorNotice]:::added --> F2 G2 -->|no or absent| J2{absent? confirm once}:::added J2 -->|now listed alive| H2 J2 -->|dead or still absent| E2[delete PTY, remove tab]:::added --> F2 end classDef added fill:#DCFCE7,stroke:#16A34A,color:#14532D,stroke-width:2px classDef changed fill:#FEF3C7,stroke:#D97706,color:#78350F,stroke-width:2px classDef removed fill:#FEE2E2,stroke:#DC2626,color:#7F1D1D,stroke-dasharray:4 3 classDef ctx fill:#E0F2FE,stroke:#0284C7,color:#0C4A6E🟩 added · 🟨 changed · 🟥 removed · 🟦 unchanged
A failed dispatch removes the tab it created only when the shell behind it is really gone; a live shell, or one someone else took over, is left alone.
Tests
website/src/test/ChatPageMoreCoverage.test.tsx— nine cases on the dispatch: rollback when the probe reports a dead shell (tab removed, oneDELETE /api/terminal/sessions/:id,ok:false); rollback of an absent session locally with NODELETE, carrying the rollback notice — proven red-first against an unconditional delete and against a silent rollback; a session that is merely OPENING (absent on the first probe, listed alive on the confirm probe) keeps its tab — proven red-first against dropping the confirm probe; keep when the probe reports a live shell with no ready frame (tab and local connection stay, no DELETE,ok:false) — proven red-first against unconditional rollback — carrying the notice that names the live shell, proven red-first against a silent keep; keep plus a surfacedaction-errorwhen the probe fails — proven red-first against a swallowed failure, and pinned to keep the probe's own transport error OUT of that copy, proven red-first against interpolating it; one shared probe request when two dispatches deadline together — proven red-first against a zero-length reuse window; keep when ready arrives and the command is sent (ok:true); leave-alone when the user closed the tab or popped the panel out before ready.website/src/i18n/locales/*— both messages in the English source, all eleven translations, and the regenerated pseudolocale; the eleven per-locale style suites underwebsite/src/i18n/style/pass, including the zh-CN full-width-punctuation rule. This round also repairs three misspellings of the Korean word for terminal that earlier rounds of this PR introduced — the file spells it correctly 50 times now, 46 of them pre-existing.website/src/test/fenceShell.test.ts— pinsRUN_IN_TERMINAL_RESULT_FALLBACK_MS > RUN_IN_TERMINAL_READY_DEADLINE_MS.website/src/test/RunInTerminalBtn.test.tsx— the button timeout test advances by the shared constant..github/coverage-baselines/frontend.txt— drops thesrc/hooks/useBottomTerminal.tsentry, which now measures above the 80% floor.Manual verification
N/A — unit coverage sufficient: every dispatch outcome is pinned against the real store, registry, and handler, with the sessions probe stubbed at
fetch; no rendering changed.Screenshots / video
Why no screenshot: failure-path tab lifecycle only. The user-visible additions render through the existing
ErrorNoticebanner with its existing styling — a new message string in an already-shipped surface, no new component, layout, or theme delta — and both the surfacing and the string are pinned by unit tests.Accepted residual
A
PROMPT_COMMAND-assigning profile (#7657) still leaves a live shell whose ready frame never arrives: the probe answersalive === true, so the tab is kept and the command is never sent. That is deliberate and is the safe half of the trade — the alternative destroys working shells — but it is a residual, not a fix. Repairing the readiness signal itself belongs to the terminal transport, which is #7638's parked RFC territory, and is out of scope here.What this branch no longer does is stay quiet about it. For such a profile this is the routine outcome, not an edge, so a 2s button flash cannot carry it: the kept tab now says the command was not sent and that its shell is running, which also tells the user the shell is theirs to run the command in.
Scope boundary — what this does NOT fix
This fixes the in-page accumulation path only: the dispatch that minted a tab rolls it back, so a click that fails stops leaving a tab behind. Two sibling paths stay open and are tracked in #10977 rather than implied here:
loadPersisted(website/src/hooks/useBottomTerminal.ts:77) restores persisted tabs without asking the backend which sessions still exist, so tabs leaked before this ships — or by a page closed mid-dispatch — return as dead tabs and keep consuming the cap.GET /api/terminal/sessionsomits theNoneplaceholder a session holds betweenws.prepare()and its shell spawn, so absence alone cannot tell opening from gone. The opening grace plus its confirm probe exist only to cover that gap; listing placeholders asopening: truewould make absence authoritative and delete both. That is a backend change inside Terminal output can be delayed when multiple windows attach to one PTY #7638's parked surface, so it is tracked in Reconcile persisted terminal dock tabs with the backend at hydrate time #10977, not taken here.Related Issues
Fixes #10822
Follow-up: #10977 (hydrate-time reconciliation + the silent tab cap)
Context: #7638 (socket takeover, parked RFC #7649), #7657 (
PROMPT_COMMAND-replacing profiles)Pattern harvest
Rule candidate: review-prompt
Pattern: "an async dispatch that creates a persistent resource must roll it back on its failure path, gated on still owning the resource AND on authoritative evidence the resource is dead — a missing signal is not proof of death"
Checklist
feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)