Skip to content

Fix: Support tool cancellation#2697

Open
slg95 wants to merge 2 commits intoolimorris:mainfrom
slg95:fix_cancellable_tools
Open

Fix: Support tool cancellation#2697
slg95 wants to merge 2 commits intoolimorris:mainfrom
slg95:fix_cancellable_tools

Conversation

@slg95
Copy link
Contributor

@slg95 slg95 commented Jan 25, 2026

Description

Currently, tools cannot be cancelled so accidental tool usage or long running tools cannot be stopped without closing the chat window and potentially losing the conversation. This PR allows the user to use the stop keymap in chat to cancel to the tool call and inform the AI agent.

This will kill the process for tools run through cmd_runner, but it does not actually cancel function calls. So tools invoked through something like MCPHub.nvim, or the builtin tools, will continue to execute in the background to completion. To actually cancel the function calls would require a bit more refactoring so I figured that it would be worth a discussion before any further implementation.

I tested this on Linux (Fedora) and Windows.

AI Usage

Claude Sonnet 4.5 did an initial version, but I cleaned it up and essentially rewrote most of it.

Related Issue(s)

Screenshots

Checklist

  • I've read the contributing guidelines and have adhered to them in this PR
  • I confirm that this PR has been majority created by me, and not AI (unless stated in the "AI Usage" section above)
  • I've run make all to ensure docs are generated, tests pass and StyLua has formatted the code
  • (optional) I've added test coverage for this fix/feature
  • (optional) I've updated the README and/or relevant docs pages

@slg95 slg95 force-pushed the fix_cancellable_tools branch from a40733c to 574ef66 Compare January 25, 2026 16:45
@@ -1,3 +1,16 @@
---@class CodeCompanion.Tools.Orchestrator
---@field cancelled boolean? Whether the tool execution has been cancelled
Copy link
Owner

Choose a reason for hiding this comment

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

minor nit but I prefer these to be in alphabetical order as my eyes can find things faster

local fmt = string.format
local Orchestrator = {}

--=============================================================================
Copy link
Owner

Choose a reason for hiding this comment

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

I'd prefer to skip these public and private blocks as the initial underscore tells us they're private

@olimorris
Copy link
Owner

This looks like a fabulous PR, thank you. Very well timed as I intend to add MCP support very soon. This looks good to merge. I'll decide whether to add it to develop or main in the coming days as I want to release v19.0.0 soon

@slg95
Copy link
Contributor Author

slg95 commented Jan 26, 2026

This looks like a fabulous PR, thank you. Very well timed as I intend to add MCP support very soon. This looks good to merge. I'll decide whether to add it to develop or main in the coming days as I want to release v19.0.0 soon

Thank you! Would you like me to fix the comments you made, or are you planning on fixing them?

@slg95 slg95 force-pushed the fix_cancellable_tools branch from 574ef66 to 6646e64 Compare January 27, 2026 00:53
@slg95
Copy link
Contributor Author

slg95 commented Jan 27, 2026

This looks like a fabulous PR, thank you. Very well timed as I intend to add MCP support very soon. This looks good to merge. I'll decide whether to add it to develop or main in the coming days as I want to release v19.0.0 soon

Thank you! Would you like me to fix the comments you made, or are you planning on fixing them?

Just updated the PR for those two small changes in orchestrator.lua and squashed the changes into the original commits.

@bassamsdata
Copy link
Contributor

Very nice and much-needed PR, thank you

Would it make sense to add an option to the function to cancel only the current tool, with a specific keymap (e.g. gq)? There are cases where cmd_runner can get stuck (it might also benefit from a timeout), and being able to cancel it while preserving pending tool calls in the queue would be helpful.

This could also be handled in a separate PR if preferred.

@slg95
Copy link
Contributor Author

slg95 commented Feb 4, 2026

Very nice and much-needed PR, thank you

Would it make sense to add an option to the function to cancel only the current tool, with a specific keymap (e.g. gq)? There are cases where cmd_runner can get stuck (it might also benefit from a timeout), and being able to cancel it while preserving pending tool calls in the queue would be helpful.

This could also be handled in a separate PR if preferred.

I defaulted to a "cancel all" approach, but I can see how you may want to only cancel the current tool call that is stuck. I would think this would belong in a separate PR if its something the maintainer wanted to support

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.

3 participants