Files
magpie/internal/provider/quota_last.go
T
yetone 77031d5817 claude usage: a /usage panel with no window is no window started, and a failure says why
koohoko5924 (Discord #help, v0.1.3057, macOS): a Claude Enterprise
account with a five-hour window only. A manual refresh took ~5s and
gave the card no window and the error "Claude Code's usage showed no
usage figures: ▐▛███▜▌ … magpie-claude-work…": Claude Code's banner,
not a reason.

Claude Code 2.1.316's Usage tab (gi) draws a window only once
Anthropic's answer has it non-null, and a window is null until the
request that starts it. An idle 5h-only account's panel loads with
the session's cost, maybe the plan's credit bar, and no window. The
helper told that panel's words, joined from the top of the screen, and
the card took them as a failure with the banner first.

- claudetui: a panel that loaded (tabs, Session/Total cost, nothing
  loading, refreshing, stale or failed, no gateway Spend limit) with no
  "Current session"/"Current week" is told as NoUsageWindow ("No usage
  window has started"). The provider reads that as no window started:
  no error, the unstarted 5h window as for any account.
- claudetui: the two rate-limit errors gi draws ("Claude is temporarily
  limiting requests to the usage page (not your usage limit)…",
  "Usage endpoint is rate limited until …") are failures to load, like
  "Failed to load usage data": retried once, then told as an error with
  the panel's own "Error: …" text, never the banner. A panel with no
  window and no error is told from its tabs on, the session's cost
  lines left out.
- provider: "(not your usage limit)" no longer reads as an account
  refused (claudeDenied, at every place claudeUsageDenied judged a
  reading: the CLI parse, codex_switch, quota_last, subscription_usage).
  Counts ("Last 7d · 403 requests …") are taken out wherever they sit in
  a line before that check, as the panel's words come on one line now.
  A no-figures error keeps every line (600 runes), not the first clipped
  at 120.

Tests are built from 2.1.316's own strings (ko/gi/hi in the bundled
cli.js), drawn on the helper's 200x50 grid row by row. Guessed: the
exact bytes of a real Enterprise panel (not captured; no such account
here) and the credit bar's layout.

Run (mote, at this tree): go test ./internal/claudetui ./internal/provider
-run 'Usage|Claude|Shim|Quota|Codex' ok; go vet for claudetui, provider,
gateway; GOOS=linux -tags nogui and GOOS=windows go vet; gofmt -l clean.

Broken by hand (each red, then restored):
- the NoUsageWindow case off: TestShimReadsUsageWithNoWindow red in all
  three panels (told the panel's words as success);
- the provider's claudeNoWindow branch off: TestClaudeUsageNoWindowStarted
  red ("showed no usage figures: No usage window has started");
- claudeDenied back to claudeUsageDenied: TestClaudeUsageWhyKept red
  ("read as an account refused");
- usageFailed back to "Failed to load usage data" and the whole screen as
  the reason: TestShimToldWhyUsageFailed red in all three (the banner as
  the reason; the rate-limit panels told as no window), TestUsageNoWindow
  red.

Not tried: a real Claude Enterprise account; a real rate-limited panel.
2026-11-06 06:30:23 +08:00

230 lines
6.8 KiB
Go

package provider
// The last allowance each account reported, kept on disk, so a vendor
// turning the usage endpoint away for a while (Anthropic's 429s) or not
// answering shows what was last read, not an error — across restarts too.
// A reading that comes back replaces it at once.
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
var lastQuotas lastQuotasT
type lastQuotasT struct {
sync.Mutex
m map[string]lastQuota // provider/user
loaded bool
}
type lastQuota struct {
At time.Time `json:"at"`
Q SubscriptionQuota `json:"quota"`
// what the windows' json leaves out, for routing
Spans []lastWindow `json:"spans,omitempty"`
}
type lastWindow struct {
Span time.Duration `json:"span,omitempty"`
Model string `json:"model,omitempty"`
Aside bool `json:"aside,omitempty"`
}
func lastQuotasPath() string { return filepath.Join(filepath.Dir(Path()), "quotas.json") }
// passing is an error that says nothing about the account: the vendor
// rate limiting, failing or out of reach, or a Claude account's usage not
// read this time (errClaudeNotAsked, errClaudeSaved), or one a setup-token
// can't read at all (errClaudeSetupToken). A sign-in gone bad
// is not one.
var passing = regexp.MustCompile(`(?i)not read yet|a setup-token can't read|doesn't read a saved account|rate.?limit|too many requests|429|internal server error|bad gateway|service unavailable|gateway timeout|deadline exceeded|timeout|timed out|connection (refused|reset)|no such host|network is unreachable|EOF|asks again|fetch failed|unable to connect|ECONN[A-Z]+|ENOTFOUND|EAI_AGAIN|socket hang up|HTTP 5\d\d`)
// keyTag names a key's card among the last readings, by what it is (a
// plan's windows, a balance) and a digest of the key, never the key: a
// key's card has no user to go by.
func keyTag(kind, key string) string {
sum := sha256.Sum256([]byte(key))
return kind + " " + hex.EncodeToString(sum[:8])
}
// keepLast is q, or the last reading of its account when q failed in
// passing; a good reading is kept for next time.
func keepLast(q SubscriptionQuota, user string) SubscriptionQuota {
if user == "" {
user = q.User
}
if user == "" || q.Provider == "" {
return q
}
key := q.Provider + "/" + strings.ToLower(user)
c := &lastQuotas
c.Lock()
defer c.Unlock()
c.load()
if q.Error == "" {
if kept := c.m[key].Q; q.readSeq != 0 && kept.readSeq > q.readSeq {
kept.Name, kept.Icon, kept.User = q.Name, q.Icon, q.User
return kept
}
if len(q.Windows) == 0 && q.Balance == "" {
return q
}
e := lastQuota{At: time.Now(), Q: q}
for _, w := range q.Windows {
if w.matches != nil {
return q // a pool that can't be written down isn't kept
}
e.Spans = append(e.Spans, lastWindow{w.Span, w.Model, w.Aside})
}
e.Q.Error = ""
e.Q.AsOf = nil
e.Q.ReadDeferred = false
if c.m == nil {
c.m = map[string]lastQuota{}
}
c.m[key] = e
if b, err := json.Marshal(c.m); err == nil {
os.MkdirAll(filepath.Dir(lastQuotasPath()), 0o700)
writeFileAtomic(lastQuotasPath(), b)
}
return q
}
if q.Provider == "claude" && claudeDenied(q.Error) {
return q
}
claudeUnavailable := q.Provider == "claude" && claudeUsageUnknown.MatchString(q.Error)
if !claudeUnavailable && !passing.MatchString(q.Error) {
return q
}
read := c.reading
if claudeUnavailable {
read = c.reported // keep the reported values, even after their reset
}
out, ok := read(key)
if !ok {
return q
}
out.ReadDeferred = readDeferred(q.Error)
out.Name, out.Icon, out.User = q.Name, q.Icon, q.User
if out.Name == "" {
out.Name, out.Icon = c.m[key].Q.Name, c.m[key].Q.Icon
}
return out
}
// hadReading is whether a good reading of provider's account (or key
// tag) user is kept: the account or key was read with windows or a
// balance before.
func hadReading(provider, user string) bool {
if provider == "" || user == "" {
return false
}
c := &lastQuotas
c.Lock()
defer c.Unlock()
c.load()
_, ok := c.m[provider+"/"+strings.ToLower(user)]
return ok
}
// readDeferred is whether err says no read was made this time (allowances
// read only when asked, a Claude account's /usage not run) rather than a
// read that failed. A read held back by holdUnasked fails inside the
// vendor's HTTP call, so its error carries errNotAsked wrapped: "Get
// \"https://…\": not read yet: …".
func readDeferred(err string) bool {
for _, e := range []error{errNotAsked, errClaudeNotAsked, errClaudeSaved} {
if strings.Contains(err, e.Error()) {
return true
}
}
return false
}
// load reads the last readings from disk, the first time. Called with c
// held.
func (c *lastQuotasT) load() {
if !c.loaded {
c.loaded = true
if b, err := os.ReadFile(lastQuotasPath()); err == nil {
json.Unmarshal(b, &c.m)
}
}
}
// reading is the last reading kept under key, its windows as they stand
// now. Called with c held.
func (c *lastQuotasT) reading(key string) (SubscriptionQuota, bool) {
out, ok := c.reported(key)
if ok {
now := time.Now()
// as read, before any window started again (none had by the zero
// time)
wasUp := usedUp(out, time.Time{})
out.Windows = elapsed(out.Windows, now)
// a hold that came with a window used up ends when that window
// starts again; one with no window used up stands till a fresh read
if out.Held && wasUp && !usedUp(out, now) {
out.Held = false
}
}
return out, ok
}
// reported is the dated snapshot, without treating a passed reset as a
// newly read zero. Called with c held.
func (c *lastQuotasT) reported(key string) (SubscriptionQuota, bool) {
e, ok := c.m[key]
if !ok {
return SubscriptionQuota{}, false
}
out := e.Q
out.Windows = make([]QuotaWindow, len(e.Q.Windows))
copy(out.Windows, e.Q.Windows)
for i := range out.Windows {
if i < len(e.Spans) {
out.Windows[i].Span, out.Windows[i].Model, out.Windows[i].Aside = e.Spans[i].Span, e.Spans[i].Model, e.Spans[i].Aside
}
}
at := e.At
out.AsOf = &at
return out, true
}
// lastAllowances is each of an agent's accounts' allowance as it last
// read, kept on disk: what routing goes by while the first reading since
// magpie started is still out, rather than by none — with none, every
// account counts as unused and the first in order went first, until the
// reading came back and put another first, taking conversations off the
// account that had them cached (vincentzhang on Discord).
func lastAllowances(agent string) map[string]Allowance {
logins, ok := usageLogins(agent)
if !ok {
return nil
}
now := time.Now()
out := map[string]Allowance{}
c := &lastQuotas
c.Lock()
defer c.Unlock()
c.load()
for _, l := range logins {
q, ok := c.reading(loginProvider(l) + "/" + strings.ToLower(l.User))
if !ok || len(q.Windows) == 0 {
continue
}
if a, ok := routedAllowance(agent, l.User, q, now); ok {
out[l.User] = a
}
}
return out
}