dsh-memu-memory 1.2.6 → 1.2.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.
- package/AGENTS.md +1 -1
- package/README.md +10 -15
- package/README.zh.md +10 -15
- package/docs/contributing.md +1 -1
- package/docs/host-adaptation.md +1 -1
- package/docs/memory-semantics.md +26 -4
- package/docs/troubleshooting.md +3 -1
- package/lib/backup.js +1 -1
- package/lib/binding-diagnostics.js +2 -2
- package/lib/budget.js +31 -0
- package/lib/client.js +276 -28
- package/lib/commitment.js +138 -0
- package/lib/consolidation-receipt.js +2 -0
- package/lib/consolidation-worker.js +3 -0
- package/lib/curator-policy.js +98 -0
- package/lib/fork-model.js +312 -0
- package/lib/freshness.js +24 -0
- package/lib/inject.js +117 -10
- package/lib/lexicon.js +1 -1
- package/lib/local-vector-service.js +4 -1
- package/lib/memory.js +600 -43
- package/lib/memu.js +4 -0
- package/lib/private.js +49 -8
- package/lib/prompts/private-consolidation.mixed.md +3 -2
- package/lib/prompts/private-consolidation.rp.md +7 -1
- package/lib/prompts/private-policy.mixed.md +1 -0
- package/lib/prompts/private-policy.rp.md +5 -1
- package/lib/prompts/system-guide.rp.md +3 -0
- package/lib/recall-cache.js +212 -0
- package/lib/rpc.js +44 -6
- package/lib/snippet.js +25 -0
- package/lib/state.js +43 -1
- package/lib/temporal.js +329 -0
- package/lib/text-retrieval.js +292 -28
- package/lib/tokenizer-v2.js +295 -0
- package/lib/tools.js +81 -4
- package/lib/webui/standalone.css +17 -1
- package/package.json +1 -1
- package/scripts/bench-recall.mjs +142 -0
- package/scripts/check-seed-compat.mjs +118 -0
- package/tests/backup.test.mjs +2 -2
- package/tests/commitment.test.mjs +144 -0
- package/tests/consolidation-fork.test.mjs +122 -5
- package/tests/curator-clusters.test.mjs +7 -1
- package/tests/curator-pairing.test.mjs +52 -8
- package/tests/disclosure.test.mjs +34 -17
- package/tests/dynamic-rrf.test.mjs +179 -0
- package/tests/fact-version.test.mjs +128 -0
- package/tests/fork-model.test.mjs +345 -0
- package/tests/help-page.test.mjs +1 -1
- package/tests/inject-conflict.test.mjs +94 -0
- package/tests/inject-guide.test.mjs +5 -3
- package/tests/inject.test.mjs +2 -2
- package/tests/injection-intent-budget.test.mjs +92 -0
- package/tests/lexicon-frequency.test.mjs +1 -1
- package/tests/loader.mjs +3 -0
- package/tests/memory-chain.test.mjs +140 -0
- package/tests/memory-fields.test.mjs +134 -0
- package/tests/memu-resident.test.mjs +33 -0
- package/tests/negation-polarity.test.mjs +135 -0
- package/tests/prompt-semantics.test.mjs +51 -0
- package/tests/query-decompose.test.mjs +62 -0
- package/tests/recall-cache.test.mjs +292 -0
- package/tests/recall-intent.test.mjs +100 -0
- package/tests/speaker-provenance.test.mjs +120 -0
- package/tests/stubs/dsh-llm-pi-ai.js +13 -0
- package/tests/temporal.test.mjs +124 -0
- package/tests/tokenizer-v2.test.mjs +142 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-chain-'))
|
|
8
|
+
process.env.DSH_MEMU_HOME = home
|
|
9
|
+
|
|
10
|
+
const { registerTools } = await import('../lib/tools.js')
|
|
11
|
+
const state = await import('../lib/state.js')
|
|
12
|
+
const inject = await import('../lib/inject.js')
|
|
13
|
+
const { dispatch } = await import('../lib/rpc.js')
|
|
14
|
+
|
|
15
|
+
test.after(() => {
|
|
16
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
function registeredTools() {
|
|
20
|
+
const tools = new Map()
|
|
21
|
+
registerTools({
|
|
22
|
+
tools: { register(tool) { tools.set(tool.name, tool); return () => {} } },
|
|
23
|
+
logger: { warn() {} },
|
|
24
|
+
})
|
|
25
|
+
return tools
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function idsIn(text) {
|
|
29
|
+
return [...String(text || '').matchAll(/mem-[a-z0-9]+-[a-z0-9]+/gi)].map((match) => match[0])
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('the real tool → recall → injection chain is reachable and carries S1–S6 fields', async () => {
|
|
33
|
+
const tools = registeredTools()
|
|
34
|
+
for (const name of ['memu_private_remember', 'memu_private_recall', 'memu_private_update', 'memu_status']) {
|
|
35
|
+
assert.ok(tools.has(name), `missing tool ${name}`)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
state.updateSettings({ randomCueChance: 0, habitsInjection: false, dedupeTurns: 0 })
|
|
39
|
+
const cwd = path.join(home, 'project-chain')
|
|
40
|
+
const sessionId = 'chain-session'
|
|
41
|
+
state.ensureSession(sessionId, { cwd, agentPreset: 'rp' })
|
|
42
|
+
state.setSessionBinding(sessionId, { sceneTime: '2026-11-06T04:00:00.000Z' })
|
|
43
|
+
const exec = { agent: { session: { header: { id: sessionId, cwd, agentPreset: 'rp' } } } }
|
|
44
|
+
|
|
45
|
+
// 1. A user fact written through the actual model-facing tool.
|
|
46
|
+
const userReply = await tools.get('memu_private_remember').execute({
|
|
47
|
+
content: '明天下午三点在码头见面,别迟到。',
|
|
48
|
+
title: '明天码头见面',
|
|
49
|
+
source: 'user_said',
|
|
50
|
+
speaker_id: 'user',
|
|
51
|
+
}, exec)
|
|
52
|
+
const [userFactId] = idsIn(userReply)
|
|
53
|
+
assert.ok(userFactId, userReply)
|
|
54
|
+
const userEntry = state.loadPrivateIndex(state.getSessionBinding(sessionId).libraryId).entries[userFactId]
|
|
55
|
+
assert.equal(userEntry.absolute_date, '2026-11-07')
|
|
56
|
+
assert.equal(userEntry.speaker_id, 'user')
|
|
57
|
+
assert.equal(userEntry.source_kind, 'user_said')
|
|
58
|
+
|
|
59
|
+
// 2. An assistant promise, written as assistant_said with a commitment.
|
|
60
|
+
const promiseReply = await tools.get('memu_private_remember').execute({
|
|
61
|
+
content: '我答应明天提前去码头占位置。',
|
|
62
|
+
title: '答应提前去码头占位',
|
|
63
|
+
source_kind: 'assistant_said',
|
|
64
|
+
commitment: { counterparty: 'user', action: '提前去码头占位', due_at: '2026-11-07', status: 'open' },
|
|
65
|
+
}, exec)
|
|
66
|
+
const [promiseId] = idsIn(promiseReply)
|
|
67
|
+
assert.ok(promiseId, promiseReply)
|
|
68
|
+
const promiseEntry = state.loadPrivateIndex(state.getSessionBinding(sessionId).libraryId).entries[promiseId]
|
|
69
|
+
assert.equal(promiseEntry.source_kind, 'assistant_said')
|
|
70
|
+
assert.equal(promiseEntry.speaker_id, 'assistant')
|
|
71
|
+
assert.equal(promiseEntry.commitment.status, 'open')
|
|
72
|
+
assert.equal(promiseEntry.openLoop, true)
|
|
73
|
+
|
|
74
|
+
// 3. Two conflicting versions of one fact, then a conflict card in the real recall.
|
|
75
|
+
const floorA = idsIn(await tools.get('memu_private_remember').execute({
|
|
76
|
+
content: '客厅地板是木色的。', title: '客厅地板木色', fact_key: 'home:living_floor_color',
|
|
77
|
+
}, exec))[0]
|
|
78
|
+
const floorB = idsIn(await tools.get('memu_private_remember').execute({
|
|
79
|
+
content: '客厅地板不是木色,是灰色瓷砖。', title: '客厅地板灰色', fact_key: 'home:living_floor_color',
|
|
80
|
+
}, exec))[0]
|
|
81
|
+
assert.ok(floorA && floorB)
|
|
82
|
+
|
|
83
|
+
// 4. The explicit recall tool sees both the dated fact and the conflict pair.
|
|
84
|
+
const recallReply = await tools.get('memu_private_recall').execute({ query: '明天码头', topK: 10 }, exec)
|
|
85
|
+
assert.ok(recallReply.includes(userFactId), recallReply)
|
|
86
|
+
assert.ok(recallReply.includes(promiseId))
|
|
87
|
+
const floorReply = await tools.get('memu_private_recall').execute({ query: '客厅地板颜色', topK: 10 }, exec)
|
|
88
|
+
assert.ok(floorReply.includes(floorA) && floorReply.includes(floorB), floorReply)
|
|
89
|
+
|
|
90
|
+
// 5. The real pre-step injection path emits one plugin message carrying the fields
|
|
91
|
+
// the model needs to answer: resolved date, speaker and the conflict card.
|
|
92
|
+
const agent = { session: { header: { id: sessionId, cwd, agentPreset: 'rp', origin: 'user' } } }
|
|
93
|
+
const dateUser = {
|
|
94
|
+
id: 'chain-u1', role: 'user', source: { kind: 'user' },
|
|
95
|
+
content: [{ type: 'text', text: '明天下午我们在码头几点见面?' }],
|
|
96
|
+
}
|
|
97
|
+
const dated = await inject.injectForStep({
|
|
98
|
+
agent,
|
|
99
|
+
decision: { kind: 'enter', messages: [dateUser] },
|
|
100
|
+
turn: 1,
|
|
101
|
+
step: 1,
|
|
102
|
+
signal: { aborted: false },
|
|
103
|
+
})
|
|
104
|
+
const datedText = dated.messages
|
|
105
|
+
.filter((message) => message?.source?.kind === 'plugin:dsh-memu-memory')
|
|
106
|
+
.map((message) => String(message?.content?.[0]?.text || ''))
|
|
107
|
+
.join('\n')
|
|
108
|
+
assert.ok(datedText.includes(userFactId), datedText)
|
|
109
|
+
assert.match(datedText, /date=2026-11-07/, 'the resolved absolute date reaches the prompt')
|
|
110
|
+
assert.match(datedText, /source=assistant_said/)
|
|
111
|
+
assert.match(datedText, /speaker=assistant/)
|
|
112
|
+
// The user message is untouched and the plugin message comes before it.
|
|
113
|
+
assert.equal(dated.messages.at(-1).source.kind, 'user')
|
|
114
|
+
assert.equal(dated.messages.at(-1).content[0].text, dateUser.content[0].text)
|
|
115
|
+
|
|
116
|
+
const floorUser = {
|
|
117
|
+
id: 'chain-u2', role: 'user', source: { kind: 'user' },
|
|
118
|
+
content: [{ type: 'text', text: '客厅地板到底是什么颜色,前后说法不一样。' }],
|
|
119
|
+
}
|
|
120
|
+
const conflicted = await inject.injectForStep({
|
|
121
|
+
agent,
|
|
122
|
+
decision: { kind: 'enter', messages: [floorUser] },
|
|
123
|
+
turn: 2,
|
|
124
|
+
step: 1,
|
|
125
|
+
signal: { aborted: false },
|
|
126
|
+
})
|
|
127
|
+
const conflictText = conflicted.messages
|
|
128
|
+
.filter((message) => message?.source?.kind === 'plugin:dsh-memu-memory')
|
|
129
|
+
.map((message) => String(message?.content?.[0]?.text || ''))
|
|
130
|
+
.join('\n')
|
|
131
|
+
assert.match(conflictText, /冲突 · home:living_floor_color/, conflictText)
|
|
132
|
+
assert.ok(conflictText.includes(floorA) && conflictText.includes(floorB))
|
|
133
|
+
|
|
134
|
+
// 6. Observability: getState exposes the recall cache counters, including staleSkipped.
|
|
135
|
+
const view = await dispatch({}, 'getState', { sessionId })
|
|
136
|
+
assert.equal(typeof view.recallCache, 'object')
|
|
137
|
+
for (const key of ['hits', 'misses', 'evictions', 'errors', 'staleSkipped']) {
|
|
138
|
+
assert.equal(typeof view.recallCache[key], 'number', key)
|
|
139
|
+
}
|
|
140
|
+
})
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-temporal-fields-'))
|
|
8
|
+
process.env.DSH_MEMU_HOME = home
|
|
9
|
+
|
|
10
|
+
const state = await import('../lib/state.js')
|
|
11
|
+
const memory = await import('../lib/memory.js')
|
|
12
|
+
|
|
13
|
+
test.after(() => {
|
|
14
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
let seq = 0
|
|
18
|
+
function session(name, sceneTime = '2026-11-06T04:00:00.000Z') {
|
|
19
|
+
seq += 1
|
|
20
|
+
const cwd = path.join(home, 'ws', `${name}-${seq}`)
|
|
21
|
+
const sessionId = `${name}-${seq}`
|
|
22
|
+
state.ensureSession(sessionId, { cwd, agentPreset: 'rp' })
|
|
23
|
+
state.setSessionBinding(sessionId, { sceneTime })
|
|
24
|
+
return {
|
|
25
|
+
sessionId,
|
|
26
|
+
cwd,
|
|
27
|
+
exec: { agent: { session: { header: { id: sessionId, cwd, agentPreset: 'rp' } } } },
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function index(sessionId) {
|
|
32
|
+
const binding = state.getSessionBinding(sessionId)
|
|
33
|
+
return state.loadPrivateIndex(binding.libraryId)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test('a relative date is resolved against sceneTime and stored as fields', async () => {
|
|
37
|
+
const { exec, sessionId } = session('fields-relative')
|
|
38
|
+
const { id, entry } = await memory.privateRemember({
|
|
39
|
+
exec, title: '明天出门带伞', content: '她说 明天 去学校门口见面。',
|
|
40
|
+
})
|
|
41
|
+
assert.equal(entry.absolute_date, '2026-11-07')
|
|
42
|
+
assert.equal(entry.date_precision, 'day')
|
|
43
|
+
assert.equal(entry.date_source, 'relative')
|
|
44
|
+
assert.equal(entry.event_time, '2026-11-06T04:00:00.000Z')
|
|
45
|
+
assert.equal(entry.observed_at, '2026-11-06T04:00:00.000Z')
|
|
46
|
+
assert.equal(entry.content, '她说 明天 去学校门口见面。', 'the original wording is kept verbatim')
|
|
47
|
+
const onDisk = index(sessionId).entries[id]
|
|
48
|
+
assert.equal(onDisk.absolute_date, '2026-11-07', 'fields survive the index round trip')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('context.occurredAt overrides sceneTime for the same write', async () => {
|
|
52
|
+
const { exec } = session('fields-occurred')
|
|
53
|
+
const { entry } = await memory.privateRemember({
|
|
54
|
+
exec, title: '后天交材料', content: '后天把材料交到教务处。',
|
|
55
|
+
context: { occurredAt: '2026-12-30T12:00:00+08:00' },
|
|
56
|
+
})
|
|
57
|
+
assert.equal(entry.absolute_date, '2027-01-01', 'crosses the year boundary in the scene time zone')
|
|
58
|
+
assert.equal(entry.observed_at, '2026-12-30T04:00:00.000Z')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('month-end and unresolved relative wording are marked, not guessed', async () => {
|
|
62
|
+
const { exec } = session('fields-precision')
|
|
63
|
+
const monthEnd = await memory.privateRemember({ exec, title: '月底对账', content: '月底前把账对完。' })
|
|
64
|
+
assert.equal(monthEnd.entry.absolute_date, '2026-11-30')
|
|
65
|
+
assert.equal(monthEnd.entry.date_precision, 'month')
|
|
66
|
+
|
|
67
|
+
const fuzzy = await memory.privateRemember({ exec, title: '下周再说', content: '下周再讨论排期。' })
|
|
68
|
+
assert.equal(fuzzy.entry.absolute_date, undefined)
|
|
69
|
+
assert.equal(fuzzy.entry.date_precision, 'unknown')
|
|
70
|
+
assert.equal(fuzzy.entry.date_source, 'relative')
|
|
71
|
+
|
|
72
|
+
const none = await memory.privateRemember({ exec, title: '窗户开着', content: '窗外的雨停了。' })
|
|
73
|
+
assert.equal(none.entry.absolute_date, undefined)
|
|
74
|
+
assert.equal(none.entry.date_precision, undefined)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('an explicit absolute_date overrides prose and an impossible date is refused', async () => {
|
|
78
|
+
const { exec } = session('fields-explicit')
|
|
79
|
+
const { entry } = await memory.privateRemember({
|
|
80
|
+
exec, title: '聚会', content: '她说 明天 见。', absoluteDate: '2026-11-20',
|
|
81
|
+
})
|
|
82
|
+
assert.equal(entry.absolute_date, '2026-11-20')
|
|
83
|
+
assert.equal(entry.date_source, 'explicit')
|
|
84
|
+
await assert.rejects(
|
|
85
|
+
() => memory.privateRemember({ exec, title: '错日期', content: '测试。', absoluteDate: '2026-02-30' }),
|
|
86
|
+
/absolute_date must be YYYY-MM-DD/,
|
|
87
|
+
)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('valid_from / valid_to hide an entry outside its window and never hide old entries without them', async () => {
|
|
91
|
+
const { exec, sessionId } = session('fields-validity')
|
|
92
|
+
const expired = await memory.privateRemember({
|
|
93
|
+
exec, title: '旧地址', content: '她住在旧公寓。', absoluteDate: '2026-10-01', validTo: '2026-10-31',
|
|
94
|
+
})
|
|
95
|
+
const upcoming = await memory.privateRemember({
|
|
96
|
+
exec, title: '新地址', content: '她下个月搬去新公寓。', absoluteDate: '2026-12-01', validFrom: '2026-12-01',
|
|
97
|
+
})
|
|
98
|
+
const current = await memory.privateRemember({
|
|
99
|
+
exec, title: '当前住处', content: '她现在住在河边的房子。', absoluteDate: '2026-11-01', validFrom: '2026-11-01', validTo: '2026-11-30',
|
|
100
|
+
})
|
|
101
|
+
const legacy = await memory.privateRemember({ exec, title: '没有日期的旧条目', content: '窗户是朝东的。' })
|
|
102
|
+
assert.equal(expired.entry.valid_to, '2026-10-31')
|
|
103
|
+
assert.equal(upcoming.entry.valid_from, '2026-12-01')
|
|
104
|
+
|
|
105
|
+
const currentEligible = memory.isMemoryEligible(current.entry, { now: Date.parse('2026-11-06T12:00:00+08:00') })
|
|
106
|
+
assert.equal(currentEligible, true)
|
|
107
|
+
assert.equal(memory.isMemoryEligible(expired.entry, { now: Date.parse('2026-11-06T12:00:00+08:00') }), false)
|
|
108
|
+
assert.equal(memory.isMemoryEligible(upcoming.entry, { now: Date.parse('2026-11-06T12:00:00+08:00') }), false)
|
|
109
|
+
assert.equal(memory.isMemoryEligible(legacy.entry, { now: Date.parse('2026-11-06T12:00:00+08:00') }), true)
|
|
110
|
+
|
|
111
|
+
const recalled = await memory.privateRecall({ exec, query: '公寓 住处 窗户', topK: 10, relevanceThreshold: 0 })
|
|
112
|
+
const ids = recalled.files.map((file) => file.name)
|
|
113
|
+
assert.ok(ids.includes(current.id), 'a valid entry is recalled')
|
|
114
|
+
assert.ok(ids.includes(legacy.id), 'an entry without a window is not filtered')
|
|
115
|
+
assert.ok(!ids.includes(expired.id), 'a past valid_to is filtered')
|
|
116
|
+
assert.ok(!ids.includes(upcoming.id), 'a future valid_from is filtered')
|
|
117
|
+
assert.equal(index(sessionId).entries[expired.id].valid_to, '2026-10-31', 'filtering is read-only')
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('rewriting the text re-derives the date; the query resolves relative words too', async () => {
|
|
121
|
+
const { exec } = session('fields-query')
|
|
122
|
+
const dated = await memory.privateRemember({
|
|
123
|
+
exec, title: '带伞', content: '出门要带伞,午后有雨。', absoluteDate: '2026-11-07',
|
|
124
|
+
})
|
|
125
|
+
// The body never says "明天"; only the resolved date can connect the query to it.
|
|
126
|
+
const recalled = await memory.privateRecall({ exec, query: '明天天气怎么样', topK: 10, relevanceThreshold: 0 })
|
|
127
|
+
assert.ok(recalled.files.some((file) => file.name === dated.id), 'the query resolves 明天 to 2026-11-07')
|
|
128
|
+
|
|
129
|
+
const updated = await memory.privateUpdate({
|
|
130
|
+
exec, id: dated.id, content: '后天出门要带伞,午后有雨。',
|
|
131
|
+
})
|
|
132
|
+
assert.equal(updated.entry.absolute_date, '2026-11-08', 'a rewrite re-resolves the date')
|
|
133
|
+
assert.equal(updated.entry.content, '后天出门要带伞,午后有雨。')
|
|
134
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-resident-'))
|
|
8
|
+
process.env.DSH_MEMU_HOME = home
|
|
9
|
+
|
|
10
|
+
const memu = await import('../lib/memu.js')
|
|
11
|
+
|
|
12
|
+
test.after(() => {
|
|
13
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('the resident singleton is declared before its first executable use', () => {
|
|
17
|
+
const source = fs.readFileSync(new URL('../lib/memu.js', import.meta.url), 'utf8')
|
|
18
|
+
const declaration = /^let residentRecord = null$/m.exec(source)
|
|
19
|
+
assert.ok(declaration, 'lib/memu.js must declare `let residentRecord = null`')
|
|
20
|
+
// Comments may mention the name before the declaration; the first executable use
|
|
21
|
+
// (stopResident) must sit after it, or ESM strict mode throws ReferenceError.
|
|
22
|
+
const firstCodeUse = source.indexOf('residentRecord === record')
|
|
23
|
+
assert.ok(firstCodeUse > declaration.index, 'the first executable use must come after the declaration')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('closing and prewarming the resident engine does not throw ReferenceError', async () => {
|
|
27
|
+
assert.equal(typeof memu.closeMemuResident, 'function')
|
|
28
|
+
assert.equal(memu.closeMemuResident(), 0, 'closing a non-existent resident is a no-op')
|
|
29
|
+
assert.equal(memu.closeMemuResident(), 0, 'close is idempotent')
|
|
30
|
+
// Default route is lexical, so prewarm returns false without spawning. The point is
|
|
31
|
+
// that the function body runs and the module-level singleton is reachable.
|
|
32
|
+
assert.equal(await memu.prewarmMemuResident(), false)
|
|
33
|
+
})
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import {
|
|
4
|
+
globalConflictCheck,
|
|
5
|
+
negationPolarity,
|
|
6
|
+
pairFeatures,
|
|
7
|
+
polarityAt,
|
|
8
|
+
} from '../lib/text-retrieval.js'
|
|
9
|
+
|
|
10
|
+
// —— §4.8 冲突预筛 P1-2:子句级否定作用域(polarityAt)——
|
|
11
|
+
//
|
|
12
|
+
// 整文本 negationPolarity 有两类误报:无关子句的否定污染、双重否定。
|
|
13
|
+
// polarityAt 只判锚点所在子句,标记计数 mod 2,对比词切片段。
|
|
14
|
+
|
|
15
|
+
test('polarityAt: negation in the same clause hits across 12 chars (existing true positive)', () => {
|
|
16
|
+
// 「不」在第 2 字,锚点「告警」在第 12 字附近——同一子句即算否定侧,
|
|
17
|
+
// 这条是既有真阳性(curator-pairing 的告警对),不能丢。
|
|
18
|
+
assert.equal(polarityAt('用户不希望每次部署失败都发告警通知', '告警'), true)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('polarityAt: negation in an unrelated clause does not pollute the anchor clause', () => {
|
|
22
|
+
// 「别」在第二个子句(别忘了),锚点「截止日期」在第一个子句——互不污染。
|
|
23
|
+
assert.equal(polarityAt('项目截止日期是5月1日,别忘了', '截止日期'), false)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('polarityAt: double negation flips back to positive (marker count mod 2)', () => {
|
|
27
|
+
assert.equal(polarityAt('不是不喜欢香菜', '香菜'), false)
|
|
28
|
+
// 多标记句:禁止 → 单个否定 → 否定侧
|
|
29
|
+
assert.equal(polarityAt('禁止在-production 环境跑迁移', 'production'), true)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('polarityAt: contrast structure frees the B side (不是A而是B)', () => {
|
|
33
|
+
// 「不是 A 而是 B」里 B(队列)是肯定侧——这是 P1-2 要修的误报方向。
|
|
34
|
+
assert.equal(polarityAt('不是缓存的问题而是队列的问题', '队列'), false)
|
|
35
|
+
// A 侧(缓存):按对比规则正文「不是 A 而是 B 里否定属于 A」,标记「不」与
|
|
36
|
+
// 「缓存」之间没有对比词,否定仍属于 A → true。任务示例写的是「缓存 → false」,
|
|
37
|
+
// 与规则正文矛盾;这里取规则正文,保住「问题是缓存」 vs 「不是缓存而是队列」
|
|
38
|
+
// 这类真阳性。若要两侧皆 false,把含对比词子句里片段 0 的标记也跳过即可。
|
|
39
|
+
assert.equal(polarityAt('不是缓存的问题而是队列的问题', '缓存'), true)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('polarityAt: 而不是 passes the negation forward to what follows', () => {
|
|
43
|
+
// 「而不是」引出的片段继承一个否定:队列是否定侧、缓存不是——
|
|
44
|
+
// 「用缓存而不是队列」与「统一用缓存」不再误报冲突。
|
|
45
|
+
assert.equal(polarityAt('用缓存而不是队列', '队列'), true)
|
|
46
|
+
assert.equal(polarityAt('用缓存而不是队列', '缓存'), false)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('polarityAt: English markers, word-bounded', () => {
|
|
50
|
+
assert.equal(polarityAt('I no longer play tennis', 'tennis'), true)
|
|
51
|
+
assert.equal(polarityAt('I play tennis every weekend', 'tennis'), false)
|
|
52
|
+
// \b 生效:not 不命中 another 这类词内片段
|
|
53
|
+
assert.equal(polarityAt('another deploy window', 'deploy'), false)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('polarityAt: anchor absent or empty is false; deterministic on repeat', () => {
|
|
57
|
+
assert.equal(polarityAt('用户不希望每次部署失败都发告警通知', '监控'), false, 'anchor not in text')
|
|
58
|
+
assert.equal(polarityAt('用户不希望每次部署失败都发告警通知', ''), false, 'empty anchor')
|
|
59
|
+
const once = polarityAt('项目截止日期是5月1日,别忘了', '截止日期')
|
|
60
|
+
assert.equal(polarityAt('项目截止日期是5月1日,别忘了', '截止日期'), once, 'same input, same output')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('polarityAt: multiple anchor occurrences — any negated clause wins (conservative)', () => {
|
|
64
|
+
// 第一处出现在干净子句、第二处出现在否定子句 → 否定侧。
|
|
65
|
+
assert.equal(polarityAt('部署窗口照旧,别改部署窗口', '部署窗口'), true)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
// —— §4.8 negationPolarity(整文本口径,原样保留)——
|
|
69
|
+
|
|
70
|
+
test('negationPolarity: whole-text ruler unchanged', () => {
|
|
71
|
+
assert.equal(negationPolarity('不发告警'), true)
|
|
72
|
+
assert.equal(negationPolarity('每次都发告警'), false)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
// —— §4.8 globalConflictCheck:锚点化 negation 分支 ——
|
|
76
|
+
|
|
77
|
+
// 误报修复对。实测 pairFeatures:charDice=0.625(≥0.55,走到 negation 分支)、
|
|
78
|
+
// containment=0(不触发豁免)、titleDice=0.333(<0.8,value 分支也不触发)。
|
|
79
|
+
// 旧口径下 a 侧整文本带「别」→ negationPolarity 为 true,b 侧 false → 误报
|
|
80
|
+
// 'negation';新口径锚点(共享 token 里最长的 3 字 trigram)落在干净子句 → null。
|
|
81
|
+
test('globalConflictCheck: negation in an unrelated clause is no longer a conflict', () => {
|
|
82
|
+
const a = { title: '备份日程', content: '项目截止日期是5月1日,别忘了' }
|
|
83
|
+
const b = { title: '备份周期', content: '项目截止日期是5月10日' }
|
|
84
|
+
const features = pairFeatures(a, b)
|
|
85
|
+
// 门槛自证:这条用例确实走进了被改造的 negation 分支,且旧口径会误报。
|
|
86
|
+
assert.ok(features.charDice >= 0.55 && features.charDice < 0.95, `charDice ${features.charDice}`)
|
|
87
|
+
assert.ok(features.containment < 0.92, `containment ${features.containment}`)
|
|
88
|
+
assert.notEqual(negationPolarity(a.content), negationPolarity(b.content), 'old ruler would flag this pair')
|
|
89
|
+
assert.equal(globalConflictCheck(a, b), null)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// 真阳性保持对(与 curator-pairing 既有断言同一对,独立再放一份)。实测
|
|
93
|
+
// charDice=0.7568(≥0.55)、titleDice=0.333;锚点落在唯一子句,b 侧「不」
|
|
94
|
+
// 单标记 → 否定侧,a 侧无标记 → 极性不同 → 仍报 'negation'。
|
|
95
|
+
test('globalConflictCheck: same-clause negation stays a conflict (true positive)', () => {
|
|
96
|
+
const a = { title: '告警偏好', content: '用户希望每次部署失败都发告警通知' }
|
|
97
|
+
const b = { title: '告警设置', content: '用户不希望每次部署失败都发告警通知' }
|
|
98
|
+
const features = pairFeatures(a, b)
|
|
99
|
+
assert.ok(features.charDice >= 0.55 && features.charDice < 0.95, `charDice ${features.charDice}`)
|
|
100
|
+
assert.equal(globalConflictCheck(a, b), 'negation')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// 双重否定对。实测 charDice=0.6875(≥0.55);共享 token 最长者落在「不是
|
|
104
|
+
// 不喜欢」子句(「我对香菜」trigram),标记计数 2 → 偶数翻回肯定 → 双侧同
|
|
105
|
+
// 极性 → 不再误报。旧口径 a 侧整文本带「不」→ 误报 'negation'。
|
|
106
|
+
test('globalConflictCheck: double negation is not a conflict', () => {
|
|
107
|
+
const a = { title: '饮食偏好', content: '我对香菜不是不喜欢,是能接受的' }
|
|
108
|
+
const b = { title: '口味偏好', content: '我对香菜是喜欢,是能接受的' }
|
|
109
|
+
const features = pairFeatures(a, b)
|
|
110
|
+
assert.ok(features.charDice >= 0.55 && features.charDice < 0.95, `charDice ${features.charDice}`)
|
|
111
|
+
assert.ok(features.containment < 0.92, `containment ${features.containment}`)
|
|
112
|
+
assert.notEqual(negationPolarity(a.content), negationPolarity(b.content), 'old ruler would flag this pair')
|
|
113
|
+
assert.equal(globalConflictCheck(a, b), null)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// 无共享锚点回退。实测 charDice=0.8333(≥0.55),但 tokenize 无任何共享 token
|
|
117
|
+
// (ab-cd vs ab / cd、ef-gh vs ef / gh,latin 连接符只在整 token 时保留)→
|
|
118
|
+
// 锚点为空 → 回退整文本 negationPolarity 对比 → b 侧 'not' → 仍报 'negation'。
|
|
119
|
+
test('globalConflictCheck: no shared token falls back to the whole-text ruler', () => {
|
|
120
|
+
const a = { title: 'note', content: 'ab-cd ef gh' }
|
|
121
|
+
const b = { title: 'note2', content: 'ab cd ef-gh not' }
|
|
122
|
+
const features = pairFeatures(a, b)
|
|
123
|
+
assert.ok(features.charDice >= 0.55 && features.charDice < 0.95, `charDice ${features.charDice}`)
|
|
124
|
+
assert.notEqual(negationPolarity(a.content), negationPolarity(b.content), 'b side carries "not"')
|
|
125
|
+
assert.equal(globalConflictCheck(a, b), 'negation')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('globalConflictCheck: deterministic — same pair, same verdict twice', () => {
|
|
129
|
+
const a = { title: '告警偏好', content: '用户希望每次部署失败都发告警通知' }
|
|
130
|
+
const b = { title: '告警设置', content: '用户不希望每次部署失败都发告警通知' }
|
|
131
|
+
assert.equal(globalConflictCheck(a, b), globalConflictCheck(a, b))
|
|
132
|
+
const c = { title: '备份日程', content: '项目截止日期是5月1日,别忘了' }
|
|
133
|
+
const d = { title: '备份周期', content: '项目截止日期是5月10日' }
|
|
134
|
+
assert.deepEqual([globalConflictCheck(c, d), globalConflictCheck(c, d)], [null, null])
|
|
135
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-prompt-128-'))
|
|
8
|
+
process.env.DSH_MEMU_HOME = home
|
|
9
|
+
|
|
10
|
+
const prompts = await import('../lib/prompts.js')
|
|
11
|
+
const { buildWorkerBrief } = await import('../lib/consolidation-worker.js')
|
|
12
|
+
|
|
13
|
+
test.after(() => {
|
|
14
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const policyVars = { shortTermTtlHours: 72, mustRememberCap: 12, unpinBudget: 2, anchorTokens: 800 }
|
|
18
|
+
const guideVars = { mustRememberCap: 12, unpinBudget: 2, shortTermTtlHours: 72, anchorTokens: 800, turnTokens: 800 }
|
|
19
|
+
|
|
20
|
+
test('the RP guide asks for scene-time dates, two versions and speaker separation', () => {
|
|
21
|
+
const text = prompts.loadPromptForDomain('system-guide', guideVars, 'rp').text
|
|
22
|
+
assert.match(text, /相对日期先按这一轮的时间换算/)
|
|
23
|
+
assert.match(text, /记忆里有两版说法时/)
|
|
24
|
+
assert.match(text, /分清是谁说的/)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('the RP policy teaches the S1–S4 write fields', () => {
|
|
28
|
+
const text = prompts.loadPromptForDomain('private-policy', policyVars, 'rp').text
|
|
29
|
+
assert.match(text, /absolute_date/)
|
|
30
|
+
assert.match(text, /date_precision=unknown/)
|
|
31
|
+
assert.match(text, /conflicts_with/)
|
|
32
|
+
assert.match(text, /supersedes/)
|
|
33
|
+
assert.match(text, /commitment/)
|
|
34
|
+
assert.match(text, /assistant_said/)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('the consolidation page and worker brief carry the same field contract', () => {
|
|
38
|
+
const text = prompts.loadPromptForDomain('private-consolidation', {
|
|
39
|
+
libraryName: '测试库', domain: 'rp', mustRememberCap: 12,
|
|
40
|
+
}, 'rp').text
|
|
41
|
+
for (const needle of ['speaker_id', 'source_kind', 'absolute_date', 'conflicts_with', 'commitment']) {
|
|
42
|
+
assert.ok(text.includes(needle), `consolidation prompt is missing ${needle}`)
|
|
43
|
+
}
|
|
44
|
+
const brief = buildWorkerBrief({
|
|
45
|
+
snapshot: { fromSeq: 1, throughSeq: 2, eventCount: 2, hash: 'prompt-128' },
|
|
46
|
+
probe: { tools: ['memu_private_remember', 'memu_private_update'] },
|
|
47
|
+
})
|
|
48
|
+
for (const needle of ['speaker_id', 'source_kind', 'absolute_date', 'conflicts_with', 'supersedes', 'commitment', 'assistant_said', '每窗最多新增 10 条']) {
|
|
49
|
+
assert.ok(brief.includes(needle), `worker brief is missing ${needle}`)
|
|
50
|
+
}
|
|
51
|
+
})
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
|
|
4
|
+
import { detectQueryIntent, decomposeQuery, extractQueryEntities } from '../lib/text-retrieval.js'
|
|
5
|
+
import { intentTopK, recallIntentBudget, RECALL_INTENT_TOPK } from '../lib/budget.js'
|
|
6
|
+
|
|
7
|
+
const NOW = new Date('2026-11-06T04:00:00Z')
|
|
8
|
+
|
|
9
|
+
test('intent detection covers the six recall strategies', () => {
|
|
10
|
+
assert.equal(detectQueryIntent('我们上次说的地板颜色到底哪个对'), 'conflict')
|
|
11
|
+
assert.equal(detectQueryIntent('你答应过我什么时候查机票'), 'commitment')
|
|
12
|
+
assert.equal(detectQueryIntent('那件事你别告诉别人'), 'boundary')
|
|
13
|
+
assert.equal(detectQueryIntent('下周三是几号'), 'date')
|
|
14
|
+
assert.equal(detectQueryIntent('今天有什么安排'), 'date')
|
|
15
|
+
assert.equal(detectQueryIntent('"林眠"住在哪里'), 'entity')
|
|
16
|
+
assert.equal(detectQueryIntent('窗外的雨停了'), 'general')
|
|
17
|
+
assert.equal(detectQueryIntent(''), 'general')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('queries are decomposed into dates, entities and an expanded search string', () => {
|
|
21
|
+
const decomposed = decomposeQuery('下周三跟"林眠"约了几点', null, {
|
|
22
|
+
now: NOW, knownEntities: ['林眠', 'Yuike'],
|
|
23
|
+
})
|
|
24
|
+
assert.equal(decomposed.intent, 'date', 'a time word outranks the quoted entity')
|
|
25
|
+
assert.deepEqual(decomposed.dates, ['2026-11-11'])
|
|
26
|
+
assert.deepEqual(decomposed.entities, ['林眠'])
|
|
27
|
+
assert.match(decomposed.expanded, /2026-11-11/)
|
|
28
|
+
assert.match(decomposed.expanded, /林眠/)
|
|
29
|
+
|
|
30
|
+
const named = decomposeQuery('Yuike 上次提到的地点', null, { now: NOW, knownEntities: ['Yuike', '林眠'] })
|
|
31
|
+
assert.equal(named.intent, 'date', '上次 makes this a time question')
|
|
32
|
+
assert.deepEqual(named.entities, ['Yuike'])
|
|
33
|
+
|
|
34
|
+
const entity = decomposeQuery('"林眠"住在哪里', null, { now: NOW })
|
|
35
|
+
assert.equal(entity.intent, 'entity')
|
|
36
|
+
assert.deepEqual(entity.entities, ['林眠'])
|
|
37
|
+
|
|
38
|
+
assert.deepEqual(decomposeQuery('').dates, [])
|
|
39
|
+
assert.deepEqual(decomposeQuery('', null, { now: NOW }).entities, [])
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('entity extraction only trusts explicit markers or a known-entity table', () => {
|
|
43
|
+
assert.deepEqual(extractQueryEntities('“Yuike”和「林眠」都在'), ['Yuike', '林眠'])
|
|
44
|
+
assert.deepEqual(extractQueryEntities('@yuike 说了什么'), ['yuike'])
|
|
45
|
+
assert.deepEqual(extractQueryEntities('Yuike 说了什么', { knownEntities: ['Yuike'] }), ['Yuike'])
|
|
46
|
+
assert.deepEqual(extractQueryEntities('一个普通句子'), [])
|
|
47
|
+
assert.deepEqual(extractQueryEntities('“x”'), [], 'one character is not an entity handle')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('intent budgets raise the candidate count without overriding a larger explicit topK', () => {
|
|
51
|
+
assert.deepEqual(RECALL_INTENT_TOPK, { general: 5, date: 8, entity: 8, conflict: 6, commitment: 5, boundary: 5 })
|
|
52
|
+
assert.equal(intentTopK('general', 3), 3)
|
|
53
|
+
assert.equal(intentTopK('general', 0), 5)
|
|
54
|
+
assert.equal(intentTopK('date', 3), 8)
|
|
55
|
+
assert.equal(intentTopK('entity', 5), 8)
|
|
56
|
+
assert.equal(intentTopK('conflict', 5), 6)
|
|
57
|
+
assert.equal(intentTopK('date', 10), 10, 'a deliberate larger request is kept')
|
|
58
|
+
assert.equal(intentTopK('date', 50), 20, 'but never unbounded')
|
|
59
|
+
assert.equal(intentTopK('unknown', 4), 4)
|
|
60
|
+
assert.deepEqual(recallIntentBudget('conflict', { privateTopK: 4 }), { intent: 'conflict', topK: 6 })
|
|
61
|
+
assert.deepEqual(recallIntentBudget('nope', { privateTopK: 2 }), { intent: 'general', topK: 2 })
|
|
62
|
+
})
|