@getanima/core 0.2.0 → 0.2.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/README.md +148 -113
- package/dist/anima.d.ts +28 -0
- package/dist/anima.d.ts.map +1 -1
- package/dist/anima.js +60 -8
- package/dist/anima.js.map +1 -1
- package/dist/esm/anima.js +340 -0
- package/dist/esm/anima.js.map +1 -0
- package/dist/esm/identity.js +205 -0
- package/dist/esm/identity.js.map +1 -0
- package/dist/esm/index.js +14 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/memory.js +429 -0
- package/dist/esm/memory.js.map +1 -0
- package/dist/esm/reflection.js +86 -0
- package/dist/esm/reflection.js.map +1 -0
- package/dist/esm/signing.js +194 -0
- package/dist/esm/signing.js.map +1 -0
- package/dist/esm/types.js +6 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/utils.js +155 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/memory.d.ts +28 -0
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +77 -0
- package/dist/memory.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +8 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +28 -9
- package/dist/utils.js.map +1 -1
- package/package.json +10 -5
- package/SPEC.md +0 -475
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Engine — storage, retrieval, consolidation, and decay.
|
|
3
|
+
*
|
|
4
|
+
* File-based. Markdown-native. Zero external dependencies.
|
|
5
|
+
*
|
|
6
|
+
* Storage layout:
|
|
7
|
+
* {storagePath}/
|
|
8
|
+
* SOUL.md — immutable identity
|
|
9
|
+
* NOW.md — lifeboat (20-line emergency context)
|
|
10
|
+
* MEMORY.md — curated long-term memory
|
|
11
|
+
* identity.json — structured identity data
|
|
12
|
+
* memory/
|
|
13
|
+
* YYYY-MM-DD.md — daily logs
|
|
14
|
+
* memories.json — structured memory index
|
|
15
|
+
* opinions/
|
|
16
|
+
* opinions.json — opinion tracker
|
|
17
|
+
*/
|
|
18
|
+
import { uid, now, dateKey, yesterdayKey, readFileSafe, writeFileSafe, appendFileSafe, memoryToMarkdown, calculateSalience, calculateDecay } from './utils';
|
|
19
|
+
import { join } from 'path';
|
|
20
|
+
const DEFAULT_DECAY = {
|
|
21
|
+
procedural: 0.0003,
|
|
22
|
+
semantic: 0.001,
|
|
23
|
+
episodic: 0.003,
|
|
24
|
+
};
|
|
25
|
+
export class MemoryEngine {
|
|
26
|
+
storagePath;
|
|
27
|
+
memoryDir;
|
|
28
|
+
decayConfig;
|
|
29
|
+
memories = [];
|
|
30
|
+
opinions = [];
|
|
31
|
+
sessionId;
|
|
32
|
+
constructor(storagePath, sessionId, decayConfig) {
|
|
33
|
+
this.storagePath = storagePath;
|
|
34
|
+
this.memoryDir = join(storagePath, 'memory');
|
|
35
|
+
this.sessionId = sessionId;
|
|
36
|
+
this.decayConfig = { ...DEFAULT_DECAY, ...decayConfig };
|
|
37
|
+
}
|
|
38
|
+
// ============ CORE: Remember ============
|
|
39
|
+
/** Store a new memory. Writes to daily log AND structured index. */
|
|
40
|
+
async remember(input) {
|
|
41
|
+
const memory = {
|
|
42
|
+
id: uid(),
|
|
43
|
+
type: input.type || 'event',
|
|
44
|
+
content: input.content,
|
|
45
|
+
importance: input.importance || 'medium',
|
|
46
|
+
tier: 'hot',
|
|
47
|
+
tags: input.tags || [],
|
|
48
|
+
timestamp: now(),
|
|
49
|
+
sessionId: this.sessionId,
|
|
50
|
+
salienceScore: undefined,
|
|
51
|
+
emotionalWeight: input.emotionalWeight || 0,
|
|
52
|
+
accessCount: 0,
|
|
53
|
+
crossDomainAccess: false,
|
|
54
|
+
decayScore: 0,
|
|
55
|
+
};
|
|
56
|
+
// Score salience
|
|
57
|
+
memory.salienceScore = calculateSalience({
|
|
58
|
+
novelty: this.estimateNovelty(memory),
|
|
59
|
+
retention: 0, // new memory, no retention yet
|
|
60
|
+
momentum: 1, // just created = max momentum
|
|
61
|
+
continuity: this.estimateContinuity(memory),
|
|
62
|
+
effort: this.estimateEffort(memory),
|
|
63
|
+
});
|
|
64
|
+
// Promote to critical tier if needed
|
|
65
|
+
if (memory.importance === 'critical' || (memory.salienceScore && memory.salienceScore > 0.8)) {
|
|
66
|
+
memory.tier = 'hot';
|
|
67
|
+
}
|
|
68
|
+
// Write to daily log (markdown, human-readable)
|
|
69
|
+
const dailyPath = join(this.memoryDir, `${dateKey()}.md`);
|
|
70
|
+
await appendFileSafe(dailyPath, memoryToMarkdown(memory));
|
|
71
|
+
// Write to structured index
|
|
72
|
+
this.memories.push(memory);
|
|
73
|
+
await this.saveIndex();
|
|
74
|
+
return memory;
|
|
75
|
+
}
|
|
76
|
+
// ============ CORE: Recall ============
|
|
77
|
+
/** Search memories by keyword matching. Returns ranked results. */
|
|
78
|
+
async recall(query, limit = 10) {
|
|
79
|
+
await this.loadIndex();
|
|
80
|
+
const queryLower = query.toLowerCase();
|
|
81
|
+
const queryWords = queryLower.split(/\s+/);
|
|
82
|
+
const scored = this.memories
|
|
83
|
+
.map(memory => {
|
|
84
|
+
const contentLower = memory.content.toLowerCase();
|
|
85
|
+
const tagsLower = memory.tags.map(t => t.toLowerCase());
|
|
86
|
+
// Simple relevance: word match count + tag match
|
|
87
|
+
let wordMatchScore = 0;
|
|
88
|
+
for (const word of queryWords) {
|
|
89
|
+
if (contentLower.includes(word))
|
|
90
|
+
wordMatchScore += 1;
|
|
91
|
+
if (tagsLower.some(t => t.includes(word)))
|
|
92
|
+
wordMatchScore += 2;
|
|
93
|
+
}
|
|
94
|
+
// No word matches = not relevant, skip entirely
|
|
95
|
+
if (wordMatchScore === 0)
|
|
96
|
+
return { memory, score: 0 };
|
|
97
|
+
let score = wordMatchScore;
|
|
98
|
+
// Boost by importance (only if words matched)
|
|
99
|
+
const importanceBoost = { low: 0, medium: 0.5, high: 1, critical: 2 };
|
|
100
|
+
score += importanceBoost[memory.importance] || 0;
|
|
101
|
+
// Boost by recency (memories from today score higher)
|
|
102
|
+
const ageMs = Date.now() - new Date(memory.timestamp).getTime();
|
|
103
|
+
const ageHours = ageMs / (1000 * 60 * 60);
|
|
104
|
+
if (ageHours < 24)
|
|
105
|
+
score += 1;
|
|
106
|
+
if (ageHours < 1)
|
|
107
|
+
score += 2;
|
|
108
|
+
// Boost by salience
|
|
109
|
+
score += (memory.salienceScore || 0) * 2;
|
|
110
|
+
// Track access
|
|
111
|
+
memory.accessCount = (memory.accessCount || 0) + 1;
|
|
112
|
+
return { memory, score };
|
|
113
|
+
})
|
|
114
|
+
.filter(({ score }) => score > 0)
|
|
115
|
+
.sort((a, b) => b.score - a.score)
|
|
116
|
+
.slice(0, limit);
|
|
117
|
+
// Save updated access counts
|
|
118
|
+
if (scored.length > 0) {
|
|
119
|
+
await this.saveIndex();
|
|
120
|
+
}
|
|
121
|
+
return scored.map(({ memory }) => memory);
|
|
122
|
+
}
|
|
123
|
+
// ============ CORE: Lifeboat (NOW.md) ============
|
|
124
|
+
/** Update the NOW.md lifeboat — call every 2 significant actions */
|
|
125
|
+
async updateLifeboat(checkpoint) {
|
|
126
|
+
const content = `# NOW.md — Lifeboat
|
|
127
|
+
*If you wake up with zero context, read this first.*
|
|
128
|
+
*Updated: ${checkpoint.updatedAt}*
|
|
129
|
+
|
|
130
|
+
## Active Task
|
|
131
|
+
${checkpoint.activeTask}
|
|
132
|
+
|
|
133
|
+
## Status
|
|
134
|
+
${checkpoint.status}
|
|
135
|
+
|
|
136
|
+
## Resume Point
|
|
137
|
+
${checkpoint.resumePoint}
|
|
138
|
+
|
|
139
|
+
${checkpoint.openThreads?.length ? `## Open Threads\n${checkpoint.openThreads.map(t => `- ${t}`).join('\n')}` : ''}
|
|
140
|
+
|
|
141
|
+
${checkpoint.keyContext?.length ? `## Key Context\n${checkpoint.keyContext.map(c => `- ${c}`).join('\n')}` : ''}
|
|
142
|
+
|
|
143
|
+
${checkpoint.emergency ? '⚠️ **EMERGENCY FLAG SET** — Something critical was happening when this was saved.' : ''}
|
|
144
|
+
`;
|
|
145
|
+
await writeFileSafe(join(this.storagePath, 'NOW.md'), content);
|
|
146
|
+
}
|
|
147
|
+
/** Read the current lifeboat */
|
|
148
|
+
async readLifeboat() {
|
|
149
|
+
return readFileSafe(join(this.storagePath, 'NOW.md'));
|
|
150
|
+
}
|
|
151
|
+
/** Write raw content to lifeboat (for preserving hand-written content) */
|
|
152
|
+
async writeLifeboatRaw(content) {
|
|
153
|
+
await writeFileSafe(join(this.storagePath, 'NOW.md'), content);
|
|
154
|
+
}
|
|
155
|
+
// ============ CORE: Daily Log ============
|
|
156
|
+
/** Read today's daily log */
|
|
157
|
+
async readDailyLog(date) {
|
|
158
|
+
const key = date || dateKey();
|
|
159
|
+
return readFileSafe(join(this.memoryDir, `${key}.md`));
|
|
160
|
+
}
|
|
161
|
+
/** Read yesterday's daily log */
|
|
162
|
+
async readYesterdayLog() {
|
|
163
|
+
return readFileSafe(join(this.memoryDir, `${yesterdayKey()}.md`));
|
|
164
|
+
}
|
|
165
|
+
// ============ CORE: Long-term Memory ============
|
|
166
|
+
/** Read MEMORY.md (curated long-term memory) */
|
|
167
|
+
async readLongTerm() {
|
|
168
|
+
return readFileSafe(join(this.storagePath, 'MEMORY.md'));
|
|
169
|
+
}
|
|
170
|
+
/** Append to MEMORY.md */
|
|
171
|
+
async writeLongTerm(content) {
|
|
172
|
+
await appendFileSafe(join(this.storagePath, 'MEMORY.md'), '\n' + content + '\n');
|
|
173
|
+
}
|
|
174
|
+
// ============ CURATION ============
|
|
175
|
+
/**
|
|
176
|
+
* Curate — review raw memories and distill what matters into MEMORY.md.
|
|
177
|
+
*
|
|
178
|
+
* This is the missing piece most agents skip. Storage is easy. Retrieval is solved.
|
|
179
|
+
* But without periodic curation, you either drown in noise or lose everything.
|
|
180
|
+
*
|
|
181
|
+
* Call this periodically (e.g., during heartbeats, daily reflection).
|
|
182
|
+
*
|
|
183
|
+
* Strategy:
|
|
184
|
+
* 1. Scan memories from the last N hours
|
|
185
|
+
* 2. Filter for high-salience, high-importance items
|
|
186
|
+
* 3. Deduplicate against existing long-term memory
|
|
187
|
+
* 4. Append curated insights to MEMORY.md
|
|
188
|
+
* 5. Return what was curated (so agents can review/edit)
|
|
189
|
+
*/
|
|
190
|
+
async curate(options) {
|
|
191
|
+
const { hoursBack = 48, minImportance = 'medium', minSalience = 0.5, dryRun = false, } = options || {};
|
|
192
|
+
await this.loadIndex();
|
|
193
|
+
const importanceRank = {
|
|
194
|
+
low: 0,
|
|
195
|
+
medium: 1,
|
|
196
|
+
high: 2,
|
|
197
|
+
critical: 3,
|
|
198
|
+
};
|
|
199
|
+
const cutoff = Date.now() - (hoursBack * 60 * 60 * 1000);
|
|
200
|
+
// Step 1: Filter memories worth curating
|
|
201
|
+
const candidates = this.memories.filter(m => {
|
|
202
|
+
const age = new Date(m.timestamp).getTime();
|
|
203
|
+
if (age < cutoff)
|
|
204
|
+
return false;
|
|
205
|
+
if (importanceRank[m.importance] < importanceRank[minImportance])
|
|
206
|
+
return false;
|
|
207
|
+
if ((m.salienceScore ?? 0.5) < minSalience)
|
|
208
|
+
return false;
|
|
209
|
+
// Skip system/boot events
|
|
210
|
+
if (m.tags?.includes('system') || m.tags?.includes('boot'))
|
|
211
|
+
return false;
|
|
212
|
+
return true;
|
|
213
|
+
});
|
|
214
|
+
if (candidates.length === 0) {
|
|
215
|
+
return { curated: [], written: false };
|
|
216
|
+
}
|
|
217
|
+
// Step 2: Deduplicate against existing long-term memory
|
|
218
|
+
const existing = await this.readLongTerm();
|
|
219
|
+
const curated = existing
|
|
220
|
+
? candidates.filter(m => !existing.includes(m.content.slice(0, 50)))
|
|
221
|
+
: candidates;
|
|
222
|
+
if (curated.length === 0 || dryRun) {
|
|
223
|
+
return { curated, written: false };
|
|
224
|
+
}
|
|
225
|
+
// Step 3: Format and write to MEMORY.md
|
|
226
|
+
const header = `\n## Curated — ${dateKey()}\n`;
|
|
227
|
+
const entries = curated
|
|
228
|
+
.sort((a, b) => importanceRank[b.importance] - importanceRank[a.importance])
|
|
229
|
+
.map(m => {
|
|
230
|
+
const tags = m.tags?.length ? ` [${m.tags.join(', ')}]` : '';
|
|
231
|
+
return `- ${m.content}${tags}`;
|
|
232
|
+
})
|
|
233
|
+
.join('\n');
|
|
234
|
+
await this.writeLongTerm(header + entries);
|
|
235
|
+
// Mark curated memories so they aren't re-curated
|
|
236
|
+
for (const m of curated) {
|
|
237
|
+
m.tags = [...(m.tags || []), 'curated'];
|
|
238
|
+
}
|
|
239
|
+
await this.saveIndex();
|
|
240
|
+
return { curated, written: true };
|
|
241
|
+
}
|
|
242
|
+
// ============ MEMORY DECAY ============
|
|
243
|
+
/** Run decay on all memories. Call during reflection/sleep. */
|
|
244
|
+
async runDecay() {
|
|
245
|
+
await this.loadIndex();
|
|
246
|
+
let decayed = 0;
|
|
247
|
+
let archived = 0;
|
|
248
|
+
let kept = 0;
|
|
249
|
+
for (const memory of this.memories) {
|
|
250
|
+
const ageMs = Date.now() - new Date(memory.timestamp).getTime();
|
|
251
|
+
const ageHours = ageMs / (1000 * 60 * 60);
|
|
252
|
+
memory.decayScore = calculateDecay({
|
|
253
|
+
type: memory.type,
|
|
254
|
+
ageHours,
|
|
255
|
+
accessCount: memory.accessCount || 0,
|
|
256
|
+
emotionalWeight: memory.emotionalWeight || 0,
|
|
257
|
+
decayRates: this.decayConfig,
|
|
258
|
+
});
|
|
259
|
+
// Recalculate salience with updated decay
|
|
260
|
+
memory.salienceScore = calculateSalience({
|
|
261
|
+
novelty: this.estimateNovelty(memory),
|
|
262
|
+
retention: Math.min(1, (memory.accessCount || 0) * 0.2),
|
|
263
|
+
momentum: Math.max(0, 1 - (ageHours / 168)), // decays over a week
|
|
264
|
+
continuity: this.estimateContinuity(memory),
|
|
265
|
+
effort: this.estimateEffort(memory),
|
|
266
|
+
});
|
|
267
|
+
// Apply tier transitions
|
|
268
|
+
if (memory.salienceScore < 0.2 && memory.importance !== 'critical') {
|
|
269
|
+
if (memory.tier !== 'archived') {
|
|
270
|
+
memory.tier = 'archived';
|
|
271
|
+
archived++;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
decayed++;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else if (memory.salienceScore < 0.5) {
|
|
278
|
+
memory.tier = 'cold';
|
|
279
|
+
kept++;
|
|
280
|
+
}
|
|
281
|
+
else if (memory.salienceScore < 0.7) {
|
|
282
|
+
memory.tier = 'warm';
|
|
283
|
+
kept++;
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
memory.tier = 'hot';
|
|
287
|
+
kept++;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
// Remove fully decayed memories (archived + salience near 0)
|
|
291
|
+
this.memories = this.memories.filter(m => m.tier !== 'archived' || m.salienceScore > 0.05 || m.importance === 'critical');
|
|
292
|
+
await this.saveIndex();
|
|
293
|
+
return { decayed, archived, kept };
|
|
294
|
+
}
|
|
295
|
+
// ============ OPINIONS ============
|
|
296
|
+
/** Record or update an opinion */
|
|
297
|
+
async opine(input) {
|
|
298
|
+
await this.loadOpinions();
|
|
299
|
+
const existing = this.opinions.find(o => o.topic.toLowerCase() === input.topic.toLowerCase());
|
|
300
|
+
if (existing) {
|
|
301
|
+
// Track opinion evolution
|
|
302
|
+
existing.previousOpinions.push({
|
|
303
|
+
opinion: existing.current,
|
|
304
|
+
confidence: existing.confidence,
|
|
305
|
+
date: existing.updatedAt,
|
|
306
|
+
reasonForChange: `Updated to: ${input.opinion}`,
|
|
307
|
+
});
|
|
308
|
+
existing.current = input.opinion;
|
|
309
|
+
existing.confidence = input.confidence;
|
|
310
|
+
existing.updatedAt = now();
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
const newOpinion = {
|
|
314
|
+
id: uid(),
|
|
315
|
+
topic: input.topic,
|
|
316
|
+
current: input.opinion,
|
|
317
|
+
confidence: input.confidence,
|
|
318
|
+
formedAt: now(),
|
|
319
|
+
updatedAt: now(),
|
|
320
|
+
previousOpinions: [],
|
|
321
|
+
};
|
|
322
|
+
this.opinions.push(newOpinion);
|
|
323
|
+
}
|
|
324
|
+
await this.saveOpinions();
|
|
325
|
+
return existing || this.opinions[this.opinions.length - 1];
|
|
326
|
+
}
|
|
327
|
+
/** Get all opinions */
|
|
328
|
+
async getOpinions() {
|
|
329
|
+
await this.loadOpinions();
|
|
330
|
+
return [...this.opinions];
|
|
331
|
+
}
|
|
332
|
+
// ============ PRE-COMPACTION HOOK ============
|
|
333
|
+
/** Emergency flush — call before context compression */
|
|
334
|
+
async emergencyFlush(context) {
|
|
335
|
+
// Save lifeboat
|
|
336
|
+
await this.updateLifeboat({
|
|
337
|
+
activeTask: context.activeTask || 'Unknown — emergency flush triggered',
|
|
338
|
+
status: 'in-progress',
|
|
339
|
+
resumePoint: 'Resumed from emergency flush',
|
|
340
|
+
keyContext: context.unsavedMemories || [],
|
|
341
|
+
emergency: true,
|
|
342
|
+
updatedAt: now(),
|
|
343
|
+
});
|
|
344
|
+
// Save any unsaved memories
|
|
345
|
+
if (context.unsavedMemories) {
|
|
346
|
+
for (const content of context.unsavedMemories) {
|
|
347
|
+
await this.remember({
|
|
348
|
+
content: `[EMERGENCY FLUSH] ${content}`,
|
|
349
|
+
type: 'event',
|
|
350
|
+
importance: 'high',
|
|
351
|
+
tags: ['emergency-flush', 'pre-compaction'],
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
// Force save index
|
|
356
|
+
await this.saveIndex();
|
|
357
|
+
}
|
|
358
|
+
// ============ INTERNAL ============
|
|
359
|
+
/** Get all memories (for boot context) */
|
|
360
|
+
async getRecentMemories(hours = 48) {
|
|
361
|
+
await this.loadIndex();
|
|
362
|
+
const cutoff = Date.now() - (hours * 60 * 60 * 1000);
|
|
363
|
+
return this.memories
|
|
364
|
+
.filter(m => new Date(m.timestamp).getTime() > cutoff)
|
|
365
|
+
.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
|
|
366
|
+
}
|
|
367
|
+
/** Get all memories */
|
|
368
|
+
async getAllMemories() {
|
|
369
|
+
await this.loadIndex();
|
|
370
|
+
return [...this.memories];
|
|
371
|
+
}
|
|
372
|
+
/** Public index loader for quickLoad (no boot event) */
|
|
373
|
+
async loadIndexPublic() {
|
|
374
|
+
await this.loadIndex();
|
|
375
|
+
await this.loadOpinions();
|
|
376
|
+
}
|
|
377
|
+
async loadIndex() {
|
|
378
|
+
const raw = await readFileSafe(join(this.memoryDir, 'memories.json'));
|
|
379
|
+
if (raw) {
|
|
380
|
+
try {
|
|
381
|
+
this.memories = JSON.parse(raw);
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
this.memories = [];
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
async saveIndex() {
|
|
389
|
+
await writeFileSafe(join(this.memoryDir, 'memories.json'), JSON.stringify(this.memories, null, 2));
|
|
390
|
+
}
|
|
391
|
+
async loadOpinions() {
|
|
392
|
+
const raw = await readFileSafe(join(this.storagePath, 'opinions', 'opinions.json'));
|
|
393
|
+
if (raw) {
|
|
394
|
+
try {
|
|
395
|
+
this.opinions = JSON.parse(raw);
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
this.opinions = [];
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async saveOpinions() {
|
|
403
|
+
await writeFileSafe(join(this.storagePath, 'opinions', 'opinions.json'), JSON.stringify(this.opinions, null, 2));
|
|
404
|
+
}
|
|
405
|
+
estimateNovelty(memory) {
|
|
406
|
+
// Simple heuristic: longer content = potentially more novel
|
|
407
|
+
const lengthScore = Math.min(1, memory.content.length / 500);
|
|
408
|
+
// Critical/emotional = more novel
|
|
409
|
+
const importanceBoost = memory.importance === 'critical' ? 0.3 : 0;
|
|
410
|
+
const emotionalBoost = (memory.emotionalWeight || 0) * 0.2;
|
|
411
|
+
return Math.min(1, lengthScore + importanceBoost + emotionalBoost);
|
|
412
|
+
}
|
|
413
|
+
estimateContinuity(memory) {
|
|
414
|
+
// How connected is this to other memories?
|
|
415
|
+
// Simple: check tag overlap
|
|
416
|
+
if (memory.tags.length === 0)
|
|
417
|
+
return 0.1;
|
|
418
|
+
const taggedMemories = this.memories.filter(m => m.id !== memory.id && m.tags.some(t => memory.tags.includes(t)));
|
|
419
|
+
return Math.min(1, taggedMemories.length * 0.2);
|
|
420
|
+
}
|
|
421
|
+
estimateEffort(memory) {
|
|
422
|
+
// How costly is this memory to reconstruct?
|
|
423
|
+
// Simple: longer = more effort, more tags = less effort (better indexed)
|
|
424
|
+
const lengthCost = Math.min(0.5, memory.content.length / 1000);
|
|
425
|
+
const tagBenefit = Math.min(0.3, memory.tags.length * 0.1);
|
|
426
|
+
return Math.max(0, lengthCost - tagBenefit);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,cAAc,EAAa,gBAAgB,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACvK,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,aAAa,GAAgB;IACjC,UAAU,EAAE,MAAM;IAClB,QAAQ,EAAE,KAAK;IACf,QAAQ,EAAE,KAAK;CAChB,CAAC;AAEF,MAAM,OAAO,YAAY;IACf,WAAW,CAAS;IACpB,SAAS,CAAS;IAClB,WAAW,CAAc;IACzB,QAAQ,GAAa,EAAE,CAAC;IACxB,QAAQ,GAAc,EAAE,CAAC;IACzB,SAAS,CAAS;IAE1B,YAAY,WAAmB,EAAE,SAAiB,EA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CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC;QAC3D,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU,CAAC,CAAC;IAC9C,CAAC;CACF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reflection Engine — self-assessment, growth tracking, memory promotion.
|
|
3
|
+
*
|
|
4
|
+
* This runs during explicit reflection periods (end of session, scheduled reviews).
|
|
5
|
+
* Never during task execution — that's a core principle.
|
|
6
|
+
*/
|
|
7
|
+
import { dateKey } from './utils';
|
|
8
|
+
export class ReflectionEngine {
|
|
9
|
+
storagePath;
|
|
10
|
+
memory;
|
|
11
|
+
identity;
|
|
12
|
+
constructor(storagePath, memory, identity) {
|
|
13
|
+
this.storagePath = storagePath;
|
|
14
|
+
this.memory = memory;
|
|
15
|
+
this.identity = identity;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Promote important daily memories to MEMORY.md (long-term).
|
|
19
|
+
* Call during reflection periods.
|
|
20
|
+
*/
|
|
21
|
+
async promoteToDurable(memories) {
|
|
22
|
+
const highValue = memories.filter(m => m.importance === 'critical' ||
|
|
23
|
+
m.importance === 'high' ||
|
|
24
|
+
(m.salienceScore && m.salienceScore > 0.6) ||
|
|
25
|
+
(m.emotionalWeight && m.emotionalWeight > 0.5));
|
|
26
|
+
if (highValue.length === 0)
|
|
27
|
+
return 0;
|
|
28
|
+
const entries = highValue.map(m => `- **[${m.type}]** ${m.content} *(${m.timestamp})*`).join('\n');
|
|
29
|
+
const section = `\n## ${dateKey()} — Promoted Memories\n${entries}\n`;
|
|
30
|
+
await this.memory.writeLongTerm(section);
|
|
31
|
+
return highValue.length;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Generate a daily review summary.
|
|
35
|
+
* Distills the day's events into key takeaways.
|
|
36
|
+
*/
|
|
37
|
+
async generateDailyReview() {
|
|
38
|
+
const allMemories = await this.memory.getAllMemories();
|
|
39
|
+
const today = dateKey();
|
|
40
|
+
const todayMemories = allMemories.filter(m => m.timestamp.startsWith(today));
|
|
41
|
+
const byType = {};
|
|
42
|
+
const highImportance = [];
|
|
43
|
+
const lessons = [];
|
|
44
|
+
for (const m of todayMemories) {
|
|
45
|
+
byType[m.type] = (byType[m.type] || 0) + 1;
|
|
46
|
+
if (m.importance === 'high' || m.importance === 'critical') {
|
|
47
|
+
highImportance.push(m.content);
|
|
48
|
+
}
|
|
49
|
+
if (m.type === 'lesson' || m.type === 'insight') {
|
|
50
|
+
lessons.push(m.content);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const review = [
|
|
54
|
+
`# Daily Review — ${today}`,
|
|
55
|
+
'',
|
|
56
|
+
`## Summary`,
|
|
57
|
+
`Total memories: ${todayMemories.length}`,
|
|
58
|
+
`By type: ${Object.entries(byType).map(([k, v]) => `${k}(${v})`).join(', ')}`,
|
|
59
|
+
'',
|
|
60
|
+
highImportance.length > 0 ? `## Important Events\n${highImportance.map(e => `- ${e}`).join('\n')}` : '',
|
|
61
|
+
lessons.length > 0 ? `## Lessons & Insights\n${lessons.map(l => `- ${l}`).join('\n')}` : '',
|
|
62
|
+
].filter(Boolean).join('\n');
|
|
63
|
+
return review;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Check for memory conflicts — when different sessions have contradictory beliefs.
|
|
67
|
+
*/
|
|
68
|
+
async detectConflicts() {
|
|
69
|
+
const opinions = await this.memory.getOpinions();
|
|
70
|
+
const conflicts = [];
|
|
71
|
+
for (const opinion of opinions) {
|
|
72
|
+
if (opinion.previousOpinions.length > 0) {
|
|
73
|
+
// This opinion has changed — potential conflict
|
|
74
|
+
const relatedMemories = await this.memory.recall(opinion.topic, 5);
|
|
75
|
+
if (relatedMemories.length > 1) {
|
|
76
|
+
conflicts.push({
|
|
77
|
+
topic: opinion.topic,
|
|
78
|
+
memories: relatedMemories,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return conflicts;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=reflection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reflection.js","sourceRoot":"","sources":["../../src/reflection.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,EAAO,OAAO,EAA+B,MAAM,SAAS,CAAC;AAGpE,MAAM,OAAO,gBAAgB;IACnB,WAAW,CAAS;IACpB,MAAM,CAAe;IACrB,QAAQ,CAAkB;IAElC,YAAY,WAAmB,EAAE,MAAoB,EAAE,QAAyB;QAC9E,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,gBAAgB,CAAC,QAAkB;QACvC,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CACpC,CAAC,CAAC,UAAU,KAAK,UAAU;YAC3B,CAAC,CAAC,UAAU,KAAK,MAAM;YACvB,CAAC,CAAC,CAAC,aAAa,IAAI,CAAC,CAAC,aAAa,GAAG,GAAG,CAAC;YAC1C,CAAC,CAAC,CAAC,eAAe,IAAI,CAAC,CAAC,eAAe,GAAG,GAAG,CAAC,CAC/C,CAAC;QAEF,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,CAAC,CAAC;QAErC,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAChC,QAAQ,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,OAAO,MAAM,CAAC,CAAC,SAAS,IAAI,CACpD,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEb,MAAM,OAAO,GAAG,QAAQ,OAAO,EAAE,yBAAyB,O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