diff --git a/packages/core/tests/embeddings/transformers-model-load.test.ts b/packages/core/tests/embeddings/transformers-model-load.test.ts index 2cb4be7..cf63215 100644 --- a/packages/core/tests/embeddings/transformers-model-load.test.ts +++ b/packages/core/tests/embeddings/transformers-model-load.test.ts @@ -27,7 +27,7 @@ vi.mock('@huggingface/transformers', () => ({ })); import { createInProcessEmbedder } from '../../src/embeddings/inprocess-embedder.js'; -import { createInProcessReranker } from '@waggle/hive-mind-core'; +import { createInProcessReranker } from '../../src/recall/inprocess-reranker.js'; import { modelLoadLockPath, withTransformersModelLoad, diff --git a/packages/core/tests/install-audit-check-parity.test.ts b/packages/core/tests/install-audit-check-parity.test.ts index 9b49122..e93571b 100644 --- a/packages/core/tests/install-audit-check-parity.test.ts +++ b/packages/core/tests/install-audit-check-parity.test.ts @@ -1,19 +1,15 @@ /** * P7/D15 A3 — drift-lock for the install_audit CHECK constraints. * - * The table is declared in TWO places: install-audit.ts (core, generated from - * the canonical @waggle/shared arrays) and hive-mind-core/src/mind/schema.ts - * (a private-monorepo literal). They MUST produce identical CHECK lists or - * auditStore.record() crashes on one path. The interleaved DDL is stripped from - * the curated OSS export. This test pins both to the - * single canonical source, so a drift in either fails CI instead of production. + * install-audit.ts generates the table DDL from the canonical @waggle/shared + * arrays. This test pins every CHECK list to that single source, so a drift + * fails CI instead of crashing auditStore.record() in production. */ import { describe, it, expect } from 'vitest'; import { sqlInList, RISK_LEVELS, APPROVAL_CLASSES, AUDIT_ACTIONS, AUDIT_CAPABILITY_TYPES, AUDIT_INITIATORS, TRUST_SOURCES, } from '@waggle/shared'; -import { SCHEMA_SQL } from '@waggle/hive-mind-core'; import { INSTALL_AUDIT_TABLE_SQL } from '../src/install-audit.js'; const COLUMNS = [ @@ -32,14 +28,9 @@ describe('install_audit CHECK parity (A3)', () => { it(`core install-audit DDL pins ${col} to the canonical list`, () => { expect(INSTALL_AUDIT_TABLE_SQL).toContain(expected); }); - - it(`private substrate schema.ts pins ${col} to the canonical list`, () => { - expect(SCHEMA_SQL).toContain(expected); - }); } - it('both DDLs accept the P5/D4 uninstalled action', () => { + it('the DDL accepts the P5/D4 uninstalled action', () => { expect(INSTALL_AUDIT_TABLE_SQL).toContain("'uninstalled'"); - expect(SCHEMA_SQL).toContain("'uninstalled'"); }); }); diff --git a/packages/core/tests/install-audit.test.ts b/packages/core/tests/install-audit.test.ts index c78fdfb..39bcf7b 100644 --- a/packages/core/tests/install-audit.test.ts +++ b/packages/core/tests/install-audit.test.ts @@ -1,30 +1,21 @@ /** - * InstallAuditStore tests. - * - * Relocated here from packages/hive-mind-core/tests/mind/ — commit 05c9ec3 - * ("relocate substrate tests") moved this test to hive-mind-core, but the - * source (`install-audit.ts`) stayed in @waggle/core and imports MindDB FROM - * @waggle/hive-mind-core. hive-mind-core cannot depend back on @waggle/core - * (dependency inversion + breaks the OSS parity model), so the relocated test - * imported a non-existent `../../src/install-audit.js` and the entire suite - * silently failed at collection — masking the FIX-3 CHECK-drift regression. - * Its correct home is alongside the source, in @waggle/core. + * InstallAuditStore tests. The audit trail lives in waggle.db (ADR-0007). */ import { describe, it, expect, beforeEach, 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 { WaggleDB } from '../src/waggle-db.js'; import { InstallAuditStore, type RecordAuditInput } from '../src/install-audit.js'; describe('InstallAuditStore', () => { let tmpDir: string; - let db: MindDB; + let db: WaggleDB; let store: InstallAuditStore; beforeEach(() => { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-audit-')); - db = new MindDB(path.join(tmpDir, 'test.mind')); + db = new WaggleDB(path.join(tmpDir, 'waggle.db')); store = new InstallAuditStore(db); }); @@ -56,11 +47,10 @@ describe('InstallAuditStore', () => { it("migrates a legacy-CHECK table to accept 'uninstalled' (P5/D4)", () => { const tmp2 = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-audit-legacy-')); - const legacyDb = new MindDB(path.join(tmp2, 'legacy.mind')); + const legacyDb = new WaggleDB(path.join(tmp2, 'waggle.db')); const raw = legacyDb.getDatabase(); - // Simulate a pre-P5 install: drop the migrated table and recreate it with - // the OLD narrower action CHECK (no 'uninstalled'), seeding one row. - raw.exec('DROP TABLE IF EXISTS install_audit'); + // Simulate a pre-P5 install: create the table with the OLD narrower action + // CHECK (no 'uninstalled'), seeding one row. raw.exec(` CREATE TABLE install_audit ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -290,7 +280,7 @@ describe('InstallAuditStore — legacy CHECK migration', () => { beforeEach(() => { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-audit-mig-')); - dbPath = path.join(tmpDir, 'legacy.mind'); + dbPath = path.join(tmpDir, 'waggle.db'); }); afterEach(() => { @@ -313,13 +303,12 @@ describe('InstallAuditStore — legacy CHECK migration', () => { )`; it('rebuilds a legacy install_audit table (narrow CHECK) and preserves rows', () => { - // 1. Create the mind DB, then forcibly downgrade install_audit to the - // historical 4-value CHECK with one legacy row — simulating a real - // user .mind created before connector/marketplace existed. + // 1. Create install_audit with the historical 4-value CHECK and one legacy + // row — the shape of an audit table created before connector/marketplace + // existed. { - const seed = new MindDB(dbPath); + const seed = new WaggleDB(dbPath); const raw = seed.getDatabase(); - raw.prepare('DROP TABLE IF EXISTS install_audit').run(); raw.prepare(OLD_DDL).run(); raw.prepare(`INSERT INTO install_audit (capability_name, capability_type, source, risk_level, trust_source, approval_class, action, initiator, detail) @@ -327,8 +316,8 @@ describe('InstallAuditStore — legacy CHECK migration', () => { seed.close(); } - // 2. Reopen — runMigrations() must rebuild install_audit with the widened CHECK. - const db = new MindDB(dbPath); + // 2. Reopen — the store must rebuild install_audit with the widened CHECK. + const db = new WaggleDB(dbPath); const store = new InstallAuditStore(db); const legacy = store.getByCapability('legacy-skill'); @@ -347,10 +336,8 @@ describe('InstallAuditStore — legacy CHECK migration', () => { }); // M2 (UX-Refactor Phase 4 / C15): the FIX-3-era DDL had every list widened - // EXCEPT risk_level — the exact shape real .minds created between FIX-3 - // (2026-05-17) and Phase 4 are in. Only the new "'low', 'medium', 'high', - // 'critical'" sentinel triggers this rebuild ('critical' alone appears in - // approval_class, so it is NOT the key). + // EXCEPT risk_level — the exact shape of audit tables created between FIX-3 + // (2026-05-17) and Phase 4. const FIX3_ERA_DDL = `CREATE TABLE install_audit ( id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp TEXT NOT NULL DEFAULT (datetime('now')), @@ -368,9 +355,8 @@ describe('InstallAuditStore — legacy CHECK migration', () => { it("M2: rebuilds a FIX-3-era table (risk_level missing 'critical') and preserves rows", () => { { - const seed = new MindDB(dbPath); + const seed = new WaggleDB(dbPath); const raw = seed.getDatabase(); - raw.prepare('DROP TABLE IF EXISTS install_audit').run(); raw.prepare(FIX3_ERA_DDL).run(); raw.prepare(`INSERT INTO install_audit (capability_name, capability_type, source, risk_level, trust_source, approval_class, action, initiator, detail) @@ -383,8 +369,8 @@ describe('InstallAuditStore — legacy CHECK migration', () => { seed.close(); } - // Reopen — runMigrations() must rebuild keyed on the M2 sentinel. - const db = new MindDB(dbPath); + // Reopen — the store must rebuild the table with the widened CHECK. + const db = new WaggleDB(dbPath); const store = new InstallAuditStore(db); const legacy = store.getByCapability('legacy-mcp'); @@ -401,7 +387,7 @@ describe('InstallAuditStore — legacy CHECK migration', () => { db.close(); // Idempotence: a second reopen must NOT rebuild again (rows + ids stable). - const db2 = new MindDB(dbPath); + const db2 = new WaggleDB(dbPath); const store2 = new InstallAuditStore(db2); const all = store2.getAll(); expect(all).toHaveLength(2); @@ -414,66 +400,4 @@ describe('InstallAuditStore — legacy CHECK migration', () => { })).not.toThrow(); db2.close(); }); - - // The rebuild now runs in ONE transaction, so a crash mid-rebuild rolls - // back — but DBs damaged by a PRE-transactional crashed rebuild exist in the - // wild with all rows stranded in install_audit__mig_old. These lock the - // recovery the FIX-3 comment always promised but never performed. - const LEGACY_ROW_INSERT = (table: string) => `INSERT INTO ${table} - (capability_name, capability_type, source, risk_level, trust_source, approval_class, action, initiator, detail) - VALUES ('stranded','mcp','marketplace','high','security-gate','blocked','blocked','system','pre-crash row')`; - - it('recovers rows stranded by a crash between RENAME and recreate (install_audit missing)', () => { - { - const seed = new MindDB(dbPath); - const raw = seed.getDatabase(); - raw.prepare('DROP TABLE IF EXISTS install_audit').run(); - raw.prepare(FIX3_ERA_DDL).run(); - raw.prepare(LEGACY_ROW_INSERT('install_audit')).run(); - // Simulate the pre-transactional crash: renamed aside, then process died - // before SCHEMA_SQL recreated install_audit. - raw.prepare('ALTER TABLE install_audit RENAME TO install_audit__mig_old').run(); - seed.close(); - } - - const db = new MindDB(dbPath); - const store = new InstallAuditStore(db); - // Rows restored AND the rebuild completed (the restored table had the - // FIX-3-era DDL, so the M2 sentinel re-triggered the rebuild) - const rows = store.getByCapability('stranded'); - expect(rows).toHaveLength(1); - expect(rows[0].detail).toBe('pre-crash row'); - expect(() => store.record({ - capabilityName: 'post-recovery', capabilityType: 'mcp', source: 'mcp', - riskLevel: 'critical', trustSource: 'security-gate', approvalClass: 'blocked', - action: 'blocked', initiator: 'system', - })).not.toThrow(); - // No stale __mig_old left to be destroyed by a future rebuild - const leftover = db.getDatabase().prepare( - "SELECT name FROM sqlite_master WHERE type='table' AND name='install_audit__mig_old'", - ).get(); - expect(leftover).toBeUndefined(); - db.close(); - }); - - it('recovers rows stranded by a crash between recreate and copy-back (fresh empty install_audit)', () => { - { - const seed = new MindDB(dbPath); // creates the CURRENT empty install_audit - const raw = seed.getDatabase(); - raw.prepare(FIX3_ERA_DDL.replace('CREATE TABLE install_audit', 'CREATE TABLE install_audit__mig_old')).run(); - raw.prepare(LEGACY_ROW_INSERT('install_audit__mig_old')).run(); - seed.close(); - } - - const db = new MindDB(dbPath); - const store = new InstallAuditStore(db); - const rows = store.getByCapability('stranded'); - expect(rows).toHaveLength(1); - expect(rows[0].detail).toBe('pre-crash row'); - const leftover = db.getDatabase().prepare( - "SELECT name FROM sqlite_master WHERE type='table' AND name='install_audit__mig_old'", - ).get(); - expect(leftover).toBeUndefined(); - db.close(); - }); }); diff --git a/packages/core/tests/workspace-manager.test.ts b/packages/core/tests/workspace-manager.test.ts index 93a4fea..b137b5f 100644 --- a/packages/core/tests/workspace-manager.test.ts +++ b/packages/core/tests/workspace-manager.test.ts @@ -3,7 +3,6 @@ import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; import { WorkspaceManager, type WorkspaceConfig } from '../src/workspace-manager.js'; -import { MultiMindCache } from '@waggle/hive-mind-core'; describe('WorkspaceManager', () => { let tmpDir: string; @@ -35,7 +34,7 @@ describe('WorkspaceManager', () => { }); describe('create', () => { - it('creates workspace with directory, config, mind file, and sessions dir', () => { + it('creates workspace with directory, config and sessions dir, and no mind file', () => { const ws = manager.create({ name: 'My Project', group: 'Work' }); expect(ws.id).toBe('my-project'); @@ -46,7 +45,7 @@ describe('WorkspaceManager', () => { const wsDir = path.join(tmpDir, 'workspaces', 'my-project'); expect(fs.existsSync(wsDir)).toBe(true); expect(fs.existsSync(path.join(wsDir, 'workspace.json'))).toBe(true); - expect(fs.existsSync(path.join(wsDir, 'workspace.mind'))).toBe(true); + expect(fs.existsSync(path.join(wsDir, 'workspace.mind'))).toBe(false); expect(fs.existsSync(path.join(wsDir, 'sessions'))).toBe(true); expect(fs.statSync(path.join(wsDir, 'sessions')).isDirectory()).toBe(true); }); @@ -233,11 +232,13 @@ describe('WorkspaceManager', () => { }); describe('getMindPath', () => { - it('returns path to workspace.mind', () => { + it('returns path to a legacy workspace.mind', () => { manager.create({ name: 'Mind Test', group: 'Work' }); + const legacyMind = path.join(tmpDir, 'workspaces', 'mind-test', 'workspace.mind'); + fs.writeFileSync(legacyMind, 'legacy mind'); const mindPath = manager.getMindPath('mind-test'); - expect(mindPath).toBe(path.join(tmpDir, 'workspaces', 'mind-test', 'workspace.mind')); + expect(mindPath).toBe(legacyMind); }); it.each(['missing-workspace', '..', '../escape', 'C:\\escape']) ( @@ -260,7 +261,6 @@ describe('WorkspaceManager', () => { manager.create({ name: 'Linked Mind', group: 'Work' }); const mindPath = path.join(tmpDir, 'workspaces', 'linked-mind', 'workspace.mind'); const missingTarget = path.join(tmpDir, 'missing-mind-target'); - fs.unlinkSync(mindPath); fs.symlinkSync(missingTarget, mindPath, process.platform === 'win32' ? 'junction' : 'dir'); expect(() => manager.getMindPath('linked-mind')).toThrow(/regular file/i); @@ -273,60 +273,18 @@ describe('WorkspaceManager', () => { const mindPath = path.join(tmpDir, 'workspaces', 'hard-linked-mind', 'workspace.mind'); const outsidePath = path.join(tmpDir, 'outside.mind'); fs.writeFileSync(outsidePath, 'outside sentinel'); - fs.unlinkSync(mindPath); fs.linkSync(outsidePath, mindPath); expect(() => manager.getMindPath('hard-linked-mind')).toThrow(/regular file/i); expect(fs.readFileSync(outsidePath, 'utf8')).toBe('outside sentinel'); }); - it('allows a valid workspace to recreate a missing mind inside its directory', () => { + it('returns the mind path of a workspace without a mind file and does not create one', () => { manager.create({ name: 'Missing Mind', group: 'Work' }); const mindPath = path.join(tmpDir, 'workspaces', 'missing-mind', 'workspace.mind'); - fs.unlinkSync(mindPath); + expect(manager.getMindPath('missing-mind')).toBe(mindPath); - - const cache = new MultiMindCache({ - maxOpen: 2, - getMindPath: id => manager.getMindPath(id), - allowedRoot: path.join(tmpDir, 'workspaces'), - }); - expect(cache.getOrOpen('missing-mind')).not.toBeNull(); - expect(fs.statSync(mindPath).isFile()).toBe(true); - cache.closeAll(); - }); - - it('contains resolver failures and rejects a post-resolution junction swap', () => { - const throwingCache = new MultiMindCache({ - maxOpen: 2, - getMindPath: () => { throw new Error('unsafe workspace'); }, - allowedRoot: path.join(tmpDir, 'workspaces'), - }); - expect(throwingCache.getOrOpen('missing')).toBeNull(); - - manager.create({ name: 'Swap Target', group: 'Work' }); - const workspaceDir = path.join(tmpDir, 'workspaces', 'swap-target'); - const outsideDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ws-swap-')); - const outsideMind = path.join(outsideDir, 'workspace.mind'); - fs.writeFileSync(outsideMind, 'outside sentinel'); - const cache = new MultiMindCache({ - maxOpen: 2, - allowedRoot: path.join(tmpDir, 'workspaces'), - getMindPath: id => { - const resolved = manager.getMindPath(id); - fs.rmSync(workspaceDir, { recursive: true, force: true }); - fs.symlinkSync(outsideDir, workspaceDir, process.platform === 'win32' ? 'junction' : 'dir'); - return resolved; - }, - }); - - try { - expect(cache.getOrOpen('swap-target')).toBeNull(); - expect(fs.readFileSync(outsideMind, 'utf8')).toBe('outside sentinel'); - } finally { - fs.unlinkSync(workspaceDir); - fs.rmSync(outsideDir, { recursive: true, force: true }); - } + expect(fs.existsSync(mindPath)).toBe(false); }); }); @@ -492,7 +450,7 @@ describe('WorkspaceManager', () => { expect(workspace.id).toBe('default'); expect(fs.statSync(sessionsDir).isDirectory()).toBe(true); - expect(fs.existsSync(path.join(workspaceDir, 'workspace.mind'))).toBe(true); + expect(fs.existsSync(path.join(workspaceDir, 'workspace.mind'))).toBe(false); expect(manager.get('default')).toEqual(workspace); }, ); @@ -544,7 +502,7 @@ describe('WorkspaceManager', () => { const wsDir = path.join(tmpDir, 'workspaces', 'cwd-derived-id'); expect(fs.existsSync(path.join(wsDir, 'workspace.json'))).toBe(true); - expect(fs.existsSync(path.join(wsDir, 'workspace.mind'))).toBe(true); + expect(fs.existsSync(path.join(wsDir, 'workspace.mind'))).toBe(false); expect(fs.existsSync(path.join(wsDir, 'sessions'))).toBe(true); });