← Back to PRs

#2806: [AI-Assisted] Fix: Repair tool_use/tool_result pairing for Claude on any provider

by Arthur742Ramos open 2026-01-27 14:58 View on GitHub →
agents
## Motivation Discovered this bug while running Clawdbot on an Azure VM using **GitHub Copilot with Claude Opus 4.5**. Every message to a WhatsApp group was failing with: ``` 400 messages.220: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_vrtx_01Hnp1oP5EXSRsPvumoXLJh9, toolu_vrtx_01X49HwBcdSEAMddUbwEUXao. Each `tool_use` block must have a corresponding `tool_result` block in the next message. ``` The session had orphaned `tool_use` blocks, and the repair logic wasn't running because GitHub Copilot wasn't recognized as needing Anthropic-style sanitization. ## Root Cause In `transcript-policy.ts`, `repairToolUseResultPairing` was only enabled for Google or Anthropic providers: ```typescript const repairToolUseResultPairing = isGoogle || isAnthropic; ``` But `github-copilot` with Claude models uses Anthropic's API format and needs this repair. The provider detection only checked for `provider === "anthropic"` or `modelApi === "anthropic-messages"`, missing Claude models accessed via other providers. ## Solution Added `isClaudeModel()` helper that detects Claude by checking if `modelId` contains "claude", and wired it into: - `repairToolUseResultPairing` (fixes the 400 error) - `validateAnthropicTurns` (Claude needs Anthropic-style turn validation) - `allowSyntheticToolResults` (allows inserting synthetic results for missing tool results) This covers Claude on any provider: github-copilot, openrouter, opencode, amazon-bedrock, etc. ## Testing Added comprehensive test suite (238 lines) covering: - Direct Anthropic provider - Claude via github-copilot, openrouter, opencode, amazon-bedrock - Non-Claude models (GPT, Llama, Gemini) - correctly NOT enabled - Edge cases (null/empty/undefined modelId) - Case-insensitive detection - google-antigravity Claude models Logic verified with standalone tests (9/9 passing). Full vitest suite requires pnpm workspace setup. ## AI Disclosure 🤖 **AI-Assisted PR** - Analysis: Claude Opus 4.5 (via Clawdbot) - Implementation: OpenCode with GPT-5.2-Codex (xhigh) and Claude Opus 4.5 (high) - Testing level: Lightly tested (logic verified, full suite not run due to workspace deps) - I understand what this code does and have reviewed it ## Files Changed - `src/agents/transcript-policy.ts` - Added `isClaudeModel()` helper and updated policy flags - `src/agents/transcript-policy.test.ts` - New comprehensive test suite <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR extends transcript sanitization policy selection to recognize Claude models even when they’re routed through non-Anthropic providers (e.g., GitHub Copilot/OpenRouter/Bedrock) by introducing an `isClaudeModel()` helper and using it to enable Anthropic-style repair/validation flags. It also adds a focused `resolveTranscriptPolicy` test suite covering Claude-via-third-party providers, non-Claude models, and a few edge cases. <h3>Confidence Score: 4/5</h3> - This PR is likely safe to merge, with a small risk of misclassification due to broad Claude model detection. - Changes are localized to transcript policy gating and backed by unit tests; the main concern is that substring-based Claude detection may enable Anthropic-specific sanitizers for unintended model IDs/providers. - src/agents/transcript-policy.ts (Claude detection heuristic) <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment -->

Most Similar PRs