claude usage: a first reading that failed is tried again after the wait (@simonzyzy on X)

Run at the pushed head (rebased on origin/main): go test internal/sessions,
internal/usage, internal/provider ok; gofmt clean; go vet darwin, linux,
windows ok. Test red on the old code by stash. Not tried: a real Claude
Code with a failing /usage; the full suite.
This commit is contained in:
yetone
2026-11-11 09:04:52 +08:00
parent f2039c13fc
commit 296a9292c7
2 changed files with 40 additions and 1 deletions
+34
View File
@@ -499,3 +499,37 @@ func soon(n int) string { return time.Now().UTC().AddDate(0, 0, n).Format("Jan 2
func sessionReset() string {
return time.Now().UTC().Add(3*time.Hour).Format("Jan 2 at 3:04pm") + " (UTC)"
}
// A first reading that failed is tried again after the wait even when
// Claude Code wasn't used since: with nothing kept, an account whose
// Claude Code is never run through magpie showed "not read" for good.
func TestClaudeFirstReadFailureTriedAgain(t *testing.T) {
var out atomic.Value
out.Store("Current session: 40% used · resets " + sessionReset() + "\nCurrent week (all models): 10% used · resets " + soon(3) + " at 2pm (UTC)\n")
var fail atomic.Bool
runs := fakeClaudeUsage(t, &out, &fail)
claudeUsedSince = func(time.Time) bool { return false }
ctx := context.Background()
fail.Store(true)
if _, err := claudeWindows(ctx, "me@x", true); err == nil || runs.Load() != 1 {
t.Fatalf("a failing first read: %v, %d runs", err, runs.Load())
}
// before the wait is up it is not run again
if _, err := claudeWindows(ctx, "me@x", true); err == nil || runs.Load() != 1 {
t.Fatalf("before the wait: %v, %d runs", err, runs.Load())
}
claudeUsage.Lock()
e := claudeUsage.m["me@x"]
e.tried = e.tried.Add(-time.Hour)
claudeUsage.m["me@x"] = e
claudeUsage.Unlock()
fail.Store(false)
ws, err := claudeWindows(ctx, "me@x", true)
if err != nil || len(ws) == 0 || runs.Load() != 2 {
t.Fatalf("after the wait: %v %v, %d runs", ws, err, runs.Load())
}
// and once read, it is kept without another run
if _, err := claudeWindows(ctx, "me@x", true); err != nil || runs.Load() != 2 {
t.Fatalf("kept: %v, %d runs", err, runs.Load())
}
}
+6 -1
View File
@@ -896,7 +896,12 @@ func claudeWindows(ctx context.Context, user string, active bool) ([]QuotaWindow
// limit since (claudeLimitHitAfter, #4443)
due := e.tried.IsZero() || claudeAsked.Load() != 0 && asked > e.asked ||
active && now.Sub(e.tried) >= claudeWaitOf(e, now) && (e.heard.After(e.tried) || claudeUsedSince(e.tried)) ||
active && claudeLimitHitAfter(e.tried)
active && claudeLimitHitAfter(e.tried) ||
// a first reading that failed (Claude Code not on a GUI's PATH, a
// network blink) is tried again after the wait, as nothing is kept
// to show and nobody need have used Claude Code since: a user who
// never routes Claude through magpie would see "not read" for good
active && e.ws == nil && e.err != nil && now.Sub(e.tried) >= e.wait
// a saved account the switch off a spent one has no reading of
// (spareRead): read once, and again only after a wait while its read
// keeps failing, never every look