The best way to control Claude Code, Codex and more from any computer or phone
fix(server): Codex turns that auto-compact end instead of looping on restored context (#25911)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 11, 2026 at 12:01 UTC
8b21cbdf1e2dc29b01e952fd1b700f091ce1b884
2 files changed
+60
-17
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+44
-12
@@ -1239,6 +1239,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1239
beforeEmitInbound?: CodexReplay.CodexAppServerReplayDriver["beforeEmitInbound"],
1240
onUsageLimits?: CodexAdapterV2.CodexAdapterV2Options["onUsageLimits"],
1241
promptSuggestions: Effect.Effect<boolean> = Effect.succeed(false),
1242
+ onNotification: (method: string) => Effect.Effect<void> = () => Effect.void,
1243
) =>
1244
Effect.gen(function* () {
1245
const fileSystem = yield* FileSystem.FileSystem;
@@ -1277,6 +1278,10 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1278
onRequest(method, params).pipe(
1279
Effect.andThen(client.request(method, params)),
1280
),
1281
+ handleServerNotification: (method, handler) =>
1282
+ client.handleServerNotification(method, (payload) =>
1283
+ onNotification(method).pipe(Effect.andThen(handler(payload))),
1284
+ ),
1285
raw: {
1286
...client.raw,
1287
request: (method, params) =>
@@ -2509,6 +2514,17 @@ describe("CodexAdapterV2 post-settle continuation", () => {
2514
},
2515
},
2516
},
2517
+ {
2518
+ type: "emit_inbound",
2519
+ label: "done",
2520
+ frame: {
2521
+ method: "turn/completed",
2522
+ params: {
2523
+ threadId: nativeThreadId,
2524
+ turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
2525
+ },
2526
+ },
2527
+ },
2528
{
2529
type: "expect_outbound",
2530
label: "restore context",
@@ -2526,20 +2542,30 @@ describe("CodexAdapterV2 post-settle continuation", () => {
2542
},
2543
},
2544
{ type: "emit_inbound", label: "restored", frame: { id: 4, result: {} } },
2529
- {
2530
- type: "emit_inbound",
2531
- label: "done",
2532
- frame: {
2533
- method: "turn/completed",
2534
- params: {
2535
- threadId: nativeThreadId,
2536
- turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
2537
- },
2538
- },
2539
- },
2545
],
2546
});
2542
- const harness = yield* makeCodexReplayHarness(transcript);
2547
+ // Codex queues items injected mid-turn as pending input and samples again.
2548
+ const wireOrder: Array<string> = [];
2549
+ const restoreRequested = yield* Deferred.make<void>();
2550
+ const harness = yield* makeCodexReplayHarness(
2551
+ transcript,
2552
+ undefined,
2553
+ (method) =>
2554
+ method === "thread/inject_items"
2555
+ ? Effect.sync(() => wireOrder.push(method)).pipe(
2556
+ Effect.andThen(Deferred.succeed(restoreRequested, undefined)),
2557
+ )
2558
+ : Effect.void,
2559
+ undefined,
2560
+ undefined,
2561
+ undefined,
2562
+ undefined,
2563
+ (method) =>
2564
+ Effect.sync(() => {
2565
+ if (method === "item/completed" || method === "turn/completed")
2566
+ wireOrder.push(method);
2567
+ }),
2568
+ );
2569
yield* (yield* McpProviderSessions.McpProviderSessions).set({
2570
environmentId: EnvironmentId.make("test"),
2571
threadId: harness.threadId,
@@ -2559,7 +2585,13 @@ describe("CodexAdapterV2 post-settle continuation", () => {
2585
}),
2586
);
2587
yield* harness.firstTerminal;
2588
+ yield* Deferred.await(restoreRequested);
2589
assert.equal(harness.terminalEvents()[0]?.status, "completed");
2590
+ assert.deepEqual(
2591
+ wireOrder,
2592
+ ["item/completed", "turn/completed", "thread/inject_items"],
2593
+ "restored T3 context while the compacting native turn was still running",
2594
+ );
2595
}).pipe(
2596
Effect.provide(
2597
Layer.mergeAll(
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+16
-5
@@ -1782,8 +1782,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1782
NonNullable<CodexTurnStartParamsWithCollaborationMode["additionalContext"]>
1783
>(),
1784
);
1785
- // Codex drops client developer messages during compaction but only
1786
- // resends additionalContext when it changes.
1785
+ const threadsAwaitingContextRestore = yield* Ref.make(new Set<string>());
1786
+ // Codex drops client developer messages during compaction but only resends additionalContext
1787
+ // when it changes. Restore after turn/completed: items injected mid-turn make the turn sample again.
1788
const restoreAdditionalContext = Effect.fn("CodexAdapterV2.restoreAdditionalContext")(
1789
function* (threadId: string) {
1790
const context = (yield* Ref.get(additionalContextByThread)).get(threadId);
@@ -5148,9 +5149,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5149
yield* client.handleServerNotification("item/completed", (payload) =>
5150
Effect.gen(function* () {
5151
if (payload.item.type === "contextCompaction")
5151
- // Fork requests off the notification reader so it can read their responses.
5152
- yield* restoreAdditionalContext(payload.threadId).pipe(
5153
- Effect.forkIn(scope, { startImmediately: true }),
5152
+ yield* Ref.update(threadsAwaitingContextRestore, (current) =>
5153
+ new Set(current).add(payload.threadId),
5154
);
5155
if (
5156
(payload.item.type === "subAgentActivity" ||
@@ -6847,6 +6847,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6847
6848
yield* client.handleServerNotification("turn/completed", (payload) =>
6849
Effect.gen(function* () {
6850
+ const compacted = yield* Ref.modify(threadsAwaitingContextRestore, (current) => {
6851
+ if (!current.has(payload.threadId)) return [false, current] as const;
6852
+ const next = new Set(current);
6853
+ next.delete(payload.threadId);
6854
+ return [true, next] as const;
6855
+ });
6856
+ if (compacted)
6857
+ // Fork requests off the notification reader so it can read their responses.
6858
+ yield* restoreAdditionalContext(payload.threadId).pipe(
6859
+ Effect.forkIn(scope, { startImmediately: true }),
6860
+ );
6861
const context = (yield* Ref.get(activeTurns)).get(payload.turn.id);
6862
if (context === undefined) {
6863
return;