claude: an account on overages is read with its windows and switched off (#4532)
Claude Code's /usage line "You are currently using your overages to power your Claude Code usage…" was in claudeUsageDenied, so the whole reading became an error: the card lost its windows, and NextLogin, which stays on an account it can't read, left Claude Code billing overages while other accounts had room (StringKe's audit, item 3). Now the line is no error. The windows under it are read as usual, and a Text row "Extra usage" with Used 100 is added (claudeOveragesWindow), so usedPast/roomLeft count the account spent and the switch moves off it. The row ends when the last used-up window renews (the time Claude Code says it switches back), else at the next reading. Routing skips Text rows, so the gateway's own account choice is unchanged. The GUI says the row "In use" in the reader's language (quotaText; a plugin's text row keeps its display as it came). The overages case is out of TestClaudeUsageFailureDoesNotHideAccountErrors: it was the bug. Run (mote): - TestClaudeUsageOverages, TestClaudeSwitchOffOverages: red on the old claude_usage_cli.go (an error; switched to ""), red with the row's Used set to 0 by hand, green now. - go test -tags nogui ./internal/provider -run 'Claude|Usage|NextLogin|Next|Switch' ok. - GUI: claude-overages (new, 5 languages, chromium + webkit) red 8 of 10 on the old app.js (en passes there), green now; with quota-text-row, quota-name-translated, i18n-keys, usage-no-reading: 40 pass. - go vet darwin/linux/windows; gofmt clean. Not tried: a real account on overages; the text is Claude Code 2.1.316's string table, and the windows under it are what StringKe describes.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -14129,6 +14129,8 @@ function quotaFill(w) {
|
||||
function quotaText(w) {
|
||||
if (w.unlimited) return t("Unlimited");
|
||||
if (w.balance != null) return t("{n} left", { n: (countFormat.format(w.balance) + " " + t(w.unit || "")).trim() });
|
||||
// a text row used 100 is Claude's overages (claudeOveragesWindow): "In use", said in the reader's language
|
||||
if (w.text && w.used >= 100) return t("In use");
|
||||
if (quotaNoShare(w)) return w.display || t("No allowance reported");
|
||||
const used = to100(w.used);
|
||||
const n = quotaLeft ? 100 - used : used;
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||||
// A Claude account on overages (#4532) is read with its windows and an
|
||||
// "Extra usage" row of text, used 100 (claudeOveragesWindow): it reads
|
||||
// "In use" in the reader's language, with no meter, ring or share, and
|
||||
// the windows beside it keep theirs. No backend, the API is faked here.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const path = require("node:path");
|
||||
const { test } = require("node:test");
|
||||
const { chromium, webkit } = require("playwright");
|
||||
|
||||
const assets = path.resolve(__dirname, "../assets");
|
||||
|
||||
function serve(lang) {
|
||||
const settings = { theme: "light", lang, tray: "panel" };
|
||||
return async (route) => {
|
||||
const url = new URL(route.request().url());
|
||||
const json = (data) => route.fulfill({ json: data });
|
||||
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
|
||||
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
||||
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings });
|
||||
if (url.pathname === "/api/settings") return json(settings);
|
||||
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
|
||||
if (url.pathname === "/api/plugins") return json({ plugins: [] });
|
||||
if (url.pathname.startsWith("/api/")) return json({});
|
||||
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
||||
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
||||
await route.fulfill({ body: await fs.readFile(file), contentType });
|
||||
};
|
||||
}
|
||||
|
||||
const IN_USE = { en: "In use", zh: "使用中", "zh-TW": "使用中", ja: "使用中", de: "In Verwendung" };
|
||||
const EXTRA = { en: "Extra usage", zh: "额外用量", "zh-TW": "額外用量", ja: "追加使用量", de: "Zusätzliche Nutzung" };
|
||||
|
||||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||||
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
|
||||
test(`${engine} ${lang}: Claude's overages row reads in use`, async (t) => {
|
||||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||||
t.after(() => browser.close());
|
||||
const page = await (await browser.newContext({ viewport: { width: 440, height: 700 } })).newPage();
|
||||
const errors = [];
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
await page.route("**/*", serve(lang));
|
||||
await page.goto("http://magpie.test/");
|
||||
await page.waitForFunction(() => typeof panelQuotaCard === "function");
|
||||
const got = await page.evaluate(() => {
|
||||
const extra = { name: "Extra usage", used: 100, display: "In use", text: true };
|
||||
const q = { windows: [{ name: "5 hours", used: 100 }, { name: "7 days", used: 40 }, extra], name: "Claude", user: "test" };
|
||||
const row = accountQuota({ test: q }, "test"), panel = panelQuotaCard(q), usage = quotaWindows(q);
|
||||
return {
|
||||
text: quotaText(extra), row: row.textContent, panel: panel.textContent, usage: usage.textContent,
|
||||
tracks: usage.querySelectorAll(".quota-track").length, rings: panel.querySelectorAll(".pq-ring").length,
|
||||
// a plugin's text row with its display keeps it as it came
|
||||
plugin: quotaText({ name: "积分余额", used: 0, display: "1,234 积分", text: true }),
|
||||
};
|
||||
});
|
||||
assert.equal(got.text, IN_USE[lang]);
|
||||
assert.equal(got.plugin, "1,234 积分");
|
||||
assert.match(got.usage, new RegExp(EXTRA[lang]));
|
||||
assert.match(got.usage, new RegExp(IN_USE[lang]));
|
||||
if (lang !== "en") assert.doesNotMatch(got.usage, /In use/);
|
||||
// the two windows keep their meters and shares; the row has none
|
||||
assert.equal(got.tracks, 2, got.usage);
|
||||
assert.equal((got.usage.match(/%/g) || []).length, 2, got.usage);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,33 @@ var errClaudeCannotRun = errors.New("Claude Code can't be run from here")
|
||||
|
||||
// An account error stays visible even when it also mentions a timeout or
|
||||
// rate limit. It is not a temporary failure to read the usage endpoint.
|
||||
var claudeUsageDenied = regexp.MustCompile(`(?i)\b(401|403)\b|not (logged|signed) in|signed out|sign-in (has )?expired|unauthorized|forbidden|authentication (failed|required)|invalid (access )?token|(session|usage) limit|hit your limit|using your overages`)
|
||||
var claudeUsageDenied = regexp.MustCompile(`(?i)\b(401|403)\b|not (logged|signed) in|signed out|sign-in (has )?expired|unauthorized|forbidden|authentication (failed|required)|invalid (access )?token|(session|usage) limit|hit your limit`)
|
||||
|
||||
// claudeOverages is /usage's line for an account past its plan's limits
|
||||
// and billed for what it uses ("You are currently using your overages to
|
||||
// power your Claude Code usage. We will automatically switch you back to
|
||||
// your subscription rate limits when they reset"), which Claude Code
|
||||
// prints above the account's windows. It is no account error: read as
|
||||
// one, the reading had no windows and an error, and the switch, which
|
||||
// stays on an account it can't read, left Claude Code spending overages
|
||||
// with other accounts that had room (StringKe, #4532).
|
||||
var claudeOverages = regexp.MustCompile(`(?i)using your overages`)
|
||||
|
||||
// claudeOveragesWindow is the window an account on overages is read with,
|
||||
// beside ws: spent (usedPast, roomLeft), so the switch moves Claude Code
|
||||
// off it as off one whose window is used up, and told as its text. It
|
||||
// ends when the last of ws's used-up windows renews, as Claude Code's
|
||||
// overages do; none named, at the next reading.
|
||||
func claudeOveragesWindow(ws []QuotaWindow) QuotaWindow {
|
||||
// the GUI says a Text row used 100 "In use" in the reader's language
|
||||
w := QuotaWindow{Name: "Extra usage", Used: 100, Display: "In use", Text: true}
|
||||
for _, x := range ws {
|
||||
if x.Used >= 100 && x.ResetsAt != nil && (w.ResetsAt == nil || x.ResetsAt.After(*w.ResetsAt)) {
|
||||
w.ResetsAt = x.ResetsAt
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// parseClaudeUsage reads /usage's windows; now dates a reset that names no
|
||||
// year.
|
||||
@@ -109,6 +135,9 @@ func parseClaudeUsage(text string, now time.Time) ([]QuotaWindow, error) {
|
||||
}
|
||||
out = append(out, w)
|
||||
}
|
||||
if slices.ContainsFunc(lines, claudeOverages.MatchString) {
|
||||
out = append(out, claudeOveragesWindow(out))
|
||||
}
|
||||
if len(out) == 0 {
|
||||
if text == "" || slices.ContainsFunc(lines, func(line string) bool {
|
||||
return strings.TrimSuffix(strings.TrimSpace(line), ".") == "You are currently using your subscription to power your Claude Code usage"
|
||||
|
||||
@@ -191,7 +191,6 @@ func TestClaudeUsageFailureDoesNotHideAccountErrors(t *testing.T) {
|
||||
subscriptionNotice + ".\nHTTP 401 Unauthorized",
|
||||
"Current session: 40% used\nHTTP 401 Unauthorized",
|
||||
"You've hit your limit · resets 3:30am (UTC)",
|
||||
"You are currently using your overages to power your Claude Code usage. We will automatically switch you back to your subscription rate limits when they reset",
|
||||
} {
|
||||
t.Run(text, func(t *testing.T) {
|
||||
out, card := claudeUsageCards(t, "Current session: 40% used", nil)
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// claudeOveragesText is Claude Code's /usage for an account past a limit
|
||||
// and billed for what it uses: its overages line, then its windows (the
|
||||
// strings as Claude Code 2.1.316 has them).
|
||||
const claudeOveragesText = `You are currently using your overages to power your Claude Code usage. We will automatically switch you back to your subscription rate limits when they reset
|
||||
|
||||
Current session: 100% used · resets 3pm (UTC)
|
||||
Current week (all models): 40% used · resets Nov 6 at 11am (UTC)
|
||||
`
|
||||
|
||||
// StringKe (#4532): the overages line made the whole reading an error,
|
||||
// so the card showed no windows and the switch, which stays on an account
|
||||
// it can't read, left Claude Code on overages. It is read with the
|
||||
// windows, and a spent "Extra usage" row that ends with the window used
|
||||
// up.
|
||||
func TestClaudeUsageOverages(t *testing.T) {
|
||||
now := time.Date(2026, 11, 1, 13, 0, 0, 0, time.UTC)
|
||||
ws, err := parseClaudeUsage(claudeOveragesText, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(ws) != 3 || ws[0].Name != "5 hours" || ws[0].Used != 100 || ws[1].Name != "7 days" || ws[1].Used != 40 {
|
||||
t.Fatalf("windows: %+v", ws)
|
||||
}
|
||||
x := ws[2]
|
||||
if x.Name != "Extra usage" || !x.Text || x.Used != 100 || x.ResetsAt == nil || !x.ResetsAt.Equal(*ws[0].ResetsAt) {
|
||||
t.Fatalf("overages row: %+v", x)
|
||||
}
|
||||
if !usedUp(SubscriptionQuota{Windows: ws}, now) {
|
||||
t.Fatal("an account on overages read as having room")
|
||||
}
|
||||
// past the session's reset, the row is gone with it
|
||||
if usedPast(SubscriptionQuota{Windows: elapsed(ws, now.Add(3*time.Hour))}, 98, now.Add(3*time.Hour)) {
|
||||
t.Fatal("still spent after the used-up window renewed")
|
||||
}
|
||||
// on overages with every window under the line it names (a model's
|
||||
// own week used up), the account is still spent, till the next reading
|
||||
text := strings.Replace(claudeOveragesText, "Current session: 100%", "Current session: 10%", 1)
|
||||
ws, err = parseClaudeUsage(text, now)
|
||||
if err != nil || len(ws) != 3 || ws[2].ResetsAt != nil || !usedPast(SubscriptionQuota{Windows: ws}, 98, now) {
|
||||
t.Fatalf("overages, windows under the line: %+v %v", ws, err)
|
||||
}
|
||||
}
|
||||
|
||||
// The switch moves Claude Code off an account on overages to a spare
|
||||
// with room, as off one whose window is used up.
|
||||
func TestClaudeSwitchOffOverages(t *testing.T) {
|
||||
claudeThree(t) // pro@x.com signed in; team and max saved
|
||||
var text atomic.Value
|
||||
text.Store("")
|
||||
fakeClaudeUsage(t, &text, nil)
|
||||
forget := func() {
|
||||
lastQuotas.Lock()
|
||||
lastQuotas.m, lastQuotas.loaded = nil, false
|
||||
lastQuotas.Unlock()
|
||||
loginUsageCache.Lock()
|
||||
loginUsageCache.m = nil
|
||||
loginUsageCache.Unlock()
|
||||
}
|
||||
forget()
|
||||
t.Cleanup(forget)
|
||||
if err := SetSwitchOnly("claude", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spare := ""
|
||||
for _, l := range Logins("claude") {
|
||||
if strings.HasPrefix(l.User, "max@x.com") {
|
||||
spare = l.User
|
||||
if err := SetLoginOn("claude", l.User, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// windows under the line, so only the overages line says it's spent
|
||||
reset := time.Now().UTC().Add(72*time.Hour).Format("Jan 2 at 3pm") + " (UTC)"
|
||||
under := "Current session: 10% used · resets " + reset + "\nCurrent week (all models): 40% used · resets " + reset + "\n"
|
||||
var mu sync.Mutex
|
||||
active := under
|
||||
UsageClaudeVia(func(_ context.Context, dir string) (string, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if dir == "" {
|
||||
return active, nil
|
||||
}
|
||||
return under, nil // the spare
|
||||
})
|
||||
ctx := context.Background()
|
||||
if to, err := SwitchWhenSpent(ctx, "claude"); err != nil || to != "" {
|
||||
t.Fatalf("with room: switched to %q: %v", to, err)
|
||||
}
|
||||
mu.Lock()
|
||||
active = strings.SplitN(claudeOveragesText, "\n", 2)[0] + "\n\n" + under
|
||||
mu.Unlock()
|
||||
forget()
|
||||
claudeUsage.Lock()
|
||||
for k, e := range claudeUsage.m {
|
||||
e.tried, e.at = e.tried.Add(-time.Hour), e.at.Add(-time.Hour)
|
||||
claudeUsage.m[k] = e
|
||||
}
|
||||
claudeUsage.Unlock()
|
||||
if to, err := SwitchWhenSpent(ctx, "claude"); err != nil || to != spare {
|
||||
t.Fatalf("on overages: switched to %q: %v, want %q", to, err, spare)
|
||||
}
|
||||
for user, u := range LoginUsage(ctx, "claude") {
|
||||
if strings.HasPrefix(user, "pro@x.com") && (u.Error != "" || len(u.Windows) != 3) {
|
||||
t.Fatalf("the overages account's card: %+v", u)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,8 @@ type QuotaWindow struct {
|
||||
// (AutoClaw's 积分余额 and 每日签到, Cursor's on-demand spend, ZCode's
|
||||
// gift to claim), which plugins send set aside with used 0 and no
|
||||
// count. Its Used is no reading, so it is told as its text, never as
|
||||
// "100% left".
|
||||
// "100% left". Claude's "Extra usage" row (claudeOveragesWindow) is
|
||||
// the one Text window with Used 100: the account counts as spent.
|
||||
Text bool `json:"text,omitempty"`
|
||||
// Amount of Limit is the window's own count in Unit, when the vendor
|
||||
// counts it so (WorkBuddy's credits): Used is Amount as a share of
|
||||
|
||||
Reference in New Issue
Block a user