Repository navigation
fix(ai): chunkTimeout counts model output, not keep-alives - #64492
adamdotdevin wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Looks good. On the base branch, the new keep-alive/ping transport test hangs and the AI SDK heartbeat test now fails. With this PR both pass, as do the full packages/ai suite and type checks for ai and core.
Moving the chunk timer after framing is the right layer: anything framing drops (comments, empty events, data: null, keepalives, unlisted event names) can no longer count as progress, and the whole-request timer still runs on the raw bytes. The document framing side effect (the chunk timeout now covers the whole body download) is fine with the 15-minute default.
Public API is unchanged, apart from one behaviour change: with includeRaw, a named Anthropic event: ping no longer appears as a raw event.
This fixes the keep-alive half of #61346. The other half, silent compaction retries, is not covered, so that issue should stay open.
Related: #61346
ecd2eb2 to
a9a4f2e
Compare
There was a problem hiding this comment.
The keep-alive/ping fix works: the new transport test hangs on the base branch and passes here. The full packages/ai suite, core aisdk.test.ts and type checks for both packages also pass. The rebased version adds a new hang, though (see the inline comment).
Turning off the executor's chunk limit for model routes also removes the only limit on reading an HTTP ≥400 body. A gateway that returns 503 headers and then stalls on the body now hangs forever when no whole-request timeout is set, which is the default. I checked this with a TestClock test that advances 20 minutes: on the base branch it fails with "Timed out waiting for response data"; with this PR it never finishes.
Public API unchanged.
| const response = yield* RequestExecutor.withTimeout( | ||
| RequestExecutor.withTls(runtime.http.execute, request.http?.tls), | ||
| request.timeout, | ||
| { ...limits, chunk: false }, |
There was a problem hiding this comment.
With chunk: false, nothing limits statusError's response.text read on an HTTP ≥400 response any more. The frame timer below only runs on success bodies. A provider or gateway that sends 503 headers and then stalls on the body hangs forever unless a total timeout is set. On the base branch this failed after 15 minutes with "Timed out waiting for response data". I reproduced it with a TestClock test: a 503 whose body sends a few bytes and then stalls, clock advanced 20 minutes. One fix is to keep the byte-level chunk limit for error statuses, or to have the executor take the frame-level chunk limit only for 2xx bodies.
There was a problem hiding this comment.
Fixed in 8b445e0: the executor keeps its byte-level chunk limit for status ≥ 400 bodies even when the caller passes chunk: false to time frames itself, since statusError reads those whole. Added a TestClock test: a 503 whose body stalls now fails after 15 minutes with the 503, and it hangs without the fix.
There was a problem hiding this comment.
I couldn't verify this: 8b445e0 isn't on the PR. pr/head is still a9a4f2e17 from 16:49 CST, my checkout doesn't have that commit, and I couldn't fetch from origin. It looks like it hasn't been pushed yet. In the code I have, executeOnce in packages/ai/src/route/executor.ts (around lines 280–285) still passes the same chunk limit to withBodyTimeouts whatever the status, so the 503 body read still has no limit.
The approach you describe is right. Choosing the limit from raw.status after headers arrive is the correct place, because statusError reads ≥ 400 bodies in full and the frame timer never sees them. Once it's pushed, check one case. http.ts sends { ...limits, chunk: false } both when it times frames itself and when the user turned the chunk timeout off. If the executor turns false back into the 15-minute default for ≥ 400, a user who disabled chunkTimeout would still get that limit on error bodies. To avoid this, have http.ts pass the user's original chunk value separately (or as a flag) and use that for ≥ 400 bodies. Or decide that this case doesn't matter.
I'll check the diff and run the new TestClock test once the commit shows up on pr/head. I made no changes to the checkout. The bun.lock modification there isn't mine.
There was a problem hiding this comment.
Pushed now: 8b445e0d0a is on the branch. The first push was blocked by the pre-push typecheck, which passed once dependencies were refreshed after the rebase.
On a user who turned chunkTimeout off: I'm treating that case as not worth the extra plumbing. statusError reads an error body whole just to build the error message, so an error body that never finishes would hang the call forever, which nobody turning off the stream timeout means. Error bodies always get the 15-minute byte limit, and success bodies honor false as before.
There was a problem hiding this comment.
Confirmed, 8b445e0d0 is on pr/head. The change in executor.ts (lines 282–286) fixes the stalled 503: when the status is ≥ 400 and the chunk limit is false, it now falls back to DEFAULT_CHUNK_TIMEOUT_MS. Success bodies behave as before. packages/ai/test/http-timeout.test.ts passes locally, 11 of 11 including the new 503 test. I didn't revert the fix to see the test hang, and I didn't rerun the full suite or the typecheck.
I'm fine with your call on users who turn chunkTimeout off. One related case you may not have intended: a user who sets a shorter chunkTimeout, say 30s, also gets 15 minutes on error bodies on model routes. That's because http.ts always passes chunk: false, so the executor never sees the user's number. It's no worse than before this PR's first commit for anything a user would notice, and fixing it needs the same plumbing you've decided against, so I'd leave it.
I made no changes to the checkout. The modified bun.lock there isn't mine.
Issue for this PR
No issue filed.
Type of change
What does this PR do?
A streamed
anthropic-messagescall through Zen went silent partway through a tool call for about 30 minutes and never errored. The model had stopped producing output, but the connection kept sending: keep-alivecomments and Anthropicpingevents, and those resetchunkTimeout:RequestExecutorwraps the raw response body in the chunk timer (withBodyTimeoutsinpackages/ai/src/route/executor.ts), so any bytes reset it, comments included.wrapSSEalready ignores comments, but every SSE event resets the deadline,pingincluded (packages/core/src/aisdk.ts:68).Fix:
chunkTimeoutnow resets only on events that carry model output.http.ts: model routes send withchunk: false, so the executor skips its byte-level chunk timer, and run the chunk timer on the framed stream instead. SSE framing already drops comments, empty events and keepalives. The header timeout and whole-request deadline stay in the executor, and every other request keeps the byte-level chunk timer. When the chunk timer fires, it raises a retryable readTransportErrorwith codeTimeout, as before. The default doesn't change.anthropic-messages.ts:pingis no longer one of the SSE event names the framing keeps, so it never becomes a frame.aisdk.ts:pingevents don't move the deadline.Side effects:
documentand the other non-SSE framings (lines, Bedrock event stream) now bound the time between frames, not between byte reads. Fordocument, the chunk timeout covers downloading the whole body.includeRaw, a namedevent: pingno longer shows up as a raw event.How did you verify your code works?
New tests:
packages/ai/test/http-timeout.test.ts:: keep-aliveandpingpast 15 minutes now fails with the read timeout.packages/core/test/aisdk.test.ts: the heartbeat test now also sendspingevents and still has to time out.Each new test fails when the fix it covers is reverted.
Also ran:
packages/ai:bun run test(2267 pass, 0 fail) andbun run typecheckpackages/core:aisdk.test.ts,session-model-transport.test.tsandbun run typecheckScreenshots / recordings
Not a UI change.
Checklist
Added by the opencode bot (AI-generated).
Related: #61346