Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(pty): a failed has-session probe is not a cold start

enes06b44ac81cf6
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index b3f650bf9..66247f68f 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -18,6 +18,7 @@ import {
import { findCommand, tmuxInstall } from './tmux-hint'
import { hookServer } from './agents/hook-server'
import {
+ probeSaysAbsent,
remoteHookEnvArgs,
remoteTmuxHasSessionArgs,
remoteTmuxKillArgs,
@@ -883,18 +884,20 @@ export class PtyManager {
}
/** Does the node's remote tmux session exist (over the project's ControlMaster)? Async so the
- * network round-trip never blocks the main event loop. */
+ * network round-trip never blocks the main event loop. A probe that FAILED for transport
+ * reasons answers "exists": only tmux's own exit 1 is evidence of absence (probeSaysAbsent) —
+ * a dead/reconnecting master read as "cold" typed a resume command into a live agent session. */
private async remoteSessionExists(
sshRemote: NonNullable<PtyCreateOptions['sshRemote']>,
sessionId: string
): Promise<boolean> {
const ssh = findSsh()
- if (!ssh) return false
+ if (!ssh) return true // can't probe → not evidence of absence; warm attach types nothing
try {
await runAsync(ssh, remoteTmuxHasSessionArgs(sshRemote.conn, sshRemote.controlPath, sessionId))
return true
- } catch {
- return false
+ } catch (e) {
+ return !probeSaysAbsent(e)
}
}
@@ -927,8 +930,11 @@ export class PtyManager {
try {
await runAsync(this.tmuxPath, ['-L', TMUX_SOCKET, 'has-session', '-t', sessionName(persistKey)])
return true
- } catch {
- return false
+ } catch (e) {
+ // Same discrimination as the remote probe: tmux's exit 1 (no session / no server —
+ // the reboot case) is absence; a spawn failure (EAGAIN under a bulk project load) is
+ // not, and cold-restoring on it would type into a live session.
+ return !probeSaysAbsent(e)
}
}
diff --git a/src/core/remote-ssh/control-master.test.ts b/src/core/remote-ssh/control-master.test.ts
index 3a829db91..ab71ec883 100644
--- a/src/core/remote-ssh/control-master.test.ts
+++ b/src/core/remote-ssh/control-master.test.ts
@@ -5,6 +5,7 @@ import {
masterArgs,
childArgs,
remoteTmuxHasSessionArgs,
+ probeSaysAbsent,
remoteCapturePaneArgs,
remoteTmuxPtyArgs,
listDirArgs,
@@ -185,3 +186,20 @@ describe('listDirArgs', () => {
])
})
})
+
+describe('probeSaysAbsent (has-session probe discrimination)', () => {
+ it('only tmux\'s own exit 1 is evidence of absence', () => {
+ expect(probeSaysAbsent(Object.assign(new Error('exit 1'), { code: 1 }))).toBe(true)
+ })
+ it('transport/spawn failures are a failed READ, never absence', () => {
+ // ssh's own failure (dead or mid-reconnect ControlMaster).
+ expect(probeSaysAbsent(Object.assign(new Error('exit 255'), { code: 255 }))).toBe(false)
+ // tmux off the remote PATH.
+ expect(probeSaysAbsent(Object.assign(new Error('exit 127'), { code: 127 }))).toBe(false)
+ // spawn-level errors carry a string code, or none at all.
+ expect(probeSaysAbsent(Object.assign(new Error('spawn'), { code: 'EAGAIN' }))).toBe(false)
+ expect(probeSaysAbsent(new Error('plain'))).toBe(false)
+ expect(probeSaysAbsent(undefined)).toBe(false)
+ expect(probeSaysAbsent(null)).toBe(false)
+ })
+})
diff --git a/src/core/remote-ssh/control-master.ts b/src/core/remote-ssh/control-master.ts
index 3ea329ce4..e5daaf650 100644
--- a/src/core/remote-ssh/control-master.ts
+++ b/src/core/remote-ssh/control-master.ts
@@ -75,6 +75,18 @@ export function exitMasterArgs(conn: SshConnection, controlPath: string): string
export function remoteTmuxHasSessionArgs(conn: SshConnection, controlPath: string, sessionId: string): string[] {
return childArgs(conn, controlPath, `tmux -L ${RMT_TMUX_SOCKET} has-session -t ${sessionId}`)
}
+/**
+ * Did a FAILED `tmux has-session` probe actually say the session is absent? Only tmux's own
+ * exit 1 ("can't find session" / "no server running") does. Anything else — ssh's 255 (a dead
+ * or mid-reconnect ControlMaster), 127 (tmux off the remote PATH), a spawn error with no
+ * numeric code — is a failed READ, and a failed read is never evidence of absence. Reading a
+ * transport failure as "cold" made the desktop replay scrollback and type `claude --resume …`
+ * into a LIVE fullscreen Claude session; the mangled line then landed in the agent's
+ * conversation as a user prompt (field report). Unknown ⇒ warm attach, which types nothing.
+ */
+export function probeSaysAbsent(err: unknown): boolean {
+ return (err as { code?: unknown } | null)?.code === 1
+}
export function remoteTmuxKillArgs(conn: SshConnection, controlPath: string, sessionId: string): string[] {
return childArgs(conn, controlPath, `tmux -L ${RMT_TMUX_SOCKET} kill-session -t ${sessionId}`)
}