The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 07:00 UTC
fix(codex): restoring a checkpoint works after the Codex session was archived (#29582)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>
Alex Southwell committed
Nov 3, 2026 at 01:40 UTC
ba962f2d2382f0e4cb183451787a8fb3363f69ca
2 files changed
+136
-22
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+115
@@ -7702,6 +7702,121 @@ describe("CodexAdapterV2 post-settle continuation", () => {
7702
),
7703
);
7704
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
+
7820
it.effect(
7821
"falls back to fork-local thread/revert on paginated history when the source turn lacks a native reference",
7822
() =>
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+21
-22
@@ -6696,6 +6696,24 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6696
);
6697
});
6698
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
+
6717
const startNativeTurn = Effect.fn("CodexAdapterV2.startNativeTurn")(
6718
function* (
6719
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
@@ -7048,8 +7066,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7066
function* (threadInput) {
7067
const nativeThreadId = yield* getNativeThreadId(threadInput.providerThread);
7068
const appThreadId = threadInput.threadId ?? threadInput.providerThread.appThreadId;
7051
- const resume = client.raw.request("thread/resume", {
7052
- threadId: nativeThreadId,
7069
+ const resume = resumeNativeThread(nativeThreadId, {
7070
excludeTurns: true,
7071
...codexThreadRuntimeParams({
7072
threadId: appThreadId,
@@ -7063,24 +7080,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7080
}),
7081
});
7082
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),
7084
Effect.flatMap(decodeCodexResumeMetadata),
7085
);
7086
return {
@@ -7834,8 +7834,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7834
}
7835
// thread/revert requires the thread loaded in this app-server; resume after restart or idle release.
7836
if (!loaded) {
7837
- yield* client.raw.request("thread/resume", {
7838
- threadId,
7837
+ yield* resumeNativeThread(threadId, {
7838
excludeTurns: true,
7839
...codexThreadRuntimeParams({
7840
threadId: threadInput.providerThread.appThreadId,