Skip to content

Commit fa31b66

Browse files
authored
fix(server): Codex sessions get T3CODE_THREAD_ID like Claude sessions (pingdotgg#28604)
1 parent 6014358 commit fa31b66

3 files changed

Lines changed: 94 additions & 9 deletions

File tree

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

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ const CODEX_TEST_RUNTIME_POLICY = ProviderAdapter.ProviderAdapterV2RuntimePolicy
486486
interactionMode: "default",
487487
cwd: "/workspace",
488488
});
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;
489494

490495
function makeCodexTestAppThread(input: {
491496
readonly threadId: ThreadId;
@@ -1180,6 +1185,13 @@ describe("CodexAdapterV2 post-settle continuation", () => {
11801185
onRequest(method, params).pipe(
11811186
Effect.andThen(client.request(method, params)),
11821187
),
1188+
raw: {
1189+
...client.raw,
1190+
request: (method, params) =>
1191+
onRequest(method, params).pipe(
1192+
Effect.andThen(client.raw.request(method, params)),
1193+
),
1194+
},
11831195
}) satisfies CodexClient.CodexAppServerClient["Service"],
11841196
),
11851197
Effect.provide(context),
@@ -1513,6 +1525,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
15131525
config: {
15141526
"tools.update_plan.enabled": true,
15151527
mcp_optional_startup_grace_ms: 10_000,
1528+
"shell_environment_policy.set.T3CODE_THREAD_ID": `thread-${scenario}`,
15161529
"mcp_servers.t3-code": {
15171530
url: "http://127.0.0.1:43123/mcp",
15181531
tool_timeout_sec: 3_900,
@@ -2508,18 +2521,75 @@ describe("CodexAdapterV2 post-settle continuation", () => {
25082521
label: "thread/resume",
25092522
frame: { id: 3, result: { thread: { id: nativeThreadId, updatedAt: 1782622450 } } },
25102523
},
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+
},
25112560
],
25122561
});
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+
);
25142571
const resumed = yield* harness.runtime.resumeThread({
25152572
providerThread: harness.providerThread,
25162573
modelSelection: CODEX_TEST_MODEL_SELECTION,
25172574
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
25182575
});
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+
});
25192588

25202589
assert.equal(resumed.nativeThreadRef?.nativeId, nativeThreadId);
25212590
assert.equal(resumed.status, "idle");
25222591
assert.equal(DateTime.toEpochMillis(resumed.updatedAt), 1782622450000);
2592+
assert.deepEqual(resumeThreadEnvs, [harness.threadId, undefined, explicitThreadId]);
25232593
}).pipe(
25242594
Effect.provide(
25252595
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -7489,12 +7559,14 @@ describe("CodexAdapterV2 post-settle continuation", () => {
74897559
],
74907560
});
74917561
const outbound: Array<string> = [];
7562+
const forkThreadEnvs: Array<unknown> = [];
74927563
const harness = yield* makeCodexReplayHarness(
74937564
transcript,
74947565
() => Effect.void,
7495-
(method) =>
7566+
(method, params) =>
74967567
Effect.sync(() => {
74977568
outbound.push(method);
7569+
if (method === "thread/fork") forkThreadEnvs.push(threadEnvOf(params));
74987570
}),
74997571
);
75007572
const now = yield* DateTime.now;
@@ -7524,6 +7596,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
75247596
assert.notEqual(forkedProviderThread.id, harness.providerThread.id);
75257597
assert.equal(forkedProviderThread.forkedFrom?.providerTurnId, firstTurn.id);
75267598
assert.deepEqual(outbound.slice(-2), ["thread/turns/list", "thread/revert"]);
7599+
assert.deepEqual(forkThreadEnvs, ["thread-fork-paginated-fallback-target"]);
75277600
}).pipe(
75287601
Effect.scoped,
75297602
Effect.provide(

‎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";
@@ -1335,6 +1336,7 @@ export const CODEX_THREAD_CONFIG = {
13351336
const CODEX_T3_MCP_TOOL_TIMEOUT_SEC = 65 * 60;
13361337

13371338
function codexThreadRuntimeParams(input: {
1339+
readonly threadId: ThreadId | null;
13381340
readonly mcpSession: McpProviderSession.McpProviderSessionConfig | undefined;
13391341
readonly modelSelection?: { readonly model: string };
13401342
readonly runtimePolicy?: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
@@ -1349,6 +1351,10 @@ function codexThreadRuntimeParams(input: {
13491351
...(input.modelSelection === undefined ? {} : { model: input.modelSelection.model }),
13501352
config: {
13511353
...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 }),
13521358
...(mcpSession === undefined
13531359
? {}
13541360
: {
@@ -6977,6 +6983,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
69776983
client.request(
69786984
"thread/start",
69796985
codexThreadRuntimeParams({
6986+
threadId: threadInput.threadId,
69806987
mcpSession,
69816988
modelSelection: threadInput.modelSelection,
69826989
runtimePolicy: threadInput.runtimePolicy,
@@ -7005,13 +7012,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
70057012
resumeThread: Effect.fn("CodexAdapterV2.resumeThread")(
70067013
function* (threadInput) {
70077014
const nativeThreadId = yield* getNativeThreadId(threadInput.providerThread);
7015+
const appThreadId = threadInput.threadId ?? threadInput.providerThread.appThreadId;
70087016
const resume = client.raw.request("thread/resume", {
70097017
threadId: nativeThreadId,
70107018
excludeTurns: true,
70117019
...codexThreadRuntimeParams({
7012-
mcpSession: yield* readMcpSession(
7013-
threadInput.threadId ?? threadInput.providerThread.appThreadId,
7014-
),
7020+
threadId: appThreadId,
7021+
mcpSession: yield* readMcpSession(appThreadId),
70157022
...(threadInput.modelSelection === undefined
70167023
? {}
70177024
: { modelSelection: threadInput.modelSelection }),
@@ -7796,6 +7803,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
77967803
threadId,
77977804
excludeTurns: true,
77987805
...codexThreadRuntimeParams({
7806+
threadId: threadInput.providerThread.appThreadId,
77997807
mcpSession: yield* readMcpSession(threadInput.providerThread.appThreadId),
78007808
modelSelection: input.modelSelection,
78017809
runtimePolicy: input.runtimePolicy,
@@ -7848,6 +7856,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
78487856
? {}
78497857
: { lastTurnId: boundary.lastTurnId }),
78507858
...codexThreadRuntimeParams({
7859+
threadId: threadInput.targetThreadId,
78517860
mcpSession: yield* mcpSessions.read(threadInput.targetThreadId),
78527861
...(threadInput.modelSelection === undefined
78537862
? {}

‎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)