Skip to content

Commit cbcd446

Browse files
authored
fix: start the Codex app-server with the provider of the gateway routing (#733)
## Summary Codex 0.166 rejects input for a thread whose provider differs from the one its app-server started with ([openai/codex#57840](openai/codex#57840)). codex-acp set the gateway provider only per thread, so the app-server kept `openai`, and the first prompt failed with `modelProviderMismatch`. ## Changes - The app-server starts with `-c model_provider=…` and `-c model_providers.<id>=…` for the current provider: the gateway, else `MODEL_PROVIDER`, else `model_provider` of `CODEX_CONFIG`. These apply to that process only; `config.toml` is not changed. - Header values go in environment variables through `env_http_headers`, so a gateway key is not in the process arguments. - A provider change from `providers/set`, `providers/disable` or `authenticate` restarts the app-server, now also when no session is open. Codex has no API to change a running app-server's provider, so it is set at process start, and a change needs a restart. ## Behavior changes - The first `providers/set` before any session restarts the app-server once. - On Codex 0.166, a session can't be resumed after switching to another provider id (e.g. `openai` → `custom-gateway`). That is Codex behavior, independent of this change.
1 parent 29195ec commit cbcd446

16 files changed

Lines changed: 278 additions & 29 deletions

‎readme-dev.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ The adapter restarts the Codex app-server when it dies (for example, killed for
144144
A `providers/set` with the routing (base URL, API type, headers) that the last `providers/set` applied does not
145145
restart the app-server: it waits for a provider update in flight and succeeds. After a failed provider update it
146146
restarts again.
147+
- The app-server starts with the provider of the routing as `-c model_provider=…` and `-c model_providers.<id>=…`
148+
overrides, for that process only; the user's `config.toml` is not changed. The provider is the gateway of
149+
`providers/set` or of the `gateway` auth method, else `MODEL_PROVIDER`, else `model_provider` of `CODEX_CONFIG`.
150+
Header values go in `CODEX_ACP_PROVIDER_HEADER_<n>` environment variables through `env_http_headers`, so that they
151+
are not in the process arguments. Codex 0.166 and later refuse input for a thread whose provider is not the one of
152+
its app-server, so a `providers/set`, `providers/disable` or `authenticate` that changes the provider restarts the
153+
app-server even when no session is open.
147154
- After `CODEX_ACP_APP_SERVER_CRASH_LIMIT` crashes in the crash window the adapter stops restarting it and says so,
148155
with the stderr of the last crash.
149156
A session that the app-server died opening twice in 30 minutes is not opened again for the rest of that time, so

‎src/CodexAcpClient.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import {
9292
narrowToListedModels,
9393
} from "./GatewayModelDiscovery";
9494
import {findSupportedEffort} from "./ModelConfigOption";
95+
import {type ProviderStartupConfig, configOverride, NO_PROVIDER_STARTUP_CONFIG} from "./CodexConfigOverride";
9596
import {disableUnsupportedSkills, withoutUnsupportedSkills} from "./UnsupportedSkills";
9697
export type {SessionMetadata, SessionMetadataWithThread} from "./SessionMetadata";
9798
/** A null turn marks unavailable outcome metadata, not a failed turn. */
@@ -1233,6 +1234,11 @@ export class CodexAcpClient {
12331234
return this.gatewayConfig?.modelProvider ?? this.modelProvider;
12341235
}
12351236

1237+
/** The provider routing that the app-server of this client must start with, see {@link providerStartupConfig}. */
1238+
providerStartupConfig(): ProviderStartupConfig {
1239+
return providerStartupConfig(this.modelProvider, this.config, this.gatewayConfig);
1240+
}
1241+
12361242
private async getResumeModelProvider(): Promise<string> {
12371243
// Prefer an explicit/gateway provider, then the provider persisted in Codex config.
12381244
// Keep OpenAI as the final fallback for ChatGPT-authenticated sessions without a configured provider.
@@ -1855,6 +1861,43 @@ function mergeGatewayConfig(config: JsonObject, gatewayConfig: GatewayConfig | n
18551861
}
18561862
}
18571863

1864+
/**
1865+
* The provider that threads get (the gateway, the launch option, or `model_provider` of the session config) as
1866+
* app-server overrides. Codex accepts input only for threads of the provider that its app-server started with, and the
1867+
* `-c` overrides set it for that process alone, without a write to the user's `config.toml`. Header values go in
1868+
* environment variables, through `env_http_headers`, so that a gateway key is not in the process arguments.
1869+
*/
1870+
export function providerStartupConfig(
1871+
modelProvider: string | null,
1872+
config: JsonObject,
1873+
gatewayConfig: GatewayConfig | null,
1874+
): ProviderStartupConfig {
1875+
const configuredProvider = typeof config["model_provider"] === "string" ? config["model_provider"] : null;
1876+
const effectiveProvider = gatewayConfig?.modelProvider ?? modelProvider ?? configuredProvider;
1877+
if (effectiveProvider === null) return NO_PROVIDER_STARTUP_CONFIG;
1878+
1879+
const args = configOverride("model_provider", effectiveProvider);
1880+
const providers = mergeGatewayConfig(config, gatewayConfig)["model_providers"];
1881+
const providerDefinition = isJsonObject(providers) ? providers[effectiveProvider] : undefined;
1882+
// Without a definition here, the provider is a built-in one or comes from the user's Codex config.
1883+
if (!isJsonObject(providerDefinition)) return {args, env: {}};
1884+
1885+
const {http_headers: headers, ...provider} = providerDefinition;
1886+
const env: Record<string, string> = {};
1887+
const envHeaders: JsonObject = isJsonObject(provider["env_http_headers"]) ? {...provider["env_http_headers"]} : {};
1888+
if (isJsonObject(headers)) {
1889+
for (const [name, value] of Object.entries(headers)) {
1890+
if (typeof value !== "string") continue;
1891+
const variable = `CODEX_ACP_PROVIDER_HEADER_${Object.keys(env).length}`;
1892+
env[variable] = value;
1893+
envHeaders[name] = variable;
1894+
}
1895+
}
1896+
if (Object.keys(envHeaders).length > 0) provider["env_http_headers"] = envHeaders;
1897+
1898+
return {args: [...args, ...configOverride(`model_providers.${effectiveProvider}`, provider)], env};
1899+
}
1900+
18581901
async function* noItems<T>(): AsyncGenerator<T[]> {}
18591902

18601903
async function* oneItemPage<T>(items: T[]): AsyncGenerator<T[]> {

‎src/CodexAcpServer.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {CodexElicitationHandler} from "./CodexElicitationHandler";
99
import {renderFileCitations} from "./CodexFileCitations";
1010
import {type CodexAuthRequest, getCodexAuthMethods, getCodexAuthMethodsV2, isCodexAuthRequest} from "./CodexAuthMethod";
1111
import {clientSupportsUrlElicitation} from "./ElicitationCapabilities";
12+
import type {ProviderStartupConfig} from "./CodexConfigOverride";
13+
import {isDeepStrictEqual} from "node:util";
1214
import {createMcpServerSignIn, type McpServerSignIn} from "./mcp/McpServerSignIn";
1315
import {getRequestedMcpServerNames, McpSessionStartup, parseMcpStartupAwaitTimeoutMs} from "./mcp/McpSessionStartup";
1416
import {
@@ -479,6 +481,8 @@ export interface CodexProcessState {
479481
codexPath: string | undefined;
480482
config: JsonObject | undefined;
481483
appServerStartupArgs: string[];
484+
/** The provider routing of the next app-server; see `CodexAcpClient.providerStartupConfig`. */
485+
providerStartupConfig?: ProviderStartupConfig;
482486
modelProvider: string | undefined;
483487
stderr: string;
484488
stderrProcess?: CodexConnection["process"];
@@ -2233,6 +2237,10 @@ export class CodexAcpServer {
22332237
logger.log("Authenticate request failed");
22342238
throw RequestError.authRequired({methodId: _params.methodId}, "Sign-in did not complete");
22352239
}
2240+
if (this.providerStartupConfigChanged()) {
2241+
// The `gateway` method sets the provider, and every other method clears it.
2242+
await withRequestCancellation(this.enqueueProviderUpdate(() => {}), signal);
2243+
}
22362244
await withRequestCancellation(this.refreshAuthState(this.getAuthProviderForAuthenticateRequest(_params)), signal);
22372245
logger.log("Authenticate request completed");
22382246
return { };
@@ -2337,7 +2345,7 @@ export class CodexAcpServer {
23372345
private async enqueueProviderUpdate(apply: (client: CodexAcpClient) => void): Promise<void> {
23382346
const previous = this.providerUpdate?.catch(() => undefined) ?? Promise.resolve();
23392347
const update = previous.then(async () => {
2340-
if (this.sessions.size === 0) {
2348+
if (this.sessions.size === 0 && !this.providerStartupConfigChanged()) {
23412349
return;
23422350
}
23432351

@@ -2485,12 +2493,23 @@ export class CodexAcpServer {
24852493
}
24862494

24872495
private async restartCodexClient(): Promise<CodexAcpClient> {
2488-
if (this.recovery === null) {
2496+
if (this.recovery === null || this.codexProcessState === null) {
24892497
throw new Error("Codex process state is unavailable");
24902498
}
2499+
this.codexProcessState.providerStartupConfig = this.codexAcpClient.providerStartupConfig();
24912500
return await this.recovery.beginReplacement();
24922501
}
24932502

2503+
/**
2504+
* Whether the app-server runs with other provider routing than the agent has now. Codex 0.166 refuses input for a
2505+
* thread whose provider is not the one that its app-server started with, so the app-server has to restart.
2506+
*/
2507+
private providerStartupConfigChanged(): boolean {
2508+
const supervisor = this.codexProcessState?.supervisor;
2509+
return supervisor !== undefined
2510+
&& !isDeepStrictEqual(supervisor.current.providerStartupConfig, this.codexAcpClient.providerStartupConfig());
2511+
}
2512+
24942513
/**
24952514
* Waits for the provider update in flight. When its app-server died, the request goes on: the failure belongs to
24962515
* the `providers/set` request, and the request restarts the app-server itself. Any other failure fails it, as before.

‎src/CodexConfigOverride.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type {JsonValue} from "./app-server/serde_json/JsonValue";
2+
3+
/** A Codex `-c key=value` override, the value written as TOML. */
4+
export function configOverride(key: string, value: JsonValue | undefined): string[] {
5+
return ["-c", `${key}=${toToml(value)}`];
6+
}
7+
8+
// JSON strings, numbers, booleans, and arrays are already valid TOML; only objects need `key = value` syntax.
9+
function toToml(value: JsonValue | undefined): string {
10+
if (Array.isArray(value)) return `[${value.map(toToml).join(", ")}]`;
11+
if (value !== null && typeof value === "object") {
12+
return `{ ${Object.entries(value).map(([key, item]) => `${JSON.stringify(key)} = ${toToml(item)}`).join(", ")} }`;
13+
}
14+
return JSON.stringify(value);
15+
}
16+
17+
/** The provider routing an app-server process starts with: `-c` overrides, and the environment variables they read. */
18+
export interface ProviderStartupConfig {
19+
args: string[];
20+
env: Record<string, string>;
21+
}
22+
23+
export const NO_PROVIDER_STARTUP_CONFIG: ProviderStartupConfig = {args: [], env: {}};

‎src/CodexHookConfig.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {JsonObject} from "./CodexAcpClient";
2-
import type {JsonValue} from "./app-server/serde_json/JsonValue";
2+
import {configOverride} from "./CodexConfigOverride";
33

44
export type PreparedCodexHookConfig = {
55
sessionConfig: JsonObject | undefined;
@@ -13,14 +13,5 @@ export type PreparedCodexHookConfig = {
1313
export function prepareCodexHookConfig(config?: JsonObject): PreparedCodexHookConfig {
1414
if (config?.["hooks"] === undefined) return {sessionConfig: config, appServerStartupArgs: ["app-server"]};
1515
const {hooks, ...sessionConfig} = config;
16-
return {sessionConfig, appServerStartupArgs: ["app-server", "-c", `hooks=${toToml(hooks)}`]};
17-
}
18-
19-
// JSON strings, numbers, booleans, and arrays are already valid TOML; only objects need `key = value` syntax.
20-
function toToml(value: JsonValue | undefined): string {
21-
if (Array.isArray(value)) return `[${value.map(toToml).join(", ")}]`;
22-
if (value !== null && typeof value === "object") {
23-
return `{ ${Object.entries(value).map(([key, item]) => `${JSON.stringify(key)} = ${toToml(item)}`).join(", ")} }`;
24-
}
25-
return JSON.stringify(value);
16+
return {sessionConfig, appServerStartupArgs: ["app-server", ...configOverride("hooks", hooks)]};
2617
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {describe, expect, it} from "vitest";
2+
import {providerStartupConfig, type JsonObject} from "../CodexAcpClient";
3+
4+
const definition = {
5+
name: "Corp",
6+
base_url: "https://llm.corp/v1",
7+
wire_api: "responses",
8+
http_headers: {Authorization: "Bearer k"},
9+
env_http_headers: {"X-Org": "CORP_ORG"},
10+
};
11+
12+
describe("providerStartupConfig", () => {
13+
it.each<[string, string | null, JsonObject]>([
14+
["no provider", null, {model: "gpt-5"}],
15+
["a launch option provider that the user's Codex config defines", "corp", {}],
16+
["a launch option provider defined in the session config", "corp", {model_provider: "other", model_providers: {corp: definition}}],
17+
["model_provider of the session config", null, {model_provider: "corp", model_providers: {corp: definition}}],
18+
])("starts the app-server with %s", async (name, modelProvider, config) => {
19+
const startupConfig = providerStartupConfig(modelProvider, config, null);
20+
21+
await expect(`${JSON.stringify(startupConfig, null, 2)}\n`)
22+
.toMatchFileSnapshot(`data/provider-startup-config/${name.replaceAll(" ", "-").replaceAll("'", "")}.json`);
23+
});
24+
});

‎src/__tests__/app-server-recovery/CodexAppServerSupervisor.test.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as os from "node:os";
33
import * as path from "node:path";
44
import {describe, expect, it, vi} from "vitest";
55
import {startCodexConnection} from "../../CodexJsonRpcConnection";
6-
import {CodexAppServerSupervisor} from "../../app-server-recovery/CodexAppServerSupervisor";
6+
import {CodexAppServerSupervisor, type SupervisedProcessState} from "../../app-server-recovery/CodexAppServerSupervisor";
77
import {describeExit} from "../../app-server-recovery/AppServerExit";
88
import {fakeAppServer, fakeSpawner} from "./fake-app-server";
99

@@ -158,16 +158,22 @@ process.stdout.write(out, () => process.exit(1));
158158
expect(supervisor.isAlive(1)).toBe(false);
159159
});
160160

161-
it("starts a new child with the startup arguments of the first one", async () => {
161+
it("starts a new child with the startup arguments of the first one and the provider startup config of the state", async () => {
162162
const initial = fakeAppServer();
163163
const startupArgs = ["app-server", "-c", "hooks={}"];
164-
const state = {connection: initial.connection, codexPath: "/bin/codex", appServerStartupArgs: startupArgs, stderr: ""};
164+
const state: SupervisedProcessState = {connection: initial.connection, codexPath: "/bin/codex", appServerStartupArgs: startupArgs, stderr: ""};
165165
const spawner = fakeSpawner();
166166
const supervisor = new CodexAppServerSupervisor(state, spawner.spawn, FAST);
167+
expect(supervisor.current.providerStartupConfig).toEqual({args: [], env: {}});
167168
initial.child.die(null, "SIGKILL");
168169
await supervisor.current.exited;
169-
supervisor.spawn();
170-
expect(spawner.spawn).toHaveBeenCalledWith("/bin/codex", startupArgs);
170+
const providerStartupConfig = {args: ["-c", "model_provider=\"custom-gateway\""], env: {CODEX_ACP_PROVIDER_HEADER_0: "k"}};
171+
state.providerStartupConfig = providerStartupConfig;
172+
173+
const child = supervisor.spawn();
174+
175+
expect(spawner.spawn).toHaveBeenCalledWith("/bin/codex", [...startupArgs, ...providerStartupConfig.args], providerStartupConfig.env);
176+
expect(child.providerStartupConfig).toEqual(providerStartupConfig);
171177
});
172178

173179
it("does not spawn after shutdown, and does not spawn over a running child", async () => {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"args": [
4+
"app-server",
5+
"-c",
6+
"model_provider=\"custom-gateway\"",
7+
"-c",
8+
"model_providers.custom-gateway={ \"name\" = \"User-provided gateway\", \"base_url\" = \"https://gateway.example/v1\", \"wire_api\" = \"responses\", \"env_http_headers\" = { \"X-Client-Feature-ID\" = \"CODEX_ACP_PROVIDER_HEADER_0\", \"X-Key\" = \"CODEX_ACP_PROVIDER_HEADER_1\" } }"
9+
],
10+
"env": {
11+
"CODEX_ACP_PROVIDER_HEADER_0": "codex",
12+
"CODEX_ACP_PROVIDER_HEADER_1": "secret"
13+
}
14+
},
15+
{
16+
"args": [
17+
"app-server"
18+
],
19+
"env": {}
20+
},
21+
{
22+
"args": [
23+
"app-server"
24+
],
25+
"env": {}
26+
}
27+
]

‎src/__tests__/app-server-recovery/recovery-fixture.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export interface RecoveryFixture {
5959
/** Every app-server, the initial one first. */
6060
servers: FakeAppServer[];
6161
current(): FakeAppServer;
62+
/** The arguments and the added environment of every app-server that the supervisor started. */
63+
launches: Array<{args: string[], env: Record<string, string>}>;
6264
acp: {notify: ReturnType<typeof vi.fn>, request: ReturnType<typeof vi.fn>};
6365
updates(): Array<{sessionId: string, update: Record<string, unknown>}>;
6466
/** Per-method answers of every app-server (also of later ones); return undefined to leave a request pending. */
@@ -80,6 +82,7 @@ export function createRecoveryFixture(options: {air?: boolean, env?: Record<stri
8082
return server;
8183
};
8284
const initial = makeServer();
85+
const launches: RecoveryFixture["launches"] = [];
8386
const state: CodexProcessState = {
8487
connection: initial.connection,
8588
codexPath: undefined,
@@ -90,7 +93,10 @@ export function createRecoveryFixture(options: {air?: boolean, env?: Record<stri
9093
};
9194
state.supervisor = new CodexAppServerSupervisor(
9295
state,
93-
() => makeServer().connection,
96+
(_codexPath, args, env) => {
97+
launches.push({args, env});
98+
return makeServer().connection;
99+
},
94100
{closeGraceMs: 5, drainMs: 1, terminateAfterMs: 5, abandonAfterMs: 5, wedgedAfterMs: 50},
95101
);
96102
const acpConnection = {
@@ -123,6 +129,7 @@ export function createRecoveryFixture(options: {air?: boolean, env?: Record<stri
123129
supervisor: state.supervisor,
124130
servers,
125131
current: () => servers.at(-1)!,
132+
launches,
126133
acp: acpConnection,
127134
updates: () => acpConnection.notify.mock.calls
128135
.filter(call => (call as unknown[])[0] === acp.methods.client.session.update)

0 commit comments

Comments
 (0)