@modusensus/dsh-mneme 0.6.6 → 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.
- package/LICENSE +21 -21
- package/README.md +463 -457
- package/cordis.patch.yml +15 -15
- package/lib/api.js +783 -693
- package/lib/client.js +1757 -1656
- package/lib/commands.js +64 -64
- package/lib/config.js +288 -288
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +439 -439
- package/lib/dream/sleep.js +561 -554
- package/lib/dream/tag-extractor.js +156 -156
- package/lib/dream.js +935 -929
- package/lib/embedding.js +154 -154
- package/lib/entities/extractor.js +242 -242
- package/lib/hot-memory.js +53 -53
- package/lib/index.js +361 -361
- package/lib/inject.js +208 -208
- package/lib/local-embedder.js +282 -282
- package/lib/mirror.js +170 -170
- package/lib/parser/tag.js +59 -59
- package/lib/parser/wiki-link.js +38 -38
- package/lib/quality-filter.js +123 -123
- package/lib/reranker.js +218 -218
- package/lib/search/adaptive.js +22 -22
- package/lib/search/bm25.js +96 -96
- package/lib/search/tag-boost.js +61 -61
- package/lib/service.js +1726 -1726
- package/lib/settings.js +172 -142
- package/lib/store.js +2238 -2238
- package/lib/summarize.js +236 -236
- package/lib/tools.js +290 -290
- package/lib/vector-index.js +116 -116
- package/package.json +80 -75
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +133 -133
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/e2e-dsh.js +218 -218
- package/scripts/stress-dsh.js +255 -255
- package/scripts/sync-lib.js +52 -52
- package/src/api.js +783 -693
- package/src/commands.js +64 -64
- package/src/config.js +288 -288
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +439 -439
- package/src/dream/sleep.js +561 -554
- package/src/dream/tag-extractor.js +156 -156
- package/src/dream.js +935 -929
- package/src/embedding.js +154 -154
- package/src/entities/extractor.js +242 -242
- package/src/hot-memory.js +53 -53
- package/src/index.js +361 -361
- package/src/inject.js +208 -208
- package/src/local-embedder.js +282 -282
- package/src/mirror.js +170 -170
- package/src/parser/tag.js +59 -59
- package/src/parser/wiki-link.js +38 -38
- package/src/quality-filter.js +123 -123
- package/src/reranker.js +218 -218
- package/src/search/adaptive.js +22 -22
- package/src/search/bm25.js +96 -96
- package/src/search/tag-boost.js +61 -61
- package/src/service.js +1726 -1726
- package/src/settings.js +172 -142
- package/src/store.js +2238 -2238
- package/src/summarize.js +236 -236
- package/src/tools.js +290 -290
- package/src/vector-index.js +116 -116
- package/test/api.test.js +594 -549
- package/test/audit.test.js +448 -448
- package/test/benchmark.test.js +35 -35
- package/test/boundary-v0625.test.js +82 -82
- package/test/client.test.js +368 -368
- package/test/clustering.test.js +100 -100
- package/test/commands.test.js +69 -69
- package/test/config.test.js +50 -50
- package/test/conflict-freeze.test.js +290 -290
- package/test/directory.test.js +134 -134
- package/test/dream.test.js +903 -901
- package/test/entities.test.js +522 -522
- package/test/epistemic.test.js +298 -298
- package/test/fnew-0112.test.js +311 -311
- package/test/fnew-03.test.js +422 -422
- package/test/graph-api.test.js +175 -175
- package/test/helpers/dream-mock.js +82 -82
- package/test/hot-memory.test.js +174 -174
- package/test/inject.test.js +103 -103
- package/test/llm-audit.test.js +279 -279
- package/test/local-embedder.test.js +227 -227
- package/test/mirror-dirty.test.js +424 -424
- package/test/mirror-edit-digest.test.js +187 -187
- package/test/mirror-generation.test.js +499 -499
- package/test/mirror.test.js +249 -249
- package/test/normalize-decisions.test.js +120 -120
- package/test/peer-blockers.test.js +190 -190
- package/test/policy-epoch.test.js +259 -259
- package/test/provenance.test.js +103 -103
- package/test/quality-filter.test.js +118 -118
- package/test/reasoning-effort.test.js +199 -199
- package/test/recall-evals.test.js +235 -235
- package/test/recall-layer.test.js +315 -315
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reranker.test.js +240 -240
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/service-search.test.js +199 -199
- package/test/service.test.js +435 -435
- package/test/settings.test.js +118 -101
- package/test/sleep.test.js +365 -365
- package/test/store.test.js +436 -436
- package/test/stress.test.js +209 -209
- package/test/summarize.test.js +191 -191
- package/test/tag-boost.test.js +125 -125
- package/test/tag.test.js +312 -312
- package/test/tools.test.js +285 -285
- package/test/vector-index.test.js +221 -221
- package/test/wiki-link.test.js +332 -332
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
// Field-name normalization guard (v0.5.3): thinking-type models
|
|
2
|
-
// (deepseek-v4-flash etc.) sometimes ignore the prompt's exact decision schema
|
|
3
|
-
// and emit alias keys —实测方案 A 输出 "consolidation"/"target_ids"、方案 B
|
|
4
|
-
// 输出 "action"/"targetIds",都不是插件要求的 "action"/"ids"。normalizeDecisions
|
|
5
|
-
// 在 validateDecisions 之前把这些变体重写回规范字段名,让"语义正确但 schema
|
|
6
|
-
// 不听话"的输出仍被应用,而不是整单被拒。
|
|
7
|
-
import test from "node:test";
|
|
8
|
-
import assert from "node:assert/strict";
|
|
9
|
-
import { normalizeDecisions, createDreamScheduler } from "../src/dream.js";
|
|
10
|
-
import { createStore } from "../src/store.js";
|
|
11
|
-
import { createService } from "../src/service.js";
|
|
12
|
-
|
|
13
|
-
test("normalizeDecisions: canonical decisions pass through untouched", () => {
|
|
14
|
-
const raw = [
|
|
15
|
-
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1", title: "t", content: "c", importance: 4, reason: "主题相近" },
|
|
16
|
-
{ action: "conflict", winner: "m3", loser: "m4", reason: "矛盾" },
|
|
17
|
-
{ action: "update", ids: ["m5"], content: "修正" },
|
|
18
|
-
{ action: "archive", ids: ["m6"], reason: "过时" },
|
|
19
|
-
{ action: "create", type: "pattern", title: "新模式", content: "发现", importance: 3 }
|
|
20
|
-
];
|
|
21
|
-
assert.deepEqual(normalizeDecisions(raw), raw);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test("normalizeDecisions: alias keys rewritten to canonical names", () => {
|
|
25
|
-
const raw = [
|
|
26
|
-
{ consolidation: "merge", target_ids: ["m1", "m2"], keep_source: "m1", new_title: "t", new_content: "c", priority: 4, rationale: "主题相近" },
|
|
27
|
-
{ action: "conflict", winner_id: "m3", loser_id: "m4", why: "矛盾" },
|
|
28
|
-
{ action: "update", targetIds: ["m5"], merged_content: "修正" },
|
|
29
|
-
{ action: "archive", memory_ids: ["m6"] }
|
|
30
|
-
];
|
|
31
|
-
assert.deepEqual(normalizeDecisions(raw), [
|
|
32
|
-
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1", title: "t", content: "c", importance: 4, reason: "主题相近" },
|
|
33
|
-
{ action: "conflict", winner: "m3", loser: "m4", reason: "矛盾" },
|
|
34
|
-
{ action: "update", ids: ["m5"], content: "修正" },
|
|
35
|
-
{ action: "archive", ids: ["m6"] }
|
|
36
|
-
]);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("normalizeDecisions: action value synonyms normalized", () => {
|
|
40
|
-
const raw = [
|
|
41
|
-
{ action: "archived", ids: ["m1"], reason: "重复" },
|
|
42
|
-
{ action: "consolidation", target_ids: ["m2", "m3"], keep_source: "m2" },
|
|
43
|
-
{ action: "combine", targetIds: ["m4", "m5"], keeper: "m5" },
|
|
44
|
-
{ action: "Remove", memory_ids: ["m6"] }
|
|
45
|
-
];
|
|
46
|
-
const normalized = normalizeDecisions(raw);
|
|
47
|
-
assert.equal(normalized[0].action, "archive");
|
|
48
|
-
assert.equal(normalized[1].action, "merge");
|
|
49
|
-
assert.equal(normalized[1].ids[0], "m2");
|
|
50
|
-
assert.equal(normalized[2].action, "merge");
|
|
51
|
-
assert.equal(normalized[2].keepSource, "m5");
|
|
52
|
-
assert.equal(normalized[3].action, "archive");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("normalizeDecisions: wrapper object around the array is unwrapped", () => {
|
|
56
|
-
const raw = { consolidation: [{ action: "merge", ids: ["m1"], keepSource: "m1", title: "t", content: "c" }] };
|
|
57
|
-
assert.deepEqual(normalizeDecisions(raw), [
|
|
58
|
-
{ action: "merge", ids: ["m1"], keepSource: "m1", title: "t", content: "c" }
|
|
59
|
-
]);
|
|
60
|
-
const raw2 = { decisions: [{ action: "archive", target_ids: ["m9"] }] };
|
|
61
|
-
assert.deepEqual(normalizeDecisions(raw2), [{ action: "archive", ids: ["m9"] }]);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("normalizeDecisions: single decision object (non-array) normalized too", () => {
|
|
65
|
-
const raw = { action: "update", targetIds: ["m1"], new_content: "修正" };
|
|
66
|
-
assert.deepEqual(normalizeDecisions(raw), [{ action: "update", ids: ["m1"], content: "修正" }]);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("normalizeDecisions: single-string ids become a one-element array", () => {
|
|
70
|
-
const raw = [{ action: "archive", target_id: "m7" }];
|
|
71
|
-
// target_id (singular) is not an alias key — the raw key survives untouched
|
|
72
|
-
// and validateDecisions rejects it (safe side). But targetIds (plural, string)
|
|
73
|
-
// IS mapped and wrapped.
|
|
74
|
-
const raw2 = [{ action: "archive", targetIds: "m7" }];
|
|
75
|
-
assert.deepEqual(normalizeDecisions(raw2), [{ action: "archive", ids: ["m7"] }]);
|
|
76
|
-
assert.deepEqual(normalizeDecisions(raw), [{ action: "archive", target_id: "m7" }]);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("normalizeDecisions: create keeps its type field (never mistaken for action)", () => {
|
|
80
|
-
const raw = { action: "create", type: "preference", title: "语言", content: "中文", importance: 3 };
|
|
81
|
-
assert.deepEqual(normalizeDecisions(raw), [
|
|
82
|
-
{ action: "create", type: "preference", title: "语言", content: "中文", importance: 3 }
|
|
83
|
-
]);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test("normalizeDecisions: null / non-parseable returns as-is (caller's no-json branch)", () => {
|
|
87
|
-
assert.equal(normalizeDecisions(null), null);
|
|
88
|
-
assert.equal(normalizeDecisions(undefined), undefined);
|
|
89
|
-
assert.equal(normalizeDecisions("not json"), "not json");
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test("dream runDream applies alias-key consolidation output end-to-end", async () => {
|
|
93
|
-
const store = createStore(":memory:");
|
|
94
|
-
const service = createService({ store, mirror: null, config: {} });
|
|
95
|
-
const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
|
|
96
|
-
const { memory: a } = service.saveWithDedupe({ type: "project", title: "插件", content: "旧", importance: 3 });
|
|
97
|
-
const { memory: b } = service.saveWithDedupe({ type: "project", title: "插件2", content: "新细节", importance: 4 });
|
|
98
|
-
const ctx = {
|
|
99
|
-
logger: { warn: () => {} },
|
|
100
|
-
agentDefaultModel: { currentSelection: () => ({ provider: "mock", model: "mock-model" }) },
|
|
101
|
-
llm: {
|
|
102
|
-
async *stream(options) {
|
|
103
|
-
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
104
|
-
if (userText.startsWith("id=")) {
|
|
105
|
-
// deepseek-v4-flash 实测风格:consolidation 当 action、target_ids 当 ids
|
|
106
|
-
yield { type: "text-delta", index: 0, text: JSON.stringify([
|
|
107
|
-
{ consolidation: "merge", target_ids: [a.id, b.id], keep_source: b.id, new_title: "插件总览", new_content: "合并内容", priority: 4 }
|
|
108
|
-
]) };
|
|
109
|
-
} else {
|
|
110
|
-
yield { type: "text-delta", index: 0, text: "记忆库总览:默认摘要。" };
|
|
111
|
-
}
|
|
112
|
-
yield { type: "finish", reason: { kind: "stop" } };
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
const result = await dream.runDream(ctx, service, {});
|
|
117
|
-
assert.equal(result.ok, true, "alias-key output must not be rejected wholesale");
|
|
118
|
-
assert.ok(result.applied > 0, "alias-key decisions still land changes");
|
|
119
|
-
store.close();
|
|
120
|
-
});
|
|
1
|
+
// Field-name normalization guard (v0.5.3): thinking-type models
|
|
2
|
+
// (deepseek-v4-flash etc.) sometimes ignore the prompt's exact decision schema
|
|
3
|
+
// and emit alias keys —实测方案 A 输出 "consolidation"/"target_ids"、方案 B
|
|
4
|
+
// 输出 "action"/"targetIds",都不是插件要求的 "action"/"ids"。normalizeDecisions
|
|
5
|
+
// 在 validateDecisions 之前把这些变体重写回规范字段名,让"语义正确但 schema
|
|
6
|
+
// 不听话"的输出仍被应用,而不是整单被拒。
|
|
7
|
+
import test from "node:test";
|
|
8
|
+
import assert from "node:assert/strict";
|
|
9
|
+
import { normalizeDecisions, createDreamScheduler } from "../src/dream.js";
|
|
10
|
+
import { createStore } from "../src/store.js";
|
|
11
|
+
import { createService } from "../src/service.js";
|
|
12
|
+
|
|
13
|
+
test("normalizeDecisions: canonical decisions pass through untouched", () => {
|
|
14
|
+
const raw = [
|
|
15
|
+
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1", title: "t", content: "c", importance: 4, reason: "主题相近" },
|
|
16
|
+
{ action: "conflict", winner: "m3", loser: "m4", reason: "矛盾" },
|
|
17
|
+
{ action: "update", ids: ["m5"], content: "修正" },
|
|
18
|
+
{ action: "archive", ids: ["m6"], reason: "过时" },
|
|
19
|
+
{ action: "create", type: "pattern", title: "新模式", content: "发现", importance: 3 }
|
|
20
|
+
];
|
|
21
|
+
assert.deepEqual(normalizeDecisions(raw), raw);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("normalizeDecisions: alias keys rewritten to canonical names", () => {
|
|
25
|
+
const raw = [
|
|
26
|
+
{ consolidation: "merge", target_ids: ["m1", "m2"], keep_source: "m1", new_title: "t", new_content: "c", priority: 4, rationale: "主题相近" },
|
|
27
|
+
{ action: "conflict", winner_id: "m3", loser_id: "m4", why: "矛盾" },
|
|
28
|
+
{ action: "update", targetIds: ["m5"], merged_content: "修正" },
|
|
29
|
+
{ action: "archive", memory_ids: ["m6"] }
|
|
30
|
+
];
|
|
31
|
+
assert.deepEqual(normalizeDecisions(raw), [
|
|
32
|
+
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1", title: "t", content: "c", importance: 4, reason: "主题相近" },
|
|
33
|
+
{ action: "conflict", winner: "m3", loser: "m4", reason: "矛盾" },
|
|
34
|
+
{ action: "update", ids: ["m5"], content: "修正" },
|
|
35
|
+
{ action: "archive", ids: ["m6"] }
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("normalizeDecisions: action value synonyms normalized", () => {
|
|
40
|
+
const raw = [
|
|
41
|
+
{ action: "archived", ids: ["m1"], reason: "重复" },
|
|
42
|
+
{ action: "consolidation", target_ids: ["m2", "m3"], keep_source: "m2" },
|
|
43
|
+
{ action: "combine", targetIds: ["m4", "m5"], keeper: "m5" },
|
|
44
|
+
{ action: "Remove", memory_ids: ["m6"] }
|
|
45
|
+
];
|
|
46
|
+
const normalized = normalizeDecisions(raw);
|
|
47
|
+
assert.equal(normalized[0].action, "archive");
|
|
48
|
+
assert.equal(normalized[1].action, "merge");
|
|
49
|
+
assert.equal(normalized[1].ids[0], "m2");
|
|
50
|
+
assert.equal(normalized[2].action, "merge");
|
|
51
|
+
assert.equal(normalized[2].keepSource, "m5");
|
|
52
|
+
assert.equal(normalized[3].action, "archive");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("normalizeDecisions: wrapper object around the array is unwrapped", () => {
|
|
56
|
+
const raw = { consolidation: [{ action: "merge", ids: ["m1"], keepSource: "m1", title: "t", content: "c" }] };
|
|
57
|
+
assert.deepEqual(normalizeDecisions(raw), [
|
|
58
|
+
{ action: "merge", ids: ["m1"], keepSource: "m1", title: "t", content: "c" }
|
|
59
|
+
]);
|
|
60
|
+
const raw2 = { decisions: [{ action: "archive", target_ids: ["m9"] }] };
|
|
61
|
+
assert.deepEqual(normalizeDecisions(raw2), [{ action: "archive", ids: ["m9"] }]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("normalizeDecisions: single decision object (non-array) normalized too", () => {
|
|
65
|
+
const raw = { action: "update", targetIds: ["m1"], new_content: "修正" };
|
|
66
|
+
assert.deepEqual(normalizeDecisions(raw), [{ action: "update", ids: ["m1"], content: "修正" }]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("normalizeDecisions: single-string ids become a one-element array", () => {
|
|
70
|
+
const raw = [{ action: "archive", target_id: "m7" }];
|
|
71
|
+
// target_id (singular) is not an alias key — the raw key survives untouched
|
|
72
|
+
// and validateDecisions rejects it (safe side). But targetIds (plural, string)
|
|
73
|
+
// IS mapped and wrapped.
|
|
74
|
+
const raw2 = [{ action: "archive", targetIds: "m7" }];
|
|
75
|
+
assert.deepEqual(normalizeDecisions(raw2), [{ action: "archive", ids: ["m7"] }]);
|
|
76
|
+
assert.deepEqual(normalizeDecisions(raw), [{ action: "archive", target_id: "m7" }]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("normalizeDecisions: create keeps its type field (never mistaken for action)", () => {
|
|
80
|
+
const raw = { action: "create", type: "preference", title: "语言", content: "中文", importance: 3 };
|
|
81
|
+
assert.deepEqual(normalizeDecisions(raw), [
|
|
82
|
+
{ action: "create", type: "preference", title: "语言", content: "中文", importance: 3 }
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("normalizeDecisions: null / non-parseable returns as-is (caller's no-json branch)", () => {
|
|
87
|
+
assert.equal(normalizeDecisions(null), null);
|
|
88
|
+
assert.equal(normalizeDecisions(undefined), undefined);
|
|
89
|
+
assert.equal(normalizeDecisions("not json"), "not json");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("dream runDream applies alias-key consolidation output end-to-end", async () => {
|
|
93
|
+
const store = createStore(":memory:");
|
|
94
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
95
|
+
const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
|
|
96
|
+
const { memory: a } = service.saveWithDedupe({ type: "project", title: "插件", content: "旧", importance: 3 });
|
|
97
|
+
const { memory: b } = service.saveWithDedupe({ type: "project", title: "插件2", content: "新细节", importance: 4 });
|
|
98
|
+
const ctx = {
|
|
99
|
+
logger: { warn: () => {} },
|
|
100
|
+
agentDefaultModel: { currentSelection: () => ({ provider: "mock", model: "mock-model" }) },
|
|
101
|
+
llm: {
|
|
102
|
+
async *stream(options) {
|
|
103
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
104
|
+
if (userText.startsWith("id=")) {
|
|
105
|
+
// deepseek-v4-flash 实测风格:consolidation 当 action、target_ids 当 ids
|
|
106
|
+
yield { type: "text-delta", index: 0, text: JSON.stringify([
|
|
107
|
+
{ consolidation: "merge", target_ids: [a.id, b.id], keep_source: b.id, new_title: "插件总览", new_content: "合并内容", priority: 4 }
|
|
108
|
+
]) };
|
|
109
|
+
} else {
|
|
110
|
+
yield { type: "text-delta", index: 0, text: "记忆库总览:默认摘要。" };
|
|
111
|
+
}
|
|
112
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const result = await dream.runDream(ctx, service, {});
|
|
117
|
+
assert.equal(result.ok, true, "alias-key output must not be rejected wholesale");
|
|
118
|
+
assert.ok(result.applied > 0, "alias-key decisions still land changes");
|
|
119
|
+
store.close();
|
|
120
|
+
});
|
|
@@ -1,190 +1,190 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { execFile } from "node:child_process";
|
|
8
|
-
import { promisify } from "node:util";
|
|
9
|
-
import { createStore } from "../src/store.js";
|
|
10
|
-
import { createMirror } from "../src/mirror.js";
|
|
11
|
-
import { createService } from "../src/service.js";
|
|
12
|
-
|
|
13
|
-
const execFileP = promisify(execFile);
|
|
14
|
-
const STORE_PATH = fileURLToPath(new URL("../src/store.js", import.meta.url));
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* v0.3.8 回归测试(audit peer 6 项运行时阻断 → INSTALLATION_NOT_APPROVED)。
|
|
18
|
-
*
|
|
19
|
-
* 测试点:
|
|
20
|
-
* A. 崩溃窗口(真实语义):store.save 在业务事务内原子递增 desired generation,
|
|
21
|
-
* 崩溃在 COMMIT 后、syncMirror 前 → 重启 recoverMirror 仅凭
|
|
22
|
-
* generation > applied_generation 捕获并收敛(peer blocker 1)
|
|
23
|
-
* B. 业务写同事务原子性:INSERT 失败回滚时 generation 不得递增(peer blocker 1/3)
|
|
24
|
-
* C. 多进程并发原子递增:8 进程 × 10 次不丢增量(peer blocker 3)
|
|
25
|
-
* D. generation 上界/负数拒绝(peer blocker 6)
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
function setup(dbPath) {
|
|
29
|
-
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-peer-"));
|
|
30
|
-
const mirrorDir = join(dir, "mirror");
|
|
31
|
-
const store = createStore(dbPath ?? ":memory:");
|
|
32
|
-
const mirror = createMirror(mirrorDir);
|
|
33
|
-
const warns = [];
|
|
34
|
-
const logger = { warn: (...a) => warns.push(a.join(" ")) };
|
|
35
|
-
const service = createService({ store, mirror, config: {}, logger });
|
|
36
|
-
return { dir, mirrorDir, store, mirror, service, warns };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
test("peer-A: 崩溃窗口——save 后(generation 已递增)不 sync 直接关闭重开,recoverMirror 捕获并收敛", () => {
|
|
40
|
-
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-peer-"));
|
|
41
|
-
const dbPath = join(dir, "mneme.db");
|
|
42
|
-
const mirrorDir = join(dir, "mirror");
|
|
43
|
-
try {
|
|
44
|
-
const store = createStore(dbPath);
|
|
45
|
-
const mirror = createMirror(mirrorDir);
|
|
46
|
-
const service = createService({ store, mirror, config: {}, logger: { warn() {} } });
|
|
47
|
-
try {
|
|
48
|
-
// 业务写:store.save 在事务内递增 desired generation,但不触发 mirror 渲染
|
|
49
|
-
store.save({ type: "project", title: "P", content: "x", importance: 3 });
|
|
50
|
-
const afterSave = store.getMirrorState();
|
|
51
|
-
assert.ok(afterSave.generation > afterSave.applied_generation,
|
|
52
|
-
"save 后 generation > applied(COMMIT 完成,mirror 未同步=崩溃窗口)");
|
|
53
|
-
} finally {
|
|
54
|
-
// 模拟崩溃:不调 syncMirror,直接关库
|
|
55
|
-
store.close();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 重启:重新打开同一 DB,recoverMirror 必须捕获债务并收敛
|
|
59
|
-
const store2 = createStore(dbPath);
|
|
60
|
-
const mirror2 = createMirror(mirrorDir);
|
|
61
|
-
const service2 = createService({ store: store2, mirror: mirror2, config: {}, logger: { warn() {} } });
|
|
62
|
-
try {
|
|
63
|
-
const result = service2.recoverMirror();
|
|
64
|
-
assert.equal(result.recovered, true, "崩溃窗口必须被 recover 捕获");
|
|
65
|
-
assert.equal(result.error, null);
|
|
66
|
-
const state = store2.getMirrorState();
|
|
67
|
-
assert.equal(state.dirty, false, "收敛后 dirty 清");
|
|
68
|
-
assert.ok(state.generation <= state.applied_generation, "收敛后无未应用债务");
|
|
69
|
-
} finally {
|
|
70
|
-
store2.close();
|
|
71
|
-
}
|
|
72
|
-
} finally {
|
|
73
|
-
rmSync(dir, { recursive: true, force: true });
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test("peer-B: 业务写同事务原子性——INSERT 失败回滚时 generation 不得递增", () => {
|
|
78
|
-
const { dir, store } = setup();
|
|
79
|
-
try {
|
|
80
|
-
store.save({ id: "dup", type: "project", title: "A", content: "x", importance: 3 });
|
|
81
|
-
const before = store.getMirrorState().generation;
|
|
82
|
-
assert.throws(
|
|
83
|
-
() => store.save({ id: "dup", type: "project", title: "B", content: "y", importance: 3 }),
|
|
84
|
-
/UNIQUE|constraint/i,
|
|
85
|
-
"重复主键 INSERT 必须抛错"
|
|
86
|
-
);
|
|
87
|
-
assert.equal(store.getMirrorState().generation, before,
|
|
88
|
-
"回滚后 generation 不递增(写与 desired generation 同事务,失败一起回滚)");
|
|
89
|
-
} finally {
|
|
90
|
-
store.close();
|
|
91
|
-
rmSync(dir, { recursive: true, force: true });
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("peer-C: 多进程并发原子递增——8 进程×10 次 incrementGeneration 不丢增量", async () => {
|
|
96
|
-
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-peer-"));
|
|
97
|
-
const dbPath = join(dir, "concurrent.db");
|
|
98
|
-
const N = 8;
|
|
99
|
-
const M = 10;
|
|
100
|
-
try {
|
|
101
|
-
// 子进程脚本:打开同一 DB 文件,原子递增 M 次
|
|
102
|
-
const worker = `
|
|
103
|
-
const { createStore } = require(process.argv[1]);
|
|
104
|
-
const store = createStore(process.argv[2]);
|
|
105
|
-
for (let i = 0; i < ${M}; i++) { store.incrementGeneration(); }
|
|
106
|
-
store.close();
|
|
107
|
-
`;
|
|
108
|
-
await Promise.all(
|
|
109
|
-
Array.from({ length: N }, () =>
|
|
110
|
-
execFileP(process.execPath, ["-e", worker, STORE_PATH, dbPath], { timeout: 30000 })
|
|
111
|
-
)
|
|
112
|
-
);
|
|
113
|
-
const store = createStore(dbPath);
|
|
114
|
-
try {
|
|
115
|
-
const state = store.getMirrorState();
|
|
116
|
-
assert.equal(state.generation, N * M,
|
|
117
|
-
`并发 ${N} 进程 × ${M} 次必须无丢失增量,得到 ${state.generation}`);
|
|
118
|
-
} finally {
|
|
119
|
-
store.close();
|
|
120
|
-
}
|
|
121
|
-
} finally {
|
|
122
|
-
rmSync(dir, { recursive: true, force: true });
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test("peer-D: generation 上界与负数拒绝", () => {
|
|
127
|
-
const { dir, store } = setup();
|
|
128
|
-
try {
|
|
129
|
-
// 负数拒绝
|
|
130
|
-
assert.throws(() => store.setMirrorState({ generation: -1 }), RangeError, "负数必须拒绝");
|
|
131
|
-
assert.throws(() => store.setMirrorState({ applied_generation: -5 }), RangeError, "负数 applied 必须拒绝");
|
|
132
|
-
// 超 MAX_SAFE_INTEGER 拒绝
|
|
133
|
-
assert.throws(
|
|
134
|
-
() => store.setMirrorState({ generation: Number.MAX_SAFE_INTEGER + 1 }),
|
|
135
|
-
RangeError,
|
|
136
|
-
"超出 MAX_SAFE_INTEGER 必须拒绝"
|
|
137
|
-
);
|
|
138
|
-
// 到上界后再 increment 必须抛错(读回不会 ERR_OUT_OF_RANGE)
|
|
139
|
-
store.setMirrorState({ generation: Number.MAX_SAFE_INTEGER });
|
|
140
|
-
assert.throws(() => store.incrementGeneration(), /exceeded MAX_SAFE_INTEGER/, "上界后再递增必须抛错");
|
|
141
|
-
// 正常递增仍工作
|
|
142
|
-
store.setMirrorState({ generation: 5 });
|
|
143
|
-
assert.equal(store.incrementGeneration().generation, 6, "正常递增不受影响");
|
|
144
|
-
} finally {
|
|
145
|
-
store.close();
|
|
146
|
-
rmSync(dir, { recursive: true, force: true });
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
test("v0.3.9-A: compareAndUpdate 的 CAS UPDATE 与 generation 同事务——miss 不得递增", () => {
|
|
151
|
-
const { dir, store } = setup();
|
|
152
|
-
try {
|
|
153
|
-
const saved = store.save({ type: "project", title: "CAS 原子", content: "v0" });
|
|
154
|
-
const before = store.getById(saved.id);
|
|
155
|
-
const genBefore = store.getMirrorState().generation;
|
|
156
|
-
|
|
157
|
-
// 成功 CAS:业务写入 + generation 递增必须一次提交(同事务)
|
|
158
|
-
const updated = store.compareAndUpdate(saved.id, before.updated_at, { content: "v1" });
|
|
159
|
-
assert.ok(updated, "当前版本 CAS 必须成功");
|
|
160
|
-
const genAfterOk = store.getMirrorState().generation;
|
|
161
|
-
assert.equal(genAfterOk, genBefore + 1, "成功 CAS 必须恰好递增一次 generation");
|
|
162
|
-
|
|
163
|
-
// miss CAS:不写任何东西,generation 也不得递增
|
|
164
|
-
const stale = store.getById(saved.id).updated_at; // v1 的 token
|
|
165
|
-
store.compareAndUpdate(saved.id, before.updated_at, { content: "v2" }); // 用旧 token → miss
|
|
166
|
-
assert.equal(store.getById(saved.id).content, "v1", "miss 不得改数据");
|
|
167
|
-
assert.equal(store.getMirrorState().generation, genAfterOk,
|
|
168
|
-
"CAS miss 不得递增 generation(UPDATE 与 increment 必须同事务)");
|
|
169
|
-
} finally {
|
|
170
|
-
store.close();
|
|
171
|
-
rmSync(dir, { recursive: true, force: true });
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test("v0.3.9-F: generation 非整数必须拒绝(不得截断)", () => {
|
|
176
|
-
const { dir, store } = setup();
|
|
177
|
-
try {
|
|
178
|
-
// 审计 peer F:1.5 这类小数此前被 Math.trunc 截断 + SQLite CHECK 接受 → 静默脏值。
|
|
179
|
-
// fail-closed:JS 与 SQL 统一只接受整数。
|
|
180
|
-
assert.throws(() => store.setMirrorState({ generation: 1.5 }), RangeError, "小数 generation 必须拒绝");
|
|
181
|
-
assert.throws(() => store.setMirrorState({ applied_generation: -1.5 }), RangeError, "负数小数必须拒绝");
|
|
182
|
-
assert.throws(() => store.setMirrorState({ generation: Number.MAX_SAFE_INTEGER + 0.5 }), RangeError, "超界小数必须拒绝");
|
|
183
|
-
// 整数仍正常
|
|
184
|
-
const s = store.setMirrorState({ generation: 7 });
|
|
185
|
-
assert.equal(s.generation, 7, "整数 generation 正常");
|
|
186
|
-
} finally {
|
|
187
|
-
store.close();
|
|
188
|
-
rmSync(dir, { recursive: true, force: true });
|
|
189
|
-
}
|
|
190
|
-
});
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { execFile } from "node:child_process";
|
|
8
|
+
import { promisify } from "node:util";
|
|
9
|
+
import { createStore } from "../src/store.js";
|
|
10
|
+
import { createMirror } from "../src/mirror.js";
|
|
11
|
+
import { createService } from "../src/service.js";
|
|
12
|
+
|
|
13
|
+
const execFileP = promisify(execFile);
|
|
14
|
+
const STORE_PATH = fileURLToPath(new URL("../src/store.js", import.meta.url));
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* v0.3.8 回归测试(audit peer 6 项运行时阻断 → INSTALLATION_NOT_APPROVED)。
|
|
18
|
+
*
|
|
19
|
+
* 测试点:
|
|
20
|
+
* A. 崩溃窗口(真实语义):store.save 在业务事务内原子递增 desired generation,
|
|
21
|
+
* 崩溃在 COMMIT 后、syncMirror 前 → 重启 recoverMirror 仅凭
|
|
22
|
+
* generation > applied_generation 捕获并收敛(peer blocker 1)
|
|
23
|
+
* B. 业务写同事务原子性:INSERT 失败回滚时 generation 不得递增(peer blocker 1/3)
|
|
24
|
+
* C. 多进程并发原子递增:8 进程 × 10 次不丢增量(peer blocker 3)
|
|
25
|
+
* D. generation 上界/负数拒绝(peer blocker 6)
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
function setup(dbPath) {
|
|
29
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-peer-"));
|
|
30
|
+
const mirrorDir = join(dir, "mirror");
|
|
31
|
+
const store = createStore(dbPath ?? ":memory:");
|
|
32
|
+
const mirror = createMirror(mirrorDir);
|
|
33
|
+
const warns = [];
|
|
34
|
+
const logger = { warn: (...a) => warns.push(a.join(" ")) };
|
|
35
|
+
const service = createService({ store, mirror, config: {}, logger });
|
|
36
|
+
return { dir, mirrorDir, store, mirror, service, warns };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test("peer-A: 崩溃窗口——save 后(generation 已递增)不 sync 直接关闭重开,recoverMirror 捕获并收敛", () => {
|
|
40
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-peer-"));
|
|
41
|
+
const dbPath = join(dir, "mneme.db");
|
|
42
|
+
const mirrorDir = join(dir, "mirror");
|
|
43
|
+
try {
|
|
44
|
+
const store = createStore(dbPath);
|
|
45
|
+
const mirror = createMirror(mirrorDir);
|
|
46
|
+
const service = createService({ store, mirror, config: {}, logger: { warn() {} } });
|
|
47
|
+
try {
|
|
48
|
+
// 业务写:store.save 在事务内递增 desired generation,但不触发 mirror 渲染
|
|
49
|
+
store.save({ type: "project", title: "P", content: "x", importance: 3 });
|
|
50
|
+
const afterSave = store.getMirrorState();
|
|
51
|
+
assert.ok(afterSave.generation > afterSave.applied_generation,
|
|
52
|
+
"save 后 generation > applied(COMMIT 完成,mirror 未同步=崩溃窗口)");
|
|
53
|
+
} finally {
|
|
54
|
+
// 模拟崩溃:不调 syncMirror,直接关库
|
|
55
|
+
store.close();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 重启:重新打开同一 DB,recoverMirror 必须捕获债务并收敛
|
|
59
|
+
const store2 = createStore(dbPath);
|
|
60
|
+
const mirror2 = createMirror(mirrorDir);
|
|
61
|
+
const service2 = createService({ store: store2, mirror: mirror2, config: {}, logger: { warn() {} } });
|
|
62
|
+
try {
|
|
63
|
+
const result = service2.recoverMirror();
|
|
64
|
+
assert.equal(result.recovered, true, "崩溃窗口必须被 recover 捕获");
|
|
65
|
+
assert.equal(result.error, null);
|
|
66
|
+
const state = store2.getMirrorState();
|
|
67
|
+
assert.equal(state.dirty, false, "收敛后 dirty 清");
|
|
68
|
+
assert.ok(state.generation <= state.applied_generation, "收敛后无未应用债务");
|
|
69
|
+
} finally {
|
|
70
|
+
store2.close();
|
|
71
|
+
}
|
|
72
|
+
} finally {
|
|
73
|
+
rmSync(dir, { recursive: true, force: true });
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("peer-B: 业务写同事务原子性——INSERT 失败回滚时 generation 不得递增", () => {
|
|
78
|
+
const { dir, store } = setup();
|
|
79
|
+
try {
|
|
80
|
+
store.save({ id: "dup", type: "project", title: "A", content: "x", importance: 3 });
|
|
81
|
+
const before = store.getMirrorState().generation;
|
|
82
|
+
assert.throws(
|
|
83
|
+
() => store.save({ id: "dup", type: "project", title: "B", content: "y", importance: 3 }),
|
|
84
|
+
/UNIQUE|constraint/i,
|
|
85
|
+
"重复主键 INSERT 必须抛错"
|
|
86
|
+
);
|
|
87
|
+
assert.equal(store.getMirrorState().generation, before,
|
|
88
|
+
"回滚后 generation 不递增(写与 desired generation 同事务,失败一起回滚)");
|
|
89
|
+
} finally {
|
|
90
|
+
store.close();
|
|
91
|
+
rmSync(dir, { recursive: true, force: true });
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("peer-C: 多进程并发原子递增——8 进程×10 次 incrementGeneration 不丢增量", async () => {
|
|
96
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-peer-"));
|
|
97
|
+
const dbPath = join(dir, "concurrent.db");
|
|
98
|
+
const N = 8;
|
|
99
|
+
const M = 10;
|
|
100
|
+
try {
|
|
101
|
+
// 子进程脚本:打开同一 DB 文件,原子递增 M 次
|
|
102
|
+
const worker = `
|
|
103
|
+
const { createStore } = require(process.argv[1]);
|
|
104
|
+
const store = createStore(process.argv[2]);
|
|
105
|
+
for (let i = 0; i < ${M}; i++) { store.incrementGeneration(); }
|
|
106
|
+
store.close();
|
|
107
|
+
`;
|
|
108
|
+
await Promise.all(
|
|
109
|
+
Array.from({ length: N }, () =>
|
|
110
|
+
execFileP(process.execPath, ["-e", worker, STORE_PATH, dbPath], { timeout: 30000 })
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
const store = createStore(dbPath);
|
|
114
|
+
try {
|
|
115
|
+
const state = store.getMirrorState();
|
|
116
|
+
assert.equal(state.generation, N * M,
|
|
117
|
+
`并发 ${N} 进程 × ${M} 次必须无丢失增量,得到 ${state.generation}`);
|
|
118
|
+
} finally {
|
|
119
|
+
store.close();
|
|
120
|
+
}
|
|
121
|
+
} finally {
|
|
122
|
+
rmSync(dir, { recursive: true, force: true });
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("peer-D: generation 上界与负数拒绝", () => {
|
|
127
|
+
const { dir, store } = setup();
|
|
128
|
+
try {
|
|
129
|
+
// 负数拒绝
|
|
130
|
+
assert.throws(() => store.setMirrorState({ generation: -1 }), RangeError, "负数必须拒绝");
|
|
131
|
+
assert.throws(() => store.setMirrorState({ applied_generation: -5 }), RangeError, "负数 applied 必须拒绝");
|
|
132
|
+
// 超 MAX_SAFE_INTEGER 拒绝
|
|
133
|
+
assert.throws(
|
|
134
|
+
() => store.setMirrorState({ generation: Number.MAX_SAFE_INTEGER + 1 }),
|
|
135
|
+
RangeError,
|
|
136
|
+
"超出 MAX_SAFE_INTEGER 必须拒绝"
|
|
137
|
+
);
|
|
138
|
+
// 到上界后再 increment 必须抛错(读回不会 ERR_OUT_OF_RANGE)
|
|
139
|
+
store.setMirrorState({ generation: Number.MAX_SAFE_INTEGER });
|
|
140
|
+
assert.throws(() => store.incrementGeneration(), /exceeded MAX_SAFE_INTEGER/, "上界后再递增必须抛错");
|
|
141
|
+
// 正常递增仍工作
|
|
142
|
+
store.setMirrorState({ generation: 5 });
|
|
143
|
+
assert.equal(store.incrementGeneration().generation, 6, "正常递增不受影响");
|
|
144
|
+
} finally {
|
|
145
|
+
store.close();
|
|
146
|
+
rmSync(dir, { recursive: true, force: true });
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("v0.3.9-A: compareAndUpdate 的 CAS UPDATE 与 generation 同事务——miss 不得递增", () => {
|
|
151
|
+
const { dir, store } = setup();
|
|
152
|
+
try {
|
|
153
|
+
const saved = store.save({ type: "project", title: "CAS 原子", content: "v0" });
|
|
154
|
+
const before = store.getById(saved.id);
|
|
155
|
+
const genBefore = store.getMirrorState().generation;
|
|
156
|
+
|
|
157
|
+
// 成功 CAS:业务写入 + generation 递增必须一次提交(同事务)
|
|
158
|
+
const updated = store.compareAndUpdate(saved.id, before.updated_at, { content: "v1" });
|
|
159
|
+
assert.ok(updated, "当前版本 CAS 必须成功");
|
|
160
|
+
const genAfterOk = store.getMirrorState().generation;
|
|
161
|
+
assert.equal(genAfterOk, genBefore + 1, "成功 CAS 必须恰好递增一次 generation");
|
|
162
|
+
|
|
163
|
+
// miss CAS:不写任何东西,generation 也不得递增
|
|
164
|
+
const stale = store.getById(saved.id).updated_at; // v1 的 token
|
|
165
|
+
store.compareAndUpdate(saved.id, before.updated_at, { content: "v2" }); // 用旧 token → miss
|
|
166
|
+
assert.equal(store.getById(saved.id).content, "v1", "miss 不得改数据");
|
|
167
|
+
assert.equal(store.getMirrorState().generation, genAfterOk,
|
|
168
|
+
"CAS miss 不得递增 generation(UPDATE 与 increment 必须同事务)");
|
|
169
|
+
} finally {
|
|
170
|
+
store.close();
|
|
171
|
+
rmSync(dir, { recursive: true, force: true });
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("v0.3.9-F: generation 非整数必须拒绝(不得截断)", () => {
|
|
176
|
+
const { dir, store } = setup();
|
|
177
|
+
try {
|
|
178
|
+
// 审计 peer F:1.5 这类小数此前被 Math.trunc 截断 + SQLite CHECK 接受 → 静默脏值。
|
|
179
|
+
// fail-closed:JS 与 SQL 统一只接受整数。
|
|
180
|
+
assert.throws(() => store.setMirrorState({ generation: 1.5 }), RangeError, "小数 generation 必须拒绝");
|
|
181
|
+
assert.throws(() => store.setMirrorState({ applied_generation: -1.5 }), RangeError, "负数小数必须拒绝");
|
|
182
|
+
assert.throws(() => store.setMirrorState({ generation: Number.MAX_SAFE_INTEGER + 0.5 }), RangeError, "超界小数必须拒绝");
|
|
183
|
+
// 整数仍正常
|
|
184
|
+
const s = store.setMirrorState({ generation: 7 });
|
|
185
|
+
assert.equal(s.generation, 7, "整数 generation 正常");
|
|
186
|
+
} finally {
|
|
187
|
+
store.close();
|
|
188
|
+
rmSync(dir, { recursive: true, force: true });
|
|
189
|
+
}
|
|
190
|
+
});
|