From 54a0d7a6feacf4503909820e601b5f57987e235c Mon Sep 17 00:00:00 2001 From: HardeNNN <42101418+HardeNNN@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:53:09 +0100 Subject: [PATCH] fix(quota): report GOAT plan limits as 20/50/100 The GOAT plan publishes a 100 USD monthly allowance with 20 USD per 5 hours and 50 USD per week. The credits endpoint reports the base per-model allowance (70 USD) and scales its rolling windows from it (14/35), so the console showed stale limits. Map the published GOAT caps explicitly, normalize the remaining balance to plan cap minus billing-period spend, and drive the monthly bar from the billing-period total. Unknown plans keep the upstream values. --- internal/app/quota.go | 47 +++++++++++++++++++++++++++++++++++++- internal/app/quota_test.go | 36 ++++++++++++++++++++++++++++- internal/web/index.html | 9 +++++--- 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/internal/app/quota.go b/internal/app/quota.go index 0c37bb0..f5a0d42 100644 --- a/internal/app/quota.go +++ b/internal/app/quota.go @@ -102,13 +102,19 @@ func (q *QuotaSnapshot) Blocked() bool { type planSpec struct { Name string MonthlyCredits float64 + // FiveHourCredits and WeeklyCredits override the upstream rolling caps when + // non-zero. The upstream endpoint scales its windows from the base + // per-model allowance, so for models whose allowance exceeds that base it + // reports caps lower than the plan's published limits. + FiveHourCredits float64 + WeeklyCredits float64 } // knownPlans mirrors the community CLI's planId mapping. Unknown plans are // reported by ID with no monthly cap, so the UI hides the derived monthly bar. var knownPlans = map[string]planSpec{ "individual-go": {Name: "Go", MonthlyCredits: 10}, - "individual-goat": {Name: "GOAT", MonthlyCredits: 70}, + "individual-goat": {Name: "GOAT", MonthlyCredits: 100, FiveHourCredits: 20, WeeklyCredits: 50}, "individual-pro": {Name: "Pro", MonthlyCredits: 30}, "individual-pro-v1": {Name: "Pro", MonthlyCredits: 80}, "individual-provider": {Name: "Provider", MonthlyCredits: 15}, @@ -313,6 +319,43 @@ func buildQuotaPlan(planID string, data map[string]any) *QuotaPlan { return plan } +// applyPlanWindows replaces the upstream rolling caps with the plan's published +// limits. The upstream caps are derived from a base per-model allowance, so +// they under-report plans or models with a higher allowance. +func applyPlanWindows(snap *QuotaSnapshot) { + if snap == nil || snap.Plan == nil { + return + } + spec, ok := planInfo(snap.Plan.PlanID) + if !ok { + return + } + if spec.FiveHourCredits > 0 && snap.FiveHour != nil { + snap.FiveHour.Cap = spec.FiveHourCredits + } + if spec.WeeklyCredits > 0 && snap.Weekly != nil { + snap.Weekly.Cap = spec.WeeklyCredits + } +} + +// applyPlanRemaining rewrites the upstream monthly balance for plans whose +// published allowance is higher than the upstream base allowance. The +// remaining figure becomes the plan cap minus the billing-period spend. +func applyPlanRemaining(snap *QuotaSnapshot) { + if snap == nil || snap.Plan == nil || snap.Plan.MonthlyCredits == nil || snap.Usage == nil { + return + } + spec, ok := planInfo(snap.Plan.PlanID) + if !ok || (spec.FiveHourCredits == 0 && spec.WeeklyCredits == 0) { + return + } + remaining := *snap.Plan.MonthlyCredits - snap.Usage.TotalCredits + if remaining < 0 { + remaining = 0 + } + snap.MonthlyCredits = &remaining +} + func parseQuotaUsage(raw map[string]any) *QuotaUsage { if raw == nil { return nil @@ -469,6 +512,7 @@ func fetchQuotaSnapshot(ctx context.Context, client *http.Client, baseURL, apiKe snap.Plan = buildQuotaPlan(planID, data) } } + applyPlanWindows(snap) // 4. usage/summary → billing-period aggregates. if usageResp, err := quotaGet(ctx, client, base+"/alpha/usage/summary", apiKey); err != nil { @@ -480,6 +524,7 @@ func fetchQuotaSnapshot(ctx context.Context, client *http.Client, baseURL, apiKe } snap.Usage = parseQuotaUsage(root) } + applyPlanRemaining(snap) if len(failures) > 0 { snap.Failures = failures diff --git a/internal/app/quota_test.go b/internal/app/quota_test.go index c1150fe..d1eb87d 100644 --- a/internal/app/quota_test.go +++ b/internal/app/quota_test.go @@ -34,7 +34,7 @@ func TestPlanInfoLongestPrefix(t *testing.T) { {"individual-pro", "Pro", 30, true}, {"individual-pro-v1", "Pro", 80, true}, {"individual_pro", "Pro", 30, true}, - {"individual-goat", "GOAT", 70, true}, + {"individual-goat", "GOAT", 100, true}, {"teams-pro", "Teams Pro", 40, true}, {"individual-max", "Max", 150, true}, {"enterprise-unknown", "", 0, false}, @@ -188,6 +188,40 @@ func TestFetchQuotaSnapshotHappyPath(t *testing.T) { } } +func TestFetchQuotaSnapshotAppliesPlanWindowCaps(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/alpha/billing/credits": + fmt.Fprint(w, `{"credits":{"monthlyCredits":70,"planId":"individual-goat"},"windowLimits":{"limited":true,"fiveHour":{"used":5,"cap":14},"weekly":{"used":40,"cap":35,"exceeded":true}}}`) + case "/alpha/billing/subscriptions": + fmt.Fprint(w, `{"data":{"planId":"individual-goat","status":"active"}}`) + case "/alpha/usage/summary": + fmt.Fprint(w, `{"data":{"totalCost":10,"totalCredits":10}}`) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + + snap, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL, "cc-key") + if err != nil { + t.Fatalf("fetch failed: %v", err) + } + if snap.Plan == nil || snap.Plan.MonthlyCredits == nil || *snap.Plan.MonthlyCredits != 100 { + t.Fatalf("GOAT plan = %+v", snap.Plan) + } + if snap.FiveHour == nil || snap.FiveHour.Cap != 20 { + t.Fatalf("GOAT five hour window = %+v", snap.FiveHour) + } + if snap.Weekly == nil || snap.Weekly.Cap != 50 || !snap.Weekly.Exceeded { + t.Fatalf("GOAT weekly window = %+v", snap.Weekly) + } + if snap.MonthlyCredits == nil || *snap.MonthlyCredits != 90 { + t.Fatalf("GOAT remaining credits = %v, want 90", snap.MonthlyCredits) + } +} + func TestFetchQuotaSnapshotSnakeCaseAndNested(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") diff --git a/internal/web/index.html b/internal/web/index.html index 4ede888..d239ac0 100644 --- a/internal/web/index.html +++ b/internal/web/index.html @@ -1186,13 +1186,16 @@