lex-agentic-inference 0.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +5 -0
- data/LICENSE +21 -0
- data/README.md +13 -0
- data/lex-agentic-inference.gemspec +30 -0
- data/lib/legion/extensions/agentic/inference/abductive/client.rb +25 -0
- data/lib/legion/extensions/agentic/inference/abductive/helpers/abduction_engine.rb +164 -0
- data/lib/legion/extensions/agentic/inference/abductive/helpers/constants.rb +39 -0
- data/lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb +106 -0
- data/lib/legion/extensions/agentic/inference/abductive/helpers/observation.rb +39 -0
- data/lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb +129 -0
- data/lib/legion/extensions/agentic/inference/abductive/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/abductive.rb +20 -0
- data/lib/legion/extensions/agentic/inference/affordance/actors/scan.rb +31 -0
- data/lib/legion/extensions/agentic/inference/affordance/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/affordance/helpers/affordance_field.rb +123 -0
- data/lib/legion/extensions/agentic/inference/affordance/helpers/affordance_item.rb +75 -0
- data/lib/legion/extensions/agentic/inference/affordance/helpers/constants.rb +42 -0
- data/lib/legion/extensions/agentic/inference/affordance/runners/affordance.rb +90 -0
- data/lib/legion/extensions/agentic/inference/affordance/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/affordance.rb +19 -0
- data/lib/legion/extensions/agentic/inference/analogical/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb +209 -0
- data/lib/legion/extensions/agentic/inference/analogical/helpers/constants.rb +38 -0
- data/lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb +113 -0
- data/lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb +106 -0
- data/lib/legion/extensions/agentic/inference/analogical/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/analogical.rb +19 -0
- data/lib/legion/extensions/agentic/inference/argument_mapping/client.rb +19 -0
- data/lib/legion/extensions/agentic/inference/argument_mapping/helpers/argument.rb +102 -0
- data/lib/legion/extensions/agentic/inference/argument_mapping/helpers/argument_engine.rb +131 -0
- data/lib/legion/extensions/agentic/inference/argument_mapping/helpers/constants.rb +44 -0
- data/lib/legion/extensions/agentic/inference/argument_mapping/runners/argument_mapping.rb +78 -0
- data/lib/legion/extensions/agentic/inference/argument_mapping/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/argument_mapping.rb +19 -0
- data/lib/legion/extensions/agentic/inference/bayesian/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb +96 -0
- data/lib/legion/extensions/agentic/inference/bayesian/helpers/belief_network.rb +147 -0
- data/lib/legion/extensions/agentic/inference/bayesian/helpers/constants.rb +36 -0
- data/lib/legion/extensions/agentic/inference/bayesian/runners/bayesian_belief.rb +125 -0
- data/lib/legion/extensions/agentic/inference/bayesian/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/bayesian.rb +19 -0
- data/lib/legion/extensions/agentic/inference/belief_revision/client.rb +19 -0
- data/lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb +155 -0
- data/lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb +193 -0
- data/lib/legion/extensions/agentic/inference/belief_revision/helpers/constants.rb +54 -0
- data/lib/legion/extensions/agentic/inference/belief_revision/helpers/evidence.rb +49 -0
- data/lib/legion/extensions/agentic/inference/belief_revision/runners/belief_revision.rb +89 -0
- data/lib/legion/extensions/agentic/inference/belief_revision/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/belief_revision.rb +21 -0
- data/lib/legion/extensions/agentic/inference/causal_attribution/client.rb +27 -0
- data/lib/legion/extensions/agentic/inference/causal_attribution/helpers/attribution.rb +92 -0
- data/lib/legion/extensions/agentic/inference/causal_attribution/helpers/attribution_engine.rb +159 -0
- data/lib/legion/extensions/agentic/inference/causal_attribution/runners/causal_attribution.rb +111 -0
- data/lib/legion/extensions/agentic/inference/causal_attribution/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/causal_attribution.rb +18 -0
- data/lib/legion/extensions/agentic/inference/causal_reasoning/client.rb +24 -0
- data/lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb +101 -0
- data/lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_graph.rb +184 -0
- data/lib/legion/extensions/agentic/inference/causal_reasoning/helpers/constants.rb +40 -0
- data/lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb +111 -0
- data/lib/legion/extensions/agentic/inference/causal_reasoning/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/causal_reasoning.rb +19 -0
- data/lib/legion/extensions/agentic/inference/coherence/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb +170 -0
- data/lib/legion/extensions/agentic/inference/coherence/helpers/constants.rb +35 -0
- data/lib/legion/extensions/agentic/inference/coherence/helpers/proposition.rb +100 -0
- data/lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb +123 -0
- data/lib/legion/extensions/agentic/inference/coherence/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/coherence.rb +19 -0
- data/lib/legion/extensions/agentic/inference/counterfactual/client.rb +19 -0
- data/lib/legion/extensions/agentic/inference/counterfactual/helpers/constants.rb +40 -0
- data/lib/legion/extensions/agentic/inference/counterfactual/helpers/counterfactual_engine.rb +157 -0
- data/lib/legion/extensions/agentic/inference/counterfactual/helpers/scenario.rb +98 -0
- data/lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb +106 -0
- data/lib/legion/extensions/agentic/inference/counterfactual/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/counterfactual.rb +19 -0
- data/lib/legion/extensions/agentic/inference/debugging/client.rb +30 -0
- data/lib/legion/extensions/agentic/inference/debugging/helpers/causal_trace.rb +51 -0
- data/lib/legion/extensions/agentic/inference/debugging/helpers/constants.rb +58 -0
- data/lib/legion/extensions/agentic/inference/debugging/helpers/correction.rb +56 -0
- data/lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb +156 -0
- data/lib/legion/extensions/agentic/inference/debugging/helpers/reasoning_error.rb +97 -0
- data/lib/legion/extensions/agentic/inference/debugging/runners/cognitive_debugging.rb +178 -0
- data/lib/legion/extensions/agentic/inference/debugging/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/debugging.rb +21 -0
- data/lib/legion/extensions/agentic/inference/enactive_cognition/client.rb +27 -0
- data/lib/legion/extensions/agentic/inference/enactive_cognition/helpers/enaction_engine.rb +120 -0
- data/lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb +122 -0
- data/lib/legion/extensions/agentic/inference/enactive_cognition/runners/enactive_cognition.rb +124 -0
- data/lib/legion/extensions/agentic/inference/enactive_cognition/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/enactive_cognition.rb +18 -0
- data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/client.rb +17 -0
- data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/constants.rb +47 -0
- data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb +82 -0
- data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/violation_engine.rb +126 -0
- data/lib/legion/extensions/agentic/inference/expectation_violation/runners/expectation_violation.rb +76 -0
- data/lib/legion/extensions/agentic/inference/expectation_violation/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/expectation_violation.rb +19 -0
- data/lib/legion/extensions/agentic/inference/free_energy/client.rb +19 -0
- data/lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb +127 -0
- data/lib/legion/extensions/agentic/inference/free_energy/helpers/constants.rb +58 -0
- data/lib/legion/extensions/agentic/inference/free_energy/helpers/free_energy_engine.rb +156 -0
- data/lib/legion/extensions/agentic/inference/free_energy/runners/free_energy.rb +97 -0
- data/lib/legion/extensions/agentic/inference/free_energy/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/free_energy.rb +19 -0
- data/lib/legion/extensions/agentic/inference/gravity/client.rb +29 -0
- data/lib/legion/extensions/agentic/inference/gravity/helpers/attractor.rb +77 -0
- data/lib/legion/extensions/agentic/inference/gravity/helpers/constants.rb +76 -0
- data/lib/legion/extensions/agentic/inference/gravity/helpers/gravity_engine.rb +164 -0
- data/lib/legion/extensions/agentic/inference/gravity/helpers/orbiting_thought.rb +64 -0
- data/lib/legion/extensions/agentic/inference/gravity/runners/gravity.rb +132 -0
- data/lib/legion/extensions/agentic/inference/gravity/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/gravity.rb +20 -0
- data/lib/legion/extensions/agentic/inference/horizon/actors/adjust.rb +45 -0
- data/lib/legion/extensions/agentic/inference/horizon/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/horizon/helpers/constants.rb +43 -0
- data/lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb +110 -0
- data/lib/legion/extensions/agentic/inference/horizon/helpers/projection.rb +59 -0
- data/lib/legion/extensions/agentic/inference/horizon/runners/cognitive_horizon.rb +107 -0
- data/lib/legion/extensions/agentic/inference/horizon/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/horizon.rb +19 -0
- data/lib/legion/extensions/agentic/inference/hypothesis_testing/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/constants.rb +37 -0
- data/lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis.rb +83 -0
- data/lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb +97 -0
- data/lib/legion/extensions/agentic/inference/hypothesis_testing/runners/hypothesis_testing.rb +115 -0
- data/lib/legion/extensions/agentic/inference/hypothesis_testing/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/hypothesis_testing.rb +19 -0
- data/lib/legion/extensions/agentic/inference/intuition/client.rb +19 -0
- data/lib/legion/extensions/agentic/inference/intuition/helpers/constants.rb +60 -0
- data/lib/legion/extensions/agentic/inference/intuition/helpers/heuristic.rb +66 -0
- data/lib/legion/extensions/agentic/inference/intuition/helpers/intuition_engine.rb +157 -0
- data/lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb +105 -0
- data/lib/legion/extensions/agentic/inference/intuition/runners/intuition.rb +87 -0
- data/lib/legion/extensions/agentic/inference/intuition/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/intuition.rb +20 -0
- data/lib/legion/extensions/agentic/inference/magnet/client.rb +29 -0
- data/lib/legion/extensions/agentic/inference/magnet/helpers/constants.rb +57 -0
- data/lib/legion/extensions/agentic/inference/magnet/helpers/field.rb +105 -0
- data/lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb +179 -0
- data/lib/legion/extensions/agentic/inference/magnet/helpers/pole.rb +80 -0
- data/lib/legion/extensions/agentic/inference/magnet/runners/cognitive_magnet.rb +124 -0
- data/lib/legion/extensions/agentic/inference/magnet/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/magnet.rb +21 -0
- data/lib/legion/extensions/agentic/inference/momentum/helpers/client.rb +17 -0
- data/lib/legion/extensions/agentic/inference/momentum/helpers/constants.rb +65 -0
- data/lib/legion/extensions/agentic/inference/momentum/helpers/idea.rb +112 -0
- data/lib/legion/extensions/agentic/inference/momentum/helpers/momentum_engine.rb +127 -0
- data/lib/legion/extensions/agentic/inference/momentum/runners/cognitive_momentum.rb +101 -0
- data/lib/legion/extensions/agentic/inference/momentum/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/momentum.rb +19 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/constants.rb +34 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_field.rb +154 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb +100 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference/runners/perceptual_inference.rb +120 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference.rb +19 -0
- data/lib/legion/extensions/agentic/inference/prediction/actors/expire_predictions.rb +45 -0
- data/lib/legion/extensions/agentic/inference/prediction/client.rb +27 -0
- data/lib/legion/extensions/agentic/inference/prediction/helpers/modes.rb +28 -0
- data/lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb +66 -0
- data/lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb +146 -0
- data/lib/legion/extensions/agentic/inference/prediction/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/prediction.rb +18 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding/helpers/constants.rb +46 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding/helpers/generative_model.rb +187 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding/helpers/prediction_error.rb +59 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb +171 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/predictive_coding.rb +20 -0
- data/lib/legion/extensions/agentic/inference/predictive_processing/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/predictive_processing/helpers/constants.rb +35 -0
- data/lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb +142 -0
- data/lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb +129 -0
- data/lib/legion/extensions/agentic/inference/predictive_processing/runners/predictive_processing.rb +104 -0
- data/lib/legion/extensions/agentic/inference/predictive_processing/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/predictive_processing.rb +19 -0
- data/lib/legion/extensions/agentic/inference/reality_testing/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb +98 -0
- data/lib/legion/extensions/agentic/inference/reality_testing/helpers/constants.rb +41 -0
- data/lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb +104 -0
- data/lib/legion/extensions/agentic/inference/reality_testing/runners/reality_testing.rb +94 -0
- data/lib/legion/extensions/agentic/inference/reality_testing/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/reality_testing.rb +19 -0
- data/lib/legion/extensions/agentic/inference/schema/client.rb +26 -0
- data/lib/legion/extensions/agentic/inference/schema/helpers/causal_relation.rb +70 -0
- data/lib/legion/extensions/agentic/inference/schema/helpers/constants.rb +50 -0
- data/lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb +173 -0
- data/lib/legion/extensions/agentic/inference/schema/runners/schema.rb +101 -0
- data/lib/legion/extensions/agentic/inference/schema/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/schema.rb +19 -0
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/client.rb +28 -0
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/constants.rb +42 -0
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/decision.rb +66 -0
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb +139 -0
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/runners/uncertainty_tolerance.rb +129 -0
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/version.rb +13 -0
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance.rb +19 -0
- data/lib/legion/extensions/agentic/inference/version.rb +11 -0
- data/lib/legion/extensions/agentic/inference.rb +44 -0
- data/spec/legion/extensions/agentic/inference/abductive/client_spec.rb +25 -0
- data/spec/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning_spec.rb +349 -0
- data/spec/legion/extensions/agentic/inference/affordance/client_spec.rb +26 -0
- data/spec/legion/extensions/agentic/inference/affordance/helpers/affordance_field_spec.rb +131 -0
- data/spec/legion/extensions/agentic/inference/affordance/helpers/affordance_item_spec.rb +107 -0
- data/spec/legion/extensions/agentic/inference/affordance/runners/affordance_spec.rb +78 -0
- data/spec/legion/extensions/agentic/inference/analogical/client_spec.rb +31 -0
- data/spec/legion/extensions/agentic/inference/analogical/helpers/analogy_engine_spec.rb +276 -0
- data/spec/legion/extensions/agentic/inference/analogical/helpers/structure_map_spec.rb +255 -0
- data/spec/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning_spec.rb +213 -0
- data/spec/legion/extensions/agentic/inference/argument_mapping/client_spec.rb +18 -0
- data/spec/legion/extensions/agentic/inference/argument_mapping/helpers/argument_engine_spec.rb +218 -0
- data/spec/legion/extensions/agentic/inference/argument_mapping/helpers/argument_spec.rb +231 -0
- data/spec/legion/extensions/agentic/inference/argument_mapping/runners/argument_mapping_spec.rb +171 -0
- data/spec/legion/extensions/agentic/inference/bayesian/client_spec.rb +20 -0
- data/spec/legion/extensions/agentic/inference/bayesian/helpers/belief_network_spec.rb +178 -0
- data/spec/legion/extensions/agentic/inference/bayesian/helpers/belief_spec.rb +137 -0
- data/spec/legion/extensions/agentic/inference/bayesian/runners/bayesian_belief_spec.rb +176 -0
- data/spec/legion/extensions/agentic/inference/belief_revision/client_spec.rb +31 -0
- data/spec/legion/extensions/agentic/inference/belief_revision/helpers/belief_network_spec.rb +176 -0
- data/spec/legion/extensions/agentic/inference/belief_revision/helpers/belief_spec.rb +153 -0
- data/spec/legion/extensions/agentic/inference/belief_revision/helpers/evidence_spec.rb +51 -0
- data/spec/legion/extensions/agentic/inference/belief_revision/runners/belief_revision_spec.rb +106 -0
- data/spec/legion/extensions/agentic/inference/causal_attribution/client_spec.rb +24 -0
- data/spec/legion/extensions/agentic/inference/causal_attribution/helpers/attribution_engine_spec.rb +181 -0
- data/spec/legion/extensions/agentic/inference/causal_attribution/helpers/attribution_spec.rb +108 -0
- data/spec/legion/extensions/agentic/inference/causal_attribution/runners/causal_attribution_spec.rb +142 -0
- data/spec/legion/extensions/agentic/inference/causal_reasoning/client_spec.rb +35 -0
- data/spec/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge_spec.rb +158 -0
- data/spec/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_graph_spec.rb +259 -0
- data/spec/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning_spec.rb +161 -0
- data/spec/legion/extensions/agentic/inference/coherence/client_spec.rb +17 -0
- data/spec/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence_spec.rb +267 -0
- data/spec/legion/extensions/agentic/inference/counterfactual/client_spec.rb +48 -0
- data/spec/legion/extensions/agentic/inference/counterfactual/helpers/constants_spec.rb +55 -0
- data/spec/legion/extensions/agentic/inference/counterfactual/helpers/counterfactual_engine_spec.rb +234 -0
- data/spec/legion/extensions/agentic/inference/counterfactual/helpers/scenario_spec.rb +193 -0
- data/spec/legion/extensions/agentic/inference/counterfactual/runners/counterfactual_spec.rb +179 -0
- data/spec/legion/extensions/agentic/inference/debugging/client_spec.rb +46 -0
- data/spec/legion/extensions/agentic/inference/debugging/helpers/causal_trace_spec.rb +84 -0
- data/spec/legion/extensions/agentic/inference/debugging/helpers/constants_spec.rb +97 -0
- data/spec/legion/extensions/agentic/inference/debugging/helpers/correction_spec.rb +98 -0
- data/spec/legion/extensions/agentic/inference/debugging/helpers/debugging_engine_spec.rb +290 -0
- data/spec/legion/extensions/agentic/inference/debugging/helpers/reasoning_error_spec.rb +164 -0
- data/spec/legion/extensions/agentic/inference/debugging/runners/cognitive_debugging_spec.rb +301 -0
- data/spec/legion/extensions/agentic/inference/enactive_cognition/client_spec.rb +19 -0
- data/spec/legion/extensions/agentic/inference/enactive_cognition/helpers/enaction_engine_spec.rb +181 -0
- data/spec/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop_spec.rb +184 -0
- data/spec/legion/extensions/agentic/inference/enactive_cognition/runners/enactive_cognition_spec.rb +214 -0
- data/spec/legion/extensions/agentic/inference/expectation_violation/expectation_violation_spec.rb +11 -0
- data/spec/legion/extensions/agentic/inference/expectation_violation/helpers/expectation_spec.rb +102 -0
- data/spec/legion/extensions/agentic/inference/expectation_violation/helpers/violation_engine_spec.rb +121 -0
- data/spec/legion/extensions/agentic/inference/expectation_violation/runners/expectation_violation_spec.rb +59 -0
- data/spec/legion/extensions/agentic/inference/free_energy/client_spec.rb +46 -0
- data/spec/legion/extensions/agentic/inference/free_energy/helpers/belief_spec.rb +183 -0
- data/spec/legion/extensions/agentic/inference/free_energy/helpers/free_energy_engine_spec.rb +211 -0
- data/spec/legion/extensions/agentic/inference/free_energy/runners/free_energy_spec.rb +118 -0
- data/spec/legion/extensions/agentic/inference/gravity/client_spec.rb +24 -0
- data/spec/legion/extensions/agentic/inference/gravity/helpers/attractor_spec.rb +143 -0
- data/spec/legion/extensions/agentic/inference/gravity/helpers/constants_spec.rb +107 -0
- data/spec/legion/extensions/agentic/inference/gravity/helpers/gravity_engine_spec.rb +193 -0
- data/spec/legion/extensions/agentic/inference/gravity/helpers/orbiting_thought_spec.rb +103 -0
- data/spec/legion/extensions/agentic/inference/gravity/runners/gravity_spec.rb +159 -0
- data/spec/legion/extensions/agentic/inference/horizon/client_spec.rb +58 -0
- data/spec/legion/extensions/agentic/inference/horizon/helpers/constants_spec.rb +98 -0
- data/spec/legion/extensions/agentic/inference/horizon/helpers/horizon_engine_spec.rb +325 -0
- data/spec/legion/extensions/agentic/inference/horizon/helpers/projection_spec.rb +155 -0
- data/spec/legion/extensions/agentic/inference/horizon/runners/cognitive_horizon_spec.rb +269 -0
- data/spec/legion/extensions/agentic/inference/hypothesis_testing/helpers/constants_spec.rb +38 -0
- data/spec/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine_spec.rb +182 -0
- data/spec/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_spec.rb +172 -0
- data/spec/legion/extensions/agentic/inference/hypothesis_testing/hypothesis_testing_spec.rb +16 -0
- data/spec/legion/extensions/agentic/inference/hypothesis_testing/runners/hypothesis_testing_spec.rb +159 -0
- data/spec/legion/extensions/agentic/inference/intuition/client_spec.rb +33 -0
- data/spec/legion/extensions/agentic/inference/intuition/helpers/heuristic_spec.rb +82 -0
- data/spec/legion/extensions/agentic/inference/intuition/helpers/intuition_engine_spec.rb +163 -0
- data/spec/legion/extensions/agentic/inference/intuition/helpers/pattern_spec.rb +160 -0
- data/spec/legion/extensions/agentic/inference/intuition/runners/intuition_spec.rb +107 -0
- data/spec/legion/extensions/agentic/inference/magnet/client_spec.rb +30 -0
- data/spec/legion/extensions/agentic/inference/magnet/helpers/constants_spec.rb +120 -0
- data/spec/legion/extensions/agentic/inference/magnet/helpers/field_spec.rb +193 -0
- data/spec/legion/extensions/agentic/inference/magnet/helpers/magnet_engine_spec.rb +281 -0
- data/spec/legion/extensions/agentic/inference/magnet/helpers/pole_spec.rb +211 -0
- data/spec/legion/extensions/agentic/inference/magnet/runners/cognitive_magnet_spec.rb +201 -0
- data/spec/legion/extensions/agentic/inference/momentum/cognitive_momentum_spec.rb +11 -0
- data/spec/legion/extensions/agentic/inference/momentum/helpers/idea_spec.rb +152 -0
- data/spec/legion/extensions/agentic/inference/momentum/helpers/momentum_engine_spec.rb +154 -0
- data/spec/legion/extensions/agentic/inference/momentum/runners/cognitive_momentum_spec.rb +97 -0
- data/spec/legion/extensions/agentic/inference/perceptual_inference/client_spec.rb +39 -0
- data/spec/legion/extensions/agentic/inference/perceptual_inference/helpers/constants_spec.rb +97 -0
- data/spec/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_field_spec.rb +270 -0
- data/spec/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis_spec.rb +206 -0
- data/spec/legion/extensions/agentic/inference/perceptual_inference/runners/perceptual_inference_spec.rb +305 -0
- data/spec/legion/extensions/agentic/inference/prediction/actors/expire_predictions_spec.rb +46 -0
- data/spec/legion/extensions/agentic/inference/prediction/client_spec.rb +14 -0
- data/spec/legion/extensions/agentic/inference/prediction/helpers/modes_spec.rb +118 -0
- data/spec/legion/extensions/agentic/inference/prediction/helpers/prediction_store_spec.rb +262 -0
- data/spec/legion/extensions/agentic/inference/prediction/runners/prediction_spec.rb +116 -0
- data/spec/legion/extensions/agentic/inference/predictive_coding/client_spec.rb +74 -0
- data/spec/legion/extensions/agentic/inference/predictive_coding/helpers/generative_model_spec.rb +194 -0
- data/spec/legion/extensions/agentic/inference/predictive_coding/helpers/prediction_error_spec.rb +109 -0
- data/spec/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding_spec.rb +210 -0
- data/spec/legion/extensions/agentic/inference/predictive_processing/client_spec.rb +82 -0
- data/spec/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model_spec.rb +220 -0
- data/spec/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor_spec.rb +206 -0
- data/spec/legion/extensions/agentic/inference/predictive_processing/runners/predictive_processing_spec.rb +213 -0
- data/spec/legion/extensions/agentic/inference/reality_testing/client_spec.rb +29 -0
- data/spec/legion/extensions/agentic/inference/reality_testing/helpers/belief_spec.rb +197 -0
- data/spec/legion/extensions/agentic/inference/reality_testing/helpers/constants_spec.rb +78 -0
- data/spec/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine_spec.rb +191 -0
- data/spec/legion/extensions/agentic/inference/reality_testing/runners/reality_testing_spec.rb +154 -0
- data/spec/legion/extensions/agentic/inference/schema/client_spec.rb +53 -0
- data/spec/legion/extensions/agentic/inference/schema/helpers/causal_relation_spec.rb +108 -0
- data/spec/legion/extensions/agentic/inference/schema/helpers/constants_spec.rb +54 -0
- data/spec/legion/extensions/agentic/inference/schema/helpers/world_model_spec.rb +179 -0
- data/spec/legion/extensions/agentic/inference/schema/runners/schema_spec.rb +146 -0
- data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/client_spec.rb +18 -0
- data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/constants_spec.rb +62 -0
- data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/decision_spec.rb +125 -0
- data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine_spec.rb +184 -0
- data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/runners/uncertainty_tolerance_spec.rb +157 -0
- data/spec/spec_helper.rb +46 -0
- metadata +412 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/inference/bayesian/helpers/constants'
|
|
4
|
+
require 'legion/extensions/agentic/inference/bayesian/helpers/belief'
|
|
5
|
+
require 'legion/extensions/agentic/inference/bayesian/helpers/belief_network'
|
|
6
|
+
require 'legion/extensions/agentic/inference/bayesian/runners/bayesian_belief'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module Agentic
|
|
11
|
+
module Inference
|
|
12
|
+
module Bayesian
|
|
13
|
+
class Client
|
|
14
|
+
include Runners::BayesianBelief
|
|
15
|
+
|
|
16
|
+
def initialize(**)
|
|
17
|
+
@belief_network = Helpers::BeliefNetwork.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :belief_network
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Inference
|
|
9
|
+
module Bayesian
|
|
10
|
+
module Helpers
|
|
11
|
+
class Belief
|
|
12
|
+
include Constants
|
|
13
|
+
|
|
14
|
+
attr_reader :id, :content, :domain, :prior, :posterior,
|
|
15
|
+
:evidence_history, :update_count, :created_at, :last_updated_at
|
|
16
|
+
|
|
17
|
+
def initialize(content:, domain:, prior: Constants::DEFAULT_PRIOR)
|
|
18
|
+
@id = SecureRandom.uuid
|
|
19
|
+
@content = content
|
|
20
|
+
@domain = domain
|
|
21
|
+
@prior = prior.clamp(Constants::PRIOR_FLOOR, Constants::PRIOR_CEILING)
|
|
22
|
+
@posterior = @prior
|
|
23
|
+
@evidence_history = []
|
|
24
|
+
@update_count = 0
|
|
25
|
+
@created_at = Time.now.utc
|
|
26
|
+
@last_updated_at = @created_at
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def update(likelihood:, evidence_id:)
|
|
30
|
+
clamped_likelihood = likelihood.clamp(Constants::LIKELIHOOD_FLOOR, Constants::LIKELIHOOD_CEILING)
|
|
31
|
+
marginal = (clamped_likelihood * @posterior) + ((1.0 - clamped_likelihood) * (1.0 - @posterior))
|
|
32
|
+
new_posterior = (clamped_likelihood * @posterior) / marginal
|
|
33
|
+
@posterior = new_posterior.clamp(Constants::PRIOR_FLOOR, Constants::PRIOR_CEILING)
|
|
34
|
+
@update_count += 1
|
|
35
|
+
@last_updated_at = Time.now.utc
|
|
36
|
+
|
|
37
|
+
@evidence_history << {
|
|
38
|
+
evidence_id: evidence_id,
|
|
39
|
+
likelihood: clamped_likelihood,
|
|
40
|
+
posterior_after: @posterior
|
|
41
|
+
}
|
|
42
|
+
@evidence_history.shift while @evidence_history.size > Constants::MAX_HISTORY
|
|
43
|
+
|
|
44
|
+
@posterior
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def log_odds
|
|
48
|
+
Math.log(@posterior / (1.0 - @posterior))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def confidence_label
|
|
52
|
+
Constants::CONFIDENCE_LABELS.each do |range, label|
|
|
53
|
+
return label if range.cover?(@posterior)
|
|
54
|
+
end
|
|
55
|
+
:unknown
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def surprise(observation_likelihood:)
|
|
59
|
+
clamped = observation_likelihood.clamp(Constants::LIKELIHOOD_FLOOR, 1.0)
|
|
60
|
+
-Math.log2(clamped)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def reset_to_prior!
|
|
64
|
+
@posterior = @prior
|
|
65
|
+
@update_count = 0
|
|
66
|
+
@evidence_history = []
|
|
67
|
+
@last_updated_at = Time.now.utc
|
|
68
|
+
@posterior
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def stale?(threshold: Constants::STALE_THRESHOLD)
|
|
72
|
+
(Time.now.utc - @last_updated_at) > threshold
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def to_h
|
|
76
|
+
{
|
|
77
|
+
id: @id,
|
|
78
|
+
content: @content,
|
|
79
|
+
domain: @domain,
|
|
80
|
+
prior: @prior,
|
|
81
|
+
posterior: @posterior,
|
|
82
|
+
confidence_label: confidence_label,
|
|
83
|
+
log_odds: log_odds,
|
|
84
|
+
update_count: @update_count,
|
|
85
|
+
evidence_history: @evidence_history,
|
|
86
|
+
created_at: @created_at,
|
|
87
|
+
last_updated_at: @last_updated_at
|
|
88
|
+
}
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module Bayesian
|
|
8
|
+
module Helpers
|
|
9
|
+
class BeliefNetwork
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
attr_reader :beliefs
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@beliefs = {}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def add_belief(content:, domain:, prior: Constants::DEFAULT_PRIOR)
|
|
19
|
+
return nil if @beliefs.size >= Constants::MAX_HYPOTHESES
|
|
20
|
+
|
|
21
|
+
belief = Belief.new(content: content, domain: domain, prior: prior)
|
|
22
|
+
@beliefs[belief.id] = belief
|
|
23
|
+
belief
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def update_belief(belief_id:, evidence_id:, likelihood:)
|
|
27
|
+
belief = @beliefs[belief_id]
|
|
28
|
+
return nil unless belief
|
|
29
|
+
|
|
30
|
+
belief.update(likelihood: likelihood, evidence_id: evidence_id)
|
|
31
|
+
belief
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def batch_update(evidence_id:, likelihoods:)
|
|
35
|
+
return {} if likelihoods.empty?
|
|
36
|
+
|
|
37
|
+
updated = {}
|
|
38
|
+
likelihoods.each do |belief_id, likelihood|
|
|
39
|
+
belief = update_belief(belief_id: belief_id, evidence_id: evidence_id, likelihood: likelihood)
|
|
40
|
+
updated[belief_id] = belief.posterior if belief
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
normalize_posteriors(updated.keys)
|
|
44
|
+
updated
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def most_probable(domain: nil, limit: 5)
|
|
48
|
+
filtered(domain).sort_by { |b| -b.posterior }.first(limit)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def least_probable(domain: nil, limit: 5)
|
|
52
|
+
filtered(domain).sort_by(&:posterior).first(limit)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def by_domain(domain:)
|
|
56
|
+
@beliefs.values.select { |b| b.domain == domain }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def posterior_distribution(domain: nil)
|
|
60
|
+
subset = filtered(domain)
|
|
61
|
+
total = subset.sum(&:posterior)
|
|
62
|
+
return {} if total.zero?
|
|
63
|
+
|
|
64
|
+
subset.to_h { |b| [b.id, b.posterior / total] }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def information_gain(belief_id:, evidence_id:, likelihood:) # rubocop:disable Lint/UnusedMethodArgument
|
|
68
|
+
belief = @beliefs[belief_id]
|
|
69
|
+
return 0.0 unless belief
|
|
70
|
+
|
|
71
|
+
prior_p = belief.posterior
|
|
72
|
+
clamped = likelihood.clamp(Constants::LIKELIHOOD_FLOOR, Constants::LIKELIHOOD_CEILING)
|
|
73
|
+
marginal = (clamped * prior_p) + ((1.0 - clamped) * (1.0 - prior_p))
|
|
74
|
+
post_p = (clamped * prior_p) / marginal
|
|
75
|
+
post_p = post_p.clamp(Constants::PRIOR_FLOOR, Constants::PRIOR_CEILING)
|
|
76
|
+
|
|
77
|
+
kl_divergence(prior_p, post_p)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def entropy(domain: nil)
|
|
81
|
+
dist = posterior_distribution(domain: domain)
|
|
82
|
+
return 0.0 if dist.empty?
|
|
83
|
+
|
|
84
|
+
-dist.values.sum do |prob|
|
|
85
|
+
next 0.0 if prob <= 0.0
|
|
86
|
+
|
|
87
|
+
prob * Math.log2(prob)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def decay_all
|
|
92
|
+
@beliefs.each_value do |belief|
|
|
93
|
+
shift = (belief.posterior - belief.prior) * Constants::DECAY_RATE
|
|
94
|
+
new_posterior = belief.posterior - shift
|
|
95
|
+
belief.instance_variable_set(:@posterior, new_posterior.clamp(Constants::PRIOR_FLOOR, Constants::PRIOR_CEILING))
|
|
96
|
+
belief.instance_variable_set(:@last_updated_at, Time.now.utc)
|
|
97
|
+
end
|
|
98
|
+
@beliefs.size
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def count
|
|
102
|
+
@beliefs.size
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def to_h
|
|
106
|
+
{
|
|
107
|
+
belief_count: @beliefs.size,
|
|
108
|
+
beliefs: @beliefs.transform_values(&:to_h)
|
|
109
|
+
}
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
def filtered(domain)
|
|
115
|
+
return @beliefs.values if domain.nil?
|
|
116
|
+
|
|
117
|
+
by_domain(domain: domain)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def normalize_posteriors(belief_ids)
|
|
121
|
+
subset = belief_ids.filter_map { |bid| @beliefs[bid] }
|
|
122
|
+
total = subset.sum(&:posterior)
|
|
123
|
+
return if total.zero?
|
|
124
|
+
|
|
125
|
+
subset.each do |belief|
|
|
126
|
+
normalized = (belief.posterior / total).clamp(Constants::PRIOR_FLOOR, Constants::PRIOR_CEILING)
|
|
127
|
+
belief.instance_variable_set(:@posterior, normalized)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def kl_divergence(prior_p, post_p)
|
|
132
|
+
return 0.0 if prior_p <= 0.0 || post_p <= 0.0
|
|
133
|
+
|
|
134
|
+
prior_q = 1.0 - prior_p
|
|
135
|
+
post_q = 1.0 - post_p
|
|
136
|
+
|
|
137
|
+
term1 = post_p * Math.log2(post_p / prior_p)
|
|
138
|
+
term2 = post_q * Math.log2(post_q / prior_q)
|
|
139
|
+
term1 + term2
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module Bayesian
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
MAX_HYPOTHESES = 200
|
|
11
|
+
MAX_EVIDENCE = 500
|
|
12
|
+
MAX_HISTORY = 300
|
|
13
|
+
|
|
14
|
+
DEFAULT_PRIOR = 0.5
|
|
15
|
+
PRIOR_FLOOR = 0.001
|
|
16
|
+
PRIOR_CEILING = 0.999
|
|
17
|
+
LIKELIHOOD_FLOOR = 0.001
|
|
18
|
+
LIKELIHOOD_CEILING = 0.999
|
|
19
|
+
|
|
20
|
+
DECAY_RATE = 0.01
|
|
21
|
+
STALE_THRESHOLD = 120
|
|
22
|
+
|
|
23
|
+
CONFIDENCE_LABELS = {
|
|
24
|
+
(0.9..) => :certain,
|
|
25
|
+
(0.7...0.9) => :confident,
|
|
26
|
+
(0.5...0.7) => :leaning,
|
|
27
|
+
(0.3...0.5) => :uncertain,
|
|
28
|
+
(..0.3) => :doubtful
|
|
29
|
+
}.freeze
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module Bayesian
|
|
8
|
+
module Runners
|
|
9
|
+
module BayesianBelief
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def add_bayesian_belief(content:, domain:, prior: nil, **)
|
|
14
|
+
pri = (prior || Helpers::Constants::DEFAULT_PRIOR).clamp(
|
|
15
|
+
Helpers::Constants::PRIOR_FLOOR,
|
|
16
|
+
Helpers::Constants::PRIOR_CEILING
|
|
17
|
+
)
|
|
18
|
+
belief = belief_network.add_belief(content: content, domain: domain, prior: pri)
|
|
19
|
+
unless belief
|
|
20
|
+
Legion::Logging.warn "[bayesian_belief] add failed: network at capacity (#{Helpers::Constants::MAX_HYPOTHESES})"
|
|
21
|
+
return { success: false, reason: :capacity_exceeded, max: Helpers::Constants::MAX_HYPOTHESES }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
Legion::Logging.debug "[bayesian_belief] add: id=#{belief.id[0..7]} domain=#{domain} prior=#{pri.round(3)}"
|
|
25
|
+
{ success: true, belief_id: belief.id, domain: domain, prior: belief.prior, posterior: belief.posterior }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def update_bayesian_belief(belief_id:, evidence_id:, likelihood:, **)
|
|
29
|
+
clamped = likelihood.clamp(Helpers::Constants::LIKELIHOOD_FLOOR, Helpers::Constants::LIKELIHOOD_CEILING)
|
|
30
|
+
belief = belief_network.update_belief(belief_id: belief_id, evidence_id: evidence_id, likelihood: clamped)
|
|
31
|
+
unless belief
|
|
32
|
+
Legion::Logging.debug "[bayesian_belief] update failed: belief_id=#{belief_id} not found"
|
|
33
|
+
return { success: false, reason: :not_found, belief_id: belief_id }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Legion::Logging.debug "[bayesian_belief] update: id=#{belief_id[0..7]} evidence=#{evidence_id} " \
|
|
37
|
+
"likelihood=#{clamped.round(3)} posterior=#{belief.posterior.round(3)}"
|
|
38
|
+
{
|
|
39
|
+
success: true,
|
|
40
|
+
belief_id: belief_id,
|
|
41
|
+
evidence_id: evidence_id,
|
|
42
|
+
likelihood: clamped,
|
|
43
|
+
posterior: belief.posterior,
|
|
44
|
+
confidence_label: belief.confidence_label,
|
|
45
|
+
update_count: belief.update_count
|
|
46
|
+
}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def batch_bayesian_update(evidence_id:, likelihoods:, **)
|
|
50
|
+
if likelihoods.nil? || likelihoods.empty?
|
|
51
|
+
Legion::Logging.debug '[bayesian_belief] batch_update: empty likelihoods, skipping'
|
|
52
|
+
return { success: true, updated: 0, posteriors: {} }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
posteriors = belief_network.batch_update(evidence_id: evidence_id, likelihoods: likelihoods)
|
|
56
|
+
Legion::Logging.debug "[bayesian_belief] batch_update: evidence=#{evidence_id} updated=#{posteriors.size}"
|
|
57
|
+
{ success: true, evidence_id: evidence_id, updated: posteriors.size, posteriors: posteriors }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def most_probable_beliefs(domain: nil, limit: 5, **)
|
|
61
|
+
beliefs = belief_network.most_probable(domain: domain, limit: limit)
|
|
62
|
+
Legion::Logging.debug "[bayesian_belief] most_probable: domain=#{domain.inspect} count=#{beliefs.size}"
|
|
63
|
+
{ success: true, beliefs: beliefs.map(&:to_h), count: beliefs.size }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def least_probable_beliefs(domain: nil, limit: 5, **)
|
|
67
|
+
beliefs = belief_network.least_probable(domain: domain, limit: limit)
|
|
68
|
+
Legion::Logging.debug "[bayesian_belief] least_probable: domain=#{domain.inspect} count=#{beliefs.size}"
|
|
69
|
+
{ success: true, beliefs: beliefs.map(&:to_h), count: beliefs.size }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def posterior_distribution(domain: nil, **)
|
|
73
|
+
dist = belief_network.posterior_distribution(domain: domain)
|
|
74
|
+
Legion::Logging.debug "[bayesian_belief] posterior_distribution: domain=#{domain.inspect} size=#{dist.size}"
|
|
75
|
+
{ success: true, distribution: dist, size: dist.size }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def information_gain(belief_id:, evidence_id:, likelihood:, **)
|
|
79
|
+
clamped = likelihood.clamp(Helpers::Constants::LIKELIHOOD_FLOOR, Helpers::Constants::LIKELIHOOD_CEILING)
|
|
80
|
+
gain = belief_network.information_gain(belief_id: belief_id, evidence_id: evidence_id, likelihood: clamped)
|
|
81
|
+
Legion::Logging.debug "[bayesian_belief] information_gain: id=#{belief_id[0..7]} likelihood=#{clamped.round(3)} gain=#{gain.round(4)}"
|
|
82
|
+
{ success: true, belief_id: belief_id, evidence_id: evidence_id, likelihood: clamped, information_gain: gain }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def belief_entropy(domain: nil, **)
|
|
86
|
+
ent = belief_network.entropy(domain: domain)
|
|
87
|
+
Legion::Logging.debug "[bayesian_belief] entropy: domain=#{domain.inspect} entropy=#{ent.round(4)}"
|
|
88
|
+
{ success: true, domain: domain, entropy: ent }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def update_bayesian_beliefs(**)
|
|
92
|
+
decayed = belief_network.decay_all
|
|
93
|
+
Legion::Logging.debug "[bayesian_belief] decay cycle: beliefs_updated=#{decayed}"
|
|
94
|
+
{ success: true, decayed: decayed }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def bayesian_belief_stats(**)
|
|
98
|
+
total = belief_network.count
|
|
99
|
+
ent = belief_network.entropy
|
|
100
|
+
most = belief_network.most_probable(limit: 1).first
|
|
101
|
+
least = belief_network.least_probable(limit: 1).first
|
|
102
|
+
|
|
103
|
+
Legion::Logging.debug "[bayesian_belief] stats: total=#{total} entropy=#{ent.round(4)}"
|
|
104
|
+
{
|
|
105
|
+
success: true,
|
|
106
|
+
total_beliefs: total,
|
|
107
|
+
entropy: ent,
|
|
108
|
+
most_probable: most&.to_h,
|
|
109
|
+
least_probable: least&.to_h,
|
|
110
|
+
capacity: Helpers::Constants::MAX_HYPOTHESES
|
|
111
|
+
}
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def belief_network
|
|
117
|
+
@belief_network ||= Helpers::BeliefNetwork.new
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/inference/bayesian/version'
|
|
4
|
+
require 'legion/extensions/agentic/inference/bayesian/helpers/constants'
|
|
5
|
+
require 'legion/extensions/agentic/inference/bayesian/helpers/belief'
|
|
6
|
+
require 'legion/extensions/agentic/inference/bayesian/helpers/belief_network'
|
|
7
|
+
require 'legion/extensions/agentic/inference/bayesian/runners/bayesian_belief'
|
|
8
|
+
require 'legion/extensions/agentic/inference/bayesian/client'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module Agentic
|
|
13
|
+
module Inference
|
|
14
|
+
module Bayesian
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module BeliefRevision
|
|
8
|
+
class Client
|
|
9
|
+
include Runners::BeliefRevision
|
|
10
|
+
|
|
11
|
+
def initialize(network: nil)
|
|
12
|
+
@network = network || Helpers::BeliefNetwork.new
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module BeliefRevision
|
|
8
|
+
module Helpers
|
|
9
|
+
class Belief
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
attr_reader :id, :proposition, :domain, :credence, :evidence_for, :evidence_against, :entrenchment,
|
|
13
|
+
:created_at, :updated_at, :revision_count
|
|
14
|
+
|
|
15
|
+
def initialize(id:, proposition:, domain: :general, credence: DEFAULT_CREDENCE)
|
|
16
|
+
@id = id
|
|
17
|
+
@proposition = proposition
|
|
18
|
+
@domain = domain
|
|
19
|
+
@credence = credence.to_f.clamp(CREDENCE_FLOOR, CREDENCE_CEILING)
|
|
20
|
+
@evidence_for = []
|
|
21
|
+
@evidence_against = []
|
|
22
|
+
@entrenchment = 0.0
|
|
23
|
+
@revision_count = 0
|
|
24
|
+
@created_at = Time.now.utc
|
|
25
|
+
@updated_at = @created_at
|
|
26
|
+
@protected = false
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def add_supporting_evidence(evidence)
|
|
30
|
+
return nil if @evidence_for.size >= MAX_EVIDENCE_PER_BELIEF
|
|
31
|
+
|
|
32
|
+
@evidence_for << evidence
|
|
33
|
+
update_credence_from_evidence(evidence, :support)
|
|
34
|
+
evidence
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def add_opposing_evidence(evidence)
|
|
38
|
+
return nil if @evidence_against.size >= MAX_EVIDENCE_PER_BELIEF
|
|
39
|
+
|
|
40
|
+
@evidence_against << evidence
|
|
41
|
+
update_credence_from_evidence(evidence, :oppose)
|
|
42
|
+
evidence
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def revise(new_credence:)
|
|
46
|
+
@credence = new_credence.to_f.clamp(CREDENCE_FLOOR, CREDENCE_CEILING)
|
|
47
|
+
@revision_count += 1
|
|
48
|
+
@updated_at = Time.now.utc
|
|
49
|
+
deepen_entrenchment
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def protect!
|
|
53
|
+
@protected = true
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def unprotect!
|
|
57
|
+
@protected = false
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def protected?
|
|
61
|
+
@protected
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def state
|
|
65
|
+
return :protected if @protected
|
|
66
|
+
return :entrenched if @entrenchment >= STATE_THRESHOLDS[:entrenched]
|
|
67
|
+
return :held if @credence >= STATE_THRESHOLDS[:held]
|
|
68
|
+
|
|
69
|
+
:tentative
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def credence_label
|
|
73
|
+
CREDENCE_LABELS.each { |range, lbl| return lbl if range.cover?(@credence) }
|
|
74
|
+
:disbelieved
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def believed?
|
|
78
|
+
@credence >= 0.5
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def disbelieved?
|
|
82
|
+
@credence < 0.3
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def contradicts?(other)
|
|
86
|
+
(believed? && other.disbelieved?) || (disbelieved? && other.believed?)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def evidence_ratio
|
|
90
|
+
total = @evidence_for.size + @evidence_against.size
|
|
91
|
+
return 0.5 if total.zero?
|
|
92
|
+
|
|
93
|
+
@evidence_for.size.to_f / total
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def total_evidence_count
|
|
97
|
+
@evidence_for.size + @evidence_against.size
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def decay
|
|
101
|
+
return if @protected
|
|
102
|
+
return if @entrenchment >= STATE_THRESHOLDS[:entrenched]
|
|
103
|
+
|
|
104
|
+
@credence = approach_default(@credence)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def to_h
|
|
108
|
+
{
|
|
109
|
+
id: @id,
|
|
110
|
+
proposition: @proposition,
|
|
111
|
+
domain: @domain,
|
|
112
|
+
credence: @credence.round(4),
|
|
113
|
+
credence_label: credence_label,
|
|
114
|
+
state: state,
|
|
115
|
+
evidence_for: @evidence_for.size,
|
|
116
|
+
evidence_against: @evidence_against.size,
|
|
117
|
+
entrenchment: @entrenchment.round(4),
|
|
118
|
+
revision_count: @revision_count,
|
|
119
|
+
protected: @protected
|
|
120
|
+
}
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
private
|
|
124
|
+
|
|
125
|
+
def update_credence_from_evidence(evidence, direction)
|
|
126
|
+
return if @protected
|
|
127
|
+
|
|
128
|
+
adjustment = evidence.weight * EVIDENCE_WEIGHT
|
|
129
|
+
resistance = @entrenchment * 0.5
|
|
130
|
+
effective = adjustment * (1.0 - resistance)
|
|
131
|
+
|
|
132
|
+
@credence = if direction == :support
|
|
133
|
+
[@credence + effective, CREDENCE_CEILING].min
|
|
134
|
+
else
|
|
135
|
+
[@credence - effective, CREDENCE_FLOOR].max
|
|
136
|
+
end
|
|
137
|
+
@updated_at = Time.now.utc
|
|
138
|
+
deepen_entrenchment
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def deepen_entrenchment
|
|
142
|
+
@entrenchment = [@entrenchment + ENTRENCHMENT_ALPHA, 1.0].min
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def approach_default(value)
|
|
146
|
+
diff = DEFAULT_CREDENCE - value
|
|
147
|
+
value + (diff * DECAY_RATE)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|