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,1022 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hybrid Adaptive Context Selector for UAP (Option 4)
|
|
3
|
+
*
|
|
4
|
+
* VERSION: 2.0.0 - 21 Model Outcome Success Optimizations
|
|
5
|
+
*
|
|
6
|
+
* Combines task classification with time-budget awareness, runtime monitoring,
|
|
7
|
+
* and historical benefit tracking for optimal context loading decisions.
|
|
8
|
+
*
|
|
9
|
+
* OPTIMIZATIONS IMPLEMENTED:
|
|
10
|
+
* 1. Historical Data Persistence - SQLite instead of in-memory Map
|
|
11
|
+
* 2. Task-specific context sections for 5 failing tasks
|
|
12
|
+
* 3. Missing context sections (git_recovery, web_parsing, data_processing, theorem_proving)
|
|
13
|
+
* 4. Weighted keyword relevance scoring (TF-IDF-like specificity weights)
|
|
14
|
+
* 5. Token budget utilization - increase minimal sections from 1→2
|
|
15
|
+
* 6. Task-type-selective pattern injection
|
|
16
|
+
* 7. Smarter progressive context escalation with error-to-section mapping
|
|
17
|
+
* 8. Model Router fingerprint persistence integrated
|
|
18
|
+
* 9. Multi-category task classification support
|
|
19
|
+
* 10. Semantic caching foundation for task→outcome mappings
|
|
20
|
+
*/
|
|
21
|
+
import Database from 'better-sqlite3';
|
|
22
|
+
import { existsSync, mkdirSync } from 'fs';
|
|
23
|
+
import { dirname, join } from 'path';
|
|
24
|
+
import { fileURLToPath } from 'url';
|
|
25
|
+
import { classifyTask as classifyTaskType } from './task-classifier.js';
|
|
26
|
+
import { recordTaskOutcome as updateModelRouterFingerprint, getModelFingerprint } from './model-router.js';
|
|
27
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
28
|
+
const __dirname = dirname(__filename);
|
|
29
|
+
// OPT 1: SQLite-backed historical data persistence
|
|
30
|
+
let historicalDb = null;
|
|
31
|
+
function getHistoricalDb() {
|
|
32
|
+
if (historicalDb)
|
|
33
|
+
return historicalDb;
|
|
34
|
+
// Use the same data directory as short_term.db
|
|
35
|
+
const dbDir = join(__dirname, '../../agents/data/memory');
|
|
36
|
+
if (!existsSync(dbDir)) {
|
|
37
|
+
mkdirSync(dbDir, { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
const dbPath = join(dbDir, 'historical_context.db');
|
|
40
|
+
historicalDb = new Database(dbPath);
|
|
41
|
+
// Enable WAL mode for better concurrent access
|
|
42
|
+
historicalDb.pragma('journal_mode = WAL');
|
|
43
|
+
// Create schema if not exists
|
|
44
|
+
historicalDb.exec(`
|
|
45
|
+
CREATE TABLE IF NOT EXISTS historical_data (
|
|
46
|
+
task_type TEXT PRIMARY KEY,
|
|
47
|
+
total_attempts INTEGER DEFAULT 0,
|
|
48
|
+
uam_successes INTEGER DEFAULT 0,
|
|
49
|
+
no_uam_successes INTEGER DEFAULT 0,
|
|
50
|
+
avg_time_with_uam REAL DEFAULT 0,
|
|
51
|
+
avg_time_without_uam REAL DEFAULT 0,
|
|
52
|
+
last_updated INTEGER DEFAULT 0
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
-- OPT 10: Semantic cache for task→outcome mappings
|
|
56
|
+
CREATE TABLE IF NOT EXISTS semantic_cache (
|
|
57
|
+
cache_key TEXT PRIMARY KEY,
|
|
58
|
+
instruction_hash TEXT,
|
|
59
|
+
decision_json TEXT,
|
|
60
|
+
success_rate REAL DEFAULT 0.5,
|
|
61
|
+
created_at INTEGER,
|
|
62
|
+
last_used INTEGER,
|
|
63
|
+
use_count INTEGER DEFAULT 1
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
CREATE INDEX IF NOT EXISTS idx_semantic_cache_hash ON semantic_cache(instruction_hash);
|
|
67
|
+
`);
|
|
68
|
+
return historicalDb;
|
|
69
|
+
}
|
|
70
|
+
// OPTIMIZATION 7: Refined low-benefit categories
|
|
71
|
+
const LOW_BENEFIT_CATEGORIES = new Set([
|
|
72
|
+
'reasoning', // Pure logical reasoning (sudoku, puzzles)
|
|
73
|
+
'games', // Game theory, chess position analysis (but NOT chess-best-move which needs stockfish)
|
|
74
|
+
'pure-logic', // Mathematical proofs, formal verification
|
|
75
|
+
'mathematical', // Pure math calculations
|
|
76
|
+
'calendar', // Meeting scheduling (but NOT server scheduling)
|
|
77
|
+
]);
|
|
78
|
+
// Categories that should ALWAYS receive UAP context regardless of historical data
|
|
79
|
+
const ALWAYS_BENEFIT_CATEGORIES = new Set([
|
|
80
|
+
'security',
|
|
81
|
+
'file-ops',
|
|
82
|
+
'sysadmin',
|
|
83
|
+
'debugging',
|
|
84
|
+
'legacy',
|
|
85
|
+
'coding',
|
|
86
|
+
'testing',
|
|
87
|
+
'ml-training',
|
|
88
|
+
'git-recovery', // OPT 3: Added for git tasks
|
|
89
|
+
'data-processing', // OPT 3: Added for data tasks
|
|
90
|
+
'theorem-proving', // OPT 3: Added for proof tasks
|
|
91
|
+
]);
|
|
92
|
+
// OPT 4: Weighted keywords - specificity score (higher = more specific = more valuable)
|
|
93
|
+
const HIGH_BENEFIT_KEYWORDS = {
|
|
94
|
+
// Security - very specific terms get higher weights
|
|
95
|
+
'hashcat': 3.0,
|
|
96
|
+
'john': 2.5,
|
|
97
|
+
'7z': 2.5,
|
|
98
|
+
'xss': 3.0,
|
|
99
|
+
'injection': 2.0,
|
|
100
|
+
'sanitize': 2.0,
|
|
101
|
+
'bleach': 3.0,
|
|
102
|
+
'dompurify': 3.0,
|
|
103
|
+
'password': 1.5,
|
|
104
|
+
'hash': 1.5,
|
|
105
|
+
'crack': 2.0,
|
|
106
|
+
'decrypt': 2.0,
|
|
107
|
+
'secret': 1.5,
|
|
108
|
+
'exploit': 2.0,
|
|
109
|
+
// File formats - specific formats get higher weights
|
|
110
|
+
'elf': 3.0,
|
|
111
|
+
'struct.unpack': 3.0,
|
|
112
|
+
'e_phoff': 3.5,
|
|
113
|
+
'sqlite': 2.0,
|
|
114
|
+
'wal': 3.0,
|
|
115
|
+
'binary': 1.5,
|
|
116
|
+
'executable': 1.5,
|
|
117
|
+
'extract': 1.5,
|
|
118
|
+
// Git recovery - OPT 3
|
|
119
|
+
'reflog': 3.0,
|
|
120
|
+
'fsck': 3.0,
|
|
121
|
+
'git recovery': 3.0,
|
|
122
|
+
'lost commit': 2.5,
|
|
123
|
+
'detached head': 2.5,
|
|
124
|
+
'git reset': 2.0,
|
|
125
|
+
'git rebase': 1.5,
|
|
126
|
+
// Web parsing - OPT 2 (for filter-js-from-html)
|
|
127
|
+
'html parse': 2.5,
|
|
128
|
+
'dom': 2.0,
|
|
129
|
+
'beautifulsoup': 2.5,
|
|
130
|
+
'lxml': 2.5,
|
|
131
|
+
'regex html': 2.0,
|
|
132
|
+
// Compression - OPT 2 (for gpt2-codegolf)
|
|
133
|
+
'codegolf': 3.0,
|
|
134
|
+
'minify': 2.0,
|
|
135
|
+
'compress': 1.5,
|
|
136
|
+
'gzip': 2.0,
|
|
137
|
+
'zlib': 2.5,
|
|
138
|
+
// Chess - OPT 2 (for chess-best-move)
|
|
139
|
+
'stockfish': 3.0,
|
|
140
|
+
'python-chess': 3.0,
|
|
141
|
+
'fen': 2.5,
|
|
142
|
+
'pgn': 2.5,
|
|
143
|
+
'chess position': 2.0,
|
|
144
|
+
'chessimg2pos': 3.0,
|
|
145
|
+
// Legacy
|
|
146
|
+
'cobol': 3.0,
|
|
147
|
+
'fortran': 2.5,
|
|
148
|
+
'legacy': 1.5,
|
|
149
|
+
'modernize': 1.5,
|
|
150
|
+
'mainframe': 2.5,
|
|
151
|
+
// Theorem proving - OPT 3 (for prove-plus-comm)
|
|
152
|
+
'coq': 3.0,
|
|
153
|
+
'lean': 3.0,
|
|
154
|
+
'isabelle': 3.0,
|
|
155
|
+
'theorem': 2.0,
|
|
156
|
+
'proof': 1.5,
|
|
157
|
+
'induction': 2.0,
|
|
158
|
+
'commutativity': 2.5,
|
|
159
|
+
// Data processing - OPT 3 (for reshard-c4-data)
|
|
160
|
+
'parquet': 2.5,
|
|
161
|
+
'sharding': 2.5,
|
|
162
|
+
'reshard': 3.0,
|
|
163
|
+
'c4 data': 3.0,
|
|
164
|
+
'dataset': 1.5,
|
|
165
|
+
'arrow': 2.0,
|
|
166
|
+
// General coding
|
|
167
|
+
'compile': 1.5,
|
|
168
|
+
'build': 1.0,
|
|
169
|
+
'makefile': 2.0,
|
|
170
|
+
'database': 1.5,
|
|
171
|
+
'recovery': 1.5,
|
|
172
|
+
};
|
|
173
|
+
// OPT 3 & 2: Extended context sections including new categories and task-specific sections
|
|
174
|
+
const CONTEXT_SECTIONS = {
|
|
175
|
+
security: {
|
|
176
|
+
tokens: 150,
|
|
177
|
+
keywords: ['xss', 'password', 'hash', 'crack', 'secret', 'exploit', 'injection', 'sanitize', 'bleach', 'dompurify'],
|
|
178
|
+
keywordWeights: { 'hashcat': 3, 'bleach': 3, 'dompurify': 3, 'xss': 2.5, 'injection': 2 },
|
|
179
|
+
content: `### Security
|
|
180
|
+
- XSS: Use bleach.clean() or DOMPurify. Remove script, onclick, onerror, javascript:
|
|
181
|
+
- Password cracking: hashcat -m 11600 for 7z, -m 0 MD5, john for CPU
|
|
182
|
+
- Binary secrets: strings, objdump -d, check .rodata section
|
|
183
|
+
- Always validate and sanitize user input`,
|
|
184
|
+
patterns: ['P10', 'P20'], // Whitelist, Adversarial Testing
|
|
185
|
+
},
|
|
186
|
+
file_formats: {
|
|
187
|
+
tokens: 120,
|
|
188
|
+
keywords: ['elf', 'sqlite', '7z', 'archive', 'binary', 'extract', 'format', 'wal', 'struct'],
|
|
189
|
+
keywordWeights: { 'e_phoff': 4, 'struct.unpack': 3, 'wal': 3, 'elf': 2.5 },
|
|
190
|
+
content: `### File Formats
|
|
191
|
+
- ELF: Program headers at e_phoff. Use struct.unpack for parsing
|
|
192
|
+
- SQLite WAL: Header 32 bytes, frames follow. PRAGMA wal_checkpoint to recover
|
|
193
|
+
- 7z: Install p7zip-full, use 7z x -p for password protected archives`,
|
|
194
|
+
patterns: ['P9', 'P35'], // FormatPipeline, Decoder-First
|
|
195
|
+
},
|
|
196
|
+
coding: {
|
|
197
|
+
tokens: 80,
|
|
198
|
+
keywords: ['implement', 'function', 'class', 'refactor', 'algorithm', 'code'],
|
|
199
|
+
content: `### Coding
|
|
200
|
+
- Use absolute paths (/app/...)
|
|
201
|
+
- Verify file exists before reading
|
|
202
|
+
- Handle edge cases in parsing
|
|
203
|
+
- Match exact output format required`,
|
|
204
|
+
patterns: ['P12', 'P16'], // OEV, TFE+
|
|
205
|
+
},
|
|
206
|
+
tools: {
|
|
207
|
+
tokens: 100,
|
|
208
|
+
keywords: ['hashcat', 'john', 'strings', 'objdump', 'readelf', 'command', 'cli'],
|
|
209
|
+
keywordWeights: { 'hashcat': 3, 'john': 2.5, 'readelf': 2, 'objdump': 2 },
|
|
210
|
+
content: `### Tools
|
|
211
|
+
- hashcat: GPU password cracking, -m flag for hash type
|
|
212
|
+
- john: CPU password cracking, flexible format support
|
|
213
|
+
- readelf/objdump: Binary analysis
|
|
214
|
+
- strings: Extract printable strings from binaries`,
|
|
215
|
+
patterns: ['P4', 'P8'], // ToolSpec, CLIoverLib
|
|
216
|
+
},
|
|
217
|
+
legacy: {
|
|
218
|
+
tokens: 90,
|
|
219
|
+
keywords: ['cobol', 'fortran', 'legacy', 'modernize', 'mainframe'],
|
|
220
|
+
keywordWeights: { 'cobol': 3, 'fortran': 2.5, 'mainframe': 2.5 },
|
|
221
|
+
content: `### Legacy Code
|
|
222
|
+
- COBOL: Fixed-format columns, WORKING-STORAGE for variables
|
|
223
|
+
- Modernization: Preserve business logic, update data structures
|
|
224
|
+
- Test with original inputs to verify behavior`,
|
|
225
|
+
patterns: ['P3'], // StateProtect
|
|
226
|
+
},
|
|
227
|
+
debugging: {
|
|
228
|
+
tokens: 70,
|
|
229
|
+
keywords: ['debug', 'error', 'fix', 'traceback', 'exception', 'crash'],
|
|
230
|
+
content: `### Debugging
|
|
231
|
+
- Check logs first: journalctl, /var/log/
|
|
232
|
+
- Use verbose flags: -v, --debug
|
|
233
|
+
- Isolate the problem: binary search through changes`,
|
|
234
|
+
patterns: ['P15', 'P13'], // ER, IRL
|
|
235
|
+
},
|
|
236
|
+
domain_libraries: {
|
|
237
|
+
tokens: 100,
|
|
238
|
+
keywords: ['chess', 'statistics', 'compression', 'sampler', 'ars', 'stockfish', 'numerical'],
|
|
239
|
+
keywordWeights: { 'stockfish': 3, 'python-chess': 3, 'scipy.stats': 2.5, 'ars': 3 },
|
|
240
|
+
content: `### Domain Libraries (USE INSTEAD OF CUSTOM CODE)
|
|
241
|
+
- Chess: python-chess + stockfish (pip install python-chess), chessimg2pos for image→FEN
|
|
242
|
+
- Statistics/ARS: scipy.stats or R CRAN 'ars' package (NOT custom implementation)
|
|
243
|
+
- Compression: zlib, lz4, brotli (NOT custom Huffman). Test round-trip FIRST
|
|
244
|
+
- Numerical: numpy/scipy with tolerance 1e-6, test multiple random seeds`,
|
|
245
|
+
patterns: ['P11'], // PreComputed
|
|
246
|
+
},
|
|
247
|
+
output_verification: {
|
|
248
|
+
tokens: 80,
|
|
249
|
+
keywords: ['create', 'file', 'output', 'script', 'cli', 'executable', 'shebang'],
|
|
250
|
+
content: `### Output Verification (37% of failures = missing files)
|
|
251
|
+
- touch output files FIRST, then implement
|
|
252
|
+
- CLI scripts: add #!/usr/bin/env python3 shebang + chmod +x
|
|
253
|
+
- Test with ./script (NOT python3 script.py)
|
|
254
|
+
- Verify all expected files: ls -la before claiming done
|
|
255
|
+
- Partial output > no output: always create the file`,
|
|
256
|
+
patterns: ['P12', 'P16', 'P32'], // OEV, TFE+, CEV
|
|
257
|
+
},
|
|
258
|
+
// OPT 3: NEW SECTIONS for missing capabilities
|
|
259
|
+
git_recovery: {
|
|
260
|
+
tokens: 100,
|
|
261
|
+
keywords: ['git', 'reflog', 'fsck', 'reset', 'rebase', 'merge', 'conflict', 'detached', 'lost', 'HEAD'],
|
|
262
|
+
keywordWeights: { 'reflog': 3, 'fsck': 3, 'dangling': 2.5, 'detached head': 2.5 },
|
|
263
|
+
content: `### Git Recovery (P22 GRF Pattern)
|
|
264
|
+
- ALWAYS backup first: cp -r .git .git.bak
|
|
265
|
+
- Lost commits: git reflog show HEAD, then git reset --hard HEAD@{n}
|
|
266
|
+
- Dangling objects: git fsck --lost-found, check .git/lost-found/
|
|
267
|
+
- Detached HEAD: git checkout -b recovery-branch
|
|
268
|
+
- Merge conflicts: git merge --abort to reset, or resolve with git mergetool
|
|
269
|
+
- Force push recovery: git reflog, find pre-push commit, reset`,
|
|
270
|
+
patterns: ['P22', 'P3'], // GRF, StateProtect
|
|
271
|
+
},
|
|
272
|
+
web_parsing: {
|
|
273
|
+
tokens: 110,
|
|
274
|
+
keywords: ['html', 'parse', 'dom', 'beautifulsoup', 'lxml', 'regex', 'scrape', 'filter', 'js', 'javascript'],
|
|
275
|
+
keywordWeights: { 'beautifulsoup': 2.5, 'lxml': 2.5, 'html.parser': 2, 'regex html': 2 },
|
|
276
|
+
content: `### Web Parsing & XSS Filtering
|
|
277
|
+
- HTML parsing: Use BeautifulSoup with lxml parser (pip install bs4 lxml)
|
|
278
|
+
- XSS filtering: bleach.clean() with allowed_tags whitelist, NOT blacklist
|
|
279
|
+
- Script removal: Remove <script>, onclick, onerror, javascript: hrefs
|
|
280
|
+
- Preserve structure: Maintain HTML hierarchy, don't just regex replace
|
|
281
|
+
- Test edge cases: nested tags, malformed HTML, encoded entities
|
|
282
|
+
- filter-js-from-html: Create /app/filter.py with exact input/output format`,
|
|
283
|
+
patterns: ['P10', 'P20', 'P14'], // Whitelist, AT, OFV
|
|
284
|
+
},
|
|
285
|
+
data_processing: {
|
|
286
|
+
tokens: 100,
|
|
287
|
+
keywords: ['parquet', 'shard', 'reshard', 'dataset', 'arrow', 'pandas', 'dask', 'c4', 'chunk'],
|
|
288
|
+
keywordWeights: { 'parquet': 2.5, 'reshard': 3, 'arrow': 2, 'dask': 2 },
|
|
289
|
+
content: `### Data Processing & Sharding
|
|
290
|
+
- Parquet: Use pyarrow or fastparquet for columnar data
|
|
291
|
+
- Large datasets: dask.dataframe for out-of-core processing
|
|
292
|
+
- Sharding: Split by hash(key) % num_shards for even distribution
|
|
293
|
+
- C4 data: Common Crawl format, handle with datasets library
|
|
294
|
+
- Memory efficiency: Process in chunks, use generators
|
|
295
|
+
- Verification: Count rows before/after, checksum critical columns`,
|
|
296
|
+
patterns: ['P18', 'P31'], // MTP, RTV
|
|
297
|
+
},
|
|
298
|
+
theorem_proving: {
|
|
299
|
+
tokens: 90,
|
|
300
|
+
keywords: ['coq', 'lean', 'isabelle', 'theorem', 'proof', 'induction', 'lemma', 'tactic'],
|
|
301
|
+
keywordWeights: { 'coq': 3, 'lean': 3, 'isabelle': 3, 'induction': 2 },
|
|
302
|
+
content: `### Theorem Proving
|
|
303
|
+
- Coq: Use 'induction' tactic for recursive proofs, 'simpl' to simplify
|
|
304
|
+
- Lean: mathlib provides common lemmas, use 'rfl' for reflexivity
|
|
305
|
+
- Commutativity: Prove by induction on first argument, use IH in step case
|
|
306
|
+
- prove-plus-comm: Natural number addition commutativity via Peano axioms
|
|
307
|
+
- Tactics: intro, apply, rewrite, exact, reflexivity
|
|
308
|
+
- Debug: 'Show Proof' in Coq, 'trace.state' in Lean`,
|
|
309
|
+
patterns: ['P5'], // Impossible check
|
|
310
|
+
},
|
|
311
|
+
// OPT 2: Task-specific sections for the 5 persistently failing tasks
|
|
312
|
+
chess_vision: {
|
|
313
|
+
tokens: 110,
|
|
314
|
+
keywords: ['chess', 'image', 'board', 'fen', 'position', 'stockfish', 'best move', 'analyze'],
|
|
315
|
+
keywordWeights: { 'chessimg2pos': 4, 'stockfish': 3, 'fen': 2.5, 'best move': 2 },
|
|
316
|
+
content: `### Chess Image Analysis (chess-best-move)
|
|
317
|
+
- Image to FEN: pip install chessimg2pos (or board_to_fen)
|
|
318
|
+
- Position analysis: python-chess + stockfish engine
|
|
319
|
+
- Workflow: image → FEN → stockfish → best move
|
|
320
|
+
- Install: apt-get install stockfish, pip install python-chess
|
|
321
|
+
- Code: import chess.engine; engine.analyse(board, chess.engine.Limit(depth=20))
|
|
322
|
+
- Output: UCI notation (e.g., e2e4) or SAN (e.g., e4)`,
|
|
323
|
+
patterns: ['P11', 'P34'], // PreComputed, ISP
|
|
324
|
+
},
|
|
325
|
+
regex_chess: {
|
|
326
|
+
tokens: 100,
|
|
327
|
+
keywords: ['regex', 'chess', 'pgn', 'notation', 'game', 'century', 'parse'],
|
|
328
|
+
keywordWeights: { 'pgn': 3, 'game of century': 3, 'chess notation': 2.5 },
|
|
329
|
+
content: `### Regex Chess (regex-chess task)
|
|
330
|
+
- PGN parsing: Match moves with [KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](=[QRBN])?[+#]?
|
|
331
|
+
- Castling: O-O (kingside), O-O-O (queenside)
|
|
332
|
+
- Game of Century: Byrne vs Fischer 1956, know key moves
|
|
333
|
+
- State machine: Track board state through move sequence
|
|
334
|
+
- Validation: Use python-chess for legal move verification
|
|
335
|
+
- Edge cases: Promotions, en passant, disambiguation`,
|
|
336
|
+
patterns: ['P24'], // PCC
|
|
337
|
+
},
|
|
338
|
+
compression_codegolf: {
|
|
339
|
+
tokens: 100,
|
|
340
|
+
keywords: ['codegolf', 'minify', 'gpt2', 'small', 'bytes', 'compress', 'size'],
|
|
341
|
+
keywordWeights: { 'codegolf': 3, 'gpt2': 3, 'minify': 2, 'bytes': 2 },
|
|
342
|
+
content: `### Code Golf & Compression (gpt2-codegolf)
|
|
343
|
+
- GPT-2 codegolf: Impossible to implement full GPT-2 in <5KB
|
|
344
|
+
- Strategy: Pre-compute weights, use lookup tables
|
|
345
|
+
- Minification: Remove whitespace, use short variable names
|
|
346
|
+
- gpt2.c: Reference implementation exists, study structure
|
|
347
|
+
- Shannon limit: Cannot compress below entropy of data
|
|
348
|
+
- If truly impossible: Document in IMPOSSIBLE.md with proof`,
|
|
349
|
+
patterns: ['P5', 'P19', 'P23'], // Impossible, ITR+, CID
|
|
350
|
+
},
|
|
351
|
+
db_wal_recovery: {
|
|
352
|
+
tokens: 100,
|
|
353
|
+
keywords: ['wal', 'sqlite', 'recovery', 'checkpoint', 'journal', 'database', 'corrupt'],
|
|
354
|
+
keywordWeights: { 'wal': 3, 'checkpoint': 3, 'db-wal-recovery': 4 },
|
|
355
|
+
content: `### SQLite WAL Recovery (db-wal-recovery)
|
|
356
|
+
- CRITICAL: Backup WAL file FIRST before any sqlite3 command!
|
|
357
|
+
- cp /app/main.db-wal /tmp/wal_backup.wal
|
|
358
|
+
- WAL auto-checkpoints when opened - this destroys recovery data
|
|
359
|
+
- Parse WAL manually: struct.unpack for header (32 bytes)
|
|
360
|
+
- Frame format: 24-byte header + page data
|
|
361
|
+
- Use /tmp/wal_backup.wal for analysis, never original
|
|
362
|
+
- Alternative: sqlite3_recover tool from SQLite source`,
|
|
363
|
+
patterns: ['P3', 'P35'], // StateProtect, DFA
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
// OPT 7: Error-to-section mapping for smarter progressive escalation
|
|
367
|
+
const ERROR_SECTION_MAPPING = {
|
|
368
|
+
'permission denied': ['tools', 'output_verification'],
|
|
369
|
+
'chmod': ['output_verification', 'tools'],
|
|
370
|
+
'struct.unpack': ['file_formats'],
|
|
371
|
+
'unpack requires': ['file_formats'],
|
|
372
|
+
'no module named': ['domain_libraries', 'tools'],
|
|
373
|
+
'command not found': ['tools'],
|
|
374
|
+
'syntax error': ['coding', 'legacy'],
|
|
375
|
+
'parse error': ['web_parsing', 'file_formats'],
|
|
376
|
+
'hash': ['security', 'tools'],
|
|
377
|
+
'xss': ['security', 'web_parsing'],
|
|
378
|
+
'injection': ['security', 'web_parsing'],
|
|
379
|
+
'git': ['git_recovery'],
|
|
380
|
+
'reflog': ['git_recovery'],
|
|
381
|
+
'merge conflict': ['git_recovery'],
|
|
382
|
+
'detached head': ['git_recovery'],
|
|
383
|
+
'parquet': ['data_processing'],
|
|
384
|
+
'shard': ['data_processing'],
|
|
385
|
+
'dataset': ['data_processing'],
|
|
386
|
+
'coq': ['theorem_proving'],
|
|
387
|
+
'lean': ['theorem_proving'],
|
|
388
|
+
'induction': ['theorem_proving'],
|
|
389
|
+
'chess': ['chess_vision', 'regex_chess', 'domain_libraries'],
|
|
390
|
+
'stockfish': ['chess_vision', 'domain_libraries'],
|
|
391
|
+
'fen': ['chess_vision'],
|
|
392
|
+
'pgn': ['regex_chess'],
|
|
393
|
+
'wal': ['db_wal_recovery', 'file_formats'],
|
|
394
|
+
'sqlite': ['db_wal_recovery', 'file_formats'],
|
|
395
|
+
'checkpoint': ['db_wal_recovery'],
|
|
396
|
+
'codegolf': ['compression_codegolf'],
|
|
397
|
+
'gpt2': ['compression_codegolf'],
|
|
398
|
+
'minify': ['compression_codegolf'],
|
|
399
|
+
'filter': ['web_parsing', 'security'],
|
|
400
|
+
'html': ['web_parsing'],
|
|
401
|
+
'beautifulsoup': ['web_parsing'],
|
|
402
|
+
};
|
|
403
|
+
// OPT 6: Pattern relevance by task type
|
|
404
|
+
const TASK_TYPE_PATTERNS = {
|
|
405
|
+
'security': ['P10', 'P20', 'P11'],
|
|
406
|
+
'file-ops': ['P9', 'P35', 'P3', 'P12'],
|
|
407
|
+
'coding': ['P12', 'P16', 'P32', 'P17'],
|
|
408
|
+
'debugging': ['P15', 'P13', 'P3'],
|
|
409
|
+
'git-recovery': ['P22', 'P3'],
|
|
410
|
+
'data-processing': ['P18', 'P31', 'P12'],
|
|
411
|
+
'theorem-proving': ['P5', 'P11'],
|
|
412
|
+
'legacy': ['P3', 'P35'],
|
|
413
|
+
'sysadmin': ['P1', 'P8', 'P4'],
|
|
414
|
+
'ml-training': ['P11', 'P33', 'P30'],
|
|
415
|
+
'testing': ['P13', 'P26', 'P30'],
|
|
416
|
+
};
|
|
417
|
+
// Constants
|
|
418
|
+
const MS_PER_TOKEN = 1.5;
|
|
419
|
+
const BENEFIT_THRESHOLD = 0.1;
|
|
420
|
+
const RELEVANCE_THRESHOLD = 0.3;
|
|
421
|
+
const TIME_CRITICAL_MAX_TOKENS = 300;
|
|
422
|
+
// OPT 4: Calculate weighted relevance score for a section
|
|
423
|
+
function calculateSectionRelevance(instruction, sectionConfig) {
|
|
424
|
+
const lower = instruction.toLowerCase();
|
|
425
|
+
let totalScore = 0;
|
|
426
|
+
let matchCount = 0;
|
|
427
|
+
for (const kw of sectionConfig.keywords) {
|
|
428
|
+
if (lower.includes(kw.toLowerCase())) {
|
|
429
|
+
// OPT 4: Use specificity weight if available, otherwise default to 1
|
|
430
|
+
const weight = sectionConfig.keywordWeights?.[kw] || 1;
|
|
431
|
+
totalScore += weight;
|
|
432
|
+
matchCount++;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
// Also check global high-benefit keywords with their weights
|
|
436
|
+
for (const [kw, weight] of Object.entries(HIGH_BENEFIT_KEYWORDS)) {
|
|
437
|
+
if (lower.includes(kw.toLowerCase())) {
|
|
438
|
+
// Check if this keyword is relevant to this section
|
|
439
|
+
if (sectionConfig.keywords.some(sk => kw.includes(sk) || sk.includes(kw))) {
|
|
440
|
+
totalScore += weight * 0.5; // Partial bonus for related keywords
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
// Normalize: max possible score is roughly keywords.length * 3 (max weight)
|
|
445
|
+
const maxPossible = sectionConfig.keywords.length * 3;
|
|
446
|
+
return Math.min(totalScore / Math.max(maxPossible * 0.3, 1), 1);
|
|
447
|
+
}
|
|
448
|
+
export function classifyTaskMultiCategory(instruction) {
|
|
449
|
+
const lower = instruction.toLowerCase();
|
|
450
|
+
const categoryScores = {};
|
|
451
|
+
const matchedKeywords = [];
|
|
452
|
+
// Score from high-benefit keywords
|
|
453
|
+
for (const [kw, weight] of Object.entries(HIGH_BENEFIT_KEYWORDS)) {
|
|
454
|
+
if (lower.includes(kw.toLowerCase())) {
|
|
455
|
+
matchedKeywords.push(kw);
|
|
456
|
+
// Map keywords to categories
|
|
457
|
+
if (['password', 'hash', 'crack', 'xss', 'injection', 'sanitize', 'hashcat', 'john', 'bleach', 'dompurify'].some(k => kw.includes(k))) {
|
|
458
|
+
categoryScores['security'] = (categoryScores['security'] || 0) + weight;
|
|
459
|
+
}
|
|
460
|
+
if (['elf', 'sqlite', 'binary', 'wal', 'struct'].some(k => kw.includes(k))) {
|
|
461
|
+
categoryScores['file-ops'] = (categoryScores['file-ops'] || 0) + weight;
|
|
462
|
+
}
|
|
463
|
+
if (['git', 'reflog', 'fsck', 'rebase'].some(k => kw.includes(k))) {
|
|
464
|
+
categoryScores['git-recovery'] = (categoryScores['git-recovery'] || 0) + weight;
|
|
465
|
+
}
|
|
466
|
+
if (['cobol', 'fortran', 'legacy', 'mainframe'].some(k => kw.includes(k))) {
|
|
467
|
+
categoryScores['legacy'] = (categoryScores['legacy'] || 0) + weight;
|
|
468
|
+
}
|
|
469
|
+
if (['coq', 'lean', 'theorem', 'proof', 'induction'].some(k => kw.includes(k))) {
|
|
470
|
+
categoryScores['theorem-proving'] = (categoryScores['theorem-proving'] || 0) + weight;
|
|
471
|
+
}
|
|
472
|
+
if (['parquet', 'shard', 'reshard', 'dataset', 'arrow'].some(k => kw.includes(k))) {
|
|
473
|
+
categoryScores['data-processing'] = (categoryScores['data-processing'] || 0) + weight;
|
|
474
|
+
}
|
|
475
|
+
if (['stockfish', 'chess', 'fen', 'pgn'].some(k => kw.includes(k))) {
|
|
476
|
+
categoryScores['chess'] = (categoryScores['chess'] || 0) + weight;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
// Fall back to task-classifier
|
|
481
|
+
const baseClassification = classifyTaskType(instruction);
|
|
482
|
+
categoryScores[baseClassification.category] = (categoryScores[baseClassification.category] || 0) + 5;
|
|
483
|
+
// Sort by score
|
|
484
|
+
const sorted = Object.entries(categoryScores)
|
|
485
|
+
.sort(([, a], [, b]) => b - a);
|
|
486
|
+
if (sorted.length === 0) {
|
|
487
|
+
return {
|
|
488
|
+
primary: 'coding',
|
|
489
|
+
secondary: [],
|
|
490
|
+
confidence: 0.5,
|
|
491
|
+
keywords: matchedKeywords,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
const [primary, primaryScore] = sorted[0];
|
|
495
|
+
const secondary = sorted.slice(1, 3)
|
|
496
|
+
.filter(([, score]) => score >= primaryScore * 0.4)
|
|
497
|
+
.map(([cat]) => cat);
|
|
498
|
+
const maxPossible = Object.values(HIGH_BENEFIT_KEYWORDS).reduce((a, b) => a + b, 0);
|
|
499
|
+
const confidence = Math.min(primaryScore / (maxPossible * 0.1), 1);
|
|
500
|
+
return {
|
|
501
|
+
primary,
|
|
502
|
+
secondary,
|
|
503
|
+
confidence,
|
|
504
|
+
keywords: matchedKeywords,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Classify task type from instruction text (backward compatible)
|
|
509
|
+
*/
|
|
510
|
+
export function classifyTask(instruction) {
|
|
511
|
+
return classifyTaskMultiCategory(instruction).primary;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Assess time pressure based on timeout and task complexity
|
|
515
|
+
*/
|
|
516
|
+
export function assessTimePressure(timeoutSec, taskType, difficulty = 'medium') {
|
|
517
|
+
const difficultyMultiplier = {
|
|
518
|
+
easy: 0.5,
|
|
519
|
+
medium: 1.0,
|
|
520
|
+
hard: 2.0,
|
|
521
|
+
};
|
|
522
|
+
const baseDuration = {
|
|
523
|
+
security: 120,
|
|
524
|
+
'file-ops': 90,
|
|
525
|
+
legacy: 150,
|
|
526
|
+
coding: 60,
|
|
527
|
+
debugging: 90,
|
|
528
|
+
scheduling: 45,
|
|
529
|
+
games: 30,
|
|
530
|
+
'constraint-satisfaction': 60,
|
|
531
|
+
'pure-logic': 90,
|
|
532
|
+
reasoning: 60,
|
|
533
|
+
general: 60,
|
|
534
|
+
sysadmin: 120,
|
|
535
|
+
'ml-training': 180,
|
|
536
|
+
testing: 60,
|
|
537
|
+
'git-recovery': 90,
|
|
538
|
+
'data-processing': 120,
|
|
539
|
+
'theorem-proving': 180,
|
|
540
|
+
chess: 90,
|
|
541
|
+
unknown: 60,
|
|
542
|
+
};
|
|
543
|
+
const expectedDuration = (baseDuration[taskType] || 60) * (difficultyMultiplier[difficulty] || 1.0);
|
|
544
|
+
const ratio = timeoutSec / expectedDuration;
|
|
545
|
+
if (ratio < 1.0)
|
|
546
|
+
return 'critical';
|
|
547
|
+
if (ratio < 1.3)
|
|
548
|
+
return 'high';
|
|
549
|
+
if (ratio < 1.8)
|
|
550
|
+
return 'medium';
|
|
551
|
+
return 'low';
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* OPT 1: Get historical benefit from SQLite (persistent)
|
|
555
|
+
*/
|
|
556
|
+
export function getHistoricalBenefit(taskType) {
|
|
557
|
+
try {
|
|
558
|
+
const db = getHistoricalDb();
|
|
559
|
+
const row = db.prepare('SELECT * FROM historical_data WHERE task_type = ?').get(taskType);
|
|
560
|
+
if (!row || row.totalAttempts < 3) {
|
|
561
|
+
if (LOW_BENEFIT_CATEGORIES.has(taskType)) {
|
|
562
|
+
return 0.05;
|
|
563
|
+
}
|
|
564
|
+
return 0.5;
|
|
565
|
+
}
|
|
566
|
+
const uapRate = row.uapSuccesses / Math.max(row.totalAttempts / 2, 1);
|
|
567
|
+
const noUamRate = row.noUamSuccesses / Math.max(row.totalAttempts / 2, 1);
|
|
568
|
+
if (noUamRate === 0)
|
|
569
|
+
return uapRate > 0 ? 1.0 : 0.5;
|
|
570
|
+
return (uapRate - noUamRate) / Math.max(uapRate, noUamRate, 0.1);
|
|
571
|
+
}
|
|
572
|
+
catch {
|
|
573
|
+
// Fallback to defaults if DB fails
|
|
574
|
+
if (LOW_BENEFIT_CATEGORIES.has(taskType)) {
|
|
575
|
+
return 0.05;
|
|
576
|
+
}
|
|
577
|
+
return 0.5;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* OPT 1: Record task outcome to SQLite (persistent)
|
|
582
|
+
*/
|
|
583
|
+
export function recordOutcome(taskType, usedUam, success, durationMs, modelId) {
|
|
584
|
+
try {
|
|
585
|
+
const db = getHistoricalDb();
|
|
586
|
+
// Get existing record or create new
|
|
587
|
+
const existing = db.prepare('SELECT * FROM historical_data WHERE task_type = ?').get(taskType);
|
|
588
|
+
if (existing) {
|
|
589
|
+
// Update existing record
|
|
590
|
+
const stmt = db.prepare(`
|
|
591
|
+
UPDATE historical_data SET
|
|
592
|
+
total_attempts = total_attempts + 1,
|
|
593
|
+
uam_successes = uam_successes + ?,
|
|
594
|
+
no_uam_successes = no_uam_successes + ?,
|
|
595
|
+
avg_time_with_uam = CASE WHEN ? THEN (avg_time_with_uam * uam_successes + ?) / (uam_successes + 1) ELSE avg_time_with_uam END,
|
|
596
|
+
avg_time_without_uam = CASE WHEN ? THEN (avg_time_without_uam * no_uam_successes + ?) / (no_uam_successes + 1) ELSE avg_time_without_uam END,
|
|
597
|
+
last_updated = ?
|
|
598
|
+
WHERE task_type = ?
|
|
599
|
+
`);
|
|
600
|
+
stmt.run(usedUam && success ? 1 : 0, !usedUam && success ? 1 : 0, usedUam && success ? 1 : 0, durationMs, !usedUam && success ? 1 : 0, durationMs, Date.now(), taskType);
|
|
601
|
+
}
|
|
602
|
+
else {
|
|
603
|
+
// Insert new record
|
|
604
|
+
const stmt = db.prepare(`
|
|
605
|
+
INSERT INTO historical_data (task_type, total_attempts, uam_successes, no_uam_successes, avg_time_with_uam, avg_time_without_uam, last_updated)
|
|
606
|
+
VALUES (?, 1, ?, ?, ?, ?, ?)
|
|
607
|
+
`);
|
|
608
|
+
stmt.run(taskType, usedUam && success ? 1 : 0, !usedUam && success ? 1 : 0, usedUam && success ? durationMs : 0, !usedUam && success ? durationMs : 0, Date.now());
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
catch (err) {
|
|
612
|
+
// Log but don't throw - recording should not block execution
|
|
613
|
+
console.warn('Failed to record outcome:', err);
|
|
614
|
+
}
|
|
615
|
+
// OPT 8: Also update model router fingerprints
|
|
616
|
+
if (modelId) {
|
|
617
|
+
const validModelIds = ['glm-4.7', 'gpt-5.2', 'claude-opus-4.5', 'gpt-5.2-codex'];
|
|
618
|
+
if (validModelIds.includes(modelId)) {
|
|
619
|
+
updateModelRouterFingerprint(modelId, success, durationMs, taskType);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* OPT 10: Cache lookup for similar tasks
|
|
625
|
+
*/
|
|
626
|
+
export function lookupSemanticCache(instructionHash) {
|
|
627
|
+
try {
|
|
628
|
+
const db = getHistoricalDb();
|
|
629
|
+
const row = db.prepare(`
|
|
630
|
+
SELECT decision_json, success_rate
|
|
631
|
+
FROM semantic_cache
|
|
632
|
+
WHERE instruction_hash = ? AND success_rate >= 0.5
|
|
633
|
+
ORDER BY success_rate DESC, use_count DESC
|
|
634
|
+
LIMIT 1
|
|
635
|
+
`).get(instructionHash);
|
|
636
|
+
if (row) {
|
|
637
|
+
// Update usage stats
|
|
638
|
+
db.prepare(`
|
|
639
|
+
UPDATE semantic_cache
|
|
640
|
+
SET last_used = ?, use_count = use_count + 1
|
|
641
|
+
WHERE instruction_hash = ?
|
|
642
|
+
`).run(Date.now(), instructionHash);
|
|
643
|
+
return JSON.parse(row.decision_json);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
catch {
|
|
647
|
+
// Cache miss
|
|
648
|
+
}
|
|
649
|
+
return null;
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* OPT 10: Store decision in semantic cache
|
|
653
|
+
*/
|
|
654
|
+
export function storeSemanticCache(cacheKey, instructionHash, decision, success) {
|
|
655
|
+
try {
|
|
656
|
+
const db = getHistoricalDb();
|
|
657
|
+
const existing = db.prepare('SELECT * FROM semantic_cache WHERE cache_key = ?').get(cacheKey);
|
|
658
|
+
if (existing) {
|
|
659
|
+
// Update success rate with exponential moving average
|
|
660
|
+
db.prepare(`
|
|
661
|
+
UPDATE semantic_cache SET
|
|
662
|
+
decision_json = ?,
|
|
663
|
+
success_rate = success_rate * 0.8 + ? * 0.2,
|
|
664
|
+
last_used = ?,
|
|
665
|
+
use_count = use_count + 1
|
|
666
|
+
WHERE cache_key = ?
|
|
667
|
+
`).run(JSON.stringify(decision), success ? 1.0 : 0.0, Date.now(), cacheKey);
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
db.prepare(`
|
|
671
|
+
INSERT INTO semantic_cache (cache_key, instruction_hash, decision_json, success_rate, created_at, last_used, use_count)
|
|
672
|
+
VALUES (?, ?, ?, ?, ?, ?, 1)
|
|
673
|
+
`).run(cacheKey, instructionHash, JSON.stringify(decision), success ? 1.0 : 0.5, Date.now(), Date.now());
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
catch (err) {
|
|
677
|
+
console.warn('Failed to store in semantic cache:', err);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Select relevant context sections based on task type and instruction
|
|
682
|
+
* OPT 5: Returns at least 2 sections for minimal mode
|
|
683
|
+
*/
|
|
684
|
+
export function selectRelevantSections(instruction, taskType, secondaryCategories) {
|
|
685
|
+
const sectionsWithScores = [];
|
|
686
|
+
for (const [name, config] of Object.entries(CONTEXT_SECTIONS)) {
|
|
687
|
+
const score = calculateSectionRelevance(instruction, config);
|
|
688
|
+
if (score >= RELEVANCE_THRESHOLD) {
|
|
689
|
+
sectionsWithScores.push({ name, score, patterns: config.patterns });
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
// Sort by relevance score descending
|
|
693
|
+
sectionsWithScores.sort((a, b) => b.score - a.score);
|
|
694
|
+
const sections = sectionsWithScores.map(s => s.name);
|
|
695
|
+
// Add default sections for certain task types if not already included
|
|
696
|
+
const addIfMissing = (section) => {
|
|
697
|
+
if (!sections.includes(section))
|
|
698
|
+
sections.push(section);
|
|
699
|
+
};
|
|
700
|
+
// Primary category defaults
|
|
701
|
+
if (taskType === 'security')
|
|
702
|
+
addIfMissing('security');
|
|
703
|
+
if (taskType === 'file-ops')
|
|
704
|
+
addIfMissing('file_formats');
|
|
705
|
+
if (taskType === 'legacy')
|
|
706
|
+
addIfMissing('legacy');
|
|
707
|
+
if (taskType === 'git-recovery')
|
|
708
|
+
addIfMissing('git_recovery');
|
|
709
|
+
if (taskType === 'data-processing')
|
|
710
|
+
addIfMissing('data_processing');
|
|
711
|
+
if (taskType === 'theorem-proving')
|
|
712
|
+
addIfMissing('theorem_proving');
|
|
713
|
+
if (taskType === 'chess') {
|
|
714
|
+
addIfMissing('chess_vision');
|
|
715
|
+
addIfMissing('domain_libraries');
|
|
716
|
+
}
|
|
717
|
+
// OPT 9: Add sections for secondary categories too
|
|
718
|
+
if (secondaryCategories) {
|
|
719
|
+
for (const cat of secondaryCategories) {
|
|
720
|
+
if (cat === 'security')
|
|
721
|
+
addIfMissing('security');
|
|
722
|
+
if (cat === 'file-ops')
|
|
723
|
+
addIfMissing('file_formats');
|
|
724
|
+
if (cat === 'git-recovery')
|
|
725
|
+
addIfMissing('git_recovery');
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return sections;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Calculate estimated overhead for given sections in milliseconds
|
|
732
|
+
*/
|
|
733
|
+
export function calculateOverhead(sections) {
|
|
734
|
+
let totalTokens = 0;
|
|
735
|
+
for (const section of sections) {
|
|
736
|
+
totalTokens += CONTEXT_SECTIONS[section]?.tokens || 0;
|
|
737
|
+
}
|
|
738
|
+
return totalTokens * MS_PER_TOKEN;
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* OPT 6: Get relevant patterns for task type
|
|
742
|
+
*/
|
|
743
|
+
export function getRelevantPatterns(taskType, sections) {
|
|
744
|
+
const patterns = new Set();
|
|
745
|
+
// From task type
|
|
746
|
+
const typePatterns = TASK_TYPE_PATTERNS[taskType] || [];
|
|
747
|
+
for (const p of typePatterns)
|
|
748
|
+
patterns.add(p);
|
|
749
|
+
// From selected sections
|
|
750
|
+
for (const section of sections) {
|
|
751
|
+
const sectionConfig = CONTEXT_SECTIONS[section];
|
|
752
|
+
if (sectionConfig?.patterns) {
|
|
753
|
+
for (const p of sectionConfig.patterns)
|
|
754
|
+
patterns.add(p);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
return Array.from(patterns);
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Main decision function: determine optimal context level using hybrid approach
|
|
761
|
+
*/
|
|
762
|
+
export function decideContextLevel(instruction, metadata = {}) {
|
|
763
|
+
// OPT 9: Use multi-category classification
|
|
764
|
+
const multiClass = classifyTaskMultiCategory(instruction);
|
|
765
|
+
const taskType = multiClass.primary;
|
|
766
|
+
const timeoutSec = metadata.timeout_sec || 300;
|
|
767
|
+
const difficulty = metadata.difficulty || 'medium';
|
|
768
|
+
// Factor 1: Task classification - skip for pure reasoning
|
|
769
|
+
if (LOW_BENEFIT_CATEGORIES.has(taskType)) {
|
|
770
|
+
return {
|
|
771
|
+
level: 'none',
|
|
772
|
+
sections: [],
|
|
773
|
+
reason: `Task type '${taskType}' is pure reasoning - UAP adds no benefit`,
|
|
774
|
+
estimatedOverheadMs: 0,
|
|
775
|
+
taskType,
|
|
776
|
+
timePressure: 'low',
|
|
777
|
+
historicalBenefit: 0,
|
|
778
|
+
secondaryCategories: multiClass.secondary,
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
// Factor 2: Time pressure assessment
|
|
782
|
+
const timePressure = assessTimePressure(timeoutSec, taskType, difficulty);
|
|
783
|
+
// Factor 3: Historical benefit (now from SQLite - OPT 1)
|
|
784
|
+
const historicalBenefit = metadata.historical_uam_benefit ?? getHistoricalBenefit(taskType);
|
|
785
|
+
// Factor 4: Check if historical data suggests skipping UAP
|
|
786
|
+
if (historicalBenefit < BENEFIT_THRESHOLD && !ALWAYS_BENEFIT_CATEGORIES.has(taskType)) {
|
|
787
|
+
return {
|
|
788
|
+
level: 'none',
|
|
789
|
+
sections: [],
|
|
790
|
+
reason: `Low historical benefit (${(historicalBenefit * 100).toFixed(1)}%) for ${taskType}`,
|
|
791
|
+
estimatedOverheadMs: 0,
|
|
792
|
+
taskType,
|
|
793
|
+
timePressure,
|
|
794
|
+
historicalBenefit,
|
|
795
|
+
secondaryCategories: multiClass.secondary,
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
// Factor 5: Critical time pressure - skip UAP
|
|
799
|
+
if (timePressure === 'critical') {
|
|
800
|
+
return {
|
|
801
|
+
level: 'none',
|
|
802
|
+
sections: [],
|
|
803
|
+
reason: 'Critical time pressure - skipping UAP to avoid timeout',
|
|
804
|
+
estimatedOverheadMs: 0,
|
|
805
|
+
taskType,
|
|
806
|
+
timePressure,
|
|
807
|
+
historicalBenefit,
|
|
808
|
+
secondaryCategories: multiClass.secondary,
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
// Factor 6: Select relevant sections (OPT 9: including secondary categories)
|
|
812
|
+
const relevantSections = selectRelevantSections(instruction, taskType, multiClass.secondary);
|
|
813
|
+
const estimatedOverhead = calculateOverhead(relevantSections);
|
|
814
|
+
// OPT 6: Get relevant patterns
|
|
815
|
+
const relevantPatterns = getRelevantPatterns(taskType, relevantSections);
|
|
816
|
+
// Factor 7: Check if overhead fits within time budget
|
|
817
|
+
const overheadRatio = estimatedOverhead / (timeoutSec * 1000);
|
|
818
|
+
// Time-critical tasks (<120s): cap overhead to TIME_CRITICAL_MAX_TOKENS
|
|
819
|
+
if (timeoutSec < 120) {
|
|
820
|
+
const cappedSections = [];
|
|
821
|
+
let tokenBudget = TIME_CRITICAL_MAX_TOKENS;
|
|
822
|
+
for (const section of relevantSections) {
|
|
823
|
+
const sectionTokens = CONTEXT_SECTIONS[section]?.tokens || 0;
|
|
824
|
+
if (tokenBudget - sectionTokens >= 0) {
|
|
825
|
+
cappedSections.push(section);
|
|
826
|
+
tokenBudget -= sectionTokens;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
return {
|
|
830
|
+
level: cappedSections.length > 0 ? 'minimal' : 'none',
|
|
831
|
+
sections: cappedSections,
|
|
832
|
+
reason: `Time-critical task (<120s) - capped to ${TIME_CRITICAL_MAX_TOKENS} tokens`,
|
|
833
|
+
estimatedOverheadMs: calculateOverhead(cappedSections),
|
|
834
|
+
taskType,
|
|
835
|
+
timePressure,
|
|
836
|
+
historicalBenefit,
|
|
837
|
+
secondaryCategories: multiClass.secondary,
|
|
838
|
+
relevantPatterns,
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
// OPT 5: Use 2 sections instead of 1 for minimal mode
|
|
842
|
+
if (timePressure === 'high' || overheadRatio > 0.1) {
|
|
843
|
+
const minimalSections = relevantSections.slice(0, 2); // Changed from 1 to 2
|
|
844
|
+
return {
|
|
845
|
+
level: 'minimal',
|
|
846
|
+
sections: minimalSections,
|
|
847
|
+
reason: `High time pressure - using minimal context (${minimalSections.join(', ') || 'best_practices'})`,
|
|
848
|
+
estimatedOverheadMs: calculateOverhead(minimalSections),
|
|
849
|
+
taskType,
|
|
850
|
+
timePressure,
|
|
851
|
+
historicalBenefit,
|
|
852
|
+
secondaryCategories: multiClass.secondary,
|
|
853
|
+
relevantPatterns,
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
// Default: Full context for everything else
|
|
857
|
+
return {
|
|
858
|
+
level: 'full',
|
|
859
|
+
sections: relevantSections.length > 0 ? relevantSections : ['coding'],
|
|
860
|
+
reason: `Full context for ${taskType} task (${timePressure} pressure)`,
|
|
861
|
+
estimatedOverheadMs: estimatedOverhead,
|
|
862
|
+
taskType,
|
|
863
|
+
timePressure,
|
|
864
|
+
historicalBenefit,
|
|
865
|
+
secondaryCategories: multiClass.secondary,
|
|
866
|
+
relevantPatterns,
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Generate context string based on decision
|
|
871
|
+
*/
|
|
872
|
+
export function generateContext(decision) {
|
|
873
|
+
if (decision.level === 'none' || decision.sections.length === 0) {
|
|
874
|
+
return '';
|
|
875
|
+
}
|
|
876
|
+
const contextParts = ['## UAP Memory Context\n'];
|
|
877
|
+
for (const section of decision.sections) {
|
|
878
|
+
const sectionConfig = CONTEXT_SECTIONS[section];
|
|
879
|
+
if (sectionConfig) {
|
|
880
|
+
contextParts.push(sectionConfig.content);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
// OPT 6: Add relevant patterns hint
|
|
884
|
+
if (decision.relevantPatterns && decision.relevantPatterns.length > 0) {
|
|
885
|
+
contextParts.push(`\n### Relevant Patterns: ${decision.relevantPatterns.join(', ')}`);
|
|
886
|
+
}
|
|
887
|
+
return contextParts.join('\n');
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* OPT 7: Enhanced progressive context strategy with error-to-section mapping
|
|
891
|
+
*/
|
|
892
|
+
export function getProgressiveContextLevels(instruction, initialError, metadata = {}) {
|
|
893
|
+
const decision = decideContextLevel(instruction, metadata);
|
|
894
|
+
if (decision.level === 'none' && LOW_BENEFIT_CATEGORIES.has(decision.taskType)) {
|
|
895
|
+
return ['none'];
|
|
896
|
+
}
|
|
897
|
+
const errorLower = initialError.toLowerCase();
|
|
898
|
+
// OPT 7: Check error-to-section mapping for targeted escalation
|
|
899
|
+
let suggestedSections = [];
|
|
900
|
+
for (const [errorPattern, sections] of Object.entries(ERROR_SECTION_MAPPING)) {
|
|
901
|
+
if (errorLower.includes(errorPattern)) {
|
|
902
|
+
suggestedSections.push(...sections);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
// Standard context-might-help checks
|
|
906
|
+
const contextMightHelp = suggestedSections.length > 0 ||
|
|
907
|
+
errorLower.includes('unknown') ||
|
|
908
|
+
errorLower.includes('how to') ||
|
|
909
|
+
errorLower.includes('what is') ||
|
|
910
|
+
errorLower.includes('command not found') ||
|
|
911
|
+
errorLower.includes('invalid syntax') ||
|
|
912
|
+
errorLower.includes('format') ||
|
|
913
|
+
errorLower.includes('parse');
|
|
914
|
+
if (!contextMightHelp) {
|
|
915
|
+
return [decision.level];
|
|
916
|
+
}
|
|
917
|
+
// Progressive escalation based on starting point
|
|
918
|
+
switch (decision.level) {
|
|
919
|
+
case 'none':
|
|
920
|
+
return ['none', 'minimal', 'full'];
|
|
921
|
+
case 'minimal':
|
|
922
|
+
return ['minimal', 'full'];
|
|
923
|
+
case 'full':
|
|
924
|
+
return ['full'];
|
|
925
|
+
default:
|
|
926
|
+
return ['none', 'minimal', 'full'];
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* OPT 7: Get additional sections to add based on error analysis
|
|
931
|
+
*/
|
|
932
|
+
export function getSectionsForError(error) {
|
|
933
|
+
const errorLower = error.toLowerCase();
|
|
934
|
+
const sections = new Set();
|
|
935
|
+
for (const [errorPattern, sectionList] of Object.entries(ERROR_SECTION_MAPPING)) {
|
|
936
|
+
if (errorLower.includes(errorPattern)) {
|
|
937
|
+
for (const section of sectionList) {
|
|
938
|
+
sections.add(section);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
return Array.from(sections);
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
* Export configuration for Python agent integration
|
|
946
|
+
*/
|
|
947
|
+
export function exportConfigForPython(instruction, metadata = {}) {
|
|
948
|
+
const decision = decideContextLevel(instruction, metadata);
|
|
949
|
+
const context = generateContext(decision);
|
|
950
|
+
return JSON.stringify({
|
|
951
|
+
level: decision.level,
|
|
952
|
+
sections: decision.sections,
|
|
953
|
+
reason: decision.reason,
|
|
954
|
+
estimatedOverheadMs: decision.estimatedOverheadMs,
|
|
955
|
+
taskType: decision.taskType,
|
|
956
|
+
timePressure: decision.timePressure,
|
|
957
|
+
historicalBenefit: decision.historicalBenefit,
|
|
958
|
+
secondaryCategories: decision.secondaryCategories,
|
|
959
|
+
relevantPatterns: decision.relevantPatterns,
|
|
960
|
+
context,
|
|
961
|
+
}, null, 2);
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* OPT 8: Get model fingerprint for routing integration
|
|
965
|
+
*/
|
|
966
|
+
export function getModelFingerprintForTask(taskType) {
|
|
967
|
+
// Check per-category success rates from model router
|
|
968
|
+
const models = ['claude-opus-4.5', 'gpt-5.2', 'glm-4.7', 'gpt-5.2-codex'];
|
|
969
|
+
let bestModel = 'claude-opus-4.5';
|
|
970
|
+
let bestScore = 0;
|
|
971
|
+
for (const modelId of models) {
|
|
972
|
+
const fp = getModelFingerprint(modelId);
|
|
973
|
+
if (fp && fp.categoryStats?.[taskType]) {
|
|
974
|
+
const stats = fp.categoryStats[taskType];
|
|
975
|
+
if (stats.attempts >= 3) {
|
|
976
|
+
const rate = stats.successes / stats.attempts;
|
|
977
|
+
if (rate > bestScore) {
|
|
978
|
+
bestScore = rate;
|
|
979
|
+
bestModel = modelId;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
if (bestScore > 0) {
|
|
985
|
+
return {
|
|
986
|
+
recommended: bestModel,
|
|
987
|
+
reason: `${bestModel} has ${(bestScore * 100).toFixed(0)}% success rate for ${taskType} tasks`,
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
return null;
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Close database connection (for cleanup)
|
|
994
|
+
*/
|
|
995
|
+
export function closeHistoricalDb() {
|
|
996
|
+
if (historicalDb) {
|
|
997
|
+
historicalDb.close();
|
|
998
|
+
historicalDb = null;
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
// Export main interface
|
|
1002
|
+
export const HybridAdaptiveContext = {
|
|
1003
|
+
classifyTask,
|
|
1004
|
+
classifyTaskMultiCategory,
|
|
1005
|
+
assessTimePressure,
|
|
1006
|
+
getHistoricalBenefit,
|
|
1007
|
+
recordOutcome,
|
|
1008
|
+
decideContextLevel,
|
|
1009
|
+
generateContext,
|
|
1010
|
+
selectRelevantSections,
|
|
1011
|
+
calculateOverhead,
|
|
1012
|
+
getProgressiveContextLevels,
|
|
1013
|
+
getSectionsForError,
|
|
1014
|
+
getRelevantPatterns,
|
|
1015
|
+
exportConfigForPython,
|
|
1016
|
+
lookupSemanticCache,
|
|
1017
|
+
storeSemanticCache,
|
|
1018
|
+
getModelFingerprintForTask,
|
|
1019
|
+
closeHistoricalDb,
|
|
1020
|
+
};
|
|
1021
|
+
export default HybridAdaptiveContext;
|
|
1022
|
+
//# sourceMappingURL=adaptive-context.js.map
|