Skip to content

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

fix(sessions): catch cold-read idle edits with a plain cache-reference compare

liuzixiang80e25f37f1a9
diff --git a/apps/desktop/src/stores/slices/session-slice.ts b/apps/desktop/src/stores/slices/session-slice.ts
index 84afa813e..a24d03a5f 100644
--- a/apps/desktop/src/stores/slices/session-slice.ts
+++ b/apps/desktop/src/stores/slices/session-slice.ts
@@ -573,9 +573,11 @@ export function createSessionSlice({
// cache entry; committing the stale window now would revert that edit
// on screen. #2233 taught loadSessionDetail itself to refuse caching
// such a read — the caller-level cache write and commit need the same
- // guard.
+ // guard. The comparison needs no "captured an entry" precondition: a
+ // cold read captures undefined, and an edit that lands in the window
+ // moves the cache from undefined to an entry — exactly the movement
+ // the runtime guard's plain `!== previous` catches there.
const transcriptMovedWhileReading =
- cacheBeforeDetailRead !== undefined &&
runtime.sessionTranscriptCache.get(id) !== cacheBeforeDetailRead &&
!runtime.liveSessionTranscripts.has(id) &&
currentState.runningSessions[id] !== true;
diff --git a/apps/desktop/test/session-select-stale-read.test.mjs b/apps/desktop/test/session-select-stale-read.test.mjs
index 6643aae60..9b8397df5 100644
--- a/apps/desktop/test/session-select-stale-read.test.mjs
+++ b/apps/desktop/test/session-select-stale-read.test.mjs
@@ -96,3 +96,41 @@ test("a running session still merges its live tail over the detail window", asyn
assert.equal(runtime.sessionTranscriptCache.get("session").length, 3, "the live tail survives for running sessions");
assert.equal(stateRef().messages.length, 3);
});
+
+test("a cold first read also keeps an idle edit that lands during the read", async t => {
+ // No cache entry exists when the read starts, so the pre-read capture is
+ // undefined. An edit that lands in the window moves the cache from
+ // undefined to the edit writer's entry — the same movement the #2233
+ // runtime guard catches with a plain `!== previous`; the caller guard must
+ // not require a defined capture to see it.
+ const stored = { id: "answer", role: "assistant", content: "before edit" };
+ const edited = [{ ...stored, content: "after edit" }];
+ const { stateRef, runtime, slice } = buildStore();
+ let finish;
+ t.mock.method(api, "getSession", () => new Promise(resolve => { finish = resolve; }));
+ const selecting = slice.selectSession("session");
+ await new Promise(resolve => setImmediate(resolve));
+ runtime.cacheSessionTranscript("session", edited, { messageStart: 0, hasMoreBefore: false });
+ stateRef().messages = edited;
+ finish({ session: { id: "session", messages: [stored], messageStart: 0, hasMoreBefore: false } });
+ await selecting;
+ assert.equal(runtime.sessionTranscriptCache.get("session"), edited, "the edit cache survives the cold read");
+ assert.equal(stateRef().messages, edited, "the visible transcript keeps the edit");
+ assert.equal(stateRef().selectingSessionId, undefined, "the selection still completes");
+});
+
+test("a cold first read without edits still hydrates the detail window", async t => {
+ // The runtime caches this read's own detail (nothing moved before it), so
+ // the guard trips on that self-write and falls back to the cache entry —
+ // the same content the plain path would have committed.
+ const saved = { id: "answer", role: "assistant", content: "saved answer" };
+ const { stateRef, runtime, slice } = buildStore();
+ t.mock.method(api, "getSession", async () => ({
+ session: { id: "session", messages: [saved], messageStart: 5, hasMoreBefore: true },
+ }));
+ await slice.selectSession("session");
+ assert.deepEqual(stateRef().messages, [saved]);
+ assert.deepEqual(runtime.sessionTranscriptCache.get("session"), [saved]);
+ assert.deepEqual(stateRef().sessionHistory.session,
+ { messageStart: 5, hasMoreBefore: true, contentLimited: true });
+});