@@ -523,6 +523,185 @@ describe("cron service timer seam coverage", () => {
523523 } ) ;
524524 } ) ;
525525
526+ it . each ( [
527+ {
528+ name : "main notification and immediate wake use the session owner and thread" ,
529+ sessionTarget : "main" ,
530+ sessionKey : "agent:ops:telegram:group:42:topic:77" ,
531+ defaultAgentId : "main" ,
532+ notify : "queue changed" ,
533+ wake : "now" ,
534+ expectedAgentId : "ops" ,
535+ expectedIntent : "immediate" ,
536+ expectDeliveryContext : true ,
537+ } ,
538+ {
539+ name : "main notification and deferred wake use the current configured owner" ,
540+ sessionTarget : "main" ,
541+ defaultAgentId : "stale-main" ,
542+ currentDefaultAgentId : "ops" ,
543+ notify : "queue changed" ,
544+ wake : "next-heartbeat" ,
545+ expectedAgentId : "ops" ,
546+ expectedIntent : "event" ,
547+ } ,
548+ {
549+ name : "isolated script wake uses the session owner without main delivery context" ,
550+ sessionTarget : "isolated" ,
551+ sessionKey : "agent:ops:telegram:group:42:topic:77" ,
552+ defaultAgentId : "main" ,
553+ notify : "queue changed" ,
554+ wake : "now" ,
555+ expectedAgentId : "ops" ,
556+ expectedIntent : "immediate" ,
557+ } ,
558+ {
559+ name : "explicit script owner wins over the current configured default" ,
560+ sessionTarget : "main" ,
561+ agentId : "ops" ,
562+ sessionKey : "agent:ops:telegram:group:42:topic:77" ,
563+ defaultAgentId : "main" ,
564+ currentDefaultAgentId : "other" ,
565+ notify : "queue changed" ,
566+ wake : "next-heartbeat" ,
567+ expectedAgentId : "ops" ,
568+ expectedIntent : "event" ,
569+ expectDeliveryContext : true ,
570+ } ,
571+ {
572+ name : "main wake-only completion keeps its session owner and thread" ,
573+ sessionTarget : "main" ,
574+ sessionKey : "agent:ops:telegram:group:42:topic:77" ,
575+ defaultAgentId : "main" ,
576+ wake : "now" ,
577+ expectedAgentId : "ops" ,
578+ expectedIntent : "immediate" ,
579+ expectDeliveryContext : true ,
580+ } ,
581+ {
582+ name : "main notification without a wake keeps its session owner and thread" ,
583+ sessionTarget : "main" ,
584+ sessionKey : "agent:ops:telegram:group:42:topic:77" ,
585+ defaultAgentId : "main" ,
586+ notify : "queue changed" ,
587+ expectedAgentId : "ops" ,
588+ expectDeliveryContext : true ,
589+ } ,
590+ ] as const ) ( "routes script side effects: $name" , async ( testCase ) => {
591+ const { storePath } = await makeStorePath ( ) ;
592+ const now = Date . parse ( "2026-08-24T12:00:00.000Z" ) ;
593+ const enqueueSystemEvent = vi . fn ( ) ;
594+ const requestHeartbeat = vi . fn ( ) ;
595+ const sessionKey = "sessionKey" in testCase ? testCase . sessionKey : undefined ;
596+ const explicitAgentId = "agentId" in testCase ? testCase . agentId : undefined ;
597+ const currentDefaultAgentId =
598+ "currentDefaultAgentId" in testCase ? testCase . currentDefaultAgentId : undefined ;
599+ const notify = "notify" in testCase ? testCase . notify : undefined ;
600+ const wake = "wake" in testCase ? testCase . wake : undefined ;
601+ const job = {
602+ ...createDueScriptJob ( { now, sessionTarget : testCase . sessionTarget } ) ,
603+ agentId : explicitAgentId ,
604+ ...( sessionKey ? { sessionKey } : { } ) ,
605+ } ;
606+ const sessionStorePath = path . join ( path . dirname ( path . dirname ( storePath ) ) , "sessions.json" ) ;
607+ const deliveryContext = {
608+ channel : "telegram" ,
609+ to : "telegram:42" ,
610+ accountId : "ops-bot" ,
611+ threadId : 77 ,
612+ } ;
613+ if ( sessionKey ) {
614+ await upsertSessionEntryCore (
615+ { storePath : sessionStorePath , sessionKey } ,
616+ {
617+ sessionId : "ops-telegram-session" ,
618+ updatedAt : now ,
619+ delivery : normalizeSessionDeliveryState ( { context : deliveryContext } ) ,
620+ } ,
621+ ) ;
622+ }
623+ const state = createCronServiceState ( {
624+ storePath,
625+ cronEnabled : true ,
626+ cronConfig : { triggers : { enabled : true } } ,
627+ log : logger ,
628+ nowMs : ( ) => now ,
629+ defaultAgentId : testCase . defaultAgentId ,
630+ ...( currentDefaultAgentId ? { resolveDefaultAgentId : ( ) => currentDefaultAgentId } : { } ) ,
631+ resolveSessionStorePath : ( ) => sessionStorePath ,
632+ enqueueSystemEvent,
633+ requestHeartbeat,
634+ runIsolatedAgentJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
635+ runScriptJob : vi . fn ( async ( ) => ( {
636+ status : "ok" as const ,
637+ ...( notify ? { notify } : { } ) ,
638+ ...( wake ? { wake } : { } ) ,
639+ } ) ) ,
640+ } ) ;
641+
642+ await expect ( executeJobCore ( state , job ) ) . resolves . toMatchObject ( { status : "ok" } ) ;
643+
644+ expect ( enqueueSystemEvent ) . toHaveBeenCalledOnce ( ) ;
645+ const [ eventText , eventOptions ] = enqueueSystemEvent . mock . calls [ 0 ] as [
646+ string ,
647+ {
648+ agentId ?: string ;
649+ contextKey ?: string ;
650+ deliveryContext ?: typeof deliveryContext ;
651+ } ,
652+ ] ;
653+ expect ( eventText ) . toBe ( notify ?? "script job script job completed" ) ;
654+ expect ( eventOptions . agentId ) . toBe ( testCase . expectedAgentId ) ;
655+ if ( "expectDeliveryContext" in testCase && testCase . expectDeliveryContext ) {
656+ expect ( eventOptions . deliveryContext ) . toEqual ( deliveryContext ) ;
657+ } else {
658+ expect ( eventOptions ) . not . toHaveProperty ( "deliveryContext" ) ;
659+ }
660+ if ( "expectedIntent" in testCase ) {
661+ expect ( requestHeartbeat ) . toHaveBeenCalledExactlyOnceWith ( {
662+ source : "cron" ,
663+ intent : testCase . expectedIntent ,
664+ reason : "cron:script-job:script" ,
665+ agentId : testCase . expectedAgentId ,
666+ } ) ;
667+ } else {
668+ expect ( requestHeartbeat ) . not . toHaveBeenCalled ( ) ;
669+ }
670+ } ) ;
671+
672+ it . each ( [ "main" , "isolated" ] as const ) (
673+ "does not resolve an owner for a quiet %s script without side effects" ,
674+ async ( sessionTarget ) => {
675+ const { storePath } = await makeStorePath ( ) ;
676+ const now = Date . parse ( "2026-08-24T12:00:00.000Z" ) ;
677+ const enqueueSystemEvent = vi . fn ( ) ;
678+ const requestHeartbeat = vi . fn ( ) ;
679+ const resolveDefaultAgentId = vi . fn ( ( ) => undefined ) ;
680+ const job = {
681+ ...createDueScriptJob ( { now, sessionTarget } ) ,
682+ agentId : undefined ,
683+ } ;
684+ const state = createCronServiceState ( {
685+ storePath,
686+ cronEnabled : true ,
687+ cronConfig : { triggers : { enabled : true } } ,
688+ log : logger ,
689+ nowMs : ( ) => now ,
690+ defaultAgentId : undefined ,
691+ resolveDefaultAgentId,
692+ enqueueSystemEvent,
693+ requestHeartbeat,
694+ runIsolatedAgentJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
695+ runScriptJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
696+ } ) ;
697+
698+ await expect ( executeJobCore ( state , job ) ) . resolves . toMatchObject ( { status : "ok" } ) ;
699+ expect ( resolveDefaultAgentId ) . not . toHaveBeenCalled ( ) ;
700+ expect ( enqueueSystemEvent ) . not . toHaveBeenCalled ( ) ;
701+ expect ( requestHeartbeat ) . not . toHaveBeenCalled ( ) ;
702+ } ,
703+ ) ;
704+
526705 it ( "delivers nothing and enqueues nothing when notify and wake are absent" , async ( ) => {
527706 const { storePath } = await makeStorePath ( ) ;
528707 const now = Date . parse ( "2026-07-18T12:00:00.000Z" ) ;
0 commit comments