The best way to control Claude Code, Codex and more from any computer or phone
Fix #29433: Restore Codex MCP apps after thread unload (#29498)
Co-authored-by: amr sarhan <amr@amrs-MacBook-Air.local> Co-authored-by: Julius Marminge <julius0216@outlook.com>
amr sarhan committed
Nov 15, 2026 at 07:40 UTC
1e60d60519cdc246f70f71c8b36e18ff5c34c41a
2 files changed
+185
-23
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+118
@@ -7988,6 +7988,124 @@ describe("CodexAdapterV2 post-settle continuation", () => {
7988
],
7989
});
7990
7991
+ it.effect("resumes an unloaded native thread and retries an MCP resource read", () =>
7992
+ Effect.scoped(
7993
+ Effect.gen(function* () {
7994
+ const scenario = "mcp-resource-thread-recovery";
7995
+ const nativeThreadId = `native-${scenario}`;
7996
+ const preamble = codexReplayPreamble({
7997
+ nativeThreadId,
7998
+ nativeTurnId: "unused-turn",
7999
+ prompt: "unused-prompt",
8000
+ }).slice(0, 5);
8001
+
8002
+ const resourceResult = {
8003
+ contents: [
8004
+ {
8005
+ uri: MCP_APP_RESOURCE,
8006
+ mimeType: "text/html;profile=mcp-app",
8007
+ text: MCP_APP_HTML,
8008
+ _meta: { ui: { csp: { connectDomains: ["https://api.weather.test"] } } },
8009
+ },
8010
+ ],
8011
+ };
8012
+
8013
+ const transcript = makeCodexReplayTranscript({
8014
+ scenario,
8015
+ entries: [
8016
+ ...preamble,
8017
+ {
8018
+ type: "expect_outbound",
8019
+ label: "initial MCP resource read",
8020
+ frame: {
8021
+ id: 3,
8022
+ method: "mcpServer/resource/read",
8023
+ params: {
8024
+ threadId: nativeThreadId,
8025
+ server: "weather",
8026
+ uri: MCP_APP_RESOURCE,
8027
+ },
8028
+ },
8029
+ },
8030
+ {
8031
+ type: "emit_inbound",
8032
+ label: "native thread is unloaded",
8033
+ frame: {
8034
+ id: 3,
8035
+ error: {
8036
+ code: -32600,
8037
+ message: `thread not found: ${nativeThreadId}`,
8038
+ },
8039
+ },
8040
+ },
8041
+ {
8042
+ type: "expect_outbound",
8043
+ label: "resume unloaded native thread",
8044
+ frame: {
8045
+ id: 4,
8046
+ method: "thread/resume",
8047
+ params: {
8048
+ threadId: nativeThreadId,
8049
+ excludeTurns: true,
8050
+ config: CodexAdapterV2.CODEX_THREAD_CONFIG,
8051
+ },
8052
+ },
8053
+ },
8054
+ {
8055
+ type: "emit_inbound",
8056
+ label: "native thread resumed",
8057
+ frame: {
8058
+ id: 4,
8059
+ result: {
8060
+ thread: { id: nativeThreadId, updatedAt: 1782622450 },
8061
+ },
8062
+ },
8063
+ },
8064
+ {
8065
+ type: "expect_outbound",
8066
+ label: "retry MCP resource read",
8067
+ frame: {
8068
+ id: 5,
8069
+ method: "mcpServer/resource/read",
8070
+ params: {
8071
+ threadId: nativeThreadId,
8072
+ server: "weather",
8073
+ uri: MCP_APP_RESOURCE,
8074
+ },
8075
+ },
8076
+ },
8077
+ {
8078
+ type: "emit_inbound",
8079
+ label: "MCP resource read succeeds",
8080
+ frame: { id: 5, result: resourceResult },
8081
+ },
8082
+ ],
8083
+ });
8084
+
8085
+ const harness = yield* makeCodexReplayHarness(transcript);
8086
+ const mcpApps = harness.runtime.mcpApps;
8087
+ assert.ok(mcpApps, "Codex runtime should expose MCP apps");
8088
+
8089
+ const result = yield* mcpApps.readResource({
8090
+ providerThread: harness.providerThread,
8091
+ server: "weather",
8092
+ uri: MCP_APP_RESOURCE,
8093
+ });
8094
+
8095
+ assert.deepEqual(result, resourceResult);
8096
+ }).pipe(
8097
+ Effect.provide(
8098
+ Layer.mergeAll(
8099
+ IdAllocator.layer,
8100
+ McpProviderSessions.layer,
8101
+ NodeServices.layer,
8102
+ NodeCrypto.layer,
8103
+ ),
8104
+ ),
8105
+ ),
8106
+ ),
8107
+ );
8108
+
8109
it.effect("captures an MCP app's resource and holds the turn open until it lands", () =>
8110
Effect.gen(function* () {
8111
const appCaptured = yield* Deferred.make<OrchestrationV2TurnItem>();
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+67
-23
@@ -379,6 +379,14 @@ function isCodexActiveWriterError(error: unknown): boolean {
379
);
380
}
381
382
+function isCodexThreadNotFoundError(error: unknown): boolean {
383
+ return (
384
+ isCodexAppServerRequestError(error) &&
385
+ error.code === -32600 &&
386
+ error.errorMessage.startsWith("thread not found:")
387
+ );
388
+}
389
+
390
function codexTimestamp(seconds: number | null | undefined): DateTime.Utc {
391
return seconds === null || seconds === undefined
392
? DateTime.nowUnsafe()
@@ -2773,7 +2781,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
2781
CodexAppServerInputStreamEndedError: () => Effect.succeed({ terminated: true }),
2782
// Unloading kills terminals, so "thread not found" means nothing remains to stop.
2783
CodexAppServerRequestError: (error) =>
2776
- error.code === -32600 && error.errorMessage.startsWith("thread not found:")
2784
+ isCodexThreadNotFoundError(error)
2785
? Effect.succeed({ terminated: true })
2786
: Effect.fail(error),
2787
}),
@@ -7720,17 +7728,45 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7728
detail,
7729
payload: cause,
7730
});
7731
+ // Codex unloads idle threads; MCP requests against one fail until it is resumed.
7732
+ const withLoadedThread = <A, E, R>(
7733
+ providerThread: OrchestrationV2ProviderThread,
7734
+ threadId: string,
7735
+ request: Effect.Effect<A, E, R>,
7736
+ ) =>
7737
+ request.pipe(
7738
+ Effect.catchIf(isCodexThreadNotFoundError, () =>
7739
+ readMcpSession(providerThread.appThreadId).pipe(
7740
+ Effect.flatMap((mcpSession) =>
7741
+ resumeNativeThread(threadId, {
7742
+ excludeTurns: true,
7743
+ ...codexThreadRuntimeParams({
7744
+ threadId: providerThread.appThreadId,
7745
+ mcpSession,
7746
+ modelSelection: input.modelSelection,
7747
+ runtimePolicy: input.runtimePolicy,
7748
+ }),
7749
+ }),
7750
+ ),
7751
+ Effect.andThen(request),
7752
+ ),
7753
+ ),
7754
+ );
7755
const mcpApps: ProviderAdapter.ProviderAdapterV2McpApps = {
7756
listTools: Effect.fn("CodexAdapterV2.listTools")(
7757
function* (toolsInput) {
7758
const threadId = yield* getNativeThreadId(toolsInput.providerThread);
7727
- const response = yield* ensureInitialized.pipe(
7728
- Effect.andThen(
7729
- client.request("mcpServerStatus/list", {
7730
- threadId,
7731
- serverName: toolsInput.server,
7732
- detail: "toolsAndAuthOnly",
7733
- }),
7759
+ const response = yield* withLoadedThread(
7760
+ toolsInput.providerThread,
7761
+ threadId,
7762
+ ensureInitialized.pipe(
7763
+ Effect.andThen(
7764
+ client.request("mcpServerStatus/list", {
7765
+ threadId,
7766
+ serverName: toolsInput.server,
7767
+ detail: "toolsAndAuthOnly",
7768
+ }),
7769
+ ),
7770
),
7771
);
7772
const server = response.data.find((entry) => entry.name === toolsInput.server);
@@ -7741,14 +7777,18 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7777
callTool: Effect.fn("CodexAdapterV2.callTool")(
7778
function* (callInput) {
7779
const threadId = yield* getNativeThreadId(callInput.providerThread);
7744
- const response = yield* ensureInitialized.pipe(
7745
- Effect.andThen(
7746
- client.request("mcpServer/tool/call", {
7747
- threadId,
7748
- server: callInput.server,
7749
- tool: callInput.tool,
7750
- arguments: callInput.arguments as Schema.Json,
7751
- }),
7780
+ const response = yield* withLoadedThread(
7781
+ callInput.providerThread,
7782
+ threadId,
7783
+ ensureInitialized.pipe(
7784
+ Effect.andThen(
7785
+ client.request("mcpServer/tool/call", {
7786
+ threadId,
7787
+ server: callInput.server,
7788
+ tool: callInput.tool,
7789
+ arguments: callInput.arguments as Schema.Json,
7790
+ }),
7791
+ ),
7792
),
7793
);
7794
return {
@@ -7765,13 +7805,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7805
readResource: Effect.fn("CodexAdapterV2.readResource")(
7806
function* (readInput) {
7807
const threadId = yield* getNativeThreadId(readInput.providerThread);
7768
- const response = yield* ensureInitialized.pipe(
7769
- Effect.andThen(
7770
- client.request("mcpServer/resource/read", {
7771
- threadId,
7772
- server: readInput.server,
7773
- uri: readInput.uri,
7774
- }),
7808
+ const response = yield* withLoadedThread(
7809
+ readInput.providerThread,
7810
+ threadId,
7811
+ ensureInitialized.pipe(
7812
+ Effect.andThen(
7813
+ client.request("mcpServer/resource/read", {
7814
+ threadId,
7815
+ server: readInput.server,
7816
+ uri: readInput.uri,
7817
+ }),
7818
+ ),
7819
),
7820
);
7821
return { contents: response.contents };