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,232 @@
|
|
|
1
|
+
import { describe, expect, it, beforeEach } from "bun:test";
|
|
2
|
+
import { runDream, formatDreamResult, type DreamResult } from "../dream-pipeline.js";
|
|
3
|
+
import type { MemoryEntry, MemoryStore } from "../store.js";
|
|
4
|
+
import type { LLMClient } from "../llm-client.js";
|
|
5
|
+
import type { Embedder } from "../embedder.js";
|
|
6
|
+
import { resetWriteCount } from "../activity-counter.js";
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Helpers
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
function makeEntry(overrides: Partial<MemoryEntry> & { id: string }): MemoryEntry {
|
|
13
|
+
return {
|
|
14
|
+
text: "test memory",
|
|
15
|
+
vector: [1, 0, 0, 0, 0],
|
|
16
|
+
category: "events",
|
|
17
|
+
scope: "project:test",
|
|
18
|
+
importance: 0.5,
|
|
19
|
+
timestamp: Date.now(),
|
|
20
|
+
metadata: JSON.stringify({
|
|
21
|
+
evolution: {
|
|
22
|
+
status: "active",
|
|
23
|
+
version: 1,
|
|
24
|
+
accessCount: 0,
|
|
25
|
+
lastAccessedAt: null,
|
|
26
|
+
supersededBy: 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[]): MemoryStore {
|
|
39
|
+
const stored: MemoryEntry[] = [...entries];
|
|
40
|
+
let storeCounter = 0;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
async list() { return stored; },
|
|
44
|
+
async stats() {
|
|
45
|
+
return {
|
|
46
|
+
totalCount: stored.length,
|
|
47
|
+
total: stored.length,
|
|
48
|
+
scopeCounts: {},
|
|
49
|
+
categoryCounts: {},
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
async store(entry: Partial<MemoryEntry>) {
|
|
53
|
+
const full = {
|
|
54
|
+
id: entry.id || `dream-${storeCounter++}`,
|
|
55
|
+
text: entry.text || "",
|
|
56
|
+
vector: entry.vector || [],
|
|
57
|
+
category: entry.category || "events",
|
|
58
|
+
scope: entry.scope || "project:test",
|
|
59
|
+
importance: entry.importance || 0.5,
|
|
60
|
+
timestamp: Date.now(),
|
|
61
|
+
metadata: entry.metadata || "{}",
|
|
62
|
+
} as MemoryEntry;
|
|
63
|
+
stored.push(full);
|
|
64
|
+
return full;
|
|
65
|
+
},
|
|
66
|
+
async update(id: string, upd: Partial<MemoryEntry>) {
|
|
67
|
+
const entry = stored.find(e => e.id === id);
|
|
68
|
+
if (entry && upd.metadata) entry.metadata = upd.metadata;
|
|
69
|
+
return entry || { id, text: "", vector: [], category: "events", scope: "project:test", importance: 0.5, timestamp: Date.now(), metadata: "{}" } as MemoryEntry;
|
|
70
|
+
},
|
|
71
|
+
async getById(id: string) {
|
|
72
|
+
return stored.find(e => e.id === id) || null;
|
|
73
|
+
},
|
|
74
|
+
async vectorSearch(_vec: number[], limit: number, _threshold: number, _scopes?: string[]) {
|
|
75
|
+
return stored.slice(0, limit).map(e => ({ entry: e, score: 0.85 }));
|
|
76
|
+
},
|
|
77
|
+
} as unknown as MemoryStore;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function createMockLLM(): LLMClient {
|
|
81
|
+
return {
|
|
82
|
+
async generateL0() { return "consolidated insight"; },
|
|
83
|
+
async extractPattern() { return "discovered pattern"; },
|
|
84
|
+
} as unknown as LLMClient;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function createMockEmbedder(): Pick<Embedder, "embedPassage"> {
|
|
88
|
+
return {
|
|
89
|
+
async embedPassage() { return [0.5, 0.5, 0, 0, 0]; },
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// Tests
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
describe("runDream", () => {
|
|
98
|
+
beforeEach(() => {
|
|
99
|
+
// Reset activity counter between tests
|
|
100
|
+
resetWriteCount();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("skips when write count is below threshold", async () => {
|
|
104
|
+
const store = createMockStore([makeEntry({ id: "a" })]);
|
|
105
|
+
const result = await runDream({
|
|
106
|
+
store,
|
|
107
|
+
llm: createMockLLM(),
|
|
108
|
+
embedder: createMockEmbedder(),
|
|
109
|
+
scope: "project:test",
|
|
110
|
+
config: { minWritesForDream: 10 },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expect(result.ran).toBe(false);
|
|
114
|
+
expect(result.reason).toContain("insufficient_writes");
|
|
115
|
+
expect(result.phases.length).toBe(1);
|
|
116
|
+
expect(result.phases[0].phase).toBe("orient");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("runs when forced despite low write count", async () => {
|
|
120
|
+
const entries = [
|
|
121
|
+
makeEntry({ id: "a", vector: [0.9, 0.1, 0, 0, 0] }),
|
|
122
|
+
makeEntry({ id: "b", vector: [0.88, 0.12, 0, 0, 0] }),
|
|
123
|
+
makeEntry({ id: "c", vector: [0.92, 0.08, 0, 0, 0] }),
|
|
124
|
+
];
|
|
125
|
+
const store = createMockStore(entries);
|
|
126
|
+
const result = await runDream({
|
|
127
|
+
store,
|
|
128
|
+
llm: createMockLLM(),
|
|
129
|
+
embedder: createMockEmbedder(),
|
|
130
|
+
scope: "project:test",
|
|
131
|
+
force: true,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect(result.ran).toBe(true);
|
|
135
|
+
expect(result.phases.length).toBe(4);
|
|
136
|
+
expect(result.phases.map(p => p.phase)).toEqual(["orient", "gather", "consolidate", "prune"]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("completes early with too few active entries", async () => {
|
|
140
|
+
const store = createMockStore([
|
|
141
|
+
makeEntry({ id: "a" }),
|
|
142
|
+
]);
|
|
143
|
+
const result = await runDream({
|
|
144
|
+
store,
|
|
145
|
+
llm: createMockLLM(),
|
|
146
|
+
embedder: createMockEmbedder(),
|
|
147
|
+
scope: "project:test",
|
|
148
|
+
force: true,
|
|
149
|
+
config: { minClusterSize: 3 },
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
expect(result.ran).toBe(true);
|
|
153
|
+
expect(result.reason).toBe("completed_early");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("works without LLM (null) — only deterministic consolidation", async () => {
|
|
157
|
+
const entries = [
|
|
158
|
+
makeEntry({ id: "a", vector: [0.9, 0.1, 0, 0, 0] }),
|
|
159
|
+
makeEntry({ id: "b", vector: [0.88, 0.12, 0, 0, 0] }),
|
|
160
|
+
makeEntry({ id: "c", vector: [0.92, 0.08, 0, 0, 0] }),
|
|
161
|
+
];
|
|
162
|
+
const store = createMockStore(entries);
|
|
163
|
+
const result = await runDream({
|
|
164
|
+
store,
|
|
165
|
+
llm: null,
|
|
166
|
+
embedder: createMockEmbedder(),
|
|
167
|
+
scope: "project:test",
|
|
168
|
+
force: true,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
expect(result.ran).toBe(true);
|
|
172
|
+
expect(result.stats.insightsGenerated).toBe(0); // No LLM = no insights
|
|
173
|
+
expect(result.stats.patternsExtracted).toBe(0);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("reports correct stats structure", async () => {
|
|
177
|
+
const entries = [
|
|
178
|
+
makeEntry({ id: "a", vector: [0.9, 0.1, 0, 0, 0] }),
|
|
179
|
+
makeEntry({ id: "b", vector: [0.88, 0.12, 0, 0, 0] }),
|
|
180
|
+
makeEntry({ id: "c", vector: [0.92, 0.08, 0, 0, 0] }),
|
|
181
|
+
];
|
|
182
|
+
const store = createMockStore(entries);
|
|
183
|
+
const result = await runDream({
|
|
184
|
+
store,
|
|
185
|
+
llm: createMockLLM(),
|
|
186
|
+
embedder: createMockEmbedder(),
|
|
187
|
+
scope: "project:test",
|
|
188
|
+
force: true,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
expect(result.stats.totalMemories).toBeGreaterThanOrEqual(0);
|
|
192
|
+
expect(result.stats.activeMemories).toBeGreaterThanOrEqual(0);
|
|
193
|
+
expect(typeof result.stats.clustersFound).toBe("number");
|
|
194
|
+
expect(typeof result.stats.insightsGenerated).toBe("number");
|
|
195
|
+
expect(typeof result.stats.patternsExtracted).toBe("number");
|
|
196
|
+
expect(typeof result.stats.mergedCount).toBe("number");
|
|
197
|
+
expect(typeof result.stats.archivedCount).toBe("number");
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("formatDreamResult", () => {
|
|
202
|
+
it("formats skipped dream", () => {
|
|
203
|
+
const result: DreamResult = {
|
|
204
|
+
ran: false,
|
|
205
|
+
reason: "insufficient_writes (3/10)",
|
|
206
|
+
phases: [{ phase: "orient", detail: "50 memories, 3 writes" }],
|
|
207
|
+
stats: { totalMemories: 50, activeMemories: 0, writesSinceLastDream: 3, clustersFound: 0, insightsGenerated: 0, patternsExtracted: 0, mergedCount: 0, archivedCount: 0 },
|
|
208
|
+
};
|
|
209
|
+
const output = formatDreamResult(result);
|
|
210
|
+
expect(output).toContain("skipped");
|
|
211
|
+
expect(output).toContain("insufficient_writes");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("formats completed dream with all phases", () => {
|
|
215
|
+
const result: DreamResult = {
|
|
216
|
+
ran: true,
|
|
217
|
+
phases: [
|
|
218
|
+
{ phase: "orient", detail: "100 memories, 15 writes" },
|
|
219
|
+
{ phase: "gather", detail: "80 active entries" },
|
|
220
|
+
{ phase: "consolidate", detail: "3 clusters, 1 merged, 2 insights, 1 pattern" },
|
|
221
|
+
{ phase: "prune", detail: "5 entries archived" },
|
|
222
|
+
],
|
|
223
|
+
stats: { totalMemories: 100, activeMemories: 80, writesSinceLastDream: 15, clustersFound: 3, insightsGenerated: 2, patternsExtracted: 1, mergedCount: 1, archivedCount: 5 },
|
|
224
|
+
};
|
|
225
|
+
const output = formatDreamResult(result);
|
|
226
|
+
expect(output).toContain("Dream completed");
|
|
227
|
+
expect(output).toContain("[orient]");
|
|
228
|
+
expect(output).toContain("[consolidate]");
|
|
229
|
+
expect(output).toContain("[prune]");
|
|
230
|
+
expect(output).toContain("Patterns: 1");
|
|
231
|
+
});
|
|
232
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { describe, test, expect } from "bun:test";
|
|
2
|
+
import { weibullDecay, adjustHalfLifeForEmotion, computeArousalBoost } from "../decay-engine.js";
|
|
3
|
+
|
|
4
|
+
describe("emotion-adjusted decay", () => {
|
|
5
|
+
test("returns base half-life when no emotion", () => {
|
|
6
|
+
expect(adjustHalfLifeForEmotion(60, undefined)).toBe(60);
|
|
7
|
+
expect(adjustHalfLifeForEmotion(60, null)).toBe(60);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("strong negative emotion extends half-life ~27%", () => {
|
|
11
|
+
const adjusted = adjustHalfLifeForEmotion(60, { valence: -0.9, arousal: 0.5, label: "frustration" });
|
|
12
|
+
expect(adjusted).toBeCloseTo(60 * 1.27, 0);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("strong positive emotion extends half-life ~24%", () => {
|
|
16
|
+
const adjusted = adjustHalfLifeForEmotion(60, { valence: 0.8, arousal: 0.3, label: "excitement" });
|
|
17
|
+
expect(adjusted).toBeCloseTo(60 * 1.24, 0);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("neutral emotion has negligible effect", () => {
|
|
21
|
+
const adjusted = adjustHalfLifeForEmotion(60, { valence: 0.05, arousal: 0.1, label: "neutral" });
|
|
22
|
+
expect(adjusted).toBeCloseTo(60, 0);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("emotional memory decays slower at 30 days", () => {
|
|
26
|
+
const baseHL = 60;
|
|
27
|
+
const emotionalHL = adjustHalfLifeForEmotion(baseHL, { valence: -0.8, arousal: 0.7, label: "frustration" });
|
|
28
|
+
const neutralDecay = weibullDecay(30, baseHL, "working");
|
|
29
|
+
const emotionalDecay = weibullDecay(30, emotionalHL, "working");
|
|
30
|
+
expect(emotionalDecay).toBeGreaterThan(neutralDecay);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("arousal boost is 1.0 for zero arousal", () => {
|
|
34
|
+
expect(computeArousalBoost({ valence: 0.5, arousal: 0, label: "positive" })).toBe(1.0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("arousal boost max ~1.1 for high arousal", () => {
|
|
38
|
+
const boost = computeArousalBoost({ valence: 0.5, arousal: 0.9, label: "excitement" });
|
|
39
|
+
expect(boost).toBeCloseTo(1.09, 1);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("arousal boost is 1.0 for null emotion", () => {
|
|
43
|
+
expect(computeArousalBoost(undefined)).toBe(1.0);
|
|
44
|
+
expect(computeArousalBoost(null)).toBe(1.0);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, test, expect } from "bun:test";
|
|
2
|
+
import { detectEmotion } from "../emotion-detector.js";
|
|
3
|
+
|
|
4
|
+
describe("emotion-detector: heuristic", () => {
|
|
5
|
+
test("detects strong negative text", () => {
|
|
6
|
+
const result = detectEmotion("This bug is driving me crazy, everything is broken and wrong");
|
|
7
|
+
expect(result.valence).toBeLessThan(-0.3);
|
|
8
|
+
expect(result.label).toBe("negative");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("detects strong positive text", () => {
|
|
12
|
+
const result = detectEmotion("Finally solved it! Works perfectly, great progress today");
|
|
13
|
+
expect(result.valence).toBeGreaterThan(0.3);
|
|
14
|
+
expect(result.label).toBe("positive");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("detects neutral text", () => {
|
|
18
|
+
const result = detectEmotion("The user prefers dark mode and uses VS Code");
|
|
19
|
+
expect(Math.abs(result.valence)).toBeLessThan(0.3);
|
|
20
|
+
expect(result.label).toBe("neutral");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("detects high arousal", () => {
|
|
24
|
+
const result = detectEmotion("URGENT! Critical production issue, fix immediately!");
|
|
25
|
+
expect(result.arousal).toBeGreaterThan(0.3);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("detects low arousal for calm text", () => {
|
|
29
|
+
const result = detectEmotion("The default configuration uses port 3000");
|
|
30
|
+
expect(result.arousal).toBeLessThan(0.3);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("handles mixed signals with net positive", () => {
|
|
34
|
+
const result = detectEmotion("Had a frustrating bug but finally solved it perfectly");
|
|
35
|
+
expect(result.valence).toBeGreaterThan(0);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("handles empty text gracefully", () => {
|
|
39
|
+
const result = detectEmotion("");
|
|
40
|
+
expect(result.valence).toBe(0);
|
|
41
|
+
expect(result.arousal).toBe(0);
|
|
42
|
+
expect(result.label).toBe("neutral");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("clamps valence to [-1, 1]", () => {
|
|
46
|
+
const result = detectEmotion("broken error wrong bug failure hate broken error wrong");
|
|
47
|
+
expect(result.valence).toBeGreaterThanOrEqual(-1);
|
|
48
|
+
expect(result.valence).toBeLessThanOrEqual(1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("clamps arousal to [0, 1]", () => {
|
|
52
|
+
const result = detectEmotion("URGENT! CRITICAL! IMMEDIATELY! ASAP!");
|
|
53
|
+
expect(result.arousal).toBeGreaterThanOrEqual(0);
|
|
54
|
+
expect(result.arousal).toBeLessThanOrEqual(1);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 1 integration tests: Emotional Valence in Decay pipeline.
|
|
3
|
+
*
|
|
4
|
+
* Validates the full wiring:
|
|
5
|
+
* 1. detectEmotion → salience + source fields
|
|
6
|
+
* 2. computeDecayScore → emotion-aware weights (0.15/0.25/0.45/0.15)
|
|
7
|
+
* 3. Backward compatibility when emotion flag is off or metadata absent
|
|
8
|
+
*/
|
|
9
|
+
import { describe, test, expect } from "bun:test";
|
|
10
|
+
import { detectEmotion } from "../emotion-detector.js";
|
|
11
|
+
import { computeDecayScore, type EvolutionMetadata } from "../memory-evolution.js";
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// 1. detectEmotion: salience + source fields
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
describe("detectEmotion salience & source", () => {
|
|
18
|
+
test("returns salience field as (|valence| + arousal) / 2", () => {
|
|
19
|
+
const result = detectEmotion("Finally fixed that horrible production bug after 3 days!");
|
|
20
|
+
expect(result.salience).toBeDefined();
|
|
21
|
+
expect(typeof result.salience).toBe("number");
|
|
22
|
+
// salience = (|valence| + arousal) / 2
|
|
23
|
+
const expected = (Math.abs(result.valence) + result.arousal) / 2;
|
|
24
|
+
expect(result.salience).toBeCloseTo(expected, 5);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("returns source: keyword", () => {
|
|
28
|
+
const result = detectEmotion("This is a great success!");
|
|
29
|
+
expect(result.source).toBe("keyword");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("neutral text returns salience 0", () => {
|
|
33
|
+
const result = detectEmotion("The meeting is at 3pm tomorrow.");
|
|
34
|
+
expect(result.salience).toBe(0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("empty text returns salience 0 and source keyword", () => {
|
|
38
|
+
const result = detectEmotion("");
|
|
39
|
+
expect(result.salience).toBe(0);
|
|
40
|
+
expect(result.source).toBe("keyword");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("high emotion + high arousal gives high salience", () => {
|
|
44
|
+
const result = detectEmotion("CRITICAL BUG! Everything is broken! Urgent fix needed!");
|
|
45
|
+
expect(result.salience).toBeGreaterThan(0.2);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("salience is clamped to [0, 1]", () => {
|
|
49
|
+
const result = detectEmotion("fail fail fail broken broken crash crash urgent urgent critical critical !!!");
|
|
50
|
+
expect(result.salience).toBeGreaterThanOrEqual(0);
|
|
51
|
+
expect(result.salience).toBeLessThanOrEqual(1);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// 2. computeDecayScore: emotion-aware weights
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
describe("computeDecayScore with emotion", () => {
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
const baseEvo: EvolutionMetadata = {
|
|
62
|
+
status: "active",
|
|
63
|
+
version: 1,
|
|
64
|
+
accessCount: 3,
|
|
65
|
+
lastAccessedAt: now - 7 * 86_400_000, // 7 days ago
|
|
66
|
+
supersededBy: null,
|
|
67
|
+
supersedes: null,
|
|
68
|
+
evolutionNote: null,
|
|
69
|
+
consolidatedInto: null,
|
|
70
|
+
contributedToPattern: null,
|
|
71
|
+
sourceMemories: [],
|
|
72
|
+
validFrom: now - 30 * 86_400_000, // 30 days old
|
|
73
|
+
validUntil: null,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// Save and restore env
|
|
77
|
+
const origFlag = process.env.RECALLNEST_EMOTION_SCORING;
|
|
78
|
+
|
|
79
|
+
test("with emotion flag ON and metadata, emotional memory scores higher", () => {
|
|
80
|
+
process.env.RECALLNEST_EMOTION_SCORING = "true";
|
|
81
|
+
try {
|
|
82
|
+
const emotionalMeta = JSON.stringify({
|
|
83
|
+
emotion: { valence: -0.9, arousal: 0.8, label: "frustration", salience: 0.85, source: "keyword" },
|
|
84
|
+
});
|
|
85
|
+
const neutralMeta = JSON.stringify({
|
|
86
|
+
emotion: { valence: 0, arousal: 0, label: "neutral", salience: 0, source: "keyword" },
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const emotionalScore = computeDecayScore(baseEvo, 0.5, now, emotionalMeta);
|
|
90
|
+
const neutralScore = computeDecayScore(baseEvo, 0.5, now, neutralMeta);
|
|
91
|
+
|
|
92
|
+
expect(emotionalScore).toBeGreaterThan(neutralScore);
|
|
93
|
+
} finally {
|
|
94
|
+
process.env.RECALLNEST_EMOTION_SCORING = origFlag;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("with emotion flag ON, weights sum close to 1.0", () => {
|
|
99
|
+
process.env.RECALLNEST_EMOTION_SCORING = "true";
|
|
100
|
+
try {
|
|
101
|
+
// At max values: time=1, freq=1, importance=1, salience=1
|
|
102
|
+
// Score should be 0.15 + 0.25 + 0.45 + 0.15 = 1.0
|
|
103
|
+
const freshEvo: EvolutionMetadata = {
|
|
104
|
+
...baseEvo,
|
|
105
|
+
accessCount: 100,
|
|
106
|
+
lastAccessedAt: now,
|
|
107
|
+
validFrom: now, // just created
|
|
108
|
+
};
|
|
109
|
+
const maxMeta = JSON.stringify({
|
|
110
|
+
emotion: { valence: 1, arousal: 1, label: "positive", salience: 1, source: "keyword" },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const score = computeDecayScore(freshEvo, 1.0, now, maxMeta);
|
|
114
|
+
expect(score).toBeCloseTo(1.0, 1);
|
|
115
|
+
} finally {
|
|
116
|
+
process.env.RECALLNEST_EMOTION_SCORING = origFlag;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("backward compat: no metadata param still works", () => {
|
|
121
|
+
const score = computeDecayScore(baseEvo, 0.5, now);
|
|
122
|
+
expect(score).toBeGreaterThan(0);
|
|
123
|
+
expect(score).toBeLessThanOrEqual(1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("backward compat: metadata without emotion still works", () => {
|
|
127
|
+
process.env.RECALLNEST_EMOTION_SCORING = "true";
|
|
128
|
+
try {
|
|
129
|
+
const noEmotionMeta = JSON.stringify({ tier: "working" });
|
|
130
|
+
const score = computeDecayScore(baseEvo, 0.5, now, noEmotionMeta);
|
|
131
|
+
expect(score).toBeGreaterThan(0);
|
|
132
|
+
expect(score).toBeLessThanOrEqual(1);
|
|
133
|
+
} finally {
|
|
134
|
+
process.env.RECALLNEST_EMOTION_SCORING = origFlag;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("with emotion flag OFF, metadata is ignored (base weights)", () => {
|
|
139
|
+
process.env.RECALLNEST_EMOTION_SCORING = "";
|
|
140
|
+
try {
|
|
141
|
+
const emotionalMeta = JSON.stringify({
|
|
142
|
+
emotion: { valence: -0.9, arousal: 0.8, label: "frustration", salience: 0.85, source: "keyword" },
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const withMeta = computeDecayScore(baseEvo, 0.5, now, emotionalMeta);
|
|
146
|
+
const withoutMeta = computeDecayScore(baseEvo, 0.5, now);
|
|
147
|
+
|
|
148
|
+
expect(withMeta).toBeCloseTo(withoutMeta, 5);
|
|
149
|
+
} finally {
|
|
150
|
+
process.env.RECALLNEST_EMOTION_SCORING = origFlag;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, test, expect } from "bun:test";
|
|
2
|
+
import { applyEmotionWeight } from "../retriever.js";
|
|
3
|
+
|
|
4
|
+
describe("applyEmotionWeight", () => {
|
|
5
|
+
const makeResult = (id: string, score: number, emotionJson?: object) => ({
|
|
6
|
+
id,
|
|
7
|
+
text: "test",
|
|
8
|
+
vector: [],
|
|
9
|
+
category: "events" as const,
|
|
10
|
+
scope: "test",
|
|
11
|
+
importance: 0.5,
|
|
12
|
+
timestamp: Date.now(),
|
|
13
|
+
metadata: emotionJson ? JSON.stringify({ emotion: emotionJson }) : "{}",
|
|
14
|
+
score,
|
|
15
|
+
sources: {},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("null query emotion returns unchanged", () => {
|
|
19
|
+
const results = [makeResult("a", 0.8, { valence: 0.9, arousal: 0.5 })];
|
|
20
|
+
const output = applyEmotionWeight(results, null);
|
|
21
|
+
expect(output[0].score).toBe(0.8);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("low-valence query returns unchanged", () => {
|
|
25
|
+
const queryEmotion = { valence: 0.1, arousal: 0, label: "neutral" };
|
|
26
|
+
const results = [makeResult("a", 0.8, { valence: 0.9, arousal: 0.5 })];
|
|
27
|
+
const output = applyEmotionWeight(results, queryEmotion);
|
|
28
|
+
expect(output[0].score).toBe(0.8);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("negative query boosts negative memories", () => {
|
|
32
|
+
const queryEmotion = { valence: -0.8, arousal: 0.5, label: "frustration" };
|
|
33
|
+
const results = [
|
|
34
|
+
makeResult("neg", 0.7, { valence: -0.7, arousal: 0.5 }),
|
|
35
|
+
makeResult("pos", 0.7, { valence: 0.8, arousal: 0.3 }),
|
|
36
|
+
];
|
|
37
|
+
const output = applyEmotionWeight(results, queryEmotion);
|
|
38
|
+
expect(output.find(r => r.id === "neg")!.score).toBeGreaterThan(
|
|
39
|
+
output.find(r => r.id === "pos")!.score
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("no emotion data leaves score unchanged", () => {
|
|
44
|
+
const queryEmotion = { valence: -0.8, arousal: 0.5, label: "negative" };
|
|
45
|
+
const results = [makeResult("no-emo", 0.7)];
|
|
46
|
+
const output = applyEmotionWeight(results, queryEmotion);
|
|
47
|
+
expect(output[0].score).toBe(0.7);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("max boost is 15%", () => {
|
|
51
|
+
const queryEmotion = { valence: 1.0, arousal: 1.0, label: "positive" };
|
|
52
|
+
const results = [makeResult("a", 1.0, { valence: 1.0, arousal: 1.0 })];
|
|
53
|
+
const output = applyEmotionWeight(results, queryEmotion);
|
|
54
|
+
expect(output[0].score).toBeCloseTo(1.15, 2);
|
|
55
|
+
});
|
|
56
|
+
});
|