@@ -5590,7 +5590,7 @@ describe("dispatchTelegramMessage draft streaming", () => {
55905590 expect ( deliveredTexts ) . not . toContain ( "stale ambient answer" ) ;
55915591 } ) ;
55925592
5593- it ( "lets newer user requests abort active same-session dispatch" , async ( ) => {
5593+ it ( "keeps newer group requests from aborting active same-session dispatch" , async ( ) => {
55945594 const historyKey = "telegram:group:-100123" ;
55955595 const groupHistories = new Map ( [ [ historyKey , [ ] ] ] ) ;
55965596 let firstStarted : ( ( ) => void ) | undefined ;
@@ -5607,18 +5607,19 @@ describe("dispatchTelegramMessage draft streaming", () => {
56075607 } ) ;
56085608 let firstAbortSignal : AbortSignal | undefined ;
56095609 dispatchReplyWithBufferedBlockDispatcher
5610- . mockImplementationOnce ( async ( { replyOptions } ) => {
5610+ . mockImplementationOnce ( async ( { dispatcherOptions , replyOptions } ) => {
56115611 firstAbortSignal = replyOptions ?. abortSignal ;
56125612 firstStarted ?.( ) ;
56135613 await firstGate ;
5614+ await dispatcherOptions . deliver ( { text : "earlier group answer" } , { kind : "final" } ) ;
56145615 return {
5615- queuedFinal : false ,
5616- counts : { block : 0 , final : 0 , tool : 0 } ,
5616+ queuedFinal : true ,
5617+ counts : { block : 0 , final : 1 , tool : 0 } ,
56175618 } ;
56185619 } )
56195620 . mockImplementationOnce ( async ( { dispatcherOptions } ) => {
56205621 secondStarted ?.( ) ;
5621- await dispatcherOptions . deliver ( { text : "fresh request answer" } , { kind : "final" } ) ;
5622+ await dispatcherOptions . deliver ( { text : "fresh group answer" } , { kind : "final" } ) ;
56225623 return {
56235624 queuedFinal : true ,
56245625 counts : { block : 0 , final : 1 , tool : 0 } ,
@@ -5660,7 +5661,7 @@ describe("dispatchTelegramMessage draft streaming", () => {
56605661 } ) ;
56615662 await secondStartGate ;
56625663
5663- expect ( firstAbortSignal ?. aborted ) . toBe ( true ) ;
5664+ expect ( firstAbortSignal ?. aborted ) . toBe ( false ) ;
56645665 releaseFirst ?.( ) ;
56655666 await Promise . all ( [ firstPromise , secondPromise ] ) ;
56665667
@@ -5669,7 +5670,8 @@ describe("dispatchTelegramMessage draft streaming", () => {
56695670 ( reply ) => reply . text ,
56705671 ) ,
56715672 ) ;
5672- expect ( deliveredTexts ) . toContain ( "fresh request answer" ) ;
5673+ expect ( deliveredTexts ) . toContain ( "fresh group answer" ) ;
5674+ expect ( deliveredTexts ) . toContain ( "earlier group answer" ) ;
56735675 } ) ;
56745676
56755677 it ( "keeps newer DM requests from aborting active same-session dispatch" , async ( ) => {
@@ -5910,6 +5912,264 @@ describe("dispatchTelegramMessage draft streaming", () => {
59105912 await sidePromise ;
59115913 } ) ;
59125914
5915+ it ( "releases fence abort authority at turn adoption" , async ( ) => {
5916+ const historyKey = "telegram:group:-100123" ;
5917+ const groupHistories = new Map ( [ [ historyKey , [ ] ] ] ) ;
5918+ let firstStarted : ( ( ) => void ) | undefined ;
5919+ const firstStartGate = new Promise < void > ( ( resolve ) => {
5920+ firstStarted = resolve ;
5921+ } ) ;
5922+ let releaseFirst : ( ( ) => void ) | undefined ;
5923+ const firstGate = new Promise < void > ( ( resolve ) => {
5924+ releaseFirst = resolve ;
5925+ } ) ;
5926+ let firstAbortSignal : AbortSignal | undefined ;
5927+ let adoptTurn : ( ( ) => void | Promise < void > ) | undefined ;
5928+ dispatchReplyWithBufferedBlockDispatcher
5929+ . mockImplementationOnce ( async ( { replyOptions } ) => {
5930+ firstAbortSignal = replyOptions ?. abortSignal ;
5931+ adoptTurn = replyOptions ?. onTurnAdopted ;
5932+ firstStarted ?.( ) ;
5933+ await firstGate ;
5934+ return {
5935+ queuedFinal : false ,
5936+ counts : { block : 0 , final : 0 , tool : 0 } ,
5937+ } ;
5938+ } )
5939+ . mockImplementationOnce ( async ( ) => ( {
5940+ queuedFinal : false ,
5941+ counts : { block : 0 , final : 0 , tool : 0 } ,
5942+ } ) ) ;
5943+
5944+ const createGroupContext = ( messageId : number , body : string ) =>
5945+ createContext ( {
5946+ ctxPayload : {
5947+ SessionKey : "agent:main:telegram:group:-100123" ,
5948+ ChatType : "group" ,
5949+ MessageSid : String ( messageId ) ,
5950+ RawBody : body ,
5951+ BodyForAgent : body ,
5952+ CommandBody : body ,
5953+ CommandAuthorized : true ,
5954+ } as unknown as TelegramMessageContext [ "ctxPayload" ] ,
5955+ msg : {
5956+ chat : { id : - 100123 , type : "supergroup" } ,
5957+ message_id : messageId ,
5958+ text : body ,
5959+ } as unknown as TelegramMessageContext [ "msg" ] ,
5960+ chatId : - 100123 ,
5961+ isGroup : true ,
5962+ historyKey,
5963+ historyLimit : 10 ,
5964+ groupHistories,
5965+ threadSpec : { id : undefined , scope : "none" } ,
5966+ } ) ;
5967+
5968+ const firstPromise = dispatchWithContext ( {
5969+ context : createGroupContext ( 99 , "@bot long turn" ) ,
5970+ streamMode : "off" ,
5971+ } ) ;
5972+ await firstStartGate ;
5973+ expect ( firstAbortSignal ?. aborted ) . toBe ( false ) ;
5974+ expect ( adoptTurn ) . toEqual ( expect . any ( Function ) ) ;
5975+
5976+ // Before adoption, fence supersede still aborts the live controller.
5977+ const { beginTelegramReplyFence, endTelegramReplyFence, supersedeTelegramReplyFence } =
5978+ await import ( "./telegram-reply-fence.js" ) ;
5979+ const preAdoptController = new AbortController ( ) ;
5980+ beginTelegramReplyFence ( {
5981+ key : "agent:main:telegram:group:pre-adopt" ,
5982+ supersede : false ,
5983+ abortController : preAdoptController ,
5984+ } ) ;
5985+ expect ( supersedeTelegramReplyFence ( "agent:main:telegram:group:pre-adopt" ) ) . toBe ( true ) ;
5986+ expect ( preAdoptController . signal . aborted ) . toBe ( true ) ;
5987+ endTelegramReplyFence ( "agent:main:telegram:group:pre-adopt" ) ;
5988+
5989+ // After adoption, the dispatch controller is released from the fence set so
5990+ // a later superseding peer (authorized explicit command) cannot abort it.
5991+ await adoptTurn ?.( ) ;
5992+ expect ( firstAbortSignal ?. aborted ) . toBe ( false ) ;
5993+
5994+ await dispatchWithContext ( {
5995+ context : createGroupContext ( 100 , "/export-trajectory bundle" ) ,
5996+ streamMode : "off" ,
5997+ } ) ;
5998+ expect ( firstAbortSignal ?. aborted ) . toBe ( false ) ;
5999+ releaseFirst ?.( ) ;
6000+ await firstPromise ;
6001+ } ) ;
6002+
6003+ it ( "lets authorized /stop kill an adopted run without the released fence controller" , async ( ) => {
6004+ const historyKey = "telegram:group:-100123" ;
6005+ const groupHistories = new Map ( [ [ historyKey , [ ] ] ] ) ;
6006+ // Core owns post-adoption abort via reply-run registry / handleStopCommand.
6007+ // Pin: after fence release, a core-owned abort still ends the run while the
6008+ // fence controller stays non-aborted.
6009+ const coreRunController = new AbortController ( ) ;
6010+ let firstStarted : ( ( ) => void ) | undefined ;
6011+ const firstStartGate = new Promise < void > ( ( resolve ) => {
6012+ firstStarted = resolve ;
6013+ } ) ;
6014+ let firstAbortSignal : AbortSignal | undefined ;
6015+ let adoptTurn : ( ( ) => void | Promise < void > ) | undefined ;
6016+ let runSettled = false ;
6017+ dispatchReplyWithBufferedBlockDispatcher
6018+ . mockImplementationOnce ( async ( { replyOptions } ) => {
6019+ firstAbortSignal = replyOptions ?. abortSignal ;
6020+ adoptTurn = replyOptions ?. onTurnAdopted ;
6021+ firstStarted ?.( ) ;
6022+ await new Promise < void > ( ( resolve ) => {
6023+ const finish = ( ) => {
6024+ if ( runSettled ) {
6025+ return ;
6026+ }
6027+ runSettled = true ;
6028+ resolve ( ) ;
6029+ } ;
6030+ firstAbortSignal ?. addEventListener ( "abort" , finish , { once : true } ) ;
6031+ coreRunController . signal . addEventListener ( "abort" , finish , { once : true } ) ;
6032+ } ) ;
6033+ return {
6034+ queuedFinal : false ,
6035+ counts : { block : 0 , final : 0 , tool : 0 } ,
6036+ } ;
6037+ } )
6038+ . mockImplementationOnce ( async ( ) => {
6039+ // Simulate core handleStopCommand / abortReplyRunBySessionId effect on the
6040+ // adopted registry-owned run (independent of the released fence controller).
6041+ coreRunController . abort ( ) ;
6042+ return {
6043+ queuedFinal : false ,
6044+ counts : { block : 0 , final : 0 , tool : 0 } ,
6045+ } ;
6046+ } ) ;
6047+
6048+ const createGroupContext = ( messageId : number , body : string ) =>
6049+ createContext ( {
6050+ ctxPayload : {
6051+ SessionKey : "agent:main:telegram:group:-100123" ,
6052+ ChatType : "group" ,
6053+ MessageSid : String ( messageId ) ,
6054+ RawBody : body ,
6055+ BodyForAgent : body ,
6056+ CommandBody : body ,
6057+ CommandAuthorized : true ,
6058+ } as unknown as TelegramMessageContext [ "ctxPayload" ] ,
6059+ msg : {
6060+ chat : { id : - 100123 , type : "supergroup" } ,
6061+ message_id : messageId ,
6062+ text : body ,
6063+ } as unknown as TelegramMessageContext [ "msg" ] ,
6064+ chatId : - 100123 ,
6065+ isGroup : true ,
6066+ historyKey,
6067+ historyLimit : 10 ,
6068+ groupHistories,
6069+ threadSpec : { id : undefined , scope : "none" } ,
6070+ } ) ;
6071+
6072+ const firstPromise = dispatchWithContext ( {
6073+ context : createGroupContext ( 99 , "@bot long adopted turn" ) ,
6074+ streamMode : "off" ,
6075+ } ) ;
6076+ await firstStartGate ;
6077+ await adoptTurn ?.( ) ;
6078+ expect ( firstAbortSignal ?. aborted ) . toBe ( false ) ;
6079+ expect ( coreRunController . signal . aborted ) . toBe ( false ) ;
6080+
6081+ await dispatchWithContext ( {
6082+ context : createGroupContext ( 100 , "/stop" ) ,
6083+ streamMode : "off" ,
6084+ } ) ;
6085+
6086+ await firstPromise ;
6087+ expect ( firstAbortSignal ?. aborted ) . toBe ( false ) ;
6088+ expect ( coreRunController . signal . aborted ) . toBe ( true ) ;
6089+ } ) ;
6090+
6091+ it ( "keeps overlapping group deliveries non-superseded" , async ( ) => {
6092+ const historyKey = "telegram:group:-100123" ;
6093+ const groupHistories = new Map ( [ [ historyKey , [ ] ] ] ) ;
6094+ let firstStarted : ( ( ) => void ) | undefined ;
6095+ const firstStartGate = new Promise < void > ( ( resolve ) => {
6096+ firstStarted = resolve ;
6097+ } ) ;
6098+ let releaseFirst : ( ( ) => void ) | undefined ;
6099+ const firstGate = new Promise < void > ( ( resolve ) => {
6100+ releaseFirst = resolve ;
6101+ } ) ;
6102+ let secondStarted : ( ( ) => void ) | undefined ;
6103+ const secondStartGate = new Promise < void > ( ( resolve ) => {
6104+ secondStarted = resolve ;
6105+ } ) ;
6106+ dispatchReplyWithBufferedBlockDispatcher
6107+ . mockImplementationOnce ( async ( { dispatcherOptions, replyOptions } ) => {
6108+ await replyOptions ?. onTurnAdopted ?.( ) ;
6109+ firstStarted ?.( ) ;
6110+ await firstGate ;
6111+ await dispatcherOptions . deliver ( { text : "earlier group answer" } , { kind : "final" } ) ;
6112+ return {
6113+ queuedFinal : true ,
6114+ counts : { block : 0 , final : 1 , tool : 0 } ,
6115+ } ;
6116+ } )
6117+ . mockImplementationOnce ( async ( { dispatcherOptions, replyOptions } ) => {
6118+ await replyOptions ?. onTurnAdopted ?.( ) ;
6119+ secondStarted ?.( ) ;
6120+ await dispatcherOptions . deliver ( { text : "fresh group answer" } , { kind : "final" } ) ;
6121+ return {
6122+ queuedFinal : true ,
6123+ counts : { block : 0 , final : 1 , tool : 0 } ,
6124+ } ;
6125+ } ) ;
6126+ deliverReplies . mockResolvedValue ( { delivered : true } ) ;
6127+
6128+ const createGroupContext = ( messageId : number , body : string ) =>
6129+ createContext ( {
6130+ ctxPayload : {
6131+ SessionKey : "agent:main:telegram:group:-100123" ,
6132+ ChatType : "group" ,
6133+ MessageSid : String ( messageId ) ,
6134+ RawBody : body ,
6135+ BodyForAgent : body ,
6136+ CommandBody : body ,
6137+ CommandAuthorized : true ,
6138+ } as unknown as TelegramMessageContext [ "ctxPayload" ] ,
6139+ msg : {
6140+ chat : { id : - 100123 , type : "supergroup" } ,
6141+ message_id : messageId ,
6142+ } as unknown as TelegramMessageContext [ "msg" ] ,
6143+ chatId : - 100123 ,
6144+ isGroup : true ,
6145+ historyKey,
6146+ historyLimit : 10 ,
6147+ groupHistories,
6148+ threadSpec : { id : undefined , scope : "none" } ,
6149+ } ) ;
6150+
6151+ const firstPromise = dispatchWithContext ( {
6152+ context : createGroupContext ( 99 , "@bot first request" ) ,
6153+ streamMode : "off" ,
6154+ } ) ;
6155+ await firstStartGate ;
6156+ const secondPromise = dispatchWithContext ( {
6157+ context : createGroupContext ( 100 , "@bot second request" ) ,
6158+ streamMode : "off" ,
6159+ } ) ;
6160+ await secondStartGate ;
6161+ releaseFirst ?.( ) ;
6162+ await Promise . all ( [ firstPromise , secondPromise ] ) ;
6163+
6164+ const deliveredTexts = deliverReplies . mock . calls . flatMap ( ( call ) =>
6165+ ( ( call [ 0 ] as { replies ?: Array < { text ?: string } > } ) . replies ?? [ ] ) . map (
6166+ ( reply ) => reply . text ,
6167+ ) ,
6168+ ) ;
6169+ expect ( deliveredTexts ) . toContain ( "earlier group answer" ) ;
6170+ expect ( deliveredTexts ) . toContain ( "fresh group answer" ) ;
6171+ } ) ;
6172+
59136173 it ( "does not drop the first chunk of a long final after a generic lane rotation" , async ( ) => {
59146174 const { answerDraftStream } = setupDraftStreams ( { answerMessageId : 2001 } ) ;
59156175 dispatchReplyWithBufferedBlockDispatcher . mockImplementation (
0 commit comments