agentdb 1.0.12 → 1.1.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/README.md +196 -838
- package/dist/benchmarks/comprehensive-benchmark.js +664 -0
- package/dist/benchmarks/frontier-benchmark.js +419 -0
- package/dist/benchmarks/reflexion-benchmark.js +370 -0
- package/dist/cli/agentdb-cli.d.ts +117 -0
- package/dist/cli/agentdb-cli.d.ts.map +1 -0
- package/dist/cli/agentdb-cli.js +718 -0
- package/dist/cli/agentdb-cli.js.map +1 -0
- package/dist/controllers/CausalMemoryGraph.d.ts +125 -0
- package/dist/controllers/CausalMemoryGraph.d.ts.map +1 -0
- package/dist/controllers/CausalMemoryGraph.js +323 -0
- package/dist/controllers/CausalMemoryGraph.js.map +1 -0
- package/dist/controllers/CausalRecall.d.ts +111 -0
- package/dist/controllers/CausalRecall.d.ts.map +1 -0
- package/dist/controllers/CausalRecall.js +282 -0
- package/dist/controllers/CausalRecall.js.map +1 -0
- package/dist/controllers/EmbeddingService.d.ts +37 -0
- package/dist/controllers/EmbeddingService.d.ts.map +1 -0
- package/dist/controllers/EmbeddingService.js +119 -0
- package/dist/controllers/EmbeddingService.js.map +1 -0
- package/dist/controllers/ExplainableRecall.d.ts +138 -0
- package/dist/controllers/ExplainableRecall.d.ts.map +1 -0
- package/dist/controllers/ExplainableRecall.js +388 -0
- package/dist/controllers/ExplainableRecall.js.map +1 -0
- package/dist/controllers/NightlyLearner.d.ts +113 -0
- package/dist/controllers/NightlyLearner.d.ts.map +1 -0
- package/dist/controllers/NightlyLearner.js +383 -0
- package/dist/controllers/NightlyLearner.js.map +1 -0
- package/dist/controllers/ReflexionMemory.d.ts +84 -0
- package/dist/controllers/ReflexionMemory.d.ts.map +1 -0
- package/dist/controllers/ReflexionMemory.js +240 -0
- package/dist/controllers/ReflexionMemory.js.map +1 -0
- package/dist/controllers/SkillLibrary.d.ts +96 -0
- package/dist/controllers/SkillLibrary.d.ts.map +1 -0
- package/dist/controllers/SkillLibrary.js +277 -0
- package/dist/controllers/SkillLibrary.js.map +1 -0
- package/dist/controllers/frontier-index.d.ts +14 -0
- package/dist/controllers/frontier-index.d.ts.map +1 -0
- package/dist/controllers/frontier-index.js +10 -0
- package/dist/controllers/frontier-index.js.map +1 -0
- package/dist/controllers/index.d.ts +12 -0
- package/dist/controllers/index.d.ts.map +1 -0
- package/dist/controllers/index.js +9 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/optimizations/BatchOperations.d.ts +61 -0
- package/dist/optimizations/BatchOperations.d.ts.map +1 -0
- package/dist/optimizations/BatchOperations.js +199 -0
- package/dist/optimizations/BatchOperations.js.map +1 -0
- package/dist/optimizations/QueryOptimizer.d.ts +82 -0
- package/dist/optimizations/QueryOptimizer.d.ts.map +1 -0
- package/dist/optimizations/QueryOptimizer.js +226 -0
- package/dist/optimizations/QueryOptimizer.js.map +1 -0
- package/dist/optimizations/index.d.ts +10 -0
- package/dist/optimizations/index.d.ts.map +1 -0
- package/dist/optimizations/index.js +8 -0
- package/dist/optimizations/index.js.map +1 -0
- package/dist/schemas/frontier-schema.sql +341 -0
- package/dist/schemas/schema.sql +382 -0
- package/dist/tests/frontier-features.test.js +665 -0
- package/package.json +54 -117
- package/src/cli/agentdb-cli.ts +861 -0
- package/src/cli/examples.sh +83 -0
- package/src/controllers/CausalMemoryGraph.ts +504 -0
- package/src/controllers/CausalRecall.ts +395 -0
- package/src/controllers/EmbeddingService.ts +141 -0
- package/src/controllers/ExplainableRecall.ts +577 -0
- package/src/controllers/NightlyLearner.ts +475 -0
- package/src/controllers/ReflexionMemory.ts +349 -0
- package/src/controllers/SkillLibrary.ts +391 -0
- package/src/controllers/frontier-index.ts +35 -0
- package/src/controllers/index.ts +13 -0
- package/src/optimizations/BatchOperations.ts +292 -0
- package/src/optimizations/QueryOptimizer.ts +294 -0
- package/src/optimizations/index.ts +11 -0
- package/src/schemas/frontier-schema.sql +341 -0
- package/src/schemas/schema.sql +382 -0
- package/CHANGELOG.md +0 -166
- package/LICENSE +0 -38
- package/LICENSE-APACHE +0 -190
- package/LICENSE-MIT +0 -21
- package/bin/agentdb.js +0 -733
- package/bin/benchmark.js +0 -165
- package/bin/plugin-cli-wrapper.mjs +0 -21
- package/dist/agentdb.js +0 -5352
- package/dist/agentdb.js.map +0 -7
- package/dist/agentdb.min.js +0 -143
- package/dist/agentdb.min.js.map +0 -7
- package/dist/cache/query-cache.d.ts +0 -105
- package/dist/cache/query-cache.d.ts.map +0 -1
- package/dist/cache/query-cache.js +0 -224
- package/dist/cache/query-cache.js.map +0 -1
- package/dist/cache/query-cache.mjs +0 -219
- package/dist/cli/cache/query-cache.d.ts +0 -104
- package/dist/cli/cache/query-cache.js +0 -244
- package/dist/cli/cli/db-commands.d.ts +0 -48
- package/dist/cli/cli/db-commands.js +0 -613
- package/dist/cli/commands.d.ts +0 -7
- package/dist/cli/commands.d.ts.map +0 -1
- package/dist/cli/commands.js +0 -113
- package/dist/cli/commands.js.map +0 -1
- package/dist/cli/commands.mjs +0 -104
- package/dist/cli/core/backend-interface.d.ts +0 -70
- package/dist/cli/core/backend-interface.js +0 -15
- package/dist/cli/core/native-backend.d.ts +0 -140
- package/dist/cli/core/native-backend.js +0 -432
- package/dist/cli/core/vector-db.d.ts +0 -126
- package/dist/cli/core/vector-db.js +0 -338
- package/dist/cli/core/wasm-backend.d.ts +0 -95
- package/dist/cli/core/wasm-backend.js +0 -418
- package/dist/cli/db-commands.d.ts +0 -49
- package/dist/cli/db-commands.d.ts.map +0 -1
- package/dist/cli/db-commands.js +0 -533
- package/dist/cli/db-commands.js.map +0 -1
- package/dist/cli/db-commands.mjs +0 -522
- package/dist/cli/generator.d.ts +0 -11
- package/dist/cli/generator.d.ts.map +0 -1
- package/dist/cli/generator.js +0 -567
- package/dist/cli/generator.js.map +0 -1
- package/dist/cli/generator.mjs +0 -527
- package/dist/cli/help.d.ts +0 -18
- package/dist/cli/help.d.ts.map +0 -1
- package/dist/cli/help.js +0 -676
- package/dist/cli/help.js.map +0 -1
- package/dist/cli/help.mjs +0 -667
- package/dist/cli/index/hnsw.d.ts +0 -164
- package/dist/cli/index/hnsw.js +0 -558
- package/dist/cli/plugin-cli.d.ts +0 -7
- package/dist/cli/plugin-cli.d.ts.map +0 -1
- package/dist/cli/plugin-cli.js +0 -295
- package/dist/cli/plugin-cli.js.map +0 -1
- package/dist/cli/plugin-cli.mjs +0 -289
- package/dist/cli/quantization/product-quantization.d.ts +0 -108
- package/dist/cli/quantization/product-quantization.js +0 -350
- package/dist/cli/query/query-builder.d.ts +0 -322
- package/dist/cli/query/query-builder.js +0 -600
- package/dist/cli/templates.d.ts +0 -14
- package/dist/cli/templates.d.ts.map +0 -1
- package/dist/cli/templates.js +0 -182
- package/dist/cli/templates.js.map +0 -1
- package/dist/cli/templates.mjs +0 -176
- package/dist/cli/types/index.d.ts +0 -116
- package/dist/cli/types/index.js +0 -5
- package/dist/cli/types.d.ts +0 -91
- package/dist/cli/types.d.ts.map +0 -1
- package/dist/cli/types.js +0 -6
- package/dist/cli/types.js.map +0 -1
- package/dist/cli/types.mjs +0 -4
- package/dist/cli/wizard/index.d.ts +0 -6
- package/dist/cli/wizard/index.d.ts.map +0 -1
- package/dist/cli/wizard/index.js +0 -138
- package/dist/cli/wizard/index.js.map +0 -1
- package/dist/cli/wizard/index.mjs +0 -131
- package/dist/cli/wizard/prompts.d.ts +0 -11
- package/dist/cli/wizard/prompts.d.ts.map +0 -1
- package/dist/cli/wizard/prompts.js +0 -482
- package/dist/cli/wizard/prompts.js.map +0 -1
- package/dist/cli/wizard/prompts.mjs +0 -470
- package/dist/cli/wizard/validator.d.ts +0 -13
- package/dist/cli/wizard/validator.d.ts.map +0 -1
- package/dist/cli/wizard/validator.js +0 -234
- package/dist/cli/wizard/validator.js.map +0 -1
- package/dist/cli/wizard/validator.mjs +0 -224
- package/dist/core/backend-interface.d.ts +0 -71
- package/dist/core/backend-interface.d.ts.map +0 -1
- package/dist/core/backend-interface.js +0 -16
- package/dist/core/backend-interface.js.map +0 -1
- package/dist/core/backend-interface.mjs +0 -12
- package/dist/core/native-backend.d.ts +0 -141
- package/dist/core/native-backend.d.ts.map +0 -1
- package/dist/core/native-backend.js +0 -457
- package/dist/core/native-backend.js.map +0 -1
- package/dist/core/native-backend.mjs +0 -449
- package/dist/core/vector-db.d.ts +0 -127
- package/dist/core/vector-db.d.ts.map +0 -1
- package/dist/core/vector-db.js +0 -270
- package/dist/core/vector-db.js.map +0 -1
- package/dist/core/vector-db.mjs +0 -265
- package/dist/core/wasm-backend.d.ts +0 -96
- package/dist/core/wasm-backend.d.ts.map +0 -1
- package/dist/core/wasm-backend.js +0 -393
- package/dist/core/wasm-backend.js.map +0 -1
- package/dist/core/wasm-backend.mjs +0 -385
- package/dist/index/hnsw-optimized.d.ts +0 -75
- package/dist/index/hnsw-optimized.d.ts.map +0 -1
- package/dist/index/hnsw-optimized.js +0 -412
- package/dist/index/hnsw-optimized.js.map +0 -1
- package/dist/index/hnsw-optimized.mjs +0 -407
- package/dist/index/hnsw.d.ts +0 -165
- package/dist/index/hnsw.d.ts.map +0 -1
- package/dist/index/hnsw.js +0 -521
- package/dist/index/hnsw.js.map +0 -1
- package/dist/index/hnsw.mjs +0 -516
- package/dist/index.browser.d.ts +0 -47
- package/dist/index.browser.d.ts.map +0 -1
- package/dist/index.browser.js +0 -72
- package/dist/index.browser.js.map +0 -1
- package/dist/index.browser.mjs +0 -54
- package/dist/index.d.ts +0 -59
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -84
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -65
- package/dist/mcp/learning/core/experience-buffer.d.ts +0 -61
- package/dist/mcp/learning/core/experience-buffer.d.ts.map +0 -1
- package/dist/mcp/learning/core/experience-buffer.js +0 -175
- package/dist/mcp/learning/core/experience-buffer.js.map +0 -1
- package/dist/mcp/learning/core/experience-buffer.mjs +0 -170
- package/dist/mcp/learning/core/experience-recorder.d.ts +0 -40
- package/dist/mcp/learning/core/experience-recorder.d.ts.map +0 -1
- package/dist/mcp/learning/core/experience-recorder.js +0 -200
- package/dist/mcp/learning/core/experience-recorder.js.map +0 -1
- package/dist/mcp/learning/core/experience-recorder.mjs +0 -195
- package/dist/mcp/learning/core/learning-manager.d.ts +0 -66
- package/dist/mcp/learning/core/learning-manager.d.ts.map +0 -1
- package/dist/mcp/learning/core/learning-manager.js +0 -252
- package/dist/mcp/learning/core/learning-manager.js.map +0 -1
- package/dist/mcp/learning/core/learning-manager.mjs +0 -247
- package/dist/mcp/learning/core/policy-optimizer.d.ts +0 -53
- package/dist/mcp/learning/core/policy-optimizer.d.ts.map +0 -1
- package/dist/mcp/learning/core/policy-optimizer.js +0 -251
- package/dist/mcp/learning/core/policy-optimizer.js.map +0 -1
- package/dist/mcp/learning/core/policy-optimizer.mjs +0 -246
- package/dist/mcp/learning/core/reward-estimator.d.ts +0 -44
- package/dist/mcp/learning/core/reward-estimator.d.ts.map +0 -1
- package/dist/mcp/learning/core/reward-estimator.js +0 -158
- package/dist/mcp/learning/core/reward-estimator.js.map +0 -1
- package/dist/mcp/learning/core/reward-estimator.mjs +0 -153
- package/dist/mcp/learning/core/session-manager.d.ts +0 -63
- package/dist/mcp/learning/core/session-manager.d.ts.map +0 -1
- package/dist/mcp/learning/core/session-manager.js +0 -202
- package/dist/mcp/learning/core/session-manager.js.map +0 -1
- package/dist/mcp/learning/core/session-manager.mjs +0 -197
- package/dist/mcp/learning/index.d.ts +0 -19
- package/dist/mcp/learning/index.d.ts.map +0 -1
- package/dist/mcp/learning/index.js +0 -30
- package/dist/mcp/learning/index.js.map +0 -1
- package/dist/mcp/learning/index.mjs +0 -19
- package/dist/mcp/learning/tools/mcp-learning-tools.d.ts +0 -369
- package/dist/mcp/learning/tools/mcp-learning-tools.d.ts.map +0 -1
- package/dist/mcp/learning/tools/mcp-learning-tools.js +0 -361
- package/dist/mcp/learning/tools/mcp-learning-tools.js.map +0 -1
- package/dist/mcp/learning/tools/mcp-learning-tools.mjs +0 -356
- package/dist/mcp/learning/types/index.d.ts +0 -138
- package/dist/mcp/learning/types/index.d.ts.map +0 -1
- package/dist/mcp/learning/types/index.js +0 -6
- package/dist/mcp/learning/types/index.js.map +0 -1
- package/dist/mcp/learning/types/index.mjs +0 -4
- package/dist/mcp-server.d.ts +0 -29
- package/dist/mcp-server.d.ts.map +0 -1
- package/dist/mcp-server.js +0 -862
- package/dist/mcp-server.js.map +0 -1
- package/dist/mcp-server.mjs +0 -857
- package/dist/plugins/base-plugin.d.ts +0 -114
- package/dist/plugins/base-plugin.d.ts.map +0 -1
- package/dist/plugins/base-plugin.js +0 -313
- package/dist/plugins/base-plugin.js.map +0 -1
- package/dist/plugins/base-plugin.mjs +0 -275
- package/dist/plugins/implementations/active-learning.d.ts +0 -135
- package/dist/plugins/implementations/active-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/active-learning.js +0 -372
- package/dist/plugins/implementations/active-learning.js.map +0 -1
- package/dist/plugins/implementations/active-learning.mjs +0 -367
- package/dist/plugins/implementations/actor-critic.d.ts +0 -64
- package/dist/plugins/implementations/actor-critic.d.ts.map +0 -1
- package/dist/plugins/implementations/actor-critic.js +0 -363
- package/dist/plugins/implementations/actor-critic.js.map +0 -1
- package/dist/plugins/implementations/actor-critic.mjs +0 -358
- package/dist/plugins/implementations/adversarial-training.d.ts +0 -133
- package/dist/plugins/implementations/adversarial-training.d.ts.map +0 -1
- package/dist/plugins/implementations/adversarial-training.js +0 -409
- package/dist/plugins/implementations/adversarial-training.js.map +0 -1
- package/dist/plugins/implementations/adversarial-training.mjs +0 -404
- package/dist/plugins/implementations/curriculum-learning.d.ts +0 -132
- package/dist/plugins/implementations/curriculum-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/curriculum-learning.js +0 -354
- package/dist/plugins/implementations/curriculum-learning.js.map +0 -1
- package/dist/plugins/implementations/curriculum-learning.mjs +0 -349
- package/dist/plugins/implementations/decision-transformer.d.ts +0 -77
- package/dist/plugins/implementations/decision-transformer.d.ts.map +0 -1
- package/dist/plugins/implementations/decision-transformer.js +0 -422
- package/dist/plugins/implementations/decision-transformer.js.map +0 -1
- package/dist/plugins/implementations/decision-transformer.mjs +0 -417
- package/dist/plugins/implementations/federated-learning.d.ts +0 -126
- package/dist/plugins/implementations/federated-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/federated-learning.js +0 -436
- package/dist/plugins/implementations/federated-learning.js.map +0 -1
- package/dist/plugins/implementations/federated-learning.mjs +0 -431
- package/dist/plugins/implementations/index.d.ts +0 -30
- package/dist/plugins/implementations/index.d.ts.map +0 -1
- package/dist/plugins/implementations/index.js +0 -45
- package/dist/plugins/implementations/index.js.map +0 -1
- package/dist/plugins/implementations/index.mjs +0 -31
- package/dist/plugins/implementations/multi-task-learning.d.ts +0 -115
- package/dist/plugins/implementations/multi-task-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/multi-task-learning.js +0 -369
- package/dist/plugins/implementations/multi-task-learning.js.map +0 -1
- package/dist/plugins/implementations/multi-task-learning.mjs +0 -364
- package/dist/plugins/implementations/neural-architecture-search.d.ts +0 -148
- package/dist/plugins/implementations/neural-architecture-search.d.ts.map +0 -1
- package/dist/plugins/implementations/neural-architecture-search.js +0 -379
- package/dist/plugins/implementations/neural-architecture-search.js.map +0 -1
- package/dist/plugins/implementations/neural-architecture-search.mjs +0 -374
- package/dist/plugins/implementations/q-learning.d.ts +0 -98
- package/dist/plugins/implementations/q-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/q-learning.js +0 -435
- package/dist/plugins/implementations/q-learning.js.map +0 -1
- package/dist/plugins/implementations/q-learning.mjs +0 -430
- package/dist/plugins/implementations/sarsa.d.ts +0 -103
- package/dist/plugins/implementations/sarsa.d.ts.map +0 -1
- package/dist/plugins/implementations/sarsa.js +0 -347
- package/dist/plugins/implementations/sarsa.js.map +0 -1
- package/dist/plugins/implementations/sarsa.mjs +0 -342
- package/dist/plugins/index.d.ts +0 -107
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/index.js +0 -179
- package/dist/plugins/index.js.map +0 -1
- package/dist/plugins/index.mjs +0 -168
- package/dist/plugins/interface.d.ts +0 -439
- package/dist/plugins/interface.d.ts.map +0 -1
- package/dist/plugins/interface.js +0 -12
- package/dist/plugins/interface.js.map +0 -1
- package/dist/plugins/interface.mjs +0 -10
- package/dist/plugins/learning-plugin.interface.d.ts +0 -257
- package/dist/plugins/learning-plugin.interface.d.ts.map +0 -1
- package/dist/plugins/learning-plugin.interface.js +0 -7
- package/dist/plugins/learning-plugin.interface.js.map +0 -1
- package/dist/plugins/learning-plugin.interface.mjs +0 -5
- package/dist/plugins/plugin-exports.d.ts +0 -71
- package/dist/plugins/plugin-exports.d.ts.map +0 -1
- package/dist/plugins/plugin-exports.js +0 -78
- package/dist/plugins/plugin-exports.js.map +0 -1
- package/dist/plugins/plugin-exports.mjs +0 -69
- package/dist/plugins/registry.d.ts +0 -206
- package/dist/plugins/registry.d.ts.map +0 -1
- package/dist/plugins/registry.js +0 -365
- package/dist/plugins/registry.js.map +0 -1
- package/dist/plugins/registry.mjs +0 -356
- package/dist/plugins/validator.d.ts +0 -63
- package/dist/plugins/validator.d.ts.map +0 -1
- package/dist/plugins/validator.js +0 -464
- package/dist/plugins/validator.js.map +0 -1
- package/dist/plugins/validator.mjs +0 -458
- package/dist/presets.d.ts +0 -65
- package/dist/presets.d.ts.map +0 -1
- package/dist/presets.js +0 -145
- package/dist/presets.js.map +0 -1
- package/dist/presets.mjs +0 -140
- package/dist/quantization/binary-quantization.d.ts +0 -104
- package/dist/quantization/binary-quantization.d.ts.map +0 -1
- package/dist/quantization/binary-quantization.js +0 -246
- package/dist/quantization/binary-quantization.js.map +0 -1
- package/dist/quantization/binary-quantization.mjs +0 -240
- package/dist/quantization/optimized-pq.d.ts +0 -138
- package/dist/quantization/optimized-pq.d.ts.map +0 -1
- package/dist/quantization/optimized-pq.js +0 -320
- package/dist/quantization/optimized-pq.js.map +0 -1
- package/dist/quantization/optimized-pq.mjs +0 -313
- package/dist/quantization/product-quantization.d.ts +0 -109
- package/dist/quantization/product-quantization.d.ts.map +0 -1
- package/dist/quantization/product-quantization.js +0 -287
- package/dist/quantization/product-quantization.js.map +0 -1
- package/dist/quantization/product-quantization.mjs +0 -282
- package/dist/quantization/scalar-quantization.d.ts +0 -100
- package/dist/quantization/scalar-quantization.d.ts.map +0 -1
- package/dist/quantization/scalar-quantization.js +0 -324
- package/dist/quantization/scalar-quantization.js.map +0 -1
- package/dist/quantization/scalar-quantization.mjs +0 -319
- package/dist/query/index.d.ts +0 -6
- package/dist/query/index.d.ts.map +0 -1
- package/dist/query/index.js +0 -9
- package/dist/query/index.js.map +0 -1
- package/dist/query/index.mjs +0 -4
- package/dist/query/query-builder.d.ts +0 -323
- package/dist/query/query-builder.d.ts.map +0 -1
- package/dist/query/query-builder.js +0 -524
- package/dist/query/query-builder.js.map +0 -1
- package/dist/query/query-builder.mjs +0 -519
- package/dist/reasoning/context-synthesizer.d.ts +0 -57
- package/dist/reasoning/context-synthesizer.d.ts.map +0 -1
- package/dist/reasoning/context-synthesizer.js +0 -224
- package/dist/reasoning/context-synthesizer.js.map +0 -1
- package/dist/reasoning/context-synthesizer.mjs +0 -219
- package/dist/reasoning/experience-curator.d.ts +0 -66
- package/dist/reasoning/experience-curator.d.ts.map +0 -1
- package/dist/reasoning/experience-curator.js +0 -288
- package/dist/reasoning/experience-curator.js.map +0 -1
- package/dist/reasoning/experience-curator.mjs +0 -283
- package/dist/reasoning/memory-optimizer.d.ts +0 -69
- package/dist/reasoning/memory-optimizer.d.ts.map +0 -1
- package/dist/reasoning/memory-optimizer.js +0 -331
- package/dist/reasoning/memory-optimizer.js.map +0 -1
- package/dist/reasoning/memory-optimizer.mjs +0 -326
- package/dist/reasoning/pattern-matcher.d.ts +0 -59
- package/dist/reasoning/pattern-matcher.d.ts.map +0 -1
- package/dist/reasoning/pattern-matcher.js +0 -229
- package/dist/reasoning/pattern-matcher.js.map +0 -1
- package/dist/reasoning/pattern-matcher.mjs +0 -224
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts +0 -118
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts.map +0 -1
- package/dist/reasoningbank/adapter/agentdb-adapter.js +0 -477
- package/dist/reasoningbank/adapter/agentdb-adapter.js.map +0 -1
- package/dist/reasoningbank/adapter/types.d.ts +0 -113
- package/dist/reasoningbank/adapter/types.d.ts.map +0 -1
- package/dist/reasoningbank/adapter/types.js +0 -9
- package/dist/reasoningbank/adapter/types.js.map +0 -1
- package/dist/reasoningbank/cli/commands.d.ts +0 -16
- package/dist/reasoningbank/cli/commands.d.ts.map +0 -1
- package/dist/reasoningbank/cli/commands.js +0 -272
- package/dist/reasoningbank/cli/commands.js.map +0 -1
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts +0 -319
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts.map +0 -1
- package/dist/reasoningbank/mcp/agentdb-tools.js +0 -301
- package/dist/reasoningbank/mcp/agentdb-tools.js.map +0 -1
- package/dist/reasoningbank/migration/migrate.d.ts +0 -25
- package/dist/reasoningbank/migration/migrate.d.ts.map +0 -1
- package/dist/reasoningbank/migration/migrate.js +0 -178
- package/dist/reasoningbank/migration/migrate.js.map +0 -1
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts +0 -37
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/context-synthesizer.js +0 -114
- package/dist/reasoningbank/reasoning/context-synthesizer.js.map +0 -1
- package/dist/reasoningbank/reasoning/experience-curator.d.ts +0 -39
- package/dist/reasoningbank/reasoning/experience-curator.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/experience-curator.js +0 -98
- package/dist/reasoningbank/reasoning/experience-curator.js.map +0 -1
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts +0 -44
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/memory-optimizer.js +0 -184
- package/dist/reasoningbank/reasoning/memory-optimizer.js.map +0 -1
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts +0 -40
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/pattern-matcher.js +0 -87
- package/dist/reasoningbank/reasoning/pattern-matcher.js.map +0 -1
- package/dist/reasoningbank/sync/quic-sync.d.ts +0 -77
- package/dist/reasoningbank/sync/quic-sync.d.ts.map +0 -1
- package/dist/reasoningbank/sync/quic-sync.js +0 -165
- package/dist/reasoningbank/sync/quic-sync.js.map +0 -1
- package/dist/sync/conflict.d.ts +0 -78
- package/dist/sync/conflict.d.ts.map +0 -1
- package/dist/sync/conflict.js +0 -202
- package/dist/sync/conflict.js.map +0 -1
- package/dist/sync/conflict.mjs +0 -196
- package/dist/sync/coordinator.d.ts +0 -111
- package/dist/sync/coordinator.d.ts.map +0 -1
- package/dist/sync/coordinator.js +0 -256
- package/dist/sync/coordinator.js.map +0 -1
- package/dist/sync/coordinator.mjs +0 -250
- package/dist/sync/delta.d.ts +0 -81
- package/dist/sync/delta.d.ts.map +0 -1
- package/dist/sync/delta.js +0 -245
- package/dist/sync/delta.js.map +0 -1
- package/dist/sync/delta.mjs +0 -238
- package/dist/sync/index.d.ts +0 -11
- package/dist/sync/index.d.ts.map +0 -1
- package/dist/sync/index.js +0 -22
- package/dist/sync/index.js.map +0 -1
- package/dist/sync/index.mjs +0 -9
- package/dist/sync/quic-sync.d.ts +0 -81
- package/dist/sync/quic-sync.d.ts.map +0 -1
- package/dist/sync/quic-sync.js +0 -329
- package/dist/sync/quic-sync.js.map +0 -1
- package/dist/sync/quic-sync.mjs +0 -323
- package/dist/sync/types.d.ts +0 -168
- package/dist/sync/types.d.ts.map +0 -1
- package/dist/sync/types.js +0 -8
- package/dist/sync/types.js.map +0 -1
- package/dist/sync/types.mjs +0 -6
- package/dist/types/index.d.ts +0 -117
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -6
- package/dist/types/index.js.map +0 -1
- package/dist/types/index.mjs +0 -4
- package/dist/wasm/sql-wasm-debug.js +0 -6989
- package/dist/wasm/sql-wasm-debug.wasm +0 -0
- package/dist/wasm/sql-wasm.js +0 -188
- package/dist/wasm/sql-wasm.wasm +0 -0
- package/dist/wasm-loader.d.ts +0 -32
- package/dist/wasm-loader.d.ts.map +0 -1
- package/dist/wasm-loader.js +0 -78
- package/dist/wasm-loader.js.map +0 -1
- package/dist/wasm-loader.mjs +0 -67
- package/examples/adaptive-learning.ts +0 -284
- package/examples/browser/README.md +0 -732
- package/examples/browser/adaptive-recommendations/index.html +0 -427
- package/examples/browser/collaborative-filtering/index.html +0 -310
- package/examples/browser/continual-learning/index.html +0 -736
- package/examples/browser/experience-replay/index.html +0 -616
- package/examples/browser/index.html +0 -369
- package/examples/browser/meta-learning/index.html +0 -789
- package/examples/browser/neuro-symbolic/index.html +0 -692
- package/examples/browser/pattern-learning/index.html +0 -620
- package/examples/browser/quantum-inspired/index.html +0 -728
- package/examples/browser/rag/index.html +0 -624
- package/examples/browser/swarm-intelligence/index.html +0 -811
- package/examples/browser-basic.html +0 -170
- package/examples/browser-wasm-real.html +0 -231
- package/examples/hnsw-example.ts +0 -148
- package/examples/mcp-learning-example.ts +0 -220
- package/examples/node-basic.js +0 -70
- package/examples/quic-sync-example.ts +0 -310
- package/examples/quick-start.js +0 -68
- package/examples/test-v1.0.7-cdn.html +0 -190
- package/examples/wasm-example.ts +0 -222
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ExplainableRecall - Provenance and Justification for Memory Retrieval
|
|
3
|
+
*
|
|
4
|
+
* Every retrieval returns:
|
|
5
|
+
* - Minimal hitting set of facts that justify the answer
|
|
6
|
+
* - Merkle proof chain for provenance
|
|
7
|
+
* - Policy compliance certificates
|
|
8
|
+
*
|
|
9
|
+
* Based on:
|
|
10
|
+
* - Minimal hitting set algorithms
|
|
11
|
+
* - Merkle tree provenance
|
|
12
|
+
* - Explainable AI techniques
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Database } from 'better-sqlite3';
|
|
16
|
+
import * as crypto from 'crypto';
|
|
17
|
+
|
|
18
|
+
export interface RecallCertificate {
|
|
19
|
+
id: string; // UUID
|
|
20
|
+
queryId: string;
|
|
21
|
+
queryText: string;
|
|
22
|
+
|
|
23
|
+
// Retrieved chunks
|
|
24
|
+
chunkIds: string[];
|
|
25
|
+
chunkTypes: string[];
|
|
26
|
+
|
|
27
|
+
// Justification
|
|
28
|
+
minimalWhy: string[]; // Minimal hitting set
|
|
29
|
+
redundancyRatio: number; // len(chunks) / len(minimalWhy)
|
|
30
|
+
completenessScore: number; // Fraction of requirements met
|
|
31
|
+
|
|
32
|
+
// Provenance
|
|
33
|
+
merkleRoot: string;
|
|
34
|
+
sourceHashes: string[];
|
|
35
|
+
proofChain: MerkleProof[];
|
|
36
|
+
|
|
37
|
+
// Policy
|
|
38
|
+
policyProof?: string;
|
|
39
|
+
policyVersion?: string;
|
|
40
|
+
accessLevel: 'public' | 'internal' | 'confidential' | 'restricted';
|
|
41
|
+
|
|
42
|
+
latencyMs?: number;
|
|
43
|
+
metadata?: Record<string, any>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface MerkleProof {
|
|
47
|
+
hash: string;
|
|
48
|
+
position: 'left' | 'right';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface JustificationPath {
|
|
52
|
+
chunkId: string;
|
|
53
|
+
chunkType: string;
|
|
54
|
+
reason: 'semantic_match' | 'causal_link' | 'prerequisite' | 'constraint';
|
|
55
|
+
necessityScore: number; // 0-1
|
|
56
|
+
pathElements: string[]; // Reasoning chain
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ProvenanceSource {
|
|
60
|
+
id?: number;
|
|
61
|
+
sourceType: 'episode' | 'skill' | 'note' | 'fact' | 'external';
|
|
62
|
+
sourceId: number;
|
|
63
|
+
contentHash: string;
|
|
64
|
+
parentHash?: string;
|
|
65
|
+
derivedFrom?: string[];
|
|
66
|
+
creator?: string;
|
|
67
|
+
metadata?: Record<string, any>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class ExplainableRecall {
|
|
71
|
+
private db: Database;
|
|
72
|
+
|
|
73
|
+
constructor(db: Database) {
|
|
74
|
+
this.db = db;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Create a recall certificate for a retrieval operation
|
|
79
|
+
*/
|
|
80
|
+
createCertificate(params: {
|
|
81
|
+
queryId: string;
|
|
82
|
+
queryText: string;
|
|
83
|
+
chunks: Array<{ id: string; type: string; content: string; relevance: number }>;
|
|
84
|
+
requirements: string[]; // Query requirements
|
|
85
|
+
accessLevel?: string;
|
|
86
|
+
}): RecallCertificate {
|
|
87
|
+
const { queryId, queryText, chunks, requirements, accessLevel = 'internal' } = params;
|
|
88
|
+
|
|
89
|
+
const startTime = Date.now();
|
|
90
|
+
|
|
91
|
+
// 1. Compute minimal hitting set
|
|
92
|
+
const minimalWhy = this.computeMinimalHittingSet(chunks, requirements);
|
|
93
|
+
|
|
94
|
+
// 2. Calculate metrics
|
|
95
|
+
const redundancyRatio = chunks.length / minimalWhy.length;
|
|
96
|
+
const completenessScore = this.calculateCompleteness(minimalWhy, requirements);
|
|
97
|
+
|
|
98
|
+
// 3. Build provenance chain
|
|
99
|
+
const sourceHashes = chunks.map(chunk =>
|
|
100
|
+
this.getOrCreateProvenance(chunk.type, parseInt(chunk.id))
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const merkleTree = this.buildMerkleTree(sourceHashes);
|
|
104
|
+
const merkleRoot = merkleTree.root;
|
|
105
|
+
|
|
106
|
+
// 4. Generate chunk metadata first (needed for certificate ID)
|
|
107
|
+
const chunkIds = chunks.map(c => c.id);
|
|
108
|
+
const chunkTypes = chunks.map(c => c.type);
|
|
109
|
+
|
|
110
|
+
// 5. Create certificate ID
|
|
111
|
+
const certificateId = this.generateCertificateId(queryId, chunkIds);
|
|
112
|
+
|
|
113
|
+
// 6. Generate proof chain for each chunk
|
|
114
|
+
const proofChain = chunks.map((chunk, idx) =>
|
|
115
|
+
this.getMerkleProof(merkleTree, idx)
|
|
116
|
+
).flat();
|
|
117
|
+
|
|
118
|
+
// 7. Store certificate
|
|
119
|
+
this.db.prepare(`
|
|
120
|
+
INSERT INTO recall_certificates (
|
|
121
|
+
id, query_id, query_text, chunk_ids, chunk_types,
|
|
122
|
+
minimal_why, redundancy_ratio, completeness_score,
|
|
123
|
+
merkle_root, source_hashes, proof_chain,
|
|
124
|
+
access_level, latency_ms
|
|
125
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
126
|
+
`).run(
|
|
127
|
+
certificateId,
|
|
128
|
+
queryId,
|
|
129
|
+
queryText,
|
|
130
|
+
JSON.stringify(chunkIds),
|
|
131
|
+
JSON.stringify(chunkTypes),
|
|
132
|
+
JSON.stringify(minimalWhy),
|
|
133
|
+
redundancyRatio,
|
|
134
|
+
completenessScore,
|
|
135
|
+
merkleRoot,
|
|
136
|
+
JSON.stringify(sourceHashes),
|
|
137
|
+
JSON.stringify(proofChain),
|
|
138
|
+
accessLevel,
|
|
139
|
+
Date.now() - startTime
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// 7. Store justification paths
|
|
143
|
+
this.storeJustificationPaths(certificateId, chunks, minimalWhy, requirements);
|
|
144
|
+
|
|
145
|
+
const certificate: RecallCertificate = {
|
|
146
|
+
id: certificateId,
|
|
147
|
+
queryId,
|
|
148
|
+
queryText,
|
|
149
|
+
chunkIds,
|
|
150
|
+
chunkTypes,
|
|
151
|
+
minimalWhy,
|
|
152
|
+
redundancyRatio,
|
|
153
|
+
completenessScore,
|
|
154
|
+
merkleRoot,
|
|
155
|
+
sourceHashes,
|
|
156
|
+
proofChain,
|
|
157
|
+
accessLevel: accessLevel as any,
|
|
158
|
+
latencyMs: Date.now() - startTime
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return certificate;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Verify a recall certificate
|
|
166
|
+
*/
|
|
167
|
+
verifyCertificate(certificateId: string): {
|
|
168
|
+
valid: boolean;
|
|
169
|
+
issues: string[];
|
|
170
|
+
} {
|
|
171
|
+
const cert = this.db.prepare(
|
|
172
|
+
'SELECT * FROM recall_certificates WHERE id = ?'
|
|
173
|
+
).get(certificateId) as any;
|
|
174
|
+
|
|
175
|
+
if (!cert) {
|
|
176
|
+
return { valid: false, issues: ['Certificate not found'] };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const issues: string[] = [];
|
|
180
|
+
|
|
181
|
+
// 1. Verify Merkle root
|
|
182
|
+
const sourceHashes = JSON.parse(cert.source_hashes);
|
|
183
|
+
const merkleTree = this.buildMerkleTree(sourceHashes);
|
|
184
|
+
|
|
185
|
+
if (merkleTree.root !== cert.merkle_root) {
|
|
186
|
+
issues.push('Merkle root mismatch');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 2. Verify chunk hashes still match
|
|
190
|
+
const chunkIds = JSON.parse(cert.chunk_ids);
|
|
191
|
+
const chunkTypes = JSON.parse(cert.chunk_types);
|
|
192
|
+
|
|
193
|
+
for (let i = 0; i < chunkIds.length; i++) {
|
|
194
|
+
const currentHash = this.getContentHash(chunkTypes[i], parseInt(chunkIds[i]));
|
|
195
|
+
if (currentHash !== sourceHashes[i]) {
|
|
196
|
+
issues.push(`Chunk ${chunkIds[i]} hash changed`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 3. Verify completeness
|
|
201
|
+
const minimalWhy = JSON.parse(cert.minimal_why);
|
|
202
|
+
if (minimalWhy.length === 0) {
|
|
203
|
+
issues.push('Empty justification set');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 4. Verify redundancy ratio
|
|
207
|
+
if (cert.redundancy_ratio < 1.0) {
|
|
208
|
+
issues.push('Invalid redundancy ratio');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
valid: issues.length === 0,
|
|
213
|
+
issues
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Get justification for why a chunk was included
|
|
219
|
+
*/
|
|
220
|
+
getJustification(certificateId: string, chunkId: string): JustificationPath | null {
|
|
221
|
+
const row = this.db.prepare(`
|
|
222
|
+
SELECT * FROM justification_paths
|
|
223
|
+
WHERE certificate_id = ? AND chunk_id = ?
|
|
224
|
+
`).get(certificateId, chunkId) as any;
|
|
225
|
+
|
|
226
|
+
if (!row) return null;
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
chunkId: row.chunk_id,
|
|
230
|
+
chunkType: row.chunk_type,
|
|
231
|
+
reason: row.reason,
|
|
232
|
+
necessityScore: row.necessity_score,
|
|
233
|
+
pathElements: JSON.parse(row.path_elements)
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Get provenance lineage for a source
|
|
239
|
+
*/
|
|
240
|
+
getProvenanceLineage(contentHash: string): ProvenanceSource[] {
|
|
241
|
+
const lineage: ProvenanceSource[] = [];
|
|
242
|
+
let currentHash: string | null = contentHash;
|
|
243
|
+
|
|
244
|
+
while (currentHash) {
|
|
245
|
+
const source = this.db.prepare(`
|
|
246
|
+
SELECT * FROM provenance_sources WHERE content_hash = ?
|
|
247
|
+
`).get(currentHash) as any;
|
|
248
|
+
|
|
249
|
+
if (!source) break;
|
|
250
|
+
|
|
251
|
+
lineage.push({
|
|
252
|
+
id: source.id,
|
|
253
|
+
sourceType: source.source_type,
|
|
254
|
+
sourceId: source.source_id,
|
|
255
|
+
contentHash: source.content_hash,
|
|
256
|
+
parentHash: source.parent_hash,
|
|
257
|
+
derivedFrom: source.derived_from ? JSON.parse(source.derived_from) : undefined,
|
|
258
|
+
creator: source.creator,
|
|
259
|
+
metadata: source.metadata ? JSON.parse(source.metadata) : undefined
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
currentHash = source.parent_hash;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return lineage;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Audit certificate access
|
|
270
|
+
*/
|
|
271
|
+
auditCertificate(certificateId: string): {
|
|
272
|
+
certificate: RecallCertificate;
|
|
273
|
+
justifications: JustificationPath[];
|
|
274
|
+
provenance: Map<string, ProvenanceSource[]>;
|
|
275
|
+
quality: {
|
|
276
|
+
completeness: number;
|
|
277
|
+
redundancy: number;
|
|
278
|
+
avgNecessity: number;
|
|
279
|
+
};
|
|
280
|
+
} {
|
|
281
|
+
const certRow = this.db.prepare(
|
|
282
|
+
'SELECT * FROM recall_certificates WHERE id = ?'
|
|
283
|
+
).get(certificateId) as any;
|
|
284
|
+
|
|
285
|
+
if (!certRow) {
|
|
286
|
+
throw new Error(`Certificate ${certificateId} not found`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const certificate: RecallCertificate = {
|
|
290
|
+
id: certRow.id,
|
|
291
|
+
queryId: certRow.query_id,
|
|
292
|
+
queryText: certRow.query_text,
|
|
293
|
+
chunkIds: JSON.parse(certRow.chunk_ids),
|
|
294
|
+
chunkTypes: JSON.parse(certRow.chunk_types),
|
|
295
|
+
minimalWhy: JSON.parse(certRow.minimal_why),
|
|
296
|
+
redundancyRatio: certRow.redundancy_ratio,
|
|
297
|
+
completenessScore: certRow.completeness_score,
|
|
298
|
+
merkleRoot: certRow.merkle_root,
|
|
299
|
+
sourceHashes: JSON.parse(certRow.source_hashes),
|
|
300
|
+
proofChain: JSON.parse(certRow.proof_chain),
|
|
301
|
+
policyProof: certRow.policy_proof,
|
|
302
|
+
policyVersion: certRow.policy_version,
|
|
303
|
+
accessLevel: certRow.access_level,
|
|
304
|
+
latencyMs: certRow.latency_ms
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// Get justifications
|
|
308
|
+
const justRows = this.db.prepare(`
|
|
309
|
+
SELECT * FROM justification_paths WHERE certificate_id = ?
|
|
310
|
+
`).all(certificateId) as any[];
|
|
311
|
+
|
|
312
|
+
const justifications = justRows.map(row => ({
|
|
313
|
+
chunkId: row.chunk_id,
|
|
314
|
+
chunkType: row.chunk_type,
|
|
315
|
+
reason: row.reason,
|
|
316
|
+
necessityScore: row.necessity_score,
|
|
317
|
+
pathElements: JSON.parse(row.path_elements)
|
|
318
|
+
}));
|
|
319
|
+
|
|
320
|
+
// Get provenance for each source
|
|
321
|
+
const provenance = new Map<string, ProvenanceSource[]>();
|
|
322
|
+
for (const hash of certificate.sourceHashes) {
|
|
323
|
+
provenance.set(hash, this.getProvenanceLineage(hash));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Calculate quality metrics
|
|
327
|
+
const avgNecessity = justifications.reduce((sum, j) => sum + j.necessityScore, 0) / justifications.length;
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
certificate,
|
|
331
|
+
justifications,
|
|
332
|
+
provenance,
|
|
333
|
+
quality: {
|
|
334
|
+
completeness: certificate.completenessScore,
|
|
335
|
+
redundancy: certificate.redundancyRatio,
|
|
336
|
+
avgNecessity
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ========================================================================
|
|
342
|
+
// Private Helper Methods
|
|
343
|
+
// ========================================================================
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Compute minimal hitting set using greedy algorithm
|
|
347
|
+
* A hitting set contains at least one element from each requirement
|
|
348
|
+
*/
|
|
349
|
+
private computeMinimalHittingSet(
|
|
350
|
+
chunks: Array<{ id: string; content: string; relevance: number }>,
|
|
351
|
+
requirements: string[]
|
|
352
|
+
): string[] {
|
|
353
|
+
if (requirements.length === 0) {
|
|
354
|
+
return chunks.slice(0, Math.min(3, chunks.length)).map(c => c.id);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const uncovered = new Set(requirements);
|
|
358
|
+
const selected: string[] = [];
|
|
359
|
+
|
|
360
|
+
// Greedy: select chunk that covers most uncovered requirements
|
|
361
|
+
while (uncovered.size > 0 && chunks.length > 0) {
|
|
362
|
+
let bestChunk: any = null;
|
|
363
|
+
let bestCoverage = 0;
|
|
364
|
+
|
|
365
|
+
for (const chunk of chunks) {
|
|
366
|
+
const coverage = Array.from(uncovered).filter(req =>
|
|
367
|
+
chunk.content.toLowerCase().includes(req.toLowerCase())
|
|
368
|
+
).length;
|
|
369
|
+
|
|
370
|
+
if (coverage > bestCoverage) {
|
|
371
|
+
bestCoverage = coverage;
|
|
372
|
+
bestChunk = chunk;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (!bestChunk) break;
|
|
377
|
+
|
|
378
|
+
selected.push(bestChunk.id);
|
|
379
|
+
|
|
380
|
+
// Remove covered requirements
|
|
381
|
+
for (const req of Array.from(uncovered)) {
|
|
382
|
+
if (bestChunk.content.toLowerCase().includes(req.toLowerCase())) {
|
|
383
|
+
uncovered.delete(req);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Remove selected chunk
|
|
388
|
+
chunks = chunks.filter(c => c.id !== bestChunk.id);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return selected;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Calculate completeness score
|
|
396
|
+
*/
|
|
397
|
+
private calculateCompleteness(minimalWhy: string[], requirements: string[]): number {
|
|
398
|
+
if (requirements.length === 0) return 1.0;
|
|
399
|
+
|
|
400
|
+
const chunks = minimalWhy.map(id => {
|
|
401
|
+
// Get chunk content
|
|
402
|
+
const episode = this.db.prepare('SELECT output FROM episodes WHERE id = ?').get(parseInt(id));
|
|
403
|
+
return episode ? (episode as any).output : '';
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
const satisfied = requirements.filter(req =>
|
|
407
|
+
chunks.some(content => content && content.toLowerCase().includes(req.toLowerCase()))
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
return satisfied.length / requirements.length;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Get or create provenance record
|
|
415
|
+
*/
|
|
416
|
+
private getOrCreateProvenance(sourceType: string, sourceId: number): string {
|
|
417
|
+
// Check if provenance exists
|
|
418
|
+
const existing = this.db.prepare(`
|
|
419
|
+
SELECT content_hash FROM provenance_sources
|
|
420
|
+
WHERE source_type = ? AND source_id = ?
|
|
421
|
+
`).get(sourceType, sourceId) as any;
|
|
422
|
+
|
|
423
|
+
if (existing) {
|
|
424
|
+
return existing.content_hash;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Create new provenance
|
|
428
|
+
const contentHash = this.getContentHash(sourceType, sourceId);
|
|
429
|
+
|
|
430
|
+
this.db.prepare(`
|
|
431
|
+
INSERT INTO provenance_sources (source_type, source_id, content_hash, creator)
|
|
432
|
+
VALUES (?, ?, ?, ?)
|
|
433
|
+
`).run(sourceType, sourceId, contentHash, 'system');
|
|
434
|
+
|
|
435
|
+
return contentHash;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Get content hash for a memory
|
|
440
|
+
*/
|
|
441
|
+
private getContentHash(sourceType: string, sourceId: number): string {
|
|
442
|
+
let content = '';
|
|
443
|
+
|
|
444
|
+
switch (sourceType) {
|
|
445
|
+
case 'episode':
|
|
446
|
+
const episode = this.db.prepare('SELECT task, output FROM episodes WHERE id = ?').get(sourceId) as any;
|
|
447
|
+
content = episode ? `${episode.task}:${episode.output}` : '';
|
|
448
|
+
break;
|
|
449
|
+
case 'skill':
|
|
450
|
+
const skill = this.db.prepare('SELECT name, code FROM skills WHERE id = ?').get(sourceId) as any;
|
|
451
|
+
content = skill ? `${skill.name}:${skill.code}` : '';
|
|
452
|
+
break;
|
|
453
|
+
case 'note':
|
|
454
|
+
const note = this.db.prepare('SELECT text FROM notes WHERE id = ?').get(sourceId) as any;
|
|
455
|
+
content = note ? note.text : '';
|
|
456
|
+
break;
|
|
457
|
+
case 'fact':
|
|
458
|
+
const fact = this.db.prepare('SELECT subject, predicate, object FROM facts WHERE id = ?').get(sourceId) as any;
|
|
459
|
+
content = fact ? `${fact.subject}:${fact.predicate}:${fact.object}` : '';
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return crypto.createHash('sha256').update(content).digest('hex');
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Build Merkle tree from hashes
|
|
468
|
+
*/
|
|
469
|
+
private buildMerkleTree(hashes: string[]): { root: string; tree: string[][] } {
|
|
470
|
+
if (hashes.length === 0) {
|
|
471
|
+
return { root: '', tree: [[]] };
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const tree: string[][] = [hashes];
|
|
475
|
+
|
|
476
|
+
while (tree[tree.length - 1].length > 1) {
|
|
477
|
+
const level = tree[tree.length - 1];
|
|
478
|
+
const nextLevel: string[] = [];
|
|
479
|
+
|
|
480
|
+
for (let i = 0; i < level.length; i += 2) {
|
|
481
|
+
if (i + 1 < level.length) {
|
|
482
|
+
const combined = level[i] + level[i + 1];
|
|
483
|
+
nextLevel.push(crypto.createHash('sha256').update(combined).digest('hex'));
|
|
484
|
+
} else {
|
|
485
|
+
nextLevel.push(level[i]);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
tree.push(nextLevel);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return { root: tree[tree.length - 1][0], tree };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Get Merkle proof for a leaf
|
|
497
|
+
*/
|
|
498
|
+
private getMerkleProof(merkleTree: { tree: string[][] }, leafIndex: number): MerkleProof[] {
|
|
499
|
+
const proof: MerkleProof[] = [];
|
|
500
|
+
let index = leafIndex;
|
|
501
|
+
|
|
502
|
+
for (let level = 0; level < merkleTree.tree.length - 1; level++) {
|
|
503
|
+
const currentLevel = merkleTree.tree[level];
|
|
504
|
+
const isLeftNode = index % 2 === 0;
|
|
505
|
+
const siblingIndex = isLeftNode ? index + 1 : index - 1;
|
|
506
|
+
|
|
507
|
+
if (siblingIndex < currentLevel.length) {
|
|
508
|
+
proof.push({
|
|
509
|
+
hash: currentLevel[siblingIndex],
|
|
510
|
+
position: isLeftNode ? 'right' : 'left'
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
index = Math.floor(index / 2);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return proof;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Generate certificate ID
|
|
522
|
+
*/
|
|
523
|
+
private generateCertificateId(queryId: string, chunkIds: string[]): string {
|
|
524
|
+
const data = `${queryId}:${chunkIds.join(',')}:${Date.now()}`;
|
|
525
|
+
return crypto.createHash('sha256').update(data).digest('hex');
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Store justification paths
|
|
530
|
+
*/
|
|
531
|
+
private storeJustificationPaths(
|
|
532
|
+
certificateId: string,
|
|
533
|
+
chunks: Array<{ id: string; type: string; relevance: number }>,
|
|
534
|
+
minimalWhy: string[],
|
|
535
|
+
requirements: string[]
|
|
536
|
+
): void {
|
|
537
|
+
const stmt = this.db.prepare(`
|
|
538
|
+
INSERT INTO justification_paths (
|
|
539
|
+
certificate_id, chunk_id, chunk_type, reason, necessity_score, path_elements
|
|
540
|
+
) VALUES (?, ?, ?, ?, ?, ?)
|
|
541
|
+
`);
|
|
542
|
+
|
|
543
|
+
for (const chunk of chunks) {
|
|
544
|
+
const isNecessary = minimalWhy.includes(chunk.id);
|
|
545
|
+
const reason = this.determineReason(chunk, requirements);
|
|
546
|
+
const necessityScore = isNecessary ? chunk.relevance : chunk.relevance * 0.5;
|
|
547
|
+
|
|
548
|
+
const pathElements = [
|
|
549
|
+
`Retrieved for query`,
|
|
550
|
+
isNecessary ? `Essential for justification` : `Supporting evidence`,
|
|
551
|
+
`Relevance: ${(chunk.relevance * 100).toFixed(1)}%`
|
|
552
|
+
];
|
|
553
|
+
|
|
554
|
+
stmt.run(
|
|
555
|
+
certificateId,
|
|
556
|
+
chunk.id,
|
|
557
|
+
chunk.type,
|
|
558
|
+
reason,
|
|
559
|
+
necessityScore,
|
|
560
|
+
JSON.stringify(pathElements)
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Determine reason for inclusion
|
|
567
|
+
*/
|
|
568
|
+
private determineReason(
|
|
569
|
+
chunk: { id: string; relevance: number },
|
|
570
|
+
requirements: string[]
|
|
571
|
+
): string {
|
|
572
|
+
if (chunk.relevance > 0.9) return 'semantic_match';
|
|
573
|
+
if (chunk.relevance > 0.7) return 'causal_link';
|
|
574
|
+
if (chunk.relevance > 0.5) return 'prerequisite';
|
|
575
|
+
return 'constraint';
|
|
576
|
+
}
|
|
577
|
+
}
|