dsh-brain/verify-local-memory-round6.mjs

68 lines
7.2 KiB
JavaScript

// Round 6: migration ownership, preexisting-record safety and restore target policy.
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const root = mkdtempSync(join(tmpdir(), 'brain-r6-'))
process.env.DSH_HOME = join(root, 'home')
const a = join(root, 'a'), b = join(root, 'b'); mkdirSync(a, { recursive: true }); mkdirSync(b, { recursive: true })
const results = []
function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) }
const { LocalMemoryManager } = 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: () => a, settings })
await manager.ensure()
const preexisting = await manager.withWorkspace(a, () => manager.remember({ content: 'preexisting same content', scope: 'global' }))
const legacy1 = join(a, '.mnemon-1'); mkdirSync(legacy1, { recursive: true }); writeFileSync(join(legacy1, 'memories.json'), JSON.stringify({ records: [{ id: 'old-1', content: 'preexisting same content', category: 'general' }] }))
const first = await manager.withWorkspace(a, () => manager.migrateLegacy(legacy1, a))
const afterFirst = await manager.withWorkspace(a, () => manager.read(preexisting.record.id, a))
check('content-only duplicate is not owned by the migration', first.imported === 0 && first.duplicates === 1 && afterFirst?.id === preexisting.record.id)
const rollback1 = await manager.withWorkspace(a, () => manager.rollbackMigration(first.copiedTo, join(root, 'restore-1')))
const stillThere = await manager.withWorkspace(a, () => manager.read(preexisting.record.id, a))
check('rollback never removes a preexisting duplicate', rollback1.removedImported === 0 && stillThere?.content === 'preexisting same content')
// Mixed existing + new record ownership.
const legacy2 = join(a, '.mnemon-2'); mkdirSync(legacy2, { recursive: true }); writeFileSync(join(legacy2, 'memories.json'), JSON.stringify({ records: [{ id: 'old-2', content: 'mixed existing', category: 'general' }, { id: 'new-2', content: 'mixed new record', category: 'general' }] }))
await manager.withWorkspace(a, () => manager.remember({ content: 'mixed existing', scope: 'global' }))
const mixed = await manager.withWorkspace(a, () => manager.migrateLegacy(legacy2, a))
const mixedRollback = await manager.withWorkspace(a, () => manager.rollbackMigration(mixed.copiedTo, join(root, 'restore-2')))
const mixedNew = (await manager.withWorkspace(a, () => manager.search('mixed new record', 5, a))).filter((record) => record.content === 'mixed new record')
const mixedExisting = (await manager.withWorkspace(a, () => manager.search('mixed existing', 5, a))).filter((record) => record.content === 'mixed existing')
check('mixed rollback removes only the new record', mixed.imported === 1 && mixed.duplicates === 1 && mixedRollback.removedImported === 1 && mixedNew.length === 0 && mixedExisting.length === 1)
// Migration under A, rollback invoked in B must use A's target.
const legacy3 = join(a, '.mnemon-3'); mkdirSync(legacy3, { recursive: true }); writeFileSync(join(legacy3, 'memories.json'), JSON.stringify({ records: [{ id: 'old-3', content: 'scope A migrated record', category: 'general' }] }))
const scoped = await manager.withWorkspace(a, () => manager.migrateLegacy(legacy3, a))
await manager.withWorkspace(b, () => manager.remember({ content: 'scope B untouched', scope: 'project' }))
const scopedRollback = await manager.withWorkspace(b, () => manager.rollbackMigration(scoped.copiedTo, join(root, 'restore-3')))
const aAfter = (await manager.withWorkspace(a, () => manager.search('scope A migrated', 5, a))).filter((record) => record.content === 'scope A migrated record')
const bAfter = (await manager.withWorkspace(b, () => manager.search('scope B untouched', 5, b))).filter((record) => record.content === 'scope B untouched')
check('rollback in another workspace still targets the receipt workspace', scopedRollback.removedImported === 1 && aAfter.length === 0 && bAfter.length === 1)
// A non-empty restore target is rejected before active mutation.
const legacy4 = join(a, '.mnemon-4'); mkdirSync(legacy4, { recursive: true }); writeFileSync(join(legacy4, 'memories.json'), JSON.stringify({ records: [{ id: 'old-4', content: 'target conflict record', category: 'general' }] }))
const conflictMigration = await manager.withWorkspace(a, () => manager.migrateLegacy(legacy4, a))
const occupied = join(root, 'occupied'); mkdirSync(occupied, { recursive: true }); writeFileSync(join(occupied, 'memories.json'), 'old different content')
let occupiedError = ''
try { await manager.withWorkspace(a, () => manager.rollbackMigration(conflictMigration.copiedTo, occupied)) } catch (error) { occupiedError = String(error) }
const conflictRecord = await manager.withWorkspace(a, () => manager.search('target conflict record', 5, a))
check('non-empty restore target is rejected without removing active records', occupiedError.includes('restore_destination_not_empty') && conflictRecord.length === 1)
const legacy5 = join(a, '.mnemon-5'); mkdirSync(legacy5, { recursive: true }); writeFileSync(join(legacy5, 'memories.json'), JSON.stringify({ records: [{ id: 'old-5', content: 'copied resume record', category: 'general' }] }))
const resume = await manager.withWorkspace(a, () => manager.migrateLegacy(legacy5, a))
const memoryFile = join(root, 'home', 'brain', 'runtime', 'memories.json')
const originalMemory = readFileSync(memoryFile)
writeFileSync(memoryFile, '{broken memory')
let copiedError = ''
try { await manager.withWorkspace(a, () => manager.rollbackMigration(resume.copiedTo, join(root, 'resume-a'))) } catch (error) { copiedError = String(error) }
const copiedState = JSON.parse(readFileSync(join(root, 'home', 'brain', 'state', 'migrations.json'), 'utf8')).find((item) => item.copiedTo === resume.copiedTo)
check('record failure leaves a copied rollback checkpoint', copiedError.includes('local_memory_corrupt') && copiedState?.rollback?.status === 'copied')
writeFileSync(memoryFile, originalMemory)
let mismatch = ''
try { await manager.withWorkspace(a, () => manager.rollbackMigration(resume.copiedTo, join(root, 'resume-b'))) } catch (error) { mismatch = String(error) }
check('resume with a different destination is rejected', mismatch.includes('restore_destination_mismatch') && !existsSync(join(root, 'resume-b')))
const resumed = await manager.withWorkspace(a, () => manager.rollbackMigration(resume.copiedTo))
const resumedRecord = await manager.withWorkspace(a, () => manager.search('copied resume', 5, a))
check('resume with the recorded destination completes and restores the copy', resumed.restoredTo === join(root, 'resume-a') && resumed.removedImported === 1 && resumedRecord.length === 0 && existsSync(join(root, 'resume-a', 'memories.json')))
const failed = results.filter((result) => !result.ok)
console.log('')
console.log(failed.length ? 'FAILED ' + failed.length + '/' + results.length + ': ' + failed.map((result) => result.name).join(', ') : 'ALL ' + results.length + ' CHECKS PASSED')
rmSync(root, { recursive: true, force: true })
process.exit(failed.length ? 1 : 0)