Files
waggle-os/packages/core/src/waggle-db.ts
shumeiko.it ad2ca875cb feat(core): give Waggle its own database, apart from memory (P1.5)
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.
2026-10-23 14:31:28 +02:00

79 lines
2.4 KiB
TypeScript

/**
* waggle.db — Waggle's own data, kept apart from memory (ADR-0007).
*
* Holds what the app needs to run: automations, notifications and held
* actions, skill hashes, thumbs feedback, cost tracking (execution traces and
* budget reservations), the user profile card and awareness items. Facts the
* agent learns about the user live in the memory store, not here.
*
* Stores that belong here take a `WaggleDB`, not a generic SQLite handle, so
* passing a memory database by mistake is a type error.
*/
import fs from 'node:fs';
import path from 'node:path';
import Database from 'better-sqlite3';
import type { Database as DatabaseType } from 'better-sqlite3';
export const WAGGLE_DB_FILE = 'waggle.db';
const BUSY_TIMEOUT_MS = 10_000;
const META_TABLE_SQL = `
CREATE TABLE IF NOT EXISTS waggle_meta (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)`;
export class WaggleDB {
private readonly db: DatabaseType;
/** Opens (and creates if needed) the database at `dbPath`; `':memory:'` is accepted. */
constructor(readonly dbPath: string) {
if (dbPath !== ':memory:') {
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
}
this.db = new Database(dbPath);
this.db.pragma('journal_mode = WAL');
this.db.pragma('foreign_keys = ON');
this.db.pragma(`busy_timeout = ${BUSY_TIMEOUT_MS}`);
this.db.exec(META_TABLE_SQL);
}
/** Opens `<dataDir>/waggle.db`. */
static openInDataDir(dataDir: string): WaggleDB {
return new WaggleDB(path.join(dataDir, WAGGLE_DB_FILE));
}
getDatabase(): DatabaseType {
return this.db;
}
getMeta(key: string): string | null {
const row = this.db.prepare('SELECT value FROM waggle_meta WHERE key = ?').get(key) as
| { value: string }
| undefined;
return row?.value ?? null;
}
setMeta(key: string, value: string): void {
this.db
.prepare('INSERT INTO waggle_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value')
.run(key, value);
}
close(): void {
if (this.db.open) this.db.close();
}
}
/** Data scope of a profile or awareness item: the personal mind or one workspace. */
export type DataScope = 'personal' | `workspace:${string}`;
export const PERSONAL_DATA_SCOPE: DataScope = 'personal';
export function workspaceDataScope(workspaceId: string): DataScope {
if (!workspaceId.trim()) throw new TypeError('workspaceId must be a non-empty string');
return `workspace:${workspaceId}`;
}