fix(streaming): enforce strict index validation in event accumulator to prevent state corruption - #1782
Open
l1ve709 wants to merge 1 commit into
Open
fix(streaming): enforce strict index validation in event accumulator to prevent state corruption#1782l1ve709 wants to merge 1 commit into
l1ve709 wants to merge 1 commit into
Conversation
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
This PR resolves an unhandled edge case in the streaming parser where out-of-sequence or dropped
content_block_startevents could silently corrupt the internal accumulator state, leading to crypticIndexErrorexceptions during downstreamcontent_block_deltaprocessing.Root Cause Analysis
The accumulator previously appended
content_block_startpayloads blindly without asserting the sequence invariant (as noted by the legacy# TODO: check indexcomment). If an SSE event is dropped or misordered by the network layer,current_snapshot.contentdesynchronizes from the expected block sequence.When a subsequent
content_block_deltaarrives, the parser relies oncontent = current_snapshot.content[event.index]. If the internal state length is misaligned with the incomingevent.index, it triggers an obscureIndexErrorinside the delta router, making it extremely difficult for developers to debug the underlying network/API failure.Changes Made
content_block_startevaluation for both standard (_messages.py) and beta (_beta_messages.py) streams.RuntimeErrordescribing the index mismatch, guaranteeing stream integrity.Validation
ruffstatic analysis and formatting guidelines.event.index == len(current_snapshot.content)before allowing.append()mutations.