recallnest 1.4.1 → 2.2.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.
- package/.dockerignore +11 -0
- package/CLAUDE.md +7 -3
- package/Dockerfile +20 -0
- package/README.md +202 -130
- package/README_CN.md +263 -190
- package/ROADMAP.md +128 -81
- package/SKILL.md +250 -0
- package/assets/dashboard.png +0 -0
- package/assets/knowledge-graph.png +0 -0
- package/assets/screenshots/ui-full.png +0 -0
- package/assets/screenshots/ui-overview.png +0 -0
- package/assets/screenshots/ui-search.png +0 -0
- package/assets/ui/app.js +227 -3
- package/assets/ui/index.html +81 -11
- package/assets/ui/styles.css +178 -2
- package/bin/recallnest.mjs +62 -0
- package/bun.lock +337 -0
- package/daily-recall.sh +23 -0
- package/daily-reflections/2026-04-02.md +35 -0
- package/daily-reflections/2026-04-03.md +37 -0
- package/daily-reflections/2026-04-04.md +37 -0
- package/daily-reflections/2026-04-05.md +35 -0
- package/daily-reflections/2026-04-06.md +36 -0
- package/daily-reflections/2026-04-07.md +35 -0
- package/daily-reflections/2026-04-08.md +35 -0
- package/daily-reflections/2026-04-12.md +35 -0
- package/data/alias-map.example.json +22 -0
- package/docs/plans/2026-04-01-recall-boost.md +100 -0
- package/docs/superpowers/plans/2026-04-11-constructive-retrieval-emotion.md +1195 -0
- package/docs/superpowers/specs/2026-04-11-constructive-retrieval-emotion-design.md +398 -0
- package/glama.json +6 -0
- package/integrations/setup-all.sh +31 -0
- package/package.json +10 -3
- package/plan-philosophy-upgrades.md +363 -0
- package/plan-product-upgrades.md +155 -0
- package/plan.md +140 -0
- package/research-memory-evolution.md +440 -0
- package/research-rl-agent-memory.md +330 -0
- package/scripts/backfill-anchors.ts +130 -0
- package/scripts/diagnose-vector-search.ts +132 -0
- package/scripts/migrate-language.ts +75 -0
- package/scripts/verify-emotion-decay.ts +148 -0
- package/src/__tests__/activity-counter.test.ts +96 -0
- package/src/__tests__/admission-control.test.ts +206 -0
- package/src/__tests__/anchor-generator.test.ts +67 -0
- package/src/__tests__/babel-integration.test.ts +139 -0
- package/src/__tests__/capture-engine.test.ts +5 -3
- package/src/__tests__/cascade-forget.test.ts +1 -1
- package/src/__tests__/cc8-cc1-essential-context.test.ts +298 -0
- package/src/__tests__/circuit-breaker.test.ts +99 -0
- package/src/__tests__/cluster-consolidation.test.ts +712 -0
- package/src/__tests__/confidence-metadata.test.ts +125 -0
- package/src/__tests__/confidence-tracker.test.ts +53 -0
- package/src/__tests__/consolidation-engine.test.ts +1 -1
- package/src/__tests__/constructive-retrieval.test.ts +509 -0
- package/src/__tests__/context-collapse-renderer.test.ts +214 -0
- package/src/__tests__/context-reconstructor.test.ts +65 -0
- package/src/__tests__/conversation-importer.test.ts +692 -0
- package/src/__tests__/data-checkup.test.ts +225 -0
- package/src/__tests__/decay-exemption.test.ts +84 -0
- package/src/__tests__/dedup-guard.test.ts +39 -0
- package/src/__tests__/distill-lock.test.ts +176 -0
- package/src/__tests__/dream-pipeline.test.ts +232 -0
- package/src/__tests__/emotion-decay.test.ts +46 -0
- package/src/__tests__/emotion-detector.test.ts +56 -0
- package/src/__tests__/emotion-phase1-integration.test.ts +153 -0
- package/src/__tests__/emotion-retrieval.test.ts +56 -0
- package/src/__tests__/ethics-layer.test.ts +476 -0
- package/src/__tests__/feature-flags-integration.test.ts +54 -0
- package/src/__tests__/frequency-tracker.test.ts +113 -0
- package/src/__tests__/graph-export.test.ts +489 -0
- package/src/__tests__/interference-detector.test.ts +203 -0
- package/src/__tests__/mcp-efficiency.test.ts +288 -0
- package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +243 -0
- package/src/__tests__/memory-evolution.test.ts +271 -0
- package/src/__tests__/memory-lint.test.ts +271 -0
- package/src/__tests__/narrative-architecture.test.ts +456 -0
- package/src/__tests__/pending-review.test.ts +77 -0
- package/src/__tests__/post-retrieval-filter.test.ts +178 -0
- package/src/__tests__/prediction-engine.test.ts +349 -0
- package/src/__tests__/predictive-prospective-memory.test.ts +416 -0
- package/src/__tests__/preference-slots.test.ts +99 -0
- package/src/__tests__/query-expander-alias.test.ts +89 -0
- package/src/__tests__/recall-governor.test.ts +227 -0
- package/src/__tests__/resume-context.test.ts +156 -1
- package/src/__tests__/salience-scoring.test.ts +106 -0
- package/src/__tests__/session-distiller.test.ts +611 -0
- package/src/__tests__/skill-engine.test.ts +546 -0
- package/src/__tests__/skill-promotion.test.ts +349 -0
- package/src/__tests__/supersede-chain.test.ts +154 -0
- package/src/__tests__/temporal-validity.test.ts +160 -0
- package/src/__tests__/topic-tag.test.ts +113 -0
- package/src/__tests__/trusted-memory.test.ts +828 -0
- package/src/__tests__/write-verifier.test.ts +122 -0
- package/src/access-tracker.ts +3 -2
- package/src/activity-counter.ts +109 -0
- package/src/adaptive-retrieval.ts +6 -5
- package/src/admission-control.ts +163 -0
- package/src/anchor-generator.ts +80 -0
- package/src/api-server.ts +10 -0
- package/src/audit-log.ts +122 -0
- package/src/auto-gc.ts +98 -29
- package/src/auto-recall.ts +33 -4
- package/src/capture-engine.ts +372 -23
- package/src/cascade-forget.ts +3 -2
- package/src/cli.ts +107 -5
- package/src/compat.ts +22 -0
- package/src/confidence-tracker.ts +210 -47
- package/src/conflict-store.ts +12 -3
- package/src/consolidation-engine.ts +296 -8
- package/src/context-collapse-renderer.ts +222 -0
- package/src/context-composer-stable-selection.ts +5 -1
- package/src/context-composer.ts +239 -4
- package/src/context-reconstructor.ts +404 -0
- package/src/conversation-importer.ts +441 -0
- package/src/data-checkup.ts +232 -0
- package/src/decay-engine.ts +84 -0
- package/src/distill-lock.ts +122 -0
- package/src/doctor.ts +8 -10
- package/src/dream-pipeline.ts +227 -0
- package/src/emotion-detector.ts +90 -0
- package/src/eval.ts +3 -2
- package/src/forget-engine.ts +269 -0
- package/src/frequency-tracker.ts +175 -0
- package/src/graph-export.ts +644 -0
- package/src/ingest.ts +36 -6
- package/src/interference-detector.ts +187 -0
- package/src/kg-extractor.ts +15 -24
- package/src/kg-store.ts +2 -1
- package/src/language-hook.ts +67 -0
- package/src/llm-client.ts +289 -6
- package/src/mcp-server.ts +707 -80
- package/src/memory-assets.ts +22 -5
- package/src/memory-evolution.ts +365 -0
- package/src/memory-lint.ts +429 -0
- package/src/memory-output.ts +69 -0
- package/src/memory-schema.ts +74 -2
- package/src/narrative-schema.ts +79 -0
- package/src/narrative-tagger.ts +164 -0
- package/src/pii-detector.ts +98 -0
- package/src/post-retrieval-filter.ts +76 -0
- package/src/prediction-engine.ts +253 -0
- package/src/preference-slots.ts +65 -2
- package/src/prospective-memory.ts +215 -0
- package/src/query-expander.ts +67 -0
- package/src/recall-governor.ts +183 -0
- package/src/retention-policy.ts +90 -0
- package/src/retriever.ts +828 -55
- package/src/rif.ts +34 -8
- package/src/runtime-config.ts +23 -8
- package/src/scope-inventory.ts +4 -3
- package/src/scope-policy.ts +4 -0
- package/src/session-distiller.ts +307 -0
- package/src/session-output.ts +40 -0
- package/src/session-schema.ts +37 -0
- package/src/session-store.ts +7 -2
- package/src/skill-engine.ts +208 -0
- package/src/skill-promotion.ts +293 -0
- package/src/skill-schema.ts +39 -0
- package/src/store.ts +96 -10
- package/src/topic-tag.ts +66 -0
- package/src/tracker.ts +3 -1
- package/src/ui-server.ts +80 -8
- package/src/workflow-observation-scope-review.ts +2 -1
- package/src/workflow-observation-store.ts +2 -1
- package/src/write-verifier.ts +126 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { buildMemoryGraph, renderGraphHTML, formatGraphExportResult } from "../graph-export.js";
|
|
3
|
+
import type { MemoryEntry, MemoryStore } from "../store.js";
|
|
4
|
+
import type { MemoryGraph } from "../graph-export.js";
|
|
5
|
+
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Helpers
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
function makeEntry(overrides: Partial<MemoryEntry> & { id: string }): MemoryEntry {
|
|
11
|
+
return {
|
|
12
|
+
text: "test memory",
|
|
13
|
+
vector: [1, 0, 0, 0, 0],
|
|
14
|
+
category: "events",
|
|
15
|
+
scope: "project:test",
|
|
16
|
+
importance: 0.7,
|
|
17
|
+
timestamp: Date.now(),
|
|
18
|
+
metadata: JSON.stringify({
|
|
19
|
+
evolution: {
|
|
20
|
+
status: "active",
|
|
21
|
+
version: 1,
|
|
22
|
+
accessCount: 3,
|
|
23
|
+
lastAccessedAt: null,
|
|
24
|
+
supersededBy: null,
|
|
25
|
+
supersedes: null,
|
|
26
|
+
evolutionNote: null,
|
|
27
|
+
consolidatedInto: null,
|
|
28
|
+
contributedToPattern: null,
|
|
29
|
+
sourceMemories: [],
|
|
30
|
+
validFrom: Date.now(),
|
|
31
|
+
validUntil: null,
|
|
32
|
+
},
|
|
33
|
+
}),
|
|
34
|
+
...overrides,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function createMockStore(entries: MemoryEntry[]): Pick<MemoryStore, "list"> & Partial<Pick<MemoryStore, "getVectors">> {
|
|
39
|
+
return {
|
|
40
|
+
async list() { return entries; },
|
|
41
|
+
async getVectors(ids: string[]) {
|
|
42
|
+
const map = new Map<string, number[]>();
|
|
43
|
+
for (const e of entries) {
|
|
44
|
+
if (ids.includes(e.id) && e.vector?.length > 0) {
|
|
45
|
+
map.set(e.id, e.vector);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return map;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// buildMemoryGraph
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
describe("buildMemoryGraph", () => {
|
|
58
|
+
it("returns nodes for active entries", async () => {
|
|
59
|
+
const entries = [
|
|
60
|
+
makeEntry({ id: "a1", text: "User prefers dark mode", category: "preferences" }),
|
|
61
|
+
makeEntry({ id: "a2", text: "Project uses Bun runtime", category: "entities" }),
|
|
62
|
+
];
|
|
63
|
+
const store = createMockStore(entries);
|
|
64
|
+
const graph = await buildMemoryGraph(store);
|
|
65
|
+
|
|
66
|
+
expect(graph.nodes).toHaveLength(2);
|
|
67
|
+
expect(graph.nodes[0].category).toBe("preferences");
|
|
68
|
+
expect(graph.nodes[1].category).toBe("entities");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("limits nodes to maxNodes", async () => {
|
|
72
|
+
const entries = Array.from({ length: 50 }, (_, i) =>
|
|
73
|
+
makeEntry({ id: `n${i}`, importance: 1 - i * 0.01 })
|
|
74
|
+
);
|
|
75
|
+
const store = createMockStore(entries);
|
|
76
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 10 });
|
|
77
|
+
|
|
78
|
+
expect(graph.nodes).toHaveLength(10);
|
|
79
|
+
// Should have the highest-importance nodes
|
|
80
|
+
expect(graph.nodes[0].importance).toBe(1);
|
|
81
|
+
expect(graph.nodes[9].importance).toBe(0.91);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("skips non-active entries", async () => {
|
|
85
|
+
const activeEntry = makeEntry({ id: "active1", text: "I am active" });
|
|
86
|
+
const supersededEntry = makeEntry({
|
|
87
|
+
id: "sup1",
|
|
88
|
+
text: "I am superseded",
|
|
89
|
+
metadata: JSON.stringify({
|
|
90
|
+
evolution: {
|
|
91
|
+
status: "superseded",
|
|
92
|
+
version: 1,
|
|
93
|
+
accessCount: 0,
|
|
94
|
+
lastAccessedAt: null,
|
|
95
|
+
supersededBy: "active1",
|
|
96
|
+
supersedes: null,
|
|
97
|
+
evolutionNote: null,
|
|
98
|
+
consolidatedInto: null,
|
|
99
|
+
contributedToPattern: null,
|
|
100
|
+
sourceMemories: [],
|
|
101
|
+
validFrom: Date.now(),
|
|
102
|
+
validUntil: Date.now(),
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
});
|
|
106
|
+
const store = createMockStore([activeEntry, supersededEntry]);
|
|
107
|
+
const graph = await buildMemoryGraph(store);
|
|
108
|
+
|
|
109
|
+
expect(graph.nodes).toHaveLength(1);
|
|
110
|
+
expect(graph.nodes[0].id).toBe("active1");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("builds supersede edges from evolution metadata", async () => {
|
|
114
|
+
const oldEntry = makeEntry({
|
|
115
|
+
id: "old1",
|
|
116
|
+
text: "Old version",
|
|
117
|
+
metadata: JSON.stringify({
|
|
118
|
+
evolution: {
|
|
119
|
+
status: "active",
|
|
120
|
+
version: 1,
|
|
121
|
+
accessCount: 0,
|
|
122
|
+
lastAccessedAt: null,
|
|
123
|
+
supersededBy: "new1",
|
|
124
|
+
supersedes: null,
|
|
125
|
+
evolutionNote: null,
|
|
126
|
+
consolidatedInto: null,
|
|
127
|
+
contributedToPattern: null,
|
|
128
|
+
sourceMemories: [],
|
|
129
|
+
validFrom: Date.now(),
|
|
130
|
+
validUntil: null,
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
});
|
|
134
|
+
const newEntry = makeEntry({
|
|
135
|
+
id: "new1",
|
|
136
|
+
text: "New version",
|
|
137
|
+
metadata: JSON.stringify({
|
|
138
|
+
evolution: {
|
|
139
|
+
status: "active",
|
|
140
|
+
version: 2,
|
|
141
|
+
accessCount: 0,
|
|
142
|
+
lastAccessedAt: null,
|
|
143
|
+
supersededBy: null,
|
|
144
|
+
supersedes: "old1",
|
|
145
|
+
evolutionNote: "Updated info",
|
|
146
|
+
consolidatedInto: null,
|
|
147
|
+
contributedToPattern: null,
|
|
148
|
+
sourceMemories: [],
|
|
149
|
+
validFrom: Date.now(),
|
|
150
|
+
validUntil: null,
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
});
|
|
154
|
+
const store = createMockStore([oldEntry, newEntry]);
|
|
155
|
+
const graph = await buildMemoryGraph(store);
|
|
156
|
+
|
|
157
|
+
const supersedeEdges = graph.edges.filter(e => e.type === "supersede");
|
|
158
|
+
expect(supersedeEdges.length).toBeGreaterThanOrEqual(1);
|
|
159
|
+
// old1 -> new1 edge
|
|
160
|
+
const edge = supersedeEdges.find(e => e.source === "old1" && e.target === "new1");
|
|
161
|
+
expect(edge).toBeDefined();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("builds cluster edges from metadata", async () => {
|
|
165
|
+
const canonical = makeEntry({
|
|
166
|
+
id: "canon1",
|
|
167
|
+
text: "Canonical entry",
|
|
168
|
+
metadata: JSON.stringify({
|
|
169
|
+
evolution: { status: "active", version: 1, accessCount: 0, lastAccessedAt: null, supersededBy: null, supersedes: null, evolutionNote: null, consolidatedInto: null, contributedToPattern: null, sourceMemories: [], validFrom: Date.now(), validUntil: null },
|
|
170
|
+
cluster_members: ["member1"],
|
|
171
|
+
}),
|
|
172
|
+
});
|
|
173
|
+
const member = makeEntry({
|
|
174
|
+
id: "member1",
|
|
175
|
+
text: "Cluster member",
|
|
176
|
+
metadata: JSON.stringify({
|
|
177
|
+
evolution: { status: "active", version: 1, accessCount: 0, lastAccessedAt: null, supersededBy: null, supersedes: null, evolutionNote: null, consolidatedInto: null, contributedToPattern: null, sourceMemories: [], validFrom: Date.now(), validUntil: null },
|
|
178
|
+
clustered_with: "canon1",
|
|
179
|
+
}),
|
|
180
|
+
});
|
|
181
|
+
const store = createMockStore([canonical, member]);
|
|
182
|
+
const graph = await buildMemoryGraph(store);
|
|
183
|
+
|
|
184
|
+
const clusterEdges = graph.edges.filter(e => e.type === "cluster");
|
|
185
|
+
expect(clusterEdges.length).toBeGreaterThanOrEqual(1);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("filters edges to existing nodes only", async () => {
|
|
189
|
+
// Entry references a supersededBy ID that is not in the node set
|
|
190
|
+
const entry = makeEntry({
|
|
191
|
+
id: "e1",
|
|
192
|
+
text: "Entry with missing reference",
|
|
193
|
+
metadata: JSON.stringify({
|
|
194
|
+
evolution: {
|
|
195
|
+
status: "active",
|
|
196
|
+
version: 1,
|
|
197
|
+
accessCount: 0,
|
|
198
|
+
lastAccessedAt: null,
|
|
199
|
+
supersededBy: "nonexistent-id",
|
|
200
|
+
supersedes: null,
|
|
201
|
+
evolutionNote: null,
|
|
202
|
+
consolidatedInto: null,
|
|
203
|
+
contributedToPattern: null,
|
|
204
|
+
sourceMemories: [],
|
|
205
|
+
validFrom: Date.now(),
|
|
206
|
+
validUntil: null,
|
|
207
|
+
},
|
|
208
|
+
}),
|
|
209
|
+
});
|
|
210
|
+
const store = createMockStore([entry]);
|
|
211
|
+
const graph = await buildMemoryGraph(store);
|
|
212
|
+
|
|
213
|
+
expect(graph.nodes).toHaveLength(1);
|
|
214
|
+
// No edges should reference nonexistent-id
|
|
215
|
+
const badEdges = graph.edges.filter(
|
|
216
|
+
e => e.source === "nonexistent-id" || e.target === "nonexistent-id"
|
|
217
|
+
);
|
|
218
|
+
expect(badEdges).toHaveLength(0);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("builds scope edges for small scope groups", async () => {
|
|
222
|
+
const entries = [
|
|
223
|
+
makeEntry({ id: "s1", scope: "project:alpha" }),
|
|
224
|
+
makeEntry({ id: "s2", scope: "project:alpha" }),
|
|
225
|
+
makeEntry({ id: "s3", scope: "project:alpha" }),
|
|
226
|
+
makeEntry({ id: "s4", scope: "project:beta" }),
|
|
227
|
+
];
|
|
228
|
+
const store = createMockStore(entries);
|
|
229
|
+
const graph = await buildMemoryGraph(store);
|
|
230
|
+
|
|
231
|
+
const scopeEdges = graph.edges.filter(e => e.type === "scope");
|
|
232
|
+
// alpha has 3 entries → chain of 2 scope edges
|
|
233
|
+
expect(scopeEdges.length).toBe(2);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("skips scope edges when scope has more than 20 entries", async () => {
|
|
237
|
+
const entries = Array.from({ length: 25 }, (_, i) =>
|
|
238
|
+
makeEntry({ id: `big${i}`, scope: "project:big" })
|
|
239
|
+
);
|
|
240
|
+
const store = createMockStore(entries);
|
|
241
|
+
const graph = await buildMemoryGraph(store);
|
|
242
|
+
|
|
243
|
+
const scopeEdges = graph.edges.filter(e => e.type === "scope");
|
|
244
|
+
expect(scopeEdges).toHaveLength(0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("truncates long text to 80 chars", async () => {
|
|
248
|
+
const longText = "A".repeat(200);
|
|
249
|
+
const store = createMockStore([makeEntry({ id: "long1", text: longText })]);
|
|
250
|
+
const graph = await buildMemoryGraph(store);
|
|
251
|
+
|
|
252
|
+
expect(graph.nodes[0].label.length).toBe(80);
|
|
253
|
+
expect(graph.nodes[0].label.endsWith("\u2026")).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("extracts accessCount from evolution", async () => {
|
|
257
|
+
const entry = makeEntry({
|
|
258
|
+
id: "ac1",
|
|
259
|
+
metadata: JSON.stringify({
|
|
260
|
+
evolution: {
|
|
261
|
+
status: "active",
|
|
262
|
+
version: 1,
|
|
263
|
+
accessCount: 42,
|
|
264
|
+
lastAccessedAt: null,
|
|
265
|
+
supersededBy: null,
|
|
266
|
+
supersedes: null,
|
|
267
|
+
evolutionNote: null,
|
|
268
|
+
consolidatedInto: null,
|
|
269
|
+
contributedToPattern: null,
|
|
270
|
+
sourceMemories: [],
|
|
271
|
+
validFrom: Date.now(),
|
|
272
|
+
validUntil: null,
|
|
273
|
+
},
|
|
274
|
+
}),
|
|
275
|
+
});
|
|
276
|
+
const store = createMockStore([entry]);
|
|
277
|
+
const graph = await buildMemoryGraph(store);
|
|
278
|
+
|
|
279
|
+
expect(graph.nodes[0].accessCount).toBe(42);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
// renderGraphHTML
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
describe("renderGraphHTML", () => {
|
|
288
|
+
const sampleGraph: MemoryGraph = {
|
|
289
|
+
nodes: [
|
|
290
|
+
{ id: "n1", label: "Test node", category: "profile", scope: "test", importance: 0.8, timestamp: Date.now(), accessCount: 5 },
|
|
291
|
+
],
|
|
292
|
+
edges: [],
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
it("output contains DOCTYPE html", () => {
|
|
296
|
+
const html = renderGraphHTML(sampleGraph);
|
|
297
|
+
expect(html).toContain("<!DOCTYPE html>");
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("output contains d3.js CDN link", () => {
|
|
301
|
+
const html = renderGraphHTML(sampleGraph);
|
|
302
|
+
expect(html).toContain("https://d3js.org/d3.v7.min.js");
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("output contains GRAPH_DATA", () => {
|
|
306
|
+
const html = renderGraphHTML(sampleGraph);
|
|
307
|
+
expect(html).toContain("GRAPH_DATA");
|
|
308
|
+
expect(html).toContain('"id":"n1"');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("output contains category colors", () => {
|
|
312
|
+
const html = renderGraphHTML(sampleGraph);
|
|
313
|
+
expect(html).toContain("#f59e0b"); // profile gold
|
|
314
|
+
expect(html).toContain("#3b82f6"); // preferences blue
|
|
315
|
+
expect(html).toContain("#10b981"); // entities green
|
|
316
|
+
expect(html).toContain("#ef4444"); // cases red
|
|
317
|
+
expect(html).toContain("#8b5cf6"); // patterns purple
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("output contains title and legend", () => {
|
|
321
|
+
const html = renderGraphHTML(sampleGraph);
|
|
322
|
+
expect(html).toContain("RecallNest Knowledge Graph");
|
|
323
|
+
expect(html).toContain("legend");
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("output contains dark background styling", () => {
|
|
327
|
+
const html = renderGraphHTML(sampleGraph);
|
|
328
|
+
expect(html).toContain("#0d1117");
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// ---------------------------------------------------------------------------
|
|
333
|
+
// formatGraphExportResult
|
|
334
|
+
// ---------------------------------------------------------------------------
|
|
335
|
+
|
|
336
|
+
describe("formatGraphExportResult", () => {
|
|
337
|
+
it("includes node and edge counts", () => {
|
|
338
|
+
const graph: MemoryGraph = {
|
|
339
|
+
nodes: [
|
|
340
|
+
{ id: "n1", label: "A", category: "profile", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
341
|
+
{ id: "n2", label: "B", category: "entities", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
342
|
+
],
|
|
343
|
+
edges: [
|
|
344
|
+
{ source: "n1", target: "n2", type: "scope" },
|
|
345
|
+
],
|
|
346
|
+
};
|
|
347
|
+
const result = formatGraphExportResult("/tmp/test.html", graph);
|
|
348
|
+
expect(result).toContain("Nodes: 2");
|
|
349
|
+
expect(result).toContain("Edges: 1");
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("includes file path", () => {
|
|
353
|
+
const graph: MemoryGraph = { nodes: [], edges: [] };
|
|
354
|
+
const result = formatGraphExportResult("/my/path/graph.html", graph);
|
|
355
|
+
expect(result).toContain("/my/path/graph.html");
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("includes category breakdown", () => {
|
|
359
|
+
const graph: MemoryGraph = {
|
|
360
|
+
nodes: [
|
|
361
|
+
{ id: "n1", label: "A", category: "profile", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
362
|
+
{ id: "n2", label: "B", category: "profile", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
363
|
+
{ id: "n3", label: "C", category: "events", scope: "s", importance: 0.5, timestamp: 0, accessCount: 0 },
|
|
364
|
+
],
|
|
365
|
+
edges: [],
|
|
366
|
+
};
|
|
367
|
+
const result = formatGraphExportResult("/tmp/test.html", graph);
|
|
368
|
+
expect(result).toContain("profile(2)");
|
|
369
|
+
expect(result).toContain("events(1)");
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("handles empty graph", () => {
|
|
373
|
+
const graph: MemoryGraph = { nodes: [], edges: [] };
|
|
374
|
+
const result = formatGraphExportResult("/tmp/empty.html", graph);
|
|
375
|
+
expect(result).toContain("Nodes: 0");
|
|
376
|
+
expect(result).toContain("Edges: 0");
|
|
377
|
+
expect(result).toContain("none");
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("shows semantic bridge count when present", () => {
|
|
381
|
+
const graph: MemoryGraph = {
|
|
382
|
+
nodes: [],
|
|
383
|
+
edges: [
|
|
384
|
+
{ source: "a", target: "b", type: "semantic" },
|
|
385
|
+
{ source: "c", target: "d", type: "semantic" },
|
|
386
|
+
{ source: "e", target: "f", type: "scope" },
|
|
387
|
+
],
|
|
388
|
+
};
|
|
389
|
+
const result = formatGraphExportResult("/tmp/test.html", graph);
|
|
390
|
+
expect(result).toContain("2 semantic");
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// ---------------------------------------------------------------------------
|
|
395
|
+
// Cross-Scope Semantic Bridges
|
|
396
|
+
// ---------------------------------------------------------------------------
|
|
397
|
+
|
|
398
|
+
describe("cross-scope semantic bridges", () => {
|
|
399
|
+
// Helper: create a normalized vector in a specific direction
|
|
400
|
+
function dirVector(index: number, dim = 8): number[] {
|
|
401
|
+
const v = new Array(dim).fill(0);
|
|
402
|
+
v[index] = 1;
|
|
403
|
+
return v;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Helper: create a vector that's a blend (for high cosine similarity)
|
|
407
|
+
function blendVector(a: number[], b: number[], ratio = 0.5): number[] {
|
|
408
|
+
const v = a.map((_, i) => a[i] * ratio + b[i] * (1 - ratio));
|
|
409
|
+
const norm = Math.sqrt(v.reduce((s, x) => s + x * x, 0));
|
|
410
|
+
return v.map(x => x / (norm || 1));
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
it("creates semantic edges between similar entries in different scopes", async () => {
|
|
414
|
+
const sharedVec = dirVector(0);
|
|
415
|
+
const entries = [
|
|
416
|
+
makeEntry({ id: "s1-a", scope: "scope-writing", vector: sharedVec, text: "good titles need cognitive gap" }),
|
|
417
|
+
makeEntry({ id: "s2-a", scope: "scope-ai-tools", vector: sharedVec, text: "prompts need surprise element" }),
|
|
418
|
+
];
|
|
419
|
+
const store = createMockStore(entries);
|
|
420
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
421
|
+
|
|
422
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
423
|
+
expect(semanticEdges.length).toBe(1);
|
|
424
|
+
expect(semanticEdges[0].source).toBe("s1-a");
|
|
425
|
+
expect(semanticEdges[0].target).toBe("s2-a");
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it("does NOT create semantic edges within the same scope", async () => {
|
|
429
|
+
const sharedVec = dirVector(0);
|
|
430
|
+
const entries = [
|
|
431
|
+
makeEntry({ id: "same-1", scope: "project:alpha", vector: sharedVec }),
|
|
432
|
+
makeEntry({ id: "same-2", scope: "project:alpha", vector: sharedVec }),
|
|
433
|
+
];
|
|
434
|
+
const store = createMockStore(entries);
|
|
435
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
436
|
+
|
|
437
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
438
|
+
expect(semanticEdges.length).toBe(0);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("does NOT create edges below similarity threshold", async () => {
|
|
442
|
+
const entries = [
|
|
443
|
+
makeEntry({ id: "far-1", scope: "scope-a", vector: dirVector(0) }),
|
|
444
|
+
makeEntry({ id: "far-2", scope: "scope-b", vector: dirVector(3) }), // orthogonal
|
|
445
|
+
];
|
|
446
|
+
const store = createMockStore(entries);
|
|
447
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
448
|
+
|
|
449
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
450
|
+
expect(semanticEdges.length).toBe(0);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it("caps semantic edges at MAX_SEMANTIC_EDGES (top-N by similarity)", async () => {
|
|
454
|
+
// Create 40 entries across 2 scopes with identical vectors → would generate
|
|
455
|
+
// 20*20=400 cross-scope pairs, but should be capped at 30
|
|
456
|
+
const vec = dirVector(0);
|
|
457
|
+
const entries: MemoryEntry[] = [];
|
|
458
|
+
for (let i = 0; i < 20; i++) {
|
|
459
|
+
entries.push(makeEntry({ id: `a-${i}`, scope: "scope-alpha", vector: vec }));
|
|
460
|
+
entries.push(makeEntry({ id: `b-${i}`, scope: "scope-beta", vector: vec }));
|
|
461
|
+
}
|
|
462
|
+
const store = createMockStore(entries);
|
|
463
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
464
|
+
|
|
465
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
466
|
+
expect(semanticEdges.length).toBeLessThanOrEqual(30);
|
|
467
|
+
expect(semanticEdges.length).toBeGreaterThan(0);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("handles entries with empty vectors gracefully", async () => {
|
|
471
|
+
const entries = [
|
|
472
|
+
makeEntry({ id: "no-vec-1", scope: "scope-a", vector: [] }),
|
|
473
|
+
makeEntry({ id: "no-vec-2", scope: "scope-b", vector: [] }),
|
|
474
|
+
makeEntry({ id: "has-vec", scope: "scope-c", vector: dirVector(0) }),
|
|
475
|
+
];
|
|
476
|
+
const store = createMockStore(entries);
|
|
477
|
+
const graph = await buildMemoryGraph(store, { maxNodes: 100 });
|
|
478
|
+
|
|
479
|
+
const semanticEdges = graph.edges.filter(e => e.type === "semantic");
|
|
480
|
+
expect(semanticEdges.length).toBe(0); // no valid pairs
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it("includes semantic bridge in HTML legend", () => {
|
|
484
|
+
const graph: MemoryGraph = { nodes: [], edges: [] };
|
|
485
|
+
const html = renderGraphHTML(graph);
|
|
486
|
+
expect(html).toContain("semantic bridge");
|
|
487
|
+
expect(html).toContain("#f472b6");
|
|
488
|
+
});
|
|
489
|
+
});
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* F2: Interference Detection + Active Forgetting Gate — tests for
|
|
3
|
+
* cluster detection, density measurement, RIF enhancement, and checkup.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, test } from "bun:test";
|
|
7
|
+
import {
|
|
8
|
+
detectInterference,
|
|
9
|
+
measureInterferenceDensity,
|
|
10
|
+
type InterferenceResult,
|
|
11
|
+
} from "../interference-detector.js";
|
|
12
|
+
import { filterInterference } from "../rif.js";
|
|
13
|
+
import type { MemoryEntry } from "../store.js";
|
|
14
|
+
import type { RetrievalResult } from "../retriever.js";
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Helpers
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
/** Create a memory entry with a specific vector for clustering. */
|
|
21
|
+
function makeMemory(id: string, vector: number[], importance = 0.7): MemoryEntry {
|
|
22
|
+
return {
|
|
23
|
+
id,
|
|
24
|
+
text: `memory ${id}`,
|
|
25
|
+
vector,
|
|
26
|
+
category: "events",
|
|
27
|
+
scope: "test",
|
|
28
|
+
importance,
|
|
29
|
+
timestamp: Date.now(),
|
|
30
|
+
metadata: JSON.stringify({ confidence: 0.7 }),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Create a unit vector in the given direction (for predictable cosine sim). */
|
|
35
|
+
function unitVec(direction: number[], dims = 4): number[] {
|
|
36
|
+
const v = Array(dims).fill(0);
|
|
37
|
+
for (let i = 0; i < direction.length && i < dims; i++) v[i] = direction[i];
|
|
38
|
+
const norm = Math.sqrt(v.reduce((s, x) => s + x * x, 0));
|
|
39
|
+
return norm > 0 ? v.map(x => x / norm) : v;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function makeRetrievalResult(id: string, vector: number[], score: number): RetrievalResult {
|
|
43
|
+
return {
|
|
44
|
+
entry: makeMemory(id, vector),
|
|
45
|
+
score,
|
|
46
|
+
sources: {},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// detectInterference
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
describe("F2: detectInterference", () => {
|
|
55
|
+
test("empty input returns empty array", () => {
|
|
56
|
+
expect(detectInterference([])).toEqual([]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("singleton memories have low risk", () => {
|
|
60
|
+
const m1 = makeMemory("a", unitVec([1, 0, 0, 0]));
|
|
61
|
+
const m2 = makeMemory("b", unitVec([0, 1, 0, 0])); // orthogonal — different cluster
|
|
62
|
+
const results = detectInterference([m1, m2]);
|
|
63
|
+
expect(results.length).toBe(2);
|
|
64
|
+
expect(results.every(r => r.interferenceRisk === "low")).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("similar vectors form a cluster", () => {
|
|
68
|
+
const base = [1, 0.1, 0, 0];
|
|
69
|
+
const similar = [1, 0.15, 0, 0]; // very similar to base
|
|
70
|
+
const m1 = makeMemory("a", unitVec(base));
|
|
71
|
+
const m2 = makeMemory("b", unitVec(similar));
|
|
72
|
+
const m3 = makeMemory("c", unitVec([0, 0, 1, 0])); // different cluster
|
|
73
|
+
const results = detectInterference([m1, m2, m3]);
|
|
74
|
+
// m1 and m2 should be in same cluster, m3 alone
|
|
75
|
+
const clusterIds = new Set(results.map(r => r.clusterId));
|
|
76
|
+
expect(clusterIds.size).toBe(2); // 2 clusters
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("cluster rank 0 has low risk, rank 3+ has high risk", () => {
|
|
80
|
+
const v = unitVec([1, 0, 0, 0]);
|
|
81
|
+
const memories = [
|
|
82
|
+
makeMemory("a", v, 0.9),
|
|
83
|
+
makeMemory("b", v, 0.8),
|
|
84
|
+
makeMemory("c", v, 0.7),
|
|
85
|
+
makeMemory("d", v, 0.3), // weakest — should be high risk
|
|
86
|
+
];
|
|
87
|
+
const results = detectInterference(memories);
|
|
88
|
+
// All in one cluster
|
|
89
|
+
const clusterIds = new Set(results.map(r => r.clusterId));
|
|
90
|
+
expect(clusterIds.size).toBe(1);
|
|
91
|
+
// Rank 0 is low risk
|
|
92
|
+
const topRanked = results.find(r => r.clusterRank === 0);
|
|
93
|
+
expect(topRanked?.interferenceRisk).toBe("low");
|
|
94
|
+
// Rank 3 is high risk
|
|
95
|
+
const bottomRanked = results.find(r => r.clusterRank === 3);
|
|
96
|
+
expect(bottomRanked?.interferenceRisk).toBe("high");
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
// measureInterferenceDensity
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
describe("F2: measureInterferenceDensity", () => {
|
|
105
|
+
test("no clusters → zero density", () => {
|
|
106
|
+
const memories = [
|
|
107
|
+
makeMemory("a", unitVec([1, 0, 0, 0])),
|
|
108
|
+
makeMemory("b", unitVec([0, 1, 0, 0])),
|
|
109
|
+
makeMemory("c", unitVec([0, 0, 1, 0])),
|
|
110
|
+
];
|
|
111
|
+
const density = measureInterferenceDensity(memories);
|
|
112
|
+
expect(density.clusterCount).toBe(0);
|
|
113
|
+
expect(density.highRiskCount).toBe(0);
|
|
114
|
+
expect(density.avgClusterSize).toBe(0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("one cluster → density reflects cluster size", () => {
|
|
118
|
+
const v = unitVec([1, 0, 0, 0]);
|
|
119
|
+
const memories = [
|
|
120
|
+
makeMemory("a", v, 0.9),
|
|
121
|
+
makeMemory("b", v, 0.7),
|
|
122
|
+
makeMemory("c", v, 0.5),
|
|
123
|
+
makeMemory("d", v, 0.3),
|
|
124
|
+
];
|
|
125
|
+
const density = measureInterferenceDensity(memories);
|
|
126
|
+
expect(density.clusterCount).toBe(1);
|
|
127
|
+
expect(density.avgClusterSize).toBe(4);
|
|
128
|
+
expect(density.highRiskCount).toBe(1); // rank 3 = high
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
// Enhanced RIF (cluster top-K)
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
describe("F2: Enhanced RIF with cluster top-K", () => {
|
|
137
|
+
test("cluster within top-K not demoted", () => {
|
|
138
|
+
const v = unitVec([1, 0, 0, 0]);
|
|
139
|
+
const results = [
|
|
140
|
+
makeRetrievalResult("a", v, 0.9),
|
|
141
|
+
makeRetrievalResult("b", v, 0.85),
|
|
142
|
+
makeRetrievalResult("c", v, 0.80),
|
|
143
|
+
];
|
|
144
|
+
const filtered = filterInterference(results, 0.85, 0.80, 3);
|
|
145
|
+
// All 3 should be kept (within top-K=3)
|
|
146
|
+
expect(filtered.length).toBe(3);
|
|
147
|
+
expect(filtered[0].entry.id).toBe("a");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("4th cluster member gets 50% score demotion", () => {
|
|
151
|
+
const v = unitVec([1, 0, 0, 0]);
|
|
152
|
+
const results = [
|
|
153
|
+
makeRetrievalResult("a", v, 0.9),
|
|
154
|
+
makeRetrievalResult("b", v, 0.85),
|
|
155
|
+
makeRetrievalResult("c", v, 0.80),
|
|
156
|
+
makeRetrievalResult("d", v, 0.75), // 4th member
|
|
157
|
+
];
|
|
158
|
+
const filtered = filterInterference(results, 0.85, 0.80, 3);
|
|
159
|
+
// d should be at the end with 50% score
|
|
160
|
+
const d = filtered.find(r => r.entry.id === "d");
|
|
161
|
+
expect(d).toBeDefined();
|
|
162
|
+
expect(d!.score).toBeCloseTo(0.375, 2); // 0.75 * 0.5
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("diverse results unaffected by cluster top-K", () => {
|
|
166
|
+
const results = [
|
|
167
|
+
makeRetrievalResult("a", unitVec([1, 0, 0, 0]), 0.9),
|
|
168
|
+
makeRetrievalResult("b", unitVec([0, 1, 0, 0]), 0.85),
|
|
169
|
+
makeRetrievalResult("c", unitVec([0, 0, 1, 0]), 0.80),
|
|
170
|
+
makeRetrievalResult("d", unitVec([0, 0, 0, 1]), 0.75),
|
|
171
|
+
];
|
|
172
|
+
const filtered = filterInterference(results, 0.85, 0.80, 3);
|
|
173
|
+
// All diverse — no demotion
|
|
174
|
+
expect(filtered.length).toBe(4);
|
|
175
|
+
expect(filtered.map(r => r.entry.id)).toEqual(["a", "b", "c", "d"]);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
// data-checkup integration
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
describe("F2: data-checkup interference check", () => {
|
|
184
|
+
// We test the check function indirectly through measureInterferenceDensity
|
|
185
|
+
test("healthy density has low high-risk ratio", () => {
|
|
186
|
+
const memories = [
|
|
187
|
+
makeMemory("a", unitVec([1, 0, 0, 0])),
|
|
188
|
+
makeMemory("b", unitVec([0, 1, 0, 0])),
|
|
189
|
+
makeMemory("c", unitVec([0, 0, 1, 0])),
|
|
190
|
+
];
|
|
191
|
+
const density = measureInterferenceDensity(memories);
|
|
192
|
+
// high-risk should be < 20% of total
|
|
193
|
+
expect(density.highRiskCount / density.totalMemories).toBeLessThan(0.2);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("unhealthy density detected in identical vectors", () => {
|
|
197
|
+
const v = unitVec([1, 0, 0, 0]);
|
|
198
|
+
const memories = Array.from({ length: 10 }, (_, i) => makeMemory(`m${i}`, v, 0.5 - i * 0.01));
|
|
199
|
+
const density = measureInterferenceDensity(memories);
|
|
200
|
+
expect(density.clusterCount).toBe(1);
|
|
201
|
+
expect(density.highRiskCount).toBeGreaterThan(0);
|
|
202
|
+
});
|
|
203
|
+
});
|