Skip to content

Breaking: propagate Python Strands tool cancellation - #497

Merged
brianstrauch merged 2 commits into
mainfrom
codex/fix-strands-tool-cancellation
Nov 12, 2026
Merged

brianstrauch merged 2 commits into
mainfrom
codex/fix-strands-tool-cancellation

Conversation

@brianstrauch

@brianstrauch brianstrauch commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Cancelling a Workflow during a Strands activity or MCP tool call currently leaves the concurrent executor waiting forever: the tool converts cancellation into an error result, then waits for an acknowledgment from a parent that has stopped consuming events. With the sequential executor, the agent instead continues and can complete successfully after cancellation.

Record the tool task's cancellation request count before each activity call. Translate Temporal activity cancellation to asyncio.CancelledError only when that count increases during the call. Independently reported activity cancellation remains a failed tool result with the matching toolUseId, including when the task caught an earlier cancellation. Both tool kinds now let a cancelled Workflow close as CANCELED, while preserving TRY_CANCEL, WAIT_CANCELLATION_COMPLETED, and ABANDON behavior.

Add twelve integration cases covering both tool kinds and executors, cleanup ordering, no subsequent model call after Workflow cancellation, and explicit history replay. Eight isolated cases cover independently reported cancellation and previously caught task cancellation, verifying that the next model call retains its matching tool result.

Breaking change: there is no replay patch. Existing histories that processed workflow or tool-task cancellation as an error result can fail replay on this version. Keep affected in-flight runs on their original workers with Worker Versioning. To recover a run stuck during tool cancellation, reset it to before the affected tool call and run it on upgraded workers. The README includes this guidance.

Validation:

  • make lint
  • make test PYTEST_ARGS='tests/test_cancellation.py tests/test_independent_cancellation.py tests/test_tool_errors.py tests/test_interrupt_exception.py -n 2 -q --tb=short' — 27 passed
  • Confirmed the original Workflow-cancellation regression times out before the initial fix, and the independent-cancellation regression loses its tool result before the follow-up fix.

@brianstrauch
brianstrauch requested a review from a team as a code owner November 12, 2026 22:25

@campoy campoy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good

@brianstrauch
brianstrauch merged commit 33b729b into main Nov 12, 2026
27 checks passed
@brianstrauch
brianstrauch deleted the codex/fix-strands-tool-cancellation branch November 12, 2026 23:01
campoy added a commit that referenced this pull request Nov 14, 2026
… heartbeat timeout (#504)

## What was changed

`TemporalAgent`, `TemporalModel` and `TemporalSandbox` now default
`heartbeat_timeout` to 10 seconds instead of `None`, so the model and
sandbox activities heartbeat by default.

- The activities already heartbeat on a timer at half the timeout
(`auto_heartbeater`). A timeout you set wins, and
`heartbeat_timeout=None` turns heartbeats off, as before.
- The timer now starts when the activity does, not at its first `await`.
Before, a model factory that blocked the event loop for over half the
timeout made the attempt fail with a heartbeat timeout. Now it takes the
whole timeout, which is when the warning below fires.
- `strands_get_model_config` is now scheduled without a heartbeat
timeout. It builds the model and reads its config synchronously, so it
can't heartbeat, and a model factory slower than the timeout would fail
it.
- A model factory slower than the heartbeat timeout now logs a warning
on the worker. The factory runs on the worker's event loop, so the
heartbeat timer can't run while it builds the model, and that attempt
may fail with a heartbeat timeout. The next attempt reuses the built
model.
- The docstrings and the README say what the default does and how to
turn it off. They also say that a model factory or model call that
blocks the event loop longer than the timeout fails. In Strands 1.56.0,
`SageMakerAIModel` and `LlamaAPIModel` call their synchronous clients on
the loop, so they need `heartbeat_timeout=None` (or a longer timeout).

`activity_as_tool` and `activity_as_hook` run your own Activities, so
they stay as they are.

### Why not build models on a thread?

An automated review round suggested it, so a slow factory couldn't block
the heartbeat timer. But that broke factories that need a running event
loop, such as one that builds an `aiohttp` session, and the fix for that
made this PR much bigger. A factory slower than 10 seconds is rare, and
the retry reuses the model it built, so a warning that says what
happened is enough.

## Why?

Without a heartbeat timeout, these activities never heartbeat. So a
cancel never reaches a running model call or sandbox command, and it
runs to the end. A lost worker also goes unnoticed until the
start-to-close timeout. #491 has the details, and the measurements
behind 10 seconds.

Does this break anything? Not replay: when core replays a scheduled
Activity, it only compares its ID and type, and the replay tests pass.
But a call that didn't set `heartbeat_timeout` now heartbeats, which in
Temporal Cloud is at most one Action every 8 seconds of a running call.

`TemporalMCPClient` isn't in this PR. Since #435, Strands requires
`temporalio-mcp` 0.4, whose Activities heartbeat (#494). But Strands'
`TemporalMCPClient` still passes `heartbeat_timeout=None`, so its
Activities don't heartbeat yet. A follow-up can give it the 10-second
default too.

This works with #497, which makes a cancelled tool call cancel the
agent. I merged its branch on top of this one, and its cancellation
tests pass with these defaults.

## Checklist

1. Closes #498

2. How was this tested:
- `test_model` now checks the heartbeat timeouts in History: none for
`strands_get_model_config`, 10 seconds for `invoke_model`. Without the
change it fails with `assert [None, None] == [None,
datetime.timedelta(seconds=10)]`.
-
`test_sandbox_operations_are_durable_and_factory_runs_for_each_activity`
now checks that all seven sandbox Activities get 10 seconds. Without the
change, they get none.
- `test_heartbeat_timer_counts_from_the_start` blocks the event loop for
0.7 seconds under a 1-second heartbeat timeout, and checks that the
first heartbeat comes before the timeout. With the old timer, the first
heartbeat would come 1.2 seconds in, so the test fails with `assert
([])`.
- `test_slow_model_factory_logs_a_warning` builds a model with a factory
that takes 0.3 seconds, under a 0.1-second heartbeat timeout, and checks
the warning. A fast factory logs nothing. Without the warning it fails
with `assert ("Building model 'slow' took" in '') == True`.
- Locally, `make test` (262 passed, 1 skipped), `make lint` and the
conventions check pass, on Python 3.12 with uv 0.12.24. With #497 merged
in, its two cancellation test files and these tests pass (53 passed).

3. Any docs updates needed?
The docstrings and the README are updated here. Release notes should
mention the new default, and how to turn it off.
campoy added a commit that referenced this pull request Nov 14, 2026
Take main's tool cancellation from #497, which replaces this branch's
change to activity and MCP tools. List a summarization agent's MCP tools
through the hook from #184. Move the blocking MCP tool and the
summarizer's MCP test to the temporalio.mcp API from #190.
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.

2 participants