@@ -23,6 +23,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
2323import { it , vi } from "@effect/vitest" ;
2424
2525import * as Context from "effect/Context" ;
26+ import * as Deferred from "effect/Deferred" ;
2627import * as Effect from "effect/Effect" ;
2728import * as Exit from "effect/Exit" ;
2829import * as Fiber from "effect/Fiber" ;
@@ -75,6 +76,7 @@ class FakeCodexRuntime implements CodexSessionRuntimeShape {
7576 updatedAt : this . now ,
7677 } satisfies ProviderSession ) ,
7778 ) ;
79+ public startEffect : Effect . Effect < ProviderSession > | undefined ;
7880
7981 public readonly sendTurnImpl = vi . fn (
8082 ( _input : CodexSessionRuntimeSendTurnInput ) : Promise < ProviderTurnStartResult > =>
@@ -127,7 +129,7 @@ class FakeCodexRuntime implements CodexSessionRuntimeShape {
127129 }
128130
129131 start ( ) {
130- return Effect . promise ( ( ) => this . startImpl ( ) ) ;
132+ return this . startEffect ?? Effect . promise ( ( ) => this . startImpl ( ) ) ;
131133 }
132134
133135 getSession = Effect . promise ( ( ) => this . startImpl ( ) ) ;
@@ -188,13 +190,24 @@ function makeRuntimeFactory() {
188190function makeScopedRuntimeFactory ( options ?: { readonly failConstruction ?: boolean } ) {
189191 const runtimes : Array < FakeCodexRuntime > = [ ] ;
190192 const releasedThreadIds : Array < ThreadId > = [ ] ;
193+ let exitDuringNextStart :
194+ | {
195+ readonly exitForwarded : Deferred . Deferred < void > ;
196+ readonly scopeReleased : Deferred . Deferred < void > ;
197+ }
198+ | undefined ;
191199
192200 const factory = vi . fn ( ( runtimeOptions : CodexSessionRuntimeOptions ) =>
193201 Effect . gen ( function * ( ) {
202+ const exitDuringStart = exitDuringNextStart ;
203+ exitDuringNextStart = undefined ;
194204 yield * Scope . Scope ;
195205 yield * Effect . addFinalizer ( ( ) =>
196206 Effect . sync ( ( ) => {
197207 releasedThreadIds . push ( runtimeOptions . threadId ) ;
208+ if ( exitDuringStart ) {
209+ Deferred . doneUnsafe ( exitDuringStart . scopeReleased , Effect . void ) ;
210+ }
198211 } ) ,
199212 ) ;
200213
@@ -206,6 +219,23 @@ function makeScopedRuntimeFactory(options?: { readonly failConstruction?: boolea
206219 }
207220
208221 const runtime = new FakeCodexRuntime ( runtimeOptions ) ;
222+ if ( exitDuringStart ) {
223+ const startImpl = runtime . startImpl . getMockImplementation ( ) ;
224+ runtime . startEffect = runtime
225+ . emit ( {
226+ id : asEventId ( "evt-session-exited-during-start" ) ,
227+ kind : "notification" ,
228+ provider : ProviderDriverKind . make ( "codex" ) ,
229+ createdAt : "2026-01-01T00:00:00.000Z" ,
230+ method : "session/exited" ,
231+ threadId : runtimeOptions . threadId ,
232+ message : "Codex App Server exited with code 1." ,
233+ } )
234+ . pipe (
235+ Effect . andThen ( Deferred . await ( exitDuringStart . exitForwarded ) ) ,
236+ Effect . andThen ( Effect . promise ( ( ) => startImpl ! ( ) ) ) ,
237+ ) ;
238+ }
209239 runtimes . push ( runtime ) ;
210240 return runtime ;
211241 } ) ,
@@ -214,6 +244,13 @@ function makeScopedRuntimeFactory(options?: { readonly failConstruction?: boolea
214244 return {
215245 factory,
216246 releasedThreadIds,
247+ exitDuringNextStart : ( ) => {
248+ exitDuringNextStart = {
249+ exitForwarded : Deferred . makeUnsafe < void > ( ) ,
250+ scopeReleased : Deferred . makeUnsafe < void > ( ) ,
251+ } ;
252+ return exitDuringNextStart ;
253+ } ,
217254 get lastRuntime ( ) : FakeCodexRuntime | undefined {
218255 return runtimes . at ( - 1 ) ;
219256 } ,
@@ -2562,6 +2599,49 @@ const scopedLifecycleLayer = it.layer(
25622599) ;
25632600
25642601scopedLifecycleLayer ( "CodexAdapterLive scoped lifecycle" , ( it ) => {
2602+ it . effect ( "cleans up an exit during startup without deleting its replacement" , ( ) =>
2603+ Effect . gen ( function * ( ) {
2604+ scopedLifecycleRuntimeFactory . releasedThreadIds . length = 0 ;
2605+ const signals = scopedLifecycleRuntimeFactory . exitDuringNextStart ( ) ;
2606+ const adapter = yield * CodexAdapter ;
2607+ const threadId = asThreadId ( "thread-exited" ) ;
2608+ const exitedFiber = yield * Stream . runHead (
2609+ Stream . filter ( adapter . streamEvents , ( event ) => event . threadId === threadId ) ,
2610+ ) . pipe (
2611+ Effect . tap ( ( ) => Deferred . succeed ( signals . exitForwarded , undefined ) ) ,
2612+ Effect . forkChild ,
2613+ ) ;
2614+
2615+ yield * adapter . startSession ( {
2616+ provider : ProviderDriverKind . make ( "codex" ) ,
2617+ threadId,
2618+ runtimeMode : "full-access" ,
2619+ } ) ;
2620+
2621+ const runtime = scopedLifecycleRuntimeFactory . lastRuntime ;
2622+ NodeAssert . ok ( runtime ) ;
2623+ const exited = yield * Fiber . join ( exitedFiber ) ;
2624+ NodeAssert . equal ( exited . _tag , "Some" ) ;
2625+ NodeAssert . equal ( yield * adapter . hasSession ( threadId ) , false ) ;
2626+ NodeAssert . deepStrictEqual ( yield * adapter . listSessions ( ) , [ ] ) ;
2627+
2628+ yield * adapter . startSession ( {
2629+ provider : ProviderDriverKind . make ( "codex" ) ,
2630+ threadId,
2631+ runtimeMode : "full-access" ,
2632+ } ) ;
2633+ const replacement = scopedLifecycleRuntimeFactory . lastRuntime ;
2634+ NodeAssert . ok ( replacement ) ;
2635+ NodeAssert . notEqual ( replacement , runtime ) ;
2636+
2637+ yield * Deferred . await ( signals . scopeReleased ) ;
2638+ NodeAssert . equal ( yield * adapter . hasSession ( threadId ) , true ) ;
2639+ NodeAssert . equal ( ( yield * adapter . listSessions ( ) ) . length , 1 ) ;
2640+ NodeAssert . equal ( runtime . closeImpl . mock . calls . length , 1 ) ;
2641+ NodeAssert . deepStrictEqual ( scopedLifecycleRuntimeFactory . releasedThreadIds , [ threadId ] ) ;
2642+ } ) ,
2643+ ) ;
2644+
25652645 it . effect ( "closes the externally owned session scope on stopSession" , ( ) =>
25662646 Effect . gen ( function * ( ) {
25672647 scopedLifecycleRuntimeFactory . releasedThreadIds . length = 0 ;
0 commit comments