@@ -33,6 +33,7 @@ import {
3333 createTestRegistry ,
3434} from "../../test-utils/channel-plugins.js" ;
3535import { createInternalHookEventPayload } from "../../test-utils/internal-hook-event-payload.js" ;
36+ import { settleReplyDispatcher } from "../dispatch-dispatcher.js" ;
3637import { getReplyPayloadMetadata } from "../reply-payload.js" ;
3738import type { MsgContext } from "../templating.js" ;
3839import { setReplyPayloadMetadata , type GetReplyOptions , type ReplyPayload } from "../types.js" ;
@@ -1354,6 +1355,7 @@ describe("dispatchReplyFromConfig", () => {
13541355 replyOptions : { runId : "slack-run-1" } ,
13551356 replyResolver : async ( ) => ( { text : "Slack command reply" } ) ,
13561357 } ) ;
1358+ await settleReplyDispatcher ( { dispatcher } ) ;
13571359
13581360 expect ( result . queuedFinal ) . toBe ( true ) ;
13591361 expect ( mocks . routeReply ) . not . toHaveBeenCalled ( ) ;
@@ -1415,6 +1417,7 @@ describe("dispatchReplyFromConfig", () => {
14151417 return { text : "✅ New session started." } ;
14161418 } ,
14171419 } ) ;
1420+ await settleReplyDispatcher ( { dispatcher } ) ;
14181421
14191422 expect ( transcriptMocks . appendAssistantMessageToSessionTranscript ) . toHaveBeenCalledWith (
14201423 expect . objectContaining ( {
@@ -1447,6 +1450,7 @@ describe("dispatchReplyFromConfig", () => {
14471450 replyResolver : async ( ) =>
14481451 setReplyPayloadMetadata ( { text : "Persisted runtime reply" } , metadata ) ,
14491452 } ) ;
1453+ await settleReplyDispatcher ( { dispatcher } ) ;
14501454
14511455 expect ( result . queuedFinal ) . toBe ( true ) ;
14521456 expect ( transcriptMocks . appendAssistantMessageToSessionTranscript ) . not . toHaveBeenCalled ( ) ;
@@ -1572,6 +1576,7 @@ describe("dispatchReplyFromConfig", () => {
15721576 dispatcher,
15731577 replyResolver : async ( ) => ( { text : "Secret Slack reply" } ) ,
15741578 } ) ;
1579+ await settleReplyDispatcher ( { dispatcher } ) ;
15751580
15761581 expect ( transcriptMocks . appendAssistantMessageToSessionTranscript ) . toHaveBeenCalledWith (
15771582 expect . objectContaining ( {
@@ -1603,6 +1608,7 @@ describe("dispatchReplyFromConfig", () => {
16031608 dispatcher,
16041609 replyResolver : async ( ) => ( { text : "Hidden Slack reply" } ) ,
16051610 } ) ;
1611+ await settleReplyDispatcher ( { dispatcher } ) ;
16061612
16071613 expect ( transcriptMocks . appendAssistantMessageToSessionTranscript ) . not . toHaveBeenCalled ( ) ;
16081614 } ) ;
@@ -10543,6 +10549,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
1054310549 sourceReplyDeliveryMode : "message_tool_only" ,
1054410550 } ,
1054510551 } ) ;
10552+ await settleReplyDispatcher ( { dispatcher } ) ;
1054610553
1054710554 expect ( result . queuedFinal ) . toBe ( true ) ;
1054810555 expect ( dispatcher . sendFinalReply ) . toHaveBeenCalledWith ( sourceReply ) ;
@@ -10607,6 +10614,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
1060710614 sourceReplyDeliveryMode : "message_tool_only" ,
1060810615 } ,
1060910616 } ) ;
10617+ await settleReplyDispatcher ( { dispatcher } ) ;
1061010618
1061110619 expect ( result . queuedFinal ) . toBe ( true ) ;
1061210620 expect ( dispatcher . sendFinalReply ) . toHaveBeenCalledWith ( sourceReply ) ;
@@ -10624,6 +10632,84 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
1062410632 } ) ;
1062510633 } ) ;
1062610634
10635+ it ( "lets a queued same-session turn finish before mirroring delivered source replies" , async ( ) => {
10636+ setNoAbort ( ) ;
10637+ sessionStoreMocks . currentEntry = {
10638+ sessionId : "s1" ,
10639+ sessionKey : "agent:main" ,
10640+ updatedAt : 0 ,
10641+ sendPolicy : "allow" ,
10642+ } ;
10643+ const order : string [ ] = [ ] ;
10644+ let followupDispatch : Promise < unknown > | undefined ;
10645+ const firstDispatcher = createReplyDispatcher ( {
10646+ deliver : async ( ) => {
10647+ if ( ! followupDispatch ) {
10648+ throw new Error ( "expected queued follow-up dispatch" ) ;
10649+ }
10650+ await followupDispatch ;
10651+ order . push ( "first-delivery" ) ;
10652+ } ,
10653+ } ) ;
10654+ const firstReply = setReplyPayloadMetadata (
10655+ { text : "first reply" } ,
10656+ {
10657+ deliverDespiteSourceReplySuppression : true ,
10658+ sourceReplyTranscriptMirror : {
10659+ sessionKey : "agent:main" ,
10660+ agentId : "main" ,
10661+ text : "first reply" ,
10662+ idempotencyKey : "run-1:internal-source-reply:queued" ,
10663+ } ,
10664+ } ,
10665+ ) ;
10666+ transcriptMocks . appendAssistantMessageToSessionTranscript . mockImplementationOnce ( async ( ) => {
10667+ order . push ( "mirror" ) ;
10668+ return { ok : true , sessionFile : "/tmp/session.jsonl" , messageId : "message-1" } ;
10669+ } ) ;
10670+
10671+ const firstResult = await dispatchReplyFromConfig ( {
10672+ ctx : buildTestCtx ( {
10673+ MessageSid : "first-message" ,
10674+ Provider : "webchat" ,
10675+ Surface : "webchat" ,
10676+ SessionKey : "agent:main" ,
10677+ } ) ,
10678+ cfg : emptyConfig ,
10679+ dispatcher : firstDispatcher ,
10680+ replyOptions : { sourceReplyDeliveryMode : "message_tool_only" } ,
10681+ replyResolver : async ( ) => {
10682+ followupDispatch = dispatchReplyFromConfig ( {
10683+ ctx : buildTestCtx ( {
10684+ Body : "follow up" ,
10685+ MessageSid : "followup-message" ,
10686+ Provider : "webchat" ,
10687+ Surface : "webchat" ,
10688+ SessionKey : "agent:main" ,
10689+ } ) ,
10690+ cfg : emptyConfig ,
10691+ dispatcher : createDispatcher ( ) ,
10692+ replyResolver : async ( ) => {
10693+ order . push ( "followup" ) ;
10694+ return { text : "second reply" } ;
10695+ } ,
10696+ } ) ;
10697+ await Promise . resolve ( ) ;
10698+ return firstReply ;
10699+ } ,
10700+ } ) ;
10701+
10702+ expect ( firstResult . queuedFinal ) . toBe ( true ) ;
10703+ await settleReplyDispatcher ( { dispatcher : firstDispatcher } ) ;
10704+ await followupDispatch ;
10705+
10706+ expect ( order ) . toEqual ( [ "followup" , "first-delivery" , "mirror" ] ) ;
10707+ const queuedMirrorCalls = transcriptMocks . appendAssistantMessageToSessionTranscript . mock . calls
10708+ . map ( ( [ params ] ) => params as { idempotencyKey ?: string } )
10709+ . filter ( ( params ) => params . idempotencyKey === "run-1:internal-source-reply:queued" ) ;
10710+ expect ( queuedMirrorCalls ) . toHaveLength ( 1 ) ;
10711+ } ) ;
10712+
1062710713 it ( "does not mirror internal source replies cancelled by dispatcher hooks" , async ( ) => {
1062810714 setNoAbort ( ) ;
1062910715 sessionStoreMocks . currentEntry = {
@@ -10661,6 +10747,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
1066110747 sourceReplyDeliveryMode : "message_tool_only" ,
1066210748 } ,
1066310749 } ) ;
10750+ await settleReplyDispatcher ( { dispatcher } ) ;
1066410751
1066510752 expect ( result . queuedFinal ) . toBe ( true ) ;
1066610753 expect ( dispatcher . sendFinalReply ) . toHaveBeenCalledWith ( sourceReply ) ;
0 commit comments