Skip to content

fix(vercel-ai-sdk): heartbeat while the model Activities wait on the provider - #465

Merged
moetemp merged 1 commit into
mainfrom
fix/vercel-ai-sdk-stream-heartbeat
Nov 12, 2026
Merged

moetemp merged 1 commit into
mainfrom
fix/vercel-ai-sdk-stream-heartbeat

Conversation

@moetemp

@moetemp moetemp commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What changed?

This PR makes the three model Activities heartbeat on a timer.

  • invokeModel, invokeModelStreaming and invokeEmbeddingModel heartbeat at half the heartbeatTimeout while they wait on the provider. Without a heartbeatTimeout they send nothing.
  • The README says what a subscriber sees when invokeModelStreaming is retried.

Fixes #AI-947

Why?

Only invokeModelStreaming sent heartbeats, and only when a stream part arrived. A heartbeatTimeout shorter than the time to the first token failed a healthy call. A cancellation request also didn't reach these Activities.

The interval is the one openai-agents, google-adk and strands-agents already use. Recorded histories replay the same.

How did you test it?

  • Unit tests. A slow model with heartbeatTimeout: '1 second' fails on main with a heartbeat timeout, and passes here.
  • make lint and make test pass in typescript/vercel-ai-sdk.
  • Not run against a real provider.

A healthy provider call that took longer than heartbeatTimeout failed with a heartbeat timeout, and a cancellation request could not reach the Activity.
@moetemp
moetemp merged commit ef48c3b into main Nov 12, 2026
27 checks passed
@moetemp
moetemp deleted the fix/vercel-ai-sdk-stream-heartbeat branch November 12, 2026 06:46
campoy added a commit that referenced this pull request Nov 13, 2026
…tbeat timeout (#510)

## What was changed

`TemporalLanguageModel` and `TemporalEmbeddingModel` now give their
Activities (`invokeModel`, `invokeModelStreaming` and
`invokeEmbeddingModel`) a 10-second `heartbeatTimeout` when the options
don't set one. `TemporalProvider` builds its models with its `default`,
`languageModel` and `embeddingModel` options, so it gets the default
too.

- The Activities already heartbeat on a timer at half the timeout
(#465), so they don't change.
- A `heartbeatTimeout` you set wins, and `heartbeatTimeout: 0` turns it
off. The default is applied with `??`, so a 0 passes through, and
Temporal schedules it as no heartbeat timeout.
- The README said the Activities send no heartbeat without a
`heartbeatTimeout`. It now gives the default and how to turn it off.

The MCP Activities don't heartbeat yet, so they still get no heartbeat
timeout.

## Why?

Without a heartbeat timeout, these Activities never heartbeat while they
wait on the provider. So a cancel never reaches the call, and a lost
Worker goes unnoticed until the 10-minute 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. I recorded a History for
each of the three Activities without this change, and all three replay
with it. But a 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 is blocked for about 10
seconds now fails the call with a heartbeat timeout, and retries it.

This goes well with #470, which passes the Activity's cancellation
signal to the model. Without a heartbeat timeout, that signal never
fires while the call waits on the provider.

This PR and #469 both change the `proxyActivities` options in
`TemporalEmbeddingModel.doEmbed`. Whichever merges second keeps both.

## Checklist

1. Closes #509

2. How was this tested:
- `Model activity (<call>) heartbeats while the provider is slow` now
runs each call three times and reads the Activity's heartbeat timeout
from History: 1 second when set (which passes only if the Activity
heartbeats, since each call takes 2 seconds), 10 seconds by default, and
none with `0`. Without the change, all three tests fail on the default:
     ```
     Difference (- actual, + expected):

       [
     -   undefined,
     +   10000,
       ]
     ```
- With `||` in place of `??`, the `0` case gets 10 seconds and fails, so
the test covers the opt-out too.
- Locally, `make test` (86 passed, 1 skipped), `make lint` and the
conventions check pass, on Node 24.

3. Any docs updates needed?
The README is updated here. Release notes should mention the new
default, and how to turn it off.
moetemp added a commit that referenced this pull request Nov 13, 2026
…al aborts (#470)

### What changed?

This PR makes a caller `abortSignal` work.

- Workflow side. The signal isn't sent to the Activity. The Activity
call runs in a `CancellationScope`, and an abort cancels it. A signal
that is already aborted schedules no Activity.
- Activity side. The three model Activities pass the Activity
cancellation signal to the model as `abortSignal`.

Fixes #AI-952

### Why?

An `AbortSignal` doesn't survive the JSON payload converter. It arrived
in the Activity as `{}`, and a real provider fails on that on every
attempt. The provider also never got a signal, so cancellation didn't
stop the provider call.

A Workflow without a signal schedules the same Activity inputs as
before.

The cancellation is behind a Workflow patch. An execution from an
earlier version replays as it ran then, when an abort sent no command.
The new behavior starts with its first model call that isn't a replay. A
call without a signal records no patch marker.

Two limits stay. The AI SDK `timeout` option still doesn't work in the
sandbox, so the README says to use `startToCloseTimeout`. And a running
Activity learns about cancellation only through heartbeats, which #465
adds.

### How did you test it?

- [x] Unit tests. They cover a signal that never aborts, one that is
already aborted, and an abort during `generateText` and `streamText`.
- [x] Histories recorded on `main` replay on this branch.
- [x] A history recorded on the old behavior, where the signal aborts
while `invokeEmbeddingModel` runs, replays on this branch. It fails to
replay without the patch gate.
- [x] `make lint` and `make test` pass in `typescript/vercel-ai-sdk`.
- [ ] I didn't test `firstChunkMs` and `chunkMs` of `streamText`.
moetemp added a commit that referenced this pull request Nov 13, 2026
### What changed?

This PR adds a "Plugin contract" section to `AGENTS.md` and a shared
test helper for payload round trips.

- `findPayloadChange(value)` sends a value through the default payload
converter and returns the first field that changed.
- The helper is in `typescript/_template` and the five TypeScript
plugins. `check_conventions.py` keeps the copies equal.
- `vercel-ai-sdk` gets a contract test for its Activity input and
results.
- The contract has six rules for plugins that run model or tool calls in
Activities.

This branch contains #465 to #470, because the contract test fails
without them. Those should merge first.

Fixes #AI-953

### Why?

The review of `@temporalio/vercel-ai-sdk` 2.0.0 found six defects. Three
had one cause. A value changed between the Workflow and the Activity,
and no test sent a real value through the converter. Each rule in the
contract comes from one of the six.

### How did you test it?

- [x] `make lint` and `make test` pass in `vercel-ai-sdk`, `langsmith`,
`openai-agents` and `strands-agents`.
- [x] `python3 scripts/ci/check_conventions.py` passes.
- [ ] `make test` in `google-adk` fails on my machine in the
`strict-layout` cleanup hook. It fails the same way on `main`.
- [ ] The four other plugins only get the helper. There's no Python
helper yet.
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