xonecas.io
64875
Mc

mini-coder / commit cc56c82

fix: never auto-rollback turns, preserve history on interrupt or error

Sean2026-03-01 cc56c829164b85cac5904b12fb158462ffb000a1 parent 59bdcd4

2 files changed

src/agent/agent.ts+33−29
@@ -561,23 +561,9 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
561561 const systemPrompt = buildSystemPrompt(cwd);
562562
563563 let lastAssistantText = "";
564- // Tracks whether the turn was fully committed (assistant messages saved).
565- // Used by rollbackTurn() to avoid double-rolling-back.
566- let turnRolledBack = false;
567-
568- // Roll back the user message saved before the LLM call, so a failed turn
569- // doesn't leave two consecutive user messages that would permanently break
570- // the session by failing ModelMessage[] schema validation.
571- const rollbackTurn = () => {
572- if (turnRolledBack) return;
573- turnRolledBack = true;
574- coreHistory.pop();
575- session.messages.pop();
576- deleteLastTurn(session.id, thisTurn);
577- if (snapped) deleteSnapshot(session.id, thisTurn);
578- snapshotStack.pop();
579- turnIndex--; // safe: thisTurn = turnIndex++ so turnIndex >= 1 here
580- };
564+ // Tracks whether the catch block stub has already been saved, to avoid
565+ // double-appending if somehow both paths fire.
566+ let errorStubSaved = false;
581567
582568 try {
583569 // Always push so the stack stays aligned 1:1 with turns. A null entry means
@@ -606,22 +592,29 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
606592 coreHistory.push(...newMessages);
607593 session.messages.push(...newMessages);
608594 saveMessages(session.id, newMessages, thisTurn);
609- } else if (wasAborted) {
610- // Ctrl+C mid-stream: the LLM reply was partial or empty.
611- // Save a stub assistant message so history stays valid.
612- // /undo is the only way to erase a turn.
595+ if (wasAborted) {
596+ // Had partial content — append a system message block so the model
597+ // 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+ };
603+ coreHistory.push(note);
604+ session.messages.push(note);
605+ saveMessages(session.id, [note], thisTurn);
606+ }
607+ } else {
608+ // No messages returned — interrupted before any content. Save a
609+ // synthetic assistant message. /undo is the only way to erase a turn.
613610 const stubMsg: CoreMessage = {
614611 role: "assistant",
615- content: "[interrupted]",
612+ content:
613+ "<system-message>Response was interrupted by the user.</system-message>",
616614 };
617615 coreHistory.push(stubMsg);
618616 session.messages.push(stubMsg);
619617 saveMessages(session.id, [stubMsg], thisTurn);
620- } else {
621- // Turn produced no assistant reply due to an error (e.g. schema
622- // validation failure). Roll back so the next user input doesn't
623- // create two consecutive user messages.
624- rollbackTurn();
625618 }
626619
627620 // Collect all assistant text from this turn so the ralph loop can detect
@@ -633,8 +626,19 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
633626 lastContextTokens = contextTokens;
634627 touchActiveSession(session);
635628 } catch (err) {
636- // Unexpected throw (network failure, etc.). Roll back if not already done.
637- rollbackTurn();
629+ // Unexpected throw (network failure, etc.). Append a system message block
630+ // so history stays valid. /undo is the only way to erase a turn.
631+ if (!errorStubSaved) {
632+ errorStubSaved = true;
633+ const stubMsg: CoreMessage = {
634+ role: "assistant",
635+ content:
636+ "<system-message>Response was interrupted due to an error.</system-message>",
637+ };
638+ coreHistory.push(stubMsg);
639+ session.messages.push(stubMsg);
640+ saveMessages(session.id, [stubMsg], thisTurn);
641+ }
638642 throw err;
639643 } finally {
640644 stopWatcher();
src/tools/shell.test.ts+2−2
@@ -14,7 +14,7 @@ describe("shellTool", () => {
1414 expect(process.stdin.isRaw).toBe(true);
1515
1616 // Execute a quick command
17- await shellTool.execute({ command: "echo hello" });
17+ await shellTool.execute({ command: "echo hello", timeout: 30_000 });
1818
1919 // Verify it restored raw mode
2020 expect(process.stdin.isRaw).toBe(true);
@@ -34,7 +34,7 @@ describe("shellTool", () => {
3434 expect(process.stdin.isRaw).toBe(false);
3535
3636 // Execute a quick command
37- await shellTool.execute({ command: "echo hello" });
37+ await shellTool.execute({ command: "echo hello", timeout: 30_000 });
3838
3939 // Verify it stayed false
4040 expect(process.stdin.isRaw).toBe(false);