gateway: a row told an earlier try's 529 overload says its kind, not the last account's usage_limit_reached (蛋仔)

蛋仔 on Discord: a Codex turn over ChatGPT accounts showed in Usage →
Requests as "529 · usage_limit_reached · 4 次尝试", while Codex said
"Selected model is at capacity. Please try a different model." The
model at capacity is no used-up quota.

Cause: when the last account left is out of its allowance and one
asked before it failed otherwise, serve tells the agent that earlier
failure (waroy's rule): call.Status and call.Error were swapped for
the earlier try's, but call.Usage.ErrType kept the last account's
usage_limit_reached, so the row paired the 529 and its words with the
quota's kind. The same swap where middleware passes every account
over. Try now keeps the failure's kind (errType, unexported, not in
the trace JSON) wherever it is kept as `other`, and both swaps take it.

Accounts were not rested for the overload: failure(529, …) is not
failQuota, vendorOverloaded rests the overloaded account 30s at most a
minute with no failure counted, and only the account that answered
usage_limit_reached rests till its reset. The new test checks that too.
Anthropic's 529 overloaded_error handling (saidOverloaded,
vendorOverloaded, fallback/patience status lists) is unchanged; the
own-sign-in path (codex_backend.go) records its own failure's kind and
never swapped one in. What Codex is told is unchanged.

Test: TestCodexCapacityNotLabelledUsageLimit (two ChatGPT accounts,
acct-1 529 with the backend's overload and with the words Codex
showed, acct-2 429 usage_limit_reached): the row is 529 · server_error,
the overloaded account rests a minute at most, one account rests hours.

Run on mote (mote-run), at this tree on origin/main 7dd2434d3:
- broken by hand (the two swaps without ErrType): the new test FAILs,
  "row: 529 · \"usage_limit_reached\" · \"Codex: Selected model is at
  capacity. Please try a different model.\"", both subtests; restored,
  ok.
- gofmt -l internal/gateway: empty; go vet, GOOS=linux go vet -tags
  nogui, GOOS=windows go vet of ./internal/gateway: ok.
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway: ok (791.8s)
- GUI files that render err_type (ledger-cache-tokens,
  usage-ledger-detail, usage-session-speed, usage-loops): Chromium 60
  pass 0 fail, WebKit 30 pass 0 fail (no GUI change).

Rebased on origin/main 844c35505 (no internal/gateway change since
7dd2434d3); the new test and the overload/spent tests: ok there.

Not tried: a real ChatGPT account at capacity (can't be made to
happen); the full ./... suite (runs after the push); Windows/Linux runs.
This commit is contained in:
yetone
2026-11-04 19:25:42 +08:00
parent 9095fd82d8
commit 7353a4bc2a
3 changed files with 109 additions and 20 deletions
@@ -0,0 +1,82 @@
package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// 蛋仔 on Discord: a Codex turn over two ChatGPT accounts, the first's
// model at capacity (529), the last out of its allowance. Codex is told
// the overload ("Selected model is at capacity. Please try a different
// model."), as it should be, but the Requests row read "529 ·
// usage_limit_reached": the status and words were the overload's, the
// kind the last account's. The row's kind is the failure Codex was told,
// and only the account out of its allowance rests for long.
func TestCodexCapacityNotLabelledUsageLimit(t *testing.T) {
for name, overload := range map[string]string{
"backend's overload": overloadedStatus,
"the words Codex showed": `{"error":{"type":"server_error","code":"server_is_overloaded","message":"Selected model is at capacity. Please try a different model."}}`,
} {
t.Run(name, func(t *testing.T) {
codexSignedIn(t, "spare@example.com")
var tried []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.ReadAll(r.Body)
acct := r.Header.Get("chatgpt-account-id")
tried = append(tried, acct)
w.Header().Set("Content-Type", "application/json")
if acct == "acct-1" {
w.WriteHeader(529)
io.WriteString(w, overload)
return
}
w.WriteHeader(429)
io.WriteString(w, `{"error":{"type":"usage_limit_reached","message":"The usage limit has been reached","plan_type":"plus","resets_in_seconds":7200}}`)
}))
t.Cleanup(up.Close)
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
pause := retryPause
retryPause = time.Millisecond
t.Cleanup(func() { retryPause = pause })
code, body := codexPost(t, oneAccountTurn)
if code != 529 || strings.Contains(strings.ToLower(body), "usage limit") {
t.Fatalf("Codex told %d %s (tried %v)", code, body, tried)
}
if tried[0] != "acct-1" || tried[len(tried)-1] != "acct-2" {
t.Fatalf("tried %v", tried)
}
rec := lastUsage(t)
if rec.Status != 529 || rec.ErrType == "usage_limit_reached" || !strings.Contains(strings.ToLower(rec.Error), "overloaded") && !strings.Contains(rec.Error, "at capacity") {
t.Fatalf("row: %d · %q · %q", rec.Status, rec.ErrType, rec.Error)
}
if rec.ErrType != "server_error" {
t.Fatalf("row's kind %q, want the overload's server_error", rec.ErrType)
}
// the overloaded account sits out a moment at most; the one out
// of its allowance rests till ChatGPT says it is back
restingUntil.Lock()
defer restingUntil.Unlock()
long := 0
for id, u := range restingUntil.m {
switch d := time.Until(u); {
case d > time.Hour:
long++
case d > overloadRestMost:
t.Errorf("%s rests %v for an overload", id, d)
}
}
if long != 1 {
t.Errorf("%d accounts rest for hours, want the one out of its allowance: %v", long, restingUntil.m)
}
})
}
}
+23 -20
View File
@@ -3169,7 +3169,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
msg := ""
switch {
case other != nil:
call.Status, call.Error = other.Status, other.Error
call.Status, call.Error, call.Usage.ErrType = other.Status, other.Error, other.errType
case call.Status < 400:
call.Status, call.Error = 404, "every account passed over by a middleware"
msg = fmt.Sprintf("none of %s's models is ready", call.Model)
@@ -3331,7 +3331,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// but with a quota, the agent is told this, as it would be had
// Antigravity been asked
if other == nil {
other = &Try{Status: turnedAwayStatus, Error: c.p.Name + ": " + hint}
other = &Try{Status: turnedAwayStatus, Error: c.p.Name + ": " + hint, errType: turnedAwayErrType}
}
skipped = append(skipped, c.label()+": "+hint)
continue
@@ -3840,7 +3840,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
break
}
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
skipped = append(skipped, c.label()+": "+call.Error)
continue
@@ -4060,7 +4060,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// nobody after it answers but with a quota of its own, that is
// what the agent is told, at a status it doesn't retry
if other == nil {
other = &Try{Status: turnedAwayStatus, Error: call.Error}
other = &Try{Status: turnedAwayStatus, Error: call.Error, errType: turnedAwayErrType}
}
try.Fail = failPrompt
s.trace.update(tr, func(t *Route) {
@@ -4102,7 +4102,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// Nothing is wrong with the account, so it doesn't rest; its
// mates, carrying the same prompt, are asked last
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failPrompt
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4148,7 +4148,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
}
if left := withRoom(cands[i+1:], c, tokens); len(left) > 0 {
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failOverflow
if hw.code() == http.StatusRequestEntityTooLarge {
@@ -4187,7 +4187,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// input item it doesn't know, #350) another's may: the next is
// asked once, as each is, and this one doesn't rest
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failShape
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4199,7 +4199,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// to the vendor, so this one doesn't rest, and a member that
// goes another way (or the proxy back up) may still answer (#381)
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failProxy
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4211,7 +4211,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// request never reached the vendor, so this one doesn't rest,
// and is asked first again once the network is back
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failOffline
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4224,7 +4224,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// is signed in again (provider/claude_auth.go), and the next
// one is asked
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failAuth
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4251,7 +4251,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
noRoom := hw.failed() && claudeNoRoom(hw.code(), hw.errBody())
if protected || zdr || noRoom {
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failOther
left := elsewhere(cands[i+1:], c)
@@ -4299,7 +4299,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// The last one left is asked again after a moment, as for any
// 503, before the agent is told
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failOther
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4311,7 +4311,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// is the model's doing, not the account's: the next is asked,
// and nobody rests (#2274)
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failOther
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4324,7 +4324,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// and mates of the same model, likely to end empty too, are
// asked last
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failOther
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4365,7 +4365,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
})
if !last {
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
skipped = append(skipped, c.label()+": "+call.Error)
matesLast(cands[i+1:], c)
@@ -4405,7 +4405,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
next, nextW, unlisted, unlistedW = unlistedNext(unlisted, unlistedW, key)
if len(next) > 0 && last {
if f := failure(hw.code(), hw.errBody()); other == nil && f != failQuota && f != failCredit {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
rest := s.restAfterMarked(c, hw.code(), hw.header, hw.errBody(), hw.sharedPool)
try.Fail, try.Rest = rest.Why, &rest
@@ -4431,7 +4431,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// whose reply said nothing (#1221) or was cut short by its
// output limit (#2274): the next is asked, and nobody rests
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
try.Fail = failOther
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
@@ -4446,7 +4446,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// then the next member as any failure's is (#2873)
try.Fail = failCut
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
skipped = append(skipped, c.label()+": "+call.Error)
@@ -4486,7 +4486,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
}
if !last && hw.failed() {
if f := failure(hw.code(), hw.errBody()); other == nil && f != failQuota && f != failCredit {
other = &Try{Status: call.Status, Error: call.Error}
other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType}
}
rest := s.restAfterMarked(c, hw.code(), hw.header, hw.errBody(), hw.sharedPool)
try.Fail, try.Rest = rest.Why, &rest
@@ -4692,7 +4692,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// (Discord, waroy)
try.Fail, try.Earlier = f, true
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
call.Status, call.Error = other.Status, other.Error
// and its row says that one's kind too: a 529 overload told
// beside the last account's usage_limit_reached read as the
// overload being the quota (蛋仔 on Discord)
call.Status, call.Error, call.Usage.ErrType = other.Status, other.Error, other.errType
failTo(w, kept, from, call.Status, call.Error)
break
} else if outgrew {
+4
View File
@@ -482,6 +482,10 @@ type Try struct {
// allowance's (Discord, waroy). Whether a try went on is the next
// try in Tries, which is appended only as it is asked (#2281)
Earlier bool `json:"earlier,omitempty"`
// errType: the kind of error the failure's body named (Usage.ErrType),
// kept for the one told in place of a later try's (Earlier), so the
// row's kind is that failure's and not the later try's
errType string
// Queued: ms it waited for one of its key's or account's slots, the
// provider's MaxConcurrency out already (concurrency.go)
Queued int64 `json:"queued,omitempty"`