@@ -3742,9 +3742,11 @@ it.effect("does not refresh pull requests for auxiliary or stale provider termin
37423742 { ...ids , rootProviderTurnId : ProviderTurnId . make ( "turn:previous" ) } ,
37433743 "interrupted" ,
37443744 ) ,
3745+ rootTerminalEvent ( ids , "cancelled" ) ,
37453746 ] ) ,
37463747 } ) ;
3747- assert . deepEqual ( observed , [ ] ) ;
3748+ // Only the root terminal settles the run and refreshes pull requests.
3749+ assert . deepEqual ( observed , [ "run:cancelled" , "pull-requests-refreshed" ] ) ;
37483750 } ) ,
37493751) ;
37503752
@@ -3772,6 +3774,58 @@ it.effect("refreshes pull requests after a provider stream exits with an error",
37723774 } ) ,
37733775) ;
37743776
3777+ it . effect ( "fails the run when its event stream ends without a root terminal" , ( ) =>
3778+ Effect . gen ( function * ( ) {
3779+ const { observed, finalEvents } = yield * captureRootRunTermination ( {
3780+ key : "stream-end-without-terminal" ,
3781+ shouldFinalizeRun : ( ) => Effect . succeed ( true ) ,
3782+ events : ( ) => Stream . empty ,
3783+ } ) ;
3784+ assert . deepEqual ( observed , [ "run:failed" , "pull-requests-refreshed" ] ) ;
3785+ assert . equal (
3786+ finalEvents . find ( ( event ) => event . type === "provider-thread.updated" ) ?. payload . status ,
3787+ "idle" ,
3788+ ) ;
3789+ } ) ,
3790+ ) ;
3791+
3792+ it . effect ( "leaves the run to restart recovery when shutdown interrupts its event stream" , ( ) =>
3793+ Effect . gen ( function * ( ) {
3794+ const { written, observed } = yield * captureRootRunTermination ( {
3795+ key : "stream-end:shutdown" ,
3796+ shouldFinalizeRun : ( ) => Effect . succeed ( true ) ,
3797+ events : ( ) => Stream . failCause ( Cause . interrupt ( ) ) ,
3798+ } ) ;
3799+ assert . deepEqual ( written , [ ] ) ;
3800+ assert . deepEqual ( observed , [ ] ) ;
3801+ } ) ,
3802+ ) ;
3803+
3804+ it . effect ( "does not start a turn whose run settled during the app context read" , ( ) =>
3805+ Effect . gen ( function * ( ) {
3806+ const contextReadStarted = yield * Deferred . make < void > ( ) ;
3807+ const runFailed = yield * Deferred . make < void > ( ) ;
3808+ const current = yield * Ref . make ( true ) ;
3809+ const providerCalls = yield * Ref . make ( 0 ) ;
3810+ const { observed } = yield * captureRootRunTermination ( {
3811+ key : "stream-end:context-read" ,
3812+ shouldStartProviderTurn : ( ) => Ref . get ( current ) ,
3813+ shouldFinalizeRun : ( ) => Ref . get ( current ) ,
3814+ events : ( ) => Stream . unwrap ( Deferred . await ( contextReadStarted ) . pipe ( Effect . as ( Stream . empty ) ) ) ,
3815+ appContext : Deferred . succeed ( contextReadStarted , undefined ) . pipe (
3816+ Effect . andThen ( Deferred . await ( runFailed ) ) ,
3817+ Effect . as ( [ ] ) ,
3818+ ) ,
3819+ refreshAfterTurn : Ref . set ( current , false ) . pipe (
3820+ Effect . andThen ( Deferred . succeed ( runFailed , undefined ) ) ,
3821+ ) ,
3822+ startTurn : ( ) => Ref . update ( providerCalls , ( count ) => count + 1 ) ,
3823+ } ) ;
3824+ assert . deepEqual ( observed , [ "run:failed" , "pull-requests-refreshed" ] ) ;
3825+ assert . equal ( yield * Ref . get ( providerCalls ) , 0 ) ;
3826+ } ) ,
3827+ ) ;
3828+
37753829it . effect ( "refreshes pull requests only once when startup failure closes its event stream" , ( ) =>
37763830 Effect . gen ( function * ( ) {
37773831 const ingestionStarted = yield * Deferred . make < void > ( ) ;
@@ -3828,6 +3882,8 @@ function captureRootRunTermination(input: {
38283882 > ;
38293883 readonly startTurn ?: ProviderAdapter . ProviderAdapterV2SessionRuntime [ "startTurn" ] ;
38303884 readonly refreshAfterTurn ?: Effect . Effect < void > ;
3885+ readonly shouldStartProviderTurn ?: RunExecutionService . RunExecutionServiceV2StartRootRunInput [ "shouldStartProviderTurn" ] ;
3886+ readonly appContext ?: ReturnType < McpAppModelContext . McpAppModelContext [ "Service" ] [ "forThread" ] > ;
38313887} ) {
38323888 return Effect . gen ( function * ( ) {
38333889 const ids = backgroundScenarioIds ( input . key ) ;
@@ -3840,6 +3896,7 @@ function captureRootRunTermination(input: {
38403896 status : "running" ,
38413897 } ) ;
38423898 const writtenItems = yield * Ref . make < ReadonlyArray < OrchestrationV2TurnItem > > ( [ ] ) ;
3899+ const finalEvents = yield * Ref . make < ReadonlyArray < OrchestrationV2DomainEvent > > ( [ ] ) ;
38433900 const observed = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
38443901 const submittedEffects = yield * Ref . make < ReadonlyArray < PendingOrchestrationEffectV2 > > ( [ ] ) ;
38453902 const committedEffects = yield * Ref . make < ReadonlyArray < PendingOrchestrationEffectV2 > > ( [ ] ) ;
@@ -3848,6 +3905,7 @@ function captureRootRunTermination(input: {
38483905 Ref . update ( writtenItems , ( current ) => [ ...current , payload ] ) ;
38493906 const captureFinalEvents = ( events : ReadonlyArray < OrchestrationV2DomainEvent > ) =>
38503907 Effect . gen ( function * ( ) {
3908+ yield * Ref . update ( finalEvents , ( current ) => [ ...current , ...events ] ) ;
38513909 for ( const event of events ) {
38523910 if ( event . type === "turn-item.updated" ) {
38533911 yield * captureTurnItem ( event . payload ) ;
@@ -3860,7 +3918,9 @@ function captureRootRunTermination(input: {
38603918 const layerTest = RunExecutionService . layer . pipe (
38613919 Layer . provide (
38623920 Layer . mergeAll (
3863- McpAppModelContext . layerEmpty ,
3921+ Layer . mock ( McpAppModelContext . McpAppModelContext ) ( {
3922+ forThread : ( ) => input . appContext ?? Effect . succeed ( [ ] ) ,
3923+ } ) ,
38643924 Layer . mock ( CheckpointService . CheckpointServiceV2 ) ( { captureBaseline : ( ) => Effect . void } ) ,
38653925 Layer . mock ( EventSink . EventSinkV2 ) ( {
38663926 write : ( payload ) =>
@@ -3970,6 +4030,9 @@ function captureRootRunTermination(input: {
39704030 attemptId : ids . attemptId ,
39714031 providerTurnOrdinal : 1 ,
39724032 shouldFinalizeRun : input . shouldFinalizeRun ,
4033+ ...( input . shouldStartProviderTurn === undefined
4034+ ? { }
4035+ : { shouldStartProviderTurn : input . shouldStartProviderTurn } ) ,
39734036 ...( input . hasUnpairedRunInterruptRequest === undefined
39744037 ? { }
39754038 : {
@@ -4000,6 +4063,7 @@ function captureRootRunTermination(input: {
40004063 yield * Deferred . await ( ingestionDone ) ;
40014064 return {
40024065 written : yield * Ref . get ( writtenItems ) ,
4066+ finalEvents : yield * Ref . get ( finalEvents ) ,
40034067 observed : yield * Ref . get ( observed ) ,
40044068 submittedEffects : yield * Ref . get ( submittedEffects ) ,
40054069 committedEffects : yield * Ref . get ( committedEffects ) ,
0 commit comments