Skip to content

feat(quota): auto-balance credential priority and weight by remaining quota - #500

Open
brownsoo wants to merge 4 commits into
router-for-me:devfrom
brownsoo:feat/quota-auto-balance
Open

brownsoo wants to merge 4 commits into
router-for-me:devfrom
brownsoo:feat/quota-auto-balance

Conversation

@brownsoo

Copy link
Copy Markdown

Summary

When managing multiple OAuth accounts for the same provider (e.g., Claude Pro/Max, Codex, Antigravity, Kimi), standard round-robin or fill-first routing strategies do not account for the actual remaining percentage across rolling quota windows (5-hour and 7-day limits). As a result, accounts that are nearly exhausted continue receiving equal traffic unless manually adjusted or until they hit hard HTTP 429 cooldowns.

This PR adds an opt-in Auto-balance weights toggle to the Quota Lookup toolbar that automatically synchronizes each OAuth credential's priority and weight fields whenever its quota is fetched or refreshed.

Changes

  • src/services/quotaAutoBalance.ts:
    • Computes recommended priority and weight from fetched QuotaState rows:
      • Healthy credentials ($\ge 15%$ bottleneck remaining): priority: 10, weight scaled proportionally ($70%$ bottleneck window $+ 30%$ average window).
      • Low quota warning ($5% \sim 15%$ remaining): demoted to priority: 5 so healthy accounts are preferred first.
      • Critical quota ($< 5%$ remaining): demoted to priority: 1, weight: 1 as a last-resort fallback.
    • Calls PATCH /credentials/fields only when computed priority or weight differs from the credential's current values.
  • src/services/quotaEnrichment.ts:
    • Hooks into commitFetchedQuota so any successful quota refresh (from Quota Lookup or Auth Files) triggers non-blocking routing sync when auto-balance is enabled.
  • src/pages/QuotaPage.tsx:
    • Adds an Auto-balance weights switch to OAuthPageToolbar and immediately applies cached quota states when toggled on.
  • Localization & Tests:
    • Adds translations for en, zh-CN, zh-TW, and ja in src/i18n/authFileList.ts.
    • Adds unit tests in tests/quotaAutoBalance.test.ts covering tier calculation and redundant-PATCH avoidance.

brownsoo and others added 4 commits October 22, 2026 16:48
- Anchor auto-balance priorities to the default (healthy 0, low -5,
  critical -10) so depleted credentials rank below untouched ones.
- Read current priority/weight from the stored credential instead of the
  list payload, which lacks weight, so unchanged values skip the PATCH.
- Remember manual priority/weight before the first change and restore
  them when auto-balance is turned off.
- Mark model- and feature-scoped quota rows and compute the bottleneck
  from account-wide windows, falling back to the healthiest model group.
- Stop mutating credential objects held in React state; publish routing
  changes so the quota and auth file pages update immutably.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Serialize sync and restore per credential and re-check the toggle so a
  late sync can no longer re-apply auto values after toggle-off.
- Treat the Claude 7-day OAuth apps window as account-wide.
- Forget saved originals for credentials that no longer exist instead of
  failing every restore.
- Remember the values auto-balance last applied and leave credentials
  edited by hand untouched on restore.
- Read only priority and weight from the credential download so unrelated
  metadata validation cannot block balancing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Listen for storage events so turning auto-balance off in one tab
  updates every other open tab and its switch.
- Re-read the stored toggle before syncing or restoring, so a tab that
  missed the event still stops applying auto values.
- Keep the in-memory value when storage is unavailable instead of
  treating it as off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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