dsh-memu-memory 1.2.1 → 1.2.3
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/AGENTS.md +3 -1
- package/README.md +23 -8
- package/README.zh.md +16 -6
- package/docs/client-conventions.md +1 -0
- package/docs/contributing.md +18 -4
- package/docs/emotion-model-spec.md +2 -2
- package/docs/env-and-commands.md +20 -0
- package/docs/host-api.md +22 -1
- package/docs/memory-semantics.md +30 -2
- package/docs/troubleshooting.md +76 -5
- package/lib/activity-log.js +1 -1
- package/lib/backup.js +24 -2
- package/lib/binding-diagnostics.js +2 -2
- package/lib/client.js +1511 -153
- package/lib/consolidation-worker.js +30 -8
- package/lib/curator-evidence.js +131 -0
- package/lib/curator-policy.js +81 -0
- package/lib/curator.js +1252 -91
- package/lib/cycle.js +12 -1
- package/lib/embedding-download.js +39 -4
- package/lib/engine.js +6 -3
- package/lib/freshness.js +97 -0
- package/lib/global-graph.js +282 -0
- package/lib/graph.js +295 -11
- package/lib/inbound.js +19 -0
- package/lib/inject.js +396 -25
- package/lib/lexicon.js +1115 -0
- package/lib/local-vector-service.js +8 -6
- package/lib/managed-runtime.js +33 -2
- package/lib/memory.js +365 -34
- package/lib/memu.js +2 -2
- package/lib/migrate.js +83 -2
- package/lib/private-links.js +587 -0
- package/lib/private.js +32 -3
- package/lib/prompts/global-curator.md +76 -19
- package/lib/prompts/private-consolidation.coding.md +4 -1
- package/lib/prompts/private-consolidation.md +3 -1
- package/lib/prompts/private-consolidation.mixed.md +3 -1
- package/lib/prompts/private-consolidation.rp.md +3 -1
- package/lib/prompts.js +1 -1
- package/lib/rpc.js +589 -10
- package/lib/salient-wake.js +402 -0
- package/lib/state.js +95 -1
- package/lib/text-retrieval.js +661 -0
- package/lib/text-similarity.js +36 -18
- package/lib/tools.js +168 -10
- package/lib/webui/standalone.css +17 -0
- package/lib/webui/standalone.js +2 -0
- package/package.json +1 -1
- package/scripts/acceptance-local.mjs +8 -5
- package/scripts/curator-pair-calibrate.mjs +259 -0
- package/scripts/drive-session.mjs +853 -0
- package/scripts/lexicon-calibrate.mjs +230 -0
- package/scripts/lexicon-dedupe.mjs +318 -0
- package/scripts/mock-llm.mjs +903 -0
- package/tests/absorb-review.test.mjs +353 -0
- package/tests/appendix-b2.test.mjs +2 -2
- package/tests/client-export.test.mjs +520 -0
- package/tests/client-render.test.mjs +32 -4
- package/tests/consolidation-fork.test.mjs +14 -12
- package/tests/consolidation-snapshot.test.mjs +39 -0
- package/tests/curator-absorb.test.mjs +443 -0
- package/tests/curator-calibrate.test.mjs +173 -0
- package/tests/curator-clusters.test.mjs +281 -0
- package/tests/curator-commit.test.mjs +52 -5
- package/tests/curator-evidence.test.mjs +102 -0
- package/tests/curator-link.test.mjs +316 -0
- package/tests/curator-pairing.test.mjs +252 -0
- package/tests/curator-probe.test.mjs +15 -6
- package/tests/curator-review-mode.test.mjs +314 -0
- package/tests/disclosure.test.mjs +2 -1
- package/tests/entity-merge.test.mjs +261 -0
- package/tests/fixtures/curator-pairs/labeled.v1.jsonl +90 -0
- package/tests/fixtures/lexicon-stub-hooks.mjs +15 -0
- package/tests/fixtures/lexicon-stub.mjs +284 -0
- package/tests/fixtures/mock-llm/reply.txt +1 -0
- package/tests/freshness.test.mjs +140 -0
- package/tests/graph-cascade.test.mjs +35 -2
- package/tests/graph.test.mjs +96 -0
- package/tests/help-page.test.mjs +415 -0
- package/tests/inbound.test.mjs +6 -3
- package/tests/inject-guide.test.mjs +28 -0
- package/tests/inject.test.mjs +157 -5
- package/tests/injection-chain.test.mjs +176 -0
- package/tests/lexicon-calibrate.test.mjs +48 -0
- package/tests/lexicon-dedupe.test.mjs +171 -0
- package/tests/lexicon-frequency.test.mjs +179 -0
- package/tests/lexicon-inject.test.mjs +410 -0
- package/tests/lexicon-perf.test.mjs +436 -0
- package/tests/lexicon-ui.test.mjs +547 -0
- package/tests/lexicon.test.mjs +922 -0
- package/tests/managed-runtime.test.mjs +32 -0
- package/tests/mock-llm.test.mjs +225 -0
- package/tests/open-loop-date.test.mjs +406 -0
- package/tests/private-links.test.mjs +445 -0
- package/tests/prompts.test.mjs +35 -0
- package/tests/recall-dedup.test.mjs +33 -4
- package/tests/salient-wake.test.mjs +362 -0
- package/tests/settings-plumbing.test.mjs +76 -0
- package/tests/sidebar.test.mjs +5 -1
- package/tests/stubs/dsh-schedule.js +105 -0
- package/tests/tool-schema.test.mjs +1 -1
- package/tests/tools.test.mjs +112 -3
- package/tests/webui-auth.test.mjs +352 -2
- package/tests/webui.test.mjs +188 -0
package/tests/inject.test.mjs
CHANGED
|
@@ -15,7 +15,7 @@ test.after(() => {
|
|
|
15
15
|
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
16
16
|
})
|
|
17
17
|
|
|
18
|
-
test('anchor
|
|
18
|
+
test('anchor shows a pinned memory as a title card, without its body', () => {
|
|
19
19
|
const cwd = path.join(home, 'project-anchor')
|
|
20
20
|
const binding = state.ensureSession('inject-anchor', { cwd, agentPreset: 'rp' })
|
|
21
21
|
const index = state.loadPrivateIndex(binding.libraryId)
|
|
@@ -32,11 +32,131 @@ test('anchor includes pinned private memory', () => {
|
|
|
32
32
|
}
|
|
33
33
|
state.savePrivateIndex(binding.libraryId, index)
|
|
34
34
|
const anchor = inject.buildAnchor(binding, { ...state.getSettings(), anchorTokens: 700 })
|
|
35
|
+
assert.ok(anchor.text.includes('[mem-test]'), 'the id is the handle the model quotes back')
|
|
35
36
|
assert.ok(anchor.text.includes('凉茶'))
|
|
36
|
-
assert.
|
|
37
|
+
assert.equal(anchor.text.includes('今天他走的时候没看我'), false, 'the anchor is a directory; the body stays out')
|
|
37
38
|
assert.ok(anchor.ids.includes('mem-test'))
|
|
38
39
|
})
|
|
39
40
|
|
|
41
|
+
test('the rule is a preface: before the private header, and it names the recall path', () => {
|
|
42
|
+
const cwd = path.join(home, 'project-rule')
|
|
43
|
+
const binding = state.ensureSession('inject-rule', { cwd, agentPreset: 'rp' })
|
|
44
|
+
const index = state.loadPrivateIndex(binding.libraryId)
|
|
45
|
+
index.entries['mem-rule'] = {
|
|
46
|
+
track: 'memory',
|
|
47
|
+
layer: 'must_remember',
|
|
48
|
+
title: '规则之下的记忆',
|
|
49
|
+
content: '这段正文不该出现在锚点里。',
|
|
50
|
+
status: 'active',
|
|
51
|
+
pinSource: 'user_explicit',
|
|
52
|
+
createdAt: new Date().toISOString(),
|
|
53
|
+
updatedAt: new Date().toISOString(),
|
|
54
|
+
}
|
|
55
|
+
state.savePrivateIndex(binding.libraryId, index)
|
|
56
|
+
const anchor = inject.buildAnchor(binding, { ...state.getSettings(), anchorTokens: 700 })
|
|
57
|
+
const headerAt = anchor.text.indexOf('===== memory anchor =====')
|
|
58
|
+
const ruleAt = anchor.text.indexOf('[rule]')
|
|
59
|
+
const privateAt = anchor.text.indexOf('[private ·')
|
|
60
|
+
assert.ok(headerAt >= 0, 'the anchor starts with its header')
|
|
61
|
+
assert.ok(anchor.text.startsWith('===== memory anchor =====\n\n[rule]'), 'the header and rule are separated by a blank line')
|
|
62
|
+
assert.ok(ruleAt > headerAt && privateAt > ruleAt, 'the rule is read before the memories it governs')
|
|
63
|
+
assert.match(anchor.text, /\[rule\][^\n]*\n\n\[private ·/, 'the rule and the private section are separated by a blank line')
|
|
64
|
+
assert.ok(anchor.text.indexOf('[mem-rule]') > privateAt)
|
|
65
|
+
assert.ok(anchor.text.includes('标题即目录'), 'the rule says the anchor is a table of contents')
|
|
66
|
+
assert.ok(anchor.text.includes('memu_private_recall 按标题关键词查'), 'and how to get the original text back')
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('an empty title falls back to the first 40 characters of the content', () => {
|
|
70
|
+
const cwd = path.join(home, 'project-fallback')
|
|
71
|
+
const binding = state.ensureSession('inject-fallback', { cwd, agentPreset: 'rp' })
|
|
72
|
+
const content = '他答应过要在雨停之前回来,可是到了第二天早上门口还是空的,只有那把伞靠在墙边。'
|
|
73
|
+
const now = new Date().toISOString()
|
|
74
|
+
const index = state.loadPrivateIndex(binding.libraryId)
|
|
75
|
+
index.entries['mem-fallback'] = {
|
|
76
|
+
track: 'memory',
|
|
77
|
+
layer: 'long_term',
|
|
78
|
+
content,
|
|
79
|
+
status: 'active',
|
|
80
|
+
createdAt: now,
|
|
81
|
+
updatedAt: now,
|
|
82
|
+
}
|
|
83
|
+
state.savePrivateIndex(binding.libraryId, index)
|
|
84
|
+
const anchor = inject.buildAnchor(binding, { ...state.getSettings(), anchorTokens: 700 })
|
|
85
|
+
assert.ok(anchor.text.includes('[mem-fallback]'), 'the core segment is a title card too')
|
|
86
|
+
assert.ok(anchor.text.includes(content.slice(0, 40)), 'a card with no title is still searchable by its opening words')
|
|
87
|
+
assert.equal(anchor.text.includes('(untitled)'), false, 'content exists, so the row is never anonymous')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('title-card lines keep source, disclosure, accuracy and the attachment count', () => {
|
|
91
|
+
const cwd = path.join(home, 'project-labels')
|
|
92
|
+
const binding = state.ensureSession('inject-labels', { cwd, agentPreset: 'rp' })
|
|
93
|
+
const now = new Date().toISOString()
|
|
94
|
+
const index = state.loadPrivateIndex(binding.libraryId)
|
|
95
|
+
index.entries['mem-labels'] = {
|
|
96
|
+
track: 'memory',
|
|
97
|
+
layer: 'must_remember',
|
|
98
|
+
title: '他编的那个故事',
|
|
99
|
+
content: '他说那是真的,但那只是他想出来的。',
|
|
100
|
+
source: 'imagined',
|
|
101
|
+
disclosure: 'never_say',
|
|
102
|
+
accuracy: 0.3,
|
|
103
|
+
confidence: 0.9,
|
|
104
|
+
attachments: [
|
|
105
|
+
{ sha256: 'a'.repeat(64) },
|
|
106
|
+
{ sha256: 'b'.repeat(64), caption: 'CAPTION-MUST-NOT-EXPAND' },
|
|
107
|
+
],
|
|
108
|
+
status: 'active',
|
|
109
|
+
pinSource: 'self',
|
|
110
|
+
createdAt: now,
|
|
111
|
+
updatedAt: now,
|
|
112
|
+
}
|
|
113
|
+
state.savePrivateIndex(binding.libraryId, index)
|
|
114
|
+
const anchor = inject.buildAnchor(binding, { ...state.getSettings(), anchorTokens: 700 })
|
|
115
|
+
const line = anchor.text.split('\n').find((row) => row.includes('[mem-labels]'))
|
|
116
|
+
assert.ok(line, 'the title card exists')
|
|
117
|
+
assert.ok(line.includes('source=imagined'), 'an imagined memory must not read as a fact')
|
|
118
|
+
assert.ok(line.includes('记得,但别直接说出口'), 'never_say survives on a title card')
|
|
119
|
+
assert.ok(line.includes('细节未必准'), 'accuracy still rides on the line')
|
|
120
|
+
assert.ok(line.includes('附件2'), 'attachments are counted')
|
|
121
|
+
assert.equal(anchor.text.includes('CAPTION-MUST-NOT-EXPAND'), false, 'captions are expanded only by the full renderer')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('with the same anchorTokens, title cards expose at least as many ids as full text', () => {
|
|
125
|
+
const cwd = path.join(home, 'project-compare')
|
|
126
|
+
const binding = state.ensureSession('inject-compare', { cwd, agentPreset: 'rp' })
|
|
127
|
+
const now = new Date().toISOString()
|
|
128
|
+
const index = state.loadPrivateIndex(binding.libraryId)
|
|
129
|
+
const items = []
|
|
130
|
+
const body = '这是一段用来占满预算的中文记忆正文,重复几遍让长度足够触发截断。'.repeat(3)
|
|
131
|
+
for (let i = 0; i < 16; i++) {
|
|
132
|
+
const id = `mem-cmp-${i}`
|
|
133
|
+
index.entries[id] = {
|
|
134
|
+
track: 'memory',
|
|
135
|
+
layer: 'must_remember',
|
|
136
|
+
title: `对比记忆 ${i}`,
|
|
137
|
+
content: `${body} BODY-${i}`,
|
|
138
|
+
status: 'active',
|
|
139
|
+
pinSource: 'self',
|
|
140
|
+
strength: 1 - i / 20,
|
|
141
|
+
createdAt: now,
|
|
142
|
+
updatedAt: new Date(Date.now() - i * 60000).toISOString(),
|
|
143
|
+
}
|
|
144
|
+
items.push([id, index.entries[id]])
|
|
145
|
+
}
|
|
146
|
+
state.savePrivateIndex(binding.libraryId, index)
|
|
147
|
+
const anchorTokens = 600
|
|
148
|
+
const anchor = inject.buildAnchor(binding, { ...state.getSettings(), anchorTokens })
|
|
149
|
+
// The old shape was the same head plus the body the card now omits. Pack it into the
|
|
150
|
+
// *whole* anchor budget (no section headers charged), which is more room than the
|
|
151
|
+
// full-text anchor ever actually got — so this comparison is deliberately unfair to
|
|
152
|
+
// the title cards.
|
|
153
|
+
const legacy = budget.packToBudget(items, anchorTokens, (entry, id) =>
|
|
154
|
+
`- [${id}] source=${entry.source || 'inferred'} · ${entry.title} · ${entry.layer || 'long_term'}`
|
|
155
|
+
+ (entry.content ? `\n ${entry.content}` : ''))
|
|
156
|
+
assert.ok(anchor.ids.length >= legacy.ids.length,
|
|
157
|
+
`title cards exposed ${anchor.ids.length} ids, full text ${legacy.ids.length}`)
|
|
158
|
+
})
|
|
159
|
+
|
|
40
160
|
test('pre-step inserts a plugin message before the user message', async () => {
|
|
41
161
|
const cwd = path.join(home, 'project-step')
|
|
42
162
|
const binding = state.ensureSession('inject-step', { cwd, agentPreset: 'coding' })
|
|
@@ -84,8 +204,10 @@ test('pre-step inserts a plugin message before the user message', async () => {
|
|
|
84
204
|
assert.equal(out.messages[1].source.kind, 'user')
|
|
85
205
|
|
|
86
206
|
const injected = out.messages[0].content[0].text
|
|
87
|
-
assert.ok(injected.includes('mem-recall'), 'per-turn recall must surface the matching entry')
|
|
88
|
-
assert.ok(injected.includes('
|
|
207
|
+
assert.ok(injected.includes('[mem-recall]'), 'per-turn recall must surface the matching entry by id')
|
|
208
|
+
assert.ok(injected.includes('Deploy convention'), 'the title is what reaches the prompt')
|
|
209
|
+
assert.equal(injected.includes('The deploy script lives in tools/deploy.ps1'), false,
|
|
210
|
+
'the turn block is a title card; the body stays behind memu_private_recall')
|
|
89
211
|
assert.ok(state.getSessionBinding('inject-step').injectedIds.includes('mem-recall'))
|
|
90
212
|
assert.equal(state.getSessionBinding('inject-step').lastRecallError, undefined)
|
|
91
213
|
|
|
@@ -93,6 +215,35 @@ test('pre-step inserts a plugin message before the user message', async () => {
|
|
|
93
215
|
assert.equal(skipped, decision)
|
|
94
216
|
})
|
|
95
217
|
|
|
218
|
+
test('buildRecall injects the title card, not the body', async () => {
|
|
219
|
+
const cwd = path.join(home, 'project-recall-title')
|
|
220
|
+
const binding = state.ensureSession('inject-recall-title', { cwd, agentPreset: 'coding' })
|
|
221
|
+
const now = new Date().toISOString()
|
|
222
|
+
const index = state.loadPrivateIndex(binding.libraryId)
|
|
223
|
+
index.entries['mem-recall-title'] = {
|
|
224
|
+
track: 'memory',
|
|
225
|
+
layer: 'long_term',
|
|
226
|
+
title: '部署脚本的位置',
|
|
227
|
+
content: 'RECALL-BODY-ONLY 部署脚本放在 tools/deploy.ps1,需要 staging token。',
|
|
228
|
+
status: 'active',
|
|
229
|
+
confidence: 0.9,
|
|
230
|
+
createdAt: now,
|
|
231
|
+
updatedAt: now,
|
|
232
|
+
}
|
|
233
|
+
state.savePrivateIndex(binding.libraryId, index)
|
|
234
|
+
const exec = { agent: { session: { header: { id: 'inject-recall-title', cwd, agentPreset: 'coding' } } } }
|
|
235
|
+
const recall = await inject.buildRecall(binding, '部署脚本的位置', 1,
|
|
236
|
+
{ ...state.getSettings(), relevanceThreshold: 0 }, exec, 'inject-recall-title')
|
|
237
|
+
assert.deepEqual(recall.errors, [])
|
|
238
|
+
assert.ok(recall.ids.includes('mem-recall-title'))
|
|
239
|
+
assert.ok(recall.text.includes('部署脚本的位置'), 'the title is the card')
|
|
240
|
+
assert.ok(recall.text.includes('[mem-recall-title]'), 'the id stays quotable')
|
|
241
|
+
assert.match(recall.text, /条目是标题卡/, 'the block tells the model how to use a title card')
|
|
242
|
+
assert.ok(recall.text.includes('memu_private_recall'), 'and which tool returns the body')
|
|
243
|
+
assert.equal(recall.text.includes('RECALL-BODY-ONLY'), false,
|
|
244
|
+
'memu_private_recall still returns full text; the turn block does not')
|
|
245
|
+
})
|
|
246
|
+
|
|
96
247
|
test('a failing recall is recorded instead of disappearing', async () => {
|
|
97
248
|
const cwd = path.join(home, 'project-recall-error')
|
|
98
249
|
const binding = state.ensureSession('inject-recall-error', { cwd, agentPreset: 'coding' })
|
|
@@ -215,7 +366,8 @@ test('per-turn mode reads the global layer every turn instead of on the cadence'
|
|
|
215
366
|
|
|
216
367
|
const perTurn2 = await inject.buildRecall(binding, query, 2, { ...base, autoInject: 'per-turn' }, exec)
|
|
217
368
|
assert.ok(perTurn2.ids.includes('mem-global-deploy'), 'per-turn recalls the global layer on a plain turn')
|
|
218
|
-
assert.ok(perTurn2.text.includes('
|
|
369
|
+
assert.ok(perTurn2.text.includes('Staging token rotation'), 'the global title card arrives')
|
|
370
|
+
assert.equal(perTurn2.text.includes('rotated every Monday'), false, 'but not its body')
|
|
219
371
|
|
|
220
372
|
const perTurn5 = await inject.buildRecall(binding, query, 5, { ...base, autoInject: 'per-turn' }, exec)
|
|
221
373
|
assert.ok(perTurn5.ids.includes('mem-global-deploy'), 'and again on the next turn, not only every Nth turn')
|
|
@@ -11,6 +11,7 @@ process.env.DSH_MEMU_HOME = home
|
|
|
11
11
|
const state = await import('../lib/state.js')
|
|
12
12
|
const memory = await import('../lib/memory.js')
|
|
13
13
|
const inject = await import('../lib/inject.js')
|
|
14
|
+
const budget = await import('../lib/budget.js')
|
|
14
15
|
|
|
15
16
|
test.after(() => {
|
|
16
17
|
try { state.flushState() } catch {}
|
|
@@ -189,3 +190,178 @@ test('the remember tool tells the model to resolve a relative day', () => {
|
|
|
189
190
|
assert.ok(source.includes('A relative day'), 'the tool description must mention relative days')
|
|
190
191
|
assert.ok(source.includes('turns one appointment into a yearly reminder'), 'and why it matters')
|
|
191
192
|
})
|
|
193
|
+
|
|
194
|
+
// ------------------------------------- D: freshness hint is conditional and bounded
|
|
195
|
+
// The hint is a footnote to a recall that already succeeded: it must appear once,
|
|
196
|
+
// after every closed <memory-context> block, and must never turn a normal turn into
|
|
197
|
+
// a hint-only or failed turn.
|
|
198
|
+
|
|
199
|
+
const DAY_MS = 86_400_000
|
|
200
|
+
|
|
201
|
+
function seedRecallEntry(binding, id, patch = {}) {
|
|
202
|
+
const now = new Date().toISOString()
|
|
203
|
+
const index = state.loadPrivateIndex(binding.libraryId)
|
|
204
|
+
index.entries[id] = {
|
|
205
|
+
track: 'memory',
|
|
206
|
+
layer: 'long_term',
|
|
207
|
+
title: '部署脚本的位置',
|
|
208
|
+
content: '部署脚本放在 tools/deploy.ps1,先跑 staging smoke。',
|
|
209
|
+
status: 'active',
|
|
210
|
+
confidence: 0.9,
|
|
211
|
+
createdAt: now,
|
|
212
|
+
updatedAt: now,
|
|
213
|
+
...patch,
|
|
214
|
+
}
|
|
215
|
+
state.savePrivateIndex(binding.libraryId, index)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function recallFor(sessionId, binding, query, patch = {}) {
|
|
219
|
+
const exec = { agent: { session: { header: { id: sessionId, agentPreset: 'coding' } } } }
|
|
220
|
+
return inject.buildRecall(binding, query, 1, {
|
|
221
|
+
...state.getSettings(),
|
|
222
|
+
relevanceThreshold: 0,
|
|
223
|
+
...patch,
|
|
224
|
+
}, exec, sessionId)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
test('a stale recalled memory appends exactly one freshness hint after the closed blocks', async () => {
|
|
228
|
+
const cwd = path.join(home, 'project-freshness-stale')
|
|
229
|
+
const binding = state.ensureSession('chain-freshness-stale', { cwd, agentPreset: 'coding' })
|
|
230
|
+
const old = new Date(Date.now() - 45 * DAY_MS).toISOString()
|
|
231
|
+
seedRecallEntry(binding, 'mem-fresh-deploy', { updatedAt: old })
|
|
232
|
+
seedRecallEntry(binding, 'mem-fresh-release', {
|
|
233
|
+
title: '发布流程的审批',
|
|
234
|
+
content: '发布流程需要提前一天审批。',
|
|
235
|
+
updatedAt: old,
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
const recall = await recallFor('chain-freshness-stale', binding, '部署脚本和发布流程')
|
|
239
|
+
assert.deepEqual(recall.errors, [])
|
|
240
|
+
assert.ok(recall.ids.includes('mem-fresh-deploy'))
|
|
241
|
+
assert.ok(recall.ids.includes('mem-fresh-release'))
|
|
242
|
+
assert.equal((recall.text.match(/<memory-freshness>/g) || []).length, 1, 'the hint appears exactly once')
|
|
243
|
+
assert.ok(recall.text.includes('上面有 2 条记忆带时间敏感信号'), 'N counts the unique stale entries')
|
|
244
|
+
assert.ok(recall.text.indexOf('<memory-freshness>') > recall.text.lastIndexOf('</memory-context>'),
|
|
245
|
+
'the hint follows the last closed context block')
|
|
246
|
+
assert.equal((recall.text.match(/<memory-context\b/g) || []).length, 1, 'recall still has one context block')
|
|
247
|
+
assert.equal((recall.text.match(/<\/memory-context>/g) || []).length, 1)
|
|
248
|
+
assert.ok(recall.text.endsWith('</memory-freshness>'))
|
|
249
|
+
|
|
250
|
+
// The age leg obeys freshnessDays: 45-day-old entries are fresh at 100 days.
|
|
251
|
+
const lenient = await recallFor('chain-freshness-stale', binding, '部署脚本和发布流程', { freshnessDays: 100 })
|
|
252
|
+
assert.ok(lenient.text.includes('</memory-context>'))
|
|
253
|
+
assert.equal(lenient.text.includes('<memory-freshness>'), false)
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
test('a fresh recalled memory adds no freshness hint', async () => {
|
|
257
|
+
const cwd = path.join(home, 'project-freshness-fresh')
|
|
258
|
+
const binding = state.ensureSession('chain-freshness-fresh', { cwd, agentPreset: 'coding' })
|
|
259
|
+
seedRecallEntry(binding, 'mem-fresh-now')
|
|
260
|
+
|
|
261
|
+
const recall = await recallFor('chain-freshness-fresh', binding, '部署脚本放在哪里')
|
|
262
|
+
assert.ok(recall.ids.includes('mem-fresh-now'))
|
|
263
|
+
assert.ok(recall.text.includes('</memory-context>'))
|
|
264
|
+
assert.equal(recall.text.includes('<memory-freshness>'), false)
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
test('the hint ships in the same plugin message as the recall block', async () => {
|
|
268
|
+
state.updateSettings({ randomCueChance: 0 })
|
|
269
|
+
const cwd = path.join(home, 'project-freshness-message')
|
|
270
|
+
const binding = state.ensureSession('chain-freshness-message', { cwd, agentPreset: 'coding' })
|
|
271
|
+
seedRecallEntry(binding, 'mem-fresh-message', { updatedAt: new Date(Date.now() - 45 * DAY_MS).toISOString() })
|
|
272
|
+
|
|
273
|
+
const user = {
|
|
274
|
+
id: 'u-freshness', role: 'user', source: { kind: 'user' },
|
|
275
|
+
content: [{ type: 'text', text: '部署脚本放在哪里' }],
|
|
276
|
+
}
|
|
277
|
+
const agent = { session: { header: { id: 'chain-freshness-message', cwd, agentPreset: 'coding', origin: 'user' } } }
|
|
278
|
+
const out = await inject.injectForStep({ agent, decision: { kind: 'enter', messages: [user] }, turn: 1, step: 1, signal: { aborted: false } })
|
|
279
|
+
|
|
280
|
+
assert.equal(out.messages.length, 2)
|
|
281
|
+
assert.equal(out.messages[0].source.kind, 'plugin:dsh-memu-memory')
|
|
282
|
+
const text = out.messages[0].content[0].text
|
|
283
|
+
assert.ok(text.includes('[mem-fresh-message]'))
|
|
284
|
+
assert.equal((text.match(/<memory-freshness>/g) || []).length, 1)
|
|
285
|
+
assert.ok(text.indexOf('<memory-freshness>') > text.lastIndexOf('</memory-context>'))
|
|
286
|
+
assert.equal(state.getSessionBinding('chain-freshness-message').lastRecallError, undefined)
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
test('anchor-only and autoInject off never append the freshness hint', async () => {
|
|
290
|
+
const cwd = path.join(home, 'project-freshness-modes')
|
|
291
|
+
const binding = state.ensureSession('chain-freshness-modes', { cwd, agentPreset: 'coding' })
|
|
292
|
+
seedRecallEntry(binding, 'mem-fresh-modes', { updatedAt: new Date(Date.now() - 90 * DAY_MS).toISOString() })
|
|
293
|
+
|
|
294
|
+
const anchorOnly = await recallFor('chain-freshness-modes', binding, '部署脚本放在哪里', { autoInject: 'anchor-only' })
|
|
295
|
+
assert.ok(anchorOnly.text.includes('</memory-context>'))
|
|
296
|
+
assert.equal(anchorOnly.text.includes('<memory-freshness>'), false)
|
|
297
|
+
|
|
298
|
+
// buildRecall itself is not the off switch; the caller is. The hint still must
|
|
299
|
+
// not appear if a future caller forgets that.
|
|
300
|
+
const off = await recallFor('chain-freshness-modes', binding, '部署脚本放在哪里', { autoInject: 'off' })
|
|
301
|
+
assert.ok(off.text.includes('</memory-context>'))
|
|
302
|
+
assert.equal(off.text.includes('<memory-freshness>'), false)
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
test('a query shorter than minUserChars gets the recall but no freshness hint', async () => {
|
|
306
|
+
const cwd = path.join(home, 'project-freshness-short')
|
|
307
|
+
const binding = state.ensureSession('chain-freshness-short', { cwd, agentPreset: 'coding' })
|
|
308
|
+
seedRecallEntry(binding, 'mem-fresh-short', {
|
|
309
|
+
title: '最新的部署方式',
|
|
310
|
+
content: '最新的部署方式放在 tools/deploy.ps1。',
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
const recall = await recallFor('chain-freshness-short', binding, '最新')
|
|
314
|
+
assert.ok(recall.ids.includes('mem-fresh-short'))
|
|
315
|
+
assert.ok(recall.text.includes('</memory-context>'))
|
|
316
|
+
assert.equal(recall.text.includes('<memory-freshness>'), false)
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
test('freshnessHint false suppresses the freshness hint', async () => {
|
|
320
|
+
const cwd = path.join(home, 'project-freshness-switch')
|
|
321
|
+
const binding = state.ensureSession('chain-freshness-switch', { cwd, agentPreset: 'coding' })
|
|
322
|
+
seedRecallEntry(binding, 'mem-fresh-switch', { updatedAt: new Date(Date.now() - 45 * DAY_MS).toISOString() })
|
|
323
|
+
|
|
324
|
+
const recall = await recallFor('chain-freshness-switch', binding, '部署脚本放在哪里', { freshnessHint: false })
|
|
325
|
+
assert.ok(recall.ids.includes('mem-fresh-switch'))
|
|
326
|
+
assert.ok(recall.text.includes('</memory-context>'))
|
|
327
|
+
assert.equal(recall.text.includes('<memory-freshness>'), false)
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
test('the hint is dropped when turnTokens cannot fit it, and the recall still succeeds', async () => {
|
|
331
|
+
const cwd = path.join(home, 'project-freshness-budget')
|
|
332
|
+
const binding = state.ensureSession('chain-freshness-budget', { cwd, agentPreset: 'coding' })
|
|
333
|
+
seedRecallEntry(binding, 'mem-fresh-budget', { updatedAt: new Date(Date.now() - 45 * DAY_MS).toISOString() })
|
|
334
|
+
|
|
335
|
+
const full = await recallFor('chain-freshness-budget', binding, '部署脚本放在哪里', { turnTokens: 800 })
|
|
336
|
+
const marker = '\n\n<memory-freshness>'
|
|
337
|
+
const at = full.text.indexOf(marker)
|
|
338
|
+
assert.ok(at > 0, 'with room the hint appears')
|
|
339
|
+
const hint = full.text.slice(at)
|
|
340
|
+
const base = full.text.slice(0, at)
|
|
341
|
+
const needed = budget.estTokens(base)
|
|
342
|
+
assert.ok(budget.estTokens(base + hint) > needed, 'the hint itself costs something')
|
|
343
|
+
|
|
344
|
+
// The smallest budget that still fits the card cannot also fit the footnote.
|
|
345
|
+
let squeezed = null
|
|
346
|
+
for (let extra = 0; extra <= 12 && !squeezed; extra += 1) {
|
|
347
|
+
const turnTokens = needed + extra
|
|
348
|
+
const attempt = await recallFor('chain-freshness-budget', binding, '部署脚本放在哪里', { turnTokens })
|
|
349
|
+
if (attempt.text) squeezed = { attempt, turnTokens }
|
|
350
|
+
}
|
|
351
|
+
assert.ok(squeezed, 'a budget near the base size still fits one card')
|
|
352
|
+
assert.ok(squeezed.attempt.text.includes('[mem-fresh-budget]'))
|
|
353
|
+
assert.ok(squeezed.attempt.text.includes('</memory-context>'))
|
|
354
|
+
assert.equal(squeezed.attempt.text.includes('<memory-freshness>'), false)
|
|
355
|
+
assert.ok(budget.estTokens(squeezed.attempt.text) <= squeezed.turnTokens)
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
test('no recall means no freshness hint', async () => {
|
|
359
|
+
const cwd = path.join(home, 'project-freshness-none')
|
|
360
|
+
const binding = state.ensureSession('chain-freshness-none', { cwd, agentPreset: 'coding' })
|
|
361
|
+
seedRecallEntry(binding, 'mem-fresh-none', { updatedAt: new Date(Date.now() - 45 * DAY_MS).toISOString() })
|
|
362
|
+
|
|
363
|
+
const recall = await recallFor('chain-freshness-none', binding, 'zzz-unrelated-token-zzz')
|
|
364
|
+
assert.equal(recall.text, '')
|
|
365
|
+
assert.deepEqual(recall.ids, [])
|
|
366
|
+
assert.equal(recall.text.includes('<memory-freshness>'), false)
|
|
367
|
+
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Dice 阈值标定脚手架(scripts/lexicon-calibrate.mjs)的轻量 smoke 测试:
|
|
2
|
+
// 只保证脚本独立运行不抛错、产出指标行;标定样本本身是给人看的报告,
|
|
3
|
+
// 不进 npm test 门禁断言(脚本不在此处做阈值回归)。
|
|
4
|
+
import test from 'node:test'
|
|
5
|
+
import assert from 'node:assert/strict'
|
|
6
|
+
import { spawnSync } from 'node:child_process'
|
|
7
|
+
import fs from 'node:fs'
|
|
8
|
+
import os from 'node:os'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import { fileURLToPath } from 'node:url'
|
|
11
|
+
|
|
12
|
+
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
13
|
+
const scriptPath = path.join(here, '..', 'scripts', 'lexicon-calibrate.mjs')
|
|
14
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-lexicon-calibrate-smoke-'))
|
|
15
|
+
process.env.DSH_MEMU_HOME = home
|
|
16
|
+
|
|
17
|
+
test.after(() => {
|
|
18
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
function runCalibrate(args) {
|
|
22
|
+
return spawnSync(process.execPath, [scriptPath, ...args], {
|
|
23
|
+
encoding: 'utf8',
|
|
24
|
+
timeout: 120_000,
|
|
25
|
+
env: { ...process.env, DSH_MEMU_HOME: fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-lexicon-calibrate-run-')) },
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
test('lexicon-calibrate.mjs runs standalone, never touches the caller data dir, and prints the 0.60 metrics', () => {
|
|
30
|
+
const run = runCalibrate([])
|
|
31
|
+
assert.equal(run.status, 0, `calibrate exits 0. stderr: ${run.stderr}`)
|
|
32
|
+
assert.match(run.stdout, /只出报告,不自动改 LEXICON_DICE_THRESHOLD/)
|
|
33
|
+
assert.match(run.stdout, /正例 16 对,负例 16 对/)
|
|
34
|
+
assert.match(run.stdout, /当前 0\.60 下的指标:precision=/)
|
|
35
|
+
// 汇总表覆盖默认阈值扫描 0.55..0.75。
|
|
36
|
+
for (const threshold of ['0.55', '0.60', '0.65', '0.70', '0.75']) {
|
|
37
|
+
assert.ok(run.stdout.includes(threshold), `sweep table contains ${threshold}`)
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('lexicon-calibrate.mjs accepts a --threshold list and still exits cleanly', () => {
|
|
42
|
+
const run = runCalibrate(['--threshold=0.5,0.62,0.9'])
|
|
43
|
+
assert.equal(run.status, 0, `stderr: ${run.stderr}`)
|
|
44
|
+
assert.match(run.stdout, /当前 0\.50 下的指标/, 'the primary threshold is the first given value')
|
|
45
|
+
for (const threshold of ['0.50', '0.62', '0.90']) {
|
|
46
|
+
assert.ok(run.stdout.includes(threshold), `sweep table contains ${threshold}`)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// 历史重复词条迁移工具(scripts/lexicon-dedupe.mjs)的端到端测试:
|
|
2
|
+
// 通过子进程跑真实 CLI(dry-run / --apply 两条路径),fixture 直接写
|
|
3
|
+
// data/lexicon/<id>.json,覆盖:可合并重复组、active+sensitive 跳过、
|
|
4
|
+
// 跨作用域同词不合并、pending revision 草稿不当作重复。
|
|
5
|
+
import test from 'node:test'
|
|
6
|
+
import assert from 'node:assert/strict'
|
|
7
|
+
import { spawnSync } from 'node:child_process'
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import os from 'node:os'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import { fileURLToPath } from 'node:url'
|
|
12
|
+
|
|
13
|
+
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
14
|
+
const scriptPath = path.join(here, '..', 'scripts', 'lexicon-dedupe.mjs')
|
|
15
|
+
|
|
16
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-lexicon-dedupe-'))
|
|
17
|
+
process.env.DSH_MEMU_HOME = home
|
|
18
|
+
|
|
19
|
+
const state = await import('../lib/state.js')
|
|
20
|
+
const paths = await import('../lib/paths.js')
|
|
21
|
+
const lexicon = await import('../lib/lexicon.js')
|
|
22
|
+
|
|
23
|
+
test.after(() => {
|
|
24
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
function runCli(args) {
|
|
28
|
+
return spawnSync(process.execPath, [scriptPath, ...args], {
|
|
29
|
+
encoding: 'utf8',
|
|
30
|
+
env: { ...process.env, DSH_MEMU_HOME: home },
|
|
31
|
+
timeout: 60_000,
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function entryOf(overrides, termId, extra = {}) {
|
|
36
|
+
return {
|
|
37
|
+
term: overrides.term,
|
|
38
|
+
normalizedTerm: lexicon.normalizeLexeme(overrides.term),
|
|
39
|
+
normalizedAliases: (overrides.aliases || []).map(a => lexicon.normalizeLexeme(a)),
|
|
40
|
+
kind: 'term',
|
|
41
|
+
meaning: overrides.meaning || '',
|
|
42
|
+
usage: overrides.usage || '',
|
|
43
|
+
aliases: overrides.aliases || [],
|
|
44
|
+
examples: overrides.examples || [],
|
|
45
|
+
context: overrides.context || {},
|
|
46
|
+
knownBy: overrides.knownBy,
|
|
47
|
+
visibility: 'shared',
|
|
48
|
+
sensitivity: overrides.sensitivity || 'normal',
|
|
49
|
+
strength: overrides.strength ?? 0.5,
|
|
50
|
+
confidence: overrides.confidence ?? 0.7,
|
|
51
|
+
status: overrides.status || 'active',
|
|
52
|
+
source: 'user',
|
|
53
|
+
evidence: overrides.evidence || [],
|
|
54
|
+
fingerprint: `fp-${termId}`,
|
|
55
|
+
createdAt: overrides.createdAt || '2026-01-01T00:00:00.000Z',
|
|
56
|
+
updatedAt: overrides.updatedAt || '2026-01-01T00:00:00.000Z',
|
|
57
|
+
hitCounts: overrides.hitCounts || { candidate: 0, injection: 0, query: 0 },
|
|
58
|
+
lastInjectedAt: null,
|
|
59
|
+
...extra,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ---- fixture:一本含四种形态的词典 ----
|
|
64
|
+
const library = state.createLibrary({ name: 'dedupe-fixture', cwd: path.join(home, 'dedupe') })
|
|
65
|
+
const lexFile = path.join(home, 'lexicon', `${library.id}.json`)
|
|
66
|
+
|
|
67
|
+
const A1 = 'lex-dup-a1', A2 = 'lex-dup-a2', A3 = 'lex-dup-a3', DRAFT = 'lex-dup-draft'
|
|
68
|
+
const S1 = 'lex-dup-s1', S2 = 'lex-dup-s2'
|
|
69
|
+
const C1 = 'lex-dup-c1', C2 = 'lex-dup-c2'
|
|
70
|
+
|
|
71
|
+
const fixture = {
|
|
72
|
+
version: 1,
|
|
73
|
+
revision: 7,
|
|
74
|
+
libraryId: library.id,
|
|
75
|
+
rejected: {
|
|
76
|
+
'fp-lex-dup-a3': { termId: A3, reason: '历史驳回记录', at: '2026-03-01T00:00:00.000Z' },
|
|
77
|
+
},
|
|
78
|
+
terms: {
|
|
79
|
+
// 组 A:同词同作用域三条(active 旧 / active 新 / rejected)+ 一条 active 的待审修订草稿。
|
|
80
|
+
[A1]: entryOf({ term: '糖刀', meaning: '短义', aliases: ['糖里藏刀'], strength: 0.3, status: 'active', updatedAt: '2026-01-01T00:00:00.000Z' }, A1),
|
|
81
|
+
[A2]: entryOf({ term: '糖刀', meaning: '较长的保留条目含义', aliases: [], examples: ['保留条目的例句'], strength: 0.9, status: 'active', updatedAt: '2026-02-01T00:00:00.000Z' }, A2),
|
|
82
|
+
[A3]: entryOf({
|
|
83
|
+
term: '糖刀',
|
|
84
|
+
meaning: '被驳回条目里最长的含义文本,合并时应当胜出成为保留条目的含义',
|
|
85
|
+
aliases: ['刀里藏糖'],
|
|
86
|
+
examples: ['驳回条目的例句'],
|
|
87
|
+
strength: 0.5,
|
|
88
|
+
status: 'rejected',
|
|
89
|
+
evidence: [{ kind: 'message', id: 'm-1', quote: '这条留言是证据' }],
|
|
90
|
+
hitCounts: { candidate: 4, injection: 1, query: 2 },
|
|
91
|
+
updatedAt: '2026-03-01T00:00:00.000Z',
|
|
92
|
+
}, A3),
|
|
93
|
+
// 待审修订草稿(revisionOf 指向 A2):不是重复,必须原样保留。
|
|
94
|
+
[DRAFT]: entryOf({ term: '糖刀', meaning: '草稿含义', status: 'pending', updatedAt: '2026-04-01T00:00:00.000Z' }, DRAFT, { revisionOf: A2, previousRevision: 6 }),
|
|
95
|
+
// 组 S:active+sensitive 与其 pending 重复 → 整组跳过。
|
|
96
|
+
[S1]: entryOf({ term: '红牌', meaning: '敏感触发词', kind: 'trigger', sensitivity: 'sensitive', status: 'active' }, S1),
|
|
97
|
+
[S2]: entryOf({ term: '红牌', meaning: '重复的敏感触发词', kind: 'trigger', sensitivity: 'sensitive', status: 'pending' }, S2),
|
|
98
|
+
// 组 C:同词跨作用域 → 不是重复。
|
|
99
|
+
[C1]: entryOf({ term: '月引', meaning: 'w1 的月引', context: { worldId: 'w1' } }, C1),
|
|
100
|
+
[C2]: entryOf({ term: '月引', meaning: 'w2 的月引', context: { worldId: 'w2' } }, C2),
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
paths.writeJson(lexFile, fixture)
|
|
104
|
+
|
|
105
|
+
const readStore = () => JSON.parse(fs.readFileSync(lexFile, 'utf8'))
|
|
106
|
+
|
|
107
|
+
test('dry-run reports duplicate groups without touching a single byte', () => {
|
|
108
|
+
const before = fs.readFileSync(lexFile)
|
|
109
|
+
const run = runCli([])
|
|
110
|
+
assert.equal(run.status, 0, `dry-run exits 0: ${run.stderr}`)
|
|
111
|
+
const out = run.stdout
|
|
112
|
+
assert.match(out, /dry-run/)
|
|
113
|
+
assert.match(out, /重复组 2 组/, '组 A 与组 S 是重复组;组 C 跨作用域不算')
|
|
114
|
+
assert.match(out, new RegExp(`保留 ${A2}`), '组 A 保留 active 且 updatedAt 新的 A2')
|
|
115
|
+
assert.match(out, new RegExp(`并入并删除: ${A1}, ${A3}`))
|
|
116
|
+
assert.match(out, /active\+sensitive/, '组 S 因 active+sensitive 被跳过')
|
|
117
|
+
assert.match(out, /mergedFrom 记入: \[lex-dup-a1, lex-dup-a3\]/)
|
|
118
|
+
assert.deepEqual(fs.readFileSync(lexFile), before, 'dry-run must not modify the file')
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('--apply merges duplicate groups, keeps sensitive/cross-scope entries and the revision draft', () => {
|
|
122
|
+
const run = runCli(['--apply'])
|
|
123
|
+
assert.equal(run.status, 0, `apply exits 0: ${run.stderr}`)
|
|
124
|
+
const store = readStore()
|
|
125
|
+
|
|
126
|
+
// 组 A:只剩保留条目 + 待审草稿。
|
|
127
|
+
assert.equal(store.terms[A1], undefined, 'A1 merged away')
|
|
128
|
+
assert.equal(store.terms[A3], undefined, 'A3 merged away')
|
|
129
|
+
const kept = store.terms[A2]
|
|
130
|
+
assert.ok(kept, 'A2 kept')
|
|
131
|
+
assert.deepEqual(kept.mergedFrom, [A1, A3], 'mergedFrom records the removed ids for audit')
|
|
132
|
+
assert.equal(kept.meaning, fixture.terms[A3].meaning, 'meaning takes the longest across the group')
|
|
133
|
+
assert.deepEqual([...kept.aliases].sort(), ['刀里藏糖', '糖里藏刀'], 'aliases are the deduplicated union')
|
|
134
|
+
assert.deepEqual(kept.examples.sort(), ['保留条目的例句', '驳回条目的例句'].sort())
|
|
135
|
+
assert.equal(kept.strength, 0.9, 'strength takes the max')
|
|
136
|
+
assert.equal(kept.status, 'active')
|
|
137
|
+
assert.equal(kept.evidence.length, 1, 'evidence merged from the rejected member')
|
|
138
|
+
assert.deepEqual(kept.hitCounts, { candidate: 4, injection: 1, query: 2 }, 'hit counts sum across the group')
|
|
139
|
+
// 拒绝指纹改指保留条目。
|
|
140
|
+
assert.equal(store.rejected['fp-lex-dup-a3'].termId, A2, 'the rejected fingerprint now points at the kept entry')
|
|
141
|
+
// 草稿、敏感组、跨作用域都原样。
|
|
142
|
+
assert.equal(store.terms[DRAFT].status, 'pending', 'the active term\'s revision draft is not a duplicate')
|
|
143
|
+
assert.equal(store.terms[DRAFT].revisionOf, A2)
|
|
144
|
+
assert.ok(store.terms[S1] && store.terms[S2], 'the active+sensitive group is untouched')
|
|
145
|
+
assert.equal(store.terms[S1].meaning, '敏感触发词')
|
|
146
|
+
assert.ok(store.terms[C1] && store.terms[C2], 'cross-scope same word is not a duplicate')
|
|
147
|
+
assert.ok(store.revision > 7, 'the revision advanced')
|
|
148
|
+
// 结果仍然是一本可严格读取的词典:8 条 − 2 条被并 = 6 条。
|
|
149
|
+
const loaded = lexicon.loadLexicon(library.id)
|
|
150
|
+
assert.ok(loaded.terms[A2])
|
|
151
|
+
assert.equal(Object.keys(loaded.terms).length, 6)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('the merged library still enforces write-side uniqueness (no re-creation of duplicates)', () => {
|
|
155
|
+
// 非敏感字段(usage)原地更新:撞上的是保留条目 A2,绝不会新建重复词条。
|
|
156
|
+
const keptMeaning = readStore().terms[A2].meaning
|
|
157
|
+
const upsert = lexicon.upsertLexiconTerm({ libraryId: library.id, term: '糖刀', kind: 'term', meaning: keptMeaning, usage: '再次直接写入' })
|
|
158
|
+
assert.equal(upsert.created, false)
|
|
159
|
+
assert.equal(upsert.term.termId, A2, 'the kept entry is the collision target, exactly like a never-duplicated term')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('--library narrows the scan; unsafe ids are a typed usage error', () => {
|
|
163
|
+
const run = runCli(['--library', library.id])
|
|
164
|
+
assert.equal(run.status, 0)
|
|
165
|
+
assert.match(run.stdout, /重复组 1 组/, 'only the skipped active+sensitive group remains')
|
|
166
|
+
assert.match(run.stdout, /active\+sensitive/, 'reported as skipped, never touched')
|
|
167
|
+
assert.match(run.stdout, /dry-run 未写入/)
|
|
168
|
+
const bad = runCli(['--library', '../escape'])
|
|
169
|
+
assert.equal(bad.status, 2, 'bad --library is a usage error, not a crash')
|
|
170
|
+
assert.match(bad.stderr, /invalid library id/)
|
|
171
|
+
})
|