@@ -104,110 +104,45 @@ describe("hasRalphSignal", () => {
104104 });
105105 });
106106
107-// ─── SIGINT handling ──────────────────────────────────────────────────────────
107+// ─── Ctrl+C / interrupt handling ──────────────────────────────────────────────
108108 //
109-// Behaviour contract (from mini-coder-idea.md):
110-// • First Ctrl+C during an active turn → cancel the turn, return to prompt.
111-// • Second Ctrl+C (or Ctrl+C when no turn is active) → exit the process.
109+// Behaviour contract:
110+// • During an LLM turn, Ctrl+C cancels the turn and returns to the prompt.
111+// • Ctrl+C when no turn is active exits the process.
112112 //
113113 // Architecture:
114-// registerTerminalCleanup() (output.ts) registers a "cleanup" handler that
115-// calls process.exit(130) ONLY when listenerCount("SIGINT") === 1.
116-// processUserInput() (agent.ts) registers a per-turn "abort" handler as the
117-// VERY FIRST synchronous action — before any await — so that Ctrl+C at any
118-// point during the turn (including the async preamble) is intercepted here.
114+// watchForInterrupt() (input.ts) sets stdin to raw mode and listens for
115+// byte 0x03 (Ctrl+C) directly — no SIGINT involved. This sidesteps Bun's
116+// unreliable SIGINT delivery when stdin is in raw mode.
117+// registerTerminalCleanup() (output.ts) handles SIGINT for the idle case
118+// (non-TTY, subprocesses sending SIGINT, etc.) and simply calls process.exit.
119119 //
120-// Note: registerTerminalCleanup() itself cannot be called in tests because it
121-// registers a handler that calls process.exit(), which would kill the runner.
122-// These tests exercise the identical conditional logic with a safe stand-in.
123-
124-describe("SIGINT handling — registerTerminalCleanup + processUserInput contract", () => {
125- // Mirrors the exact conditional in registerTerminalCleanup's SIGINT handler.
126- // The real handler calls process.exit(130) instead of log.push("would-exit").
127- function makeCleanupHandler(log: string[]) {
128- return () => {
129- if (process.listenerCount("SIGINT") > 1) {
130- log.push("skipped");
131- return;
132- }
133- log.push("would-exit");
134- };
135- }
136-
137- test("cleanup handler skips exit while an abort handler is registered", () => {
138- const log: string[] = [];
139- const cleanup = makeCleanupHandler(log);
140- process.on("SIGINT", cleanup);
141-
142- const abort = () => {
143- log.push("abort-fired");
144- process.removeListener("SIGINT", abort);
145- };
146- process.on("SIGINT", abort);
147-
148- process.emit("SIGINT");
149-
150- process.removeListener("SIGINT", cleanup);
151- expect(log).toEqual(["skipped", "abort-fired"]);
152- });
153-
154- test("cleanup handler exits when it is the sole listener", () => {
155- const log: string[] = [];
156- const cleanup = makeCleanupHandler(log);
157- process.on("SIGINT", cleanup);
158-
159- process.emit("SIGINT");
160-
161- process.removeListener("SIGINT", cleanup);
162- expect(log).toEqual(["would-exit"]);
163- });
164-
165- test("abort handler registered before any await covers the async preamble", () => {
166- // processUserInput registers onSigInt synchronously at the TOP of the
167- // function (before resolveFileRefs / takeSnapshot awaits). This test
168- // confirms that a SIGINT fired during those awaits is caught by the abort
169- // handler rather than escaping to the cleanup handler and exiting.
170- const log: string[] = [];
171- const cleanup = makeCleanupHandler(log);
172- process.on("SIGINT", cleanup);
173-
174- // Abort handler is in place before any await fires
175- const abort = () => {
176- log.push("abort-fired");
177- process.removeListener("SIGINT", abort);
178- };
179- process.on("SIGINT", abort);
180-
181- // SIGINT fires mid-preamble (simulated synchronously)
182- process.emit("SIGINT");
183-
184- process.removeListener("SIGINT", cleanup);
185- process.removeListener("SIGINT", abort);
186-
187- // Cleanup skips (listenerCount=2); abort intercepts → no exit
188- expect(log).toEqual(["skipped", "abort-fired"]);
189- });
190-
191- test("second SIGINT after turn abort exits — intentional per-spec behaviour", () => {
192- // After the first Ctrl+C the abort handler removes itself (listenerCount→1).
193- // A subsequent Ctrl+C should exit (spec: "Second time it exits the app").
194- const log: string[] = [];
195- const cleanup = makeCleanupHandler(log);
196- process.on("SIGINT", cleanup);
197-
198- const abort = () => {
199- log.push("abort-fired");
200- process.removeListener("SIGINT", abort);
201- };
202- process.on("SIGINT", abort);
203-
204- process.emit("SIGINT"); // first — aborts the turn
205- expect(log).toEqual(["skipped", "abort-fired"]);
206- log.length = 0;
207-
208- process.emit("SIGINT"); // second — exits
209- process.removeListener("SIGINT", cleanup);
210-
211- expect(log).toEqual(["would-exit"]);
120+// These tests verify the AbortController / abort-signal wiring independently
121+// of the actual TTY/stdin machinery.
122+
123+describe("interrupt via AbortController", () => {
124+ test("aborting the controller sets wasAborted via signal listener", () => {
125+ const controller = new AbortController();
126+ let wasAborted = false;
127+ controller.signal.addEventListener("abort", () => {
128+ wasAborted = true;
129+ });
130+ expect(wasAborted).toBe(false);
131+ controller.abort();
132+ expect(wasAborted).toBe(true);
133+ });
134+
135+ test("abort is idempotent — repeated aborts do not throw", () => {
136+ const controller = new AbortController();
137+ controller.abort();
138+ expect(() => controller.abort()).not.toThrow();
139+ expect(controller.signal.aborted).toBe(true);
140+ });
141+
142+ test("signal.aborted reflects abort state correctly", () => {
143+ const controller = new AbortController();
144+ expect(controller.signal.aborted).toBe(false);
145+ controller.abort();
146+ expect(controller.signal.aborted).toBe(true);
212147 });
213148 });