@@ -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 /**