Skip to content

fix: append error tool message when tool call is cancelled#2492

Merged
andreynering merged 1 commit intocharmbracelet:mainfrom
fuleinist:fix/issue-529
Mar 26, 2026
Merged

fix: append error tool message when tool call is cancelled#2492
andreynering merged 1 commit intocharmbracelet:mainfrom
fuleinist:fix/issue-529

Conversation

@fuleinist
Copy link
Copy Markdown
Contributor

@fuleinist fuleinist commented Mar 26, 2026

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

  • OnToolInputStart: Use parent ctx instead of genCtx for messages.Update
  • OnToolCall: Use parent ctx instead of genCtx for messages.Update
  • OnToolResult: Use parent ctx instead of genCtx for messages.Create
  • Updated error message to 'Error: user cancelled assistant tool calling'

Testing

  • Existing tests pass (go build succeeds)
  • Fix ensures tool call cancellation properly creates error tool message

Fixes #529
Fixes #1206

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
@fuleinist fuleinist requested a review from a team as a code owner March 26, 2026 08:48
@fuleinist fuleinist requested review from andreynering and aymanbagabas and removed request for a team March 26, 2026 08:48
@charmcli
Copy link
Copy Markdown
Contributor

charmcli commented Mar 26, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@fuleinist
Copy link
Copy Markdown
Contributor Author

I have read the Contributor License Agreement (CLA) and hereby sign the CLA.

Copy link
Copy Markdown
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

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

Looks good. Thank you @fuleinist!

@andreynering andreynering merged commit 2928dcb into charmbracelet:main Mar 26, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot resume cancelled Crush session Wrong context when tool_call is cancelled

3 participants