TypeScript
fix(server): retry Codex capacity failures with empty input (#25611)
Yash Singh740de0e642d6
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.tsindex 602e1a4d56f..1e762543a13 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts@@ -2214,6 +2214,436 @@ describe("CodexAdapterV2 post-settle continuation", () => {),);+ it.effect.each([+ "recover",+ "completion-only",+ "exhaust",+ "stop",+ "stop-continued",+ "stop-starting",+ "started-first",+ "completed-first",+ "new-run",+ "steer",+ "start-fails",+ ] as const)("handles terminal Codex capacity failures when retries %s", (outcome) =>+ Effect.scoped(+ Effect.gen(function* () {+ const scenario = `capacity-${outcome}`;+ const nativeThreadId = `native-${scenario}`;+ const nativeTurnId = (attempt: number) => `${nativeThreadId}-${attempt}`;+ const prompt = "Continue the task.";+ const capacityError = {+ message: "Selected model is at capacity. Please try a different model.",+ codexErrorInfo: "serverOverloaded",+ };+ const failureEntries = (attempt: number): Array<CodexReplay.CodexAppServerReplayEntry> => [+ {+ type: "emit_inbound",+ frame: {+ method: "error",+ params: {+ threadId: nativeThreadId,+ turnId: nativeTurnId(attempt),+ willRetry: false,+ error: capacityError,+ },+ },+ },+ {+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: nativeThreadId,+ turn: {+ ...makeCodexReplayTurn({ id: nativeTurnId(attempt), status: "failed" }),+ error: capacityError,+ },+ },+ },+ },+ ];+ const entries = codexReplayPreamble({+ nativeThreadId,+ nativeTurnId: nativeTurnId(0),+ prompt,+ });+ entries.push(+ ...(outcome === "completion-only" ? failureEntries(0).slice(1) : failureEntries(0)),+ );+ if (outcome === "completion-only") entries.push(...failureEntries(0).slice(1));+ const retries = outcome === "exhaust" ? 5 : outcome === "stop" ? 0 : 1;+ const steeredText = "Use the smaller change.";+ for (let attempt = 1; attempt <= retries; attempt++) {+ const start = codexReplayPreamble({+ nativeThreadId,+ nativeTurnId: nativeTurnId(attempt),+ prompt,+ }).slice(-3);+ if (+ outcome === "started-first" ||+ outcome === "stop-starting" ||+ outcome === "completed-first" ||+ outcome === "new-run"+ ) {+ start.splice(1, 0, ...start.splice(2, 1));+ }+ for (const entry of start) {+ if (entry.type === "expect_outbound") {+ entries.push({+ ...entry,+ frame: {+ id: 3 + attempt,+ method: "turn/start",+ params: {+ threadId: nativeThreadId,+ input: [],+ cwd: "/workspace",+ model: "gpt-5.4",+ approvalPolicy: "never",+ approvalsReviewer: "user",+ sandboxPolicy: { type: "dangerFullAccess" },+ summary: "detailed",+ collaborationMode: { mode: "default", settings: { model: "gpt-5.4" } },+ },+ },+ });+ } else if (entry.type === "emit_inbound" && entry.label === "turn/start") {+ if (outcome === "completed-first" || outcome === "new-run")+ entries.push({+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: nativeThreadId,+ turn: makeCodexReplayTurn({ id: nativeTurnId(attempt), status: "completed" }),+ },+ },+ });+ if (outcome === "new-run") {+ entries.push(+ ...codexReplayPreamble({+ nativeThreadId,+ nativeTurnId: nativeTurnId(2),+ prompt: "Next task.",+ })+ .slice(-3)+ .map((next, index) => (index < 2 ? withReplayRequestId(next, 5) : next)),+ ...failureEntries(2),+ );+ }+ entries.push(+ outcome === "start-fails"+ ? {+ type: "emit_inbound",+ frame: {+ id: 3 + attempt,+ error: { code: -32603, message: "Start failed" },+ },+ }+ : withReplayRequestId(entry, 3 + attempt),+ );+ } else if (outcome !== "start-fails") {+ entries.push(+ outcome === "stop-starting" && entry.type === "emit_inbound"+ ? { ...entry, afterMs: 1_000 }+ : entry,+ );+ if (outcome === "stop-starting") {+ entries.push(+ {+ type: "expect_outbound",+ frame: {+ id: 5,+ method: "turn/interrupt",+ params: { threadId: nativeThreadId, turnId: nativeTurnId(attempt) },+ },+ },+ { type: "emit_inbound", frame: { id: 5, result: {} } },+ );+ }+ }+ }+ if (outcome === "exhaust") entries.push(...failureEntries(attempt));+ if (+ outcome === "recover" ||+ outcome === "completion-only" ||+ outcome === "started-first" ||+ outcome === "stop-starting"+ ) {+ entries.push({+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: nativeThreadId,+ turn: makeCodexReplayTurn({+ id: nativeTurnId(attempt),+ status: outcome === "stop-starting" ? "interrupted" : "completed",+ }),+ },+ },+ });+ }+ if (outcome === "stop-continued" || outcome === "steer") {+ const steering = outcome === "steer";+ entries.push(+ {+ type: "expect_outbound",+ frame: {+ id: 5,+ method: steering ? "turn/steer" : "turn/interrupt",+ params: steering+ ? {+ threadId: nativeThreadId,+ expectedTurnId: nativeTurnId(attempt),+ input: [{ type: "text", text: steeredText }],+ }+ : { threadId: nativeThreadId, turnId: nativeTurnId(attempt) },+ },+ },+ {+ type: "emit_inbound",+ frame: { id: 5, result: steering ? { turnId: nativeTurnId(attempt) } : {} },+ },+ {+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: nativeThreadId,+ turn: makeCodexReplayTurn({+ id: nativeTurnId(attempt),+ status: steering ? "completed" : "interrupted",+ }),+ },+ },+ },+ );+ }+ }+ const milestones = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();+ const starts: Array<unknown> = [];+ const retryStarted = yield* Deferred.make<void>();+ const harness = yield* makeCodexReplayHarness(+ makeCodexReplayTranscript({ scenario, entries }),+ (event) =>+ event.type === "turn.terminal" ||+ (event.type === "turn_item.updated" &&+ event.turnItem.type === "error" &&+ event.turnItem.status === "running")+ ? Queue.offer(milestones, event)+ : Effect.void,+ (method, params) =>+ Effect.gen(function* () {+ if (method === "turn/start") {+ starts.push(params);+ if (starts.length === 2) yield* Deferred.succeed(retryStarted, undefined);+ }+ }),+ );+ const turnInput = makeCodexTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now: yield* DateTime.now,+ attemptId: RunAttemptId.make(scenario),+ text: prompt,+ });+ yield* harness.runtime.startTurn(turnInput);+ for (let attempt = 1; attempt <= Math.max(retries, 1); attempt++) {+ const milestone = yield* Queue.take(milestones);+ assert.equal(milestone.type, "turn_item.updated");+ if (milestone.type !== "turn_item.updated" || milestone.turnItem.type !== "error") return;+ const providerTurnId = milestone.turnItem.providerTurnId;+ assert.isNotNull(providerTurnId);+ if (providerTurnId === null) return;+ assert.deepEqual(milestone.turnItem.retry, {+ attempt,+ maxAttempts: 5,+ retryDelayMs: Math.min(2_000 * 2 ** (attempt - 1), 30_000),+ });+ assert.isEmpty(harness.terminalEvents());+ if (outcome === "stop") {+ yield* harness.runtime.interruptTurn({+ providerThread: harness.providerThread,+ providerTurnId,+ });+ }+ const steering =+ outcome === "steer"+ ? yield* harness.runtime+ .steerTurn({+ ...turnInput,+ providerTurnId,+ message: { ...turnInput.message, text: steeredText },+ })+ .pipe(Effect.forkScoped)+ : undefined;+ yield* TestClock.adjust(Math.min(2_000 * 2 ** (attempt - 1), 30_000));+ if (outcome === "stop-starting") {+ yield* Deferred.await(retryStarted);+ const stopping = yield* harness.runtime+ .interruptTurn({ providerThread: harness.providerThread, providerTurnId })+ .pipe(Effect.forkScoped);+ yield* TestClock.adjust("1 second");+ yield* Fiber.join(stopping);+ }+ if (steering !== undefined) yield* Fiber.join(steering);+ if (outcome === "stop-continued") {+ yield* harness.until(() =>+ harness.events.some(+ (event) =>+ event.type === "provider_turn.updated" &&+ event.providerTurn.nativeTurnRef?.nativeId === nativeTurnId(1),+ ),+ );+ yield* harness.runtime.interruptTurn({+ providerThread: harness.providerThread,+ providerTurnId,+ });+ }+ }+ yield* harness.awaitTerminal(1);+ if (outcome === "new-run") {+ assert.equal((yield* Queue.take(milestones)).type, "turn.terminal");+ yield* harness.runtime.startTurn({+ ...turnInput,+ attemptId: RunAttemptId.make("capacity-next-task"),+ message: { ...turnInput.message, text: "Next task." },+ });+ const nextRetry = yield* Queue.take(milestones);+ assert.equal(nextRetry.type, "turn_item.updated");+ if (nextRetry.type !== "turn_item.updated" || nextRetry.turnItem.type !== "error") return;+ assert.equal(nextRetry.turnItem.retry?.attempt, 1);+ return;+ }+ const terminal = harness.terminalEvents()[0];+ assert.equal(+ terminal?.status,+ outcome === "recover" ||+ outcome === "completion-only" ||+ outcome === "steer" ||+ outcome === "started-first" ||+ outcome === "completed-first"+ ? "completed"+ : outcome === "stop" || outcome === "stop-continued" || outcome === "stop-starting"+ ? "interrupted"+ : "failed",+ );+ assert.lengthOf(starts, 1 + retries);+ assert.lengthOf(harness.terminalEvents(), 1);+ assert.lengthOf(+ harness.events.filter(+ (event) =>+ event.type === "provider_turn.updated" && event.providerTurn.status === "running",+ ),+ outcome === "start-fails" ? 1 : 1 + retries,+ );+ const retryItems = harness.events.flatMap((event) =>+ event.type === "turn_item.updated" && event.turnItem.type === "error"+ ? [event.turnItem]+ : [],+ );+ assert.isTrue(+ ["completed", "failed", "interrupted"].includes(retryItems.at(-1)?.status ?? ""),+ );+ assert.lengthOf(new Set(retryItems.map((item) => item.id)), 1);++ if (outcome === "recover") {+ const fs = yield* FileSystem.FileSystem;+ const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-capacity-retry-" });+ const localTranscript = yield* decodeReplayTranscriptJson(+ (yield* encodeReplayTranscriptJson(+ makeCodexReplayTranscript({ scenario, entries }),+ )).replaceAll(yield* encodeStringJson("/workspace"), yield* encodeStringJson(cwd)),+ );+ const driver = yield* CodexReplay.makeReplayDriver(localTranscript);+ yield* Effect.gen(function* () {+ const orchestrator = yield* Orchestrator.OrchestratorV2;+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;+ const threadId = ThreadId.make("thread:capacity-retry");+ yield* orchestrator.dispatch({+ type: "thread.create",+ commandId: CommandId.make("create-capacity-retry"),+ threadId,+ projectId: ProjectId.make("project:capacity-retry"),+ title: "Capacity retry",+ modelSelection: CODEX_TEST_MODEL_SELECTION,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: cwd,+ createdBy: "user",+ creationSource: "web",+ });+ const retrying = yield* orchestrator.streamDomainEvents.pipe(+ Stream.filter(+ (event) =>+ event.type === "turn-item.updated" &&+ event.payload.type === "error" &&+ event.payload.retry != null &&+ event.payload.status === "running",+ ),+ Stream.runHead,+ Effect.forkChild({ startImmediately: true }),+ );+ yield* orchestrator.dispatch({+ type: "message.dispatch",+ commandId: CommandId.make("start-capacity-retry"),+ threadId,+ messageId: MessageId.make("message:capacity-retry"),+ text: prompt,+ attachments: [],+ createdBy: "user",+ creationSource: "web",+ dispatchMode: { type: "start_immediately" },+ });+ yield* worker.drain();+ yield* Fiber.join(retrying);+ assert.equal(+ (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,+ "running",+ );+ const completed = yield* orchestrator.streamDomainEvents.pipe(+ Stream.filter(+ (event) => event.type === "run.updated" && event.payload.status === "waiting",+ ),+ Stream.runHead,+ Effect.forkChild({ startImmediately: true }),+ );+ yield* TestClock.adjust("2 seconds");+ yield* Fiber.join(completed);+ yield* worker.drain();+ const projection = yield* orchestrator.getThreadProjection(threadId);+ assert.equal(projection.runs.at(-1)?.status, "completed");+ assert.deepEqual(+ projection.providerTurns.map((turn) => turn.status),+ ["failed", "completed"],+ );+ assert.lengthOf(+ projection.messages.filter((message) => message.role === "user"),+ 1,+ );+ assert.isNull((yield* Ref.get(driver.state)).failure);+ }).pipe(+ Effect.provide(+ ProviderReplayHarness.layerWithRegistry(+ { name: "codex-capacity-retry", runtimePolicyOverride: { cwd } },+ CodexAdapterV2Testkit.layer({ transcript: localTranscript, driver }),+ { runEffectWorker: false },+ ),+ ),+ );+ }+ }).pipe(+ Effect.provide(+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),+ ),+ ),+ ),+ );+it.effect("resolves retryable app-server errors on resumed provider activity", () =>Effect.scoped(Effect.gen(function* () {@@ -5007,6 +5437,265 @@ describe("CodexAdapterV2 post-settle continuation", () => {};};+ it.effect.each(["completed", "failed", "interrupted", "starting", "replied-last"] as const)(+ "handles a late native child after the capacity retry %s",+ (parentStatus) =>+ Effect.scoped(+ Effect.gen(function* () {+ const retriedTurnId = "native-capacity-recovered-parent";+ const retrying = yield* Deferred.make<void>();+ const entries: Array<CodexReplay.CodexAppServerReplayEntry> = [+ ...lateChildPrefix().spawned,+ {+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: RESUME_NATIVE_THREAD,+ turn: {+ ...makeCodexReplayTurn({ id: RESUME_NATIVE_TURN, status: "failed" }),+ error: {+ message: "Selected model is at capacity.",+ codexErrorInfo: "serverOverloaded",+ },+ },+ },+ },+ },+ {+ type: "expect_outbound",+ frame: {+ id: 4,+ method: "turn/start",+ params: {+ threadId: RESUME_NATIVE_THREAD,+ input: [],+ cwd: "/workspace",+ model: "gpt-5.4",+ approvalPolicy: "never",+ approvalsReviewer: "user",+ sandboxPolicy: { type: "dangerFullAccess" },+ summary: "detailed",+ collaborationMode: { mode: "default", settings: { model: "gpt-5.4" } },+ },+ },+ },+ ...(parentStatus === "starting" ? [childTurnCompleted(RESUME_CHILD_TURN_1)] : []),+ {+ type: "emit_inbound",+ frame: {+ id: 4,+ result: {+ turn: makeCodexReplayTurn({ id: retriedTurnId, status: "inProgress" }),+ },+ },+ },+ {+ type: "emit_inbound",+ frame: {+ method: "turn/started",+ params: {+ threadId: RESUME_NATIVE_THREAD,+ turn: makeCodexReplayTurn({ id: retriedTurnId, status: "inProgress" }),+ },+ },+ },+ {+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: RESUME_NATIVE_THREAD,+ turn: {+ ...makeCodexReplayTurn({+ id: retriedTurnId,+ status:+ parentStatus === "starting" || parentStatus === "replied-last"+ ? "completed"+ : parentStatus,+ }),+ error:+ parentStatus === "failed"+ ? { message: "Recovery failed.", codexErrorInfo: "internalServerError" }+ : null,+ },+ },+ },+ },+ childAgentMessage({+ id: "child-after-capacity",+ text: "CAPACITY_CHILD_DONE",+ turnId: RESUME_CHILD_TURN_1,+ completedAtMs: 1782622480000,+ afterMs: 60_000,+ }),+ childTurnCompleted(RESUME_CHILD_TURN_1),+ ];+ if (parentStatus === "replied-last") {+ entries.push(...entries.splice(lateChildPrefix().spawned.length + 2, 1));+ }+ const harness = yield* makeCodexReplayHarness(+ makeCodexReplayTranscript({ scenario: "capacity-late-child", entries }),+ (event) =>+ event.type === "turn_item.updated" &&+ event.turnItem.type === "error" &&+ event.turnItem.status === "running"+ ? Deferred.succeed(retrying, undefined)+ : Effect.void,+ );+ yield* harness.runtime.startTurn(+ makeCodexTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now: yield* DateTime.now,+ attemptId: RunAttemptId.make("capacity-late-child"),+ text: RESUME_PROMPT,+ }),+ );+ yield* Deferred.await(retrying);+ yield* TestClock.adjust("2 seconds");+ if (parentStatus === "interrupted") assert.isEmpty(harness.terminalEvents());+ else {+ yield* harness.awaitTerminal(1);+ const terminal = harness.terminalEvents()[0];+ if (terminal?.status === "failed") assert.isTrue(terminal.childTurnsContinue);+ }+ yield* TestClock.adjust("60 seconds");+ yield* harness.until(+ () => harness.subagentUpdates().at(-1)?.subagent.status === "completed",+ );+ yield* harness.awaitTerminal(1);+ assert.lengthOf(+ harness.continuationRequests,+ parentStatus === "completed" || parentStatus === "replied-last" ? 1 : 0,+ );+ if (parentStatus === "completed" || parentStatus === "replied-last")+ assert.include(harness.continuationRequests[0]?.detail, "CAPACITY_CHILD_DONE");+ }).pipe(+ Effect.provide(+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),+ ),+ ),+ ),+ );++ it.effect("stops native children when capacity retry startup fails", () =>+ Effect.scoped(+ Effect.gen(function* () {+ const retrying = yield* Deferred.make<void>();+ const starting = yield* Deferred.make<void>();+ const release = yield* Deferred.make<void>();+ const interrupts: unknown[] = [];+ let starts = 0;+ const harness = yield* makeCodexReplayHarness(+ makeCodexReplayTranscript({+ scenario: "capacity-start-fails-stop-child",+ entries: [+ ...lateChildPrefix().spawned,+ {+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: RESUME_NATIVE_THREAD,+ turn: {+ ...makeCodexReplayTurn({ id: RESUME_NATIVE_TURN, status: "failed" }),+ error: { message: "At capacity", codexErrorInfo: "serverOverloaded" },+ },+ },+ },+ },+ {+ type: "expect_outbound",+ frame: {+ id: 4,+ method: "turn/start",+ params: {+ threadId: RESUME_NATIVE_THREAD,+ input: [],+ cwd: "/workspace",+ model: "gpt-5.4",+ approvalPolicy: "never",+ approvalsReviewer: "user",+ sandboxPolicy: { type: "dangerFullAccess" },+ summary: "detailed",+ collaborationMode: { mode: "default", settings: { model: "gpt-5.4" } },+ },+ },+ },+ {+ type: "emit_inbound",+ frame: { id: 4, error: { code: -32603, message: "Start failed" } },+ },+ {+ type: "expect_outbound",+ frame: {+ id: 5,+ method: "turn/interrupt",+ params: { threadId: RESUME_CHILD_THREAD, turnId: RESUME_CHILD_TURN_1 },+ },+ },+ { type: "emit_inbound", frame: { id: 5, result: {} } },+ {+ type: "emit_inbound",+ frame: {+ method: "turn/completed",+ params: {+ threadId: RESUME_CHILD_THREAD,+ turn: makeCodexReplayTurn({ id: RESUME_CHILD_TURN_1, status: "interrupted" }),+ },+ },+ },+ ],+ }),+ (event) =>+ event.type === "turn_item.updated" &&+ event.turnItem.type === "error" &&+ event.turnItem.status === "running"+ ? Deferred.succeed(retrying, undefined)+ : Effect.void,+ (method, params) =>+ Effect.gen(function* () {+ if (method === "turn/interrupt") interrupts.push(params);+ if (method === "turn/start" && ++starts === 2) {+ yield* Deferred.succeed(starting, undefined);+ yield* Deferred.await(release);+ }+ }),+ );+ yield* harness.runtime.startTurn(+ makeCodexTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now: yield* DateTime.now,+ attemptId: RunAttemptId.make("capacity-start-fails-stop-child"),+ text: RESUME_PROMPT,+ }),+ );+ yield* Deferred.await(retrying);+ const root = harness.events.find((event) => event.type === "provider_turn.updated");+ assert.equal(root?.type, "provider_turn.updated");+ if (root?.type !== "provider_turn.updated") return;+ yield* TestClock.adjust("2 seconds");+ yield* Deferred.await(starting);+ const stopping = yield* harness.runtime+ .interruptTurn({+ providerThread: harness.providerThread,+ providerTurnId: root.providerTurn.id,+ })+ .pipe(Effect.forkChild({ startImmediately: true }));+ yield* Deferred.succeed(release, undefined);+ yield* Fiber.join(stopping);+ assert.lengthOf(interrupts, 1);+ assert.equal(harness.subagentUpdates().at(-1)?.subagent.status, "interrupted");+ }).pipe(+ Effect.provide(+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),+ ),+ ),+ ),+ );+it.effect("names a late child spawned without a prompt in its wake", () =>Effect.scoped(Effect.gen(function* () {@@ -6434,6 +7123,96 @@ describe("CodexAdapterV2 post-settle continuation", () => {),);+ it.effect("Stop prevents a capacity retry while pausing its goal", () =>+ Effect.gen(function* () {+ const nativeTurnId = "goal-capacity-stop";+ const prompt = "Keep going";+ const retrying = yield* Deferred.make<void>();+ const pausing = yield* Deferred.make<void>();+ const requests: Array<string> = [];+ const harness = yield* makeCodexReplayHarness(+ makeCodexReplayTranscript({+ scenario: "goal-capacity-stop",+ entries: [+ ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt }),+ notification("goal active", "thread/goal/updated", {+ threadId: nativeThreadId,+ turnId: nativeTurnId,+ goal: codexGoal("active", 10),+ }),+ notification("capacity failure", "turn/completed", {+ threadId: nativeThreadId,+ turn: {+ ...makeCodexReplayTurn({ id: nativeTurnId, status: "failed" }),+ error: {+ message: "Selected model is at capacity.",+ codexErrorInfo: "serverOverloaded",+ },+ },+ }),+ {+ type: "expect_outbound",+ frame: {+ id: 4,+ method: "thread/goal/set",+ params: { threadId: nativeThreadId, status: "paused" },+ },+ },+ {+ type: "emit_inbound",+ afterMs: 3_000,+ frame: { id: 4, result: { goal: codexGoal("paused", 10) } },+ },+ ],+ }),+ (event) =>+ event.type === "turn_item.updated" &&+ event.turnItem.type === "error" &&+ event.turnItem.status === "running"+ ? Deferred.succeed(retrying, undefined)+ : Effect.void,+ (method) =>+ Effect.gen(function* () {+ requests.push(method);+ if (method === "thread/goal/set") yield* Deferred.succeed(pausing, undefined);+ }),+ );+ yield* harness.runtime.startTurn(+ makeCodexTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now: yield* DateTime.now,+ attemptId: RunAttemptId.make("goal-capacity-stop"),+ text: prompt,+ }),+ );+ yield* Deferred.await(retrying);+ const stopping = yield* harness.runtime+ .interruptTurn({+ providerThread: harness.providerThread,+ providerTurnId: (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({+ driver: CodexAdapterV2.CODEX_DRIVER_KIND,+ nativeTurnId,+ }),+ })+ .pipe(Effect.forkScoped);+ yield* Deferred.await(pausing);+ yield* TestClock.adjust("3 seconds");+ assert.lengthOf(+ requests.filter((method) => method === "turn/start"),+ 1,+ );+ yield* Fiber.join(stopping);+ yield* harness.awaitTerminal(1);+ assert.equal(harness.terminalEvents()[0]?.status, "interrupted");+ }).pipe(+ Effect.scoped,+ Effect.provide(+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),+ ),+ ),+ );+it.effect("pauses an active goal before Stop interrupts its turn", () =>Effect.gen(function* () {const prompt = "Keep going";diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.tsindex 6dba639b63b..74c4b46646e 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts@@ -86,6 +86,7 @@ import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";import * as Fiber from "effect/Fiber";import * as FileSystem from "effect/FileSystem";+import * as Function from "effect/Function";import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Path from "effect/Path";@@ -152,6 +153,7 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";const CODEX_PROVIDER = ProviderDriverKind.make("codex");+const CODEX_CAPACITY_MAX_RETRIES = 5;export const CODEX_DRIVER_KIND = CODEX_PROVIDER;export const CODEX_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(CODEX_DRIVER_KIND);@@ -1109,6 +1111,8 @@ function withCodexManagedFailure(}interface ActiveCodexTurnContext {+ capacityRetryAttempt?: number;+ capacityRetryPending?: Deferred.Deferred<ActiveCodexTurnContext | undefined>;closedCommandItemIds?: ReadonlySet<string>;latestProviderFailure?: {readonly nativeMessage: string;@@ -1141,6 +1145,7 @@ interface ActiveCodexTurnContext {}interface ActiveCodexProviderRetry {+ readonly context?: ActiveCodexTurnContext;readonly nativeMessage: string;readonly nativeCode: string | null;readonly retry: OrchestrationV2ProviderRetry;@@ -1149,6 +1154,30 @@ interface ActiveCodexProviderRetry {readonly itemOrdinal: number;}+function codexRetryTurnItem(+ activeContext: ActiveCodexTurnContext,+ state: ActiveCodexProviderRetry,+ status: Parameters<typeof makeProviderRetryTurnItem>[0]["status"],+ updatedAt: DateTime.Utc,+) {+ // A continuation keeps the retry row attached to the native turn that failed.+ const context = state.context ?? activeContext;+ return makeProviderRetryTurnItem({+ driver: CODEX_PROVIDER,+ threadId: context.projectionThreadId,+ runId: context.projectionRunId,+ nodeId: context.providerNodeId,+ providerThreadId: context.providerThread.id,+ providerTurnId: context.providerTurnId,+ itemOrdinal: state.itemOrdinal,+ failure: state.failure,+ retry: state.retry,+ status,+ startedAt: state.startedAt,+ updatedAt,+ });+}+interface TrackedRunningCommandItem {readonly id: string;readonly command: string;@@ -1815,12 +1844,23 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (const goalsByNativeThread = new Map<string, OrchestrationV2ProviderGoal | null>();const rootProviderThreads = new Map<string, OrchestrationV2ProviderThread>();const goalHolds = new Map<string, CodexGoalHold>();- const goalRuns = new Map<ProviderTurnId, Array<ActiveCodexTurnContext>>();+ const continuedRuns = new Map<ProviderTurnId, Array<ActiveCodexTurnContext>>();+ const capacityContinuations = new Map<+ string,+ { readonly context: ActiveCodexTurnContext; readonly retry: ActiveCodexProviderRetry }+ >();const goalActivations = new Map<string, { stopped: boolean }>();// Held runs must not complete while Stop resolves its target.const goalStops = new Map<string, number>();- const latestGoalTurnId = (providerTurnId: ProviderTurnId) =>- goalRuns.get(providerTurnId)?.at(-1)?.providerTurnId ?? providerTurnId;+ const latestRunTurnId = (providerTurnId: ProviderTurnId) =>+ continuedRuns.get(providerTurnId)?.at(-1)?.providerTurnId ?? providerTurnId;+ const isContinuedRunDescendant = (+ candidate: ActiveCodexTurnContext,+ context: ActiveCodexTurnContext,+ ) =>+ (continuedRuns.get(context.providerTurnId) ?? [context]).some((ancestor) =>+ isDescendantCodexTurn(candidate, ancestor),+ );const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) =>Queue.offer(events, event).pipe(Effect.asVoid);@@ -1882,20 +1922,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (yield* emitProviderEvent({type: "turn_item.updated",driver: CODEX_PROVIDER,- turnItem: makeProviderRetryTurnItem({- driver: CODEX_PROVIDER,- threadId: context.projectionThreadId,- runId: context.projectionRunId,- nodeId: context.providerNodeId,- providerThreadId: context.providerThread.id,- providerTurnId: context.providerTurnId,- itemOrdinal: providerRetry.itemOrdinal,- failure: providerRetry.failure,- retry: providerRetry.retry,- status: "completed",- startedAt: providerRetry.startedAt,- updatedAt,- }),+ turnItem: codexRetryTurnItem(context, providerRetry, "completed", updatedAt),});});@@ -1934,6 +1961,24 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (fileChangeKindsByItem: new Map(),runningCompactionItemIds: new Set(),};+ const nativeThreadId = input.turnInput.providerThread.nativeThreadRef?.nativeId;+ const continuation =+ nativeThreadId == null ? undefined : capacityContinuations.get(nativeThreadId);+ if (continuation !== undefined) {+ context.capacityRetryAttempt = continuation.context.capacityRetryAttempt ?? 0;+ yield* Ref.update(providerRetries, (current) =>+ new Map(current).set(providerTurnId, continuation.retry),+ );+ const turns = continuedRuns.get(continuation.context.providerTurnId) ?? [+ continuation.context,+ ];+ turns.push(context);+ continuedRuns.set(continuation.context.providerTurnId, turns);+ continuedRuns.set(providerTurnId, turns);+ if (continuation.context.capacityRetryPending !== undefined) {+ yield* Deferred.succeed(continuation.context.capacityRetryPending, context);+ }+ }yield* Ref.update(limitedTurnItems, (current) => {const next = new Map(current);next.delete(context.providerThread.id);@@ -1946,6 +1991,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (updated.set(input.nativeTurnId, context);return updated;});+ if (continuation !== undefined && nativeThreadId != null) {+ capacityContinuations.delete(nativeThreadId);+ }yield* emitProviderEvent({type: "provider_turn.updated",driver: CODEX_PROVIDER,@@ -4380,10 +4428,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (nativeTurnId: payload.turn.id,startedAt: codexTimestamp(payload.turn.startedAt),});- const goalRun = goalRuns.get(goalHold.context.providerTurnId) ?? [goalHold.context];+ const goalRun = continuedRuns.get(goalHold.context.providerTurnId) ?? [+ goalHold.context,+ ];goalRun.push(next);- goalRuns.set(goalHold.context.providerTurnId, goalRun);- goalRuns.set(next.providerTurnId, goalRun);+ continuedRuns.set(goalHold.context.providerTurnId, goalRun);+ continuedRuns.set(next.providerTurnId, goalRun);yield* Deferred.succeed(goalHold.next, next);return;}@@ -4493,6 +4543,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (previous?.itemOrdinal ??(yield* resolveItemOrdinal(context, `terminal-failure:${context.providerTurnId}`));const state: ActiveCodexProviderRetry = {+ ...(previous?.context === undefined ? {} : { context: previous.context }),nativeMessage: payload.error.message,nativeCode: code,retry,@@ -4508,20 +4559,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (yield* emitProviderEvent({type: "turn_item.updated",driver: CODEX_PROVIDER,- turnItem: makeProviderRetryTurnItem({- driver: CODEX_PROVIDER,- threadId: context.projectionThreadId,- runId: context.projectionRunId,- nodeId: context.providerNodeId,- providerThreadId: context.providerThread.id,- providerTurnId: context.providerTurnId,- itemOrdinal,- failure,- retry,- status: "running",- startedAt: state.startedAt,- updatedAt,- }),+ turnItem: codexRetryTurnItem(context, state, "running", updatedAt),});}).pipe(Effect.orDie),);@@ -5617,8 +5655,16 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (}: event;yield* emitProviderEvent(current);- for (const turn of goalRuns.get(context.providerTurnId) ?? []) {- goalRuns.delete(turn.providerTurnId);+ const continued = continuedRuns.get(context.providerTurnId) ?? [context];+ if (current.status !== "completed") {+ yield* Ref.update(+ terminalizedNonCompletedNativeTurns,+ (nativeTurns) =>+ new Set([...nativeTurns, ...continued.map((turn) => turn.nativeTurnId)]),+ );+ }+ for (const turn of continued) {+ continuedRuns.delete(turn.providerTurnId);}const nativeThreadId = context.providerThread.nativeThreadRef?.nativeId;const rootProviderThread =@@ -5656,7 +5702,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (}) {const event = yield* makeRootTerminalEvent(input);const hasActiveDescendants = Array.from((yield* Ref.get(activeTurns)).values()).some(- (candidate) => isDescendantCodexTurn(candidate, input.context),+ (candidate) => isContinuedRunDescendant(candidate, input.context),);// Failed roots leave native children working and must settle without them.// Interrupted roots wait for descendants because Stop interrupts the whole lineage.@@ -5781,7 +5827,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (for (const [nativeTurnId, deferred] of current) {if (!activeTurnContexts.some((candidate) =>- isDescendantCodexTurn(candidate, deferred.context),+ isContinuedRunDescendant(candidate, deferred.context),)) {updated.delete(nativeTurnId);@@ -5803,8 +5849,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (readonly completedAt: DateTime.Utc;readonly failureMessage?: string;readonly failureErrorInfo?: CodexErrorInfo;+ readonly continueRun?: boolean;}) =>- turnTerminalizationPermit.withPermits(1)(+ // Capacity continuation already holds the permit across finalization and restart.+ (input.continueRun ? Function.identity : turnTerminalizationPermit.withPermits(1))(Effect.gen(function* () {const current = (yield* Ref.get(activeTurns)).get(input.nativeTurnId);if (current !== input.context) {@@ -5822,31 +5870,33 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (});if (providerRetry !== undefined &&- (input.status !== "failed" || input.context.subagent !== null)+ !input.continueRun &&+ (providerRetry.context !== undefined ||+ input.status !== "failed" ||+ input.context.subagent !== null)) {yield* emitProviderEvent({type: "turn_item.updated",driver: CODEX_PROVIDER,- turnItem: makeProviderRetryTurnItem({- driver: CODEX_PROVIDER,- threadId: input.context.projectionThreadId,- runId: input.context.projectionRunId,- nodeId: input.context.providerNodeId,- providerThreadId: input.context.providerThread.id,- providerTurnId: input.context.providerTurnId,- itemOrdinal: providerRetry.itemOrdinal,- failure: providerRetry.failure,- retry: providerRetry.retry,- status: providerTurnStatusToTerminal(input.status),- startedAt: providerRetry.startedAt,- updatedAt: input.completedAt,- }),+ turnItem: codexRetryTurnItem(+ input.context,+ providerRetry,+ input.status === "failed" &&+ providerRetry.context !== input.context &&+ providerRetry.context !== undefined+ ? "interrupted"+ : providerTurnStatusToTerminal(input.status),+ input.completedAt,+ ),});}- if (input.status !== "completed") {+ if (input.status !== "completed" && !input.continueRun) {yield* Ref.update(terminalizedNonCompletedNativeTurns, (current) => {const updated = new Set(current);- updated.add(input.nativeTurnId);+ for (const turn of continuedRuns.get(input.context.providerTurnId) ?? [+ input.context,+ ])+ updated.add(turn.nativeTurnId);return updated;});}@@ -5967,11 +6017,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (// Only direct children wake an idle root turn; grandchildren report to their// own parent, and running root turns receive results natively.const parent = input.context.subagent.parentContext;+ const parentNativeThreadId = parent.providerThread.nativeThreadRef?.nativeId;if (continuationRequests !== undefined &&parent.subagent === null &&(input.status === "completed" || input.status === "failed") &&!(yield* Ref.get(terminalizedNonCompletedNativeTurns)).has(parent.nativeTurnId) &&+ (parentNativeThreadId == null ||+ !capacityContinuations.has(parentNativeThreadId)) &&![...(yield* Ref.get(activeTurns)).values()].some((active) =>active.subagent === null &&@@ -6017,7 +6070,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (input.completedAt,input.status === "interrupted" || input.status === "failed",);- if (input.context.subagent === null) {+ if (input.context.subagent === null && !input.continueRun) {yield* emitOrDeferRootTerminal({...input,...(providerRetry === undefined ? {} : { providerRetry }),@@ -6051,6 +6104,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (if (input.context.nativeStartReady !== undefined) {yield* Deferred.succeed(input.context.nativeStartReady, undefined);}+ if (!input.continueRun && input.context.capacityRetryPending !== undefined) {+ yield* Deferred.succeed(input.context.capacityRetryPending, undefined);+ }yield* flushReadyRootTerminals();if (!retainSettledContext && !interruptInProgress) {yield* Ref.update(runningCommandItemsByTurn, (current) => {@@ -6092,6 +6148,141 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (}),);+ // Codex can end an overloaded turn without retry advice. Continue its+ // persisted history with empty input instead of resending the user message.+ const retryCapacityFailure = Effect.fn("CodexAdapterV2.retryCapacityFailure")(function* (+ terminal: Parameters<typeof finalizeCodexTurn>[0],+ ) {+ const context = terminal.context;+ const code =+ terminal.failureErrorInfo == null+ ? context.latestProviderFailure?.nativeCode+ : codexErrorInfoCode(terminal.failureErrorInfo);+ if (+ terminal.status !== "failed" ||+ code !== "serverOverloaded" ||+ context.subagent !== null ||+ (context.input.message.attachments.length === 0 &&+ context.input.message.text.trim().toLowerCase() === "/compact")+ )+ return false;++ return yield* turnTerminalizationPermit.withPermits(1)(+ Effect.gen(function* () {+ if ((yield* Ref.get(activeTurns)).get(terminal.nativeTurnId) !== context) return true;+ if (context.capacityRetryPending !== undefined) return true;+ if (+ (context.capacityRetryAttempt ?? 0) >= CODEX_CAPACITY_MAX_RETRIES ||+ (yield* Ref.get(interruptingNativeTurns)).has(terminal.nativeTurnId)+ )+ return false;++ const attempt = (context.capacityRetryAttempt ?? 0) + 1;+ context.capacityRetryAttempt = attempt;+ const pending = yield* Deferred.make<ActiveCodexTurnContext | undefined>();+ context.capacityRetryPending = pending;+ const retryDelayMs = Math.min(2_000 * 2 ** (attempt - 1), 30_000);+ const now = yield* DateTime.now;+ const previous = (yield* Ref.get(providerRetries)).get(context.providerTurnId);+ const state: ActiveCodexProviderRetry = {+ context: previous?.context ?? context,+ nativeMessage:+ terminal.failureMessage ??+ context.latestProviderFailure?.nativeMessage ??+ "Selected model is at capacity.",+ nativeCode: code,+ failure: makeProviderFailure({+ message:+ context.latestProviderFailure?.failure.message ?? terminal.failureMessage,+ code,+ class: "provider_error",+ retryable: true,+ }),+ retry: { attempt, maxAttempts: CODEX_CAPACITY_MAX_RETRIES, retryDelayMs },+ itemOrdinal:+ previous?.itemOrdinal ??+ (yield* resolveItemOrdinal(+ context,+ `terminal-failure:${context.providerTurnId}`,+ )),+ startedAt: previous?.startedAt ?? now,+ };+ yield* Ref.update(providerRetries, (current) =>+ new Map(current).set(context.providerTurnId, state),+ );+ yield* emitProviderEvent({+ type: "turn_item.updated",+ driver: CODEX_PROVIDER,+ turnItem: codexRetryTurnItem(context, state, "running", now),+ });+ yield* Effect.sleep(retryDelayMs).pipe(+ Effect.andThen(+ Effect.gen(function* () {+ const nativeThreadId = yield* getNativeThreadId(context.providerThread);+ const claimed = yield* turnTerminalizationPermit.withPermits(1)(+ Effect.gen(function* () {+ if (+ (yield* Ref.get(activeTurns)).get(terminal.nativeTurnId) !== context ||+ goalStops.has(nativeThreadId) ||+ (yield* Ref.get(interruptingNativeTurns)).has(terminal.nativeTurnId)+ )+ return false;+ capacityContinuations.set(nativeThreadId, { context, retry: state });+ yield* finalizeCodexTurn({ ...terminal, continueRun: true });+ return true;+ }),+ );+ if (!claimed) return;+ // Notifications before the RPC response also take the terminalization permit.+ yield* startNativeTurn(+ {+ ...context.input,+ providerTurnOrdinal: context.providerTurnOrdinal + 1,+ },+ [],+ ).pipe(+ Effect.catch((cause) =>+ Effect.gen(function* () {+ if (yield* Deferred.isDone(pending)) return;+ yield* Effect.logWarning(+ "orchestration-v2.codex-capacity-retry-start-failed",+ {+ providerTurnId: context.providerTurnId,+ cause,+ },+ );+ yield* emitProviderEvent({+ type: "turn_item.updated",+ driver: CODEX_PROVIDER,+ turnItem: codexRetryTurnItem(+ context,+ state,+ "failed",+ yield* DateTime.now,+ ),+ });+ yield* Deferred.succeed(pending, undefined);+ yield* turnTerminalizationPermit.withPermits(1)(+ emitOrDeferRootTerminal({ ...terminal, providerRetry: state }),+ );+ }),+ ),+ Effect.ensuring(+ Effect.sync(() => {+ if (capacityContinuations.get(nativeThreadId)?.context === context)+ capacityContinuations.delete(nativeThreadId);+ }),+ ),+ );+ }),+ ),+ Effect.forkIn(scope, { startImmediately: true }),+ );+ return true;+ }),+ );+ });+yield* client.handleServerNotification("turn/completed", (payload) =>Effect.gen(function* () {const context = (yield* Ref.get(activeTurns)).get(payload.turn.id);@@ -6104,7 +6295,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ((yield* Ref.get(interruptingNativeTurns)).has(payload.turn.id)? "interrupted": nativeStatus;- yield* finalizeCodexTurn({+ const terminal = {context,nativeTurnId: payload.turn.id,status,@@ -6117,7 +6308,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (? {}: { failureErrorInfo: payload.turn.error.codexErrorInfo }),}),- });+ };+ if (!(yield* retryCapacityFailure(terminal))) yield* finalizeCodexTurn(terminal);}),);@@ -6231,10 +6423,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (goalStops.set(goalThreadId, (goalStops.get(goalThreadId) ?? 0) + 1);return yield* Effect.gen(function* () {// The run can settle while the pause is in flight, which drops its record.- const requestedTurnId = latestGoalTurnId(turnInput.providerTurnId);+ const requestedTurnId = latestRunTurnId(turnInput.providerTurnId);const heldBefore = goalHolds.get(goalThreadId)?.context;const turnsBefore = [- ...(goalRuns.get(requestedTurnId) ?? []),+ ...(continuedRuns.get(requestedTurnId) ?? []),...Array.from((yield* Ref.get(activeTurns)).values()).filter((context) => context.providerTurnId === requestedTurnId,),@@ -6258,13 +6450,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (}return yield* turnTerminalizationPermit.withPermits(1)(Effect.gen(function* () {- const providerTurnId = latestGoalTurnId(turnInput.providerTurnId);+ const providerTurnId = latestRunTurnId(turnInput.providerTurnId);const hold = goalHolds.get(goalThreadId);const held = hold !== undefined && hold.context.providerTurnId === providerTurnId;const goalTurns: ReadonlyArray<ActiveCodexTurnContext> = Array.from(new Set([...turnsBefore,- ...(goalRuns.get(providerTurnId) ?? []),+ ...(continuedRuns.get(providerTurnId) ?? []),...(held ? [hold.context] : []),]),);@@ -6319,6 +6511,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (return next;});const started = yield* client.request("turn/start", turnStartParams);+ // A turn/started notification may have registered and completed this turn already.+ if ((yield* Ref.get(pendingRootTurns)).get(threadId) !== turnInput) return;yield* registerRootTurn({turnInput,nativeTurnId: started.turn.id,@@ -6331,6 +6525,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (Effect.ensuring(Effect.flatMap(getNativeThreadId(turnInput.providerThread), (threadId) =>Ref.update(pendingRootTurns, (current) => {+ if (current.get(threadId) !== turnInput) return current;const updated = new Map(current);updated.delete(threadId);return updated;@@ -6764,15 +6959,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (steerTurn: Effect.fn("CodexAdapterV2.steerTurn")(function* (turnInput) {const threadId = yield* getNativeThreadId(turnInput.providerThread);- const latestTurnId = latestGoalTurnId(turnInput.providerTurnId);+ const latestTurnId = latestRunTurnId(turnInput.providerTurnId);const held = heldGoalTurn(turnInput.providerThread, latestTurnId);const providerTurnId =held === undefined? latestTurnId: ((yield* Deferred.await(held.hold.next))?.providerTurnId ?? latestTurnId);- const activeTurn = Array.from((yield* Ref.get(activeTurns)).values()).find(+ let activeTurn = Array.from((yield* Ref.get(activeTurns)).values()).find((candidate) => candidate.providerTurnId === providerTurnId,);+ const startingRetry = capacityContinuations.get(threadId)?.context;+ if (activeTurn === undefined && startingRetry?.providerTurnId === providerTurnId) {+ activeTurn = startingRetry;+ }+ while (activeTurn?.capacityRetryPending !== undefined) {+ activeTurn = yield* Deferred.await(activeTurn.capacityRetryPending);+ }if (activeTurn === undefined) {return yield* toProtocolError(`Provider turn ${turnInput.providerTurnId} is not active and cannot be steered.`,@@ -6822,6 +7024,24 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (),interruptTurn: Effect.fn("CodexAdapterV2.interruptTurn")(function* (requestedInput) {+ const nativeThreadId = requestedInput.providerThread.nativeThreadRef?.nativeId;+ const capacityRetry =+ nativeThreadId == null+ ? undefined+ : capacityContinuations.get(nativeThreadId)?.context;+ const startingRetry =+ capacityRetry?.providerTurnId === latestRunTurnId(requestedInput.providerTurnId)+ ? capacityRetry+ : undefined;+ if (startingRetry?.capacityRetryPending !== undefined) {+ const started = yield* Deferred.await(startingRetry.capacityRetryPending).pipe(+ Effect.timeoutOption("10 seconds"),+ );+ if (Option.isNone(started))+ return yield* toProtocolError(+ "Codex did not start the capacity retry within 10 seconds; Stop could not be delivered.",+ );+ }const { turnInput, goalTurns } = yield* resolveGoalStopTarget(requestedInput);const [activeTurnContexts, settledTurnContexts] =yield* turnTerminalizationPermit.withPermits(1)(@@ -6842,7 +7062,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (): undefined);const lineageRoots = Array.from(- new Set([...(activeTurn === undefined ? [] : [activeTurn]), ...goalTurns]),+ new Set([+ ...(activeTurn === undefined ? [] : [activeTurn]),+ ...(startingRetry === undefined ? [] : [startingRetry]),+ ...goalTurns,+ ]),);const inLineage = (context: ActiveCodexTurnContext) =>lineageRoots.some(@@ -7053,6 +7277,15 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (yield* Effect.gen(function* () {for (const target of interruptTargets) {const context = target.context;+ if (context.capacityRetryPending !== undefined) {+ yield* finalizeCodexTurn({+ context,+ nativeTurnId: context.nativeTurnId,+ status: "interrupted",+ completedAt: yield* DateTime.now,+ });+ continue;+ }// Null start timestamps ACK queued turns; interruption must wait for turn/started.if (context.nativeStartReady !== undefined) {const ready = yield* Deferred.await(context.nativeStartReady).pipe(@@ -7193,7 +7426,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ();},(effect, requestedInput) =>- effect.pipe(+ Effect.acquireUseRelease(+ Effect.sync(() => {+ const nativeThreadId = requestedInput.providerThread.nativeThreadRef?.nativeId;+ if (nativeThreadId != null)+ goalStops.set(nativeThreadId, (goalStops.get(nativeThreadId) ?? 0) + 1);+ return nativeThreadId;+ }),+ () => effect,+ (nativeThreadId) =>+ Effect.sync(() => {+ if (nativeThreadId == null) return;+ const stops = (goalStops.get(nativeThreadId) ?? 1) - 1;+ if (stops > 0) goalStops.set(nativeThreadId, stops);+ else goalStops.delete(nativeThreadId);+ }),+ ).pipe(Effect.mapError((cause) =>new ProviderAdapter.ProviderAdapterInterruptError({diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.tsindex 3a5bc04ecba..e1dbac21bc5 100644--- a/apps/server/src/orchestration-v2/ProviderTurnControlService.ts+++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.ts@@ -127,7 +127,11 @@ export const layer: Layer.Layer<const interruptProviderThread = targetsRecordedSession? providerThread: { ...providerThread, providerSessionId: input.providerSessionId };- if (providerTurn.status !== "running") {+ const continuingRun =+ input.operation === "steer" &&+ context.run?.status === "running" &&+ context.run.activeAttemptId === providerTurn.runAttemptId;+ if (providerTurn.status !== "running" && !continuingRun) {if (input.operation === "steer") {return yield* new ProviderTurnControlError({threadId: input.threadId,diff --git a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.tsindex 4c2d6712c5a..7481dcaa41b 100644--- a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts+++ b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts@@ -42,6 +42,7 @@ it.effect.each([true, "without native steering"],[true, "with interrupting native steering"],[true, "settled only"],+ [false, "during continuation"],] as const).map(([mailbox, timing]) => ({mailbox,@@ -135,7 +136,7 @@ it.effect.each(steerTurn: (turn) =>Effect.gen(function* () {steerCalls += 1;- if (timing === "after delivery") return;+ if (timing === "after delivery" || timing === "during continuation") return;yield* Deferred.succeed(steerEntered, undefined);yield* Deferred.await(rejectSteer);return yield* new ProviderAdapter.ProviderAdapterSteerRunError({@@ -284,6 +285,29 @@ it.effect.each(: {}),});if (timing !== "before dispatch") yield* dispatchSteer;+ if (timing === "during continuation") {+ const projection = yield* orchestrator.getThreadProjection(threadId);+ const turn = projection.providerTurns[0]!;+ const failed = yield* watch(+ (event) => event.type === "provider-turn.updated" && event.payload.status === "failed",+ );+ yield* Queue.offer(events, {+ type: "provider_turn.updated",+ driver,+ providerTurn: { ...turn, status: "failed", completedAt: yield* DateTime.now },+ });+ yield* Fiber.join(failed);+ yield* worker.drain();+ assert.equal(steerCalls, 1);+ assert.lengthOf(started, 1);+ const continued = yield* orchestrator.getThreadProjection(threadId);+ assert.equal(continued.runs[0]?.status, "running");+ assert.equal(+ continued.messages.find((message) => message.id === messageId)?.runId,+ first.runId,+ );+ return;+ }if (timing === "after delivery") yield* worker.drain();const delivery =timing === "during delivery" ? yield* worker.runOnce.pipe(Effect.forkScoped) : null;