@modusensus/dsh-mneme 0.6.7 → 0.6.8

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 (117) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +463 -463
  3. package/cordis.patch.yml +15 -15
  4. package/lib/api.js +783 -783
  5. package/lib/client.js +1757 -1757
  6. package/lib/commands.js +64 -64
  7. package/lib/config.js +288 -288
  8. package/lib/dream/clustering.js +118 -118
  9. package/lib/dream/decisions.js +439 -439
  10. package/lib/dream/sleep.js +561 -554
  11. package/lib/dream/tag-extractor.js +156 -156
  12. package/lib/dream.js +935 -929
  13. package/lib/embedding.js +154 -154
  14. package/lib/entities/extractor.js +242 -242
  15. package/lib/hot-memory.js +53 -53
  16. package/lib/index.js +361 -361
  17. package/lib/inject.js +208 -208
  18. package/lib/local-embedder.js +282 -282
  19. package/lib/mirror.js +170 -170
  20. package/lib/parser/tag.js +59 -59
  21. package/lib/parser/wiki-link.js +38 -38
  22. package/lib/quality-filter.js +123 -123
  23. package/lib/reranker.js +218 -218
  24. package/lib/search/adaptive.js +22 -22
  25. package/lib/search/bm25.js +96 -96
  26. package/lib/search/tag-boost.js +61 -61
  27. package/lib/service.js +1726 -1726
  28. package/lib/settings.js +172 -172
  29. package/lib/store.js +2238 -2238
  30. package/lib/summarize.js +236 -236
  31. package/lib/tools.js +290 -290
  32. package/lib/vector-index.js +116 -116
  33. package/package.json +80 -80
  34. package/scripts/benchmark-embed.js +201 -201
  35. package/scripts/benchmark-recall.js +133 -133
  36. package/scripts/benchmark-rerank.js +166 -166
  37. package/scripts/e2e-dsh.js +218 -218
  38. package/scripts/stress-dsh.js +255 -255
  39. package/scripts/sync-lib.js +52 -52
  40. package/src/api.js +783 -783
  41. package/src/commands.js +64 -64
  42. package/src/config.js +288 -288
  43. package/src/dream/clustering.js +118 -118
  44. package/src/dream/decisions.js +439 -439
  45. package/src/dream/sleep.js +561 -554
  46. package/src/dream/tag-extractor.js +156 -156
  47. package/src/dream.js +935 -929
  48. package/src/embedding.js +154 -154
  49. package/src/entities/extractor.js +242 -242
  50. package/src/hot-memory.js +53 -53
  51. package/src/index.js +361 -361
  52. package/src/inject.js +208 -208
  53. package/src/local-embedder.js +282 -282
  54. package/src/mirror.js +170 -170
  55. package/src/parser/tag.js +59 -59
  56. package/src/parser/wiki-link.js +38 -38
  57. package/src/quality-filter.js +123 -123
  58. package/src/reranker.js +218 -218
  59. package/src/search/adaptive.js +22 -22
  60. package/src/search/bm25.js +96 -96
  61. package/src/search/tag-boost.js +61 -61
  62. package/src/service.js +1726 -1726
  63. package/src/settings.js +172 -172
  64. package/src/store.js +2238 -2238
  65. package/src/summarize.js +236 -236
  66. package/src/tools.js +290 -290
  67. package/src/vector-index.js +116 -116
  68. package/test/api.test.js +594 -594
  69. package/test/audit.test.js +448 -448
  70. package/test/benchmark.test.js +35 -35
  71. package/test/boundary-v0625.test.js +82 -82
  72. package/test/client.test.js +368 -368
  73. package/test/clustering.test.js +100 -100
  74. package/test/commands.test.js +69 -69
  75. package/test/config.test.js +50 -50
  76. package/test/conflict-freeze.test.js +290 -290
  77. package/test/directory.test.js +134 -134
  78. package/test/dream.test.js +903 -901
  79. package/test/entities.test.js +522 -522
  80. package/test/epistemic.test.js +298 -298
  81. package/test/fnew-0112.test.js +311 -311
  82. package/test/fnew-03.test.js +422 -422
  83. package/test/graph-api.test.js +175 -175
  84. package/test/helpers/dream-mock.js +82 -82
  85. package/test/hot-memory.test.js +174 -174
  86. package/test/inject.test.js +103 -103
  87. package/test/llm-audit.test.js +279 -279
  88. package/test/local-embedder.test.js +227 -227
  89. package/test/mirror-dirty.test.js +424 -424
  90. package/test/mirror-edit-digest.test.js +187 -187
  91. package/test/mirror-generation.test.js +499 -499
  92. package/test/mirror.test.js +249 -249
  93. package/test/normalize-decisions.test.js +120 -120
  94. package/test/peer-blockers.test.js +190 -190
  95. package/test/policy-epoch.test.js +259 -259
  96. package/test/provenance.test.js +103 -103
  97. package/test/quality-filter.test.js +118 -118
  98. package/test/reasoning-effort.test.js +199 -199
  99. package/test/recall-evals.test.js +235 -235
  100. package/test/recall-layer.test.js +315 -315
  101. package/test/receipt-chain.test.js +451 -451
  102. package/test/reflection.test.js +226 -226
  103. package/test/reranker.test.js +240 -240
  104. package/test/search-fusion.test.js +90 -90
  105. package/test/semantic.test.js +124 -124
  106. package/test/service-search.test.js +199 -199
  107. package/test/service.test.js +435 -435
  108. package/test/settings.test.js +118 -118
  109. package/test/sleep.test.js +365 -365
  110. package/test/store.test.js +436 -436
  111. package/test/stress.test.js +209 -209
  112. package/test/summarize.test.js +191 -191
  113. package/test/tag-boost.test.js +125 -125
  114. package/test/tag.test.js +312 -312
  115. package/test/tools.test.js +285 -285
  116. package/test/vector-index.test.js +221 -221
  117. package/test/wiki-link.test.js +332 -332
@@ -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
+ });