@@ -65,6 +65,7 @@ import type { McpInvocationScope } from "./McpInvocationContext.ts";
6565import { idleThreadProjection , liveThreadShell } from "./McpToolAccess.testkit.ts" ;
6666import * as OrchestratorMcpService from "./OrchestratorMcpService.ts" ;
6767import { resolveRuntimeMode } from "./delegatedTaskModes.ts" ;
68+ import type { ProjectionRecordFilter } from "../orchestration-v2/ProjectionStore.ts" ;
6869import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" ;
6970
7071const decodeTarget = Schema . decodeUnknownEffect ( OrchestratorMcpTarget ) ;
@@ -453,6 +454,7 @@ describe("OrchestratorMcpService", () => {
453454 getThreadRecords : ( threadId ) =>
454455 Effect . succeed ( threadId === parentThreadId ? parentProjection : childProjection ) ,
455456 getThreadShell : ( threadId ) => Effect . succeed ( liveThreadShell ( threadId ) ) ,
457+ listAppOwnedChildThreadIds : ( ) => Effect . succeed ( [ ] ) ,
456458 dispatch : ( command ) =>
457459 Ref . update ( dispatched , ( commands ) => [ ...commands , command ] ) . pipe (
458460 Effect . as ( { } as never ) ,
@@ -544,6 +546,7 @@ describe("OrchestratorMcpService", () => {
544546 ? Effect . map ( Ref . get ( status ) , parentProjection )
545547 : Effect . succeed ( childProjection ) ,
546548 getThreadShell : ( threadId ) => Effect . succeed ( liveThreadShell ( threadId ) ) ,
549+ listAppOwnedChildThreadIds : ( ) => Effect . succeed ( [ ] ) ,
547550 delegatedTaskResultPending : ( ) => Effect . succeed ( false ) ,
548551 stopDelegatedTasks : ( ) => Effect . void ,
549552 // The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
@@ -642,6 +645,7 @@ describe("OrchestratorMcpService", () => {
642645 getThreadRecords : ( threadId ) =>
643646 Effect . succeed ( threadId === parentThreadId ? parentProjection : childProjection ) ,
644647 getThreadShell : ( threadId ) => Effect . succeed ( liveThreadShell ( threadId ) ) ,
648+ listAppOwnedChildThreadIds : ( ) => Effect . succeed ( [ ] ) ,
645649 dispatch : ( command ) =>
646650 Ref . update ( dispatched , ( commands ) => [ ...commands , command ] ) . pipe (
647651 Effect . andThen ( Effect . fail ( new Error ( "simulated stop failure" ) as never ) ) ,
@@ -723,6 +727,7 @@ describe("OrchestratorMcpService", () => {
723727 getThreadRecords : ( threadId ) =>
724728 Effect . succeed ( threadId === parentThreadId ? parentProjection : childProjection ) ,
725729 getThreadShell : ( threadId ) => Effect . succeed ( liveThreadShell ( threadId ) ) ,
730+ listAppOwnedChildThreadIds : ( ) => Effect . succeed ( [ ] ) ,
726731 dispatch : ( command ) =>
727732 Ref . update ( dispatched , ( commands ) => [ ...commands , command ] ) . pipe (
728733 Effect . andThen (
@@ -810,6 +815,7 @@ describe("OrchestratorMcpService", () => {
810815 getThreadRecords : ( threadId ) =>
811816 Effect . succeed ( threadId === parentThreadId ? parentProjection : childProjection ) ,
812817 getThreadShell : ( threadId ) => Effect . succeed ( liveThreadShell ( threadId ) ) ,
818+ listAppOwnedChildThreadIds : ( ) => Effect . succeed ( [ ] ) ,
813819 dispatch : ( command ) =>
814820 Ref . update ( dispatched , ( commands ) => [ ...commands , command ] ) . pipe (
815821 Effect . andThen (
@@ -865,16 +871,25 @@ describe("OrchestratorMcpService", () => {
865871 const taskId = NodeId . make ( "node:mcp-cancel-grandchild-task" ) ;
866872 const dispatched = yield * Ref . make < ReadonlyArray < unknown > > ( [ ] ) ;
867873 const stoppedBelow = yield * Ref . make ( false ) ;
868- const appOwnedTask = ( id : string , threadId : ThreadId , childId : ThreadId ) => ( {
874+ const subagentReads = yield * Ref . make < ReadonlyArray < ProjectionRecordFilter | undefined > > ( [ ] ) ;
875+ const appOwnedTask = (
876+ id : string ,
877+ threadId : ThreadId ,
878+ childId : ThreadId ,
879+ settled ?: "settled" ,
880+ ) => ( {
869881 id : NodeId . make ( id ) ,
870882 threadId,
871883 origin : "app_owned" ,
872884 childThreadId : childId ,
873885 driver : "codex" ,
874886 model : "gpt-5.6-terra" ,
875- result : null ,
876- completionDelivery : { state : "pending" } ,
887+ status : settled === undefined ? "running" : "completed" ,
888+ result : settled === undefined ? null : "Done." ,
889+ completionDelivery : { state : settled === undefined ? "pending" : "delivered" } ,
877890 } ) ;
891+ // Older tasks the parent and child dispatched and that have long settled.
892+ const settledThreadId = ThreadId . make ( "thread:mcp-cancel-grandchild-settled" ) ;
878893 const projections = new Map ( [
879894 [
880895 parentThreadId ,
@@ -886,7 +901,15 @@ describe("OrchestratorMcpService", () => {
886901 } ,
887902 runs : [ ] ,
888903 contextTransfers : [ ] ,
889- subagents : [ appOwnedTask ( taskId , parentThreadId , childThreadId ) ] ,
904+ subagents : [
905+ appOwnedTask (
906+ "node:mcp-cancel-grandchild-old" ,
907+ parentThreadId ,
908+ settledThreadId ,
909+ "settled" ,
910+ ) ,
911+ appOwnedTask ( taskId , parentThreadId , childThreadId ) ,
912+ ] ,
890913 } ,
891914 ] ,
892915 [
@@ -897,12 +920,30 @@ describe("OrchestratorMcpService", () => {
897920 contextTransfers : [ ] ,
898921 messages : [ ] ,
899922 subagents : [
923+ appOwnedTask (
924+ "node:mcp-cancel-grandchild-done" ,
925+ childThreadId ,
926+ settledThreadId ,
927+ "settled" ,
928+ ) ,
900929 appOwnedTask ( "node:mcp-cancel-grandchild-below" , childThreadId , grandchildThreadId ) ,
901930 ] ,
902931 providerThreads : [ ] ,
903932 runtimeRequests : [ ] ,
904933 } ,
905934 ] ,
935+ [
936+ settledThreadId ,
937+ {
938+ thread : { id : settledThreadId } ,
939+ runs : [ ] ,
940+ contextTransfers : [ ] ,
941+ messages : [ ] ,
942+ subagents : [ ] ,
943+ providerThreads : [ ] ,
944+ runtimeRequests : [ ] ,
945+ } ,
946+ ] ,
906947 [
907948 grandchildThreadId ,
908949 {
@@ -919,7 +960,21 @@ describe("OrchestratorMcpService", () => {
919960 const layerDependencies = Layer . mergeAll (
920961 NodeServices . layer ,
921962 Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
922- getThreadRecords : ( threadId ) => Effect . succeed ( projections . get ( threadId ) ! ) ,
963+ getThreadRecords : ( threadId , fields , filter ) =>
964+ ( ( fields as ReadonlyArray < string > ) . includes ( "subagents" )
965+ ? Ref . update ( subagentReads , ( reads ) => [ ...reads , filter ] )
966+ : Effect . void
967+ ) . pipe ( Effect . as ( projections . get ( threadId ) ! ) ) ,
968+ listAppOwnedChildThreadIds : ( threadId ) =>
969+ Effect . succeed (
970+ projections
971+ . get ( threadId ) !
972+ . subagents . flatMap ( ( task ) =>
973+ task . origin === "app_owned" && task . childThreadId !== null
974+ ? [ task . childThreadId ]
975+ : [ ] ,
976+ ) ,
977+ ) ,
923978 getThreadShell : ( threadId ) =>
924979 Effect . succeed (
925980 threadId === grandchildThreadId && child . raisedDuringStop !== true
@@ -982,6 +1037,12 @@ describe("OrchestratorMcpService", () => {
9821037 const error = yield * service
9831038 . cancelTask ( scope , { taskId, clientRequestId : "cancel-grandchild-above-modes" } )
9841039 . pipe ( Effect . flip ) ;
1040+ // No read decodes every task a thread has dispatched.
1041+ for ( const filter of yield * Ref . get ( subagentReads ) ) {
1042+ assert . isTrue (
1043+ filter ?. subagentIds !== undefined || filter ?. omitSettledSubagents !== undefined ,
1044+ ) ;
1045+ }
9851046 return {
9861047 code : error . code ,
9871048 dispatched : yield * Ref . get ( dispatched ) ,
0 commit comments