@@ -1657,9 +1657,8 @@ describe("CodexAdapterV2 post-settle continuation", () => {
16571657 return yield * Effect . die ( `Timed out waiting for ${ label } .` ) ;
16581658 } ) ;
16591659
1660- const makeCodexReplayHarness = (
1660+ const makeCodexReplayRuntime = (
16611661 transcript : CodexReplay . CodexAppServerReplayTranscript ,
1662- onEvent : ( event : ProviderAdapterV2Event ) => Effect . Effect < unknown > = ( ) => Effect . void ,
16631662 onRequest : ( method : string , params : unknown ) => Effect . Effect < void > = ( ) => Effect . void ,
16641663 readChildMetadata ?: ( threadId : string ) => Effect . Effect < unknown > ,
16651664 ) =>
@@ -1721,6 +1720,21 @@ describe("CodexAdapterV2 post-settle continuation", () => {
17211720 modelSelection : CODEX_TEST_MODEL_SELECTION ,
17221721 runtimePolicy : CODEX_TEST_RUNTIME_POLICY ,
17231722 } ) ;
1723+ return { runtime, threadId, continuationRequests } ;
1724+ } ) ;
1725+
1726+ const makeCodexReplayHarness = (
1727+ transcript : CodexReplay . CodexAppServerReplayTranscript ,
1728+ onEvent : ( event : ProviderAdapterV2Event ) => Effect . Effect < unknown > = ( ) => Effect . void ,
1729+ onRequest : ( method : string , params : unknown ) => Effect . Effect < void > = ( ) => Effect . void ,
1730+ readChildMetadata ?: ( threadId : string ) => Effect . Effect < unknown > ,
1731+ ) =>
1732+ Effect . gen ( function * ( ) {
1733+ const { runtime, threadId, continuationRequests } = yield * makeCodexReplayRuntime (
1734+ transcript ,
1735+ onRequest ,
1736+ readChildMetadata ,
1737+ ) ;
17241738 const providerThread = yield * runtime . ensureThread ( {
17251739 threadId,
17261740 modelSelection : CODEX_TEST_MODEL_SELECTION ,
@@ -2078,6 +2092,127 @@ describe("CodexAdapterV2 post-settle continuation", () => {
20782092 } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
20792093 ) ;
20802094
2095+ it . effect . each ( [ false , true ] ) (
2096+ "preserves native sessions during concurrent cold resumes (initialization fails: %s)" ,
2097+ ( initializationFails ) =>
2098+ Effect . gen ( function * ( ) {
2099+ const scenario = `concurrent-resume-${ initializationFails } ` ;
2100+ const nativeIds = [ "native-resume-first" , "native-resume-second" ] as const ;
2101+ const handshake = codexReplayPreamble ( {
2102+ nativeThreadId : "unused" ,
2103+ nativeTurnId : "unused" ,
2104+ prompt : "unused" ,
2105+ } ) . slice ( 0 , 3 ) ;
2106+ const transcript = makeCodexReplayTranscript ( {
2107+ scenario,
2108+ entries : [
2109+ ...( initializationFails
2110+ ? [
2111+ handshake [ 0 ] ! ,
2112+ {
2113+ type : "emit_inbound" as const ,
2114+ frame : { id : 1 , error : { code : - 32603 , message : "Initialization failed" } } ,
2115+ } ,
2116+ ]
2117+ : [ ] ) ,
2118+ ...handshake . map ( ( entry ) =>
2119+ initializationFails &&
2120+ "frame" in entry &&
2121+ Predicate . isObject ( entry . frame ) &&
2122+ "id" in entry . frame
2123+ ? { ...entry , frame : { ...entry . frame , id : 2 } }
2124+ : entry ,
2125+ ) ,
2126+ ...( initializationFails ? nativeIds . toReversed ( ) : nativeIds ) . flatMap (
2127+ ( nativeId , index ) : Array < CodexReplay . CodexAppServerReplayEntry > => [
2128+ {
2129+ type : "expect_outbound" ,
2130+ frame : {
2131+ id : index + ( initializationFails ? 3 : 2 ) ,
2132+ method : "thread/resume" ,
2133+ params : {
2134+ threadId : nativeId ,
2135+ excludeTurns : true ,
2136+ config : CodexAdapterV2 . CODEX_THREAD_CONFIG ,
2137+ } ,
2138+ } ,
2139+ } ,
2140+ {
2141+ type : "emit_inbound" ,
2142+ frame : {
2143+ id : index + ( initializationFails ? 3 : 2 ) ,
2144+ result : { thread : { id : nativeId , updatedAt : 1782622450 } } ,
2145+ } ,
2146+ } ,
2147+ ] ,
2148+ ) ,
2149+ ] ,
2150+ } ) ;
2151+ const initializationStarted = yield * Deferred . make < void > ( ) ;
2152+ const releaseInitialization = yield * Deferred . make < void > ( ) ;
2153+ const secondStarted = yield * Deferred . make < void > ( ) ;
2154+ let initializeCount = 0 ;
2155+ const { runtime } = yield * makeCodexReplayRuntime ( transcript , ( method ) =>
2156+ Effect . gen ( function * ( ) {
2157+ if ( method !== "initialize" ) return ;
2158+ initializeCount ++ ;
2159+ yield * Deferred . succeed ( initializationStarted , undefined ) ;
2160+ yield * Deferred . await ( releaseInitialization ) ;
2161+ } ) ,
2162+ ) ;
2163+ const now = yield * DateTime . now ;
2164+ const resume = ( nativeId : string ) =>
2165+ runtime . resumeThread ( {
2166+ providerThread : {
2167+ id : ProviderThreadId . make ( `provider-${ nativeId } ` ) ,
2168+ driver : CodexAdapterV2 . CODEX_DRIVER_KIND ,
2169+ providerInstanceId : CodexAdapterV2 . CODEX_DEFAULT_INSTANCE_ID ,
2170+ providerSessionId : runtime . providerSession . id ,
2171+ appThreadId : ThreadId . make ( `app-${ nativeId } ` ) ,
2172+ ownerNodeId : null ,
2173+ nativeThreadRef : {
2174+ driver : CodexAdapterV2 . CODEX_DRIVER_KIND ,
2175+ nativeId,
2176+ strength : "strong" ,
2177+ } ,
2178+ nativeConversationHeadRef : null ,
2179+ status : "idle" ,
2180+ firstRunOrdinal : 1 ,
2181+ lastRunOrdinal : 1 ,
2182+ handoffIds : [ ] ,
2183+ forkedFrom : null ,
2184+ createdAt : now ,
2185+ updatedAt : now ,
2186+ } ,
2187+ modelSelection : CODEX_TEST_MODEL_SELECTION ,
2188+ runtimePolicy : CODEX_TEST_RUNTIME_POLICY ,
2189+ } ) ;
2190+ const first = yield * resume ( nativeIds [ 0 ] ) . pipe ( Effect . result , Effect . forkChild ) ;
2191+ yield * Deferred . await ( initializationStarted ) ;
2192+ const second = yield * Deferred . succeed ( secondStarted , undefined ) . pipe (
2193+ Effect . andThen ( resume ( nativeIds [ 1 ] ) ) ,
2194+ Effect . forkChild ,
2195+ ) ;
2196+ yield * Deferred . await ( secondStarted ) ;
2197+ assert . equal ( initializeCount , 1 ) ;
2198+ yield * Deferred . succeed ( releaseInitialization , undefined ) ;
2199+ const firstResult = yield * Fiber . join ( first ) ;
2200+ const resumedSecond = yield * Fiber . join ( second ) ;
2201+ assert . equal ( resumedSecond . nativeThreadRef ?. nativeId , nativeIds [ 1 ] ) ;
2202+ if ( initializationFails ) {
2203+ assert . equal ( firstResult . _tag , "Failure" ) ;
2204+ const retried = yield * resume ( nativeIds [ 0 ] ) ;
2205+ assert . equal ( retried . nativeThreadRef ?. nativeId , nativeIds [ 0 ] ) ;
2206+ } else {
2207+ assert . equal ( firstResult . _tag , "Success" ) ;
2208+ if ( firstResult . _tag === "Success" ) {
2209+ assert . equal ( firstResult . success . nativeThreadRef ?. nativeId , nativeIds [ 0 ] ) ;
2210+ }
2211+ }
2212+ assert . equal ( initializeCount , initializationFails ? 2 : 1 ) ;
2213+ } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( IdAllocator . layer , NodeServices . layer ) ) ) ,
2214+ ) ;
2215+
20812216 it . effect ( "identifies sessions to Codex with the same client info as main" , ( ) =>
20822217 Effect . gen ( function * ( ) {
20832218 const transcript = makeCodexReplayTranscript ( {
0 commit comments