@@ -2311,6 +2311,108 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
23112311 } ,
23122312) ;
23132313
2314+ // The run-update listener mirrors a follow-up's status onto its task outside any test fiber, so
2315+ // its statements are recorded from the layer down, for one parent thread at a time.
2316+ let recordReadsOf : ThreadId | undefined ;
2317+ const recordedReads : Array < string > = [ ] ;
2318+ const layerRecordingReads = Layer . succeed ( Statement . CurrentTransformer , ( statement ) => {
2319+ const [ query , params ] = statement . compile ( ) ;
2320+ if ( recordReadsOf !== undefined && params . includes ( recordReadsOf ) ) recordedReads . push ( query ) ;
2321+ return Effect . succeed ( statement ) ;
2322+ } ) ;
2323+
2324+ it . layer (
2325+ Layer . merge ( layerTest , ProviderContinuationRequests . layer ) . pipe (
2326+ Layer . provide ( layerRecordingReads ) ,
2327+ ) ,
2328+ ) ( "subagent follow-up status" , ( it ) => {
2329+ it . effect ( "mirrors a follow-up's status reading only its task's node and card" , ( ) =>
2330+ Effect . gen ( function * ( ) {
2331+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
2332+ const eventSink = yield * EventSink . EventSinkV2 ;
2333+ const now = yield * DateTime . now ;
2334+ const parentThreadId = ThreadId . make ( "thread:narrow-status-parent" ) ;
2335+ const projectId = ProjectId . make ( "project:narrow-status" ) ;
2336+ const parentRunId = RunId . make ( "run:narrow-status-parent" ) ;
2337+ const rootNodeId = NodeId . make ( "node:narrow-status-parent" ) ;
2338+ yield * seedParentWithTerminalTask ( {
2339+ threadId : parentThreadId ,
2340+ projectId,
2341+ runId : parentRunId ,
2342+ rootNodeId,
2343+ taskId : NodeId . make ( "node:narrow-status-seed" ) ,
2344+ deliveryState : "acknowledged" ,
2345+ now,
2346+ } ) ;
2347+ const child = yield * seedRestartCancelledChild ( {
2348+ parentThreadId,
2349+ projectId,
2350+ parentRunId,
2351+ rootNodeId,
2352+ name : "narrow-status-child" ,
2353+ completionWake : "always" ,
2354+ continuationPending : false ,
2355+ runStatus : "completed" ,
2356+ now,
2357+ } ) ;
2358+ yield * orchestrator . recoverDelegatedTasks ;
2359+ for ( const name of [ "active" , "queued" ] ) {
2360+ yield * orchestrator . dispatch ( {
2361+ type : "message.dispatch" ,
2362+ createdBy : "agent" ,
2363+ creationSource : "mcp" ,
2364+ commandId : CommandId . make ( `command:narrow-status:${ name } ` ) ,
2365+ threadId : child . childThreadId ,
2366+ senderThreadId : parentThreadId ,
2367+ messageId : MessageId . make ( `message:narrow-status:${ name } ` ) ,
2368+ text : name ,
2369+ attachments : [ ] ,
2370+ dispatchMode : { type : "queue_after_active" } ,
2371+ } ) ;
2372+ }
2373+ const queued = ( yield * orchestrator . getThreadProjection ( child . childThreadId ) ) . runs . find (
2374+ ( run ) => run . ordinal === 3 ,
2375+ ) ! ;
2376+ assert . equal ( queued . status , "queued" ) ;
2377+ const afterSequence = yield * eventSink . latestSequence ( ) ;
2378+ recordReadsOf = parentThreadId ;
2379+ yield * orchestrator . dispatch ( {
2380+ type : "thread.stop" ,
2381+ threadId : child . childThreadId ,
2382+ commandId : CommandId . make ( "command:narrow-status:stop" ) ,
2383+ } ) ;
2384+ yield * eventSink . stream ( { afterSequence, eventType : "subagent.updated" } ) . pipe (
2385+ Stream . filter (
2386+ ( stored ) =>
2387+ stored . event . type === "subagent.updated" &&
2388+ stored . event . payload . id === queued . delegatedTaskId &&
2389+ stored . event . payload . status === "idle" ,
2390+ ) ,
2391+ Stream . take ( 1 ) ,
2392+ Stream . runHead ,
2393+ ) ;
2394+ recordReadsOf = undefined ;
2395+ const paused = yield * orchestrator . getThreadProjection ( parentThreadId ) ;
2396+ assert . equal ( paused . nodes . find ( ( row ) => row . id === queued . delegatedTaskId ) ?. status , "idle" ) ;
2397+ assert . equal (
2398+ paused . turnItems . find (
2399+ ( row ) => row . type === "subagent" && row . subagentId === queued . delegatedTaskId ,
2400+ ) ?. status ,
2401+ "idle" ,
2402+ ) ;
2403+ const nodeReads = recordedReads . filter ( ( query ) =>
2404+ / S E L E C T p a y l o a d _ j s o n \s + F R O M o r c h e s t r a t i o n _ v 2 _ p r o j e c t i o n _ ( n o d e s | t u r n _ i t e m s ) \b / . test ( query ) ,
2405+ ) ;
2406+ assert . isNotEmpty ( nodeReads ) ;
2407+ assert . deepEqual (
2408+ nodeReads . filter ( ( query ) => ! / A N D ( n o d e \. ) ? n o d e _ i d I N / . test ( query ) ) ,
2409+ [ ] ,
2410+ "a parent node or card read is not narrowed" ,
2411+ ) ;
2412+ } ) ,
2413+ ) ;
2414+ } ) ;
2415+
23142416it . layer ( layerTest ) ( "delegated tasks across a server restart" , ( it ) => {
23152417 it . effect ( "queues a child result without creating a provider history handoff" , ( ) =>
23162418 Effect . gen ( function * ( ) {
@@ -2404,11 +2506,12 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24042506 } ) ,
24052507 ) ;
24062508
2407- // Finalizing a child reads, from its parent, only that child's tasks and the transfers it sent,
2408- // and the other tasks of a delivery it joins by id.
2509+ // Finalizing a child reads, from its parent, only that child's task, node, card and the transfers
2510+ // it sent, and the message and other tasks of a delivery it joins by id.
24092511 it . effect ( "finalizing a delegated child reads only that child's records from its parent" , ( ) =>
24102512 Effect . gen ( function * ( ) {
24112513 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
2514+ const sink = yield * EventSink . EventSinkV2 ;
24122515 const now = yield * DateTime . now ;
24132516 const threadId = ThreadId . make ( "thread:narrow-finalize-parent" ) ;
24142517 const projectId = ProjectId . make ( "project:narrow-finalize" ) ;
@@ -2450,12 +2553,81 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24502553 runStatus : "completed" ,
24512554 now,
24522555 } ) ;
2556+ const cards = [
2557+ { taskId : siblingTaskId , status : "completed" as const , ordinal : 100 } ,
2558+ { taskId : child . taskId , status : "running" as const , ordinal : 101 } ,
2559+ ] ;
2560+ yield * sink . write ( {
2561+ commandId : CommandId . make ( "command:narrow-finalize-cards" ) ,
2562+ events : cards . flatMap ( ( { taskId, status, ordinal } ) => [
2563+ {
2564+ id : EventId . make ( `event:narrow-finalize-node:${ taskId } ` ) ,
2565+ type : "node.updated" as const ,
2566+ threadId,
2567+ runId,
2568+ nodeId : taskId ,
2569+ occurredAt : now ,
2570+ payload : {
2571+ id : taskId ,
2572+ threadId,
2573+ runId,
2574+ parentNodeId : rootNodeId ,
2575+ rootNodeId,
2576+ kind : "subagent" as const ,
2577+ status,
2578+ countsForRun : false ,
2579+ providerThreadId : null ,
2580+ providerTurnId : null ,
2581+ nativeItemRef : null ,
2582+ runtimeRequestId : null ,
2583+ checkpointScopeId : null ,
2584+ startedAt : now ,
2585+ completedAt : status === "completed" ? now : null ,
2586+ } ,
2587+ } ,
2588+ {
2589+ id : EventId . make ( `event:narrow-finalize-card:${ taskId } ` ) ,
2590+ type : "turn-item.updated" as const ,
2591+ threadId,
2592+ runId,
2593+ nodeId : taskId ,
2594+ occurredAt : now ,
2595+ payload : {
2596+ id : TurnItemId . make ( `turn-item:narrow-finalize:${ taskId } ` ) ,
2597+ type : "subagent" as const ,
2598+ threadId,
2599+ runId,
2600+ nodeId : taskId ,
2601+ providerThreadId : null ,
2602+ providerTurnId : null ,
2603+ nativeItemRef : null ,
2604+ parentItemId : null ,
2605+ ordinal,
2606+ status,
2607+ title : null ,
2608+ startedAt : now ,
2609+ completedAt : status === "completed" ? now : null ,
2610+ updatedAt : now ,
2611+ subagentId : taskId ,
2612+ origin : "app_owned" as const ,
2613+ driver,
2614+ providerInstanceId : modelSelection . instanceId ,
2615+ childThreadId : taskId === child . taskId ? child . childThreadId : null ,
2616+ prompt : `Run ${ taskId } .` ,
2617+ result : status === "completed" ? "child finished" : null ,
2618+ } ,
2619+ } ,
2620+ ] ) ,
2621+ } ) ;
24532622 const parentReads : Array < string > = [ ] ;
24542623 const observe : Statement . Transformer = ( statement ) => {
24552624 const [ query , params ] = statement . compile ( ) ;
24562625 if (
24572626 params . includes ( threadId ) &&
2458- / F R O M o r c h e s t r a t i o n _ v 2 _ p r o j e c t i o n _ ( s u b a g e n t s | c o n t e x t _ t r a n s f e r s ) \b / . test ( query )
2627+ ( / F R O M o r c h e s t r a t i o n _ v 2 _ p r o j e c t i o n _ ( s u b a g e n t s | c o n t e x t _ t r a n s f e r s ) \b / . test ( query ) ||
2628+ / S E L E C T p a y l o a d _ j s o n \s + F R O M o r c h e s t r a t i o n _ v 2 _ p r o j e c t i o n _ ( n o d e s | t u r n _ i t e m s | m e s s a g e s ) \b / . test (
2629+ query ,
2630+ ) )
24592631 ) {
24602632 parentReads . push ( query ) ;
24612633 }
@@ -2486,6 +2658,30 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24862658 [ ] ,
24872659 "a parent context-transfer read is not narrowed" ,
24882660 ) ;
2661+ // Nodes and cards by the task's node, messages by id.
2662+ for ( const [ table , narrowed ] of [
2663+ [ "nodes" , / A N D n o d e \. n o d e _ i d I N / ] ,
2664+ [ "turn_items" , / A N D n o d e _ i d I N / ] ,
2665+ [ "messages" , / A N D m e s s a g e _ i d I N / ] ,
2666+ ] as const ) {
2667+ const reads = parentReads . filter ( ( query ) =>
2668+ query . includes ( `FROM orchestration_v2_projection_${ table } ` ) ,
2669+ ) ;
2670+ assert . isNotEmpty ( reads , table ) ;
2671+ assert . deepEqual (
2672+ reads . filter ( ( query ) => ! narrowed . test ( query ) ) ,
2673+ [ ] ,
2674+ `a parent ${ table } read is not narrowed` ,
2675+ ) ;
2676+ }
2677+ // The child's node and card still end with its task.
2678+ assert . equal ( projection . nodes . find ( ( row ) => row . id === child . taskId ) ?. status , "completed" ) ;
2679+ assert . equal (
2680+ projection . turnItems . find (
2681+ ( row ) => row . type === "subagent" && row . subagentId === child . taskId ,
2682+ ) ?. status ,
2683+ "completed" ,
2684+ ) ;
24892685 // The delivery the child joins still names the sibling it was opened for.
24902686 const message = projection . messages . find ( ( candidate ) => candidate . id === messageId ) ;
24912687 assert . deepEqual ( message ?. delegatedCompletion ?. taskIds , [ siblingTaskId , child . taskId ] ) ;
0 commit comments