PR #9976 review: keep a chat on screen and reload its history
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
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.
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.
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.
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.
bun test src/react/useChatSessionat970c631e: 28 pass, 0 fail.- Same tests with
useChatSession.tsset back to base63b5e28f: 26 pass, 2 fail. The two new tests fail ("shows the cached transcript and loads the history again…" and "stops asking for an unreadable history once the chat closes"), so they cover the bug. tsc --noEmitinpackages/chat: clean.lingui compile --strictinpackages/i18n: clean (the 2 new strings are present in every catalog).
Desktop (TranscriptLoadState): NOT verified. I tried twice to run the dev desktop with a CDP port in this 8 GB cloud box:
bun run dev:desktop: the API failed withEADDRINUSE :3001because the workspace stack was already running, and turbo stopped the run.bun run devinapps/desktopwithRENDERER_REMOTE_DEBUG_PORT=9333: the window and CDP came up atlocalhost: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.
Not run: mobile (apps/mobile ChatSessionView also uses useChatSession, so it gets the retry but has no banner).
Questions for the author
- Should mobile show the
historyUnavailable/unreachablestate too? Today it gets the retry with no visible state, and a chat with no cache is still blank there while it retries. - Is
journal_missingever permanent (journal deleted or rotated)? If yes, an open-ended retry is the wrong UX for it; see the second finding. - When the cached transcript is kept, live events start from the host's cursor, so the events between the cache cursor and the host cursor are missing until the reload works. Is a "may be out of date" hint in the banner worth it?
Test / CI notes
- Commit
970c631eac7e. CI: Lint, Typecheck (it covers the desktop typecheck the author could not run locally), Test, Sherif, Vale, Version Sync, Build CLI and the preview deploys all pass. Mobile build/preview and CodeRabbit were still pending when I checked; E2E Mobile was skipped. - Locally: the hook tests and the base comparison above,
tscinpackages/chat, and a strict Lingui compile.