fix: append error tool message when tool call is cancelled#2492
Merged
andreynering merged 1 commit intocharmbracelet:mainfrom Mar 26, 2026
Merged
fix: append error tool message when tool call is cancelled#2492andreynering merged 1 commit intocharmbracelet:mainfrom
andreynering merged 1 commit intocharmbracelet:mainfrom
Conversation
When a user cancels a tool call by pressing Esc twice, the assistant message with tool_calls was remaining in the conversation history but no tool response followed. This caused 400 Bad Request errors from model providers on the next message. The fix uses the parent context (ctx) instead of the request context (genCtx) when updating messages during tool call tracking and tool result creation. This ensures that even if the request is cancelled mid-stream, the message updates and creations succeed. Additionally, the error message was updated from 'Tool execution canceled by user' to 'Error: user cancelled assistant tool calling' to better indicate the cancellation occurred during the assistant tool calling phase. Fixes charmbracelet#529
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Contributor
Author
|
I have read the Contributor License Agreement (CLA) and hereby sign the CLA. |
andreynering
approved these changes
Mar 26, 2026
Member
andreynering
left a comment
There was a problem hiding this comment.
Looks good. Thank you @fuleinist!
This was referenced Mar 26, 2026
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.
Summary
When a user cancels a tool call by pressing Esc twice, the assistant message with tool_calls remains in the conversation history but no tool response follows. This causes 400 Bad Request errors from model providers on the next message.
The fix uses the parent context (ctx) instead of the request context (genCtx) when updating messages during tool call tracking and tool result creation. This ensures that even if the request is cancelled mid-stream, the message updates and creations succeed.
Additionally, the error message was updated from 'Tool execution canceled by user' to 'Error: user cancelled assistant tool calling' to better indicate the cancellation occurred during the assistant tool calling phase.
Changes
Testing
Fixes #529
Fixes #1206