|
| 1 | +import type { IncomingMessage } from "node:http"; |
| 2 | +import { beforeAll, beforeEach, describe, expect, it } from "vitest"; |
| 3 | +import { createGatewayRequestDiagnosticTrace } from "../../gateway/request-diagnostic-trace.js"; |
| 4 | +import { makeAttemptResult } from "./run.overflow-compaction.fixture.js"; |
| 5 | +import { |
| 6 | + loadRunOverflowCompactionHarness, |
| 7 | + mockedClassifyFailoverReason, |
| 8 | + mockedGlobalHookRunner, |
| 9 | + mockedRunEmbeddedAttempt, |
| 10 | + overflowBaseRunParams, |
| 11 | + resetRunOverflowCompactionHarnessMocks, |
| 12 | +} from "./run.overflow-compaction.harness.js"; |
| 13 | + |
| 14 | +let runEmbeddedAgent: typeof import("./run.js").runEmbeddedAgent; |
| 15 | +let traceContext: typeof import("../../infra/diagnostic-trace-context.js"); |
| 16 | + |
| 17 | +function requestWithTraceparent(traceparent: string): IncomingMessage { |
| 18 | + return { headers: { traceparent } } as IncomingMessage; |
| 19 | +} |
| 20 | + |
| 21 | +describe("runEmbeddedAgent diagnostic trace chain", () => { |
| 22 | + beforeAll(async () => { |
| 23 | + ({ runEmbeddedAgent } = await loadRunOverflowCompactionHarness()); |
| 24 | + traceContext = await import("../../infra/diagnostic-trace-context.js"); |
| 25 | + }); |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + resetRunOverflowCompactionHarnessMocks(); |
| 29 | + mockedGlobalHookRunner.hasHooks.mockImplementation(() => false); |
| 30 | + mockedClassifyFailoverReason.mockReturnValue(null); |
| 31 | + }); |
| 32 | + |
| 33 | + it("passes a gateway trace into attempt diagnostics", async () => { |
| 34 | + const inboundTrace = traceContext.createDiagnosticTraceContext({ |
| 35 | + traceId: "4bf92f3577b34da6a3ce929d0e0e4736", |
| 36 | + spanId: "00f067aa0ba902b7", |
| 37 | + }); |
| 38 | + let activeAttemptTrace: ReturnType<typeof traceContext.getActiveDiagnosticTraceContext>; |
| 39 | + mockedRunEmbeddedAttempt.mockImplementationOnce(async () => { |
| 40 | + activeAttemptTrace = traceContext.getActiveDiagnosticTraceContext(); |
| 41 | + return makeAttemptResult(); |
| 42 | + }); |
| 43 | + |
| 44 | + await runEmbeddedAgent({ |
| 45 | + ...overflowBaseRunParams, |
| 46 | + runId: "run-trace-chain", |
| 47 | + sessionId: "session-trace-chain", |
| 48 | + sessionKey: "agent:main:trace-chain", |
| 49 | + provider: "anthropic", |
| 50 | + model: "claude-sonnet-4.6", |
| 51 | + messageChannel: "gateway-test", |
| 52 | + diagnosticTrace: inboundTrace, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(activeAttemptTrace?.traceId).toBe(inboundTrace.traceId); |
| 56 | + expect(activeAttemptTrace?.parentSpanId).toMatch(/^[0-9a-f]{16}$/); |
| 57 | + expect(activeAttemptTrace?.parentSpanId).not.toBe(inboundTrace.spanId); |
| 58 | + expect(activeAttemptTrace?.spanId).toMatch(/^[0-9a-f]{16}$/); |
| 59 | + expect(activeAttemptTrace?.spanId).not.toBe(inboundTrace.spanId); |
| 60 | + }); |
| 61 | + |
| 62 | + it("creates a root invocation trace for non-gateway embedded runs", async () => { |
| 63 | + let activeAttemptTrace: ReturnType<typeof traceContext.getActiveDiagnosticTraceContext>; |
| 64 | + mockedRunEmbeddedAttempt.mockImplementationOnce(async () => { |
| 65 | + activeAttemptTrace = traceContext.getActiveDiagnosticTraceContext(); |
| 66 | + return makeAttemptResult(); |
| 67 | + }); |
| 68 | + |
| 69 | + await runEmbeddedAgent({ |
| 70 | + ...overflowBaseRunParams, |
| 71 | + runId: "run-local-trace-chain", |
| 72 | + sessionId: "session-local-trace-chain", |
| 73 | + sessionKey: "agent:main:local-trace-chain", |
| 74 | + provider: "anthropic", |
| 75 | + model: "claude-sonnet-4.6", |
| 76 | + }); |
| 77 | + |
| 78 | + expect(activeAttemptTrace?.traceId).toMatch(/^[0-9a-f]{32}$/); |
| 79 | + expect(activeAttemptTrace?.spanId).toMatch(/^[0-9a-f]{16}$/); |
| 80 | + expect(activeAttemptTrace?.parentSpanId).toMatch(/^[0-9a-f]{16}$/); |
| 81 | + }); |
| 82 | + |
| 83 | + it("captures the active gateway request trace for embedded runs", async () => { |
| 84 | + const parentTraceId = "4bf92f3577b34da6a3ce929d0e0e4736"; |
| 85 | + const parentSpanId = "00f067aa0ba902b7"; |
| 86 | + const requestTrace = createGatewayRequestDiagnosticTrace( |
| 87 | + requestWithTraceparent(`00-${parentTraceId}-${parentSpanId}-01`), |
| 88 | + { honorTraceparent: true }, |
| 89 | + ); |
| 90 | + let activeAttemptTrace: ReturnType<typeof traceContext.getActiveDiagnosticTraceContext>; |
| 91 | + mockedRunEmbeddedAttempt.mockImplementationOnce(async () => { |
| 92 | + activeAttemptTrace = traceContext.getActiveDiagnosticTraceContext(); |
| 93 | + return makeAttemptResult(); |
| 94 | + }); |
| 95 | + |
| 96 | + await traceContext.runWithDiagnosticTraceContext(requestTrace, () => |
| 97 | + runEmbeddedAgent({ |
| 98 | + ...overflowBaseRunParams, |
| 99 | + runId: "run-active-gateway-trace", |
| 100 | + sessionId: "session-active-gateway-trace", |
| 101 | + sessionKey: "agent:main:active-gateway-trace", |
| 102 | + provider: "anthropic", |
| 103 | + model: "claude-sonnet-4.6", |
| 104 | + messageChannel: "gateway-test", |
| 105 | + }), |
| 106 | + ); |
| 107 | + |
| 108 | + expect(requestTrace.traceId).toBe(parentTraceId); |
| 109 | + expect(requestTrace.parentSpanId).toBe(parentSpanId); |
| 110 | + expect(activeAttemptTrace?.traceId).toBe(parentTraceId); |
| 111 | + expect(activeAttemptTrace?.parentSpanId).toMatch(/^[0-9a-f]{16}$/); |
| 112 | + expect(activeAttemptTrace?.parentSpanId).not.toBe(requestTrace.spanId); |
| 113 | + expect(activeAttemptTrace?.spanId).toMatch(/^[0-9a-f]{16}$/); |
| 114 | + expect(activeAttemptTrace?.spanId).not.toBe(requestTrace.spanId); |
| 115 | + }); |
| 116 | +}); |
0 commit comments