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.
This commit is contained in:
shumeiko.it
2026-10-23 14:31:28 +02:00
parent 20567ecb10
commit ad2ca875cb
65 changed files with 6131 additions and 201 deletions

View File

@@ -12,7 +12,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { MindDB } from '@waggle/core';
import { AwarenessLayer, MindDB, WaggleDB, workspaceDataScope } from '@waggle/core';
import {
extractProgressItems,
extractOpenQuestions,
@@ -112,19 +112,8 @@ function extractDecisionItems(
// ── Active items from awareness layer ──────────────────────────────────
interface AwarenessRow {
id: number;
category: string;
content: string;
created_at: string;
}
function extractAwarenessItems(raw: ReturnType<MindDB['getDatabase']>): StateItem[] {
const items = raw.prepare(
`SELECT id, category, content, created_at FROM awareness
WHERE expires_at IS NULL OR expires_at > datetime('now')
ORDER BY priority DESC LIMIT 10`,
).all() as AwarenessRow[];
function extractAwarenessItems(waggleDb: WaggleDB, workspaceId: string): StateItem[] {
const items = new AwarenessLayer(waggleDb, workspaceDataScope(workspaceId)).getAll();
return items
.filter(i => i.category === 'task' || i.category === 'action' || i.category === 'pending')
@@ -228,12 +217,14 @@ export interface BuildWorkspaceStateOpts {
dataDir: string;
workspaceId: string;
wsManager: WsManagerLike;
/** waggle.db, which holds the workspace's awareness items. */
waggleDb: WaggleDB;
/** @deprecated Read-only state construction no longer mutates global workspace state. */
activateWorkspaceMind?: (id: string) => boolean;
}
export function buildWorkspaceState(opts: BuildWorkspaceStateOpts): WorkspaceState | null {
const { dataDir, workspaceId, wsManager } = opts;
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
const ws = wsManager.get(workspaceId);
if (!ws) return null;
@@ -257,7 +248,6 @@ export function buildWorkspaceState(opts: BuildWorkspaceStateOpts): WorkspaceSta
}
recentDecisions = extractDecisionItems(raw, 5);
awarenessItems = extractAwarenessItems(raw);
wsDb.close();
wsDb = null;
@@ -266,6 +256,10 @@ export function buildWorkspaceState(opts: BuildWorkspaceStateOpts): WorkspaceSta
return null;
}
// Awareness items live in waggle.db; failing to read them must not drop the
// memory-sourced state above.
try { awarenessItems = extractAwarenessItems(waggleDb, workspaceId); } catch { /* non-blocking */ }
// ── Session-sourced state ────────────────────────────────────────
const sessionsDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');