@@ -386,6 +386,98 @@ it.layer(TestLayer)("delegated completion delivery repairs", (it) => {
386386 } ) ,
387387 ) ;
388388
389+ it . effect ( "acceptance batches a settled_only sibling once its spawning run ended" , ( ) =>
390+ Effect . gen ( function * ( ) {
391+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
392+ const sink = yield * EventSink . EventSinkV2 ;
393+ const now = yield * DateTime . now ;
394+ const threadId = ThreadId . make ( "mailbox-mixed" ) ;
395+ const runId = RunId . make ( "mailbox-mixed-parent" ) ;
396+ const taskId = NodeId . make ( "mailbox-mixed-always" ) ;
397+ const siblingId = NodeId . make ( "mailbox-mixed-settled-only" ) ;
398+ const messageId = MessageId . make ( `message:delegated-delivery:${ threadId } ` ) ;
399+ yield * seedParentWithTerminalTask ( {
400+ threadId,
401+ runId,
402+ projectId : ProjectId . make ( "mailbox-mixed-project" ) ,
403+ rootNodeId : NodeId . make ( "mailbox-mixed-root" ) ,
404+ taskId,
405+ deliveryState : "claimed" ,
406+ completionWake : "always" ,
407+ deliveryTaskIds : [ taskId ] ,
408+ now,
409+ } ) ;
410+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
411+ const task = projection . subagents [ 0 ] ! ;
412+ const spawningRun = projection . runs . find ( ( row ) => row . id === runId ) ! ;
413+ // A restart cut the spawning run; its continuation is the live run now.
414+ yield * sink . write ( {
415+ events : [
416+ {
417+ ...runEvent ( { threadId, runId, ordinal : 1 , status : "cancelled" , now } ) ,
418+ payload : { ...spawningRun , status : "cancelled" as const , completedAt : now } ,
419+ } ,
420+ runEvent ( {
421+ threadId,
422+ runId : RunId . make ( "mailbox-mixed-continuation" ) ,
423+ ordinal : 2 ,
424+ status : "running" ,
425+ now,
426+ } ) ,
427+ {
428+ id : EventId . make ( "mailbox-mixed-message" ) ,
429+ type : "message.updated" ,
430+ threadId,
431+ runId,
432+ occurredAt : now ,
433+ payload : {
434+ id : messageId ,
435+ threadId,
436+ runId,
437+ nodeId : task . parentNodeId ,
438+ role : "user" ,
439+ text : "Background task finished" ,
440+ attachments : [ ] ,
441+ streaming : false ,
442+ createdBy : "agent" ,
443+ creationSource : "server" ,
444+ createdAt : now ,
445+ updatedAt : now ,
446+ delegatedCompletion : { parentRunId : runId , generation : 1 , taskIds : [ taskId ] } ,
447+ } ,
448+ } ,
449+ {
450+ id : EventId . make ( `event:${ siblingId } ` ) ,
451+ type : "subagent.updated" as const ,
452+ threadId,
453+ runId,
454+ nodeId : siblingId ,
455+ occurredAt : now ,
456+ payload : {
457+ ...task ,
458+ id : siblingId ,
459+ completionWake : "settled_only" as const ,
460+ completionDelivery : { state : "pending" as const , observedByRunId : null } ,
461+ } ,
462+ } ,
463+ ] ,
464+ } ) ;
465+ yield * orchestrator . dispatch ( {
466+ type : "notification.delivery.accept" ,
467+ commandId : CommandId . make ( "accept-mixed" ) ,
468+ threadId,
469+ messageId,
470+ } ) ;
471+ const accepted = yield * orchestrator . getThreadProjection ( threadId ) ;
472+ const cohort = accepted . runs . find ( ( row ) => row . id === runId ) ?. delegatedCompletion ;
473+ assert . deepEqual ( cohort ?. delivery ?. taskIds , [ siblingId ] ) ;
474+ assert . equal (
475+ accepted . subagents . find ( ( row ) => row . id === siblingId ) ?. completionDelivery ?. state ,
476+ "claimed" ,
477+ ) ;
478+ } ) ,
479+ ) ;
480+
389481 it . effect ( "builds completion text and metadata from the same live cohort" , ( ) =>
390482 Effect . gen ( function * ( ) {
391483 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
@@ -852,6 +944,38 @@ it.layer(TestLayer)("delegated tasks across a server restart", (it) => {
852944 const stopped = yield * child ( "restart-stopped-child" , false ) ;
853945 // Settled with only background work left: its interim reply is not the result.
854946 const backgrounded = yield * child ( "restart-backgrounded-child" , true , "completed" ) ;
947+ // A second restart cut the first continuation before it started.
948+ const recut = yield * child ( "restart-recut-child" , false ) ;
949+ const recutContinuationId = RunId . make ( "run:restart-recut-child:2" ) ;
950+ const recutCommandId = CommandId . make ( "command:restart-recut-child:reconcile" ) ;
951+ const recutRun = runEvent ( {
952+ threadId : recut . childThreadId ,
953+ runId : recutContinuationId ,
954+ ordinal : 2 ,
955+ status : "cancelled" ,
956+ now,
957+ } ) ;
958+ yield * eventSink . writeWithEffects ( {
959+ commandId : recutCommandId ,
960+ events : [
961+ {
962+ ...recutRun ,
963+ payload : {
964+ ...recutRun . payload ,
965+ startedAt : null ,
966+ restartContinuationOfRunId : recut . childRunId ,
967+ } ,
968+ } ,
969+ ] ,
970+ effects : [
971+ {
972+ id : `effect:restart-continuation:${ recutContinuationId } ` ,
973+ commandId : recutCommandId ,
974+ threadId : recut . childThreadId ,
975+ request : { type : "provider-runtime.continue" , sourceRunId : recutContinuationId } ,
976+ } ,
977+ ] ,
978+ } ) ;
855979
856980 yield * orchestrator . recoverDelegatedTasks ;
857981
@@ -863,6 +987,10 @@ it.layer(TestLayer)("delegated tasks across a server restart", (it) => {
863987 assert . isNull ( task ( resumed . taskId ) ?. result ?? null ) ;
864988 assert . equal ( task ( backgrounded . taskId ) ?. status , "running" ) ;
865989 assert . isNull ( task ( backgrounded . taskId ) ?. result ?? null ) ;
990+ assert . equal ( task ( recut . taskId ) ?. status , "running" ) ;
991+ assert . isTrue ( yield * orchestrator . delegatedTaskAwaitsRestart ( recut . childThreadId ) ) ;
992+ assert . isTrue ( yield * orchestrator . delegatedTaskAwaitsRestart ( resumed . childThreadId ) ) ;
993+ assert . isFalse ( yield * orchestrator . delegatedTaskAwaitsRestart ( stopped . childThreadId ) ) ;
866994 assert . isFalse (
867995 recovered . contextTransfers . some (
868996 ( transfer ) => transfer . sourceThreadId === resumed . childThreadId ,
0 commit comments