Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(desktop): undo optimistic deletes when host reports no change
Seraph-x2549e596e7d1
diff --git a/apps/desktop/src/stores/slices/transcript-slice.ts b/apps/desktop/src/stores/slices/transcript-slice.tsindex b6791acc3..af0eb3541 100644--- a/apps/desktop/src/stores/slices/transcript-slice.ts+++ b/apps/desktop/src/stores/slices/transcript-slice.ts@@ -420,23 +420,31 @@ export function createTranscriptSlice({errorCode: null,}));try {- await api.deleteSessionMessage({ sessionId, messageId });+ const result = await api.deleteSessionMessage({ sessionId, messageId });+ if (result.deleted) return;} catch (error) {- const current = get();- const rollback = mergeLiveSessionMessages(- previous,- current.activeSessionId === sessionId- ? current.messages- : runtime.sessionTranscriptCache.get(sessionId) ?? [],- );- runtime.cacheSessionTranscript(sessionId, rollback, fullWindow);- set((current) => ({- messages:- current.activeSessionId === sessionId ? rollback : current.messages,+ set({error: error instanceof Error ? error.message : String(error),errorCode: (error as { code?: string })?.code ?? null,- }));+ });}+ // A missing durable target is a Host no-op, so undo the optimistic+ // removal just as on failure. Keep callbacks received during the RPC,+ // including completed updates, rather than restoring their old values.+ const current = get();+ const live = current.activeSessionId === sessionId+ ? current.messages+ : runtime.sessionTranscriptCache.get(sessionId) ?? [];+ const liveById = new Map(live.map((message) => [message.id, message]));+ const rollback = mergeLiveSessionMessages(+ previous.map((message) => liveById.get(message.id) ?? message),+ live,+ );+ runtime.cacheSessionTranscript(sessionId, rollback, fullWindow);+ set((current) => ({+ messages:+ current.activeSessionId === sessionId ? rollback : current.messages,+ }));} finally {releaseDelete();if (pendingMessageDeletes.get(sessionId) === completion) {diff --git a/apps/desktop/test/bounded-history-mutations.test.mjs b/apps/desktop/test/bounded-history-mutations.test.mjsindex 62c47544b..be2869023 100644--- a/apps/desktop/test/bounded-history-mutations.test.mjs+++ b/apps/desktop/test/bounded-history-mutations.test.mjs@@ -294,3 +294,55 @@ test("a queued delete cannot switch to a different active session and releases iassert.deepEqual(calls.at(-1), { sessionId: "s", messageId: "u2" });assert.deepEqual(h.state().messages, []);});++test("a missing durable delete target restores the optimistic user-turn projection and cache", async () => {+ const durable = previous.filter((message) => message.id !== "u1");+ const cached = new Map();+ const h = harness({ runtime: {+ loadFullSessionMessages: async () => durable,+ sessionTranscriptCache: cached,+ cacheSessionTranscript: (id, messages) => cached.set(id, messages),+ }, api: { deleteSessionMessage: async () => ({ ok: true, deleted: false, deletedCount: 0 }) } });+ await h.actions.deleteMessage("u1");+ assert.deepEqual(h.state().messages, previous, "a Host no-op cannot hide durable replies after a live-only prompt");+ assert.deepEqual(cached.get("s"), previous);+ assert.equal(h.state().error, null, "a missing target is an acknowledged no-op, not an RPC failure");+});++for (const outcome of ["no-op", "failure"]) {+ for (const switchSession of [false, true]) {+ test(`${outcome} delete restores its own projection while preserving a new turn, late terminal update, and ${switchSession ? "session switch" : "active session"}`, async () => {+ const started = deferred();+ const response = deferred();+ const cached = new Map();+ const initial = previous.map((message) => message.id === "a2"+ ? { ...message, content: "partial reply", status: "streaming" } : message);+ const h = harness({ state: { messages: initial }, runtime: {+ loadFullSessionMessages: async () => initial,+ sessionTranscriptCache: cached,+ cacheSessionTranscript: (id, messages) => cached.set(id, messages),+ }, api: { deleteSessionMessage: async () => {+ started.resolve();+ await response.promise;+ if (outcome === "failure") throw new Error("delete failed");+ return { ok: true, deleted: false, deletedCount: 0 };+ } } });+ const deleting = h.actions.deleteMessage("u1");+ await started.promise;+ const completed = { ...row("a2", "assistant"), content: "late completed reply" };+ const live = [row("u2", "user"), completed, row("u3", "user"), { ...row("a3", "assistant"), status: "streaming" }];+ const other = [row("other", "user")];+ cached.set("s", live);+ h.update({ activeSessionId: switchSession ? "other" : "s", messages: switchSession ? other : live,+ isRunning: true, runningSessions: { s: true, ...(switchSession ? { other: true } : {}) } });+ response.resolve();+ await deleting;+ const restored = [...previous.slice(0, 3), ...live];+ assert.deepEqual(cached.get("s"), restored);+ assert.deepEqual(h.state().messages, switchSession ? other : restored);+ assert.equal(h.state().isRunning, true);+ assert.equal(h.state().runningSessions.s, true);+ assert.equal(h.state().error, outcome === "failure" ? "delete failed" : null);+ });+ }+}diff --git a/apps/desktop/test/persistence-outbox.test.mjs b/apps/desktop/test/persistence-outbox.test.mjsindex 2c77279a3..19f042921 100644--- a/apps/desktop/test/persistence-outbox.test.mjs+++ b/apps/desktop/test/persistence-outbox.test.mjs@@ -418,9 +418,13 @@ test("quarantined appends survive a restart without being replayed (#1560)", asy0,);const before = calls.length;+ const retained = await readFile(join(dir, DEAD_LETTER_FILE), "utf8");await reopened.flush(() => host);- assert.equal(calls.length, before, "no replay against the host");- const dead = JSON.parse(await readFile(join(dir, DEAD_LETTER_FILE), "utf8"));+ await reopened.prepareSessionMutation("s1", () => host);+ await reopened.flush(() => host);+ assert.equal(calls.length, before, "neither restart nor mutation preparation replays a dead letter");+ assert.equal(await readFile(join(dir, DEAD_LETTER_FILE), "utf8"), retained, "manual recovery retains the original payload");+ const dead = JSON.parse(retained);assert.deepEqual(dead.map((entry) => entry.key),["message:s1:torn"],