Skip to content

Commit b83a441

Browse files
fix(server): Codex protocol records go to the log of the thread they belong to
One app-server serves every Codex thread on an instance, but its protocol logger was bound to the thread that opened it, so every thread's records landed in that one file. Route each frame instead: thread start, resume and fork requests name their T3 thread in per-thread config, their responses map the native thread id, later frames carry that id, and responses follow their request. Frames tied to no thread, such as initialize, go to the global provider log. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0c4b1d8 commit b83a441

2 files changed

Lines changed: 114 additions & 5 deletions

File tree

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ describe("CodexAdapterV2 native protocol logging", () => {
310310
const providerSessionId = ProviderSessionId.make("provider-session-1");
311311
const protocolLogger = CodexAdapterV2.makeCodexAppServerProtocolLogger({
312312
nativeEventLogger: logger,
313-
threadId,
314313
providerSessionId,
315314
});
316315

@@ -319,12 +318,52 @@ describe("CodexAdapterV2 native protocol logging", () => {
319318
return;
320319
}
321320

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+
322360
yield* protocolLogger({
323361
direction: "incoming",
324362
stage: "decoded",
325363
payload: {
326364
method: "thread/event",
327365
params: {
366+
threadId: "native-1",
328367
id: "evt-1",
329368
http_headers: { Authorization: "Bearer secret-codex-token" },
330369
usage: { inputTokens: 12, outputTokens: 8, totalTokens: 20 },
@@ -351,6 +390,7 @@ describe("CodexAdapterV2 native protocol logging", () => {
351390
payload: {
352391
method: "thread/event",
353392
params: {
393+
threadId: "native-1",
354394
id: "evt-1",
355395
http_headers: { Authorization: "[REDACTED]" },
356396
usage: { inputTokens: 12, outputTokens: 8, totalTokens: 20 },

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

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
type ServerProviderCompatibilityAdvisory,
4141
type ServerProviderModel,
4242
isDelegatedAppThread,
43+
ThreadId,
4344
} from "@t3tools/contracts";
4445
import { SKILL_MENTION_PATTERN } from "@t3tools/shared/composerInlineTokens";
4546
import { AgentScope } from "@t3tools/shared/AgentScope";
@@ -78,7 +79,6 @@ import type {
7879
ProviderTurnId,
7980
ProviderInstanceId,
8081
RuntimeMode,
81-
ThreadId,
8282
} from "@t3tools/contracts";
8383
import * as CodexClient from "effect-codex-app-server/client";
8484
import * as CodexErrors from "effect-codex-app-server/errors";
@@ -1544,18 +1544,88 @@ export const makeCodexAppServerSpawnCommand = Effect.fn(
15441544
});
15451545
});
15461546

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+
15471616
export function makeCodexAppServerProtocolLogger(input: {
15481617
readonly nativeEventLogger: ProviderEventLoggers.EventNdjsonLogger | undefined;
1549-
readonly threadId: ThreadId;
15501618
readonly providerSessionId: OrchestrationV2ProviderSession["id"];
15511619
}): CodexClient.CodexAppServerClientOptions["logger"] | undefined {
15521620
const { nativeEventLogger } = input;
15531621
if (nativeEventLogger === undefined) {
15541622
return undefined;
15551623
}
15561624

1625+
const routeThread = makeCodexProtocolThreadRouter();
15571626
return (event) => {
15581627
if (!shouldPersistProviderEvent("native", event)) return Effect.void;
1628+
const threadId = event.stage === "decoded" ? routeThread(event.direction, event.payload) : null;
15591629
return nativeEventLogger
15601630
.write(
15611631
{
@@ -1565,7 +1635,7 @@ export function makeCodexAppServerProtocolLogger(input: {
15651635
providerSessionId: input.providerSessionId,
15661636
event: redactCodexProtocolValue(boundProviderEventForLogging(event)),
15671637
},
1568-
input.threadId,
1638+
threadId,
15691639
)
15701640
.pipe(Effect.ignore);
15711641
};
@@ -1667,7 +1737,6 @@ export const layerAppServerClientFactory: Layer.Layer<
16671737
);
16681738
const protocolLogger = makeCodexAppServerProtocolLogger({
16691739
nativeEventLogger,
1670-
threadId: input.threadId,
16711740
providerSessionId: input.providerSessionId,
16721741
});
16731742
const clientOptions: CodexClient.CodexAppServerClientOptions =

0 commit comments

Comments
 (0)