GeezerVison is showing github.com — fix(combos): a pool-held 429 must not park the combo target past the … · AgentRadarLabs/opencodex@d9a8539
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/server/responses/core-combo.ts +47-9Lines changed: 47 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,7 +48,12 @@ import { routeConcreteModel, comboRouteDecisionTrace } from "../../router"; | 48 | 48 | import { memoryModelRouteReason } from "./memory-models"; | 49 | 49 | import { poolAccountProviderLabel } from "../../providers/label"; | 50 | 50 | import { getAccountSet } from "../../oauth/store"; | 51 | | -import { formatAnthropicProviderForLog, getAnthropicAccountHealthSnapshot } from "../../oauth/anthropic-routing"; | | 51 | +import { | | 52 | + formatAnthropicProviderForLog, | | 53 | + getAnthropicAccountHealthSnapshot, | | 54 | + getAnthropicPoolRetryAfterSeconds, | | 55 | + getEligibleAnthropicAccounts, | | 56 | +} from "../../oauth/anthropic-routing"; | 52 | 57 | import { codexAccountLogLabel } from "../../codex/account-label"; | 53 | 58 | import { codexQuotaScopeForModel, getCodexQuotaHealthSnapshot } from "../../codex/routing"; | 54 | 59 | import { isCanonicalOpenAiForwardProvider } from "../../providers/openai-tiers"; | @@ -119,6 +124,33 @@ function cooledPoolAccountLabel(config: OcxConfig, providerName: string, modelId | 119 | 124 | } | 120 | 125 |
| 121 | 126 |
| | 127 | +/** | | 128 | + * True only for the Anthropic OAuth pool's OWN "all accounts cooled" 429. Its Retry-After is the | | 129 | + * earliest per-account cooldown restated (`getAnthropicPoolRetryAfterSeconds`), which the pool | | 130 | + * keeps enforcing locally, so it must not also park the combo target: an account added, resumed or | | 131 | + * cleared a minute later would otherwise sit ignored until the target expired, up to the 24h | | 132 | + * server-delay ceiling. | | 133 | + * | | 134 | + * Provenance, not just current state: the target must be the OAuth pool, no account may have been | | 135 | + * identified for the failure, and the pool must be holding the wait right now. An upstream 429 on | | 136 | + * a pool account always cools and names that account, and an API-key provider never qualifies, so | | 137 | + * a genuine upstream Retry-After still parks the target in full. | | 138 | + */ | | 139 | +export function isAnthropicPoolLocalRefusal( | | 140 | + config: OcxConfig, | | 141 | + providerName: string, | | 142 | + status: number, | | 143 | + failedAccount: string | undefined, | | 144 | + now = Date.now(), | | 145 | +): boolean { | | 146 | + return providerName === "anthropic" | | 147 | + && config.providers[providerName]?.authMode === "oauth" | | 148 | + && status === 429 | | 149 | + && failedAccount === undefined | | 150 | + && getEligibleAnthropicAccounts(now).length === 0 | | 151 | + && getAnthropicPoolRetryAfterSeconds(now) !== null; | | 152 | +} | | 153 | + | 122 | 154 | /** | 123 | 155 | * A combo's execution policy is DECLARED by the combo, not inherited from the single-target | 124 | 156 | * profile. | @@ -988,8 +1020,20 @@ export async function executeComboResponses( | 988 | 1020 | }); | 989 | 1021 | const failedTargetKey = targetKey(pick.target); | 990 | 1022 | let failedTargetCooldownRecorded = false; | | 1023 | + // The dispatch rewrote this to name the pool account that actually served the turn. | | 1024 | + const failedAccount = cooledPoolAccountLabel( | | 1025 | + config, | | 1026 | + pick.target.provider, | | 1027 | + pick.target.model, | | 1028 | + poolAccountProviderLabel(childLog.provider, pick.target.provider), | | 1029 | + ); | | 1030 | + // The target still cools on the pool's own refusal (the anti-hammer guard), but for the local | | 1031 | + // fallback rather than the pool's restated per-account Retry-After. | | 1032 | + const poolLocalRefusal = isAnthropicPoolLocalRefusal( | | 1033 | + config, pick.target.provider, failure.response.status, failedAccount, failureNow, | | 1034 | + ); | 991 | 1035 | const nextPick = advanceComboAfterFailure(config, pick, { | 992 | | - retryAfter: failure.retryAfter, | | 1036 | + retryAfter: poolLocalRefusal ? undefined : failure.retryAfter, | 993 | 1037 | resetAt: failure.resetAt, | 994 | 1038 | cooldownMs: combo.cooldownMs, | 995 | 1039 | now: failureNow, | @@ -998,13 +1042,7 @@ export async function executeComboResponses( | 998 | 1042 | status: failure.response.status, | 999 | 1043 | code: failure.upstreamCode, | 1000 | 1044 | 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 | | - ), | | 1045 | + failedAccount, | 1008 | 1046 | onCooldownRecorded: target => { | 1009 | 1047 | failedTargetCooldownRecorded ||= targetKey(target) === failedTargetKey; | 1010 | 1048 | }, | |
Collapse file Copy file name to clipboardExpand all lines: tests/routing/always-on-429-failover.test.ts +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,8 +14,11 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | 14 | 14 | import { mkdtempSync } from "node:fs"; | 15 | 15 | import { tmpdir } from "node:os"; | 16 | 16 | import { join } from "node:path"; | | 17 | +import { clearComboTargetCooldowns, coolComboTarget, isComboTargetInCooldown } from "../../src/combos/failover"; | | 18 | +import { isAnthropicPoolLocalRefusal } from "../../src/server/responses/core-combo"; | 17 | 19 | import { | 18 | 20 | clearAnthropicAccountPoolState, | | 21 | + getAnthropicPoolRetryAfterSeconds, | 19 | 22 | getEligibleAnthropicAccounts, | 20 | 23 | hasAnthropicFailoverQuorum, | 21 | 24 | isAnthropicAccountPoolEnabled, | @@ -136,6 +139,57 @@ describe("Anthropic reactive 429 failover without the pool flag", () => { | 136 | 139 | expect(rotateAnthropicAccountOn429(poolAbsent(), ids[0]!, null)).toBe(ids[1]); | 137 | 140 | expect(rotateAnthropicAccountOn429(poolAbsent(), ids[1]!, null)).toBeNull(); | 138 | 141 | }); | | 142 | + | | 143 | + // The combo layer keys its target cooldown off this. While the pool holds the wait, its 429 | | 144 | + // Retry-After is only its own earliest account cooldown restated, so the combo must not also | | 145 | + // park the target on it -- otherwise an account added a minute later sits ignored until the | | 146 | + // target cooldown expires, which for a weekly window is the 24h server-delay ceiling. | | 147 | + test("only the OAuth pool's own all-cooled 429 is a local refusal", async () => { | | 148 | + const ids = await seedAccounts(2); | | 149 | + const oauth = poolAbsent(); | | 150 | + const refusal = (config = oauth, account?: string) => | | 151 | + isAnthropicPoolLocalRefusal(config, "anthropic", 429, account); | | 152 | + expect(refusal()).toBe(false); | | 153 | + const weekly = String(4 * 86_400); | | 154 | + expect(rotateAnthropicAccountOn429(oauth, ids[0]!, weekly)).toBe(ids[1]); | | 155 | + // One account still eligible: an upstream 429 here is about THAT account, not the pool. | | 156 | + expect(refusal()).toBe(false); | | 157 | + expect(rotateAnthropicAccountOn429(oauth, ids[1]!, weekly)).toBeNull(); | | 158 | + expect(getAnthropicPoolRetryAfterSeconds()).toBeGreaterThan(86_400); | | 159 | + expect(refusal()).toBe(true); | | 160 | + // Provenance, not just pool state: an identified account or an API-key provider is upstream | | 161 | + // speaking, so its Retry-After must still park the target in full. | | 162 | + expect(refusal(oauth, "anthropic-p0000000")).toBe(false); | | 163 | + const apiKey = { ...oauth, providers: { anthropic: { ...oauth.providers.anthropic!, authMode: "key" } } } as OcxConfig; | | 164 | + expect(refusal(apiKey)).toBe(false); | | 165 | + expect(isAnthropicPoolLocalRefusal(oauth, "anthropic", 503, undefined)).toBe(false); | | 166 | + // A fresh account makes the pool usable again at once, which a parked target would ignore. | | 167 | + await saveCredential("anthropic", { | | 168 | + access: "access-new", refresh: "refresh-new", expires: Date.now() + 3_600_000, | | 169 | + | | 170 | + } as never); | | 171 | + expect(refusal()).toBe(false); | | 172 | + }); | | 173 | + | | 174 | + // The combo-layer half, mirroring core-combo's call site: the target still cools (the | | 175 | + // anti-hammer guard), but for the local fallback rather than the pool's multi-day Retry-After. | | 176 | + // A single account is deliberately never cooled by rotation (see the no-op test above), so the | | 177 | + // pool cannot hold the wait there and an upstream Retry-After keeps parking the target as before. | | 178 | + test("a pool-held 429 cools the combo target for minutes, not for the pool's Retry-After", async () => { | | 179 | + const ids = await seedAccounts(2); | | 180 | + const weekly = String(4 * 86_400); | | 181 | + rotateAnthropicAccountOn429(poolAbsent(), ids[0]!, weekly); | | 182 | + rotateAnthropicAccountOn429(poolAbsent(), ids[1]!, weekly); | | 183 | + const now = Date.now(); | | 184 | + const local = isAnthropicPoolLocalRefusal(poolAbsent(), "anthropic", 429, undefined, now); | | 185 | + expect(local).toBe(true); | | 186 | + const target = { provider: "anthropic", model: "claude-opus-5" }; | | 187 | + clearComboTargetCooldowns("pool-held"); | | 188 | + coolComboTarget("pool-held", target, { now, retryAfter: local ? undefined : weekly, status: 429 }); | | 189 | + expect(isComboTargetInCooldown("pool-held", target, now + 1)).toBe(true); | | 190 | + expect(isComboTargetInCooldown("pool-held", target, now + 10 * 60_000)).toBe(false); | | 191 | + clearComboTargetCooldowns("pool-held"); | | 192 | + }); | 139 | 193 | }); | 140 | 194 |
| 141 | 195 | describe("proactive Anthropic routing stays opt-in", () => { | |
|