Skip to content

Fail over on errors at the start of a stream; pause upstreams by the reason they give - #239

Merged
fylorn merged 2 commits into
mainfrom
feat/stream-start-failover
Sep 29, 2026
Merged

fylorn merged 2 commits into
mainfrom
feat/stream-start-failover

Conversation

@fylorn

@fylorn fylorn commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

What

Errors at the start of a stream fail over. A streamed answer is held until its first content arrives (at most failover.stream_start_wait_secs, default 15 s, and 1 MiB). An error reported in the stream before that point moves the request to the next candidate, just like an error status would:

Upstream format Recognised before content
Anthropic event: error (overloaded_error → 529, rate_limit_error → 429, …); message_start/ping are preamble
Responses / Codex backend response.failed, error; response.created/in_progress/codex.* are preamble
Chat chunks with error; a role-only first chunk is preamble
Gemini chunks with error (its HTTP code)
Bedrock eventstream exceptions (throttlingException → 429, …)

The bytes read while waiting are replayed in front of the rest of the stream, so a served answer reaches the client byte for byte. Anything unrecognised counts as content (never withhold a good answer). The last candidate is not held.

Failures are classified by the reason the upstream gives (tw_gateway::failure):

  • 401/403, 402 and 404 now fail over; 400/422 only when the body names a balance, a used-up quota or an unavailable model. Other 4xx still reach the client untouched.
  • Insufficient balance → paused no_balance_pause_secs (1800). Used-up quota → paused until the reset time (from the body resets_at/resets_in_seconds, the quota headers, or the GLM 429), else quota_pause_secs (3600). Rate limit with Retry-After (or Gemini retryDelay) → that long, at most rate_limit_max_pause_secs (3600). A missing model moves on without counting against the upstream.
  • Failures without a reason keep the consecutive-failure breaker (failures_to_pause, 3); the pause starts at pause_secs (60) and doubles on each re-open up to max_pause_secs (600), back to the start after a success.
  • The last candidate's own 4xx now reaches the client as the upstream wrote it (e.g. invalid x-api-key) instead of our paraphrase. 5xx/429 on the last candidate are unchanged.

The two hard rules in health still hold: a single candidate bypasses pausing, and when every candidate is paused they are all tried (fail-open).

Config / API

  • New failover section in config.yaml; defaults stay out of the file, out-of-range values are refused (config.failover_range). Documented in docs/config.md / docs/config.zh-CN.md (generated tables).
  • Overview.failover (FailoverView) — CONTROL_API_VERSION 30 (shared with Estimate count_tokens locally; resend without reasoning sealed by another account #238, same release). Lite edits it with PATCH /config like retention.
  • New message codes: config.failover_range, gw.upstream.stream_opening_error.
  • tw-breaker is untouched: the per-upstream cooldown is passed as a Policy per call.

Tests

Unit tests for classification, the stream-opening judge (per format, split chunks, byte-for-byte replay) and the pause policy (doubling, ceiling, reset, stated reasons, settings from config); end-to-end tests for overloaded-at-start failover, no retry once content has started, 402 pausing the upstream at once, and the last upstream's 401 reaching the client; control-plane test for the overview and PATCH.

🤖 Generated with Claude Code

@fylorn
fylorn force-pushed the feat/stream-start-failover branch from d8ca9ff to 77d577c Compare September 29, 2026 15:28
fylorn and others added 2 commits September 29, 2026 23:46
…streams by the reason they give

A streamed answer is now held until its first content arrives (at most
failover.stream_start_wait_secs, 1 MiB). An error the upstream reports in
the stream before that point (Anthropic overloaded_error, Codex
response.failed, Bedrock throttlingException, Chat/Gemini error chunks)
moves the request to the next candidate, the same as an error status. The
bytes read while waiting are handed on unchanged.

Failures are classified by what the upstream says (new `failure` module):
- 401/403 (credentials), 402 (balance) and 404 (no such model) now fail
  over; 400/422 only when the body names a balance, a used-up quota or an
  unavailable model. Other 4xx still go straight to the client.
- An insufficient balance, a used-up quota (until the reset time from the
  body, the quota headers or the GLM 429) and a rate limit with
  Retry-After set the upstream aside at once for that long. Failures
  without a reason keep the consecutive-failure breaker, with the pause
  doubling on each re-open up to a ceiling and resetting on success.
- The last candidate's own 4xx reaches the client as the upstream wrote it.

All durations are configurable in a new `failover` section (defaults are
not written to the file), exposed as Overview.failover (CONTROL_API_VERSION
29) and documented in docs/config*.md.

New message codes: config.failover_range, gw.upstream.stream_opening_error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gateway holds a stream until its first content; a mock that stops right
after message_start never reaches the client at all, which is not what the
cancellation check is about. Real upstreams send the thinking block's start
right away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn force-pushed the feat/stream-start-failover branch from 77d577c to e9cafcc Compare September 29, 2026 15:51
@fylorn
fylorn merged commit e2a54f8 into main Sep 29, 2026
4 checks passed
@fylorn
fylorn deleted the feat/stream-start-failover branch September 29, 2026 16:02
@fylorn fylorn mentioned this pull request Sep 29, 2026
fylorn added a commit that referenced this pull request Sep 29, 2026
- Bump the workspace version to 0.56.0
- release-notes/0.56.0.md

Includes #237 (conversation affinity, route held per turn), #238 (local
token counts, resend without sealed reasoning) and #239 (fail over at the
start of a stream, pause upstreams by the reason they give).

The control-plane protocol goes from 28 to 30; the request store schema
(22) is unchanged. groups[].session_affinity is removed.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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