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.

feat(restart): in-place restart choreography, registry and bulk summary

enes3f1462eac218
diff --git a/src/renderer/terminal/agent-restart.test.ts b/src/renderer/terminal/agent-restart.test.ts
index e7925d944..5fdaaf235 100644
--- a/src/renderer/terminal/agent-restart.test.ts
+++ b/src/renderer/terminal/agent-restart.test.ts
@@ -1,5 +1,14 @@
-import { describe, expect, it } from 'vitest'
-import { exitSequence, isShellCommand, restartEligibility } from './agent-restart'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import {
+ agentRestartFn,
+ exitSequence,
+ isShellCommand,
+ performRestartResume,
+ registerAgentRestart,
+ restartEligibility,
+ summarizeOutcomes,
+ type RestartOutcome
+} from './agent-restart'
describe('exitSequence', () => {
it('knows claude and codex, refuses others', () => {
@@ -45,3 +54,102 @@ describe('restartEligibility', () => {
})
})
})
+
+function fakeIo() {
+ const written: string[] = []
+ let cb: ((chunk: string) => void) | null = null
+ return {
+ written,
+ io: {
+ write(d: string) {
+ written.push(d)
+ // deliverCommand verifies by echo: reflect every write back immediately.
+ cb?.(d)
+ },
+ onData(f: (chunk: string) => void) {
+ cb = f
+ return () => {
+ cb = null
+ }
+ }
+ }
+ }
+}
+
+describe('performRestartResume', () => {
+ beforeEach(() => vi.useFakeTimers())
+ afterEach(() => vi.useRealTimers())
+
+ it('sends exit, waits for the shell, then delivers the resume command', async () => {
+ const { written, io } = fakeIo()
+ let pane = 'claude'
+ const p = performRestartResume({
+ agentId: 'claude',
+ sessionId: 'sid-1',
+ io,
+ paneCommand: async () => pane,
+ timeoutMs: 6000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(250) // a few polls while the CLI is still up
+ pane = 'zsh'
+ await vi.advanceTimersByTimeAsync(5000)
+ expect(await p).toBe('restarted')
+ expect(written[0]).toBe('/exit\r')
+ expect(written.join('')).toContain('claude --resume sid-1')
+ })
+
+ it('gives up without delivering when the pane never returns to a shell', async () => {
+ const { written, io } = fakeIo()
+ const p = performRestartResume({
+ agentId: 'claude',
+ sessionId: 'sid-1',
+ io,
+ paneCommand: async () => 'claude',
+ 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(
+ await performRestartResume({
+ agentId: 'gemini',
+ sessionId: 's',
+ io,
+ paneCommand: async () => 'zsh'
+ })
+ ).toBe('not-eligible')
+ })
+})
+
+describe('agent restart registry', () => {
+ it('registers, resolves and unregisters; re-register supersedes', () => {
+ const a = async (): Promise<RestartOutcome> => 'restarted'
+ const un = registerAgentRestart('n1', a)
+ expect(agentRestartFn('n1')).toBe(a)
+ const b = async (): Promise<RestartOutcome> => 'exit-timeout'
+ registerAgentRestart('n1', b)
+ un() // stale unregister from the superseded registration must be inert
+ expect(agentRestartFn('n1')).toBe(b)
+ })
+
+ it('drops a live registration on unregister (node unmount)', () => {
+ const fn = async (): Promise<RestartOutcome> => 'restarted'
+ registerAgentRestart('n2', fn)()
+ expect(agentRestartFn('n2')).toBeUndefined()
+ })
+})
+
+describe('summarizeOutcomes', () => {
+ it('counts restarted / timeout / pre-skips into the toast line', () => {
+ expect(
+ summarizeOutcomes(['restarted', 'restarted', 'exit-timeout'], { working: 2, noSession: 1 })
+ ).toBe('2 restarted · 1 failed (exit timeout) · 2 skipped (working) · 1 skipped (no session)')
+ expect(summarizeOutcomes(['restarted'], { working: 0, noSession: 0 })).toBe('1 restarted')
+ })
+})
diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.ts
index 89da1cfda..1699e1277 100644
--- a/src/renderer/terminal/agent-restart.ts
+++ b/src/renderer/terminal/agent-restart.ts
@@ -4,7 +4,8 @@
* model list without losing the conversation. Kept free of DOM/IPC so the node menu, the bulk
* filter and the restart choreography can all share exactly one set of rules.
*/
-import { canResume } from '../../shared/agents/config'
+import { canResume, resumeCommand } from '../../shared/agents/config'
+import { deliverCommand, type DeliveryIo } from './command-delivery'
/** In-band exit command per agent CLI. Only agents listed here can be restarted in place —
* an unknown CLI has no safe way to be asked to quit. */
@@ -50,3 +51,73 @@ export function restartEligibility(
if (!sessionId) return { ok: false, reason: 'no-session' }
return { ok: true }
}
+
+export type RestartOutcome = 'restarted' | 'exit-timeout' | 'not-eligible'
+
+export const RESTART_EXIT_TIMEOUT_MS = 6000
+export const RESTART_POLL_MS = 250
+
+/**
+ * In-place CLI restart: ask the agent to quit, wait until a SHELL owns the pane, then
+ * echo-deliver the resume command into it. Never force-kills — on timeout the CLI is left
+ * running and the caller reports the node. `paneCommand` errors count as "not a shell yet";
+ * the timeout is the backstop.
+ */
+export async function performRestartResume(d: {
+ agentId: string
+ sessionId: string
+ io: DeliveryIo
+ paneCommand: () => Promise<string | null>
+ timeoutMs?: number
+ pollMs?: number
+}): Promise<RestartOutcome> {
+ const exit = exitSequence(d.agentId)
+ const cmd = resumeCommand(d.agentId, d.sessionId)
+ if (!exit || !cmd) return 'not-eligible'
+ const timeoutMs = d.timeoutMs ?? RESTART_EXIT_TIMEOUT_MS
+ const pollMs = d.pollMs ?? RESTART_POLL_MS
+ d.io.write(exit + '\r')
+ const deadline = Date.now() + timeoutMs
+ for (;;) {
+ await new Promise((r) => setTimeout(r, pollMs))
+ let pane: string | null = null
+ try {
+ pane = await d.paneCommand()
+ } catch {
+ // transient IPC failure — keep polling until the deadline
+ }
+ if (isShellCommand(pane)) break
+ if (Date.now() > deadline) return 'exit-timeout'
+ }
+ deliverCommand(d.io, cmd)
+ return 'restarted'
+}
+
+// ── Node registry (same park-surviving pattern as TerminalNode's restartSubs) ────────────
+const restartFns = new Map<string, () => Promise<RestartOutcome>>()
+
+/** Register a node's restart closure; returns an unregister that is inert if superseded. */
+export function registerAgentRestart(nodeId: string, fn: () => Promise<RestartOutcome>): () => void {
+ restartFns.set(nodeId, fn)
+ return () => {
+ if (restartFns.get(nodeId) === fn) restartFns.delete(nodeId)
+ }
+}
+
+export function agentRestartFn(nodeId: string): (() => Promise<RestartOutcome>) | undefined {
+ return restartFns.get(nodeId)
+}
+
+/** One toast line for the bulk action; zero-count parts are omitted. */
+export function summarizeOutcomes(
+ outcomes: RestartOutcome[],
+ skipped: { working: number; noSession: number }
+): string {
+ const restarted = outcomes.filter((o) => o === 'restarted').length
+ const failed = outcomes.filter((o) => o === 'exit-timeout').length
+ const parts = [`${restarted} restarted`]
+ if (failed) parts.push(`${failed} failed (exit timeout)`)
+ if (skipped.working) parts.push(`${skipped.working} skipped (working)`)
+ if (skipped.noSession) parts.push(`${skipped.noSession} skipped (no session)`)
+ return parts.join(' · ')
+}
diff --git a/src/renderer/terminal/command-delivery.ts b/src/renderer/terminal/command-delivery.ts
index 0d39b05d7..89c5819f9 100644
--- a/src/renderer/terminal/command-delivery.ts
+++ b/src/renderer/terminal/command-delivery.ts
@@ -51,9 +51,12 @@ 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.
const submit = (): void => {
- io.write('\r')
finish()
+ io.write('\r')
}
const tryOnce = (): void => {
if (done) return