Skip to content

[WIP] after human cancel on claude code cli, the copilot-api keep return 400#3

Draft
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-1-2
Draft

[WIP] after human cancel on claude code cli, the copilot-api keep return 400#3
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-1-2

Conversation

Copy link
Copy Markdown

Copilot AI commented Jul 31, 2025

  • Analyzed the issue: "after human cancel on claude code cli, the copilot-api keep return 400"
  • Explored existing codebase and found tool conversation cleanup logic already exists
  • Built and tested current implementation - all existing tests pass
  • Identified edge case: current logic only removes tool_use blocks when followed by user messages
  • Created test case that reproduces the issue: tool_use followed by assistant message without tool_result
  • Update cleanup logic to handle assistant messages after abandoned tool_use
  • Ensure all tests pass with the updated logic
  • Verify the fix resolves the 400 error scenario

The Fix:
Modified validateAndCleanupToolConversations function to detect abandoned tool_use blocks more comprehensively:

  • Before: Only removed tool_use blocks when followed by subsequent user messages
  • After: Removes tool_use blocks when followed by any subsequent messages (user or assistant)

This fixes the issue where Claude Code CLI cancellations could leave tool_use blocks in an invalid state, causing subsequent 400 errors from the Anthropic API.

Changes Made:

  1. Updated logic in tool-conversation-cleanup.ts to check for any subsequent messages, not just user messages
  2. Added comprehensive test cases for edge scenarios including assistant messages after tool_use
  3. Added specific test with exact tool ID from error message (toolu_vrtx_01GG3T6npgARqCjxkn9Fm9KW)
  4. Added test for long conversations (216+ messages) to verify the fix works in complex scenarios
  5. Updated documentation to reflect the broader scope of abandonment detection
  6. All existing tests continue to pass, ensuring backward compatibility

Root Cause:
The issue occurred when a tool_use block was followed by an assistant message instead of a user message. The previous logic only considered user messages as indicators of tool abandonment, but assistant messages can also indicate that a tool call was cancelled or bypassed.

Test Coverage:

  • ✅ Original Claude Code CLI cancellation scenario (existing test)
  • ✅ Tool_use followed by assistant message without tool_result (new test)
  • ✅ Long conversations with abandoned tools (new test)
  • ✅ Exact tool ID from error message (new test)
  • ✅ Mixed scenarios with partial completions (existing test)
  • ✅ Valid pending tool calls at end of conversation (existing test)

Fixes #1.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@hao1939
Copy link
Copy Markdown
Owner

hao1939 commented Jul 31, 2025

please take the fix: https://github.com/qike-ms/copilot-api/pull/1/files

Co-authored-by: hao1939 <1547343+hao1939@users.noreply.github.com>
@hao1939
Copy link
Copy Markdown
Owner

hao1939 commented Jul 31, 2025

try again

Copy link
Copy Markdown
Author

Copilot AI commented Jul 31, 2025

please take the fix: https://github.com/qike-ms/copilot-api/pull/1/files

I can't access external URLs directly. Could you please share the specific changes from that PR that you'd like me to implement? You can either:

  1. Copy/paste the relevant code changes here
  2. Describe what specific fix you want me to apply

The current implementation handles abandoned tool_use blocks by detecting when they're followed by any subsequent messages without tool_result blocks. If there's a different approach or additional handling needed, I'm happy to implement it once I understand what changes you'd like me to make.

Copy link
Copy Markdown
Author

Copilot AI commented Jul 31, 2025

try again

I can't access external URLs directly. Could you please share the specific changes from that PR that you'd like me to implement? You can either:

  1. Copy/paste the relevant code changes here
  2. Describe what specific fix you want me to apply

The current implementation handles abandoned tool_use blocks by detecting when they're followed by any subsequent messages without tool_result blocks. If there's a different approach or additional handling needed, I'm happy to implement it once I understand what changes you'd like me to make.

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.

after human cancel on claude code cli, the copilot-api keep return 400

2 participants