fix(terminal): tear down runtime-owned headless PTYs on whole-tab close (#9114)

* fix(terminal): tear down runtime-owned headless PTYs on whole-tab close

#8958 placed the closeTerminalTab renderer relay ahead of the authoritative
headless teardown in closeMobileSessionTab. Production always wires
closeTerminalTab, so the teardown became dead code for whole-parent closes
whenever a window is attached. The relay resolves close targets only from
renderer state (resolveTerminalCloseTarget), which never contains a
runtime-owned headless tab, so it acks success without killing the PTY —
leaking SSH-durable/serve PTYs and letting syncMobileSessionTabs republish
the "closed" tab (the exact case the removed comment warned about).

Run the isRuntimeOwnedHeadlessMobileTab teardown before the relay so headless
tabs are killed and pruned authoritatively even with a renderer attached;
normal adopted tabs still take the durable closeTerminalTab path. Add a
regression test that wires both notifiers (prior tests covered only the
no-notifier and in-graph cases).

* fix(terminal): preserve adopted tab close guards

* fix(runtime): preserve headless close durability on notifier failure
This commit is contained in:
Brennan Benson
2026-07-16 18:41:49 -07:00
committed by GitHub
parent f6f2561623
commit 91ece6ca7e
2 changed files with 157 additions and 9 deletions
+29 -9
View File
@@ -4685,26 +4685,33 @@ export class OrcaRuntimeService {
(candidate) => candidate.type === 'terminal' && candidate.parentTabId === tab.parentTabId
).length
const closingWholeParent = tab.id !== tabId || parentLeafCount <= 1
// Why: a runtime-owned headless tab is absent from renderer state, so the
// closeTerminalTab relay below would ack success without killing its PTY,
// and syncMobileSessionTabs would republish the "closed" tab. Only bypass
// the relay when no renderer owns the parent: an adopted tab needs the
// renderer's live pin guard and durable close transaction.
if (closingWholeParent && !this.tabs.has(tab.parentTabId)) {
this.closeHeadlessMobileTerminalTab(worktreeId, snapshot!, tab)
this.notifyRendererOfHeadlessTerminalClose(tab.parentTabId)
this.store?.flushOrThrow?.()
return { closed: true }
}
if (closingWholeParent && this.notifier?.closeTerminalTab) {
// Why: whole-tab close is a lifecycle transaction. The renderer reply
// arrives only after canonical retirement and a forced session flush.
await this.notifier.closeTerminalTab(tab.parentTabId)
return { closed: true }
}
if (!this.notifier?.closeTerminal) {
// Why: notifier implementations without the acknowledged relay may expose
// only raw pane close. Runtime-owned parents still need de-persist + kill.
if (closingWholeParent && this.isRuntimeOwnedHeadlessMobileTab(worktreeId, tab)) {
this.closeHeadlessMobileTerminalTab(worktreeId, snapshot!, tab)
this.notifyRendererOfHeadlessTerminalClose(tab.parentTabId)
this.store?.flushOrThrow?.()
return { closed: true }
}
// Why: a runtime-owned headless tab whose whole parent is being closed must
// be torn down authoritatively even with a renderer attached — kill the
// PTY, drop the persisted binding, and prune+emit — or syncMobileSessionTabs
// keeps republishing the "closed" tab with a live PTY. Best-effort notify the
// renderer too so any adopted pane closes (no dead pane). A single split leaf
// (exact id, multi-leaf parent) keeps the per-leaf path so siblings survive.
if (closingWholeParent && this.isRuntimeOwnedHeadlessMobileTab(worktreeId, tab)) {
if (!this.notifier?.closeTerminal) {
this.closeHeadlessMobileTerminalTab(worktreeId, snapshot!, tab)
this.notifier?.closeTerminal(tab.parentTabId)
this.store?.flushOrThrow?.()
return { closed: true }
}
@@ -4732,6 +4739,19 @@ export class OrcaRuntimeService {
return { closed: true }
}
private notifyRendererOfHeadlessTerminalClose(parentTabId: string): void {
// Why: this relay is advisory after main owns teardown; renderer failure must
// not prevent the authoritative session flush or turn the close into failure.
try {
this.notifier?.closeTerminal(parentTabId)
} catch (error) {
console.warn('[runtime] failed to notify renderer after headless terminal close', {
parentTabId,
error
})
}
}
private async closeHeadlessMobileBrowserTab(
worktreeId: string,
snapshot: RuntimeMobileSessionTabsSnapshot,