@@ -103,3 +103,111 @@ describe("hasRalphSignal", () => {
103103 expect(hasRalphSignal("I finished the task.")).toBe(false);
104104 });
105105 });
106+
107+// ─── SIGINT handling ──────────────────────────────────────────────────────────
108+//
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.
112+//
113+// 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.
119+//
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"]);
212+ });
213+});