@@ -1438,6 +1438,137 @@ it.effect("keeps ingesting owned child events after the root turn terminalizes",
14381438 } ) ,
14391439) ;
14401440
1441+ it . effect ( "ends a nested native subagent and its thread when the root run is interrupted" , ( ) =>
1442+ Effect . gen ( function * ( ) {
1443+ const key = "run-execution-nested-native" ;
1444+ const threadId = ThreadId . make ( `thread:${ key } ` ) ;
1445+ const childThreadId = ThreadId . make ( `thread:${ key } :child` ) ;
1446+ const grandchildThreadId = ThreadId . make ( `thread:${ key } :grandchild` ) ;
1447+ const runId = RunId . make ( `run:${ key } ` ) ;
1448+ const attemptId = RunAttemptId . make ( `attempt:${ key } ` ) ;
1449+ const providerInstanceId = ProviderInstanceId . make ( "codex" ) ;
1450+ const providerThreadId = ProviderThreadId . make ( `provider-thread:${ key } ` ) ;
1451+ const rootProviderTurnId = ProviderTurnId . make ( `provider-turn:${ key } ` ) ;
1452+ const nestedId = NodeId . make ( `node:${ key } :nested` ) ;
1453+ const grandchildRootId = NodeId . make ( `node:${ key } :grandchild-root` ) ;
1454+ const finalized = yield * Deferred . make < ReadonlyArray < OrchestrationV2DomainEvent > > ( ) ;
1455+ const layerTest = RunExecutionService . layer . pipe (
1456+ Layer . provide (
1457+ Layer . mergeAll (
1458+ McpAppModelContext . layerEmpty ,
1459+ Layer . mock ( CheckpointService . CheckpointServiceV2 ) ( { captureBaseline : ( ) => Effect . void } ) ,
1460+ Layer . mock ( EventSink . EventSinkV2 ) ( {
1461+ write : ( ) => Effect . succeed ( [ ] ) ,
1462+ writeWithEffects : ( input ) =>
1463+ Effect . as (
1464+ input . events . some ( ( event ) => event . type === "run.updated" )
1465+ ? Deferred . succeed ( finalized , input . events )
1466+ : Effect . void ,
1467+ [ ] ,
1468+ ) ,
1469+ } ) ,
1470+ IdAllocator . layer ,
1471+ Layer . mock ( ProviderEventIngestor . ProviderEventIngestorV2 ) ( {
1472+ ingestNormalized : ( ) => Effect . succeed ( [ ] ) ,
1473+ } ) ,
1474+ ServerSettings . layerTest ( ) ,
1475+ ) ,
1476+ ) ,
1477+ ) ;
1478+ const subagentThread = ( id : ThreadId , parentThreadId : ThreadId ) =>
1479+ ( {
1480+ type : "app_thread.created" ,
1481+ driver,
1482+ appThread : {
1483+ id,
1484+ lineage : { parentThreadId, relationshipToParent : "subagent" , rootThreadId : threadId } ,
1485+ } ,
1486+ } ) as ProviderAdapterV2Event ;
1487+ const events : ReadonlyArray < ProviderAdapterV2Event > = [
1488+ subagentThread ( childThreadId , threadId ) ,
1489+ subagentThread ( grandchildThreadId , childThreadId ) ,
1490+ {
1491+ type : "subagent.updated" ,
1492+ driver,
1493+ subagent : {
1494+ id : nestedId ,
1495+ threadId : childThreadId ,
1496+ runId : null ,
1497+ origin : "provider_native" ,
1498+ childThreadId : grandchildThreadId ,
1499+ status : "running" ,
1500+ } ,
1501+ } as ProviderAdapterV2Event ,
1502+ {
1503+ type : "node.updated" ,
1504+ driver,
1505+ node : {
1506+ id : grandchildRootId ,
1507+ threadId : grandchildThreadId ,
1508+ runId : null ,
1509+ kind : "root_turn" ,
1510+ status : "running" ,
1511+ } ,
1512+ } as ProviderAdapterV2Event ,
1513+ {
1514+ type : "turn.terminal" ,
1515+ driver,
1516+ providerThreadId,
1517+ providerTurnId : rootProviderTurnId ,
1518+ runOrdinal : 1 ,
1519+ status : "interrupted" ,
1520+ failure : null ,
1521+ threadDisposition : "reusable" ,
1522+ } ,
1523+ ] ;
1524+
1525+ yield * Effect . gen ( function * ( ) {
1526+ const runExecution = yield * RunExecutionService . RunExecutionServiceV2 ;
1527+ yield * runExecution . startRootRun ( {
1528+ commandId : CommandId . make ( `command:${ key } ` ) ,
1529+ appThread : { id : threadId } as OrchestrationV2AppThread ,
1530+ providerSessionId : ProviderSessionId . make ( `session:${ key } ` ) ,
1531+ session : {
1532+ events : Stream . fromIterable ( events ) ,
1533+ startTurn : ( ) => Effect . void ,
1534+ } as unknown as ProviderAdapterV2SessionRuntime ,
1535+ run : { id : runId , threadId, ordinal : 1 , providerInstanceId } as OrchestrationV2Run ,
1536+ rootNode : { id : NodeId . make ( `node:${ key } ` ) } as OrchestrationV2ExecutionNode ,
1537+ checkpointScope : {
1538+ id : CheckpointScopeId . make ( `checkpoint-scope:${ key } ` ) ,
1539+ } as OrchestrationV2CheckpointScope ,
1540+ providerThread : { id : providerThreadId , driver } as OrchestrationV2ProviderThread ,
1541+ attempt : { id : attemptId , providerTurnId : rootProviderTurnId } as OrchestrationV2RunAttempt ,
1542+ attemptId,
1543+ providerTurnOrdinal : 1 ,
1544+ message : {
1545+ messageId : MessageId . make ( `message:${ key } :user` ) ,
1546+ text : "Spawn a nested subagent." ,
1547+ attachments : [ ] ,
1548+ createdBy : "user" ,
1549+ creationSource : "web" ,
1550+ } ,
1551+ modelSelection : { instanceId : providerInstanceId , model : "gpt-5.4" } ,
1552+ runtimePolicy : {
1553+ runtimeMode : "full-access" ,
1554+ interactionMode : "default" ,
1555+ cwd : process . cwd ( ) ,
1556+ approvalPolicy : "never" ,
1557+ sandboxPolicy : { type : "readOnly" , access : { type : "fullAccess" } , networkAccess : false } ,
1558+ } ,
1559+ } ) ;
1560+ } ) . pipe ( Effect . provide ( layerTest ) ) ;
1561+
1562+ const ended = ( yield * Deferred . await ( finalized ) ) . flatMap ( ( event ) =>
1563+ event . type === "subagent.updated" || event . type === "node.updated"
1564+ ? [ [ event . type , event . payload . id , event . payload . status ] ]
1565+ : [ ] ,
1566+ ) ;
1567+ assert . deepInclude ( ended , [ "subagent.updated" , nestedId , "interrupted" ] ) ;
1568+ assert . deepInclude ( ended , [ "node.updated" , grandchildRootId , "interrupted" ] ) ;
1569+ } ) ,
1570+ ) ;
1571+
14411572it . effect ( "keeps ingesting a late background command item completion after root terminal" , ( ) =>
14421573 Effect . gen ( function * ( ) {
14431574 const observed = yield * runBackgroundItemScenario ( "bg-command" , ( ids ) => [
0 commit comments