refactor(core): remove the single-mind to multi-mind migration (P1.2)
needsMigration and migrateToMultiMind moved a pre-M4 default.mind to personal.mind at startup. The community edition has no installs with that layout, so the module, its export, the startup step, its "migration" progress phase and the default.mind first-run check go.
This commit is contained in:
@@ -97,8 +97,8 @@ packages/shared/src/ # shared types, Zod schemas, MCP catalog
|
||||
Already removed: tiers and Stripe (P0.1); KVARK, enterprise packs, the compliance
|
||||
subsystem, self-learning and `packages/optimizer` (P1.4, P1.6, ADR-0004); the team and
|
||||
WaggleDance routes in the sidecar and `team-sync` in core, with the team server moved to
|
||||
`packages/team-server` (P1.1). Still to be removed, so do not build on them:
|
||||
`install-audit.ts` and `migration` in core (P1.2).
|
||||
`packages/team-server` (P1.1); the single-mind to multi-mind migration in core (P1.2).
|
||||
Still to be removed, so do not build on it: `install-audit.ts` in core (P1.2).
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -56,7 +56,6 @@ export { importLegacyMindData, type LegacyMindSource, type LegacyImportReport }
|
||||
|
||||
// ── Waggle-specific orchestration ──
|
||||
export { WaggleConfig, type ProviderEntry } from './config.js';
|
||||
export { needsMigration, migrateToMultiMind } from './migration.js';
|
||||
export {
|
||||
InstallAuditStore, INSTALL_AUDIT_TABLE_SQL,
|
||||
type InstallAuditEntry, type RecordAuditInput,
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
|
||||
/**
|
||||
* Check if a migration from default.mind to personal.mind is needed.
|
||||
* Returns true if default.mind exists AND personal.mind does NOT exist.
|
||||
*/
|
||||
export function needsMigration(waggleDir: string): boolean {
|
||||
const defaultMind = path.join(waggleDir, 'default.mind');
|
||||
const personalMind = path.join(waggleDir, 'personal.mind');
|
||||
return fs.existsSync(defaultMind) && !fs.existsSync(personalMind);
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate from the old single-mind layout (default.mind) to the
|
||||
* M4 multi-mind layout (personal.mind + workspaces/).
|
||||
*
|
||||
* Steps when migration is needed:
|
||||
* 1. Copy default.mind -> personal.mind
|
||||
* 2. Rename default.mind -> default.mind.bak
|
||||
* 3. Create workspaces/ directory
|
||||
*
|
||||
* This is a pure file operation -- no data transformation is performed.
|
||||
*/
|
||||
export function migrateToMultiMind(waggleDir: string): { migrated: boolean; message: string } {
|
||||
if (!needsMigration(waggleDir)) {
|
||||
return { migrated: false, message: 'No migration needed' };
|
||||
}
|
||||
|
||||
const defaultMind = path.join(waggleDir, 'default.mind');
|
||||
const personalMind = path.join(waggleDir, 'personal.mind');
|
||||
const backupMind = path.join(waggleDir, 'default.mind.bak');
|
||||
const workspacesDir = path.join(waggleDir, 'workspaces');
|
||||
|
||||
// 1. Copy default.mind -> personal.mind
|
||||
fs.copyFileSync(defaultMind, personalMind);
|
||||
|
||||
// 2. Rename default.mind -> default.mind.bak
|
||||
fs.renameSync(defaultMind, backupMind);
|
||||
|
||||
// 3. Create workspaces/ directory (idempotent)
|
||||
fs.mkdirSync(workspacesDir, { recursive: true });
|
||||
|
||||
return { migrated: true, message: 'Migrated default.mind to personal.mind' };
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
import { describe, it, expect, afterEach } from 'vitest';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import * as os from 'node:os';
|
||||
import { MindDB } from '@waggle/hive-mind-core';
|
||||
import { FrameStore } from '@waggle/hive-mind-core';
|
||||
import { needsMigration, migrateToMultiMind } from '../src/migration.js';
|
||||
|
||||
describe('Migration: default.mind → personal.mind', () => {
|
||||
let tmpDir: string;
|
||||
|
||||
function makeTmpDir(): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-migration-'));
|
||||
return dir;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
if (tmpDir && fs.existsSync(tmpDir)) {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe('needsMigration', () => {
|
||||
it('returns true when default.mind exists and personal.mind does not', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
// Create a real MindDB so it's a valid SQLite file
|
||||
const db = new MindDB(path.join(tmpDir, 'default.mind'));
|
||||
db.close();
|
||||
|
||||
expect(needsMigration(tmpDir)).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false when personal.mind already exists', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
// Both exist
|
||||
const db1 = new MindDB(path.join(tmpDir, 'default.mind'));
|
||||
db1.close();
|
||||
const db2 = new MindDB(path.join(tmpDir, 'personal.mind'));
|
||||
db2.close();
|
||||
|
||||
expect(needsMigration(tmpDir)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false on fresh install (nothing exists)', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
expect(needsMigration(tmpDir)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('migrateToMultiMind', () => {
|
||||
it('migrates default.mind to personal.mind with data preserved', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
const defaultPath = path.join(tmpDir, 'default.mind');
|
||||
|
||||
// Create a MindDB with real data
|
||||
const db = new MindDB(defaultPath);
|
||||
// Create a session (foreign key requirement)
|
||||
db.getDatabase().prepare(
|
||||
"INSERT OR IGNORE INTO sessions (gop_id, status) VALUES (?, 'active')"
|
||||
).run('test-gop');
|
||||
const frames = new FrameStore(db);
|
||||
frames.createIFrame('test-gop', 'migration test content', 'normal');
|
||||
db.close();
|
||||
|
||||
const result = migrateToMultiMind(tmpDir);
|
||||
|
||||
expect(result.migrated).toBe(true);
|
||||
expect(result.message).toBe('Migrated default.mind to personal.mind');
|
||||
|
||||
// Verify personal.mind has the data
|
||||
const personalPath = path.join(tmpDir, 'personal.mind');
|
||||
expect(fs.existsSync(personalPath)).toBe(true);
|
||||
|
||||
const personalDb = new MindDB(personalPath);
|
||||
const personalFrames = new FrameStore(personalDb);
|
||||
const state = personalFrames.reconstructState('test-gop');
|
||||
expect(state.iframe).not.toBeNull();
|
||||
expect(state.iframe!.content).toBe('migration test content');
|
||||
personalDb.close();
|
||||
});
|
||||
|
||||
it('keeps default.mind as backup (.bak) after migration', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
const db = new MindDB(path.join(tmpDir, 'default.mind'));
|
||||
db.close();
|
||||
|
||||
migrateToMultiMind(tmpDir);
|
||||
|
||||
expect(fs.existsSync(path.join(tmpDir, 'default.mind.bak'))).toBe(true);
|
||||
expect(fs.existsSync(path.join(tmpDir, 'default.mind'))).toBe(false);
|
||||
});
|
||||
|
||||
it('creates workspaces directory', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
const db = new MindDB(path.join(tmpDir, 'default.mind'));
|
||||
db.close();
|
||||
|
||||
migrateToMultiMind(tmpDir);
|
||||
|
||||
const wsDir = path.join(tmpDir, 'workspaces');
|
||||
expect(fs.existsSync(wsDir)).toBe(true);
|
||||
expect(fs.statSync(wsDir).isDirectory()).toBe(true);
|
||||
});
|
||||
|
||||
it('does not migrate twice (idempotent)', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
const db = new MindDB(path.join(tmpDir, 'default.mind'));
|
||||
db.close();
|
||||
|
||||
const first = migrateToMultiMind(tmpDir);
|
||||
expect(first.migrated).toBe(true);
|
||||
|
||||
const second = migrateToMultiMind(tmpDir);
|
||||
expect(second.migrated).toBe(false);
|
||||
expect(second.message).toBe('No migration needed');
|
||||
});
|
||||
|
||||
it('returns no migration needed when nothing exists', () => {
|
||||
tmpDir = makeTmpDir();
|
||||
const result = migrateToMultiMind(tmpDir);
|
||||
expect(result.migrated).toBe(false);
|
||||
expect(result.message).toBe('No migration needed');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import net from 'node:net';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { needsMigration, migrateToMultiMind, VaultStore, WaggleConfig, WAGGLE_DB_FILE } from '@waggle/core';
|
||||
import { VaultStore, WaggleConfig, WAGGLE_DB_FILE } from '@waggle/core';
|
||||
import { buildLocalServer } from './index.js';
|
||||
import type { LlmHealthStatus } from './index.js';
|
||||
import { startLiteLLM, stopLiteLLM, type LiteLLMStatus } from './lifecycle.js';
|
||||
@@ -28,7 +28,7 @@ const log = createLogger('service');
|
||||
|
||||
// ── Startup progress types ─────────────────────────────────────────
|
||||
|
||||
export type StartupPhase = 'init' | 'migration' | 'litellm' | 'server' | 'ready';
|
||||
export type StartupPhase = 'init' | 'litellm' | 'server' | 'ready';
|
||||
|
||||
export interface StartupEvent {
|
||||
phase: StartupPhase;
|
||||
@@ -114,12 +114,11 @@ export function resolveServicePort(optionPort?: number): number {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a fresh install: no waggle.db and no legacy personal.mind
|
||||
* or default.mind.
|
||||
* Check if this is a fresh install: no waggle.db and no legacy personal.mind.
|
||||
*/
|
||||
export function isFirstRun(dataDir: string): boolean {
|
||||
if (!fs.existsSync(dataDir)) return true;
|
||||
return ![WAGGLE_DB_FILE, 'personal.mind', 'default.mind']
|
||||
return ![WAGGLE_DB_FILE, 'personal.mind']
|
||||
.some((file) => fs.existsSync(path.join(dataDir, file)));
|
||||
}
|
||||
|
||||
@@ -173,10 +172,9 @@ function getConfiguredProviderIds(dataDir: string, server?: FastifyInstance): st
|
||||
* Start the Waggle agent service.
|
||||
*
|
||||
* 1. Resolves/creates dataDir (option > WAGGLE_DATA_DIR > ~/.waggle)
|
||||
* 2. Runs migration if needed (default.mind -> personal.mind)
|
||||
* 3. Starts the LiteLLM proxy when config.json enables it and it is not skipped
|
||||
* 4. Builds & starts local Fastify server
|
||||
* 5. Registers graceful shutdown handlers
|
||||
* 2. Starts the LiteLLM proxy when config.json enables it and it is not skipped
|
||||
* 3. Builds & starts local Fastify server
|
||||
* 4. Registers graceful shutdown handlers
|
||||
*
|
||||
* Optionally accepts an onProgress callback to emit startup phase events
|
||||
* for UI splash screen display.
|
||||
@@ -229,21 +227,8 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
}
|
||||
|
||||
// 2. Check/run migration
|
||||
if (needsMigration(dataDir)) {
|
||||
emit({ phase: 'migration', message: 'Migrating to multi-mind layout...', progress: 0.15 });
|
||||
try {
|
||||
migrateToMultiMind(dataDir);
|
||||
emit({ phase: 'migration', message: 'Migration complete', progress: 0.2 });
|
||||
} catch (err) {
|
||||
const msg = `Migration failed: ${err instanceof Error ? err.message : String(err)}`;
|
||||
emit({ phase: 'migration', message: msg, progress: 0.2 });
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// D11: ONE startup line answering "which state am I running against?" —
|
||||
// the resolved dataDir. Logged after wipe/migration so it reflects the
|
||||
// the resolved dataDir. Logged after the wipe so it reflects the
|
||||
// directory the server will actually serve.
|
||||
log.info(`Data dir: ${dataDir}`);
|
||||
|
||||
|
||||
@@ -62,13 +62,6 @@ describe('isFirstRun', () => {
|
||||
expect(isFirstRun(dir)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false when default.mind exists (needs migration)', () => {
|
||||
const dir = makeTmpDir();
|
||||
tmpDirs.push(dir);
|
||||
fs.writeFileSync(path.join(dir, 'default.mind'), '');
|
||||
expect(isFirstRun(dir)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true when directory does not exist yet', () => {
|
||||
const dir = path.join(os.tmpdir(), 'waggle-nonexistent-' + Date.now());
|
||||
// Don't create it
|
||||
@@ -112,9 +105,6 @@ describe('startService onProgress', () => {
|
||||
expect(phases).toContain('init');
|
||||
expect(phases).toContain('ready');
|
||||
|
||||
// Should NOT have migration phase (fresh install, no default.mind)
|
||||
expect(phases).not.toContain('migration');
|
||||
|
||||
// Progress should go from 0 to 1
|
||||
expect(events[0].progress).toBeLessThanOrEqual(0.2);
|
||||
expect(events[events.length - 1].progress).toBe(1);
|
||||
@@ -126,29 +116,6 @@ describe('startService onProgress', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('emits migration phase when default.mind exists', async () => {
|
||||
const dataDir = makeTmpDir();
|
||||
tmpDirs.push(dataDir);
|
||||
|
||||
// Create default.mind to trigger migration
|
||||
fs.writeFileSync(path.join(dataDir, 'default.mind'), '');
|
||||
|
||||
const port = randomPort();
|
||||
const events: StartupEvent[] = [];
|
||||
|
||||
const { server } = await startService({
|
||||
dataDir,
|
||||
port,
|
||||
skipLiteLLM: true,
|
||||
onProgress: (event) => events.push(event),
|
||||
});
|
||||
cleanups.push(async () => { await server.close(); });
|
||||
|
||||
const phases = events.map(e => e.phase);
|
||||
expect(phases).toContain('migration');
|
||||
expect(phases).toContain('ready');
|
||||
});
|
||||
|
||||
it('emits litellm phase when not skipped (but still skips in test)', async () => {
|
||||
const dataDir = makeTmpDir();
|
||||
tmpDirs.push(dataDir);
|
||||
|
||||
Reference in New Issue
Block a user