fix: stop refetching usage every render on accounts without rate-limit windows
Closes #343
Enterprise accounts have no 5-hour/7-day rate-limit windows, so the usage API returns five_hour: null and seven_day*: null (#343). Those buckets parse to utilization 0 with no resets_at, which leaves the cache permanently "incomplete" for any widget that requires a reset timestamp: fetchUsageData then bypasses the cache and hits the live API on every render. At a 15s refresh interval Anthropic starts answering 429 with Retry-After: 300, and the lock makes the usage/money widgets flicker [Rate limited] / [Timeout] indefinitely.
Observed on a real Enterprise account: the API answers 200 with all windows null, the cache holds {"sessionUsage":0,...} with no reset fields, and the lock sits at {"blockedUntil":...,"error":"rate-limited"} in a permanent refetch/backoff cycle.
| Area | Change |
|---|---|
usage-fetch.ts |
a missing window reset field counts as satisfied once its paired utilization is cached: a null bucket parses to utilization 0 with no resets_at, so refetching can never produce the timestamp |
usage-fetch.test.ts |
probe test with the Enterprise response shape: first run fetches once, a second probe 10s later is served entirely from cache (zero requests) |
Notes:
- Same conclusiveness idea as #416, which covers the sibling
monthly_limit: nullcase; the two PRs touch the same function and either rebases trivially on the other. - No rendering changes; widgets display exactly what they did before, they just stop hammering the API.
@zachthedev If a user with an enterprise account logs out and logs back in with a personal account, will the usage ever refresh again if the enterprise sentinel is written?
@zachthedev If a user with an enterprise account logs out and logs back in with a personal account, will the usage ever refresh again if the enterprise sentinel is written?
Good point, I can try with my setup.
@sirmalloc
If a user with an enterprise account logs out and logs back in with a personal account, will the usage ever refresh again if the enterprise sentinel is written?
No. The sentinel only gates completeness (hasRequiredUsageField); the cache is served only when also fresh: cacheAge < CACHE_MAX_AGE (180s) && hasRequiredUsageFields(...), ANDed. This PR touches only the second term, so a stale cache still refetches. After enterprise->personal it refetches at the 180s mark with the new token and the real windows replace it; the sentinel is recomputed per call (not persisted).
During that window it serves the prior snapshot, which is the existing <=180s lag of a non-account-keyed cache, not something the sentinel introduces.
#460 drops that 180s: it fingerprints the token into the cache and invalidates on mismatch. Orthogonal to this fix.