claude-memory-layer 1.0.10 → 1.0.12

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 (142) hide show
  1. package/AGENTS.md +60 -0
  2. package/README.md +166 -2
  3. package/bootstrap-kb/decisions/decisions.md +244 -0
  4. package/bootstrap-kb/glossary/glossary.md +46 -0
  5. package/bootstrap-kb/modules/.claude-plugin.md +22 -0
  6. package/bootstrap-kb/modules/agents.md.md +15 -0
  7. package/bootstrap-kb/modules/claude.md.md +15 -0
  8. package/bootstrap-kb/modules/context.md.md +15 -0
  9. package/bootstrap-kb/modules/docs.md +18 -0
  10. package/bootstrap-kb/modules/handoff.md.md +15 -0
  11. package/bootstrap-kb/modules/package-lock.json.md +15 -0
  12. package/bootstrap-kb/modules/package.json.md +15 -0
  13. package/bootstrap-kb/modules/plan.md.md +15 -0
  14. package/bootstrap-kb/modules/readme.md.md +15 -0
  15. package/bootstrap-kb/modules/scripts.md +26 -0
  16. package/bootstrap-kb/modules/spec.md.md +15 -0
  17. package/bootstrap-kb/modules/specs.md +20 -0
  18. package/bootstrap-kb/modules/src.md +51 -0
  19. package/bootstrap-kb/modules/tests.md +42 -0
  20. package/bootstrap-kb/modules/tsconfig.json.md +15 -0
  21. package/bootstrap-kb/modules/vitest.config.ts.md +15 -0
  22. package/bootstrap-kb/overview/overview.md +40 -0
  23. package/bootstrap-kb/sources/manifest.json +950 -0
  24. package/bootstrap-kb/sources/manifest.md +227 -0
  25. package/bootstrap-kb/timeline/timeline.md +57 -0
  26. package/d.sh +3 -0
  27. package/deploy.sh +3 -0
  28. package/dist/cli/index.js +3577 -389
  29. package/dist/cli/index.js.map +4 -4
  30. package/dist/core/index.js +1383 -138
  31. package/dist/core/index.js.map +4 -4
  32. package/dist/hooks/post-tool-use.js +1917 -214
  33. package/dist/hooks/post-tool-use.js.map +4 -4
  34. package/dist/hooks/session-end.js +1813 -231
  35. package/dist/hooks/session-end.js.map +4 -4
  36. package/dist/hooks/session-start.js +1802 -205
  37. package/dist/hooks/session-start.js.map +4 -4
  38. package/dist/hooks/stop.js +1909 -248
  39. package/dist/hooks/stop.js.map +4 -4
  40. package/dist/hooks/user-prompt-submit.js +1861 -206
  41. package/dist/hooks/user-prompt-submit.js.map +4 -4
  42. package/dist/server/api/index.js +2341 -217
  43. package/dist/server/api/index.js.map +4 -4
  44. package/dist/server/index.js +2350 -226
  45. package/dist/server/index.js.map +4 -4
  46. package/dist/services/memory-service.js +1805 -206
  47. package/dist/services/memory-service.js.map +4 -4
  48. package/dist/ui/app.js +1447 -55
  49. package/dist/ui/index.html +318 -147
  50. package/dist/ui/style.css +892 -0
  51. package/docs/MCP_MEMORY_SERVICE_COMPARATIVE_REVIEW.md +271 -0
  52. package/docs/MEMU_ADOPTION.md +40 -0
  53. package/docs/OPERATIONS.md +18 -0
  54. package/memory/.claude-plugin/commands/2026-02-25.md +263 -0
  55. package/memory/_index.md +405 -0
  56. package/memory/default/uncategorized/2026-02-25.md +4839 -0
  57. package/memory/specs/20260207-dashboard-upgrade/2026-02-25.md +142 -0
  58. package/memory/specs/citations-system/2026-02-25.md +1121 -0
  59. package/memory/specs/endless-mode/2026-02-25.md +1392 -0
  60. package/memory/specs/entity-edge-model/2026-02-25.md +1263 -0
  61. package/memory/specs/evidence-aligner-v2/2026-02-25.md +1028 -0
  62. package/memory/specs/mcp-desktop-integration/2026-02-25.md +1334 -0
  63. package/memory/specs/post-tool-use-hook/2026-02-25.md +1164 -0
  64. package/memory/specs/private-tags/2026-02-25.md +1057 -0
  65. package/memory/specs/progressive-disclosure/2026-02-25.md +1436 -0
  66. package/memory/specs/task-entity-system/2026-02-25.md +924 -0
  67. package/memory/specs/vector-outbox-v2/2026-02-25.md +1510 -0
  68. package/memory/specs/web-viewer-ui/2026-02-25.md +1709 -0
  69. package/package.json +9 -2
  70. package/scripts/build.ts +6 -0
  71. package/scripts/fix-sync-gap.js +32 -0
  72. package/scripts/heartbeat-memory-orchestrator.sh +28 -0
  73. package/scripts/report-sync-gap.js +26 -0
  74. package/scripts/review-queue-auto-resolve.js +21 -0
  75. package/scripts/sync-gap-auto-heal.sh +17 -0
  76. package/specs/20260207-dashboard-upgrade/context.md +38 -0
  77. package/specs/20260207-dashboard-upgrade/spec.md +96 -0
  78. package/src/cli/index.ts +391 -60
  79. package/src/core/consolidated-store.ts +63 -1
  80. package/src/core/consolidation-worker.ts +115 -6
  81. package/src/core/event-store.ts +14 -0
  82. package/src/core/index.ts +1 -0
  83. package/src/core/ingest-interceptor.ts +80 -0
  84. package/src/core/markdown-mirror.ts +70 -0
  85. package/src/core/md-mirror.ts +92 -0
  86. package/src/core/mongo-sync-config.ts +165 -0
  87. package/src/core/mongo-sync-worker.ts +381 -0
  88. package/src/core/retriever.ts +540 -150
  89. package/src/core/sqlite-event-store.ts +794 -7
  90. package/src/core/sqlite-wrapper.ts +8 -0
  91. package/src/core/tag-taxonomy.ts +51 -0
  92. package/src/core/turn-state.ts +159 -0
  93. package/src/core/types.ts +51 -8
  94. package/src/core/vector-store.ts +21 -3
  95. package/src/hooks/post-tool-use.ts +68 -23
  96. package/src/hooks/session-end.ts +8 -3
  97. package/src/hooks/stop.ts +96 -25
  98. package/src/hooks/user-prompt-submit.ts +44 -5
  99. package/src/server/api/chat.ts +244 -0
  100. package/src/server/api/citations.ts +3 -3
  101. package/src/server/api/events.ts +30 -5
  102. package/src/server/api/health.ts +53 -0
  103. package/src/server/api/index.ts +9 -1
  104. package/src/server/api/projects.ts +74 -0
  105. package/src/server/api/search.ts +3 -3
  106. package/src/server/api/sessions.ts +3 -3
  107. package/src/server/api/stats.ts +89 -8
  108. package/src/server/api/turns.ts +143 -0
  109. package/src/server/api/utils.ts +46 -0
  110. package/src/services/bootstrap-organizer.ts +443 -0
  111. package/src/services/codex-session-history-importer.ts +474 -0
  112. package/src/services/memory-service.ts +508 -71
  113. package/src/services/session-history-importer.ts +215 -51
  114. package/src/ui/app.js +1447 -55
  115. package/src/ui/index.html +318 -147
  116. package/src/ui/style.css +892 -0
  117. package/tests/bootstrap-organizer.test.ts +111 -0
  118. package/tests/consolidation-worker.test.ts +75 -0
  119. package/tests/ingest-interceptor.test.ts +38 -0
  120. package/tests/markdown-mirror.test.ts +85 -0
  121. package/tests/md-mirror.test.ts +50 -0
  122. package/tests/retriever-fallback-chain.test.ts +223 -0
  123. package/tests/retriever-strategy-scope.test.ts +97 -0
  124. package/tests/retriever.memu-adoption.test.ts +122 -0
  125. package/tests/sqlite-event-store-replication.test.ts +92 -0
  126. package/.claude/settings.local.json +0 -27
  127. package/.claude-memory/test.sqlite +0 -0
  128. package/.history/package_20260201112328.json +0 -45
  129. package/.history/package_20260201113602.json +0 -45
  130. package/.history/package_20260201113713.json +0 -45
  131. package/.history/package_20260201114110.json +0 -45
  132. package/.history/package_20260201114632.json +0 -46
  133. package/.history/package_20260201133143.json +0 -45
  134. package/.history/package_20260201134319.json +0 -45
  135. package/.history/package_20260201134326.json +0 -45
  136. package/.history/package_20260201134334.json +0 -45
  137. package/.history/package_20260201134912.json +0 -45
  138. package/.history/package_20260201142928.json +0 -46
  139. package/.history/package_20260201192048.json +0 -47
  140. package/.history/package_20260202114053.json +0 -49
  141. package/.history/package_20260202121115.json +0 -49
  142. package/test_access.js +0 -49
@@ -4,26 +4,37 @@ import { dirname } from 'path';
4
4
  const require = createRequire(import.meta.url);
5
5
  const __filename = fileURLToPath(import.meta.url);
6
6
  const __dirname = dirname(__filename);
7
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
+ }) : x)(function(x) {
10
+ if (typeof require !== "undefined")
11
+ return require.apply(this, arguments);
12
+ throw Error('Dynamic require of "' + x + '" is not supported');
13
+ });
7
14
 
8
15
  // src/server/index.ts
9
- import { Hono as Hono7 } from "hono";
16
+ import { Hono as Hono11 } from "hono";
10
17
  import { cors } from "hono/cors";
11
18
  import { logger } from "hono/logger";
12
19
  import { serve } from "@hono/node-server";
13
20
  import { serveStatic } from "@hono/node-server/serve-static";
14
- import * as path2 from "path";
15
- import * as fs2 from "fs";
21
+ import * as path6 from "path";
22
+ import * as fs6 from "fs";
16
23
 
17
24
  // src/server/api/index.ts
18
- import { Hono as Hono6 } from "hono";
25
+ import { Hono as Hono10 } from "hono";
19
26
 
20
27
  // src/server/api/sessions.ts
21
28
  import { Hono } from "hono";
22
29
 
30
+ // src/server/api/utils.ts
31
+ import * as path4 from "path";
32
+ import * as os2 from "os";
33
+
23
34
  // src/services/memory-service.ts
24
- import * as path from "path";
35
+ import * as path3 from "path";
25
36
  import * as os from "os";
26
- import * as fs from "fs";
37
+ import * as fs4 from "fs";
27
38
  import * as crypto2 from "crypto";
28
39
 
29
40
  // src/core/event-store.ts
@@ -81,11 +92,11 @@ function toDate(value) {
81
92
  return new Date(value);
82
93
  return new Date(String(value));
83
94
  }
84
- function createDatabase(path3, options) {
95
+ function createDatabase(path7, options) {
85
96
  if (options?.readOnly) {
86
- return new duckdb.Database(path3, { access_mode: "READ_ONLY" });
97
+ return new duckdb.Database(path7, { access_mode: "READ_ONLY" });
87
98
  }
88
- return new duckdb.Database(path3);
99
+ return new duckdb.Database(path7);
89
100
  }
90
101
  function dbRun(db, sql, params = []) {
91
102
  return new Promise((resolve2, reject) => {
@@ -349,6 +360,17 @@ var EventStore = class {
349
360
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
350
361
  )
351
362
  `);
363
+ await dbRun(this.db, `
364
+ CREATE TABLE IF NOT EXISTS consolidated_rules (
365
+ rule_id VARCHAR PRIMARY KEY,
366
+ rule TEXT NOT NULL,
367
+ topics JSON,
368
+ source_memory_ids JSON,
369
+ source_events JSON,
370
+ confidence FLOAT DEFAULT 0.5,
371
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
372
+ )
373
+ `);
352
374
  await dbRun(this.db, `
353
375
  CREATE TABLE IF NOT EXISTS endless_config (
354
376
  key VARCHAR PRIMARY KEY,
@@ -368,6 +390,7 @@ var EventStore = class {
368
390
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
369
391
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
370
392
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
393
+ await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
371
394
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
372
395
  this.initialized = true;
373
396
  }
@@ -755,8 +778,14 @@ import { randomUUID as randomUUID2 } from "crypto";
755
778
 
756
779
  // src/core/sqlite-wrapper.ts
757
780
  import Database from "better-sqlite3";
758
- function createSQLiteDatabase(path3, options) {
759
- const db = new Database(path3, {
781
+ import * as fs from "fs";
782
+ import * as nodePath from "path";
783
+ function createSQLiteDatabase(path7, options) {
784
+ const dir = nodePath.dirname(path7);
785
+ if (!fs.existsSync(dir)) {
786
+ fs.mkdirSync(dir, { recursive: true });
787
+ }
788
+ const db = new Database(path7, {
760
789
  readonly: options?.readonly ?? false
761
790
  });
762
791
  if (!options?.readonly && (options?.walMode ?? true)) {
@@ -797,6 +826,64 @@ function toSQLiteTimestamp(date) {
797
826
  return date.toISOString();
798
827
  }
799
828
 
829
+ // src/core/markdown-mirror.ts
830
+ import * as fs2 from "fs/promises";
831
+ import * as path from "path";
832
+ var DEFAULT_NAMESPACE = "default";
833
+ var DEFAULT_CATEGORY = "uncategorized";
834
+ function sanitizeSegment(input, fallback) {
835
+ const raw = String(input ?? "").trim().toLowerCase();
836
+ const safe = raw.normalize("NFKD").replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
837
+ if (!safe || safe === "." || safe === "..")
838
+ return fallback;
839
+ return safe;
840
+ }
841
+ function getCategorySegments(metadata, eventType) {
842
+ const raw = metadata?.categoryPath;
843
+ if (Array.isArray(raw) && raw.length > 0) {
844
+ return raw.map((s) => sanitizeSegment(s, DEFAULT_CATEGORY));
845
+ }
846
+ const single = metadata?.category;
847
+ if (typeof single === "string" && single.trim()) {
848
+ return [sanitizeSegment(single, DEFAULT_CATEGORY)];
849
+ }
850
+ return [sanitizeSegment(eventType, DEFAULT_CATEGORY)];
851
+ }
852
+ function buildMirrorPath(rootDir, event) {
853
+ const metadata = event.metadata;
854
+ const namespace = sanitizeSegment(metadata?.namespace, DEFAULT_NAMESPACE);
855
+ const categories = getCategorySegments(metadata, event.eventType);
856
+ const d = event.timestamp;
857
+ const yyyy = d.getFullYear();
858
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
859
+ const dd = String(d.getDate()).padStart(2, "0");
860
+ return path.join(rootDir, "memory", namespace, ...categories, `${yyyy}-${mm}-${dd}.md`);
861
+ }
862
+ function formatMirrorEntry(event) {
863
+ const category = Array.isArray(event.metadata?.categoryPath) ? event.metadata.categoryPath.join("/") : String(event.metadata?.category ?? event.eventType);
864
+ return [
865
+ "",
866
+ `- ts: ${event.timestamp.toISOString()}`,
867
+ ` id: ${event.id}`,
868
+ ` type: ${event.eventType}`,
869
+ ` session: ${event.sessionId}`,
870
+ ` category: ${category}`,
871
+ " content: |",
872
+ ...event.content.split("\n").map((line) => ` ${line}`)
873
+ ].join("\n") + "\n";
874
+ }
875
+ var MarkdownMirror = class {
876
+ constructor(rootDir) {
877
+ this.rootDir = rootDir;
878
+ }
879
+ async append(event) {
880
+ const outPath = buildMirrorPath(this.rootDir, event);
881
+ await fs2.mkdir(path.dirname(outPath), { recursive: true });
882
+ await fs2.appendFile(outPath, formatMirrorEntry(event), "utf8");
883
+ return outPath;
884
+ }
885
+ };
886
+
800
887
  // src/core/sqlite-event-store.ts
801
888
  var SQLiteEventStore = class {
802
889
  constructor(dbPath, options) {
@@ -806,10 +893,12 @@ var SQLiteEventStore = class {
806
893
  readonly: this.readOnly,
807
894
  walMode: !this.readOnly
808
895
  });
896
+ this.markdownMirror = this.readOnly || !options?.markdownMirrorRoot ? null : new MarkdownMirror(options.markdownMirrorRoot);
809
897
  }
810
898
  db;
811
899
  initialized = false;
812
900
  readOnly;
901
+ markdownMirror;
813
902
  /**
814
903
  * Initialize database schema
815
904
  */
@@ -1016,6 +1105,17 @@ var SQLiteEventStore = class {
1016
1105
  created_at TEXT DEFAULT (datetime('now'))
1017
1106
  );
1018
1107
 
1108
+ -- Consolidated Rules table (long-term stable memory)
1109
+ CREATE TABLE IF NOT EXISTS consolidated_rules (
1110
+ rule_id TEXT PRIMARY KEY,
1111
+ rule TEXT NOT NULL,
1112
+ topics TEXT,
1113
+ source_memory_ids TEXT,
1114
+ source_events TEXT,
1115
+ confidence REAL DEFAULT 0.5,
1116
+ created_at TEXT DEFAULT (datetime('now'))
1117
+ );
1118
+
1019
1119
  -- Endless Mode Config table
1020
1120
  CREATE TABLE IF NOT EXISTS endless_config (
1021
1121
  key TEXT PRIMARY KEY,
@@ -1023,6 +1123,41 @@ var SQLiteEventStore = class {
1023
1123
  updated_at TEXT DEFAULT (datetime('now'))
1024
1124
  );
1025
1125
 
1126
+ -- Memory Helpfulness tracking
1127
+ CREATE TABLE IF NOT EXISTS memory_helpfulness (
1128
+ id TEXT PRIMARY KEY,
1129
+ event_id TEXT NOT NULL,
1130
+ session_id TEXT NOT NULL,
1131
+ retrieval_score REAL DEFAULT 0,
1132
+ query_preview TEXT,
1133
+ session_continued INTEGER DEFAULT 0,
1134
+ prompt_count_after INTEGER DEFAULT 0,
1135
+ tool_success_count INTEGER DEFAULT 0,
1136
+ tool_total_count INTEGER DEFAULT 0,
1137
+ was_reasked INTEGER DEFAULT 0,
1138
+ helpfulness_score REAL DEFAULT 0.5,
1139
+ created_at TEXT DEFAULT (datetime('now')),
1140
+ measured_at TEXT
1141
+ );
1142
+
1143
+ -- Retrieval trace log (query -> candidates -> selected for context)
1144
+ CREATE TABLE IF NOT EXISTS retrieval_traces (
1145
+ trace_id TEXT PRIMARY KEY,
1146
+ session_id TEXT,
1147
+ project_hash TEXT,
1148
+ query_text TEXT NOT NULL,
1149
+ strategy TEXT,
1150
+ candidate_event_ids TEXT,
1151
+ selected_event_ids TEXT,
1152
+ candidate_details_json TEXT,
1153
+ selected_details_json TEXT,
1154
+ candidate_count INTEGER DEFAULT 0,
1155
+ selected_count INTEGER DEFAULT 0,
1156
+ confidence TEXT,
1157
+ fallback_trace TEXT,
1158
+ created_at TEXT DEFAULT (datetime('now'))
1159
+ );
1160
+
1026
1161
  -- Sync position tracking (for SQLite -> DuckDB sync)
1027
1162
  CREATE TABLE IF NOT EXISTS sync_positions (
1028
1163
  target_name TEXT PRIMARY KEY,
@@ -1047,7 +1182,14 @@ var SQLiteEventStore = class {
1047
1182
  CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score);
1048
1183
  CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
1049
1184
  CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
1185
+ CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence);
1050
1186
  CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
1187
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
1188
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
1189
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
1190
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_created_at ON retrieval_traces(created_at DESC);
1191
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_project_hash ON retrieval_traces(project_hash);
1192
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_session_id ON retrieval_traces(session_id);
1051
1193
 
1052
1194
  -- FTS5 Full-Text Search for fast keyword search
1053
1195
  CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
@@ -1071,6 +1213,14 @@ var SQLiteEventStore = class {
1071
1213
  INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
1072
1214
  END;
1073
1215
  `);
1216
+ try {
1217
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN selected_details_json TEXT;`);
1218
+ } catch {
1219
+ }
1220
+ try {
1221
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN candidate_details_json TEXT;`);
1222
+ } catch {
1223
+ }
1074
1224
  const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
1075
1225
  const columnNames = tableInfo.map((col) => col.name);
1076
1226
  if (!columnNames.includes("access_count")) {
@@ -1091,6 +1241,15 @@ var SQLiteEventStore = class {
1091
1241
  console.error("Error adding last_accessed_at column:", err);
1092
1242
  }
1093
1243
  }
1244
+ if (!columnNames.includes("turn_id")) {
1245
+ try {
1246
+ sqliteExec(this.db, `
1247
+ ALTER TABLE events ADD COLUMN turn_id TEXT;
1248
+ `);
1249
+ } catch (err) {
1250
+ console.error("Error adding turn_id column:", err);
1251
+ }
1252
+ }
1094
1253
  try {
1095
1254
  sqliteExec(this.db, `
1096
1255
  CREATE INDEX IF NOT EXISTS idx_events_access_count ON events(access_count DESC);
@@ -1103,6 +1262,12 @@ var SQLiteEventStore = class {
1103
1262
  `);
1104
1263
  } catch (err) {
1105
1264
  }
1265
+ try {
1266
+ sqliteExec(this.db, `
1267
+ CREATE INDEX IF NOT EXISTS idx_events_turn_id ON events(turn_id);
1268
+ `);
1269
+ } catch (err) {
1270
+ }
1106
1271
  this.initialized = true;
1107
1272
  }
1108
1273
  /**
@@ -1127,9 +1292,11 @@ var SQLiteEventStore = class {
1127
1292
  const id = randomUUID2();
1128
1293
  const timestamp = toSQLiteTimestamp(input.timestamp);
1129
1294
  try {
1295
+ const metadata = input.metadata || {};
1296
+ const turnId = metadata.turnId || null;
1130
1297
  const insertEvent = this.db.prepare(`
1131
- INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
1132
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1298
+ INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
1299
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1133
1300
  `);
1134
1301
  const insertDedup = this.db.prepare(`
1135
1302
  INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
@@ -1146,12 +1313,28 @@ var SQLiteEventStore = class {
1146
1313
  input.content,
1147
1314
  canonicalKey,
1148
1315
  dedupeKey,
1149
- JSON.stringify(input.metadata || {})
1316
+ JSON.stringify(metadata),
1317
+ turnId
1150
1318
  );
1151
1319
  insertDedup.run(dedupeKey, id);
1152
1320
  insertLevel.run(id);
1153
1321
  });
1154
1322
  transaction();
1323
+ if (this.markdownMirror) {
1324
+ const event = {
1325
+ id,
1326
+ eventType: input.eventType,
1327
+ sessionId: input.sessionId,
1328
+ timestamp: input.timestamp,
1329
+ content: input.content,
1330
+ canonicalKey,
1331
+ dedupeKey,
1332
+ metadata
1333
+ };
1334
+ this.markdownMirror.append(event).catch((err) => {
1335
+ console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
1336
+ });
1337
+ }
1155
1338
  return { success: true, eventId: id, isDuplicate: false };
1156
1339
  } catch (error) {
1157
1340
  return {
@@ -1210,6 +1393,92 @@ var SQLiteEventStore = class {
1210
1393
  );
1211
1394
  return rows.map(this.rowToEvent);
1212
1395
  }
1396
+ /**
1397
+ * Get events since a SQLite rowid (for robust incremental replication).
1398
+ * Rowid is monotonic for append-only tables, independent of client timestamps.
1399
+ */
1400
+ async getEventsSinceRowid(lastRowid, limit = 1e3) {
1401
+ await this.initialize();
1402
+ const rows = sqliteAll(
1403
+ this.db,
1404
+ `SELECT rowid as _rowid, * FROM events WHERE rowid > ? ORDER BY rowid ASC LIMIT ?`,
1405
+ [lastRowid, limit]
1406
+ );
1407
+ return rows.map((row) => ({
1408
+ rowid: row._rowid,
1409
+ event: this.rowToEvent(row)
1410
+ }));
1411
+ }
1412
+ /**
1413
+ * Import events with fixed IDs (used for cross-machine replication).
1414
+ * Idempotent: skips if event id or dedupeKey already exists.
1415
+ *
1416
+ * NOTE: This bypasses the append() id generation to preserve stable IDs.
1417
+ */
1418
+ async importEvents(events) {
1419
+ if (events.length === 0)
1420
+ return { inserted: 0, skipped: 0 };
1421
+ if (this.readOnly)
1422
+ return { inserted: 0, skipped: events.length };
1423
+ await this.initialize();
1424
+ const getById = this.db.prepare(`SELECT id FROM events WHERE id = ?`);
1425
+ const getByDedupe = this.db.prepare(`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`);
1426
+ const insertEvent = this.db.prepare(`
1427
+ INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
1428
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1429
+ `);
1430
+ const insertDedup = this.db.prepare(`
1431
+ INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
1432
+ `);
1433
+ const insertLevel = this.db.prepare(`
1434
+ INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')
1435
+ `);
1436
+ let inserted = 0;
1437
+ let skipped = 0;
1438
+ const insertedEvents = [];
1439
+ const tx = this.db.transaction((batch) => {
1440
+ for (const ev of batch) {
1441
+ const existingById = getById.get(ev.id);
1442
+ if (existingById) {
1443
+ skipped++;
1444
+ continue;
1445
+ }
1446
+ const canonicalKey = ev.canonicalKey || makeCanonicalKey(ev.content);
1447
+ const dedupeKey = ev.dedupeKey || makeDedupeKey(ev.content, ev.sessionId);
1448
+ const existingByDedupe = getByDedupe.get(dedupeKey);
1449
+ if (existingByDedupe) {
1450
+ skipped++;
1451
+ continue;
1452
+ }
1453
+ const metadata = ev.metadata || {};
1454
+ const turnId = metadata.turnId;
1455
+ insertEvent.run(
1456
+ ev.id,
1457
+ ev.eventType,
1458
+ ev.sessionId,
1459
+ toSQLiteTimestamp(ev.timestamp),
1460
+ ev.content,
1461
+ canonicalKey,
1462
+ dedupeKey,
1463
+ JSON.stringify(metadata),
1464
+ turnId ?? null
1465
+ );
1466
+ insertDedup.run(dedupeKey, ev.id);
1467
+ insertLevel.run(ev.id);
1468
+ inserted++;
1469
+ insertedEvents.push(ev);
1470
+ }
1471
+ });
1472
+ tx(events);
1473
+ if (this.markdownMirror && insertedEvents.length > 0) {
1474
+ for (const ev of insertedEvents) {
1475
+ this.markdownMirror.append(ev).catch((err) => {
1476
+ console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
1477
+ });
1478
+ }
1479
+ }
1480
+ return { inserted, skipped };
1481
+ }
1213
1482
  /**
1214
1483
  * Create or update session
1215
1484
  */
@@ -1372,6 +1641,35 @@ var SQLiteEventStore = class {
1372
1641
  [error, ...ids]
1373
1642
  );
1374
1643
  }
1644
+ /**
1645
+ * Get embedding/vector outbox health statistics
1646
+ */
1647
+ async getOutboxStats() {
1648
+ await this.initialize();
1649
+ const embeddingRows = sqliteAll(
1650
+ this.db,
1651
+ `SELECT status, COUNT(*) as count FROM embedding_outbox GROUP BY status`
1652
+ );
1653
+ const vectorRows = sqliteAll(
1654
+ this.db,
1655
+ `SELECT status, COUNT(*) as count FROM vector_outbox GROUP BY status`
1656
+ );
1657
+ const fromRows = (rows) => {
1658
+ const out = { pending: 0, processing: 0, failed: 0, total: 0 };
1659
+ for (const row of rows) {
1660
+ const key = row.status;
1661
+ if (key === "pending" || key === "processing" || key === "failed") {
1662
+ out[key] += row.count;
1663
+ }
1664
+ out.total += row.count;
1665
+ }
1666
+ return out;
1667
+ };
1668
+ return {
1669
+ embedding: fromRows(embeddingRows),
1670
+ vector: fromRows(vectorRows)
1671
+ };
1672
+ }
1375
1673
  /**
1376
1674
  * Update memory level
1377
1675
  */
@@ -1496,11 +1794,11 @@ var SQLiteEventStore = class {
1496
1794
  );
1497
1795
  }
1498
1796
  /**
1499
- * Get most accessed memories
1797
+ * Get most accessed memories (falls back to recent events if none accessed)
1500
1798
  */
1501
1799
  async getMostAccessed(limit = 10) {
1502
1800
  await this.initialize();
1503
- const rows = sqliteAll(
1801
+ let rows = sqliteAll(
1504
1802
  this.db,
1505
1803
  `SELECT * FROM events
1506
1804
  WHERE access_count > 0
@@ -1508,8 +1806,166 @@ var SQLiteEventStore = class {
1508
1806
  LIMIT ?`,
1509
1807
  [limit]
1510
1808
  );
1809
+ if (rows.length === 0) {
1810
+ rows = sqliteAll(
1811
+ this.db,
1812
+ `SELECT * FROM events
1813
+ ORDER BY timestamp DESC
1814
+ LIMIT ?`,
1815
+ [limit]
1816
+ );
1817
+ }
1511
1818
  return rows.map((row) => this.rowToEvent(row));
1512
1819
  }
1820
+ /**
1821
+ * Record a memory retrieval for helpfulness tracking
1822
+ */
1823
+ async recordRetrieval(eventId, sessionId, score, query) {
1824
+ if (this.readOnly)
1825
+ return;
1826
+ await this.initialize();
1827
+ const id = randomUUID2();
1828
+ sqliteRun(
1829
+ this.db,
1830
+ `INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
1831
+ VALUES (?, ?, ?, ?, ?, datetime('now'))`,
1832
+ [id, eventId, sessionId, score, query.slice(0, 100)]
1833
+ );
1834
+ }
1835
+ /**
1836
+ * Evaluate helpfulness for all retrievals in a session
1837
+ * Called at session end - uses behavioral signals to compute score
1838
+ */
1839
+ async evaluateSessionHelpfulness(sessionId) {
1840
+ if (this.readOnly)
1841
+ return;
1842
+ await this.initialize();
1843
+ const retrievals = sqliteAll(
1844
+ this.db,
1845
+ `SELECT * FROM memory_helpfulness WHERE session_id = ? AND measured_at IS NULL`,
1846
+ [sessionId]
1847
+ );
1848
+ if (retrievals.length === 0)
1849
+ return;
1850
+ const sessionEvents = sqliteAll(
1851
+ this.db,
1852
+ `SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
1853
+ [sessionId]
1854
+ );
1855
+ const promptEvents = sessionEvents.filter((e) => e.event_type === "user_prompt");
1856
+ const toolEvents = sessionEvents.filter((e) => e.event_type === "tool_observation");
1857
+ let toolSuccessCount = 0;
1858
+ let toolTotalCount = toolEvents.length;
1859
+ for (const t of toolEvents) {
1860
+ try {
1861
+ const content = JSON.parse(t.content);
1862
+ if (content.success !== false)
1863
+ toolSuccessCount++;
1864
+ } catch {
1865
+ toolSuccessCount++;
1866
+ }
1867
+ }
1868
+ const toolSuccessRatio = toolTotalCount > 0 ? toolSuccessCount / toolTotalCount : 0.5;
1869
+ for (const retrieval of retrievals) {
1870
+ const retrievalTime = retrieval.created_at;
1871
+ const eventsAfter = sessionEvents.filter((e) => e.timestamp > retrievalTime);
1872
+ const sessionContinued = eventsAfter.length > 0 ? 1 : 0;
1873
+ const promptsAfter = promptEvents.filter((e) => e.timestamp > retrievalTime);
1874
+ const promptCountAfter = promptsAfter.length;
1875
+ const queryWords = new Set((retrieval.query_preview || "").toLowerCase().split(/\s+/).filter((w) => w.length > 2));
1876
+ let wasReasked = 0;
1877
+ for (const p of promptsAfter) {
1878
+ const pWords = new Set(p.content.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
1879
+ let overlap = 0;
1880
+ for (const w of queryWords) {
1881
+ if (pWords.has(w))
1882
+ overlap++;
1883
+ }
1884
+ if (queryWords.size > 0 && overlap / queryWords.size > 0.5) {
1885
+ wasReasked = 1;
1886
+ break;
1887
+ }
1888
+ }
1889
+ const retrievalScore = retrieval.retrieval_score || 0;
1890
+ const helpfulnessScore = 0.3 * Math.min(retrievalScore, 1) + 0.25 * (sessionContinued ? 1 : 0) + 0.25 * toolSuccessRatio + 0.2 * (wasReasked ? 0 : 1);
1891
+ sqliteRun(
1892
+ this.db,
1893
+ `UPDATE memory_helpfulness
1894
+ SET session_continued = ?, prompt_count_after = ?,
1895
+ tool_success_count = ?, tool_total_count = ?,
1896
+ was_reasked = ?, helpfulness_score = ?,
1897
+ measured_at = datetime('now')
1898
+ WHERE id = ?`,
1899
+ [
1900
+ sessionContinued,
1901
+ promptCountAfter,
1902
+ toolSuccessCount,
1903
+ toolTotalCount,
1904
+ wasReasked,
1905
+ helpfulnessScore,
1906
+ retrieval.id
1907
+ ]
1908
+ );
1909
+ }
1910
+ }
1911
+ /**
1912
+ * Get most helpful memories ranked by helpfulness score
1913
+ */
1914
+ async getHelpfulMemories(limit = 10) {
1915
+ await this.initialize();
1916
+ const rows = sqliteAll(
1917
+ this.db,
1918
+ `SELECT
1919
+ mh.event_id,
1920
+ AVG(mh.helpfulness_score) as avg_score,
1921
+ COUNT(*) as eval_count,
1922
+ e.content,
1923
+ e.access_count
1924
+ FROM memory_helpfulness mh
1925
+ JOIN events e ON e.id = mh.event_id
1926
+ WHERE mh.measured_at IS NOT NULL
1927
+ GROUP BY mh.event_id
1928
+ ORDER BY avg_score DESC
1929
+ LIMIT ?`,
1930
+ [limit]
1931
+ );
1932
+ return rows.map((r) => ({
1933
+ eventId: r.event_id,
1934
+ summary: r.content.substring(0, 200) + (r.content.length > 200 ? "..." : ""),
1935
+ helpfulnessScore: Math.round(r.avg_score * 100) / 100,
1936
+ accessCount: r.access_count || 0,
1937
+ evaluationCount: r.eval_count
1938
+ }));
1939
+ }
1940
+ /**
1941
+ * Get helpfulness statistics for dashboard
1942
+ */
1943
+ async getHelpfulnessStats() {
1944
+ await this.initialize();
1945
+ const stats = sqliteGet(
1946
+ this.db,
1947
+ `SELECT
1948
+ AVG(helpfulness_score) as avg_score,
1949
+ COUNT(*) as total_evaluated,
1950
+ SUM(CASE WHEN helpfulness_score >= 0.7 THEN 1 ELSE 0 END) as helpful,
1951
+ SUM(CASE WHEN helpfulness_score >= 0.4 AND helpfulness_score < 0.7 THEN 1 ELSE 0 END) as neutral,
1952
+ SUM(CASE WHEN helpfulness_score < 0.4 THEN 1 ELSE 0 END) as unhelpful
1953
+ FROM memory_helpfulness
1954
+ WHERE measured_at IS NOT NULL`
1955
+ );
1956
+ const totalRow = sqliteGet(
1957
+ this.db,
1958
+ `SELECT COUNT(*) as total FROM memory_helpfulness`
1959
+ );
1960
+ return {
1961
+ avgScore: Math.round((stats?.avg_score || 0) * 100) / 100,
1962
+ totalEvaluated: stats?.total_evaluated || 0,
1963
+ totalRetrievals: totalRow?.total || 0,
1964
+ helpful: stats?.helpful || 0,
1965
+ neutral: stats?.neutral || 0,
1966
+ unhelpful: stats?.unhelpful || 0
1967
+ };
1968
+ }
1513
1969
  /**
1514
1970
  * Fast keyword search using FTS5
1515
1971
  * Returns events matching the search query, ranked by relevance
@@ -1572,12 +2028,222 @@ var SQLiteEventStore = class {
1572
2028
  getDatabase() {
1573
2029
  return this.db;
1574
2030
  }
2031
+ async recordRetrievalTrace(input) {
2032
+ await this.initialize();
2033
+ const traceId = randomUUID2();
2034
+ sqliteRun(
2035
+ this.db,
2036
+ `INSERT INTO retrieval_traces (
2037
+ trace_id, session_id, project_hash, query_text, strategy,
2038
+ candidate_event_ids, selected_event_ids, candidate_details_json, selected_details_json,
2039
+ candidate_count, selected_count, confidence, fallback_trace
2040
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2041
+ [
2042
+ traceId,
2043
+ input.sessionId || null,
2044
+ input.projectHash || null,
2045
+ input.queryText,
2046
+ input.strategy || null,
2047
+ JSON.stringify(input.candidateEventIds || []),
2048
+ JSON.stringify(input.selectedEventIds || []),
2049
+ JSON.stringify(input.candidateDetails || []),
2050
+ JSON.stringify(input.selectedDetails || []),
2051
+ (input.candidateEventIds || []).length,
2052
+ (input.selectedEventIds || []).length,
2053
+ input.confidence || null,
2054
+ JSON.stringify(input.fallbackTrace || [])
2055
+ ]
2056
+ );
2057
+ }
2058
+ async getRecentRetrievalTraces(limit = 50) {
2059
+ await this.initialize();
2060
+ const rows = sqliteAll(
2061
+ this.db,
2062
+ `SELECT * FROM retrieval_traces ORDER BY created_at DESC LIMIT ?`,
2063
+ [limit]
2064
+ );
2065
+ return rows.map((row) => ({
2066
+ traceId: row.trace_id,
2067
+ sessionId: row.session_id || void 0,
2068
+ projectHash: row.project_hash || void 0,
2069
+ queryText: row.query_text,
2070
+ strategy: row.strategy || void 0,
2071
+ candidateEventIds: row.candidate_event_ids ? JSON.parse(row.candidate_event_ids) : [],
2072
+ selectedEventIds: row.selected_event_ids ? JSON.parse(row.selected_event_ids) : [],
2073
+ candidateDetails: row.candidate_details_json ? JSON.parse(row.candidate_details_json) : [],
2074
+ selectedDetails: row.selected_details_json ? JSON.parse(row.selected_details_json) : [],
2075
+ candidateCount: Number(row.candidate_count || 0),
2076
+ selectedCount: Number(row.selected_count || 0),
2077
+ confidence: row.confidence || void 0,
2078
+ fallbackTrace: row.fallback_trace ? JSON.parse(row.fallback_trace) : [],
2079
+ createdAt: toDateFromSQLite(row.created_at)
2080
+ }));
2081
+ }
2082
+ async getRetrievalTraceStats() {
2083
+ await this.initialize();
2084
+ const row = sqliteGet(
2085
+ this.db,
2086
+ `SELECT
2087
+ COUNT(*) as total_queries,
2088
+ AVG(candidate_count) as avg_candidate_count,
2089
+ AVG(selected_count) as avg_selected_count,
2090
+ CASE
2091
+ WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
2092
+ ELSE 0
2093
+ END as selection_rate
2094
+ FROM retrieval_traces`,
2095
+ []
2096
+ );
2097
+ return {
2098
+ totalQueries: Number(row?.total_queries || 0),
2099
+ avgCandidateCount: Number(row?.avg_candidate_count || 0),
2100
+ avgSelectedCount: Number(row?.avg_selected_count || 0),
2101
+ selectionRate: Number(row?.selection_rate || 0)
2102
+ };
2103
+ }
1575
2104
  /**
1576
2105
  * Close database connection
1577
2106
  */
1578
2107
  async close() {
1579
2108
  sqliteClose(this.db);
1580
2109
  }
2110
+ /**
2111
+ * Get events grouped by turn_id for a session
2112
+ * Returns turns ordered by first event timestamp (newest first)
2113
+ */
2114
+ async getSessionTurns(sessionId, options) {
2115
+ await this.initialize();
2116
+ const limit = options?.limit || 20;
2117
+ const offset = options?.offset || 0;
2118
+ const turnRows = sqliteAll(
2119
+ this.db,
2120
+ `SELECT turn_id, MIN(timestamp) as min_ts
2121
+ FROM events
2122
+ WHERE session_id = ? AND turn_id IS NOT NULL
2123
+ GROUP BY turn_id
2124
+ ORDER BY min_ts DESC
2125
+ LIMIT ? OFFSET ?`,
2126
+ [sessionId, limit, offset]
2127
+ );
2128
+ const turns = [];
2129
+ for (const turnRow of turnRows) {
2130
+ const events = await this.getEventsByTurn(turnRow.turn_id);
2131
+ const promptEvent = events.find((e) => e.eventType === "user_prompt");
2132
+ const toolEvents = events.filter((e) => e.eventType === "tool_observation");
2133
+ const hasResponse = events.some((e) => e.eventType === "agent_response");
2134
+ turns.push({
2135
+ turnId: turnRow.turn_id,
2136
+ events,
2137
+ startedAt: toDateFromSQLite(turnRow.min_ts),
2138
+ promptPreview: promptEvent ? promptEvent.content.slice(0, 200) + (promptEvent.content.length > 200 ? "..." : "") : "(no prompt)",
2139
+ eventCount: events.length,
2140
+ toolCount: toolEvents.length,
2141
+ hasResponse
2142
+ });
2143
+ }
2144
+ return turns;
2145
+ }
2146
+ /**
2147
+ * Get all events for a specific turn_id
2148
+ */
2149
+ async getEventsByTurn(turnId) {
2150
+ await this.initialize();
2151
+ const rows = sqliteAll(
2152
+ this.db,
2153
+ `SELECT * FROM events WHERE turn_id = ? ORDER BY timestamp ASC`,
2154
+ [turnId]
2155
+ );
2156
+ return rows.map(this.rowToEvent);
2157
+ }
2158
+ /**
2159
+ * Count total turns for a session
2160
+ */
2161
+ async countSessionTurns(sessionId) {
2162
+ await this.initialize();
2163
+ const row = sqliteGet(
2164
+ this.db,
2165
+ `SELECT COUNT(DISTINCT turn_id) as count
2166
+ FROM events
2167
+ WHERE session_id = ? AND turn_id IS NOT NULL`,
2168
+ [sessionId]
2169
+ );
2170
+ return row?.count || 0;
2171
+ }
2172
+ /**
2173
+ * Migrate existing events: backfill turn_id for events that have turnId in metadata
2174
+ * but no turn_id column value (for events stored before this migration)
2175
+ */
2176
+ async backfillTurnIds() {
2177
+ await this.initialize();
2178
+ const rows = sqliteAll(
2179
+ this.db,
2180
+ `SELECT id, metadata FROM events
2181
+ WHERE turn_id IS NULL AND metadata IS NOT NULL AND metadata LIKE '%turnId%'`
2182
+ );
2183
+ let updated = 0;
2184
+ for (const row of rows) {
2185
+ try {
2186
+ const metadata = JSON.parse(row.metadata);
2187
+ if (metadata.turnId) {
2188
+ sqliteRun(
2189
+ this.db,
2190
+ `UPDATE events SET turn_id = ? WHERE id = ?`,
2191
+ [metadata.turnId, row.id]
2192
+ );
2193
+ updated++;
2194
+ }
2195
+ } catch {
2196
+ }
2197
+ }
2198
+ return updated;
2199
+ }
2200
+ /**
2201
+ * Delete all events for a session (for force reimport)
2202
+ */
2203
+ async deleteSessionEvents(sessionId) {
2204
+ await this.initialize();
2205
+ const events = sqliteAll(
2206
+ this.db,
2207
+ `SELECT id FROM events WHERE session_id = ?`,
2208
+ [sessionId]
2209
+ );
2210
+ if (events.length === 0)
2211
+ return 0;
2212
+ const eventIds = events.map((e) => e.id);
2213
+ const placeholders = eventIds.map(() => "?").join(",");
2214
+ const ftsTriggersDropped = [];
2215
+ for (const triggerName of ["events_fts_delete", "events_fts_update", "events_fts_insert"]) {
2216
+ try {
2217
+ sqliteRun(this.db, `DROP TRIGGER IF EXISTS ${triggerName}`);
2218
+ ftsTriggersDropped.push(triggerName);
2219
+ } catch {
2220
+ }
2221
+ }
2222
+ for (const table of ["event_dedup", "memory_levels", "embedding_queue", "embedding_outbox", "vector_outbox"]) {
2223
+ try {
2224
+ sqliteRun(this.db, `DELETE FROM ${table} WHERE event_id IN (${placeholders})`, eventIds);
2225
+ } catch {
2226
+ }
2227
+ }
2228
+ const result = sqliteRun(this.db, `DELETE FROM events WHERE session_id = ?`, [sessionId]);
2229
+ if (ftsTriggersDropped.length > 0) {
2230
+ try {
2231
+ sqliteRun(this.db, `INSERT INTO events_fts(events_fts) VALUES('rebuild')`);
2232
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_insert AFTER INSERT ON events BEGIN
2233
+ INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
2234
+ END`);
2235
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_delete AFTER DELETE ON events BEGIN
2236
+ INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
2237
+ END`);
2238
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_update AFTER UPDATE ON events BEGIN
2239
+ INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
2240
+ INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
2241
+ END`);
2242
+ } catch {
2243
+ }
2244
+ }
2245
+ return result.changes || 0;
2246
+ }
1581
2247
  /**
1582
2248
  * Convert database row to MemoryEvent
1583
2249
  */
@@ -1598,6 +2264,9 @@ var SQLiteEventStore = class {
1598
2264
  if (row.last_accessed_at !== void 0) {
1599
2265
  event.last_accessed_at = row.last_accessed_at;
1600
2266
  }
2267
+ if (row.turn_id !== void 0 && row.turn_id !== null) {
2268
+ event.turn_id = row.turn_id;
2269
+ }
1601
2270
  return event;
1602
2271
  }
1603
2272
  };
@@ -1809,7 +2478,16 @@ var VectorStore = class {
1809
2478
  metadata: JSON.stringify(record.metadata || {})
1810
2479
  };
1811
2480
  if (!this.table) {
1812
- this.table = await this.db.createTable(this.tableName, [data]);
2481
+ try {
2482
+ this.table = await this.db.createTable(this.tableName, [data]);
2483
+ } catch (e) {
2484
+ if (e?.message?.includes("already exists")) {
2485
+ this.table = await this.db.openTable(this.tableName);
2486
+ await this.table.add([data]);
2487
+ } else {
2488
+ throw e;
2489
+ }
2490
+ }
1813
2491
  } else {
1814
2492
  await this.table.add([data]);
1815
2493
  }
@@ -1835,7 +2513,16 @@ var VectorStore = class {
1835
2513
  metadata: JSON.stringify(record.metadata || {})
1836
2514
  }));
1837
2515
  if (!this.table) {
1838
- this.table = await this.db.createTable(this.tableName, data);
2516
+ try {
2517
+ this.table = await this.db.createTable(this.tableName, data);
2518
+ } catch (e) {
2519
+ if (e?.message?.includes("already exists")) {
2520
+ this.table = await this.db.openTable(this.tableName);
2521
+ await this.table.add(data);
2522
+ } else {
2523
+ throw e;
2524
+ }
2525
+ }
1839
2526
  } else {
1840
2527
  await this.table.add(data);
1841
2528
  }
@@ -2275,7 +2962,20 @@ var DEFAULT_OPTIONS = {
2275
2962
  topK: 5,
2276
2963
  minScore: 0.7,
2277
2964
  maxTokens: 2e3,
2278
- includeSessionContext: true
2965
+ includeSessionContext: true,
2966
+ strategy: "auto",
2967
+ rerankWithKeyword: true,
2968
+ decayPolicy: {
2969
+ enabled: true,
2970
+ windowDays: 30,
2971
+ maxPenalty: 0.15
2972
+ },
2973
+ graphHop: {
2974
+ enabled: true,
2975
+ maxHops: 1,
2976
+ hopPenalty: 0.08
2977
+ },
2978
+ projectScopeMode: "global"
2279
2979
  };
2280
2980
  var Retriever = class {
2281
2981
  eventStore;
@@ -2285,6 +2985,7 @@ var Retriever = class {
2285
2985
  sharedStore;
2286
2986
  sharedVectorStore;
2287
2987
  graduation;
2988
+ queryRewriter;
2288
2989
  constructor(eventStore, vectorStore, embedder, matcher, sharedOptions) {
2289
2990
  this.eventStore = eventStore;
2290
2991
  this.vectorStore = vectorStore;
@@ -2293,47 +2994,105 @@ var Retriever = class {
2293
2994
  this.sharedStore = sharedOptions?.sharedStore;
2294
2995
  this.sharedVectorStore = sharedOptions?.sharedVectorStore;
2295
2996
  }
2296
- /**
2297
- * Set graduation pipeline for access tracking
2298
- */
2299
2997
  setGraduationPipeline(graduation) {
2300
2998
  this.graduation = graduation;
2301
2999
  }
2302
- /**
2303
- * Set shared stores after construction
2304
- */
2305
3000
  setSharedStores(sharedStore, sharedVectorStore) {
2306
3001
  this.sharedStore = sharedStore;
2307
3002
  this.sharedVectorStore = sharedVectorStore;
2308
3003
  }
2309
- /**
2310
- * Retrieve relevant memories for a query
2311
- */
3004
+ setQueryRewriter(rewriter) {
3005
+ this.queryRewriter = rewriter;
3006
+ }
2312
3007
  async retrieve(query, options = {}) {
2313
3008
  const opts = { ...DEFAULT_OPTIONS, ...options };
2314
- const queryEmbedding = await this.embedder.embed(query);
2315
- const searchResults = await this.vectorStore.search(queryEmbedding.vector, {
2316
- limit: opts.topK * 2,
2317
- // Get extra for filtering
3009
+ const sessionFilter = opts.scope?.sessionId ?? opts.sessionId;
3010
+ const fallbackTrace = [];
3011
+ const fallbackEnabled = (opts.strategy ?? "auto") === "auto";
3012
+ const primaryStrategy = opts.strategy === "auto" ? "fast" : opts.strategy || "fast";
3013
+ let current = await this.runStage(query, {
3014
+ strategy: primaryStrategy,
3015
+ topK: opts.topK,
2318
3016
  minScore: opts.minScore,
2319
- sessionId: opts.sessionId
3017
+ sessionId: sessionFilter,
3018
+ scope: opts.scope,
3019
+ rerankWithKeyword: opts.rerankWithKeyword !== false,
3020
+ rerankWeights: opts.rerankWeights,
3021
+ decayPolicy: opts.decayPolicy,
3022
+ intentRewrite: opts.intentRewrite === true,
3023
+ graphHop: opts.graphHop,
3024
+ projectScopeMode: opts.projectScopeMode,
3025
+ projectHash: opts.projectHash,
3026
+ allowedProjectHashes: opts.allowedProjectHashes
2320
3027
  });
2321
- const matchResult = this.matcher.matchSearchResults(
2322
- searchResults,
2323
- (eventId) => this.getEventAgeDays(eventId)
2324
- );
2325
- const memories = await this.enrichResults(searchResults.slice(0, opts.topK), opts);
3028
+ fallbackTrace.push(`stage:primary:${primaryStrategy}`);
3029
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results) && primaryStrategy !== "deep") {
3030
+ current = await this.runStage(query, {
3031
+ strategy: "deep",
3032
+ topK: opts.topK,
3033
+ minScore: opts.minScore,
3034
+ sessionId: sessionFilter,
3035
+ scope: opts.scope,
3036
+ rerankWithKeyword: opts.rerankWithKeyword !== false,
3037
+ rerankWeights: opts.rerankWeights,
3038
+ decayPolicy: opts.decayPolicy,
3039
+ graphHop: opts.graphHop,
3040
+ projectScopeMode: opts.projectScopeMode,
3041
+ projectHash: opts.projectHash,
3042
+ allowedProjectHashes: opts.allowedProjectHashes
3043
+ });
3044
+ fallbackTrace.push("fallback:deep");
3045
+ }
3046
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
3047
+ current = await this.runStage(query, {
3048
+ strategy: "deep",
3049
+ topK: opts.topK,
3050
+ minScore: Math.max(0.5, opts.minScore - 0.15),
3051
+ sessionId: void 0,
3052
+ scope: void 0,
3053
+ rerankWithKeyword: true,
3054
+ rerankWeights: opts.rerankWeights,
3055
+ decayPolicy: opts.decayPolicy,
3056
+ graphHop: opts.graphHop,
3057
+ projectScopeMode: opts.projectScopeMode,
3058
+ projectHash: opts.projectHash,
3059
+ allowedProjectHashes: opts.allowedProjectHashes
3060
+ });
3061
+ fallbackTrace.push("fallback:scope-expanded");
3062
+ }
3063
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
3064
+ const summary = await this.buildSummaryFallback(query, opts.topK);
3065
+ current = {
3066
+ results: summary,
3067
+ candidateResults: summary,
3068
+ matchResult: this.matcher.matchSearchResults(summary, () => 0)
3069
+ };
3070
+ fallbackTrace.push("fallback:summary");
3071
+ }
3072
+ const memories = await this.enrichResults(current.results.slice(0, opts.topK), opts);
2326
3073
  const context = this.buildContext(memories, opts.maxTokens);
2327
3074
  return {
2328
3075
  memories,
2329
- matchResult,
3076
+ matchResult: current.matchResult,
2330
3077
  totalTokens: this.estimateTokens(context),
2331
- context
3078
+ context,
3079
+ fallbackTrace,
3080
+ selectedDebug: current.results.slice(0, opts.topK).map((r) => ({
3081
+ eventId: r.eventId,
3082
+ score: r.score,
3083
+ semanticScore: r.semanticScore,
3084
+ lexicalScore: r.lexicalScore,
3085
+ recencyScore: r.recencyScore
3086
+ })),
3087
+ candidateDebug: (current.candidateResults || []).slice(0, Math.max(opts.topK * 3, 20)).map((r) => ({
3088
+ eventId: r.eventId,
3089
+ score: r.score,
3090
+ semanticScore: r.semanticScore,
3091
+ lexicalScore: r.lexicalScore,
3092
+ recencyScore: r.recencyScore
3093
+ }))
2332
3094
  };
2333
3095
  }
2334
- /**
2335
- * Retrieve with unified search (project + shared)
2336
- */
2337
3096
  async retrieveUnified(query, options = {}) {
2338
3097
  const projectResult = await this.retrieve(query, options);
2339
3098
  if (!options.includeShared || !this.sharedStore || !this.sharedVectorStore) {
@@ -2341,22 +3100,19 @@ var Retriever = class {
2341
3100
  }
2342
3101
  try {
2343
3102
  const queryEmbedding = await this.embedder.embed(query);
2344
- const sharedVectorResults = await this.sharedVectorStore.search(
2345
- queryEmbedding.vector,
2346
- {
2347
- limit: options.topK || 5,
2348
- minScore: options.minScore || 0.7,
2349
- excludeProjectHash: options.projectHash
2350
- }
2351
- );
3103
+ const sharedVectorResults = await this.sharedVectorStore.search(queryEmbedding.vector, {
3104
+ limit: options.topK || 5,
3105
+ minScore: options.minScore || 0.7,
3106
+ excludeProjectHash: options.projectHash
3107
+ });
2352
3108
  const sharedMemories = [];
2353
3109
  for (const result of sharedVectorResults) {
2354
3110
  const entry = await this.sharedStore.get(result.entryId);
2355
- if (entry) {
2356
- if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
2357
- sharedMemories.push(entry);
2358
- await this.sharedStore.recordUsage(entry.entryId);
2359
- }
3111
+ if (!entry)
3112
+ continue;
3113
+ if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
3114
+ sharedMemories.push(entry);
3115
+ await this.sharedStore.recordUsage(entry.entryId);
2360
3116
  }
2361
3117
  }
2362
3118
  const unifiedContext = this.buildUnifiedContext(projectResult, sharedMemories);
@@ -2371,50 +3127,243 @@ var Retriever = class {
2371
3127
  return projectResult;
2372
3128
  }
2373
3129
  }
2374
- /**
2375
- * Build unified context combining project and shared memories
2376
- */
2377
- buildUnifiedContext(projectResult, sharedMemories) {
2378
- let context = projectResult.context;
2379
- if (sharedMemories.length > 0) {
2380
- context += "\n\n## Cross-Project Knowledge\n\n";
2381
- for (const memory of sharedMemories.slice(0, 3)) {
2382
- context += `### ${memory.title}
2383
- `;
2384
- if (memory.symptoms.length > 0) {
2385
- context += `**Symptoms:** ${memory.symptoms.join(", ")}
2386
- `;
2387
- }
2388
- context += `**Root Cause:** ${memory.rootCause}
2389
- `;
2390
- context += `**Solution:** ${memory.solution}
2391
- `;
2392
- if (memory.technologies && memory.technologies.length > 0) {
2393
- context += `**Technologies:** ${memory.technologies.join(", ")}
2394
- `;
3130
+ async runStage(query, input) {
3131
+ let initialResults = await this.searchByStrategy(query, {
3132
+ strategy: input.strategy,
3133
+ topK: input.topK,
3134
+ minScore: input.minScore,
3135
+ sessionId: input.sessionId
3136
+ });
3137
+ if (input.intentRewrite && input.strategy === "deep" && this.queryRewriter) {
3138
+ const rewritten = (await this.queryRewriter(query))?.trim();
3139
+ if (rewritten && rewritten !== query) {
3140
+ const rewrittenResults = await this.searchByStrategy(rewritten, {
3141
+ strategy: "deep",
3142
+ topK: input.topK,
3143
+ minScore: Math.max(0.5, input.minScore - 0.1),
3144
+ sessionId: input.sessionId
3145
+ });
3146
+ initialResults = this.mergeResults(initialResults, rewrittenResults, input.topK * 3);
3147
+ }
3148
+ }
3149
+ const expandedResults = input.graphHop?.enabled === false ? initialResults : await this.expandGraphHops(initialResults, {
3150
+ maxHops: Math.max(1, input.graphHop?.maxHops ?? 1),
3151
+ hopPenalty: Math.max(0, input.graphHop?.hopPenalty ?? 0.08),
3152
+ limit: input.topK * 4
3153
+ });
3154
+ const rerankedResults = input.rerankWithKeyword ? this.rerankByKeywordOverlap(expandedResults, query, input.rerankWeights, input.decayPolicy) : expandedResults;
3155
+ const filtered = await this.applyScopeFilters(rerankedResults, {
3156
+ scope: input.scope,
3157
+ projectScopeMode: input.projectScopeMode,
3158
+ projectHash: input.projectHash,
3159
+ allowedProjectHashes: input.allowedProjectHashes
3160
+ });
3161
+ const top = filtered.slice(0, input.topK);
3162
+ const matchResult = this.matcher.matchSearchResults(top, () => 0);
3163
+ return { results: top, candidateResults: filtered, matchResult };
3164
+ }
3165
+ mergeResults(primary, secondary, limit) {
3166
+ const byId = /* @__PURE__ */ new Map();
3167
+ for (const row of primary)
3168
+ byId.set(row.eventId, row);
3169
+ for (const row of secondary) {
3170
+ const prev = byId.get(row.eventId);
3171
+ if (!prev || row.score > prev.score) {
3172
+ byId.set(row.eventId, row);
3173
+ }
3174
+ }
3175
+ return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, limit);
3176
+ }
3177
+ async expandGraphHops(seeds, opts) {
3178
+ const byId = /* @__PURE__ */ new Map();
3179
+ for (const s of seeds)
3180
+ byId.set(s.eventId, s);
3181
+ let frontier = seeds.map((s) => ({ row: s, hop: 0 }));
3182
+ for (let hop = 1; hop <= opts.maxHops; hop += 1) {
3183
+ const next = [];
3184
+ for (const f of frontier) {
3185
+ const ev = await this.eventStore.getEvent(f.row.eventId);
3186
+ if (!ev)
3187
+ continue;
3188
+ const rel = ev.metadata?.relatedEventIds ?? [];
3189
+ const relatedIds = Array.isArray(rel) ? rel.filter((x) => typeof x === "string") : [];
3190
+ for (const rid of relatedIds) {
3191
+ if (byId.has(rid))
3192
+ continue;
3193
+ const target = await this.eventStore.getEvent(rid);
3194
+ if (!target)
3195
+ continue;
3196
+ const score = Math.max(0, f.row.score - opts.hopPenalty * hop);
3197
+ const row = {
3198
+ id: `hop-${hop}-${rid}`,
3199
+ eventId: target.id,
3200
+ content: target.content,
3201
+ score,
3202
+ sessionId: target.sessionId,
3203
+ eventType: target.eventType,
3204
+ timestamp: target.timestamp.toISOString()
3205
+ };
3206
+ byId.set(row.eventId, row);
3207
+ next.push({ row, hop });
3208
+ if (byId.size >= opts.limit)
3209
+ break;
2395
3210
  }
2396
- context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
2397
-
2398
- `;
3211
+ if (byId.size >= opts.limit)
3212
+ break;
2399
3213
  }
3214
+ frontier = next;
3215
+ if (frontier.length === 0 || byId.size >= opts.limit)
3216
+ break;
2400
3217
  }
2401
- return context;
3218
+ return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, opts.limit);
3219
+ }
3220
+ shouldFallback(matchResult, results) {
3221
+ if (results.length === 0)
3222
+ return true;
3223
+ if (matchResult.confidence === "none")
3224
+ return true;
3225
+ return false;
3226
+ }
3227
+ async buildSummaryFallback(query, topK) {
3228
+ const recent = await this.eventStore.getRecentEvents(Math.max(topK * 6, 20));
3229
+ const q = this.tokenize(query);
3230
+ const ranked = recent.map((e) => ({ e, overlap: this.keywordOverlap(q, this.tokenize(e.content)) })).filter((r) => r.overlap > 0).sort((a, b) => b.overlap - a.overlap).slice(0, topK).map((row, idx) => ({
3231
+ id: `summary-${row.e.id}`,
3232
+ eventId: row.e.id,
3233
+ content: row.e.content,
3234
+ score: Math.max(0.25, 0.6 - idx * 0.05),
3235
+ sessionId: row.e.sessionId,
3236
+ eventType: row.e.eventType,
3237
+ timestamp: row.e.timestamp.toISOString()
3238
+ }));
3239
+ return ranked;
3240
+ }
3241
+ async searchByStrategy(query, input) {
3242
+ const strategy = input.strategy === "auto" ? "deep" : input.strategy;
3243
+ if (strategy === "fast") {
3244
+ const keyword = await this.searchByKeyword(query, {
3245
+ limit: Math.max(5, input.topK * 3),
3246
+ sessionId: input.sessionId
3247
+ });
3248
+ return keyword;
3249
+ }
3250
+ const queryEmbedding = await this.embedder.embed(query);
3251
+ return this.vectorStore.search(queryEmbedding.vector, {
3252
+ limit: Math.max(5, input.topK * 3),
3253
+ minScore: input.minScore,
3254
+ sessionId: input.sessionId
3255
+ });
3256
+ }
3257
+ async searchByKeyword(query, input) {
3258
+ if (this.eventStore.keywordSearch) {
3259
+ const rows = await this.eventStore.keywordSearch(query, input.limit);
3260
+ const filtered2 = input.sessionId ? rows.filter((r) => r.event.sessionId === input.sessionId) : rows;
3261
+ return filtered2.map((row, idx) => ({
3262
+ id: `kw-${row.event.id}`,
3263
+ eventId: row.event.id,
3264
+ content: row.event.content,
3265
+ score: Math.max(0.4, 1 - idx * 0.04),
3266
+ sessionId: row.event.sessionId,
3267
+ eventType: row.event.eventType,
3268
+ timestamp: row.event.timestamp.toISOString()
3269
+ }));
3270
+ }
3271
+ const recent = await this.eventStore.getRecentEvents(input.limit * 4);
3272
+ const tokens = this.tokenize(query);
3273
+ const filtered = recent.filter((e) => input.sessionId ? e.sessionId === input.sessionId : true).map((e) => ({ e, overlap: this.keywordOverlap(tokens, this.tokenize(e.content)) })).filter((r) => r.overlap > 0).sort((a, b) => b.overlap - a.overlap).slice(0, input.limit);
3274
+ return filtered.map((row, idx) => ({
3275
+ id: `kw-fallback-${row.e.id}`,
3276
+ eventId: row.e.id,
3277
+ content: row.e.content,
3278
+ score: Math.max(0.3, 0.9 - idx * 0.05),
3279
+ sessionId: row.e.sessionId,
3280
+ eventType: row.e.eventType,
3281
+ timestamp: row.e.timestamp.toISOString()
3282
+ }));
3283
+ }
3284
+ rerankByKeywordOverlap(results, query, weights, decayPolicy) {
3285
+ const q = this.tokenize(query);
3286
+ const now = Date.now();
3287
+ const sw = Math.max(0, weights?.semantic ?? 0.7);
3288
+ const lw = Math.max(0, weights?.lexical ?? 0.2);
3289
+ const rw = Math.max(0, weights?.recency ?? 0.1);
3290
+ const total = sw + lw + rw || 1;
3291
+ const decayEnabled = decayPolicy?.enabled !== false;
3292
+ const decayWindow = Math.max(1, decayPolicy?.windowDays ?? 30);
3293
+ const decayMaxPenalty = Math.max(0, decayPolicy?.maxPenalty ?? 0.15);
3294
+ return [...results].map((r) => {
3295
+ const overlap = this.keywordOverlap(q, this.tokenize(r.content));
3296
+ const recencyDays = Math.max(0, (now - new Date(r.timestamp).getTime()) / (1e3 * 60 * 60 * 24));
3297
+ const recency = Math.max(0, 1 - recencyDays / decayWindow);
3298
+ let blended = (r.score * sw + overlap * lw + recency * rw) / total;
3299
+ if (decayEnabled && recencyDays > decayWindow && overlap < 0.5) {
3300
+ const ageFactor = Math.min(1, (recencyDays - decayWindow) / decayWindow);
3301
+ blended -= decayMaxPenalty * ageFactor;
3302
+ }
3303
+ return { ...r, score: Math.max(0, blended), semanticScore: r.score, lexicalScore: overlap, recencyScore: recency };
3304
+ }).sort((a, b) => b.score - a.score);
3305
+ }
3306
+ async applyScopeFilters(results, options) {
3307
+ const scope = options?.scope;
3308
+ const projectScopeMode = options?.projectScopeMode ?? "global";
3309
+ const allowedProjectHashes = new Set(
3310
+ [options?.projectHash, ...options?.allowedProjectHashes || []].filter(
3311
+ (value) => typeof value === "string" && value.length > 0
3312
+ )
3313
+ );
3314
+ if (!scope && projectScopeMode === "global")
3315
+ return results;
3316
+ const normalizedIncludes = (scope?.contentIncludes || []).map((s) => s.toLowerCase());
3317
+ const filtered = [];
3318
+ for (const result of results) {
3319
+ if (scope?.sessionId && result.sessionId !== scope.sessionId)
3320
+ continue;
3321
+ if (scope?.sessionIdPrefix && !result.sessionId.startsWith(scope.sessionIdPrefix))
3322
+ continue;
3323
+ if (scope?.eventTypes && scope.eventTypes.length > 0 && !scope.eventTypes.includes(result.eventType))
3324
+ continue;
3325
+ const event = await this.eventStore.getEvent(result.eventId);
3326
+ if (!event)
3327
+ continue;
3328
+ if (scope?.canonicalKeyPrefix && !event.canonicalKey.startsWith(scope.canonicalKeyPrefix))
3329
+ continue;
3330
+ if (normalizedIncludes.length > 0) {
3331
+ const lc = event.content.toLowerCase();
3332
+ if (!normalizedIncludes.some((needle) => lc.includes(needle)))
3333
+ continue;
3334
+ }
3335
+ if (scope?.metadata && !this.matchesMetadataScope(event.metadata, scope.metadata))
3336
+ continue;
3337
+ const projectHash = this.extractProjectHash(event.metadata);
3338
+ filtered.push({ result, projectHash });
3339
+ }
3340
+ if (projectScopeMode === "global" || allowedProjectHashes.size === 0) {
3341
+ return filtered.map((x) => x.result);
3342
+ }
3343
+ const projectMatched = filtered.filter((x) => x.projectHash && allowedProjectHashes.has(x.projectHash));
3344
+ if (projectScopeMode === "strict") {
3345
+ return projectMatched.map((x) => x.result);
3346
+ }
3347
+ return (projectMatched.length > 0 ? projectMatched : filtered).map((x) => x.result);
3348
+ }
3349
+ extractProjectHash(metadata) {
3350
+ if (!metadata || typeof metadata !== "object")
3351
+ return void 0;
3352
+ const scope = metadata.scope;
3353
+ if (!scope || typeof scope !== "object")
3354
+ return void 0;
3355
+ const project = scope.project;
3356
+ if (!project || typeof project !== "object")
3357
+ return void 0;
3358
+ const hash = project.hash;
3359
+ return typeof hash === "string" && hash.length > 0 ? hash : void 0;
2402
3360
  }
2403
- /**
2404
- * Retrieve memories from a specific session
2405
- */
2406
3361
  async retrieveFromSession(sessionId) {
2407
3362
  return this.eventStore.getSessionEvents(sessionId);
2408
3363
  }
2409
- /**
2410
- * Get recent memories across all sessions
2411
- */
2412
3364
  async retrieveRecent(limit = 100) {
2413
3365
  return this.eventStore.getRecentEvents(limit);
2414
3366
  }
2415
- /**
2416
- * Enrich search results with full event data
2417
- */
2418
3367
  async enrichResults(results, options) {
2419
3368
  const memories = [];
2420
3369
  for (const result of results) {
@@ -2422,27 +3371,16 @@ var Retriever = class {
2422
3371
  if (!event)
2423
3372
  continue;
2424
3373
  if (this.graduation) {
2425
- this.graduation.recordAccess(
2426
- event.id,
2427
- options.sessionId || "unknown",
2428
- result.score
2429
- );
3374
+ this.graduation.recordAccess(event.id, options.sessionId || "unknown", result.score);
2430
3375
  }
2431
3376
  let sessionContext;
2432
3377
  if (options.includeSessionContext) {
2433
3378
  sessionContext = await this.getSessionContext(event.sessionId, event.id);
2434
3379
  }
2435
- memories.push({
2436
- event,
2437
- score: result.score,
2438
- sessionContext
2439
- });
3380
+ memories.push({ event, score: result.score, sessionContext });
2440
3381
  }
2441
3382
  return memories;
2442
3383
  }
2443
- /**
2444
- * Get surrounding context from the same session
2445
- */
2446
3384
  async getSessionContext(sessionId, eventId) {
2447
3385
  const sessionEvents = await this.eventStore.getSessionEvents(sessionId);
2448
3386
  const eventIndex = sessionEvents.findIndex((e) => e.id === eventId);
@@ -2455,55 +3393,86 @@ var Retriever = class {
2455
3393
  return void 0;
2456
3394
  return contextEvents.filter((e) => e.id !== eventId).map((e) => `[${e.eventType}]: ${e.content.slice(0, 200)}...`).join("\n");
2457
3395
  }
2458
- /**
2459
- * Build context string from memories (respecting token limit)
2460
- */
3396
+ buildUnifiedContext(projectResult, sharedMemories) {
3397
+ let context = projectResult.context;
3398
+ if (sharedMemories.length === 0)
3399
+ return context;
3400
+ context += "\n\n## Cross-Project Knowledge\n\n";
3401
+ for (const memory of sharedMemories.slice(0, 3)) {
3402
+ context += `### ${memory.title}
3403
+ `;
3404
+ if (memory.symptoms.length > 0)
3405
+ context += `**Symptoms:** ${memory.symptoms.join(", ")}
3406
+ `;
3407
+ context += `**Root Cause:** ${memory.rootCause}
3408
+ `;
3409
+ context += `**Solution:** ${memory.solution}
3410
+ `;
3411
+ if (memory.technologies && memory.technologies.length > 0)
3412
+ context += `**Technologies:** ${memory.technologies.join(", ")}
3413
+ `;
3414
+ context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
3415
+
3416
+ `;
3417
+ }
3418
+ return context;
3419
+ }
2461
3420
  buildContext(memories, maxTokens) {
2462
3421
  const parts = [];
2463
3422
  let currentTokens = 0;
2464
3423
  for (const memory of memories) {
2465
3424
  const memoryText = this.formatMemory(memory);
2466
3425
  const memoryTokens = this.estimateTokens(memoryText);
2467
- if (currentTokens + memoryTokens > maxTokens) {
3426
+ if (currentTokens + memoryTokens > maxTokens)
2468
3427
  break;
2469
- }
2470
3428
  parts.push(memoryText);
2471
3429
  currentTokens += memoryTokens;
2472
3430
  }
2473
- if (parts.length === 0) {
3431
+ if (parts.length === 0)
2474
3432
  return "";
2475
- }
2476
3433
  return `## Relevant Memories
2477
3434
 
2478
3435
  ${parts.join("\n\n---\n\n")}`;
2479
3436
  }
2480
- /**
2481
- * Format a single memory for context
2482
- */
2483
3437
  formatMemory(memory) {
2484
3438
  const { event, score, sessionContext } = memory;
2485
3439
  const date = event.timestamp.toISOString().split("T")[0];
2486
3440
  let text = `**${event.eventType}** (${date}, score: ${score.toFixed(2)})
2487
3441
  ${event.content}`;
2488
- if (sessionContext) {
3442
+ if (sessionContext)
2489
3443
  text += `
2490
3444
 
2491
3445
  _Context:_ ${sessionContext}`;
2492
- }
2493
3446
  return text;
2494
3447
  }
2495
- /**
2496
- * Estimate token count (rough approximation)
2497
- */
3448
+ matchesMetadataScope(metadata, expected) {
3449
+ if (!metadata)
3450
+ return false;
3451
+ return Object.entries(expected).every(([path7, value]) => {
3452
+ const actual = path7.split(".").reduce((acc, key) => {
3453
+ if (typeof acc !== "object" || acc === null)
3454
+ return void 0;
3455
+ return acc[key];
3456
+ }, metadata);
3457
+ return actual === value;
3458
+ });
3459
+ }
3460
+ tokenize(text) {
3461
+ return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter((t) => t.length >= 2).slice(0, 64);
3462
+ }
3463
+ keywordOverlap(a, b) {
3464
+ if (a.length === 0 || b.length === 0)
3465
+ return 0;
3466
+ const bs = new Set(b);
3467
+ let hit = 0;
3468
+ for (const t of a)
3469
+ if (bs.has(t))
3470
+ hit += 1;
3471
+ return hit / a.length;
3472
+ }
2498
3473
  estimateTokens(text) {
2499
3474
  return Math.ceil(text.length / 4);
2500
3475
  }
2501
- /**
2502
- * Get event age in days (for recency scoring)
2503
- */
2504
- getEventAgeDays(eventId) {
2505
- return 0;
2506
- }
2507
3476
  };
2508
3477
  function createRetriever(eventStore, vectorStore, embedder, matcher) {
2509
3478
  return new Retriever(eventStore, vectorStore, embedder, matcher);
@@ -3793,6 +4762,59 @@ var ConsolidatedStore = class {
3793
4762
  [memoryId]
3794
4763
  );
3795
4764
  }
4765
+ /**
4766
+ * Create a long-term rule promoted from stable summaries
4767
+ */
4768
+ async createRule(input) {
4769
+ const ruleId = randomUUID6();
4770
+ await dbRun(
4771
+ this.db,
4772
+ `INSERT INTO consolidated_rules
4773
+ (rule_id, rule, topics, source_memory_ids, source_events, confidence, created_at)
4774
+ VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
4775
+ [
4776
+ ruleId,
4777
+ input.rule,
4778
+ JSON.stringify(input.topics),
4779
+ JSON.stringify(input.sourceMemoryIds),
4780
+ JSON.stringify(input.sourceEvents),
4781
+ input.confidence
4782
+ ]
4783
+ );
4784
+ return ruleId;
4785
+ }
4786
+ async getRules(options) {
4787
+ const limit = options?.limit || 100;
4788
+ const rows = await dbAll(
4789
+ this.db,
4790
+ `SELECT * FROM consolidated_rules ORDER BY confidence DESC, created_at DESC LIMIT ?`,
4791
+ [limit]
4792
+ );
4793
+ return rows.map((row) => ({
4794
+ ruleId: row.rule_id,
4795
+ rule: row.rule,
4796
+ topics: JSON.parse(row.topics || "[]"),
4797
+ sourceMemoryIds: JSON.parse(row.source_memory_ids || "[]"),
4798
+ sourceEvents: JSON.parse(row.source_events || "[]"),
4799
+ confidence: Number(row.confidence ?? 0.5),
4800
+ createdAt: toDate(row.created_at) || /* @__PURE__ */ new Date()
4801
+ }));
4802
+ }
4803
+ async countRules() {
4804
+ const result = await dbAll(
4805
+ this.db,
4806
+ `SELECT COUNT(*) as count FROM consolidated_rules`
4807
+ );
4808
+ return result[0]?.count || 0;
4809
+ }
4810
+ async hasRuleForSourceMemory(memoryId) {
4811
+ const rows = await dbAll(
4812
+ this.db,
4813
+ `SELECT COUNT(*) as count FROM consolidated_rules WHERE source_memory_ids LIKE ?`,
4814
+ [`%"${memoryId}"%`]
4815
+ );
4816
+ return (rows[0]?.count || 0) > 0;
4817
+ }
3796
4818
  /**
3797
4819
  * Get count of consolidated memories
3798
4820
  */
@@ -3942,7 +4964,14 @@ var ConsolidationWorker = class {
3942
4964
  * Force a consolidation run (manual trigger)
3943
4965
  */
3944
4966
  async forceRun() {
3945
- return await this.consolidate();
4967
+ const out = await this.consolidateWithReport();
4968
+ return out.consolidatedCount;
4969
+ }
4970
+ /**
4971
+ * Force a consolidation run and return metrics report
4972
+ */
4973
+ async forceRunWithReport() {
4974
+ return this.consolidateWithReport();
3946
4975
  }
3947
4976
  /**
3948
4977
  * Schedule the next consolidation check
@@ -3982,12 +5011,21 @@ var ConsolidationWorker = class {
3982
5011
  * Perform consolidation
3983
5012
  */
3984
5013
  async consolidate() {
5014
+ const out = await this.consolidateWithReport();
5015
+ return out.consolidatedCount;
5016
+ }
5017
+ async consolidateWithReport() {
3985
5018
  const workingSet = await this.workingSetStore.get();
3986
5019
  if (workingSet.recentEvents.length < 3) {
3987
- return 0;
5020
+ return {
5021
+ consolidatedCount: 0,
5022
+ promotedRuleCount: 0,
5023
+ report: this.buildCostQualityReport(workingSet.recentEvents, [], 0)
5024
+ };
3988
5025
  }
3989
5026
  const groups = this.groupByTopic(workingSet.recentEvents);
3990
5027
  let consolidatedCount = 0;
5028
+ const createdMemoryIds = [];
3991
5029
  for (const group of groups) {
3992
5030
  if (group.events.length < 3)
3993
5031
  continue;
@@ -3996,14 +5034,16 @@ var ConsolidationWorker = class {
3996
5034
  if (alreadyConsolidated)
3997
5035
  continue;
3998
5036
  const summary = await this.summarize(group);
3999
- await this.consolidatedStore.create({
5037
+ const memoryId = await this.consolidatedStore.create({
4000
5038
  summary,
4001
5039
  topics: group.topics,
4002
5040
  sourceEvents: eventIds,
4003
5041
  confidence: this.calculateConfidence(group)
4004
5042
  });
5043
+ createdMemoryIds.push(memoryId);
4005
5044
  consolidatedCount++;
4006
5045
  }
5046
+ const promotedRuleCount = await this.promoteStableSummariesToRules(createdMemoryIds);
4007
5047
  if (consolidatedCount > 0) {
4008
5048
  const consolidatedEventIds = groups.filter((g) => g.events.length >= 3).flatMap((g) => g.events.map((e) => e.id));
4009
5049
  const oldEventIds = consolidatedEventIds.filter((id) => {
@@ -4017,7 +5057,61 @@ var ConsolidationWorker = class {
4017
5057
  await this.workingSetStore.prune(oldEventIds);
4018
5058
  }
4019
5059
  }
4020
- return consolidatedCount;
5060
+ const report = this.buildCostQualityReport(workingSet.recentEvents, groups, consolidatedCount);
5061
+ return { consolidatedCount, promotedRuleCount, report };
5062
+ }
5063
+ async promoteStableSummariesToRules(memoryIds) {
5064
+ let promoted = 0;
5065
+ for (const memoryId of memoryIds) {
5066
+ const memory = await this.consolidatedStore.get(memoryId);
5067
+ if (!memory)
5068
+ continue;
5069
+ if (memory.confidence < 0.55)
5070
+ continue;
5071
+ if (memory.sourceEvents.length < 4)
5072
+ continue;
5073
+ const exists = await this.consolidatedStore.hasRuleForSourceMemory(memoryId);
5074
+ if (exists)
5075
+ continue;
5076
+ const rule = this.buildRuleFromSummary(memory.summary, memory.topics);
5077
+ if (!rule)
5078
+ continue;
5079
+ await this.consolidatedStore.createRule({
5080
+ rule,
5081
+ topics: memory.topics,
5082
+ sourceMemoryIds: [memory.memoryId],
5083
+ sourceEvents: memory.sourceEvents,
5084
+ confidence: Math.min(1, memory.confidence + 0.08)
5085
+ });
5086
+ promoted++;
5087
+ }
5088
+ return promoted;
5089
+ }
5090
+ buildRuleFromSummary(summary, topics) {
5091
+ const lines = summary.split(/\r?\n/).map((l) => l.trim()).filter(Boolean).filter((l) => !l.toLowerCase().startsWith("topics:"));
5092
+ const bullet = lines.find((l) => l.startsWith("- "))?.replace(/^-\s*/, "");
5093
+ const seed = bullet || lines[0];
5094
+ if (!seed || seed.length < 8)
5095
+ return null;
5096
+ const topicPrefix = topics.length > 0 ? `[${topics.slice(0, 2).join(", ")}] ` : "";
5097
+ return `${topicPrefix}${seed}`;
5098
+ }
5099
+ buildCostQualityReport(events, groups, consolidatedCount) {
5100
+ const beforeTokenEstimate = events.reduce((acc, e) => acc + this.estimateTokens(e.content), 0);
5101
+ const afterSummaries = groups.filter((g) => g.events.length >= 3).slice(0, Math.max(consolidatedCount, 1));
5102
+ const afterTokenEstimate = afterSummaries.length > 0 ? afterSummaries.reduce((acc, g) => acc + this.estimateTokens(this.ruleBasedSummary(g)), 0) : beforeTokenEstimate;
5103
+ const reductionRatio = beforeTokenEstimate > 0 ? Math.max(0, (beforeTokenEstimate - afterTokenEstimate) / beforeTokenEstimate) : 0;
5104
+ const qualityGuardPassed = consolidatedCount === 0 ? true : groups.filter((g) => g.events.length >= 3).every((g) => this.calculateConfidence(g) >= 0.55);
5105
+ return {
5106
+ beforeTokenEstimate,
5107
+ afterTokenEstimate,
5108
+ reductionRatio,
5109
+ qualityGuardPassed,
5110
+ details: `groups=${groups.length}, consolidated=${consolidatedCount}`
5111
+ };
5112
+ }
5113
+ estimateTokens(text) {
5114
+ return Math.ceil((text || "").length / 4);
4021
5115
  }
4022
5116
  /**
4023
5117
  * Check if consolidation should run
@@ -4577,13 +5671,185 @@ function createGraduationWorker(eventStore, graduation, config) {
4577
5671
  );
4578
5672
  }
4579
5673
 
5674
+ // src/core/md-mirror.ts
5675
+ import * as fs3 from "node:fs";
5676
+ import * as path2 from "node:path";
5677
+ function sanitizeSegment2(input, fallback) {
5678
+ const v = (input || "").trim().toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
5679
+ return v || fallback;
5680
+ }
5681
+ function getAtPath(obj, dotted) {
5682
+ if (!obj)
5683
+ return void 0;
5684
+ return dotted.split(".").reduce((acc, key) => {
5685
+ if (!acc || typeof acc !== "object")
5686
+ return void 0;
5687
+ return acc[key];
5688
+ }, obj);
5689
+ }
5690
+ function buildMirrorPath2(rootDir, event) {
5691
+ const meta = event.metadata;
5692
+ const namespaceRaw = getAtPath(meta, "namespace") ?? getAtPath(meta, "scope.namespace") ?? event.eventType;
5693
+ const namespace = sanitizeSegment2(typeof namespaceRaw === "string" ? namespaceRaw : void 0, "general");
5694
+ const categoryRaw = getAtPath(meta, "categoryPath") ?? getAtPath(meta, "scope.categoryPath");
5695
+ const categoryPath = Array.isArray(categoryRaw) && categoryRaw.length > 0 ? categoryRaw.map((x) => sanitizeSegment2(typeof x === "string" ? x : void 0, "uncategorized")) : ["uncategorized"];
5696
+ const d = event.timestamp;
5697
+ const yyyy = d.getFullYear();
5698
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
5699
+ const dd = String(d.getDate()).padStart(2, "0");
5700
+ return path2.join(rootDir, "memory", namespace, ...categoryPath, `${yyyy}-${mm}-${dd}.md`);
5701
+ }
5702
+ var MarkdownMirror2 = class {
5703
+ constructor(rootDir) {
5704
+ this.rootDir = rootDir;
5705
+ }
5706
+ async append(event, eventId) {
5707
+ const out = buildMirrorPath2(this.rootDir, event);
5708
+ fs3.mkdirSync(path2.dirname(out), { recursive: true });
5709
+ const lines = [
5710
+ "",
5711
+ `## ${event.timestamp.toISOString()} | ${eventId ?? "pending-id"}`,
5712
+ `- type: ${event.eventType}`,
5713
+ `- session: ${event.sessionId}`,
5714
+ event.content
5715
+ ];
5716
+ await fs3.promises.appendFile(out, lines.join("\n"), "utf8");
5717
+ await this.refreshIndex();
5718
+ }
5719
+ async refreshIndex() {
5720
+ const memoryRoot = path2.join(this.rootDir, "memory");
5721
+ await fs3.promises.mkdir(memoryRoot, { recursive: true });
5722
+ const files = [];
5723
+ await this.walk(memoryRoot, files);
5724
+ const mdFiles = files.filter((f) => f.endsWith(".md")).map((f) => path2.relative(this.rootDir, f)).filter((rel) => rel !== path2.join("memory", "_index.md")).sort();
5725
+ const index = [
5726
+ "# Memory Index",
5727
+ "",
5728
+ "Generated automatically by MarkdownMirror.",
5729
+ "",
5730
+ ...mdFiles.map((rel) => `- ${rel}`),
5731
+ ""
5732
+ ].join("\n");
5733
+ await fs3.promises.writeFile(path2.join(memoryRoot, "_index.md"), index, "utf8");
5734
+ }
5735
+ async walk(dir, out) {
5736
+ const entries = await fs3.promises.readdir(dir, { withFileTypes: true });
5737
+ for (const e of entries) {
5738
+ const full = path2.join(dir, e.name);
5739
+ if (e.isDirectory()) {
5740
+ await this.walk(full, out);
5741
+ } else {
5742
+ out.push(full);
5743
+ }
5744
+ }
5745
+ }
5746
+ };
5747
+
5748
+ // src/core/ingest-interceptor.ts
5749
+ var IngestInterceptorRegistry = class {
5750
+ before = [];
5751
+ after = [];
5752
+ onError = [];
5753
+ registerBefore(interceptor) {
5754
+ this.before.push(interceptor);
5755
+ return () => {
5756
+ this.before = this.before.filter((i) => i !== interceptor);
5757
+ };
5758
+ }
5759
+ registerAfter(interceptor) {
5760
+ this.after.push(interceptor);
5761
+ return () => {
5762
+ this.after = this.after.filter((i) => i !== interceptor);
5763
+ };
5764
+ }
5765
+ registerOnError(interceptor) {
5766
+ this.onError.push(interceptor);
5767
+ return () => {
5768
+ this.onError = this.onError.filter((i) => i !== interceptor);
5769
+ };
5770
+ }
5771
+ async run(stage, context) {
5772
+ const interceptors = stage === "before" ? this.before : stage === "after" ? this.after : this.onError;
5773
+ for (const interceptor of interceptors) {
5774
+ await interceptor({ ...context, stage });
5775
+ }
5776
+ }
5777
+ };
5778
+ function mergeHierarchicalMetadata(base, patch) {
5779
+ if (!base && !patch)
5780
+ return void 0;
5781
+ if (!base)
5782
+ return patch;
5783
+ if (!patch)
5784
+ return base;
5785
+ const result = { ...base };
5786
+ for (const [key, value] of Object.entries(patch)) {
5787
+ const current = result[key];
5788
+ if (typeof current === "object" && current !== null && !Array.isArray(current) && typeof value === "object" && value !== null && !Array.isArray(value)) {
5789
+ result[key] = mergeHierarchicalMetadata(
5790
+ current,
5791
+ value
5792
+ );
5793
+ } else {
5794
+ result[key] = value;
5795
+ }
5796
+ }
5797
+ return result;
5798
+ }
5799
+
5800
+ // src/core/tag-taxonomy.ts
5801
+ var TAG_NAMESPACES = {
5802
+ SYSTEM: "sys:",
5803
+ QUALITY: "q:",
5804
+ PROJECT: "proj:",
5805
+ TOPIC: "topic:",
5806
+ TEMPORAL: "t:",
5807
+ USER: "user:",
5808
+ AGENT: "agent:"
5809
+ };
5810
+ var VALID_TAG_NAMESPACES = new Set(Object.values(TAG_NAMESPACES));
5811
+ function parseTag(tag) {
5812
+ const value = (tag || "").trim();
5813
+ const idx = value.indexOf(":");
5814
+ if (idx <= 0)
5815
+ return { value };
5816
+ const namespace = `${value.slice(0, idx)}:`;
5817
+ const tagValue = value.slice(idx + 1);
5818
+ if (!tagValue)
5819
+ return { value };
5820
+ return { namespace, value: tagValue };
5821
+ }
5822
+ function validateTag(tag) {
5823
+ const normalized = (tag || "").trim();
5824
+ if (!normalized)
5825
+ return false;
5826
+ const { namespace } = parseTag(normalized);
5827
+ if (!namespace)
5828
+ return true;
5829
+ return VALID_TAG_NAMESPACES.has(namespace);
5830
+ }
5831
+ function normalizeTags(tags) {
5832
+ if (!Array.isArray(tags))
5833
+ return [];
5834
+ const dedup = /* @__PURE__ */ new Set();
5835
+ for (const item of tags) {
5836
+ if (typeof item !== "string")
5837
+ continue;
5838
+ const normalized = item.trim();
5839
+ if (!validateTag(normalized))
5840
+ continue;
5841
+ dedup.add(normalized);
5842
+ }
5843
+ return [...dedup];
5844
+ }
5845
+
4580
5846
  // src/services/memory-service.ts
4581
5847
  function normalizePath(projectPath) {
4582
- const expanded = projectPath.startsWith("~") ? path.join(os.homedir(), projectPath.slice(1)) : projectPath;
5848
+ const expanded = projectPath.startsWith("~") ? path3.join(os.homedir(), projectPath.slice(1)) : projectPath;
4583
5849
  try {
4584
- return fs.realpathSync(expanded);
5850
+ return fs4.realpathSync(expanded);
4585
5851
  } catch {
4586
- return path.resolve(expanded);
5852
+ return path3.resolve(expanded);
4587
5853
  }
4588
5854
  }
4589
5855
  function hashProjectPath(projectPath) {
@@ -4592,10 +5858,21 @@ function hashProjectPath(projectPath) {
4592
5858
  }
4593
5859
  function getProjectStoragePath(projectPath) {
4594
5860
  const hash = hashProjectPath(projectPath);
4595
- return path.join(os.homedir(), ".claude-code", "memory", "projects", hash);
5861
+ return path3.join(os.homedir(), ".claude-code", "memory", "projects", hash);
5862
+ }
5863
+ var REGISTRY_PATH = path3.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
5864
+ var SHARED_STORAGE_PATH = path3.join(os.homedir(), ".claude-code", "memory", "shared");
5865
+ function loadSessionRegistry() {
5866
+ try {
5867
+ if (fs4.existsSync(REGISTRY_PATH)) {
5868
+ const data = fs4.readFileSync(REGISTRY_PATH, "utf-8");
5869
+ return JSON.parse(data);
5870
+ }
5871
+ } catch (error) {
5872
+ console.error("Failed to load session registry:", error);
5873
+ }
5874
+ return { version: 1, sessions: {} };
4596
5875
  }
4597
- var REGISTRY_PATH = path.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
4598
- var SHARED_STORAGE_PATH = path.join(os.homedir(), ".claude-code", "memory", "shared");
4599
5876
  var MemoryService = class {
4600
5877
  // Primary store: SQLite (WAL mode) - for hooks, always available
4601
5878
  sqliteStore;
@@ -4610,6 +5887,7 @@ var MemoryService = class {
4610
5887
  vectorWorker = null;
4611
5888
  graduationWorker = null;
4612
5889
  initialized = false;
5890
+ ingestInterceptors = new IngestInterceptorRegistry();
4613
5891
  // Endless Mode components
4614
5892
  workingSetStore = null;
4615
5893
  consolidatedStore = null;
@@ -4623,20 +5901,27 @@ var MemoryService = class {
4623
5901
  sharedPromoter = null;
4624
5902
  sharedStoreConfig = null;
4625
5903
  projectHash = null;
5904
+ projectPath = null;
4626
5905
  readOnly;
4627
5906
  lightweightMode;
5907
+ mdMirror;
4628
5908
  constructor(config) {
4629
5909
  const storagePath = this.expandPath(config.storagePath);
4630
5910
  this.readOnly = config.readOnly ?? false;
4631
5911
  this.lightweightMode = config.lightweightMode ?? false;
4632
- if (!this.readOnly && !fs.existsSync(storagePath)) {
4633
- fs.mkdirSync(storagePath, { recursive: true });
5912
+ this.mdMirror = new MarkdownMirror2(process.cwd());
5913
+ if (!this.readOnly && !fs4.existsSync(storagePath)) {
5914
+ fs4.mkdirSync(storagePath, { recursive: true });
4634
5915
  }
4635
5916
  this.projectHash = config.projectHash || null;
5917
+ this.projectPath = config.projectPath || null;
4636
5918
  this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
4637
5919
  this.sqliteStore = new SQLiteEventStore(
4638
- path.join(storagePath, "events.sqlite"),
4639
- { readonly: this.readOnly }
5920
+ path3.join(storagePath, "events.sqlite"),
5921
+ {
5922
+ readonly: this.readOnly,
5923
+ markdownMirrorRoot: storagePath
5924
+ }
4640
5925
  );
4641
5926
  const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
4642
5927
  if (!analyticsEnabled) {
@@ -4644,7 +5929,7 @@ var MemoryService = class {
4644
5929
  } else if (this.readOnly) {
4645
5930
  try {
4646
5931
  this.analyticsStore = new EventStore(
4647
- path.join(storagePath, "analytics.duckdb"),
5932
+ path3.join(storagePath, "analytics.duckdb"),
4648
5933
  { readOnly: true }
4649
5934
  );
4650
5935
  } catch {
@@ -4652,11 +5937,11 @@ var MemoryService = class {
4652
5937
  }
4653
5938
  } else {
4654
5939
  this.analyticsStore = new EventStore(
4655
- path.join(storagePath, "analytics.duckdb"),
5940
+ path3.join(storagePath, "analytics.duckdb"),
4656
5941
  { readOnly: false }
4657
5942
  );
4658
5943
  }
4659
- this.vectorStore = new VectorStore(path.join(storagePath, "vectors"));
5944
+ this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
4660
5945
  this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
4661
5946
  this.matcher = getDefaultMatcher();
4662
5947
  this.retriever = createRetriever(
@@ -4666,6 +5951,7 @@ var MemoryService = class {
4666
5951
  this.embedder,
4667
5952
  this.matcher
4668
5953
  );
5954
+ this.retriever.setQueryRewriter((q) => this.rewriteQueryIntent(q));
4669
5955
  this.graduation = createGraduationPipeline(this.sqliteStore);
4670
5956
  }
4671
5957
  /**
@@ -4725,16 +6011,16 @@ var MemoryService = class {
4725
6011
  */
4726
6012
  async initializeSharedStore() {
4727
6013
  const sharedPath = this.sharedStoreConfig?.sharedStoragePath ? this.expandPath(this.sharedStoreConfig.sharedStoragePath) : SHARED_STORAGE_PATH;
4728
- if (!fs.existsSync(sharedPath)) {
4729
- fs.mkdirSync(sharedPath, { recursive: true });
6014
+ if (!fs4.existsSync(sharedPath)) {
6015
+ fs4.mkdirSync(sharedPath, { recursive: true });
4730
6016
  }
4731
6017
  this.sharedEventStore = createSharedEventStore(
4732
- path.join(sharedPath, "shared.duckdb")
6018
+ path3.join(sharedPath, "shared.duckdb")
4733
6019
  );
4734
6020
  await this.sharedEventStore.initialize();
4735
6021
  this.sharedStore = createSharedStore(this.sharedEventStore);
4736
6022
  this.sharedVectorStore = createSharedVectorStore(
4737
- path.join(sharedPath, "vectors")
6023
+ path3.join(sharedPath, "vectors")
4738
6024
  );
4739
6025
  await this.sharedVectorStore.initialize();
4740
6026
  this.sharedPromoter = createSharedPromoter(
@@ -4745,6 +6031,86 @@ var MemoryService = class {
4745
6031
  );
4746
6032
  this.retriever.setSharedStores(this.sharedStore, this.sharedVectorStore);
4747
6033
  }
6034
+ registerIngestBefore(interceptor) {
6035
+ return this.ingestInterceptors.registerBefore(interceptor);
6036
+ }
6037
+ registerIngestAfter(interceptor) {
6038
+ return this.ingestInterceptors.registerAfter(interceptor);
6039
+ }
6040
+ registerIngestOnError(interceptor) {
6041
+ return this.ingestInterceptors.registerOnError(interceptor);
6042
+ }
6043
+ async ingestWithInterceptors(operation, input, onSuccess) {
6044
+ const normalizedInput = {
6045
+ ...input,
6046
+ metadata: mergeHierarchicalMetadata(
6047
+ {
6048
+ ingest: {
6049
+ operation,
6050
+ pipeline: "default",
6051
+ ts: (/* @__PURE__ */ new Date()).toISOString()
6052
+ },
6053
+ ...this.projectHash ? {
6054
+ scope: {
6055
+ project: {
6056
+ hash: this.projectHash,
6057
+ ...this.projectPath ? { path: this.projectPath } : {}
6058
+ }
6059
+ },
6060
+ tags: [`proj:${this.projectHash}`]
6061
+ } : {}
6062
+ },
6063
+ input.metadata
6064
+ )
6065
+ };
6066
+ if (this.projectHash && normalizedInput.metadata) {
6067
+ const meta = normalizedInput.metadata;
6068
+ const currentTags = Array.isArray(meta.tags) ? meta.tags.filter((x) => typeof x === "string") : [];
6069
+ const projectTag = `proj:${this.projectHash}`;
6070
+ if (!currentTags.includes(projectTag)) {
6071
+ meta.tags = [...currentTags, projectTag];
6072
+ }
6073
+ }
6074
+ if (normalizedInput.metadata) {
6075
+ const meta = normalizedInput.metadata;
6076
+ const normalizedTags = normalizeTags(meta.tags);
6077
+ if (normalizedTags.length > 0) {
6078
+ meta.tags = normalizedTags;
6079
+ }
6080
+ }
6081
+ await this.ingestInterceptors.run("before", {
6082
+ operation,
6083
+ sessionId: normalizedInput.sessionId,
6084
+ event: normalizedInput
6085
+ });
6086
+ try {
6087
+ const result = await this.sqliteStore.append(normalizedInput);
6088
+ if (result.success && !result.isDuplicate) {
6089
+ if (onSuccess) {
6090
+ await onSuccess(result.eventId);
6091
+ }
6092
+ try {
6093
+ await this.mdMirror.append(normalizedInput, result.eventId);
6094
+ } catch {
6095
+ }
6096
+ }
6097
+ await this.ingestInterceptors.run("after", {
6098
+ operation,
6099
+ sessionId: normalizedInput.sessionId,
6100
+ event: normalizedInput
6101
+ });
6102
+ return result;
6103
+ } catch (error) {
6104
+ const normalizedError = error instanceof Error ? error : new Error(String(error));
6105
+ await this.ingestInterceptors.run("error", {
6106
+ operation,
6107
+ sessionId: normalizedInput.sessionId,
6108
+ event: normalizedInput,
6109
+ error: normalizedError
6110
+ });
6111
+ throw error;
6112
+ }
6113
+ }
4748
6114
  /**
4749
6115
  * Start a new session
4750
6116
  */
@@ -4772,50 +6138,57 @@ var MemoryService = class {
4772
6138
  */
4773
6139
  async storeUserPrompt(sessionId, content, metadata) {
4774
6140
  await this.initialize();
4775
- const result = await this.sqliteStore.append({
4776
- eventType: "user_prompt",
4777
- sessionId,
4778
- timestamp: /* @__PURE__ */ new Date(),
4779
- content,
4780
- metadata
4781
- });
4782
- if (result.success && !result.isDuplicate) {
4783
- await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
4784
- }
4785
- return result;
6141
+ return this.ingestWithInterceptors(
6142
+ "user_prompt",
6143
+ {
6144
+ eventType: "user_prompt",
6145
+ sessionId,
6146
+ timestamp: /* @__PURE__ */ new Date(),
6147
+ content,
6148
+ metadata
6149
+ },
6150
+ async (eventId) => {
6151
+ await this.sqliteStore.enqueueForEmbedding(eventId, content);
6152
+ }
6153
+ );
4786
6154
  }
4787
6155
  /**
4788
6156
  * Store an agent response
4789
6157
  */
4790
6158
  async storeAgentResponse(sessionId, content, metadata) {
4791
6159
  await this.initialize();
4792
- const result = await this.sqliteStore.append({
4793
- eventType: "agent_response",
4794
- sessionId,
4795
- timestamp: /* @__PURE__ */ new Date(),
4796
- content,
4797
- metadata
4798
- });
4799
- if (result.success && !result.isDuplicate) {
4800
- await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
4801
- }
4802
- return result;
6160
+ return this.ingestWithInterceptors(
6161
+ "agent_response",
6162
+ {
6163
+ eventType: "agent_response",
6164
+ sessionId,
6165
+ timestamp: /* @__PURE__ */ new Date(),
6166
+ content,
6167
+ metadata
6168
+ },
6169
+ async (eventId) => {
6170
+ await this.sqliteStore.enqueueForEmbedding(eventId, content);
6171
+ }
6172
+ );
4803
6173
  }
4804
6174
  /**
4805
6175
  * Store a session summary
4806
6176
  */
4807
- async storeSessionSummary(sessionId, summary) {
6177
+ async storeSessionSummary(sessionId, summary, metadata) {
4808
6178
  await this.initialize();
4809
- const result = await this.sqliteStore.append({
4810
- eventType: "session_summary",
4811
- sessionId,
4812
- timestamp: /* @__PURE__ */ new Date(),
4813
- content: summary
4814
- });
4815
- if (result.success && !result.isDuplicate) {
4816
- await this.sqliteStore.enqueueForEmbedding(result.eventId, summary);
4817
- }
4818
- return result;
6179
+ return this.ingestWithInterceptors(
6180
+ "session_summary",
6181
+ {
6182
+ eventType: "session_summary",
6183
+ sessionId,
6184
+ timestamp: /* @__PURE__ */ new Date(),
6185
+ content: summary,
6186
+ metadata
6187
+ },
6188
+ async (eventId) => {
6189
+ await this.sqliteStore.enqueueForEmbedding(eventId, summary);
6190
+ }
6191
+ );
4819
6192
  }
4820
6193
  /**
4821
6194
  * Store a tool observation
@@ -4823,39 +6196,182 @@ var MemoryService = class {
4823
6196
  async storeToolObservation(sessionId, payload) {
4824
6197
  await this.initialize();
4825
6198
  const content = JSON.stringify(payload);
4826
- const result = await this.sqliteStore.append({
4827
- eventType: "tool_observation",
4828
- sessionId,
4829
- timestamp: /* @__PURE__ */ new Date(),
4830
- content,
4831
- metadata: {
4832
- toolName: payload.toolName,
4833
- success: payload.success
6199
+ const turnId = payload.metadata?.turnId;
6200
+ return this.ingestWithInterceptors(
6201
+ "tool_observation",
6202
+ {
6203
+ eventType: "tool_observation",
6204
+ sessionId,
6205
+ timestamp: /* @__PURE__ */ new Date(),
6206
+ content,
6207
+ metadata: {
6208
+ toolName: payload.toolName,
6209
+ success: payload.success,
6210
+ ...turnId ? { turnId } : {}
6211
+ }
6212
+ },
6213
+ async (eventId) => {
6214
+ const embeddingContent = createToolObservationEmbedding(
6215
+ payload.toolName,
6216
+ payload.metadata || {},
6217
+ payload.success
6218
+ );
6219
+ await this.sqliteStore.enqueueForEmbedding(eventId, embeddingContent);
4834
6220
  }
4835
- });
4836
- if (result.success && !result.isDuplicate) {
4837
- const embeddingContent = createToolObservationEmbedding(
4838
- payload.toolName,
4839
- payload.metadata || {},
4840
- payload.success
4841
- );
4842
- await this.sqliteStore.enqueueForEmbedding(result.eventId, embeddingContent);
4843
- }
4844
- return result;
6221
+ );
4845
6222
  }
4846
6223
  /**
4847
6224
  * Retrieve relevant memories for a query
4848
6225
  */
4849
6226
  async retrieveMemories(query, options) {
4850
6227
  await this.initialize();
6228
+ const rerankWeights = await this.getRerankWeights(options?.adaptiveRerank === true);
6229
+ let result;
4851
6230
  if (options?.includeShared && this.sharedStore) {
4852
- return this.retriever.retrieveUnified(query, {
6231
+ result = await this.retriever.retrieveUnified(query, {
4853
6232
  ...options,
6233
+ intentRewrite: options?.intentRewrite === true,
6234
+ rerankWeights,
4854
6235
  includeShared: true,
4855
- projectHash: this.projectHash || void 0
6236
+ projectHash: this.projectHash || void 0,
6237
+ projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
6238
+ allowedProjectHashes: options?.allowedProjectHashes
6239
+ });
6240
+ } else {
6241
+ result = await this.retriever.retrieve(query, {
6242
+ ...options,
6243
+ intentRewrite: options?.intentRewrite === true,
6244
+ rerankWeights,
6245
+ projectHash: this.projectHash || void 0,
6246
+ projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
6247
+ allowedProjectHashes: options?.allowedProjectHashes
6248
+ });
6249
+ }
6250
+ try {
6251
+ const selectedEventIds = result.memories.map((m) => m.event.id);
6252
+ const selectedDetails = (result.selectedDebug || []).map((d) => ({
6253
+ eventId: d.eventId,
6254
+ score: d.score,
6255
+ semanticScore: d.semanticScore,
6256
+ lexicalScore: d.lexicalScore,
6257
+ recencyScore: d.recencyScore
6258
+ }));
6259
+ const candidateDetails = (result.candidateDebug || []).map((d) => ({
6260
+ eventId: d.eventId,
6261
+ score: d.score,
6262
+ semanticScore: d.semanticScore,
6263
+ lexicalScore: d.lexicalScore,
6264
+ recencyScore: d.recencyScore
6265
+ }));
6266
+ const candidateEventIds = candidateDetails.length > 0 ? candidateDetails.map((d) => d.eventId) : selectedEventIds;
6267
+ await this.sqliteStore.recordRetrievalTrace({
6268
+ sessionId: options?.sessionId,
6269
+ projectHash: this.projectHash || void 0,
6270
+ queryText: query,
6271
+ strategy: options?.strategy || "auto",
6272
+ candidateEventIds,
6273
+ selectedEventIds,
6274
+ candidateDetails,
6275
+ selectedDetails,
6276
+ confidence: result.matchResult.confidence,
6277
+ fallbackTrace: result.fallbackTrace || []
4856
6278
  });
6279
+ } catch {
6280
+ }
6281
+ return result;
6282
+ }
6283
+ getConfiguredRerankWeights() {
6284
+ const semantic = Number(process.env.MEMORY_RERANK_WEIGHT_SEMANTIC ?? "");
6285
+ const lexical = Number(process.env.MEMORY_RERANK_WEIGHT_LEXICAL ?? "");
6286
+ const recency = Number(process.env.MEMORY_RERANK_WEIGHT_RECENCY ?? "");
6287
+ const allFinite = [semantic, lexical, recency].every((v) => Number.isFinite(v));
6288
+ if (!allFinite)
6289
+ return void 0;
6290
+ const nonNegative = [semantic, lexical, recency].every((v) => v >= 0);
6291
+ const total = semantic + lexical + recency;
6292
+ if (!nonNegative || total <= 0)
6293
+ return void 0;
6294
+ return {
6295
+ semantic: semantic / total,
6296
+ lexical: lexical / total,
6297
+ recency: recency / total
6298
+ };
6299
+ }
6300
+ async getRerankWeights(adaptive) {
6301
+ const configured = this.getConfiguredRerankWeights();
6302
+ if (configured)
6303
+ return configured;
6304
+ if (adaptive)
6305
+ return this.getAdaptiveRerankWeights();
6306
+ return void 0;
6307
+ }
6308
+ async rewriteQueryIntent(query) {
6309
+ if (process.env.MEMORY_INTENT_REWRITE_ENABLED !== "1")
6310
+ return null;
6311
+ const apiUrl = process.env.COMPANY_STOCK_API_URL || process.env.COMPANY_INT_API_URL;
6312
+ if (!apiUrl)
6313
+ return null;
6314
+ const controller = new AbortController();
6315
+ const timeoutMs = Number(process.env.MEMORY_INTENT_REWRITE_TIMEOUT_MS || 5e3);
6316
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
6317
+ try {
6318
+ const prompt = [
6319
+ "Rewrite user query for memory retrieval intent expansion.",
6320
+ "Return plain text only, one line, no markdown.",
6321
+ `Query: ${query}`
6322
+ ].join("\n");
6323
+ const res = await fetch(apiUrl, {
6324
+ method: "POST",
6325
+ headers: {
6326
+ "Content-Type": "application/json",
6327
+ Accept: "*/*",
6328
+ Origin: process.env.COMPANY_INT_ORIGIN || "http://company-int.aplusai.ai",
6329
+ Referer: process.env.COMPANY_INT_REFERER || "http://company-int.aplusai.ai/"
6330
+ },
6331
+ body: JSON.stringify({
6332
+ question: prompt,
6333
+ company_name: null,
6334
+ conversation_id: null
6335
+ }),
6336
+ signal: controller.signal
6337
+ });
6338
+ const text = (await res.text()).trim();
6339
+ if (!text)
6340
+ return null;
6341
+ const oneLine = text.replace(/^data:\s*/gm, "").split(/\r?\n/).map((x) => x.trim()).filter(Boolean).join(" ").slice(0, 240);
6342
+ if (!oneLine || oneLine.toLowerCase() === query.toLowerCase())
6343
+ return null;
6344
+ return oneLine;
6345
+ } catch {
6346
+ return null;
6347
+ } finally {
6348
+ clearTimeout(timeout);
6349
+ }
6350
+ }
6351
+ async getAdaptiveRerankWeights() {
6352
+ try {
6353
+ const s = await this.sqliteStore.getHelpfulnessStats();
6354
+ if (s.totalEvaluated < 20)
6355
+ return void 0;
6356
+ let semantic = 0.7;
6357
+ let lexical = 0.2;
6358
+ let recency = 0.1;
6359
+ if (s.avgScore < 0.45) {
6360
+ semantic -= 0.1;
6361
+ lexical += 0.1;
6362
+ } else if (s.avgScore > 0.75) {
6363
+ semantic += 0.05;
6364
+ lexical -= 0.05;
6365
+ }
6366
+ if (s.unhelpful > s.helpful) {
6367
+ recency += 0.05;
6368
+ semantic -= 0.03;
6369
+ lexical -= 0.02;
6370
+ }
6371
+ return { semantic, lexical, recency };
6372
+ } catch {
6373
+ return void 0;
4857
6374
  }
4858
- return this.retriever.retrieve(query, options);
4859
6375
  }
4860
6376
  /**
4861
6377
  * Fast keyword search using SQLite FTS5
@@ -4897,6 +6413,18 @@ var MemoryService = class {
4897
6413
  /**
4898
6414
  * Get memory statistics
4899
6415
  */
6416
+ async getOutboxStats() {
6417
+ await this.initialize();
6418
+ return this.sqliteStore.getOutboxStats();
6419
+ }
6420
+ async getRetrievalTraceStats() {
6421
+ await this.initialize();
6422
+ return this.sqliteStore.getRetrievalTraceStats();
6423
+ }
6424
+ async getRecentRetrievalTraces(limit = 50) {
6425
+ await this.initialize();
6426
+ return this.sqliteStore.getRecentRetrievalTraces(limit);
6427
+ }
4900
6428
  async getStats() {
4901
6429
  await this.initialize();
4902
6430
  const recentEvents = await this.sqliteStore.getRecentEvents(1e4);
@@ -5113,6 +6641,31 @@ var MemoryService = class {
5113
6641
  return [];
5114
6642
  return this.consolidatedStore.getAll({ limit });
5115
6643
  }
6644
+ /**
6645
+ * Extract topic keywords from event content (markdown headings and key terms)
6646
+ */
6647
+ extractTopicsFromContent(content) {
6648
+ const topics = /* @__PURE__ */ new Set();
6649
+ const headings = content.match(/^#{1,3}\s+(.+)$/gm);
6650
+ if (headings) {
6651
+ for (const h of headings.slice(0, 5)) {
6652
+ const text = h.replace(/^#+\s+/, "").replace(/[*_`#]/g, "").trim();
6653
+ if (text.length > 2 && text.length < 50) {
6654
+ topics.add(text);
6655
+ }
6656
+ }
6657
+ }
6658
+ const boldTerms = content.match(/\*\*([^*]+)\*\*/g);
6659
+ if (boldTerms) {
6660
+ for (const b of boldTerms.slice(0, 5)) {
6661
+ const text = b.replace(/\*\*/g, "").trim();
6662
+ if (text.length > 2 && text.length < 30) {
6663
+ topics.add(text);
6664
+ }
6665
+ }
6666
+ }
6667
+ return Array.from(topics).slice(0, 5);
6668
+ }
5116
6669
  /**
5117
6670
  * Increment access count for memories that were used in prompts
5118
6671
  */
@@ -5136,8 +6689,7 @@ var MemoryService = class {
5136
6689
  return events.map((event) => ({
5137
6690
  memoryId: event.id,
5138
6691
  summary: event.content.substring(0, 200) + (event.content.length > 200 ? "..." : ""),
5139
- topics: [],
5140
- // Could extract topics from content if needed
6692
+ topics: this.extractTopicsFromContent(event.content),
5141
6693
  accessCount: event.access_count || 0,
5142
6694
  lastAccessed: event.last_accessed_at || null,
5143
6695
  confidence: 1,
@@ -5158,6 +6710,34 @@ var MemoryService = class {
5158
6710
  }
5159
6711
  return [];
5160
6712
  }
6713
+ /**
6714
+ * Record a memory retrieval for helpfulness tracking
6715
+ */
6716
+ async recordRetrieval(eventId, sessionId, score, query) {
6717
+ await this.initialize();
6718
+ await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
6719
+ }
6720
+ /**
6721
+ * Evaluate helpfulness of retrievals in a session (called at session end)
6722
+ */
6723
+ async evaluateSessionHelpfulness(sessionId) {
6724
+ await this.initialize();
6725
+ await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
6726
+ }
6727
+ /**
6728
+ * Get most helpful memories ranked by helpfulness score
6729
+ */
6730
+ async getHelpfulMemories(limit = 10) {
6731
+ await this.initialize();
6732
+ return this.sqliteStore.getHelpfulMemories(limit);
6733
+ }
6734
+ /**
6735
+ * Get helpfulness statistics for dashboard
6736
+ */
6737
+ async getHelpfulnessStats() {
6738
+ await this.initialize();
6739
+ return this.sqliteStore.getHelpfulnessStats();
6740
+ }
5161
6741
  /**
5162
6742
  * Mark a consolidated memory as accessed
5163
6743
  */
@@ -5221,6 +6801,44 @@ var MemoryService = class {
5221
6801
  lastConsolidation
5222
6802
  };
5223
6803
  }
6804
+ // ============================================================
6805
+ // Turn Grouping Methods
6806
+ // ============================================================
6807
+ /**
6808
+ * Get events grouped by turn for a session
6809
+ */
6810
+ async getSessionTurns(sessionId, options) {
6811
+ await this.initialize();
6812
+ return this.sqliteStore.getSessionTurns(sessionId, options);
6813
+ }
6814
+ /**
6815
+ * Get all events for a specific turn
6816
+ */
6817
+ async getEventsByTurn(turnId) {
6818
+ await this.initialize();
6819
+ return this.sqliteStore.getEventsByTurn(turnId);
6820
+ }
6821
+ /**
6822
+ * Count total turns for a session
6823
+ */
6824
+ async countSessionTurns(sessionId) {
6825
+ await this.initialize();
6826
+ return this.sqliteStore.countSessionTurns(sessionId);
6827
+ }
6828
+ /**
6829
+ * Backfill turn_ids from metadata for events stored before the migration
6830
+ */
6831
+ async backfillTurnIds() {
6832
+ await this.initialize();
6833
+ return this.sqliteStore.backfillTurnIds();
6834
+ }
6835
+ /**
6836
+ * Delete all events for a session (for force reimport)
6837
+ */
6838
+ async deleteSessionEvents(sessionId) {
6839
+ await this.initialize();
6840
+ return this.sqliteStore.deleteSessionEvents(sessionId);
6841
+ }
5224
6842
  /**
5225
6843
  * Format Endless Mode context for Claude
5226
6844
  */
@@ -5297,7 +6915,7 @@ var MemoryService = class {
5297
6915
  */
5298
6916
  expandPath(p) {
5299
6917
  if (p.startsWith("~")) {
5300
- return path.join(os.homedir(), p.slice(1));
6918
+ return path3.join(os.homedir(), p.slice(1));
5301
6919
  }
5302
6920
  return p;
5303
6921
  }
@@ -5320,6 +6938,7 @@ function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
5320
6938
  serviceCache.set(hash, new MemoryService({
5321
6939
  storagePath,
5322
6940
  projectHash: hash,
6941
+ projectPath,
5323
6942
  // Override shared store config - hooks don't need DuckDB
5324
6943
  sharedStoreConfig: sharedStoreConfig ?? { enabled: false },
5325
6944
  analyticsEnabled: false
@@ -5329,12 +6948,36 @@ function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
5329
6948
  return serviceCache.get(hash);
5330
6949
  }
5331
6950
 
6951
+ // src/server/api/utils.ts
6952
+ function getServiceFromQuery(c) {
6953
+ const project = c.req.query("project");
6954
+ if (project) {
6955
+ const isHash = /^[a-f0-9]{8}$/.test(project);
6956
+ let storagePath;
6957
+ if (isHash) {
6958
+ storagePath = path4.join(os2.homedir(), ".claude-code", "memory", "projects", project);
6959
+ } else {
6960
+ const crypto3 = __require("crypto");
6961
+ const normalized = project.replace(/\/+$/, "") || "/";
6962
+ const hash = crypto3.createHash("sha256").update(normalized).digest("hex").slice(0, 8);
6963
+ storagePath = path4.join(os2.homedir(), ".claude-code", "memory", "projects", hash);
6964
+ }
6965
+ return new MemoryService({
6966
+ storagePath,
6967
+ readOnly: true,
6968
+ analyticsEnabled: false,
6969
+ sharedStoreConfig: { enabled: false }
6970
+ });
6971
+ }
6972
+ return getReadOnlyMemoryService();
6973
+ }
6974
+
5332
6975
  // src/server/api/sessions.ts
5333
6976
  var sessionsRouter = new Hono();
5334
6977
  sessionsRouter.get("/", async (c) => {
5335
6978
  const page = parseInt(c.req.query("page") || "1", 10);
5336
6979
  const pageSize = parseInt(c.req.query("pageSize") || "20", 10);
5337
- const memoryService = getReadOnlyMemoryService();
6980
+ const memoryService = getServiceFromQuery(c);
5338
6981
  try {
5339
6982
  await memoryService.initialize();
5340
6983
  const recentEvents = await memoryService.getRecentEvents(1e3);
@@ -5378,7 +7021,7 @@ sessionsRouter.get("/", async (c) => {
5378
7021
  });
5379
7022
  sessionsRouter.get("/:id", async (c) => {
5380
7023
  const { id } = c.req.param();
5381
- const memoryService = getReadOnlyMemoryService();
7024
+ const memoryService = getServiceFromQuery(c);
5382
7025
  try {
5383
7026
  await memoryService.initialize();
5384
7027
  const events = await memoryService.getSessionHistory(id);
@@ -5419,18 +7062,36 @@ var eventsRouter = new Hono2();
5419
7062
  eventsRouter.get("/", async (c) => {
5420
7063
  const sessionId = c.req.query("sessionId");
5421
7064
  const eventType = c.req.query("type");
7065
+ const level = c.req.query("level");
7066
+ const sort = c.req.query("sort") || "recent";
5422
7067
  const limit = parseInt(c.req.query("limit") || "100", 10);
5423
7068
  const offset = parseInt(c.req.query("offset") || "0", 10);
5424
- const memoryService = getReadOnlyMemoryService();
7069
+ const memoryService = getServiceFromQuery(c);
5425
7070
  try {
5426
7071
  await memoryService.initialize();
5427
- let events = await memoryService.getRecentEvents(limit + offset + 1e3);
7072
+ let events;
7073
+ if (level) {
7074
+ events = await memoryService.getEventsByLevel(level, { limit: limit + offset + 1e3, offset: 0 });
7075
+ } else {
7076
+ events = await memoryService.getRecentEvents(limit + offset + 1e3);
7077
+ }
5428
7078
  if (sessionId) {
5429
7079
  events = events.filter((e) => e.sessionId === sessionId);
5430
7080
  }
5431
7081
  if (eventType) {
5432
7082
  events = events.filter((e) => e.eventType === eventType);
5433
7083
  }
7084
+ if (sort === "accessed") {
7085
+ events.sort((a, b) => {
7086
+ const aTime = a.last_accessed_at || "";
7087
+ const bTime = b.last_accessed_at || "";
7088
+ return bTime.localeCompare(aTime);
7089
+ });
7090
+ } else if (sort === "most-accessed") {
7091
+ events.sort((a, b) => (b.access_count || 0) - (a.access_count || 0));
7092
+ } else if (sort === "oldest") {
7093
+ events.sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
7094
+ }
5434
7095
  const total = events.length;
5435
7096
  events = events.slice(offset, offset + limit);
5436
7097
  return c.json({
@@ -5440,7 +7101,9 @@ eventsRouter.get("/", async (c) => {
5440
7101
  timestamp: e.timestamp,
5441
7102
  sessionId: e.sessionId,
5442
7103
  preview: e.content.slice(0, 200) + (e.content.length > 200 ? "..." : ""),
5443
- contentLength: e.content.length
7104
+ contentLength: e.content.length,
7105
+ accessCount: e.access_count || 0,
7106
+ lastAccessedAt: e.last_accessed_at || null
5444
7107
  })),
5445
7108
  total,
5446
7109
  limit,
@@ -5455,7 +7118,7 @@ eventsRouter.get("/", async (c) => {
5455
7118
  });
5456
7119
  eventsRouter.get("/:id", async (c) => {
5457
7120
  const { id } = c.req.param();
5458
- const memoryService = getReadOnlyMemoryService();
7121
+ const memoryService = getServiceFromQuery(c);
5459
7122
  try {
5460
7123
  await memoryService.initialize();
5461
7124
  const recentEvents = await memoryService.getRecentEvents(1e4);
@@ -5495,7 +7158,7 @@ eventsRouter.get("/:id", async (c) => {
5495
7158
  import { Hono as Hono3 } from "hono";
5496
7159
  var searchRouter = new Hono3();
5497
7160
  searchRouter.post("/", async (c) => {
5498
- const memoryService = getReadOnlyMemoryService();
7161
+ const memoryService = getServiceFromQuery(c);
5499
7162
  try {
5500
7163
  const body = await c.req.json();
5501
7164
  if (!body.query) {
@@ -5539,7 +7202,7 @@ searchRouter.get("/", async (c) => {
5539
7202
  return c.json({ error: 'Query parameter "q" is required' }, 400);
5540
7203
  }
5541
7204
  const topK = parseInt(c.req.query("topK") || "5", 10);
5542
- const memoryService = getReadOnlyMemoryService();
7205
+ const memoryService = getServiceFromQuery(c);
5543
7206
  try {
5544
7207
  await memoryService.initialize();
5545
7208
  const result = await memoryService.retrieveMemories(query, { topK });
@@ -5567,7 +7230,7 @@ searchRouter.get("/", async (c) => {
5567
7230
  import { Hono as Hono4 } from "hono";
5568
7231
  var statsRouter = new Hono4();
5569
7232
  statsRouter.get("/shared", async (c) => {
5570
- const memoryService = getReadOnlyMemoryService();
7233
+ const memoryService = getServiceFromQuery(c);
5571
7234
  try {
5572
7235
  await memoryService.initialize();
5573
7236
  const sharedStats = await memoryService.getSharedStoreStats();
@@ -5624,7 +7287,7 @@ statsRouter.get("/levels/:level", async (c) => {
5624
7287
  if (!validLevels.includes(level)) {
5625
7288
  return c.json({ error: `Invalid level. Must be one of: ${validLevels.join(", ")}` }, 400);
5626
7289
  }
5627
- const memoryService = getReadOnlyMemoryService();
7290
+ const memoryService = getServiceFromQuery(c);
5628
7291
  try {
5629
7292
  await memoryService.initialize();
5630
7293
  let events = await memoryService.getEventsByLevel(level, { limit: limit * 2, offset });
@@ -5671,7 +7334,7 @@ statsRouter.get("/levels/:level", async (c) => {
5671
7334
  }
5672
7335
  });
5673
7336
  statsRouter.get("/", async (c) => {
5674
- const memoryService = getReadOnlyMemoryService();
7337
+ const memoryService = getServiceFromQuery(c);
5675
7338
  try {
5676
7339
  await memoryService.initialize();
5677
7340
  const stats = await memoryService.getStats();
@@ -5688,6 +7351,7 @@ statsRouter.get("/", async (c) => {
5688
7351
  acc[day] = (acc[day] || 0) + 1;
5689
7352
  return acc;
5690
7353
  }, {});
7354
+ const retrievalTrace = await memoryService.getRetrievalTraceStats();
5691
7355
  return c.json({
5692
7356
  storage: {
5693
7357
  eventCount: stats.totalEvents,
@@ -5705,7 +7369,8 @@ statsRouter.get("/", async (c) => {
5705
7369
  heapUsed: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
5706
7370
  heapTotal: Math.round(process.memoryUsage().heapTotal / 1024 / 1024)
5707
7371
  },
5708
- levelStats: stats.levelStats
7372
+ levelStats: stats.levelStats,
7373
+ retrievalTrace
5709
7374
  });
5710
7375
  } catch (error) {
5711
7376
  return c.json({ error: error.message }, 500);
@@ -5715,7 +7380,7 @@ statsRouter.get("/", async (c) => {
5715
7380
  });
5716
7381
  statsRouter.get("/most-accessed", async (c) => {
5717
7382
  const limit = parseInt(c.req.query("limit") || "10", 10);
5718
- const memoryService = getReadOnlyMemoryService();
7383
+ const memoryService = getServiceFromQuery(c);
5719
7384
  try {
5720
7385
  await memoryService.initialize();
5721
7386
  console.log("[most-accessed] Fetching most accessed memories, limit:", limit);
@@ -5746,7 +7411,7 @@ statsRouter.get("/most-accessed", async (c) => {
5746
7411
  });
5747
7412
  statsRouter.get("/timeline", async (c) => {
5748
7413
  const days = parseInt(c.req.query("days") || "7", 10);
5749
- const memoryService = getReadOnlyMemoryService();
7414
+ const memoryService = getServiceFromQuery(c);
5750
7415
  try {
5751
7416
  await memoryService.initialize();
5752
7417
  const recentEvents = await memoryService.getRecentEvents(1e4);
@@ -5776,8 +7441,75 @@ statsRouter.get("/timeline", async (c) => {
5776
7441
  await memoryService.shutdown();
5777
7442
  }
5778
7443
  });
7444
+ statsRouter.get("/helpfulness", async (c) => {
7445
+ const limit = parseInt(c.req.query("limit") || "10", 10);
7446
+ const memoryService = getServiceFromQuery(c);
7447
+ try {
7448
+ await memoryService.initialize();
7449
+ const stats = await memoryService.getHelpfulnessStats();
7450
+ const topMemories = await memoryService.getHelpfulMemories(limit);
7451
+ return c.json({
7452
+ ...stats,
7453
+ topMemories: topMemories.map((m) => ({
7454
+ eventId: m.eventId,
7455
+ summary: m.summary,
7456
+ helpfulnessScore: m.helpfulnessScore,
7457
+ accessCount: m.accessCount,
7458
+ evaluationCount: m.evaluationCount
7459
+ }))
7460
+ });
7461
+ } catch (error) {
7462
+ return c.json({
7463
+ avgScore: 0,
7464
+ totalEvaluated: 0,
7465
+ totalRetrievals: 0,
7466
+ helpful: 0,
7467
+ neutral: 0,
7468
+ unhelpful: 0,
7469
+ topMemories: []
7470
+ });
7471
+ } finally {
7472
+ await memoryService.shutdown();
7473
+ }
7474
+ });
7475
+ statsRouter.get("/retrieval-traces", async (c) => {
7476
+ const limit = parseInt(c.req.query("limit") || "50", 10);
7477
+ const memoryService = getServiceFromQuery(c);
7478
+ try {
7479
+ await memoryService.initialize();
7480
+ const traces = await memoryService.getRecentRetrievalTraces(limit);
7481
+ const traceStats = await memoryService.getRetrievalTraceStats();
7482
+ return c.json({
7483
+ stats: traceStats,
7484
+ traces: traces.map((t) => ({
7485
+ traceId: t.traceId,
7486
+ sessionId: t.sessionId || null,
7487
+ projectHash: t.projectHash || null,
7488
+ queryText: t.queryText,
7489
+ strategy: t.strategy || null,
7490
+ candidateEventIds: t.candidateEventIds,
7491
+ selectedEventIds: t.selectedEventIds,
7492
+ candidateDetails: t.candidateDetails || [],
7493
+ selectedDetails: t.selectedDetails || [],
7494
+ candidateCount: t.candidateCount,
7495
+ selectedCount: t.selectedCount,
7496
+ confidence: t.confidence || null,
7497
+ fallbackTrace: t.fallbackTrace,
7498
+ createdAt: t.createdAt.toISOString()
7499
+ }))
7500
+ });
7501
+ } catch (error) {
7502
+ return c.json({
7503
+ stats: { totalQueries: 0, avgCandidateCount: 0, avgSelectedCount: 0, selectionRate: 0 },
7504
+ traces: [],
7505
+ error: error.message
7506
+ }, 500);
7507
+ } finally {
7508
+ await memoryService.shutdown();
7509
+ }
7510
+ });
5779
7511
  statsRouter.post("/graduation/run", async (c) => {
5780
- const memoryService = getReadOnlyMemoryService();
7512
+ const memoryService = getServiceFromQuery(c);
5781
7513
  try {
5782
7514
  await memoryService.initialize();
5783
7515
  const result = await memoryService.forceGraduation();
@@ -5838,7 +7570,7 @@ var citationsRouter = new Hono5();
5838
7570
  citationsRouter.get("/:id", async (c) => {
5839
7571
  const { id } = c.req.param();
5840
7572
  const citationId = parseCitationId(id) || id;
5841
- const memoryService = getReadOnlyMemoryService();
7573
+ const memoryService = getServiceFromQuery(c);
5842
7574
  try {
5843
7575
  await memoryService.initialize();
5844
7576
  const recentEvents = await memoryService.getRecentEvents(1e4);
@@ -5872,7 +7604,7 @@ citationsRouter.get("/:id", async (c) => {
5872
7604
  citationsRouter.get("/:id/related", async (c) => {
5873
7605
  const { id } = c.req.param();
5874
7606
  const citationId = parseCitationId(id) || id;
5875
- const memoryService = getReadOnlyMemoryService();
7607
+ const memoryService = getServiceFromQuery(c);
5876
7608
  try {
5877
7609
  await memoryService.initialize();
5878
7610
  const recentEvents = await memoryService.getRecentEvents(1e4);
@@ -5908,23 +7640,415 @@ citationsRouter.get("/:id/related", async (c) => {
5908
7640
  }
5909
7641
  });
5910
7642
 
7643
+ // src/server/api/turns.ts
7644
+ import { Hono as Hono6 } from "hono";
7645
+ var turnsRouter = new Hono6();
7646
+ turnsRouter.get("/", async (c) => {
7647
+ const sessionId = c.req.query("sessionId");
7648
+ const limit = parseInt(c.req.query("limit") || "20", 10);
7649
+ const offset = parseInt(c.req.query("offset") || "0", 10);
7650
+ if (!sessionId) {
7651
+ return c.json({ error: "sessionId is required" }, 400);
7652
+ }
7653
+ const memoryService = getServiceFromQuery(c);
7654
+ try {
7655
+ await memoryService.initialize();
7656
+ const turns = await memoryService.getSessionTurns(sessionId, { limit, offset });
7657
+ const totalTurns = await memoryService.countSessionTurns(sessionId);
7658
+ return c.json({
7659
+ turns: turns.map((t) => ({
7660
+ turnId: t.turnId,
7661
+ startedAt: t.startedAt.toISOString(),
7662
+ promptPreview: t.promptPreview,
7663
+ eventCount: t.eventCount,
7664
+ toolCount: t.toolCount,
7665
+ hasResponse: t.hasResponse,
7666
+ events: t.events.map((e) => ({
7667
+ id: e.id,
7668
+ eventType: e.eventType,
7669
+ timestamp: e.timestamp instanceof Date ? e.timestamp.toISOString() : e.timestamp,
7670
+ preview: e.content.slice(0, 300) + (e.content.length > 300 ? "..." : ""),
7671
+ contentLength: e.content.length
7672
+ }))
7673
+ })),
7674
+ total: totalTurns,
7675
+ limit,
7676
+ offset,
7677
+ hasMore: offset + limit < totalTurns
7678
+ });
7679
+ } catch (error) {
7680
+ return c.json({ error: error.message }, 500);
7681
+ } finally {
7682
+ await memoryService.shutdown();
7683
+ }
7684
+ });
7685
+ turnsRouter.get("/:turnId", async (c) => {
7686
+ const { turnId } = c.req.param();
7687
+ const memoryService = getServiceFromQuery(c);
7688
+ try {
7689
+ await memoryService.initialize();
7690
+ const events = await memoryService.getEventsByTurn(turnId);
7691
+ if (events.length === 0) {
7692
+ return c.json({ error: "Turn not found" }, 404);
7693
+ }
7694
+ const promptEvent = events.find((e) => e.eventType === "user_prompt");
7695
+ const toolEvents = events.filter((e) => e.eventType === "tool_observation");
7696
+ const responseEvents = events.filter((e) => e.eventType === "agent_response");
7697
+ return c.json({
7698
+ turnId,
7699
+ sessionId: events[0].sessionId,
7700
+ startedAt: events[0].timestamp instanceof Date ? events[0].timestamp.toISOString() : events[0].timestamp,
7701
+ prompt: promptEvent ? {
7702
+ id: promptEvent.id,
7703
+ content: promptEvent.content,
7704
+ timestamp: promptEvent.timestamp instanceof Date ? promptEvent.timestamp.toISOString() : promptEvent.timestamp
7705
+ } : null,
7706
+ tools: toolEvents.map((e) => {
7707
+ let toolName = "";
7708
+ let success = true;
7709
+ try {
7710
+ const parsed = JSON.parse(e.content);
7711
+ toolName = parsed.toolName || "";
7712
+ success = parsed.success !== false;
7713
+ } catch {
7714
+ }
7715
+ return {
7716
+ id: e.id,
7717
+ toolName,
7718
+ success,
7719
+ timestamp: e.timestamp instanceof Date ? e.timestamp.toISOString() : e.timestamp,
7720
+ preview: e.content.slice(0, 500) + (e.content.length > 500 ? "..." : "")
7721
+ };
7722
+ }),
7723
+ responses: responseEvents.map((e) => ({
7724
+ id: e.id,
7725
+ content: e.content,
7726
+ timestamp: e.timestamp instanceof Date ? e.timestamp.toISOString() : e.timestamp
7727
+ })),
7728
+ totalEvents: events.length
7729
+ });
7730
+ } catch (error) {
7731
+ return c.json({ error: error.message }, 500);
7732
+ } finally {
7733
+ await memoryService.shutdown();
7734
+ }
7735
+ });
7736
+ turnsRouter.post("/backfill", async (c) => {
7737
+ const memoryService = getServiceFromQuery(c);
7738
+ try {
7739
+ await memoryService.initialize();
7740
+ const updated = await memoryService.backfillTurnIds();
7741
+ return c.json({
7742
+ success: true,
7743
+ updated,
7744
+ message: `Backfilled turn_id for ${updated} events`
7745
+ });
7746
+ } catch (error) {
7747
+ return c.json({
7748
+ success: false,
7749
+ error: error.message
7750
+ }, 500);
7751
+ } finally {
7752
+ await memoryService.shutdown();
7753
+ }
7754
+ });
7755
+
7756
+ // src/server/api/projects.ts
7757
+ import { Hono as Hono7 } from "hono";
7758
+ import * as fs5 from "fs";
7759
+ import * as path5 from "path";
7760
+ import * as os3 from "os";
7761
+ var projectsRouter = new Hono7();
7762
+ projectsRouter.get("/", async (c) => {
7763
+ try {
7764
+ const projectsDir = path5.join(os3.homedir(), ".claude-code", "memory", "projects");
7765
+ if (!fs5.existsSync(projectsDir)) {
7766
+ return c.json({ projects: [] });
7767
+ }
7768
+ const projectHashes = fs5.readdirSync(projectsDir).filter((name) => {
7769
+ const fullPath = path5.join(projectsDir, name);
7770
+ return fs5.statSync(fullPath).isDirectory();
7771
+ });
7772
+ const registry = loadSessionRegistry();
7773
+ const hashToPath = /* @__PURE__ */ new Map();
7774
+ for (const entry of Object.values(registry.sessions)) {
7775
+ if (!hashToPath.has(entry.projectHash)) {
7776
+ hashToPath.set(entry.projectHash, entry.projectPath);
7777
+ }
7778
+ }
7779
+ const projects = projectHashes.map((hash) => {
7780
+ const dirPath = path5.join(projectsDir, hash);
7781
+ const dbPath = path5.join(dirPath, "events.sqlite");
7782
+ let dbSize = 0;
7783
+ if (fs5.existsSync(dbPath)) {
7784
+ dbSize = fs5.statSync(dbPath).size;
7785
+ }
7786
+ const projectPath = hashToPath.get(hash) || `unknown (${hash})`;
7787
+ return {
7788
+ hash,
7789
+ projectPath,
7790
+ projectName: path5.basename(projectPath),
7791
+ dbSize,
7792
+ dbSizeHuman: formatBytes(dbSize)
7793
+ };
7794
+ });
7795
+ projects.sort((a, b) => a.projectName.localeCompare(b.projectName));
7796
+ return c.json({ projects });
7797
+ } catch (error) {
7798
+ return c.json({ projects: [], error: error.message }, 500);
7799
+ }
7800
+ });
7801
+ function formatBytes(bytes) {
7802
+ if (bytes === 0)
7803
+ return "0 B";
7804
+ const k = 1024;
7805
+ const sizes = ["B", "KB", "MB", "GB"];
7806
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
7807
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + " " + sizes[i];
7808
+ }
7809
+
7810
+ // src/server/api/chat.ts
7811
+ import { Hono as Hono8 } from "hono";
7812
+ import { streamSSE } from "hono/streaming";
7813
+ import { spawn } from "child_process";
7814
+ var chatRouter = new Hono8();
7815
+ var CLAUDE_TIMEOUT_MS = 12e4;
7816
+ chatRouter.post("/", async (c) => {
7817
+ let body;
7818
+ try {
7819
+ body = await c.req.json();
7820
+ } catch {
7821
+ return c.json({ error: "Invalid JSON body" }, 400);
7822
+ }
7823
+ if (!body.message?.trim()) {
7824
+ return c.json({ error: "Message is required" }, 400);
7825
+ }
7826
+ const memoryService = getServiceFromQuery(c);
7827
+ try {
7828
+ await memoryService.initialize();
7829
+ let memoryContext = "";
7830
+ let statsContext = "";
7831
+ try {
7832
+ const result = await memoryService.retrieveMemories(body.message, {
7833
+ topK: 8,
7834
+ minScore: 0.5
7835
+ });
7836
+ if (result.memories.length > 0) {
7837
+ const parts = ["## Relevant Memories\n"];
7838
+ for (const m of result.memories) {
7839
+ const date = new Date(m.event.timestamp).toISOString().split("T")[0];
7840
+ const content = m.event.content.slice(0, 500);
7841
+ parts.push(`### [${m.event.eventType}] ${date} (score: ${m.score.toFixed(2)})`);
7842
+ parts.push(content);
7843
+ if (m.sessionContext) {
7844
+ parts.push(`_Context: ${m.sessionContext}_`);
7845
+ }
7846
+ parts.push("");
7847
+ }
7848
+ memoryContext = parts.join("\n");
7849
+ }
7850
+ } catch {
7851
+ }
7852
+ try {
7853
+ const stats = await memoryService.getStats();
7854
+ const levels = stats.levelStats.map((l) => `${l.level}: ${l.count}`).join(", ");
7855
+ statsContext = [
7856
+ "## Memory Stats",
7857
+ `- Total events: ${stats.totalEvents}`,
7858
+ `- Vector nodes: ${stats.vectorCount}`,
7859
+ `- By level: ${levels}`
7860
+ ].join("\n");
7861
+ } catch {
7862
+ }
7863
+ const fullPrompt = buildPrompt(
7864
+ statsContext,
7865
+ memoryContext,
7866
+ body.history || [],
7867
+ body.message
7868
+ );
7869
+ return streamSSE(c, async (stream) => {
7870
+ try {
7871
+ await streamClaudeResponse(fullPrompt, stream);
7872
+ } catch (err) {
7873
+ await stream.writeSSE({
7874
+ event: "error",
7875
+ data: JSON.stringify({ error: err.message })
7876
+ });
7877
+ }
7878
+ });
7879
+ } catch (error) {
7880
+ return c.json({ error: error.message }, 500);
7881
+ } finally {
7882
+ await memoryService.shutdown();
7883
+ }
7884
+ });
7885
+ function buildPrompt(statsContext, memoryContext, history, currentMessage) {
7886
+ const parts = [];
7887
+ parts.push("You are a helpful assistant that answers questions about the user's code memory data.");
7888
+ parts.push("The memory system tracks coding sessions, tool usage, prompts, and responses.");
7889
+ parts.push("Answer concisely based on the memory context below. If you don't have enough data, say so.");
7890
+ parts.push("Use markdown formatting in your responses.\n");
7891
+ if (statsContext) {
7892
+ parts.push(statsContext);
7893
+ parts.push("");
7894
+ }
7895
+ if (memoryContext) {
7896
+ parts.push(memoryContext);
7897
+ } else {
7898
+ parts.push("No directly relevant memories found for this query.");
7899
+ parts.push("Answer based on general knowledge or suggest the user rephrase.\n");
7900
+ }
7901
+ parts.push("---\n");
7902
+ const recentHistory = history.slice(-10);
7903
+ if (recentHistory.length > 0) {
7904
+ parts.push("## Conversation History\n");
7905
+ for (const msg of recentHistory) {
7906
+ const prefix = msg.role === "user" ? "User" : "Assistant";
7907
+ parts.push(`**${prefix}:** ${msg.content}
7908
+ `);
7909
+ }
7910
+ }
7911
+ parts.push(`**User:** ${currentMessage}`);
7912
+ return parts.join("\n");
7913
+ }
7914
+ function streamClaudeResponse(prompt, stream) {
7915
+ return new Promise((resolve2, reject) => {
7916
+ const proc = spawn("claude", [
7917
+ "-p",
7918
+ "--output-format",
7919
+ "stream-json",
7920
+ "--verbose"
7921
+ ], {
7922
+ stdio: ["pipe", "pipe", "pipe"],
7923
+ env: { ...process.env }
7924
+ });
7925
+ const timeout = setTimeout(() => {
7926
+ proc.kill("SIGTERM");
7927
+ reject(new Error("Chat response timed out after 2 minutes"));
7928
+ }, CLAUDE_TIMEOUT_MS);
7929
+ proc.stdin.write(prompt);
7930
+ proc.stdin.end();
7931
+ let buffer = "";
7932
+ let lastSentText = "";
7933
+ proc.stdout.on("data", async (chunk) => {
7934
+ buffer += chunk.toString();
7935
+ const lines = buffer.split("\n");
7936
+ buffer = lines.pop() || "";
7937
+ for (const line of lines) {
7938
+ if (!line.trim())
7939
+ continue;
7940
+ try {
7941
+ const parsed = JSON.parse(line);
7942
+ if (parsed.type === "assistant" && parsed.message?.content) {
7943
+ const textBlocks = parsed.message.content.filter((b) => b.type === "text").map((b) => b.text).join("");
7944
+ if (textBlocks.length > lastSentText.length) {
7945
+ const delta = textBlocks.slice(lastSentText.length);
7946
+ lastSentText = textBlocks;
7947
+ await stream.writeSSE({
7948
+ event: "message",
7949
+ data: JSON.stringify({ content: delta })
7950
+ });
7951
+ }
7952
+ }
7953
+ if (parsed.type === "result") {
7954
+ await stream.writeSSE({ event: "done", data: "{}" });
7955
+ }
7956
+ } catch {
7957
+ }
7958
+ }
7959
+ });
7960
+ proc.stderr.on("data", (chunk) => {
7961
+ if (process.env.CLAUDE_MEMORY_DEBUG) {
7962
+ console.error("[chat] claude stderr:", chunk.toString());
7963
+ }
7964
+ });
7965
+ proc.on("error", (err) => {
7966
+ clearTimeout(timeout);
7967
+ if (err.code === "ENOENT") {
7968
+ reject(new Error("Claude CLI not found. Install with: npm install -g @anthropic-ai/claude-code"));
7969
+ } else {
7970
+ reject(err);
7971
+ }
7972
+ });
7973
+ proc.on("close", async (code) => {
7974
+ clearTimeout(timeout);
7975
+ if (buffer.trim()) {
7976
+ try {
7977
+ const parsed = JSON.parse(buffer);
7978
+ if (parsed.type === "result") {
7979
+ await stream.writeSSE({ event: "done", data: "{}" });
7980
+ }
7981
+ } catch {
7982
+ }
7983
+ }
7984
+ if (code !== 0 && code !== null) {
7985
+ reject(new Error(`Claude CLI exited with code ${code}`));
7986
+ } else {
7987
+ resolve2();
7988
+ }
7989
+ });
7990
+ });
7991
+ }
7992
+
7993
+ // src/server/api/health.ts
7994
+ import { Hono as Hono9 } from "hono";
7995
+ var healthRouter = new Hono9();
7996
+ healthRouter.get("/", async (c) => {
7997
+ const memoryService = getServiceFromQuery(c);
7998
+ try {
7999
+ await memoryService.initialize();
8000
+ const [stats, outbox] = await Promise.all([
8001
+ memoryService.getStats(),
8002
+ memoryService.getOutboxStats()
8003
+ ]);
8004
+ const outboxPending = outbox.embedding.pending + outbox.vector.pending;
8005
+ const outboxFailed = outbox.embedding.failed + outbox.vector.failed;
8006
+ const status = outboxFailed > 0 ? "needs-attention" : "ok";
8007
+ return c.json({
8008
+ status,
8009
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
8010
+ storage: {
8011
+ totalEvents: stats.totalEvents,
8012
+ vectorCount: stats.vectorCount
8013
+ },
8014
+ outbox: {
8015
+ embedding: outbox.embedding,
8016
+ vector: outbox.vector,
8017
+ totals: {
8018
+ pending: outboxPending,
8019
+ failed: outboxFailed
8020
+ }
8021
+ },
8022
+ levelStats: stats.levelStats
8023
+ });
8024
+ } catch (error) {
8025
+ return c.json({
8026
+ status: "error",
8027
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
8028
+ error: error.message
8029
+ }, 500);
8030
+ } finally {
8031
+ await memoryService.shutdown();
8032
+ }
8033
+ });
8034
+
5911
8035
  // src/server/api/index.ts
5912
- var apiRouter = new Hono6().route("/sessions", sessionsRouter).route("/events", eventsRouter).route("/search", searchRouter).route("/stats", statsRouter).route("/citations", citationsRouter);
8036
+ var apiRouter = new Hono10().route("/sessions", sessionsRouter).route("/events", eventsRouter).route("/search", searchRouter).route("/stats", statsRouter).route("/citations", citationsRouter).route("/turns", turnsRouter).route("/projects", projectsRouter).route("/chat", chatRouter).route("/health", healthRouter);
5913
8037
 
5914
8038
  // src/server/index.ts
5915
- var app = new Hono7();
8039
+ var app = new Hono11();
5916
8040
  app.use("/*", cors());
5917
8041
  app.use("/*", logger());
5918
8042
  app.route("/api", apiRouter);
5919
8043
  app.get("/health", (c) => c.json({ status: "ok", timestamp: (/* @__PURE__ */ new Date()).toISOString() }));
5920
- var uiPath = path2.join(__dirname, "../../dist/ui");
5921
- if (fs2.existsSync(uiPath)) {
8044
+ var uiPath = path6.join(__dirname, "../../dist/ui");
8045
+ if (fs6.existsSync(uiPath)) {
5922
8046
  app.use("/*", serveStatic({ root: uiPath }));
5923
8047
  }
5924
8048
  app.get("*", (c) => {
5925
- const indexPath = path2.join(uiPath, "index.html");
5926
- if (fs2.existsSync(indexPath)) {
5927
- return c.html(fs2.readFileSync(indexPath, "utf-8"));
8049
+ const indexPath = path6.join(uiPath, "index.html");
8050
+ if (fs6.existsSync(indexPath)) {
8051
+ return c.html(fs6.readFileSync(indexPath, "utf-8"));
5928
8052
  }
5929
8053
  return c.text('UI not built. Run "npm run build:ui" first.', 404);
5930
8054
  });