provider: a Claude account not used in five hours shows its 5-hour window unused, not missing (motui9535)

motui9535 (Discord #feedback, v0.1.2909, screenshot): of three Team
accounts the preferred one showed 7 days 17% and 7 days · Fable 30% and
no 5-hour window; the two others showed all three. Anthropic leaves a
window out (null) while it hasn't started (claudeWindowsExpected says as
much for the warm-up), so Claude Code's /usage has no "Current session"
line for an account nobody used in the last five hours, and the card
took the missing line as no window at all. The aggregate's 5 hours
(100%) then averaged only the two other accounts.

A whole /usage reading (claudeWindows, the one place the active and the
saved accounts' readings are kept) now gets the 5-hour window it leaves
out as not started: 0% used, no reset, first, as Claude Code orders
them (claudeKindOrder, taken out of NoteClaudeLimits). A header heard
later replaces it as before. Only the 5-hour window: a reading that
tells any window tells the week. parseClaudeUsage is unchanged
(TestClaudeUsageWeeklyOnly still holds that the parser makes none up);
TestClaudeSwitchWeeklyOnly's card now has the unstarted 5-hour window.

Run (mote, at this tree):
- go test ./internal/provider: ok (132.9s)
- go test -run 'Claude|Usage|Quota' ./internal/gateway ./internal/cli ./internal/gui: ok
- go vet ./internal/provider on darwin, linux (-tags nogui), windows; gofmt -l clean
Broken by hand: without the claudeWithUnstarted call,
TestClaudeUnstartedSessionShownUnused is red: windows ["7 days" "7 days
· Fable"], motui's shape.
Not tried: a real Claude account's /usage with an unstarted session (the
null comes from claudeWindowsExpected's note and the screenshot); no GUI
test, the cards draw every window /api/usage/quotas gives and the change
is in what it gives; Windows/Linux runs.
This commit is contained in:
yetone
2026-11-04 16:47:45 +08:00
parent f8987ce617
commit 17154e1654
4 changed files with 93 additions and 10 deletions
+26 -9
View File
@@ -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
@@ -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)
}
}
@@ -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)
}
}
+2
View File
@@ -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{}