recallnest 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/CHANGELOG.md +304 -0
  2. package/README.md +251 -258
  3. package/README_CN.md +226 -247
  4. package/assets/ui/app.js +6 -1
  5. package/bin/recallnest.mjs +20 -37
  6. package/docs/api-reference.md +8 -0
  7. package/docs/architecture.md +18 -13
  8. package/eval/cases-canary.json +107 -9
  9. package/integrations/examples/claude-agent-sdk/memory-agent.ts +2 -0
  10. package/integrations/examples/langchain/memory-chain.py +4 -0
  11. package/integrations/examples/openai-agents-sdk/memory-agent.py +4 -0
  12. package/package.json +12 -3
  13. package/src/activity-counter.ts +1 -0
  14. package/src/adaptive-retrieval.ts +23 -0
  15. package/src/api-server.ts +13 -11
  16. package/src/asset-sync.ts +26 -2
  17. package/src/audit-log.ts +2 -0
  18. package/src/auto-gc.ts +4 -3
  19. package/src/belief-history.ts +137 -31
  20. package/src/capture-engine.ts +459 -71
  21. package/src/cascade-forget.ts +7 -3
  22. package/src/checkpoint-mirror.ts +417 -0
  23. package/src/cli.ts +574 -54
  24. package/src/conflict-engine.ts +43 -15
  25. package/src/consolidation-engine.ts +24 -3
  26. package/src/context-collapse-renderer.ts +8 -1
  27. package/src/context-composer-stable.ts +5 -1
  28. package/src/context-composer.ts +11 -2
  29. package/src/distill-lock.ts +246 -74
  30. package/src/dream-pipeline.ts +3 -1
  31. package/src/embedder.ts +24 -0
  32. package/src/env-config.ts +136 -0
  33. package/src/flock.ts +97 -0
  34. package/src/forget-engine.ts +342 -58
  35. package/src/gateway-server.ts +4 -0
  36. package/src/ingest-plan.ts +56 -0
  37. package/src/ingest.ts +294 -121
  38. package/src/kg-contradiction.ts +753 -0
  39. package/src/kg-extractor.ts +17 -0
  40. package/src/kg-store.ts +1 -1
  41. package/src/known-item-eval.ts +455 -0
  42. package/src/llm-client.ts +13 -26
  43. package/src/llm-consolidation.ts +2 -2
  44. package/src/mcp-tool-deps.ts +6 -0
  45. package/src/mcp-tools-advanced.ts +80 -45
  46. package/src/mcp-tools-core.ts +23 -17
  47. package/src/mcp-tools-governance.ts +13 -7
  48. package/src/memory-assets.ts +144 -8
  49. package/src/memory-boundaries.ts +19 -0
  50. package/src/memory-evolution.ts +9 -0
  51. package/src/memory-lint.ts +1 -1
  52. package/src/memory-output.ts +311 -14
  53. package/src/memory-reconcile.ts +1437 -0
  54. package/src/memory-schema.ts +11 -0
  55. package/src/memory-store-port.ts +1 -0
  56. package/src/minis-archive.ts +151 -0
  57. package/src/noise-filter.ts +15 -2
  58. package/src/pii-detector.ts +31 -1
  59. package/src/pivot-apply.ts +55 -5
  60. package/src/pivot-evidence.ts +128 -0
  61. package/src/ppr-traversal.ts +25 -1
  62. package/src/preference-matcher.ts +40 -28
  63. package/src/prospective-memory.ts +7 -4
  64. package/src/retriever.ts +297 -57
  65. package/src/retroactive-boost.ts +3 -2
  66. package/src/runtime-config.ts +15 -1
  67. package/src/scope-policy.ts +1 -0
  68. package/src/session-distiller.ts +6 -2
  69. package/src/session-engine.ts +11 -4
  70. package/src/session-output.ts +20 -1
  71. package/src/session-schema.ts +5 -2
  72. package/src/session-store.ts +59 -12
  73. package/src/skill-promotion.ts +67 -1
  74. package/src/store-receipts.ts +109 -0
  75. package/src/store-write-budget.ts +37 -0
  76. package/src/store.ts +298 -57
  77. package/src/text-fingerprint.ts +25 -0
  78. package/src/trigger-store.ts +443 -0
  79. package/src/ui-server.ts +22 -21
  80. package/src/version-manager.ts +19 -5
  81. package/src/write-verifier.ts +16 -11
  82. package/src/__tests__/access-tracker-novelty.test.ts +0 -324
  83. package/src/__tests__/activity-counter.test.ts +0 -125
  84. package/src/__tests__/adaptive-fidelity.test.ts +0 -102
  85. package/src/__tests__/admission-control.test.ts +0 -206
  86. package/src/__tests__/age-label.test.ts +0 -33
  87. package/src/__tests__/anchor-generator.test.ts +0 -67
  88. package/src/__tests__/api-server-security.test.ts +0 -102
  89. package/src/__tests__/auto-consolidation.test.ts +0 -168
  90. package/src/__tests__/auto-recall.test.ts +0 -131
  91. package/src/__tests__/babel-integration.test.ts +0 -144
  92. package/src/__tests__/batch-dedup.test.ts +0 -65
  93. package/src/__tests__/belief-history.test.ts +0 -220
  94. package/src/__tests__/canary-eval.test.ts +0 -114
  95. package/src/__tests__/capture-engine.test.ts +0 -1328
  96. package/src/__tests__/capture-heuristic.test.ts +0 -171
  97. package/src/__tests__/cascade-forget.test.ts +0 -210
  98. package/src/__tests__/category-min-scores.test.ts +0 -116
  99. package/src/__tests__/cc8-cc1-essential-context.test.ts +0 -298
  100. package/src/__tests__/circuit-breaker.test.ts +0 -99
  101. package/src/__tests__/cluster-consolidation.test.ts +0 -1236
  102. package/src/__tests__/confidence-metadata.test.ts +0 -125
  103. package/src/__tests__/confidence-tracker.test.ts +0 -261
  104. package/src/__tests__/conflict-advisor.test.ts +0 -206
  105. package/src/__tests__/conflict-escalation.test.ts +0 -149
  106. package/src/__tests__/conflict-lifecycle.test.ts +0 -83
  107. package/src/__tests__/conflict-output.test.ts +0 -85
  108. package/src/__tests__/conflict-store.test.ts +0 -418
  109. package/src/__tests__/connector-v1.test.ts +0 -181
  110. package/src/__tests__/consolidation-engine.test.ts +0 -520
  111. package/src/__tests__/constructive-retrieval.test.ts +0 -509
  112. package/src/__tests__/context-collapse-renderer.test.ts +0 -214
  113. package/src/__tests__/context-composer-pins.test.ts +0 -64
  114. package/src/__tests__/context-composer-task-selection.test.ts +0 -24
  115. package/src/__tests__/context-reconstructor.test.ts +0 -65
  116. package/src/__tests__/context-renderer.test.ts +0 -91
  117. package/src/__tests__/continuity-eval.test.ts +0 -275
  118. package/src/__tests__/continuity-schema.test.ts +0 -97
  119. package/src/__tests__/contracts.test.ts +0 -262
  120. package/src/__tests__/conversation-importer.test.ts +0 -692
  121. package/src/__tests__/core-recall-eval.test.ts +0 -235
  122. package/src/__tests__/core-summary.test.ts +0 -109
  123. package/src/__tests__/data-checkup.test.ts +0 -304
  124. package/src/__tests__/decay-exemption.test.ts +0 -129
  125. package/src/__tests__/dedup-actions.test.ts +0 -58
  126. package/src/__tests__/dedup-guard.test.ts +0 -39
  127. package/src/__tests__/deterministic-id.test.ts +0 -39
  128. package/src/__tests__/distill-lock.test.ts +0 -305
  129. package/src/__tests__/distill-session-argshape.test.ts +0 -79
  130. package/src/__tests__/doctor.test.ts +0 -319
  131. package/src/__tests__/dream-pipeline.test.ts +0 -820
  132. package/src/__tests__/dream-scope-isolation.test.ts +0 -386
  133. package/src/__tests__/dream-write-batching.test.ts +0 -211
  134. package/src/__tests__/drill-down.test.ts +0 -108
  135. package/src/__tests__/embedder.test.ts +0 -122
  136. package/src/__tests__/emotion-decay.test.ts +0 -46
  137. package/src/__tests__/emotion-detector.test.ts +0 -56
  138. package/src/__tests__/emotion-phase1-integration.test.ts +0 -153
  139. package/src/__tests__/emotion-retrieval.test.ts +0 -56
  140. package/src/__tests__/entity-resolver.test.ts +0 -58
  141. package/src/__tests__/env-config-invariant.test.ts +0 -80
  142. package/src/__tests__/env-config.test.ts +0 -211
  143. package/src/__tests__/error-signature-boost.test.ts +0 -87
  144. package/src/__tests__/error-signature.test.ts +0 -95
  145. package/src/__tests__/error-taxonomy.test.ts +0 -129
  146. package/src/__tests__/ethics-layer.test.ts +0 -476
  147. package/src/__tests__/event-segmenter.test.ts +0 -153
  148. package/src/__tests__/feature-flags-integration.test.ts +0 -54
  149. package/src/__tests__/frequency-tracker.test.ts +0 -113
  150. package/src/__tests__/freshness.test.ts +0 -186
  151. package/src/__tests__/graph-export.test.ts +0 -489
  152. package/src/__tests__/hotness-blend.test.ts +0 -283
  153. package/src/__tests__/ingest-chunking.test.ts +0 -44
  154. package/src/__tests__/ingest-scope-prefix.test.ts +0 -69
  155. package/src/__tests__/ingest.test.ts +0 -360
  156. package/src/__tests__/interference-detector.test.ts +0 -203
  157. package/src/__tests__/kg-extractor.test.ts +0 -192
  158. package/src/__tests__/kg-store.test.ts +0 -300
  159. package/src/__tests__/llm-client-pivot.test.ts +0 -628
  160. package/src/__tests__/llm-consolidation.test.ts +0 -226
  161. package/src/__tests__/maintenance-locks.test.ts +0 -197
  162. package/src/__tests__/mcp-efficiency.test.ts +0 -288
  163. package/src/__tests__/mcp-related-scopes.test.ts +0 -151
  164. package/src/__tests__/memory-boundaries.test.ts +0 -282
  165. package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +0 -362
  166. package/src/__tests__/memory-evolution.test.ts +0 -271
  167. package/src/__tests__/memory-health-rebalance.test.ts +0 -144
  168. package/src/__tests__/memory-lint.test.ts +0 -421
  169. package/src/__tests__/memory-output.test.ts +0 -224
  170. package/src/__tests__/memory-promotion.test.ts +0 -315
  171. package/src/__tests__/memory-utility.test.ts +0 -256
  172. package/src/__tests__/migrate-dedup-ids.test.ts +0 -65
  173. package/src/__tests__/multi-vector.test.ts +0 -242
  174. package/src/__tests__/narrative-architecture.test.ts +0 -456
  175. package/src/__tests__/noise-filter.test.ts +0 -205
  176. package/src/__tests__/noise-prototype-bank.test.ts +0 -280
  177. package/src/__tests__/obsidian-connector.test.ts +0 -363
  178. package/src/__tests__/omitted-reporting.test.ts +0 -98
  179. package/src/__tests__/openai-contract.test.ts +0 -513
  180. package/src/__tests__/pending-review.test.ts +0 -77
  181. package/src/__tests__/pii-redact.test.ts +0 -333
  182. package/src/__tests__/pivot-apply.test.ts +0 -373
  183. package/src/__tests__/pivot-distill-supervisor.test.ts +0 -457
  184. package/src/__tests__/pivot-distill.test.ts +0 -1178
  185. package/src/__tests__/post-retrieval-filter.test.ts +0 -178
  186. package/src/__tests__/ppr-traversal.test.ts +0 -256
  187. package/src/__tests__/prediction-engine.test.ts +0 -349
  188. package/src/__tests__/predictive-prospective-memory.test.ts +0 -416
  189. package/src/__tests__/preference-guard-smoke.test.ts +0 -211
  190. package/src/__tests__/preference-matcher.test.ts +0 -282
  191. package/src/__tests__/preference-slots.test.ts +0 -205
  192. package/src/__tests__/promotion-distinct-sources.test.ts +0 -162
  193. package/src/__tests__/prospective-memory.test.ts +0 -283
  194. package/src/__tests__/pull-from-macbook-sync-only.test.ts +0 -679
  195. package/src/__tests__/query-entity-detector.test.ts +0 -171
  196. package/src/__tests__/query-expander-alias.test.ts +0 -146
  197. package/src/__tests__/query-expander.test.ts +0 -34
  198. package/src/__tests__/recall-gate.test.ts +0 -266
  199. package/src/__tests__/recall-governor.test.ts +0 -227
  200. package/src/__tests__/recall-mode.test.ts +0 -64
  201. package/src/__tests__/resolve-db-path.test.ts +0 -130
  202. package/src/__tests__/resolve-tier.test.ts +0 -64
  203. package/src/__tests__/result-synthesizer.test.ts +0 -198
  204. package/src/__tests__/resume-context.test.ts +0 -4898
  205. package/src/__tests__/retrieval-eval.test.ts +0 -163
  206. package/src/__tests__/retrieval-trace.test.ts +0 -213
  207. package/src/__tests__/retrieve-audit-revision.test.ts +0 -255
  208. package/src/__tests__/retriever-boundary-weight.test.ts +0 -136
  209. package/src/__tests__/retriever-lazy-init-dispatch.test.ts +0 -123
  210. package/src/__tests__/retroactive-boost.test.ts +0 -223
  211. package/src/__tests__/salience-scoring.test.ts +0 -106
  212. package/src/__tests__/scope-inventory.test.ts +0 -200
  213. package/src/__tests__/scope-policy.test.ts +0 -69
  214. package/src/__tests__/scope-suggester.test.ts +0 -80
  215. package/src/__tests__/session-checkpoint.test.ts +0 -291
  216. package/src/__tests__/session-distiller.test.ts +0 -640
  217. package/src/__tests__/session-gc.test.ts +0 -215
  218. package/src/__tests__/skill-engine.test.ts +0 -552
  219. package/src/__tests__/skill-promotion.test.ts +0 -654
  220. package/src/__tests__/skill-verifier.test.ts +0 -122
  221. package/src/__tests__/source-heartbeat.test.ts +0 -254
  222. package/src/__tests__/stable-context-length-contract.test.ts +0 -62
  223. package/src/__tests__/store-patch-metadata.test.ts +0 -235
  224. package/src/__tests__/store-read-consistency.test.ts +0 -122
  225. package/src/__tests__/store-schema-migration.test.ts +0 -87
  226. package/src/__tests__/store-scope-audit.test.ts +0 -81
  227. package/src/__tests__/store-upsert.test.ts +0 -201
  228. package/src/__tests__/supersede-chain.test.ts +0 -154
  229. package/src/__tests__/synthesis-contract.test.ts +0 -252
  230. package/src/__tests__/synthesis-promotion.test.ts +0 -369
  231. package/src/__tests__/temporal-parser.test.ts +0 -241
  232. package/src/__tests__/temporal-validity.test.ts +0 -160
  233. package/src/__tests__/term-registry-terminal-roster.test.ts +0 -127
  234. package/src/__tests__/term-registry.test.ts +0 -121
  235. package/src/__tests__/tool-error-surfacing.test.ts +0 -98
  236. package/src/__tests__/tool-output-compressor.test.ts +0 -160
  237. package/src/__tests__/topic-tag.test.ts +0 -113
  238. package/src/__tests__/trusted-memory.test.ts +0 -867
  239. package/src/__tests__/ui-server-security.test.ts +0 -93
  240. package/src/__tests__/usage-tracker.test.ts +0 -287
  241. package/src/__tests__/utility-weight.test.ts +0 -57
  242. package/src/__tests__/verbatim-self-recall.test.ts +0 -279
  243. package/src/__tests__/version-manager.test.ts +0 -248
  244. package/src/__tests__/workflow-observation-skill-outcome.test.ts +0 -352
  245. package/src/__tests__/workflow-observation.test.ts +0 -433
  246. 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
- });