#2806: [AI-Assisted] Fix: Repair tool_use/tool_result pairing for Claude on any provider
agents
Cluster:
Tool Call ID Sanitization
## 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
#13831: fix(agents): include Anthropic in tool call ID sanitization
by lailoo · 2026-02-11
84.8%
#10232: Fix: Add anthropic mode for tool call ID sanitization
by StreetJammer · 2026-02-06
84.7%
#12812: fix(transcript-policy): sanitize tool call IDs for all non-OpenAI p...
by justin-nevins · 2026-02-09
82.4%
#8345: fix: prevent synthetic error repair from creating tool_result for d...
by vishaltandale00 · 2026-02-03
81.9%
#8270: fix: support snake_case 'tool_use' in transcript repair (#8264)
by heliosarchitect · 2026-02-03
81.4%
#20747: feat(copilot): support Claude models with correct context windows (...
by yuf1011 · 2026-02-19
81.3%
#11447: feat(providers): add Claude models to GitHub Copilot provider
by yylonly · 2026-02-07
81.2%
#13976: fix(anthropic): include Anthropic in tool call ID sanitization
by omair445 · 2026-02-11
80.3%
#4700: fix: deduplicate tool_use IDs and enable sanitization for Anthropic
by marcelomar21 · 2026-01-30
80.0%
#18926: fix(agents): preserve thinking signatures for direct Anthropic API
by BinHPdev · 2026-02-17
79.6%