diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go index 94b8745c9..322411d3f 100644 --- a/internal/gateway/routing.go +++ b/internal/gateway/routing.go @@ -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 diff --git a/internal/gateway/routing_test.go b/internal/gateway/routing_test.go index 38b4fe21f..1d02537e7 100644 --- a/internal/gateway/routing_test.go +++ b/internal/gateway/routing_test.go @@ -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) + } +}