Skip to content

fix(sessions): keep selectSession from reverting edits made during its detail read - #2274

Closed
SeanLiu326 wants to merge 3 commits into
vastsa:mainfrom
SeanLiu326:fix/select-session-stale-read
Closed

SeanLiu326 wants to merge 3 commits into
vastsa:mainfrom
SeanLiu326:fix/select-session-stale-read

Conversation

@SeanLiu326

Copy link
Copy Markdown
Contributor

Fixes #2273.

What

A bounded detail read that selectSession starts on a warm re-select could silently revert an idle-session edit made while the read was in flight: deleteMessage / activateMessageRevision replaced the transcript cache entry and state.messages, and the finished pre-edit read then re-cached and committed its stale window. #2233 fixed exactly this shape inside loadSessionDetail — but the runtime guard only skips the cache write and still returns the stale detail, and selectSession, the read's biggest caller, re-cached and committed it unconditionally. Its only continuation guard (navigationIntentIsCurrent) does not observe edits, and the liveMessages merge path engages for running sessions only, so an idle-session writer was unprotected.

The fix

Mirror the runtime guard at the caller, inside selectSession only (no new module):

  • capture the cache reference right before awaiting the in-flight read;
  • transcriptMovedWhileReading = an entry existed before the read, the entry is a different array now, and the session is idle (no live flag, not running);
  • when superseded: skip the cacheSessionTranscript write and commit the edit writer's cache content with its cached window (via commitSelection's existing default) instead of the stale window. Otherwise the behavior is identical to before.

Tests

  • New session-select-stale-read.test.mjs drives the real slice through the existing minimal store harness: (1) a superseded read keeps the edit in the cache, state.messages, and the window while the selection still completes; (2) an uneventful re-select still hydrates the read window and clears selectingSessionId; (3) a running session still merges its live tail over the detail window.
  • session-switch-performance.test.mjs's source pin updated from the old call shape to the guarded one (intent preserved: the final commit carries the loaded window unless superseded).
  • node --test session + transcript families green (34/34 in the six directly related suites, full session-*/transcript-* families in the run log); tsc -p apps/desktop/tsconfig.json --noEmit clean.

Branch contains latest main d128b12e2.

…s detail read

selectSession awaited the bounded detail read and then re-cached and committed its window unconditionally. An idle-session edit (message delete, revision switch) that landed while the read was in flight replaces the transcript cache entry, so committing the stale window reverted the edit on screen and in the cache. vastsa#2233 taught loadSessionDetail itself to refuse caching such a read; the caller-level commit now applies the same guard: when the cache entry moved during the read for an idle session, the commit keeps the edit writer's cache content and cached window instead.

Pinned by session-select-stale-read.test.mjs driving the real slice (superseded read keeps the edit; uneventful re-select still hydrates; running sessions still merge the live tail).

@aiuo-codepilot aiuo-codepilot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AIUO CodePilot · Preliminary review

Diff-only preliminary review of the selectSession stale-read guard and its tests. The caller-level cache-reference comparison is a reasonable mirror of the #2233 runtime guard, but one edge case is not covered: an edit that lands while a cold-cache read is in flight is still reverted, because the guard requires a pre-read cache entry. Verified against the added diff only; no tests, typecheck, runtime behavior, or repository state were checked.

  • medium apps/desktop/src/stores/slices/session\-slice\.ts:533: The guard requires `cacheBeforeDetailRead !== undefined`, so an idle-session edit that lands during a cold-cache read (no transcript cache entry captured before `await detailPromise`) cannot be detected. The edit writer's cache entry is then skipped by `cacheSessionTranscript`, but `commitSelection` still receives `selectedMessages`/`historyWindow`, i.e. the stale detail window, reverting the just-made edit on screen — the same #2273 shape this PR targets, only for the first-read path.
    Evidence: const cacheBeforeDetailRead = runtime\.sessionTranscriptCache\.get\(id\);

Head: 9056cfd
Base: d128b12
CI: success; mergeable: true.

Validation: code/tests NOT EXECUTED. Diff-only preliminary analysis; not approval. Human review and repository gates remain required.

…e compare

The guard required a captured cache entry before comparing, so a read
that started with no transcript cache entry could never see the movement
an in-flight idle edit makes (undefined -> entry). The runtime guard
this mirrors (vastsa#2233) needs no such precondition; dropping it keeps the
caller-level compare aligned with that semantics. Warm re-selects are
unaffected: the comparison is the same reference check it always was,
and a quiet read whose runtime write replaces the entry still resolves
to the same window content.
@SeanLiu326

Copy link
Copy Markdown
Contributor Author

Took the cold-cache edge from the review seriously and traced it through the real selection flow (36169c655).

What the trace showed: in a plain re-select the cold-read capture is rarely undefined — selectSession snapshots the active session's projection into the cache before awaiting (session-slice.ts:405-411), which for a same-id re-select writes an entry and satisfies the old !== undefined precondition indirectly. The genuinely reachable gap was the cold-start first selection (empty activeSessionId, no projection write) plus an idle edit landing in the read window from a retained pane.

Fix: the guard now uses the same plain reference comparison as the runtime guard it mirrors — runtime.sessionTranscriptCache.get(id) !== cacheBeforeDetailRead with no captured-entry precondition. undefined !== entry catches exactly the cold-start movement the review described; a quiet read whose runtime write replaces the entry resolves to the same window content, and warm re-selects compare the same references as before.

New coverage (session-select-stale-read.test.mjs): a cold first read keeps an idle edit that lands mid-read, and a cold first read without edits still hydrates the detail window (pins the guard's fallback chain through the runtime's own cache write). Suite is 5/5; image-conversation-selection + user-select stay green (14/14 across the three files) and tsc --noEmit is clean.

@vastsa

vastsa commented Nov 13, 2026

Copy link
Copy Markdown
Owner

The revert is real. Two issues with the guard: (1) the snapshot is taken just before detail ??= await detailPromise, but the read started earlier and the summary path awaits queueWorkspaceAlignment in between, so an idle edit in that window is not detected and the stale window still commits; (2) it re-implements in the caller the staleness check loadSessionDetail already performs. Prefer having loadSessionDetail report that it refused to cache (e.g. return a stale flag from the same comparison it already makes), and have selectSession honor that single signal. Please add a test for an edit that lands during workspace alignment.

@vastsa

vastsa commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Thanks @SeanLiu326 — both of your commits landed in #2358 with authorship preserved. On top: the stale-read baseline is taken before workspace alignment (covers the edit-during-alignment window from review), and an evicted cache entry is not treated as an edit. A loadSessionDetail-returned stale flag was not used because its baseline predates selectSession's own provisional commits, which would make a retained background pane look like an edit; a regression test pins that. Closing as superseded.

@vastsa vastsa closed this Nov 15, 2026

This branch was previously deployed

1 inactive deployment
Preview — e89221b9 Deployed Nov 12, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

selectSession re-caches and commits a stale detail window, reverting an idle edit made during the read

2 participants