Skip to content

[typescript/openai-agents] breaking: report cancellation from model activities - #695

Open
pcbeingused333 wants to merge 2 commits into
temporalio:mainfrom
pcbeingused333:fix/openai-agents-cancel-signal
Open

pcbeingused333 wants to merge 2 commits into
temporalio:mainfrom
pcbeingused333:fix/openai-agents-cancel-signal

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #503

A cancelled model Activity could keep the provider call running and report success. The OpenAI Responses stream can end normally after aborting its HTTP request, so handling thrown abort errors alone can still let the Activity return partial events as a successful result.

What changed

Pass the Activity's cancellation signal to the provider and check it before returning a model response or a collected stream. If the signal has aborted, surface Temporal's CancelledFailure. Errors from calls whose signal has not aborted retain their existing failure classification. Live subscribers may already have received partial events.

Breaking change and rollout

With WAIT_CANCELLATION_COMPLETED, Workflows observe the Activity's terminal outcome. An affected cancellation can now record ActivityTaskCanceled where the Activity could previously record ActivityTaskCompleted, changing which path the Workflow takes. Let affected in-flight runs finish on their existing Workers before upgrading, or keep them pinned to those Workers with Worker Versioning.

Tests

The new regression case yields one text delta, cancels the Activity, and lets the iterator return normally. It expects CancelledFailure. The existing tests cover providers that throw after aborting. To run both paths, build the plugin and run pnpm exec ava lib/__tests__/activity-cancellation.test.js lib/__tests__/streaming-activity.test.js from the plugin directory.

Disclosure

This change was prepared with assistance from an AI coding tool.

…cancelled

invokeModelActivity and invokeModelStreamActivity built the ModelRequest
without a signal, so cancelling the Activity left the model call running.
Pass the Activity cancellation signal as request.signal and, once it has
aborted, throw its CancelledFailure instead of wrapping the client's abort
error as a retryable ModelInvocationError.
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 25, 2026 02:24

@temporal-factory temporal-factory Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Both added tests pass, but they only cover providers that throw on abort. The supported OpenAI Responses HTTP stream can instead close normally on abort, causing the activity to report success with a partial result.

Comment on lines 255 to +257
return events;
} catch (error) {
throw toModelInvocationFailure(error);
throw failureOrCancellation(error);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Report cancellation when the OpenAI stream closes without throwing

When cancellation arrives after the OpenAI Responses HTTP stream has started, the OpenAI client can swallow the transport AbortError and end iteration normally. I reproduced this with the locked OpenAI provider and a mocked SSE response: the activity resolved with partial events and no response_done, because failureOrCancellation only runs on a throw. Temporal can then record a successful activity with a truncated result instead of a cancellation. Check the activity signal before returning the events, and cover normal iterator termination on abort in the regression test (AGENTS.md:71).

Comment on lines +174 to +176
function failureOrCancellation(error: unknown): unknown {
const signal = Context.current().cancellationSignal;
return signal.aborted ? signal.reason : toModelInvocationFailure(error);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Document the cancellation outcome change for in-flight runs

With WAIT_CANCELLATION_COMPLETED, a workflow observes the activity's actual outcome. A model call that previously ignored activity cancellation and returned a result can now record ActivityTaskCanceled instead of ActivityTaskCompleted, changing which branch an in-flight workflow takes. The PR describes the new behavior but neither gates it nor marks it as breaking with rollout guidance for affected runs, as required by AGENTS.md:75.

@pcbeingused333 pcbeingused333 changed the title [typescript/openai-agents] abort the model call when the Activity is cancelled [typescript/openai-agents] breaking: report cancellation from model activities Nov 29, 2026
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

The follow-up checks the Activity cancellation signal before returning a model result, including when the provider ends the stream normally after yielding a partial event. It adds a regression case for that exit path and documents the history change and rollout guidance for workflows using WAIT_CANCELLATION_COMPLETED.

This comment was drafted with Claude Code.

This branch has not been deployed

No deployments
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] openai-agents and vercel-ai-sdk (TypeScript): a cancelled model Activity doesn't stop the model call

1 participant