Repository navigation
Anthropic: merge every run of one role into one message - #20
Merged
Merged
Conversation
Previously only consecutive all-tool_result user entries were merged. A plain progress note after a tool-result turn, two plain user messages in a row, and adjacent assistant turns (receipts kept in place) reached the wire as consecutive same-role entries. Runs of one role now become one entry, content blocks in order; plain texts become separate text blocks (no joined string, byte-stable), empty texts are dropped from a merge, and lone messages keep their shape. The cache breakpoint is placed after merging, so it lands on the merged last entry. Tests cover stream and non-stream bodies. Co-Authored-By: Claude Fable 5.1 <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
mergingToolResultsonly merged consecutive user entries made entirely oftool_resultblocks. SwiftAgentKit now sends a plain-text progress note right after a tool-result turn, sometimes preceded by a stored nudge, and in-place receipts can put two assistant turns side by side. Those went out as consecutive same-role entries.mergingConsecutiveRoles: any run of one role becomes one entry, content blocks in order. Plain texts in a run become separate text blocks (not a joined string), so no separator is invented and bytes stay deterministic. Empty texts are dropped from a merge (an empty text block is a 400). Lone messages keep their shape.markingLastBlockForCache, so the last-message breakpoint lands on the last block of the merged final entry.Tests
New
AnthropicRoleMergeTests(stream and non-stream bodies): tool results + note, two plain user messages, consecutive assistants, empty-text drop, alternating conversations unchanged, sorted keys + byte stability.swift test: 168 tests passed.🤖 Generated with Claude Code