Skip to content

fix(codex): retain quota cooldown after account retry - #1353

Merged
Wibias merged 3 commits into
lidge-jun:devfrom
luvs01:agent/fix-combo-quota-cooldown
Aug 10, 2026
Merged

fix(codex): retain quota cooldown after account retry#1353
Wibias merged 3 commits into
lidge-jun:devfrom
luvs01:agent/fix-combo-quota-cooldown

Conversation

@luvs01

@luvs01 luvs01 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Retain the first Codex pool account's quota outcome when a successful alternate-account retry follows a reset-derived combo deferral.
  • Keep that first account eligible for a later combo model when the alternate retry also fails.
  • Preserve the existing fail-closed behavior for ordinary and explicit Retry-After cooldowns, which are still recorded before the alternate send.

The combo deferral was designed to let a later model target reuse the same account, but it also suppressed the first account's quota outcome after another account had successfully served the request. That could leave an exhausted account active or thread-affined and send the next request back to it.

Verification

  • Bun 1.3.14: focused account-retry/combo regressions — 3 passed, 0 failed.
  • bun run typecheck — passed.
  • bun run privacy:scan — passed.
  • git diff --check — passed.
  • Independent focused review found no actionable P0-P3 findings.
  • A full tests/server-auth.test.ts pass before the final focused refinement reported 71 passed and one transient existing timeout; that timeout passed alone on both this branch and clean dev.
  • The full repository suite was attempted once, but Bun 1.3.14 panicked after 1,208 seconds amid unrelated Windows identity/history/catalog failures, so no changed-path failure was observed.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed. (No user-facing command or configuration contract changed.)
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • All CI tests are green on my local testing.

  • I pushed my PR to the latest dev commit.

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

Summary by CodeRabbit

  • Bug Fixes

    • Improved retry handling when alternate account requests fail or encounter rate limits.
    • Preserved cooldown and quota information after unsuccessful retries.
    • Ensured successful retries correctly update service health and quota status.
    • Improved failover so requests can continue to later models after repeated rate limits.
    • Prevented transport failures from incorrectly recording quota outcomes.
  • Tests

    • Added coverage for retry cooldown persistence, quota resets, and multi-model failover scenarios.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: fec60c41-167e-456d-999e-7407f8bd601b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Alternate Codex account retries defer reset-derived quota outcomes until the alternate request returns a successful HTTP response. Transport failures return before host-health reset. Auth tests cover cooldown persistence and multi-account, multi-model failover.

Changes

Codex retry health

Layer / File(s) Summary
Retry outcome flow
src/server/responses/core.ts
The retry path defers reset-derived quota recording during alternate attempts. Transport failures return before host-health reset. Successful responses reset host health and commit deferred outcomes.
Failover health coverage
tests/server-auth.test.ts
The retry harness accepts combo configurations. Tests cover Retry-After persistence, reset-derived shared cooldowns, and later combo targets after failed retries.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant FirstAccount
  participant AlternateAccount
  participant QuotaHealth
  FirstAccount->>AlternateAccount: Send account retry
  AlternateAccount-->>FirstAccount: Return transport failure or HTTP response
  AlternateAccount->>QuotaHealth: Reset host health after success
  FirstAccount->>QuotaHealth: Commit deferred quota outcome after success
Loading

Possibly related PRs

Suggested reviewers: ingwannu, lidge-jun, wibias

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving Codex quota cooldowns after an account retry.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Aug 9, 2026
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

✅ READY

  • all PR quality gates passed; the review readiness checklist is complete.

Review readiness checklist

  • ✅ All CI tests are green on my local testing.
  • ✅ I pushed my PR to the latest dev commit.
  • ✅ I resolved all correct Codex and CodeRabbit findings.
  • ✅ My PR is ready for review.

4/4 boxes ticked.

This pull request has been marked Ready for Review.
The review-ready label marks this PR as ready; review automation runs independently. If no CodeRabbit review appears, comment @coderabbitai review to request one.
Maintainers notified: @lidge-jun @Ingwannu @Wibias

@luvs01

luvs01 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@src/server/responses/core.ts`:
- Around line 507-512: Update the alternate-response retry flow around
retryHostKey so a successful circuit-enabled request retains its
UpstreamHostAdmissionLease and passes that matching lease to
resetUpstreamHostHealth instead of null. If no lease exists, perform the
required host-admission flow before resetting health; preserve the existing
no-lease reset behavior when the circuit threshold is disabled.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f252f095-9c4a-4a67-94d9-75c7025c9e51

📥 Commits

Reviewing files that changed from the base of the PR and between 4f746d1 and 96cdf4d.

📒 Files selected for processing (2)
  • src/server/responses/core.ts
  • tests/server-auth.test.ts

Comment thread src/server/responses/core.ts

Wibias commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Please rebase this PR onto the latest dev (e8ce2b93dd3ef8074d96dee407704c5dc06e2699) before merge, then rerun the exact-head CI checks.

@luvs01
luvs01 force-pushed the agent/fix-combo-quota-cooldown branch from 96cdf4d to ef3977c Compare August 9, 2026 23:25
@Wibias
Wibias marked this pull request as ready for review August 9, 2026 23:47
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

@github-actions[bot] The readiness gate is complete. I will review the current PR changes.

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Rate Limit Exceeded

@github-actions[bot] have exceeded the limit for the number of chat messages per hour. Please wait 48 minutes and 23 seconds before sending another message.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Rate Limit Exceeded

@github-actions[bot] have exceeded the limit for the number of chat messages per hour. Please wait 47 minutes and 18 seconds before sending another message.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@tests/server-auth.test.ts`:
- Around line 2324-2345: Strengthen the assertions in the test using
getCodexUpstreamHealth so cooldownUntil is verified to be greater than the
current assertion time, while preserving the existing numeric and retry-after
source checks. Capture the current time immediately before asserting the health
entry to ensure the stored cooldown is still active.
🪄 Autofix

❌ Autofix failed (check again to retry)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 00e78aed-bf38-4d08-a315-acb8d977231f

📥 Commits

Reviewing files that changed from the base of the PR and between 96cdf4d and ef3977c.

📒 Files selected for processing (1)
  • tests/server-auth.test.ts

Comment thread tests/server-auth.test.ts
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

An unexpected error occurred while generating fixes: Not Found - https://docs.github.com/rest/git/refs#get-a-reference

@Wibias
Wibias marked this pull request as draft August 10, 2026 00:29
@luvs01
luvs01 force-pushed the agent/fix-combo-quota-cooldown branch from ef3977c to a274792 Compare August 10, 2026 00:34
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Rate Limit Exceeded

@github-actions[bot] have exceeded the limit for the number of chat messages per hour. Please wait 20 minutes and 38 seconds before sending another message.

@Wibias
Wibias marked this pull request as ready for review August 10, 2026 00:39
@github-actions
github-actions Bot marked this pull request as draft August 10, 2026 00:39
@Wibias
Wibias marked this pull request as ready for review August 10, 2026 00:40

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/server/responses/core.ts (1)

1729-1741: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Delete the same credential-bearing headers that the openai-responses adapter forwards.

openai-responses.ts forwards all FORWARD_HEADERS into the outgoing request at src/adapters/openai-responses.ts:1183-1185; this strip only removes authorization and chatgpt-account-id in src/server/responses/core.ts:1735-1736, leaving headers like openai-beta, originator, session_id, session-id, and the x-codex-* headers on the third-party route. Delete the same headers here, or derive the strip set from the adapter’s forwarded list.

🤖 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/server/responses/core.ts` around lines 1729 - 1741, The
stripClaudeMainAuth branch in the response handling flow must remove every
credential-bearing header that the openai-responses adapter forwards, not only
authorization and chatgpt-account-id. Update the selectedForwardHeaders cleanup
to delete or derive the complete FORWARD_HEADERS set, including openai-beta,
originator, session_id, session-id, and x-codex-* headers, while preserving the
existing route and provider sanitization.

Source: Path instructions

🤖 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.

Inline comments:
In `@tests/server-auth.test.ts`:
- Around line 2420-2427: Update the test around the request in the
preserved-first-account scenario to assert the cooldown state directly using the
imported getCodexQuotaHealthSnapshot helper. Verify the snapshot for pool-a
retains the expected cooldown/reset-derived state, while preserving the existing
response and dispatch assertions and cleanup.

---

Outside diff comments:
In `@src/server/responses/core.ts`:
- Around line 1729-1741: The stripClaudeMainAuth branch in the response handling
flow must remove every credential-bearing header that the openai-responses
adapter forwards, not only authorization and chatgpt-account-id. Update the
selectedForwardHeaders cleanup to delete or derive the complete FORWARD_HEADERS
set, including openai-beta, originator, session_id, session-id, and x-codex-*
headers, while preserving the existing route and provider sanitization.
🪄 Autofix

❌ Autofix failed (check again to retry)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: dda2b2d8-9831-45a8-92a7-3858ee303d88

📥 Commits

Reviewing files that changed from the base of the PR and between ef3977c and a274792.

📒 Files selected for processing (2)
  • src/server/responses/core.ts
  • tests/server-auth.test.ts

Comment thread tests/server-auth.test.ts
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

An unexpected error occurred while generating fixes: Not Found - https://docs.github.com/rest/git/refs#get-a-reference

@github-actions
github-actions Bot marked this pull request as draft August 10, 2026 00:49
@luvs01
luvs01 force-pushed the agent/fix-combo-quota-cooldown branch from a274792 to d9119fc Compare August 10, 2026 01:11
@Wibias
Wibias marked this pull request as ready for review August 10, 2026 01:17
@github-actions
github-actions Bot marked this pull request as draft August 10, 2026 01:21
@Wibias
Wibias marked this pull request as ready for review August 10, 2026 01:21
@github-actions
github-actions Bot marked this pull request as draft August 10, 2026 01:22
@github-actions
github-actions Bot marked this pull request as ready for review August 10, 2026 01:27
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

@github-actions[bot] The readiness gate is complete. I will review the current PR changes.

⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Wibias
Wibias merged commit 4ba439c into lidge-jun:dev Aug 10, 2026
38 of 47 checks passed

Wibias commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Thanks @luvs01! Really appreciate the focused fix, the regression coverage, and the careful follow-up on the review feedback. Merged. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working review-ready

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants