Files
magpie/internal/provider/claude_usage_cli.go
T
yetone 3a74cd92e8 claude: /usage waits for its figures, and a read without them is unknown
Seagull fries (haioushutiao, Discord #help; magpie 0.1.2704, Claude Code
2.1.316, Windows 11, Claude Pro): the Claude card said 暂时无法获取额度,
"Claude Code's /usage told no allowance: ❯ /usage ─── Settin s Status
Config Usage …". Reproduced with a real Claude Code 2.1.316 under
Windows' pseudo console in a sandbox: /usage's panel is drawn first with
"Loading usage data…" while Claude Code asks Anthropic for the figures
(5s timeout, retried only on a 401), and the helper took 3s of a still
screen as the panel drawn whole. On a slow answer it told the panel's
frame as the result, and parseClaudeUsage called any text with no window
"told no allowance". A failed load ("Error: Failed to load usage data",
"r to retry") went the same way.

claudetui:
- screen.go: a small terminal grid (CUP/CUU..CHA/VPA, EL, ED, ECH, CR/LF
  with scrolling, OSC/DCS skipped, an escape or UTF-8 character cut
  between reads kept), 200x50 on Windows as the pseudo console is made,
  80x24 elsewhere. The pseudo console sends only the cells that changed,
  skipping a cell that holds the same letter ("Settin\x1b[1Cs"), so the
  stream read split words and kept a redrawn digit beside the old one.
  While /usage is read, each look of the screen with a window is kept
  (panel), and usageText reads that before the stream.
- command(), built on b4073c06a's wait for the panel (ikunbots; its
  usageDrawn and "hadn't drawn" error kept, now also judged on the grid,
  since the pseudo console's split "Settin s" never matched its tabs):
  "Loading usage data" on screen is waited out (to
  commandWait, s.doing "was still loading /usage's figures" for the
  deadline message); "Failed to load usage data" gets one "r"; a panel
  still with no figure and loading or failed on screen is an
  error_during_execution result, "/usage couldn't load the plan's usage:
  <screen>".
- the fake draws the panel a frame at a time (fakeFrames), as Claude Code
  does: with one write, the pseudo console passed on only the last
  frame, which no longer shows the windows. TestShimReadsUsage now passes
  on Windows (#4820, the fake's side; a real panel with figures was not
  seen on Windows, see below).

provider:
- parseClaudeUsage: a panel with no figure is "Claude Code's /usage
  showed no usage figures: …", not "told no allowance" (kept for
  claudeUsageDenied's refusals).
- claudeUsageUnknown: Claude Code's "temporarily unavailable" and the
  helper's "couldn't load the plan's usage" keep the account's reported
  reading (quota_last), as the first did alone before. Other no-figure
  text is still shown as an error (TestClaudeHeaderSurvivesUnclassified-
  UsageFailure, TestClaudeUsageReadingAloneDoesNotHideUnclassifiedFailure
  hold that).

Fixtures are the reporter's case as the pseudo console passed it on for
the real 2.1.316 (a temporary dump of the helper's screen bytes, sandbox config,
proxy that never answers / refuses): testdata/usage-loading-win-2.1.316.bin
and usage-failed-win-2.1.316.bin.

Run at the pushed head:
- mote: go test ./internal/claudetui ./internal/provider ok; go test
  -run 'Claude|Usage|TUI' ./internal/gateway ok; GOOS=darwin/linux/
  windows go vet ./internal/claudetui ./internal/provider ok; gofmt -l
  clean.
- xiaomi-gpu (win-run): go test ./internal/claudetui: all pass except
  TestShimFirstMessageWaitsForTools, red on Windows at main (#4810).
- xiaomi-gpu, real Claude Code 2.1.316 (npm), sandbox CLAUDE_CONFIG_DIR
  with a fake token, magpie.exe built from this tree, helper run as the
  gateway runs it: a proxy that never answers gives is_error "/usage
  couldn't load the plan's usage: … Settings Status … Loading usage data…"
  after the 20s wait (was a success with the chrome); a refused proxy
  shows "Failed to load usage data", "r" is pressed, and the same error
  comes back in 6.6s. "Settings" is whole now.

Broken by hand (before the rebase onto b4073c06a; the merged loop is
re-checked below):
- old tui.go: TestShimWaitsForUsageFigures slow, retried and failed again
  all red, each telling the reporter's chrome as success.
- the "r" not written: TestShimWaitsForUsageFigures/retried red.
- old parse message: TestClaudeUsageWithoutFiguresIsUnknown red ("told
  no allowance"); old quota_last check: its couldn't-load cases red.
- after the rebase, on the merged loop: the "r" not written,
  TestShimWaitsForUsageFigures/retried red; the old "anything still 3s"
  in place of usageDrawn on the grid, TestShimWaitsPastTheBanner red.

Not tried: a real account (the box has none of the owner's), so a real
2.1.316 panel with figures was never seen through the pseudo console;
whether Claude Code's frames that show the windows reach the pseudo
console's output before it renders the next one is shown only by the
fake. Not taken: asking Anthropic's /api/oauth/usage, which the owner
ruled out (9bf6d4352).
2026-11-03 05:16:40 +08:00

294 lines
11 KiB
Go

package provider
// A Claude account's allowance, as Claude Code's own /usage tells it:
// magpie runs `claude -p /usage` (a command Claude Code answers itself,
// asking no model) and reads its lines, never asking Anthropic itself.
import (
"context"
"errors"
"regexp"
"slices"
"strconv"
"strings"
"sync"
"time"
// the zone /usage names its resets in: Windows has no zone database,
// and without one a reset was read in the machine's own zone
_ "time/tzdata"
)
// claudeCLIUsage runs Claude Code's /usage in configDir, or its own
// directory when empty, and is what it printed. The gateway sets it
// (UsageClaudeVia). It is held for reading while it runs, as
// loginUsageFor is: Allowances reads Claude's usage in the background, and
// tests swap it.
var claudeCLIUsage struct {
sync.RWMutex
f func(ctx context.Context, configDir string) (string, error)
}
// UsageClaudeVia sets how Claude Code's /usage runs. It returns once no
// run is still going through the one it replaces.
func UsageClaudeVia(f func(ctx context.Context, configDir string) (string, error)) {
claudeCLIUsage.Lock()
claudeCLIUsage.f = f
claudeCLIUsage.Unlock()
}
// runClaudeUsage is what Claude Code's /usage printed, through
// claudeCLIUsage; errClaudeCannotRun when nothing set it.
func runClaudeUsage(ctx context.Context, configDir string) (string, error) {
claudeCLIUsage.RLock()
defer claudeCLIUsage.RUnlock()
if claudeCLIUsage.f == nil {
return "", errClaudeCannotRun
}
return claudeCLIUsage.f(ctx, configDir)
}
// readClaudeUsage is the allowance of the account in configDir, from
// Claude Code's /usage. Empty reads the account it is signed in to.
func readClaudeUsage(ctx context.Context, configDir string) ([]QuotaWindow, error) {
text, err := runClaudeUsage(ctx, configDir)
if err != nil {
return []QuotaWindow{}, err
}
return parseClaudeUsage(text, time.Now())
}
// "Current session: 13% used · resets Oct 1 at 3:30pm (Asia/Shanghai)",
// "Current week (all models): 4% used · resets Oct 3 at 2pm (Asia/Shanghai)",
// "Current week (Fable): 0% used"
var claudeUsageLineRE = regexp.MustCompile(`^Current (session|week(?: \(([^)]+)\))?):\s*([0-9.]+)% used(?:\s*·\s*resets (.+))?$`)
// A successful /usage run can tell only how the subscription is billed,
// without an allowance. This says nothing about the account's limits.
var errClaudeUsageUnavailable = errors.New("Claude Code's /usage is temporarily unavailable")
// claudeUsageUnknown is a /usage read that told nothing of the account,
// for a reason that passes: Claude Code's own "temporarily unavailable",
// or a panel that couldn't load its figures (the helper's "/usage
// couldn't load the plan's usage": still "Loading usage data…", or
// "Failed to load usage data" after a retry). The card keeps the
// account's last reading. Windows' pseudo console showed 2.1.316's panel
// while it still loaded, and the card said the account had no allowance
// (Seagull fries).
var claudeUsageUnknown = regexp.MustCompile(`^(?:Claude Code: )?(?:` + regexp.QuoteMeta(errClaudeUsageUnavailable.Error()) + `|/usage couldn't load the plan's usage)`)
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`)
// 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.
func parseClaudeUsage(text string, now time.Time) ([]QuotaWindow, error) {
out := []QuotaWindow{}
text = strings.TrimSpace(ansi.ReplaceAllString(text, ""))
lines := strings.Split(text, "\n")
// Account errors take precedence over notices (and partial readings).
for _, line := range lines {
if claudeUsageDenied.MatchString(line) {
return out, errors.New("Claude Code's /usage told no allowance: " + clipLine(strings.TrimSpace(line)))
}
}
const week = 7 * 24 * time.Hour
for _, line := range lines {
m := claudeUsageLineRE.FindStringSubmatch(strings.TrimSpace(line))
if m == nil {
continue
}
used, err := strconv.ParseFloat(m[3], 64)
if err != nil {
continue
}
w := QuotaWindow{Name: "5 hours", Used: used, Span: 5 * time.Hour}
if m[1] != "session" {
w.Name, w.Span = "7 days", week
// "Sonnet only" (Claude Code 2.1.316's week of Sonnet) is the
// Sonnet week: "7 days · Sonnet", counting sonnet, the window
// its seven_day_sonnet header keeps (claudeKinds). Read whole,
// it counted models named "sonnet-only", which none is, so
// routing never saw the Sonnet week used up (StringKe, #4532)
scope := strings.TrimSpace(m[2])
if s, ok := strings.CutSuffix(strings.ToLower(scope), " only"); ok && s != "" {
scope = strings.TrimSpace(scope[:len(s)])
}
if scope != "" && !strings.EqualFold(scope, "all models") {
w.Name, w.Model = "7 days · "+scope, claudeScopeModel(scope)
}
}
if slices.ContainsFunc(out, func(x QuotaWindow) bool { return x.Name == w.Name }) {
continue
}
if t, ok := claudeResetTime(m[4], now, w.Span); ok {
w.ResetsAt = &t
}
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"
}) {
return out, errClaudeUsageUnavailable
}
// a panel with no figure says nothing of the account's allowance:
// unknown, its words told
first, _, _ := strings.Cut(text, "\n")
return out, errors.New("Claude Code's /usage showed no usage figures: " + clipLine(first))
}
return out, nil
}
func clipLine(s string) string {
if r := []rune(s); len(r) > 120 {
return string(r[:120]) + "…"
}
return s
}
// claudeResetTime reads "Oct 1 at 3:30pm (Asia/Shanghai)", "Oct 9, 2:59pm
// (UTC)" (Claude Code 2.1.285 on) or "3pm (Asia/Shanghai)", in the zone
// named, else local time. It ends a window span long, so it is never further
// ahead than that (and claudeResetSlack): a date with no year is in the first
// year it is less than a day past and no further ahead, and a time alone is
// the next one from now no further ahead. Where none is, as for a reading
// days old or a clock that is off, it is in the latest year (or day) where
// it is already past, and the window reads as renewed (elapsed). Where the
// clocks go back and read that time twice, the reading not yet past is
// taken.
func claudeResetTime(s string, now time.Time, span time.Duration) (time.Time, bool) {
s = strings.TrimSpace(s)
if s == "" {
return time.Time{}, false
}
loc := time.Local
if i := strings.LastIndex(s, "("); i >= 0 && strings.HasSuffix(s, ")") {
if l, err := time.LoadLocation(s[i+1 : len(s)-1]); err == nil {
loc = l
}
s = strings.TrimSpace(s[:i])
}
s = strings.ReplaceAll(strings.ReplaceAll(s, "AM", "am"), "PM", "pm")
ref := now.In(loc)
latest := ref.Add(span + claudeResetSlack)
for _, layout := range []string{
"Jan 2 at 3:04pm", "Jan 2 at 3pm", "Jan 2, 2006 at 3:04pm", "Jan 2, 2006 at 3pm",
"Jan 2, 3:04pm", "Jan 2, 3pm", "Jan 2, 2006, 3:04pm", "Jan 2, 2006, 3pm",
} {
t, err := time.ParseInLocation(layout, s, loc)
if err != nil {
continue
}
if t.Year() != 0 {
return t, true
}
// Dec 31 read just after New Year is last year's
var past time.Time
for y := ref.Year() - 1; y <= ref.Year()+1; y++ {
d := time.Date(y, t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, loc)
if r, ok := nextReading(d, ref, ref.Add(-24*time.Hour)); ok && !r.After(latest) {
return r, true
}
if d.Before(ref) {
past = d
}
}
return past, true
}
for _, layout := range []string{"3:04pm", "3pm"} {
c, err := time.ParseInLocation(layout, s, loc)
if err != nil {
continue
}
// a day whose clocks skip that time is passed over: Santiago's skip
// midnight, so "12:30am" before it is the day after tomorrow's
for d := 0; d <= 2; d++ {
t := time.Date(ref.Year(), ref.Month(), ref.Day()+d, c.Hour(), c.Minute(), 0, 0, loc)
if t.Hour() != c.Hour() || t.Minute() != c.Minute() {
continue
}
if r, ok := nextReading(t, ref, ref); ok {
if !r.After(latest) {
return r, true
}
break
}
}
for d := 0; d >= -2; d-- {
t := time.Date(ref.Year(), ref.Month(), ref.Day()+d, c.Hour(), c.Minute(), 0, 0, loc)
if t.Hour() == c.Hour() && t.Minute() == c.Minute() && t.Before(ref) {
return t, true
}
}
return time.Time{}, false
}
return time.Time{}, false
}
// claudeResetSlack is how much further ahead than its window a reset may
// read, as this machine's clock may be behind Anthropic's. Claude Code
// prints the reset from the instant Anthropic gives, seconds dropped, so it
// is never later than that.
const claudeResetSlack = 2 * time.Hour
// nextReading is the first instant whose clock reads as t's does that isn't
// before ref, else the first that isn't before lim. As the clocks go back, an
// hour is read twice, and time.Date may give either instant.
func nextReading(t, ref, lim time.Time) (time.Time, bool) {
first, last := t, t
start, end := t.ZoneBounds()
_, off := t.Zone()
if !start.IsZero() {
_, prev := start.Add(-time.Second).Zone()
if e := t.Add(time.Duration(off-prev) * time.Second); prev > off && e.Before(start) {
first = e
}
}
if !end.IsZero() {
_, next := end.Zone()
if l := t.Add(time.Duration(off-next) * time.Second); next < off && !l.Before(end) {
last = l
}
}
for _, from := range []time.Time{ref, lim} {
for _, r := range []time.Time{first, last} {
if !r.Before(from) {
return r, true
}
}
}
return time.Time{}, false
}