Skip to content

Cache reset times in-memory to preserve pace/deficit/reserve display#427

Open
Whoaa512 wants to merge 1 commit intosteipete:mainfrom
Whoaa512:fix/cache-reset-times-for-pace
Open

Cache reset times in-memory to preserve pace/deficit/reserve display#427
Whoaa512 wants to merge 1 commit intosteipete:mainfrom
Whoaa512:fix/cache-reset-times-for-pace

Conversation

@Whoaa512
Copy link

@Whoaa512 Whoaa512 commented Feb 25, 2026

Problem

When Claude usage is fetched via a non-cookie auth path (e.g. OAuth), resetsAt can come back as nil. This causes the pace/deficit/reserve display to disappear until the next cookie-based fetch, which may prompt the user for keychain access.

Fix

Add a dedicated in-memory reset time cache (lastKnownResetTimes) on UsageStore, separate from the snapshots dict (which gets cleared on fetch failures and provider disable).

When a fresh fetch returns nil for resetsAt:

  • Backfill from the cached value if it hasn't expired (still in the future)
  • Also backfills windowMinutes and resetDescription when missing
  • Identity-aware: skips backfill when account email changes between cached and fresh snapshots
  • Applied in both the main refresh path and the token-account refresh path
  • Cache cleared on credential file changes and provider disable/re-enable

Changes

  • RateWindow.backfillingResetTime(from:) — backfill a single rate window
  • UsageSnapshot.backfillingResetTimes(from:) — backfill primary/secondary/tertiary with identity check
  • UsageStore.lastKnownResetTimes — dedicated cache dict that survives snapshot clears
  • UsageStore+Refresh.swift — apply backfill in main refresh path; clear cache on credential changes and provider disable
  • UsageStore+TokenAccounts.swift — apply backfill in token-account refresh path
  • 8 new tests covering backfill, expiry, account switch, same-account, and no-op cases

Testing

  • swift build
  • swift test — all 803+ tests pass ✅
  • New ResetTimeBackfillTests — 8/8 pass ✅

Related: #232, #84

@Whoaa512 Whoaa512 marked this pull request as draft February 25, 2026 04:48
@Whoaa512 Whoaa512 force-pushed the fix/cache-reset-times-for-pace branch from 7e2b719 to 6ff5903 Compare February 25, 2026 05:18
@Whoaa512 Whoaa512 marked this pull request as ready for review February 25, 2026 05:35
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6ff5903406

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

When a fetch returns nil resetsAt (e.g. non-cookie auth path), backfill
from a dedicated in-memory cache if the cached reset time hasn't expired.

- Identity-aware: skips backfill when account email changes
- Applied in both main refresh and token-account refresh paths
- Cache cleared on credential changes and provider disable
- 8 new tests covering backfill, expiry, account switch scenarios
@Whoaa512 Whoaa512 force-pushed the fix/cache-reset-times-for-pace branch from 6ff5903 to 55f4351 Compare February 25, 2026 08:58
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.

1 participant