/** * One-time copy of memories from the legacy `.mind` files into the memory * store (P1.5). The legacy files are only read: the data directory may be * shared with the commercial edition. Each scope is copied once; progress is * kept in waggle.db, so an interrupted copy resumes where it stopped, and the * store does not write identical text twice in one scope. */ import fs from 'node:fs'; import Database from 'better-sqlite3'; import { MEMORY_IMPORTANCE, MEMORY_SOURCES, scopeKey, type MemoryImportance, type MemoryKind, type MemoryScope, type MemorySource, type MemoryStore, } from '@waggle/memory-contract'; import type { WaggleDB } from '@waggle/core'; export interface LegacyMemorySource { /** A legacy `.mind` file. */ path: string; scope: MemoryScope; } export interface LegacyMemoryImportReport { scope: string; status: 'imported' | 'already-imported' | 'missing' | 'failed'; copied: number; skipped: number; error?: string; } interface LegacyFrameRow { id: number; content: string; importance: string; source: string | null; created_at: string | null; } /** Content prefixes the legacy engine used as types, mapped to contract kinds. */ const PREFIX_KINDS: ReadonlyArray<[RegExp, MemoryKind, boolean]> = [ // [pattern, kind, strip the first line (a header)] [/^\[mind-profile [^\]]*\]/, 'profile', true], [/^\[mind-fact\]/, 'fact', true], [/^\[mind-event\]/, 'event', true], [/^(User preference:|Style note:)/, 'preference', false], [/^Decision:/, 'decision', false], [/^(\[Session summary|Research findings:)/, 'summary', false], [/^(\[Loop:|\[Sub-agent:|User asked:|Work completed:|Recommendation:)/, 'note', false], ]; /** Raw dialogue turns were an engine-specific recall lane; they are not memories. */ const SKIPPED_PREFIX = /^\[mind-rawturn /; const metaKey = (scope: MemoryScope): string => `memory-import:${scopeKey(scope)}`; const BATCH = 200; export async function importLegacyMemories( memory: MemoryStore, waggleDb: WaggleDB, sources: readonly LegacyMemorySource[], onProgress?: (report: LegacyMemoryImportReport) => void, isCancelled: () => boolean = () => false, ): Promise { const reports: LegacyMemoryImportReport[] = []; for (const source of sources) { if (isCancelled()) break; const report = await importOne(memory, waggleDb, source, isCancelled); reports.push(report); onProgress?.(report); } return reports; } async function importOne( memory: MemoryStore, waggleDb: WaggleDB, source: LegacyMemorySource, isCancelled: () => boolean, ): Promise { const scope = scopeKey(source.scope); const report: LegacyMemoryImportReport = { scope, status: 'imported', copied: 0, skipped: 0 }; const state = waggleDb.getMeta(metaKey(source.scope)); if (state === 'done') return { ...report, status: 'already-imported' }; if (!fs.existsSync(source.path) || fs.statSync(source.path).size === 0) { waggleDb.setMeta(metaKey(source.scope), 'done'); return { ...report, status: 'missing' }; } let db: Database.Database | null = null; try { db = new Database(source.path, { readonly: true, fileMustExist: true }); const hasFrames = db.prepare( "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'memory_frames'", ).get(); if (!hasFrames) { waggleDb.setMeta(metaKey(source.scope), 'done'); return { ...report, status: 'missing' }; } let lastId = state ? Number(state) || 0 : 0; const page = db.prepare( `SELECT id, content, importance, source, created_at FROM memory_frames WHERE id > ? ORDER BY id LIMIT ?`, ); for (;;) { const rows = page.all(lastId, BATCH) as LegacyFrameRow[]; if (rows.length === 0) break; for (const row of rows) { if (isCancelled()) { waggleDb.setMeta(metaKey(source.scope), String(lastId)); return report; } const mapped = mapFrame(row); if (mapped) { await memory.remember({ scope: source.scope, text: mapped.text, kind: mapped.kind, importance: mapped.importance, source: mapped.source, origin: 'legacy-import', ...(row.created_at && { metadata: { legacy_created_at: row.created_at } }), }); report.copied++; } else { report.skipped++; } lastId = row.id; } waggleDb.setMeta(metaKey(source.scope), String(lastId)); } waggleDb.setMeta(metaKey(source.scope), 'done'); return report; } catch (err) { return { ...report, status: 'failed', error: err instanceof Error ? err.message : String(err) }; } finally { db?.close(); } } /** Converts a legacy frame, or returns null for one that is not carried over. */ export function mapFrame(row: Pick): { text: string; kind: MemoryKind; importance: MemoryImportance; source: MemorySource; } | null { const content = row.content?.trim() ?? ''; if (!content || row.importance === 'deprecated' || SKIPPED_PREFIX.test(content)) return null; let kind: MemoryKind = 'fact'; let text = content; for (const [pattern, mappedKind, stripHeader] of PREFIX_KINDS) { if (!pattern.test(content)) continue; kind = mappedKind; if (stripHeader) { const newline = content.indexOf('\n'); text = newline >= 0 ? content.slice(newline + 1).trim() : content.replace(pattern, '').trim(); } break; } if (!text) return null; const importance = (MEMORY_IMPORTANCE as readonly string[]).includes(row.importance) ? row.importance as MemoryImportance : 'normal'; const source = row.source && (MEMORY_SOURCES as readonly string[]).includes(row.source) ? row.source as MemorySource : 'import'; return { text, kind, importance, source }; }