gateway: a vendor's overload rests no account and climbs no backoff (celestial_eagle_99230)

celestial_eagle_99230 on Discord (v0.1.2670, Codex on auto/gpt-6-sol,
eight ChatGPT Plus accounts, reported several times): every account
showed "休息中 · 529 · 失败 · 10 分钟后恢复", with "它已连续失败 5 次:
休息 10 分钟,每次更久". The request log had the ChatGPT backend's
{"type":"service_unavailable_error","code":"server_is_overloaded",
"message":"Our servers are currently overloaded. Please try again
later."} on /backend-api/codex/responses.

restAfterMarked read the 529 as failOther: the account's own failure,
counted on the backoff ladder (1m, 2m, 4m, 8m, 10m cap). An overload is
the vendor's servers, which every account meets alike, so one spell of
it benched all eight accounts for growing periods and left nothing to
answer. b58669eb0 (v0.1.2721) changed only how the overload is told to
Codex; origin/main still rested and climbed this way.

Now an overload (vendorOverloaded: a 529, or a 5xx whose words say
overloaded, the existing saidOverloaded rule: ChatGPT's
server_is_overloaded, Anthropic's overloaded_error, Gemini's 503 "The
model is overloaded") rests only the model on that account
(restID), 30s (overloadRest), or as long as Retry-After asks up to a
minute (overloadRestMost), every time alike, and counts no failure. The
next account is still asked, the serve loop's patience still waits out
an overload on every member as before, and when all stay busy the
client is told the overload as b58669eb0 tells it (unchanged here). Quota,
credit, rate limits, 401/403 and a plain 5xx rest and climb as before.
The one choke point covers every route that rests (serve loop, group
decider, plugin calls). A provider set never to rest (RestCapOff) still
doesn't.

docs/subsystems/gateway-routing.md says it beside fallbackCooldown.

Tests (internal/gateway/overload_rest_test.go), from the reporter's
literal error body:
- TestOverloadRestsNoAccount: three Codex accounts, two answering 529
  server_is_overloaded with Retry-After 7, six requests: no account rests
  whole, the model rests <=7s by retry-after with 0 failures.
- TestOverloadRestIsShortAndFlat: six overloads in a row for each of
  ChatGPT 529, ChatGPT stream error, Anthropic 529 overloaded_error,
  Gemini 503: model rest <=30s, 0 failures each time; Retry-After 600
  is held to a minute; then a 500 is the first ladder step (1 failure),
  a 401 rests the account (2), a usage_limit_reached is quota.

Broken by hand: with origin/main's routing.go both tests go red, the
rests climbing 1m, 2m, 4m, 8m, 10m by backoff for the same overloads.

Run on mote at this head: gofmt -l clean; GOOS=linux and GOOS=windows
go vet ./internal/gateway ok; go test -count=1 -timeout 45m
./internal/gateway: every test ok except
TestCodexPoolSaysTheClientsVersion, the known #4807 (another test's Codex
identity reaches it in a full package run); it and the new tests pass
run alone at this head.

Not tried: a real ChatGPT overload (can't be caused on demand); the
full ./... suite (another session's full suite was running on mote);
GUI suite (no GUI change: the rest shows with the existing "retry-after"
and "cooldown" wording). A real 5xx met during an overload's 30s rest
isn't counted (backingOff sees a failOther rest), so it rests at the
current step rather than climbing one; that errs toward resting less.
This commit is contained in:
yetone
2026-11-02 20:50:46 +08:00
parent 5daba8337b
commit f3551387c6
3 changed files with 160 additions and 2 deletions
File diff suppressed because one or more lines are too long
+125
View File
@@ -0,0 +1,125 @@
package gateway
import (
"io"
"net/http"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// What celestial_eagle_99230's request log had from the ChatGPT backend on
// /backend-api/codex/responses (Discord, v0.1.2670, eight Plus accounts on
// gpt-6-sol): a 529 whose error is the server's overload.
const chatgptOverloaded = `{"error":{"type":"service_unavailable_error","code":"server_is_overloaded","message":"Our servers are currently overloaded. Please try again later."}}`
// An upstream overload is the vendor's moment, not the account's fault:
// every Codex account that met ChatGPT's 529 server_is_overloaded rests
// its model alone, briefly (a Retry-After honoured up to a minute), and
// counts no failure, however often it comes. Before, each overload counted
// as the account failing: "failed 5 times in a row", every one of the
// eight accounts resting up to the 10-minute cap, and nothing left to
// answer the user (celestial_eagle_99230 on Discord).
func TestOverloadRestsNoAccount(t *testing.T) {
codexSignedIn(t, "b@example.com", "c@example.com")
if err := provider.SetRouting("codex", provider.Ordered); err != nil {
t.Fatal(err)
}
var tried []string
chatgptRefusing(t, &tried, func(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Retry-After", "7")
w.WriteHeader(529)
io.WriteString(w, chatgptOverloaded)
}, "acct-2")
s := New()
// every request of the spell: the first two accounts overloaded, the
// third answers
rested := 0
for range 6 {
restingUntil.Lock()
for k := range restingUntil.m {
restingUntil.m[k] = time.Now().Add(-time.Second)
}
restingUntil.Unlock()
if code, out := codexAskOn(s, `{"model":"gpt-6-sol","stream":true,"input":"ping"}`); code != 200 {
t.Fatalf("%d %s", code, out)
}
for _, who := range []string{"codex@me@example.com", "codex@b@example.com"} {
if r, ok := restOf(who); ok {
t.Fatalf("%s rests whole for an overload: %+v", who, r)
}
r, ok := restOf(who + "/gpt-6-sol")
if !ok {
continue
}
rested++
if r.Why != failOther || r.Failures != 0 || r.By != "retry-after" || r.Model != "gpt-6-sol" || time.Until(r.Until) > 7*time.Second {
t.Fatalf("%s rests %v by %s, %d failures in a row, for an overload", who, time.Until(r.Until).Round(time.Second), r.By, r.Failures)
}
}
}
if rested == 0 {
t.Fatalf("no account's model rests (tried %v): the test proves nothing", tried)
}
}
// The rest an overload takes is the same each time and counts nothing,
// for every vendor's way of saying it; a Retry-After is honoured up to a
// minute. The account's own failures, a 401 and a usage limit, rest it
// as before, and climb the ladder from where the overloads left it.
func TestOverloadRestIsShortAndFlat(t *testing.T) {
fresh(t)
s := New()
c := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}}, model: "gpt-6-sol", rest: "codex@me@example.com"}
over := func() {
restingUntil.Lock()
for k := range restingUntil.m {
restingUntil.m[k] = time.Now().Add(-time.Second)
}
restingUntil.Unlock()
}
for _, x := range []struct {
name string
status int
body string
}{
{"chatgpt 529", 529, chatgptOverloaded},
{"chatgpt stream", 529, `{"error":{"code":"server_is_overloaded","message":"Our servers are currently overloaded. Please try again later."}}`},
{"anthropic 529", 529, `{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}`},
{"gemini 503", 503, `{"error":{"code":503,"message":"The model is overloaded. Please try again later.","status":"UNAVAILABLE"}}`},
} {
t.Run(x.name, func(t *testing.T) {
for i := range 6 {
over()
r := s.restAfter(c, x.status, http.Header{}, []byte(x.body))
if r.Why != failOther || r.Failures != 0 || r.Key != c.restID() || r.By != "cooldown" || time.Until(r.Until) > overloadRest {
t.Fatalf("overload %d: rests %s %v by %s, %d failures", i+1, r.Key, time.Until(r.Until).Round(time.Second), r.By, r.Failures)
}
}
})
}
h := http.Header{}
h.Set("Retry-After", "600")
over()
if r := s.restAfter(c, 529, h, []byte(chatgptOverloaded)); r.By != "retry-after" || time.Until(r.Until) > overloadRestMost {
t.Fatalf("Retry-After 600: rests %v by %s", time.Until(r.Until).Round(time.Second), r.By)
}
// a plain 5xx is the account's failure, as before: the first step,
// not one past the overloads
over()
if r := s.restAfter(c, 500, http.Header{}, []byte(`{"error":{"message":"An error occurred while processing your request.","type":"server_error"}}`)); r.Failures != 1 || r.By != "backoff" {
t.Fatalf("a 500 after the overloads: %d failures by %s", r.Failures, r.By)
}
over()
if r := s.restAfter(c, 401, http.Header{}, []byte(`{"error":{"message":"Your authentication token has expired.","code":"token_expired"}}`)); r.Key != c.restKey() || r.Failures != 2 {
t.Fatalf("a 401 rests %s, %d failures", r.Key, r.Failures)
}
over()
if r := s.restAfter(c, 429, http.Header{}, []byte(`{"error":{"type":"usage_limit_reached","message":"The usage limit has been reached"}}`)); r.Why != failQuota {
t.Fatalf("a usage limit rests as %s", r.Why)
}
}
+34 -1
View File
@@ -182,6 +182,12 @@ const (
// and a day over
longestQuota = 8 * 24 * time.Hour
longestRetry = 10 * time.Minute // failing again and again
// overloadRest: a vendor too busy for the request (vendorOverloaded)
// rests the model on that account this long when it says nothing of
// when, and no longer than overloadRestMost when it does: the same
// every time, never a step on the backoff ladder
overloadRest = 30 * time.Second
overloadRestMost = time.Minute
// longestRateRest is the most a rate limit that keeps coming back as
// soon as its rest is over is waited out, when the vendor says nothing
// of when (rateRest)
@@ -1246,6 +1252,9 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// trial entry zcode.z.ai's firewall blocked (zcodeTrialBlocked): the
// account's GLM Coding Plan goes to another host
unserved := why == failOther && (modelRefused(status, body) || codeAssistModelMissing(c.p, status, body) || zcodeTrialBlocked(c, status, body))
// the vendor too busy for the request: the model's moment, not the
// account's fault (vendorOverloaded)
overloaded := why == failOther && !unserved && c.model != "" && vendorOverloaded(status, body)
r := Rest{Why: why, Status: status, By: "cooldown", Error: restError(status, body)}
// out of allowance or credit for as long as magpie guessed: the vendor
// named no time, nor did the user (asksAt)
@@ -1395,6 +1404,21 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
r.By, r.Until = "off", now
return r
}
if overloaded {
// the vendor's servers too busy for anyone (celestial_eagle_99230
// on Discord: ChatGPT's 529 server_is_overloaded on gpt-6-sol
// rested eight Plus accounts each up to the 10-minute cap,
// "failed 5 times in a row", and left nothing to answer). It
// says nothing of the account: its model sits out a moment on
// it, as long as the vendor's Retry-After asks up to a minute,
// and no failure is counted, so the next overload rests it as
// briefly and its own failures climb as they did
d, r.By = overloadRest, "cooldown"
if w := retryAfter(header, now); w > 0 {
d, r.By = min(w, overloadRestMost), "retry-after"
}
break
}
counted := c.restKey()
if unserved {
counted = c.restID()
@@ -1475,7 +1499,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// a refusal that names its reset and points to the account's other
// models is the model's limit: the others still answer (WorkBuddy's
// "您也可以切换其他模型继续使用", #1669)
if pooled || unserved || modelBackoff || why == failQuota && resetsWhen.Match(wordsOf(body)) && otherModels.Match(wordsOf(body)) {
if pooled || unserved || modelBackoff || overloaded || why == failQuota && resetsWhen.Match(wordsOf(body)) && otherModels.Match(wordsOf(body)) {
id = c.restID()
}
// Cline's daily limit of one free model is that model's: its others,
@@ -1561,6 +1585,15 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
return r
}
// vendorOverloaded says a failure is the vendor's servers too busy for
// the request — a 529 (Anthropic's overloaded_error, the ChatGPT backend's
// server_is_overloaded), or a 5xx whose words say overloaded (Gemini's
// 503 "The model is overloaded") — which any account of it would have
// met alike (saidOverloaded).
func vendorOverloaded(status int, body []byte) bool {
return saidOverloaded(status, string(wordsOf(body)))
}
// modelCooledDown is a relay's refusal of one model whose pooled
// credentials cooled down (modelCooling): out of quota, credit or rate,
// the limit is that model's on the relay, and the key's other models are