fix: preserve Codex usage when token refresh fails - #386
Conversation
📝 WalkthroughWalkthroughCodex token refresh now uses JWT expiry windows, provides specific refresh-token errors, preserves upstream usage status, and conditionally requires re-authentication based on live usage results. Cache handling and tests were updated accordingly. ChangesCodex refresh and usage flow
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant UsageAggregator
participant TokenRefresh
participant WHAMUsage
participant DiskCache
UsageAggregator->>TokenRefresh: Check JWT-aware staleness
TokenRefresh-->>UsageAggregator: Refreshed tokens or refresh error
UsageAggregator->>WHAMUsage: Fetch usage with access token
WHAMUsage-->>UsageAggregator: Live usage and upstream status
UsageAggregator->>DiskCache: Write fresh data or read prior snapshot
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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.
🧹 Nitpick comments (1)
src/lib/codex-token-refresh.js (1)
58-85: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMissing test for the
refresh_token_invalidatedmessage branch.The
refresh_token_expiredandrefresh_token_reusedbranches now have dedicated tests, butrefresh_token_invalidated(line 78-80) has no equivalent coverage intest/codex-token-refresh.test.js, despite being explicitly called out in the PR objectives ("improves messages for expired, reused, or invalidated refresh tokens").✅ Suggested test addition
it("explains a refresh_token_invalidated 401 separately", async () => { await assert.rejects( refreshCodexTokens({ refreshToken: "rt-abc", fetchImpl: async () => ({ ok: false, status: 401, json: async () => ({ error: { code: "refresh_token_invalidated" } }), }), }), (err) => { assert.equal(err.code, "REFRESH_TOKEN_EXPIRED"); assert.equal(err.openaiErrorCode, "refresh_token_invalidated"); assert.match(err.message, /revoked/); return true; }, ); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/lib/codex-token-refresh.js` around lines 58 - 85, Add a dedicated test in the refreshCodexTokens test suite for a 401 response containing error code refresh_token_invalidated. Assert the rejected error preserves REFRESH_TOKEN_EXPIRED and openaiErrorCode, and that its message describes the token as revoked.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/lib/codex-token-refresh.js`:
- Around line 58-85: Add a dedicated test in the refreshCodexTokens test suite
for a 401 response containing error code refresh_token_invalidated. Assert the
rejected error preserves REFRESH_TOKEN_EXPIRED and openaiErrorCode, and that its
message describes the token as revoked.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 6109beb5-9a0d-40b2-af0b-a96d015e1dd8
📒 Files selected for processing (4)
src/lib/codex-token-refresh.jssrc/lib/usage-limits.jstest/codex-token-refresh.test.jstest/usage-limits.test.js
Summary
Testing
node --test test/codex-token-refresh.test.js test/usage-limits.test.js— 117 passednpm run validate:guardrailsSummary by CodeRabbit
Improvements
Bug Fixes