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
@@ -6,10 +6,13 @@ const require = createRequire(import.meta.url);
6
6
  const __filename = fileURLToPath(import.meta.url);
7
7
  const __dirname = dirname(__filename);
8
8
 
9
+ // src/hooks/user-prompt-submit.ts
10
+ import { randomUUID as randomUUID9 } from "crypto";
11
+
9
12
  // src/services/memory-service.ts
10
- import * as path from "path";
13
+ import * as path3 from "path";
11
14
  import * as os from "os";
12
- import * as fs from "fs";
15
+ import * as fs4 from "fs";
13
16
  import * as crypto2 from "crypto";
14
17
 
15
18
  // src/core/event-store.ts
@@ -67,11 +70,11 @@ function toDate(value) {
67
70
  return new Date(value);
68
71
  return new Date(String(value));
69
72
  }
70
- function createDatabase(path2, options) {
73
+ function createDatabase(path5, options) {
71
74
  if (options?.readOnly) {
72
- return new duckdb.Database(path2, { access_mode: "READ_ONLY" });
75
+ return new duckdb.Database(path5, { access_mode: "READ_ONLY" });
73
76
  }
74
- return new duckdb.Database(path2);
77
+ return new duckdb.Database(path5);
75
78
  }
76
79
  function dbRun(db, sql, params = []) {
77
80
  return new Promise((resolve2, reject) => {
@@ -335,6 +338,17 @@ var EventStore = class {
335
338
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
336
339
  )
337
340
  `);
341
+ await dbRun(this.db, `
342
+ CREATE TABLE IF NOT EXISTS consolidated_rules (
343
+ rule_id VARCHAR PRIMARY KEY,
344
+ rule TEXT NOT NULL,
345
+ topics JSON,
346
+ source_memory_ids JSON,
347
+ source_events JSON,
348
+ confidence FLOAT DEFAULT 0.5,
349
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
350
+ )
351
+ `);
338
352
  await dbRun(this.db, `
339
353
  CREATE TABLE IF NOT EXISTS endless_config (
340
354
  key VARCHAR PRIMARY KEY,
@@ -354,6 +368,7 @@ var EventStore = class {
354
368
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
355
369
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
356
370
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
371
+ await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
357
372
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
358
373
  this.initialized = true;
359
374
  }
@@ -741,8 +756,14 @@ import { randomUUID as randomUUID2 } from "crypto";
741
756
 
742
757
  // src/core/sqlite-wrapper.ts
743
758
  import Database from "better-sqlite3";
744
- function createSQLiteDatabase(path2, options) {
745
- const db = new Database(path2, {
759
+ import * as fs from "fs";
760
+ import * as nodePath from "path";
761
+ function createSQLiteDatabase(path5, options) {
762
+ const dir = nodePath.dirname(path5);
763
+ if (!fs.existsSync(dir)) {
764
+ fs.mkdirSync(dir, { recursive: true });
765
+ }
766
+ const db = new Database(path5, {
746
767
  readonly: options?.readonly ?? false
747
768
  });
748
769
  if (!options?.readonly && (options?.walMode ?? true)) {
@@ -783,6 +804,64 @@ function toSQLiteTimestamp(date) {
783
804
  return date.toISOString();
784
805
  }
785
806
 
807
+ // src/core/markdown-mirror.ts
808
+ import * as fs2 from "fs/promises";
809
+ import * as path from "path";
810
+ var DEFAULT_NAMESPACE = "default";
811
+ var DEFAULT_CATEGORY = "uncategorized";
812
+ function sanitizeSegment(input, fallback) {
813
+ const raw = String(input ?? "").trim().toLowerCase();
814
+ const safe = raw.normalize("NFKD").replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
815
+ if (!safe || safe === "." || safe === "..")
816
+ return fallback;
817
+ return safe;
818
+ }
819
+ function getCategorySegments(metadata, eventType) {
820
+ const raw = metadata?.categoryPath;
821
+ if (Array.isArray(raw) && raw.length > 0) {
822
+ return raw.map((s) => sanitizeSegment(s, DEFAULT_CATEGORY));
823
+ }
824
+ const single = metadata?.category;
825
+ if (typeof single === "string" && single.trim()) {
826
+ return [sanitizeSegment(single, DEFAULT_CATEGORY)];
827
+ }
828
+ return [sanitizeSegment(eventType, DEFAULT_CATEGORY)];
829
+ }
830
+ function buildMirrorPath(rootDir, event) {
831
+ const metadata = event.metadata;
832
+ const namespace = sanitizeSegment(metadata?.namespace, DEFAULT_NAMESPACE);
833
+ const categories = getCategorySegments(metadata, event.eventType);
834
+ const d = event.timestamp;
835
+ const yyyy = d.getFullYear();
836
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
837
+ const dd = String(d.getDate()).padStart(2, "0");
838
+ return path.join(rootDir, "memory", namespace, ...categories, `${yyyy}-${mm}-${dd}.md`);
839
+ }
840
+ function formatMirrorEntry(event) {
841
+ const category = Array.isArray(event.metadata?.categoryPath) ? event.metadata.categoryPath.join("/") : String(event.metadata?.category ?? event.eventType);
842
+ return [
843
+ "",
844
+ `- ts: ${event.timestamp.toISOString()}`,
845
+ ` id: ${event.id}`,
846
+ ` type: ${event.eventType}`,
847
+ ` session: ${event.sessionId}`,
848
+ ` category: ${category}`,
849
+ " content: |",
850
+ ...event.content.split("\n").map((line) => ` ${line}`)
851
+ ].join("\n") + "\n";
852
+ }
853
+ var MarkdownMirror = class {
854
+ constructor(rootDir) {
855
+ this.rootDir = rootDir;
856
+ }
857
+ async append(event) {
858
+ const outPath = buildMirrorPath(this.rootDir, event);
859
+ await fs2.mkdir(path.dirname(outPath), { recursive: true });
860
+ await fs2.appendFile(outPath, formatMirrorEntry(event), "utf8");
861
+ return outPath;
862
+ }
863
+ };
864
+
786
865
  // src/core/sqlite-event-store.ts
787
866
  var SQLiteEventStore = class {
788
867
  constructor(dbPath, options) {
@@ -792,10 +871,12 @@ var SQLiteEventStore = class {
792
871
  readonly: this.readOnly,
793
872
  walMode: !this.readOnly
794
873
  });
874
+ this.markdownMirror = this.readOnly || !options?.markdownMirrorRoot ? null : new MarkdownMirror(options.markdownMirrorRoot);
795
875
  }
796
876
  db;
797
877
  initialized = false;
798
878
  readOnly;
879
+ markdownMirror;
799
880
  /**
800
881
  * Initialize database schema
801
882
  */
@@ -1002,6 +1083,17 @@ var SQLiteEventStore = class {
1002
1083
  created_at TEXT DEFAULT (datetime('now'))
1003
1084
  );
1004
1085
 
1086
+ -- Consolidated Rules table (long-term stable memory)
1087
+ CREATE TABLE IF NOT EXISTS consolidated_rules (
1088
+ rule_id TEXT PRIMARY KEY,
1089
+ rule TEXT NOT NULL,
1090
+ topics TEXT,
1091
+ source_memory_ids TEXT,
1092
+ source_events TEXT,
1093
+ confidence REAL DEFAULT 0.5,
1094
+ created_at TEXT DEFAULT (datetime('now'))
1095
+ );
1096
+
1005
1097
  -- Endless Mode Config table
1006
1098
  CREATE TABLE IF NOT EXISTS endless_config (
1007
1099
  key TEXT PRIMARY KEY,
@@ -1009,6 +1101,41 @@ var SQLiteEventStore = class {
1009
1101
  updated_at TEXT DEFAULT (datetime('now'))
1010
1102
  );
1011
1103
 
1104
+ -- Memory Helpfulness tracking
1105
+ CREATE TABLE IF NOT EXISTS memory_helpfulness (
1106
+ id TEXT PRIMARY KEY,
1107
+ event_id TEXT NOT NULL,
1108
+ session_id TEXT NOT NULL,
1109
+ retrieval_score REAL DEFAULT 0,
1110
+ query_preview TEXT,
1111
+ session_continued INTEGER DEFAULT 0,
1112
+ prompt_count_after INTEGER DEFAULT 0,
1113
+ tool_success_count INTEGER DEFAULT 0,
1114
+ tool_total_count INTEGER DEFAULT 0,
1115
+ was_reasked INTEGER DEFAULT 0,
1116
+ helpfulness_score REAL DEFAULT 0.5,
1117
+ created_at TEXT DEFAULT (datetime('now')),
1118
+ measured_at TEXT
1119
+ );
1120
+
1121
+ -- Retrieval trace log (query -> candidates -> selected for context)
1122
+ CREATE TABLE IF NOT EXISTS retrieval_traces (
1123
+ trace_id TEXT PRIMARY KEY,
1124
+ session_id TEXT,
1125
+ project_hash TEXT,
1126
+ query_text TEXT NOT NULL,
1127
+ strategy TEXT,
1128
+ candidate_event_ids TEXT,
1129
+ selected_event_ids TEXT,
1130
+ candidate_details_json TEXT,
1131
+ selected_details_json TEXT,
1132
+ candidate_count INTEGER DEFAULT 0,
1133
+ selected_count INTEGER DEFAULT 0,
1134
+ confidence TEXT,
1135
+ fallback_trace TEXT,
1136
+ created_at TEXT DEFAULT (datetime('now'))
1137
+ );
1138
+
1012
1139
  -- Sync position tracking (for SQLite -> DuckDB sync)
1013
1140
  CREATE TABLE IF NOT EXISTS sync_positions (
1014
1141
  target_name TEXT PRIMARY KEY,
@@ -1033,7 +1160,14 @@ var SQLiteEventStore = class {
1033
1160
  CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score);
1034
1161
  CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
1035
1162
  CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
1163
+ CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence);
1036
1164
  CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
1165
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
1166
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
1167
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
1168
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_created_at ON retrieval_traces(created_at DESC);
1169
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_project_hash ON retrieval_traces(project_hash);
1170
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_session_id ON retrieval_traces(session_id);
1037
1171
 
1038
1172
  -- FTS5 Full-Text Search for fast keyword search
1039
1173
  CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
@@ -1057,6 +1191,14 @@ var SQLiteEventStore = class {
1057
1191
  INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
1058
1192
  END;
1059
1193
  `);
1194
+ try {
1195
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN selected_details_json TEXT;`);
1196
+ } catch {
1197
+ }
1198
+ try {
1199
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN candidate_details_json TEXT;`);
1200
+ } catch {
1201
+ }
1060
1202
  const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
1061
1203
  const columnNames = tableInfo.map((col) => col.name);
1062
1204
  if (!columnNames.includes("access_count")) {
@@ -1077,6 +1219,15 @@ var SQLiteEventStore = class {
1077
1219
  console.error("Error adding last_accessed_at column:", err);
1078
1220
  }
1079
1221
  }
1222
+ if (!columnNames.includes("turn_id")) {
1223
+ try {
1224
+ sqliteExec(this.db, `
1225
+ ALTER TABLE events ADD COLUMN turn_id TEXT;
1226
+ `);
1227
+ } catch (err) {
1228
+ console.error("Error adding turn_id column:", err);
1229
+ }
1230
+ }
1080
1231
  try {
1081
1232
  sqliteExec(this.db, `
1082
1233
  CREATE INDEX IF NOT EXISTS idx_events_access_count ON events(access_count DESC);
@@ -1089,6 +1240,12 @@ var SQLiteEventStore = class {
1089
1240
  `);
1090
1241
  } catch (err) {
1091
1242
  }
1243
+ try {
1244
+ sqliteExec(this.db, `
1245
+ CREATE INDEX IF NOT EXISTS idx_events_turn_id ON events(turn_id);
1246
+ `);
1247
+ } catch (err) {
1248
+ }
1092
1249
  this.initialized = true;
1093
1250
  }
1094
1251
  /**
@@ -1113,9 +1270,11 @@ var SQLiteEventStore = class {
1113
1270
  const id = randomUUID2();
1114
1271
  const timestamp = toSQLiteTimestamp(input.timestamp);
1115
1272
  try {
1273
+ const metadata = input.metadata || {};
1274
+ const turnId = metadata.turnId || null;
1116
1275
  const insertEvent = this.db.prepare(`
1117
- INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
1118
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1276
+ INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
1277
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1119
1278
  `);
1120
1279
  const insertDedup = this.db.prepare(`
1121
1280
  INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
@@ -1132,12 +1291,28 @@ var SQLiteEventStore = class {
1132
1291
  input.content,
1133
1292
  canonicalKey,
1134
1293
  dedupeKey,
1135
- JSON.stringify(input.metadata || {})
1294
+ JSON.stringify(metadata),
1295
+ turnId
1136
1296
  );
1137
1297
  insertDedup.run(dedupeKey, id);
1138
1298
  insertLevel.run(id);
1139
1299
  });
1140
1300
  transaction();
1301
+ if (this.markdownMirror) {
1302
+ const event = {
1303
+ id,
1304
+ eventType: input.eventType,
1305
+ sessionId: input.sessionId,
1306
+ timestamp: input.timestamp,
1307
+ content: input.content,
1308
+ canonicalKey,
1309
+ dedupeKey,
1310
+ metadata
1311
+ };
1312
+ this.markdownMirror.append(event).catch((err) => {
1313
+ console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
1314
+ });
1315
+ }
1141
1316
  return { success: true, eventId: id, isDuplicate: false };
1142
1317
  } catch (error) {
1143
1318
  return {
@@ -1196,6 +1371,92 @@ var SQLiteEventStore = class {
1196
1371
  );
1197
1372
  return rows.map(this.rowToEvent);
1198
1373
  }
1374
+ /**
1375
+ * Get events since a SQLite rowid (for robust incremental replication).
1376
+ * Rowid is monotonic for append-only tables, independent of client timestamps.
1377
+ */
1378
+ async getEventsSinceRowid(lastRowid, limit = 1e3) {
1379
+ await this.initialize();
1380
+ const rows = sqliteAll(
1381
+ this.db,
1382
+ `SELECT rowid as _rowid, * FROM events WHERE rowid > ? ORDER BY rowid ASC LIMIT ?`,
1383
+ [lastRowid, limit]
1384
+ );
1385
+ return rows.map((row) => ({
1386
+ rowid: row._rowid,
1387
+ event: this.rowToEvent(row)
1388
+ }));
1389
+ }
1390
+ /**
1391
+ * Import events with fixed IDs (used for cross-machine replication).
1392
+ * Idempotent: skips if event id or dedupeKey already exists.
1393
+ *
1394
+ * NOTE: This bypasses the append() id generation to preserve stable IDs.
1395
+ */
1396
+ async importEvents(events) {
1397
+ if (events.length === 0)
1398
+ return { inserted: 0, skipped: 0 };
1399
+ if (this.readOnly)
1400
+ return { inserted: 0, skipped: events.length };
1401
+ await this.initialize();
1402
+ const getById = this.db.prepare(`SELECT id FROM events WHERE id = ?`);
1403
+ const getByDedupe = this.db.prepare(`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`);
1404
+ const insertEvent = this.db.prepare(`
1405
+ INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
1406
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1407
+ `);
1408
+ const insertDedup = this.db.prepare(`
1409
+ INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
1410
+ `);
1411
+ const insertLevel = this.db.prepare(`
1412
+ INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')
1413
+ `);
1414
+ let inserted = 0;
1415
+ let skipped = 0;
1416
+ const insertedEvents = [];
1417
+ const tx = this.db.transaction((batch) => {
1418
+ for (const ev of batch) {
1419
+ const existingById = getById.get(ev.id);
1420
+ if (existingById) {
1421
+ skipped++;
1422
+ continue;
1423
+ }
1424
+ const canonicalKey = ev.canonicalKey || makeCanonicalKey(ev.content);
1425
+ const dedupeKey = ev.dedupeKey || makeDedupeKey(ev.content, ev.sessionId);
1426
+ const existingByDedupe = getByDedupe.get(dedupeKey);
1427
+ if (existingByDedupe) {
1428
+ skipped++;
1429
+ continue;
1430
+ }
1431
+ const metadata = ev.metadata || {};
1432
+ const turnId = metadata.turnId;
1433
+ insertEvent.run(
1434
+ ev.id,
1435
+ ev.eventType,
1436
+ ev.sessionId,
1437
+ toSQLiteTimestamp(ev.timestamp),
1438
+ ev.content,
1439
+ canonicalKey,
1440
+ dedupeKey,
1441
+ JSON.stringify(metadata),
1442
+ turnId ?? null
1443
+ );
1444
+ insertDedup.run(dedupeKey, ev.id);
1445
+ insertLevel.run(ev.id);
1446
+ inserted++;
1447
+ insertedEvents.push(ev);
1448
+ }
1449
+ });
1450
+ tx(events);
1451
+ if (this.markdownMirror && insertedEvents.length > 0) {
1452
+ for (const ev of insertedEvents) {
1453
+ this.markdownMirror.append(ev).catch((err) => {
1454
+ console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
1455
+ });
1456
+ }
1457
+ }
1458
+ return { inserted, skipped };
1459
+ }
1199
1460
  /**
1200
1461
  * Create or update session
1201
1462
  */
@@ -1358,6 +1619,35 @@ var SQLiteEventStore = class {
1358
1619
  [error, ...ids]
1359
1620
  );
1360
1621
  }
1622
+ /**
1623
+ * Get embedding/vector outbox health statistics
1624
+ */
1625
+ async getOutboxStats() {
1626
+ await this.initialize();
1627
+ const embeddingRows = sqliteAll(
1628
+ this.db,
1629
+ `SELECT status, COUNT(*) as count FROM embedding_outbox GROUP BY status`
1630
+ );
1631
+ const vectorRows = sqliteAll(
1632
+ this.db,
1633
+ `SELECT status, COUNT(*) as count FROM vector_outbox GROUP BY status`
1634
+ );
1635
+ const fromRows = (rows) => {
1636
+ const out = { pending: 0, processing: 0, failed: 0, total: 0 };
1637
+ for (const row of rows) {
1638
+ const key = row.status;
1639
+ if (key === "pending" || key === "processing" || key === "failed") {
1640
+ out[key] += row.count;
1641
+ }
1642
+ out.total += row.count;
1643
+ }
1644
+ return out;
1645
+ };
1646
+ return {
1647
+ embedding: fromRows(embeddingRows),
1648
+ vector: fromRows(vectorRows)
1649
+ };
1650
+ }
1361
1651
  /**
1362
1652
  * Update memory level
1363
1653
  */
@@ -1482,11 +1772,11 @@ var SQLiteEventStore = class {
1482
1772
  );
1483
1773
  }
1484
1774
  /**
1485
- * Get most accessed memories
1775
+ * Get most accessed memories (falls back to recent events if none accessed)
1486
1776
  */
1487
1777
  async getMostAccessed(limit = 10) {
1488
1778
  await this.initialize();
1489
- const rows = sqliteAll(
1779
+ let rows = sqliteAll(
1490
1780
  this.db,
1491
1781
  `SELECT * FROM events
1492
1782
  WHERE access_count > 0
@@ -1494,8 +1784,166 @@ var SQLiteEventStore = class {
1494
1784
  LIMIT ?`,
1495
1785
  [limit]
1496
1786
  );
1787
+ if (rows.length === 0) {
1788
+ rows = sqliteAll(
1789
+ this.db,
1790
+ `SELECT * FROM events
1791
+ ORDER BY timestamp DESC
1792
+ LIMIT ?`,
1793
+ [limit]
1794
+ );
1795
+ }
1497
1796
  return rows.map((row) => this.rowToEvent(row));
1498
1797
  }
1798
+ /**
1799
+ * Record a memory retrieval for helpfulness tracking
1800
+ */
1801
+ async recordRetrieval(eventId, sessionId, score, query) {
1802
+ if (this.readOnly)
1803
+ return;
1804
+ await this.initialize();
1805
+ const id = randomUUID2();
1806
+ sqliteRun(
1807
+ this.db,
1808
+ `INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
1809
+ VALUES (?, ?, ?, ?, ?, datetime('now'))`,
1810
+ [id, eventId, sessionId, score, query.slice(0, 100)]
1811
+ );
1812
+ }
1813
+ /**
1814
+ * Evaluate helpfulness for all retrievals in a session
1815
+ * Called at session end - uses behavioral signals to compute score
1816
+ */
1817
+ async evaluateSessionHelpfulness(sessionId) {
1818
+ if (this.readOnly)
1819
+ return;
1820
+ await this.initialize();
1821
+ const retrievals = sqliteAll(
1822
+ this.db,
1823
+ `SELECT * FROM memory_helpfulness WHERE session_id = ? AND measured_at IS NULL`,
1824
+ [sessionId]
1825
+ );
1826
+ if (retrievals.length === 0)
1827
+ return;
1828
+ const sessionEvents = sqliteAll(
1829
+ this.db,
1830
+ `SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
1831
+ [sessionId]
1832
+ );
1833
+ const promptEvents = sessionEvents.filter((e) => e.event_type === "user_prompt");
1834
+ const toolEvents = sessionEvents.filter((e) => e.event_type === "tool_observation");
1835
+ let toolSuccessCount = 0;
1836
+ let toolTotalCount = toolEvents.length;
1837
+ for (const t of toolEvents) {
1838
+ try {
1839
+ const content = JSON.parse(t.content);
1840
+ if (content.success !== false)
1841
+ toolSuccessCount++;
1842
+ } catch {
1843
+ toolSuccessCount++;
1844
+ }
1845
+ }
1846
+ const toolSuccessRatio = toolTotalCount > 0 ? toolSuccessCount / toolTotalCount : 0.5;
1847
+ for (const retrieval of retrievals) {
1848
+ const retrievalTime = retrieval.created_at;
1849
+ const eventsAfter = sessionEvents.filter((e) => e.timestamp > retrievalTime);
1850
+ const sessionContinued = eventsAfter.length > 0 ? 1 : 0;
1851
+ const promptsAfter = promptEvents.filter((e) => e.timestamp > retrievalTime);
1852
+ const promptCountAfter = promptsAfter.length;
1853
+ const queryWords = new Set((retrieval.query_preview || "").toLowerCase().split(/\s+/).filter((w) => w.length > 2));
1854
+ let wasReasked = 0;
1855
+ for (const p of promptsAfter) {
1856
+ const pWords = new Set(p.content.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
1857
+ let overlap = 0;
1858
+ for (const w of queryWords) {
1859
+ if (pWords.has(w))
1860
+ overlap++;
1861
+ }
1862
+ if (queryWords.size > 0 && overlap / queryWords.size > 0.5) {
1863
+ wasReasked = 1;
1864
+ break;
1865
+ }
1866
+ }
1867
+ const retrievalScore = retrieval.retrieval_score || 0;
1868
+ const helpfulnessScore = 0.3 * Math.min(retrievalScore, 1) + 0.25 * (sessionContinued ? 1 : 0) + 0.25 * toolSuccessRatio + 0.2 * (wasReasked ? 0 : 1);
1869
+ sqliteRun(
1870
+ this.db,
1871
+ `UPDATE memory_helpfulness
1872
+ SET session_continued = ?, prompt_count_after = ?,
1873
+ tool_success_count = ?, tool_total_count = ?,
1874
+ was_reasked = ?, helpfulness_score = ?,
1875
+ measured_at = datetime('now')
1876
+ WHERE id = ?`,
1877
+ [
1878
+ sessionContinued,
1879
+ promptCountAfter,
1880
+ toolSuccessCount,
1881
+ toolTotalCount,
1882
+ wasReasked,
1883
+ helpfulnessScore,
1884
+ retrieval.id
1885
+ ]
1886
+ );
1887
+ }
1888
+ }
1889
+ /**
1890
+ * Get most helpful memories ranked by helpfulness score
1891
+ */
1892
+ async getHelpfulMemories(limit = 10) {
1893
+ await this.initialize();
1894
+ const rows = sqliteAll(
1895
+ this.db,
1896
+ `SELECT
1897
+ mh.event_id,
1898
+ AVG(mh.helpfulness_score) as avg_score,
1899
+ COUNT(*) as eval_count,
1900
+ e.content,
1901
+ e.access_count
1902
+ FROM memory_helpfulness mh
1903
+ JOIN events e ON e.id = mh.event_id
1904
+ WHERE mh.measured_at IS NOT NULL
1905
+ GROUP BY mh.event_id
1906
+ ORDER BY avg_score DESC
1907
+ LIMIT ?`,
1908
+ [limit]
1909
+ );
1910
+ return rows.map((r) => ({
1911
+ eventId: r.event_id,
1912
+ summary: r.content.substring(0, 200) + (r.content.length > 200 ? "..." : ""),
1913
+ helpfulnessScore: Math.round(r.avg_score * 100) / 100,
1914
+ accessCount: r.access_count || 0,
1915
+ evaluationCount: r.eval_count
1916
+ }));
1917
+ }
1918
+ /**
1919
+ * Get helpfulness statistics for dashboard
1920
+ */
1921
+ async getHelpfulnessStats() {
1922
+ await this.initialize();
1923
+ const stats = sqliteGet(
1924
+ this.db,
1925
+ `SELECT
1926
+ AVG(helpfulness_score) as avg_score,
1927
+ COUNT(*) as total_evaluated,
1928
+ SUM(CASE WHEN helpfulness_score >= 0.7 THEN 1 ELSE 0 END) as helpful,
1929
+ SUM(CASE WHEN helpfulness_score >= 0.4 AND helpfulness_score < 0.7 THEN 1 ELSE 0 END) as neutral,
1930
+ SUM(CASE WHEN helpfulness_score < 0.4 THEN 1 ELSE 0 END) as unhelpful
1931
+ FROM memory_helpfulness
1932
+ WHERE measured_at IS NOT NULL`
1933
+ );
1934
+ const totalRow = sqliteGet(
1935
+ this.db,
1936
+ `SELECT COUNT(*) as total FROM memory_helpfulness`
1937
+ );
1938
+ return {
1939
+ avgScore: Math.round((stats?.avg_score || 0) * 100) / 100,
1940
+ totalEvaluated: stats?.total_evaluated || 0,
1941
+ totalRetrievals: totalRow?.total || 0,
1942
+ helpful: stats?.helpful || 0,
1943
+ neutral: stats?.neutral || 0,
1944
+ unhelpful: stats?.unhelpful || 0
1945
+ };
1946
+ }
1499
1947
  /**
1500
1948
  * Fast keyword search using FTS5
1501
1949
  * Returns events matching the search query, ranked by relevance
@@ -1558,12 +2006,222 @@ var SQLiteEventStore = class {
1558
2006
  getDatabase() {
1559
2007
  return this.db;
1560
2008
  }
2009
+ async recordRetrievalTrace(input) {
2010
+ await this.initialize();
2011
+ const traceId = randomUUID2();
2012
+ sqliteRun(
2013
+ this.db,
2014
+ `INSERT INTO retrieval_traces (
2015
+ trace_id, session_id, project_hash, query_text, strategy,
2016
+ candidate_event_ids, selected_event_ids, candidate_details_json, selected_details_json,
2017
+ candidate_count, selected_count, confidence, fallback_trace
2018
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2019
+ [
2020
+ traceId,
2021
+ input.sessionId || null,
2022
+ input.projectHash || null,
2023
+ input.queryText,
2024
+ input.strategy || null,
2025
+ JSON.stringify(input.candidateEventIds || []),
2026
+ JSON.stringify(input.selectedEventIds || []),
2027
+ JSON.stringify(input.candidateDetails || []),
2028
+ JSON.stringify(input.selectedDetails || []),
2029
+ (input.candidateEventIds || []).length,
2030
+ (input.selectedEventIds || []).length,
2031
+ input.confidence || null,
2032
+ JSON.stringify(input.fallbackTrace || [])
2033
+ ]
2034
+ );
2035
+ }
2036
+ async getRecentRetrievalTraces(limit = 50) {
2037
+ await this.initialize();
2038
+ const rows = sqliteAll(
2039
+ this.db,
2040
+ `SELECT * FROM retrieval_traces ORDER BY created_at DESC LIMIT ?`,
2041
+ [limit]
2042
+ );
2043
+ return rows.map((row) => ({
2044
+ traceId: row.trace_id,
2045
+ sessionId: row.session_id || void 0,
2046
+ projectHash: row.project_hash || void 0,
2047
+ queryText: row.query_text,
2048
+ strategy: row.strategy || void 0,
2049
+ candidateEventIds: row.candidate_event_ids ? JSON.parse(row.candidate_event_ids) : [],
2050
+ selectedEventIds: row.selected_event_ids ? JSON.parse(row.selected_event_ids) : [],
2051
+ candidateDetails: row.candidate_details_json ? JSON.parse(row.candidate_details_json) : [],
2052
+ selectedDetails: row.selected_details_json ? JSON.parse(row.selected_details_json) : [],
2053
+ candidateCount: Number(row.candidate_count || 0),
2054
+ selectedCount: Number(row.selected_count || 0),
2055
+ confidence: row.confidence || void 0,
2056
+ fallbackTrace: row.fallback_trace ? JSON.parse(row.fallback_trace) : [],
2057
+ createdAt: toDateFromSQLite(row.created_at)
2058
+ }));
2059
+ }
2060
+ async getRetrievalTraceStats() {
2061
+ await this.initialize();
2062
+ const row = sqliteGet(
2063
+ this.db,
2064
+ `SELECT
2065
+ COUNT(*) as total_queries,
2066
+ AVG(candidate_count) as avg_candidate_count,
2067
+ AVG(selected_count) as avg_selected_count,
2068
+ CASE
2069
+ WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
2070
+ ELSE 0
2071
+ END as selection_rate
2072
+ FROM retrieval_traces`,
2073
+ []
2074
+ );
2075
+ return {
2076
+ totalQueries: Number(row?.total_queries || 0),
2077
+ avgCandidateCount: Number(row?.avg_candidate_count || 0),
2078
+ avgSelectedCount: Number(row?.avg_selected_count || 0),
2079
+ selectionRate: Number(row?.selection_rate || 0)
2080
+ };
2081
+ }
1561
2082
  /**
1562
2083
  * Close database connection
1563
2084
  */
1564
2085
  async close() {
1565
2086
  sqliteClose(this.db);
1566
2087
  }
2088
+ /**
2089
+ * Get events grouped by turn_id for a session
2090
+ * Returns turns ordered by first event timestamp (newest first)
2091
+ */
2092
+ async getSessionTurns(sessionId, options) {
2093
+ await this.initialize();
2094
+ const limit = options?.limit || 20;
2095
+ const offset = options?.offset || 0;
2096
+ const turnRows = sqliteAll(
2097
+ this.db,
2098
+ `SELECT turn_id, MIN(timestamp) as min_ts
2099
+ FROM events
2100
+ WHERE session_id = ? AND turn_id IS NOT NULL
2101
+ GROUP BY turn_id
2102
+ ORDER BY min_ts DESC
2103
+ LIMIT ? OFFSET ?`,
2104
+ [sessionId, limit, offset]
2105
+ );
2106
+ const turns = [];
2107
+ for (const turnRow of turnRows) {
2108
+ const events = await this.getEventsByTurn(turnRow.turn_id);
2109
+ const promptEvent = events.find((e) => e.eventType === "user_prompt");
2110
+ const toolEvents = events.filter((e) => e.eventType === "tool_observation");
2111
+ const hasResponse = events.some((e) => e.eventType === "agent_response");
2112
+ turns.push({
2113
+ turnId: turnRow.turn_id,
2114
+ events,
2115
+ startedAt: toDateFromSQLite(turnRow.min_ts),
2116
+ promptPreview: promptEvent ? promptEvent.content.slice(0, 200) + (promptEvent.content.length > 200 ? "..." : "") : "(no prompt)",
2117
+ eventCount: events.length,
2118
+ toolCount: toolEvents.length,
2119
+ hasResponse
2120
+ });
2121
+ }
2122
+ return turns;
2123
+ }
2124
+ /**
2125
+ * Get all events for a specific turn_id
2126
+ */
2127
+ async getEventsByTurn(turnId) {
2128
+ await this.initialize();
2129
+ const rows = sqliteAll(
2130
+ this.db,
2131
+ `SELECT * FROM events WHERE turn_id = ? ORDER BY timestamp ASC`,
2132
+ [turnId]
2133
+ );
2134
+ return rows.map(this.rowToEvent);
2135
+ }
2136
+ /**
2137
+ * Count total turns for a session
2138
+ */
2139
+ async countSessionTurns(sessionId) {
2140
+ await this.initialize();
2141
+ const row = sqliteGet(
2142
+ this.db,
2143
+ `SELECT COUNT(DISTINCT turn_id) as count
2144
+ FROM events
2145
+ WHERE session_id = ? AND turn_id IS NOT NULL`,
2146
+ [sessionId]
2147
+ );
2148
+ return row?.count || 0;
2149
+ }
2150
+ /**
2151
+ * Migrate existing events: backfill turn_id for events that have turnId in metadata
2152
+ * but no turn_id column value (for events stored before this migration)
2153
+ */
2154
+ async backfillTurnIds() {
2155
+ await this.initialize();
2156
+ const rows = sqliteAll(
2157
+ this.db,
2158
+ `SELECT id, metadata FROM events
2159
+ WHERE turn_id IS NULL AND metadata IS NOT NULL AND metadata LIKE '%turnId%'`
2160
+ );
2161
+ let updated = 0;
2162
+ for (const row of rows) {
2163
+ try {
2164
+ const metadata = JSON.parse(row.metadata);
2165
+ if (metadata.turnId) {
2166
+ sqliteRun(
2167
+ this.db,
2168
+ `UPDATE events SET turn_id = ? WHERE id = ?`,
2169
+ [metadata.turnId, row.id]
2170
+ );
2171
+ updated++;
2172
+ }
2173
+ } catch {
2174
+ }
2175
+ }
2176
+ return updated;
2177
+ }
2178
+ /**
2179
+ * Delete all events for a session (for force reimport)
2180
+ */
2181
+ async deleteSessionEvents(sessionId) {
2182
+ await this.initialize();
2183
+ const events = sqliteAll(
2184
+ this.db,
2185
+ `SELECT id FROM events WHERE session_id = ?`,
2186
+ [sessionId]
2187
+ );
2188
+ if (events.length === 0)
2189
+ return 0;
2190
+ const eventIds = events.map((e) => e.id);
2191
+ const placeholders = eventIds.map(() => "?").join(",");
2192
+ const ftsTriggersDropped = [];
2193
+ for (const triggerName of ["events_fts_delete", "events_fts_update", "events_fts_insert"]) {
2194
+ try {
2195
+ sqliteRun(this.db, `DROP TRIGGER IF EXISTS ${triggerName}`);
2196
+ ftsTriggersDropped.push(triggerName);
2197
+ } catch {
2198
+ }
2199
+ }
2200
+ for (const table of ["event_dedup", "memory_levels", "embedding_queue", "embedding_outbox", "vector_outbox"]) {
2201
+ try {
2202
+ sqliteRun(this.db, `DELETE FROM ${table} WHERE event_id IN (${placeholders})`, eventIds);
2203
+ } catch {
2204
+ }
2205
+ }
2206
+ const result = sqliteRun(this.db, `DELETE FROM events WHERE session_id = ?`, [sessionId]);
2207
+ if (ftsTriggersDropped.length > 0) {
2208
+ try {
2209
+ sqliteRun(this.db, `INSERT INTO events_fts(events_fts) VALUES('rebuild')`);
2210
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_insert AFTER INSERT ON events BEGIN
2211
+ INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
2212
+ END`);
2213
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_delete AFTER DELETE ON events BEGIN
2214
+ INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
2215
+ END`);
2216
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_update AFTER UPDATE ON events BEGIN
2217
+ INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
2218
+ INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
2219
+ END`);
2220
+ } catch {
2221
+ }
2222
+ }
2223
+ return result.changes || 0;
2224
+ }
1567
2225
  /**
1568
2226
  * Convert database row to MemoryEvent
1569
2227
  */
@@ -1584,6 +2242,9 @@ var SQLiteEventStore = class {
1584
2242
  if (row.last_accessed_at !== void 0) {
1585
2243
  event.last_accessed_at = row.last_accessed_at;
1586
2244
  }
2245
+ if (row.turn_id !== void 0 && row.turn_id !== null) {
2246
+ event.turn_id = row.turn_id;
2247
+ }
1587
2248
  return event;
1588
2249
  }
1589
2250
  };
@@ -1795,7 +2456,16 @@ var VectorStore = class {
1795
2456
  metadata: JSON.stringify(record.metadata || {})
1796
2457
  };
1797
2458
  if (!this.table) {
1798
- this.table = await this.db.createTable(this.tableName, [data]);
2459
+ try {
2460
+ this.table = await this.db.createTable(this.tableName, [data]);
2461
+ } catch (e) {
2462
+ if (e?.message?.includes("already exists")) {
2463
+ this.table = await this.db.openTable(this.tableName);
2464
+ await this.table.add([data]);
2465
+ } else {
2466
+ throw e;
2467
+ }
2468
+ }
1799
2469
  } else {
1800
2470
  await this.table.add([data]);
1801
2471
  }
@@ -1821,7 +2491,16 @@ var VectorStore = class {
1821
2491
  metadata: JSON.stringify(record.metadata || {})
1822
2492
  }));
1823
2493
  if (!this.table) {
1824
- this.table = await this.db.createTable(this.tableName, data);
2494
+ try {
2495
+ this.table = await this.db.createTable(this.tableName, data);
2496
+ } catch (e) {
2497
+ if (e?.message?.includes("already exists")) {
2498
+ this.table = await this.db.openTable(this.tableName);
2499
+ await this.table.add(data);
2500
+ } else {
2501
+ throw e;
2502
+ }
2503
+ }
1825
2504
  } else {
1826
2505
  await this.table.add(data);
1827
2506
  }
@@ -2261,7 +2940,20 @@ var DEFAULT_OPTIONS = {
2261
2940
  topK: 5,
2262
2941
  minScore: 0.7,
2263
2942
  maxTokens: 2e3,
2264
- includeSessionContext: true
2943
+ includeSessionContext: true,
2944
+ strategy: "auto",
2945
+ rerankWithKeyword: true,
2946
+ decayPolicy: {
2947
+ enabled: true,
2948
+ windowDays: 30,
2949
+ maxPenalty: 0.15
2950
+ },
2951
+ graphHop: {
2952
+ enabled: true,
2953
+ maxHops: 1,
2954
+ hopPenalty: 0.08
2955
+ },
2956
+ projectScopeMode: "global"
2265
2957
  };
2266
2958
  var Retriever = class {
2267
2959
  eventStore;
@@ -2271,6 +2963,7 @@ var Retriever = class {
2271
2963
  sharedStore;
2272
2964
  sharedVectorStore;
2273
2965
  graduation;
2966
+ queryRewriter;
2274
2967
  constructor(eventStore, vectorStore, embedder, matcher, sharedOptions) {
2275
2968
  this.eventStore = eventStore;
2276
2969
  this.vectorStore = vectorStore;
@@ -2279,47 +2972,105 @@ var Retriever = class {
2279
2972
  this.sharedStore = sharedOptions?.sharedStore;
2280
2973
  this.sharedVectorStore = sharedOptions?.sharedVectorStore;
2281
2974
  }
2282
- /**
2283
- * Set graduation pipeline for access tracking
2284
- */
2285
2975
  setGraduationPipeline(graduation) {
2286
2976
  this.graduation = graduation;
2287
2977
  }
2288
- /**
2289
- * Set shared stores after construction
2290
- */
2291
2978
  setSharedStores(sharedStore, sharedVectorStore) {
2292
2979
  this.sharedStore = sharedStore;
2293
2980
  this.sharedVectorStore = sharedVectorStore;
2294
2981
  }
2295
- /**
2296
- * Retrieve relevant memories for a query
2297
- */
2982
+ setQueryRewriter(rewriter) {
2983
+ this.queryRewriter = rewriter;
2984
+ }
2298
2985
  async retrieve(query, options = {}) {
2299
2986
  const opts = { ...DEFAULT_OPTIONS, ...options };
2300
- const queryEmbedding = await this.embedder.embed(query);
2301
- const searchResults = await this.vectorStore.search(queryEmbedding.vector, {
2302
- limit: opts.topK * 2,
2303
- // Get extra for filtering
2987
+ const sessionFilter = opts.scope?.sessionId ?? opts.sessionId;
2988
+ const fallbackTrace = [];
2989
+ const fallbackEnabled = (opts.strategy ?? "auto") === "auto";
2990
+ const primaryStrategy = opts.strategy === "auto" ? "fast" : opts.strategy || "fast";
2991
+ let current = await this.runStage(query, {
2992
+ strategy: primaryStrategy,
2993
+ topK: opts.topK,
2304
2994
  minScore: opts.minScore,
2305
- sessionId: opts.sessionId
2995
+ sessionId: sessionFilter,
2996
+ scope: opts.scope,
2997
+ rerankWithKeyword: opts.rerankWithKeyword !== false,
2998
+ rerankWeights: opts.rerankWeights,
2999
+ decayPolicy: opts.decayPolicy,
3000
+ intentRewrite: opts.intentRewrite === true,
3001
+ graphHop: opts.graphHop,
3002
+ projectScopeMode: opts.projectScopeMode,
3003
+ projectHash: opts.projectHash,
3004
+ allowedProjectHashes: opts.allowedProjectHashes
2306
3005
  });
2307
- const matchResult = this.matcher.matchSearchResults(
2308
- searchResults,
2309
- (eventId) => this.getEventAgeDays(eventId)
2310
- );
2311
- const memories = await this.enrichResults(searchResults.slice(0, opts.topK), opts);
3006
+ fallbackTrace.push(`stage:primary:${primaryStrategy}`);
3007
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results) && primaryStrategy !== "deep") {
3008
+ current = await this.runStage(query, {
3009
+ strategy: "deep",
3010
+ topK: opts.topK,
3011
+ minScore: opts.minScore,
3012
+ sessionId: sessionFilter,
3013
+ scope: opts.scope,
3014
+ rerankWithKeyword: opts.rerankWithKeyword !== false,
3015
+ rerankWeights: opts.rerankWeights,
3016
+ decayPolicy: opts.decayPolicy,
3017
+ graphHop: opts.graphHop,
3018
+ projectScopeMode: opts.projectScopeMode,
3019
+ projectHash: opts.projectHash,
3020
+ allowedProjectHashes: opts.allowedProjectHashes
3021
+ });
3022
+ fallbackTrace.push("fallback:deep");
3023
+ }
3024
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
3025
+ current = await this.runStage(query, {
3026
+ strategy: "deep",
3027
+ topK: opts.topK,
3028
+ minScore: Math.max(0.5, opts.minScore - 0.15),
3029
+ sessionId: void 0,
3030
+ scope: void 0,
3031
+ rerankWithKeyword: true,
3032
+ rerankWeights: opts.rerankWeights,
3033
+ decayPolicy: opts.decayPolicy,
3034
+ graphHop: opts.graphHop,
3035
+ projectScopeMode: opts.projectScopeMode,
3036
+ projectHash: opts.projectHash,
3037
+ allowedProjectHashes: opts.allowedProjectHashes
3038
+ });
3039
+ fallbackTrace.push("fallback:scope-expanded");
3040
+ }
3041
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
3042
+ const summary = await this.buildSummaryFallback(query, opts.topK);
3043
+ current = {
3044
+ results: summary,
3045
+ candidateResults: summary,
3046
+ matchResult: this.matcher.matchSearchResults(summary, () => 0)
3047
+ };
3048
+ fallbackTrace.push("fallback:summary");
3049
+ }
3050
+ const memories = await this.enrichResults(current.results.slice(0, opts.topK), opts);
2312
3051
  const context = this.buildContext(memories, opts.maxTokens);
2313
3052
  return {
2314
3053
  memories,
2315
- matchResult,
3054
+ matchResult: current.matchResult,
2316
3055
  totalTokens: this.estimateTokens(context),
2317
- context
3056
+ context,
3057
+ fallbackTrace,
3058
+ selectedDebug: current.results.slice(0, opts.topK).map((r) => ({
3059
+ eventId: r.eventId,
3060
+ score: r.score,
3061
+ semanticScore: r.semanticScore,
3062
+ lexicalScore: r.lexicalScore,
3063
+ recencyScore: r.recencyScore
3064
+ })),
3065
+ candidateDebug: (current.candidateResults || []).slice(0, Math.max(opts.topK * 3, 20)).map((r) => ({
3066
+ eventId: r.eventId,
3067
+ score: r.score,
3068
+ semanticScore: r.semanticScore,
3069
+ lexicalScore: r.lexicalScore,
3070
+ recencyScore: r.recencyScore
3071
+ }))
2318
3072
  };
2319
3073
  }
2320
- /**
2321
- * Retrieve with unified search (project + shared)
2322
- */
2323
3074
  async retrieveUnified(query, options = {}) {
2324
3075
  const projectResult = await this.retrieve(query, options);
2325
3076
  if (!options.includeShared || !this.sharedStore || !this.sharedVectorStore) {
@@ -2327,22 +3078,19 @@ var Retriever = class {
2327
3078
  }
2328
3079
  try {
2329
3080
  const queryEmbedding = await this.embedder.embed(query);
2330
- const sharedVectorResults = await this.sharedVectorStore.search(
2331
- queryEmbedding.vector,
2332
- {
2333
- limit: options.topK || 5,
2334
- minScore: options.minScore || 0.7,
2335
- excludeProjectHash: options.projectHash
2336
- }
2337
- );
3081
+ const sharedVectorResults = await this.sharedVectorStore.search(queryEmbedding.vector, {
3082
+ limit: options.topK || 5,
3083
+ minScore: options.minScore || 0.7,
3084
+ excludeProjectHash: options.projectHash
3085
+ });
2338
3086
  const sharedMemories = [];
2339
3087
  for (const result of sharedVectorResults) {
2340
3088
  const entry = await this.sharedStore.get(result.entryId);
2341
- if (entry) {
2342
- if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
2343
- sharedMemories.push(entry);
2344
- await this.sharedStore.recordUsage(entry.entryId);
2345
- }
3089
+ if (!entry)
3090
+ continue;
3091
+ if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
3092
+ sharedMemories.push(entry);
3093
+ await this.sharedStore.recordUsage(entry.entryId);
2346
3094
  }
2347
3095
  }
2348
3096
  const unifiedContext = this.buildUnifiedContext(projectResult, sharedMemories);
@@ -2357,50 +3105,243 @@ var Retriever = class {
2357
3105
  return projectResult;
2358
3106
  }
2359
3107
  }
2360
- /**
2361
- * Build unified context combining project and shared memories
2362
- */
2363
- buildUnifiedContext(projectResult, sharedMemories) {
2364
- let context = projectResult.context;
2365
- if (sharedMemories.length > 0) {
2366
- context += "\n\n## Cross-Project Knowledge\n\n";
2367
- for (const memory of sharedMemories.slice(0, 3)) {
2368
- context += `### ${memory.title}
2369
- `;
2370
- if (memory.symptoms.length > 0) {
2371
- context += `**Symptoms:** ${memory.symptoms.join(", ")}
2372
- `;
2373
- }
2374
- context += `**Root Cause:** ${memory.rootCause}
2375
- `;
2376
- context += `**Solution:** ${memory.solution}
2377
- `;
2378
- if (memory.technologies && memory.technologies.length > 0) {
2379
- context += `**Technologies:** ${memory.technologies.join(", ")}
2380
- `;
3108
+ async runStage(query, input) {
3109
+ let initialResults = await this.searchByStrategy(query, {
3110
+ strategy: input.strategy,
3111
+ topK: input.topK,
3112
+ minScore: input.minScore,
3113
+ sessionId: input.sessionId
3114
+ });
3115
+ if (input.intentRewrite && input.strategy === "deep" && this.queryRewriter) {
3116
+ const rewritten = (await this.queryRewriter(query))?.trim();
3117
+ if (rewritten && rewritten !== query) {
3118
+ const rewrittenResults = await this.searchByStrategy(rewritten, {
3119
+ strategy: "deep",
3120
+ topK: input.topK,
3121
+ minScore: Math.max(0.5, input.minScore - 0.1),
3122
+ sessionId: input.sessionId
3123
+ });
3124
+ initialResults = this.mergeResults(initialResults, rewrittenResults, input.topK * 3);
3125
+ }
3126
+ }
3127
+ const expandedResults = input.graphHop?.enabled === false ? initialResults : await this.expandGraphHops(initialResults, {
3128
+ maxHops: Math.max(1, input.graphHop?.maxHops ?? 1),
3129
+ hopPenalty: Math.max(0, input.graphHop?.hopPenalty ?? 0.08),
3130
+ limit: input.topK * 4
3131
+ });
3132
+ const rerankedResults = input.rerankWithKeyword ? this.rerankByKeywordOverlap(expandedResults, query, input.rerankWeights, input.decayPolicy) : expandedResults;
3133
+ const filtered = await this.applyScopeFilters(rerankedResults, {
3134
+ scope: input.scope,
3135
+ projectScopeMode: input.projectScopeMode,
3136
+ projectHash: input.projectHash,
3137
+ allowedProjectHashes: input.allowedProjectHashes
3138
+ });
3139
+ const top = filtered.slice(0, input.topK);
3140
+ const matchResult = this.matcher.matchSearchResults(top, () => 0);
3141
+ return { results: top, candidateResults: filtered, matchResult };
3142
+ }
3143
+ mergeResults(primary, secondary, limit) {
3144
+ const byId = /* @__PURE__ */ new Map();
3145
+ for (const row of primary)
3146
+ byId.set(row.eventId, row);
3147
+ for (const row of secondary) {
3148
+ const prev = byId.get(row.eventId);
3149
+ if (!prev || row.score > prev.score) {
3150
+ byId.set(row.eventId, row);
3151
+ }
3152
+ }
3153
+ return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, limit);
3154
+ }
3155
+ async expandGraphHops(seeds, opts) {
3156
+ const byId = /* @__PURE__ */ new Map();
3157
+ for (const s of seeds)
3158
+ byId.set(s.eventId, s);
3159
+ let frontier = seeds.map((s) => ({ row: s, hop: 0 }));
3160
+ for (let hop = 1; hop <= opts.maxHops; hop += 1) {
3161
+ const next = [];
3162
+ for (const f of frontier) {
3163
+ const ev = await this.eventStore.getEvent(f.row.eventId);
3164
+ if (!ev)
3165
+ continue;
3166
+ const rel = ev.metadata?.relatedEventIds ?? [];
3167
+ const relatedIds = Array.isArray(rel) ? rel.filter((x) => typeof x === "string") : [];
3168
+ for (const rid of relatedIds) {
3169
+ if (byId.has(rid))
3170
+ continue;
3171
+ const target = await this.eventStore.getEvent(rid);
3172
+ if (!target)
3173
+ continue;
3174
+ const score = Math.max(0, f.row.score - opts.hopPenalty * hop);
3175
+ const row = {
3176
+ id: `hop-${hop}-${rid}`,
3177
+ eventId: target.id,
3178
+ content: target.content,
3179
+ score,
3180
+ sessionId: target.sessionId,
3181
+ eventType: target.eventType,
3182
+ timestamp: target.timestamp.toISOString()
3183
+ };
3184
+ byId.set(row.eventId, row);
3185
+ next.push({ row, hop });
3186
+ if (byId.size >= opts.limit)
3187
+ break;
2381
3188
  }
2382
- context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
2383
-
2384
- `;
3189
+ if (byId.size >= opts.limit)
3190
+ break;
2385
3191
  }
3192
+ frontier = next;
3193
+ if (frontier.length === 0 || byId.size >= opts.limit)
3194
+ break;
2386
3195
  }
2387
- return context;
3196
+ return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, opts.limit);
3197
+ }
3198
+ shouldFallback(matchResult, results) {
3199
+ if (results.length === 0)
3200
+ return true;
3201
+ if (matchResult.confidence === "none")
3202
+ return true;
3203
+ return false;
3204
+ }
3205
+ async buildSummaryFallback(query, topK) {
3206
+ const recent = await this.eventStore.getRecentEvents(Math.max(topK * 6, 20));
3207
+ const q = this.tokenize(query);
3208
+ 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) => ({
3209
+ id: `summary-${row.e.id}`,
3210
+ eventId: row.e.id,
3211
+ content: row.e.content,
3212
+ score: Math.max(0.25, 0.6 - idx * 0.05),
3213
+ sessionId: row.e.sessionId,
3214
+ eventType: row.e.eventType,
3215
+ timestamp: row.e.timestamp.toISOString()
3216
+ }));
3217
+ return ranked;
3218
+ }
3219
+ async searchByStrategy(query, input) {
3220
+ const strategy = input.strategy === "auto" ? "deep" : input.strategy;
3221
+ if (strategy === "fast") {
3222
+ const keyword = await this.searchByKeyword(query, {
3223
+ limit: Math.max(5, input.topK * 3),
3224
+ sessionId: input.sessionId
3225
+ });
3226
+ return keyword;
3227
+ }
3228
+ const queryEmbedding = await this.embedder.embed(query);
3229
+ return this.vectorStore.search(queryEmbedding.vector, {
3230
+ limit: Math.max(5, input.topK * 3),
3231
+ minScore: input.minScore,
3232
+ sessionId: input.sessionId
3233
+ });
3234
+ }
3235
+ async searchByKeyword(query, input) {
3236
+ if (this.eventStore.keywordSearch) {
3237
+ const rows = await this.eventStore.keywordSearch(query, input.limit);
3238
+ const filtered2 = input.sessionId ? rows.filter((r) => r.event.sessionId === input.sessionId) : rows;
3239
+ return filtered2.map((row, idx) => ({
3240
+ id: `kw-${row.event.id}`,
3241
+ eventId: row.event.id,
3242
+ content: row.event.content,
3243
+ score: Math.max(0.4, 1 - idx * 0.04),
3244
+ sessionId: row.event.sessionId,
3245
+ eventType: row.event.eventType,
3246
+ timestamp: row.event.timestamp.toISOString()
3247
+ }));
3248
+ }
3249
+ const recent = await this.eventStore.getRecentEvents(input.limit * 4);
3250
+ const tokens = this.tokenize(query);
3251
+ 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);
3252
+ return filtered.map((row, idx) => ({
3253
+ id: `kw-fallback-${row.e.id}`,
3254
+ eventId: row.e.id,
3255
+ content: row.e.content,
3256
+ score: Math.max(0.3, 0.9 - idx * 0.05),
3257
+ sessionId: row.e.sessionId,
3258
+ eventType: row.e.eventType,
3259
+ timestamp: row.e.timestamp.toISOString()
3260
+ }));
3261
+ }
3262
+ rerankByKeywordOverlap(results, query, weights, decayPolicy) {
3263
+ const q = this.tokenize(query);
3264
+ const now = Date.now();
3265
+ const sw = Math.max(0, weights?.semantic ?? 0.7);
3266
+ const lw = Math.max(0, weights?.lexical ?? 0.2);
3267
+ const rw = Math.max(0, weights?.recency ?? 0.1);
3268
+ const total = sw + lw + rw || 1;
3269
+ const decayEnabled = decayPolicy?.enabled !== false;
3270
+ const decayWindow = Math.max(1, decayPolicy?.windowDays ?? 30);
3271
+ const decayMaxPenalty = Math.max(0, decayPolicy?.maxPenalty ?? 0.15);
3272
+ return [...results].map((r) => {
3273
+ const overlap = this.keywordOverlap(q, this.tokenize(r.content));
3274
+ const recencyDays = Math.max(0, (now - new Date(r.timestamp).getTime()) / (1e3 * 60 * 60 * 24));
3275
+ const recency = Math.max(0, 1 - recencyDays / decayWindow);
3276
+ let blended = (r.score * sw + overlap * lw + recency * rw) / total;
3277
+ if (decayEnabled && recencyDays > decayWindow && overlap < 0.5) {
3278
+ const ageFactor = Math.min(1, (recencyDays - decayWindow) / decayWindow);
3279
+ blended -= decayMaxPenalty * ageFactor;
3280
+ }
3281
+ return { ...r, score: Math.max(0, blended), semanticScore: r.score, lexicalScore: overlap, recencyScore: recency };
3282
+ }).sort((a, b) => b.score - a.score);
3283
+ }
3284
+ async applyScopeFilters(results, options) {
3285
+ const scope = options?.scope;
3286
+ const projectScopeMode = options?.projectScopeMode ?? "global";
3287
+ const allowedProjectHashes = new Set(
3288
+ [options?.projectHash, ...options?.allowedProjectHashes || []].filter(
3289
+ (value) => typeof value === "string" && value.length > 0
3290
+ )
3291
+ );
3292
+ if (!scope && projectScopeMode === "global")
3293
+ return results;
3294
+ const normalizedIncludes = (scope?.contentIncludes || []).map((s) => s.toLowerCase());
3295
+ const filtered = [];
3296
+ for (const result of results) {
3297
+ if (scope?.sessionId && result.sessionId !== scope.sessionId)
3298
+ continue;
3299
+ if (scope?.sessionIdPrefix && !result.sessionId.startsWith(scope.sessionIdPrefix))
3300
+ continue;
3301
+ if (scope?.eventTypes && scope.eventTypes.length > 0 && !scope.eventTypes.includes(result.eventType))
3302
+ continue;
3303
+ const event = await this.eventStore.getEvent(result.eventId);
3304
+ if (!event)
3305
+ continue;
3306
+ if (scope?.canonicalKeyPrefix && !event.canonicalKey.startsWith(scope.canonicalKeyPrefix))
3307
+ continue;
3308
+ if (normalizedIncludes.length > 0) {
3309
+ const lc = event.content.toLowerCase();
3310
+ if (!normalizedIncludes.some((needle) => lc.includes(needle)))
3311
+ continue;
3312
+ }
3313
+ if (scope?.metadata && !this.matchesMetadataScope(event.metadata, scope.metadata))
3314
+ continue;
3315
+ const projectHash = this.extractProjectHash(event.metadata);
3316
+ filtered.push({ result, projectHash });
3317
+ }
3318
+ if (projectScopeMode === "global" || allowedProjectHashes.size === 0) {
3319
+ return filtered.map((x) => x.result);
3320
+ }
3321
+ const projectMatched = filtered.filter((x) => x.projectHash && allowedProjectHashes.has(x.projectHash));
3322
+ if (projectScopeMode === "strict") {
3323
+ return projectMatched.map((x) => x.result);
3324
+ }
3325
+ return (projectMatched.length > 0 ? projectMatched : filtered).map((x) => x.result);
3326
+ }
3327
+ extractProjectHash(metadata) {
3328
+ if (!metadata || typeof metadata !== "object")
3329
+ return void 0;
3330
+ const scope = metadata.scope;
3331
+ if (!scope || typeof scope !== "object")
3332
+ return void 0;
3333
+ const project = scope.project;
3334
+ if (!project || typeof project !== "object")
3335
+ return void 0;
3336
+ const hash = project.hash;
3337
+ return typeof hash === "string" && hash.length > 0 ? hash : void 0;
2388
3338
  }
2389
- /**
2390
- * Retrieve memories from a specific session
2391
- */
2392
3339
  async retrieveFromSession(sessionId) {
2393
3340
  return this.eventStore.getSessionEvents(sessionId);
2394
3341
  }
2395
- /**
2396
- * Get recent memories across all sessions
2397
- */
2398
3342
  async retrieveRecent(limit = 100) {
2399
3343
  return this.eventStore.getRecentEvents(limit);
2400
3344
  }
2401
- /**
2402
- * Enrich search results with full event data
2403
- */
2404
3345
  async enrichResults(results, options) {
2405
3346
  const memories = [];
2406
3347
  for (const result of results) {
@@ -2408,27 +3349,16 @@ var Retriever = class {
2408
3349
  if (!event)
2409
3350
  continue;
2410
3351
  if (this.graduation) {
2411
- this.graduation.recordAccess(
2412
- event.id,
2413
- options.sessionId || "unknown",
2414
- result.score
2415
- );
3352
+ this.graduation.recordAccess(event.id, options.sessionId || "unknown", result.score);
2416
3353
  }
2417
3354
  let sessionContext;
2418
3355
  if (options.includeSessionContext) {
2419
3356
  sessionContext = await this.getSessionContext(event.sessionId, event.id);
2420
3357
  }
2421
- memories.push({
2422
- event,
2423
- score: result.score,
2424
- sessionContext
2425
- });
3358
+ memories.push({ event, score: result.score, sessionContext });
2426
3359
  }
2427
3360
  return memories;
2428
3361
  }
2429
- /**
2430
- * Get surrounding context from the same session
2431
- */
2432
3362
  async getSessionContext(sessionId, eventId) {
2433
3363
  const sessionEvents = await this.eventStore.getSessionEvents(sessionId);
2434
3364
  const eventIndex = sessionEvents.findIndex((e) => e.id === eventId);
@@ -2441,54 +3371,85 @@ var Retriever = class {
2441
3371
  return void 0;
2442
3372
  return contextEvents.filter((e) => e.id !== eventId).map((e) => `[${e.eventType}]: ${e.content.slice(0, 200)}...`).join("\n");
2443
3373
  }
2444
- /**
2445
- * Build context string from memories (respecting token limit)
2446
- */
3374
+ buildUnifiedContext(projectResult, sharedMemories) {
3375
+ let context = projectResult.context;
3376
+ if (sharedMemories.length === 0)
3377
+ return context;
3378
+ context += "\n\n## Cross-Project Knowledge\n\n";
3379
+ for (const memory of sharedMemories.slice(0, 3)) {
3380
+ context += `### ${memory.title}
3381
+ `;
3382
+ if (memory.symptoms.length > 0)
3383
+ context += `**Symptoms:** ${memory.symptoms.join(", ")}
3384
+ `;
3385
+ context += `**Root Cause:** ${memory.rootCause}
3386
+ `;
3387
+ context += `**Solution:** ${memory.solution}
3388
+ `;
3389
+ if (memory.technologies && memory.technologies.length > 0)
3390
+ context += `**Technologies:** ${memory.technologies.join(", ")}
3391
+ `;
3392
+ context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
3393
+
3394
+ `;
3395
+ }
3396
+ return context;
3397
+ }
2447
3398
  buildContext(memories, maxTokens) {
2448
3399
  const parts = [];
2449
3400
  let currentTokens = 0;
2450
3401
  for (const memory of memories) {
2451
3402
  const memoryText = this.formatMemory(memory);
2452
3403
  const memoryTokens = this.estimateTokens(memoryText);
2453
- if (currentTokens + memoryTokens > maxTokens) {
3404
+ if (currentTokens + memoryTokens > maxTokens)
2454
3405
  break;
2455
- }
2456
3406
  parts.push(memoryText);
2457
3407
  currentTokens += memoryTokens;
2458
3408
  }
2459
- if (parts.length === 0) {
3409
+ if (parts.length === 0)
2460
3410
  return "";
2461
- }
2462
3411
  return `## Relevant Memories
2463
3412
 
2464
3413
  ${parts.join("\n\n---\n\n")}`;
2465
3414
  }
2466
- /**
2467
- * Format a single memory for context
2468
- */
2469
3415
  formatMemory(memory) {
2470
3416
  const { event, score, sessionContext } = memory;
2471
3417
  const date = event.timestamp.toISOString().split("T")[0];
2472
3418
  let text = `**${event.eventType}** (${date}, score: ${score.toFixed(2)})
2473
3419
  ${event.content}`;
2474
- if (sessionContext) {
3420
+ if (sessionContext)
2475
3421
  text += `
2476
3422
 
2477
3423
  _Context:_ ${sessionContext}`;
2478
- }
2479
3424
  return text;
2480
3425
  }
2481
- /**
2482
- * Estimate token count (rough approximation)
2483
- */
2484
- estimateTokens(text) {
2485
- return Math.ceil(text.length / 4);
3426
+ matchesMetadataScope(metadata, expected) {
3427
+ if (!metadata)
3428
+ return false;
3429
+ return Object.entries(expected).every(([path5, value]) => {
3430
+ const actual = path5.split(".").reduce((acc, key) => {
3431
+ if (typeof acc !== "object" || acc === null)
3432
+ return void 0;
3433
+ return acc[key];
3434
+ }, metadata);
3435
+ return actual === value;
3436
+ });
2486
3437
  }
2487
- /**
2488
- * Get event age in days (for recency scoring)
2489
- */
2490
- getEventAgeDays(eventId) {
2491
- return 0;
3438
+ tokenize(text) {
3439
+ return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter((t) => t.length >= 2).slice(0, 64);
3440
+ }
3441
+ keywordOverlap(a, b) {
3442
+ if (a.length === 0 || b.length === 0)
3443
+ return 0;
3444
+ const bs = new Set(b);
3445
+ let hit = 0;
3446
+ for (const t of a)
3447
+ if (bs.has(t))
3448
+ hit += 1;
3449
+ return hit / a.length;
3450
+ }
3451
+ estimateTokens(text) {
3452
+ return Math.ceil(text.length / 4);
2492
3453
  }
2493
3454
  };
2494
3455
  function createRetriever(eventStore, vectorStore, embedder, matcher) {
@@ -3779,6 +4740,59 @@ var ConsolidatedStore = class {
3779
4740
  [memoryId]
3780
4741
  );
3781
4742
  }
4743
+ /**
4744
+ * Create a long-term rule promoted from stable summaries
4745
+ */
4746
+ async createRule(input) {
4747
+ const ruleId = randomUUID6();
4748
+ await dbRun(
4749
+ this.db,
4750
+ `INSERT INTO consolidated_rules
4751
+ (rule_id, rule, topics, source_memory_ids, source_events, confidence, created_at)
4752
+ VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
4753
+ [
4754
+ ruleId,
4755
+ input.rule,
4756
+ JSON.stringify(input.topics),
4757
+ JSON.stringify(input.sourceMemoryIds),
4758
+ JSON.stringify(input.sourceEvents),
4759
+ input.confidence
4760
+ ]
4761
+ );
4762
+ return ruleId;
4763
+ }
4764
+ async getRules(options) {
4765
+ const limit = options?.limit || 100;
4766
+ const rows = await dbAll(
4767
+ this.db,
4768
+ `SELECT * FROM consolidated_rules ORDER BY confidence DESC, created_at DESC LIMIT ?`,
4769
+ [limit]
4770
+ );
4771
+ return rows.map((row) => ({
4772
+ ruleId: row.rule_id,
4773
+ rule: row.rule,
4774
+ topics: JSON.parse(row.topics || "[]"),
4775
+ sourceMemoryIds: JSON.parse(row.source_memory_ids || "[]"),
4776
+ sourceEvents: JSON.parse(row.source_events || "[]"),
4777
+ confidence: Number(row.confidence ?? 0.5),
4778
+ createdAt: toDate(row.created_at) || /* @__PURE__ */ new Date()
4779
+ }));
4780
+ }
4781
+ async countRules() {
4782
+ const result = await dbAll(
4783
+ this.db,
4784
+ `SELECT COUNT(*) as count FROM consolidated_rules`
4785
+ );
4786
+ return result[0]?.count || 0;
4787
+ }
4788
+ async hasRuleForSourceMemory(memoryId) {
4789
+ const rows = await dbAll(
4790
+ this.db,
4791
+ `SELECT COUNT(*) as count FROM consolidated_rules WHERE source_memory_ids LIKE ?`,
4792
+ [`%"${memoryId}"%`]
4793
+ );
4794
+ return (rows[0]?.count || 0) > 0;
4795
+ }
3782
4796
  /**
3783
4797
  * Get count of consolidated memories
3784
4798
  */
@@ -3928,7 +4942,14 @@ var ConsolidationWorker = class {
3928
4942
  * Force a consolidation run (manual trigger)
3929
4943
  */
3930
4944
  async forceRun() {
3931
- return await this.consolidate();
4945
+ const out = await this.consolidateWithReport();
4946
+ return out.consolidatedCount;
4947
+ }
4948
+ /**
4949
+ * Force a consolidation run and return metrics report
4950
+ */
4951
+ async forceRunWithReport() {
4952
+ return this.consolidateWithReport();
3932
4953
  }
3933
4954
  /**
3934
4955
  * Schedule the next consolidation check
@@ -3968,12 +4989,21 @@ var ConsolidationWorker = class {
3968
4989
  * Perform consolidation
3969
4990
  */
3970
4991
  async consolidate() {
4992
+ const out = await this.consolidateWithReport();
4993
+ return out.consolidatedCount;
4994
+ }
4995
+ async consolidateWithReport() {
3971
4996
  const workingSet = await this.workingSetStore.get();
3972
4997
  if (workingSet.recentEvents.length < 3) {
3973
- return 0;
4998
+ return {
4999
+ consolidatedCount: 0,
5000
+ promotedRuleCount: 0,
5001
+ report: this.buildCostQualityReport(workingSet.recentEvents, [], 0)
5002
+ };
3974
5003
  }
3975
5004
  const groups = this.groupByTopic(workingSet.recentEvents);
3976
5005
  let consolidatedCount = 0;
5006
+ const createdMemoryIds = [];
3977
5007
  for (const group of groups) {
3978
5008
  if (group.events.length < 3)
3979
5009
  continue;
@@ -3982,14 +5012,16 @@ var ConsolidationWorker = class {
3982
5012
  if (alreadyConsolidated)
3983
5013
  continue;
3984
5014
  const summary = await this.summarize(group);
3985
- await this.consolidatedStore.create({
5015
+ const memoryId = await this.consolidatedStore.create({
3986
5016
  summary,
3987
5017
  topics: group.topics,
3988
5018
  sourceEvents: eventIds,
3989
5019
  confidence: this.calculateConfidence(group)
3990
5020
  });
5021
+ createdMemoryIds.push(memoryId);
3991
5022
  consolidatedCount++;
3992
5023
  }
5024
+ const promotedRuleCount = await this.promoteStableSummariesToRules(createdMemoryIds);
3993
5025
  if (consolidatedCount > 0) {
3994
5026
  const consolidatedEventIds = groups.filter((g) => g.events.length >= 3).flatMap((g) => g.events.map((e) => e.id));
3995
5027
  const oldEventIds = consolidatedEventIds.filter((id) => {
@@ -4003,7 +5035,61 @@ var ConsolidationWorker = class {
4003
5035
  await this.workingSetStore.prune(oldEventIds);
4004
5036
  }
4005
5037
  }
4006
- return consolidatedCount;
5038
+ const report = this.buildCostQualityReport(workingSet.recentEvents, groups, consolidatedCount);
5039
+ return { consolidatedCount, promotedRuleCount, report };
5040
+ }
5041
+ async promoteStableSummariesToRules(memoryIds) {
5042
+ let promoted = 0;
5043
+ for (const memoryId of memoryIds) {
5044
+ const memory = await this.consolidatedStore.get(memoryId);
5045
+ if (!memory)
5046
+ continue;
5047
+ if (memory.confidence < 0.55)
5048
+ continue;
5049
+ if (memory.sourceEvents.length < 4)
5050
+ continue;
5051
+ const exists = await this.consolidatedStore.hasRuleForSourceMemory(memoryId);
5052
+ if (exists)
5053
+ continue;
5054
+ const rule = this.buildRuleFromSummary(memory.summary, memory.topics);
5055
+ if (!rule)
5056
+ continue;
5057
+ await this.consolidatedStore.createRule({
5058
+ rule,
5059
+ topics: memory.topics,
5060
+ sourceMemoryIds: [memory.memoryId],
5061
+ sourceEvents: memory.sourceEvents,
5062
+ confidence: Math.min(1, memory.confidence + 0.08)
5063
+ });
5064
+ promoted++;
5065
+ }
5066
+ return promoted;
5067
+ }
5068
+ buildRuleFromSummary(summary, topics) {
5069
+ const lines = summary.split(/\r?\n/).map((l) => l.trim()).filter(Boolean).filter((l) => !l.toLowerCase().startsWith("topics:"));
5070
+ const bullet = lines.find((l) => l.startsWith("- "))?.replace(/^-\s*/, "");
5071
+ const seed = bullet || lines[0];
5072
+ if (!seed || seed.length < 8)
5073
+ return null;
5074
+ const topicPrefix = topics.length > 0 ? `[${topics.slice(0, 2).join(", ")}] ` : "";
5075
+ return `${topicPrefix}${seed}`;
5076
+ }
5077
+ buildCostQualityReport(events, groups, consolidatedCount) {
5078
+ const beforeTokenEstimate = events.reduce((acc, e) => acc + this.estimateTokens(e.content), 0);
5079
+ const afterSummaries = groups.filter((g) => g.events.length >= 3).slice(0, Math.max(consolidatedCount, 1));
5080
+ const afterTokenEstimate = afterSummaries.length > 0 ? afterSummaries.reduce((acc, g) => acc + this.estimateTokens(this.ruleBasedSummary(g)), 0) : beforeTokenEstimate;
5081
+ const reductionRatio = beforeTokenEstimate > 0 ? Math.max(0, (beforeTokenEstimate - afterTokenEstimate) / beforeTokenEstimate) : 0;
5082
+ const qualityGuardPassed = consolidatedCount === 0 ? true : groups.filter((g) => g.events.length >= 3).every((g) => this.calculateConfidence(g) >= 0.55);
5083
+ return {
5084
+ beforeTokenEstimate,
5085
+ afterTokenEstimate,
5086
+ reductionRatio,
5087
+ qualityGuardPassed,
5088
+ details: `groups=${groups.length}, consolidated=${consolidatedCount}`
5089
+ };
5090
+ }
5091
+ estimateTokens(text) {
5092
+ return Math.ceil((text || "").length / 4);
4007
5093
  }
4008
5094
  /**
4009
5095
  * Check if consolidation should run
@@ -4563,13 +5649,185 @@ function createGraduationWorker(eventStore, graduation, config) {
4563
5649
  );
4564
5650
  }
4565
5651
 
5652
+ // src/core/md-mirror.ts
5653
+ import * as fs3 from "node:fs";
5654
+ import * as path2 from "node:path";
5655
+ function sanitizeSegment2(input, fallback) {
5656
+ const v = (input || "").trim().toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
5657
+ return v || fallback;
5658
+ }
5659
+ function getAtPath(obj, dotted) {
5660
+ if (!obj)
5661
+ return void 0;
5662
+ return dotted.split(".").reduce((acc, key) => {
5663
+ if (!acc || typeof acc !== "object")
5664
+ return void 0;
5665
+ return acc[key];
5666
+ }, obj);
5667
+ }
5668
+ function buildMirrorPath2(rootDir, event) {
5669
+ const meta = event.metadata;
5670
+ const namespaceRaw = getAtPath(meta, "namespace") ?? getAtPath(meta, "scope.namespace") ?? event.eventType;
5671
+ const namespace = sanitizeSegment2(typeof namespaceRaw === "string" ? namespaceRaw : void 0, "general");
5672
+ const categoryRaw = getAtPath(meta, "categoryPath") ?? getAtPath(meta, "scope.categoryPath");
5673
+ const categoryPath = Array.isArray(categoryRaw) && categoryRaw.length > 0 ? categoryRaw.map((x) => sanitizeSegment2(typeof x === "string" ? x : void 0, "uncategorized")) : ["uncategorized"];
5674
+ const d = event.timestamp;
5675
+ const yyyy = d.getFullYear();
5676
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
5677
+ const dd = String(d.getDate()).padStart(2, "0");
5678
+ return path2.join(rootDir, "memory", namespace, ...categoryPath, `${yyyy}-${mm}-${dd}.md`);
5679
+ }
5680
+ var MarkdownMirror2 = class {
5681
+ constructor(rootDir) {
5682
+ this.rootDir = rootDir;
5683
+ }
5684
+ async append(event, eventId) {
5685
+ const out = buildMirrorPath2(this.rootDir, event);
5686
+ fs3.mkdirSync(path2.dirname(out), { recursive: true });
5687
+ const lines = [
5688
+ "",
5689
+ `## ${event.timestamp.toISOString()} | ${eventId ?? "pending-id"}`,
5690
+ `- type: ${event.eventType}`,
5691
+ `- session: ${event.sessionId}`,
5692
+ event.content
5693
+ ];
5694
+ await fs3.promises.appendFile(out, lines.join("\n"), "utf8");
5695
+ await this.refreshIndex();
5696
+ }
5697
+ async refreshIndex() {
5698
+ const memoryRoot = path2.join(this.rootDir, "memory");
5699
+ await fs3.promises.mkdir(memoryRoot, { recursive: true });
5700
+ const files = [];
5701
+ await this.walk(memoryRoot, files);
5702
+ const mdFiles = files.filter((f) => f.endsWith(".md")).map((f) => path2.relative(this.rootDir, f)).filter((rel) => rel !== path2.join("memory", "_index.md")).sort();
5703
+ const index = [
5704
+ "# Memory Index",
5705
+ "",
5706
+ "Generated automatically by MarkdownMirror.",
5707
+ "",
5708
+ ...mdFiles.map((rel) => `- ${rel}`),
5709
+ ""
5710
+ ].join("\n");
5711
+ await fs3.promises.writeFile(path2.join(memoryRoot, "_index.md"), index, "utf8");
5712
+ }
5713
+ async walk(dir, out) {
5714
+ const entries = await fs3.promises.readdir(dir, { withFileTypes: true });
5715
+ for (const e of entries) {
5716
+ const full = path2.join(dir, e.name);
5717
+ if (e.isDirectory()) {
5718
+ await this.walk(full, out);
5719
+ } else {
5720
+ out.push(full);
5721
+ }
5722
+ }
5723
+ }
5724
+ };
5725
+
5726
+ // src/core/ingest-interceptor.ts
5727
+ var IngestInterceptorRegistry = class {
5728
+ before = [];
5729
+ after = [];
5730
+ onError = [];
5731
+ registerBefore(interceptor) {
5732
+ this.before.push(interceptor);
5733
+ return () => {
5734
+ this.before = this.before.filter((i) => i !== interceptor);
5735
+ };
5736
+ }
5737
+ registerAfter(interceptor) {
5738
+ this.after.push(interceptor);
5739
+ return () => {
5740
+ this.after = this.after.filter((i) => i !== interceptor);
5741
+ };
5742
+ }
5743
+ registerOnError(interceptor) {
5744
+ this.onError.push(interceptor);
5745
+ return () => {
5746
+ this.onError = this.onError.filter((i) => i !== interceptor);
5747
+ };
5748
+ }
5749
+ async run(stage, context) {
5750
+ const interceptors = stage === "before" ? this.before : stage === "after" ? this.after : this.onError;
5751
+ for (const interceptor of interceptors) {
5752
+ await interceptor({ ...context, stage });
5753
+ }
5754
+ }
5755
+ };
5756
+ function mergeHierarchicalMetadata(base, patch) {
5757
+ if (!base && !patch)
5758
+ return void 0;
5759
+ if (!base)
5760
+ return patch;
5761
+ if (!patch)
5762
+ return base;
5763
+ const result = { ...base };
5764
+ for (const [key, value] of Object.entries(patch)) {
5765
+ const current = result[key];
5766
+ if (typeof current === "object" && current !== null && !Array.isArray(current) && typeof value === "object" && value !== null && !Array.isArray(value)) {
5767
+ result[key] = mergeHierarchicalMetadata(
5768
+ current,
5769
+ value
5770
+ );
5771
+ } else {
5772
+ result[key] = value;
5773
+ }
5774
+ }
5775
+ return result;
5776
+ }
5777
+
5778
+ // src/core/tag-taxonomy.ts
5779
+ var TAG_NAMESPACES = {
5780
+ SYSTEM: "sys:",
5781
+ QUALITY: "q:",
5782
+ PROJECT: "proj:",
5783
+ TOPIC: "topic:",
5784
+ TEMPORAL: "t:",
5785
+ USER: "user:",
5786
+ AGENT: "agent:"
5787
+ };
5788
+ var VALID_TAG_NAMESPACES = new Set(Object.values(TAG_NAMESPACES));
5789
+ function parseTag(tag) {
5790
+ const value = (tag || "").trim();
5791
+ const idx = value.indexOf(":");
5792
+ if (idx <= 0)
5793
+ return { value };
5794
+ const namespace = `${value.slice(0, idx)}:`;
5795
+ const tagValue = value.slice(idx + 1);
5796
+ if (!tagValue)
5797
+ return { value };
5798
+ return { namespace, value: tagValue };
5799
+ }
5800
+ function validateTag(tag) {
5801
+ const normalized = (tag || "").trim();
5802
+ if (!normalized)
5803
+ return false;
5804
+ const { namespace } = parseTag(normalized);
5805
+ if (!namespace)
5806
+ return true;
5807
+ return VALID_TAG_NAMESPACES.has(namespace);
5808
+ }
5809
+ function normalizeTags(tags) {
5810
+ if (!Array.isArray(tags))
5811
+ return [];
5812
+ const dedup = /* @__PURE__ */ new Set();
5813
+ for (const item of tags) {
5814
+ if (typeof item !== "string")
5815
+ continue;
5816
+ const normalized = item.trim();
5817
+ if (!validateTag(normalized))
5818
+ continue;
5819
+ dedup.add(normalized);
5820
+ }
5821
+ return [...dedup];
5822
+ }
5823
+
4566
5824
  // src/services/memory-service.ts
4567
5825
  function normalizePath(projectPath) {
4568
- const expanded = projectPath.startsWith("~") ? path.join(os.homedir(), projectPath.slice(1)) : projectPath;
5826
+ const expanded = projectPath.startsWith("~") ? path3.join(os.homedir(), projectPath.slice(1)) : projectPath;
4569
5827
  try {
4570
- return fs.realpathSync(expanded);
5828
+ return fs4.realpathSync(expanded);
4571
5829
  } catch {
4572
- return path.resolve(expanded);
5830
+ return path3.resolve(expanded);
4573
5831
  }
4574
5832
  }
4575
5833
  function hashProjectPath(projectPath) {
@@ -4578,14 +5836,14 @@ function hashProjectPath(projectPath) {
4578
5836
  }
4579
5837
  function getProjectStoragePath(projectPath) {
4580
5838
  const hash = hashProjectPath(projectPath);
4581
- return path.join(os.homedir(), ".claude-code", "memory", "projects", hash);
5839
+ return path3.join(os.homedir(), ".claude-code", "memory", "projects", hash);
4582
5840
  }
4583
- var REGISTRY_PATH = path.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
4584
- var SHARED_STORAGE_PATH = path.join(os.homedir(), ".claude-code", "memory", "shared");
5841
+ var REGISTRY_PATH = path3.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
5842
+ var SHARED_STORAGE_PATH = path3.join(os.homedir(), ".claude-code", "memory", "shared");
4585
5843
  function loadSessionRegistry() {
4586
5844
  try {
4587
- if (fs.existsSync(REGISTRY_PATH)) {
4588
- const data = fs.readFileSync(REGISTRY_PATH, "utf-8");
5845
+ if (fs4.existsSync(REGISTRY_PATH)) {
5846
+ const data = fs4.readFileSync(REGISTRY_PATH, "utf-8");
4589
5847
  return JSON.parse(data);
4590
5848
  }
4591
5849
  } catch (error) {
@@ -4611,6 +5869,7 @@ var MemoryService = class {
4611
5869
  vectorWorker = null;
4612
5870
  graduationWorker = null;
4613
5871
  initialized = false;
5872
+ ingestInterceptors = new IngestInterceptorRegistry();
4614
5873
  // Endless Mode components
4615
5874
  workingSetStore = null;
4616
5875
  consolidatedStore = null;
@@ -4624,20 +5883,27 @@ var MemoryService = class {
4624
5883
  sharedPromoter = null;
4625
5884
  sharedStoreConfig = null;
4626
5885
  projectHash = null;
5886
+ projectPath = null;
4627
5887
  readOnly;
4628
5888
  lightweightMode;
5889
+ mdMirror;
4629
5890
  constructor(config) {
4630
5891
  const storagePath = this.expandPath(config.storagePath);
4631
5892
  this.readOnly = config.readOnly ?? false;
4632
5893
  this.lightweightMode = config.lightweightMode ?? false;
4633
- if (!this.readOnly && !fs.existsSync(storagePath)) {
4634
- fs.mkdirSync(storagePath, { recursive: true });
5894
+ this.mdMirror = new MarkdownMirror2(process.cwd());
5895
+ if (!this.readOnly && !fs4.existsSync(storagePath)) {
5896
+ fs4.mkdirSync(storagePath, { recursive: true });
4635
5897
  }
4636
5898
  this.projectHash = config.projectHash || null;
5899
+ this.projectPath = config.projectPath || null;
4637
5900
  this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
4638
5901
  this.sqliteStore = new SQLiteEventStore(
4639
- path.join(storagePath, "events.sqlite"),
4640
- { readonly: this.readOnly }
5902
+ path3.join(storagePath, "events.sqlite"),
5903
+ {
5904
+ readonly: this.readOnly,
5905
+ markdownMirrorRoot: storagePath
5906
+ }
4641
5907
  );
4642
5908
  const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
4643
5909
  if (!analyticsEnabled) {
@@ -4645,7 +5911,7 @@ var MemoryService = class {
4645
5911
  } else if (this.readOnly) {
4646
5912
  try {
4647
5913
  this.analyticsStore = new EventStore(
4648
- path.join(storagePath, "analytics.duckdb"),
5914
+ path3.join(storagePath, "analytics.duckdb"),
4649
5915
  { readOnly: true }
4650
5916
  );
4651
5917
  } catch {
@@ -4653,11 +5919,11 @@ var MemoryService = class {
4653
5919
  }
4654
5920
  } else {
4655
5921
  this.analyticsStore = new EventStore(
4656
- path.join(storagePath, "analytics.duckdb"),
5922
+ path3.join(storagePath, "analytics.duckdb"),
4657
5923
  { readOnly: false }
4658
5924
  );
4659
5925
  }
4660
- this.vectorStore = new VectorStore(path.join(storagePath, "vectors"));
5926
+ this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
4661
5927
  this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
4662
5928
  this.matcher = getDefaultMatcher();
4663
5929
  this.retriever = createRetriever(
@@ -4667,6 +5933,7 @@ var MemoryService = class {
4667
5933
  this.embedder,
4668
5934
  this.matcher
4669
5935
  );
5936
+ this.retriever.setQueryRewriter((q) => this.rewriteQueryIntent(q));
4670
5937
  this.graduation = createGraduationPipeline(this.sqliteStore);
4671
5938
  }
4672
5939
  /**
@@ -4726,16 +5993,16 @@ var MemoryService = class {
4726
5993
  */
4727
5994
  async initializeSharedStore() {
4728
5995
  const sharedPath = this.sharedStoreConfig?.sharedStoragePath ? this.expandPath(this.sharedStoreConfig.sharedStoragePath) : SHARED_STORAGE_PATH;
4729
- if (!fs.existsSync(sharedPath)) {
4730
- fs.mkdirSync(sharedPath, { recursive: true });
5996
+ if (!fs4.existsSync(sharedPath)) {
5997
+ fs4.mkdirSync(sharedPath, { recursive: true });
4731
5998
  }
4732
5999
  this.sharedEventStore = createSharedEventStore(
4733
- path.join(sharedPath, "shared.duckdb")
6000
+ path3.join(sharedPath, "shared.duckdb")
4734
6001
  );
4735
6002
  await this.sharedEventStore.initialize();
4736
6003
  this.sharedStore = createSharedStore(this.sharedEventStore);
4737
6004
  this.sharedVectorStore = createSharedVectorStore(
4738
- path.join(sharedPath, "vectors")
6005
+ path3.join(sharedPath, "vectors")
4739
6006
  );
4740
6007
  await this.sharedVectorStore.initialize();
4741
6008
  this.sharedPromoter = createSharedPromoter(
@@ -4746,6 +6013,86 @@ var MemoryService = class {
4746
6013
  );
4747
6014
  this.retriever.setSharedStores(this.sharedStore, this.sharedVectorStore);
4748
6015
  }
6016
+ registerIngestBefore(interceptor) {
6017
+ return this.ingestInterceptors.registerBefore(interceptor);
6018
+ }
6019
+ registerIngestAfter(interceptor) {
6020
+ return this.ingestInterceptors.registerAfter(interceptor);
6021
+ }
6022
+ registerIngestOnError(interceptor) {
6023
+ return this.ingestInterceptors.registerOnError(interceptor);
6024
+ }
6025
+ async ingestWithInterceptors(operation, input, onSuccess) {
6026
+ const normalizedInput = {
6027
+ ...input,
6028
+ metadata: mergeHierarchicalMetadata(
6029
+ {
6030
+ ingest: {
6031
+ operation,
6032
+ pipeline: "default",
6033
+ ts: (/* @__PURE__ */ new Date()).toISOString()
6034
+ },
6035
+ ...this.projectHash ? {
6036
+ scope: {
6037
+ project: {
6038
+ hash: this.projectHash,
6039
+ ...this.projectPath ? { path: this.projectPath } : {}
6040
+ }
6041
+ },
6042
+ tags: [`proj:${this.projectHash}`]
6043
+ } : {}
6044
+ },
6045
+ input.metadata
6046
+ )
6047
+ };
6048
+ if (this.projectHash && normalizedInput.metadata) {
6049
+ const meta = normalizedInput.metadata;
6050
+ const currentTags = Array.isArray(meta.tags) ? meta.tags.filter((x) => typeof x === "string") : [];
6051
+ const projectTag = `proj:${this.projectHash}`;
6052
+ if (!currentTags.includes(projectTag)) {
6053
+ meta.tags = [...currentTags, projectTag];
6054
+ }
6055
+ }
6056
+ if (normalizedInput.metadata) {
6057
+ const meta = normalizedInput.metadata;
6058
+ const normalizedTags = normalizeTags(meta.tags);
6059
+ if (normalizedTags.length > 0) {
6060
+ meta.tags = normalizedTags;
6061
+ }
6062
+ }
6063
+ await this.ingestInterceptors.run("before", {
6064
+ operation,
6065
+ sessionId: normalizedInput.sessionId,
6066
+ event: normalizedInput
6067
+ });
6068
+ try {
6069
+ const result = await this.sqliteStore.append(normalizedInput);
6070
+ if (result.success && !result.isDuplicate) {
6071
+ if (onSuccess) {
6072
+ await onSuccess(result.eventId);
6073
+ }
6074
+ try {
6075
+ await this.mdMirror.append(normalizedInput, result.eventId);
6076
+ } catch {
6077
+ }
6078
+ }
6079
+ await this.ingestInterceptors.run("after", {
6080
+ operation,
6081
+ sessionId: normalizedInput.sessionId,
6082
+ event: normalizedInput
6083
+ });
6084
+ return result;
6085
+ } catch (error) {
6086
+ const normalizedError = error instanceof Error ? error : new Error(String(error));
6087
+ await this.ingestInterceptors.run("error", {
6088
+ operation,
6089
+ sessionId: normalizedInput.sessionId,
6090
+ event: normalizedInput,
6091
+ error: normalizedError
6092
+ });
6093
+ throw error;
6094
+ }
6095
+ }
4749
6096
  /**
4750
6097
  * Start a new session
4751
6098
  */
@@ -4773,50 +6120,57 @@ var MemoryService = class {
4773
6120
  */
4774
6121
  async storeUserPrompt(sessionId, content, metadata) {
4775
6122
  await this.initialize();
4776
- const result = await this.sqliteStore.append({
4777
- eventType: "user_prompt",
4778
- sessionId,
4779
- timestamp: /* @__PURE__ */ new Date(),
4780
- content,
4781
- metadata
4782
- });
4783
- if (result.success && !result.isDuplicate) {
4784
- await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
4785
- }
4786
- return result;
6123
+ return this.ingestWithInterceptors(
6124
+ "user_prompt",
6125
+ {
6126
+ eventType: "user_prompt",
6127
+ sessionId,
6128
+ timestamp: /* @__PURE__ */ new Date(),
6129
+ content,
6130
+ metadata
6131
+ },
6132
+ async (eventId) => {
6133
+ await this.sqliteStore.enqueueForEmbedding(eventId, content);
6134
+ }
6135
+ );
4787
6136
  }
4788
6137
  /**
4789
6138
  * Store an agent response
4790
6139
  */
4791
6140
  async storeAgentResponse(sessionId, content, metadata) {
4792
6141
  await this.initialize();
4793
- const result = await this.sqliteStore.append({
4794
- eventType: "agent_response",
4795
- sessionId,
4796
- timestamp: /* @__PURE__ */ new Date(),
4797
- content,
4798
- metadata
4799
- });
4800
- if (result.success && !result.isDuplicate) {
4801
- await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
4802
- }
4803
- return result;
6142
+ return this.ingestWithInterceptors(
6143
+ "agent_response",
6144
+ {
6145
+ eventType: "agent_response",
6146
+ sessionId,
6147
+ timestamp: /* @__PURE__ */ new Date(),
6148
+ content,
6149
+ metadata
6150
+ },
6151
+ async (eventId) => {
6152
+ await this.sqliteStore.enqueueForEmbedding(eventId, content);
6153
+ }
6154
+ );
4804
6155
  }
4805
6156
  /**
4806
6157
  * Store a session summary
4807
6158
  */
4808
- async storeSessionSummary(sessionId, summary) {
6159
+ async storeSessionSummary(sessionId, summary, metadata) {
4809
6160
  await this.initialize();
4810
- const result = await this.sqliteStore.append({
4811
- eventType: "session_summary",
4812
- sessionId,
4813
- timestamp: /* @__PURE__ */ new Date(),
4814
- content: summary
4815
- });
4816
- if (result.success && !result.isDuplicate) {
4817
- await this.sqliteStore.enqueueForEmbedding(result.eventId, summary);
4818
- }
4819
- return result;
6161
+ return this.ingestWithInterceptors(
6162
+ "session_summary",
6163
+ {
6164
+ eventType: "session_summary",
6165
+ sessionId,
6166
+ timestamp: /* @__PURE__ */ new Date(),
6167
+ content: summary,
6168
+ metadata
6169
+ },
6170
+ async (eventId) => {
6171
+ await this.sqliteStore.enqueueForEmbedding(eventId, summary);
6172
+ }
6173
+ );
4820
6174
  }
4821
6175
  /**
4822
6176
  * Store a tool observation
@@ -4824,39 +6178,182 @@ var MemoryService = class {
4824
6178
  async storeToolObservation(sessionId, payload) {
4825
6179
  await this.initialize();
4826
6180
  const content = JSON.stringify(payload);
4827
- const result = await this.sqliteStore.append({
4828
- eventType: "tool_observation",
4829
- sessionId,
4830
- timestamp: /* @__PURE__ */ new Date(),
4831
- content,
4832
- metadata: {
4833
- toolName: payload.toolName,
4834
- success: payload.success
6181
+ const turnId = payload.metadata?.turnId;
6182
+ return this.ingestWithInterceptors(
6183
+ "tool_observation",
6184
+ {
6185
+ eventType: "tool_observation",
6186
+ sessionId,
6187
+ timestamp: /* @__PURE__ */ new Date(),
6188
+ content,
6189
+ metadata: {
6190
+ toolName: payload.toolName,
6191
+ success: payload.success,
6192
+ ...turnId ? { turnId } : {}
6193
+ }
6194
+ },
6195
+ async (eventId) => {
6196
+ const embeddingContent = createToolObservationEmbedding(
6197
+ payload.toolName,
6198
+ payload.metadata || {},
6199
+ payload.success
6200
+ );
6201
+ await this.sqliteStore.enqueueForEmbedding(eventId, embeddingContent);
4835
6202
  }
4836
- });
4837
- if (result.success && !result.isDuplicate) {
4838
- const embeddingContent = createToolObservationEmbedding(
4839
- payload.toolName,
4840
- payload.metadata || {},
4841
- payload.success
4842
- );
4843
- await this.sqliteStore.enqueueForEmbedding(result.eventId, embeddingContent);
4844
- }
4845
- return result;
6203
+ );
4846
6204
  }
4847
6205
  /**
4848
6206
  * Retrieve relevant memories for a query
4849
6207
  */
4850
6208
  async retrieveMemories(query, options) {
4851
6209
  await this.initialize();
6210
+ const rerankWeights = await this.getRerankWeights(options?.adaptiveRerank === true);
6211
+ let result;
4852
6212
  if (options?.includeShared && this.sharedStore) {
4853
- return this.retriever.retrieveUnified(query, {
6213
+ result = await this.retriever.retrieveUnified(query, {
4854
6214
  ...options,
6215
+ intentRewrite: options?.intentRewrite === true,
6216
+ rerankWeights,
4855
6217
  includeShared: true,
4856
- projectHash: this.projectHash || void 0
6218
+ projectHash: this.projectHash || void 0,
6219
+ projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
6220
+ allowedProjectHashes: options?.allowedProjectHashes
6221
+ });
6222
+ } else {
6223
+ result = await this.retriever.retrieve(query, {
6224
+ ...options,
6225
+ intentRewrite: options?.intentRewrite === true,
6226
+ rerankWeights,
6227
+ projectHash: this.projectHash || void 0,
6228
+ projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
6229
+ allowedProjectHashes: options?.allowedProjectHashes
6230
+ });
6231
+ }
6232
+ try {
6233
+ const selectedEventIds = result.memories.map((m) => m.event.id);
6234
+ const selectedDetails = (result.selectedDebug || []).map((d) => ({
6235
+ eventId: d.eventId,
6236
+ score: d.score,
6237
+ semanticScore: d.semanticScore,
6238
+ lexicalScore: d.lexicalScore,
6239
+ recencyScore: d.recencyScore
6240
+ }));
6241
+ const candidateDetails = (result.candidateDebug || []).map((d) => ({
6242
+ eventId: d.eventId,
6243
+ score: d.score,
6244
+ semanticScore: d.semanticScore,
6245
+ lexicalScore: d.lexicalScore,
6246
+ recencyScore: d.recencyScore
6247
+ }));
6248
+ const candidateEventIds = candidateDetails.length > 0 ? candidateDetails.map((d) => d.eventId) : selectedEventIds;
6249
+ await this.sqliteStore.recordRetrievalTrace({
6250
+ sessionId: options?.sessionId,
6251
+ projectHash: this.projectHash || void 0,
6252
+ queryText: query,
6253
+ strategy: options?.strategy || "auto",
6254
+ candidateEventIds,
6255
+ selectedEventIds,
6256
+ candidateDetails,
6257
+ selectedDetails,
6258
+ confidence: result.matchResult.confidence,
6259
+ fallbackTrace: result.fallbackTrace || []
4857
6260
  });
6261
+ } catch {
6262
+ }
6263
+ return result;
6264
+ }
6265
+ getConfiguredRerankWeights() {
6266
+ const semantic = Number(process.env.MEMORY_RERANK_WEIGHT_SEMANTIC ?? "");
6267
+ const lexical = Number(process.env.MEMORY_RERANK_WEIGHT_LEXICAL ?? "");
6268
+ const recency = Number(process.env.MEMORY_RERANK_WEIGHT_RECENCY ?? "");
6269
+ const allFinite = [semantic, lexical, recency].every((v) => Number.isFinite(v));
6270
+ if (!allFinite)
6271
+ return void 0;
6272
+ const nonNegative = [semantic, lexical, recency].every((v) => v >= 0);
6273
+ const total = semantic + lexical + recency;
6274
+ if (!nonNegative || total <= 0)
6275
+ return void 0;
6276
+ return {
6277
+ semantic: semantic / total,
6278
+ lexical: lexical / total,
6279
+ recency: recency / total
6280
+ };
6281
+ }
6282
+ async getRerankWeights(adaptive) {
6283
+ const configured = this.getConfiguredRerankWeights();
6284
+ if (configured)
6285
+ return configured;
6286
+ if (adaptive)
6287
+ return this.getAdaptiveRerankWeights();
6288
+ return void 0;
6289
+ }
6290
+ async rewriteQueryIntent(query) {
6291
+ if (process.env.MEMORY_INTENT_REWRITE_ENABLED !== "1")
6292
+ return null;
6293
+ const apiUrl = process.env.COMPANY_STOCK_API_URL || process.env.COMPANY_INT_API_URL;
6294
+ if (!apiUrl)
6295
+ return null;
6296
+ const controller = new AbortController();
6297
+ const timeoutMs = Number(process.env.MEMORY_INTENT_REWRITE_TIMEOUT_MS || 5e3);
6298
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
6299
+ try {
6300
+ const prompt = [
6301
+ "Rewrite user query for memory retrieval intent expansion.",
6302
+ "Return plain text only, one line, no markdown.",
6303
+ `Query: ${query}`
6304
+ ].join("\n");
6305
+ const res = await fetch(apiUrl, {
6306
+ method: "POST",
6307
+ headers: {
6308
+ "Content-Type": "application/json",
6309
+ Accept: "*/*",
6310
+ Origin: process.env.COMPANY_INT_ORIGIN || "http://company-int.aplusai.ai",
6311
+ Referer: process.env.COMPANY_INT_REFERER || "http://company-int.aplusai.ai/"
6312
+ },
6313
+ body: JSON.stringify({
6314
+ question: prompt,
6315
+ company_name: null,
6316
+ conversation_id: null
6317
+ }),
6318
+ signal: controller.signal
6319
+ });
6320
+ const text = (await res.text()).trim();
6321
+ if (!text)
6322
+ return null;
6323
+ const oneLine = text.replace(/^data:\s*/gm, "").split(/\r?\n/).map((x) => x.trim()).filter(Boolean).join(" ").slice(0, 240);
6324
+ if (!oneLine || oneLine.toLowerCase() === query.toLowerCase())
6325
+ return null;
6326
+ return oneLine;
6327
+ } catch {
6328
+ return null;
6329
+ } finally {
6330
+ clearTimeout(timeout);
6331
+ }
6332
+ }
6333
+ async getAdaptiveRerankWeights() {
6334
+ try {
6335
+ const s = await this.sqliteStore.getHelpfulnessStats();
6336
+ if (s.totalEvaluated < 20)
6337
+ return void 0;
6338
+ let semantic = 0.7;
6339
+ let lexical = 0.2;
6340
+ let recency = 0.1;
6341
+ if (s.avgScore < 0.45) {
6342
+ semantic -= 0.1;
6343
+ lexical += 0.1;
6344
+ } else if (s.avgScore > 0.75) {
6345
+ semantic += 0.05;
6346
+ lexical -= 0.05;
6347
+ }
6348
+ if (s.unhelpful > s.helpful) {
6349
+ recency += 0.05;
6350
+ semantic -= 0.03;
6351
+ lexical -= 0.02;
6352
+ }
6353
+ return { semantic, lexical, recency };
6354
+ } catch {
6355
+ return void 0;
4858
6356
  }
4859
- return this.retriever.retrieve(query, options);
4860
6357
  }
4861
6358
  /**
4862
6359
  * Fast keyword search using SQLite FTS5
@@ -4898,6 +6395,18 @@ var MemoryService = class {
4898
6395
  /**
4899
6396
  * Get memory statistics
4900
6397
  */
6398
+ async getOutboxStats() {
6399
+ await this.initialize();
6400
+ return this.sqliteStore.getOutboxStats();
6401
+ }
6402
+ async getRetrievalTraceStats() {
6403
+ await this.initialize();
6404
+ return this.sqliteStore.getRetrievalTraceStats();
6405
+ }
6406
+ async getRecentRetrievalTraces(limit = 50) {
6407
+ await this.initialize();
6408
+ return this.sqliteStore.getRecentRetrievalTraces(limit);
6409
+ }
4901
6410
  async getStats() {
4902
6411
  await this.initialize();
4903
6412
  const recentEvents = await this.sqliteStore.getRecentEvents(1e4);
@@ -5114,6 +6623,31 @@ var MemoryService = class {
5114
6623
  return [];
5115
6624
  return this.consolidatedStore.getAll({ limit });
5116
6625
  }
6626
+ /**
6627
+ * Extract topic keywords from event content (markdown headings and key terms)
6628
+ */
6629
+ extractTopicsFromContent(content) {
6630
+ const topics = /* @__PURE__ */ new Set();
6631
+ const headings = content.match(/^#{1,3}\s+(.+)$/gm);
6632
+ if (headings) {
6633
+ for (const h of headings.slice(0, 5)) {
6634
+ const text = h.replace(/^#+\s+/, "").replace(/[*_`#]/g, "").trim();
6635
+ if (text.length > 2 && text.length < 50) {
6636
+ topics.add(text);
6637
+ }
6638
+ }
6639
+ }
6640
+ const boldTerms = content.match(/\*\*([^*]+)\*\*/g);
6641
+ if (boldTerms) {
6642
+ for (const b of boldTerms.slice(0, 5)) {
6643
+ const text = b.replace(/\*\*/g, "").trim();
6644
+ if (text.length > 2 && text.length < 30) {
6645
+ topics.add(text);
6646
+ }
6647
+ }
6648
+ }
6649
+ return Array.from(topics).slice(0, 5);
6650
+ }
5117
6651
  /**
5118
6652
  * Increment access count for memories that were used in prompts
5119
6653
  */
@@ -5137,8 +6671,7 @@ var MemoryService = class {
5137
6671
  return events.map((event) => ({
5138
6672
  memoryId: event.id,
5139
6673
  summary: event.content.substring(0, 200) + (event.content.length > 200 ? "..." : ""),
5140
- topics: [],
5141
- // Could extract topics from content if needed
6674
+ topics: this.extractTopicsFromContent(event.content),
5142
6675
  accessCount: event.access_count || 0,
5143
6676
  lastAccessed: event.last_accessed_at || null,
5144
6677
  confidence: 1,
@@ -5159,6 +6692,34 @@ var MemoryService = class {
5159
6692
  }
5160
6693
  return [];
5161
6694
  }
6695
+ /**
6696
+ * Record a memory retrieval for helpfulness tracking
6697
+ */
6698
+ async recordRetrieval(eventId, sessionId, score, query) {
6699
+ await this.initialize();
6700
+ await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
6701
+ }
6702
+ /**
6703
+ * Evaluate helpfulness of retrievals in a session (called at session end)
6704
+ */
6705
+ async evaluateSessionHelpfulness(sessionId) {
6706
+ await this.initialize();
6707
+ await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
6708
+ }
6709
+ /**
6710
+ * Get most helpful memories ranked by helpfulness score
6711
+ */
6712
+ async getHelpfulMemories(limit = 10) {
6713
+ await this.initialize();
6714
+ return this.sqliteStore.getHelpfulMemories(limit);
6715
+ }
6716
+ /**
6717
+ * Get helpfulness statistics for dashboard
6718
+ */
6719
+ async getHelpfulnessStats() {
6720
+ await this.initialize();
6721
+ return this.sqliteStore.getHelpfulnessStats();
6722
+ }
5162
6723
  /**
5163
6724
  * Mark a consolidated memory as accessed
5164
6725
  */
@@ -5222,6 +6783,44 @@ var MemoryService = class {
5222
6783
  lastConsolidation
5223
6784
  };
5224
6785
  }
6786
+ // ============================================================
6787
+ // Turn Grouping Methods
6788
+ // ============================================================
6789
+ /**
6790
+ * Get events grouped by turn for a session
6791
+ */
6792
+ async getSessionTurns(sessionId, options) {
6793
+ await this.initialize();
6794
+ return this.sqliteStore.getSessionTurns(sessionId, options);
6795
+ }
6796
+ /**
6797
+ * Get all events for a specific turn
6798
+ */
6799
+ async getEventsByTurn(turnId) {
6800
+ await this.initialize();
6801
+ return this.sqliteStore.getEventsByTurn(turnId);
6802
+ }
6803
+ /**
6804
+ * Count total turns for a session
6805
+ */
6806
+ async countSessionTurns(sessionId) {
6807
+ await this.initialize();
6808
+ return this.sqliteStore.countSessionTurns(sessionId);
6809
+ }
6810
+ /**
6811
+ * Backfill turn_ids from metadata for events stored before the migration
6812
+ */
6813
+ async backfillTurnIds() {
6814
+ await this.initialize();
6815
+ return this.sqliteStore.backfillTurnIds();
6816
+ }
6817
+ /**
6818
+ * Delete all events for a session (for force reimport)
6819
+ */
6820
+ async deleteSessionEvents(sessionId) {
6821
+ await this.initialize();
6822
+ return this.sqliteStore.deleteSessionEvents(sessionId);
6823
+ }
5225
6824
  /**
5226
6825
  * Format Endless Mode context for Claude
5227
6826
  */
@@ -5298,7 +6897,7 @@ var MemoryService = class {
5298
6897
  */
5299
6898
  expandPath(p) {
5300
6899
  if (p.startsWith("~")) {
5301
- return path.join(os.homedir(), p.slice(1));
6900
+ return path3.join(os.homedir(), p.slice(1));
5302
6901
  }
5303
6902
  return p;
5304
6903
  }
@@ -5308,10 +6907,11 @@ function getLightweightMemoryService(sessionId) {
5308
6907
  const projectInfo = getSessionProject(sessionId);
5309
6908
  const key = projectInfo ? `lightweight_${projectInfo.projectHash}` : "lightweight_global";
5310
6909
  if (!serviceCache.has(key)) {
5311
- const storagePath = projectInfo ? getProjectStoragePath(projectInfo.projectPath) : path.join(os.homedir(), ".claude-code", "memory");
6910
+ const storagePath = projectInfo ? getProjectStoragePath(projectInfo.projectPath) : path3.join(os.homedir(), ".claude-code", "memory");
5312
6911
  serviceCache.set(key, new MemoryService({
5313
6912
  storagePath,
5314
6913
  projectHash: projectInfo?.projectHash,
6914
+ projectPath: projectInfo?.projectPath,
5315
6915
  lightweightMode: true,
5316
6916
  // Skip embedder/vector/workers
5317
6917
  analyticsEnabled: false,
@@ -5321,19 +6921,63 @@ function getLightweightMemoryService(sessionId) {
5321
6921
  return serviceCache.get(key);
5322
6922
  }
5323
6923
 
6924
+ // src/core/turn-state.ts
6925
+ import * as fs5 from "fs";
6926
+ import * as path4 from "path";
6927
+ import * as os2 from "os";
6928
+ var TURN_STATE_DIR = path4.join(os2.homedir(), ".claude-code", "memory");
6929
+ function getStatePath(sessionId) {
6930
+ return path4.join(TURN_STATE_DIR, `.turn-state-${sessionId}.json`);
6931
+ }
6932
+ function writeTurnState(sessionId, turnId) {
6933
+ try {
6934
+ if (!fs5.existsSync(TURN_STATE_DIR)) {
6935
+ fs5.mkdirSync(TURN_STATE_DIR, { recursive: true });
6936
+ }
6937
+ const state = {
6938
+ turnId,
6939
+ sessionId,
6940
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
6941
+ };
6942
+ const filePath = getStatePath(sessionId);
6943
+ const tempPath = filePath + ".tmp";
6944
+ fs5.writeFileSync(tempPath, JSON.stringify(state));
6945
+ fs5.renameSync(tempPath, filePath);
6946
+ } catch (error) {
6947
+ if (process.env.CLAUDE_MEMORY_DEBUG) {
6948
+ console.error("Failed to write turn state:", error);
6949
+ }
6950
+ }
6951
+ }
6952
+
5324
6953
  // src/hooks/user-prompt-submit.ts
5325
6954
  var MAX_MEMORIES = parseInt(process.env.CLAUDE_MEMORY_MAX_COUNT || "5");
5326
6955
  var MIN_SCORE = parseFloat(process.env.CLAUDE_MEMORY_MIN_SCORE || "0.3");
5327
6956
  var ENABLE_SEARCH = process.env.CLAUDE_MEMORY_SEARCH !== "false";
6957
+ function shouldStorePrompt(prompt) {
6958
+ const trimmed = prompt.trim();
6959
+ if (trimmed.startsWith("/"))
6960
+ return false;
6961
+ if (trimmed.length < 15)
6962
+ return false;
6963
+ if (!/[a-zA-Z가-힣]{2,}/.test(trimmed))
6964
+ return false;
6965
+ return true;
6966
+ }
5328
6967
  async function main() {
5329
6968
  const inputData = await readStdin();
5330
6969
  const input = JSON.parse(inputData);
6970
+ const turnId = randomUUID9();
6971
+ writeTurnState(input.session_id, turnId);
5331
6972
  const memoryService = getLightweightMemoryService(input.session_id);
5332
6973
  try {
5333
- await memoryService.storeUserPrompt(
5334
- input.session_id,
5335
- input.prompt
5336
- );
6974
+ if (shouldStorePrompt(input.prompt)) {
6975
+ await memoryService.storeUserPrompt(
6976
+ input.session_id,
6977
+ input.prompt,
6978
+ { turnId }
6979
+ );
6980
+ }
5337
6981
  let context = "";
5338
6982
  if (ENABLE_SEARCH && input.prompt.length > 10) {
5339
6983
  const results = await memoryService.keywordSearch(input.prompt, {
@@ -5343,6 +6987,17 @@ async function main() {
5343
6987
  if (results.length > 0) {
5344
6988
  const eventIds = results.map((r) => r.event.id);
5345
6989
  await memoryService.incrementMemoryAccess(eventIds);
6990
+ for (const r of results) {
6991
+ try {
6992
+ await memoryService.recordRetrieval(
6993
+ r.event.id,
6994
+ input.session_id,
6995
+ r.score,
6996
+ input.prompt
6997
+ );
6998
+ } catch {
6999
+ }
7000
+ }
5346
7001
  const memories = results.map((r) => {
5347
7002
  const preview = r.event.content.length > 300 ? r.event.content.substring(0, 300) + "..." : r.event.content;
5348
7003
  return `- [${r.event.eventType}] ${preview}`;