From 7a35667b2eaffc6ddccc52084ca4c6a1685e267c Mon Sep 17 00:00:00 2001 From: Phon Date: Thu, 12 Nov 2026 22:13:55 +0530 Subject: [PATCH 1/4] fix(server): Codex /compact binds to its own turn, not a resumed turn that starts first --- .../Adapters/CodexAdapterV2.test.ts | 125 ++++++++++++++ .../Adapters/CodexAdapterV2.ts | 155 ++++++++++++++---- 2 files changed, 249 insertions(+), 31 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 6e5fd3516e0b1..b64efe45a9d52 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -2915,6 +2915,131 @@ describe("CodexAdapterV2 post-settle continuation", () => { ), ); + const compactionAfterResumedTurnTranscript = (startOutcome: "accepted" | "interrupted") => { + const nativeThreadId = "compact-thread"; + const nativeTurnId = "compact-turn"; + const resumedTurnId = "resumed-turn"; + const item = { type: "contextCompaction", id: "compact-item" }; + const startResponse = + startOutcome === "accepted" + ? { type: "emit_inbound" as const, label: "compact", frame: { id: 3, result: {} } } + : { + type: "emit_inbound" as const, + label: "compact", + frame: { + id: 3, + error: { code: -32_000, message: "Context compaction ended with interrupted." }, + }, + }; + return makeCodexReplayTranscript({ + scenario: `compaction-after-resumed-turn-${startOutcome}`, + entries: [ + ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "unused" }).slice(0, 5), + { + type: "expect_outbound", + label: "compact", + frame: { id: 3, method: "thread/compact/start", params: { threadId: nativeThreadId } }, + }, + { + type: "emit_inbound", + label: "start", + frame: { + method: "turn/started", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: resumedTurnId, status: "inProgress" }), + }, + }, + }, + { + type: "emit_inbound", + label: "complete", + frame: { + method: "turn/completed", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: resumedTurnId, status: "interrupted" }), + }, + }, + }, + startResponse, + { + type: "emit_inbound", + label: "start", + frame: { + method: "turn/started", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: nativeTurnId, status: "inProgress" }), + }, + }, + }, + ...(["item/started", "item/completed"] as const).map((method) => ({ + type: "emit_inbound" as const, + label: method, + frame: { method, params: { threadId: nativeThreadId, turnId: nativeTurnId, item } }, + })), + { + type: "emit_inbound", + label: "complete", + frame: { + method: "turn/completed", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }), + }, + }, + }, + ], + }); + }; + + it.effect.each(["accepted", "interrupted"] as const)( + "tracks the compaction item after an interrupted resumed turn when the start RPC is %s", + (startOutcome) => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeCodexReplayHarness( + compactionAfterResumedTurnTranscript(startOutcome), + ); + assert.isDefined(harness.runtime.compactThread); + yield* harness.runtime.compactThread!( + makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("compact-attempt"), + text: "/compact", + }), + ); + yield* harness.firstTerminal; + const items = harness.events.flatMap((event) => + event.type === "turn_item.updated" && event.turnItem.type === "compaction" + ? [event.turnItem] + : [], + ); + assert.deepEqual( + items.map((entry) => entry.status), + ["running", "completed"], + ); + // The resumed turn was never adopted: the only terminal is the compaction's. + assert.deepEqual( + harness.terminalEvents().map((event) => event.status), + ["completed"], + ); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + it.effect.each(["failed", "interrupted", "Stop"] as const)( "settles unfinished compaction on %s without changing completed compaction", (outcome) => diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 4e8fd1ac5f30b..c773d7dce21d0 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -372,6 +372,16 @@ function isCodexActiveWriterError(error: unknown): boolean { ); } +// Codex can report a compaction as interrupted when a different turn on the +// thread ends first, while the compaction itself still runs; its item decides. +function isCompactionInterruptedError(error: unknown): boolean { + return ( + isCodexAppServerRequestError(error) && + error.method === "thread/compact/start" && + error.errorMessage.includes("Context compaction ended with interrupted") + ); +} + function codexTimestamp(seconds: number | null | undefined): DateTime.Utc { return seconds === null || seconds === undefined ? DateTime.nowUnsafe() @@ -1942,6 +1952,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( const pendingRootTurns = yield* Ref.make( new Map(), ); + const pendingCompactions = yield* Ref.make( + new Map(), + ); + const heldTurnStarts = new Map< + string, + { readonly threadId: string; readonly turn: CodexSchema.V2TurnStartedNotification__Turn } + >(); const turnWaiters = yield* Ref.make(new Map>()); const subagentThreads = yield* Ref.make(new Map()); const subagentSelections = new Map>(); @@ -4856,29 +4873,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( }), ); - yield* client.handleServerNotification("turn/started", (payload) => - Effect.gen(function* () { - const context = (yield* Ref.get(activeTurns)).get(payload.turn.id); - if (context !== undefined) { - if (context.nativeStartReady !== undefined) { - yield* Deferred.succeed(context.nativeStartReady, undefined); - } - return; - } - const pendingRootTurn = (yield* Ref.get(pendingRootTurns)).get(payload.threadId); - if (pendingRootTurn !== undefined) { - yield* registerRootTurn({ - turnInput: pendingRootTurn, - nativeTurnId: payload.turn.id, - startedAt: codexTimestamp(payload.turn.startedAt), - }); - yield* Ref.update(pendingRootTurns, (current) => { - const updated = new Map(current); - updated.delete(payload.threadId); - return updated; - }); - return; - } + // The turn/started handling for a turn nobody claimed: goal continuations, + // orphan goal turns, and subagent bookkeeping. + const handleUnclaimedTurnStart = Effect.fn("CodexAdapterV2.handleUnclaimedTurnStart")( + function* (payload: CodexSchema.V2TurnStartedNotification) { const goalHold = goalHolds.get(payload.threadId); if (goalHold !== undefined) { goalHolds.delete(payload.threadId); @@ -4939,6 +4937,80 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( nativeTurnId: payload.turn.id, startedAt: codexTimestamp(payload.turn.startedAt), }); + }, + ); + + // A turn held for a pending compaction is classified by its first item: a + // contextCompaction item makes it the compaction run; anything else, or the + // turn ending itemless, releases it to the unclaimed-turn handling. + const classifyHeldTurnStart = Effect.fn("CodexAdapterV2.classifyHeldTurnStart")( + function* (input: { readonly turnId: string; readonly isCompactionItem: boolean }) { + const held = heldTurnStarts.get(input.turnId); + if (held === undefined) return; + heldTurnStarts.delete(input.turnId); + if (input.isCompactionItem) { + const pendingCompaction = (yield* Ref.get(pendingCompactions)).get(held.threadId); + if (pendingCompaction !== undefined) { + yield* Ref.update(pendingCompactions, (current) => { + const updated = new Map(current); + updated.delete(held.threadId); + return updated; + }); + yield* registerRootTurn({ + turnInput: pendingCompaction, + nativeTurnId: input.turnId, + startedAt: codexTimestamp(held.turn.startedAt), + }); + return; + } + } + yield* handleUnclaimedTurnStart({ threadId: held.threadId, turn: held.turn }); + }, + ); + + const releaseHeldTurnStarts = Effect.fn("CodexAdapterV2.releaseHeldTurnStarts")(function* ( + threadId: string, + ) { + for (const [turnId, held] of heldTurnStarts) { + if (held.threadId !== threadId) continue; + heldTurnStarts.delete(turnId); + yield* handleUnclaimedTurnStart({ threadId, turn: held.turn }); + } + }); + + yield* client.handleServerNotification("turn/started", (payload) => + Effect.gen(function* () { + const context = (yield* Ref.get(activeTurns)).get(payload.turn.id); + if (context !== undefined) { + if (context.nativeStartReady !== undefined) { + yield* Deferred.succeed(context.nativeStartReady, undefined); + } + return; + } + const pendingRootTurn = (yield* Ref.get(pendingRootTurns)).get(payload.threadId); + if (pendingRootTurn !== undefined) { + yield* registerRootTurn({ + turnInput: pendingRootTurn, + nativeTurnId: payload.turn.id, + startedAt: codexTimestamp(payload.turn.startedAt), + }); + yield* Ref.update(pendingRootTurns, (current) => { + const updated = new Map(current); + updated.delete(payload.threadId); + return updated; + }); + return; + } + // A pending compaction must not adopt whichever turn starts first; hold + // the turn until its first item says whether it is the compaction turn. + if ((yield* Ref.get(pendingCompactions)).has(payload.threadId)) { + heldTurnStarts.set(payload.turn.id, { + threadId: payload.threadId, + turn: payload.turn, + }); + return; + } + yield* handleUnclaimedTurnStart(payload); }).pipe(Effect.orDie, turnTerminalizationPermit.withPermits(1)), ); @@ -5118,6 +5190,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* client.handleServerNotification("item/started", (payload) => Effect.gen(function* () { + yield* classifyHeldTurnStart({ + turnId: payload.turnId, + isCompactionItem: payload.item.type === "contextCompaction", + }); const context = yield* awaitActiveTurn(payload.turnId); if (context === undefined) { return; @@ -5272,6 +5348,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* client.handleServerNotification("item/completed", (payload) => Effect.gen(function* () { + yield* classifyHeldTurnStart({ + turnId: payload.turnId, + isCompactionItem: payload.item.type === "contextCompaction", + }); if (payload.item.type === "contextCompaction") yield* Ref.update(threadsAwaitingContextRestore, (current) => new Set(current).add(payload.threadId), @@ -6998,6 +7078,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* client.handleServerNotification("turn/completed", (payload) => Effect.gen(function* () { + if (heldTurnStarts.has(payload.turn.id)) { + yield* classifyHeldTurnStart({ + turnId: payload.turn.id, + isCompactionItem: false, + }).pipe(Effect.orDie); + } const compacted = yield* Ref.modify(threadsAwaitingContextRestore, (current) => { if (!current.has(payload.threadId)) return [false, current] as const; const next = new Set(current); @@ -7661,17 +7747,24 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( compactThread: Effect.fn("CodexAdapterV2.compactThread")( function* (turnInput) { const threadId = yield* getNativeThreadId(turnInput.providerThread); - yield* Ref.update(pendingRootTurns, (current) => + yield* Ref.update(pendingCompactions, (current) => new Map(current).set(threadId, turnInput), ); - // Clear on interruption; a late turn/started must not be adopted after an interrupted start. + // Clear on failure; a late compaction item must not be adopted after + // a failed start, and held turns go back to normal. The interruption + // error is the exception: the compaction item still decides. yield* client.request("thread/compact/start", { threadId }).pipe( - Effect.onError(() => - Ref.update(pendingRootTurns, (current) => { - const next = new Map(current); - next.delete(threadId); - return next; - }), + Effect.catch((cause) => + isCompactionInterruptedError(cause) + ? Effect.void + : Ref.update(pendingCompactions, (current) => { + const next = new Map(current); + next.delete(threadId); + return next; + }).pipe( + Effect.andThen(releaseHeldTurnStarts(threadId)), + Effect.andThen(Effect.fail(cause)), + ), ), ); }, From 1722e78cf0a0e91ef8f30ebef885c7f88c24fb54 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 16:59:50 +0000 Subject: [PATCH 2/4] fix(server): keep compaction cleanup on interruption and hold the turn permit for released turns AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/CodexAdapterV2.ts | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index c773d7dce21d0..6cb53b0ae300a 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -7082,7 +7082,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* classifyHeldTurnStart({ turnId: payload.turn.id, isCompactionItem: false, - }).pipe(Effect.orDie); + }).pipe(Effect.orDie, turnTerminalizationPermit.withPermits(1)); } const compacted = yield* Ref.modify(threadsAwaitingContextRestore, (current) => { if (!current.has(payload.threadId)) return [false, current] as const; @@ -7750,21 +7750,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* Ref.update(pendingCompactions, (current) => new Map(current).set(threadId, turnInput), ); - // Clear on failure; a late compaction item must not be adopted after - // a failed start, and held turns go back to normal. The interruption - // error is the exception: the compaction item still decides. + // On failure or interruption, a late compaction item must not be adopted + // and held turns go back to normal. Codex's interrupted reply is not a + // failure: the compaction item still decides. yield* client.request("thread/compact/start", { threadId }).pipe( - Effect.catch((cause) => - isCompactionInterruptedError(cause) - ? Effect.void - : Ref.update(pendingCompactions, (current) => { - const next = new Map(current); - next.delete(threadId); - return next; - }).pipe( - Effect.andThen(releaseHeldTurnStarts(threadId)), - Effect.andThen(Effect.fail(cause)), - ), + Effect.asVoid, + Effect.catchIf(isCompactionInterruptedError, () => Effect.void), + Effect.onError(() => + Ref.update(pendingCompactions, (current) => { + const next = new Map(current); + next.delete(threadId); + return next; + }).pipe( + Effect.andThen(releaseHeldTurnStarts(threadId)), + Effect.orDie, + turnTerminalizationPermit.withPermits(1), + ), ), ); }, From b3ebd816683e183e61dd5013a34d7b2e54749daa Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 17:24:42 +0000 Subject: [PATCH 3/4] fix(server): a compaction stopped by its PreCompact hook still settles A held turn that reports a preCompact hook is the compaction's turn, even when the hook stops it before any contextCompaction item. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/CodexAdapterV2.test.ts | 57 +++++++++++++++---- .../Adapters/CodexAdapterV2.ts | 9 ++- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index b64efe45a9d52..53e3faf802f05 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -2915,13 +2915,15 @@ describe("CodexAdapterV2 post-settle continuation", () => { ), ); - const compactionAfterResumedTurnTranscript = (startOutcome: "accepted" | "interrupted") => { + const compactionAfterResumedTurnTranscript = ( + startOutcome: "accepted" | "interrupted" | "hook-stopped", + ) => { const nativeThreadId = "compact-thread"; const nativeTurnId = "compact-turn"; const resumedTurnId = "resumed-turn"; const item = { type: "contextCompaction", id: "compact-item" }; const startResponse = - startOutcome === "accepted" + startOutcome !== "interrupted" ? { type: "emit_inbound" as const, label: "compact", frame: { id: 3, result: {} } } : { type: "emit_inbound" as const, @@ -2974,11 +2976,38 @@ describe("CodexAdapterV2 post-settle continuation", () => { }, }, }, - ...(["item/started", "item/completed"] as const).map((method) => ({ - type: "emit_inbound" as const, - label: method, - frame: { method, params: { threadId: nativeThreadId, turnId: nativeTurnId, item } }, - })), + ...(startOutcome === "hook-stopped" + ? [ + { + type: "emit_inbound" as const, + label: "hook/completed", + frame: { + method: "hook/completed", + params: { + threadId: nativeThreadId, + turnId: nativeTurnId, + run: { + id: "compact-hook", + eventName: "preCompact", + source: "project", + sourcePath: "/workspace/.codex/hooks.json", + scope: "turn", + executionMode: "sync", + handlerType: "command", + displayOrder: 1, + startedAt: 1782622440, + status: "stopped", + entries: [{ kind: "stop", text: "Not now." }], + }, + }, + }, + }, + ] + : (["item/started", "item/completed"] as const).map((method) => ({ + type: "emit_inbound" as const, + label: method, + frame: { method, params: { threadId: nativeThreadId, turnId: nativeTurnId, item } }, + }))), { type: "emit_inbound", label: "complete", @@ -2986,7 +3015,10 @@ describe("CodexAdapterV2 post-settle continuation", () => { method: "turn/completed", params: { threadId: nativeThreadId, - turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }), + turn: makeCodexReplayTurn({ + id: nativeTurnId, + status: startOutcome === "hook-stopped" ? "interrupted" : "completed", + }), }, }, }, @@ -2994,8 +3026,8 @@ describe("CodexAdapterV2 post-settle continuation", () => { }); }; - it.effect.each(["accepted", "interrupted"] as const)( - "tracks the compaction item after an interrupted resumed turn when the start RPC is %s", + it.effect.each(["accepted", "interrupted", "hook-stopped"] as const)( + "binds the compaction turn after an interrupted resumed turn (%s)", (startOutcome) => Effect.scoped( Effect.gen(function* () { @@ -3018,14 +3050,15 @@ describe("CodexAdapterV2 post-settle continuation", () => { ? [event.turnItem] : [], ); + const hookStopped = startOutcome === "hook-stopped"; assert.deepEqual( items.map((entry) => entry.status), - ["running", "completed"], + hookStopped ? [] : ["running", "completed"], ); // The resumed turn was never adopted: the only terminal is the compaction's. assert.deepEqual( harness.terminalEvents().map((event) => event.status), - ["completed"], + [hookStopped ? "interrupted" : "completed"], ); }).pipe( Effect.provide( diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 6cb53b0ae300a..166d5e14c3109 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -4859,7 +4859,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* client.handleServerNotification("hook/completed", (payload) => Effect.gen(function* () { - if (payload.turnId == null || payload.run.status !== "stopped") return; + if (payload.turnId == null) return; + // Codex runs preCompact only for compaction, and can stop it before any item. + if (payload.run.eventName === "preCompact" && heldTurnStarts.has(payload.turnId)) + yield* classifyHeldTurnStart({ + turnId: payload.turnId, + isCompactionItem: true, + }).pipe(Effect.orDie, turnTerminalizationPermit.withPermits(1)); + if (payload.run.status !== "stopped") return; const context = (yield* Ref.get(activeTurns)).get(payload.turnId); if (context?.providerThread.nativeThreadRef?.nativeId !== payload.threadId) return; const reason = payload.run.entries From 2c2a2072346a25ad8b48eba9ee4a7a59c13a3d8c Mon Sep 17 00:00:00 2001 From: Phon Date: Thu, 19 Nov 2026 22:42:42 +0530 Subject: [PATCH 4/4] test(server): bind compaction test to the compaction turn's terminal The test waited on the first terminal event of any turn, so the hook-stopped row passed even with the original mis-binding. Wait for the terminal event belonging to the compaction turn itself and assert its identity and hook stop reason. --- .../Adapters/CodexAdapterV2.test.ts | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index bd2dbf3774d20..c7d9f85eac852 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -3953,7 +3953,17 @@ describe("CodexAdapterV2 post-settle continuation", () => { text: "/compact", }), ); - yield* harness.firstTerminal; + const compactProviderTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({ + driver: CodexAdapterV2.CODEX_DRIVER_KIND, + nativeTurnId: "compact-turn", + }); + // The resumed turn ends interrupted too, so the first terminal cannot + // tell the turns apart; wait for the compaction turn's own terminal. + yield* harness.until(() => + harness + .terminalEvents() + .some((event) => event.providerTurnId === compactProviderTurnId), + ); const items = harness.events.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction" ? [event.turnItem] @@ -3964,10 +3974,21 @@ describe("CodexAdapterV2 post-settle continuation", () => { items.map((entry) => entry.status), hookStopped ? [] : ["running", "completed"], ); - // The resumed turn was never adopted: the only terminal is the compaction's. + // The resumed turn was never adopted: the only terminal is the + // compaction's own, with the hook stop reason attached when stopped. assert.deepEqual( - harness.terminalEvents().map((event) => event.status), - [hookStopped ? "interrupted" : "completed"], + harness.terminalEvents().map((event) => ({ + providerTurnId: event.providerTurnId, + status: event.status, + reason: event.status === "failed" ? undefined : event.reason, + })), + [ + { + providerTurnId: compactProviderTurnId, + status: hookStopped ? "interrupted" : "completed", + reason: hookStopped ? "preCompact hook (project): Not now." : undefined, + }, + ], ); }).pipe( Effect.provide(