GeezerVison is showing github.com — fix(combos): a spent pool account must not cool the whole target (car… · lidge-jun/opencodex@99878c3
File treeExpand file treeCollapse file tree Open diff view settings docs-site/src/content/docs/guides
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs-site/src/content/docs/guides/combos.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -354,6 +354,10 @@ five seconds. A valid immediate | 354 | 354 | `Retry-After: 0` remains an immediate upstream directive rather than being replaced by a configured | 355 | 355 | cooldown. | 356 | 356 |
| | 357 | +For an Anthropic OAuth or Codex pool, a 429 tied to one account that the pool has cooled does | | 358 | +not cool the whole combo target. Other accounts behind that target remain available. A 429 with | | 359 | +no identified, cooled pool account still cools the target, as do provider-wide failures. | | 360 | + | 357 | 361 | ### Last-resort targets | 358 | 362 |
| 359 | 363 | A brief cooldown on a preferred target otherwise routes straight to whatever | |
Collapse file Copy file name to clipboardExpand all lines: scripts/test-layout/layout.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 100 | 100 | "ambiguous-resend-composition.test.ts": "lib", | 101 | 101 | "ambiguous-resend-gate.test.ts": "lib", | 102 | 102 | "anthropic-account-pool.test.ts": "adapters/anthropic", | | 103 | + "anthropic-combo-account-cooldown.test.ts": "adapters/anthropic", | 103 | 104 | "anthropic-model-routes.test.ts": "adapters/anthropic", | 104 | 105 | "anthropic-agentrouter-language-framing.test.ts": "adapters/anthropic", | 105 | 106 | "anthropic-baseurl-override.test.ts": "adapters/anthropic", | |
Collapse file Copy file name to clipboardExpand all lines: src/combos/resolve.ts +16-1Lines changed: 16 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -380,13 +380,28 @@ export function advanceComboAfterFailure( | 380 | 380 | status?: number; | 381 | 381 | code?: string | null; | 382 | 382 | message?: string; | | 383 | + /** Account-qualified provider label of the account this failure came from, when a pool resolved one. */ | | 384 | + failedAccount?: string; | 383 | 385 | } = {}, | 384 | 386 | ): ComboPick | null { | 385 | 387 | noteComboFailure(pick.comboId, pick.target, pick.writerGeneration); | 386 | 388 | const combo = getCombo(config, pick.comboId); | | 389 | + // A 429 from a pooled provider names the ACCOUNT that is spent, not the target. The pool | | 390 | + // records that account's own cooldown and rotates past it, so cooling the target here blacks | | 391 | + // out the accounts that still answer: on 2026-09-28 one Anthropic account replied | | 392 | + // `Retry-After: 318747` (88.5h) and anthropic/claude-opus-5-5 left selection for four hours | | 393 | + // while four of five accounts still returned 200. | | 394 | + // | | 395 | + // Only a pool-resolved account qualifies, and an unidentified account still cools the target -- | | 396 | + // including the pool's OWN "all accounts are cooled" 429, which refuses locally before it picks | | 397 | + // one. That is what keeps this from trading a blackhole for a hammer: when nothing else is | | 398 | + // holding the target back, the target cooldown still does. | | 399 | + const accountScoped = options.cooldownScope === "target" | | 400 | + && options.status === 429 | | 401 | + && options.failedAccount !== undefined; | 387 | 402 | // "none" records no cooldown at all: the failure described the request, not the target, so | 388 | 403 | // the target must stay immediately selectable for the next (differently shaped) request. | 389 | | - if (options.cooldownScope !== "none") { | | 404 | + if (options.cooldownScope !== "none" && !accountScoped) { | 390 | 405 | const cooldownTargets = options.cooldownScope === "provider" && combo | 391 | 406 | ? combo.targets.filter(target => target.provider === pick.target.provider) | 392 | 407 | : [pick.target]; | |
Collapse file Copy file name to clipboardExpand all lines: src/oauth/anthropic-routing.ts +31-19Lines changed: 31 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -736,13 +736,41 @@ export function rotateAnthropicAccountOn429( | 736 | 736 | rateLimitHeaders?: AnthropicRateLimitHeaders | null, | 737 | 737 | decision: AnthropicRouteDecision | null = null, | 738 | 738 | ): string | null { | | 739 | + if (!recordAnthropicAccount429(config, failedAccountId, retryAfterHeader, now, rateLimitHeaders)) return null; | | 740 | + | | 741 | + // The pool's strategy is a PROACTIVE policy. When the pool is disabled, reactive | | 742 | + // presence-only recovery must not silently reactivate round-robin/fill-first merely | | 743 | + // because those dormant values remain in config. The quota picker is the neutral | | 744 | + // recovery policy already used by the default strategy. | | 745 | + const next = isAnthropicAccountPoolEnabled(config) | | 746 | + ? pickAlternateAnthropicAccount(config, failedAccountId, now, decision) | | 747 | + : pickLowestUsage(config, failedAccountId, now); | | 748 | + if (!next) { | | 749 | + console.warn(`[anthropic-pool] ${decision ? `route:#${decision.position} ` : ""}no eligible replacement; returning 429`); | | 750 | + return null; | | 751 | + } | | 752 | + | | 753 | + console.warn( | | 754 | + `[anthropic-pool] ${decision ? `route:#${decision.position} ` : ""}429 on ${formatAnthropicAccountOrdinal(failedAccountId)}; failing over to ${formatAnthropicAccountOrdinal(next)}`, | | 755 | + ); | | 756 | + return next; | | 757 | +} | | 758 | + | | 759 | +/** Record the account refusal even when this request has no remaining retry sends. */ | | 760 | +export function recordAnthropicAccount429( | | 761 | + config: OcxConfig, | | 762 | + failedAccountId: string, | | 763 | + retryAfterHeader: string | null | undefined, | | 764 | + now = Date.now(), | | 765 | + rateLimitHeaders?: AnthropicRateLimitHeaders | null, | | 766 | +): boolean { | 739 | 767 | // Reactive 429 failover is NOT gated on the pool flag. That flag buys PROACTIVE routing -- | 740 | 768 | // session affinity, quota-ranked new-session selection, autoSwitchThreshold, strategy -- all | 741 | 769 | // of which move a HEALTHY request and stay opt-in. Rotating away from an account upstream has | 742 | 770 | // just rate-limited is a different thing: it only ever runs after a refusal, and stranding a | 743 | 771 | // 429 while a second logged-in account sits idle is a defect, not a configuration choice. | 744 | 772 | // Presence is the activation rule, the same one an apiKeyPool of two keys already uses. | 745 | | - if (!isAnthropicAccountPoolEnabled(config) && !hasAnthropicFailoverQuorum(now)) return null; | | 773 | + if (!isAnthropicAccountPoolEnabled(config) && !hasAnthropicFailoverQuorum(now)) return false; | 746 | 774 |
| 747 | 775 | // Retry-After first: it is the header written FOR this decision. The rejected window's | 748 | 776 | // reset is the fallback, because a 429 that omits Retry-After still carries it -- and | @@ -760,26 +788,10 @@ export function rotateAnthropicAccountOn429( | 760 | 788 | sweepExpiredOnWrite(now); | 761 | 789 | clearAnthropicSessionAffinityForAccount(failedAccountId); | 762 | 790 | notePoolRotationFailure(POOL_KEY_ANTHROPIC, failedAccountId); | 763 | | - // A rotation means the roster in use just changed; do not answer the next activation question | 764 | | - // from a count read taken before the failure. | | 791 | + // The refused account changed the eligible roster even when no retry send remains. | 765 | 792 | quorumCache = null; | 766 | 793 |
| 767 | | - // The pool's strategy is a PROACTIVE policy. When the pool is disabled, reactive | 768 | | - // presence-only recovery must not silently reactivate round-robin/fill-first merely | 769 | | - // because those dormant values remain in config. The quota picker is the neutral | 770 | | - // recovery policy already used by the default strategy. | 771 | | - const next = isAnthropicAccountPoolEnabled(config) | 772 | | - ? pickAlternateAnthropicAccount(config, failedAccountId, now, decision) | 773 | | - : pickLowestUsage(config, failedAccountId, now); | 774 | | - if (!next) { | 775 | | - console.warn(`[anthropic-pool] ${decision ? `route:#${decision.position} ` : ""}no eligible replacement; returning 429`); | 776 | | - return null; | 777 | | - } | 778 | | - | 779 | | - console.warn( | 780 | | - `[anthropic-pool] ${decision ? `route:#${decision.position} ` : ""}429 on ${formatAnthropicAccountOrdinal(failedAccountId)}; failing over to ${formatAnthropicAccountOrdinal(next)}`, | 781 | | - ); | 782 | | - return next; | | 794 | + return true; | 783 | 795 | } | 784 | 796 |
| 785 | 797 | export interface AnthropicSelectionRoutingOptions { | |
Collapse file Copy file name to clipboardExpand all lines: src/providers/label.ts +20-1Lines changed: 20 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { CODEX_ACCOUNT_LOG_LABEL_RE, KEY_ACCOUNT_LOG_LABEL_RE, apiKeyAccountLogLabel, oauthAccountLogLabel } from "../codex/account-label"; | | 1 | +import { ACCOUNT_LOG_LABEL_RE, CODEX_ACCOUNT_LOG_LABEL_RE, KEY_ACCOUNT_LOG_LABEL_RE, apiKeyAccountLogLabel, oauthAccountLogLabel } from "../codex/account-label"; | 2 | 2 | import type { OcxProviderConfig } from "../types"; | 3 | 3 |
| 4 | 4 | export function canonicalUsageProviderLabel(provider: string): string { | @@ -25,6 +25,25 @@ export function stampApiKeyAccountLabel( | 25 | 25 | } | 26 | 26 | } | 27 | 27 |
| | 28 | +/** | | 29 | + * The account-qualified provider label a pool dispatch stamped for this request, or undefined. | | 30 | + * | | 31 | + * An account pool rewrites the logged provider to name the account that served the turn | | 32 | + * (`anthropic-p2e22d0`, `openai-main`); the configured provider name never does, so a label that | | 33 | + * differs from the configured one AND ends in an account label identifies exactly one account. | | 34 | + * | | 35 | + * Deliberately NOT `accountLogLabel`: that is stamped for a lone configured API key too, so it | | 36 | + * cannot tell a pooled account apart from a provider that has no pool to rotate within. | | 37 | + */ | | 38 | +export function poolAccountProviderLabel( | | 39 | + loggedProvider: string | undefined, | | 40 | + configuredProvider: string, | | 41 | +): string | undefined { | | 42 | + if (!loggedProvider?.startsWith(`${configuredProvider}-`)) return undefined; | | 43 | + return ACCOUNT_LOG_LABEL_RE.test(loggedProvider.slice(configuredProvider.length + 1)) | | 44 | + ? loggedProvider : undefined; | | 45 | +} | | 46 | + | 28 | 47 | export function baseProviderLabel(provider: string): string { | 29 | 48 | const canonical = canonicalUsageProviderLabel(provider); | 30 | 49 | if (canonical !== provider) return canonical; | |
Collapse file Copy file name to clipboardExpand all lines: src/server/responses/adapter-continuation.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ import { bindRouteReasoningReplayScope } from "./core-replay"; | 35 | 35 | import { | 36 | 36 | ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST, | 37 | 37 | rotateAnthropicAccountOn429, | | 38 | + recordAnthropicAccount429, | 38 | 39 | getAnthropicPoolAccessSnapshot, | 39 | 40 | formatAnthropicProviderForLog, | 40 | 41 | } from "../../oauth/anthropic-routing"; | @@ -425,6 +426,11 @@ export function createAdapterContinuations( | 425 | 426 | } | 426 | 427 | } | 427 | 428 | } | | 429 | + if (response.status === 429 && transportState.anthropicPoolAccountId | | 430 | + && transportState.anthropicPoolFailovers >= ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST) { | | 431 | + recordAnthropicAccount429(config, transportState.anthropicPoolAccountId, | | 432 | + response.headers.get("retry-after"), Date.now(), response.headers); | | 433 | + } | 428 | 434 | // Generic OAuth rotation for the continuation loop. The streaming loop grew this arm with | 429 | 435 | // #2568 and this one did not, so an xAI/Cursor/Kimi/Copilot/Antigravity/Nous continuation | 430 | 436 | // 429 stayed terminal even with failover fully active -- the same class of divergence the | |
Collapse file Copy file name to clipboardExpand all lines: src/server/responses/adapter-dispatch.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,6 +56,7 @@ import { bindRouteReasoningReplayScope } from "./core-replay"; | 56 | 56 | import { | 57 | 57 | ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST, | 58 | 58 | rotateAnthropicAccountOn429, | | 59 | + recordAnthropicAccount429, | 59 | 60 | getAnthropicPoolAccessSnapshot, | 60 | 61 | formatAnthropicProviderForLog, | 61 | 62 | } from "../../oauth/anthropic-routing"; | @@ -1000,6 +1001,11 @@ export async function prepareAdapterExchange( | 1000 | 1001 | break; | 1001 | 1002 | } | 1002 | 1003 | } | | 1004 | + if (upstreamResponse.status === 429 && transportState.anthropicPoolAccountId | | 1005 | + && transportState.anthropicPoolFailovers >= ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST) { | | 1006 | + recordAnthropicAccount429(config, transportState.anthropicPoolAccountId, | | 1007 | + upstreamResponse.headers.get("retry-after"), Date.now(), upstreamResponse.headers); | | 1008 | + } | 1003 | 1009 | // Generic OAuth account failover (#2568) rotates reactively after a refusal when | 1004 | 1010 | // two accounts are stored. Kiro additionally classifies bounded 400/403 refusals; | 1005 | 1011 | // all other providers retain the original 429 loop below. | |
Collapse file Copy file name to clipboardExpand all lines: src/server/responses/core-combo.ts +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,11 @@ import { | 46 | 46 | import { hasUnreadableEncryptedAgentTask } from "./encrypted-payload"; | 47 | 47 | import { routeConcreteModel, comboRouteDecisionTrace } from "../../router"; | 48 | 48 | import { memoryModelRouteReason } from "./memory-models"; | | 49 | +import { poolAccountProviderLabel } from "../../providers/label"; | | 50 | +import { getAccountSet } from "../../oauth/store"; | | 51 | +import { formatAnthropicProviderForLog, getAnthropicAccountHealthSnapshot } from "../../oauth/anthropic-routing"; | | 52 | +import { codexAccountLogLabel } from "../../codex/account-label"; | | 53 | +import { codexQuotaScopeForModel, getCodexQuotaHealthSnapshot } from "../../codex/routing"; | 49 | 54 | import { isCanonicalOpenAiForwardProvider } from "../../providers/openai-tiers"; | 50 | 55 | import type { AgentTaskRecoveryFailureReason } from "./agent-task-recovery"; | 51 | 56 | import { | @@ -97,6 +102,22 @@ import { clientWireOf } from "../inference/client-wire"; | 97 | 102 | */ | 98 | 103 | export const COMBO_TARGET_BASE_SENDS = CODEX_TEXT_GUARDED_BUDGET_POLICY.baseSendAllowance; | 99 | 104 |
| | 105 | +function cooledPoolAccountLabel(config: OcxConfig, providerName: string, modelId: string, label: string | undefined): string | undefined { | | 106 | + if (!label) return undefined; | | 107 | + if (providerName === "anthropic") { | | 108 | + const matches = getAccountSet("anthropic")?.accounts.filter(account => | | 109 | + formatAnthropicProviderForLog("anthropic", account.id) === label) ?? []; | | 110 | + return matches.length === 1 && getAnthropicAccountHealthSnapshot(matches[0]!.id) ? label : undefined; | | 111 | + } | | 112 | + const provider = config.providers[providerName]; | | 113 | + if (!provider || !isCanonicalOpenAiForwardProvider(provider)) return undefined; | | 114 | + const matches = (config.codexAccounts ?? []).filter(account => | | 115 | + `${providerName}-${codexAccountLogLabel(account)}` === label); | | 116 | + return matches.length === 1 | | 117 | + && getCodexQuotaHealthSnapshot(matches[0]!.id, codexQuotaScopeForModel(modelId)) | | 118 | + ? label : undefined; | | 119 | +} | | 120 | + | 100 | 121 |
| 101 | 122 | /** | 102 | 123 | * A combo's execution policy is DECLARED by the combo, not inherited from the single-target | @@ -977,6 +998,13 @@ export async function executeComboResponses( | 977 | 998 | status: failure.response.status, | 978 | 999 | code: failure.upstreamCode, | 979 | 1000 | message: failure.classificationText, | | 1001 | + // The dispatch rewrote this to name the pool account that actually served the turn. | | 1002 | + failedAccount: cooledPoolAccountLabel( | | 1003 | + config, | | 1004 | + pick.target.provider, | | 1005 | + pick.target.model, | | 1006 | + poolAccountProviderLabel(childLog.provider, pick.target.provider), | | 1007 | + ), | 980 | 1008 | onCooldownRecorded: target => { | 981 | 1009 | failedTargetCooldownRecorded ||= targetKey(target) === failedTargetKey; | 982 | 1010 | }, | |
Collapse file Copy file name to clipboardExpand all lines: structure/providers-and-adapters.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | For Anthropic OAuth, `src/oauth/anthropic-routing.ts` applies the first matching `anthropicAccountPool.routes` rule to every eligible pick. The declared account order is stable while its candidates remain eligible; active, manual, affinity, quota and strategy preferences only choose inside that set. A healthy session affinity outside a model route is ignored for that request and retained for later unrouted or differently routed models; the routed commit does not overwrite it. An explicit fallback widens an empty route to the ordinary pool, and fill-first then advances in ordinary pool order from the active account. A missing eligible route fails locally without that fallback. The rules are operator allowlists, not provider entitlement evidence. Request logs use `route:#<n>` for the 1-based rule position, not the operator name. | 4 | 4 |
| | 5 | +An Anthropic 429 records the served account's cooldown even when the request has used its allowed retry sends. That final account remains excluded on the next request; combo target cooling is skipped only after the matching account cooldown is present. | | 6 | + | 5 | 7 | GitHub Copilot `modelContextTiers` is selected per upstream model. The Chat and Responses | 6 | 8 | adapters set `contextTier` only when the canonical routed provider is `github-copilot` | 7 | 9 | and a tier is configured. Otherwise passthrough retains caller-supplied values. The server carries provider identity | |
Collapse file Copy file name to clipboardExpand all lines: structure/runtime.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -504,7 +504,7 @@ This is also why the classifier cannot duplicate visible output. Native byte str | 504 | 504 |
| 505 | 505 | Regression coverage: `tests/responses/responses-forward-prompt-envelope.test.ts`, `tests/routing/router-combo-failover-classification.test.ts`, `tests/routing/routing-policy-fallback.test.ts`, `tests/helpers/combo-context-overflow-cases.ts`, and `tests/server/server-combo-failover-e2e.test.ts`. | 506 | 506 |
| 507 | | -`src/combos/failover.ts` uses a 10-minute fallback for a spent account usage window (codes `usage_limit_exceeded`, `usage_limit_reached`, `1308`, or `usage limit reached` / `usage limit has been reached` / `token-plan <window> quota has been exhausted` prose, including HTTP 502) and for provider-scoped credential or billing failure codes such as `invalid_api_key` and `insufficient_quota`. This duration does not change failure classification or cooldown scope; upstream retry/reset signals and configured durations retain precedence. It caps explicit upstream `Retry-After` target cooldowns at 24 hours while reset-derived, configured, and fallback cooldowns remain capped at 10 minutes. | | 507 | +`src/combos/failover.ts` uses a 10-minute fallback for a spent account usage window (codes `usage_limit_exceeded`, `usage_limit_reached`, `1308`, or `usage limit reached` / `usage limit has been reached` / `token-plan <window> quota has been exhausted` prose, including HTTP 502) and for provider-scoped credential or billing failure codes such as `invalid_api_key` and `insufficient_quota`. This duration does not change failure classification or cooldown scope; upstream retry/reset signals and configured durations retain precedence. It caps explicit upstream `Retry-After` target cooldowns at 24 hours while reset-derived, configured, and fallback cooldowns remain capped at 10 minutes. `src/combos/resolve.ts` omits a target cooldown for a 429 only when the failed child identifies one account in an Anthropic OAuth or canonical Codex pool and that account has a live pool cooldown. `src/server/responses/core-combo.ts` checks the pool's health record before passing that attribution; unknown accounts, ambiguous labels, API-key and generic OAuth providers, and other failures keep their ordinary target cooldown. | 508 | 508 |
| 509 | 509 | ## Combo default effort precedence | 510 | 510 |
| |
|