diff --git a/internal/provider/allowance_pooled_test.go b/internal/provider/allowance_pooled_test.go index 0ecdc1c5f..ea6e134e7 100644 --- a/internal/provider/allowance_pooled_test.go +++ b/internal/provider/allowance_pooled_test.go @@ -97,3 +97,31 @@ func TestPluginPoolCountsBareIDOfSizes(t *testing.T) { t.Errorf("grok-4.8@1m: used %v, want 16 (Cursor Models)", used) } } + +// A model no window counts, on an account whose every window is some +// models' only — Antigravity's Gemini models that fetchAvailableModels +// gives no quota (#3737) beside Claude's own — is refused alone: nothing +// says the account is out, and Claude used up never stopped Gemini 4 +// either (runking on Discord). With a window of the whole account, its +// refusal is still the account's. +func TestAllowancePooledModelNoWindowCounts(t *testing.T) { + now := time.Now() + week := now.Add(5 * 24 * time.Hour) + agy := allowanceOf([]QuotaWindow{ + {Name: "Claude Sonnet 4.6", Model: "claude-sonnet-4-6", Family: "Claude", Used: 100, ResetsAt: &week}, + {Name: "Gemini 3 Flash", Model: "gemini-3-flash", Family: "Gemini", Used: 8, ResetsAt: &week}, + {Name: "5 hours", Pool: "Gemini", Used: 8, Aside: true}, + }, now) + for _, m := range []string{"gemini-4-argon-high", "claude-sonnet-4-6", "gemini-3-flash"} { + if !agy.Pooled(m, 100, now) { + t.Errorf("Antigravity: %s refused rests the account", m) + } + } + whole := Allowance{{Used: 30, Resets: week, Span: 7 * 24 * time.Hour}, {Used: 10, Resets: week, Model: "opus"}} + if whole.Pooled("gemini-4-argon-high", 100, now) { + t.Error("a model the account's own week counts, refused, rests only the model") + } + if (Allowance{{Used: 10, Resets: week, Model: "opus"}, {Used: 20, Resets: week}}).Pooled("x", 100, now) { + t.Error("with a window of the whole account, a model no window counts is refused alone") + } +} diff --git a/internal/provider/google.go b/internal/provider/google.go index 732cf326d..70e35577a 100644 --- a/internal/provider/google.go +++ b/internal/provider/google.go @@ -20,11 +20,13 @@ import ( "context" "crypto/sha256" "encoding/binary" + "encoding/hex" "encoding/json" "errors" "fmt" "io" "log" + "math" "net/http" "net/url" "os" @@ -1371,8 +1373,21 @@ func (g googleAccount) pools(ctx context.Context, ws []QuotaWindow) []QuotaWindo } type group struct { pool, about string - five bool // has its 5 hours, which stand in for its models' + five int // its 5 hours' window in out, which stand in for its models'; -1 without } + var shape []string // each group's name and buckets, for the log + for _, gr := range res.Groups { + var ids []string + for _, b := range gr.Buckets { + id := cmp.Or(b.BucketID, b.Window) + if b.Disabled { + id += " (disabled)" + } + ids = append(ids, id) + } + shape = append(shape, fmt.Sprintf("%q [%s]", gr.DisplayName, strings.Join(ids, ", "))) + } + noteQuotaSummary(g.user, shape) var out []QuotaWindow var groups []group for _, gr := range res.Groups { @@ -1380,7 +1395,7 @@ func (g googleAccount) pools(ctx context.Context, ws []QuotaWindow) []QuotaWindo if pool == "" { continue } - n, fiveHour := 0, false + n, five := 0, -1 for _, b := range gr.Buckets { if b.Disabled { continue @@ -1408,14 +1423,17 @@ func (g googleAccount) pools(ctx context.Context, ws []QuotaWindow) []QuotaWindo w.ResetsAt = &t w.ResetSecs = int64(max(time.Until(t), 0) / time.Second) } + if w.Span == modelSpan && five < 0 { + five = len(out) + } out = append(out, w) n++ - fiveHour = fiveHour || w.Span == modelSpan } if n > 0 { - groups = append(groups, group{pool, strings.ToLower(gr.DisplayName + " " + gr.Description), fiveHour}) + groups = append(groups, group{pool, strings.ToLower(gr.DisplayName + " " + gr.Description), five}) } } + own := map[string]bool{} // a model's own pool made below, by its name for i := range ws { if ws[i].Pool != "" || ws[i].Family == "" { continue @@ -1429,9 +1447,33 @@ func (g googleAccount) pools(ctx context.Context, ws []QuotaWindow) []QuotaWindo // GPT-OSS"), one with its 5 hours: without them its models' own // stand (#745); a group that says a version ("Gemini 4 Argon") is // its model's alone, never the family's + // + // A model whose own 5 hours read otherwise than the group's is + // not drawing on it: the summary left out the group it does draw + // on (Gemini 4 Argon on an account whose summary has no such + // group), and folded into the family's pool its reading would be + // hidden behind the family's (runking on Discord). It is a pool of + // its own, named as Google names the model. for _, gr := range groups { - if gr.five && !versioned(gr.pool) && strings.Contains(gr.about, strings.ToLower(ws[i].Family)) { - ws[i].Pool = gr.pool + if gr.five >= 0 && !versioned(gr.pool) && strings.Contains(gr.about, strings.ToLower(ws[i].Family)) { + if sameReading(ws[i], out[gr.five]) { + ws[i].Pool = gr.pool + } else if name := modelPool(ws[i]); name != "" { + ws[i].Pool = name + // its reading's span as its reset tells: Argon's, + // a week's (#4585), is past any 5 hours + w := ws[i] + w.Name, w.Span = "5 hours", modelSpan + if w.ResetsAt != nil && time.Until(*w.ResetsAt) > modelSpan+time.Minute { + w.Name, w.Span = "7 days", 7*24*time.Hour + ws[i].Span = w.Span + } + if !own[name] { + own[name] = true + w.Model, w.Family, w.Aside = "", "", true + out = append(out, w) + } + } break } } @@ -1439,6 +1481,51 @@ func (g googleAccount) pools(ctx context.Context, ws []QuotaWindow) []QuotaWindo return out } +// sameReading is whether a model's own 5 hours (m) read as its group's (p) +// do: the same share left and, both told, the same reset to a minute, as +// the two replies give one allowance. +func sameReading(m, p QuotaWindow) bool { + if math.Abs(m.Used-p.Used) > 0.5 { + return false + } + if m.ResetsAt == nil || p.ResetsAt == nil { + return true // a model's left out when its 5 hours haven't started + } + d := m.ResetsAt.Sub(*p.ResetsAt) + return d < time.Minute && d > -time.Minute +} + +// modelPool is the name of a pool of one model's own: its name as Google +// gives it, without the level ("Gemini 4 Argon (High)" is Gemini 4 +// Argon), so the levels of one model, read alike, share it. +func modelPool(w QuotaWindow) string { + name := strings.TrimSpace(w.Name) + if i := strings.Index(name, " ("); i > 0 { + name = strings.TrimSpace(name[:i]) + } + return name +} + +// summaryShapes is the last shape of each account's quota summary logged, +// by a hash of the account, so a change is logged once, not every read. +var summaryShapes sync.Map + +// noteQuotaSummary logs the groups and bucket ids a quota summary had, +// (parts), when they differ from the last read's: what a report of a missing or +// misnamed group needs. No token, no account: the account is a hash. +func noteQuotaSummary(user string, parts []string) { + text := strings.Join(parts, "; ") + if text == "" { + text = "no groups" + } + sum := sha256.Sum256([]byte(strings.ToLower(user))) + key := hex.EncodeToString(sum[:4]) + if old, ok := summaryShapes.Swap(key, text); ok && old == text { + return + } + log.Printf("antigravity account %s: quota summary groups: %s", key, text) +} + // poolNames is whether a quota group's name (pool) is a model's own: the // model's name or id begins with it and it says a version, as "Gemini 4 // Argon" does Gemini 4 Argon (High)'s; "Gemini" names a family, not a model. diff --git a/internal/provider/google_test.go b/internal/provider/google_test.go index 81ed5fdb5..310fd9d75 100644 --- a/internal/provider/google_test.go +++ b/internal/provider/google_test.go @@ -891,3 +891,91 @@ func TestAntigravityQuotaModelGroup(t *testing.T) { } } } + +// runking on Discord: an account whose summary has no Gemini 4 Argon +// group, only "Gemini Models" and "Claude and GPT models" (an Ultra +// account's, whose Argon quota Antigravity-Manager reads in Gemini's; or +// the prod endpoint's, CodexBar), while fetchAvailableModels gives +// gemini-4-argon a reading of its own, apart from the other Gemini +// models'. Folded into Gemini's pool, its reading was hidden behind +// Gemini's 92%: the card showed Gemini and Claude, no Gemini 4. A model +// read otherwise than its group is a pool of its own, named as Google +// names it; one read alike folds, as before; with the group in the +// summary, the group's (TestAntigravityQuotaModelGroup). +func TestAntigravityQuotaModelApartFromGroup(t *testing.T) { + at := func(d time.Duration) string { return time.Now().Add(d).UTC().Format(time.RFC3339) } + five, week := at(3*time.Hour), at(6*24*time.Hour) + gemini := `{"displayName":"Gemini Models","description":"Models within this group: Gemini Flash, Gemini Pro","buckets":[ + {"bucketId":"gemini-weekly","window":"weekly","remainingFraction":0.98,"resetTime":"` + week + `"}, + {"bucketId":"gemini-5h","window":"5h","remainingFraction":0.92,"resetTime":"` + five + `"}]}` + claude := `{"displayName":"Claude and GPT models","description":"Models within this group: Claude Opus, Claude Sonnet, GPT-OSS","buckets":[ + {"bucketId":"3p-weekly","window":"weekly","resetTime":"` + week + `"}, + {"bucketId":"3p-5h","window":"5h","resetTime":"` + five + `"}]}` + argonGroup := `{"displayName":"Gemini 4 Argon","description":"For paid Pro plans, Argon quota is separate.","buckets":[ + {"bucketId":"gemini-argon-wtu-weekly","window":"weekly","remainingFraction":0.53,"resetTime":"` + week + `","displayName":"Gemini 4 Argon Weekly Limit"}]}` + models := func(argon string) string { + return `{"models":{ + "gemini-3-flash":{"displayName":"Gemini 3 Flash","quotaInfo":{"remainingFraction":0.92,"resetTime":"` + five + `"}}, + "gemini-3.1-pro-high":{"displayName":"Gemini 3.1 Pro (High)","quotaInfo":{"remainingFraction":0.92,"resetTime":"` + five + `"}}, + "gemini-4-argon-high":{"displayName":"Gemini 4 Argon (High)","quotaInfo":` + argon + `}, + "gemini-4-argon-low":{"displayName":"Gemini 4 Argon (Low)","quotaInfo":` + argon + `}, + "claude-sonnet-4-6":{"displayName":"Claude Sonnet 4.6","quotaInfo":{"resetTime":"` + five + `"}}}}` + } + apart := `{"remainingFraction":0.53,"resetTime":"` + week + `"}` + alike := `{"remainingFraction":0.92,"resetTime":"` + five + `"}` + f := &fakeGoogle{ + load: `{"allowedTiers":[{"id":"free-tier","name":"Antigravity","isDefault":true}]}`, + onboard: `{"done":true,"response":{"cloudaicompanionProject":"ag-proj"}}`, + } + googleSandbox(t, f) + auth := googleAuth{AccessToken: "tok", RefreshToken: "rt-ag", Expiry: time.Now().Add(time.Hour).UnixMilli()} + if err := addGoogleLogin("antigravity", "ag@example.com", "", auth); err != nil { + t.Fatal(err) + } + argonPool := map[string]string{"gemini-3-flash": "Gemini", "gemini-3.1-pro-high": "Gemini", + "gemini-4-argon-high": "Gemini 4 Argon", "gemini-4-argon-low": "Gemini 4 Argon", "claude-sonnet-4-6": "Claude & GPT"} + folded := maps.Clone(argonPool) + folded["gemini-4-argon-high"], folded["gemini-4-argon-low"] = "Gemini", "Gemini" + for _, c := range []struct { + name, groups, argon string + pools map[string]string + pooled string + }{ + {"no Argon group, read apart (runking)", gemini + "," + claude, apart, argonPool, + "Gemini · 7 days,Gemini · 5 hours,Claude & GPT · 7 days,Claude & GPT · 5 hours,Gemini 4 Argon · 7 days"}, + {"no Argon group, read alike", gemini + "," + claude, alike, folded, + "Gemini · 7 days,Gemini · 5 hours,Claude & GPT · 7 days,Claude & GPT · 5 hours"}, + {"the Argon group", gemini + "," + claude + "," + argonGroup, apart, argonPool, + "Gemini 4 Argon (High),Gemini 4 Argon (Low),Gemini · 7 days,Gemini · 5 hours,Claude & GPT · 7 days,Claude & GPT · 5 hours,Gemini 4 Argon · 7 days"}, + } { + f.mu.Lock() + f.models, f.summary = models(c.argon), `{"groups":[`+c.groups+`]}` + f.mu.Unlock() + q := googleLogins("antigravity")[0].acct.quota(context.Background(), "") + pools := map[string]string{} + for _, w := range q.Windows { + if w.Model != "" { + pools[w.Model] = w.Pool + } + } + if !maps.Equal(pools, c.pools) { + t.Errorf("%s: pools %v, want %v", c.name, pools, c.pools) + } + var names []string + for _, w := range PooledWindows(q.Windows) { + names = append(names, w.Name) + } + if got := strings.Join(names, ","); got != c.pooled { + t.Errorf("%s: pooled\n%s\nwant\n%s", c.name, got, c.pooled) + } + for _, w := range q.Windows { + if w.Pool == "Gemini 4 Argon" && w.Model == "" && (w.Used < 46 || w.Used > 48 || !w.Aside) { + t.Errorf("%s: Argon's pool reads %+v, want 47%% used, aside", c.name, w) + } + } + // routing reads the models' own windows, none of the pools' + if a := allowanceOf(q.Windows, time.Now()); len(a) != 5 { + t.Errorf("%s: allowance has %d limits, want the 5 models'", c.name, len(a)) + } + } +} diff --git a/internal/provider/routing.go b/internal/provider/routing.go index 07bc3f0ec..3d5ae4e8f 100644 --- a/internal/provider/routing.go +++ b/internal/provider/routing.go @@ -629,6 +629,13 @@ func (a Allowance) Pooled(model string, share float64, now time.Time) bool { pooled = pooled || scoped whole = whole || !scoped } + if !pooled && !whole { + // no window counts model, as Antigravity's Gemini models that + // fetchAvailableModels gives no quota (#3737): when every window + // the account has is some models' only, nothing is known to be + // the whole account's, and the refusal is the model's + return len(a) > 0 && !slices.ContainsFunc(a, func(l Limit) bool { return l.Model == "" && l.matches == nil }) + } return full || pooled && !whole }