@modusensus/dsh-mneme 0.9.5 → 0.10.0
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 +9 -5
- package/docs/CONFIGURATION.md +21 -0
- package/docs/DAEMON.md +6 -2
- package/docs/DISTILL.md +90 -6
- package/docs/SLEEP.md +20 -6
- package/docs/STORAGE.md +1 -0
- package/lib/api-standalone.js +11 -3
- package/lib/api.js +25 -5
- package/lib/client.js +137 -10
- package/lib/config.js +33 -1
- package/lib/continuity.js +11 -3
- package/lib/dream/decisions.js +88 -27
- package/lib/dream/sleep.js +467 -81
- package/lib/dream.js +110 -13
- package/lib/fence.js +24 -0
- package/lib/index.js +15 -6
- package/lib/inject.js +9 -4
- package/lib/lang.js +17 -12
- package/lib/pipeline-nudge-auto.js +3 -0
- package/lib/pipeline-nudge.js +41 -8
- package/lib/pipeline-runner.js +15 -0
- package/lib/pre-inject-gate.js +14 -0
- package/lib/profile-extract.js +4 -1
- package/lib/provenance-http.js +162 -37
- package/lib/service.js +30 -0
- package/lib/session-gate.js +91 -0
- package/lib/settings.js +13 -1
- package/lib/store.js +526 -18
- package/lib/summarize.js +152 -15
- package/lib/tools.js +26 -6
- package/package.json +4 -2
- package/src/api-standalone.js +11 -3
- package/src/api.js +25 -5
- package/src/config.js +33 -1
- package/src/continuity.js +11 -3
- package/src/dream/decisions.js +88 -27
- package/src/dream/sleep.js +467 -81
- package/src/dream.js +110 -13
- package/src/fence.js +24 -0
- package/src/index.js +15 -6
- package/src/inject.js +9 -4
- package/src/lang.js +17 -12
- package/src/pipeline-nudge-auto.js +3 -0
- package/src/pipeline-nudge.js +41 -8
- package/src/pipeline-runner.js +15 -0
- package/src/pre-inject-gate.js +14 -0
- package/src/profile-extract.js +4 -1
- package/src/provenance-http.js +162 -37
- package/src/service.js +30 -0
- package/src/session-gate.js +91 -0
- package/src/settings.js +13 -1
- package/src/store.js +526 -18
- package/src/summarize.js +152 -15
- package/src/tools.js +26 -6
- package/test/api.test.js +153 -4
- package/test/client.test.js +215 -0
- package/test/config.test.js +8 -0
- package/test/conflict-reviewed.test.js +79 -0
- package/test/continuity.test.js +29 -1
- package/test/dependency-overrides.test.js +78 -0
- package/test/differentiate-feedback.test.js +248 -0
- package/test/dream-decision-evidence.test.js +314 -0
- package/test/dream-fence.test.js +43 -0
- package/test/inject-pools.test.js +2 -1
- package/test/mint-source.test.js +83 -0
- package/test/pipeline-nudge-auto.test.js +26 -2
- package/test/pipeline-nudge.test.js +87 -0
- package/test/pipeline-runner.test.js +26 -0
- package/test/prompt-stance.test.js +128 -0
- package/test/provenance-http.test.js +200 -17
- package/test/release-docs.test.js +7 -1
- package/test/security-doc.test.js +43 -4
- package/test/session-gate.test.js +264 -0
- package/test/settings.test.js +4 -0
- package/test/sleep-action-set.test.js +16 -11
- package/test/sleep-catchup.test.js +315 -0
- package/test/sleep-failure-backoff.test.js +156 -0
- package/test/sleep.test.js +293 -0
- package/test/standalone-api.test.js +50 -0
- package/test/store.test.js +220 -0
- package/test/summarize.test.js +172 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// Issue #533:differentiate 的正反馈死循环回归护栏。
|
|
2
|
+
//
|
|
3
|
+
// 现象(issue #533 实测):sleepActionSet="full" 下,一对高相似记忆被判成
|
|
4
|
+
// differentiate,旧实现把**同一段**差异注记同时追加到两侧正文,指望"注记进
|
|
5
|
+
// embedding 后下一轮不再判重复"。离线复现证明恰好相反——两侧共享同一段后缀会让
|
|
6
|
+
// 向量同向靠拢(0.876 → 0.960 只用 5 轮,把注记截到 0 字照样往上爬,因为推高相似
|
|
7
|
+
// 度的是共享前缀本身而不是注记长短),于是下一轮又被判重复、又追加一段,实测单条
|
|
8
|
+
// 堆到 19 段、正文 150 字膨胀到 2200+ 字。
|
|
9
|
+
//
|
|
10
|
+
// 现在的契约:differentiate **不改正文**,裁决落 differentiated_pairs 账本;候选
|
|
11
|
+
// 构造阶段读账本,已裁决且两侧正文未变就不送模型。本文件锁的就是这两件事——
|
|
12
|
+
// 「正文不变」和「第二轮不再花钱」。
|
|
13
|
+
import test from "node:test";
|
|
14
|
+
import assert from "node:assert/strict";
|
|
15
|
+
import { applyDecisions, validateDecisions } from "../src/dream/decisions.js";
|
|
16
|
+
import { createStore } from "../src/store.js";
|
|
17
|
+
import { createService } from "../src/service.js";
|
|
18
|
+
import { createVectorIndex } from "../src/vector-index.js";
|
|
19
|
+
import { runSleep } from "../src/dream/sleep.js";
|
|
20
|
+
|
|
21
|
+
const embedder = {
|
|
22
|
+
// 批量接口要按输入条数返回(向量回填走这一条),返回单个向量会让 usable 判空。
|
|
23
|
+
embedSingle: async () => [1, 0, 0],
|
|
24
|
+
embed: async (texts) => (Array.isArray(texts) ? texts.map(() => [1, 0, 0]) : [1, 0, 0]),
|
|
25
|
+
schedule: () => {},
|
|
26
|
+
modelHash: "mock#1",
|
|
27
|
+
dimension: 3
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function setup() {
|
|
31
|
+
const store = createStore(":memory:");
|
|
32
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
33
|
+
const vectorIndex = createVectorIndex({ store });
|
|
34
|
+
service.setEmbedder(embedder);
|
|
35
|
+
service.setVectorIndex(vectorIndex);
|
|
36
|
+
return { store, service, vectorIndex };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function makeMemory(service, title, content, type = "project") {
|
|
40
|
+
return service.saveWithDedupe({ type, title, content, importance: 3 }).memory;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function snapshotOf(store, ...mems) {
|
|
44
|
+
return new Map(mems.map((m) => [m.id, store.getById(m.id)]));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function captureCtx(reply) {
|
|
48
|
+
const seen = [];
|
|
49
|
+
return {
|
|
50
|
+
seen,
|
|
51
|
+
logger: { warn: () => {}, info: () => {} },
|
|
52
|
+
agentDefaultModel: { currentSelection: () => ({ provider: "mock", model: "sleep-model" }) },
|
|
53
|
+
llm: {
|
|
54
|
+
async *stream(options) {
|
|
55
|
+
seen.push(options.messages.find((m) => m.system ?? m.role === "system")?.content?.[0]?.text ?? "");
|
|
56
|
+
yield { type: "text-delta", index: 0, text: typeof reply === "function" ? reply() : reply };
|
|
57
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function baseConfig(overrides = {}) {
|
|
64
|
+
return {
|
|
65
|
+
sleepModeEnabled: true,
|
|
66
|
+
sleepIdleMinutes: 5,
|
|
67
|
+
sleepMinIntervalHours: 8,
|
|
68
|
+
sleepConflictStrictness: "normal",
|
|
69
|
+
sleepArchiveDays: 30,
|
|
70
|
+
sleepCompressDays: 90,
|
|
71
|
+
sleepPatternMinMemories: 100,
|
|
72
|
+
sleepMaxPatternPerRun: 3,
|
|
73
|
+
...overrides
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function seedPair(service, vectorIndex, t1, t2, sim = 1.0) {
|
|
78
|
+
const a = makeMemory(service, t1, `${t1} 的内容`);
|
|
79
|
+
const b = makeMemory(service, t2, `${t2} 的内容`);
|
|
80
|
+
const sin = Math.sqrt(Math.max(0, 1 - sim * sim));
|
|
81
|
+
vectorIndex.saveEmbedding(a.id, [1, 0, 0]);
|
|
82
|
+
vectorIndex.saveEmbedding(b.id, [sim, sin, 0]);
|
|
83
|
+
return { a, b };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ---------------------------------------------------------------- 正文不变
|
|
87
|
+
|
|
88
|
+
test("issue#533: repeated differentiate with drifting wording never grows the bodies", () => {
|
|
89
|
+
const { service, store } = setup();
|
|
90
|
+
const a = makeMemory(service, "内网 SSH 端口", "内网走 22");
|
|
91
|
+
const b = makeMemory(service, "外网 SSH 端口", "外网映射 2222");
|
|
92
|
+
const before = { [a.id]: service.getById(a.id).content, [b.id]: service.getById(b.id).content };
|
|
93
|
+
// 模型每轮换一种说法——这正是字面 includes 幂等失效的场景,也是 issue 里单条堆到
|
|
94
|
+
// 19 段的真实成因。账本口径下措辞漂移不再产生任何正文副作用。
|
|
95
|
+
const wordings = [
|
|
96
|
+
["内网直连", "外网映射"],
|
|
97
|
+
["走内网", "走外网映射"],
|
|
98
|
+
["内网那台直连", "外网那台做映射"],
|
|
99
|
+
["内网的机器直连", "外网的机器走映射"],
|
|
100
|
+
["内网直连(22)", "外网映射(2222)"]
|
|
101
|
+
];
|
|
102
|
+
let appliedRounds = 0;
|
|
103
|
+
for (const distinctions of wordings) {
|
|
104
|
+
const snapshot = snapshotOf(store, service.getById(a.id), service.getById(b.id));
|
|
105
|
+
const { applied } = applyDecisions(
|
|
106
|
+
[{ action: "differentiate", ids: [a.id, b.id], distinctions }],
|
|
107
|
+
service, null, snapshot, {}
|
|
108
|
+
);
|
|
109
|
+
appliedRounds += applied;
|
|
110
|
+
}
|
|
111
|
+
assert.equal(appliedRounds, 1, "only the first round is a real verdict; the rest are idempotent");
|
|
112
|
+
// 这一条是 #533 的核心断言:无论判多少轮,正文长度一个字节都不许变。
|
|
113
|
+
assert.equal(service.getById(a.id).content, before[a.id], "side A body is byte-identical");
|
|
114
|
+
assert.equal(service.getById(b.id).content, before[b.id], "side B body is byte-identical");
|
|
115
|
+
assert.equal(service.getById(a.id).content.length, "内网走 22".length, "no note accumulates");
|
|
116
|
+
store.close();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("issue#533: the ledger keeps one row per pair and refreshes the distinctions", () => {
|
|
120
|
+
const { service, store } = setup();
|
|
121
|
+
const a = makeMemory(service, "甲", "内容甲");
|
|
122
|
+
const b = makeMemory(service, "乙", "内容乙");
|
|
123
|
+
for (const distinctions of [["第一版"], ["第二版", "补充版"]]) {
|
|
124
|
+
applyDecisions(
|
|
125
|
+
[{ action: "differentiate", ids: [a.id, b.id], distinctions }],
|
|
126
|
+
service, null, snapshotOf(store, service.getById(a.id), service.getById(b.id)), {}
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
// 顺序归一:不管送审时谁在前,账本只有一行。
|
|
130
|
+
assert.equal(store.listDifferentiatedPairs().length, 1, "one pair, one row");
|
|
131
|
+
const record = service.findDifferentiatedPair(b.id, a.id);
|
|
132
|
+
assert.deepEqual(record.distinctions, ["第一版"], "an unchanged body keeps the original verdict");
|
|
133
|
+
store.close();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// -------------------------------------------------------------- 三层幂等
|
|
137
|
+
|
|
138
|
+
test("issue#533: a body edited after the verdict re-opens the pair", () => {
|
|
139
|
+
const { service, store } = setup();
|
|
140
|
+
const a = makeMemory(service, "甲", "内容甲");
|
|
141
|
+
const b = makeMemory(service, "乙", "内容乙");
|
|
142
|
+
applyDecisions(
|
|
143
|
+
[{ action: "differentiate", ids: [a.id, b.id], distinctions: ["甲面", "乙面"] }],
|
|
144
|
+
service, null, snapshotOf(store, service.getById(a.id), service.getById(b.id)), {}
|
|
145
|
+
);
|
|
146
|
+
// 原样重放 → 幂等。
|
|
147
|
+
const replay = applyDecisions(
|
|
148
|
+
[{ action: "differentiate", ids: [a.id, b.id], distinctions: ["甲面", "乙面"] }],
|
|
149
|
+
service, null, snapshotOf(store, service.getById(a.id), service.getById(b.id)), {}
|
|
150
|
+
);
|
|
151
|
+
assert.equal(replay.applied, 0, "an unchanged pair is idempotent");
|
|
152
|
+
// 一侧被改写 → 结论可能不再成立,必须能重新裁决(宁可再判一次,不静音真的演化)。
|
|
153
|
+
service.update(a.id, { content: "内容甲被改写" });
|
|
154
|
+
const reopened = applyDecisions(
|
|
155
|
+
[{ action: "differentiate", ids: [a.id, b.id], distinctions: ["甲面", "乙面", "改写后"] }],
|
|
156
|
+
service, null, snapshotOf(store, service.getById(a.id), service.getById(b.id)), {}
|
|
157
|
+
);
|
|
158
|
+
assert.equal(reopened.applied, 1, "an edited side re-opens the pair");
|
|
159
|
+
assert.deepEqual(
|
|
160
|
+
service.findDifferentiatedPair(a.id, b.id).distinctions, ["甲面", "乙面", "改写后"],
|
|
161
|
+
"the refreshed verdict replaces the stale one"
|
|
162
|
+
);
|
|
163
|
+
store.close();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("issue#533: legacy rows that still carry the old note are treated as adjudicated", () => {
|
|
167
|
+
const { service, store } = setup();
|
|
168
|
+
// 旧版本写进正文的注记块原样造出来(升级不清账:这些行没有账本行)。
|
|
169
|
+
const a = makeMemory(service, "甲", "内容甲\n\n(差异注记:甲面;乙面)");
|
|
170
|
+
const b = makeMemory(service, "乙", "内容乙\n\n(差异注记:甲面;乙面)");
|
|
171
|
+
const { applied } = applyDecisions(
|
|
172
|
+
[{ action: "differentiate", ids: [a.id, b.id], distinctions: ["甲面", "乙面"] }],
|
|
173
|
+
service, null, snapshotOf(store, service.getById(a.id), service.getById(b.id)), {}
|
|
174
|
+
);
|
|
175
|
+
assert.equal(applied, 0, "no ledger row is needed once both sides carry the note");
|
|
176
|
+
assert.equal(service.getById(a.id).content, "内容甲\n\n(差异注记:甲面;乙面)", "the legacy body is untouched");
|
|
177
|
+
store.close();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("issue#533: a bare service without the ledger degrades to skipped, never throws", () => {
|
|
181
|
+
// 人工确认路径(service.js 的裸 applyService)没有账本透传——此时「正文不变」本身
|
|
182
|
+
// 就是全部副作用,没有账本就没有可写之处,只能是 skipped,不能算 applied。
|
|
183
|
+
const mems = new Map();
|
|
184
|
+
const bare = {
|
|
185
|
+
getById: (id) => mems.get(id) ?? null,
|
|
186
|
+
update: (id, patch) => { mems.set(id, { ...mems.get(id), ...patch }); },
|
|
187
|
+
setArchived: () => {},
|
|
188
|
+
transaction: (fn) => fn()
|
|
189
|
+
};
|
|
190
|
+
mems.set("a", { id: "a", title: "甲", content: "内容甲", archived: false });
|
|
191
|
+
mems.set("b", { id: "b", title: "乙", content: "内容乙", archived: false });
|
|
192
|
+
const { applied, failures } = applyDecisions(
|
|
193
|
+
[{ action: "differentiate", ids: ["a", "b"], distinctions: ["甲面", "乙面"] }],
|
|
194
|
+
bare, null, null, {}
|
|
195
|
+
);
|
|
196
|
+
assert.equal(applied, 0, "nothing to record, nothing applied");
|
|
197
|
+
assert.equal(failures.length, 0, "degrades cleanly");
|
|
198
|
+
assert.equal(mems.get("a").content, "内容甲", "still never touches the body");
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// ------------------------------------------------------- 候选构造阶段抑制
|
|
202
|
+
|
|
203
|
+
test("issue#533: a second sleep cycle does not pay for an already-adjudicated pair", async () => {
|
|
204
|
+
const { service, store, vectorIndex } = setup();
|
|
205
|
+
const { a, b } = seedPair(service, vectorIndex, "甲端口", "乙端口");
|
|
206
|
+
const reply = () => JSON.stringify([
|
|
207
|
+
{ action: "differentiate", ids: [a.id, b.id], distinctions: ["甲面", "乙面"] }
|
|
208
|
+
]);
|
|
209
|
+
const ctx1 = captureCtx(reply);
|
|
210
|
+
const first = await runSleep(ctx1, service, baseConfig({ sleepActionSet: "full" }), ctx1.logger, { embedder, vectorIndex }, null);
|
|
211
|
+
assert.equal(first.phases.conflicts.applied, 1, "the first cycle adjudicates the pair");
|
|
212
|
+
const callsAfterFirst = ctx1.seen.length;
|
|
213
|
+
assert.ok(service.findDifferentiatedPair(a.id, b.id), "the verdict landed in the ledger");
|
|
214
|
+
|
|
215
|
+
// 第二轮:同一对、同一相似度,账本命中且两侧正文未变 → 候选在送 LLM 之前就被压掉。
|
|
216
|
+
const ctx2 = captureCtx(reply);
|
|
217
|
+
const second = await runSleep(ctx2, service, baseConfig({ sleepActionSet: "full" }), ctx2.logger, { embedder, vectorIndex }, null);
|
|
218
|
+
assert.equal(second.phases.conflicts.status, "noop", "the phase has nothing left to adjudicate");
|
|
219
|
+
assert.equal(second.phases.conflicts.suppressed, 1, "the pair is accounted for, not silently dropped");
|
|
220
|
+
// 只数冲突阶段那次调用:patterns 阶段与本题无关,别把它的调用算进来。
|
|
221
|
+
assert.equal(ctx2.seen.filter((p) => p.includes("冲突仲裁")).length, 0,
|
|
222
|
+
"no LLM call is made for a settled pair");
|
|
223
|
+
|
|
224
|
+
// 但这不等于「永远不再判」:一侧被改写后必须重新回到候选里。
|
|
225
|
+
service.update(a.id, { content: "甲端口 的内容被改写" });
|
|
226
|
+
const ctx3 = captureCtx(reply);
|
|
227
|
+
const third = await runSleep(ctx3, service, baseConfig({ sleepActionSet: "full" }), ctx3.logger, { embedder, vectorIndex }, null);
|
|
228
|
+
assert.ok(ctx3.seen.some((p) => p.includes("冲突仲裁")), "an edited pair is re-sent to the model");
|
|
229
|
+
assert.ok(third.phases.conflicts, "the phase still runs");
|
|
230
|
+
store.close();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("issue#533: the audit row reports suppressed separately from 'no conflicts found'", async () => {
|
|
234
|
+
const { service, store, vectorIndex } = setup();
|
|
235
|
+
const { a, b } = seedPair(service, vectorIndex, "甲端口", "乙端口");
|
|
236
|
+
// 先手工记一笔账(正文与相似度都没变),第二轮应报 noop + suppressed。
|
|
237
|
+
applyDecisions(
|
|
238
|
+
[{ action: "differentiate", ids: [a.id, b.id], distinctions: ["甲面", "乙面"] }],
|
|
239
|
+
service, null, snapshotOf(store, service.getById(a.id), service.getById(b.id)), {}
|
|
240
|
+
);
|
|
241
|
+
const ctx = captureCtx(JSON.stringify([]));
|
|
242
|
+
const result = await runSleep(ctx, service, baseConfig({ sleepActionSet: "full" }), ctx.logger, { embedder, vectorIndex }, null);
|
|
243
|
+
assert.equal(result.phases.conflicts.status, "noop");
|
|
244
|
+
assert.equal(result.phases.conflicts.reason, "all candidate pairs already differentiated");
|
|
245
|
+
assert.equal(result.phases.conflicts.suppressed, 1);
|
|
246
|
+
assert.equal(result.phases.conflicts.pairs, undefined, "no pair is reported as pending work");
|
|
247
|
+
store.close();
|
|
248
|
+
});
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// issue #534:[0.9.5] dream 决策层 19 轮里 17 轮整轮判定无效,两轮 ok 也 applied=0,
|
|
2
|
+
// 失败时不落原始决策。作者报的是三件互相独立的观测缺口,本文件各锁一条:
|
|
3
|
+
//
|
|
4
|
+
// 1. 失败的轮次只剩「N errors」这个结论,模型原文随日志滚掉,事后无法复现;
|
|
5
|
+
// → dream_runs.raw_output(不入 decisions 列:那一列的语义是「可重放决策数组」,
|
|
6
|
+
// applyDecisions 直接吃它,塞非决策对象会污染重放者)。
|
|
7
|
+
// 2. skipped 里只有 index/action/ids/error 四键,看不出这条决策是「缺 title」
|
|
8
|
+
// 还是「缺 content」——都是同一个 error 串里的不同分支;
|
|
9
|
+
// → 逐条补 has_keepSource / has_title / has_content 三个布尔。
|
|
10
|
+
// 3. 审计算 95% 成功率、真实整轮只有 10.5%:两套账没有共同分母。
|
|
11
|
+
// → llm_audit_logs.metadata.decision_validation(verdict + 三个计数 + 动作名),
|
|
12
|
+
// 不改 status/token/timestamp(那是调用层事实,改写等于伪造账本)。
|
|
13
|
+
import test from "node:test";
|
|
14
|
+
import assert from "node:assert/strict";
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import {
|
|
17
|
+
createDreamScheduler,
|
|
18
|
+
rawOutputPayload,
|
|
19
|
+
decisionValidationMeta,
|
|
20
|
+
validateDecisions
|
|
21
|
+
} from "../src/dream.js";
|
|
22
|
+
import { createStore } from "../src/store.js";
|
|
23
|
+
import { createService } from "../src/service.js";
|
|
24
|
+
import { mockCtx } from "./helpers/dream-mock.js";
|
|
25
|
+
|
|
26
|
+
function snapshot(ids) {
|
|
27
|
+
return new Map(ids.map((id) => [id, {
|
|
28
|
+
id, type: "project", title: `t-${id}`, content: `c-${id}`, importance: 3, archived: false, forgotten: false
|
|
29
|
+
}]));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function setup() {
|
|
33
|
+
const store = createStore(":memory:");
|
|
34
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
35
|
+
const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
|
|
36
|
+
return { store, service, dream };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function consolidateAudits(service) {
|
|
40
|
+
return service.listLlmAudits({ limit: 20 }).filter((a) => a.operation_type === "dream_consolidate");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function metaOf(row) {
|
|
44
|
+
const meta = row.metadata;
|
|
45
|
+
return typeof meta === "string" ? JSON.parse(meta) : meta;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------- 建议 2 载荷
|
|
49
|
+
|
|
50
|
+
test("#534: rawOutputPayload 只留头尾,空文本整键不写", () => {
|
|
51
|
+
assert.equal(rawOutputPayload(""), undefined, "空文本不许落空壳对象冒充「有原文」");
|
|
52
|
+
assert.equal(rawOutputPayload(undefined), undefined);
|
|
53
|
+
assert.equal(rawOutputPayload(null), undefined);
|
|
54
|
+
|
|
55
|
+
const short = rawOutputPayload("抱歉,我无法处理这个任务");
|
|
56
|
+
assert.equal(short.raw_len, 12);
|
|
57
|
+
assert.equal(short.raw_has_bracket, false, "有没有 '[' 是区分「没按格式写」与「格式对但救不回来」的判据");
|
|
58
|
+
assert.equal("raw_tail" in short, false, "短文本不补 tail:tail 是给跑飞的长输出定位收尾的");
|
|
59
|
+
|
|
60
|
+
const long = `起手 ${"填".repeat(600)} 收尾段落`;
|
|
61
|
+
const payload = rawOutputPayload(long);
|
|
62
|
+
assert.equal(payload.raw_len, long.length);
|
|
63
|
+
assert.equal(payload.raw_head, long.slice(0, 300), "头部固定 300 字,口径沿用 #504");
|
|
64
|
+
assert.equal(payload.raw_tail, long.slice(-200), "仅 >500 字时补末 200 字");
|
|
65
|
+
assert.equal(payload.raw_has_bracket, false);
|
|
66
|
+
|
|
67
|
+
assert.equal(rawOutputPayload("[{").raw_has_bracket, true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("#534: 拒收轮与合法子集的载荷形状一致,只是 verdict 不同", () => {
|
|
71
|
+
const rejected = decisionValidationMeta({ verdict: "rejected", errors: ["e1", "e2"], skipped: [{ action: "merge" }, { action: "merge" }, { action: "archive" }], survivorCount: 0 });
|
|
72
|
+
assert.deepEqual(rejected.decision_validation, {
|
|
73
|
+
verdict: "rejected",
|
|
74
|
+
error_count: 2,
|
|
75
|
+
skipped_count: 3,
|
|
76
|
+
survivor_count: 0,
|
|
77
|
+
invalid_actions: ["merge", "archive"]
|
|
78
|
+
});
|
|
79
|
+
const ok = decisionValidationMeta({ verdict: "ok", errors: [], skipped: [], survivorCount: 4 });
|
|
80
|
+
assert.equal(ok.decision_validation.verdict, "ok");
|
|
81
|
+
assert.deepEqual(ok.decision_validation.invalid_actions, []);
|
|
82
|
+
assert.equal(ok.decision_validation.survivor_count, 4);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------- 建议 1 三布尔
|
|
86
|
+
|
|
87
|
+
test("#534: skipped 逐条带 has_keepSource / has_title / has_content,能区分缺哪一项", () => {
|
|
88
|
+
const ids = ["a", "b"];
|
|
89
|
+
const snap = snapshot(ids);
|
|
90
|
+
// ① merge 什么都不给 → 三个布尔全 false(keepSource 不在 ids 里、title/content 空)
|
|
91
|
+
const bare = validateDecisions([{ action: "merge", ids }], snap, { skipInvalid: true });
|
|
92
|
+
assert.equal(bare.skipped.length, 1);
|
|
93
|
+
const skippedBare = bare.skipped[0];
|
|
94
|
+
assert.deepEqual(Object.keys(skippedBare).sort(), ["action", "error", "has_content", "has_keepSource", "has_title", "ids", "index"],
|
|
95
|
+
"既有四键保留,只加三个布尔——不塞原文");
|
|
96
|
+
assert.equal(skippedBare.has_keepSource, false);
|
|
97
|
+
assert.equal(skippedBare.has_title, false);
|
|
98
|
+
assert.equal(skippedBare.has_content, false);
|
|
99
|
+
|
|
100
|
+
// ② 三项都给但 keepSource 不指向自家人 → 仍被拒,但三布尔能看出「字段是齐的」
|
|
101
|
+
const full = validateDecisions([{
|
|
102
|
+
action: "merge", ids, keepSource: "zzz", title: "合并标题", content: "合并正文", importance: 4
|
|
103
|
+
}], snap, { skipInvalid: true });
|
|
104
|
+
assert.equal(full.skipped.length, 1);
|
|
105
|
+
assert.equal(full.skipped[0].has_keepSource, true, "写了 keepSource 就要看清「写了但不合法」");
|
|
106
|
+
assert.equal(full.skipped[0].has_title, true);
|
|
107
|
+
assert.equal(full.skipped[0].has_content, true);
|
|
108
|
+
assert.match(full.skipped[0].error, /keepSource/, "具体不合法的点仍在 error 里,布尔只补形状");
|
|
109
|
+
|
|
110
|
+
// ③ 空白字符串不算「有」:模型常把缺省值写成空串
|
|
111
|
+
const blank = validateDecisions([{ action: "merge", ids, keepSource: "a", title: " ", content: "\n" }], snap, { skipInvalid: true });
|
|
112
|
+
assert.equal(blank.skipped[0].has_title, false);
|
|
113
|
+
assert.equal(blank.skipped[0].has_content, false);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------------- 建议 2 落库(端到端)
|
|
117
|
+
|
|
118
|
+
test("#534: 解析失败轮把模型原文写进 raw_output,decisions 仍然不落库", async () => {
|
|
119
|
+
const { store, service, dream } = setup();
|
|
120
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
121
|
+
const prose = "抱歉,我无法处理这个任务";
|
|
122
|
+
const result = await dream.runDream(mockCtx({ onConsolidation: () => prose }), service, {});
|
|
123
|
+
assert.equal(result.ok, false);
|
|
124
|
+
assert.match(result.error, /no json array/);
|
|
125
|
+
|
|
126
|
+
const run = store.getDreamRun(result.runId);
|
|
127
|
+
assert.equal(run.decisions, undefined, "回归锁:decisions 列仍只承载「可重放决策数组」");
|
|
128
|
+
assert.ok(run.raw_output, "失败轮必须留下原文读数(此前只有一条会滚掉的 warn)");
|
|
129
|
+
assert.equal(run.raw_output.raw_len, prose.length);
|
|
130
|
+
assert.equal(run.raw_output.raw_head, prose, "原文本身要能在库外复现模型到底吐了什么");
|
|
131
|
+
assert.equal(run.raw_output.raw_has_bracket, false);
|
|
132
|
+
store.close();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("#534: 校验整单拒绝轮同样落 raw_output(原文是有效的 JSON 数组)", async () => {
|
|
136
|
+
const { store, service, dream } = setup();
|
|
137
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
138
|
+
const decisions = JSON.stringify([{ action: "archive", ids: ["deadbeef"] }]);
|
|
139
|
+
const result = await dream.runDream(mockCtx({ onConsolidation: () => decisions }), service, {});
|
|
140
|
+
assert.equal(result.ok, false);
|
|
141
|
+
assert.match(result.error, /invalid decisions/);
|
|
142
|
+
|
|
143
|
+
const run = store.getDreamRun(result.runId);
|
|
144
|
+
assert.equal(run.decisions[0]._validationFailed, true, "既有哨兵不变(#135 锁)");
|
|
145
|
+
assert.equal(run.raw_output.raw_len, decisions.length);
|
|
146
|
+
assert.equal(run.raw_output.raw_head, decisions, "被拒但格式合法的轮次,原文最有价值:能离线重跑校验");
|
|
147
|
+
assert.equal(run.raw_output.raw_has_bracket, true, "区分「格式对但内容不合法」与「压根没写 JSON」");
|
|
148
|
+
store.close();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("#534: 流错误(没有文本可留)落 NULL,不许写空壳对象", async () => {
|
|
152
|
+
const { store, service, dream } = setup();
|
|
153
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
154
|
+
const ctx = {
|
|
155
|
+
logger: { warn: () => {}, info: () => {} },
|
|
156
|
+
llm: {
|
|
157
|
+
async *stream() {
|
|
158
|
+
yield { type: "finish", reason: { kind: "error", failure: { code: "RATE_LIMIT", message: "429" } } };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const result = await dream.runDream(ctx, service, {});
|
|
163
|
+
assert.equal(result.ok, false);
|
|
164
|
+
const run = store.getDreamRun(result.runId);
|
|
165
|
+
assert.equal(run.raw_output, undefined, "提供方错误时模型没吐过正文,落 NULL 而不是 {raw_len:0}");
|
|
166
|
+
store.close();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------- 建议 3 分账
|
|
170
|
+
|
|
171
|
+
test("#534: 校验判决补回同一行的 metadata,调用层事实不动", async () => {
|
|
172
|
+
const { store, service, dream } = setup();
|
|
173
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
174
|
+
await dream.runDream(mockCtx({ onConsolidation: () => "抱歉,我无法处理这个任务" }), service, {});
|
|
175
|
+
const row = consolidateAudits(service).find((a) => a.status === "error");
|
|
176
|
+
assert.ok(row, "调用层仍按既有口径记 error(吐了正文却不是 JSON 数组)");
|
|
177
|
+
const meta = metaOf(row);
|
|
178
|
+
assert.deepEqual(meta.decision_validation, {
|
|
179
|
+
verdict: "unparsed",
|
|
180
|
+
error_count: 0,
|
|
181
|
+
skipped_count: 0,
|
|
182
|
+
survivor_count: 0,
|
|
183
|
+
invalid_actions: []
|
|
184
|
+
}, "「没解析出数组」是判决的第四种状态,不能跟「整单拒绝」混成一个");
|
|
185
|
+
assert.equal(meta.parse_failure, true, "#504 的解析失败读数必须保留(dream-fence 锁)");
|
|
186
|
+
assert.equal(row.status, "error", "补注不许改写调用层 status");
|
|
187
|
+
assert.ok(row.total_tokens >= 0, "token 计数原样保留");
|
|
188
|
+
store.close();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("#534: 调用成功但决策被整单拒绝 → 同一行同时读出两层结论", async () => {
|
|
192
|
+
const { store, service, dream } = setup();
|
|
193
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
194
|
+
await dream.runDream(mockCtx({
|
|
195
|
+
onConsolidation: () => JSON.stringify([{ action: "archive", ids: ["deadbeef"] }])
|
|
196
|
+
}), service, {});
|
|
197
|
+
const rows = consolidateAudits(service);
|
|
198
|
+
const row = rows[0];
|
|
199
|
+
assert.equal(row.status, "success", "调用是真的成功了——这是作者说的「审计 95% 成功率」那一侧");
|
|
200
|
+
const meta = metaOf(row);
|
|
201
|
+
assert.equal(meta.decision_validation.verdict, "rejected", "整轮无效在这一侧才看得见(真实 10.5%)");
|
|
202
|
+
assert.equal(meta.decision_validation.skipped_count, 1);
|
|
203
|
+
assert.deepEqual(meta.decision_validation.invalid_actions, ["archive"]);
|
|
204
|
+
assert.ok(meta.decision_validation.error_count >= 1, "all-invalid 的整单错误串要计入 error_count");
|
|
205
|
+
store.close();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("#534: 合法子集(degraded)的 verdict 与 survivor_count 对得上", async () => {
|
|
209
|
+
const { store, service, dream } = setup();
|
|
210
|
+
const { memory: m } = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
211
|
+
await dream.runDream(mockCtx({
|
|
212
|
+
onConsolidation: () => JSON.stringify([
|
|
213
|
+
{ action: "keep", ids: [m.id] },
|
|
214
|
+
{ action: "archive", ids: ["deadbeef"] }
|
|
215
|
+
])
|
|
216
|
+
}), service, {});
|
|
217
|
+
const row = consolidateAudits(service)[0];
|
|
218
|
+
assert.equal(row.status, "success");
|
|
219
|
+
const meta = metaOf(row);
|
|
220
|
+
assert.equal(meta.decision_validation.verdict, "degraded");
|
|
221
|
+
assert.equal(meta.decision_validation.skipped_count, 1);
|
|
222
|
+
// 校验器就地 splice 掉被跳过的条目,所以存活数就是校验后的 decisions 长度:
|
|
223
|
+
// 这里 1 条 keep 存活、1 条 archive 被跳过(隐式 keep 不会重复补已 claim 的 id)。
|
|
224
|
+
assert.equal(meta.decision_validation.survivor_count, 1, "存活数是校验后真正会被消费的条目数");
|
|
225
|
+
store.close();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("#534: 全部合法的轮次 verdict=ok,metadata 不因补注长出多余键", async () => {
|
|
229
|
+
const { store, service, dream } = setup();
|
|
230
|
+
const { memory: m } = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
231
|
+
const result = await dream.runDream(mockCtx({
|
|
232
|
+
onConsolidation: () => JSON.stringify([{ action: "keep", ids: [m.id] }])
|
|
233
|
+
}), service, {});
|
|
234
|
+
assert.equal(result.ok, true);
|
|
235
|
+
const row = consolidateAudits(service)[0];
|
|
236
|
+
const meta = metaOf(row);
|
|
237
|
+
assert.equal(meta.decision_validation.verdict, "ok");
|
|
238
|
+
assert.equal(meta.decision_validation.skipped_count, 0);
|
|
239
|
+
assert.equal(meta.parse_failure, undefined, "成功轮不许带解析失败读数");
|
|
240
|
+
store.close();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("#534: 覆盖率不足(没有逐条跳过)也判 degraded,与 dream_runs.status 同源", async () => {
|
|
244
|
+
// CodeRabbit on #535:coverageShortfall 同样是校验层降级。此前 verdict 只认
|
|
245
|
+
// skippedInvalid,于是「模型给了合法子集、但覆盖不到半数」的轮次会落成
|
|
246
|
+
// dream_runs.status="degraded" 而 metadata 写 verdict="ok"——两套账在最该
|
|
247
|
+
// 对齐的一格上又对不上。这条锁把它们绑死在同一个判据上。
|
|
248
|
+
const { store, service, dream } = setup();
|
|
249
|
+
const ids = [];
|
|
250
|
+
for (let i = 0; i < 5; i += 1) {
|
|
251
|
+
ids.push(service.saveWithDedupe({ type: "project", title: `覆盖-${i}`, content: `覆盖正文-${i}`, importance: 3 }).memory.id);
|
|
252
|
+
}
|
|
253
|
+
const result = await dream.runDream(mockCtx({
|
|
254
|
+
// 2/5 = 40% 覆盖 < 下限 50%,且没有一条被逐条跳过——降级理由只能是 coverage。
|
|
255
|
+
// 必须让一条真落地(archive 掉一条):全 keep 轮会先被 noChange 分支截走
|
|
256
|
+
// (那里 status 只由 summaryStored 决定),走不到 coverage 这一格。
|
|
257
|
+
onConsolidation: () => JSON.stringify([
|
|
258
|
+
{ action: "keep", ids: [ids[0]] },
|
|
259
|
+
{ action: "archive", ids: [ids[1]], reason: "重复条目" }
|
|
260
|
+
])
|
|
261
|
+
}), service, { dreamImplicitKeep: true, dreamMinExplicitCoverage: 0.5 });
|
|
262
|
+
const run = store.getDreamRun(result.runId);
|
|
263
|
+
assert.equal(run.applied, 1, "先确认这条路径真的落地了改动(否则测的还是 noChange 分支)");
|
|
264
|
+
assert.equal(run.status, "degraded", "覆盖率不足在 run 行上一直是 degraded(#104 方向 1)");
|
|
265
|
+
const meta = metaOf(consolidateAudits(service)[0]);
|
|
266
|
+
assert.equal(meta.decision_validation.verdict, "degraded", "审计 metadata 必须跟 run 行说同一件事");
|
|
267
|
+
assert.equal(meta.decision_validation.skipped_count, 0, "这条路径没有逐条跳过,判据只能是 coverage");
|
|
268
|
+
assert.equal(meta.decision_validation.survivor_count, 5, "未 claim 的 4 条被隐式 keep 补齐,全部存活");
|
|
269
|
+
store.close();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("#534: 审计行的 id 来自本次插入,不是「当前最大 id」", async () => {
|
|
273
|
+
// CodeRabbit on #535(Major):saveLlmAudit 原来先 INSERT 再
|
|
274
|
+
// `SELECT * ORDER BY id DESC LIMIT 1`。库支持多进程共用,另一个写者插在
|
|
275
|
+
// 中间就会把这行读成别人的调用,随后 annotate 把本轮判决写到别人头上。
|
|
276
|
+
// 这里用「连续两次写入各自拿回自己那行」锁住回读口径。
|
|
277
|
+
const { store, service } = setup();
|
|
278
|
+
const first = service.saveLlmAudit({ trigger_source: "t", operation_type: "o1", model_id: "m", status: "success" });
|
|
279
|
+
service.saveLlmAudit({ trigger_source: "t", operation_type: "o2", model_id: "m", status: "success" });
|
|
280
|
+
const second = service.saveLlmAudit({ trigger_source: "t", operation_type: "o3", model_id: "m", status: "success" });
|
|
281
|
+
assert.equal(first.operation_type, "o1", "回读的必须是自己插入的那一行");
|
|
282
|
+
assert.equal(second.operation_type, "o3");
|
|
283
|
+
assert.ok(second.id > first.id, "id 单调,但取值只认本次插入");
|
|
284
|
+
|
|
285
|
+
// 判决补注同样按 id 定位:插一行噪声在后面,也不能改变目标行。
|
|
286
|
+
service.saveLlmAudit({ trigger_source: "t", operation_type: "noise", model_id: "m", status: "success" });
|
|
287
|
+
const updated = service.annotateLlmAudit(first.id, { decision_validation: { verdict: "rejected" } });
|
|
288
|
+
assert.equal(updated.operation_type, "o1", "补注不许漂到别的行");
|
|
289
|
+
const rows = service.listLlmAudits({ limit: 20 });
|
|
290
|
+
assert.equal(rows.find((r) => r.operation_type === "noise").metadata, undefined, "噪声行不许被写上本轮判决");
|
|
291
|
+
store.close();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("#534: saveLlmAudit 不再用「取最大 id」回读(源码锁)", () => {
|
|
295
|
+
const src = readFileSync(new URL("../src/store.js", import.meta.url), "utf8");
|
|
296
|
+
const body = src.slice(src.indexOf("function saveLlmAudit(entry)"));
|
|
297
|
+
const head = body.slice(0, body.indexOf("\n }\n"));
|
|
298
|
+
// 先去注释再判:解释这条坑的注释本身会引述旧写法,不剥掉就永远命中自己。
|
|
299
|
+
const code = head.replace(/^\s*\/\/.*$/gm, "");
|
|
300
|
+
assert.equal(code.includes("ORDER BY id DESC LIMIT 1"), false,
|
|
301
|
+
"多进程共用同一库时「取最大 id」会读成别人的行——必须用 lastInsertRowid");
|
|
302
|
+
assert.match(code, /lastInsertRowid/, "回读口径锁在本次插入返回的 rowid 上");
|
|
303
|
+
assert.match(code, /SELECT \* FROM llm_audit_logs WHERE id = \?/, "回读用主键定位,不用排序取首行");
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// ---------------------------------------------------------------- 改动面守卫
|
|
307
|
+
|
|
308
|
+
test("#534: decisions 列之外没有别的写入口往那一列塞非决策对象", () => {
|
|
309
|
+
// 这条锁防的是「后来有人图省事,把原文/诊断内联进 decisions」——那会同时打挂
|
|
310
|
+
// applyDecisions 的重放语义与所有按决策数组消费的下游。
|
|
311
|
+
const src = readFileSync(new URL("../src/dream.js", import.meta.url), "utf8");
|
|
312
|
+
assert.match(src, /raw_output:\s*result\.raw_output/, "finish 必须把 raw_output 转发给 saveDreamRun");
|
|
313
|
+
assert.equal(src.includes("_rawOutput"), false, "不许另起一个内联标记走 decisions 列");
|
|
314
|
+
});
|
package/test/dream-fence.test.js
CHANGED
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
import test from "node:test";
|
|
15
15
|
import assert from "node:assert/strict";
|
|
16
16
|
import { readFileSync } from "node:fs";
|
|
17
|
+
import { dirname, join, resolve } from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
import { breakFenceMarkers } from "../src/fence.js";
|
|
17
20
|
import { createStore } from "../src/store.js";
|
|
18
21
|
import { createService } from "../src/service.js";
|
|
19
22
|
import { createDreamScheduler, fenceListBody } from "../src/dream.js";
|
|
@@ -21,6 +24,7 @@ import { parseEntries } from "./helpers/dream-mock.js";
|
|
|
21
24
|
import { STR } from "../src/lang.js";
|
|
22
25
|
|
|
23
26
|
const LISTING = "id=a1 | type=project | importance=3 | updated=2026-10-20T00:00:00Z | title=旧1";
|
|
27
|
+
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
24
28
|
|
|
25
29
|
test("围栏 + 角色声明包住清单,且清单只在围栏之内", () => {
|
|
26
30
|
const out = fenceListBody("zh", LISTING);
|
|
@@ -162,3 +166,42 @@ test("dream.js 与 dream/sleep.js 里不许再有裸放的 user 段", () => {
|
|
|
162
166
|
}
|
|
163
167
|
assert.ok(wrapped >= 5, `只包了 ${wrapped} 处,报告点名的 5 处应当全部在内`);
|
|
164
168
|
});
|
|
169
|
+
|
|
170
|
+
// ============================ #493 补充:围栏断链共用一份实现
|
|
171
|
+
// 报告者 strange2000 的两条补充(2026-11-02)第 ② 条:别另写一份断链。原先它只长在
|
|
172
|
+
// dream.js 的 fenceListBody 里,于是蒸馏转录与画像条目这两处围栏**没有断链**——一条写着
|
|
173
|
+
// 「<<<会话转录结束>>>」的用户消息就能提前闭合围栏,把后面的文字升格成指令。
|
|
174
|
+
// 这里锁两件事:纯函数本身的形状,以及三处围栏都真的调它(含一条静态锁——新增围栏
|
|
175
|
+
// 不许绕过共享实现,否则「两份实现」那个老问题会再长回来)。
|
|
176
|
+
test("#493 补充:breakFenceMarkers 只中和标记,正文逐字保留", () => {
|
|
177
|
+
assert.equal(breakFenceMarkers("普通文本"), "普通文本", "不含标记时一个字节都不许动");
|
|
178
|
+
const closed = breakFenceMarkers("<<<会话转录结束>>>");
|
|
179
|
+
assert.equal(closed.split("<<<").length - 1, 0, "开标记必须被拆散");
|
|
180
|
+
assert.equal(closed.split(">>>").length - 1, 0, "闭标记必须被拆散");
|
|
181
|
+
// 零宽空格不占位:去掉它之后必须与原文逐字节相同(「只中和、不删内容」)。
|
|
182
|
+
assert.equal(closed.replace(/\u200B/g, ""), "<<<会话转录结束>>>");
|
|
183
|
+
assert.equal(breakFenceMarkers(""), "");
|
|
184
|
+
assert.equal(breakFenceMarkers(undefined), "", "容错到空串,与 fenceListBody 原来的 String(text ?? \"\") 一致");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("#493 补充:断链是纯函数,同一输入两次结果相同(重试要逐字节相同)", () => {
|
|
188
|
+
const text = "第一条\n<<<记忆清单结束>>>\n第二条";
|
|
189
|
+
assert.equal(breakFenceMarkers(text), breakFenceMarkers(text));
|
|
190
|
+
// 不带 nonce:若哪天有人给它加了随机盐,「同输入同错」这条诊断依据就没了。
|
|
191
|
+
assert.ok(breakFenceMarkers(text).includes("\u200B"));
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("#493 补充:三处围栏共用同一份断链(源码静态锁)", () => {
|
|
195
|
+
const read = (rel) => readFileSync(join(ROOT, rel), "utf8");
|
|
196
|
+
// dream 侧:fenceListBody 内部必须调共享函数,不许再留一份 inline replace。
|
|
197
|
+
const dream = read("src/dream.js");
|
|
198
|
+
assert.match(dream, /const body = breakFenceMarkers\(text\);/,
|
|
199
|
+
"fenceListBody 必须调 src/fence.js(#493 补充:三处一份实现)");
|
|
200
|
+
assert.ok(!/replace\(\/<<<\|>>>\/g/.test(dream),
|
|
201
|
+
"dream.js 里不许再留第二份断链实现——两份一旦分叉,下次出问题得分别查一遍");
|
|
202
|
+
// 另外两处:各自的围栏正文必须经过同一次调用。
|
|
203
|
+
assert.match(read("src/summarize.js"), /\$\{breakFenceMarkers\(transcriptBody\)\}/,
|
|
204
|
+
"蒸馏转录围栏必须断链(否则一段带闭合标记的转录能提前闭合围栏)");
|
|
205
|
+
assert.match(read("src/profile-extract.js"), /breakFenceMarkers\(listing\)/,
|
|
206
|
+
"画像条目围栏同样要断链");
|
|
207
|
+
});
|
|
@@ -135,7 +135,8 @@ test("#249: capability guide registers exactly once, and stays off when the key
|
|
|
135
135
|
assert.equal(on.sections.length, 1, "exactly one guide section");
|
|
136
136
|
assert.equal(on.sections[0].name, "memory-guide");
|
|
137
137
|
assert.equal(on.sections[0].order, 150, "插件指引段的既有 order 惯例");
|
|
138
|
-
assert.equal(on.sections[0].text,
|
|
138
|
+
assert.equal(typeof on.sections[0].text, "function", "guide text resolves per prompt context so session gating can apply");
|
|
139
|
+
assert.equal(on.sections[0].text({}), MEMORY_GUIDE_SECTION, "normal sessions receive the same guide text");
|
|
139
140
|
// 不可逆工具(memory_delete)的克制指引必须在场:这是全 guide 里唯一有数据
|
|
140
141
|
// 损失后果的一句,被误删掉的回归要能立刻转红。
|
|
141
142
|
assert.ok(MEMORY_GUIDE_SECTION.includes("memory_delete"), "guide keeps the irreversible-tool warning");
|