dsh-memu-memory 1.3.0 → 1.3.2
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/CHANGELOG.md +50 -0
- package/README.md +3 -3
- package/README.zh.md +3 -1
- package/docs/contributing.md +2 -2
- package/docs/env-and-commands.md +12 -1
- package/docs/memory-semantics.md +8 -2
- package/docs/troubleshooting.md +9 -6
- package/lib/auto-link/apply.js +249 -0
- package/lib/auto-link/candidates.js +155 -0
- package/lib/auto-link/decide.js +304 -0
- package/lib/auto-link/normalize.js +99 -0
- package/lib/auto-link/policy.js +42 -0
- package/lib/auto-link/suppression.js +101 -0
- package/lib/auto-link/terminology.js +265 -0
- package/lib/binding-diagnostics.js +3 -3
- package/lib/client.js +1435 -70
- package/lib/consolidation-receipt.js +81 -5
- package/lib/consolidation-worker.js +57 -25
- package/lib/curator.js +91 -11
- package/lib/engine.js +13 -3
- package/lib/graph.js +326 -39
- package/lib/inject.js +35 -15
- package/lib/media.js +8 -1
- package/lib/memory.js +27 -1
- package/lib/memu.js +26 -12
- package/lib/private-links.js +105 -137
- package/lib/private.js +280 -53
- package/lib/prompts/global-curator.md +6 -3
- package/lib/prompts/system-guide.md +4 -5
- package/lib/prompts.js +27 -1
- package/lib/rpc.js +69 -9
- package/lib/state.js +23 -4
- package/lib/tools.js +199 -67
- package/lib/webui/graph-LICENSE.txt +1 -62
- package/lib/webui/graph-view.bundle.js +1 -1
- package/lib/webui/graph-view.js +891 -124
- package/lib/webui/standalone.js +3 -3
- package/package.json +1 -2
- package/scripts/build-graph.mjs +3 -3
- package/scripts/check-graph-browser.mjs +106 -23
- package/scripts/rebuild-auto-links.mjs +252 -0
- package/tests/audit-regression.test.mjs +11 -8
- package/tests/auto-link-candidates.test.mjs +161 -0
- package/tests/auto-link-decide.test.mjs +512 -0
- package/tests/auto-link-migration.test.mjs +133 -0
- package/tests/client-render.test.mjs +7 -6
- package/tests/consolidation-credential.test.mjs +2 -2
- package/tests/consolidation-fork.test.mjs +188 -9
- package/tests/consolidation-ledger.test.mjs +31 -0
- package/tests/consolidation-snapshot.test.mjs +9 -8
- package/tests/curator-probe.test.mjs +32 -2
- package/tests/cycle.test.mjs +1 -1
- package/tests/graph-view.test.mjs +163 -21
- package/tests/graph.test.mjs +26 -0
- package/tests/help-page.test.mjs +3 -3
- package/tests/inject-conflict.test.mjs +1 -1
- package/tests/inject.test.mjs +1 -1
- package/tests/injection-chain.test.mjs +2 -2
- package/tests/injection-rollback.test.mjs +1 -1
- package/tests/memory-chain.test.mjs +28 -0
- package/tests/memu-resident-route.test.mjs +130 -0
- package/tests/mock-llm.test.mjs +1 -1
- package/tests/private-links.test.mjs +81 -45
- package/tests/prompts.test.mjs +1 -1
- package/tests/stubs/child-process-spy.mjs +14 -0
- package/tests/tool-schema.test.mjs +2 -1
- package/tests/tools.test.mjs +49 -21
|
@@ -0,0 +1,161 @@
|
|
|
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-candidates-'))
|
|
8
|
+
process.env.DSH_MEMU_HOME = home
|
|
9
|
+
process.env.DSH_MEMU_MEMORY_HOME = home
|
|
10
|
+
|
|
11
|
+
const state = await import('../lib/state.js')
|
|
12
|
+
const graph = await import('../lib/graph.js')
|
|
13
|
+
const rpc = await import('../lib/rpc.js')
|
|
14
|
+
const terminology = await import('../lib/auto-link/terminology.js')
|
|
15
|
+
const candidates = await import('../lib/auto-link/candidates.js')
|
|
16
|
+
|
|
17
|
+
test.after(() => {
|
|
18
|
+
try { state.flushState() } catch {}
|
|
19
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
let counter = 0
|
|
23
|
+
function library(name) {
|
|
24
|
+
counter += 1
|
|
25
|
+
return state.ensureSession(`cand-${name}-${counter}`, { cwd: path.join(home, `ws-${name}-${counter}`), agentPreset: 'rp' }).libraryId
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function entry(title, content, extra = {}) {
|
|
29
|
+
return { title, content, status: 'active', visibility: 'shared', layer: 'long_term', createdAt: '2026-11-01T00:00:00.000Z', updatedAt: '2026-11-01T00:00:00.000Z', ...extra }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function fixture() {
|
|
33
|
+
return {
|
|
34
|
+
'mem-1': entry('「小柚」今天来了', '她带着伞。'),
|
|
35
|
+
'mem-2': entry('小柚的伞', '小柚把伞放在门口。'),
|
|
36
|
+
'mem-3': entry('她叫阿兰', '阿兰是隔壁的邻居。'),
|
|
37
|
+
'mem-4': entry('yuike_01 上线', 'yuike_01 又迟到了。'),
|
|
38
|
+
'mem-5': entry('星港夜班', '今日星港开始检修,傍晚星港的风很大。'),
|
|
39
|
+
'mem-6': entry('星港入口', '星港入口风很大。'),
|
|
40
|
+
'mem-7': entry('值班记录', '这个记录写得很乱,这个本子也旧了。'),
|
|
41
|
+
'mem-8': entry('阿兰的猫', '阿兰养了一只很凶的猫。'),
|
|
42
|
+
'mem-9': entry('yuike_01 的工位', 'yuike_01 把工位收拾得很干净。'),
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
test('term statistics carry df/titleDf/boundary/cohesion for real names and drop glue bigrams', () => {
|
|
47
|
+
const rows = Object.entries(fixture()).map(([id, item]) => ({ id, title: item.title, text: `${item.title}\n${item.content}` }))
|
|
48
|
+
const stats = terminology.buildTermStats(rows)
|
|
49
|
+
const you = stats.get('小柚')
|
|
50
|
+
assert.ok(you, '小柚 is a repeated bigram')
|
|
51
|
+
assert.equal(you.df, 2)
|
|
52
|
+
assert.equal(you.titleDf, 2)
|
|
53
|
+
assert.ok(you.avLeft >= 2 && you.avRight >= 2, 'both boundaries vary: ' + you.avLeft + '/' + you.avRight)
|
|
54
|
+
assert.ok(you.freedom >= 2)
|
|
55
|
+
const alan = stats.get('阿兰')
|
|
56
|
+
assert.ok(alan && alan.df >= 2)
|
|
57
|
+
const star = stats.get('星港')
|
|
58
|
+
assert.ok(star && star.df >= 2 && star.tf >= 4)
|
|
59
|
+
assert.equal(terminology.looksGeneric('这个'), true, 'function characters disqualify a surface')
|
|
60
|
+
assert.equal(terminology.looksGeneric('一条'), true, 'measure-word templates disqualify a surface')
|
|
61
|
+
assert.equal(terminology.looksGeneric('小柚'), false)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('ranking puts evidenced terms in tier 1 and pure statistics in tier 2', () => {
|
|
65
|
+
const entries = Object.entries(fixture()).map(([id, item]) => ({ id, title: item.title, text: `${item.title}\n${item.content}` }))
|
|
66
|
+
const stats = terminology.buildTermStats(entries)
|
|
67
|
+
const ranked = terminology.rankCandidates(entries, stats)
|
|
68
|
+
const tier1 = ranked.tier1.map(row => row.term)
|
|
69
|
+
const tier2 = ranked.tier2.map(row => row.term)
|
|
70
|
+
assert.ok(tier1.includes('小柚'), 'quoted name is tier 1: ' + tier1.join(','))
|
|
71
|
+
assert.ok(tier1.includes('阿兰'), 'name-intro is tier 1: ' + tier1.join(','))
|
|
72
|
+
assert.ok(tier1.includes('yuike_01'), 'mixed identifier is tier 1: ' + tier1.join(','))
|
|
73
|
+
assert.ok(tier2.includes('星港'), 'statistical-only name stays tier 2: ' + tier2.join(','))
|
|
74
|
+
assert.equal([...tier1, ...tier2].includes('这个'), false, 'glue bigram never becomes a candidate')
|
|
75
|
+
assert.equal([...tier1, ...tier2].includes('2026'), false)
|
|
76
|
+
assert.ok(ranked.tier1.find(row => row.term === '小柚').quoted)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('the queue snapshot persists, maps existing entities and stays stable across reads', () => {
|
|
80
|
+
const lib = library('snapshot')
|
|
81
|
+
state.savePrivateIndex(lib, { version: 1, entries: fixture() })
|
|
82
|
+
const alan = graph.createEntity({ libraryId: lib, name: '阿兰', kind: 'person' })
|
|
83
|
+
const first = candidates.generateAnchorCandidates({ libraryId: lib })
|
|
84
|
+
assert.ok(fs.existsSync(candidates.candidatesFile(lib)), 'the derived snapshot is written next to the index')
|
|
85
|
+
const row = first.tiers.tier1.find(item => item.term === '阿兰')
|
|
86
|
+
assert.ok(row, '阿兰 is nominated')
|
|
87
|
+
assert.equal(row.entityId, alan.id, 'the row points at the existing entity')
|
|
88
|
+
assert.equal(row.entityStatus, 'provisional')
|
|
89
|
+
assert.ok(first.entities.provisional.some(item => item.id === alan.id))
|
|
90
|
+
assert.equal(first.entities.confirmed.length, 0)
|
|
91
|
+
assert.ok(first.totals.memories >= 7)
|
|
92
|
+
const second = candidates.generateAnchorCandidates({ libraryId: lib })
|
|
93
|
+
assert.deepEqual(second, first, 'an unchanged library returns the cached snapshot')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('RPC confirm flips status and backfills mention edges in one action', async () => {
|
|
97
|
+
const lib = library('confirm-rpc')
|
|
98
|
+
state.savePrivateIndex(lib, { version: 1, entries: {
|
|
99
|
+
'mem-a': entry('阿兰来了', '阿兰坐在门口。'),
|
|
100
|
+
'mem-b': entry('阿兰的信', '阿兰寄来一封信。'),
|
|
101
|
+
} })
|
|
102
|
+
const alan = graph.createEntity({ libraryId: lib, name: '阿兰', kind: 'person' })
|
|
103
|
+
const queue = await rpc.dispatch({}, 'getAnchorCandidates', { libraryId: lib })
|
|
104
|
+
const rows = [...queue.tiers.tier1, ...queue.tiers.tier2]
|
|
105
|
+
assert.ok(rows.some(row => row.term === '阿兰' && row.entityId === alan.id), 'queue exposes the entity link')
|
|
106
|
+
const result = await rpc.dispatch({}, 'confirmEntities', { libraryId: lib, entityIds: [alan.id] })
|
|
107
|
+
assert.deepEqual(result.confirmed, [alan.id])
|
|
108
|
+
assert.equal(result.entities, 2)
|
|
109
|
+
const index = state.loadPrivateIndex(lib)
|
|
110
|
+
for (const id of ['mem-a', 'mem-b']) {
|
|
111
|
+
assert.ok((index.entries[id].links || []).some(link => link.to === alan.id && link.evidence?.rule === 'entity-mention'), id + ' gained a mention edge')
|
|
112
|
+
}
|
|
113
|
+
const store = graph.graphExport(lib)
|
|
114
|
+
assert.equal(store.entities[alan.id].status, 'confirmed')
|
|
115
|
+
assert.equal(store.entities[alan.id].origin, 'user')
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('RPC createEntity confirms the new row and attaches mentions immediately', async () => {
|
|
119
|
+
const lib = library('create-rpc')
|
|
120
|
+
state.savePrivateIndex(lib, { version: 1, entries: {
|
|
121
|
+
'mem-a': entry('周宁来了', '周宁坐在窗边。'),
|
|
122
|
+
} })
|
|
123
|
+
const created = await rpc.dispatch({}, 'createEntity', { libraryId: lib, name: '周宁', kind: 'person' })
|
|
124
|
+
assert.equal(created.status, 'confirmed')
|
|
125
|
+
assert.equal(created.origin, 'user')
|
|
126
|
+
const index = state.loadPrivateIndex(lib)
|
|
127
|
+
assert.ok((index.entries['mem-a'].links || []).some(link => link.to === created.id && link.evidence?.rule === 'entity-mention'))
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('legacy grading keeps asserted rows and downgrades everything else to provisional', () => {
|
|
131
|
+
const lib = library('grade')
|
|
132
|
+
const a = graph.createEntity({ libraryId: lib, name: '周宁', kind: 'person' })
|
|
133
|
+
const b = graph.createEntity({ libraryId: lib, name: '群里', kind: 'person' })
|
|
134
|
+
const c = graph.createEntity({ libraryId: lib, name: '许哥', kind: 'person' })
|
|
135
|
+
state.savePrivateIndex(lib, { version: 1, entries: {
|
|
136
|
+
'mem-a': entry('旧记录', '旧内容。', { links: [{ to: c.id, kind: 'person', note: '用户手动确认过的实体引用。', confidence: 0.9 }] }),
|
|
137
|
+
} })
|
|
138
|
+
// Simulate pre-status rows: strip the fields createEntity now writes.
|
|
139
|
+
const file = path.join(home, 'index', `${lib}.entities.json`)
|
|
140
|
+
const store = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
141
|
+
for (const id of [a.id, b.id, c.id]) {
|
|
142
|
+
delete store.entities[id].status
|
|
143
|
+
delete store.entities[id].confirmedAt
|
|
144
|
+
delete store.entities[id].confirmedBy
|
|
145
|
+
delete store.entities[id].origin
|
|
146
|
+
}
|
|
147
|
+
store.entities[a.id].origin = 'user'
|
|
148
|
+
fs.writeFileSync(file, JSON.stringify(store, null, 2), 'utf8')
|
|
149
|
+
|
|
150
|
+
const dry = graph.gradeLegacyEntities({ libraryId: lib, apply: false })
|
|
151
|
+
assert.equal(dry.scanned, 3)
|
|
152
|
+
assert.equal(dry.changed, 0)
|
|
153
|
+
assert.equal(JSON.parse(fs.readFileSync(file, 'utf8')).entities[a.id].status, undefined, 'dry run writes nothing')
|
|
154
|
+
|
|
155
|
+
const applied = graph.gradeLegacyEntities({ libraryId: lib, apply: true })
|
|
156
|
+
assert.equal(applied.changed, 3)
|
|
157
|
+
const graded = JSON.parse(fs.readFileSync(file, 'utf8')).entities
|
|
158
|
+
assert.equal(graded[a.id].status, 'confirmed', 'origin protection')
|
|
159
|
+
assert.equal(graded[c.id].status, 'confirmed', 'a non-auto inbound link protects the row')
|
|
160
|
+
assert.equal(graded[b.id].status, 'provisional', 'garbage without evidence stays provisional')
|
|
161
|
+
})
|