claude-memory-layer 1.0.9 → 1.0.11

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 (73) hide show
  1. package/dist/cli/index.js +1373 -184
  2. package/dist/cli/index.js.map +4 -4
  3. package/dist/core/index.js +445 -7
  4. package/dist/core/index.js.map +2 -2
  5. package/dist/hooks/post-tool-use.js +705 -43
  6. package/dist/hooks/post-tool-use.js.map +4 -4
  7. package/dist/hooks/session-end.js +593 -52
  8. package/dist/hooks/session-end.js.map +3 -3
  9. package/dist/hooks/session-start.js +581 -25
  10. package/dist/hooks/session-start.js.map +3 -3
  11. package/dist/hooks/stop.js +693 -73
  12. package/dist/hooks/stop.js.map +4 -4
  13. package/dist/hooks/user-prompt-submit.js +674 -94
  14. package/dist/hooks/user-prompt-submit.js.map +4 -4
  15. package/dist/server/api/index.js +1045 -42
  16. package/dist/server/api/index.js.map +4 -4
  17. package/dist/server/index.js +1054 -51
  18. package/dist/server/index.js.map +4 -4
  19. package/dist/services/memory-service.js +599 -25
  20. package/dist/services/memory-service.js.map +3 -3
  21. package/dist/ui/app.js +1380 -55
  22. package/dist/ui/index.html +311 -148
  23. package/dist/ui/style.css +892 -0
  24. package/docs/OPERATIONS.md +18 -0
  25. package/package.json +8 -2
  26. package/scripts/fix-sync-gap.js +32 -0
  27. package/scripts/heartbeat-memory-orchestrator.sh +28 -0
  28. package/scripts/report-sync-gap.js +26 -0
  29. package/scripts/review-queue-auto-resolve.js +21 -0
  30. package/scripts/sync-gap-auto-heal.sh +17 -0
  31. package/specs/20260207-dashboard-upgrade/context.md +38 -0
  32. package/specs/20260207-dashboard-upgrade/spec.md +96 -0
  33. package/src/cli/index.ts +110 -58
  34. package/src/core/sqlite-event-store.ts +542 -6
  35. package/src/core/sqlite-wrapper.ts +8 -0
  36. package/src/core/turn-state.ts +159 -0
  37. package/src/core/types.ts +23 -8
  38. package/src/core/vector-store.ts +21 -3
  39. package/src/hooks/post-tool-use.ts +68 -23
  40. package/src/hooks/session-end.ts +8 -3
  41. package/src/hooks/stop.ts +96 -25
  42. package/src/hooks/user-prompt-submit.ts +78 -65
  43. package/src/server/api/chat.ts +244 -0
  44. package/src/server/api/citations.ts +3 -3
  45. package/src/server/api/events.ts +30 -5
  46. package/src/server/api/index.ts +7 -1
  47. package/src/server/api/projects.ts +74 -0
  48. package/src/server/api/search.ts +3 -3
  49. package/src/server/api/sessions.ts +3 -3
  50. package/src/server/api/stats.ts +43 -7
  51. package/src/server/api/turns.ts +143 -0
  52. package/src/server/api/utils.ts +46 -0
  53. package/src/services/memory-service.ts +208 -9
  54. package/src/services/session-history-importer.ts +215 -51
  55. package/src/ui/app.js +1380 -55
  56. package/src/ui/index.html +311 -148
  57. package/src/ui/style.css +892 -0
  58. package/.claude/settings.local.json +0 -27
  59. package/.claude-memory/test.sqlite +0 -0
  60. package/.history/package_20260201112328.json +0 -45
  61. package/.history/package_20260201113602.json +0 -45
  62. package/.history/package_20260201113713.json +0 -45
  63. package/.history/package_20260201114110.json +0 -45
  64. package/.history/package_20260201114632.json +0 -46
  65. package/.history/package_20260201133143.json +0 -45
  66. package/.history/package_20260201134319.json +0 -45
  67. package/.history/package_20260201134326.json +0 -45
  68. package/.history/package_20260201134334.json +0 -45
  69. package/.history/package_20260201134912.json +0 -45
  70. package/.history/package_20260201142928.json +0 -46
  71. package/.history/package_20260201192048.json +0 -47
  72. package/.history/package_20260202114053.json +0 -49
  73. package/test_access.js +0 -49
@@ -6,10 +6,14 @@ 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/stop.ts
10
+ import * as fs4 from "fs";
11
+ import * as readline from "readline";
12
+
9
13
  // src/services/memory-service.ts
10
14
  import * as path from "path";
11
15
  import * as os from "os";
12
- import * as fs from "fs";
16
+ import * as fs2 from "fs";
13
17
  import * as crypto2 from "crypto";
14
18
 
15
19
  // src/core/event-store.ts
@@ -67,11 +71,11 @@ function toDate(value) {
67
71
  return new Date(value);
68
72
  return new Date(String(value));
69
73
  }
70
- function createDatabase(path2, options) {
74
+ function createDatabase(path3, options) {
71
75
  if (options?.readOnly) {
72
- return new duckdb.Database(path2, { access_mode: "READ_ONLY" });
76
+ return new duckdb.Database(path3, { access_mode: "READ_ONLY" });
73
77
  }
74
- return new duckdb.Database(path2);
78
+ return new duckdb.Database(path3);
75
79
  }
76
80
  function dbRun(db, sql, params = []) {
77
81
  return new Promise((resolve2, reject) => {
@@ -741,8 +745,14 @@ import { randomUUID as randomUUID2 } from "crypto";
741
745
 
742
746
  // src/core/sqlite-wrapper.ts
743
747
  import Database from "better-sqlite3";
744
- function createSQLiteDatabase(path2, options) {
745
- const db = new Database(path2, {
748
+ import * as fs from "fs";
749
+ import * as nodePath from "path";
750
+ function createSQLiteDatabase(path3, options) {
751
+ const dir = nodePath.dirname(path3);
752
+ if (!fs.existsSync(dir)) {
753
+ fs.mkdirSync(dir, { recursive: true });
754
+ }
755
+ const db = new Database(path3, {
746
756
  readonly: options?.readonly ?? false
747
757
  });
748
758
  if (!options?.readonly && (options?.walMode ?? true)) {
@@ -1009,6 +1019,23 @@ var SQLiteEventStore = class {
1009
1019
  updated_at TEXT DEFAULT (datetime('now'))
1010
1020
  );
1011
1021
 
1022
+ -- Memory Helpfulness tracking
1023
+ CREATE TABLE IF NOT EXISTS memory_helpfulness (
1024
+ id TEXT PRIMARY KEY,
1025
+ event_id TEXT NOT NULL,
1026
+ session_id TEXT NOT NULL,
1027
+ retrieval_score REAL DEFAULT 0,
1028
+ query_preview TEXT,
1029
+ session_continued INTEGER DEFAULT 0,
1030
+ prompt_count_after INTEGER DEFAULT 0,
1031
+ tool_success_count INTEGER DEFAULT 0,
1032
+ tool_total_count INTEGER DEFAULT 0,
1033
+ was_reasked INTEGER DEFAULT 0,
1034
+ helpfulness_score REAL DEFAULT 0.5,
1035
+ created_at TEXT DEFAULT (datetime('now')),
1036
+ measured_at TEXT
1037
+ );
1038
+
1012
1039
  -- Sync position tracking (for SQLite -> DuckDB sync)
1013
1040
  CREATE TABLE IF NOT EXISTS sync_positions (
1014
1041
  target_name TEXT PRIMARY KEY,
@@ -1034,6 +1061,31 @@ var SQLiteEventStore = class {
1034
1061
  CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
1035
1062
  CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
1036
1063
  CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
1064
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
1065
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
1066
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
1067
+
1068
+ -- FTS5 Full-Text Search for fast keyword search
1069
+ CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
1070
+ content,
1071
+ event_id UNINDEXED,
1072
+ content='events',
1073
+ content_rowid='rowid'
1074
+ );
1075
+
1076
+ -- Triggers to keep FTS in sync with events table
1077
+ CREATE TRIGGER IF NOT EXISTS events_fts_insert AFTER INSERT ON events BEGIN
1078
+ INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
1079
+ END;
1080
+
1081
+ CREATE TRIGGER IF NOT EXISTS events_fts_delete AFTER DELETE ON events BEGIN
1082
+ INSERT INTO events_fts(events_fts, rowid, content, event_id) VALUES('delete', OLD.rowid, OLD.content, OLD.id);
1083
+ END;
1084
+
1085
+ CREATE TRIGGER IF NOT EXISTS events_fts_update AFTER UPDATE ON events BEGIN
1086
+ INSERT INTO events_fts(events_fts, rowid, content, event_id) VALUES('delete', OLD.rowid, OLD.content, OLD.id);
1087
+ INSERT INTO events_fts(rowid, content, event_id) VALUES (NEW.rowid, NEW.content, NEW.id);
1088
+ END;
1037
1089
  `);
1038
1090
  const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
1039
1091
  const columnNames = tableInfo.map((col) => col.name);
@@ -1055,6 +1107,15 @@ var SQLiteEventStore = class {
1055
1107
  console.error("Error adding last_accessed_at column:", err);
1056
1108
  }
1057
1109
  }
1110
+ if (!columnNames.includes("turn_id")) {
1111
+ try {
1112
+ sqliteExec(this.db, `
1113
+ ALTER TABLE events ADD COLUMN turn_id TEXT;
1114
+ `);
1115
+ } catch (err) {
1116
+ console.error("Error adding turn_id column:", err);
1117
+ }
1118
+ }
1058
1119
  try {
1059
1120
  sqliteExec(this.db, `
1060
1121
  CREATE INDEX IF NOT EXISTS idx_events_access_count ON events(access_count DESC);
@@ -1067,6 +1128,12 @@ var SQLiteEventStore = class {
1067
1128
  `);
1068
1129
  } catch (err) {
1069
1130
  }
1131
+ try {
1132
+ sqliteExec(this.db, `
1133
+ CREATE INDEX IF NOT EXISTS idx_events_turn_id ON events(turn_id);
1134
+ `);
1135
+ } catch (err) {
1136
+ }
1070
1137
  this.initialized = true;
1071
1138
  }
1072
1139
  /**
@@ -1091,9 +1158,11 @@ var SQLiteEventStore = class {
1091
1158
  const id = randomUUID2();
1092
1159
  const timestamp = toSQLiteTimestamp(input.timestamp);
1093
1160
  try {
1161
+ const metadata = input.metadata || {};
1162
+ const turnId = metadata.turnId || null;
1094
1163
  const insertEvent = this.db.prepare(`
1095
- INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
1096
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1164
+ INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
1165
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1097
1166
  `);
1098
1167
  const insertDedup = this.db.prepare(`
1099
1168
  INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
@@ -1110,7 +1179,8 @@ var SQLiteEventStore = class {
1110
1179
  input.content,
1111
1180
  canonicalKey,
1112
1181
  dedupeKey,
1113
- JSON.stringify(input.metadata || {})
1182
+ JSON.stringify(metadata),
1183
+ turnId
1114
1184
  );
1115
1185
  insertDedup.run(dedupeKey, id);
1116
1186
  insertLevel.run(id);
@@ -1460,11 +1530,11 @@ var SQLiteEventStore = class {
1460
1530
  );
1461
1531
  }
1462
1532
  /**
1463
- * Get most accessed memories
1533
+ * Get most accessed memories (falls back to recent events if none accessed)
1464
1534
  */
1465
1535
  async getMostAccessed(limit = 10) {
1466
1536
  await this.initialize();
1467
- const rows = sqliteAll(
1537
+ let rows = sqliteAll(
1468
1538
  this.db,
1469
1539
  `SELECT * FROM events
1470
1540
  WHERE access_count > 0
@@ -1472,8 +1542,222 @@ var SQLiteEventStore = class {
1472
1542
  LIMIT ?`,
1473
1543
  [limit]
1474
1544
  );
1545
+ if (rows.length === 0) {
1546
+ rows = sqliteAll(
1547
+ this.db,
1548
+ `SELECT * FROM events
1549
+ ORDER BY timestamp DESC
1550
+ LIMIT ?`,
1551
+ [limit]
1552
+ );
1553
+ }
1475
1554
  return rows.map((row) => this.rowToEvent(row));
1476
1555
  }
1556
+ /**
1557
+ * Record a memory retrieval for helpfulness tracking
1558
+ */
1559
+ async recordRetrieval(eventId, sessionId, score, query) {
1560
+ if (this.readOnly)
1561
+ return;
1562
+ await this.initialize();
1563
+ const id = randomUUID2();
1564
+ sqliteRun(
1565
+ this.db,
1566
+ `INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
1567
+ VALUES (?, ?, ?, ?, ?, datetime('now'))`,
1568
+ [id, eventId, sessionId, score, query.slice(0, 100)]
1569
+ );
1570
+ }
1571
+ /**
1572
+ * Evaluate helpfulness for all retrievals in a session
1573
+ * Called at session end - uses behavioral signals to compute score
1574
+ */
1575
+ async evaluateSessionHelpfulness(sessionId) {
1576
+ if (this.readOnly)
1577
+ return;
1578
+ await this.initialize();
1579
+ const retrievals = sqliteAll(
1580
+ this.db,
1581
+ `SELECT * FROM memory_helpfulness WHERE session_id = ? AND measured_at IS NULL`,
1582
+ [sessionId]
1583
+ );
1584
+ if (retrievals.length === 0)
1585
+ return;
1586
+ const sessionEvents = sqliteAll(
1587
+ this.db,
1588
+ `SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
1589
+ [sessionId]
1590
+ );
1591
+ const promptEvents = sessionEvents.filter((e) => e.event_type === "user_prompt");
1592
+ const toolEvents = sessionEvents.filter((e) => e.event_type === "tool_observation");
1593
+ let toolSuccessCount = 0;
1594
+ let toolTotalCount = toolEvents.length;
1595
+ for (const t of toolEvents) {
1596
+ try {
1597
+ const content = JSON.parse(t.content);
1598
+ if (content.success !== false)
1599
+ toolSuccessCount++;
1600
+ } catch {
1601
+ toolSuccessCount++;
1602
+ }
1603
+ }
1604
+ const toolSuccessRatio = toolTotalCount > 0 ? toolSuccessCount / toolTotalCount : 0.5;
1605
+ for (const retrieval of retrievals) {
1606
+ const retrievalTime = retrieval.created_at;
1607
+ const eventsAfter = sessionEvents.filter((e) => e.timestamp > retrievalTime);
1608
+ const sessionContinued = eventsAfter.length > 0 ? 1 : 0;
1609
+ const promptsAfter = promptEvents.filter((e) => e.timestamp > retrievalTime);
1610
+ const promptCountAfter = promptsAfter.length;
1611
+ const queryWords = new Set((retrieval.query_preview || "").toLowerCase().split(/\s+/).filter((w) => w.length > 2));
1612
+ let wasReasked = 0;
1613
+ for (const p of promptsAfter) {
1614
+ const pWords = new Set(p.content.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
1615
+ let overlap = 0;
1616
+ for (const w of queryWords) {
1617
+ if (pWords.has(w))
1618
+ overlap++;
1619
+ }
1620
+ if (queryWords.size > 0 && overlap / queryWords.size > 0.5) {
1621
+ wasReasked = 1;
1622
+ break;
1623
+ }
1624
+ }
1625
+ const retrievalScore = retrieval.retrieval_score || 0;
1626
+ const helpfulnessScore = 0.3 * Math.min(retrievalScore, 1) + 0.25 * (sessionContinued ? 1 : 0) + 0.25 * toolSuccessRatio + 0.2 * (wasReasked ? 0 : 1);
1627
+ sqliteRun(
1628
+ this.db,
1629
+ `UPDATE memory_helpfulness
1630
+ SET session_continued = ?, prompt_count_after = ?,
1631
+ tool_success_count = ?, tool_total_count = ?,
1632
+ was_reasked = ?, helpfulness_score = ?,
1633
+ measured_at = datetime('now')
1634
+ WHERE id = ?`,
1635
+ [
1636
+ sessionContinued,
1637
+ promptCountAfter,
1638
+ toolSuccessCount,
1639
+ toolTotalCount,
1640
+ wasReasked,
1641
+ helpfulnessScore,
1642
+ retrieval.id
1643
+ ]
1644
+ );
1645
+ }
1646
+ }
1647
+ /**
1648
+ * Get most helpful memories ranked by helpfulness score
1649
+ */
1650
+ async getHelpfulMemories(limit = 10) {
1651
+ await this.initialize();
1652
+ const rows = sqliteAll(
1653
+ this.db,
1654
+ `SELECT
1655
+ mh.event_id,
1656
+ AVG(mh.helpfulness_score) as avg_score,
1657
+ COUNT(*) as eval_count,
1658
+ e.content,
1659
+ e.access_count
1660
+ FROM memory_helpfulness mh
1661
+ JOIN events e ON e.id = mh.event_id
1662
+ WHERE mh.measured_at IS NOT NULL
1663
+ GROUP BY mh.event_id
1664
+ ORDER BY avg_score DESC
1665
+ LIMIT ?`,
1666
+ [limit]
1667
+ );
1668
+ return rows.map((r) => ({
1669
+ eventId: r.event_id,
1670
+ summary: r.content.substring(0, 200) + (r.content.length > 200 ? "..." : ""),
1671
+ helpfulnessScore: Math.round(r.avg_score * 100) / 100,
1672
+ accessCount: r.access_count || 0,
1673
+ evaluationCount: r.eval_count
1674
+ }));
1675
+ }
1676
+ /**
1677
+ * Get helpfulness statistics for dashboard
1678
+ */
1679
+ async getHelpfulnessStats() {
1680
+ await this.initialize();
1681
+ const stats = sqliteGet(
1682
+ this.db,
1683
+ `SELECT
1684
+ AVG(helpfulness_score) as avg_score,
1685
+ COUNT(*) as total_evaluated,
1686
+ SUM(CASE WHEN helpfulness_score >= 0.7 THEN 1 ELSE 0 END) as helpful,
1687
+ SUM(CASE WHEN helpfulness_score >= 0.4 AND helpfulness_score < 0.7 THEN 1 ELSE 0 END) as neutral,
1688
+ SUM(CASE WHEN helpfulness_score < 0.4 THEN 1 ELSE 0 END) as unhelpful
1689
+ FROM memory_helpfulness
1690
+ WHERE measured_at IS NOT NULL`
1691
+ );
1692
+ const totalRow = sqliteGet(
1693
+ this.db,
1694
+ `SELECT COUNT(*) as total FROM memory_helpfulness`
1695
+ );
1696
+ return {
1697
+ avgScore: Math.round((stats?.avg_score || 0) * 100) / 100,
1698
+ totalEvaluated: stats?.total_evaluated || 0,
1699
+ totalRetrievals: totalRow?.total || 0,
1700
+ helpful: stats?.helpful || 0,
1701
+ neutral: stats?.neutral || 0,
1702
+ unhelpful: stats?.unhelpful || 0
1703
+ };
1704
+ }
1705
+ /**
1706
+ * Fast keyword search using FTS5
1707
+ * Returns events matching the search query, ranked by relevance
1708
+ */
1709
+ async keywordSearch(query, limit = 10) {
1710
+ await this.initialize();
1711
+ const searchTerms = query.replace(/['"(){}[\]^~*?:\\/-]/g, " ").split(/\s+/).filter((term) => term.length > 1).map((term) => `"${term}"*`).join(" OR ");
1712
+ if (!searchTerms) {
1713
+ return [];
1714
+ }
1715
+ try {
1716
+ const rows = sqliteAll(
1717
+ this.db,
1718
+ `SELECT e.*, fts.rank
1719
+ FROM events_fts fts
1720
+ JOIN events e ON e.id = fts.event_id
1721
+ WHERE events_fts MATCH ?
1722
+ ORDER BY fts.rank
1723
+ LIMIT ?`,
1724
+ [searchTerms, limit]
1725
+ );
1726
+ return rows.map((row) => ({
1727
+ event: this.rowToEvent(row),
1728
+ rank: row.rank
1729
+ }));
1730
+ } catch (error) {
1731
+ const likePattern = `%${query}%`;
1732
+ const rows = sqliteAll(
1733
+ this.db,
1734
+ `SELECT *, 0 as rank FROM events
1735
+ WHERE content LIKE ?
1736
+ ORDER BY timestamp DESC
1737
+ LIMIT ?`,
1738
+ [likePattern, limit]
1739
+ );
1740
+ return rows.map((row) => ({
1741
+ event: this.rowToEvent(row),
1742
+ rank: 0
1743
+ }));
1744
+ }
1745
+ }
1746
+ /**
1747
+ * Rebuild FTS index from existing events
1748
+ * Call this once after upgrading to FTS5
1749
+ */
1750
+ async rebuildFtsIndex() {
1751
+ await this.initialize();
1752
+ const countRow = sqliteGet(this.db, "SELECT COUNT(*) as count FROM events", []);
1753
+ const totalEvents = countRow?.count ?? 0;
1754
+ sqliteExec(this.db, `
1755
+ DELETE FROM events_fts;
1756
+ INSERT INTO events_fts(rowid, content, event_id)
1757
+ SELECT rowid, content, id FROM events;
1758
+ `);
1759
+ return totalEvents;
1760
+ }
1477
1761
  /**
1478
1762
  * Get database instance for direct access
1479
1763
  */
@@ -1486,6 +1770,143 @@ var SQLiteEventStore = class {
1486
1770
  async close() {
1487
1771
  sqliteClose(this.db);
1488
1772
  }
1773
+ /**
1774
+ * Get events grouped by turn_id for a session
1775
+ * Returns turns ordered by first event timestamp (newest first)
1776
+ */
1777
+ async getSessionTurns(sessionId, options) {
1778
+ await this.initialize();
1779
+ const limit = options?.limit || 20;
1780
+ const offset = options?.offset || 0;
1781
+ const turnRows = sqliteAll(
1782
+ this.db,
1783
+ `SELECT turn_id, MIN(timestamp) as min_ts
1784
+ FROM events
1785
+ WHERE session_id = ? AND turn_id IS NOT NULL
1786
+ GROUP BY turn_id
1787
+ ORDER BY min_ts DESC
1788
+ LIMIT ? OFFSET ?`,
1789
+ [sessionId, limit, offset]
1790
+ );
1791
+ const turns = [];
1792
+ for (const turnRow of turnRows) {
1793
+ const events = await this.getEventsByTurn(turnRow.turn_id);
1794
+ const promptEvent = events.find((e) => e.eventType === "user_prompt");
1795
+ const toolEvents = events.filter((e) => e.eventType === "tool_observation");
1796
+ const hasResponse = events.some((e) => e.eventType === "agent_response");
1797
+ turns.push({
1798
+ turnId: turnRow.turn_id,
1799
+ events,
1800
+ startedAt: toDateFromSQLite(turnRow.min_ts),
1801
+ promptPreview: promptEvent ? promptEvent.content.slice(0, 200) + (promptEvent.content.length > 200 ? "..." : "") : "(no prompt)",
1802
+ eventCount: events.length,
1803
+ toolCount: toolEvents.length,
1804
+ hasResponse
1805
+ });
1806
+ }
1807
+ return turns;
1808
+ }
1809
+ /**
1810
+ * Get all events for a specific turn_id
1811
+ */
1812
+ async getEventsByTurn(turnId) {
1813
+ await this.initialize();
1814
+ const rows = sqliteAll(
1815
+ this.db,
1816
+ `SELECT * FROM events WHERE turn_id = ? ORDER BY timestamp ASC`,
1817
+ [turnId]
1818
+ );
1819
+ return rows.map(this.rowToEvent);
1820
+ }
1821
+ /**
1822
+ * Count total turns for a session
1823
+ */
1824
+ async countSessionTurns(sessionId) {
1825
+ await this.initialize();
1826
+ const row = sqliteGet(
1827
+ this.db,
1828
+ `SELECT COUNT(DISTINCT turn_id) as count
1829
+ FROM events
1830
+ WHERE session_id = ? AND turn_id IS NOT NULL`,
1831
+ [sessionId]
1832
+ );
1833
+ return row?.count || 0;
1834
+ }
1835
+ /**
1836
+ * Migrate existing events: backfill turn_id for events that have turnId in metadata
1837
+ * but no turn_id column value (for events stored before this migration)
1838
+ */
1839
+ async backfillTurnIds() {
1840
+ await this.initialize();
1841
+ const rows = sqliteAll(
1842
+ this.db,
1843
+ `SELECT id, metadata FROM events
1844
+ WHERE turn_id IS NULL AND metadata IS NOT NULL AND metadata LIKE '%turnId%'`
1845
+ );
1846
+ let updated = 0;
1847
+ for (const row of rows) {
1848
+ try {
1849
+ const metadata = JSON.parse(row.metadata);
1850
+ if (metadata.turnId) {
1851
+ sqliteRun(
1852
+ this.db,
1853
+ `UPDATE events SET turn_id = ? WHERE id = ?`,
1854
+ [metadata.turnId, row.id]
1855
+ );
1856
+ updated++;
1857
+ }
1858
+ } catch {
1859
+ }
1860
+ }
1861
+ return updated;
1862
+ }
1863
+ /**
1864
+ * Delete all events for a session (for force reimport)
1865
+ */
1866
+ async deleteSessionEvents(sessionId) {
1867
+ await this.initialize();
1868
+ const events = sqliteAll(
1869
+ this.db,
1870
+ `SELECT id FROM events WHERE session_id = ?`,
1871
+ [sessionId]
1872
+ );
1873
+ if (events.length === 0)
1874
+ return 0;
1875
+ const eventIds = events.map((e) => e.id);
1876
+ const placeholders = eventIds.map(() => "?").join(",");
1877
+ const ftsTriggersDropped = [];
1878
+ for (const triggerName of ["events_fts_delete", "events_fts_update", "events_fts_insert"]) {
1879
+ try {
1880
+ sqliteRun(this.db, `DROP TRIGGER IF EXISTS ${triggerName}`);
1881
+ ftsTriggersDropped.push(triggerName);
1882
+ } catch {
1883
+ }
1884
+ }
1885
+ for (const table of ["event_dedup", "memory_levels", "embedding_queue", "embedding_outbox", "vector_outbox"]) {
1886
+ try {
1887
+ sqliteRun(this.db, `DELETE FROM ${table} WHERE event_id IN (${placeholders})`, eventIds);
1888
+ } catch {
1889
+ }
1890
+ }
1891
+ const result = sqliteRun(this.db, `DELETE FROM events WHERE session_id = ?`, [sessionId]);
1892
+ if (ftsTriggersDropped.length > 0) {
1893
+ try {
1894
+ sqliteRun(this.db, `INSERT INTO events_fts(events_fts) VALUES('rebuild')`);
1895
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_insert AFTER INSERT ON events BEGIN
1896
+ INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
1897
+ END`);
1898
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_delete AFTER DELETE ON events BEGIN
1899
+ INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
1900
+ END`);
1901
+ sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_update AFTER UPDATE ON events BEGIN
1902
+ INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
1903
+ INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
1904
+ END`);
1905
+ } catch {
1906
+ }
1907
+ }
1908
+ return result.changes || 0;
1909
+ }
1489
1910
  /**
1490
1911
  * Convert database row to MemoryEvent
1491
1912
  */
@@ -1506,6 +1927,9 @@ var SQLiteEventStore = class {
1506
1927
  if (row.last_accessed_at !== void 0) {
1507
1928
  event.last_accessed_at = row.last_accessed_at;
1508
1929
  }
1930
+ if (row.turn_id !== void 0 && row.turn_id !== null) {
1931
+ event.turn_id = row.turn_id;
1932
+ }
1509
1933
  return event;
1510
1934
  }
1511
1935
  };
@@ -1717,7 +2141,16 @@ var VectorStore = class {
1717
2141
  metadata: JSON.stringify(record.metadata || {})
1718
2142
  };
1719
2143
  if (!this.table) {
1720
- this.table = await this.db.createTable(this.tableName, [data]);
2144
+ try {
2145
+ this.table = await this.db.createTable(this.tableName, [data]);
2146
+ } catch (e) {
2147
+ if (e?.message?.includes("already exists")) {
2148
+ this.table = await this.db.openTable(this.tableName);
2149
+ await this.table.add([data]);
2150
+ } else {
2151
+ throw e;
2152
+ }
2153
+ }
1721
2154
  } else {
1722
2155
  await this.table.add([data]);
1723
2156
  }
@@ -1743,7 +2176,16 @@ var VectorStore = class {
1743
2176
  metadata: JSON.stringify(record.metadata || {})
1744
2177
  }));
1745
2178
  if (!this.table) {
1746
- this.table = await this.db.createTable(this.tableName, data);
2179
+ try {
2180
+ this.table = await this.db.createTable(this.tableName, data);
2181
+ } catch (e) {
2182
+ if (e?.message?.includes("already exists")) {
2183
+ this.table = await this.db.openTable(this.tableName);
2184
+ await this.table.add(data);
2185
+ } else {
2186
+ throw e;
2187
+ }
2188
+ }
1747
2189
  } else {
1748
2190
  await this.table.add(data);
1749
2191
  }
@@ -4489,7 +4931,7 @@ function createGraduationWorker(eventStore, graduation, config) {
4489
4931
  function normalizePath(projectPath) {
4490
4932
  const expanded = projectPath.startsWith("~") ? path.join(os.homedir(), projectPath.slice(1)) : projectPath;
4491
4933
  try {
4492
- return fs.realpathSync(expanded);
4934
+ return fs2.realpathSync(expanded);
4493
4935
  } catch {
4494
4936
  return path.resolve(expanded);
4495
4937
  }
@@ -4506,8 +4948,8 @@ var REGISTRY_PATH = path.join(os.homedir(), ".claude-code", "memory", "session-r
4506
4948
  var SHARED_STORAGE_PATH = path.join(os.homedir(), ".claude-code", "memory", "shared");
4507
4949
  function loadSessionRegistry() {
4508
4950
  try {
4509
- if (fs.existsSync(REGISTRY_PATH)) {
4510
- const data = fs.readFileSync(REGISTRY_PATH, "utf-8");
4951
+ if (fs2.existsSync(REGISTRY_PATH)) {
4952
+ const data = fs2.readFileSync(REGISTRY_PATH, "utf-8");
4511
4953
  return JSON.parse(data);
4512
4954
  }
4513
4955
  } catch (error) {
@@ -4547,11 +4989,13 @@ var MemoryService = class {
4547
4989
  sharedStoreConfig = null;
4548
4990
  projectHash = null;
4549
4991
  readOnly;
4992
+ lightweightMode;
4550
4993
  constructor(config) {
4551
4994
  const storagePath = this.expandPath(config.storagePath);
4552
4995
  this.readOnly = config.readOnly ?? false;
4553
- if (!this.readOnly && !fs.existsSync(storagePath)) {
4554
- fs.mkdirSync(storagePath, { recursive: true });
4996
+ this.lightweightMode = config.lightweightMode ?? false;
4997
+ if (!this.readOnly && !fs2.existsSync(storagePath)) {
4998
+ fs2.mkdirSync(storagePath, { recursive: true });
4555
4999
  }
4556
5000
  this.projectHash = config.projectHash || null;
4557
5001
  this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
@@ -4596,6 +5040,10 @@ var MemoryService = class {
4596
5040
  if (this.initialized)
4597
5041
  return;
4598
5042
  await this.sqliteStore.initialize();
5043
+ if (this.lightweightMode) {
5044
+ this.initialized = true;
5045
+ return;
5046
+ }
4599
5047
  if (this.analyticsStore) {
4600
5048
  try {
4601
5049
  await this.analyticsStore.initialize();
@@ -4642,8 +5090,8 @@ var MemoryService = class {
4642
5090
  */
4643
5091
  async initializeSharedStore() {
4644
5092
  const sharedPath = this.sharedStoreConfig?.sharedStoragePath ? this.expandPath(this.sharedStoreConfig.sharedStoragePath) : SHARED_STORAGE_PATH;
4645
- if (!fs.existsSync(sharedPath)) {
4646
- fs.mkdirSync(sharedPath, { recursive: true });
5093
+ if (!fs2.existsSync(sharedPath)) {
5094
+ fs2.mkdirSync(sharedPath, { recursive: true });
4647
5095
  }
4648
5096
  this.sharedEventStore = createSharedEventStore(
4649
5097
  path.join(sharedPath, "shared.duckdb")
@@ -4740,6 +5188,7 @@ var MemoryService = class {
4740
5188
  async storeToolObservation(sessionId, payload) {
4741
5189
  await this.initialize();
4742
5190
  const content = JSON.stringify(payload);
5191
+ const turnId = payload.metadata?.turnId;
4743
5192
  const result = await this.sqliteStore.append({
4744
5193
  eventType: "tool_observation",
4745
5194
  sessionId,
@@ -4747,7 +5196,8 @@ var MemoryService = class {
4747
5196
  content,
4748
5197
  metadata: {
4749
5198
  toolName: payload.toolName,
4750
- success: payload.success
5199
+ success: payload.success,
5200
+ ...turnId ? { turnId } : {}
4751
5201
  }
4752
5202
  });
4753
5203
  if (result.success && !result.isDuplicate) {
@@ -4765,9 +5215,6 @@ var MemoryService = class {
4765
5215
  */
4766
5216
  async retrieveMemories(query, options) {
4767
5217
  await this.initialize();
4768
- if (this.vectorWorker) {
4769
- await this.vectorWorker.processAll();
4770
- }
4771
5218
  if (options?.includeShared && this.sharedStore) {
4772
5219
  return this.retriever.retrieveUnified(query, {
4773
5220
  ...options,
@@ -4777,6 +5224,29 @@ var MemoryService = class {
4777
5224
  }
4778
5225
  return this.retriever.retrieve(query, options);
4779
5226
  }
5227
+ /**
5228
+ * Fast keyword search using SQLite FTS5
5229
+ * Much faster than vector search - no embedding model needed
5230
+ */
5231
+ async keywordSearch(query, options) {
5232
+ await this.initialize();
5233
+ const results = await this.sqliteStore.keywordSearch(query, options?.topK ?? 10);
5234
+ const maxRank = Math.min(...results.map((r) => r.rank), -1e-3);
5235
+ const minRank = Math.max(...results.map((r) => r.rank), -1e3);
5236
+ const rankRange = maxRank - minRank || 1;
5237
+ return results.map((r) => ({
5238
+ event: r.event,
5239
+ score: 1 - (r.rank - minRank) / rankRange
5240
+ // Normalize to 0-1
5241
+ })).filter((r) => !options?.minScore || r.score >= options.minScore);
5242
+ }
5243
+ /**
5244
+ * Rebuild FTS index (call after database upgrade)
5245
+ */
5246
+ async rebuildFtsIndex() {
5247
+ await this.initialize();
5248
+ return this.sqliteStore.rebuildFtsIndex();
5249
+ }
4780
5250
  /**
4781
5251
  * Get session history
4782
5252
  */
@@ -5010,6 +5480,31 @@ var MemoryService = class {
5010
5480
  return [];
5011
5481
  return this.consolidatedStore.getAll({ limit });
5012
5482
  }
5483
+ /**
5484
+ * Extract topic keywords from event content (markdown headings and key terms)
5485
+ */
5486
+ extractTopicsFromContent(content) {
5487
+ const topics = /* @__PURE__ */ new Set();
5488
+ const headings = content.match(/^#{1,3}\s+(.+)$/gm);
5489
+ if (headings) {
5490
+ for (const h of headings.slice(0, 5)) {
5491
+ const text = h.replace(/^#+\s+/, "").replace(/[*_`#]/g, "").trim();
5492
+ if (text.length > 2 && text.length < 50) {
5493
+ topics.add(text);
5494
+ }
5495
+ }
5496
+ }
5497
+ const boldTerms = content.match(/\*\*([^*]+)\*\*/g);
5498
+ if (boldTerms) {
5499
+ for (const b of boldTerms.slice(0, 5)) {
5500
+ const text = b.replace(/\*\*/g, "").trim();
5501
+ if (text.length > 2 && text.length < 30) {
5502
+ topics.add(text);
5503
+ }
5504
+ }
5505
+ }
5506
+ return Array.from(topics).slice(0, 5);
5507
+ }
5013
5508
  /**
5014
5509
  * Increment access count for memories that were used in prompts
5015
5510
  */
@@ -5033,8 +5528,7 @@ var MemoryService = class {
5033
5528
  return events.map((event) => ({
5034
5529
  memoryId: event.id,
5035
5530
  summary: event.content.substring(0, 200) + (event.content.length > 200 ? "..." : ""),
5036
- topics: [],
5037
- // Could extract topics from content if needed
5531
+ topics: this.extractTopicsFromContent(event.content),
5038
5532
  accessCount: event.access_count || 0,
5039
5533
  lastAccessed: event.last_accessed_at || null,
5040
5534
  confidence: 1,
@@ -5055,6 +5549,34 @@ var MemoryService = class {
5055
5549
  }
5056
5550
  return [];
5057
5551
  }
5552
+ /**
5553
+ * Record a memory retrieval for helpfulness tracking
5554
+ */
5555
+ async recordRetrieval(eventId, sessionId, score, query) {
5556
+ await this.initialize();
5557
+ await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
5558
+ }
5559
+ /**
5560
+ * Evaluate helpfulness of retrievals in a session (called at session end)
5561
+ */
5562
+ async evaluateSessionHelpfulness(sessionId) {
5563
+ await this.initialize();
5564
+ await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
5565
+ }
5566
+ /**
5567
+ * Get most helpful memories ranked by helpfulness score
5568
+ */
5569
+ async getHelpfulMemories(limit = 10) {
5570
+ await this.initialize();
5571
+ return this.sqliteStore.getHelpfulMemories(limit);
5572
+ }
5573
+ /**
5574
+ * Get helpfulness statistics for dashboard
5575
+ */
5576
+ async getHelpfulnessStats() {
5577
+ await this.initialize();
5578
+ return this.sqliteStore.getHelpfulnessStats();
5579
+ }
5058
5580
  /**
5059
5581
  * Mark a consolidated memory as accessed
5060
5582
  */
@@ -5118,6 +5640,44 @@ var MemoryService = class {
5118
5640
  lastConsolidation
5119
5641
  };
5120
5642
  }
5643
+ // ============================================================
5644
+ // Turn Grouping Methods
5645
+ // ============================================================
5646
+ /**
5647
+ * Get events grouped by turn for a session
5648
+ */
5649
+ async getSessionTurns(sessionId, options) {
5650
+ await this.initialize();
5651
+ return this.sqliteStore.getSessionTurns(sessionId, options);
5652
+ }
5653
+ /**
5654
+ * Get all events for a specific turn
5655
+ */
5656
+ async getEventsByTurn(turnId) {
5657
+ await this.initialize();
5658
+ return this.sqliteStore.getEventsByTurn(turnId);
5659
+ }
5660
+ /**
5661
+ * Count total turns for a session
5662
+ */
5663
+ async countSessionTurns(sessionId) {
5664
+ await this.initialize();
5665
+ return this.sqliteStore.countSessionTurns(sessionId);
5666
+ }
5667
+ /**
5668
+ * Backfill turn_ids from metadata for events stored before the migration
5669
+ */
5670
+ async backfillTurnIds() {
5671
+ await this.initialize();
5672
+ return this.sqliteStore.backfillTurnIds();
5673
+ }
5674
+ /**
5675
+ * Delete all events for a session (for force reimport)
5676
+ */
5677
+ async deleteSessionEvents(sessionId) {
5678
+ await this.initialize();
5679
+ return this.sqliteStore.deleteSessionEvents(sessionId);
5680
+ }
5121
5681
  /**
5122
5682
  * Format Endless Mode context for Claude
5123
5683
  */
@@ -5200,40 +5760,21 @@ var MemoryService = class {
5200
5760
  }
5201
5761
  };
5202
5762
  var serviceCache = /* @__PURE__ */ new Map();
5203
- var GLOBAL_KEY = "__global__";
5204
- function getDefaultMemoryService() {
5205
- if (!serviceCache.has(GLOBAL_KEY)) {
5206
- serviceCache.set(GLOBAL_KEY, new MemoryService({
5207
- storagePath: "~/.claude-code/memory",
5763
+ function getLightweightMemoryService(sessionId) {
5764
+ const projectInfo = getSessionProject(sessionId);
5765
+ const key = projectInfo ? `lightweight_${projectInfo.projectHash}` : "lightweight_global";
5766
+ if (!serviceCache.has(key)) {
5767
+ const storagePath = projectInfo ? getProjectStoragePath(projectInfo.projectPath) : path.join(os.homedir(), ".claude-code", "memory");
5768
+ serviceCache.set(key, new MemoryService({
5769
+ storagePath,
5770
+ projectHash: projectInfo?.projectHash,
5771
+ lightweightMode: true,
5772
+ // Skip embedder/vector/workers
5208
5773
  analyticsEnabled: false,
5209
- // Hooks don't need DuckDB
5210
5774
  sharedStoreConfig: { enabled: false }
5211
- // Shared store uses DuckDB too
5212
- }));
5213
- }
5214
- return serviceCache.get(GLOBAL_KEY);
5215
- }
5216
- function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
5217
- const hash = hashProjectPath(projectPath);
5218
- if (!serviceCache.has(hash)) {
5219
- const storagePath = getProjectStoragePath(projectPath);
5220
- serviceCache.set(hash, new MemoryService({
5221
- storagePath,
5222
- projectHash: hash,
5223
- // Override shared store config - hooks don't need DuckDB
5224
- sharedStoreConfig: sharedStoreConfig ?? { enabled: false },
5225
- analyticsEnabled: false
5226
- // Hooks don't need DuckDB
5227
5775
  }));
5228
5776
  }
5229
- return serviceCache.get(hash);
5230
- }
5231
- function getMemoryServiceForSession(sessionId) {
5232
- const projectInfo = getSessionProject(sessionId);
5233
- if (projectInfo) {
5234
- return getMemoryServiceForProject(projectInfo.projectPath);
5235
- }
5236
- return getDefaultMemoryService();
5777
+ return serviceCache.get(key);
5237
5778
  }
5238
5779
 
5239
5780
  // src/core/privacy/tag-parser.ts
@@ -5381,6 +5922,52 @@ function applyPrivacyFilter(content, config) {
5381
5922
  };
5382
5923
  }
5383
5924
 
5925
+ // src/core/turn-state.ts
5926
+ import * as fs3 from "fs";
5927
+ import * as path2 from "path";
5928
+ import * as os2 from "os";
5929
+ var TURN_STATE_DIR = path2.join(os2.homedir(), ".claude-code", "memory");
5930
+ function getStatePath(sessionId) {
5931
+ return path2.join(TURN_STATE_DIR, `.turn-state-${sessionId}.json`);
5932
+ }
5933
+ function readTurnState(sessionId) {
5934
+ try {
5935
+ const filePath = getStatePath(sessionId);
5936
+ if (!fs3.existsSync(filePath)) {
5937
+ return null;
5938
+ }
5939
+ const data = fs3.readFileSync(filePath, "utf-8");
5940
+ const state = JSON.parse(data);
5941
+ if (state.sessionId !== sessionId) {
5942
+ return null;
5943
+ }
5944
+ const createdAt = new Date(state.createdAt).getTime();
5945
+ const now = Date.now();
5946
+ if (now - createdAt > 30 * 60 * 1e3) {
5947
+ clearTurnState(sessionId);
5948
+ return null;
5949
+ }
5950
+ return state.turnId;
5951
+ } catch (error) {
5952
+ if (process.env.CLAUDE_MEMORY_DEBUG) {
5953
+ console.error("Failed to read turn state:", error);
5954
+ }
5955
+ return null;
5956
+ }
5957
+ }
5958
+ function clearTurnState(sessionId) {
5959
+ try {
5960
+ const filePath = getStatePath(sessionId);
5961
+ if (fs3.existsSync(filePath)) {
5962
+ fs3.unlinkSync(filePath);
5963
+ }
5964
+ } catch (error) {
5965
+ if (process.env.CLAUDE_MEMORY_DEBUG) {
5966
+ console.error("Failed to clear turn state:", error);
5967
+ }
5968
+ }
5969
+ }
5970
+
5384
5971
  // src/hooks/stop.ts
5385
5972
  var DEFAULT_PRIVACY_CONFIG = {
5386
5973
  excludePatterns: ["password", "secret", "api_key", "token", "bearer"],
@@ -5392,32 +5979,65 @@ var DEFAULT_PRIVACY_CONFIG = {
5392
5979
  supportedFormats: ["xml"]
5393
5980
  }
5394
5981
  };
5982
+ async function extractAssistantMessages(transcriptPath) {
5983
+ if (!fs4.existsSync(transcriptPath))
5984
+ return [];
5985
+ const messages = [];
5986
+ const stats = fs4.statSync(transcriptPath);
5987
+ const readStart = Math.max(0, stats.size - 200 * 1024);
5988
+ const stream = fs4.createReadStream(transcriptPath, {
5989
+ start: readStart,
5990
+ encoding: "utf8"
5991
+ });
5992
+ const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
5993
+ for await (const line of rl) {
5994
+ try {
5995
+ const entry = JSON.parse(line);
5996
+ if (entry.type !== "assistant")
5997
+ continue;
5998
+ const content = entry.message?.content;
5999
+ if (!Array.isArray(content))
6000
+ continue;
6001
+ const textParts = content.filter((c) => c.type === "text").map((c) => c.text).filter(Boolean);
6002
+ if (textParts.length > 0) {
6003
+ messages.push(textParts.join("\n"));
6004
+ }
6005
+ } catch {
6006
+ }
6007
+ }
6008
+ return messages;
6009
+ }
5395
6010
  async function main() {
5396
6011
  const inputData = await readStdin();
5397
6012
  const input = JSON.parse(inputData);
5398
- const memoryService = getMemoryServiceForSession(input.session_id);
6013
+ const memoryService = getLightweightMemoryService(input.session_id);
5399
6014
  try {
5400
- for (const message of input.messages) {
5401
- if (message.role === "assistant" && message.content) {
5402
- const filterResult = applyPrivacyFilter(message.content, DEFAULT_PRIVACY_CONFIG);
5403
- let content = filterResult.content;
5404
- if (content.length > 5e3) {
5405
- content = content.slice(0, 5e3) + "...[truncated]";
5406
- }
5407
- await memoryService.storeAgentResponse(
5408
- input.session_id,
5409
- content,
5410
- {
5411
- stopReason: input.stop_reason,
5412
- privacy: filterResult.metadata
5413
- }
5414
- );
6015
+ const turnId = readTurnState(input.session_id);
6016
+ const assistantMessages = await extractAssistantMessages(input.transcript_path);
6017
+ for (const text of assistantMessages) {
6018
+ const filterResult = applyPrivacyFilter(text, DEFAULT_PRIVACY_CONFIG);
6019
+ let content = filterResult.content;
6020
+ if (content.length > 5e3) {
6021
+ content = content.slice(0, 5e3) + "...[truncated]";
5415
6022
  }
6023
+ if (content.trim().length < 10)
6024
+ continue;
6025
+ await memoryService.storeAgentResponse(
6026
+ input.session_id,
6027
+ content,
6028
+ {
6029
+ privacy: filterResult.metadata,
6030
+ ...turnId ? { turnId } : {}
6031
+ }
6032
+ );
5416
6033
  }
6034
+ clearTurnState(input.session_id);
5417
6035
  await memoryService.processPendingEmbeddings();
5418
6036
  console.log(JSON.stringify({}));
5419
6037
  } catch (error) {
5420
- console.error("Memory hook error:", error);
6038
+ if (process.env.CLAUDE_MEMORY_DEBUG) {
6039
+ console.error("Stop hook error:", error);
6040
+ }
5421
6041
  console.log(JSON.stringify({}));
5422
6042
  }
5423
6043
  }