diff --git a/internal/gui/assets/plugins/usage/page.js b/internal/gui/assets/plugins/usage/page.js index b8647f7c1..799e72a44 100644 --- a/internal/gui/assets/plugins/usage/page.js +++ b/internal/gui/assets/plugins/usage/page.js @@ -741,14 +741,15 @@ function quotaWho(q) { // /api/warm-now {agent, user}), for this account. Idle is as the // warm-up tells it (idle in codex_warmup.go): nothing used and its reset a // whole window away, or gone by; or not read at all, which a Claude account -// never used is, while a Codex account with no 5-hour window has none to -// start. One just warmed reads the same for warmSlack, so it waits that out. +// never read is, while an account read with no 5-hour window (a Codex +// plan's, a Claude Ultra's, shangyuan0818 on X) has none to start. One just warmed reads the +// same for warmSlack, so it waits that out. const quotaWarmed = new Map(); function quotaWarm(q) { const agent = q.provider, user = q.user, key = agent + "\n" + user?.toLowerCase(); if (!user || q.error || !(state.settings?.[agent + "Users"] || []).some((u) => u.toLowerCase() === user.toLowerCase())) return null; const w = q.windows?.find((x) => x.name === "5 hours"), now = Date.now(), at = w?.resetsAt ? Date.parse(w.resetsAt) : 0; - if (w ? !(at && at <= now) && (w.used > 0 || at && at - now < 5 * 3600e3 - 600e3) : agent !== "claude") return null; + if (w ? !(at && at <= now) && (w.used > 0 || at && at - now < 5 * 3600e3 - 600e3) : agent !== "claude" || q.windows?.length) return null; const busy = quotaWarmed.get(key); if (busy && busy !== true && now - busy < 600e3) return null; const b = el("button", "text acct-warm" + (busy === true ? " busy" : ""), t("Warm up")); diff --git a/internal/gui/tests/quota-warm.test.cjs b/internal/gui/tests/quota-warm.test.cjs index 37c6959ed..10fef0b67 100644 --- a/internal/gui/tests/quota-warm.test.cjs +++ b/internal/gui/tests/quota-warm.test.cjs @@ -32,11 +32,13 @@ const quotas = [ { provider: "codex", name: "Codex", icon: "codex-color", plan: "Plus", user: "elsewhere@example.com", windows: [{ name: "5 hours", used: 0, resetsAt: iso(now + 5 * H) }] }, // a Claude account never used: no window read { provider: "claude", name: "Claude Code", icon: "claude-color", plan: "Max", user: "claude@example.com", windows: [] }, + // a Claude Ultra read with a week and no 5-hour window: it has none to start (shangyuan0818 on X) + { provider: "claude", name: "Claude Code", icon: "claude-color", plan: "Ultra", user: "ultra@example.com", windows: [{ name: "7 days", used: 12, resetsAt: iso(now + 72 * H) }] }, ]; const WHY = "429 Too Many Requests: usage limit reached"; function serve(lang, asked) { - const settings = { lang, theme: "light", currency: "usd", codexUsers: ["idle-account-with-a-long-name@example.com", "busy@example.com", "ran-out@example.com", "zero@example.com"], claudeUsers: ["claude@example.com"] }; + const settings = { lang, theme: "light", currency: "usd", codexUsers: ["idle-account-with-a-long-name@example.com", "busy@example.com", "ran-out@example.com", "zero@example.com"], claudeUsers: ["claude@example.com", "ultra@example.com"] }; const state = { agents: [], profiles: [], settings }; return async (r) => { const url = new URL(r.request().url()); @@ -116,7 +118,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.ok((await idle.getAttribute("title")).startsWith(w.tip), await idle.getAttribute("title")); await warmOf(page, "Ran-Out@Example.com").waitFor(); await warmOf(page, "claude@example.com").waitFor(); - for (const u of ["busy@example.com", "zero@example.com", "elsewhere@example.com"]) { + for (const u of ["busy@example.com", "zero@example.com", "elsewhere@example.com", "ultra@example.com"]) { assert.equal(await page.locator(".subscription-account", { has: page.locator(`[title="${u}"]`) }).count(), 1, u + "'s line is there"); assert.equal(await warmOf(page, u).count(), 0, u + " has no Warm up"); } diff --git a/internal/provider/claude_limits.go b/internal/provider/claude_limits.go index 0021551fa..53be4ed1f 100644 --- a/internal/provider/claude_limits.go +++ b/internal/provider/claude_limits.go @@ -62,6 +62,41 @@ func claudeWithUnstarted(ws []QuotaWindow) []QuotaWindow { return out } +// claudeNoFiveHour says whether the whole reading ws of an account, read at +// now, is of one the vendor sends no 5-hour window for, so claudeWithUnstarted +// must not make one up. Anthropic leaves a window out (null) while it hasn't +// started, which is all an idle account's reading says, so it is the use +// that tells: an account whose week rose since prev, read at prevAt less than +// five hours ago, was used inside five hours, and one used so has a 5-hour +// window running where its plan has one. prev with no 5-hour window at all +// (the made-up one has no reset) stays so, as does the card kept on disk, until +// a reading tells one (shangyuan0818 on X: a verified Ultra account has none). +func claudeNoFiveHour(prev []QuotaWindow, prevAt time.Time, ws []QuotaWindow, now time.Time) bool { + has := func(l []QuotaWindow) bool { + return slices.ContainsFunc(l, func(w QuotaWindow) bool { return w.Name == "5 hours" }) + } + if has(ws) || len(prev) == 0 { + return false + } + if !has(prev) { + return true // seen without one, and nothing since says it started + } + if prevAt.IsZero() || now.Sub(prevAt) >= 5*time.Hour { + return false + } + week := func(l []QuotaWindow) (QuotaWindow, bool) { + for _, w := range l { + if w.Name == "7 days" && w.ResetsAt != nil { + return w, true + } + } + return QuotaWindow{}, false + } + a, ok1 := week(prev) + b, ok2 := week(ws) + return ok1 && ok2 && b.Used > a.Used && b.ResetsAt.Sub(*a.ResetsAt).Abs() < time.Hour +} + // claudeHeard is how long what Claude Code said as it answered stands in // for the usage endpoint when that fails. const claudeHeard = time.Hour diff --git a/internal/provider/claude_no_five_hour_test.go b/internal/provider/claude_no_five_hour_test.go new file mode 100644 index 000000000..9ecd2208b --- /dev/null +++ b/internal/provider/claude_no_five_hour_test.go @@ -0,0 +1,50 @@ +package provider + +import ( + "testing" + "time" +) + +// shangyuan0818 on X: a verified Claude Ultra account has no 5-hour limit, +// and the Usage overview still showed one. Anthropic leaves a window out +// until a request starts it, so a reading of an idle account and one of an +// account with no such window look alike; the one made up for an idle +// account (claudeWithUnstarted) stays up only till the account is seen used +// (its week rises) with none, and then stays down, across a restart too, +// until a reading or header tells one. +func TestClaudeNoFiveHourWindowNotMadeUp(t *testing.T) { + reset := time.Now().UTC().Add(72*time.Hour).Format("Jan 2 at 3pm") + " (UTC)" + week := func(used string) string { + return "Current week (all models): " + used + "% used · resets " + reset + "\n" + } + out, card := claudeUsageCards(t, week("10"), nil) + has5 := func(q SubscriptionQuota) bool { + for _, w := range q.Windows { + if w.Name == "5 hours" { + return true + } + } + return false + } + // idle: no way to tell, the account is taken to have one, not started + if q := card(); !has5(q) { + t.Fatalf("an account not seen used shows its 5-hour window unstarted: %+v", q.Windows) + } + // used, and still none: the vendor sends none + out.Store(week("12")) + retryClaudeUsage() + if q := card(); has5(q) || len(q.Windows) != 1 { + t.Fatalf("an account used with no 5-hour window shows one: %+v", q.Windows) + } + // idle again: still none + retryClaudeUsage() + if q := card(); has5(q) { + t.Fatalf("a window the account hasn't came back while idle: %+v", q.Windows) + } + // a reading that tells one shows it + out.Store("Current session: 3% used · resets " + time.Now().UTC().Add(4*time.Hour).Format("Jan 2 at 3:04pm") + " (UTC)\n" + week("13")) + retryClaudeUsage() + if q := card(); !has5(q) { + t.Fatalf("a 5-hour window the vendor sent: %+v", q.Windows) + } +} diff --git a/internal/provider/subscription_usage.go b/internal/provider/subscription_usage.go index 918ef9ba2..a504b2f70 100644 --- a/internal/provider/subscription_usage.go +++ b/internal/provider/subscription_usage.go @@ -1067,8 +1067,20 @@ 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) + // a whole reading: a window it leaves out hasn't started, unless the + // account is seen used with none (claudeNoFiveHour) + prev, prevAt := e.ws, e.at + if prev == nil { + if q, ok := lastReading("claude/" + key); ok { + prev, prevAt = q.Windows, e.at + if q.ReadAt != nil { + prevAt = *q.ReadAt + } + } + } + if !claudeNoFiveHour(prev, prevAt, ws, now) { + ws = claudeWithUnstarted(ws) + } c.Lock() if c.m == nil { c.m = map[string]claudeUsageEntry{}