Summary
Cloud Agent sessions in the Kilo Cloud session and Kilo Android app can fail after one or two interactions with an upstream Anthropic validation error like:
messages.N.content.M: 'tool_use' ids must be unique
Reproduction
- Start a Cloud Agent session that uses Anthropic Messages API routing.
- Complete a few interactions that include tool calls.
- Continue the conversation until the prior assistant response with
tool_use blocks is re-included or duplicated in the message history.
- The next request to the gateway fails before reaching Anthropic successfully.
Expected behavior
The gateway should repair malformed message history before forwarding it upstream, matching the existing OpenAI-format repairTools() behavior.
Actual behavior
Duplicate Anthropic-format tool_use content blocks are forwarded to Anthropic, which rejects the request with tool_use ids must be unique.
Root cause
apps/web/src/lib/ai-gateway/tool-calling.ts already deduplicates duplicate tool_calls for kind: 'chat_completions' requests, but the Anthropic Messages API path (kind: 'messages') stores tool calls inline as content blocks. Those inline tool_use blocks were not deduplicated before provider transforms.
Proposed fix
Add repairMessagesTools() for the Anthropic Messages API format, run it for kind: 'messages' gateway requests, and add regression tests for duplicate tool_use blocks and orphan tool_result blocks.
Summary
Cloud Agent sessions in the Kilo Cloud session and Kilo Android app can fail after one or two interactions with an upstream Anthropic validation error like:
messages.N.content.M: 'tool_use' ids must be uniqueReproduction
tool_useblocks is re-included or duplicated in the message history.Expected behavior
The gateway should repair malformed message history before forwarding it upstream, matching the existing OpenAI-format
repairTools()behavior.Actual behavior
Duplicate Anthropic-format
tool_usecontent blocks are forwarded to Anthropic, which rejects the request withtool_use ids must be unique.Root cause
apps/web/src/lib/ai-gateway/tool-calling.tsalready deduplicates duplicatetool_callsforkind: 'chat_completions'requests, but the Anthropic Messages API path (kind: 'messages') stores tool calls inline ascontentblocks. Those inlinetool_useblocks were not deduplicated before provider transforms.Proposed fix
Add
repairMessagesTools()for the Anthropic Messages API format, run it forkind: 'messages'gateway requests, and add regression tests for duplicatetool_useblocks and orphantool_resultblocks.