gateway: a relay's 429 for one model's cooled-down credentials rests that model, not the whole key (#3039)

A relay that pools credentials per model (CLIProxyAPI and its forks,
OmniRoute) answers 429 code model_cooldown, "All credentials for model
model-a are cooling down via provider upstream (last error: ...)", with
a Retry-After of up to an hour. restAfter read that as the key's: the
whole key sat out the Retry-After, relay/model-b and /model-c got a
local 429 carrying model-a's sentence without a request leaving, while
the relay already served them (Welkon's report).

Now that wording (modelCooling, which also takes OmniRoute's "All
credentials for the requested model are cooling down") makes the
refusal the model's, read as out of quota, out of credit or a rate
limit (modelCooledDown): the rest, its said sentence, the rate ladder
(rateRest), the credit ladder (creditBackoff) and the sink are all
keyed by the model, in the same places a remote magpie's model rest is
(remoteModelRest). A cool-down that names no model still rests the key.
The wording was checked against the relays' own source:
CLIProxyAPI-family selector_errors.go (modelCooldownError, 429, its
Retry-After) and OmniRoute's open-sse/utils/error.ts.

Ported from ggbdpq's hunt52-c-model429-scope (42d5f7f5). Changed from
the patch: it scoped only the final rest id, so a rate-limited model
still climbed the key's ladder and sank the whole key, and a credit
refusal climbed the key's credit ladder; it took no failCredit; and its
regex missed OmniRoute's "requested model".

Test: TestModelCoolingDownRestsTheModelOnly drives restAfter with the
report's literal body and three more real shapes (CLIProxyAPI's
marshalled model_cooldown, OmniRoute's, a credit last error), with the
key's own ladder seeded at rung 3.

Run (mote): with origin/main's routing.go and the new test it fails
"quota: model-a's cooling-down rested {... Key:relay Model: ...}".
Broken by hand, each red: final id branch dropped (quota case, Key
relay); sink back to restKey ("rate: ... sank the whole key"); rateRest
id back ("rate: ... rung 4 of the key's ladder"); creditBackoff id back
("credit: ... rung 4"); regex without "the requested" ("requested: ...
Key:relay"); regex a bare "cooling down" (the account-level negative,
Key relay/model-b). Restored, green.

Run at this head (rebased on df1c3dace), on mote: go test -tags nogui
-count=1 -run 'Rest|Sink|Remote|Credit|Quota|Rate|Cline|ZCode|Smart|Cool'
./internal/gateway ok (24.1s); gofmt -l clean. At the review tree:
-race green on the new test; go vet -tags nogui ./internal/gateway on
darwin, GOOS=linux and GOOS=windows clean.

Not tried: a real CLIProxyAPI or OmniRoute relay (bodies are from the
report and the relays' source); the whole internal/gateway package and
./... before the push (the package runs in the background after).
Not changed: CLIProxyAPI's model_unavailable ("... are temporarily unavailable"), which is not a
429 and was not reported.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-10-31 09:35:51 +08:00
co-authored by ggbdpq
parent df1c3dace3
commit 2c5d9101bc
2 changed files with 114 additions and 3 deletions
+25 -3
View File
@@ -327,6 +327,12 @@ var (
// answer ("切换其他模型", "switch to the other models"): its limit is
// the model's, and the model alone rests
otherModels = regexp.MustCompile(`(?i)switch(?:ing)? to (?:the )?other models?|切换(?:到)?其他模型`)
// modelCooling is a relay that pools credentials per model saying
// those of the requested model cooled down: CLIProxyAPI's 429
// model_cooldown, "All credentials for model model-a are cooling down
// via provider upstream" (#3039, Welkon's relay), or OmniRoute's "All
// credentials for the requested model are cooling down"
modelCooling = regexp.MustCompile(`(?i)credentials for (?:the requested )?model .{0,200}?cooling down`)
// clineFreeLimit: Cline's refusal of a free model whose day's allowance
// is used up, 429 "Daily free limit reached on model deepseek/deepseek-
// v4-flash. Try again in 23h 59m", as its own clients read it (cline/
@@ -1200,7 +1206,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// under below; another computer's magpie rests its model
// for it, as before
id := c.restKey()
if c.p.PaidBesideFree(c.model) {
if c.p.PaidBesideFree(c.model) || modelCooledDown(why, body) {
id = c.restID()
}
d, r.By, r.Failures, guessed = creditBackoff(id, header, body, now)
@@ -1266,7 +1272,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// key's own limit apart): the sink keeps the same scope, or
// one model's 429 would send the remote's other models, never
// limited, behind the others too
if remoteModelRest(c, failRate, header) {
if remoteModelRest(c, failRate, header) || modelCooledDown(failRate, body) {
sink(c.restID(), now)
} else {
sink(c.restKey(), now)
@@ -1388,6 +1394,13 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
if why == failQuota && zcodeTrialRefused(c, status, body) {
id = c.restID()
}
// a relay's 429 that names the model whose credentials cooled down is
// that model's: its siblings on the same key still answer upstream,
// and the model's own sentence rests with the model, not on every
// sibling's refusal (#3039)
if modelCooledDown(why, body) {
id = c.restID()
}
// out of credit, or of a quota, on a model that costs credits where
// the subscription serves other models free is that model's: the free
// ones draw on no credits, and the account still serves them (Creat.wu
@@ -1434,6 +1447,15 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
return r
}
// 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
// still asked (#3039). Its rest, its ladder and its sink are the model's,
// as a remote magpie's are (remoteModelRest).
func modelCooledDown(why string, body []byte) bool {
return (why == failQuota || why == failCredit || why == failRate) && modelCooling.Match(wordsOf(body))
}
// remoteModelRest says a refusal for want of quota, credit or rate rests
// only the model it was about, when it came from another computer's magpie
// (a Remote magpie provider, LAN share): that magpie serves each of its
@@ -1539,7 +1561,7 @@ func backingOff(key string, now time.Time) bool {
// count where it is, so one request's own retries don't stretch it.
func (s *Server) rateRest(c candidate, header http.Header, body []byte, sharedPool bool, now time.Time) (time.Duration, string, int) {
id := c.restKey()
if c.isOpenRouterFree() && sharedPool || remoteModelRest(c, failRate, header) {
if c.isOpenRouterFree() && sharedPool || remoteModelRest(c, failRate, header) || modelCooledDown(failRate, body) {
id = c.restID()
}
n := 1
+89
View File
@@ -1,6 +1,7 @@
package gateway
import (
"bytes"
"net/http"
"strconv"
"testing"
@@ -324,3 +325,91 @@ func TestSmartFiveHoursAlone(t *testing.T) {
t.Fatalf("a week renewing within the hour first: %s", got)
}
}
// A relay's 429 that names the model whose credentials cooled down is
// that model's rest: its siblings on the same key still answer upstream,
// and none of them is told the model's sentence (#3039, Welkon's relay:
// "All credentials for model model-a are cooling down via provider
// upstream", Retry-After an hour). Resting the key benched every sibling
// for that hour without one being asked. Read as a rate limit or out of
// credit, the model's ladder and sink are its own too.
func TestModelCoolingDownRestsTheModelOnly(t *testing.T) {
old := allowances
defer func() { allowances = old }()
fresh(t)
forgetSinks(t)
allowances = func(string) map[string]provider.Allowance {
return map[string]provider.Allowance{}
}
key := func(model string) candidate {
return candidate{p: provider.Provider{ID: "relay", Key: "sk-relay"}, model: model, rest: "relay"}
}
s := &Server{}
header := http.Header{}
header.Set("Retry-After", "3597")
for _, tc := range []struct {
name, why string
body string
noWait bool // sent with no Retry-After: on the credit ladder
}{
// the report's own bytes
{"quota", failQuota, `{"error":{"message":"All credentials for model model-a are cooling down via provider upstream (last error: usage_limit_reached: The usage limit has been reached)"}}`, false},
// CLIProxyAPI's modelCooldownError as it marshals it, with no
// last error: a rate limit
{"rate", failRate, `{"error":{"code":"model_cooldown","message":"All credentials for model model-a are cooling down via provider codex","model":"model-a","provider":"codex","reset_seconds":3597,"reset_time":"59m57s"}}`, false},
// OmniRoute's, naming no model: the requested one
{"requested", failRate, `{"error":{"message":"All credentials for the requested model are cooling down","type":"rate_limit_error","code":"model_cooldown","reset_seconds":3597}}`, false},
{"credit", failCredit, `{"error":{"message":"All credentials for model model-a are cooling down via provider upstream (last error: insufficient balance)"}}`, true},
} {
forgetSinks(t)
a := key("model-a")
// the key's own run of rests of this kind, just ended: the
// model's rest climbs its own ladder, not the key's
restingUntil.Lock()
restingUntil.m[a.restKey()] = time.Now().Add(-time.Second)
restingUntil.note[a.restKey()] = Rest{Why: tc.why, Failures: 3}
restingUntil.Unlock()
h := header
if tc.noWait {
h = http.Header{}
}
r := s.restAfter(a, 429, h, []byte(tc.body))
if r.Why != tc.why {
t.Fatalf("%s: read as %q, want %q", tc.name, r.Why, tc.why)
}
if r.Key != a.restID() || r.Model != a.model {
t.Fatalf("%s: model-a's cooling-down rested %+v (model %q), not the model alone", tc.name, r, r.Model)
}
if _, ok := restOf(key("model-b").restID()); ok {
t.Fatalf("%s: model-a cooling down rested model-b", tc.name)
}
if _, ok := restOf(key("model-b").restKey()); ok {
t.Fatalf("%s: model-a cooling down rested the whole key", tc.name)
}
if _, ok := sunk.m[a.restKey()]; ok {
t.Fatalf("%s: model-a cooling down sank the whole key", tc.name)
}
if (tc.why == failRate || tc.why == failCredit) && r.Failures != 1 {
t.Fatalf("%s: model-a's rest is rung %d of the key's ladder, want the model's first", tc.name, r.Failures)
}
if tc.why == failRate {
if _, ok := sunk.m[a.restID()]; !ok {
t.Fatalf("%s: model-a, rate limited with quota left, didn't sink", tc.name)
}
}
if tc.why == failQuota {
// the model's own sentence is the model's rest's to keep
if kept, _ := restOf(a.restID()); !bytes.Contains([]byte(kept.said), []byte("model-a")) {
t.Fatalf("%s: model-a's rest keeps %q", tc.name, kept.said)
}
}
}
// an account-level cool-down that names no model rests the key: the
// model-scoped branch is for wordings that name one
forgetSinks(t)
whole := key("model-b")
if r := s.restAfter(whole, 429, header, []byte(`{"error":{"message":"All credentials are cooling down via provider upstream"}}`)); r.Key != whole.restKey() {
t.Fatalf("an account-level cool-down rested %+v, want the key", r)
}
}