Skip to content

Anthropic: merge every run of one role into one message - #20

Merged
ayman3000 merged 1 commit into
mainfrom
fix/anthropic-merge-consecutive-roles
Oct 9, 2026
Merged

ayman3000 merged 1 commit into
mainfrom
fix/anthropic-merge-consecutive-roles

Conversation

@ayman3000

Copy link
Copy Markdown
Owner

Summary

  • mergingToolResults only merged consecutive user entries made entirely of tool_result blocks. 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.
  • Replaced with 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.
  • Merge runs before 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

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>
@ayman3000
ayman3000 merged commit bad5c3c into main Oct 9, 2026
1 check passed
@ayman3000
ayman3000 deleted the fix/anthropic-merge-consecutive-roles branch October 9, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant