Skip to content

fix(gateway): give the stream phase its own budget - #137

Open
SK-DEV-AI wants to merge 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:fix/stream-phase-budget
Open

SK-DEV-AI wants to merge 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:fix/stream-phase-budget

Conversation

@SK-DEV-AI

Copy link
Copy Markdown
Contributor

What

retry.timeout_seconds bounded the whole downstream stream, not just the request. handleInference built the upstream HTTP request from requestCtx, so when the budget expired mid-generation the upstream connection was torn down.

A long thinking turn (xhigh/max reasoning, large context) legitimately runs for many minutes. The client saw a truncated stream carrying the Go error text context deadline exceeded — which reads as a context-limit failure rather than a timeout, and is why this went unreported for so long.

New retry.stream_timeout_seconds bounds the stream phase only.

Why the split is safe

  • The attempt phase keeps its own bounds: attempt_timeout_seconds (dial + write + headers + first event), ResponseHeaderTimeout, and the connect timeout. A wedged upstream still cannot hold a connection open.
  • The upstream request carries the stream context, so the connection survives past the request budget.
  • The stream context derives from the client context, not from requestCtx, so a client disconnect still tears the stream down immediately.
  • 0 keeps the historical behavior: the request-level timeout also bounds the stream.

Evidence

Measured on a live deployment with retry.timeout_seconds: 300:

  • 15 stream_error outcomes, 11 of them within 40ms of exactly 300s, all on minimax-m3.1-flash-free (1M context, xhigh/max effort).
  • 11 further turns reported 200 with duration_ms pinned at exactly 300s — truncated streams that looked successful.

Upstream silence during the thinking phase is short (measured 8-56s), so the 300s wall was pure budget, not a slow model.

Tests

internal/gateway/stream_budget_test.go:

  • a stream outlives the request budget when a stream budget is configured;
  • the request budget still bounds the stream when no stream budget is set (historical behavior preserved);
  • a client disconnect still ends the stream immediately.

Reverting the one-line change that carries the stream context into the upstream request fails the first test.

Config

{ "retry": { "max_attempts": 6, "timeout_seconds": 300, "stream_timeout_seconds": 1800 } }

stream_timeout_seconds is opt-in: omit it and nothing changes.

The request-level budget (retry.timeout_seconds) also bounded the whole
downstream stream: handleInference built the upstream HTTP request from
requestCtx, so when the budget expired mid-generation the upstream connection
was torn down.

A long thinking turn (xhigh/max reasoning, large context) legitimately runs for
many minutes, and the client saw a truncated stream carrying the Go error text
"context deadline exceeded" — which reads as a context-limit failure rather
than a timeout. Measured on a live deployment: 15 stream errors, 11 of them
within 40ms of exactly the 300s budget, plus 11 "successful" 200s also pinned
at exactly 300s (truncated streams reported as success).

retry.stream_timeout_seconds now bounds the stream phase only:

- The attempt phase keeps its own bounds (attempt_timeout_seconds,
  ResponseHeaderTimeout, connect timeout), so a wedged upstream still cannot
  hold a connection open.
- The upstream request carries the stream context, so the connection survives
  past the request budget.
- The stream context derives from the CLIENT context, so a client disconnect
  still tears the stream down immediately.
- 0 keeps the historical behavior: the request-level timeout also bounds the
  stream.

Tests: a stream outlives the request budget when the budget is configured; the
request budget still bounds the stream when it is not; a client disconnect
still ends the stream immediately. Reverting the one-line change that carries
the stream context fails the first test.
Suydev added a commit to Suydev/opencode2api that referenced this pull request Nov 13, 2026
Suydev added a commit to Suydev/opencode2api that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant