@@ -302,76 +302,122 @@ describe("agent activity audit projection", () => {
302302 } ) ;
303303 } ) ;
304304
305- it ( "keeps a valid unknown agent id distinct from missing provenance" , ( ) => {
306- const runId = "run-agent-named-unknown" ;
307- const started = projectAgentEventToAudit (
308- agentEvent ( { runId, sessionKey : "global" , agentId : "unknown" } ) ,
305+ it ( "does not infer agent identity from a session key" , ( ) => {
306+ const projected = projectAgentEventToAudit (
307+ agentEvent ( { agentId : undefined , sessionKey : "agent:admin:main" } ) ,
308+ ) ;
309+
310+ expect ( projected ) . toMatchObject ( {
311+ actorType : "system" ,
312+ actorId : "unknown" ,
313+ agentId : "unknown" ,
314+ sessionKey : "agent:admin:main" ,
315+ } ) ;
316+ } ) ;
317+
318+ it ( "keeps an explicit agent id named unknown distinct from missing identity" , ( ) => {
319+ const projected = projectAgentEventToAudit (
320+ agentEvent ( { agentId : "unknown" , sessionKey : undefined , sessionId : undefined } ) ,
309321 ) ;
310- const finished = projectAgentEventToAudit (
322+
323+ expect ( projected ) . toMatchObject ( {
324+ actorType : "agent" ,
325+ actorId : "unknown" ,
326+ agentId : "unknown" ,
327+ } ) ;
328+ } ) ;
329+
330+ it ( "does not share reused run id provenance across recorder instances" , ( ) => {
331+ const runId = "run-reused-across-recorders" ;
332+ projectAgentEventToAudit (
311333 agentEvent ( {
312334 runId,
313- seq : 2 ,
314- sessionKey : undefined ,
315- sessionId : undefined ,
316- agentId : undefined ,
317- data : { phase : "end" } ,
335+ lifecycleGeneration : "gateway-before-reuse" ,
336+ sessionKey : "agent:support:main" ,
337+ sessionId : "session-support" ,
338+ agentId : "support" ,
318339 } ) ,
319340 ) ;
320- const tool = projectToolExecutionEventToAudit (
321- toolEvent ( {
341+
342+ const projected = projectAgentEventToAudit (
343+ agentEvent ( {
322344 runId,
323- seq : 3 ,
345+ lifecycleGeneration : "gateway-after-reuse" ,
324346 sessionKey : undefined ,
325347 sessionId : undefined ,
326348 agentId : undefined ,
327349 } ) ,
328350 ) ;
329- const missing = projectAgentEventToAudit (
351+
352+ expect ( projected ) . toMatchObject ( {
353+ actorType : "system" ,
354+ actorId : "unknown" ,
355+ agentId : "unknown" ,
356+ } ) ;
357+ expect ( projected ) . not . toHaveProperty ( "sessionKey" ) ;
358+ expect ( projected ) . not . toHaveProperty ( "sessionId" ) ;
359+ } ) ;
360+
361+ it ( "does not let tool events inherit remembered lifecycle provenance" , async ( ) => {
362+ const inputs : AuditEventInput [ ] = [ ] ;
363+ const recorder = createAgentEventAuditRecorder ( { writer : captureAuditWriter ( inputs ) } ) ;
364+ const runId = "run-tool-no-provenance" ;
365+ recorder . record (
330366 agentEvent ( {
331- runId : "run-missing-provenance" ,
367+ runId,
368+ sessionKey : "agent:support:main" ,
369+ sessionId : "session-support" ,
370+ agentId : "support" ,
371+ } ) ,
372+ ) ;
373+ recorder . recordTool (
374+ toolEvent ( {
375+ runId,
332376 sessionKey : undefined ,
333377 sessionId : undefined ,
334378 agentId : undefined ,
335379 } ) ,
336380 ) ;
381+ await recorder . stop ( ) ;
337382
338- expect ( [ started , finished , tool ] ) . toEqual ( [
339- expect . objectContaining ( { actorType : "agent" , actorId : "unknown" , agentId : "unknown" } ) ,
340- expect . objectContaining ( { actorType : "agent" , actorId : "unknown" , agentId : "unknown" } ) ,
341- expect . objectContaining ( { actorType : "agent" , actorId : "unknown" , agentId : "unknown" } ) ,
342- ] ) ;
343- expect ( missing ) . toMatchObject ( {
383+ expect ( inputs . at ( - 1 ) ) . toMatchObject ( {
344384 actorType : "system" ,
345385 actorId : "unknown" ,
346386 agentId : "unknown" ,
347387 } ) ;
388+ expect ( inputs . at ( - 1 ) ) . not . toHaveProperty ( "sessionKey" ) ;
389+ expect ( inputs . at ( - 1 ) ) . not . toHaveProperty ( "sessionId" ) ;
348390 } ) ;
349391
350- it ( "keeps tool actions on the canonical lifecycle session" , ( ) => {
351- const runId = "run-channel-routed" ;
352- projectAgentEventToAudit (
392+ it ( "preserves explicit lifecycle and tool provenance independently" , ( ) => {
393+ const lifecycle = projectAgentEventToAudit (
353394 agentEvent ( {
354- runId,
355- sessionKey : "agent:support:channel:customer" ,
356- sessionId : "session-canonical" ,
357- agentId : "support" ,
395+ sessionKey : "agent:lifecycle:main" ,
396+ sessionId : "session-lifecycle" ,
397+ agentId : "lifecycle" ,
358398 } ) ,
359399 ) ;
360-
361- const projected = projectToolExecutionEventToAudit (
400+ const tool = projectToolExecutionEventToAudit (
362401 toolEvent ( {
363- runId,
364- sessionKey : "agent:main:sandbox:temporary" ,
365- sessionId : "session-sandbox" ,
366- agentId : "main" ,
402+ sessionKey : "agent:tool:sandbox:temporary" ,
403+ sessionId : "session-tool" ,
404+ agentId : "tool" ,
367405 } ) ,
368406 ) ;
369407
370- expect ( projected ) . toMatchObject ( {
371- actorId : "support" ,
372- agentId : "support" ,
373- sessionKey : "agent:support:channel:customer" ,
374- sessionId : "session-canonical" ,
408+ expect ( lifecycle ) . toMatchObject ( {
409+ actorType : "agent" ,
410+ actorId : "lifecycle" ,
411+ agentId : "lifecycle" ,
412+ sessionKey : "agent:lifecycle:main" ,
413+ sessionId : "session-lifecycle" ,
414+ } ) ;
415+ expect ( tool ) . toMatchObject ( {
416+ actorType : "agent" ,
417+ actorId : "tool" ,
418+ agentId : "tool" ,
419+ sessionKey : "agent:tool:sandbox:temporary" ,
420+ sessionId : "session-tool" ,
375421 } ) ;
376422 } ) ;
377423
@@ -415,15 +461,12 @@ describe("agent activity audit projection", () => {
415461 it ( "omits prompt, arguments, results, and raw errors from run and tool records" , ( ) => {
416462 const secret = "super-secret-payload" ;
417463 projectAgentEventToAudit ( agentEvent ( { data : { phase : "start" , prompt : secret } , seq : 1 } ) ) ;
418- const started = projectToolExecutionEventToAudit (
419- toolEvent ( { seq : 2 , sessionKey : undefined , sessionId : undefined } ) ,
420- ) ;
464+ const started = projectToolExecutionEventToAudit ( toolEvent ( { seq : 2 , agentId : "coder" } ) ) ;
421465 const failed = projectToolExecutionEventToAudit (
422466 toolEvent ( {
423467 type : "tool.execution.error" ,
424468 seq : 3 ,
425- sessionKey : undefined ,
426- sessionId : undefined ,
469+ agentId : "coder" ,
427470 durationMs : 10 ,
428471 errorCategory : secret ,
429472 errorCode : secret ,
@@ -519,14 +562,24 @@ describe("agent activity audit projection", () => {
519562 const projected = projectToolExecutionEventToAudit (
520563 toolEvent ( {
521564 type,
565+ agentId : "tool-agent" ,
566+ sessionKey : "agent:tool-agent:main" ,
567+ sessionId : "session-tool-agent" ,
522568 ...( type === "tool.execution.completed" || type === "tool.execution.error"
523569 ? { durationMs : 10 }
524570 : { deniedReason : "policy" , reason : "secret detail" } ) ,
525571 ...( type === "tool.execution.error" ? { errorCategory : "test" } : { } ) ,
526572 } ) ,
527573 ) ;
528574
529- expect ( projected ) . toMatchObject ( { status } ) ;
575+ expect ( projected ) . toMatchObject ( {
576+ status,
577+ actorType : "agent" ,
578+ actorId : "tool-agent" ,
579+ agentId : "tool-agent" ,
580+ sessionKey : "agent:tool-agent:main" ,
581+ sessionId : "session-tool-agent" ,
582+ } ) ;
530583 expect ( projected ?. errorCode ) . toBe ( errorCode ) ;
531584 expect ( projected ) . not . toHaveProperty ( "reason" ) ;
532585 } ) ;
0 commit comments