@modusensus/dsh-mneme 0.8.7 → 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.
@@ -0,0 +1,173 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { createDreamScheduler } from "../src/dream.js";
4
+ import { createStore } from "../src/store.js";
5
+ import { createService } from "../src/service.js";
6
+
7
+ // Issue #292:autoDream 同会话内连续失败退避(autoDreamFailureBackoff,opt-in)。
8
+ //
9
+ // 这些用例锁的是「调度器节流语义」,不是实现细节——防的回归是:
10
+ // ① 退避闸失效/写反 → 恒定失败的模型(#135 空体面)按 dreamMinIntervalMinutes
11
+ // 的固定节奏连发刷配额(#89 只挡了间隔内重试,间隔本身不会变长);
12
+ // ② 成功后连败计数不清零 → 模型恢复正常后仍被旧失败拖着重试推迟(巩固变相停摆);
13
+ // ③ 30 分钟封顶失效 → 指数无限翻倍,一整天都不再巩固;
14
+ // ④ 默认关路径被顺手改掉 → 未 opt-in 用户的重试间隔漂移(默认关 = 行为与
15
+ // 现状逐字节一致的承诺);
16
+ // ⑤ 0 基数被偷偷补了间隔 → 未配 dreamMinIntervalMinutes 的用户开退避即被限流
17
+ // (文档口径:基数 0 时本键不自己产生间隔)。
18
+ //
19
+ // 时钟与定时器全部注入(同 test/dream-peak-hours.test.js 的房型):「失败 → 推迟
20
+ // → 放行」才能被确定性覆盖,也不在 CI 上留真实等待。退避从上次开跑(lastRunAt)
21
+ // 起算:失败 run 本就占用基数间隔(#89 现状,lastRunAt 在开跑时刷新),退避放大
22
+ // 的是同一道闸,不新增状态面。
23
+ function dreamSetup() {
24
+ const store = createStore(":memory:");
25
+ const service = createService({ store, mirror: null, config: {} });
26
+ return { store, service };
27
+ }
28
+
29
+ function fakeClock(startMs) {
30
+ let nowMs = startMs;
31
+ const timers = [];
32
+ let seq = 1;
33
+ return {
34
+ now: () => nowMs,
35
+ setNow: (v) => { nowMs = v; },
36
+ timers,
37
+ setTimeoutFn: (fn, delay) => { const t = { id: seq++, at: nowMs + delay, fn }; timers.push(t); return t.id; },
38
+ clearTimeoutFn: (id) => { const i = timers.findIndex((t) => t.id === id); if (i >= 0) timers.splice(i, 1); }
39
+ };
40
+ }
41
+
42
+ const T0 = new Date(2026, 8, 27, 10, 0, 0, 0).getTime();
43
+
44
+ /**
45
+ * 触发一次并等 run 真正落完:断言闸门这次是放行的、恰好挂了一个 delayMs=0 的
46
+ * 执行定时器,点火后用 setImmediate 冲掉 startRun 的 Promise 链(纯微任务冲刷,
47
+ * 不留真实等待)。失败路径下 baseline 不刷新,同一份写入可持续触发。
48
+ */
49
+ async function runOnce(clock, dream, service) {
50
+ assert.equal(dream.maybeSchedule(service), true, "前置:阈值已过、间隔闸放行");
51
+ const ts = clock.timers.splice(0);
52
+ assert.equal(ts.length, 1, "前置:恰好挂了一个执行定时器");
53
+ ts[0].fn();
54
+ await new Promise((r) => setImmediate(r));
55
+ }
56
+
57
+ test("默认关:失败后重试间隔与现状一致(基数,不翻倍)", async () => {
58
+ const { store, service } = dreamSetup();
59
+ const clock = fakeClock(T0);
60
+ let runs = 0;
61
+ const dream = createDreamScheduler({
62
+ onRun: async () => { runs++; return { ok: false, error: "llm failed" }; },
63
+ thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 100,
64
+ failureBackoff: false, // 显式默认值:锁「默认关 = 行为逐字节不变」
65
+ logger: { warn: () => {} },
66
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn
67
+ });
68
+ service.saveWithDedupe({ type: "project", title: "a", content: "x" });
69
+ await runOnce(clock, dream, service); // 失败一次:失败 run 占用基数间隔(#89 现状)
70
+ assert.equal(runs, 1);
71
+ clock.setNow(T0 + 50);
72
+ assert.equal(dream.maybeSchedule(service), false, "基数间隔内照旧跳过");
73
+ clock.setNow(T0 + 100);
74
+ assert.equal(dream.maybeSchedule(service), true, "关键:基数间隔一到即放行——不翻倍");
75
+ store.close();
76
+ });
77
+
78
+ test("开启:连续失败 2 次 → 第 3 次触发被指数推迟", async () => {
79
+ const { store, service } = dreamSetup();
80
+ const clock = fakeClock(T0);
81
+ let runs = 0;
82
+ const dream = createDreamScheduler({
83
+ onRun: async () => { runs++; return { ok: false, error: "empty body" }; },
84
+ thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 100,
85
+ failureBackoff: true,
86
+ logger: { warn: () => {} },
87
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn
88
+ });
89
+ service.saveWithDedupe({ type: "project", title: "a", content: "x" });
90
+ await runOnce(clock, dream, service); // 败 1:effective = 100 × 2^1 = 200
91
+ assert.equal(runs, 1);
92
+ clock.setNow(T0 + 100);
93
+ assert.equal(dream.maybeSchedule(service), false, "关键:基数到点不放行(现状会在 +100 放行)");
94
+ clock.setNow(T0 + 200);
95
+ await runOnce(clock, dream, service); // 败 2:effective = 100 × 2^2 = 400
96
+ assert.equal(runs, 2);
97
+ clock.setNow(T0 + 200 + 300);
98
+ assert.equal(dream.maybeSchedule(service), false, "关键:2 次连败后 400ms 内不放行");
99
+ clock.setNow(T0 + 200 + 400);
100
+ assert.equal(dream.maybeSchedule(service), true, "自上次开跑起满 400ms 才放行第 3 次");
101
+ store.close();
102
+ });
103
+
104
+ test("成功一次后计数清零、间隔恢复基数", async () => {
105
+ const { store, service } = dreamSetup();
106
+ const clock = fakeClock(T0);
107
+ let failNext = true;
108
+ const dream = createDreamScheduler({
109
+ onRun: async () => ({ ok: !failNext }),
110
+ thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 100,
111
+ failureBackoff: true,
112
+ logger: { warn: () => {} },
113
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn
114
+ });
115
+ service.saveWithDedupe({ type: "project", title: "a", content: "x" });
116
+ await runOnce(clock, dream, service); // 败 1:effective = 200
117
+ clock.setNow(T0 + 100);
118
+ assert.equal(dream.maybeSchedule(service), false, "前置:退避确实在挡(基数到点不放行)");
119
+ clock.setNow(T0 + 200);
120
+ failNext = false;
121
+ await runOnce(clock, dream, service); // 成功:连败清零、baseline 刷新为当前库量
122
+ service.saveWithDedupe({ type: "project", title: "b", content: "y" }); // 补过刷新后的阈值
123
+ clock.setNow(T0 + 300); // 距上次开跑 +100 = 基数
124
+ assert.equal(dream.maybeSchedule(service), true, "关键:成功清零后基数即放行(未清零要等 +400)");
125
+ store.close();
126
+ });
127
+
128
+ test("cap:连败 10 次 → 推迟量封顶 30 分钟(未封顶需等约 34 小时)", async () => {
129
+ const { store, service } = dreamSetup();
130
+ const clock = fakeClock(T0);
131
+ const BASE = 2 * 60000; // 2 分钟基数:2^10 × 基数 ≈ 34h,30 分钟封顶必被触发
132
+ let runs = 0;
133
+ const dream = createDreamScheduler({
134
+ onRun: async () => { runs++; return { ok: false }; },
135
+ thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: BASE,
136
+ failureBackoff: true,
137
+ logger: { warn: () => {} },
138
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn
139
+ });
140
+ service.saveWithDedupe({ type: "project", title: "a", content: "x" });
141
+ let lastFailAt = T0;
142
+ for (let i = 1; i <= 10; i++) {
143
+ if (i > 1) {
144
+ const wait = Math.min(BASE * 2 ** (i - 1), 30 * 60000); // 与实现同一口径的期望间隔
145
+ clock.setNow(lastFailAt + wait);
146
+ }
147
+ await runOnce(clock, dream, service);
148
+ lastFailAt = clock.now();
149
+ }
150
+ assert.equal(runs, 10);
151
+ clock.setNow(lastFailAt + 30 * 60000 - 1);
152
+ assert.equal(dream.maybeSchedule(service), false, "关键:封顶后 30 分钟内仍不放行");
153
+ clock.setNow(lastFailAt + 30 * 60000);
154
+ assert.equal(dream.maybeSchedule(service), true, "封顶生效:恰好 30 分钟放行,不再随失败数翻倍");
155
+ store.close();
156
+ });
157
+
158
+ test("基数为 0 时退避不自己产生间隔(需先配 dreamMinIntervalMinutes)", async () => {
159
+ const { store, service } = dreamSetup();
160
+ const clock = fakeClock(T0);
161
+ const dream = createDreamScheduler({
162
+ onRun: async () => ({ ok: false }),
163
+ thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 0,
164
+ failureBackoff: true,
165
+ logger: { warn: () => {} },
166
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn
167
+ });
168
+ service.saveWithDedupe({ type: "project", title: "a", content: "x" });
169
+ await runOnce(clock, dream, service); // 失败一次
170
+ clock.setNow(T0);
171
+ assert.equal(dream.maybeSchedule(service), true, "关键:0 基数无闸可翻倍,开退避也不限流");
172
+ store.close();
173
+ });
@@ -0,0 +1,194 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { createDreamScheduler } from "../src/dream.js";
4
+ import { createStore } from "../src/store.js";
5
+ import { createService } from "../src/service.js";
6
+
7
+ // Issue #239(第 4 项,错峰队列)镜像到巩固:dreamPeakHours / dreamPeakMaxDeferMinutes。
8
+ //
9
+ // 这些用例锁的是「排程语义」,不是实现细节——它们防的回归是:
10
+ // ① 错峰闸门被写反/失效 → 高峰期又开始做梦(省钱能力静默消失);
11
+ // ② 顺延路径把 skip 做成静默 → 用户看到「不再巩固了」却查不出原因;
12
+ // ③ 顺延没有上限 → 长高峰(如全天)把巩固永久饿死;
13
+ // ④ 非法时段串被当成「全天高峰」→ 一个笔误把功能整个停掉;
14
+ // ⑤ dispose 漏清顺延定时器 → 进程关掉后仍触发一次巩固。
15
+ //
16
+ // 时钟与定时器全部注入(同 src/dream/sleep.js 的房型):排程不绑死真实时钟,
17
+ // 「高峰顺延 → 非高峰补跑」才能被确定性覆盖,也不会在 CI 上留下真实等待。
18
+ function dreamSetup() {
19
+ const store = createStore(":memory:");
20
+ const service = createService({ store, mirror: null, config: {} });
21
+ return { store, service };
22
+ }
23
+
24
+ /** 本地墙钟时间:用本地时区构造,getHours() 在 ubuntu/windows CI 上都等于入参。 */
25
+ function at(hour, minute = 0) {
26
+ return new Date(2026, 5, 15, hour, minute, 0, 0).getTime();
27
+ }
28
+
29
+ function fakeClock(startMs) {
30
+ let nowMs = startMs;
31
+ const timers = [];
32
+ let seq = 1;
33
+ return {
34
+ now: () => nowMs,
35
+ setNow: (v) => { nowMs = v; },
36
+ timers,
37
+ setTimeoutFn: (fn, delay) => { const t = { id: seq++, at: nowMs + delay, fn }; timers.push(t); return t.id; },
38
+ clearTimeoutFn: (id) => { const i = timers.findIndex((t) => t.id === id); if (i >= 0) timers.splice(i, 1); }
39
+ };
40
+ }
41
+
42
+ /** 造出「超过阈值」的写入量(thresholdCount=2)。 */
43
+ function fill(service, n = 2) {
44
+ for (let i = 0; i < n; i++) service.saveWithDedupe({ type: "project", title: `m${i}`, content: "x".repeat(50) });
45
+ }
46
+
47
+ test("dreamPeakHours: 命中高峰不调模型,登记 skip 审计并顺延", () => {
48
+ const { store, service } = dreamSetup();
49
+ const clock = fakeClock(at(10, 0)); // 本地 10:00,落在 09:00-18:00 内
50
+ let runs = 0;
51
+ const audits = [];
52
+ const dream = createDreamScheduler({
53
+ onRun: async () => { runs++; return { ok: true }; },
54
+ thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
55
+ peakHours: "09:00-18:00",
56
+ peakMaxDeferMinutes: 0, // 不设上限:顺延应落在真正的高峰结束时刻
57
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
58
+ auditPeakSkip: (info) => audits.push(info)
59
+ });
60
+ fill(service);
61
+ assert.equal(dream.maybeSchedule(service), false, "高峰内不排「马上跑」");
62
+ assert.equal(runs, 0, "高峰内绝不调 LLM");
63
+ assert.equal(audits.length, 1, "skip 必须留痕,否则「为什么不再巩固」不可观测");
64
+ assert.equal(audits[0].count, 2, "审计带上窗口里积了多少(阈值继续累积,不是丢弃)");
65
+ assert.equal(clock.timers.length, 1, "挂了一个顺延定时器");
66
+ assert.equal(clock.timers[0].at, at(18, 0), "顺延到最近的高峰结束时刻");
67
+ store.close();
68
+ });
69
+
70
+ test("dreamPeakHours: 顺延到点后补跑(非高峰真的会跑)", async () => {
71
+ const { store, service } = dreamSetup();
72
+ const clock = fakeClock(at(10, 0));
73
+ let runs = 0;
74
+ const dream = createDreamScheduler({
75
+ onRun: async () => { runs++; return { ok: true }; },
76
+ thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
77
+ peakHours: "09:00-18:00",
78
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
79
+ auditPeakSkip: () => {}
80
+ });
81
+ fill(service);
82
+ dream.maybeSchedule(service);
83
+ const deferred = clock.timers[0];
84
+ clock.setNow(deferred.at); // 走到 18:00
85
+ await deferred.fn();
86
+ assert.equal(runs, 1, "出高峰后补跑一次");
87
+ store.close();
88
+ });
89
+
90
+ test("dreamPeakHours: 重复触发不叠加顺延定时器", () => {
91
+ const { store, service } = dreamSetup();
92
+ const clock = fakeClock(at(10, 0));
93
+ let audits = 0;
94
+ const dream = createDreamScheduler({
95
+ onRun: async () => ({ ok: true }),
96
+ thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
97
+ peakHours: "09:00-18:00",
98
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
99
+ auditPeakSkip: () => { audits++; }
100
+ });
101
+ fill(service);
102
+ dream.maybeSchedule(service);
103
+ dream.maybeSchedule(service); // 顺延期间又来一次写入触发
104
+ assert.equal(clock.timers.length, 1, "同一时刻只有一个顺延定时器");
105
+ assert.equal(audits, 1, "不重复刷审计行");
106
+ store.close();
107
+ });
108
+
109
+ test("dreamPeakMaxDeferMinutes: 上限截断后到点照跑,长高峰不饿死巩固", async () => {
110
+ const { store, service } = dreamSetup();
111
+ const clock = fakeClock(at(10, 0));
112
+ let runs = 0;
113
+ const dream = createDreamScheduler({
114
+ onRun: async () => { runs++; return { ok: true }; },
115
+ thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
116
+ peakHours: "00:00-23:59", // 全天高峰:不设上限就会顺延到 23:59
117
+ peakMaxDeferMinutes: 120,
118
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
119
+ auditPeakSkip: () => {}
120
+ });
121
+ fill(service);
122
+ dream.maybeSchedule(service);
123
+ const t = clock.timers[0];
124
+ assert.equal(t.at, at(12, 0), "被上限截断到 120 分钟后(而不是等到 23:59)");
125
+ clock.setNow(t.at);
126
+ await t.fn();
127
+ assert.equal(runs, 1, "截断放行:仍在高峰也照跑,避免无限顺延");
128
+ store.close();
129
+ });
130
+
131
+ test("dreamPeakHours: 非法时段串按「未配置」处理(宁可不省,也不能误停)", async () => {
132
+ const { store, service } = dreamSetup();
133
+ const clock = fakeClock(at(10, 0));
134
+ let runs = 0;
135
+ let audits = 0;
136
+ const dream = createDreamScheduler({
137
+ onRun: async () => { runs++; return { ok: true }; },
138
+ thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
139
+ peakHours: "10:00-99:00", // 非法:解析失败必须回落成「关闭」
140
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
141
+ auditPeakSkip: () => { audits++; }
142
+ });
143
+ fill(service);
144
+ assert.equal(dream.maybeSchedule(service), true, "非法串 = 未配置 → 走原有触发路径");
145
+ assert.equal(audits, 0, "不写 skip 审计");
146
+ const t = clock.timers[0];
147
+ clock.setNow(t.at);
148
+ await t.fn();
149
+ assert.equal(runs, 1, "巩固照常进行");
150
+ store.close();
151
+ });
152
+
153
+ test("dreamPeakHours: 非高峰时段不受影响(行为与现状一致)", async () => {
154
+ const { store, service } = dreamSetup();
155
+ const clock = fakeClock(at(20, 0)); // 本地 20:00,在 09:00-18:00 之外
156
+ let runs = 0;
157
+ const dream = createDreamScheduler({
158
+ onRun: async () => { runs++; return { ok: true }; },
159
+ thresholdCount: 2, thresholdChars: 5000, delayMs: 5,
160
+ peakHours: "09:00-18:00",
161
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
162
+ auditPeakSkip: () => { throw new Error("非高峰不该写 skip 审计"); }
163
+ });
164
+ fill(service);
165
+ assert.equal(dream.maybeSchedule(service), true, "非高峰照常排程");
166
+ const t = clock.timers[0];
167
+ assert.equal(t.at, at(20, 0) + 5, "走的是 delayMs 的「马上跑」路径,不是顺延路径");
168
+ clock.setNow(t.at);
169
+ await t.fn();
170
+ assert.equal(runs, 1);
171
+ store.close();
172
+ });
173
+
174
+ test("dreamPeakHours: dispose 清掉顺延定时器,进程关闭后不再触发巩固", async () => {
175
+ const { store, service } = dreamSetup();
176
+ const clock = fakeClock(at(10, 0));
177
+ let runs = 0;
178
+ const dream = createDreamScheduler({
179
+ onRun: async () => { runs++; return { ok: true }; },
180
+ thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
181
+ peakHours: "09:00-18:00",
182
+ now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
183
+ auditPeakSkip: () => {}
184
+ });
185
+ fill(service);
186
+ dream.maybeSchedule(service);
187
+ const t = clock.timers[0];
188
+ await dream.dispose();
189
+ assert.equal(clock.timers.length, 0, "顺延定时器已清");
190
+ clock.setNow(t.at);
191
+ await t.fn(); // 模拟「定时器已经进了事件队列,清不掉」的那一帧
192
+ assert.equal(runs, 0, "disposed 守卫拦住补跑");
193
+ store.close();
194
+ });
@@ -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
@@ -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
+ });