@modusensus/dsh-mneme 0.2.10 → 0.2.11
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.
- package/README.md +3 -3
- package/lib/service.js +21 -2
- package/package.json +1 -1
- package/src/service.js +21 -2
- package/test/fnew-0112.test.js +316 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/@modusensus/dsh-mneme)
|
|
6
6
|
[](LICENSE)
|
|
7
7
|
[](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)
|
|
8
|
-
[](https://github.com/modusensus/dsh-mneme)
|
|
9
9
|
|
|
10
10
|
> 给 DeepSeek Harness 的跨会话记忆插件:让 Agent 记住你、记住项目、自动整理记忆。**Mneme**(Μνήμη)——希腊记忆女神 Mnemosyne 之名,掌管记忆与梦境,正如 autoDream 在后台巩固记忆。
|
|
11
11
|
|
|
@@ -225,7 +225,7 @@ src/
|
|
|
225
225
|
lib/
|
|
226
226
|
├── client.js # Web 面板(手写 ModuleLoader bundle)
|
|
227
227
|
└── *.js # src 的同步分发产物
|
|
228
|
-
test/ #
|
|
228
|
+
test/ # 373 个 node:test 测试(含审计与三轴线压测不变量)
|
|
229
229
|
scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压测 · sync-lib.js 同步
|
|
230
230
|
```
|
|
231
231
|
|
|
@@ -234,7 +234,7 @@ scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压
|
|
|
234
234
|
```bash
|
|
235
235
|
cd dsh-mneme
|
|
236
236
|
npm install # 安装 peer 依赖(以 devDependencies 形式,用于本地测试)
|
|
237
|
-
npm test # 运行
|
|
237
|
+
npm test # 运行 373 个测试
|
|
238
238
|
npm run stress # 三轴线压测:长会话检索 / 冲突仲裁 / 多 Agent 并发(离线 mock LLM)
|
|
239
239
|
npm run sync # 把 src/ 同步到 lib/(发布时由 prepack 钩子自动执行)
|
|
240
240
|
```
|
package/lib/service.js
CHANGED
|
@@ -221,7 +221,11 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
221
221
|
throw error;
|
|
222
222
|
} finally {
|
|
223
223
|
txDepth--;
|
|
224
|
-
|
|
224
|
+
try {
|
|
225
|
+
syncMirror();
|
|
226
|
+
} catch (error) {
|
|
227
|
+
console.warn("syncMirror failed after transaction:", error);
|
|
228
|
+
}
|
|
225
229
|
notifyWrite();
|
|
226
230
|
}
|
|
227
231
|
}
|
|
@@ -293,6 +297,17 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
293
297
|
if (typeof edit.title === "string" && edit.title.trim()) patch.title = edit.title.trim();
|
|
294
298
|
if (typeof edit.content === "string" && edit.content.trim()) patch.content = edit.content.trim();
|
|
295
299
|
if (Object.keys(patch).length) {
|
|
300
|
+
// 启动回灌(F-NEW-01):digest 存在且匹配 = 文件自渲染后无人触碰(旧机器
|
|
301
|
+
// 镜像),机器 wins,DB 的 New 必须保留,静默改回 Old 是 bug。
|
|
302
|
+
const digestMatches = typeof edit.digest === "string"
|
|
303
|
+
&& typeof edit.title === "string"
|
|
304
|
+
&& typeof edit.content === "string"
|
|
305
|
+
&& createHash("sha256").update(`${edit.title}\x00${edit.content}`).digest("hex") === edit.digest;
|
|
306
|
+
if (digestMatches) continue;
|
|
307
|
+
// 文件 == store(无实际变化)时不覆盖,也不计入 applied。
|
|
308
|
+
const hasDiff = (patch.title !== undefined && existing.title !== patch.title)
|
|
309
|
+
|| (patch.content !== undefined && existing.content !== patch.content);
|
|
310
|
+
if (!hasDiff) continue;
|
|
296
311
|
store.update(edit.id, patch);
|
|
297
312
|
applied++;
|
|
298
313
|
}
|
|
@@ -387,7 +402,11 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
387
402
|
*/
|
|
388
403
|
function syncMirror() {
|
|
389
404
|
if (txDepth > 0 || !mirror) return; // deferred to the transaction's commit
|
|
390
|
-
|
|
405
|
+
try {
|
|
406
|
+
mirror.sync(reconcileHumanEdits(store.list({ limit: 500, includeForgotten: false })));
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.warn("syncMirror failed:", error);
|
|
409
|
+
}
|
|
391
410
|
}
|
|
392
411
|
|
|
393
412
|
return {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modusensus/dsh-mneme",
|
|
3
3
|
"description": "Cross-session memory plugin for DeepSeek Harness with autoDream consolidation: SQLite store, Markdown mirrors, 6 model tools, automatic injection, session summarization, user profile/rules, custom slash commands, vector (semantic) search, and a Web GUI panel",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.11",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|
package/src/service.js
CHANGED
|
@@ -221,7 +221,11 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
221
221
|
throw error;
|
|
222
222
|
} finally {
|
|
223
223
|
txDepth--;
|
|
224
|
-
|
|
224
|
+
try {
|
|
225
|
+
syncMirror();
|
|
226
|
+
} catch (error) {
|
|
227
|
+
console.warn("syncMirror failed after transaction:", error);
|
|
228
|
+
}
|
|
225
229
|
notifyWrite();
|
|
226
230
|
}
|
|
227
231
|
}
|
|
@@ -293,6 +297,17 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
293
297
|
if (typeof edit.title === "string" && edit.title.trim()) patch.title = edit.title.trim();
|
|
294
298
|
if (typeof edit.content === "string" && edit.content.trim()) patch.content = edit.content.trim();
|
|
295
299
|
if (Object.keys(patch).length) {
|
|
300
|
+
// 启动回灌(F-NEW-01):digest 存在且匹配 = 文件自渲染后无人触碰(旧机器
|
|
301
|
+
// 镜像),机器 wins,DB 的 New 必须保留,静默改回 Old 是 bug。
|
|
302
|
+
const digestMatches = typeof edit.digest === "string"
|
|
303
|
+
&& typeof edit.title === "string"
|
|
304
|
+
&& typeof edit.content === "string"
|
|
305
|
+
&& createHash("sha256").update(`${edit.title}\x00${edit.content}`).digest("hex") === edit.digest;
|
|
306
|
+
if (digestMatches) continue;
|
|
307
|
+
// 文件 == store(无实际变化)时不覆盖,也不计入 applied。
|
|
308
|
+
const hasDiff = (patch.title !== undefined && existing.title !== patch.title)
|
|
309
|
+
|| (patch.content !== undefined && existing.content !== patch.content);
|
|
310
|
+
if (!hasDiff) continue;
|
|
296
311
|
store.update(edit.id, patch);
|
|
297
312
|
applied++;
|
|
298
313
|
}
|
|
@@ -387,7 +402,11 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
387
402
|
*/
|
|
388
403
|
function syncMirror() {
|
|
389
404
|
if (txDepth > 0 || !mirror) return; // deferred to the transaction's commit
|
|
390
|
-
|
|
405
|
+
try {
|
|
406
|
+
mirror.sync(reconcileHumanEdits(store.list({ limit: 500, includeForgotten: false })));
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.warn("syncMirror failed:", error);
|
|
409
|
+
}
|
|
391
410
|
}
|
|
392
411
|
|
|
393
412
|
return {
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, readFileSync, writeFileSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { createStore } from "../src/store.js";
|
|
8
|
+
import { createMirror, TYPE_FILE } from "../src/mirror.js";
|
|
9
|
+
import { createService } from "../src/service.js";
|
|
10
|
+
import { applyDecisions } from "../src/dream.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* F-NEW-01 / F-NEW-02 回归测试(audit peer 新阻断项)。
|
|
14
|
+
*
|
|
15
|
+
* F-NEW-01 启动回灌静默改回:mergeHumanEdits 必须按 last-rendered digest 判定
|
|
16
|
+
* "无人触碰的旧机器镜像"(digest 匹配 → 机器 wins,保留 DB 的 New)与
|
|
17
|
+
* "人工动过的镜像"(digest 缺失/不匹配 → 覆盖)。
|
|
18
|
+
* F-NEW-02 transaction mirror 失败误报未提交:COMMIT 后 syncMirror 抛错不得
|
|
19
|
+
* 外抛、不得把已提交事务当失败(applied/committed 必须如实反映真实提交)。
|
|
20
|
+
*
|
|
21
|
+
* 测试点由 Kimi K2.7 设计。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const CONFLICT_MARKER = "并发冲突";
|
|
25
|
+
|
|
26
|
+
function digestOf(title, content) {
|
|
27
|
+
return createHash("sha256").update(`${title}\x00${content}`).digest("hex");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function mirrorPath(dir, type) {
|
|
31
|
+
return join(dir, TYPE_FILE[type]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function setup({ dbPath } = {}) {
|
|
35
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-fnew-"));
|
|
36
|
+
const mirrorDir = join(dir, "mirror");
|
|
37
|
+
const store = createStore(dbPath ?? ":memory:");
|
|
38
|
+
const mirror = createMirror(mirrorDir);
|
|
39
|
+
const service = createService({ store, mirror, config: {} });
|
|
40
|
+
return { dir, mirrorDir, store, mirror, service };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function stripDigestComment(text) {
|
|
44
|
+
return text.replace(/<!--\s*mirror-digest: [a-f0-9]+ -->\n?/g, "");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 构造"DB 已提交 New + 镜像仍为旧渲染"的真实状态:先用 service 保存 Old
|
|
49
|
+
* (渲染出带有效 digest 的旧镜像),再绕过 service 直接写 store 为 New,
|
|
50
|
+
* 模拟镜像同步失败后 DB 与镜像分叉。
|
|
51
|
+
*/
|
|
52
|
+
function seedStaleMirror(service, store, { type, title, old, next }) {
|
|
53
|
+
const { memory } = service.saveWithDedupe({ type, title, content: old, importance: 3 });
|
|
54
|
+
store.update(memory.id, { content: next });
|
|
55
|
+
assert.equal(store.getById(memory.id).content, next, "前置:DB 必须为 New");
|
|
56
|
+
return memory;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ── F-NEW-01:启动回灌 digest 判定 ──────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
test("F-NEW-01 a: 旧镜像带有效 digest → 回灌跳过覆盖,store 保持 New、applied=0", () => {
|
|
62
|
+
const { dir, mirrorDir, store, mirror, service } = setup();
|
|
63
|
+
try {
|
|
64
|
+
const m = seedStaleMirror(service, store, { type: "project", title: "启动回灌", old: "Old", next: "New" });
|
|
65
|
+
const edits = mirror.readHumanEdits("project");
|
|
66
|
+
assert.ok(edits.length >= 1, "镜像必须可读回编辑");
|
|
67
|
+
assert.equal(edits.find((e) => e.id === m.id)?.digest, digestOf("启动回灌", "Old"), "digest 必须随旧渲染写入");
|
|
68
|
+
const applied = service.mergeHumanEdits("project", edits);
|
|
69
|
+
assert.equal(applied, 0, "digest 匹配 = 无人触碰 → 不得覆盖");
|
|
70
|
+
assert.equal(store.getById(m.id).content, "New", "DB 的 New 必须保留");
|
|
71
|
+
assert.ok(!store.getById(m.id).content.includes(CONFLICT_MARKER), "不得出现冲突标记");
|
|
72
|
+
} finally {
|
|
73
|
+
rmSync(dir, { recursive: true, force: true });
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("F-NEW-01 b: 老文件无 digest → 保守覆盖成 Old(行为保留)", () => {
|
|
78
|
+
const { dir, mirrorDir, store, mirror, service } = setup();
|
|
79
|
+
try {
|
|
80
|
+
const m = seedStaleMirror(service, store, { type: "preference", title: "老文件", old: "Old", next: "New" });
|
|
81
|
+
const file = mirrorPath(mirrorDir, "preference");
|
|
82
|
+
writeFileSync(file, stripDigestComment(readFileSync(file, "utf8")), "utf8");
|
|
83
|
+
const edits = mirror.readHumanEdits("preference");
|
|
84
|
+
const edit = edits.find((e) => e.id === m.id);
|
|
85
|
+
assert.equal(edit.digest, undefined, "无 digest 字段");
|
|
86
|
+
const applied = service.mergeHumanEdits("preference", edits);
|
|
87
|
+
assert.equal(applied, 1, "digest 缺失 = 视为人工动过 → 覆盖");
|
|
88
|
+
assert.equal(store.getById(m.id).content, "Old", "store 被覆盖为旧值");
|
|
89
|
+
} finally {
|
|
90
|
+
rmSync(dir, { recursive: true, force: true });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("F-NEW-01 c: 镜像被人工改(digest 不匹配)→ 覆盖成人工值", () => {
|
|
95
|
+
const { dir, mirrorDir, store, mirror, service } = setup();
|
|
96
|
+
try {
|
|
97
|
+
const m = seedStaleMirror(service, store, { type: "decision", title: "人工改", old: "机器旧", next: "机器新" });
|
|
98
|
+
const file = mirrorPath(mirrorDir, "decision");
|
|
99
|
+
// 人工改内容但保留 digest 注释行(此时 digest 已不匹配)
|
|
100
|
+
writeFileSync(file, readFileSync(file, "utf8").replace("机器旧", "人类编辑值"), "utf8");
|
|
101
|
+
const edits = mirror.readHumanEdits("decision");
|
|
102
|
+
const edit = edits.find((e) => e.id === m.id);
|
|
103
|
+
assert.equal(edit.content, "人类编辑值");
|
|
104
|
+
assert.notEqual(edit.digest, digestOf("人工改", "人类编辑值"), "digest 必须不匹配");
|
|
105
|
+
const applied = service.mergeHumanEdits("decision", edits);
|
|
106
|
+
assert.equal(applied, 1, "digest 不匹配 = 人工动过 → 覆盖");
|
|
107
|
+
assert.equal(store.getById(m.id).content, "人类编辑值", "人工值必须落地");
|
|
108
|
+
} finally {
|
|
109
|
+
rmSync(dir, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("F-NEW-01 d: 端到端启动回灌(index.js 同款读-merge 循环)→ DB New 保留且重开持久", () => {
|
|
114
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-fnew-"));
|
|
115
|
+
const dbPath = join(dir, "memory.db");
|
|
116
|
+
const mirrorDir = join(dir, "mirror");
|
|
117
|
+
try {
|
|
118
|
+
// 首次"启动":保存 Old → DB 直写 New(镜像分叉)
|
|
119
|
+
let { store, mirror, service } = setup({ dbPath });
|
|
120
|
+
const m = seedStaleMirror(service, store, { type: "summary", title: "总览", old: "Old", next: "New" });
|
|
121
|
+
store.close();
|
|
122
|
+
|
|
123
|
+
// 第二次"启动":完整走 index.js 的回灌循环(先读全部类型再逐类 merge)
|
|
124
|
+
store = createStore(dbPath);
|
|
125
|
+
mirror = createMirror(mirrorDir);
|
|
126
|
+
service = createService({ store, mirror, config: {} });
|
|
127
|
+
for (const type of Object.keys(TYPE_FILE)) {
|
|
128
|
+
const edits = mirror.readHumanEdits(type);
|
|
129
|
+
if (edits.length) service.mergeHumanEdits(type, edits);
|
|
130
|
+
}
|
|
131
|
+
assert.equal(store.getById(m.id).content, "New", "重启回灌不得把 DB 静默改回 Old");
|
|
132
|
+
store.close();
|
|
133
|
+
|
|
134
|
+
// 第三次打开:确认持久化且不会再被翻转
|
|
135
|
+
store = createStore(dbPath);
|
|
136
|
+
assert.equal(store.getById(m.id).content, "New", "重开后 DB 仍为 New");
|
|
137
|
+
store.close();
|
|
138
|
+
} finally {
|
|
139
|
+
rmSync(dir, { recursive: true, force: true });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("F-NEW-01: hasDiff 守卫——补丁与 store 相同则不覆盖、不计数、updated_at 不变", () => {
|
|
144
|
+
const { dir, mirrorDir, store, mirror, service } = setup();
|
|
145
|
+
try {
|
|
146
|
+
const m = seedStaleMirror(service, store, { type: "project", title: "无差异", old: "Old", next: "New" });
|
|
147
|
+
const file = mirrorPath(mirrorDir, "project");
|
|
148
|
+
// 文件内容 == store(New),但 digest 注释是旧渲染的 → 判定走覆盖分支,
|
|
149
|
+
// hasDiff 必须拦住无实际变化的 UPDATE。
|
|
150
|
+
writeFileSync(file, readFileSync(file, "utf8").replace("Old", "New"), "utf8");
|
|
151
|
+
const edits = mirror.readHumanEdits("project");
|
|
152
|
+
assert.equal(edits.find((e) => e.id === m.id).content, "New");
|
|
153
|
+
const before = store.getById(m.id).updated_at;
|
|
154
|
+
const applied = service.mergeHumanEdits("project", edits);
|
|
155
|
+
assert.equal(applied, 0, "无实际差异不得计入 applied");
|
|
156
|
+
assert.equal(store.getById(m.id).content, "New");
|
|
157
|
+
assert.equal(store.getById(m.id).updated_at, before, "不得发出无意义的 UPDATE");
|
|
158
|
+
} finally {
|
|
159
|
+
rmSync(dir, { recursive: true, force: true });
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("F-NEW-01: 混合编辑逐条独立判定(有效 digest 跳过 / 缺 digest 覆盖 / 不匹配覆盖)", () => {
|
|
164
|
+
const { dir, mirrorDir, store, mirror, service } = setup();
|
|
165
|
+
try {
|
|
166
|
+
const a = seedStaleMirror(service, store, { type: "project", title: "A", old: "A-旧", next: "A-新" });
|
|
167
|
+
const b = service.saveWithDedupe({ type: "project", title: "B", content: "B-旧", importance: 3 }).memory;
|
|
168
|
+
const c = service.saveWithDedupe({ type: "project", title: "C", content: "C-旧", importance: 3 }).memory;
|
|
169
|
+
store.update(b.id, { content: "B-新" });
|
|
170
|
+
store.update(c.id, { content: "C-新" });
|
|
171
|
+
// 手工构造编辑数组:a 有效 digest(跳过);b 无 digest(覆盖);c digest 不匹配(覆盖)
|
|
172
|
+
const edits = [
|
|
173
|
+
{ id: a.id, title: "A", content: "A-旧", digest: digestOf("A", "A-旧") },
|
|
174
|
+
{ id: b.id, title: "B", content: "B-旧" },
|
|
175
|
+
{ id: c.id, title: "C", content: "C-人类", digest: digestOf("C", "C-旧") }
|
|
176
|
+
];
|
|
177
|
+
const applied = service.mergeHumanEdits("project", edits);
|
|
178
|
+
assert.equal(applied, 2, "只有 b、c 被覆盖");
|
|
179
|
+
assert.equal(store.getById(a.id).content, "A-新", "a 保持 New");
|
|
180
|
+
assert.equal(store.getById(b.id).content, "B-旧", "b 无 digest → 覆盖成旧值");
|
|
181
|
+
assert.equal(store.getById(c.id).content, "C-人类", "c 人工值落地");
|
|
182
|
+
} finally {
|
|
183
|
+
rmSync(dir, { recursive: true, force: true });
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// ── F-NEW-02:transaction 已提交 + mirror 失败不得误报 ─────────────────────
|
|
188
|
+
|
|
189
|
+
function throwingMirrorSync(mirror, message = "No space left on device") {
|
|
190
|
+
const original = mirror.sync.bind(mirror);
|
|
191
|
+
const err = new Error(message);
|
|
192
|
+
err.code = "ENOSPC";
|
|
193
|
+
mirror.sync = () => { throw err; };
|
|
194
|
+
return { original, err };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
test("F-NEW-02 e: COMMIT 后 mirror.sync 抛 ENOSPC → transaction 正常返回、不抛、DB 已提交且重开持久", () => {
|
|
198
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-fnew-"));
|
|
199
|
+
const dbPath = join(dir, "memory.db");
|
|
200
|
+
const mirrorDir = join(dir, "mirror");
|
|
201
|
+
try {
|
|
202
|
+
const store = createStore(dbPath);
|
|
203
|
+
const mirror = createMirror(mirrorDir);
|
|
204
|
+
const service = createService({ store, mirror, config: {} });
|
|
205
|
+
const { original } = throwingMirrorSync(mirror);
|
|
206
|
+
|
|
207
|
+
let warns = 0;
|
|
208
|
+
let committedId;
|
|
209
|
+
const origWarn = console.warn;
|
|
210
|
+
console.warn = (...a) => { warns++; origWarn(...a); };
|
|
211
|
+
try {
|
|
212
|
+
let result;
|
|
213
|
+
assert.doesNotThrow(() => {
|
|
214
|
+
result = service.transaction(() => {
|
|
215
|
+
const { memory } = service.saveWithDedupe({ type: "project", title: "原子", content: "已提交", importance: 3 });
|
|
216
|
+
committedId = memory.id;
|
|
217
|
+
return "tx-result";
|
|
218
|
+
});
|
|
219
|
+
}, "syncMirror 失败不得向外抛(已 COMMIT 不得被当成未提交)");
|
|
220
|
+
assert.equal(result, "tx-result", "transaction 必须返回 fn 的结果");
|
|
221
|
+
} finally {
|
|
222
|
+
console.warn = origWarn;
|
|
223
|
+
}
|
|
224
|
+
assert.ok(warns >= 1, "fail-safe 应走 console.warn 路径");
|
|
225
|
+
assert.equal(store.count(), 1, "DB 必须已提交");
|
|
226
|
+
assert.equal(service.getById(committedId).content, "已提交");
|
|
227
|
+
|
|
228
|
+
// 恢复 mirror.sync,下一次写应能重新渲染,镜像最终与 store 一致
|
|
229
|
+
mirror.sync = original;
|
|
230
|
+
service.saveWithDedupe({ type: "project", title: "后续", content: "x", importance: 2 });
|
|
231
|
+
const file = readFileSync(mirrorPath(mirrorDir, "project"), "utf8");
|
|
232
|
+
assert.match(file, /已提交/, "失败后的下一次同步必须收敛镜像");
|
|
233
|
+
|
|
234
|
+
// 重开验证持久化
|
|
235
|
+
store.close();
|
|
236
|
+
const reopened = createStore(dbPath);
|
|
237
|
+
assert.equal(reopened.count(), 2, "重开后已提交数据仍在");
|
|
238
|
+
assert.equal(reopened.getById(committedId).content, "已提交");
|
|
239
|
+
reopened.close();
|
|
240
|
+
} finally {
|
|
241
|
+
rmSync(dir, { recursive: true, force: true });
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("F-NEW-02 f: mirror.sync 抛错时 applyDecisions 的 applied/committed/failures 如实(不虚报 0)", () => {
|
|
246
|
+
const { dir, mirrorDir, store, mirror, service } = setup();
|
|
247
|
+
try {
|
|
248
|
+
const { memory: m } = service.saveWithDedupe({ type: "project", title: "归档目标", content: "x", importance: 3 });
|
|
249
|
+
const { original } = throwingMirrorSync(mirror);
|
|
250
|
+
try {
|
|
251
|
+
// 修复前:transaction finally 的 mirror 异常会向上抛 → 被 applyDecisions
|
|
252
|
+
// 当成 failure → applied:0、committed:[],receipt/outcome 虚报 reconcile。
|
|
253
|
+
const { applied, conflicts, failures, committed } = applyDecisions(
|
|
254
|
+
[{ action: "archive", ids: [m.id] }],
|
|
255
|
+
service,
|
|
256
|
+
null,
|
|
257
|
+
null
|
|
258
|
+
);
|
|
259
|
+
assert.equal(applied, 1, "真实提交数为 1,不是 0");
|
|
260
|
+
assert.equal(failures.length, 0, "已提交的决策不得被误判为失败");
|
|
261
|
+
assert.equal(conflicts.length, 0);
|
|
262
|
+
assert.equal(committed.length, 1, "committed 必须来自真实提交子步骤");
|
|
263
|
+
assert.equal(committed[0].action, "archive");
|
|
264
|
+
assert.equal(store.getById(m.id).archived, true, "DB 必须真实归档");
|
|
265
|
+
} finally {
|
|
266
|
+
mirror.sync = original;
|
|
267
|
+
}
|
|
268
|
+
} finally {
|
|
269
|
+
rmSync(dir, { recursive: true, force: true });
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("F-NEW-02 g: 回滚路径——fn 抛错 → ROLLBACK + 原错误传播(mirror 失败不得掩盖)", () => {
|
|
274
|
+
const { dir, mirrorDir, store, mirror, service } = setup();
|
|
275
|
+
try {
|
|
276
|
+
const { original } = throwingMirrorSync(mirror);
|
|
277
|
+
let threw = null;
|
|
278
|
+
try {
|
|
279
|
+
service.transaction(() => {
|
|
280
|
+
service.saveWithDedupe({ type: "project", title: "应回滚", content: "x", importance: 3 });
|
|
281
|
+
throw new Error("boom");
|
|
282
|
+
});
|
|
283
|
+
} catch (error) {
|
|
284
|
+
threw = error;
|
|
285
|
+
} finally {
|
|
286
|
+
mirror.sync = original;
|
|
287
|
+
}
|
|
288
|
+
assert.ok(threw, "必须抛错");
|
|
289
|
+
assert.equal(threw.message, "boom", "必须传播原始错误,而非 mirror 的 ENOSPC");
|
|
290
|
+
assert.equal(store.count(), 0, "回滚后无残留写入");
|
|
291
|
+
} finally {
|
|
292
|
+
rmSync(dir, { recursive: true, force: true });
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("F-NEW-02: mirror 自身 fail-safe——底层 sync 抛错被吞并 console.warn,不向上抛", () => {
|
|
297
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-fnew-"));
|
|
298
|
+
const dbPath = join(dir, "memory.db");
|
|
299
|
+
const mirrorDir = join(dir, "mirror");
|
|
300
|
+
try {
|
|
301
|
+
const store = createStore(dbPath);
|
|
302
|
+
const mirror = createMirror(mirrorDir);
|
|
303
|
+
const service = createService({ store, mirror, config: {} });
|
|
304
|
+
const { original } = throwingMirrorSync(mirror, "EACCES");
|
|
305
|
+
try {
|
|
306
|
+
assert.doesNotThrow(() => service.saveWithDedupe({ type: "history", title: "非事务路径", content: "x", importance: 3 }),
|
|
307
|
+
"事务外的写路径(saveWithDedupe→syncMirror)也必须 fail-safe");
|
|
308
|
+
assert.equal(store.count(), 1, "store 写入不受 mirror 失败影响");
|
|
309
|
+
} finally {
|
|
310
|
+
mirror.sync = original;
|
|
311
|
+
}
|
|
312
|
+
store.close();
|
|
313
|
+
} finally {
|
|
314
|
+
rmSync(dir, { recursive: true, force: true });
|
|
315
|
+
}
|
|
316
|
+
});
|