Skip to content

Commit 8fb6bbb

Browse files
saphidclaude
authored andcommitted
fix(server): forking a Codex thread archived outside T3 Code no longer fails
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 60aec28 commit 8fb6bbb

2 files changed

Lines changed: 96 additions & 15 deletions

File tree

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7613,6 +7613,79 @@ describe("CodexAdapterV2 post-settle continuation", () => {
76137613
),
76147614
);
76157615

7616+
it.effect("unarchives an archived Codex source thread before forking it", () =>
7617+
Effect.gen(function* () {
7618+
const nativeThreadId = "archived-fork-source";
7619+
const forkThreadId = "archived-fork-target";
7620+
const forkParams = {
7621+
threadId: nativeThreadId,
7622+
lastTurnId: "native-turn-first",
7623+
config: CodexAdapterV2.CODEX_THREAD_CONFIG,
7624+
};
7625+
const exchange = (
7626+
id: number,
7627+
method: string,
7628+
params: unknown,
7629+
reply: { readonly result: unknown } | { readonly error: unknown },
7630+
): Array<CodexReplay.CodexAppServerReplayEntry> => [
7631+
{ type: "expect_outbound", label: method, frame: { id, method, params } },
7632+
{ type: "emit_inbound", label: method, frame: { id, ...reply } },
7633+
];
7634+
const transcript = makeCodexReplayTranscript({
7635+
scenario: "codex-archived-fork",
7636+
entries: [
7637+
...codexReplayPreamble({
7638+
nativeThreadId,
7639+
nativeTurnId: "archived-fork-turn",
7640+
prompt: "unused",
7641+
}).slice(0, 5),
7642+
...exchange(3, "thread/fork", forkParams, {
7643+
error: {
7644+
code: -32600,
7645+
message: `session ${nativeThreadId} is archived. Run \`codex unarchive ${nativeThreadId}\` to unarchive it first.`,
7646+
},
7647+
}),
7648+
...exchange(
7649+
4,
7650+
"thread/unarchive",
7651+
{ threadId: nativeThreadId },
7652+
{ result: codexReplayThreadResult({ nativeThreadId, forkedFromId: null }) },
7653+
),
7654+
...exchange(5, "thread/fork", forkParams, {
7655+
result: codexReplayThreadResult({
7656+
nativeThreadId: forkThreadId,
7657+
forkedFromId: nativeThreadId,
7658+
}),
7659+
}),
7660+
],
7661+
});
7662+
const harness = yield* makeCodexReplayHarness(transcript);
7663+
const now = yield* DateTime.now;
7664+
const firstTurn = codexReplaySourceTurn({
7665+
id: "provider-turn-first",
7666+
ordinal: 1,
7667+
nativeId: "native-turn-first",
7668+
providerThreadId: harness.providerThread.id,
7669+
now,
7670+
});
7671+
7672+
const forkedProviderThread = yield* harness.runtime.forkThread({
7673+
sourceProviderThread: harness.providerThread,
7674+
sourceProviderTurns: [firstTurn],
7675+
providerTurnId: firstTurn.id,
7676+
targetThreadId: ThreadId.make("thread-archived-fork-target"),
7677+
});
7678+
7679+
assert.equal(forkedProviderThread.nativeThreadRef?.nativeId, forkThreadId);
7680+
assert.equal(forkedProviderThread.forkedFrom?.providerTurnId, firstTurn.id);
7681+
}).pipe(
7682+
Effect.scoped,
7683+
Effect.provide(
7684+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7685+
),
7686+
),
7687+
);
7688+
76167689
describe("native goals", () => {
76177690
const nativeThreadId = "goal-thread";
76187691
const objective = "Ship the feature";

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7883,24 +7883,32 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
78837883
function* (threadInput) {
78847884
const threadId = yield* getNativeThreadId(threadInput.sourceProviderThread);
78857885
const boundary = yield* resolveCodexForkBoundary(threadInput);
7886+
const fork = client.request("thread/fork", {
7887+
threadId,
7888+
...(boundary.lastTurnId === undefined ? {} : { lastTurnId: boundary.lastTurnId }),
7889+
...codexThreadRuntimeParams({
7890+
threadId: threadInput.targetThreadId,
7891+
mcpSession: yield* mcpSessions.read(threadInput.targetThreadId),
7892+
...(threadInput.modelSelection === undefined
7893+
? {}
7894+
: { modelSelection: threadInput.modelSelection }),
7895+
...(threadInput.runtimePolicy === undefined
7896+
? {}
7897+
: { runtimePolicy: threadInput.runtimePolicy }),
7898+
}),
7899+
});
78867900
const response = yield* ensureInitialized.pipe(
78877901
Effect.andThen(
7888-
client.request("thread/fork", {
7889-
threadId,
7890-
...(boundary.lastTurnId === undefined
7891-
? {}
7892-
: { lastTurnId: boundary.lastTurnId }),
7893-
...codexThreadRuntimeParams({
7894-
threadId: threadInput.targetThreadId,
7895-
mcpSession: yield* mcpSessions.read(threadInput.targetThreadId),
7896-
...(threadInput.modelSelection === undefined
7897-
? {}
7898-
: { modelSelection: threadInput.modelSelection }),
7899-
...(threadInput.runtimePolicy === undefined
7900-
? {}
7901-
: { runtimePolicy: threadInput.runtimePolicy }),
7902+
fork.pipe(
7903+
Effect.catchTags({
7904+
CodexAppServerRequestError: (cause) =>
7905+
/\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(cause.errorMessage)
7906+
? client.raw
7907+
.request("thread/unarchive", { threadId })
7908+
.pipe(Effect.andThen(fork))
7909+
: Effect.fail(cause),
79027910
}),
7903-
}),
7911+
),
79047912
),
79057913
Effect.mapError(
79067914
(cause) =>

0 commit comments

Comments
 (0)