Skip to content

feat(usage): invalidate the usage cache when the account token changes#460

Merged
sirmalloc merged 3 commits into
sirmalloc:mainfrom
zachthedev:feat-usage-cache-account-invalidation
Jun 17, 2026
Merged

feat(usage): invalidate the usage cache when the account token changes#460
sirmalloc merged 3 commits into
sirmalloc:mainfrom
zachthedev:feat-usage-cache-account-invalidation

Conversation

@zachthedev

Copy link
Copy Markdown
Contributor

Closes #459

The cache was keyed only by CACHE_MAX_AGE, so a logout/login to another account served the prior account's usage until the 180s TTL.

Fingerprints the token (truncated SHA-256; an identifier, not the token) and persists it in the cache. fetchUsageData resolves the token first and gates the file-cache read on a fingerprint match; a mismatch refetches immediately.

  • No token: gate is a no-op (existing no-token path).
  • Pre-fingerprint caches refetch once on upgrade.
  • The OAuth token rotates per account, so this also busts on rotation (one extra refetch); stable account-id keying is out of scope.

Complements #434. Tests: account-switch (mismatch refetches) + same-account (match serves). bun run lint clean; bun test 1554/1554.

The usage cache was keyed only by CACHE_MAX_AGE, so a logout/login to a different account served the prior account stale usage until the 180s TTL. Fingerprint the token (truncated SHA-256, an identifier not the token) and persist it with the cache; fetchUsageData resolves the token first and gates the file-cache read on a fingerprint match, so a mismatch refetches immediately. No-token falls through to the existing path; pre-fingerprint caches refetch once on upgrade.

Closes sirmalloc#459

Co-Authored-By: Claude <noreply@anthropic.com>
Thread the current usage token fingerprint into stale-cache fallback handling so cached usage from a previous account is not returned when a lock is active or the API is unavailable.

This keeps account-switch invalidation consistent across fresh file-cache reads, active locks, rate-limit backoffs, API errors, and parse errors while preserving the existing no-token fallback behavior.

Add regression coverage for active-lock and rate-limited fallback paths with mismatched cached token hashes.
@sirmalloc sirmalloc merged commit 151521c into sirmalloc:main Jun 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

usage cache is not invalidated on account/login switch (stale until TTL)

2 participants