fix: shallow copy content_block in BetaMessageStream#accumulateMessage#938
Open
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
Open
Conversation
…Stream
In BetaMessageStream#accumulateMessage, the content_block_start case
pushes event.content_block directly into the snapshot's content array
without copying it. This is inconsistent with MessageStream, which
correctly uses { ...event.content_block } to create a shallow copy.
Without the copy, the snapshot holds a direct reference to the event
object. Any external code that retains and mutates the original event's
content_block would inadvertently corrupt the accumulated message
snapshot. The non-beta MessageStream already guards against this by
creating a shallow copy.
This aligns BetaMessageStream with the existing MessageStream behavior.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
BetaMessageStream: Thecontent_block_startcase inBetaMessageStream#accumulateMessagepushesevent.content_blockdirectly into the snapshot's content array, while the non-betaMessageStreamcorrectly uses{ ...event.content_block }to create a shallow copy.Details
In
src/lib/MessageStream.ts(line 600):In
src/lib/BetaMessageStream.ts(line 606, before this fix):Without the copy, the snapshot holds a direct reference to the event object. Any external code that retains and mutates the original event's
content_blockwould inadvertently corrupt the accumulated message snapshot. The rest of thecontent_block_deltahandling in both files already creates new objects via spread ({ ...snapshotContent, ... }), so this is specifically a gap in thecontent_block_startcase of the beta stream.This is a one-character fix (
{ ...event.content_block }instead ofevent.content_block) that alignsBetaMessageStreamwith the existingMessageStreambehavior.Test plan
MessageStreamcontent_block_deltahandlers already create new objects via spread, so this only affects the initial content block reference🤖 Generated with Claude Code