@modusensus/dsh-mneme 0.2.9 → 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/mirror.js +14 -2
- package/lib/service.js +37 -5
- package/package.json +1 -1
- package/src/mirror.js +14 -2
- package/src/service.js +37 -5
- package/test/fnew-0112.test.js +316 -0
- package/test/mirror-edit-digest.test.js +185 -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/mirror.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from "node:fs";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
|
|
4
5
|
export const TYPE_FILE = {
|
|
@@ -21,6 +22,13 @@ function unescape(text) {
|
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
function renderMemory(m) {
|
|
25
|
+
// last-rendered digest baseline: sha256(title \x00 content). service.js
|
|
26
|
+
// compares the file hash against this to tell "untouched by a human" (machine
|
|
27
|
+
// write wins) apart from a real human edit, so a not-yet-re-rendered store
|
|
28
|
+
// update is not misread as a concurrent human edit.
|
|
29
|
+
const digest = createHash("sha256")
|
|
30
|
+
.update(`${m.title}\x00${m.content}`)
|
|
31
|
+
.digest("hex");
|
|
24
32
|
const lines = [];
|
|
25
33
|
lines.push(`## ${esc(m.title)}`);
|
|
26
34
|
lines.push("");
|
|
@@ -31,6 +39,7 @@ function renderMemory(m) {
|
|
|
31
39
|
lines.push(`- **更新时间**: ${m.updated_at}`);
|
|
32
40
|
if (m.source) lines.push(`- **来源**: ${esc(m.source)}`);
|
|
33
41
|
lines.push("");
|
|
42
|
+
lines.push(`<!-- mirror-digest: ${digest} -->`);
|
|
34
43
|
lines.push(m.content);
|
|
35
44
|
lines.push("");
|
|
36
45
|
lines.push("---");
|
|
@@ -86,7 +95,8 @@ export function createMirror(dir) {
|
|
|
86
95
|
let body = text
|
|
87
96
|
.slice(blockStart, blockEnd)
|
|
88
97
|
.replace(/^- \*\*ID\*\*: `[^`]+`\n?/, "")
|
|
89
|
-
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
98
|
+
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
99
|
+
.replace(/^<!-- mirror-digest: [a-f0-9]+ -->\n?/m, "");
|
|
90
100
|
const separators = [...body.matchAll(/^---\s*$/gm)];
|
|
91
101
|
const lastSep = separators[separators.length - 1];
|
|
92
102
|
if (lastSep) body = body.slice(0, lastSep.index);
|
|
@@ -97,11 +107,13 @@ export function createMirror(dir) {
|
|
|
97
107
|
// during a three-way merge of human edits (see service.syncMirror).
|
|
98
108
|
const block = text.slice(blockStart, blockEnd);
|
|
99
109
|
const updatedMatch = block.match(/- \*\*更新时间\*\*: ([^\n]+)/);
|
|
110
|
+
const digestMatch = block.match(/<!-- mirror-digest: ([a-f0-9]+) -->/);
|
|
100
111
|
edits.push({
|
|
101
112
|
id: anchor[1],
|
|
102
113
|
title: titleMatch ? unescape(titleMatch[1]).trim() : undefined,
|
|
103
114
|
content: body,
|
|
104
|
-
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined
|
|
115
|
+
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined,
|
|
116
|
+
digest: digestMatch ? digestMatch[1] : undefined
|
|
105
117
|
});
|
|
106
118
|
|
|
107
119
|
const lineEnd = text.indexOf("\n", blockStart);
|
package/lib/service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { TYPE_FILE } from "./mirror.js";
|
|
3
3
|
|
|
4
4
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
@@ -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
|
}
|
|
@@ -349,8 +364,21 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
349
364
|
const humanChanged = (typeof edit.title === "string" && edit.title !== m.title)
|
|
350
365
|
|| (typeof edit.content === "string" && edit.content !== m.content);
|
|
351
366
|
if (!humanChanged) { result.push(m); continue; }
|
|
352
|
-
//
|
|
353
|
-
//
|
|
367
|
+
// 判断文件是否被人工动过:digest 存在且匹配则无人触碰,否则视为人工动过。
|
|
368
|
+
// digest 是渲染时对 sha256(title \x00 content) 的记录;机器 store 更新后
|
|
369
|
+
// 镜像还没重渲染时读到旧内容,digest 仍匹配 → 机器 wins,不会误判为
|
|
370
|
+
// 并发人工编辑导致机器写丢失 + 伪冲突标记。
|
|
371
|
+
const digestMatches = typeof edit.digest === "string"
|
|
372
|
+
&& typeof edit.title === "string"
|
|
373
|
+
&& typeof edit.content === "string"
|
|
374
|
+
&& createHash("sha256").update(`${edit.title}\x00${edit.content}`).digest("hex") === edit.digest;
|
|
375
|
+
if (digestMatches) {
|
|
376
|
+
// 无人触碰,机器 wins,走原样
|
|
377
|
+
result.push(m);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
// 人工动过(digest 不存在=老文件/手工文件保守视为人工动过),走三方合并
|
|
381
|
+
// (保留现有 storeChanged 逻辑)
|
|
354
382
|
const storeChanged = edit.updated_at !== undefined && m.updated_at !== edit.updated_at;
|
|
355
383
|
if (storeChanged) {
|
|
356
384
|
const marker = `\n\n> ⚠️ 并发冲突:人工编辑 vs 记忆库并发更新(${m.updated_at})\n> 记忆库版本:${m.content}`;
|
|
@@ -374,7 +402,11 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
374
402
|
*/
|
|
375
403
|
function syncMirror() {
|
|
376
404
|
if (txDepth > 0 || !mirror) return; // deferred to the transaction's commit
|
|
377
|
-
|
|
405
|
+
try {
|
|
406
|
+
mirror.sync(reconcileHumanEdits(store.list({ limit: 500, includeForgotten: false })));
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.warn("syncMirror failed:", error);
|
|
409
|
+
}
|
|
378
410
|
}
|
|
379
411
|
|
|
380
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/mirror.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from "node:fs";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
|
|
4
5
|
export const TYPE_FILE = {
|
|
@@ -21,6 +22,13 @@ function unescape(text) {
|
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
function renderMemory(m) {
|
|
25
|
+
// last-rendered digest baseline: sha256(title \x00 content). service.js
|
|
26
|
+
// compares the file hash against this to tell "untouched by a human" (machine
|
|
27
|
+
// write wins) apart from a real human edit, so a not-yet-re-rendered store
|
|
28
|
+
// update is not misread as a concurrent human edit.
|
|
29
|
+
const digest = createHash("sha256")
|
|
30
|
+
.update(`${m.title}\x00${m.content}`)
|
|
31
|
+
.digest("hex");
|
|
24
32
|
const lines = [];
|
|
25
33
|
lines.push(`## ${esc(m.title)}`);
|
|
26
34
|
lines.push("");
|
|
@@ -31,6 +39,7 @@ function renderMemory(m) {
|
|
|
31
39
|
lines.push(`- **更新时间**: ${m.updated_at}`);
|
|
32
40
|
if (m.source) lines.push(`- **来源**: ${esc(m.source)}`);
|
|
33
41
|
lines.push("");
|
|
42
|
+
lines.push(`<!-- mirror-digest: ${digest} -->`);
|
|
34
43
|
lines.push(m.content);
|
|
35
44
|
lines.push("");
|
|
36
45
|
lines.push("---");
|
|
@@ -86,7 +95,8 @@ export function createMirror(dir) {
|
|
|
86
95
|
let body = text
|
|
87
96
|
.slice(blockStart, blockEnd)
|
|
88
97
|
.replace(/^- \*\*ID\*\*: `[^`]+`\n?/, "")
|
|
89
|
-
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
98
|
+
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
99
|
+
.replace(/^<!-- mirror-digest: [a-f0-9]+ -->\n?/m, "");
|
|
90
100
|
const separators = [...body.matchAll(/^---\s*$/gm)];
|
|
91
101
|
const lastSep = separators[separators.length - 1];
|
|
92
102
|
if (lastSep) body = body.slice(0, lastSep.index);
|
|
@@ -97,11 +107,13 @@ export function createMirror(dir) {
|
|
|
97
107
|
// during a three-way merge of human edits (see service.syncMirror).
|
|
98
108
|
const block = text.slice(blockStart, blockEnd);
|
|
99
109
|
const updatedMatch = block.match(/- \*\*更新时间\*\*: ([^\n]+)/);
|
|
110
|
+
const digestMatch = block.match(/<!-- mirror-digest: ([a-f0-9]+) -->/);
|
|
100
111
|
edits.push({
|
|
101
112
|
id: anchor[1],
|
|
102
113
|
title: titleMatch ? unescape(titleMatch[1]).trim() : undefined,
|
|
103
114
|
content: body,
|
|
104
|
-
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined
|
|
115
|
+
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined,
|
|
116
|
+
digest: digestMatch ? digestMatch[1] : undefined
|
|
105
117
|
});
|
|
106
118
|
|
|
107
119
|
const lineEnd = text.indexOf("\n", blockStart);
|
package/src/service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { TYPE_FILE } from "./mirror.js";
|
|
3
3
|
|
|
4
4
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
@@ -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
|
}
|
|
@@ -349,8 +364,21 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
349
364
|
const humanChanged = (typeof edit.title === "string" && edit.title !== m.title)
|
|
350
365
|
|| (typeof edit.content === "string" && edit.content !== m.content);
|
|
351
366
|
if (!humanChanged) { result.push(m); continue; }
|
|
352
|
-
//
|
|
353
|
-
//
|
|
367
|
+
// 判断文件是否被人工动过:digest 存在且匹配则无人触碰,否则视为人工动过。
|
|
368
|
+
// digest 是渲染时对 sha256(title \x00 content) 的记录;机器 store 更新后
|
|
369
|
+
// 镜像还没重渲染时读到旧内容,digest 仍匹配 → 机器 wins,不会误判为
|
|
370
|
+
// 并发人工编辑导致机器写丢失 + 伪冲突标记。
|
|
371
|
+
const digestMatches = typeof edit.digest === "string"
|
|
372
|
+
&& typeof edit.title === "string"
|
|
373
|
+
&& typeof edit.content === "string"
|
|
374
|
+
&& createHash("sha256").update(`${edit.title}\x00${edit.content}`).digest("hex") === edit.digest;
|
|
375
|
+
if (digestMatches) {
|
|
376
|
+
// 无人触碰,机器 wins,走原样
|
|
377
|
+
result.push(m);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
// 人工动过(digest 不存在=老文件/手工文件保守视为人工动过),走三方合并
|
|
381
|
+
// (保留现有 storeChanged 逻辑)
|
|
354
382
|
const storeChanged = edit.updated_at !== undefined && m.updated_at !== edit.updated_at;
|
|
355
383
|
if (storeChanged) {
|
|
356
384
|
const marker = `\n\n> ⚠️ 并发冲突:人工编辑 vs 记忆库并发更新(${m.updated_at})\n> 记忆库版本:${m.content}`;
|
|
@@ -374,7 +402,11 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
374
402
|
*/
|
|
375
403
|
function syncMirror() {
|
|
376
404
|
if (txDepth > 0 || !mirror) return; // deferred to the transaction's commit
|
|
377
|
-
|
|
405
|
+
try {
|
|
406
|
+
mirror.sync(reconcileHumanEdits(store.list({ limit: 500, includeForgotten: false })));
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.warn("syncMirror failed:", error);
|
|
409
|
+
}
|
|
378
410
|
}
|
|
379
411
|
|
|
380
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
|
+
});
|
|
@@ -0,0 +1,185 @@
|
|
|
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 } from "../src/mirror.js";
|
|
9
|
+
import { createService } from "../src/service.js";
|
|
10
|
+
import { applyDecisions } from "../src/dream.js";
|
|
11
|
+
|
|
12
|
+
const CONFLICT_MARKER = "并发冲突";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Regression tests for the mirror-digest fix: a machine store write that has
|
|
16
|
+
* not yet been re-rendered to the mirror must NOT be misread as a concurrent
|
|
17
|
+
* human edit (which lost the machine write and planted a fake conflict marker).
|
|
18
|
+
* Each machine-write path runs against a stale mirror file (the pre-transaction
|
|
19
|
+
* render), exactly like the audited reproductions.
|
|
20
|
+
*/
|
|
21
|
+
function setup() {
|
|
22
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-digest-"));
|
|
23
|
+
const store = createStore(":memory:");
|
|
24
|
+
const mirror = createMirror(dir);
|
|
25
|
+
const service = createService({ store, mirror, config: {} });
|
|
26
|
+
return { dir, store, mirror, service };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function mirrorFile(dir, type) {
|
|
30
|
+
return join(dir, { preference: "preferences.md", project: "projects.md", decision: "decisions.md", history: "history.md" }[type]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function digestOf(title, content) {
|
|
34
|
+
return createHash("sha256").update(`${title}\x00${content}`).digest("hex");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test("digest 匹配:直接 update 后机器写落地、无伪冲突 marker", () => {
|
|
38
|
+
const { dir, store, service } = setup();
|
|
39
|
+
try {
|
|
40
|
+
const { memory: m } = service.saveWithDedupe({ type: "project", title: "直接更新", content: "v1", importance: 3 });
|
|
41
|
+
service.update(m.id, { content: "v2" });
|
|
42
|
+
// 镜像此时还是 v1 的旧渲染;digest 仍匹配 → 机器 wins
|
|
43
|
+
assert.equal(store.getById(m.id).content, "v2", "机器新值必须落地");
|
|
44
|
+
assert.ok(!store.getById(m.id).content.includes(CONFLICT_MARKER), "不得出现伪冲突 marker");
|
|
45
|
+
const file = readFileSync(mirrorFile(dir, "project"), "utf8");
|
|
46
|
+
assert.match(file, /v2/, "镜像已重渲染为新值");
|
|
47
|
+
assert.ok(!file.includes(CONFLICT_MARKER), "镜像不得包含伪冲突 marker");
|
|
48
|
+
} finally {
|
|
49
|
+
rmSync(dir, { recursive: true, force: true });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("digest 匹配:事务内 update 后机器写落地、无伪冲突 marker", () => {
|
|
54
|
+
const { dir, store, service } = setup();
|
|
55
|
+
try {
|
|
56
|
+
const { memory: m } = service.saveWithDedupe({ type: "project", title: "事务更新", content: "tx v1", importance: 3 });
|
|
57
|
+
service.transaction(() => {
|
|
58
|
+
service.update(m.id, { content: "tx v2" });
|
|
59
|
+
});
|
|
60
|
+
assert.equal(store.getById(m.id).content, "tx v2", "事务内机器新值必须落地");
|
|
61
|
+
assert.ok(!store.getById(m.id).content.includes(CONFLICT_MARKER), "不得出现伪冲突 marker");
|
|
62
|
+
const file = readFileSync(mirrorFile(dir, "project"), "utf8");
|
|
63
|
+
assert.match(file, /tx v2/, "镜像已重渲染为新值");
|
|
64
|
+
assert.ok(!file.includes(CONFLICT_MARKER), "镜像不得包含伪冲突 marker");
|
|
65
|
+
} finally {
|
|
66
|
+
rmSync(dir, { recursive: true, force: true });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("digest 匹配:saveWithDedupe 同标题 merge 后新值落地、无伪冲突 marker", () => {
|
|
71
|
+
const { dir, store, service } = setup();
|
|
72
|
+
try {
|
|
73
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "旧内容", importance: 3 });
|
|
74
|
+
const result = service.saveWithDedupe({ type: "preference", title: "语言", content: "新内容", importance: 5 });
|
|
75
|
+
assert.equal(result.action, "merged");
|
|
76
|
+
assert.equal(store.count(), 1, "同标题合并不新增条目");
|
|
77
|
+
const m = service.getById(result.memory.id);
|
|
78
|
+
assert.equal(m.content, "新内容", "合并后新值必须落地");
|
|
79
|
+
assert.ok(!m.content.includes(CONFLICT_MARKER), "不得出现伪冲突 marker");
|
|
80
|
+
const file = readFileSync(mirrorFile(dir, "preference"), "utf8");
|
|
81
|
+
assert.match(file, /新内容/, "镜像已重渲染为新值");
|
|
82
|
+
assert.ok(!file.includes(CONFLICT_MARKER), "镜像不得包含伪冲突 marker");
|
|
83
|
+
} finally {
|
|
84
|
+
rmSync(dir, { recursive: true, force: true });
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("digest 匹配:Dream merge 后 keeper 是新值、无伪冲突、source 归档", () => {
|
|
89
|
+
const { dir, store, service } = setup();
|
|
90
|
+
try {
|
|
91
|
+
const a = service.saveWithDedupe({ type: "project", title: "DreamKeeper", content: "keep old", importance: 3 });
|
|
92
|
+
const b = service.saveWithDedupe({ type: "project", title: "DreamSource", content: "src old", importance: 3 });
|
|
93
|
+
// 真实 applyDecisions 路径:keeper 更新 + source 归档在同一事务里,
|
|
94
|
+
// 提交时 syncMirror 读到的是事务前渲染的旧镜像 → 修复前会误判为人工编辑。
|
|
95
|
+
const { applied, failures } = applyDecisions(
|
|
96
|
+
[{ action: "merge", ids: [a.memory.id, b.memory.id], keepSource: a.memory.id, title: "DreamKeeper", content: "keeper new", importance: 5 }],
|
|
97
|
+
service,
|
|
98
|
+
null,
|
|
99
|
+
null
|
|
100
|
+
);
|
|
101
|
+
assert.equal(applied, 1);
|
|
102
|
+
assert.equal(failures.length, 0);
|
|
103
|
+
const keeper = store.getById(a.memory.id);
|
|
104
|
+
assert.equal(keeper.content, "keeper new", "keeper 必须是合并后的新值");
|
|
105
|
+
assert.equal(keeper.title, "DreamKeeper");
|
|
106
|
+
assert.ok(!keeper.content.includes(CONFLICT_MARKER), "keeper 不得出现伪冲突 marker");
|
|
107
|
+
assert.equal(store.getById(b.memory.id).archived, true, "source 必须归档");
|
|
108
|
+
const file = readFileSync(mirrorFile(dir, "project"), "utf8");
|
|
109
|
+
assert.match(file, /keeper new/, "镜像已重渲染为新值");
|
|
110
|
+
assert.ok(!file.includes(CONFLICT_MARKER), "镜像不得包含伪冲突 marker");
|
|
111
|
+
} finally {
|
|
112
|
+
rmSync(dir, { recursive: true, force: true });
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("真实人工编辑控制组:只改文件 → 人工 wins、无 marker(store 未变)", () => {
|
|
117
|
+
const { dir, store, service } = setup();
|
|
118
|
+
try {
|
|
119
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "机器内容", importance: 3 });
|
|
120
|
+
// 人工改文件内容,但保留 digest 注释行(digest 已不匹配)
|
|
121
|
+
const file = mirrorFile(dir, "preference");
|
|
122
|
+
writeFileSync(file, readFileSync(file, "utf8").replace("机器内容", "人类编辑内容"), "utf8");
|
|
123
|
+
// 下一次无关 store 写触发 syncMirror → 必须合并人工编辑回 store
|
|
124
|
+
service.saveWithDedupe({ type: "project", title: "无关", content: "x", importance: 3 });
|
|
125
|
+
const m = service.list({ type: "preference", includeArchived: true }).find((p) => p.title === "语言");
|
|
126
|
+
assert.equal(m.content, "人类编辑内容", "人工编辑必须合并回 store");
|
|
127
|
+
assert.ok(!m.content.includes(CONFLICT_MARKER), "store 未变时不得出现冲突 marker");
|
|
128
|
+
} finally {
|
|
129
|
+
rmSync(dir, { recursive: true, force: true });
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("真实人工编辑控制组:文件与 store 同时变更 → 三方合并保留双方 + marker", () => {
|
|
134
|
+
const { dir, store, service } = setup();
|
|
135
|
+
try {
|
|
136
|
+
const { memory: m } = service.saveWithDedupe({ type: "preference", title: "语言", content: "机器内容", importance: 3 });
|
|
137
|
+
// 人工改文件(digest 行保留但内容已变)
|
|
138
|
+
const file = mirrorFile(dir, "preference");
|
|
139
|
+
writeFileSync(file, readFileSync(file, "utf8").replace("机器内容", "人类编辑内容"), "utf8");
|
|
140
|
+
// 机器并发改 store → 下一次 sync 必须三方合并,保留双方 + marker
|
|
141
|
+
service.update(m.id, { content: "并发机器版本" });
|
|
142
|
+
const updated = service.getById(m.id);
|
|
143
|
+
assert.ok(updated.content.includes("人类编辑内容"), "人工版本必须保留为头部");
|
|
144
|
+
assert.ok(updated.content.includes("并发机器版本"), "store 并发版本必须保留");
|
|
145
|
+
assert.ok(updated.content.includes(CONFLICT_MARKER), "必须出现真正的冲突 marker");
|
|
146
|
+
} finally {
|
|
147
|
+
rmSync(dir, { recursive: true, force: true });
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("老文件无 digest → 保守走三方合并(保留双方 + marker)", () => {
|
|
152
|
+
const { dir, store, service } = setup();
|
|
153
|
+
try {
|
|
154
|
+
const { memory: m } = service.saveWithDedupe({ type: "history", title: "旧", content: "旧内容", importance: 3 });
|
|
155
|
+
// 模拟修复前渲染的老文件:去掉 digest 注释行,但内容仍是旧内容
|
|
156
|
+
const file = mirrorFile(dir, "history");
|
|
157
|
+
const rendered = readFileSync(file, "utf8");
|
|
158
|
+
writeFileSync(file, rendered.replace(/<!--\s*mirror-digest: [a-f0-9]+ -->\n?/g, ""), "utf8");
|
|
159
|
+
// 机器更新 → 无 digest 必须保守视为人工动过 → 三方合并
|
|
160
|
+
service.update(m.id, { content: "新机器内容" });
|
|
161
|
+
const updated = service.getById(m.id);
|
|
162
|
+
assert.ok(updated.content.includes("旧内容"), "老文件内容必须被保留");
|
|
163
|
+
assert.ok(updated.content.includes("新机器内容"), "机器新版本必须被保留");
|
|
164
|
+
assert.ok(updated.content.includes(CONFLICT_MARKER), "必须出现冲突 marker");
|
|
165
|
+
} finally {
|
|
166
|
+
rmSync(dir, { recursive: true, force: true });
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("digest 从 body 剥除:读回的 content 不含 digest 注释,digest 字段正确", () => {
|
|
171
|
+
const { dir, mirror, service } = setup();
|
|
172
|
+
try {
|
|
173
|
+
const { memory: m } = service.saveWithDedupe({ type: "decision", title: "剥离", content: "line1\nline2", importance: 3 });
|
|
174
|
+
const edits = mirror.readHumanEdits("decision");
|
|
175
|
+
const edit = edits.find((e) => e.id === m.id);
|
|
176
|
+
assert.ok(edit, "读到该条目");
|
|
177
|
+
assert.equal(edit.title, "剥离");
|
|
178
|
+
assert.equal(edit.content, "line1\nline2", "正文必须不含结构字段");
|
|
179
|
+
assert.ok(!edit.content.includes("mirror-digest"), "content 不得包含 digest 注释");
|
|
180
|
+
assert.ok(!edit.content.includes("<!--"), "content 不得包含任何 HTML 注释");
|
|
181
|
+
assert.equal(edit.digest, digestOf("剥离", "line1\nline2"), "digest 字段暴露给 reconcile 使用");
|
|
182
|
+
} finally {
|
|
183
|
+
rmSync(dir, { recursive: true, force: true });
|
|
184
|
+
}
|
|
185
|
+
});
|