Skip to content

fix: prevent repeated text on reconnect by using block snapshots#1038

Open
VibhorGautam wants to merge 1 commit intoItzCrazyKns:masterfrom
VibhorGautam:fix/repeated-text-on-reconnect-938
Open

fix: prevent repeated text on reconnect by using block snapshots#1038
VibhorGautam wants to merge 1 commit intoItzCrazyKns:masterfrom
VibhorGautam:fix/repeated-text-on-reconnect-938

Conversation

@VibhorGautam
Copy link

@VibhorGautam VibhorGautam commented Mar 8, 2026

Summary

Fixes #938

When a client reconnects to an in-progress session (e.g. after a network blip or tab switch), the subscribe() method in SessionManager replayed every historical event — every block creation and every incremental updateBlock patch. This caused the client to visually rebuild content from scratch, producing duplicated text in the response.

Root cause

subscribe() stored all emitted events in an array and replayed the entire array to new subscribers. On initial connection this was harmless (array was empty), but on reconnect the array contained hundreds of events. The client already had the content from before the disconnect, so replaying everything doubled it.

Fix

Changed subscribe() to send block snapshots (current state via structuredClone) instead of replaying every historical event. Non-block milestone events (researchComplete, end, error) are still replayed so reconnecting clients know if the session already finished.

This means:

  • Initial subscribers (new session) get an empty block map — no replay, same as before
  • Reconnecting subscribers get the latest state of each block — no duplication
  • If the session ended while disconnected, the client still receives the end signal

How to test

  1. npm install && npm run dev, open http://localhost:3000
  2. Send a search query and while the response is still streaming, open browser DevTools → Network tab
  3. Find the active SSE/fetch connection to /api/chat and block it (or toggle your network off for a second, then back on) to force a reconnect through /api/reconnect
  4. The response should continue from where it left off without the text appearing twice
  5. Also let a response fully complete, then force a reconnect — the full response should show once, not doubled

When a client reconnects to an in-progress session, the subscribe()
method was replaying every historical event — every block creation and
every incremental updateBlock patch. This caused reconnecting clients
to visually rebuild content from scratch, producing duplicated text.

Changed subscribe() to send the current state of each block as a
snapshot instead. Non-block milestone events (researchComplete, end,
error) are still replayed so the client knows if the session finished
while it was disconnected.

Fixes ItzCrazyKns#938
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repeated Text in Responses

1 participant