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(server): Codex sessions get T3CODE_THREAD_ID like Claude sessions (#28604)
Ruslan Panamarenka committed
Nov 1, 2026 at 23:07 UTC
fa31b66bbb2ae5234205a8628a78e038d917202f
3 files changed
+94
-9
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+75
-2
@@ -486,6 +486,11 @@ const CODEX_TEST_RUNTIME_POLICY = ProviderAdapter.ProviderAdapterV2RuntimePolicy
486
interactionMode: "default",
487
cwd: "/workspace",
488
});
489
+const T3_THREAD_ENV_KEY = "shell_environment_policy.set.T3CODE_THREAD_ID";
490
+const threadEnvOf = (params: unknown) =>
491
+ Predicate.hasProperty(params, "config") && Predicate.hasProperty(params.config, T3_THREAD_ENV_KEY)
492
+ ? params.config[T3_THREAD_ENV_KEY]
493
+ : undefined;
494
495
function makeCodexTestAppThread(input: {
496
readonly threadId: ThreadId;
@@ -1180,6 +1185,13 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1185
onRequest(method, params).pipe(
1186
Effect.andThen(client.request(method, params)),
1187
),
1188
+ raw: {
1189
+ ...client.raw,
1190
+ request: (method, params) =>
1191
+ onRequest(method, params).pipe(
1192
+ Effect.andThen(client.raw.request(method, params)),
1193
+ ),
1194
+ },
1195
}) satisfies CodexClient.CodexAppServerClient["Service"],
1196
),
1197
Effect.provide(context),
@@ -1513,6 +1525,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1525
config: {
1526
"tools.update_plan.enabled": true,
1527
mcp_optional_startup_grace_ms: 10_000,
1528
+ "shell_environment_policy.set.T3CODE_THREAD_ID": `thread-${scenario}`,
1529
"mcp_servers.t3-code": {
1530
url: "http://127.0.0.1:43123/mcp",
1531
tool_timeout_sec: 3_900,
@@ -2508,18 +2521,75 @@ describe("CodexAdapterV2 post-settle continuation", () => {
2521
label: "thread/resume",
2522
frame: { id: 3, result: { thread: { id: nativeThreadId, updatedAt: 1782622450 } } },
2523
},
2524
+ {
2525
+ type: "expect_outbound",
2526
+ label: "thread/resume without an app thread",
2527
+ frame: {
2528
+ id: 4,
2529
+ method: "thread/resume",
2530
+ params: {
2531
+ threadId: nativeThreadId,
2532
+ excludeTurns: true,
2533
+ config: CodexAdapterV2.CODEX_THREAD_CONFIG,
2534
+ },
2535
+ },
2536
+ },
2537
+ {
2538
+ type: "emit_inbound",
2539
+ label: "thread/resume without an app thread",
2540
+ frame: { id: 4, result: { thread: { id: nativeThreadId, updatedAt: 1782622450 } } },
2541
+ },
2542
+ {
2543
+ type: "expect_outbound",
2544
+ label: "thread/resume with an explicit app thread",
2545
+ frame: {
2546
+ id: 5,
2547
+ method: "thread/resume",
2548
+ params: {
2549
+ threadId: nativeThreadId,
2550
+ excludeTurns: true,
2551
+ config: CodexAdapterV2.CODEX_THREAD_CONFIG,
2552
+ },
2553
+ },
2554
+ },
2555
+ {
2556
+ type: "emit_inbound",
2557
+ label: "thread/resume with an explicit app thread",
2558
+ frame: { id: 5, result: { thread: { id: nativeThreadId, updatedAt: 1782622450 } } },
2559
+ },
2560
],
2561
});
2513
- const harness = yield* makeCodexReplayHarness(transcript);
2562
+ const resumeThreadEnvs: Array<unknown> = [];
2563
+ const harness = yield* makeCodexReplayHarness(
2564
+ transcript,
2565
+ () => Effect.void,
2566
+ (method, params) =>
2567
+ Effect.sync(() => {
2568
+ if (method === "thread/resume") resumeThreadEnvs.push(threadEnvOf(params));
2569
+ }),
2570
+ );
2571
const resumed = yield* harness.runtime.resumeThread({
2572
providerThread: harness.providerThread,
2573
modelSelection: CODEX_TEST_MODEL_SELECTION,
2574
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
2575
});
2576
+ yield* harness.runtime.resumeThread({
2577
+ providerThread: { ...harness.providerThread, appThreadId: null },
2578
+ modelSelection: CODEX_TEST_MODEL_SELECTION,
2579
+ runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
2580
+ });
2581
+ const explicitThreadId = ThreadId.make(`thread-${scenario}-explicit`);
2582
+ yield* harness.runtime.resumeThread({
2583
+ threadId: explicitThreadId,
2584
+ providerThread: harness.providerThread,
2585
+ modelSelection: CODEX_TEST_MODEL_SELECTION,
2586
+ runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
2587
+ });
2588
2589
assert.equal(resumed.nativeThreadRef?.nativeId, nativeThreadId);
2590
assert.equal(resumed.status, "idle");
2591
assert.equal(DateTime.toEpochMillis(resumed.updatedAt), 1782622450000);
2592
+ assert.deepEqual(resumeThreadEnvs, [harness.threadId, undefined, explicitThreadId]);
2593
}).pipe(
2594
Effect.provide(
2595
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -7489,12 +7559,14 @@ describe("CodexAdapterV2 post-settle continuation", () => {
7559
],
7560
});
7561
const outbound: Array<string> = [];
7562
+ const forkThreadEnvs: Array<unknown> = [];
7563
const harness = yield* makeCodexReplayHarness(
7564
transcript,
7565
() => Effect.void,
7495
- (method) =>
7566
+ (method, params) =>
7567
Effect.sync(() => {
7568
outbound.push(method);
7569
+ if (method === "thread/fork") forkThreadEnvs.push(threadEnvOf(params));
7570
}),
7571
);
7572
const now = yield* DateTime.now;
@@ -7524,6 +7596,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
7596
assert.notEqual(forkedProviderThread.id, harness.providerThread.id);
7597
assert.equal(forkedProviderThread.forkedFrom?.providerTurnId, firstTurn.id);
7598
assert.deepEqual(outbound.slice(-2), ["thread/turns/list", "thread/revert"]);
7599
+ assert.deepEqual(forkThreadEnvs, ["thread-fork-paginated-fallback-target"]);
7600
}).pipe(
7601
Effect.scoped,
7602
Effect.provide(
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+12
-3
@@ -101,6 +101,7 @@ import * as Stream from "effect/Stream";
101
import { ChildProcess, ChildProcessSpawner } from "effect/process";
102
103
import { resolveAttachmentPath, resolveAttachmentPathById } from "../../attachmentStore.ts";
104
+import * as PortScanner from "../../preview/PortScanner.ts";
105
import { getCodexServiceTierOptionValue } from "../../codexModelOptions.ts";
106
import { ServerConfig } from "../../config.ts";
107
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
@@ -1335,6 +1336,7 @@ export const CODEX_THREAD_CONFIG = {
1336
const CODEX_T3_MCP_TOOL_TIMEOUT_SEC = 65 * 60;
1337
1338
function codexThreadRuntimeParams(input: {
1339
+ readonly threadId: ThreadId | null;
1340
readonly mcpSession: McpProviderSession.McpProviderSessionConfig | undefined;
1341
readonly modelSelection?: { readonly model: string };
1342
readonly runtimePolicy?: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
@@ -1349,6 +1351,10 @@ function codexThreadRuntimeParams(input: {
1351
...(input.modelSelection === undefined ? {} : { model: input.modelSelection.model }),
1352
config: {
1353
...CODEX_THREAD_CONFIG,
1354
+ // One app-server serves many threads, so the id goes in per-thread shell config, not its env.
1355
+ ...(input.threadId === null
1356
+ ? {}
1357
+ : { [`shell_environment_policy.set.${PortScanner.AGENT_THREAD_ENV}`]: input.threadId }),
1358
...(mcpSession === undefined
1359
? {}
1360
: {
@@ -6977,6 +6983,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6983
client.request(
6984
"thread/start",
6985
codexThreadRuntimeParams({
6986
+ threadId: threadInput.threadId,
6987
mcpSession,
6988
modelSelection: threadInput.modelSelection,
6989
runtimePolicy: threadInput.runtimePolicy,
@@ -7005,13 +7012,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7012
resumeThread: Effect.fn("CodexAdapterV2.resumeThread")(
7013
function* (threadInput) {
7014
const nativeThreadId = yield* getNativeThreadId(threadInput.providerThread);
7015
+ const appThreadId = threadInput.threadId ?? threadInput.providerThread.appThreadId;
7016
const resume = client.raw.request("thread/resume", {
7017
threadId: nativeThreadId,
7018
excludeTurns: true,
7019
...codexThreadRuntimeParams({
7012
- mcpSession: yield* readMcpSession(
7013
- threadInput.threadId ?? threadInput.providerThread.appThreadId,
7014
- ),
7020
+ threadId: appThreadId,
7021
+ mcpSession: yield* readMcpSession(appThreadId),
7022
...(threadInput.modelSelection === undefined
7023
? {}
7024
: { modelSelection: threadInput.modelSelection }),
@@ -7796,6 +7803,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7803
threadId,
7804
excludeTurns: true,
7805
...codexThreadRuntimeParams({
7806
+ threadId: threadInput.providerThread.appThreadId,
7807
mcpSession: yield* readMcpSession(threadInput.providerThread.appThreadId),
7808
modelSelection: input.modelSelection,
7809
runtimePolicy: input.runtimePolicy,
@@ -7848,6 +7856,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7856
? {}
7857
: { lastTurnId: boundary.lastTurnId }),
7858
...codexThreadRuntimeParams({
7859
+ threadId: threadInput.targetThreadId,
7860
mcpSession: yield* mcpSessions.read(threadInput.targetThreadId),
7861
...(threadInput.modelSelection === undefined
7862
? {}
packages/effect-codex-app-server/src/replay.ts
+7
-4
@@ -237,9 +237,9 @@ function recorderThreadConfigKeys(transcript: CodexAppServerReplayTranscript): R
237
}
238
239
/**
240
- * `cwd`, `model` and host-supplied MCP servers (machine-local URLs, short-lived
241
- * authorization headers) vary per machine, and recorder-only keys are not the
242
- * client's.
240
+ * `cwd`, `model`, host-supplied MCP servers (machine-local URLs, short-lived
241
+ * authorization headers) and the T3 thread id vary per machine or run, and
242
+ * recorder-only keys are not the client's.
243
*/
244
function normalizeThreadRequest(frame: unknown, ignoredConfigKeys: ReadonlySet<string>): unknown {
245
if (typeof frame !== "object" || frame === null) return frame;
@@ -257,7 +257,10 @@ function normalizeThreadRequest(frame: unknown, ignoredConfigKeys: ReadonlySet<s
257
if (typeof config !== "object" || config === null) return { ...record, params };
258
const owned = Object.fromEntries(
259
Object.entries(config as Record<string, unknown>).filter(
260
- ([key]) => key !== "mcp_servers" && !key.startsWith("mcp_servers."),
260
+ ([key]) =>
261
+ key !== "mcp_servers" &&
262
+ !key.startsWith("mcp_servers.") &&
263
+ key !== "shell_environment_policy.set.T3CODE_THREAD_ID",
264
),
265
);
266
for (const key of ignoredConfigKeys) delete owned[key];