Repository navigation
Fix stale Codex usage after reset - #21748
Conversation
📝 WalkthroughWalkthroughThe reset outcome now reaches Priority: ⬇️ Low Merge Risk: 🟡 Moderate · up to A reset can still display stale weekly usage for accounts whose session window appears before the weekly value refreshes. Correct the visibility predicate before merging. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Full details: Description checkExplanation The description covers the change, rationale, testing, visual impact, AI disclosure, and compatibility considerations. However, it does not provide the required linked issue and explicitly states that no issue was provided.
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 79c3acda-8d1e-4618-afa1-2a805b25235f
📒 Files selected for processing (3)
src/main/rate-limits/service-account-target-selection.test.tssrc/main/rate-limits/service/service-account-refresh.tssrc/main/rate-limits/service/service-fetch-targets.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.
| } finally { | ||
| this.finishFetchCycle(controller) | ||
| } | ||
| const resetVisible = fresh.status === 'ok' && (fresh.session?.usedPercent ?? 0) <= 0 |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Do not treat a missing session window as a visible reset.
fresh.session?.usedPercent ?? 0 converts a null session to zero. An ok response with no session window therefore stops the retry loop immediately.
Require a session window before declaring the reset visible. Otherwise, a temporarily missing session can return blank or stale usage instead of using the remaining retries.
Proposed fix
- const resetVisible = fresh.status === 'ok' && (fresh.session?.usedPercent ?? 0) <= 0
+ const resetVisible =
+ fresh.status === 'ok' && fresh.session !== null && fresh.session.usedPercent <= 0📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const resetVisible = fresh.status === 'ok' && (fresh.session?.usedPercent ?? 0) <= 0 | |
| const resetVisible = | |
| fresh.status === 'ok' && fresh.session !== null && fresh.session.usedPercent <= 0 |
There was a problem hiding this comment.
Important
The post-reset retry only inspects the 5-hour session window, so it never retries when weekly is the window the reset changed — and for weekly-only accounts it exits on the very first read. Details inline.
Reviewed changes
fetchCodexResetResultState now converges the Codex usage snapshot after a successful reset instead of trusting the first (possibly lagging) read.
- Bounded post-reset retry — after
outcome === 'reset', re-read Codex usage up to 3 extra times, 250 ms apart, stopping once the session window reports 0%. - Outcome-aware gating — non-
resetoutcomes still perform a single read; the target/provenance/generation guards around publishing are unchanged. - Regression test — asserts 100% → 0% converges and that
fetchCodexRateLimitsis called twice.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
| } finally { | ||
| this.finishFetchCycle(controller) | ||
| } | ||
| const resetVisible = fresh.status === 'ok' && (fresh.session?.usedPercent ?? 0) <= 0 |
There was a problem hiding this comment.
The retry stops as soon as session.usedPercent <= 0, so it never covers the weekly window. For a weekly-only account (session: null) the predicate is true on the first read and the loop breaks immediately, even when weekly is the window the reset changed — the same stale-usage symptom this PR fixes.
Technical details
# Post-reset retry predicate only observes the session window
## Affected sites
- `src/main/rate-limits/service/service-fetch-targets.ts:126` — `resetVisible` reads `fresh.session?.usedPercent ?? 0`, so a null or zero session satisfies it regardless of `weekly`.
- `src/main/rate-limits/service-account-target-selection.test.ts:149` — the new test only exercises the session path (100 → 0).
## Required outcome
- Decide which windows the reset is expected to change and wait for all of them (or the specific exhausted one) before stopping.
## Evidence
- Weekly-only is a supported shape: `codex-rate-limit-window-classification.ts:43` classifies a 10080-minute primary as `weekly` with `session: null`; `service-account-target-selection.test.ts:456` exercises it.
- Reset eligibility is credit-only, not window-specific: `src/shared/codex-reset-credit-scope.ts:59`, `codex-reset-credit-scope-validation.ts:47`.
- User-facing copy: `CodexSwitcherMenu.tsx:113` ("resets any eligible usage windows"); `docs/site/content/docs/mobile.mdx:76` ("refreshes eligible rate-limit windows").
- External: openai/codex issues #37396 and #45570 report a redeemed credit clearing the weekly window.
## Open questions for the human
- Does a reset always clear every exhausted window together, or only the one that triggered it? If `weekly` can lag independently the predicate needs to include it; if the windows always move together, `session` may be enough but a null session still short-circuits the loop.| if (attempt > 0) { | ||
| await waitForCodexResetRefresh() | ||
| } | ||
| const controller = this.beginFetchCycle() |
There was a problem hiding this comment.
stop() aborts in-flight fetches, but an aborted fetch resolves to status: 'error', which this loop treats as retryable. Since stop() already cleared activeFetchAbortControllers, the fresh controller each attempt creates is never aborted, so up to three real Codex fetches run after the service is torn down.
Technical details
# Retry loop outlives service teardown
## Affected sites
- `src/main/rate-limits/service/service-fetch-targets.ts:107` — a new `beginFetchCycle()` per attempt with no abort check.
- `src/main/rate-limits/service/service-configuration.ts:108` — `stop()` calls `abortActiveFetchCycle()`, which aborts and then clears the controller set.
- `src/main/rate-limits/codex-rate-limit-fetch-result.ts:3` — an aborted fetch returns `status: 'error'`, so `resetVisible` stays false and the loop retries.
## Required outcome
- A `stop()` during the retry sequence should end the sequence rather than spawn three more reads.
## Suggested approach
- Hold one controller for the whole sequence (register it via `beginFetchCycle`, finish it once), pass its `signal` to each `fetchCodexRateLimits` call, and wait with the existing `delayUnlessAborted` (`src/main/rate-limits/service/service-types.ts:140`) so an abort breaks the loop.There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 0558c325-1b80-4de0-bd42-a336ac3cd9aa
📒 Files selected for processing (2)
src/main/rate-limits/service-account-target-selection.test.tssrc/main/rate-limits/service/service-fetch-targets.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
| } | ||
| const sessionImproved = | ||
| fresh.session !== null && | ||
| (previous.session === null || fresh.session.usedPercent < previous.session.usedPercent) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Do not treat a newly present window as reset visibility.
If previous.weekly is 100% and previous.session is null, a response with session.usedPercent still at 100% and weekly.usedPercent still at 100% sets sessionImproved to true. The loop then stops before the weekly window refreshes.
Only count a window with a previous value when its usedPercent decreases. Add a test for this mixed-window response.
Proposed fix
const sessionImproved =
- fresh.session !== null &&
- (previous.session === null || fresh.session.usedPercent < previous.session.usedPercent)
+ fresh.session !== null &&
+ previous.session !== null &&
+ fresh.session.usedPercent < previous.session.usedPercent
const weeklyImproved =
- fresh.weekly !== null &&
- (previous.weekly === null || fresh.weekly.usedPercent < previous.weekly.usedPercent)
+ fresh.weekly !== null &&
+ previous.weekly !== null &&
+ fresh.weekly.usedPercent < previous.weekly.usedPercentAlso applies to: 39-39

ELI5
After a Codex reset succeeds, the status bar now keeps checking briefly until the refreshed quota is visible, instead of leaving the old 100% value on screen.
What Changed
Why
The reset mutation and quota read are separate backend operations, so the first read can briefly lag behind a successful reset. A short, bounded retry makes the status bar converge to the new data without adding a permanent polling cost.
Linked Issue
No issue was provided for this request.
Visual Proof
N/A — this is a data-refresh timing fix; the status bar layout and controls are unchanged.
Testing
pnpm test src/main/rate-limits/service-account-target-selection.test.tspnpm tc:nodeAI Disclosure
Codex (GPT-6)
Review
Agent skill upstream boundary
Notes
The retry is limited to successful reset outcomes and at most three additional reads with 250 ms spacing. No platform-specific, SSH, mobile, or wire-format behavior changed.
Checklist