@modusensus/dsh-mneme 0.8.6 → 0.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +19 -148
  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 +349 -61
  6. package/lib/config.js +118 -7
  7. package/lib/content-hash.js +54 -0
  8. package/lib/continuity.js +220 -0
  9. package/lib/document-index.js +94 -0
  10. package/lib/document.js +93 -11
  11. package/lib/dream.js +138 -39
  12. package/lib/guide.js +18 -0
  13. package/lib/index.js +87 -12
  14. package/lib/inject.js +6 -2
  15. package/lib/lang.js +15 -4
  16. package/lib/maintenance.js +142 -0
  17. package/lib/mirror.js +147 -15
  18. package/lib/peak-hours.js +117 -0
  19. package/lib/recall-stats.js +52 -2
  20. package/lib/scope.js +34 -6
  21. package/lib/service.js +86 -7
  22. package/lib/settings.js +16 -2
  23. package/lib/store.js +318 -21
  24. package/lib/summarize.js +52 -113
  25. package/lib/tools.js +37 -11
  26. package/lib/write-admission.js +259 -0
  27. package/package.json +4 -4
  28. package/src/api-standalone.js +57 -1
  29. package/src/api.js +34 -11
  30. package/src/config.js +118 -7
  31. package/src/content-hash.js +54 -0
  32. package/src/continuity.js +220 -0
  33. package/src/document-index.js +94 -0
  34. package/src/document.js +93 -11
  35. package/src/dream.js +138 -39
  36. package/src/guide.js +18 -0
  37. package/src/index.js +87 -12
  38. package/src/inject.js +6 -2
  39. package/src/lang.js +15 -4
  40. package/src/maintenance.js +142 -0
  41. package/src/mirror.js +147 -15
  42. package/src/peak-hours.js +117 -0
  43. package/src/recall-stats.js +52 -2
  44. package/src/scope.js +34 -6
  45. package/src/service.js +86 -7
  46. package/src/settings.js +16 -2
  47. package/src/store.js +318 -21
  48. package/src/summarize.js +52 -113
  49. package/src/tools.js +37 -11
  50. package/src/write-admission.js +259 -0
  51. package/test/api.test.js +21 -3
  52. package/test/client.test.js +103 -6
  53. package/test/content-hash.test.js +204 -0
  54. package/test/continuity.test.js +333 -0
  55. package/test/document-dir.test.js +233 -0
  56. package/test/document.test.js +151 -3
  57. package/test/dream-failure-backoff.test.js +173 -0
  58. package/test/dream-peak-hours.test.js +194 -0
  59. package/test/inject-parent-gate.test.js +106 -0
  60. package/test/inject-pools.test.js +8 -3
  61. package/test/llm-audit.test.js +42 -0
  62. package/test/maintenance.test.js +391 -0
  63. package/test/mirror-types.test.js +208 -0
  64. package/test/reasoning-effort.test.js +6 -0
  65. package/test/recall-evals.test.js +18 -0
  66. package/test/recall-stats.test.js +66 -0
  67. package/test/summarize-reasoning-effort.test.js +189 -0
  68. 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
+ });
@@ -48,11 +48,17 @@ test("issue#135: reasoningEffort unset = undefined (auto-lowest at resolve time)
48
48
  const cfg = Config({});
49
49
  assert.equal(cfg.dreamReasoningEffort, undefined, "unset must be distinguishable from explicit 'none'");
50
50
  assert.equal(cfg.sleepReasoningEffort, undefined);
51
+ // issue #315: the distill knob defaults to 'none' (entity-extraction shape,
52
+ // no auto-lowest resolution — distill keeps its current behavior until the
53
+ // user opts in).
54
+ assert.equal(cfg.summarizeReasoningEffort, "none", "distill effort defaults to 'none'");
55
+ assert.equal(Config({ summarizeReasoningEffort: "off" }).summarizeReasoningEffort, "off");
51
56
  assert.equal(Config({ dreamReasoningEffort: "none" }).dreamReasoningEffort, "none", "explicit 'none' kept");
52
57
  assert.equal(Config({ dreamReasoningEffort: "off" }).dreamReasoningEffort, "off", "'off' now a valid explicit choice");
53
58
  assert.equal(Config({ sleepReasoningEffort: "medium" }).sleepReasoningEffort, "medium");
54
59
  assert.throws(() => Config({ dreamReasoningEffort: "bogus" }), "invalid effort rejected");
55
60
  assert.throws(() => Config({ sleepReasoningEffort: "ultra" }), "invalid effort rejected");
61
+ assert.throws(() => Config({ summarizeReasoningEffort: "max" }), "invalid distill effort rejected");
56
62
  });
57
63
 
58
64
  // ---------------------------------------------------------------- dream passthrough
@@ -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
+ });
@@ -0,0 +1,189 @@
1
+ // Regression for issue #315: the distill (summarize) LLM call gets a reasoning
2
+ // effort knob, mirroring the entity-extraction shape (issue #109) and reusing
3
+ // the dream/sleep fallback machinery. Pins:
4
+ // - explicit effort is forwarded on the distill stream call;
5
+ // - default 'none' omits the field entirely (current behavior unchanged);
6
+ // - a stream-level effort rejection (error finish chunk, the rc.1 shape that
7
+ // never reaches catch) is retried once WITHOUT the field;
8
+ // - non-effort stream failures are NOT retried (no blind double calls) and
9
+ // the aborted window stays retryable (cursor not consumed);
10
+ // - 'off' works like any explicit effort (rejected → retried without it).
11
+ // Mock shapes (ctx.on subscription capture, {delta} text chunks, finish
12
+ // {reason:{kind,failure}}) follow the existing summarize.test.js conventions.
13
+ import test from "node:test";
14
+ import assert from "node:assert/strict";
15
+ import { createSummarizer } from "../src/summarize.js";
16
+ import { createStore } from "../src/store.js";
17
+ import { createService } from "../src/service.js";
18
+
19
+ const ENTRIES_JSON = JSON.stringify([
20
+ { type: "decision", title: "选型", content: "确定用 node:sqlite", importance: 4 }
21
+ ]);
22
+
23
+ function userMessage(text, seq) {
24
+ return {
25
+ seq,
26
+ type: "user/message",
27
+ data: { source: { kind: "user" }, content: [{ type: "text", text }] }
28
+ };
29
+ }
30
+
31
+ function setup(configOver = {}, stream) {
32
+ const store = createStore(":memory:");
33
+ const service = createService({ store, mirror: null, config: {} });
34
+ const calls = [];
35
+ const events = [];
36
+ const ctx = {
37
+ on(name, fn) {
38
+ events.push({ name, fn });
39
+ return () => {};
40
+ },
41
+ llm: {
42
+ stream(options) {
43
+ calls.push(options);
44
+ return stream(options);
45
+ }
46
+ }
47
+ };
48
+ const config = { autoSummarize: true, distillRateLimitIntervalMs: 0, ...configOver };
49
+ createSummarizer(ctx, service, config, {});
50
+ const handler = events.find((e) => e.name === "session/event").fn;
51
+ const session = {
52
+ id: "s315",
53
+ requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
54
+ events: [userMessage("帮我选型", 1), { seq: 2, type: "turn/end" }]
55
+ };
56
+ return { store, service, calls, handler, session };
57
+ }
58
+
59
+ test("issue#315: explicit summarizeReasoningEffort is forwarded on the distill call", async () => {
60
+ const { store, calls, handler, session } = setup(
61
+ { summarizeReasoningEffort: "low" },
62
+ () => (async function* () {
63
+ yield { type: "text-delta", delta: ENTRIES_JSON };
64
+ yield { type: "finish", kind: "ok" };
65
+ })()
66
+ );
67
+ await handler(session, { seq: 2, type: "turn/end" });
68
+ assert.equal(calls.length, 1);
69
+ assert.equal(calls[0].reasoningEffort, "low", "the configured effort rides the distill call");
70
+ store.close();
71
+ });
72
+
73
+ test("issue#315: default 'none' omits the reasoningEffort field entirely", async () => {
74
+ const { store, calls, handler, session } = setup(
75
+ {},
76
+ () => (async function* () {
77
+ yield { type: "text-delta", delta: ENTRIES_JSON };
78
+ yield { type: "finish", kind: "ok" };
79
+ })()
80
+ );
81
+ await handler(session, { seq: 2, type: "turn/end" });
82
+ assert.equal(calls.length, 1);
83
+ assert.equal("reasoningEffort" in calls[0], false, "unset/'none' must not add the field");
84
+ assert.equal(store.count(), 1, "entries still stored as before");
85
+ store.close();
86
+ });
87
+
88
+ test("issue#315: stream-level effort rejection retries once without the field", async () => {
89
+ const { store, calls, handler, session } = setup(
90
+ { summarizeReasoningEffort: "low" },
91
+ (options) => (async function* () {
92
+ if (options.reasoningEffort) {
93
+ // rc.1 shape: the provider rejection arrives as a terminal error finish
94
+ // chunk (adapterStream never throws), so the retry must be triggered
95
+ // from the chunk path, not a catch block.
96
+ yield {
97
+ type: "finish",
98
+ reason: {
99
+ kind: "error",
100
+ failure: {
101
+ code: "UNSUPPORTED_REASONING_EFFORT",
102
+ message: 'provider "deepseek" model "deepseek-chat" does not support reasoning effort "low"'
103
+ }
104
+ }
105
+ };
106
+ return;
107
+ }
108
+ yield { type: "text-delta", delta: ENTRIES_JSON };
109
+ yield { type: "finish", kind: "ok" };
110
+ })()
111
+ );
112
+ await handler(session, { seq: 2, type: "turn/end" });
113
+ assert.equal(calls.length, 2, "exactly one no-effort retry");
114
+ assert.equal(calls[0].reasoningEffort, "low", "first attempt forwards the effort");
115
+ assert.equal("reasoningEffort" in calls[1], false, "retry omits the rejected effort field");
116
+ assert.equal(store.count(), 1, "the retried run still stores its entry");
117
+ store.close();
118
+ });
119
+
120
+ test("issue#315: non-effort stream failures are not retried and stay abortable", async () => {
121
+ const { store, calls, handler, session } = setup(
122
+ { summarizeReasoningEffort: "low" },
123
+ () => (async function* () {
124
+ yield { type: "finish", reason: { kind: "error", failure: { code: "PROVIDER_GONE", message: "provider deepseek is not registered" } } };
125
+ })()
126
+ );
127
+ await handler(session, { seq: 2, type: "turn/end" });
128
+ assert.equal(calls.length, 1, "no blind retry when the stream failure is not an effort rejection");
129
+ assert.equal(store.count(), 0, "failed window stores nothing");
130
+ store.close();
131
+ });
132
+
133
+ test("issue#315: off works like any explicit effort (rejected → retried without it)", async () => {
134
+ const { store, calls, handler, session } = setup(
135
+ { summarizeReasoningEffort: "off" },
136
+ (options) => (async function* () {
137
+ if (options.reasoningEffort) {
138
+ yield { type: "finish", reason: { kind: "error", failure: { code: "UNSUPPORTED_REASONING_EFFORT", message: 'reasoning effort "off" is not supported here' } } };
139
+ return;
140
+ }
141
+ yield { type: "text-delta", delta: ENTRIES_JSON };
142
+ yield { type: "finish", kind: "ok" };
143
+ })()
144
+ );
145
+ await handler(session, { seq: 2, type: "turn/end" });
146
+ assert.equal(calls.length, 2);
147
+ assert.equal(calls[0].reasoningEffort, "off");
148
+ assert.equal(store.count(), 1);
149
+ store.close();
150
+ });
151
+
152
+ test("issue#315: successful effort-fallback retry records audit as success, not the first failure", async () => {
153
+ const { store, service, calls, handler, session } = setup(
154
+ { summarizeReasoningEffort: "low" },
155
+ (options) => (async function* () {
156
+ if (options.reasoningEffort) {
157
+ // first attempt is rejected (effort unsupported)…
158
+ yield { type: "finish", reason: { kind: "error", failure: { code: "UNSUPPORTED_REASONING_EFFORT", message: 'reasoning effort "low" rejected' } } };
159
+ return;
160
+ }
161
+ // …but the no-effort retry succeeds.
162
+ yield { type: "text-delta", delta: ENTRIES_JSON };
163
+ yield { type: "finish", kind: "ok" };
164
+ })()
165
+ );
166
+ await handler(session, { seq: 2, type: "turn/end" });
167
+ assert.equal(calls.length, 2, "effort rejection + one retry");
168
+ const rows = service.listLlmAudits();
169
+ const summarize = rows.find((r) => r.operation_type === "summarize_compress");
170
+ assert.ok(summarize, "summarize audit row present");
171
+ assert.equal(summarize.status, "success",
172
+ "the retried run must record success, not the first attempt's error status");
173
+ store.close();
174
+ });
175
+
176
+ test("issue#315: abort is not misread as an effort rejection even when the message matches the pattern", async () => {
177
+ const { store, calls, handler, session } = setup(
178
+ { summarizeReasoningEffort: "low" },
179
+ () => (async function* () {
180
+ // The message matches EFFORT_REJECT_RE on purpose: without the AbortError
181
+ // guard in withEffortFallback this would be misread as an effort rejection
182
+ // and trigger a wasteful no-effort retry.
183
+ throw Object.assign(new Error("aborted by dispose: UNSUPPORTED_REASONING_EFFORT"), { name: "AbortError" });
184
+ })()
185
+ );
186
+ await handler(session, { seq: 2, type: "turn/end" });
187
+ assert.equal(calls.length, 1, "abort must not trigger a no-effort fallback retry");
188
+ store.close();
189
+ });