xonecas.io
64875
Mc

mini-coder / commit a26c55d

Update interrupt controls and todo notes

xonecas2026-03-08 a26c55dd3ec11e142e4f86f13395654304493aa2 parent 575bfd4

7 files changed

TODO.md+16−4
@@ -1,10 +1,22 @@
11 # TODO
22
3+## Disambiguiate CTRL+c to quit vs interrupt/return to prompt
4+
5+- Let's change it so we use ESC to interrupt gracefully the assistant (cancel)
6+
7+---
8+
9+## Weird prompt behaviours related to past
10+
11+- Weird whitepace when reaching max line length in the terminal
12+- `PASTE` word appears verbatim sometimes
13+- Extra spaces at the end of the line, pushing the cursor away from the text
14+
315 ---
416
517 ## Subagents id/number/lane could be made more robust
618
7-- Currently we use an incremental integer for this, but in a long session with several conversations (user uses /new) it can become confusing (seeing subagent number 16 after /new). Let's think of a better way to id subagents and their lanes/worktrees. Maybe with a UUID?
19+- Currently we use an incremental integer for this, but in a long session with several conversations (user uses /new) it can become confusing (seeing subagent number 16 after /new). Let's think of a better way to id subagents and their lanes/worktrees. Maybe with a UUID?
820
921 ---
1022
@@ -23,9 +35,9 @@ We currently have a ton of overlap with these features, Commands run within suba
2335
2436 Let's clean things up:
2537
26-- `/review` command should use the same code as a custom-commands, consolidate the two. We can create the global `/review` command in `~/.agents/commands` at app start if it doesn't exist. That way it can be a pure custom command and the users are encouraged to edit it for their own custom reviews. Never overwrite the file if it exists. Print a line notifying the user that the command was created.
38+- `/review` command should use the same code as a custom-commands, consolidate the two. We can create the global `/review` command in `~/.agents/commands` at app start if it doesn't exist. That way it can be a pure custom command and the users are encouraged to edit it for their own custom reviews. Never overwrite the file if it exists. Print a line notifying the user that the command was created.
2739
28-- Custom commands and custom subagents: Custom commands just spawn generic subagents with a dedicated prompt, custom agents are just subagents with a dedicated system prompt and a main agent provided prompt. I believe these are already well implemented, and share most of the code.
40+- Custom commands and custom subagents: Custom commands just spawn generic subagents with a dedicated prompt, custom agents are just subagents with a dedicated system prompt and a main agent provided prompt. I believe these are already well implemented, and share most of the code.
2941
3042 ---
3143
@@ -42,4 +54,4 @@ We need to do research first, but maybe we can leverage the hooks feature to ach
4254 ## Deferred fixes
4355
4456 - model-info: in `resolveFromProviderRow`, when canonical capability exists but `contextWindow` is null, fall back to provider row context (`capability.contextWindow ?? row.contextWindow`).
45-- Worktrees share bun lockfile and node_modules. Question this choice, is this really a good idea, or just an opportunity for things to go wrong?
57+- Worktrees share bun lockfile and node_modules. Question this choice, is this really a good idea, or just an opportunity for things to go wrong?
src/agent/agent.test.ts+5−8
@@ -108,18 +108,15 @@ describe("hasRalphSignal", () => {
108108 });
109109 });
110110
111-// ─── Ctrl+C / interrupt handling ──────────────────────────────────────────────
111+// ─── ESC / Ctrl+C handling ────────────────────────────────────────────────────
112112 //
113113 // Behaviour contract:
114-// • During an LLM turn, Ctrl+C cancels the turn and returns to the prompt.
115-// • Ctrl+C when no turn is active exits the process.
114+// • During an LLM turn, ESC cancels the turn and returns to the prompt.
115+// • Ctrl+C exits the process.
116116 //
117117 // Architecture:
118-// watchForInterrupt() (input.ts) sets stdin to raw mode and listens for
119-// byte 0x03 (Ctrl+C) directly — no SIGINT involved. This sidesteps Bun's
120-// unreliable SIGINT delivery when stdin is in raw mode.
121-// registerTerminalCleanup() (output.ts) handles SIGINT for the idle case
122-// (non-TTY, subprocesses sending SIGINT, etc.) and simply calls process.exit.
118+// watchForCancel() (input.ts) sets stdin to raw mode and listens for
119+// byte 0x1B (ESC) to abort the turn, and byte 0x03 (Ctrl+C) to quit.
123120 //
124121 // These tests verify the AbortController / abort-signal wiring independently
125122 // of the actual TTY/stdin machinery.
src/agent/session-runner.ts+2−2
@@ -1,6 +1,6 @@
11 import * as c from "yoctocolors";
22 import type { ImageAttachment } from "../cli/image-types.ts";
3-import { watchForInterrupt } from "../cli/input.ts";
3+import { watchForCancel } from "../cli/input.ts";
44 import { resolveModel } from "../llm-api/providers.ts";
55 import type { ThinkingEffort } from "../llm-api/providers.ts";
66 import type { CoreMessage } from "../llm-api/turn.ts";
@@ -107,7 +107,7 @@ export class SessionRunner {
107107 abortController.signal.addEventListener("abort", () => {
108108 wasAborted = true;
109109 });
110- const stopWatcher = watchForInterrupt(abortController);
110+ const stopWatcher = watchForCancel(abortController);
111111
112112 const { text: resolvedText, images: refImages } = await resolveFileRefs(
113113 text,
src/cli/commands.ts+1−1
@@ -508,7 +508,7 @@ function handleHelp(
508508 );
509509 writeln();
510510 writeln(
511- ` ${c.dim("ctrl+c")} cancel ${c.dim("·")} ${c.dim("ctrl+d")} exit ${c.dim("·")} ${c.dim("ctrl+r")} history search ${c.dim("·")} ${c.dim("↑↓")} history`,
511+ ` ${c.dim("esc")} cancel response ${c.dim("·")} ${c.dim("ctrl+c")} exit ${c.dim("·")} ${c.dim("ctrl+d")} exit ${c.dim("·")} ${c.dim("ctrl+r")} history search ${c.dim("·")} ${c.dim("↑↓")} history`,
512512 );
513513 writeln();
514514 }
src/cli/input.test.ts+20−1
@@ -1,5 +1,5 @@
11 import { describe, expect, test } from "bun:test";
2-import { pasteLabel } from "./input.ts";
2+import { getTurnControlAction, pasteLabel } from "./input.ts";
33
44 describe("pasteLabel", () => {
55 test("single short line", () => {
@@ -26,3 +26,22 @@ describe("pasteLabel", () => {
2626 expect(pasteLabel("")).toBe('[pasted: ""]');
2727 });
2828 });
29+
30+describe("getTurnControlAction", () => {
31+ test("treats ESC as cancel", () => {
32+ expect(getTurnControlAction(new Uint8Array([0x1b]))).toBe("cancel");
33+ });
34+
35+ test("treats Ctrl+C as quit", () => {
36+ expect(getTurnControlAction(new Uint8Array([0x03]))).toBe("quit");
37+ });
38+
39+ test("uses the first matching control byte in the chunk", () => {
40+ expect(getTurnControlAction(new Uint8Array([0x03, 0x1b]))).toBe("quit");
41+ expect(getTurnControlAction(new Uint8Array([0x1b, 0x03]))).toBe("cancel");
42+ });
43+
44+ test("ignores unrelated bytes", () => {
45+ expect(getTurnControlAction(new Uint8Array([0x61, 0x62, 0x63]))).toBeNull();
46+ });
47+});
src/cli/input.ts+46−22
@@ -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 }
src/cli/output.ts+3−1
@@ -72,7 +72,9 @@ export function renderBanner(model: string, cwd: string): void {
7272 writeln();
7373 writeln(` ${c.cyan("mc")} ${c.dim(`mini-coder · v${PACKAGE_VERSION}`)}`);
7474 writeln(` ${c.dim(model)} ${c.dim("·")} ${c.dim(cwd)}`);
75- writeln(` ${c.dim("/help for commands · ctrl+d to exit")}`);
75+ writeln(
76+ ` ${c.dim("/help for commands · esc cancel · ctrl+c/ctrl+d exit")}`,
77+ );
7678 writeln();
7779 }