@cleocode/core 2026.4.37 → 2026.4.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/hooks/handlers/task-hooks.d.ts.map +1 -1
  2. package/dist/hooks/handlers/task-hooks.js +11 -0
  3. package/dist/hooks/handlers/task-hooks.js.map +1 -1
  4. package/dist/index.js +644 -33
  5. package/dist/index.js.map +4 -4
  6. package/dist/internal.d.ts +3 -1
  7. package/dist/internal.d.ts.map +1 -1
  8. package/dist/internal.js +3 -1
  9. package/dist/internal.js.map +1 -1
  10. package/dist/memory/decisions.d.ts.map +1 -1
  11. package/dist/memory/decisions.js +18 -0
  12. package/dist/memory/decisions.js.map +1 -1
  13. package/dist/memory/engine-compat.d.ts +17 -0
  14. package/dist/memory/engine-compat.d.ts.map +1 -1
  15. package/dist/memory/engine-compat.js +36 -0
  16. package/dist/memory/engine-compat.js.map +1 -1
  17. package/dist/memory/graph-memory-bridge.d.ts +158 -0
  18. package/dist/memory/graph-memory-bridge.d.ts.map +1 -0
  19. package/dist/memory/graph-memory-bridge.js +519 -0
  20. package/dist/memory/graph-memory-bridge.js.map +1 -0
  21. package/dist/memory/index.d.ts +1 -0
  22. package/dist/memory/index.d.ts.map +1 -1
  23. package/dist/memory/index.js +2 -0
  24. package/dist/memory/index.js.map +1 -1
  25. package/dist/memory/learnings.d.ts.map +1 -1
  26. package/dist/memory/learnings.js +18 -0
  27. package/dist/memory/learnings.js.map +1 -1
  28. package/dist/memory/llm-extraction.js.map +1 -1
  29. package/dist/memory/patterns.d.ts.map +1 -1
  30. package/dist/memory/patterns.js +18 -0
  31. package/dist/memory/patterns.js.map +1 -1
  32. package/dist/memory/quality-feedback.d.ts +129 -0
  33. package/dist/memory/quality-feedback.d.ts.map +1 -0
  34. package/dist/memory/quality-feedback.js +449 -0
  35. package/dist/memory/quality-feedback.js.map +1 -0
  36. package/dist/memory/sleep-consolidation.d.ts +98 -0
  37. package/dist/memory/sleep-consolidation.d.ts.map +1 -0
  38. package/dist/memory/sleep-consolidation.js +706 -0
  39. package/dist/memory/sleep-consolidation.js.map +1 -0
  40. package/dist/memory/temporal-supersession.d.ts +155 -0
  41. package/dist/memory/temporal-supersession.d.ts.map +1 -0
  42. package/dist/memory/temporal-supersession.js +406 -0
  43. package/dist/memory/temporal-supersession.js.map +1 -0
  44. package/package.json +6 -6
  45. package/src/hooks/handlers/task-hooks.ts +11 -0
  46. package/src/internal.ts +12 -0
  47. package/src/memory/__tests__/graph-memory-bridge.test.ts +357 -0
  48. package/src/memory/__tests__/llm-extraction.test.ts +17 -0
  49. package/src/memory/__tests__/quality-feedback.test.ts +418 -0
  50. package/src/memory/__tests__/sleep-consolidation.test.ts +790 -0
  51. package/src/memory/__tests__/temporal-supersession.test.ts +534 -0
  52. package/src/memory/decisions.ts +24 -0
  53. package/src/memory/engine-compat.ts +37 -0
  54. package/src/memory/graph-memory-bridge.ts +751 -0
  55. package/src/memory/index.ts +2 -0
  56. package/src/memory/learnings.ts +24 -0
  57. package/src/memory/patterns.ts +24 -0
  58. package/src/memory/quality-feedback.ts +640 -0
  59. package/src/memory/sleep-consolidation.ts +932 -0
  60. package/src/memory/temporal-supersession.ts +568 -0
  61. package/src/store/__tests__/performance-safety.test.ts +4 -4
@@ -0,0 +1,706 @@
1
+ /**
2
+ * Sleep-Time Consolidation — LLM-driven background memory hygiene for CLEO BRAIN.
3
+ *
4
+ * Implements the "sleep-time compute" pattern inspired by Letta OS: after a
5
+ * session ends, a cheap LLM pass runs in the background to:
6
+ * 1. Merge near-duplicate entries (embedding similarity > 0.85)
7
+ * 2. Prune short-tier stale entries with low quality (7d old, quality < 0.4)
8
+ * 3. Synthesize frequently-cited learnings into higher-quality patterns
9
+ * 4. Extract cross-cutting insights from clusters of related observations
10
+ *
11
+ * All LLM calls use `claude-haiku-4-5-20251001` (cheapest available model).
12
+ * No API key = silent no-op for LLM steps; structural steps still run.
13
+ * All errors are caught and logged — nothing here may block session end.
14
+ *
15
+ * ## Configuration
16
+ *
17
+ * Add to `config.json` under `brain.sleepConsolidation`:
18
+ * ```json
19
+ * {
20
+ * "brain": {
21
+ * "sleepConsolidation": {
22
+ * "enabled": true
23
+ * }
24
+ * }
25
+ * }
26
+ * ```
27
+ *
28
+ * @task T555
29
+ * @epic T549
30
+ * @see packages/core/src/memory/observer-reflector.ts (Observer/Reflector pattern)
31
+ * @see packages/core/src/memory/brain-lifecycle.ts (runConsolidation)
32
+ */
33
+ import { randomBytes } from 'node:crypto';
34
+ import { getBrainNativeDb } from '../store/brain-sqlite.js';
35
+ import { typedAll } from '../store/typed-query.js';
36
+ import { resolveAnthropicApiKey } from './anthropic-key-resolver.js';
37
+ import { storeLearning } from './learnings.js';
38
+ import { storePattern } from './patterns.js';
39
+ // ============================================================================
40
+ // Constants
41
+ // ============================================================================
42
+ /** Cheap model for all sleep-consolidation LLM calls. */
43
+ const SLEEP_MODEL = 'claude-haiku-4-5-20251001';
44
+ /** Embedding similarity threshold above which two entries are considered duplicates. */
45
+ const DUPLICATE_SIMILARITY_THRESHOLD = 0.85;
46
+ /** Minimum age (days) before a short-tier entry can be pruned for low quality. */
47
+ const STALE_AGE_DAYS = 7;
48
+ /** Maximum quality score for a short-tier entry to be considered for pruning. */
49
+ const PRUNE_QUALITY_THRESHOLD = 0.4;
50
+ /** Minimum citation count to trigger pattern synthesis for a learning. */
51
+ const SYNTHESIS_CITATION_MIN = 3;
52
+ /** Maximum tokens for LLM responses. */
53
+ const MAX_RESPONSE_TOKENS = 1024;
54
+ /** Source tag written to brain_observations for sleep-consolidation results. */
55
+ const SLEEP_SOURCE = 'sleep-consolidation';
56
+ /**
57
+ * Load sleep consolidation configuration from the project config.
58
+ * Defaults to enabled=true when config is missing or unreadable.
59
+ *
60
+ * @param projectRoot - Project root directory.
61
+ */
62
+ async function loadSleepConfig(projectRoot) {
63
+ try {
64
+ const { loadConfig } = await import('../config.js');
65
+ const config = await loadConfig(projectRoot);
66
+ const brain = config.brain;
67
+ const sc = brain?.['sleepConsolidation'];
68
+ return { enabled: sc?.['enabled'] !== false };
69
+ }
70
+ catch {
71
+ return { enabled: true };
72
+ }
73
+ }
74
+ /**
75
+ * Call the Anthropic Messages API via native fetch using the cheap model.
76
+ *
77
+ * Uses `resolveAnthropicApiKey()` — never accesses ANTHROPIC_API_KEY directly.
78
+ * Returns null when the key is unavailable or the call fails.
79
+ *
80
+ * @param systemPrompt - System instruction for the LLM.
81
+ * @param userContent - User message content.
82
+ */
83
+ async function callLlm(systemPrompt, userContent) {
84
+ const apiKey = resolveAnthropicApiKey();
85
+ if (!apiKey)
86
+ return null;
87
+ try {
88
+ const response = await fetch('https://api.anthropic.com/v1/messages', {
89
+ method: 'POST',
90
+ headers: {
91
+ 'Content-Type': 'application/json',
92
+ 'x-api-key': apiKey,
93
+ 'anthropic-version': '2023-06-01',
94
+ },
95
+ body: JSON.stringify({
96
+ model: SLEEP_MODEL,
97
+ max_tokens: MAX_RESPONSE_TOKENS,
98
+ system: systemPrompt,
99
+ messages: [{ role: 'user', content: userContent }],
100
+ }),
101
+ });
102
+ if (!response.ok) {
103
+ const body = await response.text().catch(() => '');
104
+ console.warn(`[sleep-consolidation] Anthropic API error ${response.status}: ${body.slice(0, 200)}`);
105
+ return null;
106
+ }
107
+ const data = (await response.json());
108
+ const textBlock = data.content.find((b) => b.type === 'text');
109
+ return textBlock?.text ?? null;
110
+ }
111
+ catch (err) {
112
+ const msg = err instanceof Error ? err.message : String(err);
113
+ console.warn(`[sleep-consolidation] LLM call failed: ${msg}`);
114
+ return null;
115
+ }
116
+ }
117
+ /**
118
+ * Attempt to parse LLM response as JSON. Strips markdown code fences before
119
+ * parsing. Returns null on parse failure.
120
+ */
121
+ function parseJson(text) {
122
+ try {
123
+ const cleaned = text
124
+ .replace(/^```(?:json)?\s*/m, '')
125
+ .replace(/\s*```\s*$/m, '')
126
+ .trim();
127
+ return JSON.parse(cleaned);
128
+ }
129
+ catch {
130
+ return null;
131
+ }
132
+ }
133
+ // ============================================================================
134
+ // Cosine similarity helper
135
+ // ============================================================================
136
+ /**
137
+ * Compute cosine similarity between two Float32 embedding buffers.
138
+ *
139
+ * Returns 0 when either buffer is null, empty, or different lengths.
140
+ * Embeddings are stored as raw Buffer of 4-byte floats (sqlite-vec format).
141
+ *
142
+ * @param a - First embedding buffer.
143
+ * @param b - Second embedding buffer.
144
+ */
145
+ function cosineSimilarity(a, b) {
146
+ if (!a || !b || a.length === 0 || a.length !== b.length)
147
+ return 0;
148
+ const floatCount = Math.floor(a.length / 4);
149
+ if (floatCount === 0)
150
+ return 0;
151
+ let dot = 0;
152
+ let normA = 0;
153
+ let normB = 0;
154
+ for (let i = 0; i < floatCount; i++) {
155
+ const va = a.readFloatLE(i * 4);
156
+ const vb = b.readFloatLE(i * 4);
157
+ dot += va * vb;
158
+ normA += va * va;
159
+ normB += vb * vb;
160
+ }
161
+ const denom = Math.sqrt(normA) * Math.sqrt(normB);
162
+ return denom === 0 ? 0 : dot / denom;
163
+ }
164
+ // ============================================================================
165
+ // Step 1: Merge Duplicates
166
+ // ============================================================================
167
+ /**
168
+ * Find near-duplicate entries using embedding cosine similarity > 0.85.
169
+ *
170
+ * For each pair above the threshold, asks the LLM whether to keep/merge.
171
+ * The LLM confirms or overrides the merge decision. Kept entry gains the
172
+ * evicted entry's citation count. Duplicates are soft-evicted (invalid_at set).
173
+ *
174
+ * Falls back to structural merge (keep higher quality) when no API key is
175
+ * available or the LLM call fails.
176
+ *
177
+ * @param projectRoot - Project root for brain.db resolution.
178
+ */
179
+ async function stepMergeDuplicates(projectRoot) {
180
+ const { getBrainDb } = await import('../store/brain-sqlite.js');
181
+ await getBrainDb(projectRoot);
182
+ const nativeDb = getBrainNativeDb();
183
+ if (!nativeDb)
184
+ return { merged: 0, llmDecisions: 0 };
185
+ let merged = 0;
186
+ let llmDecisions = 0;
187
+ const now = new Date().toISOString().replace('T', ' ').slice(0, 19);
188
+ // Fetch observations that have embeddings and are active
189
+ let rows;
190
+ try {
191
+ rows = typedAll(nativeDb.prepare(`
192
+ SELECT id, title, narrative, quality_score, citation_count, memory_tier, created_at, embedding
193
+ FROM brain_observations
194
+ WHERE embedding IS NOT NULL
195
+ AND invalid_at IS NULL
196
+ AND memory_tier = 'short'
197
+ ORDER BY quality_score DESC
198
+ LIMIT 200
199
+ `));
200
+ }
201
+ catch {
202
+ return { merged: 0, llmDecisions: 0 };
203
+ }
204
+ if (rows.length < 2)
205
+ return { merged: 0, llmDecisions: 0 };
206
+ // Build candidate pairs above the similarity threshold
207
+ const pairs = [];
208
+ for (let i = 0; i < rows.length; i++) {
209
+ for (let j = i + 1; j < rows.length; j++) {
210
+ const sim = cosineSimilarity(rows[i].embedding, rows[j].embedding);
211
+ if (sim >= DUPLICATE_SIMILARITY_THRESHOLD) {
212
+ pairs.push({ a: rows[i], b: rows[j], similarity: sim });
213
+ }
214
+ }
215
+ }
216
+ if (pairs.length === 0)
217
+ return { merged: 0, llmDecisions: 0 };
218
+ // Ask the LLM for a batch merge decision (max 10 pairs per call)
219
+ const pairBatch = pairs.slice(0, 10);
220
+ const pairDescriptions = pairBatch.map(({ a, b, similarity }, idx) => ({
221
+ pair: idx,
222
+ similarity: Math.round(similarity * 100) / 100,
223
+ a: { id: a.id, text: `${a.title ?? ''} ${a.narrative ?? ''}`.trim().slice(0, 120) },
224
+ b: { id: b.id, text: `${b.title ?? ''} ${b.narrative ?? ''}`.trim().slice(0, 120) },
225
+ }));
226
+ const systemPrompt = 'You are a memory deduplication assistant. Given pairs of nearly-identical memory entries ' +
227
+ 'evaluate whether they should be merged. For each pair output: {"pair":N,"merge":true/false,"keep":"<id>"}. ' +
228
+ 'Output a JSON array only, no prose. Merge when content is substantially the same; keep when content is distinct.';
229
+ const userContent = `Memory entry pairs to evaluate:\n${JSON.stringify(pairDescriptions, null, 2)}`;
230
+ let decisions = [];
231
+ const rawResponse = await callLlm(systemPrompt, userContent);
232
+ if (rawResponse) {
233
+ const parsed = parseJson(rawResponse);
234
+ if (Array.isArray(parsed)) {
235
+ decisions = parsed;
236
+ llmDecisions = decisions.filter((d) => d.merge).length;
237
+ }
238
+ }
239
+ // Apply decisions (structural fallback when LLM unavailable)
240
+ const processedIds = new Set();
241
+ for (let idx = 0; idx < pairBatch.length; idx++) {
242
+ const { a, b } = pairBatch[idx];
243
+ if (processedIds.has(a.id) || processedIds.has(b.id))
244
+ continue;
245
+ const decision = decisions.find((d) => d.pair === idx);
246
+ const shouldMerge = decision ? decision.merge : true; // default: merge near-duplicates
247
+ if (!shouldMerge)
248
+ continue;
249
+ // Determine which to keep: prefer LLM decision, fallback to higher quality
250
+ let keepId;
251
+ let evictId;
252
+ if (decision?.keep === a.id || decision?.keep === b.id) {
253
+ keepId = decision.keep;
254
+ evictId = keepId === a.id ? b.id : a.id;
255
+ }
256
+ else {
257
+ const aQ = a.quality_score ?? 0.5;
258
+ const bQ = b.quality_score ?? 0.5;
259
+ keepId = aQ >= bQ ? a.id : b.id;
260
+ evictId = keepId === a.id ? b.id : a.id;
261
+ }
262
+ const keepRow = a.id === keepId ? a : b;
263
+ const evictRow = a.id === evictId ? a : b;
264
+ const combinedCitations = (keepRow.citation_count ?? 0) + (evictRow.citation_count ?? 0);
265
+ try {
266
+ nativeDb
267
+ .prepare(`UPDATE brain_observations SET invalid_at = ?, updated_at = ? WHERE id = ?`)
268
+ .run(now, now, evictId);
269
+ if (combinedCitations > (keepRow.citation_count ?? 0)) {
270
+ nativeDb
271
+ .prepare(`UPDATE brain_observations SET citation_count = ?, updated_at = ? WHERE id = ?`)
272
+ .run(combinedCitations, now, keepId);
273
+ }
274
+ merged++;
275
+ processedIds.add(a.id);
276
+ processedIds.add(b.id);
277
+ }
278
+ catch {
279
+ /* best-effort */
280
+ }
281
+ }
282
+ return { merged, llmDecisions };
283
+ }
284
+ // ============================================================================
285
+ // Step 2: Prune Stale Entries
286
+ // ============================================================================
287
+ /**
288
+ * Prune short-tier entries older than STALE_AGE_DAYS with quality < PRUNE_QUALITY_THRESHOLD.
289
+ *
290
+ * Before evicting, asks the LLM whether any entries should be preserved despite
291
+ * their low score. Preserved entries have their quality_score bumped to 0.5 so
292
+ * they survive future prune passes.
293
+ *
294
+ * @param projectRoot - Project root for brain.db resolution.
295
+ */
296
+ async function stepPruneStale(projectRoot) {
297
+ const { getBrainDb } = await import('../store/brain-sqlite.js');
298
+ await getBrainDb(projectRoot);
299
+ const nativeDb = getBrainNativeDb();
300
+ if (!nativeDb)
301
+ return { pruned: 0, preserved: 0 };
302
+ const staleCutoff = new Date(Date.now() - STALE_AGE_DAYS * 24 * 60 * 60 * 1000)
303
+ .toISOString()
304
+ .replace('T', ' ')
305
+ .slice(0, 19);
306
+ const now = new Date().toISOString().replace('T', ' ').slice(0, 19);
307
+ let candidates;
308
+ try {
309
+ candidates = typedAll(nativeDb.prepare(`
310
+ SELECT id, title, narrative, quality_score, citation_count, memory_tier, created_at, embedding
311
+ FROM brain_observations
312
+ WHERE memory_tier = 'short'
313
+ AND invalid_at IS NULL
314
+ AND quality_score IS NOT NULL
315
+ AND quality_score < ?
316
+ AND created_at < ?
317
+ ORDER BY quality_score ASC
318
+ LIMIT 50
319
+ `), PRUNE_QUALITY_THRESHOLD, staleCutoff);
320
+ }
321
+ catch {
322
+ return { pruned: 0, preserved: 0 };
323
+ }
324
+ if (candidates.length === 0)
325
+ return { pruned: 0, preserved: 0 };
326
+ // Ask LLM which entries to preserve despite low quality
327
+ const candidateDescriptions = candidates.slice(0, 20).map((row) => ({
328
+ id: row.id,
329
+ age_days: Math.round((Date.now() - new Date(row.created_at ?? 0).getTime()) / (24 * 60 * 60 * 1000)),
330
+ quality: Math.round((row.quality_score ?? 0) * 100) / 100,
331
+ citations: row.citation_count,
332
+ text: `${row.title ?? ''} ${row.narrative ?? ''}`.trim().slice(0, 100),
333
+ }));
334
+ const systemPrompt = 'You are a memory curator. Given a list of low-quality, stale memory entries, ' +
335
+ 'decide which ones are worth preserving (i.e. contain unique, non-redundant information ' +
336
+ 'that would be hard to reconstruct). Return a JSON array of IDs to preserve: {"preserve":["id1","id2",...]}. ' +
337
+ 'Only preserve entries with genuinely unique information. When in doubt, allow eviction.';
338
+ const userContent = `Candidate entries for eviction:\n${JSON.stringify(candidateDescriptions, null, 2)}`;
339
+ let preserveIds = new Set();
340
+ const rawResponse = await callLlm(systemPrompt, userContent);
341
+ if (rawResponse) {
342
+ const parsed = parseJson(rawResponse);
343
+ if (parsed && Array.isArray(parsed.preserve)) {
344
+ preserveIds = new Set(parsed.preserve);
345
+ }
346
+ }
347
+ let pruned = 0;
348
+ let preserved = 0;
349
+ for (const row of candidates) {
350
+ if (preserveIds.has(row.id)) {
351
+ // Bump quality so it won't be pruned next pass
352
+ try {
353
+ nativeDb
354
+ .prepare(`UPDATE brain_observations SET quality_score = 0.5, updated_at = ? WHERE id = ?`)
355
+ .run(now, row.id);
356
+ preserved++;
357
+ }
358
+ catch {
359
+ /* best-effort */
360
+ }
361
+ }
362
+ else {
363
+ try {
364
+ nativeDb
365
+ .prepare(`UPDATE brain_observations SET invalid_at = ?, updated_at = ? WHERE id = ?`)
366
+ .run(now, now, row.id);
367
+ pruned++;
368
+ }
369
+ catch {
370
+ /* best-effort */
371
+ }
372
+ }
373
+ }
374
+ return { pruned, preserved };
375
+ }
376
+ // ============================================================================
377
+ // Step 3: Strengthen Patterns
378
+ // ============================================================================
379
+ /**
380
+ * Find learnings cited >= SYNTHESIS_CITATION_MIN times and ask the LLM to
381
+ * synthesize them into a single higher-quality pattern entry.
382
+ *
383
+ * The synthesized pattern is stored via storePattern() with
384
+ * source='sleep-consolidation'. The original learnings are left intact.
385
+ *
386
+ * @param projectRoot - Project root for brain.db resolution.
387
+ */
388
+ async function stepStrengthenPatterns(projectRoot) {
389
+ const { getBrainDb } = await import('../store/brain-sqlite.js');
390
+ await getBrainDb(projectRoot);
391
+ const nativeDb = getBrainNativeDb();
392
+ if (!nativeDb)
393
+ return { synthesized: 0, patternsGenerated: 0 };
394
+ let candidates;
395
+ try {
396
+ candidates = typedAll(nativeDb.prepare(`
397
+ SELECT id, insight, confidence, citation_count, source, memory_tier
398
+ FROM brain_learnings
399
+ WHERE citation_count >= ?
400
+ AND invalid_at IS NULL
401
+ ORDER BY citation_count DESC, confidence DESC
402
+ LIMIT 10
403
+ `), SYNTHESIS_CITATION_MIN);
404
+ }
405
+ catch {
406
+ return { synthesized: 0, patternsGenerated: 0 };
407
+ }
408
+ if (candidates.length === 0)
409
+ return { synthesized: 0, patternsGenerated: 0 };
410
+ // Check if we already have a sleep-consolidation pattern from these
411
+ // (avoid re-synthesizing the same learnings every session)
412
+ let existingPatterns;
413
+ try {
414
+ existingPatterns = typedAll(nativeDb.prepare(`
415
+ SELECT id, pattern, context, impact, frequency, memory_tier
416
+ FROM brain_patterns
417
+ WHERE source_type = ?
418
+ AND invalid_at IS NULL
419
+ ORDER BY frequency DESC
420
+ LIMIT 5
421
+ `), SLEEP_SOURCE);
422
+ }
423
+ catch {
424
+ existingPatterns = [];
425
+ }
426
+ const existingPatternTexts = existingPatterns.map((p) => p.pattern.slice(0, 80)).join('; ');
427
+ const learningDescriptions = candidates.map((l) => ({
428
+ id: l.id,
429
+ insight: l.insight.slice(0, 200),
430
+ citations: l.citation_count,
431
+ confidence: Math.round(l.confidence * 100) / 100,
432
+ }));
433
+ const systemPrompt = 'You are a knowledge synthesizer. Given frequently-cited learnings, extract 1-3 ' +
434
+ 'higher-order patterns that capture the essence of what has been repeatedly confirmed. ' +
435
+ 'Each pattern should be actionable and generalizable. ' +
436
+ 'Return JSON: {"patterns":[{"pattern":"...","context":"...","impact":"high|medium|low"}]}. ' +
437
+ 'Skip patterns already captured in the existing list. Output JSON only, no prose.';
438
+ const userContent = `Frequently-cited learnings to synthesize:\n${JSON.stringify(learningDescriptions, null, 2)}\n\n` +
439
+ `Already captured patterns (do not duplicate): ${existingPatternTexts || 'none'}`;
440
+ const rawResponse = await callLlm(systemPrompt, userContent);
441
+ if (!rawResponse)
442
+ return { synthesized: candidates.length, patternsGenerated: 0 };
443
+ const parsed = parseJson(rawResponse);
444
+ if (!parsed || !Array.isArray(parsed.patterns)) {
445
+ return { synthesized: candidates.length, patternsGenerated: 0 };
446
+ }
447
+ let patternsGenerated = 0;
448
+ for (const p of parsed.patterns) {
449
+ if (!p.pattern?.trim())
450
+ continue;
451
+ try {
452
+ const impact = p.impact === 'high' || p.impact === 'medium' || p.impact === 'low' ? p.impact : 'medium';
453
+ await storePattern(projectRoot, {
454
+ type: 'optimization',
455
+ pattern: p.pattern.slice(0, 500),
456
+ context: (p.context ?? '').slice(0, 500),
457
+ impact,
458
+ source: SLEEP_SOURCE,
459
+ });
460
+ patternsGenerated++;
461
+ }
462
+ catch {
463
+ /* best-effort */
464
+ }
465
+ }
466
+ return { synthesized: candidates.length, patternsGenerated };
467
+ }
468
+ // ============================================================================
469
+ // Step 4: Generate Cross-Cutting Insights
470
+ // ============================================================================
471
+ /**
472
+ * Cluster recent observations by shared entity overlap and ask the LLM to
473
+ * extract a cross-cutting insight for each cluster.
474
+ *
475
+ * Insights are stored as brain_observations with source='sleep-consolidation'
476
+ * and memory_tier='medium' (they represent synthesized knowledge).
477
+ *
478
+ * @param projectRoot - Project root for brain.db resolution.
479
+ */
480
+ async function stepGenerateInsights(projectRoot) {
481
+ const { getBrainDb } = await import('../store/brain-sqlite.js');
482
+ await getBrainDb(projectRoot);
483
+ const nativeDb = getBrainNativeDb();
484
+ if (!nativeDb)
485
+ return { clustersProcessed: 0, insightsStored: 0 };
486
+ // Fetch recent non-sleep observations (last 14 days)
487
+ const recent14d = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000)
488
+ .toISOString()
489
+ .replace('T', ' ')
490
+ .slice(0, 19);
491
+ let observations;
492
+ try {
493
+ observations = typedAll(nativeDb.prepare(`
494
+ SELECT id,
495
+ COALESCE(title, '') || ' ' || COALESCE(narrative, '') AS text
496
+ FROM brain_observations
497
+ WHERE created_at >= ?
498
+ AND invalid_at IS NULL
499
+ AND (source_type IS NULL OR source_type NOT IN (?, 'observer-compressed'))
500
+ ORDER BY quality_score DESC
501
+ LIMIT 60
502
+ `), recent14d, SLEEP_SOURCE);
503
+ }
504
+ catch {
505
+ return { clustersProcessed: 0, insightsStored: 0 };
506
+ }
507
+ if (observations.length < 5)
508
+ return { clustersProcessed: 0, insightsStored: 0 };
509
+ // Simple entity-based clustering: tokenise each observation into words >=4
510
+ // chars, group observations sharing >= 3 tokens into the same cluster.
511
+ const STOP = new Set([
512
+ 'this',
513
+ 'that',
514
+ 'with',
515
+ 'from',
516
+ 'have',
517
+ 'been',
518
+ 'will',
519
+ 'when',
520
+ 'then',
521
+ 'they',
522
+ 'were',
523
+ 'also',
524
+ 'into',
525
+ 'over',
526
+ 'some',
527
+ 'more',
528
+ 'very',
529
+ 'just',
530
+ 'each',
531
+ 'both',
532
+ ]);
533
+ function tokenize(text) {
534
+ const tokens = text
535
+ .toLowerCase()
536
+ .replace(/[^a-z0-9\s]/g, ' ')
537
+ .split(/\s+/)
538
+ .filter((t) => t.length >= 4 && !STOP.has(t));
539
+ return new Set(tokens);
540
+ }
541
+ const tokenSets = observations.map((o) => ({ id: o.id, text: o.text, tokens: tokenize(o.text) }));
542
+ // Build clusters greedily (each observation joins the first compatible cluster)
543
+ const clusters = [];
544
+ for (const obs of tokenSets) {
545
+ let placed = false;
546
+ for (const cluster of clusters) {
547
+ // Check overlap with the first member of the cluster
548
+ const firstText = tokenSets.find((t) => t.id === cluster.memberIds[0]);
549
+ if (!firstText)
550
+ continue;
551
+ let shared = 0;
552
+ for (const tok of obs.tokens) {
553
+ if (firstText.tokens.has(tok))
554
+ shared++;
555
+ }
556
+ if (shared >= 3) {
557
+ cluster.memberIds.push(obs.id);
558
+ cluster.texts.push(obs.text.slice(0, 120));
559
+ placed = true;
560
+ break;
561
+ }
562
+ }
563
+ if (!placed && clusters.length < 5) {
564
+ clusters.push({ memberIds: [obs.id], texts: [obs.text.slice(0, 120)] });
565
+ }
566
+ }
567
+ // Only process clusters with >= 3 members
568
+ const validClusters = clusters.filter((c) => c.memberIds.length >= 3);
569
+ if (validClusters.length === 0)
570
+ return { clustersProcessed: 0, insightsStored: 0 };
571
+ const clusterDescriptions = validClusters.map((c, i) => ({
572
+ cluster: i,
573
+ entries: c.texts.slice(0, 5),
574
+ }));
575
+ const systemPrompt = 'You are a cross-domain insight extractor. Given clusters of related memory entries, ' +
576
+ 'identify one cross-cutting insight per cluster that would not be obvious from any single entry. ' +
577
+ 'Return JSON: {"insights":[{"cluster":N,"insight":"...","confidence":0.0-1.0}]}. ' +
578
+ 'Only include high-value insights (confidence >= 0.7). Output JSON only, no prose.';
579
+ const userContent = `Memory clusters to analyse:\n${JSON.stringify(clusterDescriptions, null, 2)}`;
580
+ const rawResponse = await callLlm(systemPrompt, userContent);
581
+ if (!rawResponse)
582
+ return { clustersProcessed: validClusters.length, insightsStored: 0 };
583
+ const parsed = parseJson(rawResponse);
584
+ if (!parsed || !Array.isArray(parsed.insights)) {
585
+ return { clustersProcessed: validClusters.length, insightsStored: 0 };
586
+ }
587
+ let insightsStored = 0;
588
+ const now = new Date().toISOString().replace('T', ' ').slice(0, 19);
589
+ for (const insight of parsed.insights) {
590
+ if (!insight.insight?.trim())
591
+ continue;
592
+ const confidence = Math.max(0, Math.min(1, insight.confidence ?? 0.7));
593
+ if (confidence < 0.7)
594
+ continue;
595
+ try {
596
+ // Store as a learning (cross-cutting insights are learnings, not observations)
597
+ await storeLearning(projectRoot, {
598
+ insight: insight.insight.slice(0, 500),
599
+ source: SLEEP_SOURCE,
600
+ confidence,
601
+ actionable: true,
602
+ application: 'Cross-cutting insight synthesized from clustered observations',
603
+ });
604
+ insightsStored++;
605
+ }
606
+ catch {
607
+ /* best-effort */
608
+ }
609
+ }
610
+ // Log the run itself as an observation for traceability
611
+ if (insightsStored > 0) {
612
+ try {
613
+ const runId = `O-${randomBytes(4).toString('hex')}`;
614
+ nativeDb
615
+ .prepare(`
616
+ INSERT INTO brain_observations
617
+ (id, type, title, narrative, source_type, quality_score, memory_tier, created_at)
618
+ VALUES (?, 'change', ?, ?, ?, 0.6, 'short', ?)
619
+ `)
620
+ .run(runId, `[sleep-consolidation] Insight generation run`, `Generated ${insightsStored} cross-cutting insights from ${validClusters.length} clusters.`, SLEEP_SOURCE, now);
621
+ }
622
+ catch {
623
+ /* best-effort */
624
+ }
625
+ }
626
+ return { clustersProcessed: validClusters.length, insightsStored };
627
+ }
628
+ // ============================================================================
629
+ // Main entry point
630
+ // ============================================================================
631
+ /**
632
+ * Run the full sleep-time consolidation pipeline for CLEO BRAIN.
633
+ *
634
+ * This is the main entry point for LLM-driven background memory hygiene.
635
+ * It is designed to run after session end (via setImmediate) and must never
636
+ * throw — all errors are caught and logged.
637
+ *
638
+ * Steps (in order):
639
+ * 1. Merge duplicates — embedding-similarity-based dedup with LLM confirmation
640
+ * 2. Prune stale — evict low-quality short-tier entries; LLM may preserve some
641
+ * 3. Strengthen patterns — synthesize frequently-cited learnings into patterns
642
+ * 4. Generate insights — extract cross-cutting insights from observation clusters
643
+ *
644
+ * Graceful degradation: when no Anthropic API key is available, LLM steps
645
+ * silently skip their LLM call and fall back to structural heuristics.
646
+ *
647
+ * @param projectRoot - Project root directory for brain.db resolution.
648
+ * @returns Aggregated result counts from each step.
649
+ */
650
+ export async function runSleepConsolidation(projectRoot) {
651
+ const empty = {
652
+ ran: false,
653
+ mergeDuplicates: { merged: 0, llmDecisions: 0 },
654
+ pruneStale: { pruned: 0, preserved: 0 },
655
+ strengthenPatterns: { synthesized: 0, patternsGenerated: 0 },
656
+ generateInsights: { clustersProcessed: 0, insightsStored: 0 },
657
+ };
658
+ // Check configuration
659
+ let config;
660
+ try {
661
+ config = await loadSleepConfig(projectRoot);
662
+ }
663
+ catch {
664
+ config = { enabled: true };
665
+ }
666
+ if (!config.enabled) {
667
+ return empty;
668
+ }
669
+ const result = {
670
+ ran: true,
671
+ mergeDuplicates: { merged: 0, llmDecisions: 0 },
672
+ pruneStale: { pruned: 0, preserved: 0 },
673
+ strengthenPatterns: { synthesized: 0, patternsGenerated: 0 },
674
+ generateInsights: { clustersProcessed: 0, insightsStored: 0 },
675
+ };
676
+ // Step 1: Merge duplicates
677
+ try {
678
+ result.mergeDuplicates = await stepMergeDuplicates(projectRoot);
679
+ }
680
+ catch (err) {
681
+ console.warn('[sleep-consolidation] Step 1 (merge duplicates) failed:', err);
682
+ }
683
+ // Step 2: Prune stale
684
+ try {
685
+ result.pruneStale = await stepPruneStale(projectRoot);
686
+ }
687
+ catch (err) {
688
+ console.warn('[sleep-consolidation] Step 2 (prune stale) failed:', err);
689
+ }
690
+ // Step 3: Strengthen patterns
691
+ try {
692
+ result.strengthenPatterns = await stepStrengthenPatterns(projectRoot);
693
+ }
694
+ catch (err) {
695
+ console.warn('[sleep-consolidation] Step 3 (strengthen patterns) failed:', err);
696
+ }
697
+ // Step 4: Generate insights
698
+ try {
699
+ result.generateInsights = await stepGenerateInsights(projectRoot);
700
+ }
701
+ catch (err) {
702
+ console.warn('[sleep-consolidation] Step 4 (generate insights) failed:', err);
703
+ }
704
+ return result;
705
+ }
706
+ //# sourceMappingURL=sleep-consolidation.js.map