fix(chat): end the resume stream with a finish chunk, not a bare 204

Merged
SW
3mo ago
+102-8

Row 1 of recoupable/app#2052. Replaces the client-side workaround in recoupable/app#2053, which will be closed.

The bug, reproduced on prod

$ curl -s -o /dev/null -w "status=%{http_code} body_bytes=%{size_download} content_type=[%{content_type}]\n" \
    "https://api.recoupable.dev/api/chat/{chatId}/stream?startIndex=0" -H "x-api-key: ***"
status=204  body_bytes=0  content_type=[]

Reopening any existing chat leaves the client re-requesting that URL at 2-3/s forever, with a spinner under the last assistant message and the composer stuck in its loading state. Measured on prod: 37 requests / 15s on a healthy 4-message chat used minutes earlier, 3,375 on one tab, against 0 for a page with no chatId.

Why a 204 does not terminate the loop

WorkflowChatTransport's resume loop has exactly two exits:

  1. a chunk of type: "finish" sets gotFinish (workflow-chat-transport.js:311)
  2. if (!res.ok || !res.body) throws

A 204 satisfies neither. It is in the 2xx range so res.ok is true, and cross-origin it reaches the browser as a non-null empty ReadableStream. Observed in the browser on the same response curl reports as zero bytes:

{ status: 204, ok: true, bodyIsNull: false, type: "cors" }

So the loop parses an empty stream, never sees finish, leaves gotFinish false, and re-enters immediately โ€” no error, no delay. That is also why there were zero console.error calls while it spun, which is what disproved the original theory that this was an error-message-matching problem.

Why upstream does not hit this

vercel-labs/open-agents returns the same new Response(null, { status: 204 }) and carries a client guard on startsWith("Failed to fetch chat: 204") โ€” the guard we copied. It works for them because their chat UI and this route are same-origin, so the 204 arrives body-null and !res.body fires.

Ours are split across app. and api.. Rather than carry a client shim upstream does not need, this says "finished" in the protocol the client already speaks. That fixes the loop for any client of this endpoint, not just our app.

The change

Both nothing-to-resume paths โ€” no active_stream_id, and a terminal run whose slot is stale bookkeeping โ€” now return a 200 UI-message stream carrying a single finish chunk:

data: {"type":"finish"}

data: [DONE]

Tests

5 new on createNothingToResumeResponse, asserting the exact SSE bytes, the text/event-stream + x-vercel-ai-ui-message-stream: v1 headers, CORS headers, and that exactly one chunk is emitted so a resume adds nothing to the transcript. RED confirmed first.

Two existing tests asserted the old contract (expect(res.status).toBe(204)) and are updated to assert the new one. Full lib/chat suite: 556 passed. tsc --noEmit and eslint clean.

Note

The endpoint's documented contract (docs#286) still says "204 when there is nothing to resume". Docs were deliberately skipped for this fix; the reference page needs a follow-up.

๐Ÿค– Generated with Claude Code

https://claude.ai/code/session_01KcNQAfA3nS7BTrMB3bXsnX


Summary by cubic

Fixes the infinite resume loop when reopening a chat with nothing to resume. A bare 204 never terminates WorkflowChatTransport's resume loop โ€” it's ok and cross-origin arrives with a non-null empty body โ€” so the client re-requested the stream endpoint at 2โ€“3 requests/sec forever.

Both nothing-to-resume paths now return a 200 UI-message stream with a single finish chunk instead of a 204. This replaces the client-side workaround in recoupable/app#2053 and fixes the loop for any client of the endpoint, not just our app. The API contract docs still say 204 and need a follow-up.

Summary by CodeRabbit

  • Bug Fixes
    • Chat resume requests with no active turn now return a completed response instead of an empty response.
    • Cross-origin resume clients can properly terminate without repeatedly retrying when there is nothing to resume.
    • Terminal chat runs now provide a clear completion signal.