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 type { WorkspaceManager, WorkspaceConfig } from '@waggle/core';
import { AwarenessLayer, workspaceDataScope, type WorkspaceManager, type WorkspaceConfig } from '@waggle/core';
import { materialFingerprint } from './notification-gate.js';
// ── Types ──────────────────────────────────────────────────────────────
@@ -34,6 +34,8 @@ export interface ProactiveContext {
workspaceManager: WorkspaceManager;
/** Get a cached workspace MindDB (opens on demand). Returns null if workspace not found. */
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
/** waggle.db, which holds awareness items. */
waggleDb: import('@waggle/core').WaggleDB;
}
// ── Helpers ────────────────────────────────────────────────────────────
@@ -57,16 +59,10 @@ function getLastSessionActivity(dataDir: string, workspaceId: string): Date | nu
return latest > 0 ? new Date(latest) : null;
}
/** Count pending awareness items in a workspace mind. */
/** Count open task and pending awareness items of a workspace. */
function countPendingAwareness(ctx: ProactiveContext, workspaceId: string): number {
const db = ctx.getWorkspaceMindDb(workspaceId);
if (!db) return 0;
try {
const raw = db.getDatabase();
const row = raw.prepare(
"SELECT COUNT(*) as cnt FROM awareness WHERE (category = 'task' OR category = 'pending') AND (expires_at IS NULL OR expires_at > datetime('now'))",
).get() as { cnt: number } | undefined;
return row?.cnt ?? 0;
return new AwarenessLayer(ctx.waggleDb, workspaceDataScope(workspaceId)).countOpen();
} catch {
return 0;
}