Skip to content

Commit e3f4334

Browse files
voltcrashclaude
andcommitted
fix(server): Codex /compact no longer adopts a goal turn that starts first
A manual compaction claimed whichever Codex turn started next on its thread. When Codex continued a goal before the compaction turn began, the compaction run tracked that goal turn, settled as interrupted when the compaction replaced it, and lost the real compaction turn. A pending compaction now claims the turn that reports its contextCompaction item. Goal continuations still join their held run first, and turns that start while a compaction is pending are not stopped as orphan goal turns. A failed or interrupted start still clears the pending entry. Fixes #18833 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 80664d5 commit e3f4334

2 files changed

Lines changed: 238 additions & 13 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,100 @@ describe("CodexAdapterV2 post-settle continuation", () => {
29592959
),
29602960
);
29612961

2962+
it.effect.each(["before", "after"] as const)(
2963+
"compacts the turn with the compaction item when another turn starts %s the start response",
2964+
(strayTurn) =>
2965+
Effect.scoped(
2966+
Effect.gen(function* () {
2967+
const nativeThreadId = `compact-stray-${strayTurn}`;
2968+
const item = { type: "contextCompaction", id: "compact-item" };
2969+
const notification = (label: string, method: string, params: unknown) => ({
2970+
type: "emit_inbound" as const,
2971+
label,
2972+
frame: { method, params: { threadId: nativeThreadId, ...(params as object) } },
2973+
});
2974+
const response = {
2975+
type: "emit_inbound" as const,
2976+
label: "compact",
2977+
frame: { id: 3, result: {} },
2978+
};
2979+
// Codex continues a goal on its own, then the compaction replaces it.
2980+
const stray = [
2981+
notification("stray start", "turn/started", {
2982+
turn: makeCodexReplayTurn({ id: "stray-turn", status: "inProgress" }),
2983+
}),
2984+
notification("stray end", "turn/completed", {
2985+
turn: makeCodexReplayTurn({ id: "stray-turn", status: "interrupted" }),
2986+
}),
2987+
];
2988+
const transcript = makeCodexReplayTranscript({
2989+
scenario: `native-compaction-stray-${strayTurn}`,
2990+
entries: [
2991+
...codexReplayPreamble({
2992+
nativeThreadId,
2993+
nativeTurnId: "unused",
2994+
prompt: "unused",
2995+
}).slice(0, 5),
2996+
{
2997+
type: "expect_outbound",
2998+
label: "compact",
2999+
frame: {
3000+
id: 3,
3001+
method: "thread/compact/start",
3002+
params: { threadId: nativeThreadId },
3003+
},
3004+
},
3005+
...(strayTurn === "before" ? [...stray, response] : [response, ...stray]),
3006+
notification("start", "turn/started", {
3007+
turn: makeCodexReplayTurn({ id: "compact-turn", status: "inProgress" }),
3008+
}),
3009+
notification("item/started", "item/started", { turnId: "compact-turn", item }),
3010+
notification("item/completed", "item/completed", { turnId: "compact-turn", item }),
3011+
notification("complete", "turn/completed", {
3012+
turn: makeCodexReplayTurn({ id: "compact-turn", status: "completed" }),
3013+
}),
3014+
],
3015+
});
3016+
const harness = yield* makeCodexReplayHarness(transcript);
3017+
yield* harness.runtime.compactThread!(
3018+
makeCodexTestTurnInput({
3019+
threadId: harness.threadId,
3020+
providerThread: harness.providerThread,
3021+
now: yield* DateTime.now,
3022+
attemptId: RunAttemptId.make("compact-attempt"),
3023+
text: "/compact",
3024+
}),
3025+
);
3026+
yield* harness.firstTerminal;
3027+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
3028+
assert.deepEqual(
3029+
harness.terminalEvents().map((event) => [event.providerTurnId, event.status]),
3030+
[
3031+
[
3032+
idAllocator.derive.providerTurn({
3033+
driver: CodexAdapterV2.CODEX_DRIVER_KIND,
3034+
nativeTurnId: "compact-turn",
3035+
}),
3036+
"completed",
3037+
],
3038+
],
3039+
);
3040+
assert.deepEqual(
3041+
harness.events.flatMap((event) =>
3042+
event.type === "turn_item.updated" && event.turnItem.type === "compaction"
3043+
? [event.turnItem.status]
3044+
: [],
3045+
),
3046+
["running", "completed"],
3047+
);
3048+
}).pipe(
3049+
Effect.provide(
3050+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3051+
),
3052+
),
3053+
),
3054+
);
3055+
29623056
it.effect("resumes a provider thread without requesting or decoding its history", () =>
29633057
Effect.scoped(
29643058
Effect.gen(function* () {
@@ -8474,6 +8568,125 @@ describe("CodexAdapterV2 post-settle continuation", () => {
84748568
),
84758569
);
84768570

8571+
it.effect("continues a held goal run while a compaction waits for its own turn", () =>
8572+
Effect.gen(function* () {
8573+
const item = { type: "contextCompaction", id: "compact-item" };
8574+
const transcript = makeCodexReplayTranscript({
8575+
scenario: "goal-continuation-during-compaction",
8576+
entries: [
8577+
...sessionStart,
8578+
...request(3, "thread/goal/get", { threadId: nativeThreadId }, { goal: null }),
8579+
...request(
8580+
4,
8581+
"thread/goal/set",
8582+
{ threadId: nativeThreadId, objective, status: "paused" },
8583+
{ goal: codexGoal("paused", 0) },
8584+
),
8585+
...withReplayRequestId(
8586+
codexReplayPreamble({
8587+
nativeThreadId,
8588+
nativeTurnId: "goal-turn-1",
8589+
prompt: objective,
8590+
}).slice(5),
8591+
5,
8592+
),
8593+
...request(
8594+
6,
8595+
"thread/goal/set",
8596+
{ threadId: nativeThreadId, status: "active" },
8597+
{ goal: codexGoal("active", 0) },
8598+
),
8599+
notification("goal active", "thread/goal/updated", {
8600+
threadId: nativeThreadId,
8601+
turnId: "goal-turn-1",
8602+
goal: codexGoal("active", 0),
8603+
}),
8604+
notification("first turn done", "turn/completed", {
8605+
threadId: nativeThreadId,
8606+
turn: makeCodexReplayTurn({ id: "goal-turn-1", status: "completed" }),
8607+
}),
8608+
...request(7, "thread/compact/start", { threadId: nativeThreadId }, {}),
8609+
notification("continuation", "turn/started", {
8610+
threadId: nativeThreadId,
8611+
turn: makeCodexReplayTurn({ id: "goal-turn-2", status: "inProgress" }),
8612+
}),
8613+
notification("continuation replaced", "turn/completed", {
8614+
threadId: nativeThreadId,
8615+
turn: makeCodexReplayTurn({ id: "goal-turn-2", status: "interrupted" }),
8616+
}),
8617+
// Otherwise the finished compaction waits for the next goal turn.
8618+
notification("goal paused", "thread/goal/updated", {
8619+
threadId: nativeThreadId,
8620+
goal: codexGoal("paused", 10),
8621+
}),
8622+
notification("compaction", "turn/started", {
8623+
threadId: nativeThreadId,
8624+
turn: makeCodexReplayTurn({ id: "compact-turn", status: "inProgress" }),
8625+
}),
8626+
...(["item/started", "item/completed"] as const).map((method) =>
8627+
notification(method, method, {
8628+
threadId: nativeThreadId,
8629+
turnId: "compact-turn",
8630+
item,
8631+
}),
8632+
),
8633+
notification("compaction done", "turn/completed", {
8634+
threadId: nativeThreadId,
8635+
turn: makeCodexReplayTurn({ id: "compact-turn", status: "completed" }),
8636+
}),
8637+
],
8638+
});
8639+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
8640+
const providerTurnId = (nativeTurnId: string) =>
8641+
idAllocator.derive.providerTurn({
8642+
driver: CodexAdapterV2.CODEX_DRIVER_KIND,
8643+
nativeTurnId,
8644+
});
8645+
const compacted = yield* Deferred.make<void>();
8646+
const harness = yield* makeCodexReplayHarness(transcript, (event) =>
8647+
event.type === "turn.terminal" && event.providerTurnId === providerTurnId("compact-turn")
8648+
? Deferred.succeed(compacted, undefined)
8649+
: Effect.void,
8650+
);
8651+
yield* harness.runtime.startTurn(yield* goalTurnInput(harness, `/goal ${objective}`));
8652+
yield* harness.runtime.compactThread!(
8653+
makeCodexTestTurnInput({
8654+
threadId: harness.threadId,
8655+
providerThread: harness.providerThread,
8656+
now: yield* DateTime.now,
8657+
attemptId: RunAttemptId.make("compact-attempt"),
8658+
text: "/compact",
8659+
}),
8660+
);
8661+
yield* Deferred.await(compacted);
8662+
8663+
assert.deepEqual(
8664+
harness.events.flatMap((event) =>
8665+
event.type === "provider_turn.updated" && event.providerTurn.status === "running"
8666+
? [[event.providerTurn.id, event.providerTurn.runAttemptId]]
8667+
: [],
8668+
),
8669+
[
8670+
[providerTurnId("goal-turn-1"), RunAttemptId.make("goal-attempt")],
8671+
[providerTurnId("goal-turn-2"), RunAttemptId.make("goal-attempt")],
8672+
[providerTurnId("compact-turn"), RunAttemptId.make("compact-attempt")],
8673+
],
8674+
);
8675+
assert.deepEqual(
8676+
harness.terminalEvents().map((event) => [event.providerTurnId, event.status]),
8677+
[
8678+
[providerTurnId("goal-turn-2"), "interrupted"],
8679+
[providerTurnId("compact-turn"), "completed"],
8680+
],
8681+
);
8682+
}).pipe(
8683+
Effect.scoped,
8684+
Effect.provide(
8685+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
8686+
),
8687+
),
8688+
);
8689+
84778690
it.effect("settles /goal pause with a reply instead of a native turn", () =>
84788691
Effect.gen(function* () {
84798692
const transcript = makeCodexReplayTranscript({

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
18541854
const pendingRootTurns = yield* Ref.make(
18551855
new Map<string, ProviderAdapter.ProviderAdapterV2TurnInput>(),
18561856
);
1857+
// `/compact` runs waiting for their native turn, by native thread. Codex
1858+
// can start another turn first (a goal it continues), so a compaction
1859+
// claims the turn that reports its contextCompaction item.
1860+
const pendingCompactions = new Map<string, ProviderAdapter.ProviderAdapterV2TurnInput>();
18571861
const turnWaiters = yield* Ref.make(new Map<string, Deferred.Deferred<void, never>>());
18581862
const subagentThreads = yield* Ref.make(new Map<string, CodexSubagentThreadContext>());
18591863
const subagentSelections = new Map<string, Omit<ModelSelection, "instanceId">>();
@@ -4442,6 +4446,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
44424446
yield* Deferred.succeed(goalHold.next, next);
44434447
return;
44444448
}
4449+
// This may be the pending compaction's turn, which its item claims.
4450+
// Stopping it as an orphan goal turn would end the compaction.
4451+
if (pendingCompactions.has(payload.threadId)) return;
44454452
// A goal turn with no run to own it (Codex continued after the run
44464453
// settled, or raced a Stop): stop it and pause the goal rather than
44474454
// work out of sight.
@@ -4620,6 +4627,19 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
46204627

46214628
yield* client.handleServerNotification("item/started", (payload) =>
46224629
Effect.gen(function* () {
4630+
const compaction = pendingCompactions.get(payload.threadId);
4631+
if (
4632+
payload.item.type === "contextCompaction" &&
4633+
compaction !== undefined &&
4634+
!(yield* Ref.get(activeTurns)).has(payload.turnId)
4635+
) {
4636+
pendingCompactions.delete(payload.threadId);
4637+
yield* registerRootTurn({
4638+
turnInput: compaction,
4639+
nativeTurnId: payload.turnId,
4640+
startedAt: DateTime.makeUnsafe(payload.startedAtMs),
4641+
});
4642+
}
46234643
const context = yield* awaitActiveTurn(payload.turnId);
46244644
if (context === undefined) {
46254645
return;
@@ -6576,20 +6596,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
65766596
compactThread: (turnInput) =>
65776597
Effect.gen(function* () {
65786598
const threadId = yield* getNativeThreadId(turnInput.providerThread);
6579-
yield* Ref.update(pendingRootTurns, (current) =>
6580-
new Map(current).set(threadId, turnInput),
6581-
);
6599+
pendingCompactions.set(threadId, turnInput);
65826600
// Cleared on interrupt too, as startTurn does: an interrupted start
6583-
// must not adopt the native turn that a late turn/started reports.
6584-
yield* client.request("thread/compact/start", { threadId }).pipe(
6585-
Effect.onError(() =>
6586-
Ref.update(pendingRootTurns, (current) => {
6587-
const next = new Map(current);
6588-
next.delete(threadId);
6589-
return next;
6590-
}),
6591-
),
6592-
);
6601+
// must not adopt the native turn that a late item/started reports.
6602+
yield* client
6603+
.request("thread/compact/start", { threadId })
6604+
.pipe(Effect.onError(() => Effect.sync(() => pendingCompactions.delete(threadId))));
65936605
}).pipe(
65946606
Effect.mapError(
65956607
(cause) =>

0 commit comments

Comments
 (0)