fix(terminal): keep pty alive across conversation switches via park frame (#323)

Switching from session A to B unmounted A's TerminalView, whose socket
close the host treated as a transient drop and started the 30s reconnect-
grace countdown — leaving the user in B for >30s killed A's shell, so
switching back respawned a fresh one instead of reattaching.

Add a third control frame {type:'park'} distinct from refresh/crash
drops: the client sends it on unmount when the tab is still open in its
session's state but the active session has moved on; the host marks the
pty as parked and skips the grace countdown. Switching back (open()
cancels the parked state) or explicitly closing the tab (close frame's
scheduleClose clears it) resume the normal lifecycle. Agent terminals
skip park — their lifetime is already indefinite on a bare drop.
This commit is contained in:
多玩幻灵qwq authored and GitHub committed 2026-08-22 17:21:27 +08:00
1 parent b7aab48deb
commit 29b8615bff
4 files changed
+163 -19

No files matched your search

+22 -4
View File
@@ -895,6 +895,10 @@ async function attachAgentList(
* - `?tab=...&sessionId=...` attaches to a UI-tab terminal (the user
* created it from the + menu). The close frame schedules a 0-ms close
* (the host's reconnect grace keeps the shell alive across a refresh).
* The park frame (sent when the user switches to another conversation)
* marks the pty as parked so the upcoming bare socket drop does NOT start
* the grace countdown — the tab is still open in its session's state, so
* the shell must survive until the user switches back or closes the tab.
*/
async function attachTerminal(
ctx: Context,
@@ -971,6 +975,16 @@ async function attachTerminal(
ptyManager.scheduleClose(handle.key, 0)
return
}
if (control !== null && control.type === 'park') {
// The user switched to another conversation: the tab is still open in
// its session's persisted state, but its view unmounted. Park the pty
// so the upcoming bare socket drop does NOT start the reconnect-grace
// countdown — the pty stays alive until the user switches back (a
// reconnecting view clears the parked state) or explicitly closes the
// tab (a close frame's scheduleClose clears it).
ptyManager.park(handle.key)
return
}
if (handle.exited) return
if (
control !== null
@@ -986,10 +1000,14 @@ async function attachTerminal(
ws.on('close', () => {
dataSub.dispose()
exitSub.dispose()
// A bare socket drop (refresh, tab switch) leaves the process alive
// for a grace period so a quick reconnect keeps it; the reconnect's
// open() cancels the pending close.
ptyManager.scheduleClose(handle.key, resolved.reconnectGraceMs)
// A parked pty (the user switched conversations and sent `{type:'park'}`)
// stays alive indefinitely — do NOT start the grace countdown. A bare
// socket drop without a prior park (refresh, crash) starts the grace
// period so a quick reconnect keeps the process; the reconnect's open()
// cancels the pending close.
if (!ptyManager.isParked(handle.key)) {
ptyManager.scheduleClose(handle.key, resolved.reconnectGraceMs)
}
})
} catch (error) {
ws.close(1011, error instanceof Error ? error.message : String(error))