agent-trace 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/agent-trace.cjs +123 -2
  2. package/package.json +1 -1
package/agent-trace.cjs CHANGED
@@ -24988,7 +24988,7 @@ async function startDashboardServer(options = {}) {
24988
24988
  var import_better_sqlite3 = __toESM(require("better-sqlite3"));
24989
24989
  var SCHEMA_SQL = `
24990
24990
  CREATE TABLE IF NOT EXISTS agent_events (
24991
- event_id TEXT NOT NULL,
24991
+ event_id TEXT NOT NULL UNIQUE,
24992
24992
  event_type TEXT NOT NULL,
24993
24993
  event_timestamp TEXT NOT NULL,
24994
24994
  session_id TEXT NOT NULL,
@@ -25100,6 +25100,7 @@ var SqliteClient = class {
25100
25100
  this.db = new import_better_sqlite3.default(dbPath);
25101
25101
  this.db.pragma("journal_mode = WAL");
25102
25102
  this.db.pragma("synchronous = NORMAL");
25103
+ this.migrateDeduplicateEvents();
25103
25104
  this.db.exec(SCHEMA_SQL);
25104
25105
  }
25105
25106
  async insertJsonEachRow(request) {
@@ -25319,9 +25320,129 @@ var SqliteClient = class {
25319
25320
  close() {
25320
25321
  this.db.close();
25321
25322
  }
25323
+ /**
25324
+ * Migration: deduplicate agent_events rows from older schemas that lacked a UNIQUE constraint.
25325
+ * Runs once — if the old table exists without a unique index, it rebuilds it.
25326
+ */
25327
+ migrateDeduplicateEvents() {
25328
+ const tableExists = this.db.prepare(
25329
+ "SELECT 1 FROM sqlite_master WHERE type='table' AND name='agent_events'"
25330
+ ).get();
25331
+ if (tableExists === void 0) {
25332
+ return;
25333
+ }
25334
+ const hasUniqueIndex = this.db.prepare(
25335
+ "SELECT 1 FROM sqlite_master WHERE type='index' AND tbl_name='agent_events' AND sql LIKE '%UNIQUE%'"
25336
+ ).get();
25337
+ const indexInfo = this.db.prepare("PRAGMA index_list('agent_events')").all();
25338
+ const hasAutoUnique = indexInfo.some((idx) => idx["unique"] === 1);
25339
+ if (hasUniqueIndex !== void 0 || hasAutoUnique) {
25340
+ return;
25341
+ }
25342
+ const countResult = this.db.prepare(
25343
+ "SELECT COUNT(*) as total FROM agent_events"
25344
+ ).get();
25345
+ const distinctResult = this.db.prepare(
25346
+ "SELECT COUNT(DISTINCT event_id) as distinct_count FROM agent_events"
25347
+ ).get();
25348
+ const total = countResult?.total ?? 0;
25349
+ const distinct = distinctResult?.distinct_count ?? 0;
25350
+ if (total === 0) {
25351
+ this.db.exec("DROP TABLE agent_events");
25352
+ const tracesExist = this.db.prepare(
25353
+ "SELECT 1 FROM sqlite_master WHERE type='table' AND name='session_traces'"
25354
+ ).get();
25355
+ if (tracesExist !== void 0) {
25356
+ this.db.exec("DELETE FROM session_traces");
25357
+ }
25358
+ return;
25359
+ }
25360
+ console.log(`[agent-trace] migrating: deduplicating agent_events (${total} rows \u2192 ${distinct} distinct)`);
25361
+ this.db.exec(`
25362
+ CREATE TABLE agent_events_dedup (
25363
+ event_id TEXT NOT NULL UNIQUE,
25364
+ event_type TEXT NOT NULL,
25365
+ event_timestamp TEXT NOT NULL,
25366
+ session_id TEXT NOT NULL,
25367
+ prompt_id TEXT,
25368
+ user_id TEXT NOT NULL DEFAULT 'unknown_user',
25369
+ source TEXT NOT NULL DEFAULT 'hook',
25370
+ agent_type TEXT NOT NULL DEFAULT 'claude_code',
25371
+ tool_name TEXT,
25372
+ tool_success INTEGER,
25373
+ tool_duration_ms REAL,
25374
+ model TEXT,
25375
+ cost_usd REAL,
25376
+ input_tokens INTEGER,
25377
+ output_tokens INTEGER,
25378
+ api_duration_ms REAL,
25379
+ lines_added INTEGER,
25380
+ lines_removed INTEGER,
25381
+ files_changed TEXT NOT NULL DEFAULT '[]',
25382
+ commit_sha TEXT,
25383
+ attributes TEXT NOT NULL DEFAULT '{}'
25384
+ );
25385
+
25386
+ INSERT OR IGNORE INTO agent_events_dedup SELECT * FROM agent_events;
25387
+
25388
+ DROP TABLE agent_events;
25389
+ ALTER TABLE agent_events_dedup RENAME TO agent_events;
25390
+
25391
+ CREATE INDEX IF NOT EXISTS idx_events_session ON agent_events(session_id);
25392
+ CREATE INDEX IF NOT EXISTS idx_events_timestamp ON agent_events(event_timestamp);
25393
+ `);
25394
+ const afterCount = this.db.prepare("SELECT COUNT(*) as c FROM agent_events").get();
25395
+ console.log(`[agent-trace] migration complete: ${afterCount.c} events after dedup (removed ${total - afterCount.c} duplicates)`);
25396
+ this.rebuildSessionTracesFromEvents();
25397
+ }
25398
+ /**
25399
+ * Rebuild session_traces by aggregating deduplicated agent_events.
25400
+ * Called after dedup migration so the dashboard has correct metrics immediately.
25401
+ */
25402
+ rebuildSessionTracesFromEvents() {
25403
+ const tracesExist = this.db.prepare(
25404
+ "SELECT 1 FROM sqlite_master WHERE type='table' AND name='session_traces'"
25405
+ ).get();
25406
+ if (tracesExist === void 0) {
25407
+ return;
25408
+ }
25409
+ this.db.exec("DELETE FROM session_traces");
25410
+ this.db.exec(`
25411
+ INSERT OR REPLACE INTO session_traces
25412
+ (session_id, version, started_at, ended_at, user_id, git_repo, git_branch,
25413
+ prompt_count, tool_call_count, api_call_count, total_cost_usd,
25414
+ total_input_tokens, total_output_tokens, lines_added, lines_removed,
25415
+ models_used, tools_used, files_touched, commit_count, updated_at)
25416
+ SELECT
25417
+ session_id,
25418
+ 1,
25419
+ MIN(event_timestamp),
25420
+ MAX(event_timestamp),
25421
+ COALESCE(MAX(CASE WHEN user_id != 'unknown_user' THEN user_id END), 'unknown_user'),
25422
+ NULL,
25423
+ NULL,
25424
+ SUM(CASE WHEN event_type LIKE '%prompt%' THEN 1 ELSE 0 END),
25425
+ SUM(CASE WHEN event_type LIKE '%tool%' THEN 1 ELSE 0 END),
25426
+ SUM(CASE WHEN event_type LIKE '%api%' THEN 1 ELSE 0 END),
25427
+ COALESCE(SUM(cost_usd), 0),
25428
+ COALESCE(SUM(input_tokens), 0),
25429
+ COALESCE(SUM(output_tokens), 0),
25430
+ COALESCE(SUM(lines_added), 0),
25431
+ COALESCE(SUM(lines_removed), 0),
25432
+ '[]',
25433
+ '[]',
25434
+ '[]',
25435
+ COUNT(DISTINCT commit_sha),
25436
+ MAX(event_timestamp)
25437
+ FROM agent_events
25438
+ GROUP BY session_id
25439
+ `);
25440
+ const rebuilt = this.db.prepare("SELECT COUNT(*) as c FROM session_traces").get();
25441
+ console.log(`[agent-trace] rebuilt ${rebuilt.c} session traces from deduplicated events`);
25442
+ }
25322
25443
  insertEvents(rows) {
25323
25444
  const insert = this.db.prepare(`
25324
- INSERT INTO agent_events
25445
+ INSERT OR IGNORE INTO agent_events
25325
25446
  (event_id, event_type, event_timestamp, session_id, prompt_id, user_id, source, agent_type,
25326
25447
  tool_name, tool_success, tool_duration_ms, model, cost_usd, input_tokens, output_tokens,
25327
25448
  api_duration_ms, lines_added, lines_removed, files_changed, commit_sha, attributes)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-trace",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Self-hosted observability for AI coding agents. One command, zero config.",
5
5
  "license": "Apache-2.0",
6
6
  "bin": {