fix(streaming): validate content_block_start index before appending#1690
Open
WalkingDreams798 wants to merge 1 commit into
Open
fix(streaming): validate content_block_start index before appending#1690WalkingDreams798 wants to merge 1 commit into
WalkingDreams798 wants to merge 1 commit into
Conversation
`accumulate_event` appended each `content_block_start` block to the snapshot without checking `event.index`, silently relying on blocks arriving in order (this was the long-standing `# TODO: check index`). An out-of-order or skipped index would corrupt the snapshot — later `content_block_delta`/`content_block_stop` events index into the wrong block or raise an opaque IndexError. Validate that `event.index == len(current_snapshot.content)` and raise a clear RuntimeError on mismatch, matching the existing "Unexpected event order" style. Applied to both the standard and beta accumulators, with unit tests covering sequential appends and the mismatch error.
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.
What
accumulate_event(in bothlib/streaming/_messages.pyand_beta_messages.py) appended eachcontent_block_startblock to the snapshot without checkingevent.index— it silently relied on blocks arriving strictly in order. This was the long-standing# TODO: check indexin both files.If a
content_block_startever arrived with an out-of-order or skipped index, the block would land at the wrong position incurrent_snapshot.content, and subsequentcontent_block_delta/content_block_stopevents (which index byevent.index) would mutate the wrong block or raise an opaqueIndexError.Change
Validate that
event.index == len(current_snapshot.content)before appending, and raise a clearRuntimeErroron mismatch — consistent with the existing'Unexpected event order, got ... before "message_start"'style already used in the same function.Applied identically to the standard and beta accumulators, resolving both TODOs.
Tests
Added
tests/lib/streaming/test_content_block_index.pycovering, for both the standard and beta paths:RuntimeErrorVerification
uv run pytest tests/lib/streaming→ 44 passeduv run ruff check→ all checks passeduv run pyright(strict) on changed files → 0 errors, 0 warnings