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,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module Momentum
|
|
8
|
+
module Helpers
|
|
9
|
+
class MomentumEngine
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
attr_reader :history
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@ideas = {}
|
|
16
|
+
@history = []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def create_idea(content:, idea_type:, domain:, mass: DEFAULT_MASS)
|
|
20
|
+
evict_oldest if @ideas.size >= MAX_IDEAS
|
|
21
|
+
|
|
22
|
+
idea = Idea.new(content: content, idea_type: idea_type, domain: domain, mass: mass)
|
|
23
|
+
@ideas[idea.id] = idea
|
|
24
|
+
record_history(:created, idea.id)
|
|
25
|
+
idea
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def reinforce_idea(idea_id:)
|
|
29
|
+
idea = @ideas[idea_id]
|
|
30
|
+
return { success: false, reason: :not_found } unless idea
|
|
31
|
+
|
|
32
|
+
idea.reinforce!
|
|
33
|
+
record_history(:reinforced, idea_id)
|
|
34
|
+
{ success: true, momentum: idea.momentum, mass: idea.mass, velocity: idea.velocity }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def challenge_idea(idea_id:)
|
|
38
|
+
idea = @ideas[idea_id]
|
|
39
|
+
return { success: false, reason: :not_found } unless idea
|
|
40
|
+
|
|
41
|
+
idea.challenge!
|
|
42
|
+
record_history(:challenged, idea_id)
|
|
43
|
+
{ success: true, momentum: idea.momentum, mass: idea.mass, velocity: idea.velocity }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def apply_force(idea_id:, force:)
|
|
47
|
+
idea = @ideas[idea_id]
|
|
48
|
+
return { success: false, reason: :not_found } unless idea
|
|
49
|
+
|
|
50
|
+
idea.apply_force(force: force)
|
|
51
|
+
record_history(:force_applied, idea_id)
|
|
52
|
+
{ success: true, momentum: idea.momentum, velocity: idea.velocity }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def surging_ideas
|
|
56
|
+
@ideas.values.select(&:surging?)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def reversing_ideas
|
|
60
|
+
@ideas.values.select(&:reversing?)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def entrenched_ideas
|
|
64
|
+
@ideas.values.select(&:entrenched?)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def ideas_by_type(idea_type:)
|
|
68
|
+
@ideas.values.select { |idea| idea.idea_type == idea_type }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def ideas_by_domain(domain:)
|
|
72
|
+
@ideas.values.select { |idea| idea.domain == domain }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def highest_momentum(limit: 5)
|
|
76
|
+
@ideas.values.sort_by { |idea| -idea.momentum }.first(limit)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def most_entrenched(limit: 5)
|
|
80
|
+
@ideas.values.sort_by { |idea| -idea.mass }.first(limit)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def apply_friction_all
|
|
84
|
+
@ideas.each_value(&:apply_friction!)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def prune_at_rest
|
|
88
|
+
ids = @ideas.select { |_id, idea| idea.at_rest? && idea.mass <= MASS_FLOOR + 0.01 }.keys
|
|
89
|
+
ids.each { |idea_id| @ideas.delete(idea_id) }
|
|
90
|
+
ids.size
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def to_h
|
|
94
|
+
{
|
|
95
|
+
total_ideas: @ideas.size,
|
|
96
|
+
surging_count: surging_ideas.size,
|
|
97
|
+
reversing_count: reversing_ideas.size,
|
|
98
|
+
entrenched_count: entrenched_ideas.size,
|
|
99
|
+
avg_momentum: avg_momentum,
|
|
100
|
+
history_count: @history.size
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
private
|
|
105
|
+
|
|
106
|
+
def avg_momentum
|
|
107
|
+
return 0.0 if @ideas.empty?
|
|
108
|
+
|
|
109
|
+
@ideas.values.sum(&:momentum) / @ideas.size
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def evict_oldest
|
|
113
|
+
oldest_id = @ideas.min_by { |_id, idea| idea.last_updated_at }&.first
|
|
114
|
+
@ideas.delete(oldest_id) if oldest_id
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def record_history(event, idea_id)
|
|
118
|
+
@history << { event: event, idea_id: idea_id, at: Time.now.utc }
|
|
119
|
+
@history.shift while @history.size > MAX_HISTORY
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module Momentum
|
|
8
|
+
module Runners
|
|
9
|
+
module CognitiveMomentum
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def create_cognitive_idea(content:, idea_type:, domain:, mass: nil, **)
|
|
14
|
+
idea = engine.create_idea(
|
|
15
|
+
content: content,
|
|
16
|
+
idea_type: idea_type.to_sym,
|
|
17
|
+
domain: domain.to_sym,
|
|
18
|
+
mass: mass || Helpers::Constants::DEFAULT_MASS
|
|
19
|
+
)
|
|
20
|
+
Legion::Logging.debug "[cognitive_momentum] create id=#{idea.id[0..7]} " \
|
|
21
|
+
"type=#{idea_type} domain=#{domain}"
|
|
22
|
+
{ success: true, idea: idea.to_h }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def reinforce_cognitive_idea(idea_id:, **)
|
|
26
|
+
result = engine.reinforce_idea(idea_id: idea_id)
|
|
27
|
+
Legion::Logging.debug "[cognitive_momentum] reinforce id=#{idea_id[0..7]} " \
|
|
28
|
+
"momentum=#{result[:momentum]&.round(3)}"
|
|
29
|
+
result
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def challenge_cognitive_idea(idea_id:, **)
|
|
33
|
+
result = engine.challenge_idea(idea_id: idea_id)
|
|
34
|
+
Legion::Logging.debug "[cognitive_momentum] challenge id=#{idea_id[0..7]} " \
|
|
35
|
+
"momentum=#{result[:momentum]&.round(3)}"
|
|
36
|
+
result
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def apply_cognitive_force(idea_id:, force:, **)
|
|
40
|
+
result = engine.apply_force(idea_id: idea_id, force: force)
|
|
41
|
+
Legion::Logging.debug "[cognitive_momentum] force id=#{idea_id[0..7]} " \
|
|
42
|
+
"f=#{force} momentum=#{result[:momentum]&.round(3)}"
|
|
43
|
+
result
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def surging_ideas_report(**)
|
|
47
|
+
ideas = engine.surging_ideas
|
|
48
|
+
Legion::Logging.debug "[cognitive_momentum] surging count=#{ideas.size}"
|
|
49
|
+
{ success: true, ideas: ideas.map(&:to_h), count: ideas.size }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def reversing_ideas_report(**)
|
|
53
|
+
ideas = engine.reversing_ideas
|
|
54
|
+
Legion::Logging.debug "[cognitive_momentum] reversing count=#{ideas.size}"
|
|
55
|
+
{ success: true, ideas: ideas.map(&:to_h), count: ideas.size }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def entrenched_ideas_report(**)
|
|
59
|
+
ideas = engine.entrenched_ideas
|
|
60
|
+
Legion::Logging.debug "[cognitive_momentum] entrenched count=#{ideas.size}"
|
|
61
|
+
{ success: true, ideas: ideas.map(&:to_h), count: ideas.size }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def highest_momentum_ideas(limit: 5, **)
|
|
65
|
+
ideas = engine.highest_momentum(limit: limit)
|
|
66
|
+
Legion::Logging.debug "[cognitive_momentum] highest_momentum count=#{ideas.size}"
|
|
67
|
+
{ success: true, ideas: ideas.map(&:to_h), count: ideas.size }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def ideas_by_domain(domain:, **)
|
|
71
|
+
ideas = engine.ideas_by_domain(domain: domain.to_sym)
|
|
72
|
+
Legion::Logging.debug '[cognitive_momentum] by_domain ' \
|
|
73
|
+
"domain=#{domain} count=#{ideas.size}"
|
|
74
|
+
{ success: true, ideas: ideas.map(&:to_h), count: ideas.size }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def update_cognitive_momentum(**)
|
|
78
|
+
engine.apply_friction_all
|
|
79
|
+
pruned = engine.prune_at_rest
|
|
80
|
+
Legion::Logging.debug "[cognitive_momentum] friction+prune pruned=#{pruned}"
|
|
81
|
+
{ success: true, pruned: pruned }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def cognitive_momentum_stats(**)
|
|
85
|
+
stats = engine.to_h
|
|
86
|
+
Legion::Logging.debug "[cognitive_momentum] stats total=#{stats[:total_ideas]}"
|
|
87
|
+
{ success: true }.merge(stats)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def engine
|
|
93
|
+
@engine ||= Helpers::MomentumEngine.new
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'momentum/version'
|
|
4
|
+
require_relative 'momentum/helpers/constants'
|
|
5
|
+
require_relative 'momentum/helpers/idea'
|
|
6
|
+
require_relative 'momentum/helpers/momentum_engine'
|
|
7
|
+
require_relative 'momentum/runners/cognitive_momentum'
|
|
8
|
+
require_relative 'momentum/helpers/client'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module Agentic
|
|
13
|
+
module Inference
|
|
14
|
+
module Momentum
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/inference/perceptual_inference/helpers/constants'
|
|
4
|
+
require 'legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis'
|
|
5
|
+
require 'legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_field'
|
|
6
|
+
require 'legion/extensions/agentic/inference/perceptual_inference/runners/perceptual_inference'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module Agentic
|
|
11
|
+
module Inference
|
|
12
|
+
module PerceptualInference
|
|
13
|
+
class Client
|
|
14
|
+
include Runners::PerceptualInference
|
|
15
|
+
|
|
16
|
+
def initialize(field: nil, **)
|
|
17
|
+
@field = field || Helpers::PerceptualField.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :field
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module PerceptualInference
|
|
8
|
+
module Helpers
|
|
9
|
+
MAX_HYPOTHESES = 30
|
|
10
|
+
MAX_EVIDENCE = 100
|
|
11
|
+
MAX_HISTORY = 200
|
|
12
|
+
DEFAULT_PRIOR = 0.5
|
|
13
|
+
PRIOR_FLOOR = 0.01
|
|
14
|
+
PRIOR_CEILING = 0.99
|
|
15
|
+
SELECTION_THRESHOLD = 0.6
|
|
16
|
+
RIVALRY_MARGIN = 0.1
|
|
17
|
+
EVIDENCE_STRENGTH_FLOOR = 0.05
|
|
18
|
+
ADAPTATION_RATE = 0.1
|
|
19
|
+
DECAY_RATE = 0.01
|
|
20
|
+
MODALITIES = %i[visual auditory somatosensory olfactory gustatory proprioceptive vestibular].freeze
|
|
21
|
+
HYPOTHESIS_STATES = %i[active selected suppressed decayed].freeze
|
|
22
|
+
PERCEPT_LABELS = {
|
|
23
|
+
(0.8..) => :vivid,
|
|
24
|
+
(0.6...0.8) => :clear,
|
|
25
|
+
(0.4...0.6) => :ambiguous,
|
|
26
|
+
(0.2...0.4) => :faint,
|
|
27
|
+
(..0.2) => :subliminal
|
|
28
|
+
}.freeze
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module PerceptualInference
|
|
8
|
+
module Helpers
|
|
9
|
+
class PerceptualField
|
|
10
|
+
attr_reader :hypotheses, :evidence_log
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@hypotheses = {}
|
|
14
|
+
@evidence_log = []
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def register_hypothesis(content:, modality:, domain: :general, prior: DEFAULT_PRIOR)
|
|
18
|
+
validate_modality!(modality)
|
|
19
|
+
prune_modality(modality) if hypotheses_for(modality).size >= MAX_HYPOTHESES
|
|
20
|
+
|
|
21
|
+
hypothesis = PerceptualHypothesis.new(
|
|
22
|
+
content: content,
|
|
23
|
+
modality: modality,
|
|
24
|
+
domain: domain,
|
|
25
|
+
prior: prior
|
|
26
|
+
)
|
|
27
|
+
@hypotheses[hypothesis.id] = hypothesis
|
|
28
|
+
hypothesis
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def present_evidence(modality:, content:, strength: 0.5)
|
|
32
|
+
validate_modality!(modality)
|
|
33
|
+
strength = [strength.to_f, EVIDENCE_STRENGTH_FLOOR].max
|
|
34
|
+
record_evidence(modality, content, strength)
|
|
35
|
+
|
|
36
|
+
active_hypotheses_for(modality).each do |h|
|
|
37
|
+
h.likelihood = compute_likelihood(h, content, strength)
|
|
38
|
+
h.compute_posterior(evidence_weight: strength)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
hypotheses_for(modality).size
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def select_percept(modality:)
|
|
45
|
+
validate_modality!(modality)
|
|
46
|
+
candidates = active_hypotheses_for(modality)
|
|
47
|
+
return nil if candidates.empty?
|
|
48
|
+
|
|
49
|
+
winner = candidates.max_by(&:posterior)
|
|
50
|
+
return nil if winner.posterior < SELECTION_THRESHOLD
|
|
51
|
+
|
|
52
|
+
candidates.each { |h| h.id == winner.id ? h.select! : h.suppress! }
|
|
53
|
+
winner
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def rivalry?(modality:)
|
|
57
|
+
validate_modality!(modality)
|
|
58
|
+
candidates = active_hypotheses_for(modality)
|
|
59
|
+
return false if candidates.size < 2
|
|
60
|
+
|
|
61
|
+
top_two = candidates.max_by(2, &:posterior)
|
|
62
|
+
top_two.first.rival_with?(top_two.last)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def current_percept(modality:)
|
|
66
|
+
validate_modality!(modality)
|
|
67
|
+
@hypotheses.values.find { |h| h.modality == modality && h.state == :selected }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def hypotheses_for(modality)
|
|
71
|
+
@hypotheses.values.select { |h| h.modality == modality }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def suppress_hypothesis(hypothesis_id:)
|
|
75
|
+
h = @hypotheses[hypothesis_id]
|
|
76
|
+
return false unless h
|
|
77
|
+
|
|
78
|
+
h.suppress!
|
|
79
|
+
true
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def adapt_priors(modality:, correct_hypothesis_id:)
|
|
83
|
+
validate_modality!(modality)
|
|
84
|
+
hypotheses_for(modality).each do |h|
|
|
85
|
+
outcome = h.id == correct_hypothesis_id ? :correct : :incorrect
|
|
86
|
+
h.adapt_prior(outcome: outcome)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def ambiguity_level
|
|
91
|
+
rival_count = MODALITIES.count { |m| rivalry?(modality: m) && hypotheses_for(m).any? }
|
|
92
|
+
total_active = MODALITIES.count { |m| hypotheses_for(m).any? }
|
|
93
|
+
return 0.0 if total_active.zero?
|
|
94
|
+
|
|
95
|
+
rival_count.to_f / total_active
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def decay_all
|
|
99
|
+
@hypotheses.each_value(&:decay)
|
|
100
|
+
@hypotheses.reject! { |_, h| h.state == :decayed }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def to_h
|
|
104
|
+
by_modality = MODALITIES.each_with_object({}) do |m, acc|
|
|
105
|
+
hs = hypotheses_for(m)
|
|
106
|
+
next if hs.empty?
|
|
107
|
+
|
|
108
|
+
acc[m] = {
|
|
109
|
+
count: hs.size,
|
|
110
|
+
rivalry: rivalry?(modality: m),
|
|
111
|
+
percept: current_percept(modality: m)&.to_h
|
|
112
|
+
}
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
{
|
|
116
|
+
hypotheses_total: @hypotheses.size,
|
|
117
|
+
ambiguity_level: ambiguity_level.round(4),
|
|
118
|
+
by_modality: by_modality
|
|
119
|
+
}
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def active_hypotheses_for(modality)
|
|
125
|
+
@hypotheses.values.select { |h| h.modality == modality && %i[active selected].include?(h.state) }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def compute_likelihood(hypothesis, evidence_content, strength)
|
|
129
|
+
base = hypothesis.content.to_s.downcase.include?(evidence_content.to_s.downcase) ? 0.8 : 0.3
|
|
130
|
+
[(base * strength) + (hypothesis.likelihood * (1.0 - strength)), 1.0].min
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def record_evidence(modality, content, strength)
|
|
134
|
+
entry = { modality: modality, content: content, strength: strength, at: Time.now.utc }
|
|
135
|
+
@evidence_log << entry
|
|
136
|
+
@evidence_log.shift while @evidence_log.size > MAX_EVIDENCE
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def prune_modality(modality)
|
|
140
|
+
candidates = hypotheses_for(modality).select { |h| %i[suppressed decayed].include?(h.state) }
|
|
141
|
+
candidates = hypotheses_for(modality).min_by(5, &:posterior) if candidates.empty?
|
|
142
|
+
candidates.each { |h| @hypotheses.delete(h.id) }
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def validate_modality!(modality)
|
|
146
|
+
raise ArgumentError, "Unknown modality: #{modality}" unless MODALITIES.include?(modality)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Inference
|
|
9
|
+
module PerceptualInference
|
|
10
|
+
module Helpers
|
|
11
|
+
class PerceptualHypothesis
|
|
12
|
+
attr_reader :id, :content, :modality, :domain, :state, :created_at
|
|
13
|
+
attr_accessor :prior, :likelihood, :posterior
|
|
14
|
+
|
|
15
|
+
def initialize(content:, modality:, domain: :general, prior: DEFAULT_PRIOR)
|
|
16
|
+
@id = SecureRandom.uuid
|
|
17
|
+
@content = content
|
|
18
|
+
@modality = modality
|
|
19
|
+
@domain = domain
|
|
20
|
+
@prior = clamp_prior(prior.to_f)
|
|
21
|
+
@likelihood = 0.5
|
|
22
|
+
@posterior = @prior
|
|
23
|
+
@state = :active
|
|
24
|
+
@created_at = Time.now.utc
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def compute_posterior(evidence_weight:)
|
|
28
|
+
weight = [evidence_weight.to_f, EVIDENCE_STRENGTH_FLOOR].max
|
|
29
|
+
raw = @prior * ((@likelihood * weight) + ((1.0 - weight) * @prior))
|
|
30
|
+
@posterior = clamp_unit(raw)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def select!
|
|
34
|
+
@state = :selected
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def suppress!
|
|
38
|
+
@state = :suppressed
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def selected?
|
|
42
|
+
@state == :selected
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def rival_with?(other)
|
|
46
|
+
return false unless other.is_a?(PerceptualHypothesis)
|
|
47
|
+
|
|
48
|
+
(posterior - other.posterior).abs <= RIVALRY_MARGIN
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def adapt_prior(outcome:)
|
|
52
|
+
delta = outcome == :correct ? ADAPTATION_RATE : -ADAPTATION_RATE
|
|
53
|
+
@prior = clamp_prior(@prior + delta)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def decay
|
|
57
|
+
if @state == :suppressed
|
|
58
|
+
@prior = clamp_prior(@prior - (DECAY_RATE * 5))
|
|
59
|
+
@state = :decayed if @prior <= PRIOR_FLOOR + 0.001
|
|
60
|
+
else
|
|
61
|
+
@prior = clamp_prior(@prior + ((DEFAULT_PRIOR - @prior) * DECAY_RATE))
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def percept_label
|
|
66
|
+
PERCEPT_LABELS.each { |range, label| return label if range.cover?(@posterior) }
|
|
67
|
+
:subliminal
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def to_h
|
|
71
|
+
{
|
|
72
|
+
id: @id,
|
|
73
|
+
content: @content,
|
|
74
|
+
modality: @modality,
|
|
75
|
+
domain: @domain,
|
|
76
|
+
prior: @prior.round(4),
|
|
77
|
+
likelihood: @likelihood.round(4),
|
|
78
|
+
posterior: @posterior.round(4),
|
|
79
|
+
state: @state,
|
|
80
|
+
label: percept_label,
|
|
81
|
+
created_at: @created_at
|
|
82
|
+
}
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def clamp_prior(value)
|
|
88
|
+
value.clamp(PRIOR_FLOOR, PRIOR_CEILING)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def clamp_unit(value)
|
|
92
|
+
value.clamp(0.0, 1.0)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|