Skip to content

InMemoryTraceStore

InMemoryTraceStore is a Map-based implementation of TraceStore for tests and local development. It has zero external dependencies and provides helper methods for test assertions.

Import

import { InMemoryTraceStore } from "@modernpath/agent-framework";

Constructor

new InMemoryTraceStore(config?: Partial<TraceStoreConfig>)
Parameter Type Default Description
config Partial<TraceStoreConfig> {} Optional configuration.
config.captureContent boolean true Store full prompts/responses in spans. Defaults to true for dev/test environments.
config.defaultTags string[] -- Tags automatically added to every new trace.

Methods

Implements all methods from the TraceStore interface, plus the following test helpers:

Test Helpers

Method Signature Description
getAllTraces getAllTraces(): Trace[] Returns all traces (unfiltered). Useful for assertions in tests.
getAllSpans getAllSpans(): Span[] Returns all spans across all traces. Useful for assertions in tests.
clear clear(): void Clears all traces, spans, sessions, and config snapshots. Call between test cases.

Code Example

import { InMemoryTraceStore } from "@modernpath/agent-framework";

// Create store for testing
const store = new InMemoryTraceStore({ captureContent: true });

// Start a trace
await store.startTrace({
  schemaVersion: 1,
  traceId: "trace-001",
  agentName: "TestAgent",
  agentVersion: "1.0.0",
  input: { prompt: "Test prompt", parameters: {} },
  status: "running",
  userId: 1,
  auditingId: 100,
  startedAt: new Date(),
  tags: ["test"],
});

// Add a span
await store.addSpan({
  schemaVersion: 1,
  spanId: "span-001",
  traceId: "trace-001",
  kind: "llm",
  name: "gemini.generateContent",
  status: "ok",
  startedAt: new Date(),
  durationMs: 250,
  input: { prompt: "Test prompt" },
  output: { text: "Response text" },
  genAiAttributes: {
    "gen_ai.request.model": "gemini-2.5-flash",
    "gen_ai.usage.input_tokens": 100,
    "gen_ai.usage.output_tokens": 50,
  },
});

// End the trace
await store.endTrace("trace-001", {
  output: { success: true, data: "Result" },
  status: "completed",
  completedAt: new Date(),
  durationMs: 300,
  tokens: { input: 100, output: 50, total: 150 },
});

// Test assertions
const traces = store.getAllTraces();
assert(traces.length === 1);
assert(traces[0].status === "completed");

const spans = store.getAllSpans();
assert(spans.length === 1);
assert(spans[0].kind === "llm");

// Clean up between tests
store.clear();

Vitest / Jest Example

import { describe, it, expect, beforeEach } from "vitest";
import { InMemoryTraceStore } from "@modernpath/agent-framework";

describe("MyAgent tracing", () => {
  let traceStore: InMemoryTraceStore;

  beforeEach(() => {
    traceStore = new InMemoryTraceStore({ captureContent: true });
  });

  it("should produce exactly 3 spans", async () => {
    const agent = createMyAgent({ traceStore });
    await agent.execute(testContext);

    const spans = traceStore.getAllSpans();
    expect(spans).toHaveLength(3);
    expect(spans.map((s) => s.kind)).toEqual(["llm", "tool", "llm"]);
  });

  it("should complete with success status", async () => {
    const agent = createMyAgent({ traceStore });
    await agent.execute(testContext);

    const traces = traceStore.getAllTraces();
    expect(traces[0].status).toBe("completed");
    expect(traces[0].output?.success).toBe(true);
  });
});