@@ -18,6 +18,7 @@ import {
1818 type OrchestrationV2ProjectedTurnItem ,
1919 type OrchestrationV2ThreadShell ,
2020} from "@t3tools/contracts" ;
21+ import * as Deferred from "effect/Deferred" ;
2122import * as Effect from "effect/Effect" ;
2223import * as TestClock from "effect/testing/TestClock" ;
2324import * as Layer from "effect/Layer" ;
@@ -364,7 +365,12 @@ it.effect("keeps unexpected handler defects private through the production regis
364365) ;
365366
366367it . effect ( "records the calling agent and the agent a tool acted on" , ( ) => {
367- const recorded : Array < { event : string ; properties : unknown } > = [ ] ;
368+ // Recording runs detached from the tool response, so each expected event is awaited.
369+ const recorded = [
370+ Deferred . makeUnsafe < { event : string ; properties : object } > ( ) ,
371+ Deferred . makeUnsafe < { event : string ; properties : object } > ( ) ,
372+ ] ;
373+ let recordedCount = 0 ;
368374 const shell = ( id : ThreadId , instanceId : string , model : string ) =>
369375 ( {
370376 id,
@@ -392,8 +398,7 @@ it.effect("records the calling agent and the agent a tool acted on", () => {
392398 Effect . provideService ( McpInvocationContext . McpInvocationContext , scope ) ,
393399 Effect . provideService ( McpSchema . McpServerClient , client ) ,
394400 ) ;
395- expect ( recorded ) . toHaveLength ( 1 ) ;
396- const [ { event, properties } ] = recorded as [ { event : string ; properties : object } ] ;
401+ const { event, properties } = yield * Deferred . await ( recorded [ 0 ] ! ) ;
397402 expect ( event ) . toBe ( "mcp.tool.invoked" ) ;
398403 expect ( properties ) . toMatchObject ( {
399404 tool : "t3_thread_fork" ,
@@ -423,7 +428,8 @@ it.effect("records the calling agent and the agent a tool acted on", () => {
423428 Effect . provideService ( McpInvocationContext . McpInvocationContext , scope ) ,
424429 Effect . provideService ( McpSchema . McpServerClient , client ) ,
425430 ) ;
426- expect ( recorded [ 1 ] ?. properties ) . toMatchObject ( {
431+ // The fork recorded a single event, so the next one is the queue edit.
432+ expect ( ( yield * Deferred . await ( recorded [ 1 ] ! ) ) . properties ) . toMatchObject ( {
427433 tool : "t3_queue_edit" ,
428434 outcome : "ok" ,
429435 callerProvider : "codex" ,
@@ -470,7 +476,10 @@ it.effect("records the calling agent and the agent a tool acted on", () => {
470476 AnalyticsService . AnalyticsService ,
471477 AnalyticsService . AnalyticsService . of ( {
472478 record : ( event , properties ) =>
473- Effect . sync ( ( ) => void recorded . push ( { event, properties } ) ) ,
479+ Deferred . succeed ( recorded [ recordedCount ++ ] ! , {
480+ event,
481+ properties : properties ?? { } ,
482+ } ) . pipe ( Effect . asVoid ) ,
474483 flush : Effect . void ,
475484 } ) ,
476485 ) ,
0 commit comments