xonecas.io
64875
Mc

mini-coder / commit 346d9cf

test: extract makeInterruptMessage helper and test interrupt stub behaviour

Sean2026-03-01 346d9cfdcf25c78947e8848036a2f2ddc10afe19 parent cc56c82

2 files changed

src/agent/agent.test.ts+39−1
@@ -1,6 +1,10 @@
11 import { describe, expect, test } from "bun:test";
22 import type { CoreMessage } from "../llm-api/turn.ts";
3-import { extractAssistantText, hasRalphSignal } from "./agent.ts";
3+import {
4+ extractAssistantText,
5+ hasRalphSignal,
6+ makeInterruptMessage,
7+} from "./agent.ts";
48
59 // ─── extractAssistantText ─────────────────────────────────────────────────────
610
@@ -146,3 +150,37 @@ describe("interrupt via AbortController", () => {
146150 expect(controller.signal.aborted).toBe(true);
147151 });
148152 });
153+
154+// ─── makeInterruptMessage ─────────────────────────────────────────────────────
155+
156+describe("makeInterruptMessage", () => {
157+ test("user reason produces an assistant role message", () => {
158+ const msg = makeInterruptMessage("user");
159+ expect(msg.role).toBe("assistant");
160+ });
161+
162+ test("error reason produces an assistant role message", () => {
163+ const msg = makeInterruptMessage("error");
164+ expect(msg.role).toBe("assistant");
165+ });
166+
167+ test("user reason content contains system-message tag", () => {
168+ const msg = makeInterruptMessage("user");
169+ expect(msg.content).toContain("<system-message>");
170+ expect(msg.content).toContain("</system-message>");
171+ expect(msg.content).toContain("interrupted by the user");
172+ });
173+
174+ test("error reason content contains system-message tag", () => {
175+ const msg = makeInterruptMessage("error");
176+ expect(msg.content).toContain("<system-message>");
177+ expect(msg.content).toContain("</system-message>");
178+ expect(msg.content).toContain("interrupted due to an error");
179+ });
180+
181+ test("user and error produce different messages", () => {
182+ expect(makeInterruptMessage("user").content).not.toBe(
183+ makeInterruptMessage("error").content,
184+ );
185+ });
186+});
src/agent/agent.ts+22−19
@@ -541,10 +541,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
541541 // /undo is the only way to erase a turn.
542542 if (wasAborted) {
543543 stopWatcher();
544- const stubMsg: CoreMessage = {
545- role: "assistant",
546- content: "[interrupted]",
547- };
544+ const stubMsg = makeInterruptMessage("user");
548545 session.messages.push(userMsg, stubMsg);
549546 saveMessages(session.id, [userMsg, stubMsg], thisTurn);
550547 coreHistory.push(userMsg, stubMsg);
@@ -595,11 +592,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
595592 if (wasAborted) {
596593 // Had partial content — append a system message block so the model
597594 // knows the response was cut short. /undo is the only way to erase.
598- const note: CoreMessage = {
599- role: "assistant",
600- content:
601- "<system-message>Response was interrupted by the user.</system-message>",
602- };
595+ const note = makeInterruptMessage("user");
603596 coreHistory.push(note);
604597 session.messages.push(note);
605598 saveMessages(session.id, [note], thisTurn);
@@ -607,11 +600,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
607600 } else {
608601 // No messages returned — interrupted before any content. Save a
609602 // synthetic assistant message. /undo is the only way to erase a turn.
610- const stubMsg: CoreMessage = {
611- role: "assistant",
612- content:
613- "<system-message>Response was interrupted by the user.</system-message>",
614- };
603+ const stubMsg = makeInterruptMessage("user");
615604 coreHistory.push(stubMsg);
616605 session.messages.push(stubMsg);
617606 saveMessages(session.id, [stubMsg], thisTurn);
@@ -630,11 +619,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
630619 // so history stays valid. /undo is the only way to erase a turn.
631620 if (!errorStubSaved) {
632621 errorStubSaved = true;
633- const stubMsg: CoreMessage = {
634- role: "assistant",
635- content:
636- "<system-message>Response was interrupted due to an error.</system-message>",
637- };
622+ const stubMsg = makeInterruptMessage("error");
638623 coreHistory.push(stubMsg);
639624 session.messages.push(stubMsg);
640625 saveMessages(session.id, [stubMsg], thisTurn);
@@ -672,6 +657,24 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
672657 }
673658 }
674659
660+// ─── Interrupt stub helpers ───────────────────────────────────────────────────
661+
662+/**
663+ * Returns a synthetic assistant CoreMessage signalling that the turn was
664+ * interrupted by the user (Ctrl+C) or by an error. Used in three places:
665+ * 1. Preamble abort (Ctrl+C before LLM call starts)
666+ * 2. Mid-stream abort with no partial content
667+ * 3. Mid-stream abort with partial content (appended after real messages)
668+ * 4. Unexpected throw in the catch block
669+ */
670+export function makeInterruptMessage(reason: "user" | "error"): CoreMessage {
671+ const text =
672+ reason === "user"
673+ ? "<system-message>Response was interrupted by the user.</system-message>"
674+ : "<system-message>Response was interrupted due to an error.</system-message>";
675+ return { role: "assistant", content: text };
676+}
677+
675678 // ─── Ralph signal detection ───────────────────────────────────────────────────
676679
677680 /**