dsh-memu-memory 1.3.7 → 1.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +1 -1
- package/CHANGELOG.md +58 -0
- package/README.md +7 -7
- package/README.zh.md +7 -7
- package/docs/client-conventions.md +1 -0
- package/docs/contributing.md +2 -2
- package/docs/memory-semantics.md +7 -6
- package/docs/troubleshooting.md +74 -7
- package/lib/binding-diagnostics.js +2 -2
- package/lib/client.js +150 -81
- package/lib/consolidation-ledger.js +6 -2
- package/lib/inbound.js +24 -9
- package/lib/inject.js +379 -215
- package/lib/memory.js +704 -181
- package/lib/paths.js +24 -0
- package/lib/private.js +242 -132
- package/lib/rpc.js +53 -6
- package/lib/snippet.js +49 -28
- package/lib/state.js +185 -26
- package/lib/tools.js +137 -28
- package/lib/webui.js +25 -1
- package/lib/worker-pool.js +260 -26
- package/package.json +1 -1
- package/scripts/check-i18n.mjs +19 -0
- package/scripts/recall-bench.mjs +4 -4
- package/scripts/simulate-injection.mjs +3 -0
- package/tests/activity-wiring.test.mjs +52 -0
- package/tests/client-render.test.mjs +207 -0
- package/tests/consolidation-feed.test.mjs +26 -0
- package/tests/consolidation-fork.test.mjs +587 -26
- package/tests/cross-library-recall.test.mjs +156 -0
- package/tests/cue.test.mjs +15 -2
- package/tests/hybrid-recall.test.mjs +488 -0
- package/tests/inbound.test.mjs +49 -4
- package/tests/inject-conflict.test.mjs +70 -9
- package/tests/inject-guide.test.mjs +13 -1
- package/tests/inject.test.mjs +10 -0
- package/tests/injection-budget.test.mjs +101 -0
- package/tests/injection-chain.test.mjs +37 -0
- package/tests/injection-rollback.test.mjs +172 -9
- package/tests/issue2-chain.test.mjs +169 -0
- package/tests/lexicon-inject.test.mjs +33 -5
- package/tests/managed-memory.test.mjs +625 -1
- package/tests/media.test.mjs +1 -1
- package/tests/new-session-gate.test.mjs +280 -25
- package/tests/open-loop-date.test.mjs +11 -1
- package/tests/recall-intent.test.mjs +52 -3
- package/tests/simulate-injection.test.mjs +4 -4
- package/tests/snippet.test.mjs +33 -1
- package/tests/state.test.mjs +8 -3
- package/tests/tool-schema.test.mjs +52 -0
- package/tests/tools.test.mjs +394 -0
- package/tests/webui.test.mjs +16 -2
- package/tests/worker-lifecycle.test.mjs +419 -36
package/tests/tools.test.mjs
CHANGED
|
@@ -10,8 +10,11 @@ process.env.DSH_MEMU_HOME = home
|
|
|
10
10
|
const { registerTools } = await import('../lib/tools.js')
|
|
11
11
|
const state = await import('../lib/state.js')
|
|
12
12
|
const { updateSettings } = state
|
|
13
|
+
const { redactSecrets } = await import('../lib/paths.js')
|
|
14
|
+
const { createHash } = await import('node:crypto')
|
|
13
15
|
|
|
14
16
|
test.after(() => {
|
|
17
|
+
state.flushState()
|
|
15
18
|
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
16
19
|
})
|
|
17
20
|
|
|
@@ -319,3 +322,394 @@ test('unpin is the model\'s call, guarded by reason, quota and cooldown', async
|
|
|
319
322
|
assert.match(quota, /quota/)
|
|
320
323
|
dispose()
|
|
321
324
|
})
|
|
325
|
+
|
|
326
|
+
let readFixtureNumber = 0
|
|
327
|
+
function readFixture(entries = {}, domain = 'coding') {
|
|
328
|
+
const sessionId = `tools-read-${++readFixtureNumber}`
|
|
329
|
+
const cwd = path.join(home, sessionId)
|
|
330
|
+
const binding = state.ensureSession(sessionId, { cwd, agentPreset: domain })
|
|
331
|
+
state.setSessionBinding(sessionId, { domainOverride: domain })
|
|
332
|
+
const index = { version: 1, entries: Object.fromEntries(Object.entries(entries).map(([id, entry]) => [id, {
|
|
333
|
+
id, title: '读取测试', content: '', status: 'active', layer: 'long_term', visibility: 'shared', disclosure: 'can_tell',
|
|
334
|
+
source: 'user_said', source_kind: 'user_said', speaker_id: 'user', domain,
|
|
335
|
+
createdAt: '2026-11-01T00:00:00.000Z', updatedAt: '2026-11-01T00:00:00.000Z', ...entry,
|
|
336
|
+
}])) }
|
|
337
|
+
state.savePrivateIndex(binding.libraryId, index)
|
|
338
|
+
const registered = fakeCtx()
|
|
339
|
+
return { ...registered, sessionId, cwd, binding, exec: { agent: { session: { header: { id: sessionId, cwd } } } },
|
|
340
|
+
privateTool: registered.tools.find((tool) => tool.name === 'memu_private_recall'),
|
|
341
|
+
globalTool: registered.tools.find((tool) => tool.name === 'memu_global_recall') }
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function parsedPage(reply) {
|
|
345
|
+
assert.equal(typeof reply, 'string', 'tool receipts remain strings')
|
|
346
|
+
const match = reply.match(/pagination: unit=codepoint · offset=(\d+) · returned_chars=(\d+) · total_chars=(\d+) · truncated=(true|false) · next_offset=(\d+|null)/)
|
|
347
|
+
assert.ok(match, `missing codepoint pagination: ${reply}`)
|
|
348
|
+
const revision = reply.match(/^revision=([a-f0-9]{64})$/m)?.[1]
|
|
349
|
+
assert.ok(revision, 'the redacted body SHA256 must be visible')
|
|
350
|
+
const body = reply.split('\nbody:\n')[1]?.split(/\n(?:next:|conflict:)/)[0]
|
|
351
|
+
assert.notEqual(body, undefined)
|
|
352
|
+
return { body, revision, offset: Number(match[1]), returnedChars: Number(match[2]), totalChars: Number(match[3]),
|
|
353
|
+
truncated: match[4] === 'true', nextOffset: match[5] === 'null' ? null : Number(match[5]) }
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const sha256 = (body) => createHash('sha256').update(body).digest('hex')
|
|
357
|
+
const tailBody = (length) => '甲'.repeat(length - 5) + '仅限测试尾'
|
|
358
|
+
|
|
359
|
+
test('empty bodies and the exact end offset are valid zero-length pages, not range errors', async () => {
|
|
360
|
+
const f = readFixture({ 'mem-empty': { content: '' }, 'mem-end': { content: '😀末尾' } })
|
|
361
|
+
try {
|
|
362
|
+
const empty = parsedPage(await f.privateTool.execute({ query: 'mem-empty' }, f.exec))
|
|
363
|
+
assert.deepEqual([empty.body, empty.totalChars, empty.returnedChars, empty.truncated, empty.nextOffset], ['', 0, 0, false, null])
|
|
364
|
+
assert.equal(empty.revision, sha256(''))
|
|
365
|
+
const start = parsedPage(await f.privateTool.execute({ query: 'mem-end' }, f.exec))
|
|
366
|
+
const end = parsedPage(await f.privateTool.execute({ query: 'mem-end', offset: start.totalChars, revision: start.revision }, f.exec))
|
|
367
|
+
assert.equal(end.body, '')
|
|
368
|
+
assert.equal(end.returnedChars, 0)
|
|
369
|
+
assert.equal(end.nextOffset, null)
|
|
370
|
+
assert.match(await f.privateTool.execute({ query: 'mem-empty', offset: 1, revision: empty.revision }, f.exec), /MEMORY_OFFSET_OUT_OF_RANGE/)
|
|
371
|
+
} finally { f.dispose() }
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
test('exact snippet centering reports its actual codepoint coordinates and a resumable next ref', async () => {
|
|
375
|
+
const id = 'mem-centered-exact'
|
|
376
|
+
const body = '😀'.repeat(200) + id + '🚀'.repeat(200)
|
|
377
|
+
const f = readFixture({ [id]: { content: body } })
|
|
378
|
+
try {
|
|
379
|
+
const reply = await f.privateTool.execute({ query: id, view: 'snippet' }, f.exec)
|
|
380
|
+
const page = parsedPage(reply)
|
|
381
|
+
assert.equal(page.offset, 160)
|
|
382
|
+
assert.equal(page.returnedChars, 160)
|
|
383
|
+
assert.equal(page.body, '…' + Array.from(body).slice(160, 320).join('') + '…')
|
|
384
|
+
assert.equal(page.nextOffset, 320)
|
|
385
|
+
assert.equal(page.totalChars, Array.from(body).length)
|
|
386
|
+
const rest = parsedPage(await f.privateTool.execute({ query: id, libraryId: f.binding.libraryId, offset: page.nextOffset, revision: page.revision }, f.exec))
|
|
387
|
+
assert.equal(rest.body, Array.from(body).slice(320).join(''))
|
|
388
|
+
} finally { f.dispose() }
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
test('private explicit reads allow secret/disclosure but never open archived or superseded entries', async () => {
|
|
392
|
+
const f = readFixture({
|
|
393
|
+
'mem-secret': { content: '显式秘密正文', visibility: 'secret', disclosure: 'never_say' },
|
|
394
|
+
'mem-asked': { content: '仅当被问到的正文', disclosure: 'only_if_asked' },
|
|
395
|
+
'mem-archived': { content: '归档正文不开放', status: 'archived' },
|
|
396
|
+
'mem-superseded': { content: '被替代正文不开放', status: 'superseded' },
|
|
397
|
+
'mem-held': { content: '待复核正文不开放', status: 'pending_review' },
|
|
398
|
+
})
|
|
399
|
+
try {
|
|
400
|
+
assert.equal(parsedPage(await f.privateTool.execute({ query: 'mem-secret' }, f.exec)).body, '显式秘密正文')
|
|
401
|
+
assert.equal(parsedPage(await f.privateTool.execute({ query: 'mem-asked' }, f.exec)).body, '仅当被问到的正文')
|
|
402
|
+
for (const id of ['mem-archived', 'mem-superseded', 'mem-held']) {
|
|
403
|
+
const reply = await f.privateTool.execute({ query: id }, f.exec)
|
|
404
|
+
assert.match(reply, /MEMORY_NOT_READABLE/)
|
|
405
|
+
assert.doesNotMatch(reply, /正文不开放/)
|
|
406
|
+
}
|
|
407
|
+
state.setSessionBinding(f.sessionId, { useLibrary: false })
|
|
408
|
+
assert.doesNotMatch(await f.privateTool.execute({ query: 'mem-secret' }, f.exec), /显式秘密正文/)
|
|
409
|
+
} finally { f.dispose() }
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
test('global exact reads retain automatic/domain eligibility', async () => {
|
|
413
|
+
const f = readFixture()
|
|
414
|
+
const saved = state.loadGlobalIndex()
|
|
415
|
+
state.saveGlobalIndex({ version: 1, entries: {
|
|
416
|
+
'gmem-good': { content: '合法全局正文', status: 'active', domain: 'coding', source: 'tool' },
|
|
417
|
+
'gmem-secret': { content: '不开放秘密', status: 'active', domain: 'coding', visibility: 'secret' },
|
|
418
|
+
'gmem-never': { content: '不开放披露', status: 'active', domain: 'coding', disclosure: 'never_say' },
|
|
419
|
+
'gmem-asked': { content: '不开放仅问', status: 'active', domain: 'coding', disclosure: 'only_if_asked' },
|
|
420
|
+
'gmem-rp': { content: '不开放异域', status: 'active', domain: 'rp' },
|
|
421
|
+
'gmem-archived': { content: '不开放归档', status: 'archived', domain: 'coding' },
|
|
422
|
+
} })
|
|
423
|
+
try {
|
|
424
|
+
assert.equal(parsedPage(await f.globalTool.execute({ query: 'gmem-good.md' }, f.exec)).body, '合法全局正文')
|
|
425
|
+
for (const id of ['gmem-secret', 'gmem-never', 'gmem-asked', 'gmem-rp', 'gmem-archived']) {
|
|
426
|
+
const reply = await f.globalTool.execute({ query: id }, f.exec)
|
|
427
|
+
assert.match(reply, /MEMORY_NOT_READABLE/)
|
|
428
|
+
assert.doesNotMatch(reply, /不开放/)
|
|
429
|
+
}
|
|
430
|
+
assert.match(await f.globalTool.execute({ query: 'gmem-unknown' }, f.exec), /MEMORY_NOT_FOUND/)
|
|
431
|
+
state.setSessionBinding(f.sessionId, { useGlobal: false })
|
|
432
|
+
assert.doesNotMatch(await f.globalTool.execute({ query: 'gmem-good' }, f.exec), /合法全局正文/)
|
|
433
|
+
} finally { state.saveGlobalIndex(saved); f.dispose() }
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
test('cross-library same-id reads honor libraryId, domain and opt-in without changing bindings', async () => {
|
|
437
|
+
const current = readFixture({ 'mem-same': { content: '本库正文', domain: 'rp' } })
|
|
438
|
+
const foreign = readFixture({ 'mem-same': { content: '外库正文' }, 'mem-foreign-rp': { content: '外库异域禁止', domain: 'rp' } })
|
|
439
|
+
const different = readFixture({ 'mem-same': { content: '异域库正文禁止' } }, 'rp')
|
|
440
|
+
const savedCross = state.getSettings().crossLibraryRecall
|
|
441
|
+
try {
|
|
442
|
+
updateSettings({ crossLibraryRecall: false })
|
|
443
|
+
assert.match(await current.privateTool.execute({ query: 'mem-same', libraryId: foreign.binding.libraryId }, current.exec), /MEMORY_LIBRARY_FORBIDDEN/)
|
|
444
|
+
updateSettings({ crossLibraryRecall: true })
|
|
445
|
+
const before = JSON.stringify(state.getSessionBinding(current.sessionId))
|
|
446
|
+
assert.equal(parsedPage(await current.privateTool.execute({ query: 'mem-same' }, current.exec)).body, '本库正文', 'primary does not domain-filter its own entries')
|
|
447
|
+
const directed = await current.privateTool.execute({ query: 'mem-same', libraryId: foreign.binding.libraryId }, current.exec)
|
|
448
|
+
assert.equal(parsedPage(directed).body, '外库正文')
|
|
449
|
+
assert.ok(directed.includes(`libraryId="${foreign.binding.libraryId}"`))
|
|
450
|
+
assert.equal(JSON.stringify(state.getSessionBinding(current.sessionId)), before)
|
|
451
|
+
assert.match(await current.privateTool.execute({ query: 'mem-foreign-rp', libraryId: foreign.binding.libraryId }, current.exec), /MEMORY_NOT_READABLE/)
|
|
452
|
+
assert.match(await current.privateTool.execute({ query: 'mem-same', libraryId: different.binding.libraryId }, current.exec), /MEMORY_LIBRARY_FORBIDDEN/)
|
|
453
|
+
state.setWorkspacePolicy(foreign.cwd, { useLibrary: false })
|
|
454
|
+
assert.match(await current.privateTool.execute({ query: 'mem-same', libraryId: foreign.binding.libraryId }, current.exec), /MEMORY_LIBRARY_FORBIDDEN/)
|
|
455
|
+
} finally {
|
|
456
|
+
updateSettings({ crossLibraryRecall: savedCross })
|
|
457
|
+
current.dispose(); foreign.dispose(); different.dispose()
|
|
458
|
+
}
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
test('unbound private/full and global reads never create a binding or library', async () => {
|
|
462
|
+
const f = readFixture()
|
|
463
|
+
const saved = state.loadGlobalIndex()
|
|
464
|
+
const sessionId = 'tools-full-unbound'
|
|
465
|
+
const unboundExec = { agent: { session: { header: { id: sessionId, cwd: path.join(home, sessionId) } } } }
|
|
466
|
+
state.saveGlobalIndex({ version: 1, entries: { 'gmem-unbound': { title: '通用全局', content: '通用只读正文', domain: 'general', status: 'active' } } })
|
|
467
|
+
try {
|
|
468
|
+
const libraries = JSON.stringify(state.listLibraries())
|
|
469
|
+
assert.match(await f.privateTool.execute({ query: 'mem-unknown' }, unboundExec), /MEMORY_UNBOUND/)
|
|
470
|
+
const global = await f.globalTool.execute({ query: 'gmem-unbound' }, unboundExec)
|
|
471
|
+
assert.equal(parsedPage(global).body, '通用只读正文')
|
|
472
|
+
assert.match(global, /unbound=true/)
|
|
473
|
+
assert.equal(state.getSessionBinding(sessionId), null)
|
|
474
|
+
assert.equal(JSON.stringify(state.listLibraries()), libraries)
|
|
475
|
+
} finally { state.saveGlobalIndex(saved); f.dispose() }
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
test('natural search keeps snippets, provenance, cross-library refs and explicit truncation flags', async () => {
|
|
479
|
+
const f = readFixture({
|
|
480
|
+
'mem-search-long': { title: '码头部署约定', content: '甲'.repeat(200) + '码头部署约定' + '乙'.repeat(200), visibility: 'secret', disclosure: 'never_say' },
|
|
481
|
+
'mem-search-short': { title: '短条咖啡约定', content: '短条咖啡约定:仅供测试。', source: 'tool', source_kind: 'tool' },
|
|
482
|
+
})
|
|
483
|
+
const foreign = readFixture({ 'mem-search-foreign': { title: '外库专用检索词', content: '外库专用检索词。' } })
|
|
484
|
+
const saved = state.loadGlobalIndex()
|
|
485
|
+
const savedSettings = state.getSettings()
|
|
486
|
+
state.saveGlobalIndex({ version: 1, entries: { 'gmem-search-long': { title: '全局分页检索词', content: '甲'.repeat(200) + '全局分页检索词' + '乙'.repeat(200), status: 'active', domain: 'coding', source: 'tool' } } })
|
|
487
|
+
updateSettings({ memoryBackend: 'agent', crossLibraryRecall: false, relevanceThreshold: 0 })
|
|
488
|
+
try {
|
|
489
|
+
const long = await f.privateTool.execute({ query: '码头部署约定', topK: 1 }, f.exec)
|
|
490
|
+
assert.match(long, /1\. \[mem-search-long\]/)
|
|
491
|
+
assert.match(long, /view=snippet.*scope=private.*truncated=true/)
|
|
492
|
+
assert.match(long, /source=user_said/)
|
|
493
|
+
assert.match(long, /visibility=secret/)
|
|
494
|
+
assert.match(long, /disclosure=never_say/)
|
|
495
|
+
assert.ok(long.includes(`libraryId="${f.binding.libraryId}"`))
|
|
496
|
+
assert.doesNotMatch(long, /甲{200}|乙{200}/)
|
|
497
|
+
const short = await f.privateTool.execute({ query: '短条咖啡约定', topK: 1 }, f.exec)
|
|
498
|
+
assert.match(short, /\[mem-search-short\]/)
|
|
499
|
+
assert.match(short, /truncated=false/)
|
|
500
|
+
updateSettings({ crossLibraryRecall: true })
|
|
501
|
+
const cross = await f.privateTool.execute({ query: '外库专用检索词', topK: 1 }, f.exec)
|
|
502
|
+
assert.match(cross, /\[mem-search-foreign\]/)
|
|
503
|
+
assert.ok(cross.includes(`libraryId="${foreign.binding.libraryId}"`))
|
|
504
|
+
assert.match(cross, /跨库只读/)
|
|
505
|
+
const global = await f.globalTool.execute({ query: '全局分页检索词', topK: 1 }, f.exec)
|
|
506
|
+
assert.match(global, /1\. \[gmem-search-long\]/)
|
|
507
|
+
assert.match(global, /view=snippet.*scope=global.*truncated=true/)
|
|
508
|
+
assert.match(global, /source=tool/)
|
|
509
|
+
assert.doesNotMatch(global, /甲{200}|乙{200}/)
|
|
510
|
+
} finally {
|
|
511
|
+
updateSettings({ memoryBackend: savedSettings.memoryBackend, crossLibraryRecall: savedSettings.crossLibraryRecall, relevanceThreshold: savedSettings.relevanceThreshold })
|
|
512
|
+
state.saveGlobalIndex(saved)
|
|
513
|
+
f.dispose(); foreign.dispose()
|
|
514
|
+
}
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
test('private/global natural search retain degradation warnings with no active engine', async () => {
|
|
518
|
+
const f = readFixture({ 'mem-degraded': { title: '降级专用检索词', content: '降级专用检索词。' } })
|
|
519
|
+
const savedSettings = state.getSettings()
|
|
520
|
+
const savedGlobal = state.loadGlobalIndex()
|
|
521
|
+
state.saveGlobalIndex({ version: 1, entries: { 'gmem-degraded': { title: '降级专用检索词', content: '降级专用检索词。', status: 'active', domain: 'coding' } } })
|
|
522
|
+
// No active vector generation in this temporary home: preflight rejects before
|
|
523
|
+
// retrieve/ensure/engine startup. The local index hit must remain visible.
|
|
524
|
+
updateSettings({ memoryBackend: 'api', vectorSource: 'local', crossLibraryRecall: false })
|
|
525
|
+
try {
|
|
526
|
+
for (const tool of [f.privateTool, f.globalTool]) {
|
|
527
|
+
const reply = await tool.execute({ query: '降级专用检索词', topK: 1 }, f.exec)
|
|
528
|
+
assert.equal(typeof reply, 'string')
|
|
529
|
+
assert.match(reply, /\[(?:g?mem)-degraded\]/)
|
|
530
|
+
assert.match(reply, /注意:本次召回已降级:/)
|
|
531
|
+
assert.match(reply, /semantic recall failed/)
|
|
532
|
+
}
|
|
533
|
+
} finally {
|
|
534
|
+
updateSettings({ memoryBackend: savedSettings.memoryBackend, vectorSource: savedSettings.vectorSource, crossLibraryRecall: savedSettings.crossLibraryRecall })
|
|
535
|
+
state.saveGlobalIndex(savedGlobal)
|
|
536
|
+
f.dispose()
|
|
537
|
+
}
|
|
538
|
+
})
|
|
539
|
+
|
|
540
|
+
test('redaction happens before coordinates/revision; changed continuations retain MEMORY_CHANGED', async () => {
|
|
541
|
+
const id = 'mem-redacted-page'
|
|
542
|
+
const original = '😀开头 sk-fixtureOnly12345678901234567890 尾部🚀' + '乙'.repeat(180)
|
|
543
|
+
const safe = redactSecrets(original)
|
|
544
|
+
const f = readFixture({ [id]: { content: original } })
|
|
545
|
+
try {
|
|
546
|
+
const first = parsedPage(await f.privateTool.execute({ query: id, limitChars: 19 }, f.exec))
|
|
547
|
+
assert.equal(first.body, Array.from(safe).slice(0, 19).join(''))
|
|
548
|
+
assert.equal(first.totalChars, Array.from(safe).length)
|
|
549
|
+
assert.equal(first.revision, sha256(safe))
|
|
550
|
+
const restReply = await f.privateTool.execute({ query: id, offset: first.nextOffset, revision: first.revision, limitChars: 8000 }, f.exec)
|
|
551
|
+
const rest = parsedPage(restReply)
|
|
552
|
+
assert.equal(first.body + rest.body, safe)
|
|
553
|
+
assert.doesNotMatch(first.body + rest.body, /fixtureOnly/)
|
|
554
|
+
assert.match(await f.privateTool.execute({ query: id, offset: first.nextOffset }, f.exec), /REVISION_REQUIRED/)
|
|
555
|
+
const index = state.loadPrivateIndex(f.binding.libraryId)
|
|
556
|
+
index.entries[id].content += '新版'
|
|
557
|
+
state.savePrivateIndex(f.binding.libraryId, index)
|
|
558
|
+
const changed = await f.privateTool.execute({ query: id, offset: first.nextOffset, revision: first.revision }, f.exec)
|
|
559
|
+
assert.match(changed, /\[MEMORY_CHANGED\]/)
|
|
560
|
+
assert.match(changed, /offset 0/)
|
|
561
|
+
assert.doesNotMatch(changed, /新版|body:/)
|
|
562
|
+
assert.notEqual(parsedPage(await f.privateTool.execute({ query: id }, f.exec)).revision, first.revision)
|
|
563
|
+
} finally { f.dispose() }
|
|
564
|
+
})
|
|
565
|
+
|
|
566
|
+
test('exact snippet stays at 160 codepoints, respects smaller page limits, and never expands conflicts', async () => {
|
|
567
|
+
const id = 'mem-exact-snippet'
|
|
568
|
+
const f = readFixture({
|
|
569
|
+
[id]: { content: tailBody(4000), conflicts_with: 'mem-other-version', visibility: 'secret', disclosure: 'never_say' },
|
|
570
|
+
'mem-other-version': { title: '不得渲染另一正文', content: '第二版秘密正文不得出现在第一版读取中。' },
|
|
571
|
+
})
|
|
572
|
+
try {
|
|
573
|
+
const before = JSON.stringify(state.loadPrivateIndex(f.binding.libraryId))
|
|
574
|
+
const reply = await f.privateTool.execute({ query: id, view: 'snippet', topK: 20 }, f.exec)
|
|
575
|
+
const page = parsedPage(reply)
|
|
576
|
+
assert.equal(page.returnedChars, 160)
|
|
577
|
+
assert.equal(page.totalChars, 4000)
|
|
578
|
+
assert.equal(page.truncated, true)
|
|
579
|
+
assert.equal(page.nextOffset, 160, 'continue after visible text, not the hidden 2000-char API page')
|
|
580
|
+
assert.equal(page.body, '甲'.repeat(160) + '…')
|
|
581
|
+
assert.match(reply, /view=snippet/)
|
|
582
|
+
assert.match(reply, /visibility=secret.*disclosure=never_say/)
|
|
583
|
+
assert.match(reply, /conflict: \[mem-other-version\]/)
|
|
584
|
+
assert.doesNotMatch(reply, /第二版秘密正文|不得渲染另一正文|仅限测试尾/)
|
|
585
|
+
const small = parsedPage(await f.privateTool.execute({ query: id, view: 'snippet', limitChars: 3 }, f.exec))
|
|
586
|
+
assert.equal(small.returnedChars, 3)
|
|
587
|
+
assert.equal(small.body, '甲'.repeat(3))
|
|
588
|
+
assert.equal(small.truncated, true)
|
|
589
|
+
assert.equal(small.nextOffset, 3)
|
|
590
|
+
const full = parsedPage(await f.privateTool.execute({ query: id, limitChars: 8000 }, f.exec))
|
|
591
|
+
assert.equal(full.body, tailBody(4000))
|
|
592
|
+
assert.equal(JSON.stringify(state.loadPrivateIndex(f.binding.libraryId)), before, 'exact full/snippet reads are not recall')
|
|
593
|
+
} finally { f.dispose() }
|
|
594
|
+
})
|
|
595
|
+
|
|
596
|
+
test('exact prefixes/.md resolve to full refs; ambiguity and unknown ids never fall back to search', async () => {
|
|
597
|
+
const f = readFixture({ 'mem-prefix-unique': { content: '唯一前缀正文' }, 'mem-prefix-two': { content: '另一个版本' } })
|
|
598
|
+
try {
|
|
599
|
+
const resolved = await f.privateTool.execute({ query: 'mem-prefix-u.md' }, f.exec)
|
|
600
|
+
assert.match(resolved, /id="mem-prefix-unique"/)
|
|
601
|
+
assert.equal(parsedPage(resolved).body, '唯一前缀正文')
|
|
602
|
+
assert.match(await f.privateTool.execute({ query: 'mem-prefix' }, f.exec), /MEMORY_ID_AMBIGUOUS/)
|
|
603
|
+
assert.match(await f.privateTool.execute({ query: 'mem-missing' }, f.exec), /MEMORY_NOT_FOUND/)
|
|
604
|
+
assert.match(await f.privateTool.execute({ query: '部署限制', view: 'full' }, f.exec), /EXACT_ID_REQUIRED/)
|
|
605
|
+
for (const query of [' mem-prefix-unique', 'mem-prefix-unique ', 'mem-prefix-unique\n', 'mem-prefix unique']) {
|
|
606
|
+
assert.match(await f.privateTool.execute({ query, view: 'full' }, f.exec), /EXACT_ID_REQUIRED/, 'no whitespace in exact ids')
|
|
607
|
+
}
|
|
608
|
+
} finally { f.dispose() }
|
|
609
|
+
})
|
|
610
|
+
|
|
611
|
+
test('recall parser enforces ranges/types and rejects natural-query paging/library targeting', async () => {
|
|
612
|
+
const f = readFixture({ 'mem-validation': { content: '校验正文' } })
|
|
613
|
+
try {
|
|
614
|
+
for (const tool of [f.privateTool, f.globalTool]) {
|
|
615
|
+
assert.match(await tool.execute({ query: '自然语言', view: 'full' }, f.exec), /EXACT_ID_REQUIRED/)
|
|
616
|
+
for (const topK of [0, 21, 1.5, '2', NaN, Infinity, null]) {
|
|
617
|
+
assert.match(await tool.execute({ query: '自然语言', topK }, f.exec), /INVALID_RECALL_ARGUMENT.*topK/)
|
|
618
|
+
}
|
|
619
|
+
for (const field of ['offset', 'limitChars', 'revision', 'libraryId']) {
|
|
620
|
+
assert.match(await tool.execute({ query: '自然语言', [field]: field === 'offset' ? 0 : field === 'limitChars' ? 2000 : 'value' }, f.exec), /INVALID_RECALL_ARGUMENT/)
|
|
621
|
+
}
|
|
622
|
+
for (const query of ['', ' ', null, 42]) assert.match(await tool.execute({ query }, f.exec), /query is required/)
|
|
623
|
+
for (const view of ['object', '', 42, null]) assert.match(await tool.execute({ query: 'mem-validation', view }, f.exec), /INVALID_RECALL_ARGUMENT.*view/)
|
|
624
|
+
for (const offset of [-1, 0.5, '1', NaN, Infinity, null]) assert.match(await tool.execute({ query: 'mem-validation', offset }, f.exec), /INVALID_RECALL_ARGUMENT.*offset/)
|
|
625
|
+
for (const limitChars of [0, -1, 8001, 1.5, '20', Infinity, null]) assert.match(await tool.execute({ query: 'mem-validation', limitChars }, f.exec), /INVALID_RECALL_ARGUMENT.*limitChars/)
|
|
626
|
+
for (const revision of ['', 42, null]) assert.match(await tool.execute({ query: 'mem-validation', revision }, f.exec), /INVALID_RECALL_ARGUMENT.*revision/)
|
|
627
|
+
}
|
|
628
|
+
const first = parsedPage(await f.privateTool.execute({ query: 'mem-validation', limitChars: 1 }, f.exec))
|
|
629
|
+
assert.equal(first.returnedChars, 1)
|
|
630
|
+
assert.match(await f.privateTool.execute({ query: 'mem-validation', offset: 99, revision: first.revision }, f.exec), /MEMORY_OFFSET_OUT_OF_RANGE/)
|
|
631
|
+
assert.match(await f.globalTool.execute({ query: 'gmem-any', libraryId: f.binding.libraryId }, f.exec), /INVALID_RECALL_ARGUMENT/)
|
|
632
|
+
assert.match(await f.privateTool.execute({ query: 'mem-validation', libraryId: '' }, f.exec), /INVALID_RECALL_ARGUMENT/)
|
|
633
|
+
} finally { f.dispose() }
|
|
634
|
+
})
|
|
635
|
+
|
|
636
|
+
for (const scope of ['private', 'global']) {
|
|
637
|
+
for (const length of [160, 161, 169]) {
|
|
638
|
+
test(`${scope} exact auto/full keeps the tail condition of a ${length}-codepoint body`, async () => {
|
|
639
|
+
const id = `${scope === 'private' ? 'mem' : 'gmem'}-tail-${length}`
|
|
640
|
+
const body = tailBody(length)
|
|
641
|
+
const f = readFixture(scope === 'private' ? { [id]: { content: body } } : {})
|
|
642
|
+
const savedGlobal = state.loadGlobalIndex()
|
|
643
|
+
if (scope === 'global') state.saveGlobalIndex({ version: 1, entries: { [id]: {
|
|
644
|
+
title: '全局尾条件', content: body, status: 'active', domain: 'coding', appliesTo: ['coding'], source: 'tool',
|
|
645
|
+
} } })
|
|
646
|
+
try {
|
|
647
|
+
const tool = scope === 'private' ? f.privateTool : f.globalTool
|
|
648
|
+
const before = JSON.stringify(state.loadPrivateIndex(f.binding.libraryId))
|
|
649
|
+
for (const view of [undefined, 'auto', 'full']) {
|
|
650
|
+
const reply = await tool.execute({ query: id, ...(view ? { view } : {}), topK: 20 }, f.exec)
|
|
651
|
+
const page = parsedPage(reply)
|
|
652
|
+
assert.equal(page.body, body)
|
|
653
|
+
assert.equal(page.totalChars, length)
|
|
654
|
+
assert.equal(page.returnedChars, length)
|
|
655
|
+
assert.equal(page.truncated, false)
|
|
656
|
+
assert.equal(page.nextOffset, null)
|
|
657
|
+
assert.equal(page.revision, sha256(body))
|
|
658
|
+
assert.match(reply, /view=full/)
|
|
659
|
+
assert.match(reply, /source=(user_said|tool)/)
|
|
660
|
+
assert.match(reply, new RegExp(`ref: scope=${scope}`))
|
|
661
|
+
assert.deepEqual(tool.output.render({}, reply), [{ type: 'text', text: reply }])
|
|
662
|
+
}
|
|
663
|
+
assert.equal(JSON.stringify(state.loadPrivateIndex(f.binding.libraryId)), before, 'exact reads must not touch search access counters')
|
|
664
|
+
} finally {
|
|
665
|
+
if (scope === 'global') state.saveGlobalIndex(savedGlobal)
|
|
666
|
+
f.dispose()
|
|
667
|
+
}
|
|
668
|
+
})
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
for (const scope of ['private', 'global']) {
|
|
673
|
+
for (const length of [169, 4000]) {
|
|
674
|
+
test(`${scope} ${length}-codepoint pagination is continuous and defaults to 2000`, async () => {
|
|
675
|
+
const id = `${scope === 'private' ? 'mem' : 'gmem'}-paged-${length}`
|
|
676
|
+
const body = '😀甲🚀乙'.repeat(Math.ceil(length / 4))
|
|
677
|
+
const expected = Array.from(body).slice(0, length - 5).join('') + '仅限测试尾'
|
|
678
|
+
const f = readFixture(scope === 'private' ? { [id]: { content: expected } } : {})
|
|
679
|
+
const savedGlobal = state.loadGlobalIndex()
|
|
680
|
+
if (scope === 'global') state.saveGlobalIndex({ version: 1, entries: { [id]: { title: '分页', content: expected, status: 'active', domain: 'coding' } } })
|
|
681
|
+
try {
|
|
682
|
+
const tool = scope === 'private' ? f.privateTool : f.globalTool
|
|
683
|
+
const defaultPage = parsedPage(await tool.execute({ query: id }, f.exec))
|
|
684
|
+
assert.equal(defaultPage.returnedChars, Math.min(2000, length))
|
|
685
|
+
let offset = 0
|
|
686
|
+
let revision = null
|
|
687
|
+
let assembled = ''
|
|
688
|
+
do {
|
|
689
|
+
const reply = await tool.execute({ query: id, view: 'full', offset, limitChars: length === 169 ? 61 : 997,
|
|
690
|
+
...(revision ? { revision } : {}) }, f.exec)
|
|
691
|
+
const page = parsedPage(reply)
|
|
692
|
+
assert.equal(page.offset, offset)
|
|
693
|
+
assert.equal(page.totalChars, length)
|
|
694
|
+
assert.equal(page.returnedChars, Array.from(page.body).length)
|
|
695
|
+
assert.equal(page.revision, sha256(expected))
|
|
696
|
+
assert.equal(page.truncated, offset + page.returnedChars < length)
|
|
697
|
+
assert.equal(page.body, Array.from(expected).slice(offset, offset + page.returnedChars).join(''))
|
|
698
|
+
assert.equal(page.body.isWellFormed(), true, 'never split emoji surrogate pairs')
|
|
699
|
+
assembled += page.body
|
|
700
|
+
revision = page.revision
|
|
701
|
+
offset = page.nextOffset
|
|
702
|
+
if (offset !== null) {
|
|
703
|
+
assert.match(reply, new RegExp(`next: memu_${scope}_recall query="${id}" view="full"`))
|
|
704
|
+
assert.match(reply, new RegExp(`offset=${offset}.*revision="${revision}"`))
|
|
705
|
+
if (scope === 'private') assert.ok(reply.includes(`libraryId="${f.binding.libraryId}"`))
|
|
706
|
+
} else assert.doesNotMatch(reply, /^next:/m)
|
|
707
|
+
} while (offset !== null)
|
|
708
|
+
assert.equal(assembled, expected)
|
|
709
|
+
} finally {
|
|
710
|
+
if (scope === 'global') state.saveGlobalIndex(savedGlobal)
|
|
711
|
+
f.dispose()
|
|
712
|
+
}
|
|
713
|
+
})
|
|
714
|
+
}
|
|
715
|
+
}
|
package/tests/webui.test.mjs
CHANGED
|
@@ -19,8 +19,8 @@ function handler(rejection) {
|
|
|
19
19
|
assert.deepEqual(routes.map(route => route.path), ['/dsh-memu-memory', '/dsh-memu'])
|
|
20
20
|
return routes[0].handler
|
|
21
21
|
}
|
|
22
|
-
async function request(url, method = 'GET', body, rejection) {
|
|
23
|
-
const req = { method, url, async *[Symbol.asyncIterator]() { if (body) yield Buffer.from(JSON.stringify(body)) } }
|
|
22
|
+
async function request(url, method = 'GET', body, rejection, headers = {}) {
|
|
23
|
+
const req = { method, url, headers, async *[Symbol.asyncIterator]() { if (body) yield Buffer.from(JSON.stringify(body)) } }
|
|
24
24
|
const res = { status: 0, headers: {}, body: '', writeHead(code, headers) { this.status = code; this.headers = headers || {} }, end(data) { this.body = data ? String(data) : '' } }
|
|
25
25
|
await handler(rejection)(req, res)
|
|
26
26
|
return res
|
|
@@ -47,6 +47,20 @@ test('standalone settings load the exact client component with local React asset
|
|
|
47
47
|
}
|
|
48
48
|
assert.equal((await request('/dsh-memu-memory/ui-assets/../../../state.json')).status, 405)
|
|
49
49
|
})
|
|
50
|
+
test('ui assets revalidate with ETag instead of reshipping the bundle', async () => {
|
|
51
|
+
// 首屏四个脚本约 800KB;no-cache 无校验器会每次全量重传。命中 If-None-Match
|
|
52
|
+
// 必须回 304 空体,过期校验器仍回完整内容。
|
|
53
|
+
const first = await request('/dsh-memu-memory/ui-assets/client.js')
|
|
54
|
+
assert.equal(first.status, 200)
|
|
55
|
+
assert.ok(first.headers.ETag, 'assets carry a validator')
|
|
56
|
+
assert.equal(first.headers['Cache-Control'], 'no-cache')
|
|
57
|
+
const again = await request('/dsh-memu-memory/ui-assets/client.js', 'GET', null, undefined, { 'if-none-match': first.headers.ETag })
|
|
58
|
+
assert.equal(again.status, 304)
|
|
59
|
+
assert.equal(again.body, '')
|
|
60
|
+
const stale = await request('/dsh-memu-memory/ui-assets/client.js', 'GET', null, undefined, { 'if-none-match': '"stale"' })
|
|
61
|
+
assert.equal(stale.status, 200)
|
|
62
|
+
assert.equal(stale.body, first.body)
|
|
63
|
+
})
|
|
50
64
|
test('all standalone scripts parse and neither shell defines a second settings editor', () => {
|
|
51
65
|
const wall = webUiHtml()
|
|
52
66
|
assert.equal(wall, standaloneHtml(), 'wall and settings must be exactly the same shell')
|