PR #9976 review: keep a chat on screen and reload its history

PR #9976 · Review workspace · author harshithmullapudi · reviewed at 970c631e

Summary

When the host knows a chat but cannot read its history (getOutline and getItems both return ok:false, for example journal_missing), useChatSession used to clear the transcript, report ready and never read the history again, so the pane stayed blank until a reload. This PR keeps the cached transcript (unless it is from another epoch), shows the host's session row, and retries resync with backoff (250 ms doubling to 60 s), exposing historyUnavailable. The desktop gets a new TranscriptLoadState overlay for "Loading chat", "host unreachable" and "couldn't load messages".

Verdict

minor issues The fix is structurally right and tested (the new test fails on main); the retry covers only the first load, so a failed resync after a stream reset can still freeze the chat, and the retry never stops for a chat whose journal is gone for good.

Findings

Medium

packages/chat/src/react/useChatSession/useChatSession.ts:471 and :575: a failed resync after a reset is not retried

The new retry only starts from the first seed (:566). The two other resync callers, onReset → void resync(true) and the snapshot.pendingReset effect, still drop a false result. Scenario: a chat is open, the host restarts (new epoch) and its journal read fails once. The reducer sets pendingReset = "epoch_changed" and then ignores every envelope of the new epoch (core/reducer/reducer.ts:146). resync fails, pendingReset does not change, so the effect does not run again. The transcript freezes with no banner. This is the same class of bug as the one this PR fixes. (Found by reading the code; not reproduced.)

Fix: make retryHistory the single "resync failed" path: when resync() returns false from onReset or the pendingReset effect, start retryHistory(0) (keep it in a ref so both can reach it), and add a test that triggers a reset while reads fail.

Minor

packages/chat/src/react/useChatSession/useChatSession.ts:513-527: the history retry never ends

For a chat whose journal is gone for good (journal_missing is, by name, not transient), every open pane calls getOutline and then getItems once a minute until the pane closes. The PR notes that on a long chat each of these replays the whole journal synchronously on the host (about 0.4 s at 60k events). Several panes or several long chats add host load with no end, and the banner says "Retrying…" forever.

Fix: stop after N attempts (or about 10 min) and switch the banner to a stable "Couldn't load this chat's messages" with a Retry button; or retry only on a host signal (stream reconnect, a new durable envelope, window focus) once the backoff reaches its cap.

Minor

packages/chat/src/react/useChatSession/useChatSession.ts:457: applied only reflects the last loop pass

If the first pass applies a seed whose result has pendingReset, and the second pass fails, resync returns false although the snapshot changed. In the history-retry path this only costs one more retry. But the snapshot then keeps pendingReset, which is the frozen state from the first finding.

Fix: after the loop, return applied && !snapshotRef.current.pendingReset (or track it in the loop), so that callers can retry.

Nit

apps/desktop/.../TranscriptLoadState/TranscriptLoadState.tsx:30: the banner covers the first transcript row

The banner is absolute top-2 z-10 over the scroller. When the cached transcript is scrolled to the top, it covers the first message while the chat is degraded. It is pointer-events-none, so clicks still go through. Not verified visually (see Evidence).

Fix: optional: render it in the flow above the scroller, or accept the overlap.

Evidence

packages/chat (hook): verified with tests.

Desktop (TranscriptLoadState): NOT verified. I tried twice to run the dev desktop with a CDP port in this 8 GB cloud box:

  1. bun run dev:desktop: the API failed with EADDRINUSE :3001 because the workspace stack was already running, and turbo stopped the run.
  2. bun run dev in apps/desktop with RENDERER_REMOTE_DEBUG_PORT=9333: the window and CDP came up at localhost:3005/#/, but the process was killed (exit 137, SIGKILL; most likely out of memory beside the API and web servers) about 20 s later, before the app rendered past a blank frame.
Blank desktop window

The only frame captured: the renderer at #/ before the process was killed. It does not show the PR's UI.

Not run: mobile (apps/mobile ChatSessionView also uses useChatSession, so it gets the retry but has no banner).

Questions for the author

Test / CI notes