xonecas.io
64875
Mc

mini-coder / commit f5474dd

fix: preserve interrupted turns in history instead of rolling back

Ctrl+C during a turn no longer erases the user message from history.
/undo is the sole mechanism for removing a turn.

- Preamble abort (Ctrl+C before LLM call): build userMsg + stub
  assistant message '[interrupted]', save both to DB and push to
  coreHistory/session.messages before returning.
- Mid-stream abort (Ctrl+C during LLM response): when newMessages is
  empty and wasAborted, save a stub '[interrupted]' assistant message
  instead of rolling back the user message.
- Real errors (schema validation, network failure, etc.) still roll back
  via the existing else/catch paths.

Sean2026-02-25 f5474dd0c778366e9bee7239c71c66f0802ef102 parent 6e5527b

1 file changed

src/agent/agent.ts+30−11
@@ -523,15 +523,6 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
523523 // touching git stash or the user's working tree state.
524524 const snapped = await takeSnapshot(cwd, session.id, thisTurn);
525525
526- // If CTRL+C was pressed during the async preamble, bail out gracefully
527- // before touching session state or calling the LLM.
528- if (wasAborted) {
529- process.removeListener("SIGINT", onSigInt);
530- if (snapped) deleteSnapshot(session.id, thisTurn);
531- turnIndex--;
532- return "";
533- }
534-
535526 const coreContent = planMode
536527 ? `${resolvedText}\n\n<system-message>PLAN MODE ACTIVE: Help the user gather context for the plan -- READ ONLY</system-message>`
537528 : ralphMode
@@ -554,6 +545,23 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
554545 }
555546 : { role: "user", content: coreContent };
556547
548+ // If CTRL+C was pressed during the async preamble, still save the user
549+ // message and a stub assistant reply so the turn stays in history.
550+ // /undo is the only way to erase a turn.
551+ if (wasAborted) {
552+ process.removeListener("SIGINT", onSigInt);
553+ const stubMsg: CoreMessage = {
554+ role: "assistant",
555+ content: "[interrupted]",
556+ };
557+ session.messages.push(userMsg, stubMsg);
558+ saveMessages(session.id, [userMsg, stubMsg], thisTurn);
559+ coreHistory.push(userMsg, stubMsg);
560+ snapshotStack.push(snapped ? thisTurn : null);
561+ touchActiveSession(session);
562+ return "";
563+ }
564+
557565 session.messages.push(userMsg);
558566 saveMessages(session.id, [userMsg], thisTurn);
559567 coreHistory.push(userMsg);
@@ -607,9 +615,20 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
607615 coreHistory.push(...newMessages);
608616 session.messages.push(...newMessages);
609617 saveMessages(session.id, newMessages, thisTurn);
618+ } else if (wasAborted) {
619+ // Ctrl+C mid-stream: the LLM reply was partial or empty.
620+ // Save a stub assistant message so history stays valid.
621+ // /undo is the only way to erase a turn.
622+ const stubMsg: CoreMessage = {
623+ role: "assistant",
624+ content: "[interrupted]",
625+ };
626+ coreHistory.push(stubMsg);
627+ session.messages.push(stubMsg);
628+ saveMessages(session.id, [stubMsg], thisTurn);
610629 } else {
611- // Turn produced no assistant reply (e.g. schema validation error or
612- // abort). Roll back the user message so the next user input doesn't
630+ // Turn produced no assistant reply due to an error (e.g. schema
631+ // validation failure). Roll back so the next user input doesn't
613632 // create two consecutive user messages.
614633 rollbackTurn();
615634 }