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,324 +0,0 @@
1
- /**
2
- * Tests for Tier 3.2: Conditional access reinforcement (prediction error gating).
3
- *
4
- * Validates that:
5
- * 1. High-similarity (low-novelty) results are NOT reinforced
6
- * 2. Low-similarity (high-novelty) results ARE reinforced
7
- * 3. Cooldown prevents duplicate reinforcement within the window
8
- * 4. When no scores provided, all entries are reinforced (backward compat)
9
- * 5. shouldReinforce() logic is correct in isolation
10
- */
11
- import { describe, expect, it, beforeEach } from "bun:test";
12
- import { AccessTracker, DEFAULT_ACCESS_TRACKER_CONFIG } from "../access-tracker.js";
13
- import type { MemoryEntry } from "../store.js";
14
-
15
- // ---------------------------------------------------------------------------
16
- // Helpers
17
- // ---------------------------------------------------------------------------
18
-
19
- function makeEntry(id: string, overrides: Partial<MemoryEntry> = {}): MemoryEntry {
20
- return {
21
- id,
22
- text: `memory-${id}`,
23
- vector: [1, 0, 0],
24
- category: "events",
25
- scope: "project:test",
26
- importance: 0.7,
27
- timestamp: Date.now(),
28
- metadata: JSON.stringify({ accessCount: 0, lastAccessedAt: 0 }),
29
- ...overrides,
30
- };
31
- }
32
-
33
- function createMockStore(entries: MemoryEntry[]) {
34
- const data = new Map(entries.map(e => [e.id, { ...e }]));
35
- const updates: Array<{ id: string; metadata?: string }> = [];
36
-
37
- // 模拟 store.patchMetadata 单写通道:读最新 metadata → patchFn → 写回。
38
- return {
39
- updates,
40
- data,
41
- async patchMetadata(
42
- id: string,
43
- patchFn: (meta: Record<string, unknown>, entry: MemoryEntry) => Record<string, unknown>,
44
- _scopeFilter?: string[],
45
- ) {
46
- const entry = data.get(id);
47
- if (!entry) return null;
48
- let meta: Record<string, unknown>;
49
- try {
50
- const parsed: unknown = JSON.parse(entry.metadata || "{}");
51
- meta = parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
52
- ? (parsed as Record<string, unknown>)
53
- : {};
54
- } catch {
55
- meta = {};
56
- }
57
- const patched = patchFn(meta, entry);
58
- entry.metadata = JSON.stringify(patched);
59
- updates.push({ id, metadata: entry.metadata });
60
- return entry;
61
- },
62
- };
63
- }
64
-
65
- // ---------------------------------------------------------------------------
66
- // shouldReinforce() unit tests
67
- // ---------------------------------------------------------------------------
68
-
69
- describe("shouldReinforce", () => {
70
- let tracker: AccessTracker;
71
-
72
- beforeEach(() => {
73
- tracker = new AccessTracker(
74
- createMockStore([]) as any,
75
- { ...DEFAULT_ACCESS_TRACKER_CONFIG, noveltyThreshold: 0.35, cooldownMs: 300_000 },
76
- );
77
- });
78
-
79
- it("rejects high-similarity (low-novelty) results", () => {
80
- // similarity 0.90 → novelty 0.10 < threshold 0.35 → reject
81
- expect(tracker.shouldReinforce("a", 0.90)).toBe(false);
82
- });
83
-
84
- it("rejects exact match (similarity 1.0)", () => {
85
- expect(tracker.shouldReinforce("a", 1.0)).toBe(false);
86
- });
87
-
88
- it("accepts low-similarity (high-novelty) results", () => {
89
- // similarity 0.50 → novelty 0.50 > threshold 0.35 → accept
90
- expect(tracker.shouldReinforce("a", 0.50)).toBe(true);
91
- });
92
-
93
- it("accepts borderline novelty (exactly at threshold)", () => {
94
- // similarity 0.65 → novelty 0.35 = threshold 0.35 → accept (not strictly less)
95
- expect(tracker.shouldReinforce("a", 0.65)).toBe(true);
96
- });
97
-
98
- it("rejects same entry within cooldown window", () => {
99
- // First call: accepted
100
- expect(tracker.shouldReinforce("a", 0.50)).toBe(true);
101
- // Simulate recording to set cooldown
102
- tracker.recordAccess(["a"], [0.50]);
103
- // Second call within cooldown: rejected
104
- expect(tracker.shouldReinforce("a", 0.50)).toBe(false);
105
- });
106
-
107
- it("accepts different entry even if another is on cooldown", () => {
108
- tracker.recordAccess(["a"], [0.50]);
109
- expect(tracker.shouldReinforce("b", 0.50)).toBe(true);
110
- });
111
-
112
- it("bypasses novelty gate when threshold is 0", () => {
113
- const noGateTracker = new AccessTracker(
114
- createMockStore([]) as any,
115
- { ...DEFAULT_ACCESS_TRACKER_CONFIG, noveltyThreshold: 0, cooldownMs: 0 },
116
- );
117
- // Even high similarity passes
118
- expect(noGateTracker.shouldReinforce("a", 0.99)).toBe(true);
119
- });
120
- });
121
-
122
- // ---------------------------------------------------------------------------
123
- // recordAccess() with scores
124
- // ---------------------------------------------------------------------------
125
-
126
- describe("recordAccess with novelty gating", () => {
127
- it("only queues novel entries for flush", async () => {
128
- const entries = [makeEntry("a"), makeEntry("b"), makeEntry("c")];
129
- const store = createMockStore(entries);
130
- const tracker = new AccessTracker(store as any, {
131
- ...DEFAULT_ACCESS_TRACKER_CONFIG,
132
- flushIntervalMs: 0, // immediate flush for testing
133
- noveltyThreshold: 0.35,
134
- cooldownMs: 0,
135
- });
136
-
137
- // scores: a=0.90 (low novelty, reject), b=0.50 (novel, accept), c=0.40 (novel, accept)
138
- tracker.recordAccess(["a", "b", "c"], [0.90, 0.50, 0.40]);
139
-
140
- await tracker.flush();
141
-
142
- // Only b and c should have been updated
143
- const updatedIds = store.updates.map(u => u.id);
144
- expect(updatedIds).not.toContain("a");
145
- expect(updatedIds).toContain("b");
146
- expect(updatedIds).toContain("c");
147
- });
148
-
149
- it("reinforces all entries when no scores provided (backward compat)", async () => {
150
- const entries = [makeEntry("a"), makeEntry("b")];
151
- const store = createMockStore(entries);
152
- const tracker = new AccessTracker(store as any, {
153
- ...DEFAULT_ACCESS_TRACKER_CONFIG,
154
- flushIntervalMs: 0,
155
- noveltyThreshold: 0.35,
156
- cooldownMs: 0,
157
- });
158
-
159
- // No scores → all reinforced
160
- tracker.recordAccess(["a", "b"]);
161
- await tracker.flush();
162
-
163
- const updatedIds = store.updates.map(u => u.id);
164
- expect(updatedIds).toContain("a");
165
- expect(updatedIds).toContain("b");
166
- });
167
-
168
- it("respects cooldown across multiple recordAccess calls", async () => {
169
- const entries = [makeEntry("x")];
170
- const store = createMockStore(entries);
171
- const tracker = new AccessTracker(store as any, {
172
- ...DEFAULT_ACCESS_TRACKER_CONFIG,
173
- flushIntervalMs: 0,
174
- noveltyThreshold: 0.35,
175
- cooldownMs: 600_000, // 10 min cooldown
176
- });
177
-
178
- // First call: novel enough → queued
179
- tracker.recordAccess(["x"], [0.50]);
180
- await tracker.flush();
181
- expect(store.updates.length).toBe(1);
182
-
183
- // Second call: same entry within cooldown → not queued
184
- tracker.recordAccess(["x"], [0.50]);
185
- await tracker.flush();
186
- // Still only 1 update (no new flush)
187
- expect(store.updates.length).toBe(1);
188
- });
189
-
190
- it("skips flush scheduling when all entries are gated out", () => {
191
- const store = createMockStore([]);
192
- const tracker = new AccessTracker(store as any, {
193
- ...DEFAULT_ACCESS_TRACKER_CONFIG,
194
- flushIntervalMs: 5000,
195
- noveltyThreshold: 0.35,
196
- cooldownMs: 0,
197
- });
198
-
199
- // All scores too high (low novelty) → nothing queued
200
- tracker.recordAccess(["a", "b"], [0.95, 0.90]);
201
- expect(tracker.pendingCount).toBe(0);
202
- });
203
-
204
- it("accessCount increments correctly after novelty-gated flush", async () => {
205
- const entries = [makeEntry("m1")];
206
- const store = createMockStore(entries);
207
- const tracker = new AccessTracker(store as any, {
208
- ...DEFAULT_ACCESS_TRACKER_CONFIG,
209
- flushIntervalMs: 0,
210
- noveltyThreshold: 0.35,
211
- cooldownMs: 0,
212
- });
213
-
214
- tracker.recordAccess(["m1"], [0.40]); // novel → accepted
215
- await tracker.flush();
216
-
217
- const updated = store.data.get("m1")!;
218
- const meta = JSON.parse(updated.metadata);
219
- expect(meta.accessCount).toBe(1);
220
- });
221
- });
222
-
223
- // ---------------------------------------------------------------------------
224
- // destroy() cleans cooldown
225
- // ---------------------------------------------------------------------------
226
-
227
- describe("destroy cleans cooldown", () => {
228
- it("clears cooldown map on destroy", () => {
229
- const tracker = new AccessTracker(
230
- createMockStore([]) as any,
231
- { ...DEFAULT_ACCESS_TRACKER_CONFIG, cooldownMs: 600_000 },
232
- );
233
- tracker.recordAccess(["a"], [0.40]);
234
- tracker.destroy();
235
- // After destroy, same entry should be reinforceable again
236
- expect(tracker.shouldReinforce("a", 0.40)).toBe(true);
237
- });
238
- });
239
-
240
- // ---------------------------------------------------------------------------
241
- // Distinct-reader bookkeeping (Artel read fan-out) + default-config semantics
242
- // ---------------------------------------------------------------------------
243
-
244
- describe("default config after 2026-07-23 production fix", () => {
245
- it("novelty gate is OFF by default (high-similarity hits are reinforced)", () => {
246
- const tracker = new AccessTracker(createMockStore([]) as any);
247
- // 生产实测修复:真实相关命中 cosine >0.65 也必须打点
248
- expect(tracker.shouldReinforce("a", 0.95)).toBe(true);
249
- });
250
- });
251
-
252
- describe("distinct-reader bookkeeping", () => {
253
- it("writes readerIds and distinctReaderCount on flush", async () => {
254
- const entries = [makeEntry("a")];
255
- const store = createMockStore(entries);
256
- const tracker = new AccessTracker(store as any, {
257
- ...DEFAULT_ACCESS_TRACKER_CONFIG,
258
- flushIntervalMs: 1,
259
- cooldownMs: 0,
260
- });
261
- tracker.recordAccess(["a"]);
262
- await tracker.flush();
263
-
264
- const meta = JSON.parse(store.data.get("a")!.metadata!) as Record<string, unknown>;
265
- expect(meta.accessCount).toBe(1);
266
- expect(meta.readerIds).toEqual([tracker.readerId]);
267
- expect(meta.distinctReaderCount).toBe(1);
268
- });
269
-
270
- it("dedups same reader and unions different readers", async () => {
271
- // 2026-08-23 语义订正:同进程内的多个 AccessTracker 现在共享一个 readerId。
272
- // 旧版每个实例各随机一个,与本文件测的那句注释(access-tracker.ts:63
273
- // "one stdio MCP server process ≈ one CC session")直接矛盾——components 按
274
- // profile 缓存,同一个会话开两个 profile 就凭空多出一个"读者",
275
- // 把 distinctReaderCount 抬高、进而虚抬 skill-promotion 的 readBoost。
276
- // 所以「不同读者」这一半改用显式 readerId 表达(跨进程/跨会话的真实形态)。
277
- const entries = [makeEntry("a")];
278
- const store = createMockStore(entries);
279
- const t1 = new AccessTracker(store as any, { ...DEFAULT_ACCESS_TRACKER_CONFIG, flushIntervalMs: 1, cooldownMs: 0 });
280
- t1.recordAccess(["a"]);
281
- await t1.flush();
282
- t1.recordAccess(["a"]);
283
- await t1.flush(); // same reader twice → still 1 distinct
284
-
285
- const t2 = new AccessTracker(store as any, { ...DEFAULT_ACCESS_TRACKER_CONFIG, flushIntervalMs: 1, cooldownMs: 0 }, "r-second99");
286
- t2.recordAccess(["a"]);
287
- await t2.flush(); // genuinely different reader → 2 distinct
288
-
289
- const meta = JSON.parse(store.data.get("a")!.metadata!) as Record<string, unknown>;
290
- expect(meta.accessCount).toBe(3);
291
- expect((meta.readerIds as string[]).sort()).toEqual([t1.readerId, t2.readerId].sort());
292
- expect(meta.distinctReaderCount).toBe(2);
293
- });
294
-
295
- it("does not inflate distinct readers when one session builds several trackers", async () => {
296
- // 这是上面那条订正保护的东西:同一会话的两个 profile 不该被数成两个读者。
297
- const entries = [makeEntry("a")];
298
- const store = createMockStore(entries);
299
- const cfg = { ...DEFAULT_ACCESS_TRACKER_CONFIG, flushIntervalMs: 1, cooldownMs: 0 };
300
- for (const tracker of [new AccessTracker(store as any, cfg), new AccessTracker(store as any, cfg)]) {
301
- tracker.recordAccess(["a"]);
302
- await tracker.flush();
303
- }
304
- const meta = JSON.parse(store.data.get("a")!.metadata!) as Record<string, unknown>;
305
- expect((meta.readerIds as string[]).length).toBe(1);
306
- expect(meta.distinctReaderCount).toBe(1);
307
- });
308
-
309
- it("saturates readerIds at READER_ID_CAP without growing the array", async () => {
310
- const capped = Array.from({ length: 8 }, (_, i) => `r-cap${i}`);
311
- const entries = [makeEntry("a", {
312
- metadata: JSON.stringify({ accessCount: 8, readerIds: capped, distinctReaderCount: 8 }),
313
- })];
314
- const store = createMockStore(entries);
315
- const tracker = new AccessTracker(store as any, { ...DEFAULT_ACCESS_TRACKER_CONFIG, flushIntervalMs: 1, cooldownMs: 0 });
316
- tracker.recordAccess(["a"]);
317
- await tracker.flush();
318
-
319
- const meta = JSON.parse(store.data.get("a")!.metadata!) as Record<string, unknown>;
320
- expect((meta.readerIds as string[]).length).toBe(8); // no growth past cap
321
- expect(meta.distinctReaderCount).toBe(8); // saturates, no overcount
322
- expect(meta.accessCount).toBe(9); // access still counted
323
- });
324
- });
@@ -1,125 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from "bun:test";
2
- import { join } from "node:path";
3
- import { mkdirSync, rmSync, existsSync, writeFileSync } from "node:fs";
4
- import {
5
- incrementWriteCount,
6
- getWriteCount,
7
- resetWriteCount,
8
- listScopesAboveThreshold,
9
- getDistillTier,
10
- type ActivityCounterConfig,
11
- } from "../activity-counter.js";
12
-
13
- const TMP_DIR = join(import.meta.dir, "../../.tmp-activity-test");
14
- const testConfig: Partial<ActivityCounterConfig> = {
15
- statsPath: join(TMP_DIR, "activity-stats.json"),
16
- lightThreshold: 3,
17
- standardThreshold: 10,
18
- deepThreshold: 20,
19
- };
20
- const A = "cc:project:a";
21
- const B = "cc:project:b";
22
-
23
- describe("activity-counter (HP-3, per-scope)", async () => {
24
- beforeEach(() => {
25
- mkdirSync(TMP_DIR, { recursive: true });
26
- const p = testConfig.statsPath!;
27
- if (existsSync(p)) rmSync(p);
28
- });
29
-
30
- afterEach(() => {
31
- rmSync(TMP_DIR, { recursive: true, force: true });
32
- });
33
-
34
- describe("incrementWriteCount", async () => {
35
- it("starts at 0 and increments by 1 for a scope", async () => {
36
- expect(getWriteCount(A, testConfig)).toBe(0);
37
- expect(await incrementWriteCount(A, 1, testConfig)).toBe(1);
38
- expect(await incrementWriteCount(A, 1, testConfig)).toBe(2);
39
- expect(getWriteCount(A, testConfig)).toBe(2);
40
- });
41
-
42
- it("increments by arbitrary n", async () => {
43
- await incrementWriteCount(A, 5, testConfig);
44
- expect(getWriteCount(A, testConfig)).toBe(5);
45
- await incrementWriteCount(A, 3, testConfig);
46
- expect(getWriteCount(A, testConfig)).toBe(8);
47
- });
48
-
49
- it("counts each scope independently", async () => {
50
- await incrementWriteCount(A, 4, testConfig);
51
- await incrementWriteCount(B, 1, testConfig);
52
- expect(getWriteCount(A, testConfig)).toBe(4);
53
- expect(getWriteCount(B, testConfig)).toBe(1);
54
- });
55
- });
56
-
57
- describe("resetWriteCount", async () => {
58
- it("resets only the given scope, leaving others intact", async () => {
59
- await incrementWriteCount(A, 7, testConfig);
60
- await incrementWriteCount(B, 5, testConfig);
61
- await resetWriteCount(A, testConfig);
62
- expect(getWriteCount(A, testConfig)).toBe(0);
63
- expect(getWriteCount(B, testConfig)).toBe(5); // not starved by A's reset
64
- });
65
- });
66
-
67
- describe("listScopesAboveThreshold", async () => {
68
- it("returns scopes at or above the threshold only", async () => {
69
- await incrementWriteCount(A, 12, testConfig);
70
- await incrementWriteCount(B, 4, testConfig);
71
- await incrementWriteCount("cc:project:c", 10, testConfig);
72
- const above = listScopesAboveThreshold(10, testConfig).sort();
73
- expect(above).toEqual(["cc:project:a", "cc:project:c"]);
74
- });
75
-
76
- it("returns empty when no scope qualifies", async () => {
77
- await incrementWriteCount(A, 2, testConfig);
78
- expect(listScopesAboveThreshold(10, testConfig)).toEqual([]);
79
- });
80
- });
81
-
82
- describe("getDistillTier", async () => {
83
- it("returns 'none' when below light threshold", async () => {
84
- await incrementWriteCount(A, 2, testConfig);
85
- expect(getDistillTier(A, testConfig)).toBe("none");
86
- });
87
-
88
- it("returns 'light' at light threshold", async () => {
89
- await incrementWriteCount(A, 3, testConfig);
90
- expect(getDistillTier(A, testConfig)).toBe("light");
91
- });
92
-
93
- it("returns 'standard' at standard threshold", async () => {
94
- await incrementWriteCount(A, 10, testConfig);
95
- expect(getDistillTier(A, testConfig)).toBe("standard");
96
- });
97
-
98
- it("returns 'deep' at and above deep threshold", async () => {
99
- await incrementWriteCount(A, 20, testConfig);
100
- expect(getDistillTier(A, testConfig)).toBe("deep");
101
- await incrementWriteCount(A, 100, testConfig);
102
- expect(getDistillTier(A, testConfig)).toBe("deep");
103
- });
104
- });
105
-
106
- describe("resilience", async () => {
107
- it("handles missing stats file gracefully", async () => {
108
- expect(getWriteCount(A, testConfig)).toBe(0);
109
- expect(getDistillTier(A, testConfig)).toBe("none");
110
- expect(listScopesAboveThreshold(1, testConfig)).toEqual([]);
111
- });
112
-
113
- it("handles corrupt stats file gracefully", async () => {
114
- writeFileSync(testConfig.statsPath!, "not-json{{{");
115
- expect(getWriteCount(A, testConfig)).toBe(0);
116
- });
117
-
118
- it("treats the legacy global format as empty (no migration)", async () => {
119
- writeFileSync(testConfig.statsPath!, JSON.stringify({ writesSinceLastDistill: 42, lastResetAt: 1 }));
120
- expect(getWriteCount(A, testConfig)).toBe(0);
121
- // first increment starts a fresh per-scope map
122
- expect(await incrementWriteCount(A, 1, testConfig)).toBe(1);
123
- });
124
- });
125
- });
@@ -1,102 +0,0 @@
1
- import { describe, expect, it } from "bun:test";
2
-
3
- import { formatCollapsedResults } from "../memory-output.js";
4
- import type { RetrievalResult } from "../retriever.js";
5
-
6
- // P-fidelity (点4): adaptive 档 search-native 实现(CC+Codex 共识 C:不复用 resume 的
7
- // collapseResults)。借鉴 RepoPrompt CE「保真度阶梯」内核——按相关性分配保真度 + token 预算,
8
- // 但渲染 query-aware(search 结果是"为什么命中",必须显示匹配证据)。
9
-
10
- function makeResult(id: string, text: string, score: number, metadata = "{}"): RetrievalResult {
11
- return {
12
- entry: {
13
- id,
14
- text,
15
- vector: [],
16
- category: "events",
17
- scope: "test",
18
- importance: 0.5,
19
- timestamp: Date.parse("2026-06-29T00:00:00.000Z"),
20
- metadata,
21
- },
22
- score,
23
- sources: {},
24
- };
25
- }
26
-
27
- const ctx = { query: "QUERYMATCH", profile: "default" as never };
28
-
29
- describe("formatCollapsedResults (adaptive, search-native)", () => {
30
- it("returns 'No results found' for empty input", () => {
31
- expect(formatCollapsedResults([], ctx)).toBe("No results found.");
32
- });
33
-
34
- it("renders high-relevance (score ≥ 0.85) as FULL text", () => {
35
- const out = formatCollapsedResults([makeResult("aaaaaaaa", "full detailed QUERYMATCH content here", 0.95)], ctx);
36
- expect(out).toContain("[FULL] aaaaaaaa");
37
- expect(out).toContain("full detailed QUERYMATCH content here");
38
- });
39
-
40
- it("renders lower-relevance as SNIP and keeps the matching evidence even when it's late in the text (P2-3)", () => {
41
- const text = "Intro sentence. " + "filler ".repeat(20) + "the QUERYMATCH evidence is right here.";
42
- const out = formatCollapsedResults([makeResult("bbbbbbbb", text, 0.60)], ctx);
43
- expect(out).toContain("[SNIP] bbbbbbbb");
44
- // 关键:query-aware snippet 必须保留匹配证据,而不是只截开头(这正是复用 collapse 时的 P2-3)
45
- expect(out).toContain("QUERYMATCH");
46
- });
47
-
48
- it("does NOT drop low-score hits — adaptive is display, not a filter (P2-1)", () => {
49
- const out = formatCollapsedResults([makeResult("cccccccc", "valid low-score QUERYMATCH hit", 0.30)], ctx);
50
- expect(out).toContain("[SNIP] cccccccc");
51
- expect(out).not.toContain("No results");
52
- });
53
-
54
- it("preserves caller order (e.g. highlight reorder), not raw score order (P2-2)", () => {
55
- // 模拟 render:highlight — contextual 最相关但 raw score 较低的排在前。
56
- const out = formatCollapsedResults([
57
- makeResult("term0000", "term-relevant QUERYMATCH but lower score", 0.55),
58
- makeResult("high0000", "higher score but less contextual", 0.95),
59
- ], ctx);
60
- const idxTerm = out.indexOf("term0000");
61
- const idxHigh = out.indexOf("high0000");
62
- expect(idxTerm).toBeGreaterThan(-1);
63
- expect(idxHigh).toBeGreaterThan(-1);
64
- expect(idxTerm).toBeLessThan(idxHigh); // term0000 仍在前
65
- });
66
-
67
- it("includes locator info (id / score / category)", () => {
68
- const out = formatCollapsedResults([makeResult("dddddddd", "QUERYMATCH content", 0.90)], ctx);
69
- expect(out).toContain("dddddddd");
70
- expect(out).toContain("90%");
71
- expect(out).toContain("events"); // category label
72
- });
73
-
74
- it("keeps the match visible for a short acronym query (adaptiveSnippet window, P2)", () => {
75
- // pickBestSnippet 对短缩写 query 会退回开头;adaptiveSnippet 用 raw query 定位 + 窗口。
76
- const text = "Some long intro line without the keyword. ".repeat(5) + "Finally the CI pipeline broke here.";
77
- const out = formatCollapsedResults([makeResult("ffffffff", text, 0.50)], { query: "CI", profile: "default" as never });
78
- expect(out).toContain("[SNIP] ffffffff");
79
- expect(out.toLowerCase()).toContain("ci pipeline"); // 匹配证据可见,而非只截开头
80
- });
81
-
82
- it("windows around a match that is late in a long single line (P2)", () => {
83
- // 超长无句读的 log 行:整句截断会丢后段匹配;adaptiveSnippet 取匹配词周围窗口。
84
- const text = "prefix ".repeat(50) + "ZEBRAWORD is the late match here" + " suffix".repeat(50);
85
- const out = formatCollapsedResults([makeResult("11111111", text, 0.50)], { query: "ZEBRAWORD", profile: "default" as never });
86
- expect(out).toContain("ZEBRAWORD"); // 后段匹配仍在窗口内
87
- });
88
-
89
- it("does not false-match an acronym inside an unrelated earlier word (word boundary, P2)", () => {
90
- // "specific" 含子串 "ci";裸 substring 会 window 到它、漏掉真的 CI。word boundary 跳过。
91
- const text = "This is a specific introduction. " + "filler ".repeat(10) + "Then the CI build failed.";
92
- const out = formatCollapsedResults([makeResult("22222222", text, 0.50)], { query: "CI", profile: "default" as never });
93
- expect(out).toContain("CI build"); // window 到真 CI,不是 specific 里的 ci
94
- });
95
-
96
- it("matches CJK fallback queries via substring (ASCII word boundary doesn't apply, P2-1)", () => {
97
- // 中文单字 query:JS \b 只认 ASCII,必须走 substring,否则退回开头丢匹配(Alice 的记忆大量中文)。
98
- const text = "开头一些无关的内容。" + "填充内容 ".repeat(10) + "这里才提到猫的关键证据。";
99
- const out = formatCollapsedResults([makeResult("33333333", text, 0.50)], { query: "猫", profile: "default" as never });
100
- expect(out).toContain("猫"); // 中文匹配证据可见
101
- });
102
- });