fix: prevent repeated text on reconnect by using block snapshots#1038
Open
VibhorGautam wants to merge 1 commit intoItzCrazyKns:masterfrom
Open
fix: prevent repeated text on reconnect by using block snapshots#1038VibhorGautam wants to merge 1 commit intoItzCrazyKns:masterfrom
VibhorGautam wants to merge 1 commit intoItzCrazyKns:masterfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #938
When a client reconnects to an in-progress session (e.g. after a network blip or tab switch), the
subscribe()method inSessionManagerreplayed every historical event — every block creation and every incrementalupdateBlockpatch. 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 viastructuredClone) 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:
endsignalHow to test
npm install && npm run dev, open http://localhost:3000/api/chatand block it (or toggle your network off for a second, then back on) to force a reconnect through/api/reconnect