fix(chat): send finish before closing the stream on every exit path

Merged
SW
4mo ago
+53-1

The last piece of the UI-stall fix — row 2 on chat#1923.

The bug

A turn whose stream closes without a terminal finish chunk leaves the client in-flight forever. useChat waits for it; WorkflowChatTransport loops while (!gotFinish). And once the run is terminal, handleResumeChatStream returns 204 and clears active_stream_id — so the chunk is unreachable after the fact. It must go out before the close.

Today sendStreamFinish runs only on the happy path: there is no catch, and the finally calls closeChatStream without a finish. Any throw — such as the AI_NoOutputGeneratedError: No output generated observed on preview — closes the stream with no terminal chunk and hangs the UI.

The fix

await Promise.all([
  clearChatActiveStream(input.chatId, workflowRunId),
  (async () => {
    if (!streamFinished) await sendStreamFinish(writable);
    await closeChatStream(writable);
  })(),
  …
]);

Ports open-agents chat.ts L939 and L968-971, which run sendFinish(writable).then(() => closeStream(writable)) on both the success path and the finally, gated by a streamClosed flag. Our guard is streamFinished, so a successful turn still emits exactly one finish — a duplicate terminal chunk is its own client-side confusion.

Written as a sequential await rather than a .then() chain: clearer, and it does not assume the step returns a thenable.

On the docs

Vercel's own docs require the close — "Remember to close the stream when finished to signal completion" (workflow/docs/api-reference/workflow/get-writable.mdx L80, and every example ends with await writable.close()). They do not cover the error path; the finish-first ordering is upstream's pattern, not something the docs spell out.

Verification

  • 4,333 tests pass — 2 new, both RED before the change:
    • sends finish before closing when runAgentStep throws
    • sends exactly one finish on a successful run (the double-emit guard)
  • tsc --noEmit: 391 errors on both main and this branch — pre-existing baseline, none added.

Preview verification to follow in a comment. The gate is the case that hung on preview: a turn whose run ends should terminate the client's stream rather than leave a stop control up indefinitely.


Summary by cubic

Always send a terminal finish chunk before closing chat streams to prevent UI hangs when a run errors. Adds a guard so finish is emitted exactly once and updates cleanup to finish then close.

  • Bug Fixes
    • Emit finish before close on all exit paths; guard with streamFinished to avoid duplicates.
    • Cleanup keeps parallel steps; stream portion is sequential: finish then close.
    • Adds tests for error path and successful run.

Summary by CodeRabbit

  • Bug Fixes
    • Improved workflow stream handling during failures and interruptions.
    • Ensured terminal completion signals are sent reliably without duplicate signals on successful runs.