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,867 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it } from "bun:test";
2
- import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
3
- import { join } from "node:path";
4
- import { tmpdir } from "node:os";
5
- import { randomUUID } from "node:crypto";
6
-
7
- import { createAuditLogger } from "../audit-log.js";
8
- import type { AuditEntry } from "../audit-log.js";
9
- import {
10
- DEFAULT_RETENTION_POLICY,
11
- loadRetentionPolicy,
12
- saveRetentionPolicy,
13
- shouldArchiveByPolicy,
14
- } from "../retention-policy.js";
15
- import type { RetentionPolicy } from "../retention-policy.js";
16
- import { scanForPII } from "../pii-detector.js";
17
-
18
- // ---------------------------------------------------------------------------
19
- // Helpers
20
- // ---------------------------------------------------------------------------
21
-
22
- function makeTmpDir(): string {
23
- const dir = join(tmpdir(), `recallnest-test-${randomUUID()}`);
24
- mkdirSync(dir, { recursive: true });
25
- return dir;
26
- }
27
-
28
- // ---------------------------------------------------------------------------
29
- // F-1: Audit Log
30
- // ---------------------------------------------------------------------------
31
-
32
- describe("F-1: audit log", () => {
33
- let tmpDir: string;
34
-
35
- beforeEach(() => {
36
- tmpDir = makeTmpDir();
37
- });
38
-
39
- afterEach(() => {
40
- try {
41
- rmSync(tmpDir, { recursive: true, force: true });
42
- } catch {
43
- // cleanup best-effort
44
- }
45
- });
46
-
47
- it("log writes an entry and count returns 1", () => {
48
- const logPath = join(tmpDir, "audit.jsonl");
49
- const logger = createAuditLogger(logPath);
50
-
51
- logger.log({ operation: "store", actor: "manual", scope: "test" });
52
-
53
- expect(logger.count()).toBe(1);
54
- });
55
-
56
- it("log writes multiple entries", () => {
57
- const logPath = join(tmpDir, "audit.jsonl");
58
- const logger = createAuditLogger(logPath);
59
-
60
- logger.log({ operation: "store", actor: "manual" });
61
- logger.log({ operation: "retrieve", actor: "agent" });
62
- logger.log({ operation: "delete", actor: "system" });
63
-
64
- expect(logger.count()).toBe(3);
65
- });
66
-
67
- it("getRecent returns newest first", () => {
68
- const logPath = join(tmpDir, "audit.jsonl");
69
- const logger = createAuditLogger(logPath);
70
-
71
- logger.log({ operation: "store", actor: "manual", details: "first" });
72
- logger.log({ operation: "update", actor: "agent", details: "second" });
73
- logger.log({ operation: "delete", actor: "system", details: "third" });
74
-
75
- const recent = logger.getRecent(2);
76
- expect(recent).toHaveLength(2);
77
- expect(recent[0].details).toBe("third");
78
- expect(recent[1].details).toBe("second");
79
- });
80
-
81
- it("getRecent defaults to 20", () => {
82
- const logPath = join(tmpDir, "audit.jsonl");
83
- const logger = createAuditLogger(logPath);
84
-
85
- for (let i = 0; i < 25; i++) {
86
- logger.log({ operation: "store", actor: "manual", details: `entry-${i}` });
87
- }
88
-
89
- const recent = logger.getRecent();
90
- expect(recent).toHaveLength(20);
91
- expect(recent[0].details).toBe("entry-24");
92
- });
93
-
94
- it("exportAll returns all entries in order", () => {
95
- const logPath = join(tmpDir, "audit.jsonl");
96
- const logger = createAuditLogger(logPath);
97
-
98
- logger.log({ operation: "store", actor: "manual", details: "a" });
99
- logger.log({ operation: "update", actor: "agent", details: "b" });
100
-
101
- const all = logger.exportAll();
102
- expect(all).toHaveLength(2);
103
- expect(all[0].details).toBe("a");
104
- expect(all[1].details).toBe("b");
105
- });
106
-
107
- it("entries have ISO timestamps", () => {
108
- const logPath = join(tmpDir, "audit.jsonl");
109
- const logger = createAuditLogger(logPath);
110
-
111
- logger.log({ operation: "store", actor: "manual" });
112
-
113
- const entries = logger.exportAll();
114
- expect(entries[0].timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/);
115
- });
116
-
117
- it("entries preserve scope and memoryId", () => {
118
- const logPath = join(tmpDir, "audit.jsonl");
119
- const logger = createAuditLogger(logPath);
120
-
121
- logger.log({
122
- operation: "store",
123
- actor: "api",
124
- scope: "project:x",
125
- memoryId: "mem-123",
126
- });
127
-
128
- const entries = logger.exportAll();
129
- expect(entries[0].scope).toBe("project:x");
130
- expect(entries[0].memoryId).toBe("mem-123");
131
- });
132
-
133
- it("truncates details to 200 chars", () => {
134
- const logPath = join(tmpDir, "audit.jsonl");
135
- const logger = createAuditLogger(logPath);
136
-
137
- const longDetails = "x".repeat(300);
138
- logger.log({ operation: "store", actor: "manual", details: longDetails });
139
-
140
- const entries = logger.exportAll();
141
- expect(entries[0].details!.length).toBe(200);
142
- });
143
-
144
- it("returns empty arrays / 0 count when file does not exist", () => {
145
- const logPath = join(tmpDir, "nonexistent.jsonl");
146
- const logger = createAuditLogger(logPath);
147
-
148
- expect(logger.getRecent()).toEqual([]);
149
- expect(logger.exportAll()).toEqual([]);
150
- expect(logger.count()).toBe(0);
151
- });
152
-
153
- it("silently handles write to read-only path", () => {
154
- // Use a path that cannot be written to (deep nested under /dev/null)
155
- const logger = createAuditLogger("/dev/null/impossible/audit.jsonl");
156
-
157
- // Should not throw
158
- expect(() => {
159
- logger.log({ operation: "store", actor: "manual" });
160
- }).not.toThrow();
161
- });
162
-
163
- it("creates parent directory if missing", () => {
164
- const nestedPath = join(tmpDir, "sub", "deep", "audit.jsonl");
165
- const logger = createAuditLogger(nestedPath);
166
-
167
- logger.log({ operation: "store", actor: "manual" });
168
-
169
- expect(existsSync(nestedPath)).toBe(true);
170
- expect(logger.count()).toBe(1);
171
- });
172
-
173
- it("skips malformed lines gracefully", () => {
174
- const logPath = join(tmpDir, "audit.jsonl");
175
- // Write some valid and invalid lines
176
- writeFileSync(
177
- logPath,
178
- '{"timestamp":"2026-01-01T00:00:00Z","operation":"store","actor":"manual"}\nBAD LINE\n{"timestamp":"2026-01-02T00:00:00Z","operation":"update","actor":"agent"}\n',
179
- );
180
-
181
- const logger = createAuditLogger(logPath);
182
- expect(logger.count()).toBe(2);
183
- expect(logger.exportAll()).toHaveLength(2);
184
- });
185
- });
186
-
187
- // ---------------------------------------------------------------------------
188
- // F-2: Retention Policy
189
- // ---------------------------------------------------------------------------
190
-
191
- describe("F-2: retention policy", () => {
192
- let tmpDir: string;
193
-
194
- beforeEach(() => {
195
- tmpDir = makeTmpDir();
196
- });
197
-
198
- afterEach(() => {
199
- try {
200
- rmSync(tmpDir, { recursive: true, force: true });
201
- } catch {
202
- // cleanup best-effort
203
- }
204
- });
205
-
206
- it("DEFAULT_RETENTION_POLICY has safe defaults", () => {
207
- expect(DEFAULT_RETENTION_POLICY.autoArchiveAfterDays).toBe(0);
208
- expect(DEFAULT_RETENTION_POLICY.maxMemories).toBe(0);
209
- expect(DEFAULT_RETENTION_POLICY.allowHardDelete).toBe(false);
210
- });
211
-
212
- it("loadRetentionPolicy returns defaults for unconfigured scope", () => {
213
- const policy = loadRetentionPolicy("unknown-scope", tmpDir);
214
- expect(policy).toEqual(DEFAULT_RETENTION_POLICY);
215
- });
216
-
217
- it("save and load round-trips correctly", () => {
218
- const custom: Partial<RetentionPolicy> = {
219
- autoArchiveAfterDays: 30,
220
- maxMemories: 100,
221
- };
222
-
223
- saveRetentionPolicy("my-scope", custom, tmpDir);
224
- const loaded = loadRetentionPolicy("my-scope", tmpDir);
225
-
226
- expect(loaded.autoArchiveAfterDays).toBe(30);
227
- expect(loaded.maxMemories).toBe(100);
228
- expect(loaded.allowHardDelete).toBe(false); // default preserved
229
- });
230
-
231
- it("partial save merges with defaults", () => {
232
- saveRetentionPolicy("scope-a", { allowHardDelete: true }, tmpDir);
233
- const loaded = loadRetentionPolicy("scope-a", tmpDir);
234
-
235
- expect(loaded.allowHardDelete).toBe(true);
236
- expect(loaded.autoArchiveAfterDays).toBe(0);
237
- expect(loaded.maxMemories).toBe(0);
238
- });
239
-
240
- it("different scopes have independent policies", () => {
241
- saveRetentionPolicy("scope-x", { maxMemories: 50 }, tmpDir);
242
- saveRetentionPolicy("scope-y", { maxMemories: 200 }, tmpDir);
243
-
244
- expect(loadRetentionPolicy("scope-x", tmpDir).maxMemories).toBe(50);
245
- expect(loadRetentionPolicy("scope-y", tmpDir).maxMemories).toBe(200);
246
- });
247
-
248
- it("shouldArchiveByPolicy: default policy never archives", () => {
249
- const result = shouldArchiveByPolicy(DEFAULT_RETENTION_POLICY, 365, 9999);
250
- expect(result.archive).toBe(false);
251
- expect(result.reason).toBeUndefined();
252
- });
253
-
254
- it("shouldArchiveByPolicy: archives when age exceeds limit", () => {
255
- const policy: RetentionPolicy = {
256
- autoArchiveAfterDays: 30,
257
- maxMemories: 0,
258
- allowHardDelete: false,
259
- };
260
-
261
- const result = shouldArchiveByPolicy(policy, 31, 10);
262
- expect(result.archive).toBe(true);
263
- expect(result.reason).toContain("31d");
264
- expect(result.reason).toContain("30d");
265
- });
266
-
267
- it("shouldArchiveByPolicy: does not archive when age is within limit", () => {
268
- const policy: RetentionPolicy = {
269
- autoArchiveAfterDays: 30,
270
- maxMemories: 0,
271
- allowHardDelete: false,
272
- };
273
-
274
- expect(shouldArchiveByPolicy(policy, 29, 10).archive).toBe(false);
275
- expect(shouldArchiveByPolicy(policy, 30, 10).archive).toBe(false);
276
- });
277
-
278
- it("shouldArchiveByPolicy: archives when count exceeds limit", () => {
279
- const policy: RetentionPolicy = {
280
- autoArchiveAfterDays: 0,
281
- maxMemories: 100,
282
- allowHardDelete: false,
283
- };
284
-
285
- const result = shouldArchiveByPolicy(policy, 5, 101);
286
- expect(result.archive).toBe(true);
287
- expect(result.reason).toContain("101");
288
- expect(result.reason).toContain("100");
289
- });
290
-
291
- it("shouldArchiveByPolicy: does not archive when count is within limit", () => {
292
- const policy: RetentionPolicy = {
293
- autoArchiveAfterDays: 0,
294
- maxMemories: 100,
295
- allowHardDelete: false,
296
- };
297
-
298
- expect(shouldArchiveByPolicy(policy, 5, 99).archive).toBe(false);
299
- expect(shouldArchiveByPolicy(policy, 5, 100).archive).toBe(false);
300
- });
301
-
302
- it("allowHardDelete defaults to false", () => {
303
- saveRetentionPolicy("scope-del", {}, tmpDir);
304
- const loaded = loadRetentionPolicy("scope-del", tmpDir);
305
- expect(loaded.allowHardDelete).toBe(false);
306
- });
307
- });
308
-
309
- // ---------------------------------------------------------------------------
310
- // F-3: PII Detector
311
- // ---------------------------------------------------------------------------
312
-
313
- describe("F-3: PII detector", () => {
314
- it("returns clean result for text without PII", () => {
315
- const result = scanForPII("This is a normal sentence about programming.");
316
- expect(result.hasPII).toBe(false);
317
- expect(result.detections).toHaveLength(0);
318
- expect(result.summary).toBe("No PII detected");
319
- });
320
-
321
- it("returns clean result for pure Chinese text without PII", () => {
322
- const result = scanForPII("今天天气不错,我们去公园散步吧。");
323
- expect(result.hasPII).toBe(false);
324
- expect(result.detections).toHaveLength(0);
325
- });
326
-
327
- it("detects API keys (high severity)", () => {
328
- const result = scanForPII("My key is sk-1234567890abcdefghijklmnop");
329
- expect(result.hasPII).toBe(true);
330
- const apiKey = result.detections.find((d) => d.type === "api_key");
331
- expect(apiKey).toBeDefined();
332
- expect(apiKey!.severity).toBe("high");
333
- });
334
-
335
- it("detects token patterns (high severity)", () => {
336
- const result = scanForPII("token=abcdefghij1234567890abcdefghij");
337
- expect(result.hasPII).toBe(true);
338
- const detection = result.detections.find((d) => d.type === "api_key");
339
- expect(detection).toBeDefined();
340
- expect(detection!.severity).toBe("high");
341
- });
342
-
343
- it("detects passwords (high severity)", () => {
344
- const result = scanForPII('password="MyS3cretP@ss!"');
345
- expect(result.hasPII).toBe(true);
346
- const pwd = result.detections.find((d) => d.type === "password");
347
- expect(pwd).toBeDefined();
348
- expect(pwd!.severity).toBe("high");
349
- });
350
-
351
- it("detects Chinese ID numbers (high severity)", () => {
352
- const result = scanForPII("身份证号: 110101199003077891");
353
- expect(result.hasPII).toBe(true);
354
- const id = result.detections.find((d) => d.type === "id_number");
355
- expect(id).toBeDefined();
356
- expect(id!.severity).toBe("high");
357
- });
358
-
359
- it("detects email addresses (low severity)", () => {
360
- const result = scanForPII("Contact me at alice@example.com");
361
- expect(result.hasPII).toBe(true);
362
- const email = result.detections.find((d) => d.type === "email");
363
- expect(email).toBeDefined();
364
- expect(email!.severity).toBe("low");
365
- });
366
-
367
- it("detects phone numbers (medium severity)", () => {
368
- const result = scanForPII("我的手机号是 13812345678");
369
- expect(result.hasPII).toBe(true);
370
- const phone = result.detections.find((d) => d.type === "phone");
371
- expect(phone).toBeDefined();
372
- expect(phone!.severity).toBe("medium");
373
- });
374
-
375
- it("detects credit card numbers (high severity)", () => {
376
- const result = scanForPII("Card: 4111-1111-1111-1111");
377
- expect(result.hasPII).toBe(true);
378
- const cc = result.detections.find((d) => d.type === "credit_card");
379
- expect(cc).toBeDefined();
380
- expect(cc!.severity).toBe("high");
381
- });
382
-
383
- it("detects credit card without separators", () => {
384
- const result = scanForPII("Card: 4111111111111111");
385
- expect(result.hasPII).toBe(true);
386
- const cc = result.detections.find((d) => d.type === "credit_card");
387
- expect(cc).toBeDefined();
388
- });
389
-
390
- it("detects multiple PII types in mixed text", () => {
391
- const text =
392
- "User alice@test.com has password=SuperSecret123 and phone 13900001234. " +
393
- "API key: sk-abcdefghijklmnopqrstuvwxyz";
394
- const result = scanForPII(text);
395
-
396
- expect(result.hasPII).toBe(true);
397
- const types = new Set(result.detections.map((d) => d.type));
398
- expect(types.has("email")).toBe(true);
399
- expect(types.has("password")).toBe(true);
400
- expect(types.has("phone")).toBe(true);
401
- expect(types.has("api_key")).toBe(true);
402
- expect(result.detections.length).toBeGreaterThanOrEqual(4);
403
- });
404
-
405
- it("masks sensitive matches (preserves head/tail, masks middle)", () => {
406
- const result = scanForPII("token=abcdefghijklmnopqrstuvwxyz1234");
407
- expect(result.hasPII).toBe(true);
408
- const detection = result.detections[0];
409
- // Masked value should contain ***
410
- expect(detection.match).toContain("***");
411
- // Should not contain the full original value
412
- expect(detection.match.length).toBeLessThan(
413
- "token=abcdefghijklmnopqrstuvwxyz1234".length,
414
- );
415
- });
416
-
417
- it("mask preserves first 4 and last 4 chars for long values", () => {
418
- // Email is a good test case: alice@example.com (17 chars, > 8)
419
- const result = scanForPII("email: alice@example.com");
420
- const email = result.detections.find((d) => d.type === "email");
421
- expect(email).toBeDefined();
422
- // "alice@example.com" -> "alic***e.com"
423
- expect(email!.match.startsWith("alic")).toBe(true);
424
- expect(email!.match.endsWith(".com")).toBe(true);
425
- expect(email!.match).toContain("***");
426
- });
427
-
428
- it("provides accurate summary counts", () => {
429
- const text =
430
- "password=MySecret123 alice@test.com 13812345678";
431
- const result = scanForPII(text);
432
-
433
- // Should mention total count and severity breakdown
434
- expect(result.summary).toContain("Found");
435
- expect(result.summary).toMatch(/\d+ high/);
436
- expect(result.summary).toMatch(/\d+ medium/);
437
- expect(result.summary).toMatch(/\d+ low/);
438
- });
439
-
440
- it("records position (char offset) of matches", () => {
441
- const text = "prefix alice@example.com suffix";
442
- const result = scanForPII(text);
443
- const email = result.detections.find((d) => d.type === "email");
444
- expect(email).toBeDefined();
445
- expect(email!.position).toBe(text.indexOf("alice@example.com"));
446
- });
447
-
448
- it("handles empty string", () => {
449
- const result = scanForPII("");
450
- expect(result.hasPII).toBe(false);
451
- expect(result.detections).toHaveLength(0);
452
- });
453
-
454
- it("can be called multiple times (regex state reset)", () => {
455
- // Ensures global regex lastIndex is properly reset between calls
456
- const text = "sk-abcdefghijklmnopqrstuvwxyz";
457
- const r1 = scanForPII(text);
458
- const r2 = scanForPII(text);
459
- expect(r1.detections.length).toBe(r2.detections.length);
460
- expect(r1.hasPII).toBe(r2.hasPII);
461
- });
462
- });
463
-
464
- // ---------------------------------------------------------------------------
465
- // F-2 Integration: Retention Policy + Auto-GC
466
- // ---------------------------------------------------------------------------
467
-
468
- import {
469
- maybeRunGc,
470
- resetGcTimestamp,
471
- DEFAULT_AUTO_GC_CONFIG,
472
- } from "../auto-gc.js";
473
- import { isActiveMemory } from "../memory-evolution.js";
474
-
475
- describe("F-2 integration: retention policy + auto-gc", () => {
476
- let tmpDir: string;
477
-
478
- beforeEach(() => {
479
- tmpDir = makeTmpDir();
480
- resetGcTimestamp();
481
- });
482
-
483
- afterEach(() => {
484
- try {
485
- rmSync(tmpDir, { recursive: true, force: true });
486
- } catch {
487
- // cleanup best-effort
488
- }
489
- });
490
-
491
- /** Build a minimal mock store for auto-gc tests. */
492
- function makeGcStore(entries: Array<{
493
- id: string;
494
- importance: number;
495
- timestamp: number;
496
- metadata: string;
497
- scope: string;
498
- }>) {
499
- const data = new Map(entries.map(e => [e.id, {
500
- ...e,
501
- text: "test",
502
- vector: [1, 0, 0],
503
- category: "events" as const,
504
- }]));
505
- const updates: Array<{ id: string; metadata: string }> = [];
506
- return {
507
- store: {
508
- stats: async () => ({ totalCount: data.size, scopeCounts: {}, categoryCounts: {} }),
509
- repairSingletonVersionGroups: async () => 0,
510
- list: async () => Array.from(data.values()),
511
- listPage: async (opts: { limit?: number; offset?: number } = {}) => {
512
- const { limit = 1000, offset = 0 } = opts;
513
- return Array.from(data.values()).slice(offset, offset + limit);
514
- },
515
- update: async (id: string, patch: { metadata: string }) => {
516
- updates.push({ id, metadata: patch.metadata });
517
- const entry = data.get(id);
518
- if (!entry) return null;
519
- entry.metadata = patch.metadata;
520
- return entry;
521
- },
522
- // 2026-08-14 auto-gc 批量归档通道:以 data 里的最新行起底应用 patchFn(生产语义)
523
- patchMetadataBatch: async (
524
- patches: Array<{ id: string; patchFn: (meta: Record<string, unknown>, entry: unknown) => Record<string, unknown> }>,
525
- ) => {
526
- let written = 0;
527
- for (const { id, patchFn } of patches) {
528
- const entry = data.get(id);
529
- if (!entry) continue;
530
- let meta: Record<string, unknown>;
531
- try { meta = JSON.parse(entry.metadata || "{}") as Record<string, unknown>; } catch { meta = {}; }
532
- entry.metadata = JSON.stringify(patchFn(meta, entry));
533
- updates.push({ id, metadata: entry.metadata });
534
- written++;
535
- }
536
- return written;
537
- },
538
- },
539
- updates,
540
- };
541
- }
542
-
543
- it("archives memories exceeding autoArchiveAfterDays", async () => {
544
- const now = Date.now();
545
- const scope = "project:retention-age";
546
- // Save a retention policy: autoArchiveAfterDays = 30
547
- saveRetentionPolicy(scope, { autoArchiveAfterDays: 30 }, tmpDir);
548
-
549
- const activeMeta = JSON.stringify({
550
- evolution: { status: "active", version: 1 },
551
- });
552
-
553
- // One memory aged 45 days (above autoArchiveAfterDays=30)
554
- // Set importance high enough and decay score high enough that decay alone won't archive
555
- const { store, updates } = makeGcStore([
556
- {
557
- id: "aged-mem",
558
- importance: 0.5,
559
- timestamp: now - 45 * 86_400_000,
560
- metadata: activeMeta,
561
- scope,
562
- },
563
- ]);
564
-
565
- const result = await maybeRunGc(
566
- store as ReturnType<typeof makeGcStore>["store"] & import("../store.js").MemoryStore,
567
- {
568
- ...DEFAULT_AUTO_GC_CONFIG,
569
- minMemoryCount: 1,
570
- minHoursSinceLastGc: 0,
571
- // Set a very low decay threshold so decay alone wouldn't trigger
572
- decayScoreThreshold: 0.001,
573
- minAgeDays: 60, // Higher than 45 → decay check skipped
574
- },
575
- tmpDir,
576
- );
577
-
578
- expect(result.triggered).toBe(true);
579
- expect(result.archivedCount).toBe(1);
580
- expect(updates.length).toBe(1);
581
- // Verify the archived metadata marks it as archived
582
- expect(isActiveMemory(updates[0].metadata)).toBe(false);
583
- });
584
-
585
- it("archives memories when scope exceeds maxMemories", async () => {
586
- const now = Date.now();
587
- const scope = "project:retention-count";
588
- // maxMemories = 5 → with 6 active memories, oldest should be archived
589
- saveRetentionPolicy(scope, { maxMemories: 5 }, tmpDir);
590
-
591
- const activeMeta = JSON.stringify({
592
- evolution: { status: "active", version: 1 },
593
- });
594
-
595
- // Create 6 memories, all recent (within minAgeDays so decay won't fire)
596
- const entries = Array.from({ length: 6 }, (_, i) => ({
597
- id: `mem-${i}`,
598
- importance: 0.5,
599
- timestamp: now - (i + 1) * 86_400_000, // 1-6 days old
600
- metadata: activeMeta,
601
- scope,
602
- }));
603
-
604
- const { store, updates } = makeGcStore(entries);
605
-
606
- const result = await maybeRunGc(
607
- store as ReturnType<typeof makeGcStore>["store"] & import("../store.js").MemoryStore,
608
- {
609
- ...DEFAULT_AUTO_GC_CONFIG,
610
- minMemoryCount: 1,
611
- minHoursSinceLastGc: 0,
612
- decayScoreThreshold: 0.001, // Very low so decay alone won't trigger
613
- minAgeDays: 365, // Very high so decay check is skipped
614
- },
615
- tmpDir,
616
- );
617
-
618
- expect(result.triggered).toBe(true);
619
- // At least one should be archived since activeCount (6) > maxMemories (5)
620
- expect(result.archivedCount).toBeGreaterThanOrEqual(1);
621
- expect(updates.length).toBeGreaterThanOrEqual(1);
622
- });
623
-
624
- it("does not affect existing behavior when no retention config exists", async () => {
625
- const now = Date.now();
626
- const scope = "project:no-retention";
627
- // No saveRetentionPolicy call → default policy (0/0/false) → no policy archival
628
-
629
- const activeMeta = JSON.stringify({
630
- evolution: { status: "active", version: 1 },
631
- });
632
-
633
- // Memory aged 10 days, within default minAgeDays (30) → decay skip
634
- // No retention policy → policy skip
635
- const { store, updates } = makeGcStore([
636
- {
637
- id: "young-mem",
638
- importance: 0.5,
639
- timestamp: now - 10 * 86_400_000,
640
- metadata: activeMeta,
641
- scope,
642
- },
643
- ]);
644
-
645
- const result = await maybeRunGc(
646
- store as ReturnType<typeof makeGcStore>["store"] & import("../store.js").MemoryStore,
647
- {
648
- ...DEFAULT_AUTO_GC_CONFIG,
649
- minMemoryCount: 1,
650
- minHoursSinceLastGc: 0,
651
- minAgeDays: 30,
652
- },
653
- tmpDir, // Points to empty retention dir
654
- );
655
-
656
- expect(result.triggered).toBe(true);
657
- expect(result.archivedCount).toBe(0);
658
- expect(updates.length).toBe(0);
659
- });
660
- });
661
-
662
- // ---------------------------------------------------------------------------
663
- // F-3 Integration: PII Detection + persistMemory
664
- // ---------------------------------------------------------------------------
665
-
666
- import { persistMemory } from "../capture-engine.js";
667
-
668
- describe("F-3 integration: PII detection + persistMemory", () => {
669
- /** Build minimal deps for persistMemory. */
670
- function createPiiTestDeps() {
671
- const storedEntries: Array<Record<string, unknown>> = [];
672
- let seq = 1;
673
- return {
674
- storedEntries,
675
- deps: {
676
- embedder: {
677
- async embedPassage(_text: string) {
678
- return [1, 0, 0];
679
- },
680
- },
681
- store: {
682
- async store(entry: Record<string, unknown>) {
683
- const stored = {
684
- ...entry,
685
- id: `00000000-0000-0000-0000-${String(seq).padStart(12, "0")}`,
686
- timestamp: 1_700_000_000_000 + seq,
687
- };
688
- seq += 1;
689
- storedEntries.push(stored);
690
- return stored;
691
- },
692
- async list(
693
- _scopeFilter?: string[],
694
- _category?: string,
695
- limit = 20,
696
- offset = 0,
697
- ) {
698
- return storedEntries.slice(offset, offset + limit);
699
- },
700
- async update(id: string, updates: Record<string, unknown>) {
701
- const index = storedEntries.findIndex((e) => e.id === id);
702
- if (index < 0) return null;
703
- storedEntries[index] = { ...storedEntries[index], ...updates };
704
- return storedEntries[index];
705
- },
706
- async getById(id: string) {
707
- return storedEntries.find((e) => e.id === id) ?? null;
708
- },
709
- async get(id: string) {
710
- return storedEntries.find((e) => e.id === id) ?? null;
711
- },
712
- },
713
- },
714
- };
715
- }
716
-
717
- it("adds piiWarning to metadata when text contains API key", async () => {
718
- const { deps, storedEntries } = createPiiTestDeps();
719
- const result = await persistMemory(deps as Parameters<typeof persistMemory>[0], {
720
- text: "My API key is sk-abcdefghijklmnopqrstuvwxyz",
721
- category: "events",
722
- importance: 0.7,
723
- scope: "project:pii-test",
724
- source: "manual",
725
- tags: [],
726
- });
727
-
728
- expect(result.id).toBeDefined();
729
- // Check the stored entry's metadata contains piiWarning
730
- const stored = storedEntries[0];
731
- const meta = JSON.parse(stored.metadata as string);
732
- expect(meta.piiWarning).toBeDefined();
733
- expect(meta.piiWarning.severity).toBe("high");
734
- expect(meta.piiWarning.detections).toBeGreaterThanOrEqual(1);
735
- expect(meta.piiWarning.summary).toContain("Found");
736
- });
737
-
738
- it("does not add piiWarning to metadata for normal text", async () => {
739
- const { deps, storedEntries } = createPiiTestDeps();
740
- const result = await persistMemory(deps as Parameters<typeof persistMemory>[0], {
741
- text: "User prefers dark mode in all applications",
742
- category: "preferences",
743
- importance: 0.7,
744
- scope: "project:pii-test",
745
- source: "manual",
746
- tags: [],
747
- });
748
-
749
- expect(result.id).toBeDefined();
750
- const stored = storedEntries[0];
751
- const meta = JSON.parse(stored.metadata as string);
752
- expect(meta.piiWarning).toBeUndefined();
753
- });
754
-
755
- it("PII detection does not block write (text with PII is still stored)", async () => {
756
- const { deps, storedEntries } = createPiiTestDeps();
757
- const sensitiveText =
758
- "password=SuperSecret123 and token=abcdefghijklmnopqrstuvwxyz1234";
759
- const result = await persistMemory(deps as Parameters<typeof persistMemory>[0], {
760
- text: sensitiveText,
761
- category: "events",
762
- importance: 0.7,
763
- scope: "project:pii-test",
764
- source: "manual",
765
- tags: [],
766
- });
767
-
768
- // Memory was stored despite PII
769
- expect(result.id).toBeDefined();
770
- expect(result.disposition).toBe("stored");
771
- expect(storedEntries.length).toBe(1);
772
- // F-3b (2026-07-10): stored text is scrubbed — secrets must not reach
773
- // storage/embedding/logs. Key names survive for readability.
774
- expect(storedEntries[0].text).toContain("password=[REDACTED:password]");
775
- expect(storedEntries[0].text).not.toContain("SuperSecret123");
776
- });
777
- });
778
-
779
- // ---------------------------------------------------------------------------
780
- // F-1 integration: audit logger in persistMemory & maybeRunGc
781
- // ---------------------------------------------------------------------------
782
-
783
- describe("F-1 integration: audit logger", () => {
784
- let tmpDir: string;
785
- beforeEach(() => { tmpDir = join(tmpdir(), `f1-audit-${randomUUID()}`); mkdirSync(tmpDir, { recursive: true }); });
786
- afterEach(() => { try { rmSync(tmpDir, { recursive: true, force: true }); } catch {} });
787
-
788
- it("persistMemory records store audit entry", async () => {
789
- const { persistMemory } = await import("../capture-engine.js");
790
- const { createAuditLogger } = await import("../audit-log.js");
791
- const logPath = join(tmpDir, "audit.jsonl");
792
- const logger = createAuditLogger(logPath);
793
-
794
- const deps = {
795
- embedder: { async embedPassage() { return [1, 0, 0]; } },
796
- store: {
797
- async store(entry: Record<string, unknown>) { return { ...entry, id: entry.id ?? "test-1", timestamp: Date.now() }; },
798
- async list() { return []; },
799
- async update() { return null; },
800
- async getById() { return null; },
801
- },
802
- auditLogger: logger,
803
- };
804
-
805
- await persistMemory(deps as Parameters<typeof persistMemory>[0], {
806
- text: "User prefers dark mode",
807
- category: "preferences",
808
- scope: "project:test",
809
- source: "manual",
810
- importance: 0.7,
811
- tags: [],
812
- });
813
-
814
- const entries = logger.exportAll();
815
- const storeEntry = entries.find(e => e.operation === "store");
816
- expect(storeEntry).toBeDefined();
817
- expect(storeEntry!.actor).toBe("manual");
818
- expect(storeEntry!.scope).toBe("project:test");
819
- });
820
-
821
- it("maybeRunGc records archive audit entry", async () => {
822
- const { maybeRunGc, resetGcTimestamp } = await import("../auto-gc.js");
823
- const { createAuditLogger } = await import("../audit-log.js");
824
- resetGcTimestamp();
825
-
826
- const logPath = join(tmpDir, "audit-gc.jsonl");
827
- const logger = createAuditLogger(logPath);
828
- const now = Date.now();
829
- const oldTs = now - 120 * 86_400_000;
830
- const activeMeta = JSON.stringify({ evolution: { status: "active", version: 1, accessCount: 0, lastAccessedAt: null, validFrom: oldTs, validUntil: null } });
831
-
832
- const rows = [{ id: "gc-1", text: "old", importance: 0.1, timestamp: oldTs, metadata: activeMeta, category: "events", scope: "project:test" }];
833
- const store = {
834
- async stats() { return { totalCount: 100, scopeCounts: {}, categoryCounts: {} }; },
835
- async repairSingletonVersionGroups() { return 0; },
836
- async list() { return rows; },
837
- async listPage(opts: { limit?: number; offset?: number } = {}) {
838
- const { limit = 1000, offset = 0 } = opts;
839
- return rows.slice(offset, offset + limit);
840
- },
841
- async update(id: string, upd: Record<string, unknown>) { return { id, ...upd }; },
842
- async patchMetadataBatch(
843
- patches: Array<{ id: string; patchFn: (meta: Record<string, unknown>, entry: unknown) => Record<string, unknown> }>,
844
- ) {
845
- for (const { id, patchFn } of patches) {
846
- const row = rows.find(r => r.id === id);
847
- if (!row) continue;
848
- row.metadata = JSON.stringify(patchFn(JSON.parse(row.metadata || "{}") as Record<string, unknown>, row));
849
- }
850
- return patches.length;
851
- },
852
- };
853
-
854
- const result = await maybeRunGc(
855
- store as Parameters<typeof maybeRunGc>[0],
856
- { minMemoryCount: 1, minHoursSinceLastGc: 0, decayScoreThreshold: 0.99, maxArchivePerRun: 10, minAgeDays: 30 },
857
- undefined, // retentionConfigDir
858
- logger, // auditLogger (4th param)
859
- );
860
-
861
- expect(result.archivedCount).toBe(1);
862
- const archiveEntry = logger.exportAll().find(e => e.operation === "archive");
863
- expect(archiveEntry).toBeDefined();
864
- expect(archiveEntry!.memoryId).toBe("gc-1");
865
- expect(archiveEntry!.actor).toBe("system");
866
- });
867
- });