agent-working-memory 0.5.0 → 0.5.2
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.
- package/dist/cli.js +15 -4
- package/dist/cli.js.map +1 -1
- package/dist/core/salience.d.ts.map +1 -1
- package/dist/core/salience.js +11 -10
- package/dist/core/salience.js.map +1 -1
- package/dist/engine/consolidation.d.ts.map +1 -1
- package/dist/engine/consolidation.js +26 -7
- package/dist/engine/consolidation.js.map +1 -1
- package/dist/mcp.js +90 -82
- package/dist/mcp.js.map +1 -1
- package/dist/storage/sqlite.d.ts +1 -0
- package/dist/storage/sqlite.d.ts.map +1 -1
- package/dist/storage/sqlite.js +11 -0
- package/dist/storage/sqlite.js.map +1 -1
- package/package.json +1 -1
- package/src/cli.ts +16 -4
- package/src/core/salience.ts +168 -162
- package/src/engine/consolidation.ts +469 -445
- package/src/hooks/sidecar.ts +289 -289
- package/src/mcp.ts +971 -963
- package/src/storage/sqlite.ts +14 -0
package/src/mcp.ts
CHANGED
|
@@ -1,963 +1,971 @@
|
|
|
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 } 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.4.0' });
|
|
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 ?? '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.4.0',
|
|
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 — is this new information or a duplicate?
|
|
147
|
-
const novelty = computeNovelty(store, AGENT_ID, params.concept, params.content);
|
|
148
|
-
|
|
149
|
-
const salience = evaluateSalience({
|
|
150
|
-
content: params.content,
|
|
151
|
-
eventType: params.event_type as SalienceEventType,
|
|
152
|
-
surprise: params.surprise,
|
|
153
|
-
decisionMade: params.decision_made,
|
|
154
|
-
causalDepth: params.causal_depth,
|
|
155
|
-
resolutionEffort: params.resolution_effort,
|
|
156
|
-
novelty,
|
|
157
|
-
memoryClass: params.memory_class,
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
if (salience.disposition === 'discard') {
|
|
161
|
-
log(AGENT_ID, 'write:discard', `"${params.concept}" salience=${salience.score.toFixed(2)} novelty=${novelty.toFixed(1)}`);
|
|
162
|
-
return {
|
|
163
|
-
content: [{
|
|
164
|
-
type: 'text' as const,
|
|
165
|
-
text: `Discarded (salience ${salience.score.toFixed(2)}, novelty ${novelty.toFixed(1)})`,
|
|
166
|
-
}],
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const engram = store.createEngram({
|
|
171
|
-
agentId: AGENT_ID,
|
|
172
|
-
concept: params.concept,
|
|
173
|
-
content: params.content,
|
|
174
|
-
tags: params.tags,
|
|
175
|
-
salience: salience.score,
|
|
176
|
-
salienceFeatures: salience.features,
|
|
177
|
-
reasonCodes: salience.reasonCodes,
|
|
178
|
-
ttl: salience.disposition === 'staging' ? DEFAULT_AGENT_CONFIG.stagingTtlMs : undefined,
|
|
179
|
-
memoryClass: params.memory_class,
|
|
180
|
-
supersedes: params.supersedes,
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
if (salience.disposition === 'staging') {
|
|
184
|
-
store.updateStage(engram.id, 'staging');
|
|
185
|
-
} else {
|
|
186
|
-
connectionEngine.enqueue(engram.id);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Handle supersession: mark old memory as superseded
|
|
190
|
-
if (params.supersedes) {
|
|
191
|
-
const oldEngram = store.getEngram(params.supersedes);
|
|
192
|
-
if (oldEngram) {
|
|
193
|
-
store.supersedeEngram(params.supersedes, engram.id);
|
|
194
|
-
// Create supersession association
|
|
195
|
-
store.upsertAssociation(engram.id, oldEngram.id, 0.8, 'causal', 0.9);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Generate embedding asynchronously (don't block response)
|
|
200
|
-
embed(`${params.concept} ${params.content}`).then(vec => {
|
|
201
|
-
store.updateEmbedding(engram.id, vec);
|
|
202
|
-
}).catch(() => {}); // Embedding failure is non-fatal
|
|
203
|
-
|
|
204
|
-
// Auto-checkpoint: track write
|
|
205
|
-
try { store.updateAutoCheckpointWrite(AGENT_ID, engram.id); } catch { /* non-fatal */ }
|
|
206
|
-
|
|
207
|
-
log(AGENT_ID, `write:${salience.disposition}`, `"${params.concept}" salience=${salience.score.toFixed(2)} novelty=${novelty.toFixed(1)} id=${engram.id}`);
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
content: [{
|
|
211
|
-
type: 'text' as const,
|
|
212
|
-
text: `Stored (${salience.disposition}) "${params.concept}" [${salience.score.toFixed(2)}]`,
|
|
213
|
-
}],
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
);
|
|
217
|
-
|
|
218
|
-
server.tool(
|
|
219
|
-
'memory_recall',
|
|
220
|
-
`Recall memories relevant to a query. Uses cognitive activation — not keyword search.
|
|
221
|
-
|
|
222
|
-
ALWAYS call this when:
|
|
223
|
-
- Starting work on a project or topic (recall what you know)
|
|
224
|
-
- Debugging (recall similar errors and solutions)
|
|
225
|
-
- Making decisions (recall past decisions and outcomes)
|
|
226
|
-
- The user mentions a topic you might have stored memories about
|
|
227
|
-
|
|
228
|
-
Accepts either "query" or "context" parameter — both work identically.
|
|
229
|
-
Returns the most relevant memories ranked by text relevance, temporal recency, and associative strength.`,
|
|
230
|
-
{
|
|
231
|
-
query: z.string().optional().describe('What to search for — describe the situation, question, or topic'),
|
|
232
|
-
context: z.string().optional().describe('Alias for query (either works)'),
|
|
233
|
-
limit: z.number().optional().default(5).describe('Max memories to return (default 5)'),
|
|
234
|
-
min_score: z.number().optional().default(0.05).describe('Minimum relevance score (default 0.05)'),
|
|
235
|
-
include_staging: z.boolean().optional().default(false).describe('Include weak/unconfirmed memories?'),
|
|
236
|
-
use_reranker: z.boolean().optional().default(true).describe('Use cross-encoder re-ranking for better relevance (default true)'),
|
|
237
|
-
use_expansion: z.boolean().optional().default(true).describe('Expand query with synonyms for better recall (default true)'),
|
|
238
|
-
},
|
|
239
|
-
async (params) => {
|
|
240
|
-
const queryText = params.query ?? params.context;
|
|
241
|
-
if (!queryText) {
|
|
242
|
-
return {
|
|
243
|
-
content: [{
|
|
244
|
-
type: 'text' as const,
|
|
245
|
-
text: 'Error: provide either "query" or "context" parameter with your search text.',
|
|
246
|
-
}],
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
const results = await activationEngine.activate({
|
|
250
|
-
agentId: AGENT_ID,
|
|
251
|
-
context: queryText,
|
|
252
|
-
limit: params.limit,
|
|
253
|
-
minScore: params.min_score,
|
|
254
|
-
includeStaging: params.include_staging,
|
|
255
|
-
useReranker: params.use_reranker,
|
|
256
|
-
useExpansion: params.use_expansion,
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
// Auto-checkpoint: track recall
|
|
260
|
-
try {
|
|
261
|
-
const ids = results.map(r => r.engram.id);
|
|
262
|
-
store.updateAutoCheckpointRecall(AGENT_ID, queryText, ids);
|
|
263
|
-
} catch { /* non-fatal */ }
|
|
264
|
-
|
|
265
|
-
log(AGENT_ID, 'recall', `"${queryText.slice(0, 80)}" → ${results.length} results`);
|
|
266
|
-
|
|
267
|
-
if (results.length === 0) {
|
|
268
|
-
return {
|
|
269
|
-
content: [{
|
|
270
|
-
type: 'text' as const,
|
|
271
|
-
text: 'No relevant memories found.',
|
|
272
|
-
}],
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const lines = results.map((r, i) => {
|
|
277
|
-
return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)}): ${r.engram.content}`;
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
return {
|
|
281
|
-
content: [{
|
|
282
|
-
type: 'text' as const,
|
|
283
|
-
text: lines.join('\n'),
|
|
284
|
-
}],
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
);
|
|
288
|
-
|
|
289
|
-
server.tool(
|
|
290
|
-
'memory_feedback',
|
|
291
|
-
`Report whether a recalled memory was actually useful. This updates the memory's confidence score — useful memories become stronger, useless ones weaken.
|
|
292
|
-
|
|
293
|
-
Always call this after using a recalled memory so the system learns what's valuable.`,
|
|
294
|
-
{
|
|
295
|
-
engram_id: z.string().describe('ID of the memory (from memory_recall results)'),
|
|
296
|
-
useful: z.boolean().describe('Was this memory actually helpful?'),
|
|
297
|
-
context: z.string().optional().describe('Brief note on why it was/wasn\'t useful'),
|
|
298
|
-
},
|
|
299
|
-
async (params) => {
|
|
300
|
-
store.logRetrievalFeedback(null, params.engram_id, params.useful, params.context ?? '');
|
|
301
|
-
|
|
302
|
-
const engram = store.getEngram(params.engram_id);
|
|
303
|
-
if (engram) {
|
|
304
|
-
const delta = params.useful
|
|
305
|
-
? DEFAULT_AGENT_CONFIG.feedbackPositiveBoost
|
|
306
|
-
: -DEFAULT_AGENT_CONFIG.feedbackNegativePenalty;
|
|
307
|
-
store.updateConfidence(engram.id, engram.confidence + delta);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return {
|
|
311
|
-
content: [{
|
|
312
|
-
type: 'text' as const,
|
|
313
|
-
text: `Feedback: ${params.useful ? '+useful' : '-not useful'}`,
|
|
314
|
-
}],
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
);
|
|
318
|
-
|
|
319
|
-
server.tool(
|
|
320
|
-
'memory_retract',
|
|
321
|
-
`Retract a memory that turned out to be wrong. Creates a correction and reduces confidence of related memories.
|
|
322
|
-
|
|
323
|
-
Use this when you discover a memory contains incorrect information.`,
|
|
324
|
-
{
|
|
325
|
-
engram_id: z.string().describe('ID of the wrong memory'),
|
|
326
|
-
reason: z.string().describe('Why is this memory wrong?'),
|
|
327
|
-
correction: z.string().optional().describe('What is the correct information? (creates a new memory)'),
|
|
328
|
-
},
|
|
329
|
-
async (params) => {
|
|
330
|
-
const result = retractionEngine.retract({
|
|
331
|
-
agentId: AGENT_ID,
|
|
332
|
-
targetEngramId: params.engram_id,
|
|
333
|
-
reason: params.reason,
|
|
334
|
-
counterContent: params.correction,
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
const parts = [`Memory ${params.engram_id} retracted.`];
|
|
338
|
-
if (result.correctionId) {
|
|
339
|
-
parts.push(`Correction stored as ${result.correctionId}.`);
|
|
340
|
-
}
|
|
341
|
-
parts.push(`${result.associatesAffected} related memories had confidence reduced.`);
|
|
342
|
-
|
|
343
|
-
return {
|
|
344
|
-
content: [{
|
|
345
|
-
type: 'text' as const,
|
|
346
|
-
text: parts.join(' '),
|
|
347
|
-
}],
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
);
|
|
351
|
-
|
|
352
|
-
server.tool(
|
|
353
|
-
'memory_supersede',
|
|
354
|
-
`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.
|
|
355
|
-
|
|
356
|
-
Use this when:
|
|
357
|
-
- A status or count has changed (e.g., "5 reviews done" → "7 reviews done")
|
|
358
|
-
- Architecture or infrastructure evolved (e.g., "two-repo model" → "three-repo model")
|
|
359
|
-
- A schedule or plan was updated
|
|
360
|
-
|
|
361
|
-
The old memory stays in the database (searchable for history) but is heavily down-ranked in recall so the current version dominates.`,
|
|
362
|
-
{
|
|
363
|
-
old_engram_id: z.string().describe('ID of the outdated memory'),
|
|
364
|
-
new_engram_id: z.string().describe('ID of the replacement memory'),
|
|
365
|
-
reason: z.string().optional().describe('Why the old memory is outdated'),
|
|
366
|
-
},
|
|
367
|
-
async (params) => {
|
|
368
|
-
const oldEngram = store.getEngram(params.old_engram_id);
|
|
369
|
-
if (!oldEngram) {
|
|
370
|
-
return { content: [{ type: 'text' as const, text: `Old memory not found: ${params.old_engram_id}` }] };
|
|
371
|
-
}
|
|
372
|
-
const newEngram = store.getEngram(params.new_engram_id);
|
|
373
|
-
if (!newEngram) {
|
|
374
|
-
return { content: [{ type: 'text' as const, text: `New memory not found: ${params.new_engram_id}` }] };
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
store.supersedeEngram(params.old_engram_id, params.new_engram_id);
|
|
378
|
-
|
|
379
|
-
// Create supersession association (new → old)
|
|
380
|
-
store.upsertAssociation(params.new_engram_id, params.old_engram_id, 0.8, 'causal', 0.9);
|
|
381
|
-
|
|
382
|
-
// Reduce old memory's confidence (not to zero — it's historical, not wrong)
|
|
383
|
-
store.updateConfidence(params.old_engram_id, Math.max(0.2, oldEngram.confidence * 0.4));
|
|
384
|
-
|
|
385
|
-
log(AGENT_ID, 'supersede', `"${oldEngram.concept}" → "${newEngram.concept}"${params.reason ? ` (${params.reason})` : ''}`);
|
|
386
|
-
|
|
387
|
-
return {
|
|
388
|
-
content: [{
|
|
389
|
-
type: 'text' as const,
|
|
390
|
-
text: `Superseded: "${oldEngram.concept}" → "${newEngram.concept}"`,
|
|
391
|
-
}],
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
);
|
|
395
|
-
|
|
396
|
-
server.tool(
|
|
397
|
-
'memory_stats',
|
|
398
|
-
`Get memory health stats — how many memories, confidence levels, association count, and system performance.
|
|
399
|
-
Also shows the activity log path so the user can tail it to see what's happening.`,
|
|
400
|
-
{},
|
|
401
|
-
async () => {
|
|
402
|
-
const metrics = evalEngine.computeMetrics(AGENT_ID);
|
|
403
|
-
const checkpoint = store.getCheckpoint(AGENT_ID);
|
|
404
|
-
const lines = [
|
|
405
|
-
`Agent: ${AGENT_ID}`,
|
|
406
|
-
`Active memories: ${metrics.activeEngramCount}`,
|
|
407
|
-
`Staging: ${metrics.stagingEngramCount}`,
|
|
408
|
-
`Retracted: ${metrics.retractedCount}`,
|
|
409
|
-
`Avg confidence: ${metrics.avgConfidence.toFixed(3)}`,
|
|
410
|
-
`Total edges: ${metrics.totalEdges}`,
|
|
411
|
-
`Edge utility: ${(metrics.edgeUtilityRate * 100).toFixed(1)}%`,
|
|
412
|
-
`Activations (24h): ${metrics.activationCount}`,
|
|
413
|
-
`Avg latency: ${metrics.avgLatencyMs.toFixed(1)}ms`,
|
|
414
|
-
``,
|
|
415
|
-
`Session writes: ${checkpoint?.auto.writeCountSinceConsolidation ?? 0}`,
|
|
416
|
-
`Session recalls: ${checkpoint?.auto.recallCountSinceConsolidation ?? 0}`,
|
|
417
|
-
`Last activity: ${checkpoint?.auto.lastActivityAt?.toISOString() ?? 'never'}`,
|
|
418
|
-
`Checkpoint: ${checkpoint?.executionState ? checkpoint.executionState.currentTask : 'none'}`,
|
|
419
|
-
``,
|
|
420
|
-
`Activity log: ${getLogPath() ?? 'not configured'}`,
|
|
421
|
-
`Hook sidecar: 127.0.0.1:${HOOK_PORT}`,
|
|
422
|
-
];
|
|
423
|
-
|
|
424
|
-
return {
|
|
425
|
-
content: [{
|
|
426
|
-
type: 'text' as const,
|
|
427
|
-
text: lines.join('\n'),
|
|
428
|
-
}],
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
);
|
|
432
|
-
|
|
433
|
-
// --- Checkpointing Tools ---
|
|
434
|
-
|
|
435
|
-
server.tool(
|
|
436
|
-
'memory_checkpoint',
|
|
437
|
-
`Save your current execution state so you can recover after context compaction.
|
|
438
|
-
|
|
439
|
-
ALWAYS call this before:
|
|
440
|
-
- Long operations (multi-file generation, large refactors, overnight work)
|
|
441
|
-
- Anything that might fill the context window
|
|
442
|
-
- Switching to a different task
|
|
443
|
-
|
|
444
|
-
Also call periodically during long sessions to avoid losing state. The state is saved per-agent and overwrites any previous checkpoint.`,
|
|
445
|
-
{
|
|
446
|
-
current_task: z.string().describe('What you are currently working on'),
|
|
447
|
-
decisions: z.array(z.string()).optional().default([])
|
|
448
|
-
.describe('Key decisions made so far'),
|
|
449
|
-
active_files: z.array(z.string()).optional().default([])
|
|
450
|
-
.describe('Files you are currently working with'),
|
|
451
|
-
next_steps: z.array(z.string()).optional().default([])
|
|
452
|
-
.describe('What needs to happen next'),
|
|
453
|
-
related_memory_ids: z.array(z.string()).optional().default([])
|
|
454
|
-
.describe('IDs of memories relevant to current work'),
|
|
455
|
-
notes: z.string().optional().default('')
|
|
456
|
-
.describe('Any other context worth preserving'),
|
|
457
|
-
episode_id: z.string().optional()
|
|
458
|
-
.describe('Current episode ID if known'),
|
|
459
|
-
},
|
|
460
|
-
async (params) => {
|
|
461
|
-
const state: ConsciousState = {
|
|
462
|
-
currentTask: params.current_task,
|
|
463
|
-
decisions: params.decisions,
|
|
464
|
-
activeFiles: params.active_files,
|
|
465
|
-
nextSteps: params.next_steps,
|
|
466
|
-
relatedMemoryIds: params.related_memory_ids,
|
|
467
|
-
notes: params.notes,
|
|
468
|
-
episodeId: params.episode_id ?? null,
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
store.saveCheckpoint(AGENT_ID, state);
|
|
472
|
-
log(AGENT_ID, 'checkpoint', `"${params.current_task}" decisions=${params.decisions.length} files=${params.active_files.length}`);
|
|
473
|
-
|
|
474
|
-
return {
|
|
475
|
-
content: [{
|
|
476
|
-
type: 'text' as const,
|
|
477
|
-
text: `Checkpoint saved: "${params.current_task}" (${params.decisions.length} decisions, ${params.active_files.length} files)`,
|
|
478
|
-
}],
|
|
479
|
-
};
|
|
480
|
-
}
|
|
481
|
-
);
|
|
482
|
-
|
|
483
|
-
server.tool(
|
|
484
|
-
'memory_restore',
|
|
485
|
-
`Restore your previous execution state after context compaction or at session start.
|
|
486
|
-
|
|
487
|
-
Returns:
|
|
488
|
-
- Your saved execution state (task, decisions, next steps, files)
|
|
489
|
-
- Recently recalled memories for context
|
|
490
|
-
- Your last write for continuity
|
|
491
|
-
- How long you were idle
|
|
492
|
-
|
|
493
|
-
Use this at the start of every session or after compaction to pick up where you left off.`,
|
|
494
|
-
{},
|
|
495
|
-
async () => {
|
|
496
|
-
const checkpoint = store.getCheckpoint(AGENT_ID);
|
|
497
|
-
|
|
498
|
-
const now = Date.now();
|
|
499
|
-
const idleMs = checkpoint
|
|
500
|
-
? now - checkpoint.auto.lastActivityAt.getTime()
|
|
501
|
-
: 0;
|
|
502
|
-
|
|
503
|
-
// Get last written engram
|
|
504
|
-
let lastWrite: { id: string; concept: string; content: string } | null = null;
|
|
505
|
-
if (checkpoint?.auto.lastWriteId) {
|
|
506
|
-
const engram = store.getEngram(checkpoint.auto.lastWriteId);
|
|
507
|
-
if (engram) {
|
|
508
|
-
lastWrite = { id: engram.id, concept: engram.concept, content: engram.content };
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
// Recall memories using last context
|
|
513
|
-
let recalledMemories: Array<{ id: string; concept: string; content: string; score: number }> = [];
|
|
514
|
-
const recallContext = checkpoint?.auto.lastRecallContext
|
|
515
|
-
?? checkpoint?.executionState?.currentTask
|
|
516
|
-
?? null;
|
|
517
|
-
|
|
518
|
-
if (recallContext) {
|
|
519
|
-
try {
|
|
520
|
-
const results = await activationEngine.activate({
|
|
521
|
-
agentId: AGENT_ID,
|
|
522
|
-
context: recallContext,
|
|
523
|
-
limit: 5,
|
|
524
|
-
minScore: 0.05,
|
|
525
|
-
useReranker: true,
|
|
526
|
-
useExpansion: true,
|
|
527
|
-
});
|
|
528
|
-
recalledMemories = results.map(r => ({
|
|
529
|
-
id: r.engram.id,
|
|
530
|
-
concept: r.engram.concept,
|
|
531
|
-
content: r.engram.content,
|
|
532
|
-
score: r.score,
|
|
533
|
-
}));
|
|
534
|
-
} catch { /* recall failure is non-fatal */ }
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
// Consolidation on restore:
|
|
538
|
-
// - If idle >5min but last consolidation was recent (graceful exit ran it), skip
|
|
539
|
-
// - If idle >5min and no recent consolidation, run full cycle (non-graceful exit fallback)
|
|
540
|
-
const MINI_IDLE_MS = 5 * 60_000;
|
|
541
|
-
const FULL_CONSOLIDATION_GAP_MS = 10 * 60_000; // 10 min — if last consolidation was longer ago, run full
|
|
542
|
-
let miniConsolidationTriggered = false;
|
|
543
|
-
let fullConsolidationTriggered = false;
|
|
544
|
-
|
|
545
|
-
if (idleMs > MINI_IDLE_MS) {
|
|
546
|
-
const sinceLastConsolidation = checkpoint?.lastConsolidationAt
|
|
547
|
-
? now - checkpoint.lastConsolidationAt.getTime()
|
|
548
|
-
: Infinity;
|
|
549
|
-
|
|
550
|
-
if (sinceLastConsolidation > FULL_CONSOLIDATION_GAP_MS) {
|
|
551
|
-
// No recent consolidation — graceful exit didn't happen, run full cycle
|
|
552
|
-
fullConsolidationTriggered = true;
|
|
553
|
-
try {
|
|
554
|
-
const result = consolidationEngine.consolidate(AGENT_ID);
|
|
555
|
-
store.markConsolidation(AGENT_ID, false);
|
|
556
|
-
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`);
|
|
557
|
-
} catch { /* consolidation failure is non-fatal */ }
|
|
558
|
-
} else {
|
|
559
|
-
// Recent consolidation exists — graceful exit already handled it, just do mini
|
|
560
|
-
miniConsolidationTriggered = true;
|
|
561
|
-
consolidationScheduler.runMiniConsolidation(AGENT_ID).catch(() => {});
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
// Format response
|
|
566
|
-
const parts: string[] = [];
|
|
567
|
-
const idleMin = Math.round(idleMs / 60_000);
|
|
568
|
-
const consolidationNote = fullConsolidationTriggered
|
|
569
|
-
? ' (full consolidation — no graceful exit detected)'
|
|
570
|
-
: miniConsolidationTriggered
|
|
571
|
-
? ' (mini-consolidation triggered)'
|
|
572
|
-
: '';
|
|
573
|
-
log(AGENT_ID, 'restore', `idle=${idleMin}min checkpoint=${!!checkpoint?.executionState} recalled=${recalledMemories.length} lastWrite=${lastWrite?.concept ?? 'none'}${fullConsolidationTriggered ? ' FULL_CONSOLIDATION' : ''}`);
|
|
574
|
-
parts.push(`Idle: ${idleMin}min${consolidationNote}`);
|
|
575
|
-
|
|
576
|
-
if (checkpoint?.executionState) {
|
|
577
|
-
const s = checkpoint.executionState;
|
|
578
|
-
parts.push(`\n**Current task:** ${s.currentTask}`);
|
|
579
|
-
if (s.decisions.length) parts.push(`**Decisions:** ${s.decisions.join('; ')}`);
|
|
580
|
-
if (s.nextSteps.length) parts.push(`**Next steps:** ${s.nextSteps.map((st, i) => `${i + 1}. ${st}`).join(', ')}`);
|
|
581
|
-
if (s.activeFiles.length) parts.push(`**Active files:** ${s.activeFiles.join(', ')}`);
|
|
582
|
-
if (s.notes) parts.push(`**Notes:** ${s.notes}`);
|
|
583
|
-
if (checkpoint.checkpointAt) parts.push(`_Saved at: ${checkpoint.checkpointAt.toISOString()}_`);
|
|
584
|
-
} else {
|
|
585
|
-
parts.push('\nNo explicit checkpoint saved.');
|
|
586
|
-
parts.push('\n**Tip:** Use memory_write to save important learnings, and memory_checkpoint before long operations so you can recover state.');
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
if (lastWrite) {
|
|
590
|
-
parts.push(`\n**Last write:** ${lastWrite.concept}\n${lastWrite.content}`);
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
if (recalledMemories.length > 0) {
|
|
594
|
-
parts.push(`\n**Recalled memories (${recalledMemories.length}):**`);
|
|
595
|
-
for (const m of recalledMemories) {
|
|
596
|
-
parts.push(`- **${m.concept}** (${m.score.toFixed(3)}): ${m.content.slice(0, 150)}${m.content.length > 150 ? '...' : ''}`);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
return {
|
|
601
|
-
content: [{
|
|
602
|
-
type: 'text' as const,
|
|
603
|
-
text: parts.join('\n'),
|
|
604
|
-
}],
|
|
605
|
-
};
|
|
606
|
-
}
|
|
607
|
-
);
|
|
608
|
-
|
|
609
|
-
// --- Task Management Tools ---
|
|
610
|
-
|
|
611
|
-
server.tool(
|
|
612
|
-
'memory_task_add',
|
|
613
|
-
`Create a task that you need to come back to. Tasks are memories with status and priority tracking.
|
|
614
|
-
|
|
615
|
-
Use this when:
|
|
616
|
-
- You identify work that needs doing but can't do it right now
|
|
617
|
-
- The user mentions something to do later
|
|
618
|
-
- You want to park a sub-task while focusing on something more urgent
|
|
619
|
-
|
|
620
|
-
Tasks automatically get high salience so they won't be discarded.`,
|
|
621
|
-
{
|
|
622
|
-
concept: z.string().describe('Short task title (3-10 words)'),
|
|
623
|
-
content: z.string().describe('Full task description — what needs doing, context, acceptance criteria'),
|
|
624
|
-
tags: z.array(z.string()).optional().describe('Tags for categorization'),
|
|
625
|
-
priority: z.enum(['urgent', 'high', 'medium', 'low']).default('medium')
|
|
626
|
-
.describe('Task priority: urgent (do now), high (do soon), medium (normal), low (backlog)'),
|
|
627
|
-
blocked_by: z.string().optional().describe('ID of a task that must finish first'),
|
|
628
|
-
},
|
|
629
|
-
async (params) => {
|
|
630
|
-
const engram = store.createEngram({
|
|
631
|
-
agentId: AGENT_ID,
|
|
632
|
-
concept: params.concept,
|
|
633
|
-
content: params.content,
|
|
634
|
-
tags: [...(params.tags ?? []), 'task'],
|
|
635
|
-
salience: 0.9, // Tasks always high salience
|
|
636
|
-
confidence: 0.8,
|
|
637
|
-
salienceFeatures: {
|
|
638
|
-
surprise: 0.5,
|
|
639
|
-
decisionMade: true,
|
|
640
|
-
causalDepth: 0.5,
|
|
641
|
-
resolutionEffort: 0.5,
|
|
642
|
-
eventType: 'decision',
|
|
643
|
-
},
|
|
644
|
-
reasonCodes: ['task-created'],
|
|
645
|
-
taskStatus: params.blocked_by ? 'blocked' : 'open',
|
|
646
|
-
taskPriority: params.priority as TaskPriority,
|
|
647
|
-
blockedBy: params.blocked_by,
|
|
648
|
-
});
|
|
649
|
-
|
|
650
|
-
connectionEngine.enqueue(engram.id);
|
|
651
|
-
|
|
652
|
-
// Generate embedding asynchronously
|
|
653
|
-
embed(`${params.concept} ${params.content}`).then(vec => {
|
|
654
|
-
store.updateEmbedding(engram.id, vec);
|
|
655
|
-
}).catch(() => {});
|
|
656
|
-
|
|
657
|
-
return {
|
|
658
|
-
content: [{
|
|
659
|
-
type: 'text' as const,
|
|
660
|
-
text: `Task created: "${params.concept}" (${params.priority})`,
|
|
661
|
-
}],
|
|
662
|
-
};
|
|
663
|
-
}
|
|
664
|
-
);
|
|
665
|
-
|
|
666
|
-
server.tool(
|
|
667
|
-
'memory_task_update',
|
|
668
|
-
`Update a task's status or priority. Use this to:
|
|
669
|
-
- Start working on a task (open → in_progress)
|
|
670
|
-
- Mark a task done (→ done)
|
|
671
|
-
- Block a task on another (→ blocked)
|
|
672
|
-
- Reprioritize (change priority)
|
|
673
|
-
- Unblock a task (clear blocked_by)`,
|
|
674
|
-
{
|
|
675
|
-
task_id: z.string().describe('ID of the task to update'),
|
|
676
|
-
status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
|
|
677
|
-
.describe('New status'),
|
|
678
|
-
priority: z.enum(['urgent', 'high', 'medium', 'low']).optional()
|
|
679
|
-
.describe('New priority'),
|
|
680
|
-
blocked_by: z.string().optional().describe('ID of blocking task (set to empty string to unblock)'),
|
|
681
|
-
},
|
|
682
|
-
async (params) => {
|
|
683
|
-
const engram = store.getEngram(params.task_id);
|
|
684
|
-
if (!engram || !engram.taskStatus) {
|
|
685
|
-
return { content: [{ type: 'text' as const, text: `Task not found: ${params.task_id}` }] };
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
if (params.blocked_by !== undefined) {
|
|
689
|
-
store.updateBlockedBy(params.task_id, params.blocked_by || null);
|
|
690
|
-
}
|
|
691
|
-
if (params.status) {
|
|
692
|
-
store.updateTaskStatus(params.task_id, params.status as TaskStatus);
|
|
693
|
-
}
|
|
694
|
-
if (params.priority) {
|
|
695
|
-
store.updateTaskPriority(params.task_id, params.priority as TaskPriority);
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
const updated = store.getEngram(params.task_id)!;
|
|
699
|
-
return {
|
|
700
|
-
content: [{
|
|
701
|
-
type: 'text' as const,
|
|
702
|
-
text: `Updated: "${updated.concept}" → ${updated.taskStatus} (${updated.taskPriority})`,
|
|
703
|
-
}],
|
|
704
|
-
};
|
|
705
|
-
}
|
|
706
|
-
);
|
|
707
|
-
|
|
708
|
-
server.tool(
|
|
709
|
-
'memory_task_list',
|
|
710
|
-
`List tasks with optional status filter. Shows tasks ordered by priority (urgent first).
|
|
711
|
-
|
|
712
|
-
Use at the start of a session to see what's pending, or to check blocked/done tasks.`,
|
|
713
|
-
{
|
|
714
|
-
status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
|
|
715
|
-
.describe('Filter by status (omit to see all active tasks)'),
|
|
716
|
-
include_done: z.boolean().optional().default(false)
|
|
717
|
-
.describe('Include completed tasks?'),
|
|
718
|
-
},
|
|
719
|
-
async (params) => {
|
|
720
|
-
let tasks = store.getTasks(AGENT_ID, params.status as TaskStatus | undefined);
|
|
721
|
-
if (!params.include_done && !params.status) {
|
|
722
|
-
tasks = tasks.filter(t => t.taskStatus !== 'done');
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
if (tasks.length === 0) {
|
|
726
|
-
return { content: [{ type: 'text' as const, text: 'No tasks found.' }] };
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
const lines = tasks.map((t, i) => {
|
|
730
|
-
const blocked = t.blockedBy ? ` [blocked by ${t.blockedBy}]` : '';
|
|
731
|
-
const tags = t.tags?.filter(tag => tag !== 'task').join(', ');
|
|
732
|
-
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}`;
|
|
733
|
-
});
|
|
734
|
-
|
|
735
|
-
return {
|
|
736
|
-
content: [{
|
|
737
|
-
type: 'text' as const,
|
|
738
|
-
text: `Tasks (${tasks.length}):\n\n${lines.join('\n\n')}`,
|
|
739
|
-
}],
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
);
|
|
743
|
-
|
|
744
|
-
server.tool(
|
|
745
|
-
'memory_task_next',
|
|
746
|
-
`Get the single most important task to work on next.
|
|
747
|
-
|
|
748
|
-
Prioritizes: in_progress tasks first (finish what you started), then by priority level, then oldest first. Skips blocked and done tasks.
|
|
749
|
-
|
|
750
|
-
Use this when you finish a task or need to decide what to do next.`,
|
|
751
|
-
{},
|
|
752
|
-
async () => {
|
|
753
|
-
const next = store.getNextTask(AGENT_ID);
|
|
754
|
-
if (!next) {
|
|
755
|
-
return { content: [{ type: 'text' as const, text: 'No actionable tasks. All clear!' }] };
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
const blocked = next.blockedBy ? `\nBlocked by: ${next.blockedBy}` : '';
|
|
759
|
-
const tags = next.tags?.filter(tag => tag !== 'task').join(', ');
|
|
760
|
-
|
|
761
|
-
return {
|
|
762
|
-
content: [{
|
|
763
|
-
type: 'text' as const,
|
|
764
|
-
text: `Next task:\n**${next.concept}** (${next.taskPriority})\nStatus: ${next.taskStatus}\n${next.content}${blocked}\n${tags ? `Tags: ${tags}\n` : ''}ID: ${next.id}`,
|
|
765
|
-
}],
|
|
766
|
-
};
|
|
767
|
-
}
|
|
768
|
-
);
|
|
769
|
-
|
|
770
|
-
// --- Task Bracket Tools ---
|
|
771
|
-
|
|
772
|
-
server.tool(
|
|
773
|
-
'memory_task_begin',
|
|
774
|
-
`Signal that you're starting a significant task. Auto-checkpoints current state and recalls relevant memories.
|
|
775
|
-
|
|
776
|
-
CALL THIS when starting:
|
|
777
|
-
- A multi-step operation (doc generation, large refactor, migration)
|
|
778
|
-
- Work on a new topic or project area
|
|
779
|
-
- Anything that might fill the context window
|
|
780
|
-
|
|
781
|
-
This ensures your state is saved before you start, and primes recall with relevant context.`,
|
|
782
|
-
{
|
|
783
|
-
topic: z.string().describe('What task are you starting? (3-15 words)'),
|
|
784
|
-
files: z.array(z.string()).optional().default([])
|
|
785
|
-
.describe('Files you expect to work with'),
|
|
786
|
-
notes: z.string().optional().default('')
|
|
787
|
-
.describe('Any additional context'),
|
|
788
|
-
},
|
|
789
|
-
async (params) => {
|
|
790
|
-
// 1. Checkpoint current state
|
|
791
|
-
const checkpoint = store.getCheckpoint(AGENT_ID);
|
|
792
|
-
const prevTask = checkpoint?.executionState?.currentTask ?? 'None';
|
|
793
|
-
|
|
794
|
-
store.saveCheckpoint(AGENT_ID, {
|
|
795
|
-
currentTask: params.topic,
|
|
796
|
-
decisions: [],
|
|
797
|
-
activeFiles: params.files,
|
|
798
|
-
nextSteps: [],
|
|
799
|
-
relatedMemoryIds: [],
|
|
800
|
-
notes: params.notes || `Started via memory_task_begin. Previous task: ${prevTask}`,
|
|
801
|
-
episodeId: null,
|
|
802
|
-
});
|
|
803
|
-
|
|
804
|
-
// 2. Auto-recall relevant memories
|
|
805
|
-
let recalledSummary = '';
|
|
806
|
-
try {
|
|
807
|
-
const results = await activationEngine.activate({
|
|
808
|
-
agentId: AGENT_ID,
|
|
809
|
-
context: params.topic,
|
|
810
|
-
limit: 5,
|
|
811
|
-
minScore: 0.05,
|
|
812
|
-
useReranker: true,
|
|
813
|
-
useExpansion: true,
|
|
814
|
-
});
|
|
815
|
-
|
|
816
|
-
if (results.length > 0) {
|
|
817
|
-
const lines = results.map((r, i) => {
|
|
818
|
-
const tags = r.engram.tags?.length ? ` [${r.engram.tags.join(', ')}]` : '';
|
|
819
|
-
return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)})${tags}\n ${r.engram.content.slice(0, 150)}${r.engram.content.length > 150 ? '...' : ''}`;
|
|
820
|
-
});
|
|
821
|
-
recalledSummary = `\n\n**Recalled memories (${results.length}):**\n${lines.join('\n')}`;
|
|
822
|
-
|
|
823
|
-
// Track recall
|
|
824
|
-
store.updateAutoCheckpointRecall(AGENT_ID, params.topic, results.map(r => r.engram.id));
|
|
825
|
-
}
|
|
826
|
-
} catch { /* recall failure is non-fatal */ }
|
|
827
|
-
|
|
828
|
-
log(AGENT_ID, 'task:begin', `"${params.topic}" prev="${prevTask}"`);
|
|
829
|
-
|
|
830
|
-
return {
|
|
831
|
-
content: [{
|
|
832
|
-
type: 'text' as const,
|
|
833
|
-
text: `Started: "${params.topic}" (prev: ${prevTask})${recalledSummary}`,
|
|
834
|
-
}],
|
|
835
|
-
};
|
|
836
|
-
}
|
|
837
|
-
);
|
|
838
|
-
|
|
839
|
-
server.tool(
|
|
840
|
-
'memory_task_end',
|
|
841
|
-
`Signal that you've finished a significant task. Writes a summary memory and auto-checkpoints.
|
|
842
|
-
|
|
843
|
-
CALL THIS when you finish:
|
|
844
|
-
- A multi-step operation
|
|
845
|
-
- Before switching to a different topic
|
|
846
|
-
- At the end of a work session
|
|
847
|
-
|
|
848
|
-
This captures what was accomplished so future sessions can recall it.`,
|
|
849
|
-
{
|
|
850
|
-
summary: z.string().describe('What was accomplished? Include key outcomes, decisions, and any issues.'),
|
|
851
|
-
tags: z.array(z.string()).optional().default([])
|
|
852
|
-
.describe('Tags for the summary memory'),
|
|
853
|
-
supersedes: z.array(z.string()).optional().default([])
|
|
854
|
-
.describe('IDs of older memories this task summary replaces (marks them as superseded)'),
|
|
855
|
-
},
|
|
856
|
-
async (params) => {
|
|
857
|
-
// 1. Write summary as a memory
|
|
858
|
-
const salience = evaluateSalience({
|
|
859
|
-
content: params.summary,
|
|
860
|
-
eventType: 'decision',
|
|
861
|
-
surprise: 0.3,
|
|
862
|
-
decisionMade: true,
|
|
863
|
-
causalDepth: 0.5,
|
|
864
|
-
resolutionEffort: 0.5,
|
|
865
|
-
});
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
store.
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
};
|
|
920
|
-
|
|
921
|
-
);
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
};
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
|
|
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 } 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.4.0' });
|
|
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.4.0',
|
|
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 — is this new information or a duplicate?
|
|
147
|
+
const novelty = computeNovelty(store, AGENT_ID, params.concept, params.content);
|
|
148
|
+
|
|
149
|
+
const salience = evaluateSalience({
|
|
150
|
+
content: params.content,
|
|
151
|
+
eventType: params.event_type as SalienceEventType,
|
|
152
|
+
surprise: params.surprise,
|
|
153
|
+
decisionMade: params.decision_made,
|
|
154
|
+
causalDepth: params.causal_depth,
|
|
155
|
+
resolutionEffort: params.resolution_effort,
|
|
156
|
+
novelty,
|
|
157
|
+
memoryClass: params.memory_class,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
if (salience.disposition === 'discard') {
|
|
161
|
+
log(AGENT_ID, 'write:discard', `"${params.concept}" salience=${salience.score.toFixed(2)} novelty=${novelty.toFixed(1)}`);
|
|
162
|
+
return {
|
|
163
|
+
content: [{
|
|
164
|
+
type: 'text' as const,
|
|
165
|
+
text: `Discarded (salience ${salience.score.toFixed(2)}, novelty ${novelty.toFixed(1)})`,
|
|
166
|
+
}],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const engram = store.createEngram({
|
|
171
|
+
agentId: AGENT_ID,
|
|
172
|
+
concept: params.concept,
|
|
173
|
+
content: params.content,
|
|
174
|
+
tags: params.tags,
|
|
175
|
+
salience: salience.score,
|
|
176
|
+
salienceFeatures: salience.features,
|
|
177
|
+
reasonCodes: salience.reasonCodes,
|
|
178
|
+
ttl: salience.disposition === 'staging' ? DEFAULT_AGENT_CONFIG.stagingTtlMs : undefined,
|
|
179
|
+
memoryClass: params.memory_class,
|
|
180
|
+
supersedes: params.supersedes,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (salience.disposition === 'staging') {
|
|
184
|
+
store.updateStage(engram.id, 'staging');
|
|
185
|
+
} else {
|
|
186
|
+
connectionEngine.enqueue(engram.id);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Handle supersession: mark old memory as superseded
|
|
190
|
+
if (params.supersedes) {
|
|
191
|
+
const oldEngram = store.getEngram(params.supersedes);
|
|
192
|
+
if (oldEngram) {
|
|
193
|
+
store.supersedeEngram(params.supersedes, engram.id);
|
|
194
|
+
// Create supersession association
|
|
195
|
+
store.upsertAssociation(engram.id, oldEngram.id, 0.8, 'causal', 0.9);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Generate embedding asynchronously (don't block response)
|
|
200
|
+
embed(`${params.concept} ${params.content}`).then(vec => {
|
|
201
|
+
store.updateEmbedding(engram.id, vec);
|
|
202
|
+
}).catch(() => {}); // Embedding failure is non-fatal
|
|
203
|
+
|
|
204
|
+
// Auto-checkpoint: track write
|
|
205
|
+
try { store.updateAutoCheckpointWrite(AGENT_ID, engram.id); } catch { /* non-fatal */ }
|
|
206
|
+
|
|
207
|
+
log(AGENT_ID, `write:${salience.disposition}`, `"${params.concept}" salience=${salience.score.toFixed(2)} novelty=${novelty.toFixed(1)} id=${engram.id}`);
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
content: [{
|
|
211
|
+
type: 'text' as const,
|
|
212
|
+
text: `Stored (${salience.disposition}) "${params.concept}" [${salience.score.toFixed(2)}]`,
|
|
213
|
+
}],
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
server.tool(
|
|
219
|
+
'memory_recall',
|
|
220
|
+
`Recall memories relevant to a query. Uses cognitive activation — not keyword search.
|
|
221
|
+
|
|
222
|
+
ALWAYS call this when:
|
|
223
|
+
- Starting work on a project or topic (recall what you know)
|
|
224
|
+
- Debugging (recall similar errors and solutions)
|
|
225
|
+
- Making decisions (recall past decisions and outcomes)
|
|
226
|
+
- The user mentions a topic you might have stored memories about
|
|
227
|
+
|
|
228
|
+
Accepts either "query" or "context" parameter — both work identically.
|
|
229
|
+
Returns the most relevant memories ranked by text relevance, temporal recency, and associative strength.`,
|
|
230
|
+
{
|
|
231
|
+
query: z.string().optional().describe('What to search for — describe the situation, question, or topic'),
|
|
232
|
+
context: z.string().optional().describe('Alias for query (either works)'),
|
|
233
|
+
limit: z.number().optional().default(5).describe('Max memories to return (default 5)'),
|
|
234
|
+
min_score: z.number().optional().default(0.05).describe('Minimum relevance score (default 0.05)'),
|
|
235
|
+
include_staging: z.boolean().optional().default(false).describe('Include weak/unconfirmed memories?'),
|
|
236
|
+
use_reranker: z.boolean().optional().default(true).describe('Use cross-encoder re-ranking for better relevance (default true)'),
|
|
237
|
+
use_expansion: z.boolean().optional().default(true).describe('Expand query with synonyms for better recall (default true)'),
|
|
238
|
+
},
|
|
239
|
+
async (params) => {
|
|
240
|
+
const queryText = params.query ?? params.context;
|
|
241
|
+
if (!queryText) {
|
|
242
|
+
return {
|
|
243
|
+
content: [{
|
|
244
|
+
type: 'text' as const,
|
|
245
|
+
text: 'Error: provide either "query" or "context" parameter with your search text.',
|
|
246
|
+
}],
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
const results = await activationEngine.activate({
|
|
250
|
+
agentId: AGENT_ID,
|
|
251
|
+
context: queryText,
|
|
252
|
+
limit: params.limit,
|
|
253
|
+
minScore: params.min_score,
|
|
254
|
+
includeStaging: params.include_staging,
|
|
255
|
+
useReranker: params.use_reranker,
|
|
256
|
+
useExpansion: params.use_expansion,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Auto-checkpoint: track recall
|
|
260
|
+
try {
|
|
261
|
+
const ids = results.map(r => r.engram.id);
|
|
262
|
+
store.updateAutoCheckpointRecall(AGENT_ID, queryText, ids);
|
|
263
|
+
} catch { /* non-fatal */ }
|
|
264
|
+
|
|
265
|
+
log(AGENT_ID, 'recall', `"${queryText.slice(0, 80)}" → ${results.length} results`);
|
|
266
|
+
|
|
267
|
+
if (results.length === 0) {
|
|
268
|
+
return {
|
|
269
|
+
content: [{
|
|
270
|
+
type: 'text' as const,
|
|
271
|
+
text: 'No relevant memories found.',
|
|
272
|
+
}],
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const lines = results.map((r, i) => {
|
|
277
|
+
return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)}): ${r.engram.content}`;
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
content: [{
|
|
282
|
+
type: 'text' as const,
|
|
283
|
+
text: lines.join('\n'),
|
|
284
|
+
}],
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
server.tool(
|
|
290
|
+
'memory_feedback',
|
|
291
|
+
`Report whether a recalled memory was actually useful. This updates the memory's confidence score — useful memories become stronger, useless ones weaken.
|
|
292
|
+
|
|
293
|
+
Always call this after using a recalled memory so the system learns what's valuable.`,
|
|
294
|
+
{
|
|
295
|
+
engram_id: z.string().describe('ID of the memory (from memory_recall results)'),
|
|
296
|
+
useful: z.boolean().describe('Was this memory actually helpful?'),
|
|
297
|
+
context: z.string().optional().describe('Brief note on why it was/wasn\'t useful'),
|
|
298
|
+
},
|
|
299
|
+
async (params) => {
|
|
300
|
+
store.logRetrievalFeedback(null, params.engram_id, params.useful, params.context ?? '');
|
|
301
|
+
|
|
302
|
+
const engram = store.getEngram(params.engram_id);
|
|
303
|
+
if (engram) {
|
|
304
|
+
const delta = params.useful
|
|
305
|
+
? DEFAULT_AGENT_CONFIG.feedbackPositiveBoost
|
|
306
|
+
: -DEFAULT_AGENT_CONFIG.feedbackNegativePenalty;
|
|
307
|
+
store.updateConfidence(engram.id, engram.confidence + delta);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
content: [{
|
|
312
|
+
type: 'text' as const,
|
|
313
|
+
text: `Feedback: ${params.useful ? '+useful' : '-not useful'}`,
|
|
314
|
+
}],
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
server.tool(
|
|
320
|
+
'memory_retract',
|
|
321
|
+
`Retract a memory that turned out to be wrong. Creates a correction and reduces confidence of related memories.
|
|
322
|
+
|
|
323
|
+
Use this when you discover a memory contains incorrect information.`,
|
|
324
|
+
{
|
|
325
|
+
engram_id: z.string().describe('ID of the wrong memory'),
|
|
326
|
+
reason: z.string().describe('Why is this memory wrong?'),
|
|
327
|
+
correction: z.string().optional().describe('What is the correct information? (creates a new memory)'),
|
|
328
|
+
},
|
|
329
|
+
async (params) => {
|
|
330
|
+
const result = retractionEngine.retract({
|
|
331
|
+
agentId: AGENT_ID,
|
|
332
|
+
targetEngramId: params.engram_id,
|
|
333
|
+
reason: params.reason,
|
|
334
|
+
counterContent: params.correction,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const parts = [`Memory ${params.engram_id} retracted.`];
|
|
338
|
+
if (result.correctionId) {
|
|
339
|
+
parts.push(`Correction stored as ${result.correctionId}.`);
|
|
340
|
+
}
|
|
341
|
+
parts.push(`${result.associatesAffected} related memories had confidence reduced.`);
|
|
342
|
+
|
|
343
|
+
return {
|
|
344
|
+
content: [{
|
|
345
|
+
type: 'text' as const,
|
|
346
|
+
text: parts.join(' '),
|
|
347
|
+
}],
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
server.tool(
|
|
353
|
+
'memory_supersede',
|
|
354
|
+
`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.
|
|
355
|
+
|
|
356
|
+
Use this when:
|
|
357
|
+
- A status or count has changed (e.g., "5 reviews done" → "7 reviews done")
|
|
358
|
+
- Architecture or infrastructure evolved (e.g., "two-repo model" → "three-repo model")
|
|
359
|
+
- A schedule or plan was updated
|
|
360
|
+
|
|
361
|
+
The old memory stays in the database (searchable for history) but is heavily down-ranked in recall so the current version dominates.`,
|
|
362
|
+
{
|
|
363
|
+
old_engram_id: z.string().describe('ID of the outdated memory'),
|
|
364
|
+
new_engram_id: z.string().describe('ID of the replacement memory'),
|
|
365
|
+
reason: z.string().optional().describe('Why the old memory is outdated'),
|
|
366
|
+
},
|
|
367
|
+
async (params) => {
|
|
368
|
+
const oldEngram = store.getEngram(params.old_engram_id);
|
|
369
|
+
if (!oldEngram) {
|
|
370
|
+
return { content: [{ type: 'text' as const, text: `Old memory not found: ${params.old_engram_id}` }] };
|
|
371
|
+
}
|
|
372
|
+
const newEngram = store.getEngram(params.new_engram_id);
|
|
373
|
+
if (!newEngram) {
|
|
374
|
+
return { content: [{ type: 'text' as const, text: `New memory not found: ${params.new_engram_id}` }] };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
store.supersedeEngram(params.old_engram_id, params.new_engram_id);
|
|
378
|
+
|
|
379
|
+
// Create supersession association (new → old)
|
|
380
|
+
store.upsertAssociation(params.new_engram_id, params.old_engram_id, 0.8, 'causal', 0.9);
|
|
381
|
+
|
|
382
|
+
// Reduce old memory's confidence (not to zero — it's historical, not wrong)
|
|
383
|
+
store.updateConfidence(params.old_engram_id, Math.max(0.2, oldEngram.confidence * 0.4));
|
|
384
|
+
|
|
385
|
+
log(AGENT_ID, 'supersede', `"${oldEngram.concept}" → "${newEngram.concept}"${params.reason ? ` (${params.reason})` : ''}`);
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
content: [{
|
|
389
|
+
type: 'text' as const,
|
|
390
|
+
text: `Superseded: "${oldEngram.concept}" → "${newEngram.concept}"`,
|
|
391
|
+
}],
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
server.tool(
|
|
397
|
+
'memory_stats',
|
|
398
|
+
`Get memory health stats — how many memories, confidence levels, association count, and system performance.
|
|
399
|
+
Also shows the activity log path so the user can tail it to see what's happening.`,
|
|
400
|
+
{},
|
|
401
|
+
async () => {
|
|
402
|
+
const metrics = evalEngine.computeMetrics(AGENT_ID);
|
|
403
|
+
const checkpoint = store.getCheckpoint(AGENT_ID);
|
|
404
|
+
const lines = [
|
|
405
|
+
`Agent: ${AGENT_ID}`,
|
|
406
|
+
`Active memories: ${metrics.activeEngramCount}`,
|
|
407
|
+
`Staging: ${metrics.stagingEngramCount}`,
|
|
408
|
+
`Retracted: ${metrics.retractedCount}`,
|
|
409
|
+
`Avg confidence: ${metrics.avgConfidence.toFixed(3)}`,
|
|
410
|
+
`Total edges: ${metrics.totalEdges}`,
|
|
411
|
+
`Edge utility: ${(metrics.edgeUtilityRate * 100).toFixed(1)}%`,
|
|
412
|
+
`Activations (24h): ${metrics.activationCount}`,
|
|
413
|
+
`Avg latency: ${metrics.avgLatencyMs.toFixed(1)}ms`,
|
|
414
|
+
``,
|
|
415
|
+
`Session writes: ${checkpoint?.auto.writeCountSinceConsolidation ?? 0}`,
|
|
416
|
+
`Session recalls: ${checkpoint?.auto.recallCountSinceConsolidation ?? 0}`,
|
|
417
|
+
`Last activity: ${checkpoint?.auto.lastActivityAt?.toISOString() ?? 'never'}`,
|
|
418
|
+
`Checkpoint: ${checkpoint?.executionState ? checkpoint.executionState.currentTask : 'none'}`,
|
|
419
|
+
``,
|
|
420
|
+
`Activity log: ${getLogPath() ?? 'not configured'}`,
|
|
421
|
+
`Hook sidecar: 127.0.0.1:${HOOK_PORT}`,
|
|
422
|
+
];
|
|
423
|
+
|
|
424
|
+
return {
|
|
425
|
+
content: [{
|
|
426
|
+
type: 'text' as const,
|
|
427
|
+
text: lines.join('\n'),
|
|
428
|
+
}],
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
// --- Checkpointing Tools ---
|
|
434
|
+
|
|
435
|
+
server.tool(
|
|
436
|
+
'memory_checkpoint',
|
|
437
|
+
`Save your current execution state so you can recover after context compaction.
|
|
438
|
+
|
|
439
|
+
ALWAYS call this before:
|
|
440
|
+
- Long operations (multi-file generation, large refactors, overnight work)
|
|
441
|
+
- Anything that might fill the context window
|
|
442
|
+
- Switching to a different task
|
|
443
|
+
|
|
444
|
+
Also call periodically during long sessions to avoid losing state. The state is saved per-agent and overwrites any previous checkpoint.`,
|
|
445
|
+
{
|
|
446
|
+
current_task: z.string().describe('What you are currently working on'),
|
|
447
|
+
decisions: z.array(z.string()).optional().default([])
|
|
448
|
+
.describe('Key decisions made so far'),
|
|
449
|
+
active_files: z.array(z.string()).optional().default([])
|
|
450
|
+
.describe('Files you are currently working with'),
|
|
451
|
+
next_steps: z.array(z.string()).optional().default([])
|
|
452
|
+
.describe('What needs to happen next'),
|
|
453
|
+
related_memory_ids: z.array(z.string()).optional().default([])
|
|
454
|
+
.describe('IDs of memories relevant to current work'),
|
|
455
|
+
notes: z.string().optional().default('')
|
|
456
|
+
.describe('Any other context worth preserving'),
|
|
457
|
+
episode_id: z.string().optional()
|
|
458
|
+
.describe('Current episode ID if known'),
|
|
459
|
+
},
|
|
460
|
+
async (params) => {
|
|
461
|
+
const state: ConsciousState = {
|
|
462
|
+
currentTask: params.current_task,
|
|
463
|
+
decisions: params.decisions,
|
|
464
|
+
activeFiles: params.active_files,
|
|
465
|
+
nextSteps: params.next_steps,
|
|
466
|
+
relatedMemoryIds: params.related_memory_ids,
|
|
467
|
+
notes: params.notes,
|
|
468
|
+
episodeId: params.episode_id ?? null,
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
store.saveCheckpoint(AGENT_ID, state);
|
|
472
|
+
log(AGENT_ID, 'checkpoint', `"${params.current_task}" decisions=${params.decisions.length} files=${params.active_files.length}`);
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
content: [{
|
|
476
|
+
type: 'text' as const,
|
|
477
|
+
text: `Checkpoint saved: "${params.current_task}" (${params.decisions.length} decisions, ${params.active_files.length} files)`,
|
|
478
|
+
}],
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
);
|
|
482
|
+
|
|
483
|
+
server.tool(
|
|
484
|
+
'memory_restore',
|
|
485
|
+
`Restore your previous execution state after context compaction or at session start.
|
|
486
|
+
|
|
487
|
+
Returns:
|
|
488
|
+
- Your saved execution state (task, decisions, next steps, files)
|
|
489
|
+
- Recently recalled memories for context
|
|
490
|
+
- Your last write for continuity
|
|
491
|
+
- How long you were idle
|
|
492
|
+
|
|
493
|
+
Use this at the start of every session or after compaction to pick up where you left off.`,
|
|
494
|
+
{},
|
|
495
|
+
async () => {
|
|
496
|
+
const checkpoint = store.getCheckpoint(AGENT_ID);
|
|
497
|
+
|
|
498
|
+
const now = Date.now();
|
|
499
|
+
const idleMs = checkpoint
|
|
500
|
+
? now - checkpoint.auto.lastActivityAt.getTime()
|
|
501
|
+
: 0;
|
|
502
|
+
|
|
503
|
+
// Get last written engram
|
|
504
|
+
let lastWrite: { id: string; concept: string; content: string } | null = null;
|
|
505
|
+
if (checkpoint?.auto.lastWriteId) {
|
|
506
|
+
const engram = store.getEngram(checkpoint.auto.lastWriteId);
|
|
507
|
+
if (engram) {
|
|
508
|
+
lastWrite = { id: engram.id, concept: engram.concept, content: engram.content };
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Recall memories using last context
|
|
513
|
+
let recalledMemories: Array<{ id: string; concept: string; content: string; score: number }> = [];
|
|
514
|
+
const recallContext = checkpoint?.auto.lastRecallContext
|
|
515
|
+
?? checkpoint?.executionState?.currentTask
|
|
516
|
+
?? null;
|
|
517
|
+
|
|
518
|
+
if (recallContext) {
|
|
519
|
+
try {
|
|
520
|
+
const results = await activationEngine.activate({
|
|
521
|
+
agentId: AGENT_ID,
|
|
522
|
+
context: recallContext,
|
|
523
|
+
limit: 5,
|
|
524
|
+
minScore: 0.05,
|
|
525
|
+
useReranker: true,
|
|
526
|
+
useExpansion: true,
|
|
527
|
+
});
|
|
528
|
+
recalledMemories = results.map(r => ({
|
|
529
|
+
id: r.engram.id,
|
|
530
|
+
concept: r.engram.concept,
|
|
531
|
+
content: r.engram.content,
|
|
532
|
+
score: r.score,
|
|
533
|
+
}));
|
|
534
|
+
} catch { /* recall failure is non-fatal */ }
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// Consolidation on restore:
|
|
538
|
+
// - If idle >5min but last consolidation was recent (graceful exit ran it), skip
|
|
539
|
+
// - If idle >5min and no recent consolidation, run full cycle (non-graceful exit fallback)
|
|
540
|
+
const MINI_IDLE_MS = 5 * 60_000;
|
|
541
|
+
const FULL_CONSOLIDATION_GAP_MS = 10 * 60_000; // 10 min — if last consolidation was longer ago, run full
|
|
542
|
+
let miniConsolidationTriggered = false;
|
|
543
|
+
let fullConsolidationTriggered = false;
|
|
544
|
+
|
|
545
|
+
if (idleMs > MINI_IDLE_MS) {
|
|
546
|
+
const sinceLastConsolidation = checkpoint?.lastConsolidationAt
|
|
547
|
+
? now - checkpoint.lastConsolidationAt.getTime()
|
|
548
|
+
: Infinity;
|
|
549
|
+
|
|
550
|
+
if (sinceLastConsolidation > FULL_CONSOLIDATION_GAP_MS) {
|
|
551
|
+
// No recent consolidation — graceful exit didn't happen, run full cycle
|
|
552
|
+
fullConsolidationTriggered = true;
|
|
553
|
+
try {
|
|
554
|
+
const result = consolidationEngine.consolidate(AGENT_ID);
|
|
555
|
+
store.markConsolidation(AGENT_ID, false);
|
|
556
|
+
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`);
|
|
557
|
+
} catch { /* consolidation failure is non-fatal */ }
|
|
558
|
+
} else {
|
|
559
|
+
// Recent consolidation exists — graceful exit already handled it, just do mini
|
|
560
|
+
miniConsolidationTriggered = true;
|
|
561
|
+
consolidationScheduler.runMiniConsolidation(AGENT_ID).catch(() => {});
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Format response
|
|
566
|
+
const parts: string[] = [];
|
|
567
|
+
const idleMin = Math.round(idleMs / 60_000);
|
|
568
|
+
const consolidationNote = fullConsolidationTriggered
|
|
569
|
+
? ' (full consolidation — no graceful exit detected)'
|
|
570
|
+
: miniConsolidationTriggered
|
|
571
|
+
? ' (mini-consolidation triggered)'
|
|
572
|
+
: '';
|
|
573
|
+
log(AGENT_ID, 'restore', `idle=${idleMin}min checkpoint=${!!checkpoint?.executionState} recalled=${recalledMemories.length} lastWrite=${lastWrite?.concept ?? 'none'}${fullConsolidationTriggered ? ' FULL_CONSOLIDATION' : ''}`);
|
|
574
|
+
parts.push(`Idle: ${idleMin}min${consolidationNote}`);
|
|
575
|
+
|
|
576
|
+
if (checkpoint?.executionState) {
|
|
577
|
+
const s = checkpoint.executionState;
|
|
578
|
+
parts.push(`\n**Current task:** ${s.currentTask}`);
|
|
579
|
+
if (s.decisions.length) parts.push(`**Decisions:** ${s.decisions.join('; ')}`);
|
|
580
|
+
if (s.nextSteps.length) parts.push(`**Next steps:** ${s.nextSteps.map((st, i) => `${i + 1}. ${st}`).join(', ')}`);
|
|
581
|
+
if (s.activeFiles.length) parts.push(`**Active files:** ${s.activeFiles.join(', ')}`);
|
|
582
|
+
if (s.notes) parts.push(`**Notes:** ${s.notes}`);
|
|
583
|
+
if (checkpoint.checkpointAt) parts.push(`_Saved at: ${checkpoint.checkpointAt.toISOString()}_`);
|
|
584
|
+
} else {
|
|
585
|
+
parts.push('\nNo explicit checkpoint saved.');
|
|
586
|
+
parts.push('\n**Tip:** Use memory_write to save important learnings, and memory_checkpoint before long operations so you can recover state.');
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (lastWrite) {
|
|
590
|
+
parts.push(`\n**Last write:** ${lastWrite.concept}\n${lastWrite.content}`);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if (recalledMemories.length > 0) {
|
|
594
|
+
parts.push(`\n**Recalled memories (${recalledMemories.length}):**`);
|
|
595
|
+
for (const m of recalledMemories) {
|
|
596
|
+
parts.push(`- **${m.concept}** (${m.score.toFixed(3)}): ${m.content.slice(0, 150)}${m.content.length > 150 ? '...' : ''}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return {
|
|
601
|
+
content: [{
|
|
602
|
+
type: 'text' as const,
|
|
603
|
+
text: parts.join('\n'),
|
|
604
|
+
}],
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
);
|
|
608
|
+
|
|
609
|
+
// --- Task Management Tools ---
|
|
610
|
+
|
|
611
|
+
server.tool(
|
|
612
|
+
'memory_task_add',
|
|
613
|
+
`Create a task that you need to come back to. Tasks are memories with status and priority tracking.
|
|
614
|
+
|
|
615
|
+
Use this when:
|
|
616
|
+
- You identify work that needs doing but can't do it right now
|
|
617
|
+
- The user mentions something to do later
|
|
618
|
+
- You want to park a sub-task while focusing on something more urgent
|
|
619
|
+
|
|
620
|
+
Tasks automatically get high salience so they won't be discarded.`,
|
|
621
|
+
{
|
|
622
|
+
concept: z.string().describe('Short task title (3-10 words)'),
|
|
623
|
+
content: z.string().describe('Full task description — what needs doing, context, acceptance criteria'),
|
|
624
|
+
tags: z.array(z.string()).optional().describe('Tags for categorization'),
|
|
625
|
+
priority: z.enum(['urgent', 'high', 'medium', 'low']).default('medium')
|
|
626
|
+
.describe('Task priority: urgent (do now), high (do soon), medium (normal), low (backlog)'),
|
|
627
|
+
blocked_by: z.string().optional().describe('ID of a task that must finish first'),
|
|
628
|
+
},
|
|
629
|
+
async (params) => {
|
|
630
|
+
const engram = store.createEngram({
|
|
631
|
+
agentId: AGENT_ID,
|
|
632
|
+
concept: params.concept,
|
|
633
|
+
content: params.content,
|
|
634
|
+
tags: [...(params.tags ?? []), 'task'],
|
|
635
|
+
salience: 0.9, // Tasks always high salience
|
|
636
|
+
confidence: 0.8,
|
|
637
|
+
salienceFeatures: {
|
|
638
|
+
surprise: 0.5,
|
|
639
|
+
decisionMade: true,
|
|
640
|
+
causalDepth: 0.5,
|
|
641
|
+
resolutionEffort: 0.5,
|
|
642
|
+
eventType: 'decision',
|
|
643
|
+
},
|
|
644
|
+
reasonCodes: ['task-created'],
|
|
645
|
+
taskStatus: params.blocked_by ? 'blocked' : 'open',
|
|
646
|
+
taskPriority: params.priority as TaskPriority,
|
|
647
|
+
blockedBy: params.blocked_by,
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
connectionEngine.enqueue(engram.id);
|
|
651
|
+
|
|
652
|
+
// Generate embedding asynchronously
|
|
653
|
+
embed(`${params.concept} ${params.content}`).then(vec => {
|
|
654
|
+
store.updateEmbedding(engram.id, vec);
|
|
655
|
+
}).catch(() => {});
|
|
656
|
+
|
|
657
|
+
return {
|
|
658
|
+
content: [{
|
|
659
|
+
type: 'text' as const,
|
|
660
|
+
text: `Task created: "${params.concept}" (${params.priority})`,
|
|
661
|
+
}],
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
);
|
|
665
|
+
|
|
666
|
+
server.tool(
|
|
667
|
+
'memory_task_update',
|
|
668
|
+
`Update a task's status or priority. Use this to:
|
|
669
|
+
- Start working on a task (open → in_progress)
|
|
670
|
+
- Mark a task done (→ done)
|
|
671
|
+
- Block a task on another (→ blocked)
|
|
672
|
+
- Reprioritize (change priority)
|
|
673
|
+
- Unblock a task (clear blocked_by)`,
|
|
674
|
+
{
|
|
675
|
+
task_id: z.string().describe('ID of the task to update'),
|
|
676
|
+
status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
|
|
677
|
+
.describe('New status'),
|
|
678
|
+
priority: z.enum(['urgent', 'high', 'medium', 'low']).optional()
|
|
679
|
+
.describe('New priority'),
|
|
680
|
+
blocked_by: z.string().optional().describe('ID of blocking task (set to empty string to unblock)'),
|
|
681
|
+
},
|
|
682
|
+
async (params) => {
|
|
683
|
+
const engram = store.getEngram(params.task_id);
|
|
684
|
+
if (!engram || !engram.taskStatus) {
|
|
685
|
+
return { content: [{ type: 'text' as const, text: `Task not found: ${params.task_id}` }] };
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (params.blocked_by !== undefined) {
|
|
689
|
+
store.updateBlockedBy(params.task_id, params.blocked_by || null);
|
|
690
|
+
}
|
|
691
|
+
if (params.status) {
|
|
692
|
+
store.updateTaskStatus(params.task_id, params.status as TaskStatus);
|
|
693
|
+
}
|
|
694
|
+
if (params.priority) {
|
|
695
|
+
store.updateTaskPriority(params.task_id, params.priority as TaskPriority);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
const updated = store.getEngram(params.task_id)!;
|
|
699
|
+
return {
|
|
700
|
+
content: [{
|
|
701
|
+
type: 'text' as const,
|
|
702
|
+
text: `Updated: "${updated.concept}" → ${updated.taskStatus} (${updated.taskPriority})`,
|
|
703
|
+
}],
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
);
|
|
707
|
+
|
|
708
|
+
server.tool(
|
|
709
|
+
'memory_task_list',
|
|
710
|
+
`List tasks with optional status filter. Shows tasks ordered by priority (urgent first).
|
|
711
|
+
|
|
712
|
+
Use at the start of a session to see what's pending, or to check blocked/done tasks.`,
|
|
713
|
+
{
|
|
714
|
+
status: z.enum(['open', 'in_progress', 'blocked', 'done']).optional()
|
|
715
|
+
.describe('Filter by status (omit to see all active tasks)'),
|
|
716
|
+
include_done: z.boolean().optional().default(false)
|
|
717
|
+
.describe('Include completed tasks?'),
|
|
718
|
+
},
|
|
719
|
+
async (params) => {
|
|
720
|
+
let tasks = store.getTasks(AGENT_ID, params.status as TaskStatus | undefined);
|
|
721
|
+
if (!params.include_done && !params.status) {
|
|
722
|
+
tasks = tasks.filter(t => t.taskStatus !== 'done');
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
if (tasks.length === 0) {
|
|
726
|
+
return { content: [{ type: 'text' as const, text: 'No tasks found.' }] };
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const lines = tasks.map((t, i) => {
|
|
730
|
+
const blocked = t.blockedBy ? ` [blocked by ${t.blockedBy}]` : '';
|
|
731
|
+
const tags = t.tags?.filter(tag => tag !== 'task').join(', ');
|
|
732
|
+
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}`;
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
return {
|
|
736
|
+
content: [{
|
|
737
|
+
type: 'text' as const,
|
|
738
|
+
text: `Tasks (${tasks.length}):\n\n${lines.join('\n\n')}`,
|
|
739
|
+
}],
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
);
|
|
743
|
+
|
|
744
|
+
server.tool(
|
|
745
|
+
'memory_task_next',
|
|
746
|
+
`Get the single most important task to work on next.
|
|
747
|
+
|
|
748
|
+
Prioritizes: in_progress tasks first (finish what you started), then by priority level, then oldest first. Skips blocked and done tasks.
|
|
749
|
+
|
|
750
|
+
Use this when you finish a task or need to decide what to do next.`,
|
|
751
|
+
{},
|
|
752
|
+
async () => {
|
|
753
|
+
const next = store.getNextTask(AGENT_ID);
|
|
754
|
+
if (!next) {
|
|
755
|
+
return { content: [{ type: 'text' as const, text: 'No actionable tasks. All clear!' }] };
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const blocked = next.blockedBy ? `\nBlocked by: ${next.blockedBy}` : '';
|
|
759
|
+
const tags = next.tags?.filter(tag => tag !== 'task').join(', ');
|
|
760
|
+
|
|
761
|
+
return {
|
|
762
|
+
content: [{
|
|
763
|
+
type: 'text' as const,
|
|
764
|
+
text: `Next task:\n**${next.concept}** (${next.taskPriority})\nStatus: ${next.taskStatus}\n${next.content}${blocked}\n${tags ? `Tags: ${tags}\n` : ''}ID: ${next.id}`,
|
|
765
|
+
}],
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
);
|
|
769
|
+
|
|
770
|
+
// --- Task Bracket Tools ---
|
|
771
|
+
|
|
772
|
+
server.tool(
|
|
773
|
+
'memory_task_begin',
|
|
774
|
+
`Signal that you're starting a significant task. Auto-checkpoints current state and recalls relevant memories.
|
|
775
|
+
|
|
776
|
+
CALL THIS when starting:
|
|
777
|
+
- A multi-step operation (doc generation, large refactor, migration)
|
|
778
|
+
- Work on a new topic or project area
|
|
779
|
+
- Anything that might fill the context window
|
|
780
|
+
|
|
781
|
+
This ensures your state is saved before you start, and primes recall with relevant context.`,
|
|
782
|
+
{
|
|
783
|
+
topic: z.string().describe('What task are you starting? (3-15 words)'),
|
|
784
|
+
files: z.array(z.string()).optional().default([])
|
|
785
|
+
.describe('Files you expect to work with'),
|
|
786
|
+
notes: z.string().optional().default('')
|
|
787
|
+
.describe('Any additional context'),
|
|
788
|
+
},
|
|
789
|
+
async (params) => {
|
|
790
|
+
// 1. Checkpoint current state
|
|
791
|
+
const checkpoint = store.getCheckpoint(AGENT_ID);
|
|
792
|
+
const prevTask = checkpoint?.executionState?.currentTask ?? 'None';
|
|
793
|
+
|
|
794
|
+
store.saveCheckpoint(AGENT_ID, {
|
|
795
|
+
currentTask: params.topic,
|
|
796
|
+
decisions: [],
|
|
797
|
+
activeFiles: params.files,
|
|
798
|
+
nextSteps: [],
|
|
799
|
+
relatedMemoryIds: [],
|
|
800
|
+
notes: params.notes || `Started via memory_task_begin. Previous task: ${prevTask}`,
|
|
801
|
+
episodeId: null,
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
// 2. Auto-recall relevant memories
|
|
805
|
+
let recalledSummary = '';
|
|
806
|
+
try {
|
|
807
|
+
const results = await activationEngine.activate({
|
|
808
|
+
agentId: AGENT_ID,
|
|
809
|
+
context: params.topic,
|
|
810
|
+
limit: 5,
|
|
811
|
+
minScore: 0.05,
|
|
812
|
+
useReranker: true,
|
|
813
|
+
useExpansion: true,
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
if (results.length > 0) {
|
|
817
|
+
const lines = results.map((r, i) => {
|
|
818
|
+
const tags = r.engram.tags?.length ? ` [${r.engram.tags.join(', ')}]` : '';
|
|
819
|
+
return `${i + 1}. **${r.engram.concept}** (${r.score.toFixed(3)})${tags}\n ${r.engram.content.slice(0, 150)}${r.engram.content.length > 150 ? '...' : ''}`;
|
|
820
|
+
});
|
|
821
|
+
recalledSummary = `\n\n**Recalled memories (${results.length}):**\n${lines.join('\n')}`;
|
|
822
|
+
|
|
823
|
+
// Track recall
|
|
824
|
+
store.updateAutoCheckpointRecall(AGENT_ID, params.topic, results.map(r => r.engram.id));
|
|
825
|
+
}
|
|
826
|
+
} catch { /* recall failure is non-fatal */ }
|
|
827
|
+
|
|
828
|
+
log(AGENT_ID, 'task:begin', `"${params.topic}" prev="${prevTask}"`);
|
|
829
|
+
|
|
830
|
+
return {
|
|
831
|
+
content: [{
|
|
832
|
+
type: 'text' as const,
|
|
833
|
+
text: `Started: "${params.topic}" (prev: ${prevTask})${recalledSummary}`,
|
|
834
|
+
}],
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
server.tool(
|
|
840
|
+
'memory_task_end',
|
|
841
|
+
`Signal that you've finished a significant task. Writes a summary memory and auto-checkpoints.
|
|
842
|
+
|
|
843
|
+
CALL THIS when you finish:
|
|
844
|
+
- A multi-step operation
|
|
845
|
+
- Before switching to a different topic
|
|
846
|
+
- At the end of a work session
|
|
847
|
+
|
|
848
|
+
This captures what was accomplished so future sessions can recall it.`,
|
|
849
|
+
{
|
|
850
|
+
summary: z.string().describe('What was accomplished? Include key outcomes, decisions, and any issues.'),
|
|
851
|
+
tags: z.array(z.string()).optional().default([])
|
|
852
|
+
.describe('Tags for the summary memory'),
|
|
853
|
+
supersedes: z.array(z.string()).optional().default([])
|
|
854
|
+
.describe('IDs of older memories this task summary replaces (marks them as superseded)'),
|
|
855
|
+
},
|
|
856
|
+
async (params) => {
|
|
857
|
+
// 1. Write summary as a memory
|
|
858
|
+
const salience = evaluateSalience({
|
|
859
|
+
content: params.summary,
|
|
860
|
+
eventType: 'decision',
|
|
861
|
+
surprise: 0.3,
|
|
862
|
+
decisionMade: true,
|
|
863
|
+
causalDepth: 0.5,
|
|
864
|
+
resolutionEffort: 0.5,
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
// Determine the real task name for the summary engram
|
|
868
|
+
const checkpoint = store.getCheckpoint(AGENT_ID);
|
|
869
|
+
const rawTask = checkpoint?.executionState?.currentTask ?? 'Unknown task';
|
|
870
|
+
// Strip any "Completed: " prefixes to avoid cascading
|
|
871
|
+
const cleanedTask = rawTask.replace(/^(Completed: )+/, '');
|
|
872
|
+
// Don't use auto-checkpoint or already-completed tasks as real task names
|
|
873
|
+
const isNamedTask = !cleanedTask.startsWith('Auto-checkpoint') && cleanedTask !== 'Unknown task';
|
|
874
|
+
const completedTask = isNamedTask
|
|
875
|
+
? cleanedTask
|
|
876
|
+
: params.summary.slice(0, 60).replace(/\n/g, ' ');
|
|
877
|
+
|
|
878
|
+
const engram = store.createEngram({
|
|
879
|
+
agentId: AGENT_ID,
|
|
880
|
+
concept: completedTask.slice(0, 80),
|
|
881
|
+
content: params.summary,
|
|
882
|
+
tags: [...params.tags, 'task-summary'],
|
|
883
|
+
salience: isNamedTask ? Math.max(salience.score, 0.7) : salience.score, // Only floor salience for named tasks
|
|
884
|
+
salienceFeatures: salience.features,
|
|
885
|
+
reasonCodes: [...salience.reasonCodes, 'task-end'],
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
connectionEngine.enqueue(engram.id);
|
|
889
|
+
|
|
890
|
+
// 2. Handle supersessions — mark old memories as outdated
|
|
891
|
+
let supersededCount = 0;
|
|
892
|
+
for (const oldId of params.supersedes) {
|
|
893
|
+
const oldEngram = store.getEngram(oldId);
|
|
894
|
+
if (oldEngram) {
|
|
895
|
+
store.supersedeEngram(oldId, engram.id);
|
|
896
|
+
store.upsertAssociation(engram.id, oldId, 0.8, 'causal', 0.9);
|
|
897
|
+
store.updateConfidence(oldId, Math.max(0.2, oldEngram.confidence * 0.4));
|
|
898
|
+
supersededCount++;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// Generate embedding asynchronously
|
|
903
|
+
embed(`Task completed: ${params.summary}`).then(vec => {
|
|
904
|
+
store.updateEmbedding(engram.id, vec);
|
|
905
|
+
}).catch(() => {});
|
|
906
|
+
|
|
907
|
+
// 2. Update checkpoint to reflect completion
|
|
908
|
+
store.saveCheckpoint(AGENT_ID, {
|
|
909
|
+
currentTask: `Completed: ${completedTask}`,
|
|
910
|
+
decisions: checkpoint?.executionState?.decisions ?? [],
|
|
911
|
+
activeFiles: [],
|
|
912
|
+
nextSteps: [],
|
|
913
|
+
relatedMemoryIds: [engram.id],
|
|
914
|
+
notes: `Task completed. Summary memory: ${engram.id}`,
|
|
915
|
+
episodeId: null,
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
store.updateAutoCheckpointWrite(AGENT_ID, engram.id);
|
|
919
|
+
log(AGENT_ID, 'task:end', `"${completedTask}" summary=${engram.id} salience=${salience.score.toFixed(2)} superseded=${supersededCount}`);
|
|
920
|
+
|
|
921
|
+
const supersededNote = supersededCount > 0 ? ` (${supersededCount} old memories superseded)` : '';
|
|
922
|
+
return {
|
|
923
|
+
content: [{
|
|
924
|
+
type: 'text' as const,
|
|
925
|
+
text: `Completed: "${completedTask}" [${salience.score.toFixed(2)}]${supersededNote}`,
|
|
926
|
+
}],
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
);
|
|
930
|
+
|
|
931
|
+
// --- Start ---
|
|
932
|
+
|
|
933
|
+
async function main() {
|
|
934
|
+
const transport = new StdioServerTransport();
|
|
935
|
+
await server.connect(transport);
|
|
936
|
+
|
|
937
|
+
// Start hook sidecar (lightweight HTTP for Claude Code hooks)
|
|
938
|
+
const sidecar = startSidecar({
|
|
939
|
+
store,
|
|
940
|
+
agentId: AGENT_ID,
|
|
941
|
+
secret: HOOK_SECRET,
|
|
942
|
+
port: HOOK_PORT,
|
|
943
|
+
onConsolidate: (agentId, reason) => {
|
|
944
|
+
console.error(`[mcp] consolidation triggered: ${reason}`);
|
|
945
|
+
const result = consolidationEngine.consolidate(agentId);
|
|
946
|
+
store.markConsolidation(agentId, false);
|
|
947
|
+
console.error(`[mcp] consolidation done: ${result.edgesStrengthened} strengthened, ${result.memoriesForgotten} forgotten`);
|
|
948
|
+
},
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
// Log to stderr (stdout is reserved for MCP protocol)
|
|
952
|
+
console.error(`AgentWorkingMemory MCP server started (agent: ${AGENT_ID}, db: ${DB_PATH})`);
|
|
953
|
+
console.error(`Hook sidecar on 127.0.0.1:${HOOK_PORT}${HOOK_SECRET ? ' (auth enabled)' : ' (no auth — set AWM_HOOK_SECRET)'}`);
|
|
954
|
+
|
|
955
|
+
// Clean shutdown
|
|
956
|
+
const cleanup = () => {
|
|
957
|
+
sidecar.close();
|
|
958
|
+
consolidationScheduler.stop();
|
|
959
|
+
stagingBuffer.stop();
|
|
960
|
+
store.close();
|
|
961
|
+
};
|
|
962
|
+
process.on('SIGINT', () => { cleanup(); process.exit(0); });
|
|
963
|
+
process.on('SIGTERM', () => { cleanup(); process.exit(0); });
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
main().catch(err => {
|
|
967
|
+
console.error('MCP server failed:', err);
|
|
968
|
+
process.exit(1);
|
|
969
|
+
});
|
|
970
|
+
|
|
971
|
+
} // end else (non-incognito)
|