recallnest 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.md +222 -254
  3. package/README_CN.md +206 -243
  4. package/package.json +3 -2
  5. package/src/capture-engine.ts +26 -4
  6. package/src/context-composer-stable.ts +5 -1
  7. package/src/embedder.ts +24 -0
  8. package/src/env-config.ts +18 -0
  9. package/src/ingest.ts +225 -17
  10. package/src/kg-extractor.ts +17 -0
  11. package/src/memory-output.ts +35 -0
  12. package/src/pivot-apply.ts +52 -1
  13. package/src/pivot-evidence.ts +128 -0
  14. package/src/ppr-traversal.ts +25 -1
  15. package/src/runtime-config.ts +9 -0
  16. package/src/session-engine.ts +11 -4
  17. package/src/session-output.ts +9 -1
  18. package/src/session-store.ts +2 -3
  19. package/src/skill-promotion.ts +67 -1
  20. package/src/__tests__/access-tracker-novelty.test.ts +0 -324
  21. package/src/__tests__/activity-counter.test.ts +0 -125
  22. package/src/__tests__/adaptive-fidelity.test.ts +0 -102
  23. package/src/__tests__/admission-control.test.ts +0 -206
  24. package/src/__tests__/age-label.test.ts +0 -33
  25. package/src/__tests__/anchor-generator.test.ts +0 -67
  26. package/src/__tests__/api-server-security.test.ts +0 -102
  27. package/src/__tests__/auto-consolidation.test.ts +0 -168
  28. package/src/__tests__/auto-recall.test.ts +0 -131
  29. package/src/__tests__/babel-integration.test.ts +0 -144
  30. package/src/__tests__/batch-dedup.test.ts +0 -65
  31. package/src/__tests__/belief-history.test.ts +0 -220
  32. package/src/__tests__/canary-eval.test.ts +0 -114
  33. package/src/__tests__/capture-engine.test.ts +0 -1328
  34. package/src/__tests__/capture-heuristic.test.ts +0 -171
  35. package/src/__tests__/cascade-forget.test.ts +0 -210
  36. package/src/__tests__/category-min-scores.test.ts +0 -116
  37. package/src/__tests__/cc8-cc1-essential-context.test.ts +0 -298
  38. package/src/__tests__/circuit-breaker.test.ts +0 -99
  39. package/src/__tests__/cluster-consolidation.test.ts +0 -1236
  40. package/src/__tests__/confidence-metadata.test.ts +0 -125
  41. package/src/__tests__/confidence-tracker.test.ts +0 -261
  42. package/src/__tests__/conflict-advisor.test.ts +0 -206
  43. package/src/__tests__/conflict-escalation.test.ts +0 -149
  44. package/src/__tests__/conflict-lifecycle.test.ts +0 -83
  45. package/src/__tests__/conflict-output.test.ts +0 -85
  46. package/src/__tests__/conflict-store.test.ts +0 -418
  47. package/src/__tests__/connector-v1.test.ts +0 -181
  48. package/src/__tests__/consolidation-engine.test.ts +0 -520
  49. package/src/__tests__/constructive-retrieval.test.ts +0 -509
  50. package/src/__tests__/context-collapse-renderer.test.ts +0 -214
  51. package/src/__tests__/context-composer-pins.test.ts +0 -64
  52. package/src/__tests__/context-composer-task-selection.test.ts +0 -24
  53. package/src/__tests__/context-reconstructor.test.ts +0 -65
  54. package/src/__tests__/context-renderer.test.ts +0 -91
  55. package/src/__tests__/continuity-eval.test.ts +0 -275
  56. package/src/__tests__/continuity-schema.test.ts +0 -97
  57. package/src/__tests__/contracts.test.ts +0 -262
  58. package/src/__tests__/conversation-importer.test.ts +0 -692
  59. package/src/__tests__/core-recall-eval.test.ts +0 -235
  60. package/src/__tests__/core-summary.test.ts +0 -109
  61. package/src/__tests__/data-checkup.test.ts +0 -304
  62. package/src/__tests__/decay-exemption.test.ts +0 -129
  63. package/src/__tests__/dedup-actions.test.ts +0 -58
  64. package/src/__tests__/dedup-guard.test.ts +0 -39
  65. package/src/__tests__/deterministic-id.test.ts +0 -39
  66. package/src/__tests__/distill-lock.test.ts +0 -305
  67. package/src/__tests__/distill-session-argshape.test.ts +0 -79
  68. package/src/__tests__/doctor.test.ts +0 -319
  69. package/src/__tests__/dream-pipeline.test.ts +0 -820
  70. package/src/__tests__/dream-scope-isolation.test.ts +0 -386
  71. package/src/__tests__/dream-write-batching.test.ts +0 -211
  72. package/src/__tests__/drill-down.test.ts +0 -108
  73. package/src/__tests__/embedder.test.ts +0 -122
  74. package/src/__tests__/emotion-decay.test.ts +0 -46
  75. package/src/__tests__/emotion-detector.test.ts +0 -56
  76. package/src/__tests__/emotion-phase1-integration.test.ts +0 -153
  77. package/src/__tests__/emotion-retrieval.test.ts +0 -56
  78. package/src/__tests__/entity-resolver.test.ts +0 -58
  79. package/src/__tests__/env-config-invariant.test.ts +0 -80
  80. package/src/__tests__/env-config.test.ts +0 -211
  81. package/src/__tests__/error-signature-boost.test.ts +0 -87
  82. package/src/__tests__/error-signature.test.ts +0 -95
  83. package/src/__tests__/error-taxonomy.test.ts +0 -129
  84. package/src/__tests__/ethics-layer.test.ts +0 -476
  85. package/src/__tests__/event-segmenter.test.ts +0 -153
  86. package/src/__tests__/feature-flags-integration.test.ts +0 -54
  87. package/src/__tests__/frequency-tracker.test.ts +0 -113
  88. package/src/__tests__/freshness.test.ts +0 -186
  89. package/src/__tests__/graph-export.test.ts +0 -489
  90. package/src/__tests__/hotness-blend.test.ts +0 -283
  91. package/src/__tests__/ingest-chunking.test.ts +0 -44
  92. package/src/__tests__/ingest-scope-prefix.test.ts +0 -69
  93. package/src/__tests__/ingest.test.ts +0 -360
  94. package/src/__tests__/interference-detector.test.ts +0 -203
  95. package/src/__tests__/kg-extractor.test.ts +0 -192
  96. package/src/__tests__/kg-store.test.ts +0 -300
  97. package/src/__tests__/llm-client-pivot.test.ts +0 -628
  98. package/src/__tests__/llm-consolidation.test.ts +0 -226
  99. package/src/__tests__/maintenance-locks.test.ts +0 -197
  100. package/src/__tests__/mcp-efficiency.test.ts +0 -288
  101. package/src/__tests__/mcp-related-scopes.test.ts +0 -151
  102. package/src/__tests__/memory-boundaries.test.ts +0 -282
  103. package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +0 -362
  104. package/src/__tests__/memory-evolution.test.ts +0 -271
  105. package/src/__tests__/memory-health-rebalance.test.ts +0 -144
  106. package/src/__tests__/memory-lint.test.ts +0 -421
  107. package/src/__tests__/memory-output.test.ts +0 -224
  108. package/src/__tests__/memory-promotion.test.ts +0 -315
  109. package/src/__tests__/memory-utility.test.ts +0 -256
  110. package/src/__tests__/migrate-dedup-ids.test.ts +0 -65
  111. package/src/__tests__/multi-vector.test.ts +0 -242
  112. package/src/__tests__/narrative-architecture.test.ts +0 -456
  113. package/src/__tests__/noise-filter.test.ts +0 -205
  114. package/src/__tests__/noise-prototype-bank.test.ts +0 -280
  115. package/src/__tests__/obsidian-connector.test.ts +0 -363
  116. package/src/__tests__/omitted-reporting.test.ts +0 -98
  117. package/src/__tests__/openai-contract.test.ts +0 -513
  118. package/src/__tests__/pending-review.test.ts +0 -77
  119. package/src/__tests__/pii-redact.test.ts +0 -333
  120. package/src/__tests__/pivot-apply.test.ts +0 -373
  121. package/src/__tests__/pivot-distill-supervisor.test.ts +0 -457
  122. package/src/__tests__/pivot-distill.test.ts +0 -1178
  123. package/src/__tests__/post-retrieval-filter.test.ts +0 -178
  124. package/src/__tests__/ppr-traversal.test.ts +0 -256
  125. package/src/__tests__/prediction-engine.test.ts +0 -349
  126. package/src/__tests__/predictive-prospective-memory.test.ts +0 -416
  127. package/src/__tests__/preference-guard-smoke.test.ts +0 -211
  128. package/src/__tests__/preference-matcher.test.ts +0 -282
  129. package/src/__tests__/preference-slots.test.ts +0 -205
  130. package/src/__tests__/promotion-distinct-sources.test.ts +0 -162
  131. package/src/__tests__/prospective-memory.test.ts +0 -283
  132. package/src/__tests__/pull-from-macbook-sync-only.test.ts +0 -679
  133. package/src/__tests__/query-entity-detector.test.ts +0 -171
  134. package/src/__tests__/query-expander-alias.test.ts +0 -146
  135. package/src/__tests__/query-expander.test.ts +0 -34
  136. package/src/__tests__/recall-gate.test.ts +0 -266
  137. package/src/__tests__/recall-governor.test.ts +0 -227
  138. package/src/__tests__/recall-mode.test.ts +0 -64
  139. package/src/__tests__/resolve-db-path.test.ts +0 -130
  140. package/src/__tests__/resolve-tier.test.ts +0 -64
  141. package/src/__tests__/result-synthesizer.test.ts +0 -198
  142. package/src/__tests__/resume-context.test.ts +0 -4898
  143. package/src/__tests__/retrieval-eval.test.ts +0 -163
  144. package/src/__tests__/retrieval-trace.test.ts +0 -213
  145. package/src/__tests__/retrieve-audit-revision.test.ts +0 -255
  146. package/src/__tests__/retriever-boundary-weight.test.ts +0 -136
  147. package/src/__tests__/retriever-lazy-init-dispatch.test.ts +0 -123
  148. package/src/__tests__/retroactive-boost.test.ts +0 -223
  149. package/src/__tests__/salience-scoring.test.ts +0 -106
  150. package/src/__tests__/scope-inventory.test.ts +0 -200
  151. package/src/__tests__/scope-policy.test.ts +0 -69
  152. package/src/__tests__/scope-suggester.test.ts +0 -80
  153. package/src/__tests__/session-checkpoint.test.ts +0 -291
  154. package/src/__tests__/session-distiller.test.ts +0 -640
  155. package/src/__tests__/session-gc.test.ts +0 -215
  156. package/src/__tests__/skill-engine.test.ts +0 -552
  157. package/src/__tests__/skill-promotion.test.ts +0 -654
  158. package/src/__tests__/skill-verifier.test.ts +0 -122
  159. package/src/__tests__/source-heartbeat.test.ts +0 -254
  160. package/src/__tests__/stable-context-length-contract.test.ts +0 -62
  161. package/src/__tests__/store-patch-metadata.test.ts +0 -235
  162. package/src/__tests__/store-read-consistency.test.ts +0 -122
  163. package/src/__tests__/store-schema-migration.test.ts +0 -87
  164. package/src/__tests__/store-scope-audit.test.ts +0 -81
  165. package/src/__tests__/store-upsert.test.ts +0 -201
  166. package/src/__tests__/supersede-chain.test.ts +0 -154
  167. package/src/__tests__/synthesis-contract.test.ts +0 -252
  168. package/src/__tests__/synthesis-promotion.test.ts +0 -369
  169. package/src/__tests__/temporal-parser.test.ts +0 -241
  170. package/src/__tests__/temporal-validity.test.ts +0 -160
  171. package/src/__tests__/term-registry-terminal-roster.test.ts +0 -127
  172. package/src/__tests__/term-registry.test.ts +0 -121
  173. package/src/__tests__/tool-error-surfacing.test.ts +0 -98
  174. package/src/__tests__/tool-output-compressor.test.ts +0 -160
  175. package/src/__tests__/topic-tag.test.ts +0 -113
  176. package/src/__tests__/trusted-memory.test.ts +0 -867
  177. package/src/__tests__/ui-server-security.test.ts +0 -93
  178. package/src/__tests__/usage-tracker.test.ts +0 -287
  179. package/src/__tests__/utility-weight.test.ts +0 -57
  180. package/src/__tests__/verbatim-self-recall.test.ts +0 -279
  181. package/src/__tests__/version-manager.test.ts +0 -248
  182. package/src/__tests__/workflow-observation-skill-outcome.test.ts +0 -352
  183. package/src/__tests__/workflow-observation.test.ts +0 -433
  184. package/src/__tests__/write-verifier.test.ts +0 -122
@@ -1,192 +0,0 @@
1
- import { describe, expect, it } from "bun:test";
2
- import { normalizeEntity, normalizePredicate, KGExtractor, isKGModeEnabled } from "../kg-extractor.js";
3
-
4
- function createMockLlm(response: unknown) {
5
- return {
6
- lastSystem: null as string | null,
7
- lastUser: null as string | null,
8
- async chatJson(_system: string, _user: string) {
9
- this.lastSystem = _system;
10
- this.lastUser = _user;
11
- return response;
12
- },
13
- // Stub remaining LLMClient methods
14
- async generateL0() { return null; },
15
- async generateL1() { return null; },
16
- async smartExtract() { return null; },
17
- async smartExtractBatch() { return []; },
18
- async dedupDecision() { return null; },
19
- async dedupBatch() { return []; },
20
- async mergeTexts() { return null; },
21
- async preferenceMatch() { return null; },
22
- async ping() { return false; },
23
- } as any;
24
- }
25
-
26
- function createMockKGStore() {
27
- return {
28
- storedTriples: [] as any[],
29
- async createTriples(triples: any[]) {
30
- const result = triples.map((t: any, i: number) => ({
31
- ...t,
32
- id: `mock-${i}`,
33
- timestamp: Date.now(),
34
- }));
35
- this.storedTriples.push(...result);
36
- return result;
37
- },
38
- } as any;
39
- }
40
-
41
- describe("normalizeEntity", () => {
42
- it("trims and collapses whitespace", () => {
43
- expect(normalizeEntity(" Hello World ")).toBe("Hello World");
44
- });
45
- it("converts to title case", () => {
46
- expect(normalizeEntity("alice")).toBe("Alice");
47
- expect(normalizeEntity("PYTHON")).toBe("Python");
48
- });
49
- it("preserves CJK text", () => {
50
- expect(normalizeEntity("张三")).toBe("张三");
51
- });
52
- it("strips quotes", () => {
53
- expect(normalizeEntity('"Alice"')).toBe("Alice");
54
- });
55
- it("returns empty for empty input", () => {
56
- expect(normalizeEntity("")).toBe("");
57
- });
58
- });
59
-
60
- describe("normalizePredicate", () => {
61
- it("converts to snake_case", () => {
62
- expect(normalizePredicate("works with")).toBe("works_with");
63
- expect(normalizePredicate("Created By")).toBe("created_by");
64
- });
65
- it("preserves CJK predicates", () => {
66
- expect(normalizePredicate("属于")).toBe("属于");
67
- });
68
- });
69
-
70
- describe("isKGModeEnabled", () => {
71
- it("reads env var", () => {
72
- const orig = process.env.RECALLNEST_KG_MODE;
73
- try {
74
- process.env.RECALLNEST_KG_MODE = "true";
75
- expect(isKGModeEnabled()).toBe(true);
76
- delete process.env.RECALLNEST_KG_MODE;
77
- expect(isKGModeEnabled()).toBe(false);
78
- } finally {
79
- if (orig !== undefined) process.env.RECALLNEST_KG_MODE = orig;
80
- else delete process.env.RECALLNEST_KG_MODE;
81
- }
82
- });
83
- });
84
-
85
- describe("KGExtractor.extract", () => {
86
- it("extracts valid triples", async () => {
87
- const llm = createMockLlm({
88
- triples: [
89
- { subject: "Alice", predicate: "uses", object: "Python", confidence: 0.9 },
90
- { subject: "Alice", predicate: "works_at", object: "Google", confidence: 0.85 },
91
- ],
92
- });
93
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
94
- const triples = await ext.extract("Alice uses Python at Google");
95
- expect(triples.length).toBe(2);
96
- expect(triples[0].subject).toBe("Alice");
97
- });
98
-
99
- it("filters low confidence", async () => {
100
- const llm = createMockLlm({
101
- triples: [
102
- { subject: "Alice", predicate: "uses", object: "Python", confidence: 0.9 },
103
- { subject: "Alice", predicate: "maybe", object: "Bob", confidence: 0.3 },
104
- ],
105
- });
106
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
107
- const triples = await ext.extract("Alice uses Python and maybe Bob");
108
- expect(triples.length).toBe(1);
109
- });
110
-
111
- it("normalizes entities", async () => {
112
- const llm = createMockLlm({
113
- triples: [{ subject: " alice ", predicate: "WORKS WITH", object: "bob", confidence: 0.9 }],
114
- });
115
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
116
- const triples = await ext.extract("alice works with bob");
117
- expect(triples[0].subject).toBe("Alice");
118
- expect(triples[0].predicate).toBe("works_with");
119
- expect(triples[0].object).toBe("Bob");
120
- });
121
-
122
- it("skips self-referencing triples", async () => {
123
- const llm = createMockLlm({
124
- triples: [
125
- { subject: "Alice", predicate: "is", object: "Alice", confidence: 0.9 },
126
- { subject: "Bob", predicate: "uses", object: "Go", confidence: 0.9 },
127
- ],
128
- });
129
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
130
- const triples = await ext.extract("Alice is Alice, Bob uses Go");
131
- expect(triples.length).toBe(1);
132
- expect(triples[0].subject).toBe("Bob");
133
- });
134
-
135
- it("deduplicates within batch", async () => {
136
- const llm = createMockLlm({
137
- triples: [
138
- { subject: "Alice", predicate: "uses", object: "Python", confidence: 0.9 },
139
- { subject: "Alice", predicate: "uses", object: "Python", confidence: 0.85 },
140
- ],
141
- });
142
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
143
- expect((await ext.extract("Alice uses Python a lot")).length).toBe(1);
144
- });
145
-
146
- it("handles null LLM response", async () => {
147
- const llm = createMockLlm(null);
148
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
149
- expect((await ext.extract("Some text here")).length).toBe(0);
150
- });
151
-
152
- it("propagates LLM transport failure (not silently zero triples)", async () => {
153
- const llm = createMockLlm(null);
154
- llm.chatJson = async () => { throw new Error("rate limited"); };
155
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
156
- // Backfill relies on this to avoid journaling failed extractions as processed
157
- await expect(ext.extract("Some text long enough here")).rejects.toThrow("rate limited");
158
- });
159
-
160
- it("skips short text", async () => {
161
- const llm = createMockLlm({ triples: [] });
162
- const ext = new KGExtractor({ llmClient: llm, kgStore: createMockKGStore() });
163
- expect((await ext.extract("hi")).length).toBe(0);
164
- expect(llm.lastUser).toBeNull(); // LLM not called
165
- });
166
- });
167
-
168
- describe("KGExtractor.extractAndStore", () => {
169
- it("persists triples to KG store", async () => {
170
- const llm = createMockLlm({
171
- triples: [
172
- { subject: "Alice", predicate: "uses", object: "Python", confidence: 0.9 },
173
- ],
174
- });
175
- const kgStore = createMockKGStore();
176
- const ext = new KGExtractor({ llmClient: llm, kgStore });
177
- const count = await ext.extractAndStore("Alice uses Python", "mem-001", "global");
178
- expect(count).toBe(1);
179
- expect(kgStore.storedTriples[0].scope).toBe("global");
180
- expect(kgStore.storedTriples[0].source_memory_id).toBe("mem-001");
181
- });
182
-
183
- it("caps source_text at 500 chars", async () => {
184
- const llm = createMockLlm({
185
- triples: [{ subject: "A", predicate: "uses", object: "B", confidence: 0.9 }],
186
- });
187
- const kgStore = createMockKGStore();
188
- const ext = new KGExtractor({ llmClient: llm, kgStore });
189
- await ext.extractAndStore("x".repeat(1000), "mem-001", "global");
190
- expect(kgStore.storedTriples[0].source_text.length).toBe(500);
191
- });
192
- });
@@ -1,300 +0,0 @@
1
- import { describe, expect, it, beforeEach, afterEach } from "bun:test";
2
- import { mkdtempSync, rmSync } from "node:fs";
3
- import { join } from "node:path";
4
- import { tmpdir } from "node:os";
5
- import { KGStore, tripleId } from "../kg-store.js";
6
-
7
- let workDir: string;
8
-
9
- function makeTriple(overrides: Record<string, unknown> = {}) {
10
- return {
11
- scope: "global",
12
- subject: "Alice",
13
- predicate: "uses",
14
- object: "Python",
15
- confidence: 0.9,
16
- source_memory_id: "mem-001",
17
- source_text: "Alice uses Python",
18
- ...overrides,
19
- };
20
- }
21
-
22
- describe("tripleId", () => {
23
- it("produces deterministic IDs", () => {
24
- const id1 = tripleId("global", "Alice", "uses", "Python");
25
- const id2 = tripleId("global", "Alice", "uses", "Python");
26
- expect(id1).toBe(id2);
27
- });
28
-
29
- it("different triples produce different IDs", () => {
30
- const id1 = tripleId("global", "Alice", "uses", "Python");
31
- const id2 = tripleId("global", "Alice", "uses", "JavaScript");
32
- expect(id1).not.toBe(id2);
33
- });
34
-
35
- it("scope affects ID", () => {
36
- const id1 = tripleId("global", "Alice", "uses", "Python");
37
- const id2 = tripleId("agent:bob", "Alice", "uses", "Python");
38
- expect(id1).not.toBe(id2);
39
- });
40
- });
41
-
42
- describe("KGStore", () => {
43
- beforeEach(() => {
44
- workDir = mkdtempSync(join(tmpdir(), "kg-store-test-"));
45
- });
46
- afterEach(() => {
47
- try { rmSync(workDir, { recursive: true, force: true }); } catch {}
48
- });
49
-
50
- describe("createTriple", () => {
51
- it("stores a triple", async () => {
52
- const store = new KGStore({ dbPath: workDir });
53
- const t = await store.createTriple(makeTriple());
54
- expect(t.id).toBeTruthy();
55
- expect(t.subject).toBe("Alice");
56
- expect(t.predicate).toBe("uses");
57
- expect(t.object).toBe("Python");
58
- expect(t.timestamp).toBeGreaterThan(0);
59
- });
60
-
61
- it("upserts on duplicate", async () => {
62
- const store = new KGStore({ dbPath: workDir });
63
- const t1 = await store.createTriple(makeTriple({ confidence: 0.8 }));
64
- const t2 = await store.createTriple(makeTriple({ confidence: 0.95 }));
65
- expect(t1.id).toBe(t2.id);
66
- expect(await store.countTriples()).toBe(1);
67
- });
68
- });
69
-
70
- describe("mention counting", () => {
71
- it("new triple starts at mention_count 1 with its source recorded", async () => {
72
- const store = new KGStore({ dbPath: workDir });
73
- const t = await store.createTriple(makeTriple());
74
- expect(t.mention_count).toBe(1);
75
- expect(t.first_seen).toBeGreaterThan(0);
76
- expect(JSON.parse(t.source_memory_ids)).toEqual(["mem-001"]);
77
- });
78
-
79
- it("same triple from a different source memory increments count", async () => {
80
- const store = new KGStore({ dbPath: workDir });
81
- const t1 = await store.createTriple(makeTriple({ source_memory_id: "mem-001" }));
82
- const t2 = await store.createTriple(makeTriple({ source_memory_id: "mem-002" }));
83
- expect(t2.id).toBe(t1.id);
84
- expect(t2.mention_count).toBe(2);
85
- expect(JSON.parse(t2.source_memory_ids).sort()).toEqual(["mem-001", "mem-002"]);
86
- expect(t2.first_seen).toBe(t1.first_seen);
87
- expect(await store.countTriples()).toBe(1);
88
- });
89
-
90
- it("same triple from the same source memory does NOT increment count", async () => {
91
- const store = new KGStore({ dbPath: workDir });
92
- const t1 = await store.createTriple(makeTriple({ confidence: 0.8 }));
93
- const t2 = await store.createTriple(makeTriple({ confidence: 0.95 }));
94
- expect(t2.mention_count).toBe(1);
95
- expect(JSON.parse(t2.source_memory_ids)).toEqual(["mem-001"]);
96
- // confidence keeps the max seen
97
- expect(t2.confidence).toBe(0.95);
98
- const again = await store.createTriple(makeTriple({ confidence: 0.5 }));
99
- expect(again.confidence).toBe(0.95);
100
- });
101
-
102
- it("batch with same triple from different sources counts each source once", async () => {
103
- const store = new KGStore({ dbPath: workDir });
104
- const triples = await store.createTriples([
105
- makeTriple({ source_memory_id: "mem-001" }),
106
- makeTriple({ source_memory_id: "mem-002" }),
107
- makeTriple({ source_memory_id: "mem-002" }), // duplicate source in batch
108
- ]);
109
- expect(triples.length).toBe(1);
110
- expect(triples[0].mention_count).toBe(2);
111
- expect(await store.countTriples()).toBe(1);
112
- });
113
-
114
- it("count survives across batch and single writes", async () => {
115
- const store = new KGStore({ dbPath: workDir });
116
- await store.createTriples([makeTriple({ source_memory_id: "mem-001" })]);
117
- await store.createTriples([makeTriple({ source_memory_id: "mem-002" })]);
118
- const t = await store.createTriple(makeTriple({ source_memory_id: "mem-003" }));
119
- expect(t.mention_count).toBe(3);
120
- });
121
- });
122
-
123
- describe("createTriples (batch)", () => {
124
- it("stores multiple triples", async () => {
125
- const store = new KGStore({ dbPath: workDir });
126
- const triples = await store.createTriples([
127
- makeTriple({ subject: "Alice", object: "Python" }),
128
- makeTriple({ subject: "Alice", object: "JavaScript" }),
129
- makeTriple({ subject: "Bob", object: "Go" }),
130
- ]);
131
- expect(triples.length).toBe(3);
132
- expect(await store.countTriples()).toBe(3);
133
- });
134
-
135
- it("deduplicates within batch", async () => {
136
- const store = new KGStore({ dbPath: workDir });
137
- const triples = await store.createTriples([
138
- makeTriple({ subject: "Alice", object: "Python" }),
139
- makeTriple({ subject: "Alice", object: "Python" }),
140
- ]);
141
- expect(triples.length).toBe(1);
142
- });
143
-
144
- it("handles empty batch", async () => {
145
- const store = new KGStore({ dbPath: workDir });
146
- expect(await store.createTriples([])).toEqual([]);
147
- });
148
- });
149
-
150
- describe("edge queries", () => {
151
- it("getOutgoingEdges", async () => {
152
- const store = new KGStore({ dbPath: workDir });
153
- await store.createTriples([
154
- makeTriple({ subject: "Alice", predicate: "uses", object: "Python" }),
155
- makeTriple({ subject: "Alice", predicate: "knows", object: "Bob" }),
156
- makeTriple({ subject: "Bob", predicate: "uses", object: "Go" }),
157
- ]);
158
- const edges = await store.getOutgoingEdges("Alice");
159
- expect(edges.length).toBe(2);
160
- expect(edges.every((e) => e.subject === "Alice")).toBe(true);
161
- });
162
-
163
- it("getIncomingEdges", async () => {
164
- const store = new KGStore({ dbPath: workDir });
165
- await store.createTriples([
166
- makeTriple({ subject: "Alice", predicate: "knows", object: "Bob" }),
167
- makeTriple({ subject: "Charlie", predicate: "knows", object: "Bob" }),
168
- makeTriple({ subject: "Bob", predicate: "uses", object: "Go" }),
169
- ]);
170
- const edges = await store.getIncomingEdges("Bob");
171
- expect(edges.length).toBe(2);
172
- expect(edges.every((e) => e.object === "Bob")).toBe(true);
173
- });
174
- });
175
-
176
- describe("getNeighborhood (BFS)", () => {
177
- it("returns 1-hop neighborhood", async () => {
178
- const store = new KGStore({ dbPath: workDir });
179
- await store.createTriples([
180
- makeTriple({ subject: "Alice", predicate: "knows", object: "Bob" }),
181
- makeTriple({ subject: "Alice", predicate: "uses", object: "Python" }),
182
- makeTriple({ subject: "Bob", predicate: "uses", object: "Go" }),
183
- makeTriple({ subject: "Charlie", predicate: "uses", object: "Rust" }),
184
- ]);
185
- const hood = await store.getNeighborhood(["Alice"], 1);
186
- const entities = hood.map((n) => n.entity).sort();
187
- expect(entities).toContain("Alice");
188
- expect(entities).toContain("Bob");
189
- expect(entities).toContain("Python");
190
- expect(entities).not.toContain("Charlie");
191
- });
192
-
193
- it("returns 2-hop neighborhood", async () => {
194
- const store = new KGStore({ dbPath: workDir });
195
- await store.createTriples([
196
- makeTriple({ subject: "Alice", predicate: "knows", object: "Bob" }),
197
- makeTriple({ subject: "Bob", predicate: "knows", object: "Charlie" }),
198
- ]);
199
- const hood = await store.getNeighborhood(["Alice"], 2);
200
- const entities = hood.map((n) => n.entity);
201
- expect(entities).toContain("Charlie");
202
- });
203
- });
204
-
205
- describe("scope isolation", () => {
206
- it("edge queries respect scope filter", async () => {
207
- const store = new KGStore({ dbPath: workDir });
208
- await store.createTriples([
209
- makeTriple({ scope: "agent:alice", subject: "Alice", object: "Python" }),
210
- makeTriple({ scope: "agent:bob", subject: "Alice", object: "Go" }),
211
- ]);
212
- const edges = await store.getOutgoingEdges("Alice", "agent:alice");
213
- expect(edges.length).toBe(1);
214
- expect(edges[0].object).toBe("Python");
215
- });
216
- });
217
-
218
- describe("deleteBySource", () => {
219
- it("deletes all triples from a source memory", async () => {
220
- const store = new KGStore({ dbPath: workDir });
221
- await store.createTriples([
222
- makeTriple({ source_memory_id: "mem-001", subject: "Alice", object: "Python" }),
223
- makeTriple({ source_memory_id: "mem-001", subject: "Alice", object: "Go" }),
224
- makeTriple({ source_memory_id: "mem-002", subject: "Bob", object: "Rust" }),
225
- ]);
226
- await store.deleteBySource("mem-001");
227
- expect(await store.countTriples()).toBe(1);
228
- });
229
-
230
- it("multi-source triple survives forgetting one source (count decremented)", async () => {
231
- const store = new KGStore({ dbPath: workDir });
232
- await store.createTriple(makeTriple({ source_memory_id: "mem-001" }));
233
- await store.createTriple(makeTriple({ source_memory_id: "mem-002" }));
234
-
235
- await store.deleteBySource("mem-001");
236
- // Fact still evidenced by mem-002 — row survives with decremented count
237
- expect(await store.countTriples()).toBe(1);
238
- const edges = await store.getOutgoingEdges("Alice");
239
- expect(edges.length).toBe(1);
240
- expect(edges[0].mention_count).toBe(1);
241
- expect(JSON.parse(edges[0].source_memory_ids)).toEqual(["mem-002"]);
242
-
243
- await store.deleteBySource("mem-002");
244
- // Last evidence gone — row deleted
245
- expect(await store.countTriples()).toBe(0);
246
- });
247
- });
248
-
249
- describe("getTriplesBySourceMemories", () => {
250
- it("maps each memory id to the triples it contributed", async () => {
251
- const store = new KGStore({ dbPath: workDir });
252
- await store.createTriples([
253
- makeTriple({ source_memory_id: "mem-001", subject: "Alice", object: "Python" }),
254
- makeTriple({ source_memory_id: "mem-001", subject: "Alice", object: "Go" }),
255
- makeTriple({ source_memory_id: "mem-002", subject: "Bob", object: "Rust" }),
256
- ]);
257
- const map = await store.getTriplesBySourceMemories(["mem-001", "mem-002", "mem-404"]);
258
- expect(map.get("mem-001")?.length).toBe(2);
259
- expect(map.get("mem-002")?.length).toBe(1);
260
- expect(map.has("mem-404")).toBe(false);
261
- });
262
-
263
- it("a multi-source triple appears under every contributing memory", async () => {
264
- const store = new KGStore({ dbPath: workDir });
265
- await store.createTriple(makeTriple({ source_memory_id: "mem-001" }));
266
- await store.createTriple(makeTriple({ source_memory_id: "mem-002" }));
267
- const map = await store.getTriplesBySourceMemories(["mem-001", "mem-002"]);
268
- expect(map.get("mem-001")?.length).toBe(1);
269
- expect(map.get("mem-002")?.length).toBe(1);
270
- expect(map.get("mem-001")?.[0].id).toBe(map.get("mem-002")?.[0].id);
271
- expect(map.get("mem-001")?.[0].mention_count).toBe(2);
272
- });
273
-
274
- it("returns empty map for empty input", async () => {
275
- const store = new KGStore({ dbPath: workDir });
276
- expect((await store.getTriplesBySourceMemories([])).size).toBe(0);
277
- });
278
- });
279
-
280
- describe("entity queries", () => {
281
- it("getAllEntities returns unique entities", async () => {
282
- const store = new KGStore({ dbPath: workDir });
283
- await store.createTriples([
284
- makeTriple({ subject: "Alice", object: "Python" }),
285
- makeTriple({ subject: "Bob", object: "Python" }),
286
- ]);
287
- const entities = await store.getAllEntities();
288
- expect(entities).toContain("Alice");
289
- expect(entities).toContain("Bob");
290
- expect(entities).toContain("Python");
291
- });
292
-
293
- it("hasEntity checks existence", async () => {
294
- const store = new KGStore({ dbPath: workDir });
295
- await store.createTriple(makeTriple());
296
- expect(await store.hasEntity("Alice")).toBe(true);
297
- expect(await store.hasEntity("Charlie")).toBe(false);
298
- });
299
- });
300
- });