@@ -41,6 +41,8 @@ const CTRL_K = "\x0B";
4141 const CTRL_L = "\x0C";
4242 const CTRL_R = "\x12";
4343 const TAB = "\x09";
44+const ESC_BYTE = 0x1b;
45+const CTRL_C_BYTE = 0x03;
4446
4547 // ─── Types ────────────────────────────────────────────────────────────────────
4648
@@ -184,42 +186,65 @@ async function readKey(reader: StreamReader): Promise<string> {
184186 return new TextDecoder().decode(value);
185187 }
186188
187-// ─── Interrupt watcher (used during LLM turns) ────────────────────────────────
188-// While the LLM is streaming we keep stdin in raw mode so that Ctrl+C arrives
189-// as byte 0x03. We listen directly on process.stdin via a dedicated 'data'
189+// ─── Turn cancel watcher (used during LLM turns) ─────────────────────────────
190+// While the LLM is streaming we keep stdin in raw mode so ESC and Ctrl+C arrive
191+// as raw bytes. We listen directly on process.stdin via a dedicated 'data'
190192 // handler — NOT via the shared ReadableStream reader — so that the watcher and
191-// readline() never race for the same bytes. The caller must call the returned
193+// readline() never race for the same bytes. The caller must call the returned
192194 // stop() in a finally block.
193195
194-export function watchForInterrupt(
195- abortController: AbortController,
196-): () => void {
197- if (!process.stdin.isTTY) return () => {};
196+export function getTurnControlAction(
197+ chunk: Uint8Array,
198+): "cancel" | "quit" | null {
199+ for (const byte of chunk) {
200+ if (byte === ESC_BYTE) return "cancel";
201+ if (byte === CTRL_C_BYTE) return "quit";
202+ }
203+ return null;
204+}
205+
206+function exitOnCtrlC(opts?: {
207+ printNewline?: boolean;
208+ disableBracketedPaste?: boolean;
209+}): never {
210+ if (opts?.printNewline) process.stdout.write("\n");
211+ if (opts?.disableBracketedPaste) process.stdout.write(BPASTE_DISABLE);
212+ terminal.setInterruptHandler(null);
213+ terminal.setRawMode(false);
214+ process.stdin.pause();
215+ terminal.restoreTerminal();
216+ process.exit(130);
217+}
198218
199- const onInterrupt = () => {
219+export function watchForCancel(abortController: AbortController): () => void {
220+ if (!terminal.isTTY) return () => {};
221+
222+ const onCancel = () => {
200223 cleanup();
201224 abortController.abort();
202225 };
203226
204227 const onData = (chunk: Buffer) => {
205- for (const byte of chunk) {
206- if (byte === 0x03) {
207- // Ctrl+C received — abort and clean up immediately.
208- onInterrupt();
209- return;
210- }
228+ const action = getTurnControlAction(chunk);
229+ if (action === "cancel") {
230+ onCancel();
231+ return;
232+ }
233+ if (action === "quit") {
234+ cleanup();
235+ exitOnCtrlC({ printNewline: true });
211236 }
212237 };
213238
214239 const cleanup = () => {
215240 process.stdin.removeListener("data", onData);
216241 terminal.setInterruptHandler(null);
217- process.stdin.setRawMode(false);
242+ terminal.setRawMode(false);
218243 process.stdin.pause();
219244 };
220245
221- terminal.setInterruptHandler(onInterrupt);
222- process.stdin.setRawMode(true);
246+ terminal.setInterruptHandler(onCancel);
247+ terminal.setRawMode(true);
223248 process.stdin.resume();
224249 process.stdin.on("data", onData);
225250
@@ -267,7 +292,7 @@ export async function readline(opts: {
267292 let pasteBuffer: string | null = null; // full text of a pending paste
268293 const imageAttachments: ImageAttachment[] = []; // images collected during this prompt
269294
270- process.stdin.setRawMode(true);
295+ terminal.setRawMode(true);
271296 process.stdin.resume();
272297 process.stdout.write(BPASTE_ENABLE);
273298
@@ -461,8 +486,7 @@ export async function readline(opts: {
461486
462487 // ── Control keys ──────────────────────────────────────────────────────
463488 if (raw === CTRL_C) {
464- process.stdout.write("\n");
465- return { type: "interrupt" };
489+ exitOnCtrlC({ printNewline: true, disableBracketedPaste: true });
466490 }
467491
468492 if (raw === CTRL_D) {
@@ -637,7 +661,7 @@ export async function readline(opts: {
637661 // Do NOT cancel the shared reader — it is reused across readline() calls.
638662 // Just reset raw mode and pause stdin until the next prompt.
639663 process.stdout.write(BPASTE_DISABLE);
640- process.stdin.setRawMode(false);
664+ terminal.setRawMode(false);
641665 process.stdin.pause();
642666 }
643667 }