refactor(anthropic): merge consecutive same-role messages in provider conversion layer#505
Merged
ahmedhesham6 merged 1 commit intomainfrom Feb 8, 2026
Conversation
… conversion layer Move the fix for Anthropic's alternating-role requirement from a pre-API sanitization step in the CLI layer into the Anthropic provider's conversion pipeline (build_messages_with_caching). When multiple tool results are converted individually, each Role::Tool message becomes a separate role="user" Anthropic message, violating Anthropic's strict alternating user/assistant requirement. The new merge_consecutive_messages() function combines these into a single user message with all tool_result content blocks. Changes: - Add merge_consecutive_messages() with content_to_blocks() and merge_content() helpers in anthropic/convert.rs - Add Default impl for AnthropicMessageContent to enable std::mem::take - Remove sanitize_tool_results() from mode_interactive.rs (dedup/orphan removal now handled by TaskBoardContextManager and provider layer) - Retain has_pending_tool_calls() and get_unresolved_tool_call_ids() guards as defense-in-depth - Refactor remaining tests to use shared helpers (test_tool_call, assistant_with_tool_calls, tool_message) - Add 10 tests covering merge scenarios: consecutive user/assistant messages, tool results, mixed string/blocks, cache control preservation, and full conversation flows
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.
Description
Move the fix for Anthropic's alternating-role requirement from a pre-API sanitization step in the CLI layer (
sanitize_tool_results) into the Anthropic provider's conversion pipeline (build_messages_with_caching).When multiple tool results are converted individually, each
Role::Toolmessage becomes a separaterole="user"Anthropic message, violating Anthropic's strict alternating user/assistant requirement. The newmerge_consecutive_messages()function combines these into a single user message with alltool_resultcontent blocks — fixing the problem at the exact layer where it originates.Related Issues
N/A — proactive refactor to simplify the defense-in-depth strategy.
Changes Made
libs/ai/src/providers/anthropic/convert.rs: Addmerge_consecutive_messages()as Phase 2 ofbuild_messages_with_caching(). Helper functionscontent_to_blocks()andmerge_content()handle content type normalization and merging. 10 new tests covering: consecutive user/assistant messages, tool result merging, mixed string/blocks content, cache control preservation, empty/single messages, and full conversation flows with multiple tool results.libs/ai/src/providers/anthropic/types.rs: AddDefaultimpl forAnthropicMessageContentto enable idiomaticstd::mem::take.cli/src/commands/agent/run/mode_interactive.rs: Removesanitize_tool_results()(dedup + orphan removal) and its pre-API call site. This was a ChatMessage-level workaround now replaced by the provider-level merge. Retainhas_pending_tool_calls()andget_unresolved_tool_call_ids()as defense-in-depth guards. Refactor remaining tests to use shared helpers (test_tool_call,assistant_with_tool_calls,tool_message).Why this is safe
merge_consecutive_messages()in the Anthropic provider — the only provider that requires it.TaskBoardContextManager::dedup_tool_results()(other context managers flatten to text, making duplication irrelevant).has_pending_tool_calls()before every API call.get_unresolved_tool_call_ids()in the user message handler.Testing
cargo test --workspace— 172 bin + all lib tests)cargo clippy --all-targets— zero warningscargo fmt --check— cleanBreaking Changes
None — internal refactor only, no public API changes.