Skip to content

fix: repair Anthropic tool-call pairing to clear upstream 11148 - #13

Merged
hawklithm merged 11 commits into
mainfrom
fix/anthropic-tool-pairing
Oct 22, 2026
Merged

hawklithm merged 11 commits into
mainfrom
fix/anthropic-tool-pairing

Conversation

@hawklithm

@hawklithm hawklithm commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Context

Some agent clients submit malformed or nonstandard Anthropic tool histories that the CodeBuddy Chat backend rejects as HTTP 400 / 11148 (tool calls and tool results do not match). The Responses adapter already had a pairing repair, but the Anthropic adapter did not.

Changes

  • Route the Anthropic and Responses adapters through a single shared tool_pairing.py normalizer.
  • Preserve valid calls and results; defer interleaved text until the current tool group completes.
  • Drop orphan results and identical duplicate results with diagnostics that do not include tool output.
  • Fail closed with HTTP 422 / detail.code=invalid_tool_history when a tool call has no result, IDs are duplicated/reused, or repeated results contradict one another. This prevents silently erasing potentially side-effecting executions. Such requests are not forwarded upstream.
  • Cover unpaired parallel calls, cross-assistant groups, duplicate/conflicting results, long mixed histories, and actual /v1/messages and /v1/responses HTTP routes.
  • Clarify the boundary between client-compatibility repair and standards-compliant Anthropic histories in English and Chinese README.
  • Add protocol-focused GitHub Actions checks for Python 3.10 and 3.12.

Validation

  • GitHub Actions protocol regression suite: 35 passed on Python 3.10; 35 passed on Python 3.12 (initial successful run).
  • These are mocked local adapter/HTTP-route tests; they do not establish a live CodeBuddy upstream A/B reproduction.
  • Full project test suite and live side-effecting tool flows were not run as part of this PR review.

Operational note

If the proxy returns 422, verify whether an unacknowledged tool may already have performed its side effect before retrying. Direct /v1/chat/completions traffic is not normalized by these adapters.

hawklithm and others added 11 commits October 21, 2026 23:01
Claude Code and CC Switch send legal Anthropic histories whose tool
results are not adjacent to the declaring assistant tool_calls:
parallel tool_use blocks answered in later turns, duplicated
tool_result blocks, and orphan results. _convert_anthropic_message()
only translated blocks, so the CodeBuddy backend rejected those
histories with HTTP 400 code 11148 ("tool calls and tool results do
not match"). The Responses path already had this repair (6274044);
the Anthropic path did not, which is why the errors only appeared
under protocol=anthropic.

Apply _sanitize_tool_pairing() to the converted messages: defer
interleaved messages until their tool group completes, and drop
orphan/duplicate results plus unanswered tool calls while keeping the
assistant text. Add regression tests covering the reported 59-message
agentic history and the individual malformed shapes.
@hawklithm
hawklithm merged commit 92698b7 into main Oct 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant