@@ -3,6 +3,10 @@ import {
33 configureExecutionIdentityAdmissionSink ,
44 type ExecutionIdentityAdmissionWork ,
55} from "../../audit/execution-identity-admission.js" ;
6+ import {
7+ getAgentRunLifecycleGeneration ,
8+ resetAgentRunRegistryForTest ,
9+ } from "../../infra/agent-run-registry.js" ;
610import { admitAutoReplyExecutionAttribution } from "./agent-runner-execution-identity.js" ;
711
812describe ( "admitAutoReplyExecutionAttribution" , ( ) => {
@@ -11,6 +15,7 @@ describe("admitAutoReplyExecutionAttribution", () => {
1115 afterEach ( ( ) => {
1216 restoreSink ?.( ) ;
1317 restoreSink = undefined ;
18+ resetAgentRunRegistryForTest ( ) ;
1419 } ) ;
1520
1621 it ( "records exact channel and requester evidence with the runtime correlation" , ( ) => {
@@ -121,6 +126,41 @@ describe("admitAutoReplyExecutionAttribution", () => {
121126 expect ( sink ) . not . toHaveBeenCalled ( ) ;
122127 } ) ;
123128
129+ it ( "captures only the winning cross-session attribution for a shared run id" , ( ) => {
130+ const work : ExecutionIdentityAdmissionWork [ ] = [ ] ;
131+ restoreSink = configureExecutionIdentityAdmissionSink ( ( item ) => {
132+ work . push ( item ) ;
133+ return true ;
134+ } ) ;
135+ const lifecycleGeneration = getAgentRunLifecycleGeneration ( ) ;
136+ const runId = "run-shared" ;
137+
138+ admitAutoReplyExecutionAttribution ( {
139+ config : { logging : { audit : { enabled : true , executionIdentity : true } } } ,
140+ lifecycleGeneration,
141+ runId,
142+ context : {
143+ isHeartbeat : false ,
144+ sessionId : "first-session" ,
145+ sessionKey : "agent:main:first-session" ,
146+ } ,
147+ } ) ;
148+
149+ expect ( ( ) =>
150+ admitAutoReplyExecutionAttribution ( {
151+ config : { logging : { audit : { enabled : true , executionIdentity : true } } } ,
152+ lifecycleGeneration,
153+ runId,
154+ context : {
155+ isHeartbeat : false ,
156+ sessionId : "second-session" ,
157+ sessionKey : "agent:main:second-session" ,
158+ } ,
159+ } ) ,
160+ ) . toThrow ( "Agent run ID is already bound to different execution attribution." ) ;
161+ expect ( work ) . toHaveLength ( 1 ) ;
162+ } ) ;
163+
124164 it ( "does not persist or replace an already admitted attribution" , ( ) => {
125165 const sink = vi . fn ( ( ) => true ) ;
126166 restoreSink = configureExecutionIdentityAdmissionSink ( sink ) ;
0 commit comments