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:
@@ -10,7 +10,7 @@ import Fastify, { type FastifyInstance, type FastifyRequest } from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import fastifyStatic from '@fastify/static';
|
||||
import websocket from '@fastify/websocket';
|
||||
import { MindDB, MultiMind, MultiMindCache, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, FrameStore, SessionStore, SuppressionStore, InstallAuditStore, CronStore, AwarenessLayer, VaultStore, SkillHashStore, HarvestSourceStore, ClaudeCodeAdapter, reconcileIndexes, TeamSync, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore, harvestSetHash, type WorkspaceConfig } from '@waggle/core';
|
||||
import { MindDB, MultiMind, WaggleDB, importLegacyMindData, workspaceDataScope, type LegacyMindSource, MultiMindCache, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, FrameStore, SessionStore, SuppressionStore, InstallAuditStore, CronStore, AwarenessLayer, VaultStore, SkillHashStore, HarvestSourceStore, ClaudeCodeAdapter, reconcileIndexes, TeamSync, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore, harvestSetHash, type WorkspaceConfig } from '@waggle/core';
|
||||
import { corsOriginAllowed } from './cors-config.js';
|
||||
import { getBoundTeamServer } from './team-server-binding.js';
|
||||
import { fetchTeamServer } from './team-server-egress.js';
|
||||
@@ -382,6 +382,7 @@ declare module 'fastify' {
|
||||
agentState: AgentState;
|
||||
auditStore: import('@waggle/core').InstallAuditStore;
|
||||
cronStore: import('@waggle/core').CronStore;
|
||||
waggleDb: import('@waggle/core').WaggleDB;
|
||||
traceStore: import('@waggle/core').ExecutionTraceStore;
|
||||
/** Active behavioral spec — the compiled `BEHAVIORAL_SPEC` baseline. */
|
||||
activeBehavioralSpec: ReturnType<typeof import('@waggle/agent').buildActiveBehavioralSpec>;
|
||||
@@ -505,18 +506,39 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
const multiMind = new MultiMind(personalPath);
|
||||
server.decorate('multiMind', multiMind);
|
||||
|
||||
// Waggle's own data (automations, notifications, cost tracking, profile and
|
||||
// awareness) lives in waggle.db, apart from memory (ADR-0007). The first
|
||||
// start copies it from the legacy .mind files, which stay untouched.
|
||||
const waggleDb = WaggleDB.openInDataDir(fullConfig.dataDir);
|
||||
server.decorate('waggleDb', waggleDb);
|
||||
const legacySources: LegacyMindSource[] = [{ path: personalPath, scope: 'personal' }];
|
||||
for (const ws of wsManager.list()) {
|
||||
try {
|
||||
legacySources.push({ path: wsManager.getMindPath(ws.id), scope: workspaceDataScope(ws.id) });
|
||||
} catch {
|
||||
// A workspace whose directory fails the path checks has nothing to copy.
|
||||
}
|
||||
}
|
||||
for (const report of importLegacyMindData(waggleDb, legacySources)) {
|
||||
if (report.status === 'imported') {
|
||||
log.info(`[waggle-db] copied legacy data for ${report.scope}: ${JSON.stringify(report.copied)}`);
|
||||
} else if (report.status === 'failed') {
|
||||
log.warn(`[waggle-db] could not copy legacy data for ${report.scope}: ${report.error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Install audit store — persistent trail for capability install events
|
||||
const auditStore = new InstallAuditStore(multiMind.personal);
|
||||
server.decorate('auditStore', auditStore);
|
||||
|
||||
// Cron store — SQLite-backed schedule persistence for Solo
|
||||
const cronStore = new CronStore(multiMind.personal);
|
||||
const cronStore = new CronStore(waggleDb);
|
||||
server.decorate('cronStore', cronStore);
|
||||
|
||||
seedDefaultCrons(cronStore);
|
||||
|
||||
// Execution trace store — persistent log of agent runs (cost tracking, run history)
|
||||
const traceStore = new ExecutionTraceStore(multiMind.personal);
|
||||
const traceStore = new ExecutionTraceStore(waggleDb);
|
||||
server.decorate('traceStore', traceStore);
|
||||
|
||||
// Workflow-harness → trace bridge. Any harness phase that completes or
|
||||
@@ -691,6 +713,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
// Orchestrator — connects to personal .mind
|
||||
const orchestrator = new Orchestrator({
|
||||
db: multiMind.personal,
|
||||
waggleDb,
|
||||
embedder,
|
||||
mode: 'local',
|
||||
version: '0.4',
|
||||
@@ -902,7 +925,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
const skills = loadSkills(waggleHome);
|
||||
|
||||
// Skill hash store — detect skill changes on disk
|
||||
const skillHashStore = new SkillHashStore(multiMind.personal);
|
||||
const skillHashStore = new SkillHashStore(waggleDb);
|
||||
server.decorate('skillHashStore', skillHashStore);
|
||||
|
||||
// Check for changed skills at startup
|
||||
@@ -1180,6 +1203,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
const createSessionOrchestrator = (workspaceMind?: MindDB): Orchestrator => {
|
||||
const sessionOrch = new Orchestrator({
|
||||
db: multiMind.personal,
|
||||
waggleDb,
|
||||
embedder,
|
||||
mode: 'local',
|
||||
version: '0.4',
|
||||
@@ -1859,7 +1883,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
return age > 7 * 24 * 60 * 60 * 1000;
|
||||
}).length;
|
||||
if (staleCount > 0) {
|
||||
const wsAwareness = new AwarenessLayer(wsDb);
|
||||
const wsAwareness = new AwarenessLayer(waggleDb, workspaceDataScope(ws.id));
|
||||
wsAwareness.add('flag', `Workspace "${ws.name}" has ${staleCount} stale memory frames (>7 days untouched)`, 0);
|
||||
}
|
||||
}
|
||||
@@ -1872,6 +1896,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
dataDir: fullConfig.dataDir,
|
||||
workspaceManager: wsManager,
|
||||
getWorkspaceMindDb,
|
||||
waggleDb,
|
||||
};
|
||||
|
||||
/** Emit a ProactiveMessage through the delivery router pipeline. */
|
||||
@@ -2152,7 +2177,8 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
const body = await resp.json() as unknown;
|
||||
return parseOpenAiTextCompletion(body).content;
|
||||
};
|
||||
const loopResult = await runLoopTick({ schedule, mindDb, embedder, chat: loopChat, log });
|
||||
const loopAwareness = new AwarenessLayer(waggleDb, wsId && wsId !== '*' ? workspaceDataScope(wsId) : 'personal');
|
||||
const loopResult = await runLoopTick({ schedule, mindDb, awareness: loopAwareness, embedder, chat: loopChat, log });
|
||||
if (!loopResult.skipped) {
|
||||
emitNotification(server, {
|
||||
title: `Loop: ${schedule.name}`,
|
||||
@@ -2981,6 +3007,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
// Close all cached workspace minds
|
||||
mindCache.closeAll();
|
||||
multiMind.close();
|
||||
waggleDb.close();
|
||||
|
||||
// Close audit DB, teams DB, and telemetry
|
||||
closeAuditDb();
|
||||
|
||||
@@ -52,6 +52,8 @@ export interface LoopTickDeps {
|
||||
schedule: LoopSchedule;
|
||||
/** The already-resolved workspace (or personal) mind for this loop. */
|
||||
mindDb: MindDB;
|
||||
/** Awareness items of the same scope, in waggle.db; the loop keeps its state there. */
|
||||
awareness: AwarenessLayer;
|
||||
/** Embedder for recall-by-meaning. */
|
||||
embedder: Embedder;
|
||||
/** Toolless completion against the local proxy/LiteLLM. */
|
||||
@@ -201,7 +203,7 @@ export function buildMakerPrompt(opts: {
|
||||
* memory-write failures ARE caught (best-effort, must not fail the whole tick).
|
||||
*/
|
||||
export async function runLoopTick(deps: LoopTickDeps): Promise<LoopTickResult> {
|
||||
const { schedule, mindDb, embedder, chat, log } = deps;
|
||||
const { schedule, mindDb, awareness, embedder, chat, log } = deps;
|
||||
|
||||
const spec = parseLoopSpec(schedule.job_config);
|
||||
if (!spec) {
|
||||
@@ -210,7 +212,6 @@ export async function runLoopTick(deps: LoopTickDeps): Promise<LoopTickResult> {
|
||||
}
|
||||
|
||||
const stateKey = `loop:${schedule.id}`; // namespaced so loops don't collide on awareness.status
|
||||
const awareness = new AwarenessLayer(mindDb);
|
||||
const prior = awareness.getByStatus(stateKey)[0];
|
||||
const priorMeta = prior ? awareness.parseMetadata(prior) : undefined;
|
||||
let priorResult = priorMeta ? String(priorMeta.result ?? '') : '';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1034,6 +1034,7 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
||||
try {
|
||||
const nowBlock = buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
@@ -1741,6 +1742,7 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
||||
if (!effectiveWorkspace) return 'No workspace state available.';
|
||||
const block = buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId: effectiveWorkspace,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
|
||||
@@ -252,6 +252,7 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
||||
getWorkspaceState: async (): Promise<string> => {
|
||||
const block = buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId: effectiveWorkspaceId,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
@@ -289,6 +290,7 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
||||
try {
|
||||
const block = buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId: workspaceId ?? 'default',
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
|
||||
@@ -64,6 +64,7 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
||||
getWorkspaceState: async (): Promise<string> => {
|
||||
const block = buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId: effectiveWorkspaceId,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
|
||||
@@ -76,7 +76,7 @@ export async function feedbackRoutes(fastify: FastifyInstance) {
|
||||
}
|
||||
|
||||
try {
|
||||
const db = fastify.multiMind.personal.getDatabase();
|
||||
const db = fastify.waggleDb.getDatabase();
|
||||
ensureFeedbackTable(db);
|
||||
|
||||
db.prepare(`
|
||||
@@ -94,7 +94,7 @@ export async function feedbackRoutes(fastify: FastifyInstance) {
|
||||
// GET /api/feedback/stats — return feedback stats
|
||||
fastify.get('/api/feedback/stats', async (_request, reply) => {
|
||||
try {
|
||||
const db = fastify.multiMind.personal.getDatabase();
|
||||
const db = fastify.waggleDb.getDatabase();
|
||||
ensureFeedbackTable(db);
|
||||
|
||||
// Total feedback count
|
||||
|
||||
@@ -260,7 +260,7 @@ export const homeRoutes: FastifyPluginAsync = async (server) => {
|
||||
// ── Greeting + identity name (B8) ─────────────────────────────
|
||||
let userName: string | undefined;
|
||||
try {
|
||||
const layer = new IdentityLayer(server.multiMind.personal);
|
||||
const layer = new IdentityLayer(server.waggleDb);
|
||||
if (layer.exists()) {
|
||||
const name = layer.get().name?.trim();
|
||||
if (name) userName = name;
|
||||
@@ -302,6 +302,7 @@ export const homeRoutes: FastifyPluginAsync = async (server) => {
|
||||
try {
|
||||
const state = buildWorkspaceState({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId: ws.id,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
// UI sees a single consistent shape regardless of whether the user has
|
||||
// completed the onboarding identity questionnaire yet.
|
||||
|
||||
import type { FastifyPluginAsync } from 'fastify';
|
||||
import { IdentityLayer } from '@waggle/core';
|
||||
import type { FastifyInstance, FastifyPluginAsync } from 'fastify';
|
||||
import { IdentityLayer, workspaceDataScope, type DataScope } from '@waggle/core';
|
||||
|
||||
interface IdentityResponseShape {
|
||||
configured: boolean;
|
||||
@@ -25,6 +25,12 @@ interface IdentityResponseShape {
|
||||
_note?: string;
|
||||
}
|
||||
|
||||
/** Profile scope for a request: a known workspace, else the personal profile. */
|
||||
function identityScope(server: FastifyInstance, wsId: string | undefined): DataScope {
|
||||
if (wsId && wsId !== 'personal' && server.workspaceManager?.get(wsId)) return workspaceDataScope(wsId);
|
||||
return 'personal';
|
||||
}
|
||||
|
||||
function placeholder(note?: string): IdentityResponseShape {
|
||||
return {
|
||||
configured: false,
|
||||
@@ -63,20 +69,12 @@ export const identityRoutes: FastifyPluginAsync = async (server) => {
|
||||
'/api/identity',
|
||||
async (request, reply) => {
|
||||
const wsId = request.query.workspace ?? request.query.workspaceId;
|
||||
const personalMindDb = server.multiMind?.personal;
|
||||
|
||||
if (!personalMindDb) {
|
||||
return reply.send(placeholder('multi-mind not initialized'));
|
||||
if (!server.waggleDb) {
|
||||
return reply.send(placeholder('waggle.db not initialized'));
|
||||
}
|
||||
|
||||
// Use the workspace mind if requested + available; else personal.
|
||||
const targetMindDb =
|
||||
wsId && wsId !== 'personal'
|
||||
? server.agentState?.getWorkspaceMindDb?.(wsId) ?? personalMindDb
|
||||
: personalMindDb;
|
||||
|
||||
try {
|
||||
const layer = new IdentityLayer(targetMindDb);
|
||||
const layer = new IdentityLayer(server.waggleDb, identityScope(server, wsId));
|
||||
if (!layer.exists()) {
|
||||
return reply.send(placeholder('identity not configured for this mind'));
|
||||
}
|
||||
@@ -115,18 +113,11 @@ export const identityRoutes: FastifyPluginAsync = async (server) => {
|
||||
}>('/api/identity', async (request, reply) => {
|
||||
const body = request.body ?? {};
|
||||
const wsId = body.workspace ?? body.workspaceId;
|
||||
const personalMindDb = server.multiMind?.personal;
|
||||
|
||||
if (!personalMindDb) {
|
||||
return reply.status(503).send({ error: 'multi-mind not initialized' });
|
||||
if (!server.waggleDb) {
|
||||
return reply.status(503).send({ error: 'waggle.db not initialized' });
|
||||
}
|
||||
|
||||
const targetMindDb =
|
||||
wsId && wsId !== 'personal'
|
||||
? server.agentState?.getWorkspaceMindDb?.(wsId) ?? personalMindDb
|
||||
: personalMindDb;
|
||||
|
||||
const layer = new IdentityLayer(targetMindDb);
|
||||
const layer = new IdentityLayer(server.waggleDb, identityScope(server, wsId));
|
||||
try {
|
||||
// Merge-on-update: an omitted field keeps its stored value. The prior
|
||||
// `body.x ?? ''` semantics meant a partial write (e.g. the onboarding
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { FastifyPluginAsync } from 'fastify';
|
||||
import type { SearchScope, Importance, FrameSource, MemoryFrame } from '@waggle/core';
|
||||
import {
|
||||
AwarenessLayer,
|
||||
workspaceDataScope,
|
||||
evaluateExternalMemoryIngress,
|
||||
FrameStore,
|
||||
KnowledgeGraph,
|
||||
@@ -796,7 +797,8 @@ export const memoryRoutes: FastifyPluginAsync = async (server) => {
|
||||
let awarenessId: number | undefined;
|
||||
if (kind === 'task') {
|
||||
try {
|
||||
awarenessId = new AwarenessLayer(targetDb).add('task', content, 0, undefined, {
|
||||
const awarenessScope = mindLabel === 'workspace' && workspaceId ? workspaceDataScope(workspaceId) : 'personal';
|
||||
awarenessId = new AwarenessLayer(server.waggleDb, awarenessScope).add('task', content, 0, undefined, {
|
||||
context: 'quick-capture',
|
||||
status: 'open',
|
||||
}).id;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { MindDB } from '@waggle/core';
|
||||
import { MindDB, type WaggleDB } from '@waggle/core';
|
||||
import { assertSafeSegment } from './validate.js';
|
||||
import { extractProgressItems, type ProgressItem } from './sessions.js';
|
||||
import {
|
||||
@@ -225,12 +225,14 @@ export function buildWorkspaceNowBlock(opts: {
|
||||
dataDir: string;
|
||||
workspaceId: string;
|
||||
wsManager: WsManagerLike;
|
||||
/** waggle.db, which holds the workspace's awareness items. */
|
||||
waggleDb: WaggleDB;
|
||||
/** @deprecated Read-only context construction no longer mutates global workspace state. */
|
||||
activateWorkspaceMind?: (id: string) => boolean;
|
||||
/** Optional cron schedules for upcoming schedule display */
|
||||
cronSchedules?: CronScheduleLike[];
|
||||
}): WorkspaceNowBlock | null {
|
||||
const { dataDir, workspaceId, wsManager } = opts;
|
||||
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
|
||||
|
||||
// Path-traversal guard: workspaceId becomes a path segment below
|
||||
// (getMindPath + dataDir/workspaces/<workspaceId>/sessions). Reject any
|
||||
@@ -248,6 +250,7 @@ export function buildWorkspaceNowBlock(opts: {
|
||||
dataDir,
|
||||
workspaceId,
|
||||
wsManager,
|
||||
waggleDb,
|
||||
});
|
||||
|
||||
if (structuredState) {
|
||||
|
||||
@@ -772,6 +772,7 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
try {
|
||||
workspaceState = buildWorkspaceState({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId: id,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
@@ -897,6 +898,7 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
try {
|
||||
state = buildWorkspaceState({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
workspaceId: id,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -363,9 +363,11 @@ describe('Local Server Mode', () => {
|
||||
(SELECT COUNT(*) FROM memory_frames) AS frames,
|
||||
(SELECT COUNT(*) FROM memory_frames_fts) AS indexed,
|
||||
(SELECT COUNT(*) FROM knowledge_entities) AS entities,
|
||||
(SELECT COUNT(*) FROM knowledge_relations) AS relations,
|
||||
(SELECT COUNT(*) FROM awareness) AS awareness
|
||||
(SELECT COUNT(*) FROM knowledge_relations) AS relations
|
||||
`).get() as Record<string, number>,
|
||||
awareness: (server.waggleDb.getDatabase().prepare(
|
||||
'SELECT COUNT(*) AS count FROM awareness_items',
|
||||
).get() as { count: number }).count,
|
||||
auditEvents: (getAuditDb(tmpDir).prepare(
|
||||
'SELECT COUNT(*) AS count FROM audit_events',
|
||||
).get() as { count: number }).count,
|
||||
@@ -403,9 +405,11 @@ describe('Local Server Mode', () => {
|
||||
(SELECT COUNT(*) FROM memory_frames) AS frames,
|
||||
(SELECT COUNT(*) FROM memory_frames_fts) AS indexed,
|
||||
(SELECT COUNT(*) FROM knowledge_entities) AS entities,
|
||||
(SELECT COUNT(*) FROM knowledge_relations) AS relations,
|
||||
(SELECT COUNT(*) FROM awareness) AS awareness
|
||||
(SELECT COUNT(*) FROM knowledge_relations) AS relations
|
||||
`).get() as Record<string, number>,
|
||||
awareness: (server.waggleDb.getDatabase().prepare(
|
||||
'SELECT COUNT(*) AS count FROM awareness_items',
|
||||
).get() as { count: number }).count,
|
||||
auditEvents: (getAuditDb(tmpDir).prepare(
|
||||
'SELECT COUNT(*) AS count FROM audit_events',
|
||||
).get() as { count: number }).count,
|
||||
@@ -566,7 +570,9 @@ describe('Local Server Mode', () => {
|
||||
const counts = () => ({
|
||||
sessions: (db.prepare('SELECT COUNT(*) AS count FROM sessions').get() as { count: number }).count,
|
||||
frames: (db.prepare('SELECT COUNT(*) AS count FROM memory_frames').get() as { count: number }).count,
|
||||
awareness: (db.prepare('SELECT COUNT(*) AS count FROM awareness').get() as { count: number }).count,
|
||||
awareness: (server.waggleDb.getDatabase().prepare(
|
||||
'SELECT COUNT(*) AS count FROM awareness_items',
|
||||
).get() as { count: number }).count,
|
||||
auditEvents: (getAuditDb(tmpDir).prepare(
|
||||
'SELECT COUNT(*) AS count FROM audit_events',
|
||||
).get() as { count: number }).count,
|
||||
@@ -610,9 +616,7 @@ describe('Local Server Mode', () => {
|
||||
kind: 'task',
|
||||
});
|
||||
expect(new FrameStore(server.multiMind.personal).getById(Number(body.frameId))?.content).toBe(content);
|
||||
const awareness = server.multiMind.personal.getDatabase().prepare(
|
||||
'SELECT content FROM awareness WHERE id = ?',
|
||||
).get(body.awarenessId) as { content: string } | undefined;
|
||||
const awareness = new waggleCore.AwarenessLayer(server.waggleDb).get(body.awarenessId);
|
||||
expect(awareness?.content).toBe(content);
|
||||
});
|
||||
|
||||
@@ -1011,7 +1015,7 @@ describe('Local Server Mode', () => {
|
||||
model: 'anthropic/claude-sonnet-4-6',
|
||||
input: 'Charge after UTC midnight on a trace started before it',
|
||||
});
|
||||
firstServer.multiMind.personal.getDatabase().prepare(`
|
||||
firstServer.waggleDb.getDatabase().prepare(`
|
||||
UPDATE execution_traces SET created_at = datetime('now', '-1 day') WHERE id = ?
|
||||
`).run(midnightTraceId);
|
||||
firstServer.traceStore.recordCost(
|
||||
@@ -1031,7 +1035,7 @@ describe('Local Server Mode', () => {
|
||||
input: 'Recover a later settlement whose ledger write failed',
|
||||
});
|
||||
firstServer.traceStore.recordCost(recoveredTraceId, 0.018);
|
||||
firstServer.multiMind.personal.getDatabase().prepare(`
|
||||
firstServer.waggleDb.getDatabase().prepare(`
|
||||
CREATE TRIGGER fail_test_trace_spend
|
||||
BEFORE INSERT ON execution_trace_spend
|
||||
WHEN NEW.trace_id = ${recoveredTraceId}
|
||||
@@ -1039,7 +1043,7 @@ describe('Local Server Mode', () => {
|
||||
`).run();
|
||||
expect(() => firstServer.traceStore.recordCost(recoveredTraceId, 0.018))
|
||||
.toThrow('simulated ledger failure');
|
||||
firstServer.multiMind.personal.getDatabase().prepare('DROP TRIGGER fail_test_trace_spend').run();
|
||||
firstServer.waggleDb.getDatabase().prepare('DROP TRIGGER fail_test_trace_spend').run();
|
||||
firstServer.traceStore.finalize(recoveredTraceId, {
|
||||
outcome: 'abandoned',
|
||||
output: 'second settlement ledger write failed',
|
||||
|
||||
@@ -226,7 +226,7 @@ describe('cron ai_task executor (#17)', () => {
|
||||
jobConfig: { prompt: 'Inspect the workspace' },
|
||||
workspaceId: wsId,
|
||||
});
|
||||
server.multiMind.personal.getDatabase().prepare(
|
||||
server.waggleDb.getDatabase().prepare(
|
||||
"UPDATE cron_schedules SET next_run_at = datetime('now', '-1 minute') WHERE id = ?",
|
||||
).run(schedule.id);
|
||||
const workspaceMind = server.agentState.getWorkspaceMindDb(wsId)!;
|
||||
@@ -276,7 +276,7 @@ describe('cron ai_task executor (#17)', () => {
|
||||
jobConfig: { prompt: 'Inspect every workspace', mode: 'ai_task' },
|
||||
workspaceId: '*',
|
||||
});
|
||||
server.multiMind.personal.getDatabase().prepare(
|
||||
server.waggleDb.getDatabase().prepare(
|
||||
"UPDATE cron_schedules SET next_run_at = datetime('now', '-1 minute') WHERE id = ?",
|
||||
).run(schedule.id);
|
||||
server.workspaceManager.update(viewerWorkspaceId, {
|
||||
|
||||
@@ -11,25 +11,23 @@ import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import Fastify from 'fastify';
|
||||
import { MindDB } from '@waggle/core';
|
||||
import { WaggleDB } from '@waggle/core';
|
||||
import { feedbackRoutes } from '../../src/local/routes/feedback.js';
|
||||
|
||||
/** Minimal multiMind mock that provides a real MindDB for the personal mind. */
|
||||
function createTestServer(db: MindDB) {
|
||||
/** Minimal server with a real in-memory waggle.db, where feedback is stored. */
|
||||
function createTestServer(db: WaggleDB) {
|
||||
const server = Fastify({ logger: false });
|
||||
server.decorate('multiMind', {
|
||||
personal: db,
|
||||
});
|
||||
server.decorate('waggleDb', db);
|
||||
server.register(feedbackRoutes);
|
||||
return { server };
|
||||
}
|
||||
|
||||
describe('Feedback Routes', () => {
|
||||
let db: MindDB;
|
||||
let db: WaggleDB;
|
||||
let server: ReturnType<typeof Fastify>;
|
||||
|
||||
beforeEach(() => {
|
||||
db = new MindDB(':memory:');
|
||||
db = new WaggleDB(':memory:');
|
||||
const result = createTestServer(db);
|
||||
server = result.server;
|
||||
});
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
* "Needs review" view lists.
|
||||
*
|
||||
* Scaffolding follows memory-center.test.ts: Fastify inject + MindDB(':memory:')
|
||||
* + plain-object decorators. localConfig is intentionally absent → the
|
||||
* + plain-object decorators, plus an in-memory waggle.db for the profile card.
|
||||
* localConfig is intentionally absent → the
|
||||
* buildWorkspaceState call inside the briefing loop throws and is absorbed by
|
||||
* the route's per-workspace try/catch (pendingCount degrades to 0), and
|
||||
* emitAuditEvent stays a safe no-op.
|
||||
@@ -19,7 +20,7 @@
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import Fastify from 'fastify';
|
||||
import { MindDB, IdentityLayer } from '@waggle/core';
|
||||
import { MindDB, IdentityLayer, WaggleDB } from '@waggle/core';
|
||||
import {
|
||||
homeRoutes,
|
||||
personalizeGreeting,
|
||||
@@ -47,6 +48,9 @@ function createTestServer(
|
||||
cronSchedules: CronScheduleLike[] = [],
|
||||
) {
|
||||
const server = Fastify({ logger: false });
|
||||
const waggleDb = new WaggleDB(':memory:');
|
||||
server.decorate('waggleDb', waggleDb);
|
||||
server.addHook('onClose', async () => waggleDb.close());
|
||||
server.decorate('multiMind', {
|
||||
personal: db,
|
||||
getFrameStore: () => undefined,
|
||||
@@ -196,7 +200,7 @@ describe('GET /api/home/briefing (P2 — J08 needsReviewCount + greeting)', () =
|
||||
|
||||
it('greets by name when identity is seeded (B8)', async () => {
|
||||
boot([{ id: 'w1', name: 'Alpha', group: 'Personal', created: new Date().toISOString() }]);
|
||||
new IdentityLayer(db).create({
|
||||
new IdentityLayer(server.waggleDb).create({
|
||||
name: 'Marko', role: 'Founder', department: 'Egzakta',
|
||||
personality: '', capabilities: '', system_prompt: '',
|
||||
});
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
|
||||
import { describe, it, expect, afterEach } from 'vitest';
|
||||
import Fastify from 'fastify';
|
||||
import { MindDB } from '@waggle/core';
|
||||
import { WaggleDB } from '@waggle/core';
|
||||
import { identityRoutes } from '../../src/local/routes/identity.js';
|
||||
|
||||
describe('POST /api/identity — merge-on-update', () => {
|
||||
let db: MindDB;
|
||||
let db: WaggleDB;
|
||||
let server: ReturnType<typeof Fastify>;
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -21,10 +21,9 @@ describe('POST /api/identity — merge-on-update', () => {
|
||||
});
|
||||
|
||||
function boot() {
|
||||
db = new MindDB(':memory:');
|
||||
db = new WaggleDB(':memory:');
|
||||
server = Fastify({ logger: false });
|
||||
server.decorate('multiMind', { personal: db });
|
||||
server.decorate('agentState', { getWorkspaceMindDb: () => undefined });
|
||||
server.decorate('waggleDb', db);
|
||||
server.register(identityRoutes);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import * as os from 'node:os';
|
||||
import { MindDB, AwarenessLayer, type Embedder } from '@waggle/core';
|
||||
import { MindDB, WaggleDB, AwarenessLayer, type Embedder } from '@waggle/core';
|
||||
import {
|
||||
runLoopTick,
|
||||
parseLoopSpec,
|
||||
@@ -44,8 +44,8 @@ function framesIn(db: MindDB, gop: string): Array<{ content: string; source: str
|
||||
}
|
||||
|
||||
/** Seed a prior-tick awareness row so the cost floor has a lastTickAt to read. */
|
||||
function seedPriorTick(db: MindDB, scheduleId: number, lastTickAt: string) {
|
||||
new AwarenessLayer(db).add('pending', `Loop ${scheduleId}`, 0, undefined, {
|
||||
function seedPriorTick(awareness: AwarenessLayer, scheduleId: number, lastTickAt: string) {
|
||||
awareness.add('pending', `Loop ${scheduleId}`, 0, undefined, {
|
||||
status: `loop:${scheduleId}`, result: 'previous report', lastTickAt,
|
||||
});
|
||||
}
|
||||
@@ -53,14 +53,19 @@ function seedPriorTick(db: MindDB, scheduleId: number, lastTickAt: string) {
|
||||
describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
let tmpDir: string;
|
||||
let db: MindDB;
|
||||
let waggleDb: WaggleDB;
|
||||
let awareness: AwarenessLayer;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-loop-'));
|
||||
db = new MindDB(path.join(tmpDir, 'test.mind'));
|
||||
waggleDb = new WaggleDB(':memory:');
|
||||
awareness = new AwarenessLayer(waggleDb);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
db.close();
|
||||
waggleDb.close();
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
@@ -76,7 +81,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
|
||||
it('writes a report frame to the loop gop with source agent_inferred', async () => {
|
||||
const chat = makeChat();
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, embedder, chat, log: silentLog });
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
|
||||
|
||||
expect(res.skipped).toBeFalsy();
|
||||
expect(res.wrote).toBe(true);
|
||||
@@ -87,7 +92,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
});
|
||||
|
||||
it('records the judge score and surfaces it in the result', async () => {
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, embedder, chat: makeChat(), log: silentLog });
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat: makeChat(), log: silentLog });
|
||||
// weighted 0.8*0.5 + 0.7*0.3 + 0.9*0.2 = 0.79 (length penalty 1 for a short report)
|
||||
expect(res.score).toBeCloseTo(0.79, 2);
|
||||
});
|
||||
@@ -97,29 +102,29 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
// lastTickAt = now, which would otherwise throttle the second).
|
||||
const noFloor = JSON.stringify({ prompt: 'Observe the pipeline.', minIntervalMs: 0 });
|
||||
const chat1 = makeChat('First run: 3 deals at risk.');
|
||||
await runLoopTick({ schedule: schedule({ job_config: noFloor }), mindDb: db, embedder, chat: chat1, log: silentLog });
|
||||
await runLoopTick({ schedule: schedule({ job_config: noFloor }), mindDb: db, awareness, embedder, chat: chat1, log: silentLog });
|
||||
|
||||
// Prior state lives under the namespaced status key.
|
||||
const items = new AwarenessLayer(db).getByStatus('loop:1');
|
||||
// Prior state lives in waggle.db under the namespaced status key.
|
||||
const items = new AwarenessLayer(waggleDb).getByStatus('loop:1');
|
||||
expect(items).toHaveLength(1);
|
||||
expect(new AwarenessLayer(db).parseMetadata(items[0]).result).toContain('First run: 3 deals at risk');
|
||||
expect(new AwarenessLayer(waggleDb).parseMetadata(items[0]).result).toContain('First run: 3 deals at risk');
|
||||
|
||||
// Second tick: the maker prompt must carry the prior report.
|
||||
const chat2 = makeChat('Second run: 1 new at-risk deal.');
|
||||
await runLoopTick({ schedule: schedule({ job_config: noFloor }), mindDb: db, embedder, chat: chat2, log: silentLog });
|
||||
await runLoopTick({ schedule: schedule({ job_config: noFloor }), mindDb: db, awareness, embedder, chat: chat2, log: silentLog });
|
||||
const makerCall = chat2.mock.calls.find(c => !String(c[0]).includes('Return the JSON now'));
|
||||
expect(makerCall?.[0]).toContain('First run: 3 deals at risk');
|
||||
|
||||
// State merged in place (still a single awareness item), result advanced.
|
||||
const after = new AwarenessLayer(db).getByStatus('loop:1');
|
||||
const after = new AwarenessLayer(waggleDb).getByStatus('loop:1');
|
||||
expect(after).toHaveLength(1);
|
||||
expect(new AwarenessLayer(db).parseMetadata(after[0]).result).toContain('Second run');
|
||||
expect(new AwarenessLayer(waggleDb).parseMetadata(after[0]).result).toContain('Second run');
|
||||
});
|
||||
|
||||
it('still reports when the judge output is unparseable (score undefined, no throw)', async () => {
|
||||
// Judge returns junk → LLMJudge yields parsed:false, score stays undefined.
|
||||
const chat = vi.fn(async () => 'not json at all');
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, embedder, chat, log: silentLog });
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
|
||||
expect(res.skipped).toBeFalsy();
|
||||
expect(res.score).toBeUndefined();
|
||||
expect(framesIn(db, 'loop')).toHaveLength(1); // the report itself is "not json at all" — still written
|
||||
@@ -127,8 +132,8 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
|
||||
it('throttles a tick whose last real run (awareness lastTickAt) is within the cost floor', async () => {
|
||||
const chat = makeChat();
|
||||
seedPriorTick(db, 1, new Date(Date.now() - 60_000).toISOString()); // ran 1 min ago
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, embedder, chat, log: silentLog });
|
||||
seedPriorTick(awareness, 1, new Date(Date.now() - 60_000).toISOString()); // ran 1 min ago
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
|
||||
expect(res.skipped).toBe(true);
|
||||
expect(res.reason).toBe('within min interval');
|
||||
expect(chat).not.toHaveBeenCalled();
|
||||
@@ -137,8 +142,8 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
|
||||
it('runs when the last real run is older than the cost floor', async () => {
|
||||
const chat = makeChat();
|
||||
seedPriorTick(db, 1, new Date(Date.now() - (LOOP_MIN_INTERVAL_MS + 60_000)).toISOString());
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, embedder, chat, log: silentLog });
|
||||
seedPriorTick(awareness, 1, new Date(Date.now() - (LOOP_MIN_INTERVAL_MS + 60_000)).toISOString());
|
||||
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
|
||||
expect(res.skipped).toBeFalsy();
|
||||
expect(chat).toHaveBeenCalled();
|
||||
});
|
||||
@@ -150,7 +155,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
// when last_run_at looks "just now".
|
||||
const chat = makeChat();
|
||||
const justNowSqlite = new Date().toISOString().slice(0, 19).replace('T', ' '); // 'YYYY-MM-DD HH:MM:SS'
|
||||
const res = await runLoopTick({ schedule: schedule({ last_run_at: justNowSqlite }), mindDb: db, embedder, chat, log: silentLog });
|
||||
const res = await runLoopTick({ schedule: schedule({ last_run_at: justNowSqlite }), mindDb: db, awareness, embedder, chat, log: silentLog });
|
||||
expect(res.skipped).toBeFalsy();
|
||||
expect(chat).toHaveBeenCalled();
|
||||
});
|
||||
@@ -159,7 +164,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
const chat = makeChat();
|
||||
const res = await runLoopTick({
|
||||
schedule: schedule({ job_config: '{}' }),
|
||||
mindDb: db, embedder, chat, log: silentLog,
|
||||
mindDb: db, awareness, embedder, chat, log: silentLog,
|
||||
});
|
||||
expect(res.skipped).toBe(true);
|
||||
expect(res.reason).toBe('no prompt');
|
||||
@@ -170,7 +175,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
|
||||
const chat = makeChat();
|
||||
const res = await runLoopTick({
|
||||
schedule: schedule({ job_config: JSON.stringify({ prompt: 'observe', writeToMemory: false }) }),
|
||||
mindDb: db, embedder, chat, log: silentLog,
|
||||
mindDb: db, awareness, embedder, chat, log: silentLog,
|
||||
});
|
||||
expect(res.skipped).toBeFalsy();
|
||||
expect(res.wrote).toBeFalsy();
|
||||
@@ -237,11 +242,15 @@ describe('parseProposal', () => {
|
||||
describe('runLoopTick — assist mode (L2 proposals)', () => {
|
||||
let tmpDir: string;
|
||||
let db: MindDB;
|
||||
let waggleDb: WaggleDB;
|
||||
let awareness: AwarenessLayer;
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-loop-a-'));
|
||||
db = new MindDB(path.join(tmpDir, 'test.mind'));
|
||||
waggleDb = new WaggleDB(':memory:');
|
||||
awareness = new AwarenessLayer(waggleDb);
|
||||
});
|
||||
afterEach(() => { db.close(); fs.rmSync(tmpDir, { recursive: true, force: true }); });
|
||||
afterEach(() => { db.close(); waggleDb.close(); fs.rmSync(tmpDir, { recursive: true, force: true }); });
|
||||
|
||||
function assistChat(report: string, fence: string) {
|
||||
return vi.fn(async (prompt: string) => {
|
||||
@@ -255,7 +264,7 @@ describe('runLoopTick — assist mode (L2 proposals)', () => {
|
||||
|
||||
it('report mode never proposes an action', async () => {
|
||||
const chat = vi.fn(async () => 'A plain report, no fence.');
|
||||
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep' }), mindDb: db, embedder, chat, log: silentLog });
|
||||
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep' }), mindDb: db, awareness, embedder, chat, log: silentLog });
|
||||
expect(res.proposedAction).toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -263,7 +272,7 @@ describe('runLoopTick — assist mode (L2 proposals)', () => {
|
||||
const chat = assistChat('Three deals went quiet.', '{"tool":"send_email","args":{"to":"x@y.z"},"summary":"nudge them"}');
|
||||
const res = await runLoopTick({
|
||||
schedule: sched({ prompt: 'sweep', mode: 'assist' }),
|
||||
mindDb: db, embedder, chat, log: silentLog,
|
||||
mindDb: db, awareness, embedder, chat, log: silentLog,
|
||||
});
|
||||
expect(res.proposedAction).toEqual({ tool: 'send_email', args: { to: 'x@y.z' }, summary: 'nudge them' });
|
||||
expect(res.summary).toContain('Three deals went quiet');
|
||||
@@ -276,7 +285,7 @@ describe('runLoopTick — assist mode (L2 proposals)', () => {
|
||||
|
||||
it('assist mode with a {none:true} proposal yields no action', async () => {
|
||||
const chat = assistChat('Nothing actionable today.', '{"none":true}');
|
||||
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep', mode: 'assist' }), mindDb: db, embedder, chat, log: silentLog });
|
||||
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep', mode: 'assist' }), mindDb: db, awareness, embedder, chat, log: silentLog });
|
||||
expect(res.proposedAction).toBeUndefined();
|
||||
expect(res.summary).toContain('Nothing actionable');
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import * as os from 'node:os';
|
||||
import { MindDB, WorkspaceManager, AwarenessLayer } from '@waggle/core';
|
||||
import { MindDB, WaggleDB, WorkspaceManager, AwarenessLayer, workspaceDataScope } from '@waggle/core';
|
||||
import {
|
||||
generateMorningBriefing,
|
||||
checkStaleWorkspaces,
|
||||
@@ -14,12 +14,19 @@ import {
|
||||
describe('Proactive Handlers', () => {
|
||||
let tmpDir: string;
|
||||
let wsManager: WorkspaceManager;
|
||||
let waggleDb: WaggleDB;
|
||||
const mindDbs = new Map<string, MindDB>();
|
||||
|
||||
/** Awareness items of one workspace, kept in waggle.db under its scope. */
|
||||
function workspaceAwareness(workspaceId: string): AwarenessLayer {
|
||||
return new AwarenessLayer(waggleDb, workspaceDataScope(workspaceId));
|
||||
}
|
||||
|
||||
function makeContext(): ProactiveContext {
|
||||
return {
|
||||
dataDir: tmpDir,
|
||||
workspaceManager: wsManager,
|
||||
waggleDb,
|
||||
getWorkspaceMindDb: (workspaceId: string) => {
|
||||
const existing = mindDbs.get(workspaceId);
|
||||
if (existing) return existing;
|
||||
@@ -39,6 +46,7 @@ describe('Proactive Handlers', () => {
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-proactive-'));
|
||||
wsManager = new WorkspaceManager(tmpDir);
|
||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -46,6 +54,7 @@ describe('Proactive Handlers', () => {
|
||||
try { db.close(); } catch { /* already closed */ }
|
||||
}
|
||||
mindDbs.clear();
|
||||
waggleDb.close();
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
@@ -73,13 +82,9 @@ describe('Proactive Handlers', () => {
|
||||
it('includes workspace summaries when pending items exist', () => {
|
||||
const ws = wsManager.create({ name: 'My Research', group: 'Work' });
|
||||
|
||||
// Initialize the mind DB (write actual data so MindDB opens it properly)
|
||||
const mindPath = path.join(tmpDir, 'workspaces', ws.id, 'workspace.mind');
|
||||
const db = new MindDB(mindPath);
|
||||
const awareness = new AwarenessLayer(db);
|
||||
const awareness = workspaceAwareness(ws.id);
|
||||
awareness.add('task', 'Review paper draft', 1);
|
||||
awareness.add('pending', 'Waiting for feedback', 0);
|
||||
mindDbs.set(ws.id, db);
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = generateMorningBriefing(ctx);
|
||||
@@ -182,19 +187,13 @@ describe('Proactive Handlers', () => {
|
||||
const ws2 = wsManager.create({ name: 'Project B', group: 'Work' });
|
||||
|
||||
// Add awareness items to ws1
|
||||
const mindPath1 = path.join(tmpDir, 'workspaces', ws1.id, 'workspace.mind');
|
||||
const db1 = new MindDB(mindPath1);
|
||||
const awareness1 = new AwarenessLayer(db1);
|
||||
const awareness1 = workspaceAwareness(ws1.id);
|
||||
awareness1.add('task', 'Fix bug #123', 1);
|
||||
awareness1.add('task', 'Write tests', 0);
|
||||
mindDbs.set(ws1.id, db1);
|
||||
|
||||
// Add awareness items to ws2
|
||||
const mindPath2 = path.join(tmpDir, 'workspaces', ws2.id, 'workspace.mind');
|
||||
const db2 = new MindDB(mindPath2);
|
||||
const awareness2 = new AwarenessLayer(db2);
|
||||
const awareness2 = workspaceAwareness(ws2.id);
|
||||
awareness2.add('pending', 'Waiting for review', 0);
|
||||
mindDbs.set(ws2.id, db2);
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = checkPendingTasks(ctx);
|
||||
@@ -218,13 +217,10 @@ describe('Proactive Handlers', () => {
|
||||
|
||||
it('sets high priority when many pending items', () => {
|
||||
const ws = wsManager.create({ name: 'Busy Project', group: 'Work' });
|
||||
const mindPath = path.join(tmpDir, 'workspaces', ws.id, 'workspace.mind');
|
||||
const db = new MindDB(mindPath);
|
||||
const awareness = new AwarenessLayer(db);
|
||||
const awareness = workspaceAwareness(ws.id);
|
||||
for (let i = 0; i < 5; i++) {
|
||||
awareness.add('task', `Task ${i}`, 1);
|
||||
}
|
||||
mindDbs.set(ws.id, db);
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = checkPendingTasks(ctx);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 ────────────────────────────────
|
||||
|
||||
@@ -259,7 +259,7 @@ describe('chat smart-router integration', () => {
|
||||
.toISOString()
|
||||
.replace('T', ' ')
|
||||
.slice(0, 19);
|
||||
initialServer.multiMind.personal.getDatabase()
|
||||
initialServer.waggleDb.getDatabase()
|
||||
.prepare('UPDATE execution_traces SET created_at = ? WHERE id = ?')
|
||||
.run(previousDay, oldTraceId);
|
||||
await initialServer.close();
|
||||
|
||||
Reference in New Issue
Block a user