cortex-mcp 2.7.6 → 2.9.0

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 (202) hide show
  1. package/CHANGELOG.md +305 -58
  2. package/README.md +394 -224
  3. package/dist/cli/setup.js +58 -60
  4. package/dist/cli/setup.js.map +1 -1
  5. package/dist/config/config.js +2 -2
  6. package/dist/config/config.js.map +1 -1
  7. package/dist/db/database.d.ts +8 -0
  8. package/dist/db/database.d.ts.map +1 -1
  9. package/dist/db/database.js +35 -54
  10. package/dist/db/database.js.map +1 -1
  11. package/dist/db/event-log.d.ts +4 -0
  12. package/dist/db/event-log.d.ts.map +1 -1
  13. package/dist/db/event-log.js +14 -10
  14. package/dist/db/event-log.js.map +1 -1
  15. package/dist/db/memory-store.d.ts +30 -3
  16. package/dist/db/memory-store.d.ts.map +1 -1
  17. package/dist/db/memory-store.js +153 -55
  18. package/dist/db/memory-store.js.map +1 -1
  19. package/dist/embedding-worker.js +1 -1
  20. package/dist/embedding-worker.js.map +1 -1
  21. package/dist/hooks/git-capture.js +3 -3
  22. package/dist/hooks/git-hooks.js +5 -2
  23. package/dist/mcp-stdio.js +39 -5
  24. package/dist/mcp-stdio.js.map +1 -1
  25. package/dist/memory/access-pattern-tracker.d.ts +51 -0
  26. package/dist/memory/access-pattern-tracker.d.ts.map +1 -0
  27. package/dist/memory/access-pattern-tracker.js +92 -0
  28. package/dist/memory/access-pattern-tracker.js.map +1 -0
  29. package/dist/memory/anticipation-engine.d.ts.map +1 -1
  30. package/dist/memory/anticipation-engine.js +18 -10
  31. package/dist/memory/anticipation-engine.js.map +1 -1
  32. package/dist/memory/auto-learner.d.ts.map +1 -1
  33. package/dist/memory/auto-learner.js +192 -45
  34. package/dist/memory/auto-learner.js.map +1 -1
  35. package/dist/memory/completion-resolver.d.ts +38 -0
  36. package/dist/memory/completion-resolver.d.ts.map +1 -0
  37. package/dist/memory/completion-resolver.js +127 -0
  38. package/dist/memory/completion-resolver.js.map +1 -0
  39. package/dist/memory/confidence-decay.d.ts.map +1 -1
  40. package/dist/memory/confidence-decay.js +13 -9
  41. package/dist/memory/confidence-decay.js.map +1 -1
  42. package/dist/memory/convention-detector.d.ts +11 -0
  43. package/dist/memory/convention-detector.d.ts.map +1 -0
  44. package/dist/memory/convention-detector.js +294 -0
  45. package/dist/memory/convention-detector.js.map +1 -0
  46. package/dist/memory/correction-detector.d.ts +33 -0
  47. package/dist/memory/correction-detector.d.ts.map +1 -0
  48. package/dist/memory/correction-detector.js +129 -0
  49. package/dist/memory/correction-detector.js.map +1 -0
  50. package/dist/memory/cross-memory-linker.d.ts +18 -0
  51. package/dist/memory/cross-memory-linker.d.ts.map +1 -0
  52. package/dist/memory/cross-memory-linker.js +115 -0
  53. package/dist/memory/cross-memory-linker.js.map +1 -0
  54. package/dist/memory/daily-diary.d.ts +30 -0
  55. package/dist/memory/daily-diary.d.ts.map +1 -0
  56. package/dist/memory/daily-diary.js +159 -0
  57. package/dist/memory/daily-diary.js.map +1 -0
  58. package/dist/memory/embedding-cache.d.ts +32 -0
  59. package/dist/memory/embedding-cache.d.ts.map +1 -0
  60. package/dist/memory/embedding-cache.js +76 -0
  61. package/dist/memory/embedding-cache.js.map +1 -0
  62. package/dist/memory/embedding-manager.d.ts.map +1 -1
  63. package/dist/memory/embedding-manager.js +6 -4
  64. package/dist/memory/embedding-manager.js.map +1 -1
  65. package/dist/memory/error-learner.d.ts +26 -0
  66. package/dist/memory/error-learner.d.ts.map +1 -0
  67. package/dist/memory/error-learner.js +145 -0
  68. package/dist/memory/error-learner.js.map +1 -0
  69. package/dist/memory/export-import.js +2 -2
  70. package/dist/memory/export-import.js.map +1 -1
  71. package/dist/memory/file-relationships.d.ts +47 -0
  72. package/dist/memory/file-relationships.d.ts.map +1 -0
  73. package/dist/memory/file-relationships.js +130 -0
  74. package/dist/memory/file-relationships.js.map +1 -0
  75. package/dist/memory/git-memory.d.ts.map +1 -1
  76. package/dist/memory/git-memory.js +20 -26
  77. package/dist/memory/git-memory.js.map +1 -1
  78. package/dist/memory/impact-analyzer.d.ts +16 -0
  79. package/dist/memory/impact-analyzer.d.ts.map +1 -0
  80. package/dist/memory/impact-analyzer.js +189 -0
  81. package/dist/memory/impact-analyzer.js.map +1 -0
  82. package/dist/memory/instructions-generator.d.ts +30 -0
  83. package/dist/memory/instructions-generator.d.ts.map +1 -0
  84. package/dist/memory/instructions-generator.js +117 -0
  85. package/dist/memory/instructions-generator.js.map +1 -0
  86. package/dist/memory/learning-rate.js +8 -7
  87. package/dist/memory/learning-rate.js.map +1 -1
  88. package/dist/memory/llm-enhancer.d.ts +2 -14
  89. package/dist/memory/llm-enhancer.d.ts.map +1 -1
  90. package/dist/memory/llm-enhancer.js +66 -46
  91. package/dist/memory/llm-enhancer.js.map +1 -1
  92. package/dist/memory/memory-cache.d.ts.map +1 -1
  93. package/dist/memory/memory-cache.js +10 -0
  94. package/dist/memory/memory-cache.js.map +1 -1
  95. package/dist/memory/memory-consolidator.d.ts.map +1 -1
  96. package/dist/memory/memory-consolidator.js +20 -14
  97. package/dist/memory/memory-consolidator.js.map +1 -1
  98. package/dist/memory/memory-decay.d.ts.map +1 -1
  99. package/dist/memory/memory-decay.js +82 -52
  100. package/dist/memory/memory-decay.js.map +1 -1
  101. package/dist/memory/memory-export-md.d.ts +12 -0
  102. package/dist/memory/memory-export-md.d.ts.map +1 -0
  103. package/dist/memory/memory-export-md.js +188 -0
  104. package/dist/memory/memory-export-md.js.map +1 -0
  105. package/dist/memory/memory-quality.d.ts +1 -1
  106. package/dist/memory/memory-quality.d.ts.map +1 -1
  107. package/dist/memory/memory-quality.js +9 -6
  108. package/dist/memory/memory-quality.js.map +1 -1
  109. package/dist/memory/memory-ranker.d.ts.map +1 -1
  110. package/dist/memory/memory-ranker.js +12 -3
  111. package/dist/memory/memory-ranker.js.map +1 -1
  112. package/dist/memory/meta-memory.js +3 -3
  113. package/dist/memory/meta-memory.js.map +1 -1
  114. package/dist/memory/mmr-reranker.d.ts +39 -0
  115. package/dist/memory/mmr-reranker.d.ts.map +1 -0
  116. package/dist/memory/mmr-reranker.js +115 -0
  117. package/dist/memory/mmr-reranker.js.map +1 -0
  118. package/dist/memory/pre-flight.d.ts +24 -0
  119. package/dist/memory/pre-flight.d.ts.map +1 -0
  120. package/dist/memory/pre-flight.js +121 -0
  121. package/dist/memory/pre-flight.js.map +1 -0
  122. package/dist/memory/preference-learner.d.ts +28 -0
  123. package/dist/memory/preference-learner.d.ts.map +1 -0
  124. package/dist/memory/preference-learner.js +144 -0
  125. package/dist/memory/preference-learner.js.map +1 -0
  126. package/dist/memory/query-expansion.d.ts +28 -0
  127. package/dist/memory/query-expansion.d.ts.map +1 -0
  128. package/dist/memory/query-expansion.js +140 -0
  129. package/dist/memory/query-expansion.js.map +1 -0
  130. package/dist/memory/regression-guard.d.ts +35 -0
  131. package/dist/memory/regression-guard.d.ts.map +1 -0
  132. package/dist/memory/regression-guard.js +90 -0
  133. package/dist/memory/regression-guard.js.map +1 -0
  134. package/dist/memory/resume-work.d.ts +37 -0
  135. package/dist/memory/resume-work.d.ts.map +1 -0
  136. package/dist/memory/resume-work.js +141 -0
  137. package/dist/memory/resume-work.js.map +1 -0
  138. package/dist/memory/session-tracker.d.ts +2 -0
  139. package/dist/memory/session-tracker.d.ts.map +1 -1
  140. package/dist/memory/session-tracker.js +26 -8
  141. package/dist/memory/session-tracker.js.map +1 -1
  142. package/dist/memory/soul-manager.d.ts +30 -0
  143. package/dist/memory/soul-manager.d.ts.map +1 -0
  144. package/dist/memory/soul-manager.js +171 -0
  145. package/dist/memory/soul-manager.js.map +1 -0
  146. package/dist/memory/success-tracker.d.ts +33 -0
  147. package/dist/memory/success-tracker.d.ts.map +1 -0
  148. package/dist/memory/success-tracker.js +75 -0
  149. package/dist/memory/success-tracker.js.map +1 -0
  150. package/dist/memory/temporal-engine.d.ts.map +1 -1
  151. package/dist/memory/temporal-engine.js +9 -13
  152. package/dist/memory/temporal-engine.js.map +1 -1
  153. package/dist/memory/tool-recommender.d.ts +29 -0
  154. package/dist/memory/tool-recommender.d.ts.map +1 -0
  155. package/dist/memory/tool-recommender.js +117 -0
  156. package/dist/memory/tool-recommender.js.map +1 -0
  157. package/dist/memory/usage-stats.d.ts +98 -0
  158. package/dist/memory/usage-stats.d.ts.map +1 -0
  159. package/dist/memory/usage-stats.js +345 -0
  160. package/dist/memory/usage-stats.js.map +1 -0
  161. package/dist/retrieval/hybrid-retriever.d.ts +0 -2
  162. package/dist/retrieval/hybrid-retriever.d.ts.map +1 -1
  163. package/dist/retrieval/hybrid-retriever.js +3 -13
  164. package/dist/retrieval/hybrid-retriever.js.map +1 -1
  165. package/dist/scanners/architecture-graph.js +2 -2
  166. package/dist/scanners/architecture-graph.js.map +1 -1
  167. package/dist/scanners/code-verifier.d.ts +1 -0
  168. package/dist/scanners/code-verifier.d.ts.map +1 -1
  169. package/dist/scanners/code-verifier.js +14 -14
  170. package/dist/scanners/code-verifier.js.map +1 -1
  171. package/dist/scanners/context-builder.d.ts.map +1 -1
  172. package/dist/scanners/context-builder.js +33 -45
  173. package/dist/scanners/context-builder.js.map +1 -1
  174. package/dist/scanners/export-map.js +2 -2
  175. package/dist/scanners/export-map.js.map +1 -1
  176. package/dist/scanners/project-scanner.js +2 -2
  177. package/dist/scanners/project-scanner.js.map +1 -1
  178. package/dist/security/encryption.js +1 -1
  179. package/dist/security/encryption.js.map +1 -1
  180. package/dist/security/feature-gate.d.ts.map +1 -1
  181. package/dist/security/feature-gate.js +62 -20
  182. package/dist/security/feature-gate.js.map +1 -1
  183. package/dist/security/license.js +282 -35
  184. package/dist/security/license.js.map +1 -1
  185. package/dist/security/rate-limiter.d.ts +4 -3
  186. package/dist/security/rate-limiter.d.ts.map +1 -1
  187. package/dist/security/rate-limiter.js +11 -29
  188. package/dist/security/rate-limiter.js.map +1 -1
  189. package/dist/server/dashboard.js +166 -327
  190. package/dist/server/dashboard.js.map +1 -1
  191. package/dist/server/mcp-handler.d.ts.map +1 -1
  192. package/dist/server/mcp-handler.js +1000 -792
  193. package/dist/server/mcp-handler.js.map +1 -1
  194. package/dist/utils/extract-tags.d.ts +16 -0
  195. package/dist/utils/extract-tags.d.ts.map +1 -0
  196. package/dist/utils/extract-tags.js +40 -0
  197. package/dist/utils/extract-tags.js.map +1 -0
  198. package/package.json +18 -8
  199. package/dist/core/event-bus.d.ts +0 -19
  200. package/dist/core/event-bus.d.ts.map +0 -1
  201. package/dist/core/event-bus.js +0 -51
  202. package/dist/core/event-bus.js.map +0 -1
@@ -23,39 +23,35 @@ const git_memory_1 = require("../memory/git-memory");
23
23
  const export_map_1 = require("../scanners/export-map");
24
24
  const architecture_graph_1 = require("../scanners/architecture-graph");
25
25
  const rate_limiter_1 = require("../security/rate-limiter");
26
- const license_1 = require("../security/license");
27
26
  const feature_gate_1 = require("../security/feature-gate");
28
27
  const export_import_1 = require("../memory/export-import");
29
28
  const llm_enhancer_1 = require("../memory/llm-enhancer");
30
- // --- Query Expansion (Synonym Map) ---
31
- const SYNONYMS = {
32
- auth: ['authentication', 'login', 'signin', 'sign-in', 'credentials'],
33
- login: ['auth', 'authentication', 'signin', 'sign-in'],
34
- db: ['database', 'sql', 'postgresql', 'postgres', 'mongodb', 'sqlite'],
35
- database: ['db', 'sql', 'postgresql', 'postgres', 'mongodb', 'sqlite'],
36
- api: ['endpoint', 'route', 'rest', 'graphql', 'http'],
37
- error: ['bug', 'fix', 'issue', 'problem', 'crash', 'fail'],
38
- bug: ['error', 'fix', 'issue', 'problem', 'crash'],
39
- style: ['css', 'design', 'theme', 'color', 'font', 'layout'],
40
- test: ['testing', 'jest', 'vitest', 'spec', 'unittest'],
41
- deploy: ['deployment', 'ci', 'cd', 'pipeline', 'docker', 'build'],
42
- };
29
+ const usage_stats_1 = require("../memory/usage-stats");
30
+ const correction_detector_1 = require("../memory/correction-detector");
31
+ const success_tracker_1 = require("../memory/success-tracker");
32
+ const error_learner_1 = require("../memory/error-learner");
33
+ const completion_resolver_1 = require("../memory/completion-resolver");
34
+ const pre_flight_1 = require("../memory/pre-flight");
35
+ const impact_analyzer_1 = require("../memory/impact-analyzer");
36
+ const cross_memory_linker_1 = require("../memory/cross-memory-linker");
37
+ const access_pattern_tracker_1 = require("../memory/access-pattern-tracker");
38
+ const soul_manager_1 = require("../memory/soul-manager");
39
+ const daily_diary_1 = require("../memory/daily-diary");
40
+ const memory_export_md_1 = require("../memory/memory-export-md");
41
+ const mmr_reranker_1 = require("../memory/mmr-reranker");
42
+ const embedding_cache_1 = require("../memory/embedding-cache");
43
+ const query_expansion_1 = require("../memory/query-expansion");
44
+ const resume_work_1 = require("../memory/resume-work");
45
+ const preference_learner_1 = require("../memory/preference-learner");
46
+ const convention_detector_1 = require("../memory/convention-detector");
47
+ const export_map_2 = require("../scanners/export-map");
48
+ const file_relationships_1 = require("../memory/file-relationships");
49
+ const instructions_generator_1 = require("../memory/instructions-generator");
50
+ const tool_recommender_1 = require("../memory/tool-recommender");
51
+ const regression_guard_1 = require("../memory/regression-guard");
52
+ // --- OpenClaw-style Query Expansion (replaced basic synonym map) ---
43
53
  function expandQuery(query) {
44
- const words = query.toLowerCase().split(/\s+/).filter(w => w.length > 2);
45
- const expanded = new Set(words);
46
- for (const word of words) {
47
- if (expanded.size >= 8)
48
- break; // Cap expansion to prevent FTS hang
49
- const syns = SYNONYMS[word];
50
- if (syns) {
51
- for (const s of syns.slice(0, 2)) {
52
- if (expanded.size >= 8)
53
- break;
54
- expanded.add(s);
55
- }
56
- }
57
- }
58
- return Array.from(expanded).join(' OR ');
54
+ return (0, query_expansion_1.expandQuery)(query);
59
55
  }
60
56
  // --- MCP Tool Definitions ---
61
57
  const MCP_TOOLS = [
@@ -143,14 +139,14 @@ const MCP_TOOLS = [
143
139
  },
144
140
  {
145
141
  name: 'force_recall',
146
- description: 'MANDATORY: Call this at the START of every conversation. Returns ALL corrections, decisions, conventions, and bug fixes. Also searches for topic-specific memories. This is the single entry point for complete context injection.',
142
+ description: 'MANDATORY: Call this at the START of every conversation. Returns ALL corrections, decisions, conventions, and bug fixes. Also searches for topic-specific memories. This is the single entry point for complete context injection. Without calling this, you WILL repeat past mistakes, contradict previous decisions, and miss critical conventions. The user expects you to remember everything from past sessions.',
147
143
  inputSchema: {
148
144
  type: 'object',
149
145
  properties: {
150
146
  topic: { type: 'string', description: 'What the user is asking about (used to search for relevant memories)' },
151
147
  currentFile: { type: 'string', description: 'Currently active file path (optional)' },
152
148
  },
153
- required: [],
149
+ required: ['topic'],
154
150
  },
155
151
  },
156
152
  {
@@ -159,9 +155,9 @@ const MCP_TOOLS = [
159
155
  inputSchema: {
160
156
  type: 'object',
161
157
  properties: {
162
- content: { type: 'string', description: 'One sentence describing the decision, correction, convention, or bug fix' },
158
+ memory: { type: 'string', description: 'One sentence describing the decision, correction, convention, or bug fix' },
163
159
  },
164
- required: ['content'],
160
+ required: ['memory'],
165
161
  },
166
162
  },
167
163
  {
@@ -202,7 +198,7 @@ const MCP_TOOLS = [
202
198
  },
203
199
  {
204
200
  name: 'auto_learn',
205
- description: 'CALL THIS AFTER EVERY RESPONSE. Pass the text of your response and Cortex will automatically extract and store any decisions, corrections, conventions, or bug fixes — with zero manual effort. This is how Cortex learns passively.',
201
+ description: 'CALL THIS AFTER EVERY RESPONSE. Pass the text of your response and Cortex will automatically extract and store any decisions, corrections, conventions, or bug fixes — with zero manual effort. This is how Cortex learns passively. If you skip this, everything you said in this conversation is LOST FOREVER — the user will have to repeat themselves next time.',
206
202
  inputSchema: {
207
203
  type: 'object',
208
204
  properties: {
@@ -240,118 +236,42 @@ const MCP_TOOLS = [
240
236
  },
241
237
  },
242
238
  {
243
- name: 'search_by_file',
244
- description: 'Find all memories related to a specific file. Use this when working on a file to see what decisions, bugs, and conventions have been recorded for it.',
245
- inputSchema: {
246
- type: 'object',
247
- properties: {
248
- filePath: { type: 'string', description: 'The file path to search for (e.g. "src/auth.ts" or "auth")' },
249
- limit: { type: 'number', description: 'Maximum results to return (default 20)' },
250
- },
251
- required: ['filePath'],
252
- },
253
- },
254
- {
255
- name: 'pin_memory',
256
- description: 'Pin a memory so it is ALWAYS injected into context, regardless of topic. Use for critical rules like "never commit to main" or "always use TypeScript strict mode".',
257
- inputSchema: {
258
- type: 'object',
259
- properties: {
260
- id: { type: 'string', description: 'ID of the memory to pin (from list_memories or recall_memory)' },
261
- },
262
- required: ['id'],
263
- },
264
- },
265
- {
266
- name: 'unpin_memory',
267
- description: 'Unpin a previously pinned memory so it returns to normal priority ranking.',
268
- inputSchema: {
269
- type: 'object',
270
- properties: {
271
- id: { type: 'string', description: 'ID of the memory to unpin' },
272
- },
273
- required: ['id'],
274
- },
275
- },
276
- {
277
- name: 'undo_last',
278
- description: 'Undo the last N memories that were stored. Use this if auto_learn stored garbage or if you made a mistake. Deletes the most recently created memories.',
279
- inputSchema: {
280
- type: 'object',
281
- properties: {
282
- count: { type: 'number', description: 'How many recent memories to undo (default 1, max 10)' },
283
- },
284
- },
285
- },
286
- {
287
- name: 'clear_memories',
288
- description: 'Bulk delete memories by type or age. Use to clean up an entire category of memories or remove old stale memories.',
289
- inputSchema: {
290
- type: 'object',
291
- properties: {
292
- type: { type: 'string', enum: ['DECISION', 'CORRECTION', 'CONVENTION', 'BUG_FIX', 'INSIGHT', 'ALL'], description: 'Delete all memories of this type (or ALL for everything)' },
293
- olderThanDays: { type: 'number', description: 'Only delete memories older than N days (optional safety filter)' },
294
- },
295
- required: ['type'],
296
- },
297
- },
298
- {
299
- name: 'search_timeline',
300
- description: 'Search memories by date range. Use to answer questions like "what did we decide last week?" or "what bugs did we fix yesterday?".',
239
+ name: 'review_code',
240
+ description: 'Review code against your stored conventions, past bug patterns, and project decisions. Returns specific violations with memory references like having a senior dev review your code.',
301
241
  inputSchema: {
302
242
  type: 'object',
303
243
  properties: {
304
- from: { type: 'string', description: 'Start date (ISO format or relative like "7 days ago", "yesterday")' },
305
- to: { type: 'string', description: 'End date (ISO format or "now"). Default: now' },
306
- type: { type: 'string', enum: ['DECISION', 'CORRECTION', 'CONVENTION', 'BUG_FIX', 'INSIGHT', 'ALL'], description: 'Filter by type (optional)' },
244
+ code: { type: 'string', description: 'The code to review' },
245
+ filename: { type: 'string', description: 'Optional filename for context-aware review' },
307
246
  },
308
- required: ['from'],
309
- },
310
- },
311
- {
312
- name: 'thumbs_up',
313
- description: 'Mark a memory as useful/accurate. This boosts its importance and helps Cortex learn which memories matter most over time.',
314
- inputSchema: {
315
- type: 'object',
316
- properties: {
317
- id: { type: 'string', description: 'ID of the memory to upvote' },
318
- },
319
- required: ['id'],
247
+ required: ['code'],
320
248
  },
321
249
  },
322
250
  {
323
- name: 'thumbs_down',
324
- description: 'Mark a memory as not useful or incorrect. This demotes its importance. Repeatedly downvoted memories will be auto-deactivated.',
251
+ name: 'pre_check',
252
+ description: 'Pre-flight check: get ALL conventions, gotchas, past bugs, and corrections for a file BEFORE writing code. Like a pilot\'s checklist — call this before making any code changes to avoid repeating past mistakes. Skipping this risks reintroducing bugs that were already fixed.',
325
253
  inputSchema: {
326
254
  type: 'object',
327
255
  properties: {
328
- id: { type: 'string', description: 'ID of the memory to downvote' },
329
- reason: { type: 'string', description: 'Why this memory is wrong or not useful (optional)' },
256
+ filename: { type: 'string', description: 'The file you are about to edit' },
257
+ task: { type: 'string', description: 'What you plan to do (helps find relevant past failures)' },
330
258
  },
331
- required: ['id'],
332
- },
333
- },
334
- {
335
- name: 'backup_brain',
336
- description: 'Create a local backup of the entire memory database file. Run this periodically to ensure the user NEVER loses their project knowledge.',
337
- inputSchema: {
338
- type: 'object',
339
- properties: {},
340
259
  },
341
260
  },
342
261
  {
343
- name: 'get_daily_summary',
344
- description: 'Read the AI-generated summary of what happened on a specific date. If no date is given, summarize today and store it.',
262
+ name: 'check_impact',
263
+ description: 'Impact analysis: before editing a file, check which other files depend on it. Shows direct and indirect dependents with risk level. Prevents breaking changes.',
345
264
  inputSchema: {
346
265
  type: 'object',
347
266
  properties: {
348
- date: { type: 'string', description: 'Date in YYYY-MM-DD format (optional, defaults to today)' },
267
+ file: { type: 'string', description: 'The file you plan to modify' },
349
268
  },
269
+ required: ['file'],
350
270
  },
351
271
  },
352
272
  {
353
- name: 'analytics',
354
- description: 'Get deep analytics about the brain: memory distribution, top accessed memories, and database health. Use this to understand what the AI knows best.',
273
+ name: 'resume_work',
274
+ description: 'Resume work after a conversation break. Returns: last session summary, current tasks, recent corrections (don\'t repeat!), recent decisions, and activity summary. Call this when starting a new conversation about ongoing work.',
355
275
  inputSchema: {
356
276
  type: 'object',
357
277
  properties: {},
@@ -378,68 +298,12 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
378
298
  result: {
379
299
  protocolVersion: '2024-11-05',
380
300
  capabilities: { tools: {}, resources: {}, prompts: {} },
381
- serverInfo: { name: 'cortex', version: '2.7.0' },
301
+ serverInfo: { name: 'Cortex', version: require('../../package.json').version },
382
302
  },
383
303
  };
384
304
  case 'notifications/initialized':
305
+ case 'notifications/cancelled':
385
306
  return null;
386
- case 'prompts/list':
387
- return {
388
- jsonrpc: '2.0',
389
- id,
390
- result: {
391
- prompts: [
392
- {
393
- name: 'cortex_system_context',
394
- description: 'Injects crucial persistent project memories, decisions, corrections, and conventions directly into the system context. Use this to start every conversation with full project awareness.',
395
- arguments: [
396
- {
397
- name: 'currentFile',
398
- description: 'The currently active file path (optional, improves relevance)',
399
- required: false,
400
- },
401
- {
402
- name: 'topic',
403
- description: 'What the user is asking about (optional, improves topic search)',
404
- required: false,
405
- },
406
- ],
407
- },
408
- ],
409
- },
410
- };
411
- case 'prompts/get': {
412
- const promptName = rpc.params?.name;
413
- const promptArgs = rpc.params?.arguments || {};
414
- if (promptName === 'cortex_system_context') {
415
- const builder = getContextBuilder(memoryStore);
416
- const memoryPayload = builder.build({
417
- currentFile: promptArgs.currentFile,
418
- maxChars: 6000,
419
- });
420
- return {
421
- jsonrpc: '2.0',
422
- id,
423
- result: {
424
- description: 'Cortex Auto-Injected System Context',
425
- messages: [
426
- {
427
- role: 'user',
428
- content: {
429
- type: 'text',
430
- text: `You are connected to Cortex MCP — a persistent memory system. The following are critical memories, rules, and decisions from previous sessions. YOU MUST OBEY THESE RULES and reference this context when relevant:\n\n${memoryPayload}`,
431
- },
432
- },
433
- ],
434
- },
435
- };
436
- }
437
- return {
438
- jsonrpc: '2.0',
439
- id,
440
- error: { code: -32602, message: `Unknown prompt: ${promptName}` },
441
- };
442
- }
443
307
  case 'tools/list':
444
308
  return {
445
309
  jsonrpc: '2.0',
@@ -453,7 +317,7 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
453
317
  result: {
454
318
  resources: [{
455
319
  uri: 'memory://brain/context',
456
- name: 'Brain Context',
320
+ name: 'Cortex MCP Context',
457
321
  description: 'Top memories — corrections, decisions, conventions. Read this before every response.',
458
322
  mimeType: 'text/plain',
459
323
  }],
@@ -480,6 +344,97 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
480
344
  error: { code: -32602, message: `Unknown resource: ${uri}` },
481
345
  };
482
346
  }
347
+ case 'prompts/list':
348
+ return {
349
+ jsonrpc: '2.0', id,
350
+ result: {
351
+ prompts: [
352
+ {
353
+ name: 'cortex-review',
354
+ description: 'Review code against stored conventions, past bugs, and project decisions. Returns specific violations.',
355
+ arguments: [
356
+ { name: 'code', description: 'The code to review', required: true },
357
+ { name: 'filename', description: 'Filename for context-aware review', required: false },
358
+ ],
359
+ },
360
+ {
361
+ name: 'cortex-debug',
362
+ description: 'Debug an issue using Cortex memory. Checks for similar past bugs, failed attempts, and gotchas.',
363
+ arguments: [
364
+ { name: 'error', description: 'The error message or issue description', required: true },
365
+ { name: 'file', description: 'The file where the error occurs', required: false },
366
+ ],
367
+ },
368
+ {
369
+ name: 'cortex-new-feature',
370
+ description: 'Pre-flight checklist before building a new feature. Gets conventions, gotchas, and architecture context.',
371
+ arguments: [
372
+ { name: 'feature', description: 'What feature you plan to build', required: true },
373
+ { name: 'files', description: 'Files you plan to modify', required: false },
374
+ ],
375
+ },
376
+ ],
377
+ },
378
+ };
379
+ case 'prompts/get': {
380
+ const promptName = rpc.params?.name;
381
+ const promptArgs = rpc.params?.arguments || {};
382
+ if (promptName === 'cortex-review') {
383
+ return {
384
+ jsonrpc: '2.0', id,
385
+ result: {
386
+ description: 'Code review against Cortex memory',
387
+ messages: [
388
+ {
389
+ role: 'user',
390
+ content: {
391
+ type: 'text',
392
+ text: `Review this code against all stored conventions, past bug patterns, and project decisions.\n\nFile: ${promptArgs.filename || 'unknown'}\n\n\`\`\`\n${promptArgs.code || '(no code provided)'}\n\`\`\`\n\nUse the review_code and pre_check tools to check against stored memory. Report any violations with memory IDs.`,
393
+ },
394
+ },
395
+ ],
396
+ },
397
+ };
398
+ }
399
+ if (promptName === 'cortex-debug') {
400
+ return {
401
+ jsonrpc: '2.0', id,
402
+ result: {
403
+ description: 'Debug with Cortex memory context',
404
+ messages: [
405
+ {
406
+ role: 'user',
407
+ content: {
408
+ type: 'text',
409
+ text: `I'm debugging this issue:\n\n${promptArgs.error || '(no error provided)'}\n\nFile: ${promptArgs.file || 'unknown'}\n\nUse recall_memory to search for similar past bugs, failed attempts, and gotchas. Check if this matches any known patterns. Use check_impact to see what other files might be affected.`,
410
+ },
411
+ },
412
+ ],
413
+ },
414
+ };
415
+ }
416
+ if (promptName === 'cortex-new-feature') {
417
+ return {
418
+ jsonrpc: '2.0', id,
419
+ result: {
420
+ description: 'New feature pre-flight checklist',
421
+ messages: [
422
+ {
423
+ role: 'user',
424
+ content: {
425
+ type: 'text',
426
+ text: `I'm building a new feature: ${promptArgs.feature || '(no feature described)'}\n\nFiles I plan to modify: ${promptArgs.files || 'unknown'}\n\nBefore I start, run pre_check for each file, check_impact for dependency risks, and recall_memory for any relevant past work. Give me a checklist of things to watch out for.`,
427
+ },
428
+ },
429
+ ],
430
+ },
431
+ };
432
+ }
433
+ return {
434
+ jsonrpc: '2.0', id,
435
+ error: { code: -32602, message: `Unknown prompt: ${promptName}` },
436
+ };
437
+ }
483
438
  case 'tools/call': {
484
439
  const toolName = rpc.params?.name;
485
440
  const args = rpc.params?.arguments || {};
@@ -490,10 +445,10 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
490
445
  result: { content: [{ type: 'text', text: 'Error: query too long (max 1000 chars)' }], isError: true },
491
446
  };
492
447
  }
493
- if (args.content && typeof args.content === 'string' && args.content.length > 5000) {
448
+ if (args.content && typeof args.content === 'string' && args.content.length > 50000) {
494
449
  return {
495
450
  jsonrpc: '2.0', id,
496
- result: { content: [{ type: 'text', text: 'Error: content too long (max 5000 chars)' }], isError: true },
451
+ result: { content: [{ type: 'text', text: 'Error: content too long (max 50000 chars)' }], isError: true },
497
452
  };
498
453
  }
499
454
  if (toolName === 'recall_memory') {
@@ -536,49 +491,25 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
536
491
  return handleAutoLearn(id, args);
537
492
  }
538
493
  else if (toolName === 'export_memories') {
539
- return handleExportMemoriesFn(memoryStore, id, args);
494
+ return handleExportMemories(id);
540
495
  }
541
496
  else if (toolName === 'import_memories') {
542
- return handleImportMemoriesFn(memoryStore, memory_cache_1, id, args);
543
- }
544
- else if (toolName === 'get_related_memories') {
545
- return handleGetRelatedMemoriesFn(memoryStore, id, args);
497
+ return handleImportMemories(id, args);
546
498
  }
547
499
  else if (toolName === 'health_check') {
548
500
  return handleHealthCheck(id);
549
501
  }
550
- else if (toolName === 'search_by_file') {
551
- return handleSearchByFile(id, args);
552
- }
553
- else if (toolName === 'pin_memory') {
554
- return handlePinMemory(id, args);
502
+ else if (toolName === 'review_code') {
503
+ return handleReviewCode(id, args);
555
504
  }
556
- else if (toolName === 'unpin_memory') {
557
- return handleUnpinMemory(id, args);
505
+ else if (toolName === 'pre_check') {
506
+ return handlePreCheck(id, args);
558
507
  }
559
- else if (toolName === 'undo_last') {
560
- return handleUndoLast(id, args);
508
+ else if (toolName === 'check_impact') {
509
+ return handleCheckImpact(id, args);
561
510
  }
562
- else if (toolName === 'clear_memories') {
563
- return handleClearMemories(id, args);
564
- }
565
- else if (toolName === 'search_timeline') {
566
- return handleSearchTimeline(id, args);
567
- }
568
- else if (toolName === 'thumbs_up') {
569
- return handleThumbsUp(id, args);
570
- }
571
- else if (toolName === 'thumbs_down') {
572
- return handleThumbsDown(id, args);
573
- }
574
- else if (toolName === 'backup_brain') {
575
- return handleBackupBrain(id);
576
- }
577
- else if (toolName === 'get_daily_summary') {
578
- return handleGetDailySummary(id, args);
579
- }
580
- else if (toolName === 'analytics') {
581
- return handleAnalytics(id);
511
+ else if (toolName === 'resume_work') {
512
+ return handleResumeWork(id);
582
513
  }
583
514
  else {
584
515
  return {
@@ -631,27 +562,65 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
631
562
  if (ftsResults.length === 0) {
632
563
  ftsResults = memoryStore.searchFTS(queryText, maxResults * 2);
633
564
  }
565
+ // OpenClaw: Try alternative queries if still 0 results
566
+ if (ftsResults.length === 0) {
567
+ const altQueries = (0, query_expansion_1.generateAlternativeQueries)(queryText);
568
+ for (const alt of altQueries.slice(1)) { // skip first (original)
569
+ ftsResults = memoryStore.searchFTS(alt, maxResults * 2);
570
+ if (ftsResults.length > 0)
571
+ break;
572
+ }
573
+ }
634
574
  console.log(` [FTS] ${ftsResults.length} results`);
635
- // 2. Vector Search (if worker ready)
575
+ // 2. Vector Search (if worker ready) — with OpenClaw-style embedding cache
636
576
  let vectorResults = [];
637
577
  if ((0, embedding_manager_1.isWorkerReady)()) {
638
- const embedding = await (0, embedding_manager_1.embedText)(queryText);
639
- vectorResults = memoryStore.searchVector(new Float32Array(embedding), maxResults * 2);
578
+ // Check cache first (OpenClaw pattern)
579
+ let embeddingArr = (0, embedding_cache_1.getCachedEmbedding)(queryText);
580
+ if (!embeddingArr) {
581
+ embeddingArr = await (0, embedding_manager_1.embedText)(queryText);
582
+ (0, embedding_cache_1.cacheEmbedding)(queryText, embeddingArr);
583
+ }
584
+ vectorResults = memoryStore.searchVector(new Float32Array(embeddingArr), maxResults * 2);
640
585
  if (vectorResults.length > 0) {
641
586
  console.log(` [VECTOR] ${vectorResults.length} results`);
642
587
  }
643
588
  }
644
589
  // 3. Hybrid Ranking
645
590
  const rawRanked = (0, memory_ranker_1.rankResults)(ftsResults, vectorResults, maxResults * 2, currentFile);
646
- // Map RankedResult to ScoredMemory
647
- ranked = rawRanked.map(r => ({
591
+ // 3a. Apply MMR re-ranking (OpenClaw-style diversity)
592
+ const mmrRanked = (0, mmr_reranker_1.applyMMR)(rawRanked.map(r => ({
648
593
  ...r,
649
594
  matchMethod: 'hybrid'
650
- }));
651
- // 3b. Context-based boost (if current file provided)
652
- if (args.currentFile) {
653
- // Boost memories related to this file or its directory
654
- // Implementation in memory-ranker.ts (pending)
595
+ })));
596
+ // Map to ScoredMemory
597
+ ranked = mmrRanked;
598
+ // 3b. Project-aware boost (memories from current project rank higher)
599
+ if (workspaceRoot) {
600
+ try {
601
+ const pkgPath = require('path').join(workspaceRoot, 'package.json');
602
+ let projectTag = '';
603
+ if (require('fs').existsSync(pkgPath)) {
604
+ const pkg = JSON.parse(require('fs').readFileSync(pkgPath, 'utf-8'));
605
+ projectTag = (pkg.name || '').toLowerCase();
606
+ }
607
+ if (!projectTag)
608
+ projectTag = require('path').basename(workspaceRoot).toLowerCase();
609
+ if (projectTag) {
610
+ ranked = ranked.map(r => {
611
+ const tags = r.memory.tags || [];
612
+ const hasProjectTag = tags.some(t => t.toLowerCase().includes(projectTag));
613
+ const hasDiffProject = tags.some(t => t.startsWith('project:') && !t.toLowerCase().includes(projectTag));
614
+ if (hasProjectTag)
615
+ return { ...r, score: r.score * 1.3 };
616
+ if (hasDiffProject)
617
+ return { ...r, score: r.score * 0.7 };
618
+ return r;
619
+ });
620
+ ranked.sort((a, b) => b.score - a.score);
621
+ }
622
+ }
623
+ catch { /* project detection failed — skip boost */ }
655
624
  }
656
625
  // 3c. Apply attention-based re-ranking (debugging→bugs, coding→conventions)
657
626
  const recallContext = (0, attention_ranker_1.detectActionContext)(queryText, currentFile);
@@ -684,14 +653,16 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
684
653
  ranked = ranked.slice(0, maxResults);
685
654
  // 4. Touch for access tracking (reinforcement — used memories get stronger)
686
655
  if (ranked.length > 0) {
687
- await Promise.all(ranked.map(m => {
688
- try {
689
- return memoryStore.touch(m.memory.id);
690
- }
691
- catch {
692
- return Promise.resolve();
656
+ memoryStore.runTransaction(() => {
657
+ for (const m of ranked) {
658
+ try {
659
+ memoryStore.touch(m.memory.id);
660
+ }
661
+ catch { /* non-fatal */ }
693
662
  }
694
- }));
663
+ });
664
+ // NEW: Record access patterns for personalized boosting
665
+ (0, access_pattern_tracker_1.recordBatchAccess)(ranked.map(r => ({ type: r.memory.type })));
695
666
  (0, confidence_decay_1.runDecayMaintenance)(memoryStore); // Opportunistic decay
696
667
  }
697
668
  (0, memory_cache_1.setCache)(cacheKey, ranked);
@@ -744,7 +715,7 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
744
715
  }
745
716
  try {
746
717
  // License check — gate memory storage
747
- const activeCount = memoryStore.getActive(9999).length;
718
+ const activeCount = memoryStore.activeCount();
748
719
  const storeCheck = (0, feature_gate_1.canStoreMemory)(activeCount);
749
720
  if (!storeCheck.allowed) {
750
721
  return {
@@ -801,25 +772,24 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
801
772
  }
802
773
  }
803
774
  catch { /* non-fatal */ }
804
- // Auto-edge creationlink to recent memories of same type/files
775
+ // Smart cross-memory linkingconnects by files, tags, AND word overlap
805
776
  try {
806
- const recent = memoryStore.getByType(memType, 5);
807
- for (const r of recent) {
808
- if (r.id !== memory.id) {
809
- memoryStore.addEdge({
810
- sourceId: memory.id,
811
- targetId: r.id,
812
- relation: 'related_to',
813
- weight: 0.5,
814
- timestamp: Date.now(),
815
- });
816
- break; // Link to most recent only
817
- }
818
- }
777
+ const linksCreated = (0, cross_memory_linker_1.autoLinkMemory)(memoryStore, memory);
778
+ if (linksCreated > 0)
779
+ console.log(` [LINK] Auto-linked ${linksCreated} related memories`);
819
780
  }
820
781
  catch { /* non-fatal */ }
821
782
  // Feed session tracker
822
783
  (0, session_tracker_1.feedSession)({ decision: `[${type}] ${sanitized.slice(0, 60)}` });
784
+ // OpenClaw: Log to daily diary (store_memory path)
785
+ try {
786
+ (0, daily_diary_1.appendDiaryEntry)({
787
+ type: type.toLowerCase().replace('_', '_'),
788
+ content: sanitized.slice(0, 100),
789
+ file: files?.[0],
790
+ });
791
+ }
792
+ catch { /* diary is non-critical */ }
823
793
  // Queue background embedding
824
794
  if ((0, embedding_manager_1.isWorkerReady)()) {
825
795
  const embedText_ = [sanitized, reason || ''].join(' ').trim();
@@ -842,6 +812,15 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
842
812
  }
843
813
  }
844
814
  function handleGetStats(id) {
815
+ const health = (0, usage_stats_1.calculateBrainHealth)(memoryStore);
816
+ const lifetime = (0, usage_stats_1.getLifetimeStats)();
817
+ const streak = (0, usage_stats_1.getStreakDisplay)();
818
+ let llmProvider = 'none';
819
+ try {
820
+ if ((0, llm_enhancer_1.isLLMAvailable)())
821
+ llmProvider = (0, llm_enhancer_1.getLLMProvider)();
822
+ }
823
+ catch { /* */ }
845
824
  return {
846
825
  jsonrpc: '2.0', id,
847
826
  result: {
@@ -853,6 +832,16 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
853
832
  totalEvents: eventLog.count(),
854
833
  vectorSearchReady: (0, embedding_manager_1.isWorkerReady)(),
855
834
  cacheSize: (0, memory_cache_1.cacheSize)(),
835
+ brainHealth: { score: health.score, grade: health.grade, tips: health.tips },
836
+ savedYouCount: lifetime.savedYouCount,
837
+ totalSessions: lifetime.totalSessions,
838
+ timeSaved: lifetime.totalMemoriesServed * 15 + lifetime.totalHallucationsCaught * 300,
839
+ streak: streak || 'Day 1',
840
+ longestStreak: lifetime.longestStreak || 0,
841
+ totalAutoLearns: lifetime.totalAutoLearns,
842
+ successPatternsLearned: lifetime.totalSuccessPatterns || 0,
843
+ errorsLearned: lifetime.totalErrorsLearned || 0,
844
+ llmProvider,
856
845
  }, null, 2),
857
846
  }],
858
847
  },
@@ -882,16 +871,46 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
882
871
  extraMemories += (0, architecture_graph_1.storeArchitectureGraph)(memoryStore, archGraph);
883
872
  }
884
873
  catch { /* non-fatal */ }
874
+ // Convention auto-detection — analyze actual code patterns
875
+ try {
876
+ const conventions = (0, convention_detector_1.detectConventions)(root);
877
+ for (const conv of conventions) {
878
+ try {
879
+ (0, memory_quality_1.storeWithQuality)(memoryStore, {
880
+ type: 'CONVENTION',
881
+ intent: conv.pattern,
882
+ action: conv.evidence,
883
+ reason: `Auto-detected from code (${conv.category}, confidence: ${conv.confidence})`,
884
+ confidence: conv.confidence,
885
+ importance: conv.confidence,
886
+ tags: ['convention', 'auto-detected', conv.category],
887
+ });
888
+ extraMemories++;
889
+ }
890
+ catch { /* skip duplicates */ }
891
+ }
892
+ }
893
+ catch { /* non-fatal */ }
885
894
  (0, memory_cache_1.invalidateCache)();
895
+ (0, usage_stats_1.trackScan)();
886
896
  const total = count + extraMemories;
897
+ // Report knowledge gaps after scan
898
+ let gapReport = '';
899
+ try {
900
+ const gaps = (0, meta_memory_1.detectKnowledgeGaps)(memoryStore, root);
901
+ if (gaps.length > 0) {
902
+ gapReport = `\n\n${(0, meta_memory_1.formatKnowledgeGaps)(gaps)}`;
903
+ }
904
+ }
905
+ catch { /* non-fatal */ }
887
906
  return {
888
907
  jsonrpc: '2.0', id,
889
908
  result: {
890
909
  content: [{
891
910
  type: 'text',
892
911
  text: total > 0
893
- ? `Project scanned successfully. ${total} memories created (stack, structure, config, git history, export map, architecture graph).`
894
- : 'Project was already scanned. No new memories created.',
912
+ ? `Project scanned successfully. ${total} memories created (stack, structure, config, git history, export map, architecture graph, coding conventions).${gapReport}`
913
+ : `Project was already scanned. No new memories created.${gapReport}`,
895
914
  }],
896
915
  },
897
916
  };
@@ -967,6 +986,9 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
967
986
  };
968
987
  }
969
988
  try {
989
+ // Track file for relationship mapping
990
+ if (args.filename)
991
+ (0, file_relationships_1.recordFileEdit)(args.filename);
970
992
  const result = (0, code_verifier_1.verifyCode)(args.code, root);
971
993
  const lines = [];
972
994
  // Imports
@@ -993,6 +1015,20 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
993
1015
  for (const [file, available] of Object.entries(result.exports.available)) {
994
1016
  lines.push(` ${file} exports: ${available.join(', ')}`);
995
1017
  }
1018
+ // Smart fix suggestions — find closest real exports
1019
+ try {
1020
+ const wsRoot = args.workspaceRoot || workspaceRoot;
1021
+ if (wsRoot) {
1022
+ const exportMap = (0, export_map_1.buildExportMap)(wsRoot);
1023
+ for (const invalid of result.exports.invalid) {
1024
+ const suggestions = (0, export_map_2.suggestRealExport)(exportMap, invalid);
1025
+ if (suggestions.length > 0) {
1026
+ lines.push(` 💡 Did you mean: ${suggestions.slice(0, 3).join(', ')}?`);
1027
+ }
1028
+ }
1029
+ }
1030
+ }
1031
+ catch { /* non-fatal */ }
996
1032
  }
997
1033
  }
998
1034
  // Env vars
@@ -1011,6 +1047,12 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1011
1047
  if (lines.length === 0) {
1012
1048
  lines.push('No imports, exports, or env vars detected in the code.');
1013
1049
  }
1050
+ // Track hallucination catches for usage stats
1051
+ const catchCount = result.imports.invalid.length + result.exports.invalid.length + result.envVars.invalid.length;
1052
+ if (catchCount > 0) {
1053
+ for (let i = 0; i < catchCount; i++)
1054
+ (0, usage_stats_1.trackCatch)();
1055
+ }
1014
1056
  return {
1015
1057
  jsonrpc: '2.0', id,
1016
1058
  result: { content: [{ type: 'text', text: lines.join('\n') }] },
@@ -1032,7 +1074,7 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1032
1074
  result: { content: [{ type: 'text', text: `[WARN] Rate limited: ${rateCheck.reason}` }], isError: true },
1033
1075
  };
1034
1076
  }
1035
- const text = (args.content || args.memory)?.trim();
1077
+ const text = args.memory?.trim();
1036
1078
  if (!text || text.length < 5) {
1037
1079
  return {
1038
1080
  jsonrpc: '2.0', id,
@@ -1094,13 +1136,77 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1094
1136
  }
1095
1137
  async function handleForceRecall(id, args) {
1096
1138
  try {
1139
+ // Fix #17: Cache force_recall with short TTL to avoid redundant rebuilds
1140
+ const cacheKey = `force_recall:${args.topic || ''}:${args.currentFile || ''}`;
1141
+ const cached = (0, memory_cache_1.getCached)(cacheKey);
1142
+ if (cached) {
1143
+ console.log(` [CACHE] force_recall hit`);
1144
+ return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: cached }] } };
1145
+ }
1097
1146
  const parts = [];
1098
1147
  // ─── BRAIN LAYER 0: End previous session + start new one ─────────
1099
1148
  (0, session_tracker_1.endSession)(memoryStore); // Save previous session summary
1149
+ (0, file_relationships_1.storeRelationships)(memoryStore); // Persist file co-edit relationships
1100
1150
  (0, session_tracker_1.startSession)();
1101
- const topic = args.topic || args.query || '';
1102
- if (topic)
1103
- (0, session_tracker_1.feedSession)({ topic });
1151
+ (0, usage_stats_1.resetSessionStats)();
1152
+ if (args.topic)
1153
+ (0, session_tracker_1.feedSession)({ topic: args.topic });
1154
+ // ─── SOUL LAYER (OpenClaw-style identity) ────────────────────────
1155
+ if (workspaceRoot) {
1156
+ try {
1157
+ (0, soul_manager_1.initSoul)(workspaceRoot);
1158
+ const soulContext = (0, soul_manager_1.formatSoul)(workspaceRoot);
1159
+ if (soulContext)
1160
+ parts.push(soulContext);
1161
+ }
1162
+ catch { /* soul is non-critical */ }
1163
+ }
1164
+ // ─── Project Detection (for project isolation) ───────────────────
1165
+ let projectName = '';
1166
+ if (workspaceRoot) {
1167
+ try {
1168
+ const pkgPath = require('path').join(workspaceRoot, 'package.json');
1169
+ if (require('fs').existsSync(pkgPath)) {
1170
+ const pkg = JSON.parse(require('fs').readFileSync(pkgPath, 'utf-8'));
1171
+ projectName = pkg.name || require('path').basename(workspaceRoot);
1172
+ }
1173
+ else {
1174
+ projectName = require('path').basename(workspaceRoot);
1175
+ }
1176
+ }
1177
+ catch {
1178
+ projectName = require('path').basename(workspaceRoot || '');
1179
+ }
1180
+ if (projectName) {
1181
+ (0, session_tracker_1.feedSession)({ project: projectName });
1182
+ }
1183
+ }
1184
+ // ─── BRAIN LAYER 0.5: Auto-scan on first run (Day 1 Empty Brain fix) ───
1185
+ if (memoryStore.activeCount() === 0 && workspaceRoot) {
1186
+ try {
1187
+ console.log(` [AUTO-SCAN] First run detected — scanning project...`);
1188
+ const scanner = new project_scanner_1.ProjectScanner(memoryStore, workspaceRoot);
1189
+ const scanCount = await scanner.scan();
1190
+ let extraMemories = 0;
1191
+ try {
1192
+ extraMemories += (0, export_map_1.storeExportMap)(memoryStore, (0, export_map_1.buildExportMap)(workspaceRoot));
1193
+ }
1194
+ catch { }
1195
+ try {
1196
+ extraMemories += (0, architecture_graph_1.storeArchitectureGraph)(memoryStore, (0, architecture_graph_1.buildArchitectureGraph)(workspaceRoot));
1197
+ }
1198
+ catch { }
1199
+ const total = scanCount + extraMemories;
1200
+ if (total > 0) {
1201
+ parts.push(`## Welcome to Cortex\n\nFirst run detected — auto-scanned your project and created ${total} memories (stack, structure, config, git history, exports, architecture). Cortex will now remember everything across sessions.`);
1202
+ (0, memory_cache_1.invalidateCache)();
1203
+ (0, usage_stats_1.trackScan)();
1204
+ }
1205
+ }
1206
+ catch (scanErr) {
1207
+ console.log(` [AUTO-SCAN] Failed: ${scanErr.message}`);
1208
+ }
1209
+ }
1104
1210
  // ─── BRAIN LAYER 1: Maintenance (runs in background) ─────────────
1105
1211
  try {
1106
1212
  // Decay old unused memories
@@ -1111,17 +1217,26 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1111
1217
  if ((0, memory_consolidator_1.shouldConsolidate)(memoryStore)) {
1112
1218
  (0, memory_consolidator_1.consolidateMemories)(memoryStore);
1113
1219
  }
1114
- }
1115
- catch { /* maintenance errors are non-fatal */ }
1116
- // ─── BRAIN LAYER 1.5: PINNED MEMORIES (Critical Rules) ───────────
1117
- // Scan ALL memories for pinned — critical rules must never be missed
1118
- const pinnedMemories = memoryStore.getActive(10000).filter(m => (m.tags || []).includes('pinned'));
1119
- if (pinnedMemories.length > 0) {
1120
- parts.push('\n## 📌 PINNED CRITICAL RULES (Must Follow)');
1121
- for (const p of pinnedMemories) {
1122
- parts.push(`- **[${p.type}]** ${p.intent}`);
1220
+ // OpenClaw: Auto-learn soul from high-access memories
1221
+ if (workspaceRoot) {
1222
+ const topMemories = memoryStore.getActive(50).map(m => ({
1223
+ type: m.type, intent: m.intent, accessCount: m.accessCount
1224
+ }));
1225
+ (0, soul_manager_1.autoLearnSoul)(workspaceRoot, topMemories);
1226
+ }
1227
+ // OpenClaw: Load access profile for personalized ranking
1228
+ (0, access_pattern_tracker_1.loadAccessProfile)(memoryStore);
1229
+ // OpenClaw: Auto-import user edits from MEMORY.md
1230
+ if (workspaceRoot) {
1231
+ try {
1232
+ const imported = (0, memory_export_md_1.importMemoryMd)(workspaceRoot, memoryStore);
1233
+ if (imported > 0)
1234
+ console.log(` [MEMORY.md] Imported ${imported} user-edited memories`);
1235
+ }
1236
+ catch { /* non-fatal */ }
1123
1237
  }
1124
1238
  }
1239
+ catch { /* maintenance errors are non-fatal */ }
1125
1240
  // ─── BRAIN LAYER 2: Attention Context ────────────────────────────
1126
1241
  const actionContext = (0, feature_gate_1.isFeatureAllowed)('attentionRanking') ? (0, attention_ranker_1.detectActionContext)(args.topic, args.currentFile) : {};
1127
1242
  const attentionLabel = (0, feature_gate_1.isFeatureAllowed)('attentionRanking') ? (0, attention_ranker_1.formatAttentionContext)(actionContext) : '';
@@ -1145,149 +1260,335 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1145
1260
  maxChars: 8000, // leave room for brain layers
1146
1261
  });
1147
1262
  parts.push(fullContext);
1148
- // ─── BRAIN LAYER 6: Anticipation (file-aware proactive recall) ───
1149
- if (args.currentFile && (0, feature_gate_1.isFeatureAllowed)('anticipation')) {
1150
- const anticipated = (0, anticipation_engine_1.formatAnticipation)((0, anticipation_engine_1.anticipate)(memoryStore, args.currentFile));
1151
- if (anticipated)
1152
- parts.push('\n' + anticipated);
1153
- }
1154
- // ─── BRAIN LAYER 7: Temporal Context (what changed recently) ─────
1155
- if ((0, feature_gate_1.isFeatureAllowed)('temporalContext')) {
1156
- const temporal = (0, temporal_engine_1.formatTemporalContext)(memoryStore);
1157
- if (temporal)
1158
- parts.push('\n' + temporal);
1159
- }
1160
- // ─── BRAIN LAYER 8: Workspace State (git changes) ────────────────
1161
- try {
1162
- const workspace = (0, temporal_engine_1.getWorkspaceDiff)(workspaceRoot || '');
1163
- if (workspace)
1164
- parts.push('\n' + workspace);
1165
- }
1166
- catch { /* git not available */ }
1167
- // ─── BRAIN LAYER 8.5: Git Memory (commit capture + file changes) ───
1168
- if ((0, feature_gate_1.isFeatureAllowed)('gitMemory')) {
1169
- try {
1170
- // Capture recent commits as memories
1171
- const commitsCaptured = (0, git_memory_1.captureGitCommits)(memoryStore, workspaceRoot || '', 5);
1172
- if (commitsCaptured > 0) {
1173
- parts.push(`\n> Captured ${commitsCaptured} new git commit(s) as memories`);
1263
+ // ─── BRAIN LAYERS 6-12: Run in parallel (independent of each other) ───
1264
+ const parallelResults = await Promise.allSettled([
1265
+ // Layer 6: Anticipation
1266
+ (async () => {
1267
+ if (args.currentFile && (0, feature_gate_1.isFeatureAllowed)('anticipation')) {
1268
+ return (0, anticipation_engine_1.formatAnticipation)((0, anticipation_engine_1.anticipate)(memoryStore, args.currentFile));
1174
1269
  }
1175
- // Show uncommitted file changes
1176
- const fileChanges = (0, git_memory_1.detectFileChanges)(workspaceRoot || '');
1177
- const fileChangeText = (0, git_memory_1.formatFileChanges)(fileChanges);
1178
- if (fileChangeText)
1179
- parts.push('\n' + fileChangeText);
1180
- }
1181
- catch { /* git not available */ }
1182
- } // end isPro() for git memory
1183
- // ─── BRAIN LAYER 9: Topic-Specific Search ────────────────────────
1184
- if (topic) {
1185
- try {
1186
- let ftsResults = memoryStore.searchFTS(topic, 15);
1187
- // Apply confidence decay + attention ranking
1188
- ftsResults = (0, confidence_decay_1.applyConfidenceDecay)(ftsResults);
1189
- ftsResults = (0, attention_ranker_1.rankByAttention)(ftsResults, actionContext);
1190
- // Causal chain: follow graph edges for top results
1191
- const seen = new Set();
1192
- const enriched = [];
1193
- for (const r of ftsResults) {
1194
- if (seen.has(r.memory.id))
1195
- continue;
1196
- seen.add(r.memory.id);
1197
- enriched.push(r);
1198
- // Follow causal links (1 hop)
1199
- try {
1200
- const related = memoryStore.getRelated(r.memory.id, 1, 3);
1201
- for (const rel of related) {
1202
- if (!seen.has(rel.memory.id)) {
1203
- seen.add(rel.memory.id);
1204
- enriched.push({ ...rel, score: rel.score * 0.7 });
1270
+ return '';
1271
+ })(),
1272
+ // Layer 6.5: Proactive Warnings — ⚠️ for files with past bugs/corrections
1273
+ (async () => {
1274
+ if (!args.currentFile)
1275
+ return '';
1276
+ try {
1277
+ const fileMemories = memoryStore.getByFile(args.currentFile, 50);
1278
+ const warnings = fileMemories.filter((m) => (m.type === 'CORRECTION' || m.type === 'BUG_FIX') && m.is_active);
1279
+ if (warnings.length === 0)
1280
+ return '';
1281
+ const lines = warnings.slice(0, 5).map((m) => `⚠️ **${m.type}**: ${m.intent}${m.reason && !m.reason.startsWith('Auto-detected') ? ` — _${m.reason}_` : ''}`);
1282
+ return `\n## ⚠️ Watch Out (past issues with this file)\n${lines.join('\n')}`;
1283
+ }
1284
+ catch {
1285
+ return '';
1286
+ }
1287
+ })(),
1288
+ // Layer 7: Temporal Context
1289
+ (async () => {
1290
+ if ((0, feature_gate_1.isFeatureAllowed)('temporalContext')) {
1291
+ return (0, temporal_engine_1.formatTemporalContext)(memoryStore);
1292
+ }
1293
+ return '';
1294
+ })(),
1295
+ // Layer 7.5: Daily Diary (OpenClaw-style)
1296
+ (async () => {
1297
+ try {
1298
+ return (0, daily_diary_1.formatDiaryContext)();
1299
+ }
1300
+ catch {
1301
+ return '';
1302
+ }
1303
+ })(),
1304
+ // Layer 8: Workspace State (git)
1305
+ (async () => {
1306
+ try {
1307
+ return (0, temporal_engine_1.getWorkspaceDiff)(workspaceRoot || '');
1308
+ }
1309
+ catch {
1310
+ return '';
1311
+ }
1312
+ })(),
1313
+ // Layer 8.5: Git Memory
1314
+ (async () => {
1315
+ if (!(0, feature_gate_1.isFeatureAllowed)('gitMemory'))
1316
+ return '';
1317
+ try {
1318
+ const commitsCaptured = (0, git_memory_1.captureGitCommits)(memoryStore, workspaceRoot || '', 5);
1319
+ const commitText = commitsCaptured > 0
1320
+ ? `\n> Captured ${commitsCaptured} new git commit(s) as memories`
1321
+ : '';
1322
+ const fileChanges = (0, git_memory_1.detectFileChanges)(workspaceRoot || '');
1323
+ const fileChangeText = (0, git_memory_1.formatFileChanges)(fileChanges);
1324
+ return [commitText, fileChangeText].filter(Boolean).join('\n');
1325
+ }
1326
+ catch {
1327
+ return '';
1328
+ }
1329
+ })(),
1330
+ // Layer 9: Topic Search — Hybrid (FTS + Vector) for deeper relevance
1331
+ (async () => {
1332
+ if (!args.topic)
1333
+ return '';
1334
+ try {
1335
+ // FTS search
1336
+ const ftsResults = memoryStore.searchFTS(args.topic, 15);
1337
+ // Vector search (semantic — catches what FTS misses)
1338
+ let vectorResults = [];
1339
+ if ((0, embedding_manager_1.isWorkerReady)()) {
1340
+ try {
1341
+ const topicEmbedding = await (0, embedding_manager_1.embedText)(args.topic);
1342
+ vectorResults = memoryStore.searchVector(new Float32Array(topicEmbedding), 10);
1343
+ }
1344
+ catch { /* vector search failure is non-fatal */ }
1345
+ }
1346
+ // Merge FTS + Vector, deduplicate by ID
1347
+ const merged = (0, memory_ranker_1.rankResults)(ftsResults, vectorResults, 20, args.currentFile);
1348
+ let ranked = merged.map(r => ({ ...r, matchMethod: 'hybrid' }));
1349
+ ranked = (0, confidence_decay_1.applyConfidenceDecay)(ranked);
1350
+ ranked = (0, attention_ranker_1.rankByAttention)(ranked, actionContext);
1351
+ const seen = new Set();
1352
+ const enriched = [];
1353
+ for (const r of ranked) {
1354
+ if (seen.has(r.memory.id))
1355
+ continue;
1356
+ seen.add(r.memory.id);
1357
+ enriched.push(r);
1358
+ try {
1359
+ const related = memoryStore.getRelated(r.memory.id, 1, 3);
1360
+ for (const rel of related) {
1361
+ if (!seen.has(rel.memory.id)) {
1362
+ seen.add(rel.memory.id);
1363
+ enriched.push({ ...rel, score: rel.score * 0.7 });
1364
+ }
1205
1365
  }
1206
1366
  }
1367
+ catch { }
1207
1368
  }
1208
- catch { }
1209
- }
1210
- if (enriched.length > 0) {
1211
- parts.push('\n## Topic: "' + topic + '"');
1212
- for (const m of enriched.slice(0, 15)) {
1213
- parts.push(`- [${m.memory.type}] ${m.memory.intent}${m.memory.reason ? ` — ${m.memory.reason}` : ''}`);
1369
+ if (enriched.length > 0) {
1370
+ const lines = ['\n## Topic: "' + args.topic + '"'];
1371
+ for (const m of enriched.slice(0, 15)) {
1372
+ lines.push(`- [${m.memory.type}] ${m.memory.intent}${m.memory.reason ? ` ${m.memory.reason}` : ''}`);
1373
+ }
1374
+ return lines.join('\n');
1214
1375
  }
1376
+ return '';
1377
+ }
1378
+ catch {
1379
+ return '\n> Note: Topic search unavailable (FTS index needs rebuild).';
1380
+ }
1381
+ })(),
1382
+ // Layer 10: Knowledge Gaps (skip when topic is specific — saves tokens)
1383
+ (async () => {
1384
+ if (args.topic)
1385
+ return ''; // Skip heavy layer for focused queries
1386
+ try {
1387
+ const gaps = (0, meta_memory_1.detectKnowledgeGaps)(memoryStore, workspaceRoot || '');
1388
+ return (0, meta_memory_1.formatKnowledgeGaps)(gaps);
1389
+ }
1390
+ catch {
1391
+ return '';
1392
+ }
1393
+ })(),
1394
+ // Layer 11: Export Map (skip when topic is specific — saves tokens)
1395
+ (async () => {
1396
+ if (args.topic)
1397
+ return ''; // Skip heavy layer for focused queries
1398
+ if (!workspaceRoot || !(0, feature_gate_1.isFeatureAllowed)('exportMap'))
1399
+ return '';
1400
+ try {
1401
+ const exportMap = (0, export_map_1.buildExportMap)(workspaceRoot);
1402
+ return exportMap.totalExports > 0 ? (0, export_map_1.formatExportMap)(exportMap) : '';
1403
+ }
1404
+ catch {
1405
+ return '';
1406
+ }
1407
+ })(),
1408
+ // Layer 12: Architecture Graph (skip when topic is specific — saves tokens)
1409
+ (async () => {
1410
+ if (args.topic)
1411
+ return ''; // Skip heavy layer for focused queries
1412
+ if (!workspaceRoot || !(0, feature_gate_1.isFeatureAllowed)('architectureGraph'))
1413
+ return '';
1414
+ try {
1415
+ const archGraph = (0, architecture_graph_1.buildArchitectureGraph)(workspaceRoot);
1416
+ return archGraph.totalFiles > 0 ? (0, architecture_graph_1.formatArchitectureGraph)(archGraph) : '';
1417
+ }
1418
+ catch {
1419
+ return '';
1215
1420
  }
1421
+ })(),
1422
+ ]);
1423
+ // Collect results (in order) — only push non-empty strings
1424
+ for (const result of parallelResults) {
1425
+ if (result.status === 'fulfilled' && result.value) {
1426
+ parts.push('\n' + result.value);
1216
1427
  }
1217
- catch {
1218
- parts.push('\n> Note: Topic search unavailable (FTS index needs rebuild).');
1428
+ }
1429
+ // ─── SMART CONTEXT SELECTION: Priority-based trimming ────────────
1430
+ // Instead of dumb slicing, trim lowest-priority sections first
1431
+ const MAX_CHARS = 12000; // ~3000 tokens — fits any model
1432
+ let output = parts.join('\n');
1433
+ if (output.length > MAX_CHARS) {
1434
+ // Priority: highest first (kept), lowest first (trimmed)
1435
+ // Layers 0-5 are high priority (welcome, sessions, corrections, core context)
1436
+ // Layers 6-12 are lower priority (anticipation, temporal, git, topic, gaps, exports, arch)
1437
+ // Trim from end (lowest priority) working backwards
1438
+ while (output.length > MAX_CHARS && parts.length > 4) {
1439
+ parts.pop(); // Remove lowest-priority section
1440
+ output = parts.join('\n');
1219
1441
  }
1442
+ if (output.length > MAX_CHARS) {
1443
+ output = output.slice(0, MAX_CHARS);
1444
+ }
1445
+ output += '\n\n> (Some context trimmed to fit token budget. Use `recall_memory` for specific queries.)';
1220
1446
  }
1221
- // ─── BRAIN LAYER 10: Knowledge Gaps (meta-memory) ────────────────
1447
+ // Track usage stats
1448
+ const memoriesInOutput = (output.match(/\[(?:CORRECTION|DECISION|CONVENTION|BUG_FIX|INSIGHT)\]/g) || []).length;
1449
+ (0, usage_stats_1.trackRecall)(memoriesInOutput);
1450
+ // Inject contextual instructions (DO/DON'T/WATCH-OUT)
1222
1451
  try {
1223
- const gaps = (0, meta_memory_1.detectKnowledgeGaps)(memoryStore, workspaceRoot || '');
1224
- const gapText = (0, meta_memory_1.formatKnowledgeGaps)(gaps);
1225
- if (gapText)
1226
- parts.push('\n' + gapText);
1452
+ const instructions = (0, instructions_generator_1.generateInstructions)(memoryStore);
1453
+ const instructionText = (0, instructions_generator_1.formatInstructions)(instructions);
1454
+ if (instructionText)
1455
+ output += '\n\n' + instructionText;
1227
1456
  }
1228
1457
  catch { /* non-fatal */ }
1229
- // ─── BRAIN LAYER 11: Export Map (anti-hallucination) ──────────────
1230
- if (workspaceRoot && (0, feature_gate_1.isFeatureAllowed)('exportMap')) {
1231
- try {
1232
- const exportMap = (0, export_map_1.buildExportMap)(workspaceRoot);
1233
- if (exportMap.totalExports > 0) {
1234
- const exportText = (0, export_map_1.formatExportMap)(exportMap);
1235
- if (exportText)
1236
- parts.push('\n' + exportText);
1458
+ // Inject tool recommendations (what to use based on context)
1459
+ try {
1460
+ const recs = (0, tool_recommender_1.recommendTools)({
1461
+ topic: args.topic,
1462
+ currentFile: args.currentFile,
1463
+ isNewConversation: true,
1464
+ });
1465
+ const recText = (0, tool_recommender_1.formatRecommendations)(recs);
1466
+ if (recText)
1467
+ output += '\n\n' + recText;
1468
+ }
1469
+ catch { /* non-fatal */ }
1470
+ // Inject user preferences (adapts AI behavior)
1471
+ try {
1472
+ const prefText = (0, preference_learner_1.getStoredPreferences)(memoryStore);
1473
+ if (prefText)
1474
+ output += '\n\n' + prefText;
1475
+ }
1476
+ catch { /* non-fatal */ }
1477
+ // Append stats footer (makes value visible — THE KEY TO ADDICTION)
1478
+ const statsFooter = (0, usage_stats_1.formatStatsFooter)(memoryStore);
1479
+ if (statsFooter)
1480
+ output += statsFooter;
1481
+ // Count corrections recalled as "saved you" moments
1482
+ const correctionsRecalled = (output.match(/\[CORRECTION\]/g) || []).length;
1483
+ for (let i = 0; i < correctionsRecalled; i++)
1484
+ (0, usage_stats_1.trackSaved)();
1485
+ // Cache the result for short-lived reuse
1486
+ (0, memory_cache_1.setCache)(cacheKey, output);
1487
+ return {
1488
+ jsonrpc: '2.0', id,
1489
+ result: { content: [{ type: 'text', text: output }] },
1490
+ };
1491
+ }
1492
+ catch (err) {
1493
+ return {
1494
+ jsonrpc: '2.0', id,
1495
+ result: { content: [{ type: 'text', text: `Force recall error: ${err.message}` }], isError: true },
1496
+ };
1497
+ }
1498
+ }
1499
+ // ─── REVIEW CODE: Check against conventions + past bugs ─────────────
1500
+ function handleReviewCode(id, args) {
1501
+ try {
1502
+ const code = args.code;
1503
+ const filename = args.filename || '';
1504
+ if (!code || code.length < 10) {
1505
+ return {
1506
+ jsonrpc: '2.0', id,
1507
+ result: { content: [{ type: 'text', text: 'Error: provide code to review (min 10 chars)' }], isError: true },
1508
+ };
1509
+ }
1510
+ const violations = [];
1511
+ const suggestions = [];
1512
+ const codeLower = code.toLowerCase();
1513
+ // Check against stored CONVENTIONS
1514
+ const conventions = memoryStore.getByType('CONVENTION', 100);
1515
+ for (const conv of conventions) {
1516
+ const intentLower = conv.intent.toLowerCase();
1517
+ // Check for common pattern violations
1518
+ if (intentLower.includes('never use') || intentLower.includes("don't use") || intentLower.includes('avoid')) {
1519
+ // Extract the forbidden thing
1520
+ const match = intentLower.match(/(?:never use|don't use|avoid)\s+(\w+(?:\s+\w+)?)/i);
1521
+ if (match) {
1522
+ const forbidden = match[1].toLowerCase();
1523
+ if (codeLower.includes(forbidden)) {
1524
+ violations.push(`⚠️ **Convention Violation** \`id:${conv.id}\`: "${conv.intent}" — Found \`${forbidden}\` in your code`);
1525
+ }
1237
1526
  }
1238
1527
  }
1239
- catch { /* non-fatal */ }
1240
- }
1241
- // ─── BRAIN LAYER 12: Architecture Graph (deep understanding) ──────
1242
- if (workspaceRoot && (0, feature_gate_1.isFeatureAllowed)('architectureGraph')) {
1243
- try {
1244
- const archGraph = (0, architecture_graph_1.buildArchitectureGraph)(workspaceRoot);
1245
- if (archGraph.totalFiles > 0) {
1246
- const archText = (0, architecture_graph_1.formatArchitectureGraph)(archGraph);
1247
- if (archText)
1248
- parts.push('\n' + archText);
1528
+ if (intentLower.includes('always use') || intentLower.includes('must use')) {
1529
+ const match = intentLower.match(/(?:always use|must use)\s+(\w+(?:\s+\w+)?)/i);
1530
+ if (match) {
1531
+ const required = match[1].toLowerCase();
1532
+ if (!codeLower.includes(required) && code.length > 50) {
1533
+ suggestions.push(`💡 **Convention Suggestion** \`id:${conv.id}\`: "${conv.intent}" — Consider using \`${required}\``);
1534
+ }
1249
1535
  }
1250
1536
  }
1251
- catch { /* non-fatal */ }
1252
1537
  }
1253
- // ─── SMART CONTEXT SELECTION: Trim to token budget ───────────────
1254
- // ─── SMART CONTEXT TRIMMING: Remove low-priority sections first ─
1255
- const MAX_CHARS = 12000; // ~3000 tokens — fits any model
1256
- let output;
1257
- if (parts.join('\n').length > MAX_CHARS) {
1258
- // Priority order: keep critical layers, trim optional ones from the end
1259
- // Parts are added in priority order (sessions, corrections, core context first)
1260
- // So we trim from the END (architecture graph, export map, knowledge gaps)
1261
- let trimmedParts = [...parts];
1262
- let totalLen = trimmedParts.join('\n').length;
1263
- while (totalLen > MAX_CHARS && trimmedParts.length > 3) {
1264
- trimmedParts.pop(); // Remove lowest priority section
1265
- totalLen = trimmedParts.join('\n').length;
1266
- }
1267
- output = trimmedParts.join('\n');
1268
- if (output.length > MAX_CHARS) {
1269
- output = output.slice(0, MAX_CHARS);
1538
+ // Check against stored BUG_FIX patterns
1539
+ const bugFixes = memoryStore.getByType('BUG_FIX', 50);
1540
+ for (const bug of bugFixes) {
1541
+ const bugLower = bug.intent.toLowerCase();
1542
+ // Extract key terms from bug description
1543
+ const bugTerms = bugLower.split(/\s+/).filter(w => w.length > 4);
1544
+ const matchCount = bugTerms.filter(t => codeLower.includes(t)).length;
1545
+ if (matchCount >= 3) {
1546
+ violations.push(`🐛 **Similar Bug Pattern** \`id:${bug.id}\`: "${bug.intent}" — This code has similarities to a past bug`);
1270
1547
  }
1271
- output += '\n\n> (Lower-priority context removed to fit token budget. Use `recall_memory` for specific queries.)';
1272
- } else {
1273
- output = parts.join('\n');
1274
1548
  }
1275
- // ─── DAY 1 EMPTY BRAIN: Guide new users ──────────────────────────
1276
- if (memoryStore.activeCount() === 0) {
1277
- return {
1278
- jsonrpc: '2.0', id,
1279
- result: { content: [{ type: 'text', text: '## 🧠 Cortex Brain — Day 1\n\nNo memories yet. Here is how to build your brain:\n\n1. **Scan this project** — call `scan_project` with the workspace root. This creates memories from your package.json, README, git history, and code structure.\n2. **Just work normally** — after every response, `auto_learn` will automatically extract and store decisions, corrections, and bug fixes.\n3. **Store manually** — use `store_memory` or `quick_store` for important rules.\n\nOnce you have memories, `force_recall` will inject them here automatically.' }] },
1280
- };
1549
+ // Check for CORRECTION patterns
1550
+ const corrections = memoryStore.getByType('CORRECTION', 50);
1551
+ for (const corr of corrections) {
1552
+ const corrLower = corr.intent.toLowerCase();
1553
+ const corrTerms = corrLower.split(/\s+/).filter(w => w.length > 4);
1554
+ const matchCount = corrTerms.filter(t => codeLower.includes(t)).length;
1555
+ if (matchCount >= 3) {
1556
+ violations.push(`🔄 **Past Correction Applies** \`id:${corr.id}\`: "${corr.intent}"`);
1557
+ }
1558
+ }
1559
+ // File-specific memories
1560
+ if (filename) {
1561
+ const fileMemories = memoryStore.getByFile(filename, 10);
1562
+ for (const fm of fileMemories) {
1563
+ suggestions.push(`📄 **File Note** \`id:${fm.id}\`: "${fm.intent}"`);
1564
+ }
1565
+ }
1566
+ (0, usage_stats_1.trackReview)();
1567
+ const lines = ['# Cortex Code Review\n'];
1568
+ if (violations.length > 0) {
1569
+ lines.push(`## ⚠️ ${violations.length} Issue${violations.length > 1 ? 's' : ''} Found\n`);
1570
+ violations.forEach(v => lines.push(v));
1571
+ }
1572
+ if (suggestions.length > 0) {
1573
+ lines.push(`\n## 💡 ${suggestions.length} Suggestion${suggestions.length > 1 ? 's' : ''}\n`);
1574
+ suggestions.forEach(s => lines.push(s));
1575
+ }
1576
+ if (violations.length === 0 && suggestions.length === 0) {
1577
+ lines.push('✅ **No issues found.** Code looks clean against your stored conventions and past bugs.');
1578
+ lines.push(`\n_Checked against ${conventions.length} conventions, ${bugFixes.length} bug fixes, ${corrections.length} corrections._`);
1579
+ }
1580
+ else {
1581
+ lines.push(`\n_Reviewed against ${conventions.length} conventions, ${bugFixes.length} bug fixes, ${corrections.length} corrections._`);
1281
1582
  }
1282
1583
  return {
1283
1584
  jsonrpc: '2.0', id,
1284
- result: { content: [{ type: 'text', text: output }] },
1585
+ result: { content: [{ type: 'text', text: lines.join('\n') }] },
1285
1586
  };
1286
1587
  }
1287
1588
  catch (err) {
1288
1589
  return {
1289
1590
  jsonrpc: '2.0', id,
1290
- result: { content: [{ type: 'text', text: `Force recall error: ${err.message}` }], isError: true },
1591
+ result: { content: [{ type: 'text', text: `Review error: ${err.message}` }], isError: true },
1291
1592
  };
1292
1593
  }
1293
1594
  }
@@ -1431,7 +1732,7 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1431
1732
  }
1432
1733
  async function handleAutoLearn(id, args) {
1433
1734
  try {
1434
- // Feature gate check (auto_learn is now free!)
1735
+ // Feature gate (launch mode: all features unlocked)
1435
1736
  if (!(0, feature_gate_1.isFeatureAllowed)('autoLearn')) {
1436
1737
  return {
1437
1738
  jsonrpc: '2.0', id,
@@ -1455,6 +1756,50 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1455
1756
  }
1456
1757
  // Extract memory-worthy patterns (regex-based)
1457
1758
  const extracted = (0, auto_learner_1.extractMemories)(text);
1759
+ (0, usage_stats_1.trackAutoLearn)(); // Track for Brain Health Score + Streak
1760
+ // SUCCESS DETECTION: capture proven approaches
1761
+ try {
1762
+ const successSignals = (0, success_tracker_1.detectSuccess)(text);
1763
+ for (const signal of successSignals) {
1764
+ const successMemory = (0, success_tracker_1.buildSuccessMemory)(signal, text);
1765
+ extracted.push({
1766
+ type: 'INSIGHT',
1767
+ content: successMemory.intent,
1768
+ confidence: signal.confidence,
1769
+ reason: successMemory.reason,
1770
+ });
1771
+ (0, usage_stats_1.trackSuccess)();
1772
+ }
1773
+ }
1774
+ catch { /* success detection failed — non-fatal */ }
1775
+ // ERROR FINGERPRINT: capture error patterns for instant recall
1776
+ try {
1777
+ if ((0, error_learner_1.containsErrors)(text)) {
1778
+ const errorPatterns = (0, error_learner_1.extractErrorPatterns)(text);
1779
+ for (const ep of errorPatterns.slice(0, 3)) {
1780
+ extracted.push({
1781
+ type: 'BUG_FIX',
1782
+ content: ep.message,
1783
+ confidence: ep.confidence,
1784
+ reason: `Error pattern: ${ep.errorType} — auto-captured for instant fix recall`,
1785
+ });
1786
+ (0, usage_stats_1.trackErrorLearned)();
1787
+ }
1788
+ }
1789
+ }
1790
+ catch { /* error learning failed — non-fatal */ }
1791
+ // COMPLETION DETECTION: demote old memories about completed topics
1792
+ try {
1793
+ const completionSignals = (0, completion_resolver_1.detectCompletion)(text);
1794
+ for (const signal of completionSignals) {
1795
+ const resolved = (0, completion_resolver_1.resolveRelatedMemories)(memoryStore, signal.topic, signal.confidence);
1796
+ if (resolved > 0) {
1797
+ console.log(` 🏁 Completion: "${signal.topic}" — demoted ${resolved} old memories`);
1798
+ (0, memory_cache_1.invalidateCache)();
1799
+ }
1800
+ }
1801
+ }
1802
+ catch { /* completion detection failed — non-fatal */ }
1458
1803
  // LLM enhancement: when API key is available and regex found nothing,
1459
1804
  // use LLM to catch implicit patterns that keywords miss
1460
1805
  if (extracted.length === 0 && (0, llm_enhancer_1.isLLMAvailable)() && text.length > 50) {
@@ -1482,6 +1827,62 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1482
1827
  };
1483
1828
  }
1484
1829
  // Store each extracted memory + feed session tracker
1830
+ // --- Helper: Independent importance scoring ---
1831
+ function calculateImportance(item) {
1832
+ // Base importance by type (corrections/bugs are more important than insights)
1833
+ const TYPE_IMPORTANCE = {
1834
+ CORRECTION: 0.85, BUG_FIX: 0.85, CONVENTION: 0.80,
1835
+ DECISION: 0.75, GOTCHA: 0.75, BUSINESS_RULE: 0.70,
1836
+ FAILED_ATTEMPT: 0.65, CURRENT_TASK: 0.60, INSIGHT: 0.55,
1837
+ };
1838
+ let importance = TYPE_IMPORTANCE[item.type] || 0.60;
1839
+ // Boost for content signals that indicate higher value
1840
+ if (/\b(always|never|must|critical|important|breaking)\b/i.test(item.content))
1841
+ importance += 0.08;
1842
+ if (/\b(error|bug|crash|fail|exception)\b/i.test(item.content))
1843
+ importance += 0.05;
1844
+ if (/\.(ts|js|py|go|rs|java|tsx|jsx)\b/.test(item.content))
1845
+ importance += 0.03; // file-specific
1846
+ if (/v?\d+\.\d+/.test(item.content))
1847
+ importance += 0.02; // version numbers
1848
+ // Blend with regex confidence (40% type-based, 60% regex confidence)
1849
+ importance = importance * 0.4 + item.confidence * 0.6;
1850
+ return Math.min(importance, 1.0);
1851
+ }
1852
+ // --- Helper: Extract topic tags from content ---
1853
+ function extractTopicTags(item) {
1854
+ const tags = [item.type.toLowerCase()];
1855
+ const content = item.content.toLowerCase();
1856
+ // Extract technology/framework mentions
1857
+ const techPatterns = /\b(react|vue|angular|next\.?js|node|express|typescript|javascript|python|rust|go|docker|kubernetes|postgres|mongodb|redis|graphql|rest|api|css|html|webpack|vite|eslint|git|npm|yarn)\b/gi;
1858
+ const techMatches = item.content.match(techPatterns);
1859
+ if (techMatches) {
1860
+ for (const tech of new Set(techMatches.map(t => t.toLowerCase()))) {
1861
+ tags.push(tech);
1862
+ }
1863
+ }
1864
+ // Extract file extensions as topic hints
1865
+ const fileExts = content.match(/\.(ts|js|py|go|rs|java|tsx|jsx|css|html|json|yaml|yml|md)\b/g);
1866
+ if (fileExts) {
1867
+ for (const ext of new Set(fileExts)) {
1868
+ tags.push(ext.replace('.', ''));
1869
+ }
1870
+ }
1871
+ // Extract key action verbs as context
1872
+ if (/\b(migrat|switch|chang|replac|upgrad|delet|remov)\w*/i.test(content))
1873
+ tags.push('migration');
1874
+ if (/\b(test|spec|assert|expect|mock)\b/i.test(content))
1875
+ tags.push('testing');
1876
+ if (/\b(deploy|ci|cd|pipeline|build|release)\b/i.test(content))
1877
+ tags.push('devops');
1878
+ if (/\b(auth|login|token|session|permission|role)\b/i.test(content))
1879
+ tags.push('auth');
1880
+ if (/\b(database|query|schema|table|index|sql)\b/i.test(content))
1881
+ tags.push('database');
1882
+ if (/\b(performance|speed|slow|fast|optimize|cache)\b/i.test(content))
1883
+ tags.push('performance');
1884
+ return [...new Set(tags)].slice(0, 8); // Cap at 8 tags
1885
+ }
1485
1886
  const stored = [];
1486
1887
  const skipped = [];
1487
1888
  for (const item of extracted) {
@@ -1507,31 +1908,25 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1507
1908
  (0, session_tracker_1.feedSession)({ topic: item.content.slice(0, 60) });
1508
1909
  break;
1509
1910
  }
1510
- // Map auto_learn string types to MemoryType enum values
1511
- // (auto_learner returns uppercase like "DECISION", but MemoryType enum uses lowercase like "decision")
1512
- const autoLearnTypeMap = {
1513
- 'DECISION': types_1.MemoryType.DECISION || 'decision',
1514
- 'CORRECTION': types_1.MemoryType.CORRECTION || 'correction',
1515
- 'BUG_FIX': types_1.MemoryType.BUG_FIX || 'bug_fix',
1516
- 'CONVENTION': types_1.MemoryType.CONVENTION || 'convention',
1517
- 'INSIGHT': types_1.MemoryType.INSIGHT || 'insight',
1518
- 'FAILED_ATTEMPT': types_1.MemoryType.FAILED_SUGGESTION || 'failed_suggestion',
1519
- 'BUSINESS_RULE': types_1.MemoryType.DECISION || 'decision',
1520
- 'GOTCHA': types_1.MemoryType.CORRECTION || 'correction',
1521
- 'CURRENT_TASK': types_1.MemoryType.INSIGHT || 'insight',
1522
- };
1523
- const mappedType = autoLearnTypeMap[item.type] || item.type.toLowerCase();
1524
1911
  const result = (0, memory_quality_1.storeWithQuality)(memoryStore, {
1525
- type: mappedType,
1912
+ type: item.type,
1526
1913
  intent: item.content,
1527
- action: item.content,
1914
+ action: `auto_learn:${item.type.toLowerCase()}`,
1528
1915
  reason: item.reason,
1529
1916
  confidence: item.confidence,
1530
- importance: item.confidence,
1531
- tags: [item.type.toLowerCase()],
1917
+ importance: calculateImportance(item),
1918
+ tags: extractTopicTags(item),
1532
1919
  });
1533
- if (result) {
1920
+ if (result.stored) {
1534
1921
  stored.push(`[${item.type}] ${item.content.slice(0, 60)}${item.content.length > 60 ? '…' : ''}`);
1922
+ // OpenClaw-style diary logging
1923
+ try {
1924
+ (0, daily_diary_1.appendDiaryEntry)({
1925
+ type: item.type.toLowerCase().replace('_', '_'),
1926
+ content: item.content.slice(0, 100),
1927
+ });
1928
+ }
1929
+ catch { /* diary is non-critical */ }
1535
1930
  }
1536
1931
  else {
1537
1932
  skipped.push(`[${item.type}] ${item.content.slice(0, 40)}… (duplicate)`);
@@ -1543,6 +1938,144 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1543
1938
  }
1544
1939
  if (stored.length > 0) {
1545
1940
  (0, memory_cache_1.invalidateCache)();
1941
+ for (let i = 0; i < stored.length; i++)
1942
+ (0, usage_stats_1.trackStore)();
1943
+ // OpenClaw: Auto-export curated MEMORY.md after storing new memories
1944
+ if (workspaceRoot && stored.length >= 2) {
1945
+ try {
1946
+ (0, memory_export_md_1.generateMemoryMd)(memoryStore, workspaceRoot);
1947
+ }
1948
+ catch { /* non-fatal */ }
1949
+ }
1950
+ }
1951
+ // ─── Auto-correction capture ─────────────────────────────────────
1952
+ // Scan AI text for self-corrections ("I apologize", "you're right")
1953
+ const aiCorrections = (0, correction_detector_1.detectAIAcknowledgments)(text);
1954
+ for (const corr of aiCorrections) {
1955
+ try {
1956
+ const corrResult = (0, memory_quality_1.storeWithQuality)(memoryStore, {
1957
+ type: 'CORRECTION',
1958
+ intent: `[AUTO-DETECTED] ${corr.fullContext}`,
1959
+ action: corr.fullContext,
1960
+ reason: `AI self-correction detected (confidence: ${corr.confidence})`,
1961
+ confidence: corr.confidence,
1962
+ importance: 0.90, // High importance — corrections prevent repeats
1963
+ tags: ['auto-correction', 'ai-acknowledgment'],
1964
+ });
1965
+ if (corrResult.stored) {
1966
+ stored.push(`[CORRECTION] ${corr.fullContext.slice(0, 60)}…`);
1967
+ }
1968
+ }
1969
+ catch { /* skip */ }
1970
+ }
1971
+ // Scan user context for direct corrections ("no, use X not Y")
1972
+ const userContext = args.context;
1973
+ if (userContext && userContext.length > 10) {
1974
+ const userCorrections = (0, correction_detector_1.detectUserCorrections)(userContext);
1975
+ for (const corr of userCorrections) {
1976
+ try {
1977
+ const content = corr.corrected
1978
+ ? `User correction: use "${corr.corrected}"${corr.original ? ` instead of "${corr.original}"` : ''}`
1979
+ : `User correction: ${corr.fullContext}`;
1980
+ const corrResult = (0, memory_quality_1.storeWithQuality)(memoryStore, {
1981
+ type: 'CORRECTION',
1982
+ intent: content,
1983
+ action: corr.fullContext,
1984
+ reason: `User correction detected (confidence: ${corr.confidence})`,
1985
+ confidence: corr.confidence,
1986
+ importance: 0.95, // Very high — user corrections are gospel
1987
+ tags: ['auto-correction', 'user-correction'],
1988
+ });
1989
+ if (corrResult.stored) {
1990
+ stored.push(`[USER CORRECTION] ${content.slice(0, 60)}…`);
1991
+ (0, memory_cache_1.invalidateCache)();
1992
+ }
1993
+ }
1994
+ catch { /* skip */ }
1995
+ }
1996
+ // ─── Preference learning ─────────────────────────────────
1997
+ const prefs = (0, preference_learner_1.detectPreferences)(userContext);
1998
+ for (const pref of prefs) {
1999
+ try {
2000
+ const prefResult = (0, memory_quality_1.storeWithQuality)(memoryStore, {
2001
+ type: 'CONVENTION',
2002
+ intent: pref.preference,
2003
+ action: `Detected from: "${pref.evidence}"`,
2004
+ reason: `User preference auto-detected (${pref.category})`,
2005
+ confidence: pref.confidence,
2006
+ importance: 0.85,
2007
+ tags: ['preference', pref.category],
2008
+ });
2009
+ if (prefResult.stored) {
2010
+ stored.push(`[PREFERENCE] ${pref.preference.slice(0, 60)}…`);
2011
+ (0, memory_cache_1.invalidateCache)();
2012
+ }
2013
+ }
2014
+ catch { /* skip */ }
2015
+ }
2016
+ }
2017
+ // ─── Build error learning ──────────────────────────────────
2018
+ // Scan for TS errors, test failures in AI text
2019
+ if ((0, error_learner_1.containsErrors)(text)) {
2020
+ const errorPatterns = (0, error_learner_1.extractErrorPatterns)(text);
2021
+ // Extract verification steps for regression prevention
2022
+ const verifySteps = (0, regression_guard_1.extractVerificationSteps)(text);
2023
+ for (const ep of errorPatterns) {
2024
+ try {
2025
+ const baseAction = `Auto-captured from build/test output`;
2026
+ const actionWithVerify = (0, regression_guard_1.attachVerification)(baseAction, verifySteps);
2027
+ const errResult = (0, memory_quality_1.storeWithQuality)(memoryStore, {
2028
+ type: 'BUG_FIX',
2029
+ intent: `[ERROR PATTERN] ${ep.errorType}: ${ep.message}`,
2030
+ action: actionWithVerify,
2031
+ reason: `Error pattern auto-detected — avoid this in future`,
2032
+ confidence: ep.confidence,
2033
+ importance: 0.90,
2034
+ tags: ['error-pattern', ep.errorType.toLowerCase(), 'auto-detected'],
2035
+ });
2036
+ if (errResult.stored) {
2037
+ stored.push(`[ERROR LEARNED] ${ep.errorType}: ${ep.message.slice(0, 50)}…`);
2038
+ (0, memory_cache_1.invalidateCache)();
2039
+ }
2040
+ }
2041
+ catch { /* skip */ }
2042
+ }
2043
+ }
2044
+ // ─── Success reinforcement ─────────────────────────────────
2045
+ // Scan user context for praise/success signals
2046
+ if (userContext && userContext.length > 5) {
2047
+ const successSignals = (0, success_tracker_1.detectSuccess)(userContext);
2048
+ for (const signal of successSignals) {
2049
+ try {
2050
+ const mem = (0, success_tracker_1.buildSuccessMemory)(signal, text);
2051
+ const successResult = (0, memory_quality_1.storeWithQuality)(memoryStore, {
2052
+ type: 'INSIGHT',
2053
+ intent: mem.intent,
2054
+ action: text.slice(0, 200),
2055
+ reason: mem.reason,
2056
+ confidence: signal.confidence,
2057
+ importance: 0.80,
2058
+ tags: mem.tags,
2059
+ });
2060
+ if (successResult.stored) {
2061
+ stored.push(`[SUCCESS] Proven approach stored from: "${signal.trigger}"`);
2062
+ (0, memory_cache_1.invalidateCache)();
2063
+ }
2064
+ }
2065
+ catch { /* skip */ }
2066
+ }
2067
+ }
2068
+ // ─── File relationship tracking ────────────────────────────
2069
+ // Track files mentioned in context for co-edit detection
2070
+ if (userContext) {
2071
+ const filePatterns = userContext.match(/[\w-]+\.\w{1,5}/g) || [];
2072
+ const codeFileExts = new Set(['ts', 'tsx', 'js', 'jsx', 'css', 'py', 'go', 'rs', 'java']);
2073
+ for (const f of filePatterns) {
2074
+ const ext = f.split('.').pop()?.toLowerCase() || '';
2075
+ if (codeFileExts.has(ext)) {
2076
+ (0, file_relationships_1.recordFileEdit)(f);
2077
+ }
2078
+ }
1546
2079
  }
1547
2080
  const lines = ['**Auto-Learn Results:**'];
1548
2081
  if (stored.length > 0) {
@@ -1625,14 +2158,16 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1625
2158
  '# Cortex Health Check\n',
1626
2159
  `| Metric | Value |`,
1627
2160
  `|--------|-------|`,
1628
- `| Plan | **Full Brain — 100% Free** |`,
1629
2161
  `| Active Memories | ${activeCount} |`,
1630
- `| Brain Layers | All 12 Active |`,
1631
- `| Session Store Count | ${stats.storeCount}/500 |`,
1632
- `| Session Auto-Learn Count | ${stats.autoLearnCount}/1000 |`,
1633
- `| Session Total Calls | ${stats.totalCalls}/5000 |`,
2162
+ `| Session Store Count | ${stats.storeCount}/100 |`,
2163
+ `| Session Auto-Learn Count | ${stats.autoLearnCount}/500 |`,
2164
+ `| Session Total Calls | ${stats.totalCalls}/2000 |`,
1634
2165
  `| Uptime | ${Math.floor(stats.uptime / 60)}m ${stats.uptime % 60}s |`,
1635
- `| Status | Healthy |`,
2166
+ `| Diary Days | ${(0, daily_diary_1.getDiaryStats)().totalDays} |`,
2167
+ `| Today's Diary Entries | ${(0, daily_diary_1.getDiaryStats)().todayEntries} |`,
2168
+ `| Top Memory Types | ${(0, access_pattern_tracker_1.getTopTypes)(3).map(t => `${t.type}(${t.count})`).join(', ') || 'none yet'} |`,
2169
+ `| Access Profile | ${(0, access_pattern_tracker_1.getAccessProfile)() ? 'loaded' : 'empty'} |`,
2170
+ `| Status | Healthy |`,
1636
2171
  ];
1637
2172
  return {
1638
2173
  jsonrpc: '2.0', id,
@@ -1646,428 +2181,101 @@ function createMCPHandler(memoryStore, eventLog, workspaceRoot) {
1646
2181
  };
1647
2182
  }
1648
2183
  }
1649
- // ═══ NEW TOOLS: search_by_file, pin, unpin, undo, clear, timeline, thumbs ═══
1650
- function handleSearchByFile(id, args) {
2184
+ // ─── Pre-Flight Check Handler ───────────────────────────────────────────────
2185
+ function handlePreCheck(id, args) {
1651
2186
  try {
1652
- const filePath = args.filePath;
1653
- const limit = args.limit || 20;
1654
- if (!filePath) {
1655
- return {
1656
- jsonrpc: '2.0', id,
1657
- result: { content: [{ type: 'text', text: 'Error: filePath is required' }], isError: true },
1658
- };
2187
+ // Track file for relationship mapping
2188
+ if (args.filename)
2189
+ (0, file_relationships_1.recordFileEdit)(args.filename);
2190
+ const result = (0, pre_flight_1.preFlightCheck)(memoryStore, args.filename, args.task);
2191
+ let text = (0, pre_flight_1.formatPreFlight)(result);
2192
+ // Add file relationship warnings
2193
+ if (args.filename) {
2194
+ const warnings = (0, file_relationships_1.checkMissingRelated)(args.filename, memoryStore);
2195
+ if (warnings.length > 0) {
2196
+ text += '\n\n## 🔗 File Relationships\n';
2197
+ warnings.forEach(w => text += w + '\n');
2198
+ }
1659
2199
  }
1660
- // Use the existing getByFile method + FTS fallback
1661
- let memories = memoryStore.getByFile(filePath, limit);
1662
- // Also do FTS search for the filename
1663
- if (memories.length < limit) {
1664
- const basename = filePath.split(/[\\/]/).pop() || filePath;
2200
+ // Add architecture context show file's role in the system
2201
+ if (args.filename && workspaceRoot) {
1665
2202
  try {
1666
- const ftsResults = memoryStore.searchFTS(basename, limit - memories.length);
1667
- const existingIds = new Set(memories.map(m => m.id));
1668
- for (const r of ftsResults) {
1669
- if (!existingIds.has(r.memory.id)) {
1670
- memories.push(r.memory);
2203
+ const archGraph = (0, architecture_graph_1.buildArchitectureGraph)(workspaceRoot);
2204
+ const basename = args.filename.replace(/\\/g, '/').split('/').pop() || args.filename;
2205
+ // nodes is Map<string, ArchNode> — find by key ending with basename
2206
+ let deps = null;
2207
+ for (const [key, node] of archGraph.nodes) {
2208
+ if (key.endsWith(basename)) {
2209
+ deps = node;
2210
+ break;
1671
2211
  }
1672
2212
  }
1673
- } catch { /* FTS may fail on special characters */ }
1674
- }
1675
- if (memories.length === 0) {
1676
- return {
1677
- jsonrpc: '2.0', id,
1678
- result: { content: [{ type: 'text', text: `No memories found for file: "${filePath}"` }] },
1679
- };
1680
- }
1681
- const lines = [`# Memories related to "${filePath}"\n`];
1682
- for (const m of memories) {
1683
- const age = Math.floor((Date.now() - m.createdAt) / 86400000);
1684
- lines.push(`- **[${m.type}]** ${m.intent}`);
1685
- if (m.reason) lines.push(` _${m.reason}_`);
1686
- lines.push(` \`id: ${m.id}\` · ${age}d old`);
1687
- }
1688
- lines.push(`\n_Total: ${memories.length} memories for this file._`);
1689
- return {
1690
- jsonrpc: '2.0', id,
1691
- result: { content: [{ type: 'text', text: lines.join('\n') }] },
1692
- };
1693
- } catch (err) {
1694
- return {
1695
- jsonrpc: '2.0', id,
1696
- result: { content: [{ type: 'text', text: `search_by_file error: ${err.message}` }], isError: true },
1697
- };
1698
- }
1699
- }
1700
- function handlePinMemory(id, args) {
1701
- try {
1702
- const memoryId = args.id;
1703
- if (!memoryId) {
1704
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: id is required' }], isError: true } };
1705
- }
1706
- const existing = memoryStore.get(memoryId);
1707
- if (!existing) {
1708
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `Error: Memory ${memoryId} not found` }], isError: true } };
1709
- }
1710
- const tags = existing.tags || [];
1711
- if (!tags.includes('pinned')) tags.push('pinned');
1712
- memoryStore.update(memoryId, { importance: 1.0, tags });
1713
- (0, memory_cache_1.invalidateCache)();
1714
- return {
1715
- jsonrpc: '2.0', id,
1716
- result: { content: [{ type: 'text', text: `📌 Memory PINNED: "${existing.intent}"\n\nThis memory will ALWAYS be included in context, regardless of topic.` }] },
1717
- };
1718
- } catch (err) {
1719
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `pin error: ${err.message}` }], isError: true } };
1720
- }
1721
- }
1722
- function handleUnpinMemory(id, args) {
1723
- try {
1724
- const memoryId = args.id;
1725
- if (!memoryId) {
1726
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: id is required' }], isError: true } };
1727
- }
1728
- const existing = memoryStore.get(memoryId);
1729
- if (!existing) {
1730
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `Error: Memory ${memoryId} not found` }], isError: true } };
1731
- }
1732
- const tags = (existing.tags || []).filter(t => t !== 'pinned');
1733
- memoryStore.update(memoryId, { importance: 0.7, tags });
1734
- (0, memory_cache_1.invalidateCache)();
1735
- return {
1736
- jsonrpc: '2.0', id,
1737
- result: { content: [{ type: 'text', text: `📌 Memory UNPINNED: "${existing.intent}"\n\nThis memory will now use normal priority ranking.` }] },
1738
- };
1739
- } catch (err) {
1740
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `unpin error: ${err.message}` }], isError: true } };
1741
- }
1742
- }
1743
- function handleUndoLast(id, args) {
1744
- try {
1745
- const count = Math.min(Math.max(args.count || 1, 1), 10);
1746
- // Get the N most recently CREATED active memories (order by created_at, not timestamp)
1747
- const db = memoryStore.db;
1748
- const recentRows = db.prepare('SELECT * FROM memory_units WHERE is_active = 1 ORDER BY created_at DESC LIMIT ?').all(count);
1749
- const recent = recentRows.map(r => memoryStore.get(r.id)).filter(Boolean);
1750
- if (recent.length === 0) {
1751
- return {
1752
- jsonrpc: '2.0', id,
1753
- result: { content: [{ type: 'text', text: 'No memories to undo.' }] },
1754
- };
1755
- }
1756
- const toUndo = recent;
1757
- const undone = [];
1758
- for (const m of toUndo) {
1759
- memoryStore.deactivate(m.id);
1760
- undone.push(`- [${m.type}] "${m.intent.slice(0, 80)}"`);
2213
+ if (deps && (deps.imports?.length > 0 || deps.importedBy?.length > 0)) {
2214
+ text += '\n\n## \ud83c\udfd7\ufe0f Architecture Context';
2215
+ if (deps.imports?.length > 0) {
2216
+ text += `\n**Imports from:** ${deps.imports.slice(0, 10).join(', ')}`;
2217
+ }
2218
+ if (deps.importedBy?.length > 0) {
2219
+ text += `\n**Imported by:** ${deps.importedBy.slice(0, 10).join(', ')}`;
2220
+ }
2221
+ }
2222
+ }
2223
+ catch { /* non-fatal */ }
1761
2224
  }
1762
- (0, memory_cache_1.invalidateCache)();
1763
2225
  return {
1764
2226
  jsonrpc: '2.0', id,
1765
- result: { content: [{ type: 'text', text: `⏪ Undone ${undone.length} memory(ies):\n\n${undone.join('\n')}` }] },
2227
+ result: { content: [{ type: 'text', text }] },
1766
2228
  };
1767
- } catch (err) {
1768
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `undo error: ${err.message}` }], isError: true } };
1769
2229
  }
1770
- }
1771
- function handleClearMemories(id, args) {
1772
- try {
1773
- const type = args.type;
1774
- const olderThanDays = args.olderThanDays;
1775
- if (!type) {
1776
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: type is required' }], isError: true } };
1777
- }
1778
- let query = 'SELECT * FROM memory_units WHERE is_active = 1';
1779
- const params = [];
1780
- if (type !== 'ALL') {
1781
- query += ' AND type = ?';
1782
- params.push(type);
1783
- }
1784
- if (olderThanDays) {
1785
- const cutoff = Date.now() - (olderThanDays * 86400000);
1786
- query += ' AND created_at < ?';
1787
- params.push(cutoff);
1788
- }
1789
- const rows = memoryStore.db ? memoryStore.db.prepare(query).all.apply(memoryStore.db.prepare(query), params) : [];
1790
- let cleared = 0;
1791
- for (const row of rows) {
1792
- memoryStore.deactivate(row.id);
1793
- cleared++;
1794
- }
1795
- (0, memory_cache_1.invalidateCache)();
1796
- const ageMsg = olderThanDays ? ` older than ${olderThanDays} days` : '';
2230
+ catch (err) {
1797
2231
  return {
1798
2232
  jsonrpc: '2.0', id,
1799
- result: { content: [{ type: 'text', text: `🗑️ Cleared ${cleared} ${type === 'ALL' ? '' : type + ' '}memories${ageMsg}.` }] },
2233
+ result: { content: [{ type: 'text', text: `Pre-check error: ${err.message}` }], isError: true },
1800
2234
  };
1801
- } catch (err) {
1802
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `clear error: ${err.message}` }], isError: true } };
1803
- }
1804
- }
1805
- function parseRelativeDate(str) {
1806
- if (!str) return Date.now();
1807
- const lower = str.toLowerCase().trim();
1808
- if (lower === 'now' || lower === 'today') return Date.now();
1809
- if (lower === 'yesterday') return Date.now() - 86400000;
1810
- // "N days ago", "N hours ago", "N weeks ago"
1811
- const relMatch = lower.match(/^(\d+)\s+(day|hour|week|month)s?\s+ago$/);
1812
- if (relMatch) {
1813
- const n = parseInt(relMatch[1], 10);
1814
- const unit = relMatch[2];
1815
- const multipliers = { hour: 3600000, day: 86400000, week: 604800000, month: 2592000000 };
1816
- return Date.now() - (n * (multipliers[unit] || 86400000));
1817
2235
  }
1818
- // Try ISO date
1819
- const parsed = new Date(str).getTime();
1820
- return isNaN(parsed) ? Date.now() - 604800000 : parsed; // default to 7 days ago
1821
2236
  }
1822
- function handleSearchTimeline(id, args) {
2237
+ // ─── Impact Analysis Handler ───────────────────────────────────────────────
2238
+ function handleCheckImpact(id, args) {
1823
2239
  try {
1824
- const fromTs = parseRelativeDate(args.from);
1825
- const toTs = parseRelativeDate(args.to || 'now');
1826
- const type = args.type;
1827
- let query = 'SELECT * FROM memory_units WHERE is_active = 1 AND created_at >= ? AND created_at <= ?';
1828
- const params = [fromTs, toTs];
1829
- if (type && type !== 'ALL') {
1830
- query += ' AND type = ?';
1831
- params.push(type);
1832
- }
1833
- query += ' ORDER BY created_at DESC LIMIT 50';
1834
- const db = memoryStore.db || memoryStore.connection;
1835
- const stmt = db.prepare(query);
1836
- const rows = stmt.all.apply(stmt, params);
1837
- if (rows.length === 0) {
2240
+ const file = args.file;
2241
+ if (!file) {
1838
2242
  return {
1839
2243
  jsonrpc: '2.0', id,
1840
- result: { content: [{ type: 'text', text: `No memories found between ${new Date(fromTs).toLocaleDateString()} and ${new Date(toTs).toLocaleDateString()}.` }] },
2244
+ result: { content: [{ type: 'text', text: 'Error: file parameter is required' }], isError: true },
1841
2245
  };
1842
2246
  }
1843
- const lines = [`# Timeline: ${new Date(fromTs).toLocaleDateString()} ${new Date(toTs).toLocaleDateString()}\n`];
1844
- let lastDate = '';
1845
- for (const row of rows) {
1846
- const dateStr = new Date(row.created_at).toLocaleDateString();
1847
- if (dateStr !== lastDate) {
1848
- lines.push(`\n## ${dateStr}`);
1849
- lastDate = dateStr;
1850
- }
1851
- lines.push(`- **[${row.type}]** ${row.intent}`);
1852
- if (row.reason) lines.push(` _${row.reason}_`);
1853
- }
1854
- lines.push(`\n_Total: ${rows.length} memories in this period._`);
1855
- return {
1856
- jsonrpc: '2.0', id,
1857
- result: { content: [{ type: 'text', text: lines.join('\n') }] },
1858
- };
1859
- } catch (err) {
1860
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `timeline error: ${err.message}` }], isError: true } };
1861
- }
1862
- }
1863
- function handleThumbsUp(id, args) {
1864
- try {
1865
- const memoryId = args.id;
1866
- if (!memoryId) {
1867
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: id is required' }], isError: true } };
1868
- }
1869
- const existing = memoryStore.get(memoryId);
1870
- if (!existing) {
1871
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `Error: Memory ${memoryId} not found` }], isError: true } };
1872
- }
1873
- // Boost importance (cap at 1.0)
1874
- const newImportance = Math.min(1.0, (existing.importance || 0.5) + 0.1);
1875
- memoryStore.update(memoryId, { importance: newImportance });
1876
- // Record in user_signals table
1877
- try {
1878
- const db = memoryStore.db || memoryStore.connection;
1879
- db.prepare('INSERT INTO user_signals (memory_id, signal, timestamp) VALUES (?, ?, ?)')
1880
- .run(memoryId, 'thumbs_up', Date.now());
1881
- } catch { /* table might not exist */ }
1882
- (0, memory_cache_1.invalidateCache)();
2247
+ const wsRoot = args.workspaceRoot || process.cwd();
2248
+ const result = (0, impact_analyzer_1.analyzeImpact)(file, wsRoot);
2249
+ const text = (0, impact_analyzer_1.formatImpact)(result);
1883
2250
  return {
1884
2251
  jsonrpc: '2.0', id,
1885
- result: { content: [{ type: 'text', text: `👍 Upvoted: "${existing.intent.slice(0, 80)}"\nImportance: ${(newImportance * 100).toFixed(0)}%` }] },
2252
+ result: { content: [{ type: 'text', text }] },
1886
2253
  };
1887
- } catch (err) {
1888
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `thumbs_up error: ${err.message}` }], isError: true } };
1889
2254
  }
1890
- }
1891
- function handleThumbsDown(id, args) {
1892
- try {
1893
- const memoryId = args.id;
1894
- const reason = args.reason || 'Not useful';
1895
- if (!memoryId) {
1896
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: id is required' }], isError: true } };
1897
- }
1898
- const existing = memoryStore.get(memoryId);
1899
- if (!existing) {
1900
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `Error: Memory ${memoryId} not found` }], isError: true } };
1901
- }
1902
- // Demote importance
1903
- const newImportance = Math.max(0.0, (existing.importance || 0.5) - 0.15);
1904
- memoryStore.update(memoryId, { importance: newImportance });
1905
- // Record in user_signals table
1906
- let downvoteCount = 0;
1907
- try {
1908
- const db = memoryStore.db || memoryStore.connection;
1909
- db.prepare('INSERT INTO user_signals (memory_id, signal, correction, timestamp) VALUES (?, ?, ?, ?)')
1910
- .run(memoryId, 'thumbs_down', reason, Date.now());
1911
- // Count total downvotes for this memory
1912
- const row = db.prepare('SELECT COUNT(*) as cnt FROM user_signals WHERE memory_id = ? AND signal = ?')
1913
- .get(memoryId, 'thumbs_down');
1914
- downvoteCount = row ? row.cnt : 0;
1915
- } catch { /* table might not exist */ }
1916
- // Auto-deactivate if 3+ downvotes
1917
- let deactivated = false;
1918
- if (downvoteCount >= 3) {
1919
- memoryStore.deactivate(memoryId);
1920
- deactivated = true;
1921
- }
1922
- (0, memory_cache_1.invalidateCache)();
1923
- let msg = `👎 Downvoted: "${existing.intent.slice(0, 80)}"\nImportance: ${(newImportance * 100).toFixed(0)}%\nReason: ${reason}`;
1924
- if (deactivated) {
1925
- msg += `\n\n⚠️ Memory auto-deactivated (received ${downvoteCount} downvotes).`;
1926
- }
2255
+ catch (err) {
1927
2256
  return {
1928
2257
  jsonrpc: '2.0', id,
1929
- result: { content: [{ type: 'text', text: msg }] },
2258
+ result: { content: [{ type: 'text', text: `Impact analysis error: ${err.message}` }], isError: true },
1930
2259
  };
1931
- } catch (err) {
1932
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `thumbs_down error: ${err.message}` }], isError: true } };
1933
2260
  }
1934
2261
  }
1935
- function handleBackupBrain(id) {
2262
+ // ─── Resume Work Handler ───────────────────────────────────────────────────
2263
+ function handleResumeWork(id) {
1936
2264
  try {
1937
- const fs = require('fs');
1938
- const path = require('path');
1939
- const dbPath = memoryStore.db?.name || memoryStore._dbPath;
1940
- if (!dbPath || !fs.existsSync(dbPath)) {
1941
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: Cannot locate database file.' }], isError: true } };
1942
- }
1943
- const backupDate = new Date().toISOString().replace(/[:.]/g, '-');
1944
- const backupPath = path.join(path.dirname(dbPath), `cognitive-backup-${backupDate}.db`);
1945
- // Close active WAL before backup
1946
- memoryStore.checkpoint?.();
1947
- fs.copyFileSync(dbPath, backupPath);
2265
+ const ctx = (0, resume_work_1.buildResumeContext)(memoryStore);
2266
+ const text = (0, resume_work_1.formatResumeContext)(ctx);
1948
2267
  return {
1949
2268
  jsonrpc: '2.0', id,
1950
- result: { content: [{ type: 'text', text: `✅ Database backed up successfully!\n\nSaved to: ${backupPath}\nSize: ${(fs.statSync(backupPath).size / 1024 / 1024).toFixed(2)} MB` }] },
2269
+ result: { content: [{ type: 'text', text }] },
1951
2270
  };
1952
- } catch (err) {
1953
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `backup error: ${err.message}` }], isError: true } };
1954
2271
  }
1955
- }
1956
- function handleGetDailySummary(id, args) {
1957
- try {
1958
- const dateStr = args.date || new Date().toISOString().split('T')[0];
1959
- const db = memoryStore.db || memoryStore.connection;
1960
- // First check if it exists
1961
- const row = db.prepare('SELECT summary FROM daily_summaries WHERE date = ?').get(dateStr);
1962
- if (row) {
1963
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `# Summary for ${dateStr}\n\n${row.summary}` }] } };
1964
- }
1965
- // If checking today and no summary exists, let's create a dynamic one
1966
- const isToday = dateStr === new Date().toISOString().split('T')[0];
1967
- if (!isToday) {
1968
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `No summary found for ${dateStr}.` }] } };
1969
- }
1970
- // Generate dynamic summary for today
1971
- const startOfDay = new Date(dateStr).setHours(0,0,0,0);
1972
- const todayMemories = db.prepare('SELECT * FROM memory_units WHERE is_active = 1 AND created_at >= ? ORDER BY created_at ASC').all(startOfDay);
1973
- if (todayMemories.length === 0) {
1974
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `No memories recorded yet today (${dateStr}).` }] } };
1975
- }
1976
- const lines = [];
1977
- let types = { DECISION: 0, BUG_FIX: 0, CORRECTION: 0, CONVENTION: 0 };
1978
- for (const m of todayMemories) {
1979
- if (types[m.type] !== undefined) types[m.type]++;
1980
- lines.push(`- **[${m.type}]** ${m.intent}`);
1981
- }
1982
- const summaryText = `Today we recorded ${todayMemories.length} memories (${types.DECISION} decisions, ${types.BUG_FIX} fixes, ${types.CORRECTION} corrections, ${types.CONVENTION} conventions).\n\nKey updates:\n${lines.join('\n')}`;
1983
- // Save it
1984
- db.prepare('INSERT OR REPLACE INTO daily_summaries (date, summary, created_at) VALUES (?, ?, ?)')
1985
- .run(dateStr, summaryText, Date.now());
1986
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `# Summary for Today (${dateStr})\n\n${summaryText}` }] } };
1987
- } catch (err) {
1988
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `summary error: ${err.message}` }], isError: true } };
1989
- }
1990
- }
1991
- function handleAnalytics(id) {
1992
- try {
1993
- const db = memoryStore.db || memoryStore.connection;
1994
- const stats = [];
1995
- // Basic counts
1996
- const total = db.prepare('SELECT COUNT(*) as c FROM memory_units').get().c;
1997
- const active = db.prepare('SELECT COUNT(*) as c FROM memory_units WHERE is_active = 1').get().c;
1998
- const pinned = db.prepare("SELECT COUNT(*) as c FROM memory_units WHERE is_active = 1 AND tags LIKE '%pinned%'").get().c;
1999
- stats.push(`## 📊 Brain Health\n- Total Memories: ${total}\n- Active Memories: ${active}\n- Pinned Rules: ${pinned}`);
2000
- // By Type
2001
- stats.push('\n## 🧩 Distribution by Type');
2002
- const types = db.prepare('SELECT type, COUNT(*) as c FROM memory_units WHERE is_active = 1 GROUP BY type ORDER BY c DESC').all();
2003
- for (const t of types) stats.push(`- **${t.type}**: ${t.c}`);
2004
- // Most Accessed
2005
- stats.push('\n## 🔥 Most Accessed Memories (Top 5)');
2006
- const hot = db.prepare('SELECT id, type, intent, access_count FROM memory_units WHERE is_active = 1 AND access_count > 0 ORDER BY access_count DESC LIMIT 5').all();
2007
- if (hot.length) {
2008
- for (const h of hot) stats.push(`- [${h.access_count}x] **${h.type}**: ${h.intent} (id: ${h.id})`);
2009
- } else {
2010
- stats.push('- No memory accesses recorded yet.');
2011
- }
2012
- // Feedback stats
2013
- try {
2014
- const up = db.prepare("SELECT COUNT(*) as c FROM user_signals WHERE signal = 'thumbs_up'").get().c;
2015
- const down = db.prepare("SELECT COUNT(*) as c FROM user_signals WHERE signal = 'thumbs_down'").get().c;
2016
- if (up > 0 || down > 0) {
2017
- stats.push(`\n## 👍 User Feedback\n- Upvotes: ${up}\n- Downvotes: ${down}`);
2018
- }
2019
- } catch { /* ignore if table missing */ }
2272
+ catch (err) {
2020
2273
  return {
2021
2274
  jsonrpc: '2.0', id,
2022
- result: { content: [{ type: 'text', text: stats.join('\n') }] },
2275
+ result: { content: [{ type: 'text', text: `Resume work error: ${err.message}` }], isError: true },
2023
2276
  };
2024
- } catch (err) {
2025
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: `analytics error: ${err.message}` }], isError: true } };
2026
2277
  }
2027
2278
  }
2028
2279
  return { handleMCPRequest };
2029
2280
  }
2030
- // ─── NEW: Export/Import/Graph Handlers ─────────────────────────────────────
2031
- async function handleExportMemoriesFn(memoryStore, id, args) {
2032
- try {
2033
- const { filePath } = args;
2034
- if (!filePath) return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: filePath is required' }], isError: true } };
2035
- const result = (0, export_import_1.exportToFile)(memoryStore, filePath);
2036
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: '✅ Exported ' + result.count + ' memories to:\n' + result.path + '\n\nShare this file with teammates or use import_memories to restore.' }] } };
2037
- } catch (err) {
2038
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'export error: ' + err.message }], isError: true } };
2039
- }
2040
- }
2041
- async function handleImportMemoriesFn(memoryStore, memory_cache_1, id, args) {
2042
- try {
2043
- const { filePath } = args;
2044
- if (!filePath) return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: filePath is required' }], isError: true } };
2045
- const result = (0, export_import_1.importFromFile)(memoryStore, filePath);
2046
- if (result.imported > 0) (0, memory_cache_1.invalidateCache)();
2047
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: '✅ Import complete!\n\n- Imported: ' + result.imported + ' memories\n- Skipped (duplicates): ' + result.skipped + '\n- Errors: ' + result.errors }] } };
2048
- } catch (err) {
2049
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'import error: ' + err.message }], isError: true } };
2050
- }
2051
- }
2052
- async function handleGetRelatedMemoriesFn(memoryStore, id, args) {
2053
- try {
2054
- const memoryId = args.id;
2055
- const maxHops = args.maxHops || 2;
2056
- if (!memoryId) return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Error: id is required' }], isError: true } };
2057
- const hops = Math.min(4, Math.max(1, maxHops));
2058
- const starting = memoryStore.get(memoryId);
2059
- if (!starting) return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'No memory found with id: ' + memoryId }] } };
2060
- const related = memoryStore.getRelated(memoryId, hops, 20);
2061
- if (related.length === 0) {
2062
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'Memory: [' + starting.type + '] ' + starting.intent + '\n\nNo related memories found yet (no graph edges). Use store_memory to build connections.' }] } };
2063
- }
2064
- const lines = ['## Starting Memory', '[' + starting.type + '] ' + starting.intent + ' (id: ' + starting.id + ')', '', '## Related Memories (via graph)'];
2065
- for (const r of related) {
2066
- lines.push('- [' + r.memory.type + '] ' + r.memory.intent + ' (id: ' + r.memory.id + ') (' + (r.score * 100).toFixed(0) + '% relevance)');
2067
- }
2068
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: lines.join('\n') }] } };
2069
- } catch (err) {
2070
- return { jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: 'get_related error: ' + err.message }], isError: true } };
2071
- }
2072
- }
2073
2281
  //# sourceMappingURL=mcp-handler.js.map