diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 330524c99ad3..35e386adfa4b 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -5074,6 +5074,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { "steer", "start-fails", "keep-changed-tier", + "keep-changed-model", ] as const)("handles terminal Codex capacity failures when retries %s", (outcome) => Effect.gen(function* () { const scenario = `capacity-${outcome}`; @@ -5118,6 +5119,8 @@ describe("CodexAdapterV2 post-settle continuation", () => { }); // A speed picked while the first attempt runs; the retry must not send the old one. const changedTier = outcome === "keep-changed-tier" ? "fast" : undefined; + // A model picked during the backoff; the retry must start on it. + const retryModel = outcome === "keep-changed-model" ? "gpt-5.5" : "gpt-5.4"; if (changedTier !== undefined) { entries.push( { @@ -5179,12 +5182,12 @@ describe("CodexAdapterV2 post-settle continuation", () => { threadId: nativeThreadId, input: [], cwd: "/workspace", - model: "gpt-5.4", + model: retryModel, approvalPolicy: "never", approvalsReviewer: "user", sandboxPolicy: { type: "dangerFullAccess" }, summary: "auto", - collaborationMode: { mode: "default", settings: { model: "gpt-5.4" } }, + collaborationMode: { mode: "default", settings: { model: retryModel } }, ...(changedTier === undefined ? {} : { serviceTier: changedTier }), }, }, @@ -5249,6 +5252,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { if ( outcome === "recover" || outcome === "keep-changed-tier" || + outcome === "keep-changed-model" || outcome === "completion-only" || outcome === "started-first" || outcome === "stop-starting" @@ -5379,6 +5383,14 @@ describe("CodexAdapterV2 post-settle continuation", () => { }) .pipe(Effect.forkScoped) : undefined; + if (outcome === "keep-changed-model") { + yield* ( + harness.runtime.applyModelSelection?.({ + providerThread: harness.providerThread, + modelSelection: { ...CODEX_TEST_MODEL_SELECTION, model: retryModel }, + }) ?? Effect.void + ); + } if (renamedBeforeRetry) { yield* harness.runtime.applyThreadTitle!({ providerThread: harness.providerThread, @@ -5423,11 +5435,17 @@ describe("CodexAdapterV2 post-settle continuation", () => { assert.equal(nextRetry.turnItem.retry?.attempt, 1); return; } + if (outcome === "keep-changed-model") + assert.deepInclude(starts[1], { + model: retryModel, + collaborationMode: { mode: "default", settings: { model: retryModel } }, + }); const terminal = harness.terminalEvents()[0]; assert.equal( terminal?.status, outcome === "recover" || outcome === "keep-changed-tier" || + outcome === "keep-changed-model" || outcome === "completion-only" || outcome === "steer" || outcome === "started-first" || diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 1c38aaebffc8..9b33ac0d6f85 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -1387,6 +1387,8 @@ function failureResetAt( interface ActiveCodexTurnContext { hookStopReason?: string; appliedServiceTier?: string; + /** Picked while the turn runs; a capacity retry starts on it. */ + latestModelSelection?: ModelSelection; capacityRetryAttempt?: number; capacityRetryPending?: Deferred.Deferred; closedCommandItemIds?: ReadonlySet; @@ -7448,17 +7450,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( ); if (!claimed) return; // Notifications before the RPC response also take the terminalization permit. + const modelSelection = + context.latestModelSelection ?? context.input.modelSelection; yield* startNativeTurn( { ...context.input, + modelSelection, // A speed changed during the failed attempt outlives the retry. ...(context.appliedServiceTier === undefined ? {} : { modelSelection: { - ...context.input.modelSelection, + ...modelSelection, options: [ - ...(context.input.modelSelection.options ?? []).filter( + ...(modelSelection.options ?? []).filter( (option) => option.id !== "serviceTier" && option.id !== "fastMode", ), @@ -8464,7 +8469,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( modelSelection, }) { const threadId = providerThread.nativeThreadRef?.nativeId; - if (threadId != null) yield* applyServiceTier(threadId, modelSelection); + if (threadId == null) return; + for (const turn of (yield* Ref.get(activeTurns)).values()) { + if (turn.providerThread.nativeThreadRef?.nativeId === threadId) { + turn.latestModelSelection = modelSelection; + } + } + yield* applyServiceTier(threadId, modelSelection); }), // Dropping this connection's subscription lets the shared app-server // shut the native thread (and its MCP servers) down once it is idle.