morphic icon indicating copy to clipboard operation
morphic copied to clipboard

[BUG] Related-question click during streaming corrupts useChat state, leaves input stuck disabled

Open Tespera opened this issue 6 months ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Vercel Runtime Logs

  • [ ] I have checked the Vercel Runtime Logs for errors (if applicable)

Current Behavior

When the assistant is mid-stream and the user clicks a follow-up "Related" question rendered by a spec block, the second sendMessage is dispatched while useChat is still processing the first one. The two streams interleave and corrupt the hook's internal state. Visible symptoms after both responses complete:

  1. The chat input stays disabled for ~5–10 seconds after the final token arrives.
  2. The devtools console emits one or more React warnings: Encountered two children with the same key, '<messageId>' (one warning per re-render of chat-messages.tsx, often 10+).
  3. The next render path throws TypeError: Cannot read properties of undefined (reading 'state') from a tool part (search-section.tsx / dynamic-tool-display.tsx) because the second assistant message reaches the React tree with a half-populated tool part.

Expected Behavior

Clicking a Related button while a response is in flight should be a no-op (or visually disabled), not start a second concurrent dispatch. The input should re-enable as soon as the response is finished.

Steps To Reproduce

  1. Configure any working AI provider so a chat actually responds.
  2. Ask any question whose response includes Related follow-up buttons.
  3. While the assistant is still streaming, click one of the Related buttons.
  4. Immediately click a second Related button.
  5. Wait for both responses to finish.
  6. Observe the symptoms above (stuck input, dup-key warnings, console TypeError).

Environment

Anything else?

Root cause — components/spec-block.tsx calls chatContext.sendMessage(...) directly with no guard; it does not check whether useChat is currently busy. Firing a second dispatch mid-stream is what breaks the hook.

Why a simple useChat.status guard is not enough — I tried that locally and discovered two follow-on traps:

  1. useChat.status does not reliably transition back to 'ready' once a stream contains tool calls or spec blocks. It can stay 'streaming' indefinitely from the consumer's perspective, so a status-based guard rejects clicks that should be allowed.
  2. The handlers prop in @json-render/react's ActionProvider is stored as useState(initialHandlers) (see node_modules/@json-render/react/dist/index.js:460) and never refreshed. Any closure captured at mount time freezes its chatContext snapshot, so a React-state value read through that closure stays stale forever.

A working shape we tested locally:

  • chat.tsx maintains a local isStreamingRef: MutableRefObject<boolean> — set to true inside a wrapped safeSendMessage, cleared inside useChat's onFinish / onError.
  • ChatContext exposes the ref (not a boolean) so spec-block's frozen closure can still read .current and always see the latest value.
  • spec-block.tsx checks chatContext.isStreamingRef.current and shows a toast / no-ops while a response is in flight.

Happy to send a PR with that shape if it sounds right, but it does touch the ChatContext API so I wanted to check whether a different design (e.g. a disabled prop on the Related button itself, or a server-side check) would fit the project better.

Tespera avatar May 22 '26 10:05 Tespera

PR #853 implements the fix described above (MutableRefObject<boolean> exposed through ChatContext). Happy to rework the API shape if you prefer a different design — see the open design question at the bottom of the PR.

Tespera avatar May 22 '26 10:05 Tespera