dsh-verbatim 0.1.0 → 0.1.1

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 (74) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +1 -1
  3. package/README.en.md +24 -22
  4. package/README.md +24 -22
  5. package/bin/cli.mjs +2 -1
  6. package/bin/mcp-memory.mjs +130 -62
  7. package/client.js +8880 -8857
  8. package/cordis.patch.yml +1 -1
  9. package/docs/ARCHITECTURE.md +1 -1
  10. package/icon.svg +1 -1
  11. package/index.js +613 -2558
  12. package/locale/en.json +1 -1
  13. package/locale/zh.json +1 -1
  14. package/package.json +2 -3
  15. package/src/config/exclude-range.js +1 -1
  16. package/src/config/global-config.js +3 -1
  17. package/src/config/session-config.js +12 -10
  18. package/src/config/session-config.test.js +375 -375
  19. package/src/derive/fact.js +6 -6
  20. package/src/derive/gist.js +2 -1
  21. package/src/host/config-views.js +2 -0
  22. package/src/host/db-admin.js +78 -15
  23. package/src/host/panel-api.js +1109 -0
  24. package/src/host/panel-http.js +322 -0
  25. package/src/host/panel-search.js +236 -0
  26. package/src/host/panel-session-store.js +411 -0
  27. package/src/host/rebuild.js +11 -3
  28. package/src/host/scene-ops.js +75 -6
  29. package/src/host/session-sync.js +91 -0
  30. package/src/index.js +2 -1
  31. package/src/ingest/cli.js +8 -7
  32. package/src/ingest/export.js +28 -18
  33. package/src/ingest/package.json +1 -1
  34. package/src/ingest/reader.js +181 -7
  35. package/src/model/embedding.js +3 -2
  36. package/src/model/llm.js +3 -2
  37. package/src/model/rerank.js +7 -4
  38. package/src/pipeline.js +159 -763
  39. package/src/scene/link.js +4 -4
  40. package/src/scene/package.json +1 -1
  41. package/src/scene/split.js +11 -11
  42. package/src/search/_fixtures.js +1 -0
  43. package/src/search/bench.js +4 -2
  44. package/src/search/chunk.js +6 -4
  45. package/src/search/dedupe.js +8 -8
  46. package/src/search/indexer.js +889 -879
  47. package/src/search/note-rank.js +64 -12
  48. package/src/search/regression.test.js +2 -1
  49. package/src/search/rrf.js +11 -11
  50. package/src/search/session-shape.js +3 -1
  51. package/src/search/thread.js +8 -8
  52. package/src/search/vector-fallback.js +509 -0
  53. package/src/search/vector-hydrate.js +309 -0
  54. package/src/search/vector-math.js +125 -0
  55. package/src/search/vector-store.js +1065 -1598
  56. package/src/search/vector-store.test.js +30 -0
  57. package/src/search/window.js +8 -8
  58. package/src/store/db.js +803 -715
  59. package/src/store/repo-extras-meta.js +430 -0
  60. package/src/store/repo-extras-search.js +580 -0
  61. package/src/store/repo.js +1024 -2133
  62. package/src/store/repo.test.js +39 -1
  63. package/src/store/schema.sql +15 -1
  64. package/src/tools/format.js +594 -503
  65. package/src/tools/index.js +7 -15
  66. package/src/tools/memory-forget.js +43 -32
  67. package/src/tools/memory-remember.js +11 -7
  68. package/src/tools/memory-search.js +721 -2206
  69. package/src/tools/remember-forget.test.js +19 -0
  70. package/src/tools/scope.js +7 -6
  71. package/src/tools/search-render.js +502 -0
  72. package/src/tools/time-route.js +71 -66
  73. package/src/tools/vector-route.js +655 -0
  74. package/sessions/qqb-p-0000000000-00000000000000000000000000000000.json +0 -12
@@ -1,375 +1,375 @@
1
- // session-config.test.js —— 会话配置(D-014 / D-166 / D-169 / D-175)
2
- //
3
- // 覆盖四件事:
4
- // ① 默认值 = { record: false, tools: false, autoRecall: false, excludeRanges: [] }(D-166/D-230:默认关)
5
- // ② 配置目录分工:运行时读 <dbRoot>/sessions/<id>.json,测试用临时 dbRoot(D-175)
6
- // ③ 写盘原子、合并语义正确、未知字段不丢
7
- // ④ 非法配置(空区间 / from > to / 解析不了 / 类型错)报明确错误,不静默跳过
8
-
9
- import test from 'node:test';
10
- import assert from 'node:assert/strict';
11
- import fs from 'node:fs';
12
- import os from 'node:os';
13
- import path from 'node:path';
14
- import { fileURLToPath } from 'node:url';
15
-
16
- import { ConfigError } from './exclude-range.js';
17
- import {
18
- CONFIG_KEYS,
19
- DEFAULT_SESSION_CONFIG,
20
- RESERVED_KEYS,
21
- SWITCH_KEYS,
22
- defaultSessionConfig,
23
- enabledOf,
24
- ensureSessionConfig,
25
- loadSessionConfig,
26
- loadSessionExcludeRanges,
27
- resolveDbRoot,
28
- resolveDshHome,
29
- saveSessionConfig,
30
- sessionConfigExists,
31
- sessionConfigPath,
32
- sessionsDir,
33
- setSessionExcludeRanges,
34
- setSessionFlag,
35
- validateSessionConfig,
36
- } from './session-config.js';
37
-
38
- /** src/config 目录 */
39
- const HERE = path.dirname(fileURLToPath(import.meta.url));
40
- /** 项目根目录(里面有个只放 fixture 的 sessions/,D-175) */
41
- const PROJECT_ROOT = path.resolve(HERE, '..', '..');
42
- /** 真实会话 id,项目内 sessions/ 下有它的 fixture */
43
- const FIXTURE_SID = 'qqb-p-0000000000-00000000000000000000000000000000';
44
-
45
- /** 2026-11-08T00:00:00+08:00 / 2026-11-09T00:00:00+08:00 */
46
- const T_11_08 = 1_794_067_200_000;
47
- const T_11_09 = 1_794_153_600_000;
48
-
49
- /** 造一个临时 dbRoot,测试结束自动清理 */
50
- function tmpRoot(t) {
51
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wm-config-'));
52
- t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
53
- return dir;
54
- }
55
-
56
- // ─────────────────────────────────────────────────────────────
57
- // ① 默认值与 schema
58
- // ─────────────────────────────────────────────────────────────
59
-
60
- test('默认配置的完整 JSON:record/tools **默认关**、autoRecall 关、无排除区间(D-166/D-230)', () => {
61
- const cfg = defaultSessionConfig();
62
- assert.deepEqual(cfg, {
63
- // 2026-11-18 作者要求:装上是默认关,由人显式打开(修正 D-009 的"记录默认常开")
64
- record: false,
65
- tools: false,
66
- autoRecall: false,
67
- excludeRanges: [],
68
- });
69
- // 字段顺序也要稳定(写进文件后人是照着顺序读的)
70
- assert.deepEqual(Object.keys(cfg), ['record', 'tools', 'autoRecall', 'excludeRanges']);
71
- assert.deepEqual(DEFAULT_SESSION_CONFIG, cfg);
72
- });
73
-
74
- test('默认值每次都是新副本,调用方改不到常量', () => {
75
- const a = defaultSessionConfig();
76
- a.record = false;
77
- a.excludeRanges.push({ from: '2026-11-08', to: '2026-11-09' });
78
- assert.equal(defaultSessionConfig().record, false); // D-230:默认关
79
- assert.deepEqual(defaultSessionConfig().excludeRanges, []);
80
- assert.ok(Object.isFrozen(DEFAULT_SESSION_CONFIG));
81
- assert.ok(Object.isFrozen(DEFAULT_SESSION_CONFIG.excludeRanges));
82
- });
83
-
84
- test('autoRecall 在 schema 里、但不在面板开关里(D-166)', () => {
85
- assert.ok(CONFIG_KEYS.includes('autoRecall'), 'schema 必须保留这个字段');
86
- assert.ok(RESERVED_KEYS.includes('autoRecall'));
87
- assert.deepEqual([...SWITCH_KEYS], ['record', 'tools'], '面板只渲染两个开关');
88
- assert.equal(SWITCH_KEYS.includes('autoRecall'), false, 'autoRecall 不进面板(D-166)');
89
- // 但协议里仍要带出来,前端不用改协议
90
- assert.deepEqual(enabledOf(defaultSessionConfig()), { record: false, tools: false, autoRecall: false });
91
- });
92
-
93
- test('validateSessionConfig:缺字段补默认值,未知字段保留并告警', () => {
94
- const warnings = [];
95
- const cfg = validateSessionConfig(
96
- { record: false, myOwnField: 42 },
97
- { onWarn: (m) => warnings.push(m) },
98
- );
99
- assert.equal(cfg.record, false);
100
- assert.equal(cfg.tools, false, '缺的字段补默认值(D-230:默认关)');
101
- assert.equal(cfg.autoRecall, false);
102
- assert.deepEqual(cfg.excludeRanges, []);
103
- assert.equal(cfg.myOwnField, 42, '未知字段要保留,别把别人写的东西冲掉');
104
- assert.equal(warnings.length, 1);
105
- assert.match(warnings[0], /未知配置项 "myOwnField"/);
106
- });
107
-
108
- // ─────────────────────────────────────────────────────────────
109
- // ② 配置目录分工(D-175)
110
- // ─────────────────────────────────────────────────────────────
111
-
112
- test('resolveDbRoot:默认 $DSH_HOME/verbatim,也接受 dbRoot / dshHome / 字符串', () => {
113
- const old = process.env.DSH_HOME;
114
- try {
115
- delete process.env.DSH_HOME;
116
- assert.equal(resolveDbRoot(), path.join(resolveDshHome(), 'verbatim'));
117
- assert.ok(resolveDbRoot().endsWith(path.join('.dsh', 'verbatim')));
118
-
119
- process.env.DSH_HOME = path.join(os.tmpdir(), 'fake-dsh-home');
120
- assert.equal(
121
- resolveDbRoot(),
122
- path.join(path.resolve(process.env.DSH_HOME), 'verbatim'),
123
- '环境变量 DSH_HOME 优先',
124
- );
125
- assert.equal(resolveDbRoot({ dshHome: 'C:/x/y' }), path.resolve('C:/x/y', 'verbatim'));
126
- assert.equal(resolveDbRoot({ dbRoot: 'C:/x/wm' }), path.resolve('C:/x/wm'));
127
- assert.equal(resolveDbRoot('C:/x/wm'), path.resolve('C:/x/wm'), '老签名:第二参直接给 dbRoot');
128
- } finally {
129
- if (old === undefined) delete process.env.DSH_HOME;
130
- else process.env.DSH_HOME = old;
131
- }
132
- });
133
-
134
- test('sessionConfigPath:<dbRoot>/sessions/<session-id>.json', () => {
135
- const root = path.join(os.tmpdir(), 'wm-root');
136
- assert.equal(
137
- sessionConfigPath('abc', { dbRoot: root }),
138
- path.join(path.resolve(root), 'sessions', 'abc.json'),
139
- );
140
- assert.equal(sessionsDir({ dbRoot: root }), path.join(path.resolve(root), 'sessions'));
141
- // 老签名(第二参直接给 dbRoot 字符串)也要能用
142
- assert.equal(sessionConfigPath('abc', root), path.join(path.resolve(root), 'sessions', 'abc.json'));
143
- });
144
-
145
- test('sessionId 校验:挡住路径穿越与 Windows 非法字符', () => {
146
- for (const bad of ['../etc', 'a/b', 'a\\b', 'a:b', 'a*b', '.hidden', '', ' ', null, 42]) {
147
- assert.throws(
148
- () => sessionConfigPath(bad, { dbRoot: os.tmpdir() }),
149
- (e) => {
150
- assert.ok(e instanceof ConfigError);
151
- assert.equal(e.path, 'sessionId');
152
- return true;
153
- },
154
- `应当拒收 sessionId=${JSON.stringify(bad)}`,
155
- );
156
- }
157
- assert.equal(sessionConfigPath(' ok-id ', { dbRoot: os.tmpdir() }), path.join(
158
- os.tmpdir(), 'sessions', 'ok-id.json',
159
- ));
160
- });
161
-
162
- test('loadSessionConfig:文件不存在 → 默认值,且不落盘', (t) => {
163
- const root = tmpRoot(t);
164
- const cfg = loadSessionConfig('missing', { dbRoot: root });
165
- assert.deepEqual(cfg, defaultSessionConfig());
166
- assert.equal(sessionConfigExists('missing', { dbRoot: root }), false, 'load 不应该顺手建文件');
167
- assert.equal(fs.existsSync(sessionsDir({ dbRoot: root })), false);
168
- });
169
-
170
- test('loadSessionConfig:读项目内 fixture(D-175:项目 sessions/ 只放 fixture)', () => {
171
- // 运行时读 $DSH_HOME/verbatim/sessions/,测试里把 dbRoot 指到项目根,读的就是 fixture
172
- const cfg = loadSessionConfig(FIXTURE_SID, { dbRoot: PROJECT_ROOT });
173
- assert.equal(cfg.record, false);
174
- assert.equal(cfg.tools, false);
175
- assert.equal(cfg.autoRecall, false, 'fixture 里没写也要补成默认 false');
176
- assert.equal(cfg.excludeRanges.length, 1);
177
- assert.equal(cfg.excludeRanges[0].from, '2026-11-08T00:00:00+08:00');
178
- assert.equal(cfg.excludeRanges[0].to, '2026-11-09T00:00:00+08:00');
179
- assert.equal(cfg.excludeRanges[0].reason, '示例:这段不想记');
180
- // fixture 里不再有多余的 sessionId —— 配置文件名已经是 session id,
181
- // 内容里再存一次是冗余,且会让每次导入刷一条"未知配置项"告警(T8 集成时发现)。
182
- // 未知字段的保留 + 告警行为由下面的 validateSessionConfig 用例单独覆盖。
183
- });
184
-
185
- test('loadSessionExcludeRanges:fixture 的区间归一化成毫秒,边界精确', async () => {
186
- const ranges = loadSessionExcludeRanges(FIXTURE_SID, { dbRoot: PROJECT_ROOT });
187
- assert.equal(ranges.length, 1);
188
- assert.equal(ranges[0].from, T_11_08);
189
- assert.equal(ranges[0].to, T_11_09);
190
- assert.equal(ranges[0].reason, '示例:这段不想记');
191
-
192
- const { isExcluded } = await import('./exclude-range.js');
193
- assert.equal(isExcluded(Date.parse('2026-11-08T23:59:59.999+08:00'), ranges), true);
194
- assert.equal(isExcluded(Date.parse('2026-11-09T00:00:00.000+08:00'), ranges), false);
195
- assert.equal(isExcluded(Date.parse('2026-11-09T08:00:00+08:00'), ranges), false, '11-09 全天正常');
196
- });
197
-
198
- // ─────────────────────────────────────────────────────────────
199
- // ③ 写盘:合并、原子、稳定
200
- // ─────────────────────────────────────────────────────────────
201
-
202
- test('saveSessionConfig:新建文件,JSON 文本稳定且人能读懂', (t) => {
203
- const root = tmpRoot(t);
204
- const cfg = saveSessionConfig('s1', {
205
- excludeRanges: [
206
- { from: '2026-11-08T00:00:00+08:00', to: '2026-11-09T00:00:00+08:00', reason: '调试期' },
207
- ],
208
- }, { dbRoot: root });
209
-
210
- assert.deepEqual(cfg, {
211
- record: false,
212
- tools: false,
213
- autoRecall: false,
214
- excludeRanges: [
215
- { from: '2026-11-08T00:00:00+08:00', to: '2026-11-09T00:00:00+08:00', reason: '调试期' },
216
- ],
217
- });
218
-
219
- const file = sessionConfigPath('s1', { dbRoot: root });
220
- const text = fs.readFileSync(file, 'utf8');
221
- assert.ok(text.endsWith('\n'), '文件末尾要有换行');
222
- // 配置里保留人写的字符串日期,不换算成 epoch(否则人看不懂,D-175 的初衷)
223
- assert.match(text, /"2026-11-08T00:00:00\+08:00"/);
224
- assert.equal(JSON.parse(text).excludeRanges[0].reason, '调试期');
225
- // 不留临时文件
226
- assert.deepEqual(
227
- fs.readdirSync(path.dirname(file)).filter((f) => f.endsWith('.tmp')),
228
- [],
229
- );
230
- });
231
-
232
- test('saveSessionConfig:patch 合并,不丢其它字段(含未知字段)', (t) => {
233
- const root = tmpRoot(t);
234
- saveSessionConfig('s2', {
235
- myOwnField: { nested: true },
236
- excludeRanges: [{ from: '2026-11-08', to: '2026-11-09', reason: '调试期' }],
237
- }, { dbRoot: root });
238
-
239
- const after = saveSessionConfig('s2', { record: false }, { dbRoot: root });
240
- assert.equal(after.record, false);
241
- assert.equal(after.tools, false, '没动的字段保持原值(默认已改成关)');
242
- assert.equal(after.excludeRanges.length, 1, '排除区间不能被一次开关操作冲掉');
243
- assert.deepEqual(after.myOwnField, { nested: true }, '未知字段要保留');
244
-
245
- // 写盘的也是合并后的结果
246
- const reload = loadSessionConfig('s2', { dbRoot: root });
247
- assert.deepEqual(reload, after);
248
- });
249
-
250
- test('saveSessionConfig:patch 非法时抛错,且不写盘', (t) => {
251
- const root = tmpRoot(t);
252
- saveSessionConfig('s3', { record: true }, { dbRoot: root });
253
- const file = sessionConfigPath('s3', { dbRoot: root });
254
- const before = fs.readFileSync(file, 'utf8');
255
-
256
- assert.throws(
257
- () => saveSessionConfig('s3', { excludeRanges: [{ from: '2026-11-09', to: '2026-11-08' }] }, { dbRoot: root }),
258
- ConfigError,
259
- );
260
- assert.equal(fs.readFileSync(file, 'utf8'), before, '校验失败不能留下半截配置');
261
-
262
- assert.throws(() => saveSessionConfig('s3', { record: 'yes' }, { dbRoot: root }), ConfigError);
263
- assert.throws(() => saveSessionConfig('s3', 'not-an-object', { dbRoot: root }), ConfigError);
264
- });
265
-
266
- test('setSessionFlag:面板 panel.set 的落点(record / tools / autoRecall)', (t) => {
267
- const root = tmpRoot(t);
268
- assert.equal(setSessionFlag('s4', 'record', false, { dbRoot: root }).record, false);
269
- assert.equal(setSessionFlag('s4', 'tools', false, { dbRoot: root }).tools, false);
270
- // autoRecall 面板不渲染,但 RPC 契约里保留(D-166)
271
- assert.equal(setSessionFlag('s4', 'autoRecall', true, { dbRoot: root }).autoRecall, true);
272
-
273
- assert.throws(
274
- () => setSessionFlag('s4', 'whatever', true, { dbRoot: root }),
275
- (e) => {
276
- assert.ok(e instanceof ConfigError);
277
- assert.match(e.message, /开关名只能是 "record" \/ "tools" \/ "autoRecall"/);
278
- return true;
279
- },
280
- );
281
- assert.throws(() => setSessionFlag('s4', 'record', 'yes', { dbRoot: root }), /必须是 true\/false/);
282
- assert.throws(() => setSessionFlag('s4', 'excludeRanges', [], { dbRoot: root }), ConfigError);
283
- });
284
-
285
- test('setSessionExcludeRanges + ensureSessionConfig', (t) => {
286
- const root = tmpRoot(t);
287
- const cfg = setSessionExcludeRanges('s5', [{ from: '2026-11-08', to: '2026-11-09', reason: '调试期' }], { dbRoot: root });
288
- assert.equal(cfg.excludeRanges.length, 1);
289
- assert.equal(sessionConfigExists('s5', { dbRoot: root }), true);
290
-
291
- // ensure:缺文件时落一份默认配置
292
- assert.equal(sessionConfigExists('s6', { dbRoot: root }), false);
293
- const made = ensureSessionConfig('s6', { dbRoot: root });
294
- assert.deepEqual(made, defaultSessionConfig());
295
- assert.equal(sessionConfigExists('s6', { dbRoot: root }), true);
296
- // 已有文件时不覆盖
297
- saveSessionConfig('s6', { tools: false }, { dbRoot: root });
298
- assert.equal(ensureSessionConfig('s6', { dbRoot: root }).tools, false);
299
- });
300
-
301
- // ─────────────────────────────────────────────────────────────
302
- // ④ 非法配置:必须报明确错误
303
- // ─────────────────────────────────────────────────────────────
304
-
305
- test('非法配置:JSON 语法错 / 空文件 / 顶层不是对象', (t) => {
306
- const root = tmpRoot(t);
307
- fs.mkdirSync(sessionsDir({ dbRoot: root }), { recursive: true });
308
-
309
- const badJson = path.join(sessionsDir({ dbRoot: root }), 'bad-json.json');
310
- fs.writeFileSync(badJson, '{ "record": true, ', 'utf8');
311
- assert.throws(
312
- () => loadSessionConfig('bad-json', { dbRoot: root }),
313
- (e) => {
314
- assert.ok(e instanceof ConfigError);
315
- assert.equal(e.code, 'CONFIG_INVALID_JSON');
316
- assert.equal(e.path, badJson, '错误要带文件路径');
317
- assert.match(e.message, /不是合法 JSON/);
318
- return true;
319
- },
320
- );
321
-
322
- const empty = path.join(sessionsDir({ dbRoot: root }), 'empty.json');
323
- fs.writeFileSync(empty, ' \n', 'utf8');
324
- assert.throws(
325
- () => loadSessionConfig('empty', { dbRoot: root }),
326
- (e) => {
327
- assert.ok(e instanceof ConfigError);
328
- assert.match(e.message, /空文件/);
329
- return true;
330
- },
331
- '空文件不能静默当成默认值(会悄悄清掉排除区间)',
332
- );
333
-
334
- const arr = path.join(sessionsDir({ dbRoot: root }), 'arr.json');
335
- fs.writeFileSync(arr, '[1,2,3]', 'utf8');
336
- assert.throws(() => loadSessionConfig('arr', { dbRoot: root }), /配置必须是 JSON 对象/);
337
- });
338
-
339
- test('非法配置:空区间、from > to、解析不了、类型不对 —— 逐条报出字段路径', (t) => {
340
- const root = tmpRoot(t);
341
- fs.mkdirSync(sessionsDir({ dbRoot: root }), { recursive: true });
342
-
343
- const cases = [
344
- { name: 'from-gt-to', body: { excludeRanges: [{ from: '2026-11-09', to: '2026-11-08' }] }, msg: /to 早于 from/ },
345
- { name: 'empty-range', body: { excludeRanges: [{ from: '2026-11-08', to: '2026-11-08' }] }, msg: /from 与 to 相同/ },
346
- { name: 'unparsable', body: { excludeRanges: [{ from: '11月8日', to: '2026-11-09' }] }, msg: /from:无法解析为时刻/ },
347
- { name: 'not-array', body: { excludeRanges: 'nope' }, msg: /excludeRanges:必须是数组/ },
348
- { name: 'record-type', body: { record: 1 }, msg: /record:必须是 true\/false/ },
349
- { name: 'tools-type', body: { tools: 'on' }, msg: /tools:必须是 true\/false/ },
350
- { name: 'autoRecall-type', body: { autoRecall: 'false' }, msg: /autoRecall:必须是 true\/false/ },
351
- { name: 'missing-to', body: { excludeRanges: [{ from: '2026-11-08' }] }, msg: /缺少 to/ },
352
- ];
353
-
354
- for (const c of cases) {
355
- const file = path.join(sessionsDir({ dbRoot: root }), `${c.name}.json`);
356
- fs.writeFileSync(file, JSON.stringify(c.body), 'utf8');
357
- assert.throws(
358
- () => loadSessionConfig(c.name, { dbRoot: root }),
359
- (e) => {
360
- assert.ok(e instanceof ConfigError, `${c.name} 应抛 ConfigError`);
361
- assert.match(e.message, c.msg, `${c.name} 的错误信息应指明原因`);
362
- assert.ok(String(e.path).includes(c.name), `${c.name} 的错误应带字段路径,实际 ${e.path}`);
363
- return true;
364
- },
365
- `${c.name} 必须报错,不能静默跳过`,
366
- );
367
- }
368
- });
369
-
370
- test('非法配置:格式(schema)正确时,坏区间一条都不能静默放过', () => {
371
- // 这一条专门锁死「静默跳过」这个失败模式
372
- const bad = [{ from: '2026-11-08', to: '2026-11-08' }];
373
- assert.throws(() => validateSessionConfig({ excludeRanges: bad }), ConfigError);
374
- assert.throws(() => validateSessionConfig({ excludeRanges: bad }, { path: 'sessions/x.json' }), /sessions\/x\.json\.excludeRanges\[0\]/);
375
- });
1
+ // session-config.test.js —— 会话配置(D-014 / D-166 / D-169 / D-175)
2
+ //
3
+ // 覆盖四件事:
4
+ // ① 默认值 = { record: false, tools: false, autoRecall: false, excludeRanges: [] }(D-166/D-230:默认关)
5
+ // ② 配置目录分工:运行时读 <dbRoot>/sessions/<id>.json,测试用临时 dbRoot(D-175)
6
+ // ③ 写盘原子、合并语义正确、未知字段不丢
7
+ // ④ 非法配置(空区间 / from > to / 解析不了 / 类型错)报明确错误,不静默跳过
8
+
9
+ import test from 'node:test';
10
+ import assert from 'node:assert/strict';
11
+ import fs from 'node:fs';
12
+ import os from 'node:os';
13
+ import path from 'node:path';
14
+ import { fileURLToPath } from 'node:url';
15
+
16
+ import { ConfigError } from './exclude-range.js';
17
+ import {
18
+ CONFIG_KEYS,
19
+ DEFAULT_SESSION_CONFIG,
20
+ RESERVED_KEYS,
21
+ SWITCH_KEYS,
22
+ defaultSessionConfig,
23
+ enabledOf,
24
+ ensureSessionConfig,
25
+ loadSessionConfig,
26
+ loadSessionExcludeRanges,
27
+ resolveDbRoot,
28
+ resolveDshHome,
29
+ saveSessionConfig,
30
+ sessionConfigExists,
31
+ sessionConfigPath,
32
+ sessionsDir,
33
+ setSessionExcludeRanges,
34
+ setSessionFlag,
35
+ validateSessionConfig,
36
+ } from './session-config.js';
37
+
38
+ /** src/config 目录 */
39
+ const HERE = path.dirname(fileURLToPath(import.meta.url));
40
+ /** 项目根目录(里面有个只放 fixture 的 sessions/,D-175) */
41
+ const PROJECT_ROOT = path.resolve(HERE, '..', '..');
42
+ /** 真实会话 id,项目内 sessions/ 下有它的 fixture */
43
+ const FIXTURE_SID = 'qqb-p-0000000000-00000000000000000000000000000000';
44
+
45
+ /** 2026-11-08T00:00:00+08:00 / 2026-11-09T00:00:00+08:00 */
46
+ const T_11_08 = 1_794_067_200_000;
47
+ const T_11_09 = 1_794_153_600_000;
48
+
49
+ /** 造一个临时 dbRoot,测试结束自动清理 */
50
+ function tmpRoot(t) {
51
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wm-config-'));
52
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
53
+ return dir;
54
+ }
55
+
56
+ // ─────────────────────────────────────────────────────────────
57
+ // ① 默认值与 schema
58
+ // ─────────────────────────────────────────────────────────────
59
+
60
+ test('默认配置的完整 JSON:record/tools **默认关**、autoRecall 关、无排除区间(D-166/D-230)', () => {
61
+ const cfg = defaultSessionConfig();
62
+ assert.deepEqual(cfg, {
63
+ // 2026-11-18 作者要求:装上是默认关,由人显式打开(修正 D-009 的"记录默认常开")
64
+ record: false,
65
+ tools: false,
66
+ autoRecall: false,
67
+ excludeRanges: [],
68
+ });
69
+ // 字段顺序也要稳定(写进文件后人是照着顺序读的)
70
+ assert.deepEqual(Object.keys(cfg), ['record', 'tools', 'autoRecall', 'excludeRanges']);
71
+ assert.deepEqual(DEFAULT_SESSION_CONFIG, cfg);
72
+ });
73
+
74
+ test('默认值每次都是新副本,调用方改不到常量', () => {
75
+ const a = defaultSessionConfig();
76
+ a.record = false;
77
+ a.excludeRanges.push({ from: '2026-11-08', to: '2026-11-09' });
78
+ assert.equal(defaultSessionConfig().record, false); // D-230:默认关
79
+ assert.deepEqual(defaultSessionConfig().excludeRanges, []);
80
+ assert.ok(Object.isFrozen(DEFAULT_SESSION_CONFIG));
81
+ assert.ok(Object.isFrozen(DEFAULT_SESSION_CONFIG.excludeRanges));
82
+ });
83
+
84
+ test('autoRecall 在 schema 里、但不在面板开关里(D-166)', () => {
85
+ assert.ok(CONFIG_KEYS.includes('autoRecall'), 'schema 必须保留这个字段');
86
+ assert.ok(RESERVED_KEYS.includes('autoRecall'));
87
+ assert.deepEqual([...SWITCH_KEYS], ['record', 'tools'], '面板只渲染两个开关');
88
+ assert.equal(SWITCH_KEYS.includes('autoRecall'), false, 'autoRecall 不进面板(D-166)');
89
+ // 但协议里仍要带出来,前端不用改协议
90
+ assert.deepEqual(enabledOf(defaultSessionConfig()), { record: false, tools: false, autoRecall: false });
91
+ });
92
+
93
+ test('validateSessionConfig:缺字段补默认值,未知字段保留并告警', () => {
94
+ const warnings = [];
95
+ const cfg = validateSessionConfig(
96
+ { record: false, myOwnField: 42 },
97
+ { onWarn: (m) => warnings.push(m) },
98
+ );
99
+ assert.equal(cfg.record, false);
100
+ assert.equal(cfg.tools, false, '缺的字段补默认值(D-230:默认关)');
101
+ assert.equal(cfg.autoRecall, false);
102
+ assert.deepEqual(cfg.excludeRanges, []);
103
+ assert.equal(cfg.myOwnField, 42, '未知字段要保留,别把别人写的东西冲掉');
104
+ assert.equal(warnings.length, 1);
105
+ assert.match(warnings[0], /未知配置项 "myOwnField"/);
106
+ });
107
+
108
+ // ─────────────────────────────────────────────────────────────
109
+ // ② 配置目录分工(D-175)
110
+ // ─────────────────────────────────────────────────────────────
111
+
112
+ test('resolveDbRoot:默认 $DSH_HOME/verbatim,也接受 dbRoot / dshHome / 字符串', () => {
113
+ const old = process.env.DSH_HOME;
114
+ try {
115
+ delete process.env.DSH_HOME;
116
+ assert.equal(resolveDbRoot(), path.join(resolveDshHome(), 'verbatim'));
117
+ assert.ok(resolveDbRoot().endsWith(path.join('.dsh', 'verbatim')));
118
+
119
+ process.env.DSH_HOME = path.join(os.tmpdir(), 'fake-dsh-home');
120
+ assert.equal(
121
+ resolveDbRoot(),
122
+ path.join(path.resolve(process.env.DSH_HOME), 'verbatim'),
123
+ '环境变量 DSH_HOME 优先',
124
+ );
125
+ assert.equal(resolveDbRoot({ dshHome: 'C:/x/y' }), path.resolve('C:/x/y', 'verbatim'));
126
+ assert.equal(resolveDbRoot({ dbRoot: 'C:/x/wm' }), path.resolve('C:/x/wm'));
127
+ assert.equal(resolveDbRoot('C:/x/wm'), path.resolve('C:/x/wm'), '老签名:第二参直接给 dbRoot');
128
+ } finally {
129
+ if (old === undefined) delete process.env.DSH_HOME;
130
+ else process.env.DSH_HOME = old;
131
+ }
132
+ });
133
+
134
+ test('sessionConfigPath:<dbRoot>/sessions/<session-id>.json', () => {
135
+ const root = path.join(os.tmpdir(), 'wm-root');
136
+ assert.equal(
137
+ sessionConfigPath('abc', { dbRoot: root }),
138
+ path.join(path.resolve(root), 'sessions', 'abc.json'),
139
+ );
140
+ assert.equal(sessionsDir({ dbRoot: root }), path.join(path.resolve(root), 'sessions'));
141
+ // 老签名(第二参直接给 dbRoot 字符串)也要能用
142
+ assert.equal(sessionConfigPath('abc', root), path.join(path.resolve(root), 'sessions', 'abc.json'));
143
+ });
144
+
145
+ test('sessionId 校验:挡住路径穿越与 Windows 非法字符', () => {
146
+ for (const bad of ['../etc', 'a/b', 'a\\b', 'a:b', 'a*b', '.hidden', '', ' ', null, 42]) {
147
+ assert.throws(
148
+ () => sessionConfigPath(bad, { dbRoot: os.tmpdir() }),
149
+ (e) => {
150
+ assert.ok(e instanceof ConfigError);
151
+ assert.equal(e.path, 'sessionId');
152
+ return true;
153
+ },
154
+ `应当拒收 sessionId=${JSON.stringify(bad)}`,
155
+ );
156
+ }
157
+ assert.equal(sessionConfigPath(' ok-id ', { dbRoot: os.tmpdir() }), path.join(
158
+ os.tmpdir(), 'sessions', 'ok-id.json',
159
+ ));
160
+ });
161
+
162
+ test('loadSessionConfig:文件不存在 → 默认值,且不落盘', (t) => {
163
+ const root = tmpRoot(t);
164
+ const cfg = loadSessionConfig('missing', { dbRoot: root });
165
+ assert.deepEqual(cfg, defaultSessionConfig());
166
+ assert.equal(sessionConfigExists('missing', { dbRoot: root }), false, 'load 不应该顺手建文件');
167
+ assert.equal(fs.existsSync(sessionsDir({ dbRoot: root })), false);
168
+ });
169
+
170
+ test('loadSessionConfig:读项目内 fixture(D-175:项目 sessions/ 只放 fixture)', () => {
171
+ // 运行时读 $DSH_HOME/verbatim/sessions/,测试里把 dbRoot 指到项目根,读的就是 fixture
172
+ const cfg = loadSessionConfig(FIXTURE_SID, { dbRoot: PROJECT_ROOT });
173
+ assert.equal(cfg.record, false);
174
+ assert.equal(cfg.tools, false);
175
+ assert.equal(cfg.autoRecall, false, 'fixture 里没写也要补成默认 false');
176
+ assert.equal(cfg.excludeRanges.length, 1);
177
+ assert.equal(cfg.excludeRanges[0].from, '2026-11-08T00:00:00+08:00');
178
+ assert.equal(cfg.excludeRanges[0].to, '2026-11-09T00:00:00+08:00');
179
+ assert.equal(cfg.excludeRanges[0].reason, '示例:这段不想记');
180
+ // fixture 里不再有多余的 sessionId —— 配置文件名已经是 session id,
181
+ // 内容里再存一次是冗余,且会让每次导入刷一条"未知配置项"告警(T8 集成时发现)。
182
+ // 未知字段的保留 + 告警行为由下面的 validateSessionConfig 用例单独覆盖。
183
+ });
184
+
185
+ test('loadSessionExcludeRanges:fixture 的区间归一化成毫秒,边界精确', async () => {
186
+ const ranges = loadSessionExcludeRanges(FIXTURE_SID, { dbRoot: PROJECT_ROOT });
187
+ assert.equal(ranges.length, 1);
188
+ assert.equal(ranges[0].from, T_11_08);
189
+ assert.equal(ranges[0].to, T_11_09);
190
+ assert.equal(ranges[0].reason, '示例:这段不想记');
191
+
192
+ const { isExcluded } = await import('./exclude-range.js');
193
+ assert.equal(isExcluded(Date.parse('2026-11-08T23:59:59.999+08:00'), ranges), true);
194
+ assert.equal(isExcluded(Date.parse('2026-11-09T00:00:00.000+08:00'), ranges), false);
195
+ assert.equal(isExcluded(Date.parse('2026-11-09T08:00:00+08:00'), ranges), false, '11-09 全天正常');
196
+ });
197
+
198
+ // ─────────────────────────────────────────────────────────────
199
+ // ③ 写盘:合并、原子、稳定
200
+ // ─────────────────────────────────────────────────────────────
201
+
202
+ test('saveSessionConfig:新建文件,JSON 文本稳定且人能读懂', (t) => {
203
+ const root = tmpRoot(t);
204
+ const cfg = saveSessionConfig('s1', {
205
+ excludeRanges: [
206
+ { from: '2026-11-08T00:00:00+08:00', to: '2026-11-09T00:00:00+08:00', reason: '调试期' },
207
+ ],
208
+ }, { dbRoot: root });
209
+
210
+ assert.deepEqual(cfg, {
211
+ record: false,
212
+ tools: false,
213
+ autoRecall: false,
214
+ excludeRanges: [
215
+ { from: '2026-11-08T00:00:00+08:00', to: '2026-11-09T00:00:00+08:00', reason: '调试期' },
216
+ ],
217
+ });
218
+
219
+ const file = sessionConfigPath('s1', { dbRoot: root });
220
+ const text = fs.readFileSync(file, 'utf8');
221
+ assert.ok(text.endsWith('\n'), '文件末尾要有换行');
222
+ // 配置里保留人写的字符串日期,不换算成 epoch(否则人看不懂,D-175 的初衷)
223
+ assert.match(text, /"2026-11-08T00:00:00\+08:00"/);
224
+ assert.equal(JSON.parse(text).excludeRanges[0].reason, '调试期');
225
+ // 不留临时文件
226
+ assert.deepEqual(
227
+ fs.readdirSync(path.dirname(file)).filter((f) => f.endsWith('.tmp')),
228
+ [],
229
+ );
230
+ });
231
+
232
+ test('saveSessionConfig:patch 合并,不丢其它字段(含未知字段)', (t) => {
233
+ const root = tmpRoot(t);
234
+ saveSessionConfig('s2', {
235
+ myOwnField: { nested: true },
236
+ excludeRanges: [{ from: '2026-11-08', to: '2026-11-09', reason: '调试期' }],
237
+ }, { dbRoot: root });
238
+
239
+ const after = saveSessionConfig('s2', { record: false }, { dbRoot: root });
240
+ assert.equal(after.record, false);
241
+ assert.equal(after.tools, false, '没动的字段保持原值(默认已改成关)');
242
+ assert.equal(after.excludeRanges.length, 1, '排除区间不能被一次开关操作冲掉');
243
+ assert.deepEqual(after.myOwnField, { nested: true }, '未知字段要保留');
244
+
245
+ // 写盘的也是合并后的结果
246
+ const reload = loadSessionConfig('s2', { dbRoot: root });
247
+ assert.deepEqual(reload, after);
248
+ });
249
+
250
+ test('saveSessionConfig:patch 非法时抛错,且不写盘', (t) => {
251
+ const root = tmpRoot(t);
252
+ saveSessionConfig('s3', { record: true }, { dbRoot: root });
253
+ const file = sessionConfigPath('s3', { dbRoot: root });
254
+ const before = fs.readFileSync(file, 'utf8');
255
+
256
+ assert.throws(
257
+ () => saveSessionConfig('s3', { excludeRanges: [{ from: '2026-11-09', to: '2026-11-08' }] }, { dbRoot: root }),
258
+ ConfigError,
259
+ );
260
+ assert.equal(fs.readFileSync(file, 'utf8'), before, '校验失败不能留下半截配置');
261
+
262
+ assert.throws(() => saveSessionConfig('s3', { record: 'yes' }, { dbRoot: root }), ConfigError);
263
+ assert.throws(() => saveSessionConfig('s3', 'not-an-object', { dbRoot: root }), ConfigError);
264
+ });
265
+
266
+ test('setSessionFlag:面板 panel.set 的落点(record / tools / autoRecall)', (t) => {
267
+ const root = tmpRoot(t);
268
+ assert.equal(setSessionFlag('s4', 'record', false, { dbRoot: root }).record, false);
269
+ assert.equal(setSessionFlag('s4', 'tools', false, { dbRoot: root }).tools, false);
270
+ // autoRecall 面板不渲染,但 RPC 契约里保留(D-166)
271
+ assert.equal(setSessionFlag('s4', 'autoRecall', true, { dbRoot: root }).autoRecall, true);
272
+
273
+ assert.throws(
274
+ () => setSessionFlag('s4', 'whatever', true, { dbRoot: root }),
275
+ (e) => {
276
+ assert.ok(e instanceof ConfigError);
277
+ assert.match(e.message, /开关名只能是 "record" \/ "tools" \/ "autoRecall"/);
278
+ return true;
279
+ },
280
+ );
281
+ assert.throws(() => setSessionFlag('s4', 'record', 'yes', { dbRoot: root }), /必须是 true\/false/);
282
+ assert.throws(() => setSessionFlag('s4', 'excludeRanges', [], { dbRoot: root }), ConfigError);
283
+ });
284
+
285
+ test('setSessionExcludeRanges + ensureSessionConfig', (t) => {
286
+ const root = tmpRoot(t);
287
+ const cfg = setSessionExcludeRanges('s5', [{ from: '2026-11-08', to: '2026-11-09', reason: '调试期' }], { dbRoot: root });
288
+ assert.equal(cfg.excludeRanges.length, 1);
289
+ assert.equal(sessionConfigExists('s5', { dbRoot: root }), true);
290
+
291
+ // ensure:缺文件时落一份默认配置
292
+ assert.equal(sessionConfigExists('s6', { dbRoot: root }), false);
293
+ const made = ensureSessionConfig('s6', { dbRoot: root });
294
+ assert.deepEqual(made, defaultSessionConfig());
295
+ assert.equal(sessionConfigExists('s6', { dbRoot: root }), true);
296
+ // 已有文件时不覆盖
297
+ saveSessionConfig('s6', { tools: false }, { dbRoot: root });
298
+ assert.equal(ensureSessionConfig('s6', { dbRoot: root }).tools, false);
299
+ });
300
+
301
+ // ─────────────────────────────────────────────────────────────
302
+ // ④ 非法配置:必须报明确错误
303
+ // ─────────────────────────────────────────────────────────────
304
+
305
+ test('非法配置:JSON 语法错 / 空文件 / 顶层不是对象', (t) => {
306
+ const root = tmpRoot(t);
307
+ fs.mkdirSync(sessionsDir({ dbRoot: root }), { recursive: true });
308
+
309
+ const badJson = path.join(sessionsDir({ dbRoot: root }), 'bad-json.json');
310
+ fs.writeFileSync(badJson, '{ "record": true, ', 'utf8');
311
+ assert.throws(
312
+ () => loadSessionConfig('bad-json', { dbRoot: root }),
313
+ (e) => {
314
+ assert.ok(e instanceof ConfigError);
315
+ assert.equal(e.code, 'CONFIG_INVALID_JSON');
316
+ assert.equal(e.path, badJson, '错误要带文件路径');
317
+ assert.match(e.message, /不是合法 JSON/);
318
+ return true;
319
+ },
320
+ );
321
+
322
+ const empty = path.join(sessionsDir({ dbRoot: root }), 'empty.json');
323
+ fs.writeFileSync(empty, ' \n', 'utf8');
324
+ assert.throws(
325
+ () => loadSessionConfig('empty', { dbRoot: root }),
326
+ (e) => {
327
+ assert.ok(e instanceof ConfigError);
328
+ assert.match(e.message, /空文件/);
329
+ return true;
330
+ },
331
+ '空文件不能静默当成默认值(会悄悄清掉排除区间)',
332
+ );
333
+
334
+ const arr = path.join(sessionsDir({ dbRoot: root }), 'arr.json');
335
+ fs.writeFileSync(arr, '[1,2,3]', 'utf8');
336
+ assert.throws(() => loadSessionConfig('arr', { dbRoot: root }), /配置必须是 JSON 对象/);
337
+ });
338
+
339
+ test('非法配置:空区间、from > to、解析不了、类型不对 —— 逐条报出字段路径', (t) => {
340
+ const root = tmpRoot(t);
341
+ fs.mkdirSync(sessionsDir({ dbRoot: root }), { recursive: true });
342
+
343
+ const cases = [
344
+ { name: 'from-gt-to', body: { excludeRanges: [{ from: '2026-11-09', to: '2026-11-08' }] }, msg: /to 早于 from/ },
345
+ { name: 'empty-range', body: { excludeRanges: [{ from: '2026-11-08', to: '2026-11-08' }] }, msg: /from 与 to 相同/ },
346
+ { name: 'unparsable', body: { excludeRanges: [{ from: '11月8日', to: '2026-11-09' }] }, msg: /from:无法解析为时刻/ },
347
+ { name: 'not-array', body: { excludeRanges: 'nope' }, msg: /excludeRanges:必须是数组/ },
348
+ { name: 'record-type', body: { record: 1 }, msg: /record:必须是 true\/false/ },
349
+ { name: 'tools-type', body: { tools: 'on' }, msg: /tools:必须是 true\/false/ },
350
+ { name: 'autoRecall-type', body: { autoRecall: 'false' }, msg: /autoRecall:必须是 true\/false/ },
351
+ { name: 'missing-to', body: { excludeRanges: [{ from: '2026-11-08' }] }, msg: /缺少 to/ },
352
+ ];
353
+
354
+ for (const c of cases) {
355
+ const file = path.join(sessionsDir({ dbRoot: root }), `${c.name}.json`);
356
+ fs.writeFileSync(file, JSON.stringify(c.body), 'utf8');
357
+ assert.throws(
358
+ () => loadSessionConfig(c.name, { dbRoot: root }),
359
+ (e) => {
360
+ assert.ok(e instanceof ConfigError, `${c.name} 应抛 ConfigError`);
361
+ assert.match(e.message, c.msg, `${c.name} 的错误信息应指明原因`);
362
+ assert.ok(String(e.path).includes(c.name), `${c.name} 的错误应带字段路径,实际 ${e.path}`);
363
+ return true;
364
+ },
365
+ `${c.name} 必须报错,不能静默跳过`,
366
+ );
367
+ }
368
+ });
369
+
370
+ test('非法配置:格式(schema)正确时,坏区间一条都不能静默放过', () => {
371
+ // 这一条专门锁死「静默跳过」这个失败模式
372
+ const bad = [{ from: '2026-11-08', to: '2026-11-08' }];
373
+ assert.throws(() => validateSessionConfig({ excludeRanges: bad }), ConfigError);
374
+ assert.throws(() => validateSessionConfig({ excludeRanges: bad }, { path: 'sessions/x.json' }), /sessions\/x\.json\.excludeRanges\[0\]/);
375
+ });