agent-working-memory 0.5.4 → 0.5.6

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 (71) hide show
  1. package/README.md +87 -46
  2. package/dist/api/routes.d.ts.map +1 -1
  3. package/dist/api/routes.js +21 -5
  4. package/dist/api/routes.js.map +1 -1
  5. package/dist/cli.js +67 -67
  6. package/dist/coordination/index.d.ts +11 -0
  7. package/dist/coordination/index.d.ts.map +1 -0
  8. package/dist/coordination/index.js +39 -0
  9. package/dist/coordination/index.js.map +1 -0
  10. package/dist/coordination/mcp-tools.d.ts +8 -0
  11. package/dist/coordination/mcp-tools.d.ts.map +1 -0
  12. package/dist/coordination/mcp-tools.js +216 -0
  13. package/dist/coordination/mcp-tools.js.map +1 -0
  14. package/dist/coordination/routes.d.ts +9 -0
  15. package/dist/coordination/routes.d.ts.map +1 -0
  16. package/dist/coordination/routes.js +434 -0
  17. package/dist/coordination/routes.js.map +1 -0
  18. package/dist/coordination/schema.d.ts +12 -0
  19. package/dist/coordination/schema.d.ts.map +1 -0
  20. package/dist/coordination/schema.js +91 -0
  21. package/dist/coordination/schema.js.map +1 -0
  22. package/dist/coordination/schemas.d.ts +208 -0
  23. package/dist/coordination/schemas.d.ts.map +1 -0
  24. package/dist/coordination/schemas.js +109 -0
  25. package/dist/coordination/schemas.js.map +1 -0
  26. package/dist/coordination/stale.d.ts +25 -0
  27. package/dist/coordination/stale.d.ts.map +1 -0
  28. package/dist/coordination/stale.js +53 -0
  29. package/dist/coordination/stale.js.map +1 -0
  30. package/dist/index.js +21 -3
  31. package/dist/index.js.map +1 -1
  32. package/dist/mcp.js +90 -79
  33. package/dist/mcp.js.map +1 -1
  34. package/dist/storage/sqlite.d.ts +3 -0
  35. package/dist/storage/sqlite.d.ts.map +1 -1
  36. package/dist/storage/sqlite.js +285 -281
  37. package/dist/storage/sqlite.js.map +1 -1
  38. package/package.json +55 -55
  39. package/src/api/index.ts +3 -3
  40. package/src/api/routes.ts +551 -536
  41. package/src/cli.ts +397 -397
  42. package/src/coordination/index.ts +47 -0
  43. package/src/coordination/mcp-tools.ts +313 -0
  44. package/src/coordination/routes.ts +656 -0
  45. package/src/coordination/schema.ts +94 -0
  46. package/src/coordination/schemas.ts +136 -0
  47. package/src/coordination/stale.ts +89 -0
  48. package/src/core/decay.ts +63 -63
  49. package/src/core/embeddings.ts +88 -88
  50. package/src/core/hebbian.ts +93 -93
  51. package/src/core/index.ts +5 -5
  52. package/src/core/logger.ts +36 -36
  53. package/src/core/query-expander.ts +66 -66
  54. package/src/core/reranker.ts +101 -101
  55. package/src/engine/activation.ts +656 -656
  56. package/src/engine/connections.ts +103 -103
  57. package/src/engine/consolidation-scheduler.ts +125 -125
  58. package/src/engine/eval.ts +102 -102
  59. package/src/engine/eviction.ts +101 -101
  60. package/src/engine/index.ts +8 -8
  61. package/src/engine/retraction.ts +100 -100
  62. package/src/engine/staging.ts +74 -74
  63. package/src/index.ts +137 -121
  64. package/src/mcp.ts +1024 -1013
  65. package/src/storage/index.ts +3 -3
  66. package/src/storage/sqlite.ts +968 -963
  67. package/src/types/agent.ts +67 -67
  68. package/src/types/checkpoint.ts +46 -46
  69. package/src/types/engram.ts +217 -217
  70. package/src/types/eval.ts +100 -100
  71. package/src/types/index.ts +6 -6
package/src/mcp.ts CHANGED
@@ -1,1013 +1,1024 @@
1
- // Copyright 2026 Robert Winter / Complete Ideas
2
- // SPDX-License-Identifier: Apache-2.0
3
- /**
4
- * MCP Server — Model Context Protocol interface for AgentWorkingMemory.
5
- *
6
- * Runs as a stdio-based MCP server that Claude Code connects to directly.
7
- * Uses the storage and engine layers in-process (no HTTP overhead).
8
- *
9
- * Tools exposed (12):
10
- * memory_write — store a memory (salience filter decides disposition)
11
- * memory_recall — activate memories by context (cognitive retrieval)
12
- * memory_feedback — report whether a recalled memory was useful
13
- * memory_retract — invalidate a wrong memory with optional correction
14
- * memory_supersede — replace an outdated memory with a current one
15
- * memory_stats — get memory health metrics
16
- * memory_checkpoint — save structured execution state (survives compaction)
17
- * memory_restore — restore state + targeted recall after compaction
18
- * memory_task_add — create a prioritized task
19
- * memory_task_update — change task status, priority, or blocking
20
- * memory_task_list — list tasks filtered by status
21
- * memory_task_next — get the highest-priority actionable task
22
- *
23
- * Run: npx tsx src/mcp.ts
24
- * Config: add to ~/.claude.json or .mcp.json
25
- */
26
-
27
- import { readFileSync } from 'node:fs';
28
- import { resolve } from 'node:path';
29
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
30
-
31
- // Load .env file if present (no external dependency)
32
- try {
33
- const envPath = resolve(process.cwd(), '.env');
34
- const envContent = readFileSync(envPath, 'utf-8');
35
- for (const line of envContent.split('\n')) {
36
- const trimmed = line.trim();
37
- if (!trimmed || trimmed.startsWith('#')) continue;
38
- const eqIdx = trimmed.indexOf('=');
39
- if (eqIdx === -1) continue;
40
- const key = trimmed.slice(0, eqIdx).trim();
41
- const val = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, '');
42
- if (!process.env[key]) process.env[key] = val;
43
- }
44
- } catch { /* No .env file */ }
45
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
46
- import { z } from 'zod';
47
-
48
- import { EngramStore } from './storage/sqlite.js';
49
- import { ActivationEngine } from './engine/activation.js';
50
- import { ConnectionEngine } from './engine/connections.js';
51
- import { StagingBuffer } from './engine/staging.js';
52
- import { EvictionEngine } from './engine/eviction.js';
53
- import { RetractionEngine } from './engine/retraction.js';
54
- import { EvalEngine } from './engine/eval.js';
55
- import { ConsolidationEngine } from './engine/consolidation.js';
56
- import { ConsolidationScheduler } from './engine/consolidation-scheduler.js';
57
- import { evaluateSalience, computeNovelty, computeNoveltyWithMatch } from './core/salience.js';
58
- import type { ConsciousState } from './types/checkpoint.js';
59
- import type { SalienceEventType } from './core/salience.js';
60
- import type { TaskStatus, TaskPriority } from './types/engram.js';
61
- import { DEFAULT_AGENT_CONFIG } from './types/agent.js';
62
- import { embed } from './core/embeddings.js';
63
- import { startSidecar } from './hooks/sidecar.js';
64
- import { initLogger, log, getLogPath } from './core/logger.js';
65
-
66
- // --- Incognito Mode ---
67
- // When AWM_INCOGNITO=1, register zero tools. Claude won't see memory tools at all.
68
- // No DB, no engines, no sidecar — just a bare MCP server that exposes nothing.
69
-
70
- const INCOGNITO = process.env.AWM_INCOGNITO === '1' || process.env.AWM_INCOGNITO === 'true';
71
-
72
- if (INCOGNITO) {
73
- console.error('AWM: incognito mode — all memory tools disabled, nothing will be recorded');
74
- const server = new McpServer({ name: 'agent-working-memory', version: '0.5.4' });
75
- const transport = new StdioServerTransport();
76
- server.connect(transport).catch(err => {
77
- console.error('MCP server failed:', err);
78
- process.exit(1);
79
- });
80
- // No tools registered — Claude won't see any memory_* tools
81
- } else {
82
-
83
- // --- Setup ---
84
-
85
- const DB_PATH = process.env.AWM_DB_PATH ?? 'memory.db';
86
- const AGENT_ID = process.env.AWM_AGENT_ID ?? process.env.WORKER_NAME ?? 'claude-code';
87
- const HOOK_PORT = parseInt(process.env.AWM_HOOK_PORT ?? '8401', 10);
88
- const HOOK_SECRET = process.env.AWM_HOOK_SECRET ?? null;
89
-
90
- initLogger(DB_PATH);
91
- log(AGENT_ID, 'startup', `MCP server starting (db: ${DB_PATH}, hooks: ${HOOK_PORT})`);
92
-
93
- const store = new EngramStore(DB_PATH);
94
- const activationEngine = new ActivationEngine(store);
95
- const connectionEngine = new ConnectionEngine(store, activationEngine);
96
- const stagingBuffer = new StagingBuffer(store, activationEngine);
97
- const evictionEngine = new EvictionEngine(store);
98
- const retractionEngine = new RetractionEngine(store);
99
- const evalEngine = new EvalEngine(store);
100
- const consolidationEngine = new ConsolidationEngine(store);
101
- const consolidationScheduler = new ConsolidationScheduler(store, consolidationEngine);
102
-
103
- stagingBuffer.start(DEFAULT_AGENT_CONFIG.stagingTtlMs);
104
- consolidationScheduler.start();
105
-
106
- const server = new McpServer({
107
- name: 'agent-working-memory',
108
- version: '0.5.4',
109
- });
110
-
111
- // --- Tools ---
112
-
113
- server.tool(
114
- 'memory_write',
115
- `Store a memory. The salience filter decides whether it's worth keeping (active), needs more evidence (staging), or should be discarded.
116
-
117
- CALL THIS PROACTIVELY — do not wait to be asked. Write memories when you:
118
- - Discover something about the codebase, bugs, or architecture
119
- - Make a decision and want to remember why
120
- - Encounter and resolve an error
121
- - Learn a user preference or project pattern
122
- - Complete a significant piece of work
123
-
124
- The concept should be a short label (3-8 words). The content should be the full detail.`,
125
- {
126
- concept: z.string().describe('Short label for this memory (3-8 words)'),
127
- content: z.string().describe('Full detail of what was learned'),
128
- tags: z.array(z.string()).optional().describe('Optional tags for categorization'),
129
- event_type: z.enum(['observation', 'decision', 'friction', 'surprise', 'causal'])
130
- .optional().default('observation')
131
- .describe('Type of event: observation (default), decision, friction (error/blocker), surprise, causal (root cause)'),
132
- surprise: z.number().min(0).max(1).optional().default(0.3)
133
- .describe('How surprising was this? 0=expected, 1=very unexpected'),
134
- decision_made: z.boolean().optional().default(false)
135
- .describe('Was a decision made? True boosts importance'),
136
- causal_depth: z.number().min(0).max(1).optional().default(0.3)
137
- .describe('How deep is the causal understanding? 0=surface, 1=root cause'),
138
- resolution_effort: z.number().min(0).max(1).optional().default(0.3)
139
- .describe('How much effort to resolve? 0=trivial, 1=significant debugging'),
140
- memory_class: z.enum(['canonical', 'working', 'ephemeral']).optional().default('working')
141
- .describe('Memory class: canonical (source-of-truth, never stages), working (default), ephemeral (temporary, decays faster)'),
142
- supersedes: z.string().optional()
143
- .describe('ID of an older memory this one replaces. The old memory is down-ranked, not deleted.'),
144
- },
145
- async (params) => {
146
- // Check novelty with match info for reinforcement
147
- const noveltyResult = computeNoveltyWithMatch(store, AGENT_ID, params.concept, params.content);
148
- const novelty = noveltyResult.novelty;
149
-
150
- // --- Reinforce-on-Duplicate check ---
151
- // Tightened thresholds: require near-exact match (novelty < 0.3, BM25 > 0.85, 60% content overlap)
152
- if (novelty < 0.3
153
- && noveltyResult.matchScore > 0.85
154
- && noveltyResult.matchedEngramId) {
155
- const matchedEngram = store.getEngram(noveltyResult.matchedEngramId);
156
- if (matchedEngram) {
157
- const existingTokens = new Set(matchedEngram.content.toLowerCase().split(/\s+/).filter(w => w.length > 3));
158
- const newTokens = new Set(params.content.toLowerCase().split(/\s+/).filter(w => w.length > 3));
159
- let overlap = 0;
160
- for (const t of newTokens) { if (existingTokens.has(t)) overlap++; }
161
- const contentOverlap = newTokens.size > 0 ? overlap / newTokens.size : 0;
162
-
163
- if (contentOverlap > 0.6) {
164
- // True duplicate — reinforce existing and skip creation
165
- store.touchEngram(noveltyResult.matchedEngramId);
166
- try { store.updateAutoCheckpointWrite(AGENT_ID, noveltyResult.matchedEngramId); } catch { /* non-fatal */ }
167
- log(AGENT_ID, 'write:reinforce', `"${params.concept}" → reinforced "${matchedEngram.concept}" (overlap=${contentOverlap.toFixed(2)})`);
168
- return {
169
- content: [{
170
- type: 'text' as const,
171
- text: `Reinforced existing memory "${matchedEngram.concept}" (overlap ${(contentOverlap * 100).toFixed(0)}%)`,
172
- }],
173
- };
174
- }
175
- // Partial match — continue to create new memory
176
- log(AGENT_ID, 'write:partial-match', `"${params.concept}" partially matched "${matchedEngram.concept}" (overlap=${contentOverlap.toFixed(2)}), creating new memory`);
177
- }
178
- }
179
-
180
- const salience = evaluateSalience({
181
- content: params.content,
182
- eventType: params.event_type as SalienceEventType,
183
- surprise: params.surprise,
184
- decisionMade: params.decision_made,
185
- causalDepth: params.causal_depth,
186
- resolutionEffort: params.resolution_effort,
187
- novelty,
188
- memoryClass: params.memory_class,
189
- });
190
-
191
- // v0.5.4: No longer discard — store everything, use salience for ranking.
192
- // Low-salience memories get low confidence so they rank below high-salience
193
- // in retrieval, but remain available for recall when needed.
194
- const isLowSalience = salience.disposition === 'discard';
195
-
196
- const CONFIDENCE_PRIORS: Record<string, number> = {
197
- decision: 0.65,
198
- friction: 0.60,
199
- causal: 0.60,
200
- surprise: 0.55,
201
- observation: 0.45,
202
- };
203
- const confidencePrior = isLowSalience
204
- ? 0.25
205
- : salience.disposition === 'staging'
206
- ? 0.40
207
- : CONFIDENCE_PRIORS[params.event_type ?? 'observation'] ?? 0.45;
208
-
209
- const engram = store.createEngram({
210
- agentId: AGENT_ID,
211
- concept: params.concept,
212
- content: params.content,
213
- tags: isLowSalience ? [...(params.tags ?? []), 'low-salience'] : params.tags,
214
- salience: salience.score,
215
- confidence: confidencePrior,
216
- salienceFeatures: salience.features,
217
- reasonCodes: salience.reasonCodes,
218
- ttl: salience.disposition === 'staging' ? DEFAULT_AGENT_CONFIG.stagingTtlMs : undefined,
219
- memoryClass: params.memory_class,
220
- supersedes: params.supersedes,
221
- });
222
-
223
- if (salience.disposition === 'staging') {
224
- store.updateStage(engram.id, 'staging');
225
- } else {
226
- connectionEngine.enqueue(engram.id);
227
- }
228
-
229
- // Handle supersession: mark old memory as superseded
230
- if (params.supersedes) {
231
- const oldEngram = store.getEngram(params.supersedes);
232
- if (oldEngram) {
233
- store.supersedeEngram(params.supersedes, engram.id);
234
- // Create supersession association
235
- store.upsertAssociation(engram.id, oldEngram.id, 0.8, 'causal', 0.9);
236
- }
237
- }
238
-
239
- // Generate embedding asynchronously (don't block response)
240
- embed(`${params.concept} ${params.content}`).then(vec => {
241
- store.updateEmbedding(engram.id, vec);
242
- }).catch(() => {}); // Embedding failure is non-fatal
243
-
244
- // Auto-checkpoint: track write
245
- try { store.updateAutoCheckpointWrite(AGENT_ID, engram.id); } catch { /* non-fatal */ }
246
-
247
- const logDisposition = isLowSalience ? 'low-salience' : salience.disposition;
248
- log(AGENT_ID, `write:${logDisposition}`, `"${params.concept}" salience=${salience.score.toFixed(2)} novelty=${novelty.toFixed(1)} id=${engram.id}`);
249
-
250
- return {
251
- content: [{
252
- type: 'text' as const,
253
- text: `Stored (${salience.disposition}) "${params.concept}" [${salience.score.toFixed(2)}]`,
254
- }],
255
- };
256
- }
257
- );
258
-
259
- server.tool(
260
- 'memory_recall',
261
- `Recall memories relevant to a query. Uses cognitive activation — not keyword search.
262
-
263
- ALWAYS call this when:
264
- - Starting work on a project or topic (recall what you know)
265
- - Debugging (recall similar errors and solutions)
266
- - Making decisions (recall past decisions and outcomes)
267
- - The user mentions a topic you might have stored memories about
268
-
269
- Accepts either "query" or "context" parameter — both work identically.
270
- Returns the most relevant memories ranked by text relevance, temporal recency, and associative strength.`,
271
- {
272
- query: z.string().optional().describe('What to search for — describe the situation, question, or topic'),
273
- context: z.string().optional().describe('Alias for query (either works)'),
274
- limit: z.number().optional().default(5).describe('Max memories to return (default 5)'),
275
- min_score: z.number().optional().default(0.05).describe('Minimum relevance score (default 0.05)'),
276
- include_staging: z.boolean().optional().default(false).describe('Include weak/unconfirmed memories?'),
277
- use_reranker: z.boolean().optional().default(true).describe('Use cross-encoder re-ranking for better relevance (default true)'),
278
- use_expansion: z.boolean().optional().default(true).describe('Expand query with synonyms for better recall (default true)'),
279
- },
280
- async (params) => {
281
- const queryText = params.query ?? params.context;
282
- if (!queryText) {
283
- return {
284
- content: [{
285
- type: 'text' as const,
286
- text: 'Error: provide either "query" or "context" parameter with your search text.',
287
- }],
288
- };
289
- }
290
- const results = await activationEngine.activate({
291
- agentId: AGENT_ID,
292
- context: queryText,
293
- limit: params.limit,
294
- minScore: params.min_score,
295
- includeStaging: params.include_staging,
296
- useReranker: params.use_reranker,
297
- useExpansion: params.use_expansion,
298
- });
299
-
300
- // Auto-checkpoint: track recall
301
- try {
302
- const ids = results.map(r => r.engram.id);
303
- store.updateAutoCheckpointRecall(AGENT_ID, queryText, ids);
304
- } catch { /* non-fatal */ }
305
-
306
- log(AGENT_ID, 'recall', `"${queryText.slice(0, 80)}" → ${results.length} results`);
307
-
308
- if (results.length === 0) {
309
- return {
310
- content: [{
311
- type: 'text' as const,
312
- text: 'No relevant memories found.',
313
- }],
314
- };
315
- }
316
-
317
- const lines = results.map((r, i) => {
318
- return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)}): ${r.engram.content}`;
319
- });
320
-
321
- return {
322
- content: [{
323
- type: 'text' as const,
324
- text: lines.join('\n'),
325
- }],
326
- };
327
- }
328
- );
329
-
330
- server.tool(
331
- 'memory_feedback',
332
- `Report whether a recalled memory was actually useful. This updates the memory's confidence score — useful memories become stronger, useless ones weaken.
333
-
334
- Always call this after using a recalled memory so the system learns what's valuable.`,
335
- {
336
- engram_id: z.string().describe('ID of the memory (from memory_recall results)'),
337
- useful: z.boolean().describe('Was this memory actually helpful?'),
338
- context: z.string().optional().describe('Brief note on why it was/wasn\'t useful'),
339
- },
340
- async (params) => {
341
- store.logRetrievalFeedback(null, params.engram_id, params.useful, params.context ?? '');
342
-
343
- const engram = store.getEngram(params.engram_id);
344
- if (engram) {
345
- const delta = params.useful
346
- ? DEFAULT_AGENT_CONFIG.feedbackPositiveBoost
347
- : -DEFAULT_AGENT_CONFIG.feedbackNegativePenalty;
348
- store.updateConfidence(engram.id, engram.confidence + delta);
349
- }
350
-
351
- return {
352
- content: [{
353
- type: 'text' as const,
354
- text: `Feedback: ${params.useful ? '+useful' : '-not useful'}`,
355
- }],
356
- };
357
- }
358
- );
359
-
360
- server.tool(
361
- 'memory_retract',
362
- `Retract a memory that turned out to be wrong. Creates a correction and reduces confidence of related memories.
363
-
364
- Use this when you discover a memory contains incorrect information.`,
365
- {
366
- engram_id: z.string().describe('ID of the wrong memory'),
367
- reason: z.string().describe('Why is this memory wrong?'),
368
- correction: z.string().optional().describe('What is the correct information? (creates a new memory)'),
369
- },
370
- async (params) => {
371
- const result = retractionEngine.retract({
372
- agentId: AGENT_ID,
373
- targetEngramId: params.engram_id,
374
- reason: params.reason,
375
- counterContent: params.correction,
376
- });
377
-
378
- const parts = [`Memory ${params.engram_id} retracted.`];
379
- if (result.correctionId) {
380
- parts.push(`Correction stored as ${result.correctionId}.`);
381
- }
382
- parts.push(`${result.associatesAffected} related memories had confidence reduced.`);
383
-
384
- return {
385
- content: [{
386
- type: 'text' as const,
387
- text: parts.join(' '),
388
- }],
389
- };
390
- }
391
- );
392
-
393
- server.tool(
394
- 'memory_supersede',
395
- `Replace an outdated memory with a newer one. Unlike retraction (which marks memories as wrong), supersession marks the old memory as outdated but historically correct.
396
-
397
- Use this when:
398
- - A status or count has changed (e.g., "5 reviews done" → "7 reviews done")
399
- - Architecture or infrastructure evolved (e.g., "two-repo model" → "three-repo model")
400
- - A schedule or plan was updated
401
-
402
- The old memory stays in the database (searchable for history) but is heavily down-ranked in recall so the current version dominates.`,
403
- {
404
- old_engram_id: z.string().describe('ID of the outdated memory'),
405
- new_engram_id: z.string().describe('ID of the replacement memory'),
406
- reason: z.string().optional().describe('Why the old memory is outdated'),
407
- },
408
- async (params) => {
409
- const oldEngram = store.getEngram(params.old_engram_id);
410
- if (!oldEngram) {
411
- return { content: [{ type: 'text' as const, text: `Old memory not found: ${params.old_engram_id}` }] };
412
- }
413
- const newEngram = store.getEngram(params.new_engram_id);
414
- if (!newEngram) {
415
- return { content: [{ type: 'text' as const, text: `New memory not found: ${params.new_engram_id}` }] };
416
- }
417
-
418
- store.supersedeEngram(params.old_engram_id, params.new_engram_id);
419
-
420
- // Create supersession association (new → old)
421
- store.upsertAssociation(params.new_engram_id, params.old_engram_id, 0.8, 'causal', 0.9);
422
-
423
- // Reduce old memory's confidence (not to zero — it's historical, not wrong)
424
- store.updateConfidence(params.old_engram_id, Math.max(0.2, oldEngram.confidence * 0.4));
425
-
426
- log(AGENT_ID, 'supersede', `"${oldEngram.concept}" → "${newEngram.concept}"${params.reason ? ` (${params.reason})` : ''}`);
427
-
428
- return {
429
- content: [{
430
- type: 'text' as const,
431
- text: `Superseded: "${oldEngram.concept}" → "${newEngram.concept}"`,
432
- }],
433
- };
434
- }
435
- );
436
-
437
- server.tool(
438
- 'memory_stats',
439
- `Get memory health stats — how many memories, confidence levels, association count, and system performance.
440
- Also shows the activity log path so the user can tail it to see what's happening.`,
441
- {},
442
- async () => {
443
- const metrics = evalEngine.computeMetrics(AGENT_ID);
444
- const checkpoint = store.getCheckpoint(AGENT_ID);
445
- const lines = [
446
- `Agent: ${AGENT_ID}`,
447
- `Active memories: ${metrics.activeEngramCount}`,
448
- `Staging: ${metrics.stagingEngramCount}`,
449
- `Retracted: ${metrics.retractedCount}`,
450
- `Avg confidence: ${metrics.avgConfidence.toFixed(3)}`,
451
- `Total edges: ${metrics.totalEdges}`,
452
- `Edge utility: ${(metrics.edgeUtilityRate * 100).toFixed(1)}%`,
453
- `Activations (24h): ${metrics.activationCount}`,
454
- `Avg latency: ${metrics.avgLatencyMs.toFixed(1)}ms`,
455
- ``,
456
- `Session writes: ${checkpoint?.auto.writeCountSinceConsolidation ?? 0}`,
457
- `Session recalls: ${checkpoint?.auto.recallCountSinceConsolidation ?? 0}`,
458
- `Last activity: ${checkpoint?.auto.lastActivityAt?.toISOString() ?? 'never'}`,
459
- `Checkpoint: ${checkpoint?.executionState ? checkpoint.executionState.currentTask : 'none'}`,
460
- ``,
461
- `Activity log: ${getLogPath() ?? 'not configured'}`,
462
- `Hook sidecar: 127.0.0.1:${HOOK_PORT}`,
463
- ];
464
-
465
- return {
466
- content: [{
467
- type: 'text' as const,
468
- text: lines.join('\n'),
469
- }],
470
- };
471
- }
472
- );
473
-
474
- // --- Checkpointing Tools ---
475
-
476
- server.tool(
477
- 'memory_checkpoint',
478
- `Save your current execution state so you can recover after context compaction.
479
-
480
- ALWAYS call this before:
481
- - Long operations (multi-file generation, large refactors, overnight work)
482
- - Anything that might fill the context window
483
- - Switching to a different task
484
-
485
- Also call periodically during long sessions to avoid losing state. The state is saved per-agent and overwrites any previous checkpoint.`,
486
- {
487
- current_task: z.string().describe('What you are currently working on'),
488
- decisions: z.array(z.string()).optional().default([])
489
- .describe('Key decisions made so far'),
490
- active_files: z.array(z.string()).optional().default([])
491
- .describe('Files you are currently working with'),
492
- next_steps: z.array(z.string()).optional().default([])
493
- .describe('What needs to happen next'),
494
- related_memory_ids: z.array(z.string()).optional().default([])
495
- .describe('IDs of memories relevant to current work'),
496
- notes: z.string().optional().default('')
497
- .describe('Any other context worth preserving'),
498
- episode_id: z.string().optional()
499
- .describe('Current episode ID if known'),
500
- },
501
- async (params) => {
502
- const state: ConsciousState = {
503
- currentTask: params.current_task,
504
- decisions: params.decisions,
505
- activeFiles: params.active_files,
506
- nextSteps: params.next_steps,
507
- relatedMemoryIds: params.related_memory_ids,
508
- notes: params.notes,
509
- episodeId: params.episode_id ?? null,
510
- };
511
-
512
- store.saveCheckpoint(AGENT_ID, state);
513
- log(AGENT_ID, 'checkpoint', `"${params.current_task}" decisions=${params.decisions.length} files=${params.active_files.length}`);
514
-
515
- return {
516
- content: [{
517
- type: 'text' as const,
518
- text: `Checkpoint saved: "${params.current_task}" (${params.decisions.length} decisions, ${params.active_files.length} files)`,
519
- }],
520
- };
521
- }
522
- );
523
-
524
- server.tool(
525
- 'memory_restore',
526
- `Restore your previous execution state after context compaction or at session start.
527
-
528
- Returns:
529
- - Your saved execution state (task, decisions, next steps, files)
530
- - Recently recalled memories for context
531
- - Your last write for continuity
532
- - How long you were idle
533
-
534
- Use this at the start of every session or after compaction to pick up where you left off.`,
535
- {},
536
- async () => {
537
- const checkpoint = store.getCheckpoint(AGENT_ID);
538
-
539
- const now = Date.now();
540
- const idleMs = checkpoint
541
- ? now - checkpoint.auto.lastActivityAt.getTime()
542
- : 0;
543
-
544
- // Get last written engram
545
- let lastWrite: { id: string; concept: string; content: string } | null = null;
546
- if (checkpoint?.auto.lastWriteId) {
547
- const engram = store.getEngram(checkpoint.auto.lastWriteId);
548
- if (engram) {
549
- lastWrite = { id: engram.id, concept: engram.concept, content: engram.content };
550
- }
551
- }
552
-
553
- // Recall memories using last context
554
- let recalledMemories: Array<{ id: string; concept: string; content: string; score: number }> = [];
555
- const recallContext = checkpoint?.auto.lastRecallContext
556
- ?? checkpoint?.executionState?.currentTask
557
- ?? null;
558
-
559
- if (recallContext) {
560
- try {
561
- const results = await activationEngine.activate({
562
- agentId: AGENT_ID,
563
- context: recallContext,
564
- limit: 5,
565
- minScore: 0.05,
566
- useReranker: true,
567
- useExpansion: true,
568
- });
569
- recalledMemories = results.map(r => ({
570
- id: r.engram.id,
571
- concept: r.engram.concept,
572
- content: r.engram.content,
573
- score: r.score,
574
- }));
575
- } catch { /* recall failure is non-fatal */ }
576
- }
577
-
578
- // Consolidation on restore:
579
- // - If idle >5min but last consolidation was recent (graceful exit ran it), skip
580
- // - If idle >5min and no recent consolidation, run full cycle (non-graceful exit fallback)
581
- const MINI_IDLE_MS = 5 * 60_000;
582
- const FULL_CONSOLIDATION_GAP_MS = 10 * 60_000; // 10 min — if last consolidation was longer ago, run full
583
- let miniConsolidationTriggered = false;
584
- let fullConsolidationTriggered = false;
585
-
586
- if (idleMs > MINI_IDLE_MS) {
587
- const sinceLastConsolidation = checkpoint?.lastConsolidationAt
588
- ? now - checkpoint.lastConsolidationAt.getTime()
589
- : Infinity;
590
-
591
- if (sinceLastConsolidation > FULL_CONSOLIDATION_GAP_MS) {
592
- // No recent consolidation — graceful exit didn't happen, run full cycle
593
- fullConsolidationTriggered = true;
594
- try {
595
- const result = await consolidationEngine.consolidate(AGENT_ID);
596
- store.markConsolidation(AGENT_ID, false);
597
- log(AGENT_ID, 'consolidation', `full sleep cycle on restore (no graceful exit, idle ${Math.round(idleMs / 60_000)}min, last consolidation ${Math.round(sinceLastConsolidation / 60_000)}min ago) — ${result.edgesStrengthened} strengthened, ${result.memoriesForgotten} forgotten`);
598
- } catch { /* consolidation failure is non-fatal */ }
599
- } else {
600
- // Recent consolidation exists — graceful exit already handled it, just do mini
601
- miniConsolidationTriggered = true;
602
- consolidationScheduler.runMiniConsolidation(AGENT_ID).catch(() => {});
603
- }
604
- }
605
-
606
- // Format response
607
- const parts: string[] = [];
608
- const idleMin = Math.round(idleMs / 60_000);
609
- const consolidationNote = fullConsolidationTriggered
610
- ? ' (full consolidation — no graceful exit detected)'
611
- : miniConsolidationTriggered
612
- ? ' (mini-consolidation triggered)'
613
- : '';
614
- log(AGENT_ID, 'restore', `idle=${idleMin}min checkpoint=${!!checkpoint?.executionState} recalled=${recalledMemories.length} lastWrite=${lastWrite?.concept ?? 'none'}${fullConsolidationTriggered ? ' FULL_CONSOLIDATION' : ''}`);
615
- parts.push(`Idle: ${idleMin}min${consolidationNote}`);
616
-
617
- if (checkpoint?.executionState) {
618
- const s = checkpoint.executionState;
619
- parts.push(`\n**Current task:** ${s.currentTask}`);
620
- if (s.decisions.length) parts.push(`**Decisions:** ${s.decisions.join('; ')}`);
621
- if (s.nextSteps.length) parts.push(`**Next steps:** ${s.nextSteps.map((st, i) => `${i + 1}. ${st}`).join(', ')}`);
622
- if (s.activeFiles.length) parts.push(`**Active files:** ${s.activeFiles.join(', ')}`);
623
- if (s.notes) parts.push(`**Notes:** ${s.notes}`);
624
- if (checkpoint.checkpointAt) parts.push(`_Saved at: ${checkpoint.checkpointAt.toISOString()}_`);
625
- } else {
626
- parts.push('\nNo explicit checkpoint saved.');
627
- parts.push('\n**Tip:** Use memory_write to save important learnings, and memory_checkpoint before long operations so you can recover state.');
628
- }
629
-
630
- if (lastWrite) {
631
- parts.push(`\n**Last write:** ${lastWrite.concept}\n${lastWrite.content}`);
632
- }
633
-
634
- if (recalledMemories.length > 0) {
635
- parts.push(`\n**Recalled memories (${recalledMemories.length}):**`);
636
- for (const m of recalledMemories) {
637
- parts.push(`- **${m.concept}** (${m.score.toFixed(3)}): ${m.content.slice(0, 150)}${m.content.length > 150 ? '...' : ''}`);
638
- }
639
- }
640
-
641
- return {
642
- content: [{
643
- type: 'text' as const,
644
- text: parts.join('\n'),
645
- }],
646
- };
647
- }
648
- );
649
-
650
- // --- Task Management Tools ---
651
-
652
- server.tool(
653
- 'memory_task_add',
654
- `Create a task that you need to come back to. Tasks are memories with status and priority tracking.
655
-
656
- Use this when:
657
- - You identify work that needs doing but can't do it right now
658
- - The user mentions something to do later
659
- - You want to park a sub-task while focusing on something more urgent
660
-
661
- Tasks automatically get high salience so they won't be discarded.`,
662
- {
663
- concept: z.string().describe('Short task title (3-10 words)'),
664
- content: z.string().describe('Full task description — what needs doing, context, acceptance criteria'),
665
- tags: z.array(z.string()).optional().describe('Tags for categorization'),
666
- priority: z.enum(['urgent', 'high', 'medium', 'low']).default('medium')
667
- .describe('Task priority: urgent (do now), high (do soon), medium (normal), low (backlog)'),
668
- blocked_by: z.string().optional().describe('ID of a task that must finish first'),
669
- },
670
- async (params) => {
671
- const engram = store.createEngram({
672
- agentId: AGENT_ID,
673
- concept: params.concept,
674
- content: params.content,
675
- tags: [...(params.tags ?? []), 'task'],
676
- salience: 0.9, // Tasks always high salience
677
- confidence: 0.8,
678
- salienceFeatures: {
679
- surprise: 0.5,
680
- decisionMade: true,
681
- causalDepth: 0.5,
682
- resolutionEffort: 0.5,
683
- eventType: 'decision',
684
- },
685
- reasonCodes: ['task-created'],
686
- taskStatus: params.blocked_by ? 'blocked' : 'open',
687
- taskPriority: params.priority as TaskPriority,
688
- blockedBy: params.blocked_by,
689
- });
690
-
691
- connectionEngine.enqueue(engram.id);
692
-
693
- // Generate embedding asynchronously
694
- embed(`${params.concept} ${params.content}`).then(vec => {
695
- store.updateEmbedding(engram.id, vec);
696
- }).catch(() => {});
697
-
698
- return {
699
- content: [{
700
- type: 'text' as const,
701
- text: `Task created: "${params.concept}" (${params.priority})`,
702
- }],
703
- };
704
- }
705
- );
706
-
707
- server.tool(
708
- 'memory_task_update',
709
- `Update a task's status or priority. Use this to:
710
- - Start working on a task (open → in_progress)
711
- - Mark a task done (→ done)
712
- - Block a task on another (→ blocked)
713
- - Reprioritize (change priority)
714
- - Unblock a task (clear blocked_by)`,
715
- {
716
- task_id: z.string().describe('ID of the task to update'),
717
- status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
718
- .describe('New status'),
719
- priority: z.enum(['urgent', 'high', 'medium', 'low']).optional()
720
- .describe('New priority'),
721
- blocked_by: z.string().optional().describe('ID of blocking task (set to empty string to unblock)'),
722
- },
723
- async (params) => {
724
- const engram = store.getEngram(params.task_id);
725
- if (!engram || !engram.taskStatus) {
726
- return { content: [{ type: 'text' as const, text: `Task not found: ${params.task_id}` }] };
727
- }
728
-
729
- if (params.blocked_by !== undefined) {
730
- store.updateBlockedBy(params.task_id, params.blocked_by || null);
731
- }
732
- if (params.status) {
733
- store.updateTaskStatus(params.task_id, params.status as TaskStatus);
734
- }
735
- if (params.priority) {
736
- store.updateTaskPriority(params.task_id, params.priority as TaskPriority);
737
- }
738
-
739
- const updated = store.getEngram(params.task_id)!;
740
- return {
741
- content: [{
742
- type: 'text' as const,
743
- text: `Updated: "${updated.concept}" → ${updated.taskStatus} (${updated.taskPriority})`,
744
- }],
745
- };
746
- }
747
- );
748
-
749
- server.tool(
750
- 'memory_task_list',
751
- `List tasks with optional status filter. Shows tasks ordered by priority (urgent first).
752
-
753
- Use at the start of a session to see what's pending, or to check blocked/done tasks.`,
754
- {
755
- status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
756
- .describe('Filter by status (omit to see all active tasks)'),
757
- include_done: z.boolean().optional().default(false)
758
- .describe('Include completed tasks?'),
759
- },
760
- async (params) => {
761
- let tasks = store.getTasks(AGENT_ID, params.status as TaskStatus | undefined);
762
- if (!params.include_done && !params.status) {
763
- tasks = tasks.filter(t => t.taskStatus !== 'done');
764
- }
765
-
766
- if (tasks.length === 0) {
767
- return { content: [{ type: 'text' as const, text: 'No tasks found.' }] };
768
- }
769
-
770
- const lines = tasks.map((t, i) => {
771
- const blocked = t.blockedBy ? ` [blocked by ${t.blockedBy}]` : '';
772
- const tags = t.tags?.filter(tag => tag !== 'task').join(', ');
773
- 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}`;
774
- });
775
-
776
- return {
777
- content: [{
778
- type: 'text' as const,
779
- text: `Tasks (${tasks.length}):\n\n${lines.join('\n\n')}`,
780
- }],
781
- };
782
- }
783
- );
784
-
785
- server.tool(
786
- 'memory_task_next',
787
- `Get the single most important task to work on next.
788
-
789
- Prioritizes: in_progress tasks first (finish what you started), then by priority level, then oldest first. Skips blocked and done tasks.
790
-
791
- Use this when you finish a task or need to decide what to do next.`,
792
- {},
793
- async () => {
794
- const next = store.getNextTask(AGENT_ID);
795
- if (!next) {
796
- return { content: [{ type: 'text' as const, text: 'No actionable tasks. All clear!' }] };
797
- }
798
-
799
- const blocked = next.blockedBy ? `\nBlocked by: ${next.blockedBy}` : '';
800
- const tags = next.tags?.filter(tag => tag !== 'task').join(', ');
801
-
802
- return {
803
- content: [{
804
- type: 'text' as const,
805
- text: `Next task:\n**${next.concept}** (${next.taskPriority})\nStatus: ${next.taskStatus}\n${next.content}${blocked}\n${tags ? `Tags: ${tags}\n` : ''}ID: ${next.id}`,
806
- }],
807
- };
808
- }
809
- );
810
-
811
- // --- Task Bracket Tools ---
812
-
813
- server.tool(
814
- 'memory_task_begin',
815
- `Signal that you're starting a significant task. Auto-checkpoints current state and recalls relevant memories.
816
-
817
- CALL THIS when starting:
818
- - A multi-step operation (doc generation, large refactor, migration)
819
- - Work on a new topic or project area
820
- - Anything that might fill the context window
821
-
822
- This ensures your state is saved before you start, and primes recall with relevant context.`,
823
- {
824
- topic: z.string().describe('What task are you starting? (3-15 words)'),
825
- files: z.array(z.string()).optional().default([])
826
- .describe('Files you expect to work with'),
827
- notes: z.string().optional().default('')
828
- .describe('Any additional context'),
829
- },
830
- async (params) => {
831
- // 1. Checkpoint current state
832
- const checkpoint = store.getCheckpoint(AGENT_ID);
833
- const prevTask = checkpoint?.executionState?.currentTask ?? 'None';
834
-
835
- store.saveCheckpoint(AGENT_ID, {
836
- currentTask: params.topic,
837
- decisions: [],
838
- activeFiles: params.files,
839
- nextSteps: [],
840
- relatedMemoryIds: [],
841
- notes: params.notes || `Started via memory_task_begin. Previous task: ${prevTask}`,
842
- episodeId: null,
843
- });
844
-
845
- // 2. Auto-recall relevant memories
846
- let recalledSummary = '';
847
- try {
848
- const results = await activationEngine.activate({
849
- agentId: AGENT_ID,
850
- context: params.topic,
851
- limit: 5,
852
- minScore: 0.05,
853
- useReranker: true,
854
- useExpansion: true,
855
- });
856
-
857
- if (results.length > 0) {
858
- const lines = results.map((r, i) => {
859
- const tags = r.engram.tags?.length ? ` [${r.engram.tags.join(', ')}]` : '';
860
- return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)})${tags}\n ${r.engram.content.slice(0, 150)}${r.engram.content.length > 150 ? '...' : ''}`;
861
- });
862
- recalledSummary = `\n\n**Recalled memories (${results.length}):**\n${lines.join('\n')}`;
863
-
864
- // Track recall
865
- store.updateAutoCheckpointRecall(AGENT_ID, params.topic, results.map(r => r.engram.id));
866
- }
867
- } catch { /* recall failure is non-fatal */ }
868
-
869
- log(AGENT_ID, 'task:begin', `"${params.topic}" prev="${prevTask}"`);
870
-
871
- return {
872
- content: [{
873
- type: 'text' as const,
874
- text: `Started: "${params.topic}" (prev: ${prevTask})${recalledSummary}`,
875
- }],
876
- };
877
- }
878
- );
879
-
880
- server.tool(
881
- 'memory_task_end',
882
- `Signal that you've finished a significant task. Writes a summary memory and auto-checkpoints.
883
-
884
- CALL THIS when you finish:
885
- - A multi-step operation
886
- - Before switching to a different topic
887
- - At the end of a work session
888
-
889
- This captures what was accomplished so future sessions can recall it.`,
890
- {
891
- summary: z.string().describe('What was accomplished? Include key outcomes, decisions, and any issues.'),
892
- tags: z.array(z.string()).optional().default([])
893
- .describe('Tags for the summary memory'),
894
- supersedes: z.array(z.string()).optional().default([])
895
- .describe('IDs of older memories this task summary replaces (marks them as superseded)'),
896
- },
897
- async (params) => {
898
- // 1. Write summary as a memory
899
- const salience = evaluateSalience({
900
- content: params.summary,
901
- eventType: 'decision',
902
- surprise: 0.3,
903
- decisionMade: true,
904
- causalDepth: 0.5,
905
- resolutionEffort: 0.5,
906
- });
907
-
908
- // Determine the real task name for the summary engram
909
- const checkpoint = store.getCheckpoint(AGENT_ID);
910
- const rawTask = checkpoint?.executionState?.currentTask ?? 'Unknown task';
911
- // Strip any "Completed: " prefixes to avoid cascading
912
- const cleanedTask = rawTask.replace(/^(Completed: )+/, '');
913
- // Don't use auto-checkpoint or already-completed tasks as real task names
914
- const isNamedTask = !cleanedTask.startsWith('Auto-checkpoint') && cleanedTask !== 'Unknown task';
915
- const completedTask = isNamedTask
916
- ? cleanedTask
917
- : params.summary.slice(0, 60).replace(/\n/g, ' ');
918
-
919
- const engram = store.createEngram({
920
- agentId: AGENT_ID,
921
- concept: completedTask.slice(0, 80),
922
- content: params.summary,
923
- tags: [...params.tags, 'task-summary'],
924
- salience: isNamedTask ? Math.max(salience.score, 0.7) : salience.score, // Only floor salience for named tasks
925
- confidence: 0.65, // Task summaries are decision-grade (completed work)
926
- salienceFeatures: salience.features,
927
- reasonCodes: [...salience.reasonCodes, 'task-end'],
928
- });
929
-
930
- connectionEngine.enqueue(engram.id);
931
-
932
- // 2. Handle supersessions — mark old memories as outdated
933
- let supersededCount = 0;
934
- for (const oldId of params.supersedes) {
935
- const oldEngram = store.getEngram(oldId);
936
- if (oldEngram) {
937
- store.supersedeEngram(oldId, engram.id);
938
- store.upsertAssociation(engram.id, oldId, 0.8, 'causal', 0.9);
939
- store.updateConfidence(oldId, Math.max(0.2, oldEngram.confidence * 0.4));
940
- supersededCount++;
941
- }
942
- }
943
-
944
- // Generate embedding asynchronously
945
- embed(`Task completed: ${params.summary}`).then(vec => {
946
- store.updateEmbedding(engram.id, vec);
947
- }).catch(() => {});
948
-
949
- // 2. Update checkpoint to reflect completion
950
- store.saveCheckpoint(AGENT_ID, {
951
- currentTask: `Completed: ${completedTask}`,
952
- decisions: checkpoint?.executionState?.decisions ?? [],
953
- activeFiles: [],
954
- nextSteps: [],
955
- relatedMemoryIds: [engram.id],
956
- notes: `Task completed. Summary memory: ${engram.id}`,
957
- episodeId: null,
958
- });
959
-
960
- store.updateAutoCheckpointWrite(AGENT_ID, engram.id);
961
- log(AGENT_ID, 'task:end', `"${completedTask}" summary=${engram.id} salience=${salience.score.toFixed(2)} superseded=${supersededCount}`);
962
-
963
- const supersededNote = supersededCount > 0 ? ` (${supersededCount} old memories superseded)` : '';
964
- return {
965
- content: [{
966
- type: 'text' as const,
967
- text: `Completed: "${completedTask}" [${salience.score.toFixed(2)}]${supersededNote}`,
968
- }],
969
- };
970
- }
971
- );
972
-
973
- // --- Start ---
974
-
975
- async function main() {
976
- const transport = new StdioServerTransport();
977
- await server.connect(transport);
978
-
979
- // Start hook sidecar (lightweight HTTP for Claude Code hooks)
980
- const sidecar = startSidecar({
981
- store,
982
- agentId: AGENT_ID,
983
- secret: HOOK_SECRET,
984
- port: HOOK_PORT,
985
- onConsolidate: async (agentId, reason) => {
986
- console.error(`[mcp] consolidation triggered: ${reason}`);
987
- const result = await consolidationEngine.consolidate(agentId);
988
- store.markConsolidation(agentId, false);
989
- console.error(`[mcp] consolidation done: ${result.edgesStrengthened} strengthened, ${result.memoriesForgotten} forgotten`);
990
- },
991
- });
992
-
993
- // Log to stderr (stdout is reserved for MCP protocol)
994
- console.error(`AgentWorkingMemory MCP server started (agent: ${AGENT_ID}, db: ${DB_PATH})`);
995
- console.error(`Hook sidecar on 127.0.0.1:${HOOK_PORT}${HOOK_SECRET ? ' (auth enabled)' : ' (no auth — set AWM_HOOK_SECRET)'}`);
996
-
997
- // Clean shutdown
998
- const cleanup = () => {
999
- sidecar.close();
1000
- consolidationScheduler.stop();
1001
- stagingBuffer.stop();
1002
- store.close();
1003
- };
1004
- process.on('SIGINT', () => { cleanup(); process.exit(0); });
1005
- process.on('SIGTERM', () => { cleanup(); process.exit(0); });
1006
- }
1007
-
1008
- main().catch(err => {
1009
- console.error('MCP server failed:', err);
1010
- process.exit(1);
1011
- });
1012
-
1013
- } // end else (non-incognito)
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * MCP Server — Model Context Protocol interface for AgentWorkingMemory.
5
+ *
6
+ * Runs as a stdio-based MCP server that Claude Code connects to directly.
7
+ * Uses the storage and engine layers in-process (no HTTP overhead).
8
+ *
9
+ * Tools exposed (12):
10
+ * memory_write — store a memory (salience filter decides disposition)
11
+ * memory_recall — activate memories by context (cognitive retrieval)
12
+ * memory_feedback — report whether a recalled memory was useful
13
+ * memory_retract — invalidate a wrong memory with optional correction
14
+ * memory_supersede — replace an outdated memory with a current one
15
+ * memory_stats — get memory health metrics
16
+ * memory_checkpoint — save structured execution state (survives compaction)
17
+ * memory_restore — restore state + targeted recall after compaction
18
+ * memory_task_add — create a prioritized task
19
+ * memory_task_update — change task status, priority, or blocking
20
+ * memory_task_list — list tasks filtered by status
21
+ * memory_task_next — get the highest-priority actionable task
22
+ *
23
+ * Run: npx tsx src/mcp.ts
24
+ * Config: add to ~/.claude.json or .mcp.json
25
+ */
26
+
27
+ import { readFileSync } from 'node:fs';
28
+ import { resolve } from 'node:path';
29
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
30
+
31
+ // Load .env file if present (no external dependency)
32
+ try {
33
+ const envPath = resolve(process.cwd(), '.env');
34
+ const envContent = readFileSync(envPath, 'utf-8');
35
+ for (const line of envContent.split('\n')) {
36
+ const trimmed = line.trim();
37
+ if (!trimmed || trimmed.startsWith('#')) continue;
38
+ const eqIdx = trimmed.indexOf('=');
39
+ if (eqIdx === -1) continue;
40
+ const key = trimmed.slice(0, eqIdx).trim();
41
+ const val = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, '');
42
+ if (!process.env[key]) process.env[key] = val;
43
+ }
44
+ } catch { /* No .env file */ }
45
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
46
+ import { z } from 'zod';
47
+
48
+ import { EngramStore } from './storage/sqlite.js';
49
+ import { ActivationEngine } from './engine/activation.js';
50
+ import { ConnectionEngine } from './engine/connections.js';
51
+ import { StagingBuffer } from './engine/staging.js';
52
+ import { EvictionEngine } from './engine/eviction.js';
53
+ import { RetractionEngine } from './engine/retraction.js';
54
+ import { EvalEngine } from './engine/eval.js';
55
+ import { ConsolidationEngine } from './engine/consolidation.js';
56
+ import { ConsolidationScheduler } from './engine/consolidation-scheduler.js';
57
+ import { evaluateSalience, computeNovelty, computeNoveltyWithMatch } from './core/salience.js';
58
+ import type { ConsciousState } from './types/checkpoint.js';
59
+ import type { SalienceEventType } from './core/salience.js';
60
+ import type { TaskStatus, TaskPriority } from './types/engram.js';
61
+ import { DEFAULT_AGENT_CONFIG } from './types/agent.js';
62
+ import { embed } from './core/embeddings.js';
63
+ import { startSidecar } from './hooks/sidecar.js';
64
+ import { initLogger, log, getLogPath } from './core/logger.js';
65
+
66
+ // --- Incognito Mode ---
67
+ // When AWM_INCOGNITO=1, register zero tools. Claude won't see memory tools at all.
68
+ // No DB, no engines, no sidecar — just a bare MCP server that exposes nothing.
69
+
70
+ const INCOGNITO = process.env.AWM_INCOGNITO === '1' || process.env.AWM_INCOGNITO === 'true';
71
+
72
+ if (INCOGNITO) {
73
+ console.error('AWM: incognito mode — all memory tools disabled, nothing will be recorded');
74
+ const server = new McpServer({ name: 'agent-working-memory', version: '0.5.5' });
75
+ const transport = new StdioServerTransport();
76
+ server.connect(transport).catch(err => {
77
+ console.error('MCP server failed:', err);
78
+ process.exit(1);
79
+ });
80
+ // No tools registered — Claude won't see any memory_* tools
81
+ } else {
82
+
83
+ // --- Setup ---
84
+
85
+ const DB_PATH = process.env.AWM_DB_PATH ?? 'memory.db';
86
+ const AGENT_ID = process.env.AWM_AGENT_ID ?? process.env.WORKER_NAME ?? 'claude-code';
87
+ const HOOK_PORT = parseInt(process.env.AWM_HOOK_PORT ?? '8401', 10);
88
+ const HOOK_SECRET = process.env.AWM_HOOK_SECRET ?? null;
89
+
90
+ initLogger(DB_PATH);
91
+ log(AGENT_ID, 'startup', `MCP server starting (db: ${DB_PATH}, hooks: ${HOOK_PORT})`);
92
+
93
+ const store = new EngramStore(DB_PATH);
94
+ const activationEngine = new ActivationEngine(store);
95
+ const connectionEngine = new ConnectionEngine(store, activationEngine);
96
+ const stagingBuffer = new StagingBuffer(store, activationEngine);
97
+ const evictionEngine = new EvictionEngine(store);
98
+ const retractionEngine = new RetractionEngine(store);
99
+ const evalEngine = new EvalEngine(store);
100
+ const consolidationEngine = new ConsolidationEngine(store);
101
+ const consolidationScheduler = new ConsolidationScheduler(store, consolidationEngine);
102
+
103
+ stagingBuffer.start(DEFAULT_AGENT_CONFIG.stagingTtlMs);
104
+ consolidationScheduler.start();
105
+
106
+ const server = new McpServer({
107
+ name: 'agent-working-memory',
108
+ version: '0.5.5',
109
+ });
110
+
111
+ // --- Tools ---
112
+
113
+ server.tool(
114
+ 'memory_write',
115
+ `Store a memory. The salience filter decides whether it's worth keeping (active), needs more evidence (staging), or should be discarded.
116
+
117
+ CALL THIS PROACTIVELY — do not wait to be asked. Write memories when you:
118
+ - Discover something about the codebase, bugs, or architecture
119
+ - Make a decision and want to remember why
120
+ - Encounter and resolve an error
121
+ - Learn a user preference or project pattern
122
+ - Complete a significant piece of work
123
+
124
+ The concept should be a short label (3-8 words). The content should be the full detail.`,
125
+ {
126
+ concept: z.string().describe('Short label for this memory (3-8 words)'),
127
+ content: z.string().describe('Full detail of what was learned'),
128
+ tags: z.array(z.string()).optional().describe('Optional tags for categorization'),
129
+ event_type: z.enum(['observation', 'decision', 'friction', 'surprise', 'causal'])
130
+ .optional().default('observation')
131
+ .describe('Type of event: observation (default), decision, friction (error/blocker), surprise, causal (root cause)'),
132
+ surprise: z.number().min(0).max(1).optional().default(0.3)
133
+ .describe('How surprising was this? 0=expected, 1=very unexpected'),
134
+ decision_made: z.boolean().optional().default(false)
135
+ .describe('Was a decision made? True boosts importance'),
136
+ causal_depth: z.number().min(0).max(1).optional().default(0.3)
137
+ .describe('How deep is the causal understanding? 0=surface, 1=root cause'),
138
+ resolution_effort: z.number().min(0).max(1).optional().default(0.3)
139
+ .describe('How much effort to resolve? 0=trivial, 1=significant debugging'),
140
+ memory_class: z.enum(['canonical', 'working', 'ephemeral']).optional().default('working')
141
+ .describe('Memory class: canonical (source-of-truth, never stages), working (default), ephemeral (temporary, decays faster)'),
142
+ supersedes: z.string().optional()
143
+ .describe('ID of an older memory this one replaces. The old memory is down-ranked, not deleted.'),
144
+ },
145
+ async (params) => {
146
+ // Check novelty with match info for reinforcement
147
+ const noveltyResult = computeNoveltyWithMatch(store, AGENT_ID, params.concept, params.content);
148
+ const novelty = noveltyResult.novelty;
149
+
150
+ // --- Reinforce-on-Duplicate check ---
151
+ // Tightened thresholds: require near-exact match (novelty < 0.3, BM25 > 0.85, 60% content overlap)
152
+ if (novelty < 0.3
153
+ && noveltyResult.matchScore > 0.85
154
+ && noveltyResult.matchedEngramId) {
155
+ const matchedEngram = store.getEngram(noveltyResult.matchedEngramId);
156
+ if (matchedEngram) {
157
+ const existingTokens = new Set(matchedEngram.content.toLowerCase().split(/\s+/).filter(w => w.length > 3));
158
+ const newTokens = new Set(params.content.toLowerCase().split(/\s+/).filter(w => w.length > 3));
159
+ let overlap = 0;
160
+ for (const t of newTokens) { if (existingTokens.has(t)) overlap++; }
161
+ const contentOverlap = newTokens.size > 0 ? overlap / newTokens.size : 0;
162
+
163
+ if (contentOverlap > 0.6) {
164
+ // True duplicate — reinforce existing and skip creation
165
+ store.touchEngram(noveltyResult.matchedEngramId);
166
+ try { store.updateAutoCheckpointWrite(AGENT_ID, noveltyResult.matchedEngramId); } catch { /* non-fatal */ }
167
+ log(AGENT_ID, 'write:reinforce', `"${params.concept}" → reinforced "${matchedEngram.concept}" (overlap=${contentOverlap.toFixed(2)})`);
168
+ return {
169
+ content: [{
170
+ type: 'text' as const,
171
+ text: `Reinforced existing memory "${matchedEngram.concept}" (overlap ${(contentOverlap * 100).toFixed(0)}%)`,
172
+ }],
173
+ };
174
+ }
175
+ // Partial match — continue to create new memory
176
+ log(AGENT_ID, 'write:partial-match', `"${params.concept}" partially matched "${matchedEngram.concept}" (overlap=${contentOverlap.toFixed(2)}), creating new memory`);
177
+ }
178
+ }
179
+
180
+ const salience = evaluateSalience({
181
+ content: params.content,
182
+ eventType: params.event_type as SalienceEventType,
183
+ surprise: params.surprise,
184
+ decisionMade: params.decision_made,
185
+ causalDepth: params.causal_depth,
186
+ resolutionEffort: params.resolution_effort,
187
+ novelty,
188
+ memoryClass: params.memory_class,
189
+ });
190
+
191
+ // v0.5.4: No longer discard — store everything, use salience for ranking.
192
+ // Low-salience memories get low confidence so they rank below high-salience
193
+ // in retrieval, but remain available for recall when needed.
194
+ const isLowSalience = salience.disposition === 'discard';
195
+
196
+ const CONFIDENCE_PRIORS: Record<string, number> = {
197
+ decision: 0.65,
198
+ friction: 0.60,
199
+ causal: 0.60,
200
+ surprise: 0.55,
201
+ observation: 0.45,
202
+ };
203
+ const confidencePrior = isLowSalience
204
+ ? 0.25
205
+ : salience.disposition === 'staging'
206
+ ? 0.40
207
+ : CONFIDENCE_PRIORS[params.event_type ?? 'observation'] ?? 0.45;
208
+
209
+ const engram = store.createEngram({
210
+ agentId: AGENT_ID,
211
+ concept: params.concept,
212
+ content: params.content,
213
+ tags: isLowSalience ? [...(params.tags ?? []), 'low-salience'] : params.tags,
214
+ salience: salience.score,
215
+ confidence: confidencePrior,
216
+ salienceFeatures: salience.features,
217
+ reasonCodes: salience.reasonCodes,
218
+ ttl: salience.disposition === 'staging' ? DEFAULT_AGENT_CONFIG.stagingTtlMs : undefined,
219
+ memoryClass: params.memory_class,
220
+ supersedes: params.supersedes,
221
+ });
222
+
223
+ if (salience.disposition === 'staging') {
224
+ store.updateStage(engram.id, 'staging');
225
+ } else {
226
+ connectionEngine.enqueue(engram.id);
227
+ }
228
+
229
+ // Handle supersession: mark old memory as superseded
230
+ if (params.supersedes) {
231
+ const oldEngram = store.getEngram(params.supersedes);
232
+ if (oldEngram) {
233
+ store.supersedeEngram(params.supersedes, engram.id);
234
+ // Create supersession association
235
+ store.upsertAssociation(engram.id, oldEngram.id, 0.8, 'causal', 0.9);
236
+ }
237
+ }
238
+
239
+ // Generate embedding asynchronously (don't block response)
240
+ embed(`${params.concept} ${params.content}`).then(vec => {
241
+ store.updateEmbedding(engram.id, vec);
242
+ }).catch(() => {}); // Embedding failure is non-fatal
243
+
244
+ // Auto-checkpoint: track write
245
+ try { store.updateAutoCheckpointWrite(AGENT_ID, engram.id); } catch { /* non-fatal */ }
246
+
247
+ const logDisposition = isLowSalience ? 'low-salience' : salience.disposition;
248
+ log(AGENT_ID, `write:${logDisposition}`, `"${params.concept}" salience=${salience.score.toFixed(2)} novelty=${novelty.toFixed(1)} id=${engram.id}`);
249
+
250
+ return {
251
+ content: [{
252
+ type: 'text' as const,
253
+ text: `Stored (${salience.disposition}) "${params.concept}" [${salience.score.toFixed(2)}]`,
254
+ }],
255
+ };
256
+ }
257
+ );
258
+
259
+ server.tool(
260
+ 'memory_recall',
261
+ `Recall memories relevant to a query. Uses cognitive activation — not keyword search.
262
+
263
+ ALWAYS call this when:
264
+ - Starting work on a project or topic (recall what you know)
265
+ - Debugging (recall similar errors and solutions)
266
+ - Making decisions (recall past decisions and outcomes)
267
+ - The user mentions a topic you might have stored memories about
268
+
269
+ Accepts either "query" or "context" parameter — both work identically.
270
+ Returns the most relevant memories ranked by text relevance, temporal recency, and associative strength.`,
271
+ {
272
+ query: z.string().optional().describe('What to search for — describe the situation, question, or topic'),
273
+ context: z.string().optional().describe('Alias for query (either works)'),
274
+ limit: z.number().optional().default(5).describe('Max memories to return (default 5)'),
275
+ min_score: z.number().optional().default(0.05).describe('Minimum relevance score (default 0.05)'),
276
+ include_staging: z.boolean().optional().default(false).describe('Include weak/unconfirmed memories?'),
277
+ use_reranker: z.boolean().optional().default(true).describe('Use cross-encoder re-ranking for better relevance (default true)'),
278
+ use_expansion: z.boolean().optional().default(true).describe('Expand query with synonyms for better recall (default true)'),
279
+ },
280
+ async (params) => {
281
+ const queryText = params.query ?? params.context;
282
+ if (!queryText) {
283
+ return {
284
+ content: [{
285
+ type: 'text' as const,
286
+ text: 'Error: provide either "query" or "context" parameter with your search text.',
287
+ }],
288
+ };
289
+ }
290
+ const results = await activationEngine.activate({
291
+ agentId: AGENT_ID,
292
+ context: queryText,
293
+ limit: params.limit,
294
+ minScore: params.min_score,
295
+ includeStaging: params.include_staging,
296
+ useReranker: params.use_reranker,
297
+ useExpansion: params.use_expansion,
298
+ });
299
+
300
+ // Auto-checkpoint: track recall
301
+ try {
302
+ const ids = results.map(r => r.engram.id);
303
+ store.updateAutoCheckpointRecall(AGENT_ID, queryText, ids);
304
+ } catch { /* non-fatal */ }
305
+
306
+ log(AGENT_ID, 'recall', `"${queryText.slice(0, 80)}" → ${results.length} results`);
307
+
308
+ if (results.length === 0) {
309
+ return {
310
+ content: [{
311
+ type: 'text' as const,
312
+ text: 'No relevant memories found.',
313
+ }],
314
+ };
315
+ }
316
+
317
+ const lines = results.map((r, i) => {
318
+ return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)}): ${r.engram.content}`;
319
+ });
320
+
321
+ return {
322
+ content: [{
323
+ type: 'text' as const,
324
+ text: lines.join('\n'),
325
+ }],
326
+ };
327
+ }
328
+ );
329
+
330
+ server.tool(
331
+ 'memory_feedback',
332
+ `Report whether a recalled memory was actually useful. This updates the memory's confidence score — useful memories become stronger, useless ones weaken.
333
+
334
+ Always call this after using a recalled memory so the system learns what's valuable.`,
335
+ {
336
+ engram_id: z.string().describe('ID of the memory (from memory_recall results)'),
337
+ useful: z.boolean().describe('Was this memory actually helpful?'),
338
+ context: z.string().optional().describe('Brief note on why it was/wasn\'t useful'),
339
+ },
340
+ async (params) => {
341
+ store.logRetrievalFeedback(null, params.engram_id, params.useful, params.context ?? '');
342
+
343
+ const engram = store.getEngram(params.engram_id);
344
+ if (engram) {
345
+ const delta = params.useful
346
+ ? DEFAULT_AGENT_CONFIG.feedbackPositiveBoost
347
+ : -DEFAULT_AGENT_CONFIG.feedbackNegativePenalty;
348
+ store.updateConfidence(engram.id, engram.confidence + delta);
349
+ }
350
+
351
+ return {
352
+ content: [{
353
+ type: 'text' as const,
354
+ text: `Feedback: ${params.useful ? '+useful' : '-not useful'}`,
355
+ }],
356
+ };
357
+ }
358
+ );
359
+
360
+ server.tool(
361
+ 'memory_retract',
362
+ `Retract a memory that turned out to be wrong. Creates a correction and reduces confidence of related memories.
363
+
364
+ Use this when you discover a memory contains incorrect information.`,
365
+ {
366
+ engram_id: z.string().describe('ID of the wrong memory'),
367
+ reason: z.string().describe('Why is this memory wrong?'),
368
+ correction: z.string().optional().describe('What is the correct information? (creates a new memory)'),
369
+ },
370
+ async (params) => {
371
+ const result = retractionEngine.retract({
372
+ agentId: AGENT_ID,
373
+ targetEngramId: params.engram_id,
374
+ reason: params.reason,
375
+ counterContent: params.correction,
376
+ });
377
+
378
+ const parts = [`Memory ${params.engram_id} retracted.`];
379
+ if (result.correctionId) {
380
+ parts.push(`Correction stored as ${result.correctionId}.`);
381
+ }
382
+ parts.push(`${result.associatesAffected} related memories had confidence reduced.`);
383
+
384
+ return {
385
+ content: [{
386
+ type: 'text' as const,
387
+ text: parts.join(' '),
388
+ }],
389
+ };
390
+ }
391
+ );
392
+
393
+ server.tool(
394
+ 'memory_supersede',
395
+ `Replace an outdated memory with a newer one. Unlike retraction (which marks memories as wrong), supersession marks the old memory as outdated but historically correct.
396
+
397
+ Use this when:
398
+ - A status or count has changed (e.g., "5 reviews done" → "7 reviews done")
399
+ - Architecture or infrastructure evolved (e.g., "two-repo model" → "three-repo model")
400
+ - A schedule or plan was updated
401
+
402
+ The old memory stays in the database (searchable for history) but is heavily down-ranked in recall so the current version dominates.`,
403
+ {
404
+ old_engram_id: z.string().describe('ID of the outdated memory'),
405
+ new_engram_id: z.string().describe('ID of the replacement memory'),
406
+ reason: z.string().optional().describe('Why the old memory is outdated'),
407
+ },
408
+ async (params) => {
409
+ const oldEngram = store.getEngram(params.old_engram_id);
410
+ if (!oldEngram) {
411
+ return { content: [{ type: 'text' as const, text: `Old memory not found: ${params.old_engram_id}` }] };
412
+ }
413
+ const newEngram = store.getEngram(params.new_engram_id);
414
+ if (!newEngram) {
415
+ return { content: [{ type: 'text' as const, text: `New memory not found: ${params.new_engram_id}` }] };
416
+ }
417
+
418
+ store.supersedeEngram(params.old_engram_id, params.new_engram_id);
419
+
420
+ // Create supersession association (new → old)
421
+ store.upsertAssociation(params.new_engram_id, params.old_engram_id, 0.8, 'causal', 0.9);
422
+
423
+ // Reduce old memory's confidence (not to zero — it's historical, not wrong)
424
+ store.updateConfidence(params.old_engram_id, Math.max(0.2, oldEngram.confidence * 0.4));
425
+
426
+ log(AGENT_ID, 'supersede', `"${oldEngram.concept}" → "${newEngram.concept}"${params.reason ? ` (${params.reason})` : ''}`);
427
+
428
+ return {
429
+ content: [{
430
+ type: 'text' as const,
431
+ text: `Superseded: "${oldEngram.concept}" → "${newEngram.concept}"`,
432
+ }],
433
+ };
434
+ }
435
+ );
436
+
437
+ server.tool(
438
+ 'memory_stats',
439
+ `Get memory health stats — how many memories, confidence levels, association count, and system performance.
440
+ Also shows the activity log path so the user can tail it to see what's happening.`,
441
+ {},
442
+ async () => {
443
+ const metrics = evalEngine.computeMetrics(AGENT_ID);
444
+ const checkpoint = store.getCheckpoint(AGENT_ID);
445
+ const lines = [
446
+ `Agent: ${AGENT_ID}`,
447
+ `Active memories: ${metrics.activeEngramCount}`,
448
+ `Staging: ${metrics.stagingEngramCount}`,
449
+ `Retracted: ${metrics.retractedCount}`,
450
+ `Avg confidence: ${metrics.avgConfidence.toFixed(3)}`,
451
+ `Total edges: ${metrics.totalEdges}`,
452
+ `Edge utility: ${(metrics.edgeUtilityRate * 100).toFixed(1)}%`,
453
+ `Activations (24h): ${metrics.activationCount}`,
454
+ `Avg latency: ${metrics.avgLatencyMs.toFixed(1)}ms`,
455
+ ``,
456
+ `Session writes: ${checkpoint?.auto.writeCountSinceConsolidation ?? 0}`,
457
+ `Session recalls: ${checkpoint?.auto.recallCountSinceConsolidation ?? 0}`,
458
+ `Last activity: ${checkpoint?.auto.lastActivityAt?.toISOString() ?? 'never'}`,
459
+ `Checkpoint: ${checkpoint?.executionState ? checkpoint.executionState.currentTask : 'none'}`,
460
+ ``,
461
+ `Activity log: ${getLogPath() ?? 'not configured'}`,
462
+ `Hook sidecar: 127.0.0.1:${HOOK_PORT}`,
463
+ ];
464
+
465
+ return {
466
+ content: [{
467
+ type: 'text' as const,
468
+ text: lines.join('\n'),
469
+ }],
470
+ };
471
+ }
472
+ );
473
+
474
+ // --- Checkpointing Tools ---
475
+
476
+ server.tool(
477
+ 'memory_checkpoint',
478
+ `Save your current execution state so you can recover after context compaction.
479
+
480
+ ALWAYS call this before:
481
+ - Long operations (multi-file generation, large refactors, overnight work)
482
+ - Anything that might fill the context window
483
+ - Switching to a different task
484
+
485
+ Also call periodically during long sessions to avoid losing state. The state is saved per-agent and overwrites any previous checkpoint.`,
486
+ {
487
+ current_task: z.string().describe('What you are currently working on'),
488
+ decisions: z.array(z.string()).optional().default([])
489
+ .describe('Key decisions made so far'),
490
+ active_files: z.array(z.string()).optional().default([])
491
+ .describe('Files you are currently working with'),
492
+ next_steps: z.array(z.string()).optional().default([])
493
+ .describe('What needs to happen next'),
494
+ related_memory_ids: z.array(z.string()).optional().default([])
495
+ .describe('IDs of memories relevant to current work'),
496
+ notes: z.string().optional().default('')
497
+ .describe('Any other context worth preserving'),
498
+ episode_id: z.string().optional()
499
+ .describe('Current episode ID if known'),
500
+ },
501
+ async (params) => {
502
+ const state: ConsciousState = {
503
+ currentTask: params.current_task,
504
+ decisions: params.decisions,
505
+ activeFiles: params.active_files,
506
+ nextSteps: params.next_steps,
507
+ relatedMemoryIds: params.related_memory_ids,
508
+ notes: params.notes,
509
+ episodeId: params.episode_id ?? null,
510
+ };
511
+
512
+ store.saveCheckpoint(AGENT_ID, state);
513
+ log(AGENT_ID, 'checkpoint', `"${params.current_task}" decisions=${params.decisions.length} files=${params.active_files.length}`);
514
+
515
+ return {
516
+ content: [{
517
+ type: 'text' as const,
518
+ text: `Checkpoint saved: "${params.current_task}" (${params.decisions.length} decisions, ${params.active_files.length} files)`,
519
+ }],
520
+ };
521
+ }
522
+ );
523
+
524
+ server.tool(
525
+ 'memory_restore',
526
+ `Restore your previous execution state after context compaction or at session start.
527
+
528
+ Returns:
529
+ - Your saved execution state (task, decisions, next steps, files)
530
+ - Recently recalled memories for context
531
+ - Your last write for continuity
532
+ - How long you were idle
533
+
534
+ Use this at the start of every session or after compaction to pick up where you left off.`,
535
+ {},
536
+ async () => {
537
+ const checkpoint = store.getCheckpoint(AGENT_ID);
538
+
539
+ const now = Date.now();
540
+ const idleMs = checkpoint
541
+ ? now - checkpoint.auto.lastActivityAt.getTime()
542
+ : 0;
543
+
544
+ // Get last written engram
545
+ let lastWrite: { id: string; concept: string; content: string } | null = null;
546
+ if (checkpoint?.auto.lastWriteId) {
547
+ const engram = store.getEngram(checkpoint.auto.lastWriteId);
548
+ if (engram) {
549
+ lastWrite = { id: engram.id, concept: engram.concept, content: engram.content };
550
+ }
551
+ }
552
+
553
+ // Recall memories using last context
554
+ let recalledMemories: Array<{ id: string; concept: string; content: string; score: number }> = [];
555
+ const recallContext = checkpoint?.auto.lastRecallContext
556
+ ?? checkpoint?.executionState?.currentTask
557
+ ?? null;
558
+
559
+ if (recallContext) {
560
+ try {
561
+ const results = await activationEngine.activate({
562
+ agentId: AGENT_ID,
563
+ context: recallContext,
564
+ limit: 5,
565
+ minScore: 0.05,
566
+ useReranker: true,
567
+ useExpansion: true,
568
+ });
569
+ recalledMemories = results.map(r => ({
570
+ id: r.engram.id,
571
+ concept: r.engram.concept,
572
+ content: r.engram.content,
573
+ score: r.score,
574
+ }));
575
+ } catch { /* recall failure is non-fatal */ }
576
+ }
577
+
578
+ // Consolidation on restore:
579
+ // - If idle >5min but last consolidation was recent (graceful exit ran it), skip
580
+ // - If idle >5min and no recent consolidation, run full cycle (non-graceful exit fallback)
581
+ const MINI_IDLE_MS = 5 * 60_000;
582
+ const FULL_CONSOLIDATION_GAP_MS = 10 * 60_000; // 10 min — if last consolidation was longer ago, run full
583
+ let miniConsolidationTriggered = false;
584
+ let fullConsolidationTriggered = false;
585
+
586
+ if (idleMs > MINI_IDLE_MS) {
587
+ const sinceLastConsolidation = checkpoint?.lastConsolidationAt
588
+ ? now - checkpoint.lastConsolidationAt.getTime()
589
+ : Infinity;
590
+
591
+ if (sinceLastConsolidation > FULL_CONSOLIDATION_GAP_MS) {
592
+ // No recent consolidation — graceful exit didn't happen, run full cycle
593
+ fullConsolidationTriggered = true;
594
+ try {
595
+ const result = await consolidationEngine.consolidate(AGENT_ID);
596
+ store.markConsolidation(AGENT_ID, false);
597
+ log(AGENT_ID, 'consolidation', `full sleep cycle on restore (no graceful exit, idle ${Math.round(idleMs / 60_000)}min, last consolidation ${Math.round(sinceLastConsolidation / 60_000)}min ago) — ${result.edgesStrengthened} strengthened, ${result.memoriesForgotten} forgotten`);
598
+ } catch { /* consolidation failure is non-fatal */ }
599
+ } else {
600
+ // Recent consolidation exists — graceful exit already handled it, just do mini
601
+ miniConsolidationTriggered = true;
602
+ consolidationScheduler.runMiniConsolidation(AGENT_ID).catch(() => {});
603
+ }
604
+ }
605
+
606
+ // Format response
607
+ const parts: string[] = [];
608
+ const idleMin = Math.round(idleMs / 60_000);
609
+ const consolidationNote = fullConsolidationTriggered
610
+ ? ' (full consolidation — no graceful exit detected)'
611
+ : miniConsolidationTriggered
612
+ ? ' (mini-consolidation triggered)'
613
+ : '';
614
+ log(AGENT_ID, 'restore', `idle=${idleMin}min checkpoint=${!!checkpoint?.executionState} recalled=${recalledMemories.length} lastWrite=${lastWrite?.concept ?? 'none'}${fullConsolidationTriggered ? ' FULL_CONSOLIDATION' : ''}`);
615
+ parts.push(`Idle: ${idleMin}min${consolidationNote}`);
616
+
617
+ if (checkpoint?.executionState) {
618
+ const s = checkpoint.executionState;
619
+ parts.push(`\n**Current task:** ${s.currentTask}`);
620
+ if (s.decisions.length) parts.push(`**Decisions:** ${s.decisions.join('; ')}`);
621
+ if (s.nextSteps.length) parts.push(`**Next steps:** ${s.nextSteps.map((st, i) => `${i + 1}. ${st}`).join(', ')}`);
622
+ if (s.activeFiles.length) parts.push(`**Active files:** ${s.activeFiles.join(', ')}`);
623
+ if (s.notes) parts.push(`**Notes:** ${s.notes}`);
624
+ if (checkpoint.checkpointAt) parts.push(`_Saved at: ${checkpoint.checkpointAt.toISOString()}_`);
625
+ } else {
626
+ parts.push('\nNo explicit checkpoint saved.');
627
+ parts.push('\n**Tip:** Use memory_write to save important learnings, and memory_checkpoint before long operations so you can recover state.');
628
+ }
629
+
630
+ if (lastWrite) {
631
+ parts.push(`\n**Last write:** ${lastWrite.concept}\n${lastWrite.content}`);
632
+ }
633
+
634
+ if (recalledMemories.length > 0) {
635
+ parts.push(`\n**Recalled memories (${recalledMemories.length}):**`);
636
+ for (const m of recalledMemories) {
637
+ parts.push(`- **${m.concept}** (${m.score.toFixed(3)}): ${m.content.slice(0, 150)}${m.content.length > 150 ? '...' : ''}`);
638
+ }
639
+ }
640
+
641
+ return {
642
+ content: [{
643
+ type: 'text' as const,
644
+ text: parts.join('\n'),
645
+ }],
646
+ };
647
+ }
648
+ );
649
+
650
+ // --- Task Management Tools ---
651
+
652
+ server.tool(
653
+ 'memory_task_add',
654
+ `Create a task that you need to come back to. Tasks are memories with status and priority tracking.
655
+
656
+ Use this when:
657
+ - You identify work that needs doing but can't do it right now
658
+ - The user mentions something to do later
659
+ - You want to park a sub-task while focusing on something more urgent
660
+
661
+ Tasks automatically get high salience so they won't be discarded.`,
662
+ {
663
+ concept: z.string().describe('Short task title (3-10 words)'),
664
+ content: z.string().describe('Full task description — what needs doing, context, acceptance criteria'),
665
+ tags: z.array(z.string()).optional().describe('Tags for categorization'),
666
+ priority: z.enum(['urgent', 'high', 'medium', 'low']).default('medium')
667
+ .describe('Task priority: urgent (do now), high (do soon), medium (normal), low (backlog)'),
668
+ blocked_by: z.string().optional().describe('ID of a task that must finish first'),
669
+ },
670
+ async (params) => {
671
+ const engram = store.createEngram({
672
+ agentId: AGENT_ID,
673
+ concept: params.concept,
674
+ content: params.content,
675
+ tags: [...(params.tags ?? []), 'task'],
676
+ salience: 0.9, // Tasks always high salience
677
+ confidence: 0.8,
678
+ salienceFeatures: {
679
+ surprise: 0.5,
680
+ decisionMade: true,
681
+ causalDepth: 0.5,
682
+ resolutionEffort: 0.5,
683
+ eventType: 'decision',
684
+ },
685
+ reasonCodes: ['task-created'],
686
+ taskStatus: params.blocked_by ? 'blocked' : 'open',
687
+ taskPriority: params.priority as TaskPriority,
688
+ blockedBy: params.blocked_by,
689
+ });
690
+
691
+ connectionEngine.enqueue(engram.id);
692
+
693
+ // Generate embedding asynchronously
694
+ embed(`${params.concept} ${params.content}`).then(vec => {
695
+ store.updateEmbedding(engram.id, vec);
696
+ }).catch(() => {});
697
+
698
+ return {
699
+ content: [{
700
+ type: 'text' as const,
701
+ text: `Task created: "${params.concept}" (${params.priority})`,
702
+ }],
703
+ };
704
+ }
705
+ );
706
+
707
+ server.tool(
708
+ 'memory_task_update',
709
+ `Update a task's status or priority. Use this to:
710
+ - Start working on a task (open → in_progress)
711
+ - Mark a task done (→ done)
712
+ - Block a task on another (→ blocked)
713
+ - Reprioritize (change priority)
714
+ - Unblock a task (clear blocked_by)`,
715
+ {
716
+ task_id: z.string().describe('ID of the task to update'),
717
+ status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
718
+ .describe('New status'),
719
+ priority: z.enum(['urgent', 'high', 'medium', 'low']).optional()
720
+ .describe('New priority'),
721
+ blocked_by: z.string().optional().describe('ID of blocking task (set to empty string to unblock)'),
722
+ },
723
+ async (params) => {
724
+ const engram = store.getEngram(params.task_id);
725
+ if (!engram || !engram.taskStatus) {
726
+ return { content: [{ type: 'text' as const, text: `Task not found: ${params.task_id}` }] };
727
+ }
728
+
729
+ if (params.blocked_by !== undefined) {
730
+ store.updateBlockedBy(params.task_id, params.blocked_by || null);
731
+ }
732
+ if (params.status) {
733
+ store.updateTaskStatus(params.task_id, params.status as TaskStatus);
734
+ }
735
+ if (params.priority) {
736
+ store.updateTaskPriority(params.task_id, params.priority as TaskPriority);
737
+ }
738
+
739
+ const updated = store.getEngram(params.task_id)!;
740
+ return {
741
+ content: [{
742
+ type: 'text' as const,
743
+ text: `Updated: "${updated.concept}" → ${updated.taskStatus} (${updated.taskPriority})`,
744
+ }],
745
+ };
746
+ }
747
+ );
748
+
749
+ server.tool(
750
+ 'memory_task_list',
751
+ `List tasks with optional status filter. Shows tasks ordered by priority (urgent first).
752
+
753
+ Use at the start of a session to see what's pending, or to check blocked/done tasks.`,
754
+ {
755
+ status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
756
+ .describe('Filter by status (omit to see all active tasks)'),
757
+ include_done: z.boolean().optional().default(false)
758
+ .describe('Include completed tasks?'),
759
+ },
760
+ async (params) => {
761
+ let tasks = store.getTasks(AGENT_ID, params.status as TaskStatus | undefined);
762
+ if (!params.include_done && !params.status) {
763
+ tasks = tasks.filter(t => t.taskStatus !== 'done');
764
+ }
765
+
766
+ if (tasks.length === 0) {
767
+ return { content: [{ type: 'text' as const, text: 'No tasks found.' }] };
768
+ }
769
+
770
+ const lines = tasks.map((t, i) => {
771
+ const blocked = t.blockedBy ? ` [blocked by ${t.blockedBy}]` : '';
772
+ const tags = t.tags?.filter(tag => tag !== 'task').join(', ');
773
+ 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}`;
774
+ });
775
+
776
+ return {
777
+ content: [{
778
+ type: 'text' as const,
779
+ text: `Tasks (${tasks.length}):\n\n${lines.join('\n\n')}`,
780
+ }],
781
+ };
782
+ }
783
+ );
784
+
785
+ server.tool(
786
+ 'memory_task_next',
787
+ `Get the single most important task to work on next.
788
+
789
+ Prioritizes: in_progress tasks first (finish what you started), then by priority level, then oldest first. Skips blocked and done tasks.
790
+
791
+ Use this when you finish a task or need to decide what to do next.`,
792
+ {},
793
+ async () => {
794
+ const next = store.getNextTask(AGENT_ID);
795
+ if (!next) {
796
+ return { content: [{ type: 'text' as const, text: 'No actionable tasks. All clear!' }] };
797
+ }
798
+
799
+ const blocked = next.blockedBy ? `\nBlocked by: ${next.blockedBy}` : '';
800
+ const tags = next.tags?.filter(tag => tag !== 'task').join(', ');
801
+
802
+ return {
803
+ content: [{
804
+ type: 'text' as const,
805
+ text: `Next task:\n**${next.concept}** (${next.taskPriority})\nStatus: ${next.taskStatus}\n${next.content}${blocked}\n${tags ? `Tags: ${tags}\n` : ''}ID: ${next.id}`,
806
+ }],
807
+ };
808
+ }
809
+ );
810
+
811
+ // --- Task Bracket Tools ---
812
+
813
+ server.tool(
814
+ 'memory_task_begin',
815
+ `Signal that you're starting a significant task. Auto-checkpoints current state and recalls relevant memories.
816
+
817
+ CALL THIS when starting:
818
+ - A multi-step operation (doc generation, large refactor, migration)
819
+ - Work on a new topic or project area
820
+ - Anything that might fill the context window
821
+
822
+ This ensures your state is saved before you start, and primes recall with relevant context.`,
823
+ {
824
+ topic: z.string().describe('What task are you starting? (3-15 words)'),
825
+ files: z.array(z.string()).optional().default([])
826
+ .describe('Files you expect to work with'),
827
+ notes: z.string().optional().default('')
828
+ .describe('Any additional context'),
829
+ },
830
+ async (params) => {
831
+ // 1. Checkpoint current state
832
+ const checkpoint = store.getCheckpoint(AGENT_ID);
833
+ const prevTask = checkpoint?.executionState?.currentTask ?? 'None';
834
+
835
+ store.saveCheckpoint(AGENT_ID, {
836
+ currentTask: params.topic,
837
+ decisions: [],
838
+ activeFiles: params.files,
839
+ nextSteps: [],
840
+ relatedMemoryIds: [],
841
+ notes: params.notes || `Started via memory_task_begin. Previous task: ${prevTask}`,
842
+ episodeId: null,
843
+ });
844
+
845
+ // 2. Auto-recall relevant memories
846
+ let recalledSummary = '';
847
+ try {
848
+ const results = await activationEngine.activate({
849
+ agentId: AGENT_ID,
850
+ context: params.topic,
851
+ limit: 5,
852
+ minScore: 0.05,
853
+ useReranker: true,
854
+ useExpansion: true,
855
+ });
856
+
857
+ if (results.length > 0) {
858
+ const lines = results.map((r, i) => {
859
+ const tags = r.engram.tags?.length ? ` [${r.engram.tags.join(', ')}]` : '';
860
+ return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)})${tags}\n ${r.engram.content.slice(0, 150)}${r.engram.content.length > 150 ? '...' : ''}`;
861
+ });
862
+ recalledSummary = `\n\n**Recalled memories (${results.length}):**\n${lines.join('\n')}`;
863
+
864
+ // Track recall
865
+ store.updateAutoCheckpointRecall(AGENT_ID, params.topic, results.map(r => r.engram.id));
866
+ }
867
+ } catch { /* recall failure is non-fatal */ }
868
+
869
+ log(AGENT_ID, 'task:begin', `"${params.topic}" prev="${prevTask}"`);
870
+
871
+ return {
872
+ content: [{
873
+ type: 'text' as const,
874
+ text: `Started: "${params.topic}" (prev: ${prevTask})${recalledSummary}`,
875
+ }],
876
+ };
877
+ }
878
+ );
879
+
880
+ server.tool(
881
+ 'memory_task_end',
882
+ `Signal that you've finished a significant task. Writes a summary memory and auto-checkpoints.
883
+
884
+ CALL THIS when you finish:
885
+ - A multi-step operation
886
+ - Before switching to a different topic
887
+ - At the end of a work session
888
+
889
+ This captures what was accomplished so future sessions can recall it.`,
890
+ {
891
+ summary: z.string().describe('What was accomplished? Include key outcomes, decisions, and any issues.'),
892
+ tags: z.array(z.string()).optional().default([])
893
+ .describe('Tags for the summary memory'),
894
+ supersedes: z.array(z.string()).optional().default([])
895
+ .describe('IDs of older memories this task summary replaces (marks them as superseded)'),
896
+ },
897
+ async (params) => {
898
+ // 1. Write summary as a memory
899
+ const salience = evaluateSalience({
900
+ content: params.summary,
901
+ eventType: 'decision',
902
+ surprise: 0.3,
903
+ decisionMade: true,
904
+ causalDepth: 0.5,
905
+ resolutionEffort: 0.5,
906
+ });
907
+
908
+ // Determine the real task name for the summary engram
909
+ const checkpoint = store.getCheckpoint(AGENT_ID);
910
+ const rawTask = checkpoint?.executionState?.currentTask ?? 'Unknown task';
911
+ // Strip any "Completed: " prefixes to avoid cascading
912
+ const cleanedTask = rawTask.replace(/^(Completed: )+/, '');
913
+ // Don't use auto-checkpoint or already-completed tasks as real task names
914
+ const isNamedTask = !cleanedTask.startsWith('Auto-checkpoint') && cleanedTask !== 'Unknown task';
915
+ const completedTask = isNamedTask
916
+ ? cleanedTask
917
+ : params.summary.slice(0, 60).replace(/\n/g, ' ');
918
+
919
+ const engram = store.createEngram({
920
+ agentId: AGENT_ID,
921
+ concept: completedTask.slice(0, 80),
922
+ content: params.summary,
923
+ tags: [...params.tags, 'task-summary'],
924
+ salience: isNamedTask ? Math.max(salience.score, 0.7) : salience.score, // Only floor salience for named tasks
925
+ confidence: 0.65, // Task summaries are decision-grade (completed work)
926
+ salienceFeatures: salience.features,
927
+ reasonCodes: [...salience.reasonCodes, 'task-end'],
928
+ });
929
+
930
+ connectionEngine.enqueue(engram.id);
931
+
932
+ // 2. Handle supersessions — mark old memories as outdated
933
+ let supersededCount = 0;
934
+ for (const oldId of params.supersedes) {
935
+ const oldEngram = store.getEngram(oldId);
936
+ if (oldEngram) {
937
+ store.supersedeEngram(oldId, engram.id);
938
+ store.upsertAssociation(engram.id, oldId, 0.8, 'causal', 0.9);
939
+ store.updateConfidence(oldId, Math.max(0.2, oldEngram.confidence * 0.4));
940
+ supersededCount++;
941
+ }
942
+ }
943
+
944
+ // Generate embedding asynchronously
945
+ embed(`Task completed: ${params.summary}`).then(vec => {
946
+ store.updateEmbedding(engram.id, vec);
947
+ }).catch(() => {});
948
+
949
+ // 2. Update checkpoint to reflect completion
950
+ store.saveCheckpoint(AGENT_ID, {
951
+ currentTask: `Completed: ${completedTask}`,
952
+ decisions: checkpoint?.executionState?.decisions ?? [],
953
+ activeFiles: [],
954
+ nextSteps: [],
955
+ relatedMemoryIds: [engram.id],
956
+ notes: `Task completed. Summary memory: ${engram.id}`,
957
+ episodeId: null,
958
+ });
959
+
960
+ store.updateAutoCheckpointWrite(AGENT_ID, engram.id);
961
+ log(AGENT_ID, 'task:end', `"${completedTask}" summary=${engram.id} salience=${salience.score.toFixed(2)} superseded=${supersededCount}`);
962
+
963
+ const supersededNote = supersededCount > 0 ? ` (${supersededCount} old memories superseded)` : '';
964
+ return {
965
+ content: [{
966
+ type: 'text' as const,
967
+ text: `Completed: "${completedTask}" [${salience.score.toFixed(2)}]${supersededNote}`,
968
+ }],
969
+ };
970
+ }
971
+ );
972
+
973
+ // --- Start ---
974
+
975
+ async function main() {
976
+ const transport = new StdioServerTransport();
977
+ await server.connect(transport);
978
+
979
+ // Start hook sidecar (lightweight HTTP for Claude Code hooks)
980
+ const sidecar = startSidecar({
981
+ store,
982
+ agentId: AGENT_ID,
983
+ secret: HOOK_SECRET,
984
+ port: HOOK_PORT,
985
+ onConsolidate: async (agentId, reason) => {
986
+ console.error(`[mcp] consolidation triggered: ${reason}`);
987
+ const result = await consolidationEngine.consolidate(agentId);
988
+ store.markConsolidation(agentId, false);
989
+ console.error(`[mcp] consolidation done: ${result.edgesStrengthened} strengthened, ${result.memoriesForgotten} forgotten`);
990
+ },
991
+ });
992
+
993
+ // Coordination MCP tools (opt-in via AWM_COORDINATION=true)
994
+ const coordEnabled = process.env.AWM_COORDINATION === 'true' || process.env.AWM_COORDINATION === '1';
995
+ if (coordEnabled) {
996
+ const { initCoordinationTables } = await import('./coordination/schema.js');
997
+ const { registerCoordinationTools } = await import('./coordination/mcp-tools.js');
998
+ initCoordinationTables(store.getDb());
999
+ registerCoordinationTools(server, store.getDb());
1000
+ } else {
1001
+ console.error('AWM: coordination tools disabled (set AWM_COORDINATION=true to enable)');
1002
+ }
1003
+
1004
+ // Log to stderr (stdout is reserved for MCP protocol)
1005
+ console.error(`AgentWorkingMemory MCP server started (agent: ${AGENT_ID}, db: ${DB_PATH})`);
1006
+ console.error(`Hook sidecar on 127.0.0.1:${HOOK_PORT}${HOOK_SECRET ? ' (auth enabled)' : ' (no auth — set AWM_HOOK_SECRET)'}`);
1007
+
1008
+ // Clean shutdown
1009
+ const cleanup = () => {
1010
+ sidecar.close();
1011
+ consolidationScheduler.stop();
1012
+ stagingBuffer.stop();
1013
+ store.close();
1014
+ };
1015
+ process.on('SIGINT', () => { cleanup(); process.exit(0); });
1016
+ process.on('SIGTERM', () => { cleanup(); process.exit(0); });
1017
+ }
1018
+
1019
+ main().catch(err => {
1020
+ console.error('MCP server failed:', err);
1021
+ process.exit(1);
1022
+ });
1023
+
1024
+ } // end else (non-incognito)