@@ -367,99 +367,107 @@ describe("OrchestratorMcpService", () => {
367367 } ) ,
368368 ) ;
369369
370- it . effect ( "cancels an orphaned follow-up through its parent when the child is idle" , ( ) =>
371- Effect . gen ( function * ( ) {
372- const parentThreadId = ThreadId . make ( "thread:mcp-orphan-parent" ) ;
373- const childThreadId = ThreadId . make ( "thread:mcp-orphan-child" ) ;
374- const taskId = NodeId . make ( "node:mcp-orphan-task" ) ;
375- const parentRunId = RunId . make ( "run:mcp-orphan-parent" ) ;
376- const dispatched = yield * Ref . make < ReadonlyArray < OrchestrationV2ServerCommand > > ( [ ] ) ;
377- const status = yield * Ref . make ( "running" ) ;
378- const parentProjection = ( taskStatus : string ) =>
379- ( {
380- thread : { id : parentThreadId } ,
381- runs : [ ] ,
370+ it . effect . each ( [ "no runs" , "a finished run" ] as const ) (
371+ "cancels an orphaned follow-up through its parent when the child has %s" ,
372+ ( childState ) =>
373+ Effect . gen ( function * ( ) {
374+ const parentThreadId = ThreadId . make ( "thread:mcp-orphan-parent" ) ;
375+ const childThreadId = ThreadId . make ( "thread:mcp-orphan-child" ) ;
376+ const taskId = NodeId . make ( "node:mcp-orphan-task" ) ;
377+ const parentRunId = RunId . make ( "run:mcp-orphan-parent" ) ;
378+ const dispatched = yield * Ref . make < ReadonlyArray < OrchestrationV2ServerCommand > > ( [ ] ) ;
379+ const status = yield * Ref . make ( "running" ) ;
380+ const parentProjection = ( taskStatus : string ) =>
381+ ( {
382+ thread : { id : parentThreadId } ,
383+ runs : [ ] ,
384+ contextTransfers : [ ] ,
385+ subagents : [
386+ {
387+ id : taskId ,
388+ threadId : parentThreadId ,
389+ runId : parentRunId ,
390+ origin : "app_owned" ,
391+ childThreadId,
392+ driver : "codex" ,
393+ model : "gpt-5.6-terra" ,
394+ status : taskStatus ,
395+ result : null ,
396+ completionDelivery : { state : "pending" } ,
397+ } ,
398+ ] ,
399+ } ) as unknown as OrchestrationV2ThreadProjection ;
400+ // A finished run whose binding to the follow-up was lost (#19002).
401+ const childProjection = {
402+ thread : { id : childThreadId } ,
403+ runs :
404+ childState === "no runs"
405+ ? [ ]
406+ : [ { id : RunId . make ( "run:mcp-orphan-child" ) , ordinal : 1 , status : "completed" } ] ,
382407 contextTransfers : [ ] ,
383- subagents : [
384- {
385- id : taskId ,
386- threadId : parentThreadId ,
387- runId : parentRunId ,
388- origin : "app_owned" ,
389- childThreadId,
390- driver : "codex" ,
391- model : "gpt-5.6-terra" ,
392- status : taskStatus ,
393- result : null ,
394- completionDelivery : { state : "pending" } ,
395- } ,
396- ] ,
397- } ) as unknown as OrchestrationV2ThreadProjection ;
398- const childProjection = {
399- thread : { id : childThreadId } ,
400- runs : [ ] ,
401- contextTransfers : [ ] ,
402- messages : [ ] ,
403- subagents : [ ] ,
404- providerThreads : [ ] ,
405- runtimeRequests : [ ] ,
406- } as unknown as OrchestrationV2ThreadProjection ;
407- const layerDependencies = Layer . mergeAll (
408- NodeServices . layer ,
409- Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
410- getThreadRecords : ( threadId ) =>
411- threadId === parentThreadId
412- ? Effect . map ( Ref . get ( status ) , parentProjection )
413- : Effect . succeed ( childProjection ) ,
414- getThreadShell : ( threadId ) => Effect . succeed ( liveThreadShell ( threadId ) ) ,
415- // The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
416- dispatch : ( command ) =>
417- Ref . update ( dispatched , ( commands ) => [ ...commands , command ] ) . pipe (
418- Effect . andThen (
419- command . type === "subagent.stop" ? Ref . set ( status , "cancelled" ) : Effect . void ,
408+ messages : [ ] ,
409+ subagents : [ ] ,
410+ providerThreads : [ ] ,
411+ runtimeRequests : [ ] ,
412+ turnItems : [ ] ,
413+ } as unknown as OrchestrationV2ThreadProjection ;
414+ const layerDependencies = Layer . mergeAll (
415+ NodeServices . layer ,
416+ Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
417+ getThreadRecords : ( threadId ) =>
418+ threadId === parentThreadId
419+ ? Effect . map ( Ref . get ( status ) , parentProjection )
420+ : Effect . succeed ( childProjection ) ,
421+ getThreadShell : ( threadId ) => Effect . succeed ( liveThreadShell ( threadId ) ) ,
422+ delegatedTaskResultPending : ( ) => Effect . succeed ( false ) ,
423+ // The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
424+ dispatch : ( command ) =>
425+ Ref . update ( dispatched , ( commands ) => [ ...commands , command ] ) . pipe (
426+ Effect . andThen (
427+ command . type === "subagent.stop" ? Ref . set ( status , "cancelled" ) : Effect . void ,
428+ ) ,
429+ Effect . as ( { } as never ) ,
420430 ) ,
421- Effect . as ( { } as never ) ,
422- ) ,
423- } ) ,
424- ServerSettings . layerTest ( ) ,
425- Layer . mock ( ProviderRegistry . ProviderRegistry ) ( { getProviders : Effect . succeed ( [ ] ) } ) ,
426- Layer . mock ( ProviderAdapterRegistry . ProviderAdapterRegistryV2 ) ( {
427- list : ( ) => Effect . succeed ( [ ] ) ,
428- } ) ,
429- Layer . mock ( ProjectService . ProjectService ) ( { } ) ,
430- Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
431- Layer . mock ( ScheduledTaskService . ScheduledTaskService ) ( { } ) ,
432- ) ;
433- const scope : McpInvocationScope = {
434- environmentId : EnvironmentId . make ( "environment:mcp-orphan" ) ,
435- requestNamespace : "provider-session:mcp-orphan" ,
436- thread : {
437- threadId : parentThreadId ,
438- providerSessionId : "provider-session:mcp-orphan" ,
439- providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
440- } ,
441- client : undefined ,
442- capabilities : new Set ( [ "orchestration" ] ) ,
443- issuedAt : 1 ,
444- } ;
445-
446- yield * Effect . gen ( function * ( ) {
447- const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
448- const result = yield * service . cancelTask ( scope , {
449- taskId,
450- clientRequestId : "cancel-orphan-task" ,
451- } ) ;
452- assert . deepEqual ( result , { taskId, status : "cancelled" } ) ;
453- const [ stop , ...rest ] = yield * Ref . get ( dispatched ) ;
454- assert . isEmpty ( rest ) ;
455- assert . equal ( stop ?. type , "subagent.stop" ) ;
456- if ( stop ?. type !== "subagent.stop" ) return ;
457- assert . deepEqual (
458- { threadId : stop . threadId , runId : stop . runId , subagentId : stop . subagentId } ,
459- { threadId : parentThreadId , runId : parentRunId , subagentId : taskId } ,
431+ } ) ,
432+ ServerSettings . layerTest ( ) ,
433+ Layer . mock ( ProviderRegistry . ProviderRegistry ) ( { getProviders : Effect . succeed ( [ ] ) } ) ,
434+ Layer . mock ( ProviderAdapterRegistry . ProviderAdapterRegistryV2 ) ( {
435+ list : ( ) => Effect . succeed ( [ ] ) ,
436+ } ) ,
437+ Layer . mock ( ProjectService . ProjectService ) ( { } ) ,
438+ Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
439+ Layer . mock ( ScheduledTaskService . ScheduledTaskService ) ( { } ) ,
460440 ) ;
461- } ) . pipe ( Effect . provide ( layerOrchestratorService . pipe ( Layer . provide ( layerDependencies ) ) ) ) ;
462- } ) ,
441+ const scope : McpInvocationScope = {
442+ environmentId : EnvironmentId . make ( "environment:mcp-orphan" ) ,
443+ requestNamespace : "provider-session:mcp-orphan" ,
444+ thread : {
445+ threadId : parentThreadId ,
446+ providerSessionId : "provider-session:mcp-orphan" ,
447+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
448+ } ,
449+ client : undefined ,
450+ capabilities : new Set ( [ "orchestration" ] ) ,
451+ issuedAt : 1 ,
452+ } ;
453+
454+ yield * Effect . gen ( function * ( ) {
455+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
456+ const result = yield * service . cancelTask ( scope , {
457+ taskId,
458+ clientRequestId : "cancel-orphan-task" ,
459+ } ) ;
460+ assert . deepEqual ( result , { taskId, status : "cancelled" } ) ;
461+ const [ stop , ...rest ] = yield * Ref . get ( dispatched ) ;
462+ assert . isEmpty ( rest ) ;
463+ assert . equal ( stop ?. type , "subagent.stop" ) ;
464+ if ( stop ?. type !== "subagent.stop" ) return ;
465+ assert . deepEqual (
466+ { threadId : stop . threadId , runId : stop . runId , subagentId : stop . subagentId } ,
467+ { threadId : parentThreadId , runId : parentRunId , subagentId : taskId } ,
468+ ) ;
469+ } ) . pipe ( Effect . provide ( layerOrchestratorService . pipe ( Layer . provide ( layerDependencies ) ) ) ) ;
470+ } ) ,
463471 ) ;
464472
465473 it . effect ( "does not dispose delivery when the child stop fails" , ( ) =>
0 commit comments