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(restart): bound the pane query by the deadline; guard the synchronous-echo delivery path

enes2159e92359ea
diff --git a/src/renderer/terminal/agent-restart.test.ts b/src/renderer/terminal/agent-restart.test.ts
index 5fdaaf235..9e5bacaa6 100644
--- a/src/renderer/terminal/agent-restart.test.ts
+++ b/src/renderer/terminal/agent-restart.test.ts
@@ -114,6 +114,21 @@ describe('performRestartResume', () => {
expect(written.join('')).not.toContain('--resume')
})
+ it('gives up when the pane query itself never settles', async () => {
+ const { written, io } = fakeIo()
+ const p = performRestartResume({
+ agentId: 'claude',
+ sessionId: 'sid-1',
+ io,
+ paneCommand: () => new Promise<string | null>(() => {}), // wedged tmux server: never settles
+ timeoutMs: 1000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(2000)
+ expect(await p).toBe('exit-timeout')
+ expect(written.join('')).not.toContain('--resume')
+ })
+
it('refuses an agent without an exit sequence', async () => {
const { io } = fakeIo()
expect(
@@ -125,6 +140,19 @@ describe('performRestartResume', () => {
})
).toBe('not-eligible')
})
+
+ it('refuses — without writing the exit — a session id we could never resume into', async () => {
+ const { written, io } = fakeIo()
+ expect(
+ await performRestartResume({
+ agentId: 'claude',
+ sessionId: '-bad', // rejected by resumeCommand's SAFE_SESSION_ID
+ io,
+ paneCommand: async () => 'zsh'
+ })
+ ).toBe('not-eligible')
+ expect(written).toEqual([]) // quitting a CLI we cannot resume would just lose the session
+ })
})
describe('agent restart registry', () => {
diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.ts
index 1699e1277..13e5b7ce0 100644
--- a/src/renderer/terminal/agent-restart.ts
+++ b/src/renderer/terminal/agent-restart.ts
@@ -81,10 +81,23 @@ export async function performRestartResume(d: {
for (;;) {
await new Promise((r) => setTimeout(r, pollMs))
let pane: string | null = null
+ let lapse: ReturnType<typeof setTimeout> | undefined
try {
- pane = await d.paneCommand()
+ // The query must be bounded, not just its rejection: a wedged tmux server can leave it
+ // pending forever, and the deadline below is only read once it settles — an unbounded
+ // await would hang this restart, and with it the bulk run's summary, permanently. A
+ // lapsed query resolves null, which reads as "not a shell yet".
+ const remaining = Math.max(0, deadline - Date.now())
+ pane = await Promise.race([
+ d.paneCommand(),
+ new Promise<null>((r) => {
+ lapse = setTimeout(() => r(null), remaining)
+ })
+ ])
} catch {
// transient IPC failure — keep polling until the deadline
+ } finally {
+ clearTimeout(lapse)
}
if (isShellCommand(pane)) break
if (Date.now() > deadline) return 'exit-timeout'
diff --git a/src/renderer/terminal/command-delivery.test.ts b/src/renderer/terminal/command-delivery.test.ts
index f68cd3062..f1506cbdd 100644
--- a/src/renderer/terminal/command-delivery.test.ts
+++ b/src/renderer/terminal/command-delivery.test.ts
@@ -27,6 +27,29 @@ function fakeIo() {
}
}
+/** A transport that echoes back from INSIDE write() — the shape the in-place restart
+ * choreography feeds deliverCommand. Every other case here uses an io that never echoes on
+ * write, so only this one can catch a re-entrant submit. */
+function echoingIo() {
+ const writes: string[] = []
+ let cb: ((chunk: string) => void) | undefined
+ return {
+ writes,
+ io: {
+ write: (d: string) => {
+ writes.push(d)
+ cb?.(d)
+ },
+ onData: (fn: (chunk: string) => void) => {
+ cb = fn
+ return () => {
+ cb = undefined
+ }
+ }
+ }
+ }
+}
+
describe('cleanEcho', () => {
it('strips CSI, OSC and other escape sequences plus line breaks', () => {
const noisy = '\x1b[1;32mprompt\x1b[0m \x1b]0;title\x07ec' + '\r\n' + 'ho text\x1b[K'
@@ -86,6 +109,17 @@ describe('deliverCommand', () => {
expect(f.writes).toEqual([CMD])
})
+ it('submits once against an io that echoes synchronously inside write', () => {
+ const f = echoingIo()
+ deliverCommand(f.io, CMD)
+ // The echo lands while write() is still on the stack: submit must already be closed, or it
+ // re-enters this listener (the tail still matches) and Enters forever.
+ expect(f.writes).toEqual([CMD, '\r'])
+ expect(f.writes.filter((w) => w === '\r')).toHaveLength(1)
+ // ...and a delivery finished inside write() must leave no verify timer behind.
+ expect(vi.getTimerCount()).toBe(0)
+ })
+
it('ignores echo arriving after submit (no double Enter)', () => {
const f = fakeIo()
deliverCommand(f.io, CMD)
diff --git a/src/renderer/terminal/command-delivery.ts b/src/renderer/terminal/command-delivery.ts
index 89c5819f9..339a814ed 100644
--- a/src/renderer/terminal/command-delivery.ts
+++ b/src/renderer/terminal/command-delivery.ts
@@ -51,9 +51,9 @@ export function deliverCommand(io: DeliveryIo, cmd: string): () => void {
if (timer) clearTimeout(timer)
unsub?.()
}
- // Close the delivery BEFORE writing Enter: an io whose write echoes back synchronously
- // (the restart choreography's pane io does) would otherwise re-enter this listener while
- // the tail still matches and submit forever.
+ // Close the delivery BEFORE writing Enter: an io whose write echoes back synchronously (the
+ // in-place restart choreography feeds one) would otherwise re-enter the listener below while
+ // the tail still matches, and submit forever.
const submit = (): void => {
finish()
io.write('\r')
@@ -62,7 +62,8 @@ export function deliverCommand(io: DeliveryIo, cmd: string): () => void {
if (done) return
attempt += 1
echoed = ''
- io.write(cmd)
+ // Arm the verify timer BEFORE the write, for the same synchronous-echo io: an echo landing
+ // inside write() finishes the delivery, and a timer armed after that would outlive it.
timer = setTimeout(() => {
if (done) return
if (attempt >= DELIVERY_ATTEMPTS) {
@@ -72,6 +73,7 @@ export function deliverCommand(io: DeliveryIo, cmd: string): () => void {
io.write(KILL_LINE)
tryOnce()
}, VERIFY_TIMEOUT_MS)
+ io.write(cmd)
}
unsub = io.onData((chunk) => {