Skip to content

Commit 92dd06c

Browse files
committed
fix(codex): release probes on resolver errors
1 parent 3056415 commit 92dd06c

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎src/server/responses/core.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ async function retryCodexPoolOnAlternateAccount(
375375
&& !(error instanceof CodexAuthContextError)
376376
&& !(error instanceof CodexAccountCooldownError)
377377
&& !(error instanceof CodexMainProfileDrainingError)
378-
) throw error;
378+
) {
379+
releaseCodexAuthContextProbeLease(firstAuthCtx);
380+
throw error;
381+
}
379382
}
380383
if (retryAuthCtx?.kind !== "pool" && retryAuthCtx?.kind !== "main-pool") {
381384
return { kind: "no-alternate" };

‎tests/server-auth.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,69 @@ describe("server local API auth", () => {
26342634
}
26352635
});
26362636

2637+
test("unexpected alternate resolver failure releases A's recovery probe and preserves the error", async () => {
2638+
const harness = await startPoolRetryHarness(() => new Response("unused"), {
2639+
omitCredentialAccountIds: ["pool-b"],
2640+
});
2641+
const redirectedFetch = globalThis.fetch;
2642+
const resolverError = new Error("synthetic unexpected alternate resolver failure");
2643+
const now = Date.now();
2644+
recordCodexUpstreamOutcome(harness.config, "pool-a", 429, {
2645+
now: now - CODEX_QUOTA_PROBE_INTERVAL_MS,
2646+
resetAt: Math.floor((now + 60 * 60_000) / 1000),
2647+
});
2648+
let sends = 0;
2649+
let selectionCalls = 0;
2650+
let observedProbeLeaseId: string | undefined;
2651+
const turnAdmissionLease = {
2652+
release() {},
2653+
beginCodexAccountSelection() {
2654+
selectionCalls += 1;
2655+
if (selectionCalls === 2) throw resolverError;
2656+
return {
2657+
mainProfileDraining: false,
2658+
claimMainProfile: () => false,
2659+
release() {},
2660+
};
2661+
},
2662+
};
2663+
2664+
globalThis.fetch = (async (input, init) => {
2665+
const accountId = new Headers(init?.headers).get("chatgpt-account-id");
2666+
if (accountId !== "acct-pool-a") return redirectedFetch(input, init);
2667+
sends += 1;
2668+
observedProbeLeaseId = getCodexUpstreamHealth("pool-a")?.probeLeaseId;
2669+
saveCodexAccountCredential("pool-b", {
2670+
accessToken: "pool-b-token",
2671+
refreshToken: "pool-b-refresh",
2672+
expiresAt: Date.now() + 10 * 60_000,
2673+
chatgptAccountId: "acct-pool-b",
2674+
});
2675+
clearAccountNeedsReauth("pool-b");
2676+
return rejectionResponse(unsupportedModelBody());
2677+
}) as typeof fetch;
2678+
2679+
try {
2680+
const request = new Request("http://localhost/v1/responses", {
2681+
method: "POST",
2682+
headers: { "content-type": "application/json", authorization: "Bearer inbound-token" },
2683+
body: JSON.stringify({ model: POOL_RETRY_MODEL, input: "hello", stream: false }),
2684+
});
2685+
const logCtx: RequestLogContext = { model: POOL_RETRY_MODEL, provider: "openai" };
2686+
await expect(handleResponses(request, harness.config, logCtx, { turnAdmissionLease })).rejects.toBe(
2687+
resolverError,
2688+
);
2689+
expect(sends).toBe(1);
2690+
expect(selectionCalls).toBe(2);
2691+
expect(observedProbeLeaseId).toEqual(expect.any(String));
2692+
expect(getCodexUpstreamHealth("pool-a")?.cooldownUntil).toEqual(expect.any(Number));
2693+
expect(getCodexUpstreamHealth("pool-a")?.probeLeaseId).toBeUndefined();
2694+
} finally {
2695+
globalThis.fetch = redirectedFetch;
2696+
await stopPoolRetryHarness(harness);
2697+
}
2698+
});
2699+
26372700
test("retry-dispatch transport failure is host-only and never triple-dispatches", async () => {
26382701
const harness = await startPoolRetryHarness(() => rejectionResponse(unsupportedModelBody()));
26392702
const affinityThread = "retry-dispatch-host-only-affinity";

0 commit comments

Comments
 (0)