Skip to content

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

refactor(openai-codex): bind fetches to the RequestAuth of their request

Jack Rudenkocf1423434893
diff --git a/packages/cli/src/code-mode/ids.ts b/packages/cli/src/code-mode/ids.ts
new file mode 100644
index 00000000..39987081
--- /dev/null
+++ b/packages/cli/src/code-mode/ids.ts
@@ -0,0 +1,42 @@
+/**
+ * Identity keys for `openai-codex` requests.
+ *
+ * `accountKey` names the ChatGPT account ONE request signs with. It is read
+ * from that request's own auth binding (the artifact `refreshAuth` returned),
+ * never from the `CodexOAuth` singleton or the transport's shared artifact,
+ * because the login can change in-process (`reloadCredentials`) between two
+ * requests, and a value read from shared state could name another request's
+ * account.
+ */
+
+import { createHash } from "node:crypto";
+import type { AuthBinding } from "../providers/transport/types.js";
+
+/** The header the Codex OAuth artifact carries when the login has an account id. */
+const ACCOUNT_ID_HEADER = "chatgpt-account-id";
+
+function sha256Hex(input: string): string {
+ return createHash("sha256").update(input).digest("hex");
+}
+
+/**
+ * `sha256("openai-codex\0" + chatgpt-account-id)` from the binding's headers.
+ *
+ * Defined only when the binding's arm is `"oauth"` and the account-id header
+ * is present (`codex-credential.ts` sets it only when the login has an account
+ * id). Undefined otherwise: an api-key request, a legacy transport with no
+ * binding, or a login without an account id.
+ */
+export function accountKeyFromBinding(binding: AuthBinding | undefined): string | undefined {
+ const auth = binding?.auth;
+ if (!auth || auth.arm !== "oauth") return undefined;
+ let accountId: string | undefined;
+ for (const [name, value] of Object.entries(auth.headers ?? {})) {
+ if (name.toLowerCase() === ACCOUNT_ID_HEADER && typeof value === "string" && value) {
+ accountId = value;
+ break;
+ }
+ }
+ if (!accountId) return undefined;
+ return sha256Hex(`openai-codex\0${accountId}`);
+}
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index 7156afdf..0cd95cd0 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -27,7 +27,7 @@ import {
type EffortLevel,
} from "../adapters/base-api-format.js";
import { MiniMaxModelDialect } from "../adapters/minimax-model-dialect.js";
-import type { ProviderTransport, StreamFormat } from "../providers/transport/types.js";
+import type { AuthBinding, ProviderTransport, StreamFormat } from "../providers/transport/types.js";
import type { ModelHandler } from "./types.js";
// Alias for readability within this file
type BaseModelAdapter = BaseAPIFormat;
@@ -37,6 +37,7 @@ import { resolveQuotaAdapter } from "../auth/quota/registry.js";
import { PLAN_POLL_INTERVAL_MS } from "../auth/quota/types.js";
import type { BehaviorEngine, BehaviorSession } from "../behavior/index.js";
import { getBehaviorEngine } from "../behavior/index.js";
+import { accountKeyFromBinding } from "../code-mode/ids.js";
import {
getLogLevel,
log,
@@ -136,6 +137,12 @@ import { captureUpstreamError } from "./shared/upstream-error-capture.js";
*/
const STREAM_RETRY_DELAYS_MS = [3_000, 15_000, 30_000];
+/**
+ * A retry leg's binding: the request's `AuthBinding` plus the account it names
+ * (`accountKeyFromBinding`), which the leg issuer compares between legs.
+ */
+type LegBinding = AuthBinding & LegAuthBinding;
+
/** What `handle()` builds for a Responses turn and `handleStream` hands the parser. */
interface ResponsesTurnWiring {
retry: ResponsesRetryOptions;
@@ -1285,9 +1292,18 @@ export class ComposedHandler implements ModelHandler {
}
// 5b. Refresh auth / health check (must happen before transformPayload, which may use auth state)
+ //
+ // `binding` is THIS request's auth: a transport that mints a per-request
+ // artifact (`openai-codex`) returns it, and every later auth-dependent call
+ // of this request (5c, the endpoint, every header fetch, the 401 retry, the
+ // retry legs) is handed it back, so a concurrent request's refresh on the
+ // same handler can never change what this one signs with. Undefined for a
+ // transport that returns nothing (every other one), which then behaves
+ // exactly as before.
+ let binding: AuthBinding | undefined;
if (this.provider.refreshAuth) {
try {
- await this.provider.refreshAuth();
+ binding = (await this.provider.refreshAuth()) || undefined;
// Quota is DELIBERATELY not fetched here.
//
// This used to await getQuotaRemaining with a 2s cap on every single
@@ -1334,7 +1350,9 @@ export class ComposedHandler implements ModelHandler {
}
);
if (outcome.kind === "respond") return outcome.response;
- // Recovered: fall through as if the first refresh had succeeded.
+ // Recovered: fall through as if the first refresh had succeeded,
+ // bound to the artifact the successful attempt minted.
+ binding = outcome.value || undefined;
} else {
log(`[${this.provider.displayName}] Auth/health check failed: ${err.message}`);
logStderr(
@@ -1396,16 +1414,14 @@ export class ComposedHandler implements ModelHandler {
// 5c. Provider payload transformation (e.g., CodeAssist envelope wrapping)
if (this.provider.transformPayload) {
- requestPayload = this.provider.transformPayload(requestPayload, claudeRequest);
+ requestPayload = this.provider.transformPayload(requestPayload, claudeRequest, binding);
}
// (Middleware beforeRequest ran at step 3b, before buildPayload — see the
// note there for why it cannot run at this point.)
- const endpoint = this.provider.getEndpoint(this.targetModel);
- // Read beside the endpoint, from the same artifact: the arm that signs THIS
- // request. Decides the Responses stall guard's default scope below.
- const authArm = this.provider.currentAuthArm?.();
+ // `let`: the 401 branch replaces `binding` and recomputes the endpoint from it.
+ let endpoint = this.provider.getEndpoint(this.targetModel, binding);
// The ORIGINAL inbound body, not the normalized `claudeRequest` clone: a
// header carrying conversation identity must see what Claude Code sent.
//
@@ -1422,14 +1438,18 @@ export class ComposedHandler implements ModelHandler {
// The touch is refresh-conditional, which makes this rare, not safe.
// Every header fetch below goes through this, so a header the wire requires
// (a beta token next to the field it wrote) survives the auth retries too.
- const fetchHeaders = async (): Promise<Record<string, string>> => {
- const fetched = await this.provider.getHeaders(payload);
+ //
+ // Signed by the binding passed, never by shared transport state: the
+ // primary fetch and parameter retry pass this request's `binding`, the 401
+ // retry the one `forceRefreshAuth` just returned, a retry leg its own.
+ const fetchHeaders = async (b: AuthBinding | undefined): Promise<Record<string, string>> => {
+ const fetched = await this.provider.getHeaders(payload, b);
mergeWireHeaders(fetched, wireHeaders);
return fetched;
};
let headers: Record<string, string>;
try {
- headers = await fetchHeaders();
+ headers = await fetchHeaders(binding);
} catch (err: any) {
const conn = classifyConnectionError(err);
if (!conn) {
@@ -1441,7 +1461,7 @@ export class ComposedHandler implements ModelHandler {
err,
conn,
this.connectionEndpointFor(err, endpoint),
- fetchHeaders,
+ () => fetchHeaders(binding),
{
startTime,
// An auth site with the unclamped primary fetch still ahead of it —
@@ -1679,7 +1699,7 @@ export class ComposedHandler implements ModelHandler {
// UNCLASSIFIED throw is rethrown unchanged, so the existing 500 route
// out of here is untouched.
const doParamRetry = async (sig?: AbortSignal): Promise<Response> => {
- const retryHeaders = await fetchHeaders();
+ const retryHeaders = await fetchHeaders(binding);
retryHeaders["Content-Type"] = retrySerialized?.contentType ?? "application/json";
return fetch(
endpoint,
@@ -1744,8 +1764,14 @@ export class ComposedHandler implements ModelHandler {
// transiently, so a ladder that could only re-run one of them would
// leave the other exactly as exposed as it was.
const doAuthRetry = async (sig?: AbortSignal): Promise<Response> => {
- await this.provider.forceRefreshAuth!();
- const retryHeaders = await fetchHeaders();
+ // A returned binding replaces this request's: the retry, and every
+ // retry leg after it, sign with the artifact just minted.
+ const refreshed = await this.provider.forceRefreshAuth!();
+ if (refreshed) {
+ binding = refreshed;
+ endpoint = this.provider.getEndpoint(this.targetModel, binding);
+ }
+ const retryHeaders = await fetchHeaders(binding);
// Same serialization as the primary request — this is a separate call
// site and the easy one to forget, which is why both are pinned by
// the same assertion.
@@ -2231,31 +2257,28 @@ export class ComposedHandler implements ModelHandler {
let legRetries = 0;
let responsesTurn: ResponsesTurnWiring | undefined;
if (this.resolveStreamFormat() === "openai-responses-sse") {
- // No transport returns a binding yet: legs use the no-binding path, which
- // is exactly the primary fetch's.
- let legBinding: LegAuthBinding | undefined;
+ // Each leg is bound like the primary fetch: the issuer re-runs
+ // `refreshAuth`, refuses a binding whose arm or account differs from the
+ // turn's ("Codex sign-in changed during the turn"), and signs the leg
+ // with the binding it just checked. A transport that returns no binding
+ // keeps the no-binding path, which is exactly the primary fetch's.
+ const toLegBinding = (b: AuthBinding | void): LegBinding | undefined =>
+ b ? { auth: b.auth, accountKey: accountKeyFromBinding(b) } : undefined;
+ let legBinding = toLegBinding(binding);
const refreshAuth = this.provider.refreshAuth?.bind(this.provider);
const forceRefreshAuth = this.provider.forceRefreshAuth?.bind(this.provider);
- const issueLeg = createLegIssuer({
+ const issueLeg = createLegIssuer<LegBinding>({
providerDisplayName: this.provider.displayName,
getBinding: () => legBinding,
setBinding: (b) => {
legBinding = b;
},
- refreshAuth: refreshAuth
- ? async () => {
- await refreshAuth();
- return undefined;
- }
- : undefined,
+ refreshAuth: refreshAuth ? async () => toLegBinding(await refreshAuth()) : undefined,
forceRefreshAuth: forceRefreshAuth
- ? async () => {
- await forceRefreshAuth();
- return undefined;
- }
+ ? async () => toLegBinding(await forceRefreshAuth())
: undefined,
- getEndpoint: () => this.provider.getEndpoint(this.targetModel),
- fetchHeaders: () => fetchHeaders(),
+ getEndpoint: (b) => this.provider.getEndpoint(this.targetModel, b),
+ fetchHeaders: (b) => fetchHeaders(b),
// `requestPayload` as it stands now: parameter recovery may have
// dropped fields, and a leg re-sends the body that was accepted.
serializeBody: (body) => {
@@ -2283,7 +2306,7 @@ export class ComposedHandler implements ModelHandler {
stall: resolveStallPolicy({
env: process.env,
providerName: this.provider.name,
- arm: authArm,
+ arm: binding?.auth?.arm,
}),
firstHeadersAt: responseHeadersAt,
},
diff --git a/packages/cli/src/handlers/shared/responses-leg-issuer.ts b/packages/cli/src/handlers/shared/responses-leg-issuer.ts
index 7bd95794..af1ee989 100644
--- a/packages/cli/src/handlers/shared/responses-leg-issuer.ts
+++ b/packages/cli/src/handlers/shared/responses-leg-issuer.ts
@@ -27,6 +27,7 @@
*/
import { log } from "../../logger.js";
+import type { SerializedBody } from "../../providers/transport/types.js";
import { classifyConnectionError } from "./connection-error.js";
import { deadlineClamp, mergeSignalIntoInit } from "./transient-retry.js";
@@ -51,18 +52,18 @@ type StreamHeadSettle =
| { kind: "ok"; response: Response }
| { kind: "exhausted"; code: string; message: string; attempts: number; unreachable?: true };
-export interface LegIssuerDeps {
+export interface LegIssuerDeps<B extends LegAuthBinding = LegAuthBinding> {
providerDisplayName: string;
/** The turn's current binding (undefined on the no-binding path). */
- getBinding: () => LegAuthBinding | undefined;
- setBinding: (binding: LegAuthBinding) => void;
- refreshAuth?: () => Promise<LegAuthBinding | undefined>;
- forceRefreshAuth?: () => Promise<LegAuthBinding | undefined>;
- getEndpoint: (binding: LegAuthBinding | undefined) => string;
+ getBinding: () => B | undefined;
+ setBinding: (binding: B) => void;
+ refreshAuth?: () => Promise<B | undefined>;
+ forceRefreshAuth?: () => Promise<B | undefined>;
+ getEndpoint: (binding: B | undefined) => string;
/** The request's own header builder (wire headers merged). */
- fetchHeaders: (binding: LegAuthBinding | undefined) => Promise<Record<string, string>>;
+ fetchHeaders: (binding: B | undefined) => Promise<Record<string, string>>;
/** The serialized body for a leg: `undefined` → the primary request's bytes. */
- serializeBody: (body: unknown | undefined) => { body: BodyInit; contentType: string };
+ serializeBody: (body: unknown | undefined) => { body: SerializedBody; contentType: string };
getRequestInit?: () => Record<string, unknown>;
enqueueRequest?: (
fn: () => Promise<Response>,
@@ -96,14 +97,14 @@ function assertSameSilo(turn: LegAuthBinding | undefined, next: LegAuthBinding):
* Build `issueLeg(body, signal)` for one request. Never throws; every failure
* is a `LegResponse`.
*/
-export function createLegIssuer(
- deps: LegIssuerDeps
+export function createLegIssuer<B extends LegAuthBinding = LegAuthBinding>(
+ deps: LegIssuerDeps<B>
): (body: unknown | undefined, signal: AbortSignal) => Promise<LegResponse> {
const now = deps.now ?? Date.now;
const doFetch = deps.fetchImpl ?? fetch;
let legNumber = 1;
- const adoptBinding = (next: LegAuthBinding | undefined) => {
+ const adoptBinding = (next: B | undefined) => {
if (!next) return;
assertSameSilo(deps.getBinding(), next);
deps.setBinding(next);
diff --git a/packages/cli/src/providers/transport/openai-codex.ts b/packages/cli/src/providers/transport/openai-codex.ts
index 03874d5e..c113b859 100644
--- a/packages/cli/src/providers/transport/openai-codex.ts
+++ b/packages/cli/src/providers/transport/openai-codex.ts
@@ -25,16 +25,19 @@ import { normalizeCodexModel } from "../../adapters/codex-api-format.js";
import { lookupModelForProvider } from "../../adapters/model-catalog.js";
import { credentials } from "../../auth/credentials/authority.js";
import { recordSignedArm } from "../../auth/credentials/billing-probe.js";
-import type { CredentialArm, RequestAuth } from "../../auth/credentials/types.js";
+import type { RequestAuth } from "../../auth/credentials/types.js";
import { classifyConnectionError } from "../../handlers/shared/connection-error.js";
import { conversationKey } from "./conversation-key.js";
import { OpenAIProviderTransport } from "./openai.js";
+import type { AuthBinding } from "./types.js";
export class OpenAICodexTransport extends OpenAIProviderTransport {
/**
- * The per-request auth artifact, populated by refreshAuth() (called before
- * getEndpoint/getHeaders/transformPayload). Null when OAuth is unavailable —
- * the transport then falls back to the OpenAI base transport's api-key path.
+ * The last auth artifact refreshAuth() minted. Shared by every request on this
+ * transport, so the request path never reads it: ComposedHandler passes the
+ * request's own `AuthBinding` instead. Read only by callers that pass no
+ * binding (probes, tests). Null when OAuth is unavailable — the transport then
+ * falls back to the OpenAI base transport's api-key path.
*/
private cachedAuth: RequestAuth | null = null;
@@ -43,7 +46,7 @@ export class OpenAICodexTransport extends OpenAIProviderTransport {
* cache the artifact. composed-handler calls this before getEndpoint/getHeaders.
* The Codex credential ignores ctx.model, so "" is fine.
*/
- async refreshAuth(): Promise<void> {
+ async refreshAuth(): Promise<AuthBinding> {
try {
this.cachedAuth = await credentials.getRequestAuth("openai-codex", { model: "" });
} catch (err) {
@@ -83,27 +86,35 @@ export class OpenAICodexTransport extends OpenAIProviderTransport {
// spend is the one error that costs them (remote-provider-types.ts).
const signedWithOAuth = this.cachedAuth?.arm === "oauth";
recordSignedArm("openai-codex", signedWithOAuth ? "subscription" : "metered");
+ // The artifact just minted, bound to THIS request. ComposedHandler passes it
+ // back to transformPayload/getEndpoint/getHeaders, so a concurrent request's
+ // refresh (which replaces `cachedAuth`) cannot change what this one signs with.
+ return { auth: this.cachedAuth };
}
/**
- * The arm of the artifact `getEndpoint`/`getHeaders` read now: `"oauth"` for
- * the ChatGPT subscription, `"api-key"` (or unknown) otherwise. Read from the
- * artifact itself, never from its truthiness (see refreshAuth above).
+ * The artifact a call reads: the request's binding when one is passed (its
+ * `auth`, even when null), else `cachedAuth` for callers that pass none.
+ * Null ⇒ the OpenAI base transport's api-key path.
*/
- currentAuthArm(): CredentialArm | undefined {
- return this.cachedAuth?.arm;
+ private authFor(binding: AuthBinding | undefined): RequestAuth | null {
+ return binding ? binding.auth : this.cachedAuth;
}
/**
* OAuth tokens only work with chatgpt.com/backend-api (endpoint comes from the
- * cached auth artifact). API keys use the standard OpenAI endpoint (super).
+ * auth artifact). API keys use the standard OpenAI endpoint (super).
*/
- override getEndpoint(_targetModel?: string): string {
- return this.cachedAuth?.endpoint ?? super.getEndpoint();
+ override getEndpoint(_targetModel?: string, binding?: AuthBinding): string {
+ return this.authFor(binding)?.endpoint ?? super.getEndpoint();
}
- override async getHeaders(): Promise<Record<string, string>> {
- if (this.cachedAuth) return { ...this.cachedAuth.headers };
+ override async getHeaders(
+ _claudeRequest?: unknown,
+ binding?: AuthBinding
+ ): Promise<Record<string, string>> {
+ const auth = this.authFor(binding);
+ if (auth) return { ...auth.headers };
// Fall back to API key auth (Bearer <OPENAI_CODEX_API_KEY>).
return super.getHeaders();
}
@@ -116,7 +127,7 @@ export class OpenAICodexTransport extends OpenAIProviderTransport {
* The auth-derived bits (store:false / include reasoning) come from the cached
* auth artifact's transformPayload, applied only when OAuth is active.
*/
- transformPayload(payload: any, claudeRequest?: any): any {
+ transformPayload(payload: any, claudeRequest?: any, binding?: AuthBinding): any {
let normalizedPayload = payload;
if (payload?.model) {
const normalized = normalizeCodexModel(payload.model);
@@ -140,7 +151,7 @@ export class OpenAICodexTransport extends OpenAIProviderTransport {
}
}
// Auth-derived store:false / include reasoning bits, only under OAuth.
- return this.cachedAuth?.transformPayload?.(normalizedPayload) ?? normalizedPayload;
+ return this.authFor(binding)?.transformPayload?.(normalizedPayload) ?? normalizedPayload;
}
/**
diff --git a/packages/cli/src/providers/transport/types.ts b/packages/cli/src/providers/transport/types.ts
index 0d49cf95..a99d2a43 100644
--- a/packages/cli/src/providers/transport/types.ts
+++ b/packages/cli/src/providers/transport/types.ts
@@ -5,7 +5,23 @@
* Does NOT own: message conversion, tool format, pricing (those are ModelAdapter concerns).
*/
-import type { CredentialArm } from "../../auth/credentials/types.js";
+import type { RequestAuth } from "../../auth/credentials/types.js";
+
+/**
+ * The auth ONE request signs with, held in a request-local by ComposedHandler.
+ *
+ * A transport that mints a per-request artifact (`openai-codex`) returns it from
+ * `refreshAuth`/`forceRefreshAuth`, and ComposedHandler hands it back to
+ * `transformPayload`, `getEndpoint` and `getHeaders`. Every auth-dependent value
+ * of the request is then computed from this one artifact, never from a field on
+ * the transport instance, which concurrent requests on one handler share (one
+ * request's refresh could otherwise change what another request signs with).
+ *
+ * `auth === null` ⇒ the transport's base (api-key) path.
+ */
+export interface AuthBinding {
+ readonly auth: RequestAuth | null;
+}
/** The wire format used for streaming responses */
export type StreamFormat =
@@ -51,8 +67,14 @@ export interface ProviderTransport {
/** Which stream parser to use for this provider's responses */
readonly streamFormat: StreamFormat;
- /** Get the full API endpoint URL for a request */
- getEndpoint(model?: string): string;
+ /**
+ * Get the full API endpoint URL for a request.
+ *
+ * `binding` is the request's auth binding (from `refreshAuth`); a transport
+ * that returns one reads auth from it ONLY. Optional, so transports that bind
+ * nothing ignore it.
+ */
+ getEndpoint(model?: string, binding?: AuthBinding): string;
/**
* Get HTTP headers (may be async for OAuth token refresh).
@@ -65,8 +87,10 @@ export interface ProviderTransport {
* serves overlapping requests from different conversations (the same race as
* `classifyTerminalError` below). Optional, so existing implementers that
* ignore it are untouched.
+ *
+ * `binding`: the request's auth binding, as for `getEndpoint`.
*/
- getHeaders(claudeRequest?: unknown): Promise<Record<string, string>>;
+ getHeaders(claudeRequest?: unknown, binding?: AuthBinding): Promise<Record<string, string>>;
/**
* Override the adapter's stream format selection.
@@ -103,25 +127,22 @@ export interface ProviderTransport {
/**
* Optional auth refresh (e.g., OAuth token rotation).
* Called once before each request if defined.
+ *
+ * A transport that returns an `AuthBinding` binds the request to that
+ * artifact: ComposedHandler passes it to every later auth-dependent call of
+ * the same request. `void` ⇒ a legacy transport with nothing to bind.
*/
- refreshAuth?(): Promise<void>;
+ // biome-ignore lint/suspicious/noConfusingVoidType: legacy transports return Promise<void>, which `undefined` would reject
+ refreshAuth?(): Promise<AuthBinding | void>;
/**
* Force refresh auth credentials after a 401 response.
* Used by OAuth providers (Vertex, CodeAssist) to handle token expiry.
* ComposedHandler calls this automatically on 401 and retries the request.
+ * A returned binding REPLACES the request's binding for the retry.
*/
- forceRefreshAuth?(): Promise<void>;
-
- /**
- * The credential arm of the auth artifact `getEndpoint`/`getHeaders` read
- * right now (`RequestAuth.arm`). Read where the request's endpoint is
- * computed, so it names the arm that signed this request. Absent or
- * undefined ⇒ unknown, which every consumer treats as NOT the subscription.
- * Only `openai-codex` implements it; the Responses stall guard is armed by
- * default only on its `"oauth"` arm.
- */
- currentAuthArm?(): CredentialArm | undefined;
+ // biome-ignore lint/suspicious/noConfusingVoidType: legacy transports return Promise<void>, which `undefined` would reject
+ forceRefreshAuth?(): Promise<AuthBinding | void>;
/**
* Optional per-provider verdict on whether an error response is TERMINAL
@@ -161,8 +182,10 @@ export interface ProviderTransport {
* `metadata`, so conversation identity (`metadata.user_id`) is unreachable
* from `payload` alone. Optional and second, so the existing implementers
* that ignore it are untouched.
+ *
+ * `binding`: the request's auth binding, as for `getEndpoint`.
*/
- transformPayload?(payload: any, claudeRequest?: any): any;
+ transformPayload?(payload: any, claudeRequest?: any, binding?: AuthBinding): any;
/**
* Serialize the request payload to wire bytes.