Repository navigation
Heartbeat Vercel AI SDK streamed model calls only on the timer - #516
Conversation
invokeModelStreaming heartbeated on every stream part, besides its timer. The SDK sends the first heartbeat at once, so even a short streamed call cost a heartbeat, which in Temporal Cloud is an Action. With heartbeatTimeout: 0, it still heartbeated on each part. Remove the per-part heartbeat. The timer already heartbeats every half of the timeout while the call runs, starting half the timeout in, so a call shorter than that sends none, and 0 turns heartbeats off. Part of #491.
| set one in the provider options. They heartbeat every half of that timeout while | ||
| they run, so a lost Worker is noticed, and a cancellation request reaches the | ||
| Activity, within about that time. A call shorter than half the timeout sends no |
There was a problem hiding this comment.
Could a reader take "that time" as half the timeout? Both limits are closer to the full timeout. A lost Worker is noticed one timeout after its last heartbeat. With the default, a cancellation request can wait about 8 seconds, because the SDK sends at most one heartbeat every 0.8 × the timeout. Your numbers in #491 show up to 7 seconds. I'd suggest "within about that timeout".
There was a problem hiding this comment.
Yes, it reads that way. I changed it to "within about that timeout".
| // The heartbeat timeout the plugin schedules by default. | ||
| const env = new MockActivityEnvironment({ heartbeatTimeoutMs: 10_000 }, { client: recordingClient(published) }); |
There was a problem hiding this comment.
You checked heartbeatTimeout: 0 by hand. Is it worth a test for it? On this instant stream it would add nothing, because the removed call never read the timeout. A stream that stalls for a short time would guard the early return in heartbeatUntilDisposed. I removed that return locally. A 350 ms stall with heartbeatTimeoutMs: 0 then sent 274 heartbeats, and the suite still passed. I'm OK with a follow-up for it.
There was a problem hiding this comment.
I added it here. The provider stalls for 100 ms with heartbeatTimeoutMs: 0, and the test checks that no heartbeat goes out.
To see it fail, I changed the guard to skip only an undefined timeout (just deleting the line does not compile). The test then fails:
Difference (- actual, + expected):
- 87
+ 0
"Within about that time" read as half the timeout, but a lost Worker is noticed one timeout after its last heartbeat, and a cancellation can wait up to about 0.8 of the timeout, because the SDK throttles heartbeats.
The early return in heartbeatUntilDisposed had no test. With a guard that only skips an undefined timeout, a 100 ms stall sends 87 heartbeats and the new test fails.
What was changed
invokeModelStreamingno longer heartbeats on every stream part. Its timer (heartbeatUntilDisposed) sends all its heartbeats, every half of the timeout, starting half the timeout in.heartbeatTimeout: 0, it sends no heartbeat at all. Before, it still heartbeated on each part.Why?
#491 sets one rule for every plugin: heartbeats come only from the timer. The per-part heartbeat broke it. The SDK sends the first heartbeat at once, so every streamed call cost a heartbeat, which in Temporal Cloud is an Action, even a call that ended in a second.
I checked it with a streamed call that sends 11 parts at once and then waits 3 seconds. 1.5 seconds in, I read the pending Activity from
DescribeWorkflowExecution:Does a slow stream still heartbeat in time? Yes. The timer doesn't wait for parts, and the slow-provider tests pass: a stream whose first part takes 2 seconds, with a 1-second heartbeat timeout, still completes.
Does this break anything? Not replay. Heartbeats happen inside the Activity, and History doesn't record them, so the Workflow's commands don't change.
Checklist
Closes [Feature Request] Heartbeat Vercel AI SDK streamed model calls only on the timer #515
How was this tested:
invokeModelStreaming publishes every part and assembles the final resultnow runs with the 10-second default heartbeat timeout and counts heartbeats. Its 12 parts send none. Without the change it fails:Model activity (<call>) heartbeats while the provider is slowtests pass with and without the change.invokeModelStreaming sends no heartbeat when heartbeatTimeout is 0stalls the provider for 100 ms withheartbeatTimeout: 0, and checks that no heartbeat goes out. If the timer's guard skips only an undefined timeout, it sends 87 and the test fails. Onmain, a 12-part stream withheartbeatTimeout: 0sends 12 heartbeats, one per part.make test(87 passed, 1 skipped),make lintand the conventions check pass, on Node 24 with Temporal TypeScript SDK 1.24.0.Any docs updates needed?
The README is updated here. Release notes can say that a streamed call shorter than half its heartbeat timeout no longer sends a heartbeat.