@modusensus/dsh-mneme 0.8.5 → 0.8.7

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.
Files changed (62) hide show
  1. package/README.md +45 -147
  2. package/bin/cli.mjs +689 -603
  3. package/lib/api-standalone.js +57 -1
  4. package/lib/api.js +34 -11
  5. package/lib/client.js +289 -59
  6. package/lib/config.js +78 -11
  7. package/lib/content-hash.js +54 -0
  8. package/lib/document-index.js +94 -0
  9. package/lib/document.js +93 -11
  10. package/lib/dream/sleep.js +122 -8
  11. package/lib/dream.js +1354 -1347
  12. package/lib/index.js +115 -14
  13. package/lib/inject.js +6 -2
  14. package/lib/lang.js +15 -4
  15. package/lib/local-embedder.js +44 -6
  16. package/lib/maintenance.js +142 -0
  17. package/lib/mirror.js +147 -15
  18. package/lib/recall-stats.js +52 -2
  19. package/lib/scope.js +34 -6
  20. package/lib/service.js +86 -7
  21. package/lib/settings.js +6 -3
  22. package/lib/store.js +248 -21
  23. package/lib/tools.js +27 -9
  24. package/lib/write-admission.js +259 -0
  25. package/package.json +4 -4
  26. package/src/api-standalone.js +57 -1
  27. package/src/api.js +34 -11
  28. package/src/config.js +78 -11
  29. package/src/content-hash.js +54 -0
  30. package/src/document-index.js +94 -0
  31. package/src/document.js +93 -11
  32. package/src/dream/sleep.js +122 -8
  33. package/src/dream.js +1354 -1347
  34. package/src/index.js +115 -14
  35. package/src/inject.js +6 -2
  36. package/src/lang.js +15 -4
  37. package/src/local-embedder.js +44 -6
  38. package/src/maintenance.js +142 -0
  39. package/src/mirror.js +147 -15
  40. package/src/recall-stats.js +52 -2
  41. package/src/scope.js +34 -6
  42. package/src/service.js +86 -7
  43. package/src/settings.js +6 -3
  44. package/src/store.js +248 -21
  45. package/src/tools.js +27 -9
  46. package/src/write-admission.js +259 -0
  47. package/test/api.test.js +15 -2
  48. package/test/client.test.js +90 -5
  49. package/test/content-hash.test.js +204 -0
  50. package/test/document-dir.test.js +233 -0
  51. package/test/document.test.js +151 -3
  52. package/test/inject-parent-gate.test.js +106 -0
  53. package/test/inject-pools.test.js +8 -3
  54. package/test/lastrunat-persist.test.js +101 -0
  55. package/test/llm-audit.test.js +136 -0
  56. package/test/local-embedder.test.js +45 -2
  57. package/test/maintenance.test.js +391 -0
  58. package/test/mirror-types.test.js +208 -0
  59. package/test/recall-evals.test.js +18 -0
  60. package/test/recall-stats.test.js +66 -0
  61. package/test/sleep.test.js +117 -0
  62. package/test/write-admission.test.js +345 -0
@@ -0,0 +1,208 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdtempSync, readFileSync, rmSync, existsSync, writeFileSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { MIRROR_EXCLUDED_TYPES, MIRROR_READONLY_TYPES, TYPE_FILE, createMirror, parseHumanEdits, renderMirrorText } from "../src/mirror.js";
7
+ import { createStore, TYPES } from "../src/store.js";
8
+ import { createService } from "../src/service.js";
9
+ import { PACKAGE_VERSION } from "../src/version-check.js";
10
+
11
+ // #278 第一批:落盘盲区。TYPE_FILE 原先只有 5 个键,pitfall / constraint /
12
+ // rejected_solution / pattern 这些后加的 type 从不进镜像与导出——落点存在,但对
13
+ // 一半活跃记忆是盲的,且没有任何提示。#296 第二批把 document 也收了进来(只读
14
+ // 指针视图),排除集因此为空;两个集合的边界仍由下面的双向断言钉住。
15
+ const FORMERLY_UNCOVERED = ["pitfall", "constraint", "rejected_solution", "pattern"];
16
+
17
+ function tempDir() {
18
+ return mkdtempSync(join(tmpdir(), "dsh-mneme-types-"));
19
+ }
20
+
21
+ function memory(type, over = {}) {
22
+ return {
23
+ id: "m1", type, title: "标题", content: "内容", tags: ["a"], importance: 3,
24
+ forgotten: false, created_at: "2026-01-01T00:00:00.000Z",
25
+ updated_at: "2026-01-01T00:00:00.000Z", ...over
26
+ };
27
+ }
28
+
29
+ test("#278: every store type has a landing point except the pinned exclusions", () => {
30
+ const missing = [...TYPES].filter((t) => !TYPE_FILE[t] && !MIRROR_EXCLUDED_TYPES.has(t));
31
+ assert.deepEqual(missing, [], "新增 type 必须显式决定落不落镜像,不能留下静默盲区");
32
+ assert.deepEqual(Object.keys(TYPE_FILE).filter((t) => !TYPES.has(t)), [], "TYPE_FILE 不得含 store 不认识的键");
33
+ assert.deepEqual([...MIRROR_EXCLUDED_TYPES].filter((t) => !TYPES.has(t)), [], "排除集里的 key 必须是真 type");
34
+ // 双向:注释说的「TYPES \ TYPE_FILE 恰好等于排除集」要真被钉住。只查上面两条
35
+ // 的话,将来有人把 document 加进 TYPE_FILE 却忘了从排除集删,两边同时变假还全绿。
36
+ assert.deepEqual([...MIRROR_EXCLUDED_TYPES].filter((t) => TYPE_FILE[t]), [], "排除集与落盘集不得重叠");
37
+ for (const type of FORMERLY_UNCOVERED) {
38
+ assert.ok(TYPE_FILE[type], `${type} 必须有镜像文件(#278 第一批验收项)`);
39
+ }
40
+ });
41
+
42
+ test("#278: an entry of a formerly uncovered type survives render → parse round-trip", () => {
43
+ const text = renderMirrorText("pitfall", [memory("pitfall")]);
44
+ const [edit] = parseHumanEdits(text);
45
+ assert.equal(edit.id, "m1");
46
+ assert.equal(edit.title, "标题");
47
+ assert.equal(edit.content, "内容", "frontmatter 与元数据行都不得进 content");
48
+ assert.equal(edit.updated_at, "2026-01-01T00:00:00.000Z");
49
+ });
50
+
51
+ test("#278: file header carries type / covered / coverage / tags union", () => {
52
+ const text = renderMirrorText("constraint", [
53
+ memory("constraint", { id: "m2", tags: ["z", "a"], updated_at: "2026-02-01T00:00:00.000Z" }),
54
+ memory("constraint", { id: "m1", tags: ["a", "m"] })
55
+ ]);
56
+ assert.ok(text.startsWith("---\n"), "frontmatter 必须在文件最前");
57
+ const fm = text.slice(4, text.indexOf("\n---", 3));
58
+ assert.match(fm, /^type: constraint$/m);
59
+ // OKF §7 actor 约定:<producer>/<version>,不是裸的 producer 名
60
+ assert.equal(fm.match(/^generated\.by: (.+)$/m)?.[1], `dsh-mneme/${PACKAGE_VERSION}`);
61
+ assert.match(fm, /^generated\.at: \d{4}-\d{2}-\d{2}T/m);
62
+ assert.match(fm, /^covered: 2$/m);
63
+ assert.match(fm, /^coverage: active-only$/m);
64
+ assert.match(fm, /^tags: \["a", "m", "z"\]$/m);
65
+ // frontmatter 不是条目、也不改条目数
66
+ assert.equal(parseHumanEdits(text).length, 2);
67
+ });
68
+
69
+ test("#278/#296: mirror writes the formerly uncovered files, and documents land as a pointer view", () => {
70
+ const dir = tempDir();
71
+ try {
72
+ const mirror = createMirror(dir);
73
+ const rows = FORMERLY_UNCOVERED.map((type, i) => memory(type, { id: `m${i}`, content: `内容${i}` }));
74
+ rows.push(memory("document", {
75
+ id: "doc1", title: "报告", content: "第一句。第二句不该出现。", doc_path: "C:/docs/report.md"
76
+ }));
77
+ mirror.sync(rows);
78
+ for (const [i, type] of FORMERLY_UNCOVERED.entries()) {
79
+ const file = join(dir, TYPE_FILE[type]);
80
+ assert.ok(existsSync(file), `${TYPE_FILE[type]} 必须落盘`);
81
+ const [edit] = parseHumanEdits(readFileSync(file, "utf8"));
82
+ assert.equal(edit.id, `m${i}`);
83
+ assert.equal(edit.content, `内容${i}`);
84
+ }
85
+ // #296 第 4 条:document 落盘,但只给指针行——id + 标题 + 摘要首句 + 路径,
86
+ // 不含正文;文件头必须说清这是只读视图(不能复用「可编辑」那句)。
87
+ assert.ok(MIRROR_READONLY_TYPES.has("document"), "document 是只读 type");
88
+ assert.ok(existsSync(join(dir, "documents.md")), "document 指针行现在落盘");
89
+ const docs = readFileSync(join(dir, "documents.md"), "utf8");
90
+ assert.match(docs, /只读视图/);
91
+ assert.ok(docs.includes("doc1") && docs.includes("报告") && docs.includes("第一句"));
92
+ assert.ok(docs.includes("report.md"), "指针行必须给出 doc_path,否则「看着完整、其实找不到文件」的老问题原样回来");
93
+ assert.ok(!docs.includes("第二句"), "只读视图不含全文,只留摘要首句");
94
+ assert.equal(parseHumanEdits(docs).length, 0, "指针行不是可回填的条目");
95
+ } finally {
96
+ rmSync(dir, { recursive: true, force: true });
97
+ }
98
+ });
99
+
100
+ test("#296: readHumanEdits never returns the read-only type — a hand-edited view cannot reach the store", () => {
101
+ const dir = tempDir();
102
+ try {
103
+ const mirror = createMirror(dir);
104
+ mirror.sync([memory("document", { id: "doc1", doc_path: "/tmp/report.md" })]);
105
+ // 模拟有人在 documents.md 里手改(视图是机器所有,改动必须被忽略而不是回填)
106
+ const file = join(dir, TYPE_FILE.document);
107
+ writeFileSync(file, readFileSync(file, "utf8") + "- **ID**: `doc1`\n- **类型**: document\n被人改过的正文\n", "utf8");
108
+ assert.deepEqual(mirror.readHumanEdits("document"), [], "指定的只读 type 也要跳过");
109
+ assert.deepEqual(mirror.readHumanEdits().filter((e) => e.id === "doc1"), [], "全量扫描同样跳过它");
110
+ } finally {
111
+ rmSync(dir, { recursive: true, force: true });
112
+ }
113
+ });
114
+
115
+ test("#278: mirror covers every active row — no silent 500-row truncation", () => {
116
+ const dir = tempDir();
117
+ let store;
118
+ try {
119
+ store = createStore(join(dir, "memory.db"));
120
+ const service = createService({ store, mirror: createMirror(dir), config: {} });
121
+ for (let i = 0; i < 501; i++) store.save({ type: "pitfall", title: `t${i}`, content: `c${i}` });
122
+ // 任意一次业务写触发一次镜像同步(不是逐条 save,避免 501 次渲染)
123
+ service.saveWithDedupe({ type: "pattern", title: "trigger", content: "x" });
124
+ const text = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
125
+ assert.match(text, /^covered: 501$/m, "文件头必须如实自述覆盖条数");
126
+ assert.equal((text.match(/^- \*\*ID\*\*: /gm) ?? []).length, 501, "超过 500 条的活跃集必须全部落盘");
127
+ assert.equal(parseHumanEdits(text).length, 501);
128
+ } finally {
129
+ // close 必须在 rm 之前:Windows 上库文件仍被打开时 rmSync 会 EPERM,把断言
130
+ // 失败覆盖成文件锁错误。
131
+ store?.close();
132
+ rmSync(dir, { recursive: true, force: true });
133
+ }
134
+ });
135
+
136
+ test("#278: scope / sensitivity 只作展示字段,且永不进 content", () => {
137
+ const text = renderMirrorText("constraint", [memory("constraint", {
138
+ agent_scope: "standard",
139
+ workspace_scope: "D:/WorkSpace/demo",
140
+ sensitivity: "personal"
141
+ })]);
142
+ assert.match(text, /^- \*\*作用域\*\*: standard \/ D:\/WorkSpace\/demo$/m);
143
+ assert.match(text, /^- \*\*敏感度\*\*: personal$/m);
144
+ assert.equal(parseHumanEdits(text)[0].content, "内容", "新字段不得进 content");
145
+ // 没值的条目不该多出空字段行(本机 2843 条里只有 37 条带 scope 标签)
146
+ const bare = renderMirrorText("constraint", [memory("constraint")]);
147
+ assert.ok(!bare.includes("作用域") && !bare.includes("敏感度"));
148
+ // 值是调用方传的任意字符串:里面的换行不得伪造出第二个条目头
149
+ const injected = renderMirrorText("constraint", [memory("constraint", {
150
+ sensitivity: "x\n- **ID**: `ghost`\n- **类型**: decision"
151
+ })]);
152
+ assert.equal(parseHumanEdits(injected).length, 1, "值里的换行不得造出幽灵条目");
153
+ // 值本身仍在(被压成一行),关键是它不再位于行首——行首才是条目头的锚
154
+ assert.ok(!/^- \*\*ID\*\*: `ghost`/m.test(injected), "不得出现伪造的条目头");
155
+ });
156
+
157
+ test("#278: mirror keeps its active-only claim — archived and forgotten rows stay out", () => {
158
+ const dir = tempDir();
159
+ let store;
160
+ try {
161
+ store = createStore(join(dir, "memory.db"));
162
+ const service = createService({ store, mirror: createMirror(dir), config: {} });
163
+ // forgotten 在 INSERT 里硬编码为 0(store.js insertMemoryRow),只能经 setForget
164
+ // 设置;archived 可以在载荷里给,这里统一走 setter 保持同一个路径。
165
+ for (const [id, title] of [["keep-c", "留"], ["arch-c", "归档"], ["gone-c", "遗忘"]]) {
166
+ store.save({ id, type: "constraint", title, content: "c" });
167
+ }
168
+ store.setArchived("arch-c", true);
169
+ store.setForget("gone-c", true);
170
+ service.saveWithDedupe({ type: "pattern", title: "trigger", content: "x" });
171
+ const text = readFileSync(join(dir, TYPE_FILE.constraint), "utf8");
172
+ // 文件头写 coverage: active-only,正文就必须只有活跃行——说一套写一套比没有
173
+ // 这个字段更糟(外部工具照着聚合)。
174
+ assert.match(text, /^covered: 1$/m, "覆盖条数只数活跃行");
175
+ assert.ok(text.includes("keep-c"));
176
+ assert.ok(!text.includes("arch-c"), "归档行不进镜像");
177
+ assert.ok(!text.includes("gone-c"), "遗忘行不进镜像");
178
+ } finally {
179
+ store?.close();
180
+ rmSync(dir, { recursive: true, force: true });
181
+ }
182
+ });
183
+
184
+ test("#278: re-syncing unchanged content does not rewrite the file", () => {
185
+ const dir = tempDir();
186
+ try {
187
+ const mirror = createMirror(dir);
188
+ const rows = [memory("pitfall"), memory("constraint", { id: "m2" })];
189
+ mirror.sync(rows);
190
+ const first = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
191
+ const firstAt = first.match(/^generated\.at: (.+)$/m)[1];
192
+ // generated.at 每次渲染都是新时间戳:无脑落盘会让编辑器、同步盘与 git 在每次
193
+ // 业务写后都看到全部镜像「被外部修改」,而内容一字未变。
194
+ mirror.sync(rows);
195
+ const second = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
196
+ assert.equal(second, first, "内容未变的文件不得被重写");
197
+ assert.equal(second.match(/^generated\.at: (.+)$/m)[1], firstAt, "未变时保留原时间戳");
198
+ // 内容真变了必须落盘
199
+ mirror.sync([memory("pitfall", { content: "改了" })]);
200
+ const third = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
201
+ assert.notEqual(third, second);
202
+ assert.ok(third.includes("改了"));
203
+ // 顺手确认「空 type 删文件」这条防复活行为没被上面的跳过逻辑带坏
204
+ assert.ok(!existsSync(join(dir, TYPE_FILE.constraint)), "该 type 没行时文件要删掉");
205
+ } finally {
206
+ rmSync(dir, { recursive: true, force: true });
207
+ }
208
+ });
@@ -233,3 +233,21 @@ test("production search never writes recall_evals, even with the switch on", asy
233
233
  assert.deepEqual(store.listRecallEvals(), [], "recall_evals stays untouched by production search");
234
234
  store.close();
235
235
  });
236
+
237
+ // ---------------------------------------------------------------- per-source signals in receipt
238
+
239
+ test("recall receipt: candidates carry per-source signals for offline fusion-weight profiling", async () => {
240
+ const { store, service } = setup({});
241
+ const captured = [];
242
+ service.setRecallRecorder((recall) => captured.push(recall));
243
+ saveMemory(service, "量子计算入门", "叠加态");
244
+ const rows = await service.searchMemories("量子", { mode: "hybrid", recordRecall: true });
245
+ assert.ok(rows.length >= 1, "search returned results");
246
+ assert.equal(captured.length, 1, "exactly one receipt");
247
+ for (const c of captured[0].candidates) {
248
+ assert.ok(c.signals && typeof c.signals === "object", "every candidate carries a signals object");
249
+ assert.ok("keyword" in c.signals, "raw keyword score present (pre-fusion, unweighted)");
250
+ assert.ok(!("final" in c.signals), "raw signals only — the fused score already lives in c.score");
251
+ }
252
+ store.close();
253
+ });
@@ -121,3 +121,69 @@ test("windowDays/exemptDays 越界回默认;扫描超上限标 truncated", ()
121
121
  assert.equal(total, 2, "窗口总数不截断 → truncated = total > rows.length");
122
122
  store.close();
123
123
  });
124
+
125
+ // ============================ 第五指标(#275):归档净增速率 + 可压掉行数
126
+
127
+ test("归档净增速率:按 updated_at 取归档时刻,窗外归档只进总数不进净增", () => {
128
+ const { store, service } = setup();
129
+ const fresh = service.saveWithDedupe({ type: "history", title: "刚归档", content: "x", importance: 3 }).memory;
130
+ const old = service.saveWithDedupe({ type: "history", title: "早归档", content: "y", importance: 3 }).memory;
131
+ service.saveWithDedupe({ type: "history", title: "活跃件", content: "z", importance: 3 });
132
+ store.setArchived(fresh.id, true);
133
+ store.setArchived(old.id, true);
134
+ // 归档时刻 = 最后一次写(setArchived 刷 updated_at):把这行推到窗口外
135
+ store.db.prepare("UPDATE memories SET updated_at = ? WHERE id = ?")
136
+ .run(new Date(Date.now() - 100 * 86400000).toISOString(), old.id);
137
+
138
+ const stats = service.recallStats({ windowDays: 30, exemptDays: 0 });
139
+ assert.equal(stats.archive.total, 2, "归档区现有行数(活跃件不算)");
140
+ assert.equal(stats.archive.addedInWindow, 1, "窗外那次归档不算本窗口净增");
141
+ assert.equal(stats.archive.perDay, Math.round((1 / 30) * 100) / 100);
142
+ store.close();
143
+ });
144
+
145
+ test("可压掉行数:同 type 同 scope 的内容哈希精确重复(跨 scope / 活跃行都不算)", () => {
146
+ const { store, service } = setup();
147
+ // 标题只差大小写、正文只差标点与空白 → 归一化后同哈希(#254 的口径)
148
+ const a = store.save({ type: "history", title: "Task A", content: "line1.\n\nline2" });
149
+ const b = store.save({ type: "history", title: "task a", content: "line1 line2" });
150
+ const otherScope = store.save({ type: "history", title: "Task A", content: "line1 line2", agent_scope: "other" });
151
+ store.save({ type: "history", title: "Task A", content: "line1 line2" });
152
+ for (const id of [a.id, b.id, otherScope.id]) store.setArchived(id, true);
153
+
154
+ const stats = service.recallStats({ windowDays: 30, exemptDays: 0 });
155
+ assert.equal(stats.archive.compressible.groups, 1, "跨 scope 不互判(与去重候选集同一把尺)");
156
+ assert.equal(stats.archive.compressible.rows, 1, "每组留一行,多出来的才叫可压掉");
157
+ assert.equal(stats.archive.total, 3, "跨 scope 行照进总数,只是不参与可压掉");
158
+ assert.equal(stats.archive.addedInWindow, 3, "三行都是本窗口内归档的");
159
+ store.close();
160
+ });
161
+
162
+ test("可压掉行数:向量口径不可用时的兜底——指标不依赖 embedding", () => {
163
+ // 回归点:回收动作会清掉归档行向量(clearArchivedEmbeddings),若指标建在向量近
164
+ // 重复上,回收一跑它就归零。这里清完向量后指标必须不变。
165
+ const { store, service } = setup();
166
+ const a = store.save({ type: "history", title: "同题", content: "同一件事" });
167
+ const b = store.save({ type: "history", title: "同题", content: "同一件事" });
168
+ store.setArchived(a.id, true);
169
+ store.setArchived(b.id, true);
170
+ assert.equal(service.recallStats({ windowDays: 30, exemptDays: 0 }).archive.compressible.rows, 1);
171
+
172
+ store.clearArchivedEmbeddings();
173
+ assert.equal(
174
+ service.recallStats({ windowDays: 30, exemptDays: 0 }).archive.compressible.rows,
175
+ 1,
176
+ "清向量不影响可压掉行数"
177
+ );
178
+ store.close();
179
+ });
180
+
181
+ test("没有归档行时给 0,不返回 null / NaN", () => {
182
+ const { store, service } = setup();
183
+ service.saveWithDedupe({ type: "project", title: "只有活跃", content: "x", importance: 3 });
184
+ const stats = service.recallStats({ windowDays: 30, exemptDays: 0 });
185
+ assert.deepEqual(stats.archive, {
186
+ total: 0, addedInWindow: 0, perDay: 0, compressible: { rows: 0, groups: 0 }
187
+ });
188
+ store.close();
189
+ });
@@ -408,3 +408,120 @@ test("issue#257 schema default for sleepMaxTokens is 8192 (was hardcoded 2048)",
408
408
  const cfg = Config({});
409
409
  assert.equal(cfg.sleepMaxTokens, 8192, "schema default covers the measured full-set peak (6967)");
410
410
  });
411
+
412
+ // ------------------------------------------- sleep LLM accounting (issue #250)
413
+ // Bug8 给 dream 与 summarize 接记账时漏了 sleep:src/dream/sleep.js 有一份自己的
414
+ // streamText,签名里没有 onUsage,两条 LLM 链路(conflict 裁决 / pattern 挖掘)的
415
+ // token 与状态从未进 llm_audit_logs——面板因此只看到两条链路,回答不了「sleep 花了
416
+ // 多少」。下面两条用例锁定「审计行落库 + token 数值来自 usage chunk」。
417
+ // 两条链路共用同一处 streamText 改动,故放在同一轮 red-green 里。
418
+
419
+ /** 在 base ctx 的流里、finish 之前插入一个 usage chunk(形态同 llm-audit.test.js)。 */
420
+ function withUsageChunk(base, usageChunk) {
421
+ return {
422
+ ...base,
423
+ llm: {
424
+ async *stream(options) {
425
+ for await (const chunk of base.llm.stream(options)) {
426
+ if (chunk.type === "finish") yield usageChunk;
427
+ yield chunk;
428
+ }
429
+ }
430
+ }
431
+ };
432
+ }
433
+
434
+ test("issue#250 sleep conflict phase writes an llm_audit row with the streamed tokens", async () => {
435
+ const { service, store, vectorIndex } = setup();
436
+ const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
437
+ const ctx = withUsageChunk(
438
+ mockCtx(() => JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])),
439
+ { type: "usage", usage: { inputTokens: 1200, outputTokens: 300, totalTokens: 1500 } }
440
+ );
441
+ const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
442
+ assert.equal(result.phases.conflicts.status, "ok", "conflict resolved");
443
+
444
+ const conflict = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_conflict");
445
+ assert.ok(conflict, "sleep_conflict audit row present");
446
+ assert.equal(conflict.trigger_source, "sleep");
447
+ assert.equal(conflict.status, "success");
448
+ assert.equal(conflict.model_id, "mock:sleep-model", "route actually used is recorded");
449
+ assert.equal(conflict.input_tokens, 1200, "input tokens come from chunk.usage");
450
+ assert.equal(conflict.output_tokens, 300, "output tokens come from chunk.usage");
451
+ assert.equal(conflict.total_tokens, 1500);
452
+ assert.ok(typeof conflict.duration_ms === "number" && conflict.duration_ms >= 0);
453
+ store.close();
454
+ });
455
+
456
+ test("issue#250 sleep pattern phase writes an llm_audit row with the streamed tokens", async () => {
457
+ const { service, store } = setup();
458
+ const a = makeMemory(service, "模式A", "反复出现的模式A", "project");
459
+ const b = makeMemory(service, "模式B", "反复出现的模式B", "project");
460
+ const ctx = withUsageChunk(
461
+ mockCtx(() => JSON.stringify([
462
+ { action: "create", type: "pattern", title: "真模式", content: "从 a 和 b 提取", importance: 3, evidence: [a.id, b.id] }
463
+ ])),
464
+ { type: "usage", usage: { inputTokens: 900, outputTokens: 250, totalTokens: 1150 } }
465
+ );
466
+ const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
467
+ assert.ok(result.phases.patterns, "pattern phase ran");
468
+
469
+ const pattern = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_pattern");
470
+ assert.ok(pattern, "sleep_pattern audit row present");
471
+ assert.equal(pattern.trigger_source, "sleep");
472
+ assert.equal(pattern.status, "success");
473
+ assert.equal(pattern.input_tokens, 900, "input tokens come from chunk.usage");
474
+ assert.equal(pattern.output_tokens, 250, "output tokens come from chunk.usage");
475
+ assert.equal(pattern.total_tokens, 1150);
476
+ store.close();
477
+ });
478
+
479
+ // 审计诚实性(issue #250):流式成功但输出里没有 JSON 数组时,phase 报 failed,
480
+ // 审计行也必须记 error——不能一个调用在两表里自相矛盾。
481
+ test("issue#250 sleep conflict parse failure is audited as status=error, not fake success", async () => {
482
+ const { service, store, vectorIndex } = setup();
483
+ const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
484
+ const ctx = mockCtx(() => "抱歉,我无法把裁决整理成 JSON。");
485
+ const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
486
+ assert.equal(result.phases.conflicts.status, "failed", "the phase reports the failure");
487
+
488
+ const row = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_conflict");
489
+ assert.ok(row, "the failed call is still audited");
490
+ assert.equal(row.status, "error", "a stream that returned unusable output is not a success");
491
+ assert.match(row.error_message, /no json array in llm output/);
492
+ store.close();
493
+ });
494
+
495
+ test("issue#250 sleep pattern parse failure is audited as status=error, not fake success", async () => {
496
+ const { service, store } = setup();
497
+ makeMemory(service, "模式A", "反复出现的模式A", "project");
498
+ makeMemory(service, "模式B", "反复出现的模式B", "project");
499
+ const ctx = mockCtx(() => "这不是 JSON 数组");
500
+ const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
501
+ // pattern 阶段对不可解析输出的既定语义是 skipped("no patterns found")——那是
502
+ // 「有没有铸造出模式」的结论,不是「这次调用成没成功」的结论,此处不改上游语义。
503
+ assert.equal(result.phases.patterns.status, "skipped", "the phase keeps its existing semantics");
504
+
505
+ const row = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_pattern");
506
+ assert.ok(row, "the call still consumed tokens and is still audited");
507
+ assert.equal(row.status, "error", "the audit records the call's truth, not the phase's conclusion");
508
+ assert.match(row.error_message, /no json array in llm output/);
509
+ store.close();
510
+ });
511
+
512
+ // 回归护栏:llmAudit.enabled === false 时 auditedSleepLlm 整段早退,auditError 钩子
513
+ // 不会被调用。所以两个 phase 的控制流必须各自解析输出、不能依赖那个钩子的副作用
514
+ // ——dream 的 runAuditedLlm 正是那样踩了(关掉审计 flag 会让 autoDream 直接失效)。
515
+ test("issue#250 sleep still adjudicates normally when llmAudit.enabled === false", async () => {
516
+ const { service, store, vectorIndex } = setup();
517
+ const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
518
+ const ctx = mockCtx(() =>
519
+ JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])
520
+ );
521
+ const config = baseConfig({ llmAudit: { enabled: false } });
522
+ const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
523
+ assert.equal(result.phases.conflicts.status, "ok", "turning the audit off must not break sleep");
524
+ assert.equal(service.getById(b.id).archived, true, "the arbitration still applied");
525
+ assert.equal(service.listLlmAudits().length, 0, "no audit rows when disabled");
526
+ store.close();
527
+ });