From ac6db89339a9e83fd42f02ba9740090ddbf6185c Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 20:06:08 +0800 Subject: [PATCH] gateway: a Grok "usage balance exhausted" 402 rests to the spent window's reset, not 30 minutes (@justacati on X) Grok Build answers a spent allowance with 402 "usage balance exhausted". "balance" matched creditWords, so the account rested as out of credit for creditRest (30 minutes) and was asked again, refused again, though the usage card knew the weekly window renews in 4 days. For a Grok plugin account (plugin:grok, plugin:grok-plugin) that 402 is now failQuota, which rests to the full window's reset (By "window"), and to the 15-minute quota rest only when no window is known. Other vendors' 402s and Grok's other 402s (payment) are unchanged. Run: TestGrokBalanceExhaustedRestsToReset red on the old code (credit, 30m), green now; TestFactoryStandardOut*, TestResetsInWords and the Rest tests of internal/gateway green; go vet darwin/windows/linux on internal/gateway. Not run: full suite; a real exhausted Grok account. --- internal/gateway/grok_balance_rest_test.go | 50 ++++++++++++++++++++++ internal/gateway/routing.go | 22 ++++++++++ 2 files changed, 72 insertions(+) create mode 100644 internal/gateway/grok_balance_rest_test.go diff --git a/internal/gateway/grok_balance_rest_test.go b/internal/gateway/grok_balance_rest_test.go new file mode 100644 index 000000000..1c135e2c6 --- /dev/null +++ b/internal/gateway/grok_balance_rest_test.go @@ -0,0 +1,50 @@ +package gateway + +import ( + "net/http" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +const grokBalanceOut402 = `{"error":{"message":"Grok: Grok Build usage balance exhausted"}}` + +// A Grok account refused with "usage balance exhausted" rests until its +// spent window renews, not 30 minutes "for credit" (@justacati on X: weekly +// 7 days 0% left, 4 days to reset, "402 余额不足 · 19:58 恢复"). +func TestGrokBalanceExhaustedRestsToReset(t *testing.T) { + old, oldAwait := allowances, awaitAllowance + defer func() { allowances, awaitAllowance = old, oldAwait }() + fresh(t) + now := time.Now() + reset := now.Add(4 * 24 * time.Hour) + al := map[string]provider.Allowance{"a@x.com": {{Used: 100, Resets: reset, Span: 7 * 24 * time.Hour}}} + allowances = func(string) map[string]provider.Allowance { return al } + grok := func(agent string) candidate { + return candidate{p: provider.Provider{ID: "grok", Account: &provider.Account{Agent: agent, User: "a@x.com"}}, model: "grok-4.7", rest: "grok"} + } + r := (&Server{}).restAfter(grok("plugin:grok"), 402, http.Header{}, []byte(grokBalanceOut402)) + if d := time.Until(r.Until); r.Why != failQuota || r.By != "window" || d < 4*24*time.Hour-time.Minute || d > 4*24*time.Hour+time.Minute { + t.Fatalf("rests %v: %+v, want out of quota to the weekly reset", d, r) + } + + // no window known: the short rest, not a week + al = nil + r = (&Server{}).restAfter(grok("plugin:grok"), 402, http.Header{}, []byte(grokBalanceOut402)) + if d := time.Until(r.Until); r.Why != failQuota || d > time.Hour { + t.Fatalf("no window known rests %v: %+v", d, r) + } + + // another vendor's 402 with the same words, and Grok's own payment 402 + // are still out of credit + al = map[string]provider.Allowance{"a@x.com": {{Used: 100, Resets: reset, Span: 7 * 24 * time.Hour}}} + r = (&Server{}).restAfter(grok("plugin:other"), 402, http.Header{}, []byte(grokBalanceOut402)) + if r.Why != failCredit { + t.Fatalf("another vendor's 402 rests %+v, want credit", r) + } + r = (&Server{}).restAfter(grok("plugin:grok"), 402, http.Header{}, []byte(`{"error":{"message":"Payment required"}}`)) + if r.Why != failCredit { + t.Fatalf("Grok's payment 402 rests %+v, want credit", r) + } +} diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go index 10f242d67..4cb23fedd 100644 --- a/internal/gateway/routing.go +++ b/internal/gateway/routing.go @@ -694,9 +694,31 @@ func failureOf(c candidate, status int, body []byte) string { if dashScopeRate(c.p, status, body) { return failRate } + if grokBalanceOut(c.p, status, body) { + return failQuota + } return failure(status, body) } +// Grok Build's 402 for a plan's allowance spent says "Grok Build usage +// balance exhausted". Its "balance" reads as money (creditWords), but the +// allowance is a window that renews, and the usage card knows when. + +// grokBalanceOut says p, a Grok account (the plugin's, either id), was +// refused with a 402 because its allowance is used up. That rests as out of +// quota, until the window it filled renews (restAfter), not as out of +// credit for half an hour (@justacati on X). Another vendor's 402 is its own. +func grokBalanceOut(p provider.Provider, status int, body []byte) bool { + if status != http.StatusPaymentRequired || p.Account == nil { + return false + } + switch p.Account.UsageAgent() { + case "plugin:grok", "plugin:grok-plugin": + return bytes.Contains(bytes.ToLower(wordsOf(body)), []byte("usage balance exhausted")) + } + return false +} + // restrictedSaid says an error is one magpie already put in words as an // account its vendor restricted: WorkBuddy's 11140 as the built-in // translates it, or as the plugin does, both ending in