|
| 1 | +// The remote server's telemetry sink and span registry (#9525). |
| 2 | +// |
| 3 | +// The wrapper in dispatch-telemetry.ts is pure and covered separately; this covers the I/O half -- |
| 4 | +// both sides of every gate, and the guarantee that a sink failure never reaches the tool caller. |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import type { McpToolCallTelemetry } from "@loopover/contract"; |
| 7 | +import { createDispatchTelemetrySink, type DispatchTelemetryEnv } from "../../src/mcp/dispatch-telemetry-sink"; |
| 8 | +import { |
| 9 | + getMcpDispatchSpanRunner, |
| 10 | + resetMcpDispatchSpanRunnerForTest, |
| 11 | + setMcpDispatchSpanRunner, |
| 12 | +} from "../../src/mcp/dispatch-span-registry"; |
| 13 | + |
| 14 | +const call: McpToolCallTelemetry = { tool: "loopover_get_repo_context", category: "maintainer", surface: "remote", ok: true, durationMs: 4 }; |
| 15 | +const properties = { usage: { tool: call.tool }, mcpToolCall: { tool: call.tool } }; |
| 16 | + |
| 17 | +function env(overrides: Partial<DispatchTelemetryEnv> = {}): DispatchTelemetryEnv { |
| 18 | + return overrides as DispatchTelemetryEnv; |
| 19 | +} |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + resetMcpDispatchSpanRunnerForTest(); |
| 23 | + vi.restoreAllMocks(); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("MCP dispatch span registry (#9525)", () => { |
| 27 | + it("is empty until a self-host boot fills it, and clears again", () => { |
| 28 | + expect(getMcpDispatchSpanRunner()).toBeUndefined(); |
| 29 | + const runner = async <T>(_name: string, _attributes: Record<string, unknown>, fn: () => Promise<T>): Promise<T> => fn(); |
| 30 | + setMcpDispatchSpanRunner(runner); |
| 31 | + expect(getMcpDispatchSpanRunner()).toBe(runner); |
| 32 | + setMcpDispatchSpanRunner(null); |
| 33 | + expect(getMcpDispatchSpanRunner()).toBeUndefined(); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe("MCP dispatch telemetry sink (#9525)", () => { |
| 38 | + it("records nothing and defers nothing when POSTHOG_API_KEY is unset", () => { |
| 39 | + const deferred: Promise<unknown>[] = []; |
| 40 | + const sink = createDispatchTelemetrySink(env(), (work) => deferred.push(work)); |
| 41 | + sink.recordToolCall(call, properties); |
| 42 | + expect(deferred).toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("treats a blank POSTHOG_API_KEY as unset", () => { |
| 46 | + const deferred: Promise<unknown>[] = []; |
| 47 | + const sink = createDispatchTelemetrySink(env({ POSTHOG_API_KEY: " " }), (work) => deferred.push(work)); |
| 48 | + sink.recordToolCall(call, properties); |
| 49 | + expect(deferred).toEqual([]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("defers one capture when the key is set, and never rejects even with no reachable host", async () => { |
| 53 | + const deferred: Promise<unknown>[] = []; |
| 54 | + const sink = createDispatchTelemetrySink(env({ POSTHOG_API_KEY: "phc_test", POSTHOG_HOST: "http://127.0.0.1:1" }), (work) => deferred.push(work)); |
| 55 | + sink.recordToolCall(call, properties); |
| 56 | + expect(deferred).toHaveLength(1); |
| 57 | + // The never-throws guarantee: a PostHog init/capture/flush failure records nothing and resolves. |
| 58 | + await expect(deferred[0]).resolves.toBeUndefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("falls back to the US-cloud host when POSTHOG_HOST is unset", async () => { |
| 62 | + const deferred: Promise<unknown>[] = []; |
| 63 | + const sink = createDispatchTelemetrySink(env({ POSTHOG_API_KEY: "phc_test" }), (work) => deferred.push(work)); |
| 64 | + sink.recordToolCall(call, properties); |
| 65 | + expect(deferred).toHaveLength(1); |
| 66 | + // Reaches the real default host and fails there; the guarantee under test is that it resolves |
| 67 | + // rather than rejecting into the tool caller. |
| 68 | + await expect(deferred[0]).resolves.toBeUndefined(); |
| 69 | + }, 20_000); |
| 70 | + |
| 71 | + it("captures nothing when the Worker exception key is unset", () => { |
| 72 | + const deferred: Promise<unknown>[] = []; |
| 73 | + const sink = createDispatchTelemetrySink(env({ POSTHOG_API_KEY: "phc_test" }), (work) => deferred.push(work)); |
| 74 | + sink.captureException(new Error("boom"), call); |
| 75 | + expect(deferred).toEqual([]); |
| 76 | + }); |
| 77 | + |
| 78 | + it("defers an exception capture when the Worker key IS set -- a separate gate from the usage one", async () => { |
| 79 | + const deferred: Promise<unknown>[] = []; |
| 80 | + const sink = createDispatchTelemetrySink( |
| 81 | + env({ WORKER_POSTHOG_API_KEY: "phc_worker", WORKER_POSTHOG_HOST: "http://127.0.0.1:1" }), |
| 82 | + (work) => deferred.push(work), |
| 83 | + ); |
| 84 | + // No POSTHOG_API_KEY here: the two gates are deliberately independent (see the sink's header), |
| 85 | + // so exception capture is on while usage events stay off. |
| 86 | + sink.recordToolCall(call, properties); |
| 87 | + sink.captureException(new Error("boom"), call); |
| 88 | + expect(deferred).toHaveLength(1); |
| 89 | + await expect(deferred[0]).resolves.toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("passes the call through untouched when no span runner is registered", async () => { |
| 93 | + const sink = createDispatchTelemetrySink(env(), () => undefined); |
| 94 | + await expect(sink.withSpan("mcp.tool/x", { tool: "x" }, async () => "through")).resolves.toBe("through"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("uses the registry's runner when a self-host boot has filled it", async () => { |
| 98 | + const seen: Array<{ name: string; attributes: Record<string, unknown> }> = []; |
| 99 | + setMcpDispatchSpanRunner(async (name, attributes, fn) => { |
| 100 | + seen.push({ name, attributes }); |
| 101 | + return fn(); |
| 102 | + }); |
| 103 | + const sink = createDispatchTelemetrySink(env(), () => undefined); |
| 104 | + await expect(sink.withSpan("mcp.tool/x", { tool: "x" }, async () => "wrapped")).resolves.toBe("wrapped"); |
| 105 | + expect(seen).toEqual([{ name: "mcp.tool/x", attributes: { tool: "x" } }]); |
| 106 | + }); |
| 107 | + |
| 108 | + it("prefers an explicitly injected runner over the registry", async () => { |
| 109 | + setMcpDispatchSpanRunner(async () => { |
| 110 | + throw new Error("registry runner should not have been used"); |
| 111 | + }); |
| 112 | + let injectedCalls = 0; |
| 113 | + const injected = async <T>(_name: string, _attributes: Record<string, unknown>, fn: () => Promise<T>): Promise<T> => { |
| 114 | + injectedCalls += 1; |
| 115 | + return fn(); |
| 116 | + }; |
| 117 | + const sink = createDispatchTelemetrySink(env(), () => undefined, injected); |
| 118 | + await expect(sink.withSpan("mcp.tool/x", {}, async () => "injected")).resolves.toBe("injected"); |
| 119 | + expect(injectedCalls).toBe(1); |
| 120 | + }); |
| 121 | +}); |
0 commit comments