refactor: drop the legacy memory engine from core, the server and the build (P1.5)

- @waggle/core no longer re-exports @waggle/hive-mind-core and no longer
  depends on it.
- The server no longer opens personal.mind or a cache of workspace minds;
  the legacy files are only read by the one-time copies of legacy data.
  releaseWorkspace closes the workspace's sessions and detaches it from the
  shared orchestrator.
- WorkspaceManager stops creating an empty workspace.mind.
- memory-center-legacy.ts had no callers left and is removed.
- build:packages no longer builds hive-mind-core and wiki-compiler; the
  sidecar aliases the memory packages to source instead.
- mem0ai stays external in the sidecar and launcher bundles: it imports a
  client for every provider it supports on demand, and bundling it failed on
  the optional peers Waggle does not install. The launcher declares mem0ai
  and no longer needs sqlite-vec.
This commit is contained in:
shumeiko.it
2026-10-24 13:34:09 +02:00
parent bde961abcf
commit e435e7da94
12 changed files with 252 additions and 190 deletions

View File

@@ -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, WaggleDB, importLegacyMindData, workspaceDataScope, type LegacyMindSource, MultiMindCache, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, FrameStore, SessionStore, SuppressionStore, InstallAuditStore, CronStore, AwarenessLayer, VaultStore, SkillHashStore, HarvestSourceStore, ClaudeCodeAdapter, reconcileIndexes, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore, harvestSetHash } from '@waggle/core';
import { WaggleDB, importLegacyMindData, workspaceDataScope, type LegacyMindSource, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, InstallAuditStore, CronStore, AwarenessLayer, VaultStore, SkillHashStore, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore } from '@waggle/core';
import { corsOriginAllowed } from './cors-config.js';
import { getStorageProvider } from './storage/index.js';
import { isLoopbackBind, resolveBindHost } from './net-config.js';
@@ -331,10 +331,6 @@ export interface AgentState {
* singleton and is unsafe for concurrent sessions. Being migrated per Phase A.1.
*/
activateWorkspaceMind: (workspaceId: string) => boolean;
/** Get a cached workspace MindDB (opens on demand). Returns null if workspace not found. */
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
/** Close and remove a workspace MindDB from cache. Used before workspace deletion to prevent EBUSY. */
closeWorkspaceMind: (workspaceId: string) => void;
/** Close a workspace's live sessions and detach it from the shared orchestrator. Used before workspace deletion. */
releaseWorkspace: (workspaceId: string) => void;
/** List all workspaces (id + name). Used by global memory search. */
@@ -362,7 +358,6 @@ export interface AgentState {
declare module 'fastify' {
interface FastifyInstance {
localConfig: LocalConfig;
multiMind: MultiMind;
workspaceManager: WorkspaceManager;
eventBus: EventEmitter;
agentRunner?: AgentRunner;
@@ -382,9 +377,6 @@ declare module 'fastify' {
skillHashStore: import('@waggle/core').SkillHashStore;
scheduler: import('./cron.js').LocalScheduler;
marketplace: import('@waggle/marketplace').MarketplaceDB | null;
/** Shared per-workspace mind cache. Routes use this to reach an arbitrary
* workspace's mind (e.g. FileIndexer for L-20 auto-indexing). */
mindCache: import('@waggle/core').MultiMindCache;
offlineManager: OfflineManager;
rateLimiter: import('./security-middleware.js').RateLimiter;
telemetry: import('@waggle/core').TelemetryStore;
@@ -492,10 +484,9 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
const defaultWorkspaceId = defaultWorkspaceConfig.id;
server.decorate('workspaceManager', wsManager);
// MultiMind — open personal mind, no workspace yet (selected via API)
// The legacy memory file of earlier versions; only the one-time copies of
// legacy data below read it, and nothing creates it any more.
const personalPath = path.join(fullConfig.dataDir, 'personal.mind');
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
@@ -1260,16 +1251,6 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
return [...base, ...crossTools];
};
// ── Workspace mind cache (LRU, max 20 open) ──
// allowedRoot defends against path-traversal via a crafted workspaceId (review
// Critical #2 from cowork/Code-Review_MultiMind_April-2026.md). Any resolved
// path outside this root is rejected before MindDB construction.
const mindCache = new MultiMindCache({
maxOpen: 20,
getMindPath: (id) => (wsManager.get(id) ? wsManager.getMindPath(id) : null),
allowedRoot: path.join(fullConfig.dataDir, 'workspaces'),
});
server.decorate('mindCache', mindCache);
let activeWorkspaceId: string | null = null;
const setActiveWorkspaceId = (workspaceId: string | null): void => {
activeWorkspaceId = workspaceId;
@@ -1287,30 +1268,15 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
return true;
};
// Helper: get a cached workspace MindDB (opens on demand)
// A6: Close and remove workspace mind DB from cache before deletion
const closeWorkspaceMind = (workspaceId: string): void => {
// Pin-aware: close any live session first so it drops its cache pin (and
// stops referencing a handle we're about to force-close). Then force-close
// the cache entry — on deletion the workspace is authoritative, so we close
// regardless of remaining pins to release the file lock before FS deletion
// (A6 EBUSY). Ordering avoids the old double-close where both the session
// and this seam closed the same MindDB.
// Before a workspace is deleted: close its live sessions and detach it from
// the shared orchestrator.
const releaseWorkspace = (workspaceId: string): void => {
sessionManager.close(workspaceId);
mindCache.close(workspaceId);
if (activeWorkspaceId === workspaceId) {
orchestrator.setWorkspace(null);
setActiveWorkspaceId(null);
}
};
// Until the legacy mind cache is removed, releasing a workspace also closes
// its cached mind so the workspace directory can be deleted (A6 EBUSY).
const releaseWorkspace = closeWorkspaceMind;
const getWorkspaceMindDb = (workspaceId: string): MindDB | null => {
if (!wsManager.get(workspaceId)) return null;
return mindCache.getOrOpen(workspaceId);
};
// Decorate with shared agent state
server.decorate('agentState', {
@@ -1331,8 +1297,6 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
buildToolsForSession,
bindWorkspaceCollaborationTools,
activateWorkspaceMind: activateWorkspaceMind,
getWorkspaceMindDb,
closeWorkspaceMind,
releaseWorkspace,
listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })),
activeWorkspaceId,
@@ -2523,9 +2487,6 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
// Close all workspace sessions (new concurrent model)
sessionManager.closeAll();
// Close all cached workspace minds
mindCache.closeAll();
multiMind.close();
await memory.close();
waggleDb.close();

View File

@@ -1,38 +0,0 @@
import type { MemoryFrame } from '@waggle/core';
import type { Memory, MemoryStatus } from '@waggle/shared';
import { asKind } from '../memory-records.js';
/**
* @deprecated Projects a legacy frame-store row into the Memory shape. Only
* `home.ts` and `artifacts.ts` still read frames; once they read memory through
* the contract (`recordToMemory` in memory-center.ts), delete this file.
*/
export function normalizeToMemory(frame: MemoryFrame, mind: string, workspaceId?: string): Memory {
let meta: Record<string, unknown> = {};
try {
const parsed: unknown = typeof frame.metadata === 'string' && frame.metadata ? JSON.parse(frame.metadata) : {};
if (parsed && typeof parsed === 'object') meta = parsed as Record<string, unknown>;
} catch { /* malformed metadata reads as empty */ }
const content = frame.content ?? '';
const importance: Memory['importance'] = frame.importance ?? 'normal';
const status: MemoryStatus = typeof meta.status === 'string'
? meta.status as MemoryStatus
: importance === 'deprecated'
? 'deprecated'
: importance === 'temporary' && frame.source === 'agent_inferred' ? 'unreviewed' : 'active';
const firstLine = content.split('\n').find((l) => l.trim().length > 0)?.trim() ?? content.trim();
return {
id: String(frame.id),
kind: asKind(meta.kind) ?? 'fact',
title: typeof meta.title === 'string' && meta.title.trim()
? meta.title
: firstLine.length > 80 ? `${firstLine.slice(0, 79)}…` : firstLine,
content,
scope: mind === 'workspace' ? 'workspace' : 'personal',
workspaceId,
source: frame.source ?? 'user_stated',
importance,
status,
createdAt: frame.created_at ?? new Date().toISOString(),
};
}

View File

@@ -25,8 +25,6 @@ import {
workspaceIdOf,
} from '../memory-records.js';
export { normalizeToMemory } from './memory-center-legacy.js';
/**
* Memory Center REST surface (S04, PRD §16.4) on the memory contract (ADR-0007).
*