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,122 +0,0 @@
1
- import { describe, test, expect } from "bun:test";
2
- import { verifyDraft, tokensOf, type VerifyDraft, type VerifyEvidence } from "../skill-verifier.js";
3
-
4
- describe("verifyDraft — tool coverage", () => {
5
- test("passes when draft tools ⊆ evidence tools and narrative resonates", () => {
6
- const draft: VerifyDraft = {
7
- tools: ["git", "bun test"],
8
- summary: "Fix the failing deploy by resetting the database migration",
9
- steps: ["reset migration", "rerun deploy"],
10
- };
11
- const evidence: VerifyEvidence[] = [
12
- { text: "deploy failed, reset migration then rerun deploy worked", tools: ["git", "bun test"] },
13
- { text: "another deploy reset migration fix", tools: ["git"] },
14
- ];
15
- const r = verifyDraft(draft, evidence);
16
- expect(r.ok).toBe(true);
17
- expect(r.coverage).toBe(1);
18
- expect(r.unmappedTools).toEqual([]);
19
- });
20
-
21
- test("fails coverage when draft claims a tool no evidence used (hallucinated command)", () => {
22
- const draft: VerifyDraft = {
23
- tools: ["fictional_cmd", "another_fake"],
24
- summary: "deploy reset migration rerun",
25
- steps: [],
26
- };
27
- const evidence: VerifyEvidence[] = [
28
- { text: "deploy reset migration rerun worked", tools: ["git"] },
29
- ];
30
- const r = verifyDraft(draft, evidence);
31
- expect(r.ok).toBe(false);
32
- expect(r.reason).toContain("coverage");
33
- expect(r.unmappedTools).toContain("fictional_cmd");
34
- });
35
-
36
- test("empty draft tools → coverage 1 (nothing to hallucinate), judged on resonance only", () => {
37
- const draft: VerifyDraft = { tools: [], summary: "deploy reset migration", steps: [] };
38
- const evidence: VerifyEvidence[] = [
39
- { text: "deploy reset migration done", tools: [] },
40
- { text: "deploy reset migration again", tools: [] },
41
- ];
42
- const r = verifyDraft(draft, evidence);
43
- expect(r.coverage).toBe(1);
44
- expect(r.ok).toBe(true);
45
- });
46
-
47
- test("coverage exactly 0.5 (1 of 2 tools mapped) passes the strict < 0.5 threshold", () => {
48
- const r = verifyDraft(
49
- { tools: ["git", "fake_cmd"], summary: "deploy reset migration rerun", steps: [] },
50
- [{ text: "deploy reset migration rerun", tools: ["git"] }],
51
- );
52
- expect(r.coverage).toBe(0.5);
53
- expect(r.reason ?? "").not.toContain("coverage");
54
- expect(r.unmappedTools).toContain("fake_cmd");
55
- });
56
- });
57
-
58
- describe("verifyDraft — evidence resonance", () => {
59
- test("fails resonance when narrative shares no tokens with evidence", () => {
60
- const draft: VerifyDraft = {
61
- tools: [],
62
- summary: "quantum entanglement teleportation protocol alpha",
63
- steps: ["calibrate flux capacitor"],
64
- };
65
- const evidence: VerifyEvidence[] = [
66
- { text: "deploy database migration reset rerun", tools: [] },
67
- { text: "another unrelated commit push branch", tools: [] },
68
- ];
69
- const r = verifyDraft(draft, evidence);
70
- expect(r.ok).toBe(false);
71
- expect(r.reason).toContain("resonance");
72
- });
73
-
74
- test("CJK: passes resonance on Chinese pattern + cases sharing ≥2 bigrams", () => {
75
- const draft: VerifyDraft = {
76
- tools: [],
77
- summary: "重启服务前先检查代理端口配置",
78
- steps: ["确认代理端口", "重启服务"],
79
- };
80
- const evidence: VerifyEvidence[] = [
81
- { text: "重启服务后代理端口失效,检查配置解决", tools: [] },
82
- { text: "代理端口配置错误导致重启失败", tools: [] },
83
- ];
84
- const r = verifyDraft(draft, evidence);
85
- expect(r.ok).toBe(true);
86
- expect(r.resonance).toBeGreaterThanOrEqual(0.5);
87
- });
88
- });
89
-
90
- describe("verifyDraft — boundaries", () => {
91
- test("empty evidence → no-evidence verdict", () => {
92
- const r = verifyDraft({ tools: ["git"], summary: "x", steps: [] }, []);
93
- expect(r.ok).toBe(false);
94
- expect(r.reason).toBe("no-evidence");
95
- });
96
-
97
- test("verdict always carries coverage + resonance numbers", () => {
98
- const r = verifyDraft(
99
- { tools: ["git"], summary: "deploy reset migration rerun", steps: [] },
100
- [{ text: "deploy reset migration rerun", tools: ["git"] }],
101
- );
102
- expect(typeof r.coverage).toBe("number");
103
- expect(typeof r.resonance).toBe("number");
104
- });
105
- });
106
-
107
- describe("tokensOf", () => {
108
- test("extracts ASCII identifiers ≥4 chars, drops stopwords", () => {
109
- const toks = tokensOf("deploy the database migration");
110
- expect(toks.has("deploy")).toBe(true);
111
- expect(toks.has("database")).toBe(true);
112
- expect(toks.has("migration")).toBe(true);
113
- expect(toks.has("the")).toBe(false);
114
- });
115
-
116
- test("extracts CJK 2-gram bigrams", () => {
117
- const toks = tokensOf("重启服务");
118
- expect(toks.has("重启")).toBe(true);
119
- expect(toks.has("启服")).toBe(true);
120
- expect(toks.has("服务")).toBe(true);
121
- });
122
- });
@@ -1,254 +0,0 @@
1
- import { describe, expect, it, beforeEach, afterEach } from "bun:test";
2
- import { existsSync, mkdirSync, rmSync, readFileSync } from "node:fs";
3
- import { join } from "node:path";
4
- import { tmpdir } from "node:os";
5
- import {
6
- readHeartbeats,
7
- writeHeartbeat,
8
- checkSourceStaleness,
9
- formatAge,
10
- type HeartbeatFile,
11
- } from "../source-heartbeat.js";
12
- import { runDataCheckup } from "../data-checkup.js";
13
- import type { MemoryEntry, MemoryStore } from "../store.js";
14
-
15
- // ---------------------------------------------------------------------------
16
- // Helpers
17
- // ---------------------------------------------------------------------------
18
-
19
- function tmpHeartbeatPath(): string {
20
- const dir = join(tmpdir(), `recallnest-test-heartbeat-${Date.now()}`);
21
- mkdirSync(dir, { recursive: true });
22
- return join(dir, "source-heartbeat.json");
23
- }
24
-
25
- function makeEntry(id: string): MemoryEntry {
26
- return {
27
- id,
28
- text: "test memory",
29
- vector: [1, 0, 0, 0, 0],
30
- category: "events",
31
- scope: "project:test",
32
- importance: 0.7,
33
- timestamp: Date.now(),
34
- metadata: JSON.stringify({
35
- evolution: {
36
- status: "active",
37
- version: 1,
38
- accessCount: 0,
39
- lastAccessedAt: null,
40
- supersededBy: null,
41
- consolidatedInto: null,
42
- contributedToPattern: null,
43
- sourceMemories: [],
44
- validFrom: Date.now(),
45
- validUntil: null,
46
- },
47
- }),
48
- };
49
- }
50
-
51
- function createMockStore(entries: MemoryEntry[]): Pick<MemoryStore, "list" | "listPage" | "stats" | "getVectors"> {
52
- return {
53
- // 复刻真实 store:list() 为性能不返回向量,诊断检查经 getVectors 补回。
54
- async list() { return entries.map(e => ({ ...e, vector: [] })); },
55
- async listPage(opts: { limit?: number; offset?: number } = {}) {
56
- const { limit = 1000, offset = 0 } = opts;
57
- return entries.slice(offset, offset + limit).map(e => ({ ...e, vector: [] }));
58
- },
59
- async stats() {
60
- return { totalCount: entries.length, scopeCounts: {}, categoryCounts: {} };
61
- },
62
- async getVectors(ids: string[]) {
63
- const map = new Map<string, number[]>();
64
- for (const e of entries) {
65
- if (ids.includes(e.id) && e.vector && e.vector.length > 0) map.set(e.id, e.vector);
66
- }
67
- return map;
68
- },
69
- } as Pick<MemoryStore, "list" | "listPage" | "stats" | "getVectors">;
70
- }
71
-
72
- // ---------------------------------------------------------------------------
73
- // Tests: readHeartbeats / writeHeartbeat
74
- // ---------------------------------------------------------------------------
75
-
76
- describe("source-heartbeat", () => {
77
- let tmpPath: string;
78
-
79
- beforeEach(() => {
80
- tmpPath = tmpHeartbeatPath();
81
- });
82
-
83
- afterEach(() => {
84
- try {
85
- const dir = join(tmpPath, "..");
86
- rmSync(dir, { recursive: true, force: true });
87
- } catch { /* ignore */ }
88
- });
89
-
90
- it("returns empty object when file does not exist", () => {
91
- const result = readHeartbeats(tmpPath);
92
- expect(result).toEqual({});
93
- });
94
-
95
- it("writes and reads back a heartbeat", () => {
96
- writeHeartbeat("obsidian", 42, [], tmpPath);
97
-
98
- const heartbeats = readHeartbeats(tmpPath);
99
- expect(Object.keys(heartbeats)).toEqual(["obsidian"]);
100
- expect(heartbeats.obsidian.source).toBe("obsidian");
101
- expect(heartbeats.obsidian.recordsIngested).toBe(42);
102
- expect(heartbeats.obsidian.errors).toEqual([]);
103
- expect(heartbeats.obsidian.lastIngest).toBeTruthy();
104
- });
105
-
106
- it("updates existing source without losing other sources", () => {
107
- writeHeartbeat("obsidian", 10, [], tmpPath);
108
- writeHeartbeat("email", 5, ["timeout"], tmpPath);
109
- writeHeartbeat("obsidian", 20, [], tmpPath); // update
110
-
111
- const heartbeats = readHeartbeats(tmpPath);
112
- expect(Object.keys(heartbeats).sort()).toEqual(["email", "obsidian"]);
113
- expect(heartbeats.obsidian.recordsIngested).toBe(20);
114
- expect(heartbeats.email.recordsIngested).toBe(5);
115
- expect(heartbeats.email.errors).toEqual(["timeout"]);
116
- });
117
-
118
- it("caps stored errors to 10", () => {
119
- const manyErrors = Array.from({ length: 15 }, (_, i) => `error-${i}`);
120
- writeHeartbeat("broken", 0, manyErrors, tmpPath);
121
-
122
- const heartbeats = readHeartbeats(tmpPath);
123
- expect(heartbeats.broken.errors.length).toBe(10);
124
- });
125
- });
126
-
127
- // ---------------------------------------------------------------------------
128
- // Tests: checkSourceStaleness
129
- // ---------------------------------------------------------------------------
130
-
131
- describe("checkSourceStaleness", () => {
132
- let tmpPath: string;
133
-
134
- beforeEach(() => {
135
- tmpPath = tmpHeartbeatPath();
136
- });
137
-
138
- afterEach(() => {
139
- try {
140
- rmSync(join(tmpPath, ".."), { recursive: true, force: true });
141
- } catch { /* ignore */ }
142
- });
143
-
144
- it("returns empty for fresh sources", () => {
145
- writeHeartbeat("obsidian", 10, [], tmpPath);
146
- const stale = checkSourceStaleness(7, tmpPath);
147
- expect(stale).toEqual([]);
148
- });
149
-
150
- it("detects stale sources", () => {
151
- // Manually write a stale heartbeat
152
- const staleDate = new Date(Date.now() - 10 * 86_400_000).toISOString();
153
- const heartbeats: HeartbeatFile = {
154
- old_source: {
155
- source: "old_source",
156
- lastIngest: staleDate,
157
- recordsIngested: 5,
158
- errors: [],
159
- },
160
- };
161
- const { writeFileSync } = require("node:fs");
162
- writeFileSync(tmpPath, JSON.stringify(heartbeats));
163
-
164
- const stale = checkSourceStaleness(7, tmpPath);
165
- expect(stale.length).toBe(1);
166
- expect(stale[0].source).toBe("old_source");
167
- expect(stale[0].daysSince).toBeGreaterThanOrEqual(9);
168
- });
169
-
170
- it("returns empty for missing file", () => {
171
- const stale = checkSourceStaleness(7, "/tmp/nonexistent-path.json");
172
- expect(stale).toEqual([]);
173
- });
174
- });
175
-
176
- // ---------------------------------------------------------------------------
177
- // Tests: formatAge
178
- // ---------------------------------------------------------------------------
179
-
180
- describe("formatAge", () => {
181
- it("returns 'just now' for recent timestamps", () => {
182
- expect(formatAge(new Date().toISOString())).toBe("just now");
183
- });
184
-
185
- it("returns hours for <24h", () => {
186
- const twoHoursAgo = new Date(Date.now() - 2 * 3_600_000).toISOString();
187
- expect(formatAge(twoHoursAgo)).toBe("2h ago");
188
- });
189
-
190
- it("returns days for >=24h", () => {
191
- const threeDaysAgo = new Date(Date.now() - 3 * 86_400_000).toISOString();
192
- expect(formatAge(threeDaysAgo)).toBe("3d ago");
193
- });
194
- });
195
-
196
- // ---------------------------------------------------------------------------
197
- // Tests: data_checkup integration
198
- // ---------------------------------------------------------------------------
199
-
200
- describe("data_checkup with source_health", () => {
201
- let tmpPath: string;
202
-
203
- beforeEach(() => {
204
- tmpPath = tmpHeartbeatPath();
205
- });
206
-
207
- afterEach(() => {
208
- try {
209
- rmSync(join(tmpPath, ".."), { recursive: true, force: true });
210
- } catch { /* ignore */ }
211
- });
212
-
213
- it("includes source_health check (8 checks total)", async () => {
214
- const entries = [makeEntry("a"), makeEntry("b")];
215
- const report = await runDataCheckup({
216
- store: createMockStore(entries),
217
- openConflictCount: 0,
218
- heartbeatPath: tmpPath,
219
- });
220
-
221
- expect(report.checks.length).toBe(8);
222
- const sourceCheck = report.checks.find((c) => c.name === "source_health");
223
- expect(sourceCheck).toBeTruthy();
224
- expect(sourceCheck!.status).toBe("ok");
225
- expect(sourceCheck!.detail).toContain("No connector sources tracked yet");
226
- });
227
-
228
- it("reports warning for stale sources", async () => {
229
- // Write a stale heartbeat
230
- const staleDate = new Date(Date.now() - 10 * 86_400_000).toISOString();
231
- const heartbeats: HeartbeatFile = {
232
- email: {
233
- source: "email",
234
- lastIngest: staleDate,
235
- recordsIngested: 5,
236
- errors: [],
237
- },
238
- };
239
- const { writeFileSync } = require("node:fs");
240
- writeFileSync(tmpPath, JSON.stringify(heartbeats));
241
-
242
- const entries = [makeEntry("a")];
243
- const report = await runDataCheckup({
244
- store: createMockStore(entries),
245
- openConflictCount: 0,
246
- heartbeatPath: tmpPath,
247
- });
248
-
249
- const sourceCheck = report.checks.find((c) => c.name === "source_health");
250
- expect(sourceCheck).toBeTruthy();
251
- expect(sourceCheck!.status).toBe("warning");
252
- expect(sourceCheck!.detail).toContain("stale");
253
- });
254
- });
@@ -1,62 +0,0 @@
1
- import { describe, expect, it } from "bun:test";
2
-
3
- import { selectStableResults } from "../context-composer-stable-selection.js";
4
- import { ResumeContextResponseSchema } from "../session-schema.js";
5
- import type { RetrievalResult } from "../retriever.js";
6
-
7
- // selectStableResults 产出的行必须过得了 stableContext 的 schema 上限。
8
- // 这条曾经断过:formatStableResult 截到 230、schema 上限 220,中间的
9
- // interleaveUnique(context-composer-stable.ts:199-206)不再截一次,
10
- // 于是 221~230 字符的行会让 composeResumeContext 末尾的
11
- // ResumeContextResponseSchema.parse 抛错,**整个 resume_context 调用失败**
12
- // —— 不是降级,是抛异常。实测 14 条真实问法里 1 条命中。
13
- //
14
- // 用长文本喂进去,逼出最长可能的输出,再拿真 schema 验,
15
- // 这样将来任一侧的数字被改动都会在这里红,而不是等生产上抛。
16
-
17
- const SCHEMA_MAX = 220;
18
-
19
- function longPreference(): RetrievalResult {
20
- return {
21
- entry: {
22
- id: "test-long-pref",
23
- // 远超上限的正文,确保格式化后会吃满截断长度
24
- text: `用户确立的一条长期偏好:${"跨端协作时先确认目标仓库与路径再动手,".repeat(30)}`,
25
- vector: [],
26
- category: "preferences",
27
- scope: "memory:pivot",
28
- importance: 0.9,
29
- timestamp: Date.now(),
30
- },
31
- score: 1,
32
- sources: {},
33
- } as RetrievalResult;
34
- }
35
-
36
- describe("stableContext 长度契约", () => {
37
- it("超长偏好格式化后不超过 schema 上限", () => {
38
- const lines = selectStableResults("preferences", [longPreference()], 3, {
39
- taskSeed: "跨端协作要注意什么",
40
- });
41
- expect(lines.length).toBeGreaterThan(0);
42
- for (const line of lines) {
43
- expect(line.length).toBeLessThanOrEqual(SCHEMA_MAX);
44
- }
45
- });
46
-
47
- it("这些行能通过真实的 ResumeContextResponseSchema", () => {
48
- const stableContext = selectStableResults("preferences", [longPreference()], 3, {
49
- taskSeed: "跨端协作要注意什么",
50
- });
51
- // 只验长度这一维:其余必填字段给最小合法值,schema 变动时这里也会跟着红
52
- expect(() =>
53
- ResumeContextResponseSchema.parse({
54
- summary: "test",
55
- generatedAt: new Date().toISOString(),
56
- stableContext,
57
- relevantPatterns: [],
58
- recentCases: [],
59
- }),
60
- ).not.toThrow();
61
- });
62
- });
@@ -1,235 +0,0 @@
1
- /**
2
- * Tests for the store-level metadata write channel (P0 Track A):
3
- * - update() atomic upsert via mergeInsert (no delete+add vanish window)
4
- * - patchMetadata() per-id serial queue: no lost increments under concurrency,
5
- * rejection isolation, queue self-cleanup, prefix/full-id queue sharing
6
- * - listPage() true DB-level pagination with category filter and includeVector
7
- */
8
- import { afterEach, describe, expect, it } from "bun:test";
9
- import { mkdtempSync, rmSync } from "node:fs";
10
- import { tmpdir } from "node:os";
11
- import { join } from "node:path";
12
-
13
- import { MemoryStore, type MemoryEntry } from "../store.js";
14
-
15
- const cleanupPaths: string[] = [];
16
-
17
- afterEach(() => {
18
- while (cleanupPaths.length > 0) {
19
- const target = cleanupPaths.pop();
20
- if (target) {
21
- rmSync(target, { recursive: true, force: true });
22
- }
23
- }
24
- });
25
-
26
- function createStore(): MemoryStore {
27
- const dbPath = mkdtempSync(join(tmpdir(), "recallnest-patch-meta-"));
28
- cleanupPaths.push(dbPath);
29
- return new MemoryStore({ dbPath, vectorDim: 3 });
30
- }
31
-
32
- async function seed(store: MemoryStore, overrides: Partial<MemoryEntry> = {}): Promise<MemoryEntry> {
33
- return store.store({
34
- text: "patch metadata target",
35
- vector: [1, 0, 0],
36
- category: "cases",
37
- scope: "project:patch-test",
38
- importance: 0.6,
39
- metadata: "{}",
40
- ...overrides,
41
- });
42
- }
43
-
44
- describe("MemoryStore.update (mergeInsert upsert)", () => {
45
- it("keeps the row continuously visible and intact after update", async () => {
46
- const store = createStore();
47
- const entry = await seed(store);
48
-
49
- const updated = await store.update(entry.id, { metadata: JSON.stringify({ marker: 1 }) });
50
- expect(updated).not.toBeNull();
51
-
52
- // Immediately readable by id, by list, and by vector search
53
- const byId = await store.getById(entry.id);
54
- expect(byId?.id).toBe(entry.id);
55
- expect(JSON.parse(byId?.metadata || "{}").marker).toBe(1);
56
-
57
- const listed = await store.list(["project:patch-test"]);
58
- expect(listed.map(e => e.id)).toContain(entry.id);
59
-
60
- const hits = await store.vectorSearch([1, 0, 0], 5, 0, ["project:patch-test"]);
61
- expect(hits.map(h => h.entry.id)).toContain(entry.id);
62
-
63
- // Vector column survived the upsert
64
- const vectors = await store.getVectors([entry.id]);
65
- expect(vectors.get(entry.id)).toEqual([1, 0, 0]);
66
-
67
- // No duplicate row was created
68
- const all = await store.list(["project:patch-test"], undefined, 100, 0);
69
- expect(all.filter(e => e.id === entry.id)).toHaveLength(1);
70
- });
71
- });
72
-
73
- describe("MemoryStore.patchMetadata", () => {
74
- it("does not lose increments under 20 concurrent mixed patches on one id", async () => {
75
- const store = createStore();
76
- const entry = await seed(store);
77
-
78
- const jobs: Array<Promise<unknown>> = [];
79
- for (let i = 0; i < 10; i++) {
80
- jobs.push(store.patchMetadata(entry.id, meta => {
81
- const usage = (meta.usage as Record<string, unknown> | undefined) ?? {};
82
- const count = typeof usage.useCount === "number" ? usage.useCount : 0;
83
- meta.usage = { ...usage, useCount: count + 1 };
84
- return meta;
85
- }));
86
- jobs.push(store.patchMetadata(entry.id, meta => {
87
- const count = typeof meta.accessCount === "number" ? meta.accessCount : 0;
88
- meta.accessCount = count + 1;
89
- return meta;
90
- }));
91
- }
92
- await Promise.all(jobs);
93
-
94
- const after = await store.getById(entry.id);
95
- const meta = JSON.parse(after?.metadata || "{}");
96
- expect(meta.usage.useCount).toBe(10);
97
- expect(meta.accessCount).toBe(10);
98
- });
99
-
100
- it("rejects the failing patch only; later patches still run; queue self-cleans", async () => {
101
- const store = createStore();
102
- const entry = await seed(store);
103
-
104
- const first = store.patchMetadata(entry.id, meta => {
105
- meta.step = 1;
106
- return meta;
107
- });
108
- const failing = store.patchMetadata(entry.id, () => {
109
- throw new Error("boom");
110
- });
111
- const third = store.patchMetadata(entry.id, meta => {
112
- meta.step = 3;
113
- return meta;
114
- });
115
-
116
- await expect(failing).rejects.toThrow("boom");
117
- await first;
118
- await third;
119
-
120
- const after = await store.getById(entry.id);
121
- expect(JSON.parse(after?.metadata || "{}").step).toBe(3);
122
- expect(store.pendingMetadataPatchCount).toBe(0);
123
- });
124
-
125
- it("routes prefix and full-id callers through the same queue", async () => {
126
- const store = createStore();
127
- const entry = await seed(store);
128
- // getById prefix semantics = string prefix (LIKE 'xxx%'), so take the
129
- // first 8 hex chars (the segment before the first hyphen).
130
- const prefix = entry.id.slice(0, 8);
131
-
132
- const jobs: Array<Promise<unknown>> = [];
133
- for (let i = 0; i < 10; i++) {
134
- jobs.push(store.patchMetadata(entry.id, meta => {
135
- const count = typeof meta.n === "number" ? meta.n : 0;
136
- meta.n = count + 1;
137
- return meta;
138
- }));
139
- jobs.push(store.patchMetadata(prefix, meta => {
140
- const count = typeof meta.n === "number" ? meta.n : 0;
141
- meta.n = count + 1;
142
- return meta;
143
- }));
144
- }
145
- await Promise.all(jobs);
146
-
147
- const after = await store.getById(entry.id);
148
- expect(JSON.parse(after?.metadata || "{}").n).toBe(20);
149
- expect(store.pendingMetadataPatchCount).toBe(0);
150
- });
151
-
152
- it("returns null for a missing id without queue residue", async () => {
153
- const store = createStore();
154
- await seed(store); // initialize table
155
- const result = await store.patchMetadata("00000000-0000-4000-8000-000000000000", meta => meta);
156
- expect(result).toBeNull();
157
- // settle the no-op queue entry
158
- await Promise.resolve();
159
- await Promise.resolve();
160
- expect(store.pendingMetadataPatchCount).toBe(0);
161
- });
162
-
163
- it("enforces scope permissions like update()", async () => {
164
- const store = createStore();
165
- const entry = await seed(store);
166
- await expect(
167
- store.patchMetadata(entry.id, meta => meta, ["project:other"]),
168
- ).rejects.toThrow(/outside accessible scopes/);
169
- });
170
- });
171
-
172
- describe("MemoryStore.listPage", () => {
173
- it("paginates at the DB level with category pushdown", async () => {
174
- const store = createStore();
175
- for (let i = 0; i < 7; i++) {
176
- await seed(store, { text: `case ${i}`, category: "cases" });
177
- }
178
- for (let i = 0; i < 3; i++) {
179
- await seed(store, { text: `pattern ${i}`, category: "patterns", vector: [0, 1, 0] });
180
- }
181
-
182
- const page1 = await store.listPage({ scopeFilter: ["project:patch-test"], category: "cases", limit: 4, offset: 0 });
183
- const page2 = await store.listPage({ scopeFilter: ["project:patch-test"], category: "cases", limit: 4, offset: 4 });
184
- expect(page1).toHaveLength(4);
185
- expect(page2).toHaveLength(3);
186
- expect([...page1, ...page2].every(e => e.category === "cases")).toBe(true);
187
- // No overlap between pages
188
- const ids = new Set([...page1, ...page2].map(e => e.id));
189
- expect(ids.size).toBe(7);
190
-
191
- const patterns = await store.listPage({ scopeFilter: ["project:patch-test"], category: "patterns", limit: 100 });
192
- expect(patterns).toHaveLength(3);
193
- });
194
-
195
- it("omits vectors by default and returns them with includeVector", async () => {
196
- const store = createStore();
197
- await seed(store);
198
-
199
- const [bare] = await store.listPage({ scopeFilter: ["project:patch-test"], limit: 1 });
200
- expect(bare.vector).toEqual([]);
201
-
202
- const [withVec] = await store.listPage({ scopeFilter: ["project:patch-test"], limit: 1, includeVector: true });
203
- expect(withVec.vector).toEqual([1, 0, 0]);
204
- });
205
- });
206
-
207
- describe("MemoryStore.repairSingletonVersionGroups", () => {
208
- it("dissolves only singleton groups and preserves all unrelated metadata", async () => {
209
- const store = createStore();
210
- const singleton = await seed(store, {
211
- text: "singleton",
212
- metadata: JSON.stringify({
213
- version_group: "vg-single",
214
- version_rank: 1.4,
215
- version_created: "2026-07-07T00:00:00.000Z",
216
- marker: "keep",
217
- }),
218
- });
219
- const pairA = await seed(store, {
220
- text: "pair-a",
221
- metadata: JSON.stringify({ version_group: "vg-pair", version_rank: 2, version_created: "2026-07-07T00:00:00.000Z" }),
222
- });
223
- const pairB = await seed(store, {
224
- text: "pair-b",
225
- metadata: JSON.stringify({ version_group: "vg-pair", version_rank: 1, version_created: "2026-07-07T00:00:00.000Z" }),
226
- });
227
-
228
- expect(await store.repairSingletonVersionGroups()).toBe(1);
229
- const singletonMeta = JSON.parse((await store.getById(singleton.id))!.metadata!);
230
- expect(singletonMeta).toEqual({ marker: "keep" });
231
- expect(JSON.parse((await store.getById(pairA.id))!.metadata!).version_group).toBe("vg-pair");
232
- expect(JSON.parse((await store.getById(pairB.id))!.metadata!).version_group).toBe("vg-pair");
233
- expect(await store.repairSingletonVersionGroups()).toBe(0);
234
- });
235
- });