Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 67 additions & 38 deletions internal/quotas/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,26 +62,21 @@ 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()
if err := ctx.Err(); err != nil {
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
}
Expand All @@ -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
}
Expand All @@ -119,37 +131,54 @@ 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)
}),
{id: googleID, name: googleName, fetch: func(ctx context.Context) ([]Report, error) {
return fetchGoogle(ctx, source, client)
}},
}
}
Expand Down
162 changes: 156 additions & 6 deletions internal/quotas/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -265,20 +266,169 @@ 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)
var calls int
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"}
}},
}),
},
}

Expand Down
Loading
Loading