claude-memory-layer 1.0.11 → 1.0.13

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 (101) 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 +2389 -286
  29. package/dist/cli/index.js.map +4 -4
  30. package/dist/core/index.js +1017 -132
  31. package/dist/core/index.js.map +4 -4
  32. package/dist/hooks/post-tool-use.js +1347 -202
  33. package/dist/hooks/post-tool-use.js.map +4 -4
  34. package/dist/hooks/session-end.js +1339 -194
  35. package/dist/hooks/session-end.js.map +4 -4
  36. package/dist/hooks/session-start.js +1343 -198
  37. package/dist/hooks/session-start.js.map +4 -4
  38. package/dist/hooks/stop.js +1351 -206
  39. package/dist/hooks/stop.js.map +4 -4
  40. package/dist/hooks/user-prompt-submit.js +1347 -202
  41. package/dist/hooks/user-prompt-submit.js.map +4 -4
  42. package/dist/server/api/index.js +1436 -211
  43. package/dist/server/api/index.js.map +4 -4
  44. package/dist/server/index.js +1445 -220
  45. package/dist/server/index.js.map +4 -4
  46. package/dist/services/memory-service.js +1345 -199
  47. package/dist/services/memory-service.js.map +4 -4
  48. package/dist/ui/app.js +69 -2
  49. package/dist/ui/index.html +8 -0
  50. package/docs/MCP_MEMORY_SERVICE_COMPARATIVE_REVIEW.md +271 -0
  51. package/docs/MEMU_ADOPTION.md +40 -0
  52. package/memory/.claude-plugin/commands/2026-02-25.md +263 -0
  53. package/memory/_index.md +405 -0
  54. package/memory/default/uncategorized/2026-02-25.md +4839 -0
  55. package/memory/specs/20260207-dashboard-upgrade/2026-02-25.md +142 -0
  56. package/memory/specs/citations-system/2026-02-25.md +1121 -0
  57. package/memory/specs/endless-mode/2026-02-25.md +1392 -0
  58. package/memory/specs/entity-edge-model/2026-02-25.md +1263 -0
  59. package/memory/specs/evidence-aligner-v2/2026-02-25.md +1028 -0
  60. package/memory/specs/mcp-desktop-integration/2026-02-25.md +1334 -0
  61. package/memory/specs/post-tool-use-hook/2026-02-25.md +1164 -0
  62. package/memory/specs/private-tags/2026-02-25.md +1057 -0
  63. package/memory/specs/progressive-disclosure/2026-02-25.md +1436 -0
  64. package/memory/specs/task-entity-system/2026-02-25.md +924 -0
  65. package/memory/specs/vector-outbox-v2/2026-02-25.md +1510 -0
  66. package/memory/specs/web-viewer-ui/2026-02-25.md +1709 -0
  67. package/package.json +2 -1
  68. package/scripts/build.ts +6 -0
  69. package/scripts/bump-patch-version.sh +18 -0
  70. package/src/cli/index.ts +281 -2
  71. package/src/core/consolidated-store.ts +63 -1
  72. package/src/core/consolidation-worker.ts +115 -6
  73. package/src/core/event-store.ts +14 -0
  74. package/src/core/index.ts +1 -0
  75. package/src/core/ingest-interceptor.ts +80 -0
  76. package/src/core/markdown-mirror.ts +70 -0
  77. package/src/core/md-mirror.ts +92 -0
  78. package/src/core/mongo-sync-config.ts +165 -0
  79. package/src/core/mongo-sync-worker.ts +381 -0
  80. package/src/core/retriever.ts +540 -150
  81. package/src/core/sqlite-event-store.ts +350 -1
  82. package/src/core/tag-taxonomy.ts +51 -0
  83. package/src/core/types.ts +28 -0
  84. package/src/server/api/health.ts +53 -0
  85. package/src/server/api/index.ts +3 -1
  86. package/src/server/api/stats.ts +46 -1
  87. package/src/services/bootstrap-organizer.ts +443 -0
  88. package/src/services/codex-session-history-importer.ts +474 -0
  89. package/src/services/memory-service.ts +373 -68
  90. package/src/services/session-history-importer.ts +53 -25
  91. package/src/ui/app.js +69 -2
  92. package/src/ui/index.html +8 -0
  93. package/tests/bootstrap-organizer.test.ts +111 -0
  94. package/tests/consolidation-worker.test.ts +75 -0
  95. package/tests/ingest-interceptor.test.ts +38 -0
  96. package/tests/markdown-mirror.test.ts +85 -0
  97. package/tests/md-mirror.test.ts +50 -0
  98. package/tests/retriever-fallback-chain.test.ts +223 -0
  99. package/tests/retriever-strategy-scope.test.ts +97 -0
  100. package/tests/retriever.memu-adoption.test.ts +122 -0
  101. package/tests/sqlite-event-store-replication.test.ts +92 -0
@@ -7,13 +7,13 @@ const __filename = fileURLToPath(import.meta.url);
7
7
  const __dirname = dirname(__filename);
8
8
 
9
9
  // src/hooks/stop.ts
10
- import * as fs4 from "fs";
10
+ import * as fs6 from "fs";
11
11
  import * as readline from "readline";
12
12
 
13
13
  // src/services/memory-service.ts
14
- import * as path from "path";
14
+ import * as path3 from "path";
15
15
  import * as os from "os";
16
- import * as fs2 from "fs";
16
+ import * as fs4 from "fs";
17
17
  import * as crypto2 from "crypto";
18
18
 
19
19
  // src/core/event-store.ts
@@ -71,11 +71,11 @@ function toDate(value) {
71
71
  return new Date(value);
72
72
  return new Date(String(value));
73
73
  }
74
- function createDatabase(path3, options) {
74
+ function createDatabase(path5, options) {
75
75
  if (options?.readOnly) {
76
- return new duckdb.Database(path3, { access_mode: "READ_ONLY" });
76
+ return new duckdb.Database(path5, { access_mode: "READ_ONLY" });
77
77
  }
78
- return new duckdb.Database(path3);
78
+ return new duckdb.Database(path5);
79
79
  }
80
80
  function dbRun(db, sql, params = []) {
81
81
  return new Promise((resolve2, reject) => {
@@ -339,6 +339,17 @@ var EventStore = class {
339
339
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
340
340
  )
341
341
  `);
342
+ await dbRun(this.db, `
343
+ CREATE TABLE IF NOT EXISTS consolidated_rules (
344
+ rule_id VARCHAR PRIMARY KEY,
345
+ rule TEXT NOT NULL,
346
+ topics JSON,
347
+ source_memory_ids JSON,
348
+ source_events JSON,
349
+ confidence FLOAT DEFAULT 0.5,
350
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
351
+ )
352
+ `);
342
353
  await dbRun(this.db, `
343
354
  CREATE TABLE IF NOT EXISTS endless_config (
344
355
  key VARCHAR PRIMARY KEY,
@@ -358,6 +369,7 @@ var EventStore = class {
358
369
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
359
370
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
360
371
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
372
+ await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence DESC)`);
361
373
  await dbRun(this.db, `CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
362
374
  this.initialized = true;
363
375
  }
@@ -747,12 +759,12 @@ import { randomUUID as randomUUID2 } from "crypto";
747
759
  import Database from "better-sqlite3";
748
760
  import * as fs from "fs";
749
761
  import * as nodePath from "path";
750
- function createSQLiteDatabase(path3, options) {
751
- const dir = nodePath.dirname(path3);
762
+ function createSQLiteDatabase(path5, options) {
763
+ const dir = nodePath.dirname(path5);
752
764
  if (!fs.existsSync(dir)) {
753
765
  fs.mkdirSync(dir, { recursive: true });
754
766
  }
755
- const db = new Database(path3, {
767
+ const db = new Database(path5, {
756
768
  readonly: options?.readonly ?? false
757
769
  });
758
770
  if (!options?.readonly && (options?.walMode ?? true)) {
@@ -793,6 +805,64 @@ function toSQLiteTimestamp(date) {
793
805
  return date.toISOString();
794
806
  }
795
807
 
808
+ // src/core/markdown-mirror.ts
809
+ import * as fs2 from "fs/promises";
810
+ import * as path from "path";
811
+ var DEFAULT_NAMESPACE = "default";
812
+ var DEFAULT_CATEGORY = "uncategorized";
813
+ function sanitizeSegment(input, fallback) {
814
+ const raw = String(input ?? "").trim().toLowerCase();
815
+ const safe = raw.normalize("NFKD").replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
816
+ if (!safe || safe === "." || safe === "..")
817
+ return fallback;
818
+ return safe;
819
+ }
820
+ function getCategorySegments(metadata, eventType) {
821
+ const raw = metadata?.categoryPath;
822
+ if (Array.isArray(raw) && raw.length > 0) {
823
+ return raw.map((s) => sanitizeSegment(s, DEFAULT_CATEGORY));
824
+ }
825
+ const single = metadata?.category;
826
+ if (typeof single === "string" && single.trim()) {
827
+ return [sanitizeSegment(single, DEFAULT_CATEGORY)];
828
+ }
829
+ return [sanitizeSegment(eventType, DEFAULT_CATEGORY)];
830
+ }
831
+ function buildMirrorPath(rootDir, event) {
832
+ const metadata = event.metadata;
833
+ const namespace = sanitizeSegment(metadata?.namespace, DEFAULT_NAMESPACE);
834
+ const categories = getCategorySegments(metadata, event.eventType);
835
+ const d = event.timestamp;
836
+ const yyyy = d.getFullYear();
837
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
838
+ const dd = String(d.getDate()).padStart(2, "0");
839
+ return path.join(rootDir, "memory", namespace, ...categories, `${yyyy}-${mm}-${dd}.md`);
840
+ }
841
+ function formatMirrorEntry(event) {
842
+ const category = Array.isArray(event.metadata?.categoryPath) ? event.metadata.categoryPath.join("/") : String(event.metadata?.category ?? event.eventType);
843
+ return [
844
+ "",
845
+ `- ts: ${event.timestamp.toISOString()}`,
846
+ ` id: ${event.id}`,
847
+ ` type: ${event.eventType}`,
848
+ ` session: ${event.sessionId}`,
849
+ ` category: ${category}`,
850
+ " content: |",
851
+ ...event.content.split("\n").map((line) => ` ${line}`)
852
+ ].join("\n") + "\n";
853
+ }
854
+ var MarkdownMirror = class {
855
+ constructor(rootDir) {
856
+ this.rootDir = rootDir;
857
+ }
858
+ async append(event) {
859
+ const outPath = buildMirrorPath(this.rootDir, event);
860
+ await fs2.mkdir(path.dirname(outPath), { recursive: true });
861
+ await fs2.appendFile(outPath, formatMirrorEntry(event), "utf8");
862
+ return outPath;
863
+ }
864
+ };
865
+
796
866
  // src/core/sqlite-event-store.ts
797
867
  var SQLiteEventStore = class {
798
868
  constructor(dbPath, options) {
@@ -802,10 +872,12 @@ var SQLiteEventStore = class {
802
872
  readonly: this.readOnly,
803
873
  walMode: !this.readOnly
804
874
  });
875
+ this.markdownMirror = this.readOnly || !options?.markdownMirrorRoot ? null : new MarkdownMirror(options.markdownMirrorRoot);
805
876
  }
806
877
  db;
807
878
  initialized = false;
808
879
  readOnly;
880
+ markdownMirror;
809
881
  /**
810
882
  * Initialize database schema
811
883
  */
@@ -1012,6 +1084,17 @@ var SQLiteEventStore = class {
1012
1084
  created_at TEXT DEFAULT (datetime('now'))
1013
1085
  );
1014
1086
 
1087
+ -- Consolidated Rules table (long-term stable memory)
1088
+ CREATE TABLE IF NOT EXISTS consolidated_rules (
1089
+ rule_id TEXT PRIMARY KEY,
1090
+ rule TEXT NOT NULL,
1091
+ topics TEXT,
1092
+ source_memory_ids TEXT,
1093
+ source_events TEXT,
1094
+ confidence REAL DEFAULT 0.5,
1095
+ created_at TEXT DEFAULT (datetime('now'))
1096
+ );
1097
+
1015
1098
  -- Endless Mode Config table
1016
1099
  CREATE TABLE IF NOT EXISTS endless_config (
1017
1100
  key TEXT PRIMARY KEY,
@@ -1036,6 +1119,24 @@ var SQLiteEventStore = class {
1036
1119
  measured_at TEXT
1037
1120
  );
1038
1121
 
1122
+ -- Retrieval trace log (query -> candidates -> selected for context)
1123
+ CREATE TABLE IF NOT EXISTS retrieval_traces (
1124
+ trace_id TEXT PRIMARY KEY,
1125
+ session_id TEXT,
1126
+ project_hash TEXT,
1127
+ query_text TEXT NOT NULL,
1128
+ strategy TEXT,
1129
+ candidate_event_ids TEXT,
1130
+ selected_event_ids TEXT,
1131
+ candidate_details_json TEXT,
1132
+ selected_details_json TEXT,
1133
+ candidate_count INTEGER DEFAULT 0,
1134
+ selected_count INTEGER DEFAULT 0,
1135
+ confidence TEXT,
1136
+ fallback_trace TEXT,
1137
+ created_at TEXT DEFAULT (datetime('now'))
1138
+ );
1139
+
1039
1140
  -- Sync position tracking (for SQLite -> DuckDB sync)
1040
1141
  CREATE TABLE IF NOT EXISTS sync_positions (
1041
1142
  target_name TEXT PRIMARY KEY,
@@ -1060,10 +1161,14 @@ var SQLiteEventStore = class {
1060
1161
  CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score);
1061
1162
  CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
1062
1163
  CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
1164
+ CREATE INDEX IF NOT EXISTS idx_consolidated_rules_confidence ON consolidated_rules(confidence);
1063
1165
  CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
1064
1166
  CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
1065
1167
  CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
1066
1168
  CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
1169
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_created_at ON retrieval_traces(created_at DESC);
1170
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_project_hash ON retrieval_traces(project_hash);
1171
+ CREATE INDEX IF NOT EXISTS idx_retrieval_traces_session_id ON retrieval_traces(session_id);
1067
1172
 
1068
1173
  -- FTS5 Full-Text Search for fast keyword search
1069
1174
  CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
@@ -1087,6 +1192,14 @@ var SQLiteEventStore = class {
1087
1192
  INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
1088
1193
  END;
1089
1194
  `);
1195
+ try {
1196
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN selected_details_json TEXT;`);
1197
+ } catch {
1198
+ }
1199
+ try {
1200
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN candidate_details_json TEXT;`);
1201
+ } catch {
1202
+ }
1090
1203
  const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
1091
1204
  const columnNames = tableInfo.map((col) => col.name);
1092
1205
  if (!columnNames.includes("access_count")) {
@@ -1186,6 +1299,21 @@ var SQLiteEventStore = class {
1186
1299
  insertLevel.run(id);
1187
1300
  });
1188
1301
  transaction();
1302
+ if (this.markdownMirror) {
1303
+ const event = {
1304
+ id,
1305
+ eventType: input.eventType,
1306
+ sessionId: input.sessionId,
1307
+ timestamp: input.timestamp,
1308
+ content: input.content,
1309
+ canonicalKey,
1310
+ dedupeKey,
1311
+ metadata
1312
+ };
1313
+ this.markdownMirror.append(event).catch((err) => {
1314
+ console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
1315
+ });
1316
+ }
1189
1317
  return { success: true, eventId: id, isDuplicate: false };
1190
1318
  } catch (error) {
1191
1319
  return {
@@ -1244,6 +1372,92 @@ var SQLiteEventStore = class {
1244
1372
  );
1245
1373
  return rows.map(this.rowToEvent);
1246
1374
  }
1375
+ /**
1376
+ * Get events since a SQLite rowid (for robust incremental replication).
1377
+ * Rowid is monotonic for append-only tables, independent of client timestamps.
1378
+ */
1379
+ async getEventsSinceRowid(lastRowid, limit = 1e3) {
1380
+ await this.initialize();
1381
+ const rows = sqliteAll(
1382
+ this.db,
1383
+ `SELECT rowid as _rowid, * FROM events WHERE rowid > ? ORDER BY rowid ASC LIMIT ?`,
1384
+ [lastRowid, limit]
1385
+ );
1386
+ return rows.map((row) => ({
1387
+ rowid: row._rowid,
1388
+ event: this.rowToEvent(row)
1389
+ }));
1390
+ }
1391
+ /**
1392
+ * Import events with fixed IDs (used for cross-machine replication).
1393
+ * Idempotent: skips if event id or dedupeKey already exists.
1394
+ *
1395
+ * NOTE: This bypasses the append() id generation to preserve stable IDs.
1396
+ */
1397
+ async importEvents(events) {
1398
+ if (events.length === 0)
1399
+ return { inserted: 0, skipped: 0 };
1400
+ if (this.readOnly)
1401
+ return { inserted: 0, skipped: events.length };
1402
+ await this.initialize();
1403
+ const getById = this.db.prepare(`SELECT id FROM events WHERE id = ?`);
1404
+ const getByDedupe = this.db.prepare(`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`);
1405
+ const insertEvent = this.db.prepare(`
1406
+ INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
1407
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1408
+ `);
1409
+ const insertDedup = this.db.prepare(`
1410
+ INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
1411
+ `);
1412
+ const insertLevel = this.db.prepare(`
1413
+ INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')
1414
+ `);
1415
+ let inserted = 0;
1416
+ let skipped = 0;
1417
+ const insertedEvents = [];
1418
+ const tx = this.db.transaction((batch) => {
1419
+ for (const ev of batch) {
1420
+ const existingById = getById.get(ev.id);
1421
+ if (existingById) {
1422
+ skipped++;
1423
+ continue;
1424
+ }
1425
+ const canonicalKey = ev.canonicalKey || makeCanonicalKey(ev.content);
1426
+ const dedupeKey = ev.dedupeKey || makeDedupeKey(ev.content, ev.sessionId);
1427
+ const existingByDedupe = getByDedupe.get(dedupeKey);
1428
+ if (existingByDedupe) {
1429
+ skipped++;
1430
+ continue;
1431
+ }
1432
+ const metadata = ev.metadata || {};
1433
+ const turnId = metadata.turnId;
1434
+ insertEvent.run(
1435
+ ev.id,
1436
+ ev.eventType,
1437
+ ev.sessionId,
1438
+ toSQLiteTimestamp(ev.timestamp),
1439
+ ev.content,
1440
+ canonicalKey,
1441
+ dedupeKey,
1442
+ JSON.stringify(metadata),
1443
+ turnId ?? null
1444
+ );
1445
+ insertDedup.run(dedupeKey, ev.id);
1446
+ insertLevel.run(ev.id);
1447
+ inserted++;
1448
+ insertedEvents.push(ev);
1449
+ }
1450
+ });
1451
+ tx(events);
1452
+ if (this.markdownMirror && insertedEvents.length > 0) {
1453
+ for (const ev of insertedEvents) {
1454
+ this.markdownMirror.append(ev).catch((err) => {
1455
+ console.warn("[SQLiteEventStore] markdown mirror append failed:", err);
1456
+ });
1457
+ }
1458
+ }
1459
+ return { inserted, skipped };
1460
+ }
1247
1461
  /**
1248
1462
  * Create or update session
1249
1463
  */
@@ -1406,6 +1620,35 @@ var SQLiteEventStore = class {
1406
1620
  [error, ...ids]
1407
1621
  );
1408
1622
  }
1623
+ /**
1624
+ * Get embedding/vector outbox health statistics
1625
+ */
1626
+ async getOutboxStats() {
1627
+ await this.initialize();
1628
+ const embeddingRows = sqliteAll(
1629
+ this.db,
1630
+ `SELECT status, COUNT(*) as count FROM embedding_outbox GROUP BY status`
1631
+ );
1632
+ const vectorRows = sqliteAll(
1633
+ this.db,
1634
+ `SELECT status, COUNT(*) as count FROM vector_outbox GROUP BY status`
1635
+ );
1636
+ const fromRows = (rows) => {
1637
+ const out = { pending: 0, processing: 0, failed: 0, total: 0 };
1638
+ for (const row of rows) {
1639
+ const key = row.status;
1640
+ if (key === "pending" || key === "processing" || key === "failed") {
1641
+ out[key] += row.count;
1642
+ }
1643
+ out.total += row.count;
1644
+ }
1645
+ return out;
1646
+ };
1647
+ return {
1648
+ embedding: fromRows(embeddingRows),
1649
+ vector: fromRows(vectorRows)
1650
+ };
1651
+ }
1409
1652
  /**
1410
1653
  * Update memory level
1411
1654
  */
@@ -1764,6 +2007,79 @@ var SQLiteEventStore = class {
1764
2007
  getDatabase() {
1765
2008
  return this.db;
1766
2009
  }
2010
+ async recordRetrievalTrace(input) {
2011
+ await this.initialize();
2012
+ const traceId = randomUUID2();
2013
+ sqliteRun(
2014
+ this.db,
2015
+ `INSERT INTO retrieval_traces (
2016
+ trace_id, session_id, project_hash, query_text, strategy,
2017
+ candidate_event_ids, selected_event_ids, candidate_details_json, selected_details_json,
2018
+ candidate_count, selected_count, confidence, fallback_trace
2019
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2020
+ [
2021
+ traceId,
2022
+ input.sessionId || null,
2023
+ input.projectHash || null,
2024
+ input.queryText,
2025
+ input.strategy || null,
2026
+ JSON.stringify(input.candidateEventIds || []),
2027
+ JSON.stringify(input.selectedEventIds || []),
2028
+ JSON.stringify(input.candidateDetails || []),
2029
+ JSON.stringify(input.selectedDetails || []),
2030
+ (input.candidateEventIds || []).length,
2031
+ (input.selectedEventIds || []).length,
2032
+ input.confidence || null,
2033
+ JSON.stringify(input.fallbackTrace || [])
2034
+ ]
2035
+ );
2036
+ }
2037
+ async getRecentRetrievalTraces(limit = 50) {
2038
+ await this.initialize();
2039
+ const rows = sqliteAll(
2040
+ this.db,
2041
+ `SELECT * FROM retrieval_traces ORDER BY created_at DESC LIMIT ?`,
2042
+ [limit]
2043
+ );
2044
+ return rows.map((row) => ({
2045
+ traceId: row.trace_id,
2046
+ sessionId: row.session_id || void 0,
2047
+ projectHash: row.project_hash || void 0,
2048
+ queryText: row.query_text,
2049
+ strategy: row.strategy || void 0,
2050
+ candidateEventIds: row.candidate_event_ids ? JSON.parse(row.candidate_event_ids) : [],
2051
+ selectedEventIds: row.selected_event_ids ? JSON.parse(row.selected_event_ids) : [],
2052
+ candidateDetails: row.candidate_details_json ? JSON.parse(row.candidate_details_json) : [],
2053
+ selectedDetails: row.selected_details_json ? JSON.parse(row.selected_details_json) : [],
2054
+ candidateCount: Number(row.candidate_count || 0),
2055
+ selectedCount: Number(row.selected_count || 0),
2056
+ confidence: row.confidence || void 0,
2057
+ fallbackTrace: row.fallback_trace ? JSON.parse(row.fallback_trace) : [],
2058
+ createdAt: toDateFromSQLite(row.created_at)
2059
+ }));
2060
+ }
2061
+ async getRetrievalTraceStats() {
2062
+ await this.initialize();
2063
+ const row = sqliteGet(
2064
+ this.db,
2065
+ `SELECT
2066
+ COUNT(*) as total_queries,
2067
+ AVG(candidate_count) as avg_candidate_count,
2068
+ AVG(selected_count) as avg_selected_count,
2069
+ CASE
2070
+ WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
2071
+ ELSE 0
2072
+ END as selection_rate
2073
+ FROM retrieval_traces`,
2074
+ []
2075
+ );
2076
+ return {
2077
+ totalQueries: Number(row?.total_queries || 0),
2078
+ avgCandidateCount: Number(row?.avg_candidate_count || 0),
2079
+ avgSelectedCount: Number(row?.avg_selected_count || 0),
2080
+ selectionRate: Number(row?.selection_rate || 0)
2081
+ };
2082
+ }
1767
2083
  /**
1768
2084
  * Close database connection
1769
2085
  */
@@ -2625,7 +2941,20 @@ var DEFAULT_OPTIONS = {
2625
2941
  topK: 5,
2626
2942
  minScore: 0.7,
2627
2943
  maxTokens: 2e3,
2628
- includeSessionContext: true
2944
+ includeSessionContext: true,
2945
+ strategy: "auto",
2946
+ rerankWithKeyword: true,
2947
+ decayPolicy: {
2948
+ enabled: true,
2949
+ windowDays: 30,
2950
+ maxPenalty: 0.15
2951
+ },
2952
+ graphHop: {
2953
+ enabled: true,
2954
+ maxHops: 1,
2955
+ hopPenalty: 0.08
2956
+ },
2957
+ projectScopeMode: "global"
2629
2958
  };
2630
2959
  var Retriever = class {
2631
2960
  eventStore;
@@ -2635,6 +2964,7 @@ var Retriever = class {
2635
2964
  sharedStore;
2636
2965
  sharedVectorStore;
2637
2966
  graduation;
2967
+ queryRewriter;
2638
2968
  constructor(eventStore, vectorStore, embedder, matcher, sharedOptions) {
2639
2969
  this.eventStore = eventStore;
2640
2970
  this.vectorStore = vectorStore;
@@ -2643,47 +2973,105 @@ var Retriever = class {
2643
2973
  this.sharedStore = sharedOptions?.sharedStore;
2644
2974
  this.sharedVectorStore = sharedOptions?.sharedVectorStore;
2645
2975
  }
2646
- /**
2647
- * Set graduation pipeline for access tracking
2648
- */
2649
2976
  setGraduationPipeline(graduation) {
2650
2977
  this.graduation = graduation;
2651
2978
  }
2652
- /**
2653
- * Set shared stores after construction
2654
- */
2655
2979
  setSharedStores(sharedStore, sharedVectorStore) {
2656
2980
  this.sharedStore = sharedStore;
2657
2981
  this.sharedVectorStore = sharedVectorStore;
2658
2982
  }
2659
- /**
2660
- * Retrieve relevant memories for a query
2661
- */
2983
+ setQueryRewriter(rewriter) {
2984
+ this.queryRewriter = rewriter;
2985
+ }
2662
2986
  async retrieve(query, options = {}) {
2663
2987
  const opts = { ...DEFAULT_OPTIONS, ...options };
2664
- const queryEmbedding = await this.embedder.embed(query);
2665
- const searchResults = await this.vectorStore.search(queryEmbedding.vector, {
2666
- limit: opts.topK * 2,
2667
- // Get extra for filtering
2988
+ const sessionFilter = opts.scope?.sessionId ?? opts.sessionId;
2989
+ const fallbackTrace = [];
2990
+ const fallbackEnabled = (opts.strategy ?? "auto") === "auto";
2991
+ const primaryStrategy = opts.strategy === "auto" ? "fast" : opts.strategy || "fast";
2992
+ let current = await this.runStage(query, {
2993
+ strategy: primaryStrategy,
2994
+ topK: opts.topK,
2668
2995
  minScore: opts.minScore,
2669
- sessionId: opts.sessionId
2996
+ sessionId: sessionFilter,
2997
+ scope: opts.scope,
2998
+ rerankWithKeyword: opts.rerankWithKeyword !== false,
2999
+ rerankWeights: opts.rerankWeights,
3000
+ decayPolicy: opts.decayPolicy,
3001
+ intentRewrite: opts.intentRewrite === true,
3002
+ graphHop: opts.graphHop,
3003
+ projectScopeMode: opts.projectScopeMode,
3004
+ projectHash: opts.projectHash,
3005
+ allowedProjectHashes: opts.allowedProjectHashes
2670
3006
  });
2671
- const matchResult = this.matcher.matchSearchResults(
2672
- searchResults,
2673
- (eventId) => this.getEventAgeDays(eventId)
2674
- );
2675
- const memories = await this.enrichResults(searchResults.slice(0, opts.topK), opts);
3007
+ fallbackTrace.push(`stage:primary:${primaryStrategy}`);
3008
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results) && primaryStrategy !== "deep") {
3009
+ current = await this.runStage(query, {
3010
+ strategy: "deep",
3011
+ topK: opts.topK,
3012
+ minScore: opts.minScore,
3013
+ sessionId: sessionFilter,
3014
+ scope: opts.scope,
3015
+ rerankWithKeyword: opts.rerankWithKeyword !== false,
3016
+ rerankWeights: opts.rerankWeights,
3017
+ decayPolicy: opts.decayPolicy,
3018
+ graphHop: opts.graphHop,
3019
+ projectScopeMode: opts.projectScopeMode,
3020
+ projectHash: opts.projectHash,
3021
+ allowedProjectHashes: opts.allowedProjectHashes
3022
+ });
3023
+ fallbackTrace.push("fallback:deep");
3024
+ }
3025
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
3026
+ current = await this.runStage(query, {
3027
+ strategy: "deep",
3028
+ topK: opts.topK,
3029
+ minScore: Math.max(0.5, opts.minScore - 0.15),
3030
+ sessionId: void 0,
3031
+ scope: void 0,
3032
+ rerankWithKeyword: true,
3033
+ rerankWeights: opts.rerankWeights,
3034
+ decayPolicy: opts.decayPolicy,
3035
+ graphHop: opts.graphHop,
3036
+ projectScopeMode: opts.projectScopeMode,
3037
+ projectHash: opts.projectHash,
3038
+ allowedProjectHashes: opts.allowedProjectHashes
3039
+ });
3040
+ fallbackTrace.push("fallback:scope-expanded");
3041
+ }
3042
+ if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
3043
+ const summary = await this.buildSummaryFallback(query, opts.topK);
3044
+ current = {
3045
+ results: summary,
3046
+ candidateResults: summary,
3047
+ matchResult: this.matcher.matchSearchResults(summary, () => 0)
3048
+ };
3049
+ fallbackTrace.push("fallback:summary");
3050
+ }
3051
+ const memories = await this.enrichResults(current.results.slice(0, opts.topK), opts);
2676
3052
  const context = this.buildContext(memories, opts.maxTokens);
2677
3053
  return {
2678
3054
  memories,
2679
- matchResult,
3055
+ matchResult: current.matchResult,
2680
3056
  totalTokens: this.estimateTokens(context),
2681
- context
3057
+ context,
3058
+ fallbackTrace,
3059
+ selectedDebug: current.results.slice(0, opts.topK).map((r) => ({
3060
+ eventId: r.eventId,
3061
+ score: r.score,
3062
+ semanticScore: r.semanticScore,
3063
+ lexicalScore: r.lexicalScore,
3064
+ recencyScore: r.recencyScore
3065
+ })),
3066
+ candidateDebug: (current.candidateResults || []).slice(0, Math.max(opts.topK * 3, 20)).map((r) => ({
3067
+ eventId: r.eventId,
3068
+ score: r.score,
3069
+ semanticScore: r.semanticScore,
3070
+ lexicalScore: r.lexicalScore,
3071
+ recencyScore: r.recencyScore
3072
+ }))
2682
3073
  };
2683
3074
  }
2684
- /**
2685
- * Retrieve with unified search (project + shared)
2686
- */
2687
3075
  async retrieveUnified(query, options = {}) {
2688
3076
  const projectResult = await this.retrieve(query, options);
2689
3077
  if (!options.includeShared || !this.sharedStore || !this.sharedVectorStore) {
@@ -2691,22 +3079,19 @@ var Retriever = class {
2691
3079
  }
2692
3080
  try {
2693
3081
  const queryEmbedding = await this.embedder.embed(query);
2694
- const sharedVectorResults = await this.sharedVectorStore.search(
2695
- queryEmbedding.vector,
2696
- {
2697
- limit: options.topK || 5,
2698
- minScore: options.minScore || 0.7,
2699
- excludeProjectHash: options.projectHash
2700
- }
2701
- );
3082
+ const sharedVectorResults = await this.sharedVectorStore.search(queryEmbedding.vector, {
3083
+ limit: options.topK || 5,
3084
+ minScore: options.minScore || 0.7,
3085
+ excludeProjectHash: options.projectHash
3086
+ });
2702
3087
  const sharedMemories = [];
2703
3088
  for (const result of sharedVectorResults) {
2704
3089
  const entry = await this.sharedStore.get(result.entryId);
2705
- if (entry) {
2706
- if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
2707
- sharedMemories.push(entry);
2708
- await this.sharedStore.recordUsage(entry.entryId);
2709
- }
3090
+ if (!entry)
3091
+ continue;
3092
+ if (!options.projectHash || entry.sourceProjectHash !== options.projectHash) {
3093
+ sharedMemories.push(entry);
3094
+ await this.sharedStore.recordUsage(entry.entryId);
2710
3095
  }
2711
3096
  }
2712
3097
  const unifiedContext = this.buildUnifiedContext(projectResult, sharedMemories);
@@ -2721,50 +3106,243 @@ var Retriever = class {
2721
3106
  return projectResult;
2722
3107
  }
2723
3108
  }
2724
- /**
2725
- * Build unified context combining project and shared memories
2726
- */
2727
- buildUnifiedContext(projectResult, sharedMemories) {
2728
- let context = projectResult.context;
2729
- if (sharedMemories.length > 0) {
2730
- context += "\n\n## Cross-Project Knowledge\n\n";
2731
- for (const memory of sharedMemories.slice(0, 3)) {
2732
- context += `### ${memory.title}
2733
- `;
2734
- if (memory.symptoms.length > 0) {
2735
- context += `**Symptoms:** ${memory.symptoms.join(", ")}
2736
- `;
2737
- }
2738
- context += `**Root Cause:** ${memory.rootCause}
2739
- `;
2740
- context += `**Solution:** ${memory.solution}
2741
- `;
2742
- if (memory.technologies && memory.technologies.length > 0) {
2743
- context += `**Technologies:** ${memory.technologies.join(", ")}
2744
- `;
3109
+ async runStage(query, input) {
3110
+ let initialResults = await this.searchByStrategy(query, {
3111
+ strategy: input.strategy,
3112
+ topK: input.topK,
3113
+ minScore: input.minScore,
3114
+ sessionId: input.sessionId
3115
+ });
3116
+ if (input.intentRewrite && input.strategy === "deep" && this.queryRewriter) {
3117
+ const rewritten = (await this.queryRewriter(query))?.trim();
3118
+ if (rewritten && rewritten !== query) {
3119
+ const rewrittenResults = await this.searchByStrategy(rewritten, {
3120
+ strategy: "deep",
3121
+ topK: input.topK,
3122
+ minScore: Math.max(0.5, input.minScore - 0.1),
3123
+ sessionId: input.sessionId
3124
+ });
3125
+ initialResults = this.mergeResults(initialResults, rewrittenResults, input.topK * 3);
3126
+ }
3127
+ }
3128
+ const expandedResults = input.graphHop?.enabled === false ? initialResults : await this.expandGraphHops(initialResults, {
3129
+ maxHops: Math.max(1, input.graphHop?.maxHops ?? 1),
3130
+ hopPenalty: Math.max(0, input.graphHop?.hopPenalty ?? 0.08),
3131
+ limit: input.topK * 4
3132
+ });
3133
+ const rerankedResults = input.rerankWithKeyword ? this.rerankByKeywordOverlap(expandedResults, query, input.rerankWeights, input.decayPolicy) : expandedResults;
3134
+ const filtered = await this.applyScopeFilters(rerankedResults, {
3135
+ scope: input.scope,
3136
+ projectScopeMode: input.projectScopeMode,
3137
+ projectHash: input.projectHash,
3138
+ allowedProjectHashes: input.allowedProjectHashes
3139
+ });
3140
+ const top = filtered.slice(0, input.topK);
3141
+ const matchResult = this.matcher.matchSearchResults(top, () => 0);
3142
+ return { results: top, candidateResults: filtered, matchResult };
3143
+ }
3144
+ mergeResults(primary, secondary, limit) {
3145
+ const byId = /* @__PURE__ */ new Map();
3146
+ for (const row of primary)
3147
+ byId.set(row.eventId, row);
3148
+ for (const row of secondary) {
3149
+ const prev = byId.get(row.eventId);
3150
+ if (!prev || row.score > prev.score) {
3151
+ byId.set(row.eventId, row);
3152
+ }
3153
+ }
3154
+ return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, limit);
3155
+ }
3156
+ async expandGraphHops(seeds, opts) {
3157
+ const byId = /* @__PURE__ */ new Map();
3158
+ for (const s of seeds)
3159
+ byId.set(s.eventId, s);
3160
+ let frontier = seeds.map((s) => ({ row: s, hop: 0 }));
3161
+ for (let hop = 1; hop <= opts.maxHops; hop += 1) {
3162
+ const next = [];
3163
+ for (const f of frontier) {
3164
+ const ev = await this.eventStore.getEvent(f.row.eventId);
3165
+ if (!ev)
3166
+ continue;
3167
+ const rel = ev.metadata?.relatedEventIds ?? [];
3168
+ const relatedIds = Array.isArray(rel) ? rel.filter((x) => typeof x === "string") : [];
3169
+ for (const rid of relatedIds) {
3170
+ if (byId.has(rid))
3171
+ continue;
3172
+ const target = await this.eventStore.getEvent(rid);
3173
+ if (!target)
3174
+ continue;
3175
+ const score = Math.max(0, f.row.score - opts.hopPenalty * hop);
3176
+ const row = {
3177
+ id: `hop-${hop}-${rid}`,
3178
+ eventId: target.id,
3179
+ content: target.content,
3180
+ score,
3181
+ sessionId: target.sessionId,
3182
+ eventType: target.eventType,
3183
+ timestamp: target.timestamp.toISOString()
3184
+ };
3185
+ byId.set(row.eventId, row);
3186
+ next.push({ row, hop });
3187
+ if (byId.size >= opts.limit)
3188
+ break;
2745
3189
  }
2746
- context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
2747
-
2748
- `;
3190
+ if (byId.size >= opts.limit)
3191
+ break;
2749
3192
  }
3193
+ frontier = next;
3194
+ if (frontier.length === 0 || byId.size >= opts.limit)
3195
+ break;
2750
3196
  }
2751
- return context;
3197
+ return [...byId.values()].sort((a, b) => b.score - a.score).slice(0, opts.limit);
3198
+ }
3199
+ shouldFallback(matchResult, results) {
3200
+ if (results.length === 0)
3201
+ return true;
3202
+ if (matchResult.confidence === "none")
3203
+ return true;
3204
+ return false;
3205
+ }
3206
+ async buildSummaryFallback(query, topK) {
3207
+ const recent = await this.eventStore.getRecentEvents(Math.max(topK * 6, 20));
3208
+ const q = this.tokenize(query);
3209
+ 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) => ({
3210
+ id: `summary-${row.e.id}`,
3211
+ eventId: row.e.id,
3212
+ content: row.e.content,
3213
+ score: Math.max(0.25, 0.6 - idx * 0.05),
3214
+ sessionId: row.e.sessionId,
3215
+ eventType: row.e.eventType,
3216
+ timestamp: row.e.timestamp.toISOString()
3217
+ }));
3218
+ return ranked;
3219
+ }
3220
+ async searchByStrategy(query, input) {
3221
+ const strategy = input.strategy === "auto" ? "deep" : input.strategy;
3222
+ if (strategy === "fast") {
3223
+ const keyword = await this.searchByKeyword(query, {
3224
+ limit: Math.max(5, input.topK * 3),
3225
+ sessionId: input.sessionId
3226
+ });
3227
+ return keyword;
3228
+ }
3229
+ const queryEmbedding = await this.embedder.embed(query);
3230
+ return this.vectorStore.search(queryEmbedding.vector, {
3231
+ limit: Math.max(5, input.topK * 3),
3232
+ minScore: input.minScore,
3233
+ sessionId: input.sessionId
3234
+ });
3235
+ }
3236
+ async searchByKeyword(query, input) {
3237
+ if (this.eventStore.keywordSearch) {
3238
+ const rows = await this.eventStore.keywordSearch(query, input.limit);
3239
+ const filtered2 = input.sessionId ? rows.filter((r) => r.event.sessionId === input.sessionId) : rows;
3240
+ return filtered2.map((row, idx) => ({
3241
+ id: `kw-${row.event.id}`,
3242
+ eventId: row.event.id,
3243
+ content: row.event.content,
3244
+ score: Math.max(0.4, 1 - idx * 0.04),
3245
+ sessionId: row.event.sessionId,
3246
+ eventType: row.event.eventType,
3247
+ timestamp: row.event.timestamp.toISOString()
3248
+ }));
3249
+ }
3250
+ const recent = await this.eventStore.getRecentEvents(input.limit * 4);
3251
+ const tokens = this.tokenize(query);
3252
+ 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);
3253
+ return filtered.map((row, idx) => ({
3254
+ id: `kw-fallback-${row.e.id}`,
3255
+ eventId: row.e.id,
3256
+ content: row.e.content,
3257
+ score: Math.max(0.3, 0.9 - idx * 0.05),
3258
+ sessionId: row.e.sessionId,
3259
+ eventType: row.e.eventType,
3260
+ timestamp: row.e.timestamp.toISOString()
3261
+ }));
3262
+ }
3263
+ rerankByKeywordOverlap(results, query, weights, decayPolicy) {
3264
+ const q = this.tokenize(query);
3265
+ const now = Date.now();
3266
+ const sw = Math.max(0, weights?.semantic ?? 0.7);
3267
+ const lw = Math.max(0, weights?.lexical ?? 0.2);
3268
+ const rw = Math.max(0, weights?.recency ?? 0.1);
3269
+ const total = sw + lw + rw || 1;
3270
+ const decayEnabled = decayPolicy?.enabled !== false;
3271
+ const decayWindow = Math.max(1, decayPolicy?.windowDays ?? 30);
3272
+ const decayMaxPenalty = Math.max(0, decayPolicy?.maxPenalty ?? 0.15);
3273
+ return [...results].map((r) => {
3274
+ const overlap = this.keywordOverlap(q, this.tokenize(r.content));
3275
+ const recencyDays = Math.max(0, (now - new Date(r.timestamp).getTime()) / (1e3 * 60 * 60 * 24));
3276
+ const recency = Math.max(0, 1 - recencyDays / decayWindow);
3277
+ let blended = (r.score * sw + overlap * lw + recency * rw) / total;
3278
+ if (decayEnabled && recencyDays > decayWindow && overlap < 0.5) {
3279
+ const ageFactor = Math.min(1, (recencyDays - decayWindow) / decayWindow);
3280
+ blended -= decayMaxPenalty * ageFactor;
3281
+ }
3282
+ return { ...r, score: Math.max(0, blended), semanticScore: r.score, lexicalScore: overlap, recencyScore: recency };
3283
+ }).sort((a, b) => b.score - a.score);
3284
+ }
3285
+ async applyScopeFilters(results, options) {
3286
+ const scope = options?.scope;
3287
+ const projectScopeMode = options?.projectScopeMode ?? "global";
3288
+ const allowedProjectHashes = new Set(
3289
+ [options?.projectHash, ...options?.allowedProjectHashes || []].filter(
3290
+ (value) => typeof value === "string" && value.length > 0
3291
+ )
3292
+ );
3293
+ if (!scope && projectScopeMode === "global")
3294
+ return results;
3295
+ const normalizedIncludes = (scope?.contentIncludes || []).map((s) => s.toLowerCase());
3296
+ const filtered = [];
3297
+ for (const result of results) {
3298
+ if (scope?.sessionId && result.sessionId !== scope.sessionId)
3299
+ continue;
3300
+ if (scope?.sessionIdPrefix && !result.sessionId.startsWith(scope.sessionIdPrefix))
3301
+ continue;
3302
+ if (scope?.eventTypes && scope.eventTypes.length > 0 && !scope.eventTypes.includes(result.eventType))
3303
+ continue;
3304
+ const event = await this.eventStore.getEvent(result.eventId);
3305
+ if (!event)
3306
+ continue;
3307
+ if (scope?.canonicalKeyPrefix && !event.canonicalKey.startsWith(scope.canonicalKeyPrefix))
3308
+ continue;
3309
+ if (normalizedIncludes.length > 0) {
3310
+ const lc = event.content.toLowerCase();
3311
+ if (!normalizedIncludes.some((needle) => lc.includes(needle)))
3312
+ continue;
3313
+ }
3314
+ if (scope?.metadata && !this.matchesMetadataScope(event.metadata, scope.metadata))
3315
+ continue;
3316
+ const projectHash = this.extractProjectHash(event.metadata);
3317
+ filtered.push({ result, projectHash });
3318
+ }
3319
+ if (projectScopeMode === "global" || allowedProjectHashes.size === 0) {
3320
+ return filtered.map((x) => x.result);
3321
+ }
3322
+ const projectMatched = filtered.filter((x) => x.projectHash && allowedProjectHashes.has(x.projectHash));
3323
+ if (projectScopeMode === "strict") {
3324
+ return projectMatched.map((x) => x.result);
3325
+ }
3326
+ return (projectMatched.length > 0 ? projectMatched : filtered).map((x) => x.result);
3327
+ }
3328
+ extractProjectHash(metadata) {
3329
+ if (!metadata || typeof metadata !== "object")
3330
+ return void 0;
3331
+ const scope = metadata.scope;
3332
+ if (!scope || typeof scope !== "object")
3333
+ return void 0;
3334
+ const project = scope.project;
3335
+ if (!project || typeof project !== "object")
3336
+ return void 0;
3337
+ const hash = project.hash;
3338
+ return typeof hash === "string" && hash.length > 0 ? hash : void 0;
2752
3339
  }
2753
- /**
2754
- * Retrieve memories from a specific session
2755
- */
2756
3340
  async retrieveFromSession(sessionId) {
2757
3341
  return this.eventStore.getSessionEvents(sessionId);
2758
3342
  }
2759
- /**
2760
- * Get recent memories across all sessions
2761
- */
2762
3343
  async retrieveRecent(limit = 100) {
2763
3344
  return this.eventStore.getRecentEvents(limit);
2764
3345
  }
2765
- /**
2766
- * Enrich search results with full event data
2767
- */
2768
3346
  async enrichResults(results, options) {
2769
3347
  const memories = [];
2770
3348
  for (const result of results) {
@@ -2772,27 +3350,16 @@ var Retriever = class {
2772
3350
  if (!event)
2773
3351
  continue;
2774
3352
  if (this.graduation) {
2775
- this.graduation.recordAccess(
2776
- event.id,
2777
- options.sessionId || "unknown",
2778
- result.score
2779
- );
3353
+ this.graduation.recordAccess(event.id, options.sessionId || "unknown", result.score);
2780
3354
  }
2781
3355
  let sessionContext;
2782
3356
  if (options.includeSessionContext) {
2783
3357
  sessionContext = await this.getSessionContext(event.sessionId, event.id);
2784
3358
  }
2785
- memories.push({
2786
- event,
2787
- score: result.score,
2788
- sessionContext
2789
- });
3359
+ memories.push({ event, score: result.score, sessionContext });
2790
3360
  }
2791
3361
  return memories;
2792
3362
  }
2793
- /**
2794
- * Get surrounding context from the same session
2795
- */
2796
3363
  async getSessionContext(sessionId, eventId) {
2797
3364
  const sessionEvents = await this.eventStore.getSessionEvents(sessionId);
2798
3365
  const eventIndex = sessionEvents.findIndex((e) => e.id === eventId);
@@ -2805,55 +3372,86 @@ var Retriever = class {
2805
3372
  return void 0;
2806
3373
  return contextEvents.filter((e) => e.id !== eventId).map((e) => `[${e.eventType}]: ${e.content.slice(0, 200)}...`).join("\n");
2807
3374
  }
2808
- /**
2809
- * Build context string from memories (respecting token limit)
2810
- */
3375
+ buildUnifiedContext(projectResult, sharedMemories) {
3376
+ let context = projectResult.context;
3377
+ if (sharedMemories.length === 0)
3378
+ return context;
3379
+ context += "\n\n## Cross-Project Knowledge\n\n";
3380
+ for (const memory of sharedMemories.slice(0, 3)) {
3381
+ context += `### ${memory.title}
3382
+ `;
3383
+ if (memory.symptoms.length > 0)
3384
+ context += `**Symptoms:** ${memory.symptoms.join(", ")}
3385
+ `;
3386
+ context += `**Root Cause:** ${memory.rootCause}
3387
+ `;
3388
+ context += `**Solution:** ${memory.solution}
3389
+ `;
3390
+ if (memory.technologies && memory.technologies.length > 0)
3391
+ context += `**Technologies:** ${memory.technologies.join(", ")}
3392
+ `;
3393
+ context += `_Confidence: ${(memory.confidence * 100).toFixed(0)}%_
3394
+
3395
+ `;
3396
+ }
3397
+ return context;
3398
+ }
2811
3399
  buildContext(memories, maxTokens) {
2812
3400
  const parts = [];
2813
3401
  let currentTokens = 0;
2814
3402
  for (const memory of memories) {
2815
3403
  const memoryText = this.formatMemory(memory);
2816
3404
  const memoryTokens = this.estimateTokens(memoryText);
2817
- if (currentTokens + memoryTokens > maxTokens) {
3405
+ if (currentTokens + memoryTokens > maxTokens)
2818
3406
  break;
2819
- }
2820
3407
  parts.push(memoryText);
2821
3408
  currentTokens += memoryTokens;
2822
3409
  }
2823
- if (parts.length === 0) {
3410
+ if (parts.length === 0)
2824
3411
  return "";
2825
- }
2826
3412
  return `## Relevant Memories
2827
3413
 
2828
3414
  ${parts.join("\n\n---\n\n")}`;
2829
3415
  }
2830
- /**
2831
- * Format a single memory for context
2832
- */
2833
3416
  formatMemory(memory) {
2834
3417
  const { event, score, sessionContext } = memory;
2835
3418
  const date = event.timestamp.toISOString().split("T")[0];
2836
3419
  let text = `**${event.eventType}** (${date}, score: ${score.toFixed(2)})
2837
3420
  ${event.content}`;
2838
- if (sessionContext) {
3421
+ if (sessionContext)
2839
3422
  text += `
2840
3423
 
2841
3424
  _Context:_ ${sessionContext}`;
2842
- }
2843
3425
  return text;
2844
3426
  }
2845
- /**
2846
- * Estimate token count (rough approximation)
2847
- */
3427
+ matchesMetadataScope(metadata, expected) {
3428
+ if (!metadata)
3429
+ return false;
3430
+ return Object.entries(expected).every(([path5, value]) => {
3431
+ const actual = path5.split(".").reduce((acc, key) => {
3432
+ if (typeof acc !== "object" || acc === null)
3433
+ return void 0;
3434
+ return acc[key];
3435
+ }, metadata);
3436
+ return actual === value;
3437
+ });
3438
+ }
3439
+ tokenize(text) {
3440
+ return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter((t) => t.length >= 2).slice(0, 64);
3441
+ }
3442
+ keywordOverlap(a, b) {
3443
+ if (a.length === 0 || b.length === 0)
3444
+ return 0;
3445
+ const bs = new Set(b);
3446
+ let hit = 0;
3447
+ for (const t of a)
3448
+ if (bs.has(t))
3449
+ hit += 1;
3450
+ return hit / a.length;
3451
+ }
2848
3452
  estimateTokens(text) {
2849
3453
  return Math.ceil(text.length / 4);
2850
3454
  }
2851
- /**
2852
- * Get event age in days (for recency scoring)
2853
- */
2854
- getEventAgeDays(eventId) {
2855
- return 0;
2856
- }
2857
3455
  };
2858
3456
  function createRetriever(eventStore, vectorStore, embedder, matcher) {
2859
3457
  return new Retriever(eventStore, vectorStore, embedder, matcher);
@@ -4143,6 +4741,59 @@ var ConsolidatedStore = class {
4143
4741
  [memoryId]
4144
4742
  );
4145
4743
  }
4744
+ /**
4745
+ * Create a long-term rule promoted from stable summaries
4746
+ */
4747
+ async createRule(input) {
4748
+ const ruleId = randomUUID6();
4749
+ await dbRun(
4750
+ this.db,
4751
+ `INSERT INTO consolidated_rules
4752
+ (rule_id, rule, topics, source_memory_ids, source_events, confidence, created_at)
4753
+ VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
4754
+ [
4755
+ ruleId,
4756
+ input.rule,
4757
+ JSON.stringify(input.topics),
4758
+ JSON.stringify(input.sourceMemoryIds),
4759
+ JSON.stringify(input.sourceEvents),
4760
+ input.confidence
4761
+ ]
4762
+ );
4763
+ return ruleId;
4764
+ }
4765
+ async getRules(options) {
4766
+ const limit = options?.limit || 100;
4767
+ const rows = await dbAll(
4768
+ this.db,
4769
+ `SELECT * FROM consolidated_rules ORDER BY confidence DESC, created_at DESC LIMIT ?`,
4770
+ [limit]
4771
+ );
4772
+ return rows.map((row) => ({
4773
+ ruleId: row.rule_id,
4774
+ rule: row.rule,
4775
+ topics: JSON.parse(row.topics || "[]"),
4776
+ sourceMemoryIds: JSON.parse(row.source_memory_ids || "[]"),
4777
+ sourceEvents: JSON.parse(row.source_events || "[]"),
4778
+ confidence: Number(row.confidence ?? 0.5),
4779
+ createdAt: toDate(row.created_at) || /* @__PURE__ */ new Date()
4780
+ }));
4781
+ }
4782
+ async countRules() {
4783
+ const result = await dbAll(
4784
+ this.db,
4785
+ `SELECT COUNT(*) as count FROM consolidated_rules`
4786
+ );
4787
+ return result[0]?.count || 0;
4788
+ }
4789
+ async hasRuleForSourceMemory(memoryId) {
4790
+ const rows = await dbAll(
4791
+ this.db,
4792
+ `SELECT COUNT(*) as count FROM consolidated_rules WHERE source_memory_ids LIKE ?`,
4793
+ [`%"${memoryId}"%`]
4794
+ );
4795
+ return (rows[0]?.count || 0) > 0;
4796
+ }
4146
4797
  /**
4147
4798
  * Get count of consolidated memories
4148
4799
  */
@@ -4292,7 +4943,14 @@ var ConsolidationWorker = class {
4292
4943
  * Force a consolidation run (manual trigger)
4293
4944
  */
4294
4945
  async forceRun() {
4295
- return await this.consolidate();
4946
+ const out = await this.consolidateWithReport();
4947
+ return out.consolidatedCount;
4948
+ }
4949
+ /**
4950
+ * Force a consolidation run and return metrics report
4951
+ */
4952
+ async forceRunWithReport() {
4953
+ return this.consolidateWithReport();
4296
4954
  }
4297
4955
  /**
4298
4956
  * Schedule the next consolidation check
@@ -4332,12 +4990,21 @@ var ConsolidationWorker = class {
4332
4990
  * Perform consolidation
4333
4991
  */
4334
4992
  async consolidate() {
4993
+ const out = await this.consolidateWithReport();
4994
+ return out.consolidatedCount;
4995
+ }
4996
+ async consolidateWithReport() {
4335
4997
  const workingSet = await this.workingSetStore.get();
4336
4998
  if (workingSet.recentEvents.length < 3) {
4337
- return 0;
4999
+ return {
5000
+ consolidatedCount: 0,
5001
+ promotedRuleCount: 0,
5002
+ report: this.buildCostQualityReport(workingSet.recentEvents, [], 0)
5003
+ };
4338
5004
  }
4339
5005
  const groups = this.groupByTopic(workingSet.recentEvents);
4340
5006
  let consolidatedCount = 0;
5007
+ const createdMemoryIds = [];
4341
5008
  for (const group of groups) {
4342
5009
  if (group.events.length < 3)
4343
5010
  continue;
@@ -4346,14 +5013,16 @@ var ConsolidationWorker = class {
4346
5013
  if (alreadyConsolidated)
4347
5014
  continue;
4348
5015
  const summary = await this.summarize(group);
4349
- await this.consolidatedStore.create({
5016
+ const memoryId = await this.consolidatedStore.create({
4350
5017
  summary,
4351
5018
  topics: group.topics,
4352
5019
  sourceEvents: eventIds,
4353
5020
  confidence: this.calculateConfidence(group)
4354
5021
  });
5022
+ createdMemoryIds.push(memoryId);
4355
5023
  consolidatedCount++;
4356
5024
  }
5025
+ const promotedRuleCount = await this.promoteStableSummariesToRules(createdMemoryIds);
4357
5026
  if (consolidatedCount > 0) {
4358
5027
  const consolidatedEventIds = groups.filter((g) => g.events.length >= 3).flatMap((g) => g.events.map((e) => e.id));
4359
5028
  const oldEventIds = consolidatedEventIds.filter((id) => {
@@ -4367,7 +5036,61 @@ var ConsolidationWorker = class {
4367
5036
  await this.workingSetStore.prune(oldEventIds);
4368
5037
  }
4369
5038
  }
4370
- return consolidatedCount;
5039
+ const report = this.buildCostQualityReport(workingSet.recentEvents, groups, consolidatedCount);
5040
+ return { consolidatedCount, promotedRuleCount, report };
5041
+ }
5042
+ async promoteStableSummariesToRules(memoryIds) {
5043
+ let promoted = 0;
5044
+ for (const memoryId of memoryIds) {
5045
+ const memory = await this.consolidatedStore.get(memoryId);
5046
+ if (!memory)
5047
+ continue;
5048
+ if (memory.confidence < 0.55)
5049
+ continue;
5050
+ if (memory.sourceEvents.length < 4)
5051
+ continue;
5052
+ const exists = await this.consolidatedStore.hasRuleForSourceMemory(memoryId);
5053
+ if (exists)
5054
+ continue;
5055
+ const rule = this.buildRuleFromSummary(memory.summary, memory.topics);
5056
+ if (!rule)
5057
+ continue;
5058
+ await this.consolidatedStore.createRule({
5059
+ rule,
5060
+ topics: memory.topics,
5061
+ sourceMemoryIds: [memory.memoryId],
5062
+ sourceEvents: memory.sourceEvents,
5063
+ confidence: Math.min(1, memory.confidence + 0.08)
5064
+ });
5065
+ promoted++;
5066
+ }
5067
+ return promoted;
5068
+ }
5069
+ buildRuleFromSummary(summary, topics) {
5070
+ const lines = summary.split(/\r?\n/).map((l) => l.trim()).filter(Boolean).filter((l) => !l.toLowerCase().startsWith("topics:"));
5071
+ const bullet = lines.find((l) => l.startsWith("- "))?.replace(/^-\s*/, "");
5072
+ const seed = bullet || lines[0];
5073
+ if (!seed || seed.length < 8)
5074
+ return null;
5075
+ const topicPrefix = topics.length > 0 ? `[${topics.slice(0, 2).join(", ")}] ` : "";
5076
+ return `${topicPrefix}${seed}`;
5077
+ }
5078
+ buildCostQualityReport(events, groups, consolidatedCount) {
5079
+ const beforeTokenEstimate = events.reduce((acc, e) => acc + this.estimateTokens(e.content), 0);
5080
+ const afterSummaries = groups.filter((g) => g.events.length >= 3).slice(0, Math.max(consolidatedCount, 1));
5081
+ const afterTokenEstimate = afterSummaries.length > 0 ? afterSummaries.reduce((acc, g) => acc + this.estimateTokens(this.ruleBasedSummary(g)), 0) : beforeTokenEstimate;
5082
+ const reductionRatio = beforeTokenEstimate > 0 ? Math.max(0, (beforeTokenEstimate - afterTokenEstimate) / beforeTokenEstimate) : 0;
5083
+ const qualityGuardPassed = consolidatedCount === 0 ? true : groups.filter((g) => g.events.length >= 3).every((g) => this.calculateConfidence(g) >= 0.55);
5084
+ return {
5085
+ beforeTokenEstimate,
5086
+ afterTokenEstimate,
5087
+ reductionRatio,
5088
+ qualityGuardPassed,
5089
+ details: `groups=${groups.length}, consolidated=${consolidatedCount}`
5090
+ };
5091
+ }
5092
+ estimateTokens(text) {
5093
+ return Math.ceil((text || "").length / 4);
4371
5094
  }
4372
5095
  /**
4373
5096
  * Check if consolidation should run
@@ -4927,13 +5650,185 @@ function createGraduationWorker(eventStore, graduation, config) {
4927
5650
  );
4928
5651
  }
4929
5652
 
5653
+ // src/core/md-mirror.ts
5654
+ import * as fs3 from "node:fs";
5655
+ import * as path2 from "node:path";
5656
+ function sanitizeSegment2(input, fallback) {
5657
+ const v = (input || "").trim().toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
5658
+ return v || fallback;
5659
+ }
5660
+ function getAtPath(obj, dotted) {
5661
+ if (!obj)
5662
+ return void 0;
5663
+ return dotted.split(".").reduce((acc, key) => {
5664
+ if (!acc || typeof acc !== "object")
5665
+ return void 0;
5666
+ return acc[key];
5667
+ }, obj);
5668
+ }
5669
+ function buildMirrorPath2(rootDir, event) {
5670
+ const meta = event.metadata;
5671
+ const namespaceRaw = getAtPath(meta, "namespace") ?? getAtPath(meta, "scope.namespace") ?? event.eventType;
5672
+ const namespace = sanitizeSegment2(typeof namespaceRaw === "string" ? namespaceRaw : void 0, "general");
5673
+ const categoryRaw = getAtPath(meta, "categoryPath") ?? getAtPath(meta, "scope.categoryPath");
5674
+ const categoryPath = Array.isArray(categoryRaw) && categoryRaw.length > 0 ? categoryRaw.map((x) => sanitizeSegment2(typeof x === "string" ? x : void 0, "uncategorized")) : ["uncategorized"];
5675
+ const d = event.timestamp;
5676
+ const yyyy = d.getFullYear();
5677
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
5678
+ const dd = String(d.getDate()).padStart(2, "0");
5679
+ return path2.join(rootDir, "memory", namespace, ...categoryPath, `${yyyy}-${mm}-${dd}.md`);
5680
+ }
5681
+ var MarkdownMirror2 = class {
5682
+ constructor(rootDir) {
5683
+ this.rootDir = rootDir;
5684
+ }
5685
+ async append(event, eventId) {
5686
+ const out = buildMirrorPath2(this.rootDir, event);
5687
+ fs3.mkdirSync(path2.dirname(out), { recursive: true });
5688
+ const lines = [
5689
+ "",
5690
+ `## ${event.timestamp.toISOString()} | ${eventId ?? "pending-id"}`,
5691
+ `- type: ${event.eventType}`,
5692
+ `- session: ${event.sessionId}`,
5693
+ event.content
5694
+ ];
5695
+ await fs3.promises.appendFile(out, lines.join("\n"), "utf8");
5696
+ await this.refreshIndex();
5697
+ }
5698
+ async refreshIndex() {
5699
+ const memoryRoot = path2.join(this.rootDir, "memory");
5700
+ await fs3.promises.mkdir(memoryRoot, { recursive: true });
5701
+ const files = [];
5702
+ await this.walk(memoryRoot, files);
5703
+ const mdFiles = files.filter((f) => f.endsWith(".md")).map((f) => path2.relative(this.rootDir, f)).filter((rel) => rel !== path2.join("memory", "_index.md")).sort();
5704
+ const index = [
5705
+ "# Memory Index",
5706
+ "",
5707
+ "Generated automatically by MarkdownMirror.",
5708
+ "",
5709
+ ...mdFiles.map((rel) => `- ${rel}`),
5710
+ ""
5711
+ ].join("\n");
5712
+ await fs3.promises.writeFile(path2.join(memoryRoot, "_index.md"), index, "utf8");
5713
+ }
5714
+ async walk(dir, out) {
5715
+ const entries = await fs3.promises.readdir(dir, { withFileTypes: true });
5716
+ for (const e of entries) {
5717
+ const full = path2.join(dir, e.name);
5718
+ if (e.isDirectory()) {
5719
+ await this.walk(full, out);
5720
+ } else {
5721
+ out.push(full);
5722
+ }
5723
+ }
5724
+ }
5725
+ };
5726
+
5727
+ // src/core/ingest-interceptor.ts
5728
+ var IngestInterceptorRegistry = class {
5729
+ before = [];
5730
+ after = [];
5731
+ onError = [];
5732
+ registerBefore(interceptor) {
5733
+ this.before.push(interceptor);
5734
+ return () => {
5735
+ this.before = this.before.filter((i) => i !== interceptor);
5736
+ };
5737
+ }
5738
+ registerAfter(interceptor) {
5739
+ this.after.push(interceptor);
5740
+ return () => {
5741
+ this.after = this.after.filter((i) => i !== interceptor);
5742
+ };
5743
+ }
5744
+ registerOnError(interceptor) {
5745
+ this.onError.push(interceptor);
5746
+ return () => {
5747
+ this.onError = this.onError.filter((i) => i !== interceptor);
5748
+ };
5749
+ }
5750
+ async run(stage, context) {
5751
+ const interceptors = stage === "before" ? this.before : stage === "after" ? this.after : this.onError;
5752
+ for (const interceptor of interceptors) {
5753
+ await interceptor({ ...context, stage });
5754
+ }
5755
+ }
5756
+ };
5757
+ function mergeHierarchicalMetadata(base, patch) {
5758
+ if (!base && !patch)
5759
+ return void 0;
5760
+ if (!base)
5761
+ return patch;
5762
+ if (!patch)
5763
+ return base;
5764
+ const result = { ...base };
5765
+ for (const [key, value] of Object.entries(patch)) {
5766
+ const current = result[key];
5767
+ if (typeof current === "object" && current !== null && !Array.isArray(current) && typeof value === "object" && value !== null && !Array.isArray(value)) {
5768
+ result[key] = mergeHierarchicalMetadata(
5769
+ current,
5770
+ value
5771
+ );
5772
+ } else {
5773
+ result[key] = value;
5774
+ }
5775
+ }
5776
+ return result;
5777
+ }
5778
+
5779
+ // src/core/tag-taxonomy.ts
5780
+ var TAG_NAMESPACES = {
5781
+ SYSTEM: "sys:",
5782
+ QUALITY: "q:",
5783
+ PROJECT: "proj:",
5784
+ TOPIC: "topic:",
5785
+ TEMPORAL: "t:",
5786
+ USER: "user:",
5787
+ AGENT: "agent:"
5788
+ };
5789
+ var VALID_TAG_NAMESPACES = new Set(Object.values(TAG_NAMESPACES));
5790
+ function parseTag(tag) {
5791
+ const value = (tag || "").trim();
5792
+ const idx = value.indexOf(":");
5793
+ if (idx <= 0)
5794
+ return { value };
5795
+ const namespace = `${value.slice(0, idx)}:`;
5796
+ const tagValue = value.slice(idx + 1);
5797
+ if (!tagValue)
5798
+ return { value };
5799
+ return { namespace, value: tagValue };
5800
+ }
5801
+ function validateTag(tag) {
5802
+ const normalized = (tag || "").trim();
5803
+ if (!normalized)
5804
+ return false;
5805
+ const { namespace } = parseTag(normalized);
5806
+ if (!namespace)
5807
+ return true;
5808
+ return VALID_TAG_NAMESPACES.has(namespace);
5809
+ }
5810
+ function normalizeTags(tags) {
5811
+ if (!Array.isArray(tags))
5812
+ return [];
5813
+ const dedup = /* @__PURE__ */ new Set();
5814
+ for (const item of tags) {
5815
+ if (typeof item !== "string")
5816
+ continue;
5817
+ const normalized = item.trim();
5818
+ if (!validateTag(normalized))
5819
+ continue;
5820
+ dedup.add(normalized);
5821
+ }
5822
+ return [...dedup];
5823
+ }
5824
+
4930
5825
  // src/services/memory-service.ts
4931
5826
  function normalizePath(projectPath) {
4932
- const expanded = projectPath.startsWith("~") ? path.join(os.homedir(), projectPath.slice(1)) : projectPath;
5827
+ const expanded = projectPath.startsWith("~") ? path3.join(os.homedir(), projectPath.slice(1)) : projectPath;
4933
5828
  try {
4934
- return fs2.realpathSync(expanded);
5829
+ return fs4.realpathSync(expanded);
4935
5830
  } catch {
4936
- return path.resolve(expanded);
5831
+ return path3.resolve(expanded);
4937
5832
  }
4938
5833
  }
4939
5834
  function hashProjectPath(projectPath) {
@@ -4942,14 +5837,14 @@ function hashProjectPath(projectPath) {
4942
5837
  }
4943
5838
  function getProjectStoragePath(projectPath) {
4944
5839
  const hash = hashProjectPath(projectPath);
4945
- return path.join(os.homedir(), ".claude-code", "memory", "projects", hash);
5840
+ return path3.join(os.homedir(), ".claude-code", "memory", "projects", hash);
4946
5841
  }
4947
- var REGISTRY_PATH = path.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
4948
- var SHARED_STORAGE_PATH = path.join(os.homedir(), ".claude-code", "memory", "shared");
5842
+ var REGISTRY_PATH = path3.join(os.homedir(), ".claude-code", "memory", "session-registry.json");
5843
+ var SHARED_STORAGE_PATH = path3.join(os.homedir(), ".claude-code", "memory", "shared");
4949
5844
  function loadSessionRegistry() {
4950
5845
  try {
4951
- if (fs2.existsSync(REGISTRY_PATH)) {
4952
- const data = fs2.readFileSync(REGISTRY_PATH, "utf-8");
5846
+ if (fs4.existsSync(REGISTRY_PATH)) {
5847
+ const data = fs4.readFileSync(REGISTRY_PATH, "utf-8");
4953
5848
  return JSON.parse(data);
4954
5849
  }
4955
5850
  } catch (error) {
@@ -4975,6 +5870,7 @@ var MemoryService = class {
4975
5870
  vectorWorker = null;
4976
5871
  graduationWorker = null;
4977
5872
  initialized = false;
5873
+ ingestInterceptors = new IngestInterceptorRegistry();
4978
5874
  // Endless Mode components
4979
5875
  workingSetStore = null;
4980
5876
  consolidatedStore = null;
@@ -4988,20 +5884,27 @@ var MemoryService = class {
4988
5884
  sharedPromoter = null;
4989
5885
  sharedStoreConfig = null;
4990
5886
  projectHash = null;
5887
+ projectPath = null;
4991
5888
  readOnly;
4992
5889
  lightweightMode;
5890
+ mdMirror;
4993
5891
  constructor(config) {
4994
5892
  const storagePath = this.expandPath(config.storagePath);
4995
5893
  this.readOnly = config.readOnly ?? false;
4996
5894
  this.lightweightMode = config.lightweightMode ?? false;
4997
- if (!this.readOnly && !fs2.existsSync(storagePath)) {
4998
- fs2.mkdirSync(storagePath, { recursive: true });
5895
+ this.mdMirror = new MarkdownMirror2(process.cwd());
5896
+ if (!this.readOnly && !fs4.existsSync(storagePath)) {
5897
+ fs4.mkdirSync(storagePath, { recursive: true });
4999
5898
  }
5000
5899
  this.projectHash = config.projectHash || null;
5900
+ this.projectPath = config.projectPath || null;
5001
5901
  this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
5002
5902
  this.sqliteStore = new SQLiteEventStore(
5003
- path.join(storagePath, "events.sqlite"),
5004
- { readonly: this.readOnly }
5903
+ path3.join(storagePath, "events.sqlite"),
5904
+ {
5905
+ readonly: this.readOnly,
5906
+ markdownMirrorRoot: storagePath
5907
+ }
5005
5908
  );
5006
5909
  const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
5007
5910
  if (!analyticsEnabled) {
@@ -5009,7 +5912,7 @@ var MemoryService = class {
5009
5912
  } else if (this.readOnly) {
5010
5913
  try {
5011
5914
  this.analyticsStore = new EventStore(
5012
- path.join(storagePath, "analytics.duckdb"),
5915
+ path3.join(storagePath, "analytics.duckdb"),
5013
5916
  { readOnly: true }
5014
5917
  );
5015
5918
  } catch {
@@ -5017,11 +5920,11 @@ var MemoryService = class {
5017
5920
  }
5018
5921
  } else {
5019
5922
  this.analyticsStore = new EventStore(
5020
- path.join(storagePath, "analytics.duckdb"),
5923
+ path3.join(storagePath, "analytics.duckdb"),
5021
5924
  { readOnly: false }
5022
5925
  );
5023
5926
  }
5024
- this.vectorStore = new VectorStore(path.join(storagePath, "vectors"));
5927
+ this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
5025
5928
  this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
5026
5929
  this.matcher = getDefaultMatcher();
5027
5930
  this.retriever = createRetriever(
@@ -5031,6 +5934,7 @@ var MemoryService = class {
5031
5934
  this.embedder,
5032
5935
  this.matcher
5033
5936
  );
5937
+ this.retriever.setQueryRewriter((q) => this.rewriteQueryIntent(q));
5034
5938
  this.graduation = createGraduationPipeline(this.sqliteStore);
5035
5939
  }
5036
5940
  /**
@@ -5090,16 +5994,16 @@ var MemoryService = class {
5090
5994
  */
5091
5995
  async initializeSharedStore() {
5092
5996
  const sharedPath = this.sharedStoreConfig?.sharedStoragePath ? this.expandPath(this.sharedStoreConfig.sharedStoragePath) : SHARED_STORAGE_PATH;
5093
- if (!fs2.existsSync(sharedPath)) {
5094
- fs2.mkdirSync(sharedPath, { recursive: true });
5997
+ if (!fs4.existsSync(sharedPath)) {
5998
+ fs4.mkdirSync(sharedPath, { recursive: true });
5095
5999
  }
5096
6000
  this.sharedEventStore = createSharedEventStore(
5097
- path.join(sharedPath, "shared.duckdb")
6001
+ path3.join(sharedPath, "shared.duckdb")
5098
6002
  );
5099
6003
  await this.sharedEventStore.initialize();
5100
6004
  this.sharedStore = createSharedStore(this.sharedEventStore);
5101
6005
  this.sharedVectorStore = createSharedVectorStore(
5102
- path.join(sharedPath, "vectors")
6006
+ path3.join(sharedPath, "vectors")
5103
6007
  );
5104
6008
  await this.sharedVectorStore.initialize();
5105
6009
  this.sharedPromoter = createSharedPromoter(
@@ -5110,6 +6014,86 @@ var MemoryService = class {
5110
6014
  );
5111
6015
  this.retriever.setSharedStores(this.sharedStore, this.sharedVectorStore);
5112
6016
  }
6017
+ registerIngestBefore(interceptor) {
6018
+ return this.ingestInterceptors.registerBefore(interceptor);
6019
+ }
6020
+ registerIngestAfter(interceptor) {
6021
+ return this.ingestInterceptors.registerAfter(interceptor);
6022
+ }
6023
+ registerIngestOnError(interceptor) {
6024
+ return this.ingestInterceptors.registerOnError(interceptor);
6025
+ }
6026
+ async ingestWithInterceptors(operation, input, onSuccess) {
6027
+ const normalizedInput = {
6028
+ ...input,
6029
+ metadata: mergeHierarchicalMetadata(
6030
+ {
6031
+ ingest: {
6032
+ operation,
6033
+ pipeline: "default",
6034
+ ts: (/* @__PURE__ */ new Date()).toISOString()
6035
+ },
6036
+ ...this.projectHash ? {
6037
+ scope: {
6038
+ project: {
6039
+ hash: this.projectHash,
6040
+ ...this.projectPath ? { path: this.projectPath } : {}
6041
+ }
6042
+ },
6043
+ tags: [`proj:${this.projectHash}`]
6044
+ } : {}
6045
+ },
6046
+ input.metadata
6047
+ )
6048
+ };
6049
+ if (this.projectHash && normalizedInput.metadata) {
6050
+ const meta = normalizedInput.metadata;
6051
+ const currentTags = Array.isArray(meta.tags) ? meta.tags.filter((x) => typeof x === "string") : [];
6052
+ const projectTag = `proj:${this.projectHash}`;
6053
+ if (!currentTags.includes(projectTag)) {
6054
+ meta.tags = [...currentTags, projectTag];
6055
+ }
6056
+ }
6057
+ if (normalizedInput.metadata) {
6058
+ const meta = normalizedInput.metadata;
6059
+ const normalizedTags = normalizeTags(meta.tags);
6060
+ if (normalizedTags.length > 0) {
6061
+ meta.tags = normalizedTags;
6062
+ }
6063
+ }
6064
+ await this.ingestInterceptors.run("before", {
6065
+ operation,
6066
+ sessionId: normalizedInput.sessionId,
6067
+ event: normalizedInput
6068
+ });
6069
+ try {
6070
+ const result = await this.sqliteStore.append(normalizedInput);
6071
+ if (result.success && !result.isDuplicate) {
6072
+ if (onSuccess) {
6073
+ await onSuccess(result.eventId);
6074
+ }
6075
+ try {
6076
+ await this.mdMirror.append(normalizedInput, result.eventId);
6077
+ } catch {
6078
+ }
6079
+ }
6080
+ await this.ingestInterceptors.run("after", {
6081
+ operation,
6082
+ sessionId: normalizedInput.sessionId,
6083
+ event: normalizedInput
6084
+ });
6085
+ return result;
6086
+ } catch (error) {
6087
+ const normalizedError = error instanceof Error ? error : new Error(String(error));
6088
+ await this.ingestInterceptors.run("error", {
6089
+ operation,
6090
+ sessionId: normalizedInput.sessionId,
6091
+ event: normalizedInput,
6092
+ error: normalizedError
6093
+ });
6094
+ throw error;
6095
+ }
6096
+ }
5113
6097
  /**
5114
6098
  * Start a new session
5115
6099
  */
@@ -5137,50 +6121,57 @@ var MemoryService = class {
5137
6121
  */
5138
6122
  async storeUserPrompt(sessionId, content, metadata) {
5139
6123
  await this.initialize();
5140
- const result = await this.sqliteStore.append({
5141
- eventType: "user_prompt",
5142
- sessionId,
5143
- timestamp: /* @__PURE__ */ new Date(),
5144
- content,
5145
- metadata
5146
- });
5147
- if (result.success && !result.isDuplicate) {
5148
- await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
5149
- }
5150
- return result;
6124
+ return this.ingestWithInterceptors(
6125
+ "user_prompt",
6126
+ {
6127
+ eventType: "user_prompt",
6128
+ sessionId,
6129
+ timestamp: /* @__PURE__ */ new Date(),
6130
+ content,
6131
+ metadata
6132
+ },
6133
+ async (eventId) => {
6134
+ await this.sqliteStore.enqueueForEmbedding(eventId, content);
6135
+ }
6136
+ );
5151
6137
  }
5152
6138
  /**
5153
6139
  * Store an agent response
5154
6140
  */
5155
6141
  async storeAgentResponse(sessionId, content, metadata) {
5156
6142
  await this.initialize();
5157
- const result = await this.sqliteStore.append({
5158
- eventType: "agent_response",
5159
- sessionId,
5160
- timestamp: /* @__PURE__ */ new Date(),
5161
- content,
5162
- metadata
5163
- });
5164
- if (result.success && !result.isDuplicate) {
5165
- await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
5166
- }
5167
- return result;
6143
+ return this.ingestWithInterceptors(
6144
+ "agent_response",
6145
+ {
6146
+ eventType: "agent_response",
6147
+ sessionId,
6148
+ timestamp: /* @__PURE__ */ new Date(),
6149
+ content,
6150
+ metadata
6151
+ },
6152
+ async (eventId) => {
6153
+ await this.sqliteStore.enqueueForEmbedding(eventId, content);
6154
+ }
6155
+ );
5168
6156
  }
5169
6157
  /**
5170
6158
  * Store a session summary
5171
6159
  */
5172
- async storeSessionSummary(sessionId, summary) {
6160
+ async storeSessionSummary(sessionId, summary, metadata) {
5173
6161
  await this.initialize();
5174
- const result = await this.sqliteStore.append({
5175
- eventType: "session_summary",
5176
- sessionId,
5177
- timestamp: /* @__PURE__ */ new Date(),
5178
- content: summary
5179
- });
5180
- if (result.success && !result.isDuplicate) {
5181
- await this.sqliteStore.enqueueForEmbedding(result.eventId, summary);
5182
- }
5183
- return result;
6162
+ return this.ingestWithInterceptors(
6163
+ "session_summary",
6164
+ {
6165
+ eventType: "session_summary",
6166
+ sessionId,
6167
+ timestamp: /* @__PURE__ */ new Date(),
6168
+ content: summary,
6169
+ metadata
6170
+ },
6171
+ async (eventId) => {
6172
+ await this.sqliteStore.enqueueForEmbedding(eventId, summary);
6173
+ }
6174
+ );
5184
6175
  }
5185
6176
  /**
5186
6177
  * Store a tool observation
@@ -5189,40 +6180,181 @@ var MemoryService = class {
5189
6180
  await this.initialize();
5190
6181
  const content = JSON.stringify(payload);
5191
6182
  const turnId = payload.metadata?.turnId;
5192
- const result = await this.sqliteStore.append({
5193
- eventType: "tool_observation",
5194
- sessionId,
5195
- timestamp: /* @__PURE__ */ new Date(),
5196
- content,
5197
- metadata: {
5198
- toolName: payload.toolName,
5199
- success: payload.success,
5200
- ...turnId ? { turnId } : {}
6183
+ return this.ingestWithInterceptors(
6184
+ "tool_observation",
6185
+ {
6186
+ eventType: "tool_observation",
6187
+ sessionId,
6188
+ timestamp: /* @__PURE__ */ new Date(),
6189
+ content,
6190
+ metadata: {
6191
+ toolName: payload.toolName,
6192
+ success: payload.success,
6193
+ ...turnId ? { turnId } : {}
6194
+ }
6195
+ },
6196
+ async (eventId) => {
6197
+ const embeddingContent = createToolObservationEmbedding(
6198
+ payload.toolName,
6199
+ payload.metadata || {},
6200
+ payload.success
6201
+ );
6202
+ await this.sqliteStore.enqueueForEmbedding(eventId, embeddingContent);
5201
6203
  }
5202
- });
5203
- if (result.success && !result.isDuplicate) {
5204
- const embeddingContent = createToolObservationEmbedding(
5205
- payload.toolName,
5206
- payload.metadata || {},
5207
- payload.success
5208
- );
5209
- await this.sqliteStore.enqueueForEmbedding(result.eventId, embeddingContent);
5210
- }
5211
- return result;
6204
+ );
5212
6205
  }
5213
6206
  /**
5214
6207
  * Retrieve relevant memories for a query
5215
6208
  */
5216
6209
  async retrieveMemories(query, options) {
5217
6210
  await this.initialize();
6211
+ const rerankWeights = await this.getRerankWeights(options?.adaptiveRerank === true);
6212
+ let result;
5218
6213
  if (options?.includeShared && this.sharedStore) {
5219
- return this.retriever.retrieveUnified(query, {
6214
+ result = await this.retriever.retrieveUnified(query, {
5220
6215
  ...options,
6216
+ intentRewrite: options?.intentRewrite === true,
6217
+ rerankWeights,
5221
6218
  includeShared: true,
5222
- projectHash: this.projectHash || void 0
6219
+ projectHash: this.projectHash || void 0,
6220
+ projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
6221
+ allowedProjectHashes: options?.allowedProjectHashes
6222
+ });
6223
+ } else {
6224
+ result = await this.retriever.retrieve(query, {
6225
+ ...options,
6226
+ intentRewrite: options?.intentRewrite === true,
6227
+ rerankWeights,
6228
+ projectHash: this.projectHash || void 0,
6229
+ projectScopeMode: options?.projectScopeMode ?? (this.projectHash ? "strict" : "global"),
6230
+ allowedProjectHashes: options?.allowedProjectHashes
5223
6231
  });
5224
6232
  }
5225
- return this.retriever.retrieve(query, options);
6233
+ try {
6234
+ const selectedEventIds = result.memories.map((m) => m.event.id);
6235
+ const selectedDetails = (result.selectedDebug || []).map((d) => ({
6236
+ eventId: d.eventId,
6237
+ score: d.score,
6238
+ semanticScore: d.semanticScore,
6239
+ lexicalScore: d.lexicalScore,
6240
+ recencyScore: d.recencyScore
6241
+ }));
6242
+ const candidateDetails = (result.candidateDebug || []).map((d) => ({
6243
+ eventId: d.eventId,
6244
+ score: d.score,
6245
+ semanticScore: d.semanticScore,
6246
+ lexicalScore: d.lexicalScore,
6247
+ recencyScore: d.recencyScore
6248
+ }));
6249
+ const candidateEventIds = candidateDetails.length > 0 ? candidateDetails.map((d) => d.eventId) : selectedEventIds;
6250
+ await this.sqliteStore.recordRetrievalTrace({
6251
+ sessionId: options?.sessionId,
6252
+ projectHash: this.projectHash || void 0,
6253
+ queryText: query,
6254
+ strategy: options?.strategy || "auto",
6255
+ candidateEventIds,
6256
+ selectedEventIds,
6257
+ candidateDetails,
6258
+ selectedDetails,
6259
+ confidence: result.matchResult.confidence,
6260
+ fallbackTrace: result.fallbackTrace || []
6261
+ });
6262
+ } catch {
6263
+ }
6264
+ return result;
6265
+ }
6266
+ getConfiguredRerankWeights() {
6267
+ const semantic = Number(process.env.MEMORY_RERANK_WEIGHT_SEMANTIC ?? "");
6268
+ const lexical = Number(process.env.MEMORY_RERANK_WEIGHT_LEXICAL ?? "");
6269
+ const recency = Number(process.env.MEMORY_RERANK_WEIGHT_RECENCY ?? "");
6270
+ const allFinite = [semantic, lexical, recency].every((v) => Number.isFinite(v));
6271
+ if (!allFinite)
6272
+ return void 0;
6273
+ const nonNegative = [semantic, lexical, recency].every((v) => v >= 0);
6274
+ const total = semantic + lexical + recency;
6275
+ if (!nonNegative || total <= 0)
6276
+ return void 0;
6277
+ return {
6278
+ semantic: semantic / total,
6279
+ lexical: lexical / total,
6280
+ recency: recency / total
6281
+ };
6282
+ }
6283
+ async getRerankWeights(adaptive) {
6284
+ const configured = this.getConfiguredRerankWeights();
6285
+ if (configured)
6286
+ return configured;
6287
+ if (adaptive)
6288
+ return this.getAdaptiveRerankWeights();
6289
+ return void 0;
6290
+ }
6291
+ async rewriteQueryIntent(query) {
6292
+ if (process.env.MEMORY_INTENT_REWRITE_ENABLED !== "1")
6293
+ return null;
6294
+ const apiUrl = process.env.COMPANY_STOCK_API_URL || process.env.COMPANY_INT_API_URL;
6295
+ if (!apiUrl)
6296
+ return null;
6297
+ const controller = new AbortController();
6298
+ const timeoutMs = Number(process.env.MEMORY_INTENT_REWRITE_TIMEOUT_MS || 5e3);
6299
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
6300
+ try {
6301
+ const prompt = [
6302
+ "Rewrite user query for memory retrieval intent expansion.",
6303
+ "Return plain text only, one line, no markdown.",
6304
+ `Query: ${query}`
6305
+ ].join("\n");
6306
+ const res = await fetch(apiUrl, {
6307
+ method: "POST",
6308
+ headers: {
6309
+ "Content-Type": "application/json",
6310
+ Accept: "*/*",
6311
+ Origin: process.env.COMPANY_INT_ORIGIN || "http://company-int.aplusai.ai",
6312
+ Referer: process.env.COMPANY_INT_REFERER || "http://company-int.aplusai.ai/"
6313
+ },
6314
+ body: JSON.stringify({
6315
+ question: prompt,
6316
+ company_name: null,
6317
+ conversation_id: null
6318
+ }),
6319
+ signal: controller.signal
6320
+ });
6321
+ const text = (await res.text()).trim();
6322
+ if (!text)
6323
+ return null;
6324
+ const oneLine = text.replace(/^data:\s*/gm, "").split(/\r?\n/).map((x) => x.trim()).filter(Boolean).join(" ").slice(0, 240);
6325
+ if (!oneLine || oneLine.toLowerCase() === query.toLowerCase())
6326
+ return null;
6327
+ return oneLine;
6328
+ } catch {
6329
+ return null;
6330
+ } finally {
6331
+ clearTimeout(timeout);
6332
+ }
6333
+ }
6334
+ async getAdaptiveRerankWeights() {
6335
+ try {
6336
+ const s = await this.sqliteStore.getHelpfulnessStats();
6337
+ if (s.totalEvaluated < 20)
6338
+ return void 0;
6339
+ let semantic = 0.7;
6340
+ let lexical = 0.2;
6341
+ let recency = 0.1;
6342
+ if (s.avgScore < 0.45) {
6343
+ semantic -= 0.1;
6344
+ lexical += 0.1;
6345
+ } else if (s.avgScore > 0.75) {
6346
+ semantic += 0.05;
6347
+ lexical -= 0.05;
6348
+ }
6349
+ if (s.unhelpful > s.helpful) {
6350
+ recency += 0.05;
6351
+ semantic -= 0.03;
6352
+ lexical -= 0.02;
6353
+ }
6354
+ return { semantic, lexical, recency };
6355
+ } catch {
6356
+ return void 0;
6357
+ }
5226
6358
  }
5227
6359
  /**
5228
6360
  * Fast keyword search using SQLite FTS5
@@ -5264,6 +6396,18 @@ var MemoryService = class {
5264
6396
  /**
5265
6397
  * Get memory statistics
5266
6398
  */
6399
+ async getOutboxStats() {
6400
+ await this.initialize();
6401
+ return this.sqliteStore.getOutboxStats();
6402
+ }
6403
+ async getRetrievalTraceStats() {
6404
+ await this.initialize();
6405
+ return this.sqliteStore.getRetrievalTraceStats();
6406
+ }
6407
+ async getRecentRetrievalTraces(limit = 50) {
6408
+ await this.initialize();
6409
+ return this.sqliteStore.getRecentRetrievalTraces(limit);
6410
+ }
5267
6411
  async getStats() {
5268
6412
  await this.initialize();
5269
6413
  const recentEvents = await this.sqliteStore.getRecentEvents(1e4);
@@ -5754,7 +6898,7 @@ var MemoryService = class {
5754
6898
  */
5755
6899
  expandPath(p) {
5756
6900
  if (p.startsWith("~")) {
5757
- return path.join(os.homedir(), p.slice(1));
6901
+ return path3.join(os.homedir(), p.slice(1));
5758
6902
  }
5759
6903
  return p;
5760
6904
  }
@@ -5764,10 +6908,11 @@ function getLightweightMemoryService(sessionId) {
5764
6908
  const projectInfo = getSessionProject(sessionId);
5765
6909
  const key = projectInfo ? `lightweight_${projectInfo.projectHash}` : "lightweight_global";
5766
6910
  if (!serviceCache.has(key)) {
5767
- const storagePath = projectInfo ? getProjectStoragePath(projectInfo.projectPath) : path.join(os.homedir(), ".claude-code", "memory");
6911
+ const storagePath = projectInfo ? getProjectStoragePath(projectInfo.projectPath) : path3.join(os.homedir(), ".claude-code", "memory");
5768
6912
  serviceCache.set(key, new MemoryService({
5769
6913
  storagePath,
5770
6914
  projectHash: projectInfo?.projectHash,
6915
+ projectPath: projectInfo?.projectPath,
5771
6916
  lightweightMode: true,
5772
6917
  // Skip embedder/vector/workers
5773
6918
  analyticsEnabled: false,
@@ -5923,20 +7068,20 @@ function applyPrivacyFilter(content, config) {
5923
7068
  }
5924
7069
 
5925
7070
  // src/core/turn-state.ts
5926
- import * as fs3 from "fs";
5927
- import * as path2 from "path";
7071
+ import * as fs5 from "fs";
7072
+ import * as path4 from "path";
5928
7073
  import * as os2 from "os";
5929
- var TURN_STATE_DIR = path2.join(os2.homedir(), ".claude-code", "memory");
7074
+ var TURN_STATE_DIR = path4.join(os2.homedir(), ".claude-code", "memory");
5930
7075
  function getStatePath(sessionId) {
5931
- return path2.join(TURN_STATE_DIR, `.turn-state-${sessionId}.json`);
7076
+ return path4.join(TURN_STATE_DIR, `.turn-state-${sessionId}.json`);
5932
7077
  }
5933
7078
  function readTurnState(sessionId) {
5934
7079
  try {
5935
7080
  const filePath = getStatePath(sessionId);
5936
- if (!fs3.existsSync(filePath)) {
7081
+ if (!fs5.existsSync(filePath)) {
5937
7082
  return null;
5938
7083
  }
5939
- const data = fs3.readFileSync(filePath, "utf-8");
7084
+ const data = fs5.readFileSync(filePath, "utf-8");
5940
7085
  const state = JSON.parse(data);
5941
7086
  if (state.sessionId !== sessionId) {
5942
7087
  return null;
@@ -5958,8 +7103,8 @@ function readTurnState(sessionId) {
5958
7103
  function clearTurnState(sessionId) {
5959
7104
  try {
5960
7105
  const filePath = getStatePath(sessionId);
5961
- if (fs3.existsSync(filePath)) {
5962
- fs3.unlinkSync(filePath);
7106
+ if (fs5.existsSync(filePath)) {
7107
+ fs5.unlinkSync(filePath);
5963
7108
  }
5964
7109
  } catch (error) {
5965
7110
  if (process.env.CLAUDE_MEMORY_DEBUG) {
@@ -5980,12 +7125,12 @@ var DEFAULT_PRIVACY_CONFIG = {
5980
7125
  }
5981
7126
  };
5982
7127
  async function extractAssistantMessages(transcriptPath) {
5983
- if (!fs4.existsSync(transcriptPath))
7128
+ if (!fs6.existsSync(transcriptPath))
5984
7129
  return [];
5985
7130
  const messages = [];
5986
- const stats = fs4.statSync(transcriptPath);
7131
+ const stats = fs6.statSync(transcriptPath);
5987
7132
  const readStart = Math.max(0, stats.size - 200 * 1024);
5988
- const stream = fs4.createReadStream(transcriptPath, {
7133
+ const stream = fs6.createReadStream(transcriptPath, {
5989
7134
  start: readStart,
5990
7135
  encoding: "utf8"
5991
7136
  });