Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
Loading
Loading