dsh-memu-memory 1.3.6 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/AGENTS.md +2 -1
  2. package/CHANGELOG.md +87 -0
  3. package/README.md +7 -7
  4. package/README.zh.md +7 -7
  5. package/docs/client-conventions.md +4 -0
  6. package/docs/contributing.md +2 -2
  7. package/docs/memory-semantics.md +10 -7
  8. package/docs/troubleshooting.md +132 -7
  9. package/lib/binding-diagnostics.js +2 -2
  10. package/lib/client.js +296 -122
  11. package/lib/consolidation-ledger.js +6 -2
  12. package/lib/consolidation-worker.js +7 -0
  13. package/lib/inbound.js +24 -9
  14. package/lib/inject.js +379 -215
  15. package/lib/lexicon.js +5 -0
  16. package/lib/local-embedding-manager.js +4 -0
  17. package/lib/local-vector-service.js +65 -1
  18. package/lib/memory.js +711 -175
  19. package/lib/paths.js +24 -0
  20. package/lib/private.js +284 -127
  21. package/lib/rpc.js +144 -45
  22. package/lib/snippet.js +49 -28
  23. package/lib/state.js +185 -26
  24. package/lib/tools.js +150 -31
  25. package/lib/webui/standalone.css +28 -1
  26. package/lib/webui.js +25 -1
  27. package/lib/worker-pool.js +260 -26
  28. package/package.json +1 -1
  29. package/scripts/check-i18n.mjs +19 -0
  30. package/scripts/recall-bench.mjs +4 -4
  31. package/scripts/simulate-injection.mjs +3 -0
  32. package/tests/activity-wiring.test.mjs +52 -0
  33. package/tests/client-render.test.mjs +207 -0
  34. package/tests/consolidation-feed.test.mjs +26 -0
  35. package/tests/consolidation-fork.test.mjs +618 -33
  36. package/tests/consolidation-snapshot.test.mjs +2 -1
  37. package/tests/cross-library-recall.test.mjs +156 -0
  38. package/tests/cue.test.mjs +15 -2
  39. package/tests/hybrid-recall.test.mjs +488 -0
  40. package/tests/inbound.test.mjs +49 -4
  41. package/tests/inject-conflict.test.mjs +70 -9
  42. package/tests/inject-guide.test.mjs +13 -1
  43. package/tests/inject.test.mjs +10 -0
  44. package/tests/injection-budget.test.mjs +101 -0
  45. package/tests/injection-chain.test.mjs +37 -0
  46. package/tests/injection-rollback.test.mjs +172 -9
  47. package/tests/issue2-chain.test.mjs +169 -0
  48. package/tests/lexicon-inject.test.mjs +33 -5
  49. package/tests/lexicon-ui.test.mjs +99 -2
  50. package/tests/managed-memory.test.mjs +625 -1
  51. package/tests/media.test.mjs +1 -1
  52. package/tests/new-session-gate.test.mjs +280 -25
  53. package/tests/open-loop-date.test.mjs +11 -1
  54. package/tests/recall-intent.test.mjs +52 -3
  55. package/tests/simulate-injection.test.mjs +4 -4
  56. package/tests/snippet.test.mjs +33 -1
  57. package/tests/state.test.mjs +8 -3
  58. package/tests/tool-schema.test.mjs +52 -0
  59. package/tests/tools.test.mjs +394 -0
  60. package/tests/webui.test.mjs +16 -2
  61. package/tests/worker-lifecycle.test.mjs +419 -36
@@ -0,0 +1,169 @@
1
+ // 服务端注入与 RPC 集成;弹窗生命周期由 new-session-gate.test.mjs 独立验证。
2
+ import test from 'node:test'
3
+ import assert from 'node:assert/strict'
4
+ import fs from 'node:fs'
5
+ import os from 'node:os'
6
+ import path from 'node:path'
7
+
8
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-issue2-chain-'))
9
+ process.env.DSH_HOME = home
10
+ process.env.DSH_MEMU_MEMORY_HOME = home
11
+ process.env.DSH_MEMU_HOME = home
12
+
13
+ const state = await import('../lib/state.js')
14
+ const inject = await import('../lib/inject.js')
15
+ const rpc = await import('../lib/rpc.js')
16
+ const activity = await import('../lib/activity-log.js')
17
+ const { domainOf } = await import('../lib/memory.js')
18
+ const { loadPromptForDomain } = await import('../lib/prompts.js')
19
+
20
+ test.after(() => {
21
+ try { state.flushState() } catch { }
22
+ try { fs.rmSync(home, { recursive: true, force: true }) } catch { }
23
+ })
24
+
25
+ // 「03_管理」:报告人的工作区名,不含任何 RP 关键词——detectDomain 判定为 coding。
26
+ const ws = path.join(home, '03_管理')
27
+ const RP_WS = path.join(home, '角色扮演-花园')
28
+
29
+ // 走生产的 pre-step 钩子路径(injectForStep + ensureUnifiedGuide),不是直接调
30
+ // 内部函数:guide 的投放与领域选择发生在钩子层,跳过它就不是「实际链路」。
31
+ const entries = []
32
+ {
33
+ const ctx = {
34
+ logger: { warn() { } },
35
+ inject() { },
36
+ on(event, fn) { entries.push({ event, fn }); return () => { } },
37
+ }
38
+ inject.registerInjection(ctx)
39
+ }
40
+ const preStep = entries.find((entry) => entry.event === 'agent/pre-step')
41
+ assert.ok(preStep, 'registerInjection must register the agent/pre-step listener')
42
+
43
+ const userText = (sessionId, text) => ({
44
+ id: sessionId + '-user-1',
45
+ role: 'user',
46
+ source: { kind: 'user' },
47
+ content: [{ type: 'text', text }],
48
+ })
49
+ const sessionEvent = entries.find((entry) => entry.event === 'session/event')
50
+ assert.ok(sessionEvent, 'registerInjection must register the session/event listener')
51
+ const sessions = new Map()
52
+ async function firstTurn(sessionId, cwd, text = '帮我整理一下这个月的待办') {
53
+ const session = { header: { id: sessionId, cwd, agentPreset: null, origin: 'user' }, messages: [] }
54
+ sessions.set(sessionId, session)
55
+ const out = await preStep.fn({
56
+ agent: { session },
57
+ turn: 1,
58
+ step: 1,
59
+ signal: { aborted: false },
60
+ }, async () => ({ kind: 'enter', messages: [userText(sessionId, text)] }))
61
+ assert.ok(!state.getSessionBinding(sessionId)?.guideRulesKey, 'a proposed guide is not yet admitted')
62
+ for (const message of out.messages) {
63
+ assert.ok(message.id && message.role === 'user', 'Native DSL requires an identified UserMessage')
64
+ if (!session.messages.some(item => item.id === message.id)) session.messages.push(message)
65
+ await sessionEvent.fn(session, { type: 'user/message', data: message })
66
+ }
67
+ return out
68
+ }
69
+ // 领域以真实入史的 guide 正文及 binding 的接纳记录交叉验证,不把 proposed 日志当成功。
70
+ function guideDomainOf(sessionId) {
71
+ const binding = state.getSessionBinding(sessionId)
72
+ const guide = sessions.get(sessionId)?.messages.find(message =>
73
+ message.source?.kind === 'plugin:dsh-memu-memory' && message.content?.[0]?.text?.startsWith('<memory-guide'))
74
+ assert.ok(guide, 'the guide must exist in host history')
75
+ assert.ok(binding.acceptedMemoryMessages?.some(record => record.messageId === guide.id && record.admittedAt),
76
+ 'the actual guide must have an admitted binding record')
77
+ const text = guide.content[0].text
78
+ assert.equal(/^<memory-guide rules="([^"]+)"/.exec(text)?.[1], binding.guideRulesKey)
79
+ const domain = domainOf(binding)
80
+ const settings = state.getSettings()
81
+ const rendered = loadPromptForDomain('system-guide', {
82
+ mustRememberCap: settings.mustRememberCap,
83
+ unpinBudget: settings.unpinBudgetPerSession ?? 2,
84
+ shortTermTtlHours: settings.shortTermTtlHours ?? 72,
85
+ anchorTokens: settings.anchorTokens,
86
+ turnTokens: settings.turnTokens,
87
+ }, domain).text.trim()
88
+ assert.ok(text.includes(rendered), 'the admitted guide content must match the binding domain')
89
+ return domain
90
+ }
91
+
92
+ test('新建会话:空白、无绑定,弹窗有权询问', async () => {
93
+ const st = await rpc.dispatch({}, 'getSessionPromptState', { sessionId: 'issue2-s1', cwd: ws })
94
+ assert.equal(st.binding, null, 'a fresh blank session must arrive unbound')
95
+ assert.equal(st.shouldPrompt, true)
96
+ assert.equal(st.settings.newSessionPromptDismissed, false)
97
+ })
98
+
99
+ test('重复只读检查不会创建会话绑定', async () => {
100
+ const st = await rpc.dispatch({}, 'getSessionPromptState', { sessionId: 'issue2-s1', cwd: ws })
101
+ assert.equal(st.binding, null, '稍后 must not write anything server-side')
102
+ })
103
+
104
+ test('首条消息:注入路径隐式建绑定,领域跟随 detectDomain 而不是硬编码 rp', async () => {
105
+ const out = await firstTurn('issue2-s1', ws)
106
+ assert.ok(Array.isArray(out.messages) && out.messages.length, 'the first turn injected nothing')
107
+ assert.ok(out.messages.some((message) => (message.content || []).some((part) => part?.type === 'text' && String(part.text).startsWith('<memory-guide'))),
108
+ 'the rules message was not delivered on the first turn')
109
+ const binding = state.getSessionBinding('issue2-s1')
110
+ assert.ok(binding, 'the injection path did not bind the session')
111
+ assert.equal(binding.domainOverride, 'coding',
112
+ 'an unasked conversation in 03_管理 must not start on the RP track (issue #2)')
113
+ assert.equal(binding.domainSource, 'auto', 'nobody answered yet: the binding stays implicit')
114
+ assert.equal(guideDomainOf('issue2-s1'), 'coding', 'the injected guide variant must match the binding domain')
115
+ })
116
+
117
+ test('隐式绑定可读取,显式领域确认写入 chosen 凭据', async () => {
118
+ const st = await rpc.dispatch({}, 'getSessionPromptState', { sessionId: 'issue2-s1', cwd: ws })
119
+ assert.equal(st.binding.domainSource, 'auto', 'the binding must still look implicit')
120
+ await rpc.dispatch({}, 'initSession', { sessionId: 'issue2-s1', cwd: ws, createLibrary: true })
121
+ await rpc.dispatch({}, 'setSessionScope', { sessionId: 'issue2-s1', useLibrary: true, useGlobal: true })
122
+ await rpc.dispatch({}, 'setDomain', { sessionId: 'issue2-s1', domain: 'coding' })
123
+ const answered = state.getSessionBinding('issue2-s1')
124
+ assert.equal(answered.domainOverride, 'coding')
125
+ assert.equal(answered.domainSource, 'chosen', 'an explicit answer must leave the chosen marker')
126
+
127
+ const after = await rpc.dispatch({}, 'getSessionPromptState', { sessionId: 'issue2-s1', cwd: ws })
128
+ assert.equal(after.binding.domainSource, 'chosen', 'the gate reads the chosen marker back')
129
+ })
130
+
131
+ test('RP 工作区名仍判为 rp:detectDomain 的判定口径没有松动', async () => {
132
+ await firstTurn('issue2-rp', RP_WS)
133
+ const binding = state.getSessionBinding('issue2-rp')
134
+ assert.equal(binding.domainOverride, 'rp', 'an RP-named folder must keep the RP track')
135
+ assert.equal(guideDomainOf('issue2-rp'), 'rp')
136
+ })
137
+
138
+ test('显式选择 general 也是真实选项,不会被 detectDomain 冲掉', async () => {
139
+ await firstTurn('issue2-general', ws)
140
+ await rpc.dispatch({}, 'setDomain', { sessionId: 'issue2-general', domain: 'general' })
141
+ const binding = state.getSessionBinding('issue2-general')
142
+ assert.equal(binding.domainOverride, 'general')
143
+ assert.equal(binding.domainSource, 'chosen')
144
+ // 重走 ensureSession(每一轮注入都会跑):显式答案必须存活。
145
+ const rebound = state.ensureSession('issue2-general', { cwd: ws, createLibrary: false })
146
+ assert.equal(rebound.domainOverride, 'general')
147
+ assert.equal(rebound.domainSource, 'chosen')
148
+ })
149
+
150
+ test('RPC 失败不再被当成已答,错误经 logClientEvent 落入活动日志', async () => {
151
+ const result = await rpc.dispatch({}, 'logClientEvent', {
152
+ sessionId: 'issue2-s1',
153
+ kind: 'error',
154
+ message: '新会话记忆询问检查失败,本轮跳过',
155
+ detail: 'Error: rpc failed: getSessionPromptState',
156
+ })
157
+ assert.ok(result.entry?.id, 'logClientEvent returned no entry')
158
+ const events = activity.listEvents({ sessionId: 'issue2-s1', limit: 10 })
159
+ assert.ok(events.some((event) => event.kind === 'error' && event.message.includes('新会话记忆询问检查失败')),
160
+ 'the client-side failure is missing from the activity log')
161
+ // 奇异输入也不抛错——日志失败不能雪上加霜。
162
+ const odd = await rpc.dispatch({}, 'logClientEvent', {})
163
+ assert.ok(odd.entry?.id)
164
+ })
165
+
166
+ test('bindingView 把 domainSource 带给客户端(WebUI 与侧栏的读取口径)', async () => {
167
+ const st = await rpc.dispatch({}, 'getState', { sessionId: 'issue2-s1' })
168
+ assert.equal(st.binding.domainSource, 'chosen')
169
+ })
@@ -5,6 +5,8 @@ import os from 'node:os'
5
5
  import path from 'node:path'
6
6
 
7
7
  const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-lexicon-inject-'))
8
+ process.env.DSH_HOME = home
9
+ process.env.DSH_MEMU_MEMORY_HOME = home
8
10
  process.env.DSH_MEMU_HOME = home
9
11
 
10
12
  const paths = await import('../lib/paths.js')
@@ -38,6 +40,22 @@ function agentFor(binding) {
38
40
  return { session: { header: { id: binding.sessionId, cwd: path.join(home, 'cwd-' + binding.sessionId), agentPreset: 'rp', origin: 'user' } } }
39
41
  }
40
42
 
43
+ function admissionHost(agent) {
44
+ agent.session.messages = []
45
+ const handlers = new Map()
46
+ const ctx = { logger: { warn() {} }, on(event, fn) { handlers.set(event, fn); return () => {} } }
47
+ inject.registerInjection(ctx)
48
+ const sessionEvent = handlers.get('session/event')
49
+ assert.equal(typeof sessionEvent, 'function')
50
+ return { ctx, async admit(out) {
51
+ for (const message of out.messages) {
52
+ assert.ok(message.id && message.role === 'user', 'Native DSL requires an identified UserMessage')
53
+ if (!agent.session.messages.some(item => item.id === message.id)) agent.session.messages.push(message)
54
+ await sessionEvent(agent.session, { type: 'user/message', data: message })
55
+ }
56
+ } }
57
+ }
58
+
41
59
  function userMessage(id, text) {
42
60
  return { id, role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text }] }
43
61
  }
@@ -311,8 +329,9 @@ test('injectForStep appends the anchor lexicon summary and the B-channel hit blo
311
329
  batch: { type: inbound.INBOUND_TYPE, version: 1, producer: 'host.user', batchId: 'user:u1',
312
330
  messages: [{ id: 'u1', role: 'user', context: {}, content: [{ type: 'text', text: '这段太糖刀了' }] }] },
313
331
  })
332
+ const host = admissionHost(agent)
314
333
  const decision = { kind: 'enter', messages: [userMessage('u1', '这段太糖刀了')] }
315
- const out = await inject.injectForStep({ agent, decision, turn: 1, step: 1, signal: { aborted: false }, log: () => {}, ctx: null })
334
+ const out = await inject.injectForStep({ agent, decision, turn: 1, step: 1, signal: { aborted: false }, log: () => {}, ctx: host.ctx })
316
335
  const texts = pluginTexts(out)
317
336
  assert.equal(texts.length, 1, 'one plugin message carries anchor + lexicon')
318
337
  assert.match(texts[0], /===== memory anchor/)
@@ -322,9 +341,13 @@ test('injectForStep appends the anchor lexicon summary and the B-channel hit blo
322
341
  assert.match(texts[0], /<memory-lexicon>/, 'the B-channel hit block is in the same message')
323
342
  assert.match(texts[0], /糖刀」:表面发糖、实际虐心/)
324
343
 
325
- // 注入记账:candidate + injection 各一次,冷却已记。
326
344
  const lib = binding.libraryId
327
- const [entryId, entry] = Object.entries(lexicon.loadLexicon(lib).terms).find(([, t]) => t.normalizedTerm === '糖刀')
345
+ const [entryId, proposedEntry] = Object.entries(lexicon.loadLexicon(lib).terms).find(([, t]) => t.normalizedTerm === '糖刀')
346
+ assert.equal(proposedEntry.hitCounts.injection, 0, 'unadmitted output must not count as injection')
347
+ assert.equal(lexicon.lexiconOnCooldown(binding.sessionId, entryId), false, 'unadmitted output must not start cooldown')
348
+ await host.admit(out)
349
+ // 真实接纳后:candidate + injection 各一次,冷却已记。
350
+ const entry = lexicon.loadLexicon(lib).terms[entryId]
328
351
  assert.deepEqual(entry.hitCounts, { candidate: 1, injection: 1, query: 0 })
329
352
  assert.ok(lexicon.lexiconOnCooldown(binding.sessionId, entryId))
330
353
  })
@@ -332,13 +355,16 @@ test('injectForStep appends the anchor lexicon summary and the B-channel hit blo
332
355
  test('the second turn inside the cooldown window injects no lexicon block but still counts candidates', async () => {
333
356
  const binding = setupSession('e2e-cooldown', { terms: [{ term: '冷却验证词', kind: 'term', meaning: '含义' }] })
334
357
  const agent = agentFor(binding)
358
+ const host = admissionHost(agent)
335
359
  const send = async (id, text, turn) => {
336
360
  inbound.ingestInboundBatch({
337
361
  sessionId: binding.sessionId, session: agent.session, ctx: null,
338
362
  batch: { type: inbound.INBOUND_TYPE, version: 1, producer: 'host.user', batchId: `user:${id}`,
339
363
  messages: [{ id, role: 'user', context: {}, content: [{ type: 'text', text }] }] },
340
364
  })
341
- return inject.injectForStep({ agent, decision: { kind: 'enter', messages: [userMessage(id, text)] }, turn, step: 1, signal: { aborted: false }, log: () => {}, ctx: null })
365
+ const out = await inject.injectForStep({ agent, decision: { kind: 'enter', messages: [userMessage(id, text)] }, turn, step: 1, signal: { aborted: false }, log: () => {}, ctx: host.ctx })
366
+ await host.admit(out)
367
+ return out
342
368
  }
343
369
  await send('c1', '关于冷却验证词', 1)
344
370
  const second = await send('c2', '再说一次冷却验证词', 2)
@@ -353,12 +379,14 @@ test('the second turn inside the cooldown window injects no lexicon block but st
353
379
  test('the B channel also rides the real inbound batch path', async () => {
354
380
  const binding = setupSession('e2e-bridge', { terms: [{ term: '糖刀', kind: 'meme', meaning: '表面发糖、实际虐心' }] })
355
381
  const agent = agentFor(binding)
382
+ const host = admissionHost(agent)
356
383
  inbound.ingestInboundBatch({
357
384
  sessionId: binding.sessionId, session: agent.session, ctx: null,
358
385
  batch: { type: inbound.INBOUND_TYPE, version: 1, producer: 'friend-app', batchId: 'bridge:1',
359
386
  messages: [{ id: 'bridge-1', role: 'user', context: {}, content: [{ type: 'text', text: '这段太糖刀了' }] }] },
360
387
  })
361
- const out = await inject.injectForStep({ agent, decision: { kind: 'enter', messages: [] }, turn: 1, step: 1, signal: { aborted: false }, log: () => {}, ctx: null })
388
+ const out = await inject.injectForStep({ agent, decision: { kind: 'enter', messages: [] }, turn: 1, step: 1, signal: { aborted: false }, log: () => {}, ctx: host.ctx })
389
+ await host.admit(out)
362
390
  const texts = pluginTexts(out)
363
391
  assert.equal(texts.length, 1)
364
392
  assert.match(texts[0], /<memory-lexicon>/)
@@ -517,6 +517,53 @@ test('export bundles a versioned lexicon member; import merges in place with tom
517
517
  assert.equal(legacy.lexicon, undefined)
518
518
  })
519
519
 
520
+ test('lexiconList libraryId=all merges the global lexicon and every private library, tagging each term with its book', async () => {
521
+ const aggA = state.createLibrary({ name: 'agg-a', cwd: path.join(home, 'agg-a') })
522
+ const aggB = state.createLibrary({ name: 'agg-b', cwd: path.join(home, 'agg-b') })
523
+ await dispatch({}, 'lexiconAdd', { libraryId: aggA.id, term: '甲书词', kind: 'slang', meaning: '来自甲库' })
524
+ await sleep(12)
525
+ await dispatch({}, 'lexiconAdd', { libraryId: aggB.id, term: '乙书词', kind: 'meme', meaning: '来自乙库' })
526
+ await sleep(12)
527
+ await dispatch({}, 'lexiconAdd', { libraryId: 'global', term: '全局词', kind: 'term', meaning: '来自全局词典' })
528
+ const all = await dispatch({}, 'lexiconList', { libraryId: 'all', pageSize: 200 })
529
+ assert.equal(all.libraryId, 'all')
530
+ assert.equal(all.scope, 'all')
531
+ const mine = all.items.filter((row) => ['甲书词', '乙书词', '全局词'].includes(row.term))
532
+ assert.equal(mine.length, 3, 'all three books contribute')
533
+ const byTerm = Object.fromEntries(mine.map((row) => [row.term, row]))
534
+ assert.equal(byTerm['甲书词'].libraryId, aggA.id)
535
+ assert.equal(byTerm['甲书词'].libraryName, 'agg-a')
536
+ assert.equal(byTerm['乙书词'].libraryId, aggB.id)
537
+ assert.equal(byTerm['全局词'].libraryId, 'global')
538
+ // 默认 active 页签:total 等于 active 计数(kind/搜索为空时筛选不额外收窄)。
539
+ assert.ok(all.total >= 3)
540
+ assert.equal(all.total, all.statusCounts.active)
541
+ // kind 筛选跨书生效:meme 命中乙书词,且结果全是 meme。
542
+ const memes = await dispatch({}, 'lexiconList', { libraryId: 'all', kind: 'meme', pageSize: 200 })
543
+ assert.ok(memes.items.some((row) => row.term === '乙书词'))
544
+ assert.ok(memes.items.every((row) => row.kind === 'meme'))
545
+ // 搜索跨书命中全局词。
546
+ const searched = await dispatch({}, 'lexiconList', { libraryId: 'all', q: '全局', pageSize: 200 })
547
+ assert.ok(searched.items.some((row) => row.term === '全局词'))
548
+ // 未知状态照样报错(与单库同一把尺)。
549
+ await assert.rejects(() => dispatch({}, 'lexiconList', { libraryId: 'all', status: 'pending' }), /unknown lexicon status/)
550
+ // 单库视图不受影响:仍只列自己的词,scope 仍是 private。
551
+ const single = await dispatch({}, 'lexiconList', { libraryId: aggA.id })
552
+ assert.ok(single.items.some((row) => row.term === '甲书词'))
553
+ assert.ok(!single.items.some((row) => row.term === '乙书词'))
554
+ assert.equal(single.scope, 'private')
555
+ })
556
+
557
+ test('lexiconList all skips an unreadable book and reports it instead of failing the whole aggregate', async () => {
558
+ const bad = state.createLibrary({ name: 'agg-bad', cwd: path.join(home, 'agg-bad') })
559
+ // 形状损坏的词典文件:readLexiconStrict 抛 LEXICON_CORRUPT,聚合应跳过并留痕。
560
+ fs.writeFileSync(path.join(dataDir(), 'lexicon', `${bad.id}.json`), '{ not valid json')
561
+ const all = await dispatch({}, 'lexiconList', { libraryId: 'all', pageSize: 200 })
562
+ assert.ok(Array.isArray(all.errors))
563
+ assert.ok(all.errors.some((x) => x.libraryId === bad.id), 'the corrupt book is reported')
564
+ assert.ok(all.items.length > 0, 'the rest of the aggregate still renders')
565
+ })
566
+
520
567
  test('the lexicon panel is a two-tab card flow with no review/pending surface; auto graph edges get a badge', () => {
521
568
  const src = fs.readFileSync(new URL('../lib/client.js', import.meta.url), 'utf8')
522
569
  const start = src.indexOf('const LEXICON_TAB_LABELS')
@@ -541,12 +588,17 @@ test('the lexicon panel is a two-tab card flow with no review/pending surface; a
541
588
  // 来源记忆跳转与命中三计数 tooltip 保留。
542
589
  assert.match(panel, /onFocusMemory/)
543
590
  assert.match(panel, /lexiconHitsDetail/)
544
- // 作用域选择器:全局词典是选择器里的第一项,不是一个私有库(与记忆墙节点图的
545
- // 「全局记忆」同一款做法);口径说明用设置页同一种 note 样式。
591
+ // 作用域选择器:「全部词典」聚合视图与全局词典都在顶部,都不是私有库(与记忆墙
592
+ // 节点图的「全局记忆」同一款做法);口径说明用设置页同一种 note 样式。
546
593
  assert.match(panel, /lexiconGlobalScope/)
547
594
  assert.match(panel, /lexiconGlobalHint/)
548
595
  assert.match(panel, /lexiconBrowseScopeHint/)
549
596
  assert.match(panel, /dshm-settings-note/)
597
+ // 「全部词典」选项:聚合浏览所有书,卡片标注来源,写操作按词条所属库路由。
598
+ assert.match(panel, /lexiconAllScope/)
599
+ assert.match(panel, /lexiconAllHint/)
600
+ assert.match(panel, /value: ALL_LEXICON_ID/)
601
+ assert.match(panel, /bookOf/)
550
602
  // 全局词条跳记忆墙时 libraryId 必须落回 null(全局页),不能把保留字当库 id。
551
603
  assert.match(panel, /isGlobalLib \? null : activeLib/)
552
604
  // 没有私有库时这一页照样可用:全局词典由用户手写,不依赖任何库。
@@ -556,6 +608,39 @@ test('the lexicon panel is a two-tab card flow with no review/pending surface; a
556
608
  assert.match(src, /t\('graphAutoLink'\)/)
557
609
  })
558
610
 
611
+ test('lexicon editing opens a right-side drawer (like the memory detail card), not an inline card form', () => {
612
+ const src = fs.readFileSync(new URL('../lib/client.js', import.meta.url), 'utf8')
613
+ const start = src.indexOf('const LEXICON_TAB_LABELS')
614
+ const end = src.indexOf('function SettingsPage')
615
+ const panel = src.slice(start, end)
616
+ // 编辑改由右侧抽屉承载:复用记忆详情抽屉的 backdrop + aside 结构,role=dialog 让
617
+ // 主题插件的不透明豁免臂认得它;卡片内不再向下展开内联表单。
618
+ assert.match(panel, /const editDrawer/)
619
+ assert.match(panel, /dshm-drawer-backdrop/)
620
+ assert.match(panel, /className: 'dshm-drawer'/)
621
+ assert.match(panel, /'aria-modal': 'true'/)
622
+ assert.match(panel, /closeEdit/)
623
+ assert.match(panel, /bookLabelOf/)
624
+ assert.ok(!/const editForm/.test(panel), 'the inline downward edit form must be gone')
625
+ // Escape 与点击 backdrop 都能关;保存/取消在抽屉底部。
626
+ assert.match(panel, /event.key === 'Escape'/)
627
+ assert.match(panel, /dshm-drawer-actions/)
628
+ // 记忆详情抽屉与词典编辑抽屉共用不透明背景:主题层色叠在 light-dark 实底上,
629
+ // 不再直接用半透明的 bg-layer-1(主题插件未接管时会透)。
630
+ assert.match(src, /\.dshm-drawer\{[^}]*background-color:#fff;background:linear-gradient\(var\(--dsw-alias-bg-layer-1,transparent\),var\(--dsw-alias-bg-layer-1,transparent\)\),light-dark\(#ffffff,#1b1d23\)/)
631
+ // 抽屉表单字段有作用域 CSS(两个抽屉共用),不是裸 span。
632
+ assert.match(src, /\.dshm-drawer-field\{/)
633
+ assert.match(src, /\.dshm-drawer-label\{/)
634
+ // 独立壳的抽屉覆盖也必须不透明:曾用半透明的 --dshm-panel-strong(.87/.84)做底板,
635
+ // 把嵌入壳的不透明修复整个盖掉,独立 WebUI 里抽屉照样透。改用实色 layer-1。
636
+ const standalone = fs.readFileSync(new URL('../lib/webui/standalone.css', import.meta.url), 'utf8')
637
+ const drawerBlock = /html\[data-standalone\] \.dshm-drawer \{[^}]*\}/.exec(standalone)?.[0] || ''
638
+ assert.ok(drawerBlock, 'the standalone drawer override must exist')
639
+ const bgDecl = /background:[^;]+;/.exec(drawerBlock)?.[0] || ''
640
+ assert.match(bgDecl, /var\(--dsw-alias-bg-layer-1/)
641
+ assert.ok(!/dshm-panel-strong/.test(bgDecl), 'the translucent panel-strong must not paint the drawer')
642
+ })
643
+
559
644
  // 全局词典的保留存储名由服务端定义、由前端复写一遍(client.js 是浏览器侧脚本,
560
645
  // 不能 import lib/)。两边漂移一次,前端就会把 `global` 当成一本不存在的私有库。
561
646
  test('the client and the server spell the global lexicon id the same way', () => {
@@ -568,3 +653,15 @@ test('the client and the server spell the global lexicon id the same way', () =>
568
653
  assert.ok(state.listLibraries().every((lib) => lib.id !== server), 'a library id may never be the reserved global id')
569
654
  })
570
655
 
656
+ // 「全部词典」聚合浏览的保留作用域名同样由服务端定义、前端复写;漂移会让前端把 'all'
657
+ // 当成一本不存在的私有库去写。
658
+ test('the client and the server spell the all-lexicon scope id the same way', () => {
659
+ const core = fs.readFileSync(new URL('../lib/lexicon.js', import.meta.url), 'utf8')
660
+ const client = fs.readFileSync(new URL('../lib/client.js', import.meta.url), 'utf8')
661
+ const server = /export const ALL_LEXICON_ID = '([^']+)'/.exec(core)?.[1]
662
+ const browser = /const ALL_LEXICON_ID = '([^']+)'/.exec(client)?.[1]
663
+ assert.ok(server, 'lib/lexicon.js must define ALL_LEXICON_ID')
664
+ assert.equal(browser, server)
665
+ assert.ok(state.listLibraries().every((lib) => lib.id !== server), 'a library id may never be the reserved all scope id')
666
+ })
667
+