Waggle kept its own tables in the memory database. Before the memory
engine can be replaced by mem0 they need a home of their own.
- add WaggleDB (<dataDir>/waggle.db). Automations, notifications, held
actions, skill hashes, thumbs feedback, cost tracking, the profile card
and awareness items live there. Their stores take a WaggleDB, so passing
a memory database is a type error.
- the profile card and awareness items are now per scope ('personal' or
'workspace:<id>') in one table each, instead of one table per .mind file
- on the first start, copy these tables from personal.mind and every
workspace.mind (importLegacyMindData). The legacy files are only read,
each scope is copied once and a table only while its target is empty.
- copy the logger, injection scanner, external-memory ingress guard,
WorkspaceManager and the embedders from @waggle/hive-mind-core into
@waggle/core, which now re-exports its own copies
- point the server, agent orchestrator and CLI at waggle.db; update the
tests that seeded these tables in a memory database
ADR-0007 records the layout and the licensing question about the copied
files.
130 lines
6.4 KiB
TypeScript
130 lines
6.4 KiB
TypeScript
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import Database from 'better-sqlite3';
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
import { AwarenessLayer } from '../src/awareness.js';
|
|
import { CronStore } from '../src/cron-store.js';
|
|
import { ExecutionTraceStore } from '../src/execution-traces.js';
|
|
import { IdentityLayer } from '../src/identity.js';
|
|
import { WaggleDB, workspaceDataScope } from '../src/waggle-db.js';
|
|
import { importLegacyMindData } from '../src/waggle-db-import.js';
|
|
|
|
let tmpDir: string;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-db-test-'));
|
|
});
|
|
|
|
afterEach(() => {
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('WaggleDB', () => {
|
|
it('creates waggle.db in the data directory and keeps meta values', () => {
|
|
const db = WaggleDB.openInDataDir(path.join(tmpDir, 'data'));
|
|
db.setMeta('k', 'v1');
|
|
db.setMeta('k', 'v2');
|
|
expect(db.getMeta('k')).toBe('v2');
|
|
expect(db.getMeta('missing')).toBeNull();
|
|
db.close();
|
|
expect(fs.existsSync(path.join(tmpDir, 'data', 'waggle.db'))).toBe(true);
|
|
});
|
|
|
|
it('rejects an empty workspace id', () => {
|
|
expect(() => workspaceDataScope(' ')).toThrow(TypeError);
|
|
});
|
|
});
|
|
|
|
describe('profile and awareness scopes', () => {
|
|
it('keeps one profile per scope', () => {
|
|
const db = new WaggleDB(':memory:');
|
|
const personal = new IdentityLayer(db);
|
|
const ws = new IdentityLayer(db, workspaceDataScope('alpha'));
|
|
personal.create({ name: 'Ana', role: 'Founder', department: '', personality: '', capabilities: '', system_prompt: '' });
|
|
expect(ws.exists()).toBe(false);
|
|
ws.create({ name: 'Alpha team', role: '', department: '', personality: '', capabilities: '', system_prompt: '' });
|
|
expect(personal.update({ role: 'CEO' }).role).toBe('CEO');
|
|
expect(ws.get().name).toBe('Alpha team');
|
|
expect(personal.toContext()).toContain('Role: CEO');
|
|
db.close();
|
|
});
|
|
|
|
it('keeps awareness items apart and refuses cross-scope edits', () => {
|
|
const db = new WaggleDB(':memory:');
|
|
const personal = new AwarenessLayer(db);
|
|
const ws = new AwarenessLayer(db, workspaceDataScope('alpha'));
|
|
const item = ws.add('task', 'Ship the beta', 2, undefined, { status: 'open' });
|
|
personal.add('pending', 'Reply to Marko');
|
|
expect(ws.getAll().map(i => i.content)).toEqual(['Ship the beta']);
|
|
expect(personal.countOpen()).toBe(1);
|
|
expect(personal.get(item.id)).toBeUndefined();
|
|
personal.remove(item.id);
|
|
expect(ws.get(item.id)).toBeDefined();
|
|
expect(ws.updateMetadata(item.id, { result: 'done' }).metadata).toContain('"result":"done"');
|
|
db.close();
|
|
});
|
|
});
|
|
|
|
describe('importLegacyMindData', () => {
|
|
function legacyMind(file: string, setup: (db: Database.Database) => void): string {
|
|
const p = path.join(tmpDir, file);
|
|
const db = new Database(p);
|
|
setup(db);
|
|
db.close();
|
|
return p;
|
|
}
|
|
|
|
it('copies app tables, the profile and awareness once, and leaves the source unchanged', () => {
|
|
const personalPath = legacyMind('personal.mind', (db) => {
|
|
db.exec(`
|
|
CREATE TABLE identity (id INTEGER PRIMARY KEY CHECK (id = 1), name TEXT NOT NULL, role TEXT NOT NULL DEFAULT '',
|
|
department TEXT NOT NULL DEFAULT '', personality TEXT NOT NULL DEFAULT '', capabilities TEXT NOT NULL DEFAULT '',
|
|
system_prompt TEXT NOT NULL DEFAULT '', created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
updated_at TEXT NOT NULL DEFAULT (datetime('now')));
|
|
INSERT INTO identity (id, name, role) VALUES (1, 'Ana', 'Founder');
|
|
CREATE TABLE awareness (id INTEGER PRIMARY KEY AUTOINCREMENT, category TEXT NOT NULL, content TEXT NOT NULL,
|
|
priority INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL DEFAULT (datetime('now')), expires_at TEXT);
|
|
INSERT INTO awareness (category, content) VALUES ('task', 'Draft the plan');
|
|
CREATE TABLE feedback_entries (id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT NOT NULL,
|
|
message_index INTEGER NOT NULL, rating TEXT NOT NULL, reason TEXT, detail TEXT NOT NULL DEFAULT '',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')));
|
|
INSERT INTO feedback_entries (session_id, message_index, rating) VALUES ('s1', 2, 'up');
|
|
`);
|
|
// App tables created by the real stores, as the legacy personal.mind had them.
|
|
const legacy = { getDatabase: () => db } as unknown as WaggleDB;
|
|
new CronStore(legacy).create({ name: 'Morning briefing', cronExpr: '0 8 * * *', jobType: 'proactive' });
|
|
new ExecutionTraceStore(legacy).start({ sessionId: 's1', input: 'hi' });
|
|
});
|
|
const wsPath = legacyMind('workspace.mind', (db) => {
|
|
db.exec(`CREATE TABLE awareness (id INTEGER PRIMARY KEY AUTOINCREMENT, category TEXT NOT NULL, content TEXT NOT NULL,
|
|
priority INTEGER NOT NULL DEFAULT 0, metadata TEXT NOT NULL DEFAULT '{}', created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
expires_at TEXT);
|
|
INSERT INTO awareness (category, content) VALUES ('pending', 'Review the contract');`);
|
|
});
|
|
const before = fs.readFileSync(personalPath);
|
|
|
|
const db = new WaggleDB(path.join(tmpDir, 'waggle.db'));
|
|
const sources = [
|
|
{ path: personalPath, scope: 'personal' as const },
|
|
{ path: wsPath, scope: workspaceDataScope('alpha') },
|
|
{ path: path.join(tmpDir, 'missing.mind'), scope: workspaceDataScope('ghost') },
|
|
];
|
|
const reports = importLegacyMindData(db, sources);
|
|
|
|
expect(reports.map(r => r.status)).toEqual(['imported', 'imported', 'missing']);
|
|
expect(new IdentityLayer(db).get()).toMatchObject({ name: 'Ana', role: 'Founder', scope: 'personal' });
|
|
expect(new AwarenessLayer(db).getAll().map(i => i.content)).toEqual(['Draft the plan']);
|
|
expect(new AwarenessLayer(db, workspaceDataScope('alpha')).getAll().map(i => i.content)).toEqual(['Review the contract']);
|
|
expect(new CronStore(db).list().map(s => s.name)).toEqual(['Morning briefing']);
|
|
expect(db.getDatabase().prepare('SELECT COUNT(*) AS n FROM feedback_entries').get()).toEqual({ n: 1 });
|
|
expect(db.getDatabase().prepare('SELECT COUNT(*) AS n FROM execution_traces').get()).toEqual({ n: 1 });
|
|
expect(fs.readFileSync(personalPath).equals(before)).toBe(true);
|
|
|
|
const again = importLegacyMindData(db, sources);
|
|
expect(again.map(r => r.status)).toEqual(['already-imported', 'already-imported', 'missing']);
|
|
expect(new CronStore(db).list()).toHaveLength(1);
|
|
db.close();
|
|
});
|
|
});
|