Skip to content

Persist the exchange while it happens — a crash or phone tab-kill mid-answer loses the question and the partial reply, leaving a titled conversation that opens empty #197

Closed

Description

@bencode

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

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 check

Refs

Activity

  1. added
    enhancement
    source:scheduled
    on Oct 23, 2026
  2. bencode commented on Oct 23, 2026

    @bencode
    OwnerAuthor

    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:149 is the only persist trigger during a live run (compaction.ts:26 also persists, but only inside the compaction pass — outside the exposure window). run.ts:42 (agent.prompt enters 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 zero pagehide/visibilitychange handling (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, queueDraft skips (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.ts yet).

    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, fire pagehide before message_end, assert the stored messages contain the user's message plus the streamed partial.

    Valid; stays open.

  3. bencode commented on Oct 24, 2026

    @bencode
    OwnerAuthor

    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) rewrote use-conversation/run.ts and agent-session.ts — background conversations: a conversation switched away from keeps answering in memory. No conversation-side flush was added: the app's only pagehide listener is still use-workspace.ts:125 and the only visibilitychange one 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):

    • Still holds exactly: 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 on message_end alone (agent-session.ts:154; the only other persistAgentMessages caller is compaction, which rewrites existing history, not in-flight messages). send still persists only the record with the question as title, then the queue (run.ts:119 title: current.title ?? conversationTitle(item), run.ts:125 queueDraft → drafts.ts:22 writeDraft → saveStoredConversationDraft); the messages themselves enter memory at run.ts:60 (agent.prompt).
    • Drifted, corrected anchors: agent-session.ts:149 → :154; run.ts:42 → :60; run.ts:96-104 → the send body now spans run.ts:98-127 (error-recovery code added since sits before agent.prompt).

    Sensor: lint passed; verify failed — main persists neither at send nor on hide.

    Verdict: valid — stays open.

  4. added
    state:in-progress
    on Oct 26, 2026
  5. self-assigned this
    on Oct 26, 2026
  6. added a commit that references this issue on Oct 26, 2026
  7. bencode commented on Oct 26, 2026

    @bencode
    OwnerAuthor

    ✓ Verification passed

    > @gamma-reader/web@0.0.0 test /Users/bencode/work/gamma-reader/ui-packages/web
    > vitest run --project unit --silent
    
    
     RUN  v5.0.0 /Users/bencode/work/gamma-reader/ui-packages/web
    
    
     Test Files  76 passed (76)
          Tests  455 passed (455)
       Start at  09:08:11
       Duration  10.25s (environment 41%, setup 26%, tests 19%, transform 8%, import 6%)
    
    Environment  |unit| jsdom was created 76 times · 34.23s total, 41% of tracked time
                 create it once per worker with pool: 'vmThreads' (keeps per-file isolation) or isolate: false (shares it across files)
                 learn more: https://vitest.dev/guide/improving-performance#test-environments
    
    
    > gamma-reader-workspace@0.0.0 check /Users/bencode/work/gamma-reader
    > biome check . && pnpm typecheck && pnpm test && pnpm test:integration
    
    Checked 456 files in 159ms. No fixes applied.
    Found 4 warnings.
    
    > gamma-reader-workspace@0.0.0 typecheck /Users/bencode/work/gamma-reader
    > pnpm -r typecheck
    
    Scope: 7 of 8 workspace projects
    packages/links typecheck$ tsc -p tsconfig.json
    packages/shared typecheck$ tsc -p tsconfig.json
    ui-packages/code-lab typecheck$ tsc -p tsconfig.json
    packages/links typecheck: Done
    ui-packages/code-lab typecheck: Done
    packages/shared typecheck: Done
    ui-packages/code-lab-playground typecheck$ tsc -p tsconfig.json
    ui-packages/web typecheck$ tsc -p tsconfig.json
    web-packages/server typecheck$ tsc -p tsconfig.json
    ui-packages/code-lab-playground typecheck: Done
    web-packages/server typecheck: Done
    ui-packages/web typecheck: Done
    
    > gamma-reader-workspace@0.0.0 test /Users/bencode/work/gamma-reader
    > pnpm -r test
    
    Scope: 7 of 8 workspace projects
    packages/links test$ vitest run
    ui-packages/code-lab test$ vitest run
    ui-packages/code-lab test:  RUN  v5.0.0 /Users/bencode/work/gamma-reader/ui-packages/code-lab
    packages/links test:  RUN  v5.0.0 /Users/bencode/work/gamma-reader/packages/links
    packages/links test:  Test Files  5 passed (5)
    packages/links test:       Tests  44 passed (44)
    packages/links test:    Start at  09:08:23
    packages/links test:    Duration  229ms (import 61%, transform 26%, tests 12%, worker 1%)
    packages/links test: Done
    ui-packages/code-lab test:  Test Files  4 passed (4)
    ui-packages/code-lab test:       Tests  14 passed (14)
    ui-packages/code-lab test:    Start at  09:08:23
    ui-packages/code-lab test:    Duration  784ms (environment 33%, tests 28%, transform 22%, import 15%, worker 1%)
    ui-packages/code-lab test: Done
    ui-packages/web test$ vitest run --project unit
    web-packages/server test$ vitest run
    web-packages/server test:  RUN  v5.0.0 /Users/bencode/work/gamma-reader/web-packages/server
    ui-packages/web test:  RUN  v5.0.0 /Users/bencode/work/gamma-reader/ui-packages/web
    web-packages/server test: warning: You appear to have cloned an empty repository.
    ui-packages/web test: Not implemented: navigation to another Document
    ui-packages/web test: Not implemented: navigation to another Document
    web-packages/server test:  Test Files  17 passed (17)
    web-packages/server test:       Tests  132 passed (132)
    web-packages/server test:    Start at  09:08:24
    web-packages/server test:    Duration  4.58s (tests 64%, import 26%, transform 10%, worker 1%)
    web-packages/server test: Done
    ui-packages/web test:  Test Files  76 passed (76)
    ui-packages/web test:       Tests  455 passed (455)
    ui-packages/web test:    Start at  09:08:25
    ui-packages/web test:    Duration  11.04s (environment 40%, setup 26%, tests 19%, transform 7%, import 7%)
    ui-packages/web test: Environment  |unit| jsdom was created 76 times · 36.06s total, 40% of tracked time
    ui-packages/web test:              create it once per worker with pool: 'vmThreads' (keeps per-file isolation) or isolate: false (shares it across files)
    ui-packages/web test:              learn more: https://vitest.dev/guide/improving-performance#test-environments
    ui-packages/web test: Done
    
    > gamma-reader-workspace@0.0.0 test:integration /Users/bencode/work/gamma-reader
    > pnpm -r --if-present test:integration
    
    Scope: 7 of 8 workspace projects
    ui-packages/web test:integration$ vitest run --project integration
    ui-packages/web test:integration:  RUN  v5.0.0 /Users/bencode/work/gamma-reader/ui-packages/web
    ui-packages/web test:integration: Not implemented: navigation to another Document
    ui-packages/web test:integration:  Test Files  7 passed (7)
    ui-packages/web test:integration:       Tests  83 passed (83)
    ui-packages/web test:integration:    Start at  09:08:36
    ui-packages/web test:integration:    Duration  9.81s (tests 73%, transform 10%, environment 7%, import 6%, setup 4%)
    ui-packages/web test:integration: Done
    
  8. added
    state:needs-verification
    and removed
    state:in-progress
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementsource:scheduledstate:needs-verification

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions