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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(restart): quit grok, opencode and copilot with Ctrl-Cs too
Erik Zwep43bfc1a4a58d
diff --git a/CLAUDE.md b/CLAUDE.mdindex 4d4028d49..f8b2eee45 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -788,11 +788,12 @@ Lifecycle, by intent:agent-restart.ts` restarts the agent CLI *inside* the pane and leaves the PTY, the tmux sessionand its scrollback untouched. It exists for **new-model pickup** — a freshly released model onlyshows up in a CLI's model list on a fresh launch, and doing that by hand means closing and- re-resuming every agent node on the canvas. Choreography: ask the CLI to quit — its own exit line- (`/quit` for grok and gemini, `/exit` for copilot and opencode), except for claude and codex,- which get a fixed number of Ctrl-Cs instead (`CTRL_C_QUITS`): in both, Ctrl-U clears only the- cursor's line, so a typed exit submitted the rest of a multi-line draft as a prompt, while Ctrl-C- clears the whole composer and quits once it is empty (#842, #928). `EXIT_SEQUENCES` is still the+ re-resuming every agent node on the canvas. Choreography: ask the CLI to quit with a fixed+ number of Ctrl-Cs (`CTRL_C_QUITS`: codex 2, claude 3, grok 3, opencode 2, copilot 2). In all+ five, Ctrl-U clears only the cursor's line, so a typed exit submitted the rest of a multi-line+ draft as a prompt, while Ctrl-C clears the whole composer and quits once it is empty (#842,+ #928). gemini alone still gets its typed exit (a bare `/quit`), because it was never measured+ — its composer is only reachable after a login. `EXIT_SEQUENCES` is still thegate — an agent not in it can never be restarted in place. Then poll `pty:pane-command` (`#{pane_current_command}`, local tmux socket or theproject's SSH ControlMaster; any failure reads as "not a shell yet") every `RESTART_POLL_MS`(250 ms) until a SHELL owns the pane, then echo-deliver `resumeCommand(...)` — the samediff --git a/src/renderer/terminal/agent-restart.test.ts b/src/renderer/terminal/agent-restart.test.tsindex f2ccb4054..1954d0d5d 100644--- a/src/renderer/terminal/agent-restart.test.ts+++ b/src/renderer/terminal/agent-restart.test.ts@@ -164,9 +164,9 @@ describe('performRestartResume', () => {it('sends exit, waits for the shell, then delivers the resume command', async () => {const { written, io } = fakeIo()- let pane = 'grok'+ let pane = 'gemini'const p = performRestartResume({- agentId: 'grok',+ agentId: 'gemini',sessionId: 'sid-1',io,paneCommand: async () => pane,@@ -178,7 +178,7 @@ describe('performRestartResume', () => {await vi.advanceTimersByTimeAsync(5000)expect(await p).toBe('restarted')expect(written.slice(0, 2)).toEqual(['\x15', '/quit\r'])- expect(written.join('')).toContain('grok --resume sid-1')+ expect(written.join('')).toContain('gemini --resume sid-1')})it('restarts claude with Ctrl-Cs only, then delivers its resume command (issue #928)', async () => {@@ -273,9 +273,9 @@ describe('performRestartResume', () => {it('clears the pending input line before asking the CLI to quit', async () => {const { written, io } = fakeIo()- let pane = 'grok'+ let pane = 'gemini'const p = performRestartResume({- agentId: 'grok',+ agentId: 'gemini',sessionId: 'sid-1',io,paneCommand: async () => pane,@@ -391,7 +391,7 @@ describe('performRestartResume', () => {const { written, io } = silentIo()let cancel: (() => void) | undefinedconst p = performRestartResume({- agentId: 'grok',+ agentId: 'gemini',sessionId: 'sid-1',io,paneCommand: async () => 'zsh',@@ -601,9 +601,9 @@ describe('performExitPhase', () => {it('quits the CLI and reports exited once the pane reports a shell — writing nothing after', async () => {const { written, io } = fakeIo()- let pane = 'grok'+ let pane = 'gemini'const p = performExitPhase({- agentId: 'grok',+ agentId: 'gemini',sessionId: 'sid-1',io,paneCommand: async () => pane,@@ -648,10 +648,10 @@ describe('performExitPhase', () => {it('reports exit-timeout, without a resume, when the CLI never lets go of the pane', async () => {const { written, io } = fakeIo()const p = performExitPhase({- agentId: 'grok',+ agentId: 'gemini',sessionId: 'sid-1',io,- paneCommand: async () => 'grok',+ paneCommand: async () => 'gemini',timeoutMs: 1000,pollMs: 100})@@ -701,19 +701,19 @@ describe('performExitPhase', () => {// CONSECUTIVE polls, because a single changed reading can be a momentary foreground CHILD of a// still-running CLI. Typing the resume line into a live CLI would send it as a message.const { io } = fakeIo()- const panes = ['grok', 'git', 'grok', 'nu', 'nu']+ const panes = ['gemini', 'git', 'gemini', 'nu', 'nu']let i = 0const p = performExitPhase({- agentId: 'grok',+ agentId: 'gemini',sessionId: 'sid-1',io,- // The pre-flight consumes the first reading, so `before` is 'grok'.+ // The pre-flight consumes the first reading, so `before` is 'gemini'.paneCommand: async () => panes[Math.min(i++, panes.length - 1)] ?? null,timeoutMs: 5000,pollMs: 100})await vi.advanceTimersByTimeAsync(150) // poll 1 → 'git': changed, but not yet twice- await vi.advanceTimersByTimeAsync(100) // poll 2 → 'grok': the CLI is still there+ await vi.advanceTimersByTimeAsync(100) // poll 2 → 'gemini': the CLI is still thereawait vi.advanceTimersByTimeAsync(250) // polls 3+4 → 'nu' twice in a rowexpect(await p).toBe('exited')})@@ -736,32 +736,17 @@ describe('performExitPhase', () => {expect(await p).toBe('not-eligible') // not a timeout: there is no pane left to time out in})- it('splits opencode exit into text then CR (batched-input guard), leaving others one-burst', async () => {- // opencode's TUI swallows a one-burst `/exit\r` (measured 1.18.18-1.18.25, Linux, tmux, isolated- // socket: `/exit` left in composer with popup armed, exit-timeout at 6s; split CR by 100ms exits- // in ~500ms, shipped with 150ms). This pins the split so a refactor cannot silently re-batch it.- for (const agentId of ['opencode'] as const) {- const { written, io } = fakeIo()- let pane = 'opencode'- const p = performExitPhase({- agentId,- sessionId: 'sid-1',- io,- paneCommand: async () => pane,- timeoutMs: 6000,- pollMs: 100- })- // Pre-flight resolves; KILL_LINE + exit text are written, CR not yet.- await vi.advanceTimersByTimeAsync(0)- expect(written).toEqual(['\x15', '/exit'])- await vi.advanceTimersByTimeAsync(150)- expect(written).toEqual(['\x15', '/exit', '\r'])- pane = 'zsh'- await vi.advanceTimersByTimeAsync(5000)- expect(await p).toBe('exited')- }- // Every other agent keeps the historical one-burst path byte-identical.- for (const agentId of ['grok', 'gemini', 'copilot'] as const) {+ it('quits grok, opencode and copilot with separate Ctrl-Cs and never types into the composer', async () => {+ // Measured on grok 1.0.40, opencode 1.18.32 and copilot 1.0.88 (macOS, isolated tmux socket,+ // CLI started from an interactive shell): Ctrl-U clears only the cursor's line, so a typed exit+ // submitted the rest of a multi-line draft as a prompt. Ctrl-C clears the whole composer:+ // grok then needs two more (arm + exit), opencode and copilot one more.+ const cases = [+ ['grok', 3],+ ['opencode', 2],+ ['copilot', 2]+ ] as const+ for (const [agentId, count] of cases) {const { written, io } = fakeIo()let pane: string = agentIdconst p = performExitPhase({@@ -773,14 +758,36 @@ describe('performExitPhase', () => {pollMs: 100})await vi.advanceTimersByTimeAsync(0)- const exit = agentId === 'copilot' ? '/exit' : '/quit'- expect(written).toEqual(['\x15', `${exit}\r`])+ expect(written, agentId).toEqual(['\x03']) // never in the same burst as the next one+ await vi.advanceTimersByTimeAsync(150 * (count - 1))+ expect(written, agentId).toEqual(Array(count).fill('\x03'))pane = 'zsh'await vi.advanceTimersByTimeAsync(5000)- expect(await p).toBe('exited')+ expect(await p, agentId).toBe('exited')+ expect(written, agentId).toEqual(Array(count).fill('\x03')) // no exit line, no CR, ever}})+ it('keeps gemini on its typed exit: line-clear, then a bare `/quit` in one burst', async () => {+ // gemini is the one agent not measured (its composer is only reachable after a login), so it+ // keeps the historical path byte-identical.+ const { written, io } = fakeIo()+ let pane = 'gemini'+ const p = performExitPhase({+ agentId: 'gemini',+ sessionId: 'sid-1',+ io,+ paneCommand: async () => pane,+ timeoutMs: 6000,+ pollMs: 100+ })+ await vi.advanceTimersByTimeAsync(0)+ expect(written).toEqual(['\x15', '/quit\r'])+ pane = 'zsh'+ await vi.advanceTimersByTimeAsync(5000)+ expect(await p).toBe('exited')+ })+it('quits codex with two separate Ctrl-Cs and never types into its composer (issue #842)', async () => {// codex-cli 0.155.1, isolated tmux socket: a one-burst `/quit\r` is never submitted, and Ctrl-U// clears only one line of the composer, so any typed exit can end up submitting a leftover draft@@ -904,26 +911,6 @@ describe('performExitPhase', () => {expect(await p).toBe('not-eligible')expect(written).toEqual(['\x03'])})-- it('abandons the split CR when the pane dies mid-delay', async () => {- const { written, io } = fakeIo()- let live = true- const p = performExitPhase({- agentId: 'opencode',- sessionId: 'sid-1',- io,- paneCommand: async () => 'opencode',- timeoutMs: 6000,- pollMs: 100,- isLive: () => live- })- await vi.advanceTimersByTimeAsync(0)- expect(written).toEqual(['\x15', '/exit'])- live = false- await vi.advanceTimersByTimeAsync(200)- expect(await p).toBe('not-eligible')- expect(written).toEqual(['\x15', '/exit']) // CR never sent into a dead pane- })})describe('performResumePhase', () => {@@ -1058,7 +1045,7 @@ describe('performRestartResume — grok', () => {beforeEach(() => vi.useFakeTimers())afterEach(() => vi.useRealTimers())- it("quits with grok's own exit line and resumes by session id", async () => {+ it('quits grok with three Ctrl-Cs and resumes by session id', async () => {const { written, io } = fakeIo()let pane = 'grok'const p = performRestartResume({@@ -1069,12 +1056,12 @@ describe('performRestartResume — grok', () => {timeoutMs: 6000,pollMs: 100})- await vi.advanceTimersByTimeAsync(250) // a few polls while the CLI is still up+ await vi.advanceTimersByTimeAsync(400) // the three Ctrl-Cs, 150ms apart+ expect(written).toEqual(['\x03', '\x03', '\x03'])pane = 'zsh'await vi.advanceTimersByTimeAsync(5000)expect(await p).toBe('restarted')- // `/quit`, not claude's `/exit` — the table is per CLI.- expect(written.slice(0, 2)).toEqual(['\x15', '/quit\r'])+ expect(written.join('')).not.toContain('/quit')expect(written.join('')).toContain('grok --resume abc-1')})@@ -1262,7 +1249,7 @@ describe('guardConcurrentRestart', () => {const { written, io } = silentIo()const run = guardConcurrentRestart('n-overlap', () =>performRestartResume({- agentId: 'grok',+ agentId: 'gemini',sessionId: 'sid-1',io,paneCommand: async () => 'zsh',@@ -1274,7 +1261,7 @@ describe('guardConcurrentRestart', () => {const first = run()await vi.advanceTimersByTimeAsync(150)// The resume line is in the pane but NOT submitted (no echo to verify it).- expect(written.join('')).toContain('grok --resume sid-1')+ expect(written.join('')).toContain('gemini --resume sid-1')// A second menu/bulk click landing in that window would splice `/exit` into the pending line// and submit `claude --resume sid-1/exit`.expect(await run()).toBe('not-eligible')diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.tsindex f07405eef..d8c7bf599 100644--- a/src/renderer/terminal/agent-restart.ts+++ b/src/renderer/terminal/agent-restart.ts@@ -48,8 +48,22 @@ const EXIT_SEQUENCES: Record<string, string> = {* an agent can be quit in place at all.* - codex (#842): one Ctrl-C clears the composer or, when it is empty, quits; two cover both.* - claude (#928): one Ctrl-C clears the composer, the next arms "Press Ctrl-C again to exit" and- * the third exits; with an empty composer the second already exits. */-const CTRL_C_QUITS: Record<string, number> = { codex: 2, claude: 3 }+ * the third exits; with an empty composer the second already exits.+ * - grok: same shape as claude — the first clears the whole composer ("Input cleared"), the next+ * arms "press again to quit" (a ~1.5 s window), the third exits. Measured on grok 1.0.40.+ * - opencode: one Ctrl-C clears the composer or, when it is empty, quits at once; two cover both.+ * Measured on 1.18.32.+ * - copilot: one Ctrl-C clears the composer AND arms "ctrl+c again to exit" (~2 s), the second+ * exits; two cover both. Measured on 1.0.88.+ * gemini is the one agent still on its typed exit: it was not measured (its composer is only+ * reachable after a login). */+const CTRL_C_QUITS: Record<string, number> = {+ codex: 2,+ claude: 3,+ grok: 3,+ opencode: 2,+ copilot: 2+}const CTRL_C = '\x03'const CTRL_C_GAP_MS = 150@@ -271,8 +285,8 @@ export async function performExitPhase(d: {// draft lost and a real turn started (tokens, possibly edits) — and the CLI, still running,// would then be reported as an exit timeout.//- // ASSUMPTION, unverified on a real build: Ctrl-U is "clear line" inside every TUI in- // EXIT_SEQUENCES (claude, codex, grok, gemini) — it is in every readline/ZLE prompt, and it is+ // ASSUMPTION, unverified on a real build: Ctrl-U is "clear line" inside every TUI that still+ // gets a typed exit (today only gemini) — it is in every readline/ZLE prompt, and it is// what command-delivery.ts already relies on for its rewrites. Each agent added to that table// inherits this assumption; only a device check retires it, per agent. If a TUI binds Ctrl-U to// something else this becomes one stray keystroke before the exit command — no worse than@@ -280,23 +294,20 @@ export async function performExitPhase(d: {// a shell. A lone Escape (\x1b) into a live agent is the user-interrupt gesture (cancels turns/thinking),// whereas \x15 is the safe line-clear attempt. Keep \x15 here even on Windows; WINDOWS_KILL_LINE// (\x1b) is strictly for shell panes (command delivery retry and hibernation wake).- // codex and claude are quit with Ctrl-Cs instead of a typed exit (issues #842 and #928, measured- // on codex-cli 0.155.1 and Claude Code 2.1.282, macOS, isolated tmux socket). In both, Ctrl-U- // clears only the line the cursor is on, so once the CR of a typed exit arrives, the rest of a- // multi-line draft is submitted with it as a prompt (codex also ignores a CR in the same burst as- // the text). Ctrl-C clears the whole composer in both, and quits once it is empty, so a fixed- // number of them, apart, quits with or without a draft (CTRL_C_QUITS). Nothing is typed and no CR+ // codex, claude, grok, opencode and copilot are quit with Ctrl-Cs instead of a typed exit (issues+ // #842 and #928; versions in CTRL_C_QUITS; macOS, isolated tmux socket, CLI started from an+ // interactive shell). In all five, Ctrl-U clears only the line the cursor is on, so once the CR+ // of a typed exit arrives, the rest of a multi-line draft is submitted with it as a prompt (codex+ // also ignores a CR in the same burst as the text). Ctrl-C clears the whole composer in all five,+ // and quits once it is empty, so a fixed number of them, apart, quits with or without a draft+ // (CTRL_C_QUITS). opencode used to get its typed `/exit` with the CR split off by 150 ms,+ // because its batched-input handling swallows a one-burst `/exit\r`; the Ctrl-Cs send no CR, so+ // that split has nothing left to do. Nothing is typed and no CR// is sent, so a draft is never submitted — but all of it is lost, where the line-clear lost only// the cursor's line. A Ctrl-C left over once the CLI has quit lands on the shell prompt, where it// does nothing.const ctrlCs = CTRL_C_QUITS[capabilityAgentId(d.agentId)] ?? 0if (!ctrlCs) d.io.write(KILL_LINE)- // opencode's TUI does not submit when text and CR arrive in the same input burst- // (batched-input handling). Measured on 1.18.18-1.18.25, Linux, tmux, isolated socket:- // one-burst `/exit\r` leaves `/exit` in the composer with popup armed and times out- // at 6s; splitting CR by 100ms exits in ~500ms. The resume half already uses- // echo-verified delivery (command-delivery.ts) for this shape; for exit we keep- // the minimal split so the other agents' blind-write contract stays unchanged.if (ctrlCs) {for (let i = 0; i < ctrlCs; i++) {if (i > 0) {@@ -305,11 +316,6 @@ export async function performExitPhase(d: {}d.io.write(CTRL_C)}- } else if (d.agentId === 'opencode') {- d.io.write(exit)- await new Promise((r) => setTimeout(r, 150))- if (gone()) return 'not-eligible'- d.io.write('\r')} else {d.io.write(exit + '\r')}