summaryrefslogtreecommitdiffhomepage
path: root/packages/trace-store/src
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-27 01:09:39 +0900
committerAdam Malczewski <[email protected]>2026-06-27 01:09:39 +0900
commit61e45e60d699ed1ca46f94a8f181c92a940317c6 (patch)
tree2892d9773c5a8e367e1e58cdb1e88d9c6ad3fe6d /packages/trace-store/src
parent63c7e64532e85e0bbdd6d9ac6825d8f86be98e7a (diff)
parent727c98c9dae516a2070eb950410314380a20c974 (diff)
downloaddispatch-61e45e60d699ed1ca46f94a8f181c92a940317c6.tar.gz
dispatch-61e45e60d699ed1ca46f94a8f181c92a940317c6.zip
Merge branch 'feature/indent-change' into dev
Diffstat (limited to 'packages/trace-store/src')
-rw-r--r--packages/trace-store/src/cli.ts10
-rw-r--r--packages/trace-store/src/easy-view.test.ts674
-rw-r--r--packages/trace-store/src/easy-view.ts344
-rw-r--r--packages/trace-store/src/store.test.ts1234
-rw-r--r--packages/trace-store/src/store.ts954
5 files changed, 1608 insertions, 1608 deletions
diff --git a/packages/trace-store/src/cli.ts b/packages/trace-store/src/cli.ts
index 9092c91..80a5c8b 100644
--- a/packages/trace-store/src/cli.ts
+++ b/packages/trace-store/src/cli.ts
@@ -2,15 +2,15 @@ import { createTraceStore } from "./store.js";
const turnId = process.argv[2];
if (turnId === undefined) {
- console.error("Usage: bun packages/trace-store/src/cli.ts <turnId> [dbPath]");
- process.exit(1);
+ console.error("Usage: bun packages/trace-store/src/cli.ts <turnId> [dbPath]");
+ process.exit(1);
}
const dbPath = process.argv[3] ?? process.env.TRACE_DB_PATH ?? "./.dispatch-data/traces.db";
const store = createTraceStore({ path: dbPath });
try {
- const output = store.easyView(turnId);
- console.log(output);
+ const output = store.easyView(turnId);
+ console.log(output);
} finally {
- store.close();
+ store.close();
}
diff --git a/packages/trace-store/src/easy-view.test.ts b/packages/trace-store/src/easy-view.test.ts
index 3ebc814..07e0316 100644
--- a/packages/trace-store/src/easy-view.test.ts
+++ b/packages/trace-store/src/easy-view.test.ts
@@ -3,358 +3,358 @@ import { describe, expect, it } from "vitest";
import { formatDuration, renderEasyView } from "./easy-view.js";
describe("renderEasyView", () => {
- it("returns empty string for empty records", () => {
- expect(renderEasyView([])).toBe("");
- });
+ it("returns empty string for empty records", () => {
+ expect(renderEasyView([])).toBe("");
+ });
- it("renders a single span with no children", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s1",
- name: "step",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "span-close",
- spanId: "s1",
- name: "step",
- timestamp: 2800,
- durationMs: 1800,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- expect(renderEasyView(records)).toBe("- step 1.8s");
- });
+ it("renders a single span with no children", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s1",
+ name: "step",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "span-close",
+ spanId: "s1",
+ name: "step",
+ timestamp: 2800,
+ durationMs: 1800,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ expect(renderEasyView(records)).toBe("- step 1.8s");
+ });
- it("renders a span with nested log records", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s1",
- name: "step",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "log",
- level: "info",
- msg: "thinking...",
- timestamp: 1200,
- extensionId: "ext",
- turnId: "t1",
- parentSpanId: "s1",
- },
- {
- kind: "span-close",
- spanId: "s1",
- name: "step",
- timestamp: 2800,
- durationMs: 1800,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- const expected = ["- step 1.8s", " - thinking..."].join("\n");
- expect(renderEasyView(records)).toBe(expected);
- });
+ it("renders a span with nested log records", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s1",
+ name: "step",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "log",
+ level: "info",
+ msg: "thinking...",
+ timestamp: 1200,
+ extensionId: "ext",
+ turnId: "t1",
+ parentSpanId: "s1",
+ },
+ {
+ kind: "span-close",
+ spanId: "s1",
+ name: "step",
+ timestamp: 2800,
+ durationMs: 1800,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ const expected = ["- step 1.8s", " - thinking..."].join("\n");
+ expect(renderEasyView(records)).toBe(expected);
+ });
- it("renders nested spans with indentation", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "parent",
- name: "step",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "span-open",
- spanId: "child",
- name: "tool.call",
- timestamp: 1100,
- extensionId: "ext",
- turnId: "t1",
- parentSpanId: "parent",
- },
- {
- kind: "log",
- level: "info",
- msg: "executing",
- timestamp: 1200,
- extensionId: "ext",
- turnId: "t1",
- parentSpanId: "child",
- },
- {
- kind: "span-close",
- spanId: "child",
- name: "tool.call",
- timestamp: 1500,
- durationMs: 400,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- parentSpanId: "parent",
- },
- {
- kind: "span-close",
- spanId: "parent",
- name: "step",
- timestamp: 2800,
- durationMs: 1800,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- const expected = ["- step 1.8s", " - tool.call 400ms", " - executing"].join("\n");
- expect(renderEasyView(records)).toBe(expected);
- });
+ it("renders nested spans with indentation", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "parent",
+ name: "step",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "span-open",
+ spanId: "child",
+ name: "tool.call",
+ timestamp: 1100,
+ extensionId: "ext",
+ turnId: "t1",
+ parentSpanId: "parent",
+ },
+ {
+ kind: "log",
+ level: "info",
+ msg: "executing",
+ timestamp: 1200,
+ extensionId: "ext",
+ turnId: "t1",
+ parentSpanId: "child",
+ },
+ {
+ kind: "span-close",
+ spanId: "child",
+ name: "tool.call",
+ timestamp: 1500,
+ durationMs: 400,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ parentSpanId: "parent",
+ },
+ {
+ kind: "span-close",
+ spanId: "parent",
+ name: "step",
+ timestamp: 2800,
+ durationMs: 1800,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ const expected = ["- step 1.8s", " - tool.call 400ms", " - executing"].join("\n");
+ expect(renderEasyView(records)).toBe(expected);
+ });
- it("renders a span-open without matching span-close as (open)", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s-open",
- name: "step",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- expect(renderEasyView(records)).toBe("- step (open)");
- });
+ it("renders a span-open without matching span-close as (open)", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s-open",
+ name: "step",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ expect(renderEasyView(records)).toBe("- step (open)");
+ });
- it("renders root-level log records without a parent span", () => {
- const records: LogRecord[] = [
- {
- kind: "log",
- level: "info",
- msg: "top-level",
- timestamp: 500,
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "log",
- level: "warn",
- msg: "a warning",
- timestamp: 600,
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- const expected = ["- top-level", "- [warn] a warning"].join("\n");
- expect(renderEasyView(records)).toBe(expected);
- });
+ it("renders root-level log records without a parent span", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "log",
+ level: "info",
+ msg: "top-level",
+ timestamp: 500,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "log",
+ level: "warn",
+ msg: "a warning",
+ timestamp: 600,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ const expected = ["- top-level", "- [warn] a warning"].join("\n");
+ expect(renderEasyView(records)).toBe(expected);
+ });
- it("skips span-close records without a matching span-open", () => {
- const records: LogRecord[] = [
- {
- kind: "span-close",
- spanId: "orphan",
- name: "orphan",
- timestamp: 1000,
- durationMs: 100,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "log",
- level: "info",
- msg: "real",
- timestamp: 2000,
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- expect(renderEasyView(records)).toBe("- real");
- });
+ it("skips span-close records without a matching span-open", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-close",
+ spanId: "orphan",
+ name: "orphan",
+ timestamp: 1000,
+ durationMs: 100,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "log",
+ level: "info",
+ msg: "real",
+ timestamp: 2000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ expect(renderEasyView(records)).toBe("- real");
+ });
- it("renders span with ERR status", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s1",
- name: "failing-step",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "span-close",
- spanId: "s1",
- name: "failing-step",
- timestamp: 1100,
- durationMs: 100,
- status: "error",
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- expect(renderEasyView(records)).toBe("- failing-step 100ms ERR");
- });
+ it("renders span with ERR status", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s1",
+ name: "failing-step",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "span-close",
+ spanId: "s1",
+ name: "failing-step",
+ timestamp: 1100,
+ durationMs: 100,
+ status: "error",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ expect(renderEasyView(records)).toBe("- failing-step 100ms ERR");
+ });
- it("renders body hint when body is present", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s1",
- name: "prompt",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: "x".repeat(2100),
- },
- {
- kind: "span-close",
- spanId: "s1",
- name: "prompt",
- timestamp: 1100,
- durationMs: 100,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- expect(renderEasyView(records)).toBe("- prompt 100ms [body 2.1k]");
- });
+ it("renders body hint when body is present", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s1",
+ name: "prompt",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "x".repeat(2100),
+ },
+ {
+ kind: "span-close",
+ spanId: "s1",
+ name: "prompt",
+ timestamp: 1100,
+ durationMs: 100,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ expect(renderEasyView(records)).toBe("- prompt 100ms [body 2.1k]");
+ });
- it("renders log record body hint", () => {
- const records: LogRecord[] = [
- {
- kind: "log",
- level: "debug",
- msg: "payload",
- timestamp: 500,
- extensionId: "ext",
- turnId: "t1",
- body: "abc",
- },
- ];
- expect(renderEasyView(records)).toBe("- [debug] payload [body 3b]");
- });
+ it("renders log record body hint", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "log",
+ level: "debug",
+ msg: "payload",
+ timestamp: 500,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "abc",
+ },
+ ];
+ expect(renderEasyView(records)).toBe("- [debug] payload [body 3b]");
+ });
- it("renders attributes on span (up to 3)", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s1",
- name: "provider.request",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- attributes: { model: "gpt-4", method: "POST", status: 200 },
- },
- {
- kind: "span-close",
- spanId: "s1",
- name: "provider.request",
- timestamp: 1500,
- durationMs: 500,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- const output = renderEasyView(records);
- expect(output).toContain("provider.request 500ms");
- expect(output).toContain('model="gpt-4"');
- expect(output).toContain('method="POST"');
- expect(output).toContain("status=200");
- });
+ it("renders attributes on span (up to 3)", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s1",
+ name: "provider.request",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ attributes: { model: "gpt-4", method: "POST", status: 200 },
+ },
+ {
+ kind: "span-close",
+ spanId: "s1",
+ name: "provider.request",
+ timestamp: 1500,
+ durationMs: 500,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ const output = renderEasyView(records);
+ expect(output).toContain("provider.request 500ms");
+ expect(output).toContain('model="gpt-4"');
+ expect(output).toContain('method="POST"');
+ expect(output).toContain("status=200");
+ });
- it("renders mixed records in correct order", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s1",
- name: "step",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "log",
- level: "info",
- msg: "inside step",
- timestamp: 1200,
- extensionId: "ext",
- turnId: "t1",
- parentSpanId: "s1",
- },
- {
- kind: "span-close",
- spanId: "s1",
- name: "step",
- timestamp: 2800,
- durationMs: 1800,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "log",
- level: "info",
- msg: "after step",
- timestamp: 3000,
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- const expected = ["- step 1.8s", " - inside step", "- after step"].join("\n");
- expect(renderEasyView(records)).toBe(expected);
- });
+ it("renders mixed records in correct order", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s1",
+ name: "step",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "log",
+ level: "info",
+ msg: "inside step",
+ timestamp: 1200,
+ extensionId: "ext",
+ turnId: "t1",
+ parentSpanId: "s1",
+ },
+ {
+ kind: "span-close",
+ spanId: "s1",
+ name: "step",
+ timestamp: 2800,
+ durationMs: 1800,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "log",
+ level: "info",
+ msg: "after step",
+ timestamp: 3000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ const expected = ["- step 1.8s", " - inside step", "- after step"].join("\n");
+ expect(renderEasyView(records)).toBe(expected);
+ });
- it("deterministic output for same input", () => {
- const records: LogRecord[] = [
- {
- kind: "span-open",
- spanId: "s1",
- name: "step",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- },
- {
- kind: "span-close",
- spanId: "s1",
- name: "step",
- timestamp: 1800,
- durationMs: 800,
- status: "ok",
- extensionId: "ext",
- turnId: "t1",
- },
- ];
- expect(renderEasyView(records)).toBe(renderEasyView(records));
- });
+ it("deterministic output for same input", () => {
+ const records: LogRecord[] = [
+ {
+ kind: "span-open",
+ spanId: "s1",
+ name: "step",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ {
+ kind: "span-close",
+ spanId: "s1",
+ name: "step",
+ timestamp: 1800,
+ durationMs: 800,
+ status: "ok",
+ extensionId: "ext",
+ turnId: "t1",
+ },
+ ];
+ expect(renderEasyView(records)).toBe(renderEasyView(records));
+ });
});
describe("formatDuration", () => {
- it("formats milliseconds under 1s", () => {
- expect(formatDuration(42)).toBe("42ms");
- expect(formatDuration(999)).toBe("999ms");
- });
+ it("formats milliseconds under 1s", () => {
+ expect(formatDuration(42)).toBe("42ms");
+ expect(formatDuration(999)).toBe("999ms");
+ });
- it("formats seconds", () => {
- expect(formatDuration(1000)).toBe("1.0s");
- expect(formatDuration(1500)).toBe("1.5s");
- expect(formatDuration(59999)).toBe("60.0s");
- });
+ it("formats seconds", () => {
+ expect(formatDuration(1000)).toBe("1.0s");
+ expect(formatDuration(1500)).toBe("1.5s");
+ expect(formatDuration(59999)).toBe("60.0s");
+ });
- it("formats minutes", () => {
- expect(formatDuration(60000)).toBe("1m0s");
- expect(formatDuration(90000)).toBe("1m30s");
- expect(formatDuration(125000)).toBe("2m5s");
- });
+ it("formats minutes", () => {
+ expect(formatDuration(60000)).toBe("1m0s");
+ expect(formatDuration(90000)).toBe("1m30s");
+ expect(formatDuration(125000)).toBe("2m5s");
+ });
});
diff --git a/packages/trace-store/src/easy-view.ts b/packages/trace-store/src/easy-view.ts
index 55477ee..cc32c9c 100644
--- a/packages/trace-store/src/easy-view.ts
+++ b/packages/trace-store/src/easy-view.ts
@@ -1,205 +1,205 @@
import type { Attributes, LogRecord } from "@dispatch/kernel";
interface SpanInfo {
- name: string;
- openTimestamp: number;
- closeTimestamp?: number;
- durationMs?: number;
- status?: string;
- body?: string;
- attributes?: Attributes;
- children: TimelineEntry[];
+ name: string;
+ openTimestamp: number;
+ closeTimestamp?: number;
+ durationMs?: number;
+ status?: string;
+ body?: string;
+ attributes?: Attributes;
+ children: TimelineEntry[];
}
interface LogEntry {
- record: LogRecord;
+ record: LogRecord;
}
type TimelineEntry = { type: "span"; span: SpanInfo } | { type: "log"; entry: LogEntry };
export function renderEasyView(records: readonly LogRecord[]): string {
- if (records.length === 0) {
- return "";
- }
-
- const sorted = [...records].sort((a, b) => a.timestamp - b.timestamp);
-
- const spansById = new Map<string, SpanInfo>();
- const roots: TimelineEntry[] = [];
- const childrenByParent = new Map<string | undefined, TimelineEntry[]>();
-
- for (const r of sorted) {
- if (r.kind === "span-open") {
- const span: SpanInfo = {
- name: r.name,
- openTimestamp: r.timestamp,
- ...(r.body !== undefined && { body: r.body }),
- ...(r.attributes !== undefined && { attributes: r.attributes }),
- children: [],
- };
- spansById.set(r.spanId, span);
- } else if (r.kind === "span-close") {
- const span = spansById.get(r.spanId);
- if (span !== undefined) {
- span.closeTimestamp = r.timestamp;
- span.durationMs = r.durationMs;
- span.status = r.status;
- if (r.body !== undefined) {
- span.body = r.body;
- }
- if (r.attributes !== undefined) {
- span.attributes = { ...span.attributes, ...r.attributes };
- }
- }
- } else {
- const entry: TimelineEntry = { type: "log", entry: { record: r } };
- const parentId = r.parentSpanId;
- let siblings = childrenByParent.get(parentId);
- if (siblings === undefined) {
- siblings = [];
- childrenByParent.set(parentId, siblings);
- }
- siblings.push(entry);
- }
- }
-
- for (const [spanId, span] of spansById) {
- const entry: TimelineEntry = { type: "span", span };
- const parentId = getSpanParentId(sorted, spanId);
- let siblings = childrenByParent.get(parentId);
- if (siblings === undefined) {
- siblings = [];
- childrenByParent.set(parentId, siblings);
- }
- siblings.push(entry);
- }
-
- for (const [parentId, children] of childrenByParent) {
- if (parentId === undefined) {
- roots.push(...children);
- } else {
- const parent = spansById.get(parentId);
- if (parent !== undefined) {
- parent.children.push(...children);
- } else {
- roots.push(...children);
- }
- }
- }
-
- sortByTimestamp(roots);
- for (const entry of roots) {
- if (entry.type === "span") {
- sortByTimestamp(entry.span.children);
- }
- }
-
- const lines: string[] = [];
- for (const entry of roots) {
- renderEntry(entry, 0, lines);
- }
- return lines.join("\n");
+ if (records.length === 0) {
+ return "";
+ }
+
+ const sorted = [...records].sort((a, b) => a.timestamp - b.timestamp);
+
+ const spansById = new Map<string, SpanInfo>();
+ const roots: TimelineEntry[] = [];
+ const childrenByParent = new Map<string | undefined, TimelineEntry[]>();
+
+ for (const r of sorted) {
+ if (r.kind === "span-open") {
+ const span: SpanInfo = {
+ name: r.name,
+ openTimestamp: r.timestamp,
+ ...(r.body !== undefined && { body: r.body }),
+ ...(r.attributes !== undefined && { attributes: r.attributes }),
+ children: [],
+ };
+ spansById.set(r.spanId, span);
+ } else if (r.kind === "span-close") {
+ const span = spansById.get(r.spanId);
+ if (span !== undefined) {
+ span.closeTimestamp = r.timestamp;
+ span.durationMs = r.durationMs;
+ span.status = r.status;
+ if (r.body !== undefined) {
+ span.body = r.body;
+ }
+ if (r.attributes !== undefined) {
+ span.attributes = { ...span.attributes, ...r.attributes };
+ }
+ }
+ } else {
+ const entry: TimelineEntry = { type: "log", entry: { record: r } };
+ const parentId = r.parentSpanId;
+ let siblings = childrenByParent.get(parentId);
+ if (siblings === undefined) {
+ siblings = [];
+ childrenByParent.set(parentId, siblings);
+ }
+ siblings.push(entry);
+ }
+ }
+
+ for (const [spanId, span] of spansById) {
+ const entry: TimelineEntry = { type: "span", span };
+ const parentId = getSpanParentId(sorted, spanId);
+ let siblings = childrenByParent.get(parentId);
+ if (siblings === undefined) {
+ siblings = [];
+ childrenByParent.set(parentId, siblings);
+ }
+ siblings.push(entry);
+ }
+
+ for (const [parentId, children] of childrenByParent) {
+ if (parentId === undefined) {
+ roots.push(...children);
+ } else {
+ const parent = spansById.get(parentId);
+ if (parent !== undefined) {
+ parent.children.push(...children);
+ } else {
+ roots.push(...children);
+ }
+ }
+ }
+
+ sortByTimestamp(roots);
+ for (const entry of roots) {
+ if (entry.type === "span") {
+ sortByTimestamp(entry.span.children);
+ }
+ }
+
+ const lines: string[] = [];
+ for (const entry of roots) {
+ renderEntry(entry, 0, lines);
+ }
+ return lines.join("\n");
}
function sortByTimestamp(entries: TimelineEntry[]): void {
- entries.sort((a, b) => {
- const tsA = a.type === "span" ? a.span.openTimestamp : a.entry.record.timestamp;
- const tsB = b.type === "span" ? b.span.openTimestamp : b.entry.record.timestamp;
- return tsA - tsB;
- });
+ entries.sort((a, b) => {
+ const tsA = a.type === "span" ? a.span.openTimestamp : a.entry.record.timestamp;
+ const tsB = b.type === "span" ? b.span.openTimestamp : b.entry.record.timestamp;
+ return tsA - tsB;
+ });
}
function getSpanParentId(records: readonly LogRecord[], spanId: string): string | undefined {
- for (const r of records) {
- if (r.kind === "span-open" && r.spanId === spanId) {
- return r.parentSpanId;
- }
- }
- return undefined;
+ for (const r of records) {
+ if (r.kind === "span-open" && r.spanId === spanId) {
+ return r.parentSpanId;
+ }
+ }
+ return undefined;
}
function renderEntry(entry: TimelineEntry, depth: number, lines: string[]): void {
- const indent = " ".repeat(depth);
-
- if (entry.type === "span") {
- const span = entry.span;
- const dur = span.durationMs !== undefined ? formatDuration(span.durationMs) : undefined;
- const statusStr = span.status === "error" ? " ERR" : "";
- const suffix = dur !== undefined ? ` ${dur}${statusStr}` : " (open)";
-
- let detail = "";
- if (span.attributes !== undefined) {
- const keys = Object.keys(span.attributes);
- if (keys.length > 0) {
- const parts: string[] = [];
- for (const k of keys.slice(0, 3)) {
- const v = span.attributes[k];
- if (v !== undefined) {
- parts.push(`${k}=${formatAttrValue(v)}`);
- }
- }
- detail = ` {${parts.join(", ")}}`;
- }
- }
-
- const bodyHint = span.body !== undefined ? ` [body ${formatSize(span.body.length)}]` : "";
-
- lines.push(`${indent}- ${span.name}${suffix}${detail}${bodyHint}`);
-
- for (const child of span.children) {
- renderEntry(child, depth + 1, lines);
- }
- } else {
- const r = entry.entry.record;
- if (r.kind === "log") {
- const lvl = levelTag(r.level);
- const bodyHint = r.body !== undefined ? ` [body ${formatSize(r.body.length)}]` : "";
- lines.push(`${indent}- ${lvl}${r.msg}${bodyHint}`);
- }
- }
+ const indent = " ".repeat(depth);
+
+ if (entry.type === "span") {
+ const span = entry.span;
+ const dur = span.durationMs !== undefined ? formatDuration(span.durationMs) : undefined;
+ const statusStr = span.status === "error" ? " ERR" : "";
+ const suffix = dur !== undefined ? ` ${dur}${statusStr}` : " (open)";
+
+ let detail = "";
+ if (span.attributes !== undefined) {
+ const keys = Object.keys(span.attributes);
+ if (keys.length > 0) {
+ const parts: string[] = [];
+ for (const k of keys.slice(0, 3)) {
+ const v = span.attributes[k];
+ if (v !== undefined) {
+ parts.push(`${k}=${formatAttrValue(v)}`);
+ }
+ }
+ detail = ` {${parts.join(", ")}}`;
+ }
+ }
+
+ const bodyHint = span.body !== undefined ? ` [body ${formatSize(span.body.length)}]` : "";
+
+ lines.push(`${indent}- ${span.name}${suffix}${detail}${bodyHint}`);
+
+ for (const child of span.children) {
+ renderEntry(child, depth + 1, lines);
+ }
+ } else {
+ const r = entry.entry.record;
+ if (r.kind === "log") {
+ const lvl = levelTag(r.level);
+ const bodyHint = r.body !== undefined ? ` [body ${formatSize(r.body.length)}]` : "";
+ lines.push(`${indent}- ${lvl}${r.msg}${bodyHint}`);
+ }
+ }
}
function levelTag(level: string): string {
- if (level === "info") {
- return "";
- }
- return `[${level}] `;
+ if (level === "info") {
+ return "";
+ }
+ return `[${level}] `;
}
export function formatDuration(ms: number): string {
- if (ms < 1000) {
- return `${ms}ms`;
- }
- const s = ms / 1000;
- if (s < 60) {
- return `${s.toFixed(1)}s`;
- }
- const m = Math.floor(s / 60);
- const rem = (s % 60).toFixed(0);
- return `${m}m${rem}s`;
+ if (ms < 1000) {
+ return `${ms}ms`;
+ }
+ const s = ms / 1000;
+ if (s < 60) {
+ return `${s.toFixed(1)}s`;
+ }
+ const m = Math.floor(s / 60);
+ const rem = (s % 60).toFixed(0);
+ return `${m}m${rem}s`;
}
function formatSize(n: number): string {
- if (n < 1024) {
- return `${n}b`;
- }
- const kb = n / 1024;
- if (kb < 1024) {
- return `${kb.toFixed(1)}k`;
- }
- const mb = kb / 1024;
- return `${mb.toFixed(1)}M`;
+ if (n < 1024) {
+ return `${n}b`;
+ }
+ const kb = n / 1024;
+ if (kb < 1024) {
+ return `${kb.toFixed(1)}k`;
+ }
+ const mb = kb / 1024;
+ return `${mb.toFixed(1)}M`;
}
function formatAttrValue(value: string | number | boolean | null): string {
- if (value === null) {
- return "null";
- }
- if (typeof value === "string") {
- if (value.length > 20) {
- return `"${value.slice(0, 17)}..."`;
- }
- return `"${value}"`;
- }
- return String(value);
+ if (value === null) {
+ return "null";
+ }
+ if (typeof value === "string") {
+ if (value.length > 20) {
+ return `"${value.slice(0, 17)}..."`;
+ }
+ return `"${value}"`;
+ }
+ return String(value);
}
diff --git a/packages/trace-store/src/store.test.ts b/packages/trace-store/src/store.test.ts
index 131d099..9b44084 100644
--- a/packages/trace-store/src/store.test.ts
+++ b/packages/trace-store/src/store.test.ts
@@ -5,516 +5,516 @@ import { describe, expect, it } from "vitest";
import { computeEvictions, createTraceStore, stableId } from "./store.js";
const logRecord: LogRecord = {
- kind: "log",
- level: "info",
- msg: "hello",
- timestamp: 1700000000000,
- extensionId: "test-ext",
- conversationId: "conv-1",
- turnId: "turn-1",
- spanId: "span-1",
- attributes: { key: "value" },
+ kind: "log",
+ level: "info",
+ msg: "hello",
+ timestamp: 1700000000000,
+ extensionId: "test-ext",
+ conversationId: "conv-1",
+ turnId: "turn-1",
+ spanId: "span-1",
+ attributes: { key: "value" },
};
const spanOpenRecord: LogRecord = {
- kind: "span-open",
- spanId: "span-2",
- name: "step",
- timestamp: 1700000000100,
- extensionId: "test-ext",
- conversationId: "conv-1",
- turnId: "turn-1",
- parentSpanId: "span-1",
+ kind: "span-open",
+ spanId: "span-2",
+ name: "step",
+ timestamp: 1700000000100,
+ extensionId: "test-ext",
+ conversationId: "conv-1",
+ turnId: "turn-1",
+ parentSpanId: "span-1",
};
const spanCloseRecord: LogRecord = {
- kind: "span-close",
- spanId: "span-2",
- name: "step",
- timestamp: 1700000000500,
- durationMs: 400,
- status: "ok",
- extensionId: "test-ext",
- conversationId: "conv-1",
- turnId: "turn-1",
- parentSpanId: "span-1",
+ kind: "span-close",
+ spanId: "span-2",
+ name: "step",
+ timestamp: 1700000000500,
+ durationMs: 400,
+ status: "ok",
+ extensionId: "test-ext",
+ conversationId: "conv-1",
+ turnId: "turn-1",
+ parentSpanId: "span-1",
};
const bodyRecord: LogRecord = {
- kind: "span-open",
- spanId: "span-3",
- name: "prompt",
- timestamp: 1700000000200,
- extensionId: "test-ext",
- conversationId: "conv-1",
- turnId: "turn-1",
- body: "the full prompt text",
+ kind: "span-open",
+ spanId: "span-3",
+ name: "prompt",
+ timestamp: 1700000000200,
+ extensionId: "test-ext",
+ conversationId: "conv-1",
+ turnId: "turn-1",
+ body: "the full prompt text",
};
const logRecordNoBody: LogRecord = {
- kind: "log",
- level: "debug",
- msg: "no body here",
- timestamp: 1700000000050,
- extensionId: "ext-min",
- turnId: "turn-1",
+ kind: "log",
+ level: "debug",
+ msg: "no body here",
+ timestamp: 1700000000050,
+ extensionId: "ext-min",
+ turnId: "turn-1",
};
describe("stableId", () => {
- it("produces a 16-char hex string", () => {
- const id = stableId(logRecord);
- expect(id).toMatch(/^[0-9a-f]{16}$/);
- });
-
- it("is deterministic for the same record", () => {
- expect(stableId(logRecord)).toBe(stableId(logRecord));
- });
-
- it("produces different ids for different records", () => {
- expect(stableId(logRecord)).not.toBe(stableId(spanOpenRecord));
- });
+ it("produces a 16-char hex string", () => {
+ const id = stableId(logRecord);
+ expect(id).toMatch(/^[0-9a-f]{16}$/);
+ });
+
+ it("is deterministic for the same record", () => {
+ expect(stableId(logRecord)).toBe(stableId(logRecord));
+ });
+
+ it("produces different ids for different records", () => {
+ expect(stableId(logRecord)).not.toBe(stableId(spanOpenRecord));
+ });
});
describe("createTraceStore", () => {
- function freshStore() {
- return createTraceStore({ path: ":memory:" });
- }
-
- it("inserts and retrieves records ordered by timestamp", () => {
- const store = freshStore();
- store.insertRecords([logRecord, spanCloseRecord, spanOpenRecord]);
- const result = store.getTurn("turn-1");
- expect(result).toHaveLength(3);
- expect(result[0]?.timestamp).toBe(1700000000000);
- expect(result[1]?.timestamp).toBe(1700000000100);
- expect(result[2]?.timestamp).toBe(1700000000500);
- store.close();
- });
-
- it("reconstructs attributes from JSON", () => {
- const store = freshStore();
- store.insertRecords([logRecord]);
- const result = store.getTurn("turn-1");
- expect(result[0]?.kind).toBe("log");
- if (result[0]?.kind === "log") {
- expect(result[0].attributes).toEqual({ key: "value" });
- }
- store.close();
- });
-
- it("reconstructs links from JSON", () => {
- const store = freshStore();
- const withLinks: LogRecord = {
- kind: "span-open",
- spanId: "span-link",
- name: "linked",
- timestamp: 1700000000999,
- extensionId: "ext",
- turnId: "turn-1",
- links: [{ spanId: "other-span", turnId: "turn-0", reason: "caused" }],
- };
- store.insertRecords([withLinks]);
- const result = store.getTurn("turn-1");
- expect(result[0]?.kind).toBe("span-open");
- if (result[0]?.kind === "span-open") {
- expect(result[0].links).toEqual([
- { spanId: "other-span", turnId: "turn-0", reason: "caused" },
- ]);
- }
- store.close();
- });
-
- it("returns empty array for unknown turnId", () => {
- const store = freshStore();
- store.insertRecords([logRecord]);
- expect(store.getTurn("nonexistent")).toEqual([]);
- store.close();
- });
-
- it("getBody returns body for body-bearing records", () => {
- const store = freshStore();
- store.insertRecords([bodyRecord]);
- const id = stableId(bodyRecord);
- expect(store.getBody(id)).toBe("the full prompt text");
- store.close();
- });
-
- it("getBody returns undefined for records without body", () => {
- const store = freshStore();
- store.insertRecords([logRecordNoBody]);
- const id = stableId(logRecordNoBody);
- expect(store.getBody(id)).toBeUndefined();
- store.close();
- });
-
- it("bodies table only holds body-bearing records", () => {
- const store = freshStore();
- store.insertRecords([logRecord, bodyRecord, logRecordNoBody]);
- const bodyId = stableId(bodyRecord);
- expect(store.getBody(bodyId)).toBe("the full prompt text");
-
- const logId = stableId(logRecord);
- expect(store.getBody(logId)).toBeUndefined();
-
- const noBodyId = stableId(logRecordNoBody);
- expect(store.getBody(noBodyId)).toBeUndefined();
- store.close();
- });
-
- it("is idempotent — re-inserting the same records produces no duplicates", () => {
- const store = freshStore();
- store.insertRecords([logRecord, spanOpenRecord, spanCloseRecord, bodyRecord]);
- store.insertRecords([logRecord, spanOpenRecord, spanCloseRecord, bodyRecord]);
- const result = store.getTurn("turn-1");
- expect(result).toHaveLength(4);
- store.close();
- });
-
- it("handles span-close record with attributes and links round-trip", () => {
- const store = freshStore();
- const closeWithMeta: LogRecord = {
- kind: "span-close",
- spanId: "span-m",
- name: "step",
- timestamp: 1700000001000,
- durationMs: 250,
- status: "error",
- extensionId: "ext",
- turnId: "turn-1",
- attributes: { httpStatus: 500 },
- links: [{ spanId: "upstream" }],
- };
- store.insertRecords([closeWithMeta]);
- const result = store.getTurn("turn-1");
- expect(result[0]?.kind).toBe("span-close");
- if (result[0]?.kind === "span-close") {
- expect(result[0].durationMs).toBe(250);
- expect(result[0].status).toBe("error");
- expect(result[0].attributes).toEqual({ httpStatus: 500 });
- expect(result[0].links).toEqual([{ spanId: "upstream" }]);
- }
- store.close();
- });
-
- it("easyView delegates to renderEasyView", () => {
- const store = freshStore();
- store.insertRecords([spanOpenRecord]);
- const output = store.easyView("turn-1");
- expect(output).toContain("step (open)");
- store.close();
- });
-
- it("persists to a file path", () => {
- const tmpPath = `/tmp/trace-store-test-${Date.now()}.db`;
- const store = createTraceStore({ path: tmpPath });
- store.insertRecords([logRecord]);
- store.close();
-
- const store2 = createTraceStore({ path: tmpPath });
- const result = store2.getTurn("turn-1");
- expect(result).toHaveLength(1);
- expect(result[0]?.kind).toBe("log");
- store2.close();
-
- try {
- unlinkSync(tmpPath);
- } catch {
- // ignore cleanup error
- }
- });
-
- it("body round-trips through getTurn", () => {
- const store = freshStore();
- store.insertRecords([bodyRecord]);
- const result = store.getTurn("turn-1");
- expect(result).toHaveLength(1);
- expect(result[0]?.kind).toBe("span-open");
- if (result[0]?.kind === "span-open") {
- expect(result[0].body).toBe("the full prompt text");
- }
- store.close();
- });
+ function freshStore() {
+ return createTraceStore({ path: ":memory:" });
+ }
+
+ it("inserts and retrieves records ordered by timestamp", () => {
+ const store = freshStore();
+ store.insertRecords([logRecord, spanCloseRecord, spanOpenRecord]);
+ const result = store.getTurn("turn-1");
+ expect(result).toHaveLength(3);
+ expect(result[0]?.timestamp).toBe(1700000000000);
+ expect(result[1]?.timestamp).toBe(1700000000100);
+ expect(result[2]?.timestamp).toBe(1700000000500);
+ store.close();
+ });
+
+ it("reconstructs attributes from JSON", () => {
+ const store = freshStore();
+ store.insertRecords([logRecord]);
+ const result = store.getTurn("turn-1");
+ expect(result[0]?.kind).toBe("log");
+ if (result[0]?.kind === "log") {
+ expect(result[0].attributes).toEqual({ key: "value" });
+ }
+ store.close();
+ });
+
+ it("reconstructs links from JSON", () => {
+ const store = freshStore();
+ const withLinks: LogRecord = {
+ kind: "span-open",
+ spanId: "span-link",
+ name: "linked",
+ timestamp: 1700000000999,
+ extensionId: "ext",
+ turnId: "turn-1",
+ links: [{ spanId: "other-span", turnId: "turn-0", reason: "caused" }],
+ };
+ store.insertRecords([withLinks]);
+ const result = store.getTurn("turn-1");
+ expect(result[0]?.kind).toBe("span-open");
+ if (result[0]?.kind === "span-open") {
+ expect(result[0].links).toEqual([
+ { spanId: "other-span", turnId: "turn-0", reason: "caused" },
+ ]);
+ }
+ store.close();
+ });
+
+ it("returns empty array for unknown turnId", () => {
+ const store = freshStore();
+ store.insertRecords([logRecord]);
+ expect(store.getTurn("nonexistent")).toEqual([]);
+ store.close();
+ });
+
+ it("getBody returns body for body-bearing records", () => {
+ const store = freshStore();
+ store.insertRecords([bodyRecord]);
+ const id = stableId(bodyRecord);
+ expect(store.getBody(id)).toBe("the full prompt text");
+ store.close();
+ });
+
+ it("getBody returns undefined for records without body", () => {
+ const store = freshStore();
+ store.insertRecords([logRecordNoBody]);
+ const id = stableId(logRecordNoBody);
+ expect(store.getBody(id)).toBeUndefined();
+ store.close();
+ });
+
+ it("bodies table only holds body-bearing records", () => {
+ const store = freshStore();
+ store.insertRecords([logRecord, bodyRecord, logRecordNoBody]);
+ const bodyId = stableId(bodyRecord);
+ expect(store.getBody(bodyId)).toBe("the full prompt text");
+
+ const logId = stableId(logRecord);
+ expect(store.getBody(logId)).toBeUndefined();
+
+ const noBodyId = stableId(logRecordNoBody);
+ expect(store.getBody(noBodyId)).toBeUndefined();
+ store.close();
+ });
+
+ it("is idempotent — re-inserting the same records produces no duplicates", () => {
+ const store = freshStore();
+ store.insertRecords([logRecord, spanOpenRecord, spanCloseRecord, bodyRecord]);
+ store.insertRecords([logRecord, spanOpenRecord, spanCloseRecord, bodyRecord]);
+ const result = store.getTurn("turn-1");
+ expect(result).toHaveLength(4);
+ store.close();
+ });
+
+ it("handles span-close record with attributes and links round-trip", () => {
+ const store = freshStore();
+ const closeWithMeta: LogRecord = {
+ kind: "span-close",
+ spanId: "span-m",
+ name: "step",
+ timestamp: 1700000001000,
+ durationMs: 250,
+ status: "error",
+ extensionId: "ext",
+ turnId: "turn-1",
+ attributes: { httpStatus: 500 },
+ links: [{ spanId: "upstream" }],
+ };
+ store.insertRecords([closeWithMeta]);
+ const result = store.getTurn("turn-1");
+ expect(result[0]?.kind).toBe("span-close");
+ if (result[0]?.kind === "span-close") {
+ expect(result[0].durationMs).toBe(250);
+ expect(result[0].status).toBe("error");
+ expect(result[0].attributes).toEqual({ httpStatus: 500 });
+ expect(result[0].links).toEqual([{ spanId: "upstream" }]);
+ }
+ store.close();
+ });
+
+ it("easyView delegates to renderEasyView", () => {
+ const store = freshStore();
+ store.insertRecords([spanOpenRecord]);
+ const output = store.easyView("turn-1");
+ expect(output).toContain("step (open)");
+ store.close();
+ });
+
+ it("persists to a file path", () => {
+ const tmpPath = `/tmp/trace-store-test-${Date.now()}.db`;
+ const store = createTraceStore({ path: tmpPath });
+ store.insertRecords([logRecord]);
+ store.close();
+
+ const store2 = createTraceStore({ path: tmpPath });
+ const result = store2.getTurn("turn-1");
+ expect(result).toHaveLength(1);
+ expect(result[0]?.kind).toBe("log");
+ store2.close();
+
+ try {
+ unlinkSync(tmpPath);
+ } catch {
+ // ignore cleanup error
+ }
+ });
+
+ it("body round-trips through getTurn", () => {
+ const store = freshStore();
+ store.insertRecords([bodyRecord]);
+ const result = store.getTurn("turn-1");
+ expect(result).toHaveLength(1);
+ expect(result[0]?.kind).toBe("span-open");
+ if (result[0]?.kind === "span-open") {
+ expect(result[0].body).toBe("the full prompt text");
+ }
+ store.close();
+ });
});
describe("content-addressed body storage", () => {
- function freshStore() {
- return createTraceStore({ path: ":memory:" });
- }
-
- it("content-addresses two identical bodies to a single stored body row", () => {
- const store = freshStore();
- const rec1: LogRecord = {
- kind: "span-open",
- spanId: "s1",
- name: "prompt",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: "identical body content",
- };
- const rec2: LogRecord = {
- kind: "span-open",
- spanId: "s2",
- name: "prompt",
- timestamp: 2000,
- extensionId: "ext",
- turnId: "t1",
- body: "identical body content",
- };
- store.insertRecords([rec1, rec2]);
-
- const id1 = stableId(rec1);
- const id2 = stableId(rec2);
- expect(store.getBody(id1)).toBe("identical body content");
- expect(store.getBody(id2)).toBe("identical body content");
- store.close();
- });
-
- it("stores distinct bodies separately", () => {
- const store = freshStore();
- const rec1: LogRecord = {
- kind: "span-open",
- spanId: "s1",
- name: "prompt",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: "body A",
- };
- const rec2: LogRecord = {
- kind: "span-open",
- spanId: "s2",
- name: "prompt",
- timestamp: 2000,
- extensionId: "ext",
- turnId: "t1",
- body: "body B",
- };
- store.insertRecords([rec1, rec2]);
-
- const id1 = stableId(rec1);
- const id2 = stableId(rec2);
- expect(store.getBody(id1)).toBe("body A");
- expect(store.getBody(id2)).toBe("body B");
- store.close();
- });
-
- it("compresses a body above the threshold and round-trips it on read", () => {
- const store = freshStore();
- const largeBody = "x".repeat(2048);
- const rec: LogRecord = {
- kind: "span-open",
- spanId: "s1",
- name: "prompt",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: largeBody,
- };
- store.insertRecords([rec]);
- const id = stableId(rec);
- expect(store.getBody(id)).toBe(largeBody);
- store.close();
- });
+ function freshStore() {
+ return createTraceStore({ path: ":memory:" });
+ }
+
+ it("content-addresses two identical bodies to a single stored body row", () => {
+ const store = freshStore();
+ const rec1: LogRecord = {
+ kind: "span-open",
+ spanId: "s1",
+ name: "prompt",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "identical body content",
+ };
+ const rec2: LogRecord = {
+ kind: "span-open",
+ spanId: "s2",
+ name: "prompt",
+ timestamp: 2000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "identical body content",
+ };
+ store.insertRecords([rec1, rec2]);
+
+ const id1 = stableId(rec1);
+ const id2 = stableId(rec2);
+ expect(store.getBody(id1)).toBe("identical body content");
+ expect(store.getBody(id2)).toBe("identical body content");
+ store.close();
+ });
+
+ it("stores distinct bodies separately", () => {
+ const store = freshStore();
+ const rec1: LogRecord = {
+ kind: "span-open",
+ spanId: "s1",
+ name: "prompt",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "body A",
+ };
+ const rec2: LogRecord = {
+ kind: "span-open",
+ spanId: "s2",
+ name: "prompt",
+ timestamp: 2000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "body B",
+ };
+ store.insertRecords([rec1, rec2]);
+
+ const id1 = stableId(rec1);
+ const id2 = stableId(rec2);
+ expect(store.getBody(id1)).toBe("body A");
+ expect(store.getBody(id2)).toBe("body B");
+ store.close();
+ });
+
+ it("compresses a body above the threshold and round-trips it on read", () => {
+ const store = freshStore();
+ const largeBody = "x".repeat(2048);
+ const rec: LogRecord = {
+ kind: "span-open",
+ spanId: "s1",
+ name: "prompt",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: largeBody,
+ };
+ store.insertRecords([rec]);
+ const id = stableId(rec);
+ expect(store.getBody(id)).toBe(largeBody);
+ store.close();
+ });
});
describe("prune", () => {
- function freshStore() {
- return createTraceStore({ path: ":memory:" });
- }
-
- it("prune by maxAgeMs deletes records and their bodies older than the cutoff", () => {
- const store = freshStore();
- const oldRec: LogRecord = {
- kind: "span-open",
- spanId: "s-old",
- name: "old-prompt",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: "old body content",
- };
- const newRec: LogRecord = {
- kind: "span-open",
- spanId: "s-new",
- name: "new-prompt",
- timestamp: Date.now(),
- extensionId: "ext",
- turnId: "t2",
- body: "new body content",
- };
- store.insertRecords([oldRec, newRec]);
-
- const summary = store.prune({ maxAgeMs: 60000 });
- expect(summary.recordsDeleted).toBe(1);
-
- const result = store.getTurn("t1");
- expect(result).toHaveLength(0);
- expect(store.getBody(stableId(oldRec))).toBeUndefined();
-
- const newResult = store.getTurn("t2");
- expect(newResult).toHaveLength(1);
- expect(store.getBody(stableId(newRec))).toBe("new body content");
- store.close();
- });
-
- it("prune by maxTotalBodyBytes evicts oldest bodies until under the cap", () => {
- const store = freshStore();
- const body1 = "a".repeat(300);
- const body2 = "b".repeat(300);
- const body3 = "c".repeat(300);
- const rec1: LogRecord = {
- kind: "span-open",
- spanId: "s1",
- name: "p",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: body1,
- };
- const rec2: LogRecord = {
- kind: "span-open",
- spanId: "s2",
- name: "p",
- timestamp: 2000,
- extensionId: "ext",
- turnId: "t2",
- body: body2,
- };
- const rec3: LogRecord = {
- kind: "span-open",
- spanId: "s3",
- name: "p",
- timestamp: 3000,
- extensionId: "ext",
- turnId: "t3",
- body: body3,
- };
- store.insertRecords([rec1, rec2, rec3]);
-
- const summary = store.prune({ maxTotalBodyBytes: 500 });
- expect(summary.bodiesDeleted).toBeGreaterThanOrEqual(1);
-
- expect(store.getBody(stableId(rec1))).toBeUndefined();
-
- const remaining = store.getTurn("t3");
- if (remaining.length > 0 && remaining[0]?.kind === "span-open") {
- expect(remaining[0].body).toBe(body3);
- }
- store.close();
- });
-
- it("prune garbage-collects an orphaned body with no referencing record", () => {
- const store = freshStore();
- const rec: LogRecord = {
- kind: "span-open",
- spanId: "s1",
- name: "p",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: "orphan body",
- };
- store.insertRecords([rec]);
-
- const summary = store.prune({ maxAgeMs: 60000 });
- expect(summary.recordsDeleted).toBe(1);
- expect(summary.bodiesDeleted).toBe(1);
- store.close();
- });
-
- it("prune keeps a still-referenced body when a duplicate referrer remains", () => {
- const store = freshStore();
- const sharedBody = "shared body content";
- const rec1: LogRecord = {
- kind: "span-open",
- spanId: "s1",
- name: "p",
- timestamp: 1000,
- extensionId: "ext",
- turnId: "t1",
- body: sharedBody,
- };
- const rec2: LogRecord = {
- kind: "span-open",
- spanId: "s2",
- name: "p",
- timestamp: Date.now(),
- extensionId: "ext",
- turnId: "t2",
- body: sharedBody,
- };
- store.insertRecords([rec1, rec2]);
-
- const summary = store.prune({ maxAgeMs: 60000 });
- expect(summary.recordsDeleted).toBe(1);
- expect(summary.bodiesDeleted).toBe(0);
-
- const id2 = stableId(rec2);
- expect(store.getBody(id2)).toBe(sharedBody);
- store.close();
- });
+ function freshStore() {
+ return createTraceStore({ path: ":memory:" });
+ }
+
+ it("prune by maxAgeMs deletes records and their bodies older than the cutoff", () => {
+ const store = freshStore();
+ const oldRec: LogRecord = {
+ kind: "span-open",
+ spanId: "s-old",
+ name: "old-prompt",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "old body content",
+ };
+ const newRec: LogRecord = {
+ kind: "span-open",
+ spanId: "s-new",
+ name: "new-prompt",
+ timestamp: Date.now(),
+ extensionId: "ext",
+ turnId: "t2",
+ body: "new body content",
+ };
+ store.insertRecords([oldRec, newRec]);
+
+ const summary = store.prune({ maxAgeMs: 60000 });
+ expect(summary.recordsDeleted).toBe(1);
+
+ const result = store.getTurn("t1");
+ expect(result).toHaveLength(0);
+ expect(store.getBody(stableId(oldRec))).toBeUndefined();
+
+ const newResult = store.getTurn("t2");
+ expect(newResult).toHaveLength(1);
+ expect(store.getBody(stableId(newRec))).toBe("new body content");
+ store.close();
+ });
+
+ it("prune by maxTotalBodyBytes evicts oldest bodies until under the cap", () => {
+ const store = freshStore();
+ const body1 = "a".repeat(300);
+ const body2 = "b".repeat(300);
+ const body3 = "c".repeat(300);
+ const rec1: LogRecord = {
+ kind: "span-open",
+ spanId: "s1",
+ name: "p",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: body1,
+ };
+ const rec2: LogRecord = {
+ kind: "span-open",
+ spanId: "s2",
+ name: "p",
+ timestamp: 2000,
+ extensionId: "ext",
+ turnId: "t2",
+ body: body2,
+ };
+ const rec3: LogRecord = {
+ kind: "span-open",
+ spanId: "s3",
+ name: "p",
+ timestamp: 3000,
+ extensionId: "ext",
+ turnId: "t3",
+ body: body3,
+ };
+ store.insertRecords([rec1, rec2, rec3]);
+
+ const summary = store.prune({ maxTotalBodyBytes: 500 });
+ expect(summary.bodiesDeleted).toBeGreaterThanOrEqual(1);
+
+ expect(store.getBody(stableId(rec1))).toBeUndefined();
+
+ const remaining = store.getTurn("t3");
+ if (remaining.length > 0 && remaining[0]?.kind === "span-open") {
+ expect(remaining[0].body).toBe(body3);
+ }
+ store.close();
+ });
+
+ it("prune garbage-collects an orphaned body with no referencing record", () => {
+ const store = freshStore();
+ const rec: LogRecord = {
+ kind: "span-open",
+ spanId: "s1",
+ name: "p",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: "orphan body",
+ };
+ store.insertRecords([rec]);
+
+ const summary = store.prune({ maxAgeMs: 60000 });
+ expect(summary.recordsDeleted).toBe(1);
+ expect(summary.bodiesDeleted).toBe(1);
+ store.close();
+ });
+
+ it("prune keeps a still-referenced body when a duplicate referrer remains", () => {
+ const store = freshStore();
+ const sharedBody = "shared body content";
+ const rec1: LogRecord = {
+ kind: "span-open",
+ spanId: "s1",
+ name: "p",
+ timestamp: 1000,
+ extensionId: "ext",
+ turnId: "t1",
+ body: sharedBody,
+ };
+ const rec2: LogRecord = {
+ kind: "span-open",
+ spanId: "s2",
+ name: "p",
+ timestamp: Date.now(),
+ extensionId: "ext",
+ turnId: "t2",
+ body: sharedBody,
+ };
+ store.insertRecords([rec1, rec2]);
+
+ const summary = store.prune({ maxAgeMs: 60000 });
+ expect(summary.recordsDeleted).toBe(1);
+ expect(summary.bodiesDeleted).toBe(0);
+
+ const id2 = stableId(rec2);
+ expect(store.getBody(id2)).toBe(sharedBody);
+ store.close();
+ });
});
describe("computeEvictions", () => {
- it("returns empty when under cap", () => {
- const bodies = [
- { hash: "a", storedSize: 100, oldestRecordTimestamp: 1000 },
- { hash: "b", storedSize: 200, oldestRecordTimestamp: 2000 },
- ];
- expect(computeEvictions(bodies, 500)).toEqual([]);
- });
-
- it("evicts oldest bodies until under cap", () => {
- const bodies = [
- { hash: "a", storedSize: 300, oldestRecordTimestamp: 1000 },
- { hash: "b", storedSize: 300, oldestRecordTimestamp: 2000 },
- { hash: "c", storedSize: 300, oldestRecordTimestamp: 3000 },
- ];
- const evicted = computeEvictions(bodies, 500);
- expect(evicted).toEqual(["a", "b"]);
- });
-
- it("evicts multiple oldest bodies", () => {
- const bodies = [
- { hash: "a", storedSize: 200, oldestRecordTimestamp: 1000 },
- { hash: "b", storedSize: 200, oldestRecordTimestamp: 2000 },
- { hash: "c", storedSize: 200, oldestRecordTimestamp: 3000 },
- ];
- const evicted = computeEvictions(bodies, 300);
- expect(evicted).toEqual(["a", "b"]);
- });
+ it("returns empty when under cap", () => {
+ const bodies = [
+ { hash: "a", storedSize: 100, oldestRecordTimestamp: 1000 },
+ { hash: "b", storedSize: 200, oldestRecordTimestamp: 2000 },
+ ];
+ expect(computeEvictions(bodies, 500)).toEqual([]);
+ });
+
+ it("evicts oldest bodies until under cap", () => {
+ const bodies = [
+ { hash: "a", storedSize: 300, oldestRecordTimestamp: 1000 },
+ { hash: "b", storedSize: 300, oldestRecordTimestamp: 2000 },
+ { hash: "c", storedSize: 300, oldestRecordTimestamp: 3000 },
+ ];
+ const evicted = computeEvictions(bodies, 500);
+ expect(evicted).toEqual(["a", "b"]);
+ });
+
+ it("evicts multiple oldest bodies", () => {
+ const bodies = [
+ { hash: "a", storedSize: 200, oldestRecordTimestamp: 1000 },
+ { hash: "b", storedSize: 200, oldestRecordTimestamp: 2000 },
+ { hash: "c", storedSize: 200, oldestRecordTimestamp: 3000 },
+ ];
+ const evicted = computeEvictions(bodies, 300);
+ expect(evicted).toEqual(["a", "b"]);
+ });
});
describe("old-schema migration", () => {
- function tmpPath(): string {
- return `/tmp/trace-store-migration-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`;
- }
-
- function cleanup(path: string): void {
- try {
- unlinkSync(path);
- } catch {
- // ignore
- }
- try {
- unlinkSync(`${path}-wal`);
- } catch {
- // ignore
- }
- try {
- unlinkSync(`${path}-shm`);
- } catch {
- // ignore
- }
- }
-
- it("migrates a pre-existing old-schema DB (records without bodyHash + bodies keyed by recordId) on open without error", () => {
- const path = tmpPath();
- try {
- const oldDb = new Database(path);
- oldDb.run("PRAGMA journal_mode = WAL");
- oldDb.run(`
+ function tmpPath(): string {
+ return `/tmp/trace-store-migration-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`;
+ }
+
+ function cleanup(path: string): void {
+ try {
+ unlinkSync(path);
+ } catch {
+ // ignore
+ }
+ try {
+ unlinkSync(`${path}-wal`);
+ } catch {
+ // ignore
+ }
+ try {
+ unlinkSync(`${path}-shm`);
+ } catch {
+ // ignore
+ }
+ }
+
+ it("migrates a pre-existing old-schema DB (records without bodyHash + bodies keyed by recordId) on open without error", () => {
+ const path = tmpPath();
+ try {
+ const oldDb = new Database(path);
+ oldDb.run("PRAGMA journal_mode = WAL");
+ oldDb.run(`
CREATE TABLE records (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
@@ -533,89 +533,89 @@ describe("old-schema migration", () => {
links TEXT
)
`);
- oldDb.run(`
+ oldDb.run(`
CREATE TABLE bodies (
recordId TEXT PRIMARY KEY REFERENCES records(id),
body TEXT NOT NULL
)
`);
- oldDb.run(
- `INSERT INTO records (id, kind, level, msg, name, spanId, parentSpanId, conversationId, turnId, extensionId, timestamp, durationMs, status, attributes, links)
+ oldDb.run(
+ `INSERT INTO records (id, kind, level, msg, name, spanId, parentSpanId, conversationId, turnId, extensionId, timestamp, durationMs, status, attributes, links)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
- [
- "rec-1",
- "span-open",
- null,
- null,
- "prompt",
- "s1",
- null,
- "conv-1",
- "t1",
- "ext",
- 1000,
- null,
- null,
- null,
- null,
- ],
- );
- oldDb.run(
- `INSERT INTO records (id, kind, level, msg, name, spanId, parentSpanId, conversationId, turnId, extensionId, timestamp, durationMs, status, attributes, links)
+ [
+ "rec-1",
+ "span-open",
+ null,
+ null,
+ "prompt",
+ "s1",
+ null,
+ "conv-1",
+ "t1",
+ "ext",
+ 1000,
+ null,
+ null,
+ null,
+ null,
+ ],
+ );
+ oldDb.run(
+ `INSERT INTO records (id, kind, level, msg, name, spanId, parentSpanId, conversationId, turnId, extensionId, timestamp, durationMs, status, attributes, links)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
- [
- "rec-2",
- "span-open",
- null,
- null,
- "prompt",
- "s2",
- null,
- "conv-1",
- "t1",
- "ext",
- 2000,
- null,
- null,
- null,
- null,
- ],
- );
-
- const sharedBody = "identical body content for migration test";
- oldDb.run("INSERT INTO bodies (recordId, body) VALUES (?, ?)", ["rec-1", sharedBody]);
- oldDb.run("INSERT INTO bodies (recordId, body) VALUES (?, ?)", ["rec-2", sharedBody]);
-
- oldDb.close();
-
- const store = createTraceStore({ path });
-
- const turn = store.getTurn("t1");
- expect(turn).toHaveLength(2);
- expect(turn[0]?.kind).toBe("span-open");
- expect(turn[1]?.kind).toBe("span-open");
-
- expect(store.getBody("rec-1")).toBe(sharedBody);
- expect(store.getBody("rec-2")).toBe(sharedBody);
-
- const db = new Database(path);
- const bodyRows = db.query("SELECT hash FROM bodies").all() as Array<{ hash: string }>;
- expect(bodyRows).toHaveLength(1);
- db.close();
-
- store.close();
- } finally {
- cleanup(path);
- }
- });
-
- it("re-opening an already-migrated DB is a no-op (no error, no double-migrate)", () => {
- const path = tmpPath();
- try {
- const oldDb = new Database(path);
- oldDb.run("PRAGMA journal_mode = WAL");
- oldDb.run(`
+ [
+ "rec-2",
+ "span-open",
+ null,
+ null,
+ "prompt",
+ "s2",
+ null,
+ "conv-1",
+ "t1",
+ "ext",
+ 2000,
+ null,
+ null,
+ null,
+ null,
+ ],
+ );
+
+ const sharedBody = "identical body content for migration test";
+ oldDb.run("INSERT INTO bodies (recordId, body) VALUES (?, ?)", ["rec-1", sharedBody]);
+ oldDb.run("INSERT INTO bodies (recordId, body) VALUES (?, ?)", ["rec-2", sharedBody]);
+
+ oldDb.close();
+
+ const store = createTraceStore({ path });
+
+ const turn = store.getTurn("t1");
+ expect(turn).toHaveLength(2);
+ expect(turn[0]?.kind).toBe("span-open");
+ expect(turn[1]?.kind).toBe("span-open");
+
+ expect(store.getBody("rec-1")).toBe(sharedBody);
+ expect(store.getBody("rec-2")).toBe(sharedBody);
+
+ const db = new Database(path);
+ const bodyRows = db.query("SELECT hash FROM bodies").all() as Array<{ hash: string }>;
+ expect(bodyRows).toHaveLength(1);
+ db.close();
+
+ store.close();
+ } finally {
+ cleanup(path);
+ }
+ });
+
+ it("re-opening an already-migrated DB is a no-op (no error, no double-migrate)", () => {
+ const path = tmpPath();
+ try {
+ const oldDb = new Database(path);
+ oldDb.run("PRAGMA journal_mode = WAL");
+ oldDb.run(`
CREATE TABLE records (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
@@ -634,58 +634,58 @@ describe("old-schema migration", () => {
links TEXT
)
`);
- oldDb.run(`
+ oldDb.run(`
CREATE TABLE bodies (
recordId TEXT PRIMARY KEY REFERENCES records(id),
body TEXT NOT NULL
)
`);
- oldDb.run(
- `INSERT INTO records (id, kind, level, msg, name, spanId, parentSpanId, conversationId, turnId, extensionId, timestamp, durationMs, status, attributes, links)
+ oldDb.run(
+ `INSERT INTO records (id, kind, level, msg, name, spanId, parentSpanId, conversationId, turnId, extensionId, timestamp, durationMs, status, attributes, links)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
- [
- "rec-1",
- "span-open",
- null,
- null,
- "prompt",
- "s1",
- null,
- "conv-1",
- "t1",
- "ext",
- 1000,
- null,
- null,
- null,
- null,
- ],
- );
- oldDb.run("INSERT INTO bodies (recordId, body) VALUES (?, ?)", ["rec-1", "some body"]);
- oldDb.close();
-
- const store1 = createTraceStore({ path });
- expect(store1.getBody("rec-1")).toBe("some body");
- store1.close();
-
- const store2 = createTraceStore({ path });
- expect(store2.getBody("rec-1")).toBe("some body");
-
- const turn = store2.getTurn("t1");
- expect(turn).toHaveLength(1);
-
- store2.close();
- } finally {
- cleanup(path);
- }
- });
-
- it("idx_records_bodyHash exists after migration", () => {
- const path = tmpPath();
- try {
- const oldDb = new Database(path);
- oldDb.run("PRAGMA journal_mode = WAL");
- oldDb.run(`
+ [
+ "rec-1",
+ "span-open",
+ null,
+ null,
+ "prompt",
+ "s1",
+ null,
+ "conv-1",
+ "t1",
+ "ext",
+ 1000,
+ null,
+ null,
+ null,
+ null,
+ ],
+ );
+ oldDb.run("INSERT INTO bodies (recordId, body) VALUES (?, ?)", ["rec-1", "some body"]);
+ oldDb.close();
+
+ const store1 = createTraceStore({ path });
+ expect(store1.getBody("rec-1")).toBe("some body");
+ store1.close();
+
+ const store2 = createTraceStore({ path });
+ expect(store2.getBody("rec-1")).toBe("some body");
+
+ const turn = store2.getTurn("t1");
+ expect(turn).toHaveLength(1);
+
+ store2.close();
+ } finally {
+ cleanup(path);
+ }
+ });
+
+ it("idx_records_bodyHash exists after migration", () => {
+ const path = tmpPath();
+ try {
+ const oldDb = new Database(path);
+ oldDb.run("PRAGMA journal_mode = WAL");
+ oldDb.run(`
CREATE TABLE records (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
@@ -704,25 +704,25 @@ describe("old-schema migration", () => {
links TEXT
)
`);
- oldDb.run(`
+ oldDb.run(`
CREATE TABLE bodies (
recordId TEXT PRIMARY KEY REFERENCES records(id),
body TEXT NOT NULL
)
`);
- oldDb.close();
-
- const store = createTraceStore({ path });
- store.close();
-
- const db = new Database(path);
- const indexes = db
- .query("SELECT name FROM sqlite_master WHERE type='index' AND name='idx_records_bodyHash'")
- .all() as Array<{ name: string }>;
- expect(indexes).toHaveLength(1);
- db.close();
- } finally {
- cleanup(path);
- }
- });
+ oldDb.close();
+
+ const store = createTraceStore({ path });
+ store.close();
+
+ const db = new Database(path);
+ const indexes = db
+ .query("SELECT name FROM sqlite_master WHERE type='index' AND name='idx_records_bodyHash'")
+ .all() as Array<{ name: string }>;
+ expect(indexes).toHaveLength(1);
+ db.close();
+ } finally {
+ cleanup(path);
+ }
+ });
});
diff --git a/packages/trace-store/src/store.ts b/packages/trace-store/src/store.ts
index f9564bb..2a9724f 100644
--- a/packages/trace-store/src/store.ts
+++ b/packages/trace-store/src/store.ts
@@ -6,58 +6,58 @@ import { renderEasyView } from "./easy-view.js";
const COMPRESS_THRESHOLD_BYTES = 1024;
export interface RetentionPolicy {
- readonly maxAgeMs?: number;
- readonly maxTotalBodyBytes?: number;
+ readonly maxAgeMs?: number;
+ readonly maxTotalBodyBytes?: number;
}
export const DEFAULT_RETENTION: Required<RetentionPolicy> = {
- maxAgeMs: 7 * 24 * 60 * 60 * 1000,
- maxTotalBodyBytes: 256 * 1024 * 1024,
+ maxAgeMs: 7 * 24 * 60 * 60 * 1000,
+ maxTotalBodyBytes: 256 * 1024 * 1024,
};
export interface PruneSummary {
- recordsDeleted: number;
- bodiesDeleted: number;
- bytesReclaimed: number;
+ recordsDeleted: number;
+ bodiesDeleted: number;
+ bytesReclaimed: number;
}
export interface TraceStore {
- insertRecords(records: readonly LogRecord[]): void;
- getTurn(turnId: string): LogRecord[];
- getBody(recordId: string): string | undefined;
- easyView(turnId: string): string;
- prune(policy: RetentionPolicy): PruneSummary;
- close(): void;
+ insertRecords(records: readonly LogRecord[]): void;
+ getTurn(turnId: string): LogRecord[];
+ getBody(recordId: string): string | undefined;
+ easyView(turnId: string): string;
+ prune(policy: RetentionPolicy): PruneSummary;
+ close(): void;
}
export function createTraceStore(opts: { path: string }): TraceStore {
- const db = new Database(opts.path);
- db.run("PRAGMA journal_mode = WAL");
- schema(db);
- return {
- insertRecords(records) {
- insertRecords(db, records);
- },
- getTurn(turnId) {
- return getTurn(db, turnId);
- },
- getBody(recordId) {
- return getBody(db, recordId);
- },
- easyView(turnId) {
- return renderEasyView(getTurn(db, turnId));
- },
- prune(policy) {
- return prune(db, policy);
- },
- close() {
- db.close();
- },
- };
+ const db = new Database(opts.path);
+ db.run("PRAGMA journal_mode = WAL");
+ schema(db);
+ return {
+ insertRecords(records) {
+ insertRecords(db, records);
+ },
+ getTurn(turnId) {
+ return getTurn(db, turnId);
+ },
+ getBody(recordId) {
+ return getBody(db, recordId);
+ },
+ easyView(turnId) {
+ return renderEasyView(getTurn(db, turnId));
+ },
+ prune(policy) {
+ return prune(db, policy);
+ },
+ close() {
+ db.close();
+ },
+ };
}
function schema(db: Database): void {
- db.run(`
+ db.run(`
CREATE TABLE IF NOT EXISTS records (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
@@ -77,15 +77,15 @@ function schema(db: Database): void {
bodyHash TEXT
)
`);
- db.run("CREATE INDEX IF NOT EXISTS idx_records_turnId ON records(turnId)");
- db.run("CREATE INDEX IF NOT EXISTS idx_records_conversationId ON records(conversationId)");
- db.run("CREATE INDEX IF NOT EXISTS idx_records_spanId ON records(spanId)");
- db.run("CREATE INDEX IF NOT EXISTS idx_records_kind ON records(kind)");
- db.run("CREATE INDEX IF NOT EXISTS idx_records_timestamp ON records(timestamp)");
+ db.run("CREATE INDEX IF NOT EXISTS idx_records_turnId ON records(turnId)");
+ db.run("CREATE INDEX IF NOT EXISTS idx_records_conversationId ON records(conversationId)");
+ db.run("CREATE INDEX IF NOT EXISTS idx_records_spanId ON records(spanId)");
+ db.run("CREATE INDEX IF NOT EXISTS idx_records_kind ON records(kind)");
+ db.run("CREATE INDEX IF NOT EXISTS idx_records_timestamp ON records(timestamp)");
- migrateOldBodies(db);
+ migrateOldBodies(db);
- db.run(`
+ db.run(`
CREATE TABLE IF NOT EXISTS bodies (
hash TEXT PRIMARY KEY,
body BLOB NOT NULL,
@@ -95,33 +95,33 @@ function schema(db: Database): void {
)
`);
- db.run("CREATE INDEX IF NOT EXISTS idx_records_bodyHash ON records(bodyHash)");
+ db.run("CREATE INDEX IF NOT EXISTS idx_records_bodyHash ON records(bodyHash)");
}
function migrateOldBodies(db: Database): void {
- const hasOldTable = db
- .query("SELECT name FROM sqlite_master WHERE type='table' AND name='bodies_old'")
- .get() as { name: string } | null;
- if (hasOldTable !== null) {
- return;
- }
-
- const cols = db.query("PRAGMA table_info(bodies)").all() as Array<{
- name: string;
- }>;
- const hasRecordId = cols.some((c) => c.name === "recordId");
- if (!hasRecordId) {
- return;
- }
-
- const oldRows = db.query("SELECT recordId, body FROM bodies").all() as Array<{
- recordId: string;
- body: string;
- }>;
-
- db.run("ALTER TABLE bodies RENAME TO bodies_old");
-
- db.run(`
+ const hasOldTable = db
+ .query("SELECT name FROM sqlite_master WHERE type='table' AND name='bodies_old'")
+ .get() as { name: string } | null;
+ if (hasOldTable !== null) {
+ return;
+ }
+
+ const cols = db.query("PRAGMA table_info(bodies)").all() as Array<{
+ name: string;
+ }>;
+ const hasRecordId = cols.some((c) => c.name === "recordId");
+ if (!hasRecordId) {
+ return;
+ }
+
+ const oldRows = db.query("SELECT recordId, body FROM bodies").all() as Array<{
+ recordId: string;
+ body: string;
+ }>;
+
+ db.run("ALTER TABLE bodies RENAME TO bodies_old");
+
+ db.run(`
CREATE TABLE IF NOT EXISTS bodies (
hash TEXT PRIMARY KEY,
body BLOB NOT NULL,
@@ -131,195 +131,195 @@ function migrateOldBodies(db: Database): void {
)
`);
- const hasBodyHash = cols.some((c) => c.name === "bodyHash");
- if (!hasBodyHash) {
- db.run("ALTER TABLE records ADD COLUMN bodyHash TEXT");
- }
+ const hasBodyHash = cols.some((c) => c.name === "bodyHash");
+ if (!hasBodyHash) {
+ db.run("ALTER TABLE records ADD COLUMN bodyHash TEXT");
+ }
- const upsertBody = db.prepare(`
+ const upsertBody = db.prepare(`
INSERT OR IGNORE INTO bodies (hash, body, isCompressed, originalSize, storedSize)
VALUES (?, ?, 0, ?, ?)
`);
- const updateRecord = db.prepare("UPDATE records SET bodyHash = ? WHERE id = ?");
-
- const migrateTxn = db.transaction(() => {
- for (const row of oldRows) {
- const hash = contentHash(row.body);
- const bodyBytes = new TextEncoder().encode(row.body);
- upsertBody.run(hash, bodyBytes, bodyBytes.length, bodyBytes.length);
- updateRecord.run(hash, row.recordId);
- }
- db.run("DROP TABLE bodies_old");
- });
- migrateTxn();
+ const updateRecord = db.prepare("UPDATE records SET bodyHash = ? WHERE id = ?");
+
+ const migrateTxn = db.transaction(() => {
+ for (const row of oldRows) {
+ const hash = contentHash(row.body);
+ const bodyBytes = new TextEncoder().encode(row.body);
+ upsertBody.run(hash, bodyBytes, bodyBytes.length, bodyBytes.length);
+ updateRecord.run(hash, row.recordId);
+ }
+ db.run("DROP TABLE bodies_old");
+ });
+ migrateTxn();
}
function sha256Hex(input: string): string {
- const data = new TextEncoder().encode(input);
- let h0 = 0x6a09e667;
- let h1 = 0xbb67ae85;
- let h2 = 0x3c6ef372;
- let h3 = 0xa54ff53a;
- let h4 = 0x510e527f;
- let h5 = 0x9b05688c;
- let h6 = 0x1f83d9ab;
- let h7 = 0x5be0cd19;
-
- const msgLen = data.length;
- const bitLen = msgLen * 8;
- const withOne = msgLen + 1;
- const paddedLen = withOne + ((96 - (withOne % 64)) % 64) + 8;
- const padded = new Uint8Array(paddedLen);
- padded.set(data);
- padded[msgLen] = 0x80;
- padded[paddedLen - 8] = (bitLen / 0x100000000) >>> 0;
- padded[paddedLen - 4] = bitLen >>> 0;
-
- const kArr = new Uint32Array(K);
-
- for (let offset = 0; offset < paddedLen; offset += 64) {
- const w = new Uint32Array(64);
- for (let i = 0; i < 16; i++) {
- const o = offset + i * 4;
- const b0 = padded[o] ?? 0;
- const b1 = padded[o + 1] ?? 0;
- const b2 = padded[o + 2] ?? 0;
- const b3 = padded[o + 3] ?? 0;
- w[i] = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
- }
- for (let i = 16; i < 64; i++) {
- const prev15 = w[i - 15] ?? 0;
- const prev2 = w[i - 2] ?? 0;
- const prev16 = w[i - 16] ?? 0;
- const prev7 = w[i - 7] ?? 0;
- const s0 = rightRotate(prev15, 7) ^ rightRotate(prev15, 18) ^ (prev15 >>> 3);
- const s1 = rightRotate(prev2, 17) ^ rightRotate(prev2, 19) ^ (prev2 >>> 10);
- w[i] = (prev16 + s0 + prev7 + s1) | 0;
- }
-
- let a = h0;
- let b = h1;
- let c = h2;
- let d = h3;
- let e = h4;
- let f = h5;
- let g = h6;
- let h = h7;
-
- for (let i = 0; i < 64; i++) {
- const S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
- const ch = (e & f) ^ (~e & g);
- const ki = kArr[i] ?? 0;
- const wi = w[i] ?? 0;
- const temp1 = (h + S1 + ch + ki + wi) | 0;
- const S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
- const maj = (a & b) ^ (a & c) ^ (b & c);
- const temp2 = (S0 + maj) | 0;
-
- h = g;
- g = f;
- f = e;
- e = (d + temp1) | 0;
- d = c;
- c = b;
- b = a;
- a = (temp1 + temp2) | 0;
- }
-
- h0 = (h0 + a) | 0;
- h1 = (h1 + b) | 0;
- h2 = (h2 + c) | 0;
- h3 = (h3 + d) | 0;
- h4 = (h4 + e) | 0;
- h5 = (h5 + f) | 0;
- h6 = (h6 + g) | 0;
- h7 = (h7 + h) | 0;
- }
-
- return (
- toHex32(h0) +
- toHex32(h1) +
- toHex32(h2) +
- toHex32(h3) +
- toHex32(h4) +
- toHex32(h5) +
- toHex32(h6) +
- toHex32(h7)
- );
+ const data = new TextEncoder().encode(input);
+ let h0 = 0x6a09e667;
+ let h1 = 0xbb67ae85;
+ let h2 = 0x3c6ef372;
+ let h3 = 0xa54ff53a;
+ let h4 = 0x510e527f;
+ let h5 = 0x9b05688c;
+ let h6 = 0x1f83d9ab;
+ let h7 = 0x5be0cd19;
+
+ const msgLen = data.length;
+ const bitLen = msgLen * 8;
+ const withOne = msgLen + 1;
+ const paddedLen = withOne + ((96 - (withOne % 64)) % 64) + 8;
+ const padded = new Uint8Array(paddedLen);
+ padded.set(data);
+ padded[msgLen] = 0x80;
+ padded[paddedLen - 8] = (bitLen / 0x100000000) >>> 0;
+ padded[paddedLen - 4] = bitLen >>> 0;
+
+ const kArr = new Uint32Array(K);
+
+ for (let offset = 0; offset < paddedLen; offset += 64) {
+ const w = new Uint32Array(64);
+ for (let i = 0; i < 16; i++) {
+ const o = offset + i * 4;
+ const b0 = padded[o] ?? 0;
+ const b1 = padded[o + 1] ?? 0;
+ const b2 = padded[o + 2] ?? 0;
+ const b3 = padded[o + 3] ?? 0;
+ w[i] = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
+ }
+ for (let i = 16; i < 64; i++) {
+ const prev15 = w[i - 15] ?? 0;
+ const prev2 = w[i - 2] ?? 0;
+ const prev16 = w[i - 16] ?? 0;
+ const prev7 = w[i - 7] ?? 0;
+ const s0 = rightRotate(prev15, 7) ^ rightRotate(prev15, 18) ^ (prev15 >>> 3);
+ const s1 = rightRotate(prev2, 17) ^ rightRotate(prev2, 19) ^ (prev2 >>> 10);
+ w[i] = (prev16 + s0 + prev7 + s1) | 0;
+ }
+
+ let a = h0;
+ let b = h1;
+ let c = h2;
+ let d = h3;
+ let e = h4;
+ let f = h5;
+ let g = h6;
+ let h = h7;
+
+ for (let i = 0; i < 64; i++) {
+ const S1 = rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
+ const ch = (e & f) ^ (~e & g);
+ const ki = kArr[i] ?? 0;
+ const wi = w[i] ?? 0;
+ const temp1 = (h + S1 + ch + ki + wi) | 0;
+ const S0 = rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
+ const maj = (a & b) ^ (a & c) ^ (b & c);
+ const temp2 = (S0 + maj) | 0;
+
+ h = g;
+ g = f;
+ f = e;
+ e = (d + temp1) | 0;
+ d = c;
+ c = b;
+ b = a;
+ a = (temp1 + temp2) | 0;
+ }
+
+ h0 = (h0 + a) | 0;
+ h1 = (h1 + b) | 0;
+ h2 = (h2 + c) | 0;
+ h3 = (h3 + d) | 0;
+ h4 = (h4 + e) | 0;
+ h5 = (h5 + f) | 0;
+ h6 = (h6 + g) | 0;
+ h7 = (h7 + h) | 0;
+ }
+
+ return (
+ toHex32(h0) +
+ toHex32(h1) +
+ toHex32(h2) +
+ toHex32(h3) +
+ toHex32(h4) +
+ toHex32(h5) +
+ toHex32(h6) +
+ toHex32(h7)
+ );
}
function rightRotate(x: number, n: number): number {
- return ((x >>> n) | (x << (32 - n))) >>> 0;
+ return ((x >>> n) | (x << (32 - n))) >>> 0;
}
function toHex32(n: number): string {
- return (n >>> 0).toString(16).padStart(8, "0");
+ return (n >>> 0).toString(16).padStart(8, "0");
}
const K = [
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];
function contentHash(body: string): string {
- return sha256Hex(body);
+ return sha256Hex(body);
}
function compressBody(body: string): { stored: Uint8Array; isCompressed: boolean } {
- const raw = new TextEncoder().encode(body);
- if (raw.length <= COMPRESS_THRESHOLD_BYTES) {
- return { stored: raw, isCompressed: false };
- }
- const compressed = gzipSync(raw);
- if (compressed.length >= raw.length) {
- return { stored: raw, isCompressed: false };
- }
- return { stored: compressed, isCompressed: true };
+ const raw = new TextEncoder().encode(body);
+ if (raw.length <= COMPRESS_THRESHOLD_BYTES) {
+ return { stored: raw, isCompressed: false };
+ }
+ const compressed = gzipSync(raw);
+ if (compressed.length >= raw.length) {
+ return { stored: raw, isCompressed: false };
+ }
+ return { stored: compressed, isCompressed: true };
}
function decompressBody(stored: Uint8Array, isCompressed: boolean): string {
- if (!isCompressed) {
- return new TextDecoder().decode(stored);
- }
- const decompressed = gunzipSync(stored);
- return new TextDecoder().decode(decompressed);
+ if (!isCompressed) {
+ return new TextDecoder().decode(stored);
+ }
+ const decompressed = gunzipSync(stored);
+ return new TextDecoder().decode(decompressed);
}
function storeBody(db: Database, body: string): string {
- const hash = contentHash(body);
- const existing = db.query("SELECT 1 FROM bodies WHERE hash = ?").get(hash) as unknown;
- if (existing !== null) {
- return hash;
- }
- const { stored, isCompressed } = compressBody(body);
- db.prepare(
- "INSERT OR IGNORE INTO bodies (hash, body, isCompressed, originalSize, storedSize) VALUES (?, ?, ?, ?, ?)",
- ).run(hash, stored, isCompressed ? 1 : 0, new TextEncoder().encode(body).length, stored.length);
- return hash;
+ const hash = contentHash(body);
+ const existing = db.query("SELECT 1 FROM bodies WHERE hash = ?").get(hash) as unknown;
+ if (existing !== null) {
+ return hash;
+ }
+ const { stored, isCompressed } = compressBody(body);
+ db.prepare(
+ "INSERT OR IGNORE INTO bodies (hash, body, isCompressed, originalSize, storedSize) VALUES (?, ?, ?, ?, ?)",
+ ).run(hash, stored, isCompressed ? 1 : 0, new TextEncoder().encode(body).length, stored.length);
+ return hash;
}
function resolveBody(db: Database, hash: string | null): string | undefined {
- if (hash === null) {
- return undefined;
- }
- const row = db.query("SELECT body, isCompressed FROM bodies WHERE hash = ?").get(hash) as {
- body: Uint8Array;
- isCompressed: number;
- } | null;
- if (row === undefined || row === null) {
- return undefined;
- }
- return decompressBody(row.body, row.isCompressed === 1);
+ if (hash === null) {
+ return undefined;
+ }
+ const row = db.query("SELECT body, isCompressed FROM bodies WHERE hash = ?").get(hash) as {
+ body: Uint8Array;
+ isCompressed: number;
+ } | null;
+ if (row === undefined || row === null) {
+ return undefined;
+ }
+ return decompressBody(row.body, row.isCompressed === 1);
}
function insertRecords(db: Database, records: readonly LogRecord[]): void {
- const recStmt = db.prepare(`
+ const recStmt = db.prepare(`
INSERT OR IGNORE INTO records
(id, kind, level, msg, name, spanId, parentSpanId,
conversationId, turnId, extensionId, timestamp,
@@ -328,294 +328,294 @@ function insertRecords(db: Database, records: readonly LogRecord[]): void {
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
- const txn = db.transaction(() => {
- for (const r of records) {
- const id = stableId(r);
- const kind = r.kind;
- let level: string | null = null;
- let msg: string | null = null;
- let name: string | null = null;
- let spanId: string | null = null;
- let parentSpanId: string | null = null;
- let durationMs: number | null = null;
- let status: string | null = null;
- let links: string | null = null;
-
- if (r.kind === "log") {
- level = r.level;
- msg = r.msg;
- spanId = r.spanId ?? null;
- parentSpanId = r.parentSpanId ?? null;
- } else if (r.kind === "span-open") {
- name = r.name;
- spanId = r.spanId;
- parentSpanId = r.parentSpanId ?? null;
- if (r.links !== undefined) {
- links = JSON.stringify(r.links);
- }
- } else {
- name = r.name;
- spanId = r.spanId;
- parentSpanId = r.parentSpanId ?? null;
- durationMs = r.durationMs;
- status = r.status;
- if (r.links !== undefined) {
- links = JSON.stringify(r.links);
- }
- }
-
- const attributes: string | null =
- r.attributes !== undefined ? JSON.stringify(r.attributes) : null;
-
- let bodyHash: string | null = null;
- if (r.body !== undefined) {
- bodyHash = storeBody(db, r.body);
- }
-
- recStmt.run(
- id,
- kind,
- level,
- msg,
- name,
- spanId,
- parentSpanId,
- r.conversationId ?? null,
- r.turnId ?? null,
- r.extensionId,
- r.timestamp,
- durationMs,
- status,
- attributes,
- links,
- bodyHash,
- );
- }
- });
- txn();
+ const txn = db.transaction(() => {
+ for (const r of records) {
+ const id = stableId(r);
+ const kind = r.kind;
+ let level: string | null = null;
+ let msg: string | null = null;
+ let name: string | null = null;
+ let spanId: string | null = null;
+ let parentSpanId: string | null = null;
+ let durationMs: number | null = null;
+ let status: string | null = null;
+ let links: string | null = null;
+
+ if (r.kind === "log") {
+ level = r.level;
+ msg = r.msg;
+ spanId = r.spanId ?? null;
+ parentSpanId = r.parentSpanId ?? null;
+ } else if (r.kind === "span-open") {
+ name = r.name;
+ spanId = r.spanId;
+ parentSpanId = r.parentSpanId ?? null;
+ if (r.links !== undefined) {
+ links = JSON.stringify(r.links);
+ }
+ } else {
+ name = r.name;
+ spanId = r.spanId;
+ parentSpanId = r.parentSpanId ?? null;
+ durationMs = r.durationMs;
+ status = r.status;
+ if (r.links !== undefined) {
+ links = JSON.stringify(r.links);
+ }
+ }
+
+ const attributes: string | null =
+ r.attributes !== undefined ? JSON.stringify(r.attributes) : null;
+
+ let bodyHash: string | null = null;
+ if (r.body !== undefined) {
+ bodyHash = storeBody(db, r.body);
+ }
+
+ recStmt.run(
+ id,
+ kind,
+ level,
+ msg,
+ name,
+ spanId,
+ parentSpanId,
+ r.conversationId ?? null,
+ r.turnId ?? null,
+ r.extensionId,
+ r.timestamp,
+ durationMs,
+ status,
+ attributes,
+ links,
+ bodyHash,
+ );
+ }
+ });
+ txn();
}
interface RecordRow {
- id: string;
- kind: string;
- level: string | null;
- msg: string | null;
- name: string | null;
- spanId: string | null;
- parentSpanId: string | null;
- conversationId: string | null;
- turnId: string | null;
- extensionId: string;
- timestamp: number;
- durationMs: number | null;
- status: string | null;
- attributes: string | null;
- links: string | null;
- bodyHash: string | null;
+ id: string;
+ kind: string;
+ level: string | null;
+ msg: string | null;
+ name: string | null;
+ spanId: string | null;
+ parentSpanId: string | null;
+ conversationId: string | null;
+ turnId: string | null;
+ extensionId: string;
+ timestamp: number;
+ durationMs: number | null;
+ status: string | null;
+ attributes: string | null;
+ links: string | null;
+ bodyHash: string | null;
}
function getTurn(db: Database, turnId: string): LogRecord[] {
- const rows = db
- .query("SELECT * FROM records WHERE turnId = ? ORDER BY timestamp ASC, rowid ASC")
- .all(turnId) as RecordRow[];
- return rows.map((row) => rowToRecord(db, row));
+ const rows = db
+ .query("SELECT * FROM records WHERE turnId = ? ORDER BY timestamp ASC, rowid ASC")
+ .all(turnId) as RecordRow[];
+ return rows.map((row) => rowToRecord(db, row));
}
function getBody(db: Database, recordId: string): string | undefined {
- const row = db.query("SELECT bodyHash FROM records WHERE id = ?").get(recordId) as {
- bodyHash: string | null;
- } | null;
- if (row === undefined || row === null || row.bodyHash === null) {
- return undefined;
- }
- return resolveBody(db, row.bodyHash);
+ const row = db.query("SELECT bodyHash FROM records WHERE id = ?").get(recordId) as {
+ bodyHash: string | null;
+ } | null;
+ if (row === undefined || row === null || row.bodyHash === null) {
+ return undefined;
+ }
+ return resolveBody(db, row.bodyHash);
}
function rowToRecord(db: Database, row: RecordRow): LogRecord {
- const attributes: Attributes | undefined =
- row.attributes !== null ? JSON.parse(row.attributes) : undefined;
- const links: SpanLink[] | undefined = row.links !== null ? JSON.parse(row.links) : undefined;
- const body: string | undefined = resolveBody(db, row.bodyHash);
-
- if (row.kind === "log") {
- const record: LogRecord = {
- kind: "log",
- level: row.level as "debug" | "info" | "warn" | "error",
- msg: row.msg ?? "",
- timestamp: row.timestamp,
- extensionId: row.extensionId,
- ...(row.conversationId !== null && { conversationId: row.conversationId }),
- ...(row.turnId !== null && { turnId: row.turnId }),
- ...(row.spanId !== null && { spanId: row.spanId }),
- ...(row.parentSpanId !== null && { parentSpanId: row.parentSpanId }),
- ...(attributes !== undefined && { attributes }),
- ...(body !== undefined && { body }),
- };
- return record;
- }
-
- if (row.kind === "span-open") {
- const record: LogRecord = {
- kind: "span-open",
- spanId: row.spanId ?? "",
- name: row.name ?? "",
- timestamp: row.timestamp,
- extensionId: row.extensionId,
- ...(row.conversationId !== null && { conversationId: row.conversationId }),
- ...(row.turnId !== null && { turnId: row.turnId }),
- ...(row.parentSpanId !== null && { parentSpanId: row.parentSpanId }),
- ...(attributes !== undefined && { attributes }),
- ...(links !== undefined && { links }),
- ...(body !== undefined && { body }),
- };
- return record;
- }
-
- const record: LogRecord = {
- kind: "span-close",
- spanId: row.spanId ?? "",
- name: row.name ?? "",
- timestamp: row.timestamp,
- durationMs: row.durationMs ?? 0,
- status: (row.status as "ok" | "error") ?? "ok",
- extensionId: row.extensionId,
- ...(row.conversationId !== null && { conversationId: row.conversationId }),
- ...(row.turnId !== null && { turnId: row.turnId }),
- ...(row.parentSpanId !== null && { parentSpanId: row.parentSpanId }),
- ...(attributes !== undefined && { attributes }),
- ...(links !== undefined && { links }),
- ...(body !== undefined && { body }),
- };
- return record;
+ const attributes: Attributes | undefined =
+ row.attributes !== null ? JSON.parse(row.attributes) : undefined;
+ const links: SpanLink[] | undefined = row.links !== null ? JSON.parse(row.links) : undefined;
+ const body: string | undefined = resolveBody(db, row.bodyHash);
+
+ if (row.kind === "log") {
+ const record: LogRecord = {
+ kind: "log",
+ level: row.level as "debug" | "info" | "warn" | "error",
+ msg: row.msg ?? "",
+ timestamp: row.timestamp,
+ extensionId: row.extensionId,
+ ...(row.conversationId !== null && { conversationId: row.conversationId }),
+ ...(row.turnId !== null && { turnId: row.turnId }),
+ ...(row.spanId !== null && { spanId: row.spanId }),
+ ...(row.parentSpanId !== null && { parentSpanId: row.parentSpanId }),
+ ...(attributes !== undefined && { attributes }),
+ ...(body !== undefined && { body }),
+ };
+ return record;
+ }
+
+ if (row.kind === "span-open") {
+ const record: LogRecord = {
+ kind: "span-open",
+ spanId: row.spanId ?? "",
+ name: row.name ?? "",
+ timestamp: row.timestamp,
+ extensionId: row.extensionId,
+ ...(row.conversationId !== null && { conversationId: row.conversationId }),
+ ...(row.turnId !== null && { turnId: row.turnId }),
+ ...(row.parentSpanId !== null && { parentSpanId: row.parentSpanId }),
+ ...(attributes !== undefined && { attributes }),
+ ...(links !== undefined && { links }),
+ ...(body !== undefined && { body }),
+ };
+ return record;
+ }
+
+ const record: LogRecord = {
+ kind: "span-close",
+ spanId: row.spanId ?? "",
+ name: row.name ?? "",
+ timestamp: row.timestamp,
+ durationMs: row.durationMs ?? 0,
+ status: (row.status as "ok" | "error") ?? "ok",
+ extensionId: row.extensionId,
+ ...(row.conversationId !== null && { conversationId: row.conversationId }),
+ ...(row.turnId !== null && { turnId: row.turnId }),
+ ...(row.parentSpanId !== null && { parentSpanId: row.parentSpanId }),
+ ...(attributes !== undefined && { attributes }),
+ ...(links !== undefined && { links }),
+ ...(body !== undefined && { body }),
+ };
+ return record;
}
interface BodyRow {
- hash: string;
- storedSize: number;
+ hash: string;
+ storedSize: number;
}
interface BodyWithTimestamp extends BodyRow {
- oldestRecordTimestamp: number;
+ oldestRecordTimestamp: number;
}
export function computeEvictions(
- bodies: readonly BodyWithTimestamp[],
- maxTotalBodyBytes: number,
+ bodies: readonly BodyWithTimestamp[],
+ maxTotalBodyBytes: number,
): string[] {
- let totalBytes = 0;
- for (const b of bodies) {
- totalBytes += b.storedSize;
- }
- if (totalBytes <= maxTotalBodyBytes) {
- return [];
- }
-
- const sorted = [...bodies].sort((a, b) => a.oldestRecordTimestamp - b.oldestRecordTimestamp);
- const evict: string[] = [];
- let remaining = totalBytes;
- for (const b of sorted) {
- if (remaining <= maxTotalBodyBytes) {
- break;
- }
- evict.push(b.hash);
- remaining -= b.storedSize;
- }
- return evict;
+ let totalBytes = 0;
+ for (const b of bodies) {
+ totalBytes += b.storedSize;
+ }
+ if (totalBytes <= maxTotalBodyBytes) {
+ return [];
+ }
+
+ const sorted = [...bodies].sort((a, b) => a.oldestRecordTimestamp - b.oldestRecordTimestamp);
+ const evict: string[] = [];
+ let remaining = totalBytes;
+ for (const b of sorted) {
+ if (remaining <= maxTotalBodyBytes) {
+ break;
+ }
+ evict.push(b.hash);
+ remaining -= b.storedSize;
+ }
+ return evict;
}
function prune(db: Database, policy: RetentionPolicy): PruneSummary {
- let recordsDeleted = 0;
- let bodiesDeleted = 0;
- let bytesReclaimed = 0;
-
- const now = Date.now();
-
- if (policy.maxAgeMs !== undefined) {
- const cutoff = now - policy.maxAgeMs;
- const oldRecords = db
- .query("SELECT id, bodyHash FROM records WHERE timestamp < ?")
- .all(cutoff) as Array<{ id: string; bodyHash: string | null }>;
-
- if (oldRecords.length > 0) {
- const bodyHashes = oldRecords.map((r) => r.bodyHash).filter((h): h is string => h !== null);
-
- const deleteTxn = db.transaction(() => {
- db.prepare("DELETE FROM records WHERE timestamp < ?").run(cutoff);
- for (const hash of bodyHashes) {
- const refCount = db
- .query("SELECT COUNT(*) as cnt FROM records WHERE bodyHash = ?")
- .get(hash) as { cnt: number };
- if (refCount.cnt === 0) {
- const bodyRow = db.query("SELECT storedSize FROM bodies WHERE hash = ?").get(hash) as {
- storedSize: number;
- } | null;
- if (bodyRow !== undefined && bodyRow !== null) {
- bytesReclaimed += bodyRow.storedSize;
- }
- db.prepare("DELETE FROM bodies WHERE hash = ?").run(hash);
- bodiesDeleted++;
- }
- }
- });
- deleteTxn();
- recordsDeleted = oldRecords.length;
- }
- }
-
- if (policy.maxTotalBodyBytes !== undefined) {
- const bodyRows = db
- .query(`
+ let recordsDeleted = 0;
+ let bodiesDeleted = 0;
+ let bytesReclaimed = 0;
+
+ const now = Date.now();
+
+ if (policy.maxAgeMs !== undefined) {
+ const cutoff = now - policy.maxAgeMs;
+ const oldRecords = db
+ .query("SELECT id, bodyHash FROM records WHERE timestamp < ?")
+ .all(cutoff) as Array<{ id: string; bodyHash: string | null }>;
+
+ if (oldRecords.length > 0) {
+ const bodyHashes = oldRecords.map((r) => r.bodyHash).filter((h): h is string => h !== null);
+
+ const deleteTxn = db.transaction(() => {
+ db.prepare("DELETE FROM records WHERE timestamp < ?").run(cutoff);
+ for (const hash of bodyHashes) {
+ const refCount = db
+ .query("SELECT COUNT(*) as cnt FROM records WHERE bodyHash = ?")
+ .get(hash) as { cnt: number };
+ if (refCount.cnt === 0) {
+ const bodyRow = db.query("SELECT storedSize FROM bodies WHERE hash = ?").get(hash) as {
+ storedSize: number;
+ } | null;
+ if (bodyRow !== undefined && bodyRow !== null) {
+ bytesReclaimed += bodyRow.storedSize;
+ }
+ db.prepare("DELETE FROM bodies WHERE hash = ?").run(hash);
+ bodiesDeleted++;
+ }
+ }
+ });
+ deleteTxn();
+ recordsDeleted = oldRecords.length;
+ }
+ }
+
+ if (policy.maxTotalBodyBytes !== undefined) {
+ const bodyRows = db
+ .query(`
SELECT b.hash, b.storedSize, MIN(r.timestamp) as oldestRecordTimestamp
FROM bodies b
JOIN records r ON r.bodyHash = b.hash
GROUP BY b.hash
`)
- .all() as Array<{ hash: string; storedSize: number; oldestRecordTimestamp: number }>;
-
- const toEvict = computeEvictions(bodyRows, policy.maxTotalBodyBytes);
-
- if (toEvict.length > 0) {
- const evictTxn = db.transaction(() => {
- for (const hash of toEvict) {
- const bodyRow = db.query("SELECT storedSize FROM bodies WHERE hash = ?").get(hash) as {
- storedSize: number;
- } | null;
- if (bodyRow !== undefined && bodyRow !== null) {
- bytesReclaimed += bodyRow.storedSize;
- }
- db.prepare("DELETE FROM records WHERE bodyHash = ?").run(hash);
- db.prepare("DELETE FROM bodies WHERE hash = ?").run(hash);
- bodiesDeleted++;
- }
- });
- evictTxn();
- recordsDeleted += toEvict.length;
- }
- }
-
- return { recordsDeleted, bodiesDeleted, bytesReclaimed };
+ .all() as Array<{ hash: string; storedSize: number; oldestRecordTimestamp: number }>;
+
+ const toEvict = computeEvictions(bodyRows, policy.maxTotalBodyBytes);
+
+ if (toEvict.length > 0) {
+ const evictTxn = db.transaction(() => {
+ for (const hash of toEvict) {
+ const bodyRow = db.query("SELECT storedSize FROM bodies WHERE hash = ?").get(hash) as {
+ storedSize: number;
+ } | null;
+ if (bodyRow !== undefined && bodyRow !== null) {
+ bytesReclaimed += bodyRow.storedSize;
+ }
+ db.prepare("DELETE FROM records WHERE bodyHash = ?").run(hash);
+ db.prepare("DELETE FROM bodies WHERE hash = ?").run(hash);
+ bodiesDeleted++;
+ }
+ });
+ evictTxn();
+ recordsDeleted += toEvict.length;
+ }
+ }
+
+ return { recordsDeleted, bodiesDeleted, bytesReclaimed };
}
function toCanonicalJson(value: unknown): string {
- if (value === null || typeof value !== "object") {
- return JSON.stringify(value);
- }
- if (Array.isArray(value)) {
- return `[${value.map(toCanonicalJson).join(",")}]`;
- }
- const obj = value as Record<string, unknown>;
- const keys = Object.keys(obj).sort();
- const entries = keys.map((k) => `${JSON.stringify(k)}:${toCanonicalJson(obj[k])}`);
- return `{${entries.join(",")}}`;
+ if (value === null || typeof value !== "object") {
+ return JSON.stringify(value);
+ }
+ if (Array.isArray(value)) {
+ return `[${value.map(toCanonicalJson).join(",")}]`;
+ }
+ const obj = value as Record<string, unknown>;
+ const keys = Object.keys(obj).sort();
+ const entries = keys.map((k) => `${JSON.stringify(k)}:${toCanonicalJson(obj[k])}`);
+ return `{${entries.join(",")}}`;
}
export function stableId(record: LogRecord): string {
- const json = toCanonicalJson(record);
- let hash = 0xcbf29ce484222325n;
- const prime = 0x100000001b3n;
- for (let i = 0; i < json.length; i++) {
- hash ^= BigInt(json.charCodeAt(i));
- hash = (hash * prime) & 0xffffffffffffffffn;
- }
- return hash.toString(16).padStart(16, "0");
+ const json = toCanonicalJson(record);
+ let hash = 0xcbf29ce484222325n;
+ const prime = 0x100000001b3n;
+ for (let i = 0; i < json.length; i++) {
+ hash ^= BigInt(json.charCodeAt(i));
+ hash = (hash * prime) & 0xffffffffffffffffn;
+ }
+ return hash.toString(16).padStart(16, "0");
}