Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ai): drop the rewound message from the agent's history

EtienneLescotdf57e9d3c224
diff --git a/electron/ai-edition/chat-persistence.test.ts b/electron/ai-edition/chat-persistence.test.ts
index dcb05265a..8c57b01a5 100644
--- a/electron/ai-edition/chat-persistence.test.ts
+++ b/electron/ai-edition/chat-persistence.test.ts
@@ -106,6 +106,30 @@ describe("local Agent chat history", () => {
]);
});
+ it("drops the rewound message from the saved history and the next turn's payload", async () => {
+ const session = createSession("proj_rewound");
+ const document = createEmptyDocument({ title: "Test", projectId: "proj_rewound" });
+ await runChat("proj_rewound", session.id, "smart cuts", config(), document);
+ await runChat("proj_rewound", session.id, "add an annotation and a zoom", config(), document);
+ const rewound = selectSession("proj_rewound", session.id)?.messages[2];
+ const undo = rewindToMessage("proj_rewound", session.id, rewound?.id ?? "");
+ expect(undo.success && undo.prompt).toBe("add an annotation and a zoom");
+ const kept = selectSession("proj_rewound", session.id)?.messages;
+ expect(kept?.map((m) => m.content)).toEqual(["smart cuts", "reply"]);
+ // The earlier message can still be rewound.
+ expect(kept?.[0]?.checkpointId).toBeTruthy();
+ await flushChatPersistence();
+ expect(readFileSync(path.join(root, "proj_rewound.chat.json"), "utf8")).not.toContain(
+ "add an annotation",
+ );
+
+ await restart();
+ await runChat("proj_rewound", session.id, "export it", config());
+ const sent = invokeMock.mock.lastCall?.[0];
+ const payload = [...(sent?.history ?? []).map((m) => m.content), sent?.userMessage];
+ expect(payload).not.toContain("add an annotation and a zoom");
+ });
+
it("restores compaction boundary for the model while keeping the full transcript", async () => {
modelMock.mockImplementation(
async () =>
diff --git a/electron/ai-edition/chat-service.ts b/electron/ai-edition/chat-service.ts
index c222a8de4..ab57b273b 100644
--- a/electron/ai-edition/chat-service.ts
+++ b/electron/ai-edition/chat-service.ts
@@ -127,9 +127,7 @@ function findCheckpointForMessage(
// ponytail: drop every checkpoint at or after the given user message id.
// Called after a rewind truncates the message list, so future checkpoints
-// stay aligned with the surviving messages. We compare against the
-// session's current message order; if the target message is gone we drop
-// all checkpoints created after the surviving tail.
+// stay aligned with the surviving messages.
function dropCheckpointsFrom(
projectId: string,
sessionId: string,
@@ -138,13 +136,10 @@ function dropCheckpointsFrom(
const key = sessionKey(projectId, sessionId);
const list = messageCheckpointsBySession.get(key);
if (!list) return;
- const session = sessionsByProject.get(projectId)?.get(sessionId);
- const surviving = session?.messages.find((m) => m.id === fromUserMessageId);
- const cutIndex = surviving ? list.findIndex((c) => c.userMessageId === fromUserMessageId) : -1;
+ const cutIndex = list.findIndex((c) => c.userMessageId === fromUserMessageId);
if (cutIndex === -1) {
- // ponytail: target message vanished (e.g. already rewound past it).
- // Drop the entire session's checkpoints — they all reference a
- // lineage that's no longer valid.
+ // ponytail: no checkpoint for the target means its lineage is already
+ // gone. Drop the entire session's checkpoints rather than guess.
messageCheckpointsBySession.delete(key);
return;
}
@@ -557,7 +552,9 @@ export function rewindToMessage(
};
}
- const survived = session.messages.slice(0, messageIndex + 1).map((m) => ({
+ // The rewound message goes too: its text goes back to the composer, and left
+ // in the history unanswered, the next turn would carry it out again.
+ const survived = session.messages.slice(0, messageIndex).map((m) => ({
...m,
toolCalls: m.toolCalls ? [...m.toolCalls] : undefined,
}));