@@ -214,6 +214,7 @@ function makeBaseParams(overrides: {
214214 runStartedAt ?: number ;
215215 sessionTarget ?: string ;
216216 deliveryBestEffort ?: boolean ;
217+ spawnOnlyHandoff ?: boolean ;
217218 runSessionKey ?: string ;
218219 resolvedDeliveryMode ?: "explicit" | "implicit" ;
219220} ) : Parameters < typeof dispatchCronDelivery > [ 0 ] {
@@ -245,6 +246,7 @@ function makeBaseParams(overrides: {
245246 resolvedDelivery,
246247 deliveryRequested : overrides . deliveryRequested ?? true ,
247248 skipHeartbeatDelivery : false ,
249+ spawnOnlyHandoff : overrides . spawnOnlyHandoff ?? false ,
248250 sourceDeliveryOutcome : {
249251 visibleDeliveries : [ ] ,
250252 verifiedMessageToolDelivery : false ,
@@ -1101,6 +1103,169 @@ describe("dispatchCronDelivery — double-announce guard", () => {
11011103 } ) ;
11021104 } ) ;
11031105
1106+ it . each ( [
1107+ {
1108+ name : "active direct" ,
1109+ activeDescendants : true ,
1110+ threadId : undefined ,
1111+ deliveryBestEffort : false ,
1112+ } ,
1113+ {
1114+ name : "active threaded" ,
1115+ activeDescendants : true ,
1116+ threadId : "42" ,
1117+ deliveryBestEffort : false ,
1118+ } ,
1119+ {
1120+ name : "completed direct" ,
1121+ activeDescendants : false ,
1122+ threadId : undefined ,
1123+ deliveryBestEffort : false ,
1124+ } ,
1125+ {
1126+ name : "completed threaded" ,
1127+ activeDescendants : false ,
1128+ threadId : "42" ,
1129+ deliveryBestEffort : false ,
1130+ } ,
1131+ {
1132+ name : "active best-effort direct" ,
1133+ activeDescendants : true ,
1134+ threadId : undefined ,
1135+ deliveryBestEffort : true ,
1136+ } ,
1137+ ] ) (
1138+ "delivers $name accepted child results without parent text" ,
1139+ async ( { activeDescendants, deliveryBestEffort, threadId } ) => {
1140+ const childReply = "Completed child result visible to the user." ;
1141+ if ( activeDescendants ) {
1142+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValueOnce ( 1 ) . mockReturnValueOnce ( 0 ) ;
1143+ } else {
1144+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( 0 ) ;
1145+ }
1146+ vi . mocked ( waitForDescendantSubagentSummary ) . mockResolvedValue ( undefined ) ;
1147+ vi . mocked ( readDescendantSubagentFallbackReply ) . mockResolvedValue ( childReply ) ;
1148+
1149+ const params = makeBaseParams ( {
1150+ spawnOnlyHandoff : true ,
1151+ deliveryBestEffort,
1152+ synthesizedText : "" ,
1153+ } ) ;
1154+ params . synthesizedText = undefined ;
1155+ params . deliveryPayloads = [ ] ;
1156+ params . summary = undefined ;
1157+ params . outputText = undefined ;
1158+ params . resolvedDelivery = makeResolvedDelivery ( { threadId } ) ;
1159+
1160+ const state = await dispatchCronDelivery ( params ) ;
1161+
1162+ expect ( waitForDescendantSubagentSummary ) . toHaveBeenCalledTimes ( activeDescendants ? 1 : 0 ) ;
1163+ expect ( readDescendantSubagentFallbackReply ) . toHaveBeenCalledWith ( {
1164+ sessionKey : params . runSessionKey ,
1165+ runStartedAt : params . runStartedAt ,
1166+ } ) ;
1167+ expect ( deliverOutboundPayloads ) . toHaveBeenCalledTimes ( 1 ) ;
1168+ expectDeliveryCall ( 0 , {
1169+ channel : "telegram" ,
1170+ to : "123456" ,
1171+ ...( threadId === undefined ? { } : { threadId } ) ,
1172+ payloads : [ { text : childReply } ] ,
1173+ } ) ;
1174+ expect ( state . delivered ) . toBe ( true ) ;
1175+ expect ( state . deliveryAttempted ) . toBe ( true ) ;
1176+ } ,
1177+ ) ;
1178+
1179+ it ( "preserves a substantive parent synthesis after an accepted child has completed" , async ( ) => {
1180+ const parentReply = "Combined parent summary already includes every child result." ;
1181+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( 0 ) ;
1182+
1183+ const state = await dispatchCronDelivery (
1184+ makeBaseParams ( { spawnOnlyHandoff : false , synthesizedText : parentReply } ) ,
1185+ ) ;
1186+
1187+ expect ( readDescendantSubagentFallbackReply ) . not . toHaveBeenCalled ( ) ;
1188+ expectDeliveryCall ( 0 , { payloads : [ { text : parentReply } ] } ) ;
1189+ expect ( state . delivered ) . toBe ( true ) ;
1190+ } ) ;
1191+
1192+ it ( "immediately delivers a substantive threaded parent while its accepted child runs" , async ( ) => {
1193+ const parentReply = "Parent summary is ready for the existing thread." ;
1194+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( 1 ) ;
1195+ const params = makeBaseParams ( { spawnOnlyHandoff : false , synthesizedText : parentReply } ) ;
1196+ params . resolvedDelivery = makeResolvedDelivery ( { threadId : "42" } ) ;
1197+
1198+ const state = await dispatchCronDelivery ( params ) ;
1199+
1200+ expect ( waitForDescendantSubagentSummary ) . not . toHaveBeenCalled ( ) ;
1201+ expect ( deliverOutboundPayloads ) . toHaveBeenCalledTimes ( 1 ) ;
1202+ expectDeliveryCall ( 0 , { threadId : "42" , payloads : [ { text : parentReply } ] } ) ;
1203+ expect ( state . delivered ) . toBe ( true ) ;
1204+ } ) ;
1205+
1206+ it . each ( [
1207+ {
1208+ name : "active child times out" ,
1209+ activeDescendants : 1 ,
1210+ error : "cron child-session handoff timed out before producing a final assistant payload" ,
1211+ } ,
1212+ {
1213+ name : "completed child has no output" ,
1214+ activeDescendants : 0 ,
1215+ error : "cron child-session handoff completed without a final assistant payload" ,
1216+ } ,
1217+ ] ) ( "fails an accepted spawn-only handoff when $name" , async ( { activeDescendants, error } ) => {
1218+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( activeDescendants ) ;
1219+ const params = makeBaseParams ( { spawnOnlyHandoff : true , synthesizedText : "" } ) ;
1220+ params . synthesizedText = undefined ;
1221+ params . deliveryPayloads = [ ] ;
1222+ params . summary = undefined ;
1223+ params . outputText = undefined ;
1224+
1225+ const state = await dispatchCronDelivery ( params ) ;
1226+
1227+ expectResultFields ( state . result , {
1228+ status : "error" ,
1229+ error,
1230+ delivered : false ,
1231+ deliveryAttempted : true ,
1232+ } ) ;
1233+ expect ( deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
1234+ } ) ;
1235+
1236+ it ( "preserves abort precedence when an accepted child handoff is interrupted" , async ( ) => {
1237+ const abortReason = "scheduled run aborted while waiting for its child" ;
1238+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( 1 ) ;
1239+ const params = makeBaseParams ( { spawnOnlyHandoff : true , synthesizedText : "" } ) ;
1240+ params . synthesizedText = undefined ;
1241+ params . deliveryPayloads = [ ] ;
1242+ params . summary = undefined ;
1243+ params . outputText = undefined ;
1244+ params . isAborted = ( ) => true ;
1245+ params . abortReason = ( ) => abortReason ;
1246+
1247+ const state = await dispatchCronDelivery ( params ) ;
1248+
1249+ expect ( waitForDescendantSubagentSummary ) . toHaveBeenCalledTimes ( 1 ) ;
1250+ expectResultFields ( state . result , { status : "error" , error : abortReason } ) ;
1251+ expect ( deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
1252+ } ) ;
1253+
1254+ it ( "keeps an empty no-spawn parent silent" , async ( ) => {
1255+ const params = makeBaseParams ( { synthesizedText : "" } ) ;
1256+ params . synthesizedText = undefined ;
1257+ params . deliveryPayloads = [ ] ;
1258+ params . summary = undefined ;
1259+ params . outputText = undefined ;
1260+
1261+ const state = await dispatchCronDelivery ( params ) ;
1262+
1263+ expect ( waitForDescendantSubagentSummary ) . not . toHaveBeenCalled ( ) ;
1264+ expect ( readDescendantSubagentFallbackReply ) . not . toHaveBeenCalled ( ) ;
1265+ expect ( deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
1266+ expect ( state . deliveryAttempted ) . toBe ( false ) ;
1267+ } ) ;
1268+
11041269 it ( "uses the run-scoped session key for isolated cron descendant fallback delivery" , async ( ) => {
11051270 const runStartedAt = 1_000 ;
11061271 const agentSessionKey = "agent:main:cron:daily-monitor" ;
0 commit comments