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 codex with two Ctrl-Cs instead of typing /quit

Erik Zwep72110c5796ba
diff --git a/src/renderer/terminal/agent-restart.test.ts b/src/renderer/terminal/agent-restart.test.ts
index de2c7de2d..d0480d0e5 100644
--- a/src/renderer/terminal/agent-restart.test.ts
+++ b/src/renderer/terminal/agent-restart.test.ts
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { readFileSync } from 'node:fs'
-import { resumeCommand } from '../../shared/agents/config'
+import { resumeCommand, setCustomAgentBaseResolver } from '../../shared/agents/config'
import { withPermissionMode } from '../../shared/agents/approval-mode'
import {
__resetAgentRestartForTests,
@@ -741,7 +741,7 @@ describe('performExitPhase', () => {
expect(await p).toBe('exited')
}
// Every other agent keeps the historical one-burst path byte-identical.
- for (const agentId of ['claude', 'codex', 'grok', 'gemini', 'copilot'] as const) {
+ for (const agentId of ['claude', 'grok', 'gemini', 'copilot'] as const) {
const { written, io } = fakeIo()
let pane: string = agentId
const p = performExitPhase({
@@ -761,6 +761,76 @@ describe('performExitPhase', () => {
}
})
+ 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
+ // as a prompt. Ctrl-C clears the whole composer when it holds text and quits when it is empty:
+ // two of them, apart, quit with or without a draft, and nothing is ever submitted.
+ for (const agentId of ['codex', 'custom:codexish'] as const) {
+ setCustomAgentBaseResolver((id) => (id === 'custom:codexish' ? 'codex' : undefined))
+ const { written, io } = fakeIo()
+ let pane = 'codex'
+ const p = performExitPhase({
+ agentId,
+ sessionId: 'sid-1',
+ io,
+ paneCommand: async () => pane,
+ timeoutMs: 6000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(0)
+ expect(written).toEqual(['\x03']) // never in the same burst as the second one
+ await vi.advanceTimersByTimeAsync(150)
+ expect(written).toEqual(['\x03', '\x03'])
+ pane = 'zsh'
+ await vi.advanceTimersByTimeAsync(5000)
+ expect(await p).toBe('exited')
+ setCustomAgentBaseResolver(null)
+ }
+ })
+
+ it('still reports codex exited when the first Ctrl-C alone quit it (empty composer)', async () => {
+ const { written, io } = fakeIo()
+ let pane = 'codex'
+ const p = performExitPhase({
+ agentId: 'codex',
+ sessionId: 'sid-1',
+ io: {
+ ...io,
+ write: (data: string) => {
+ io.write(data)
+ pane = 'zsh' // an empty composer: codex quits on the very first Ctrl-C
+ }
+ },
+ paneCommand: async () => pane,
+ timeoutMs: 6000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(150)
+ expect(written).toEqual(['\x03', '\x03']) // the second 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
+ const p = performExitPhase({
+ agentId: 'codex',
+ sessionId: 'sid-1',
+ io,
+ paneCommand: async () => 'codex',
+ timeoutMs: 6000,
+ pollMs: 100,
+ isLive: () => live
+ })
+ await vi.advanceTimersByTimeAsync(0)
+ live = false
+ await vi.advanceTimersByTimeAsync(200)
+ 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
diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.ts
index 2db0830a3..b38d75ace 100644
--- a/src/renderer/terminal/agent-restart.ts
+++ b/src/renderer/terminal/agent-restart.ts
@@ -43,6 +43,12 @@ 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. */
+const CTRL_C = '\x03'
+const CODEX_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)
// exits with claude's `/exit` — its own CLI grammar is claude's. A baseless custom agent has no
@@ -270,14 +276,28 @@ 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).
- d.io.write(KILL_LINE)
+ // 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)
// 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 (d.agentId === 'opencode') {
+ 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)
+ } else if (d.agentId === 'opencode') {
d.io.write(exit)
await new Promise((r) => setTimeout(r, 150))
if (gone()) return 'not-eligible'