@modusensus/dsh-mneme 0.8.3 → 0.8.5

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 (205) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +727 -636
  3. package/bin/cli.mjs +0 -0
  4. package/bin/dsh-mneme-mcp.mjs +510 -0
  5. package/cordis.patch.yml +21 -21
  6. package/lib/api-standalone.js +506 -316
  7. package/lib/api.js +1403 -1316
  8. package/lib/bootstrap.js +211 -0
  9. package/lib/client.js +4657 -4366
  10. package/lib/commands.js +95 -95
  11. package/lib/config.js +615 -510
  12. package/lib/document.js +221 -0
  13. package/lib/dream/clustering.js +118 -118
  14. package/lib/dream/decisions.js +658 -658
  15. package/lib/dream/narratives.js +44 -0
  16. package/lib/dream/sleep.js +702 -700
  17. package/lib/dream.js +147 -16
  18. package/lib/embedding.js +177 -177
  19. package/lib/entities/extractor.js +249 -249
  20. package/lib/guide.js +44 -0
  21. package/lib/heat.js +144 -136
  22. package/lib/hot-memory.js +46 -46
  23. package/lib/index.js +515 -515
  24. package/lib/inject.js +375 -295
  25. package/lib/lang.js +208 -179
  26. package/lib/local-embedder.js +320 -320
  27. package/lib/mirror.js +198 -198
  28. package/lib/organize.js +450 -0
  29. package/lib/quality-filter.js +126 -126
  30. package/lib/recall-stats.js +125 -0
  31. package/lib/reranker.js +269 -269
  32. package/lib/runtime/adopt.js +176 -176
  33. package/lib/runtime/closure.js +172 -172
  34. package/lib/runtime/download.js +247 -247
  35. package/lib/runtime/layout.js +265 -235
  36. package/lib/runtime/loader.js +252 -249
  37. package/lib/runtime/model-fetch.js +361 -361
  38. package/lib/runtime/provision.js +237 -237
  39. package/lib/runtime/tarball.js +130 -130
  40. package/lib/runtime/verify.js +204 -191
  41. package/lib/scope.js +118 -118
  42. package/lib/search/adaptive.js +60 -22
  43. package/lib/search/bm25.js +96 -96
  44. package/lib/service.js +2324 -2087
  45. package/lib/settings.js +459 -420
  46. package/lib/store.js +2549 -2367
  47. package/lib/summarize.js +820 -449
  48. package/lib/tools.js +702 -574
  49. package/lib/vector-index.js +110 -110
  50. package/lib/version-check.js +122 -122
  51. package/package.json +109 -108
  52. package/runtime-manifest.json +526 -596
  53. package/scripts/benchmark-embed.js +201 -201
  54. package/scripts/benchmark-recall.js +199 -199
  55. package/scripts/benchmark-rerank.js +166 -166
  56. package/scripts/build-runtime-manifest.mjs +224 -217
  57. package/scripts/check-sync.js +42 -42
  58. package/scripts/e2e-dsh.js +220 -220
  59. package/scripts/mneme-runtime.mjs +277 -277
  60. package/scripts/stress-dsh.js +255 -255
  61. package/scripts/sync-lib.js +47 -47
  62. package/scripts/sync-test-badge.mjs +67 -61
  63. package/src/api-standalone.js +506 -316
  64. package/src/api.js +1403 -1316
  65. package/src/bootstrap.js +211 -0
  66. package/src/commands.js +95 -95
  67. package/src/config.js +615 -510
  68. package/src/document.js +221 -0
  69. package/src/dream/clustering.js +118 -118
  70. package/src/dream/decisions.js +658 -658
  71. package/src/dream/narratives.js +44 -0
  72. package/src/dream/sleep.js +702 -700
  73. package/src/dream.js +147 -16
  74. package/src/embedding.js +177 -177
  75. package/src/entities/extractor.js +249 -249
  76. package/src/guide.js +44 -0
  77. package/src/heat.js +144 -136
  78. package/src/hot-memory.js +46 -46
  79. package/src/index.js +515 -515
  80. package/src/inject.js +375 -295
  81. package/src/lang.js +208 -179
  82. package/src/local-embedder.js +320 -320
  83. package/src/mirror.js +198 -198
  84. package/src/organize.js +450 -0
  85. package/src/quality-filter.js +126 -126
  86. package/src/recall-stats.js +125 -0
  87. package/src/reranker.js +269 -269
  88. package/src/runtime/adopt.js +176 -176
  89. package/src/runtime/closure.js +172 -172
  90. package/src/runtime/download.js +247 -247
  91. package/src/runtime/layout.js +265 -235
  92. package/src/runtime/loader.js +252 -249
  93. package/src/runtime/model-fetch.js +361 -361
  94. package/src/runtime/provision.js +237 -237
  95. package/src/runtime/tarball.js +130 -130
  96. package/src/runtime/verify.js +204 -191
  97. package/src/scope.js +118 -118
  98. package/src/search/adaptive.js +60 -22
  99. package/src/search/bm25.js +96 -96
  100. package/src/service.js +2324 -2087
  101. package/src/settings.js +459 -420
  102. package/src/store.js +2549 -2367
  103. package/src/summarize.js +820 -449
  104. package/src/tools.js +702 -574
  105. package/src/vector-index.js +110 -110
  106. package/src/version-check.js +122 -122
  107. package/test/adaptive-inject-budget.test.js +85 -0
  108. package/test/api-routes-gaps.test.js +218 -218
  109. package/test/api.test.js +1640 -1482
  110. package/test/audit.test.js +448 -448
  111. package/test/benchmark.test.js +94 -94
  112. package/test/bootstrap.test.js +220 -0
  113. package/test/client.test.js +860 -736
  114. package/test/clustering.test.js +100 -100
  115. package/test/commands.test.js +106 -106
  116. package/test/config.test.js +66 -66
  117. package/test/conflict-freeze.test.js +300 -300
  118. package/test/document.test.js +523 -0
  119. package/test/dream-archive-guardrail.test.js +116 -116
  120. package/test/dream-candidate-hybrid.test.js +281 -281
  121. package/test/dream-coverage-degrade.test.js +86 -86
  122. package/test/dream.test.js +1308 -1254
  123. package/test/embedding-ready.test.js +196 -196
  124. package/test/entities.test.js +547 -547
  125. package/test/entity-recall.test.js +179 -0
  126. package/test/epistemic.test.js +298 -298
  127. package/test/fnew-0112.test.js +311 -311
  128. package/test/fnew-03.test.js +422 -422
  129. package/test/graph-api.test.js +175 -175
  130. package/test/heat.test.js +175 -148
  131. package/test/helpers/dream-mock.js +82 -82
  132. package/test/helpers/peer-worker.mjs +25 -25
  133. package/test/helpers/runtime-source.js +60 -60
  134. package/test/helpers/tar-builder.js +80 -80
  135. package/test/hot-memory.test.js +145 -145
  136. package/test/inject-content-cap.test.js +91 -0
  137. package/test/inject-first-round-bm25.test.js +89 -89
  138. package/test/inject-heat.test.js +61 -0
  139. package/test/inject-pools.test.js +170 -0
  140. package/test/inject-recall.test.js +105 -0
  141. package/test/inject-rotation.test.js +161 -161
  142. package/test/inject.test.js +283 -249
  143. package/test/lang.test.js +87 -87
  144. package/test/lib-smoke.test.js +96 -96
  145. package/test/llm-audit.test.js +462 -309
  146. package/test/llm-message-source.test.js +177 -177
  147. package/test/local-embedder.test.js +249 -249
  148. package/test/mcp-stdio.test.js +385 -0
  149. package/test/mirror-dirty.test.js +424 -424
  150. package/test/mirror-edit-digest.test.js +187 -187
  151. package/test/mirror-generation.test.js +499 -499
  152. package/test/mirror.test.js +249 -249
  153. package/test/narrative-bars.test.js +162 -0
  154. package/test/organize.test.js +418 -0
  155. package/test/peer-blockers.test.js +193 -193
  156. package/test/policy-epoch.test.js +259 -259
  157. package/test/quality-filter.test.js +186 -186
  158. package/test/reasoning-effort.test.js +746 -746
  159. package/test/recall-evals.test.js +235 -235
  160. package/test/recall-layer.test.js +326 -326
  161. package/test/recall-stats.test.js +123 -0
  162. package/test/receipt-chain.test.js +451 -451
  163. package/test/reflection.test.js +226 -226
  164. package/test/reindex-backfill.test.js +62 -62
  165. package/test/reranker-single-logit.test.js +73 -73
  166. package/test/reranker.test.js +197 -197
  167. package/test/resident-status.test.js +83 -0
  168. package/test/runtime-adopt.test.js +206 -206
  169. package/test/runtime-cli.test.js +89 -89
  170. package/test/runtime-closure.test.js +193 -193
  171. package/test/runtime-download.test.js +302 -302
  172. package/test/runtime-layout.test.js +163 -163
  173. package/test/runtime-loader.test.js +255 -237
  174. package/test/runtime-manifest.test.js +159 -158
  175. package/test/runtime-model-fetch.test.js +348 -348
  176. package/test/runtime-provision.test.js +101 -101
  177. package/test/runtime-tarball.test.js +81 -81
  178. package/test/runtime-verify.test.js +279 -214
  179. package/test/scope-candidates.test.js +239 -239
  180. package/test/scope-explicit.test.js +452 -452
  181. package/test/scope-retrieval.test.js +197 -197
  182. package/test/scope-storage.test.js +255 -255
  183. package/test/scope-strict.test.js +231 -231
  184. package/test/search-fusion.test.js +90 -90
  185. package/test/semantic.test.js +124 -124
  186. package/test/service-search.test.js +174 -174
  187. package/test/service.test.js +405 -405
  188. package/test/settings.test.js +261 -261
  189. package/test/skip-invalid-restore.test.js +262 -262
  190. package/test/sleep-action-set.test.js +422 -422
  191. package/test/sleep-heat.test.js +126 -125
  192. package/test/sleep-scheduler-rearm.test.js +89 -89
  193. package/test/sleep.test.js +410 -373
  194. package/test/standalone-api.test.js +544 -359
  195. package/test/store-vector-cache.test.js +98 -98
  196. package/test/store.test.js +396 -383
  197. package/test/stress.test.js +209 -209
  198. package/test/summarize.test.js +1288 -575
  199. package/test/tools-dto-schema-matrix.test.js +325 -0
  200. package/test/tools-memory-get-schema.test.js +65 -65
  201. package/test/tools.test.js +374 -341
  202. package/test/updated-at-semantics.test.js +113 -113
  203. package/test/vector-index.test.js +221 -221
  204. package/test/version-check.test.js +152 -152
  205. package/test/webServer-optional.test.js +82 -82
@@ -1,499 +1,499 @@
1
- import test from "node:test";
2
- import assert from "node:assert/strict";
3
- import { mkdtempSync, rmSync, existsSync } from "node:fs";
4
- import { tmpdir } from "node:os";
5
- import { join } from "node:path";
6
- import { EventEmitter } from "node:events";
7
- import { DatabaseSync } from "node:sqlite";
8
- import { createStore } from "../src/store.js";
9
- import { createMirror } from "../src/mirror.js";
10
- import { createService } from "../src/service.js";
11
- import { createApi } from "../src/api.js";
12
- import { createSettings } from "../src/settings.js";
13
-
14
- /**
15
- * v0.3.6 回归测试(audit peer 4 类阻断)。
16
- *
17
- * 覆盖(测试点由 Kimi K2.7 设计):
18
- * A. 崩溃窗口:DB 已提交但 dirty 状态未写(generation > applied 且 dirty=false)→
19
- * recoverMirror 仍能捕获并收敛
20
- * B. markMirrorDirty 自身失败 fail-safe:状态写失败不抛,generation 债务仍在,
21
- * recoverMirror 仍能捕获
22
- * C. 全局状态 / 双 worker 交织:旧 gen 的 markMirrorCleanForGeneration 不能清新故障
23
- * D. 逐 type 部分成功:setTypeStatus 记录/合并/收敛
24
- * E. /health 鉴权 + 脱敏 + fail-closed
25
- * F. 迁移幂等:旧库打开自动加列不丢数据
26
- */
27
-
28
- const MIRROR_ERR = "No space left on device";
29
-
30
- function setup({ dbPath } = {}) {
31
- const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-gen-"));
32
- const mirrorDir = join(dir, "mirror");
33
- const store = createStore(dbPath ?? ":memory:");
34
- const mirror = createMirror(mirrorDir);
35
- const warns = [];
36
- const logger = { warn: (...a) => warns.push(a.join(" ")) };
37
- const service = createService({ store, mirror, config: {}, logger });
38
- return { dir, mirrorDir, store, mirror, service, warns };
39
- }
40
-
41
- function makeSyncMock() {
42
- const state = { mode: "ok", calls: 0 };
43
- state.sync = () => {
44
- state.calls += 1;
45
- if (state.mode === "fail") {
46
- const err = new Error(MIRROR_ERR);
47
- err.code = "ENOSPC";
48
- throw err;
49
- }
50
- };
51
- return state;
52
- }
53
-
54
- class FakeRes extends EventEmitter {
55
- constructor() { super(); this.statusCode = 200; this.body = ""; }
56
- writeHead(code, headers) { this.statusCode = code; this.headers = headers; return this; }
57
- end(text) { this.body = text ?? ""; this.emit("end"); return this; }
58
- }
59
-
60
- function healthRouteSetup(store, service, apiToken = "") {
61
- const settings = createSettings(store.db);
62
- const commands = {
63
- add: (def) => settings.addCommand(def),
64
- remove: (id) => settings.removeCommand(id),
65
- list: () => settings.listCommands()
66
- };
67
- const routes = [];
68
- const ctx = {
69
- webServer: { register(route) { routes.push(route); return () => {}; } }
70
- };
71
- createApi(ctx, service, settings, commands, undefined, undefined, apiToken);
72
- const health = routes.find((r) => r.path === "/api/dsh-mneme/health");
73
- assert.ok(health, "必须注册 /api/dsh-mneme/health");
74
- return health;
75
- }
76
-
77
- // ── C. 全局状态 / 双 worker 交织(store 层 CAS/fence)─────────────────────
78
-
79
- test("V0.3.6-C1: 双 worker 交织——旧 gen 的 markMirrorCleanForGeneration 被 fence 拦截,不能清新故障", () => {
80
- const { dir, store } = setup();
81
- try {
82
- // 初始干净:gen=2 已 applied
83
- store.setMirrorState({ dirty: 0, generation: 2, applied_generation: 2 });
84
- // worker B 随后失败 → 新债务:desired 前进到 3
85
- store.markMirrorDirty("ENOSPC: disk full", "t1");
86
- assert.equal(store.getMirrorState().generation, 3, "失败必须递增 desired generation");
87
-
88
- // worker A(旧 gen=2)晚到的 clean:必须被 fence 拦截
89
- const afterStale = store.markMirrorCleanForGeneration(2, "t2");
90
- assert.equal(afterStale.dirty, true, "旧 gen 的 CAS 必须被 fence 拦截,dirty 不能清");
91
- assert.equal(afterStale.last_error, "ENOSPC: disk full", "新故障的 last_error 必须保留");
92
- assert.equal(afterStale.applied_generation, 2, "applied 最多追到旧 gen=2");
93
- assert.equal(afterStale.generation, 3, "desired 仍是 3");
94
-
95
- // 当前 gen 的 clean 才允许通过
96
- const afterFresh = store.markMirrorCleanForGeneration(3, "t3");
97
- assert.equal(afterFresh.dirty, false, "当前 gen 的 clean 必须通过");
98
- assert.equal(afterFresh.last_error, null);
99
- assert.ok(afterFresh.success_at, "clean 必须更新 success_at");
100
- assert.equal(afterFresh.applied_generation, 3);
101
- } finally {
102
- store.close();
103
- rmSync(dir, { recursive: true, force: true });
104
- }
105
- });
106
-
107
- test("V0.3.6-C2: markMirrorDirty 递增 desired generation,applied_generation 不动", () => {
108
- const { dir, store } = setup();
109
- try {
110
- const s1 = store.markMirrorDirty("boom", "t1");
111
- assert.equal(s1.dirty, true);
112
- assert.equal(s1.generation, 1);
113
- assert.equal(s1.applied_generation, 0, "失败不得误标 applied");
114
- assert.equal(s1.last_error, "boom");
115
- assert.ok(s1.last_attempt, "必须记录 last_attempt");
116
-
117
- const s2 = store.markMirrorDirty("boom2", "t2");
118
- assert.equal(s2.generation, 2, "连续失败必须递增 desired");
119
- assert.equal(s2.applied_generation, 0);
120
- assert.equal(s2.last_error, "boom2");
121
- } finally {
122
- store.close();
123
- rmSync(dir, { recursive: true, force: true });
124
- }
125
- });
126
-
127
- test("V0.3.6-C3: incrementGeneration 每次 +1,不动 dirty/applied", () => {
128
- const { dir, store } = setup();
129
- try {
130
- assert.equal(store.incrementGeneration().generation, 1);
131
- assert.equal(store.incrementGeneration().generation, 2);
132
- assert.equal(store.incrementGeneration().generation, 3);
133
- const state = store.getMirrorState();
134
- assert.equal(state.applied_generation, 0, "increment 不动 applied");
135
- assert.equal(state.dirty, false, "increment 不改 dirty");
136
- } finally {
137
- store.close();
138
- rmSync(dir, { recursive: true, force: true });
139
- }
140
- });
141
-
142
- // ── D. 逐 type 部分成功(store 层 setTypeStatus)──────────────────────────
143
-
144
- test("V0.3.6-D1: setTypeStatus 记录逐 type committed/failed/pending 回执(peer blocker 4)", () => {
145
- const { dir, store } = setup();
146
- try {
147
- // 状态必填且必须合法
148
- assert.throws(() => store.setTypeStatus("project", { applied_gen: 1 }), /status must be one of/, "缺 status 必须抛错");
149
- assert.throws(() => store.setTypeStatus("project", { status: "bogus" }), /status must be one of/, "非法 status 必须抛错");
150
-
151
- const s1 = store.setTypeStatus("project", { status: "failed", last_error: "e1" });
152
- assert.equal(s1.type_status.project.status, "failed");
153
- assert.equal(s1.type_status.project.last_error, "e1");
154
-
155
- const s2 = store.setTypeStatus("project", { status: "committed", applied_gen: 7, last_error: null });
156
- assert.equal(s2.type_status.project.status, "committed", "同 type 状态更新");
157
- assert.equal(s2.type_status.project.applied_gen, 7);
158
-
159
- const s3 = store.setTypeStatus("decision", { status: "pending" });
160
- assert.equal(s3.type_status.decision.status, "pending", "另一 type 独立");
161
- assert.equal(s3.type_status.project.status, "committed", "同 type 不受影响");
162
-
163
- // getTypeStatus 返回同一解析后的 map
164
- assert.deepEqual(store.getTypeStatus(), {
165
- project: { status: "committed", applied_gen: 7, last_error: null },
166
- decision: { status: "pending" }
167
- });
168
- // type_status 持久化为 JSON 文本
169
- const row = store.db.prepare("SELECT type_status FROM mirror_state WHERE id='main'").get();
170
- assert.ok(JSON.parse(row.type_status).project.status === "committed", "type_status 必须以 JSON 落库");
171
- } finally {
172
- store.close();
173
- rmSync(dir, { recursive: true, force: true });
174
- }
175
- });
176
-
177
- // ── A. 崩溃窗口(service 层 recoverMirror)────────────────────────────────
178
-
179
- test("V0.3.6-A1: 崩溃窗口——generation>applied 且 dirty=false(DB 已提交但 dirty 未写)recoverMirror 仍恢复", () => {
180
- const { dir, store, mirror, service } = setup();
181
- try {
182
- const mock = makeSyncMock();
183
- mirror.sync = mock.sync;
184
- // 构造崩溃窗口:COMMIT 后、markMirrorDirty/clean 前进程退出
185
- store.setMirrorState({ dirty: 0, generation: 5, applied_generation: 3 });
186
- assert.equal(store.getMirrorState().dirty, false, "前置:dirty 未写");
187
-
188
- const result = service.recoverMirror();
189
- assert.equal(result.recovered, true, "崩溃窗口也必须判定恢复");
190
- assert.equal(result.error, null);
191
- assert.equal(mock.calls, 1, "恰好重渲染一次后收敛");
192
- const state = store.getMirrorState();
193
- assert.equal(state.dirty, false, "收敛后 dirty 清");
194
- assert.ok(state.generation <= state.applied_generation, "收敛后无未应用债务");
195
- } finally {
196
- store.close();
197
- rmSync(dir, { recursive: true, force: true });
198
- }
199
- });
200
-
201
- test("V0.3.6-A2: 干净且无未应用债务(generation<=applied)不触发 sync", () => {
202
- const { dir, store, mirror, service } = setup();
203
- try {
204
- const mock = makeSyncMock();
205
- mirror.sync = mock.sync;
206
- store.setMirrorState({ dirty: 0, generation: 2, applied_generation: 5 });
207
- const result = service.recoverMirror();
208
- assert.equal(result.recovered, true);
209
- assert.equal(result.error, null);
210
- assert.equal(mock.calls, 0, "无债务不得触发任何 sync");
211
- } finally {
212
- store.close();
213
- rmSync(dir, { recursive: true, force: true });
214
- }
215
- });
216
-
217
- // ── B. markMirrorDirty 自身失败 fail-safe ─────────────────────────────────
218
-
219
- test("V0.3.6-B1: markMirrorDirty 自身写失败——syncMirror 不抛、generation 债务仍在、recoverMirror 仍能捕获", () => {
220
- const { dir, store, mirror, service, warns } = setup();
221
- try {
222
- const mock = makeSyncMock();
223
- mirror.sync = mock.sync;
224
- mock.mode = "fail";
225
-
226
- // 模拟状态写入挂掉:markMirrorDirty 抛错
227
- const original = store.markMirrorDirty.bind(store);
228
- store.markMirrorDirty = () => { throw new Error("db closed"); };
229
- try {
230
- assert.doesNotThrow(
231
- () => service.saveWithDedupe({ type: "project", title: "fail-safe", content: "x", importance: 3 }),
232
- "markMirrorDirty 失败不得让 syncMirror/saveWithDedupe 向外抛"
233
- );
234
- assert.ok(warns.some((w) => w.includes("markMirrorDirty failed")), "必须走 logger.warn 记录");
235
- const state = store.getMirrorState();
236
- assert.equal(state.dirty, false, "markMirrorDirty 写失败时 dirty 落不了库");
237
- assert.ok(state.generation > state.applied_generation,
238
- "incrementGeneration 的 generation 债务仍在,是 durable 的恢复信号");
239
- assert.equal(store.count(), 1, "业务写不受状态写失败影响");
240
- } finally {
241
- store.markMirrorDirty = original;
242
- }
243
-
244
- // 恢复写能力后,recoverMirror 仅凭 generation>applied 也能捕获并收敛
245
- mock.mode = "ok";
246
- const result = service.recoverMirror();
247
- assert.equal(result.recovered, true, "generation 债务必须能被 recover 捕获");
248
- const finalState = store.getMirrorState();
249
- assert.equal(finalState.dirty, false);
250
- assert.ok(finalState.generation <= finalState.applied_generation, "收敛后无债务");
251
- } finally {
252
- store.close();
253
- rmSync(dir, { recursive: true, force: true });
254
- }
255
- });
256
-
257
- // ── D. 逐 type 部分成功(service 层 type_status 生命周期)─────────────────
258
-
259
- test("V0.3.6-D2: syncMirror 成功时逐 type 记录 committed(applied_gen=gen)", () => {
260
- const { dir, store, mirror, service } = setup();
261
- try {
262
- const mock = makeSyncMock();
263
- mirror.sync = mock.sync;
264
- service.saveWithDedupe({ type: "project", title: "P", content: "x", importance: 3 });
265
- const ts = store.getTypeStatus();
266
- assert.ok(ts.project, "覆盖的 type 必须记录 type_status");
267
- assert.equal(ts.project.status, "committed");
268
- assert.equal(ts.project.last_error, null);
269
- assert.equal(ts.project.applied_gen, store.getMirrorState().applied_generation,
270
- "type_status.applied_gen 与全局 applied 一致");
271
- } finally {
272
- store.close();
273
- rmSync(dir, { recursive: true, force: true });
274
- }
275
- });
276
-
277
- test("V0.3.6-D3: syncMirror 失败时逐 type 记录 failed,recoverMirror 后追到最新 applied", () => {
278
- const { dir, store, mirror, service } = setup();
279
- try {
280
- const mock = makeSyncMock();
281
- mirror.sync = mock.sync;
282
- // 第一次成功建立 committed type_status
283
- mock.mode = "ok";
284
- service.saveWithDedupe({ type: "project", title: "P", content: "x", importance: 3 });
285
- assert.equal(store.getTypeStatus().project.status, "committed");
286
-
287
- // 模拟一次"债务轮次":incrementGeneration 但未成功 applied(崩溃在 type_status 层面)
288
- store.incrementGeneration();
289
- const state = store.getMirrorState();
290
- assert.ok(state.generation > state.applied_generation, "前置:存在未应用债务");
291
-
292
- const result = service.recoverMirror();
293
- assert.equal(result.recovered, true);
294
- const finalState = store.getMirrorState();
295
- assert.ok(finalState.generation <= finalState.applied_generation);
296
- const ts = store.getTypeStatus();
297
- assert.equal(ts.project.applied_gen, finalState.applied_generation,
298
- "type_status 必须追到最新 applied(部分成功债务表达并收敛)");
299
- assert.equal(ts.project.status, "committed");
300
- } finally {
301
- store.close();
302
- rmSync(dir, { recursive: true, force: true });
303
- }
304
- });
305
-
306
- // ── E. /health 鉴权 + 脱敏 + fail-closed ──────────────────────────────────
307
-
308
- test("V0.3.6-E1: apiToken 设置后未鉴权访问 /health 返回 401,正确 token 才放行", () => {
309
- const { dir, store, mirror, service } = setup();
310
- try {
311
- const TOKEN = "secret-token-123";
312
- const health = healthRouteSetup(store, service, TOKEN);
313
- const res401 = new FakeRes();
314
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, res401);
315
- assert.equal(res401.statusCode, 401, "无 token 必须 401");
316
- assert.deepEqual(JSON.parse(res401.body), { error: "unauthorized" }, "不得回显任何内部信息");
317
-
318
- const resWrong = new FakeRes();
319
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: { authorization: "Bearer wrong-token" } }, resWrong);
320
- assert.equal(resWrong.statusCode, 401, "错误 token 必须 401");
321
-
322
- const resOk = new FakeRes();
323
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: { authorization: `Bearer ${TOKEN}` } }, resOk);
324
- assert.equal(resOk.statusCode, 200, "正确 Bearer token 必须放行");
325
-
326
- const resHeader = new FakeRes();
327
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: { "x-dsh-mneme-token": TOKEN } }, resHeader);
328
- assert.equal(resHeader.statusCode, 200, "x-dsh-mneme-token 也必须放行");
329
- } finally {
330
- store.close();
331
- rmSync(dir, { recursive: true, force: true });
332
- }
333
- });
334
-
335
- test("V0.3.6-E2: 未配置 apiToken 时 /health 免鉴权(向后兼容)", () => {
336
- const { dir, store, mirror, service } = setup();
337
- try {
338
- const health = healthRouteSetup(store, service, "");
339
- const res = new FakeRes();
340
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, res);
341
- assert.equal(res.statusCode, 200, "未配置 token 必须免鉴权");
342
- } finally {
343
- store.close();
344
- rmSync(dir, { recursive: true, force: true });
345
- }
346
- });
347
-
348
- test("V0.3.6-E3: /health 只返回脱敏错误码(no-space/permission/sync-failed),不回显原始 last_error", () => {
349
- const { dir, store, mirror, service } = setup();
350
- try {
351
- const health = healthRouteSetup(store, service, "");
352
- const cases = [
353
- { error: "No space left on device", expect: "no-space" },
354
- { error: "EACCES: permission denied", expect: "permission" },
355
- { error: "sync exploded with internals", expect: "sync-failed" }
356
- ];
357
- for (const { error, expect } of cases) {
358
- store.setMirrorState({ dirty: 1, last_error: error, last_attempt: "t" });
359
- const res = new FakeRes();
360
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, res);
361
- assert.equal(res.statusCode, 200);
362
- const body = JSON.parse(res.body);
363
- assert.equal(body.mirror.dirty, true);
364
- assert.equal(body.mirror.status, "degraded");
365
- assert.equal(body.mirror.last_error, expect, `${error} 必须映射为 ${expect}`);
366
- const raw = JSON.stringify(body);
367
- assert.ok(!raw.includes(error.split(":")[0]), `原始错误不得出现在响应: ${error}`);
368
- }
369
- } finally {
370
- store.close();
371
- rmSync(dir, { recursive: true, force: true });
372
- }
373
- });
374
-
375
- test("V0.3.6-E4: /health 读失败 fail-closed → status:unknown、dirty:null,不误报 clean", () => {
376
- const { dir, store, mirror, service } = setup();
377
- try {
378
- const health = healthRouteSetup(store, service, "");
379
- const original = service.getMirrorHealth.bind(service);
380
-
381
- // 抛错 → fail-closed unknown
382
- service.getMirrorHealth = () => { const e = new Error("SQLITE_CORRUPT"); throw e; };
383
- const resThrow = new FakeRes();
384
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, resThrow);
385
- assert.equal(resThrow.statusCode, 200);
386
- const bodyThrow = JSON.parse(resThrow.body);
387
- assert.equal(bodyThrow.mirror.status, "unknown", "读失败必须 status:unknown");
388
- assert.equal(bodyThrow.mirror.dirty, null, "读失败必须 dirty:null(不得谎报 clean)");
389
- assert.equal(bodyThrow.mirror.last_error, null);
390
- assert.ok(!JSON.stringify(bodyThrow).includes("SQLITE_CORRUPT"), "异常详情不得泄漏");
391
-
392
- // 返回 null → fail-closed unknown
393
- service.getMirrorHealth = () => null;
394
- const resNull = new FakeRes();
395
- health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, resNull);
396
- assert.equal(JSON.parse(resNull.body).mirror.status, "unknown");
397
- assert.equal(JSON.parse(resNull.body).mirror.dirty, null);
398
- service.getMirrorHealth = original;
399
- } finally {
400
- store.close();
401
- rmSync(dir, { recursive: true, force: true });
402
- }
403
- });
404
-
405
- // ── F. 迁移幂等 ───────────────────────────────────────────────────────────
406
-
407
- test("V0.3.6-F1: 旧库(v0.3.5 5 列)打开自动 ALTER 加 3 列,不丢数据,重复打开幂等", () => {
408
- const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-gen-"));
409
- const dbPath = join(dir, "memory.db");
410
- try {
411
- // 用裸 DatabaseSync 构造 v0.3.5 时代的旧库(mirror_state 只有 5 列)
412
- const old = new DatabaseSync(dbPath);
413
- old.exec(`
414
- CREATE TABLE mirror_state (
415
- id TEXT PRIMARY KEY,
416
- dirty INTEGER NOT NULL DEFAULT 0,
417
- last_error TEXT,
418
- last_attempt TEXT,
419
- success_at TEXT
420
- );
421
- INSERT INTO mirror_state (id, dirty, last_error, last_attempt) VALUES ('main', 1, 'old error', 'old attempt');
422
- CREATE TABLE memories (
423
- id TEXT PRIMARY KEY, type TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL,
424
- tags TEXT NOT NULL DEFAULT '[]', importance INTEGER NOT NULL DEFAULT 3, forgotten INTEGER NOT NULL DEFAULT 0,
425
- archived INTEGER NOT NULL DEFAULT 0, source TEXT, embedding TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
426
- );
427
- INSERT INTO memories (id, type, title, content, tags, importance, forgotten, archived, created_at, updated_at)
428
- VALUES ('m1', 'project', '老数据', '必须保留', '[]', 3, 0, 0, 't', 't');
429
- `);
430
- old.close();
431
-
432
- let store = createStore(dbPath);
433
- const cols = store.db.prepare("PRAGMA table_info(mirror_state)").all().map((c) => c.name);
434
- assert.deepEqual(cols.sort(),
435
- ["applied_generation", "dirty", "generation", "id", "last_attempt", "last_error", "success_at", "type_status"],
436
- "旧库打开必须自动补齐 3 列");
437
- const state = store.getMirrorState();
438
- assert.equal(state.dirty, true, "旧 dirty 数据必须保留");
439
- assert.equal(state.last_error, "old error", "旧 last_error 必须保留");
440
- assert.equal(state.last_attempt, "old attempt", "旧 last_attempt 必须保留");
441
- assert.equal(state.generation, 0, "新列默认 0");
442
- assert.equal(state.applied_generation, 0);
443
- assert.deepEqual(state.type_status, {}, "新列默认空对象");
444
- assert.equal(store.count(), 1, "memories 数据必须保留");
445
- assert.equal(store.getById("m1").title, "老数据");
446
-
447
- // 重复打开幂等:不抛、列不变、数据不变
448
- store.close();
449
- store = createStore(dbPath);
450
- const cols2 = store.db.prepare("PRAGMA table_info(mirror_state)").all().map((c) => c.name);
451
- assert.deepEqual(cols2.sort(),
452
- ["applied_generation", "dirty", "generation", "id", "last_attempt", "last_error", "success_at", "type_status"],
453
- "重复打开不重复加列");
454
- assert.equal(store.getMirrorState().last_error, "old error", "重复打开不丢数据");
455
- assert.equal(store.count(), 1);
456
-
457
- // 新方法在迁移后的库上正常工作
458
- const clean = store.markMirrorCleanForGeneration(0, "t");
459
- assert.equal(clean.dirty, false, "迁移库上 fence clean 正常工作");
460
- store.setTypeStatus("project", { status: "failed", applied_gen: 0 });
461
- assert.equal(store.getTypeStatus().project.status, "failed", "迁移库上 setTypeStatus 正常工作");
462
- store.close();
463
- } finally {
464
- rmSync(dir, { recursive: true, force: true });
465
- }
466
- });
467
-
468
- test("v0.3.9-D: mirror 逐 type 物理终态——兄弟 type 失败不误标已提交 type", () => {
469
- const { dir, store, mirror, service } = setup();
470
- try {
471
- // 注入逐 type 故障:project 写成功、decision 抛错(模拟 EISDIR),其余 type 正常。
472
- // 对应审计 D:project 文件已物理提交、decision 失败,状态必须逐 type 记录——
473
- // 不能像旧逻辑那样整体批量标 failed。
474
- const original = mirror.sync.bind(mirror);
475
- mirror.sync = (memories) => {
476
- const results = original(memories);
477
- results.decision = { ok: false, error: "EISDIR: decision.md is a directory" };
478
- return results;
479
- };
480
- // project 与 decision 都有真实记忆 → 触发逐 type 渲染
481
- store.save({ id: "mem-project", type: "project", title: "已提交", content: "物理写入", importance: 3, tags: [] });
482
- store.save({ id: "mem-decision", type: "decision", title: "写失败", content: "此 type 失败", importance: 3, tags: [] });
483
- // 直接调 service 内部 syncMirror(通过一次写触发)
484
- service.saveWithDedupe({ type: "project", title: "触发", content: "sync", importance: 3 });
485
-
486
- const ts = store.getTypeStatus();
487
- // project 物理提交 → 必须 committed,不能被 decision 失败拖成 failed
488
- assert.equal(ts.project.status, "committed", "物理已提交的 type 必须标记 committed");
489
- assert.equal(ts.decision.status, "failed", "失败的 type 必须标记 failed");
490
- // 部分失败 = 未完全收敛 → dirty 必须持久
491
- assert.equal(store.getMirrorState().dirty, true, "部分 type 失败必须持久 dirty");
492
- // 镜像里 project 文件真实存在(物理终态已落地,不是整体失败)
493
- const projectFile = mirror.filePath("project");
494
- assert.ok(existsSync(projectFile), "project 镜像文件必须已物理写入");
495
- } finally {
496
- store.close();
497
- rmSync(dir, { recursive: true, force: true });
498
- }
499
- });
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdtempSync, rmSync, existsSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { EventEmitter } from "node:events";
7
+ import { DatabaseSync } from "node:sqlite";
8
+ import { createStore } from "../src/store.js";
9
+ import { createMirror } from "../src/mirror.js";
10
+ import { createService } from "../src/service.js";
11
+ import { createApi } from "../src/api.js";
12
+ import { createSettings } from "../src/settings.js";
13
+
14
+ /**
15
+ * v0.3.6 回归测试(audit peer 4 类阻断)。
16
+ *
17
+ * 覆盖(测试点由 Kimi K2.7 设计):
18
+ * A. 崩溃窗口:DB 已提交但 dirty 状态未写(generation > applied 且 dirty=false)→
19
+ * recoverMirror 仍能捕获并收敛
20
+ * B. markMirrorDirty 自身失败 fail-safe:状态写失败不抛,generation 债务仍在,
21
+ * recoverMirror 仍能捕获
22
+ * C. 全局状态 / 双 worker 交织:旧 gen 的 markMirrorCleanForGeneration 不能清新故障
23
+ * D. 逐 type 部分成功:setTypeStatus 记录/合并/收敛
24
+ * E. /health 鉴权 + 脱敏 + fail-closed
25
+ * F. 迁移幂等:旧库打开自动加列不丢数据
26
+ */
27
+
28
+ const MIRROR_ERR = "No space left on device";
29
+
30
+ function setup({ dbPath } = {}) {
31
+ const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-gen-"));
32
+ const mirrorDir = join(dir, "mirror");
33
+ const store = createStore(dbPath ?? ":memory:");
34
+ const mirror = createMirror(mirrorDir);
35
+ const warns = [];
36
+ const logger = { warn: (...a) => warns.push(a.join(" ")) };
37
+ const service = createService({ store, mirror, config: {}, logger });
38
+ return { dir, mirrorDir, store, mirror, service, warns };
39
+ }
40
+
41
+ function makeSyncMock() {
42
+ const state = { mode: "ok", calls: 0 };
43
+ state.sync = () => {
44
+ state.calls += 1;
45
+ if (state.mode === "fail") {
46
+ const err = new Error(MIRROR_ERR);
47
+ err.code = "ENOSPC";
48
+ throw err;
49
+ }
50
+ };
51
+ return state;
52
+ }
53
+
54
+ class FakeRes extends EventEmitter {
55
+ constructor() { super(); this.statusCode = 200; this.body = ""; }
56
+ writeHead(code, headers) { this.statusCode = code; this.headers = headers; return this; }
57
+ end(text) { this.body = text ?? ""; this.emit("end"); return this; }
58
+ }
59
+
60
+ function healthRouteSetup(store, service, apiToken = "") {
61
+ const settings = createSettings(store.db);
62
+ const commands = {
63
+ add: (def) => settings.addCommand(def),
64
+ remove: (id) => settings.removeCommand(id),
65
+ list: () => settings.listCommands()
66
+ };
67
+ const routes = [];
68
+ const ctx = {
69
+ webServer: { register(route) { routes.push(route); return () => {}; } }
70
+ };
71
+ createApi(ctx, service, settings, commands, undefined, undefined, apiToken);
72
+ const health = routes.find((r) => r.path === "/api/dsh-mneme/health");
73
+ assert.ok(health, "必须注册 /api/dsh-mneme/health");
74
+ return health;
75
+ }
76
+
77
+ // ── C. 全局状态 / 双 worker 交织(store 层 CAS/fence)─────────────────────
78
+
79
+ test("V0.3.6-C1: 双 worker 交织——旧 gen 的 markMirrorCleanForGeneration 被 fence 拦截,不能清新故障", () => {
80
+ const { dir, store } = setup();
81
+ try {
82
+ // 初始干净:gen=2 已 applied
83
+ store.setMirrorState({ dirty: 0, generation: 2, applied_generation: 2 });
84
+ // worker B 随后失败 → 新债务:desired 前进到 3
85
+ store.markMirrorDirty("ENOSPC: disk full", "t1");
86
+ assert.equal(store.getMirrorState().generation, 3, "失败必须递增 desired generation");
87
+
88
+ // worker A(旧 gen=2)晚到的 clean:必须被 fence 拦截
89
+ const afterStale = store.markMirrorCleanForGeneration(2, "t2");
90
+ assert.equal(afterStale.dirty, true, "旧 gen 的 CAS 必须被 fence 拦截,dirty 不能清");
91
+ assert.equal(afterStale.last_error, "ENOSPC: disk full", "新故障的 last_error 必须保留");
92
+ assert.equal(afterStale.applied_generation, 2, "applied 最多追到旧 gen=2");
93
+ assert.equal(afterStale.generation, 3, "desired 仍是 3");
94
+
95
+ // 当前 gen 的 clean 才允许通过
96
+ const afterFresh = store.markMirrorCleanForGeneration(3, "t3");
97
+ assert.equal(afterFresh.dirty, false, "当前 gen 的 clean 必须通过");
98
+ assert.equal(afterFresh.last_error, null);
99
+ assert.ok(afterFresh.success_at, "clean 必须更新 success_at");
100
+ assert.equal(afterFresh.applied_generation, 3);
101
+ } finally {
102
+ store.close();
103
+ rmSync(dir, { recursive: true, force: true });
104
+ }
105
+ });
106
+
107
+ test("V0.3.6-C2: markMirrorDirty 递增 desired generation,applied_generation 不动", () => {
108
+ const { dir, store } = setup();
109
+ try {
110
+ const s1 = store.markMirrorDirty("boom", "t1");
111
+ assert.equal(s1.dirty, true);
112
+ assert.equal(s1.generation, 1);
113
+ assert.equal(s1.applied_generation, 0, "失败不得误标 applied");
114
+ assert.equal(s1.last_error, "boom");
115
+ assert.ok(s1.last_attempt, "必须记录 last_attempt");
116
+
117
+ const s2 = store.markMirrorDirty("boom2", "t2");
118
+ assert.equal(s2.generation, 2, "连续失败必须递增 desired");
119
+ assert.equal(s2.applied_generation, 0);
120
+ assert.equal(s2.last_error, "boom2");
121
+ } finally {
122
+ store.close();
123
+ rmSync(dir, { recursive: true, force: true });
124
+ }
125
+ });
126
+
127
+ test("V0.3.6-C3: incrementGeneration 每次 +1,不动 dirty/applied", () => {
128
+ const { dir, store } = setup();
129
+ try {
130
+ assert.equal(store.incrementGeneration().generation, 1);
131
+ assert.equal(store.incrementGeneration().generation, 2);
132
+ assert.equal(store.incrementGeneration().generation, 3);
133
+ const state = store.getMirrorState();
134
+ assert.equal(state.applied_generation, 0, "increment 不动 applied");
135
+ assert.equal(state.dirty, false, "increment 不改 dirty");
136
+ } finally {
137
+ store.close();
138
+ rmSync(dir, { recursive: true, force: true });
139
+ }
140
+ });
141
+
142
+ // ── D. 逐 type 部分成功(store 层 setTypeStatus)──────────────────────────
143
+
144
+ test("V0.3.6-D1: setTypeStatus 记录逐 type committed/failed/pending 回执(peer blocker 4)", () => {
145
+ const { dir, store } = setup();
146
+ try {
147
+ // 状态必填且必须合法
148
+ assert.throws(() => store.setTypeStatus("project", { applied_gen: 1 }), /status must be one of/, "缺 status 必须抛错");
149
+ assert.throws(() => store.setTypeStatus("project", { status: "bogus" }), /status must be one of/, "非法 status 必须抛错");
150
+
151
+ const s1 = store.setTypeStatus("project", { status: "failed", last_error: "e1" });
152
+ assert.equal(s1.type_status.project.status, "failed");
153
+ assert.equal(s1.type_status.project.last_error, "e1");
154
+
155
+ const s2 = store.setTypeStatus("project", { status: "committed", applied_gen: 7, last_error: null });
156
+ assert.equal(s2.type_status.project.status, "committed", "同 type 状态更新");
157
+ assert.equal(s2.type_status.project.applied_gen, 7);
158
+
159
+ const s3 = store.setTypeStatus("decision", { status: "pending" });
160
+ assert.equal(s3.type_status.decision.status, "pending", "另一 type 独立");
161
+ assert.equal(s3.type_status.project.status, "committed", "同 type 不受影响");
162
+
163
+ // getTypeStatus 返回同一解析后的 map
164
+ assert.deepEqual(store.getTypeStatus(), {
165
+ project: { status: "committed", applied_gen: 7, last_error: null },
166
+ decision: { status: "pending" }
167
+ });
168
+ // type_status 持久化为 JSON 文本
169
+ const row = store.db.prepare("SELECT type_status FROM mirror_state WHERE id='main'").get();
170
+ assert.ok(JSON.parse(row.type_status).project.status === "committed", "type_status 必须以 JSON 落库");
171
+ } finally {
172
+ store.close();
173
+ rmSync(dir, { recursive: true, force: true });
174
+ }
175
+ });
176
+
177
+ // ── A. 崩溃窗口(service 层 recoverMirror)────────────────────────────────
178
+
179
+ test("V0.3.6-A1: 崩溃窗口——generation>applied 且 dirty=false(DB 已提交但 dirty 未写)recoverMirror 仍恢复", () => {
180
+ const { dir, store, mirror, service } = setup();
181
+ try {
182
+ const mock = makeSyncMock();
183
+ mirror.sync = mock.sync;
184
+ // 构造崩溃窗口:COMMIT 后、markMirrorDirty/clean 前进程退出
185
+ store.setMirrorState({ dirty: 0, generation: 5, applied_generation: 3 });
186
+ assert.equal(store.getMirrorState().dirty, false, "前置:dirty 未写");
187
+
188
+ const result = service.recoverMirror();
189
+ assert.equal(result.recovered, true, "崩溃窗口也必须判定恢复");
190
+ assert.equal(result.error, null);
191
+ assert.equal(mock.calls, 1, "恰好重渲染一次后收敛");
192
+ const state = store.getMirrorState();
193
+ assert.equal(state.dirty, false, "收敛后 dirty 清");
194
+ assert.ok(state.generation <= state.applied_generation, "收敛后无未应用债务");
195
+ } finally {
196
+ store.close();
197
+ rmSync(dir, { recursive: true, force: true });
198
+ }
199
+ });
200
+
201
+ test("V0.3.6-A2: 干净且无未应用债务(generation<=applied)不触发 sync", () => {
202
+ const { dir, store, mirror, service } = setup();
203
+ try {
204
+ const mock = makeSyncMock();
205
+ mirror.sync = mock.sync;
206
+ store.setMirrorState({ dirty: 0, generation: 2, applied_generation: 5 });
207
+ const result = service.recoverMirror();
208
+ assert.equal(result.recovered, true);
209
+ assert.equal(result.error, null);
210
+ assert.equal(mock.calls, 0, "无债务不得触发任何 sync");
211
+ } finally {
212
+ store.close();
213
+ rmSync(dir, { recursive: true, force: true });
214
+ }
215
+ });
216
+
217
+ // ── B. markMirrorDirty 自身失败 fail-safe ─────────────────────────────────
218
+
219
+ test("V0.3.6-B1: markMirrorDirty 自身写失败——syncMirror 不抛、generation 债务仍在、recoverMirror 仍能捕获", () => {
220
+ const { dir, store, mirror, service, warns } = setup();
221
+ try {
222
+ const mock = makeSyncMock();
223
+ mirror.sync = mock.sync;
224
+ mock.mode = "fail";
225
+
226
+ // 模拟状态写入挂掉:markMirrorDirty 抛错
227
+ const original = store.markMirrorDirty.bind(store);
228
+ store.markMirrorDirty = () => { throw new Error("db closed"); };
229
+ try {
230
+ assert.doesNotThrow(
231
+ () => service.saveWithDedupe({ type: "project", title: "fail-safe", content: "x", importance: 3 }),
232
+ "markMirrorDirty 失败不得让 syncMirror/saveWithDedupe 向外抛"
233
+ );
234
+ assert.ok(warns.some((w) => w.includes("markMirrorDirty failed")), "必须走 logger.warn 记录");
235
+ const state = store.getMirrorState();
236
+ assert.equal(state.dirty, false, "markMirrorDirty 写失败时 dirty 落不了库");
237
+ assert.ok(state.generation > state.applied_generation,
238
+ "incrementGeneration 的 generation 债务仍在,是 durable 的恢复信号");
239
+ assert.equal(store.count(), 1, "业务写不受状态写失败影响");
240
+ } finally {
241
+ store.markMirrorDirty = original;
242
+ }
243
+
244
+ // 恢复写能力后,recoverMirror 仅凭 generation>applied 也能捕获并收敛
245
+ mock.mode = "ok";
246
+ const result = service.recoverMirror();
247
+ assert.equal(result.recovered, true, "generation 债务必须能被 recover 捕获");
248
+ const finalState = store.getMirrorState();
249
+ assert.equal(finalState.dirty, false);
250
+ assert.ok(finalState.generation <= finalState.applied_generation, "收敛后无债务");
251
+ } finally {
252
+ store.close();
253
+ rmSync(dir, { recursive: true, force: true });
254
+ }
255
+ });
256
+
257
+ // ── D. 逐 type 部分成功(service 层 type_status 生命周期)─────────────────
258
+
259
+ test("V0.3.6-D2: syncMirror 成功时逐 type 记录 committed(applied_gen=gen)", () => {
260
+ const { dir, store, mirror, service } = setup();
261
+ try {
262
+ const mock = makeSyncMock();
263
+ mirror.sync = mock.sync;
264
+ service.saveWithDedupe({ type: "project", title: "P", content: "x", importance: 3 });
265
+ const ts = store.getTypeStatus();
266
+ assert.ok(ts.project, "覆盖的 type 必须记录 type_status");
267
+ assert.equal(ts.project.status, "committed");
268
+ assert.equal(ts.project.last_error, null);
269
+ assert.equal(ts.project.applied_gen, store.getMirrorState().applied_generation,
270
+ "type_status.applied_gen 与全局 applied 一致");
271
+ } finally {
272
+ store.close();
273
+ rmSync(dir, { recursive: true, force: true });
274
+ }
275
+ });
276
+
277
+ test("V0.3.6-D3: syncMirror 失败时逐 type 记录 failed,recoverMirror 后追到最新 applied", () => {
278
+ const { dir, store, mirror, service } = setup();
279
+ try {
280
+ const mock = makeSyncMock();
281
+ mirror.sync = mock.sync;
282
+ // 第一次成功建立 committed type_status
283
+ mock.mode = "ok";
284
+ service.saveWithDedupe({ type: "project", title: "P", content: "x", importance: 3 });
285
+ assert.equal(store.getTypeStatus().project.status, "committed");
286
+
287
+ // 模拟一次"债务轮次":incrementGeneration 但未成功 applied(崩溃在 type_status 层面)
288
+ store.incrementGeneration();
289
+ const state = store.getMirrorState();
290
+ assert.ok(state.generation > state.applied_generation, "前置:存在未应用债务");
291
+
292
+ const result = service.recoverMirror();
293
+ assert.equal(result.recovered, true);
294
+ const finalState = store.getMirrorState();
295
+ assert.ok(finalState.generation <= finalState.applied_generation);
296
+ const ts = store.getTypeStatus();
297
+ assert.equal(ts.project.applied_gen, finalState.applied_generation,
298
+ "type_status 必须追到最新 applied(部分成功债务表达并收敛)");
299
+ assert.equal(ts.project.status, "committed");
300
+ } finally {
301
+ store.close();
302
+ rmSync(dir, { recursive: true, force: true });
303
+ }
304
+ });
305
+
306
+ // ── E. /health 鉴权 + 脱敏 + fail-closed ──────────────────────────────────
307
+
308
+ test("V0.3.6-E1: apiToken 设置后未鉴权访问 /health 返回 401,正确 token 才放行", () => {
309
+ const { dir, store, mirror, service } = setup();
310
+ try {
311
+ const TOKEN = "secret-token-123";
312
+ const health = healthRouteSetup(store, service, TOKEN);
313
+ const res401 = new FakeRes();
314
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, res401);
315
+ assert.equal(res401.statusCode, 401, "无 token 必须 401");
316
+ assert.deepEqual(JSON.parse(res401.body), { error: "unauthorized" }, "不得回显任何内部信息");
317
+
318
+ const resWrong = new FakeRes();
319
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: { authorization: "Bearer wrong-token" } }, resWrong);
320
+ assert.equal(resWrong.statusCode, 401, "错误 token 必须 401");
321
+
322
+ const resOk = new FakeRes();
323
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: { authorization: `Bearer ${TOKEN}` } }, resOk);
324
+ assert.equal(resOk.statusCode, 200, "正确 Bearer token 必须放行");
325
+
326
+ const resHeader = new FakeRes();
327
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: { "x-dsh-mneme-token": TOKEN } }, resHeader);
328
+ assert.equal(resHeader.statusCode, 200, "x-dsh-mneme-token 也必须放行");
329
+ } finally {
330
+ store.close();
331
+ rmSync(dir, { recursive: true, force: true });
332
+ }
333
+ });
334
+
335
+ test("V0.3.6-E2: 未配置 apiToken 时 /health 免鉴权(向后兼容)", () => {
336
+ const { dir, store, mirror, service } = setup();
337
+ try {
338
+ const health = healthRouteSetup(store, service, "");
339
+ const res = new FakeRes();
340
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, res);
341
+ assert.equal(res.statusCode, 200, "未配置 token 必须免鉴权");
342
+ } finally {
343
+ store.close();
344
+ rmSync(dir, { recursive: true, force: true });
345
+ }
346
+ });
347
+
348
+ test("V0.3.6-E3: /health 只返回脱敏错误码(no-space/permission/sync-failed),不回显原始 last_error", () => {
349
+ const { dir, store, mirror, service } = setup();
350
+ try {
351
+ const health = healthRouteSetup(store, service, "");
352
+ const cases = [
353
+ { error: "No space left on device", expect: "no-space" },
354
+ { error: "EACCES: permission denied", expect: "permission" },
355
+ { error: "sync exploded with internals", expect: "sync-failed" }
356
+ ];
357
+ for (const { error, expect } of cases) {
358
+ store.setMirrorState({ dirty: 1, last_error: error, last_attempt: "t" });
359
+ const res = new FakeRes();
360
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, res);
361
+ assert.equal(res.statusCode, 200);
362
+ const body = JSON.parse(res.body);
363
+ assert.equal(body.mirror.dirty, true);
364
+ assert.equal(body.mirror.status, "degraded");
365
+ assert.equal(body.mirror.last_error, expect, `${error} 必须映射为 ${expect}`);
366
+ const raw = JSON.stringify(body);
367
+ assert.ok(!raw.includes(error.split(":")[0]), `原始错误不得出现在响应: ${error}`);
368
+ }
369
+ } finally {
370
+ store.close();
371
+ rmSync(dir, { recursive: true, force: true });
372
+ }
373
+ });
374
+
375
+ test("V0.3.6-E4: /health 读失败 fail-closed → status:unknown、dirty:null,不误报 clean", () => {
376
+ const { dir, store, mirror, service } = setup();
377
+ try {
378
+ const health = healthRouteSetup(store, service, "");
379
+ const original = service.getMirrorHealth.bind(service);
380
+
381
+ // 抛错 → fail-closed unknown
382
+ service.getMirrorHealth = () => { const e = new Error("SQLITE_CORRUPT"); throw e; };
383
+ const resThrow = new FakeRes();
384
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, resThrow);
385
+ assert.equal(resThrow.statusCode, 200);
386
+ const bodyThrow = JSON.parse(resThrow.body);
387
+ assert.equal(bodyThrow.mirror.status, "unknown", "读失败必须 status:unknown");
388
+ assert.equal(bodyThrow.mirror.dirty, null, "读失败必须 dirty:null(不得谎报 clean)");
389
+ assert.equal(bodyThrow.mirror.last_error, null);
390
+ assert.ok(!JSON.stringify(bodyThrow).includes("SQLITE_CORRUPT"), "异常详情不得泄漏");
391
+
392
+ // 返回 null → fail-closed unknown
393
+ service.getMirrorHealth = () => null;
394
+ const resNull = new FakeRes();
395
+ health.handler({ url: "/api/dsh-mneme/health", method: "GET", headers: {} }, resNull);
396
+ assert.equal(JSON.parse(resNull.body).mirror.status, "unknown");
397
+ assert.equal(JSON.parse(resNull.body).mirror.dirty, null);
398
+ service.getMirrorHealth = original;
399
+ } finally {
400
+ store.close();
401
+ rmSync(dir, { recursive: true, force: true });
402
+ }
403
+ });
404
+
405
+ // ── F. 迁移幂等 ───────────────────────────────────────────────────────────
406
+
407
+ test("V0.3.6-F1: 旧库(v0.3.5 5 列)打开自动 ALTER 加 3 列,不丢数据,重复打开幂等", () => {
408
+ const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-gen-"));
409
+ const dbPath = join(dir, "memory.db");
410
+ try {
411
+ // 用裸 DatabaseSync 构造 v0.3.5 时代的旧库(mirror_state 只有 5 列)
412
+ const old = new DatabaseSync(dbPath);
413
+ old.exec(`
414
+ CREATE TABLE mirror_state (
415
+ id TEXT PRIMARY KEY,
416
+ dirty INTEGER NOT NULL DEFAULT 0,
417
+ last_error TEXT,
418
+ last_attempt TEXT,
419
+ success_at TEXT
420
+ );
421
+ INSERT INTO mirror_state (id, dirty, last_error, last_attempt) VALUES ('main', 1, 'old error', 'old attempt');
422
+ CREATE TABLE memories (
423
+ id TEXT PRIMARY KEY, type TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL,
424
+ tags TEXT NOT NULL DEFAULT '[]', importance INTEGER NOT NULL DEFAULT 3, forgotten INTEGER NOT NULL DEFAULT 0,
425
+ archived INTEGER NOT NULL DEFAULT 0, source TEXT, embedding TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
426
+ );
427
+ INSERT INTO memories (id, type, title, content, tags, importance, forgotten, archived, created_at, updated_at)
428
+ VALUES ('m1', 'project', '老数据', '必须保留', '[]', 3, 0, 0, 't', 't');
429
+ `);
430
+ old.close();
431
+
432
+ let store = createStore(dbPath);
433
+ const cols = store.db.prepare("PRAGMA table_info(mirror_state)").all().map((c) => c.name);
434
+ assert.deepEqual(cols.sort(),
435
+ ["applied_generation", "dirty", "generation", "id", "last_attempt", "last_error", "success_at", "type_status"],
436
+ "旧库打开必须自动补齐 3 列");
437
+ const state = store.getMirrorState();
438
+ assert.equal(state.dirty, true, "旧 dirty 数据必须保留");
439
+ assert.equal(state.last_error, "old error", "旧 last_error 必须保留");
440
+ assert.equal(state.last_attempt, "old attempt", "旧 last_attempt 必须保留");
441
+ assert.equal(state.generation, 0, "新列默认 0");
442
+ assert.equal(state.applied_generation, 0);
443
+ assert.deepEqual(state.type_status, {}, "新列默认空对象");
444
+ assert.equal(store.count(), 1, "memories 数据必须保留");
445
+ assert.equal(store.getById("m1").title, "老数据");
446
+
447
+ // 重复打开幂等:不抛、列不变、数据不变
448
+ store.close();
449
+ store = createStore(dbPath);
450
+ const cols2 = store.db.prepare("PRAGMA table_info(mirror_state)").all().map((c) => c.name);
451
+ assert.deepEqual(cols2.sort(),
452
+ ["applied_generation", "dirty", "generation", "id", "last_attempt", "last_error", "success_at", "type_status"],
453
+ "重复打开不重复加列");
454
+ assert.equal(store.getMirrorState().last_error, "old error", "重复打开不丢数据");
455
+ assert.equal(store.count(), 1);
456
+
457
+ // 新方法在迁移后的库上正常工作
458
+ const clean = store.markMirrorCleanForGeneration(0, "t");
459
+ assert.equal(clean.dirty, false, "迁移库上 fence clean 正常工作");
460
+ store.setTypeStatus("project", { status: "failed", applied_gen: 0 });
461
+ assert.equal(store.getTypeStatus().project.status, "failed", "迁移库上 setTypeStatus 正常工作");
462
+ store.close();
463
+ } finally {
464
+ rmSync(dir, { recursive: true, force: true });
465
+ }
466
+ });
467
+
468
+ test("v0.3.9-D: mirror 逐 type 物理终态——兄弟 type 失败不误标已提交 type", () => {
469
+ const { dir, store, mirror, service } = setup();
470
+ try {
471
+ // 注入逐 type 故障:project 写成功、decision 抛错(模拟 EISDIR),其余 type 正常。
472
+ // 对应审计 D:project 文件已物理提交、decision 失败,状态必须逐 type 记录——
473
+ // 不能像旧逻辑那样整体批量标 failed。
474
+ const original = mirror.sync.bind(mirror);
475
+ mirror.sync = (memories) => {
476
+ const results = original(memories);
477
+ results.decision = { ok: false, error: "EISDIR: decision.md is a directory" };
478
+ return results;
479
+ };
480
+ // project 与 decision 都有真实记忆 → 触发逐 type 渲染
481
+ store.save({ id: "mem-project", type: "project", title: "已提交", content: "物理写入", importance: 3, tags: [] });
482
+ store.save({ id: "mem-decision", type: "decision", title: "写失败", content: "此 type 失败", importance: 3, tags: [] });
483
+ // 直接调 service 内部 syncMirror(通过一次写触发)
484
+ service.saveWithDedupe({ type: "project", title: "触发", content: "sync", importance: 3 });
485
+
486
+ const ts = store.getTypeStatus();
487
+ // project 物理提交 → 必须 committed,不能被 decision 失败拖成 failed
488
+ assert.equal(ts.project.status, "committed", "物理已提交的 type 必须标记 committed");
489
+ assert.equal(ts.decision.status, "failed", "失败的 type 必须标记 failed");
490
+ // 部分失败 = 未完全收敛 → dirty 必须持久
491
+ assert.equal(store.getMirrorState().dirty, true, "部分 type 失败必须持久 dirty");
492
+ // 镜像里 project 文件真实存在(物理终态已落地,不是整体失败)
493
+ const projectFile = mirror.filePath("project");
494
+ assert.ok(existsSync(projectFile), "project 镜像文件必须已物理写入");
495
+ } finally {
496
+ store.close();
497
+ rmSync(dir, { recursive: true, force: true });
498
+ }
499
+ });