@claude-flow/cli 3.32.3 → 3.32.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/helpers/helpers.manifest.json +2 -2
- package/catalog-manifest.json +2 -2
- package/dist/src/agenticow/speculative-exploration.d.ts +148 -0
- package/dist/src/agenticow/speculative-exploration.js +218 -0
- package/dist/src/appliance/gguf-engine.d.ts +91 -0
- package/dist/src/appliance/gguf-engine.js +425 -0
- package/dist/src/appliance/ruvllm-bridge.d.ts +102 -0
- package/dist/src/appliance/ruvllm-bridge.js +292 -0
- package/dist/src/appliance/rvfa-builder.d.ts +44 -0
- package/dist/src/appliance/rvfa-builder.js +329 -0
- package/dist/src/appliance/rvfa-distribution.d.ts +97 -0
- package/dist/src/appliance/rvfa-distribution.js +370 -0
- package/dist/src/appliance/rvfa-format.d.ts +111 -0
- package/dist/src/appliance/rvfa-format.js +393 -0
- package/dist/src/appliance/rvfa-runner.d.ts +69 -0
- package/dist/src/appliance/rvfa-runner.js +237 -0
- package/dist/src/appliance/rvfa-signing.d.ts +123 -0
- package/dist/src/appliance/rvfa-signing.js +347 -0
- package/dist/src/auth/client.d.ts +89 -0
- package/dist/src/auth/client.js +242 -0
- package/dist/src/auth/constants.d.ts +7 -0
- package/dist/src/auth/constants.js +7 -0
- package/dist/src/auth/scopes.d.ts +14 -0
- package/dist/src/auth/scopes.js +21 -0
- package/dist/src/auth/security-bridge.d.ts +36 -0
- package/dist/src/auth/security-bridge.js +42 -0
- package/dist/src/auth/session.d.ts +20 -0
- package/dist/src/auth/session.js +32 -0
- package/dist/src/auth/state.d.ts +19 -0
- package/dist/src/auth/state.js +53 -0
- package/dist/src/auth/types.d.ts +27 -0
- package/dist/src/auth/types.js +11 -0
- package/dist/src/autopilot-state.d.ts +77 -0
- package/dist/src/autopilot-state.js +271 -0
- package/dist/src/benchmarks/gaia-agent-planning.smoke.d.ts +18 -0
- package/dist/src/benchmarks/gaia-agent-planning.smoke.js +253 -0
- package/dist/src/benchmarks/gaia-agent.d.ts +198 -0
- package/dist/src/benchmarks/gaia-agent.js +651 -0
- package/dist/src/benchmarks/gaia-causal-memory.d.ts +133 -0
- package/dist/src/benchmarks/gaia-causal-memory.js +281 -0
- package/dist/src/benchmarks/gaia-causal-memory.smoke.d.ts +22 -0
- package/dist/src/benchmarks/gaia-causal-memory.smoke.js +300 -0
- package/dist/src/benchmarks/gaia-convergence.d.ts +138 -0
- package/dist/src/benchmarks/gaia-convergence.js +260 -0
- package/dist/src/benchmarks/gaia-convergence.smoke.d.ts +19 -0
- package/dist/src/benchmarks/gaia-convergence.smoke.js +246 -0
- package/dist/src/benchmarks/gaia-critic.d.ts +123 -0
- package/dist/src/benchmarks/gaia-critic.js +312 -0
- package/dist/src/benchmarks/gaia-critic.smoke.d.ts +21 -0
- package/dist/src/benchmarks/gaia-critic.smoke.js +327 -0
- package/dist/src/benchmarks/gaia-decomposer.d.ts +125 -0
- package/dist/src/benchmarks/gaia-decomposer.js +350 -0
- package/dist/src/benchmarks/gaia-decomposer.smoke.d.ts +21 -0
- package/dist/src/benchmarks/gaia-decomposer.smoke.js +228 -0
- package/dist/src/benchmarks/gaia-e2e-smoke.d.ts +27 -0
- package/dist/src/benchmarks/gaia-e2e-smoke.js +136 -0
- package/dist/src/benchmarks/gaia-extract.smoke.d.ts +45 -0
- package/dist/src/benchmarks/gaia-extract.smoke.js +242 -0
- package/dist/src/benchmarks/gaia-hardness/features.d.ts +46 -0
- package/dist/src/benchmarks/gaia-hardness/features.js +170 -0
- package/dist/src/benchmarks/gaia-hardness/predictor.d.ts +105 -0
- package/dist/src/benchmarks/gaia-hardness/predictor.js +260 -0
- package/dist/src/benchmarks/gaia-hardness/predictor.smoke.d.ts +20 -0
- package/dist/src/benchmarks/gaia-hardness/predictor.smoke.js +235 -0
- package/dist/src/benchmarks/gaia-hardness/train-data-loader.d.ts +51 -0
- package/dist/src/benchmarks/gaia-hardness/train-data-loader.js +179 -0
- package/dist/src/benchmarks/gaia-judge.d.ts +88 -0
- package/dist/src/benchmarks/gaia-judge.js +437 -0
- package/dist/src/benchmarks/gaia-loader.d.ts +87 -0
- package/dist/src/benchmarks/gaia-loader.js +326 -0
- package/dist/src/benchmarks/gaia-tools/file_read.d.ts +35 -0
- package/dist/src/benchmarks/gaia-tools/file_read.js +403 -0
- package/dist/src/benchmarks/gaia-tools/grounded_query.d.ts +126 -0
- package/dist/src/benchmarks/gaia-tools/grounded_query.js +225 -0
- package/dist/src/benchmarks/gaia-tools/index.d.ts +32 -0
- package/dist/src/benchmarks/gaia-tools/index.js +36 -0
- package/dist/src/benchmarks/gaia-tools/types.d.ts +62 -0
- package/dist/src/benchmarks/gaia-tools/types.js +12 -0
- package/dist/src/benchmarks/gaia-tools/web_search.d.ts +30 -0
- package/dist/src/benchmarks/gaia-tools/web_search.js +210 -0
- package/dist/src/benchmarks/gaia-voting.d.ts +88 -0
- package/dist/src/benchmarks/gaia-voting.js +297 -0
- package/dist/src/benchmarks/gaia-voting.smoke.d.ts +20 -0
- package/dist/src/benchmarks/gaia-voting.smoke.js +332 -0
- package/dist/src/benchmarks/pretrain/index.d.ts +58 -0
- package/dist/src/benchmarks/pretrain/index.js +404 -0
- package/dist/src/business-pods/bbs-budget-tracker.d.ts +139 -0
- package/dist/src/business-pods/bbs-budget-tracker.js +358 -0
- package/dist/src/business-pods/domain-affinity-policy.d.ts +47 -0
- package/dist/src/business-pods/domain-affinity-policy.js +65 -0
- package/dist/src/business-pods/pod-schema.d.ts +96 -0
- package/dist/src/business-pods/pod-schema.js +225 -0
- package/dist/src/commands/advisor.d.ts +15 -0
- package/dist/src/commands/advisor.js +94 -0
- package/dist/src/commands/agent-wasm.d.ts +14 -0
- package/dist/src/commands/agent-wasm.js +333 -0
- package/dist/src/commands/agent.d.ts +8 -0
- package/dist/src/commands/agent.js +927 -0
- package/dist/src/commands/analyze.d.ts +19 -0
- package/dist/src/commands/analyze.js +2047 -0
- package/dist/src/commands/announcements.d.ts +17 -0
- package/dist/src/commands/announcements.js +0 -0
- package/dist/src/commands/appliance-advanced.d.ts +9 -0
- package/dist/src/commands/appliance-advanced.js +215 -0
- package/dist/src/commands/appliance.d.ts +8 -0
- package/dist/src/commands/appliance.js +404 -0
- package/dist/src/commands/auth.d.ts +15 -0
- package/dist/src/commands/auth.js +244 -0
- package/dist/src/commands/autopilot.d.ts +15 -0
- package/dist/src/commands/autopilot.js +407 -0
- package/dist/src/commands/benchmark.d.ts +10 -0
- package/dist/src/commands/benchmark.js +460 -0
- package/dist/src/commands/claims.d.ts +10 -0
- package/dist/src/commands/claims.js +620 -0
- package/dist/src/commands/cleanup.d.ts +13 -0
- package/dist/src/commands/cleanup.js +250 -0
- package/dist/src/commands/completions.d.ts +10 -0
- package/dist/src/commands/completions.js +539 -0
- package/dist/src/commands/config.d.ts +8 -0
- package/dist/src/commands/config.js +428 -0
- package/dist/src/commands/daemon.d.ts +59 -0
- package/dist/src/commands/daemon.js +1733 -0
- package/dist/src/commands/deployment.d.ts +10 -0
- package/dist/src/commands/deployment.js +672 -0
- package/dist/src/commands/doctor.d.ts +10 -0
- package/dist/src/commands/doctor.js +1902 -0
- package/dist/src/commands/eject.d.ts +33 -0
- package/dist/src/commands/eject.js +195 -0
- package/dist/src/commands/embeddings.d.ts +18 -0
- package/dist/src/commands/embeddings.js +1623 -0
- package/dist/src/commands/funnel.d.ts +18 -0
- package/dist/src/commands/funnel.js +302 -0
- package/dist/src/commands/gaia-bench.d.ts +40 -0
- package/dist/src/commands/gaia-bench.js +597 -0
- package/dist/src/commands/guidance.d.ts +8 -0
- package/dist/src/commands/guidance.js +556 -0
- package/dist/src/commands/hive-mind.d.ts +11 -0
- package/dist/src/commands/hive-mind.js +1319 -0
- package/dist/src/commands/hooks.d.ts +8 -0
- package/dist/src/commands/hooks.js +4606 -0
- package/dist/src/commands/index.d.ts +118 -0
- package/dist/src/commands/index.js +366 -0
- package/dist/src/commands/init.d.ts +13 -0
- package/dist/src/commands/init.js +1297 -0
- package/dist/src/commands/issues.d.ts +21 -0
- package/dist/src/commands/issues.js +567 -0
- package/dist/src/commands/mcp.d.ts +11 -0
- package/dist/src/commands/mcp.js +732 -0
- package/dist/src/commands/memory-backup.d.ts +11 -0
- package/dist/src/commands/memory-backup.js +46 -0
- package/dist/src/commands/memory-distill.d.ts +27 -0
- package/dist/src/commands/memory-distill.js +374 -0
- package/dist/src/commands/memory.d.ts +8 -0
- package/dist/src/commands/memory.js +1596 -0
- package/dist/src/commands/metaharness.d.ts +39 -0
- package/dist/src/commands/metaharness.js +215 -0
- package/dist/src/commands/migrate.d.ts +8 -0
- package/dist/src/commands/migrate.js +742 -0
- package/dist/src/commands/neural.d.ts +10 -0
- package/dist/src/commands/neural.js +4331 -0
- package/dist/src/commands/performance.d.ts +10 -0
- package/dist/src/commands/performance.js +583 -0
- package/dist/src/commands/plugins.d.ts +11 -0
- package/dist/src/commands/plugins.js +826 -0
- package/dist/src/commands/process.d.ts +10 -0
- package/dist/src/commands/process.js +694 -0
- package/dist/src/commands/progress.d.ts +11 -0
- package/dist/src/commands/progress.js +259 -0
- package/dist/src/commands/providers.d.ts +10 -0
- package/dist/src/commands/providers.js +502 -0
- package/dist/src/commands/proxy-lifecycle.d.ts +20 -0
- package/dist/src/commands/proxy-lifecycle.js +277 -0
- package/dist/src/commands/proxy.d.ts +21 -0
- package/dist/src/commands/proxy.js +403 -0
- package/dist/src/commands/route.d.ts +16 -0
- package/dist/src/commands/route.js +822 -0
- package/dist/src/commands/ruvector/backup.d.ts +11 -0
- package/dist/src/commands/ruvector/backup.js +747 -0
- package/dist/src/commands/ruvector/benchmark.d.ts +11 -0
- package/dist/src/commands/ruvector/benchmark.js +490 -0
- package/dist/src/commands/ruvector/import.d.ts +18 -0
- package/dist/src/commands/ruvector/import.js +373 -0
- package/dist/src/commands/ruvector/index.d.ts +29 -0
- package/dist/src/commands/ruvector/index.js +129 -0
- package/dist/src/commands/ruvector/init.d.ts +11 -0
- package/dist/src/commands/ruvector/init.js +467 -0
- package/dist/src/commands/ruvector/migrate.d.ts +11 -0
- package/dist/src/commands/ruvector/migrate.js +498 -0
- package/dist/src/commands/ruvector/optimize.d.ts +11 -0
- package/dist/src/commands/ruvector/optimize.js +505 -0
- package/dist/src/commands/ruvector/pg-utils.d.ts +14 -0
- package/dist/src/commands/ruvector/pg-utils.js +41 -0
- package/dist/src/commands/ruvector/setup.d.ts +18 -0
- package/dist/src/commands/ruvector/setup.js +765 -0
- package/dist/src/commands/ruvector/status.d.ts +11 -0
- package/dist/src/commands/ruvector/status.js +479 -0
- package/dist/src/commands/security.d.ts +10 -0
- package/dist/src/commands/security.js +1012 -0
- package/dist/src/commands/session.d.ts +8 -0
- package/dist/src/commands/session.js +757 -0
- package/dist/src/commands/settings.d.ts +19 -0
- package/dist/src/commands/settings.js +180 -0
- package/dist/src/commands/spinner.d.ts +16 -0
- package/dist/src/commands/spinner.js +329 -0
- package/dist/src/commands/start.d.ts +8 -0
- package/dist/src/commands/start.js +418 -0
- package/dist/src/commands/status.d.ts +8 -0
- package/dist/src/commands/status.js +608 -0
- package/dist/src/commands/swarm.d.ts +8 -0
- package/dist/src/commands/swarm.js +891 -0
- package/dist/src/commands/task.d.ts +8 -0
- package/dist/src/commands/task.js +675 -0
- package/dist/src/commands/transfer-store.d.ts +13 -0
- package/dist/src/commands/transfer-store.js +428 -0
- package/dist/src/commands/update.d.ts +8 -0
- package/dist/src/commands/update.js +276 -0
- package/dist/src/commands/verify.d.ts +19 -0
- package/dist/src/commands/verify.js +261 -0
- package/dist/src/commands/version.d.ts +42 -0
- package/dist/src/commands/version.js +106 -0
- package/dist/src/commands/workflow.d.ts +8 -0
- package/dist/src/commands/workflow.js +617 -0
- package/dist/src/config/harness-feedback-applier.d.ts +50 -0
- package/dist/src/config/harness-feedback-applier.js +122 -0
- package/dist/src/config/proven-config-refresh.d.ts +39 -0
- package/dist/src/config/proven-config-refresh.js +154 -0
- package/dist/src/config/proven-config-rvfa.d.ts +23 -0
- package/dist/src/config/proven-config-rvfa.js +73 -0
- package/dist/src/config/proven-config.d.ts +86 -0
- package/dist/src/config/proven-config.js +176 -0
- package/dist/src/config-adapter.d.ts +15 -0
- package/dist/src/config-adapter.js +186 -0
- package/dist/src/encryption/vault.d.ts +94 -0
- package/dist/src/encryption/vault.js +172 -0
- package/dist/src/fs-secure.d.ts +86 -0
- package/dist/src/fs-secure.js +133 -0
- package/dist/src/funnel/advisor-tip.d.ts +58 -0
- package/dist/src/funnel/advisor-tip.js +92 -0
- package/dist/src/funnel/attribution.d.ts +37 -0
- package/dist/src/funnel/attribution.js +101 -0
- package/dist/src/funnel/consent.d.ts +22 -0
- package/dist/src/funnel/consent.js +58 -0
- package/dist/src/funnel/credit-errors.d.ts +31 -0
- package/dist/src/funnel/credit-errors.js +88 -0
- package/dist/src/funnel/credit-notifier.d.ts +44 -0
- package/dist/src/funnel/credit-notifier.js +74 -0
- package/dist/src/funnel/disclosure.d.ts +47 -0
- package/dist/src/funnel/disclosure.js +109 -0
- package/dist/src/funnel/enrollment.d.ts +36 -0
- package/dist/src/funnel/enrollment.js +64 -0
- package/dist/src/funnel/environment.d.ts +17 -0
- package/dist/src/funnel/environment.js +39 -0
- package/dist/src/funnel/event-transport.d.ts +51 -0
- package/dist/src/funnel/event-transport.js +199 -0
- package/dist/src/funnel/events.d.ts +42 -0
- package/dist/src/funnel/events.js +150 -0
- package/dist/src/funnel/index.d.ts +21 -0
- package/dist/src/funnel/index.js +21 -0
- package/dist/src/funnel/insights.d.ts +50 -0
- package/dist/src/funnel/insights.js +120 -0
- package/dist/src/funnel/local-signals.d.ts +20 -0
- package/dist/src/funnel/local-signals.js +77 -0
- package/dist/src/funnel/message-transport.d.ts +51 -0
- package/dist/src/funnel/message-transport.js +149 -0
- package/dist/src/funnel/messages.d.ts +55 -0
- package/dist/src/funnel/messages.js +160 -0
- package/dist/src/funnel/payout.d.ts +40 -0
- package/dist/src/funnel/payout.js +60 -0
- package/dist/src/funnel/power-saver-notifier.d.ts +44 -0
- package/dist/src/funnel/power-saver-notifier.js +92 -0
- package/dist/src/funnel/precedence.d.ts +16 -0
- package/dist/src/funnel/precedence.js +85 -0
- package/dist/src/funnel/promo.d.ts +41 -0
- package/dist/src/funnel/promo.js +144 -0
- package/dist/src/funnel/rate-limit-notifier.d.ts +55 -0
- package/dist/src/funnel/rate-limit-notifier.js +102 -0
- package/dist/src/funnel/rotation.d.ts +19 -0
- package/dist/src/funnel/rotation.js +70 -0
- package/dist/src/funnel/state.d.ts +13 -0
- package/dist/src/funnel/state.js +52 -0
- package/dist/src/funnel/toggle-cooldown.d.ts +17 -0
- package/dist/src/funnel/toggle-cooldown.js +32 -0
- package/dist/src/funnel/types.d.ts +98 -0
- package/dist/src/funnel/types.js +26 -0
- package/dist/src/index.d.ts +81 -0
- package/dist/src/index.js +609 -0
- package/dist/src/infrastructure/in-memory-repositories.d.ts +68 -0
- package/dist/src/infrastructure/in-memory-repositories.js +264 -0
- package/dist/src/init/claudemd-generator.d.ts +16 -0
- package/dist/src/init/claudemd-generator.js +368 -0
- package/dist/src/init/executor.d.ts +41 -0
- package/dist/src/init/executor.js +2142 -0
- package/dist/src/init/helper-refresh.d.ts +79 -0
- package/dist/src/init/helper-refresh.js +347 -0
- package/dist/src/init/helper-signing.d.ts +37 -0
- package/dist/src/init/helper-signing.js +67 -0
- package/dist/src/init/helpers-generator.d.ts +87 -0
- package/dist/src/init/helpers-generator.js +1456 -0
- package/dist/src/init/index.d.ts +13 -0
- package/dist/src/init/index.js +15 -0
- package/dist/src/init/mcp-generator.d.ts +26 -0
- package/dist/src/init/mcp-generator.js +126 -0
- package/dist/src/init/memory-package-resolver.d.ts +53 -0
- package/dist/src/init/memory-package-resolver.js +118 -0
- package/dist/src/init/settings-generator.d.ts +14 -0
- package/dist/src/init/settings-generator.js +462 -0
- package/dist/src/init/statusline-generator.d.ts +28 -0
- package/dist/src/init/statusline-generator.js +177 -0
- package/dist/src/init/types.d.ts +331 -0
- package/dist/src/init/types.js +276 -0
- package/dist/src/log-filters.d.ts +46 -0
- package/dist/src/log-filters.js +107 -0
- package/dist/src/mcp-client.d.ts +92 -0
- package/dist/src/mcp-client.js +397 -0
- package/dist/src/mcp-server.d.ts +163 -0
- package/dist/src/mcp-server.js +751 -0
- package/dist/src/mcp-tools/agent-execute-core.d.ts +115 -0
- package/dist/src/mcp-tools/agent-execute-core.js +587 -0
- package/dist/src/mcp-tools/agent-tools.d.ts +9 -0
- package/dist/src/mcp-tools/agent-tools.js +837 -0
- package/dist/src/mcp-tools/agentbbs-tools.d.ts +28 -0
- package/dist/src/mcp-tools/agentbbs-tools.js +394 -0
- package/dist/src/mcp-tools/agentdb-tools.d.ts +35 -0
- package/dist/src/mcp-tools/agentdb-tools.js +1473 -0
- package/dist/src/mcp-tools/agenticow-loader.d.ts +59 -0
- package/dist/src/mcp-tools/agenticow-loader.js +105 -0
- package/dist/src/mcp-tools/agenticow-speculate-tools.d.ts +24 -0
- package/dist/src/mcp-tools/agenticow-speculate-tools.js +209 -0
- package/dist/src/mcp-tools/agenticow-tools.d.ts +36 -0
- package/dist/src/mcp-tools/agenticow-tools.js +360 -0
- package/dist/src/mcp-tools/analyze-tools.d.ts +38 -0
- package/dist/src/mcp-tools/analyze-tools.js +346 -0
- package/dist/src/mcp-tools/auto-install.d.ts +83 -0
- package/dist/src/mcp-tools/auto-install.js +131 -0
- package/dist/src/mcp-tools/autopilot-tools.d.ts +12 -0
- package/dist/src/mcp-tools/autopilot-tools.js +231 -0
- package/dist/src/mcp-tools/browser-intent-tools.d.ts +162 -0
- package/dist/src/mcp-tools/browser-intent-tools.js +548 -0
- package/dist/src/mcp-tools/browser-session-tools.d.ts +27 -0
- package/dist/src/mcp-tools/browser-session-tools.js +398 -0
- package/dist/src/mcp-tools/browser-tools.d.ts +21 -0
- package/dist/src/mcp-tools/browser-tools.js +760 -0
- package/dist/src/mcp-tools/business-pod-tools.d.ts +20 -0
- package/dist/src/mcp-tools/business-pod-tools.js +169 -0
- package/dist/src/mcp-tools/claims-tools.d.ts +12 -0
- package/dist/src/mcp-tools/claims-tools.js +863 -0
- package/dist/src/mcp-tools/config-tools.d.ts +8 -0
- package/dist/src/mcp-tools/config-tools.js +411 -0
- package/dist/src/mcp-tools/coordination-tools.d.ts +13 -0
- package/dist/src/mcp-tools/coordination-tools.js +729 -0
- package/dist/src/mcp-tools/daa-tools.d.ts +13 -0
- package/dist/src/mcp-tools/daa-tools.js +534 -0
- package/dist/src/mcp-tools/embeddings-tools.d.ts +9 -0
- package/dist/src/mcp-tools/embeddings-tools.js +904 -0
- package/dist/src/mcp-tools/github-tools.d.ts +9 -0
- package/dist/src/mcp-tools/github-tools.js +659 -0
- package/dist/src/mcp-tools/guidance-tools.d.ts +15 -0
- package/dist/src/mcp-tools/guidance-tools.js +639 -0
- package/dist/src/mcp-tools/hive-mind-tools.d.ts +8 -0
- package/dist/src/mcp-tools/hive-mind-tools.js +953 -0
- package/dist/src/mcp-tools/hooks-tools.d.ts +65 -0
- package/dist/src/mcp-tools/hooks-tools.js +4836 -0
- package/dist/src/mcp-tools/http-fetch-tools.d.ts +55 -0
- package/dist/src/mcp-tools/http-fetch-tools.js +329 -0
- package/dist/src/mcp-tools/index.d.ts +34 -0
- package/dist/src/mcp-tools/index.js +40 -0
- package/dist/src/mcp-tools/managed-agent-tools.d.ts +22 -0
- package/dist/src/mcp-tools/managed-agent-tools.js +357 -0
- package/dist/src/mcp-tools/memory-tools.d.ts +14 -0
- package/dist/src/mcp-tools/memory-tools.js +1330 -0
- package/dist/src/mcp-tools/metaharness-tools.d.ts +51 -0
- package/dist/src/mcp-tools/metaharness-tools.js +684 -0
- package/dist/src/mcp-tools/neural-tools.d.ts +54 -0
- package/dist/src/mcp-tools/neural-tools.js +1168 -0
- package/dist/src/mcp-tools/performance-tools.d.ts +16 -0
- package/dist/src/mcp-tools/performance-tools.js +675 -0
- package/dist/src/mcp-tools/progress-tools.d.ts +14 -0
- package/dist/src/mcp-tools/progress-tools.js +348 -0
- package/dist/src/mcp-tools/request-tracker.d.ts +17 -0
- package/dist/src/mcp-tools/request-tracker.js +27 -0
- package/dist/src/mcp-tools/ruvllm-tools.d.ts +9 -0
- package/dist/src/mcp-tools/ruvllm-tools.js +355 -0
- package/dist/src/mcp-tools/security-tools.d.ts +18 -0
- package/dist/src/mcp-tools/security-tools.js +556 -0
- package/dist/src/mcp-tools/session-tools.d.ts +8 -0
- package/dist/src/mcp-tools/session-tools.js +517 -0
- package/dist/src/mcp-tools/swarm-tools.d.ts +37 -0
- package/dist/src/mcp-tools/swarm-tools.js +390 -0
- package/dist/src/mcp-tools/system-tools.d.ts +13 -0
- package/dist/src/mcp-tools/system-tools.js +688 -0
- package/dist/src/mcp-tools/task-tools.d.ts +8 -0
- package/dist/src/mcp-tools/task-tools.js +487 -0
- package/dist/src/mcp-tools/terminal-tools.d.ts +8 -0
- package/dist/src/mcp-tools/terminal-tools.js +306 -0
- package/dist/src/mcp-tools/testgen-tools.d.ts +26 -0
- package/dist/src/mcp-tools/testgen-tools.js +168 -0
- package/dist/src/mcp-tools/tool-loop-guardrail.d.ts +31 -0
- package/dist/src/mcp-tools/tool-loop-guardrail.js +71 -0
- package/dist/src/mcp-tools/transfer-tools.d.ts +14 -0
- package/dist/src/mcp-tools/transfer-tools.js +447 -0
- package/dist/src/mcp-tools/types.d.ts +8 -0
- package/dist/src/mcp-tools/types.js +8 -0
- package/dist/src/mcp-tools/validate-input.d.ts +9 -0
- package/dist/src/mcp-tools/validate-input.js +9 -0
- package/dist/src/mcp-tools/wasm-agent-tools.d.ts +13 -0
- package/dist/src/mcp-tools/wasm-agent-tools.js +840 -0
- package/dist/src/mcp-tools/workflow-tools.d.ts +8 -0
- package/dist/src/mcp-tools/workflow-tools.js +884 -0
- package/dist/src/memory/bge-embedder.d.ts +25 -0
- package/dist/src/memory/bge-embedder.js +121 -0
- package/dist/src/memory/cross-encoder-rerank.d.ts +33 -0
- package/dist/src/memory/cross-encoder-rerank.js +123 -0
- package/dist/src/memory/embedding-policy.d.ts +21 -0
- package/dist/src/memory/embedding-policy.js +30 -0
- package/dist/src/memory/embedding-quantization.d.ts +62 -0
- package/dist/src/memory/embedding-quantization.js +156 -0
- package/dist/src/memory/ewc-consolidation.d.ts +305 -0
- package/dist/src/memory/ewc-consolidation.js +611 -0
- package/dist/src/memory/graph-edge-writer.d.ts +95 -0
- package/dist/src/memory/graph-edge-writer.js +217 -0
- package/dist/src/memory/hybrid-retrieval.d.ts +77 -0
- package/dist/src/memory/hybrid-retrieval.js +192 -0
- package/dist/src/memory/intelligence.d.ts +405 -0
- package/dist/src/memory/intelligence.js +1316 -0
- package/dist/src/memory/lucene-bm25.d.ts +19 -0
- package/dist/src/memory/lucene-bm25.js +308 -0
- package/dist/src/memory/memory-bridge.d.ts +537 -0
- package/dist/src/memory/memory-bridge.js +2460 -0
- package/dist/src/memory/memory-initializer.d.ts +556 -0
- package/dist/src/memory/memory-initializer.js +3021 -0
- package/dist/src/memory/neural-package-bridge.d.ts +48 -0
- package/dist/src/memory/neural-package-bridge.js +87 -0
- package/dist/src/memory/rabitq-index.d.ts +60 -0
- package/dist/src/memory/rabitq-index.js +242 -0
- package/dist/src/memory/sona-optimizer.d.ts +267 -0
- package/dist/src/memory/sona-optimizer.js +779 -0
- package/dist/src/memory/structured-distill.d.ts +48 -0
- package/dist/src/memory/structured-distill.js +125 -0
- package/dist/src/output.d.ts +9 -0
- package/dist/src/output.js +9 -0
- package/dist/src/parser.d.ts +89 -0
- package/dist/src/parser.js +516 -0
- package/dist/src/plugins/manager.d.ts +133 -0
- package/dist/src/plugins/manager.js +415 -0
- package/dist/src/plugins/store/discovery.d.ts +99 -0
- package/dist/src/plugins/store/discovery.js +1224 -0
- package/dist/src/plugins/store/index.d.ts +76 -0
- package/dist/src/plugins/store/index.js +141 -0
- package/dist/src/plugins/store/search.d.ts +46 -0
- package/dist/src/plugins/store/search.js +230 -0
- package/dist/src/plugins/store/types.d.ts +279 -0
- package/dist/src/plugins/store/types.js +7 -0
- package/dist/src/plugins/tests/demo-plugin-store.d.ts +7 -0
- package/dist/src/plugins/tests/demo-plugin-store.js +126 -0
- package/dist/src/plugins/tests/standalone-test.d.ts +12 -0
- package/dist/src/plugins/tests/standalone-test.js +188 -0
- package/dist/src/plugins/tests/test-plugin-store.d.ts +7 -0
- package/dist/src/plugins/tests/test-plugin-store.js +206 -0
- package/dist/src/production/circuit-breaker.d.ts +101 -0
- package/dist/src/production/circuit-breaker.js +241 -0
- package/dist/src/production/error-handler.d.ts +92 -0
- package/dist/src/production/error-handler.js +299 -0
- package/dist/src/production/index.d.ts +23 -0
- package/dist/src/production/index.js +18 -0
- package/dist/src/production/monitoring.d.ts +161 -0
- package/dist/src/production/monitoring.js +356 -0
- package/dist/src/production/rate-limiter.d.ts +80 -0
- package/dist/src/production/rate-limiter.js +201 -0
- package/dist/src/production/retry.d.ts +48 -0
- package/dist/src/production/retry.js +179 -0
- package/dist/src/prompt.d.ts +44 -0
- package/dist/src/prompt.js +501 -0
- package/dist/src/proxy/install.d.ts +29 -0
- package/dist/src/proxy/install.js +135 -0
- package/dist/src/proxy/lifecycle.d.ts +61 -0
- package/dist/src/proxy/lifecycle.js +249 -0
- package/dist/src/proxy/paths.d.ts +34 -0
- package/dist/src/proxy/paths.js +70 -0
- package/dist/src/proxy/release.d.ts +47 -0
- package/dist/src/proxy/release.js +138 -0
- package/dist/src/proxy/token-bridge.d.ts +5 -0
- package/dist/src/proxy/token-bridge.js +61 -0
- package/dist/src/proxy/verify.d.ts +44 -0
- package/dist/src/proxy/verify.js +68 -0
- package/dist/src/runtime/headless.d.ts +60 -0
- package/dist/src/runtime/headless.js +284 -0
- package/dist/src/runtime/parent-death-watchdog.d.ts +42 -0
- package/dist/src/runtime/parent-death-watchdog.js +70 -0
- package/dist/src/ruvector/agent-wasm.d.ts +228 -0
- package/dist/src/ruvector/agent-wasm.js +463 -0
- package/dist/src/ruvector/ast-analyzer.d.ts +67 -0
- package/dist/src/ruvector/ast-analyzer.js +277 -0
- package/dist/src/ruvector/codemods/engine.d.ts +45 -0
- package/dist/src/ruvector/codemods/engine.js +291 -0
- package/dist/src/ruvector/codemods/scope-analysis.d.ts +29 -0
- package/dist/src/ruvector/codemods/scope-analysis.js +162 -0
- package/dist/src/ruvector/coverage-router.d.ts +160 -0
- package/dist/src/ruvector/coverage-router.js +531 -0
- package/dist/src/ruvector/coverage-tools.d.ts +33 -0
- package/dist/src/ruvector/coverage-tools.js +157 -0
- package/dist/src/ruvector/diff-classifier.d.ts +175 -0
- package/dist/src/ruvector/diff-classifier.js +699 -0
- package/dist/src/ruvector/diskann-backend.d.ts +78 -0
- package/dist/src/ruvector/diskann-backend.js +310 -0
- package/dist/src/ruvector/enhanced-model-router.d.ts +172 -0
- package/dist/src/ruvector/enhanced-model-router.js +577 -0
- package/dist/src/ruvector/graph-analyzer.d.ts +187 -0
- package/dist/src/ruvector/graph-analyzer.js +929 -0
- package/dist/src/ruvector/graph-backend.d.ts +79 -0
- package/dist/src/ruvector/graph-backend.js +220 -0
- package/dist/src/ruvector/index.d.ts +38 -0
- package/dist/src/ruvector/index.js +86 -0
- package/dist/src/ruvector/lora-adapter.d.ts +292 -0
- package/dist/src/ruvector/lora-adapter.js +710 -0
- package/dist/src/ruvector/model-prices.d.ts +50 -0
- package/dist/src/ruvector/model-prices.js +72 -0
- package/dist/src/ruvector/model-router.d.ts +408 -0
- package/dist/src/ruvector/model-router.js +1162 -0
- package/dist/src/ruvector/neural-router.d.ts +182 -0
- package/dist/src/ruvector/neural-router.js +825 -0
- package/dist/src/ruvector/output-verifier.d.ts +83 -0
- package/dist/src/ruvector/output-verifier.js +277 -0
- package/dist/src/ruvector/q-learning-router.d.ts +227 -0
- package/dist/src/ruvector/q-learning-router.js +721 -0
- package/dist/src/ruvector/router-calibrator.d.ts +65 -0
- package/dist/src/ruvector/router-calibrator.js +120 -0
- package/dist/src/ruvector/router-parallel-recorder.d.ts +127 -0
- package/dist/src/ruvector/router-parallel-recorder.js +183 -0
- package/dist/src/ruvector/router-trajectory.d.ts +194 -0
- package/dist/src/ruvector/router-trajectory.js +281 -0
- package/dist/src/ruvector/run-transcript-recorder.d.ts +154 -0
- package/dist/src/ruvector/run-transcript-recorder.js +209 -0
- package/dist/src/ruvector/ruvllm-wasm.d.ts +179 -0
- package/dist/src/ruvector/ruvllm-wasm.js +379 -0
- package/dist/src/ruvector/semantic-router.d.ts +77 -0
- package/dist/src/ruvector/semantic-router.js +178 -0
- package/dist/src/ruvector/task-embedder.d.ts +56 -0
- package/dist/src/ruvector/task-embedder.js +237 -0
- package/dist/src/ruvector/trajectory-tree.d.ts +113 -0
- package/dist/src/ruvector/trajectory-tree.js +237 -0
- package/dist/src/ruvector/vector-db.d.ts +73 -0
- package/dist/src/ruvector/vector-db.js +301 -0
- package/dist/src/ruvector/wasm-embedder.d.ts +13 -0
- package/dist/src/ruvector/wasm-embedder.js +143 -0
- package/dist/src/security/builtin-aidefence.d.ts +34 -0
- package/dist/src/security/builtin-aidefence.js +86 -0
- package/dist/src/services/agentic-flow-bridge.d.ts +50 -0
- package/dist/src/services/agentic-flow-bridge.js +95 -0
- package/dist/src/services/ai-job-dedup.d.ts +61 -0
- package/dist/src/services/ai-job-dedup.js +136 -0
- package/dist/src/services/checkpoint-gate.d.ts +140 -0
- package/dist/src/services/checkpoint-gate.js +223 -0
- package/dist/src/services/claim-service.d.ts +204 -0
- package/dist/src/services/claim-service.js +818 -0
- package/dist/src/services/config-file-manager.d.ts +37 -0
- package/dist/src/services/config-file-manager.js +224 -0
- package/dist/src/services/container-worker-pool.d.ts +204 -0
- package/dist/src/services/container-worker-pool.js +589 -0
- package/dist/src/services/daemon-autostart.d.ts +17 -0
- package/dist/src/services/daemon-autostart.js +102 -0
- package/dist/src/services/distill-oracle.d.ts +190 -0
- package/dist/src/services/distill-oracle.js +349 -0
- package/dist/src/services/distill-tuning.d.ts +111 -0
- package/dist/src/services/distill-tuning.js +510 -0
- package/dist/src/services/evolve-proof.d.ts +253 -0
- package/dist/src/services/evolve-proof.js +343 -0
- package/dist/src/services/fable-harness.d.ts +208 -0
- package/dist/src/services/fable-harness.js +388 -0
- package/dist/src/services/git-workspace-identity.d.ts +42 -0
- package/dist/src/services/git-workspace-identity.js +99 -0
- package/dist/src/services/global-ai-budget.d.ts +135 -0
- package/dist/src/services/global-ai-budget.js +415 -0
- package/dist/src/services/harness-benchmark.d.ts +68 -0
- package/dist/src/services/harness-benchmark.js +94 -0
- package/dist/src/services/harness-canary.d.ts +60 -0
- package/dist/src/services/harness-canary.js +69 -0
- package/dist/src/services/harness-corpus-harvester.d.ts +51 -0
- package/dist/src/services/harness-corpus-harvester.js +74 -0
- package/dist/src/services/harness-flywheel-generations.d.ts +115 -0
- package/dist/src/services/harness-flywheel-generations.js +360 -0
- package/dist/src/services/harness-flywheel-runtime.d.ts +25 -0
- package/dist/src/services/harness-flywheel-runtime.js +92 -0
- package/dist/src/services/harness-flywheel.d.ts +48 -0
- package/dist/src/services/harness-flywheel.js +207 -0
- package/dist/src/services/harness-frozen-eval.d.ts +22 -0
- package/dist/src/services/harness-frozen-eval.js +66 -0
- package/dist/src/services/harness-hosts.d.ts +40 -0
- package/dist/src/services/harness-hosts.js +88 -0
- package/dist/src/services/harness-improvement-ledger.d.ts +63 -0
- package/dist/src/services/harness-improvement-ledger.js +101 -0
- package/dist/src/services/harness-loop.d.ts +53 -0
- package/dist/src/services/harness-loop.js +85 -0
- package/dist/src/services/harness-qualification.d.ts +66 -0
- package/dist/src/services/harness-qualification.js +143 -0
- package/dist/src/services/harness-replay.d.ts +37 -0
- package/dist/src/services/harness-replay.js +92 -0
- package/dist/src/services/harness-verify.d.ts +33 -0
- package/dist/src/services/harness-verify.js +26 -0
- package/dist/src/services/harness-worker.d.ts +23 -0
- package/dist/src/services/harness-worker.js +66 -0
- package/dist/src/services/headless-worker-executor.d.ts +358 -0
- package/dist/src/services/headless-worker-executor.js +1269 -0
- package/dist/src/services/index.d.ts +13 -0
- package/dist/src/services/index.js +11 -0
- package/dist/src/services/memory-backup.d.ts +53 -0
- package/dist/src/services/memory-backup.js +198 -0
- package/dist/src/services/memory-distillation.d.ts +41 -0
- package/dist/src/services/memory-distillation.js +277 -0
- package/dist/src/services/native-training.d.ts +68 -0
- package/dist/src/services/native-training.js +141 -0
- package/dist/src/services/registry-api.d.ts +58 -0
- package/dist/src/services/registry-api.js +146 -0
- package/dist/src/services/repo-supervisor.d.ts +70 -0
- package/dist/src/services/repo-supervisor.js +228 -0
- package/dist/src/services/ruvector-training.d.ts +222 -0
- package/dist/src/services/ruvector-training.js +693 -0
- package/dist/src/services/swarm-memory-branches.d.ts +135 -0
- package/dist/src/services/swarm-memory-branches.js +213 -0
- package/dist/src/services/weight-eft.d.ts +305 -0
- package/dist/src/services/weight-eft.js +296 -0
- package/dist/src/services/worker-daemon.d.ts +439 -0
- package/dist/src/services/worker-daemon.js +1865 -0
- package/dist/src/services/worker-queue.d.ts +194 -0
- package/dist/src/services/worker-queue.js +513 -0
- package/dist/src/services/workspace-lease.d.ts +55 -0
- package/dist/src/services/workspace-lease.js +191 -0
- package/dist/src/suggest.d.ts +53 -0
- package/dist/src/suggest.js +200 -0
- package/dist/src/transfer/anonymization/index.d.ts +25 -0
- package/dist/src/transfer/anonymization/index.js +175 -0
- package/dist/src/transfer/deploy-seraphine.d.ts +13 -0
- package/dist/src/transfer/deploy-seraphine.js +205 -0
- package/dist/src/transfer/export.d.ts +25 -0
- package/dist/src/transfer/export.js +113 -0
- package/dist/src/transfer/index.d.ts +12 -0
- package/dist/src/transfer/index.js +31 -0
- package/dist/src/transfer/ipfs/client.d.ts +109 -0
- package/dist/src/transfer/ipfs/client.js +307 -0
- package/dist/src/transfer/ipfs/upload.d.ts +95 -0
- package/dist/src/transfer/ipfs/upload.js +413 -0
- package/dist/src/transfer/models/seraphine.d.ts +72 -0
- package/dist/src/transfer/models/seraphine.js +373 -0
- package/dist/src/transfer/serialization/cfp.d.ts +49 -0
- package/dist/src/transfer/serialization/cfp.js +183 -0
- package/dist/src/transfer/storage/gcs.d.ts +82 -0
- package/dist/src/transfer/storage/gcs.js +272 -0
- package/dist/src/transfer/storage/index.d.ts +6 -0
- package/dist/src/transfer/storage/index.js +6 -0
- package/dist/src/transfer/store/discovery.d.ts +84 -0
- package/dist/src/transfer/store/discovery.js +382 -0
- package/dist/src/transfer/store/download.d.ts +70 -0
- package/dist/src/transfer/store/download.js +334 -0
- package/dist/src/transfer/store/index.d.ts +84 -0
- package/dist/src/transfer/store/index.js +153 -0
- package/dist/src/transfer/store/publish.d.ts +76 -0
- package/dist/src/transfer/store/publish.js +294 -0
- package/dist/src/transfer/store/registry.d.ts +58 -0
- package/dist/src/transfer/store/registry.js +285 -0
- package/dist/src/transfer/store/search.d.ts +54 -0
- package/dist/src/transfer/store/search.js +232 -0
- package/dist/src/transfer/store/tests/standalone-test.d.ts +12 -0
- package/dist/src/transfer/store/tests/standalone-test.js +190 -0
- package/dist/src/transfer/store/types.d.ts +193 -0
- package/dist/src/transfer/store/types.js +6 -0
- package/dist/src/transfer/test-seraphine.d.ts +6 -0
- package/dist/src/transfer/test-seraphine.js +105 -0
- package/dist/src/transfer/tests/test-store.d.ts +7 -0
- package/dist/src/transfer/tests/test-store.js +214 -0
- package/dist/src/transfer/types.d.ts +245 -0
- package/dist/src/transfer/types.js +6 -0
- package/dist/src/types.d.ts +13 -0
- package/dist/src/types.js +13 -0
- package/dist/src/update/checker.d.ts +34 -0
- package/dist/src/update/checker.js +191 -0
- package/dist/src/update/executor.d.ts +33 -0
- package/dist/src/update/executor.js +217 -0
- package/dist/src/update/index.d.ts +33 -0
- package/dist/src/update/index.js +64 -0
- package/dist/src/update/rate-limiter.d.ts +20 -0
- package/dist/src/update/rate-limiter.js +96 -0
- package/dist/src/update/validator.d.ts +17 -0
- package/dist/src/update/validator.js +123 -0
- package/package.json +1 -1
|
@@ -0,0 +1,3021 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Memory Initializer
|
|
3
|
+
* Properly initializes the memory database with sql.js (WASM SQLite)
|
|
4
|
+
* Includes pattern tables, vector embeddings, migration state tracking
|
|
5
|
+
*
|
|
6
|
+
* ADR-053: Routes through ControllerRegistry → AgentDB v3 when available,
|
|
7
|
+
* falls back to raw sql.js for backwards compatibility.
|
|
8
|
+
*
|
|
9
|
+
* @module v3/cli/memory-initializer
|
|
10
|
+
*/
|
|
11
|
+
import * as fs from 'fs';
|
|
12
|
+
import * as path from 'path';
|
|
13
|
+
import { createRequire } from 'node:module';
|
|
14
|
+
import { readFileMaybeEncrypted, writeFileAtomic, writeFileRestricted } from '../fs-secure.js';
|
|
15
|
+
import { restoreMemoryDbFromBackup } from '../services/memory-backup.js';
|
|
16
|
+
/**
|
|
17
|
+
* #2356 — cached, synchronous capability probe for @ruvector/core. `getHNSWStatus`
|
|
18
|
+
* is sync and is called by `neural status` in a fresh process that never warms
|
|
19
|
+
* the lazy HNSW singleton, so reporting availability off the warm singleton
|
|
20
|
+
* alone produced a false "Not loaded — @ruvector/core not available" even when
|
|
21
|
+
* the package is installed and exposes VectorDb. Resolving the module (without
|
|
22
|
+
* importing/initializing it) is a faithful, cheap availability signal.
|
|
23
|
+
*/
|
|
24
|
+
let _ruvectorCoreResolvable;
|
|
25
|
+
function isRuvectorCoreResolvable() {
|
|
26
|
+
if (_ruvectorCoreResolvable !== undefined)
|
|
27
|
+
return _ruvectorCoreResolvable;
|
|
28
|
+
try {
|
|
29
|
+
const req = createRequire(import.meta.url);
|
|
30
|
+
req.resolve('@ruvector/core');
|
|
31
|
+
_ruvectorCoreResolvable = true;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
_ruvectorCoreResolvable = false;
|
|
35
|
+
}
|
|
36
|
+
return _ruvectorCoreResolvable;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* #1854: previously every site that needed the memory directory hardcoded
|
|
40
|
+
* `getMemoryRoot()`, so the documented config entry
|
|
41
|
+
* points (`memory.persistPath` config field, `memory configure --path`,
|
|
42
|
+
* `CLAUDE_FLOW_MEMORY_PATH` env var) all silently no-op'd. This helper
|
|
43
|
+
* is the single source of truth — every `.swarm/memory.db` resolution in
|
|
44
|
+
* this file flows through it.
|
|
45
|
+
*
|
|
46
|
+
* Precedence (highest → lowest):
|
|
47
|
+
* 1. CLAUDE_FLOW_MEMORY_PATH env var
|
|
48
|
+
* 2. memory.persistPath / memory.path in claude-flow.config.json (cwd or
|
|
49
|
+
* the directory the CLI was invoked from)
|
|
50
|
+
* 3. Default: cwd/.swarm
|
|
51
|
+
*
|
|
52
|
+
* Cached per-process so repeated lookups are cheap; reset only by spawning
|
|
53
|
+
* a fresh process (which is how config changes already propagate).
|
|
54
|
+
*/
|
|
55
|
+
let _memoryRootCache;
|
|
56
|
+
export function getMemoryRoot() {
|
|
57
|
+
if (_memoryRootCache !== undefined)
|
|
58
|
+
return _memoryRootCache;
|
|
59
|
+
// 1. Env var
|
|
60
|
+
const envPath = process.env.CLAUDE_FLOW_MEMORY_PATH;
|
|
61
|
+
if (envPath && envPath.trim().length > 0) {
|
|
62
|
+
_memoryRootCache = path.resolve(envPath);
|
|
63
|
+
return _memoryRootCache;
|
|
64
|
+
}
|
|
65
|
+
// 2. Config file (claude-flow.config.json)
|
|
66
|
+
const configCandidates = [
|
|
67
|
+
path.resolve(process.cwd(), 'claude-flow.config.json'),
|
|
68
|
+
path.resolve(process.cwd(), '.claude-flow', 'config.json'),
|
|
69
|
+
];
|
|
70
|
+
for (const configPath of configCandidates) {
|
|
71
|
+
if (!fs.existsSync(configPath))
|
|
72
|
+
continue;
|
|
73
|
+
try {
|
|
74
|
+
const raw = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
75
|
+
const fromConfig = raw?.memory?.persistPath ?? raw?.memory?.path;
|
|
76
|
+
if (typeof fromConfig === 'string' && fromConfig.trim().length > 0) {
|
|
77
|
+
_memoryRootCache = path.resolve(fromConfig);
|
|
78
|
+
return _memoryRootCache;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
/* malformed config — fall through to default */
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// 3. Default
|
|
86
|
+
_memoryRootCache = path.resolve(process.cwd(), '.swarm');
|
|
87
|
+
return _memoryRootCache;
|
|
88
|
+
}
|
|
89
|
+
/** For tests + the `memory configure` flow that mutates the config at runtime. */
|
|
90
|
+
export function _resetMemoryRootCache() {
|
|
91
|
+
_memoryRootCache = undefined;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* #2105: Resolve the full path to the SQLite memory database.
|
|
95
|
+
* Precedence (highest to lowest):
|
|
96
|
+
* 1. cliFlag - explicit --path flag passed by a subcommand
|
|
97
|
+
* 2. CLAUDE_FLOW_DB_PATH - full file-path override (new in #2105)
|
|
98
|
+
* 3. getMemoryRoot()/memory.db - directory from CLAUDE_FLOW_MEMORY_PATH /
|
|
99
|
+
* config / default cwd/.swarm
|
|
100
|
+
*/
|
|
101
|
+
export function resolveDbPath(cliFlag) {
|
|
102
|
+
if (cliFlag && cliFlag.trim().length > 0) {
|
|
103
|
+
return path.resolve(cliFlag);
|
|
104
|
+
}
|
|
105
|
+
const envDb = process.env.CLAUDE_FLOW_DB_PATH;
|
|
106
|
+
if (envDb && envDb.trim().length > 0) {
|
|
107
|
+
return path.resolve(envDb);
|
|
108
|
+
}
|
|
109
|
+
return path.join(getMemoryRoot(), 'memory.db');
|
|
110
|
+
}
|
|
111
|
+
// ADR-053: Lazy import of AgentDB v3 bridge
|
|
112
|
+
let _bridge;
|
|
113
|
+
async function getBridge() {
|
|
114
|
+
// #2120 — Allow callers to force the raw sql.js fallback path. The
|
|
115
|
+
// ensureSchemaColumns backfill (NULL → 'active') lives in that
|
|
116
|
+
// fallback, so smokes that verify legacy-DB migration semantics need a
|
|
117
|
+
// way to bypass the bridge. Also useful when the bridge would hang on
|
|
118
|
+
// network-bound init (Xenova model fetch) in offline CI.
|
|
119
|
+
if (process.env.CLAUDE_FLOW_DISABLE_BRIDGE === '1')
|
|
120
|
+
return null;
|
|
121
|
+
if (_bridge === null)
|
|
122
|
+
return null;
|
|
123
|
+
if (_bridge)
|
|
124
|
+
return _bridge;
|
|
125
|
+
try {
|
|
126
|
+
_bridge = await import('./memory-bridge.js');
|
|
127
|
+
return _bridge;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
_bridge = null;
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Enhanced schema with pattern confidence, temporal decay, versioning
|
|
136
|
+
* Vector embeddings enabled for semantic search
|
|
137
|
+
*/
|
|
138
|
+
export const MEMORY_SCHEMA_V3 = `
|
|
139
|
+
-- RuFlo V3 Memory Database
|
|
140
|
+
-- Version: 3.0.0
|
|
141
|
+
-- Features: Pattern learning, vector embeddings, temporal decay, migration tracking
|
|
142
|
+
|
|
143
|
+
PRAGMA journal_mode = WAL;
|
|
144
|
+
PRAGMA synchronous = NORMAL;
|
|
145
|
+
PRAGMA foreign_keys = ON;
|
|
146
|
+
|
|
147
|
+
-- ============================================
|
|
148
|
+
-- CORE MEMORY TABLES
|
|
149
|
+
-- ============================================
|
|
150
|
+
|
|
151
|
+
-- Memory entries (main storage)
|
|
152
|
+
CREATE TABLE IF NOT EXISTS memory_entries (
|
|
153
|
+
id TEXT PRIMARY KEY,
|
|
154
|
+
key TEXT NOT NULL,
|
|
155
|
+
namespace TEXT DEFAULT 'default',
|
|
156
|
+
content TEXT NOT NULL,
|
|
157
|
+
type TEXT DEFAULT 'semantic' CHECK(type IN ('semantic', 'episodic', 'procedural', 'working', 'pattern')),
|
|
158
|
+
|
|
159
|
+
-- Vector embedding for semantic search (stored as JSON array)
|
|
160
|
+
embedding TEXT,
|
|
161
|
+
embedding_model TEXT DEFAULT 'local',
|
|
162
|
+
embedding_dimensions INTEGER,
|
|
163
|
+
|
|
164
|
+
-- Metadata
|
|
165
|
+
tags TEXT, -- JSON array
|
|
166
|
+
metadata TEXT, -- JSON object
|
|
167
|
+
owner_id TEXT,
|
|
168
|
+
|
|
169
|
+
-- Timestamps
|
|
170
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
171
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
172
|
+
expires_at INTEGER,
|
|
173
|
+
last_accessed_at INTEGER,
|
|
174
|
+
|
|
175
|
+
-- Access tracking for hot/cold detection
|
|
176
|
+
access_count INTEGER DEFAULT 0,
|
|
177
|
+
|
|
178
|
+
-- Status
|
|
179
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deleted')),
|
|
180
|
+
|
|
181
|
+
UNIQUE(namespace, key)
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
-- Indexes for memory entries
|
|
185
|
+
CREATE INDEX IF NOT EXISTS idx_memory_namespace ON memory_entries(namespace);
|
|
186
|
+
CREATE INDEX IF NOT EXISTS idx_memory_key ON memory_entries(key);
|
|
187
|
+
CREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(type);
|
|
188
|
+
CREATE INDEX IF NOT EXISTS idx_memory_status ON memory_entries(status);
|
|
189
|
+
CREATE INDEX IF NOT EXISTS idx_memory_created ON memory_entries(created_at);
|
|
190
|
+
CREATE INDEX IF NOT EXISTS idx_memory_accessed ON memory_entries(last_accessed_at);
|
|
191
|
+
CREATE INDEX IF NOT EXISTS idx_memory_owner ON memory_entries(owner_id);
|
|
192
|
+
|
|
193
|
+
-- ============================================
|
|
194
|
+
-- PATTERN LEARNING TABLES
|
|
195
|
+
-- ============================================
|
|
196
|
+
|
|
197
|
+
-- Learned patterns with confidence scoring and versioning
|
|
198
|
+
CREATE TABLE IF NOT EXISTS patterns (
|
|
199
|
+
id TEXT PRIMARY KEY,
|
|
200
|
+
|
|
201
|
+
-- Pattern identification
|
|
202
|
+
name TEXT NOT NULL,
|
|
203
|
+
pattern_type TEXT NOT NULL CHECK(pattern_type IN (
|
|
204
|
+
'task-routing', 'error-recovery', 'optimization', 'learning',
|
|
205
|
+
'coordination', 'prediction', 'code-pattern', 'workflow'
|
|
206
|
+
)),
|
|
207
|
+
|
|
208
|
+
-- Pattern definition
|
|
209
|
+
condition TEXT NOT NULL, -- Regex or semantic match
|
|
210
|
+
action TEXT NOT NULL, -- What to do when pattern matches
|
|
211
|
+
description TEXT,
|
|
212
|
+
|
|
213
|
+
-- Confidence scoring (0.0 - 1.0)
|
|
214
|
+
confidence REAL DEFAULT 0.5,
|
|
215
|
+
success_count INTEGER DEFAULT 0,
|
|
216
|
+
failure_count INTEGER DEFAULT 0,
|
|
217
|
+
|
|
218
|
+
-- Temporal decay
|
|
219
|
+
decay_rate REAL DEFAULT 0.01, -- How fast confidence decays
|
|
220
|
+
half_life_days INTEGER DEFAULT 30, -- Days until confidence halves without use
|
|
221
|
+
|
|
222
|
+
-- Vector embedding for semantic pattern matching
|
|
223
|
+
embedding TEXT,
|
|
224
|
+
embedding_dimensions INTEGER,
|
|
225
|
+
|
|
226
|
+
-- Versioning
|
|
227
|
+
version INTEGER DEFAULT 1,
|
|
228
|
+
parent_id TEXT REFERENCES patterns(id),
|
|
229
|
+
|
|
230
|
+
-- Metadata
|
|
231
|
+
tags TEXT, -- JSON array
|
|
232
|
+
metadata TEXT, -- JSON object
|
|
233
|
+
source TEXT, -- Where the pattern was learned from
|
|
234
|
+
|
|
235
|
+
-- Timestamps
|
|
236
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
237
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
238
|
+
last_matched_at INTEGER,
|
|
239
|
+
last_success_at INTEGER,
|
|
240
|
+
last_failure_at INTEGER,
|
|
241
|
+
|
|
242
|
+
-- Status
|
|
243
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deprecated', 'experimental'))
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
-- Indexes for patterns
|
|
247
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);
|
|
248
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);
|
|
249
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_status ON patterns(status);
|
|
250
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_last_matched ON patterns(last_matched_at);
|
|
251
|
+
|
|
252
|
+
-- Pattern evolution history (for versioning)
|
|
253
|
+
CREATE TABLE IF NOT EXISTS pattern_history (
|
|
254
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
255
|
+
pattern_id TEXT NOT NULL REFERENCES patterns(id),
|
|
256
|
+
version INTEGER NOT NULL,
|
|
257
|
+
|
|
258
|
+
-- Snapshot of pattern state
|
|
259
|
+
confidence REAL,
|
|
260
|
+
success_count INTEGER,
|
|
261
|
+
failure_count INTEGER,
|
|
262
|
+
condition TEXT,
|
|
263
|
+
action TEXT,
|
|
264
|
+
|
|
265
|
+
-- What changed
|
|
266
|
+
change_type TEXT CHECK(change_type IN ('created', 'updated', 'success', 'failure', 'decay', 'merged', 'split')),
|
|
267
|
+
change_reason TEXT,
|
|
268
|
+
|
|
269
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
CREATE INDEX IF NOT EXISTS idx_pattern_history_pattern ON pattern_history(pattern_id);
|
|
273
|
+
|
|
274
|
+
-- ============================================
|
|
275
|
+
-- LEARNING & TRAJECTORY TABLES
|
|
276
|
+
-- ============================================
|
|
277
|
+
|
|
278
|
+
-- Learning trajectories (SONA integration)
|
|
279
|
+
CREATE TABLE IF NOT EXISTS trajectories (
|
|
280
|
+
id TEXT PRIMARY KEY,
|
|
281
|
+
session_id TEXT,
|
|
282
|
+
|
|
283
|
+
-- Trajectory state
|
|
284
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'completed', 'failed', 'abandoned')),
|
|
285
|
+
verdict TEXT CHECK(verdict IN ('success', 'failure', 'partial', NULL)),
|
|
286
|
+
|
|
287
|
+
-- Context
|
|
288
|
+
task TEXT,
|
|
289
|
+
context TEXT, -- JSON object
|
|
290
|
+
|
|
291
|
+
-- Metrics
|
|
292
|
+
total_steps INTEGER DEFAULT 0,
|
|
293
|
+
total_reward REAL DEFAULT 0,
|
|
294
|
+
|
|
295
|
+
-- Timestamps
|
|
296
|
+
started_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
297
|
+
ended_at INTEGER,
|
|
298
|
+
|
|
299
|
+
-- Reference to extracted pattern (if any)
|
|
300
|
+
extracted_pattern_id TEXT REFERENCES patterns(id)
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
-- Trajectory steps
|
|
304
|
+
CREATE TABLE IF NOT EXISTS trajectory_steps (
|
|
305
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
306
|
+
trajectory_id TEXT NOT NULL REFERENCES trajectories(id),
|
|
307
|
+
step_number INTEGER NOT NULL,
|
|
308
|
+
|
|
309
|
+
-- Step data
|
|
310
|
+
action TEXT NOT NULL,
|
|
311
|
+
observation TEXT,
|
|
312
|
+
reward REAL DEFAULT 0,
|
|
313
|
+
|
|
314
|
+
-- Metadata
|
|
315
|
+
metadata TEXT, -- JSON object
|
|
316
|
+
|
|
317
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
CREATE INDEX IF NOT EXISTS idx_steps_trajectory ON trajectory_steps(trajectory_id);
|
|
321
|
+
|
|
322
|
+
-- ============================================
|
|
323
|
+
-- MIGRATION STATE TRACKING
|
|
324
|
+
-- ============================================
|
|
325
|
+
|
|
326
|
+
-- Migration state (for resume capability)
|
|
327
|
+
CREATE TABLE IF NOT EXISTS migration_state (
|
|
328
|
+
id TEXT PRIMARY KEY,
|
|
329
|
+
migration_type TEXT NOT NULL, -- 'v2-to-v3', 'pattern', 'memory', etc.
|
|
330
|
+
|
|
331
|
+
-- Progress tracking
|
|
332
|
+
status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'in_progress', 'completed', 'failed', 'rolled_back')),
|
|
333
|
+
total_items INTEGER DEFAULT 0,
|
|
334
|
+
processed_items INTEGER DEFAULT 0,
|
|
335
|
+
failed_items INTEGER DEFAULT 0,
|
|
336
|
+
skipped_items INTEGER DEFAULT 0,
|
|
337
|
+
|
|
338
|
+
-- Current position (for resume)
|
|
339
|
+
current_batch INTEGER DEFAULT 0,
|
|
340
|
+
last_processed_id TEXT,
|
|
341
|
+
|
|
342
|
+
-- Source/destination info
|
|
343
|
+
source_path TEXT,
|
|
344
|
+
source_type TEXT,
|
|
345
|
+
destination_path TEXT,
|
|
346
|
+
|
|
347
|
+
-- Backup info
|
|
348
|
+
backup_path TEXT,
|
|
349
|
+
backup_created_at INTEGER,
|
|
350
|
+
|
|
351
|
+
-- Error tracking
|
|
352
|
+
last_error TEXT,
|
|
353
|
+
errors TEXT, -- JSON array of errors
|
|
354
|
+
|
|
355
|
+
-- Timestamps
|
|
356
|
+
started_at INTEGER,
|
|
357
|
+
completed_at INTEGER,
|
|
358
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
359
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
-- ============================================
|
|
363
|
+
-- SESSION MANAGEMENT
|
|
364
|
+
-- ============================================
|
|
365
|
+
|
|
366
|
+
-- Sessions for context persistence
|
|
367
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
368
|
+
id TEXT PRIMARY KEY,
|
|
369
|
+
|
|
370
|
+
-- Session state
|
|
371
|
+
state TEXT NOT NULL, -- JSON object with full session state
|
|
372
|
+
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'paused', 'completed', 'expired')),
|
|
373
|
+
|
|
374
|
+
-- Context
|
|
375
|
+
project_path TEXT,
|
|
376
|
+
branch TEXT,
|
|
377
|
+
|
|
378
|
+
-- Metrics
|
|
379
|
+
tasks_completed INTEGER DEFAULT 0,
|
|
380
|
+
patterns_learned INTEGER DEFAULT 0,
|
|
381
|
+
|
|
382
|
+
-- Timestamps
|
|
383
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
384
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
385
|
+
expires_at INTEGER
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
-- ============================================
|
|
389
|
+
-- VECTOR INDEX METADATA (for HNSW)
|
|
390
|
+
-- ============================================
|
|
391
|
+
|
|
392
|
+
-- Track HNSW index state
|
|
393
|
+
CREATE TABLE IF NOT EXISTS vector_indexes (
|
|
394
|
+
id TEXT PRIMARY KEY,
|
|
395
|
+
name TEXT NOT NULL UNIQUE,
|
|
396
|
+
|
|
397
|
+
-- Index configuration
|
|
398
|
+
dimensions INTEGER NOT NULL,
|
|
399
|
+
metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),
|
|
400
|
+
|
|
401
|
+
-- HNSW parameters
|
|
402
|
+
hnsw_m INTEGER DEFAULT 16,
|
|
403
|
+
hnsw_ef_construction INTEGER DEFAULT 200,
|
|
404
|
+
hnsw_ef_search INTEGER DEFAULT 100,
|
|
405
|
+
|
|
406
|
+
-- Quantization
|
|
407
|
+
quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),
|
|
408
|
+
quantization_bits INTEGER DEFAULT 8,
|
|
409
|
+
|
|
410
|
+
-- Statistics
|
|
411
|
+
total_vectors INTEGER DEFAULT 0,
|
|
412
|
+
last_rebuild_at INTEGER,
|
|
413
|
+
|
|
414
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
415
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
-- ============================================
|
|
419
|
+
-- GRAPH EDGES (ADR-130 Phase 1)
|
|
420
|
+
-- Unified knowledge graph backend — sql.js canonical store
|
|
421
|
+
-- ============================================
|
|
422
|
+
|
|
423
|
+
-- Unified graph edges table (ADR-130)
|
|
424
|
+
-- Node IDs use domain-prefixed format: {domain}:{uuid}
|
|
425
|
+
-- where domain in (mem, agent, task, entity, span, pattern)
|
|
426
|
+
CREATE TABLE IF NOT EXISTS graph_edges (
|
|
427
|
+
id TEXT PRIMARY KEY, -- edge-{uuid}
|
|
428
|
+
source_id TEXT NOT NULL, -- domain-prefixed node ID
|
|
429
|
+
target_id TEXT NOT NULL, -- domain-prefixed node ID
|
|
430
|
+
relation TEXT NOT NULL, -- e.g. "caused", "depends-on", "imports"
|
|
431
|
+
weight REAL DEFAULT 1.0,
|
|
432
|
+
-- Temporal / reliability semantics (ADR-130 §"graph that forgets" property)
|
|
433
|
+
confidence REAL DEFAULT 1.0, -- [0,1]; updated by JUDGE step
|
|
434
|
+
decay_rate REAL DEFAULT 0.0, -- per-day exponential decay applied at read time
|
|
435
|
+
last_reinforced TEXT, -- ISO-8601; set when CONSOLIDATE re-touches edge
|
|
436
|
+
witness_id TEXT, -- FK to verification/witness-fixes.json (ADR-103)
|
|
437
|
+
-- Embedding storage: "inline:{base64}" | "vector_indexes:{id}" | NULL
|
|
438
|
+
embedding_ref TEXT,
|
|
439
|
+
metadata TEXT, -- JSON blob for plugin-specific fields
|
|
440
|
+
created_at TEXT NOT NULL
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
CREATE INDEX IF NOT EXISTS idx_graph_edges_source ON graph_edges (source_id);
|
|
444
|
+
CREATE INDEX IF NOT EXISTS idx_graph_edges_target ON graph_edges (target_id);
|
|
445
|
+
CREATE INDEX IF NOT EXISTS idx_graph_edges_relation ON graph_edges (relation);
|
|
446
|
+
CREATE INDEX IF NOT EXISTS idx_graph_edges_reinforced ON graph_edges (last_reinforced);
|
|
447
|
+
|
|
448
|
+
-- ============================================
|
|
449
|
+
-- SYSTEM METADATA
|
|
450
|
+
-- ============================================
|
|
451
|
+
|
|
452
|
+
CREATE TABLE IF NOT EXISTS metadata (
|
|
453
|
+
key TEXT PRIMARY KEY,
|
|
454
|
+
value TEXT NOT NULL,
|
|
455
|
+
updated_at INTEGER DEFAULT (strftime('%s', 'now') * 1000)
|
|
456
|
+
);
|
|
457
|
+
`;
|
|
458
|
+
let hnswIndex = null;
|
|
459
|
+
let hnswInitializing = false;
|
|
460
|
+
/**
|
|
461
|
+
* Get or create the HNSW index singleton
|
|
462
|
+
* Lazily initializes from SQLite data on first use
|
|
463
|
+
*/
|
|
464
|
+
export async function getHNSWIndex(options) {
|
|
465
|
+
const dimensions = options?.dimensions ?? 384;
|
|
466
|
+
// Return existing index if already initialized
|
|
467
|
+
if (hnswIndex?.initialized && !options?.forceRebuild) {
|
|
468
|
+
return hnswIndex;
|
|
469
|
+
}
|
|
470
|
+
// Prevent concurrent initialization
|
|
471
|
+
if (hnswInitializing) {
|
|
472
|
+
// Wait for initialization to complete
|
|
473
|
+
while (hnswInitializing) {
|
|
474
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
475
|
+
}
|
|
476
|
+
return hnswIndex;
|
|
477
|
+
}
|
|
478
|
+
hnswInitializing = true;
|
|
479
|
+
try {
|
|
480
|
+
// Import @ruvector/core dynamically
|
|
481
|
+
// Handle both ESM (default export) and CJS patterns
|
|
482
|
+
const ruvectorModule = await import('@ruvector/core').catch(() => null);
|
|
483
|
+
if (!ruvectorModule) {
|
|
484
|
+
hnswInitializing = false;
|
|
485
|
+
return null; // HNSW not available
|
|
486
|
+
}
|
|
487
|
+
// ESM returns { default: { VectorDb, ... } }, CJS returns { VectorDb, ... }
|
|
488
|
+
const ruvectorCore = ruvectorModule.default || ruvectorModule;
|
|
489
|
+
if (!ruvectorCore?.VectorDb) {
|
|
490
|
+
hnswInitializing = false;
|
|
491
|
+
return null; // VectorDb not found
|
|
492
|
+
}
|
|
493
|
+
const { VectorDb } = ruvectorCore;
|
|
494
|
+
// Persistent storage paths — resolve to absolute to survive CWD changes
|
|
495
|
+
const swarmDir = getMemoryRoot();
|
|
496
|
+
if (!fs.existsSync(swarmDir)) {
|
|
497
|
+
fs.mkdirSync(swarmDir, { recursive: true });
|
|
498
|
+
}
|
|
499
|
+
const hnswPath = path.join(swarmDir, 'hnsw.index');
|
|
500
|
+
const metadataPath = path.join(swarmDir, 'hnsw.metadata.json');
|
|
501
|
+
const dbPath = options?.dbPath ? path.resolve(options.dbPath) : path.join(swarmDir, 'memory.db');
|
|
502
|
+
// Create HNSW index with persistent storage
|
|
503
|
+
// @ruvector/core uses string enum for distanceMetric: 'Cosine', 'Euclidean', 'DotProduct', 'Manhattan'
|
|
504
|
+
const db = new VectorDb({
|
|
505
|
+
dimensions,
|
|
506
|
+
distanceMetric: 'Cosine',
|
|
507
|
+
storagePath: hnswPath // Persistent storage!
|
|
508
|
+
});
|
|
509
|
+
// Load metadata (entry info) if exists
|
|
510
|
+
const entries = new Map();
|
|
511
|
+
if (fs.existsSync(metadataPath)) {
|
|
512
|
+
try {
|
|
513
|
+
const metadataJson = fs.readFileSync(metadataPath, 'utf-8');
|
|
514
|
+
const metadata = JSON.parse(metadataJson);
|
|
515
|
+
for (const [key, value] of metadata) {
|
|
516
|
+
entries.set(key, value);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
catch {
|
|
520
|
+
// Metadata load failed, will rebuild
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
hnswIndex = {
|
|
524
|
+
db,
|
|
525
|
+
entries,
|
|
526
|
+
dimensions,
|
|
527
|
+
initialized: false
|
|
528
|
+
};
|
|
529
|
+
// Check if index already has data (from persistent storage)
|
|
530
|
+
const existingLen = await db.len();
|
|
531
|
+
if (existingLen > 0 && entries.size > 0) {
|
|
532
|
+
// Index loaded from disk, skip SQLite sync
|
|
533
|
+
hnswIndex.initialized = true;
|
|
534
|
+
hnswInitializing = false;
|
|
535
|
+
return hnswIndex;
|
|
536
|
+
}
|
|
537
|
+
if (fs.existsSync(dbPath)) {
|
|
538
|
+
try {
|
|
539
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
540
|
+
const SQL = await initSqlJs();
|
|
541
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
542
|
+
const sqlDb = new SQL.Database(fileBuffer);
|
|
543
|
+
// Load all entries with embeddings
|
|
544
|
+
const result = sqlDb.exec(`
|
|
545
|
+
SELECT id, key, namespace, content, embedding
|
|
546
|
+
FROM memory_entries
|
|
547
|
+
WHERE status = 'active' AND embedding IS NOT NULL
|
|
548
|
+
LIMIT 10000
|
|
549
|
+
`);
|
|
550
|
+
if (result[0]?.values) {
|
|
551
|
+
for (const row of result[0].values) {
|
|
552
|
+
const [id, key, ns, content, embeddingJson] = row;
|
|
553
|
+
if (embeddingJson) {
|
|
554
|
+
try {
|
|
555
|
+
const embedding = JSON.parse(embeddingJson);
|
|
556
|
+
const vector = new Float32Array(embedding);
|
|
557
|
+
await db.insert({
|
|
558
|
+
id: String(id),
|
|
559
|
+
vector
|
|
560
|
+
});
|
|
561
|
+
hnswIndex.entries.set(String(id), {
|
|
562
|
+
id: String(id),
|
|
563
|
+
key: key || String(id),
|
|
564
|
+
namespace: ns || 'default',
|
|
565
|
+
content: content || ''
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
catch {
|
|
569
|
+
// Skip invalid embeddings
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
sqlDb.close();
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
// SQLite load failed, start with empty index
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
hnswIndex.initialized = true;
|
|
581
|
+
hnswInitializing = false;
|
|
582
|
+
return hnswIndex;
|
|
583
|
+
}
|
|
584
|
+
catch {
|
|
585
|
+
hnswInitializing = false;
|
|
586
|
+
return null;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Save HNSW metadata to disk for persistence
|
|
591
|
+
*/
|
|
592
|
+
function saveHNSWMetadata() {
|
|
593
|
+
if (!hnswIndex?.entries)
|
|
594
|
+
return;
|
|
595
|
+
try {
|
|
596
|
+
const swarmDir = getMemoryRoot();
|
|
597
|
+
const metadataPath = path.join(swarmDir, 'hnsw.metadata.json');
|
|
598
|
+
const metadata = Array.from(hnswIndex.entries.entries());
|
|
599
|
+
fs.writeFileSync(metadataPath, JSON.stringify(metadata));
|
|
600
|
+
}
|
|
601
|
+
catch {
|
|
602
|
+
// Silently fail - metadata save is best-effort
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Add entry to HNSW index (with automatic persistence)
|
|
607
|
+
*/
|
|
608
|
+
export async function addToHNSWIndex(id, embedding, entry) {
|
|
609
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
610
|
+
const bridge = await getBridge();
|
|
611
|
+
if (bridge) {
|
|
612
|
+
const bridgeResult = await bridge.bridgeAddToHNSW(id, embedding, entry);
|
|
613
|
+
if (bridgeResult === true)
|
|
614
|
+
return true;
|
|
615
|
+
}
|
|
616
|
+
const index = await getHNSWIndex({ dimensions: embedding.length });
|
|
617
|
+
if (!index)
|
|
618
|
+
return false;
|
|
619
|
+
try {
|
|
620
|
+
const vector = new Float32Array(embedding);
|
|
621
|
+
await index.db.insert({
|
|
622
|
+
id,
|
|
623
|
+
vector
|
|
624
|
+
});
|
|
625
|
+
index.entries.set(id, entry);
|
|
626
|
+
// Save metadata for persistence (debounced would be better for high-volume)
|
|
627
|
+
saveHNSWMetadata();
|
|
628
|
+
return true;
|
|
629
|
+
}
|
|
630
|
+
catch {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Search HNSW index (150x faster than brute-force)
|
|
636
|
+
* Returns results sorted by similarity (highest first)
|
|
637
|
+
*/
|
|
638
|
+
export async function searchHNSWIndex(queryEmbedding, options) {
|
|
639
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
640
|
+
const bridge = await getBridge();
|
|
641
|
+
if (bridge) {
|
|
642
|
+
const bridgeResult = await bridge.bridgeSearchHNSW(queryEmbedding, options);
|
|
643
|
+
if (bridgeResult)
|
|
644
|
+
return bridgeResult;
|
|
645
|
+
}
|
|
646
|
+
const index = await getHNSWIndex({ dimensions: queryEmbedding.length });
|
|
647
|
+
if (!index)
|
|
648
|
+
return null;
|
|
649
|
+
try {
|
|
650
|
+
const vector = new Float32Array(queryEmbedding);
|
|
651
|
+
const k = options?.k ?? 10;
|
|
652
|
+
// HNSW search returns results with cosine distance (lower = more similar)
|
|
653
|
+
const results = await index.db.search({ vector, k: k * 2 }); // Get extra for filtering
|
|
654
|
+
const filtered = [];
|
|
655
|
+
for (const result of results) {
|
|
656
|
+
const entry = index.entries.get(result.id);
|
|
657
|
+
if (!entry)
|
|
658
|
+
continue;
|
|
659
|
+
// Filter by namespace if specified
|
|
660
|
+
if (options?.namespace && options.namespace !== 'all' && entry.namespace !== options.namespace) {
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
// Convert cosine distance to similarity score (1 - distance)
|
|
664
|
+
// Cosine distance from @ruvector/core: 0 = identical, 2 = opposite
|
|
665
|
+
const score = 1 - (result.score / 2);
|
|
666
|
+
filtered.push({
|
|
667
|
+
id: entry.id.substring(0, 12),
|
|
668
|
+
key: entry.key || entry.id.substring(0, 15),
|
|
669
|
+
content: entry.content.substring(0, 60) + (entry.content.length > 60 ? '...' : ''),
|
|
670
|
+
score,
|
|
671
|
+
namespace: entry.namespace
|
|
672
|
+
});
|
|
673
|
+
if (filtered.length >= k)
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
// Sort by score descending (highest similarity first)
|
|
677
|
+
filtered.sort((a, b) => b.score - a.score);
|
|
678
|
+
return filtered;
|
|
679
|
+
}
|
|
680
|
+
catch {
|
|
681
|
+
return null;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Get HNSW index status
|
|
686
|
+
*/
|
|
687
|
+
export function getHNSWStatus() {
|
|
688
|
+
// ADR-053: If bridge was previously loaded, report availability
|
|
689
|
+
if (_bridge && _bridge !== null) {
|
|
690
|
+
// Bridge is loaded — HNSW-equivalent is available via AgentDB v3
|
|
691
|
+
return {
|
|
692
|
+
available: true,
|
|
693
|
+
initialized: true,
|
|
694
|
+
entryCount: hnswIndex?.entries.size ?? 0,
|
|
695
|
+
dimensions: hnswIndex?.dimensions ?? 384
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
// #2356: `available` now reflects real capability (index already loaded OR
|
|
699
|
+
// @ruvector/core installed and resolvable), not merely whether the lazy
|
|
700
|
+
// singleton happens to be warm in this process. `initialized` still reports
|
|
701
|
+
// whether the in-process index is actually loaded, so callers can tell
|
|
702
|
+
// "installed but not yet loaded" apart from "loaded".
|
|
703
|
+
return {
|
|
704
|
+
available: hnswIndex !== null || isRuvectorCoreResolvable(),
|
|
705
|
+
initialized: hnswIndex?.initialized ?? false,
|
|
706
|
+
entryCount: hnswIndex?.entries.size ?? 0,
|
|
707
|
+
dimensions: hnswIndex?.dimensions ?? 384
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Clear the HNSW index (for rebuilding)
|
|
712
|
+
*/
|
|
713
|
+
export function clearHNSWIndex() {
|
|
714
|
+
hnswIndex = null;
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Invalidate the in-memory HNSW cache so the next search rebuilds from DB.
|
|
718
|
+
* Call this after deleting entries that had embeddings to prevent ghost
|
|
719
|
+
* vectors from appearing in search results.
|
|
720
|
+
*/
|
|
721
|
+
export function rebuildSearchIndex() {
|
|
722
|
+
hnswIndex = null;
|
|
723
|
+
hnswInitializing = false;
|
|
724
|
+
}
|
|
725
|
+
// ============================================================================
|
|
726
|
+
// INT8 VECTOR QUANTIZATION (4x memory reduction)
|
|
727
|
+
// ============================================================================
|
|
728
|
+
/**
|
|
729
|
+
* Quantize a Float32 embedding to Int8 (4x memory reduction)
|
|
730
|
+
* Uses symmetric quantization with scale factor stored per-vector
|
|
731
|
+
*
|
|
732
|
+
* @param embedding - Float32 embedding array
|
|
733
|
+
* @returns Quantized Int8 array with scale factor
|
|
734
|
+
*/
|
|
735
|
+
export function quantizeInt8(embedding) {
|
|
736
|
+
const arr = embedding instanceof Float32Array ? embedding : new Float32Array(embedding);
|
|
737
|
+
// Find min/max for symmetric quantization
|
|
738
|
+
let min = Infinity, max = -Infinity;
|
|
739
|
+
for (let i = 0; i < arr.length; i++) {
|
|
740
|
+
if (arr[i] < min)
|
|
741
|
+
min = arr[i];
|
|
742
|
+
if (arr[i] > max)
|
|
743
|
+
max = arr[i];
|
|
744
|
+
}
|
|
745
|
+
// Symmetric quantization: scale = max(|min|, |max|) / 127
|
|
746
|
+
const absMax = Math.max(Math.abs(min), Math.abs(max));
|
|
747
|
+
const scale = absMax / 127 || 1e-10; // Avoid division by zero
|
|
748
|
+
const zeroPoint = 0; // Symmetric quantization
|
|
749
|
+
// Quantize
|
|
750
|
+
const quantized = new Int8Array(arr.length);
|
|
751
|
+
for (let i = 0; i < arr.length; i++) {
|
|
752
|
+
// Clamp to [-127, 127] to leave room for potential rounding
|
|
753
|
+
const q = Math.round(arr[i] / scale);
|
|
754
|
+
quantized[i] = Math.max(-127, Math.min(127, q));
|
|
755
|
+
}
|
|
756
|
+
return { quantized, scale, zeroPoint };
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Dequantize Int8 back to Float32
|
|
760
|
+
*
|
|
761
|
+
* @param quantized - Int8 quantized array
|
|
762
|
+
* @param scale - Scale factor from quantization
|
|
763
|
+
* @param zeroPoint - Zero point (usually 0 for symmetric)
|
|
764
|
+
* @returns Float32Array
|
|
765
|
+
*/
|
|
766
|
+
export function dequantizeInt8(quantized, scale, zeroPoint = 0) {
|
|
767
|
+
const result = new Float32Array(quantized.length);
|
|
768
|
+
for (let i = 0; i < quantized.length; i++) {
|
|
769
|
+
result[i] = (quantized[i] - zeroPoint) * scale;
|
|
770
|
+
}
|
|
771
|
+
return result;
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Compute cosine similarity between quantized vectors
|
|
775
|
+
* Faster than dequantizing first
|
|
776
|
+
*/
|
|
777
|
+
export function quantizedCosineSim(a, aScale, b, bScale) {
|
|
778
|
+
if (a.length !== b.length)
|
|
779
|
+
return 0;
|
|
780
|
+
let dot = 0, normA = 0, normB = 0;
|
|
781
|
+
for (let i = 0; i < a.length; i++) {
|
|
782
|
+
dot += a[i] * b[i];
|
|
783
|
+
normA += a[i] * a[i];
|
|
784
|
+
normB += b[i] * b[i];
|
|
785
|
+
}
|
|
786
|
+
// Scales cancel out in cosine similarity for normalized vectors
|
|
787
|
+
const mag = Math.sqrt(normA * normB);
|
|
788
|
+
return mag === 0 ? 0 : dot / mag;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Get quantization statistics for an embedding
|
|
792
|
+
*/
|
|
793
|
+
export function getQuantizationStats(embedding) {
|
|
794
|
+
const len = embedding.length;
|
|
795
|
+
const originalBytes = len * 4; // Float32 = 4 bytes
|
|
796
|
+
const quantizedBytes = len + 8; // Int8 = 1 byte + 8 bytes for scale/zeroPoint
|
|
797
|
+
const compressionRatio = originalBytes / quantizedBytes;
|
|
798
|
+
return { originalBytes, quantizedBytes, compressionRatio };
|
|
799
|
+
}
|
|
800
|
+
// ============================================================================
|
|
801
|
+
// FLASH ATTENTION-STYLE BATCH OPERATIONS (V8-Optimized)
|
|
802
|
+
// ============================================================================
|
|
803
|
+
/**
|
|
804
|
+
* Batch cosine similarity - compute query against multiple vectors
|
|
805
|
+
* Optimized for V8 JIT with typed arrays
|
|
806
|
+
* ~50μs per 1000 vectors (384-dim)
|
|
807
|
+
*/
|
|
808
|
+
export function batchCosineSim(query, vectors) {
|
|
809
|
+
const n = vectors.length;
|
|
810
|
+
const scores = new Float32Array(n);
|
|
811
|
+
if (n === 0 || query.length === 0)
|
|
812
|
+
return scores;
|
|
813
|
+
// Pre-compute query norm
|
|
814
|
+
let queryNorm = 0;
|
|
815
|
+
for (let i = 0; i < query.length; i++) {
|
|
816
|
+
queryNorm += query[i] * query[i];
|
|
817
|
+
}
|
|
818
|
+
queryNorm = Math.sqrt(queryNorm);
|
|
819
|
+
if (queryNorm === 0)
|
|
820
|
+
return scores;
|
|
821
|
+
// Compute similarities
|
|
822
|
+
for (let v = 0; v < n; v++) {
|
|
823
|
+
const vec = vectors[v];
|
|
824
|
+
const len = Math.min(query.length, vec.length);
|
|
825
|
+
let dot = 0, vecNorm = 0;
|
|
826
|
+
for (let i = 0; i < len; i++) {
|
|
827
|
+
dot += query[i] * vec[i];
|
|
828
|
+
vecNorm += vec[i] * vec[i];
|
|
829
|
+
}
|
|
830
|
+
vecNorm = Math.sqrt(vecNorm);
|
|
831
|
+
scores[v] = vecNorm === 0 ? 0 : dot / (queryNorm * vecNorm);
|
|
832
|
+
}
|
|
833
|
+
return scores;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Softmax normalization for attention scores
|
|
837
|
+
* Numerically stable implementation
|
|
838
|
+
*/
|
|
839
|
+
export function softmaxAttention(scores, temperature = 1.0) {
|
|
840
|
+
const n = scores.length;
|
|
841
|
+
const result = new Float32Array(n);
|
|
842
|
+
if (n === 0)
|
|
843
|
+
return result;
|
|
844
|
+
// Find max for numerical stability
|
|
845
|
+
let max = scores[0];
|
|
846
|
+
for (let i = 1; i < n; i++) {
|
|
847
|
+
if (scores[i] > max)
|
|
848
|
+
max = scores[i];
|
|
849
|
+
}
|
|
850
|
+
// Compute exp and sum
|
|
851
|
+
let sum = 0;
|
|
852
|
+
for (let i = 0; i < n; i++) {
|
|
853
|
+
result[i] = Math.exp((scores[i] - max) / temperature);
|
|
854
|
+
sum += result[i];
|
|
855
|
+
}
|
|
856
|
+
// Normalize
|
|
857
|
+
if (sum > 0) {
|
|
858
|
+
for (let i = 0; i < n; i++) {
|
|
859
|
+
result[i] /= sum;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
return result;
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Top-K selection with partial sort (O(n + k log k))
|
|
866
|
+
* More efficient than full sort for small k
|
|
867
|
+
*/
|
|
868
|
+
export function topKIndices(scores, k) {
|
|
869
|
+
const n = scores.length;
|
|
870
|
+
if (k >= n) {
|
|
871
|
+
// Return all indices sorted by score
|
|
872
|
+
return Array.from({ length: n }, (_, i) => i)
|
|
873
|
+
.sort((a, b) => scores[b] - scores[a]);
|
|
874
|
+
}
|
|
875
|
+
// Build min-heap of size k
|
|
876
|
+
const heap = [];
|
|
877
|
+
for (let i = 0; i < n; i++) {
|
|
878
|
+
if (heap.length < k) {
|
|
879
|
+
heap.push({ idx: i, score: scores[i] });
|
|
880
|
+
// Bubble up
|
|
881
|
+
let j = heap.length - 1;
|
|
882
|
+
while (j > 0) {
|
|
883
|
+
const parent = Math.floor((j - 1) / 2);
|
|
884
|
+
if (heap[j].score < heap[parent].score) {
|
|
885
|
+
[heap[j], heap[parent]] = [heap[parent], heap[j]];
|
|
886
|
+
j = parent;
|
|
887
|
+
}
|
|
888
|
+
else
|
|
889
|
+
break;
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
else if (scores[i] > heap[0].score) {
|
|
893
|
+
// Replace min and heapify down
|
|
894
|
+
heap[0] = { idx: i, score: scores[i] };
|
|
895
|
+
let j = 0;
|
|
896
|
+
while (true) {
|
|
897
|
+
const left = 2 * j + 1, right = 2 * j + 2;
|
|
898
|
+
let smallest = j;
|
|
899
|
+
if (left < k && heap[left].score < heap[smallest].score)
|
|
900
|
+
smallest = left;
|
|
901
|
+
if (right < k && heap[right].score < heap[smallest].score)
|
|
902
|
+
smallest = right;
|
|
903
|
+
if (smallest === j)
|
|
904
|
+
break;
|
|
905
|
+
[heap[j], heap[smallest]] = [heap[smallest], heap[j]];
|
|
906
|
+
j = smallest;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
// Extract and sort descending
|
|
911
|
+
return heap.sort((a, b) => b.score - a.score).map(h => h.idx);
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Flash Attention-style search
|
|
915
|
+
* Combines batch similarity, softmax, and top-k in one pass
|
|
916
|
+
* Returns indices and attention weights
|
|
917
|
+
*/
|
|
918
|
+
export function flashAttentionSearch(query, vectors, options = {}) {
|
|
919
|
+
const { k = 10, temperature = 1.0, threshold = 0 } = options;
|
|
920
|
+
// Compute batch similarity
|
|
921
|
+
const scores = batchCosineSim(query, vectors);
|
|
922
|
+
// Get top-k indices
|
|
923
|
+
const indices = topKIndices(scores, k);
|
|
924
|
+
// Filter by threshold
|
|
925
|
+
const filtered = indices.filter(i => scores[i] >= threshold);
|
|
926
|
+
// Extract scores for filtered results
|
|
927
|
+
const topScores = new Float32Array(filtered.length);
|
|
928
|
+
for (let i = 0; i < filtered.length; i++) {
|
|
929
|
+
topScores[i] = scores[filtered[i]];
|
|
930
|
+
}
|
|
931
|
+
// Compute attention weights (softmax over top-k)
|
|
932
|
+
const weights = softmaxAttention(topScores, temperature);
|
|
933
|
+
return { indices: filtered, scores: topScores, weights };
|
|
934
|
+
}
|
|
935
|
+
// ============================================================================
|
|
936
|
+
// METADATA AND INITIALIZATION
|
|
937
|
+
// ============================================================================
|
|
938
|
+
/**
|
|
939
|
+
* Initial metadata to insert after schema creation
|
|
940
|
+
*/
|
|
941
|
+
export function getInitialMetadata(backend) {
|
|
942
|
+
return `
|
|
943
|
+
INSERT OR REPLACE INTO metadata (key, value) VALUES
|
|
944
|
+
('schema_version', '3.0.0'),
|
|
945
|
+
('backend', '${backend}'),
|
|
946
|
+
('created_at', '${new Date().toISOString()}'),
|
|
947
|
+
('sql_js', 'true'),
|
|
948
|
+
('vector_embeddings', 'enabled'),
|
|
949
|
+
('pattern_learning', 'enabled'),
|
|
950
|
+
('temporal_decay', 'enabled'),
|
|
951
|
+
('hnsw_indexing', 'enabled');
|
|
952
|
+
|
|
953
|
+
-- Create default vector index configuration. Dimension matches the default
|
|
954
|
+
-- ONNX embedding model (Xenova/all-MiniLM-L6-v2, 384-dim); HNSW rejects
|
|
955
|
+
-- inserts whose dim does not match this row, so a 768 here breaks every
|
|
956
|
+
-- memory_store --vector and memory_search on a fresh install (#1947).
|
|
957
|
+
INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES
|
|
958
|
+
('default', 'default', 384),
|
|
959
|
+
('patterns', 'patterns', 384);
|
|
960
|
+
`;
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Ensure memory_entries table has all required columns
|
|
964
|
+
* Adds missing columns for older databases (e.g., 'content' column)
|
|
965
|
+
*/
|
|
966
|
+
export async function ensureSchemaColumns(dbPath) {
|
|
967
|
+
const columnsAdded = [];
|
|
968
|
+
try {
|
|
969
|
+
if (!fs.existsSync(dbPath)) {
|
|
970
|
+
return { success: true, columnsAdded: [] };
|
|
971
|
+
}
|
|
972
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
973
|
+
const SQL = await initSqlJs();
|
|
974
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
975
|
+
const db = new SQL.Database(fileBuffer);
|
|
976
|
+
// Get current columns in memory_entries
|
|
977
|
+
const tableInfo = db.exec("PRAGMA table_info(memory_entries)");
|
|
978
|
+
const existingColumns = new Set(tableInfo[0]?.values?.map(row => row[1]) || []);
|
|
979
|
+
// Required columns that may be missing in older schemas
|
|
980
|
+
// Issue #977: 'type' column was missing from this list, causing store failures on older DBs
|
|
981
|
+
const requiredColumns = [
|
|
982
|
+
{ name: 'content', definition: "content TEXT DEFAULT ''" },
|
|
983
|
+
{ name: 'type', definition: "type TEXT DEFAULT 'semantic'" },
|
|
984
|
+
{ name: 'embedding', definition: 'embedding TEXT' },
|
|
985
|
+
{ name: 'embedding_model', definition: "embedding_model TEXT DEFAULT 'local'" },
|
|
986
|
+
{ name: 'embedding_dimensions', definition: 'embedding_dimensions INTEGER' },
|
|
987
|
+
{ name: 'tags', definition: 'tags TEXT' },
|
|
988
|
+
{ name: 'metadata', definition: 'metadata TEXT' },
|
|
989
|
+
{ name: 'owner_id', definition: 'owner_id TEXT' },
|
|
990
|
+
{ name: 'expires_at', definition: 'expires_at INTEGER' },
|
|
991
|
+
{ name: 'last_accessed_at', definition: 'last_accessed_at INTEGER' },
|
|
992
|
+
{ name: 'access_count', definition: 'access_count INTEGER DEFAULT 0' },
|
|
993
|
+
{ name: 'status', definition: "status TEXT DEFAULT 'active'" }
|
|
994
|
+
];
|
|
995
|
+
let modified = false;
|
|
996
|
+
for (const col of requiredColumns) {
|
|
997
|
+
if (!existingColumns.has(col.name)) {
|
|
998
|
+
try {
|
|
999
|
+
db.run(`ALTER TABLE memory_entries ADD COLUMN ${col.definition}`);
|
|
1000
|
+
columnsAdded.push(col.name);
|
|
1001
|
+
modified = true;
|
|
1002
|
+
}
|
|
1003
|
+
catch (e) {
|
|
1004
|
+
// Column might already exist or other error - continue
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
// #2120 — Belt-and-suspenders backfill. `ALTER TABLE ADD COLUMN
|
|
1009
|
+
// status TEXT DEFAULT 'active'` should populate existing rows with
|
|
1010
|
+
// 'active' in modern SQLite, but: (a) some auto-memory bridge writes
|
|
1011
|
+
// happen via INSERT paths that pass an explicit NULL, (b) some
|
|
1012
|
+
// historical sql.js builds skipped the DEFAULT backfill, (c)
|
|
1013
|
+
// entries can be migrated in from older snapshots. After ensuring
|
|
1014
|
+
// the column exists, force-backfill any remaining NULL → 'active'.
|
|
1015
|
+
// Safe on already-correct DBs (0 rows updated).
|
|
1016
|
+
if (columnsAdded.includes('status') || existingColumns.has('status')) {
|
|
1017
|
+
try {
|
|
1018
|
+
db.run(`UPDATE memory_entries SET status = 'active' WHERE status IS NULL`);
|
|
1019
|
+
modified = true;
|
|
1020
|
+
}
|
|
1021
|
+
catch {
|
|
1022
|
+
/* table is read-only or doesn't exist — skip */
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
if (modified) {
|
|
1026
|
+
// Save updated database
|
|
1027
|
+
const data = db.export();
|
|
1028
|
+
writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
|
|
1029
|
+
}
|
|
1030
|
+
db.close();
|
|
1031
|
+
return { success: true, columnsAdded };
|
|
1032
|
+
}
|
|
1033
|
+
catch (error) {
|
|
1034
|
+
return {
|
|
1035
|
+
success: false,
|
|
1036
|
+
columnsAdded,
|
|
1037
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Check for legacy database installations and migrate if needed
|
|
1043
|
+
*/
|
|
1044
|
+
export async function checkAndMigrateLegacy(options) {
|
|
1045
|
+
const { dbPath, verbose = false } = options;
|
|
1046
|
+
// Check for legacy locations
|
|
1047
|
+
const legacyPaths = [
|
|
1048
|
+
path.join(process.cwd(), 'memory.db'),
|
|
1049
|
+
path.join(process.cwd(), '.claude/memory.db'),
|
|
1050
|
+
path.join(process.cwd(), 'data/memory.db'),
|
|
1051
|
+
path.join(process.cwd(), '.claude-flow/memory.db')
|
|
1052
|
+
];
|
|
1053
|
+
for (const legacyPath of legacyPaths) {
|
|
1054
|
+
if (fs.existsSync(legacyPath) && legacyPath !== dbPath) {
|
|
1055
|
+
try {
|
|
1056
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
1057
|
+
const SQL = await initSqlJs();
|
|
1058
|
+
const legacyBuffer = fs.readFileSync(legacyPath);
|
|
1059
|
+
const legacyDb = new SQL.Database(legacyBuffer);
|
|
1060
|
+
// Check if it has data
|
|
1061
|
+
const countResult = legacyDb.exec('SELECT COUNT(*) FROM memory_entries');
|
|
1062
|
+
const count = countResult[0]?.values[0]?.[0] || 0;
|
|
1063
|
+
// Get version if available
|
|
1064
|
+
let version = 'unknown';
|
|
1065
|
+
try {
|
|
1066
|
+
const versionResult = legacyDb.exec("SELECT value FROM metadata WHERE key='schema_version'");
|
|
1067
|
+
version = versionResult[0]?.values[0]?.[0] || 'unknown';
|
|
1068
|
+
}
|
|
1069
|
+
catch { /* no metadata table */ }
|
|
1070
|
+
legacyDb.close();
|
|
1071
|
+
if (count > 0) {
|
|
1072
|
+
return {
|
|
1073
|
+
needsMigration: true,
|
|
1074
|
+
legacyVersion: version,
|
|
1075
|
+
legacyEntries: count
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
catch {
|
|
1080
|
+
// Not a valid SQLite database, skip
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
return { needsMigration: false };
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* ADR-053: Activate ControllerRegistry so AgentDB v3 controllers
|
|
1088
|
+
* (ReasoningBank, SkillLibrary, ExplainableRecall, etc.) are instantiated.
|
|
1089
|
+
*
|
|
1090
|
+
* Uses the memory-bridge's getControllerRegistry() which lazily creates
|
|
1091
|
+
* a singleton ControllerRegistry and initializes it with the given dbPath.
|
|
1092
|
+
* After this call, all enabled controllers are ready for immediate use.
|
|
1093
|
+
*
|
|
1094
|
+
* Failures are isolated: if @claude-flow/memory or agentdb is not installed,
|
|
1095
|
+
* this returns an empty result without throwing.
|
|
1096
|
+
*/
|
|
1097
|
+
async function activateControllerRegistry(dbPath, verbose) {
|
|
1098
|
+
const startTime = performance.now();
|
|
1099
|
+
const activated = [];
|
|
1100
|
+
const failed = [];
|
|
1101
|
+
try {
|
|
1102
|
+
const bridge = await getBridge();
|
|
1103
|
+
if (!bridge) {
|
|
1104
|
+
return { activated, failed, initTimeMs: performance.now() - startTime };
|
|
1105
|
+
}
|
|
1106
|
+
const registry = await bridge.getControllerRegistry(dbPath);
|
|
1107
|
+
if (!registry) {
|
|
1108
|
+
return { activated, failed, initTimeMs: performance.now() - startTime };
|
|
1109
|
+
}
|
|
1110
|
+
// Collect controller status from the registry
|
|
1111
|
+
if (typeof registry.listControllers === 'function') {
|
|
1112
|
+
const controllers = registry.listControllers();
|
|
1113
|
+
for (const ctrl of controllers) {
|
|
1114
|
+
if (ctrl.enabled) {
|
|
1115
|
+
activated.push(ctrl.name);
|
|
1116
|
+
}
|
|
1117
|
+
else {
|
|
1118
|
+
failed.push(ctrl.name);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
if (verbose && activated.length > 0) {
|
|
1123
|
+
console.log(`ControllerRegistry: ${activated.length} controllers activated`);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
catch {
|
|
1127
|
+
// ControllerRegistry activation is best-effort
|
|
1128
|
+
}
|
|
1129
|
+
return { activated, failed, initTimeMs: performance.now() - startTime };
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Self-heal an EXISTING memory database that is missing the `vector_indexes`
|
|
1133
|
+
* table or per-namespace rows.
|
|
1134
|
+
*
|
|
1135
|
+
* Why this exists: fresh installs create `vector_indexes` + seed rows, but a
|
|
1136
|
+
* DB written by an older CLI or by agentdb directly may have thousands of
|
|
1137
|
+
* embedded rows in `memory_entries` and NO `vector_indexes` table at all.
|
|
1138
|
+
* Two things break as a result:
|
|
1139
|
+
* 1. The statusline's vector count read collapsed to `0` (the count query
|
|
1140
|
+
* referenced the missing table and failed whole — now split, but the
|
|
1141
|
+
* HNSW flag still needs the table).
|
|
1142
|
+
* 2. #1941 — `memory_search` routes per namespace via `vector_indexes`; a
|
|
1143
|
+
* namespace with no row returns 0 results even when entries exist.
|
|
1144
|
+
*
|
|
1145
|
+
* This is idempotent and conservative:
|
|
1146
|
+
* - Does NOTHING (no writes) when the table already exists and every embedded
|
|
1147
|
+
* namespace already has a row — the common already-healed path, hit on
|
|
1148
|
+
* every MCP start, must not write to the live DB unnecessarily.
|
|
1149
|
+
* - Before ANY write, runs `PRAGMA quick_check`; if the DB reports structural
|
|
1150
|
+
* corruption it SKIPS the repair entirely (returns `corrupt:true`) rather
|
|
1151
|
+
* than writing into a malformed btree and risking making it worse. The
|
|
1152
|
+
* caller/user should recover via `sqlite3 old.db .recover | sqlite3 new.db`.
|
|
1153
|
+
* - Does NOT checkpoint. mode=ro readers already see committed WAL frames, and
|
|
1154
|
+
* forcing a checkpoint on a DB with a torn WAL could persist latent damage.
|
|
1155
|
+
*
|
|
1156
|
+
* When a repair IS needed and the DB is healthy: creates the table if absent,
|
|
1157
|
+
* seeds the fresh-install default rows, and backfills an accurate
|
|
1158
|
+
* `total_vectors` per namespace. Runs on the existing-DB path of
|
|
1159
|
+
* `initializeMemoryDatabase` (MCP start / `memory init`) and from `ruflo init`.
|
|
1160
|
+
*
|
|
1161
|
+
* Uses better-sqlite3 (WAL-safe, native). If the native module is unavailable
|
|
1162
|
+
* it is a silent no-op — the split statusline query already prevents the count
|
|
1163
|
+
* from zeroing; only the HNSW flag and namespace routing stay degraded.
|
|
1164
|
+
*/
|
|
1165
|
+
/**
|
|
1166
|
+
* Auto-recover a structurally-corrupt memory DB into a clean one, universally
|
|
1167
|
+
* (better-sqlite3 only — no dependency on the external `sqlite3` CLI, which is
|
|
1168
|
+
* absent on many npx hosts). Safe by construction:
|
|
1169
|
+
* 1. Confirms corruption (quick_check) — no-op on a healthy DB.
|
|
1170
|
+
* 2. Acquires an EXCLUSIVE lock (BEGIN IMMEDIATE). If another process is
|
|
1171
|
+
* writing, it SKIPS (returns reason:'writer-active') rather than racing a
|
|
1172
|
+
* writer and losing its in-flight writes — the mistake that must not recur.
|
|
1173
|
+
* 3. Rebuilds a fresh DB table-by-table (schema + rows), skipping any single
|
|
1174
|
+
* table whose pages won't scan so one bad table can't abort the whole
|
|
1175
|
+
* rebuild.
|
|
1176
|
+
* 4. VERIFIES the rebuild (integrity_check == ok AND recovered
|
|
1177
|
+
* memory_entries count >= the readable source count) BEFORE touching the
|
|
1178
|
+
* original.
|
|
1179
|
+
* 5. Backs up the corrupt DB to `<db>.corrupt-<ts>.bak`, then atomically
|
|
1180
|
+
* renames the verified rebuild into place and drops stale -wal/-shm.
|
|
1181
|
+
* On any failure the original + backup are left intact — never destructive.
|
|
1182
|
+
*/
|
|
1183
|
+
export async function recoverMemoryDatabase(dbPath, opts = {}) {
|
|
1184
|
+
if (!dbPath || !fs.existsSync(dbPath))
|
|
1185
|
+
return { recovered: false, reason: 'no-db' };
|
|
1186
|
+
// Fallback for when the in-place rebuild can't produce a verified DB (issue
|
|
1187
|
+
// #2584): rebuild-from-corrupt salvages nothing when the damage is total, so
|
|
1188
|
+
// restore the newest integrity-ok backup instead of leaving the store dead.
|
|
1189
|
+
const restoreFromBackup = async (reason) => {
|
|
1190
|
+
const r = await restoreMemoryDbFromBackup(dbPath, { verbose: opts.verbose });
|
|
1191
|
+
if (r.restored) {
|
|
1192
|
+
return { recovered: true, restoredFromBackup: true, backupPath: r.corruptBackupPath, rows: r.rows, from: r.from };
|
|
1193
|
+
}
|
|
1194
|
+
return { recovered: false, reason, restoreReason: r.skipped };
|
|
1195
|
+
};
|
|
1196
|
+
let Database;
|
|
1197
|
+
try {
|
|
1198
|
+
// Module name behind a variable so TS does not statically resolve the
|
|
1199
|
+
// optional native dep's types at build time (CI may not install them).
|
|
1200
|
+
const mod = 'better-sqlite3';
|
|
1201
|
+
Database = (await import(mod)).default;
|
|
1202
|
+
}
|
|
1203
|
+
catch {
|
|
1204
|
+
return await restoreFromBackup('no-native');
|
|
1205
|
+
}
|
|
1206
|
+
const ts = Date.now();
|
|
1207
|
+
const tmpPath = `${dbPath}.recovering-${ts}`;
|
|
1208
|
+
const bakPath = `${dbPath}.corrupt-${ts}.bak`;
|
|
1209
|
+
let src;
|
|
1210
|
+
let dst;
|
|
1211
|
+
try {
|
|
1212
|
+
src = new Database(dbPath, { timeout: 1500 });
|
|
1213
|
+
// Confirm corruption — never rewrite a healthy DB.
|
|
1214
|
+
const qc = src.prepare('PRAGMA quick_check(1)').get();
|
|
1215
|
+
const qcVal = qc ? String(Object.values(qc)[0] ?? '') : '';
|
|
1216
|
+
if (qcVal.toLowerCase() === 'ok') {
|
|
1217
|
+
src.close();
|
|
1218
|
+
return { recovered: false, reason: 'not-corrupt' };
|
|
1219
|
+
}
|
|
1220
|
+
// Exclusive-writer guard: acquire the write lock. If busy, another process
|
|
1221
|
+
// is writing — do NOT race it. Reading within this txn is still allowed.
|
|
1222
|
+
try {
|
|
1223
|
+
src.exec('BEGIN IMMEDIATE');
|
|
1224
|
+
}
|
|
1225
|
+
catch {
|
|
1226
|
+
src.close();
|
|
1227
|
+
return { recovered: false, reason: 'writer-active' };
|
|
1228
|
+
}
|
|
1229
|
+
let srcRows = 0;
|
|
1230
|
+
try {
|
|
1231
|
+
srcRows = src.prepare('SELECT COUNT(*) AS c FROM memory_entries').get()?.c ?? 0;
|
|
1232
|
+
}
|
|
1233
|
+
catch { /* unreadable */ }
|
|
1234
|
+
try {
|
|
1235
|
+
fs.rmSync(tmpPath, { force: true });
|
|
1236
|
+
}
|
|
1237
|
+
catch { /* fresh */ }
|
|
1238
|
+
dst = new Database(tmpPath);
|
|
1239
|
+
// Copy schema: tables first (so data can be inserted), then indexes/triggers.
|
|
1240
|
+
const objects = src
|
|
1241
|
+
.prepare("SELECT type, name, sql FROM sqlite_master WHERE sql IS NOT NULL AND name NOT LIKE 'sqlite_%'")
|
|
1242
|
+
.all();
|
|
1243
|
+
const tables = objects.filter(o => o.type === 'table');
|
|
1244
|
+
const others = objects.filter(o => o.type !== 'table');
|
|
1245
|
+
for (const t of tables) {
|
|
1246
|
+
try {
|
|
1247
|
+
dst.exec(t.sql);
|
|
1248
|
+
}
|
|
1249
|
+
catch { /* skip an untranslatable table def */ }
|
|
1250
|
+
}
|
|
1251
|
+
// Copy rows table-by-table; a table whose pages won't scan is skipped whole.
|
|
1252
|
+
let copiedEntries = 0;
|
|
1253
|
+
for (const t of tables) {
|
|
1254
|
+
try {
|
|
1255
|
+
const cols = dst.prepare(`PRAGMA table_info("${t.name}")`).all().map(c => c.name);
|
|
1256
|
+
if (!cols.length)
|
|
1257
|
+
continue;
|
|
1258
|
+
const colList = cols.map(c => `"${c}"`).join(',');
|
|
1259
|
+
const placeholders = cols.map(() => '?').join(',');
|
|
1260
|
+
const insert = dst.prepare(`INSERT OR IGNORE INTO "${t.name}" (${colList}) VALUES (${placeholders})`);
|
|
1261
|
+
const rows = src.prepare(`SELECT ${colList} FROM "${t.name}"`).all();
|
|
1262
|
+
const runAll = dst.transaction((rs) => {
|
|
1263
|
+
for (const r of rs)
|
|
1264
|
+
insert.run(cols.map(c => r[c]));
|
|
1265
|
+
});
|
|
1266
|
+
runAll(rows);
|
|
1267
|
+
if (t.name === 'memory_entries')
|
|
1268
|
+
copiedEntries = rows.length;
|
|
1269
|
+
}
|
|
1270
|
+
catch { /* skip a table whose data pages are unreadable */ }
|
|
1271
|
+
}
|
|
1272
|
+
for (const o of others) {
|
|
1273
|
+
try {
|
|
1274
|
+
dst.exec(o.sql);
|
|
1275
|
+
}
|
|
1276
|
+
catch { /* an index over corrupt data — non-fatal */ }
|
|
1277
|
+
}
|
|
1278
|
+
dst.close();
|
|
1279
|
+
dst = null;
|
|
1280
|
+
try {
|
|
1281
|
+
src.exec('ROLLBACK');
|
|
1282
|
+
}
|
|
1283
|
+
catch { /* ignore */ }
|
|
1284
|
+
src.close();
|
|
1285
|
+
src = null;
|
|
1286
|
+
// Verify BEFORE touching the original.
|
|
1287
|
+
const check = new Database(tmpPath, { readonly: true });
|
|
1288
|
+
const integ = String(check.pragma('integrity_check', { simple: true }) ?? '');
|
|
1289
|
+
let dstRows = 0;
|
|
1290
|
+
try {
|
|
1291
|
+
dstRows = check.prepare('SELECT COUNT(*) AS c FROM memory_entries').get()?.c ?? 0;
|
|
1292
|
+
}
|
|
1293
|
+
catch { /* */ }
|
|
1294
|
+
check.close();
|
|
1295
|
+
if (integ.toLowerCase() !== 'ok' || dstRows < srcRows) {
|
|
1296
|
+
try {
|
|
1297
|
+
fs.rmSync(tmpPath, { force: true });
|
|
1298
|
+
}
|
|
1299
|
+
catch { /* */ }
|
|
1300
|
+
if (opts.verbose) {
|
|
1301
|
+
console.log(`memory DB rebuild-in-place failed (integrity=${integ}, rows ${dstRows}/${srcRows}) — trying newest good backup`);
|
|
1302
|
+
}
|
|
1303
|
+
return await restoreFromBackup('verify-failed');
|
|
1304
|
+
}
|
|
1305
|
+
// Back up the corrupt DB, then atomically swap in the verified rebuild.
|
|
1306
|
+
fs.copyFileSync(dbPath, bakPath);
|
|
1307
|
+
fs.renameSync(tmpPath, dbPath);
|
|
1308
|
+
for (const s of ['-wal', '-shm']) {
|
|
1309
|
+
try {
|
|
1310
|
+
fs.rmSync(`${dbPath}${s}`, { force: true });
|
|
1311
|
+
}
|
|
1312
|
+
catch { /* */ }
|
|
1313
|
+
}
|
|
1314
|
+
if (opts.verbose) {
|
|
1315
|
+
console.log(`memory DB auto-recovered: ${dstRows} rows, integrity ok. Corrupt original saved to ${bakPath}`);
|
|
1316
|
+
}
|
|
1317
|
+
return { recovered: true, backupPath: bakPath, rows: dstRows };
|
|
1318
|
+
}
|
|
1319
|
+
catch (e) {
|
|
1320
|
+
try {
|
|
1321
|
+
dst?.close();
|
|
1322
|
+
}
|
|
1323
|
+
catch { /* */ }
|
|
1324
|
+
try {
|
|
1325
|
+
src?.exec('ROLLBACK');
|
|
1326
|
+
}
|
|
1327
|
+
catch { /* */ }
|
|
1328
|
+
try {
|
|
1329
|
+
src?.close();
|
|
1330
|
+
}
|
|
1331
|
+
catch { /* */ }
|
|
1332
|
+
try {
|
|
1333
|
+
fs.rmSync(tmpPath, { force: true });
|
|
1334
|
+
}
|
|
1335
|
+
catch { /* */ }
|
|
1336
|
+
if (opts.verbose)
|
|
1337
|
+
console.log(`memory DB rebuild-in-place error (${e?.message ?? e}) — trying newest good backup`);
|
|
1338
|
+
return await restoreFromBackup('error');
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
export async function repairVectorIndexes(dbPath, opts = {}) {
|
|
1342
|
+
const res = { repaired: false, tableCreated: false, namespaces: [], corrupt: false };
|
|
1343
|
+
if (!dbPath || !fs.existsSync(dbPath))
|
|
1344
|
+
return res;
|
|
1345
|
+
let Database;
|
|
1346
|
+
try {
|
|
1347
|
+
// Module name behind a variable so TS does not statically resolve the
|
|
1348
|
+
// optional native dep's types at build time (CI may not install them).
|
|
1349
|
+
const mod = 'better-sqlite3';
|
|
1350
|
+
Database = (await import(mod)).default;
|
|
1351
|
+
}
|
|
1352
|
+
catch {
|
|
1353
|
+
// Native module absent (e.g. WASM-only host). Statusline fix still covers
|
|
1354
|
+
// the display; nothing to repair here.
|
|
1355
|
+
return res;
|
|
1356
|
+
}
|
|
1357
|
+
let db;
|
|
1358
|
+
try {
|
|
1359
|
+
db = new Database(dbPath, { timeout: 3000 });
|
|
1360
|
+
const tableExists = (name) => (db.prepare("SELECT COUNT(*) AS c FROM sqlite_master WHERE type='table' AND name=?").get(name)?.c ?? 0) > 0;
|
|
1361
|
+
// Nothing to key off if there is no entries table.
|
|
1362
|
+
if (!tableExists('memory_entries')) {
|
|
1363
|
+
db.close();
|
|
1364
|
+
return res;
|
|
1365
|
+
}
|
|
1366
|
+
const hasVectorIndexes = tableExists('vector_indexes');
|
|
1367
|
+
// Namespaces that actually have embeddings.
|
|
1368
|
+
const nsRows = db
|
|
1369
|
+
.prepare("SELECT COALESCE(namespace, 'default') AS ns, COUNT(*) AS c " +
|
|
1370
|
+
'FROM memory_entries WHERE embedding IS NOT NULL GROUP BY ns')
|
|
1371
|
+
.all();
|
|
1372
|
+
// Which of those are already present in vector_indexes? If the table exists
|
|
1373
|
+
// and every embedded namespace already has a row, the DB is already healed
|
|
1374
|
+
// — return WITHOUT writing anything (common path on every MCP start).
|
|
1375
|
+
let needsWrite = !hasVectorIndexes;
|
|
1376
|
+
if (hasVectorIndexes) {
|
|
1377
|
+
const present = new Set(db.prepare('SELECT name FROM vector_indexes').all().map(r => r.name));
|
|
1378
|
+
needsWrite = nsRows.some(r => !present.has(String(r.ns || 'default')));
|
|
1379
|
+
}
|
|
1380
|
+
if (!needsWrite) {
|
|
1381
|
+
db.close();
|
|
1382
|
+
return res;
|
|
1383
|
+
}
|
|
1384
|
+
// A write is needed. GUARD: never write into a structurally corrupt DB —
|
|
1385
|
+
// that risks worsening the damage. quick_check is cheaper than a full
|
|
1386
|
+
// integrity_check and only runs on the rare repair path, not every start.
|
|
1387
|
+
const qc = db.prepare('PRAGMA quick_check(1)').get();
|
|
1388
|
+
const qcVal = qc ? String(Object.values(qc)[0] ?? '') : '';
|
|
1389
|
+
if (qcVal.toLowerCase() !== 'ok') {
|
|
1390
|
+
res.corrupt = true;
|
|
1391
|
+
db.close(); // release our handle before recovery may swap the file
|
|
1392
|
+
if (opts.autoRecover) {
|
|
1393
|
+
// Auto-fix: rebuild the corrupt DB (backup + verify + atomic swap), then
|
|
1394
|
+
// provision vector_indexes on the clean rebuild. This is what makes any
|
|
1395
|
+
// npx-deployed ruflo self-repair a corrupt memory DB on init / MCP start.
|
|
1396
|
+
const rec = await recoverMemoryDatabase(dbPath, { verbose: opts.verbose });
|
|
1397
|
+
if (rec.recovered) {
|
|
1398
|
+
const healed = await repairVectorIndexes(dbPath, { verbose: opts.verbose });
|
|
1399
|
+
return { ...healed, corrupt: true, recovered: true, backupPath: rec.backupPath };
|
|
1400
|
+
}
|
|
1401
|
+
if (opts.verbose) {
|
|
1402
|
+
console.log(`vector_indexes repair skipped — corruption (${qcVal}); auto-recovery not run (${rec.reason ?? 'unknown'})`);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
else if (opts.verbose) {
|
|
1406
|
+
console.log('vector_indexes repair SKIPPED — memory DB reports corruption (' + qcVal + '). ' +
|
|
1407
|
+
'Recover with: sqlite3 <db> .recover | sqlite3 <db>.recovered');
|
|
1408
|
+
}
|
|
1409
|
+
return res;
|
|
1410
|
+
}
|
|
1411
|
+
if (!hasVectorIndexes) {
|
|
1412
|
+
db.exec(`CREATE TABLE IF NOT EXISTS vector_indexes (
|
|
1413
|
+
id TEXT PRIMARY KEY,
|
|
1414
|
+
name TEXT NOT NULL UNIQUE,
|
|
1415
|
+
dimensions INTEGER NOT NULL,
|
|
1416
|
+
metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),
|
|
1417
|
+
hnsw_m INTEGER DEFAULT 16,
|
|
1418
|
+
hnsw_ef_construction INTEGER DEFAULT 200,
|
|
1419
|
+
hnsw_ef_search INTEGER DEFAULT 100,
|
|
1420
|
+
quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),
|
|
1421
|
+
quantization_bits INTEGER DEFAULT 8,
|
|
1422
|
+
total_vectors INTEGER DEFAULT 0,
|
|
1423
|
+
last_rebuild_at INTEGER,
|
|
1424
|
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
1425
|
+
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
1426
|
+
)`);
|
|
1427
|
+
res.tableCreated = true;
|
|
1428
|
+
}
|
|
1429
|
+
// 384 = default ONNX model dim (Xenova/all-MiniLM-L6-v2); HNSW rejects
|
|
1430
|
+
// dim-mismatched inserts, so this must match the stored embeddings (#1947).
|
|
1431
|
+
const ensureRow = db.prepare('INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES (?, ?, 384)');
|
|
1432
|
+
const setCount = db.prepare('UPDATE vector_indexes SET total_vectors = ?, updated_at = ? WHERE name = ?');
|
|
1433
|
+
const backfill = db.transaction(() => {
|
|
1434
|
+
// Parity with a fresh install's seed rows.
|
|
1435
|
+
ensureRow.run('default', 'default');
|
|
1436
|
+
ensureRow.run('patterns', 'patterns');
|
|
1437
|
+
const now = Date.now();
|
|
1438
|
+
for (const r of nsRows) {
|
|
1439
|
+
const ns = String(r.ns || 'default');
|
|
1440
|
+
ensureRow.run(ns, ns);
|
|
1441
|
+
setCount.run(r.c, now, ns);
|
|
1442
|
+
res.namespaces.push(ns);
|
|
1443
|
+
}
|
|
1444
|
+
});
|
|
1445
|
+
backfill();
|
|
1446
|
+
res.repaired = res.tableCreated || res.namespaces.length > 0;
|
|
1447
|
+
db.close();
|
|
1448
|
+
if (opts.verbose && res.repaired) {
|
|
1449
|
+
console.log(`vector_indexes ${res.tableCreated ? 'created' : 'refreshed'} — ` +
|
|
1450
|
+
`backfilled ${res.namespaces.length} namespace(s)`);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
catch (e) {
|
|
1454
|
+
try {
|
|
1455
|
+
db?.close();
|
|
1456
|
+
}
|
|
1457
|
+
catch { /* already closed */ }
|
|
1458
|
+
if (opts.verbose) {
|
|
1459
|
+
console.log(`vector_indexes repair skipped: ${e?.message ?? e}`);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
return res;
|
|
1463
|
+
}
|
|
1464
|
+
/**
|
|
1465
|
+
* Initialize the memory database properly using sql.js
|
|
1466
|
+
*/
|
|
1467
|
+
export async function initializeMemoryDatabase(options) {
|
|
1468
|
+
const { backend = 'hybrid', dbPath: customPath, force = false, verbose = false, migrate = true } = options;
|
|
1469
|
+
const swarmDir = getMemoryRoot();
|
|
1470
|
+
const dbPath = customPath || path.join(swarmDir, 'memory.db');
|
|
1471
|
+
const dbDir = path.dirname(dbPath);
|
|
1472
|
+
try {
|
|
1473
|
+
// Create directory if needed
|
|
1474
|
+
if (!fs.existsSync(dbDir)) {
|
|
1475
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
1476
|
+
}
|
|
1477
|
+
// Check for legacy installations
|
|
1478
|
+
if (migrate) {
|
|
1479
|
+
const legacyCheck = await checkAndMigrateLegacy({ dbPath, verbose });
|
|
1480
|
+
if (legacyCheck.needsMigration && verbose) {
|
|
1481
|
+
console.log(`Found legacy database (v${legacyCheck.legacyVersion}) with ${legacyCheck.legacyEntries} entries`);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
// Check existing database
|
|
1485
|
+
// #1791.6 — Idempotent re-init: if the database already exists and the
|
|
1486
|
+
// caller did not pass --force, treat it as a successful no-op instead of
|
|
1487
|
+
// an error. Callers (CLI, MCP tools, embeddings) can branch on
|
|
1488
|
+
// `alreadyExists` if they want a different message; previous behavior
|
|
1489
|
+
// surfaced an `[ERROR]` and a "Initialization failed" spinner even when
|
|
1490
|
+
// the existing DB was perfectly healthy.
|
|
1491
|
+
if (fs.existsSync(dbPath) && !force) {
|
|
1492
|
+
// #2568-followup: an existing DB may predate `vector_indexes` (or was
|
|
1493
|
+
// written by agentdb directly). Self-heal it here — this branch is hit on
|
|
1494
|
+
// every MCP-server start and `memory init`, so any ruflo repairs itself.
|
|
1495
|
+
// Idempotent + best-effort; never turns a healthy re-init into a failure.
|
|
1496
|
+
const heal = await repairVectorIndexes(dbPath, { verbose, autoRecover: true });
|
|
1497
|
+
return {
|
|
1498
|
+
success: true,
|
|
1499
|
+
alreadyExists: true,
|
|
1500
|
+
backend,
|
|
1501
|
+
dbPath,
|
|
1502
|
+
schemaVersion: '3.0.0',
|
|
1503
|
+
tablesCreated: heal.tableCreated ? ['vector_indexes'] : [],
|
|
1504
|
+
indexesCreated: [],
|
|
1505
|
+
features: {
|
|
1506
|
+
vectorEmbeddings: false,
|
|
1507
|
+
patternLearning: false,
|
|
1508
|
+
temporalDecay: false,
|
|
1509
|
+
hnswIndexing: heal.repaired,
|
|
1510
|
+
migrationTracking: false
|
|
1511
|
+
}
|
|
1512
|
+
};
|
|
1513
|
+
}
|
|
1514
|
+
// Try to use sql.js (WASM SQLite)
|
|
1515
|
+
let db;
|
|
1516
|
+
let usedSqlJs = false;
|
|
1517
|
+
try {
|
|
1518
|
+
// Dynamic import of sql.js
|
|
1519
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
1520
|
+
const SQL = await initSqlJs();
|
|
1521
|
+
// Load existing database or create new
|
|
1522
|
+
if (fs.existsSync(dbPath) && force) {
|
|
1523
|
+
fs.unlinkSync(dbPath);
|
|
1524
|
+
}
|
|
1525
|
+
db = new SQL.Database();
|
|
1526
|
+
usedSqlJs = true;
|
|
1527
|
+
}
|
|
1528
|
+
catch (e) {
|
|
1529
|
+
// sql.js not available, fall back to writing schema file
|
|
1530
|
+
if (verbose) {
|
|
1531
|
+
console.log('sql.js not available, writing schema file for later initialization');
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
if (usedSqlJs && db) {
|
|
1535
|
+
// Execute schema
|
|
1536
|
+
db.run(MEMORY_SCHEMA_V3);
|
|
1537
|
+
// Insert initial metadata
|
|
1538
|
+
db.run(getInitialMetadata(backend));
|
|
1539
|
+
// Save to file
|
|
1540
|
+
const data = db.export();
|
|
1541
|
+
const buffer = Buffer.from(data);
|
|
1542
|
+
writeFileRestricted(dbPath, buffer, { encrypt: true });
|
|
1543
|
+
// Close database
|
|
1544
|
+
db.close();
|
|
1545
|
+
// Also create schema file for reference
|
|
1546
|
+
const schemaPath = path.join(dbDir, 'schema.sql');
|
|
1547
|
+
fs.writeFileSync(schemaPath, MEMORY_SCHEMA_V3 + '\n' + getInitialMetadata(backend));
|
|
1548
|
+
// ADR-053: Activate ControllerRegistry so controllers (ReasoningBank,
|
|
1549
|
+
// SkillLibrary, ExplainableRecall, etc.) are instantiated during init
|
|
1550
|
+
const controllerResult = await activateControllerRegistry(dbPath, verbose);
|
|
1551
|
+
return {
|
|
1552
|
+
success: true,
|
|
1553
|
+
backend,
|
|
1554
|
+
dbPath,
|
|
1555
|
+
schemaVersion: '3.0.0',
|
|
1556
|
+
tablesCreated: [
|
|
1557
|
+
'memory_entries',
|
|
1558
|
+
'patterns',
|
|
1559
|
+
'pattern_history',
|
|
1560
|
+
'trajectories',
|
|
1561
|
+
'trajectory_steps',
|
|
1562
|
+
'migration_state',
|
|
1563
|
+
'sessions',
|
|
1564
|
+
'vector_indexes',
|
|
1565
|
+
'metadata'
|
|
1566
|
+
],
|
|
1567
|
+
indexesCreated: [
|
|
1568
|
+
'idx_memory_namespace',
|
|
1569
|
+
'idx_memory_key',
|
|
1570
|
+
'idx_memory_type',
|
|
1571
|
+
'idx_memory_status',
|
|
1572
|
+
'idx_memory_created',
|
|
1573
|
+
'idx_memory_accessed',
|
|
1574
|
+
'idx_memory_owner',
|
|
1575
|
+
'idx_patterns_type',
|
|
1576
|
+
'idx_patterns_confidence',
|
|
1577
|
+
'idx_patterns_status',
|
|
1578
|
+
'idx_patterns_last_matched',
|
|
1579
|
+
'idx_pattern_history_pattern',
|
|
1580
|
+
'idx_steps_trajectory'
|
|
1581
|
+
],
|
|
1582
|
+
features: {
|
|
1583
|
+
vectorEmbeddings: true,
|
|
1584
|
+
patternLearning: true,
|
|
1585
|
+
temporalDecay: true,
|
|
1586
|
+
hnswIndexing: true,
|
|
1587
|
+
migrationTracking: true
|
|
1588
|
+
},
|
|
1589
|
+
controllers: controllerResult,
|
|
1590
|
+
};
|
|
1591
|
+
}
|
|
1592
|
+
else {
|
|
1593
|
+
// Fall back to schema file approach
|
|
1594
|
+
const schemaPath = path.join(dbDir, 'schema.sql');
|
|
1595
|
+
fs.writeFileSync(schemaPath, MEMORY_SCHEMA_V3 + '\n' + getInitialMetadata(backend));
|
|
1596
|
+
// Create minimal valid SQLite file
|
|
1597
|
+
const sqliteHeader = Buffer.alloc(4096, 0);
|
|
1598
|
+
// SQLite format 3 header
|
|
1599
|
+
Buffer.from('SQLite format 3\0').copy(sqliteHeader, 0);
|
|
1600
|
+
sqliteHeader[16] = 0x10; // page size high byte (4096)
|
|
1601
|
+
sqliteHeader[17] = 0x00; // page size low byte
|
|
1602
|
+
sqliteHeader[18] = 0x01; // file format write version
|
|
1603
|
+
sqliteHeader[19] = 0x01; // file format read version
|
|
1604
|
+
sqliteHeader[24] = 0x00; // max embedded payload
|
|
1605
|
+
sqliteHeader[25] = 0x40;
|
|
1606
|
+
sqliteHeader[26] = 0x20; // min embedded payload
|
|
1607
|
+
sqliteHeader[27] = 0x20; // leaf payload
|
|
1608
|
+
writeFileRestricted(dbPath, sqliteHeader, { encrypt: true });
|
|
1609
|
+
// ADR-053: Activate ControllerRegistry even on fallback path
|
|
1610
|
+
const controllerResult = await activateControllerRegistry(dbPath, verbose);
|
|
1611
|
+
return {
|
|
1612
|
+
success: true,
|
|
1613
|
+
backend,
|
|
1614
|
+
dbPath,
|
|
1615
|
+
schemaVersion: '3.0.0',
|
|
1616
|
+
tablesCreated: [
|
|
1617
|
+
'memory_entries (pending)',
|
|
1618
|
+
'patterns (pending)',
|
|
1619
|
+
'pattern_history (pending)',
|
|
1620
|
+
'trajectories (pending)',
|
|
1621
|
+
'trajectory_steps (pending)',
|
|
1622
|
+
'migration_state (pending)',
|
|
1623
|
+
'sessions (pending)',
|
|
1624
|
+
'vector_indexes (pending)',
|
|
1625
|
+
'metadata (pending)'
|
|
1626
|
+
],
|
|
1627
|
+
indexesCreated: [],
|
|
1628
|
+
features: {
|
|
1629
|
+
vectorEmbeddings: true,
|
|
1630
|
+
patternLearning: true,
|
|
1631
|
+
temporalDecay: true,
|
|
1632
|
+
hnswIndexing: true,
|
|
1633
|
+
migrationTracking: true
|
|
1634
|
+
},
|
|
1635
|
+
controllers: controllerResult,
|
|
1636
|
+
};
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
catch (error) {
|
|
1640
|
+
return {
|
|
1641
|
+
success: false,
|
|
1642
|
+
backend,
|
|
1643
|
+
dbPath,
|
|
1644
|
+
schemaVersion: '3.0.0',
|
|
1645
|
+
tablesCreated: [],
|
|
1646
|
+
indexesCreated: [],
|
|
1647
|
+
features: {
|
|
1648
|
+
vectorEmbeddings: false,
|
|
1649
|
+
patternLearning: false,
|
|
1650
|
+
temporalDecay: false,
|
|
1651
|
+
hnswIndexing: false,
|
|
1652
|
+
migrationTracking: false
|
|
1653
|
+
},
|
|
1654
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1655
|
+
};
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Check if memory database is properly initialized
|
|
1660
|
+
*/
|
|
1661
|
+
export async function checkMemoryInitialization(dbPath) {
|
|
1662
|
+
const swarmDir = getMemoryRoot();
|
|
1663
|
+
const path_ = dbPath || path.join(swarmDir, 'memory.db');
|
|
1664
|
+
if (!fs.existsSync(path_)) {
|
|
1665
|
+
return { initialized: false };
|
|
1666
|
+
}
|
|
1667
|
+
try {
|
|
1668
|
+
// Try to load with sql.js
|
|
1669
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
1670
|
+
const SQL = await initSqlJs();
|
|
1671
|
+
const fileBuffer = fs.readFileSync(path_);
|
|
1672
|
+
const db = new SQL.Database(fileBuffer);
|
|
1673
|
+
// Check for metadata table
|
|
1674
|
+
const tables = db.exec("SELECT name FROM sqlite_master WHERE type='table'");
|
|
1675
|
+
const tableNames = tables[0]?.values?.map(v => v[0]) || [];
|
|
1676
|
+
// Get version
|
|
1677
|
+
let version = 'unknown';
|
|
1678
|
+
let backend = 'unknown';
|
|
1679
|
+
try {
|
|
1680
|
+
const versionResult = db.exec("SELECT value FROM metadata WHERE key='schema_version'");
|
|
1681
|
+
version = versionResult[0]?.values[0]?.[0] || 'unknown';
|
|
1682
|
+
const backendResult = db.exec("SELECT value FROM metadata WHERE key='backend'");
|
|
1683
|
+
backend = backendResult[0]?.values[0]?.[0] || 'unknown';
|
|
1684
|
+
}
|
|
1685
|
+
catch {
|
|
1686
|
+
// Metadata table might not exist
|
|
1687
|
+
}
|
|
1688
|
+
db.close();
|
|
1689
|
+
return {
|
|
1690
|
+
initialized: true,
|
|
1691
|
+
version,
|
|
1692
|
+
backend,
|
|
1693
|
+
features: {
|
|
1694
|
+
vectorEmbeddings: tableNames.includes('vector_indexes'),
|
|
1695
|
+
patternLearning: tableNames.includes('patterns'),
|
|
1696
|
+
temporalDecay: tableNames.includes('pattern_history')
|
|
1697
|
+
},
|
|
1698
|
+
tables: tableNames
|
|
1699
|
+
};
|
|
1700
|
+
}
|
|
1701
|
+
catch {
|
|
1702
|
+
// Could not read database
|
|
1703
|
+
return { initialized: false };
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
/**
|
|
1707
|
+
* Apply temporal decay to patterns
|
|
1708
|
+
* Reduces confidence of patterns that haven't been used recently
|
|
1709
|
+
*/
|
|
1710
|
+
export async function applyTemporalDecay(dbPath) {
|
|
1711
|
+
const swarmDir = getMemoryRoot();
|
|
1712
|
+
const path_ = dbPath || path.join(swarmDir, 'memory.db');
|
|
1713
|
+
try {
|
|
1714
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
1715
|
+
const SQL = await initSqlJs();
|
|
1716
|
+
const fileBuffer = fs.readFileSync(path_);
|
|
1717
|
+
const db = new SQL.Database(fileBuffer);
|
|
1718
|
+
// Apply decay: confidence *= exp(-decay_rate * days_since_last_use)
|
|
1719
|
+
const now = Date.now();
|
|
1720
|
+
const decayQuery = `
|
|
1721
|
+
UPDATE patterns
|
|
1722
|
+
SET
|
|
1723
|
+
confidence = confidence * (1.0 - decay_rate * ((? - COALESCE(last_matched_at, created_at)) / 86400000.0)),
|
|
1724
|
+
updated_at = ?
|
|
1725
|
+
WHERE status = 'active'
|
|
1726
|
+
AND confidence > 0.1
|
|
1727
|
+
AND (? - COALESCE(last_matched_at, created_at)) > 86400000
|
|
1728
|
+
`;
|
|
1729
|
+
db.run(decayQuery, [now, now, now]);
|
|
1730
|
+
const changes = db.getRowsModified();
|
|
1731
|
+
// Save (atomic — issue #2584: a torn full-image flush corrupts the store)
|
|
1732
|
+
const data = db.export();
|
|
1733
|
+
writeFileAtomic(path_, Buffer.from(data));
|
|
1734
|
+
db.close();
|
|
1735
|
+
return {
|
|
1736
|
+
success: true,
|
|
1737
|
+
patternsDecayed: changes
|
|
1738
|
+
};
|
|
1739
|
+
}
|
|
1740
|
+
catch (error) {
|
|
1741
|
+
return {
|
|
1742
|
+
success: false,
|
|
1743
|
+
patternsDecayed: 0,
|
|
1744
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1745
|
+
};
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
let embeddingModelState = null;
|
|
1749
|
+
/**
|
|
1750
|
+
* Lazy load ONNX embedding model
|
|
1751
|
+
* Only loads when first embedding is requested
|
|
1752
|
+
*/
|
|
1753
|
+
export async function loadEmbeddingModel(options) {
|
|
1754
|
+
const { verbose = false } = options || {};
|
|
1755
|
+
const startTime = Date.now();
|
|
1756
|
+
// Already loaded
|
|
1757
|
+
if (embeddingModelState?.loaded) {
|
|
1758
|
+
return {
|
|
1759
|
+
success: true,
|
|
1760
|
+
dimensions: embeddingModelState.dimensions,
|
|
1761
|
+
modelName: 'cached',
|
|
1762
|
+
loadTime: 0
|
|
1763
|
+
};
|
|
1764
|
+
}
|
|
1765
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
1766
|
+
const bridge = await getBridge();
|
|
1767
|
+
if (bridge) {
|
|
1768
|
+
const bridgeResult = await bridge.bridgeLoadEmbeddingModel();
|
|
1769
|
+
if (bridgeResult && bridgeResult.success) {
|
|
1770
|
+
// Mark local state as loaded too so subsequent calls use cache
|
|
1771
|
+
embeddingModelState = {
|
|
1772
|
+
loaded: true,
|
|
1773
|
+
model: null, // Bridge handles embedding
|
|
1774
|
+
tokenizer: null,
|
|
1775
|
+
dimensions: bridgeResult.dimensions
|
|
1776
|
+
};
|
|
1777
|
+
return bridgeResult;
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
try {
|
|
1781
|
+
// ADR-094: prefer @huggingface/transformers (clears protobufjs <7.5.5
|
|
1782
|
+
// critical RCE chain), fall back to legacy @xenova/transformers.
|
|
1783
|
+
// Inlined here rather than depending on @claude-flow/embeddings to
|
|
1784
|
+
// avoid a circular optional-dep at install time; the logic mirrors
|
|
1785
|
+
// @claude-flow/embeddings/src/transformers-loader.ts.
|
|
1786
|
+
let transformersSource = null;
|
|
1787
|
+
let pipelineFn = null;
|
|
1788
|
+
{
|
|
1789
|
+
const tryLoad = async (specifier) => {
|
|
1790
|
+
try {
|
|
1791
|
+
return (await import(specifier));
|
|
1792
|
+
}
|
|
1793
|
+
catch {
|
|
1794
|
+
return null;
|
|
1795
|
+
}
|
|
1796
|
+
};
|
|
1797
|
+
const hf = await tryLoad('@huggingface/transformers');
|
|
1798
|
+
if (hf && typeof hf.pipeline === 'function') {
|
|
1799
|
+
pipelineFn = hf.pipeline;
|
|
1800
|
+
transformersSource = '@huggingface/transformers';
|
|
1801
|
+
}
|
|
1802
|
+
else {
|
|
1803
|
+
const xen = await tryLoad('@xenova/transformers');
|
|
1804
|
+
if (xen && typeof xen.pipeline === 'function') {
|
|
1805
|
+
pipelineFn = xen.pipeline;
|
|
1806
|
+
transformersSource = '@xenova/transformers';
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
if (pipelineFn && transformersSource) {
|
|
1811
|
+
// #2461: pipelineFn() can throw with `fetch failed` on Windows behind
|
|
1812
|
+
// a corporate proxy / strict firewall when transformers tries to pull
|
|
1813
|
+
// the model files from the HuggingFace CDN. Without the catch, that
|
|
1814
|
+
// throw escapes the outer try and aborts loadEmbeddingModel() with
|
|
1815
|
+
// success=false BEFORE we reach the (working) ruvector ONNX fallback
|
|
1816
|
+
// below — leaving embeddingModelState=null, which then crashes
|
|
1817
|
+
// generateLocalEmbedding() with "Cannot read properties of null
|
|
1818
|
+
// (reading 'model')" on every memory store / search call.
|
|
1819
|
+
try {
|
|
1820
|
+
if (verbose) {
|
|
1821
|
+
console.log(`Loading ONNX embedding model via ${transformersSource} (all-MiniLM-L6-v2)...`);
|
|
1822
|
+
}
|
|
1823
|
+
const embedder = await pipelineFn('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
|
|
1824
|
+
embeddingModelState = {
|
|
1825
|
+
loaded: true,
|
|
1826
|
+
model: embedder,
|
|
1827
|
+
tokenizer: null,
|
|
1828
|
+
dimensions: 384 // MiniLM-L6 produces 384-dim vectors
|
|
1829
|
+
};
|
|
1830
|
+
return {
|
|
1831
|
+
success: true,
|
|
1832
|
+
dimensions: 384,
|
|
1833
|
+
modelName: 'Xenova/all-MiniLM-L6-v2',
|
|
1834
|
+
loadTime: Date.now() - startTime
|
|
1835
|
+
};
|
|
1836
|
+
}
|
|
1837
|
+
catch (err) {
|
|
1838
|
+
if (verbose) {
|
|
1839
|
+
console.warn(`${transformersSource} pipeline init failed (${err instanceof Error ? err.message : String(err)}); ` +
|
|
1840
|
+
'falling through to ruvector ONNX / agentic-flow / hash fallback.');
|
|
1841
|
+
}
|
|
1842
|
+
// Intentional fall-through to the next embedder branch.
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
// Fallback: Check for agentic-flow ReasoningBank embeddings (v3)
|
|
1846
|
+
const reasoningBank = await import('agentic-flow/reasoningbank').catch(() => null);
|
|
1847
|
+
if (reasoningBank?.computeEmbedding) {
|
|
1848
|
+
if (verbose) {
|
|
1849
|
+
console.log('Loading agentic-flow ReasoningBank embedding model...');
|
|
1850
|
+
}
|
|
1851
|
+
embeddingModelState = {
|
|
1852
|
+
loaded: true,
|
|
1853
|
+
model: { embed: reasoningBank.computeEmbedding },
|
|
1854
|
+
tokenizer: null,
|
|
1855
|
+
dimensions: 768
|
|
1856
|
+
};
|
|
1857
|
+
return {
|
|
1858
|
+
success: true,
|
|
1859
|
+
dimensions: 768,
|
|
1860
|
+
modelName: 'agentic-flow/reasoningbank',
|
|
1861
|
+
loadTime: Date.now() - startTime
|
|
1862
|
+
};
|
|
1863
|
+
}
|
|
1864
|
+
// Fallback: Check for ruvector ONNX embedder (bundled MiniLM-L6-v2 since v0.2.15)
|
|
1865
|
+
// v0.2.16: LoRA B=0 fix makes AdaptiveEmbedder safe (identity when untrained)
|
|
1866
|
+
// Note: isReady() returns false until first embed() call (lazy init), so we
|
|
1867
|
+
// skip the isReady() gate and verify with a probe embed instead.
|
|
1868
|
+
const ruvector = await import('ruvector').catch(() => null);
|
|
1869
|
+
if (ruvector?.initOnnxEmbedder) {
|
|
1870
|
+
try {
|
|
1871
|
+
await ruvector.initOnnxEmbedder();
|
|
1872
|
+
// Fallback: OptimizedOnnxEmbedder (raw ONNX, lazy-inits on first embed)
|
|
1873
|
+
const onnxEmb = ruvector.getOptimizedOnnxEmbedder?.();
|
|
1874
|
+
if (onnxEmb?.embed) {
|
|
1875
|
+
// Probe embed to trigger lazy ONNX init and verify it works
|
|
1876
|
+
const probe = await onnxEmb.embed('test');
|
|
1877
|
+
if (probe && probe.length > 0 && (Array.isArray(probe) ? probe.some((v) => v !== 0) : true)) {
|
|
1878
|
+
if (verbose) {
|
|
1879
|
+
console.log(`Loading ruvector ONNX embedder (all-MiniLM-L6-v2, ${probe.length}d)...`);
|
|
1880
|
+
}
|
|
1881
|
+
embeddingModelState = {
|
|
1882
|
+
loaded: true,
|
|
1883
|
+
model: (text) => onnxEmb.embed(text),
|
|
1884
|
+
tokenizer: null,
|
|
1885
|
+
dimensions: probe.length || 384
|
|
1886
|
+
};
|
|
1887
|
+
return {
|
|
1888
|
+
success: true,
|
|
1889
|
+
dimensions: probe.length || 384,
|
|
1890
|
+
modelName: 'ruvector/onnx',
|
|
1891
|
+
loadTime: Date.now() - startTime
|
|
1892
|
+
};
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
catch {
|
|
1897
|
+
// ruvector ONNX init failed, continue to next fallback
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
// Legacy fallback: Check for agentic-flow core embeddings
|
|
1901
|
+
const agenticFlow = await import('agentic-flow').catch(() => null);
|
|
1902
|
+
if (agenticFlow && agenticFlow.embeddings) {
|
|
1903
|
+
if (verbose) {
|
|
1904
|
+
console.log('Loading agentic-flow embedding model...');
|
|
1905
|
+
}
|
|
1906
|
+
embeddingModelState = {
|
|
1907
|
+
loaded: true,
|
|
1908
|
+
model: agenticFlow.embeddings,
|
|
1909
|
+
tokenizer: null,
|
|
1910
|
+
dimensions: 768
|
|
1911
|
+
};
|
|
1912
|
+
return {
|
|
1913
|
+
success: true,
|
|
1914
|
+
dimensions: 768,
|
|
1915
|
+
modelName: 'agentic-flow',
|
|
1916
|
+
loadTime: Date.now() - startTime
|
|
1917
|
+
};
|
|
1918
|
+
}
|
|
1919
|
+
// No ONNX model available - use fallback
|
|
1920
|
+
embeddingModelState = {
|
|
1921
|
+
loaded: true,
|
|
1922
|
+
model: null, // Will use simple hash-based fallback
|
|
1923
|
+
tokenizer: null,
|
|
1924
|
+
dimensions: 128 // Smaller fallback dimensions
|
|
1925
|
+
};
|
|
1926
|
+
return {
|
|
1927
|
+
success: true,
|
|
1928
|
+
dimensions: 128,
|
|
1929
|
+
modelName: 'hash-fallback',
|
|
1930
|
+
loadTime: Date.now() - startTime
|
|
1931
|
+
};
|
|
1932
|
+
}
|
|
1933
|
+
catch (error) {
|
|
1934
|
+
return {
|
|
1935
|
+
success: false,
|
|
1936
|
+
dimensions: 0,
|
|
1937
|
+
modelName: 'none',
|
|
1938
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
/**
|
|
1943
|
+
* Generate real embedding for text
|
|
1944
|
+
* Uses ONNX model if available, falls back to deterministic hash
|
|
1945
|
+
*
|
|
1946
|
+
* AUDIT #3: the `backend` field is the authoritative signal for whether the
|
|
1947
|
+
* returned vector carries real ONNX semantics ('onnx') or the deterministic
|
|
1948
|
+
* hash fallback ('mock'). The hash fallback produces inverted/meaningless
|
|
1949
|
+
* semantics, so operators MUST be able to tell the two apart even when the
|
|
1950
|
+
* `model` string reports a real model name (e.g. the AgentDB bridge always
|
|
1951
|
+
* labels its output 'Xenova/all-MiniLM-L6-v2' regardless of whether AgentDB's
|
|
1952
|
+
* own embedder is real or stubbed). Set `backend` truthfully by the path that
|
|
1953
|
+
* actually produced the vector. Do NOT change the embedding math.
|
|
1954
|
+
*/
|
|
1955
|
+
export async function generateEmbedding(text) {
|
|
1956
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
1957
|
+
const bridge = await getBridge();
|
|
1958
|
+
if (bridge) {
|
|
1959
|
+
const bridgeResult = await bridge.bridgeGenerateEmbedding(text);
|
|
1960
|
+
if (bridgeResult) {
|
|
1961
|
+
// The bridge labels its output with a real model name unconditionally;
|
|
1962
|
+
// honor the backend it reports if present, otherwise treat a real model
|
|
1963
|
+
// name as ONNX (the bridge only returns when AgentDB's embedder exists).
|
|
1964
|
+
const backend = bridgeResult.backend ?? 'onnx';
|
|
1965
|
+
return { ...bridgeResult, backend };
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
return generateLocalEmbedding(text);
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Generate an embedding using ONLY the local model chain (transformers.js /
|
|
1972
|
+
* ruvector ONNX / hash fallback) — never the AgentDB bridge.
|
|
1973
|
+
*
|
|
1974
|
+
* #2312: this MUST stay bridge-free. `memory-bridge.ts` rescues a degraded
|
|
1975
|
+
* agentdb embedder by delegating to this module; if that delegation went
|
|
1976
|
+
* through `generateEmbedding` (bridge-first), the call would re-enter the
|
|
1977
|
+
* patched `agentdb.embedder.embed` and recurse unboundedly:
|
|
1978
|
+
*
|
|
1979
|
+
* generateEmbedding → bridgeGenerateEmbedding → embedder.embed (patched)
|
|
1980
|
+
* → generateEmbedding → … (heap OOM at ~4 GB on CI, no stack overflow
|
|
1981
|
+
* because the cycle is async/microtask-driven)
|
|
1982
|
+
*
|
|
1983
|
+
* Keeping the local chain as its own export breaks that cycle structurally.
|
|
1984
|
+
*/
|
|
1985
|
+
export async function generateLocalEmbedding(text) {
|
|
1986
|
+
// Ensure model is loaded
|
|
1987
|
+
if (!embeddingModelState?.loaded) {
|
|
1988
|
+
await loadEmbeddingModel();
|
|
1989
|
+
}
|
|
1990
|
+
// #2461: loadEmbeddingModel() can leave embeddingModelState null when an
|
|
1991
|
+
// earlier loader (transformers fetch, ruvector init) throws and we never
|
|
1992
|
+
// reach the hash-fallback assignment. Don't lie with a `!` non-null
|
|
1993
|
+
// assertion — fall back to a synthetic hash-fallback state so we degrade
|
|
1994
|
+
// to the deterministic 128-dim hash embedding instead of crashing the
|
|
1995
|
+
// entire memory store/search path with "reading property 'model' of null".
|
|
1996
|
+
const state = embeddingModelState ?? {
|
|
1997
|
+
loaded: true,
|
|
1998
|
+
model: null,
|
|
1999
|
+
tokenizer: null,
|
|
2000
|
+
dimensions: 128,
|
|
2001
|
+
};
|
|
2002
|
+
// Use ONNX model if available
|
|
2003
|
+
if (state.model && typeof state.model === 'function') {
|
|
2004
|
+
try {
|
|
2005
|
+
const output = await state.model(text, { pooling: 'mean', normalize: true });
|
|
2006
|
+
// Handle both @xenova/transformers (output.data) and ruvector (plain array) formats
|
|
2007
|
+
const embedding = output?.data
|
|
2008
|
+
? Array.from(output.data)
|
|
2009
|
+
: Array.isArray(output) ? output : null;
|
|
2010
|
+
if (embedding) {
|
|
2011
|
+
return {
|
|
2012
|
+
embedding,
|
|
2013
|
+
dimensions: embedding.length,
|
|
2014
|
+
model: 'onnx',
|
|
2015
|
+
backend: 'onnx'
|
|
2016
|
+
};
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
catch {
|
|
2020
|
+
// Fall through to fallback
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
// Deterministic hash-based fallback (for testing/demo without ONNX).
|
|
2024
|
+
// AUDIT #3: backend='mock' — these vectors do NOT carry real semantics.
|
|
2025
|
+
(await import('./embedding-policy.js')).enforceNoStub('memory-initializer.generateLocalEmbedding'); // "no stubs" strict mode
|
|
2026
|
+
const embedding = generateHashEmbedding(text, state.dimensions);
|
|
2027
|
+
return {
|
|
2028
|
+
embedding,
|
|
2029
|
+
dimensions: state.dimensions,
|
|
2030
|
+
model: 'hash-fallback',
|
|
2031
|
+
backend: 'mock'
|
|
2032
|
+
};
|
|
2033
|
+
}
|
|
2034
|
+
/**
|
|
2035
|
+
* Generate embeddings for multiple texts
|
|
2036
|
+
* Uses parallel execution for API-based providers (2-4x faster)
|
|
2037
|
+
* Note: Local ONNX inference is CPU-bound, so parallelism has limited benefit
|
|
2038
|
+
*
|
|
2039
|
+
* @param texts - Array of texts to embed
|
|
2040
|
+
* @param options - Batch options
|
|
2041
|
+
* @returns Array of embedding results with timing info
|
|
2042
|
+
*/
|
|
2043
|
+
export async function generateBatchEmbeddings(texts, options) {
|
|
2044
|
+
const { concurrency = texts.length, onProgress } = options || {};
|
|
2045
|
+
const startTime = Date.now();
|
|
2046
|
+
// Ensure model is loaded first (prevents cold start in parallel)
|
|
2047
|
+
if (!embeddingModelState?.loaded) {
|
|
2048
|
+
await loadEmbeddingModel();
|
|
2049
|
+
}
|
|
2050
|
+
// Process in parallel with optional concurrency limit
|
|
2051
|
+
if (concurrency >= texts.length) {
|
|
2052
|
+
// Full parallelism
|
|
2053
|
+
const embeddings = await Promise.all(texts.map(async (text, i) => {
|
|
2054
|
+
const result = await generateEmbedding(text);
|
|
2055
|
+
onProgress?.(i + 1, texts.length);
|
|
2056
|
+
return { text, ...result };
|
|
2057
|
+
}));
|
|
2058
|
+
const totalTime = Date.now() - startTime;
|
|
2059
|
+
return {
|
|
2060
|
+
results: embeddings,
|
|
2061
|
+
totalTime,
|
|
2062
|
+
avgTime: totalTime / texts.length
|
|
2063
|
+
};
|
|
2064
|
+
}
|
|
2065
|
+
// Limited concurrency using chunking
|
|
2066
|
+
const results = [];
|
|
2067
|
+
let completed = 0;
|
|
2068
|
+
for (let i = 0; i < texts.length; i += concurrency) {
|
|
2069
|
+
const chunk = texts.slice(i, i + concurrency);
|
|
2070
|
+
const chunkResults = await Promise.all(chunk.map(async (text) => {
|
|
2071
|
+
const result = await generateEmbedding(text);
|
|
2072
|
+
completed++;
|
|
2073
|
+
onProgress?.(completed, texts.length);
|
|
2074
|
+
return { text, ...result };
|
|
2075
|
+
}));
|
|
2076
|
+
results.push(...chunkResults);
|
|
2077
|
+
}
|
|
2078
|
+
const totalTime = Date.now() - startTime;
|
|
2079
|
+
return {
|
|
2080
|
+
results,
|
|
2081
|
+
totalTime,
|
|
2082
|
+
avgTime: totalTime / texts.length
|
|
2083
|
+
};
|
|
2084
|
+
}
|
|
2085
|
+
/**
|
|
2086
|
+
* Generate deterministic hash-based embedding
|
|
2087
|
+
* Not semantic, but deterministic and useful for testing
|
|
2088
|
+
*/
|
|
2089
|
+
function generateHashEmbedding(text, dimensions) {
|
|
2090
|
+
const embedding = new Array(dimensions).fill(0);
|
|
2091
|
+
// Simple hash-based approach for reproducibility
|
|
2092
|
+
const words = text.toLowerCase().split(/\s+/);
|
|
2093
|
+
for (let i = 0; i < words.length; i++) {
|
|
2094
|
+
const word = words[i];
|
|
2095
|
+
for (let j = 0; j < word.length; j++) {
|
|
2096
|
+
const charCode = word.charCodeAt(j);
|
|
2097
|
+
const idx = (charCode * (i + 1) * (j + 1)) % dimensions;
|
|
2098
|
+
embedding[idx] += Math.sin(charCode * 0.1) * 0.1;
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
// Normalize to unit vector
|
|
2102
|
+
const magnitude = Math.sqrt(embedding.reduce((sum, v) => sum + v * v, 0)) || 1;
|
|
2103
|
+
return embedding.map(v => v / magnitude);
|
|
2104
|
+
}
|
|
2105
|
+
/**
|
|
2106
|
+
* Verify memory initialization works correctly
|
|
2107
|
+
* Tests: write, read, search, patterns
|
|
2108
|
+
*/
|
|
2109
|
+
export async function verifyMemoryInit(dbPath, options) {
|
|
2110
|
+
const { verbose = false } = options || {};
|
|
2111
|
+
const tests = [];
|
|
2112
|
+
try {
|
|
2113
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2114
|
+
const SQL = await initSqlJs();
|
|
2115
|
+
const fs = await import('fs');
|
|
2116
|
+
// Load database
|
|
2117
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2118
|
+
const db = new SQL.Database(fileBuffer);
|
|
2119
|
+
// Test 1: Schema verification
|
|
2120
|
+
const schemaStart = Date.now();
|
|
2121
|
+
const tables = db.exec("SELECT name FROM sqlite_master WHERE type='table'");
|
|
2122
|
+
const tableNames = tables[0]?.values?.map(v => v[0]) || [];
|
|
2123
|
+
const expectedTables = ['memory_entries', 'patterns', 'metadata', 'vector_indexes'];
|
|
2124
|
+
const missingTables = expectedTables.filter(t => !tableNames.includes(t));
|
|
2125
|
+
tests.push({
|
|
2126
|
+
name: 'Schema verification',
|
|
2127
|
+
passed: missingTables.length === 0,
|
|
2128
|
+
details: missingTables.length > 0 ? `Missing: ${missingTables.join(', ')}` : `${tableNames.length} tables found`,
|
|
2129
|
+
duration: Date.now() - schemaStart
|
|
2130
|
+
});
|
|
2131
|
+
// Test 2: Write entry
|
|
2132
|
+
const writeStart = Date.now();
|
|
2133
|
+
const testId = `test_${Date.now()}`;
|
|
2134
|
+
const testKey = 'verification_test';
|
|
2135
|
+
const testValue = 'This is a verification test entry for memory initialization';
|
|
2136
|
+
try {
|
|
2137
|
+
db.run(`
|
|
2138
|
+
INSERT INTO memory_entries (id, key, namespace, content, type, created_at, updated_at)
|
|
2139
|
+
VALUES (?, ?, 'test', ?, 'semantic', ?, ?)
|
|
2140
|
+
`, [testId, testKey, testValue, Date.now(), Date.now()]);
|
|
2141
|
+
tests.push({
|
|
2142
|
+
name: 'Write entry',
|
|
2143
|
+
passed: true,
|
|
2144
|
+
details: 'Entry written successfully',
|
|
2145
|
+
duration: Date.now() - writeStart
|
|
2146
|
+
});
|
|
2147
|
+
}
|
|
2148
|
+
catch (e) {
|
|
2149
|
+
tests.push({
|
|
2150
|
+
name: 'Write entry',
|
|
2151
|
+
passed: false,
|
|
2152
|
+
details: e instanceof Error ? e.message : 'Write failed',
|
|
2153
|
+
duration: Date.now() - writeStart
|
|
2154
|
+
});
|
|
2155
|
+
}
|
|
2156
|
+
// Test 3: Read entry
|
|
2157
|
+
const readStart = Date.now();
|
|
2158
|
+
try {
|
|
2159
|
+
const result = db.exec(`SELECT content FROM memory_entries WHERE id = ?`, [testId]);
|
|
2160
|
+
const content = result[0]?.values[0]?.[0];
|
|
2161
|
+
tests.push({
|
|
2162
|
+
name: 'Read entry',
|
|
2163
|
+
passed: content === testValue,
|
|
2164
|
+
details: content === testValue ? 'Content matches' : 'Content mismatch',
|
|
2165
|
+
duration: Date.now() - readStart
|
|
2166
|
+
});
|
|
2167
|
+
}
|
|
2168
|
+
catch (e) {
|
|
2169
|
+
tests.push({
|
|
2170
|
+
name: 'Read entry',
|
|
2171
|
+
passed: false,
|
|
2172
|
+
details: e instanceof Error ? e.message : 'Read failed',
|
|
2173
|
+
duration: Date.now() - readStart
|
|
2174
|
+
});
|
|
2175
|
+
}
|
|
2176
|
+
// Test 4: Write with embedding
|
|
2177
|
+
const embeddingStart = Date.now();
|
|
2178
|
+
try {
|
|
2179
|
+
const { embedding, dimensions, model } = await generateEmbedding(testValue);
|
|
2180
|
+
const embeddingJson = JSON.stringify(embedding);
|
|
2181
|
+
db.run(`
|
|
2182
|
+
UPDATE memory_entries
|
|
2183
|
+
SET embedding = ?, embedding_dimensions = ?, embedding_model = ?
|
|
2184
|
+
WHERE id = ?
|
|
2185
|
+
`, [embeddingJson, dimensions, model, testId]);
|
|
2186
|
+
tests.push({
|
|
2187
|
+
name: 'Generate embedding',
|
|
2188
|
+
passed: true,
|
|
2189
|
+
details: `${dimensions}-dim vector (${model})`,
|
|
2190
|
+
duration: Date.now() - embeddingStart
|
|
2191
|
+
});
|
|
2192
|
+
}
|
|
2193
|
+
catch (e) {
|
|
2194
|
+
tests.push({
|
|
2195
|
+
name: 'Generate embedding',
|
|
2196
|
+
passed: false,
|
|
2197
|
+
details: e instanceof Error ? e.message : 'Embedding failed',
|
|
2198
|
+
duration: Date.now() - embeddingStart
|
|
2199
|
+
});
|
|
2200
|
+
}
|
|
2201
|
+
// Test 5: Pattern storage
|
|
2202
|
+
const patternStart = Date.now();
|
|
2203
|
+
try {
|
|
2204
|
+
const patternId = `pattern_${Date.now()}`;
|
|
2205
|
+
db.run(`
|
|
2206
|
+
INSERT INTO patterns (id, name, pattern_type, condition, action, confidence, created_at, updated_at)
|
|
2207
|
+
VALUES (?, 'test-pattern', 'task-routing', 'test condition', 'test action', 0.5, ?, ?)
|
|
2208
|
+
`, [patternId, Date.now(), Date.now()]);
|
|
2209
|
+
tests.push({
|
|
2210
|
+
name: 'Pattern storage',
|
|
2211
|
+
passed: true,
|
|
2212
|
+
details: 'Pattern stored with confidence scoring',
|
|
2213
|
+
duration: Date.now() - patternStart
|
|
2214
|
+
});
|
|
2215
|
+
// Cleanup test pattern
|
|
2216
|
+
db.run(`DELETE FROM patterns WHERE id = ?`, [patternId]);
|
|
2217
|
+
}
|
|
2218
|
+
catch (e) {
|
|
2219
|
+
tests.push({
|
|
2220
|
+
name: 'Pattern storage',
|
|
2221
|
+
passed: false,
|
|
2222
|
+
details: e instanceof Error ? e.message : 'Pattern storage failed',
|
|
2223
|
+
duration: Date.now() - patternStart
|
|
2224
|
+
});
|
|
2225
|
+
}
|
|
2226
|
+
// Test 6: Vector index configuration
|
|
2227
|
+
const indexStart = Date.now();
|
|
2228
|
+
try {
|
|
2229
|
+
const indexResult = db.exec(`SELECT name, dimensions, hnsw_m, hnsw_ef_construction FROM vector_indexes`);
|
|
2230
|
+
const indexes = indexResult[0]?.values || [];
|
|
2231
|
+
tests.push({
|
|
2232
|
+
name: 'Vector index config',
|
|
2233
|
+
passed: indexes.length > 0,
|
|
2234
|
+
details: `${indexes.length} indexes configured (HNSW M=16, ef=200)`,
|
|
2235
|
+
duration: Date.now() - indexStart
|
|
2236
|
+
});
|
|
2237
|
+
}
|
|
2238
|
+
catch (e) {
|
|
2239
|
+
tests.push({
|
|
2240
|
+
name: 'Vector index config',
|
|
2241
|
+
passed: false,
|
|
2242
|
+
details: e instanceof Error ? e.message : 'Index check failed',
|
|
2243
|
+
duration: Date.now() - indexStart
|
|
2244
|
+
});
|
|
2245
|
+
}
|
|
2246
|
+
// Verification is read-only: sql.js holds an in-memory copy; discarding it on
|
|
2247
|
+
// close() leaves the on-disk DB untouched. Writing back here would race the
|
|
2248
|
+
// still-open better-sqlite3 handle (WAL) owned by ControllerRegistry /
|
|
2249
|
+
// repairVectorIndexes — atomic rename fails with EPERM on Windows (#2596)
|
|
2250
|
+
// and risks clobbering concurrent writes on all platforms.
|
|
2251
|
+
db.close();
|
|
2252
|
+
const passed = tests.filter(t => t.passed).length;
|
|
2253
|
+
const failed = tests.filter(t => !t.passed).length;
|
|
2254
|
+
return {
|
|
2255
|
+
success: failed === 0,
|
|
2256
|
+
tests,
|
|
2257
|
+
summary: {
|
|
2258
|
+
passed,
|
|
2259
|
+
failed,
|
|
2260
|
+
total: tests.length
|
|
2261
|
+
}
|
|
2262
|
+
};
|
|
2263
|
+
}
|
|
2264
|
+
catch (error) {
|
|
2265
|
+
return {
|
|
2266
|
+
success: false,
|
|
2267
|
+
tests: [{
|
|
2268
|
+
name: 'Database access',
|
|
2269
|
+
passed: false,
|
|
2270
|
+
details: error instanceof Error ? error.message : 'Unknown error'
|
|
2271
|
+
}],
|
|
2272
|
+
summary: { passed: 0, failed: 1, total: 1 }
|
|
2273
|
+
};
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
/**
|
|
2277
|
+
* Store an entry directly using sql.js
|
|
2278
|
+
* This bypasses MCP and writes directly to the database
|
|
2279
|
+
*/
|
|
2280
|
+
export async function storeEntry(options) {
|
|
2281
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
2282
|
+
const bridge = await getBridge();
|
|
2283
|
+
if (bridge) {
|
|
2284
|
+
const bridgeResult = await bridge.bridgeStoreEntry(options);
|
|
2285
|
+
if (bridgeResult) {
|
|
2286
|
+
// Keep HNSW index in sync with bridge-stored entries
|
|
2287
|
+
if (bridgeResult.rawEmbedding && bridgeResult.success) {
|
|
2288
|
+
const ns = options.namespace || 'default';
|
|
2289
|
+
await addToHNSWIndex(bridgeResult.id, bridgeResult.rawEmbedding, {
|
|
2290
|
+
id: bridgeResult.id,
|
|
2291
|
+
key: options.key,
|
|
2292
|
+
namespace: ns,
|
|
2293
|
+
content: options.value,
|
|
2294
|
+
}).catch(() => { });
|
|
2295
|
+
}
|
|
2296
|
+
return bridgeResult;
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
// Fallback: raw sql.js
|
|
2300
|
+
const { key, value, namespace = 'default', generateEmbeddingFlag = true, tags = [], ttl, dbPath: customPath, upsert = false } = options;
|
|
2301
|
+
const swarmDir = getMemoryRoot();
|
|
2302
|
+
const dbPath = customPath ? path.resolve(customPath) : path.join(swarmDir, 'memory.db');
|
|
2303
|
+
try {
|
|
2304
|
+
if (!fs.existsSync(dbPath)) {
|
|
2305
|
+
return { success: false, id: '', error: 'Database not initialized. Run: claude-flow memory init' };
|
|
2306
|
+
}
|
|
2307
|
+
// Ensure schema has all required columns (migration for older DBs)
|
|
2308
|
+
await ensureSchemaColumns(dbPath);
|
|
2309
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2310
|
+
const SQL = await initSqlJs();
|
|
2311
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2312
|
+
const db = new SQL.Database(fileBuffer);
|
|
2313
|
+
const id = `entry_${Date.now()}_${Math.random().toString(36).substring(7)}`;
|
|
2314
|
+
const now = Date.now();
|
|
2315
|
+
// Generate embedding if requested
|
|
2316
|
+
let embeddingJson = null;
|
|
2317
|
+
let embeddingDimensions = null;
|
|
2318
|
+
let embeddingModel = null;
|
|
2319
|
+
if (generateEmbeddingFlag && value.length > 0) {
|
|
2320
|
+
const embResult = await generateEmbedding(value);
|
|
2321
|
+
embeddingJson = JSON.stringify(embResult.embedding);
|
|
2322
|
+
embeddingDimensions = embResult.dimensions;
|
|
2323
|
+
embeddingModel = embResult.model;
|
|
2324
|
+
}
|
|
2325
|
+
// #1941: provision a `vector_indexes` row for this namespace before the
|
|
2326
|
+
// entry insert. The HNSW lookup uses this table to find which namespaces
|
|
2327
|
+
// are indexed — without a row, `memory_search({namespace:"X"})` returns
|
|
2328
|
+
// 0 even when memory_entries holds matching rows. INSERT OR IGNORE
|
|
2329
|
+
// preserves the existing `default` / `patterns` rows.
|
|
2330
|
+
try {
|
|
2331
|
+
db.run(`INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES (?, ?, ?)`, [namespace, namespace, embeddingDimensions ?? 384]);
|
|
2332
|
+
}
|
|
2333
|
+
catch { /* vector_indexes may not exist on legacy DBs — fall through */ }
|
|
2334
|
+
// Insert or update entry (upsert mode uses REPLACE)
|
|
2335
|
+
const insertSql = upsert
|
|
2336
|
+
? `INSERT OR REPLACE INTO memory_entries (
|
|
2337
|
+
id, key, namespace, content, type,
|
|
2338
|
+
embedding, embedding_dimensions, embedding_model,
|
|
2339
|
+
tags, metadata, created_at, updated_at, expires_at, status
|
|
2340
|
+
) VALUES (?, ?, ?, ?, 'semantic', ?, ?, ?, ?, ?, ?, ?, ?, 'active')`
|
|
2341
|
+
: `INSERT INTO memory_entries (
|
|
2342
|
+
id, key, namespace, content, type,
|
|
2343
|
+
embedding, embedding_dimensions, embedding_model,
|
|
2344
|
+
tags, metadata, created_at, updated_at, expires_at, status
|
|
2345
|
+
) VALUES (?, ?, ?, ?, 'semantic', ?, ?, ?, ?, ?, ?, ?, ?, 'active')`;
|
|
2346
|
+
db.run(insertSql, [
|
|
2347
|
+
id,
|
|
2348
|
+
key,
|
|
2349
|
+
namespace,
|
|
2350
|
+
value,
|
|
2351
|
+
embeddingJson,
|
|
2352
|
+
embeddingDimensions,
|
|
2353
|
+
embeddingModel,
|
|
2354
|
+
tags.length > 0 ? JSON.stringify(tags) : null,
|
|
2355
|
+
'{}',
|
|
2356
|
+
now,
|
|
2357
|
+
now,
|
|
2358
|
+
ttl ? now + (ttl * 1000) : null
|
|
2359
|
+
]);
|
|
2360
|
+
// Save
|
|
2361
|
+
const data = db.export();
|
|
2362
|
+
writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
|
|
2363
|
+
db.close();
|
|
2364
|
+
// Add to HNSW index for faster future searches
|
|
2365
|
+
if (embeddingJson) {
|
|
2366
|
+
const embResult = JSON.parse(embeddingJson);
|
|
2367
|
+
await addToHNSWIndex(id, embResult, {
|
|
2368
|
+
id,
|
|
2369
|
+
key,
|
|
2370
|
+
namespace,
|
|
2371
|
+
content: value
|
|
2372
|
+
});
|
|
2373
|
+
}
|
|
2374
|
+
return {
|
|
2375
|
+
success: true,
|
|
2376
|
+
id,
|
|
2377
|
+
embedding: embeddingJson ? { dimensions: embeddingDimensions, model: embeddingModel } : undefined
|
|
2378
|
+
};
|
|
2379
|
+
}
|
|
2380
|
+
catch (error) {
|
|
2381
|
+
return {
|
|
2382
|
+
success: false,
|
|
2383
|
+
id: '',
|
|
2384
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2385
|
+
};
|
|
2386
|
+
}
|
|
2387
|
+
}
|
|
2388
|
+
/**
|
|
2389
|
+
* Search entries using sql.js with vector similarity
|
|
2390
|
+
* Uses HNSW index for 150x faster search when available
|
|
2391
|
+
*/
|
|
2392
|
+
export async function searchEntries(options) {
|
|
2393
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
2394
|
+
const bridge = await getBridge();
|
|
2395
|
+
if (bridge) {
|
|
2396
|
+
const bridgeResult = await bridge.bridgeSearchEntries(options);
|
|
2397
|
+
if (bridgeResult)
|
|
2398
|
+
return bridgeResult;
|
|
2399
|
+
}
|
|
2400
|
+
// Fallback: raw sql.js
|
|
2401
|
+
const { query, namespace, limit = 10, threshold = 0.3, dbPath: customPath } = options;
|
|
2402
|
+
const effectiveNamespace = namespace || 'all';
|
|
2403
|
+
const swarmDir = getMemoryRoot();
|
|
2404
|
+
const dbPath = customPath ? path.resolve(customPath) : path.join(swarmDir, 'memory.db');
|
|
2405
|
+
const startTime = Date.now();
|
|
2406
|
+
try {
|
|
2407
|
+
if (!fs.existsSync(dbPath)) {
|
|
2408
|
+
return { success: false, results: [], searchTime: 0, error: 'Database not found' };
|
|
2409
|
+
}
|
|
2410
|
+
// Ensure schema has all required columns (migration for older DBs)
|
|
2411
|
+
await ensureSchemaColumns(dbPath);
|
|
2412
|
+
// Generate query embedding
|
|
2413
|
+
const queryEmb = await generateEmbedding(query);
|
|
2414
|
+
const queryEmbedding = queryEmb.embedding;
|
|
2415
|
+
// Try RaBitQ pre-filter first (32× compressed Hamming scan)
|
|
2416
|
+
try {
|
|
2417
|
+
const { searchRabitq } = await import('./rabitq-index.js');
|
|
2418
|
+
const rabitqCandidates = await searchRabitq(queryEmbedding, { k: limit * 2, namespace: effectiveNamespace });
|
|
2419
|
+
if (rabitqCandidates && rabitqCandidates.length > 0) {
|
|
2420
|
+
// Rerank candidates with exact cosine similarity from SQLite
|
|
2421
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2422
|
+
const SQL = await initSqlJs();
|
|
2423
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2424
|
+
const db = new SQL.Database(fileBuffer);
|
|
2425
|
+
const reranked = [];
|
|
2426
|
+
for (const candidate of rabitqCandidates) {
|
|
2427
|
+
const stmt = db.prepare('SELECT content, embedding FROM memory_entries WHERE id = ? AND status = ?');
|
|
2428
|
+
stmt.bind([candidate.id, 'active']);
|
|
2429
|
+
if (stmt.step()) {
|
|
2430
|
+
const [content, embeddingJson] = stmt.get();
|
|
2431
|
+
let score = 0;
|
|
2432
|
+
if (embeddingJson) {
|
|
2433
|
+
try {
|
|
2434
|
+
const embedding = JSON.parse(embeddingJson);
|
|
2435
|
+
score = cosineSim(queryEmbedding, embedding);
|
|
2436
|
+
}
|
|
2437
|
+
catch { /* skip */ }
|
|
2438
|
+
}
|
|
2439
|
+
if (score >= threshold) {
|
|
2440
|
+
reranked.push({
|
|
2441
|
+
id: candidate.id.substring(0, 12),
|
|
2442
|
+
key: candidate.key || candidate.id.substring(0, 15),
|
|
2443
|
+
content: (content || '').substring(0, 60) + ((content || '').length > 60 ? '...' : ''),
|
|
2444
|
+
score,
|
|
2445
|
+
namespace: candidate.namespace,
|
|
2446
|
+
});
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
stmt.free();
|
|
2450
|
+
}
|
|
2451
|
+
db.close();
|
|
2452
|
+
if (reranked.length > 0) {
|
|
2453
|
+
reranked.sort((a, b) => b.score - a.score);
|
|
2454
|
+
return { success: true, results: reranked.slice(0, limit), searchTime: Date.now() - startTime };
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
}
|
|
2458
|
+
catch { /* RaBitQ unavailable, fall through */ }
|
|
2459
|
+
// Try HNSW search (150x faster than brute-force)
|
|
2460
|
+
const hnswResults = await searchHNSWIndex(queryEmbedding, { k: limit, namespace: effectiveNamespace });
|
|
2461
|
+
if (hnswResults && hnswResults.length > 0) {
|
|
2462
|
+
// Filter by threshold
|
|
2463
|
+
const filtered = hnswResults.filter(r => r.score >= threshold);
|
|
2464
|
+
return {
|
|
2465
|
+
success: true,
|
|
2466
|
+
results: filtered,
|
|
2467
|
+
searchTime: Date.now() - startTime
|
|
2468
|
+
};
|
|
2469
|
+
}
|
|
2470
|
+
// Fall back to brute-force SQLite search
|
|
2471
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2472
|
+
const SQL = await initSqlJs();
|
|
2473
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2474
|
+
const db = new SQL.Database(fileBuffer);
|
|
2475
|
+
// Get entries with embeddings
|
|
2476
|
+
const searchStmt = db.prepare(effectiveNamespace !== 'all'
|
|
2477
|
+
? `SELECT id, key, namespace, content, embedding FROM memory_entries WHERE status = 'active' AND namespace = ? LIMIT 1000`
|
|
2478
|
+
: `SELECT id, key, namespace, content, embedding FROM memory_entries WHERE status = 'active' LIMIT 1000`);
|
|
2479
|
+
if (effectiveNamespace !== 'all') {
|
|
2480
|
+
searchStmt.bind([effectiveNamespace]);
|
|
2481
|
+
}
|
|
2482
|
+
const searchRows = [];
|
|
2483
|
+
while (searchStmt.step()) {
|
|
2484
|
+
searchRows.push(searchStmt.get());
|
|
2485
|
+
}
|
|
2486
|
+
searchStmt.free();
|
|
2487
|
+
const entries = searchRows.length > 0 ? [{ values: searchRows }] : [];
|
|
2488
|
+
const results = [];
|
|
2489
|
+
if (entries[0]?.values) {
|
|
2490
|
+
for (const row of entries[0].values) {
|
|
2491
|
+
const [id, key, ns, content, embeddingJson] = row;
|
|
2492
|
+
let score = 0;
|
|
2493
|
+
if (embeddingJson) {
|
|
2494
|
+
try {
|
|
2495
|
+
const embedding = JSON.parse(embeddingJson);
|
|
2496
|
+
score = cosineSim(queryEmbedding, embedding);
|
|
2497
|
+
}
|
|
2498
|
+
catch {
|
|
2499
|
+
// Invalid embedding, use keyword score
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
// Fallback to keyword matching
|
|
2503
|
+
if (score < threshold) {
|
|
2504
|
+
const lowerContent = (content || '').toLowerCase();
|
|
2505
|
+
const lowerQuery = query.toLowerCase();
|
|
2506
|
+
const words = lowerQuery.split(/\s+/);
|
|
2507
|
+
const matchCount = words.filter(w => lowerContent.includes(w)).length;
|
|
2508
|
+
const keywordScore = matchCount / words.length * 0.5;
|
|
2509
|
+
score = Math.max(score, keywordScore);
|
|
2510
|
+
}
|
|
2511
|
+
if (score >= threshold) {
|
|
2512
|
+
results.push({
|
|
2513
|
+
id: id.substring(0, 12),
|
|
2514
|
+
key: key || id.substring(0, 15),
|
|
2515
|
+
content: (content || '').substring(0, 60) + ((content || '').length > 60 ? '...' : ''),
|
|
2516
|
+
score,
|
|
2517
|
+
namespace: ns || 'default'
|
|
2518
|
+
});
|
|
2519
|
+
}
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
db.close();
|
|
2523
|
+
// Sort by score
|
|
2524
|
+
results.sort((a, b) => b.score - a.score);
|
|
2525
|
+
return {
|
|
2526
|
+
success: true,
|
|
2527
|
+
results: results.slice(0, limit),
|
|
2528
|
+
searchTime: Date.now() - startTime
|
|
2529
|
+
};
|
|
2530
|
+
}
|
|
2531
|
+
catch (error) {
|
|
2532
|
+
return {
|
|
2533
|
+
success: false,
|
|
2534
|
+
results: [],
|
|
2535
|
+
searchTime: Date.now() - startTime,
|
|
2536
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2537
|
+
};
|
|
2538
|
+
}
|
|
2539
|
+
}
|
|
2540
|
+
/**
|
|
2541
|
+
* Optimized cosine similarity
|
|
2542
|
+
* V8 JIT-friendly - avoids manual unrolling which can hurt performance
|
|
2543
|
+
* ~0.5μs per 384-dim vector comparison
|
|
2544
|
+
*/
|
|
2545
|
+
function cosineSim(a, b) {
|
|
2546
|
+
if (!a || !b || a.length === 0 || b.length === 0)
|
|
2547
|
+
return 0;
|
|
2548
|
+
const len = Math.min(a.length, b.length);
|
|
2549
|
+
let dot = 0, normA = 0, normB = 0;
|
|
2550
|
+
// Simple loop - V8 optimizes this well
|
|
2551
|
+
for (let i = 0; i < len; i++) {
|
|
2552
|
+
const ai = a[i], bi = b[i];
|
|
2553
|
+
dot += ai * bi;
|
|
2554
|
+
normA += ai * ai;
|
|
2555
|
+
normB += bi * bi;
|
|
2556
|
+
}
|
|
2557
|
+
// Combined sqrt for slightly better performance
|
|
2558
|
+
const mag = Math.sqrt(normA * normB);
|
|
2559
|
+
return mag === 0 ? 0 : dot / mag;
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
* List all entries from the memory database
|
|
2563
|
+
*/
|
|
2564
|
+
export async function listEntries(options) {
|
|
2565
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
2566
|
+
const bridge = await getBridge();
|
|
2567
|
+
if (bridge) {
|
|
2568
|
+
const bridgeResult = await bridge.bridgeListEntries(options);
|
|
2569
|
+
if (bridgeResult)
|
|
2570
|
+
return bridgeResult;
|
|
2571
|
+
}
|
|
2572
|
+
// Fallback: raw sql.js
|
|
2573
|
+
const { namespace, limit = 20, offset = 0, dbPath: customPath } = options;
|
|
2574
|
+
const swarmDir = getMemoryRoot();
|
|
2575
|
+
const dbPath = customPath || path.join(swarmDir, 'memory.db');
|
|
2576
|
+
try {
|
|
2577
|
+
if (!fs.existsSync(dbPath)) {
|
|
2578
|
+
return { success: false, entries: [], total: 0, error: 'Database not found' };
|
|
2579
|
+
}
|
|
2580
|
+
// Ensure schema has all required columns (migration for older DBs)
|
|
2581
|
+
await ensureSchemaColumns(dbPath);
|
|
2582
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2583
|
+
const SQL = await initSqlJs();
|
|
2584
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2585
|
+
const db = new SQL.Database(fileBuffer);
|
|
2586
|
+
// #2120 — accept `status IS NULL` alongside `'active'`. Old DBs
|
|
2587
|
+
// that predate the status column may have NULL after migration.
|
|
2588
|
+
// See memory-bridge.ts:bridgeListEntries for full context.
|
|
2589
|
+
// Get total count
|
|
2590
|
+
const countStmt = namespace
|
|
2591
|
+
? db.prepare(`SELECT COUNT(*) as cnt FROM memory_entries WHERE (status = 'active' OR status IS NULL) AND namespace = ?`)
|
|
2592
|
+
: db.prepare(`SELECT COUNT(*) as cnt FROM memory_entries WHERE (status = 'active' OR status IS NULL)`);
|
|
2593
|
+
if (namespace) {
|
|
2594
|
+
countStmt.bind([namespace]);
|
|
2595
|
+
}
|
|
2596
|
+
const countRows = [];
|
|
2597
|
+
while (countStmt.step()) {
|
|
2598
|
+
countRows.push(countStmt.get());
|
|
2599
|
+
}
|
|
2600
|
+
countStmt.free();
|
|
2601
|
+
const countResult = countRows.length > 0 ? [{ values: countRows }] : [];
|
|
2602
|
+
const total = countResult[0]?.values?.[0]?.[0] || 0;
|
|
2603
|
+
// Get entries
|
|
2604
|
+
const safeLimit = parseInt(String(limit), 10) || 100;
|
|
2605
|
+
const safeOffset = parseInt(String(offset), 10) || 0;
|
|
2606
|
+
// #2120 — same NULL-as-active acceptance as the count above.
|
|
2607
|
+
const listStmt = namespace
|
|
2608
|
+
? db.prepare(`SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at FROM memory_entries WHERE (status = 'active' OR status IS NULL) AND namespace = ? ORDER BY updated_at DESC LIMIT ? OFFSET ?`)
|
|
2609
|
+
: db.prepare(`SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at FROM memory_entries WHERE (status = 'active' OR status IS NULL) ORDER BY updated_at DESC LIMIT ? OFFSET ?`);
|
|
2610
|
+
if (namespace) {
|
|
2611
|
+
listStmt.bind([namespace, safeLimit, safeOffset]);
|
|
2612
|
+
}
|
|
2613
|
+
else {
|
|
2614
|
+
listStmt.bind([safeLimit, safeOffset]);
|
|
2615
|
+
}
|
|
2616
|
+
const listRows = [];
|
|
2617
|
+
while (listStmt.step()) {
|
|
2618
|
+
listRows.push(listStmt.get());
|
|
2619
|
+
}
|
|
2620
|
+
listStmt.free();
|
|
2621
|
+
const result = listRows.length > 0 ? [{ values: listRows }] : [];
|
|
2622
|
+
const entries = [];
|
|
2623
|
+
if (result[0]?.values) {
|
|
2624
|
+
for (const row of result[0].values) {
|
|
2625
|
+
const [id, key, ns, content, embedding, accessCount, createdAt, updatedAt] = row;
|
|
2626
|
+
const entry = {
|
|
2627
|
+
// #2073: don't truncate id when content is requested — callers
|
|
2628
|
+
// (notably memory_export) need the full id to round-trip via import.
|
|
2629
|
+
id: options.includeContent ? String(id) : String(id).substring(0, 20),
|
|
2630
|
+
key: key || String(id).substring(0, 15),
|
|
2631
|
+
namespace: ns || 'default',
|
|
2632
|
+
size: (content || '').length,
|
|
2633
|
+
accessCount: accessCount || 0,
|
|
2634
|
+
createdAt: createdAt || new Date().toISOString(),
|
|
2635
|
+
updatedAt: updatedAt || new Date().toISOString(),
|
|
2636
|
+
hasEmbedding: !!embedding && embedding.length > 10
|
|
2637
|
+
};
|
|
2638
|
+
if (options.includeContent) {
|
|
2639
|
+
entry.content = content || '';
|
|
2640
|
+
}
|
|
2641
|
+
entries.push(entry);
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
db.close();
|
|
2645
|
+
return { success: true, entries, total };
|
|
2646
|
+
}
|
|
2647
|
+
catch (error) {
|
|
2648
|
+
return {
|
|
2649
|
+
success: false,
|
|
2650
|
+
entries: [],
|
|
2651
|
+
total: 0,
|
|
2652
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2653
|
+
};
|
|
2654
|
+
}
|
|
2655
|
+
}
|
|
2656
|
+
/**
|
|
2657
|
+
* Get a specific entry from the memory database
|
|
2658
|
+
*/
|
|
2659
|
+
export async function getEntry(options) {
|
|
2660
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
2661
|
+
const bridge = await getBridge();
|
|
2662
|
+
if (bridge) {
|
|
2663
|
+
const bridgeResult = await bridge.bridgeGetEntry(options);
|
|
2664
|
+
if (bridgeResult)
|
|
2665
|
+
return bridgeResult;
|
|
2666
|
+
}
|
|
2667
|
+
// Fallback: raw sql.js
|
|
2668
|
+
const { key, namespace = 'default', dbPath: customPath } = options;
|
|
2669
|
+
const swarmDir = getMemoryRoot();
|
|
2670
|
+
const dbPath = customPath || path.join(swarmDir, 'memory.db');
|
|
2671
|
+
try {
|
|
2672
|
+
if (!fs.existsSync(dbPath)) {
|
|
2673
|
+
return { success: false, found: false, error: 'Database not found' };
|
|
2674
|
+
}
|
|
2675
|
+
// Ensure schema has all required columns (migration for older DBs)
|
|
2676
|
+
await ensureSchemaColumns(dbPath);
|
|
2677
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2678
|
+
const SQL = await initSqlJs();
|
|
2679
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2680
|
+
const db = new SQL.Database(fileBuffer);
|
|
2681
|
+
// Find entry by key
|
|
2682
|
+
const getStmt = db.prepare(`
|
|
2683
|
+
SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, tags
|
|
2684
|
+
FROM memory_entries
|
|
2685
|
+
WHERE status = 'active'
|
|
2686
|
+
AND key = ?
|
|
2687
|
+
AND namespace = ?
|
|
2688
|
+
LIMIT 1
|
|
2689
|
+
`);
|
|
2690
|
+
getStmt.bind([key, namespace]);
|
|
2691
|
+
const getRows = [];
|
|
2692
|
+
while (getStmt.step()) {
|
|
2693
|
+
getRows.push(getStmt.get());
|
|
2694
|
+
}
|
|
2695
|
+
getStmt.free();
|
|
2696
|
+
const result = getRows.length > 0 ? [{ values: getRows }] : [];
|
|
2697
|
+
if (!result[0]?.values?.[0]) {
|
|
2698
|
+
db.close();
|
|
2699
|
+
return { success: true, found: false };
|
|
2700
|
+
}
|
|
2701
|
+
const [id, entryKey, ns, content, embedding, accessCount, createdAt, updatedAt, tagsJson] = result[0].values[0];
|
|
2702
|
+
// Update access count
|
|
2703
|
+
db.run(`
|
|
2704
|
+
UPDATE memory_entries
|
|
2705
|
+
SET access_count = access_count + 1, last_accessed_at = strftime('%s', 'now') * 1000
|
|
2706
|
+
WHERE id = ?
|
|
2707
|
+
`, [String(id)]);
|
|
2708
|
+
// Save updated database
|
|
2709
|
+
const data = db.export();
|
|
2710
|
+
writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
|
|
2711
|
+
db.close();
|
|
2712
|
+
let tags = [];
|
|
2713
|
+
if (tagsJson) {
|
|
2714
|
+
try {
|
|
2715
|
+
tags = JSON.parse(tagsJson);
|
|
2716
|
+
}
|
|
2717
|
+
catch {
|
|
2718
|
+
// Invalid JSON
|
|
2719
|
+
}
|
|
2720
|
+
}
|
|
2721
|
+
return {
|
|
2722
|
+
success: true,
|
|
2723
|
+
found: true,
|
|
2724
|
+
entry: {
|
|
2725
|
+
id: String(id),
|
|
2726
|
+
key: entryKey || String(id),
|
|
2727
|
+
namespace: ns || 'default',
|
|
2728
|
+
content: content || '',
|
|
2729
|
+
accessCount: (accessCount || 0) + 1,
|
|
2730
|
+
createdAt: createdAt || new Date().toISOString(),
|
|
2731
|
+
updatedAt: updatedAt || new Date().toISOString(),
|
|
2732
|
+
hasEmbedding: !!embedding && embedding.length > 10,
|
|
2733
|
+
tags
|
|
2734
|
+
}
|
|
2735
|
+
};
|
|
2736
|
+
}
|
|
2737
|
+
catch (error) {
|
|
2738
|
+
return {
|
|
2739
|
+
success: false,
|
|
2740
|
+
found: false,
|
|
2741
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2742
|
+
};
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
/**
|
|
2746
|
+
* Delete a memory entry by key and namespace
|
|
2747
|
+
* Issue #980: Properly supports namespaced entries
|
|
2748
|
+
*/
|
|
2749
|
+
export async function deleteEntry(options) {
|
|
2750
|
+
// ADR-053: Try AgentDB v3 bridge first
|
|
2751
|
+
const bridge = await getBridge();
|
|
2752
|
+
if (bridge) {
|
|
2753
|
+
const bridgeResult = await bridge.bridgeDeleteEntry(options);
|
|
2754
|
+
if (bridgeResult) {
|
|
2755
|
+
// #1122: Bridge path must also invalidate the in-memory HNSW index.
|
|
2756
|
+
// Without this, deleted vectors remain as ghost entries in search results.
|
|
2757
|
+
if (bridgeResult.deleted && hnswIndex?.entries) {
|
|
2758
|
+
// Remove the entry from the HNSW entries map by key+namespace composite
|
|
2759
|
+
for (const [id, entry] of hnswIndex.entries) {
|
|
2760
|
+
if (entry?.key === options.key && (entry?.namespace ?? 'default') === (options.namespace ?? 'default')) {
|
|
2761
|
+
hnswIndex.entries.delete(id);
|
|
2762
|
+
break;
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
saveHNSWMetadata();
|
|
2766
|
+
rebuildSearchIndex();
|
|
2767
|
+
}
|
|
2768
|
+
return bridgeResult;
|
|
2769
|
+
}
|
|
2770
|
+
}
|
|
2771
|
+
// Fallback: raw sql.js
|
|
2772
|
+
const { key, namespace = 'default', dbPath: customPath } = options;
|
|
2773
|
+
const swarmDir = getMemoryRoot();
|
|
2774
|
+
const dbPath = customPath || path.join(swarmDir, 'memory.db');
|
|
2775
|
+
try {
|
|
2776
|
+
if (!fs.existsSync(dbPath)) {
|
|
2777
|
+
return {
|
|
2778
|
+
success: false,
|
|
2779
|
+
deleted: false,
|
|
2780
|
+
key,
|
|
2781
|
+
namespace,
|
|
2782
|
+
remainingEntries: 0,
|
|
2783
|
+
error: 'Database not found'
|
|
2784
|
+
};
|
|
2785
|
+
}
|
|
2786
|
+
// Ensure schema has all required columns (migration for older DBs)
|
|
2787
|
+
await ensureSchemaColumns(dbPath);
|
|
2788
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2789
|
+
const SQL = await initSqlJs();
|
|
2790
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2791
|
+
const db = new SQL.Database(fileBuffer);
|
|
2792
|
+
// Check if entry exists first
|
|
2793
|
+
const checkStmt = db.prepare(`
|
|
2794
|
+
SELECT id FROM memory_entries
|
|
2795
|
+
WHERE status = 'active'
|
|
2796
|
+
AND key = ?
|
|
2797
|
+
AND namespace = ?
|
|
2798
|
+
LIMIT 1
|
|
2799
|
+
`);
|
|
2800
|
+
checkStmt.bind([key, namespace]);
|
|
2801
|
+
const checkRows = [];
|
|
2802
|
+
while (checkStmt.step()) {
|
|
2803
|
+
checkRows.push(checkStmt.get());
|
|
2804
|
+
}
|
|
2805
|
+
checkStmt.free();
|
|
2806
|
+
const checkResult = checkRows.length > 0 ? [{ values: checkRows }] : [];
|
|
2807
|
+
if (!checkResult[0]?.values?.[0]) {
|
|
2808
|
+
// Get remaining count before closing
|
|
2809
|
+
const countResult = db.exec(`SELECT COUNT(*) FROM memory_entries WHERE status = 'active'`);
|
|
2810
|
+
const remainingEntries = countResult[0]?.values?.[0]?.[0] || 0;
|
|
2811
|
+
db.close();
|
|
2812
|
+
return {
|
|
2813
|
+
success: true,
|
|
2814
|
+
deleted: false,
|
|
2815
|
+
key,
|
|
2816
|
+
namespace,
|
|
2817
|
+
remainingEntries,
|
|
2818
|
+
error: `Key '${key}' not found in namespace '${namespace}'`
|
|
2819
|
+
};
|
|
2820
|
+
}
|
|
2821
|
+
// Capture the entry ID for HNSW cleanup
|
|
2822
|
+
const entryId = String(checkResult[0].values[0][0]);
|
|
2823
|
+
// Delete the entry (soft delete by setting status to 'deleted')
|
|
2824
|
+
// Also null out the embedding to clean up vector data from SQLite
|
|
2825
|
+
db.run(`
|
|
2826
|
+
UPDATE memory_entries
|
|
2827
|
+
SET status = 'deleted',
|
|
2828
|
+
embedding = NULL,
|
|
2829
|
+
updated_at = strftime('%s', 'now') * 1000
|
|
2830
|
+
WHERE key = ?
|
|
2831
|
+
AND namespace = ?
|
|
2832
|
+
AND status = 'active'
|
|
2833
|
+
`, [key, namespace]);
|
|
2834
|
+
// Get remaining count
|
|
2835
|
+
const countResult = db.exec(`SELECT COUNT(*) FROM memory_entries WHERE status = 'active'`);
|
|
2836
|
+
const remainingEntries = countResult[0]?.values?.[0]?.[0] || 0;
|
|
2837
|
+
// Save updated database
|
|
2838
|
+
const data = db.export();
|
|
2839
|
+
writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
|
|
2840
|
+
db.close();
|
|
2841
|
+
// Clean up in-memory HNSW index so ghost vectors don't appear in searches.
|
|
2842
|
+
// Remove the entry from the HNSW entries map and invalidate the index.
|
|
2843
|
+
// The next search will rebuild the HNSW index from the remaining DB rows.
|
|
2844
|
+
if (hnswIndex?.entries) {
|
|
2845
|
+
hnswIndex.entries.delete(entryId);
|
|
2846
|
+
saveHNSWMetadata();
|
|
2847
|
+
// Invalidate the HNSW index so it rebuilds from DB on next search.
|
|
2848
|
+
// We can't surgically remove a vector from the HNSW graph, so we
|
|
2849
|
+
// clear the entire index; it will be lazily rebuilt from SQLite.
|
|
2850
|
+
rebuildSearchIndex();
|
|
2851
|
+
}
|
|
2852
|
+
return {
|
|
2853
|
+
success: true,
|
|
2854
|
+
deleted: true,
|
|
2855
|
+
key,
|
|
2856
|
+
namespace,
|
|
2857
|
+
remainingEntries
|
|
2858
|
+
};
|
|
2859
|
+
}
|
|
2860
|
+
catch (error) {
|
|
2861
|
+
return {
|
|
2862
|
+
success: false,
|
|
2863
|
+
deleted: false,
|
|
2864
|
+
key,
|
|
2865
|
+
namespace,
|
|
2866
|
+
remainingEntries: 0,
|
|
2867
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2868
|
+
};
|
|
2869
|
+
}
|
|
2870
|
+
}
|
|
2871
|
+
// #2666 — Namespace reconciliation ("reaping"). `deleteEntry` above only ever
|
|
2872
|
+
// soft-deletes (UPDATE ... SET status='deleted'), and the row's (namespace,
|
|
2873
|
+
// key) stays occupied against the UNIQUE(namespace, key) constraint — a
|
|
2874
|
+
// caller that needs a namespace to be genuinely empty (e.g. a plugin
|
|
2875
|
+
// rebuilding its whole index after a source file was deleted, so a stale
|
|
2876
|
+
// row would otherwise survive forever with no dangling-ref/cycle signal to
|
|
2877
|
+
// ever catch it) has no way to get there through the public CLI/MCP surface.
|
|
2878
|
+
// `purgeNamespace` is a real `DELETE FROM memory_entries WHERE namespace = ?`
|
|
2879
|
+
// — irreversible, namespace-scoped, and lock-protected against a second
|
|
2880
|
+
// concurrent purge/delete on the same db file (see withMemoryDbLock below).
|
|
2881
|
+
//
|
|
2882
|
+
// This does NOT fully close #2621 (a concurrent daemon/MCP server already
|
|
2883
|
+
// mid read-modify-write on the sql.js fallback path can still flush an
|
|
2884
|
+
// older in-memory image after this purge commits, resurrecting rows) — that
|
|
2885
|
+
// requires every memory.db writer to respect the same lock, which is a
|
|
2886
|
+
// larger change than this namespace-reconcile primitive. The lock here
|
|
2887
|
+
// bounds the race to "another purge/delete running at the same instant",
|
|
2888
|
+
// which is the concrete case this feature needs to be safe against.
|
|
2889
|
+
const MEMORY_DB_LOCK_STALE_MS = 10_000;
|
|
2890
|
+
function delayMs(ms) {
|
|
2891
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
2892
|
+
}
|
|
2893
|
+
/**
|
|
2894
|
+
* Advisory O_EXCL lock scoped to a single memory.db file (`<dbPath>.lock`),
|
|
2895
|
+
* same stale-takeover pattern as services/global-ai-budget.ts. Only
|
|
2896
|
+
* meaningful between callers that opt into it (purgeNamespace does); it
|
|
2897
|
+
* cannot coordinate against writers that don't call this helper.
|
|
2898
|
+
*/
|
|
2899
|
+
export async function withMemoryDbLock(dbPath, fn) {
|
|
2900
|
+
const lockFile = `${dbPath}.lock`;
|
|
2901
|
+
const deadline = Date.now() + 5000;
|
|
2902
|
+
for (;;) {
|
|
2903
|
+
try {
|
|
2904
|
+
const fd = fs.openSync(lockFile, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_WRONLY, 0o600);
|
|
2905
|
+
fs.writeSync(fd, String(process.pid));
|
|
2906
|
+
fs.closeSync(fd);
|
|
2907
|
+
try {
|
|
2908
|
+
return await fn();
|
|
2909
|
+
}
|
|
2910
|
+
finally {
|
|
2911
|
+
try {
|
|
2912
|
+
fs.unlinkSync(lockFile);
|
|
2913
|
+
}
|
|
2914
|
+
catch { /* already gone */ }
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
catch (e) {
|
|
2918
|
+
if (e.code !== 'EEXIST')
|
|
2919
|
+
throw e;
|
|
2920
|
+
try {
|
|
2921
|
+
const st = fs.lstatSync(lockFile);
|
|
2922
|
+
if (Date.now() - st.mtimeMs > MEMORY_DB_LOCK_STALE_MS) {
|
|
2923
|
+
fs.unlinkSync(lockFile);
|
|
2924
|
+
continue;
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2927
|
+
catch { /* raced — retry */ }
|
|
2928
|
+
if (Date.now() > deadline) {
|
|
2929
|
+
throw new Error(`timed out acquiring memory.db lock: ${lockFile}`);
|
|
2930
|
+
}
|
|
2931
|
+
await delayMs(25);
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
}
|
|
2935
|
+
const NAMESPACE_PATTERN = /^[A-Za-z0-9._-]{1,128}$/;
|
|
2936
|
+
export async function purgeNamespace(options) {
|
|
2937
|
+
const { namespace, dbPath: customPath } = options;
|
|
2938
|
+
if (!NAMESPACE_PATTERN.test(namespace)) {
|
|
2939
|
+
return { success: false, deletedCount: 0, remainingEntries: 0, error: `Invalid namespace: ${namespace}` };
|
|
2940
|
+
}
|
|
2941
|
+
const swarmDir = getMemoryRoot();
|
|
2942
|
+
const dbPath = customPath ? path.resolve(customPath) : path.join(swarmDir, 'memory.db');
|
|
2943
|
+
return withMemoryDbLock(dbPath, async () => {
|
|
2944
|
+
// ADR-053: try the AgentDB v3 bridge first — a real SQLite handle, so
|
|
2945
|
+
// the DELETE is a genuine transactional statement, not a whole-file
|
|
2946
|
+
// read/mutate/rewrite.
|
|
2947
|
+
const bridge = await getBridge();
|
|
2948
|
+
if (bridge) {
|
|
2949
|
+
const bridgeResult = await bridge.bridgePurgeNamespace({ namespace, dbPath });
|
|
2950
|
+
if (bridgeResult) {
|
|
2951
|
+
if (bridgeResult.deletedCount > 0 && hnswIndex?.entries) {
|
|
2952
|
+
for (const [id, entry] of hnswIndex.entries) {
|
|
2953
|
+
if ((entry?.namespace ?? 'default') === namespace)
|
|
2954
|
+
hnswIndex.entries.delete(id);
|
|
2955
|
+
}
|
|
2956
|
+
saveHNSWMetadata();
|
|
2957
|
+
rebuildSearchIndex();
|
|
2958
|
+
}
|
|
2959
|
+
return bridgeResult;
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
// Fallback: raw sql.js, same whole-file read/mutate/rewrite shape as
|
|
2963
|
+
// deleteEntry's fallback path above (and the same encryption handling).
|
|
2964
|
+
try {
|
|
2965
|
+
if (!fs.existsSync(dbPath)) {
|
|
2966
|
+
return { success: false, deletedCount: 0, remainingEntries: 0, error: 'Database not found' };
|
|
2967
|
+
}
|
|
2968
|
+
await ensureSchemaColumns(dbPath);
|
|
2969
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
2970
|
+
const SQL = await initSqlJs();
|
|
2971
|
+
const fileBuffer = readFileMaybeEncrypted(dbPath, null);
|
|
2972
|
+
const db = new SQL.Database(fileBuffer);
|
|
2973
|
+
const beforeResult = db.exec(`SELECT COUNT(*) FROM memory_entries WHERE namespace = ?`, [namespace]);
|
|
2974
|
+
const deletedCount = beforeResult[0]?.values?.[0]?.[0] || 0;
|
|
2975
|
+
db.run(`DELETE FROM memory_entries WHERE namespace = ?`, [namespace]);
|
|
2976
|
+
const countResult = db.exec(`SELECT COUNT(*) FROM memory_entries WHERE status = 'active'`);
|
|
2977
|
+
const remainingEntries = countResult[0]?.values?.[0]?.[0] || 0;
|
|
2978
|
+
const data = db.export();
|
|
2979
|
+
writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
|
|
2980
|
+
db.close();
|
|
2981
|
+
if (deletedCount > 0 && hnswIndex?.entries) {
|
|
2982
|
+
for (const [id, entry] of hnswIndex.entries) {
|
|
2983
|
+
if ((entry?.namespace ?? 'default') === namespace)
|
|
2984
|
+
hnswIndex.entries.delete(id);
|
|
2985
|
+
}
|
|
2986
|
+
saveHNSWMetadata();
|
|
2987
|
+
rebuildSearchIndex();
|
|
2988
|
+
}
|
|
2989
|
+
return { success: true, deletedCount, remainingEntries };
|
|
2990
|
+
}
|
|
2991
|
+
catch (error) {
|
|
2992
|
+
return {
|
|
2993
|
+
success: false,
|
|
2994
|
+
deletedCount: 0,
|
|
2995
|
+
remainingEntries: 0,
|
|
2996
|
+
error: error instanceof Error ? error.message : String(error),
|
|
2997
|
+
};
|
|
2998
|
+
}
|
|
2999
|
+
});
|
|
3000
|
+
}
|
|
3001
|
+
export default {
|
|
3002
|
+
initializeMemoryDatabase,
|
|
3003
|
+
checkMemoryInitialization,
|
|
3004
|
+
checkAndMigrateLegacy,
|
|
3005
|
+
ensureSchemaColumns,
|
|
3006
|
+
applyTemporalDecay,
|
|
3007
|
+
loadEmbeddingModel,
|
|
3008
|
+
generateEmbedding,
|
|
3009
|
+
verifyMemoryInit,
|
|
3010
|
+
storeEntry,
|
|
3011
|
+
searchEntries,
|
|
3012
|
+
listEntries,
|
|
3013
|
+
getEntry,
|
|
3014
|
+
deleteEntry,
|
|
3015
|
+
purgeNamespace,
|
|
3016
|
+
withMemoryDbLock,
|
|
3017
|
+
rebuildSearchIndex,
|
|
3018
|
+
MEMORY_SCHEMA_V3,
|
|
3019
|
+
getInitialMetadata
|
|
3020
|
+
};
|
|
3021
|
+
//# sourceMappingURL=memory-initializer.js.map
|