|
| 1 | +import type { Span } from "@opentelemetry/api"; |
| 2 | +import * as ai from "ai"; |
| 3 | +import { MockLanguageModelV3 } from "ai/test"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { z } from "zod"; |
| 6 | +import { NodeVoltAgentObservability } from "../observability"; |
| 7 | +import { createTool } from "../tool"; |
| 8 | +import { Agent } from "./agent"; |
| 9 | +import { SubAgentManager } from "./subagent"; |
| 10 | + |
| 11 | +// Mock the AI SDK functions |
| 12 | +vi.mock("ai", async () => { |
| 13 | + const actual = await vi.importActual<typeof import("ai")>("ai"); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + generateText: vi.fn(), |
| 17 | + streamText: vi.fn(), |
| 18 | + generateObject: vi.fn(), |
| 19 | + streamObject: vi.fn(), |
| 20 | + stepCountIs: vi.fn(() => vi.fn(() => false)), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +describe("Agent Concurrent Tool Spans", () => { |
| 25 | + let observability: NodeVoltAgentObservability; |
| 26 | + let mockModel: MockLanguageModelV3; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + observability = new NodeVoltAgentObservability(); |
| 30 | + mockModel = new MockLanguageModelV3(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should provide unique parent spans to tools running in parallel", async () => { |
| 34 | + const capturedSpans: Map<string, Span> = new Map(); |
| 35 | + |
| 36 | + // Create two tools that capture their parentToolSpan |
| 37 | + const toolA = createTool({ |
| 38 | + name: "toolA", |
| 39 | + description: "Tool A", |
| 40 | + parameters: z.object({}), |
| 41 | + execute: async (_, options) => { |
| 42 | + // Capture the span passed in options |
| 43 | + const span = (options as any).parentToolSpan; |
| 44 | + capturedSpans.set("toolA", span); |
| 45 | + // Simulate some async work to allow interleaving |
| 46 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 47 | + return "resultA"; |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const toolB = createTool({ |
| 52 | + name: "toolB", |
| 53 | + description: "Tool B", |
| 54 | + parameters: z.object({}), |
| 55 | + execute: async (_, options) => { |
| 56 | + // Capture the span passed in options |
| 57 | + const span = (options as any).parentToolSpan; |
| 58 | + capturedSpans.set("toolB", span); |
| 59 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 60 | + return "resultB"; |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + const agent = new Agent({ |
| 65 | + name: "test-agent", |
| 66 | + instructions: "test instructions", |
| 67 | + model: mockModel as any, |
| 68 | + observability, |
| 69 | + tools: [toolA, toolB], |
| 70 | + toolRouting: false, // Disable tool routing to ensure tools are directly available |
| 71 | + }); |
| 72 | + |
| 73 | + // Mock ai.generateText to simulate parallel tool execution |
| 74 | + vi.mocked(ai.generateText).mockImplementation(async (options: any) => { |
| 75 | + // options.tools contains the wrapped tools from Agent.ts |
| 76 | + const toolAWrapper = options.tools.toolA; |
| 77 | + const toolBWrapper = options.tools.toolB; |
| 78 | + |
| 79 | + // Parallel tools execution simulation |
| 80 | + await Promise.all([toolAWrapper.execute({}), toolBWrapper.execute({})]); |
| 81 | + |
| 82 | + return { |
| 83 | + text: "Done", |
| 84 | + finishReason: "stop", |
| 85 | + usage: { inputTokens: 10, outputTokens: 10, totalTokens: 20 }, |
| 86 | + steps: [], |
| 87 | + toolCalls: [], |
| 88 | + toolResults: [], |
| 89 | + response: { id: "test", modelId: "test", timestamp: new Date(), messages: [] }, |
| 90 | + } as any; |
| 91 | + }); |
| 92 | + |
| 93 | + await agent.generateText("Run tools in parallel"); |
| 94 | + |
| 95 | + expect(capturedSpans.has("toolA")).toBe(true); |
| 96 | + expect(capturedSpans.has("toolB")).toBe(true); |
| 97 | + |
| 98 | + const spanA = capturedSpans.get("toolA"); |
| 99 | + const spanB = capturedSpans.get("toolB"); |
| 100 | + |
| 101 | + expect(spanA).toBeDefined(); |
| 102 | + expect(spanB).toBeDefined(); |
| 103 | + |
| 104 | + // Spans shouldn't be the same |
| 105 | + expect(spanA).not.toBe(spanB); |
| 106 | + expect((spanA as any).name).toContain("tool.execution:toolA"); |
| 107 | + expect((spanB as any).name).toContain("tool.execution:toolB"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should provide unique parent spans to subagents running in parallel via delegate_task", async () => { |
| 111 | + // 1. Setup SubAgentManager |
| 112 | + const subAgent = new Agent({ |
| 113 | + name: "sub", |
| 114 | + instructions: "sub", |
| 115 | + model: mockModel as any, |
| 116 | + }); |
| 117 | + |
| 118 | + // Mock generateText on subAgent to verify parentSpan |
| 119 | + const generateTextSpy = vi.spyOn(subAgent, "generateText"); |
| 120 | + generateTextSpy.mockResolvedValue({ text: "ok", usage: {} } as any); |
| 121 | + |
| 122 | + const manager = new SubAgentManager("parent", [{ agent: subAgent, method: "generateText" }]); |
| 123 | + |
| 124 | + // 2. Create delegate_task tool |
| 125 | + const delegateTool = manager.createDelegateTool({ |
| 126 | + sourceAgent: new Agent({ |
| 127 | + name: "parent", |
| 128 | + instructions: "parent", |
| 129 | + model: mockModel as any, |
| 130 | + }), |
| 131 | + // Simulate creation-time parentToolSpan (stale/wrong one) |
| 132 | + parentToolSpan: { spanContext: () => ({ traceId: "stale" }) } as any, |
| 133 | + }); |
| 134 | + |
| 135 | + // 3. Simulate concurrent execution with distinct parent spans |
| 136 | + const span1 = { spanContext: () => ({ traceId: "trace1" }) } as any; |
| 137 | + const span2 = { spanContext: () => ({ traceId: "trace2" }) } as any; |
| 138 | + |
| 139 | + await Promise.all([ |
| 140 | + delegateTool.execute?.({ task: "task1", targetAgents: ["sub"] }, { |
| 141 | + parentToolSpan: span1, |
| 142 | + } as any), |
| 143 | + delegateTool.execute?.({ task: "task2", targetAgents: ["sub"] }, { |
| 144 | + parentToolSpan: span2, |
| 145 | + } as any), |
| 146 | + ]); |
| 147 | + |
| 148 | + // 4. Verify subAgent.generateText was called with correct parent spans |
| 149 | + expect(generateTextSpy).toHaveBeenCalledTimes(2); |
| 150 | + |
| 151 | + const calls = generateTextSpy.mock.calls; |
| 152 | + // Extract parentSpan from options (second argument to generateText) |
| 153 | + const spans = calls.map((c) => (c[1] as any).parentSpan); |
| 154 | + |
| 155 | + // Should contain the spans passed during execution |
| 156 | + expect(spans).toContain(span1); |
| 157 | + expect(spans).toContain(span2); |
| 158 | + |
| 159 | + // Should NOT contain the stale creation-time span |
| 160 | + const staleSpanInCalls = spans.some((s) => s && s.spanContext().traceId === "stale"); |
| 161 | + expect(staleSpanInCalls).toBe(false); |
| 162 | + }); |
| 163 | +}); |
0 commit comments