Skip to content

Commit 3b35338

Browse files
authored
fix: skip the app-server restart when providers/set repeats the applied config (#728)
## Summary Fixes [IJAI-5588](https://youtrack.jetbrains.com/issue/IJAI-5588). A `providers/set` that sent the same values as the routing already in effect still restarted the Codex app-server whenever a session was open. Codex writes a thread to disk only on its first model turn, so it cannot resume a session that has not had one: a new session, or one that only ran an adapter-handled command such as `/mcp`. After the restart, every prompt in such a session failed with "had no messages yet and was lost". AIR sends such a repeated `providers/set` after a policy refresh briefly reports no routes. - `setProvider` skips the restart when the new provider config equals the one the last `providers/set` applied. A repeated request still waits for a provider update in flight and only then responds, so a success response still means the routing is in effect. - After a failed provider update, the same request restarts again (new `providerUpdateFailed` flag). Without it, retrying a failed `providers/set` would report success with nothing applied. - `sameGatewayRouting` (base URL, wire API and headers) becomes `sameProviderConfig`: `modelProvider` plus the whole `config`, compared by value with `util.isDeepStrictEqual`. A field added to `config` later is compared without changes here. `prepareProvider` reuses the previous model listing through the same check (`hasProviderRouting`), so the listing reuse and the restart skip cannot drift apart. - `readme-dev.md` documents the behavior. A real routing change still loses sessions that have no model turn, as before. That is a Codex limitation and is already documented. `providers/disable` still always restarts; the real-Codex e2e test `hooks.test.ts` relies on that. ## Validation - `npm run typecheck`: passed - `npm test`: 2,419 passed, 39 skipped - `git diff --check`: passed - New tests in `src/__tests__/app-server-recovery/recovery.test.ts`: a session with no messages survives a repeated `providers/set` while a changed header still restarts; a repeated request during a running restart responds only after it. Both fail with the skip turned off. - The existing test "counts provider restarts whose handshake fails" covers the retry after a failed update. `CodexHomeWatcher.test.ts` ("reports a rollout that another process moved into archived_sessions…") failed in 2 of 4 full runs on this branch and passes when run alone. It does not touch providers.
1 parent e059ab7 commit 3b35338

4 files changed

Lines changed: 75 additions & 11 deletions

File tree

‎readme-dev.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ The adapter restarts the Codex app-server when it dies (for example, killed for
141141
- Codex writes a thread to disk only with its first message, so a session without messages cannot be resumed in a new
142142
app-server, after a crash or a provider restart. Its next use fails with "had no messages yet and was lost"; the
143143
client starts a new session. A provider restart still completes for the agent and the other sessions.
144+
A `providers/set` with the routing (base URL, API type, headers) that the last `providers/set` applied does not
145+
restart the app-server: it waits for a provider update in flight and succeeds. After a failed provider update it
146+
restarts again.
144147
- After `CODEX_ACP_APP_SERVER_CRASH_LIMIT` crashes in the crash window the adapter stops restarting it and says so,
145148
with the stderr of the last crash.
146149
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: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type {JsonValue} from "./app-server/serde_json/JsonValue";
2828
import {ModelId} from "./ModelId";
2929
import {AgentMode} from "./AgentMode";
3030
import path from "node:path";
31+
import {isDeepStrictEqual} from "node:util";
3132
import {logger} from "./Logger";
3233
import {isAccountReadAuthFailureError, isAccountReadUnavailableError} from "./CodexThreadErrors";
3334
import {listedSessionTitle, normalizeSessionTitle} from "./SessionTitle";
@@ -471,11 +472,10 @@ export class CodexAcpClient {
471472
}
472473
// Reuse needs the very same headers, so a rotated token lists again, and a failed listing then fails that
473474
// `providers/set`. Deliberate: AIR keeps its proxy key for the IDE's lifetime and does not rotate it.
474-
const previous = this.gatewayConfigSource === "acpProviders" ? this.gatewayConfig : null;
475+
const previousListing = this.hasProviderRouting(gatewayConfig) ? this.gatewayConfig?.listedModels ?? null : null;
475476
try {
476-
gatewayConfig.listedModels = previous?.listedModels && sameGatewayRouting(previous, gatewayConfig)
477-
? previous.listedModels
478-
: await listGatewayModels(gatewayConfig.config.base_url, gatewayConfig.config.http_headers);
477+
gatewayConfig.listedModels = previousListing
478+
?? await listGatewayModels(gatewayConfig.config.base_url, gatewayConfig.config.http_headers);
479479
} catch (error) {
480480
if (error instanceof GatewayListingError) {
481481
throw RequestError.internalError(
@@ -488,6 +488,13 @@ export class CodexAcpClient {
488488
return gatewayConfig;
489489
}
490490

491+
/** Whether `providers/set` already applied the provider config of `gatewayConfig`. */
492+
hasProviderRouting(gatewayConfig: GatewayConfig): boolean {
493+
return this.gatewayConfigSource === "acpProviders"
494+
&& this.gatewayConfig !== null
495+
&& sameProviderConfig(this.gatewayConfig, gatewayConfig);
496+
}
497+
491498
/** `providers/set`: replaces the routing with a configuration from {@link prepareProvider}. */
492499
setProvider(gatewayConfig: GatewayConfig): void {
493500
this.gatewayConfigSource = "acpProviders";
@@ -1614,6 +1621,10 @@ type WireApi = "responses";
16141621

16151622
type GatewayConfigSource = "authentication" | "acpProviders";
16161623

1624+
/**
1625+
* Everything that the gateway routing gives Codex. A setting that `providers/set` changes belongs here: an unchanged
1626+
* configuration, compared by {@link sameProviderConfig}, does not restart the app-server.
1627+
*/
16171628
export interface GatewayConfig {
16181629
modelProvider: string;
16191630
config: {
@@ -1665,13 +1676,12 @@ function buildGatewayConfig(params: GatewayRoutingParams): GatewayConfig {
16651676
};
16661677
}
16671678

1668-
function sameGatewayRouting(left: GatewayConfig, right: GatewayConfig): boolean {
1669-
const leftHeaders = Object.entries(left.config.http_headers);
1670-
const rightHeaders = right.config.http_headers;
1671-
return left.config.base_url === right.config.base_url
1672-
&& left.config.wire_api === right.config.wire_api
1673-
&& leftHeaders.length === Object.keys(rightHeaders).length
1674-
&& leftHeaders.every(([name, value]) => rightHeaders[name] === value);
1679+
/**
1680+
* Whether Codex gets the same provider config from both, compared by value with keys in any order.
1681+
* The model listing is a result of the config, not a part of it.
1682+
*/
1683+
function sameProviderConfig(left: GatewayConfig, right: GatewayConfig): boolean {
1684+
return left.modelProvider === right.modelProvider && isDeepStrictEqual(left.config, right.config);
16751685
}
16761686

16771687
function readMetaAdditionalRoots(meta?: Record<string, unknown> | null): string[] | undefined {

‎src/CodexAcpServer.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ export class CodexAcpServer {
556556
private readonly recovery: AppServerRecovery<SessionState> | null;
557557
private initializeRequest: Pick<acp.InitializeRequest, "clientInfo"> | null = null;
558558
private providerUpdate: Promise<void> | null = null;
559+
/** The last provider update failed, so the app-server may not run the routing of the agent. */
560+
private providerUpdateFailed = false;
559561
/**
560562
* Numbers `providers/set` and `providers/disable` requests in arrival order. A listing finishing after a newer
561563
* request was applied is dropped; a newer request that failed drops nothing.
@@ -2294,6 +2296,13 @@ export class CodexAcpServer {
22942296
return { };
22952297
}
22962298
this.appliedProviderRequest = request;
2299+
if (this.codexAcpClient.hasProviderRouting(gatewayConfig) && !this.providerUpdateFailed) {
2300+
// The same routing again needs no restart. A restart would lose every session that Codex has not
2301+
// persisted yet (no model turn). An update in flight that applies this routing still has to finish.
2302+
logger.log("providers/set unchanged: the app-server is not restarted");
2303+
await this.waitForProviderUpdate();
2304+
return { };
2305+
}
22972306
this.codexAcpClient.setProvider(gatewayConfig);
22982307
// The restarted app-server takes the listing too and does not list the models again.
22992308
await this.enqueueProviderUpdate((client) => client.setProvider(gatewayConfig));
@@ -2429,6 +2438,11 @@ export class CodexAcpServer {
24292438
}
24302439
});
24312440
this.providerUpdate = update;
2441+
update.then(() => {
2442+
this.providerUpdateFailed = false;
2443+
}, () => {
2444+
this.providerUpdateFailed = true;
2445+
});
24322446
this.recovery?.settleReplacement(update);
24332447
try {
24342448
await update;

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,43 @@ describe("app-server recovery", () => {
517517
await expect(prompt).resolves.toMatchObject({stopReason: "end_turn"});
518518
});
519519

520+
it("keeps a session without messages when providers/set repeats the applied routing", async () => {
521+
const fixture = createRecoveryFixture();
522+
await initialize(fixture);
523+
const provider = {providerId: "openai", apiType: "openai" as const, baseUrl: "https://gateway.example/v1", headers: {"X-Key": "k"}};
524+
await fixture.agent.setProvider(provider);
525+
const {sessionId} = await fixture.agent.newSession({cwd: "/work", mcpServers: []});
526+
527+
await fixture.agent.setProvider({...provider, headers: {...provider.headers}});
528+
529+
expect(fixture.servers).toHaveLength(1);
530+
const {prompt} = await startTurn(fixture, sessionId);
531+
completeTurn(fixture, sessionId);
532+
await expect(prompt).resolves.toMatchObject({stopReason: "end_turn"});
533+
await fixture.agent.setProvider({...provider, headers: {"X-Key": "rotated"}});
534+
expect(fixture.servers).toHaveLength(2);
535+
});
536+
537+
it("answers a repeated providers/set only after the provider update in flight for it", async () => {
538+
const fixture = createRecoveryFixture();
539+
const sessionId = await openSession(fixture);
540+
fixture.answers.set("thread/resume", () => undefined);
541+
const provider = {providerId: "openai", apiType: "openai" as const, baseUrl: "https://gateway.example/v1"};
542+
const first = fixture.agent.setProvider(provider);
543+
await vi.waitFor(() => expect(fixture.servers.length === 2 && fixture.current().rpc.hasPending("thread/resume")).toBe(true));
544+
545+
let repeatedDone = false;
546+
const repeated = fixture.agent.setProvider(provider).then(() => {
547+
repeatedDone = true;
548+
});
549+
await new Promise(resolve => setTimeout(resolve, 10));
550+
expect(repeatedDone).toBe(false);
551+
552+
fixture.current().rpc.resolve("thread/resume", resumed(sessionId));
553+
await Promise.all([first, repeated]);
554+
expect(fixture.servers).toHaveLength(2);
555+
});
556+
520557
it("keeps the provider routing of the agent across a crash restart", async () => {
521558
const fixture = createRecoveryFixture();
522559
await initialize(fixture);

0 commit comments

Comments
 (0)