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.
This commit is contained in:
yetone
2026-11-11 20:06:08 +08:00
parent 7bcb161938
commit ac6db89339
2 changed files with 72 additions and 0 deletions
@@ -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)
}
}
+22
View File
@@ -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