Skip to content

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

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

felixzsh wants to merge 1 commit into
anomalyco:devfrom
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:

BigBrotherHui added a commit to BigBrotherHui/opencode that referenced this pull request Oct 10, 2026
fix(ai): bound native stream stalls on framed events

Move chunkTimeout from the raw byte stream to the framed stream so SSE
comment keepalives no longer reset the deadline and mask a stalled
generation. Aligns the native @opencode/ai transport with the AI SDK
path, which already resets only on parsed events.

Closes anomalyco#43519
@thdxr
thdxr changed the base branch from v2 to dev October 10, 2026 19:28
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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.

chunkTimeout resets on SSE comment heartbeats — keepalive-warm stalled streams never time out

1 participant