Repository navigation
[typescript/google-adk] heartbeat streamed model calls only on the timer - #518
Conversation
adk-invokeModelStreaming heartbeated after every chunk, on top of the timer at half the heartbeat timeout. The SDK sends a call's first heartbeat at once, so even a short streamed call cost a Temporal Cloud Action, and the timer already keeps a slow stream alive. Drop the per-chunk heartbeat. Now a call shorter than half the timeout sends no heartbeat, and heartbeatTimeout: 0 turns heartbeats off completely. Part of #491.
The guard in startAdaptiveHeartbeat had no test: with it skipping only an undefined timeout, the whole suite still passed. The heartbeat test now runs a 100 ms streamed call with a 10-second timeout and with 0. With that weaker guard, the 0 case sends 77 heartbeats and fails.
There was a problem hiding this comment.
This PR looks right to me for what it sets out to do.
One thing I think the per-chunk heartbeat did add: the first chunk's heartbeat went out at once, so a cancel requested before it reached a short streamed call. Now it can't. Questions inline about that, about heartbeatTimeout: 0, and about a positive case for the test.
| @@ -131,8 +131,6 @@ export function createModelActivities(options: ModelActivitiesOptions = {}): Mod | |||
| const model = resolveModel(args.model); | |||
| const request = fromWireRequest(args.request); | |||
| for await (const response of model.generateContentAsync(request, true, abortSignal)) { | |||
There was a problem hiding this comment.
The first chunk's heartbeat went out at once, so a cancel requested before it, say from a failed sibling node, stopped the call there. Now a streamed call shorter than half the timeout can't be cancelled: 15 seconds with the README's streaming example. Is that an accepted cost, and should the README say so?
There was a problem hiding this comment.
yeah, I think that's a fair cost and it's the same across all plugins
| call shorter than half the timeout sends none. A cancel reaches a running call | ||
| only through a heartbeat. In Temporal Cloud each heartbeat that reaches the | ||
| service is an Action: with the default, at most one every 8 seconds of a running | ||
| call. Set `heartbeatTimeout: 0` to turn heartbeats off. The bound on a stalled |
There was a problem hiding this comment.
With 0, a streamed call used to learn of a cancel at a chunk, and now nothing delivers one, so WAIT_CANCELLATION_COMPLETED waits for the whole call. Could this say that turning heartbeats off also turns cancels off for a running call?
There was a problem hiding this comment.
Good call, done. It now says that with heartbeatTimeout: 0 a cancel never reaches a running call. The TemporalModelOptions.activity JSDoc also says WAIT_CANCELLATION_COMPLETED then waits until the call ends.
| })) as unknown as typeof WorkflowStreamClient.fromWithinActivity; | ||
| try { | ||
| const activities = createModelActivities({ modelProvider: (model) => new SlowStreamingLlm({ model }) }); | ||
| for (const heartbeatTimeoutMs of [10_000, 0]) { |
There was a problem hiding this comment.
Both cases expect zero, so the test would also pass with the timer removed from the streaming Activity. Could it add a short timeout, say 100 ms, and expect at least one heartbeat? That would also make the SlowStreamingLlm comment true for a working timer.
There was a problem hiding this comment.
Good catch: no other test covered that timer. The test now also runs a 100 ms timeout and expects a heartbeat. With the timer removed from the streaming Activity, it fails:
heartbeatTimeoutMs 100
Difference (- actual, + expected):
- false
+ true
A cancel reaches a running call only through a heartbeat. So it can't stop a call shorter than half the timeout, and with heartbeatTimeout: 0 it never reaches a running call. The README and the TemporalModelOptions.activity JSDoc now say so. The streaming example drops its 30-second heartbeat timeout and uses the 10-second default, so a cancel arrives 5 seconds into a call, not 15. The heartbeat test now also runs a 100 ms timeout and expects a heartbeat. Before, every case expected none, so it passed with the timer removed from the streaming Activity.
What was changed
adk-invokeModelStreamingno longer heartbeats after every chunk. Like every other Activity in the plugin, it heartbeats only on the timer at half the heartbeat timeout.TemporalModelOptions.activityJSDoc and the README said a streamed call also heartbeats per chunk. Now they say it doesn't, and thatheartbeatTimeout: 0turns heartbeats off, not just the timer, so a cancel never reaches a running call.startAdaptiveHeartbeatnow says it's the only source of heartbeats, so a call shorter than half the timeout sends none.Why?
#491 sets one rule for every plugin: heartbeats come only from the timer, and the first one goes out half the timeout into a call. Streamed model calls here broke it.
What did that cost? The SDK sends a call's first heartbeat at once, so every streamed call that sent a chunk cost a Temporal Cloud Action, even one that ended in a second. The timer already keeps a slow stream alive, so the per-chunk heartbeats added nothing. Now, with the 10-second default, a streamed call shorter than 5 seconds sends no heartbeat.
Is there a downside? Yes, a small one. A cancel reaches a call only through a heartbeat, so a cancel that came in before the first chunk used to stop the call there. Now it waits for the timer, like a non-streamed call always has. After that first chunk, the SDK held the next heartbeat for 0.8 × the timeout anyway.
Does this affect replay? No. Heartbeats come from the Activity, not the Workflow, so replay never sees them. The replay tests pass.
Checklist
Closes [Feature Request] Heartbeat Google ADK TypeScript streamed model calls only on the timer #517
How was this tested:
streamedCallHeartbeatsOnlyOnTimerinstreaming.test.ts: it runs a three-chunk streamed call that takes 100 ms in aMockActivityEnvironmentand checks for heartbeats. A 10-second heartbeat timeout and 0 should send none, and 100 ms should send at least one. I added a new test because History doesn't record heartbeats, so the end-to-end streaming tests can't count them, and the other mock tests cover failures. With the per-chunk heartbeat back, the 10-second case fails:- false,+ true). No other test covers that timer. And if the timer's guard skips only an undefined timeout, the 0 case fails.make test(254 passed) andmake lintpass, on Node 22.23.4 with Temporal CLI 1.10.1. Before the review fixes, the conventions check also passed, on Node 24 with Temporal CLI 1.10.0.Any docs updates needed?
The JSDoc and the README are updated here. Release notes should say that streamed model calls no longer heartbeat per chunk.