import crypto from 'node:crypto'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); 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, OptimizationLogStore, ImprovementSignalStore, HarvestSourceStore, ClaudeCodeAdapter, reconcileIndexes, TeamSync, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore, EvolutionRunStore, ComplianceTemplateStore, 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'; import { getStorageProvider } from './storage/index.js'; import { isLoopbackBind, resolveBindHost } from './net-config.js'; import { isLocalRequest } from './origin-guard.js'; import { MemoryWeaver } from '@waggle/weaver'; import { Orchestrator, createSystemTools, createPlanTools, createGitTools, createDocumentTools, createSpreadsheetTools, createPresentationTools, createPdfTools, createSkillTools, createSubAgentTools, createWorkflowTools, runAgentLoop, parseOpenAiTextCompletion, ensureIdentity, loadSystemPrompt, loadSkills, HookRegistry, loadHooksFromConfig, CostTracker, CommandRegistry, registerWorkflowCommands, registerMarketplaceCommands, createCronTools, createSearchTools, createBrowserTools, createLspTools, createCliTools, createInsightsTools, createConnectorSearchTools, createCrossWorkspaceTools, McpRuntime, McpToolRetriever, isWithinBudget, getRecentLogs, setPersonaDataDir, deliverCronResult, createDefaultDeliveryPreferences, buildActiveBehavioralSpec, loadBehavioralSpecOverrides, TraceRecorder, HarnessTraceBridge, safeFetch, detectInstalledTools, PERSONAS, type ToolDefinition, type LoadedSkill, type DeliveryPreferences, } from '@waggle/agent'; import { PluginRuntimeManager, getStarterSkillsDir, validatePluginManifest } from '@waggle/sdk'; import { MarketplaceDB, MarketplaceSync, seedMcpServers, seedNewSources } from '@waggle/marketplace'; import type { RiskLevel } from '@waggle/shared'; import { runConnectorFetch } from './connector-harvest.js'; import { writeAutoSyncSummaryFrame } from './harvest-autosync-frame.js'; import { runLoopTick } from './loop-executor.js'; import { enqueueHeldAction } from './held-action-executor.js'; import { workspaceRoutes } from './routes/workspaces.js'; import { chatRoutes, type AgentRunner } from './routes/chat.js'; import { memoryRoutes } from './routes/memory.js'; import { memoryCenterRoutes } from './routes/memory-center.js'; import { artifactRoutes } from './routes/artifacts.js'; import { settingsRoutes } from './routes/settings.js'; import { embeddingRoutes, buildEmbeddingStatusPayload } from './routes/embedding.js'; import { sessionRoutes, findUndistilledSessions, markSessionDistilled } from './routes/sessions.js'; import { knowledgeRoutes } from './routes/knowledge.js'; import { litellmRoutes } from './routes/litellm.js'; import { runMemoryLaneExtraction } from './memory-lane-cron.js'; import { VectorEnrichmentService } from './services/vector-enrichment-service.js'; import { ingestRoutes, readFileRegistry } from './routes/ingest.js'; import { mindRoutes } from './routes/mind.js'; import { agentRoutes } from './routes/agent.js'; import { skillRoutes } from './routes/skills.js'; import { skillsAliasRoutes } from './routes/skills-aliases.js'; import { skillsHygieneRoutes } from './routes/skills-hygiene.js'; import { skillsAuditRoutes } from './routes/skills-audit.js'; import { approvalRoutes } from './routes/approval.js'; import { anthropicProxyRoutes } from './routes/anthropic-proxy.js'; import { teamRoutes } from './routes/team.js'; import { taskRoutes } from './routes/tasks.js'; import { capabilitiesRoutes } from './routes/capabilities.js'; import { toolsRoutes } from './routes/tools.js'; import { externalToolRunRoutes } from './routes/external-tool-runs.js'; import { routeProposalRoutes } from './routes/route-proposals.js'; import { waggleDanceRoutes } from './routes/waggle-dance.js'; import { commandRoutes } from './routes/commands.js'; import { commandRoutes as commandCenterRoutes } from './routes/command.js'; import { homeRoutes } from './routes/home.js'; import { onboardingRoutes } from './routes/onboarding.js'; import { cronRoutes } from './routes/cron.js'; import { notificationRoutes, emitNotification, emitSubagentStatus } from './routes/notifications.js'; import { materialFingerprint } from './notification-gate.js'; import { marketplaceDevRoutes } from './routes/marketplace-dev.js'; import { marketplaceRoutes } from './routes/marketplace.js'; import { CapabilityProposalStore, createCapabilityProposalRoutes, installMarketplaceApprovalIdentity, } from './routes/capability-proposals.js'; import { agentSearchRoutes } from './routes/agent-search.js'; import { connectorRoutes } from './routes/connectors.js'; import { mcpRoutes } from './routes/mcps.js'; import { extendRoutes } from './routes/extend.js'; import { populateMcpRuntimeFromConfig } from './mcp-config.js'; import { scheduleMarketplaceBackgroundSync } from './marketplace-background-sync.js'; import { fleetRoutes } from './routes/fleet.js'; import { importRoutes } from './routes/import.js'; import { vaultRoutes } from './routes/vault.js'; import { personaRoutes } from './routes/personas.js'; import { feedbackRoutes } from './routes/feedback.js'; import { workflowRoutes } from './routes/workflows.js'; import { workspaceTemplateRoutes } from './routes/workspace-templates.js'; import { evolutionRoutes } from './routes/evolution.js'; import { exportRoutes } from './routes/export.js'; import { dataEraseRoutes } from './routes/data-erase.js'; import { costRoutes } from './routes/cost.js'; import { backupRoutes } from './routes/backup.js'; import { offlineRoutes } from './routes/offline.js'; import { weaverRoutes } from './routes/weaver.js'; import { eventRoutes, closeAuditDb, cleanupAuditEvents, emitAuditEvent } from './routes/events.js'; import { closeTeamsDb } from './routes/team.js'; import { pinRoutes } from './routes/pins.js'; import { documentRoutes } from './routes/documents.js'; import { fileRoutes } from './routes/files.js'; import { browseRoutes } from './routes/browse.js'; import { browserExtRoutes } from './routes/browser-ext.js'; import { BROWSER_COMPANION_CREDENTIAL_VAULT_KEY, browserCompanionCredentialMatches, isBrowserCompanionCredentialHash, } from './browser-companion-pairing.js'; import { telegramRoutes, pushTelegramMessage } from './routes/telegram.js'; import { ChannelManager } from './channels/manager.js'; import { runChannelChatTurn } from './channels/chat-client.js'; import { channelRoutes } from './channels/routes.js'; import { isChannelPlatform } from './channels/types.js'; import { WorkspaceTurnCoordinator } from './workspace-turn-coordinator.js'; import { IdleSessionWatcher, readRecentTranscript, buildReviewInstruction, NOTHING_TO_DO } from './idle-watcher.js'; import { DreamJournal } from './dream-journal.js'; import { dreamRoutes } from './routes/dreams.js'; import { oauthRoutes } from './routes/oauth.js'; import { waggleSignalRoutes } from './routes/waggle-signals.js'; import { providerRoutes } from './routes/providers.js'; import { profileRoutes } from './routes/profile.js'; import { telemetryRoutes } from './routes/telemetry.js'; import { agentGroupRoutes } from './routes/agent-groups.js'; import { localJobRoutes } from './routes/jobs.js'; import { agentEntityRoutes } from './routes/agents.js'; import { automationRoutes } from './routes/automations.js'; import { harvestRoutes } from './routes/harvest.js'; import { wikiRoutes } from './routes/wiki.js'; import { identityRoutes } from './routes/identity.js'; import { agentRunRoutes } from './routes/agent-run.js'; import { agentRunsRoutes } from './routes/agent-runs.js'; import { localInferenceRoutes } from './routes/local-inference.js'; import { complianceRoutes } from './routes/compliance.js'; import { OfflineManager } from './offline-manager.js'; import { listOllamaChatModelIds } from './model-availability.js'; import { log, createLogger } from './logger.js'; import { installErrorHandler } from './error-handler.js'; import { seedDefaultCrons } from './setup-crons.js'; import { registerConnectors } from './setup-connectors.js'; import { cronScheduleHasReadOnlyViewerTarget, securityMiddleware, } from './security-middleware.js'; import { LocalScheduler, makeRecordExecutionCallback } from './cron.js'; import { EvolutionService, isEvolutionAutoEnabled } from './services/evolution-service.js'; import { generateMorningBriefing, checkStaleWorkspaces, checkPendingTasks, suggestCapabilities, type ProactiveContext, type ProactiveMessage, } from './proactive-handlers.js'; import { generateMonthlyAssessment, saveAssessmentToMind } from './monthly-assessment.js'; import { MAX_WORKSPACE_SESSIONS, WorkspaceSessionManager } from './workspace-sessions.js'; import { EventEmitter } from 'node:events'; import { LocalJobStore } from './job-store.js'; import { AgentRunRegistry } from './agent-run-registry.js'; import { ExecutorRegistry } from './executor-registry.js'; import { hydrateProviderEnvFromVault } from './provider-env.js'; export interface LocalConfig { port: number; host: string; dataDir: string; // ~/.waggle litellmUrl: string; // http://localhost:4000 /** Immutable desktop-launch identity used to prove sidecar ownership. */ instanceId?: string; /** Governed CLI execution — allowlist of program names the agent may run. */ cli?: { allowlist?: string[] }; /** True only when startService owns the LiteLLM child lifecycle. */ manageLiteLLM?: boolean; /** Stable child-process port, retained even while requests use a fallback proxy. */ managedLiteLLMPort?: number; /** Route all agent-loop traffic through this server's built-in provider proxy. */ useBuiltInProxy?: boolean; /** False when a managed caller must resolve provider state before health probes start. */ startOfflineManagerOnListen?: boolean; } /** Pending approval request — resolved when user approves or denies. */ export interface PendingApproval { resolve: (approved: boolean) => void; toolName: string; input: Record; timestamp: number; riskLevel?: RiskLevel; } /** * Shared agent state — initialized once at server startup, * accessible by all route modules for feature parity with CLI. */ /** LLM provider health: healthy (verified), degraded (configured, not verified), unavailable */ export type LlmHealthStatus = 'healthy' | 'degraded' | 'unavailable'; /** Which LLM provider is active and its runtime health */ export interface LlmProviderStatus { /** Which provider is handling LLM requests */ provider: 'litellm' | 'anthropic-proxy' | 'ollama'; /** Runtime health — truthful, not optimistic */ health: LlmHealthStatus; /** Human-readable detail (e.g. "LiteLLM on port 4000" or "No API key configured") */ detail: string; /** When this status was last checked */ checkedAt: string; } export interface WorkspaceCollaborationBinding { /** Tools visible to the owning turn after persona, availability, and intent filtering. */ visibleTools: ToolDefinition[]; /** Broader policy-filtered pool from which child workers receive their tools. */ workerTools: ToolDefinition[]; runLoop: AgentRunner; signal: AbortSignal; runChildTransaction: ( tools: readonly ToolDefinition[], operation: () => Promise, ) => Promise; defaultModel: string; } const WORKSPACE_COLLABORATION_TOOL_NAMES = new Set([ 'spawn_agent', 'list_agents', 'get_agent_result', 'compose_workflow', 'orchestrate_workflow', 'list_harnesses', 'run_harness', ]); export type WorkspaceCollaborationToolFactory = ( workerTools: ToolDefinition[], runLoop: AgentRunner, defaultModel: string, signal?: AbortSignal, ) => ToolDefinition[]; export function bindWorkspaceChildTools( options: WorkspaceCollaborationBinding, createTools: WorkspaceCollaborationToolFactory, ): ToolDefinition[] { const enabledNames = new Set(options.visibleTools.map((tool) => tool.name)); const workerTools = options.workerTools.filter( (tool) => !WORKSPACE_COLLABORATION_TOOL_NAMES.has(tool.name), ); const childRunLoop: AgentRunner = (config) => options.runChildTransaction( config.tools, () => options.runLoop({ ...config, signal: options.signal }), ); const replacements = createTools( workerTools, childRunLoop, options.defaultModel, options.signal, ).filter((tool) => enabledNames.has(tool.name)); return [ ...options.visibleTools.filter( (tool) => !WORKSPACE_COLLABORATION_TOOL_NAMES.has(tool.name), ), ...replacements, ]; } export interface AgentState { orchestrator: Orchestrator; allTools: ToolDefinition[]; hookRegistry: HookRegistry; costTracker: CostTracker; skills: LoadedSkill[]; userSystemPrompt: string | null; sessionHistories: Map; currentModel: string; litellmApiKey: string; pendingApprovals: Map; /** Serializes checkout-reading/writing agent turns by canonical physical root. */ workspaceTurnCoordinator: WorkspaceTurnCoordinator; /** Phase B.3: persistent "always allow" grant store for gated tools. */ approvalGrantStore: import('./approval-grants.js').ApprovalGrantStore; /** * Rebuild workspace-scoped tools (system, git, document) for a given directory. * Optional `orchForMindTools` replaces the shared-orchestrator mind tools with a * session-specific orchestrator's tools so concurrent sessions don't collide. */ buildToolsForWorkspace: (workspacePath: string, orchForMindTools?: Orchestrator, workspaceId?: string) => ToolDefinition[]; /** * Create a fresh Orchestrator for a workspace session with the given mind mounted. * Each session gets its own instance (Option Y fix from docs/plans/phase-a-the-room.md §9). */ createSessionOrchestrator: { (): Orchestrator; (workspaceMind: import('@waggle/core').MindDB): Orchestrator; }; /** * Build the full tool pool for a session, using the session's orchestrator for mind tools. * When `sourceWorkspaceId` is provided, also injects Phase B.2 cross-workspace read tools * (read_other_workspace, list_workspaces, list_workspace_files) scoped to that source. */ buildToolsForSession: (sessionOrch: Orchestrator, workspacePath: string, sourceWorkspaceId?: string) => ToolDefinition[]; /** * Recreate collaboration producers after primitive workspace tools are lease-wrapped. * The fresh producers run each child loop as one scope-local transaction. */ bindWorkspaceCollaborationTools: (options: WorkspaceCollaborationBinding) => ToolDefinition[]; /** * Activate workspace mind for the given workspace ID. Returns true if switched. * @deprecated — use `sessionManager.getOrCreate()` with `createSessionOrchestrator` * and `buildToolsForSession` instead. This legacy path uses a shared orchestrator * 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; /** List all workspaces (id + name). Used by global memory search. */ listWorkspaces: () => Array<{ id: string; name: string }>; /** Currently active workspace ID (null = personal only) */ activeWorkspaceId: string | null; /** Current sub-agent orchestrator instance (set during workflow execution) */ subagentOrchestrator: import('@waggle/agent').SubagentOrchestrator | null; /** Plugin runtime manager — lifecycle, tools, skills from plugins */ pluginRuntimeManager: import('@waggle/sdk').PluginRuntimeManager; /** MCP server runtime — stdio servers, health, tools */ mcpRuntime: import('@waggle/agent').McpRuntime; /** Steal #6: relevance-gates MCP tools into the model pool per conversation */ mcpToolRetriever: import('@waggle/agent').McpToolRetriever; /** Command registry — slash commands */ commandRegistry: import('@waggle/agent').CommandRegistry; /** LLM provider status — which provider is active and whether it's truly healthy */ llmProvider: LlmProviderStatus; /** Session token for WebSocket authentication (generated on server startup) */ wsSessionToken: string; /** SHA-256 hash of the paired Browser Companion credential, or null when unpaired. */ browserCompanionCredentialHash: string | null; /** Memory-weaver run timestamps for the personal mind. */ weaverState: { lastPersonalConsolidation: string | null; lastPersonalDecay: string | null }; /** Per-workspace memory-weaver run timestamps, keyed by workspace ID. */ workspaceWeaverStatus: Record; } declare module 'fastify' { interface FastifyInstance { localConfig: LocalConfig; multiMind: MultiMind; workspaceManager: WorkspaceManager; eventBus: EventEmitter; agentRunner?: AgentRunner; agentState: AgentState; auditStore: import('@waggle/core').InstallAuditStore; cronStore: import('@waggle/core').CronStore; traceStore: import('@waggle/core').ExecutionTraceStore; evolutionStore: import('@waggle/core').EvolutionRunStore; /** * Active behavioral spec — baseline `BEHAVIORAL_SPEC` with any * accepted-and-deployed evolution overrides applied. Re-derived on * the 'behavioral-spec:reloaded' event. */ activeBehavioralSpec: ReturnType; vault: import('@waggle/core').VaultStore; embeddingProvider: import('@waggle/core').EmbeddingProviderInstance; skillHashStore: import('@waggle/core').SkillHashStore; scheduler: import('./cron.js').LocalScheduler; /** Idle-session self-evolution watcher. Inert unless self-evolution.json enabled. */ idleWatcher: import('./idle-watcher.js').IdleSessionWatcher; /** H-10 G1: autonomous evolution daemon. Inert unless WAGGLE_EVOLUTION_AUTO_ENABLED=1. */ evolutionService: import('./services/evolution-service.js').EvolutionService; 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; connectorRegistry: import('@waggle/agent').ConnectorRegistry; sessionManager: import('./workspace-sessions.js').WorkspaceSessionManager; /** * Bridges workflow-harness phase events into the execution trace store * so harnessed runs become training signal for the self-evolution loop. * Started at boot, stopped on close. */ harnessTraceBridge: HarnessTraceBridge; /** Clears the cached LLM key-validation result; called by the settings route after a key update. */ _invalidateKeyValidationCache?: () => void; } } const LOOPBACK_BOOTSTRAP_HOSTS = new Set(['127.0.0.1', 'localhost', '::1']); function loopbackAuthorityMatchesRequest( rawOrigin: string, rawHost: string | undefined, ): boolean { if (!rawHost) return false; try { const origin = new URL(rawOrigin); const requestUrl = new URL(`http://${rawHost}`); const originHost = origin.hostname.replace(/^\[|\]$/g, ''); const requestHost = requestUrl.hostname.replace(/^\[|\]$/g, ''); return origin.protocol === 'http:' && origin.username === '' && origin.password === '' && LOOPBACK_BOOTSTRAP_HOSTS.has(originHost) && LOOPBACK_BOOTSTRAP_HOSTS.has(requestHost) && origin.host === requestUrl.host; } catch { return false; } } function browserBootstrapAuthorityAllowed(request: FastifyRequest): boolean { const secFetchSite = request.headers['sec-fetch-site']; if (secFetchSite !== undefined && secFetchSite !== 'same-origin') return false; const rawOrigin = request.headers.origin ?? request.headers.referer; if (!rawOrigin) return secFetchSite === 'same-origin'; return loopbackAuthorityMatchesRequest(rawOrigin, request.headers.host); } function desktopBootstrapMatches( rawHeader: string | string[] | undefined, expected: string | null, ): boolean { if (!expected || typeof rawHeader !== 'string') return false; const actualBytes = Buffer.from(rawHeader); const expectedBytes = Buffer.from(expected); return actualBytes.length === expectedBytes.length && crypto.timingSafeEqual(actualBytes, expectedBytes); } export async function buildLocalServer(config: Partial = {}) { const fullConfig: LocalConfig = { port: parseInt(process.env.WAGGLE_PORT ?? '3333'), host: resolveBindHost(), dataDir: config.dataDir ?? process.env.WAGGLE_DATA_DIR ?? '', litellmUrl: config.litellmUrl ?? 'http://localhost:4000', ...config, instanceId: config.instanceId ?? process.env.WAGGLE_INSTANCE_ID, }; const managedDesktopBootstrap = typeof fullConfig.instanceId === 'string' && fullConfig.instanceId.trim().length > 0; const rawDesktopBootstrapToken = process.env.WAGGLE_DESKTOP_BOOTSTRAP_TOKEN?.trim(); // The credential is needed only to bind this server instance to its Tauri // launcher. Remove it before any tool discovery or child process can inherit it. delete process.env.WAGGLE_DESKTOP_BOOTSTRAP_TOKEN; const desktopBootstrapToken = rawDesktopBootstrapToken && rawDesktopBootstrapToken.length >= 32 && rawDesktopBootstrapToken.length <= 200 ? rawDesktopBootstrapToken : null; const server = Fastify({ logger: false }); const localJobStore = new LocalJobStore(); server.decorate('localJobStore', localJobStore); const agentRunRegistry = new AgentRunRegistry(path.join(fullConfig.dataDir, 'agent-runs.json')); server.decorate('agentRunRegistry', agentRunRegistry); // ── Global error handler ── // Fastify's own logger is disabled here, so without this an unhandled route // exception logs nowhere and leaks raw err.message in the 500 body. One handler // for the whole instance: 4xx pass through; 5xx log full context via `log` and // return a generic envelope (message only outside production). See error-handler.ts. installErrorHandler(server, log); // Decorate with local config server.decorate('localConfig', fullConfig); // Set data dir for custom personas (loaded from ~/.waggle/personas/) setPersonaDataDir(fullConfig.dataDir); // Event bus (replaces Redis pub/sub) const eventBus = new EventEmitter(); server.decorate('eventBus', eventBus); // Workspace manager const wsManager = new WorkspaceManager(fullConfig.dataDir); const defaultWorkspaceConfig = wsManager.ensureDefault(); const defaultWorkspaceId = defaultWorkspaceConfig.id; server.decorate('workspaceManager', wsManager); // MultiMind — open personal mind, no workspace yet (selected via API) const personalPath = path.join(fullConfig.dataDir, 'personal.mind'); const multiMind = new MultiMind(personalPath); server.decorate('multiMind', multiMind); // Install audit store — persistent trail for capability install events const auditStore = new InstallAuditStore(multiMind.personal); server.decorate('auditStore', auditStore); // M-06: seed the built-in KVARK enterprise template on first boot. // Idempotent — subsequent boots see the row and no-op. try { const templateStore = new ComplianceTemplateStore(multiMind.personal); templateStore.seedKvarkTemplateIfMissing(); } catch (err) { // Non-fatal: users can create the template manually from the UI if the // seed fails (disk full, schema drift, etc.). server.log.warn({ err }, 'Failed to seed KVARK compliance template'); } // Cron store — SQLite-backed schedule persistence for Solo const cronStore = new CronStore(multiMind.personal); server.decorate('cronStore', cronStore); seedDefaultCrons(cronStore); // Execution trace store — persistent log of agent runs (self-evolution substrate) const traceStore = new ExecutionTraceStore(multiMind.personal); server.decorate('traceStore', traceStore); // Workflow-harness → trace bridge. Any harness phase that completes or // aborts anywhere in the app emits on the shared `harnessEvents` emitter; // this bridge translates those into rows in `execution_traces` so harness // runs become training data for the self-evolution loop. const harnessTraceBridge = new HarnessTraceBridge({ recorder: new TraceRecorder(traceStore), }); harnessTraceBridge.start(); server.decorate('harnessTraceBridge', harnessTraceBridge); server.addHook('onClose', async () => { harnessTraceBridge.stop(); }); // Evolution run store — audit trail of proposed/accepted/rejected self-evolution runs const evolutionStore = new EvolutionRunStore(multiMind.personal); server.decorate('evolutionStore', evolutionStore); // Behavioral spec with deployed overrides applied (self-evolution deploy target). // Re-derived on the 'behavioral-spec:reloaded' event emitted by evolution routes. const initialOverrides = loadBehavioralSpecOverrides(fullConfig.dataDir); let activeBehavioralSpec = buildActiveBehavioralSpec(initialOverrides); server.decorate('activeBehavioralSpec', activeBehavioralSpec); eventBus.on('behavioral-spec:reloaded', () => { const latest = loadBehavioralSpecOverrides(fullConfig.dataDir); activeBehavioralSpec = buildActiveBehavioralSpec(latest); // Re-decorate so server.activeBehavioralSpec returns the fresh value. // Fastify's decorate() is idempotent for reassignment via the instance. (server as unknown as { activeBehavioralSpec: typeof activeBehavioralSpec }).activeBehavioralSpec = activeBehavioralSpec; }); // Vault — encrypted secret storage const vault = new VaultStore(fullConfig.dataDir); server.decorate('vault', vault); // Vault is canonical; env is the compatibility layer for provider SDKs. let hydrated = hydrateProviderEnvFromVault(vault); // Pass-through secrets whose vault name already matches the env var name for (const passthrough of ['TAVILY_API_KEY', 'BRAVE_API_KEY']) { const entry = vault.get(passthrough); if (entry?.value && !process.env[passthrough]) { process.env[passthrough] = entry.value; hydrated++; } } if (hydrated > 0) { log.info(`Hydrated ${hydrated} env var(s) from vault for provider SDKs`); } // M2-7: Telemetry — local, opt-in, privacy-first const waggleConfigForTelemetry = new WaggleConfig(fullConfig.dataDir); const telemetry = new TelemetryStore(fullConfig.dataDir, waggleConfigForTelemetry.getTelemetryEnabled()); server.decorate('telemetry', telemetry); telemetry.track(TELEMETRY_EVENTS.APP_START, { version: '0.2.0', platform: process.platform }); // embeddingProvider decorated later after creation (needs vault for API keys) // Migrate plaintext keys from config.json to vault on first run try { const waggleConfig = new WaggleConfig(fullConfig.dataDir); const configProviders = waggleConfig.getProviders(); if (Object.keys(configProviders).length > 0) { const migrated = vault.migrateFromConfig({ providers: configProviders }); let scrubbed = 0; for (const [name, provider] of Object.entries(configProviders)) { if (!provider.apiKey) continue; waggleConfig.setProvider(name, { ...provider, apiKey: '' }); scrubbed++; } if (scrubbed > 0) waggleConfig.save(); if (migrated > 0) { log.info(` Migrated ${migrated} API key(s) to encrypted vault`); } if (scrubbed > 0) { log.info(` Scrubbed ${scrubbed} plaintext provider key(s) from config`); } } } catch { // Migration failure should never block startup } // Connector Registry — manages registered connectors and generates dynamic tools const connectorRegistry = registerConnectors(vault); server.decorate('connectorRegistry', connectorRegistry); // Seed marketplace.db if not present, then open it for production routes let marketplaceDb: MarketplaceDB | null = null; try { const marketplaceDbTarget = path.join(fullConfig.dataDir, 'marketplace.db'); if (!fs.existsSync(marketplaceDbTarget)) { // Packaged service.js and marketplace.db are sibling Tauri resources. // Development and built-package candidates follow for non-Tauri runs. // __dirname resolves to packages/server/src/local during tsx dev, and // to packages/server/dist/local after a build — both need ../../.. // to reach the packages/ root, plus a fallback for the production bundle. const seedPaths = [ path.resolve(__dirname, 'marketplace.db'), // packaged Tauri resource path.resolve(__dirname, '../../../marketplace/marketplace.db'), // dev: tsx from src/local path.resolve(__dirname, '../../../../marketplace/marketplace.db'), // built: dist/local path.resolve(__dirname, '../../../../packages/marketplace/marketplace.db'), path.resolve(__dirname, '../../../../../packages/marketplace/marketplace.db'), ]; for (const seedPath of seedPaths) { if (fs.existsSync(seedPath)) { fs.copyFileSync(seedPath, marketplaceDbTarget); log.info(` Seeded marketplace.db from ${seedPath}`); break; } } } if (fs.existsSync(marketplaceDbTarget)) { marketplaceDb = new MarketplaceDB(marketplaceDbTarget); // Seed MCP server registry entries if not already present try { const mcpAdded = seedMcpServers(marketplaceDb); if (mcpAdded > 0) { log.info(` Seeded ${mcpAdded} MCP servers into marketplace`); } } catch (err) { log.warn(` MCP registry seed failed: ${(err as Error).message}`); } // Seed new marketplace sources (skills.sh, mcpmarket, npm, awesome-mcp-servers, etc.) try { const sourcesAdded = seedNewSources(marketplaceDb); if (sourcesAdded > 0) { log.info(` Seeded ${sourcesAdded} new marketplace sources`); } } catch (err) { log.warn(` Source seed failed: ${(err as Error).message}`); } log.info(' Marketplace DB loaded'); } } catch (err) { log.warn(` Marketplace DB failed to load: ${(err as Error).message}`); // Marketplace is optional — never block startup } server.decorate('marketplace', marketplaceDb); const capabilityProposalStore = new CapabilityProposalStore(); server.decorate('capabilityProposalStore', capabilityProposalStore); // ── Daily marketplace sync (non-blocking, 60s delay after startup) ── const stopMarketplaceBackgroundSync = scheduleMarketplaceBackgroundSync({ marketplaceDb, log }); // ── Agent state (matches CLI initialization) ──────────────────────── const wsSessionToken = crypto.randomBytes(32).toString('hex'); const storedBrowserCompanionHash = vault.get(BROWSER_COMPANION_CREDENTIAL_VAULT_KEY)?.value; const browserCompanionCredentialHash = isBrowserCompanionCredentialHash(storedBrowserCompanionHash) ? storedBrowserCompanionHash : null; const litellmApiKey = fullConfig.useBuiltInProxy ? wsSessionToken : process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev'; // Build embedding config from WaggleConfig + Vault keys const waggleConfig = new WaggleConfig(fullConfig.dataDir || undefined); fullConfig.cli = { allowlist: waggleConfig.getCliAllowlist() }; const embeddingConfig: EmbeddingProviderConfig = waggleConfig.getEmbeddingConfig(); // Inject API keys from encrypted Vault (never from config.json) try { const voyageEntry = vault.get('voyage-api-key'); if (voyageEntry?.value) embeddingConfig.voyage = { apiKey: voyageEntry.value }; } catch { /* vault may not have this key */ } try { const openaiEntry = vault.get('openai'); if (openaiEntry?.value) embeddingConfig.openai = { apiKey: openaiEntry.value }; } catch { /* vault may not have this key */ } // Environment variable override for CI/headless if (process.env.VOYAGE_API_KEY && !embeddingConfig.voyage) { embeddingConfig.voyage = { apiKey: process.env.VOYAGE_API_KEY }; } if (process.env.OPENAI_API_KEY && !embeddingConfig.openai) { embeddingConfig.openai = { apiKey: process.env.OPENAI_API_KEY }; } // Create embedding provider with InProcess → Ollama → API → Mock fallback chain const embeddingProvider = await createEmbeddingProvider(embeddingConfig); const embedder = embeddingProvider; // satisfies Embedder — drop-in replacement // Decorate server with embedding provider for status endpoints server.decorate('embeddingProvider', embeddingProvider); const waggleHome = fullConfig.dataDir || path.join(os.homedir(), '.waggle'); const rerankerCacheDir = path.join(waggleHome, 'models', 'reranker'); // Orchestrator — connects to personal .mind const orchestrator = new Orchestrator({ db: multiMind.personal, embedder, mode: 'local', version: '0.4', rerankerCacheDir, }); ensureIdentity(orchestrator.getIdentity()); // Build tools — use a default workspace (homedir), but tools are rebuilt // per-request when a workspace directory is specified in chat. const defaultWorkspace = os.homedir(); const mindTools = orchestrator.getTools(); const systemTools = createSystemTools({ workspace: defaultWorkspace, denySensitiveFiles: true, }); const planTools = createPlanTools(); const gitTools = createGitTools(defaultWorkspace); const documentTools = [ ...createDocumentTools(defaultWorkspace), ...createSpreadsheetTools(defaultWorkspace), ...createPresentationTools(defaultWorkspace), ...createPdfTools(defaultWorkspace), ]; // Skill management tools — let the agent discover, install, create, and acquire skills const starterSkillsDir = getStarterSkillsDir(); const skillTools = createSkillTools({ waggleHome, starterSkillsDir, auditStore, nativeToolNames: [ ...mindTools.map(t => t.name), ...systemTools.map(t => t.name), ...planTools.map(t => t.name), ...gitTools.map(t => t.name), ...documentTools.map(t => t.name), ], getInstalledSkills: () => { // Return the current in-memory skill state (hot-reloadable) return server.agentState?.skills ?? loadSkills(waggleHome); }, onSkillsChanged: () => { // Hot-reload skills into agent state const fresh = loadSkills(waggleHome); // Will be set after agentState is created (see below) reloadSkills?.(fresh); }, searchMarketplace: async (query: string) => { // Search the marketplace DB if available (graceful degradation) if (!marketplaceDb) return []; try { const results = marketplaceDb.search({ query, limit: 10 }); return results.packages.map(pkg => ({ packageId: pkg.id, name: pkg.name, description: pkg.description, packageType: pkg.package_type, installType: pkg.waggle_install_type, version: pkg.version, author: pkg.author, source: 'marketplace', score: undefined, // FTS5 doesn't expose raw scores through our API })); } catch { return []; } }, }); // Cron tools — let the agent manage cron schedules (via REST API). // The chat route replaces surviving cron tools with request-bound copies so // overlapping turns cannot exchange delivery origins. const cronTools = createCronTools(); // Search tools — Tavily + Brave with vault-backed API keys const searchTools = createSearchTools(async (key: string) => { try { return vault.get(key)?.value ?? null; } catch { return null; } }); // Browser tools — Playwright-based browser automation const browserTools = createBrowserTools(defaultWorkspace); // LSP tools — TypeScript language server integration const lspTools = createLspTools(defaultWorkspace); // CLI tools — governed CLI program execution const cliAllowlist = fullConfig.cli?.allowlist ?? []; const cliTools = createCliTools({ allowlist: cliAllowlist, getAllowlist: () => fullConfig.cli?.allowlist ?? [], }); // Dynamic connector tools (initial — regenerated per workspace in buildToolsForWorkspace) const defaultConnectorTools = connectorRegistry.generateTools(); // Insights tools — self-analytics so the agent can reflect on its own performance. // Aggregates optimization logs + improvement signals across the personal mind and // every workspace so the agent sees a complete picture regardless of where it runs. const insightsTools = createInsightsTools({ getOptimizationLogs: (limit: number) => { const rows: Array<{ tools_used: string; was_correction: number; input_tokens: number; output_tokens: number; timestamp: string; }> = []; try { rows.push(...new OptimizationLogStore(multiMind.personal).getRecent(limit)); } catch { /* personal store unavailable — keep going */ } for (const ws of wsManager.list()) { const wsDb = getWorkspaceMindDb(ws.id); if (!wsDb) continue; try { rows.push(...new OptimizationLogStore(wsDb).getRecent(limit)); } catch { /* workspace store unavailable — skip */ } } rows.sort((a, b) => (b.timestamp ?? '').localeCompare(a.timestamp ?? '')); return rows.slice(0, limit).map(row => { let toolNames = ''; try { const parsed = JSON.parse(row.tools_used ?? '[]'); if (Array.isArray(parsed)) toolNames = parsed.join(','); } catch { /* malformed — leave empty */ } return { model: undefined, tool_names: toolNames, total_tokens: (row.input_tokens ?? 0) + (row.output_tokens ?? 0), estimated_cost: 0, was_correction: row.was_correction, created_at: row.timestamp, }; }); }, getImprovementSignals: () => { const collectFrom = (db: MindDB) => { const store = new ImprovementSignalStore(db); return [ ...store.getByCategory('capability_gap'), ...store.getByCategory('correction'), ...store.getByCategory('workflow_pattern'), ]; }; const all: Array<{ category: string; pattern_key: string; detail: string; count: number; first_seen: string; last_seen: string; }> = []; try { all.push(...collectFrom(multiMind.personal)); } catch { /* skip */ } for (const ws of wsManager.list()) { const wsDb = getWorkspaceMindDb(ws.id); if (!wsDb) continue; try { all.push(...collectFrom(wsDb)); } catch { /* skip */ } } return all.map(s => ({ category: s.category, pattern_key: s.pattern_key, detail: s.detail, count: s.count, first_seen: s.first_seen, last_seen: s.last_seen, })); }, }); // Connector search tools — give the agent natural-language discovery over // the 148-entry MCP catalog (find_connector, list_connector_categories). // Static data, zero I/O, no deps — cheap to always-include. const connectorSearchTools = createConnectorSearchTools(); // Collect all non-subagent tools first (sub-agent tools need the full list) const baseTools = [...mindTools, ...systemTools, ...planTools, ...gitTools, ...documentTools, ...skillTools, ...cronTools, ...searchTools, ...browserTools, ...lspTools, ...cliTools, ...insightsTools, ...connectorSearchTools, ...defaultConnectorTools]; // PR4: expose the full native-tool name union to the agent-search route so // its "you already have a tool for this" lane isn't blind to search/browser/ // cli/cron/connector tools (the agent's own union omits them). server.decorate('agentToolNames', baseTools.map(t => t.name)); // Sub-agent tools — let the main agent spawn specialist sub-agents. // onSubAgentStatus relays start/done/error transitions to the notifications // SSE stream so the Room canvas can render live tiles for sub-agents // spawned via spawn_agent (bug #9). Without this wiring, sub-agents ran // silently and Room stayed empty. // // onSubAgentComplete persists the result into the active mind (workspace if // active, else personal) so specialist work survives beyond the 30-min // in-memory eviction window — Skills 2.0 gap L. const subAgentTools = createSubAgentTools({ availableTools: baseTools, runLoop: runAgentLoop, get litellmUrl() { return fullConfig.litellmUrl; }, litellmApiKey: litellmApiKey, defaultModel: 'claude-sonnet-4-6', onSubAgentStatus: (event) => { emitSubagentStatus(server, server.agentState.activeWorkspaceId ?? defaultWorkspaceId, [{ id: event.agentId, name: event.name, role: event.role, // emitSubagentStatus uses the workflow-orchestrator vocabulary // ('failed' vs our internal 'error'); map at the boundary. status: event.status === 'error' ? 'failed' : event.status, task: event.task, toolsUsed: event.toolsUsed, startedAt: event.startedAt, completedAt: event.completedAt, }]); }, onSubAgentComplete: async (result) => { // Gap L — persist sub-agent result to the active mind so specialist // work is searchable after the 30-min in-memory cache evicts. // Best-effort: any failure is swallowed by subagent-tools so the // main-agent-visible return path stays intact. try { const wsId = server.agentState.activeWorkspaceId; const wsDb = wsId ? mindCache.getOrOpen(wsId) : null; const targetDb = wsDb ?? multiMind.personal; const mindLabel = wsDb ? `workspace:${wsId}` : 'personal'; // Structured content: tag provenance so search/evolution can filter // for sub-agent-derived frames and so humans can see where it came from. const content = `[Sub-agent: ${result.role}] ${result.agentName}\n\n${result.response}\n\n— tools: ${result.toolsUsed.join(', ') || 'none'} · ${(result.duration / 1000).toFixed(1)}s · ${result.usage.inputTokens + result.usage.outputTokens} tokens`; // Use raw frames — cognify requires a per-mind pipeline we don't // construct here; the decayFrames weaver will pick these up later. const frames = new FrameStore(targetDb); const sessions = new SessionStore(targetDb); const active = sessions.getActive(); const gopId = active.length > 0 ? active[0].gop_id : sessions.create().gop_id; const latestI = frames.getLatestIFrame(gopId); if (latestI) { frames.createPFrame(gopId, content, latestI.id, 'normal', 'agent_inferred'); } else { frames.createIFrame(gopId, content, 'normal', 'agent_inferred'); } log.debug(`Gap L: persisted sub-agent result ${result.agentId} to ${mindLabel} mind`); } catch (err) { log.debug('Gap L: sub-agent persistence failed (non-blocking)', err); } }, }); // Workflow tools — multi-agent workflow templates (research, review, plan-execute) const workflowTools = createWorkflowTools({ availableTools: baseTools, runLoop: runAgentLoop, get litellmUrl() { return fullConfig.litellmUrl; }, litellmApiKey: litellmApiKey, defaultModel: 'claude-sonnet-4-6', onWorkerStatus: (event) => { // Relay sub-agent status to eventBus for SSE notification stream const orch = server.agentState.subagentOrchestrator; const agents = orch ? orch.getWorkers().map(w => ({ id: w.id, name: w.name, role: w.role, status: w.status, task: w.task, toolsUsed: w.toolsUsed, startedAt: w.startedAt, completedAt: w.completedAt, })) : [{ id: event.workerId, name: event.workerState.name, role: event.workerState.role, status: event.workerState.status, task: event.workerState.task, toolsUsed: event.workerState.toolsUsed, startedAt: event.workerState.startedAt, completedAt: event.workerState.completedAt, }]; emitSubagentStatus(server, server.agentState.activeWorkspaceId ?? defaultWorkspaceId, agents); }, }); const allTools = [...baseTools, ...subAgentTools, ...workflowTools]; // Load user customizations from ~/.waggle/ const userSystemPrompt = loadSystemPrompt(waggleHome); const skills = loadSkills(waggleHome); // Skill hash store — detect skill changes on disk const skillHashStore = new SkillHashStore(multiMind.personal); server.decorate('skillHashStore', skillHashStore); // Check for changed skills at startup const hashCheck = skillHashStore.checkAll(skills); // Auto-verify new skills (first install — no change to flag) for (const name of hashCheck.added) { const skill = skills.find(s => s.name === name); if (skill) skillHashStore.verify(name, skill.content); } // Log changed skills as warnings if (hashCheck.changed.length > 0) { log.info(` WARNING: ${hashCheck.changed.length} skill(s) changed on disk: ${hashCheck.changed.join(', ')}`); log.info(' Run /skills to review changes'); } // Clean up removed skill hashes for (const name of hashCheck.removed) { skillHashStore.removeHash(name); } // Hook registry with user-configured hooks const hookRegistry = new HookRegistry(); await loadHooksFromConfig(path.join(waggleHome, 'hooks.json'), hookRegistry); // Cost tracker const costTracker = new CostTracker({}); const budgetConfig = new WaggleConfig(fullConfig.dataDir); costTracker.setBudget( budgetConfig.getDailyBudget(), budgetConfig.getBudgetHardCap() ? 'hard' : 'soft', ); const budgetDay = new Date().toISOString().slice(0, 10); const budgetDayStart = `${budgetDay}T00:00:00.000Z`; try { costTracker.initializeDailyCarryover( budgetDay, traceStore.getTotalCostSince(budgetDayStart, traceStore.getLatestId()), ); } catch (error) { if (budgetConfig.getBudgetHardCap() && budgetConfig.getDailyBudget() !== null) { throw new Error('Cannot initialize hard daily model budget from persisted spend', { cause: error }); } server.log.warn({ err: error }, 'Persisted daily model spend unavailable'); } // Command registry — workflow-native slash commands const commandRegistry = new CommandRegistry(); registerWorkflowCommands(commandRegistry); registerMarketplaceCommands(commandRegistry); // Plugin runtime manager — lifecycle, tools, skills from plugins const pluginRuntimeManager = new PluginRuntimeManager(); // Auto-load installed plugins from ~/.waggle/plugins/ const pluginsDir = path.join(waggleHome, 'plugins'); if (fs.existsSync(pluginsDir)) { try { const pluginDirs = fs.readdirSync(pluginsDir, { withFileTypes: true }) .filter(d => d.isDirectory()) .map(d => d.name); for (const pluginName of pluginDirs) { const pluginPath = path.join(pluginsDir, pluginName); const manifestPath = path.join(pluginPath, 'plugin.json'); if (!fs.existsSync(manifestPath)) continue; try { const raw = JSON.parse(fs.readFileSync(manifestPath, 'utf-8')) as Record; const validation = validatePluginManifest(raw); if (!validation.valid) { log.info(` Skipping plugin "${pluginName}": invalid manifest — ${validation.errors.join(', ')}`); continue; } pluginRuntimeManager.register( raw as unknown as Parameters[0], { pluginDir: pluginPath }, ); await pluginRuntimeManager.enable(raw.name as string); } catch (err) { log.info(` Failed to load plugin "${pluginName}": ${(err as Error).message}`); } } const active = pluginRuntimeManager.getActive(); if (active.length > 0) { log.info(` Loaded ${active.length} plugin(s): ${active.map(p => p.getManifest().name).join(', ')}`); } } catch { /* non-blocking — plugin scan failure must not prevent startup */ } } // MCP server runtime — stdio servers, health, tools. // UX-Refactor Phase 4 (C4): populated at boot from the persisted // /.mcp.json (written by the marketplace installer + /api/mcps). // Register-only — servers stay 'stopped' until started via // POST /api/mcps/:id/start, so boot never spawns surprise processes. const mcpRuntime = new McpRuntime(); populateMcpRuntimeFromConfig(mcpRuntime, fullConfig.dataDir, log); // Steal #6: relevance-gates MCP tools into the model pool. Holds the lazy // embedding index + per-conversation union-only accumulator; the mock-embedder // fallback keeps it honest when semantic embeddings are unavailable. const mcpToolRetriever = new McpToolRetriever({ embedder }); // Session histories (server-side, like CLI) const sessionHistories = new Map(); // Default model — W2C: initialize from config.json's defaultModel so the // top-bar chip and Settings → Models agree from the first launch (this was // hardcoded, so a user whose default was e.g. Opus saw Sonnet in the chip // until they changed it). The literal is only the last-resort fallback. const currentModel = new WaggleConfig(fullConfig.dataDir).getDefaultModel() || 'claude-sonnet-4-6'; // Pending approvals map for confirmation gates const pendingApprovals = new Map(); const workspaceTurnCoordinator = new WorkspaceTurnCoordinator(); // Phase B.3: persistent "always allow" grant store — survives sessions // so approved (tool, target) combinations don't re-prompt every time. const { ApprovalGrantStore } = await import('./approval-grants.js'); const approvalGrantStore = new ApprovalGrantStore(fullConfig.dataDir); // Skill hot-reload callback is defined as a const after agentState is created // (see `reloadSkills` below); referenced lazily from the onSkillsChanged closure. // L-18: Resolve a FileBackend for a workspace's storage type. Team (S3/MinIO) // workspaces return a provider-backed FileBackend so the agent's file tools // route through the shared bucket instead of the local filesystem. Local / // virtual workspaces return undefined — system-tools falls back to fs.* // and existing behavior is unchanged. const buildFileBackendForWorkspace = (workspaceId: string | undefined) => { if (!workspaceId) return undefined; try { const wsMeta = wsManager.get(workspaceId); if (!wsMeta || wsMeta.storageType !== 'team') return undefined; const provider = getStorageProvider( { id: wsMeta.id, storageType: wsMeta.storageType, storagePath: wsMeta.storagePath, storageConfig: (wsMeta as unknown as { storageConfig?: Record }).storageConfig, }, fullConfig.dataDir, ); return { read: (p: string) => provider.read(p), write: async (p: string, data: Buffer, mime?: string) => { await provider.write(p, data, mime); }, exists: (p: string) => provider.exists(p), delete: (p: string) => provider.delete(p), }; } catch (err) { server.log.warn({ err, workspaceId }, 'Failed to build FileBackend for workspace'); return undefined; } }; const createWorkspaceCollaborationTools = ( workerTools: ToolDefinition[], runLoop: AgentRunner, defaultModel: string, signal?: AbortSignal, ): ToolDefinition[] => { const workspaceSubAgentTools = createSubAgentTools({ availableTools: workerTools, runLoop, get litellmUrl() { return fullConfig.litellmUrl; }, litellmApiKey, defaultModel, onSubAgentStatus: (event) => { emitSubagentStatus(server, server.agentState.activeWorkspaceId ?? defaultWorkspaceId, [{ id: event.agentId, name: event.name, role: event.role, status: event.status === 'error' ? 'failed' : event.status, task: event.task, toolsUsed: event.toolsUsed, startedAt: event.startedAt, completedAt: event.completedAt, }]); }, }); const workspaceWorkflowTools = createWorkflowTools({ availableTools: workerTools, runLoop, get litellmUrl() { return fullConfig.litellmUrl; }, litellmApiKey, defaultModel, signal, onWorkerStatus: (event) => { const orch = server.agentState.subagentOrchestrator; const agents = orch ? orch.getWorkers().map(w => ({ id: w.id, name: w.name, role: w.role, status: w.status, task: w.task, toolsUsed: w.toolsUsed, startedAt: w.startedAt, completedAt: w.completedAt, })) : [{ id: event.workerId, name: event.workerState.name, role: event.workerState.role, status: event.workerState.status, task: event.workerState.task, toolsUsed: event.workerState.toolsUsed, startedAt: event.workerState.startedAt, completedAt: event.workerState.completedAt, }]; emitSubagentStatus(server, server.agentState.activeWorkspaceId ?? defaultWorkspaceId, agents); }, }); return [...workspaceSubAgentTools, ...workspaceWorkflowTools]; }; const bindWorkspaceCollaborationTools = ( options: WorkspaceCollaborationBinding, ): ToolDefinition[] => bindWorkspaceChildTools(options, createWorkspaceCollaborationTools); // Factory to rebuild workspace-scoped tools for a given directory. // Optional `orchForMindTools` replaces the shared-orchestrator mind tools with // a session-specific orchestrator's tools (Option Y fix from Phase A.1 plan). const buildToolsForWorkspace = ( wsPath: string, orchForMindTools?: Orchestrator, workspaceId?: string, ): ToolDefinition[] => { // Dynamic connector tools (only for connected connectors) const connectorTools = connectorRegistry.generateTools(); // Resolve which mind tools to use — per-session (if provided) or the shared default. // Per-session is required for concurrent multi-workspace chat; the shared default // is kept for legacy call sites that are still being migrated. const mindToolsForRequest = orchForMindTools ? orchForMindTools.getTools() : mindTools; // L-18: when the workspace is team-backed, route file tools through the // shared provider. Otherwise pass only the cwd (fs fallback). const fileBackend = buildFileBackendForWorkspace(workspaceId); const wsMeta = workspaceId ? wsManager.get(workspaceId) : null; const denySensitiveFiles = Boolean(wsMeta?.directory || wsMeta?.storagePath); const wsBase = [ ...mindToolsForRequest, ...createSystemTools({ workspace: wsPath, fileBackend, denySensitiveFiles }), ...createPlanTools(), ...createGitTools(wsPath), ...createDocumentTools(wsPath), ...createSpreadsheetTools(wsPath), ...createPresentationTools(wsPath), ...createPdfTools(wsPath), ...skillTools, ...cronTools, ...searchTools, ...createBrowserTools(wsPath), ...createLspTools(wsPath), ...cliTools, ...connectorTools, ]; return [ ...wsBase, ...createWorkspaceCollaborationTools(wsBase, runAgentLoop, 'claude-sonnet-4-6'), ]; }; // ── Workspace Session Manager ──────────────────────────────────── // Manages concurrent workspace sessions with independent minds, orchestrators, // and tools. const sessionManager = new WorkspaceSessionManager(MAX_WORKSPACE_SESSIONS); server.decorate('sessionManager', sessionManager); /** * Create a fresh Orchestrator for a workspace session. * * Each session gets its own instance so concurrent sessions on different * workspaces cannot corrupt each other's workspace layers via setWorkspaceMind(). * Pointer to the shared personal mind is safe — better-sqlite3 WAL mode handles * concurrent reads and serializes writes through SQLite's internal lock. * * See docs/plans/phase-a-the-room.md §9 for the Option X vs Option Y decision. */ const createSessionOrchestrator = (workspaceMind?: MindDB): Orchestrator => { const sessionOrch = new Orchestrator({ db: multiMind.personal, embedder, mode: 'local', version: '0.4', rerankerCacheDir, }); if (workspaceMind) sessionOrch.setWorkspaceMind(workspaceMind); return sessionOrch; }; /** * Build the tool pool for a workspace session using the session's own orchestrator * for mind tools. Includes cross-workspace read tools (Phase B.2) that capture * the source workspace as a closure so cross-workspace citations identify both * sides of the access. */ const buildToolsForSession = ( sessionOrch: Orchestrator, wsPath: string, sourceWorkspaceId?: string, ): ToolDefinition[] => { // L-18: pass sourceWorkspaceId through so buildToolsForWorkspace can resolve // the workspace's storage type and thread a FileBackend for team storage. const base = buildToolsForWorkspace(wsPath, sessionOrch, sourceWorkspaceId); if (!sourceWorkspaceId) return base; const crossTools = createCrossWorkspaceTools({ sourceWorkspaceId, embedder, getMindForWorkspace: (id) => mindCache.getOrOpen(id), listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })), listWorkspaceFiles: async (id, subPath) => { try { const entries = readFileRegistry(fullConfig.dataDir, id); // FileRegistryEntry only has name/type/summary/sizeBytes/ingestedAt — // there's no real path hierarchy for virtual storage, so we filter by // name prefix when a subPath is provided. const filtered = subPath ? entries.filter(e => e.name.startsWith(subPath)) : entries; return filtered.map(e => ({ name: e.name, type: 'file' as const, size: e.sizeBytes, modifiedAt: e.ingestedAt, })); } catch { return []; } }, }); 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); const vectorEnrichmentService = new VectorEnrichmentService({ personalMind: multiMind.personal, embeddingProvider, listWorkspaceIds: () => wsManager.list().map(workspace => workspace.id), acquireWorkspaceMind: workspaceId => mindCache.acquire(workspaceId), releaseWorkspaceMind: workspaceId => mindCache.release(workspaceId), log: (level, message) => log[level](message), }); let activeWorkspaceId: string | null = null; const setActiveWorkspaceId = (workspaceId: string | null): void => { activeWorkspaceId = workspaceId; const decorated = (server as FastifyInstance & { agentState?: AgentState }).agentState; if (decorated) decorated.activeWorkspaceId = workspaceId; }; // ── TeamSync cache — one TeamSync instance per team workspace ── const teamSyncCache = new Map(); const serializeTeamSyncBinding = ( teamServer: NonNullable>, teamId: string, ): string => JSON.stringify({ url: teamServer.url, teamId, token: crypto.createHash('sha256').update(teamServer.token ?? '').digest('hex'), userId: teamServer.userId ?? 'local-user', displayName: teamServer.displayName ?? 'You', }); function getTeamSync(workspaceId: string, wsConfig: WorkspaceConfig | null, waggleConfig: WaggleConfig): TeamSync | null { if (!wsConfig?.teamId || !wsConfig?.teamServerUrl) { teamSyncCache.delete(workspaceId); return null; } const teamServer = getBoundTeamServer(wsConfig.teamServerUrl, waggleConfig.getTeamServer()); if (!teamServer?.token) { teamSyncCache.delete(workspaceId); return null; } const binding = serializeTeamSyncBinding(teamServer, wsConfig.teamId); const cached = teamSyncCache.get(workspaceId); if (cached?.binding === binding) return cached.sync; const sync = new TeamSync({ teamServerUrl: teamServer.url, teamSlug: wsConfig.teamId, // teamId is used as slug authToken: teamServer.token, userId: teamServer.userId ?? 'local-user', displayName: teamServer.displayName ?? 'You', }, fetchTeamServer); const pushFrame = sync.pushFrame.bind(sync); sync.pushFrame = async (frame) => { const currentWorkspace = wsManager.get(workspaceId); const currentWaggleConfig = new WaggleConfig(fullConfig.dataDir); const currentTeamServer = getBoundTeamServer( currentWorkspace?.teamServerUrl, currentWaggleConfig.getTeamServer(), ); const currentBinding = currentWorkspace?.teamId && currentTeamServer?.token ? serializeTeamSyncBinding(currentTeamServer, currentWorkspace.teamId) : null; const cached = teamSyncCache.get(workspaceId); if (cached?.sync !== sync) { return cached && currentBinding === cached.binding ? cached.sync.pushFrame(frame) : null; } if (currentBinding === binding) return pushFrame(frame); teamSyncCache.delete(workspaceId); if (currentWorkspace?.teamId && currentTeamServer?.token) { const replacement = getTeamSync(workspaceId, currentWorkspace, currentWaggleConfig); if (replacement) { if (activeWorkspaceId === workspaceId) orchestrator.setTeamSync(replacement); return replacement.pushFrame(frame); } } if (activeWorkspaceId === workspaceId) orchestrator.setTeamSync(null); return null; }; teamSyncCache.set(workspaceId, { sync, binding }); return sync; } /** * Activate a workspace's .mind file on the orchestrator. * Opens the mind if not cached, then calls setWorkspaceMind(). */ const activateWorkspaceMind = (workspaceId: string): boolean => { if (activeWorkspaceId === workspaceId) return true; // already active if (!wsManager.get(workspaceId)) return false; const wsDb = mindCache.getOrOpen(workspaceId); if (!wsDb) return false; orchestrator.setWorkspaceMind(wsDb); setActiveWorkspaceId(workspaceId); return true; }; // ── Memory Weaver — background consolidation & decay (A1 fix) ──── // Runs on the personal mind at startup. Workspace minds get weavers when activated. const personalFrames = new FrameStore(multiMind.personal); const personalSessions = new SessionStore(multiMind.personal); const personalWeaver = new MemoryWeaver(multiMind.personal, personalFrames, personalSessions); const weaverTimers: NodeJS.Timeout[] = []; const workspaceWeavers = new Map(); // F2: Track weaver last-run timestamps for status endpoint const weaverState: { lastPersonalConsolidation: string | null; lastPersonalDecay: string | null } = { lastPersonalConsolidation: null, lastPersonalDecay: null, }; const workspaceWeaverStatus: Record = {}; // Run personal mind weaver on intervals const runPersonalConsolidation = () => { try { const active = personalSessions.getActive(); for (const s of active) personalWeaver.consolidateGop(s.gop_id); weaverState.lastPersonalConsolidation = new Date().toISOString(); } catch (err) { log.debug('Personal consolidation skipped', err); } }; const runPersonalDecay = () => { try { personalWeaver.decayFrames(); personalWeaver.strengthenFrames(); weaverState.lastPersonalDecay = new Date().toISOString(); } catch (err) { log.debug('Personal decay skipped', err); } }; weaverTimers.push(setInterval(runPersonalConsolidation, 60 * 60 * 1000)); // hourly weaverTimers.push(setInterval(runPersonalDecay, 24 * 60 * 60 * 1000)); // daily // ── Harvest auto-sync — poll stale sources every 30 min ── const runHarvestAutoSync = async () => { try { const harvestStore = new HarvestSourceStore(multiMind.personal); const stale = harvestStore.getStale(); if (stale.length === 0) return; const { ClaudeCodeAdapter: CCA } = await import('@waggle/core'); const personalSessionStore = new SessionStore(multiMind.personal); personalSessionStore.ensure('harvest', 'harvest', 'Imported memory from external sources'); const personalFrameStore = new FrameStore(multiMind.personal); // #7 sticky erasure: don't re-materialize an erased subject on auto-sync. const suppression = new SuppressionStore(multiMind.personal); for (const src of stale) { try { // Only auto-sync filesystem-scannable sources if (src.source !== 'claude-code') continue; const homedir = (await import('node:os')).default.homedir(); const scanDir = (await import('node:path')).default.join(homedir, '.claude'); const adapter = new CCA(); const items = adapter.scan(scanDir); if (items.length === 0) continue; let saved = 0; let couldNotVerify = 0; let skippedUnsafe = 0; for (const item of items) { // #7 sticky erasure: distinguish a confirmed erasure MATCH from a // fail-closed read ERROR — both skip the write, but a broken-DB read // must be reported as "could not verify", not silently as "erased". const supp = suppression.checkSuppressed(item.source, item.id); if (supp.suppressed) { if (supp.reason === 'error') couldNotVerify++; continue; // #7 Art.17 erased (or unverifiable — fail-closed skip) } // #7 Art.17: stamp the subject key (metadata.sourceId) so a subject-mode // DSAR can reach this auto-synced summary — shared with the cron path. if (!writeAutoSyncSummaryFrame(personalFrameStore, item)) { skippedUnsafe++; continue; } saved++; } // R3-004: store the content digest so the manual harvest route can // skip an unchanged re-scan on the next sync. harvestStore.recordSync(src.source, items.length, saved, harvestSetHash(items)); log.info(`[harvest-auto-sync] ${src.source}: imported ${saved} items` + (couldNotVerify > 0 ? ` (${couldNotVerify} could not be verified against the erasure list — skipped, fail-closed)` : '') + (skippedUnsafe > 0 ? ` (${skippedUnsafe} unsafe items skipped)` : '')); } catch (err) { log.debug(`[harvest-auto-sync] ${src.source} failed:`, err); } } } catch (err) { log.debug('[harvest-auto-sync] skipped:', err); } }; weaverTimers.push(setInterval(runHarvestAutoSync, 30 * 60 * 1000)); // every 30 min // Extend activateWorkspaceMind to also start a weaver for the workspace // and distill any undistilled sessions into durable memory frames. const baseActivateWorkspaceMind = activateWorkspaceMind; const activateWorkspaceMindWithWeaver = (workspaceId: string): boolean => { const result = baseActivateWorkspaceMind(workspaceId); // E4: Workspace topic tracking REMOVED — was saving to personal mind, causing // cross-workspace leakage. Workspace names are available from workspace config // and don't need to be duplicated into memory frames. if (result && !workspaceWeavers.has(workspaceId)) { const wsDb = mindCache.getIfOpen(workspaceId); if (wsDb) { const wsFrames = new FrameStore(wsDb); const wsSessions = new SessionStore(wsDb); const wsWeaver = new MemoryWeaver(wsDb, wsFrames, wsSessions); const timers: NodeJS.Timeout[] = []; timers.push(setInterval(() => { try { const active = wsSessions.getActive(); for (const s of active) wsWeaver.consolidateGop(s.gop_id); } catch { /* non-blocking */ } }, 60 * 60 * 1000)); timers.push(setInterval(() => { try { wsWeaver.decayFrames(); wsWeaver.strengthenFrames(); } catch (err) { log.debug('Workspace memory decay skipped', err); } }, 24 * 60 * 60 * 1000)); workspaceWeavers.set(workspaceId, { weaver: wsWeaver, timers }); // E2: Distill undistilled sessions into durable memory frames on activation try { const sessionsDir = path.join(fullConfig.dataDir, 'workspaces', workspaceId, 'sessions'); const undistilled = findUndistilledSessions(sessionsDir); for (const session of undistilled) { const distilled = wsWeaver.distillSessionContent(session.date, session.summary, session.keyPoints); if (distilled) markSessionDistilled(session.filePath); } } catch (err) { log.debug('Workspace distillation skipped', err); } } } // After mind activation for team workspaces — pull remote frames if (result) { const wsConfig = wsManager.get(workspaceId); if (wsConfig?.teamId) { const sync = getTeamSync(workspaceId, wsConfig, new WaggleConfig(fullConfig.dataDir)); if (sync) { // Set TeamSync on the orchestrator for push-on-write orchestrator.setTeamSync(sync); // Pull team frames into local workspace mind const wsDb = mindCache.getIfOpen(workspaceId); if (wsDb) { const wsSessionStore = new SessionStore(wsDb); wsSessionStore.ensure('team-sync', 'team-sync', 'Frames synced from team server'); const wsFrameStore = new FrameStore(wsDb); sync.pullFrames(sync.getLastSyncTimestamp() ?? undefined).then(pulled => { if (pulled.length === 0) return; let inserted = 0; for (const f of pulled) { try { // Provenance is carried in the content prefix ([Team:author]); the source // column must be schema-valid, so use 'import' (these frames are imported // from the team server). Previously passed 'team_sync', which violated the // memory_frames.source CHECK → every pulled frame was silently skipped. wsFrameStore.createIFrame('team-sync', `[Team:${f.authorName}] ${f.content}`, f.importance, 'import'); inserted++; } catch { /* duplicate or constraint — skip */ } } if (inserted > 0) { log.info(`TeamSync: pulled ${inserted} new frames for workspace ${workspaceId}`); } }).catch(err => { log.warn(`TeamSync pull failed:`, err.message); }); } } else { orchestrator.setTeamSync(null); } } else { // Non-team workspace — clear TeamSync orchestrator.setTeamSync(null); } } return result; }; // 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. sessionManager.close(workspaceId); mindCache.close(workspaceId); teamSyncCache.delete(workspaceId); if (activeWorkspaceId === workspaceId) { orchestrator.setTeamSync(null); orchestrator.clearWorkspaceMind(); setActiveWorkspaceId(null); } }; const getWorkspaceMindDb = (workspaceId: string): MindDB | null => { if (!wsManager.get(workspaceId)) return null; return mindCache.getOrOpen(workspaceId); }; // Decorate with shared agent state server.decorate('agentState', { orchestrator, allTools, hookRegistry, costTracker, skills, userSystemPrompt, sessionHistories, currentModel, litellmApiKey, pendingApprovals, workspaceTurnCoordinator, approvalGrantStore, buildToolsForWorkspace, createSessionOrchestrator, buildToolsForSession, bindWorkspaceCollaborationTools, activateWorkspaceMind: activateWorkspaceMindWithWeaver, getWorkspaceMindDb, closeWorkspaceMind, listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })), activeWorkspaceId, weaverState, workspaceWeaverStatus, subagentOrchestrator: null, pluginRuntimeManager, mcpRuntime, mcpToolRetriever, commandRegistry, llmProvider: { provider: 'anthropic-proxy' as const, health: 'unavailable' as const, detail: 'Not yet initialized', checkedAt: new Date().toISOString(), }, wsSessionToken, browserCompanionCredentialHash, }); activateWorkspaceMindWithWeaver(defaultWorkspaceId); // Wire up skill hot-reload callback const reloadSkills = (fresh: LoadedSkill[]) => { server.agentState.skills.length = 0; server.agentState.skills.push(...fresh); }; // Dream Diary — records the nightly curation runs below so /api/dreams can // narrate them to the user (docs/plans/DREAM-DIARY-2026-07-09.md). const dreamJournal = new DreamJournal(fullConfig.dataDir); server.decorate('dreamJournal', dreamJournal); const executorRegistry = new ExecutorRegistry({ detectTools: async () => (await detectInstalledTools()).tools, personas: () => PERSONAS, }); server.decorate('executorRegistry', executorRegistry); // Local scheduler — runs cron jobs in-process (Solo, no Redis/BullMQ) const persistCronHistory = makeRecordExecutionCallback(cronStore); // #17: hard daily bound on ai_task firings per schedule — each firing is a // full agent turn (real LLM spend), so a runaway cron must self-limit even // past the create-time min-interval guard. const AI_TASK_DAILY_CAP = 24; const scheduler = new LocalScheduler(cronStore, async (schedule) => { switch (schedule.job_type) { case 'memory_consolidation': { // Check if this is a marketplace sync (uses memory_consolidation type for system-level scheduling) const mcJobConfig = JSON.parse(schedule.job_config || '{}'); if (mcJobConfig.action === 'index_reconcile') { try { // Reconcile personal mind FTS/vec indexes const result = await reconcileIndexes(multiMind.personal); if (result.ftsFixed > 0 || result.vecFixed > 0) { log.info(`[cron] Index reconciliation: FTS=${result.ftsFixed} vec=${result.vecFixed} fixed (personal)`); } // Reconcile workspace minds let ftsTotal = result.ftsFixed; let vecTotal = result.vecFixed; const workspaces = wsManager.list(); for (const ws of workspaces) { const wsDb = getWorkspaceMindDb(ws.id); if (wsDb) { const wsResult = await reconcileIndexes(wsDb); if (wsResult.ftsFixed > 0 || wsResult.vecFixed > 0) { log.info(`[cron] Index reconciliation: FTS=${wsResult.ftsFixed} vec=${wsResult.vecFixed} fixed (workspace "${ws.name}")`); } ftsTotal += wsResult.ftsFixed; vecTotal += wsResult.vecFixed; } } dreamJournal.record('index_reconcile', { ftsFixed: ftsTotal, vecFixed: vecTotal }); } catch (err) { log.warn(`[cron] Index reconciliation failed: ${(err as Error).message}`); } } else if (mcJobConfig.action === 'marketplace_sync' && marketplaceDb) { try { const vaultLookup = server.vault ? (key: string) => server.vault!.get(key)?.value ?? null : undefined; const sync = new MarketplaceSync(marketplaceDb, vaultLookup, (url, init) => safeFetch(url, init)); const results = await sync.syncAll(); const totalAdded = results.reduce((sum, r) => sum + r.added, 0); if (totalAdded > 0) { eventBus.emit('notification', { type: 'notification', timestamp: new Date().toISOString(), title: 'Marketplace sync complete', body: `${totalAdded} new capability${totalAdded === 1 ? '' : 's'} discovered`, category: 'agent', actionUrl: '/skills', }); } log.info(`[cron] Marketplace sync: ${totalAdded} added across ${results.length} sources`); } catch (err) { log.warn(`[cron] Marketplace sync failed: ${(err as Error).message}`); } } else if (mcJobConfig.action === 'harvest_sync') { // Phase 7: Memory Harvest — scan registered filesystem sources for new data. // Only filesystem-based sources (currently claude-code) can auto-sync; API-based // sources like chatgpt/gemini require user-uploaded exports. try { // Frames have a FK to sessions(gop_id) — ensure the stable // `harvest` session row exists before creating any frames. new SessionStore(multiMind.personal).ensure('harvest', 'harvest', 'Imported memory from external sources'); const harvestStore = new HarvestSourceStore(multiMind.personal); const personalFrames = new FrameStore(multiMind.personal); // #7 sticky erasure: don't re-materialize an erased subject on cron sync. const suppression = new SuppressionStore(multiMind.personal); const stale = harvestStore.getStale(); let totalItems = 0; let totalFrames = 0; let sourcesScanned = 0; let totalCouldNotVerify = 0; let totalSkippedUnsafe = 0; for (const src of stale) { if (src.source !== 'claude-code' || !src.sourcePath) continue; try { const adapter = new ClaudeCodeAdapter(); const items = adapter.scan(src.sourcePath); let saved = 0; for (const item of items) { // #7 sticky erasure: a fail-closed read ERROR is skipped like a // MATCH but tallied separately (report "could not verify", not "erased"). const supp = suppression.checkSuppressed(item.source, item.id); if (supp.suppressed) { if (supp.reason === 'error') totalCouldNotVerify++; continue; // #7 Art.17 erased (or unverifiable — fail-closed skip) } // #7 Art.17: stamp the subject key (metadata.sourceId) so a // subject-mode DSAR reaches this cron-synced summary — shared helper. if (!writeAutoSyncSummaryFrame(personalFrames, item)) { totalSkippedUnsafe++; continue; } saved++; } // R3-004: store the content digest for next-sync skip. harvestStore.recordSync(src.source, items.length, saved, harvestSetHash(items)); totalItems += items.length; totalFrames += saved; sourcesScanned++; } catch (innerErr) { log.warn(`[cron] Harvest sync: source "${src.source}" failed: ${(innerErr as Error).message}`); } } if (sourcesScanned > 0) { log.info(`[cron] Harvest sync: ${totalFrames} frames from ${totalItems} items across ${sourcesScanned} source(s)` + (totalCouldNotVerify > 0 ? ` (${totalCouldNotVerify} could not be verified against the erasure list — skipped, fail-closed)` : '') + (totalSkippedUnsafe > 0 ? ` (${totalSkippedUnsafe} unsafe items skipped)` : '')); } dreamJournal.record('harvest_sync', { framesSaved: totalFrames, itemsScanned: totalItems, sourcesScanned, couldNotVerify: totalCouldNotVerify, }); } catch (err) { log.warn(`[cron] Harvest sync failed: ${(err as Error).message}`); } } else if (mcJobConfig.action === 'memory_compact') { // Phase 9a: Memory compaction — prune temporary/deprecated frames, merge stale P-frames. // Runs across personal mind and every workspace mind. try { const personalResult = new FrameStore(multiMind.personal).compact(); let wsTempPruned = 0; let wsDepPruned = 0; let wsMerged = 0; for (const ws of wsManager.list()) { const wsDb = getWorkspaceMindDb(ws.id); if (!wsDb) continue; try { const r = new FrameStore(wsDb).compact(); wsTempPruned += r.temporaryPruned; wsDepPruned += r.deprecatedPruned; wsMerged += r.pframesMerged; } catch (innerErr) { log.warn(`[cron] Memory compaction: workspace "${ws.name}" failed: ${(innerErr as Error).message}`); } } dreamJournal.record('memory_compact', { temporaryPruned: personalResult.temporaryPruned + wsTempPruned, deprecatedPruned: personalResult.deprecatedPruned + wsDepPruned, pframesMerged: personalResult.pframesMerged + wsMerged, }); const total = personalResult.temporaryPruned + personalResult.deprecatedPruned + personalResult.pframesMerged + wsTempPruned + wsDepPruned + wsMerged; if (total > 0) { log.info( `[cron] Memory compaction: personal(temp=${personalResult.temporaryPruned}, dep=${personalResult.deprecatedPruned}, merged=${personalResult.pframesMerged}) ` + `workspaces(temp=${wsTempPruned}, dep=${wsDepPruned}, merged=${wsMerged})` ); } } catch (err) { log.warn(`[cron] Memory compaction failed: ${(err as Error).message}`); } } else if (mcJobConfig.action === 'memory_lane_extract') { // W4.3d: extraction of the benchmark-proven recall lanes // (facts/events/profiles) over frames written since the last run. // Routes through the built-in proxy ('fast' tier = haiku) — the // same internal-LLM pattern as prompt_optimization above. try { const llmCall = async (prompt: string, _model: 'fast' | 'accurate'): Promise => { const proxyUrl = `http://127.0.0.1:${fullConfig.port ?? 3333}/v1/chat/completions`; const res = await fetch(proxyUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, signal: AbortSignal.timeout(90_000), body: JSON.stringify({ model: 'claude-haiku-4-5', max_tokens: 4096, messages: [{ role: 'user', content: prompt }], }), }); if (!res.ok) throw new Error(`lane-extract LLM HTTP ${res.status}`); const data = await res.json() as unknown; return parseOpenAiTextCompletion(data).content; }; const minds: Array<{ label: string; db: import('@waggle/core').MindDB }> = [ { label: 'personal', db: multiMind.personal }, ]; for (const ws of wsManager.list()) { const wsDb = getWorkspaceMindDb(ws.id); if (wsDb) minds.push({ label: `workspace "${ws.name}"`, db: wsDb }); } let laneFramesProcessed = 0; let laneFacts = 0; let laneEvents = 0; let laneProfiles = 0; for (const mind of minds) { try { const r = await runMemoryLaneExtraction(mind.db, llmCall); if (!r.skipped) { if (r.errors.length > 0) { log.warn( `[cron] Memory lanes (${mind.label}) incomplete; watermark held for retry: ` + r.errors.join('; ').slice(0, 200) ); continue; } log.info( `[cron] Memory lanes (${mind.label}): ${r.framesProcessed} frames → ` + `facts=${r.written?.factsWritten ?? 0} events=${r.written?.eventsWritten ?? 0} ` + `profiles=${r.written?.profilesWritten ?? 0}` + (r.errors.length ? ` (errors: ${r.errors.join('; ').slice(0, 200)})` : '') ); laneFramesProcessed += r.framesProcessed; laneFacts += r.written?.factsWritten ?? 0; laneEvents += r.written?.eventsWritten ?? 0; laneProfiles += r.written?.profilesWritten ?? 0; } } catch (innerErr) { log.warn(`[cron] Memory lanes (${mind.label}) failed: ${(innerErr as Error).message}`); } } dreamJournal.record('memory_lane_extract', { framesProcessed: laneFramesProcessed, factsWritten: laneFacts, eventsWritten: laneEvents, profilesWritten: laneProfiles, }); } catch (err) { log.warn(`[cron] Memory lane extraction failed: ${(err as Error).message}`); } } else { // Normal memory consolidation try { runPersonalConsolidation(); } catch { /* non-blocking */ } } // UX-Refactor Phase 3: cron_execution_history retention. recordExecution // now writes a row per tick (Journey 16), so prune >30-day rows on the // same nightly consolidation cadence (mirrors optStore.pruneOlderThan). try { cronStore.pruneExecutionHistory(30); } catch { /* best-effort */ } // L2: flip held actions past their 7-day TTL to 'expired' so stale // proposals stop being approvable and the queue doesn't grow unbounded. try { cronStore.expireStalePendingActions(); } catch { /* best-effort */ } break; } case 'workspace_health': // Log stale frame count per workspace as awareness flag try { const workspaces = wsManager.list(); for (const ws of workspaces) { const wsDb = getWorkspaceMindDb(ws.id); if (wsDb) { const wsFrames = new FrameStore(wsDb); const staleCount = wsFrames.list({ limit: 100 }) .filter(f => { const age = Date.now() - new Date(f.last_accessed).getTime(); return age > 7 * 24 * 60 * 60 * 1000; }).length; if (staleCount > 0) { const wsAwareness = new AwarenessLayer(wsDb); wsAwareness.add('flag', `Workspace "${ws.name}" has ${staleCount} stale memory frames (>7 days untouched)`, 0); } } } } catch { /* non-blocking */ } break; case 'proactive': { const proactiveConfig = JSON.parse(schedule.job_config || '{}'); const proactiveCtx: ProactiveContext = { dataDir: fullConfig.dataDir, workspaceManager: wsManager, getWorkspaceMindDb, }; /** Emit a ProactiveMessage through the delivery router pipeline. */ const emitProactive = (msg: ProactiveMessage) => { // Load delivery preferences from config (or use defaults) let deliveryPrefs: DeliveryPreferences; try { const configPath = path.join(fullConfig.dataDir, 'delivery-preferences.json'); if (fs.existsSync(configPath)) { deliveryPrefs = { ...createDefaultDeliveryPreferences(), ...JSON.parse(fs.readFileSync(configPath, 'utf-8')) }; } else { deliveryPrefs = createDefaultDeliveryPreferences(); } } catch { deliveryPrefs = createDefaultDeliveryPreferences(); } const inAppEmitter = (notification: { title: string; body: string; category: string; actionUrl?: string }) => { emitNotification( server, { ...notification, category: notification.category as 'cron' | 'agent' | 'task' }, msg.dedupeKey && msg.materialHash ? { dedupeKey: msg.dedupeKey, materialHash: msg.materialHash } : undefined, ); }; deliverCronResult( { title: msg.title, body: msg.body, jobType: msg.type, workspaceId: msg.workspaceId, priority: msg.priority }, deliveryPrefs, server.connectorRegistry, inAppEmitter, ).catch(err => log.warn(`[cron-delivery] Delivery failed: ${(err as Error).message}`)); }; try { switch (proactiveConfig.action) { case 'morning_briefing': { const briefing = generateMorningBriefing(proactiveCtx); if (briefing) emitProactive(briefing); break; } case 'stale_workspace_check': { const staleAlerts = checkStaleWorkspaces(proactiveCtx); for (const alert of staleAlerts) emitProactive(alert); break; } case 'task_reminder': { const reminders = checkPendingTasks(proactiveCtx); for (const reminder of reminders) emitProactive(reminder); break; } case 'capability_suggestion': { const suggestion = suggestCapabilities(proactiveCtx); if (suggestion) emitProactive(suggestion); break; } default: log.warn(`[cron] Unknown proactive action: ${proactiveConfig.action}`); } } catch (err) { log.warn(`[cron] Proactive handler failed: ${(err as Error).message}`); } break; } case 'prompt_optimization': { // Background GEPA/Ax prompt optimization — runs daily at 2 AM. // Iterates workspaces with optimizationEnabled, checks budget, reads logs, // and invokes PromptOptimizer for variant generation. try { const workspaces = wsManager.list().filter(ws => ws.optimizationEnabled); for (const ws of workspaces) { const wsDb = getWorkspaceMindDb(ws.id); if (!wsDb) continue; const optStore = new OptimizationLogStore(wsDb); const budget = ws.optimizationBudget ?? 100; // cents, default $1/day // Check budget before spending any tokens if (!isWithinBudget(optStore, budget)) { log.info(`[cron] Prompt optimization: workspace "${ws.name}" over budget, skipping`); continue; } // Read recent logs for analysis const recentLogs = getRecentLogs(optStore, 100); if (recentLogs.length < 5) { // Not enough data to optimize yet continue; } // Analyze patterns: high correction rate, common tools, avg turn count const stats = optStore.getStats(); const correctionRate = stats.correctionRate; const avgTurns = stats.avgTurnCount; // Only trigger optimization if correction rate is above threshold // or turn count is significantly above average (indicating inefficiency) if (correctionRate > 0.2 || avgTurns > 15) { const rate = (correctionRate * 100).toFixed(1); log.info(`[cron] Prompt optimization: workspace "${ws.name}" — correction rate ${rate}%, avg turns ${avgTurns.toFixed(1)}`); // Re-check budget before the LLM call (another workspace may have consumed tokens) if (!isWithinBudget(optStore, budget)) { log.info(` GEPA optimization skipped — daily budget exceeded`); continue; } // Get the most recent system prompt from the workspace's logs const currentSystemPrompt = recentLogs[0]?.system_prompt ?? ''; if (!currentSystemPrompt) { log.info(`[cron] Prompt optimization: no system prompt in logs, skipping`); continue; } // Generate variant via the built-in Anthropic proxy (localhost) try { const proxyUrl = `http://127.0.0.1:${fullConfig.port ?? 3333}/v1/chat/completions`; const variantResponse = await fetch(proxyUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'claude-sonnet-4-6', max_tokens: 4096, messages: [ { role: 'system', content: `You are a prompt optimization specialist. Your task is to improve an AI agent's system prompt based on performance signals. The agent's correction rate is ${rate}% (target: under 20%) and average turn count is ${avgTurns.toFixed(1)} (target: under 15). A high correction rate means the agent frequently misunderstands instructions or produces wrong outputs. A high turn count means the agent takes too many steps to complete tasks. Analyze the current system prompt and generate an improved variant that: 1. Adds clearer instructions for areas where corrections are common 2. Reduces unnecessary verbosity that inflates turn count 3. Preserves the core identity and capabilities 4. Is production-ready (not a draft or explanation) Return ONLY the improved system prompt text. No commentary, no markdown fences, no explanation.`, }, { role: 'user', content: `Here is the current system prompt to improve:\n\n${currentSystemPrompt.slice(0, 12000)}`, }, ], }), signal: AbortSignal.timeout(60_000), }); if (!variantResponse.ok) { log.warn(`[cron] GEPA variant generation failed: HTTP ${variantResponse.status}`); } else { const variantBody = await variantResponse.json() as unknown; const variantText = parseOpenAiTextCompletion(variantBody).content; if (variantText.length > 100) { // Store the variant in the optimization_log with a marker optStore.insert({ sessionId: `gepa-variant-${Date.now()}`, workspaceId: ws.id, systemPrompt: variantText, toolsUsed: ['gepa_variant'], turnCount: 0, wasCorrection: false, inputTokens: currentSystemPrompt.length, outputTokens: variantText.length, }); log.info(` GEPA generated prompt variant (correction_rate=${rate}%)`); } else { log.warn(`[cron] GEPA variant too short (${variantText.length} chars), discarding`); } } } catch (variantErr) { log.warn(`[cron] GEPA variant generation error: ${(variantErr as Error).message}`); } // Anti-nag: one signal per (workspace, month, correction-rate bucket). // Re-runs on the same month with an unchanged rate bucket are suppressed. emitNotification(server, { title: 'Prompt optimization signal', body: `Workspace "${ws.name}" has ${(correctionRate * 100).toFixed(0)}% correction rate — optimization candidate`, category: 'agent', }, { dedupeKey: `prompt_optimization:${ws.id}`, materialHash: materialFingerprint({ wsId: ws.id, period: new Date().toISOString().slice(0, 7), rateBucket: Math.round(correctionRate * 10), }), }); } // Prune old logs (>30 days) to keep the .mind file lean optStore.pruneOlderThan(30); } } catch (err) { log.warn(`[cron] Prompt optimization failed: ${(err as Error).message}`); } break; } case 'agent_task': { // F9: Execute an agent task prompt in a workspace context const taskConfig = JSON.parse(schedule.job_config || '{}'); const taskPrompt = taskConfig.prompt as string | undefined; const taskWorkspace = schedule.workspace_id; if (!taskPrompt) { log.warn(`[cron] agent_task "${schedule.name}" has no prompt in job_config, skipping`); break; } try { // Determine target workspaces: "*" means all, otherwise single workspace const targetWorkspaces: Array<{ id: string; name: string }> = []; if (taskWorkspace === '*') { // F30: Global/cross-workspace — iterate all workspaces const allWs = wsManager.list(); for (const ws of allWs) { targetWorkspaces.push({ id: ws.id, name: ws.name }); } } else if (taskWorkspace) { const ws = wsManager.get(taskWorkspace); if (ws) { targetWorkspaces.push({ id: ws.id, name: ws.name }); } else { log.warn(`[cron] agent_task "${schedule.name}" references unknown workspace "${taskWorkspace}"`); } } if (targetWorkspaces.length === 0) { log.warn(`[cron] agent_task "${schedule.name}" has no valid target workspaces`); break; } // #17 ai_task daily cap: every firing is a full agent turn (real // LLM spend) — bound runaway schedules regardless of cron expr. if (taskConfig.mode === 'ai_task' && cronStore.countExecutionsToday(schedule.id) >= AI_TASK_DAILY_CAP) { log.warn(`[cron] ai_task "${schedule.name}" skipped — daily cap (${AI_TASK_DAILY_CAP}) reached`); break; } let aiTaskSucceeded = false; const targetErrors: unknown[] = []; for (const target of targetWorkspaces) { try { // Activate workspace mind so agent has context activateWorkspaceMindWithWeaver(target.id); if (taskConfig.mode === 'ai_task') { // #17: full agent turn through the loopback chat route — // tools, memory recall, governance, approval-gated writes // HELD (proposeHeld). origin:'automation' (#13) keeps the // scheduled turn out of memory write-back. Legacy rows // without mode keep the toolless completion path below. const turn = await runChannelChatTurn({ port: fullConfig.port ?? 3333, sessionToken: server.agentState.wsSessionToken, message: `[Scheduled task "${schedule.name}" for workspace "${target.name}"]\n\n${taskPrompt}`, workspace: target.id, session: `schedule-${schedule.id}`, proposeHeld: true, origin: 'automation', }); const output = turn.content || ''; if (!turn.error) aiTaskSucceeded = true; // Delivery: origin channel first (stamped from the trusted // turn-origin snapshot at create time), notification always. const deliverTo = taskConfig.deliverTo as { platform?: string; chatId?: string } | undefined; if (!turn.error && deliverTo?.platform && deliverTo?.chatId && output && isChannelPlatform(deliverTo.platform)) { const sent = await server.channelManager ?.sendTo(deliverTo.platform, deliverTo.chatId, `[${schedule.name}]\n${output}`) .catch((e: unknown) => { log.warn(`[cron] ai_task "${schedule.name}" channel delivery failed: ${e instanceof Error ? e.message : e}`); return false; }); if (!sent) log.warn(`[cron] ai_task "${schedule.name}" delivery skipped — ${deliverTo.platform} adapter not running`); } const summary = output.length > 200 ? output.slice(0, 197) + '...' : output; emitNotification(server, { title: `Scheduled task: ${schedule.name}`, body: turn.error ? `Error: ${turn.error}` : (summary || 'Task completed'), category: 'cron', actionUrl: `/workspaces/${target.id}`, }); if (turn.error) { throw new Error(`ai_task failed for workspace "${target.name}": ${turn.error}`); } else { log.info(`[cron] ai_task "${schedule.name}" completed for workspace "${target.name}" (${output.length} chars)`); } continue; } // Use the built-in Anthropic proxy to process the prompt const proxyUrl = `http://127.0.0.1:${fullConfig.port ?? 3333}/v1/chat/completions`; const response = await fetch(proxyUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: server.agentState.currentModel ?? 'claude-sonnet-4-6', max_tokens: 2048, messages: [ { role: 'user', content: `[Scheduled task for workspace "${target.name}"]\n\n${taskPrompt}`, }, ], }), signal: AbortSignal.timeout(120_000), }); if (response.ok) { const body = await response.json() as unknown; const output = parseOpenAiTextCompletion(body).content; const summary = output.length > 200 ? output.slice(0, 197) + '...' : output; emitNotification(server, { title: `Scheduled task: ${schedule.name}`, body: summary || 'Task completed', category: 'cron', actionUrl: `/workspaces/${target.id}`, }); log.info(`[cron] agent_task "${schedule.name}" completed for workspace "${target.name}" (${output.length} chars)`); } else { throw new Error(`agent_task LLM call failed: HTTP ${response.status}`); } } catch (wsErr) { log.warn(`[cron] agent_task "${schedule.name}" failed for workspace "${target.name}": ${(wsErr as Error).message}`); targetErrors.push(wsErr); } } if (targetErrors.length === 1) throw targetErrors[0]; if (targetErrors.length > 1) { throw new AggregateError(targetErrors, `agent_task failed in ${targetErrors.length} workspaces`); } // #17 once mode: one-shot ai_task disables itself after the first // successful run (row kept for execution history). if (taskConfig.mode === 'ai_task' && taskConfig.once === true && aiTaskSucceeded) { cronStore.update(schedule.id, { enabled: false }); log.info(`[cron] ai_task "${schedule.name}" was one-shot — schedule disabled after successful run`); } } catch (err) { log.warn(`[cron] agent_task handler failed: ${(err as Error).message}`); throw err; } break; } case 'connector_fetch': { // Auto-fetch (§3.C): pull fresh data from opted-in connectors into the // personal mind. Cost-free — raw frames, no LLM extraction. Free (Solo). try { new SessionStore(multiMind.personal).ensure('harvest', 'harvest', 'Imported memory from external sources'); const personalFrames = new FrameStore(multiMind.personal); const statePath = path.join(fullConfig.dataDir, 'connector-harvest-state.json'); const r = await runConnectorFetch({ connectors: connectorRegistry.getConnected(), writeFrame: (content) => { personalFrames.createIFrame('harvest', content, 'normal', 'import'); }, loadState: () => { try { const raw = JSON.parse(fs.readFileSync(statePath, 'utf-8')) as Partial<{ lastFetchedAt: string; hashes: Record }>; return { lastFetchedAt: raw.lastFetchedAt, hashes: raw.hashes ?? {} }; } catch { return { hashes: {} }; } }, saveState: (s) => { try { fs.writeFileSync(statePath, JSON.stringify(s)); } catch { /* best-effort */ } }, // Safety floor: at most one real sweep per ~20h even if the cron // expression is mis-set tight (auto-fetch is a daily cadence by design). minIntervalMs: 20 * 60 * 60 * 1000, log: (m) => log.info(`[cron] connector_fetch: ${m}`), }); if (r.skippedByFloor) { log.info('[cron] connector_fetch: within frequency floor — skipped'); } else { log.info(`[cron] connector_fetch: ${r.framesWritten} frames from ${r.connectorsFetched} connector(s); ${r.skippedUnchanged} unchanged, ${r.skippedUnsafe} unsafe, ${r.errors.length} error(s)`); } } catch (err) { log.warn(`[cron] connector_fetch failed: ${err instanceof Error ? err.message : String(err)}`); } break; } case 'monthly_assessment': { try { const assessment = generateMonthlyAssessment(fullConfig, multiMind.personal); saveAssessmentToMind(multiMind.personal, assessment); log.info(`[cron] Monthly assessment for ${assessment.period}: ${assessment.totalInteractions} interactions, ${(assessment.correctionRate * 100).toFixed(1)}% correction rate`); emitNotification(server, { title: 'Monthly agent assessment ready', body: `${assessment.period} report: ${assessment.totalInteractions} interactions, ${(assessment.correctionRate * 100).toFixed(1)}% correction rate — check your workspace home`, category: 'agent', actionUrl: '/', }, { // Anti-nag: one per period + counters. Same month with unchanged // counters is suppressed; a new month or changed numbers re-fires. dedupeKey: 'monthly_assessment', materialHash: materialFingerprint({ period: assessment.period, totalInteractions: assessment.totalInteractions, correctionRateBucket: Math.round(assessment.correctionRate * 100), }), }); } catch (err) { log.warn(`[cron] Monthly assessment failed: ${(err as Error).message}`); } break; } case 'loop': { // Loop v0 (L1, report-only): a stateful, memory-powered scheduled // automation. Resolve the target mind (a specific workspace, else the // personal mind), build a toolless completion against the local proxy, // and delegate to runLoopTick (recall → maker → checker → memory write). // Throws from the maker/checker LLM path are intentionally NOT caught // here so the scheduler records the failure and auto-disables after // repeated failures (recall/write failures are caught inside runLoopTick). const wsId = schedule.workspace_id; const mindDb = wsId && wsId !== '*' ? getWorkspaceMindDb(wsId) : multiMind.personal; if (!mindDb) { log.warn(`[cron] loop "${schedule.name}" references unknown workspace "${wsId}" — skipping`); break; } if (wsId && wsId !== '*') activateWorkspaceMindWithWeaver(wsId); const loopModel = server.agentState.currentModel ?? 'claude-sonnet-4-6'; const loopProxyUrl = `http://127.0.0.1:${fullConfig.port ?? 3333}/v1/chat/completions`; const loopChat = async (prompt: string, maxTokens = 2048): Promise => { const resp = await fetch(loopProxyUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: loopModel, max_tokens: maxTokens, messages: [{ role: 'user', content: prompt }], }), signal: AbortSignal.timeout(120_000), }); if (!resp.ok) throw new Error(`Loop LLM call failed: HTTP ${resp.status}`); const body = await resp.json() as unknown; return parseOpenAiTextCompletion(body).content; }; const loopResult = await runLoopTick({ schedule, mindDb, embedder, chat: loopChat, log }); if (!loopResult.skipped) { emitNotification(server, { title: `Loop: ${schedule.name}`, body: loopResult.summary || 'Loop ran', category: 'cron', actionUrl: wsId && wsId !== '*' ? `/workspaces/${wsId}` : '/', }); log.info(`[cron] loop "${schedule.name}" ran${loopResult.score !== undefined ? ` (score ${loopResult.score.toFixed(2)})` : ''}${loopResult.wrote ? ', wrote 1 frame' : ''}`); // L2 assist mode: hold the maker's proposed action for human approval. // enqueueHeldAction validates (allowlist + critical + injection) and // persists 'held'; nothing executes until a human approves. if (loopResult.proposedAction) { const enq = enqueueHeldAction(server, { workspaceId: wsId ?? null, source: `loop:${schedule.id}`, tool: loopResult.proposedAction.tool, args: loopResult.proposedAction.args, summary: loopResult.proposedAction.summary || undefined, }); log.info( 'refused' in enq ? `[cron] loop "${schedule.name}" proposed ${loopResult.proposedAction.tool} — refused (${enq.refused})` : `[cron] loop "${schedule.name}" proposed ${loopResult.proposedAction.tool} — held for approval (${enq.id})`, ); } } break; } } }, (schedule, result) => { // UX-Refactor Phase 3 (Journey 16): persist the execution outcome to // cron_execution_history. Until now recordExecution was never called, so // GET /api/cron/:id/history, /api/automations/:id/logs and the Home // overnight failure feed all read an empty table. Fires for scheduler // ticks AND manual triggers (executeJob); the C26 /api/automations/test // preview is validation-only and never reaches the executor. The // persistence closure is the shared makeRecordExecutionCallback (cron.ts) // so automations.test.ts exercises the SAME code, not a hand-copied mirror. persistCronHistory(schedule, result); // Q16:C — Emit notification after every cron job tick (success or failure) if (result.success) { emitNotification(server, { title: `${schedule.name || 'Scheduled task'} completed`, body: 'Scheduled task ran successfully.', category: 'cron', actionUrl: '/settings/mission-control', }); } else { emitNotification(server, { title: `${schedule.name || 'Scheduled task'} failed`, body: `Error: ${result.error ?? 'Unknown error'}`, category: 'cron', actionUrl: '/settings/mission-control', }); } // FR-2 §runtime · 2026-05-28 addictiveness audit. When the user // sets `outputChannel: 'telegram'` in a schedule's job_config, // push a one-line digest line to their Telegram so the cron // becomes a daily-driver hook for personas P6/P8/P10. Fire-and- // forget — the catch ensures a Telegram outage never crashes // the scheduler tick. try { const cfg = JSON.parse(schedule.job_config || '{}') as { outputChannel?: string }; if (cfg.outputChannel === 'telegram') { const name = schedule.name || 'Scheduled task'; const expr = schedule.cron_expr ? ` (${schedule.cron_expr})` : ''; const text = result.success ? `✓ Waggle: ${name}${expr} ran successfully.` : `✗ Waggle: ${name}${expr} failed — ${result.error ?? 'unknown error'}`; pushTelegramMessage(server, text) .then(r => { if (!r.ok) log.warn(`[cron→telegram] skipped: ${r.reason}`); }) .catch(err => log.warn(`[cron→telegram] push failed: ${err}`)); } } catch (err) { log.warn(`[cron→telegram] config parse failed: ${err}`); } }, notification => { emitNotification(server, { ...notification, category: 'cron', actionUrl: '/settings/mission-control', }); }, schedule => !cronScheduleHasReadOnlyViewerTarget(schedule, server)); scheduler.start(); server.decorate('scheduler', scheduler); // Self-evolution: idle-session watcher (CowAgent steal #3, v1). Default OFF via // /self-evolution.json; when enabled, it reviews idle sessions through // a restricted `session-reviewer` loopback turn whose only write (create_skill) // is HELD for human approval. Skipped under test to keep unit runs hermetic // (mirrors the ChannelManager VITEST guard below). const idleWatcher = new IdleSessionWatcher({ dataDir: fullConfig.dataDir, emitNotification: (event, options) => emitNotification(server, event, options), log: { info: (msg: string) => log.info(msg), warn: (msg: string) => log.warn(msg) }, runReviewTurn: async ({ sessionId, workspaceId }) => { const transcript = readRecentTranscript(fullConfig.dataDir, workspaceId, sessionId); if (!transcript) return { content: NOTHING_TO_DO }; const res = await runChannelChatTurn({ port: server.localConfig.port, sessionToken: server.agentState.wsSessionToken, message: buildReviewInstruction(sessionId, transcript), workspace: workspaceId, session: `evolve-${sessionId}`, persona: 'session-reviewer', proposeHeld: true, origin: 'automation', }); return { content: res.content, ...(res.error ? { error: res.error } : {}) }; }, }); server.decorate('idleWatcher', idleWatcher); if (!process.env.VITEST && process.env.NODE_ENV !== 'test') { idleWatcher.start(); } server.addHook('onClose', async () => { idleWatcher.stop(); }); // H-10 G1: Evolution service — autonomous self-evolution daemon. // Disabled by default; opt-in via WAGGLE_EVOLUTION_AUTO_ENABLED=1 so // existing deployments don't start burning Anthropic credits silently. // Even when enabled, the service only produces `proposed` runs — accept // still happens manually via the Memory → Evolution UI. const evolutionService = new EvolutionService( { traceStore, runStore: evolutionStore, getApiKey: () => server.vault?.get('anthropic')?.value ?? null, getActiveBehavioralSpec: () => (server as unknown as { activeBehavioralSpec: Record }).activeBehavioralSpec, log: (level, msg) => { if (level === 'warn') log.warn(`[evolution-service] ${msg}`); else log.info(`[evolution-service] ${msg}`); }, }, { tickIntervalMs: parseInt(process.env.WAGGLE_EVOLUTION_TICK_INTERVAL_MS ?? '', 10) || undefined, minTracesPerTarget: parseInt(process.env.WAGGLE_EVOLUTION_MIN_TRACES ?? '', 10) || undefined, }, ); server.decorate('evolutionService', evolutionService); if (isEvolutionAutoEnabled()) { evolutionService.start(); log.info('[evolution-service] autonomous evolution enabled (WAGGLE_EVOLUTION_AUTO_ENABLED=1)'); } // F2: Daily audit event cleanup (retain 90 days by default) const auditRetentionDays = parseInt(process.env.WAGGLE_AUDIT_RETENTION_DAYS ?? '90', 10); const auditCleanupTimer = setInterval(() => { const deleted = cleanupAuditEvents(fullConfig.dataDir, auditRetentionDays); if (deleted > 0) { log.info(`[audit] Cleaned up ${deleted} events older than ${auditRetentionDays} days`); } }, 24 * 60 * 60 * 1000); // once per day // PM-6: Offline manager — periodic LLM health checks const resolveOfflineManagerEndpoint = (): string => { if (!fullConfig.useBuiltInProxy) return fullConfig.litellmUrl; const address = server.server.address(); return address && typeof address === 'object' ? `http://127.0.0.1:${address.port}/v1` : fullConfig.litellmUrl; }; const offlineManager = new OfflineManager({ dataDir: fullConfig.dataDir, getLlmEndpoint: resolveOfflineManagerEndpoint, getLlmApiKey: () => server.agentState?.litellmApiKey ?? '', checkLlmReadiness: async (signal) => { const selectedModel = server.agentState.currentModel.trim(); const selectedOllama = selectedModel.toLowerCase().startsWith('ollama/'); const unselectedOllama = !selectedModel && server.agentState.llmProvider.provider === 'ollama'; if (selectedOllama || unselectedOllama) { const localModels = await listOllamaChatModelIds(signal); return selectedOllama ? localModels.some(model => model.toLowerCase() === selectedModel.toLowerCase()) : localModels.length > 0; } const endpoint = resolveOfflineManagerEndpoint().replace(/\/+$/, ''); const apiKey = server.agentState.litellmApiKey; const headers: Record = {}; if (apiKey) headers.Authorization = `Bearer ${apiKey}`; const response = await fetch(`${endpoint}/health/readiness`, { method: 'GET', headers, signal: AbortSignal.any([signal, AbortSignal.timeout(5_000)]), }); if (!response.ok) return false; if (server.agentState.llmProvider.provider !== 'litellm') return true; // LiteLLM readiness alone only proves the router process is accepting // traffic. Its local model catalog must also expose the selected route. const modelsResponse = await fetch(`${endpoint}/models`, { method: 'GET', headers, signal: AbortSignal.any([signal, AbortSignal.timeout(5_000)]), }); if (!modelsResponse.ok) return false; const modelsPayload = await modelsResponse.json() as { data?: Array<{ id?: string }>; }; const modelIds = (modelsPayload.data ?? []) .map(model => model.id) .filter((model): model is string => typeof model === 'string'); return selectedModel ? modelIds.includes(selectedModel) : modelIds.length > 0; }, eventBus, checkIntervalMs: 30_000, }); if (fullConfig.startOfflineManagerOnListen !== false) { server.addHook('onListen', async () => { offlineManager.start(); }); } server.addHook('preClose', async () => { await offlineManager.stop(); }); server.decorate('offlineManager', offlineManager); // Plugins // CORS restricted to known Tauri/dev origins — prevents cross-origin attacks from arbitrary websites await server.register(cors, { origin: (origin, cb) => { if (corsOriginAllowed(origin)) { cb(null, true); } else { cb(new Error('CORS: origin not allowed'), false); } }, }); await server.register(websocket); // Security middleware — headers + rate limiting + bearer auth (local server only) const rateLimiterMaxRequests = Number.parseInt(process.env.WAGGLE_RATE_LIMIT_MAX_REQUESTS ?? '', 10); const rateLimiterWindowMs = Number.parseInt(process.env.WAGGLE_RATE_LIMIT_WINDOW_MS ?? '', 10); const rateLimiterConfig = { ...(Number.isFinite(rateLimiterMaxRequests) && rateLimiterMaxRequests > 0 ? { maxRequests: rateLimiterMaxRequests } : {}), ...(Number.isFinite(rateLimiterWindowMs) && rateLimiterWindowMs > 0 ? { windowMs: rateLimiterWindowMs } : {}), }; await server.register(securityMiddleware, { sessionToken: server.agentState.wsSessionToken, authenticateBrowserCompanionToken: (token) => browserCompanionCredentialMatches( token, server.agentState.browserCompanionCredentialHash, ), authenticateRunToken: (token) => { const run = agentRunRegistry.authenticateCredential(token); if (!run) return false; return { runId: run.id, model: run.executor.model }; }, rateLimiter: Object.keys(rateLimiterConfig).length > 0 ? rateLimiterConfig : undefined, }); // D1: session-token bootstrap. Managed desktop launches require a random // per-launch credential delivered only through Tauri IPC. An ordinary browser // can forge a tauri.localhost Origin, so Origin alone is not an application // identity. Browser-only mode has no IPC channel and is instead bound to the // exact loopback request authority (host + port). server.get('/api/auth/session-token', async (request, reply) => { if (!isLoopbackBind()) { return reply.code(403).send({ error: 'Session bootstrap is available only on a loopback-bound sidecar.', code: 'SESSION_BOOTSTRAP_LOOPBACK_ONLY', }); } if (managedDesktopBootstrap) { if (!desktopBootstrapMatches( request.headers['x-waggle-desktop-bootstrap'], desktopBootstrapToken, )) { return reply.code(403).send({ error: 'Desktop session bootstrap requires the owned launch credential.', code: 'DESKTOP_BOOTSTRAP_REQUIRED', }); } } else if (!browserBootstrapAuthorityAllowed(request)) { return reply.code(403).send({ error: 'Session bootstrap origin does not match the local service authority.', code: 'SESSION_BOOTSTRAP_ORIGIN_MISMATCH', }); } reply.header('Cache-Control', 'no-store'); return { token: server.agentState.wsSessionToken }; }); // P5 (PDF 2026-04-17): minimal debug-log viewer for support attachments. // Returns health + last 500 audit events + recent cost entries as JSON. server.get('/api/debug/logs', async (request, reply) => { // R2-006: same-origin gate — this endpoint exposes audit history and is // recon material; never serve it cross-origin (defense in depth behind // the loopback bind). if (!isLocalRequest(request)) { return reply.code(403).send({ error: 'Forbidden: external origin' }); } const payload: Record = { timestamp: new Date().toISOString(), version: '0.2.0', platform: process.platform, }; try { payload.health = await server.inject({ method: 'GET', url: '/health' }).then(r => JSON.parse(r.payload)); } catch { /* non-blocking */ } try { const raw = (server.auditStore as unknown as { db?: { prepare: (sql: string) => { all: () => unknown[] } } })?.db; if (raw) { // R2-006: expose only a coarse, non-recon column subset (no capability // names/versions/sources, risk_level, approval_class, initiator or detail) // and cap the rows — the support bundle needs activity shape, not an inventory. payload.auditRecent = raw.prepare('SELECT timestamp, action, capability_type FROM install_audit ORDER BY timestamp DESC LIMIT 50').all(); } } catch { /* non-blocking */ } // R2-006: vault key NAMES dropped — even names are recon material and the // support bundle does not need them. reply.header('Content-Type', 'application/json; charset=utf-8'); reply.header('Content-Disposition', `attachment; filename="waggle-logs-${Date.now()}.json"`); return payload; }); // Routes await server.register(workspaceRoutes); await server.register(chatRoutes); await server.register(memoryRoutes); await server.register(memoryCenterRoutes); await server.register(settingsRoutes); await server.register(embeddingRoutes); await server.register(sessionRoutes); await server.register(knowledgeRoutes); await server.register(litellmRoutes); await server.register(ingestRoutes); await server.register(mindRoutes); await server.register(agentRoutes); await server.register(skillRoutes); // UX-Refactor Phase 3: /api/skills/:id aliases (S06/S19) — delegates into // skillRoutes handlers, so it registers right after them. await server.register(skillsAliasRoutes); await server.register(skillsHygieneRoutes); await server.register(skillsAuditRoutes); await server.register(approvalRoutes); await server.register(anthropicProxyRoutes); await server.register(teamRoutes); await server.register(taskRoutes); await server.register(capabilitiesRoutes); await server.register(toolsRoutes); await server.register(externalToolRunRoutes); await server.register(routeProposalRoutes); await server.register(waggleDanceRoutes); await server.register(commandRoutes); // UX-Refactor Phase 1: Home Cockpit (S01) + Command Center (S00/S03) surfaces. // commandCenterRoutes registers the singular /api/command/* aliases (B4) — it // is distinct from the plural commandRoutes (/api/commands/execute) above. await server.register(homeRoutes); await server.register(onboardingRoutes); await server.register(commandCenterRoutes); await server.register(cronRoutes); // UX-Refactor Phase 3: Agent entity (S09/S18, B3 agents.json) + the // /api/automations/* PRD-vocabulary alias over cron (S11/S20, B4). // agentEntityRoutes is distinct from agentRoutes (/api/agent/* + /api/agents/ // active) and agentGroupRoutes; the static /api/agents/active keeps precedence // over the new /:id param route (find-my-way prefers static segments). await server.register(agentEntityRoutes); await server.register(automationRoutes); await server.register(notificationRoutes); await server.register(marketplaceDevRoutes); await server.register(marketplaceRoutes); await server.register(createCapabilityProposalRoutes( capabilityProposalStore, (identity) => installMarketplaceApprovalIdentity(server, identity), )); await server.register(agentSearchRoutes); await server.register(connectorRoutes); // UX-Refactor Phase 4 (Extend layer): MCP Hub (S08) + the marketplace // bare-path alias / shared install-audit read (S21, C18). extendRoutes // delegates onto marketplaceRoutes via inject — register after it. await server.register(mcpRoutes); await server.register(extendRoutes); await server.register(fleetRoutes); await server.register(importRoutes); await server.register(vaultRoutes); await server.register(personaRoutes); await server.register(feedbackRoutes); await server.register(workspaceTemplateRoutes); await server.register(evolutionRoutes); await server.register(exportRoutes); await server.register(dataEraseRoutes); await server.register(costRoutes); await server.register(backupRoutes); await server.register(offlineRoutes); await server.register(weaverRoutes); await server.register(eventRoutes); await server.register(workflowRoutes); await server.register(pinRoutes); await server.register(documentRoutes); await server.register(artifactRoutes); await server.register(fileRoutes); await server.register(waggleSignalRoutes); await server.register(providerRoutes); await server.register(profileRoutes); await server.register(oauthRoutes); await server.register(browseRoutes); await server.register(browserExtRoutes); await server.register(telegramRoutes); // IM channel adapters (Slack/Telegram/WhatsApp/Discord) — see // docs/plans/CHANNELS-ARC-2026-07-09.md. Manager owns adapter lifecycle; // adapters run agent turns through loopback POST /api/chat. { const channelManager = new ChannelManager({ dataDir: server.localConfig.dataDir, port: server.localConfig.port, sessionToken: server.agentState.wsSessionToken, vault: { get: (key: string) => server.vault?.get(key) ?? null, has: (key: string) => server.vault?.has(key) ?? false, set: (key: string, value: string, metadata?: Record) => { if (!server.vault) throw new Error('Vault is unavailable'); server.vault.set(key, value, metadata); }, delete: (key: string) => server.vault?.delete(key) ?? false, }, log: { info: (msg: string) => server.log.info(msg), warn: (msg: string) => server.log.warn(msg), }, listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })), onAudit: (event) => emitAuditEvent(server, { workspaceId: 'default', eventType: event.type, input: JSON.stringify({ platform: event.platform, detail: event.detail ?? '' }), }), }); server.decorate('channelManager', channelManager); // Auto-start enabled channels once the server is up; skip under test to // keep unit runs hermetic (mirrors the teams-server VITEST guard below). if (!process.env.VITEST && process.env.NODE_ENV !== 'test') { server.addHook('onReady', async () => { await channelManager.startEnabled(); }); } server.addHook('onClose', async () => { await channelManager.stopAll(); }); } await server.register(channelRoutes); await server.register(dreamRoutes); await server.register(telemetryRoutes); await server.register(agentGroupRoutes); await server.register(localJobRoutes); await server.register(harvestRoutes); await server.register(wikiRoutes); await server.register(identityRoutes); await server.register(agentRunRoutes); await server.register(agentRunsRoutes); await server.register(localInferenceRoutes); await server.register(complianceRoutes); // ── Static file serving for web mode ────────────────────────── // When WAGGLE_FRONTEND_DIR is set, serve the React frontend as static files. // Otherwise probe likely build outputs. The canonical build (per root // package.json `build` script) is `/dist`, which apps/web/vite // writes to. `app/dist` is kept as a fallback for legacy Tauri-shell // builds but is no longer the primary source — if both exist, root dist wins. const frontendDir = process.env.WAGGLE_FRONTEND_DIR ?? [ // Root `dist/` — primary build target from `npm run build` path.resolve(process.cwd(), 'dist'), // from monorepo root path.resolve(process.cwd(), '..', '..', 'dist'), // from packages/server/ path.resolve(process.cwd(), '..', 'dist'), // from packages/ ...(typeof process.argv[1] === 'string' ? [path.resolve(path.dirname(process.argv[1]), '..', '..', '..', '..', 'dist')] : []), // Legacy `app/dist/` — Tauri shell build, kept as fallback path.resolve(process.cwd(), 'app', 'dist'), // from monorepo root path.resolve(process.cwd(), '..', '..', 'app', 'dist'), // from packages/server/ path.resolve(process.cwd(), '..', 'app', 'dist'), // from packages/ ...(typeof process.argv[1] === 'string' ? [path.resolve(path.dirname(process.argv[1]), '..', '..', '..', '..', 'app', 'dist')] : []), ].find(d => fs.existsSync(path.join(d, 'index.html'))) ?? path.resolve(process.cwd(), 'dist'); if (fs.existsSync(frontendDir) && fs.existsSync(path.join(frontendDir, 'index.html'))) { await server.register(fastifyStatic, { root: frontendDir, prefix: '/', wildcard: false, }); // SPA fallback: serve index.html for all non-API, non-static, non-asset routes server.setNotFoundHandler((request, reply) => { // Don't intercept API routes, WebSocket upgrades, or static assets if (request.url.startsWith('/api/') || request.url.startsWith('/v1/') || request.url.startsWith('/assets/') || request.url === '/health' || request.url === '/ws') { reply.code(404).send({ error: 'Not found' }); return; } reply.sendFile('index.html', frontendDir); }); } // WebSocket endpoint — event bus relay to frontend // 11A-6: Require session token for WebSocket authentication // 11A-8: Per-connection listener tracking (don't kill other clients on disconnect) server.get('/ws', { websocket: true }, (socket, request) => { // 11A-6: Validate session token from query param const url = new URL(request.url, 'http://localhost'); const token = url.searchParams.get('token'); if (token !== server.agentState.wsSessionToken) { socket.close(4001, 'Unauthorized: invalid session token'); return; } // Per-connection listener references for clean removal on disconnect const listenerMap = new Map void>(); // Forward approval events and agent events to the WebSocket client const eventTypes = ['approval_required', 'step', 'tool', 'done', 'error', 'presence_update', 'notification'] as const; for (const evt of eventTypes) { const handler = (data: unknown) => { try { socket.send(JSON.stringify({ event: evt, data })); } catch { // Client disconnected } }; listenerMap.set(evt, handler); eventBus.on(evt, handler); } socket.on('message', (raw: Buffer) => { try { const msg = JSON.parse(raw.toString()) as { type: string; requestId?: string; reason?: string }; if (msg.type === 'approve' && msg.requestId) { const pending = server.agentState.pendingApprovals.get(msg.requestId); if (pending) { server.agentState.pendingApprovals.delete(msg.requestId); pending.resolve(true); } } else if (msg.type === 'deny' && msg.requestId) { const pending = server.agentState.pendingApprovals.get(msg.requestId); if (pending) { server.agentState.pendingApprovals.delete(msg.requestId); pending.resolve(false); } } } catch { // Ignore malformed messages } }); socket.on('close', () => { // Remove only THIS connection's listeners, not all clients' for (const [evt, handler] of listenerMap) { eventBus.removeListener(evt, handler); } listenerMap.clear(); }); }); // P0-3: Cached API key validation — verify key actually works, not just exists let keyValidationCache: { valid: boolean; checkedAt: number; keyHash: string } | null = null; let keyValidationInFlight: Promise | null = null; const KEY_VALIDATION_TTL = 30 * 1000; // 30 seconds — short so vault updates are picked up quickly function hashKey(key: string): string { // Simple hash for cache invalidation — not cryptographic let h = 0; for (let i = 0; i < key.length; i++) { h = ((h << 5) - h + key.charCodeAt(i)) | 0; } return String(h); } function getAnthropicApiKey(): string { const vaultKey = server.vault?.get('anthropic')?.value; return vaultKey || process.env.ANTHROPIC_API_KEY || ''; } function freshAnthropicValidation(): boolean | null { const apiKey = getAnthropicApiKey(); if (!apiKey) return false; const currentHash = hashKey(apiKey); if ( keyValidationCache && keyValidationCache.keyHash === currentHash && Date.now() - keyValidationCache.checkedAt < KEY_VALIDATION_TTL ) { return keyValidationCache.valid; } return null; } function markAnthropicProxyDegraded(detail = 'Built-in Anthropic proxy (API key invalid or expired — update in Settings > API Keys)'): void { server.agentState.llmProvider.health = 'degraded'; server.agentState.llmProvider.detail = detail; } async function validateAnthropicKey(): Promise { // Read key from vault first, fall back to env var const apiKey = getAnthropicApiKey(); if (!apiKey) return false; const currentHash = hashKey(apiKey); // Return cached result if fresh AND key hasn't changed if ( keyValidationCache && keyValidationCache.keyHash === currentHash && Date.now() - keyValidationCache.checkedAt < KEY_VALIDATION_TTL ) { return keyValidationCache.valid; } try { // Lightweight API call to verify key works const res = await fetch('https://api.anthropic.com/v1/messages', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': apiKey, 'anthropic-version': '2023-06-01', }, body: JSON.stringify({ model: 'claude-haiku-4-5-20251001', max_tokens: 1, messages: [{ role: 'user', content: 'hi' }], }), signal: AbortSignal.timeout(5000), }); // 200 = valid, 401 = invalid/expired, 400 = valid key but bad request (still means key works) const valid = res.status !== 401 && res.status !== 403; keyValidationCache = { valid, checkedAt: Date.now(), keyHash: currentHash }; return valid; } catch { // Network error — don't cache failure, key might be fine return keyValidationCache?.valid ?? true; } } function validateAnthropicKeyInBackground(): void { if (keyValidationInFlight) return; keyValidationInFlight = validateAnthropicKey() .then((valid) => { if (!valid && server.agentState.llmProvider.provider === 'anthropic-proxy') { markAnthropicProxyDegraded(); } }) .finally(() => { keyValidationInFlight = null; }); keyValidationInFlight.catch(() => { /* fire-and-forget guard */ }); } // Expose cache invalidation for settings route to call after key update server._invalidateKeyValidationCache = () => { keyValidationCache = null; // If currently degraded due to bad key, reset to healthy for re-validation if (server.agentState.llmProvider.health === 'degraded') { server.agentState.llmProvider.health = 'healthy'; server.agentState.llmProvider.detail = 're-validating after key change'; } }; // IMP-15: API docs — auto-generated from Fastify route registry server.get('/api/docs', async () => { const routes: Array<{ method: string; url: string; prefix: string }> = []; // Fastify exposes registered routes via the internal routing tree, which is // not part of the public typings — read it through an `unknown[]` and narrow. const allRoutes: unknown[] = (server as { routes?: unknown[] }).routes ?? []; // Iterate printRoutes() style — Fastify 4.x stores routes differently // Use server.printRoutes() as a fallback reference try { // Fastify stores routes in server[Symbol.for('registered-routes')] or similar // Best approach: iterate after ready using routesByMethod for (const route of allRoutes) { if (route && typeof route === 'object' && 'url' in route && 'method' in route) { const r = route as { url: string; method: string | string[]; prefix?: string }; const methods = Array.isArray(r.method) ? r.method : [r.method]; for (const m of methods) { routes.push({ method: m, url: r.url, prefix: r.prefix ?? '' }); } } } } catch { /* fallback to empty */ } // If routes array is empty (Fastify internals differ), provide static list if (routes.length === 0) { return { openapi: '3.0.0', info: { title: 'Waggle API', version: '1.0.0', description: 'Workspace-native AI agent platform' }, note: 'Dynamic route listing unavailable. Use GET /health for status.', categories: { chat: ['POST /api/chat'], workspaces: ['GET /api/workspaces', 'POST /api/workspaces', 'GET /api/workspaces/:id', 'PUT /api/workspaces/:id', 'DELETE /api/workspaces/:id', 'GET /api/workspaces/:id/cost', 'GET /api/workspaces/:id/context', 'GET /api/workspaces/:id/files', 'GET /api/workspaces/:id/storage', 'GET /api/workspaces/:id/storage/files', 'GET /api/workspaces/:id/storage/read', 'POST /api/workspaces/:id/storage/write', 'DELETE /api/workspaces/:id/storage/delete'], memory: ['GET /api/memory/search', 'GET /api/memory/frames', 'POST /api/memory/frames', 'DELETE /api/memory/frames/:id', 'GET /api/memory/stats', 'GET /api/memory/graph'], sessions: ['GET /api/workspaces/:workspaceId/sessions', 'POST /api/workspaces/:workspaceId/sessions', 'PATCH /api/sessions/:sessionId', 'DELETE /api/sessions/:sessionId', 'GET /api/sessions/:sessionId/summary'], teams: ['GET /api/teams', 'POST /api/teams', 'GET /api/teams/:id', 'PUT /api/teams/:id', 'DELETE /api/teams/:id', 'POST /api/teams/:id/members', 'PUT /api/teams/:id/members/:userId', 'DELETE /api/teams/:id/members/:userId', 'GET /api/teams/:id/activity'], events: ['GET /api/events', 'GET /api/events/stats', 'GET /api/events/stream'], settings: ['GET /api/settings', 'PUT /api/settings', 'PATCH /api/settings', 'POST /api/settings/test-key', 'POST /api/settings/probe-provider'], vault: ['GET /api/vault', 'POST /api/vault', 'DELETE /api/vault/:name'], fleet: ['GET /api/fleet', 'POST /api/fleet/:workspaceId/pause', 'POST /api/fleet/:workspaceId/resume', 'POST /api/fleet/:workspaceId/kill'], cost: ['GET /api/cost/summary', 'GET /api/cost/by-workspace', 'GET /api/costs'], agent: ['GET /api/agent/status', 'GET /api/agent/model', 'GET /api/agent/cost'], marketplace: ['GET /api/marketplace/search', 'GET /api/marketplace/packs', 'POST /api/marketplace/install', 'POST /api/marketplace/uninstall'], connectors: ['GET /api/connectors', 'POST /api/connectors/:id/connect', 'POST /api/connectors/:id/disconnect'], other: ['GET /health', 'POST /api/export', 'POST /api/backup', 'POST /api/restore', 'GET /api/cron', 'POST /api/ingest', 'GET /api/personas', 'GET /api/workspace-templates'], }, }; } // Dynamic route listing const grouped: Record = {}; for (const r of routes) { if (!r.url.startsWith('/api/') && r.url !== '/health') continue; const category = r.url.split('/')[2] ?? 'other'; if (!grouped[category]) grouped[category] = []; grouped[category].push(`${r.method} ${r.url}`); } return { openapi: '3.0.0', info: { title: 'Waggle API', version: '1.0.0', description: 'Workspace-native AI agent platform' }, endpointCount: routes.filter(r => r.url.startsWith('/api/')).length, categories: grouped, }; }); // Embedding provider status + reprobe endpoints. (The set/write side — // POST /api/embedding/provider — lives in routes/embedding.ts.) server.get('/api/embedding/status', async () => { // Enriched shape: live status + persisted `configuredProvider` + `envOverride`. return buildEmbeddingStatusPayload(server); }); server.post('/api/embedding/reprobe', async () => { // Re-read API keys from Vault (user may have just added one) try { const voyageEntry = vault.get('voyage-api-key'); if (voyageEntry?.value) embeddingConfig.voyage = { apiKey: voyageEntry.value }; } catch { /* skip */ } try { const openaiEntry = vault.get('openai'); if (openaiEntry?.value) embeddingConfig.openai = { apiKey: openaiEntry.value }; } catch { /* skip */ } return embeddingProvider.reprobe(); }); // Health check — truthful, not optimistic server.get('/health', async () => { const llm = { ...server.agentState.llmProvider }; const listeningAddress = server.server.address(); const servicePort = typeof listeningAddress === 'object' && listeningAddress ? listeningAddress.port : server.localConfig.port; // P0-3: If provider is anthropic-proxy and claims healthy, validate the key actually works if ( llm.provider === 'anthropic-proxy' && llm.health === 'healthy' && llm.detail.startsWith('Built-in Anthropic proxy') ) { const keyValid = freshAnthropicValidation(); if (keyValid === false) { llm.health = 'degraded'; llm.detail = 'Built-in Anthropic proxy (API key invalid or expired — update in Settings > API Keys)'; markAnthropicProxyDegraded(llm.detail); } else if (keyValid === null) { validateAnthropicKeyInBackground(); } } const dbHealthy = (() => { try { multiMind.personal.getDatabase().prepare('SELECT 1').get(); return true; } catch { return false; } })(); // Startup may resolve a provider after OfflineManager's initial probe. // Once a later completion-readiness probe runs, it becomes authoritative. const offlineState = offlineManager.state; const providerCheckedAt = Date.parse(llm.checkedAt); const readinessCheckedAt = Date.parse(offlineManager.lastCheck); const providerStatusIsNewer = Number.isFinite(providerCheckedAt) && providerCheckedAt > readinessCheckedAt; const llmReachable = providerStatusIsNewer ? llm.health === 'healthy' : !offlineState.offline; const effectiveOfflineState = { ...offlineState, offline: !llmReachable, since: llmReachable ? null : offlineState.since, }; const overallStatus = llmReachable && llm.health === 'healthy' && dbHealthy ? 'ok' : llm.health === 'unavailable' || !dbHealthy ? 'unavailable' : 'degraded'; // Memory stats — frame count + mind file size const memoryStats = (() => { try { const db = multiMind.personal.getDatabase(); const row = db.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number } | undefined; const frameCount = row?.cnt ?? 0; // Get mind file size from the known personal mind path let mindSizeBytes = 0; try { const stat = fs.statSync(personalPath); mindSizeBytes = stat.size; } catch { /* file may not exist yet */ } // Embedding coverage — frames with vectors vs total frames let embeddingCoverage = 0; try { const totalRow = db.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }; const vecRow = db.prepare('SELECT COUNT(*) as cnt FROM memory_frames_vec').get() as { cnt: number } | undefined; const vecCount = vecRow?.cnt ?? 0; if (totalRow.cnt > 0) { embeddingCoverage = Math.round((vecCount / totalRow.cnt) * 100); } } catch { /* vec table may not exist */ } const embeddingProviderStatus = server.embeddingProvider?.getStatus?.() ?? null; return { frameCount, mindSizeBytes, embeddingCoverage, embeddingProvider: embeddingProviderStatus }; } catch { return { frameCount: 0, mindSizeBytes: 0, embeddingCoverage: 0, embeddingProvider: null }; } })(); // Service health — watchdog and notification SSE status const serviceHealth = { watchdogRunning: scheduler.isRunning(), notificationSSEActive: eventBus.listenerCount('notification') > 0, }; return { status: overallStatus, mode: 'local', instanceId: server.localConfig.instanceId ?? null, port: servicePort, timestamp: new Date().toISOString(), llm: { provider: llm.provider, health: llm.health, detail: llm.detail, checkedAt: llm.checkedAt, reachable: llmReachable, lastCheck: new Date().toISOString(), }, database: { healthy: dbHealthy }, memoryStats, serviceHealth, defaultModel: server.agentState.currentModel, offline: effectiveOfflineState, // R1-001: wsToken intentionally NOT returned — /health is unauthenticated // and the token authenticates every other route. Localhost clients are // trusted by security-middleware and never needed it. }; }); // Cleanup on close server.addHook('onClose', async () => { // Stop cron scheduler localJobStore.close(); agentRunRegistry.close(); stopMarketplaceBackgroundSync(); scheduler.stop(); evolutionService.stop(); await offlineManager.stop(); clearInterval(auditCleanupTimer); // Stop MCP servers await mcpRuntime.stopAll().catch(() => {}); // Stop weaver timers for (const t of weaverTimers) clearInterval(t); for (const [, ww] of workspaceWeavers) { for (const t of ww.timers) clearInterval(t); } workspaceWeavers.clear(); // Close all workspace sessions (new concurrent model) sessionManager.closeAll(); // Let the active bounded pass finish before closing any borrowed mind. await vectorEnrichmentService.stop(); // Close all cached workspace minds mindCache.closeAll(); multiMind.close(); // Close audit DB, teams DB, and telemetry closeAuditDb(); closeTeamsDb(); try { telemetry.close(); } catch { /* already closed */ } // Close marketplace DB if (marketplaceDb) { try { marketplaceDb.close(); } catch { /* already closed */ } } }); vectorEnrichmentService.start(); // ── Start Teams server if PostgreSQL is configured (TEAMS tier) ── // Skip in test environment to avoid port conflicts and addHook-after-listen errors if (process.env.DATABASE_URL && !process.env.VITEST && process.env.NODE_ENV !== 'test') { // DAY0V-03 / F-02: cloud-mode detected (DATABASE_URL set) — require CLERK_SECRET_KEY. // The Teams server registers wsGateway, which itself hard-fails in NODE_ENV=production // when the key is absent (see packages/server/src/ws/gateway.ts DAY0V-01 fix). We surface // the misconfiguration here too so the operator sees a single clear log line instead of // a buildServer rejection further down. if (!process.env.CLERK_SECRET_KEY) { log.error( '[teams-server] CLERK_SECRET_KEY missing while DATABASE_URL is set. Cloud/Teams ' + 'deployment requires both. Skipping Teams server startup — set CLERK_SECRET_KEY ' + 'and restart, or unset DATABASE_URL to run desktop-only.', ); } else { const teamsPort = parseInt(process.env.TEAMS_SERVER_PORT ?? '3101', 10); import('../index.js').then(({ buildServer }) => { buildServer().then(teamsServer => { teamsServer.listen({ port: teamsPort, host: '127.0.0.1' }, (err) => { if (err) { log.info(`[teams-server] Failed to start: ${err.message}`); } else { log.info(`[teams-server] Running at http://127.0.0.1:${teamsPort}`); } }); server.addHook('onClose', async () => { await teamsServer.close(); }); }).catch(err => log.info(`[teams-server] Build failed: ${(err as Error).message}`)); }).catch(() => { /* Teams server optional — non-blocking */ }); } } return server; }