From 6a409a87cb83907b9e6946ab86a34d723b63fb79 Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Thu, 19 Nov 2026 11:01:25 -0600 Subject: [PATCH 1/2] Refactor: Generalize quota fetchers to return multiple reports Each quota fetcher currently produces one report card and always replaces its ID with the fetcher's default ID. Google AI subscriptions divide usage into independent model groups that need separate cards so each group ranks by its own waste and hides shorter windows only when that group's longer window is full. Change-Id: I2bb60257238f1c243f4ec52efa40128d5c8ceeab --- internal/quotas/collect.go | 104 +++++++++++++++++++++------------- internal/quotas/fetch_test.go | 13 +++-- 2 files changed, 72 insertions(+), 45 deletions(-) diff --git a/internal/quotas/collect.go b/internal/quotas/collect.go index 7b27604..51e9133 100644 --- a/internal/quotas/collect.go +++ b/internal/quotas/collect.go @@ -15,7 +15,7 @@ const ( type fetcher struct { id, name string - fetch func(context.Context) (Report, error) + fetch func(context.Context) ([]Report, error) } // Collector shares one read of every service. @@ -62,15 +62,12 @@ func (c *Collector) clock() time.Time { } func (c *Collector) read(ctx context.Context) (Snapshot, error) { - results := make([]Report, len(c.fetchers)) - keep := make([]bool, len(c.fetchers)) + results := make([][]Report, len(c.fetchers)) var group sync.WaitGroup for index, item := range c.fetchers { group.Go(func() { - report, err := item.fetch(ctx) - kept, ok := guard(item.id, item.name, report, err) - results[index] = kept - keep[index] = ok + reports, err := item.fetch(ctx) + results[index] = guard(item.id, item.name, reports, err) }) } group.Wait() @@ -78,10 +75,8 @@ func (c *Collector) read(ctx context.Context) (Snapshot, error) { return Snapshot{}, err } reports := make([]Report, 0, len(results)) - for index, report := range results { - if keep[index] { - reports = append(reports, report) - } + for _, batch := range results { + reports = append(reports, batch...) } return Snapshot{FetchedAt: c.clock().UTC(), Reports: reports}, nil } @@ -97,16 +92,33 @@ func NewCollector() *Collector { } } -func guard(id, name string, report Report, err error) (Report, bool) { - if err == nil { - report.ID = id - if report.Name == "" { - report.Name = name +func guard(id, name string, reports []Report, err error) []Report { + if err != nil { + if failed, ok := errorReport(id, name, err); ok { + return []Report{failed} } - report.OK = true - report.Limits = Standing(report.Limits) - return report, true + return nil + } + kept := make([]Report, 0, len(reports)) + for _, report := range reports { + kept = append(kept, normalizeReport(id, name, report)) + } + return kept +} + +func normalizeReport(id, name string, report Report) Report { + if report.ID == "" { + report.ID = id + } + if report.Name == "" { + report.Name = name } + report.OK = true + report.Limits = Standing(report.Limits) + return report +} + +func errorReport(id, name string, err error) (Report, bool) { if _, ok := errors.AsType[MissingError](err); ok { return Report{}, false } @@ -119,38 +131,52 @@ func guard(id, name string, report Report, err error) (Report, bool) { return Report{ID: id, Name: name, Error: readFailure(err)}, true } +func singleFetcher(id, name string, fetch func(context.Context) (Report, error)) fetcher { + return fetcher{ + id: id, + name: name, + fetch: func(ctx context.Context) ([]Report, error) { + report, err := fetch(ctx) + if err != nil { + return nil, err + } + return []Report{report}, nil + }, + } +} + func defaultFetchers(source Source, client *http.Client) []fetcher { return []fetcher{ - {anthropicID, anthropicName, func(ctx context.Context) (Report, error) { + singleFetcher(anthropicID, anthropicName, func(ctx context.Context) (Report, error) { return fetchAnthropic(ctx, source, client) - }}, - {copilotID, copilotName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(copilotID, copilotName, func(ctx context.Context) (Report, error) { return fetchCopilot(ctx, source, client) - }}, - {codexID, codexName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(codexID, codexName, func(ctx context.Context) (Report, error) { return fetchCodex(ctx, source, client) - }}, - {openCodeID, openCodeName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(openCodeID, openCodeName, func(ctx context.Context) (Report, error) { return fetchOpenCode(ctx, source, client) - }}, - {grokID, grokName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(grokID, grokName, func(ctx context.Context) (Report, error) { return fetchGrok(ctx, source, client) - }}, - {zaiID, zaiName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(zaiID, zaiName, func(ctx context.Context) (Report, error) { return fetchZAI(ctx, source, client) - }}, - {miniMaxID, miniMaxName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(miniMaxID, miniMaxName, func(ctx context.Context) (Report, error) { return fetchMiniMax(ctx, source, client) - }}, - {deepSeekID, deepSeekName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(deepSeekID, deepSeekName, func(ctx context.Context) (Report, error) { return fetchDeepSeek(ctx, source, client) - }}, - {ollamaID, ollamaName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(ollamaID, ollamaName, func(ctx context.Context) (Report, error) { return fetchOllama(ctx, source, client) - }}, - {museID, museName, func(ctx context.Context) (Report, error) { + }), + singleFetcher(museID, museName, func(ctx context.Context) (Report, error) { return fetchMuse(ctx, source, client) - }}, + }), } } diff --git a/internal/quotas/fetch_test.go b/internal/quotas/fetch_test.go index 997d27b..002b1a4 100644 --- a/internal/quotas/fetch_test.go +++ b/internal/quotas/fetch_test.go @@ -32,9 +32,10 @@ func TestAnthropicReadsSessionAndWeek(t *testing.T) { "ANTHROPIC_TOKEN": "sk-ant-oat-test", }), client) require.NoError(t, err) - report, ok := guard(anthropicID, anthropicName, fetched, err) + reports := guard(anthropicID, anthropicName, []Report{fetched}, err) - require.True(t, ok) + require.Len(t, reports, 1) + report := reports[0] assert.Equal(t, anthropicName, report.Name) assert.Equal(t, []string{"Week, all models", "Week, Opus"}, labels(report.Limits)) assert.Equal(t, []string{"Extra usage is off."}, report.Notes) @@ -272,13 +273,13 @@ func TestCollectorOmitsAMissingSignInAndReusesAFreshRead(t *testing.T) { collector := &Collector{ now: func() time.Time { return now }, fetchers: []fetcher{ - {id: "a", name: "Alpha", fetch: func(context.Context) (Report, error) { + singleFetcher("a", "Alpha", func(context.Context) (Report, error) { calls++ return Report{Limits: []Limit{{Label: "Week", Percent: 10, Window: time.Hour, ResetsAt: now.Add(time.Hour)}}}, nil - }}, - {id: "b", name: "Beta", fetch: func(context.Context) (Report, error) { + }), + singleFetcher("b", "Beta", func(context.Context) (Report, error) { return Report{}, MissingError{Text: "missing"} - }}, + }), }, } From 4be364cb1d99eb009e401d8de4920159d02e4eff Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Thu, 19 Nov 2026 11:04:31 -0600 Subject: [PATCH 2/2] Add Google AI subscription quota support via agy CLI Google AI subscriptions (such as Google AI Pro) have no public REST API for subscription quotas and divide usage across independent model groups, each with its own 5-hour and weekly limits. Read usage through the Antigravity CLI (`agy -p /usage --output-format json`) rather than calling undocumented internal endpoints or reading private OAuth token files. Print mode answers `/usage` without running a turn or spending quota. Emit one report card per quota group so each group sorts by its own waste and keeps the existing JSON report schema unchanged. Watch stderr and cancel immediately when the CLI asks for interactive login so an unsigned-in installation is omitted without waiting for a timeout. Change-Id: I7c044aaca09344884fa7e3f9c162fd356dfe1d8f --- internal/quotas/collect.go | 3 + internal/quotas/fetch_test.go | 149 ++++++++++++++++ internal/quotas/google.go | 327 ++++++++++++++++++++++++++++++++++ 3 files changed, 479 insertions(+) create mode 100644 internal/quotas/google.go diff --git a/internal/quotas/collect.go b/internal/quotas/collect.go index 51e9133..5ac00ce 100644 --- a/internal/quotas/collect.go +++ b/internal/quotas/collect.go @@ -177,6 +177,9 @@ func defaultFetchers(source Source, client *http.Client) []fetcher { singleFetcher(museID, museName, func(ctx context.Context) (Report, error) { return fetchMuse(ctx, source, client) }), + {id: googleID, name: googleName, fetch: func(ctx context.Context) ([]Report, error) { + return fetchGoogle(ctx, source, client) + }}, } } diff --git a/internal/quotas/fetch_test.go b/internal/quotas/fetch_test.go index 002b1a4..f55255d 100644 --- a/internal/quotas/fetch_test.go +++ b/internal/quotas/fetch_test.go @@ -266,6 +266,155 @@ printf '%s\n' '{"allowance":{"usedPercent":36,"windowDurationMins":300,"resetsAt assert.Empty(t, report.Notes) } +func TestGoogleReadsQuotaGroupsIntoSeparateReports(t *testing.T) { + t.Parallel() + now := time.Date(2026, 11, 19, 16, 0, 0, 0, time.UTC) + script := writeScript(t, `#!/bin/sh +if [ "$1" != "-p" ] || [ "$2" != "/usage" ] || [ "$3" != "--output-format" ] || [ "$4" != "json" ]; then + echo "unexpected args: $*" >&2 + exit 1 +fi +cat <<'EOF' +{ + "status": "SUCCESS", + "command": { + "name": "usage", + "data": { + "groups": [ + { + "name": "Gemini Models", + "description": "Models within this group: Gemini 4 Neon, Gemini 3.8 Flash", + "buckets": [ + {"id": "gemini-weekly", "name": "Weekly Limit Remaining", "window": "weekly", "remaining_fraction": 0.9, "reset_time": "2026-11-26T14:00:00Z"}, + {"id": "gemini-5h", "name": "Five Hour Limit Remaining", "window": "5h", "remaining_fraction": 0.65, "reset_time": "2026-11-19T19:00:00Z"} + ] + }, + { + "name": "Claude and GPT models", + "description": "Models within this group: Claude Opus 5.5, GPT-OSS 120B", + "buckets": [ + {"id": "3p-weekly", "name": "Weekly Limit Remaining", "window": "weekly", "reset_time": "2026-11-26T16:00:00Z"}, + {"id": "3p-5h", "name": "Five Hour Limit Remaining", "window": "5h", "remaining_fraction": 0.8, "reset_time": "2026-11-19T21:00:00Z"} + ] + }, + { + "name": "Gemini 4 Argon", + "description": "For paid Pro plans, Argon quota is separate.", + "buckets": [ + {"id": "gemini-argon-wtu-weekly", "name": "Gemini 4 Argon Weekly Limit", "window": "weekly", "remaining_fraction": 1, "reset_time": "2026-11-20T16:00:00Z"} + ] + }, + { + "name": "Empty", + "buckets": [] + } + ] + } + } +} +EOF +`) + source := envSource(map[string]string{"QUOTAS_AGY_COMMAND": script}) + collector := &Collector{ + now: func() time.Time { return now }, + fetchers: []fetcher{{ + id: googleID, + name: googleName, + fetch: func(ctx context.Context) ([]Report, error) { + return fetchGoogle(ctx, source, nil) + }, + }}, + } + + snapshot, err := collector.Reports(t.Context(), true) + require.NoError(t, err) + require.Len(t, snapshot.Reports, 3) + + gemini := snapshot.Reports[0] + assert.Equal(t, "google-ai-gemini-models", gemini.ID) + assert.Equal(t, "Google AI (Gemini Models)", gemini.Name) + assert.Equal(t, []string{"5 hours", "Week"}, labels(gemini.Limits)) + assert.InDelta(t, 35.0, gemini.Limits[0].Percent, 1e-9) + assert.InDelta(t, 10.0, gemini.Limits[1].Percent, 1e-9) + assert.Equal(t, []string{"Models: Gemini 4 Neon, Gemini 3.8 Flash"}, gemini.Notes) + + thirdParty := snapshot.Reports[1] + assert.Equal(t, "google-ai-claude-and-gpt-models", thirdParty.ID) + assert.Equal(t, "Google AI (Claude and GPT models)", thirdParty.Name) + // An exhausted week (omitted remaining_fraction with reset_time) drops only this group's 5-hour limit. + assert.Equal(t, []string{"Week"}, labels(thirdParty.Limits)) + assert.InDelta(t, 100.0, thirdParty.Limits[0].Percent, 1e-9) + assert.Equal(t, LevelCritical, thirdParty.Limits[0].Level) + + argon := snapshot.Reports[2] + assert.Equal(t, "google-ai-gemini-4-argon", argon.ID) + assert.Equal(t, "Google AI (Gemini 4 Argon)", argon.Name) + assert.Equal(t, []string{"Week"}, labels(argon.Limits)) + assert.InDelta(t, 0.0, argon.Limits[0].Percent, 1e-9) + assert.Empty(t, argon.Notes) + + assert.Equal(t, []string{ + "Google AI (Gemini 4 Argon)", + "Google AI (Gemini Models)", + "Google AI (Claude and GPT models)", + }, names(Ordered(snapshot.Reports, now))) +} + +func TestGoogleCancelsAuthPromptAsMissing(t *testing.T) { + t.Parallel() + script := writeScript(t, `#!/bin/sh +echo "Authentication required. Please visit the URL to log in:" >&2 +sleep 30 +`) + start := time.Now() + _, err := fetchGoogle(t.Context(), envSource(map[string]string{"QUOTAS_AGY_COMMAND": script}), nil) + var missing MissingError + require.ErrorAs(t, err, &missing) + assert.Less(t, time.Since(start), 5*time.Second) +} + +func TestGoogleHandlesCLIExitAndErrors(t *testing.T) { + t.Parallel() + cases := []struct { + name string + script string + wantMissing bool + }{ + { + name: "exit code 2 API key mode", + script: "#!/bin/sh\nexit 2\n", + wantMissing: true, + }, + { + name: "auth error payload", + script: "#!/bin/sh\nprintf '%s\n' '{\"status\":\"ERROR\",\"error\":\"authentication failed or timed out\"}'\n", + wantMissing: true, + }, + { + name: "general CLI failure", + script: "#!/bin/sh\nexit 1\n", + }, + { + name: "malformed JSON", + script: "#!/bin/sh\nprintf 'not-json'\n", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + script := writeScript(t, tc.script) + _, err := fetchGoogle(t.Context(), envSource(map[string]string{"QUOTAS_AGY_COMMAND": script}), nil) + if tc.wantMissing { + var missing MissingError + require.ErrorAs(t, err, &missing) + return + } + var said SaidError + require.ErrorAs(t, err, &said) + }) + } +} + func TestCollectorOmitsAMissingSignInAndReusesAFreshRead(t *testing.T) { t.Parallel() now := time.Date(2026, 3, 1, 12, 0, 0, 0, time.UTC) diff --git a/internal/quotas/google.go b/internal/quotas/google.go new file mode 100644 index 0000000..a872930 --- /dev/null +++ b/internal/quotas/google.go @@ -0,0 +1,327 @@ +package quotas + +import ( + "bufio" + "bytes" + "cmp" + "context" + "errors" + "fmt" + "io" + "net/http" + "os/exec" + "slices" + "strconv" + "strings" + "sync" + "time" + "unicode" + + json "encoding/json/v2" +) + +const ( + googleID = "google-ai" + googleName = "Google AI" + googleMissing = "The Antigravity CLI is not installed: install it, then sign in with agy." + googleAuthMissing = "Sign in with the Antigravity CLI (`agy`) to read Google's plan." + googleModelsLead = "Models within this group: " + dayWindow = 24 * time.Hour + monthWindow = 30 * 24 * time.Hour +) + +func fetchGoogle(ctx context.Context, source Source, _ *http.Client) ([]Report, error) { + command, err := source.Command("QUOTAS_AGY_COMMAND", "agy", googleMissing) + if err != nil { + return nil, err + } + output, err := runAgyUsage(ctx, command, source.userHome()) + if err != nil { + return nil, err + } + return parseGoogleReports(output) +} + +func runAgyUsage(ctx context.Context, command, home string) ([]byte, error) { + timeoutCtx, cancelTimeout := context.WithTimeoutCause(ctx, readWait, SaidError{Text: fmt.Sprintf("No answer in %d s.", int(readWait.Seconds()))}) + defer cancelTimeout() + runCtx, cancelRun := context.WithCancelCause(timeoutCtx) + defer cancelRun(nil) + + process := exec.CommandContext(runCtx, command, "-p", "/usage", "--output-format", "json") + process.Dir = home + process.Stdin = nil + process.WaitDelay = time.Second + var stdout bytes.Buffer + process.Stdout = &stdout + stderr, err := process.StderrPipe() + if err != nil { + return nil, SaidError{Text: "Antigravity could not be started."} + } + if err := process.Start(); err != nil { + return nil, MissingError{Text: googleMissing} + } + + var watch sync.WaitGroup + watch.Go(func() { + watchAgyStderr(stderr, cancelRun) + }) + watch.Wait() + waitErr := process.Wait() + return agyOutput(runCtx, stdout.Bytes(), waitErr) +} + +func watchAgyStderr(stderr io.Reader, cancel context.CancelCauseFunc) { + scanner := bufio.NewScanner(stderr) + scanner.Buffer(make([]byte, 0, 16*1024), readLimit) + for scanner.Scan() { + if agyAuthText(scanner.Text()) { + cancel(MissingError{Text: googleAuthMissing}) + return + } + } +} + +func agyOutput(ctx context.Context, output []byte, waitErr error) ([]byte, error) { + if err := agyContextError(ctx); err != nil { + return nil, err + } + if waitErr != nil { + return nil, agyExitError(output, waitErr) + } + return output, nil +} + +func agyContextError(ctx context.Context) error { + if ctx.Err() == nil { + return nil + } + cause := context.Cause(ctx) + if missingErr, ok := errors.AsType[MissingError](cause); ok { + return missingErr + } + if said, ok := errors.AsType[SaidError](cause); ok { + return said + } + return SaidError{Text: fmt.Sprintf("No answer in %d s.", int(readWait.Seconds()))} +} + +func agyExitError(output []byte, waitErr error) error { + if exitErr, ok := errors.AsType[*exec.ExitError](waitErr); ok && noAccountQuotaExit(exitErr) { + return MissingError{Text: googleAuthMissing} + } + if agyAuthOutput(output) { + return MissingError{Text: googleAuthMissing} + } + return SaidError{Text: "The Antigravity CLI could not read Google's plan."} +} + +func parseGoogleReports(output []byte) ([]Report, error) { + var decoded any + if err := json.Unmarshal(output, &decoded); err != nil { + return nil, SaidError{Text: "The Antigravity CLI did not answer with a valid report."} + } + answer, ok := asObject(decoded) + if !ok { + return nil, SaidError{Text: "The Antigravity CLI did not answer with a valid report."} + } + if err := googleAnswerError(answer); err != nil { + return nil, err + } + data, ok := googleUsageData(answer) + if !ok { + return nil, SaidError{Text: "The Antigravity CLI did not answer with its usage."} + } + groups := asRecords(data["groups"]) + usedIDs := make(map[string]int, len(groups)) + reports := make([]Report, 0, len(groups)) + for index, group := range groups { + limits := googleLimits(asRecords(group["buckets"])) + if len(limits) == 0 { + continue + } + groupName, _ := asText(group["name"]) + reports = append(reports, Report{ + ID: googleGroupID(groupName, index, usedIDs), + Name: googleGroupName(groupName), + Limits: limits, + Notes: googleGroupNotes(group), + }) + } + return reports, nil +} + +func googleAnswerError(answer map[string]any) error { + status, _ := asText(answer["status"]) + if !strings.EqualFold(status, "error") { + return nil + } + if agyAuthText(textOrEmpty(answer["error"])) { + return MissingError{Text: googleAuthMissing} + } + return SaidError{Text: "The Antigravity CLI could not read Google's plan."} +} + +func googleUsageData(answer map[string]any) (map[string]any, bool) { + command, ok := asObject(answer["command"]) + if !ok { + return nil, false + } + return asObject(command["data"]) +} + +func googleLimits(buckets []map[string]any) []Limit { + limits := make([]Limit, 0, len(buckets)) + for _, bucket := range buckets { + if limit, ok := googleBucketLimit(bucket); ok { + limits = append(limits, limit) + } + } + slices.SortStableFunc(limits, compareLimitWindow) + return limits +} + +func compareLimitWindow(left, right Limit) int { + leftKnown := hasWindow(left) + rightKnown := hasWindow(right) + if leftKnown != rightKnown { + if leftKnown { + return -1 + } + return 1 + } + return cmp.Compare(left.Window, right.Window) +} + +func googleBucketLimit(bucket map[string]any) (Limit, bool) { + reset := resetAt(bucket["reset_time"]) + remaining, hasRemaining := asNumber(bucket["remaining_fraction"]) + if !hasRemaining { + if reset.IsZero() { + return Limit{}, false + } + remaining = 0 + } + remaining = min(1, max(0, remaining)) + percent := (1 - remaining) * 100 + label, window := googleWindow(bucket) + return Limit{ + Label: label, + Percent: percent, + ResetsAt: reset, + Level: LevelOf(percent, ""), + Window: window, + }, true +} + +func googleWindow(bucket map[string]any) (string, time.Duration) { + windowText, _ := asText(bucket["window"]) + switch strings.ToLower(windowText) { + case "5h": + return "5 hours", sessionWindow + case "weekly": + return "Week", weekWindow + case "daily", "24h": + return "Day", dayWindow + case "monthly": + return "Month", monthWindow + } + if minutes, ok := googleWindowMinutes(windowText); ok { + return WindowLabel(minutes), WindowDuration(minutes) + } + display, _ := asText(bucket["name"]) + return cmp.Or(display, windowText, "Limit"), 0 +} + +func googleWindowMinutes(text string) (float64, bool) { + text = strings.ToLower(strings.TrimSpace(text)) + if len(text) < 2 { + return 0, false + } + unit := text[len(text)-1] + amount, err := strconv.ParseFloat(text[:len(text)-1], 64) + if err != nil || nonPositive(amount) { + return 0, false + } + switch unit { + case 'm': + return amount, true + case 'h': + return amount * 60, true + case 'd': + return amount * 1440, true + default: + return 0, false + } +} + +func googleGroupID(groupName string, index int, used map[string]int) string { + slug := googleSlug(groupName) + base := fmt.Sprintf("%s-%d", googleID, index+1) + if slug != "" { + base = googleID + "-" + slug + } + used[base]++ + if used[base] == 1 { + return base + } + return fmt.Sprintf("%s-%d", base, used[base]) +} + +func googleSlug(name string) string { + var out strings.Builder + dashed := false + for _, r := range strings.ToLower(name) { + if unicode.IsLetter(r) || unicode.IsDigit(r) { + if dashed && hasBuilderText(&out) { + out.WriteByte('-') + } + out.WriteRune(r) + dashed = false + continue + } + dashed = true + } + return out.String() +} + +func googleGroupName(groupName string) string { + if groupName == "" { + return googleName + } + return fmt.Sprintf("%s (%s)", googleName, groupName) +} + +func googleGroupNotes(group map[string]any) []string { + description, ok := asText(group["description"]) + if !ok { + return nil + } + models, found := strings.CutPrefix(description, googleModelsLead) + models = strings.TrimSpace(models) + if !found || blank(models) { + return nil + } + return []string{"Models: " + models} +} + +func agyAuthOutput(output []byte) bool { + var decoded any + if err := json.Unmarshal(output, &decoded); err != nil { + return agyAuthText(string(output)) + } + answer, ok := asObject(decoded) + if !ok { + return false + } + return agyAuthText(textOrEmpty(answer["error"])) +} + +func agyAuthText(text string) bool { + lower := strings.ToLower(text) + return strings.Contains(lower, "authentication required") || strings.Contains(lower, "authentication failed") +} + +func noAccountQuotaExit(err *exec.ExitError) bool { return err.ExitCode() == 2 } + +func hasBuilderText(builder *strings.Builder) bool { return builder.Len() > 0 }