oficina / equipo Public

Treat a Claude usage 401 as transient, not a refusal

A 401 means the access token is stale or revoked; the CLI renews it on
its next turn. Marking it as "Usage not available for this sign-in"
for a week would mislabel an idle login. Only 403 and 404 now count as
refusals.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude pushed by paul@keymail.dev 09ae90f08e9c677751ac51a79a7715eba5808a44 parent 12ea906
2 files changed, +7 −7
  • internal/runner/thin/claude_usage.go +3 −3
  • internal/runner/thin/claude_usage_test.go +4 −4

CI passed — run details

diff --git a/internal/runner/thin/claude_usage.go b/internal/runner/thin/claude_usage.go
index f63642b0..9980edc7 100644
--- a/internal/runner/thin/claude_usage.go
+++ b/internal/runner/thin/claude_usage.go
@@ -102,8 +102,8 @@ func claudeUsage(raw []byte) *connect.Limit {
return &connect.Limit{Headroom: h}
}
-// readClaudeUsage remembers endpoint refusals for one token for a week.
-// Transient errors retain the last reading and are retried on the next check.
+// readClaudeUsage remembers 403/404 refusals for one token for a week; a 401
+// (a stale or revoked token) is retried like a transient error.
func (c *Conns) readClaudeUsage(ctx context.Context, id string, token []byte) {
c.usageMu.Lock()
defer c.usageMu.Unlock()
@@ -132,7 +132,7 @@ func (c *Conns) readClaudeUsage(ctx context.Context, id string, token []byte) {
c.changed()
return
}
- if status != 401 && status != 403 && status != 404 {
+ if status != 403 && status != 404 {
return
}
if _, err = c.State.db.ExecContext(ctx, "INSERT INTO conn_usage(id,mark,read_at) VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET mark=excluded.mark, read_at=excluded.read_at", id, tokenMark, c.clock().UTC().Format(time.RFC3339Nano)); err != nil {
diff --git a/internal/runner/thin/claude_usage_test.go b/internal/runner/thin/claude_usage_test.go
index b709a657..7ccb7fab 100644
--- a/internal/runner/thin/claude_usage_test.go
+++ b/internal/runner/thin/claude_usage_test.go
@@ -91,17 +91,17 @@ func TestClaudeUsageRefresh(t *testing.T) {
if h.Windows[0].Minutes != 300 || h.Windows[1].Minutes != 10080 || h.Windows[0].ResetsAt.Format(time.RFC3339) != "2026-11-14T14:00:00Z" {
t.Fatal("windows changed")
}
- } else if status != 500 && (held.Headroom == nil || !held.Headroom.Unavailable || held.Headroom.CheckedAt != nil) {
+ } else if status != 500 && status != 401 && (held.Headroom == nil || !held.Headroom.Unavailable || held.Headroom.CheckedAt != nil) {
t.Fatalf("refusal %+v", held.Headroom)
- } else if status == 500 && held.Headroom != nil {
- t.Fatal("500 invented a reading")
+ } else if (status == 500 || status == 401) && held.Headroom != nil {
+ t.Fatalf("%d invented a reading or a refusal", status)
}
// A restart and Check again both respect a refused endpoint's backoff.
r.conns = &Conns{Runner: r.cfg, State: r.state}
setup()
at = at.Add(time.Hour)
r.conns.CheckAgain(context.Background(), b.ID)
- denied := status == 401 || status == 403 || status == 404
+ denied := status == 403 || status == 404
want := 2
if denied {
want = 1