The best way to control Claude Code, Codex and more from any computer or phone
fix(server): Codex protocol records go to the log of the thread they belong to (#35975)
Julius Marminge committed
Nov 12, 2026 at 20:55 UTC
8fe745d7afc6cfa5758c0b0614d03a242ba0ecc2
2 files changed
+114
-5
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+41
-1
@@ -310,7 +310,6 @@ describe("CodexAdapterV2 native protocol logging", () => {
310
const providerSessionId = ProviderSessionId.make("provider-session-1");
311
const protocolLogger = CodexAdapterV2.makeCodexAppServerProtocolLogger({
312
nativeEventLogger: logger,
313
- threadId,
313
providerSessionId,
314
});
315
@@ -319,12 +318,52 @@ describe("CodexAdapterV2 native protocol logging", () => {
318
return;
319
}
320
321
+ const startThread = (requestId: number, appThreadId: string, nativeThreadId: string) =>
322
+ Effect.gen(function* () {
323
+ yield* protocolLogger({
324
+ direction: "outgoing",
325
+ stage: "decoded",
326
+ payload: {
327
+ id: requestId,
328
+ method: "thread/start",
329
+ params: { config: { "shell_environment_policy.set.T3CODE_THREAD_ID": appThreadId } },
330
+ },
331
+ });
332
+ yield* protocolLogger({
333
+ direction: "incoming",
334
+ stage: "decoded",
335
+ payload: { id: requestId, result: { thread: { id: nativeThreadId } } },
336
+ });
337
+ });
338
+ yield* startThread(1, "thread-1", "native-1");
339
+ yield* startThread(2, "thread-2", "native-2");
340
+ yield* protocolLogger({
341
+ direction: "outgoing",
342
+ stage: "decoded",
343
+ payload: { id: 3, method: "turn/start", params: { threadId: "native-2" } },
344
+ });
345
+ yield* protocolLogger({
346
+ direction: "incoming",
347
+ stage: "decoded",
348
+ payload: { id: 3, result: { turn: { id: "turn-2" } } },
349
+ });
350
+ yield* protocolLogger({
351
+ direction: "incoming",
352
+ stage: "decoded",
353
+ payload: { id: 4, result: { userAgent: "codex/1.0.0" } },
354
+ });
355
+ assert.deepEqual(
356
+ writes.splice(0).map((write) => write.threadId),
357
+ ["thread-1", "thread-1", "thread-2", "thread-2", "thread-2", "thread-2", null],
358
+ );
359
+
360
yield* protocolLogger({
361
direction: "incoming",
362
stage: "decoded",
363
payload: {
364
method: "thread/event",
365
params: {
366
+ threadId: "native-1",
367
id: "evt-1",
368
http_headers: { Authorization: "Bearer secret-codex-token" },
369
usage: { inputTokens: 12, outputTokens: 8, totalTokens: 20 },
@@ -351,6 +390,7 @@ describe("CodexAdapterV2 native protocol logging", () => {
390
payload: {
391
method: "thread/event",
392
params: {
393
+ threadId: "native-1",
394
id: "evt-1",
395
http_headers: { Authorization: "[REDACTED]" },
396
usage: { inputTokens: 12, outputTokens: 8, totalTokens: 20 },
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+73
-4
@@ -40,6 +40,7 @@ import {
40
type ServerProviderCompatibilityAdvisory,
41
type ServerProviderModel,
42
isDelegatedAppThread,
43
+ ThreadId,
44
} from "@t3tools/contracts";
45
import { SKILL_MENTION_PATTERN } from "@t3tools/shared/composerInlineTokens";
46
import { AgentScope } from "@t3tools/shared/AgentScope";
@@ -78,7 +79,6 @@ import type {
79
ProviderTurnId,
80
ProviderInstanceId,
81
RuntimeMode,
81
- ThreadId,
82
} from "@t3tools/contracts";
83
import * as CodexClient from "effect-codex-app-server/client";
84
import * as CodexErrors from "effect-codex-app-server/errors";
@@ -1544,9 +1544,77 @@ export const makeCodexAppServerSpawnCommand = Effect.fn(
1544
});
1545
});
1546
1547
+function readCodexProtocolPath(value: unknown, ...path: ReadonlyArray<string>): unknown {
1548
+ let current = value;
1549
+ for (const key of path) {
1550
+ if (current === null || typeof current !== "object") return undefined;
1551
+ current = Reflect.get(current, key);
1552
+ }
1553
+ return current;
1554
+}
1555
+
1556
+const CODEX_THREAD_ENV_CONFIG_KEY = `shell_environment_policy.set.${PortScanner.AGENT_THREAD_ENV}`;
1557
+
1558
+/**
1559
+ * One app-server serves every Codex thread on an instance, so each frame is routed to the
1560
+ * T3 thread it belongs to. Frames tied to no thread, such as `initialize`, go to the global log.
1561
+ */
1562
+function makeCodexProtocolThreadRouter() {
1563
+ const threadIdsByNativeId = new Map<string, ThreadId>();
1564
+ const threadIdsByRequest = new Map<string, ThreadId>();
1565
+ return (direction: "incoming" | "outgoing", frame: unknown): ThreadId | null => {
1566
+ const id = readCodexProtocolPath(frame, "id");
1567
+ const method = readCodexProtocolPath(frame, "method");
1568
+ const isRequest = typeof method === "string";
1569
+ const requestKey =
1570
+ typeof id === "string" || typeof id === "number"
1571
+ ? `${isRequest === (direction === "outgoing") ? "client" : "server"}:${id}`
1572
+ : undefined;
1573
+ if (!isRequest) {
1574
+ if (requestKey === undefined) return null;
1575
+ const threadId = threadIdsByRequest.get(requestKey);
1576
+ threadIdsByRequest.delete(requestKey);
1577
+ const nativeThreadId = readCodexProtocolPath(frame, "result", "thread", "id");
1578
+ if (threadId !== undefined && typeof nativeThreadId === "string") {
1579
+ threadIdsByNativeId.set(nativeThreadId, threadId);
1580
+ }
1581
+ return threadId ?? null;
1582
+ }
1583
+
1584
+ const params = readCodexProtocolPath(frame, "params");
1585
+ const configuredThreadId = readCodexProtocolPath(params, "config", CODEX_THREAD_ENV_CONFIG_KEY);
1586
+ const nativeThreadId =
1587
+ readCodexProtocolPath(params, "threadId") ?? readCodexProtocolPath(params, "thread", "id");
1588
+ let threadId =
1589
+ typeof configuredThreadId === "string"
1590
+ ? ThreadId.make(configuredThreadId)
1591
+ : typeof nativeThreadId === "string"
1592
+ ? threadIdsByNativeId.get(nativeThreadId)
1593
+ : undefined;
1594
+ if (threadId === undefined && typeof nativeThreadId === "string") {
1595
+ const parentNativeThreadId = readCodexProtocolPath(
1596
+ params,
1597
+ "thread",
1598
+ "source",
1599
+ "subAgent",
1600
+ "thread_spawn",
1601
+ "parent_thread_id",
1602
+ );
1603
+ threadId =
1604
+ typeof parentNativeThreadId === "string"
1605
+ ? threadIdsByNativeId.get(parentNativeThreadId)
1606
+ : undefined;
1607
+ if (threadId !== undefined) threadIdsByNativeId.set(nativeThreadId, threadId);
1608
+ }
1609
+ if (threadId !== undefined && requestKey !== undefined) {
1610
+ threadIdsByRequest.set(requestKey, threadId);
1611
+ }
1612
+ return threadId ?? null;
1613
+ };
1614
+}
1615
+
1616
export function makeCodexAppServerProtocolLogger(input: {
1617
readonly nativeEventLogger: ProviderEventLoggers.EventNdjsonLogger | undefined;
1549
- readonly threadId: ThreadId;
1618
readonly providerSessionId: OrchestrationV2ProviderSession["id"];
1619
}): CodexClient.CodexAppServerClientOptions["logger"] | undefined {
1620
const { nativeEventLogger } = input;
@@ -1554,8 +1622,10 @@ export function makeCodexAppServerProtocolLogger(input: {
1622
return undefined;
1623
}
1624
1625
+ const routeThread = makeCodexProtocolThreadRouter();
1626
return (event) => {
1627
if (!shouldPersistProviderEvent("native", event)) return Effect.void;
1628
+ const threadId = event.stage === "decoded" ? routeThread(event.direction, event.payload) : null;
1629
return nativeEventLogger
1630
.write(
1631
{
@@ -1565,7 +1635,7 @@ export function makeCodexAppServerProtocolLogger(input: {
1635
providerSessionId: input.providerSessionId,
1636
event: redactCodexProtocolValue(boundProviderEventForLogging(event)),
1637
},
1568
- input.threadId,
1638
+ threadId,
1639
)
1640
.pipe(Effect.ignore);
1641
};
@@ -1667,7 +1737,6 @@ export const layerAppServerClientFactory: Layer.Layer<
1737
);
1738
const protocolLogger = makeCodexAppServerProtocolLogger({
1739
nativeEventLogger,
1670
- threadId: input.threadId,
1740
providerSessionId: input.providerSessionId,
1741
});
1742
const clientOptions: CodexClient.CodexAppServerClientOptions =