Skip to content

Commit ba962f2

Browse files
saphidclaudemaria-rcks
authored
fix(codex): restoring a checkpoint works after the Codex session was archived (pingdotgg#29582)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 5349ecb commit ba962f2

2 files changed

Lines changed: 136 additions & 22 deletions

File tree

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7702,6 +7702,121 @@ describe("CodexAdapterV2 post-settle continuation", () => {
77027702
),
77037703
);
77047704

7705+
it.effect("unarchives an unloaded Codex thread before rolling it back", () =>
7706+
Effect.gen(function* () {
7707+
const nativeThreadId = "archived-rollback-thread";
7708+
const resumeParams = {
7709+
threadId: nativeThreadId,
7710+
excludeTurns: true,
7711+
cwd: CODEX_TEST_RUNTIME_POLICY.cwd,
7712+
model: CODEX_TEST_MODEL_SELECTION.model,
7713+
config: CodexAdapterV2.CODEX_THREAD_CONFIG,
7714+
};
7715+
const thread = codexReplayThreadResult({ nativeThreadId, forkedFromId: null });
7716+
const exchange = (
7717+
id: number,
7718+
method: string,
7719+
params: unknown,
7720+
reply: { readonly result: unknown } | { readonly error: unknown },
7721+
): Array<CodexReplay.CodexAppServerReplayEntry> => [
7722+
{ type: "expect_outbound", label: method, frame: { id, method, params } },
7723+
{ type: "emit_inbound", label: method, frame: { id, ...reply } },
7724+
];
7725+
const transcript = makeCodexReplayTranscript({
7726+
scenario: "codex-archived-rollback",
7727+
entries: [
7728+
...codexReplayPreamble({
7729+
nativeThreadId,
7730+
nativeTurnId: "archived-rollback-turn",
7731+
prompt: "unused",
7732+
}).slice(0, 5),
7733+
...exchange(
7734+
3,
7735+
"thread/read",
7736+
{ threadId: nativeThreadId, includeTurns: false },
7737+
{
7738+
result: {
7739+
thread: {
7740+
id: nativeThreadId,
7741+
historyMode: "paginated",
7742+
status: { type: "notLoaded" },
7743+
},
7744+
},
7745+
},
7746+
),
7747+
...exchange(4, "thread/resume", resumeParams, {
7748+
error: { code: -32600, message: `session ${nativeThreadId} is archived` },
7749+
}),
7750+
...exchange(5, "thread/unarchive", { threadId: nativeThreadId }, { result: thread }),
7751+
...exchange(6, "thread/resume", resumeParams, { result: thread }),
7752+
...exchange(
7753+
7,
7754+
"thread/turns/list",
7755+
{
7756+
threadId: nativeThreadId,
7757+
cursor: null,
7758+
limit: 1,
7759+
sortDirection: "desc",
7760+
itemsView: "summary",
7761+
},
7762+
{
7763+
result: {
7764+
data: [{ id: "native-turn-second", items: [], status: "completed", error: null }],
7765+
nextCursor: null,
7766+
},
7767+
},
7768+
),
7769+
...exchange(
7770+
8,
7771+
"thread/revert",
7772+
{ threadId: nativeThreadId, beforeTurnId: "native-turn-second" },
7773+
{ result: thread },
7774+
),
7775+
],
7776+
});
7777+
const harness = yield* makeCodexReplayHarness(transcript);
7778+
const now = yield* DateTime.now;
7779+
const firstTurn = codexReplaySourceTurn({
7780+
id: "provider-turn-first",
7781+
ordinal: 1,
7782+
nativeId: "native-turn-first",
7783+
providerThreadId: harness.providerThread.id,
7784+
now,
7785+
});
7786+
const secondTurn = codexReplaySourceTurn({
7787+
id: "provider-turn-second",
7788+
ordinal: 2,
7789+
nativeId: "native-turn-second",
7790+
providerThreadId: harness.providerThread.id,
7791+
now,
7792+
});
7793+
7794+
const snapshot = yield* harness.runtime.rollbackThread({
7795+
providerThread: harness.providerThread,
7796+
target: {
7797+
type: "provider_turn",
7798+
checkpointId: CheckpointId.make("checkpoint-archived-rollback"),
7799+
appRunOrdinal: 1,
7800+
providerTurn: firstTurn,
7801+
},
7802+
providerThreadTurns: [firstTurn, secondTurn],
7803+
});
7804+
7805+
assert.equal(snapshot.providerThread.nativeThreadRef?.nativeId, nativeThreadId);
7806+
assert.deepEqual(snapshot.providerThread.nativeConversationHeadRef, firstTurn.nativeTurnRef);
7807+
}).pipe(
7808+
Effect.scoped,
7809+
Effect.provide(
7810+
Layer.mergeAll(
7811+
IdAllocator.layer,
7812+
McpProviderSessions.layer,
7813+
NodeServices.layer,
7814+
NodeCrypto.layer,
7815+
),
7816+
),
7817+
),
7818+
);
7819+
77057820
it.effect(
77067821
"falls back to fork-local thread/revert on paginated history when the source turn lacks a native reference",
77077822
() =>

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6696,6 +6696,24 @@ 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 = Effect.fn("CodexAdapterV2.resumeNativeThread")(function* (
6701+
threadId: string,
6702+
params: Record<string, unknown>,
6703+
) {
6704+
const resume = client.raw.request("thread/resume", { threadId, ...params });
6705+
return yield* resume.pipe(
6706+
Effect.catchTags({
6707+
CodexAppServerRequestError: (cause) =>
6708+
/\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(cause.errorMessage)
6709+
? client.raw
6710+
.request("thread/unarchive", { threadId })
6711+
.pipe(Effect.andThen(resume))
6712+
: Effect.fail(cause),
6713+
}),
6714+
);
6715+
});
6716+
66996717
const startNativeTurn = Effect.fn("CodexAdapterV2.startNativeTurn")(
67006718
function* (
67016719
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
@@ -7048,8 +7066,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
70487066
function* (threadInput) {
70497067
const nativeThreadId = yield* getNativeThreadId(threadInput.providerThread);
70507068
const appThreadId = threadInput.threadId ?? threadInput.providerThread.appThreadId;
7051-
const resume = client.raw.request("thread/resume", {
7052-
threadId: nativeThreadId,
7069+
const resume = resumeNativeThread(nativeThreadId, {
70537070
excludeTurns: true,
70547071
...codexThreadRuntimeParams({
70557072
threadId: appThreadId,
@@ -7063,24 +7080,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
70637080
}),
70647081
});
70657082
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-
),
7083+
Effect.andThen(resume),
70847084
Effect.flatMap(decodeCodexResumeMetadata),
70857085
);
70867086
return {
@@ -7834,8 +7834,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
78347834
}
78357835
// thread/revert requires the thread loaded in this app-server; resume after restart or idle release.
78367836
if (!loaded) {
7837-
yield* client.raw.request("thread/resume", {
7838-
threadId,
7837+
yield* resumeNativeThread(threadId, {
78397838
excludeTurns: true,
78407839
...codexThreadRuntimeParams({
78417840
threadId: threadInput.providerThread.appThreadId,

0 commit comments

Comments
 (0)