@@ -788,6 +788,73 @@ describe("tryDispatchAcpReply", () => {
788788 clearAgentRunContext ( runId , replacementGeneration ) ;
789789 } ) ;
790790
791+ it ( "captures caller-owned ACP lifecycle before lazy dispatch setup yields" , async ( ) => {
792+ setReadyAcpResolution ( ) ;
793+ const runId = "caller-reused-during-setup" ;
794+ const admittedGeneration = getAgentEventLifecycleGeneration ( ) ;
795+ claimAgentRunContext ( runId , {
796+ agentId : "caller" ,
797+ lifecycleGeneration : admittedGeneration ,
798+ sessionKey,
799+ } ) ;
800+ const admittedToken = getAgentRunContextLifecycleToken ( runId , admittedGeneration ) ;
801+ let startGeneration : string | undefined ;
802+ let startToken : object | undefined ;
803+ let callbackGeneration : string | undefined ;
804+ let callbackToken : object | undefined ;
805+ let terminalGeneration : string | undefined ;
806+ let terminalToken : object | undefined ;
807+
808+ auditMocks . emitAcpLifecycleStart . mockImplementationOnce (
809+ ( params : { lifecycleGeneration ?: string } ) => {
810+ startGeneration = params . lifecycleGeneration ;
811+ startToken = getAgentRunExecutionContextLifecycleToken ( runId ) ;
812+ } ,
813+ ) ;
814+ auditMocks . emitAcpRuntimeEvent . mockImplementationOnce ( ( ) => {
815+ callbackGeneration = getAgentRunExecutionLifecycleGeneration ( ) ;
816+ callbackToken = getAgentRunExecutionContextLifecycleToken ( runId ) ;
817+ } ) ;
818+ auditMocks . emitAcpLifecycleEnd . mockImplementationOnce ( ( ) => {
819+ terminalGeneration = getAgentRunExecutionLifecycleGeneration ( ) ;
820+ terminalToken = getAgentRunExecutionContextLifecycleToken ( runId ) ;
821+ } ) ;
822+ managerMocks . runTurn . mockImplementationOnce (
823+ async ( { onEvent } : { onEvent ?: ( event : unknown ) => Promise < void > } ) => {
824+ if ( onEvent ) {
825+ await emitToolLifecycleEvents ( onEvent , "tool-after-setup-reuse" ) ;
826+ }
827+ } ,
828+ ) ;
829+
830+ const dispatch = runDispatch ( { bodyForAgent : "audit setup race" , runId } ) ;
831+ const replacementGeneration = rotateAgentEventLifecycleGeneration ( ) ;
832+ claimAgentRunContext ( runId , {
833+ agentId : "replacement" ,
834+ lifecycleGeneration : replacementGeneration ,
835+ sessionKey : "agent:replacement:main" ,
836+ } ) ;
837+ const replacementToken = getAgentRunContextLifecycleToken ( runId , replacementGeneration ) ;
838+
839+ await dispatch ;
840+
841+ expect ( admittedToken ) . toBeTypeOf ( "object" ) ;
842+ expect ( replacementToken ) . toBeTypeOf ( "object" ) ;
843+ expect ( replacementToken ) . not . toBe ( admittedToken ) ;
844+ expect ( startGeneration ) . toBe ( admittedGeneration ) ;
845+ expect ( startToken ) . toBe ( admittedToken ) ;
846+ expect ( callbackGeneration ) . toBe ( admittedGeneration ) ;
847+ expect ( callbackToken ) . toBe ( admittedToken ) ;
848+ expect ( terminalGeneration ) . toBe ( admittedGeneration ) ;
849+ expect ( terminalToken ) . toBe ( admittedToken ) ;
850+ expect ( getAgentRunContext ( runId ) ) . toMatchObject ( {
851+ agentId : "replacement" ,
852+ lifecycleGeneration : replacementGeneration ,
853+ sessionKey : "agent:replacement:main" ,
854+ } ) ;
855+ clearAgentRunContext ( runId , replacementGeneration ) ;
856+ } ) ;
857+
791858 it ( "keeps audit run ids unique when channel message ids repeat" , async ( ) => {
792859 setReadyAcpResolution ( ) ;
793860
0 commit comments