feat(usage): invalidate the usage cache when the account token changes#460
Merged
sirmalloc merged 3 commits intoJun 17, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
fetchUsageDataresolves the token first and gates the file-cache read on a fingerprint match; a mismatch refetches immediately.Complements #434. Tests: account-switch (mismatch refetches) + same-account (match serves).
bun run lintclean;bun test1554/1554.