@@ -2404,7 +2404,8 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24042404 } ) ,
24052405 ) ;
24062406
2407- // Finalizing a child reads, from its parent, only that child's tasks and the transfers it sent.
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.
24082409 it . effect ( "finalizing a delegated child reads only that child's records from its parent" , ( ) =>
24092410 Effect . gen ( function * ( ) {
24102411 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
@@ -2413,15 +2414,31 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24132414 const projectId = ProjectId . make ( "project:narrow-finalize" ) ;
24142415 const runId = RunId . make ( "run:narrow-finalize-parent" ) ;
24152416 const rootNodeId = NodeId . make ( "node:narrow-finalize-parent" ) ;
2417+ const siblingTaskId = NodeId . make ( "node:narrow-finalize-seed" ) ;
2418+ const messageId = MessageId . make ( `message:delegated-delivery:${ threadId } ` ) ;
24162419 yield * seedParentWithTerminalTask ( {
24172420 threadId,
24182421 projectId,
24192422 runId,
24202423 rootNodeId,
2421- taskId : NodeId . make ( "node:narrow-finalize-seed" ) ,
2422- deliveryState : "acknowledged" ,
2424+ taskId : siblingTaskId ,
2425+ deliveryState : "claimed" ,
2426+ completionWake : "always" ,
2427+ deliveryTaskIds : [ siblingTaskId ] ,
24232428 now,
24242429 } ) ;
2430+ yield * orchestrator . dispatch ( {
2431+ type : "message.dispatch" ,
2432+ commandId : CommandId . make ( "command:narrow-finalize-delivery" ) ,
2433+ threadId,
2434+ messageId,
2435+ text : `Delegated task ${ siblingTaskId } reached a terminal state.` ,
2436+ attachments : [ ] ,
2437+ dispatchMode : { type : "queue_after_active" } ,
2438+ createdBy : "agent" ,
2439+ creationSource : "server" ,
2440+ delegatedCompletion : { parentRunId : runId , generation : 1 , taskIds : [ siblingTaskId ] } ,
2441+ } ) ;
24252442 const child = yield * seedRestartCancelledChild ( {
24262443 parentThreadId : threadId ,
24272444 projectId,
@@ -2447,27 +2464,33 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24472464 yield * orchestrator . recoverDelegatedTasks . pipe (
24482465 Effect . provideService ( Statement . CurrentTransformer , observe ) ,
24492466 ) ;
2450- const task = ( yield * orchestrator . getThreadProjection ( threadId ) ) . subagents . find (
2451- ( candidate ) => candidate . id === child . taskId ,
2452- ) ;
2467+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
2468+ const task = projection . subagents . find ( ( candidate ) => candidate . id === child . taskId ) ;
24532469 assert . equal ( task ?. status , "completed" ) ;
2454- // The finalize read names the child: its tasks by child thread, its transfers by source.
2455- assert . isTrue (
2456- parentReads . some (
2457- ( query ) =>
2458- query . includes ( "FROM orchestration_v2_projection_subagents" ) &&
2459- / A N D c h i l d _ t h r e a d _ i d ( = | I N ) / . test ( query ) ,
2460- ) ,
2461- "the parent's subagent read is not narrowed to the child" ,
2470+ // Every finalize read names its rows: tasks by child thread or id, transfers by source.
2471+ const subagentReads = parentReads . filter ( ( query ) =>
2472+ query . includes ( "FROM orchestration_v2_projection_subagents" ) ,
24622473 ) ;
2463- assert . isTrue (
2464- parentReads . some (
2465- ( query ) =>
2466- query . includes ( "FROM orchestration_v2_projection_context_transfers" ) &&
2467- / A N D s o u r c e _ t h r e a d _ i d ( = | I N ) / . test ( query ) ,
2468- ) ,
2469- "the parent's context-transfer read is not narrowed to the child" ,
2474+ const transferReads = parentReads . filter ( ( query ) =>
2475+ query . includes ( "FROM orchestration_v2_projection_context_transfers" ) ,
2476+ ) ;
2477+ assert . isNotEmpty ( subagentReads ) ;
2478+ assert . isNotEmpty ( transferReads ) ;
2479+ assert . deepEqual (
2480+ subagentReads . filter ( ( query ) => ! / A N D ( c h i l d _ t h r e a d _ i d | s u b a g e n t _ i d ) ( = | I N ) / . test ( query ) ) ,
2481+ [ ] ,
2482+ "a parent subagent read is not narrowed" ,
24702483 ) ;
2484+ assert . deepEqual (
2485+ transferReads . filter ( ( query ) => ! / A N D s o u r c e _ t h r e a d _ i d ( = | I N ) / . test ( query ) ) ,
2486+ [ ] ,
2487+ "a parent context-transfer read is not narrowed" ,
2488+ ) ;
2489+ // The delivery the child joins still names the sibling it was opened for.
2490+ const message = projection . messages . find ( ( candidate ) => candidate . id === messageId ) ;
2491+ assert . deepEqual ( message ?. delegatedCompletion ?. taskIds , [ siblingTaskId , child . taskId ] ) ;
2492+ assert . include ( message ?. text ?? "" , `Delegated task ${ siblingTaskId } [completed]` ) ;
2493+ assert . include ( message ?. text ?? "" , `Delegated task ${ child . taskId } ` ) ;
24712494 } ) ,
24722495 ) ;
24732496
0 commit comments