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

@@ -2,17 +2,20 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { MindDB } from '@waggle/core';
import { MindDB, WaggleDB } from '@waggle/core';
import { buildWorkspaceNowBlock, formatWorkspaceNowPrompt, type WorkspaceNowBlock } from '../../src/local/routes/workspace-context.js';
describe('workspace-context', () => {
let tmpDir: string;
let waggleDb: WaggleDB;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ctx-test-'));
waggleDb = WaggleDB.openInDataDir(tmpDir);
});
afterEach(() => {
waggleDb.close();
// On Windows, SQLite WAL files may keep handles open briefly; best-effort cleanup
try {
fs.rmSync(tmpDir, { recursive: true, force: true });
@@ -92,6 +95,7 @@ describe('workspace-context', () => {
const result = buildWorkspaceNowBlock({
dataDir: tmpDir,
waggleDb,
workspaceId: 'does-not-exist',
wsManager,
activateWorkspaceMind: noopActivate,
@@ -112,6 +116,7 @@ describe('workspace-context', () => {
const result = buildWorkspaceNowBlock({
dataDir: tmpDir,
waggleDb,
workspaceId: 'empty-ws',
wsManager,
activateWorkspaceMind: noopActivate,
@@ -134,6 +139,7 @@ describe('workspace-context', () => {
const result = buildWorkspaceNowBlock({
dataDir: tmpDir,
waggleDb,
workspaceId: 'zero-ws',
wsManager,
activateWorkspaceMind: noopActivate,
@@ -169,6 +175,7 @@ describe('workspace-context', () => {
const result = buildWorkspaceNowBlock({
dataDir: tmpDir,
waggleDb,
workspaceId: 'rich-ws',
wsManager,
activateWorkspaceMind,
@@ -217,6 +224,7 @@ describe('workspace-context', () => {
const result = buildWorkspaceNowBlock({
dataDir: tmpDir,
waggleDb,
workspaceId: 'capped-ws',
wsManager,
activateWorkspaceMind: noopActivate,

View File

@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { MindDB } from '@waggle/core';
import { AwarenessLayer, MindDB, WaggleDB, workspaceDataScope, type AwarenessCategory } from '@waggle/core';
import {
buildWorkspaceState,
formatWorkspaceStatePrompt,
@@ -12,12 +12,15 @@ import {
describe('workspace-state', () => {
let tmpDir: string;
let waggleDb: WaggleDB;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ws-state-'));
waggleDb = WaggleDB.openInDataDir(tmpDir);
});
afterEach(() => {
waggleDb.close();
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* */ }
});
@@ -28,7 +31,7 @@ describe('workspace-state', () => {
messages: Array<{ role: string; content: string }>;
ageMs?: number;
}>;
awareness?: Array<{ category: string; content: string; priority: number }>;
awareness?: Array<{ category: AwarenessCategory; content: string; priority: number }>;
}) {
const wsDir = path.join(tmpDir, 'workspaces', id);
fs.mkdirSync(wsDir, { recursive: true });
@@ -50,18 +53,17 @@ describe('workspace-state', () => {
).run(frame.content, frame.importance, createdAt);
}
if (opts?.awareness) {
for (const item of opts.awareness) {
raw.prepare(
`INSERT INTO awareness (category, content, priority, created_at)
VALUES (?, ?, ?, datetime('now'))`,
).run(item.category, item.content, item.priority);
}
}
db.close();
}
// Awareness items live in waggle.db under the workspace's scope, not in the mind.
if (opts?.awareness) {
const awareness = new AwarenessLayer(waggleDb, workspaceDataScope(id));
for (const item of opts.awareness) {
awareness.add(item.category, item.content, item.priority);
}
}
if (opts?.sessions) {
const sessDir = path.join(wsDir, 'sessions');
fs.mkdirSync(sessDir, { recursive: true });
@@ -126,6 +128,7 @@ describe('workspace-state', () => {
it('returns null for non-existent workspace', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'nonexistent',
wsManager: makeManager(new Map()),
activateWorkspaceMind: noopActivate,
@@ -139,6 +142,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'empty',
wsManager: makeManager(new Map([['empty', { id: 'empty', name: 'Empty' }]])),
activateWorkspaceMind: noopActivate,
@@ -153,6 +157,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'noframes',
wsManager: makeManager(new Map([['noframes', { id: 'noframes', name: 'No Frames' }]])),
activateWorkspaceMind: noopActivate,
@@ -171,6 +176,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'ws1',
wsManager: makeManager(new Map([['ws1', { id: 'ws1', name: 'Project Alpha' }]])),
activateWorkspaceMind,
@@ -198,6 +204,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'ws2',
wsManager: makeManager(new Map([['ws2', { id: 'ws2', name: 'API Project' }]])),
activateWorkspaceMind: noopActivate,
@@ -224,6 +231,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'ws3',
wsManager: makeManager(new Map([['ws3', { id: 'ws3', name: 'Infra' }]])),
activateWorkspaceMind: noopActivate,
@@ -258,6 +266,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'ws4',
wsManager: makeManager(new Map([['ws4', { id: 'ws4', name: 'Multi' }]])),
activateWorkspaceMind: noopActivate,
@@ -285,6 +294,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'ws5',
wsManager: makeManager(new Map([['ws5', { id: 'ws5', name: 'Blocked' }]])),
activateWorkspaceMind: noopActivate,
@@ -315,6 +325,7 @@ describe('workspace-state', () => {
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'ws6',
wsManager: makeManager(new Map([['ws6', { id: 'ws6', name: 'Full' }]])),
activateWorkspaceMind: noopActivate,
@@ -332,6 +343,29 @@ describe('workspace-state', () => {
expect(Array.isArray(result!.active)).toBe(true);
expect(Array.isArray(result!.nextActions)).toBe(true);
});
it("lists the workspace's awareness items from waggle.db as active, not other scopes", () => {
setupWorkspace('ws7', {
frames: [{ content: 'Project with open tasks', importance: 'normal' }],
awareness: [
{ category: 'task', content: 'Ship the installer', priority: 2 },
{ category: 'flag', content: 'Context flag only', priority: 1 },
],
});
new AwarenessLayer(waggleDb).add('task', 'Personal errand', 5);
new AwarenessLayer(waggleDb, workspaceDataScope('other')).add('task', 'Other workspace task', 5);
const result = buildWorkspaceState({
dataDir: tmpDir,
waggleDb,
workspaceId: 'ws7',
wsManager: makeManager(new Map([['ws7', { id: 'ws7', name: 'Tasks' }]])),
});
expect(result).not.toBeNull();
const fromAwareness = result!.active.filter(a => a.source === 'awareness');
expect(fromAwareness.map(a => a.content)).toEqual(['Ship the installer']);
});
});
// ── formatWorkspaceStatePrompt ────────────────────────────────