claude-flow 3.22.0 → 3.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,252 +1,1048 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * Intelligence Layer Stub (ADR-050)
4
- * Minimal fallback — full version is copied from package source.
5
- * Provides: init, getContext, recordEdit, feedback, consolidate
3
+ * Intelligence Layer (ADR-050)
4
+ *
5
+ * Closes the intelligence loop by wiring PageRank-ranked memory into
6
+ * the hook system. Pure CJS — no ESM imports of @claude-flow/memory.
7
+ *
8
+ * Data files (all under .claude-flow/data/):
9
+ * auto-memory-store.json — written by auto-memory-hook.mjs
10
+ * graph-state.json — serialized graph (nodes + edges + pageRanks)
11
+ * ranked-context.json — pre-computed ranked entries for fast lookup
12
+ * pending-insights.jsonl — append-only edit/task log
6
13
  */
14
+
7
15
  'use strict';
8
16
 
9
17
  const fs = require('fs');
10
18
  const path = require('path');
11
- const os = require('os');
12
19
 
13
20
  const DATA_DIR = path.join(process.cwd(), '.claude-flow', 'data');
14
21
  const STORE_PATH = path.join(DATA_DIR, 'auto-memory-store.json');
22
+ const GRAPH_PATH = path.join(DATA_DIR, 'graph-state.json');
15
23
  const RANKED_PATH = path.join(DATA_DIR, 'ranked-context.json');
16
24
  const PENDING_PATH = path.join(DATA_DIR, 'pending-insights.jsonl');
17
25
  const SESSION_DIR = path.join(process.cwd(), '.claude-flow', 'sessions');
18
26
  const SESSION_FILE = path.join(SESSION_DIR, 'current.json');
19
27
 
20
28
  // ── Safety limits (fixes #1530, #1531) ─────────────────────────────────────
21
- var MAX_DATA_FILE_SIZE = 10 * 1024 * 1024; // 10 MB — skip files larger than this
22
- var MAX_GRAPH_NODES = 5000; // skip PageRank if graph exceeds this
29
+ const MAX_DATA_FILE_SIZE = 10 * 1024 * 1024; // 10 MB — skip files larger than this
30
+ const MAX_GRAPH_NODES = 5000; // skip PageRank if graph exceeds this
23
31
 
24
- function ensureDir(dir) {
25
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
26
- }
32
+ // ── Stop words for trigram matching ──────────────────────────────────────────
27
33
 
28
- function readJSON(p) {
29
- // Safety: skip files exceeding MAX_DATA_FILE_SIZE (#1531)
30
- try { var stat = fs.statSync(p); if (stat.size > MAX_DATA_FILE_SIZE) { process.stderr.write("[INTELLIGENCE] WARN: Skipping " + path.basename(p) + " (" + Math.round(stat.size / 1048576) + "MB exceeds 10MB limit)\n"); return null; } } catch(e) { /* file may not exist */ }
31
- try { return fs.existsSync(p) ? JSON.parse(fs.readFileSync(p, "utf-8")) : null; }
32
- catch { return null; }
33
- }
34
+ const STOP_WORDS = new Set([
35
+ 'the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
36
+ 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could',
37
+ 'should', 'may', 'might', 'shall', 'can', 'to', 'of', 'in', 'for',
38
+ 'on', 'with', 'at', 'by', 'from', 'as', 'into', 'through', 'during',
39
+ 'before', 'after', 'and', 'but', 'or', 'nor', 'not', 'so', 'yet',
40
+ 'both', 'either', 'neither', 'each', 'every', 'all', 'any', 'few',
41
+ 'more', 'most', 'other', 'some', 'such', 'no', 'only', 'own', 'same',
42
+ 'than', 'too', 'very', 'just', 'because', 'if', 'when', 'which',
43
+ 'who', 'whom', 'this', 'that', 'these', 'those', 'it', 'its',
44
+ ]);
34
45
 
35
- function writeJSON(p, data) {
36
- ensureDir(path.dirname(p));
37
- fs.writeFileSync(p, JSON.stringify(data, null, 2), "utf-8");
46
+ // ── Helpers ──────────────────────────────────────────────────────────────────
47
+
48
+ function ensureDataDir() {
49
+ if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
38
50
  }
39
51
 
40
- function sessionGet(key) {
41
- var session = readJSON(SESSION_FILE);
42
- if (!session) return null;
43
- return key ? (session.context || {})[key] : session.context;
52
+ function readJSON(filePath) {
53
+ // Safety: skip files exceeding MAX_DATA_FILE_SIZE (#1531)
54
+ try {
55
+ const stat = fs.statSync(filePath);
56
+ if (stat.size > MAX_DATA_FILE_SIZE) {
57
+ process.stderr.write("[INTELLIGENCE] WARN: Skipping " + path.basename(filePath) + " (" + Math.round(stat.size / 1048576) + "MB exceeds 10MB limit)\n");
58
+ return null;
59
+ }
60
+ } catch { /* file may not exist yet */ }
61
+ try {
62
+ if (fs.existsSync(filePath)) return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
63
+ } catch { /* corrupt file — start fresh */ }
64
+ return null;
44
65
  }
45
66
 
46
- function sessionSet(key, value) {
47
- var session = readJSON(SESSION_FILE);
48
- if (!session) return;
49
- if (!session.context) session.context = {};
50
- session.context[key] = value;
51
- writeJSON(SESSION_FILE, session);
67
+ function writeJSON(filePath, data) {
68
+ ensureDataDir();
69
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
52
70
  }
53
71
 
54
72
  function tokenize(text) {
55
73
  if (!text) return [];
56
- return text.toLowerCase().replace(/[^a-z0-9\s]/g, " ").split(/\s+/).filter(function(w) { return w.length > 2; });
74
+ return text.toLowerCase()
75
+ .replace(/[^a-z0-9\s-]/g, ' ')
76
+ .split(/\s+/)
77
+ .filter(w => w.length > 2 && !STOP_WORDS.has(w));
57
78
  }
58
79
 
59
- // ── Truncation transparency (FIX 4) ─────────────────────────────────────────
60
- // Silently severing stored values means later reasoning can be built on
61
- // incomplete text. Warn (debug-gated) and mark the cut point with an ellipsis
62
- // so it is visible that the value was clipped.
63
- var DEBUG = !!(process.env.RUFLO_DEBUG || process.env.DEBUG);
64
- function clip(text, max, label) {
65
- text = text == null ? "" : String(text);
66
- if (text.length <= max) return text;
67
- if (DEBUG) {
68
- process.stderr.write("[INTELLIGENCE] WARN: truncated " + (label || "value") +
69
- " from " + text.length + " to " + max + " chars\n");
80
+ function trigrams(words) {
81
+ const t = new Set();
82
+ for (const w of words) {
83
+ for (let i = 0; i <= w.length - 3; i++) t.add(w.slice(i, i + 3));
70
84
  }
71
- return text.slice(0, max - 1) + "…";
85
+ return t;
86
+ }
87
+
88
+ function jaccardSimilarity(setA, setB) {
89
+ if (setA.size === 0 && setB.size === 0) return 0;
90
+ let intersection = 0;
91
+ for (const item of setA) { if (setB.has(item)) intersection++; }
92
+ return intersection / (setA.size + setB.size - intersection);
72
93
  }
73
94
 
74
95
  // ── Deduplication helper (fixes #1518) ──────────────────────────────────────
96
+
75
97
  function deduplicateById(entries) {
76
98
  if (!entries || !Array.isArray(entries)) return entries;
77
- var seen = new Map();
78
- for (var i = 0; i < entries.length; i++) {
79
- var id = entries[i].id || entries[i].key;
99
+ const seen = new Map();
100
+ for (const entry of entries) {
101
+ const id = entry.id || entry.key;
80
102
  if (id) {
81
- seen.set(id, entries[i]);
103
+ seen.set(id, entry);
82
104
  } else {
83
- seen.set('__no_id_' + seen.size, entries[i]);
105
+ seen.set(`__no_id_${seen.size}`, entry);
84
106
  }
85
107
  }
86
108
  return Array.from(seen.values());
87
109
  }
88
110
 
111
+ // ADR-095 G6 — content-hash dedup. The April audit measured 5,706 entries
112
+ // in the auto-memory store with only ~20 unique by content; 5,686 dupes
113
+ // were the same MEMORY.md sections imported from sibling project dirs
114
+ // with different IDs. deduplicateById can't catch these (the IDs really
115
+ // are different); we need a content fingerprint.
116
+ //
117
+ // Fast non-cryptographic fingerprint — collisions on 64-bit FNV-1a are
118
+ // vanishingly rare for human prose at the scale of an auto-memory store.
119
+ // Whitespace-normalized so trivially-different formatting doesn't bypass dedup.
120
+ function fingerprintContent(text) {
121
+ if (typeof text !== 'string' || text.length === 0) return '0';
122
+ const norm = text.replace(/\s+/g, ' ').trim().toLowerCase();
123
+ // FNV-1a 64-bit (split into 32-bit halves to stay within Number safe int)
124
+ let h1 = 0x811c9dc5, h2 = 0xcbf29ce4;
125
+ for (let i = 0; i < norm.length; i++) {
126
+ const c = norm.charCodeAt(i);
127
+ h1 ^= c; h1 = Math.imul(h1, 0x01000193) >>> 0;
128
+ h2 ^= c; h2 = Math.imul(h2, 0x100000001b3 & 0xffffffff) >>> 0;
129
+ }
130
+ return `${h1.toString(16)}_${h2.toString(16)}_${norm.length}`;
131
+ }
132
+
133
+ function deduplicateByContent(entries) {
134
+ if (!entries || !Array.isArray(entries)) return entries;
135
+ const seen = new Map();
136
+ for (const entry of entries) {
137
+ const content = entry.content || entry.summary || entry.value || '';
138
+ const fp = fingerprintContent(typeof content === 'string' ? content : JSON.stringify(content));
139
+ if (!seen.has(fp)) {
140
+ seen.set(fp, entry);
141
+ } else {
142
+ // Keep the entry with the higher accessCount or earlier createdAt
143
+ const existing = seen.get(fp);
144
+ const existingAccess = existing.accessCount || 0;
145
+ const candidateAccess = entry.accessCount || 0;
146
+ if (candidateAccess > existingAccess) seen.set(fp, entry);
147
+ }
148
+ }
149
+ return Array.from(seen.values());
150
+ }
151
+
152
+ // ── Session state helpers ────────────────────────────────────────────────────
153
+
154
+ function sessionGet(key) {
155
+ try {
156
+ if (!fs.existsSync(SESSION_FILE)) return null;
157
+ const session = JSON.parse(fs.readFileSync(SESSION_FILE, 'utf-8'));
158
+ return key ? (session.context || {})[key] : session.context;
159
+ } catch { return null; }
160
+ }
161
+
162
+ function sessionSet(key, value) {
163
+ try {
164
+ if (!fs.existsSync(SESSION_DIR)) fs.mkdirSync(SESSION_DIR, { recursive: true });
165
+ let session = {};
166
+ if (fs.existsSync(SESSION_FILE)) {
167
+ session = JSON.parse(fs.readFileSync(SESSION_FILE, 'utf-8'));
168
+ }
169
+ if (!session.context) session.context = {};
170
+ session.context[key] = value;
171
+ session.updatedAt = new Date().toISOString();
172
+ fs.writeFileSync(SESSION_FILE, JSON.stringify(session, null, 2), 'utf-8');
173
+ } catch { /* best effort */ }
174
+ }
175
+
176
+ // ── PageRank ─────────────────────────────────────────────────────────────────
177
+
178
+ function computePageRank(nodes, edges, damping, maxIter) {
179
+ damping = damping || 0.85;
180
+ maxIter = maxIter || 30;
181
+
182
+ const ids = Object.keys(nodes);
183
+ const n = ids.length;
184
+ if (n === 0) return {};
185
+
186
+ // Build adjacency: outgoing edges per node
187
+ const outLinks = {};
188
+ const inLinks = {};
189
+ for (const id of ids) { outLinks[id] = []; inLinks[id] = []; }
190
+ for (const edge of edges) {
191
+ if (outLinks[edge.sourceId]) outLinks[edge.sourceId].push(edge.targetId);
192
+ if (inLinks[edge.targetId]) inLinks[edge.targetId].push(edge.sourceId);
193
+ }
194
+
195
+ // Initialize ranks
196
+ const ranks = {};
197
+ for (const id of ids) ranks[id] = 1 / n;
198
+
199
+ // Power iteration (with dangling node redistribution)
200
+ for (let iter = 0; iter < maxIter; iter++) {
201
+ const newRanks = {};
202
+ let diff = 0;
203
+
204
+ // Collect rank from dangling nodes (no outgoing edges)
205
+ let danglingSum = 0;
206
+ for (const id of ids) {
207
+ if (outLinks[id].length === 0) danglingSum += ranks[id];
208
+ }
209
+
210
+ for (const id of ids) {
211
+ let sum = 0;
212
+ for (const src of inLinks[id]) {
213
+ const outCount = outLinks[src].length;
214
+ if (outCount > 0) sum += ranks[src] / outCount;
215
+ }
216
+ // Dangling rank distributed evenly + teleport
217
+ newRanks[id] = (1 - damping) / n + damping * (sum + danglingSum / n);
218
+ diff += Math.abs(newRanks[id] - ranks[id]);
219
+ }
220
+
221
+ for (const id of ids) ranks[id] = newRanks[id];
222
+ if (diff < 1e-6) break; // converged
223
+ }
224
+
225
+ return ranks;
226
+ }
227
+
228
+ // ── Edge building ────────────────────────────────────────────────────────────
229
+
230
+ function buildEdges(entries) {
231
+ const edges = [];
232
+ const byCategory = {};
233
+
234
+ for (const entry of entries) {
235
+ const cat = entry.category || entry.namespace || 'default';
236
+ if (!byCategory[cat]) byCategory[cat] = [];
237
+ byCategory[cat].push(entry);
238
+ }
239
+
240
+ // Temporal edges: entries from same sourceFile
241
+ const byFile = {};
242
+ for (const entry of entries) {
243
+ const file = (entry.metadata && entry.metadata.sourceFile) || null;
244
+ if (file) {
245
+ if (!byFile[file]) byFile[file] = [];
246
+ byFile[file].push(entry);
247
+ }
248
+ }
249
+ for (const file of Object.keys(byFile)) {
250
+ const group = byFile[file];
251
+ for (let i = 0; i < group.length - 1; i++) {
252
+ edges.push({
253
+ sourceId: group[i].id,
254
+ targetId: group[i + 1].id,
255
+ type: 'temporal',
256
+ weight: 0.5,
257
+ });
258
+ }
259
+ }
260
+
261
+ // Similarity edges within categories (Jaccard > 0.3).
262
+ // ADR-095 G6 perf: hoist the trigram computation outside the inner
263
+ // loop. Previously we re-tokenized + re-trigrammed group[j] for every
264
+ // i — O(n²) extra work for nothing. Now compute once per entry.
265
+ for (const cat of Object.keys(byCategory)) {
266
+ const group = byCategory[cat];
267
+ if (group.length < 2) continue;
268
+
269
+ // Cache trigram sets for every entry in the group.
270
+ const triCache = new Array(group.length);
271
+ for (let i = 0; i < group.length; i++) {
272
+ triCache[i] = trigrams(tokenize(group[i].content || group[i].summary || ''));
273
+ }
274
+
275
+ for (let i = 0; i < group.length; i++) {
276
+ const triA = triCache[i];
277
+ for (let j = i + 1; j < group.length; j++) {
278
+ const sim = jaccardSimilarity(triA, triCache[j]);
279
+ if (sim > 0.3) {
280
+ edges.push({
281
+ sourceId: group[i].id,
282
+ targetId: group[j].id,
283
+ type: 'similar',
284
+ weight: sim,
285
+ });
286
+ }
287
+ }
288
+ }
289
+ }
290
+
291
+ return edges;
292
+ }
293
+
294
+ // ── Bootstrap from MEMORY.md files ───────────────────────────────────────────
295
+
296
+ /**
297
+ * If auto-memory-store.json is empty, bootstrap by parsing MEMORY.md and
298
+ * topic files from the auto-memory directory. This removes the dependency
299
+ * on @claude-flow/memory for the initial seed.
300
+ */
89
301
  function bootstrapFromMemoryFiles() {
90
- var entries = [];
91
- // Scope to current project only (not all 51+ project dirs).
92
- // Match Claude Code's project-dir slug convention: every non-alphanumeric char
93
- // becomes '-' (e.g. "G:\\My Drive\\TJ_Vault" -> "G--My-Drive-TJ-Vault"). The old
94
- // version only stripped a leading '/' and replaced '/', so on Windows the slug
95
- // kept ':' and '\\' and never matched the real ~/.claude/projects/<slug> dir —
96
- // memory bootstrap then silently found nothing (FIX 5). Runs are NOT collapsed:
97
- // Claude emits "G--My" because ':' and '\\' each map to their own '-'.
98
- var projectSlug = process.cwd().replace(/[^a-zA-Z0-9]/g, '-');
99
- var candidates = [
100
- path.join(os.homedir(), ".claude", "projects", projectSlug, "memory"),
101
- path.join(process.cwd(), ".claude-flow", "memory"),
102
- path.join(process.cwd(), ".claude", "memory"),
302
+ const entries = [];
303
+ const cwd = process.cwd();
304
+
305
+ // Search for auto-memory directories
306
+ const candidates = [
307
+ // Claude Code auto-memory (project-scoped)
308
+ path.join(require('os').homedir(), '.claude', 'projects'),
309
+ // Local project memory
310
+ path.join(cwd, '.claude-flow', 'memory'),
311
+ path.join(cwd, '.claude', 'memory'),
103
312
  ];
104
- for (var i = 0; i < candidates.length; i++) {
105
- try {
106
- if (!fs.existsSync(candidates[i])) continue;
107
- var files = [];
313
+
314
+ // Find MEMORY.md in project-scoped dirs
315
+ for (const base of candidates) {
316
+ if (!fs.existsSync(base)) continue;
317
+
318
+ // For the projects dir, scope to CURRENT project only (not all 51+ dirs)
319
+ if (base.endsWith('projects')) {
108
320
  try {
109
- var items = fs.readdirSync(candidates[i], { withFileTypes: true, recursive: true });
110
- for (var j = 0; j < items.length; j++) {
111
- if (items[j].name === "MEMORY.md") {
112
- var parentDir = items[j].parentPath || items[j].path || candidates[i];
113
- var fp = path.join(parentDir, items[j].name);
114
- files.push(fp);
115
- }
321
+ // Match Claude Code's project-dir slug: every non-alphanumeric char -> '-'
322
+ // (e.g. "G:\\My Drive\\TJ_Vault" -> "G--My-Drive-TJ-Vault"). The old version
323
+ // only handled POSIX '/', so on Windows the slug kept ':' and '\\' and never
324
+ // matched the real <projects>/<slug>/memory dir bootstrap found nothing (FIX 5).
325
+ const projectSlug = cwd.replace(/[^a-zA-Z0-9]/g, '-');
326
+ const memDir = path.join(base, projectSlug, 'memory');
327
+ if (fs.existsSync(memDir)) {
328
+ parseMemoryDir(memDir, entries);
116
329
  }
117
- } catch (e) { continue; }
118
- for (var k = 0; k < files.length; k++) {
119
- try {
120
- var content = fs.readFileSync(files[k], "utf-8");
121
- var sections = content.split(/^##\s+/m).filter(function(s) { return s.trim().length > 20; });
122
- for (var s = 0; s < sections.length; s++) {
123
- var lines = sections[s].split("\n");
124
- var title = lines[0] ? lines[0].trim() : "section-" + s;
125
- var clippedContent = clip(sections[s], 500, "memory content");
126
- entries.push({
127
- id: "mem-" + entries.length,
128
- content: clippedContent,
129
- summary: clip(title, 100, "memory summary"),
130
- category: "memory",
131
- confidence: 0.5,
132
- sourceFile: files[k],
133
- words: tokenize(clippedContent),
134
- });
135
- }
136
- } catch (e) { /* skip */ }
137
- }
138
- } catch (e) { /* skip */ }
330
+ } catch { /* skip */ }
331
+ } else if (fs.existsSync(base)) {
332
+ parseMemoryDir(base, entries);
333
+ }
139
334
  }
335
+
140
336
  return entries;
141
337
  }
142
338
 
143
- function loadEntries() {
144
- var store = readJSON(STORE_PATH);
145
- // Support both formats: flat array or { entries: [...] }
146
- var entries = null;
147
- if (store) {
148
- if (Array.isArray(store) && store.length > 0) {
149
- entries = store;
150
- } else if (store.entries && store.entries.length > 0) {
151
- entries = store.entries;
339
+ // Truncation transparency (FIX 4): mark the cut with an ellipsis and warn under
340
+ // debug, so later reasoning isn't silently built on severed text.
341
+ const CLIP_DEBUG = !!(process.env.RUFLO_DEBUG || process.env.DEBUG);
342
+ function clip(text, max, label) {
343
+ text = text == null ? '' : String(text);
344
+ if (text.length <= max) return text;
345
+ if (CLIP_DEBUG) process.stderr.write(`[INTELLIGENCE] WARN: truncated ${label || 'value'} from ${text.length} to ${max} chars\n`);
346
+ return text.slice(0, max - 1) + '…';
347
+ }
348
+
349
+ function parseMemoryDir(dir, entries) {
350
+ try {
351
+ const files = fs.readdirSync(dir).filter(f => f.endsWith('.md'));
352
+ for (const file of files) {
353
+ const filePath = path.join(dir, file);
354
+ const content = fs.readFileSync(filePath, 'utf-8');
355
+ if (!content.trim()) continue;
356
+
357
+ // Parse markdown sections as separate entries
358
+ const sections = content.split(/^##?\s+/m).filter(Boolean);
359
+ for (let sIdx = 0; sIdx < sections.length; sIdx++) {
360
+ const section = sections[sIdx];
361
+ const lines = section.trim().split('\n');
362
+ const title = lines[0].trim();
363
+ const body = lines.slice(1).join('\n').trim();
364
+ if (!body || body.length < 10) continue;
365
+
366
+ const id = `mem-${file.replace('.md', '')}-${title.replace(/[^a-z0-9]/gi, '-').toLowerCase().slice(0, 30)}-${sIdx}`;
367
+ entries.push({
368
+ id,
369
+ key: title.toLowerCase().replace(/[^a-z0-9]+/g, '-').slice(0, 50),
370
+ content: clip(body, 500, 'memory content'),
371
+ summary: title,
372
+ namespace: file === 'MEMORY.md' ? 'core' : file.replace('.md', ''),
373
+ type: 'semantic',
374
+ metadata: { sourceFile: filePath, bootstrapped: true },
375
+ createdAt: Date.now(),
376
+ });
377
+ }
378
+ }
379
+ } catch { /* skip unreadable dirs */ }
380
+ }
381
+
382
+ // ── Exported functions ───────────────────────────────────────────────────────
383
+
384
+ /**
385
+ * init() — Called from session-restore. Budget: <200ms.
386
+ * Reads auto-memory-store.json, builds graph, computes PageRank, writes caches.
387
+ * If store is empty, bootstraps from MEMORY.md files directly.
388
+ */
389
+ function init() {
390
+ ensureDataDir();
391
+
392
+ // Check if graph-state.json is fresh (within 60s of store)
393
+ const graphState = readJSON(GRAPH_PATH);
394
+ let store = readJSON(STORE_PATH);
395
+
396
+ // Bootstrap from MEMORY.md files if store is empty
397
+ if (!store || !Array.isArray(store) || store.length === 0) {
398
+ const bootstrapped = bootstrapFromMemoryFiles();
399
+ if (bootstrapped.length > 0) {
400
+ store = bootstrapped;
401
+ writeJSON(STORE_PATH, store);
402
+ } else {
403
+ return { nodes: 0, edges: 0, message: 'No memory entries to index' };
152
404
  }
153
405
  }
154
- if (entries) {
155
- return entries.map(function(e, i) {
406
+
407
+ // Deduplicate store entries by ID (fixes #1518 — 194MB → ~79KB)
408
+ let deduped = deduplicateById(store);
409
+ // ADR-095 G6: also dedupe by content fingerprint. The April audit
410
+ // measured 5,706 entries with only ~20 unique by content because the
411
+ // same MEMORY.md sections get imported from sibling project dirs with
412
+ // different IDs. deduplicateById can't catch that; deduplicateByContent
413
+ // can. Cuts the graph from O(n²) over near-identical duplicates down
414
+ // to O(unique²), which is the difference between a 100MB graph-state
415
+ // and a kilobytes-scale one for typical workloads.
416
+ const beforeContentDedup = deduped.length;
417
+ deduped = deduplicateByContent(deduped);
418
+ if (deduped.length < store.length) {
419
+ process.stderr.write(
420
+ `[INTELLIGENCE] Deduped store: ${store.length} -> ${deduped.length} entries ` +
421
+ `(by-id: ${store.length - beforeContentDedup} dropped, by-content: ${beforeContentDedup - deduped.length} dropped)\n`
422
+ );
423
+ writeJSON(STORE_PATH, deduped);
424
+ }
425
+
426
+ // Skip rebuild if graph is fresh and store hasn't changed
427
+ if (graphState && graphState.nodeCount === deduped.length) {
428
+ const age = Date.now() - (graphState.updatedAt || 0);
429
+ if (age < 60000) {
156
430
  return {
157
- id: e.id || ("entry-" + i),
158
- content: e.content || e.value || "",
159
- summary: e.summary || e.key || "",
160
- category: e.category || e.namespace || "default",
161
- confidence: e.confidence || 0.5,
162
- sourceFile: e.sourceFile || (e.metadata && e.metadata.sourceFile) || "",
163
- words: tokenize((e.content || e.value || "") + " " + (e.summary || e.key || "")),
431
+ nodes: graphState.nodeCount || Object.keys(graphState.nodes || {}).length,
432
+ edges: (graphState.edges || []).length,
433
+ message: 'Graph cache hit',
164
434
  };
165
- });
166
- }
167
- return bootstrapFromMemoryFiles();
168
- }
169
-
170
- function matchScore(promptWords, entryWords) {
171
- if (!promptWords.length || !entryWords.length) return 0;
172
- var entrySet = {};
173
- for (var i = 0; i < entryWords.length; i++) entrySet[entryWords[i]] = true;
174
- var overlap = 0;
175
- for (var j = 0; j < promptWords.length; j++) {
176
- if (entrySet[promptWords[j]]) overlap++;
177
- }
178
- var union = Object.keys(entrySet).length + promptWords.length - overlap;
179
- return union > 0 ? overlap / union : 0;
180
- }
181
-
182
- var cachedEntries = null;
183
-
184
- module.exports = {
185
- init: function() {
186
- cachedEntries = deduplicateById(loadEntries());
187
- var ranked = cachedEntries.map(function(e) {
188
- return { id: e.id, content: e.content, summary: e.summary, category: e.category, confidence: e.confidence, words: e.words };
189
- });
190
- writeJSON(RANKED_PATH, { version: 1, computedAt: Date.now(), entries: ranked });
191
- return { nodes: cachedEntries.length, edges: 0 };
192
- },
193
-
194
- getContext: function(prompt) {
195
- if (!prompt) return null;
196
- var ranked = readJSON(RANKED_PATH);
197
- var entries = (ranked && ranked.entries) || (cachedEntries || []);
198
- if (!entries.length) return null;
199
- var promptWords = tokenize(prompt);
200
- if (!promptWords.length) return null;
201
- var scored = entries.map(function(e) {
202
- return { entry: e, score: matchScore(promptWords, e.words || tokenize(e.content + " " + e.summary)) };
203
- }).filter(function(s) { return s.score > 0.05; });
204
- scored.sort(function(a, b) { return b.score - a.score; });
205
- var top = scored.slice(0, 5);
206
- if (!top.length) return null;
207
- var prevMatched = sessionGet("lastMatchedPatterns");
208
- var matchedIds = top.map(function(s) { return s.entry.id; });
209
- sessionSet("lastMatchedPatterns", matchedIds);
210
- var lines = ["[INTELLIGENCE] Relevant patterns for this task:"];
211
- for (var j = 0; j < top.length; j++) {
212
- var e = top[j];
213
- var conf = e.entry.confidence || 0.5;
214
- var summary = (e.entry.summary || e.entry.content || "").substring(0, 80);
215
- lines.push(" * (" + conf.toFixed(2) + ") " + summary);
216
- }
217
- return lines.join("\n");
218
- },
219
-
220
- recordEdit: function(file) {
221
- if (!file) return;
222
- ensureDir(DATA_DIR);
223
- var line = JSON.stringify({ type: "edit", file: file, timestamp: Date.now() }) + "\n";
224
- fs.appendFileSync(PENDING_PATH, line, "utf-8");
225
- },
226
-
227
- feedback: function(success) {
228
- // Stub: no-op in minimal version
229
- },
230
-
231
- consolidate: function() {
232
- var count = 0;
233
- if (fs.existsSync(PENDING_PATH)) {
435
+ }
436
+ }
437
+
438
+ // Build nodes from deduped entries
439
+ const nodes = {};
440
+ for (const entry of deduped) {
441
+ const id = entry.id || entry.key || `entry-${Math.random().toString(36).slice(2, 8)}`;
442
+ nodes[id] = {
443
+ id,
444
+ category: entry.namespace || entry.type || 'default',
445
+ confidence: (entry.metadata && entry.metadata.confidence) || 0.5,
446
+ accessCount: (entry.metadata && entry.metadata.accessCount) || 0,
447
+ createdAt: entry.createdAt || Date.now(),
448
+ };
449
+ // Ensure entry has id for edge building
450
+ entry.id = id;
451
+ }
452
+
453
+ // Build edges
454
+ const edges = buildEdges(deduped);
455
+
456
+ // Compute PageRank (skip if graph too large — #1531)
457
+ const nodeCount = Object.keys(nodes).length;
458
+ let pageRanks = {};
459
+ if (nodeCount > MAX_GRAPH_NODES) {
460
+ process.stderr.write("[INTELLIGENCE] WARN: Graph has " + nodeCount + " nodes (>" + MAX_GRAPH_NODES + "), skipping PageRank\n");
461
+ for (const id of Object.keys(nodes)) pageRanks[id] = 1 / nodeCount;
462
+ } else {
463
+ pageRanks = computePageRank(nodes, edges, 0.85, 30);
464
+ }
465
+
466
+ // Write graph state
467
+ const graph = {
468
+ version: 1,
469
+ updatedAt: Date.now(),
470
+ nodeCount: Object.keys(nodes).length,
471
+ nodes,
472
+ edges,
473
+ pageRanks,
474
+ };
475
+ writeJSON(GRAPH_PATH, graph);
476
+
477
+ // Build ranked context for fast lookup
478
+ const rankedEntries = deduped.map(entry => {
479
+ const id = entry.id;
480
+ const content = entry.content || entry.value || '';
481
+ const summary = entry.summary || entry.key || '';
482
+ const words = tokenize(content + ' ' + summary);
483
+ return {
484
+ id,
485
+ content,
486
+ summary,
487
+ category: entry.namespace || entry.type || 'default',
488
+ confidence: nodes[id] ? nodes[id].confidence : 0.5,
489
+ pageRank: pageRanks[id] || 0,
490
+ accessCount: nodes[id] ? nodes[id].accessCount : 0,
491
+ words,
492
+ };
493
+ }).sort((a, b) => {
494
+ const scoreA = 0.6 * a.pageRank + 0.4 * a.confidence;
495
+ const scoreB = 0.6 * b.pageRank + 0.4 * b.confidence;
496
+ return scoreB - scoreA;
497
+ });
498
+
499
+ const ranked = {
500
+ version: 1,
501
+ computedAt: Date.now(),
502
+ entries: rankedEntries,
503
+ };
504
+ writeJSON(RANKED_PATH, ranked);
505
+
506
+ return {
507
+ nodes: Object.keys(nodes).length,
508
+ edges: edges.length,
509
+ message: 'Graph built and ranked',
510
+ };
511
+ }
512
+
513
+ /**
514
+ * getContext(prompt) — Called from route. Budget: <15ms.
515
+ * Matches prompt to ranked entries, returns top-5 formatted context.
516
+ */
517
+ function getContext(prompt) {
518
+ if (!prompt) return null;
519
+
520
+ const ranked = readJSON(RANKED_PATH);
521
+ if (!ranked || !ranked.entries || ranked.entries.length === 0) return null;
522
+
523
+ const promptWords = tokenize(prompt);
524
+ if (promptWords.length === 0) return null;
525
+ const promptTrigrams = trigrams(promptWords);
526
+
527
+ const ALPHA = 0.6; // content match weight
528
+ const MIN_THRESHOLD = 0.05;
529
+ const TOP_K = 5;
530
+
531
+ // Score each entry
532
+ const scored = [];
533
+ for (const entry of ranked.entries) {
534
+ const entryTrigrams = trigrams(entry.words || []);
535
+ const contentMatch = jaccardSimilarity(promptTrigrams, entryTrigrams);
536
+ const score = ALPHA * contentMatch + (1 - ALPHA) * (entry.pageRank || 0);
537
+ if (score >= MIN_THRESHOLD) {
538
+ scored.push({ ...entry, score });
539
+ }
540
+ }
541
+
542
+ if (scored.length === 0) return null;
543
+
544
+ // Sort by score descending, take top-K
545
+ scored.sort((a, b) => b.score - a.score);
546
+ const topEntries = scored.slice(0, TOP_K);
547
+
548
+ // Boost previously matched patterns (implicit success: user continued working)
549
+ const prevMatched = sessionGet('lastMatchedPatterns');
550
+
551
+ // Store NEW matched IDs in session state for feedback
552
+ const matchedIds = topEntries.map(e => e.id);
553
+ sessionSet('lastMatchedPatterns', matchedIds);
554
+
555
+ // Only boost previous if they differ from current (avoid double-boosting)
556
+ if (prevMatched && Array.isArray(prevMatched)) {
557
+ const newSet = new Set(matchedIds);
558
+ const toBoost = prevMatched.filter(id => !newSet.has(id));
559
+ if (toBoost.length > 0) boostConfidence(toBoost, 0.03);
560
+ }
561
+
562
+ // Format output
563
+ const lines = ['[INTELLIGENCE] Relevant patterns for this task:'];
564
+ for (let i = 0; i < topEntries.length; i++) {
565
+ const e = topEntries[i];
566
+ const display = (e.summary || e.content || '').slice(0, 80);
567
+ const accessed = e.accessCount || 0;
568
+ lines.push(` * (${e.score.toFixed(2)}) ${display} [rank #${i + 1}, ${accessed}x accessed]`);
569
+ }
570
+
571
+ return lines.join('\n');
572
+ }
573
+
574
+ /**
575
+ * recordEdit(file) — Called from post-edit. Budget: <2ms.
576
+ * Appends to pending-insights.jsonl.
577
+ */
578
+ function recordEdit(file, success) {
579
+ ensureDataDir();
580
+ const entry = JSON.stringify({
581
+ type: 'edit',
582
+ file: file || 'unknown',
583
+ // ADR-174: record failures too, not just successes — the learning substrate
584
+ // needs negative examples. Defaults true; an explicit false is a failed edit.
585
+ success: success !== false,
586
+ timestamp: Date.now(),
587
+ sessionId: sessionGet('sessionId') || null,
588
+ });
589
+ fs.appendFileSync(PENDING_PATH, entry + '\n', 'utf-8');
590
+ }
591
+
592
+ /**
593
+ * feedback(success) — Called from post-task. Budget: <10ms.
594
+ * Boosts or decays confidence for last-matched patterns.
595
+ */
596
+ function feedback(success) {
597
+ const matchedIds = sessionGet('lastMatchedPatterns');
598
+ if (!matchedIds || !Array.isArray(matchedIds)) return;
599
+
600
+ const amount = success ? 0.05 : -0.02;
601
+ boostConfidence(matchedIds, amount);
602
+ }
603
+
604
+ function boostConfidence(ids, amount) {
605
+ const ranked = readJSON(RANKED_PATH);
606
+ if (!ranked || !ranked.entries) return;
607
+
608
+ let changed = false;
609
+ for (const entry of ranked.entries) {
610
+ if (ids.includes(entry.id)) {
611
+ entry.confidence = Math.max(0, Math.min(1, (entry.confidence || 0.5) + amount));
612
+ entry.accessCount = (entry.accessCount || 0) + 1;
613
+ changed = true;
614
+ }
615
+ }
616
+
617
+ if (changed) writeJSON(RANKED_PATH, ranked);
618
+
619
+ // Also update graph-state confidence
620
+ const graph = readJSON(GRAPH_PATH);
621
+ if (graph && graph.nodes) {
622
+ for (const id of ids) {
623
+ if (graph.nodes[id]) {
624
+ graph.nodes[id].confidence = Math.max(0, Math.min(1, (graph.nodes[id].confidence || 0.5) + amount));
625
+ graph.nodes[id].accessCount = (graph.nodes[id].accessCount || 0) + 1;
626
+ }
627
+ }
628
+ writeJSON(GRAPH_PATH, graph);
629
+ }
630
+ }
631
+
632
+ /**
633
+ * consolidate() — Called from session-end. Budget: <500ms.
634
+ * Processes pending insights, rebuilds edges, recomputes PageRank.
635
+ */
636
+ function consolidate() {
637
+ ensureDataDir();
638
+
639
+ let store = readJSON(STORE_PATH);
640
+ if (!store || !Array.isArray(store)) {
641
+ return { entries: 0, edges: 0, newEntries: 0, message: 'No store to consolidate' };
642
+ }
643
+
644
+ // Deduplicate store entries by ID before processing (fixes #1518)
645
+ const preDedupCount = store.length;
646
+ store = deduplicateById(store);
647
+
648
+ // 1. Process pending insights
649
+ let newEntries = 0;
650
+ if (fs.existsSync(PENDING_PATH)) {
651
+ const lines = fs.readFileSync(PENDING_PATH, 'utf-8').trim().split('\n').filter(Boolean);
652
+ const editCounts = {};
653
+ for (const line of lines) {
234
654
  try {
235
- var content = fs.readFileSync(PENDING_PATH, "utf-8").trim();
236
- count = content ? content.split("\n").length : 0;
237
- fs.writeFileSync(PENDING_PATH, "", "utf-8");
238
- } catch (e) { /* skip */ }
239
- }
240
- return { entries: count, edges: 0, newEntries: 0 };
241
- },
242
-
243
- stats: function(json) {
244
- var ranked = readJSON(RANKED_PATH);
245
- var count = ranked && ranked.entries ? ranked.entries.length : 0;
246
- if (json) {
247
- console.log(JSON.stringify({ entries: count, computedAt: ranked ? ranked.computedAt : null }));
248
- } else {
249
- console.log('[INTELLIGENCE] Stats: ' + count + ' entries loaded');
655
+ const insight = JSON.parse(line);
656
+ if (insight.file) {
657
+ editCounts[insight.file] = (editCounts[insight.file] || 0) + 1;
658
+ }
659
+ } catch { /* skip malformed */ }
660
+ }
661
+
662
+ // Create entries for frequently-edited files (3+ edits)
663
+ for (const [file, count] of Object.entries(editCounts)) {
664
+ if (count >= 3) {
665
+ const exists = store.some(e =>
666
+ (e.metadata && e.metadata.sourceFile === file && e.metadata.autoGenerated)
667
+ );
668
+ if (!exists) {
669
+ store.push({
670
+ id: `insight-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
671
+ key: `frequent-edit-${path.basename(file)}`,
672
+ content: `File ${file} was edited ${count} times this session — likely a hot path worth monitoring.`,
673
+ summary: `Frequently edited: ${path.basename(file)} (${count}x)`,
674
+ namespace: 'insights',
675
+ type: 'procedural',
676
+ metadata: { sourceFile: file, editCount: count, autoGenerated: true },
677
+ createdAt: Date.now(),
678
+ });
679
+ newEntries++;
680
+ }
681
+ }
682
+ }
683
+
684
+ // Clear pending
685
+ fs.writeFileSync(PENDING_PATH, '', 'utf-8');
686
+ }
687
+
688
+ // 2. Confidence decay for unaccessed entries
689
+ const graph = readJSON(GRAPH_PATH);
690
+ if (graph && graph.nodes) {
691
+ const now = Date.now();
692
+ for (const id of Object.keys(graph.nodes)) {
693
+ const node = graph.nodes[id];
694
+ const hoursSinceCreation = (now - (node.createdAt || now)) / (1000 * 60 * 60);
695
+ if (node.accessCount === 0 && hoursSinceCreation > 24) {
696
+ node.confidence = Math.max(0.05, (node.confidence || 0.5) - 0.005 * Math.floor(hoursSinceCreation / 24));
697
+ }
698
+ }
699
+ }
700
+
701
+ // 3. Rebuild edges with updated store
702
+ for (const entry of store) {
703
+ if (!entry.id) entry.id = `entry-${Math.random().toString(36).slice(2, 8)}`;
704
+ }
705
+ const edges = buildEdges(store);
706
+
707
+ // 4. Build updated nodes
708
+ const nodes = {};
709
+ for (const entry of store) {
710
+ nodes[entry.id] = {
711
+ id: entry.id,
712
+ category: entry.namespace || entry.type || 'default',
713
+ confidence: (graph && graph.nodes && graph.nodes[entry.id])
714
+ ? graph.nodes[entry.id].confidence
715
+ : (entry.metadata && entry.metadata.confidence) || 0.5,
716
+ accessCount: (graph && graph.nodes && graph.nodes[entry.id])
717
+ ? graph.nodes[entry.id].accessCount
718
+ : (entry.metadata && entry.metadata.accessCount) || 0,
719
+ createdAt: entry.createdAt || Date.now(),
720
+ };
721
+ }
722
+
723
+ // 5. Recompute PageRank (skip if graph too large — #1531)
724
+ const nodeCount = Object.keys(nodes).length;
725
+ let pageRanks = {};
726
+ if (nodeCount > MAX_GRAPH_NODES) {
727
+ process.stderr.write("[INTELLIGENCE] WARN: Graph has " + nodeCount + " nodes (>" + MAX_GRAPH_NODES + "), skipping PageRank in consolidate\n");
728
+ for (const id of Object.keys(nodes)) pageRanks[id] = 1 / nodeCount;
729
+ } else {
730
+ pageRanks = computePageRank(nodes, edges, 0.85, 30);
731
+ }
732
+
733
+ // 6. Write updated graph
734
+ writeJSON(GRAPH_PATH, {
735
+ version: 1,
736
+ updatedAt: Date.now(),
737
+ nodeCount: Object.keys(nodes).length,
738
+ nodes,
739
+ edges,
740
+ pageRanks,
741
+ });
742
+
743
+ // 7. Write updated ranked context
744
+ const rankedEntries = store.map(entry => {
745
+ const id = entry.id;
746
+ const content = entry.content || entry.value || '';
747
+ const summary = entry.summary || entry.key || '';
748
+ const words = tokenize(content + ' ' + summary);
749
+ return {
750
+ id,
751
+ content,
752
+ summary,
753
+ category: entry.namespace || entry.type || 'default',
754
+ confidence: nodes[id] ? nodes[id].confidence : 0.5,
755
+ pageRank: pageRanks[id] || 0,
756
+ accessCount: nodes[id] ? nodes[id].accessCount : 0,
757
+ words,
758
+ };
759
+ }).sort((a, b) => {
760
+ const scoreA = 0.6 * a.pageRank + 0.4 * a.confidence;
761
+ const scoreB = 0.6 * b.pageRank + 0.4 * b.confidence;
762
+ return scoreB - scoreA;
763
+ });
764
+
765
+ writeJSON(RANKED_PATH, {
766
+ version: 1,
767
+ computedAt: Date.now(),
768
+ entries: rankedEntries,
769
+ });
770
+
771
+ // 8. Persist updated store (deduped or with new insight entries)
772
+ if (newEntries > 0 || store.length < preDedupCount) writeJSON(STORE_PATH, store);
773
+
774
+ // 9. Save snapshot for delta tracking
775
+ const updatedGraph = readJSON(GRAPH_PATH);
776
+ const updatedRanked = readJSON(RANKED_PATH);
777
+ saveSnapshot(updatedGraph, updatedRanked);
778
+
779
+ return {
780
+ entries: store.length,
781
+ edges: edges.length,
782
+ newEntries,
783
+ message: 'Consolidated',
784
+ };
785
+ }
786
+
787
+ // ── Snapshot for delta tracking ─────────────────────────────────────────────
788
+
789
+ const SNAPSHOT_PATH = path.join(DATA_DIR, 'intelligence-snapshot.json');
790
+
791
+ function saveSnapshot(graph, ranked) {
792
+ const snap = {
793
+ timestamp: Date.now(),
794
+ nodes: graph ? Object.keys(graph.nodes || {}).length : 0,
795
+ edges: graph ? (graph.edges || []).length : 0,
796
+ pageRankSum: 0,
797
+ confidences: [],
798
+ accessCounts: [],
799
+ topPatterns: [],
800
+ };
801
+
802
+ if (graph && graph.pageRanks) {
803
+ for (const v of Object.values(graph.pageRanks)) snap.pageRankSum += v;
804
+ }
805
+ if (graph && graph.nodes) {
806
+ for (const n of Object.values(graph.nodes)) {
807
+ snap.confidences.push(n.confidence || 0.5);
808
+ snap.accessCounts.push(n.accessCount || 0);
809
+ }
810
+ }
811
+ if (ranked && ranked.entries) {
812
+ snap.topPatterns = ranked.entries.slice(0, 10).map(e => ({
813
+ id: e.id,
814
+ summary: (e.summary || '').slice(0, 60),
815
+ confidence: e.confidence || 0.5,
816
+ pageRank: e.pageRank || 0,
817
+ accessCount: e.accessCount || 0,
818
+ }));
819
+ }
820
+
821
+ // Keep history: append to array, cap at 50
822
+ let history = readJSON(SNAPSHOT_PATH);
823
+ if (!Array.isArray(history)) history = [];
824
+ history.push(snap);
825
+ if (history.length > 50) history = history.slice(-50);
826
+ writeJSON(SNAPSHOT_PATH, history);
827
+ }
828
+
829
+ /**
830
+ * stats() — Diagnostic report showing intelligence health and improvement.
831
+ * Can be called as: node intelligence.cjs stats [--json]
832
+ */
833
+ function stats(outputJson) {
834
+ const graph = readJSON(GRAPH_PATH);
835
+ const ranked = readJSON(RANKED_PATH);
836
+ const history = readJSON(SNAPSHOT_PATH) || [];
837
+ const pending = fs.existsSync(PENDING_PATH)
838
+ ? fs.readFileSync(PENDING_PATH, 'utf-8').trim().split('\n').filter(Boolean).length
839
+ : 0;
840
+
841
+ // Current state
842
+ const nodes = graph ? Object.keys(graph.nodes || {}).length : 0;
843
+ const edges = graph ? (graph.edges || []).length : 0;
844
+ const density = nodes > 1 ? (2 * edges) / (nodes * (nodes - 1)) : 0;
845
+
846
+ // Confidence distribution
847
+ const confidences = [];
848
+ const accessCounts = [];
849
+ if (graph && graph.nodes) {
850
+ for (const n of Object.values(graph.nodes)) {
851
+ confidences.push(n.confidence || 0.5);
852
+ accessCounts.push(n.accessCount || 0);
853
+ }
854
+ }
855
+ confidences.sort((a, b) => a - b);
856
+ const confMin = confidences.length ? confidences[0] : 0;
857
+ const confMax = confidences.length ? confidences[confidences.length - 1] : 0;
858
+ const confMean = confidences.length ? confidences.reduce((s, c) => s + c, 0) / confidences.length : 0;
859
+ const confMedian = confidences.length ? confidences[Math.floor(confidences.length / 2)] : 0;
860
+
861
+ // Access stats
862
+ const totalAccess = accessCounts.reduce((s, c) => s + c, 0);
863
+ const accessedCount = accessCounts.filter(c => c > 0).length;
864
+
865
+ // PageRank stats
866
+ let prSum = 0, prMax = 0, prMaxId = '';
867
+ if (graph && graph.pageRanks) {
868
+ for (const [id, pr] of Object.entries(graph.pageRanks)) {
869
+ prSum += pr;
870
+ if (pr > prMax) { prMax = pr; prMaxId = id; }
871
+ }
872
+ }
873
+
874
+ // Top patterns by composite score
875
+ const topPatterns = (ranked && ranked.entries || []).slice(0, 10).map((e, i) => ({
876
+ rank: i + 1,
877
+ summary: (e.summary || '').slice(0, 60),
878
+ confidence: (e.confidence || 0.5).toFixed(3),
879
+ pageRank: (e.pageRank || 0).toFixed(4),
880
+ accessed: e.accessCount || 0,
881
+ score: (0.6 * (e.pageRank || 0) + 0.4 * (e.confidence || 0.5)).toFixed(4),
882
+ }));
883
+
884
+ // Edge type breakdown
885
+ const edgeTypes = {};
886
+ if (graph && graph.edges) {
887
+ for (const e of graph.edges) {
888
+ edgeTypes[e.type || 'unknown'] = (edgeTypes[e.type || 'unknown'] || 0) + 1;
889
+ }
890
+ }
891
+
892
+ // Delta from previous snapshot
893
+ let delta = null;
894
+ if (history.length >= 2) {
895
+ const prev = history[history.length - 2];
896
+ const curr = history[history.length - 1];
897
+ const elapsed = (curr.timestamp - prev.timestamp) / 1000;
898
+ const prevConfMean = prev.confidences.length
899
+ ? prev.confidences.reduce((s, c) => s + c, 0) / prev.confidences.length : 0;
900
+ const currConfMean = curr.confidences.length
901
+ ? curr.confidences.reduce((s, c) => s + c, 0) / curr.confidences.length : 0;
902
+ const prevAccess = prev.accessCounts.reduce((s, c) => s + c, 0);
903
+ const currAccess = curr.accessCounts.reduce((s, c) => s + c, 0);
904
+
905
+ delta = {
906
+ elapsed: elapsed < 3600 ? `${Math.round(elapsed / 60)}m` : `${(elapsed / 3600).toFixed(1)}h`,
907
+ nodes: curr.nodes - prev.nodes,
908
+ edges: curr.edges - prev.edges,
909
+ confidenceMean: currConfMean - prevConfMean,
910
+ totalAccess: currAccess - prevAccess,
911
+ };
912
+ }
913
+
914
+ // Trend over all history
915
+ let trend = null;
916
+ if (history.length >= 3) {
917
+ const first = history[0];
918
+ const last = history[history.length - 1];
919
+ const sessions = history.length;
920
+ const firstConfMean = first.confidences.length
921
+ ? first.confidences.reduce((s, c) => s + c, 0) / first.confidences.length : 0;
922
+ const lastConfMean = last.confidences.length
923
+ ? last.confidences.reduce((s, c) => s + c, 0) / last.confidences.length : 0;
924
+ trend = {
925
+ sessions,
926
+ nodeGrowth: last.nodes - first.nodes,
927
+ edgeGrowth: last.edges - first.edges,
928
+ confidenceDrift: lastConfMean - firstConfMean,
929
+ direction: lastConfMean > firstConfMean ? 'improving' :
930
+ lastConfMean < firstConfMean ? 'declining' : 'stable',
931
+ };
932
+ }
933
+
934
+ const report = {
935
+ graph: { nodes, edges, density: +density.toFixed(4) },
936
+ confidence: {
937
+ min: +confMin.toFixed(3), max: +confMax.toFixed(3),
938
+ mean: +confMean.toFixed(3), median: +confMedian.toFixed(3),
939
+ },
940
+ access: { total: totalAccess, patternsAccessed: accessedCount, patternsNeverAccessed: nodes - accessedCount },
941
+ pageRank: { sum: +prSum.toFixed(4), topNode: prMaxId, topNodeRank: +prMax.toFixed(4) },
942
+ edgeTypes,
943
+ pendingInsights: pending,
944
+ snapshots: history.length,
945
+ topPatterns,
946
+ delta,
947
+ trend,
948
+ };
949
+
950
+ if (outputJson) {
951
+ console.log(JSON.stringify(report, null, 2));
952
+ return report;
953
+ }
954
+
955
+ // Human-readable output
956
+ const bar = '+' + '-'.repeat(62) + '+';
957
+ console.log(bar);
958
+ console.log('|' + ' Intelligence Diagnostics (ADR-050)'.padEnd(62) + '|');
959
+ console.log(bar);
960
+ console.log('');
961
+
962
+ console.log(' Graph');
963
+ console.log(` Nodes: ${nodes}`);
964
+ console.log(` Edges: ${edges} (${Object.entries(edgeTypes).map(([t,c]) => `${c} ${t}`).join(', ') || 'none'})`);
965
+ console.log(` Density: ${(density * 100).toFixed(1)}%`);
966
+ console.log('');
967
+
968
+ console.log(' Confidence');
969
+ console.log(` Min: ${confMin.toFixed(3)}`);
970
+ console.log(` Max: ${confMax.toFixed(3)}`);
971
+ console.log(` Mean: ${confMean.toFixed(3)}`);
972
+ console.log(` Median: ${confMedian.toFixed(3)}`);
973
+ console.log('');
974
+
975
+ console.log(' Access');
976
+ console.log(` Total accesses: ${totalAccess}`);
977
+ console.log(` Patterns used: ${accessedCount}/${nodes}`);
978
+ console.log(` Never accessed: ${nodes - accessedCount}`);
979
+ console.log(` Pending insights: ${pending}`);
980
+ console.log('');
981
+
982
+ console.log(' PageRank');
983
+ console.log(` Sum: ${prSum.toFixed(4)} (should be ~1.0)`);
984
+ console.log(` Top node: ${prMaxId || '(none)'} (${prMax.toFixed(4)})`);
985
+ console.log('');
986
+
987
+ if (topPatterns.length > 0) {
988
+ console.log(' Top Patterns (by composite score)');
989
+ console.log(' ' + '-'.repeat(60));
990
+ for (const p of topPatterns) {
991
+ console.log(` #${p.rank} ${p.summary}`);
992
+ console.log(` conf=${p.confidence} pr=${p.pageRank} score=${p.score} accessed=${p.accessed}x`);
250
993
  }
251
- },
252
- };
994
+ console.log('');
995
+ }
996
+
997
+ if (delta) {
998
+ console.log(` Last Delta (${delta.elapsed} ago)`);
999
+ const sign = v => v > 0 ? `+${v}` : `${v}`;
1000
+ console.log(` Nodes: ${sign(delta.nodes)}`);
1001
+ console.log(` Edges: ${sign(delta.edges)}`);
1002
+ console.log(` Confidence: ${delta.confidenceMean >= 0 ? '+' : ''}${delta.confidenceMean.toFixed(4)}`);
1003
+ console.log(` Accesses: ${sign(delta.totalAccess)}`);
1004
+ console.log('');
1005
+ }
1006
+
1007
+ if (trend) {
1008
+ console.log(` Trend (${trend.sessions} snapshots)`);
1009
+ console.log(` Node growth: ${trend.nodeGrowth >= 0 ? '+' : ''}${trend.nodeGrowth}`);
1010
+ console.log(` Edge growth: ${trend.edgeGrowth >= 0 ? '+' : ''}${trend.edgeGrowth}`);
1011
+ console.log(` Confidence drift: ${trend.confidenceDrift >= 0 ? '+' : ''}${trend.confidenceDrift.toFixed(4)}`);
1012
+ console.log(` Direction: ${trend.direction.toUpperCase()}`);
1013
+ console.log('');
1014
+ }
1015
+
1016
+ if (!delta && !trend) {
1017
+ console.log(' No history yet — run more sessions to see deltas and trends.');
1018
+ console.log('');
1019
+ }
1020
+
1021
+ console.log(bar);
1022
+ return report;
1023
+ }
1024
+
1025
+ module.exports = { init, getContext, recordEdit, feedback, consolidate, stats };
1026
+
1027
+ // ── CLI entrypoint ──────────────────────────────────────────────────────────
1028
+ if (require.main === module) {
1029
+ const cmd = process.argv[2];
1030
+ const jsonFlag = process.argv.includes('--json');
1031
+
1032
+ const cmds = {
1033
+ init: () => { const r = init(); console.log(JSON.stringify(r)); },
1034
+ stats: () => { stats(jsonFlag); },
1035
+ consolidate: () => { const r = consolidate(); console.log(JSON.stringify(r)); },
1036
+ };
1037
+
1038
+ if (cmd && cmds[cmd]) {
1039
+ cmds[cmd]();
1040
+ } else {
1041
+ console.log('Usage: intelligence.cjs <stats|init|consolidate> [--json]');
1042
+ console.log('');
1043
+ console.log(' stats Show intelligence diagnostics and trends');
1044
+ console.log(' stats --json Output as JSON for programmatic use');
1045
+ console.log(' init Build graph and rank entries');
1046
+ console.log(' consolidate Process pending insights and recompute');
1047
+ }
1048
+ }