@@ -14,6 +14,16 @@ import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
1414import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
1515import { SILENT_REPLY_TOKEN } from "../../auto-reply/tokens.js" ;
1616import * as deliveryQueueSqlite from "../../infra/delivery-queue-sqlite.js" ;
17+ import {
18+ captureActivePluginRegistrySnapshot ,
19+ restoreActivePluginRegistrySnapshot ,
20+ setActivePluginRegistry ,
21+ } from "../../plugins/runtime.js" ;
22+ import {
23+ createDirectOutboundTestAdapter ,
24+ createOutboundTestPlugin ,
25+ createTestRegistry ,
26+ } from "../../test-utils/channel-plugins.js" ;
1727
1828const directCronCompletionRetention = {
1929 idPrefix : "cron-direct-delivery:v1:" ,
@@ -1172,6 +1182,130 @@ describe("dispatchCronDelivery — double-announce guard", () => {
11721182 } ,
11731183 ) ;
11741184
1185+ it . each ( [
1186+ {
1187+ name : "active Telegram topic" ,
1188+ channel : "telegram" ,
1189+ to : "123456" ,
1190+ threadId : 42 ,
1191+ activeDescendants : true ,
1192+ deliveryBestEffort : false ,
1193+ } ,
1194+ {
1195+ name : "completed Telegram topic" ,
1196+ channel : "telegram" ,
1197+ to : "123456" ,
1198+ threadId : "42" ,
1199+ activeDescendants : false ,
1200+ deliveryBestEffort : false ,
1201+ } ,
1202+ {
1203+ name : "active Discord thread" ,
1204+ channel : "discord" ,
1205+ to : "123456789012345678" ,
1206+ threadId : "123456789012345679" ,
1207+ activeDescendants : true ,
1208+ deliveryBestEffort : false ,
1209+ } ,
1210+ {
1211+ name : "completed Slack thread" ,
1212+ channel : "slack" ,
1213+ to : "C1234567890" ,
1214+ threadId : "1712345678.123456" ,
1215+ activeDescendants : false ,
1216+ deliveryBestEffort : false ,
1217+ } ,
1218+ {
1219+ name : "active Matrix thread" ,
1220+ channel : "matrix" ,
1221+ to : "!room:example.org" ,
1222+ threadId : "$event:example.org" ,
1223+ activeDescendants : true ,
1224+ deliveryBestEffort : false ,
1225+ } ,
1226+ {
1227+ name : "completed Microsoft Teams thread" ,
1228+ channel : "msteams" ,
1229+ 1230+ threadId : "1712345678901" ,
1231+ activeDescendants : false ,
1232+ deliveryBestEffort : false ,
1233+ } ,
1234+ {
1235+ name : "active best-effort Slack thread" ,
1236+ channel : "slack" ,
1237+ to : "C1234567890" ,
1238+ threadId : "1712345678.654321" ,
1239+ activeDescendants : true ,
1240+ deliveryBestEffort : true ,
1241+ } ,
1242+ ] ) (
1243+ "delivers the child result instead of an interim parent reply to an $name" ,
1244+ async ( { activeDescendants, channel, deliveryBestEffort, threadId, to } ) => {
1245+ const parentReply = "on it, pulling everything together" ;
1246+ const childReply = "Completed child result visible to the user." ;
1247+ vi . mocked ( isLikelyInterimCronMessage ) . mockImplementation ( ( text ) => text === parentReply ) ;
1248+ if ( activeDescendants ) {
1249+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValueOnce ( 1 ) . mockReturnValueOnce ( 0 ) ;
1250+ vi . mocked ( waitForDescendantSubagentSummary ) . mockResolvedValue ( childReply ) ;
1251+ } else {
1252+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( 0 ) ;
1253+ vi . mocked ( readDescendantSubagentFallbackReply ) . mockResolvedValue ( childReply ) ;
1254+ }
1255+
1256+ const params = makeBaseParams ( {
1257+ spawnOnlyHandoff : true ,
1258+ synthesizedText : parentReply ,
1259+ deliveryBestEffort,
1260+ } ) ;
1261+ params . agentSessionKey = "agent:main:cron:test-job" ;
1262+ params . runSessionKey = "agent:main:cron:test-job:run:test-session-id" ;
1263+ params . job . deleteAfterRun = true ;
1264+ params . resolvedDelivery = makeResolvedDelivery ( { channel, to, threadId } ) ;
1265+ const normalizeTarget = vi . fn ( ( raw : string ) => raw . trim ( ) ) ;
1266+ const previousPluginRegistry = captureActivePluginRegistrySnapshot ( ) ;
1267+ let state : Awaited < ReturnType < typeof dispatchCronDelivery > > ;
1268+ try {
1269+ setActivePluginRegistry (
1270+ createTestRegistry ( [
1271+ {
1272+ pluginId : channel ,
1273+ source : "test" ,
1274+ plugin : createOutboundTestPlugin ( {
1275+ id : channel ,
1276+ outbound : createDirectOutboundTestAdapter ( { channel } ) ,
1277+ messaging : { normalizeTarget } ,
1278+ } ) ,
1279+ } ,
1280+ ] ) ,
1281+ ) ;
1282+ state = await dispatchCronDelivery ( params ) ;
1283+ } finally {
1284+ restoreActivePluginRegistrySnapshot ( previousPluginRegistry ) ;
1285+ }
1286+
1287+ expect ( waitForDescendantSubagentSummary ) . toHaveBeenCalledTimes ( activeDescendants ? 1 : 0 ) ;
1288+ if ( activeDescendants ) {
1289+ expect ( waitForDescendantSubagentSummary ) . toHaveBeenCalledWith (
1290+ expect . objectContaining ( { initialReply : parentReply } ) ,
1291+ ) ;
1292+ }
1293+ expect ( deliverOutboundPayloads ) . toHaveBeenCalledTimes ( 1 ) ;
1294+ expectDeliveryCall ( 0 , {
1295+ channel,
1296+ to,
1297+ threadId,
1298+ payloads : [ { text : childReply } ] ,
1299+ } ) ;
1300+ expect ( normalizeTarget ) . toHaveBeenCalledWith ( to ) ;
1301+ expect ( state . delivered ) . toBe ( true ) ;
1302+ expect ( state . cronRunSessionCleanupAttempted ) . toBe ( true ) ;
1303+ expect ( callGateway ) . toHaveBeenCalledWith (
1304+ expect . objectContaining ( { method : "sessions.delete" } ) ,
1305+ ) ;
1306+ } ,
1307+ ) ;
1308+
11751309 it ( "preserves a substantive parent synthesis after an accepted child has completed" , async ( ) => {
11761310 const parentReply = "Combined parent summary already includes every child result." ;
11771311 vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( 0 ) ;
@@ -1199,35 +1333,88 @@ describe("dispatchCronDelivery — double-announce guard", () => {
11991333 expect ( state . delivered ) . toBe ( true ) ;
12001334 } ) ;
12011335
1336+ it ( "preserves structured parent output followed by an interim threaded reply" , async ( ) => {
1337+ const mediaPayload = { mediaUrl : "https://example.invalid/chart.png" } ;
1338+ const interimPayload = { text : "on it, pulling everything together" } ;
1339+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( 1 ) ;
1340+ vi . mocked ( isLikelyInterimCronMessage ) . mockReturnValue ( true ) ;
1341+ const params = makeBaseParams ( {
1342+ spawnOnlyHandoff : false ,
1343+ synthesizedText : interimPayload . text ,
1344+ } ) ;
1345+ params . deliveryPayloadHasStructuredContent = true ;
1346+ params . deliveryPayloads = [ mediaPayload , interimPayload ] ;
1347+ params . resolvedDelivery = makeResolvedDelivery ( { threadId : "42" } ) ;
1348+
1349+ const state = await dispatchCronDelivery ( params ) ;
1350+
1351+ expect ( waitForDescendantSubagentSummary ) . not . toHaveBeenCalled ( ) ;
1352+ expect ( deliverOutboundPayloads ) . toHaveBeenCalledOnce ( ) ;
1353+ expectDeliveryCall ( 0 , {
1354+ threadId : "42" ,
1355+ payloads : [ mediaPayload , interimPayload ] ,
1356+ } ) ;
1357+ expect ( state . delivered ) . toBe ( true ) ;
1358+ } ) ;
1359+
12021360 it . each ( [
12031361 {
1204- name : "active child times out" ,
1362+ name : "active child with no parent text times out" ,
12051363 activeDescendants : 1 ,
1364+ parentReply : undefined ,
12061365 error : "cron child-session handoff timed out before producing a final assistant payload" ,
12071366 } ,
12081367 {
1209- name : "completed child has no output" ,
1368+ name : "completed child with no parent text has no output" ,
12101369 activeDescendants : 0 ,
1370+ parentReply : undefined ,
12111371 error : "cron child-session handoff completed without a final assistant payload" ,
12121372 } ,
1213- ] ) ( "fails an accepted spawn-only handoff when $name" , async ( { activeDescendants, error } ) => {
1214- vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( activeDescendants ) ;
1215- const params = makeBaseParams ( { spawnOnlyHandoff : true , synthesizedText : "" } ) ;
1216- params . synthesizedText = undefined ;
1217- params . deliveryPayloads = [ ] ;
1218- params . summary = undefined ;
1219- params . outputText = undefined ;
1373+ {
1374+ name : "active child leaves only the interim parent reply" ,
1375+ activeDescendants : 1 ,
1376+ parentReply : "on it, pulling everything together" ,
1377+ error : "cron child-session handoff timed out before producing a final assistant payload" ,
1378+ } ,
1379+ {
1380+ name : "completed child leaves only the interim parent reply" ,
1381+ activeDescendants : 0 ,
1382+ parentReply : "on it, pulling everything together" ,
1383+ error : "cron child-session handoff completed without a final assistant payload" ,
1384+ } ,
1385+ ] ) (
1386+ "keeps a one-shot child-owned handoff when $name" ,
1387+ async ( { activeDescendants, error, parentReply } ) => {
1388+ vi . mocked ( countActiveDescendantRuns ) . mockReturnValue ( activeDescendants ) ;
1389+ vi . mocked ( isLikelyInterimCronMessage ) . mockImplementation ( ( text ) => text === parentReply ) ;
1390+ const params = makeBaseParams ( {
1391+ spawnOnlyHandoff : true ,
1392+ synthesizedText : parentReply ?? "" ,
1393+ } ) ;
1394+ params . agentSessionKey = "agent:main:cron:test-job" ;
1395+ params . job . deleteAfterRun = true ;
1396+ params . resolvedDelivery = makeResolvedDelivery ( { threadId : "42" } ) ;
1397+ if ( ! parentReply ) {
1398+ params . synthesizedText = undefined ;
1399+ params . deliveryPayloads = [ ] ;
1400+ params . summary = undefined ;
1401+ params . outputText = undefined ;
1402+ }
12201403
1221- const state = await dispatchCronDelivery ( params ) ;
1404+ const state = await dispatchCronDelivery ( params ) ;
12221405
1223- expectResultFields ( state . result , {
1224- status : "error" ,
1225- error,
1226- delivered : false ,
1227- deliveryAttempted : true ,
1228- } ) ;
1229- expect ( deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
1230- } ) ;
1406+ expectResultFields ( state . result , {
1407+ status : "error" ,
1408+ error,
1409+ delivered : false ,
1410+ deliveryAttempted : true ,
1411+ } ) ;
1412+ expect ( state . synthesizedText ) . toBeUndefined ( ) ;
1413+ expect ( state . deliveryPayloads ) . toEqual ( [ ] ) ;
1414+ expect ( deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
1415+ expect ( callGateway ) . not . toHaveBeenCalled ( ) ;
1416+ } ,
1417+ ) ;
12311418
12321419 it ( "preserves abort precedence when an accepted child handoff is interrupted" , async ( ) => {
12331420 const abortReason = "scheduled run aborted while waiting for its child" ;
0 commit comments