hayden4608 (Discord) and handong66 (#4791) have four Codex accounts. Three show 5 hours 100% left with 7 days 0% left, and one shows 5 hours 0% left with 7 days 22% left. The All accounts strip read "5 hours 75% left", but none of those three can spend its 5 hours until its week starts again. The right figure is 0%. mergeQuotas (quotaall.js) is the one place that sums windows across accounts. It feeds the Usage page's strip, the tray panel's strip and the Routing groups' line. The native menu-bar tray, the TUI, the CLI and /v1/magpie/quotas show one account at a time and are unchanged. The GUI's JSON doesn't carry Aside, Model or the matches, so the call is made in Go. provider.WithHeldWindows marks a window heldBy another window of the same account when that other window: - stops the account for every model, by routing's rule: not aside, no model, no matches, used >= 100, reset not passed, and not Codex credits that go on; and - starts again after this one (by reset times, else by span). So 7 days used up holds 5 hours, but 5 hours used up doesn't hold a week with 22% left. /api/usage/quotas and its refresh apply it. mergeQuotas counts a held share window as 100 used and a held counted one as its limit, and the strip's tooltip says how many were counted that way (en/zh/zh-TW/ja/de). The account cards still show the vendor's own figure. Run on mote (macOS) at this head's parent tree: - go vet for darwin, linux (nogui) and windows: ok; gofmt -l: none. - go test -count=1 ./internal/provider ./internal/gui, before the quota_span leftOut line and an i18n comment fix. The provider package's only red was TestQuotaSpanCarriesEveryWindowField, which wanted heldBy named, and it is now named. internal/gui's only red was TestMainWindowCorners/macOS_26_SDK, which is #3949. - GUI, chromium and webkit, every language, 440px and 360px: quota-*, usage-* (all-accounts, merge, accounts, fold, refresh and more), panel-usage/quota/switch-accounts/balance-order, routing-quota-left, routing-group, tray-usage, i18n-keys, gui-de/ja/zh-tw, old-webkit: 704 of 707. The reds were i18n-keys (a quoted "All accounts" in window_held.go's comment, fixed here) and usage-fold's "17 subscriptions and 29 accounts" in both engines, which is #4790 and is red at origin/main too (see below). At the pushed head, rebased oneb3b9376e, on mote: gofmt none, vet ok. internal/provider ok. internal/gui is red only on TestMainWindowCorners (#3949). GUI usage-merge, usage-all-accounts, i18n-keys and usage-fold, both engines: 44 of 46. The 2 reds are usage-fold's 29 accounts, which fail the same at origin/maineb3b9376eon mote (noted on #4790). Broken by hand, each red, then restored (cmp): - Go, no HeldBy set: both TestHeldWindows tests red (a, b, c not held). - Go, outlasts always true: red on d and on "a week that starts again first". - JS, held forced false: usage-merge plus all 5 languages of usage-all-accounts red in chromium, showing the reporter's "5 hours · 75% left". Not tried: real accounts. The issue names no provider; Codex is assumed from the cards' 5 hours / 7 days. Not done: marking a card as held by its week, the reporter's optional second ask. Not written: a handler-level test that /api/usage/quotas carries heldBy. Not run on Linux or Windows.
87 lines
4.0 KiB
Go
87 lines
4.0 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// #4791 (hayden4608 on Discord, handong66): four accounts of one plan,
|
|
// three with 100% of 5 hours left and 0% of 7 days, one with 0% of 5 hours
|
|
// and 22% of 7 days. The three can't spend their 5 hours until their week
|
|
// starts again, so their 5 hours are held by it; the fourth's week isn't
|
|
// held by its five hours, which start again first.
|
|
func TestHeldWindowsReportersCase(t *testing.T) {
|
|
now := time.Unix(1_793_620_800, 0) // 2026-11-02 12:00 UTC
|
|
// /wham/usage's rate_limit, as Codex reads it
|
|
codex := func(user string, fiveUsed, weekUsed float64, fiveIn, weekIn int64) SubscriptionQuota {
|
|
var u codexUsage
|
|
b := []byte(`{"plan_type":"plus","rate_limit":{"allowed":true,` +
|
|
`"primary_window":{"used_percent":` + jsonNum(fiveUsed) + `,"limit_window_seconds":18000,"reset_at":` + jsonNum(float64(now.Unix()+fiveIn)) + `},` +
|
|
`"secondary_window":{"used_percent":` + jsonNum(weekUsed) + `,"limit_window_seconds":604800,"reset_at":` + jsonNum(float64(now.Unix()+weekIn)) + `}}}`)
|
|
if err := json.Unmarshal(b, &u); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return SubscriptionQuota{Provider: "codex", User: user, Plan: "plus", Windows: []QuotaWindow{u.RateLimit.Primary.window(), u.RateLimit.Secondary.window()}}
|
|
}
|
|
qs := []SubscriptionQuota{
|
|
codex("a@example.com", 0, 100, 5*3600, 3*86400),
|
|
codex("b@example.com", 0, 100, 5*3600, 2*86400),
|
|
codex("c@example.com", 0, 100, 5*3600, 86400),
|
|
codex("d@example.com", 100, 78, 2*3600, 4*86400),
|
|
}
|
|
out := WithHeldWindows(qs, now)
|
|
for i := range 3 {
|
|
if got := out[i].Windows[0].HeldBy; got != "7 days" {
|
|
t.Errorf("%s: 5 hours held by %q, want 7 days", out[i].User, got)
|
|
}
|
|
if got := out[i].Windows[1].HeldBy; got != "" {
|
|
t.Errorf("%s: 7 days held by %q, want none", out[i].User, got)
|
|
}
|
|
}
|
|
for _, w := range out[3].Windows {
|
|
if w.HeldBy != "" {
|
|
t.Errorf("d: %s held by %q: a week behind five hours used up keeps its 22%% for after them", w.Name, w.HeldBy)
|
|
}
|
|
}
|
|
for _, q := range qs {
|
|
for _, w := range q.Windows {
|
|
if w.HeldBy != "" {
|
|
t.Fatalf("the cached cards were written: %s %s", q.User, w.Name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHeldWindowsOnlyByWhatStopsTheAccount(t *testing.T) {
|
|
now := time.Unix(1_793_620_800, 0)
|
|
at := func(d time.Duration) *time.Time { t := now.Add(d); return &t }
|
|
five := QuotaWindow{Name: "5 hours", Used: 0, ResetsAt: at(time.Hour), Span: 5 * time.Hour}
|
|
for _, c := range []struct {
|
|
why string
|
|
week QuotaWindow
|
|
user string
|
|
held bool
|
|
}{
|
|
{"a week used up", QuotaWindow{Name: "7 days", Used: 100, ResetsAt: at(48 * time.Hour), Span: 168 * time.Hour}, "", true},
|
|
{"a week used up, its reset not told", QuotaWindow{Name: "7 days", Used: 100}, "", true},
|
|
{"one model's week (Claude's Opus)", QuotaWindow{Name: "7 days Opus", Used: 100, ResetsAt: at(48 * time.Hour), Model: "opus"}, "", false},
|
|
{"money aside", QuotaWindow{Name: "Extra usage", Used: 100, Aside: true}, "", false},
|
|
{"a week whose reset has passed", QuotaWindow{Name: "7 days", Used: 100, ResetsAt: at(-time.Minute)}, "", false},
|
|
{"a week that starts again first", QuotaWindow{Name: "7 days", Used: 100, ResetsAt: at(30 * time.Minute)}, "", false},
|
|
{"a week with room", QuotaWindow{Name: "7 days", Used: 99, ResetsAt: at(48 * time.Hour)}, "", false},
|
|
{"a ChatGPT account going on on its credits", QuotaWindow{Name: "7 days", Used: 100, ResetsAt: at(48 * time.Hour), Credits: true}, "e@example.com", false},
|
|
} {
|
|
q := SubscriptionQuota{Provider: "codex", User: c.user, Windows: []QuotaWindow{five, c.week}}
|
|
got := WithHeldWindows([]SubscriptionQuota{q}, now)[0].Windows[0].HeldBy
|
|
if (got != "") != c.held {
|
|
t.Errorf("%s: 5 hours held by %q, want held %v", c.why, got, c.held)
|
|
}
|
|
}
|
|
// another magpie's card is as it sent it, a failed reading as it was
|
|
q := SubscriptionQuota{Provider: "codex", From: "studio", Windows: []QuotaWindow{five, {Name: "7 days", Used: 100}}}
|
|
if got := WithHeldWindows([]SubscriptionQuota{q}, now)[0].Windows[0].HeldBy; got != "" {
|
|
t.Errorf("another magpie's card held by %q", got)
|
|
}
|
|
}
|