Skip to content

Commit 2ee9784

Browse files
fix(server): Codex resumes a thread that is still closing after unsubscribe
With thread_unload_delay_secs=0, Codex replies to thread/unsubscribe before its unload finishes and rejects thread/resume with "thread <id> is closing" until it emits thread/closed. The linked-environment reload (unsubscribe, then resume) failed the turn when it hit that window. unloadThread now arms a per-thread signal that the existing thread/closed handler completes. On the closing error, resumeNativeThread waits for it (at most 10 seconds, not at all when the thread already closed) and resumes once more. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5e90d1e commit 2ee9784

3 files changed

Lines changed: 127 additions & 8 deletions

File tree

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,109 @@ describe("CodexAdapterV2 post-settle continuation", () => {
30943094
),
30953095
);
30963096

3097+
// regression: #33747 Codex refuses thread/resume until an unsubscribed thread finishes closing
3098+
it.effect.each([
3099+
{ name: "waits for thread/closed after a closing error", closedFirst: false },
3100+
{ name: "retries at once when the thread already closed", closedFirst: true },
3101+
])("$name", ({ name, closedFirst }) =>
3102+
Effect.scoped(
3103+
Effect.gen(function* () {
3104+
const nativeThreadId = "closing-thread";
3105+
const params = {
3106+
threadId: nativeThreadId,
3107+
excludeTurns: true,
3108+
cwd: CODEX_TEST_RUNTIME_POLICY.cwd,
3109+
model: CODEX_TEST_MODEL_SELECTION.model,
3110+
config: CodexAdapterV2.CODEX_THREAD_CONFIG,
3111+
};
3112+
const closed: CodexReplay.CodexAppServerReplayEntry = {
3113+
type: "emit_inbound",
3114+
label: "thread closed",
3115+
frame: { method: "thread/closed", params: { threadId: nativeThreadId } },
3116+
};
3117+
const closingError: CodexReplay.CodexAppServerReplayEntry = {
3118+
type: "emit_inbound",
3119+
label: "resume closing error",
3120+
frame: {
3121+
id: 4,
3122+
error: {
3123+
code: -32600,
3124+
message: `thread ${nativeThreadId} is closing; retry thread/resume after the thread is closed`,
3125+
},
3126+
},
3127+
};
3128+
const harness = yield* makeCodexReplayHarness(
3129+
makeCodexReplayTranscript({
3130+
scenario: name,
3131+
entries: [
3132+
...codexReplayPreamble({
3133+
nativeThreadId,
3134+
nativeTurnId: "unused-turn",
3135+
prompt: "unused-prompt",
3136+
}).slice(0, 5),
3137+
{
3138+
type: "expect_outbound",
3139+
label: "unsubscribe",
3140+
frame: {
3141+
id: 3,
3142+
method: "thread/unsubscribe",
3143+
params: { threadId: nativeThreadId },
3144+
},
3145+
},
3146+
{
3147+
type: "emit_inbound",
3148+
label: "unsubscribe result",
3149+
frame: { id: 3, result: { status: "unsubscribed" } },
3150+
},
3151+
{
3152+
type: "expect_outbound",
3153+
label: "resume closing thread",
3154+
frame: { id: 4, method: "thread/resume", params },
3155+
},
3156+
...(closedFirst ? [closed, closingError] : [closingError, closed]),
3157+
{
3158+
type: "expect_outbound",
3159+
label: "retry identical resume",
3160+
frame: { id: 5, method: "thread/resume", params },
3161+
},
3162+
{
3163+
type: "emit_inbound",
3164+
label: "retry result",
3165+
frame: {
3166+
id: 5,
3167+
result: {
3168+
thread: {
3169+
id: nativeThreadId,
3170+
updatedAt: 1782622450,
3171+
turns: [{ type: "unknown-history-item" }],
3172+
},
3173+
},
3174+
},
3175+
},
3176+
],
3177+
}),
3178+
);
3179+
yield* harness.runtime.unloadThread!({ providerThread: harness.providerThread });
3180+
const resumed = yield* harness.runtime.resumeThread({
3181+
providerThread: harness.providerThread,
3182+
modelSelection: CODEX_TEST_MODEL_SELECTION,
3183+
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
3184+
});
3185+
assert.equal(resumed.nativeThreadRef?.nativeId, nativeThreadId);
3186+
assert.equal(resumed.status, "idle");
3187+
}).pipe(
3188+
Effect.provide(
3189+
Layer.mergeAll(
3190+
IdAllocator.layer,
3191+
McpProviderSessions.layer,
3192+
NodeServices.layer,
3193+
NodeCrypto.layer,
3194+
),
3195+
),
3196+
),
3197+
),
3198+
);
3199+
30973200
it.effect.each([
30983201
"recover",
30993202
"completion-only",

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
19351935
const pendingRuntimeRequests = yield* Ref.make(
19361936
new Map<string, PendingCodexRuntimeRequest>(),
19371937
);
1938+
/** Completed by thread/closed after this connection unsubscribes from the native thread. */
1939+
const threadClosings = new Map<string, Deferred.Deferred<void>>();
19381940
/** Codex acceptForSession covers only the exact command or patch, not future commands. */
19391941
const sessionApprovals = new Map<
19401942
string,
@@ -4868,6 +4870,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
48684870
// their command items, while the shared provider session stays alive.
48694871
yield* client.handleServerNotification("thread/closed", (payload) =>
48704872
Effect.gen(function* () {
4873+
const closing = threadClosings.get(payload.threadId);
4874+
threadClosings.delete(payload.threadId);
4875+
if (closing !== undefined) yield* Deferred.succeed(closing, undefined);
48714876
const completedAt = yield* DateTime.now;
48724877
for (const context of (yield* Ref.get(settledTurns)).values()) {
48734878
if (context.providerThread.nativeThreadRef?.nativeId !== payload.threadId) continue;
@@ -6826,20 +6831,31 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
68266831
);
68276832
});
68286833

6829-
// Codex refuses to resume a session archived elsewhere until it is unarchived.
6834+
// Codex refuses to resume a session archived elsewhere until it is unarchived, and
6835+
// one it is still unloading after thread/unsubscribe until it emits thread/closed.
68306836
const resumeNativeThread = Effect.fn("CodexAdapterV2.resumeNativeThread")(function* (
68316837
threadId: string,
68326838
params: Record<string, unknown>,
68336839
) {
68346840
const resume = client.raw.request("thread/resume", { threadId, ...params });
68356841
return yield* resume.pipe(
68366842
Effect.catchTags({
6837-
CodexAppServerRequestError: (cause) =>
6838-
/\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(cause.errorMessage)
6839-
? client.raw
6840-
.request("thread/unarchive", { threadId })
6841-
.pipe(Effect.andThen(resume))
6842-
: Effect.fail(cause),
6843+
CodexAppServerRequestError: (cause) => {
6844+
if (/\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(cause.errorMessage)) {
6845+
return client.raw
6846+
.request("thread/unarchive", { threadId })
6847+
.pipe(Effect.andThen(resume));
6848+
}
6849+
if (/\bthread \S+ is closing\b/i.test(cause.errorMessage)) {
6850+
const closing = threadClosings.get(threadId);
6851+
return (
6852+
closing === undefined
6853+
? Effect.void
6854+
: Deferred.await(closing).pipe(Effect.timeoutOption("10 seconds"))
6855+
).pipe(Effect.andThen(resume));
6856+
}
6857+
return Effect.fail(cause);
6858+
},
68436859
}),
68446860
);
68456861
});
@@ -7381,6 +7397,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73817397
function* (unloadInput) {
73827398
const nativeThreadId = yield* getNativeThreadId(unloadInput.providerThread);
73837399
sessionApprovals.delete(nativeThreadId);
7400+
threadClosings.set(nativeThreadId, yield* Deferred.make<void>());
73847401
yield* client.request("thread/unsubscribe", { threadId: nativeThreadId });
73857402
},
73867403
(effect, unloadInput) =>

‎apps/server/src/provider/codexLaunchArgs.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { describe, it } from "vite-plus/test";
55
import { codexAppServerArgs, codexExecLaunchArgs } from "./codexLaunchArgs.ts";
66

77
describe("codexAppServerArgs", () => {
8-
// T3 unsubscribes only when it unloads a thread; Codex applies the last repeated -c.
98
it.each([
109
{
1110
name: "preserves escaped TOML quotes in config values",

0 commit comments

Comments
 (0)