diff --git a/internal/provider/claude_limits.go b/internal/provider/claude_limits.go index 7537f4abe..0a1efd42d 100644 --- a/internal/provider/claude_limits.go +++ b/internal/provider/claude_limits.go @@ -37,6 +37,31 @@ var claudeKinds = []struct { {"seven_day_overage_included", "7 days · Fable", "fable", 7 * 24 * time.Hour}, } +// claudeKindOrder is w's place among claudeKinds, the windows Claude Code +// names, as a reading orders them; one it doesn't name goes last. +func claudeKindOrder(w QuotaWindow) int { + for i, k := range claudeKinds { + if k.name == w.Name { + return i + } + } + return len(claudeKinds) +} + +// claudeWithUnstarted is a whole reading ws with the 5-hour window every +// Claude account has (as claudeWindowsExpected), when it leaves it out, as not +// started: Anthropic leaves a window out (null) while it hasn't started, so +// /usage has no "Current session" line for an account not used in the last +// five hours, and its card showed no 5-hour window at all (motui9535). +// The week needs none: a reading that tells any window tells the week, a +// session used being the week used too. Ordered as Claude Code names +// them, the 5-hour window first. +func claudeWithUnstarted(ws []QuotaWindow) []QuotaWindow { + out := withExpected(ws, []QuotaWindow{{Name: "5 hours", Span: 5 * time.Hour}}) + slices.SortStableFunc(out, func(a, b QuotaWindow) int { return claudeKindOrder(a) - claudeKindOrder(b) }) + return out +} + // claudeHeard is how long what Claude Code said as it answered stands in // for the usage endpoint when that fails. const claudeHeard = time.Hour @@ -81,15 +106,7 @@ func NoteClaudeLimits(user string, ls []ClaudeLimit) { } } ws = append(ws, heard...) - order := func(w QuotaWindow) int { - for i, k := range claudeKinds { - if k.name == w.Name { - return i - } - } - return len(claudeKinds) - } - slices.SortStableFunc(ws, func(a, b QuotaWindow) int { return order(a) - order(b) }) + slices.SortStableFunc(ws, func(a, b QuotaWindow) int { return claudeKindOrder(a) - claudeKindOrder(b) }) e.ws, e.at, e.heard = ws, time.Now(), time.Now() e.heardSeq = quotaReadSequence.Add(1) // after every reading started before it, as the cards order them e.err = nil // a new observation supersedes the failed reading diff --git a/internal/provider/claude_unstarted_test.go b/internal/provider/claude_unstarted_test.go new file mode 100644 index 000000000..bc5c3707e --- /dev/null +++ b/internal/provider/claude_unstarted_test.go @@ -0,0 +1,61 @@ +package provider + +import ( + "strings" + "testing" + "time" +) + +// motui9535 (Discord, v0.1.2909, a screenshot): of three Team accounts the +// preferred one showed its week and its Fable week and no 5-hour window, +// the two others all three. Anthropic leaves a window out (null) while it +// hasn't started, so /usage has no "Current session" line for an account +// not used in the last five hours; every Claude account has one all the +// same (claudeWindowsExpected). The card shows it unused, first, as the +// others order it, not missing; and a header heard later still replaces it. +func TestClaudeUnstartedSessionShownUnused(t *testing.T) { + reset := time.Now().UTC().Add(72*time.Hour).Format("Jan 2 at 3pm") + " (UTC)" + text := "Current week (all models): 17% used · resets " + reset + "\nCurrent week (Fable): 30% used · resets " + reset + "\n" + _, card := claudeUsageCards(t, text, nil) + q := card() + if q.Error != "" { + t.Fatalf("reading: %+v", q) + } + var names []string + for _, w := range q.Windows { + names = append(names, w.Name) + } + if strings.Join(names, ", ") != "5 hours, 7 days, 7 days · Fable" { + t.Fatalf("windows %q, want the 5-hour one first and the two weeks: %+v", names, q.Windows) + } + five := q.Windows[0] + if five.Used != 0 || five.ResetsAt != nil || five.Span != 5*time.Hour || five.Model != "" { + t.Fatalf("the 5-hour window not started: %+v", five) + } + if q.Windows[1].Used != 17 || q.Windows[2].Used != 30 { + t.Fatalf("the weeks as read: %+v", q.Windows) + } + + // the account used: Claude Code's header names the 5-hour window, and + // it stands in place of the unused one + NoteClaudeLimits("a@example.com", []ClaudeLimit{{Kind: "five_hour", Used: .12, ResetsAt: time.Now().Add(4 * time.Hour).Unix()}}) + lastQuotas.Lock() + lastQuotas.m, lastQuotas.loaded = nil, false + lastQuotas.Unlock() + loginUsageCache.Lock() + loginUsageCache.m = nil + loginUsageCache.Unlock() + q = card() + n := 0 + for _, w := range q.Windows { + if w.Name == "5 hours" { + n++ + if w.Used != 12 || w.ResetsAt == nil { + t.Fatalf("the heard 5-hour window: %+v", w) + } + } + } + if n != 1 || q.Windows[0].Name != "5 hours" { + t.Fatalf("one 5-hour window, first: %+v", q.Windows) + } +} diff --git a/internal/provider/claude_usage_weekly_test.go b/internal/provider/claude_usage_weekly_test.go index 65c0fa968..cde849bf5 100644 --- a/internal/provider/claude_usage_weekly_test.go +++ b/internal/provider/claude_usage_weekly_test.go @@ -147,7 +147,10 @@ func TestClaudeSwitchWeeklyOnly(t *testing.T) { } q := LoginUsage(ctx, "claude") for user, u := range q { - if strings.HasPrefix(user, "pro@x.com") && (len(u.Windows) != 2 || u.Windows[0].Name != "7 days" || u.Windows[1].Model != "fable") { + // the card has the 5-hour window /usage left out, not started + // (motui9535): Anthropic leaves one out while it hasn't started + if strings.HasPrefix(user, "pro@x.com") && (len(u.Windows) != 3 || u.Windows[0].Name != "5 hours" || u.Windows[0].Used != 0 || u.Windows[0].ResetsAt != nil || + u.Windows[1].Name != "7 days" || u.Windows[2].Model != "fable") { t.Fatalf("Claude Code's own account read: %+v", u) } } diff --git a/internal/provider/subscription_usage.go b/internal/provider/subscription_usage.go index 851b5b621..664d197f2 100644 --- a/internal/provider/subscription_usage.go +++ b/internal/provider/subscription_usage.go @@ -919,6 +919,8 @@ func claudeWindows(ctx context.Context, user string, active bool) ([]QuotaWindow } return ws, err } + // a whole reading: a window it leaves out hasn't started + ws = claudeWithUnstarted(ws) c.Lock() if c.m == nil { c.m = map[string]claudeUsageEntry{}