|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import type { ToolCall, ToolResult } from "@intx/types/runtime"; |
| 3 | +import type { Telemetry } from "./index.js"; |
| 4 | +import type { TurnContext } from "../session/hooks.js"; |
| 5 | +import { classifySpanKind, emitAiObservability, turnTraceId } from "./ai-observability.js"; |
| 6 | + |
| 7 | +const SUBAGENT_TOOL_NAME = "task"; |
| 8 | + |
| 9 | +function fakeTelemetry(): { telemetry: Telemetry; captured: { event: string; properties: Record<string, unknown> }[] } { |
| 10 | + const captured: { event: string; properties: Record<string, unknown> }[] = []; |
| 11 | + const telemetry: Telemetry = { |
| 12 | + enabled: true, |
| 13 | + capture: (event, properties = {}) => { |
| 14 | + captured.push({ event, properties }); |
| 15 | + }, |
| 16 | + flush: async () => {}, |
| 17 | + }; |
| 18 | + return { telemetry, captured }; |
| 19 | +} |
| 20 | + |
| 21 | +function fakeTurnContext(overrides: Partial<TurnContext> = {}): TurnContext { |
| 22 | + const toolCalls: ToolCall[] = [ |
| 23 | + { |
| 24 | + id: "call-1", |
| 25 | + name: "read_file", |
| 26 | + arguments: { path: "/Users/attacker/secret-project/plan.md" }, |
| 27 | + }, |
| 28 | + { |
| 29 | + id: "call-2", |
| 30 | + name: SUBAGENT_TOOL_NAME, |
| 31 | + arguments: { description: "explore", prompt: "find the leaked API key XYZ-SECRET-123" }, |
| 32 | + }, |
| 33 | + ]; |
| 34 | + const toolResults: ToolResult[] = [ |
| 35 | + { callId: "call-1", content: "file contents: super secret prompt text" }, |
| 36 | + { callId: "call-2", content: "sub-agent report containing prompt XYZ-SECRET-123", isError: true }, |
| 37 | + ]; |
| 38 | + return { |
| 39 | + turnIndex: 3, |
| 40 | + assistantTurn: { |
| 41 | + role: "assistant", |
| 42 | + content: [{ type: "text", text: "here is the plan: XYZ-SECRET-123" }], |
| 43 | + model: "claude-x", |
| 44 | + timestamp: 0, |
| 45 | + }, |
| 46 | + toolCalls, |
| 47 | + toolResults, |
| 48 | + usage: { input: 10, output: 20, cacheRead: 1, cacheWrite: 2, thinking: 3 }, |
| 49 | + source: { provider: "anthropic", model: "claude-x" }, |
| 50 | + durationMs: 456, |
| 51 | + ...overrides, |
| 52 | + } as TurnContext; |
| 53 | +} |
| 54 | + |
| 55 | +describe("classifySpanKind", () => { |
| 56 | + test("classifies the subagent tool as subagent_call", () => { |
| 57 | + expect(classifySpanKind(SUBAGENT_TOOL_NAME, SUBAGENT_TOOL_NAME)).toBe("subagent_call"); |
| 58 | + }); |
| 59 | + |
| 60 | + test("classifies every other tool as tool_call, regardless of name", () => { |
| 61 | + expect(classifySpanKind("read_file", SUBAGENT_TOOL_NAME)).toBe("tool_call"); |
| 62 | + expect(classifySpanKind("mcp__acme__fetch_secret", SUBAGENT_TOOL_NAME)).toBe("tool_call"); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe("turnTraceId", () => { |
| 67 | + test("derives from an explicit session id and turn index rather than inventing a random id", () => { |
| 68 | + expect(turnTraceId(3, "session-abc")).toBe("session-abc:turn:3"); |
| 69 | + expect(turnTraceId(3, "session-abc")).toBe(turnTraceId(3, "session-abc")); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("emitAiObservability", () => { |
| 74 | + test("emits one $ai_generation and one $ai_span per tool call", () => { |
| 75 | + const { telemetry, captured } = fakeTelemetry(); |
| 76 | + const ctx = fakeTurnContext(); |
| 77 | + |
| 78 | + emitAiObservability(telemetry, ctx, { subagentToolName: SUBAGENT_TOOL_NAME }); |
| 79 | + |
| 80 | + expect(captured.length).toBe(3); |
| 81 | + expect(captured[0]?.event).toBe("$ai_generation"); |
| 82 | + expect(captured[1]?.event).toBe("$ai_span"); |
| 83 | + expect(captured[2]?.event).toBe("$ai_span"); |
| 84 | + }); |
| 85 | + |
| 86 | + test("tree linkage: generation and spans share $ai_trace_id, spans carry $ai_parent_id", () => { |
| 87 | + const { telemetry, captured } = fakeTelemetry(); |
| 88 | + const ctx = fakeTurnContext(); |
| 89 | + |
| 90 | + emitAiObservability(telemetry, ctx, { subagentToolName: SUBAGENT_TOOL_NAME }); |
| 91 | + |
| 92 | + const traceId = turnTraceId(ctx.turnIndex); |
| 93 | + const generation = captured.find((c) => c.event === "$ai_generation")!; |
| 94 | + const spans = captured.filter((c) => c.event === "$ai_span"); |
| 95 | + |
| 96 | + expect(generation.properties.$ai_trace_id).toBe(traceId); |
| 97 | + for (const span of spans) { |
| 98 | + expect(span.properties.$ai_trace_id).toBe(traceId); |
| 99 | + expect(span.properties.$ai_parent_id).toBe(traceId); |
| 100 | + } |
| 101 | + expect(spans[0]?.properties.$ai_span_id).toBe("call-1"); |
| 102 | + expect(spans[1]?.properties.$ai_span_id).toBe("call-2"); |
| 103 | + }); |
| 104 | + |
| 105 | + test("classifies the tool-call span kind by fixed enum, never the raw tool name", () => { |
| 106 | + const { telemetry, captured } = fakeTelemetry(); |
| 107 | + const ctx = fakeTurnContext(); |
| 108 | + |
| 109 | + emitAiObservability(telemetry, ctx, { subagentToolName: SUBAGENT_TOOL_NAME }); |
| 110 | + |
| 111 | + const spans = captured.filter((c) => c.event === "$ai_span"); |
| 112 | + expect(spans[0]?.properties.span_kind).toBe("tool_call"); |
| 113 | + expect(spans[1]?.properties.span_kind).toBe("subagent_call"); |
| 114 | + for (const span of spans) { |
| 115 | + expect(span.properties.span_kind).not.toBe("read_file"); |
| 116 | + expect(span.properties.span_kind).not.toBe(SUBAGENT_TOOL_NAME); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + test("propagates tool error status onto the span without the result content", () => { |
| 121 | + const { telemetry, captured } = fakeTelemetry(); |
| 122 | + const ctx = fakeTurnContext(); |
| 123 | + |
| 124 | + emitAiObservability(telemetry, ctx, { subagentToolName: SUBAGENT_TOOL_NAME }); |
| 125 | + |
| 126 | + const spans = captured.filter((c) => c.event === "$ai_span"); |
| 127 | + expect(spans[0]?.properties.status).toBe("ok"); |
| 128 | + expect(spans[1]?.properties.status).toBe("error"); |
| 129 | + }); |
| 130 | + |
| 131 | + test("never leaks prompt text, tool arguments, tool results, or file paths", () => { |
| 132 | + const { telemetry, captured } = fakeTelemetry(); |
| 133 | + const ctx = fakeTurnContext(); |
| 134 | + |
| 135 | + emitAiObservability(telemetry, ctx, { subagentToolName: SUBAGENT_TOOL_NAME }); |
| 136 | + |
| 137 | + const serialized = JSON.stringify(captured); |
| 138 | + expect(serialized).not.toContain("secret-project"); |
| 139 | + expect(serialized).not.toContain("plan.md"); |
| 140 | + expect(serialized).not.toContain("XYZ-SECRET-123"); |
| 141 | + expect(serialized).not.toContain("super secret prompt text"); |
| 142 | + expect(serialized).not.toContain("find the leaked"); |
| 143 | + expect(serialized).not.toContain("here is the plan"); |
| 144 | + expect(serialized).not.toContain("/Users/attacker"); |
| 145 | + }); |
| 146 | +}); |
0 commit comments