TypeScript
fix(server): reject Codex turns owned by an earlier run (#25224)
maria54d0a161bb20
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.tsindex 8efdc1b468b..1f39dee9a10 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts@@ -1268,6 +1268,105 @@ describe("CodexAdapterV2 post-settle continuation", () => {};});+ it.effect.each(["before", "after"] as const)(+ "rejects a previous run's native turn when it completes %s the new start response",+ (completionOrder) =>+ Effect.gen(function* () {+ const nativeThreadId = "reused-turn-thread";+ const nativeTurnId = "reused-turn";+ const preamble = codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "work" });+ const retry = codexReplayPreamble({+ nativeThreadId,+ nativeTurnId: "retry-turn",+ prompt: "work",+ });+ const completion: CodexReplay.CodexAppServerReplayEntry = {+ type: "emit_inbound",+ label: "original turn completed",+ frame: {+ method: "turn/completed",+ params: {+ threadId: nativeThreadId,+ turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),+ },+ },+ };+ const harness = yield* makeCodexReplayHarness(+ makeCodexReplayTranscript({+ scenario: `reused-turn-${completionOrder}`,+ entries: [+ ...preamble,+ withReplayRequestId(preamble[5]!, 4),+ ...(completionOrder === "before" ? [completion] : []),+ withReplayRequestId(preamble[6]!, 4),+ ...(completionOrder === "after" ? [completion] : []),+ ...retry.slice(5, 7).map((entry) => withReplayRequestId(entry, 5)),+ {+ type: "emit_inbound",+ label: "retry completed",+ frame: {+ method: "turn/completed",+ params: {+ threadId: nativeThreadId,+ turn: makeCodexReplayTurn({ id: "retry-turn", status: "completed" }),+ },+ },+ },+ ],+ }),+ );+ const first = makeCodexTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now: yield* DateTime.now,+ attemptId: RunAttemptId.make("original"),+ text: "work",+ });+ yield* harness.runtime.startTurn(first);+ const result = yield* harness.runtime+ .startTurn({+ ...first,+ runId: RunId.make("replacement"),+ attemptId: RunAttemptId.make("replacement"),+ rootNodeId: NodeId.make("replacement"),+ runOrdinal: 2,+ providerTurnOrdinal: 2,+ })+ .pipe(Effect.result);+ assert.equal(result._tag, "Failure", "a different run must not adopt the original turn");+ if (result._tag !== "Failure") return;+ const failure = result.failure;+ assert.equal(failure._tag, "ProviderAdapterTurnStartError");+ assert.instanceOf(failure.cause, ProviderAdapter.ProviderAdapterProtocolError);+ yield* harness.firstTerminal;+ assert.deepEqual(+ harness.terminalEvents().map((event) => event.runOrdinal),+ [1],+ );+ yield* harness.runtime.startTurn({+ ...first,+ runId: RunId.make("retry"),+ attemptId: RunAttemptId.make("retry"),+ rootNodeId: NodeId.make("retry"),+ runOrdinal: 3,+ providerTurnOrdinal: 3,+ });+ yield* harness.awaitTerminal(2);+ assert.deepEqual(+ harness.terminalEvents().map((event) => [event.runOrdinal, event.status]),+ [+ [1, "completed"],+ [3, "completed"],+ ],+ );+ }).pipe(+ Effect.scoped,+ Effect.provide(+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),+ ),+ ),+ );+it.effect.each(["supported", "unsupported", "invalid"] as const)("delivers native history with %s app-server protocol",(response) =>diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.tsindex 37426780466..d637851fb38 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts@@ -6650,7 +6650,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (else next.delete(threadId);return next;});+ // Completion can remove the old context while turn/start is in flight.+ const previousTurns = new Map([+ ...(yield* Ref.get(settledTurns)),+ ...(yield* Ref.get(activeTurns)),+ ]);const started = yield* client.request("turn/start", turnStartParams);+ const previousTurn =+ previousTurns.get(started.turn.id) ??+ (yield* Ref.get(activeTurns)).get(started.turn.id);+ if (+ previousTurn !== undefined &&+ previousTurn.input.attemptId !== turnInput.attemptId+ ) {+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({+ driver: CODEX_PROVIDER,+ detail:+ "Codex returned a turn belonging to an earlier run. Stop the existing turn before retrying this message.",+ });+ }// A turn/started notification may have registered and completed this turn already.if ((yield* Ref.get(pendingRootTurns)).get(threadId) !== turnInput) return;yield* registerRootTurn({