Skip to content

feat(codex): account quota policy (5h/weekly) with automatic rotation - #366

Merged
diegosouzapw merged 2 commits into
diegosouzapw:mainfrom
rexname:feature/codex-account-limit-rotation
Mar 14, 2026
Merged

diegosouzapw merged 2 commits into
diegosouzapw:mainfrom
rexname:feature/codex-account-limit-rotation

Conversation

@rexname

@rexname rexname commented Mar 14, 2026 •

Copy link
Copy Markdown
Contributor

Why

Codex accounts can hit 5-hour and weekly quota windows during active sessions. When that happens, routing may repeatedly pick an account that is near exhaustion, causing avoidable failures and manual intervention.

This PR adds account-level limit policy controls and predictable rotation so traffic keeps flowing through eligible accounts.

What was implemented

  • Added account-level Codex quota policy toggles:
    • 5h (ON/OFF)
    • Weekly (ON/OFF)
  • Added quota-window status helper for session (5h) and weekly windows.
  • Updated credential selection to skip Codex accounts when enabled windows reach the configured threshold (>=90% used).
  • Added all-rate-limited response metadata when all eligible Codex accounts are blocked by policy.
  • Kept account auto-reuse behavior aligned with provider reset times (resetAt) so accounts become eligible again after reset.
  • Added partial-safe merge behavior for codexLimitPolicy in provider update API.
  • Added documentation updates:
    • Root README (English)
    • i18n README note (English)
    • Indonesian README with scenarios

Practical scenarios

  • 5h ON + Weekly ON: account is skipped when either window reaches threshold.
  • 5h OFF + Weekly ON: only weekly window can block the account.
  • 5h ON + Weekly OFF: only 5h window can block the account.
  • When resetAt passes: the account becomes eligible again automatically.

Files changed

  • src/domain/quotaCache.ts
  • src/sse/services/auth.ts
  • src/app/api/providers/[id]/route.ts
  • src/app/(dashboard)/dashboard/providers/[id]/page.tsx
  • README.md
  • docs/i18n/README.md
  • docs/i18n/id/README.md

Verification

  • npm run -s typecheck:core
  • Manual validation flow for provider-level toggle persistence and rotation behavior

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive system for managing Codex account quotas, specifically addressing 5-hour and weekly usage limits. The primary goal is to prevent service disruptions by automatically rotating traffic away from accounts nearing their quota exhaustion and re-enabling them after reset times, thereby improving reliability and reducing manual intervention for users.

Highlights

  • Codex Quota Policy Toggles: Implemented dashboard toggles for 5-hour and weekly quota policies on Codex accounts, allowing users to enable or disable enforcement for each window.
  • Intelligent Account Rotation: Enhanced credential selection logic to automatically skip Codex accounts when their enabled quota windows reach 90% usage, ensuring traffic is routed to eligible accounts.
  • Quota Window Status Helper: Added a utility function to determine the status of 5-hour and weekly quota windows, providing details like usage percentage, reset times, and whether a threshold has been reached.
  • All-Rate-Limited Response Metadata: Introduced new response metadata to indicate when all eligible Codex accounts are blocked by policy, including retryAfter information for better client handling.
  • Automatic Account Re-eligibility: Ensured that Codex accounts automatically become eligible again for routing once their resetAt time passes, removing the need for manual re-enabling.
  • Partial-Safe Policy Merge: Implemented a deep-merge strategy for codexLimitPolicy within the provider update API, allowing for safe and partial updates to policy settings.
  • Documentation Updates: Updated the root README, i18n README, and Indonesian README to clearly document the new Codex account limit management features and their practical scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • README.md
    • Added a new section 'Codex Account Limit Management (5h + Weekly)' detailing the new policy toggles, threshold, rotation, and reset behaviors, along with practical scenarios.
  • docs/i18n/README.md
    • Added a 'Recent note: Codex account limit policy' section summarizing the new features.
  • docs/i18n/id/README.md
    • Added an Indonesian translation of the 'Codex Account Limit Management (5h + Weekly)' section.
  • src/app/(dashboard)/dashboard/providers/[id]/page.tsx
    • Introduced handleToggleCodexLimit function to manage Codex 5h and weekly limit policies via API calls.
    • Passed isCodex, onToggleCodex5h, and onToggleCodexWeekly props to the ConnectionRow component.
    • Modified ConnectionRow to display and enable toggles for 5h and weekly Codex limit policies when the provider is Codex.
    • Added providerSpecificData to ConnectionRow.propTypes and new prop types for the Codex toggle functions.
  • src/app/api/providers/[id]/route.ts
    • Added normalizeCodexLimitPolicy function to handle merging and defaulting of Codex limit policy settings.
    • Implemented logic in the PUT handler to deep-merge codexLimitPolicy within providerSpecificData specifically for Codex providers.
  • src/domain/quotaCache.ts
    • Defined QuotaWindowStatus interface for detailed quota window information.
    • Added clampPercent helper function to ensure percentage values are within 0-100.
    • Implemented getQuotaWindowStatus to retrieve status for specific quota windows (e.g., 'session', 'weekly') including usage, reset time, and threshold status.
  • src/sse/services/auth.ts
    • Imported getQuotaWindowStatus.
    • Added helper functions toBooleanOrDefault, getCodexLimitPolicy, and parseFutureDateMs.
    • Modified getProviderCredentials to filter availableConnections based on Codex limit policies, checking 5h and weekly usage thresholds.
    • Introduced logic to log blocked accounts and return allRateLimited metadata with retryAfter information if all Codex accounts are blocked by policy.
    • Adjusted the quota-aware prioritization to use policyEligibleConnections.
Activity
  • No specific activity (comments, reviews, etc.) was provided in the context.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

Comment thread src/app/(dashboard)/dashboard/providers/[id]/page.tsx
@kilo-code-bot

kilo-code-bot Bot commented Mar 14, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: Issues Resolved | Recommendation: Merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 0

The previously flagged issue has been addressed in this diff:

  • Missing user feedback on API failure (line 377 in old file) - RESOLVED: The handleToggleCodexLimit function now properly handles errors with notify.error() calls in both the API failure case (line ~142 in diff) and catch block (line ~162 in diff).

Files Reviewed (7 files)

  • README.md - Documentation additions for Codex account limit management
  • docs/i18n/README.md - i18n documentation update
  • docs/i18n/id/README.md - Indonesian translation
  • src/app/(dashboard)/dashboard/providers/[id]/page.tsx - Dashboard UI with Codex policy toggles
  • src/app/api/providers/[id]/route.ts - API endpoint with deep-merge logic for codexLimitPolicy
  • src/domain/quotaCache.ts - New getQuotaWindowStatus function with proper handling of expired windows (P1 issue resolved)
  • src/sse/services/auth.ts - Account filtering logic with policy-aware selection

Positive observations:

  • The Codex quota policy implementation is well-structured with proper type safety
  • The 90% threshold constant (CODEX_QUOTA_THRESHOLD_PERCENT) is properly defined and used
  • Account rotation logic correctly handles both 5h and weekly window policies
  • The getQuotaWindowStatus function properly handles edge cases with the clampPercent helper and addresses the stale cache issue by setting reachedThreshold: false when the window has expired
  • Deep-merge strategy for codexLimitPolicy in the API is properly handled with default values correctly set to true for both use5h and useWeekly

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a quota management policy for Codex accounts, allowing for automatic rotation when usage thresholds for 5-hour or weekly windows are met. The changes include backend logic for tracking and enforcing these policies, as well as frontend components in the dashboard to manage them. The documentation has also been updated accordingly. My review focuses on improving code maintainability by reducing duplication, avoiding side effects in getter functions, and improving readability. I've identified a few areas for improvement, such as extracting duplicated logic into helper functions and replacing magic numbers with named constants.

Comment thread src/app/(dashboard)/dashboard/providers/[id]/page.tsx Outdated
Comment thread src/domain/quotaCache.ts Outdated
Comment thread src/sse/services/auth.ts Outdated
Comment thread src/sse/services/auth.ts Outdated

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

Copy link
Copy Markdown

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: 0967295a51

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/domain/quotaCache.ts Outdated
rexname added 2 commits March 15, 2026 01:33
- add quota window status helper for Codex session (5h) and weekly windows
- enforce policy-based account filtering when enabled windows reach threshold
- return all-rate-limited metadata when no Codex account is eligible
- add per-account dashboard toggles for 5h and weekly policy controls
- merge codexLimitPolicy safely on provider updates to preserve partial settings
- document purpose and usage scenarios in README (EN + ID + i18n note)
- add user-facing success/error notifications for Codex limit toggle API calls
- deduplicate Codex policy default normalization in providers page
- make getQuotaWindowStatus side-effect free (no cache mutation in getter)
- avoid stale threshold blocking after resetAt has passed
- extract named Codex quota threshold constant
- extract helper for earliest future reset date selection
@rexname
rexname force-pushed the feature/codex-account-limit-rotation branch from 0967295 to 1555883 Compare March 14, 2026 18:35
@rexname

rexname commented Mar 14, 2026

Copy link
Copy Markdown
Contributor Author

😺

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

Copy link
Copy Markdown

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: 1555883633

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/sse/services/auth.ts
Comment on lines +257 to +258
if (policy.use5h && sessionStatus?.reachedThreshold) {
reasons.push(`5h usage ${Math.round(sessionStatus.usedPercentage)}%`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle cold quota cache before applying Codex policy

This policy check treats a missing window status as eligible (sessionStatus/weeklyStatus stay null and no reason is added), so a fresh process with an empty quota cache will route traffic through accounts even when their 5h/weekly usage is already above the configured threshold. Because the cache is only populated after usage fetches/429-derived entries, the new default-ON policy can be silently bypassed after restart until another path seeds quota data.

Useful? React with 👍 / 👎.

Comment thread src/sse/services/auth.ts
Comment on lines +290 to +293
if (policyEligibleConnections.length === 0 && availableConnections.length > 0) {
const earliestResetAt = getEarliestFutureDate(
blockedByPolicy.map((entry) => entry.resetAt)
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include cooldown accounts when computing retryAfter

When all currently available Codex accounts are filtered by quota policy, retryAfter is computed only from blockedByPolicy, but accounts already excluded by rateLimitedUntil are ignored here even if they recover sooner. In mixed states (policy-blocked + temporarily rate-limited), this can return a much later fallback window (often 5m), and downstream code uses it to set Retry-After, causing clients to back off longer than necessary.

Useful? React with 👍 / 👎.

@diegosouzapw
diegosouzapw merged commit db8d97b into diegosouzapw:main Mar 14, 2026
diegosouzapw added a commit that referenced this pull request Mar 14, 2026
- PR #368: gpt-5.4 in Codex model registry (cx/gpt-5.4, codex/gpt-5.4)
- PR #367: Codex fast tier toggle (default-off, full stack, 48 tests)
- PR #366: Codex quota policy 5h/weekly with auto-rotation
- fix #356: analytics charts show provider display names not raw IDs
@rexname
rexname deleted the feature/codex-account-limit-rotation branch March 15, 2026 02:57
prakersh pushed a commit to prakersh/OmniRoute that referenced this pull request Mar 26, 2026
…t-limit-rotation

Thanks @rexname (Maulana Hasanudin)! 🎉 

Codex account quota policy (5h/weekly) with auto-rotation is now merged. Highlights:
- Per-account policy toggles (5h + weekly ON/OFF) in the Provider dashboard
- Accounts automatically skipped when enabled quota window reaches 90% threshold
- Auto re-eligibility when resetAt timestamp passes (no manual intervention needed)
- Side-effect free `getQuotaWindowStatus` getter design
- Safe partial merge of `codexLimitPolicy` on provider updates

Merged on top of main (v2.5.0) with no conflicts. Analytics label fix (diegosouzapw#356) included. Thanks for the excellent quality and the 2-commit cleanup round! 🙏
prakersh pushed a commit to prakersh/OmniRoute that referenced this pull request Mar 26, 2026
- PR diegosouzapw#368: gpt-5.4 in Codex model registry (cx/gpt-5.4, codex/gpt-5.4)
- PR diegosouzapw#367: Codex fast tier toggle (default-off, full stack, 48 tests)
- PR diegosouzapw#366: Codex quota policy 5h/weekly with auto-rotation
- fix diegosouzapw#356: analytics charts show provider display names not raw IDs
Poid-ZA pushed a commit to Poid-ZA/OmniRoute that referenced this pull request Aug 5, 2026
…t-limit-rotation

Thanks @rexname (Maulana Hasanudin)! 🎉 

Codex account quota policy (5h/weekly) with auto-rotation is now merged. Highlights:
- Per-account policy toggles (5h + weekly ON/OFF) in the Provider dashboard
- Accounts automatically skipped when enabled quota window reaches 90% threshold
- Auto re-eligibility when resetAt timestamp passes (no manual intervention needed)
- Side-effect free `getQuotaWindowStatus` getter design
- Safe partial merge of `codexLimitPolicy` on provider updates

Merged on top of main (v2.5.0) with no conflicts. Analytics label fix (diegosouzapw#356) included. Thanks for the excellent quality and the 2-commit cleanup round! 🙏
Poid-ZA pushed a commit to Poid-ZA/OmniRoute that referenced this pull request Aug 5, 2026
- PR diegosouzapw#368: gpt-5.4 in Codex model registry (cx/gpt-5.4, codex/gpt-5.4)
- PR diegosouzapw#367: Codex fast tier toggle (default-off, full stack, 48 tests)
- PR diegosouzapw#366: Codex quota policy 5h/weekly with auto-rotation
- fix diegosouzapw#356: analytics charts show provider display names not raw IDs
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…t-limit-rotation

Thanks @rexname (Maulana Hasanudin)! 🎉 

Codex account quota policy (5h/weekly) with auto-rotation is now merged. Highlights:
- Per-account policy toggles (5h + weekly ON/OFF) in the Provider dashboard
- Accounts automatically skipped when enabled quota window reaches 90% threshold
- Auto re-eligibility when resetAt timestamp passes (no manual intervention needed)
- Side-effect free `getQuotaWindowStatus` getter design
- Safe partial merge of `codexLimitPolicy` on provider updates

Merged on top of main (v2.5.0) with no conflicts. Analytics label fix (diegosouzapw#356) included. Thanks for the excellent quality and the 2-commit cleanup round! 🙏
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
- PR diegosouzapw#368: gpt-5.4 in Codex model registry (cx/gpt-5.4, codex/gpt-5.4)
- PR diegosouzapw#367: Codex fast tier toggle (default-off, full stack, 48 tests)
- PR diegosouzapw#366: Codex quota policy 5h/weekly with auto-rotation
- fix diegosouzapw#356: analytics charts show provider display names not raw IDs
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.

2 participants