@@ -57,9 +57,12 @@ it.effect.each([
5757 "manager-release-interrupt" ,
5858 "provider-stopped" ,
5959 "stream-ended" ,
60+ "released-before-subscribe" ,
61+ "released-during-subscribe" ,
6062] as const ) ( "Stop crosses native start: %s" , ( scenario ) =>
6163 Effect . scoped (
6264 Effect . gen ( function * ( ) {
65+ const scope = yield * Effect . scope ;
6366 const cwd = yield * checkpointWorkspace ( `start-stop-${ scenario } ` ) ;
6467 const events = yield * Queue . unbounded < ProviderAdapter . ProviderAdapterV2Event , Cause . Done > ( ) ;
6568 const entered = yield * Deferred . make < void > ( ) ;
@@ -76,6 +79,12 @@ it.effect.each([
7679 const releaseTerminal = yield * Deferred . make < void > ( ) ;
7780 const ownedWait = yield * Deferred . make < void > ( ) ;
7881 const beforeOwnership = yield * Deferred . make < void > ( ) ;
82+ const closeHeld = yield * Deferred . make < void > ( ) ;
83+ const releaseClose = yield * Deferred . make < void > ( ) ;
84+ const lateSubscribed = yield * Deferred . make < void > ( ) ;
85+ const lateSubscriptionClosed = yield * Deferred . make < void > ( ) ;
86+ const lateSubscription =
87+ scenario === "released-before-subscribe" || scenario === "released-during-subscribe" ;
7988 const driver = ProviderDriverKind . make ( "codex" ) ;
8089 const instanceId = ProviderInstanceId . make ( "codex" ) ;
8190 const modelSelection = { instanceId, model : "synthetic" } ;
@@ -97,6 +106,12 @@ it.effect.each([
97106 planSelectionTransition : ( ) => Effect . succeed ( { type : "apply_on_next_turn" } ) ,
98107 openSession : ( input ) =>
99108 Effect . gen ( function * ( ) {
109+ if ( scenario === "released-during-subscribe" )
110+ yield * Effect . addFinalizer ( ( ) =>
111+ Deferred . succeed ( closeHeld , undefined ) . pipe (
112+ Effect . andThen ( Deferred . await ( releaseClose ) ) ,
113+ ) ,
114+ ) ;
100115 const now = yield * DateTime . now ;
101116 const startTurn : ProviderAdapter . ProviderAdapterV2SessionRuntime [ "startTurn" ] = (
102117 turn ,
@@ -281,6 +296,7 @@ it.effect.each([
281296 const outbox = yield * EffectOutbox . EffectOutboxV2 ;
282297 const execution = yield * RunExecutionService . RunExecutionServiceV2 ;
283298 const threads = yield * ThreadManagementService . ThreadManagementService ;
299+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
284300 yield * orchestrator . dispatch ( {
285301 type : "thread.create" ,
286302 commandId : CommandId . make ( "create" ) ,
@@ -310,6 +326,33 @@ it.effect.each([
310326 const originalStart = execution . startRootRun ;
311327 let staleInput : RunExecutionService . RunExecutionServiceV2StartRootRunInput | undefined ;
312328 using _beforeOwnership = vi . spyOn ( execution , "startRootRun" ) . mockImplementation ( ( input ) => {
329+ if ( lateSubscription )
330+ return Effect . gen ( function * ( ) {
331+ const released = yield * manager
332+ . release ( { providerSessionId : input . providerSessionId , reason : "manual_shutdown" } )
333+ . pipe ( Effect . orDie , Effect . forkIn ( scope ) ) ;
334+ if ( scenario === "released-before-subscribe" ) yield * Fiber . join ( released ) ;
335+ else yield * Deferred . await ( closeHeld ) ;
336+ const startup = yield * originalStart ( {
337+ ...input ,
338+ session : {
339+ ...input . session ,
340+ subscribeEvents : input . session . subscribeEvents ! . pipe (
341+ Effect . map ( ( subscription ) => ( {
342+ ...subscription ,
343+ close : subscription . close . pipe (
344+ Effect . tap ( ( ) => Deferred . succeed ( lateSubscriptionClosed , undefined ) ) ,
345+ ) ,
346+ } ) ) ,
347+ Effect . tap ( ( ) => Deferred . succeed ( lateSubscribed , undefined ) ) ,
348+ ) ,
349+ } ,
350+ } ) . pipe ( Effect . forkIn ( scope ) ) ;
351+ yield * Deferred . await ( lateSubscribed ) ;
352+ yield * Deferred . succeed ( releaseClose , undefined ) ;
353+ yield * Fiber . join ( released ) ;
354+ yield * Fiber . join ( startup ) ;
355+ } ) ;
313356 if ( scenario === "unsupported-compact" ) {
314357 const session = { ...input . session } ;
315358 delete session . compactThread ;
@@ -341,7 +384,11 @@ it.effect.each([
341384 } ) ;
342385 yield * send ( "first" ) ;
343386 const startup = yield * worker . drain ( 1 ) . pipe ( Effect . forkScoped ) ;
344- if ( scenario === "unsupported-compact" || scenario === "prompt-refusal" ) {
387+ if (
388+ scenario === "unsupported-compact" ||
389+ scenario === "prompt-refusal" ||
390+ lateSubscription
391+ ) {
345392 yield * Fiber . join ( startup ) ;
346393 const failed = yield * orchestrator . getThreadProjection ( threadId ) ;
347394 const run = failed . runs [ 0 ] ! ;
@@ -359,6 +406,7 @@ it.effect.each([
359406 ) ;
360407 assert . equal ( starts , 0 ) ;
361408 assert . deepEqual ( interrupts , [ ] ) ;
409+ if ( lateSubscription ) assert . isTrue ( yield * Deferred . isDone ( lateSubscriptionClosed ) ) ;
362410 return ;
363411 }
364412 if ( scenario === "before-ownership" ) {
0 commit comments