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): quit claude with three Ctrl-Cs instead of typing /exit

Erik Zwepd8e89701bb6f
diff --git a/CLAUDE.md b/CLAUDE.md
index d964d18eb..4d4028d49 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -788,9 +788,12 @@ Lifecycle, by intent:
agent-restart.ts` restarts the agent CLI *inside* the pane and leaves the PTY, the tmux session
and its scrollback untouched. It exists for **new-model pickup** — a freshly released model only
shows 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: write the CLI's own exit line (`/exit`
- for claude, `/quit` for codex — that table is also the gate, an agent not in it can never be
- restarted in place), poll `pty:pane-command` (`#{pane_current_command}`, local tmux socket or the
+ 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
+ gate — an agent not in it can never be restarted in place. Then poll `pty:pane-command` (`#{pane_current_command}`, local tmux socket or the
project'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 same
`claude --resume` / `codex resume` the cold restore uses. **Nothing is ever killed**: if the CLI
diff --git a/src/renderer/terminal/agent-restart.test.ts b/src/renderer/terminal/agent-restart.test.ts
index d0480d0e5..f2ccb4054 100644
--- a/src/renderer/terminal/agent-restart.test.ts
+++ b/src/renderer/terminal/agent-restart.test.ts
@@ -163,6 +163,25 @@ describe('performRestartResume', () => {
afterEach(() => vi.useRealTimers())
it('sends exit, waits for the shell, then delivers the resume command', async () => {
+ const { written, io } = fakeIo()
+ let pane = 'grok'
+ const p = performRestartResume({
+ agentId: 'grok',
+ 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.slice(0, 2)).toEqual(['\x15', '/quit\r'])
+ expect(written.join('')).toContain('grok --resume sid-1')
+ })
+
+ it('restarts claude with Ctrl-Cs only, then delivers its resume command (issue #928)', async () => {
const { written, io } = fakeIo()
let pane = 'claude'
const p = performRestartResume({
@@ -173,11 +192,12 @@ describe('performRestartResume', () => {
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')
- expect(written.slice(0, 2)).toEqual(['\x15', '/exit\r'])
+ expect(written.join('')).not.toContain('/exit')
expect(written.join('')).toContain('claude --resume sid-1')
})
@@ -253,9 +273,9 @@ describe('performRestartResume', () => {
it('clears the pending input line before asking the CLI to quit', async () => {
const { written, io } = fakeIo()
- let pane = 'claude'
+ let pane = 'grok'
const p = performRestartResume({
- agentId: 'claude',
+ agentId: 'grok',
sessionId: 'sid-1',
io,
paneCommand: async () => pane,
@@ -268,7 +288,7 @@ describe('performRestartResume', () => {
expect(await p).toBe('restarted')
// Half-typed user text in the prompt would otherwise be SUBMITTED as `…the/exit`.
expect(written[0]).toBe('\x15')
- expect(written[1]).toBe('/exit\r')
+ expect(written[1]).toBe('/quit\r')
})
it('takes a pane command that CHANGED away from the CLI as proof it quit', async () => {
@@ -371,7 +391,7 @@ describe('performRestartResume', () => {
const { written, io } = silentIo()
let cancel: (() => void) | undefined
const p = performRestartResume({
- agentId: 'claude',
+ agentId: 'grok',
sessionId: 'sid-1',
io,
paneCommand: async () => 'zsh',
@@ -512,7 +532,7 @@ describe('performRestartResume — a CLI that is slow to quit (issue #899)', ()
pane = 'zsh' // the CLI finally lets go
await vi.advanceTimersByTimeAsync(5000)
expect(await p).toBe('restarted')
- expect(written.slice(0, 2)).toEqual(['\x15', '/exit\r'])
+ expect(written.slice(0, 3)).toEqual(['\x03', '\x03', '\x03'])
expect(written.join('')).toContain('claude --resume sid-1')
})
@@ -581,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 = 'claude'
+ let pane = 'grok'
const p = performExitPhase({
- agentId: 'claude',
+ agentId: 'grok',
sessionId: 'sid-1',
io,
paneCommand: async () => pane,
@@ -596,7 +616,7 @@ describe('performExitPhase', () => {
expect(await p).toBe('exited')
// The exit phase's ENTIRE output: clear the line, then the CLI's own exit command. The resume
// line belongs to the other phase, so nothing here may reach the pane after `/exit`.
- expect(written).toEqual(['\x15', '/exit\r'])
+ expect(written).toEqual(['\x15', '/quit\r'])
})
it('writes NOTHING and refuses when the pre-flight probe answers null', async () => {
@@ -628,16 +648,16 @@ 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: 'claude',
+ agentId: 'grok',
sessionId: 'sid-1',
io,
- paneCommand: async () => 'claude',
+ paneCommand: async () => 'grok',
timeoutMs: 1000,
pollMs: 100
})
await vi.advanceTimersByTimeAsync(2000)
expect(await p).toBe('exit-timeout')
- expect(written).toEqual(['\x15', '/exit\r']) // never force-killed, never resumed
+ expect(written).toEqual(['\x15', '/quit\r']) // never force-killed, never resumed
})
it('refuses a session we could never resume into — the exit alone would lose it', async () => {
@@ -681,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 = ['claude', 'git', 'claude', 'nu', 'nu']
+ const panes = ['grok', 'git', 'grok', 'nu', 'nu']
let i = 0
const p = performExitPhase({
- agentId: 'claude',
+ agentId: 'grok',
sessionId: 'sid-1',
io,
- // The pre-flight consumes the first reading, so `before` is 'claude'.
+ // The pre-flight consumes the first reading, so `before` is 'grok'.
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 → 'claude': the CLI is still there
+ await vi.advanceTimersByTimeAsync(100) // poll 2 → 'grok': the CLI is still there
await vi.advanceTimersByTimeAsync(250) // polls 3+4 → 'nu' twice in a row
expect(await p).toBe('exited')
})
@@ -741,7 +761,7 @@ describe('performExitPhase', () => {
expect(await p).toBe('exited')
}
// Every other agent keeps the historical one-burst path byte-identical.
- for (const agentId of ['claude', 'grok', 'gemini', 'copilot'] as const) {
+ for (const agentId of ['grok', 'gemini', 'copilot'] as const) {
const { written, io } = fakeIo()
let pane: string = agentId
const p = performExitPhase({
@@ -753,7 +773,7 @@ describe('performExitPhase', () => {
pollMs: 100
})
await vi.advanceTimersByTimeAsync(0)
- const exit = agentId === 'claude' || agentId === 'copilot' ? '/exit' : '/quit'
+ const exit = agentId === 'copilot' ? '/exit' : '/quit'
expect(written).toEqual(['\x15', `${exit}\r`])
pane = 'zsh'
await vi.advanceTimersByTimeAsync(5000)
@@ -812,6 +832,60 @@ describe('performExitPhase', () => {
expect(await p).toBe('exited')
})
+ it('quits claude with three separate Ctrl-Cs and never types into its composer (issue #928)', async () => {
+ // Claude Code 2.1.282, isolated tmux socket: Ctrl-U clears only the cursor's line, so a typed
+ // `/exit` after it submits the rest of a multi-line draft as a prompt. One Ctrl-C clears the whole
+ // composer, the next one arms "Press Ctrl-C again to exit", the third exits. With an empty
+ // composer the second one already exits and the third lands on the shell prompt.
+ for (const agentId of ['claude', 'custom:claudeish'] as const) {
+ setCustomAgentBaseResolver((id) => (id === 'custom:claudeish' ? 'claude' : undefined))
+ const { written, io } = fakeIo()
+ let pane = 'claude'
+ const p = performExitPhase({
+ agentId,
+ sessionId: 'sid-1',
+ io,
+ paneCommand: async () => pane,
+ timeoutMs: 6000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(0)
+ expect(written).toEqual(['\x03'])
+ await vi.advanceTimersByTimeAsync(150)
+ expect(written).toEqual(['\x03', '\x03'])
+ await vi.advanceTimersByTimeAsync(150)
+ expect(written).toEqual(['\x03', '\x03', '\x03'])
+ pane = 'zsh'
+ await vi.advanceTimersByTimeAsync(5000)
+ expect(await p).toBe('exited')
+ setCustomAgentBaseResolver(null)
+ }
+ })
+
+ it('still reports claude exited when the second Ctrl-C already quit it (empty composer)', async () => {
+ const { written, io } = fakeIo()
+ let pane = 'claude'
+ const p = performExitPhase({
+ agentId: 'claude',
+ sessionId: 'sid-1',
+ io: {
+ ...io,
+ write: (data: string) => {
+ io.write(data)
+ // An empty composer: the first Ctrl-C arms "Press Ctrl-C again", the second quits.
+ if (written.length === 2) pane = 'zsh'
+ }
+ },
+ paneCommand: async () => pane,
+ timeoutMs: 6000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(300)
+ expect(written).toEqual(['\x03', '\x03', '\x03']) // the third one lands on the shell prompt
+ await vi.advanceTimersByTimeAsync(5000)
+ expect(await p).toBe('exited')
+ })
+
it('does not send codex its second Ctrl-C when the pane dies in between', async () => {
const { written, io } = fakeIo()
let live = true
@@ -1188,7 +1262,7 @@ describe('guardConcurrentRestart', () => {
const { written, io } = silentIo()
const run = guardConcurrentRestart('n-overlap', () =>
performRestartResume({
- agentId: 'claude',
+ agentId: 'grok',
sessionId: 'sid-1',
io,
paneCommand: async () => 'zsh',
@@ -1200,12 +1274,12 @@ 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('claude --resume sid-1')
+ expect(written.join('')).toContain('grok --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')
expect(await probe()).toBe('not-eligible')
- expect(written.filter((w) => w === '/exit\r')).toHaveLength(1)
+ expect(written.filter((w) => w === '/quit\r')).toHaveLength(1)
await vi.advanceTimersByTimeAsync(10_000) // the fail-open submit ends the delivery
expect(await first).toBe('restarted')
expect(await probe()).toBe('restarted') // pane free again
diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.ts
index b38d75ace..f07405eef 100644
--- a/src/renderer/terminal/agent-restart.ts
+++ b/src/renderer/terminal/agent-restart.ts
@@ -43,11 +43,15 @@ const EXIT_SEQUENCES: Record<string, string> = {
opencode: '/exit'
}
-/** codex is quit with two Ctrl-Cs rather than a typed `/quit` — see `performExitPhase`. Its
- * `/quit` entry above still gates the restart and hibernation, which ask whether an agent can be
- * quit in place at all. */
+/** Agents quit with Ctrl-Cs rather than their typed exit, and how many — see `performExitPhase`.
+ * Their entry in EXIT_SEQUENCES above still gates the restart and hibernation, which ask whether
+ * 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 }
const CTRL_C = '\x03'
-const CODEX_CTRL_C_GAP_MS = 150
+const CTRL_C_GAP_MS = 150
export function exitSequence(agentId: string): string | null {
// Resolve through the BASE harness so a custom agent that inherits claude (e.g. a proxy wrapper)
@@ -276,27 +280,31 @@ 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 is quit with Ctrl-C instead of its `/quit` (issue #842, codex-cli 0.155.1, macOS,
- // isolated tmux socket). A typed exit fails there both ways: `/quit\r` in one burst is never
- // submitted, and once the CR does arrive, Ctrl-U has cleared only one line of a multi-line draft,
- // so the rest is submitted with the exit as a prompt. Ctrl-C clears the whole composer when it
- // holds text and quits codex when it is empty, so two of them, apart, quit it with or without a
- // draft. 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 its last line. If the first one already quit codex, the
- // second lands on the shell prompt, where it does nothing.
- const quitWithCtrlC = capabilityAgentId(d.agentId) === 'codex'
- if (!quitWithCtrlC) d.io.write(KILL_LINE)
+ // 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
+ // 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)] ?? 0
+ if (!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 (quitWithCtrlC) {
- d.io.write(CTRL_C)
- await new Promise((r) => setTimeout(r, CODEX_CTRL_C_GAP_MS))
- if (gone()) return 'not-eligible'
- d.io.write(CTRL_C)
+ if (ctrlCs) {
+ for (let i = 0; i < ctrlCs; i++) {
+ if (i > 0) {
+ await new Promise((r) => setTimeout(r, CTRL_C_GAP_MS))
+ if (gone()) return 'not-eligible'
+ }
+ d.io.write(CTRL_C)
+ }
} else if (d.agentId === 'opencode') {
d.io.write(exit)
await new Promise((r) => setTimeout(r, 150))