xonecas.io
64875
Mc

mini-coder / commit c0a0cf6

fix: resolve bugs in /undo related to state synchronization and shell execution

This commit correctly re-aligns the in-memory array state from the database as opposed to doing fragile array slicing to handle turns with multiple user messages. It also makes sure to push a null item to the snapshotStack to avoid alignment bugs between turnIndex and snapshotStack on shell input.

xonecas2026-03-08 c0a0cf68dbc2c85c9c8c5f3e8b828f7e7b91cd14 parent 11a2103

2 files changed

src/agent/input-loop.ts+1−0
@@ -64,6 +64,7 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
6464 runner.session.messages.push(msg);
6565 saveMessages(runner.session.id, [msg], thisTurn);
6666 runner.coreHistory.push(msg);
67+ runner.snapshotStack.push(null);
6768 }
6869 continue;
6970 }
src/agent/undo-snapshot.ts+11−24
@@ -1,5 +1,5 @@
11 import type { CoreMessage } from "../llm-api/turn.ts";
2-import { deleteLastTurn } from "../session/db/index.ts";
2+import { deleteLastTurn, loadMessages } from "../session/db/index.ts";
33 import type { ActiveSession } from "../session/manager.ts";
44 import {
55 restoreSnapshot,
@@ -24,34 +24,21 @@ export async function undoLastTurn(ctx: UndoContext): Promise<boolean> {
2424 // Nothing to undo if there are no messages
2525 if (session.messages.length === 0) return false;
2626
27- // Find the message index where the last turn starts (last user message)
28- let lastUserIdx = -1;
29- for (let i = session.messages.length - 1; i >= 0; i--) {
30- if (session.messages[i]?.role === "user") {
31- lastUserIdx = i;
32- break;
33- }
34- }
35- if (lastUserIdx === -1) return false;
36-
37- // Trim in-memory DB history
38- session.messages.splice(lastUserIdx);
39-
40- // Trim coreHistory to match — find last user message in coreHistory
41- let coreLastUserIdx = -1;
42- for (let i = coreHistory.length - 1; i >= 0; i--) {
43- if (coreHistory[i]?.role === "user") {
44- coreLastUserIdx = i;
45- break;
46- }
47- }
48- if (coreLastUserIdx !== -1) coreHistory.splice(coreLastUserIdx);
49-
5027 // Delete from DB and decrement turn counter
5128 const deleted = deleteLastTurn(session.id);
29+ if (!deleted) return false;
30+
5231 const poppedTurn = snapshotStack.pop() ?? null;
5332 if (turnIndex > 0) ctx.setTurnIndex(turnIndex - 1);
5433
34+ // Sync in-memory history reliably with DB state
35+ const remaining = loadMessages(session.id);
36+ session.messages.length = 0;
37+ session.messages.push(...remaining);
38+
39+ coreHistory.length = 0;
40+ coreHistory.push(...remaining);
41+
5542 // Restore files from the SQLite snapshot for the turn being undone
5643 if (poppedTurn !== null) {
5744 const restoreResult: SnapshotRestoreResult = await restoreSnapshot(