@@ -619,7 +619,7 @@ describe("scheduleRestartSentinelWake", () => {
619619 expect ( mocks . requestHeartbeat ) . not . toHaveBeenCalled ( ) ;
620620 } ) ;
621621
622- it ( "does not dispatch agentTurn continuation into a terminal session entry" , async ( ) => {
622+ it ( "dispatches agentTurn continuation for a completed run entry" , async ( ) => {
623623 mocks . readRestartSentinel . mockResolvedValue ( {
624624 payload : {
625625 sessionKey : "agent:main:main" ,
@@ -652,44 +652,47 @@ describe("scheduleRestartSentinelWake", () => {
652652
653653 await scheduleRestartSentinelWake ( { deps : { } as never } ) ;
654654
655- expect ( mocks . enqueueSessionDelivery ) . not . toHaveBeenCalled ( ) ;
656- expect ( mocks . recordInboundSessionAndDispatchReply ) . not . toHaveBeenCalled ( ) ;
657- expect ( mocks . enqueueSystemEvent ) . toHaveBeenCalledWith ( "restart message" , {
658- sessionKey : "agent:main:main" ,
659- deliveryContext : {
660- channel : "whatsapp" ,
661- to : "+15550002" ,
662- accountId : "acct-2" ,
663- threadId : "thread-42" ,
664- } ,
665- } ) ;
666- expect ( mocks . logWarn ) . toHaveBeenCalledWith (
667- "restart summary: continuation skipped: session is terminal" ,
668- {
655+ expect ( mocks . enqueueSessionDelivery ) . toHaveBeenCalledWith (
656+ expect . objectContaining ( {
657+ kind : "agentTurn" ,
669658 sessionKey : "agent:main:main" ,
670- continuationKind : "agentTurn" ,
671- status : "done" ,
672- endedAt : expect . any ( Number ) ,
673- } ,
659+ message : "continue after restart" ,
660+ messageId : "restart-sentinel:agent:main:main:agentTurn:123" ,
661+ expectedSessionId : "agent:main:main" ,
662+ route : {
663+ channel : "whatsapp" ,
664+ to : "+15550002" ,
665+ accountId : "acct-2" ,
666+ threadId : "thread-42" ,
667+ chatType : "direct" ,
668+ } ,
669+ } ) ,
670+ ) ;
671+ expect ( mocks . recordInboundSessionAndDispatchReply ) . toHaveBeenCalledTimes ( 1 ) ;
672+ expectContinuationDispatchFields (
673+ { routeSessionKey : "agent:main:main" } ,
674+ { Body : "continue after restart" } ,
674675 ) ;
676+ expect ( mocks . enqueueSystemEvent ) . not . toHaveBeenCalled ( ) ;
677+ expect ( mocks . logWarn ) . not . toHaveBeenCalled ( ) ;
675678 } ) ;
676679
677- it ( "does not dispatch a queued agentTurn continuation after the session becomes terminal " , async ( ) => {
680+ it ( "does not dispatch a queued agentTurn continuation after the session key changes " , async ( ) => {
678681 const activeEntry : LoadedSessionEntry = {
679682 cfg : { } ,
680683 entry : {
681- sessionId : "agent:main:main " ,
684+ sessionId : "old-session-id " ,
682685 updatedAt : Date . now ( ) ,
683686 } ,
684687 store : { } ,
685688 storePath : "/tmp/sessions.json" ,
686689 canonicalKey : "agent:main:main" ,
687690 legacyKey : undefined ,
688691 } ;
689- const terminalEntry : LoadedSessionEntry = {
692+ const replacementEntry : LoadedSessionEntry = {
690693 cfg : { } ,
691694 entry : {
692- sessionId : "agent:main:main " ,
695+ sessionId : "new-session-id " ,
693696 updatedAt : Date . now ( ) ,
694697 status : "done" ,
695698 endedAt : Date . now ( ) - 1_000 ,
@@ -715,7 +718,7 @@ describe("scheduleRestartSentinelWake", () => {
715718 } ,
716719 } ,
717720 } as Awaited < ReturnType < typeof mocks . readRestartSentinel > > ) ;
718- mocks . loadSessionEntry . mockReturnValueOnce ( activeEntry ) . mockReturnValue ( terminalEntry ) ;
721+ mocks . loadSessionEntry . mockReturnValueOnce ( activeEntry ) . mockReturnValue ( replacementEntry ) ;
719722
720723 await scheduleRestartSentinelWake ( { deps : { } as never } ) ;
721724
@@ -736,18 +739,15 @@ describe("scheduleRestartSentinelWake", () => {
736739 reason : "wake" ,
737740 sessionKey : "agent:main:main" ,
738741 } ) ;
739- expect ( mocks . logWarn ) . toHaveBeenCalledWith (
740- "restart continuation skipped: session is terminal" ,
741- {
742- sessionKey : "agent:main:main" ,
743- queueId : "session-delivery-1" ,
744- status : "done" ,
745- endedAt : expect . any ( Number ) ,
746- } ,
747- ) ;
742+ expect ( mocks . logWarn ) . toHaveBeenCalledWith ( "restart continuation skipped: session changed" , {
743+ sessionKey : "agent:main:main" ,
744+ queueId : "session-delivery-1" ,
745+ expectedSessionId : "old-session-id" ,
746+ actualSessionId : "new-session-id" ,
747+ } ) ;
748748 } ) ;
749749
750- it ( "still delivers systemEvent continuations for terminal session entries" , async ( ) => {
750+ it ( "still delivers systemEvent continuations for completed run entries" , async ( ) => {
751751 mocks . readRestartSentinel . mockResolvedValue ( {
752752 payload : {
753753 sessionKey : "agent:main:main" ,
@@ -791,7 +791,7 @@ describe("scheduleRestartSentinelWake", () => {
791791 } ) ;
792792 expect ( mocks . recordInboundSessionAndDispatchReply ) . not . toHaveBeenCalled ( ) ;
793793 expect ( mocks . logWarn ) . not . toHaveBeenCalledWith (
794- "restart summary: continuation skipped: session is terminal " ,
794+ "restart continuation skipped: session changed " ,
795795 expect . anything ( ) ,
796796 ) ;
797797 } ) ;
0 commit comments