diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index fd5f89b86a0c1..c7d9f85eac852 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -3824,6 +3824,185 @@ describe("CodexAdapterV2 post-settle continuation", () => { ), ); + 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 !== "interrupted" + ? { 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" }), + }, + }, + }, + ...(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", + frame: { + method: "turn/completed", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ + id: nativeTurnId, + status: startOutcome === "hook-stopped" ? "interrupted" : "completed", + }), + }, + }, + }, + ], + }); + }; + + 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* () { + 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", + }), + ); + 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] + : [], + ); + const hookStopped = startOutcome === "hook-stopped"; + assert.deepEqual( + items.map((entry) => entry.status), + hookStopped ? [] : ["running", "completed"], + ); + // 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) => ({ + 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( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + it.effect.each([ { itemType: "contextCompaction", outcome: "failed" }, { itemType: "contextCompaction", outcome: "interrupted" }, diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 4c164b86ab1e7..dde0107a5e521 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -381,6 +381,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 isCodexThreadNotFoundError(error: unknown): boolean { return ( isCodexAppServerRequestError(error) && @@ -2123,6 +2133,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>(); @@ -5061,7 +5078,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 @@ -5075,29 +5099,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); @@ -5158,6 +5163,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)), ); @@ -5354,6 +5433,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; @@ -5509,6 +5592,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), @@ -7306,6 +7393,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, turnTerminalizationPermit.withPermits(1)); + } const compacted = yield* Ref.modify(threadsAwaitingContextRestore, (current) => { if (!current.has(payload.threadId)) return [false, current] as const; const next = new Set(current); @@ -8039,17 +8132,25 @@ 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. + // 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.asVoid, + Effect.catchIf(isCompactionInterruptedError, () => Effect.void), Effect.onError(() => - Ref.update(pendingRootTurns, (current) => { + Ref.update(pendingCompactions, (current) => { const next = new Map(current); next.delete(threadId); return next; - }), + }).pipe( + Effect.andThen(releaseHeldTurnStarts(threadId)), + Effect.orDie, + turnTerminalizationPermit.withPermits(1), + ), ), ); },