Skip to content

Commit c99e933

Browse files
committed
fix(server): Codex sessions get T3CODE_THREAD_ID like Claude sessions
One app-server serves every Codex thread, so the id cannot go in its process env. Set it per thread through shell_environment_policy in the thread/start, thread/resume and thread/fork config instead. Closes #28542
1 parent 460b88b commit c99e933

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
15131513
config: {
15141514
"tools.update_plan.enabled": true,
15151515
mcp_optional_startup_grace_ms: 10_000,
1516+
"shell_environment_policy.set.T3CODE_THREAD_ID": `thread-${scenario}`,
15161517
"mcp_servers.t3-code": {
15171518
url: "http://127.0.0.1:43123/mcp",
15181519
tool_timeout_sec: 3_900,

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import * as Stream from "effect/Stream";
101101
import { ChildProcess, ChildProcessSpawner } from "effect/process";
102102

103103
import { resolveAttachmentPath, resolveAttachmentPathById } from "../../attachmentStore.ts";
104+
import * as PortScanner from "../../preview/PortScanner.ts";
104105
import { getCodexServiceTierOptionValue } from "../../codexModelOptions.ts";
105106
import { ServerConfig } from "../../config.ts";
106107
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
@@ -1334,6 +1335,7 @@ export const CODEX_THREAD_CONFIG = {
13341335
const CODEX_T3_MCP_TOOL_TIMEOUT_SEC = 65 * 60;
13351336

13361337
function codexThreadRuntimeParams(input: {
1338+
readonly threadId: ThreadId | null;
13371339
readonly mcpSession: McpProviderSession.McpProviderSessionConfig | undefined;
13381340
readonly modelSelection?: { readonly model: string };
13391341
readonly runtimePolicy?: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
@@ -1348,6 +1350,10 @@ function codexThreadRuntimeParams(input: {
13481350
...(input.modelSelection === undefined ? {} : { model: input.modelSelection.model }),
13491351
config: {
13501352
...CODEX_THREAD_CONFIG,
1353+
// One app-server serves many threads, so the id goes in per-thread shell config, not its env.
1354+
...(input.threadId === null
1355+
? {}
1356+
: { [`shell_environment_policy.set.${PortScanner.AGENT_THREAD_ENV}`]: input.threadId }),
13511357
...(mcpSession === undefined
13521358
? {}
13531359
: {
@@ -6957,6 +6963,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
69576963
client.request(
69586964
"thread/start",
69596965
codexThreadRuntimeParams({
6966+
threadId: threadInput.threadId,
69606967
mcpSession,
69616968
modelSelection: threadInput.modelSelection,
69626969
runtimePolicy: threadInput.runtimePolicy,
@@ -6985,13 +6992,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
69856992
resumeThread: Effect.fn("CodexAdapterV2.resumeThread")(
69866993
function* (threadInput) {
69876994
const nativeThreadId = yield* getNativeThreadId(threadInput.providerThread);
6995+
const appThreadId = threadInput.threadId ?? threadInput.providerThread.appThreadId;
69886996
const resume = client.raw.request("thread/resume", {
69896997
threadId: nativeThreadId,
69906998
excludeTurns: true,
69916999
...codexThreadRuntimeParams({
6992-
mcpSession: yield* readMcpSession(
6993-
threadInput.threadId ?? threadInput.providerThread.appThreadId,
6994-
),
7000+
threadId: appThreadId,
7001+
mcpSession: yield* readMcpSession(appThreadId),
69957002
...(threadInput.modelSelection === undefined
69967003
? {}
69977004
: { modelSelection: threadInput.modelSelection }),
@@ -7776,6 +7783,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
77767783
threadId,
77777784
excludeTurns: true,
77787785
...codexThreadRuntimeParams({
7786+
threadId: threadInput.providerThread.appThreadId,
77797787
mcpSession: yield* readMcpSession(threadInput.providerThread.appThreadId),
77807788
modelSelection: input.modelSelection,
77817789
runtimePolicy: input.runtimePolicy,
@@ -7828,6 +7836,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
78287836
? {}
78297837
: { lastTurnId: boundary.lastTurnId }),
78307838
...codexThreadRuntimeParams({
7839+
threadId: threadInput.targetThreadId,
78317840
mcpSession: yield* mcpSessions.read(threadInput.targetThreadId),
78327841
...(threadInput.modelSelection === undefined
78337842
? {}

‎packages/effect-codex-app-server/src/replay.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ function recorderThreadConfigKeys(transcript: CodexAppServerReplayTranscript): R
237237
}
238238

239239
/**
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.
243243
*/
244244
function normalizeThreadRequest(frame: unknown, ignoredConfigKeys: ReadonlySet<string>): unknown {
245245
if (typeof frame !== "object" || frame === null) return frame;
@@ -257,7 +257,10 @@ function normalizeThreadRequest(frame: unknown, ignoredConfigKeys: ReadonlySet<s
257257
if (typeof config !== "object" || config === null) return { ...record, params };
258258
const owned = Object.fromEntries(
259259
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",
261264
),
262265
);
263266
for (const key of ignoredConfigKeys) delete owned[key];

0 commit comments

Comments
 (0)