@@ -40,6 +40,7 @@ import {
4040 type ServerProviderCompatibilityAdvisory ,
4141 type ServerProviderModel ,
4242 isDelegatedAppThread ,
43+ ThreadId ,
4344} from "@t3tools/contracts" ;
4445import { SKILL_MENTION_PATTERN } from "@t3tools/shared/composerInlineTokens" ;
4546import { AgentScope } from "@t3tools/shared/AgentScope" ;
@@ -78,7 +79,6 @@ import type {
7879 ProviderTurnId ,
7980 ProviderInstanceId ,
8081 RuntimeMode ,
81- ThreadId ,
8282} from "@t3tools/contracts" ;
8383import * as CodexClient from "effect-codex-app-server/client" ;
8484import * 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+
15471616export 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