Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(desktop): preserve live AI outputs on message deletion and clear obsolete outbox appends on replacement

Morning·Galaxy Dawnd5076e35ce76
diff --git a/apps/desktop/electron/main/ipc/session-ipc.ts b/apps/desktop/electron/main/ipc/session-ipc.ts
index e7a145312..95fa24d6d 100644
--- a/apps/desktop/electron/main/ipc/session-ipc.ts
+++ b/apps/desktop/electron/main/ipc/session-ipc.ts
@@ -441,6 +441,7 @@ export function registerSessionIpc({
const sessionId = String(input?.sessionId || "");
if (!sessionId) throw new Error("sessionId required");
rejectNativeMutation(sessionId, "transcript replacement");
+ await persistenceOutbox.dropSession(sessionId);
// Drop the live pi-agent so the next prompt reseeds from the truncated
// transcript instead of replaying the discarded branch in memory.
if (sidecar) {
diff --git a/apps/desktop/src/stores/slices/transcript-slice.ts b/apps/desktop/src/stores/slices/transcript-slice.ts
index 3e3b0ae0a..02991a4c9 100644
--- a/apps/desktop/src/stores/slices/transcript-slice.ts
+++ b/apps/desktop/src/stores/slices/transcript-slice.ts
@@ -361,12 +361,14 @@ export function createTranscriptSlice({
if (!canEdit()) return;
const sessionId = get().activeSessionId;
if (!sessionId || get().isRunning) return;
+ const liveMessages = get().messages;
const fullMessages = await runtime.loadFullSessionMessages(sessionId);
if (!fullMessages || get().activeSessionId !== sessionId) return;
+ const merged = mergeLiveSessionMessages(fullMessages, liveMessages);
set((current) =>
current.activeSessionId === sessionId
? {
- messages: fullMessages,
+ messages: merged,
sessionHistory: {
...current.sessionHistory,
[sessionId]: { messageStart: 0, hasMoreBefore: false },
diff --git a/apps/desktop/test/delete-message-preserves-transcript.test.mjs b/apps/desktop/test/delete-message-preserves-transcript.test.mjs
new file mode 100644
index 000000000..f9aa0bdde
--- /dev/null
+++ b/apps/desktop/test/delete-message-preserves-transcript.test.mjs
@@ -0,0 +1,77 @@
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import test from "node:test";
+import { readStoreModule, readMainSource } from "./helpers/source-contracts.mjs";
+
+register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));
+const { mergeLiveSessionMessages } = await import("../src/lib/session-transcript.ts");
+
+test("deleteMessage merges live in-memory messages before slicing out the deleted turn", async () => {
+ const sliceSource = await readStoreModule("slices/transcript-slice.ts");
+
+ // Contract: deleteMessage must capture current live messages and merge with fullMessages
+ assert.match(
+ sliceSource,
+ /deleteMessage:\s*async\s*\(messageId\)\s*=>\s*\{[\s\S]*?const\s+liveMessages\s*=\s*get\(\)\.messages;/,
+ "deleteMessage must read get().messages as liveMessages before overwriting",
+ );
+ assert.match(
+ sliceSource,
+ /mergeLiveSessionMessages\(fullMessages,\s*liveMessages\)/,
+ "deleteMessage must merge fullMessages with liveMessages using mergeLiveSessionMessages",
+ );
+});
+
+test("sessionReplaceMessages IPC drops stale pending outbox appends for the session", async () => {
+ const sessionIpcSource = await readMainSource("ipc/session-ipc.ts");
+
+ assert.match(
+ sessionIpcSource,
+ /IPC\.invoke\.sessionReplaceMessages[\s\S]*?await\s+persistenceOutbox\.dropSession\(sessionId\);/,
+ "sessionReplaceMessages must drop stale outbox appends before or during replacement",
+ );
+});
+
+test("mergeLiveSessionMessages preserves live AI outputs when backend disk is missing them, and only deleted turn is removed", () => {
+ // Simulate backend host-core having only user messages persisted (e.g. outbox was pending or full)
+ const durableMessages = [
+ { id: "u1", role: "user", content: "first task", status: "complete" },
+ { id: "u2", role: "user", content: "second task", status: "complete" },
+ { id: "u3", role: "user", content: "third task", status: "complete" },
+ ];
+
+ // Live messages currently in renderer UI (containing streaming/completed AI outputs for all turns)
+ const liveMessages = [
+ { id: "u1", role: "user", content: "first task", status: "complete" },
+ { id: "a1", role: "assistant", content: "first ai answer", status: "complete" },
+ { id: "u2", role: "user", content: "second task", status: "complete" },
+ { id: "t2", role: "tool", content: "tool result for second task", status: "complete" },
+ { id: "a2", role: "assistant", content: "second ai answer", status: "complete" },
+ { id: "u3", role: "user", content: "third task", status: "complete" },
+ { id: "a3", role: "assistant", content: "third ai answer", status: "complete" },
+ ];
+
+ // 1. Merge must preserve all live AI outputs
+ const merged = mergeLiveSessionMessages(durableMessages, liveMessages);
+ assert.equal(merged.length, 7, "All 7 messages must be preserved after merging");
+
+ // 2. Perform the exact deletion logic as deleteMessage on turn 2 ("u2")
+ const index = merged.findIndex((m) => m.id === "u2");
+ assert.equal(index, 2);
+
+ let end = index + 1;
+ while (end < merged.length && merged[end].role !== "user") {
+ end += 1;
+ }
+ assert.equal(end, 5, "Turn 2 covers u2, t2, a2 (indices 2, 3, 4)");
+
+ const next = [...merged.slice(0, index), ...merged.slice(end)];
+ const nextIds = next.map((m) => m.id);
+
+ // 3. Assert that turn 1 (u1, a1) and turn 3 (u3, a3) retain their complete user + AI messages!
+ assert.deepEqual(nextIds, ["u1", "a1", "u3", "a3"]);
+ assert.equal(next[0].role, "user");
+ assert.equal(next[1].role, "assistant");
+ assert.equal(next[2].role, "user");
+ assert.equal(next[3].role, "assistant");
+});