Skip to content

Comments

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

Open
Arthur742Ramos wants to merge 1 commit intoopenclaw:mainfrom
Arthur742Ramos:fix/claude-tool-use-repair-any-provider
Open

[AI-Assisted] Fix: Repair tool_use/tool_result pairing for Claude on any provider#2806
Arthur742Ramos wants to merge 1 commit intoopenclaw:mainfrom
Arthur742Ramos:fix/claude-tool-use-repair-any-provider

Conversation

@Arthur742Ramos
Copy link

@Arthur742Ramos Arthur742Ramos commented Jan 27, 2026

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:

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 Overview

Greptile Summary

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.

Confidence Score: 4/5

  • 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)

(2/5) Greptile learns from your feedback when you react with thumbs up/down!

When using Claude models via non-Anthropic providers (github-copilot,
openrouter, amazon-bedrock, etc), the repairToolUseResultPairing sanitizer
was not running, causing 400 errors when sessions had orphaned tool_use
blocks without matching tool_result.

Added isClaudeModel() helper that detects Claude by modelId, and wired it
into:
- repairToolUseResultPairing (fixes the 400 error)
- validateAnthropicTurns (Claude needs Anthropic-style turn validation)
- allowSyntheticToolResults (allows inserting synthetic results for missing)

Added comprehensive test suite covering:
- Direct Anthropic provider
- Claude via github-copilot, openrouter, opencode, amazon-bedrock
- Non-Claude models (GPT, Llama, Gemini)
- Edge cases (null/empty modelId)
- Case-insensitive detection

Fixes: '400 messages.220: tool_use ids were found without tool_result blocks'
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +65 to +72
/**
* Detects Claude models by checking if the modelId contains 'claude'.
* This catches Claude models accessed via non-Anthropic providers like
* github-copilot, openrouter, etc.
*/
function isClaudeModel(modelId?: string | null): boolean {
if (!modelId) return false;
return modelId.toLowerCase().includes("claude");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Claude detection is over-broad and may incorrectly enable Anthropic-specific policies

isClaudeModel() is a plain substring check (modelId.toLowerCase().includes("claude")), and isClaude is then used to enable validateAnthropicTurns/allowSyntheticToolResults (and tool_use/tool_result repair) for any non-OpenAI provider. If a provider ever has a non-Anthropic-format model whose ID contains "claude" (or modelId is user-configurable and contains that substring), this would turn on Anthropic-style validation/repair and could cause incorrect sanitization or unexpected transcript mutations.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/transcript-policy.ts
Line: 65:72

Comment:
[P1] Claude detection is over-broad and may incorrectly enable Anthropic-specific policies

`isClaudeModel()` is a plain substring check (`modelId.toLowerCase().includes("claude")`), and `isClaude` is then used to enable `validateAnthropicTurns`/`allowSyntheticToolResults` (and tool_use/tool_result repair) for any non-OpenAI provider. If a provider ever has a non-Anthropic-format model whose ID contains "claude" (or modelId is user-configurable and contains that substring), this would turn on Anthropic-style validation/repair and could cause incorrect sanitization or unexpected transcript mutations.

How can I resolve this? If you propose a fix, please make it concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant