ccstatusline icon indicating copy to clipboard operation
ccstatusline copied to clipboard

fix: stop refetching usage every render on accounts without rate-limit windows

Open zachthedev opened this issue 5 months ago • 3 comments

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: null case; 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 avatar Jun 13 '26 01:06 zachthedev

@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?

sirmalloc avatar Jun 15 '26 18:06 sirmalloc

@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.

zachthedev avatar Jun 15 '26 18:06 zachthedev

@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.

zachthedev avatar Jun 16 '26 02:06 zachthedev