Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(openai-codex): stop the 401 retry resending under another account

Jack Rudenko75369ba1acca
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index b1a27d80..4742b148 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -117,7 +117,11 @@ import {
isContextOverflowError,
isRequestShapeError,
} from "./shared/request-shape.js";
-import { type LegAuthBinding, createLegIssuer } from "./shared/responses-leg-issuer.js";
+import {
+ type LegAuthBinding,
+ createLegIssuer,
+ isSameCredentialSilo,
+} from "./shared/responses-leg-issuer.js";
import { sniffResponsesStreamHead } from "./shared/stream-head-sniffer.js";
import { createAnthropicPassthroughStream } from "./shared/stream-parsers/anthropic-sse.js";
import { createDevinConnectStream } from "./shared/stream-parsers/devin-connect.js";
@@ -1895,6 +1899,23 @@ export class ComposedHandler implements ModelHandler {
// retry leg after it, sign with the artifact just minted.
const refreshed = await this.provider.forceRefreshAuth!();
if (refreshed) {
+ // The body was built for THIS binding's account (code mode renders
+ // its encrypted reasoning and keys its records by it): a refresh
+ // onto another arm or account must not resend it. Answered with
+ // the upstream's own 401, never a throw: inside the recovery
+ // ladder an unclassified throw escapes `handle()`.
+ if (
+ binding &&
+ !isSameCredentialSilo(
+ { auth: binding.auth, accountKey: accountKeyFromBinding(binding) },
+ { auth: refreshed.auth, accountKey: accountKeyFromBinding(refreshed) }
+ )
+ ) {
+ log(`[${this.provider.displayName}] sign-in changed during the request; not resent`);
+ return new Response("Codex sign-in changed during the request; it was not resent", {
+ status: response.status,
+ });
+ }
binding = refreshed;
endpoint = this.provider.getEndpoint(this.targetModel, binding);
}
diff --git a/packages/cli/src/handlers/shared/responses-leg-issuer.ts b/packages/cli/src/handlers/shared/responses-leg-issuer.ts
index af1ee989..9ebc1d97 100644
--- a/packages/cli/src/handlers/shared/responses-leg-issuer.ts
+++ b/packages/cli/src/handlers/shared/responses-leg-issuer.ts
@@ -86,11 +86,21 @@ export interface LegIssuerDeps<B extends LegAuthBinding = LegAuthBinding> {
/** Thrown inside the issuer when the credential silo changed; never escapes it. */
class BindingChanged extends Error {}
+/**
+ * The same credential silo: the same arm and the same account. One rule for
+ * every place that adopts a refreshed binding mid-request (each retry leg, and
+ * the primary 401 retry in composed-handler), since the body was built for the
+ * first one.
+ */
+export function isSameCredentialSilo(turn: LegAuthBinding, next: LegAuthBinding): boolean {
+ return (
+ (turn.auth?.arm ?? undefined) === (next.auth?.arm ?? undefined) &&
+ turn.accountKey === next.accountKey
+ );
+}
+
function assertSameSilo(turn: LegAuthBinding | undefined, next: LegAuthBinding): void {
- if (!turn) return;
- const armChanged = (turn.auth?.arm ?? undefined) !== (next.auth?.arm ?? undefined);
- const accountChanged = turn.accountKey !== next.accountKey;
- if (armChanged || accountChanged) throw new BindingChanged();
+ if (turn && !isSameCredentialSilo(turn, next)) throw new BindingChanged();
}
/**