fix: defer message_end until all content blocks are closed#37
Merged
Conversation
The assistant SDK event can arrive before all content_block_stop events have fired, especially with streaming-input mode. When message_end was emitted immediately on assistant, finishCurrent() froze in-flight blocks with done=false, empty toolInput, and no toolResult — tool pills showed "Running..." permanently and text blocks got orphaned. Track openBlockCount (incremented on content_block_start, decremented on content_block_stop). When assistant arrives, stash the message_end payload and only flush it once openBlockCount reaches zero. This guarantees every block receives its block_end before the message is finalized. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Made-with: Cursor
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
The
assistantSDK event can fire before allcontent_block_stopevents, especially with streaming-input mode. This caused two bugs:message_endtriggeredfinishCurrent()which froze in-flight blocks withdone=false, emptytoolInput, notoolResult. Tool pills showed "Running..." permanently and text blocks got orphaned.toolInputanddoneonly arrive onblock_end. Ifblock_endraces aftermessage_end, the pill never updates.Both bugs are one fix: track
openBlockCount(incremented oncontent_block_start, decremented oncontent_block_stop). Whenassistantarrives, stash themessage_endpayload and only flush onceopenBlockCounthits zero.Diagnosed collaboratively — the Mitzo phone agent identified the root cause during a live debugging session.
Test plan
block_endprecedesmessage_endwhenassistantfires early)tsc --noEmitclean, pre-commit hooks passmessage_endMade with Cursor