summaryrefslogtreecommitdiffhomepage
path: root/packages/trace-store/src/store.ts
blob: f9564bb914d330b84dfdd30f1e3f7b81c6b21f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import { Database } from "bun:sqlite";
import { gunzipSync, gzipSync } from "node:zlib";
import type { Attributes, LogRecord, SpanLink } from "@dispatch/kernel";
import { renderEasyView } from "./easy-view.js";

const COMPRESS_THRESHOLD_BYTES = 1024;

export interface RetentionPolicy {
	readonly maxAgeMs?: number;
	readonly maxTotalBodyBytes?: number;
}

export const DEFAULT_RETENTION: Required<RetentionPolicy> = {
	maxAgeMs: 7 * 24 * 60 * 60 * 1000,
	maxTotalBodyBytes: 256 * 1024 * 1024,
};

export interface PruneSummary {
	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;
}

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();
		},
	};
}

function schema(db: Database): void {
	db.run(`
		CREATE TABLE IF NOT EXISTS records (
			id TEXT PRIMARY KEY,
			kind TEXT NOT NULL,
			level TEXT,
			msg TEXT,
			name TEXT,
			spanId TEXT,
			parentSpanId TEXT,
			conversationId TEXT,
			turnId TEXT,
			extensionId TEXT NOT NULL,
			timestamp INTEGER NOT NULL,
			durationMs INTEGER,
			status TEXT,
			attributes TEXT,
			links TEXT,
			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)");

	migrateOldBodies(db);

	db.run(`
		CREATE TABLE IF NOT EXISTS bodies (
			hash TEXT PRIMARY KEY,
			body BLOB NOT NULL,
			isCompressed INTEGER NOT NULL DEFAULT 0,
			originalSize INTEGER NOT NULL,
			storedSize INTEGER NOT NULL
		)
	`);

	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(`
		CREATE TABLE IF NOT EXISTS bodies (
			hash TEXT PRIMARY KEY,
			body BLOB NOT NULL,
			isCompressed INTEGER NOT NULL DEFAULT 0,
			originalSize INTEGER NOT NULL,
			storedSize INTEGER NOT NULL
		)
	`);

	const hasBodyHash = cols.some((c) => c.name === "bodyHash");
	if (!hasBodyHash) {
		db.run("ALTER TABLE records ADD COLUMN bodyHash TEXT");
	}

	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();
}

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)
	);
}

function rightRotate(x: number, n: number): number {
	return ((x >>> n) | (x << (32 - n))) >>> 0;
}

function toHex32(n: number): string {
	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,
];

function contentHash(body: string): string {
	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 };
}

function decompressBody(stored: Uint8Array, isCompressed: boolean): string {
	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;
}

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);
}

function insertRecords(db: Database, records: readonly LogRecord[]): void {
	const recStmt = db.prepare(`
		INSERT OR IGNORE INTO records
			(id, kind, level, msg, name, spanId, parentSpanId,
			 conversationId, turnId, extensionId, timestamp,
			 durationMs, status, attributes, links, bodyHash)
		VALUES
			(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	`);

	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;
}

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));
}

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);
}

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;
}

interface BodyRow {
	hash: string;
	storedSize: number;
}

interface BodyWithTimestamp extends BodyRow {
	oldestRecordTimestamp: number;
}

export function computeEvictions(
	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;
}

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(`
				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 };
}

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(",")}}`;
}

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");
}