@modusensus/dsh-mneme 0.7.21 → 0.7.23
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.en.md +10 -7
- package/README.md +11 -7
- package/bin/cli.mjs +0 -0
- package/lib/config.js +13 -1
- package/lib/dream/decisions.js +114 -75
- package/lib/dream.js +23 -7
- package/lib/index.js +5 -1
- package/lib/settings.js +6 -1
- package/package.json +1 -1
- package/src/config.js +13 -1
- package/src/dream/decisions.js +114 -75
- package/src/dream.js +23 -7
- package/src/index.js +5 -1
- package/src/settings.js +6 -1
- package/test/api.test.js +10 -4
- package/test/dream.test.js +182 -5
- package/test/reasoning-effort.test.js +68 -3
- package/test/skip-invalid-restore.test.js +260 -0
package/test/dream.test.js
CHANGED
|
@@ -62,10 +62,13 @@ test("archived or summary entries cannot be decision targets", () => {
|
|
|
62
62
|
assert.equal(ok, false);
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
-
test("empty decision list
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
test("empty decision list is a no-op success (model: nothing to consolidate)", () => {
|
|
66
|
+
// 合法 JSON [] 是模型完整评估后确认无需操作(CONSOLIDATION_PROMPT 允许空输出),
|
|
67
|
+
// 不是空体/截断——显式短路 ok,避免隐式 keep 的覆盖率检查误判 0% 为失败。
|
|
68
|
+
const { ok, errors, skipped } = validateDecisions([], snapshot(["a"]));
|
|
69
|
+
assert.equal(ok, true);
|
|
70
|
+
assert.deepEqual(errors, []);
|
|
71
|
+
assert.deepEqual(skipped, []);
|
|
69
72
|
});
|
|
70
73
|
|
|
71
74
|
test("empty ids rejects", () => {
|
|
@@ -825,7 +828,9 @@ test("consolidation prompt pins the decision schema (action field, single-string
|
|
|
825
828
|
logger: { warn: () => {} },
|
|
826
829
|
llm: {
|
|
827
830
|
async *stream(options) {
|
|
828
|
-
|
|
831
|
+
// 只捕获第一次调用(consolidation)的 system 文本:空数组现在走 no-op 成功、
|
|
832
|
+
// summary 会照常跑并覆盖 systemText,所以最后一次调用捕获到的是 SUMMARY_PROMPT。
|
|
833
|
+
if (!systemText) systemText = options.messages.find((m) => m.role === "system")?.content?.[0]?.text ?? "";
|
|
829
834
|
yield { type: "text-delta", text: "[]" };
|
|
830
835
|
yield { type: "finish", reason: { kind: "ok" } };
|
|
831
836
|
}
|
|
@@ -899,3 +904,175 @@ test("Bug8: a failed LLM call is recorded with status=error and does not block t
|
|
|
899
904
|
assert.ok(rows[0].error_message, "error message recorded");
|
|
900
905
|
store.close();
|
|
901
906
|
});
|
|
907
|
+
|
|
908
|
+
// --- Issue #89 回归修复:v0.6.9(Issue #26)的 skipInvalid 宽容路径在 v0.7.11
|
|
909
|
+
// 重写中丢失,弱模型(如 qwen3.8-flash)单条非法决策导致整单拒绝、合法子集
|
|
910
|
+
// 全部白烧。以下单测自 v0.6.9 测试原样移植,锁定恢复后的行为。-------------
|
|
911
|
+
|
|
912
|
+
test("validateDecisions skipInvalid: a single invalid decision is skipped, valid subset survives", () => {
|
|
913
|
+
const snap = new Map([
|
|
914
|
+
["p", { id: "p", type: "preference", title: "语言", content: "中文", importance: 3, archived: false, forgotten: false }],
|
|
915
|
+
["j", { id: "j", type: "project", title: "插件", content: "内容", importance: 3, archived: false, forgotten: false }],
|
|
916
|
+
["a", { id: "a", type: "project", title: "旧A", content: "过时A", importance: 3, archived: false, forgotten: false }],
|
|
917
|
+
["b", { id: "b", type: "project", title: "旧B", content: "过时B", importance: 3, archived: false, forgotten: false }]
|
|
918
|
+
]);
|
|
919
|
+
const decisions = [
|
|
920
|
+
{ action: "merge", ids: ["p", "j"], keepSource: "p", title: "跨类型", content: "不应合并", importance: 4 },
|
|
921
|
+
{ action: "archive", ids: ["a"], reason: "stale" },
|
|
922
|
+
{ action: "archive", ids: ["b"], reason: "stale" }
|
|
923
|
+
];
|
|
924
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
925
|
+
assert.equal(ok, true, `valid subset should survive, got: ${errors.join("; ")}`);
|
|
926
|
+
assert.equal(skipped.length, 1, "cross-type merge recorded as skipped");
|
|
927
|
+
assert.equal(skipped[0].index, 0, "the skipped one is the cross-type merge");
|
|
928
|
+
assert.match(skipped[0].error, /multiple types/, "skip reason mentions types");
|
|
929
|
+
// invalid merge spliced out of the caller's array; valid archives + implicit
|
|
930
|
+
// keeps for p/j survive (p/j were left unclaimed by the skipped merge)
|
|
931
|
+
assert.deepEqual(decisions.map((d) => d.action), ["archive", "archive", "keep", "keep"]);
|
|
932
|
+
assert.deepEqual(decisions[0].ids, ["a"]);
|
|
933
|
+
assert.ok(decisions.some((d) => d.action === "keep" && d.ids.includes("p")), "p auto-kept");
|
|
934
|
+
assert.ok(decisions.some((d) => d.action === "keep" && d.ids.includes("j")), "j auto-kept");
|
|
935
|
+
});
|
|
936
|
+
|
|
937
|
+
test("validateDecisions skipInvalid: an all-invalid batch still rejects (coverage floor guards truncation)", () => {
|
|
938
|
+
const snap = new Map([
|
|
939
|
+
["p", { id: "p", type: "preference", title: "语言", content: "中文", importance: 3, archived: false, forgotten: false }],
|
|
940
|
+
["j", { id: "j", type: "project", title: "插件", content: "内容", importance: 3, archived: false, forgotten: false }],
|
|
941
|
+
["d1", { id: "d1", type: "decision", title: "决定", content: "内容D", importance: 3, archived: false, forgotten: false }],
|
|
942
|
+
["b", { id: "b", type: "project", title: "旧B", content: "过时B", importance: 3, archived: false, forgotten: false }]
|
|
943
|
+
]);
|
|
944
|
+
// both decisions are cross-type merges → both skipped → valid claims = 0
|
|
945
|
+
const decisions = [
|
|
946
|
+
{ action: "merge", ids: ["p", "j"], keepSource: "p", title: "跨类型", content: "不应合并", importance: 4 },
|
|
947
|
+
{ action: "merge", ids: ["d1", "b"], keepSource: "d1", title: "跨类型2", content: "不应合并", importance: 4 }
|
|
948
|
+
];
|
|
949
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
950
|
+
assert.equal(ok, false, "no valid decisions left → whole batch rejected");
|
|
951
|
+
assert.equal(skipped.length, 2);
|
|
952
|
+
assert.ok(errors.some((e) => e.includes("coverage")), "coverage error present");
|
|
953
|
+
assert.equal(decisions.length, 2, "rejected batch left untouched (splice only on the success path)");
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
test("validateDecisions skipInvalid: runaway update count still rejects the whole batch (global cap)", () => {
|
|
957
|
+
const snap = new Map([
|
|
958
|
+
["a", { id: "a", type: "project", title: "A", content: "旧A", importance: 3, archived: false, forgotten: false, created_at: "2020-01-01T00:00:00.000Z" }],
|
|
959
|
+
["b", { id: "b", type: "project", title: "B", content: "旧B", importance: 3, archived: false, forgotten: false, created_at: "2020-01-01T00:00:00.000Z" }],
|
|
960
|
+
["c", { id: "c", type: "project", title: "C", content: "旧C", importance: 3, archived: false, forgotten: false, created_at: "2020-01-01T00:00:00.000Z" }]
|
|
961
|
+
]);
|
|
962
|
+
const decisions = [
|
|
963
|
+
{ action: "update", ids: ["a"], content: "新A" },
|
|
964
|
+
{ action: "update", ids: ["b"], content: "新B" },
|
|
965
|
+
{ action: "update", ids: ["c"], content: "新C" }
|
|
966
|
+
];
|
|
967
|
+
const { ok, errors } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
968
|
+
assert.equal(ok, false, "3 updates > default cap 2 → still rejects");
|
|
969
|
+
assert.ok(errors.some((e) => e.includes("too many update decisions")), "global cap error present");
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
test("validateDecisions allowCrossTypeMerge: cross-type merge is allowed when the flag is on", () => {
|
|
973
|
+
const snap = new Map([
|
|
974
|
+
["p", { id: "p", type: "preference", title: "语言", content: "中文", importance: 3, archived: false, forgotten: false }],
|
|
975
|
+
["j", { id: "j", type: "project", title: "插件", content: "内容", importance: 3, archived: false, forgotten: false }]
|
|
976
|
+
]);
|
|
977
|
+
const decisions = [
|
|
978
|
+
{ action: "merge", ids: ["p", "j"], keepSource: "p", title: "合并", content: "合并内容", importance: 4 }
|
|
979
|
+
];
|
|
980
|
+
const { ok, errors } = validateDecisions(decisions, snap, { allowCrossTypeMerge: true });
|
|
981
|
+
assert.equal(ok, true, `cross-type merge allowed with the flag, got: ${errors.join("; ")}`);
|
|
982
|
+
assert.equal(decisions.length, 1, "no keep appended (both snapshot ids claimed)");
|
|
983
|
+
});
|
|
984
|
+
|
|
985
|
+
test("validateDecisions default (no options) stays strict — sleep passes unchanged", () => {
|
|
986
|
+
const snap = new Map([
|
|
987
|
+
["a", { id: "a", type: "project", title: "A", content: "旧A", importance: 3, archived: false, forgotten: false }],
|
|
988
|
+
["b", { id: "b", type: "project", title: "B", content: "旧B", importance: 3, archived: false, forgotten: false }]
|
|
989
|
+
]);
|
|
990
|
+
const { ok, skipped } = validateDecisions([{ action: "archive", ids: ["zzz"], reason: "x" }], snap);
|
|
991
|
+
assert.equal(ok, false, "strict rejection without skipInvalid");
|
|
992
|
+
assert.deepEqual(skipped, [], "no skipped bookkeeping in strict mode");
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
test("issue#89: dream run with a skipped invalid decision lands the valid subset and is marked degraded", async () => {
|
|
996
|
+
const { store, service } = dreamSetup();
|
|
997
|
+
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;
|
|
998
|
+
const b = service.saveWithDedupe({ type: "project", title: "旧B", content: "过时B" }).memory;
|
|
999
|
+
const pref = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" }).memory;
|
|
1000
|
+
const c = service.saveWithDedupe({ type: "project", title: "旧C", content: "过时C" }).memory;
|
|
1001
|
+
const warnings = [];
|
|
1002
|
+
const ctx = {
|
|
1003
|
+
logger: { warn: (m) => warnings.push(String(m)) },
|
|
1004
|
+
llm: {
|
|
1005
|
+
async *stream(options) {
|
|
1006
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
1007
|
+
if (userText.startsWith("id=")) {
|
|
1008
|
+
// 一条跨类型 merge(非法 → 跳过)+ 两条合法 archive(覆盖 2/4 快照,
|
|
1009
|
+
// 高于 50% 显式覆盖率下限)
|
|
1010
|
+
yield { type: "text-delta", index: 0, text: JSON.stringify([
|
|
1011
|
+
{ action: "merge", ids: [pref.id, a.id], keepSource: pref.id, title: "跨类型", content: "不应合并", importance: 4 },
|
|
1012
|
+
{ action: "archive", ids: [b.id], reason: "stale" },
|
|
1013
|
+
{ action: "archive", ids: [c.id], reason: "stale" }
|
|
1014
|
+
]) };
|
|
1015
|
+
} else {
|
|
1016
|
+
yield { type: "text-delta", index: 0, text: "记忆库总览:弱模型的个别非法决策不再拖垮整轮巩固。" };
|
|
1017
|
+
}
|
|
1018
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
1023
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model" });
|
|
1024
|
+
assert.equal(result.ok, true, "valid subset absorbed (ok for the baseline)");
|
|
1025
|
+
assert.equal(result.status, "degraded", "run marked degraded, not faked ok");
|
|
1026
|
+
assert.equal(store.getById(b.id).archived, true, "valid archive applied");
|
|
1027
|
+
assert.equal(store.getById(pref.id).archived, false, "invalid merge did not touch its targets");
|
|
1028
|
+
assert.ok(warnings.some((w) => w.includes("skipped") && w.includes("multiple types")), "skip reason logged");
|
|
1029
|
+
store.close();
|
|
1030
|
+
});
|
|
1031
|
+
|
|
1032
|
+
test("issue#89: dreamSkipInvalid:false restores the whole-batch strict rejection", async () => {
|
|
1033
|
+
const { store, service } = dreamSetup();
|
|
1034
|
+
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;
|
|
1035
|
+
const b = service.saveWithDedupe({ type: "project", title: "旧B", content: "过时B" }).memory;
|
|
1036
|
+
const pref = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" }).memory;
|
|
1037
|
+
const ctx = {
|
|
1038
|
+
logger: { warn: () => {} },
|
|
1039
|
+
llm: {
|
|
1040
|
+
async *stream(options) {
|
|
1041
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
1042
|
+
if (userText.startsWith("id=")) {
|
|
1043
|
+
yield { type: "text-delta", index: 0, text: JSON.stringify([
|
|
1044
|
+
{ action: "merge", ids: [pref.id, a.id], keepSource: pref.id, title: "跨类型", content: "不应合并", importance: 4 },
|
|
1045
|
+
{ action: "archive", ids: [b.id], reason: "stale" }
|
|
1046
|
+
]) };
|
|
1047
|
+
} else {
|
|
1048
|
+
yield { type: "text-delta", index: 0, text: "记忆库总览。" };
|
|
1049
|
+
}
|
|
1050
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
};
|
|
1054
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
1055
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model", dreamSkipInvalid: false });
|
|
1056
|
+
assert.equal(result.ok, false, "strict mode rejects the batch");
|
|
1057
|
+
assert.equal(result.error, "invalid decisions: 1 errors");
|
|
1058
|
+
assert.equal(store.getById(b.id).archived, false, "nothing applied under strict rejection");
|
|
1059
|
+
store.close();
|
|
1060
|
+
});
|
|
1061
|
+
|
|
1062
|
+
test("issue#89: minIntervalMs throttles re-triggering regardless of run outcome", async () => {
|
|
1063
|
+
const { store, service } = dreamSetup();
|
|
1064
|
+
let runs = 0;
|
|
1065
|
+
const dream = createDreamScheduler({
|
|
1066
|
+
onRun: async () => { runs++; return { ok: false, error: "llm failed" }; },
|
|
1067
|
+
thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 80,
|
|
1068
|
+
logger: { warn: () => {} }
|
|
1069
|
+
});
|
|
1070
|
+
service.saveWithDedupe({ type: "project", title: "a", content: "x" });
|
|
1071
|
+
assert.equal(dream.maybeSchedule(service), true, "first trigger scheduled");
|
|
1072
|
+
await new Promise((r) => setTimeout(r, 20)); // delayMs 0 → run already dispatched and finished
|
|
1073
|
+
assert.equal(runs, 1, "ran once");
|
|
1074
|
+
assert.equal(dream.maybeSchedule(service), false, "inside the min interval, even after a failed run");
|
|
1075
|
+
await new Promise((r) => setTimeout(r, 90));
|
|
1076
|
+
assert.equal(dream.maybeSchedule(service), true, "interval elapsed → eligible again (baseline unmoved by failure)");
|
|
1077
|
+
store.close();
|
|
1078
|
+
});
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import test from "node:test";
|
|
9
9
|
import assert from "node:assert/strict";
|
|
10
10
|
import { Config } from "../src/config.js";
|
|
11
|
-
import { createDreamScheduler } from "../src/dream.js";
|
|
11
|
+
import { createDreamScheduler, parseReceipt } from "../src/dream.js";
|
|
12
12
|
import { runSleep } from "../src/dream/sleep.js";
|
|
13
13
|
import { createStore } from "../src/store.js";
|
|
14
14
|
import { createService } from "../src/service.js";
|
|
@@ -24,12 +24,20 @@ const embedder = {
|
|
|
24
24
|
|
|
25
25
|
// ---------------------------------------------------------------- config schema
|
|
26
26
|
|
|
27
|
-
test("issue#9: dreamMaxTokens
|
|
28
|
-
assert.equal(Config({}).dreamMaxTokens,
|
|
27
|
+
test("issue#9: dreamMaxTokens defaults to 32768 and accepts values up to 131072", () => {
|
|
28
|
+
assert.equal(Config({}).dreamMaxTokens, 32768, "default raised for thinking-model headroom");
|
|
29
29
|
assert.equal(Config({ dreamMaxTokens: 131072 }).dreamMaxTokens, 131072, "new upper bound accepted");
|
|
30
30
|
assert.equal(Config({ dreamMaxTokens: 65536 }).dreamMaxTokens, 65536, "intermediate value accepted");
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
+
test("issue#9: dreamMaxTokens clamps to [256, 131072], out-of-range values are rejected", () => {
|
|
34
|
+
assert.equal(Config({ dreamMaxTokens: 256 }).dreamMaxTokens, 256, "lower bound accepted");
|
|
35
|
+
assert.equal(Config({ dreamMaxTokens: 100000 }).dreamMaxTokens, 100000, "raised default tier accepted");
|
|
36
|
+
assert.throws(() => Config({ dreamMaxTokens: 255 }), "below min rejected");
|
|
37
|
+
assert.throws(() => Config({ dreamMaxTokens: 131073 }), "above max rejected");
|
|
38
|
+
assert.throws(() => Config({ dreamMaxTokens: 0 }), "zero rejected");
|
|
39
|
+
});
|
|
40
|
+
|
|
33
41
|
test("issue#9: reasoningEffort config defaults to none and rejects unknown values", () => {
|
|
34
42
|
const cfg = Config({});
|
|
35
43
|
assert.equal(cfg.dreamReasoningEffort, "none");
|
|
@@ -107,6 +115,63 @@ test("issue#9: dream forwards dreamReasoningEffort on both LLM calls", async ()
|
|
|
107
115
|
store.close();
|
|
108
116
|
});
|
|
109
117
|
|
|
118
|
+
test("issue#9: dreamMaxTokens is forwarded as maxTokens on consolidation and summary calls", async () => {
|
|
119
|
+
const store = createStore(":memory:");
|
|
120
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
121
|
+
const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
|
|
122
|
+
const { memory: a } = service.saveWithDedupe({ type: "project", title: "插件", content: "旧", importance: 3 });
|
|
123
|
+
const { memory: b } = service.saveWithDedupe({ type: "project", title: "插件2", content: "新细节", importance: 4 });
|
|
124
|
+
const captured = [];
|
|
125
|
+
const ctx = dreamCtx({
|
|
126
|
+
captured,
|
|
127
|
+
onConsolidation: () => JSON.stringify([
|
|
128
|
+
{ action: "merge", ids: [a.id, b.id], keepSource: b.id, title: "插件总览", content: "合并内容", importance: 4 }
|
|
129
|
+
])
|
|
130
|
+
});
|
|
131
|
+
const result = await dream.runDream(ctx, service, { dreamMaxTokens: 100000 });
|
|
132
|
+
assert.equal(result.ok, true);
|
|
133
|
+
assert.equal(captured.length, 2, "consolidation + summary both hit the LLM");
|
|
134
|
+
for (const options of captured) {
|
|
135
|
+
assert.equal(options.maxTokens, 100000, `raised budget forwarded on ${options.purpose}`);
|
|
136
|
+
}
|
|
137
|
+
store.close();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("issue#9: thinking-model empty body (no text emitted, budget burnt on reasoning) fails as no json array", async () => {
|
|
141
|
+
const store = createStore(":memory:");
|
|
142
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
143
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
144
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
145
|
+
const ctx = dreamCtx({ onConsolidation: () => "" }); // 思考型模型把预算烧光 → 无正文
|
|
146
|
+
const result = await dream.runDream(ctx, service, { dreamMaxTokens: 32768 });
|
|
147
|
+
assert.equal(result.ok, false, "empty body is a hard failure, never faked ok");
|
|
148
|
+
assert.match(result.error, /no json array/);
|
|
149
|
+
const run = store.listDreamRuns()[0];
|
|
150
|
+
assert.equal(run.status, "failed", "audit row records failed");
|
|
151
|
+
assert.match(run.error, /no json array/, "audit error_message carries the empty-body cause");
|
|
152
|
+
assert.equal(parseReceipt(run.receipt).status, "failed", "receipt records failed");
|
|
153
|
+
store.close();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("legal empty decision list [] is a no-op success, not a failure", async () => {
|
|
157
|
+
// 真实模型在记忆无冗余时合法输出 [](CONSOLIDATION_PROMPT 允许"无问题无需输出"),
|
|
158
|
+
// 此前被 validateDecisions 判 failed → 审计表反复失败。现在应 ok:true、applied 0、
|
|
159
|
+
// 审计记 ok,且 summary 照常产出。
|
|
160
|
+
const store = createStore(":memory:");
|
|
161
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
162
|
+
service.saveWithDedupe({ type: "project", title: "插件", content: "内容", importance: 3 });
|
|
163
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
164
|
+
const ctx = dreamCtx({ onConsolidation: () => "[]" }); // 模型:无需合并
|
|
165
|
+
const result = await dream.runDream(ctx, service, { dreamMaxTokens: 32768 });
|
|
166
|
+
assert.equal(result.ok, true, "empty [] is a valid no-op, never failed");
|
|
167
|
+
assert.equal(result.applied, 0, "no decisions to apply");
|
|
168
|
+
assert.equal(result.summary, true, "summary still produced");
|
|
169
|
+
const run = store.listDreamRuns()[0];
|
|
170
|
+
assert.equal(run.status, "ok", "audit row records ok, not failed");
|
|
171
|
+
assert.equal(parseReceipt(run.receipt).status, "ok", "receipt records ok");
|
|
172
|
+
store.close();
|
|
173
|
+
});
|
|
174
|
+
|
|
110
175
|
test("issue#25: dreamProvider/dreamModel config wins over the agentDefaultModel route", async () => {
|
|
111
176
|
const store = createStore(":memory:");
|
|
112
177
|
const service = createService({ store, mirror: null, config: {} });
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// --- Issue #89 恢复功能的全量单测(v0.7.22)----------------------------------
|
|
2
|
+
// v0.7.22 把 v0.6.9(Issue #26)的 skipInvalid 宽容校验路径原样搬回。核心行为
|
|
3
|
+
// 契约:单条非法决策只跳过该条(记入 skipped、不 claim 任何 id),合法子集照常
|
|
4
|
+
// 应用、run 记 degraded;但全局信号(update/create 上限、显式覆盖率下限)不受
|
|
5
|
+
// 该开关影响、始终整单拒绝。以下测试逐条锁死恢复后的分支:非法动作按原因分类
|
|
6
|
+
// 跳过(create/unknown/archived/summary/重复 claim/merge 参数/update 参数/
|
|
7
|
+
// conflict 参数)、skipInvalid 与覆盖率下限/上限/allowCrossTypeMerge 的交互,
|
|
8
|
+
// 以及 runDream 层"全非法仍 failed / 部分非法 degraded 如实进审计"。
|
|
9
|
+
import test from "node:test";
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import { validateDecisions, createDreamScheduler, parseReceipt } from "../src/dream.js";
|
|
12
|
+
import { createStore } from "../src/store.js";
|
|
13
|
+
import { createService } from "../src/service.js";
|
|
14
|
+
import { mockCtx } from "./helpers/dream-mock.js";
|
|
15
|
+
|
|
16
|
+
function dreamSetup() {
|
|
17
|
+
const store = createStore(":memory:");
|
|
18
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
19
|
+
return { store, service };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 合法记忆快照工厂:id 首字母作 type 前缀(p=preference / 其余 project),
|
|
23
|
+
// created_at 默认 2020 年(保证 update 保护期检查放行)。
|
|
24
|
+
function makeSnap(ids) {
|
|
25
|
+
return new Map(ids.map((id) => [
|
|
26
|
+
id,
|
|
27
|
+
{
|
|
28
|
+
id,
|
|
29
|
+
type: id.startsWith("p") ? "preference" : "project",
|
|
30
|
+
title: `标题${id}`,
|
|
31
|
+
content: `内容${id}`,
|
|
32
|
+
importance: 3,
|
|
33
|
+
archived: false,
|
|
34
|
+
forgotten: false,
|
|
35
|
+
created_at: "2020-01-01T00:00:00.000Z"
|
|
36
|
+
}
|
|
37
|
+
]));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ------------------------------------------------------------- 按原因逐条跳过
|
|
41
|
+
|
|
42
|
+
test("validateDecisions skipInvalid: create with empty title is skipped; strict mode rejects it", () => {
|
|
43
|
+
const emptySnap = new Map(); // create 不 claim id:空快照下覆盖率恒 1
|
|
44
|
+
const decisions = [
|
|
45
|
+
{ action: "create", title: "", content: "body", type: "pattern" },
|
|
46
|
+
{ action: "create", title: "ok", content: "c", type: "pattern" }
|
|
47
|
+
];
|
|
48
|
+
const { ok, errors, skipped } = validateDecisions(decisions, emptySnap, { skipInvalid: true });
|
|
49
|
+
assert.equal(ok, true, `valid create should survive, got: ${errors.join("; ")}`);
|
|
50
|
+
assert.equal(skipped.length, 1);
|
|
51
|
+
assert.match(skipped[0].error, /create needs non-empty title/);
|
|
52
|
+
assert.deepEqual(decisions.map((d) => d.action), ["create"], "empty-title create spliced out");
|
|
53
|
+
assert.deepEqual(decisions[0].title, "ok");
|
|
54
|
+
|
|
55
|
+
const strict = validateDecisions([{ action: "create", title: "", content: "b", type: "pattern" }], emptySnap);
|
|
56
|
+
assert.equal(strict.ok, false, "strict mode hard-rejects a bad create");
|
|
57
|
+
assert.ok(strict.errors.some((e) => e.includes("create needs non-empty title")));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("validateDecisions skipInvalid: unknown id, archived, and summary targets are skipped, valid siblings survive", () => {
|
|
61
|
+
const snap = new Map([
|
|
62
|
+
...makeSnap(["b", "c"]),
|
|
63
|
+
["arch", { id: "arch", type: "project", title: "旧arch", content: "x", importance: 3, archived: true, forgotten: false, created_at: "2020-01-01T00:00:00.000Z" }],
|
|
64
|
+
["s", { id: "s", type: "summary", title: "总览", content: "y", importance: 3, archived: false, forgotten: false, created_at: "2020-01-01T00:00:00.000Z" }]
|
|
65
|
+
]);
|
|
66
|
+
const decisions = [
|
|
67
|
+
{ action: "archive", ids: ["zzz"], reason: "gone" },
|
|
68
|
+
{ action: "archive", ids: ["arch"], reason: "stale" },
|
|
69
|
+
{ action: "archive", ids: ["s"], reason: "stale" },
|
|
70
|
+
{ action: "archive", ids: ["b"], reason: "stale" },
|
|
71
|
+
{ action: "archive", ids: ["c"], reason: "stale" }
|
|
72
|
+
];
|
|
73
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
74
|
+
assert.equal(ok, true, `valid archives should survive, got: ${errors.join("; ")}`);
|
|
75
|
+
assert.equal(skipped.length, 3);
|
|
76
|
+
assert.match(skipped[0].error, /unknown id/);
|
|
77
|
+
assert.match(skipped[1].error, /archived or summary/);
|
|
78
|
+
assert.match(skipped[2].error, /archived or summary/);
|
|
79
|
+
// claimed = {b,c} = 2/4 = 50% 恰好过下限;arch/s 未 claim → 隐式 keep
|
|
80
|
+
assert.deepEqual(decisions.map((d) => d.action), ["archive", "archive", "keep", "keep"]);
|
|
81
|
+
assert.deepEqual(decisions[0].ids, ["b"]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("validateDecisions skipInvalid: a later decision re-claiming an already-claimed id is skipped, first survives", () => {
|
|
85
|
+
const snap = makeSnap(["a", "b", "c"]);
|
|
86
|
+
const decisions = [
|
|
87
|
+
{ action: "merge", ids: ["a", "b"], keepSource: "a", title: "合并", content: "m", importance: 4 },
|
|
88
|
+
{ action: "merge", ids: ["b", "c"], keepSource: "b", title: "合并2", content: "m2", importance: 4 }
|
|
89
|
+
];
|
|
90
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
91
|
+
assert.equal(ok, true, `first merge should survive, got: ${errors.join("; ")}`);
|
|
92
|
+
assert.equal(skipped.length, 1);
|
|
93
|
+
assert.equal(skipped[0].index, 1);
|
|
94
|
+
assert.match(skipped[0].error, /claimed by multiple/);
|
|
95
|
+
// claimed = {a,b} = 2/3 = 67% 过下限;c 隐式 keep,绝不因重复 claim 双 keep
|
|
96
|
+
assert.deepEqual(decisions.map((d) => d.action), ["merge", "keep"]);
|
|
97
|
+
assert.deepEqual(decisions[0].ids, ["a", "b"]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("validateDecisions skipInvalid: merge with keepSource outside ids is skipped", () => {
|
|
101
|
+
const snap = makeSnap(["a", "b"]);
|
|
102
|
+
const decisions = [
|
|
103
|
+
{ action: "merge", ids: ["a", "b"], keepSource: "zzz", title: "t", content: "c", importance: 4 },
|
|
104
|
+
{ action: "archive", ids: ["a"], reason: "stale" }
|
|
105
|
+
];
|
|
106
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
107
|
+
assert.equal(ok, true, `valid archive should survive, got: ${errors.join("; ")}`);
|
|
108
|
+
assert.equal(skipped.length, 1);
|
|
109
|
+
assert.match(skipped[0].error, /keepSource must be one of ids/);
|
|
110
|
+
assert.deepEqual(decisions.map((d) => d.action), ["archive", "keep"]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("validateDecisions skipInvalid: update targeting multiple ids / no change / summary / too-young memory is skipped", () => {
|
|
114
|
+
const now = Date.now();
|
|
115
|
+
const snap = new Map([
|
|
116
|
+
...makeSnap(["a", "b"]),
|
|
117
|
+
["s", { id: "s", type: "summary", title: "总览", content: "y", importance: 3, archived: false, forgotten: false, created_at: "2020-01-01T00:00:00.000Z" }],
|
|
118
|
+
["y", { id: "y", type: "project", title: "Y", content: "新Y", importance: 3, archived: false, forgotten: false, created_at: new Date(now).toISOString() }]
|
|
119
|
+
]);
|
|
120
|
+
const decisions = [
|
|
121
|
+
{ action: "update", ids: ["a", "b"], content: "x" },
|
|
122
|
+
{ action: "update", ids: ["a"], content: "内容a" },
|
|
123
|
+
{ action: "update", ids: ["s"], content: "x" },
|
|
124
|
+
{ action: "update", ids: ["y"], content: "x" },
|
|
125
|
+
{ action: "update", ids: ["a"], content: "新A" },
|
|
126
|
+
{ action: "update", ids: ["b"], content: "新B" }
|
|
127
|
+
];
|
|
128
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
129
|
+
assert.equal(ok, true, `valid updates should survive, got: ${errors.join("; ")}`);
|
|
130
|
+
assert.equal(skipped.length, 4);
|
|
131
|
+
assert.match(skipped[0].error, /exactly one id/);
|
|
132
|
+
assert.match(skipped[1].error, /change at least one field/);
|
|
133
|
+
assert.match(skipped[2].error, /cannot update summary/);
|
|
134
|
+
assert.match(skipped[3].error, /too young/);
|
|
135
|
+
// claimed = {a,b} = 2/4 = 50% 过下限;s/y 隐式 keep
|
|
136
|
+
assert.deepEqual(decisions.map((d) => d.action), ["update", "update", "keep", "keep"]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("validateDecisions skipInvalid: conflict without a distinct winner/loser is skipped", () => {
|
|
140
|
+
const snap = makeSnap(["w", "l", "c"]);
|
|
141
|
+
const decisions = [
|
|
142
|
+
{ action: "conflict", winner: "w", loser: "l" },
|
|
143
|
+
{ action: "conflict", loser: "c" }
|
|
144
|
+
];
|
|
145
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
146
|
+
assert.equal(ok, true, `valid conflict should survive, got: ${errors.join("; ")}`);
|
|
147
|
+
assert.equal(skipped.length, 1);
|
|
148
|
+
assert.match(skipped[0].error, /conflict needs distinct winner and loser/);
|
|
149
|
+
assert.deepEqual(decisions.map((d) => d.action), ["conflict", "keep"]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ------------------------------------------------------- skipInvalid 与全局闸门
|
|
153
|
+
|
|
154
|
+
test("validateDecisions skipInvalid: valid subset below the coverage floor still rejects the whole batch", () => {
|
|
155
|
+
const snap = makeSnap(["p", "j", "x"]);
|
|
156
|
+
const decisions = [
|
|
157
|
+
{ action: "archive", ids: ["p"], reason: "stale" },
|
|
158
|
+
{ action: "merge", ids: ["p", "j"], keepSource: "p", title: "跨类型", content: "m", importance: 4 }
|
|
159
|
+
];
|
|
160
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
161
|
+
assert.equal(ok, false, "skip does not bypass the coverage floor");
|
|
162
|
+
assert.equal(skipped.length, 1);
|
|
163
|
+
assert.ok(errors.some((e) => e.includes("coverage")), "coverage error present");
|
|
164
|
+
assert.equal(decisions.length, 2, "rejected batch left untouched (splice only on the success path)");
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("validateDecisions skipInvalid: updates skipped for other reasons do not count toward the update cap", () => {
|
|
168
|
+
const snap = makeSnap(["a", "b", "c", "d"]);
|
|
169
|
+
const decisions = [
|
|
170
|
+
{ action: "update", ids: ["a"], content: "内容a" },
|
|
171
|
+
{ action: "update", ids: ["b"], content: "内容b" },
|
|
172
|
+
{ action: "update", ids: ["c"], content: "新C" },
|
|
173
|
+
{ action: "archive", ids: ["d"], reason: "stale" }
|
|
174
|
+
];
|
|
175
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true, maxUpdatePerRun: 2 });
|
|
176
|
+
assert.equal(ok, true, `cap counts survivors only, got: ${errors.join("; ")}`);
|
|
177
|
+
assert.equal(skipped.length, 2);
|
|
178
|
+
// 3 条 update 输入 → 2 条跳过 → 幸存 1 条 ≤ cap 2;claimed {c,d} = 2/4 = 50%
|
|
179
|
+
assert.deepEqual(decisions.map((d) => d.action), ["update", "archive", "keep", "keep"]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("validateDecisions skipInvalid: creates skipped for other reasons do not count toward the create cap", () => {
|
|
183
|
+
const emptySnap = new Map();
|
|
184
|
+
const decisions = [];
|
|
185
|
+
for (let i = 0; i < 5; i++) decisions.push({ action: "create", title: `t${i}`, content: "c", type: "pattern" });
|
|
186
|
+
decisions.push({ action: "create", title: "", content: "x", type: "pattern" });
|
|
187
|
+
const { ok, errors, skipped } = validateDecisions(decisions, emptySnap, { skipInvalid: true, maxCreatePerRun: 5 });
|
|
188
|
+
assert.equal(ok, true, `cap counts survivors only, got: ${errors.join("; ")}`);
|
|
189
|
+
assert.equal(skipped.length, 1);
|
|
190
|
+
assert.equal(decisions.length, 5, "only the 5 valid creates survive the cap");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("validateDecisions skipInvalid + allowCrossTypeMerge compose: cross-type merge allowed, unrelated invalid entry still skipped", () => {
|
|
194
|
+
const snap = makeSnap(["p", "j", "b", "c"]);
|
|
195
|
+
const decisions = [
|
|
196
|
+
{ action: "merge", ids: ["p", "j"], keepSource: "p", title: "合并", content: "m", importance: 4 },
|
|
197
|
+
{ action: "archive", ids: ["zzz"], reason: "gone" }
|
|
198
|
+
];
|
|
199
|
+
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true, allowCrossTypeMerge: true });
|
|
200
|
+
assert.equal(ok, true, `flag-enabled merge survives, got: ${errors.join("; ")}`);
|
|
201
|
+
assert.equal(skipped.length, 1);
|
|
202
|
+
assert.match(skipped[0].error, /unknown id/);
|
|
203
|
+
assert.deepEqual(decisions.map((d) => d.action), ["merge", "keep", "keep"]);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// ----------------------------------------------------------- runDream 层 e2e
|
|
207
|
+
|
|
208
|
+
test("issue#89: with default skipInvalid on, an all-invalid batch still fails the run (nothing valid survives)", async () => {
|
|
209
|
+
const { store, service } = dreamSetup();
|
|
210
|
+
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;
|
|
211
|
+
const b = service.saveWithDedupe({ type: "project", title: "旧B", content: "过时B" }).memory;
|
|
212
|
+
const pref = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" }).memory;
|
|
213
|
+
const pref2 = service.saveWithDedupe({ type: "preference", title: "语气", content: "轻松" }).memory;
|
|
214
|
+
const ctx = mockCtx({
|
|
215
|
+
onConsolidation: () => JSON.stringify([
|
|
216
|
+
{ action: "merge", ids: [pref.id, a.id], keepSource: pref.id, title: "跨类型", content: "x", importance: 4 },
|
|
217
|
+
{ action: "merge", ids: [pref2.id, b.id], keepSource: pref2.id, title: "跨类型2", content: "y", importance: 4 }
|
|
218
|
+
])
|
|
219
|
+
});
|
|
220
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
221
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model" });
|
|
222
|
+
assert.equal(result.ok, false, "all invalid → whole batch rejected, not silently kept");
|
|
223
|
+
assert.match(result.error, /invalid decisions/);
|
|
224
|
+
const run = store.listDreamRuns()[0];
|
|
225
|
+
assert.equal(run.status, "failed", "audit row marks failed, not degraded (nothing valid landed)");
|
|
226
|
+
assert.equal(parseReceipt(run.receipt).status, "failed", "receipt marks failed");
|
|
227
|
+
assert.equal(store.getById(a.id).archived, false, "nothing applied");
|
|
228
|
+
assert.equal(store.getById(b.id).archived, false, "nothing applied");
|
|
229
|
+
store.close();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("issue#89: a partial-invalid run is marked degraded in the audit row and receipt", async () => {
|
|
233
|
+
const { store, service } = dreamSetup();
|
|
234
|
+
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;
|
|
235
|
+
const b = service.saveWithDedupe({ type: "project", title: "旧B", content: "过时B" }).memory;
|
|
236
|
+
const pref = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" }).memory;
|
|
237
|
+
const c = service.saveWithDedupe({ type: "project", title: "旧C", content: "过时C" }).memory;
|
|
238
|
+
const warnings = [];
|
|
239
|
+
const ctx = {
|
|
240
|
+
...mockCtx({
|
|
241
|
+
onConsolidation: () => JSON.stringify([
|
|
242
|
+
{ action: "merge", ids: [pref.id, a.id], keepSource: pref.id, title: "跨类型", content: "x", importance: 4 },
|
|
243
|
+
{ action: "archive", ids: [b.id], reason: "stale" },
|
|
244
|
+
{ action: "archive", ids: [c.id], reason: "stale" }
|
|
245
|
+
])
|
|
246
|
+
}),
|
|
247
|
+
logger: { warn: (m) => warnings.push(String(m)) }
|
|
248
|
+
};
|
|
249
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
250
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model" });
|
|
251
|
+
assert.equal(result.ok, true, "valid subset absorbed (ok for the baseline)");
|
|
252
|
+
assert.equal(result.status, "degraded", "run marked degraded, not faked ok");
|
|
253
|
+
const run = store.listDreamRuns()[0];
|
|
254
|
+
assert.equal(run.status, "degraded", "audit row records degraded, never ok");
|
|
255
|
+
assert.equal(parseReceipt(run.receipt).status, "degraded", "receipt records degraded");
|
|
256
|
+
assert.equal(store.getById(b.id).archived, true, "valid archive landed");
|
|
257
|
+
assert.equal(store.getById(pref.id).archived, false, "invalid merge did not touch its targets");
|
|
258
|
+
assert.ok(warnings.some((w) => w.includes("skipped") && w.includes("multiple types")), "skip reason logged");
|
|
259
|
+
store.close();
|
|
260
|
+
});
|