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): owe a slow-but-readable pane its minimum exit samples

review3a390c474c45
diff --git a/src/renderer/terminal/agent-restart.test.ts b/src/renderer/terminal/agent-restart.test.ts
index 04c8c1209..3411504c2 100644
--- a/src/renderer/terminal/agent-restart.test.ts
+++ b/src/renderer/terminal/agent-restart.test.ts
@@ -17,6 +17,7 @@ import {
planBulkRestart,
RESTART_EXIT_TIMEOUT_MS,
RESTART_LATE_EXIT_MS,
+ RESTART_MIN_EXIT_SAMPLES,
BULK_RESTART_CONCURRENCY,
registerAgentHibernate,
registerAgentRestart,
@@ -1817,3 +1818,62 @@ describe('settleRecycledNode — the recycle outlives its canvas', () => {
expect(t.calls).toContain('dropPark')
})
})
+
+// A Windows session-host pane is read through a PowerShell process-table walk, ~1.5 s per sample
+// when cold. The 6 s window alone then holds two or three samples, and a CLI that quit at ~8 s
+// (6.5 s after the exit was written) would be reported as one that refused.
+describe('performExitPhase on a SLOW pane read (Windows session host)', () => {
+ beforeEach(() => vi.useFakeTimers())
+ afterEach(() => vi.useRealTimers())
+
+ const slowPane = (exitAtMs: number, running: string, shell: string) => {
+ const start = Date.now()
+ let calls = 0
+ return {
+ calls: () => calls,
+ read: () => {
+ calls++
+ return new Promise<string | null>((r) =>
+ setTimeout(() => r(Date.now() - start >= exitAtMs ? shell : running), 1500)
+ )
+ }
+ }
+ }
+
+ it.each(['pwsh.exe', 'powershell.exe', 'cmd.exe'])(
+ 'takes its minimum samples and sees %s take the pane back',
+ async (shell) => {
+ const { io } = fakeIo()
+ const pane = slowPane(8000, 'node.exe', shell)
+ const p = performExitPhase({ agentId: 'gemini', sessionId: 'sid-1', io, paneCommand: pane.read })
+ await vi.advanceTimersByTimeAsync(20_000)
+ expect(await p).toBe('exited')
+ expect(pane.calls()).toBeGreaterThanOrEqual(1 + RESTART_MIN_EXIT_SAMPLES)
+ }
+ )
+
+ it('a slow pane whose CLI never quits still ends, bounded, with nothing killed', async () => {
+ const { written, io } = fakeIo()
+ const pane = slowPane(Number.POSITIVE_INFINITY, 'node.exe', 'pwsh.exe')
+ const p = performExitPhase({ agentId: 'gemini', sessionId: 'sid-1', io, paneCommand: pane.read })
+ await vi.advanceTimersByTimeAsync(RESTART_EXIT_TIMEOUT_MS + RESTART_MIN_EXIT_SAMPLES * 3500)
+ expect(await p).toBe('exit-timeout')
+ expect(written).toEqual(['\x15', '/quit\r'])
+ })
+
+ it('a fast (tmux) pane is untouched by the floor: it times out at the window', async () => {
+ const { io } = fakeIo()
+ let settled = false
+ const p = performExitPhase({
+ agentId: 'gemini',
+ sessionId: 'sid-1',
+ io,
+ paneCommand: async () => 'gemini',
+ timeoutMs: 1000,
+ pollMs: 100
+ }).finally(() => (settled = true))
+ await vi.advanceTimersByTimeAsync(1150)
+ expect(settled).toBe(true)
+ expect(await p).toBe('exit-timeout')
+ })
+})
diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.ts
index 91aff7506..e7a1f093f 100644
--- a/src/renderer/terminal/agent-restart.ts
+++ b/src/renderer/terminal/agent-restart.ts
@@ -150,6 +150,17 @@ export type RestartOutcome = 'restarted' | 'exit-timeout' | 'not-eligible' | 'ho
export const RESTART_EXIT_TIMEOUT_MS = 6000
export const RESTART_POLL_MS = 250
+/**
+ * The fewest READABLE post-exit samples the exit phase takes before it may report `'exit-timeout'`.
+ * A tmux pane answers in ~4 ms, so the 6 s window holds ~24 samples and this floor never binds. A
+ * Windows session-host pane is a process-table walk through PowerShell — up to ~1.5 s cold — and
+ * there the wall-clock window alone could close after two or three samples, reporting a CLI that
+ * quit at 4 s as one that refused. While the pane keeps ANSWERING (slowly), each owed sample gets at
+ * least `max(window left, timeoutMs / 2)`; a pane that stops answering ends the run exactly as
+ * before. Bounded: at most this many samples are owed, and nothing is ever killed.
+ */
+export const RESTART_MIN_EXIT_SAMPLES = 4
+
/**
* How much longer a user-asked RESTART keeps watching after RESTART_EXIT_TIMEOUT_MS, as long as
* the pane can still be read (issue #899). The exit is irreversible and the resume is the only way
@@ -326,13 +337,22 @@ export async function performExitPhase(d: {
// Equal to `deadline` when no late window was asked for, so every bound below is unchanged.
const lateDeadline = deadline + Math.max(0, d.lateExitMs ?? 0)
let last: string | null = null
+ // RESTART_MIN_EXIT_SAMPLES: readable samples so far, and whether the previous one was readable.
+ // The pre-flight's reading counts as "readable" for the first poll's budget, not as a sample.
+ let samples = 0
+ let readable = true
for (;;) {
await new Promise((r) => setTimeout(r, pollMs))
// Each query is bounded by the window it runs in: a query wedged inside the base window still
// lapses at `deadline` (and its null ends the run there), exactly as without a late window.
const until = Date.now() <= deadline ? deadline : lateDeadline
- const pane = await queryPaneWithin(d.paneCommand, Math.max(0, until - Date.now()))
+ const left = Math.max(0, until - Date.now())
+ // A pane that has been answering, just slowly, is owed its minimum samples even past the window.
+ const owedSample = readable && samples < RESTART_MIN_EXIT_SAMPLES
+ const pane = await queryPaneWithin(d.paneCommand, owedSample ? Math.max(left, timeoutMs / 2) : left)
if (gone()) return 'not-eligible' // stop polling a pane that no longer exists
+ readable = pane !== null
+ if (readable) samples++
// Two ways to know the CLI let go of the pane. The allowlist is the confident one and is
// taken immediately. The other — "the foreground command is no longer what it was before the
// exit" — covers the shells the allowlist cannot know (`xonsh`, `elvish`, anything the
@@ -346,7 +366,13 @@ export async function performExitPhase(d: {
// Past the base window, keep going only inside the late window and only while the pane is
// still readable — i.e. the CLI we asked to quit is visibly still there (or a changed reading
// is waiting for its confirming second poll). A pane we can no longer see is not "slow".
- if (Date.now() >= deadline && (Date.now() > lateDeadline || pane === null)) return 'exit-timeout'
+ if (
+ Date.now() >= deadline &&
+ (Date.now() > lateDeadline || pane === null) &&
+ // Still readable and under the floor: one more (slow) sample before giving up.
+ !(pane !== null && samples < RESTART_MIN_EXIT_SAMPLES)
+ )
+ return 'exit-timeout'
}
}
diff --git a/src/renderer/terminal/hibernation-policy.test.ts b/src/renderer/terminal/hibernation-policy.test.ts
index e93f80699..c2d4d7b9f 100644
--- a/src/renderer/terminal/hibernation-policy.test.ts
+++ b/src/renderer/terminal/hibernation-policy.test.ts
@@ -1,4 +1,7 @@
import { describe, it, expect } from 'vitest'
+import { isShellCommand } from '../../shared/agents/pane'
+import type { PaneOwner } from '../../shared/agents/pane-owner-predicate'
+import { decideWakeResume } from './wake-identity'
import {
planHibernation,
shouldAutoWake,
@@ -176,3 +179,40 @@ describe('shouldAutoWake — the mount timer / visibility edge / kanban modal-op
expect(shouldAutoWake(undefined, undefined)).toBe(false)
})
})
+
+// Eco is platform-neutral: a Windows session-host pane goes through exactly the policy a tmux pane
+// does, and the wake has to recognise the Windows shells the exit settled on.
+describe('Eco on Windows (session host) — the same policy as tmux', () => {
+ it('a candidate that differs only by backend gets the same verdict', () => {
+ const tmux = [base('a', { lastEventAt: 1 }), base('b', { lastEventAt: 2, offscreen: false })]
+ const host = tmux.map((c) => ({ ...c, backend: 'session-host', platform: 'win32' }))
+ expect(planHibernation(host, NOW, cfg)).toEqual(planHibernation(tmux, NOW, cfg))
+ expect(planHibernation(host, NOW, cfg)).toEqual(['a'])
+ })
+
+ it.each(['pwsh.exe', 'PWSH.EXE', 'powershell.exe', 'cmd.exe', 'pwsh', 'cmd'])(
+ '%s is a shell for the exit poll',
+ (name) => expect(isShellCommand(name)).toBe(true)
+ )
+
+ const owner = (command: string, argv: string[] = [command]): PaneOwner => ({
+ panePid: 10,
+ tty: 'win32-console:10',
+ paneId: 'win32:gen-1:2026-10-01T00:00:00Z',
+ command,
+ argv,
+ pids: [10]
+ })
+
+ it.each(['pwsh', 'powershell', 'cmd'])('wakes into an idle %s console', (shell) => {
+ expect(decideWakeResume({ owner: owner(shell), recorded: null, exitedByUs: false, agentId: 'claude' })).toBe('resume')
+ const recorded = { panePid: 10, paneId: 'win32:gen-1:2026-10-01T00:00:00Z', command: shell }
+ expect(decideWakeResume({ owner: owner(shell), recorded, exitedByUs: true, agentId: 'claude' })).toBe('resume')
+ })
+
+ it('never wakes into a console a node-run agent still owns', () => {
+ expect(
+ decideWakeResume({ owner: owner('node', ['claude']), recorded: null, exitedByUs: false, agentId: 'claude' })
+ ).toBe('agent-running')
+ })
+})