|
| 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 { |
| 6 | + classifyErrorKind, |
| 7 | + classifySpanKind, |
| 8 | + emitAiObservability, |
| 9 | + emitAiTurnFailure, |
| 10 | + secondsFromMs, |
| 11 | + turnTraceId, |
| 12 | +} from "./ai-observability.js"; |
| 13 | + |
| 14 | +const SUBAGENT_TOOL_NAME = "task"; |
| 15 | +const SESSION_ID = "0199-parent-session"; |
| 16 | + |
| 17 | +function fakeTelemetry(): { telemetry: Telemetry; captured: { event: string; properties: Record<string, unknown> }[] } { |
| 18 | + const captured: { event: string; properties: Record<string, unknown> }[] = []; |
| 19 | + const telemetry: Telemetry = { |
| 20 | + enabled: true, |
| 21 | + capture: (event, properties = {}) => { |
| 22 | + captured.push({ event, properties }); |
| 23 | + }, |
| 24 | + flush: async () => {}, |
| 25 | + discard: () => {}, |
| 26 | + }; |
| 27 | + return { telemetry, captured }; |
| 28 | +} |
| 29 | + |
| 30 | +function fakeTurnContext(overrides: Partial<TurnContext> = {}): TurnContext { |
| 31 | + const toolCalls: ToolCall[] = [ |
| 32 | + { |
| 33 | + id: "call-1", |
| 34 | + name: "read_file", |
| 35 | + arguments: { path: "/Users/attacker/secret-project/plan.md" }, |
| 36 | + }, |
| 37 | + { |
| 38 | + id: "call-2", |
| 39 | + name: SUBAGENT_TOOL_NAME, |
| 40 | + arguments: { description: "explore", prompt: "find the leaked API key XYZ-SECRET-123" }, |
| 41 | + }, |
| 42 | + ]; |
| 43 | + const toolResults: ToolResult[] = [ |
| 44 | + { callId: "call-1", content: "file contents: super secret prompt text" }, |
| 45 | + { callId: "call-2", content: "sub-agent report containing prompt XYZ-SECRET-123", isError: true }, |
| 46 | + ]; |
| 47 | + return { |
| 48 | + turnIndex: 3, |
| 49 | + assistantTurn: { |
| 50 | + role: "assistant", |
| 51 | + content: [{ type: "text", text: "here is the plan: XYZ-SECRET-123" }], |
| 52 | + model: "model-x", |
| 53 | + timestamp: 0, |
| 54 | + }, |
| 55 | + toolCalls, |
| 56 | + toolResults, |
| 57 | + usage: { input: 10, output: 20, cacheRead: 1, cacheWrite: 2, thinking: 3 }, |
| 58 | + source: { provider: "openai-compatible", model: "model-x" }, |
| 59 | + durationMs: 4560, |
| 60 | + ...overrides, |
| 61 | + } as TurnContext; |
| 62 | +} |
| 63 | + |
| 64 | +const emitOptions = { sessionId: SESSION_ID, subagentToolName: SUBAGENT_TOOL_NAME }; |
| 65 | + |
| 66 | +describe("secondsFromMs", () => { |
| 67 | + test("converts milliseconds to fractional seconds, the unit PostHog documents", () => { |
| 68 | + expect(secondsFromMs(361)).toBe(0.361); |
| 69 | + expect(secondsFromMs(4560)).toBe(4.56); |
| 70 | + expect(secondsFromMs(0)).toBe(0); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("classifySpanKind", () => { |
| 75 | + test("classifies the subagent tool as subagent_call", () => { |
| 76 | + expect(classifySpanKind(SUBAGENT_TOOL_NAME, SUBAGENT_TOOL_NAME)).toBe("subagent_call"); |
| 77 | + }); |
| 78 | + |
| 79 | + test("classifies every other tool as tool_call, regardless of name", () => { |
| 80 | + expect(classifySpanKind("read_file", SUBAGENT_TOOL_NAME)).toBe("tool_call"); |
| 81 | + expect(classifySpanKind("mcp__acme__fetch_secret", SUBAGENT_TOOL_NAME)).toBe("tool_call"); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("classifyErrorKind", () => { |
| 86 | + test("reduces provider messages to fixed reasons", () => { |
| 87 | + expect(classifyErrorKind("HTTP 429 rate limit exceeded")).toBe("rate_limit"); |
| 88 | + expect(classifyErrorKind("401 Unauthorized")).toBe("auth"); |
| 89 | + expect(classifyErrorKind("request timed out after 60s")).toBe("timeout"); |
| 90 | + expect(classifyErrorKind("The operation was aborted")).toBe("cancelled"); |
| 91 | + expect(classifyErrorKind("upstream returned garbage")).toBe("inference_failed"); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("turnTraceId", () => { |
| 96 | + test("derives from the runtime session id and turn index rather than inventing a random id", () => { |
| 97 | + expect(turnTraceId("session-abc", 3)).toBe("session-abc:turn:3"); |
| 98 | + expect(turnTraceId("session-abc", 3)).toBe(turnTraceId("session-abc", 3)); |
| 99 | + }); |
| 100 | + |
| 101 | + test("a sub-agent turn never collides with the parent turn of the same index", () => { |
| 102 | + // Sub-agents run in this same process, so a process-wide scope would make |
| 103 | + // these identical and silently merge two unrelated traces. |
| 104 | + expect(turnTraceId("parent-session", 3)).not.toBe(turnTraceId("subagent-session", 3)); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe("emitAiObservability", () => { |
| 109 | + test("emits one $ai_generation and one $ai_span per tool call", () => { |
| 110 | + const { telemetry, captured } = fakeTelemetry(); |
| 111 | + |
| 112 | + emitAiObservability(telemetry, fakeTurnContext(), emitOptions); |
| 113 | + |
| 114 | + expect(captured.length).toBe(3); |
| 115 | + expect(captured[0]?.event).toBe("$ai_generation"); |
| 116 | + expect(captured[1]?.event).toBe("$ai_span"); |
| 117 | + expect(captured[2]?.event).toBe("$ai_span"); |
| 118 | + }); |
| 119 | + |
| 120 | + test("reports latency in seconds, not milliseconds", () => { |
| 121 | + const { telemetry, captured } = fakeTelemetry(); |
| 122 | + |
| 123 | + emitAiObservability(telemetry, fakeTurnContext({ durationMs: 361 }), emitOptions); |
| 124 | + |
| 125 | + const generation = captured.find((c) => c.event === "$ai_generation"); |
| 126 | + expect(generation?.properties.$ai_latency).toBe(0.361); |
| 127 | + expect(generation?.properties.$ai_latency).not.toBe(361); |
| 128 | + }); |
| 129 | + |
| 130 | + test("names every field PostHog's LLM analytics views actually query", () => { |
| 131 | + const { telemetry, captured } = fakeTelemetry(); |
| 132 | + |
| 133 | + emitAiObservability(telemetry, fakeTurnContext(), emitOptions); |
| 134 | + |
| 135 | + const generation = captured.find((c) => c.event === "$ai_generation"); |
| 136 | + expect(generation?.properties.$ai_provider).toBe("openai-compatible"); |
| 137 | + expect(generation?.properties.$ai_model).toBe("model-x"); |
| 138 | + expect(generation?.properties.$ai_input_tokens).toBe(10); |
| 139 | + expect(generation?.properties.$ai_output_tokens).toBe(20); |
| 140 | + expect(generation?.properties).not.toHaveProperty("provider_id"); |
| 141 | + expect(generation?.properties).not.toHaveProperty("input_tokens"); |
| 142 | + expect(generation?.properties).not.toHaveProperty("duration_ms"); |
| 143 | + }); |
| 144 | + |
| 145 | + test("flat trace: spans parent onto the trace id, not onto each other", () => { |
| 146 | + const { telemetry, captured } = fakeTelemetry(); |
| 147 | + const ctx = fakeTurnContext(); |
| 148 | + |
| 149 | + emitAiObservability(telemetry, ctx, emitOptions); |
| 150 | + |
| 151 | + const traceId = turnTraceId(SESSION_ID, ctx.turnIndex); |
| 152 | + const generation = captured.find((c) => c.event === "$ai_generation"); |
| 153 | + const spans = captured.filter((c) => c.event === "$ai_span"); |
| 154 | + |
| 155 | + expect(generation?.properties.$ai_trace_id).toBe(traceId); |
| 156 | + for (const span of spans) { |
| 157 | + expect(span.properties.$ai_trace_id).toBe(traceId); |
| 158 | + expect(span.properties.$ai_parent_id).toBe(traceId); |
| 159 | + } |
| 160 | + expect(spans[0]?.properties.$ai_span_id).toBe("call-1"); |
| 161 | + expect(spans[1]?.properties.$ai_span_id).toBe("call-2"); |
| 162 | + }); |
| 163 | + |
| 164 | + test("names the span by fixed enum, never the raw tool name", () => { |
| 165 | + const { telemetry, captured } = fakeTelemetry(); |
| 166 | + |
| 167 | + emitAiObservability(telemetry, fakeTurnContext(), emitOptions); |
| 168 | + |
| 169 | + const spans = captured.filter((c) => c.event === "$ai_span"); |
| 170 | + expect(spans[0]?.properties.$ai_span_name).toBe("tool_call"); |
| 171 | + expect(spans[1]?.properties.$ai_span_name).toBe("subagent_call"); |
| 172 | + for (const span of spans) { |
| 173 | + expect(span.properties.$ai_span_name).not.toBe("read_file"); |
| 174 | + expect(span.properties.$ai_span_name).not.toBe(SUBAGENT_TOOL_NAME); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + test("propagates tool error state onto the span without the result content", () => { |
| 179 | + const { telemetry, captured } = fakeTelemetry(); |
| 180 | + |
| 181 | + emitAiObservability(telemetry, fakeTurnContext(), emitOptions); |
| 182 | + |
| 183 | + const spans = captured.filter((c) => c.event === "$ai_span"); |
| 184 | + expect(spans[0]?.properties.$ai_is_error).toBe(false); |
| 185 | + expect(spans[1]?.properties.$ai_is_error).toBe(true); |
| 186 | + }); |
| 187 | + |
| 188 | + test("never leaks prompt text, tool arguments, tool results, or file paths", () => { |
| 189 | + const { telemetry, captured } = fakeTelemetry(); |
| 190 | + |
| 191 | + emitAiObservability(telemetry, fakeTurnContext(), emitOptions); |
| 192 | + |
| 193 | + const serialized = JSON.stringify(captured); |
| 194 | + expect(serialized).not.toContain("secret-project"); |
| 195 | + expect(serialized).not.toContain("plan.md"); |
| 196 | + expect(serialized).not.toContain("XYZ-SECRET-123"); |
| 197 | + expect(serialized).not.toContain("super secret prompt text"); |
| 198 | + expect(serialized).not.toContain("find the leaked"); |
| 199 | + expect(serialized).not.toContain("here is the plan"); |
| 200 | + expect(serialized).not.toContain("/Users/attacker"); |
| 201 | + }); |
| 202 | +}); |
| 203 | + |
| 204 | +describe("emitAiTurnFailure", () => { |
| 205 | + test("emits an errored $ai_generation for a turn that never completed", () => { |
| 206 | + const { telemetry, captured } = fakeTelemetry(); |
| 207 | + |
| 208 | + emitAiTurnFailure(telemetry, { |
| 209 | + sessionId: SESSION_ID, |
| 210 | + turnIndex: 7, |
| 211 | + error: "HTTP 429 rate limit exceeded", |
| 212 | + }); |
| 213 | + |
| 214 | + expect(captured.length).toBe(1); |
| 215 | + expect(captured[0]?.event).toBe("$ai_generation"); |
| 216 | + expect(captured[0]?.properties.$ai_trace_id).toBe(turnTraceId(SESSION_ID, 7)); |
| 217 | + expect(captured[0]?.properties.$ai_is_error).toBe(true); |
| 218 | + expect(captured[0]?.properties.$ai_error).toBe("rate_limit"); |
| 219 | + }); |
| 220 | + |
| 221 | + test("never leaks the raw provider error message", () => { |
| 222 | + const { telemetry, captured } = fakeTelemetry(); |
| 223 | + |
| 224 | + emitAiTurnFailure(telemetry, { |
| 225 | + sessionId: SESSION_ID, |
| 226 | + turnIndex: 7, |
| 227 | + error: |
| 228 | + "429 rate limit on https://api.internal.acme.corp/v1/chat while reading /Users/attacker/secret-project/plan.md: XYZ-SECRET-123", |
| 229 | + }); |
| 230 | + |
| 231 | + const serialized = JSON.stringify(captured); |
| 232 | + expect(serialized).not.toContain("acme.corp"); |
| 233 | + expect(serialized).not.toContain("/Users/attacker"); |
| 234 | + expect(serialized).not.toContain("secret-project"); |
| 235 | + expect(serialized).not.toContain("XYZ-SECRET-123"); |
| 236 | + }); |
| 237 | +}); |
0 commit comments