Repository navigation
fix(strands): give the model the activity's cancellation signal - #297
Merged
Merged
Conversation
campoy
force-pushed
the
fix/strands-typescript-model-cancel-signal-lost
branch
from
October 30, 2026 23:36
f6c47a9 to
7038d00
Compare
campoy
marked this pull request as ready for review
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
force-pushed
the
fix/strands-typescript-model-cancel-signal-lost
branch
from
October 31, 2026 01:52
3952aed to
ecc7667
Compare
Other tests on main use ModelActivity. Rebasing the commit that removed this PR's own copy of the import also removed main's.
brianstrauch
approved these changes
Nov 3, 2026
This was referenced Nov 12, 2026
Closed
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was changed
The model Activities give the model the Activity's own cancellation signal
(
Context.current().cancellationSignal) ascancelSignal, in place of the agent's.Strands puts the agent's
cancelSignal, anAbortSignal, in the model's stream options. It reachedthe Activity as a plain object (
{}). From@strands-agents/sdk1.15.0,OpenAIModelhands it to theOpenAI client, which calls
signal.addEventListener, so everyOpenAIModelcall through the pluginfailed 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 passescancelSignalto its client(in Strands 1.19.0,
OpenAIModelandBedrockModel) then stops the provider call, as cancelling theagent does natively.
AnthropicModel,GoogleModelandVercelModelignorecancelSignal, so theirprovider call keeps running and the Activity can still complete with its result. A remote Activity
learns of cancellation only through heartbeats, so without a
heartbeatTimeoutthe model call runs tocompletion. The README's new Cancellation section documents this, and how
TemporalAgentcancellationtypes behave.
Model clients abort with their own errors (OpenAI's
APIUserAbortErroris namedError), whichTemporal would record as an Activity failure. When the model throws after the Activity's signal
aborted, the Activity throws the signal's
CancelledFailureinstead, so the Activity ends ascancelled. One helper in
model-activity.tsdoes this forinvokeModelandinvokeModelStreaming. Workflow code and the Activity input do not change.Why?
See #296.
Compatibility
No version gate is needed, and nothing that works today breaks:
object without ever aborting (Bedrock's HTTP handler sets
onaborton it) now get a signal thataborts when the model Activity is cancelled with a
heartbeatTimeoutset. Natively, cancelling theagent aborts their call the same way.
OpenAIModelcalls; during a rollout they are retrieduntil an upgraded Worker takes them.
Checklist
Closes [Bug] strands-agents (TypeScript): OpenAIModel calls fail in the model Activity on Strands 1.15.0 and later #296
How was this tested:
invokeModel gives the model Message instances and an AbortSignal(the existing wire-datatest, extended) checks that the model gets an
AbortSignal. It fails onmain(
expected cancelSignal to be an AbortSignal).cancelling the workflow aborts the model call in invokeModel and cancels the workflow, andthe same for
invokeModelStreaming, runTemporalModel.streamon a real Worker withheartbeatTimeoutandWAIT_CANCELLATION_COMPLETED, cancel the Workflow mid-call, and use amodel that throws an
ErrornamedErroronce its signal aborts, as OpenAI's client does. Theycheck that the model's signal aborted and that the history has
ActivityTaskCanceled, noActivityTaskFailed, andWorkflowExecutionCanceled. Without theCancelledFailurerethrowboth 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 withWAIT_CANCELLATION_COMPLETED, runagent.invoke()on a real Workerwith
heartbeatTimeoutand cancel the Workflow mid-call. They check that the model's signalaborts while the Worker runs and that the history has
ActivityTaskCancelRequested,WorkflowExecutionCanceledand noWorkflowTaskFailed(plusActivityTaskCanceledwithWAIT_CANCELLATION_COMPLETED). WithTemporalAgent'sModelErrorunwrap disabled, both time outwith repeated
WorkflowTaskFailed.cancelling a TemporalAgent workflow with TRY_CANCEL while the model ignores its signal, and thesame with
WAIT_CANCELLATION_COMPLETED, use a model that keeps going after its signal aborts andreturns
late result. WithTRY_CANCELthe run is cancelled. WithWAIT_CANCELLATION_COMPLETEDthe run completes with
late result, and the history hasActivityTaskCompleted,WorkflowExecutionCompletedand noWorkflowExecutionCanceled.OpenAIModeland a recordingfetch, Responses and ChatCompletions: on
mainit fails with@strands-agents/sdk1.15.0, 1.19.0 and 1.22.0 and passeswith 1.14.0; with the signal change it passes on all four. Also with
@temporalio/*1.25.0 andStrands 1.22.0. (This ran before the
CancelledFailurerethrow was added.)cancelSignalwas taken from the locked Strands 1.19.0 sources: onlyOpenAIModelandBedrockModelpass it to their clients.make sync,make lint,make test(86 tests),make buildandcheck_conventions.pypass. The Node 22, macOS and Windows cells did not run locally.Any docs updates needed?
The README gains a Cancellation section with the
heartbeatTimeoutrequirement, the dependence onthe model honoring
cancelSignal, and howTemporalAgent'sTRY_CANCELandWAIT_CANCELLATION_COMPLETEDcancellation types differ, including that withWAIT_CANCELLATION_COMPLETEDa model Activity that completes after cancellation lets the runcomplete normally. The Models section's note from fix(strands): run stateful models with their server-side state (breaking) #285, which said
OpenAIModelcalls work onlybefore
@strands-agents/sdk1.15.0, is rewritten: such calls work with current releases, and onlya 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 throughTemporal (a cancellation scope or a Workflow cancel).
cancelSignal(in Strands 1.19.0,AnthropicModel,GoogleModelandVercelModel) are not aborted, and their Activity can complete with the model's result aftercancellation. With
WAIT_CANCELLATION_COMPLETED, the agent then continues with that result and therun can complete instead of being cancelled.