Repository navigation
fix(server): Claude usage served from a saved reading keeps its real age - #38778
iamshadmantaqi wants to merge 2 commits into
Conversation
…its fetch time Fails on main: the probe stamps the saved reading with the probe time.
While /api/oauth/usage is rate-limited, Claude Code answers get_usage from the reading it saved in .claude.json and does not say so. The probe stamped that reading with the probe time, so an hour-old reading showed as just checked. Usage that matches the saved reading now takes its fetch time, never later than the probe.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Problem
While
/api/oauth/usageis rate-limited, Claude Code answers the SDK'sget_usagefrom the reading it saved in the login's.claude.json(cachedUsageUtilization, up to an hour old), and the response does not say so.probeClaudeCapabilitiesstamps every successful usage read with the probe time (usageCheckedAt), and that becomesusageLimits.checkedAt. So each probe movescheckedAtforward while the numbers stay frozen, and an hour-old reading shows as just checked.Change
After a successful probe, the Claude driver already reads the login's
.claude.jsonfor the organisation id. It now also readscachedUsageUtilization. When every usage window in theget_usageresponse matches that saved reading,usageCheckedAtbecomes the savedfetchedAtMs, never later than the probe. When the numbers differ, the response came from a fresh read and keeps the probe time.This is safe in the fresh case too: Claude Code rewrites the saved reading on every successful read unless it is under a minute old, so a match means the numbers are at most a minute older than
fetchedAtMs.Scope
Refs #38070
This covers the first problem in the issue: cached data shown as freshly checked. It does not change how often
readClaudeResetCreditscalls/api/oauth/usageor add a back-off on 429. #33979 is open for the reset-credit side, so that part stays out of this PR.Verification
Added one case to
apps/server/src/provider/ProviderInstanceRegistry.test.ts. It runs the Claude driver with a mocked SDK whoseget_usagereturns the same numbers as a saved reading fetched 40 minutes before the probe. It then changes the numbers and checks that a fresh read keeps the probe time.Without the fix (test commit only):
With the fix:
vp run check:changed: format, lint, knip, graphql, typecheck (3 packages) and tests (3 files) all pass.I checked the Claude Code 2.1.326 binary for this behaviour. Its usage read serves the saved snapshot with
source: "persisted"on a failed fetch, answers from a snapshot under 60 s old without asking the endpoint ("Usage read answered from a snapshot Ns old; endpoint not asked"), and skips rewriting the snapshot while it is under 60 s old.Not verified: I did not reproduce a live 429 against a real login, so the end-to-end path through a real rate-limited CLI is untested.
Model: Claude Opus 5.5. Harness: Claude Code in T3 Code.