@devries-cameron20 / t3code-1 / commits / 54d0a161bb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): reject Codex turns owned by an earlier run (#25224)

Co-authored-by: Yukun Shan <92423096+nateEc@users.noreply.github.com>

maria committed Oct 30, 2026 at 21:12 UTC 54d0a161bb20db5d2ccb50f7dad2b4d1bbf05e97
2 files changed +117
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+99
@@ -1268,6 +1268,105 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1268 };
1269 });
1270
1271 + it.effect.each(["before", "after"] as const)(
1272 + "rejects a previous run's native turn when it completes %s the new start response",
1273 + (completionOrder) =>
1274 + Effect.gen(function* () {
1275 + const nativeThreadId = "reused-turn-thread";
1276 + const nativeTurnId = "reused-turn";
1277 + const preamble = codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "work" });
1278 + const retry = codexReplayPreamble({
1279 + nativeThreadId,
1280 + nativeTurnId: "retry-turn",
1281 + prompt: "work",
1282 + });
1283 + const completion: CodexReplay.CodexAppServerReplayEntry = {
1284 + type: "emit_inbound",
1285 + label: "original turn completed",
1286 + frame: {
1287 + method: "turn/completed",
1288 + params: {
1289 + threadId: nativeThreadId,
1290 + turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
1291 + },
1292 + },
1293 + };
1294 + const harness = yield* makeCodexReplayHarness(
1295 + makeCodexReplayTranscript({
1296 + scenario: `reused-turn-${completionOrder}`,
1297 + entries: [
1298 + ...preamble,
1299 + withReplayRequestId(preamble[5]!, 4),
1300 + ...(completionOrder === "before" ? [completion] : []),
1301 + withReplayRequestId(preamble[6]!, 4),
1302 + ...(completionOrder === "after" ? [completion] : []),
1303 + ...retry.slice(5, 7).map((entry) => withReplayRequestId(entry, 5)),
1304 + {
1305 + type: "emit_inbound",
1306 + label: "retry completed",
1307 + frame: {
1308 + method: "turn/completed",
1309 + params: {
1310 + threadId: nativeThreadId,
1311 + turn: makeCodexReplayTurn({ id: "retry-turn", status: "completed" }),
1312 + },
1313 + },
1314 + },
1315 + ],
1316 + }),
1317 + );
1318 + const first = makeCodexTestTurnInput({
1319 + threadId: harness.threadId,
1320 + providerThread: harness.providerThread,
1321 + now: yield* DateTime.now,
1322 + attemptId: RunAttemptId.make("original"),
1323 + text: "work",
1324 + });
1325 + yield* harness.runtime.startTurn(first);
1326 + const result = yield* harness.runtime
1327 + .startTurn({
1328 + ...first,
1329 + runId: RunId.make("replacement"),
1330 + attemptId: RunAttemptId.make("replacement"),
1331 + rootNodeId: NodeId.make("replacement"),
1332 + runOrdinal: 2,
1333 + providerTurnOrdinal: 2,
1334 + })
1335 + .pipe(Effect.result);
1336 + assert.equal(result._tag, "Failure", "a different run must not adopt the original turn");
1337 + if (result._tag !== "Failure") return;
1338 + const failure = result.failure;
1339 + assert.equal(failure._tag, "ProviderAdapterTurnStartError");
1340 + assert.instanceOf(failure.cause, ProviderAdapter.ProviderAdapterProtocolError);
1341 + yield* harness.firstTerminal;
1342 + assert.deepEqual(
1343 + harness.terminalEvents().map((event) => event.runOrdinal),
1344 + [1],
1345 + );
1346 + yield* harness.runtime.startTurn({
1347 + ...first,
1348 + runId: RunId.make("retry"),
1349 + attemptId: RunAttemptId.make("retry"),
1350 + rootNodeId: NodeId.make("retry"),
1351 + runOrdinal: 3,
1352 + providerTurnOrdinal: 3,
1353 + });
1354 + yield* harness.awaitTerminal(2);
1355 + assert.deepEqual(
1356 + harness.terminalEvents().map((event) => [event.runOrdinal, event.status]),
1357 + [
1358 + [1, "completed"],
1359 + [3, "completed"],
1360 + ],
1361 + );
1362 + }).pipe(
1363 + Effect.scoped,
1364 + Effect.provide(
1365 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
1366 + ),
1367 + ),
1368 + );
1369 +
1370 it.effect.each(["supported", "unsupported", "invalid"] as const)(
1371 "delivers native history with %s app-server protocol",
1372 (response) =>
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+18
@@ -6650,7 +6650,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6650 else next.delete(threadId);
6651 return next;
6652 });
6653 + // Completion can remove the old context while turn/start is in flight.
6654 + const previousTurns = new Map([
6655 + ...(yield* Ref.get(settledTurns)),
6656 + ...(yield* Ref.get(activeTurns)),
6657 + ]);
6658 const started = yield* client.request("turn/start", turnStartParams);
6659 + const previousTurn =
6660 + previousTurns.get(started.turn.id) ??
6661 + (yield* Ref.get(activeTurns)).get(started.turn.id);
6662 + if (
6663 + previousTurn !== undefined &&
6664 + previousTurn.input.attemptId !== turnInput.attemptId
6665 + ) {
6666 + return yield* new ProviderAdapter.ProviderAdapterProtocolError({
6667 + driver: CODEX_PROVIDER,
6668 + detail:
6669 + "Codex returned a turn belonging to an earlier run. Stop the existing turn before retrying this message.",
6670 + });
6671 + }
6672 // A turn/started notification may have registered and completed this turn already.
6673 if ((yield* Ref.get(pendingRootTurns)).get(threadId) !== turnInput) return;
6674 yield* registerRootTurn({