@monoes/monomindcli 1.14.7 → 1.15.0
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/agents/reengineer-squad/boss.md +113 -0
- package/.claude/agents/reengineer-squad/critic-architect.md +132 -0
- package/.claude/agents/reengineer-squad/git-manager.md +145 -0
- package/.claude/agents/reengineer-squad/idea-generator.md +95 -0
- package/.claude/agents/reengineer-squad/implementer.md +112 -0
- package/.claude/agents/reengineer-squad/integration-planner.md +112 -0
- package/.claude/agents/reengineer-squad/source-analyst.md +103 -0
- package/.claude/agents/reengineer-squad/target-analyst.md +118 -0
- package/.claude/agents/reengineer-squad/tester.md +105 -0
- package/.claude/commands/mastermind/master.md +35 -14
- package/.claude/helpers/handlers/capture-handler.cjs +155 -18
- package/.claude/helpers/monolean-activate.cjs +20 -0
- package/.claude/helpers/monolean-config.cjs +76 -0
- package/.claude/helpers/monolean-instructions.cjs +109 -0
- package/.claude/helpers/monolean-propagate.cjs +9 -0
- package/.claude/helpers/monolean-tracker.cjs +18 -0
- package/.claude/helpers/skill-registry.json +2 -2
- package/.claude/skills/agent-browser-testing/SKILL.md +301 -18
- package/.claude/skills/mastermind/runorg.md +69 -23
- package/.claude/skills/monodesign/SKILL.md +32 -1
- package/.claude/skills/monodesign/adapt.md +53 -0
- package/.claude/skills/monodesign/agents/monodesign-asset-producer.md +100 -0
- package/.claude/skills/monodesign/animate.md +65 -0
- package/.claude/skills/monodesign/audit.md +89 -0
- package/.claude/skills/monodesign/bolder.md +50 -0
- package/.claude/skills/monodesign/clarify.md +64 -0
- package/.claude/skills/monodesign/colorize.md +68 -0
- package/.claude/skills/monodesign/craft.md +51 -0
- package/.claude/skills/monodesign/critique.md +66 -0
- package/.claude/skills/monodesign/delight.md +47 -0
- package/.claude/skills/monodesign/distill.md +56 -0
- package/.claude/skills/monodesign/document.md +80 -0
- package/.claude/skills/monodesign/extract.md +74 -0
- package/.claude/skills/monodesign/harden.md +65 -0
- package/.claude/skills/monodesign/live.md +59 -0
- package/.claude/skills/monodesign/onboard.md +50 -0
- package/.claude/skills/monodesign/optimize.md +64 -0
- package/.claude/skills/monodesign/overdrive.md +56 -0
- package/.claude/skills/monodesign/polish.md +68 -0
- package/.claude/skills/monodesign/quieter.md +57 -0
- package/.claude/skills/monodesign/reference/antipatterns-catalog.md +248 -76
- package/.claude/skills/monodesign/reference/codex.md +107 -0
- package/.claude/skills/monodesign/reference/craft.md +3 -0
- package/.claude/skills/monodesign/reference/hooks.md +99 -0
- package/.claude/skills/monodesign/reference/image-prompts.md +12 -0
- package/.claude/skills/monodesign/shape.md +71 -0
- package/.claude/skills/monodesign/teach.md +69 -0
- package/.claude/skills/monodesign/typeset.md +59 -0
- package/.claude/skills/monolean/SKILL.md +118 -0
- package/.claude/skills/monolean-audit/SKILL.md +41 -0
- package/.claude/skills/monolean-debt/SKILL.md +46 -0
- package/.claude/skills/monolean-help/SKILL.md +60 -0
- package/.claude/skills/monolean-review/SKILL.md +57 -0
- package/bin/cli.js +3 -1
- package/dist/dashboard/server.js +137 -0
- package/dist/src/__tests__/browse-adapters.test.d.ts +2 -0
- package/dist/src/__tests__/browse-adapters.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-adapters.test.js +51 -0
- package/dist/src/__tests__/browse-adapters.test.js.map +1 -0
- package/dist/src/__tests__/browse-analyzer.test.d.ts +2 -0
- package/dist/src/__tests__/browse-analyzer.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-analyzer.test.js +68 -0
- package/dist/src/__tests__/browse-analyzer.test.js.map +1 -0
- package/dist/src/__tests__/browse-builtin-handlers.test.d.ts +2 -0
- package/dist/src/__tests__/browse-builtin-handlers.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-builtin-handlers.test.js +139 -0
- package/dist/src/__tests__/browse-builtin-handlers.test.js.map +1 -0
- package/dist/src/__tests__/browse-cdp.test.d.ts +2 -0
- package/dist/src/__tests__/browse-cdp.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-cdp.test.js +169 -0
- package/dist/src/__tests__/browse-cdp.test.js.map +1 -0
- package/dist/src/__tests__/browse-dashboard.test.d.ts +2 -0
- package/dist/src/__tests__/browse-dashboard.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-dashboard.test.js +179 -0
- package/dist/src/__tests__/browse-dashboard.test.js.map +1 -0
- package/dist/src/__tests__/browse-engine.test.d.ts +2 -0
- package/dist/src/__tests__/browse-engine.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-engine.test.js +122 -0
- package/dist/src/__tests__/browse-engine.test.js.map +1 -0
- package/dist/src/__tests__/browse-expression.test.d.ts +2 -0
- package/dist/src/__tests__/browse-expression.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-expression.test.js +54 -0
- package/dist/src/__tests__/browse-expression.test.js.map +1 -0
- package/dist/src/__tests__/browse-store.test.d.ts +2 -0
- package/dist/src/__tests__/browse-store.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-store.test.js +99 -0
- package/dist/src/__tests__/browse-store.test.js.map +1 -0
- package/dist/src/__tests__/browse-workflow-types.test.d.ts +2 -0
- package/dist/src/__tests__/browse-workflow-types.test.d.ts.map +1 -0
- package/dist/src/__tests__/browse-workflow-types.test.js +33 -0
- package/dist/src/__tests__/browse-workflow-types.test.js.map +1 -0
- package/dist/src/browser/action-builder/analyzer.d.ts +11 -0
- package/dist/src/browser/action-builder/analyzer.d.ts.map +1 -0
- package/dist/src/browser/action-builder/analyzer.js +71 -0
- package/dist/src/browser/action-builder/analyzer.js.map +1 -0
- package/dist/src/browser/action-builder/types.d.ts +47 -0
- package/dist/src/browser/action-builder/types.d.ts.map +1 -0
- package/dist/src/browser/action-builder/types.js +2 -0
- package/dist/src/browser/action-builder/types.js.map +1 -0
- package/dist/src/browser/adapters/gemini.d.ts +3 -0
- package/dist/src/browser/adapters/gemini.d.ts.map +1 -0
- package/dist/src/browser/adapters/gemini.js +16 -0
- package/dist/src/browser/adapters/gemini.js.map +1 -0
- package/dist/src/browser/adapters/google.d.ts +3 -0
- package/dist/src/browser/adapters/google.d.ts.map +1 -0
- package/dist/src/browser/adapters/google.js +17 -0
- package/dist/src/browser/adapters/google.js.map +1 -0
- package/dist/src/browser/adapters/index.d.ts +19 -0
- package/dist/src/browser/adapters/index.d.ts.map +1 -0
- package/dist/src/browser/adapters/index.js +23 -0
- package/dist/src/browser/adapters/index.js.map +1 -0
- package/dist/src/browser/adapters/instagram.d.ts +3 -0
- package/dist/src/browser/adapters/instagram.d.ts.map +1 -0
- package/dist/src/browser/adapters/instagram.js +17 -0
- package/dist/src/browser/adapters/instagram.js.map +1 -0
- package/dist/src/browser/adapters/linkedin.d.ts +3 -0
- package/dist/src/browser/adapters/linkedin.d.ts.map +1 -0
- package/dist/src/browser/adapters/linkedin.js +19 -0
- package/dist/src/browser/adapters/linkedin.js.map +1 -0
- package/dist/src/browser/adapters/microsoft.d.ts +3 -0
- package/dist/src/browser/adapters/microsoft.d.ts.map +1 -0
- package/dist/src/browser/adapters/microsoft.js +16 -0
- package/dist/src/browser/adapters/microsoft.js.map +1 -0
- package/dist/src/browser/adapters/x.d.ts +3 -0
- package/dist/src/browser/adapters/x.d.ts.map +1 -0
- package/dist/src/browser/adapters/x.js +19 -0
- package/dist/src/browser/adapters/x.js.map +1 -0
- package/dist/src/browser/dashboard/api-types.d.ts +50 -0
- package/dist/src/browser/dashboard/api-types.d.ts.map +1 -0
- package/dist/src/browser/dashboard/api-types.js +14 -0
- package/dist/src/browser/dashboard/api-types.js.map +1 -0
- package/dist/src/browser/dashboard/server.d.ts +9 -0
- package/dist/src/browser/dashboard/server.d.ts.map +1 -0
- package/dist/src/browser/dashboard/server.js +62 -0
- package/dist/src/browser/dashboard/server.js.map +1 -0
- package/dist/src/browser/dashboard/ui.html +1811 -0
- package/dist/src/browser/workflow/builtin-handlers.d.ts +3 -0
- package/dist/src/browser/workflow/builtin-handlers.d.ts.map +1 -0
- package/dist/src/browser/workflow/builtin-handlers.js +343 -0
- package/dist/src/browser/workflow/builtin-handlers.js.map +1 -0
- package/dist/src/browser/workflow/engine.d.ts +15 -0
- package/dist/src/browser/workflow/engine.d.ts.map +1 -0
- package/dist/src/browser/workflow/engine.js +127 -0
- package/dist/src/browser/workflow/engine.js.map +1 -0
- package/dist/src/browser/workflow/expression.d.ts +4 -0
- package/dist/src/browser/workflow/expression.d.ts.map +1 -0
- package/dist/src/browser/workflow/expression.js +64 -0
- package/dist/src/browser/workflow/expression.js.map +1 -0
- package/dist/src/browser/workflow/store.d.ts +24 -0
- package/dist/src/browser/workflow/store.d.ts.map +1 -0
- package/dist/src/browser/workflow/store.js +145 -0
- package/dist/src/browser/workflow/store.js.map +1 -0
- package/dist/src/browser/workflow/types.d.ts +48 -0
- package/dist/src/browser/workflow/types.d.ts.map +1 -0
- package/dist/src/browser/workflow/types.js +2 -0
- package/dist/src/browser/workflow/types.js.map +1 -0
- package/dist/src/commands/browse-action.d.ts +4 -0
- package/dist/src/commands/browse-action.d.ts.map +1 -0
- package/dist/src/commands/browse-action.js +151 -0
- package/dist/src/commands/browse-action.js.map +1 -0
- package/dist/src/commands/browse-platform.d.ts +4 -0
- package/dist/src/commands/browse-platform.d.ts.map +1 -0
- package/dist/src/commands/browse-platform.js +117 -0
- package/dist/src/commands/browse-platform.js.map +1 -0
- package/dist/src/commands/browse-workflow.d.ts +4 -0
- package/dist/src/commands/browse-workflow.d.ts.map +1 -0
- package/dist/src/commands/browse-workflow.js +153 -0
- package/dist/src/commands/browse-workflow.js.map +1 -0
- package/dist/src/commands/browse.d.ts +10 -6
- package/dist/src/commands/browse.d.ts.map +1 -1
- package/dist/src/commands/browse.js +11 -2154
- package/dist/src/commands/browse.js.map +1 -1
- package/dist/src/commands/design-detect.d.ts +21 -0
- package/dist/src/commands/design-detect.d.ts.map +1 -0
- package/dist/src/commands/design-detect.js +127 -0
- package/dist/src/commands/design-detect.js.map +1 -0
- package/dist/src/commands/design-palette.d.ts +22 -0
- package/dist/src/commands/design-palette.d.ts.map +1 -0
- package/dist/src/commands/design-palette.js +539 -0
- package/dist/src/commands/design-palette.js.map +1 -0
- package/dist/src/commands/hooks-core-commands.d.ts +10 -0
- package/dist/src/commands/hooks-core-commands.d.ts.map +1 -0
- package/dist/src/commands/hooks-core-commands.js +377 -0
- package/dist/src/commands/hooks-core-commands.js.map +1 -0
- package/dist/src/commands/hooks-coverage-commands.d.ts +12 -0
- package/dist/src/commands/hooks-coverage-commands.d.ts.map +1 -0
- package/dist/src/commands/hooks-coverage-commands.js +1217 -0
- package/dist/src/commands/hooks-coverage-commands.js.map +1 -0
- package/dist/src/commands/hooks-coverage-utils.d.ts +42 -0
- package/dist/src/commands/hooks-coverage-utils.d.ts.map +1 -0
- package/dist/src/commands/hooks-coverage-utils.js +220 -0
- package/dist/src/commands/hooks-coverage-utils.js.map +1 -0
- package/dist/src/commands/hooks-extended-commands.d.ts +14 -0
- package/dist/src/commands/hooks-extended-commands.d.ts.map +1 -0
- package/dist/src/commands/hooks-extended-commands.js +579 -0
- package/dist/src/commands/hooks-extended-commands.js.map +1 -0
- package/dist/src/commands/hooks-formatting.d.ts +13 -0
- package/dist/src/commands/hooks-formatting.d.ts.map +1 -0
- package/dist/src/commands/hooks-formatting.js +42 -0
- package/dist/src/commands/hooks-formatting.js.map +1 -0
- package/dist/src/commands/hooks-routing-commands.d.ts +15 -0
- package/dist/src/commands/hooks-routing-commands.d.ts.map +1 -0
- package/dist/src/commands/hooks-routing-commands.js +723 -0
- package/dist/src/commands/hooks-routing-commands.js.map +1 -0
- package/dist/src/commands/hooks-workers.d.ts +9 -0
- package/dist/src/commands/hooks-workers.d.ts.map +1 -0
- package/dist/src/commands/hooks-workers.js +782 -0
- package/dist/src/commands/hooks-workers.js.map +1 -0
- package/dist/src/commands/hooks.d.ts +8 -0
- package/dist/src/commands/hooks.d.ts.map +1 -1
- package/dist/src/commands/hooks.js +179 -4103
- package/dist/src/commands/hooks.js.map +1 -1
- package/dist/src/commands/index.d.ts +1 -0
- package/dist/src/commands/index.d.ts.map +1 -1
- package/dist/src/commands/index.js +6 -0
- package/dist/src/commands/index.js.map +1 -1
- package/dist/src/commands/org.d.ts.map +1 -1
- package/dist/src/commands/org.js +14 -15
- package/dist/src/commands/org.js.map +1 -1
- package/dist/src/commands/tokens.d.ts.map +1 -1
- package/dist/src/commands/tokens.js +77 -1
- package/dist/src/commands/tokens.js.map +1 -1
- package/dist/src/init/executor.d.ts.map +1 -1
- package/dist/src/init/executor.js +18 -8
- package/dist/src/init/executor.js.map +1 -1
- package/dist/src/init/settings-generator.d.ts.map +1 -1
- package/dist/src/init/settings-generator.js +39 -5
- package/dist/src/init/settings-generator.js.map +1 -1
- package/dist/src/init/statusline-generator.d.ts.map +1 -1
- package/dist/src/init/statusline-generator.js +25 -5
- package/dist/src/init/statusline-generator.js.map +1 -1
- package/dist/src/mcp-tools/browser-tools.d.ts +3 -5
- package/dist/src/mcp-tools/browser-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/browser-tools.js +619 -326
- package/dist/src/mcp-tools/browser-tools.js.map +1 -1
- package/dist/src/mcp-tools/hooks-embedding.d.ts +161 -0
- package/dist/src/mcp-tools/hooks-embedding.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-embedding.js +506 -0
- package/dist/src/mcp-tools/hooks-embedding.js.map +1 -0
- package/dist/src/mcp-tools/hooks-intelligence.d.ts +26 -0
- package/dist/src/mcp-tools/hooks-intelligence.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-intelligence.js +1328 -0
- package/dist/src/mcp-tools/hooks-intelligence.js.map +1 -0
- package/dist/src/mcp-tools/hooks-routing.d.ts +27 -0
- package/dist/src/mcp-tools/hooks-routing.d.ts.map +1 -0
- package/dist/src/mcp-tools/hooks-routing.js +1591 -0
- package/dist/src/mcp-tools/hooks-routing.js.map +1 -0
- package/dist/src/mcp-tools/hooks-tools.d.ts +3 -38
- package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/hooks-tools.js +5 -3393
- package/dist/src/mcp-tools/hooks-tools.js.map +1 -1
- package/dist/src/mcp-tools/monograph-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/monograph-tools.js +24 -14
- package/dist/src/mcp-tools/monograph-tools.js.map +1 -1
- package/dist/src/mcp-tools/workflow-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/workflow-tools.js +54 -1
- package/dist/src/mcp-tools/workflow-tools.js.map +1 -1
- package/dist/src/memory/embedding-operations.d.ts +58 -0
- package/dist/src/memory/embedding-operations.d.ts.map +1 -0
- package/dist/src/memory/embedding-operations.js +299 -0
- package/dist/src/memory/embedding-operations.js.map +1 -0
- package/dist/src/memory/ewc-consolidation.d.ts.map +1 -1
- package/dist/src/memory/ewc-consolidation.js +37 -3
- package/dist/src/memory/ewc-consolidation.js.map +1 -1
- package/dist/src/memory/hnsw-operations.d.ts +130 -0
- package/dist/src/memory/hnsw-operations.d.ts.map +1 -0
- package/dist/src/memory/hnsw-operations.js +400 -0
- package/dist/src/memory/hnsw-operations.js.map +1 -0
- package/dist/src/memory/intelligence.d.ts.map +1 -1
- package/dist/src/memory/intelligence.js +42 -23
- package/dist/src/memory/intelligence.js.map +1 -1
- package/dist/src/memory/memory-bridge.d.ts.map +1 -1
- package/dist/src/memory/memory-bridge.js +52 -8
- package/dist/src/memory/memory-bridge.js.map +1 -1
- package/dist/src/memory/memory-crud.d.ts +67 -0
- package/dist/src/memory/memory-crud.d.ts.map +1 -0
- package/dist/src/memory/memory-crud.js +415 -0
- package/dist/src/memory/memory-crud.js.map +1 -0
- package/dist/src/memory/memory-initializer.d.ts +9 -322
- package/dist/src/memory/memory-initializer.d.ts.map +1 -1
- package/dist/src/memory/memory-initializer.js +17 -1794
- package/dist/src/memory/memory-initializer.js.map +1 -1
- package/dist/src/memory/memory-migrations.d.ts +30 -0
- package/dist/src/memory/memory-migrations.d.ts.map +1 -0
- package/dist/src/memory/memory-migrations.js +134 -0
- package/dist/src/memory/memory-migrations.js.map +1 -0
- package/dist/src/memory/memory-read.d.ts +78 -0
- package/dist/src/memory/memory-read.d.ts.map +1 -0
- package/dist/src/memory/memory-read.js +331 -0
- package/dist/src/memory/memory-read.js.map +1 -0
- package/dist/src/memory/memory-schema.d.ts +13 -0
- package/dist/src/memory/memory-schema.d.ts.map +1 -0
- package/dist/src/memory/memory-schema.js +167 -0
- package/dist/src/memory/memory-schema.js.map +1 -0
- package/dist/src/memory/sona-optimizer.d.ts.map +1 -1
- package/dist/src/memory/sona-optimizer.js +37 -4
- package/dist/src/memory/sona-optimizer.js.map +1 -1
- package/dist/src/monovector/route-outcomes.d.ts.map +1 -1
- package/dist/src/monovector/route-outcomes.js +16 -6
- package/dist/src/monovector/route-outcomes.js.map +1 -1
- package/dist/src/pricing/model-pricing.d.ts +41 -0
- package/dist/src/pricing/model-pricing.d.ts.map +1 -0
- package/dist/src/pricing/model-pricing.js +61 -0
- package/dist/src/pricing/model-pricing.js.map +1 -0
- package/dist/src/ui/.monomind/capture/active-run.json +1 -0
- package/dist/src/ui/.monomind/orgs/system-trial-qa/runs/real-events-1782290897.convs.jsonl +3 -0
- package/dist/src/ui/.monomind/orgs/system-trial-qa/runs/real-events-1782290897.jsonl +11 -0
- package/dist/src/ui/.monomind/orgs/system-trial-qa/runs/rigid-qa-restart-1782288201.jsonl +540 -0
- package/dist/src/ui/.monomind/orgs/system-trial-qa-threads.jsonl +3 -0
- package/dist/src/ui/.monomind/orgs/test-event-fix/runs/rigid-qa-restart-1782288201.jsonl +2 -0
- package/dist/src/ui/MODULARIZATION_PLAN.md +79 -0
- package/dist/src/ui/collector.mjs +23 -13
- package/dist/src/ui/dashboard.html +1652 -13
- package/dist/src/ui/data/known-projects.json +1 -0
- package/dist/src/ui/data/mastermind-events.jsonl +553 -0
- package/dist/src/ui/data/sessions/_index.json +1 -0
- package/dist/src/ui/data/sessions/final-sess-001.jsonl +542 -0
- package/dist/src/ui/data/unknown-events.jsonl +1 -0
- package/dist/src/ui/orgs.html +154 -10
- package/dist/src/ui/server.mjs +1131 -168
- package/dist/src/ui/sse-manager.mjs +119 -0
- package/dist/src/update/checker.js +1 -1
- package/dist/src/update/checker.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/workflow/builtin-handlers.js +321 -0
- package/dist/workflow/engine.js +253 -0
- package/dist/workflow/expression.js +98 -0
- package/dist/workflow/types.js +2 -0
- package/package.json +8 -5
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
*/
|
|
11
11
|
import * as fs from 'fs';
|
|
12
12
|
import * as path from 'path';
|
|
13
|
-
import { safeParseEmbedding } from './memory-bridge.js';
|
|
14
13
|
/** Maximum SQLite database file size accepted before read (256 MB). */
|
|
15
14
|
const MAX_DB_FILE_BYTES = 256 * 1024 * 1024;
|
|
16
15
|
// ADR-053: Lazy import of AgentDB v1 bridge
|
|
@@ -29,675 +28,22 @@ async function getBridge() {
|
|
|
29
28
|
return null;
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
|
-
/**
|
|
33
|
-
* Enhanced schema with pattern confidence, temporal decay, versioning
|
|
34
|
-
* Vector embeddings enabled for semantic search
|
|
35
|
-
*/
|
|
36
|
-
export const MEMORY_SCHEMA = `
|
|
37
|
-
-- Monomind Memory Database
|
|
38
|
-
-- Version: 3.0.0
|
|
39
|
-
-- Features: Pattern learning, vector embeddings, temporal decay, migration tracking
|
|
40
|
-
|
|
41
|
-
PRAGMA journal_mode = WAL;
|
|
42
|
-
PRAGMA synchronous = NORMAL;
|
|
43
|
-
PRAGMA foreign_keys = ON;
|
|
44
|
-
|
|
45
|
-
-- ============================================
|
|
46
|
-
-- CORE MEMORY TABLES
|
|
47
|
-
-- ============================================
|
|
48
|
-
|
|
49
|
-
-- Memory entries (main storage)
|
|
50
|
-
CREATE TABLE IF NOT EXISTS memory_entries (
|
|
51
|
-
id TEXT PRIMARY KEY,
|
|
52
|
-
key TEXT NOT NULL,
|
|
53
|
-
namespace TEXT DEFAULT 'default',
|
|
54
|
-
content TEXT NOT NULL,
|
|
55
|
-
type TEXT DEFAULT 'semantic' CHECK(type IN ('semantic', 'episodic', 'procedural', 'working', 'pattern')),
|
|
56
|
-
|
|
57
|
-
-- Vector embedding for semantic search (stored as JSON array)
|
|
58
|
-
embedding TEXT,
|
|
59
|
-
embedding_model TEXT DEFAULT 'local',
|
|
60
|
-
embedding_dimensions INTEGER,
|
|
61
|
-
|
|
62
|
-
-- Metadata
|
|
63
|
-
tags TEXT, -- JSON array
|
|
64
|
-
metadata TEXT, -- JSON object
|
|
65
|
-
owner_id TEXT,
|
|
66
|
-
|
|
67
|
-
-- Timestamps
|
|
68
|
-
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
69
|
-
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
70
|
-
expires_at INTEGER,
|
|
71
|
-
last_accessed_at INTEGER,
|
|
72
|
-
|
|
73
|
-
-- Access tracking for hot/cold detection
|
|
74
|
-
access_count INTEGER DEFAULT 0,
|
|
75
|
-
|
|
76
|
-
-- Status
|
|
77
|
-
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deleted')),
|
|
78
|
-
|
|
79
|
-
UNIQUE(namespace, key)
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
-- Indexes for memory entries
|
|
83
|
-
CREATE INDEX IF NOT EXISTS idx_memory_namespace ON memory_entries(namespace);
|
|
84
|
-
CREATE INDEX IF NOT EXISTS idx_memory_key ON memory_entries(key);
|
|
85
|
-
CREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(type);
|
|
86
|
-
CREATE INDEX IF NOT EXISTS idx_memory_status ON memory_entries(status);
|
|
87
|
-
CREATE INDEX IF NOT EXISTS idx_memory_created ON memory_entries(created_at);
|
|
88
|
-
CREATE INDEX IF NOT EXISTS idx_memory_accessed ON memory_entries(last_accessed_at);
|
|
89
|
-
CREATE INDEX IF NOT EXISTS idx_memory_owner ON memory_entries(owner_id);
|
|
90
|
-
|
|
91
|
-
-- ============================================
|
|
92
|
-
-- PATTERN LEARNING TABLES
|
|
93
|
-
-- ============================================
|
|
94
|
-
|
|
95
|
-
-- Learned patterns with confidence scoring and versioning
|
|
96
|
-
CREATE TABLE IF NOT EXISTS patterns (
|
|
97
|
-
id TEXT PRIMARY KEY,
|
|
98
|
-
|
|
99
|
-
-- Pattern identification
|
|
100
|
-
name TEXT NOT NULL,
|
|
101
|
-
pattern_type TEXT NOT NULL CHECK(pattern_type IN (
|
|
102
|
-
'task-routing', 'error-recovery', 'optimization', 'learning',
|
|
103
|
-
'coordination', 'prediction', 'code-pattern', 'workflow'
|
|
104
|
-
)),
|
|
105
|
-
|
|
106
|
-
-- Pattern definition
|
|
107
|
-
condition TEXT NOT NULL, -- Regex or semantic match
|
|
108
|
-
action TEXT NOT NULL, -- What to do when pattern matches
|
|
109
|
-
description TEXT,
|
|
110
|
-
|
|
111
|
-
-- Confidence scoring (0.0 - 1.0)
|
|
112
|
-
confidence REAL DEFAULT 0.5,
|
|
113
|
-
success_count INTEGER DEFAULT 0,
|
|
114
|
-
failure_count INTEGER DEFAULT 0,
|
|
115
|
-
|
|
116
|
-
-- Temporal decay
|
|
117
|
-
decay_rate REAL DEFAULT 0.01, -- How fast confidence decays
|
|
118
|
-
half_life_days INTEGER DEFAULT 30, -- Days until confidence halves without use
|
|
119
|
-
|
|
120
|
-
-- Vector embedding for semantic pattern matching
|
|
121
|
-
embedding TEXT,
|
|
122
|
-
embedding_dimensions INTEGER,
|
|
123
|
-
|
|
124
|
-
-- Versioning
|
|
125
|
-
version INTEGER DEFAULT 1,
|
|
126
|
-
parent_id TEXT REFERENCES patterns(id),
|
|
127
|
-
|
|
128
|
-
-- Metadata
|
|
129
|
-
tags TEXT, -- JSON array
|
|
130
|
-
metadata TEXT, -- JSON object
|
|
131
|
-
source TEXT, -- Where the pattern was learned from
|
|
132
|
-
|
|
133
|
-
-- Timestamps
|
|
134
|
-
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
135
|
-
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
136
|
-
last_matched_at INTEGER,
|
|
137
|
-
last_success_at INTEGER,
|
|
138
|
-
last_failure_at INTEGER,
|
|
139
|
-
|
|
140
|
-
-- Status
|
|
141
|
-
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deprecated', 'experimental'))
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
-- Indexes for patterns
|
|
145
|
-
CREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);
|
|
146
|
-
CREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);
|
|
147
|
-
CREATE INDEX IF NOT EXISTS idx_patterns_status ON patterns(status);
|
|
148
|
-
CREATE INDEX IF NOT EXISTS idx_patterns_last_matched ON patterns(last_matched_at);
|
|
149
|
-
|
|
150
|
-
-- Pattern evolution history (for versioning)
|
|
151
|
-
CREATE TABLE IF NOT EXISTS pattern_history (
|
|
152
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
153
|
-
pattern_id TEXT NOT NULL REFERENCES patterns(id),
|
|
154
|
-
version INTEGER NOT NULL,
|
|
155
|
-
|
|
156
|
-
-- Snapshot of pattern state
|
|
157
|
-
confidence REAL,
|
|
158
|
-
success_count INTEGER,
|
|
159
|
-
failure_count INTEGER,
|
|
160
|
-
condition TEXT,
|
|
161
|
-
action TEXT,
|
|
162
|
-
|
|
163
|
-
-- What changed
|
|
164
|
-
change_type TEXT CHECK(change_type IN ('created', 'updated', 'success', 'failure', 'decay', 'merged', 'split')),
|
|
165
|
-
change_reason TEXT,
|
|
166
|
-
|
|
167
|
-
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
168
|
-
);
|
|
169
|
-
|
|
170
|
-
CREATE INDEX IF NOT EXISTS idx_pattern_history_pattern ON pattern_history(pattern_id);
|
|
171
|
-
|
|
172
|
-
-- ============================================
|
|
173
|
-
-- LEARNING & TRAJECTORY TABLES
|
|
174
|
-
-- ============================================
|
|
175
|
-
|
|
176
|
-
-- Learning trajectories (SONA integration)
|
|
177
|
-
CREATE TABLE IF NOT EXISTS trajectories (
|
|
178
|
-
id TEXT PRIMARY KEY,
|
|
179
|
-
session_id TEXT,
|
|
180
|
-
|
|
181
|
-
-- Trajectory state
|
|
182
|
-
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'completed', 'failed', 'abandoned')),
|
|
183
|
-
verdict TEXT CHECK(verdict IN ('success', 'failure', 'partial', NULL)),
|
|
184
|
-
|
|
185
|
-
-- Context
|
|
186
|
-
task TEXT,
|
|
187
|
-
context TEXT, -- JSON object
|
|
188
|
-
|
|
189
|
-
-- Metrics
|
|
190
|
-
total_steps INTEGER DEFAULT 0,
|
|
191
|
-
total_reward REAL DEFAULT 0,
|
|
192
|
-
|
|
193
|
-
-- Timestamps
|
|
194
|
-
started_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
195
|
-
ended_at INTEGER,
|
|
196
|
-
|
|
197
|
-
-- Reference to extracted pattern (if any)
|
|
198
|
-
extracted_pattern_id TEXT REFERENCES patterns(id)
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
-- Trajectory steps
|
|
202
|
-
CREATE TABLE IF NOT EXISTS trajectory_steps (
|
|
203
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
204
|
-
trajectory_id TEXT NOT NULL REFERENCES trajectories(id),
|
|
205
|
-
step_number INTEGER NOT NULL,
|
|
206
|
-
|
|
207
|
-
-- Step data
|
|
208
|
-
action TEXT NOT NULL,
|
|
209
|
-
observation TEXT,
|
|
210
|
-
reward REAL DEFAULT 0,
|
|
211
|
-
|
|
212
|
-
-- Metadata
|
|
213
|
-
metadata TEXT, -- JSON object
|
|
214
|
-
|
|
215
|
-
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
216
|
-
);
|
|
217
|
-
|
|
218
|
-
CREATE INDEX IF NOT EXISTS idx_steps_trajectory ON trajectory_steps(trajectory_id);
|
|
219
|
-
|
|
220
|
-
-- ============================================
|
|
221
|
-
-- MIGRATION STATE TRACKING
|
|
222
|
-
-- ============================================
|
|
223
|
-
|
|
224
|
-
-- Migration state (for resume capability)
|
|
225
|
-
CREATE TABLE IF NOT EXISTS migration_state (
|
|
226
|
-
id TEXT PRIMARY KEY,
|
|
227
|
-
migration_type TEXT NOT NULL, -- 'v2-to-v1', 'pattern', 'memory', etc.
|
|
228
|
-
|
|
229
|
-
-- Progress tracking
|
|
230
|
-
status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'in_progress', 'completed', 'failed', 'rolled_back')),
|
|
231
|
-
total_items INTEGER DEFAULT 0,
|
|
232
|
-
processed_items INTEGER DEFAULT 0,
|
|
233
|
-
failed_items INTEGER DEFAULT 0,
|
|
234
|
-
skipped_items INTEGER DEFAULT 0,
|
|
235
|
-
|
|
236
|
-
-- Current position (for resume)
|
|
237
|
-
current_batch INTEGER DEFAULT 0,
|
|
238
|
-
last_processed_id TEXT,
|
|
239
|
-
|
|
240
|
-
-- Source/destination info
|
|
241
|
-
source_path TEXT,
|
|
242
|
-
source_type TEXT,
|
|
243
|
-
destination_path TEXT,
|
|
244
|
-
|
|
245
|
-
-- Backup info
|
|
246
|
-
backup_path TEXT,
|
|
247
|
-
backup_created_at INTEGER,
|
|
248
|
-
|
|
249
|
-
-- Error tracking
|
|
250
|
-
last_error TEXT,
|
|
251
|
-
errors TEXT, -- JSON array of errors
|
|
252
|
-
|
|
253
|
-
-- Timestamps
|
|
254
|
-
started_at INTEGER,
|
|
255
|
-
completed_at INTEGER,
|
|
256
|
-
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
257
|
-
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
-- ============================================
|
|
261
|
-
-- SESSION MANAGEMENT
|
|
262
|
-
-- ============================================
|
|
263
|
-
|
|
264
|
-
-- Sessions for context persistence
|
|
265
|
-
CREATE TABLE IF NOT EXISTS sessions (
|
|
266
|
-
id TEXT PRIMARY KEY,
|
|
267
|
-
|
|
268
|
-
-- Session state
|
|
269
|
-
state TEXT NOT NULL, -- JSON object with full session state
|
|
270
|
-
status TEXT DEFAULT 'active' CHECK(status IN ('active', 'paused', 'completed', 'expired')),
|
|
271
|
-
|
|
272
|
-
-- Context
|
|
273
|
-
project_path TEXT,
|
|
274
|
-
branch TEXT,
|
|
275
|
-
|
|
276
|
-
-- Metrics
|
|
277
|
-
tasks_completed INTEGER DEFAULT 0,
|
|
278
|
-
patterns_learned INTEGER DEFAULT 0,
|
|
279
|
-
|
|
280
|
-
-- Timestamps
|
|
281
|
-
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
282
|
-
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
283
|
-
expires_at INTEGER
|
|
284
|
-
);
|
|
285
|
-
|
|
286
|
-
-- ============================================
|
|
287
|
-
-- VECTOR INDEX METADATA (for HNSW)
|
|
288
|
-
-- ============================================
|
|
289
|
-
|
|
290
|
-
-- Track HNSW index state
|
|
291
|
-
CREATE TABLE IF NOT EXISTS vector_indexes (
|
|
292
|
-
id TEXT PRIMARY KEY,
|
|
293
|
-
name TEXT NOT NULL UNIQUE,
|
|
294
|
-
|
|
295
|
-
-- Index configuration
|
|
296
|
-
dimensions INTEGER NOT NULL,
|
|
297
|
-
metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),
|
|
298
|
-
|
|
299
|
-
-- HNSW parameters
|
|
300
|
-
hnsw_m INTEGER DEFAULT 16,
|
|
301
|
-
hnsw_ef_construction INTEGER DEFAULT 200,
|
|
302
|
-
hnsw_ef_search INTEGER DEFAULT 100,
|
|
303
|
-
|
|
304
|
-
-- Quantization
|
|
305
|
-
quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),
|
|
306
|
-
quantization_bits INTEGER DEFAULT 8,
|
|
307
|
-
|
|
308
|
-
-- Statistics
|
|
309
|
-
total_vectors INTEGER DEFAULT 0,
|
|
310
|
-
last_rebuild_at INTEGER,
|
|
311
|
-
|
|
312
|
-
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
|
|
313
|
-
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
|
|
314
|
-
);
|
|
315
|
-
|
|
316
|
-
-- ============================================
|
|
317
|
-
-- SYSTEM METADATA
|
|
318
|
-
-- ============================================
|
|
319
|
-
|
|
320
|
-
CREATE TABLE IF NOT EXISTS metadata (
|
|
321
|
-
key TEXT PRIMARY KEY,
|
|
322
|
-
value TEXT NOT NULL,
|
|
323
|
-
updated_at INTEGER DEFAULT (strftime('%s', 'now') * 1000)
|
|
324
|
-
);
|
|
325
|
-
`;
|
|
326
|
-
let hnswIndex = null;
|
|
327
|
-
let hnswInitializing = false;
|
|
328
|
-
/**
|
|
329
|
-
* Get or create the HNSW index singleton
|
|
330
|
-
* Lazily initializes from SQLite data on first use
|
|
331
|
-
*/
|
|
332
|
-
export async function getHNSWIndex(options) {
|
|
333
|
-
const dimensions = options?.dimensions ?? 384;
|
|
334
|
-
// Return existing index if already initialized
|
|
335
|
-
if (hnswIndex?.initialized && !options?.forceRebuild) {
|
|
336
|
-
return hnswIndex;
|
|
337
|
-
}
|
|
338
|
-
// Prevent concurrent initialization
|
|
339
|
-
if (hnswInitializing) {
|
|
340
|
-
// Wait for initialization to complete (max 5s)
|
|
341
|
-
let waitIterations = 0;
|
|
342
|
-
while (hnswInitializing && waitIterations < 500) {
|
|
343
|
-
await new Promise(resolve => setTimeout(resolve, 10));
|
|
344
|
-
waitIterations++;
|
|
345
|
-
}
|
|
346
|
-
if (hnswInitializing) {
|
|
347
|
-
throw new Error('HNSW initialization timed out after 5s');
|
|
348
|
-
}
|
|
349
|
-
// Init may have failed — return null rather than a non-initialized index object
|
|
350
|
-
return hnswIndex?.initialized ? hnswIndex : null;
|
|
351
|
-
}
|
|
352
|
-
hnswInitializing = true;
|
|
353
|
-
try {
|
|
354
|
-
// Native @monoes/core HNSW (WASM VectorDb) was removed in the lean teardown.
|
|
355
|
-
// This function is kept for callers that check its return value — all callers
|
|
356
|
-
// already handle null by falling back to the pure-JS / brute-force path.
|
|
357
|
-
// The AgentDB bridge (memory-bridge.ts) provides HNSW via agentdb instead.
|
|
358
|
-
// Native backend removed — return null so callers use the pure-JS fallback.
|
|
359
|
-
hnswInitializing = false;
|
|
360
|
-
return null;
|
|
361
|
-
}
|
|
362
|
-
catch {
|
|
363
|
-
hnswInitializing = false;
|
|
364
|
-
return null;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* Save HNSW metadata to disk for persistence
|
|
369
|
-
*/
|
|
370
|
-
function saveHNSWMetadata() {
|
|
371
|
-
if (!hnswIndex?.entries)
|
|
372
|
-
return;
|
|
373
|
-
try {
|
|
374
|
-
const swarmDir = path.join(process.cwd(), '.swarm');
|
|
375
|
-
const metadataPath = path.join(swarmDir, 'hnsw.metadata.json');
|
|
376
|
-
const metadata = Array.from(hnswIndex.entries.entries());
|
|
377
|
-
const tmp = metadataPath + '.tmp';
|
|
378
|
-
fs.writeFileSync(tmp, JSON.stringify(metadata));
|
|
379
|
-
fs.renameSync(tmp, metadataPath);
|
|
380
|
-
}
|
|
381
|
-
catch {
|
|
382
|
-
// Silently fail - metadata save is best-effort
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Add entry to HNSW index (with automatic persistence)
|
|
387
|
-
*/
|
|
388
|
-
export async function addToHNSWIndex(id, embedding, entry) {
|
|
389
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
390
|
-
const bridge = await getBridge();
|
|
391
|
-
if (bridge) {
|
|
392
|
-
const bridgeResult = await bridge.bridgeAddToHNSW(id, embedding, entry);
|
|
393
|
-
if (bridgeResult === true)
|
|
394
|
-
return true;
|
|
395
|
-
}
|
|
396
|
-
const index = await getHNSWIndex({ dimensions: embedding.length });
|
|
397
|
-
if (!index)
|
|
398
|
-
return false;
|
|
399
|
-
try {
|
|
400
|
-
const vector = new Float32Array(embedding);
|
|
401
|
-
await index.db.insert({
|
|
402
|
-
id,
|
|
403
|
-
vector
|
|
404
|
-
});
|
|
405
|
-
index.entries.set(id, entry);
|
|
406
|
-
// Save metadata for persistence (debounced would be better for high-volume)
|
|
407
|
-
saveHNSWMetadata();
|
|
408
|
-
return true;
|
|
409
|
-
}
|
|
410
|
-
catch {
|
|
411
|
-
return false;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Search HNSW index (150x faster than brute-force)
|
|
416
|
-
* Returns results sorted by similarity (highest first)
|
|
417
|
-
*/
|
|
418
|
-
export async function searchHNSWIndex(queryEmbedding, options) {
|
|
419
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
420
|
-
const bridge = await getBridge();
|
|
421
|
-
if (bridge) {
|
|
422
|
-
const bridgeResult = await bridge.bridgeSearchHNSW(queryEmbedding, options);
|
|
423
|
-
if (bridgeResult)
|
|
424
|
-
return bridgeResult;
|
|
425
|
-
}
|
|
426
|
-
const index = await getHNSWIndex({ dimensions: queryEmbedding.length });
|
|
427
|
-
if (!index)
|
|
428
|
-
return null;
|
|
429
|
-
try {
|
|
430
|
-
const vector = new Float32Array(queryEmbedding);
|
|
431
|
-
const k = options?.k ?? 10;
|
|
432
|
-
// HNSW search returns results with cosine distance (lower = more similar)
|
|
433
|
-
const results = await index.db.search({ vector, k: k * 2 }); // Get extra for filtering
|
|
434
|
-
const filtered = [];
|
|
435
|
-
for (const result of results) {
|
|
436
|
-
const entry = index.entries.get(result.id);
|
|
437
|
-
if (!entry)
|
|
438
|
-
continue;
|
|
439
|
-
// Filter by namespace if specified
|
|
440
|
-
if (options?.namespace && options.namespace !== 'all' && entry.namespace !== options.namespace) {
|
|
441
|
-
continue;
|
|
442
|
-
}
|
|
443
|
-
// Convert cosine distance to similarity score (1 - distance)
|
|
444
|
-
// Cosine distance convention: 0 = identical, 2 = opposite
|
|
445
|
-
const score = 1 - (result.score / 2);
|
|
446
|
-
filtered.push({
|
|
447
|
-
id: entry.id.substring(0, 12),
|
|
448
|
-
key: entry.key || entry.id.substring(0, 15),
|
|
449
|
-
content: entry.content.substring(0, 60) + (entry.content.length > 60 ? '...' : ''),
|
|
450
|
-
score,
|
|
451
|
-
namespace: entry.namespace
|
|
452
|
-
});
|
|
453
|
-
if (filtered.length >= k)
|
|
454
|
-
break;
|
|
455
|
-
}
|
|
456
|
-
// Sort by score descending (highest similarity first)
|
|
457
|
-
filtered.sort((a, b) => b.score - a.score);
|
|
458
|
-
return filtered;
|
|
459
|
-
}
|
|
460
|
-
catch {
|
|
461
|
-
return null;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
/**
|
|
465
|
-
* Get HNSW index status
|
|
466
|
-
*/
|
|
467
|
-
export function getHNSWStatus() {
|
|
468
|
-
// ADR-053: If bridge was previously loaded, report availability
|
|
469
|
-
if (_bridge && _bridge !== null) {
|
|
470
|
-
// Bridge is loaded — HNSW-equivalent is available via AgentDB v1
|
|
471
|
-
return {
|
|
472
|
-
available: true,
|
|
473
|
-
initialized: true,
|
|
474
|
-
entryCount: hnswIndex?.entries.size ?? 0,
|
|
475
|
-
dimensions: hnswIndex?.dimensions ?? 384
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
return {
|
|
479
|
-
available: hnswIndex !== null,
|
|
480
|
-
initialized: hnswIndex?.initialized ?? false,
|
|
481
|
-
entryCount: hnswIndex?.entries.size ?? 0,
|
|
482
|
-
dimensions: hnswIndex?.dimensions ?? 384
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Clear the HNSW index (for rebuilding)
|
|
487
|
-
*/
|
|
488
|
-
export function clearHNSWIndex() {
|
|
489
|
-
hnswIndex = null;
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Invalidate the in-memory HNSW cache so the next search rebuilds from DB.
|
|
493
|
-
* Call this after deleting entries that had embeddings to prevent ghost
|
|
494
|
-
* vectors from appearing in search results.
|
|
495
|
-
*/
|
|
496
|
-
export function rebuildSearchIndex() {
|
|
497
|
-
hnswIndex = null;
|
|
498
|
-
hnswInitializing = false;
|
|
499
|
-
}
|
|
500
31
|
// ============================================================================
|
|
501
|
-
//
|
|
32
|
+
// Re-exports from extracted modules (ARCH-4)
|
|
502
33
|
// ============================================================================
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
* @returns Quantized Int8 array with scale factor
|
|
509
|
-
*/
|
|
510
|
-
export function quantizeInt8(embedding) {
|
|
511
|
-
const arr = embedding instanceof Float32Array ? embedding : new Float32Array(embedding);
|
|
512
|
-
// Find min/max for symmetric quantization
|
|
513
|
-
let min = Infinity, max = -Infinity;
|
|
514
|
-
for (let i = 0; i < arr.length; i++) {
|
|
515
|
-
if (arr[i] < min)
|
|
516
|
-
min = arr[i];
|
|
517
|
-
if (arr[i] > max)
|
|
518
|
-
max = arr[i];
|
|
519
|
-
}
|
|
520
|
-
// Symmetric quantization: scale = max(|min|, |max|) / 127
|
|
521
|
-
const absMax = Math.max(Math.abs(min), Math.abs(max));
|
|
522
|
-
const scale = absMax / 127 || 1e-10; // Avoid division by zero
|
|
523
|
-
const zeroPoint = 0; // Symmetric quantization
|
|
524
|
-
// Quantize
|
|
525
|
-
const quantized = new Int8Array(arr.length);
|
|
526
|
-
for (let i = 0; i < arr.length; i++) {
|
|
527
|
-
// Clamp to [-127, 127] to leave room for potential rounding
|
|
528
|
-
const q = Math.round(arr[i] / scale);
|
|
529
|
-
quantized[i] = Math.max(-127, Math.min(127, q));
|
|
530
|
-
}
|
|
531
|
-
return { quantized, scale, zeroPoint };
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
|
-
* Dequantize Int8 back to Float32
|
|
535
|
-
*
|
|
536
|
-
* @param quantized - Int8 quantized array
|
|
537
|
-
* @param scale - Scale factor from quantization
|
|
538
|
-
* @param zeroPoint - Zero point (usually 0 for symmetric)
|
|
539
|
-
* @returns Float32Array
|
|
540
|
-
*/
|
|
541
|
-
export function dequantizeInt8(quantized, scale, zeroPoint = 0) {
|
|
542
|
-
const result = new Float32Array(quantized.length);
|
|
543
|
-
for (let i = 0; i < quantized.length; i++) {
|
|
544
|
-
result[i] = (quantized[i] - zeroPoint) * scale;
|
|
545
|
-
}
|
|
546
|
-
return result;
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Compute cosine similarity between quantized vectors
|
|
550
|
-
* Faster than dequantizing first
|
|
551
|
-
*/
|
|
552
|
-
export function quantizedCosineSim(a, aScale, b, bScale) {
|
|
553
|
-
if (a.length !== b.length)
|
|
554
|
-
return 0;
|
|
555
|
-
let dot = 0, normA = 0, normB = 0;
|
|
556
|
-
for (let i = 0; i < a.length; i++) {
|
|
557
|
-
dot += a[i] * b[i];
|
|
558
|
-
normA += a[i] * a[i];
|
|
559
|
-
normB += b[i] * b[i];
|
|
560
|
-
}
|
|
561
|
-
// Scales cancel out in cosine similarity for normalized vectors
|
|
562
|
-
const mag = Math.sqrt(normA * normB);
|
|
563
|
-
return mag === 0 ? 0 : dot / mag;
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* Get quantization statistics for an embedding
|
|
567
|
-
*/
|
|
568
|
-
export function getQuantizationStats(embedding) {
|
|
569
|
-
const len = embedding.length;
|
|
570
|
-
const originalBytes = len * 4; // Float32 = 4 bytes
|
|
571
|
-
const quantizedBytes = len + 8; // Int8 = 1 byte + 8 bytes for scale/zeroPoint
|
|
572
|
-
const compressionRatio = originalBytes / quantizedBytes;
|
|
573
|
-
return { originalBytes, quantizedBytes, compressionRatio };
|
|
574
|
-
}
|
|
34
|
+
export { MEMORY_SCHEMA } from './memory-schema.js';
|
|
35
|
+
export { getHNSWIndex, addToHNSWIndex, searchHNSWIndex, getHNSWStatus, clearHNSWIndex, rebuildSearchIndex, quantizeInt8, dequantizeInt8, quantizedCosineSim, getQuantizationStats, batchCosineSim, softmaxAttention, topKIndices, flashAttentionSearch, } from './hnsw-operations.js';
|
|
36
|
+
export { ensureSchemaColumns, checkAndMigrateLegacy, } from './memory-migrations.js';
|
|
37
|
+
export { loadEmbeddingModel, generateEmbedding, generateBatchEmbeddings, generateHashEmbedding, } from './embedding-operations.js';
|
|
38
|
+
export { verifyMemoryInit, storeEntry, searchEntries, listEntries, getEntry, deleteEntry, } from './memory-crud.js';
|
|
575
39
|
// ============================================================================
|
|
576
|
-
//
|
|
40
|
+
// Local imports for use in this file
|
|
577
41
|
// ============================================================================
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
export function batchCosineSim(query, vectors) {
|
|
584
|
-
const n = vectors.length;
|
|
585
|
-
const scores = new Float32Array(n);
|
|
586
|
-
if (n === 0 || query.length === 0)
|
|
587
|
-
return scores;
|
|
588
|
-
// Pre-compute query norm
|
|
589
|
-
let queryNorm = 0;
|
|
590
|
-
for (let i = 0; i < query.length; i++) {
|
|
591
|
-
queryNorm += query[i] * query[i];
|
|
592
|
-
}
|
|
593
|
-
queryNorm = Math.sqrt(queryNorm);
|
|
594
|
-
if (queryNorm === 0)
|
|
595
|
-
return scores;
|
|
596
|
-
// Compute similarities
|
|
597
|
-
for (let v = 0; v < n; v++) {
|
|
598
|
-
const vec = vectors[v];
|
|
599
|
-
const len = Math.min(query.length, vec.length);
|
|
600
|
-
let dot = 0, vecNorm = 0;
|
|
601
|
-
for (let i = 0; i < len; i++) {
|
|
602
|
-
dot += query[i] * vec[i];
|
|
603
|
-
vecNorm += vec[i] * vec[i];
|
|
604
|
-
}
|
|
605
|
-
vecNorm = Math.sqrt(vecNorm);
|
|
606
|
-
scores[v] = vecNorm === 0 ? 0 : dot / (queryNorm * vecNorm);
|
|
607
|
-
}
|
|
608
|
-
return scores;
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* Softmax normalization for attention scores
|
|
612
|
-
* Numerically stable implementation
|
|
613
|
-
*/
|
|
614
|
-
export function softmaxAttention(scores, temperature = 1.0) {
|
|
615
|
-
const n = scores.length;
|
|
616
|
-
const result = new Float32Array(n);
|
|
617
|
-
if (n === 0)
|
|
618
|
-
return result;
|
|
619
|
-
// Find max for numerical stability
|
|
620
|
-
let max = scores[0];
|
|
621
|
-
for (let i = 1; i < n; i++) {
|
|
622
|
-
if (scores[i] > max)
|
|
623
|
-
max = scores[i];
|
|
624
|
-
}
|
|
625
|
-
// Compute exp and sum
|
|
626
|
-
let sum = 0;
|
|
627
|
-
for (let i = 0; i < n; i++) {
|
|
628
|
-
result[i] = Math.exp((scores[i] - max) / temperature);
|
|
629
|
-
sum += result[i];
|
|
630
|
-
}
|
|
631
|
-
// Normalize
|
|
632
|
-
if (sum > 0) {
|
|
633
|
-
for (let i = 0; i < n; i++) {
|
|
634
|
-
result[i] /= sum;
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
return result;
|
|
638
|
-
}
|
|
639
|
-
/**
|
|
640
|
-
* Top-K selection with partial sort (O(n + k log k))
|
|
641
|
-
* More efficient than full sort for small k
|
|
642
|
-
*/
|
|
643
|
-
export function topKIndices(scores, k) {
|
|
644
|
-
const n = scores.length;
|
|
645
|
-
if (k >= n) {
|
|
646
|
-
// Return all indices sorted by score
|
|
647
|
-
return Array.from({ length: n }, (_, i) => i)
|
|
648
|
-
.sort((a, b) => scores[b] - scores[a]);
|
|
649
|
-
}
|
|
650
|
-
// Build min-heap of size k
|
|
651
|
-
const heap = [];
|
|
652
|
-
for (let i = 0; i < n; i++) {
|
|
653
|
-
if (heap.length < k) {
|
|
654
|
-
heap.push({ idx: i, score: scores[i] });
|
|
655
|
-
// Bubble up
|
|
656
|
-
let j = heap.length - 1;
|
|
657
|
-
while (j > 0) {
|
|
658
|
-
const parent = Math.floor((j - 1) / 2);
|
|
659
|
-
if (heap[j].score < heap[parent].score) {
|
|
660
|
-
[heap[j], heap[parent]] = [heap[parent], heap[j]];
|
|
661
|
-
j = parent;
|
|
662
|
-
}
|
|
663
|
-
else
|
|
664
|
-
break;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
else if (scores[i] > heap[0].score) {
|
|
668
|
-
// Replace min and heapify down
|
|
669
|
-
heap[0] = { idx: i, score: scores[i] };
|
|
670
|
-
let j = 0;
|
|
671
|
-
while (true) {
|
|
672
|
-
const left = 2 * j + 1, right = 2 * j + 2;
|
|
673
|
-
let smallest = j;
|
|
674
|
-
if (left < k && heap[left].score < heap[smallest].score)
|
|
675
|
-
smallest = left;
|
|
676
|
-
if (right < k && heap[right].score < heap[smallest].score)
|
|
677
|
-
smallest = right;
|
|
678
|
-
if (smallest === j)
|
|
679
|
-
break;
|
|
680
|
-
[heap[j], heap[smallest]] = [heap[smallest], heap[j]];
|
|
681
|
-
j = smallest;
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
// Extract and sort descending
|
|
686
|
-
return heap.sort((a, b) => b.score - a.score).map(h => h.idx);
|
|
687
|
-
}
|
|
688
|
-
/**
|
|
689
|
-
* Flash Attention-style search
|
|
690
|
-
* Combines batch similarity, softmax, and top-k in one pass.
|
|
691
|
-
* Returns indices and attention weights.
|
|
692
|
-
*/
|
|
693
|
-
export function flashAttentionSearch(query, vectors, options = {}) {
|
|
694
|
-
const { k = 10, temperature = 1.0, threshold = 0 } = options;
|
|
695
|
-
const scores = batchCosineSim(query, vectors);
|
|
696
|
-
const indices = topKIndices(scores, k).filter(i => scores[i] >= threshold);
|
|
697
|
-
const topScores = new Float32Array(indices.map(i => scores[i]));
|
|
698
|
-
const weights = softmaxAttention(topScores, temperature);
|
|
699
|
-
return { indices, scores: topScores, weights };
|
|
700
|
-
}
|
|
42
|
+
import { MEMORY_SCHEMA } from './memory-schema.js';
|
|
43
|
+
import { checkAndMigrateLegacy, ensureSchemaColumns } from './memory-migrations.js';
|
|
44
|
+
import { rebuildSearchIndex } from './hnsw-operations.js';
|
|
45
|
+
import { verifyMemoryInit, storeEntry, searchEntries, listEntries, getEntry, deleteEntry, } from './memory-crud.js';
|
|
46
|
+
import { loadEmbeddingModel, generateEmbedding, } from './embedding-operations.js';
|
|
701
47
|
// ============================================================================
|
|
702
48
|
// METADATA AND INITIALIZATION
|
|
703
49
|
// ============================================================================
|
|
@@ -718,133 +64,14 @@ INSERT OR REPLACE INTO metadata (key, value) VALUES
|
|
|
718
64
|
('hnsw_indexing', 'enabled');
|
|
719
65
|
|
|
720
66
|
-- Create default vector index configuration
|
|
67
|
+
-- Dimensions match BRIDGE_EMBEDDING_DIMS=384 (Xenova/all-MiniLM-L6-v2).
|
|
68
|
+
-- 768 was a legacy value from the agentic-flow era; 384 is the actual
|
|
69
|
+
-- embedding size used by memory-bridge.ts and AgentDB v1.
|
|
721
70
|
INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES
|
|
722
|
-
('default', 'default',
|
|
723
|
-
('patterns', 'patterns',
|
|
71
|
+
('default', 'default', 384),
|
|
72
|
+
('patterns', 'patterns', 384);
|
|
724
73
|
`;
|
|
725
74
|
}
|
|
726
|
-
/**
|
|
727
|
-
* Ensure memory_entries table has all required columns
|
|
728
|
-
* Adds missing columns for older databases (e.g., 'content' column)
|
|
729
|
-
*/
|
|
730
|
-
export async function ensureSchemaColumns(dbPath) {
|
|
731
|
-
const columnsAdded = [];
|
|
732
|
-
try {
|
|
733
|
-
if (!fs.existsSync(dbPath)) {
|
|
734
|
-
return { success: true, columnsAdded: [] };
|
|
735
|
-
}
|
|
736
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
737
|
-
const SQL = await initSqlJs();
|
|
738
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
739
|
-
const ensureStat = fs.statSync(dbPath);
|
|
740
|
-
if (ensureStat.size > MAX_DB_FILE_BYTES) {
|
|
741
|
-
return { success: false, columnsAdded, error: `Database file too large: ${ensureStat.size} bytes` };
|
|
742
|
-
}
|
|
743
|
-
const fileBuffer = fs.readFileSync(dbPath);
|
|
744
|
-
const db = new SQL.Database(fileBuffer);
|
|
745
|
-
// Get current columns in memory_entries
|
|
746
|
-
const tableInfo = db.exec("PRAGMA table_info(memory_entries)");
|
|
747
|
-
const existingColumns = new Set(tableInfo[0]?.values?.map(row => row[1]) || []);
|
|
748
|
-
// Required columns that may be missing in older schemas
|
|
749
|
-
// Issue #977: 'type' column was missing from this list, causing store failures on older DBs
|
|
750
|
-
const requiredColumns = [
|
|
751
|
-
{ name: 'content', definition: "content TEXT DEFAULT ''" },
|
|
752
|
-
{ name: 'type', definition: "type TEXT DEFAULT 'semantic'" },
|
|
753
|
-
{ name: 'embedding', definition: 'embedding TEXT' },
|
|
754
|
-
{ name: 'embedding_model', definition: "embedding_model TEXT DEFAULT 'local'" },
|
|
755
|
-
{ name: 'embedding_dimensions', definition: 'embedding_dimensions INTEGER' },
|
|
756
|
-
{ name: 'tags', definition: 'tags TEXT' },
|
|
757
|
-
{ name: 'metadata', definition: 'metadata TEXT' },
|
|
758
|
-
{ name: 'owner_id', definition: 'owner_id TEXT' },
|
|
759
|
-
{ name: 'expires_at', definition: 'expires_at INTEGER' },
|
|
760
|
-
{ name: 'last_accessed_at', definition: 'last_accessed_at INTEGER' },
|
|
761
|
-
{ name: 'access_count', definition: 'access_count INTEGER DEFAULT 0' },
|
|
762
|
-
{ name: 'status', definition: "status TEXT DEFAULT 'active'" }
|
|
763
|
-
];
|
|
764
|
-
let modified = false;
|
|
765
|
-
for (const col of requiredColumns) {
|
|
766
|
-
if (!existingColumns.has(col.name)) {
|
|
767
|
-
try {
|
|
768
|
-
db.run(`ALTER TABLE memory_entries ADD COLUMN ${col.definition}`);
|
|
769
|
-
columnsAdded.push(col.name);
|
|
770
|
-
modified = true;
|
|
771
|
-
}
|
|
772
|
-
catch (e) {
|
|
773
|
-
// Column might already exist or other error - continue
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
if (modified) {
|
|
778
|
-
// Save updated database (atomic to avoid corruption on crash)
|
|
779
|
-
const data = db.export();
|
|
780
|
-
const tmp = dbPath + '.tmp';
|
|
781
|
-
fs.writeFileSync(tmp, Buffer.from(data));
|
|
782
|
-
fs.renameSync(tmp, dbPath);
|
|
783
|
-
}
|
|
784
|
-
db.close();
|
|
785
|
-
return { success: true, columnsAdded };
|
|
786
|
-
}
|
|
787
|
-
catch (error) {
|
|
788
|
-
return {
|
|
789
|
-
success: false,
|
|
790
|
-
columnsAdded,
|
|
791
|
-
error: error instanceof Error ? error.message : String(error)
|
|
792
|
-
};
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
/**
|
|
796
|
-
* Check for legacy database installations and migrate if needed
|
|
797
|
-
*/
|
|
798
|
-
export async function checkAndMigrateLegacy(options) {
|
|
799
|
-
const { dbPath, verbose = false } = options;
|
|
800
|
-
// Check for legacy locations
|
|
801
|
-
const legacyPaths = [
|
|
802
|
-
path.join(process.cwd(), 'memory.db'),
|
|
803
|
-
path.join(process.cwd(), '.claude/memory.db'),
|
|
804
|
-
path.join(process.cwd(), 'data/memory.db'),
|
|
805
|
-
path.join(process.cwd(), '.monomind/memory.db')
|
|
806
|
-
];
|
|
807
|
-
for (const legacyPath of legacyPaths) {
|
|
808
|
-
if (fs.existsSync(legacyPath) && legacyPath !== dbPath) {
|
|
809
|
-
try {
|
|
810
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
811
|
-
const SQL = await initSqlJs();
|
|
812
|
-
// Guard against excessively large legacy DB files to prevent OOM.
|
|
813
|
-
const legacyStat = fs.statSync(legacyPath);
|
|
814
|
-
if (legacyStat.size > MAX_DB_FILE_BYTES) {
|
|
815
|
-
if (verbose) {
|
|
816
|
-
console.warn(`[memory] Skipping legacy DB at ${legacyPath}: file too large (${legacyStat.size} bytes)`);
|
|
817
|
-
}
|
|
818
|
-
continue;
|
|
819
|
-
}
|
|
820
|
-
const legacyBuffer = fs.readFileSync(legacyPath);
|
|
821
|
-
const legacyDb = new SQL.Database(legacyBuffer);
|
|
822
|
-
// Check if it has data
|
|
823
|
-
const countResult = legacyDb.exec('SELECT COUNT(*) FROM memory_entries');
|
|
824
|
-
const count = countResult[0]?.values[0]?.[0] || 0;
|
|
825
|
-
// Get version if available
|
|
826
|
-
let version = 'unknown';
|
|
827
|
-
try {
|
|
828
|
-
const versionResult = legacyDb.exec("SELECT value FROM metadata WHERE key='schema_version'");
|
|
829
|
-
version = versionResult[0]?.values[0]?.[0] || 'unknown';
|
|
830
|
-
}
|
|
831
|
-
catch { /* no metadata table */ }
|
|
832
|
-
legacyDb.close();
|
|
833
|
-
if (count > 0) {
|
|
834
|
-
return {
|
|
835
|
-
needsMigration: true,
|
|
836
|
-
legacyVersion: version,
|
|
837
|
-
legacyEntries: count
|
|
838
|
-
};
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
catch {
|
|
842
|
-
// Not a valid SQLite database, skip
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
return { needsMigration: false };
|
|
847
|
-
}
|
|
848
75
|
/**
|
|
849
76
|
* ADR-053: Activate ControllerRegistry so AgentDB v1 controllers
|
|
850
77
|
* (ReasoningBank, SkillLibrary, ExplainableRecall, etc.) are instantiated.
|
|
@@ -1184,1010 +411,6 @@ export async function applyTemporalDecay(dbPath) {
|
|
|
1184
411
|
};
|
|
1185
412
|
}
|
|
1186
413
|
}
|
|
1187
|
-
let embeddingModelState = null;
|
|
1188
|
-
/**
|
|
1189
|
-
* Lazy load ONNX embedding model
|
|
1190
|
-
* Only loads when first embedding is requested
|
|
1191
|
-
*/
|
|
1192
|
-
export async function loadEmbeddingModel(options) {
|
|
1193
|
-
const { verbose = false } = options || {};
|
|
1194
|
-
const startTime = Date.now();
|
|
1195
|
-
// Already loaded
|
|
1196
|
-
if (embeddingModelState?.loaded) {
|
|
1197
|
-
return {
|
|
1198
|
-
success: true,
|
|
1199
|
-
dimensions: embeddingModelState.dimensions,
|
|
1200
|
-
modelName: 'cached',
|
|
1201
|
-
loadTime: 0
|
|
1202
|
-
};
|
|
1203
|
-
}
|
|
1204
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
1205
|
-
const bridge = await getBridge();
|
|
1206
|
-
if (bridge) {
|
|
1207
|
-
const bridgeResult = await bridge.bridgeLoadEmbeddingModel();
|
|
1208
|
-
if (bridgeResult && bridgeResult.success) {
|
|
1209
|
-
// Mark local state as loaded too so subsequent calls use cache
|
|
1210
|
-
embeddingModelState = {
|
|
1211
|
-
loaded: true,
|
|
1212
|
-
model: null, // Bridge handles embedding
|
|
1213
|
-
tokenizer: null,
|
|
1214
|
-
dimensions: bridgeResult.dimensions
|
|
1215
|
-
};
|
|
1216
|
-
return bridgeResult;
|
|
1217
|
-
}
|
|
1218
|
-
}
|
|
1219
|
-
try {
|
|
1220
|
-
// Try to import @xenova/transformers for ONNX embeddings
|
|
1221
|
-
const transformers = await import('@xenova/transformers').catch(() => null);
|
|
1222
|
-
if (transformers) {
|
|
1223
|
-
if (verbose) {
|
|
1224
|
-
console.log('Loading ONNX embedding model (all-MiniLM-L6-v2)...');
|
|
1225
|
-
}
|
|
1226
|
-
// Use small, fast model for local embeddings
|
|
1227
|
-
const { pipeline } = transformers;
|
|
1228
|
-
const embedder = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
|
|
1229
|
-
embeddingModelState = {
|
|
1230
|
-
loaded: true,
|
|
1231
|
-
model: embedder,
|
|
1232
|
-
tokenizer: null,
|
|
1233
|
-
dimensions: 384 // MiniLM-L6 produces 384-dim vectors
|
|
1234
|
-
};
|
|
1235
|
-
return {
|
|
1236
|
-
success: true,
|
|
1237
|
-
dimensions: 384,
|
|
1238
|
-
modelName: 'Xenova/all-MiniLM-L6-v2',
|
|
1239
|
-
loadTime: Date.now() - startTime
|
|
1240
|
-
};
|
|
1241
|
-
}
|
|
1242
|
-
// Fallback: Check for agentic-flow ReasoningBank embeddings (v1)
|
|
1243
|
-
const reasoningBank = await import('agentic-flow/reasoningbank').catch(() => null);
|
|
1244
|
-
if (reasoningBank?.computeEmbedding) {
|
|
1245
|
-
if (verbose) {
|
|
1246
|
-
console.log('Loading agentic-flow ReasoningBank embedding model...');
|
|
1247
|
-
}
|
|
1248
|
-
embeddingModelState = {
|
|
1249
|
-
loaded: true,
|
|
1250
|
-
model: { embed: reasoningBank.computeEmbedding },
|
|
1251
|
-
tokenizer: null,
|
|
1252
|
-
dimensions: 768
|
|
1253
|
-
};
|
|
1254
|
-
return {
|
|
1255
|
-
success: true,
|
|
1256
|
-
dimensions: 768,
|
|
1257
|
-
modelName: 'agentic-flow/reasoningbank',
|
|
1258
|
-
loadTime: Date.now() - startTime
|
|
1259
|
-
};
|
|
1260
|
-
}
|
|
1261
|
-
// Fallback: Check for monovector ONNX embedder (bundled MiniLM-L6-v2 since v0.2.15)
|
|
1262
|
-
// v0.2.16: LoRA B=0 fix makes AdaptiveEmbedder safe (identity when untrained)
|
|
1263
|
-
// Note: isReady() returns false until first embed() call (lazy init), so we
|
|
1264
|
-
// skip the isReady() gate and verify with a probe embed instead.
|
|
1265
|
-
const monovector = await import('monovector').catch(() => null);
|
|
1266
|
-
if (monovector?.initOnnxEmbedder) {
|
|
1267
|
-
try {
|
|
1268
|
-
await monovector.initOnnxEmbedder();
|
|
1269
|
-
// Fallback: OptimizedOnnxEmbedder (raw ONNX, lazy-inits on first embed)
|
|
1270
|
-
const onnxEmb = monovector.getOptimizedOnnxEmbedder?.();
|
|
1271
|
-
if (onnxEmb?.embed) {
|
|
1272
|
-
// Probe embed to trigger lazy ONNX init and verify it works
|
|
1273
|
-
const probe = await onnxEmb.embed('test');
|
|
1274
|
-
if (probe && probe.length > 0 && (Array.isArray(probe) ? probe.some((v) => v !== 0) : true)) {
|
|
1275
|
-
if (verbose) {
|
|
1276
|
-
console.log(`Loading monovector ONNX embedder (all-MiniLM-L6-v2, ${probe.length}d)...`);
|
|
1277
|
-
}
|
|
1278
|
-
embeddingModelState = {
|
|
1279
|
-
loaded: true,
|
|
1280
|
-
model: (text) => onnxEmb.embed(text),
|
|
1281
|
-
tokenizer: null,
|
|
1282
|
-
dimensions: probe.length || 384
|
|
1283
|
-
};
|
|
1284
|
-
return {
|
|
1285
|
-
success: true,
|
|
1286
|
-
dimensions: probe.length || 384,
|
|
1287
|
-
modelName: 'monovector/onnx',
|
|
1288
|
-
loadTime: Date.now() - startTime
|
|
1289
|
-
};
|
|
1290
|
-
}
|
|
1291
|
-
}
|
|
1292
|
-
}
|
|
1293
|
-
catch {
|
|
1294
|
-
// monovector ONNX init failed, continue to next fallback
|
|
1295
|
-
}
|
|
1296
|
-
}
|
|
1297
|
-
// Legacy fallback: Check for agentic-flow core embeddings
|
|
1298
|
-
const agenticFlow = await import('agentic-flow').catch(() => null);
|
|
1299
|
-
if (agenticFlow && agenticFlow.embeddings) {
|
|
1300
|
-
if (verbose) {
|
|
1301
|
-
console.log('Loading agentic-flow embedding model...');
|
|
1302
|
-
}
|
|
1303
|
-
embeddingModelState = {
|
|
1304
|
-
loaded: true,
|
|
1305
|
-
model: agenticFlow.embeddings,
|
|
1306
|
-
tokenizer: null,
|
|
1307
|
-
dimensions: 768
|
|
1308
|
-
};
|
|
1309
|
-
return {
|
|
1310
|
-
success: true,
|
|
1311
|
-
dimensions: 768,
|
|
1312
|
-
modelName: 'agentic-flow',
|
|
1313
|
-
loadTime: Date.now() - startTime
|
|
1314
|
-
};
|
|
1315
|
-
}
|
|
1316
|
-
// No ONNX model available - use fallback
|
|
1317
|
-
embeddingModelState = {
|
|
1318
|
-
loaded: true,
|
|
1319
|
-
model: null, // Will use simple hash-based fallback
|
|
1320
|
-
tokenizer: null,
|
|
1321
|
-
dimensions: 128 // Smaller fallback dimensions
|
|
1322
|
-
};
|
|
1323
|
-
return {
|
|
1324
|
-
success: true,
|
|
1325
|
-
dimensions: 128,
|
|
1326
|
-
modelName: 'hash-fallback',
|
|
1327
|
-
loadTime: Date.now() - startTime
|
|
1328
|
-
};
|
|
1329
|
-
}
|
|
1330
|
-
catch (error) {
|
|
1331
|
-
return {
|
|
1332
|
-
success: false,
|
|
1333
|
-
dimensions: 0,
|
|
1334
|
-
modelName: 'none',
|
|
1335
|
-
error: error instanceof Error ? error.message : String(error)
|
|
1336
|
-
};
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
/**
|
|
1340
|
-
* Generate real embedding for text
|
|
1341
|
-
* Uses ONNX model if available, falls back to deterministic hash
|
|
1342
|
-
*/
|
|
1343
|
-
export async function generateEmbedding(text) {
|
|
1344
|
-
// Cap input text — caller may pass arbitrarily large content. Without this
|
|
1345
|
-
// cap, the hash-fallback below burns O(text.length × dimension) sin() calls
|
|
1346
|
-
// per call, and ONNX tokenization can saturate memory on multi-MB inputs.
|
|
1347
|
-
if (typeof text !== 'string')
|
|
1348
|
-
text = String(text ?? '');
|
|
1349
|
-
if (text.length > 16 * 1024)
|
|
1350
|
-
text = text.slice(0, 16 * 1024);
|
|
1351
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
1352
|
-
const bridge = await getBridge();
|
|
1353
|
-
if (bridge) {
|
|
1354
|
-
const bridgeResult = await bridge.bridgeGenerateEmbedding(text);
|
|
1355
|
-
if (bridgeResult)
|
|
1356
|
-
return bridgeResult;
|
|
1357
|
-
}
|
|
1358
|
-
// Ensure model is loaded
|
|
1359
|
-
if (!embeddingModelState?.loaded) {
|
|
1360
|
-
await loadEmbeddingModel();
|
|
1361
|
-
}
|
|
1362
|
-
const state = embeddingModelState;
|
|
1363
|
-
// Use ONNX model if available
|
|
1364
|
-
if (state.model && typeof state.model === 'function') {
|
|
1365
|
-
try {
|
|
1366
|
-
const output = await state.model(text, { pooling: 'mean', normalize: true });
|
|
1367
|
-
// Handle both @xenova/transformers (output.data) and monovector (plain array) formats
|
|
1368
|
-
const embedding = output?.data
|
|
1369
|
-
? Array.from(output.data)
|
|
1370
|
-
: Array.isArray(output) ? output : null;
|
|
1371
|
-
if (embedding) {
|
|
1372
|
-
return {
|
|
1373
|
-
embedding,
|
|
1374
|
-
dimensions: embedding.length,
|
|
1375
|
-
model: 'onnx'
|
|
1376
|
-
};
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
catch {
|
|
1380
|
-
// Fall through to fallback
|
|
1381
|
-
}
|
|
1382
|
-
}
|
|
1383
|
-
// Deterministic hash-based fallback (for testing/demo without ONNX)
|
|
1384
|
-
const embedding = generateHashEmbedding(text, state.dimensions);
|
|
1385
|
-
return {
|
|
1386
|
-
embedding,
|
|
1387
|
-
dimensions: state.dimensions,
|
|
1388
|
-
model: 'hash-fallback'
|
|
1389
|
-
};
|
|
1390
|
-
}
|
|
1391
|
-
/**
|
|
1392
|
-
* Generate embeddings for multiple texts
|
|
1393
|
-
* Uses parallel execution for API-based providers (2-4x faster)
|
|
1394
|
-
* Note: Local ONNX inference is CPU-bound, so parallelism has limited benefit
|
|
1395
|
-
*
|
|
1396
|
-
* @param texts - Array of texts to embed
|
|
1397
|
-
* @param options - Batch options
|
|
1398
|
-
* @returns Array of embedding results with timing info
|
|
1399
|
-
*/
|
|
1400
|
-
export async function generateBatchEmbeddings(texts, options) {
|
|
1401
|
-
const { concurrency = texts.length, onProgress } = options || {};
|
|
1402
|
-
const startTime = Date.now();
|
|
1403
|
-
// Ensure model is loaded first (prevents cold start in parallel)
|
|
1404
|
-
if (!embeddingModelState?.loaded) {
|
|
1405
|
-
await loadEmbeddingModel();
|
|
1406
|
-
}
|
|
1407
|
-
// Process in parallel with optional concurrency limit
|
|
1408
|
-
if (concurrency >= texts.length) {
|
|
1409
|
-
// Full parallelism
|
|
1410
|
-
const embeddings = await Promise.all(texts.map(async (text, i) => {
|
|
1411
|
-
const result = await generateEmbedding(text);
|
|
1412
|
-
onProgress?.(i + 1, texts.length);
|
|
1413
|
-
return { text, ...result };
|
|
1414
|
-
}));
|
|
1415
|
-
const totalTime = Date.now() - startTime;
|
|
1416
|
-
return {
|
|
1417
|
-
results: embeddings,
|
|
1418
|
-
totalTime,
|
|
1419
|
-
avgTime: totalTime / texts.length
|
|
1420
|
-
};
|
|
1421
|
-
}
|
|
1422
|
-
// Limited concurrency using chunking
|
|
1423
|
-
const results = [];
|
|
1424
|
-
let completed = 0;
|
|
1425
|
-
for (let i = 0; i < texts.length; i += concurrency) {
|
|
1426
|
-
const chunk = texts.slice(i, i + concurrency);
|
|
1427
|
-
const chunkResults = await Promise.all(chunk.map(async (text) => {
|
|
1428
|
-
const result = await generateEmbedding(text);
|
|
1429
|
-
completed++;
|
|
1430
|
-
onProgress?.(completed, texts.length);
|
|
1431
|
-
return { text, ...result };
|
|
1432
|
-
}));
|
|
1433
|
-
results.push(...chunkResults);
|
|
1434
|
-
}
|
|
1435
|
-
const totalTime = Date.now() - startTime;
|
|
1436
|
-
return {
|
|
1437
|
-
results,
|
|
1438
|
-
totalTime,
|
|
1439
|
-
avgTime: totalTime / texts.length
|
|
1440
|
-
};
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
|
-
* Generate deterministic hash-based embedding
|
|
1444
|
-
* Not semantic, but deterministic and useful for testing
|
|
1445
|
-
*/
|
|
1446
|
-
function generateHashEmbedding(text, dimensions) {
|
|
1447
|
-
const embedding = new Array(dimensions).fill(0);
|
|
1448
|
-
// Simple hash-based approach for reproducibility
|
|
1449
|
-
const words = text.toLowerCase().split(/\s+/);
|
|
1450
|
-
for (let i = 0; i < words.length; i++) {
|
|
1451
|
-
const word = words[i];
|
|
1452
|
-
for (let j = 0; j < word.length; j++) {
|
|
1453
|
-
const charCode = word.charCodeAt(j);
|
|
1454
|
-
const idx = (charCode * (i + 1) * (j + 1)) % dimensions;
|
|
1455
|
-
embedding[idx] += Math.sin(charCode * 0.1) * 0.1;
|
|
1456
|
-
}
|
|
1457
|
-
}
|
|
1458
|
-
// Normalize to unit vector
|
|
1459
|
-
const magnitude = Math.sqrt(embedding.reduce((sum, v) => sum + v * v, 0)) || 1;
|
|
1460
|
-
return embedding.map(v => v / magnitude);
|
|
1461
|
-
}
|
|
1462
|
-
/**
|
|
1463
|
-
* Verify memory initialization works correctly
|
|
1464
|
-
* Tests: write, read, search, patterns
|
|
1465
|
-
*/
|
|
1466
|
-
export async function verifyMemoryInit(dbPath, options) {
|
|
1467
|
-
const { verbose = false } = options || {};
|
|
1468
|
-
const tests = [];
|
|
1469
|
-
try {
|
|
1470
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
1471
|
-
const SQL = await initSqlJs();
|
|
1472
|
-
const fs = await import('fs');
|
|
1473
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
1474
|
-
const verifyStat = fs.statSync(dbPath);
|
|
1475
|
-
if (verifyStat.size > MAX_DB_FILE_BYTES) {
|
|
1476
|
-
return { success: false, tests: [{ name: 'Database access', passed: false, details: `File too large: ${verifyStat.size} bytes` }], summary: { passed: 0, failed: 1, total: 1 } };
|
|
1477
|
-
}
|
|
1478
|
-
// Load database
|
|
1479
|
-
const fileBuffer = fs.readFileSync(dbPath);
|
|
1480
|
-
const db = new SQL.Database(fileBuffer);
|
|
1481
|
-
// Test 1: Schema verification
|
|
1482
|
-
const schemaStart = Date.now();
|
|
1483
|
-
const tables = db.exec("SELECT name FROM sqlite_master WHERE type='table'");
|
|
1484
|
-
const tableNames = tables[0]?.values?.map(v => v[0]) || [];
|
|
1485
|
-
const expectedTables = ['memory_entries', 'patterns', 'metadata', 'vector_indexes'];
|
|
1486
|
-
const missingTables = expectedTables.filter(t => !tableNames.includes(t));
|
|
1487
|
-
tests.push({
|
|
1488
|
-
name: 'Schema verification',
|
|
1489
|
-
passed: missingTables.length === 0,
|
|
1490
|
-
details: missingTables.length > 0 ? `Missing: ${missingTables.join(', ')}` : `${tableNames.length} tables found`,
|
|
1491
|
-
duration: Date.now() - schemaStart
|
|
1492
|
-
});
|
|
1493
|
-
// Test 2: Write entry
|
|
1494
|
-
const writeStart = Date.now();
|
|
1495
|
-
const testId = `test_${Date.now()}`;
|
|
1496
|
-
const testKey = 'verification_test';
|
|
1497
|
-
const testValue = 'This is a verification test entry for memory initialization';
|
|
1498
|
-
try {
|
|
1499
|
-
db.run(`
|
|
1500
|
-
INSERT INTO memory_entries (id, key, namespace, content, type, created_at, updated_at)
|
|
1501
|
-
VALUES (?, ?, 'test', ?, 'semantic', ?, ?)
|
|
1502
|
-
`, [testId, testKey, testValue, Date.now(), Date.now()]);
|
|
1503
|
-
tests.push({
|
|
1504
|
-
name: 'Write entry',
|
|
1505
|
-
passed: true,
|
|
1506
|
-
details: 'Entry written successfully',
|
|
1507
|
-
duration: Date.now() - writeStart
|
|
1508
|
-
});
|
|
1509
|
-
}
|
|
1510
|
-
catch (e) {
|
|
1511
|
-
tests.push({
|
|
1512
|
-
name: 'Write entry',
|
|
1513
|
-
passed: false,
|
|
1514
|
-
details: e instanceof Error ? e.message : 'Write failed',
|
|
1515
|
-
duration: Date.now() - writeStart
|
|
1516
|
-
});
|
|
1517
|
-
}
|
|
1518
|
-
// Test 3: Read entry
|
|
1519
|
-
const readStart = Date.now();
|
|
1520
|
-
try {
|
|
1521
|
-
const result = db.exec(`SELECT content FROM memory_entries WHERE id = ?`, [testId]);
|
|
1522
|
-
const content = result[0]?.values[0]?.[0];
|
|
1523
|
-
tests.push({
|
|
1524
|
-
name: 'Read entry',
|
|
1525
|
-
passed: content === testValue,
|
|
1526
|
-
details: content === testValue ? 'Content matches' : 'Content mismatch',
|
|
1527
|
-
duration: Date.now() - readStart
|
|
1528
|
-
});
|
|
1529
|
-
}
|
|
1530
|
-
catch (e) {
|
|
1531
|
-
tests.push({
|
|
1532
|
-
name: 'Read entry',
|
|
1533
|
-
passed: false,
|
|
1534
|
-
details: e instanceof Error ? e.message : 'Read failed',
|
|
1535
|
-
duration: Date.now() - readStart
|
|
1536
|
-
});
|
|
1537
|
-
}
|
|
1538
|
-
// Test 4: Write with embedding
|
|
1539
|
-
const embeddingStart = Date.now();
|
|
1540
|
-
try {
|
|
1541
|
-
const { embedding, dimensions, model } = await generateEmbedding(testValue);
|
|
1542
|
-
const embeddingJson = JSON.stringify(embedding);
|
|
1543
|
-
db.run(`
|
|
1544
|
-
UPDATE memory_entries
|
|
1545
|
-
SET embedding = ?, embedding_dimensions = ?, embedding_model = ?
|
|
1546
|
-
WHERE id = ?
|
|
1547
|
-
`, [embeddingJson, dimensions, model, testId]);
|
|
1548
|
-
tests.push({
|
|
1549
|
-
name: 'Generate embedding',
|
|
1550
|
-
passed: true,
|
|
1551
|
-
details: `${dimensions}-dim vector (${model})`,
|
|
1552
|
-
duration: Date.now() - embeddingStart
|
|
1553
|
-
});
|
|
1554
|
-
}
|
|
1555
|
-
catch (e) {
|
|
1556
|
-
tests.push({
|
|
1557
|
-
name: 'Generate embedding',
|
|
1558
|
-
passed: false,
|
|
1559
|
-
details: e instanceof Error ? e.message : 'Embedding failed',
|
|
1560
|
-
duration: Date.now() - embeddingStart
|
|
1561
|
-
});
|
|
1562
|
-
}
|
|
1563
|
-
// Test 5: Pattern storage
|
|
1564
|
-
const patternStart = Date.now();
|
|
1565
|
-
try {
|
|
1566
|
-
const patternId = `pattern_${Date.now()}`;
|
|
1567
|
-
db.run(`
|
|
1568
|
-
INSERT INTO patterns (id, name, pattern_type, condition, action, confidence, created_at, updated_at)
|
|
1569
|
-
VALUES (?, 'test-pattern', 'task-routing', 'test condition', 'test action', 0.5, ?, ?)
|
|
1570
|
-
`, [patternId, Date.now(), Date.now()]);
|
|
1571
|
-
tests.push({
|
|
1572
|
-
name: 'Pattern storage',
|
|
1573
|
-
passed: true,
|
|
1574
|
-
details: 'Pattern stored with confidence scoring',
|
|
1575
|
-
duration: Date.now() - patternStart
|
|
1576
|
-
});
|
|
1577
|
-
// Cleanup test pattern
|
|
1578
|
-
db.run(`DELETE FROM patterns WHERE id = ?`, [patternId]);
|
|
1579
|
-
}
|
|
1580
|
-
catch (e) {
|
|
1581
|
-
tests.push({
|
|
1582
|
-
name: 'Pattern storage',
|
|
1583
|
-
passed: false,
|
|
1584
|
-
details: e instanceof Error ? e.message : 'Pattern storage failed',
|
|
1585
|
-
duration: Date.now() - patternStart
|
|
1586
|
-
});
|
|
1587
|
-
}
|
|
1588
|
-
// Test 6: Vector index configuration
|
|
1589
|
-
const indexStart = Date.now();
|
|
1590
|
-
try {
|
|
1591
|
-
const indexResult = db.exec(`SELECT name, dimensions, hnsw_m, hnsw_ef_construction FROM vector_indexes`);
|
|
1592
|
-
const indexes = indexResult[0]?.values || [];
|
|
1593
|
-
tests.push({
|
|
1594
|
-
name: 'Vector index config',
|
|
1595
|
-
passed: indexes.length > 0,
|
|
1596
|
-
details: `${indexes.length} indexes configured (HNSW M=16, ef=200)`,
|
|
1597
|
-
duration: Date.now() - indexStart
|
|
1598
|
-
});
|
|
1599
|
-
}
|
|
1600
|
-
catch (e) {
|
|
1601
|
-
tests.push({
|
|
1602
|
-
name: 'Vector index config',
|
|
1603
|
-
passed: false,
|
|
1604
|
-
details: e instanceof Error ? e.message : 'Index check failed',
|
|
1605
|
-
duration: Date.now() - indexStart
|
|
1606
|
-
});
|
|
1607
|
-
}
|
|
1608
|
-
// Cleanup test entry
|
|
1609
|
-
db.run(`DELETE FROM memory_entries WHERE id = ?`, [testId]);
|
|
1610
|
-
// Save changes atomically
|
|
1611
|
-
const data = db.export();
|
|
1612
|
-
const dbTmpHealth = dbPath + '.tmp';
|
|
1613
|
-
fs.writeFileSync(dbTmpHealth, Buffer.from(data));
|
|
1614
|
-
fs.renameSync(dbTmpHealth, dbPath);
|
|
1615
|
-
db.close();
|
|
1616
|
-
const passed = tests.filter(t => t.passed).length;
|
|
1617
|
-
const failed = tests.filter(t => !t.passed).length;
|
|
1618
|
-
return {
|
|
1619
|
-
success: failed === 0,
|
|
1620
|
-
tests,
|
|
1621
|
-
summary: {
|
|
1622
|
-
passed,
|
|
1623
|
-
failed,
|
|
1624
|
-
total: tests.length
|
|
1625
|
-
}
|
|
1626
|
-
};
|
|
1627
|
-
}
|
|
1628
|
-
catch (error) {
|
|
1629
|
-
return {
|
|
1630
|
-
success: false,
|
|
1631
|
-
tests: [{
|
|
1632
|
-
name: 'Database access',
|
|
1633
|
-
passed: false,
|
|
1634
|
-
details: error instanceof Error ? error.message : 'Unknown error'
|
|
1635
|
-
}],
|
|
1636
|
-
summary: { passed: 0, failed: 1, total: 1 }
|
|
1637
|
-
};
|
|
1638
|
-
}
|
|
1639
|
-
}
|
|
1640
|
-
/**
|
|
1641
|
-
* Store an entry directly using sql.js
|
|
1642
|
-
* This bypasses MCP and writes directly to the database
|
|
1643
|
-
*/
|
|
1644
|
-
export async function storeEntry(options) {
|
|
1645
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
1646
|
-
const bridge = await getBridge();
|
|
1647
|
-
if (bridge) {
|
|
1648
|
-
const bridgeResult = await bridge.bridgeStoreEntry(options);
|
|
1649
|
-
if (bridgeResult)
|
|
1650
|
-
return bridgeResult;
|
|
1651
|
-
}
|
|
1652
|
-
// Fallback: raw sql.js
|
|
1653
|
-
const { key, value, namespace = 'default', generateEmbeddingFlag = true, tags = [], ttl, dbPath: customPath, upsert = false } = options;
|
|
1654
|
-
const swarmDir = path.resolve(process.cwd(), '.swarm');
|
|
1655
|
-
const dbPath = customPath ? path.resolve(customPath) : path.join(swarmDir, 'memory.db');
|
|
1656
|
-
try {
|
|
1657
|
-
if (!fs.existsSync(dbPath)) {
|
|
1658
|
-
return { success: false, id: '', error: 'Database not initialized. Run: monomind memory init' };
|
|
1659
|
-
}
|
|
1660
|
-
// Ensure schema has all required columns (migration for older DBs)
|
|
1661
|
-
await ensureSchemaColumns(dbPath);
|
|
1662
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
1663
|
-
const SQL = await initSqlJs();
|
|
1664
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
1665
|
-
const storeStat = fs.statSync(dbPath);
|
|
1666
|
-
if (storeStat.size > MAX_DB_FILE_BYTES) {
|
|
1667
|
-
return { success: false, id: '', error: `Database file too large: ${storeStat.size} bytes` };
|
|
1668
|
-
}
|
|
1669
|
-
const fileBuffer = fs.readFileSync(dbPath);
|
|
1670
|
-
const db = new SQL.Database(fileBuffer);
|
|
1671
|
-
const id = `entry_${Date.now()}_${Math.random().toString(36).substring(7)}`;
|
|
1672
|
-
const now = Date.now();
|
|
1673
|
-
// Generate embedding if requested
|
|
1674
|
-
let embeddingJson = null;
|
|
1675
|
-
let embeddingDimensions = null;
|
|
1676
|
-
let embeddingModel = null;
|
|
1677
|
-
if (generateEmbeddingFlag && value.length > 0) {
|
|
1678
|
-
const embResult = await generateEmbedding(value);
|
|
1679
|
-
embeddingJson = JSON.stringify(embResult.embedding);
|
|
1680
|
-
embeddingDimensions = embResult.dimensions;
|
|
1681
|
-
embeddingModel = embResult.model;
|
|
1682
|
-
}
|
|
1683
|
-
// Insert or update entry (upsert mode uses REPLACE)
|
|
1684
|
-
const insertSql = upsert
|
|
1685
|
-
? `INSERT OR REPLACE INTO memory_entries (
|
|
1686
|
-
id, key, namespace, content, type,
|
|
1687
|
-
embedding, embedding_dimensions, embedding_model,
|
|
1688
|
-
tags, metadata, created_at, updated_at, expires_at, status
|
|
1689
|
-
) VALUES (?, ?, ?, ?, 'semantic', ?, ?, ?, ?, ?, ?, ?, ?, 'active')`
|
|
1690
|
-
: `INSERT INTO memory_entries (
|
|
1691
|
-
id, key, namespace, content, type,
|
|
1692
|
-
embedding, embedding_dimensions, embedding_model,
|
|
1693
|
-
tags, metadata, created_at, updated_at, expires_at, status
|
|
1694
|
-
) VALUES (?, ?, ?, ?, 'semantic', ?, ?, ?, ?, ?, ?, ?, ?, 'active')`;
|
|
1695
|
-
db.run(insertSql, [
|
|
1696
|
-
id,
|
|
1697
|
-
key,
|
|
1698
|
-
namespace,
|
|
1699
|
-
value,
|
|
1700
|
-
embeddingJson,
|
|
1701
|
-
embeddingDimensions,
|
|
1702
|
-
embeddingModel,
|
|
1703
|
-
tags.length > 0 ? JSON.stringify(tags) : null,
|
|
1704
|
-
'{}',
|
|
1705
|
-
now,
|
|
1706
|
-
now,
|
|
1707
|
-
ttl ? now + (ttl * 1000) : null
|
|
1708
|
-
]);
|
|
1709
|
-
// Save atomically
|
|
1710
|
-
const data = db.export();
|
|
1711
|
-
const dbTmpStore = dbPath + '.tmp';
|
|
1712
|
-
fs.writeFileSync(dbTmpStore, Buffer.from(data));
|
|
1713
|
-
fs.renameSync(dbTmpStore, dbPath);
|
|
1714
|
-
db.close();
|
|
1715
|
-
// Add to HNSW index for faster future searches (validated to reject malformed embeddings)
|
|
1716
|
-
if (embeddingJson) {
|
|
1717
|
-
const embResult = safeParseEmbedding(embeddingJson);
|
|
1718
|
-
if (embResult) {
|
|
1719
|
-
await addToHNSWIndex(id, embResult, {
|
|
1720
|
-
id,
|
|
1721
|
-
key,
|
|
1722
|
-
namespace,
|
|
1723
|
-
content: value
|
|
1724
|
-
});
|
|
1725
|
-
}
|
|
1726
|
-
}
|
|
1727
|
-
return {
|
|
1728
|
-
success: true,
|
|
1729
|
-
id,
|
|
1730
|
-
embedding: embeddingJson ? { dimensions: embeddingDimensions, model: embeddingModel } : undefined
|
|
1731
|
-
};
|
|
1732
|
-
}
|
|
1733
|
-
catch (error) {
|
|
1734
|
-
return {
|
|
1735
|
-
success: false,
|
|
1736
|
-
id: '',
|
|
1737
|
-
error: error instanceof Error ? error.message : String(error)
|
|
1738
|
-
};
|
|
1739
|
-
}
|
|
1740
|
-
}
|
|
1741
|
-
/**
|
|
1742
|
-
* Search entries using sql.js with vector similarity
|
|
1743
|
-
* Uses HNSW index for 150x faster search when available
|
|
1744
|
-
*/
|
|
1745
|
-
export async function searchEntries(options) {
|
|
1746
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
1747
|
-
const bridge = await getBridge();
|
|
1748
|
-
if (bridge) {
|
|
1749
|
-
const bridgeResult = await bridge.bridgeSearchEntries(options);
|
|
1750
|
-
if (bridgeResult)
|
|
1751
|
-
return bridgeResult;
|
|
1752
|
-
}
|
|
1753
|
-
// Fallback: raw sql.js
|
|
1754
|
-
const { query, namespace, limit = 10, threshold = 0.3, dbPath: customPath } = options;
|
|
1755
|
-
const effectiveNamespace = namespace || 'all';
|
|
1756
|
-
const swarmDir = path.resolve(process.cwd(), '.swarm');
|
|
1757
|
-
const dbPath = customPath ? path.resolve(customPath) : path.join(swarmDir, 'memory.db');
|
|
1758
|
-
const startTime = Date.now();
|
|
1759
|
-
try {
|
|
1760
|
-
if (!fs.existsSync(dbPath)) {
|
|
1761
|
-
return { success: false, results: [], searchTime: 0, error: 'Database not found' };
|
|
1762
|
-
}
|
|
1763
|
-
// Ensure schema has all required columns (migration for older DBs)
|
|
1764
|
-
await ensureSchemaColumns(dbPath);
|
|
1765
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
1766
|
-
const searchStat = fs.statSync(dbPath);
|
|
1767
|
-
if (searchStat.size > MAX_DB_FILE_BYTES) {
|
|
1768
|
-
return { success: false, results: [], searchTime: 0, error: `Database file too large: ${searchStat.size} bytes` };
|
|
1769
|
-
}
|
|
1770
|
-
// Generate query embedding
|
|
1771
|
-
const queryEmb = await generateEmbedding(query);
|
|
1772
|
-
const queryEmbedding = queryEmb.embedding;
|
|
1773
|
-
// Try HNSW search first (150x faster)
|
|
1774
|
-
const hnswResults = await searchHNSWIndex(queryEmbedding, { k: limit, namespace: effectiveNamespace });
|
|
1775
|
-
if (hnswResults && hnswResults.length > 0) {
|
|
1776
|
-
// Filter by threshold
|
|
1777
|
-
const filtered = hnswResults.filter(r => r.score >= threshold);
|
|
1778
|
-
return {
|
|
1779
|
-
success: true,
|
|
1780
|
-
results: filtered,
|
|
1781
|
-
searchTime: Date.now() - startTime
|
|
1782
|
-
};
|
|
1783
|
-
}
|
|
1784
|
-
// Fall back to brute-force SQLite search
|
|
1785
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
1786
|
-
const SQL = await initSqlJs();
|
|
1787
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
1788
|
-
const searchFbStat = fs.statSync(dbPath);
|
|
1789
|
-
if (searchFbStat.size > MAX_DB_FILE_BYTES) {
|
|
1790
|
-
return { success: false, results: [], searchTime: Date.now() - startTime, error: `Database file too large: ${searchFbStat.size} bytes` };
|
|
1791
|
-
}
|
|
1792
|
-
const fileBuffer = fs.readFileSync(dbPath);
|
|
1793
|
-
const db = new SQL.Database(fileBuffer);
|
|
1794
|
-
// Get entries with embeddings
|
|
1795
|
-
const searchStmt = db.prepare(effectiveNamespace !== 'all'
|
|
1796
|
-
? `SELECT id, key, namespace, content, embedding FROM memory_entries WHERE status = 'active' AND namespace = ? LIMIT 1000`
|
|
1797
|
-
: `SELECT id, key, namespace, content, embedding FROM memory_entries WHERE status = 'active' LIMIT 1000`);
|
|
1798
|
-
if (effectiveNamespace !== 'all') {
|
|
1799
|
-
searchStmt.bind([effectiveNamespace]);
|
|
1800
|
-
}
|
|
1801
|
-
const searchRows = [];
|
|
1802
|
-
while (searchStmt.step()) {
|
|
1803
|
-
searchRows.push(searchStmt.get());
|
|
1804
|
-
}
|
|
1805
|
-
searchStmt.free();
|
|
1806
|
-
const entries = searchRows.length > 0 ? [{ values: searchRows }] : [];
|
|
1807
|
-
const results = [];
|
|
1808
|
-
if (entries[0]?.values) {
|
|
1809
|
-
for (const row of entries[0].values) {
|
|
1810
|
-
const [id, key, ns, content, embeddingJson] = row;
|
|
1811
|
-
let score = 0;
|
|
1812
|
-
if (embeddingJson) {
|
|
1813
|
-
const embedding = safeParseEmbedding(embeddingJson);
|
|
1814
|
-
if (embedding && embedding.length === queryEmbedding.length) {
|
|
1815
|
-
score = cosineSim(queryEmbedding, embedding);
|
|
1816
|
-
}
|
|
1817
|
-
}
|
|
1818
|
-
// Fallback to keyword matching
|
|
1819
|
-
if (score < threshold) {
|
|
1820
|
-
const lowerContent = (content || '').toLowerCase();
|
|
1821
|
-
const lowerQuery = query.toLowerCase();
|
|
1822
|
-
const words = lowerQuery.split(/\s+/).filter(w => w.length > 0);
|
|
1823
|
-
if (words.length > 0) {
|
|
1824
|
-
const matchCount = words.filter(w => lowerContent.includes(w)).length;
|
|
1825
|
-
const keywordScore = matchCount / words.length * 0.5;
|
|
1826
|
-
score = Math.max(score, keywordScore);
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
1829
|
-
if (score >= threshold) {
|
|
1830
|
-
results.push({
|
|
1831
|
-
id: id.substring(0, 12),
|
|
1832
|
-
key: key || id.substring(0, 15),
|
|
1833
|
-
content: (content || '').substring(0, 60) + ((content || '').length > 60 ? '...' : ''),
|
|
1834
|
-
score,
|
|
1835
|
-
namespace: ns || 'default'
|
|
1836
|
-
});
|
|
1837
|
-
}
|
|
1838
|
-
}
|
|
1839
|
-
}
|
|
1840
|
-
db.close();
|
|
1841
|
-
// Sort by score
|
|
1842
|
-
results.sort((a, b) => b.score - a.score);
|
|
1843
|
-
return {
|
|
1844
|
-
success: true,
|
|
1845
|
-
results: results.slice(0, limit),
|
|
1846
|
-
searchTime: Date.now() - startTime
|
|
1847
|
-
};
|
|
1848
|
-
}
|
|
1849
|
-
catch (error) {
|
|
1850
|
-
return {
|
|
1851
|
-
success: false,
|
|
1852
|
-
results: [],
|
|
1853
|
-
searchTime: Date.now() - startTime,
|
|
1854
|
-
error: error instanceof Error ? error.message : String(error)
|
|
1855
|
-
};
|
|
1856
|
-
}
|
|
1857
|
-
}
|
|
1858
|
-
/**
|
|
1859
|
-
* Optimized cosine similarity
|
|
1860
|
-
* V8 JIT-friendly - avoids manual unrolling which can hurt performance
|
|
1861
|
-
* ~0.5μs per 384-dim vector comparison
|
|
1862
|
-
*/
|
|
1863
|
-
function cosineSim(a, b) {
|
|
1864
|
-
if (!a || !b || a.length === 0 || b.length === 0)
|
|
1865
|
-
return 0;
|
|
1866
|
-
const len = Math.min(a.length, b.length);
|
|
1867
|
-
let dot = 0, normA = 0, normB = 0;
|
|
1868
|
-
// Simple loop - V8 optimizes this well
|
|
1869
|
-
for (let i = 0; i < len; i++) {
|
|
1870
|
-
const ai = a[i], bi = b[i];
|
|
1871
|
-
dot += ai * bi;
|
|
1872
|
-
normA += ai * ai;
|
|
1873
|
-
normB += bi * bi;
|
|
1874
|
-
}
|
|
1875
|
-
// Combined sqrt for slightly better performance
|
|
1876
|
-
const mag = Math.sqrt(normA * normB);
|
|
1877
|
-
return mag === 0 ? 0 : dot / mag;
|
|
1878
|
-
}
|
|
1879
|
-
/**
|
|
1880
|
-
* List all entries from the memory database
|
|
1881
|
-
*/
|
|
1882
|
-
export async function listEntries(options) {
|
|
1883
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
1884
|
-
const bridge = await getBridge();
|
|
1885
|
-
if (bridge) {
|
|
1886
|
-
const bridgeResult = await bridge.bridgeListEntries(options);
|
|
1887
|
-
if (bridgeResult)
|
|
1888
|
-
return bridgeResult;
|
|
1889
|
-
}
|
|
1890
|
-
// Fallback: raw sql.js
|
|
1891
|
-
const { namespace, limit = 20, offset = 0, dbPath: customPath } = options;
|
|
1892
|
-
const swarmDir = path.join(process.cwd(), '.swarm');
|
|
1893
|
-
const dbPath = customPath || path.join(swarmDir, 'memory.db');
|
|
1894
|
-
try {
|
|
1895
|
-
if (!fs.existsSync(dbPath)) {
|
|
1896
|
-
return { success: false, entries: [], total: 0, error: 'Database not found' };
|
|
1897
|
-
}
|
|
1898
|
-
// Ensure schema has all required columns (migration for older DBs)
|
|
1899
|
-
await ensureSchemaColumns(dbPath);
|
|
1900
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
1901
|
-
const SQL = await initSqlJs();
|
|
1902
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
1903
|
-
const listStat = fs.statSync(dbPath);
|
|
1904
|
-
if (listStat.size > MAX_DB_FILE_BYTES) {
|
|
1905
|
-
return { success: false, entries: [], total: 0, error: `Database file too large: ${listStat.size} bytes` };
|
|
1906
|
-
}
|
|
1907
|
-
const fileBuffer = fs.readFileSync(dbPath);
|
|
1908
|
-
const db = new SQL.Database(fileBuffer);
|
|
1909
|
-
// Get total count
|
|
1910
|
-
const countStmt = namespace
|
|
1911
|
-
? db.prepare(`SELECT COUNT(*) as cnt FROM memory_entries WHERE status = 'active' AND namespace = ?`)
|
|
1912
|
-
: db.prepare(`SELECT COUNT(*) as cnt FROM memory_entries WHERE status = 'active'`);
|
|
1913
|
-
if (namespace) {
|
|
1914
|
-
countStmt.bind([namespace]);
|
|
1915
|
-
}
|
|
1916
|
-
const countRows = [];
|
|
1917
|
-
while (countStmt.step()) {
|
|
1918
|
-
countRows.push(countStmt.get());
|
|
1919
|
-
}
|
|
1920
|
-
countStmt.free();
|
|
1921
|
-
const countResult = countRows.length > 0 ? [{ values: countRows }] : [];
|
|
1922
|
-
const total = countResult[0]?.values?.[0]?.[0] || 0;
|
|
1923
|
-
// Get entries — cap limit to 10 000 to prevent full-table loads that OOM
|
|
1924
|
-
// the sql.js in-memory database on large datasets.
|
|
1925
|
-
const MAX_LIST_LIMIT = 10_000;
|
|
1926
|
-
const rawLimit = parseInt(String(limit), 10);
|
|
1927
|
-
const safeLimit = Number.isFinite(rawLimit) && rawLimit > 0 ? Math.min(rawLimit, MAX_LIST_LIMIT) : 100;
|
|
1928
|
-
const rawOffset = parseInt(String(offset), 10);
|
|
1929
|
-
const safeOffset = Number.isFinite(rawOffset) && rawOffset >= 0 ? rawOffset : 0;
|
|
1930
|
-
const listStmt = namespace
|
|
1931
|
-
? db.prepare(`SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at FROM memory_entries WHERE status = 'active' AND namespace = ? ORDER BY updated_at DESC LIMIT ? OFFSET ?`)
|
|
1932
|
-
: db.prepare(`SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at FROM memory_entries WHERE status = 'active' ORDER BY updated_at DESC LIMIT ? OFFSET ?`);
|
|
1933
|
-
if (namespace) {
|
|
1934
|
-
listStmt.bind([namespace, safeLimit, safeOffset]);
|
|
1935
|
-
}
|
|
1936
|
-
else {
|
|
1937
|
-
listStmt.bind([safeLimit, safeOffset]);
|
|
1938
|
-
}
|
|
1939
|
-
const listRows = [];
|
|
1940
|
-
while (listStmt.step()) {
|
|
1941
|
-
listRows.push(listStmt.get());
|
|
1942
|
-
}
|
|
1943
|
-
listStmt.free();
|
|
1944
|
-
const result = listRows.length > 0 ? [{ values: listRows }] : [];
|
|
1945
|
-
const entries = [];
|
|
1946
|
-
if (result[0]?.values) {
|
|
1947
|
-
for (const row of result[0].values) {
|
|
1948
|
-
const [id, key, ns, content, embedding, accessCount, createdAt, updatedAt] = row;
|
|
1949
|
-
entries.push({
|
|
1950
|
-
id: String(id).substring(0, 20),
|
|
1951
|
-
key: key || String(id).substring(0, 15),
|
|
1952
|
-
namespace: ns || 'default',
|
|
1953
|
-
size: (content || '').length,
|
|
1954
|
-
accessCount: accessCount || 0,
|
|
1955
|
-
createdAt: createdAt || new Date().toISOString(),
|
|
1956
|
-
updatedAt: updatedAt || new Date().toISOString(),
|
|
1957
|
-
hasEmbedding: !!embedding && embedding.length > 10
|
|
1958
|
-
});
|
|
1959
|
-
}
|
|
1960
|
-
}
|
|
1961
|
-
db.close();
|
|
1962
|
-
return { success: true, entries, total };
|
|
1963
|
-
}
|
|
1964
|
-
catch (error) {
|
|
1965
|
-
return {
|
|
1966
|
-
success: false,
|
|
1967
|
-
entries: [],
|
|
1968
|
-
total: 0,
|
|
1969
|
-
error: error instanceof Error ? error.message : String(error)
|
|
1970
|
-
};
|
|
1971
|
-
}
|
|
1972
|
-
}
|
|
1973
|
-
/**
|
|
1974
|
-
* Get a specific entry from the memory database
|
|
1975
|
-
*/
|
|
1976
|
-
export async function getEntry(options) {
|
|
1977
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
1978
|
-
const bridge = await getBridge();
|
|
1979
|
-
if (bridge) {
|
|
1980
|
-
const bridgeResult = await bridge.bridgeGetEntry(options);
|
|
1981
|
-
if (bridgeResult)
|
|
1982
|
-
return bridgeResult;
|
|
1983
|
-
}
|
|
1984
|
-
// Fallback: raw sql.js
|
|
1985
|
-
const { key, namespace = 'default', dbPath: customPath } = options;
|
|
1986
|
-
const swarmDir = path.join(process.cwd(), '.swarm');
|
|
1987
|
-
const dbPath = customPath || path.join(swarmDir, 'memory.db');
|
|
1988
|
-
try {
|
|
1989
|
-
if (!fs.existsSync(dbPath)) {
|
|
1990
|
-
return { success: false, found: false, error: 'Database not found' };
|
|
1991
|
-
}
|
|
1992
|
-
// Ensure schema has all required columns (migration for older DBs)
|
|
1993
|
-
await ensureSchemaColumns(dbPath);
|
|
1994
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
1995
|
-
const SQL = await initSqlJs();
|
|
1996
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
1997
|
-
const getStat = fs.statSync(dbPath);
|
|
1998
|
-
if (getStat.size > MAX_DB_FILE_BYTES) {
|
|
1999
|
-
return { success: false, found: false, error: `Database file too large: ${getStat.size} bytes` };
|
|
2000
|
-
}
|
|
2001
|
-
const fileBuffer = fs.readFileSync(dbPath);
|
|
2002
|
-
const db = new SQL.Database(fileBuffer);
|
|
2003
|
-
// Find entry by key
|
|
2004
|
-
const getStmt = db.prepare(`
|
|
2005
|
-
SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, tags
|
|
2006
|
-
FROM memory_entries
|
|
2007
|
-
WHERE status = 'active'
|
|
2008
|
-
AND key = ?
|
|
2009
|
-
AND namespace = ?
|
|
2010
|
-
LIMIT 1
|
|
2011
|
-
`);
|
|
2012
|
-
getStmt.bind([key, namespace]);
|
|
2013
|
-
const getRows = [];
|
|
2014
|
-
while (getStmt.step()) {
|
|
2015
|
-
getRows.push(getStmt.get());
|
|
2016
|
-
}
|
|
2017
|
-
getStmt.free();
|
|
2018
|
-
const result = getRows.length > 0 ? [{ values: getRows }] : [];
|
|
2019
|
-
if (!result[0]?.values?.[0]) {
|
|
2020
|
-
db.close();
|
|
2021
|
-
return { success: true, found: false };
|
|
2022
|
-
}
|
|
2023
|
-
const [id, entryKey, ns, content, embedding, accessCount, createdAt, updatedAt, tagsJson] = result[0].values[0];
|
|
2024
|
-
db.close();
|
|
2025
|
-
let tags = [];
|
|
2026
|
-
if (tagsJson) {
|
|
2027
|
-
try {
|
|
2028
|
-
tags = JSON.parse(tagsJson);
|
|
2029
|
-
}
|
|
2030
|
-
catch {
|
|
2031
|
-
// Invalid JSON
|
|
2032
|
-
}
|
|
2033
|
-
}
|
|
2034
|
-
return {
|
|
2035
|
-
success: true,
|
|
2036
|
-
found: true,
|
|
2037
|
-
entry: {
|
|
2038
|
-
id: String(id),
|
|
2039
|
-
key: entryKey || String(id),
|
|
2040
|
-
namespace: ns || 'default',
|
|
2041
|
-
content: content || '',
|
|
2042
|
-
accessCount: (accessCount || 0) + 1,
|
|
2043
|
-
createdAt: createdAt || new Date().toISOString(),
|
|
2044
|
-
updatedAt: updatedAt || new Date().toISOString(),
|
|
2045
|
-
hasEmbedding: !!embedding && embedding.length > 10,
|
|
2046
|
-
tags
|
|
2047
|
-
}
|
|
2048
|
-
};
|
|
2049
|
-
}
|
|
2050
|
-
catch (error) {
|
|
2051
|
-
return {
|
|
2052
|
-
success: false,
|
|
2053
|
-
found: false,
|
|
2054
|
-
error: error instanceof Error ? error.message : String(error)
|
|
2055
|
-
};
|
|
2056
|
-
}
|
|
2057
|
-
}
|
|
2058
|
-
/**
|
|
2059
|
-
* Delete a memory entry by key and namespace
|
|
2060
|
-
* Issue #980: Properly supports namespaced entries
|
|
2061
|
-
*/
|
|
2062
|
-
export async function deleteEntry(options) {
|
|
2063
|
-
// ADR-053: Try AgentDB v1 bridge first
|
|
2064
|
-
const bridge = await getBridge();
|
|
2065
|
-
if (bridge) {
|
|
2066
|
-
const bridgeResult = await bridge.bridgeDeleteEntry(options);
|
|
2067
|
-
if (bridgeResult) {
|
|
2068
|
-
// #1122: Bridge path must also invalidate the in-memory HNSW index.
|
|
2069
|
-
// Without this, deleted vectors remain as ghost entries in search results.
|
|
2070
|
-
if (bridgeResult.deleted && hnswIndex?.entries) {
|
|
2071
|
-
// Remove the entry from the HNSW entries map by key+namespace composite
|
|
2072
|
-
for (const [id, entry] of hnswIndex.entries) {
|
|
2073
|
-
if (entry?.key === options.key && (entry?.namespace ?? 'default') === (options.namespace ?? 'default')) {
|
|
2074
|
-
hnswIndex.entries.delete(id);
|
|
2075
|
-
break;
|
|
2076
|
-
}
|
|
2077
|
-
}
|
|
2078
|
-
saveHNSWMetadata();
|
|
2079
|
-
rebuildSearchIndex();
|
|
2080
|
-
}
|
|
2081
|
-
return bridgeResult;
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
// Fallback: raw sql.js
|
|
2085
|
-
const { key, namespace = 'default', dbPath: customPath } = options;
|
|
2086
|
-
const swarmDir = path.join(process.cwd(), '.swarm');
|
|
2087
|
-
const dbPath = customPath || path.join(swarmDir, 'memory.db');
|
|
2088
|
-
try {
|
|
2089
|
-
if (!fs.existsSync(dbPath)) {
|
|
2090
|
-
return {
|
|
2091
|
-
success: false,
|
|
2092
|
-
deleted: false,
|
|
2093
|
-
key,
|
|
2094
|
-
namespace,
|
|
2095
|
-
remainingEntries: 0,
|
|
2096
|
-
error: 'Database not found'
|
|
2097
|
-
};
|
|
2098
|
-
}
|
|
2099
|
-
// Ensure schema has all required columns (migration for older DBs)
|
|
2100
|
-
await ensureSchemaColumns(dbPath);
|
|
2101
|
-
const initSqlJs = (await import('sql.js')).default;
|
|
2102
|
-
const SQL = await initSqlJs();
|
|
2103
|
-
// Guard against excessively large DB files to prevent OOM.
|
|
2104
|
-
const deleteStat = fs.statSync(dbPath);
|
|
2105
|
-
if (deleteStat.size > MAX_DB_FILE_BYTES) {
|
|
2106
|
-
return { success: false, deleted: false, key, namespace, remainingEntries: 0, error: `Database file too large: ${deleteStat.size} bytes` };
|
|
2107
|
-
}
|
|
2108
|
-
const fileBuffer = fs.readFileSync(dbPath);
|
|
2109
|
-
const db = new SQL.Database(fileBuffer);
|
|
2110
|
-
// Check if entry exists first
|
|
2111
|
-
const checkStmt = db.prepare(`
|
|
2112
|
-
SELECT id FROM memory_entries
|
|
2113
|
-
WHERE status = 'active'
|
|
2114
|
-
AND key = ?
|
|
2115
|
-
AND namespace = ?
|
|
2116
|
-
LIMIT 1
|
|
2117
|
-
`);
|
|
2118
|
-
checkStmt.bind([key, namespace]);
|
|
2119
|
-
const checkRows = [];
|
|
2120
|
-
while (checkStmt.step()) {
|
|
2121
|
-
checkRows.push(checkStmt.get());
|
|
2122
|
-
}
|
|
2123
|
-
checkStmt.free();
|
|
2124
|
-
const checkResult = checkRows.length > 0 ? [{ values: checkRows }] : [];
|
|
2125
|
-
if (!checkResult[0]?.values?.[0]) {
|
|
2126
|
-
// Get remaining count before closing
|
|
2127
|
-
const countResult = db.exec(`SELECT COUNT(*) FROM memory_entries WHERE status = 'active'`);
|
|
2128
|
-
const remainingEntries = countResult[0]?.values?.[0]?.[0] || 0;
|
|
2129
|
-
db.close();
|
|
2130
|
-
return {
|
|
2131
|
-
success: true,
|
|
2132
|
-
deleted: false,
|
|
2133
|
-
key,
|
|
2134
|
-
namespace,
|
|
2135
|
-
remainingEntries,
|
|
2136
|
-
error: `Key '${key}' not found in namespace '${namespace}'`
|
|
2137
|
-
};
|
|
2138
|
-
}
|
|
2139
|
-
// Capture the entry ID for HNSW cleanup
|
|
2140
|
-
const entryId = String(checkResult[0].values[0][0]);
|
|
2141
|
-
// Delete the entry (soft delete by setting status to 'deleted')
|
|
2142
|
-
// Also null out the embedding to clean up vector data from SQLite
|
|
2143
|
-
db.run(`
|
|
2144
|
-
UPDATE memory_entries
|
|
2145
|
-
SET status = 'deleted',
|
|
2146
|
-
embedding = NULL,
|
|
2147
|
-
updated_at = strftime('%s', 'now') * 1000
|
|
2148
|
-
WHERE key = ?
|
|
2149
|
-
AND namespace = ?
|
|
2150
|
-
AND status = 'active'
|
|
2151
|
-
`, [key, namespace]);
|
|
2152
|
-
// Get remaining count
|
|
2153
|
-
const countResult = db.exec(`SELECT COUNT(*) FROM memory_entries WHERE status = 'active'`);
|
|
2154
|
-
const remainingEntries = countResult[0]?.values?.[0]?.[0] || 0;
|
|
2155
|
-
// Save updated database atomically
|
|
2156
|
-
const data = db.export();
|
|
2157
|
-
const dbTmpDelete = dbPath + '.tmp';
|
|
2158
|
-
fs.writeFileSync(dbTmpDelete, Buffer.from(data));
|
|
2159
|
-
fs.renameSync(dbTmpDelete, dbPath);
|
|
2160
|
-
db.close();
|
|
2161
|
-
// Clean up in-memory HNSW index so ghost vectors don't appear in searches.
|
|
2162
|
-
// Remove the entry from the HNSW entries map and invalidate the index.
|
|
2163
|
-
// The next search will rebuild the HNSW index from the remaining DB rows.
|
|
2164
|
-
if (hnswIndex?.entries) {
|
|
2165
|
-
hnswIndex.entries.delete(entryId);
|
|
2166
|
-
saveHNSWMetadata();
|
|
2167
|
-
// Invalidate the HNSW index so it rebuilds from DB on next search.
|
|
2168
|
-
// We can't surgically remove a vector from the HNSW graph, so we
|
|
2169
|
-
// clear the entire index; it will be lazily rebuilt from SQLite.
|
|
2170
|
-
rebuildSearchIndex();
|
|
2171
|
-
}
|
|
2172
|
-
return {
|
|
2173
|
-
success: true,
|
|
2174
|
-
deleted: true,
|
|
2175
|
-
key,
|
|
2176
|
-
namespace,
|
|
2177
|
-
remainingEntries
|
|
2178
|
-
};
|
|
2179
|
-
}
|
|
2180
|
-
catch (error) {
|
|
2181
|
-
return {
|
|
2182
|
-
success: false,
|
|
2183
|
-
deleted: false,
|
|
2184
|
-
key,
|
|
2185
|
-
namespace,
|
|
2186
|
-
remainingEntries: 0,
|
|
2187
|
-
error: error instanceof Error ? error.message : String(error)
|
|
2188
|
-
};
|
|
2189
|
-
}
|
|
2190
|
-
}
|
|
2191
414
|
export default {
|
|
2192
415
|
initializeMemoryDatabase,
|
|
2193
416
|
checkMemoryInitialization,
|