Fix HTTP 400 errors after Claude Code CLI tool call cancellation#2
Merged
Fix HTTP 400 errors after Claude Code CLI tool call cancellation#2
Conversation
Copilot
AI
changed the title
[WIP] after human cancel on claude code cli, the copilot-api keep return 400
Fix HTTP 400 errors after Claude Code CLI tool call cancellation
Jul 31, 2025
Owner
|
run the test |
Author
Tests have been successfully executed. All 36 tests pass across 5 test files, including the tool conversation cleanup functionality. The code also passes linting and builds successfully. |
Owner
|
LSTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a critical issue where cancelling tool calls in Claude Code CLI would cause all subsequent API requests to fail with HTTP 400 errors.
Problem
When a user cancels a tool call in Claude Code CLI, the conversation history retains
tool_useblocks without correspondingtool_resultblocks. The Anthropic API strictly requires that everytool_useblock be followed by a matchingtool_resultblock in the next message. Without this pairing, all subsequent requests fail with:This creates a persistent error state where the API becomes unusable until the conversation is reset.
Solution
Added intelligent tool conversation validation and cleanup that:
tool_useblocks followed by user messages without correspondingtool_resultblockstool_useblocks at conversation end (still awaiting results)tool_useblocks when there's clear evidence of cancellationExample
Before the fix, this conversation would cause persistent 400 errors:
{ "messages": [ {"role": "user", "content": "What's the weather?"}, { "role": "assistant", "content": [ {"type": "text", "text": "I'll check for you."}, {"type": "tool_use", "id": "call_123", "name": "get_weather", "input": {...}} ] }, {"role": "user", "content": "Actually, never mind."} ] }After the fix, the abandoned
tool_useblock is automatically removed, preventing API rejection.Implementation
The fix integrates seamlessly into the existing Anthropic message translation pipeline, ensuring all problematic payloads are cleaned before reaching the API.
Fixes #1.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
aur.archlinux.orgbun run ./src/main.ts --help(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.