recallnest 2.3.0 → 2.5.4
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/CHANGELOG.md +150 -0
- package/README.md +17 -31
- package/README_CN.md +12 -22
- package/assets/ui/app.js +1031 -0
- package/assets/ui/index.html +224 -0
- package/assets/ui/styles.css +781 -0
- package/config.json.example +4 -1
- package/docs/api-reference.md +1291 -0
- package/docs/architecture.md +168 -0
- package/docs/connector-spec.md +126 -0
- package/eval/cases-canary.json +58 -0
- package/eval/cases-scope-robustness.json +278 -0
- package/eval/cases.json +117 -20
- package/package.json +22 -1
- package/src/__tests__/access-tracker-novelty.test.ts +238 -0
- package/src/__tests__/activity-counter.test.ts +125 -0
- package/src/__tests__/adaptive-fidelity.test.ts +102 -0
- package/src/__tests__/admission-control.test.ts +206 -0
- package/src/__tests__/anchor-generator.test.ts +67 -0
- package/src/__tests__/auto-consolidation.test.ts +168 -0
- package/src/__tests__/auto-recall.test.ts +131 -0
- package/src/__tests__/babel-integration.test.ts +144 -0
- package/src/__tests__/batch-dedup.test.ts +65 -0
- package/src/__tests__/canary-eval.test.ts +114 -0
- package/src/__tests__/capture-engine.test.ts +1238 -0
- package/src/__tests__/capture-heuristic.test.ts +171 -0
- package/src/__tests__/cascade-forget.test.ts +210 -0
- package/src/__tests__/category-min-scores.test.ts +33 -0
- 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 +782 -0
- package/src/__tests__/confidence-metadata.test.ts +125 -0
- package/src/__tests__/confidence-tracker.test.ts +261 -0
- package/src/__tests__/conflict-advisor.test.ts +206 -0
- package/src/__tests__/conflict-escalation.test.ts +149 -0
- package/src/__tests__/conflict-lifecycle.test.ts +83 -0
- package/src/__tests__/conflict-output.test.ts +85 -0
- package/src/__tests__/conflict-store.test.ts +374 -0
- package/src/__tests__/connector-v1.test.ts +181 -0
- package/src/__tests__/consolidation-engine.test.ts +333 -0
- package/src/__tests__/constructive-retrieval.test.ts +509 -0
- package/src/__tests__/context-collapse-renderer.test.ts +214 -0
- package/src/__tests__/context-composer-pins.test.ts +64 -0
- package/src/__tests__/context-composer-task-selection.test.ts +24 -0
- package/src/__tests__/context-reconstructor.test.ts +65 -0
- package/src/__tests__/context-renderer.test.ts +91 -0
- package/src/__tests__/continuity-eval.test.ts +275 -0
- package/src/__tests__/continuity-schema.test.ts +97 -0
- package/src/__tests__/contracts.test.ts +216 -0
- package/src/__tests__/conversation-importer.test.ts +692 -0
- package/src/__tests__/core-recall-eval.test.ts +235 -0
- package/src/__tests__/core-summary.test.ts +109 -0
- package/src/__tests__/data-checkup.test.ts +276 -0
- package/src/__tests__/decay-exemption.test.ts +84 -0
- package/src/__tests__/dedup-actions.test.ts +58 -0
- package/src/__tests__/dedup-guard.test.ts +39 -0
- package/src/__tests__/deterministic-id.test.ts +39 -0
- package/src/__tests__/distill-lock.test.ts +305 -0
- package/src/__tests__/distill-session-argshape.test.ts +79 -0
- package/src/__tests__/doctor.test.ts +319 -0
- package/src/__tests__/dream-pipeline.test.ts +321 -0
- package/src/__tests__/drill-down.test.ts +108 -0
- package/src/__tests__/embedder.test.ts +122 -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__/entity-resolver.test.ts +58 -0
- package/src/__tests__/env-config.test.ts +151 -0
- package/src/__tests__/error-signature-boost.test.ts +87 -0
- package/src/__tests__/error-signature.test.ts +95 -0
- package/src/__tests__/error-taxonomy.test.ts +129 -0
- package/src/__tests__/ethics-layer.test.ts +476 -0
- package/src/__tests__/event-segmenter.test.ts +153 -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__/hotness-blend.test.ts +76 -0
- package/src/__tests__/ingest-chunking.test.ts +44 -0
- package/src/__tests__/ingest.test.ts +360 -0
- package/src/__tests__/interference-detector.test.ts +203 -0
- package/src/__tests__/kg-extractor.test.ts +192 -0
- package/src/__tests__/kg-store.test.ts +300 -0
- package/src/__tests__/llm-consolidation.test.ts +226 -0
- package/src/__tests__/maintenance-locks.test.ts +174 -0
- package/src/__tests__/mcp-efficiency.test.ts +288 -0
- package/src/__tests__/mcp-related-scopes.test.ts +151 -0
- package/src/__tests__/memory-boundaries.test.ts +250 -0
- package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +345 -0
- package/src/__tests__/memory-evolution.test.ts +271 -0
- package/src/__tests__/memory-health-rebalance.test.ts +144 -0
- package/src/__tests__/memory-lint.test.ts +421 -0
- package/src/__tests__/memory-output.test.ts +215 -0
- package/src/__tests__/memory-promotion.test.ts +315 -0
- package/src/__tests__/migrate-dedup-ids.test.ts +65 -0
- package/src/__tests__/multi-vector.test.ts +242 -0
- package/src/__tests__/narrative-architecture.test.ts +456 -0
- package/src/__tests__/noise-filter.test.ts +205 -0
- package/src/__tests__/obsidian-connector.test.ts +363 -0
- package/src/__tests__/omitted-reporting.test.ts +98 -0
- package/src/__tests__/pending-review.test.ts +77 -0
- package/src/__tests__/pii-redact.test.ts +314 -0
- package/src/__tests__/post-retrieval-filter.test.ts +178 -0
- package/src/__tests__/ppr-traversal.test.ts +256 -0
- package/src/__tests__/prediction-engine.test.ts +349 -0
- package/src/__tests__/predictive-prospective-memory.test.ts +416 -0
- package/src/__tests__/preference-guard-smoke.test.ts +211 -0
- package/src/__tests__/preference-matcher.test.ts +230 -0
- package/src/__tests__/preference-slots.test.ts +205 -0
- package/src/__tests__/prospective-memory.test.ts +283 -0
- package/src/__tests__/query-entity-detector.test.ts +171 -0
- package/src/__tests__/query-expander-alias.test.ts +146 -0
- package/src/__tests__/query-expander.test.ts +34 -0
- package/src/__tests__/recall-governor.test.ts +227 -0
- package/src/__tests__/recall-mode.test.ts +64 -0
- package/src/__tests__/result-synthesizer.test.ts +198 -0
- package/src/__tests__/resume-context.test.ts +4898 -0
- package/src/__tests__/retrieval-trace.test.ts +213 -0
- package/src/__tests__/retriever-boundary-weight.test.ts +136 -0
- package/src/__tests__/retroactive-boost.test.ts +223 -0
- package/src/__tests__/salience-scoring.test.ts +106 -0
- package/src/__tests__/scope-inventory.test.ts +200 -0
- package/src/__tests__/scope-policy.test.ts +69 -0
- package/src/__tests__/scope-suggester.test.ts +80 -0
- package/src/__tests__/session-checkpoint.test.ts +291 -0
- package/src/__tests__/session-distiller.test.ts +640 -0
- package/src/__tests__/session-gc.test.ts +215 -0
- package/src/__tests__/skill-engine.test.ts +552 -0
- package/src/__tests__/skill-promotion.test.ts +601 -0
- package/src/__tests__/skill-verifier.test.ts +122 -0
- package/src/__tests__/source-heartbeat.test.ts +250 -0
- package/src/__tests__/store-patch-metadata.test.ts +205 -0
- package/src/__tests__/store-scope-audit.test.ts +81 -0
- package/src/__tests__/store-upsert.test.ts +184 -0
- package/src/__tests__/supersede-chain.test.ts +154 -0
- package/src/__tests__/temporal-parser.test.ts +241 -0
- package/src/__tests__/temporal-validity.test.ts +160 -0
- package/src/__tests__/term-registry.test.ts +121 -0
- package/src/__tests__/tool-error-surfacing.test.ts +98 -0
- package/src/__tests__/tool-output-compressor.test.ts +160 -0
- package/src/__tests__/topic-tag.test.ts +113 -0
- package/src/__tests__/trusted-memory.test.ts +839 -0
- package/src/__tests__/usage-tracker.test.ts +287 -0
- package/src/__tests__/version-manager.test.ts +213 -0
- package/src/__tests__/workflow-observation-skill-outcome.test.ts +352 -0
- package/src/__tests__/workflow-observation.test.ts +433 -0
- package/src/__tests__/write-verifier.test.ts +122 -0
- package/src/access-tracker.ts +38 -32
- package/src/activity-counter.ts +46 -18
- package/src/aliases.ts +133 -0
- package/src/api-server.ts +27 -7
- package/src/audit-log.ts +4 -2
- package/src/auto-consolidation.ts +8 -3
- package/src/auto-gc.ts +85 -21
- package/src/capture-engine.ts +54 -21
- package/src/cli.ts +169 -37
- package/src/confidence-tracker.ts +36 -18
- package/src/consolidation-engine.ts +109 -11
- package/src/context-composer.ts +8 -1
- package/src/data-checkup.ts +95 -6
- package/src/distill-lock.ts +194 -15
- package/src/doctor.ts +9 -2
- package/src/dream-pipeline.ts +118 -34
- package/src/env-config.ts +77 -0
- package/src/error-signature.ts +153 -0
- package/src/error-taxonomy.ts +115 -0
- package/src/eval.ts +251 -6
- package/src/idempotency.ts +8 -0
- package/src/ingest.ts +31 -10
- package/src/kg-extractor.ts +6 -2
- package/src/kg-store.ts +236 -44
- package/src/llm-consolidation.ts +2 -1
- package/src/mcp-server.ts +85 -1799
- package/src/mcp-tool-deps.ts +48 -0
- package/src/mcp-tools-advanced.ts +941 -0
- package/src/mcp-tools-core.ts +633 -0
- package/src/mcp-tools-governance.ts +475 -0
- package/src/memory-boundaries.ts +3 -1
- package/src/memory-evolution.ts +73 -1
- package/src/memory-lint.ts +183 -10
- package/src/memory-output.ts +114 -3
- package/src/memory-promotion.ts +246 -0
- package/src/memory-schema.ts +25 -2
- package/src/memory-store-port.ts +43 -0
- package/src/multi-vector.ts +2 -1
- package/src/narrative-schema.ts +2 -1
- package/src/noise-filter.ts +57 -6
- package/src/pii-detector.ts +157 -5
- package/src/query-expander.ts +90 -9
- package/src/result-synthesizer.ts +2 -1
- package/src/retention-policy.ts +2 -1
- package/src/retriever.ts +259 -139
- package/src/runtime-config.ts +8 -1
- package/src/scope-suggester.ts +113 -0
- package/src/session-distiller.ts +23 -2
- package/src/session-engine.ts +2 -1
- package/src/session-schema.ts +1 -0
- package/src/session-store.ts +5 -0
- package/src/skill-engine.ts +78 -0
- package/src/skill-promotion.ts +247 -47
- package/src/skill-schema.ts +16 -3
- package/src/skill-verifier.ts +152 -0
- package/src/store.ts +267 -38
- package/src/tool-output-compressor.ts +3 -1
- package/src/ui-server.ts +2 -1
- package/src/usage-tracker.ts +242 -0
- package/src/version-manager.ts +3 -2
- package/src/workflow-observation-engine.ts +2 -1
- package/src/workflow-observation-schema.ts +2 -1
- package/src/workflow-observation-store.ts +6 -2
- package/.env +0 -4
- package/CLAUDE.md +0 -46
- package/config.json +0 -55
- package/daily-recall.sh +0 -110
- package/logs/daily-recall.error.log +0 -0
- package/logs/daily-recall.log +0 -60
- package/logs/ingest-2026-04-11.log +0 -110
- package/logs/ingest-2026-04-12.log +0 -90
- package/logs/ingest-2026-04-13.log +0 -87
- package/logs/launchagent-stderr.log +0 -0
- package/logs/launchagent-stdout.log +0 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { groupTurnsIntoChunks } from "../ingest.js";
|
|
4
|
+
import { isNoise } from "../noise-filter.js";
|
|
5
|
+
|
|
6
|
+
// P1 defence-in-depth: groupTurnsIntoChunks pre-filters whole turns via
|
|
7
|
+
// filterNoiseTurns BEFORE chunking, but long turns then get split — and the
|
|
8
|
+
// split fragments were never re-checked. A long turn whose tail is orphaned
|
|
9
|
+
// context-JSON therefore leaks severed JSON field-lines as standalone chunks.
|
|
10
|
+
// This guards that no chunk survives as noise, without over-deleting real talk.
|
|
11
|
+
|
|
12
|
+
function userTurn(text: string) {
|
|
13
|
+
return { role: "user" as const, text, timestamp: "2026-05-21T00:00:00Z", sessionId: "s1" };
|
|
14
|
+
}
|
|
15
|
+
function assistantTurn(text: string) {
|
|
16
|
+
return { role: "assistant" as const, text, timestamp: "2026-05-21T00:00:01Z", sessionId: "s1" };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("groupTurnsIntoChunks — post-split noise filtering (P1)", () => {
|
|
20
|
+
it("drops severed context-JSON fragments produced by splitting a long turn", () => {
|
|
21
|
+
// Real-content opener (passes turn-level filterNoiseTurns), then a long tail
|
|
22
|
+
// of orphaned JSON field-lines that chunking peels into standalone chunks.
|
|
23
|
+
const opener = "我们今天把记忆系统这周的进展过一遍,重点是噪声治理和召回质量的提升。".repeat(8);
|
|
24
|
+
const jsonTail = Array.from(
|
|
25
|
+
{ length: 60 },
|
|
26
|
+
(_, i) => `"time": "2026-05-21T15:${String(i).padStart(2, "0")}:03.796Z",`,
|
|
27
|
+
).join("\n");
|
|
28
|
+
const chunks = groupTurnsIntoChunks([userTurn(`${opener}\n${jsonTail}`)]);
|
|
29
|
+
|
|
30
|
+
expect(chunks.length).toBeGreaterThan(0); // real content survives
|
|
31
|
+
// No chunk may survive as a recognizable noise fragment.
|
|
32
|
+
expect(chunks.every((c) => !isNoise(c.text))).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("keeps a normal long Q&A conversation intact (no over-deletion)", () => {
|
|
36
|
+
const longQuestion = "你帮我想想这个方案怎么落地,我担心几个点没考虑周全,得一起捋一捋。".repeat(20);
|
|
37
|
+
const longAnswer = "可以分三步走,先做最小验证看效果,再逐步扩展覆盖面,最后补回归测试兜底。".repeat(20);
|
|
38
|
+
const chunks = groupTurnsIntoChunks([userTurn(longQuestion), assistantTurn(longAnswer)]);
|
|
39
|
+
|
|
40
|
+
expect(chunks.length).toBeGreaterThan(0);
|
|
41
|
+
expect(chunks.every((c) => !isNoise(c.text))).toBe(true); // real talk not deleted
|
|
42
|
+
expect(chunks.some((c) => c.text.includes("最小验证"))).toBe(true); // content retained
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { dedupCheck } from "../ingest.js";
|
|
4
|
+
|
|
5
|
+
function buildSearchResult(text: string, score: number) {
|
|
6
|
+
return {
|
|
7
|
+
score,
|
|
8
|
+
entry: {
|
|
9
|
+
id: `memory-${score}`,
|
|
10
|
+
text,
|
|
11
|
+
vector: [score],
|
|
12
|
+
category: "events",
|
|
13
|
+
scope: "cc:test-session",
|
|
14
|
+
importance: 0.6,
|
|
15
|
+
timestamp: 1_700_000_000_000,
|
|
16
|
+
metadata: "{}",
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe("dedupCheck", () => {
|
|
22
|
+
it("stores a new same-brand item preference instead of treating it as a duplicate topic", async () => {
|
|
23
|
+
let llmCalls = 0;
|
|
24
|
+
const store = {
|
|
25
|
+
async vectorSearch() {
|
|
26
|
+
return [
|
|
27
|
+
buildSearchResult("喜欢吃麦当劳的麦旋风", 0.92),
|
|
28
|
+
buildSearchResult("喜欢吃麦当劳的板烧鸡腿堡", 0.89),
|
|
29
|
+
];
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const llm = {
|
|
33
|
+
async dedupDecision() {
|
|
34
|
+
llmCalls += 1;
|
|
35
|
+
return { action: "SKIP" as const, reason: "same topic" };
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const result = await dedupCheck(
|
|
40
|
+
store as any,
|
|
41
|
+
[1, 0, 0],
|
|
42
|
+
"我喜欢吃麦当劳的麦辣鸡翅",
|
|
43
|
+
llm as any,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
expect(result.action).toBe("store");
|
|
47
|
+
expect(result.reason).toBe("unique");
|
|
48
|
+
expect(result.existingText).toBe("喜欢吃麦当劳的板烧鸡腿堡");
|
|
49
|
+
expect(llmCalls).toBe(0);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("stores a new atomic preference even when the closest match is an aggregate summary", async () => {
|
|
53
|
+
const store = {
|
|
54
|
+
async vectorSearch() {
|
|
55
|
+
return [
|
|
56
|
+
buildSearchResult("喜欢吃麦当劳的麦旋风、板烧鸡腿堡和藤椒鸡派", 0.94),
|
|
57
|
+
];
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const result = await dedupCheck(
|
|
62
|
+
store as any,
|
|
63
|
+
[1, 0, 0],
|
|
64
|
+
"我喜欢吃麦当劳的麦辣鸡翅",
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(result.action).toBe("store");
|
|
68
|
+
expect(result.reason).toBe("unique");
|
|
69
|
+
expect(result.existingText).toContain("麦旋风");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("still skips when an exact atomic preference already exists among the candidates", async () => {
|
|
73
|
+
const store = {
|
|
74
|
+
async vectorSearch() {
|
|
75
|
+
return [
|
|
76
|
+
buildSearchResult("喜欢吃麦当劳的麦旋风、板烧鸡腿堡和藤椒鸡派", 0.94),
|
|
77
|
+
buildSearchResult("我喜欢吃麦当劳的麦辣鸡翅", 0.82),
|
|
78
|
+
];
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const result = await dedupCheck(
|
|
83
|
+
store as any,
|
|
84
|
+
[1, 0, 0],
|
|
85
|
+
"我喜欢吃麦当劳的麦辣鸡翅",
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
expect(result.action).toBe("skip");
|
|
89
|
+
expect(result.reason).toBe("exact");
|
|
90
|
+
expect(result.existingText).toBe("我喜欢吃麦当劳的麦辣鸡翅");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("does not force-create when the same brand-item preference is only rephrased", async () => {
|
|
94
|
+
let llmCalls = 0;
|
|
95
|
+
const store = {
|
|
96
|
+
async vectorSearch() {
|
|
97
|
+
return [
|
|
98
|
+
buildSearchResult("我喜欢吃麦当劳的麦辣鸡翅", 0.75),
|
|
99
|
+
];
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
const llm = {
|
|
103
|
+
async dedupDecision() {
|
|
104
|
+
llmCalls += 1;
|
|
105
|
+
return { action: "SKIP" as const, reason: "same item, rephrased" };
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const result = await dedupCheck(
|
|
110
|
+
store as any,
|
|
111
|
+
[1, 0, 0],
|
|
112
|
+
"用户喜欢麦当劳的麦辣鸡翅",
|
|
113
|
+
llm as any,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
expect(result.action).toBe("skip");
|
|
117
|
+
expect(result.reason).toBe("llm-skip");
|
|
118
|
+
expect(result.existingText).toBe("我喜欢吃麦当劳的麦辣鸡翅");
|
|
119
|
+
expect(llmCalls).toBe(1);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("does not trigger the brand-item guard for non-preference text that mentions the same brand and item", async () => {
|
|
123
|
+
let llmCalls = 0;
|
|
124
|
+
const store = {
|
|
125
|
+
async vectorSearch() {
|
|
126
|
+
return [
|
|
127
|
+
buildSearchResult("我喜欢喝星巴克的抹茶拿铁", 0.75),
|
|
128
|
+
];
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
const llm = {
|
|
132
|
+
async dedupDecision() {
|
|
133
|
+
llmCalls += 1;
|
|
134
|
+
return { action: "SKIP" as const, reason: "same context, not a new preference" };
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const result = await dedupCheck(
|
|
139
|
+
store as any,
|
|
140
|
+
[1, 0, 0],
|
|
141
|
+
"我们刚讨论过星巴克的抹茶拿铁做法",
|
|
142
|
+
llm as any,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
expect(result.action).toBe("skip");
|
|
146
|
+
expect(result.reason).toBe("llm-skip");
|
|
147
|
+
expect(result.existingText).toBe("我喜欢喝星巴克的抹茶拿铁");
|
|
148
|
+
expect(llmCalls).toBe(1);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("stores a new reply-style preference instead of collapsing different style traits into one topic", async () => {
|
|
152
|
+
let llmCalls = 0;
|
|
153
|
+
const store = {
|
|
154
|
+
async vectorSearch() {
|
|
155
|
+
return [
|
|
156
|
+
buildSearchResult("User prefers concise, direct replies.", 0.91),
|
|
157
|
+
];
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
const llm = {
|
|
161
|
+
async dedupDecision() {
|
|
162
|
+
llmCalls += 1;
|
|
163
|
+
return { action: "SKIP" as const, reason: "same topic" };
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const result = await dedupCheck(
|
|
168
|
+
store as any,
|
|
169
|
+
[1, 0, 0],
|
|
170
|
+
"User prefers colloquial, grounded replies.",
|
|
171
|
+
llm as any,
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
expect(result.action).toBe("store");
|
|
175
|
+
expect(result.reason).toBe("unique");
|
|
176
|
+
expect(result.existingText).toBe("User prefers concise, direct replies.");
|
|
177
|
+
expect(llmCalls).toBe(0);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("does not trigger the reply-style guard for descriptive text about a draft", async () => {
|
|
181
|
+
let llmCalls = 0;
|
|
182
|
+
const store = {
|
|
183
|
+
async vectorSearch() {
|
|
184
|
+
return [
|
|
185
|
+
buildSearchResult("User prefers colloquial, grounded replies.", 0.75),
|
|
186
|
+
];
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
const llm = {
|
|
190
|
+
async dedupDecision() {
|
|
191
|
+
llmCalls += 1;
|
|
192
|
+
return { action: "SKIP" as const, reason: "draft note, not a user preference" };
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const result = await dedupCheck(
|
|
197
|
+
store as any,
|
|
198
|
+
[1, 0, 0],
|
|
199
|
+
"这段文案简洁直接,先别改。",
|
|
200
|
+
llm as any,
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
expect(result.action).toBe("skip");
|
|
204
|
+
expect(result.reason).toBe("llm-skip");
|
|
205
|
+
expect(result.existingText).toBe("User prefers colloquial, grounded replies.");
|
|
206
|
+
expect(llmCalls).toBe(1);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("stores a new tool-choice preference instead of collapsing different tool choices into one topic", async () => {
|
|
210
|
+
let llmCalls = 0;
|
|
211
|
+
const store = {
|
|
212
|
+
async vectorSearch() {
|
|
213
|
+
return [
|
|
214
|
+
buildSearchResult("Uses Bun over Node.", 0.91),
|
|
215
|
+
];
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
const llm = {
|
|
219
|
+
async dedupDecision() {
|
|
220
|
+
llmCalls += 1;
|
|
221
|
+
return { action: "SKIP" as const, reason: "same topic" };
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const result = await dedupCheck(
|
|
226
|
+
store as any,
|
|
227
|
+
[1, 0, 0],
|
|
228
|
+
"Prefers rg over grep.",
|
|
229
|
+
llm as any,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
expect(result.action).toBe("store");
|
|
233
|
+
expect(result.reason).toBe("unique");
|
|
234
|
+
expect(result.existingText).toBe("Uses Bun over Node.");
|
|
235
|
+
expect(llmCalls).toBe(0);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("does not trigger the tool-choice guard for narrative migration text", async () => {
|
|
239
|
+
let llmCalls = 0;
|
|
240
|
+
const store = {
|
|
241
|
+
async vectorSearch() {
|
|
242
|
+
return [
|
|
243
|
+
buildSearchResult("Prefers rg over grep.", 0.75),
|
|
244
|
+
];
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
const llm = {
|
|
248
|
+
async dedupDecision() {
|
|
249
|
+
llmCalls += 1;
|
|
250
|
+
return { action: "SKIP" as const, reason: "migration note, not a tool preference" };
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const result = await dedupCheck(
|
|
255
|
+
store as any,
|
|
256
|
+
[1, 0, 0],
|
|
257
|
+
"文档里写了 uses Bun over Node 的迁移说明。",
|
|
258
|
+
llm as any,
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
expect(result.action).toBe("skip");
|
|
262
|
+
expect(result.reason).toBe("llm-skip");
|
|
263
|
+
expect(result.existingText).toBe("Prefers rg over grep.");
|
|
264
|
+
expect(llmCalls).toBe(1);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("stores a borderline chunk when LLM says it should CREATE", async () => {
|
|
268
|
+
let llmCalls = 0;
|
|
269
|
+
const store = {
|
|
270
|
+
async vectorSearch() {
|
|
271
|
+
return [
|
|
272
|
+
buildSearchResult(
|
|
273
|
+
"A2A gateway 之前是先本地 smoke,再补 Claude SDK 配置。",
|
|
274
|
+
0.74,
|
|
275
|
+
),
|
|
276
|
+
];
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
const llm = {
|
|
280
|
+
async dedupDecision() {
|
|
281
|
+
llmCalls += 1;
|
|
282
|
+
return { action: "CREATE" as const, reason: "new implementation branch" };
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const result = await dedupCheck(
|
|
287
|
+
store as any,
|
|
288
|
+
[1, 0, 0],
|
|
289
|
+
"A2A gateway 升级后要补 MCP transport 配置和 LaunchAgent 环境变量,不是原来的 smoke 流程。",
|
|
290
|
+
llm as any,
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
expect(result.action).toBe("store");
|
|
294
|
+
expect(result.reason).toBe("unique");
|
|
295
|
+
expect(llmCalls).toBe(1);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("stores a borderline chunk when LLM says it adds new information and should MERGE", async () => {
|
|
299
|
+
let llmCalls = 0;
|
|
300
|
+
const store = {
|
|
301
|
+
async vectorSearch() {
|
|
302
|
+
return [
|
|
303
|
+
buildSearchResult(
|
|
304
|
+
"A2A 调 Claude SDK 时,可以先看 `adapters/claude.js` 里的权限相关配置。",
|
|
305
|
+
0.77,
|
|
306
|
+
),
|
|
307
|
+
];
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
const llm = {
|
|
311
|
+
async dedupDecision() {
|
|
312
|
+
llmCalls += 1;
|
|
313
|
+
return { action: "MERGE" as const, reason: "same topic but new implementation detail" };
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const result = await dedupCheck(
|
|
318
|
+
store as any,
|
|
319
|
+
[1, 0, 0],
|
|
320
|
+
"给 A2A 调用传 permissionMode: \"dontAsk\" + allowedTools,避免 Claude SDK 在后台 HTTP handler 里卡死等权限确认。",
|
|
321
|
+
llm as any,
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
expect(result.action).toBe("store");
|
|
325
|
+
expect(result.reason).toBe("llm-merge");
|
|
326
|
+
expect(result.existingText).toContain("权限相关配置");
|
|
327
|
+
expect(llmCalls).toBe(1);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("skips a hard-threshold duplicate without calling the LLM", async () => {
|
|
331
|
+
let llmCalls = 0;
|
|
332
|
+
const store = {
|
|
333
|
+
async vectorSearch() {
|
|
334
|
+
return [
|
|
335
|
+
buildSearchResult(
|
|
336
|
+
"OpenClaw provider 配置里已经写过 `ANTHROPIC_BASE_URL` 和 `ANTHROPIC_AUTH_TOKEN` 的设置方法。",
|
|
337
|
+
0.95,
|
|
338
|
+
),
|
|
339
|
+
];
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
const llm = {
|
|
343
|
+
async dedupDecision() {
|
|
344
|
+
llmCalls += 1;
|
|
345
|
+
return { action: "MERGE" as const, reason: "should never be called" };
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const result = await dedupCheck(
|
|
350
|
+
store as any,
|
|
351
|
+
[1, 0, 0],
|
|
352
|
+
"OpenClaw provider 配置里已经写过 `ANTHROPIC_BASE_URL` 和 `ANTHROPIC_AUTH_TOKEN` 的设置方法,但这里重复了一遍。",
|
|
353
|
+
llm as any,
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
expect(result.action).toBe("skip");
|
|
357
|
+
expect(result.reason).toBe("hard");
|
|
358
|
+
expect(llmCalls).toBe(0);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
@@ -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
|
+
});
|