Fix duplicate assistant messages during overlapping submits#1232
Merged
threepointone merged 3 commits intomainfrom Mar 31, 2026
Merged
Fix duplicate assistant messages during overlapping submits#1232threepointone merged 3 commits intomainfrom
threepointone merged 3 commits intomainfrom
Conversation
🦋 Changeset detectedLatest commit: 19aaf9c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
Refactor streaming-protection logic and add robust tests for edge cases. Renamed moveMessageById to moveMessageToEnd and simplified its behavior to always move a message to the end. Introduced anchorMessageId in protectedStreamingAssistant to remember the message that an in-progress assistant should be anchored to, and updated restore logic to reinsert the assistant either after the anchor or at the front if no anchor exists. Consolidated local response ID handling inside the effect (localResponseIds) and ensure IDs are cleared on CF_AGENT_CHAT_CLEAR; also clear protectedStreamingAssistant on cleanup. Tests: wrap sendMessage calls in act, and add multiple new tests covering multiple server broadcasts during a stream, CF_AGENT_CHAT_CLEAR mid-stream, server-inserted messages that require anchor-based restoration, handling done without a start chunk, and verifying no protection is activated when not streaming.
3598823 to
89a4d88
Compare
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
CF_AGENT_CHAT_MESSAGESbroadcasts arrive mid-stream, then restore the original message ordering once the stream completesuseEffectcleanup to prevent leaks on unmount or effect re-runsCF_AGENT_CHAT_CLEARmid-protection, anchor-based restore with server-injected messages,done:truewithout a priorstartchunk, and the not-streaming guardCloses #1231
Testing
Reviewer Notes
useAgentChat; it does not change server persistence ormessageConcurrencysemantics.user, assistant, userbetween turns.