@devries-cameron20 / t3code-1 / commits / c642c4b57d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): settling a Codex thread stops its MCP servers right away (#33767)

Julius Marminge committed Nov 11, 2026 at 15:55 UTC c642c4b57d3eb831c3d20552ec591d6155d36f0c
4 files changed +151 -12
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+103
@@ -3265,6 +3265,109 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3265 ),
3266 );
3267
3268 + // regression: #33747 Codex refuses thread/resume until an unsubscribed thread finishes closing
3269 + it.effect.each([
3270 + { name: "waits for thread/closed after a closing error", closedFirst: false },
3271 + { name: "retries at once when the thread already closed", closedFirst: true },
3272 + ])("$name", ({ name, closedFirst }) =>
3273 + Effect.scoped(
3274 + Effect.gen(function* () {
3275 + const nativeThreadId = "closing-thread";
3276 + const params = {
3277 + threadId: nativeThreadId,
3278 + excludeTurns: true,
3279 + cwd: CODEX_TEST_RUNTIME_POLICY.cwd,
3280 + model: CODEX_TEST_MODEL_SELECTION.model,
3281 + config: CodexAdapterV2.CODEX_THREAD_CONFIG,
3282 + };
3283 + const closed: CodexReplay.CodexAppServerReplayEntry = {
3284 + type: "emit_inbound",
3285 + label: "thread closed",
3286 + frame: { method: "thread/closed", params: { threadId: nativeThreadId } },
3287 + };
3288 + const closingError: CodexReplay.CodexAppServerReplayEntry = {
3289 + type: "emit_inbound",
3290 + label: "resume closing error",
3291 + frame: {
3292 + id: 4,
3293 + error: {
3294 + code: -32600,
3295 + message: `thread ${nativeThreadId} is closing; retry thread/resume after the thread is closed`,
3296 + },
3297 + },
3298 + };
3299 + const harness = yield* makeCodexReplayHarness(
3300 + makeCodexReplayTranscript({
3301 + scenario: name,
3302 + entries: [
3303 + ...codexReplayPreamble({
3304 + nativeThreadId,
3305 + nativeTurnId: "unused-turn",
3306 + prompt: "unused-prompt",
3307 + }).slice(0, 5),
3308 + {
3309 + type: "expect_outbound",
3310 + label: "unsubscribe",
3311 + frame: {
3312 + id: 3,
3313 + method: "thread/unsubscribe",
3314 + params: { threadId: nativeThreadId },
3315 + },
3316 + },
3317 + {
3318 + type: "emit_inbound",
3319 + label: "unsubscribe result",
3320 + frame: { id: 3, result: { status: "unsubscribed" } },
3321 + },
3322 + {
3323 + type: "expect_outbound",
3324 + label: "resume closing thread",
3325 + frame: { id: 4, method: "thread/resume", params },
3326 + },
3327 + ...(closedFirst ? [closed, closingError] : [closingError, closed]),
3328 + {
3329 + type: "expect_outbound",
3330 + label: "retry identical resume",
3331 + frame: { id: 5, method: "thread/resume", params },
3332 + },
3333 + {
3334 + type: "emit_inbound",
3335 + label: "retry result",
3336 + frame: {
3337 + id: 5,
3338 + result: {
3339 + thread: {
3340 + id: nativeThreadId,
3341 + updatedAt: 1782622450,
3342 + turns: [{ type: "unknown-history-item" }],
3343 + },
3344 + },
3345 + },
3346 + },
3347 + ],
3348 + }),
3349 + );
3350 + yield* harness.runtime.unloadThread!({ providerThread: harness.providerThread });
3351 + const resumed = yield* harness.runtime.resumeThread({
3352 + providerThread: harness.providerThread,
3353 + modelSelection: CODEX_TEST_MODEL_SELECTION,
3354 + runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
3355 + });
3356 + assert.equal(resumed.nativeThreadRef?.nativeId, nativeThreadId);
3357 + assert.equal(resumed.status, "idle");
3358 + }).pipe(
3359 + Effect.provide(
3360 + Layer.mergeAll(
3361 + IdAllocator.layer,
3362 + McpProviderSessions.layer,
3363 + NodeServices.layer,
3364 + NodeCrypto.layer,
3365 + ),
3366 + ),
3367 + ),
3368 + ),
3369 + );
3370 +
3371 it.effect.each([
3372 "recover",
3373 "completion-only",
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+27 -7
@@ -1962,6 +1962,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1962 const pendingRuntimeRequests = yield* Ref.make(
1963 new Map<string, PendingCodexRuntimeRequest>(),
1964 );
1965 + /** Completed by thread/closed after this connection unsubscribes from the native thread. */
1966 + const threadClosings = new Map<string, Deferred.Deferred<void>>();
1967 /** Codex acceptForSession covers only the exact command or patch, not future commands. */
1968 const sessionApprovals = new Map<
1969 string,
@@ -4998,6 +5000,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5000 // their command items, while the shared provider session stays alive.
5001 yield* client.handleServerNotification("thread/closed", (payload) =>
5002 Effect.gen(function* () {
5003 + const closing = threadClosings.get(payload.threadId);
5004 + threadClosings.delete(payload.threadId);
5005 + if (closing !== undefined) yield* Deferred.succeed(closing, undefined);
5006 const completedAt = yield* DateTime.now;
5007 for (const context of (yield* Ref.get(settledTurns)).values()) {
5008 if (context.providerThread.nativeThreadRef?.nativeId !== payload.threadId) continue;
@@ -7078,7 +7083,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7083 );
7084 });
7085
7081 - // Codex refuses to resume a session archived elsewhere until it is unarchived.
7086 + // Codex refuses to resume a session archived elsewhere until it is unarchived, and
7087 + // one it is still unloading after thread/unsubscribe until it emits thread/closed.
7088 const resumeNativeThread = Effect.fn("CodexAdapterV2.resumeNativeThread")(function* (
7089 threadId: string,
7090 params: Record<string, unknown>,
@@ -7086,12 +7092,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7092 const resume = client.raw.request("thread/resume", { threadId, ...params });
7093 return yield* resume.pipe(
7094 Effect.catchTags({
7089 - CodexAppServerRequestError: (cause) =>
7090 - /\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(cause.errorMessage)
7091 - ? client.raw
7092 - .request("thread/unarchive", { threadId })
7093 - .pipe(Effect.andThen(resume))
7094 - : Effect.fail(cause),
7095 + CodexAppServerRequestError: (cause) => {
7096 + if (/\bsession \S+ is archived\b|\bcodex unarchive\b/i.test(cause.errorMessage)) {
7097 + return client.raw
7098 + .request("thread/unarchive", { threadId })
7099 + .pipe(Effect.andThen(resume));
7100 + }
7101 + if (/\bthread \S+ is closing\b/i.test(cause.errorMessage)) {
7102 + const closing = threadClosings.get(threadId);
7103 + return (
7104 + closing === undefined
7105 + ? Effect.void
7106 + : Deferred.await(closing).pipe(Effect.timeoutOption("10 seconds"))
7107 + ).pipe(Effect.andThen(resume));
7108 + }
7109 + return Effect.fail(cause);
7110 + },
7111 }),
7112 );
7113 });
@@ -7634,6 +7650,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7650 function* (unloadInput) {
7651 const nativeThreadId = yield* getNativeThreadId(unloadInput.providerThread);
7652 sessionApprovals.delete(nativeThreadId);
7653 + // A resume may already wait on the pending entry; replacing it would strand that wait.
7654 + if (!threadClosings.has(nativeThreadId)) {
7655 + threadClosings.set(nativeThreadId, yield* Deferred.make<void>());
7656 + }
7657 yield* client.request("thread/unsubscribe", { threadId: nativeThreadId });
7658 },
7659 (effect, unloadInput) =>
apps/server/src/provider/codexLaunchArgs.test.ts
+18 -5
@@ -9,11 +9,24 @@ import {
9 } from "./codexLaunchArgs.ts";
10
11 describe("codexAppServerArgs", () => {
12 - it("preserves escaped TOML quotes in config values", () => {
13 - NodeAssert.deepStrictEqual(
14 - codexAppServerArgs(String.raw`--config model=\"gpt-6.1-sol\" --strict-config`),
15 - ["app-server", "--config", 'model="gpt-6.1-sol"', "--strict-config"],
16 - );
12 + it.each([
13 + {
14 + name: "preserves escaped TOML quotes in config values",
15 + launchArgs: String.raw`--config model=\"gpt-6.1-sol\" --strict-config`,
16 + expected: ["--config", 'model="gpt-6.1-sol"', "--strict-config"],
17 + },
18 + {
19 + name: "lets launch args override the unload delay",
20 + launchArgs: "-c thread_unload_delay_secs=1800",
21 + expected: ["-c", "thread_unload_delay_secs=1800"],
22 + },
23 + ])("asks Codex to unload unsubscribed threads immediately and $name", (row) => {
24 + NodeAssert.deepStrictEqual(codexAppServerArgs(row.launchArgs), [
25 + "app-server",
26 + "-c",
27 + "thread_unload_delay_secs=0",
28 + ...row.expected,
29 + ]);
30 });
31 });
32
apps/server/src/provider/codexLaunchArgs.ts
+3
@@ -9,8 +9,11 @@ export const resolveCodexLaunchArgs = (
9
10 const codexLaunchArgv = (launchArgs?: string): ReadonlyArray<string> => tokenizeCliArgs(launchArgs);
11
12 +// T3 unsubscribes only when it unloads a thread; Codex's own unload delay would stack on that.
13 export const codexAppServerArgs = (launchArgs?: string) => [
14 "app-server",
15 + "-c",
16 + "thread_unload_delay_secs=0",
17 ...codexLaunchArgv(launchArgs),
18 ];
19