Skip to content

fix(strands): give the model the activity's cancellation signal - #297

Merged
campoy merged 9 commits into
mainfrom
fix/strands-typescript-model-cancel-signal-lost
Nov 4, 2026
Merged

campoy merged 9 commits into
mainfrom
fix/strands-typescript-model-cancel-signal-lost

Conversation

@campoy

@campoy campoy commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

The model Activities give the model the Activity's own cancellation signal
(Context.current().cancellationSignal) as cancelSignal, in place of the agent's.

Strands puts the agent's cancelSignal, an AbortSignal, in the model's stream options. It reached
the Activity as a plain object ({}). From @strands-agents/sdk 1.15.0, OpenAIModel hands it to the
OpenAI client, which calls signal.addEventListener, so every OpenAIModel call through the plugin
failed before its request, Responses and Chat Completions alike. That includes the plugin's locked
1.19.0 and the latest 1.22.0.

With the Activity's signal, the request is sent as it is natively. When the model Activity has a
heartbeatTimeout, cancelling it aborts the signal; a model that passes cancelSignal to its client
(in Strands 1.19.0, OpenAIModel and BedrockModel) then stops the provider call, as cancelling the
agent does natively. AnthropicModel, GoogleModel and VercelModel ignore cancelSignal, so their
provider call keeps running and the Activity can still complete with its result. A remote Activity
learns of cancellation only through heartbeats, so without a heartbeatTimeout the model call runs to
completion. The README's new Cancellation section documents this, and how TemporalAgent cancellation
types behave.

Model clients abort with their own errors (OpenAI's APIUserAbortError is named Error), which
Temporal would record as an Activity failure. When the model throws after the Activity's signal
aborted, the Activity throws the signal's CancelledFailure instead, so the Activity ends as
cancelled. One helper in model-activity.ts does this for invokeModel and
invokeModelStreaming. Workflow code and the Activity input do not change.

Why?

See #296.

Compatibility

No version gate is needed, and nothing that works today breaks:

  • Workflow code is unchanged, so every history replays as before.
  • Models that ignored the signal still ignore it and behave as before. Models that accepted the plain
    object without ever aborting (Bedrock's HTTP handler sets onabort on it) now get a signal that
    aborts when the model Activity is cancelled with a heartbeatTimeout set. Natively, cancelling the
    agent aborts their call the same way.
  • A Worker without this change still fails OpenAIModel calls; during a rollout they are retried
    until an upgraded Worker takes them.

Checklist

  1. Closes [Bug] strands-agents (TypeScript): OpenAIModel calls fail in the model Activity on Strands 1.15.0 and later #296

  2. How was this tested:

    • invokeModel gives the model Message instances and an AbortSignal (the existing wire-data
      test, extended) checks that the model gets an AbortSignal. It fails on main
      (expected cancelSignal to be an AbortSignal).
    • cancelling the workflow aborts the model call in invokeModel and cancels the workflow, and
      the same for invokeModelStreaming, run TemporalModel.stream on a real Worker with
      heartbeatTimeout and WAIT_CANCELLATION_COMPLETED, cancel the Workflow mid-call, and use a
      model that throws an Error named Error once its signal aborts, as OpenAI's client does. They
      check that the model's signal aborted and that the history has ActivityTaskCanceled, no
      ActivityTaskFailed, and WorkflowExecutionCanceled. Without the CancelledFailure rethrow
      both fail with expected a CancelledFailure, got ActivityFailure: Activity task failed.
    • cancelling a TemporalAgent workflow with TRY_CANCEL aborts the model call and cancels the workflow, and the same with WAIT_CANCELLATION_COMPLETED, run agent.invoke() on a real Worker
      with heartbeatTimeout and cancel the Workflow mid-call. They check that the model's signal
      aborts while the Worker runs and that the history has ActivityTaskCancelRequested,
      WorkflowExecutionCanceled and no WorkflowTaskFailed (plus ActivityTaskCanceled with
      WAIT_CANCELLATION_COMPLETED). With TemporalAgent's ModelError unwrap disabled, both time out
      with repeated WorkflowTaskFailed.
    • cancelling a TemporalAgent workflow with TRY_CANCEL while the model ignores its signal, and the
      same with WAIT_CANCELLATION_COMPLETED, use a model that keeps going after its signal aborts and
      returns late result. With TRY_CANCEL the run is cancelled. With WAIT_CANCELLATION_COMPLETED
      the run completes with late result, and the history has ActivityTaskCompleted,
      WorkflowExecutionCompleted and no WorkflowExecutionCanceled.
    • [Bug] strands-agents (TypeScript): OpenAIModel calls fail in the model Activity on Strands 1.15.0 and later #296's reproduction, with the real OpenAIModel and a recording fetch, Responses and Chat
      Completions: on main it fails with @strands-agents/sdk 1.15.0, 1.19.0 and 1.22.0 and passes
      with 1.14.0; with the signal change it passes on all four. Also with @temporalio/* 1.25.0 and
      Strands 1.22.0. (This ran before the CancelledFailure rethrow was added.)
    • Which models honor cancelSignal was taken from the locked Strands 1.19.0 sources: only
      OpenAIModel and BedrockModel pass it to their clients.
    • Local, in a Linux container: make sync, make lint, make test (86 tests), make build and
      check_conventions.py pass. The Node 22, macOS and Windows cells did not run locally.
    • Not run against a real model provider, and not with Bedrock.
  3. Any docs updates needed?
    The README gains a Cancellation section with the heartbeatTimeout requirement, the dependence on
    the model honoring cancelSignal, and how TemporalAgent's TRY_CANCEL and
    WAIT_CANCELLATION_COMPLETED cancellation types differ, including that with
    WAIT_CANCELLATION_COMPLETED a model Activity that completes after cancellation lets the run
    complete normally. The Models section's note from fix(strands): run stateful models with their server-side state (breaking) #285, which said OpenAIModel calls work only
    before @strands-agents/sdk 1.15.0, is rewritten: such calls work with current releases, and only
    a Worker without this change still fails them during a rollout.

Not covered

  • agent.cancel() in Workflow code still does not cancel the model Activity; cancel it through
    Temporal (a cancellation scope or a Workflow cancel).
  • Models that ignore cancelSignal (in Strands 1.19.0, AnthropicModel, GoogleModel and
    VercelModel) are not aborted, and their Activity can complete with the model's result after
    cancellation. With WAIT_CANCELLATION_COMPLETED, the agent then continues with that result and the
    run can complete instead of being cancelled.

@campoy
campoy force-pushed the fix/strands-typescript-model-cancel-signal-lost branch from f6c47a9 to 7038d00 Compare October 30, 2026 23:36
@campoy
campoy marked this pull request as ready for review October 31, 2026 01:35
@campoy
campoy requested a review from a team as a code owner October 31, 2026 01:35
Strands passes the agent's cancelSignal, an AbortSignal, in the model's
stream options. The model activity got it as a plain object, so
OpenAIModel, which hands it to the OpenAI client, failed every call with
"signal.addEventListener is not a function" before sending a request,
from @strands-agents/sdk 1.15.0 on.

The model activities now give the model the activity's own cancellation
signal instead. Cancelling the activity then aborts the model call, as
cancelling the agent does natively.
@campoy
campoy force-pushed the fix/strands-typescript-model-cancel-signal-lost branch from 3952aed to ecc7667 Compare October 31, 2026 01:52
Other tests on main use ModelActivity. Rebasing the commit that removed
this PR's own copy of the import also removed main's.
@campoy
campoy merged commit c21bdd2 into main Nov 4, 2026
37 of 39 checks passed
@campoy
campoy deleted the fix/strands-typescript-model-cancel-signal-lost branch November 4, 2026 00:52
campoy added a commit that referenced this pull request Nov 13, 2026
…eout (#508)

## What was changed

`TemporalModel`, and so `TemporalAgent`, now schedules the model
activities with a 10-second `heartbeatTimeout` unless `activityOptions`
sets one. So `invokeModel`, `invokeModelStreaming`,
`strandsInvokeStatefulModel` and `strandsInvokeStatefulModelStreaming`
heartbeat by default.

- The activities already heartbeat on a timer at half the timeout
(`autoHeartbeat`), so they don't change. A timeout you set wins, and
`heartbeatTimeout: 0` (or `'0s'`) turns heartbeats off.
- `strandsGetModelConfig` is now scheduled without a heartbeat timeout,
even when you set one. It builds the model and reads its config
synchronously, so a heartbeat timer can't fire until it returns, and a
model factory slower than the timeout would fail it.
- The README and the docs on `TemporalAgent`, `TemporalModel` and the
model activity said cancellation needs a `heartbeatTimeout` you set.
They now say what the default does and how to turn it off.

`activityAsTool` and `activityAsHook` run your own activities, so they
stay as they are.

## Why?

Without a heartbeat timeout, the model activities never heartbeat. So a
cancel never reaches a running model call: since #297 the model gets the
activity's cancellation signal, but the signal only aborts once the
worker hears about the cancel, and it only hears through a heartbeat.
With the default, the cancel stops models that pass `cancelSignal` to
their client, like `OpenAIModel` and `BedrockModel`. A lost worker also
goes unnoticed until the start-to-close timeout, 10 minutes by default.
#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,
including the histories recorded with no heartbeat timeout. But a model
call that didn't set `heartbeatTimeout` now heartbeats, which in
Temporal Cloud is at most one Action every 8 seconds of a running call.
And a Worker whose event loop blocks for about 10 seconds now fails the
call with a heartbeat timeout, and retries it.

`TemporalMCPClient` isn't in this PR. Its activities never heartbeat, so
a default there would fail every MCP call longer than 10 seconds. It
needs a timer first, in a later PR.

## Checklist

1. Closes #507

2. How was this tested:
- `TemporalAgent dispatches model.stream() through invokeModel activity`
now reads the heartbeat timeouts from History: none for
`strandsGetModelConfig`, 10 seconds for `invokeModel`. Without the
change it fails:
     ```
     Difference (- actual, + expected):
       [
         [
           'strandsGetModelConfig',
           0,
         ],
         [
           'invokeModel',
     -     0,
     +     10000,
         ],
       ]
     ```
- The two `cancelling a TemporalAgent workflow ... aborts the model
call` tests, whose agent sets `heartbeatTimeout: '5 seconds'`, now check
that the model call keeps those 5 seconds and the config read gets none.
Without the change, the config read got 5 seconds too.
- To check the opt-out, I ran a throwaway test that schedules one model
call with each setting and reads the activity's heartbeat timeout from
History and from `Context.current().info`:
     ```
heartbeatTimeout="default" scheduled invokeModel: heartbeatTimeout
{"seconds":"10"} (10000 ms); activity info heartbeatTimeoutMs=10000
heartbeatTimeout="undefined" scheduled invokeModel: heartbeatTimeout
{"seconds":"10"} (10000 ms); activity info heartbeatTimeoutMs=10000
heartbeatTimeout="0" scheduled invokeModel: heartbeatTimeout {} (0 ms);
activity info heartbeatTimeoutMs=0
heartbeatTimeout="0s" scheduled invokeModel: heartbeatTimeout {} (0 ms);
activity info heartbeatTimeoutMs=0
heartbeatTimeout="5 seconds" scheduled invokeModel: heartbeatTimeout
{"seconds":"5"} (5000 ms); activity info heartbeatTimeoutMs=5000
     ```
`autoHeartbeat` starts its timer only when `heartbeatTimeoutMs > 0`, so
`0` means no heartbeats.
- Locally, `make test` (105 passed, including the replay tests), `make
lint` and the conventions check pass, with Node 24.

3. Any docs updates needed?
The README and docs 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 17, 2026
## What was changed

`@temporalio/strands-agents` now requires `@strands-agents/sdk`
`^1.13.0` instead of `^1.3.0`, in both `peerDependencies` and
`devDependencies`. The lock file only changes the specifier; it still
installs 1.19.0.

## Why?

Since #297, the model activity passes its cancellation signal to the
model as `cancelSignal`, and `StreamOptions` only has that field from
Strands 1.13.0. Below that, the plugin doesn't compile:

```
src/model-activity.ts(54,49): error TS2353: Object literal may only specify known properties, and 'cancelSignal' does not exist in type 'StreamOptions'.
```

npm still accepts any 1.3.0+ next to the plugin, so a project on Strands
1.3 to 1.12 gets an install that doesn't match what the plugin expects.
CI never saw it: PR CI uses the lock (1.19.0) and the nightly uses the
newest version (1.25.0).

I found this while testing the GA-wave release candidates at their
lowest allowed versions. With clean builds, every Strands release from
1.13.0 to 1.19.0 passes all 139 tests, and 1.10.0, 1.11.x and 1.12.0
fail to compile.

## Checklist

1. Closes: no issue.

2. How was this tested: in `typescript/strands-agents`,
- with the committed lock (`@strands-agents/sdk` 1.19.0): `make lint`
passes and `make test` passes 139 tests.
- with `@strands-agents/sdk` pinned to exactly 1.13.0: `make test`
passes 139 tests.
   - `scripts/ci/check_conventions.py` passes.

3. Any docs updates needed? No. The README doesn't state a Strands
range.
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.

[Bug] strands-agents (TypeScript): OpenAIModel calls fail in the model Activity on Strands 1.15.0 and later

2 participants