The Open Framework for Building Agents
test(e2e): check installed trace topology in agent-basic-runtime (#4461)
Chad Hietala53b7eb7c2b06
diff --git a/e2e/fixtures/agent-basic-runtime/agent/agent.ts b/e2e/fixtures/agent-basic-runtime/agent/agent.tsindex 35983af8c..51d3ef6d7 100644--- a/e2e/fixtures/agent-basic-runtime/agent/agent.ts+++ b/e2e/fixtures/agent-basic-runtime/agent/agent.ts@@ -22,6 +22,12 @@ const childModel = mockModel({const config = e2eAgentConfig({mock: ({ lastUserMessage, toolResults, userMessages }) => {+ if (lastUserMessage?.includes("inspect_trace")) {+ const result = toolResults.find((entry) => entry.name === "inspect_trace");+ return result === undefined+ ? { toolCalls: [{ name: "inspect_trace", input: {} }] }+ : JSON.stringify(result.output);+ }if (lastUserMessage?.includes("favorite word") && lastUserMessage.includes("?")) {const remembered = userMessages.map((message) => /My favorite word is (\w+)/u.exec(message)?.[1])diff --git a/e2e/fixtures/agent-basic-runtime/agent/instrumentation/trace-audit.ts b/e2e/fixtures/agent-basic-runtime/agent/instrumentation/trace-audit.tsnew file mode 100644index 000000000..0fbef2965--- /dev/null+++ b/e2e/fixtures/agent-basic-runtime/agent/instrumentation/trace-audit.ts@@ -0,0 +1,4 @@+import { otelIntegration } from "eve/instrumentation/otel";+import { traceAuditProcessor } from "../lib/trace-audit";++export default otelIntegration({ spanProcessors: [traceAuditProcessor] });diff --git a/e2e/fixtures/agent-basic-runtime/agent/lib/trace-audit.ts b/e2e/fixtures/agent-basic-runtime/agent/lib/trace-audit.tsnew file mode 100644index 000000000..e777d6d6c--- /dev/null+++ b/e2e/fixtures/agent-basic-runtime/agent/lib/trace-audit.ts@@ -0,0 +1,74 @@+import type { SpanProcessor } from "eve/instrumentation/otel";++interface AuditSpan {+ name: string;+ traceId: string;+ spanId: string;+ parentSpanId?: string;+ attributes: Readonly<Record<string, unknown>>;+}++// Instrumentation and tools load in separate bundles, so they share the buffer+// globally. Spans are kept per run so concurrent evals cannot evict each other's.+const runs = ((globalThis as Record<symbol, unknown>)[+ Symbol.for("agent-basic-runtime.trace-audit")+] ??= new Map()) as Map<string, AuditSpan[]>;++export const traceAuditProcessor: SpanProcessor = {+ onStart(value) {+ const span = value as {+ name?: string;+ attributes?: Readonly<Record<string, unknown>>;+ spanContext?: () => { traceId: string; spanId: string };+ parentSpanContext?: { spanId: string };+ };+ const runId = span.attributes?.["agent.run.id"];+ if (span.spanContext === undefined || span.name === undefined || typeof runId !== "string")+ return;+ const reference = span.spanContext();+ let spans = runs.get(runId);+ if (spans === undefined) {+ spans = [];+ runs.set(runId, spans);+ if (runs.size > 64) runs.delete(runs.keys().next().value!);+ }+ spans.push({+ name: span.name,+ ...reference,+ parentSpanId: span.parentSpanContext?.spanId,+ attributes: { ...span.attributes },+ });+ while (spans.length > 128) spans.shift();+ },+ onEnd() {},+ async forceFlush() {},+ async shutdown() {},+};++export function inspectTrace(runId: string) {+ const owned = runs.get(runId) ?? [];+ // The tool's own span is durable and starts only when the call completes, so+ // the audit reads the step that requested it and that step's model call.+ const modelOf = (step: AuditSpan) =>+ owned.find(+ (span) =>+ span.name.startsWith("chat ") &&+ span.parentSpanId === step.spanId &&+ span.traceId === step.traceId,+ );+ const step = [...owned]+ .reverse()+ .find((span) => span.name === "agent.step" && modelOf(span) !== undefined);+ const model = step === undefined ? undefined : modelOf(step);+ return {+ modelStep: step !== undefined && model !== undefined,+ identity:+ step !== undefined &&+ model !== undefined &&+ [step, model].every(+ (span) =>+ span.attributes["agent.trace.schema.version"] === 4 &&+ typeof span.attributes["gen_ai.conversation.id"] === "string",+ ),+ };+}diff --git a/e2e/fixtures/agent-basic-runtime/agent/tools/inspect_trace.ts b/e2e/fixtures/agent-basic-runtime/agent/tools/inspect_trace.tsnew file mode 100644index 000000000..5c8592320--- /dev/null+++ b/e2e/fixtures/agent-basic-runtime/agent/tools/inspect_trace.ts@@ -0,0 +1,26 @@+import { defineDynamic, defineTool } from "eve/tools";+import { z } from "zod";+import { inspectTrace } from "../lib/trace-audit";++export default defineDynamic({+ events: {+ "step.started": (_event, ctx) => {+ const requested = ctx.messages.some(+ (message) =>+ message.role === "user" &&+ (typeof message.content === "string"+ ? message.content+ : message.content.map((part) => (part.type === "text" ? part.text : "")).join(" ")+ ).includes("inspect_trace"),+ );+ if (!requested) return null;+ return defineTool({+ description: "Check the trace relationships for this diagnostic step.",+ inputSchema: z.object({}),+ async execute(_input, toolContext) {+ return inspectTrace(toolContext.session.id);+ },+ });+ },+ },+});diff --git a/e2e/fixtures/agent-basic-runtime/evals/runtime/trace-topology.eval.ts b/e2e/fixtures/agent-basic-runtime/evals/runtime/trace-topology.eval.tsnew file mode 100644index 000000000..c9d578151--- /dev/null+++ b/e2e/fixtures/agent-basic-runtime/evals/runtime/trace-topology.eval.ts@@ -0,0 +1,26 @@+import { defineEval } from "eve/evals";+import { satisfies } from "eve/evals/expect";++export default defineEval({+ description:+ "The installed tracing pipeline retains eve identity and model-step parent relationships.",+ async test(t) {+ const turn = await t.send(+ "Alice is checking the support agent's diagnostics. Call inspect_trace once and report its result.",+ );+ turn.expectOk();+ turn.calledTool("inspect_trace", { count: 1, status: "completed" });+ const result = turn.toolCalls.find((call) => call.name === "inspect_trace")?.output;+ await t.require(+ result,+ satisfies(+ (value: unknown) =>+ typeof value === "object" &&+ value !== null &&+ Reflect.get(value, "modelStep") === true &&+ Reflect.get(value, "identity") === true,+ "the model has its step parent and eve schema identity is preserved",+ ),+ );+ },+});