Skip to content

Commit 4a7c290

Browse files
saphidclaude
andcommitted
fix(codex): restoring a checkpoint works after the Codex session was archived
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cbd3e14 commit 4a7c290

2 files changed

Lines changed: 128 additions & 22 deletions

File tree

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7466,6 +7466,116 @@ describe("CodexAdapterV2 post-settle continuation", () => {
74667466
),
74677467
);
74687468

7469+
it.effect("unarchives an unloaded Codex thread before rolling it back", () =>
7470+
Effect.gen(function* () {
7471+
const nativeThreadId = "archived-rollback-thread";
7472+
const resumeParams = {
7473+
threadId: nativeThreadId,
7474+
excludeTurns: true,
7475+
cwd: CODEX_TEST_RUNTIME_POLICY.cwd,
7476+
model: CODEX_TEST_MODEL_SELECTION.model,
7477+
config: CodexAdapterV2.CODEX_THREAD_CONFIG,
7478+
};
7479+
const thread = codexReplayThreadResult({ nativeThreadId, forkedFromId: null });
7480+
const exchange = (
7481+
id: number,
7482+
method: string,
7483+
params: unknown,
7484+
reply: { readonly result: unknown } | { readonly error: unknown },
7485+
): Array<CodexReplay.CodexAppServerReplayEntry> => [
7486+
{ type: "expect_outbound", label: method, frame: { id, method, params } },
7487+
{ type: "emit_inbound", label: method, frame: { id, ...reply } },
7488+
];
7489+
const transcript = makeCodexReplayTranscript({
7490+
scenario: "codex-archived-rollback",
7491+
entries: [
7492+
...codexReplayPreamble({
7493+
nativeThreadId,
7494+
nativeTurnId: "archived-rollback-turn",
7495+
prompt: "unused",
7496+
}).slice(0, 5),
7497+
...exchange(
7498+
3,
7499+
"thread/read",
7500+
{ threadId: nativeThreadId, includeTurns: false },
7501+
{
7502+
result: {
7503+
thread: {
7504+
id: nativeThreadId,
7505+
historyMode: "paginated",
7506+
status: { type: "notLoaded" },
7507+
},
7508+
},
7509+
},
7510+
),
7511+
...exchange(4, "thread/resume", resumeParams, {
7512+
error: { code: -32600, message: `session ${nativeThreadId} is archived` },
7513+
}),
7514+
...exchange(5, "thread/unarchive", { threadId: nativeThreadId }, { result: thread }),
7515+
...exchange(6, "thread/resume", resumeParams, { result: thread }),
7516+
...exchange(
7517+
7,
7518+
"thread/turns/list",
7519+
{
7520+
threadId: nativeThreadId,
7521+
cursor: null,
7522+
limit: 1,
7523+
sortDirection: "desc",
7524+
itemsView: "summary",
7525+
},
7526+
{
7527+
result: {
7528+
data: [{ id: "native-turn-second", items: [], status: "completed", error: null }],
7529+
nextCursor: null,
7530+
},
7531+
},
7532+
),
7533+
...exchange(
7534+
8,
7535+
"thread/revert",
7536+
{ threadId: nativeThreadId, beforeTurnId: "native-turn-second" },
7537+
{ result: thread },
7538+
),
7539+
],
7540+
});
7541+
const harness = yield* makeCodexReplayHarness(transcript);
7542+
const now = yield* DateTime.now;
7543+
const firstTurn = codexReplaySourceTurn({
7544+
id: "provider-turn-first",
7545+
ordinal: 1,
7546+
nativeId: "native-turn-first",
7547+
providerThreadId: harness.providerThread.id,
7548+
now,
7549+
});
7550+
const secondTurn = codexReplaySourceTurn({
7551+
id: "provider-turn-second",
7552+
ordinal: 2,
7553+
nativeId: "native-turn-second",
7554+
providerThreadId: harness.providerThread.id,
7555+
now,
7556+
});
7557+
7558+
const snapshot = yield* harness.runtime.rollbackThread({
7559+
providerThread: harness.providerThread,
7560+
target: {
7561+
type: "provider_turn",
7562+
checkpointId: CheckpointId.make("checkpoint-archived-rollback"),
7563+
appRunOrdinal: 1,
7564+
providerTurn: firstTurn,
7565+
},
7566+
providerThreadTurns: [firstTurn, secondTurn],
7567+
});
7568+
7569+
assert.equal(snapshot.providerThread.nativeThreadRef?.nativeId, nativeThreadId);
7570+
assert.deepEqual(snapshot.providerThread.nativeConversationHeadRef, firstTurn.nativeTurnRef);
7571+
}).pipe(
7572+
Effect.scoped,
7573+
Effect.provide(
7574+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7575+
),
7576+
),
7577+
);
7578+
74697579
it.effect(
74707580
"falls back to fork-local thread/revert on paginated history when the source turn lacks a native reference",
74717581
() =>

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6696,6 +6696,21 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
66966696
);
66976697
});
66986698

6699+
// Codex refuses to resume a session archived elsewhere until it is unarchived.
6700+
const resumeNativeThread = (threadId: string, params: Record<string, unknown>) => {
6701+
const resume = client.raw.request("thread/resume", { threadId, ...params });
6702+
return resume.pipe(
6703+
Effect.catchTags({
6704+
CodexAppServerRequestError: (cause) =>
6705+
/\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(cause.errorMessage)
6706+
? client.raw
6707+
.request("thread/unarchive", { threadId })
6708+
.pipe(Effect.andThen(resume))
6709+
: Effect.fail(cause),
6710+
}),
6711+
);
6712+
};
6713+
66996714
const startNativeTurn = Effect.fn("CodexAdapterV2.startNativeTurn")(
67006715
function* (
67016716
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
@@ -7048,8 +7063,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
70487063
function* (threadInput) {
70497064
const nativeThreadId = yield* getNativeThreadId(threadInput.providerThread);
70507065
const appThreadId = threadInput.threadId ?? threadInput.providerThread.appThreadId;
7051-
const resume = client.raw.request("thread/resume", {
7052-
threadId: nativeThreadId,
7066+
const resume = resumeNativeThread(nativeThreadId, {
70537067
excludeTurns: true,
70547068
...codexThreadRuntimeParams({
70557069
threadId: appThreadId,
@@ -7063,24 +7077,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
70637077
}),
70647078
});
70657079
const response = yield* ensureInitialized.pipe(
7066-
Effect.andThen(
7067-
resume.pipe(
7068-
Effect.catchTags({
7069-
CodexAppServerRequestError: (cause) => {
7070-
if (
7071-
!/\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(
7072-
cause.errorMessage,
7073-
)
7074-
) {
7075-
return Effect.fail(cause);
7076-
}
7077-
return client.raw
7078-
.request("thread/unarchive", { threadId: nativeThreadId })
7079-
.pipe(Effect.andThen(resume));
7080-
},
7081-
}),
7082-
),
7083-
),
7080+
Effect.andThen(resume),
70847081
Effect.flatMap(decodeCodexResumeMetadata),
70857082
);
70867083
return {
@@ -7834,8 +7831,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
78347831
}
78357832
// thread/revert requires the thread loaded in this app-server; resume after restart or idle release.
78367833
if (!loaded) {
7837-
yield* client.raw.request("thread/resume", {
7838-
threadId,
7834+
yield* resumeNativeThread(threadId, {
78397835
excludeTurns: true,
78407836
...codexThreadRuntimeParams({
78417837
threadId: threadInput.providerThread.appThreadId,

0 commit comments

Comments
 (0)