fix: prefer fresh access tokens during account selection#326
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 12 minutes and 32 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
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 |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Summary
What Changed
lib/accounts.tsusing a five-minute freshness windowtest/accounts.test.tsandtest/accounts-load-from-disk.test.tsfor fresh-preference and all-stale fallback behaviorValidation
npm run lintnpm run typechecknpm testnpm test -- test/documentation.test.tsnpm run buildDocs and Governance Checklist
docs/getting-started.mdupdated (if onboarding flow changed)docs/features.mdupdated (if capability surface changed)docs/reference/*pages updated (if commands/settings/paths changed)docs/upgrade.mdupdated (if migration behavior changed)SECURITY.mdandCONTRIBUTING.mdreviewed for alignmentRisk and Rollback
66d2d63Additional Notes
npm test -- test/accounts.test.ts test/accounts-load-from-disk.test.tsnote: greptile review for oc-chatgpt-multi-auth. cite files like
lib/foo.ts:123. confirm regression tests + windows concurrency/token redaction coverage.Greptile Summary
this pr adds fresh-token-aware account selection across all four selection paths (
isAccountAvailableForFamily,getCurrentOrNextForFamily,getNextForFamily,getCurrentOrNextForFamilyHybrid) using a 5-minute freshness window, with a stale-account fallback when the whole pool is near expiry. the refactor also extractsisAccountSelectableForFamilyto deduplicate the enabled/rate-limit/cooldown checks.hasFreshAccessTokenandhasFreshAvailableAccountForFamilycorrectly gate fresh preference; the stale-fallback behavior (all accounts stale → allow all) is tested and workingaccountsWithMetricsmapping ingetCurrentOrNextForFamilyHybridno longer filtersenabled === falseaccounts — they're now included withisAvailable: false.selectHybridAccount's LRU fallback iterates the full array, so a disabled account can be returned when every other account is rate-limited/cooling down. the oldif (account.enabled === false) return null;guard must be restoredgetNextForFamilyfresh-preference case (previously flagged) is now covered inaccounts-load-from-disk.test.tsnpm testis unchecked in the validation checklist — full suite should be confirmed green before merge given the scope of the changeConfidence Score: 4/5
safe to merge after fixing the disabled-account LRU regression in the hybrid selector
one confirmed P1 regression: disabled accounts are no longer filtered from the accountsWithMetrics array, making them selectable by the LRU fallback path in selectHybridAccount when all enabled accounts are unavailable; the fix is a one-line restore of the existing guard
lib/accounts.ts lines 556-570 (accountsWithMetrics map in getCurrentOrNextForFamilyHybrid)
Important Files Changed
Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[selection request] --> B[hasFreshAvailableAccountForFamily\nscan all accounts] B -->|any fresh+available| C[requireFresh = true] B -->|none fresh or available| D[requireFresh = false] C --> E[isAccountSelectableForFamily\nenabled? not rate-limited? not cooling?\nand hasFreshAccessToken?] D --> F[isAccountSelectableForFamily\nenabled? not rate-limited? not cooling?] E -->|pass| G[return account] F -->|pass| G E -->|fail for all| H{selection path} F -->|fail for all| H H -->|getCurrentOrNextForFamily\ngetNextForFamily| I[return null] H -->|getCurrentOrNextForFamilyHybrid| J[selectHybridAccount LRU fallback] J -->|OLD: disabled accounts filtered out| K[safe LRU pick] J -->|NEW: disabled accounts included with isAvailable=false| L[⚠️ may return disabled account]Comments Outside Diff (2)
index.ts, line 1914-1926 (link)isAccountAvailableForFamilynow does a full O(N) pool scan (viahasFreshAvailableAccountForFamily) on every call. the caller at line 1914 is already a loop overstreamFallbackCandidateOrder, so each failover event now scans the entire account pool once per candidate. for a large pool this degrades unnecessarily.pre-compute
requireFreshonce before the loop — or expose a batch-availability helper — to restore O(N) total cost:this mirrors the pattern already used inside
getCurrentOrNextForFamilyand the other internal selectors.Prompt To Fix With AI
lib/accounts.ts, line 556-570 (link)the old code had
if (account.enabled === false) return null;in theaccountsWithMetricsmap, which filtered disabled accounts out entirely. the new code removes that guard and instead relies onisAccountSelectableForFamilyreturningfalse, but that only setsisAvailable: false— the account still stays in theaccountsWithMetricsarray.in
selectHybridAccountthe LRU fallback path (if (available.length === 0)) iterates the fullresolvedAccountsarray, not just the available slice. so when every non-disabled account is rate-limited or cooling down, a disabled account can be returned fromselectHybridAccount, andgetCurrentOrNextForFamilyHybridreturns it without anyenabledcheck.fix: restore the early-return null for disabled accounts so they're excluded from the array:
Prompt To Fix With AI
Prompt To Fix All With AI
Reviews (2): Last reviewed commit: "test: cover fresh family selection happy..." | Re-trigger Greptile