agent-working-memory 0.3.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 (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +311 -0
  3. package/dist/api/index.d.ts +2 -0
  4. package/dist/api/index.d.ts.map +1 -0
  5. package/dist/api/index.js +2 -0
  6. package/dist/api/index.js.map +1 -0
  7. package/dist/api/routes.d.ts +53 -0
  8. package/dist/api/routes.d.ts.map +1 -0
  9. package/dist/api/routes.js +388 -0
  10. package/dist/api/routes.js.map +1 -0
  11. package/dist/cli.d.ts +12 -0
  12. package/dist/cli.d.ts.map +1 -0
  13. package/dist/cli.js +245 -0
  14. package/dist/cli.js.map +1 -0
  15. package/dist/core/decay.d.ts +36 -0
  16. package/dist/core/decay.d.ts.map +1 -0
  17. package/dist/core/decay.js +38 -0
  18. package/dist/core/decay.js.map +1 -0
  19. package/dist/core/embeddings.d.ts +33 -0
  20. package/dist/core/embeddings.d.ts.map +1 -0
  21. package/dist/core/embeddings.js +76 -0
  22. package/dist/core/embeddings.js.map +1 -0
  23. package/dist/core/hebbian.d.ts +38 -0
  24. package/dist/core/hebbian.d.ts.map +1 -0
  25. package/dist/core/hebbian.js +74 -0
  26. package/dist/core/hebbian.js.map +1 -0
  27. package/dist/core/index.d.ts +4 -0
  28. package/dist/core/index.d.ts.map +1 -0
  29. package/dist/core/index.js +4 -0
  30. package/dist/core/index.js.map +1 -0
  31. package/dist/core/query-expander.d.ts +24 -0
  32. package/dist/core/query-expander.d.ts.map +1 -0
  33. package/dist/core/query-expander.js +58 -0
  34. package/dist/core/query-expander.js.map +1 -0
  35. package/dist/core/reranker.d.ts +25 -0
  36. package/dist/core/reranker.d.ts.map +1 -0
  37. package/dist/core/reranker.js +75 -0
  38. package/dist/core/reranker.js.map +1 -0
  39. package/dist/core/salience.d.ts +30 -0
  40. package/dist/core/salience.d.ts.map +1 -0
  41. package/dist/core/salience.js +81 -0
  42. package/dist/core/salience.js.map +1 -0
  43. package/dist/engine/activation.d.ts +38 -0
  44. package/dist/engine/activation.d.ts.map +1 -0
  45. package/dist/engine/activation.js +516 -0
  46. package/dist/engine/activation.js.map +1 -0
  47. package/dist/engine/connections.d.ts +31 -0
  48. package/dist/engine/connections.d.ts.map +1 -0
  49. package/dist/engine/connections.js +74 -0
  50. package/dist/engine/connections.js.map +1 -0
  51. package/dist/engine/consolidation-scheduler.d.ts +31 -0
  52. package/dist/engine/consolidation-scheduler.d.ts.map +1 -0
  53. package/dist/engine/consolidation-scheduler.js +115 -0
  54. package/dist/engine/consolidation-scheduler.js.map +1 -0
  55. package/dist/engine/consolidation.d.ts +62 -0
  56. package/dist/engine/consolidation.d.ts.map +1 -0
  57. package/dist/engine/consolidation.js +368 -0
  58. package/dist/engine/consolidation.js.map +1 -0
  59. package/dist/engine/eval.d.ts +22 -0
  60. package/dist/engine/eval.d.ts.map +1 -0
  61. package/dist/engine/eval.js +79 -0
  62. package/dist/engine/eval.js.map +1 -0
  63. package/dist/engine/eviction.d.ts +29 -0
  64. package/dist/engine/eviction.d.ts.map +1 -0
  65. package/dist/engine/eviction.js +86 -0
  66. package/dist/engine/eviction.js.map +1 -0
  67. package/dist/engine/index.d.ts +7 -0
  68. package/dist/engine/index.d.ts.map +1 -0
  69. package/dist/engine/index.js +7 -0
  70. package/dist/engine/index.js.map +1 -0
  71. package/dist/engine/retraction.d.ts +32 -0
  72. package/dist/engine/retraction.d.ts.map +1 -0
  73. package/dist/engine/retraction.js +77 -0
  74. package/dist/engine/retraction.js.map +1 -0
  75. package/dist/engine/staging.d.ts +33 -0
  76. package/dist/engine/staging.d.ts.map +1 -0
  77. package/dist/engine/staging.js +63 -0
  78. package/dist/engine/staging.js.map +1 -0
  79. package/dist/index.d.ts +2 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +95 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/mcp.d.ts +24 -0
  84. package/dist/mcp.d.ts.map +1 -0
  85. package/dist/mcp.js +532 -0
  86. package/dist/mcp.js.map +1 -0
  87. package/dist/storage/index.d.ts +2 -0
  88. package/dist/storage/index.d.ts.map +1 -0
  89. package/dist/storage/index.js +2 -0
  90. package/dist/storage/index.js.map +1 -0
  91. package/dist/storage/sqlite.d.ts +116 -0
  92. package/dist/storage/sqlite.d.ts.map +1 -0
  93. package/dist/storage/sqlite.js +750 -0
  94. package/dist/storage/sqlite.js.map +1 -0
  95. package/dist/types/agent.d.ts +30 -0
  96. package/dist/types/agent.d.ts.map +1 -0
  97. package/dist/types/agent.js +23 -0
  98. package/dist/types/agent.js.map +1 -0
  99. package/dist/types/checkpoint.d.ts +50 -0
  100. package/dist/types/checkpoint.d.ts.map +1 -0
  101. package/dist/types/checkpoint.js +8 -0
  102. package/dist/types/checkpoint.js.map +1 -0
  103. package/dist/types/engram.d.ts +165 -0
  104. package/dist/types/engram.d.ts.map +1 -0
  105. package/dist/types/engram.js +8 -0
  106. package/dist/types/engram.js.map +1 -0
  107. package/dist/types/eval.d.ts +84 -0
  108. package/dist/types/eval.d.ts.map +1 -0
  109. package/dist/types/eval.js +11 -0
  110. package/dist/types/eval.js.map +1 -0
  111. package/dist/types/index.d.ts +5 -0
  112. package/dist/types/index.d.ts.map +1 -0
  113. package/dist/types/index.js +5 -0
  114. package/dist/types/index.js.map +1 -0
  115. package/package.json +55 -0
  116. package/src/api/index.ts +1 -0
  117. package/src/api/routes.ts +528 -0
  118. package/src/cli.ts +260 -0
  119. package/src/core/decay.ts +61 -0
  120. package/src/core/embeddings.ts +82 -0
  121. package/src/core/hebbian.ts +91 -0
  122. package/src/core/index.ts +3 -0
  123. package/src/core/query-expander.ts +64 -0
  124. package/src/core/reranker.ts +99 -0
  125. package/src/core/salience.ts +95 -0
  126. package/src/engine/activation.ts +577 -0
  127. package/src/engine/connections.ts +101 -0
  128. package/src/engine/consolidation-scheduler.ts +123 -0
  129. package/src/engine/consolidation.ts +443 -0
  130. package/src/engine/eval.ts +100 -0
  131. package/src/engine/eviction.ts +99 -0
  132. package/src/engine/index.ts +6 -0
  133. package/src/engine/retraction.ts +98 -0
  134. package/src/engine/staging.ts +72 -0
  135. package/src/index.ts +100 -0
  136. package/src/mcp.ts +635 -0
  137. package/src/storage/index.ts +1 -0
  138. package/src/storage/sqlite.ts +893 -0
  139. package/src/types/agent.ts +65 -0
  140. package/src/types/checkpoint.ts +44 -0
  141. package/src/types/engram.ts +194 -0
  142. package/src/types/eval.ts +98 -0
  143. package/src/types/index.ts +4 -0
package/src/mcp.ts ADDED
@@ -0,0 +1,635 @@
1
+ /**
2
+ * MCP Server — Model Context Protocol interface for AgentWorkingMemory.
3
+ *
4
+ * Runs as a stdio-based MCP server that Claude Code connects to directly.
5
+ * Uses the storage and engine layers in-process (no HTTP overhead).
6
+ *
7
+ * Tools exposed (11):
8
+ * memory_write — store a memory (salience filter decides disposition)
9
+ * memory_recall — activate memories by context (cognitive retrieval)
10
+ * memory_feedback — report whether a recalled memory was useful
11
+ * memory_retract — invalidate a wrong memory with optional correction
12
+ * memory_stats — get memory health metrics
13
+ * memory_checkpoint — save structured execution state (survives compaction)
14
+ * memory_restore — restore state + targeted recall after compaction
15
+ * memory_task_add — create a prioritized task
16
+ * memory_task_update — change task status, priority, or blocking
17
+ * memory_task_list — list tasks filtered by status
18
+ * memory_task_next — get the highest-priority actionable task
19
+ *
20
+ * Run: npx tsx src/mcp.ts
21
+ * Config: add to ~/.claude.json or .mcp.json
22
+ */
23
+
24
+ import { readFileSync } from 'node:fs';
25
+ import { resolve } from 'node:path';
26
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
27
+
28
+ // Load .env file if present (no external dependency)
29
+ try {
30
+ const envPath = resolve(process.cwd(), '.env');
31
+ const envContent = readFileSync(envPath, 'utf-8');
32
+ for (const line of envContent.split('\n')) {
33
+ const trimmed = line.trim();
34
+ if (!trimmed || trimmed.startsWith('#')) continue;
35
+ const eqIdx = trimmed.indexOf('=');
36
+ if (eqIdx === -1) continue;
37
+ const key = trimmed.slice(0, eqIdx).trim();
38
+ const val = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, '');
39
+ if (!process.env[key]) process.env[key] = val;
40
+ }
41
+ } catch { /* No .env file */ }
42
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
43
+ import { z } from 'zod';
44
+
45
+ import { EngramStore } from './storage/sqlite.js';
46
+ import { ActivationEngine } from './engine/activation.js';
47
+ import { ConnectionEngine } from './engine/connections.js';
48
+ import { StagingBuffer } from './engine/staging.js';
49
+ import { EvictionEngine } from './engine/eviction.js';
50
+ import { RetractionEngine } from './engine/retraction.js';
51
+ import { EvalEngine } from './engine/eval.js';
52
+ import { ConsolidationEngine } from './engine/consolidation.js';
53
+ import { ConsolidationScheduler } from './engine/consolidation-scheduler.js';
54
+ import { evaluateSalience } from './core/salience.js';
55
+ import type { ConsciousState } from './types/checkpoint.js';
56
+ import type { SalienceEventType } from './core/salience.js';
57
+ import type { TaskStatus, TaskPriority } from './types/engram.js';
58
+ import { DEFAULT_AGENT_CONFIG } from './types/agent.js';
59
+ import { embed } from './core/embeddings.js';
60
+
61
+ // --- Setup ---
62
+
63
+ const DB_PATH = process.env.AWM_DB_PATH ?? 'memory.db';
64
+ const AGENT_ID = process.env.AWM_AGENT_ID ?? 'claude-code';
65
+
66
+ const store = new EngramStore(DB_PATH);
67
+ const activationEngine = new ActivationEngine(store);
68
+ const connectionEngine = new ConnectionEngine(store, activationEngine);
69
+ const stagingBuffer = new StagingBuffer(store, activationEngine);
70
+ const evictionEngine = new EvictionEngine(store);
71
+ const retractionEngine = new RetractionEngine(store);
72
+ const evalEngine = new EvalEngine(store);
73
+ const consolidationEngine = new ConsolidationEngine(store);
74
+ const consolidationScheduler = new ConsolidationScheduler(store, consolidationEngine);
75
+
76
+ stagingBuffer.start(DEFAULT_AGENT_CONFIG.stagingTtlMs);
77
+ consolidationScheduler.start();
78
+
79
+ const server = new McpServer({
80
+ name: 'agent-working-memory',
81
+ version: '0.3.0',
82
+ });
83
+
84
+ // --- Tools ---
85
+
86
+ server.tool(
87
+ 'memory_write',
88
+ `Store a memory. The salience filter decides whether it's worth keeping (active), needs more evidence (staging), or should be discarded.
89
+
90
+ Use this when you learn something that might be useful later:
91
+ - Discoveries about the codebase, bugs, or architecture
92
+ - Decisions you made and why
93
+ - Errors encountered and how they were resolved
94
+ - User preferences or project patterns
95
+
96
+ The concept should be a short label (3-8 words). The content should be the full detail.`,
97
+ {
98
+ concept: z.string().describe('Short label for this memory (3-8 words)'),
99
+ content: z.string().describe('Full detail of what was learned'),
100
+ tags: z.array(z.string()).optional().describe('Optional tags for categorization'),
101
+ event_type: z.enum(['observation', 'decision', 'friction', 'surprise', 'causal'])
102
+ .optional().default('observation')
103
+ .describe('Type of event: observation (default), decision, friction (error/blocker), surprise, causal (root cause)'),
104
+ surprise: z.number().min(0).max(1).optional().default(0.3)
105
+ .describe('How surprising was this? 0=expected, 1=very unexpected'),
106
+ decision_made: z.boolean().optional().default(false)
107
+ .describe('Was a decision made? True boosts importance'),
108
+ causal_depth: z.number().min(0).max(1).optional().default(0.3)
109
+ .describe('How deep is the causal understanding? 0=surface, 1=root cause'),
110
+ resolution_effort: z.number().min(0).max(1).optional().default(0.3)
111
+ .describe('How much effort to resolve? 0=trivial, 1=significant debugging'),
112
+ },
113
+ async (params) => {
114
+ const salience = evaluateSalience({
115
+ content: params.content,
116
+ eventType: params.event_type as SalienceEventType,
117
+ surprise: params.surprise,
118
+ decisionMade: params.decision_made,
119
+ causalDepth: params.causal_depth,
120
+ resolutionEffort: params.resolution_effort,
121
+ });
122
+
123
+ if (salience.disposition === 'discard') {
124
+ return {
125
+ content: [{
126
+ type: 'text' as const,
127
+ text: `Memory discarded (salience ${salience.score.toFixed(2)} below threshold). Not worth storing.`,
128
+ }],
129
+ };
130
+ }
131
+
132
+ const engram = store.createEngram({
133
+ agentId: AGENT_ID,
134
+ concept: params.concept,
135
+ content: params.content,
136
+ tags: params.tags,
137
+ salience: salience.score,
138
+ salienceFeatures: salience.features,
139
+ reasonCodes: salience.reasonCodes,
140
+ ttl: salience.disposition === 'staging' ? DEFAULT_AGENT_CONFIG.stagingTtlMs : undefined,
141
+ });
142
+
143
+ if (salience.disposition === 'staging') {
144
+ store.updateStage(engram.id, 'staging');
145
+ } else {
146
+ connectionEngine.enqueue(engram.id);
147
+ }
148
+
149
+ // Generate embedding asynchronously (don't block response)
150
+ embed(`${params.concept} ${params.content}`).then(vec => {
151
+ store.updateEmbedding(engram.id, vec);
152
+ }).catch(() => {}); // Embedding failure is non-fatal
153
+
154
+ // Auto-checkpoint: track write
155
+ try { store.updateAutoCheckpointWrite(AGENT_ID, engram.id); } catch { /* non-fatal */ }
156
+
157
+ return {
158
+ content: [{
159
+ type: 'text' as const,
160
+ text: `Memory stored (${salience.disposition}). ID: ${engram.id}\nConcept: ${params.concept}\nSalience: ${salience.score.toFixed(2)}`,
161
+ }],
162
+ };
163
+ }
164
+ );
165
+
166
+ server.tool(
167
+ 'memory_recall',
168
+ `Recall memories relevant to a context. Uses cognitive activation — not keyword search.
169
+
170
+ Use this when you need to remember something:
171
+ - Starting a new task (recall relevant past experience)
172
+ - Debugging (recall similar errors and solutions)
173
+ - Making decisions (recall past decisions and outcomes)
174
+
175
+ Returns the most relevant memories ranked by a composite score of text relevance, temporal recency, and associative strength.`,
176
+ {
177
+ context: z.string().describe('What are you thinking about? Describe the current situation or question'),
178
+ limit: z.number().optional().default(5).describe('Max memories to return (default 5)'),
179
+ min_score: z.number().optional().default(0.05).describe('Minimum relevance score (default 0.05)'),
180
+ include_staging: z.boolean().optional().default(false).describe('Include weak/unconfirmed memories?'),
181
+ use_reranker: z.boolean().optional().default(true).describe('Use cross-encoder re-ranking for better relevance (default true)'),
182
+ use_expansion: z.boolean().optional().default(true).describe('Expand query with synonyms for better recall (default true)'),
183
+ },
184
+ async (params) => {
185
+ const results = await activationEngine.activate({
186
+ agentId: AGENT_ID,
187
+ context: params.context,
188
+ limit: params.limit,
189
+ minScore: params.min_score,
190
+ includeStaging: params.include_staging,
191
+ useReranker: params.use_reranker,
192
+ useExpansion: params.use_expansion,
193
+ });
194
+
195
+ // Auto-checkpoint: track recall
196
+ try {
197
+ const ids = results.map(r => r.engram.id);
198
+ store.updateAutoCheckpointRecall(AGENT_ID, params.context, ids);
199
+ } catch { /* non-fatal */ }
200
+
201
+ if (results.length === 0) {
202
+ return {
203
+ content: [{
204
+ type: 'text' as const,
205
+ text: 'No relevant memories found.',
206
+ }],
207
+ };
208
+ }
209
+
210
+ const lines = results.map((r, i) => {
211
+ const tags = r.engram.tags?.length ? ` [${r.engram.tags.join(', ')}]` : '';
212
+ return `${i + 1}. **${r.engram.concept}** (score: ${r.score.toFixed(3)})${tags}\n ${r.engram.content}\n _${r.why}_\n ID: ${r.engram.id}`;
213
+ });
214
+
215
+ return {
216
+ content: [{
217
+ type: 'text' as const,
218
+ text: `Recalled ${results.length} memories:\n\n${lines.join('\n\n')}`,
219
+ }],
220
+ };
221
+ }
222
+ );
223
+
224
+ server.tool(
225
+ 'memory_feedback',
226
+ `Report whether a recalled memory was actually useful. This updates the memory's confidence score — useful memories become stronger, useless ones weaken.
227
+
228
+ Always call this after using a recalled memory so the system learns what's valuable.`,
229
+ {
230
+ engram_id: z.string().describe('ID of the memory (from memory_recall results)'),
231
+ useful: z.boolean().describe('Was this memory actually helpful?'),
232
+ context: z.string().optional().describe('Brief note on why it was/wasn\'t useful'),
233
+ },
234
+ async (params) => {
235
+ store.logRetrievalFeedback(null, params.engram_id, params.useful, params.context ?? '');
236
+
237
+ const engram = store.getEngram(params.engram_id);
238
+ if (engram) {
239
+ const delta = params.useful
240
+ ? DEFAULT_AGENT_CONFIG.feedbackPositiveBoost
241
+ : -DEFAULT_AGENT_CONFIG.feedbackNegativePenalty;
242
+ store.updateConfidence(engram.id, engram.confidence + delta);
243
+ }
244
+
245
+ return {
246
+ content: [{
247
+ type: 'text' as const,
248
+ text: `Feedback recorded: ${params.useful ? 'useful' : 'not useful'}. Confidence ${params.useful ? 'increased' : 'decreased'}.`,
249
+ }],
250
+ };
251
+ }
252
+ );
253
+
254
+ server.tool(
255
+ 'memory_retract',
256
+ `Retract a memory that turned out to be wrong. Creates a correction and reduces confidence of related memories.
257
+
258
+ Use this when you discover a memory contains incorrect information.`,
259
+ {
260
+ engram_id: z.string().describe('ID of the wrong memory'),
261
+ reason: z.string().describe('Why is this memory wrong?'),
262
+ correction: z.string().optional().describe('What is the correct information? (creates a new memory)'),
263
+ },
264
+ async (params) => {
265
+ const result = retractionEngine.retract({
266
+ agentId: AGENT_ID,
267
+ targetEngramId: params.engram_id,
268
+ reason: params.reason,
269
+ counterContent: params.correction,
270
+ });
271
+
272
+ const parts = [`Memory ${params.engram_id} retracted.`];
273
+ if (result.correctionId) {
274
+ parts.push(`Correction stored as ${result.correctionId}.`);
275
+ }
276
+ parts.push(`${result.associatesAffected} related memories had confidence reduced.`);
277
+
278
+ return {
279
+ content: [{
280
+ type: 'text' as const,
281
+ text: parts.join(' '),
282
+ }],
283
+ };
284
+ }
285
+ );
286
+
287
+ server.tool(
288
+ 'memory_stats',
289
+ `Get memory health stats — how many memories, confidence levels, association count, and system performance.`,
290
+ {},
291
+ async () => {
292
+ const metrics = evalEngine.computeMetrics(AGENT_ID);
293
+ const lines = [
294
+ `Agent: ${AGENT_ID}`,
295
+ `Active memories: ${metrics.activeEngramCount}`,
296
+ `Staging: ${metrics.stagingEngramCount}`,
297
+ `Retracted: ${metrics.retractedCount}`,
298
+ `Avg confidence: ${metrics.avgConfidence.toFixed(3)}`,
299
+ `Total edges: ${metrics.totalEdges}`,
300
+ `Edge utility: ${(metrics.edgeUtilityRate * 100).toFixed(1)}%`,
301
+ `Activations (24h): ${metrics.activationCount}`,
302
+ `Avg latency: ${metrics.avgLatencyMs.toFixed(1)}ms`,
303
+ ];
304
+
305
+ return {
306
+ content: [{
307
+ type: 'text' as const,
308
+ text: lines.join('\n'),
309
+ }],
310
+ };
311
+ }
312
+ );
313
+
314
+ // --- Checkpointing Tools ---
315
+
316
+ server.tool(
317
+ 'memory_checkpoint',
318
+ `Save your current execution state so you can recover after context compaction.
319
+
320
+ Use this when:
321
+ - You're about to do something that might fill the context window
322
+ - You've made important decisions you don't want to lose
323
+ - You want to preserve your working state before a long operation
324
+
325
+ The state is saved per-agent and overwrites any previous checkpoint.`,
326
+ {
327
+ current_task: z.string().describe('What you are currently working on'),
328
+ decisions: z.array(z.string()).optional().default([])
329
+ .describe('Key decisions made so far'),
330
+ active_files: z.array(z.string()).optional().default([])
331
+ .describe('Files you are currently working with'),
332
+ next_steps: z.array(z.string()).optional().default([])
333
+ .describe('What needs to happen next'),
334
+ related_memory_ids: z.array(z.string()).optional().default([])
335
+ .describe('IDs of memories relevant to current work'),
336
+ notes: z.string().optional().default('')
337
+ .describe('Any other context worth preserving'),
338
+ episode_id: z.string().optional()
339
+ .describe('Current episode ID if known'),
340
+ },
341
+ async (params) => {
342
+ const state: ConsciousState = {
343
+ currentTask: params.current_task,
344
+ decisions: params.decisions,
345
+ activeFiles: params.active_files,
346
+ nextSteps: params.next_steps,
347
+ relatedMemoryIds: params.related_memory_ids,
348
+ notes: params.notes,
349
+ episodeId: params.episode_id ?? null,
350
+ };
351
+
352
+ store.saveCheckpoint(AGENT_ID, state);
353
+
354
+ return {
355
+ content: [{
356
+ type: 'text' as const,
357
+ text: `Checkpoint saved.\nTask: ${params.current_task}\nDecisions: ${params.decisions.length}\nNext steps: ${params.next_steps.length}\nFiles: ${params.active_files.length}`,
358
+ }],
359
+ };
360
+ }
361
+ );
362
+
363
+ server.tool(
364
+ 'memory_restore',
365
+ `Restore your previous execution state after context compaction or at session start.
366
+
367
+ Returns:
368
+ - Your saved execution state (task, decisions, next steps, files)
369
+ - Recently recalled memories for context
370
+ - Your last write for continuity
371
+ - How long you were idle
372
+
373
+ Use this at the start of every session or after compaction to pick up where you left off.`,
374
+ {},
375
+ async () => {
376
+ const checkpoint = store.getCheckpoint(AGENT_ID);
377
+
378
+ const now = Date.now();
379
+ const idleMs = checkpoint
380
+ ? now - checkpoint.auto.lastActivityAt.getTime()
381
+ : 0;
382
+
383
+ // Get last written engram
384
+ let lastWrite: { id: string; concept: string; content: string } | null = null;
385
+ if (checkpoint?.auto.lastWriteId) {
386
+ const engram = store.getEngram(checkpoint.auto.lastWriteId);
387
+ if (engram) {
388
+ lastWrite = { id: engram.id, concept: engram.concept, content: engram.content };
389
+ }
390
+ }
391
+
392
+ // Recall memories using last context
393
+ let recalledMemories: Array<{ id: string; concept: string; content: string; score: number }> = [];
394
+ const recallContext = checkpoint?.auto.lastRecallContext
395
+ ?? checkpoint?.executionState?.currentTask
396
+ ?? null;
397
+
398
+ if (recallContext) {
399
+ try {
400
+ const results = await activationEngine.activate({
401
+ agentId: AGENT_ID,
402
+ context: recallContext,
403
+ limit: 5,
404
+ minScore: 0.05,
405
+ useReranker: true,
406
+ useExpansion: true,
407
+ });
408
+ recalledMemories = results.map(r => ({
409
+ id: r.engram.id,
410
+ concept: r.engram.concept,
411
+ content: r.engram.content,
412
+ score: r.score,
413
+ }));
414
+ } catch { /* recall failure is non-fatal */ }
415
+ }
416
+
417
+ // Trigger mini-consolidation if idle >5min
418
+ const MINI_IDLE_MS = 5 * 60_000;
419
+ let miniConsolidationTriggered = false;
420
+ if (idleMs > MINI_IDLE_MS) {
421
+ miniConsolidationTriggered = true;
422
+ consolidationScheduler.runMiniConsolidation(AGENT_ID).catch(() => {});
423
+ }
424
+
425
+ // Format response
426
+ const parts: string[] = [];
427
+ const idleMin = Math.round(idleMs / 60_000);
428
+ parts.push(`Idle: ${idleMin}min${miniConsolidationTriggered ? ' (mini-consolidation triggered)' : ''}`);
429
+
430
+ if (checkpoint?.executionState) {
431
+ const s = checkpoint.executionState;
432
+ parts.push(`\n**Current task:** ${s.currentTask}`);
433
+ if (s.decisions.length) parts.push(`**Decisions:** ${s.decisions.join('; ')}`);
434
+ if (s.nextSteps.length) parts.push(`**Next steps:** ${s.nextSteps.map((st, i) => `${i + 1}. ${st}`).join(', ')}`);
435
+ if (s.activeFiles.length) parts.push(`**Active files:** ${s.activeFiles.join(', ')}`);
436
+ if (s.notes) parts.push(`**Notes:** ${s.notes}`);
437
+ if (checkpoint.checkpointAt) parts.push(`_Saved at: ${checkpoint.checkpointAt.toISOString()}_`);
438
+ } else {
439
+ parts.push('\nNo explicit checkpoint saved.');
440
+ }
441
+
442
+ if (lastWrite) {
443
+ parts.push(`\n**Last write:** ${lastWrite.concept}\n${lastWrite.content}`);
444
+ }
445
+
446
+ if (recalledMemories.length > 0) {
447
+ parts.push(`\n**Recalled memories (${recalledMemories.length}):**`);
448
+ for (const m of recalledMemories) {
449
+ parts.push(`- **${m.concept}** (${m.score.toFixed(3)}): ${m.content.slice(0, 150)}${m.content.length > 150 ? '...' : ''}`);
450
+ }
451
+ }
452
+
453
+ return {
454
+ content: [{
455
+ type: 'text' as const,
456
+ text: parts.join('\n'),
457
+ }],
458
+ };
459
+ }
460
+ );
461
+
462
+ // --- Task Management Tools ---
463
+
464
+ server.tool(
465
+ 'memory_task_add',
466
+ `Create a task that you need to come back to. Tasks are memories with status and priority tracking.
467
+
468
+ Use this when:
469
+ - You identify work that needs doing but can't do it right now
470
+ - The user mentions something to do later
471
+ - You want to park a sub-task while focusing on something more urgent
472
+
473
+ Tasks automatically get high salience so they won't be discarded.`,
474
+ {
475
+ concept: z.string().describe('Short task title (3-10 words)'),
476
+ content: z.string().describe('Full task description — what needs doing, context, acceptance criteria'),
477
+ tags: z.array(z.string()).optional().describe('Tags for categorization'),
478
+ priority: z.enum(['urgent', 'high', 'medium', 'low']).default('medium')
479
+ .describe('Task priority: urgent (do now), high (do soon), medium (normal), low (backlog)'),
480
+ blocked_by: z.string().optional().describe('ID of a task that must finish first'),
481
+ },
482
+ async (params) => {
483
+ const engram = store.createEngram({
484
+ agentId: AGENT_ID,
485
+ concept: params.concept,
486
+ content: params.content,
487
+ tags: [...(params.tags ?? []), 'task'],
488
+ salience: 0.9, // Tasks always high salience
489
+ confidence: 0.8,
490
+ salienceFeatures: {
491
+ surprise: 0.5,
492
+ decisionMade: true,
493
+ causalDepth: 0.5,
494
+ resolutionEffort: 0.5,
495
+ eventType: 'decision',
496
+ },
497
+ reasonCodes: ['task-created'],
498
+ taskStatus: params.blocked_by ? 'blocked' : 'open',
499
+ taskPriority: params.priority as TaskPriority,
500
+ blockedBy: params.blocked_by,
501
+ });
502
+
503
+ connectionEngine.enqueue(engram.id);
504
+
505
+ // Generate embedding asynchronously
506
+ embed(`${params.concept} ${params.content}`).then(vec => {
507
+ store.updateEmbedding(engram.id, vec);
508
+ }).catch(() => {});
509
+
510
+ return {
511
+ content: [{
512
+ type: 'text' as const,
513
+ text: `Task created: ${engram.id}\nTitle: ${params.concept}\nPriority: ${params.priority}\nStatus: ${engram.taskStatus}`,
514
+ }],
515
+ };
516
+ }
517
+ );
518
+
519
+ server.tool(
520
+ 'memory_task_update',
521
+ `Update a task's status or priority. Use this to:
522
+ - Start working on a task (open → in_progress)
523
+ - Mark a task done (→ done)
524
+ - Block a task on another (→ blocked)
525
+ - Reprioritize (change priority)
526
+ - Unblock a task (clear blocked_by)`,
527
+ {
528
+ task_id: z.string().describe('ID of the task to update'),
529
+ status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
530
+ .describe('New status'),
531
+ priority: z.enum(['urgent', 'high', 'medium', 'low']).optional()
532
+ .describe('New priority'),
533
+ blocked_by: z.string().optional().describe('ID of blocking task (set to empty string to unblock)'),
534
+ },
535
+ async (params) => {
536
+ const engram = store.getEngram(params.task_id);
537
+ if (!engram || !engram.taskStatus) {
538
+ return { content: [{ type: 'text' as const, text: `Task not found: ${params.task_id}` }] };
539
+ }
540
+
541
+ if (params.blocked_by !== undefined) {
542
+ store.updateBlockedBy(params.task_id, params.blocked_by || null);
543
+ }
544
+ if (params.status) {
545
+ store.updateTaskStatus(params.task_id, params.status as TaskStatus);
546
+ }
547
+ if (params.priority) {
548
+ store.updateTaskPriority(params.task_id, params.priority as TaskPriority);
549
+ }
550
+
551
+ const updated = store.getEngram(params.task_id)!;
552
+ return {
553
+ content: [{
554
+ type: 'text' as const,
555
+ text: `Task updated: ${updated.concept}\nStatus: ${updated.taskStatus}\nPriority: ${updated.taskPriority}${updated.blockedBy ? `\nBlocked by: ${updated.blockedBy}` : ''}`,
556
+ }],
557
+ };
558
+ }
559
+ );
560
+
561
+ server.tool(
562
+ 'memory_task_list',
563
+ `List tasks with optional status filter. Shows tasks ordered by priority (urgent first).
564
+
565
+ Use at the start of a session to see what's pending, or to check blocked/done tasks.`,
566
+ {
567
+ status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
568
+ .describe('Filter by status (omit to see all active tasks)'),
569
+ include_done: z.boolean().optional().default(false)
570
+ .describe('Include completed tasks?'),
571
+ },
572
+ async (params) => {
573
+ let tasks = store.getTasks(AGENT_ID, params.status as TaskStatus | undefined);
574
+ if (!params.include_done && !params.status) {
575
+ tasks = tasks.filter(t => t.taskStatus !== 'done');
576
+ }
577
+
578
+ if (tasks.length === 0) {
579
+ return { content: [{ type: 'text' as const, text: 'No tasks found.' }] };
580
+ }
581
+
582
+ const lines = tasks.map((t, i) => {
583
+ const blocked = t.blockedBy ? ` [blocked by ${t.blockedBy}]` : '';
584
+ const tags = t.tags?.filter(tag => tag !== 'task').join(', ');
585
+ return `${i + 1}. [${t.taskStatus}] **${t.concept}** (${t.taskPriority})${blocked}\n ${t.content.slice(0, 120)}${t.content.length > 120 ? '...' : ''}\n ${tags ? `Tags: ${tags} | ` : ''}ID: ${t.id}`;
586
+ });
587
+
588
+ return {
589
+ content: [{
590
+ type: 'text' as const,
591
+ text: `Tasks (${tasks.length}):\n\n${lines.join('\n\n')}`,
592
+ }],
593
+ };
594
+ }
595
+ );
596
+
597
+ server.tool(
598
+ 'memory_task_next',
599
+ `Get the single most important task to work on next.
600
+
601
+ Prioritizes: in_progress tasks first (finish what you started), then by priority level, then oldest first. Skips blocked and done tasks.
602
+
603
+ Use this when you finish a task or need to decide what to do next.`,
604
+ {},
605
+ async () => {
606
+ const next = store.getNextTask(AGENT_ID);
607
+ if (!next) {
608
+ return { content: [{ type: 'text' as const, text: 'No actionable tasks. All clear!' }] };
609
+ }
610
+
611
+ const blocked = next.blockedBy ? `\nBlocked by: ${next.blockedBy}` : '';
612
+ const tags = next.tags?.filter(tag => tag !== 'task').join(', ');
613
+
614
+ return {
615
+ content: [{
616
+ type: 'text' as const,
617
+ text: `Next task:\n**${next.concept}** (${next.taskPriority})\nStatus: ${next.taskStatus}\n${next.content}${blocked}\n${tags ? `Tags: ${tags}\n` : ''}ID: ${next.id}`,
618
+ }],
619
+ };
620
+ }
621
+ );
622
+
623
+ // --- Start ---
624
+
625
+ async function main() {
626
+ const transport = new StdioServerTransport();
627
+ await server.connect(transport);
628
+ // Log to stderr (stdout is reserved for MCP protocol)
629
+ console.error(`AgentWorkingMemory MCP server started (agent: ${AGENT_ID}, db: ${DB_PATH})`);
630
+ }
631
+
632
+ main().catch(err => {
633
+ console.error('MCP server failed:', err);
634
+ process.exit(1);
635
+ });
@@ -0,0 +1 @@
1
+ export * from './sqlite.js';