dsh-memu-memory 1.3.1 → 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 +27 -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 +2 -2
- package/lib/client.js +1412 -69
- package/lib/graph.js +269 -18
- package/lib/memory.js +11 -0
- package/lib/memu.js +7 -6
- package/lib/private-links.js +105 -137
- package/lib/private.js +138 -38
- package/lib/rpc.js +67 -7
- package/lib/state.js +7 -2
- package/lib/tools.js +4 -2
- 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-fork.test.mjs +148 -0
- package/tests/cycle.test.mjs +1 -1
- package/tests/graph-view.test.mjs +163 -21
- package/tests/graph.test.mjs +5 -0
- package/tests/help-page.test.mjs +3 -3
- package/tests/memu-resident-route.test.mjs +130 -0
- package/tests/private-links.test.mjs +81 -45
- package/tests/stubs/child-process-spy.mjs +14 -0
- package/tests/tools.test.mjs +24 -18
|
@@ -19,6 +19,10 @@ const {
|
|
|
19
19
|
inferEntityKind,
|
|
20
20
|
} = privateLinks
|
|
21
21
|
|
|
22
|
+
// 旧写路径的用例验证 deterministic 模式下的行为;默认的 suggest(只出候选)
|
|
23
|
+
// 与 off 由文件末尾的专门用例覆盖。
|
|
24
|
+
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
25
|
+
|
|
22
26
|
test.after(() => {
|
|
23
27
|
try { state.flushState() } catch {}
|
|
24
28
|
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
@@ -301,25 +305,22 @@ test('an existing entity with a conflicting kind blocks the auto-link and become
|
|
|
301
305
|
|
|
302
306
|
// ---- 应用层 ----
|
|
303
307
|
|
|
304
|
-
test('
|
|
308
|
+
test('deterministic mode links mentions of a confirmed entity and never writes lexical pairs', async () => {
|
|
305
309
|
activity.clearEvents()
|
|
306
310
|
const binding = bind('links-apply')
|
|
307
311
|
const libraryId = binding.libraryId
|
|
308
|
-
const yuike = graph.createEntity({ libraryId, name: 'Yuike', kind: 'person' })
|
|
312
|
+
const yuike = graph.createEntity({ libraryId, name: 'Yuike', kind: 'person', status: 'confirmed', confirmedBy: 'user' })
|
|
309
313
|
state.savePrivateIndex(libraryId, snapshot({
|
|
310
314
|
'mem-a': entry('猫娘yuike', 'yuike 是一只住在窗台的猫娘,喜欢在午后晒太阳。'),
|
|
311
315
|
'mem-b': entry('yuike名字的由来', '这个名字来自小时候养过的一只白猫,后来一直被沿用。'),
|
|
312
316
|
}))
|
|
313
317
|
|
|
314
|
-
const result = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-apply', ids: ['mem-a'] })
|
|
315
|
-
assert.equal(result.linked,
|
|
316
|
-
assert.equal(result.entities, 2)
|
|
318
|
+
const result = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-apply', ids: ['mem-a', 'mem-b'] })
|
|
319
|
+
assert.equal(result.linked, 0, 'the old lexical memory↔memory edge is never written')
|
|
320
|
+
assert.equal(result.entities, 2, 'both memories mention the confirmed entity')
|
|
317
321
|
assert.equal(result.suggested, 0)
|
|
318
322
|
assert.deepEqual(result.candidates, [])
|
|
319
|
-
// The compact return the worker tool forwards: edges only, no bodies or notes, and
|
|
320
|
-
// one entry per write (the memory pair first, then both hub links).
|
|
321
323
|
assert.deepEqual(result.autoLinked.map(item => `${item.from}->${item.to}:${item.kind}`), [
|
|
322
|
-
'mem-a->mem-b:relation',
|
|
323
324
|
`mem-a->${yuike.id}:person`,
|
|
324
325
|
`mem-b->${yuike.id}:person`,
|
|
325
326
|
])
|
|
@@ -327,64 +328,51 @@ test('autoLinkPrivateMemories applies the link, reuses Yuike and is idempotent',
|
|
|
327
328
|
|
|
328
329
|
const index = state.loadPrivateIndex(libraryId)
|
|
329
330
|
const outbound = index.entries['mem-a'].links || []
|
|
330
|
-
|
|
331
|
-
assert.ok(memoryLink, 'the high-confidence memory link is persisted')
|
|
332
|
-
assert.equal(memoryLink.kind, 'relation')
|
|
333
|
-
assert.equal(memoryLink.source, 'auto', 'the auto source is persisted on the link record')
|
|
334
|
-
assert.match(memoryLink.note, /自动关联/)
|
|
335
|
-
assert.match(memoryLink.note, /yuike/)
|
|
336
|
-
assert.ok(memoryLink.confidence >= 0.6 && memoryLink.confidence <= 0.78)
|
|
331
|
+
assert.equal(outbound.some(link => link.to === 'mem-b'), false, 'no lexical pair edge survives')
|
|
337
332
|
const entityLink = outbound.find(link => link.to === yuike.id)
|
|
338
|
-
assert.ok(entityLink, 'the
|
|
333
|
+
assert.ok(entityLink, 'the confirmed-entity mention is persisted')
|
|
339
334
|
assert.equal(entityLink.kind, 'person')
|
|
340
335
|
assert.equal(entityLink.source, 'auto')
|
|
341
|
-
assert.
|
|
342
|
-
assert.equal(entityLink.
|
|
343
|
-
assert.
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
assert.
|
|
348
|
-
assert.equal(entities[0].id, yuike.id)
|
|
336
|
+
assert.equal(entityLink.confidence, 1, 'deterministic edges are confidence 1.0')
|
|
337
|
+
assert.equal(entityLink.schemaVersion, 2)
|
|
338
|
+
assert.equal(entityLink.evidence.rule, 'entity-mention')
|
|
339
|
+
assert.equal(entityLink.evidence.entityId, yuike.id)
|
|
340
|
+
assert.equal(entityLink.evidence.surface, 'Yuike')
|
|
341
|
+
assert.equal(entityLink.evidence.field, 'title')
|
|
342
|
+
assert.match(entityLink.note, /已确认实体/)
|
|
349
343
|
|
|
350
344
|
const events = activity.listEvents({ kind: 'write', sessionId: 'links-apply' })
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
assert.
|
|
354
|
-
assert.equal(
|
|
355
|
-
assert.deepEqual(new Set(linkEvent.refs), new Set(['mem-a', 'mem-b']))
|
|
356
|
-
assert.equal(entityEvents.length, 2)
|
|
357
|
-
assert.ok(entityEvents.every(event => event.detail.source === 'auto'))
|
|
358
|
-
assert.ok(entityEvents.every(event => event.detail.kind === 'person'))
|
|
345
|
+
const applyEvent = events.find(event => event.detail?.op === 'auto-link-apply' && event.ok)
|
|
346
|
+
assert.ok(applyEvent, 'the deterministic write is recorded in the activity log')
|
|
347
|
+
assert.equal(applyEvent.detail.added, 2)
|
|
348
|
+
assert.equal(applyEvent.detail.entity, 2)
|
|
359
349
|
|
|
360
350
|
activity.clearEvents()
|
|
361
|
-
const again = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-apply', ids: ['mem-a'] })
|
|
362
|
-
assert.equal(again.linked, 0
|
|
363
|
-
assert.equal(again.entities, 0, '
|
|
351
|
+
const again = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-apply', ids: ['mem-a', 'mem-b'] })
|
|
352
|
+
assert.equal(again.linked, 0)
|
|
353
|
+
assert.equal(again.entities, 0, 'an unchanged pass is a no-op')
|
|
354
|
+
assert.deepEqual(again.autoLinked, [])
|
|
364
355
|
const after = state.loadPrivateIndex(libraryId)
|
|
365
|
-
assert.equal((after.entries['mem-a'].links || []).filter(link => link.to === 'mem-b').length, 1)
|
|
366
356
|
assert.equal((after.entries['mem-a'].links || []).filter(link => link.to === yuike.id).length, 1)
|
|
367
357
|
})
|
|
368
358
|
|
|
369
|
-
test('
|
|
359
|
+
test('a confirmed non-person entity produces relation-kind mention edges', async () => {
|
|
370
360
|
activity.clearEvents()
|
|
371
361
|
const binding = bind('links-thing')
|
|
372
362
|
const libraryId = binding.libraryId
|
|
363
|
+
const nebula = graph.createEntity({ libraryId, name: 'nebula', kind: 'thing', status: 'confirmed', confirmedBy: 'user' })
|
|
373
364
|
state.savePrivateIndex(libraryId, snapshot({
|
|
374
365
|
'mem-c': entry('nebula kickoff', 'the nebula survey starts at dawn near the outer ring.'),
|
|
375
366
|
'mem-d': entry('nebula notes', 'a nebula chart was folded inside the old folder.'),
|
|
376
367
|
}))
|
|
377
|
-
const result = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-thing', ids: ['mem-c'] })
|
|
378
|
-
assert.equal(result.linked,
|
|
368
|
+
const result = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-thing', ids: ['mem-c', 'mem-d'] })
|
|
369
|
+
assert.equal(result.linked, 0)
|
|
379
370
|
assert.equal(result.entities, 2)
|
|
380
|
-
const entity = Object.values(graph.graphExport(libraryId).entities)
|
|
381
|
-
.find(item => normalizeEntityName(item.name) === 'nebula')
|
|
382
|
-
assert.ok(entity)
|
|
383
|
-
assert.equal(entity.kind, 'thing')
|
|
384
371
|
const index = state.loadPrivateIndex(libraryId)
|
|
385
|
-
const link = (index.entries['mem-c'].links || []).find(item => item.to ===
|
|
372
|
+
const link = (index.entries['mem-c'].links || []).find(item => item.to === nebula.id)
|
|
386
373
|
assert.ok(link)
|
|
387
|
-
assert.equal(link.kind, 'relation', 'non-person
|
|
374
|
+
assert.equal(link.kind, 'relation', 'non-person entities use kind relation')
|
|
375
|
+
assert.equal(link.evidence.rule, 'entity-mention')
|
|
388
376
|
})
|
|
389
377
|
|
|
390
378
|
test('autoLinkPrivateMemories never throws on a missing or broken index and logs a warning path', async () => {
|
|
@@ -437,6 +425,54 @@ test('autoLinkPrivateMemories never throws on a missing or broken index and logs
|
|
|
437
425
|
assert.equal(activity.listEvents({ kind: 'write' }).length, before, 'no ids means no work and no log row')
|
|
438
426
|
})
|
|
439
427
|
|
|
428
|
+
test('suggest is the default mode and the only valid values are suggest/deterministic/off', () => {
|
|
429
|
+
assert.equal(state.DEFAULT_SETTINGS.autoLinkMode, 'suggest', 'migration starts with the write gate closed')
|
|
430
|
+
assert.equal(state.DEFAULT_SETTINGS.allowWorkerAssertion, false, 'worker assertion is reserved and off')
|
|
431
|
+
assert.throws(() => state.updateSettings({ autoLinkMode: 'auto' }), /invalid setting autoLinkMode/)
|
|
432
|
+
assert.throws(() => state.updateSettings({ allowWorkerAssertion: 'yes' }), /must be boolean/)
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
test('suggest mode returns candidates without writing edges or creating entities', async () => {
|
|
436
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
437
|
+
try {
|
|
438
|
+
const binding = bind('links-suggest')
|
|
439
|
+
const libraryId = binding.libraryId
|
|
440
|
+
state.savePrivateIndex(libraryId, snapshot({
|
|
441
|
+
'mem-a': entry('星港入口', '深夜的星港入口很冷,风从卸货区一直灌进来。'),
|
|
442
|
+
'mem-b': entry('星港夜班', '星港的夜班登记表还压在值班室桌上。'),
|
|
443
|
+
}))
|
|
444
|
+
const result = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-suggest', ids: ['mem-a'] })
|
|
445
|
+
assert.equal(result.linked, 0, 'suggest never writes a memory edge')
|
|
446
|
+
assert.equal(result.entities, 0, 'suggest never creates or links an entity')
|
|
447
|
+
assert.deepEqual(result.autoLinked, [])
|
|
448
|
+
assert.equal(result.candidates.length, 1, 'the uncertain pair is still returned for adjudication')
|
|
449
|
+
assert.equal(result.suggested, 1)
|
|
450
|
+
const index = state.loadPrivateIndex(libraryId)
|
|
451
|
+
assert.equal(index.entries['mem-a'].links, undefined, 'index is untouched')
|
|
452
|
+
assert.deepEqual(Object.keys(graph.graphExport(libraryId).entities), [], 'no entity is created')
|
|
453
|
+
} finally {
|
|
454
|
+
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
455
|
+
}
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
test('off mode skips the suggestion pass entirely', async () => {
|
|
459
|
+
state.updateSettings({ autoLinkMode: 'off' })
|
|
460
|
+
try {
|
|
461
|
+
const binding = bind('links-off')
|
|
462
|
+
const libraryId = binding.libraryId
|
|
463
|
+
state.savePrivateIndex(libraryId, snapshot({
|
|
464
|
+
'mem-a': entry('猫娘yuike', 'yuike 是一只住在窗台的猫娘,喜欢在午后晒太阳。'),
|
|
465
|
+
'mem-b': entry('yuike名字的由来', '这个名字来自小时候养过的一只白猫,后来一直被沿用。'),
|
|
466
|
+
}))
|
|
467
|
+
const result = await autoLinkPrivateMemories({ libraryId, sessionId: 'links-off', ids: ['mem-a'] })
|
|
468
|
+
assert.deepEqual(result, ZERO)
|
|
469
|
+
assert.equal(state.loadPrivateIndex(libraryId).entries['mem-a'].links, undefined)
|
|
470
|
+
assert.deepEqual(Object.keys(graph.graphExport(libraryId).entities), [])
|
|
471
|
+
} finally {
|
|
472
|
+
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
473
|
+
}
|
|
474
|
+
})
|
|
475
|
+
|
|
440
476
|
test('the default export exposes the whole module surface', () => {
|
|
441
477
|
assert.equal(privateLinks.default.suggestPrivateLinks, suggestPrivateLinks)
|
|
442
478
|
assert.equal(privateLinks.default.autoLinkPrivateMemories, autoLinkPrivateMemories)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Only lib/memu.js is routed here (see tests/memu-resident-route.test.mjs), and
|
|
2
|
+
// only `spawn` is intercepted: the test injects a fake child that speaks the
|
|
3
|
+
// resident worker protocol. Everything else, and every other importer in the
|
|
4
|
+
// process, still gets the real child_process.
|
|
5
|
+
import real from 'node:child_process'
|
|
6
|
+
|
|
7
|
+
export * from 'node:child_process'
|
|
8
|
+
|
|
9
|
+
export const spawn = (...args) => {
|
|
10
|
+
const spy = globalThis.__dshMemuSpawnSpy
|
|
11
|
+
return typeof spy === 'function' ? spy(...args) : real.spawn(...args)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default { ...real, spawn }
|
package/tests/tools.test.mjs
CHANGED
|
@@ -160,11 +160,13 @@ test('lexicon propose writes an active entry immediately; search only sees activ
|
|
|
160
160
|
dispose()
|
|
161
161
|
})
|
|
162
162
|
|
|
163
|
-
test('link suggest
|
|
163
|
+
test('link suggest refreshes deterministic mention edges and returns only uncertain candidates', async () => {
|
|
164
164
|
const { tools, dispose } = fakeCtx()
|
|
165
165
|
const cwd = path.join(home, 'project-links')
|
|
166
166
|
const execLinks = { agent: { session: { header: { id: 'tools-links', cwd, agentPreset: 'rp' } } } }
|
|
167
167
|
const binding = state.ensureSession('tools-links', { cwd, agentPreset: 'rp' })
|
|
168
|
+
const graph = await import('../lib/graph.js')
|
|
169
|
+
const yuike = graph.createEntity({ libraryId: binding.libraryId, name: 'yuike', kind: 'person', status: 'confirmed', confirmedBy: 'user' })
|
|
168
170
|
state.savePrivateIndex(binding.libraryId, { version: 1, entries: {
|
|
169
171
|
'mem-a': { title: '猫娘yuike', content: 'yuike 是一只住在窗台的猫娘,喜欢晒太阳。', status: 'active', visibility: 'shared', layer: 'long_term' },
|
|
170
172
|
'mem-b': { title: 'yuike名字的由来', content: '这个名字来自小时候养过的一只白猫。', status: 'active', visibility: 'shared', layer: 'long_term' },
|
|
@@ -179,24 +181,28 @@ test('link suggest applies high-confidence links and returns only uncertain cand
|
|
|
179
181
|
const empty = await suggest.execute({ ids: [] }, execLinks)
|
|
180
182
|
assert.match(empty, /1-20/)
|
|
181
183
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
assert.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
184
|
+
updateSettings({ autoLinkMode: 'deterministic' })
|
|
185
|
+
try {
|
|
186
|
+
const payload = JSON.parse(await suggest.execute({ ids: ['mem-a', 'mem-b'] }, execLinks))
|
|
187
|
+
assert.deepEqual(Object.keys(payload).sort(), ['autoLinked', 'candidates'], 'the tool answers with the two-field contract only')
|
|
188
|
+
assert.deepEqual(payload.candidates, [], 'the yuike pair is a deterministic mention, not an uncertain candidate')
|
|
189
|
+
assert.equal(payload.autoLinked.length, 2, 'one mention edge per memory, no lexical pair edge')
|
|
190
|
+
for (const edge of payload.autoLinked) {
|
|
191
|
+
assert.deepEqual(Object.keys(edge).sort(), ['from', 'kind', 'to'], 'no bodies or notes in the compact payload')
|
|
192
|
+
assert.equal(edge.to, yuike.id)
|
|
193
|
+
assert.equal(edge.kind, 'person')
|
|
194
|
+
}
|
|
195
|
+
// The deterministic edge survives into the graph the UI reads.
|
|
196
|
+
const view = graph.getGraph({ libraryId: binding.libraryId, focusId: 'mem-a', depth: 1 })
|
|
197
|
+
assert.ok(view.edges.some((edge) => edge.from === 'mem-a' && edge.to === yuike.id && edge.source === 'auto'))
|
|
195
198
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
199
|
+
// Running it again is idempotent: no new links, same empty candidate list.
|
|
200
|
+
const rerun = JSON.parse(await suggest.execute({ ids: ['mem-a', 'mem-b'] }, execLinks))
|
|
201
|
+
assert.deepEqual(rerun.autoLinked, [])
|
|
202
|
+
assert.deepEqual(rerun.candidates, [])
|
|
203
|
+
} finally {
|
|
204
|
+
updateSettings({ autoLinkMode: 'suggest' })
|
|
205
|
+
}
|
|
200
206
|
dispose()
|
|
201
207
|
})
|
|
202
208
|
|