88 NodeId ,
99 ORCHESTRATOR_MCP_RUNNING_TASK_NEXT ,
1010 TurnItemId ,
11+ type OrchestrationV2RemoteTaskRequest ,
1112 type OrchestrationV2Run ,
1213 type OrchestrationV2Subagent ,
1314 type OrchestrationV2ThreadProjection ,
@@ -63,6 +64,7 @@ import {
6364 type ServerProviderModel ,
6465 ThreadId ,
6566 isOrchestrationV2WorkActive ,
67+ unansweredRemoteTaskRequests ,
6668} from "@t3tools/contracts" ;
6769import { getProviderOptionCurrentValue } from "@t3tools/shared/model" ;
6870import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError" ;
@@ -131,6 +133,10 @@ const TASK_WAKE_EVENTS = [
131133 { thread : "child" , eventType : "provider-thread.updated" } ,
132134 { thread : "child" , eventType : "runtime-request.updated" } ,
133135] as const ;
136+ const REMOTE_TASK_WAKE_EVENTS = [
137+ { thread : "parent" , eventType : "subagent.updated" } ,
138+ { thread : "parent" , eventType : "thread.metadata-updated" } ,
139+ ] as const ;
134140const SECRET_REQUEST_POLL_INTERVAL_MS = 500 ;
135141const DEFAULT_THREAD_LIST_LIMIT = 50 ;
136142const DEFAULT_THREAD_READ_LIMIT = 50 ;
@@ -510,6 +516,23 @@ function pendingTaskApproval(
510516 return approval ;
511517}
512518
519+ /** The oldest approval a remote task's thread there waits on that the user has not answered here. */
520+ function remoteTaskApproval (
521+ task : OrchestrationV2Subagent ,
522+ unanswered : ReadonlyArray < OrchestrationV2RemoteTaskRequest > ,
523+ ) : OrchestratorMcpDelegateTaskResult [ "pendingApproval" ] {
524+ for ( const request of unanswered . toSorted (
525+ ( left , right ) =>
526+ DateTime . toEpochMillis ( left . createdAt ) - DateTime . toEpochMillis ( right . createdAt ) ,
527+ ) ) {
528+ const runId = request . runId ?? task . remoteChild ?. runId ;
529+ if ( request . kind !== "user_input" && runId !== undefined ) {
530+ return { requestId : request . requestId , kind : request . kind , runId } ;
531+ }
532+ }
533+ return null ;
534+ }
535+
513536function directAppOwnedChildTasks (
514537 parent : Pick < OrchestrationV2ThreadProjection , "thread" | "subagents" > ,
515538 target : Pick < OrchestrationV2ThreadProjection , "thread" | "runs" > ,
@@ -1411,7 +1434,15 @@ const make = Effect.gen(function* () {
14111434 ) ;
14121435 }
14131436 if ( task . childThreadId === null ) {
1414- return yield * readRemoteTask ( scope , task , waitTimedOut , acknowledgeTerminal ) ;
1437+ return yield * readRemoteTask (
1438+ scope ,
1439+ task ,
1440+ unansweredRemoteTaskRequests ( parentProjection . thread ) . filter (
1441+ ( request ) => request . taskId === task . id ,
1442+ ) ,
1443+ waitTimedOut ,
1444+ acknowledgeTerminal ,
1445+ ) ;
14151446 }
14161447 const childControls = yield * threadManagement
14171448 . getThreadRecords (
@@ -1607,8 +1638,8 @@ const make = Effect.gen(function* () {
16071638 Math . max ( 1 , requestedTimeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS ) ,
16081639 ) ;
16091640 const waited = yield * waitForTask ( scope , taskId , timeoutMs ) ;
1610- if ( Option . isSome ( waited ) ) return waited . value ;
1611- // As for a local task: once the blocking wait ends, a later result wakes the parent.
1641+ if ( Option . isSome ( waited ) && isTerminalTaskStatus ( waited . value . status ) ) return waited . value ;
1642+ // As for a local task: once the blocking wait ends without a result , a later one wakes the parent.
16121643 yield * threadManagement
16131644 . dispatch ( {
16141645 type : "delegated_task.wake-policy" ,
@@ -1622,7 +1653,7 @@ const make = Effect.gen(function* () {
16221653 completionWake : "always" ,
16231654 } )
16241655 . pipe ( Effect . ignoreCause ( { log : true } ) ) ;
1625- return yield * readTask ( scope , taskId , true , true ) ;
1656+ return yield * readTask ( scope , taskId , Option . isNone ( waited ) , true ) ;
16261657 } ) ;
16271658
16281659 /**
@@ -1632,6 +1663,7 @@ const make = Effect.gen(function* () {
16321663 const readRemoteTask = Effect . fn ( "OrchestratorMcpService.readRemoteTask" ) ( function * (
16331664 scope : McpThreadInvocationScope ,
16341665 task : OrchestrationV2Subagent ,
1666+ unanswered : ReadonlyArray < OrchestrationV2RemoteTaskRequest > ,
16351667 waitTimedOut : boolean ,
16361668 acknowledgeTerminal : boolean ,
16371669 ) : Effect . fn . Return < OrchestratorMcpDelegateTaskResult , OrchestratorMcpFailure > {
@@ -1698,9 +1730,8 @@ const make = Effect.gen(function* () {
16981730 : null ,
16991731 latestTerminalSummary : terminal ? task . result : null ,
17001732 latestTerminalResultContextTransferId : null ,
1701- // Its questions and approvals are answered in the environment it runs in.
1702- hasPendingQuestion : false ,
1703- pendingApproval : null ,
1733+ hasPendingQuestion : ! terminal && unanswered . some ( ( request ) => request . kind === "user_input" ) ,
1734+ pendingApproval : terminal ? null : remoteTaskApproval ( task , unanswered ) ,
17041735 waitTimedOut,
17051736 } satisfies OrchestratorMcpDelegateTaskResult ;
17061737 } ) ;
@@ -1721,12 +1752,10 @@ const make = Effect.gen(function* () {
17211752 return Option . some ( initial ) ;
17221753 }
17231754 // One stream per event type, so transcript events never fill a buffer.
1724- // A remote task has no child here; its completion lands on the parent.
1755+ // A remote task has no child here; its completion lands on the parent, and so
1756+ // does the mirror of what its thread there waits on.
17251757 const childThreadId = initial . childThreadId ;
1726- const wakeEvents =
1727- childThreadId === null
1728- ? TASK_WAKE_EVENTS . filter ( ( { thread } ) => thread === "parent" )
1729- : TASK_WAKE_EVENTS ;
1758+ const wakeEvents = childThreadId === null ? REMOTE_TASK_WAKE_EVENTS : TASK_WAKE_EVENTS ;
17301759 return yield * Stream . mergeAll (
17311760 wakeEvents . map ( ( { thread, eventType } ) =>
17321761 threadManagement . streamStoredEventsFrom ( {
0 commit comments