feat(chat): resume a refreshed page from the recent tail, not chunk zero

Closed
SW
4mo ago
+75-8

Row 6 of chat#1923. Blocked by api#810 — see below.

The problem

A reload mid-turn has no counted position, so resume: true reconnects with no startIndex and the server replays the entire response — 418 chunks in the verified prod run. All of that content is already persisted and rendered from the database on load, so the replay is pure waste.

The fix

Ask for a bounded tail instead: REFRESH_RESUME_WINDOW = -50. The server resolves negative indices relative to the end of the stream and reports where the read landed via x-workflow-stream-tail-index, which is what lets later retries in the same session return to exact absolute positions.

A counted position always wins over the window. The fresh-load flag is cleared the moment any chunk arrives, so only the first reconnect of a freshly loaded page uses it — every subsequent one resumes from what the client actually received.

Hard dependency on api#810

The tail header is invisible to browser JS until Access-Control-Expose-Headers ships. Per the SDK docs, without that header a negative startIndex is "assumed to be 0, replaying the entire stream" — so merging this first would be a no-op at best. api#810 must land first.

Why this doesn't remove the frame counting

Worth restating, because it is the thing most likely to be misread: headers are sent before the body, so x-workflow-stream-tail-index reports the tail at the moment the read was opened, never where it ended. A read advertising tail: 9 went on to deliver 22 chunks. Counting frames off the wire remains the only way to know how far a long-lived read actually got; this header is an anchor for relative indices and nothing more.

Tests

4 new cases, RED before GREEN: a fresh load requests the bounded window; the window is negative; a counted position is preferred over it; a non-fresh reconnect with no position still reads from the beginning.

88 chat test files passing; tsc --noEmit clean in touched files; eslint clean.

Merge order

api#810 → this. Note it also touches hooks/useChatTransport.ts, as does chat#1925; whichever lands second will want a rebase.

Refs chat#1923

🤖 Generated with Claude Code


Summary by cubic

Resume a refreshed chat page from the recent tail instead of chunk 0 to avoid replaying entire responses and speed up reloads. Applies only to the first reconnect when there’s no counted position; counted positions still win.

  • New Features

    • On fresh reloads with no position, request startIndex=-50 to fetch only the recent tail.
    • If a counted position exists, resume from lastChunkIndex + 1.
    • Use x-workflow-stream-tail-index to anchor subsequent retries; frame counting stays for progress.
    • Clear the fresh-load flag once any chunk arrives so later reconnects use the counted position.
  • Dependencies

    • Blocked by api#810 to expose x-workflow-stream-tail-index via Access-Control-Expose-Headers.
    • Merge order: api#810 → this.