universal-agent-protocol 0.5.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/LICENSE +21 -0
- package/README.md +462 -0
- package/dist/analyzers/index.d.ts +3 -0
- package/dist/analyzers/index.d.ts.map +1 -0
- package/dist/analyzers/index.js +656 -0
- package/dist/analyzers/index.js.map +1 -0
- package/dist/bin/cli.d.ts +3 -0
- package/dist/bin/cli.d.ts.map +1 -0
- package/dist/bin/cli.js +506 -0
- package/dist/bin/cli.js.map +1 -0
- package/dist/bin/tool-calls.d.ts +3 -0
- package/dist/bin/tool-calls.d.ts.map +1 -0
- package/dist/bin/tool-calls.js +4 -0
- package/dist/bin/tool-calls.js.map +1 -0
- package/dist/cli/agent.d.ts +20 -0
- package/dist/cli/agent.d.ts.map +1 -0
- package/dist/cli/agent.js +434 -0
- package/dist/cli/agent.js.map +1 -0
- package/dist/cli/analyze.d.ts +7 -0
- package/dist/cli/analyze.d.ts.map +1 -0
- package/dist/cli/analyze.js +103 -0
- package/dist/cli/analyze.js.map +1 -0
- package/dist/cli/coord.d.ts +7 -0
- package/dist/cli/coord.d.ts.map +1 -0
- package/dist/cli/coord.js +138 -0
- package/dist/cli/coord.js.map +1 -0
- package/dist/cli/dashboard.d.ts +8 -0
- package/dist/cli/dashboard.d.ts.map +1 -0
- package/dist/cli/dashboard.js +704 -0
- package/dist/cli/dashboard.js.map +1 -0
- package/dist/cli/deploy.d.ts +19 -0
- package/dist/cli/deploy.d.ts.map +1 -0
- package/dist/cli/deploy.js +267 -0
- package/dist/cli/deploy.js.map +1 -0
- package/dist/cli/droids.d.ts +9 -0
- package/dist/cli/droids.d.ts.map +1 -0
- package/dist/cli/droids.js +227 -0
- package/dist/cli/droids.js.map +1 -0
- package/dist/cli/generate.d.ts +17 -0
- package/dist/cli/generate.d.ts.map +1 -0
- package/dist/cli/generate.js +432 -0
- package/dist/cli/generate.js.map +1 -0
- package/dist/cli/hooks.d.ts +9 -0
- package/dist/cli/hooks.d.ts.map +1 -0
- package/dist/cli/hooks.js +374 -0
- package/dist/cli/hooks.js.map +1 -0
- package/dist/cli/init.d.ts +11 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +316 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/mcp-router.d.ts +16 -0
- package/dist/cli/mcp-router.d.ts.map +1 -0
- package/dist/cli/mcp-router.js +143 -0
- package/dist/cli/mcp-router.js.map +1 -0
- package/dist/cli/memory.d.ts +24 -0
- package/dist/cli/memory.d.ts.map +1 -0
- package/dist/cli/memory.js +877 -0
- package/dist/cli/memory.js.map +1 -0
- package/dist/cli/model.d.ts +15 -0
- package/dist/cli/model.d.ts.map +1 -0
- package/dist/cli/model.js +270 -0
- package/dist/cli/model.js.map +1 -0
- package/dist/cli/patterns.d.ts +26 -0
- package/dist/cli/patterns.d.ts.map +1 -0
- package/dist/cli/patterns.js +587 -0
- package/dist/cli/patterns.js.map +1 -0
- package/dist/cli/setup-mcp-router.d.ts +8 -0
- package/dist/cli/setup-mcp-router.d.ts.map +1 -0
- package/dist/cli/setup-mcp-router.js +163 -0
- package/dist/cli/setup-mcp-router.js.map +1 -0
- package/dist/cli/setup.d.ts +13 -0
- package/dist/cli/setup.d.ts.map +1 -0
- package/dist/cli/setup.js +146 -0
- package/dist/cli/setup.js.map +1 -0
- package/dist/cli/sync.d.ts +7 -0
- package/dist/cli/sync.d.ts.map +1 -0
- package/dist/cli/sync.js +26 -0
- package/dist/cli/sync.js.map +1 -0
- package/dist/cli/task.d.ts +33 -0
- package/dist/cli/task.d.ts.map +1 -0
- package/dist/cli/task.js +616 -0
- package/dist/cli/task.js.map +1 -0
- package/dist/cli/tool-calls.d.ts +8 -0
- package/dist/cli/tool-calls.d.ts.map +1 -0
- package/dist/cli/tool-calls.js +239 -0
- package/dist/cli/tool-calls.js.map +1 -0
- package/dist/cli/update.d.ts +10 -0
- package/dist/cli/update.d.ts.map +1 -0
- package/dist/cli/update.js +300 -0
- package/dist/cli/update.js.map +1 -0
- package/dist/cli/visualize.d.ts +77 -0
- package/dist/cli/visualize.d.ts.map +1 -0
- package/dist/cli/visualize.js +287 -0
- package/dist/cli/visualize.js.map +1 -0
- package/dist/cli/worktree.d.ts +9 -0
- package/dist/cli/worktree.d.ts.map +1 -0
- package/dist/cli/worktree.js +175 -0
- package/dist/cli/worktree.js.map +1 -0
- package/dist/coordination/capability-router.d.ts +79 -0
- package/dist/coordination/capability-router.d.ts.map +1 -0
- package/dist/coordination/capability-router.js +324 -0
- package/dist/coordination/capability-router.js.map +1 -0
- package/dist/coordination/database.d.ts +13 -0
- package/dist/coordination/database.d.ts.map +1 -0
- package/dist/coordination/database.js +131 -0
- package/dist/coordination/database.js.map +1 -0
- package/dist/coordination/deploy-batcher.d.ts +101 -0
- package/dist/coordination/deploy-batcher.d.ts.map +1 -0
- package/dist/coordination/deploy-batcher.js +565 -0
- package/dist/coordination/deploy-batcher.js.map +1 -0
- package/dist/coordination/index.d.ts +5 -0
- package/dist/coordination/index.d.ts.map +1 -0
- package/dist/coordination/index.js +5 -0
- package/dist/coordination/index.js.map +1 -0
- package/dist/coordination/service.d.ts +81 -0
- package/dist/coordination/service.d.ts.map +1 -0
- package/dist/coordination/service.js +603 -0
- package/dist/coordination/service.js.map +1 -0
- package/dist/generators/claude-md.d.ts +3 -0
- package/dist/generators/claude-md.d.ts.map +1 -0
- package/dist/generators/claude-md.js +977 -0
- package/dist/generators/claude-md.js.map +1 -0
- package/dist/generators/template-loader.d.ts +105 -0
- package/dist/generators/template-loader.d.ts.map +1 -0
- package/dist/generators/template-loader.js +291 -0
- package/dist/generators/template-loader.js.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp-router/config/parser.d.ts +9 -0
- package/dist/mcp-router/config/parser.d.ts.map +1 -0
- package/dist/mcp-router/config/parser.js +174 -0
- package/dist/mcp-router/config/parser.js.map +1 -0
- package/dist/mcp-router/executor/client.d.ts +31 -0
- package/dist/mcp-router/executor/client.d.ts.map +1 -0
- package/dist/mcp-router/executor/client.js +187 -0
- package/dist/mcp-router/executor/client.js.map +1 -0
- package/dist/mcp-router/index.d.ts +22 -0
- package/dist/mcp-router/index.d.ts.map +1 -0
- package/dist/mcp-router/index.js +18 -0
- package/dist/mcp-router/index.js.map +1 -0
- package/dist/mcp-router/output-compressor.d.ts +26 -0
- package/dist/mcp-router/output-compressor.d.ts.map +1 -0
- package/dist/mcp-router/output-compressor.js +236 -0
- package/dist/mcp-router/output-compressor.js.map +1 -0
- package/dist/mcp-router/search/fuzzy.d.ts +26 -0
- package/dist/mcp-router/search/fuzzy.d.ts.map +1 -0
- package/dist/mcp-router/search/fuzzy.js +94 -0
- package/dist/mcp-router/search/fuzzy.js.map +1 -0
- package/dist/mcp-router/server.d.ts +50 -0
- package/dist/mcp-router/server.d.ts.map +1 -0
- package/dist/mcp-router/server.js +229 -0
- package/dist/mcp-router/server.js.map +1 -0
- package/dist/mcp-router/session-stats.d.ts +37 -0
- package/dist/mcp-router/session-stats.d.ts.map +1 -0
- package/dist/mcp-router/session-stats.js +56 -0
- package/dist/mcp-router/session-stats.js.map +1 -0
- package/dist/mcp-router/tools/discover.d.ts +37 -0
- package/dist/mcp-router/tools/discover.d.ts.map +1 -0
- package/dist/mcp-router/tools/discover.js +65 -0
- package/dist/mcp-router/tools/discover.js.map +1 -0
- package/dist/mcp-router/tools/execute.d.ts +43 -0
- package/dist/mcp-router/tools/execute.d.ts.map +1 -0
- package/dist/mcp-router/tools/execute.js +103 -0
- package/dist/mcp-router/tools/execute.js.map +1 -0
- package/dist/mcp-router/types.d.ts +62 -0
- package/dist/mcp-router/types.d.ts.map +1 -0
- package/dist/mcp-router/types.js +6 -0
- package/dist/mcp-router/types.js.map +1 -0
- package/dist/memory/adaptive-context.d.ts +146 -0
- package/dist/memory/adaptive-context.d.ts.map +1 -0
- package/dist/memory/adaptive-context.js +1022 -0
- package/dist/memory/adaptive-context.js.map +1 -0
- package/dist/memory/agent-scoped-memory.d.ts +67 -0
- package/dist/memory/agent-scoped-memory.d.ts.map +1 -0
- package/dist/memory/agent-scoped-memory.js +126 -0
- package/dist/memory/agent-scoped-memory.js.map +1 -0
- package/dist/memory/backends/base.d.ts +18 -0
- package/dist/memory/backends/base.d.ts.map +1 -0
- package/dist/memory/backends/base.js +2 -0
- package/dist/memory/backends/base.js.map +1 -0
- package/dist/memory/backends/factory.d.ts +4 -0
- package/dist/memory/backends/factory.d.ts.map +1 -0
- package/dist/memory/backends/factory.js +53 -0
- package/dist/memory/backends/factory.js.map +1 -0
- package/dist/memory/backends/github.d.ts +22 -0
- package/dist/memory/backends/github.d.ts.map +1 -0
- package/dist/memory/backends/github.js +118 -0
- package/dist/memory/backends/github.js.map +1 -0
- package/dist/memory/backends/qdrant-cloud.d.ts +32 -0
- package/dist/memory/backends/qdrant-cloud.d.ts.map +1 -0
- package/dist/memory/backends/qdrant-cloud.js +168 -0
- package/dist/memory/backends/qdrant-cloud.js.map +1 -0
- package/dist/memory/context-compressor.d.ts +74 -0
- package/dist/memory/context-compressor.d.ts.map +1 -0
- package/dist/memory/context-compressor.js +289 -0
- package/dist/memory/context-compressor.js.map +1 -0
- package/dist/memory/correction-propagator.d.ts +44 -0
- package/dist/memory/correction-propagator.d.ts.map +1 -0
- package/dist/memory/correction-propagator.js +156 -0
- package/dist/memory/correction-propagator.js.map +1 -0
- package/dist/memory/daily-log.d.ts +67 -0
- package/dist/memory/daily-log.d.ts.map +1 -0
- package/dist/memory/daily-log.js +143 -0
- package/dist/memory/daily-log.js.map +1 -0
- package/dist/memory/dynamic-retrieval.d.ts +110 -0
- package/dist/memory/dynamic-retrieval.d.ts.map +1 -0
- package/dist/memory/dynamic-retrieval.js +688 -0
- package/dist/memory/dynamic-retrieval.js.map +1 -0
- package/dist/memory/embeddings.d.ts +116 -0
- package/dist/memory/embeddings.d.ts.map +1 -0
- package/dist/memory/embeddings.js +461 -0
- package/dist/memory/embeddings.js.map +1 -0
- package/dist/memory/hierarchical-memory.d.ts +141 -0
- package/dist/memory/hierarchical-memory.d.ts.map +1 -0
- package/dist/memory/hierarchical-memory.js +477 -0
- package/dist/memory/hierarchical-memory.js.map +1 -0
- package/dist/memory/memory-consolidator.d.ts +124 -0
- package/dist/memory/memory-consolidator.d.ts.map +1 -0
- package/dist/memory/memory-consolidator.js +514 -0
- package/dist/memory/memory-consolidator.js.map +1 -0
- package/dist/memory/memory-maintenance.d.ts +39 -0
- package/dist/memory/memory-maintenance.d.ts.map +1 -0
- package/dist/memory/memory-maintenance.js +305 -0
- package/dist/memory/memory-maintenance.js.map +1 -0
- package/dist/memory/model-router.d.ts +102 -0
- package/dist/memory/model-router.d.ts.map +1 -0
- package/dist/memory/model-router.js +448 -0
- package/dist/memory/model-router.js.map +1 -0
- package/dist/memory/multi-view-memory.d.ts +134 -0
- package/dist/memory/multi-view-memory.d.ts.map +1 -0
- package/dist/memory/multi-view-memory.js +420 -0
- package/dist/memory/multi-view-memory.js.map +1 -0
- package/dist/memory/prepopulate.d.ts +76 -0
- package/dist/memory/prepopulate.d.ts.map +1 -0
- package/dist/memory/prepopulate.js +815 -0
- package/dist/memory/prepopulate.js.map +1 -0
- package/dist/memory/semantic-compression.d.ts +77 -0
- package/dist/memory/semantic-compression.d.ts.map +1 -0
- package/dist/memory/semantic-compression.js +348 -0
- package/dist/memory/semantic-compression.js.map +1 -0
- package/dist/memory/serverless-qdrant.d.ts +102 -0
- package/dist/memory/serverless-qdrant.d.ts.map +1 -0
- package/dist/memory/serverless-qdrant.js +369 -0
- package/dist/memory/serverless-qdrant.js.map +1 -0
- package/dist/memory/short-term/factory.d.ts +26 -0
- package/dist/memory/short-term/factory.d.ts.map +1 -0
- package/dist/memory/short-term/factory.js +28 -0
- package/dist/memory/short-term/factory.js.map +1 -0
- package/dist/memory/short-term/indexeddb.d.ts +25 -0
- package/dist/memory/short-term/indexeddb.d.ts.map +1 -0
- package/dist/memory/short-term/indexeddb.js +64 -0
- package/dist/memory/short-term/indexeddb.js.map +1 -0
- package/dist/memory/short-term/schema.d.ts +6 -0
- package/dist/memory/short-term/schema.d.ts.map +1 -0
- package/dist/memory/short-term/schema.js +119 -0
- package/dist/memory/short-term/schema.js.map +1 -0
- package/dist/memory/short-term/sqlite.d.ts +50 -0
- package/dist/memory/short-term/sqlite.d.ts.map +1 -0
- package/dist/memory/short-term/sqlite.js +221 -0
- package/dist/memory/short-term/sqlite.js.map +1 -0
- package/dist/memory/speculative-cache.d.ts +111 -0
- package/dist/memory/speculative-cache.d.ts.map +1 -0
- package/dist/memory/speculative-cache.js +409 -0
- package/dist/memory/speculative-cache.js.map +1 -0
- package/dist/memory/task-classifier.d.ts +34 -0
- package/dist/memory/task-classifier.d.ts.map +1 -0
- package/dist/memory/task-classifier.js +300 -0
- package/dist/memory/task-classifier.js.map +1 -0
- package/dist/memory/terminal-bench-knowledge.d.ts +48 -0
- package/dist/memory/terminal-bench-knowledge.d.ts.map +1 -0
- package/dist/memory/terminal-bench-knowledge.js +399 -0
- package/dist/memory/terminal-bench-knowledge.js.map +1 -0
- package/dist/memory/write-gate.d.ts +39 -0
- package/dist/memory/write-gate.d.ts.map +1 -0
- package/dist/memory/write-gate.js +190 -0
- package/dist/memory/write-gate.js.map +1 -0
- package/dist/models/executor.d.ts +130 -0
- package/dist/models/executor.d.ts.map +1 -0
- package/dist/models/executor.js +383 -0
- package/dist/models/executor.js.map +1 -0
- package/dist/models/index.d.ts +15 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +17 -0
- package/dist/models/index.js.map +1 -0
- package/dist/models/planner.d.ts +71 -0
- package/dist/models/planner.d.ts.map +1 -0
- package/dist/models/planner.js +344 -0
- package/dist/models/planner.js.map +1 -0
- package/dist/models/router.d.ts +75 -0
- package/dist/models/router.d.ts.map +1 -0
- package/dist/models/router.js +344 -0
- package/dist/models/router.js.map +1 -0
- package/dist/models/types.d.ts +370 -0
- package/dist/models/types.d.ts.map +1 -0
- package/dist/models/types.js +181 -0
- package/dist/models/types.js.map +1 -0
- package/dist/tasks/coordination.d.ts +74 -0
- package/dist/tasks/coordination.d.ts.map +1 -0
- package/dist/tasks/coordination.js +237 -0
- package/dist/tasks/coordination.js.map +1 -0
- package/dist/tasks/database.d.ts +14 -0
- package/dist/tasks/database.d.ts.map +1 -0
- package/dist/tasks/database.js +128 -0
- package/dist/tasks/database.js.map +1 -0
- package/dist/tasks/index.d.ts +5 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +5 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/tasks/service.d.ts +39 -0
- package/dist/tasks/service.d.ts.map +1 -0
- package/dist/tasks/service.js +582 -0
- package/dist/tasks/service.js.map +1 -0
- package/dist/tasks/types.d.ts +224 -0
- package/dist/tasks/types.d.ts.map +1 -0
- package/dist/tasks/types.js +64 -0
- package/dist/tasks/types.js.map +1 -0
- package/dist/types/analysis.d.ts +82 -0
- package/dist/types/analysis.d.ts.map +1 -0
- package/dist/types/analysis.js +2 -0
- package/dist/types/analysis.js.map +1 -0
- package/dist/types/config.d.ts +3023 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +292 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/coordination.d.ts +240 -0
- package/dist/types/coordination.d.ts.map +1 -0
- package/dist/types/coordination.js +43 -0
- package/dist/types/coordination.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +4 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/calculate-average.d.ts +15 -0
- package/dist/utils/calculate-average.d.ts.map +1 -0
- package/dist/utils/calculate-average.js +21 -0
- package/dist/utils/calculate-average.js.map +1 -0
- package/dist/utils/config-manager.d.ts +30 -0
- package/dist/utils/config-manager.d.ts.map +1 -0
- package/dist/utils/config-manager.js +41 -0
- package/dist/utils/config-manager.js.map +1 -0
- package/dist/utils/dijkstra.d.ts +17 -0
- package/dist/utils/dijkstra.d.ts.map +1 -0
- package/dist/utils/dijkstra.js +91 -0
- package/dist/utils/dijkstra.js.map +1 -0
- package/dist/utils/fetch-with-retry.d.ts +5 -0
- package/dist/utils/fetch-with-retry.d.ts.map +1 -0
- package/dist/utils/fetch-with-retry.js +61 -0
- package/dist/utils/fetch-with-retry.js.map +1 -0
- package/dist/utils/merge-claude-md.d.ts +28 -0
- package/dist/utils/merge-claude-md.d.ts.map +1 -0
- package/dist/utils/merge-claude-md.js +342 -0
- package/dist/utils/merge-claude-md.js.map +1 -0
- package/dist/utils/order-processor-refactored.d.ts +126 -0
- package/dist/utils/order-processor-refactored.d.ts.map +1 -0
- package/dist/utils/order-processor-refactored.js +165 -0
- package/dist/utils/order-processor-refactored.js.map +1 -0
- package/dist/utils/order-processor-strategy.d.ts +72 -0
- package/dist/utils/order-processor-strategy.d.ts.map +1 -0
- package/dist/utils/order-processor-strategy.js +158 -0
- package/dist/utils/order-processor-strategy.js.map +1 -0
- package/dist/utils/order-processor.d.ts +242 -0
- package/dist/utils/order-processor.d.ts.map +1 -0
- package/dist/utils/order-processor.js +370 -0
- package/dist/utils/order-processor.js.map +1 -0
- package/dist/utils/rate-limiter-simple.d.ts +58 -0
- package/dist/utils/rate-limiter-simple.d.ts.map +1 -0
- package/dist/utils/rate-limiter-simple.js +100 -0
- package/dist/utils/rate-limiter-simple.js.map +1 -0
- package/dist/utils/rate-limiter.d.ts +62 -0
- package/dist/utils/rate-limiter.d.ts.map +1 -0
- package/dist/utils/rate-limiter.js +150 -0
- package/dist/utils/rate-limiter.js.map +1 -0
- package/dist/utils/string-similarity.d.ts +37 -0
- package/dist/utils/string-similarity.d.ts.map +1 -0
- package/dist/utils/string-similarity.js +114 -0
- package/dist/utils/string-similarity.js.map +1 -0
- package/dist/utils/validate-json.d.ts +51 -0
- package/dist/utils/validate-json.d.ts.map +1 -0
- package/dist/utils/validate-json.js +99 -0
- package/dist/utils/validate-json.js.map +1 -0
- package/package.json +96 -0
- package/templates/CLAUDE.template.md +11 -0
- package/templates/CLAUDE_ARCHITECTURE.template.md +103 -0
- package/templates/CLAUDE_CODING.template.md +125 -0
- package/templates/CLAUDE_DROIDS.template.md +109 -0
- package/templates/CLAUDE_MEMORY.template.md +130 -0
- package/templates/CLAUDE_WORKFLOWS.template.md +136 -0
- package/templates/PROJECT.template.md +209 -0
- package/templates/SCHEMA.md +57 -0
- package/templates/archive/CLAUDE.template.root-v6.md +762 -0
- package/templates/archive/CLAUDE.template.v6.md +762 -0
- package/templates/hooks/pre-compact.sh +68 -0
- package/templates/hooks/session-start.sh +106 -0
- package/tools/agents/README.md +224 -0
- package/tools/agents/UAP/README.md +351 -0
- package/tools/agents/UAP/__init__.py +9 -0
- package/tools/agents/UAP/cli.py +675 -0
- package/tools/agents/UAP/version.py +2 -0
- package/tools/agents/benchmarks/benchmark_memory_systems.py +637 -0
- package/tools/agents/benchmarks/results/benchmark_20260106_064817.json +170 -0
- package/tools/agents/benchmarks/results/benchmark_20260106_064817.md +51 -0
- package/tools/agents/config/chat_template.jinja +172 -0
- package/tools/agents/docker-compose.qdrant.yml +24 -0
- package/tools/agents/migrations/apply.py +256 -0
- package/tools/agents/scripts/fix_qwen_chat_template.py +314 -0
- package/tools/agents/scripts/init_qdrant.py +151 -0
- package/tools/agents/scripts/memory_migration.py +518 -0
- package/tools/agents/scripts/migrate_memory_to_qdrant.py +113 -0
- package/tools/agents/scripts/query_memory.py +189 -0
- package/tools/agents/scripts/qwen_tool_call_test.py +419 -0
- package/tools/agents/scripts/qwen_tool_call_wrapper.py +517 -0
- package/tools/agents/scripts/start-services.sh +96 -0
- package/tools/agents/tests/test_uap_compliance.py +257 -0
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dynamic Memory Retrieval System for UAP
|
|
3
|
+
*
|
|
4
|
+
* Retrieves relevant memories based on task content, not static context.
|
|
5
|
+
* Implements semantic search with fallback to keyword matching.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Adaptive retrieval depth based on query complexity
|
|
9
|
+
* - Context budget management to prevent overflow
|
|
10
|
+
* - Speculative prefetch for common patterns
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readFileSync } from 'fs';
|
|
13
|
+
import { join } from 'path';
|
|
14
|
+
import Database from 'better-sqlite3';
|
|
15
|
+
import { classifyTask, extractTaskEntities, getSuggestedMemoryQueries } from './task-classifier.js';
|
|
16
|
+
import { ContextBudget } from './context-compressor.js';
|
|
17
|
+
import { compressToSemanticUnits } from './semantic-compression.js';
|
|
18
|
+
import { decideContextLevel, recordOutcome } from './adaptive-context.js';
|
|
19
|
+
import { getRelevantKnowledge, recordKnowledgeOutcome } from './terminal-bench-knowledge.js';
|
|
20
|
+
import { contentHash, jaccardSimilarity } from '../utils/string-similarity.js';
|
|
21
|
+
const DEFAULT_RETRIEVAL_DEPTHS = {
|
|
22
|
+
simple: { shortTerm: 3, sessionMem: 2, longTerm: 5, patterns: 3 },
|
|
23
|
+
moderate: { shortTerm: 6, sessionMem: 5, longTerm: 8, patterns: 5 },
|
|
24
|
+
complex: { shortTerm: 10, sessionMem: 8, longTerm: 15, patterns: 8 },
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Measure query complexity to determine retrieval depth
|
|
28
|
+
* Based on SimpleMem's adaptive query-aware retrieval
|
|
29
|
+
*/
|
|
30
|
+
export function measureQueryComplexity(query) {
|
|
31
|
+
let score = 0;
|
|
32
|
+
// Length-based scoring (lower thresholds)
|
|
33
|
+
const wordCount = query.split(/\s+/).length;
|
|
34
|
+
if (wordCount > 30)
|
|
35
|
+
score += 1.5;
|
|
36
|
+
else if (wordCount > 12)
|
|
37
|
+
score += 0.75;
|
|
38
|
+
else if (wordCount > 6)
|
|
39
|
+
score += 0.25;
|
|
40
|
+
// Technical terms increase complexity
|
|
41
|
+
const techPatterns = [
|
|
42
|
+
/debug|fix|error|exception|bug/i,
|
|
43
|
+
/implement|refactor|optimize|build/i,
|
|
44
|
+
/configure|setup|install|deploy/i,
|
|
45
|
+
/security|vulnerability|cve|auth/i,
|
|
46
|
+
/performance|memory|cpu|latency/i,
|
|
47
|
+
/database|query|migration|schema/i,
|
|
48
|
+
/test|coverage|mock|spec/i,
|
|
49
|
+
];
|
|
50
|
+
for (const pattern of techPatterns) {
|
|
51
|
+
if (pattern.test(query))
|
|
52
|
+
score += 0.4;
|
|
53
|
+
}
|
|
54
|
+
// Multiple entities/files increase complexity
|
|
55
|
+
const fileMatches = query.match(/[\w./\\-]+\.(ts|js|py|json|yaml|sh|sql)/gi);
|
|
56
|
+
if (fileMatches) {
|
|
57
|
+
score += fileMatches.length * 0.3;
|
|
58
|
+
}
|
|
59
|
+
// Multi-step tasks are complex
|
|
60
|
+
if (/and then|after that|followed by|step \d|first.*then|also|additionally/i.test(query)) {
|
|
61
|
+
score += 1;
|
|
62
|
+
}
|
|
63
|
+
// Questions about "why" or "how" are moderate
|
|
64
|
+
if (/^(why|how|what caused|explain)/i.test(query)) {
|
|
65
|
+
score += 0.5;
|
|
66
|
+
}
|
|
67
|
+
// Multiple actions in one query
|
|
68
|
+
const actionWords = query.match(/\b(fix|implement|configure|debug|create|update|delete|add|remove)\b/gi);
|
|
69
|
+
if (actionWords && actionWords.length > 1) {
|
|
70
|
+
score += actionWords.length * 0.3;
|
|
71
|
+
}
|
|
72
|
+
if (score >= 2)
|
|
73
|
+
return 'complex';
|
|
74
|
+
if (score >= 1)
|
|
75
|
+
return 'moderate';
|
|
76
|
+
return 'simple';
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get retrieval limits based on query complexity
|
|
80
|
+
*/
|
|
81
|
+
export function getRetrievalDepth(complexity, config = DEFAULT_RETRIEVAL_DEPTHS) {
|
|
82
|
+
return config[complexity];
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Main function to retrieve task-specific memory context
|
|
86
|
+
* Now with adaptive retrieval depth and context budget management
|
|
87
|
+
*/
|
|
88
|
+
export async function retrieveDynamicMemoryContext(taskInstruction, projectRoot = process.cwd(), options = {}) {
|
|
89
|
+
const { maxTokens = 2000, useSemanticCompression = true, taskMetadata } = options;
|
|
90
|
+
// Step 0: Adaptive context decision - skip UAP if not beneficial
|
|
91
|
+
const contextDecision = decideContextLevel(taskInstruction, taskMetadata);
|
|
92
|
+
if (contextDecision.level === 'none') {
|
|
93
|
+
const classification = classifyTask(taskInstruction);
|
|
94
|
+
return {
|
|
95
|
+
classification,
|
|
96
|
+
relevantMemories: [],
|
|
97
|
+
patterns: [],
|
|
98
|
+
gotchas: [],
|
|
99
|
+
projectContext: '',
|
|
100
|
+
formattedContext: '',
|
|
101
|
+
queryComplexity: 'simple',
|
|
102
|
+
tokenBudget: { used: 0, remaining: maxTokens, total: maxTokens },
|
|
103
|
+
contextDecision,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
// Adjust maxTokens based on context decision
|
|
107
|
+
const effectiveMaxTokens = contextDecision.level === 'minimal'
|
|
108
|
+
? Math.min(maxTokens, 800)
|
|
109
|
+
: maxTokens;
|
|
110
|
+
// Step 1: Classify the task
|
|
111
|
+
const classification = classifyTask(taskInstruction);
|
|
112
|
+
// Step 2: Measure query complexity for adaptive retrieval
|
|
113
|
+
const queryComplexity = measureQueryComplexity(taskInstruction);
|
|
114
|
+
const retrievalDepth = getRetrievalDepth(queryComplexity);
|
|
115
|
+
// Step 3: Initialize context budget
|
|
116
|
+
const budget = new ContextBudget(effectiveMaxTokens);
|
|
117
|
+
// Step 4: Extract entities from task
|
|
118
|
+
const entities = extractTaskEntities(taskInstruction);
|
|
119
|
+
// Step 5: Get suggested memory queries
|
|
120
|
+
const suggestedQueries = getSuggestedMemoryQueries(classification);
|
|
121
|
+
// Step 6: Query all memory sources with adaptive limits
|
|
122
|
+
// OPTIMIZATION 8: Pass effectiveMaxTokens to coordinate budgets
|
|
123
|
+
const memories = await queryAllMemorySources(taskInstruction, classification, entities, suggestedQueries, projectRoot, retrievalDepth, effectiveMaxTokens);
|
|
124
|
+
// Step 7: Apply semantic compression if enabled and we have many memories
|
|
125
|
+
let compressionStats;
|
|
126
|
+
let processedMemories = memories;
|
|
127
|
+
if (useSemanticCompression && memories.length > 5) {
|
|
128
|
+
const memoryData = memories.map(m => ({
|
|
129
|
+
content: m.content,
|
|
130
|
+
type: m.type,
|
|
131
|
+
importance: Math.round(m.relevance * 10),
|
|
132
|
+
}));
|
|
133
|
+
const compressed = compressToSemanticUnits(memoryData);
|
|
134
|
+
compressionStats = {
|
|
135
|
+
ratio: compressed.overallRatio,
|
|
136
|
+
sourceTokens: compressed.totalSourceTokens,
|
|
137
|
+
compressedTokens: compressed.totalCompressedTokens,
|
|
138
|
+
};
|
|
139
|
+
// Replace memories with compressed versions if significant savings
|
|
140
|
+
if (compressed.overallRatio > 1.5) {
|
|
141
|
+
processedMemories = compressed.units.flatMap(unit => unit.atomicFacts.map(fact => ({
|
|
142
|
+
content: fact.content,
|
|
143
|
+
type: fact.type === 'gotcha' ? 'gotcha' :
|
|
144
|
+
fact.type === 'lesson' ? 'lesson' :
|
|
145
|
+
fact.type === 'pattern' ? 'pattern' : 'context',
|
|
146
|
+
relevance: fact.actionability,
|
|
147
|
+
source: 'semantic-compression',
|
|
148
|
+
})));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Step 8: Extract patterns and gotchas
|
|
152
|
+
const patterns = processedMemories
|
|
153
|
+
.filter(m => m.type === 'pattern')
|
|
154
|
+
.map(m => m.content)
|
|
155
|
+
.slice(0, retrievalDepth.patterns);
|
|
156
|
+
const gotchas = processedMemories
|
|
157
|
+
.filter(m => m.type === 'gotcha')
|
|
158
|
+
.map(m => m.content)
|
|
159
|
+
.slice(0, retrievalDepth.patterns);
|
|
160
|
+
// Step 9: Get project-specific context
|
|
161
|
+
const projectContext = await getProjectContext(classification, projectRoot);
|
|
162
|
+
// Step 10: Format context with budget allocation
|
|
163
|
+
const { content: formattedContext } = budget.allocate('main', formatContextWithRecencyBias(classification, processedMemories, patterns, gotchas, projectContext));
|
|
164
|
+
return {
|
|
165
|
+
classification,
|
|
166
|
+
relevantMemories: processedMemories,
|
|
167
|
+
patterns,
|
|
168
|
+
gotchas,
|
|
169
|
+
projectContext,
|
|
170
|
+
formattedContext,
|
|
171
|
+
queryComplexity,
|
|
172
|
+
tokenBudget: {
|
|
173
|
+
used: budget.usage().used,
|
|
174
|
+
remaining: budget.remaining(),
|
|
175
|
+
total: effectiveMaxTokens,
|
|
176
|
+
},
|
|
177
|
+
compressionStats,
|
|
178
|
+
contextDecision,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Query all memory sources for relevant information
|
|
183
|
+
* Uses adaptive retrieval depth to limit queries based on complexity
|
|
184
|
+
*
|
|
185
|
+
* OPTIMIZATION 8: Removed internal TOKEN_BUDGET hardcap - let the outer
|
|
186
|
+
* ContextBudget manager handle truncation to avoid double-counting.
|
|
187
|
+
* Pass maxTokens through from caller for proper budget coordination.
|
|
188
|
+
*/
|
|
189
|
+
async function queryAllMemorySources(taskInstruction, classification, entities, suggestedQueries, projectRoot, depth, maxTokens) {
|
|
190
|
+
const memories = [];
|
|
191
|
+
// Source 1: Short-term SQLite memory (limited by depth)
|
|
192
|
+
const shortTermMemories = await queryShortTermMemory(classification, entities, projectRoot, depth.shortTerm);
|
|
193
|
+
memories.push(...shortTermMemories);
|
|
194
|
+
// Source 2: Session memories (limited by depth)
|
|
195
|
+
const sessionMemories = await querySessionMemory(taskInstruction, projectRoot, depth.sessionMem);
|
|
196
|
+
memories.push(...sessionMemories);
|
|
197
|
+
// Source 3: Long-term prepopulated memory (limited by depth)
|
|
198
|
+
const longTermMemories = await queryLongTermMemory(suggestedQueries, projectRoot, depth.longTerm);
|
|
199
|
+
memories.push(...longTermMemories);
|
|
200
|
+
// Source 4: CLAUDE.md sections relevant to task
|
|
201
|
+
const claudeMdMemories = await queryCLAUDEMd(classification, projectRoot);
|
|
202
|
+
memories.push(...claudeMdMemories);
|
|
203
|
+
// Source 5: Category-specific patterns from droids (limited by depth)
|
|
204
|
+
const droidPatterns = getCategoryPatterns(classification, depth.patterns);
|
|
205
|
+
memories.push(...droidPatterns);
|
|
206
|
+
// Source 6: Terminal-Bench domain knowledge (proven to improve accuracy)
|
|
207
|
+
// OPTIMIZATION 2: Also match file-creation type from domain knowledge
|
|
208
|
+
const domainKnowledge = getRelevantKnowledge(taskInstruction, classification.category);
|
|
209
|
+
for (const k of domainKnowledge) {
|
|
210
|
+
memories.push({
|
|
211
|
+
content: k.content,
|
|
212
|
+
type: k.type === 'gotcha' ? 'gotcha' : k.type === 'pattern' ? 'pattern' : 'context',
|
|
213
|
+
relevance: k.importance / 10,
|
|
214
|
+
source: 'terminal-bench-knowledge',
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
// Deduplicate and sort by relevance
|
|
218
|
+
const uniqueMemories = deduplicateMemories(memories);
|
|
219
|
+
uniqueMemories.sort((a, b) => b.relevance - a.relevance);
|
|
220
|
+
// OPTIMIZATION 8: Use maxTokens from caller (coordinated with outer ContextBudget)
|
|
221
|
+
// instead of hardcoded internal budget that caused double-counting
|
|
222
|
+
const effectiveBudget = maxTokens || 3000;
|
|
223
|
+
const budgeted = [];
|
|
224
|
+
let usedTokens = 0;
|
|
225
|
+
for (const mem of uniqueMemories) {
|
|
226
|
+
const memTokens = Math.ceil(mem.content.length / 4);
|
|
227
|
+
if (usedTokens + memTokens > effectiveBudget && budgeted.length > 0)
|
|
228
|
+
break;
|
|
229
|
+
budgeted.push(mem);
|
|
230
|
+
usedTokens += memTokens;
|
|
231
|
+
}
|
|
232
|
+
return budgeted;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Query short-term SQLite memory using parameterized queries (secure)
|
|
236
|
+
*/
|
|
237
|
+
async function queryShortTermMemory(classification, entities, projectRoot, limit = 5) {
|
|
238
|
+
const dbPath = join(projectRoot, 'agents/data/memory/short_term.db');
|
|
239
|
+
if (!existsSync(dbPath))
|
|
240
|
+
return [];
|
|
241
|
+
const memories = [];
|
|
242
|
+
const perKeywordLimit = Math.max(1, Math.ceil(limit / 3));
|
|
243
|
+
let db = null;
|
|
244
|
+
try {
|
|
245
|
+
db = new Database(dbPath, { readonly: true });
|
|
246
|
+
// Query by category keywords using parameterized queries (secure)
|
|
247
|
+
const keywordStmt = db.prepare(`
|
|
248
|
+
SELECT type, content FROM memories
|
|
249
|
+
WHERE content LIKE ?
|
|
250
|
+
ORDER BY id DESC
|
|
251
|
+
LIMIT ?
|
|
252
|
+
`);
|
|
253
|
+
for (const keyword of classification.keywords.slice(0, 3)) {
|
|
254
|
+
const rows = keywordStmt.all(`%${keyword}%`, perKeywordLimit);
|
|
255
|
+
for (const row of rows) {
|
|
256
|
+
if (row.content) {
|
|
257
|
+
memories.push({
|
|
258
|
+
content: row.content.slice(0, 500),
|
|
259
|
+
type: row.type === 'lesson' ? 'lesson' : 'context',
|
|
260
|
+
relevance: 0.7,
|
|
261
|
+
source: 'short-term-memory',
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// Query by technology mentions using parameterized queries (secure)
|
|
267
|
+
for (const tech of entities.technologies.slice(0, 2)) {
|
|
268
|
+
const rows = keywordStmt.all(`%${tech}%`, 2);
|
|
269
|
+
for (const row of rows) {
|
|
270
|
+
if (row.content) {
|
|
271
|
+
memories.push({
|
|
272
|
+
content: row.content.slice(0, 500),
|
|
273
|
+
type: row.type === 'gotcha' ? 'gotcha' : 'context',
|
|
274
|
+
relevance: 0.6,
|
|
275
|
+
source: 'short-term-memory',
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
// Ignore query errors
|
|
283
|
+
}
|
|
284
|
+
finally {
|
|
285
|
+
db?.close();
|
|
286
|
+
}
|
|
287
|
+
return memories;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Query session memories for recent decisions using parameterized queries (secure)
|
|
291
|
+
*/
|
|
292
|
+
async function querySessionMemory(_taskInstruction, projectRoot, limit = 5) {
|
|
293
|
+
const dbPath = join(projectRoot, 'agents/data/memory/short_term.db');
|
|
294
|
+
if (!existsSync(dbPath))
|
|
295
|
+
return [];
|
|
296
|
+
const memories = [];
|
|
297
|
+
let db = null;
|
|
298
|
+
try {
|
|
299
|
+
db = new Database(dbPath, { readonly: true });
|
|
300
|
+
const stmt = db.prepare(`
|
|
301
|
+
SELECT type, content FROM session_memories
|
|
302
|
+
WHERE importance >= 7
|
|
303
|
+
ORDER BY id DESC
|
|
304
|
+
LIMIT ?
|
|
305
|
+
`);
|
|
306
|
+
const rows = stmt.all(limit);
|
|
307
|
+
for (const row of rows) {
|
|
308
|
+
if (row.content) {
|
|
309
|
+
memories.push({
|
|
310
|
+
content: row.content.slice(0, 500),
|
|
311
|
+
type: row.type === 'lesson' ? 'lesson' : row.type === 'decision' ? 'context' : 'pattern',
|
|
312
|
+
relevance: 0.8,
|
|
313
|
+
source: 'session-memory',
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
// Ignore query errors
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
db?.close();
|
|
323
|
+
}
|
|
324
|
+
return memories;
|
|
325
|
+
}
|
|
326
|
+
// OPTIMIZATION 9: Stopwords to filter from long-term memory queries
|
|
327
|
+
// These words match nearly everything and reduce query precision
|
|
328
|
+
const QUERY_STOPWORDS = new Set([
|
|
329
|
+
'a', 'an', 'the', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
|
|
330
|
+
'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could',
|
|
331
|
+
'should', 'may', 'might', 'shall', 'can', 'need', 'must',
|
|
332
|
+
'to', 'of', 'in', 'for', 'on', 'with', 'at', 'by', 'from', 'as',
|
|
333
|
+
'into', 'through', 'during', 'before', 'after', 'above', 'below',
|
|
334
|
+
'and', 'but', 'or', 'nor', 'not', 'so', 'yet', 'both', 'either',
|
|
335
|
+
'this', 'that', 'these', 'those', 'it', 'its',
|
|
336
|
+
'best', 'most', 'very', 'good', 'great', 'well', 'new', 'more',
|
|
337
|
+
'common', 'general', 'basic', 'simple', 'all', 'any', 'some',
|
|
338
|
+
'how', 'what', 'when', 'where', 'which', 'who', 'why',
|
|
339
|
+
'practices', 'tips', 'implementation', 'gotchas', 'mistakes', 'patterns',
|
|
340
|
+
]);
|
|
341
|
+
/**
|
|
342
|
+
* Query long-term prepopulated memory
|
|
343
|
+
* OPTIMIZATION 9: Added stopword filtering for better query precision
|
|
344
|
+
*/
|
|
345
|
+
async function queryLongTermMemory(queries, projectRoot, _limit = 5) {
|
|
346
|
+
const memoryPath = join(projectRoot, 'agents/data/memory/long_term_prepopulated.json');
|
|
347
|
+
if (!existsSync(memoryPath))
|
|
348
|
+
return [];
|
|
349
|
+
const memories = [];
|
|
350
|
+
try {
|
|
351
|
+
const data = JSON.parse(readFileSync(memoryPath, 'utf-8'));
|
|
352
|
+
const allMemories = data.memories || data.lessons || data || [];
|
|
353
|
+
for (const query of queries.slice(0, 5)) {
|
|
354
|
+
if (memories.length >= _limit)
|
|
355
|
+
break;
|
|
356
|
+
const queryLower = query.toLowerCase();
|
|
357
|
+
// OPTIMIZATION 9: Filter out stopwords to improve match quality
|
|
358
|
+
const queryWords = queryLower.split(/\s+/).filter(w => w.length > 2 && !QUERY_STOPWORDS.has(w));
|
|
359
|
+
// Skip queries that are entirely stopwords
|
|
360
|
+
if (queryWords.length === 0)
|
|
361
|
+
continue;
|
|
362
|
+
for (const mem of allMemories) {
|
|
363
|
+
if (memories.length >= _limit)
|
|
364
|
+
break;
|
|
365
|
+
const content = (mem.content || mem.text || JSON.stringify(mem)).toLowerCase();
|
|
366
|
+
const matchCount = queryWords.filter(w => content.includes(w)).length;
|
|
367
|
+
// Require at least 1 non-stopword match (was 2 including stopwords)
|
|
368
|
+
if (matchCount >= 1 && matchCount / queryWords.length >= 0.3) {
|
|
369
|
+
memories.push({
|
|
370
|
+
content: (mem.content || mem.text || JSON.stringify(mem)).slice(0, 500),
|
|
371
|
+
type: mem.type || 'lesson',
|
|
372
|
+
relevance: matchCount / queryWords.length,
|
|
373
|
+
source: 'long-term-memory',
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
// Ignore parse errors
|
|
381
|
+
}
|
|
382
|
+
return memories;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Query CLAUDE.md for relevant sections
|
|
386
|
+
*/
|
|
387
|
+
async function queryCLAUDEMd(classification, projectRoot) {
|
|
388
|
+
const claudeMdPath = join(projectRoot, 'CLAUDE.md');
|
|
389
|
+
if (!existsSync(claudeMdPath))
|
|
390
|
+
return [];
|
|
391
|
+
const memories = [];
|
|
392
|
+
try {
|
|
393
|
+
const content = readFileSync(claudeMdPath, 'utf-8');
|
|
394
|
+
// Extract Code Field section (always relevant)
|
|
395
|
+
const codeFieldMatch = content.match(/## .*CODE FIELD.*?(?=\n## |\n---\n|$)/s);
|
|
396
|
+
if (codeFieldMatch) {
|
|
397
|
+
memories.push({
|
|
398
|
+
content: codeFieldMatch[0].slice(0, 800),
|
|
399
|
+
type: 'pattern',
|
|
400
|
+
relevance: 0.9,
|
|
401
|
+
source: 'CLAUDE.md',
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
// Extract category-specific sections
|
|
405
|
+
const categorySectionMap = {
|
|
406
|
+
'sysadmin': [/## .*System|Admin|Linux|Network.*?(?=\n## |\n---\n|$)/si],
|
|
407
|
+
'security': [/## .*Security|Auth.*?(?=\n## |\n---\n|$)/si],
|
|
408
|
+
'testing': [/## .*Test.*?(?=\n## |\n---\n|$)/si],
|
|
409
|
+
'coding': [/## .*Coding|Convention|Pattern.*?(?=\n## |\n---\n|$)/si],
|
|
410
|
+
};
|
|
411
|
+
const patterns = categorySectionMap[classification.category] || [];
|
|
412
|
+
for (const pattern of patterns) {
|
|
413
|
+
const match = content.match(pattern);
|
|
414
|
+
if (match) {
|
|
415
|
+
memories.push({
|
|
416
|
+
content: match[0].slice(0, 600),
|
|
417
|
+
type: 'context',
|
|
418
|
+
relevance: 0.75,
|
|
419
|
+
source: 'CLAUDE.md',
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
// Extract gotchas/troubleshooting sections
|
|
424
|
+
const gotchasMatch = content.match(/## .*Troubleshoot|Gotcha|Common.*?(?=\n## |\n---\n|$)/si);
|
|
425
|
+
if (gotchasMatch) {
|
|
426
|
+
memories.push({
|
|
427
|
+
content: gotchasMatch[0].slice(0, 500),
|
|
428
|
+
type: 'gotcha',
|
|
429
|
+
relevance: 0.7,
|
|
430
|
+
source: 'CLAUDE.md',
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
catch {
|
|
435
|
+
// Ignore read errors
|
|
436
|
+
}
|
|
437
|
+
return memories;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Get category-specific patterns from droid knowledge
|
|
441
|
+
*/
|
|
442
|
+
function getCategoryPatterns(classification, limit = 4) {
|
|
443
|
+
const patterns = [];
|
|
444
|
+
const categoryPatterns = {
|
|
445
|
+
'sysadmin': [
|
|
446
|
+
'Use `ip addr` instead of deprecated `ifconfig` for network info',
|
|
447
|
+
'Use `ss -tlnp` instead of `netstat` for listening ports',
|
|
448
|
+
'Always check `journalctl -u <service>` for service logs',
|
|
449
|
+
'Use `make -j$(nproc)` for parallel kernel compilation',
|
|
450
|
+
],
|
|
451
|
+
'security': [
|
|
452
|
+
'Never log sensitive data (passwords, tokens, keys)',
|
|
453
|
+
'Use parameterized queries to prevent SQL injection',
|
|
454
|
+
'Validate and sanitize all user input',
|
|
455
|
+
'Check for CVE exploits before attempting complex attacks',
|
|
456
|
+
],
|
|
457
|
+
'ml-training': [
|
|
458
|
+
'Start with smaller models (distilbert vs bert-large) for speed',
|
|
459
|
+
'Use `CUDA_VISIBLE_DEVICES` to select specific GPUs',
|
|
460
|
+
'Cache datasets to avoid repeated downloads',
|
|
461
|
+
'Set `num_train_epochs=3` initially, increase if needed',
|
|
462
|
+
],
|
|
463
|
+
'debugging': [
|
|
464
|
+
'Use `pip check` to detect dependency conflicts',
|
|
465
|
+
'Use `git reflog` to recover lost commits',
|
|
466
|
+
'Check `conda env export` before modifying environments',
|
|
467
|
+
'Add verbose flags (-v, --debug) to diagnose issues',
|
|
468
|
+
],
|
|
469
|
+
'coding': [
|
|
470
|
+
'State assumptions before writing code',
|
|
471
|
+
'Handle edge cases explicitly (empty arrays, null values)',
|
|
472
|
+
'Use TypeScript strict mode for better type safety',
|
|
473
|
+
'Include try-catch for operations that can fail',
|
|
474
|
+
],
|
|
475
|
+
'testing': [
|
|
476
|
+
'Test edge cases: empty input, null, undefined',
|
|
477
|
+
'Use mocks for external dependencies',
|
|
478
|
+
'Aim for high coverage on critical paths',
|
|
479
|
+
'Run tests before committing: `npm test`',
|
|
480
|
+
],
|
|
481
|
+
};
|
|
482
|
+
const relevantPatterns = categoryPatterns[classification.category] || [];
|
|
483
|
+
const patternLimit = Math.max(1, Math.ceil(limit * 0.6));
|
|
484
|
+
for (const pattern of relevantPatterns.slice(0, patternLimit)) {
|
|
485
|
+
patterns.push({
|
|
486
|
+
content: pattern,
|
|
487
|
+
type: 'pattern',
|
|
488
|
+
relevance: 0.85,
|
|
489
|
+
source: 'droid-knowledge',
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
// Add common gotchas
|
|
493
|
+
const commonGotchas = [
|
|
494
|
+
'Array index: use `i < length`, not `i <= length`',
|
|
495
|
+
'JSON.parse throws on invalid input - wrap in try/catch',
|
|
496
|
+
'Empty array reduce needs initial value',
|
|
497
|
+
'Map.get() returns undefined for missing keys',
|
|
498
|
+
];
|
|
499
|
+
const gotchaLimit = Math.max(1, limit - patternLimit);
|
|
500
|
+
for (const gotcha of commonGotchas.slice(0, gotchaLimit)) {
|
|
501
|
+
patterns.push({
|
|
502
|
+
content: gotcha,
|
|
503
|
+
type: 'gotcha',
|
|
504
|
+
relevance: 0.8,
|
|
505
|
+
source: 'droid-knowledge',
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return patterns;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Get project-specific context
|
|
512
|
+
*/
|
|
513
|
+
async function getProjectContext(classification, projectRoot) {
|
|
514
|
+
const sections = [];
|
|
515
|
+
// Add project structure if relevant
|
|
516
|
+
if (['coding', 'testing', 'debugging'].includes(classification.category)) {
|
|
517
|
+
try {
|
|
518
|
+
const pkgJsonPath = join(projectRoot, 'package.json');
|
|
519
|
+
if (existsSync(pkgJsonPath)) {
|
|
520
|
+
const pkg = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
|
|
521
|
+
sections.push(`Project: ${pkg.name} v${pkg.version}`);
|
|
522
|
+
if (pkg.scripts) {
|
|
523
|
+
const scripts = Object.keys(pkg.scripts).slice(0, 5).join(', ');
|
|
524
|
+
sections.push(`Available scripts: ${scripts}`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
catch {
|
|
529
|
+
// Ignore
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return sections.join('\n');
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Format context with recency bias (critical info at END)
|
|
536
|
+
* Based on Droid's hierarchical prompting strategy
|
|
537
|
+
*/
|
|
538
|
+
function formatContextWithRecencyBias(classification, memories, patterns, gotchas, projectContext) {
|
|
539
|
+
const sections = [];
|
|
540
|
+
// Section 1: Project context (less critical, at start)
|
|
541
|
+
if (projectContext) {
|
|
542
|
+
sections.push('## Project Context\n' + projectContext);
|
|
543
|
+
}
|
|
544
|
+
// Section 2: General patterns (medium priority)
|
|
545
|
+
if (patterns.length > 0) {
|
|
546
|
+
sections.push('## Relevant Patterns\n' + patterns.slice(0, 5).map(p => `- ${p}`).join('\n'));
|
|
547
|
+
}
|
|
548
|
+
// Section 3: Retrieved memories
|
|
549
|
+
const lessons = memories.filter(m => m.type === 'lesson').slice(0, 3);
|
|
550
|
+
if (lessons.length > 0) {
|
|
551
|
+
sections.push('## Lessons from Memory\n' + lessons.map(m => `- ${m.content}`).join('\n'));
|
|
552
|
+
}
|
|
553
|
+
// Section 4: Task classification info
|
|
554
|
+
sections.push(`## Task Classification
|
|
555
|
+
- Category: ${classification.category}
|
|
556
|
+
- Suggested approach: Use ${classification.suggestedDroid} patterns
|
|
557
|
+
- Key focus: ${classification.keywords.slice(0, 3).join(', ')}`);
|
|
558
|
+
// Section 5: CRITICAL - Gotchas at END (recency bias)
|
|
559
|
+
if (gotchas.length > 0) {
|
|
560
|
+
sections.push('## ⚠️ CRITICAL: Avoid These Mistakes\n' + gotchas.slice(0, 4).map(g => `- ${g}`).join('\n'));
|
|
561
|
+
}
|
|
562
|
+
// Section 6: Final reminders (most recent = highest attention)
|
|
563
|
+
sections.push(`## Final Reminders
|
|
564
|
+
- State assumptions before coding
|
|
565
|
+
- Handle edge cases explicitly
|
|
566
|
+
- Verify solution before reporting success`);
|
|
567
|
+
return sections.join('\n\n') + '\n\n---\n\n';
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Deduplicate memories by content hash AND semantic similarity
|
|
571
|
+
* Uses SHA-256 based content hash for exact deduplication,
|
|
572
|
+
* then Jaccard similarity for near-duplicate detection
|
|
573
|
+
*/
|
|
574
|
+
function deduplicateMemories(memories) {
|
|
575
|
+
const seen = new Set();
|
|
576
|
+
const unique = [];
|
|
577
|
+
const SIMILARITY_THRESHOLD = 0.8;
|
|
578
|
+
for (const mem of memories) {
|
|
579
|
+
// Phase 1: Exact content hash deduplication
|
|
580
|
+
const key = contentHash(mem.content);
|
|
581
|
+
if (seen.has(key))
|
|
582
|
+
continue;
|
|
583
|
+
// Phase 2: Semantic similarity check against existing unique memories
|
|
584
|
+
const contentLower = mem.content.toLowerCase();
|
|
585
|
+
let isDuplicate = false;
|
|
586
|
+
for (const existing of unique) {
|
|
587
|
+
const similarity = jaccardSimilarity(contentLower, existing.content.toLowerCase());
|
|
588
|
+
if (similarity > SIMILARITY_THRESHOLD) {
|
|
589
|
+
isDuplicate = true;
|
|
590
|
+
// Keep the one with higher relevance
|
|
591
|
+
if (mem.relevance > existing.relevance) {
|
|
592
|
+
const idx = unique.indexOf(existing);
|
|
593
|
+
unique[idx] = mem;
|
|
594
|
+
}
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if (!isDuplicate) {
|
|
599
|
+
seen.add(key);
|
|
600
|
+
unique.push(mem);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
return unique;
|
|
604
|
+
}
|
|
605
|
+
export function recordTaskFeedback(outcome) {
|
|
606
|
+
const { instruction, success, durationMs, modelId, agentOutput, projectRoot } = outcome;
|
|
607
|
+
// 1. Classify the task for feedback routing
|
|
608
|
+
const classification = classifyTask(instruction);
|
|
609
|
+
const taskType = classification.category;
|
|
610
|
+
// 2. Update adaptive context historical data
|
|
611
|
+
recordOutcome(taskType, true, success, durationMs, modelId);
|
|
612
|
+
// 3. Record knowledge outcome to boost/demote domain knowledge
|
|
613
|
+
recordKnowledgeOutcome(instruction, success);
|
|
614
|
+
// 4. Extract new learnings from agent output (on success only)
|
|
615
|
+
if (success && agentOutput) {
|
|
616
|
+
const learnedPatterns = extractLearnedPatterns(agentOutput, taskType);
|
|
617
|
+
for (const pattern of learnedPatterns) {
|
|
618
|
+
const persistPath = projectRoot
|
|
619
|
+
? join(projectRoot, 'agents/data/memory/long_term_prepopulated.json')
|
|
620
|
+
: undefined;
|
|
621
|
+
recordKnowledgeOutcome(instruction, true, {
|
|
622
|
+
category: taskType,
|
|
623
|
+
type: pattern.type,
|
|
624
|
+
content: pattern.content,
|
|
625
|
+
keywords: pattern.keywords,
|
|
626
|
+
importance: 7,
|
|
627
|
+
}, persistPath);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Extract learned patterns from successful agent output
|
|
633
|
+
* Looks for commands that worked, solutions that passed, and techniques used
|
|
634
|
+
*/
|
|
635
|
+
function extractLearnedPatterns(agentOutput, taskType) {
|
|
636
|
+
const patterns = [];
|
|
637
|
+
// Extract successful commands (lines starting with $ or containing common tool names)
|
|
638
|
+
const commandMatches = agentOutput.match(/^\$\s+.+$/gm);
|
|
639
|
+
if (commandMatches) {
|
|
640
|
+
for (const cmd of commandMatches.slice(0, 3)) {
|
|
641
|
+
const cleanCmd = cmd.replace(/^\$\s+/, '').trim();
|
|
642
|
+
if (cleanCmd.length > 10 && cleanCmd.length < 200) {
|
|
643
|
+
const keywords = extractKeywordsFromCommand(cleanCmd);
|
|
644
|
+
patterns.push({
|
|
645
|
+
type: 'tool',
|
|
646
|
+
content: `Successful command for ${taskType}: ${cleanCmd}`,
|
|
647
|
+
keywords: [taskType, ...keywords],
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
// Extract "worked" or "fixed" phrases
|
|
653
|
+
const solutionMatches = agentOutput.match(/(?:fixed|solved|resolved|working|passed|succeeded)[^.]*\./gi);
|
|
654
|
+
if (solutionMatches) {
|
|
655
|
+
for (const solution of solutionMatches.slice(0, 2)) {
|
|
656
|
+
if (solution.length > 15 && solution.length < 300) {
|
|
657
|
+
patterns.push({
|
|
658
|
+
type: 'pattern',
|
|
659
|
+
content: solution.trim(),
|
|
660
|
+
keywords: [taskType],
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return patterns;
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Extract keywords from a command string
|
|
669
|
+
*/
|
|
670
|
+
function extractKeywordsFromCommand(cmd) {
|
|
671
|
+
const toolNames = ['hashcat', 'john', 'readelf', 'objdump', 'strings', 'sqlite3',
|
|
672
|
+
'make', 'gcc', 'python', 'pip', 'npm', 'git', 'docker', 'curl', 'wget',
|
|
673
|
+
'stockfish', 'bleach', '7z', 'tar', 'grep', 'awk', 'sed'];
|
|
674
|
+
const keywords = [];
|
|
675
|
+
const cmdLower = cmd.toLowerCase();
|
|
676
|
+
for (const tool of toolNames) {
|
|
677
|
+
if (cmdLower.includes(tool)) {
|
|
678
|
+
keywords.push(tool);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
// Extract flags (e.g., -m 11600)
|
|
682
|
+
const flags = cmd.match(/-\w+\s+\S+/g);
|
|
683
|
+
if (flags) {
|
|
684
|
+
keywords.push(...flags.slice(0, 3).map(f => f.trim()));
|
|
685
|
+
}
|
|
686
|
+
return keywords;
|
|
687
|
+
}
|
|
688
|
+
//# sourceMappingURL=dynamic-retrieval.js.map
|