@@ -18,6 +18,7 @@ import { |
18 | 18 | type OrchestrationV2ProjectedTurnItem, |
19 | 19 | type OrchestrationV2ThreadShell, |
20 | 20 | } from "@t3tools/contracts"; |
| 21 | +import * as Deferred from "effect/Deferred"; |
21 | 22 | import * as Effect from "effect/Effect"; |
22 | 23 | import * as TestClock from "effect/testing/TestClock"; |
23 | 24 | import * as Layer from "effect/Layer"; |
@@ -364,7 +365,12 @@ it.effect("keeps unexpected handler defects private through the production regis |
364 | 365 | ); |
365 | 366 |
|
366 | 367 | it.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; |
368 | 374 | const shell = (id: ThreadId, instanceId: string, model: string) => |
369 | 375 | ({ |
370 | 376 | id, |
@@ -392,8 +398,7 @@ it.effect("records the calling agent and the agent a tool acted on", () => { |
392 | 398 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
393 | 399 | Effect.provideService(McpSchema.McpServerClient, client), |
394 | 400 | ); |
395 | | - expect(recorded).toHaveLength(1); |
396 | | - const [{ event, properties }] = recorded as [{ event: string; properties: object }]; |
| 401 | + const { event, properties } = yield* Deferred.await(recorded[0]!); |
397 | 402 | expect(event).toBe("mcp.tool.invoked"); |
398 | 403 | expect(properties).toMatchObject({ |
399 | 404 | tool: "t3_thread_fork", |
@@ -423,7 +428,8 @@ it.effect("records the calling agent and the agent a tool acted on", () => { |
423 | 428 | Effect.provideService(McpInvocationContext.McpInvocationContext, scope), |
424 | 429 | Effect.provideService(McpSchema.McpServerClient, client), |
425 | 430 | ); |
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({ |
427 | 433 | tool: "t3_queue_edit", |
428 | 434 | outcome: "ok", |
429 | 435 | callerProvider: "codex", |
@@ -470,7 +476,10 @@ it.effect("records the calling agent and the agent a tool acted on", () => { |
470 | 476 | AnalyticsService.AnalyticsService, |
471 | 477 | AnalyticsService.AnalyticsService.of({ |
472 | 478 | 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), |
474 | 483 | flush: Effect.void, |
475 | 484 | }), |
476 | 485 | ), |
|