sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] Tool starts after cancellation when cancel happens during an async BeforeToolCallEvent hook

Open opieter-aws opened this issue 1 month ago • 0 comments

Human Overview

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

TypeScript (also affects Python, see below)

Strands Version

TypeScript @strands-agents/sdk 1.19.0 and 1.21.0; Python strands-agents 1.61.1

Language Runtime Version

Node.js 22+; Python 3.10 and 3.13

Operating System

Not OS-specific (reproduced offline with a scripted model)

Installation Method

npm

Steps to Reproduce

  1. Register an async BeforeToolCallEvent hook that awaits something (e.g. an approval or policy lookup) before allowing the call.
  2. Invoke an agent whose (scripted) model requests a tool that writes a marker file.
  3. While the hook is still awaiting, call agent.cancel(), or abort the external cancelSignal (TS) / cancel_signal (Python).
  4. Let the hook return and allow the call.
  5. The tool callback starts with the cancel signal already set and writes the marker file. The tool result has status: "success", yet the run reports stopReason: "cancelled".

This happens with both the concurrent and the sequential tool executor, in both SDKs.

Expected Behavior

Per the documented "Before tool execution" cancellation checkpoint, a cancel that arrives while the before-tool hook is running should prevent the tool from starting. The call should be skipped, the same way as a hook-requested cancel.

Actual Behavior

After the hook returns, the executor only checks the hook's own cancel flag (event.cancel / cancel_tool). It never re-checks the agent's cancel signal, so the tool runs after cancellation. The reported outcome (cancelled) disagrees with the recorded tool result (success). The hook's allow/deny decision is still enforced, so this is not a permission bypass.

Possible Solution

In both executors, check the cancel signal immediately after the hook returns and handle it like a hook-requested cancel, with regression tests covering agent.cancel() and the external signal on both executors.

Workaround until fixed: check the agent's cancel signal at the end of async before-tool hooks and set event.cancel / cancel_tool.

Additional Context

Code and docs references
  • TypeScript: strands-ts/src/tools/executors/executor.ts, the post-hook event.cancel check. The sequential executor checks the signal only before the hook runs, which is too early for this case.
  • Python: strands-py/src/strands/tools/executors/_executor.py, the post-hook cancel_tool check.
  • Docs: the agent-loop page lists "Before tool execution" as a cancellation checkpoint, and the Agent.cancel() docstring lists it as a point where the agent stops.

Related Issues

None found.

opieter-aws avatar Oct 29 '26 19:10 opieter-aws