@@ -351,6 +351,8 @@ export function delegatedTaskRun(
351351 childProjection : Pick < OrchestrationV2ThreadProjection , "runs" | "contextTransfers" > ,
352352 task : OrchestrationV2Subagent ,
353353) : OrchestrationV2Run | undefined {
354+ const taskRun = childProjection . runs . find ( ( run ) => run . delegatedTaskId === task . id ) ;
355+ if ( taskRun !== undefined ) return taskRun ;
354356 const spawnTransfer = childProjection . contextTransfers . find (
355357 ( transfer ) =>
356358 transfer . type === "subagent_spawn" &&
@@ -390,22 +392,60 @@ function isTerminalTaskStatus(
390392 ) ;
391393}
392394
393- function directAppOwnedChildTask (
395+ function directAppOwnedChildTasks (
394396 parent : Pick < OrchestrationV2ThreadProjection , "thread" | "subagents" > ,
395- target : Pick < OrchestrationV2ThreadProjection , "thread" > ,
396- ) : OrchestrationV2Subagent | undefined {
397+ target : Pick < OrchestrationV2ThreadProjection , "thread" | "runs" > ,
398+ returnedRunIds : ReadonlySet < RunId | null > ,
399+ ) : ReadonlyArray < OrchestrationV2Subagent > {
397400 if (
398401 target . thread . lineage . parentThreadId !== parent . thread . id ||
399402 target . thread . lineage . relationshipToParent !== "subagent"
400403 ) {
401- return undefined ;
404+ return [ ] ;
402405 }
403- return parent . subagents . find (
406+ const original = parent . subagents . find (
404407 ( task ) =>
405408 task . origin === "app_owned" &&
406409 task . threadId === parent . thread . id &&
407410 task . childThreadId === target . thread . id ,
408411 ) ;
412+ const taskIds = new Set (
413+ target . runs
414+ . filter ( ( run ) => returnedRunIds . has ( run . id ) )
415+ . map ( ( run ) => run . delegatedTaskId ?? original ?. id ) ,
416+ ) ;
417+ return parent . subagents . filter (
418+ ( task ) =>
419+ taskIds . has ( task . id ) &&
420+ task . origin === "app_owned" &&
421+ task . childThreadId === target . thread . id ,
422+ ) ;
423+ }
424+
425+ function taskResultTransfer (
426+ parent : Pick < OrchestrationV2ThreadProjection , "thread" | "contextTransfers" > ,
427+ child : Pick < OrchestrationV2ThreadProjection , "runs" | "contextTransfers" > ,
428+ task : OrchestrationV2Subagent ,
429+ ) {
430+ const originalRun = delegatedTaskRun ( child , task ) ;
431+ const resultRunIds = new Set (
432+ child . runs
433+ . filter (
434+ ( run ) =>
435+ run . delegatedTaskId === task . id ||
436+ ( originalRun ?. delegatedTaskId === undefined && run . delegatedTaskId === undefined ) ,
437+ )
438+ . map ( ( run ) => run . id ) ,
439+ ) ;
440+ if ( originalRun !== undefined ) resultRunIds . add ( originalRun . id ) ;
441+ return parent . contextTransfers . find (
442+ ( transfer ) =>
443+ transfer . type === "subagent_result" &&
444+ transfer . sourceThreadId === task . childThreadId &&
445+ transfer . targetThreadId === parent . thread . id &&
446+ ( ( transfer . sourcePoint . runId !== undefined && resultRunIds . has ( transfer . sourcePoint . runId ) ) ||
447+ ( transfer . sourcePoint . runId === undefined && originalRun ?. delegatedTaskId === undefined ) ) ,
448+ ) ;
409449}
410450
411451function pageIncludesTerminalTaskResult ( input : {
@@ -418,12 +458,7 @@ function pageIncludesTerminalTaskResult(input: {
418458 > ;
419459 readonly maxChars : number ;
420460} ) : boolean {
421- const transfer = input . parent . contextTransfers . find (
422- ( transfer ) =>
423- transfer . type === "subagent_result" &&
424- transfer . sourceThreadId === input . target . thread . id &&
425- transfer . targetThreadId === input . parent . thread . id ,
426- ) ;
461+ const transfer = taskResultTransfer ( input . parent , input . target , input . task ) ;
427462 if ( transfer === undefined ) return false ;
428463 const run =
429464 transfer . sourcePoint . runId === undefined
@@ -1241,9 +1276,15 @@ const make = Effect.gen(function* () {
12411276 const childRun = delegatedTaskRun ( childControls , task ) ;
12421277 const terminalRun = latestTerminalResultRun ( childControls , childRun ) ;
12431278 const progress = delegatedTaskProgress ( childControls ) ;
1279+ const taskResultRun =
1280+ childRun ?. delegatedTaskId === task . id
1281+ ? childControls . runs
1282+ . filter ( ( run ) => run . delegatedTaskId === task . id )
1283+ . toSorted ( ( a , b ) => b . ordinal - a . ordinal ) [ 0 ]
1284+ : progress . resultRun ;
12441285 const resultRunIds = [
12451286 ...new Set (
1246- [ progress . resultRun ?. id , terminalRun ?. id ] . filter ( ( id ) : id is RunId => id !== undefined ) ,
1287+ [ taskResultRun ?. id , terminalRun ?. id ] . filter ( ( id ) : id is RunId => id !== undefined ) ,
12471288 ) ,
12481289 ] ;
12491290 const resultRecords = yield * threadManagement
@@ -1280,15 +1321,15 @@ const make = Effect.gen(function* () {
12801321 : childRun ,
12811322 )
12821323 : workState === "result_available"
1283- ? taskStatusForRun ( progress . resultRun ?? childRun )
1324+ ? taskStatusForRun ( taskResultRun ?? childRun )
12841325 : taskStatusForRun ( childRun ) === "queued"
12851326 ? "queued"
12861327 : "running" ;
12871328 const derivedResult =
12881329 task . result !== null
12891330 ? task . result
1290- : progress . resultRun !== undefined && isTerminalTaskStatus ( status )
1291- ? subagentResultForRun ( childProjection , progress . resultRun ) . text
1331+ : taskResultRun !== undefined && isTerminalTaskStatus ( status )
1332+ ? subagentResultForRun ( childProjection , taskResultRun ) . text
12921333 : null ;
12931334 const resultTransfers = parentProjection . contextTransfers . filter (
12941335 ( transfer ) =>
@@ -1304,7 +1345,7 @@ const make = Effect.gen(function* () {
13041345 ? resultTransfers . find ( ( transfer ) => transfer . sourcePoint . runId === undefined )
13051346 : undefined ) ??
13061347 null ) ;
1307- const resultTransfer = resultTransfers [ 0 ] ?? null ;
1348+ const resultTransfer = taskResultTransfer ( parentProjection , childProjection , task ) ?? null ;
13081349 const terminalStatus = terminalRun === undefined ? null : taskStatusForRun ( terminalRun ) ;
13091350 const response = {
13101351 taskId : task . id ,
@@ -2315,45 +2356,48 @@ const make = Effect.gen(function* () {
23152356 { concurrency : 1 } ,
23162357 ) ;
23172358 const messagesByThreadId = new Map ( sourceMessages ) ;
2318- const task = parent === undefined ? undefined : directAppOwnedChildTask ( parent , target ) ;
2359+ const returnedRunIds = new Set (
2360+ page
2361+ . filter ( ( row ) => row . sourceThreadId === target . thread . id )
2362+ . map ( ( row ) => row . item . runId ) ,
2363+ ) ;
2364+ const tasks =
2365+ parent === undefined ? [ ] : directAppOwnedChildTasks ( parent , target , returnedRunIds ) ;
23192366 if (
23202367 parent !== undefined &&
23212368 scope . thread !== undefined &&
2322- task !== undefined &&
2369+ tasks . length > 0 &&
23232370 ( input . textOffset ?? 0 ) === 0
23242371 ) {
2325- const transfer = parent . contextTransfers . find (
2326- ( transfer ) =>
2327- transfer . type === "subagent_result" &&
2328- transfer . sourceThreadId === target . thread . id &&
2329- transfer . targetThreadId === parent . thread . id ,
2330- ) ;
2331- const resultRunId = transfer ?. sourcePoint . runId ?? delegatedTaskRun ( target , task ) ?. id ;
2332- const resultRunIds = resultRunId === undefined ? [ ] : [ resultRunId ] ;
2333- const resultRecords = yield * threadManagement
2334- . getThreadRecords ( target . thread . id , [ "messages" , "turnItems" ] , {
2335- messageRoles : [ "assistant" ] ,
2336- messageRunIds : resultRunIds ,
2337- turnItemRunIds : resultRunIds ,
2338- turnItemTypes : [ "assistant_message" , "error" ] ,
2339- } )
2340- . pipe ( Effect . mapError ( threadManagementFailure ) ) ;
2341- if (
2342- pageIncludesTerminalTaskResult ( {
2343- parent,
2344- page,
2345- task,
2346- target : { ...target , ...resultRecords } ,
2347- maxChars,
2348- } )
2349- ) {
2350- yield * readTask (
2351- scope as McpThreadInvocationScope ,
2352- task . id ,
2353- false ,
2354- true ,
2355- "thread-read-acknowledge" ,
2356- ) ;
2372+ for ( const task of tasks ) {
2373+ const transfer = taskResultTransfer ( parent , target , task ) ;
2374+ const resultRunId = transfer ?. sourcePoint . runId ?? delegatedTaskRun ( target , task ) ?. id ;
2375+ const resultRunIds = resultRunId === undefined ? [ ] : [ resultRunId ] ;
2376+ const resultRecords = yield * threadManagement
2377+ . getThreadRecords ( target . thread . id , [ "messages" , "turnItems" ] , {
2378+ messageRoles : [ "assistant" ] ,
2379+ messageRunIds : resultRunIds ,
2380+ turnItemRunIds : resultRunIds ,
2381+ turnItemTypes : [ "assistant_message" , "error" ] ,
2382+ } )
2383+ . pipe ( Effect . mapError ( threadManagementFailure ) ) ;
2384+ if (
2385+ pageIncludesTerminalTaskResult ( {
2386+ parent,
2387+ page,
2388+ task,
2389+ target : { ...target , ...resultRecords } ,
2390+ maxChars,
2391+ } )
2392+ ) {
2393+ yield * readTask (
2394+ scope as McpThreadInvocationScope ,
2395+ task . id ,
2396+ false ,
2397+ true ,
2398+ "thread-read-acknowledge" ,
2399+ ) ;
2400+ }
23572401 }
23582402 }
23592403 return {
@@ -2419,6 +2463,9 @@ const make = Effect.gen(function* () {
24192463 threadId : input . threadId ,
24202464 messageId,
24212465 runId : result . run . id ,
2466+ ...( result . run . delegatedTaskId === undefined
2467+ ? { }
2468+ : { taskId : result . run . delegatedTaskId } ) ,
24222469 status : result . run . status ,
24232470 delivery : result . delivery ,
24242471 } satisfies OrchestratorMcpThreadSendResult ;
0 commit comments