dsh-soul 0.7.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -2
- package/README_EN.md +17 -2
- package/client/index.mjs +1391 -10
- package/index.mjs +803 -26
- package/lib/bindings.mjs +430 -0
- package/lib/config.mjs +40 -1
- package/lib/history.mjs +593 -0
- package/package.json +6 -3
- package/scripts/verify-bindings.mjs +552 -0
- package/scripts/verify-client.mjs +548 -9
- package/scripts/verify-config.mjs +894 -9
- package/scripts/verify-e2e-prompt.mjs +932 -24
- package/scripts/verify-history.mjs +771 -0
|
@@ -16,6 +16,9 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
|
16
16
|
import {
|
|
17
17
|
DEFAULT_CONFIG,
|
|
18
18
|
FIELD_LIMITS,
|
|
19
|
+
HISTORY_MAX_ENTRIES_DEFAULT,
|
|
20
|
+
HISTORY_MAX_ENTRIES_MAX,
|
|
21
|
+
HISTORY_MAX_ENTRIES_MIN,
|
|
19
22
|
LANGUAGE_VALUES,
|
|
20
23
|
PERSONA_FIELDS,
|
|
21
24
|
PERSONA_NAME_MAX,
|
|
@@ -24,11 +27,13 @@ import {
|
|
|
24
27
|
REPLY_LENGTH_VALUES,
|
|
25
28
|
STYLE_VALUES,
|
|
26
29
|
TRAIT_VALUES,
|
|
30
|
+
clampHistoryMaxEntries,
|
|
27
31
|
migrateConfig,
|
|
28
32
|
normalizePersonas,
|
|
29
33
|
sanitizeConfig,
|
|
30
34
|
sanitizePersonaName
|
|
31
35
|
} from '../lib/config.mjs'
|
|
36
|
+
import { MAX_ENTRIES } from '../lib/history.mjs'
|
|
32
37
|
import {
|
|
33
38
|
BUILTIN_PERSONAS,
|
|
34
39
|
builtinPersonaNames,
|
|
@@ -45,7 +50,7 @@ import { assertCount, skipNote } from './lib/skip-report.mjs'
|
|
|
45
50
|
// 注意:本脚本没有环境相关的跳过分支(只读仓库内的文件与纯函数,不依赖浏览器 / DSH /
|
|
46
51
|
// 平台能力),所以链里的 `--strict` 对它是空操作 —— 这是**事实描述**,不是缺陷。
|
|
47
52
|
// 它的数字由这里的 assertCount 守住;需要 --strict 的是那些有跳过能力的脚本。
|
|
48
|
-
const EXPECTED_ASSERTIONS =
|
|
53
|
+
const EXPECTED_ASSERTIONS = 137
|
|
49
54
|
|
|
50
55
|
let passed = 0
|
|
51
56
|
const failures = []
|
|
@@ -458,8 +463,97 @@ check('migrateConfig:合法光轨配置保留,颜色转大写', () => {
|
|
|
458
463
|
assert.equal(config.trailWidth, 'thick')
|
|
459
464
|
})
|
|
460
465
|
|
|
461
|
-
|
|
466
|
+
// —— 变更记录保留条数(0.8.0 打磨)——
|
|
467
|
+
// 这条字段与其它容量参数的区别:它同时被三处消费(压实上限、响应里的 max、UI 表单),
|
|
468
|
+
// 任一处漏读都会出现「显示的和实际的不一致」,而那种不一致用户是察觉不到的
|
|
469
|
+
// —— 他只会发现「说好保留 50 条,怎么还能撤销到很早之前」。
|
|
470
|
+
check('DEFAULT_CONFIG 含变更记录条数默认值,且与 lib/history.mjs 的常量一致', () => {
|
|
471
|
+
assert.equal(DEFAULT_CONFIG.historyMaxEntries, 200)
|
|
472
|
+
assert.equal(HISTORY_MAX_ENTRIES_DEFAULT, 200)
|
|
473
|
+
// 两条常量分居两个模块,各有各的用途(一个是配置默认值,一个是压实兜底)。
|
|
474
|
+
// 数值必须相等,否则「没传上限」时的行为会与默认配置不符。
|
|
475
|
+
assert.equal(MAX_ENTRIES, HISTORY_MAX_ENTRIES_DEFAULT, 'lib/history.mjs 的兜底必须等于配置默认值')
|
|
476
|
+
assert.equal(HISTORY_MAX_ENTRIES_MIN, 50)
|
|
477
|
+
assert.equal(HISTORY_MAX_ENTRIES_MAX, 500)
|
|
478
|
+
})
|
|
479
|
+
|
|
480
|
+
check('clampHistoryMaxEntries:区间内原样、越界夹取、非整数返回 null', () => {
|
|
481
|
+
assert.equal(clampHistoryMaxEntries(200), 200)
|
|
482
|
+
assert.equal(clampHistoryMaxEntries(50), 50)
|
|
483
|
+
assert.equal(clampHistoryMaxEntries(500), 500)
|
|
484
|
+
// 夹取而不是拒绝:容量参数越界时放宽比报错更符合直觉(用户要 30,给 50 是可解释的)
|
|
485
|
+
assert.equal(clampHistoryMaxEntries(30), 50)
|
|
486
|
+
assert.equal(clampHistoryMaxEntries(100000), 500)
|
|
487
|
+
assert.equal(clampHistoryMaxEntries(0), 50)
|
|
488
|
+
assert.equal(clampHistoryMaxEntries(-5), 50)
|
|
489
|
+
// 非整数 / 非数字交给调用方决定:这里「猜」会把脏数据和手误混为一谈
|
|
490
|
+
assert.equal(clampHistoryMaxEntries(50.5), null)
|
|
491
|
+
assert.equal(clampHistoryMaxEntries('100'), null, '字符串不做隐式转换:表单里传字符串是调用方的 bug')
|
|
492
|
+
assert.equal(clampHistoryMaxEntries(NaN), null)
|
|
493
|
+
assert.equal(clampHistoryMaxEntries(Infinity), null)
|
|
494
|
+
assert.equal(clampHistoryMaxEntries(null), null)
|
|
495
|
+
assert.equal(clampHistoryMaxEntries(undefined), null)
|
|
496
|
+
})
|
|
497
|
+
|
|
498
|
+
check('sanitizeConfig:变更记录条数越界夹取、非整数才拒绝', () => {
|
|
499
|
+
assert.equal(sanitizeConfig({ historyMaxEntries: 100 }).patch.historyMaxEntries, 100)
|
|
500
|
+
assert.equal(sanitizeConfig({ historyMaxEntries: 30 }).patch.historyMaxEntries, 50, '越界夹取,不报错')
|
|
501
|
+
assert.equal(sanitizeConfig({ historyMaxEntries: 9000 }).patch.historyMaxEntries, 500)
|
|
502
|
+
assert.equal(sanitizeConfig({ historyMaxEntries: 30 }).errors.historyMaxEntries, undefined)
|
|
503
|
+
assert.ok(sanitizeConfig({ historyMaxEntries: 12.5 }).errors.historyMaxEntries)
|
|
504
|
+
assert.ok(sanitizeConfig({ historyMaxEntries: '200' }).errors.historyMaxEntries)
|
|
505
|
+
assert.ok(sanitizeConfig({ historyMaxEntries: null }).errors.historyMaxEntries)
|
|
506
|
+
assert.equal('historyMaxEntries' in sanitizeConfig({ historyMaxEntries: 'x' }).patch, false)
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
check('migrateConfig:变更记录条数非数字回退默认值、是数字则一律夹取', () => {
|
|
510
|
+
// 磁盘配置是**不可信输入**:手改过的 null、字符串都会原样读进来。
|
|
511
|
+
// 判据只有一条 ——「是数字就夹取,不是数字才回退」。刻意不给 0 / 负数开特例:
|
|
512
|
+
// · 与 sanitizeConfig 必须同策略,否则同一个 0 走 HTTP 变成 50、走手改配置变成 200,
|
|
513
|
+
// 同一个输入两种结果,是脏数据能钻过去的缝;
|
|
514
|
+
// · 0 的意图方向是「少留一点」,夹到下限 50 顺着这个方向;
|
|
515
|
+
// 若回退成默认值 200 反而把保留窗口**变大**了,与用户的意思正好相反。
|
|
516
|
+
assert.equal(migrateConfig({}).historyMaxEntries, 200)
|
|
517
|
+
assert.equal(migrateConfig({ historyMaxEntries: null }).historyMaxEntries, 200)
|
|
518
|
+
assert.equal(migrateConfig({ historyMaxEntries: 'abc' }).historyMaxEntries, 200)
|
|
519
|
+
assert.equal(migrateConfig({ historyMaxEntries: '200' }).historyMaxEntries, 200, '字符串不当数字用')
|
|
520
|
+
assert.equal(migrateConfig({ historyMaxEntries: 12.5 }).historyMaxEntries, 200, '小数不是合法条数')
|
|
521
|
+
assert.equal(migrateConfig({ historyMaxEntries: 0 }).historyMaxEntries, 50)
|
|
522
|
+
assert.equal(migrateConfig({ historyMaxEntries: -100 }).historyMaxEntries, 50)
|
|
523
|
+
assert.equal(migrateConfig({ historyMaxEntries: 10 }).historyMaxEntries, 50)
|
|
524
|
+
assert.equal(migrateConfig({ historyMaxEntries: 99999 }).historyMaxEntries, 500)
|
|
525
|
+
assert.equal(migrateConfig({ historyMaxEntries: 400 }).historyMaxEntries, 400)
|
|
526
|
+
})
|
|
527
|
+
|
|
528
|
+
// 这条是「UI 与后端契约」的地基:设置页的下拉选项必须全部落在服务端接受的区间内。
|
|
529
|
+
// 只在客户端加一档 1000,用户选中后保存会被夹成 500 —— 界面显示 1000、实际存 500。
|
|
530
|
+
check('变更记录条数:设置页下拉选项全部落在合法区间内,且必含默认档', () => {
|
|
531
|
+
const clientText = readFileSync(new URL('../client/index.mjs', import.meta.url), 'utf8')
|
|
532
|
+
const block = clientText.match(/const HISTORY_MAX_ENTRIES_OPTIONS = \[([^\]]*)\]/)?.[1] ?? ''
|
|
533
|
+
assert.ok(block !== '', '找不到 client/index.mjs 的 HISTORY_MAX_ENTRIES_OPTIONS')
|
|
534
|
+
const options = block.split(',').map((s) => Number(s.trim())).filter((n) => Number.isFinite(n))
|
|
535
|
+
assert.ok(options.length >= 2, '至少要有两个档位,否则这个设置项没有选择余地')
|
|
536
|
+
for (const n of options) {
|
|
537
|
+
assert.ok(n >= HISTORY_MAX_ENTRIES_MIN && n <= HISTORY_MAX_ENTRIES_MAX, `档位 ${n} 超出服务端接受的 ${HISTORY_MAX_ENTRIES_MIN}–${HISTORY_MAX_ENTRIES_MAX}`)
|
|
538
|
+
assert.equal(clampHistoryMaxEntries(n), n, `档位 ${n} 被服务端夹取,说明两端对不上`)
|
|
539
|
+
}
|
|
540
|
+
assert.ok(options.includes(HISTORY_MAX_ENTRIES_DEFAULT), `选项里必须有默认档 ${HISTORY_MAX_ENTRIES_DEFAULT}`)
|
|
541
|
+
// 客户端那份默认值也必须与后端一致(表单初值用它)
|
|
542
|
+
assert.match(clientText, /const HISTORY_MAX_ENTRIES_DEFAULT = 200/, '客户端默认值必须与 lib/config.mjs 一致')
|
|
543
|
+
})
|
|
462
544
|
|
|
545
|
+
check('变更记录条数:config 双语文案键齐全,且 UI 真的用了它', () => {
|
|
546
|
+
const clientText = readFileSync(new URL('../client/index.mjs', import.meta.url), 'utf8')
|
|
547
|
+
for (const key of ['history.maxEntries', 'history.maxEntriesHint', 'history.maxEntriesDefault', 'history.maxEntriesOption']) {
|
|
548
|
+
// 中英各一份:漏一份会让 makeFallbackT 把裸键名渲染到屏幕上
|
|
549
|
+
const hits = clientText.split(`'${key}':`).length - 1
|
|
550
|
+
assert.equal(hits, 2, `文案键 ${key} 应为中英各一份,实际 ${hits} 份`)
|
|
551
|
+
}
|
|
552
|
+
assert.match(clientText, /id: 'soul-historyMaxEntries'/, '设置页必须有这个下拉的 id(label 的 htmlFor 指向它)')
|
|
553
|
+
assert.match(clientText, /htmlFor: 'soul-historyMaxEntries'/, 'label 必须关联到控件,否则点标签不会聚焦')
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
console.log('插件清单与图标')
|
|
463
557
|
// DSH 读取 package.json 的 icon 字段(app-boot 的 readPluginMeta / iconOf),
|
|
464
558
|
// 在插件管理列表与侧栏入口渲染。图标缺失只是回退默认插图,但路径写错、
|
|
465
559
|
// 或没进 files 白名单,就会出现「仓库里有、装完没有」的静默失败,故固化为检查。
|
|
@@ -1590,10 +1684,13 @@ check('客户端预设行摘要不读「关于你」', () => {
|
|
|
1590
1684
|
assert.ok(/row\.style/.test(block[1]), '预设行摘要仍应读 style')
|
|
1591
1685
|
})
|
|
1592
1686
|
|
|
1593
|
-
check('
|
|
1594
|
-
// HTTP 保存 / 删除各一处,/soul 命令的 save / del
|
|
1687
|
+
check('服务端五条写路径都拒绝内置名,读取路径都走合并库', () => {
|
|
1688
|
+
// HTTP 保存 / 删除各一处,/soul 命令的 save / del 各一处,
|
|
1689
|
+
// 再加 0.8.0-c 的「恢复预设」——它也是一条写路径。
|
|
1690
|
+
// 恢复路径这一处不是冗余防御:记录文件按契约是**不可信输入**(可以被人手改),
|
|
1691
|
+
// 手工塞进 removed 列表里的内置名若不拦,就会造出一个遮蔽内置预设的同名自定义条目。
|
|
1595
1692
|
const rejections = indexCode.split('isBuiltinPersona(personaName)').length - 1
|
|
1596
|
-
assert.equal(rejections,
|
|
1693
|
+
assert.equal(rejections, 5, `内置名拒绝点应为 5 处(HTTP 2 + 命令层 2 + 恢复预设 1),实际 ${rejections} 处`)
|
|
1597
1694
|
assert.ok(indexCode.split('mergePersonas(').length - 1 >= 3, '预设库读取应统一走 mergePersonas')
|
|
1598
1695
|
assert.ok(/resolvePersona\(personaName, config\.personas\)/.test(indexCode), '命令层 use 未走 resolvePersona')
|
|
1599
1696
|
assert.ok(/resolvePersona\(personaName, current\.personas\)/.test(indexCode), 'HTTP use 未走 resolvePersona')
|
|
@@ -1654,6 +1751,50 @@ check('客户端「参与编译的字段」与宿主 compilePrompt 实际读取
|
|
|
1654
1751
|
assert.ok(clientKeys.length >= 5, `参与编译的字段过少:${clientKeys.length}`)
|
|
1655
1752
|
})
|
|
1656
1753
|
|
|
1754
|
+
check('NON_PROMPT_FIELDS 由 DEFAULT_CONFIG 减去 PROMPT_INPUT_FIELDS 推导,且编译源码读取的字段全部已登记', () => {
|
|
1755
|
+
// 这一对断言是**互相依存**的,缺一条另一条就会变成空话:
|
|
1756
|
+
// - 「推导」保证新增字段默认被当成影响提示词(方向安全:最坏多刷新一次,
|
|
1757
|
+
// 而不是「改了不生效」);
|
|
1758
|
+
// - 但推导只有在「compilePrompt 只读 PROMPT_INPUT_FIELDS 里的字段」为真时才安全。
|
|
1759
|
+
// 若将来有人往 compilePrompt 里加了一个 config.X 却忘了登记进 PROMPT_INPUT_FIELDS,
|
|
1760
|
+
// 那个字段会被推导进 NON_PROMPT_FIELDS —— 于是「改了它,提示词不变」,
|
|
1761
|
+
// 而且没有任何报错。这是本插件最坏的一类静默失败,所以必须在这里钉死。
|
|
1762
|
+
|
|
1763
|
+
// ① 不得退回手写清单:index.mjs 里该常量必须由 DEFAULT_CONFIG 与 PROMPT_INPUT_FIELDS 推出
|
|
1764
|
+
const derived = indexCode.match(/const NON_PROMPT_FIELDS = new Set\([\s\S]*?\)\n/)
|
|
1765
|
+
assert.ok(derived, 'index.mjs 缺少 NON_PROMPT_FIELDS 定义')
|
|
1766
|
+
assert.ok(/Object\.keys\(DEFAULT_CONFIG\)/.test(derived[0]), 'NON_PROMPT_FIELDS 必须由 Object.keys(DEFAULT_CONFIG) 推导,不得手写清单')
|
|
1767
|
+
assert.ok(/PROMPT_INPUT_FIELDS\.includes/.test(derived[0]), 'NON_PROMPT_FIELDS 必须用 PROMPT_INPUT_FIELDS 做筛选')
|
|
1768
|
+
assert.equal(
|
|
1769
|
+
/'trailEnabled'|'trailColor'/.test(derived[0]),
|
|
1770
|
+
false,
|
|
1771
|
+
'NON_PROMPT_FIELDS 里不得再出现手写的字段名(这正是漏掉 requireToolConfirmation / historyMaxEntries 的成因)'
|
|
1772
|
+
)
|
|
1773
|
+
assert.ok(/from '\.\/lib\/config\.mjs'|PROMPT_INPUT_FIELDS,/.test(indexCode), 'index.mjs 应 import PROMPT_INPUT_FIELDS')
|
|
1774
|
+
|
|
1775
|
+
// ② 推导的安全前提:编译源码读取的每个 config.X 都必须在 PROMPT_INPUT_FIELDS 里
|
|
1776
|
+
const registered = new Set(PROMPT_INPUT_FIELDS)
|
|
1777
|
+
const readKeys = [...new Set([...promptCompilerSource().matchAll(/config\.(\w+)/g)].map((m) => m[1]))]
|
|
1778
|
+
assert.ok(readKeys.length >= 8, `编译源码里解析到的字段过少:${readKeys.length}(正则或函数边界可能已失效)`)
|
|
1779
|
+
const unregistered = readKeys.filter((key) => !registered.has(key))
|
|
1780
|
+
assert.deepEqual(
|
|
1781
|
+
unregistered,
|
|
1782
|
+
[],
|
|
1783
|
+
`提示词编译读取了未登记的字段:${unregistered.join(', ')}。` +
|
|
1784
|
+
'它们会被推导进 NON_PROMPT_FIELDS,导致「改了配置但提示词不更新」且不报错。' +
|
|
1785
|
+
'若确属提示词字段,请加进 lib/config.mjs 的 PROMPT_INPUT_FIELDS;' +
|
|
1786
|
+
'若确实不影响提示词,请把读取从这几个函数里移出去。'
|
|
1787
|
+
)
|
|
1788
|
+
|
|
1789
|
+
// ③ 两个集合必须互补且覆盖 DEFAULT_CONFIG 全键(推导语法错了会两边都漏)
|
|
1790
|
+
const all = Object.keys(DEFAULT_CONFIG)
|
|
1791
|
+
const promptSide = all.filter((key) => registered.has(key))
|
|
1792
|
+
const nonPromptSide = all.filter((key) => !registered.has(key))
|
|
1793
|
+
assert.equal(promptSide.length + nonPromptSide.length, all.length, 'DEFAULT_CONFIG 键分类不完整')
|
|
1794
|
+
assert.deepEqual(promptSide.slice().sort(), [...PROMPT_INPUT_FIELDS].sort(), 'PROMPT_INPUT_FIELDS 里出现了 DEFAULT_CONFIG 之外的键')
|
|
1795
|
+
assert.equal(nonPromptSide.length, 6, `不影响提示词的字段数应为 6(requireToolConfirmation + 4 个 trail* + historyMaxEntries),实际 ${nonPromptSide.length}`)
|
|
1796
|
+
})
|
|
1797
|
+
|
|
1657
1798
|
check('客户端复刻的枚举常量与宿主 lib/config.mjs 逐项一致', () => {
|
|
1658
1799
|
// 浏览器模块表不允许 client/index.mjs import lib/config.mjs,所以客户端必须**复刻**
|
|
1659
1800
|
// 一份常量;这些常量本身写着「与 lib/config.mjs 的合法值保持一致」,但那只是注释。
|
|
@@ -1684,9 +1825,25 @@ check('客户端复刻的枚举常量与宿主 lib/config.mjs 逐项一致', ()
|
|
|
1684
1825
|
check('客户端 FIELD_KEYS 覆盖宿主全部配置字段(dirty 门控不漏字段)', () => {
|
|
1685
1826
|
// FIELD_KEYS 驱动 dirty 判定与保存载荷。漏一个字段的后果是静默的:
|
|
1686
1827
|
// 改那个字段后「保存」按钮可能仍被禁用(看起来像界面卡住),或保存结果被判成「无变化」。
|
|
1828
|
+
//
|
|
1829
|
+
// 0.8.1 起 FIELD_KEYS 由两段组合而成(`[...FORM_FIELD_KEYS, ...EXTERNALLY_MANAGED_FIELDS]`):
|
|
1830
|
+
// 「保留条数」搬去插件详情页后不再属于设置页表单,但**仍然是一个配置字段**,
|
|
1831
|
+
// 必须继续被这张全集表覆盖。所以这里按组合语义解析,而不是要求它仍是单个数组字面量
|
|
1832
|
+
// —— 断言要守的是「全集对不对」,不是「写法长什么样」。
|
|
1833
|
+
const parseKeys = (name) => {
|
|
1834
|
+
const m = clientCode.match(new RegExp(`const ${name} = \\[([^\\]]*)\\]`))
|
|
1835
|
+
assert.ok(m, `客户端缺少 ${name}`)
|
|
1836
|
+
return [...m[1].matchAll(/'([^']*)'/g)].map((x) => x[1])
|
|
1837
|
+
}
|
|
1687
1838
|
const m = clientCode.match(/const FIELD_KEYS = \[([^\]]*)\]/)
|
|
1688
1839
|
assert.ok(m, '客户端缺少 FIELD_KEYS')
|
|
1689
|
-
const clientKeys =
|
|
1840
|
+
const clientKeys = m[1].includes('...')
|
|
1841
|
+
// 组合式:把每个展开项按同名常量解析出来再拼起来
|
|
1842
|
+
? m[1].split(',').flatMap((part) => {
|
|
1843
|
+
const spread = part.trim().match(/^\.\.\.(\w+)$/)
|
|
1844
|
+
return spread ? parseKeys(spread[1]) : part.trim().match(/'([^']*)'/g) || []
|
|
1845
|
+
}).map((x) => x.replace(/'/g, ''))
|
|
1846
|
+
: parseKeys('FIELD_KEYS')
|
|
1690
1847
|
const hostFields = Object.keys(DEFAULT_CONFIG).sort()
|
|
1691
1848
|
assert.deepEqual(
|
|
1692
1849
|
[...clientKeys].sort(),
|
|
@@ -1695,6 +1852,20 @@ check('客户端 FIELD_KEYS 覆盖宿主全部配置字段(dirty 门控不漏
|
|
|
1695
1852
|
)
|
|
1696
1853
|
// 顺序无关紧要但重复有害:同一字段出现两次会让 dirty 判定重复计算。
|
|
1697
1854
|
assert.equal(new Set(clientKeys).size, clientKeys.length, 'FIELD_KEYS 含重复字段')
|
|
1855
|
+
// 组合式写法下,两段不得交叉:表单字段与「别处管理」的字段是互斥集合,
|
|
1856
|
+
// 交叉会让「这个字段归谁管」变成两处都要读才能回答。
|
|
1857
|
+
const formKeys = parseKeys('FORM_FIELD_KEYS')
|
|
1858
|
+
const externalKeys = parseKeys('EXTERNALLY_MANAGED_FIELDS')
|
|
1859
|
+
assert.deepEqual(
|
|
1860
|
+
formKeys.filter((k) => externalKeys.includes(k)),
|
|
1861
|
+
[],
|
|
1862
|
+
'FORM_FIELD_KEYS 与 EXTERNALLY_MANAGED_FIELDS 不得有交集'
|
|
1863
|
+
)
|
|
1864
|
+
// dirty 判定必须只用表单段:拿全集去比,会让表单对一个它并不持有的字段反复比较。
|
|
1865
|
+
// (全集里可能包含表单没有的字段,那样 dirty 会恒为真或恒为假 —— 都不是想要的语义。)
|
|
1866
|
+
const dirtyExpr = clientCode.match(/const dirty = (\w+)\.some\(/)
|
|
1867
|
+
assert.ok(dirtyExpr, '应能读到 dirty 的判定表达式')
|
|
1868
|
+
assert.equal(dirtyExpr[1], 'FORM_FIELD_KEYS', 'dirty 必须只看 FORM_FIELD_KEYS(表单实际持有的字段)')
|
|
1698
1869
|
})
|
|
1699
1870
|
|
|
1700
1871
|
check('预设应用只有一条实现路径,且非法字段必须回报(HTTP 与 /soul use 共用)', () => {
|
|
@@ -2036,12 +2207,17 @@ check('移除损坏配置只有一条实现:lib/store.mjs 的 moveAsideConfigF
|
|
|
2036
2207
|
})
|
|
2037
2208
|
|
|
2038
2209
|
check('重置是配置损坏时的逃生口:先移开损坏文件,再复用同一条写入路径', () => {
|
|
2039
|
-
|
|
2210
|
+
// 签名带 source 形参(0.8.0 起用于变更记录的归属),故匹配式允许括号内有内容
|
|
2211
|
+
const matched = indexCode.match(/async function resetConfigWithEscape\([\s\S]*?\) \{[\s\S]*?\n\}/)
|
|
2040
2212
|
assert.ok(matched, 'index.mjs 中找不到 resetConfigWithEscape')
|
|
2041
2213
|
const body = matched[0]
|
|
2042
2214
|
assert.ok(/moveAsideConfigFile\(configPath\(\)\)/.test(body), '重置应先移开损坏文件(=备份到 .corrupt)')
|
|
2043
2215
|
assert.ok(/configLoadFailure = null/.test(body), '移开之后必须清掉失败标记,否则 saveConfig 仍会拒绝')
|
|
2044
2216
|
assert.ok(/commitConfig\(/.test(body), '重置应复用 commitConfig,而不是另写一份「读—改—写」')
|
|
2217
|
+
assert.ok(
|
|
2218
|
+
/commitConfig\([\s\S]*?,\s*source\)/.test(body),
|
|
2219
|
+
'source 形参必须穿透给 commitConfig——否则重置动作在变更记录里失去归属'
|
|
2220
|
+
)
|
|
2045
2221
|
assert.ok(
|
|
2046
2222
|
/catch[\s\S]*?configLoadFailure = failure[\s\S]*?throw/.test(body),
|
|
2047
2223
|
'写入失败必须恢复失败标记,否则会把「文件仍然不可用」误报成正常'
|
|
@@ -2051,13 +2227,21 @@ check('重置是配置损坏时的逃生口:先移开损坏文件,再复用
|
|
|
2051
2227
|
check('两个重置入口都走逃生口(HTTP 与斜杠命令)', () => {
|
|
2052
2228
|
const route = indexCode.match(/path: '\/api\/soul\/config\/reset'[\s\S]*?\n \}\)/)
|
|
2053
2229
|
assert.ok(route, '找不到 /api/soul/config/reset 端点')
|
|
2054
|
-
assert.ok(/resetConfigWithEscape\(
|
|
2230
|
+
assert.ok(/resetConfigWithEscape\(/.test(route[0]), 'HTTP 重置必须走逃生口')
|
|
2055
2231
|
assert.ok(/backupPath/.test(route[0]), 'HTTP 重置应回传备份路径,供界面如实说明数据去哪了')
|
|
2232
|
+
assert.ok(
|
|
2233
|
+
/resetConfigWithEscape\(\{ source: 'web', action: 'reset' \}\)/.test(route[0]),
|
|
2234
|
+
'HTTP 重置必须标注来源 web/reset,否则变更记录里认不出这次重置从哪来'
|
|
2235
|
+
)
|
|
2056
2236
|
|
|
2057
2237
|
const resetBranch = indexCode.match(/if \(first === 'reset'\)[\s\S]*?\n \}/)
|
|
2058
2238
|
assert.ok(resetBranch, "找不到 /soul reset 分支")
|
|
2059
|
-
assert.ok(/resetConfigWithEscape\(
|
|
2239
|
+
assert.ok(/resetConfigWithEscape\(/.test(resetBranch[0]), '/soul reset 必须走逃生口')
|
|
2060
2240
|
assert.ok(/backupPath/.test(resetBranch[0]), '/soul reset 应告知备份位置')
|
|
2241
|
+
assert.ok(
|
|
2242
|
+
/resetConfigWithEscape\(\{ source: 'command', action: 'reset' \}\)/.test(resetBranch[0]),
|
|
2243
|
+
'/soul reset 必须标注来源 command/reset——与网页重置是两个入口,记录里要分得开'
|
|
2244
|
+
)
|
|
2061
2245
|
|
|
2062
2246
|
assert.equal(
|
|
2063
2247
|
/commitConfig\(current => defaultConfigPreservingPersonas\(current\)\)/.test(indexCode),
|
|
@@ -2066,6 +2250,186 @@ check('两个重置入口都走逃生口(HTTP 与斜杠命令)', () => {
|
|
|
2066
2250
|
)
|
|
2067
2251
|
})
|
|
2068
2252
|
|
|
2253
|
+
check('提交即留痕:唯一漏斗的每个调用点都自带来源归属', () => {
|
|
2254
|
+
// 0.8.0 的变更记录要回答「谁改的」。事后从配置差异里推不出来,所以来源必须由
|
|
2255
|
+
// 调用方显式给出 —— 这条断言守的就是「显式」二字:既不许缺,也不许给默认值兜底。
|
|
2256
|
+
const signature = indexCode.match(/async function commitConfig\(([^)]*)\)/)
|
|
2257
|
+
assert.ok(signature, 'index.mjs 中找不到 commitConfig')
|
|
2258
|
+
assert.match(signature[1], /mutate,\s*source/, 'commitConfig 必须有 source 形参')
|
|
2259
|
+
assert.equal(
|
|
2260
|
+
/source\s*=\s*[^,)]/.test(signature[1]),
|
|
2261
|
+
false,
|
|
2262
|
+
'source 不得有默认值——默认值会让「忘了标注』的调用点静默算作某个来源,比缺来源更糟'
|
|
2263
|
+
)
|
|
2264
|
+
|
|
2265
|
+
// 全部来源标注:18 处调用点各一个。数量写死是刻意的——新增写路径时这里会红,
|
|
2266
|
+
// 提醒作者同时决定它的来源归属,而不是让它悄悄落进默认桶。
|
|
2267
|
+
// (0.8.0-c 加了两条撤销路径:撤销本身也写配置,于是「我撤销过一次」也留下一条
|
|
2268
|
+
// 记录,审计因此是链条而不是快照,能看出「改了、撤了、又改回去」。)
|
|
2269
|
+
const expected = [
|
|
2270
|
+
["{ source: 'web', action: 'save' }", 'HTTP 保存配置'],
|
|
2271
|
+
["{ source: 'web', action: 'reset' }", 'HTTP 重置'],
|
|
2272
|
+
["{ source: 'web', action: 'savePreset' }", 'HTTP 保存预设'],
|
|
2273
|
+
["{ source: 'web', action: 'deletePreset' }", 'HTTP 删除预设'],
|
|
2274
|
+
["{ source: 'persona', action: 'use', label: entry && typeof entry.name === 'string' ? entry.name : undefined }", '应用预设(两条入口共用,label 带预设名)'],
|
|
2275
|
+
["{ source: 'command', action: 'nickname' }", '/soul <昵称>'],
|
|
2276
|
+
["{ source: 'command', action: 'reset' }", '/soul reset'],
|
|
2277
|
+
["{ source: 'command', action: 'enable' }", '/soul enable'],
|
|
2278
|
+
["{ source: 'command', action: 'disable' }", '/soul disable'],
|
|
2279
|
+
["{ source: 'command', action: 'savePreset' }", '/soul save'],
|
|
2280
|
+
["{ source: 'command', action: 'deletePreset' }", '/soul del'],
|
|
2281
|
+
["{ source: 'command', action: 'set' }", '/soul set'],
|
|
2282
|
+
["{ source: 'command', action: 'confirm' }", '/soul confirm'],
|
|
2283
|
+
["{ source: 'tool', action: 'setPersona' }", 'set_persona 工具'],
|
|
2284
|
+
["{ source: 'service', action: 'update' }", 'soulConfig.updateConfig'],
|
|
2285
|
+
["{ source: 'service', action: 'reset' }", 'soulConfig.resetConfig'],
|
|
2286
|
+
["{ source: 'rollback', action: 'rollback' }", 'HTTP 撤销'],
|
|
2287
|
+
["{ source: 'restorePersona', action: 'restorePreset' }", 'HTTP 恢复预设']
|
|
2288
|
+
]
|
|
2289
|
+
const found = expected.filter(([literal]) => indexCode.includes(literal))
|
|
2290
|
+
assert.deepEqual(
|
|
2291
|
+
found.map(([literal]) => literal),
|
|
2292
|
+
expected.map(([literal]) => literal),
|
|
2293
|
+
`缺少来源标注:${expected.filter(([l]) => !indexCode.includes(l)).map(([, who]) => who).join('、')}`
|
|
2294
|
+
)
|
|
2295
|
+
const total = indexCode.match(/\{ source: '[a-z][A-Za-z]*', action: '/g) || []
|
|
2296
|
+
assert.equal(total.length, expected.length, `来源标注应有 ${expected.length} 处,实际 ${total.length} 处——多出来的是未登记的写路径`)
|
|
2297
|
+
|
|
2298
|
+
// 关联性(光有标注不够,还要落在对的地方):抽几个有稳定锚点的切片来钉,
|
|
2299
|
+
// 防止「标注都在,但张冠李戴」——例如把 confirm 的标注挪到 set 上。
|
|
2300
|
+
const personaApply = indexCode.match(/async function applyPersonaEntry\(ctx, entry\)[\s\S]*?\n\}/)?.[0] ?? ''
|
|
2301
|
+
assert.match(personaApply, /\{ source: 'persona', action: 'use', label: entry && typeof entry\.name === 'string' \? entry\.name : undefined \}/, 'applyPersonaEntry 的来源应是 persona/use 且 label 带预设名')
|
|
2302
|
+
const toolSlice = indexSource.match(/defineTool\([\s\S]*?\n \}\)/)?.[0] ?? ''
|
|
2303
|
+
assert.match(toolSlice, /\{ source: 'tool', action: 'setPersona' \}/, 'set_persona 工具的来源应是 tool/setPersona')
|
|
2304
|
+
assert.match(indexCode, /updateConfig[\s\S]*?\{ source: 'service', action: 'update' \}/, 'updateConfig 的来源应是 service/update')
|
|
2305
|
+
assert.match(indexCode, /resetConfig\(\)[\s\S]*?resetConfigWithEscape\(\{ source: 'service', action: 'reset' \}\)/, 'resetConfig 服务的来源应是 service/reset')
|
|
2306
|
+
// 两条撤销路径的来源必须靠在自己的路由切片里(两条路由都调 commitConfig,
|
|
2307
|
+
// 标注写反了在「16 处齐不齐」这种判据下完全看不出来)
|
|
2308
|
+
const rollbackRoute = indexCode.match(/path: '\/api\/soul\/history\/rollback'[\s\S]*?\n \}\)/)?.[0] ?? ''
|
|
2309
|
+
assert.match(rollbackRoute, /\{ source: 'rollback', action: 'rollback' \}/, '撤销路由的来源应是 rollback/rollback')
|
|
2310
|
+
const restoreRoute = indexCode.match(/path: '\/api\/soul\/history\/restore-persona'[\s\S]*?\n \}\)/)?.[0] ?? ''
|
|
2311
|
+
assert.match(restoreRoute, /\{ source: 'restorePersona', action: 'restorePreset' \}/, '恢复预设路由的来源应是 restorePersona/restorePreset')
|
|
2312
|
+
|
|
2313
|
+
// 重置的 source 必须由 commitConfig 的形参穿透,而不是在内部写死 —— 否则三个重置
|
|
2314
|
+
// 入口(网页 / 命令 / 服务)会在记录里退化成同一个来源。
|
|
2315
|
+
const escape = indexCode.match(/async function resetConfigWithEscape\(([^)]*)\)[\s\S]*?\n\}/)
|
|
2316
|
+
assert.ok(escape, '找不到 resetConfigWithEscape')
|
|
2317
|
+
assert.match(escape[1], /source/, 'resetConfigWithEscape 必须接收 source')
|
|
2318
|
+
assert.match(escape[0], /commitConfig\([\s\S]*?,\s*source\)/, 'source 必须穿透给 commitConfig')
|
|
2319
|
+
})
|
|
2320
|
+
|
|
2321
|
+
check('留痕时机在配置落盘之后,且失败不得连带配置写失败', () => {
|
|
2322
|
+
const body = indexCode.match(/async function commitConfig\([^)]*\)[\s\S]*?\n\}/)?.[0] ?? ''
|
|
2323
|
+
assert.ok(body !== '', '找不到 commitConfig 函数体')
|
|
2324
|
+
const saveAt = body.indexOf('await saveConfig(next)')
|
|
2325
|
+
const auditAt = body.indexOf('await auditCommit(')
|
|
2326
|
+
assert.ok(saveAt !== -1 && auditAt !== -1, 'commitConfig 应在 saveConfig 之后调用 auditCommit')
|
|
2327
|
+
assert.ok(
|
|
2328
|
+
saveAt < auditAt,
|
|
2329
|
+
'留痕必须在配置落盘之后:反过来的话「配置写失败」会留下一条描述没发生过的变更的记录'
|
|
2330
|
+
)
|
|
2331
|
+
|
|
2332
|
+
// 差分基准必须是 saveConfig 的**返回值**(已剔除 tone / presets / examples 等废弃字段),
|
|
2333
|
+
// 而不是 mutate 的产物 —— 后者会把那些字段误报成变更。
|
|
2334
|
+
assert.match(
|
|
2335
|
+
body,
|
|
2336
|
+
/auditCommit\(current,\s*config,/,
|
|
2337
|
+
'auditCommit 的第二个参数应是 saveConfig 返回的 config,而不是 mutate 的中间产物'
|
|
2338
|
+
)
|
|
2339
|
+
|
|
2340
|
+
const audit = indexCode.match(/async function auditCommit\([^)]*\)[\s\S]*?\n\}/)?.[0] ?? ''
|
|
2341
|
+
assert.ok(audit !== '', '找不到 auditCommit')
|
|
2342
|
+
assert.match(audit, /try\s*\{/, 'auditCommit 必须自带 try —— 审计失败不能往外抛')
|
|
2343
|
+
assert.match(
|
|
2344
|
+
audit,
|
|
2345
|
+
/catch[\s\S]*?lastHistoryFailure\s*=/,
|
|
2346
|
+
'审计失败必须记进 lastHistoryFailure(经诊断端点回传),不得静默吞掉'
|
|
2347
|
+
)
|
|
2348
|
+
assert.equal(
|
|
2349
|
+
/catch[\s\S]*?throw/.test(audit),
|
|
2350
|
+
false,
|
|
2351
|
+
'审计失败绝不能向上抛:那会把「记不下来」升级成「改不了」,而配置写入才是核心功能'
|
|
2352
|
+
)
|
|
2353
|
+
|
|
2354
|
+
// 没变东西就不记:否则每次点「保存」都刷一条噪声条目。
|
|
2355
|
+
// 关键是把「真的没变」与「条目构造失败」分成两条路:后者是调用点漏标 source 的代码
|
|
2356
|
+
// 缺陷,若被当成「无变化」跳过,表现就是「配置改了、记录里没有、也没有任何告警」。
|
|
2357
|
+
assert.match(
|
|
2358
|
+
audit,
|
|
2359
|
+
/const changedAny =[\s\S]*?if \(!changedAny\)/,
|
|
2360
|
+
'「什么都没变」必须在构造条目之前用字段差分判掉,不能靠 buildHistoryEntry 返回 null 来表达'
|
|
2361
|
+
)
|
|
2362
|
+
assert.match(audit, /if \(!entry\)/, '有变化却构造不出条目时应有独立分支')
|
|
2363
|
+
// 匹配到 **非 null** 的赋值:`lastHistoryFailure = null` 不算「报出来了」——
|
|
2364
|
+
// 那恰好是「静默跳过」的写法,而它也能被更松的正则放过。
|
|
2365
|
+
assert.match(
|
|
2366
|
+
audit,
|
|
2367
|
+
/if \(!entry\)[\s\S]*?lastHistoryFailure\s*=\s*`/,
|
|
2368
|
+
'条目构造失败必须带原因记进 lastHistoryFailure(赋 null 等于静默跳过)'
|
|
2369
|
+
)
|
|
2370
|
+
assert.ok(
|
|
2371
|
+
/lastHistoryFailure\s*=\s*null/.test(audit),
|
|
2372
|
+
'记录成功(或无需记录)时应清掉上一次的失败原因,否则旧告警会永远挂着'
|
|
2373
|
+
)
|
|
2374
|
+
|
|
2375
|
+
// 路径:与配置同目录的兄弟文件,由 lib/history.mjs 推导,index.mjs 不自己拼字符串。
|
|
2376
|
+
assert.match(indexCode, /function historyPath\(\)[\s\S]*?historyPathFor\(configPath\(\)\)/, 'historyPath 应由 historyPathFor(configPath()) 推导')
|
|
2377
|
+
assert.equal(
|
|
2378
|
+
/\.history\.jsonl/.test(indexCode),
|
|
2379
|
+
false,
|
|
2380
|
+
'文件名后缀是 lib/history.mjs 的契约,index.mjs 里不该再有一份字面副本(改一处忘一处会写到别的文件)'
|
|
2381
|
+
)
|
|
2382
|
+
})
|
|
2383
|
+
|
|
2384
|
+
check('变更记录四条路由:方法校验、撤销语义、恢复预设的护栏', () => {
|
|
2385
|
+
// 契约里最容易退化的两处:一是写路由忘了校验 req.method(GET 也能触发写入,
|
|
2386
|
+
// 来自 /api/soul/config/reset 的真实教训);二是撤销把「读记录」放到队列外,
|
|
2387
|
+
// 于是并发时规划建立在已被别人改过的配置上。
|
|
2388
|
+
const routes = ['/api/soul/history', '/api/soul/history/rollback', '/api/soul/history/restore-persona', '/api/soul/history/clear']
|
|
2389
|
+
for (const path of routes) {
|
|
2390
|
+
const body = indexCode.match(new RegExp(`path: '${path.replace(/\//g, '\\/')}'[\\s\\S]*?\\n \\}\\)`))?.[0] ?? ''
|
|
2391
|
+
assert.ok(body !== '', `缺少路由 ${path}`)
|
|
2392
|
+
const expected = path === '/api/soul/history' ? 'GET' : 'POST'
|
|
2393
|
+
assert.match(body, new RegExp(`req\\.method !== '${expected}'`), `${path} 必须显式校验 req.method === ${expected}`)
|
|
2394
|
+
assert.match(body, /send\(res, 405, \{ ok: false, error: 'Method not allowed' \}\)/, `${path} 的方法不符应返回 405`)
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
const rollback = indexCode.match(/path: '\/api\/soul\/history\/rollback'[\s\S]*?\n \}\)/)?.[0] ?? ''
|
|
2398
|
+
// 撤销必须是**异步 mutate**:只有 mutate 体内能保证「读记录 → 规划 → 写配置」在同一队列
|
|
2399
|
+
// 任务里。同步 mutate 签名的写法会迫使调用方在入队前读记录,那正是竞态窗口。
|
|
2400
|
+
assert.match(rollback, /commitConfig\(async \(current\) => \{/, '撤销必须用 async mutate,在队列内读记录')
|
|
2401
|
+
assert.match(rollback, /if \(read\.error\) throw httpError\(500/, '记录读不出来必须中止,不能按「空记录」继续')
|
|
2402
|
+
assert.match(rollback, /planRollback\(read\.entries, current, id\)/, '撤销必须走 planRollback(链式指纹校验)')
|
|
2403
|
+
assert.match(rollback, /plan\.reason === 'missing'[\s\S]*?throw httpError\(400/, '找不到 id 应回 400')
|
|
2404
|
+
assert.match(rollback, /throw httpError\(409,/, '记录与配置对不上应回 409(不给一个猜出来的结果)')
|
|
2405
|
+
assert.ok(
|
|
2406
|
+
/Object\.keys\(plan\.patch\)\.length === 0\) return \{ \.\.\.current \}/.test(rollback),
|
|
2407
|
+
'补丁为空是合法结果(目标改动已被后续改回来),应原样返回而不是报错'
|
|
2408
|
+
)
|
|
2409
|
+
assert.match(rollback, /promptChanged\.length > 0\) refreshPromptAndInject/, '撤销后必须按同一门控刷新提示词')
|
|
2410
|
+
|
|
2411
|
+
const restore = indexCode.match(/path: '\/api\/soul\/history\/restore-persona'[\s\S]*?\n \}\)/)?.[0] ?? ''
|
|
2412
|
+
assert.match(restore, /sanitizePersonaName\(item\.name\)/, '恢复预设必须重新校验名字(记录是不可信输入)')
|
|
2413
|
+
assert.match(restore, /isBuiltinPersona\(personaName\)/, '恢复预设必须拒绝内置名')
|
|
2414
|
+
assert.match(restore, /hasOwn\(personas, personaName\)/, '同名已存在必须拒绝,不得静默覆盖用户后来的编辑')
|
|
2415
|
+
assert.match(restore, /skipped\.push\(\{ name: personaName, reason: 'exists' \}\)/, '跳过原因要如实回报,不能只报成功的')
|
|
2416
|
+
// 空白名这条分支只在记录被手改时才可达(normalizePersonaEvents 只看「非空且不超长」,
|
|
2417
|
+
// 而 ' ' 三条空格满足它、sanitizePersonaName 才拒绝)。恰恰因此必须回报 ——
|
|
2418
|
+
// 一条「恢复了 2 个」的成功消息里不说有 1 个没恢复,是最容易被忽略的那种漏报。
|
|
2419
|
+
assert.match(
|
|
2420
|
+
restore,
|
|
2421
|
+
/skipped\.push\(\{ name: item\.name, reason: 'invalid-name' \}\)/,
|
|
2422
|
+
'名字非法的条目也要报进 skipped(它在记录被手改时可达,不能只报能恢复的)'
|
|
2423
|
+
)
|
|
2424
|
+
assert.match(restore, /if \(restored\.length === 0\)[\s\S]*?throw httpError\(400/, '一个都没恢复时应回 400 而不是空成功')
|
|
2425
|
+
|
|
2426
|
+
const clear = indexCode.match(/path: '\/api\/soul\/history\/clear'[\s\S]*?\n \}\)/)?.[0] ?? ''
|
|
2427
|
+
// 清空若不入队,就会与并发的追加撞在一起 —— 最坏是「刚清完又冒出一条」。
|
|
2428
|
+
assert.match(clear, /enqueueConfigWrite\(async \(\) => \{/, '清空必须借配置写队列串行')
|
|
2429
|
+
assert.match(clear, /clearHistoryFile\(historyPath\(\)\)/, '清空应走 clearHistoryFile')
|
|
2430
|
+
assert.match(clear, /lastHistoryFailure = null/, '清空后应清掉上一次的写入失败告警')
|
|
2431
|
+
})
|
|
2432
|
+
|
|
2069
2433
|
check('重置失败不得被报成成功', () => {
|
|
2070
2434
|
const handler = clientCode.match(/const handleReset = async \(\) => \{[\s\S]*?\n \}/)
|
|
2071
2435
|
assert.ok(handler, '找不到客户端的 handleReset')
|
|
@@ -2733,6 +3097,527 @@ check('「存为预设」入口与「保存设置」并列,且点开才问名
|
|
|
2733
3097
|
}
|
|
2734
3098
|
})
|
|
2735
3099
|
|
|
3100
|
+
check('变更记录分组:字段标签覆盖全部审计字段,且只渲染服务端已倒序的记录', () => {
|
|
3101
|
+
// 这一组把服务端记录**逐字段**画给用户看。画的是 `before`/`after` 里的键名 ——
|
|
3102
|
+
// 键名是英文标识符,一旦某个键没有对应文案,界面上就会明晃晃写着 `trailWidth`。
|
|
3103
|
+
// 静态断言检查不了「新增配置字段时会不会漏」,所以这里做的是**覆盖率**断言:
|
|
3104
|
+
// 以 lib/config.mjs 的 DEFAULT_CONFIG 为唯一真源,比对客户端标签表。
|
|
3105
|
+
const configSource = readFileSync(new URL('../lib/config.mjs', import.meta.url), 'utf8')
|
|
3106
|
+
const defaultBlock = configSource.match(/export const DEFAULT_CONFIG = \{([\s\S]*?)\n\}/)?.[1] ?? ''
|
|
3107
|
+
assert.ok(defaultBlock !== '', '找不到 lib/config.mjs 的 DEFAULT_CONFIG')
|
|
3108
|
+
const auditKeys = []
|
|
3109
|
+
for (const line of defaultBlock.split('\n')) {
|
|
3110
|
+
const m = line.match(/^\s{2}([A-Za-z][A-Za-z0-9]*):/)
|
|
3111
|
+
if (m) auditKeys.push(m[1])
|
|
3112
|
+
}
|
|
3113
|
+
assert.ok(auditKeys.length >= 16, `DEFAULT_CONFIG 至少应有 16 个字段,实际解析出 ${auditKeys.length}`)
|
|
3114
|
+
|
|
3115
|
+
// 收尾的 `}` 是缩进的(客户端里的块都在函数体内),用顶格 `\n}` 会一路吞到远处
|
|
3116
|
+
// ——首版就是这么把 231 个键算进来的。
|
|
3117
|
+
const labelsBlock = clientCode.match(/const HISTORY_FIELD_LABELS = \{([\s\S]*?)\n\s*\}/)?.[1] ?? ''
|
|
3118
|
+
assert.ok(labelsBlock !== '', '找不到客户端 HISTORY_FIELD_LABELS')
|
|
3119
|
+
const labelKeys = [...labelsBlock.matchAll(/([A-Za-z][A-Za-z0-9]*):\s*'([^']+)'/g)].map((m) => m[1])
|
|
3120
|
+
for (const key of auditKeys) {
|
|
3121
|
+
assert.ok(labelKeys.includes(key),
|
|
3122
|
+
`审计字段 ${key} 没有历史标签 —— 变更记录里会把它显示成裸键名(新增配置字段时必须同步 HISTORY_FIELD_LABELS)`)
|
|
3123
|
+
}
|
|
3124
|
+
assert.equal(labelKeys.length, auditKeys.length,
|
|
3125
|
+
`HISTORY_FIELD_LABELS 有 ${labelKeys.length} 项、审计字段有 ${auditKeys.length} 个 —— 多出来的标签指向了不存在的字段`)
|
|
3126
|
+
|
|
3127
|
+
// 值标签只允许查**已存在的**文案键:拼错一个键名,词典回退会把键名本身画到屏幕上。
|
|
3128
|
+
for (const m of labelsBlock.matchAll(/'((?:field|trail|toggle)\.[A-Za-z]+)'/g)) {
|
|
3129
|
+
assert.ok(clientSource.includes(`'${m[1]}': `), `标签指向的文案键 ${m[1]} 不存在`)
|
|
3130
|
+
}
|
|
3131
|
+
// 枚举值必须与设置页共用同一份取值表:各写一份迟早会把 'casual' 显示成键名。
|
|
3132
|
+
const enumBlock = clientCode.match(/const HISTORY_ENUM_VALUES = \{([\s\S]*?)\n\s*\}/)?.[1] ?? ''
|
|
3133
|
+
assert.ok(enumBlock !== '', '找不到 HISTORY_ENUM_VALUES(枚举值的文案来源)')
|
|
3134
|
+
for (const [field, constName] of [
|
|
3135
|
+
['style', 'STYLE_VALUES'],
|
|
3136
|
+
['headingLists', 'TRAIT_VALUES'],
|
|
3137
|
+
['emoji', 'TRAIT_VALUES'],
|
|
3138
|
+
['tables', 'TRAIT_VALUES'],
|
|
3139
|
+
['replyLength', 'REPLY_LENGTH_VALUES'],
|
|
3140
|
+
['trailSpeed', 'TRAIL_SPEED_VALUES'],
|
|
3141
|
+
['trailWidth', 'TRAIL_WIDTH_VALUES']
|
|
3142
|
+
]) {
|
|
3143
|
+
assert.match(enumBlock, new RegExp(`\\b${field}:\\s*${constName}\\b`),
|
|
3144
|
+
`变更记录的 ${field} 取值表应复用 ${constName},不得另写一份`)
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
// 倒序是服务端的契约(toHistoryResponse 里 reverse 过一次)。客户端再排一次
|
|
3148
|
+
// 会把「最新在前」悄悄翻成「最旧在前」—— 用户看到的将是几年前的改动排第一行。
|
|
3149
|
+
// (0.8.1 双入口后,列表渲染在插件详情页组件 SoulHistorySection 内,
|
|
3150
|
+
// 缩进为 6 空格 —— 原设置页手风琴内是 8 空格,匹配缩进要跟着搬。)
|
|
3151
|
+
const listBlock = clientSource.match(/e\('ul', \{ className: 'soul-history-list' \},([\s\S]*?)\n\s{16}\)\)?,/)?.[1] ?? ''
|
|
3152
|
+
assert.ok(listBlock !== '', '找不到变更记录列表的渲染')
|
|
3153
|
+
assert.equal(/\.reverse\(\)|\.sort\(/.test(listBlock), false,
|
|
3154
|
+
'变更记录列表不得在客户端重排 —— 服务端已经按时间倒序返回')
|
|
3155
|
+
// 时间必须是相对时间:记录里存的是 ISO 字符串,直接画出来没人读得下去。
|
|
3156
|
+
assert.match(clientCode, /const historyTimeText = \(rawAt\)/, '缺少相对时间格式化函数')
|
|
3157
|
+
assert.match(clientCode, /!Number\.isFinite\(at\)\) return String\(rawAt\)/,
|
|
3158
|
+
'相对时间解析失败时应原样显示,而不是画出 NaN')
|
|
3159
|
+
// 时钟回拨(或服务端时间略快)会让差值变负 —— 「-3 分钟前」会被读成记录坏了。
|
|
3160
|
+
assert.match(clientCode, /if \(diff < 60 \* 1000\) return t\('history\.timeJustNow'\)/,
|
|
3161
|
+
'差值小于一分钟(含负数)都该显示「刚刚」')
|
|
3162
|
+
|
|
3163
|
+
// 0.8.0 打磨:折叠行必须带 label(应用了哪个预设)——不然「应用预设 · 自定义指令」
|
|
3164
|
+
// 答不了用户最常问的那句「我切到哪个人设了」。
|
|
3165
|
+
assert.match(clientCode, /entry\.label === 'string' && entry\.label !== ''/,
|
|
3166
|
+
'折叠摘要必须使用条目的 label(预设名),旧记录无 label 时原样省略')
|
|
3167
|
+
assert.ok(clientSource.includes(`'history.labelSuffix': ':{label}'`) ||
|
|
3168
|
+
clientSource.includes(`'history.labelSuffix': ': {label}'`),
|
|
3169
|
+
'缺少 history.labelSuffix 文案键(zh+en)')
|
|
3170
|
+
// 展开详情必须是「展开即全文」:原值块 + 新值块各自铺开,长文本换行显示。
|
|
3171
|
+
assert.match(clientCode, /soul-history-diff-value/, '展开详情缺少全文值块(.soul-history-diff-value)')
|
|
3172
|
+
assert.match(clientCode, /soul-history-diff-tag/, '值块前必须有「原值/新值」标签(.soul-history-diff-tag)')
|
|
3173
|
+
// 旧的 grid + 箭头布局已被竖排全文取代;残留的 from/to 箭头类名说明只改了一半。
|
|
3174
|
+
assert.equal(/soul-history-diff-from|soul-history-diff-to|soul-history-diff-arrow/.test(clientCode), false,
|
|
3175
|
+
'旧的 88px 网格省略布局残留(soul-history-diff-from/to/arrow)—— 全文块已取代它')
|
|
3176
|
+
})
|
|
3177
|
+
|
|
3178
|
+
// ---------- 工作区绑定(0.8.1-b)----------
|
|
3179
|
+
|
|
3180
|
+
check('工作区绑定:provider 每轮按会话 cwd 解析,且必须是同步的', () => {
|
|
3181
|
+
const opts = stripComments(extractObject(indexSource, 'spCtx.systemPrompt.section('))
|
|
3182
|
+
assert.ok(opts !== '', '找不到 spCtx.systemPrompt.section(...) 调用')
|
|
3183
|
+
|
|
3184
|
+
// section 的 text 回调**必须读装配上下文**。宿主侧的原话是
|
|
3185
|
+
// `text: typeof entry.text === "function" ? entry.text(context) : entry.text`
|
|
3186
|
+
// (dsh-system-prompt/lib/index.js),也就是每个 step 都会重新求值一次 ——
|
|
3187
|
+
// 这正是「改配置后下一轮生效」这条通道的机制。不看入参就退化成全局一份文本,
|
|
3188
|
+
// 工作区绑定会**静默失效**:设置页显示绑定成功,会话里拿到的却是全局人设。
|
|
3189
|
+
assert.match(
|
|
3190
|
+
opts,
|
|
3191
|
+
/text:\s*\(?\s*assembly\s*\)?\s*=>/,
|
|
3192
|
+
'section 的 text 必须是接受装配上下文的函数(不看 assembly 就无法按 cwd 解析绑定)'
|
|
3193
|
+
)
|
|
3194
|
+
assert.match(
|
|
3195
|
+
opts,
|
|
3196
|
+
/cwdOfAgent\(\s*assembly\s*&&\s*assembly\.agent\s*\)/,
|
|
3197
|
+
'text 回调应经 cwdOfAgent(assembly && assembly.agent) 取会话目录 —— 直接 assembly.agent 会在诊断装配(agent 缺席)时抛错'
|
|
3198
|
+
)
|
|
3199
|
+
assert.match(opts, /configForCwd\(/, 'text 回调必须经 configForCwd 合成该目录生效的配置')
|
|
3200
|
+
assert.equal(/async\s*\(?\s*assembly/.test(opts), false, 'text 回调不得是 async')
|
|
3201
|
+
assert.equal(/await /.test(opts), false, 'text 回调内不得出现 await')
|
|
3202
|
+
|
|
3203
|
+
// 宿主是**同步**调用 text 的(`section.text(context)` 位于一个非 async 的 map 回调里)。
|
|
3204
|
+
// 返回 Promise 会被当成 section 文本对象直接塞进提示词 —— 坏得无声无息。
|
|
3205
|
+
// 因此 realpath 这种异步 I/O 只能出现在**键**侧(loadBindings),不能出现在这里。
|
|
3206
|
+
assert.equal(
|
|
3207
|
+
/realpath|resolveDirIdentity/.test(opts),
|
|
3208
|
+
false,
|
|
3209
|
+
'text 回调里不得做 realpath(异步 I/O):它必须是同步的,身份解析只能在 loadBindings() 对键做一次'
|
|
3210
|
+
)
|
|
3211
|
+
assert.match(
|
|
3212
|
+
opts,
|
|
3213
|
+
/configCache \|\| DEFAULT_CONFIG/,
|
|
3214
|
+
'text 回调仍应读 configCache(保存后下一轮立即生效)'
|
|
3215
|
+
)
|
|
3216
|
+
})
|
|
3217
|
+
|
|
3218
|
+
check('工作区绑定:注入通道逐 agent 编译,不共用一份全局文本', () => {
|
|
3219
|
+
const matched = indexCode.match(/function injectPromptToAllAgents\(ctx, config\)[\s\S]*?\n\}/)
|
|
3220
|
+
assert.ok(matched, 'index.mjs 中找不到 injectPromptToAllAgents')
|
|
3221
|
+
const body = matched[0]
|
|
3222
|
+
|
|
3223
|
+
// 草案 §6.3:配置不再全局唯一之后,算一份文本发给所有人就是**把 A 项目的人设
|
|
3224
|
+
// 注入 B 项目的会话** —— 比不注入更糟(不注入时它至少还是全局配置,与用户以为的一致)。
|
|
3225
|
+
const loopAt = body.indexOf('for (const agent of listed)')
|
|
3226
|
+
assert.ok(loopAt !== -1, '找不到遍历活动会话的循环')
|
|
3227
|
+
const loop = body.slice(loopAt)
|
|
3228
|
+
assert.match(
|
|
3229
|
+
loop,
|
|
3230
|
+
/configForCwd\(\s*config\s*,\s*cwdOfAgent\(\s*agent\s*\)\s*\)/,
|
|
3231
|
+
'编译必须发生在循环体内、按该 agent 的 cwd 解析 —— 循环外算一份就和绑定功能互相抵消'
|
|
3232
|
+
)
|
|
3233
|
+
// 反证:循环外不得再有 compilePrompt(否则那份文本会被复用给所有会话)
|
|
3234
|
+
assert.equal(
|
|
3235
|
+
/const prompt = compilePrompt\(config\)/.test(body.slice(0, loopAt)),
|
|
3236
|
+
false,
|
|
3237
|
+
'循环外不得预先编译一份全局文本'
|
|
3238
|
+
)
|
|
3239
|
+
// 逐 agent 编译不得以破坏可观测性为代价(与上面那条断言同一组契约)
|
|
3240
|
+
assert.ok(/delivered\+\+/.test(body) && /failed\+\+/.test(body), '仍应如实统计成功与失败数')
|
|
3241
|
+
assert.match(
|
|
3242
|
+
body,
|
|
3243
|
+
/configForCwd\(config, cwdOfAgent\(agent\)\)[\s\S]{0,120}?const prompt = compilePrompt\(perAgent\)/,
|
|
3244
|
+
'应当先用该会话的 cwd 合成配置、再编译(顺序反了等于还是全局文本)'
|
|
3245
|
+
)
|
|
3246
|
+
})
|
|
3247
|
+
|
|
3248
|
+
check('工作区绑定:合成配置只覆盖人设字段,全局总开关不被覆盖', () => {
|
|
3249
|
+
const fn = stripComments(extractObject(indexSource, 'function configForCwd'))
|
|
3250
|
+
assert.ok(fn !== '', '找不到 configForCwd 函数体')
|
|
3251
|
+
assert.match(fn, /resolveBindingForCwd\(/, '应先按目录解析绑定')
|
|
3252
|
+
assert.match(
|
|
3253
|
+
fn,
|
|
3254
|
+
/pickPersonaValues\(persona\)/,
|
|
3255
|
+
'必须用 pickPersonaValues 走白名单 —— 直接展开条目会把磁盘上残留的「关于你」写回活动配置'
|
|
3256
|
+
)
|
|
3257
|
+
assert.match(fn, /sanitizeConfig\(/, '合成的补丁要经同一套校验(与 /personas/use 的取值语义逐字段一致)')
|
|
3258
|
+
// `enabled` 不属于 PERSONA_FIELDS:关掉插件后,绑定的目录也不该偷偷注入人设。
|
|
3259
|
+
assert.equal(
|
|
3260
|
+
/\benabled\b/.test(fn),
|
|
3261
|
+
false,
|
|
3262
|
+
'configForCwd 不得触碰 enabled —— 全局总开关必须始终说话'
|
|
3263
|
+
)
|
|
3264
|
+
const cwdOf = stripComments(extractObject(indexSource, 'function cwdOfAgent'))
|
|
3265
|
+
assert.ok(cwdOf !== '', '找不到 cwdOfAgent 函数体')
|
|
3266
|
+
assert.match(
|
|
3267
|
+
cwdOf,
|
|
3268
|
+
/agent\?\.session\?\.header\?\.cwd/,
|
|
3269
|
+
'cwdOfAgent 必须用可选链容忍 undefined —— 宿主在诊断装配时不传 agent(absent on diagnostics)'
|
|
3270
|
+
)
|
|
3271
|
+
assert.match(
|
|
3272
|
+
cwdOf,
|
|
3273
|
+
/typeof cwd === 'string' && cwd\.trim\(\) !== ''/,
|
|
3274
|
+
'空串 / 非字符串的 cwd 必须当成「没有会话信息」,否则 normalizeDirPath 会拿它去匹配'
|
|
3275
|
+
)
|
|
3276
|
+
})
|
|
3277
|
+
|
|
3278
|
+
check('工作区绑定:读盘只在装配路径之外,且失败不阻塞启动', () => {
|
|
3279
|
+
// 绑定表要单独缓存的原因:两个消费者都在**每一轮装配**上(text provider 每轮一次、
|
|
3280
|
+
// 注入每次保存一次)。读盘 + 最多 200 次 realpath 放进这条路径不可接受。
|
|
3281
|
+
const load = stripComments(extractObject(indexSource, 'async function loadBindings'))
|
|
3282
|
+
assert.ok(load !== '', '找不到 loadBindings 函数体')
|
|
3283
|
+
assert.match(load, /readBindingsFile\(/, '应经 lib/bindings.mjs 读取(损坏 / 版本的判定在那里)')
|
|
3284
|
+
assert.match(load, /resolveDirIdentity\(/, '应把每条键解析成身份形态(解 Junction / 符号链接)')
|
|
3285
|
+
assert.equal(/throw /.test(load), false, 'loadBindings 不得抛错:绑定是可选增强,读不出来不该让装配流程崩掉')
|
|
3286
|
+
// 失败时不写缓存(与 loadConfig 同款:用户改对文件后下次加载自动恢复),
|
|
3287
|
+
// 但必须把比较表清空 —— 宁可暂时不生效,也不能拿旧表继续命中。
|
|
3288
|
+
assert.match(
|
|
3289
|
+
load,
|
|
3290
|
+
/if \(failure\) \{[\s\S]*?bindingsCompareTable = \{\}[\s\S]*?bindingsDisplayOf = new Map\(\)[\s\S]*?return /,
|
|
3291
|
+
'加载失败时必须清空比较表并提前返回(不得留着旧表继续命中)'
|
|
3292
|
+
)
|
|
3293
|
+
const failBlock = load.match(/if \(failure\) \{[\s\S]*?\n \}/)?.[0] ?? ''
|
|
3294
|
+
assert.equal(
|
|
3295
|
+
/bindingsCache\s*=/.test(failBlock),
|
|
3296
|
+
false,
|
|
3297
|
+
'失败分支不得写 bindingsCache:缓存了空表,用户修好文件后不重启就恢复不了'
|
|
3298
|
+
)
|
|
3299
|
+
|
|
3300
|
+
// 身份键不得覆盖展示键:展示键是用户亲手写下的那个名字,优先级更高;
|
|
3301
|
+
// 被挡住的身份键只是少一条别名,判定方向安全。
|
|
3302
|
+
assert.match(
|
|
3303
|
+
load,
|
|
3304
|
+
/if \(hasOwn\(compareTable, identity\)\) continue/,
|
|
3305
|
+
'身份键撞上另一条展示键时应跳过 —— 覆盖会让 UI 显示用户没写过的目录名'
|
|
3306
|
+
)
|
|
3307
|
+
assert.match(
|
|
3308
|
+
load,
|
|
3309
|
+
/compareKey\(identity\) === compareKey\(dir\)\) continue/,
|
|
3310
|
+
'身份键与展示键相同时不应另占一个键位(否则 shadowed 会出现重复项)'
|
|
3311
|
+
)
|
|
3312
|
+
|
|
3313
|
+
// 启动时读一次:必须在装配路径之外做完(text provider 是同步的,不能在里面 await)。
|
|
3314
|
+
const applyBody = stripComments(extractObject(indexSource, 'export async function apply'))
|
|
3315
|
+
assert.ok(applyBody !== '', '找不到 apply 函数体')
|
|
3316
|
+
assert.match(applyBody, /await ensureBindings\(\)/, 'apply() 应在注册 section 之前先读一次绑定表')
|
|
3317
|
+
const sectionAt = applyBody.indexOf('registerSystemPrompt(')
|
|
3318
|
+
const bindingsAt = applyBody.indexOf('await ensureBindings()')
|
|
3319
|
+
assert.ok(bindingsAt !== -1 && sectionAt !== -1, 'apply() 里应同时有 ensureBindings 与 registerSystemPrompt')
|
|
3320
|
+
assert.ok(
|
|
3321
|
+
bindingsAt < sectionAt,
|
|
3322
|
+
'读绑定表必须排在注册 section 之前 —— 注册那一刻 provider 就可能被调用,此时表还没读出来'
|
|
3323
|
+
)
|
|
3324
|
+
})
|
|
3325
|
+
|
|
3326
|
+
check('工作区绑定:命中结果回报展示键,失效项去重后如实上报', () => {
|
|
3327
|
+
const fn = stripComments(extractObject(indexSource, 'function resolveBindingForCwd'))
|
|
3328
|
+
assert.ok(fn !== '', '找不到 resolveBindingForCwd 函数体')
|
|
3329
|
+
assert.match(fn, /normalizeDirPath\(cwd\)/, 'cwd 侧只做纯字符串规范化(realpath 已在键侧做过)')
|
|
3330
|
+
assert.match(
|
|
3331
|
+
fn,
|
|
3332
|
+
/bindingsDisplayOf\.get\(/,
|
|
3333
|
+
'命中后必须把身份键翻译回展示键 —— 用户不认得 realpath 吐出来的名字'
|
|
3334
|
+
)
|
|
3335
|
+
// 同一条绑定的展示键与身份键可能同时命中,不去重 UI 会把一条失效绑定画两遍。
|
|
3336
|
+
assert.match(fn, /const seen = new Set\(\)/, 'shadowed 需要按翻译后的 dir 去重')
|
|
3337
|
+
// ★ 只声明 Set 而不判 membership 等于**没有去重** —— 这两行必须成对存在。
|
|
3338
|
+
// 曾有变异把 `if (seen.has(dir)) continue` 整行删掉,而上面那条只认 `new Set()`
|
|
3339
|
+
// 的断言照样通过:断言看住了「有没有去重集合」,却没看住「有没有用它」。
|
|
3340
|
+
assert.match(
|
|
3341
|
+
fn,
|
|
3342
|
+
/if \(seen\.has\(dir\)\) continue/,
|
|
3343
|
+
'去重必须真的判 membership(`seen.add` 之前要先 `seen.has` 才 continue),否则 Set 形同虚设'
|
|
3344
|
+
)
|
|
3345
|
+
assert.match(
|
|
3346
|
+
fn,
|
|
3347
|
+
/shadowed\.push\(\{ \.\.\.item, dir \}\)/,
|
|
3348
|
+
'shadowed 项也要带上展示键(UI 要能说出「这条更具体的绑定为什么没生效」)'
|
|
3349
|
+
)
|
|
3350
|
+
assert.equal(
|
|
3351
|
+
/shadowed\.push\(\{ \.\.\.item, dir: item\.dir \}\)/.test(fn),
|
|
3352
|
+
false,
|
|
3353
|
+
'shadowed 不得保留原始键 —— 身份键会直接显示给用户'
|
|
3354
|
+
)
|
|
3355
|
+
})
|
|
3356
|
+
|
|
3357
|
+
check('工作区绑定四条契约:同一路径只注册一次、方法分派、队列内读盘、删空不刷新', () => {
|
|
3358
|
+
// ---------- ① 同一 (kind, path) 只能注册一次 ----------
|
|
3359
|
+
//
|
|
3360
|
+
// 宿主 dsh-host-webserver 的 register() 对重复 (kind, path) **直接抛错**:
|
|
3361
|
+
// if (table.has(route.path)) throw new Error(`webserver: duplicate ${route.kind} route "${route.path}"`)
|
|
3362
|
+
// 而注册发生在 ctx.inject 回调里 ⇒ 一次重复注册不是「后一个生效」,是**整个插件
|
|
3363
|
+
// 装配失败**(用户看到的是插件加载不出来,与控制台里那句重复路由毫无字面关联)。
|
|
3364
|
+
//
|
|
3365
|
+
// ⚠️ 这条是元断言:它必须自己解析出全部注册点,而不是逐个点名。曾经 GET 与 POST
|
|
3366
|
+
// 各写了一次 register('/api/soul/bindings'),本机假宿主用 Map.set 静默覆盖,
|
|
3367
|
+
// 一路绿到浏览器里才炸。
|
|
3368
|
+
const registrations = [...indexCode.matchAll(/webServer\.register\(\{\s*kind:\s*'(\w+)',\s*path:\s*'([^']+)'/g)].map(
|
|
3369
|
+
(m) => ({ kind: m[1], path: m[2] })
|
|
3370
|
+
)
|
|
3371
|
+
assert.ok(registrations.length >= 16, `注册点解析失败(只找到 ${registrations.length} 处,路由数量不该这么少)`)
|
|
3372
|
+
const seenRoutes = new Map()
|
|
3373
|
+
const duplicated = []
|
|
3374
|
+
for (const route of registrations) {
|
|
3375
|
+
const key = `${route.kind} ${route.path}`
|
|
3376
|
+
if (seenRoutes.has(key)) duplicated.push(key)
|
|
3377
|
+
seenRoutes.set(key, true)
|
|
3378
|
+
}
|
|
3379
|
+
assert.deepEqual(
|
|
3380
|
+
duplicated,
|
|
3381
|
+
[],
|
|
3382
|
+
`同一个 (kind, path) 被注册了多次:${JSON.stringify(duplicated)} —— 宿主会抛 ` +
|
|
3383
|
+
'webserver: duplicate,装配直接失败。GET/POST 必须在 handler 内按 req.method 分派。'
|
|
3384
|
+
)
|
|
3385
|
+
|
|
3386
|
+
// ---------- ② GET / POST 共用一次注册,且两条分支都在 ----------
|
|
3387
|
+
const bindingsRoutes = [...indexCode.matchAll(/path: '\/api\/soul\/bindings'[\s\S]*?\n \}\)/g)].map((m) => m[0])
|
|
3388
|
+
assert.equal(bindingsRoutes.length, 1, `/api/soul/bindings 应只注册一次,实际 ${bindingsRoutes.length} 次`)
|
|
3389
|
+
const bindings = bindingsRoutes[0]
|
|
3390
|
+
assert.match(bindings, /if \(req\.method === 'GET'\)/, 'GET 分支缺失:列表读不出来了')
|
|
3391
|
+
assert.match(bindings, /if \(req\.method !== 'POST'\)/, 'POST 之外的其它方法必须落到 405')
|
|
3392
|
+
assert.match(bindings, /send\(res, 405, \{ ok: false, error: 'Method not allowed' \}\)/, '方法不符应返回 405')
|
|
3393
|
+
// 顺序:GET 必须在 405 之前判掉,否则列表接口自己先被 405 拦住。
|
|
3394
|
+
assert.ok(
|
|
3395
|
+
bindings.indexOf("req.method === 'GET'") < bindings.indexOf("req.method !== 'POST'"),
|
|
3396
|
+
'GET 分支必须先于 405 判定 —— 反过来写会让读取接口自己收到 405'
|
|
3397
|
+
)
|
|
3398
|
+
|
|
3399
|
+
const del = indexCode.match(/path: '\/api\/soul\/bindings\/delete'[\s\S]*?\n \}\)/)?.[0] ?? ''
|
|
3400
|
+
assert.ok(del !== '', '缺少解绑路由')
|
|
3401
|
+
assert.match(del, /req\.method !== 'POST'/, '解绑必须显式校验 req.method === POST')
|
|
3402
|
+
assert.match(del, /send\(res, 405, \{ ok: false, error: 'Method not allowed' \}\)/, '解绑的方法不符应返回 405')
|
|
3403
|
+
|
|
3404
|
+
// ---------- ③ 写路径:准入在队列外,读盘在队列内 ----------
|
|
3405
|
+
assert.match(bindings, /bindingRefusal\(/, '写绑定前必须过 bindingRefusal(路径非法 / 过长 / 人设不存在)')
|
|
3406
|
+
assert.match(bindings, /normalizeBindingEntry\(/, '写入的键值必须规范化(否则同一目录存成多条)')
|
|
3407
|
+
// 队列外读会与并发写形成窗口,后写覆盖前写。绑定表是**整表替换**式的读改写,
|
|
3408
|
+
// 这个窗口不是理论问题:设置页连点两次就是两次并发 POST。
|
|
3409
|
+
assert.match(
|
|
3410
|
+
bindings,
|
|
3411
|
+
/await enqueueBindingsWrite\(async \(\) => \{[\s\S]*?readBindingsFile\(bindingsPath\(\)\)/,
|
|
3412
|
+
'读盘必须在 enqueueBindingsWrite 队列**内**:队列外读会有窗口,后写覆盖前写'
|
|
3413
|
+
)
|
|
3414
|
+
assert.equal(
|
|
3415
|
+
/const read = await readBindingsFile\(bindingsPath\(\)\)[\s\S]{0,400}?await enqueueBindingsWrite/.test(bindings),
|
|
3416
|
+
false,
|
|
3417
|
+
'读盘不得在入队之前 —— 这是竞态窗口的另一种写法'
|
|
3418
|
+
)
|
|
3419
|
+
assert.match(bindings, /const isNew = !hasOwn\(table, entry\.dir\)/, '改绑与新增必须分开判定')
|
|
3420
|
+
// 满表时改绑仍应可用:否则用户只能先删一条再改,而删除是不可逆的。
|
|
3421
|
+
assert.match(
|
|
3422
|
+
bindings,
|
|
3423
|
+
/if \(isNew && Object\.keys\(table\)\.length >= MAX_BINDINGS\)/,
|
|
3424
|
+
'只有真新增才受上限约束 —— 满表后连改绑都做不了会让用户只能先删再绑'
|
|
3425
|
+
)
|
|
3426
|
+
|
|
3427
|
+
// ---------- ④ 删除:按比较键命中,删空不刷新 ----------
|
|
3428
|
+
assert.match(del, /normalizeDirPath\(parsed\.body && parsed\.body\.dir\)/, '解绑目标要规范化')
|
|
3429
|
+
// 配置里存的可能是另一种写法(分隔符 / 尾分隔符 / 盘符大小写),拿原串直接
|
|
3430
|
+
// delete 会「点了没反应」—— 用户看到列表里那一行,点下去什么都没发生。
|
|
3431
|
+
assert.match(
|
|
3432
|
+
del,
|
|
3433
|
+
/Object\.keys\(table\)\.find\(dir => compareKey\(dir\) === compareKey\(target\)\)/,
|
|
3434
|
+
'必须按比较键找目标:拿原串 delete 会「点了没反应」'
|
|
3435
|
+
)
|
|
3436
|
+
assert.equal(/delete table\[target\]/.test(del), false, '不得用原串直接 delete')
|
|
3437
|
+
// 一次空解绑不该向所有活动会话注入快照:那是一条「配置已更新」的假消息。
|
|
3438
|
+
assert.match(
|
|
3439
|
+
del,
|
|
3440
|
+
/if \(removed\) await afterBindingsWrite\(config\)/,
|
|
3441
|
+
'没删掉任何东西时不得刷新提示词(空解绑会向所有会话注入一条假更新)'
|
|
3442
|
+
)
|
|
3443
|
+
|
|
3444
|
+
// ---------- ⑤ 写后必须自己刷新:第三类会改变提示词的事实 ----------
|
|
3445
|
+
for (const [name, source] of [['新增/改绑', bindings], ['解绑', del]]) {
|
|
3446
|
+
assert.match(source, /afterBindingsWrite\(config\)/, `${name} 写完后必须触发刷新`)
|
|
3447
|
+
}
|
|
3448
|
+
const after = extractObject(indexSource, 'const afterBindingsWrite')
|
|
3449
|
+
assert.ok(after !== '', '找不到 afterBindingsWrite')
|
|
3450
|
+
// 绑定表改了但内存缓存没重载,刷新出去的仍是旧解析结果 —— 表现是「保存了却不生效」。
|
|
3451
|
+
assert.match(after, /await loadBindings\(\)/, '刷新前必须重载绑定表,否则刷出去的还是旧解析结果')
|
|
3452
|
+
assert.match(after, /refreshPromptAndInject\(ctx, fresh\)/, '绑定变更必须自己刷新提示词')
|
|
3453
|
+
// 绑定写不能借配置写队列:两套数据因果无关,共用队列只会把延迟叠在一起(§4.3)。
|
|
3454
|
+
assert.match(indexCode, /let bindingsWriteQueue = Promise\.resolve\(\)/, '绑定写应有独立的队列实例')
|
|
3455
|
+
assert.equal(
|
|
3456
|
+
/enqueueConfigWrite[\s\S]{0,200}?writeBindingsFile/.test(indexCode),
|
|
3457
|
+
false,
|
|
3458
|
+
'绑定写不得走配置写队列'
|
|
3459
|
+
)
|
|
3460
|
+
})
|
|
3461
|
+
|
|
3462
|
+
check('工作区绑定只读视图:失效条目标注、sessions 走与会话相同的解析路径', () => {
|
|
3463
|
+
const view = extractObject(indexSource, 'async function bindingsView')
|
|
3464
|
+
assert.ok(view !== '', '找不到 bindingsView')
|
|
3465
|
+
// 失效引用**不自动删除**(用户可能只是临时删了预设、打算改回来),但必须显式标注 ——
|
|
3466
|
+
// 否则用户在目录里等一个永远不会来的人设。
|
|
3467
|
+
assert.match(view, /invalid\.push\(\{ dir, persona: name \}\)/, '失效引用要如实列出')
|
|
3468
|
+
assert.equal(/delete cache\.bindings\[/.test(view), false, '不得自动删除失效引用(设计 §4.2)')
|
|
3469
|
+
assert.match(view, /warning: bindingsLoadFailure/, '整表读不出来要作为 warning 回传,与「一条都没绑」区分')
|
|
3470
|
+
assert.match(view, /max: MAX_BINDINGS/, '要把条数上限回传(界面才能说清还有多少余量)')
|
|
3471
|
+
assert.match(view, /sessions: describeActiveSessions\(ctx, config\)/, '要带回活动会话的解析结果')
|
|
3472
|
+
|
|
3473
|
+
const sessions = extractObject(indexSource, 'function describeActiveSessions')
|
|
3474
|
+
assert.ok(sessions !== '', '找不到 describeActiveSessions')
|
|
3475
|
+
// 列不出活动会话不是错误:设置页只是少一段「当前用的谁」,仍能管理绑定表。
|
|
3476
|
+
// 两个分支(agents 服务缺席 / list() 抛错)都必须**返回空列表而不是抛出去** ——
|
|
3477
|
+
// 读侧一抛,整个 GET /api/soul/bindings 就 500,用户连绑定表都看不到了。
|
|
3478
|
+
assert.match(sessions, /agents\.list\(\)/, '活动会话来自 agents.list()')
|
|
3479
|
+
assert.equal(/throw /.test(sessions), false, 'describeActiveSessions 不得抛错:列不出会话只是少一段信息,不该让整个读接口失败')
|
|
3480
|
+
assert.match(sessions, /typeof agents\.list !== 'function'\) return out/, 'agents 服务不可用时应返回空列表')
|
|
3481
|
+
assert.match(sessions, /catch \{[\s\S]{0,200}?return out/, 'list() 抛错时同样返回空列表,且不得把异常冒泡给 HTTP 层')
|
|
3482
|
+
// 生效人设必须走与真实装配**同一条路径**,否则设置页显示的人设与实际注入的可能不一致 ——
|
|
3483
|
+
// 那正是最难查的一类 bug。
|
|
3484
|
+
assert.match(sessions, /configForCwd\(config, cwd\)\.config/, '生效人设必须走 configForCwd(与真实装配同一条路径)')
|
|
3485
|
+
// ★ 光「算了」还不够,还得**回报出去**:sessions[].persona 是设置页回答「我这个项目
|
|
3486
|
+
// 现在到底是谁」的唯一来源。曾有一条变异把这一项写成 null 而全部断言照绿 ——
|
|
3487
|
+
// 因为断言只看 binding 名,没看生效人设。
|
|
3488
|
+
assert.match(
|
|
3489
|
+
sessions,
|
|
3490
|
+
/persona: findActivePersonaName\(effective\)/,
|
|
3491
|
+
'sessions 必须回报生效人设(不是 null、也不是全局那一个):界面要答得出「这个会话现在是谁」'
|
|
3492
|
+
)
|
|
3493
|
+
// 只有**没有 cwd** 的分支才可以用全局配置算生效人设(那时确实没有绑定可解析)。
|
|
3494
|
+
// 有 cwd 的分支若也这么算,带绑定的会话会显示成全局那个名字,而注入的是绑定的人设。
|
|
3495
|
+
assert.equal(
|
|
3496
|
+
(sessions.match(/persona: findActivePersonaName\(config\)/g) || []).length,
|
|
3497
|
+
1,
|
|
3498
|
+
'用全局配置算生效人设只允许出现在「没有 cwd」那一个分支里'
|
|
3499
|
+
)
|
|
3500
|
+
// 没有 cwd 的会话要如实说出来,而不是静默省略:用户需要知道「这个会话没有目录」。
|
|
3501
|
+
assert.match(sessions, /cwd: null/, '没有 cwd 的会话要显式回报,不能静默跳过')
|
|
3502
|
+
// 「直接绑定」与「继承自上级」的判别必须在宿主做:客户端拿原始串比较时
|
|
3503
|
+
// Windows 上 `D:\项目A` 与 `d:\项目a` 会被判成不同目录。
|
|
3504
|
+
assert.match(
|
|
3505
|
+
sessions,
|
|
3506
|
+
/compareKey\(normalizeDirPath\(resolved\.dir\)\) === compareKey\(normalizeDirPath\(cwd\)\)/,
|
|
3507
|
+
'inherited 判别必须在宿主做,且用比较键(原始串比较在 Windows 上会误判大小写)'
|
|
3508
|
+
)
|
|
3509
|
+
assert.equal(
|
|
3510
|
+
/resolved\.dir === cwd/.test(sessions),
|
|
3511
|
+
false,
|
|
3512
|
+
'不得用原始串比较目录 —— 盘符大小写 / 分隔符差异会让「就是本目录」显示成「继承自上级」'
|
|
3513
|
+
)
|
|
3514
|
+
})
|
|
3515
|
+
|
|
3516
|
+
check('工作区绑定:纯函数层不依赖宿主,隐私边界不破', () => {
|
|
3517
|
+
const bindingSource = readFileSync(new URL('../lib/bindings.mjs', import.meta.url), 'utf8')
|
|
3518
|
+
const bindingCode = stripComments(bindingSource)
|
|
3519
|
+
assert.equal(
|
|
3520
|
+
/from 'cordis'|require\('cordis'\)/.test(bindingCode),
|
|
3521
|
+
false,
|
|
3522
|
+
'lib/bindings.mjs 不得依赖 cordis —— 它要能被脚本独立加载测试'
|
|
3523
|
+
)
|
|
3524
|
+
// 绑定表里存的是**本机目录结构**,属于用户隐私:它只用于选人设,内容绝不进提示词。
|
|
3525
|
+
assert.equal(
|
|
3526
|
+
/compilePrompt|systemPrompt|injectPromptToAllAgents/.test(bindingCode),
|
|
3527
|
+
false,
|
|
3528
|
+
'lib/bindings.mjs 不得触碰提示词装配 —— 目录路径不进 system prompt'
|
|
3529
|
+
)
|
|
3530
|
+
assert.equal(/console\./.test(bindingCode), false, '纯函数层不得打印')
|
|
3531
|
+
// 绑定文件的读写必须复用统一原语(原子写 / 损坏处理 / 路径推导),不得各自造一份。
|
|
3532
|
+
assert.match(bindingCode, /from '\.\/store\.mjs'/, '应复用 lib/store.mjs 的读写原语')
|
|
3533
|
+
assert.match(
|
|
3534
|
+
bindingCode,
|
|
3535
|
+
/await writeConfigFile\(filePath, serializeBindings\(bindings\)\)/,
|
|
3536
|
+
'writeBindingsFile 必须经 writeConfigFile 原子写'
|
|
3537
|
+
)
|
|
3538
|
+
// 绑定文件是配置文件的**兄弟**:由 configPath() 推导,测试把配置指到临时目录时绑定自动跟随。
|
|
3539
|
+
assert.match(indexCode, /function bindingsPath\(\)[\s\S]{0,120}?bindingsPathFor\(configPath\(\)\)/,
|
|
3540
|
+
'绑定文件路径应由 configPath() 推导(「配置在哪、绑定就在哪」是结构事实)')
|
|
3541
|
+
})
|
|
3542
|
+
|
|
3543
|
+
// ---------- 设置页「工作区绑定」:渲染层契约(0.8.1-d)----------
|
|
3544
|
+
//
|
|
3545
|
+
// 行为层在 verify-client C29–C33;这里守的是**源码文本才能看清**的那几条:
|
|
3546
|
+
// uiWorkspace 必须动态注入、组件引用的每个 bindings.* 键都要在两种语言里存在、
|
|
3547
|
+
// 每个用到的 class 都要有对应样式规则。缺任何一条都不会让任何断言变红 ——
|
|
3548
|
+
// 只有真机上会显示成原始 key 或裸 HTML。
|
|
3549
|
+
|
|
3550
|
+
check('工作区绑定:uiWorkspace 只走动态 inject,静态依赖表不得含它', () => {
|
|
3551
|
+
// 静态数组里多一个服务 = 缺它时**整个插件不激活**(设置页与人设卡一起消失)
|
|
3552
|
+
const staticInject = clientCode.match(/const inject = \[[^\]]*\]/)
|
|
3553
|
+
assert.ok(staticInject, '应能读到静态 inject 数组')
|
|
3554
|
+
assert.equal(
|
|
3555
|
+
/uiWorkspace/.test(staticInject[0]),
|
|
3556
|
+
false,
|
|
3557
|
+
`uiWorkspace 不得进静态 inject(缺服务会让整插件不激活):${staticInject[0]}`
|
|
3558
|
+
)
|
|
3559
|
+
assert.match(
|
|
3560
|
+
clientCode,
|
|
3561
|
+
/ctx\.inject\(\s*\['uiWorkspace'\]/,
|
|
3562
|
+
'必须用动态 ctx.inject([\'uiWorkspace\'], …) 获取目录点选服务'
|
|
3563
|
+
)
|
|
3564
|
+
// 只靠「按钮渲染时判空」是不够的:服务可能在按钮画出来**之后**被撤走
|
|
3565
|
+
// (插件卸载 / scope 卸载 / 物化顺序变化),此时点下去就是对 null 取属性,
|
|
3566
|
+
// 用户看到的是一个看不懂的 TypeError。handlePick 自己必须再守一次。
|
|
3567
|
+
assert.match(
|
|
3568
|
+
clientCode,
|
|
3569
|
+
/async function handlePick\(\)[\s\S]{0,200}if \(!directoryPicker\)/,
|
|
3570
|
+
'handlePick 必须自己判空(服务可能在渲染后才被撤走)'
|
|
3571
|
+
)
|
|
3572
|
+
})
|
|
3573
|
+
|
|
3574
|
+
check('工作区绑定:SoulBindings 引用的 bindings.* 键在 zh / en 里都存在', () => {
|
|
3575
|
+
const zhText = extractObject(clientSource, 'const zh = {')
|
|
3576
|
+
const enText = extractObject(clientSource, 'const en = {')
|
|
3577
|
+
const zhKeys = objectKeys(zhText)
|
|
3578
|
+
const enKeys = objectKeys(enText)
|
|
3579
|
+
// 从组件源码里抓它实际用到的键(而不是手抄一份清单 —— 手抄清单不会跟着组件变)
|
|
3580
|
+
const at = clientCode.indexOf('function SoulBindings(props)')
|
|
3581
|
+
assert.ok(at !== -1, '应存在 SoulBindings 组件')
|
|
3582
|
+
const body = clientCode.slice(at)
|
|
3583
|
+
const used = new Set([...body.matchAll(/t\('(bindings\.[A-Za-z]+)'/g)].map((m) => m[1]))
|
|
3584
|
+
assert.ok(used.size >= 20, `SoulBindings 里抓到的 bindings.* 键数偏少:${used.size}`)
|
|
3585
|
+
for (const key of used) {
|
|
3586
|
+
assert.ok(zhKeys.has(key), `zh 词典缺键 ${key}(真机上会直接显示原始 key)`)
|
|
3587
|
+
assert.ok(enKeys.has(key), `en 词典缺键 ${key}(英文界面下会漏出中文或原始 key)`)
|
|
3588
|
+
}
|
|
3589
|
+
// 带占位符的文案要两种语言都带同样的占位符,否则某种语言下 {} 会原样打出来
|
|
3590
|
+
for (const key of ['bindings.bound', 'bindings.unbound', 'bindings.count', 'bindings.moreSessions', 'bindings.invalid']) {
|
|
3591
|
+
const slot = (text) => {
|
|
3592
|
+
const line = text.split('\n').find((l) => l.trim().startsWith(`'${key}':`))
|
|
3593
|
+
assert.ok(line, `${key} 应存在`)
|
|
3594
|
+
return [...line.matchAll(/\{(\w+)\}/g)].map((m) => m[1]).sort()
|
|
3595
|
+
}
|
|
3596
|
+
assert.deepEqual(slot(zhText), slot(enText), `${key} 的占位符在 zh / en 必须一致`)
|
|
3597
|
+
}
|
|
3598
|
+
})
|
|
3599
|
+
|
|
3600
|
+
check('工作区绑定:组件用到的 class 都有 CSS 规则', () => {
|
|
3601
|
+
// 只抓「组件自己命名」的 soul-bindings-* / .soul-bindings,复用宿主式既有 class 的不算
|
|
3602
|
+
const at = clientCode.indexOf('function SoulBindings(props)')
|
|
3603
|
+
const body = clientCode.slice(at)
|
|
3604
|
+
const classes = new Set(
|
|
3605
|
+
[...body.matchAll(/className: '([^']+)'/g)]
|
|
3606
|
+
.flatMap((m) => m[1].split(/\s+/))
|
|
3607
|
+
.filter((c) => c === 'soul-bindings' || c.startsWith('soul-bindings-'))
|
|
3608
|
+
)
|
|
3609
|
+
assert.ok(classes.size >= 10, `抓到的绑定专属 class 数偏少:${classes.size}`)
|
|
3610
|
+
for (const cls of classes) {
|
|
3611
|
+
// 选择器可以出现在规则开头,也可以在一个**组合**选择器里(`.a,.b{`)——
|
|
3612
|
+
// 只认行首会把「与既有按钮共用一条描边规则」误判成「没有样式」
|
|
3613
|
+
assert.match(
|
|
3614
|
+
clientCode,
|
|
3615
|
+
new RegExp(`'\\.${cls}[{,:\\[ ]|,\\.${cls}[{,:\\[ ]`),
|
|
3616
|
+
`class .${cls} 没有样式规则(会渲染成裸 HTML)`
|
|
3617
|
+
)
|
|
3618
|
+
}
|
|
3619
|
+
})
|
|
3620
|
+
|
|
2736
3621
|
// 「断言数一致」与「每一项真的通过」是两回事:前者只防脚本与声明漂移,
|
|
2737
3622
|
// 后者才是校验本身。check() 里捕获的任何异常都必须让整体失败 —— 否则失败的
|
|
2738
3623
|
// check 只打印一行 ✗ 却仍退出 0,这正是「看起来绿、实际没守住」的形态。
|