Skip to content

fix(transcript): keep a turn started during Stop's settled read in charge of the transcript - #2409

Merged
vastsa merged 3 commits into
vastsa:mainfrom
SeanLiu326:fix/stop-settled-read-live-turn
Nov 21, 2026
Merged

vastsa merged 3 commits into
vastsa:mainfrom
SeanLiu326:fix/stop-settled-read-live-turn

Conversation

@SeanLiu326

Copy link
Copy Markdown
Contributor

Fixes #2408. Sibling of #2385 — same late-read family, triggered by the native/remote Stop branch instead of delete.

What

After the #2383 confirmation, the native/remote Stop branch clears isRunning immediately (unlocking the composer) and then awaits a settled-state getSession. A prompt submitted during that read starts a real turn, and the read's resolution then wholesale-replaced messages with its pre-turn snapshot, forced isRunning: false, and wrote the stale transcript into the session cache and retained transcripts: the new turn's optimistic row vanished, the spinner muted mid-stream, and the session became double-sendable. Remote sessions widen the window to network latency. The reconciliation's only guard was activeSessionId === sessionId — it never asked whether a NEW turn owns the transcript.

The fix

When the settled read resolves, skip the reconciliation entirely if a turn owns the transcript (runningSessions[sessionId], liveSessionTranscripts membership, or the active session's isRunning): the newer state stands and the live flow reconciles. No turn restarted → the settled read lands exactly as before, byte-identical behavior.

Tests

native-stop-refresh-failure.test.mjs gains, for pi-native and remote:

  • a turn started during the settled read keeps its optimistic rows (messages identity), spinner (isRunning/runningSessions stay true), and neither the session cache nor the retained transcripts receive the stale snapshot;
  • with no restart, the settled rows still land, cache included, and the session stays stopped.

Existing failure and navigation cases pass untouched. Full stop-family node --test 9/9; desktop tsc --noEmit clean. Branch contains latest main 047c4eab1.

…arge of the transcript

The native/remote Stop branch clears isRunning as soon as the abort RPC resolves, unlocking the composer, and then awaits a settled-state getSession. A prompt submitted while that read is in flight starts a real turn - optimistic user row, runningSessions and isRunning back to true - and the read then wholesale-replaced messages with its pre-turn snapshot, forced isRunning false, and wrote the stale transcript into the session cache and retained transcripts: the new turn's row vanished, its spinner muted, and double sends became possible. Remote sessions widen the window to network latency.

The settled read is now skipped entirely when a turn owns the transcript by the time it resolves (runningSessions flagged, liveSessionTranscripts membership, or the active session's isRunning): the newer state stands and the live flow reconciles from here. When no turn restarted, the settled read lands exactly as before. Regression tests cover both cases for pi-native and remote, alongside the existing failure and navigation cases.
…the settled-read guard

The duplicate-compact review harness and native-pi-sessions tests build minimal runtime stubs that carry only the members the old code touched; the new ownership guard called liveSessionTranscripts.has unconditionally and crashed them. Optional chaining keeps the same semantics (no membership means no ownership) without requiring the member.
@SeanLiu326

Copy link
Copy Markdown
Contributor Author

The red JS build was mine — found and fixed in 99b5b2fe0.

The failing test (native-pi-sessions "keeps one durable user row…") drives the same native Stop branch with a minimal runtime stub that predates liveSessionTranscripts; the new ownership guard called .has() unconditionally and crashed it with a TypeError. Optional chaining keeps identical semantics (no membership means no ownership) while tolerating the stub. Local on-branch: native-pi-sessions plus the stop family 33/33, tsc --noEmit clean. CI rerun is on the new head.

@vastsa

vastsa commented Nov 20, 2026

Copy link
Copy Markdown
Owner

The problem is real (transcript-slice.ts:492-504 replaces messages after the settled read without checking whether a new turn took over), but the guard introduces a regression.

The runtime.liveSessionTranscripts.has(sessionId) clause makes the guard fire even when no new turn started. Any turn that streamed is added to that set (events-slice.ts:320 on message_update/message_end/tool_*, session-runtime.ts:215, :378), and Stop never removes it — deletion only happens on session selection (session-slice.ts:626), project removal, or cache eviction. So after a normal Stop of a streamed pi-native/remote turn, the settled read is discarded and the transcript stays on the partial streaming row.

Repro: in "the settled read still lands when no turn restarted", add runtime.liveSessionTranscripts.add(id) before slice.abort(). It fails on this branch (messages stay [{content:"partial", status:"streaming"}]) and passes on main. The existing test passes only because its live set is empty, which a real turn never leaves it.

Dropping that clause (keeping runningSessions[sessionId] / active isRunning) and adding the populated-set case as a regression test would resolve it.

@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 Stop settled-read guard and its tests. The guard skips transcript/cache reconciliation when a newer turn appears to own the session, and new tests cover restart and no-restart paths for pi-native and remote. No evidenced defect found in the added code; note that the guard's `liveSessionTranscripts`/`isRunning` reliance and mock-based test construction were not verified against the live runtime, and no tests were executed here.

No evidenced findings in the supplied diff.

Head: f4ff7b6
Base: 95df2e0
CI: success; mergeable: true.

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

@vastsa
vastsa merged commit c558d61 into vastsa:main Nov 21, 2026
5 checks passed
dash0-app Bot added a commit to Nick-JuRan/PI-Desktop that referenced this pull request Nov 23, 2026
…-repair

Upstream's stop-path guard (vastsa#2409) asserts that a settled snapshot read
during Stop must leave no trace when a new turn owns the transcript. The
fork's bounded readSettledTail caches internally before the guard can
run, so the guard now drops that premature cache entry; the live turn's
projection re-caches its own messages immediately.

Upstream's transcript changes also pulled lib/api.ts into the
e2e-transcript-anchor-repair bundle graph for the first time, so its
bare @pi-desktop/shared source alias needs the fork's /fork subpath
alias beside it, as mode-run-elapsed.test.mjs already carries.

Co-authored-by: dash0-app[bot] <270606358+dash0-app[bot]@users.noreply.github.com>
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.

Stop's settled-state read overwrites a turn that started during the read (optimistic row lost, spinner muted)

2 participants