dsh-brain/verify-local-memory.mjs

57 lines
5.5 KiB
JavaScript

import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const home = mkdtempSync(join(tmpdir(), 'brain-local-'))
const project = join(home, 'project')
process.env.DSH_HOME = join(home, '.dsh')
mkdirSync(project, { recursive: true })
const results = []
function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) }
const { LocalMemoryManager, globalBrainRoot, projectBrainRoot, projectIdFor } = await import('./lib/local-memory/index.js')
const { LocalMemoryRemote } = await import('./lib/local-memory/index.js')
const settings = () => ({ localMemoryEnabled: true, cloudMemoryEnabled: false, autoMemorySpaces: true, autoSummaryEnabled: true, autoProfileEnabled: true, localMemoryK: 5 })
const manager = new LocalMemoryManager({ cwd: () => project, settings })
const spaces = await manager.ensure()
check('global space is auto-created', spaces.global.id === 'global' && existsSync(join(globalBrainRoot(), 'spaces', 'global', 'space.json')))
check('project space is auto-created', spaces.project.scope === 'project' && existsSync(join(projectBrainRoot(project), 'memory-space', 'space.json')))
check('project .dsh/brain is ignored without ignoring .dsh', readFileSync(join(project, '.dsh', '.gitignore'), 'utf8').includes('brain/'))
check('no .mnemon directory is created', !existsSync(join(project, '.mnemon')) && !existsSync(join(home, '.dsh', '.mnemon')))
const a = await manager.remember({ content: '用户偏好简短回答', category: 'preference', tags: ['profile'], scope: 'global', source: 'user' })
check('global JSON store lives in runtime', existsSync(join(globalBrainRoot(), 'runtime', 'memories.json')))
check('memory has stable fields', a.record.id.startsWith('lm_') && a.record.revision === 1 && typeof a.record.changeSeq === 'number' && a.record.scope === 'global')
const dup = await manager.remember({ content: '用户偏好简短回答', category: 'preference', tags: ['profile'], scope: 'global', source: 'user' })
check('exact duplicate returns existing record', dup.duplicate === true && dup.record.id === a.record.id)
const p = await manager.remember({ content: '项目使用 TypeScript', category: 'project', scope: 'project' })
check('project memory goes to project space', p.record.scope === 'project' && p.record.projectId === projectIdFor(projectBrainRoot(project)))
check('search covers project and global spaces', (await manager.search('TypeScript')).some((record) => record.id === p.record.id) && (await manager.search('简短')).some((record) => record.id === a.record.id))
const updated = await manager.remember({ content: 'ignored' }); void updated
await manager.forget(a.record.id)
check('forget is a tombstone invisible to search', !(await manager.search('简短回答')).some((record) => record.id === a.record.id))
const restarted = new LocalMemoryManager({ cwd: () => project, settings })
const again = await restarted.ensure()
check('restart reuses spaces without duplicates', again.global.id === spaces.global.id && again.project.id === spaces.project.id)
check('restart retains project memory', (await restarted.search('TypeScript')).some((record) => record.id === p.record.id))
const summary = await restarted.summarizeConversation({ sessionId: 's1', turns: [{ turnId: 't1', user: '我喜欢简短回答。记住:以后总是用中文。' }] })
check('summary captures explicit preferences', summary.stored === 2, JSON.stringify(summary))
check('summary does not store the raw transcript', !(await restarted.search('简短回答')).some((record) => record.content.includes('我喜欢简短回答。记住')))
check('USER.md projection is generated', readFileSync(join(globalBrainRoot(), 'runtime', 'USER.md'), 'utf8').includes('喜欢简短回答'))
check('MEMORY.md projection is generated', readFileSync(join(globalBrainRoot(), 'runtime', 'MEMORY.md'), 'utf8').includes('总是')
)
const summary2 = await restarted.summarizeConversation({ sessionId: 's1', turns: [{ turnId: 't1', user: '我喜欢简短回答。记住:以后总是用中文。' }] })
check('summary checkpoint is incremental', summary2.stored === 0 && summary2.duplicates === 0)
await restarted.proposeProfile('可能是内向的人', 'inferred', true)
check('inferred profile stays out of USER.md', !readFileSync(join(globalBrainRoot(), 'runtime', 'USER.md'), 'utf8').includes('内向'))
const doc = await restarted.addDocument({ title: '项目决策', content: '使用本地记忆优先。', tags: ['decision'] })
check('project documents are stored', (await restarted.documents()).some((item) => item.id === doc.id))
mkdirSync(join(project, '.mnemon'), { recursive: true })
const report = await restarted.legacyMigrationReport()
check('legacy .mnemon is reported, not moved', report.found.includes(join(project, '.mnemon')))
const copied = await restarted.migrateLegacy(join(project, '.mnemon'))
check('legacy migration copies into backups', existsSync(copied.copiedTo) && existsSync(join(project, '.mnemon')))
const remote = new LocalMemoryRemote(restarted)
const searchResult = await remote.search('TypeScript', 5)
check('local MemoryRemote search is usable', searchResult.ok === true && (searchResult.data.matches ?? []).length >= 1)
console.log('\n' + (results.every((r) => r.ok) ? 'ALL ' + results.length + ' CHECKS PASSED' : results.filter((r) => !r.ok).length + ' FAILED'))
rmSync(home, { recursive: true, force: true })
process.exit(results.every((r) => r.ok) ? 0 : 1)