Skip to content

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

fix(sessions): baseline the stale-read guard before workspace alignment

vastsaa3d0610ce1c7
diff --git a/apps/desktop/src/stores/slices/session-slice.ts b/apps/desktop/src/stores/slices/session-slice.ts
index a24d03a5f..32d538920 100644
--- a/apps/desktop/src/stores/slices/session-slice.ts
+++ b/apps/desktop/src/stores/slices/session-slice.ts
@@ -497,6 +497,11 @@ export function createSessionSlice({
runtime.cacheSessionTranscript(id, [], EMPTY_SESSION_WINDOW);
commitSelection([], true, EMPTY_SESSION_WINDOW);
}
+ // Baseline for the stale-read guard below. It is taken after this
+ // selection's own provisional commits (which can move the cache entry
+ // themselves) but before workspace alignment, so an idle edit landing
+ // during alignment or the read is still detected.
+ const transcriptBeforeAlignment = runtime.sessionTranscriptCache.get(id);
if (summary) {
if (
!(await runtime.queueWorkspaceAlignment(() =>
@@ -531,7 +536,6 @@ export function createSessionSlice({
);
}
- const cacheBeforeDetailRead = runtime.sessionTranscriptCache.get(id);
detail ??= await detailPromise;
if (!runtime.navigationIntentIsCurrent(intent)) return;
if (detail.session && sessionReadLooksEmpty(detail.session)) {
@@ -569,18 +573,21 @@ export function createSessionSlice({
: { messageStart: 0, hasMoreBefore: false };
const currentState = get();
// An idle-session edit (message delete, revision switch) that landed
- // while the bounded read above was in flight replaces the transcript
- // 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. 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 =
- runtime.sessionTranscriptCache.get(id) !== cacheBeforeDetailRead &&
+ // while this selection aligned the workspace or awaited the bounded
+ // read replaces the transcript cache entry; committing the stale
+ // window now would revert that edit on screen. loadSessionDetail
+ // already refuses to cache such a read, but its baseline predates this
+ // selection's own provisional commits, so the caller keeps its own.
+ // An entry that vanished (cache eviction) is not an edit.
+ const transcriptNow = runtime.sessionTranscriptCache.get(id);
+ const supersedingTranscript =
+ transcriptNow !== undefined &&
+ transcriptNow !== transcriptBeforeAlignment &&
!runtime.liveSessionTranscripts.has(id) &&
- currentState.runningSessions[id] !== true;
+ currentState.runningSessions[id] !== true
+ ? transcriptNow
+ : undefined;
+ const transcriptMovedWhileReading = supersedingTranscript !== undefined;
const liveMessages =
(runningAtSelection ||
currentState.runningSessions[id] === true ||
@@ -595,11 +602,7 @@ export function createSessionSlice({
? mergeLiveSessionMessages(detail.session.messages ?? [], liveMessages)
: detail.session.messages ?? []
: liveMessages ?? [];
- const committedMessages = transcriptMovedWhileReading
- ? runtime.sessionTranscriptCache.get(id)
- ?? currentState.retainedTranscripts[id]
- ?? selectedMessages
- : selectedMessages;
+ const committedMessages = supersedingTranscript ?? selectedMessages;
if (detail.session) {
if (isRemoteSession(detail.session)) {
const { messages: _messages, ...summary } = detail.session;
diff --git a/apps/desktop/test/session-select-stale-read.test.mjs b/apps/desktop/test/session-select-stale-read.test.mjs
index 9b8397df5..b655cc21b 100644
--- a/apps/desktop/test/session-select-stale-read.test.mjs
+++ b/apps/desktop/test/session-select-stale-read.test.mjs
@@ -134,3 +134,51 @@ test("a cold first read without edits still hydrates the detail window", async t
assert.deepEqual(stateRef().sessionHistory.session,
{ messageStart: 5, hasMoreBefore: true, contentLimited: true });
});
+
+test("an idle edit that lands during workspace alignment survives the detail commit", async t => {
+ // The read starts before selectSession aligns the workspace; an edit made
+ // while the project activates is just as stale-making as one made while
+ // the read itself is in flight.
+ const stored = { id: "answer", role: "assistant", content: "before edit" };
+ const edited = [{ ...stored, content: "after edit" }];
+ const { stateRef, runtime, slice } = buildStore();
+ stateRef().sessions = [{ id: "session", mode: "agent", projectPath: "/project" }];
+ runtime.cacheSessionTranscript("session", [stored], { messageStart: 0, hasMoreBefore: false });
+ stateRef().messages = runtime.sessionTranscriptCache.get("session");
+ t.mock.method(api, "getSession", async () => ({
+ session: { id: "session", messages: [stored], messageStart: 0, hasMoreBefore: false },
+ }));
+ stateRef().activateProject = async () => {
+ runtime.cacheSessionTranscript("session", edited, { messageStart: 0, hasMoreBefore: false });
+ stateRef().messages = edited;
+ stateRef().activeProjectPath = "/project";
+ return { path: "/project" };
+ };
+ await slice.selectSession("session");
+ assert.equal(runtime.sessionTranscriptCache.get("session"), edited, "the edit cache survives");
+ assert.equal(stateRef().messages, edited, "the visible transcript keeps the edit");
+});
+
+test("selecting a retained background pane still hydrates the fresh detail", async t => {
+ // selectSession commits the retained pane itself before the read settles;
+ // that self-write must not be mistaken for a superseding edit.
+ const retained = [{ id: "answer", role: "assistant", content: "retained" }];
+ const fresh = [{ id: "answer", role: "assistant", content: "fresh" }];
+ const { stateRef, runtime, slice } = buildStore();
+ stateRef().activeSessionId = "other";
+ stateRef().sessions = [{ id: "session", mode: "agent" }, { id: "other", mode: "agent" }];
+ stateRef().retainedTranscripts = { session: retained };
+ Object.assign(stateRef(), {
+ workPanelOpen: false,
+ workPanelTabs: [],
+ activeWorkPanelTabId: null,
+ workPanelContexts: {},
+ });
+ t.mock.method(api, "getSession", async () => ({
+ session: { id: "session", messages: fresh, messageStart: 0, hasMoreBefore: false },
+ }));
+ await slice.selectSession("session");
+ assert.equal(stateRef().activeSessionId, "session");
+ assert.deepEqual(stateRef().messages, fresh);
+ assert.deepEqual(runtime.sessionTranscriptCache.get("session"), fresh);
+});