|
|
|
|
@@ -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();
|
|
|
|
|
|
|
|
|
|
|