Repository navigation
[typescript/openai-agents] breaking: report cancellation from model activities - #695
pcbeingused333 wants to merge 2 commits into
Conversation
…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.
| return events; | ||
| } catch (error) { | ||
| throw toModelInvocationFailure(error); | ||
| throw failureOrCancellation(error); |
There was a problem hiding this comment.
[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).
| function failureOrCancellation(error: unknown): unknown { | ||
| const signal = Context.current().cancellationSignal; | ||
| return signal.aborted ? signal.reason : toModelInvocationFailure(error); |
There was a problem hiding this comment.
[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.
|
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 This comment was drafted with Claude Code. |
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 recordActivityTaskCanceledwhere the Activity could previously recordActivityTaskCompleted, 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 runpnpm exec ava lib/__tests__/activity-cancellation.test.js lib/__tests__/streaming-activity.test.jsfrom the plugin directory.Disclosure
This change was prepared with assistance from an AI coding tool.