gateway: an Antigravity account out of quota rests until the reset its 429 names, not 15 minutes (Dani Ariyanto)

Dani Ariyanto (Discord #feedback, 10-24): seven Antigravity accounts,
several ticked, one First, OpenCode on Gemini 3.8 Flash. With the First
account out, requests kept going to it instead of the next account, which
had all its quota.

Cause: Antigravity's 429 for a model's quota used up says when it is back
as a Go duration: "You have exhausted your capacity on this model. Your
quota will reset after 3h19m30s." (QUOTA_EXHAUSTED; the fixture
antigravityQuota, already in the tests since ea1c5d045). Nothing read
that form. The try was failQuota and the request failed over to the next
account, but the account rested only quotaRest (15 minutes). The
per-model window fetchAvailableModels gives is the 5-hour figure, so when
the weekly pool is the one spent, no window reads full either. Every 15
minutes for hours or days, the First account was asked again, refused,
and only then passed over.

What changed:
- routing.go: quotaResetAfter reads "quota will reset after <duration>",
  and resetsAfter returns it (beside Factory's "resets in ...") when it is
  above zero and no more than longestWritten. The rest is By "resets" for
  that long. As before, pooled decides whether the account or only that
  model rests. keepRetry passes it on as Retry-After when no account is
  left.
- docs/subsystems/gateway-routing.md: a bullet on the form.

Checked and not changed:
- Failover within one request already worked. A probe with two accounts,
  OpenCode's system prompt on /v1/chat/completions, streamed and not, with
  the literal 429, had u1 fail as quota and u2 answer 200. A per-model
  window the vendor reports at 0 (remainingFraction left out) benches the
  account until that window's reset after one try.
- Routing doesn't read the pool windows from retrieveUserQuotaSummary
  (Aside). That is unchanged.

Run, in a worktree at ed91aa04e:
- TestAntigravityQuotaRestsUntilItsReset is red on the old routing.go
  ("u1 rests 14m59.98s by "quota", not the 3h19m30s its quota resets
  after") and green with the change. It also checks that the next request
  asks only u2, plus reader cases: 45m2s, 26s, 155h2m1s, past 7 days (0),
  cut short (0), and the "check quota" 429 with no time (0).
- go test ./internal/gateway -count=1 -timeout 45m: ok (598s)
- go test ./... -timeout 45m: not run before this push. Another
  session's full suite was running here; it runs after the push.
- gofmt -l clean. GOOS=linux go vet -tags nogui ./... and GOOS=windows go
  vet ./... clean.

Not tried: a real Antigravity account out of quota (none spent locally),
so the 429's words come from the existing fixture, not a fresh capture.
RetryInfo's retryDelay detail isn't read.
This commit is contained in:
yetone
2026-10-24 15:31:51 +08:00
parent 1d6b72ada8
commit ef1a203df9
3 changed files with 83 additions and 3 deletions
+1
View File
@@ -137,6 +137,7 @@ recorded as a prediction failure (`noPrediction`).
- A sub2api key out of its own limit answers 429 `api key 7天限额已用完` (or `日限额`, `5小时限额`) as `rate_limit_exceeded`. It is out of quota (`plannedWords`), not rate limited: it rests till the window it filled renews when its windows as last read say so, else `quotaRest`, and its windows are read again (`staleKeyAllowance`). A reading that finds that window full no more (the limit raised in the relay's panel, or the key's usage reset) lifts the rest (`renewed`, told through `provider.OnRenewed`), as a Codex reset spent does an account's; the key's other rests stay. While it rests out of its windows, they are read again each time it is planned (`restingOf`, once a minute at most), so this holds where nothing weighs it by them: an ordered group. The relay out of accounts for everyone (503 `No available accounts`, or a later version's 429 `All available accounts are currently rate-limited`) says nothing of the key's windows: it backs off as any failure or rate limit does, never to the window. A key's windows count only its own spend, so other users draining the relay's accounts show up as those errors, not in the windows.
- 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.
- 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`).
+60
View File
@@ -7,6 +7,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
@@ -535,3 +536,62 @@ func TestAntigravityTurnedAwayHintIsTheRoutingPages(t *testing.T) {
t.Fatal("plugins/routing/client.js doesn't know the note the gateway adds to Antigravity's refusal")
}
}
// Antigravity's 429 for a model's quota used up says when it is back,
// "Your quota will reset after 3h19m30s.", and the account rests that long:
// read as a quota that says nothing, it rested 15 minutes, and the account
// first in order was asked again, and refused, every quarter of an hour
// while the next ticked one had all its quota left (Dani Ariyanto on
// Discord, 10-24: OpenCode on Gemini 3.8 Flash, seven accounts).
func TestAntigravityQuotaRestsUntilItsReset(t *testing.T) {
var asked []string
s, _ := antigravityGroupFor(t, []string{"u1@example.com", "u2@example.com"}, "gemini-3.8-flash", func(w http.ResponseWriter, r *http.Request) {
who := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
asked = append(asked, who)
if who == "u1@example.com" {
w.WriteHeader(http.StatusTooManyRequests)
io.WriteString(w, antigravityQuota)
return
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"from u2"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":3}}}`))
})
turn := func(n int) (int, string) {
body := fmt.Sprintf(`{"model":"antigravity/gemini-3.8-flash","stream":false,"messages":[{"role":"system","content":"You are opencode, an interactive CLI tool."},{"role":"user","content":"hi %d"}]}`, n)
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body)))
return rec.Code, rec.Body.String()
}
if code, out := turn(1); code != 200 || !strings.Contains(out, "from u2") || !slices.Equal(asked, []string{"u1@example.com", "u2@example.com"}) {
t.Fatalf("the turn: %d %s, asked %v", code, out, asked)
}
r := lastRoute(s)
if len(r.Tries) != 2 || r.Tries[0].Fail != failQuota || r.Tries[0].Rest == nil {
t.Fatalf("the used-up try: %+v", r.Tries)
}
want := 3*time.Hour + 19*time.Minute + 30*time.Second
if d := time.Until(r.Tries[0].Rest.Until); r.Tries[0].Rest.By != "resets" || d < want-time.Minute || d > want {
t.Fatalf("u1 rests %v by %q, not the %v its quota resets after", d, r.Tries[0].Rest.By, want)
}
asked = nil
if code, _ := turn(2); code != 200 || !slices.Equal(asked, []string{"u2@example.com"}) {
t.Fatalf("the next turn: %d, asked %v", code, asked)
}
for _, c := range []struct {
body string
want time.Duration
}{
{antigravityQuota, want},
{`{"error":{"code":429,"message":"You have exhausted your capacity on this model. Your quota will reset after 45m2s.","status":"RESOURCE_EXHAUSTED"}}`, 45*time.Minute + 2*time.Second},
{`{"error":{"code":429,"message":"You have exhausted your capacity on this model. Your quota will reset after 26s.","status":"RESOURCE_EXHAUSTED"}}`, 26 * time.Second},
{`{"error":{"code":429,"message":"You have exhausted your capacity on this model. Your quota will reset after 155h2m1s.","status":"RESOURCE_EXHAUSTED"}}`, 155*time.Hour + 2*time.Minute + time.Second},
{`{"error":{"code":429,"message":"Your quota will reset after 200h0m0s.","status":"RESOURCE_EXHAUSTED"}}`, 0}, // past longestWritten
{`{"error":{"code":429,"message":"You have exhausted your capacity on this model. Your quota will reset after ","status":"RESOURCE_EXHAUSTED"}}`, 0},
{turnedAway, 0},
} {
if got := resetsAfter([]byte(c.body)); got != c.want {
t.Errorf("%s: %v, want %v", c.body, got, c.want)
}
}
}
+22 -3
View File
@@ -749,10 +749,29 @@ func resetsZhipu(body []byte, now time.Time) time.Duration {
// them, each a number and its unit, and nothing else between.
var resetsInWords = regexp.MustCompile(`(?i)\bresets? in (?:(\d+)\s*d(?:ays?)?\b\s*)?(?:(\d+)\s*h(?:ours?|rs?)?\b\s*)?(?:(\d+)\s*m(?:in(?:utes?|s)?)?\b)?`)
// resetsAfter is how long the refusal says until it is back (resetsInWords),
// held to longestWritten. Zero when it doesn't say.
// quotaResetAfter: Antigravity's (Google Code Assist's) 429 for a model's
// allowance used up, which 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." Unread, the account rested the quarter of
// an hour of a quota that says nothing, and the account first in order
// was asked again, and refused, every 15 minutes for the hours or days
// its quota was out (Dani Ariyanto on Discord, 10-24).
var quotaResetAfter = regexp.MustCompile(`(?i)\bquota will reset after ((?:\d+h)?(?:\d+m)?(?:\d+(?:\.\d+)?s)?)`)
// resetsAfter is how long the refusal says until it is back
// (resetsInWords, quotaResetAfter), held to longestWritten. Zero when it
// doesn't say.
func resetsAfter(body []byte) time.Duration {
return spanWritten(resetsInWords.FindSubmatch(wordsOf(body)))
body = wordsOf(body)
if d := spanWritten(resetsInWords.FindSubmatch(body)); d > 0 {
return d
}
if m := quotaResetAfter.FindSubmatch(body); m != nil && len(m[1]) > 0 {
if d, err := time.ParseDuration(string(m[1])); err == nil && d > 0 && d <= longestWritten {
return d
}
}
return 0
}
// clineModelLimited says c, a provider whose requests reach Cline's API,