Skip to content

fix(flow-chat): repair turns truncated after switching back from a peer device - #3690

Merged
bobleer merged 3 commits into
openBitFun:mainfrom
bobleer:bob/settled-turn-after-surface-switch
Nov 2, 2026
Merged

bobleer merged 3 commits into
openBitFun:mainfrom
bobleer:bob/settled-turn-after-surface-switch

Conversation

@bobleer

@bobleer bobleer commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A local turn that is still running while the Desktop window renders a peer device (Remote Control → another client) can stay truncated after switching back to the local device. Reproduced on 1.0.8; main had the same code path.

Root cause

Traced with the reporter's own webview log and persisted session data:

  1. While a peer surface is rendered, surface routing drops the local turn's later chunks, its DialogTurnCompleted, and the SessionHistoryChanged fence.
  2. On return, the incremental backfill cannot serve the gap: the replayable tail holds 1024 events and almost every stream chunk is one. The refresh therefore falls back to restore_session_view. The host reports Idle, so no runtime projection is offered.
  3. The persisted copy of the turn was complete (21 rounds plus the final answer). It was still refused:
    • The local SessionStream never saw the terminal event, so it still owns the turn as executing. persistedReadMayReplaceTurn therefore fell through to isRunningSnapshotForwardProgress.
    • That check keys text and thinking items by (type, attemptId, ordinal) and requires a literal prefix. Live items carry attempt metadata the persisted record lacks, and live text coalesces newlines. So the complete record looked lossy and the truncated turn stayed on screen for good.

Fix

  • When a host read reports the host's own state, a terminal persisted turn that the host is not executing is the settled turn. The Runtime persists the terminal record before it leaves Processing, so stale local stream ownership no longer vetoes it. The stream hands that turn to the persisted record via adoptHostSettledTurn.
  • For that case, the content guard is now shape-neutral (settledReadCoversProjectedTurn). It still refuses a read that is missing a painted round, tool, tool result or text. It ignores attempt metadata, newline or whitespace coalescing, segment boundaries, and superseded attempts.
  • When a live event for the next turn races the read (for example a remote controller started it while the window was away), the persisted merge used to be skipped wholesale. Settled turns are now still adopted, because the race only concerns the turn the host is executing.
  • Applies to the peer/surface refresh, reconcileSettledDialogTurn, and disk hydrate. Hydrate uses the new path only when the restored session reports a host state.
  • docs/architecture/session-projection.md documents the ownership handoff.

Tests

  • FlowChatStore.test.ts: new cases for a missed-terminal turn:
    • the host-settled record is adopted and ownership moves to the persisted record. This case fails without the fix.
    • the live projection is kept while the host still executes that turn.
    • a settled record that is missing painted text is refused.
    • a live event for the next turn racing the read no longer blocks adoption of the settled turn. This case also fails without the fix.
  • The existing content-safety cases still pass.
  • pnpm --dir src/web-ui exec vitest run src/flow_chat/store src/flow_chat/session-stream src/flow_chat/services/flow-chat-manager src/infrastructure/peer-device: 868 passed. The 3 failures are in PendingQueueModule.test.ts and fail the same way on main, because Node has no localStorage there.
  • pnpm run type-check:web passes.

Remote scenarios

  • Peer Device Mode (local surface returning from a peer): covered by store-level fixtures. Not exercised against a live peer in this PR.
  • Remote workspace, Remote Control, and Detached Dispatch: no change to their protocols. The peer surface keeps using RelaySessionHistory.
  • Upgrade compatibility: frontend-only. There are no wire or persisted shape changes, and older hosts without a reported state keep the previous path.

…ce switch

A turn kept running locally while the window rendered a peer device.
Surface routing dropped its later chunks and terminal event, and the
journal tail could not serve the gap, so the return path fell back to
the persisted record. The stream still believed the turn was executing,
and the shape-sensitive progress comparison then refused the complete
record because live items carry attempt metadata and coalesced
newlines that the persisted record does not. The turn stayed truncated
for good.

A host read that reports a terminal record for a turn the host is not
executing now hands that turn to the persisted record, and its content
guard compares painted rounds, tools and text without depending on
attempt keys, newline coalescing or segment boundaries.
…read

A remote controller can start the next turn while the window renders
another device. Its live events then change the session during the
restore read, and the store skipped the whole persisted merge, so the
missed tail of the previous, already settled turn was never repaired.
That race only concerns the turn the host is executing; settled turns
are still adopted.
@bobleer
bobleer merged commit 13a3b6d into openBitFun:main Nov 2, 2026
9 checks passed
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.

1 participant