← Back to PRs

#13831: fix(agents): include Anthropic in tool call ID sanitization

by lailoo open 2026-02-11 02:46 View on GitHub →
agents stale size: XS trusted-contributor
## Summary Fixes #13799 ## Problem `sanitizeToolCallIds` in `transcript-policy.ts` only enables tool call ID sanitization for Google and Mistral providers, not Anthropic. This causes the Anthropic API to reject requests when `tool_use` IDs contain characters outside `^[a-zA-Z0-9-]+$` (e.g., underscores in `toolu_01XYZ` style IDs from other providers or synthetic results). The error manifests as: ``` messages.370.content.1.tooluse.id: String should match pattern '^[a-zA-Z0-9-]+$' ``` **Root cause** in `src/agents/transcript-policy.ts`: ```typescript const sanitizeToolCallIds = isGoogle || isMistral; // Missing: isAnthropic and OpenRouter Claude models ``` ## Fix Add `isAnthropic` and `isOpenRouterAnthropic` to the `sanitizeToolCallIds` condition, and also to `repairToolUseResultPairing` for OpenRouter Claude models. **Before:** ```typescript const sanitizeToolCallIds = isGoogle || isMistral; ``` **After:** ```typescript const sanitizeToolCallIds = isGoogle || isAnthropic || isOpenRouterAnthropic || isMistral; ``` Also added `isOpenRouterAnthropic` detection (same pattern as existing `isOpenRouterGemini`) for OpenRouter + Claude model IDs. ## Reproduction & Verification ### Before fix (main branch) — 4 tests fail: ``` pnpm vitest run src/agents/transcript-policy.test.ts Tests 4 failed | 4 passed (8) FAIL: enables tool call ID sanitization for Anthropic (strict mode) FAIL: enables tool call ID sanitization for Anthropic via provider detection FAIL: enables tool call ID sanitization for OpenRouter with Claude models FAIL: enables tool call ID sanitization for OpenRouter with anthropic in model ID ``` ### After fix — All 24 tests pass: ``` pnpm vitest run src/agents/transcript-policy.test.ts src/agents/pi-embedded-runner.sanitize-session-history.test.ts src/agents/tool-call-id.test.ts Test Files 3 passed (3) Tests 24 passed (24) ``` ## Effect on User Experience **Before fix:** Anthropic API rejects requests with tool calls containing non-alphanumeric IDs, causing persistent `tool_use.id` format errors. The gateway logs "Tool calls will be sanitized on retry" but no sanitization actually runs for Anthropic. **After fix:** Tool call IDs are sanitized to `^[a-zA-Z0-9]+$` (strict mode) before sending to Anthropic, preventing format rejection errors. This applies to direct Anthropic connections and OpenRouter/OpenCode with Claude models. ## Testing - ✅ 24 tests pass (pnpm vitest run src/agents/transcript-policy.test.ts src/agents/pi-embedded-runner.sanitize-session-history.test.ts src/agents/tool-call-id.test.ts) - ✅ Lint passes <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR updates `resolveTranscriptPolicy` to enable tool-call ID sanitization for Anthropic models and for OpenRouter/OpenCode routes that serve Anthropic/Claude models, preventing Anthropic-side `tool_use.id` pattern rejections. It also expands transcript repair (`repairToolUseResultPairing`) to run for OpenRouter Claude models and adjusts the `sanitizeSessionHistory` test to assert sanitization is enabled for Anthropic APIs. A changelog entry documents the fix. <h3>Confidence Score: 5/5</h3> - This PR is safe to merge with minimal risk. - Changes are narrowly scoped to provider/model detection in transcript sanitization policy and align with existing sanitization/repair mechanisms. No behavior changes for OpenAI paths, and the updated unit test matches the intended new policy. - No files require special attention <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs