Commit Graph
7 Commits
Author SHA1 Message Date
yetoneandump45nose ae98cda651 gateway: an inner group's own Patience waits out its busy members through an outer group (#2410)
serve read Patience only from the group the request named (g.Waits()).
planLevel stamped a nested group's FirstToken (#2246) on its candidates
but not its Patience, so an inner group's budget was never read through
an outer one: an inner 4 min under an outer default gave up at 1 min,
and an inner Off was still asked again for 60s.

Each candidate carries the innermost Patience that is set on its way
down (innerPatience, as innerFirstToken). waitsOf reads it as Group.Waits
does, falling back to the asked group's when unset; PatienceOff keeps the
member out of replan. replan counts each member's budget from waitFrom
and asks again only those with budget left past the pause; the hold on
the last failure lasts while any member's may (maxWaits), and the
trace's Patience is the member's own. With no nested Patience set,
every member's budget is g.Waits(), as before.

Patch by ump45nose (61b1c17c); ggbdpq checked it against main on the
issue. Review changes: the docs sentence was placed by hand (the docs
hunk no longer applied), and the tests' budgets moved from inner 8s /
outer 3s to 10s / 4s, so every round's pause ends 2s or more from a
budget line (it was 1s from the 3s line), as a timing test leaves room
on both sides.

Run:
- With fallback.go, gateway.go, patience.go and patience_test.go put
  back to main: TestNestedGroupPatienceThroughOuter FAIL ("a.n=2, want
  3 — the inner group's 10s budget was not read"),
  TestNestedGroupPatienceInnerOff FAIL ("code=529 a.n=13 (want 529, 1 —
  no replan)"). With replan reading only the fallback budget:
  ThroughOuter FAIL the same. Restored: ok.
- TestNestedGroupPatienceFallsBackToOuter, TestGroupWaitsOut*,
  TestPatienceIsUsedUp: ok.
- Full suite, go test -tags nogui -timeout 45m ./... (set -o pipefail,
  sandbox HOME, load ~350) at 0224e826f (these four commits on
  ebf3f48f7) plus #2545's patch: every package ok except internal/agent,
  whose one red (TestRekeyGivesWSLAgentTheSharingKey) is #2545's, which
  was dropped (problems posted on #2545). internal/agent alone at
  0224e826f: ok. Rebased on dccfefdf7 (#2635, internal/library only):
  build, vet, internal/library ok. gofmt -l clean; go vet darwin (gui),
  GOOS=linux and GOOS=windows -tags nogui: clean. GUI (i18n-keys,
  gui-de/ja/zh-TW, import-*, key-import, login-import): all pass except
  login-import's Chromium case, which fails the same alone on origin/main
  ebf3f48f7 (#1307, output posted there).

Co-authored-by: ump45nose <ump45nose@users.noreply.github.com>
2026-10-17 11:54:56 +08:00
yetone 10f3877071 gateway tests: TestCodexHeardWithinItsIdleTimeout leaves over a second of room on both sides, where a busy machine's lag carried member a's failure past Codex's quiet limit (#1800)
#1800 has the test failing two ways on busy machines. The body check:
member a failed at 0.7s and Codex's quiet limit was 1s, so 300ms of the
gateway's lag put a's failure past the limit, and it went to Codex as
it was. The bound on the second half: 3s, against 1.2s expected and
3.2s for the pause waited.

Now the quiet limit is 2s (a fails 1.3s before it), b's reply comes at
4.7s with the first event bounded at 3.5s, and the second half's pause
is 10s with a 6s bound (1.2s expected, 11.2s waited).

Run:
- Reproduced the body failure on origin/main 7d15df78a by injecting
  350ms into a's wait (1050ms): overload_test.go:269 got a's
  server_is_overloaded, as in #1800's comments.
- With the change: passes 3/3; with 1s injected into a's wait (1.7s);
  with 1.3s injected into each member's wait in the second half; 8/8
  under 24 busy loops on top of other sessions' load (load average
  38 to 69).
- Broken by hand: tooQuiet returning false fails the first bound
  (4.72s); the replan and passing pauses no longer skipped when too
  quiet fails the second (21.8s, a 2, b 1).
- gofmt -l clean; go vet darwin, linux -tags nogui, windows.

Not tried: the whole gateway package after the change (only this
test); CI's machines. The test takes about 6.3s, up from 4.5s.
2026-10-14 08:47:30 +08:00
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