Repository navigation
Description
Problem
A conversation's messages reach IndexedDB only when a reply finishes: the persist runs on the message_end event alone (ui-packages/web/src/features/conversation/use-conversation/agent-session.ts:149). Everything before that moment lives in memory.
So when a tab dies mid-answer — an iOS tab kill under memory pressure, a dead battery, a crash, an accidental close — the exchange goes with it. What makes the loss visible is what does survive: send persists the conversation record with the question as its title before the run starts (ui-packages/web/src/features/conversation/use-conversation/run.ts:96-104), so after the kill, History lists a conversation whose title is exactly the question the reader asked, and opening it shows no messages at all. A titled, empty conversation is the artifact the loss leaves behind.
The exposure window is not a moment; it is the whole run. A single answer may stream for minutes (the model proxy allows 300 s per request, web-packages/server/src/model-proxy/routes.ts:90), and a run with tool calls — reading files, searching, running cells — is longer still. The reader watches the answer build the entire time, on the one device class most likely to kill the tab.
The app already guards other surfaces against exactly this death: the workspace flushes on pagehide (ui-packages/web/src/features/workspace/use-workspace.ts:125) and unsaved source warns on beforeunload (workspace-context.tsx:102). The conversation — the surface a reader spends the most live time in — has no guard: no persist at send, no flush at hide.
Proposal
- Persist the reader's message the moment it is sent, in
send/run, through the existingappendStoredConversationMessagespath — the question should never exist only in memory. - Flush the in-flight assistant message on
pagehide/ hiddenvisibilitychange, the same guard class Keep unsaved Source edits across an iOS tab swipe — beforeunload, their only guard, never fires there #167 asks for on the source side, so a killed tab keeps every word that already streamed.
Goal
After this, a tab killed mid-answer reopens with the question and the reply up to its last streamed word — the conversation resumes where it stopped, and History never shows a titled conversation that opens empty.
Verification
grep -q "persistAgentMessages\|appendStoredConversationMessages" ui-packages/web/src/features/conversation/use-conversation/run.ts
pnpm --filter @gamma-reader/web test --silent
pnpm checkRefs
- ui-packages/web/src/features/conversation/use-conversation/agent-session.ts:149 — the only persist trigger,
message_end - ui-packages/web/src/features/conversation/use-conversation/run.ts:42,96-104 — the message enters memory at
prompt; only the record, title and queue are persisted at send - ui-packages/web/src/features/workspace/use-workspace.ts:125 — the pagehide flush precedent, workspace only
- ui-packages/web/src/features/workspace/workspace-context.tsx:102 — the beforeunload warn, source only
- web-packages/server/src/model-proxy/routes.ts:90 — one reply may stream for five minutes
- Keep unsaved Source edits across an iOS tab swipe — beforeunload, their only guard, never fires there #167 — the same mobile-lifecycle failure for unsaved source edits; this is the conversation's face of it
- Say the Tutorial returns — its Delete dialog promises a removal the app silently undoes #163, Say why nothing opens — a first visit in a browser that blocks site data meets only 'Projects could not be opened in this browser', and its one link loops back to the same screen #180 — other trust surfaces already filed (Tutorial's promise, blocked storage)
- ui-packages/web/PRODUCT.md — local-first trust: what the reader wrote and read should survive the browser, not just the model's goodbye
Activity
bencode commentedon Oct 23, 2026
Independent review — posted by the scheduled issue-reviewer loop, independent of the authoring session.
Evidence audit (HEAD
35b3289): confirmed, all anchors exact.agent-session.ts:149is the only persist trigger during a live run (compaction.ts:26also persists, but only inside the compaction pass — outside the exposure window).run.ts:42(agent.promptenters memory),:96-104(title set at 98, record queued at 103-104),use-workspace.ts:125(pagehide flush, workspace only),workspace-context.tsx:102(beforeunload, source only),model-proxy/routes.ts:90(AbortSignal.timeout(300_000)). Conversation feature has zeropagehide/visibilitychangehandling (grep) — "no guard" holds.One nuance that strengthens the case: send persists the record via
queueDraft→writeDraft→saveStoredConversationDraft(drafts.ts:14-18), but for a brand-new conversation whose draft never flushed,queueDraftskips (drafts.ts:26-29) — then nothing survives and the conversation vanishes from History entirely, not "opens empty". The first-message loss is worse than stated; persist-at-send covers both shapes.Duplicate check: none; #167 is the same guard-class for source edits, correctly cross-referenced rather than duplicated.
Sensor: lint ✓ · verify ✗ (expected pre-implementation — no persist call in
run.tsyet).Sharpening: the first verification step is an implementation-detail grep (asserts a call site in
run.ts). Prefer an outcome anchor: start a run, emit a partial assistant message, firepagehidebeforemessage_end, assert the stored messages contain the user's message plus the streamed partial.Valid; stays open.
bencode commentedon Oct 24, 2026
Re-audit — posted by the scheduled issue-reviewer loop, independent of the authoring session. (Signal check: marker-only comments, no reactions, no assignee.)
Delta since last review (2026-10-23T08:20Z →
90b046f): PR #198 (5489f17) rewroteuse-conversation/run.tsandagent-session.ts— background conversations: a conversation switched away from keeps answering in memory. No conversation-side flush was added: the app's onlypagehidelistener is stilluse-workspace.ts:125and the onlyvisibilitychangeone is source-sync's (use-source-sync.ts:181). The change strengthens the premise — a background run is now even more purely in-memory state.Evidence audit (main @
90b046f):use-workspace.ts:125(pagehide flush, workspace only);workspace-context.tsx:102(beforeunload warn, source only);server model-proxy/routes.ts:90(AbortSignal.timeout(300_000)). Persist still fires onmessage_endalone (agent-session.ts:154; the only otherpersistAgentMessagescaller is compaction, which rewrites existing history, not in-flight messages).sendstill persists only the record with the question as title, then the queue (run.ts:119title: current.title ?? conversationTitle(item),run.ts:125queueDraft→drafts.ts:22writeDraft→saveStoredConversationDraft); the messages themselves enter memory atrun.ts:60(agent.prompt).agent-session.ts:149→ :154;run.ts:42→ :60;run.ts:96-104→ thesendbody now spansrun.ts:98-127(error-recovery code added since sits beforeagent.prompt).Sensor:
lintpassed;verifyfailed — main persists neither at send nor on hide.Verdict: valid — stays open.
Merge pull request #217 from bencode/feat/persist-exchange-in-flight
bencode commentedon Oct 26, 2026
✓ Verification passed