Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a ZCode Start Plan trial that ended or is spent rests that trial alone, not the account (#3661)
yetone65ee8c65c6b4
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex 3be021438..62c0c83a2 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -203,6 +203,7 @@ recorded as a prediction failure (`noPrediction`).- A 5xx is a server error, not quota, unless the vendor's own quota code says so (`quotaCodes`: `usage_limit_reached`, `RESOURCE_EXHAUSTED`, Claude Code's `usage limit reached|<unix>`). A relay's 500 "当前模型 gpt-5.5 负载已经达到上限,请稍后重试" names its load, not the key's allowance: it was read as out of quota by `usedUpWords` (上限), which rested the key till a reset and kept a group's Patience from waiting it out (#1418). The quota words (`usedUpWords`) still apply to a 4xx.- A 429 that names in words when the account is back (WorkBuddy's "将在 2026-10-11 20:16:02 UTC+8重置" and "your usage will reset at 2026-10-11 12:37:53 UTC+8, …", #1669; Volcengine Ark's "You have exceeded the monthly usage quota. It will reset at 2026-10-25 23:59:59 +0800 CST.", #3251, #3276) is out of quota in either language (`resetsWhen`), and rests until that time (`resetsWritten`, read in the offset written). Only the reset phrasing right beside a whole date and time with its UTC/GMT offset, or its offset as Go prints one (four digits, then the zone's name, which isn't read), followed by 重置 or punctuation, is read, so a refusal cut short isn't read as UTC and another vendor's date in passing is no reset; a time past, not a real date, or further off than `longestSaid` isn't read: `longestWritten` (7 days), or `longestMonthly` (35 days) when the refusal says its allowance is a month's (`monthlyWords`: Ark's, Zhipu's 1310 "Weekly/Monthly Limit Exhausted"). Such a reset is the one rest out of quota held past `longestQuota`, to `longestMonthly`; a window read full or the user's own rest is still held to 8 days (`TestMonthlyWrittenResetRestsTheMonth`). Ark's windows as its AccessKey reads them (`volcPlanQuotas`) still reach only its usage card, not routing (#3251). Zhipu's 1308 and 1310 (Z.ai and bigmodel.cn, GLM Coding Plan: "Your limit will reset at 2026-10-15 20:12:13", "您的限额将在 … 重置") write the time with no zone; it is Beijing's, and is read as UTC+8 in those words only (`zhipuResets`, `resetsZhipu`), never when a digit, colon or zone of its own follows (Player on Discord: read by nothing, the key rested `quotaRest` and the refusal said "not asked again before 17:12" while Z.ai said 20:12; `TestZhipuRestsUntilItsReset`). One that points to the account's other models (`otherModels`: "切换其他模型", "switch to the other models") rests that model alone (`restID`). Out of credit or quota on a model that costs credits, where the subscription serves other models free (`catalog.Model.Free`: WorkBuddy's x0.00, Qoder's is_free, Cline's free models; `Provider.PaidBesideFree`), rests that model alone too: the free ones draw on no credits (Creat.wu on Discord, `TestOutOfCreditLeavesFreeModels`). A Credits window leaves the free models out the same way, so an account at 100% isn't ranked spent for them: the WorkBuddy plugin's lists them as `notModels` (0.1.23), and the built-in's scopes itself by the provider's last fetched list (`wbSpareFree`; with no list read, every model counts) (plugins#138, hheei: an account at 560 / 560 was still served deepseek-v4.1-flash; `TestWorkBuddyCreditsSpareFreeModels`). A free model the vendor refuses all the same (WorkBuddy refuses even 0-rate models to an account with no package) rests the account on its own refusal. Cline's daily limit of one free model, 429 "Daily free limit reached on model deepseek/deepseek-v4-flash. Try again in 23h 59m" (the words Cline's own clients read it by, `isClineFreeModelLimitMessage`), rests that model alone (`clineFreeLimit`, `clineModelLimited`, keyed by `restID`) until the time it names (`clineBackIn`, `tryAgainIn`, held to `longestWritten`; `resets`), from a provider whose requests reach Cline's API (`ClinePinnable`: the ClinePass preset, one at cline.bot, the Cline plugin's). The key's other models, free or the plan's, are still served, and the model out of its day is answered with its refusal without asking Cline again (#1398, wuasakura: one free model's 429 sent the whole Cline account to the back; `TestClineFreeLimitRestsTheModel`, `TestClineFreeLimitLeavesOtherModels`). Another vendor's same words, and a Cline 429 that doesn't name the model's limit, rest the key as before. WorkBuddy's 429 14018 "Credits exhausted" is out of credit (`brokeWords`), not a rate limit, on the built-in's raw body and the plugin's `insufficient_quota` alike (`TestCreditsExhaustedIsOutOfCredit`). On the rate-limit branch, a reset time the refusal names (`resetsAt`, or `resetsHeader` as `keepRetry` noted it) is believed up to `longestQuota`, as out of quota believes it, and the Routing page says it rests until then (`resets`); a `Retry-After` or a rate-limit reset header is still held to `longestWait` (an hour) (`rateRest`). The same rest runs for a plugin's requests: the WorkBuddy plugin passes these 429s on as they come (`TestPluginWrittenResetRests`). A JSON error body that escapes what isn't ASCII as `\uXXXX` (Python's json.dumps does by default) has its strings decoded before any word list (`creditWords`, `rateWords`, `plannedWords`, `usedUpWords`, `resetsWhen`, …) is matched (`wordsOf`), so an escaped 余额不足 or 用量 reads as an unescaped one does.- Antigravity's 429 for a model's quota used up writes how long until it is back as Go prints a duration: "You have exhausted your capacity on this model. Your quota will reset after 3h19m30s." (`quotaResetAfter`, read by `resetsAfter` beside Factory's "resets in …", held to `longestWritten`). The account (or, as `pooled` says, that model on it) rests that long (`resets`), and the agent's Retry-After says it when no account is left. Unread, it rested `quotaRest`, so the account first in order was asked again, and refused, every 15 minutes while the next ticked account had its quota (Dani Ariyanto on Discord, 10-24; `TestAntigravityQuotaRestsUntilItsReset`). Its other 429, "Resource has been exhausted (e.g. check quota).", names no time and rests `quotaRest` as before.+- A ZCode plugin account's Start Plan trial entry (`<model>-Trial`) that the plugin refuses before sending anything rests that entry alone (`zcodeTrialRefused`, `restID`), not the account: a 400 "ZCode has no active limited quota for GLM-5.3-Trial" when no live plan holds it any more (the trial ended), and a 429 "ZCode's limited quota for glm-5.3 is exhausted or temporarily unavailable" when its bucket is spent (the plugin names the bucket, the entry less `-Trial`). Read as `failQuota` by their words, both rested `zcode@…`, and the account's GLM Coding Plan models were paused with the trial (#3661, orion-nebula-dev; after skygunner's e04a1b994, which covered the 400). Only the plugin's exact words naming the model asked for count: another model named, a model without `-Trial`, the words on another status, the account's own quota or key, and the same words from another vendor still rest the account (`TestZCodeTrialRefusalRestsTheTrialAlone`). An expired trial still kept in a group is asked first and refused locally, as the guessed quota rest is asked again a minute on; the Routing page strikes it as skipped, which the gateway doesn't do for a member no account lists (#3805).- An account resting out of its window (a Claude account by the time Claude Code's refusal named, `resets`, or any by `window`) is back once magpie's reading of its windows (`provider.Allowances`) finds it full till sooner: the window used below `SpentShareOf`, or its reset passed, with no other window over the same models still full. The reading tells `provider.OnRenewed` as the account's `UsageAgent` and user, and `renewed` lifts that account's rests out of quota or by `window`; its rests for a rate limit, its credit, a verification or a failure stay, as a key's do, and so does every rest of other accounts still out of their windows. Planning reads every account candidate's allowance, ordered groups included, so this needs no step of its own in `restingOf`.- Only an account is held at a usage cap (`capHeld`) or rested out of one model's pool (`pooled`); a key's windows order it and rest it, nothing more.- A rest of one model alone (keyed by `restID`: a pool, a model not served, a paid model beside free ones) names that model in the trace (`Rest.Model`). The Routing page shows it on that model's rows only: the account's other models' rows on the stage, and the account's row under Accounts and keys, don't read as resting. The account's row names the models resting on a line of their own under what it has left, and its Try now lifts their rests. A kept request from before `Rest.Model` has the model on the end of `Rest.Key`, which the page reads the same (`restModel` in `plugins/routing/client.js`, #1960, `routing-model-rest.test.cjs`).diff --git a/internal/gateway/routing.go b/internal/gateway/routing.goindex 7aee70698..44ca2bb3e 100644--- a/internal/gateway/routing.go+++ b/internal/gateway/routing.go@@ -1127,6 +1127,11 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, boif why == failQuota && clineModelLimited(c, body) {id = c.restID()}+ // a ZCode Start Plan trial entry ended or spent is that entry's: the+ // account's GLM Coding Plan models still answer (#3661)+ if why == failQuota && zcodeTrialRefused(c, status, 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.wudiff --git a/internal/gateway/zcode_trial.go b/internal/gateway/zcode_trial.gonew file mode 100644index 000000000..7ed73f43c--- /dev/null+++ b/internal/gateway/zcode_trial.go@@ -0,0 +1,46 @@+package gateway++import (+ "net/http"+ "regexp"+ "strings"+)++// zcodeEndedWords and zcodeSpentWords are the ZCode plugin's own answers+// for a Start Plan trial entry (<model>-Trial) it can't serve, given before+// anything is sent: a 400 "ZCode has no active limited quota for+// GLM-5.3-Trial" when no live plan holds the model any more (the trial+// ended), and a 429+// "ZCode's limited quota for glm-5.3 is exhausted or temporarily+// unavailable" when its bucket is spent. The plugin's words, after any+// "<provider>: " put in front of them.+var (+ zcodeEndedWords = regexp.MustCompile(`(?:^|: )ZCode has no active limited quota for (\S+)$`)+ zcodeSpentWords = regexp.MustCompile(`(?:^|: )ZCode's limited quota for (\S+) is exhausted or temporarily unavailable$`)+)++// zcodeTrialRefused says c, a ZCode plugin account asked for one of its+// Start Plan trial entries, was refused for that entry's own quota+// (zcodeEndedWords, zcodeSpentWords), naming the model asked for. The+// trial is that model's: the account's GLM Coding Plan still serves its+// own models, so only the trial entry rests (#3661, orion-nebula-dev: two+// expired trials rested "zcode@zcode", and GLM-5.3 and GLM-5.3-Flash with+// it). Any other+// refusal of a ZCode account, an account-wide 429 or 1113 among them,+// rests the account as before.+func zcodeTrialRefused(c candidate, status int, body []byte) bool {+ if c.p.PluginProvider() != "zcode" || !strings.HasSuffix(strings.ToLower(c.model), "-trial") {+ return false+ }+ said := strings.TrimSpace(vendorMessage(body))+ switch status {+ case http.StatusBadRequest:+ m := zcodeEndedWords.FindStringSubmatch(said)+ return m != nil && strings.EqualFold(m[1], c.model)+ case http.StatusTooManyRequests:+ // the plugin names the bucket's own model, the entry less -Trial+ m := zcodeSpentWords.FindStringSubmatch(said)+ return m != nil && strings.EqualFold(m[1]+"-Trial", c.model)+ }+ return false+}diff --git a/internal/gateway/zcode_trial_test.go b/internal/gateway/zcode_trial_test.gonew file mode 100644index 000000000..8214b0dcb--- /dev/null+++ b/internal/gateway/zcode_trial_test.go@@ -0,0 +1,143 @@+package gateway++import (+ "io"+ "net/http"+ "strings"+ "sync"+ "testing"++ "github.com/yetone/magpie/internal/provider"+)++// The ZCode plugin's refusals of a Start Plan trial entry, as it writes them+// (zcode 0.1.31's fetch, JSON.stringify of its error): the trial ended, in+// the words of #3661's routing log, and a trial's bucket spent.+const (+ zcodeFlashTrialEnded = `{"type":"error","error":{"type":"invalid_request_error","message":"ZCode has no active limited quota for GLM-5.3-Flash-Trial"}}`+ zcodeTrialEnded = `{"type":"error","error":{"type":"invalid_request_error","message":"ZCode has no active limited quota for GLM-5.3-Trial"}}`+ zcodeTrialSpent = `{"type":"error","error":{"type":"rate_limit_error","message":"ZCode's limited quota for glm-5.3 is exhausted or temporarily unavailable"}}`+)++// A ZCode account's Start Plan trial entry that ended, or whose bucket is+// spent, rests that entry alone: the next request for the account's GLM+// Coding Plan model is still served by it (#3661,+// orion-nebula-dev: GLM-5.3-Flash-Trial and GLM-5.3-Trial rested+// "zcode@zcode", and GLM-5.3 and GLM-5.3-Flash with it). A refusal of the+// account — its quota, its key, another model named, a model that isn't a+// trial — and the same words from another vendor still rest the account.+// After skygunner's TestZCodeTrialQuotaRestsTheModelAlone (e04a1b994).+func TestZCodeTrialRefusalRestsTheTrialAlone(t *testing.T) {+ for _, tc := range []struct {+ name, vendor, model, body string+ status int+ listed bool // the plugin still lists the trial entry+ accountRest bool+ }{+ {"flash trial ended", "zcode", "GLM-5.3-Flash-Trial", zcodeFlashTrialEnded, 400, false, false},+ {"trial ended", "zcode", "GLM-5.3-Trial", zcodeTrialEnded, 400, false, false},+ {"trial ended, still listed", "zcode", "GLM-5.3-Trial", zcodeTrialEnded, 400, true, false},+ {"trial spent", "zcode", "GLM-5.3-Trial", zcodeTrialSpent, 429, true, false},+ {"account quota", "zcode", "GLM-5.3", `{"error":{"type":"rate_limit_error","message":"quota exhausted"}}`, 429, false, true},+ {"account quota 400", "zcode", "GLM-5.3", `{"error":{"type":"invalid_request_error","message":"quota exhausted for this account"}}`, 400, false, true},+ {"account key", "zcode", "GLM-5.3", `{"error":{"message":"invalid api key"}}`, 401, false, true},+ {"another trial named", "zcode", "GLM-5.3-Flash-Trial", zcodeTrialEnded, 400, false, true},+ {"spent names another", "zcode", "GLM-5.3-Flash-Trial", zcodeTrialSpent, 429, true, true},+ {"not a trial", "zcode", "GLM-5.3", `{"type":"error","error":{"type":"invalid_request_error","message":"ZCode has no active limited quota for GLM-5.3"}}`, 400, false, true},+ {"ended words on a 429", "zcode", "GLM-5.3-Trial", zcodeTrialEnded, 429, false, true},+ {"another vendor's words", "factory", "GLM-5.3-Trial", zcodeTrialEnded, 400, false, true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ // a ZCode list with no free model, which would rest a paid+ // model alone whatever it said (PaidBesideFree)+ t.Setenv("FAKE_ZCODE", "1")+ t.Setenv("FAKE_NO_FREE", "1")+ if tc.listed {+ t.Setenv("FAKE_ZCODE_TRIAL", "1")+ }+ var mu sync.Mutex+ var asked []string+ good := &adaptiveVendor{}+ movedFake(t, tc.vendor, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ body, err := io.ReadAll(r.Body)+ if err != nil {+ t.Error(err)+ w.WriteHeader(500)+ return+ }+ model := modelOf(body)+ mu.Lock()+ asked = append(asked, model)+ mu.Unlock()+ if model == tc.model {+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(tc.status)+ io.WriteString(w, tc.body)+ return+ }+ r.Body = io.NopCloser(strings.NewReader(string(body)))+ good.ServeHTTP(w, r)+ }))+ p, err := provider.Find(tc.vendor)+ if err != nil {+ t.Fatal(err)+ }+ c := candidate{p: *p, rest: tc.vendor, model: tc.model}+ // the account's windows were read, so its rest isn't narrowed+ // later by a read that comes in+ old := allowances+ allowances = func(string) map[string]provider.Allowance {+ return map[string]provider.Allowance{p.Account.User: {{Used: 10}}}+ }+ t.Cleanup(func() { allowances = old })+ b := &scripted{replies: []reply{{200, "", chatOK}, {200, "", chatOK}}}+ scriptedOn(t, "b", provider.Chat, b)+ // the trial, then the account's own model, each with another+ // provider behind it+ for name, first := range map[string]string{"T": tc.model, "S": "GLM-5.3"} {+ if err := provider.SaveGroup(provider.Group{Name: name, Members: []string{tc.vendor + "/" + first, "b/m"}, Routing: provider.Ordered}); err != nil {+ t.Fatal(err)+ }+ }+ s := New()+ // the trial refused, and the other provider answers+ code, body := postAs(t, s, "", `{"model":"group/t","messages":[{"role":"user","content":"hi"}]}`)+ if code != 200 {+ t.Fatalf("trial request: %d %s", code, body)+ }+ tries := s.trace.routes[len(s.trace.routes)-1].Tries+ if len(tries) != 2 || tries[0].Status != tc.status || tries[0].Rest == nil {+ t.Fatalf("trial request tries: %+v", tries)+ }+ want := c.restID()+ if tc.accountRest {+ want = c.restKey()+ }+ if tries[0].Rest.Key != want {+ t.Fatalf("the refusal rested %q, want %q", tries[0].Rest.Key, want)+ }+ if _, ok := restOf(c.restKey()); ok != tc.accountRest {+ t.Fatalf("account resting = %v, want %v", ok, tc.accountRest)+ }+ if _, ok := restOf(c.restID()); !tc.accountRest && !ok {+ t.Fatalf("%s isn't resting", tc.model)+ }+ // then the account's own GLM-5.3: served by the account unless+ // the account was refused+ if code, body := postAs(t, s, "", `{"model":"group/s","messages":[{"role":"user","content":"hi"}]}`); code != 200 {+ t.Fatalf("GLM-5.3 request: %d %s", code, body)+ }+ mu.Lock()+ defer mu.Unlock()+ if tc.accountRest {+ if b.n != 2 || len(asked) != 1 {+ t.Fatalf("account fault: asked %v, the other provider %d times", asked, b.n)+ }+ return+ }+ if b.n != 1 || len(asked) != 2 || asked[1] != "GLM-5.3" {+ t.Fatalf("the trial's refusal kept the account's GLM-5.3 out: asked %v, the other provider %d times", asked, b.n)+ }+ })+ }+}diff --git a/internal/plugin/testdata/fake/index.js b/internal/plugin/testdata/fake/index.jsindex dab055e0f..8cc085d2a 100644--- a/internal/plugin/testdata/fake/index.js+++ b/internal/plugin/testdata/fake/index.js@@ -109,6 +109,12 @@ export const FakePlugin = async ({ client }) => ({const budget = { low: 4096, high: 16000, max: 31999 }const variants = Object.fromEntries(["low", "high", "max"].map((e) => [e, { thinking: { type: "enabled", budgetTokens: budget[e] } }]))cfg.provider[ID].models["GLM-5.3"] = { name: "GLM-5.3", provider: { npm: "@ai-sdk/anthropic" }, reasoning: true, variants, limit: { context: 1000000, output: 128000 } }+ // $FAKE_ZCODE_TRIAL: the Start Plan trial models beside the+ // subscription's, as a ZCode account that once had them lists them+ if (process.env.FAKE_ZCODE_TRIAL) {+ cfg.provider[ID].models["GLM-5.3-Flash-Trial"] = { name: "GLM 5.3 Flash Trial", provider: { npm: "@ai-sdk/anthropic" }, reasoning: true, limit: { context: 1000000, output: 128000 } }+ cfg.provider[ID].models["GLM-5.3-Trial"] = { name: "GLM 5.3 Trial", provider: { npm: "@ai-sdk/anthropic" }, reasoning: true, limit: { context: 1000000, output: 128000 } }+ }}// $FAKE_SEARCHES: the provider's upstream searches by itself (#2027),// as a plugin in front of Claude's API says@@ -291,8 +297,9 @@ export const FakePlugin = async ({ client }) => ({if (auth?.type === "oauth" && auth.refresh?.startsWith("rot-")) {await client.auth.set({ path: { id: ID }, body: { ...auth, refresh: auth.refresh + "+" } })}- // fake-1 costs the plan nothing, as a WorkBuddy model of x0.00 credits- p.models["fake-1"].free = true+ // fake-1 costs the plan nothing, as a WorkBuddy model of x0.00+ // credits ($FAKE_NO_FREE: a list with no free model, as ZCode's is)+ if (!process.env.FAKE_NO_FREE) p.models["fake-1"].free = true// fake-claude's credits are a number, discounted; fake-gemini's as// WorkBuddy's picker writes themif (p.models["fake-claude"]) Object.assign(p.models["fake-claude"], { rate: 0.5, rateWas: 1 })