fix(chat): end the reconnect loop on a 204 instead of retrying forever

Closed
SW
3mo ago
+91-4

Row 1 of #2052.

The bug

Reopening any existing chat leaves the client re-requesting GET /api/chat/{chatId}/stream?startIndex=0 at ~2-3/s, every response a 204, forever — with a spinner under the last assistant message and the send button stuck in its loading state, on a conversation that finished minutes ago.

Measured on prod 2026-09-02 (DevTools filtered to startIndex, requests per 15s):

ChatRequests / 15s
healthy, 4 messages, used successfully minutes earlier37 (3,375 total on that tab)
older session49
older session, independent43
fresh page, no chatId (control)0

Root cause

The existing 204 guard can never match. It tested error.message.startsWith("Failed to fetch chat: 204"), but @workflow/ai@4.2.0 never lets that raw message escape reconnectToStream:

  1. Inside while (!gotFinish) a 204 has no body, so !res.body throws Failed to fetch chat: 204 (workflow-chat-transport.js L275).
  2. The catch at L319 increments consecutiveErrors and continues the loop with no delay — that is the tight cadence.
  3. On the third it throws a different error at L326: Failed to reconnect after 3 consecutive errors. Last error: Failed to fetch chat: 204 .

That wrapper starts with Failed to reconnect after, so startsWith missed, return null never ran, and "nothing to resume" became a permanent hard failure.

The spinner is the same fact, not a second symptom: useChat is configured resume: shouldResumeStream({ authenticated, routeChatId }), so every chat opened at /sessions/[sessionId]/chats/[chatId] resumes; the loop never terminates, status never leaves "streaming", and messages.tsx L46 renders the spinner on exactly that.

Prior art: shouldResumeStream's own docstring records this failure — "Resuming that one made the while (!gotFinish) loop reissue the GET 118 times against a 204" (recoupable/chat#1949 F4a). That change gated when resume runs, which fixed the homepage case and left the loop unbounded everywhere resume legitimately runs. This fixes the loop rather than narrowing the gate again.

The change

is204NothingToResume matches the marker anywhere in the chain, still anchored to the status so a 401 or 500 still surfaces. Extracted to its own file so both shapes are pinned by tests rather than by a string literal at the call site.

Tests

5 new cases (raw form, wrapped form, other statuses, incidental "204" in unrelated text, non-Error values). RED confirmed before implementation. lib/chat suite: 38 passed. tsc --noEmit clean.

Preview verification

Pending — draft until verified on the preview for this SHA. Done when: reopening a completed chat settles with zero repeating stream requests over 60s, no spinner under the last message, and a composer that can send immediately.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KcNQAfA3nS7BTrMB3bXsnX


Summary by cubic

Reopening a completed chat no longer triggers an endless loop of 204 stream reconnects with a spinner and a stuck composer. A 204 is ok: true with a non-null empty ReadableStream, so the resume loop never hit its !res.ok || !res.body guard and re-requested forever; wrapping fetch to null the 204 body lets the existing "nothing to resume" handling fire, while real failures like 401/500 still surface. Implements row 1 of #2052.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of empty chat responses so HTTP 204 responses are recognized correctly.
    • Chat requests now report a clear fetch error for empty responses instead of attempting to process them as streamed content.
    • Existing response status, status text, and headers remain preserved when handling no-content responses.