Skip to content

fix(ai): bound native stream stalls on framed events - #52866

Open
felixzsh wants to merge 1 commit into
anomalyco:v2from
felixzsh:framed-stall-timeout
Open

felixzsh wants to merge 1 commit into
anomalyco:v2from
felixzsh:framed-stall-timeout

Conversation

@felixzsh

@felixzsh felixzsh commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Related: #43618 (merged, AI SDK path) and #51879 (open, v1 + SDK path). This fixes the remaining native @opencode/ai HTTP transport, which still measured the stall on raw bytes after #49229.

Issue for this PR

Closes #43519

Type of change

  • Bug fix

What does this PR do?

chunkTimeout was applied to the raw response byte stream before framing in
packages/ai/src/route/transport/http.ts. Framing.sse drops SSE comments
(: keepalive), empty events and data: null, but the stall timer reset on any
byte, so a provider or proxy that keeps a stalled generation warm with comment
heartbeats never tripped the timeout (#43519).

This moves the timeout onto the framed stream so it resets on emitted events
only. A keepalive produces no frame and no longer counts as progress, so the
request fails with the existing Transport { operation: "read", code: "Timeout" } error and follows the retry policy. The AI SDK path already worked
this way (wrapSSE in packages/core/src/aisdk.ts resets only on parsed
events); this makes the native path match it.

Why it works: Stream.timeoutOrElse arms its deadline per pull, and framing
consumes and drops non-event bytes within a single pull without emitting an
empty chunk, so dropped bytes cannot reset the deadline.

How did you verify your code works?

  • Added ignores SSE comment keepalives when bounding a stalled stream to
    packages/ai/test/http-timeout.test.ts: a server sends one content chunk,
    then a : keepalive comment 10s later; with chunkTimeout: 20_000 the
    request must fail with the read timeout. Against the current v2 http.ts
    the test times out instead of failing, so it is a real regression test.
  • bun typecheck in packages/ai: passes.
  • bun test in packages/ai: 1730 pass, 28 skip, 0 fail.

Screenshots / recordings

N/A (no UI change).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The HTTP transport applied the chunk timeout to the raw response byte
stream before framing. Framing drops SSE comment keepalives and other
empty events, so any such byte reset the stall timer and a provider could
hold a stalled generation warm forever (anomalyco#43519). This matched the native
path but not the AI SDK path, whose wrapSSE resets only on parsed events.

Bound the timeout on framed progress instead. A stream that only sends
keepalives now still trips the chunk timeout, while real events keep
resetting it.
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant