From 5fe06f3cdc97aaf38ddc1318ccae77d11374d82a Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 25 Apr 2026 12:34:12 -0700 Subject: [PATCH] test(diagnostics-otel): cover untrusted trace parent rejection --- .../diagnostics-otel/src/service.test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/extensions/diagnostics-otel/src/service.test.ts b/extensions/diagnostics-otel/src/service.test.ts index aed978cf933..da854175d2a 100644 --- a/extensions/diagnostics-otel/src/service.test.ts +++ b/extensions/diagnostics-otel/src/service.test.ts @@ -1449,6 +1449,65 @@ describe("diagnostics-otel service", () => { await service.stop?.(ctx); }); + test("does not parent untrusted diagnostic lifecycle spans from injected trace ids", async () => { + const service = createDiagnosticsOtelService(); + const ctx = createOtelContext(OTEL_TEST_ENDPOINT, { traces: true, metrics: true }); + await service.start(ctx); + + emitDiagnosticEvent({ + type: "run.completed", + runId: "run-1", + provider: "openai", + model: "gpt-5.4", + outcome: "completed", + durationMs: 100, + trace: { + traceId: TRACE_ID, + spanId: CHILD_SPAN_ID, + parentSpanId: SPAN_ID, + traceFlags: "01", + }, + }); + emitDiagnosticEvent({ + type: "model.call.completed", + runId: "run-1", + callId: "call-1", + provider: "openai", + model: "gpt-5.4", + durationMs: 80, + trace: { + traceId: TRACE_ID, + spanId: GRANDCHILD_SPAN_ID, + parentSpanId: CHILD_SPAN_ID, + traceFlags: "01", + }, + }); + emitDiagnosticEvent({ + type: "tool.execution.completed", + runId: "run-1", + toolName: "read", + durationMs: 20, + trace: { + traceId: TRACE_ID, + spanId: TOOL_SPAN_ID, + parentSpanId: GRANDCHILD_SPAN_ID, + traceFlags: "01", + }, + }); + await flushDiagnosticEvents(); + + expect(telemetryState.tracer.setSpanContext).not.toHaveBeenCalled(); + const parentBySpanName = Object.fromEntries( + telemetryState.tracer.startSpan.mock.calls.map((call) => [call[0], call[2]]), + ); + expect(parentBySpanName).toMatchObject({ + "openclaw.run": undefined, + "openclaw.model.call": undefined, + "openclaw.tool.execution": undefined, + }); + await service.stop?.(ctx); + }); + test("exports exec process spans without command text", async () => { const service = createDiagnosticsOtelService(); const ctx = createOtelContext(OTEL_TEST_ENDPOINT, { traces: true, metrics: true });