Commit Graph
5 Commits
Author SHA1 Message Date
yetone a68d2ac944 gateway: a held stream lets through only text, reasoning or a call, so a 200 that only framed a reply fails over (#1418)
Aliceonly's trace on #1418: sub2api answered 502 "Upstream HTTP/2 stream
failed", then Codex account A answered 200, sent only the frames before a
reply, and failed server_is_overloaded 37s in. Five candidates after it
were never asked, and Codex ended the turn. The stream had been let
through (h.passing), so holdWriter.failed() was false and neither the next
candidate nor a replan was tried.

Two ways a frames-only stream went through:
- streamEvent's default was eventContent: any event it didn't know (and an
  item of a type said() didn't know) counted as the reply begun. Now only
  the events of each protocol that carry text, reasoning, a call or a
  reply's end (replyEvent) count; anything else is eventLead, held as a
  ping is. An unknown item counts only when it is a call or a tool's
  (web_search_call, server_tool_use, mcp_*). A stream that ends well is
  still released whole, so this can only delay, never drop, a reply.
- a reply under a Content-Type that says neither JSON nor a stream, once
  the agent had a stream's headers, went straight through when its first
  bytes were SSE (sseStart -> pass()). It is now held as a stream from
  there and read event by event.

Which event Codex's backend actually sent isn't in the trace; the test
uses a keepalive, response.rate_limits.updated and an unknown item type.

Run: go test ./internal/gateway -count=1, green (428s).
TestFramesThenOverloadedGoesToTheNext (new: 502, then 200 frames + unknown
events + server_is_overloaded, then a third member) and
TestStreamEventUnknownOnlyFrames (new) fail on the old fallback.go: the
overloaded stream reaches the agent and c is never asked.
TestHeldUnsureStreamPasses/KeptAlive now expect the stream held until its
first word, and TestHeldUnsureStreamFailsHeld (new) a failure among held
frames; all fail on the old code. Not tried against the real ChatGPT
backend.
2026-10-13 07:30:06 +08:00
yetone e9ae318589 gateway: a relay's 500 "负载已经达到上限" is a busy server, not quota, so a group's Patience waits it out (#1418, real-jacket)
real-jacket's relay answered 500 "当前模型 gpt-5.5 负载已经达到上限,请稍后重试
(request id: …)". usedUpWords matched 上限, so failure() called it
failQuota: the key rested till a reset and busy() (5xx only when
failOther) never let Patience wait, so the agent got the 500 at once.

Change: a 5xx is out of quota only on a vendor's own quota code
(quotaCodes: Claude Code's "usage limit reached|<unix>",
usage_limit_reached, RESOURCE_EXHAUSTED). The quota words still classify a
4xx, and the 429 plan words are unchanged; credit is unchanged.
gateway-routing.md says so.

Run:
- routing_test's failure table: the literal 500 → failOther; a 503
  "upstream quota service unavailable" → failOther; 500
  usage_limit_reached and 503 RESOURCE_EXHAUSTED → failQuota; 403
  "今日额度已达上限" → failQuota; 429 "当前分组上游负载已饱和" → failRate.
- New TestGroupWaitsOutRelayLoadLimit (two members answering the literal
  500, Patience 240): fails on the old code with the 500 (a 1, b 3), now
  200 after a replan with no quota rest.
- go test ./internal/gateway, gofmt, GOOS=linux/windows go vet -tags nogui.

Not done: a 429 with the same load wording still reads as quota (no
literal case of it seen).
2026-10-11 19:59:15 +08:00
yetone 53cce81664 gateway: Codex hears an event of its reply within its idle timeout while magpie holds its tries, instead of reconnecting on "idle timeout waiting for SSE" (fadenoob on Discord)
Codex counts only SSE events toward its stream idle timeout (300s by
default), not the ": keepalive" comments magpie sends while it holds a
try. Each try was bounded by itself (holdLead, 4 minutes since
1a5d7908), and a group's replan pauses (c43681d6) come between tries,
so held tries and pauses could add up past 300s with no event, and
Codex dropped the stream and reconnected. Reproduced against Codex's
own behaviour with a fake that sends only comments: the same error.

keptAlive now keeps the time of the request's start or its last event
(told) and whether the agent streams. For a streaming Responses agent,
once agentQuietMost (4 minutes) has passed with no event, the try in
hand is let through (scan, keepQuiet), and a replan or same-one retry
whose pause would end past the bound isn't taken, so the agent gets
the reply or the error in time.

Run: TestCodexHeardWithinItsIdleTimeout (agentQuietMost scaled to 1s;
a slow second member after a failed first: first event in ~1.0s,
bound 1.6s, old code 2.2s, red; every member overloaded with a 2s
replan pause: error reaches the agent in ~1.2s). Passed 3 times.
go test -tags nogui ./internal/gateway ./internal/provider ok; gofmt
-l clean; GOOS=linux and GOOS=windows go vet -tags nogui on both.
docs/subsystems/gateway-routing.md says the bound.
2026-10-10 18:54:16 +08:00
yetone c43681d64c gateway: a group whose every member is busy asks them again within a budget, 1 minute by default, instead of ending the agent's turn on the last one's error (#1418, Aliceonly)
Aliceonly's Codex turns ended on server_is_overloaded when plus-B and
sub2api were both overloaded, to be told "continue" by hand. #1421
promised a group-wide budget for errors that pass. This builds it.

When every member of a group has failed in a way that passes (busy:
408/500/502/503/504/529 that isn't a proxy, quota or response
protection; a 429 that is a short rate limit, not a plan or credit),
and nothing of the reply has been sent, the members that failed so are
asked again in turn after a pause (replan). The pause starts at 2s,
doubles each round up to 30s, and is never shorter than the soonest
Retry-After; a member still asking for time goes after the others. At
most 6 rounds, and only while the group's Patience lasts, counted from
the first time every member had failed (provider.Group.Patience:
0 = 60s default, -1 off, at most 240s). While another member is busy
too, the last one isn't first retried alone (passing): the members take
turns. The last member's failure is held while the budget lasts, and a
streaming agent gets SSE keepalives through the pause (pauseAlive). The
trace's try says Again (the pause), Replan (how many) and Patience.
Plain providers are unchanged.

GUI: the group editor's "Every member busy" row (Off / 1 min / 2 min /
4 min) and the trace's line for a replanned try, in en, zh, zh-TW, ja
and de.

Run:
- TestGroupWaitsOutEveryMemberOverloaded, TestCodexStreamWaitsOutOverloadedGroup
  (new): both fail with patience forced to 0 (the old behaviour: the
  529, and the response.failed stream, reach the agent), pass now.
- go test -tags nogui ./... under a temp HOME: all ok.
- gofmt -l: clean. go vet (darwin, GOOS=linux, GOOS=windows) on
  internal/gateway and internal/provider: clean.
- GUI: group-patience (new), group-first-token, i18n-keys, gui-ja,
  gui-de in Chromium and WebKit: 15/15 pass.
- Real use: a sandbox `magpie serve` on 127.0.0.1:3782 under a scratch
  HOME, group/sol of two Responses providers on a fake upstream that
  sends 200 + response.created/in_progress, pings, and 20s later
  response.failed server_is_overloaded. curl streaming /v1/responses:
  plus-B failed at 20s (held, not let through), sub2api at 40s, 2s
  pause, plus-B asked again answered "hello from a"; the client saw
  only keepalives and the reply. Trace: plus-B 529, sub2api 529 with
  again 2000 replan 2 patience 60, plus-B 200. With both always
  overloaded the request ended after 3 rounds (127s) with the vendor's
  response.failed.
2026-10-10 17:49:07 +08:00
yetone 1a5d790814 gateway: a stream that has said nothing but its frames stays held while the agent is kept alive, so a server_is_overloaded after 15s goes to the group's next member (#1418, Aliceonly)
Aliceonly's trace: plus-B answered 200 with response.created,
in_progress and codex.rate_limits, nothing else, and 36s on failed the
response with server_is_overloaded. The holdWriter let any stream
through on the first write after holdLongest (15s), so the failure came
after passing was set: it reached Codex as the turn's end, and sub2api,
next in the group, was never asked.

A stream with nothing but its lead frames is now held as long as one
that is thinking (holdLead = holdThinking) when its agent is being kept
alive with SSE comments (keptQuiet: a streaming agent that isn't
Gemini). The comments keep the agent from timing out, so holding costs
nothing; a stream with content is let through as before, and a Gemini
stream, whose agent takes no comment, is let through at 15s as before.

Run:
- TestLeadOnlyStreamHeldWhileKeptAlive (new): fails on the old code with
  "let through after 15s with nothing said" (checked by putting the old
  hold back), passes now.
- go test -tags nogui ./internal/gateway under a temp HOME: ok.
- gofmt -l internal/gateway: clean.
2026-10-10 17:49:07 +08:00