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,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Inference
|
|
9
|
+
module EnactiveCognition
|
|
10
|
+
module Helpers
|
|
11
|
+
class SensorimotorLoop
|
|
12
|
+
MAX_COUPLINGS = 200
|
|
13
|
+
MAX_ACTIONS = 500
|
|
14
|
+
MAX_PERCEPTIONS = 500
|
|
15
|
+
MAX_HISTORY = 300
|
|
16
|
+
|
|
17
|
+
DEFAULT_COUPLING_STRENGTH = 0.5
|
|
18
|
+
COUPLING_FLOOR = 0.0
|
|
19
|
+
COUPLING_CEILING = 1.0
|
|
20
|
+
|
|
21
|
+
REINFORCEMENT_RATE = 0.1
|
|
22
|
+
DECOUPLING_RATE = 0.15
|
|
23
|
+
|
|
24
|
+
PREDICTION_ACCURACY_THRESHOLD = 0.6
|
|
25
|
+
|
|
26
|
+
COUPLING_DECAY = 0.02
|
|
27
|
+
STALE_THRESHOLD = 120
|
|
28
|
+
|
|
29
|
+
COUPLING_LABELS = {
|
|
30
|
+
(0.8..) => :entrained,
|
|
31
|
+
(0.6...0.8) => :coupled,
|
|
32
|
+
(0.4...0.6) => :forming,
|
|
33
|
+
(0.2...0.4) => :weak,
|
|
34
|
+
(..0.2) => :decoupled
|
|
35
|
+
}.freeze
|
|
36
|
+
|
|
37
|
+
LOOP_TYPES = %i[sensorimotor cognitive social].freeze
|
|
38
|
+
|
|
39
|
+
attr_reader :id, :action, :perception, :domain, :loop_type,
|
|
40
|
+
:coupling_strength, :prediction_accuracy,
|
|
41
|
+
:execution_count, :accurate_predictions,
|
|
42
|
+
:created_at, :last_executed_at
|
|
43
|
+
|
|
44
|
+
def initialize(action:, perception:, domain:, loop_type: :sensorimotor)
|
|
45
|
+
@id = SecureRandom.uuid
|
|
46
|
+
@action = action
|
|
47
|
+
@perception = perception
|
|
48
|
+
@domain = domain
|
|
49
|
+
@loop_type = LOOP_TYPES.include?(loop_type) ? loop_type : :sensorimotor
|
|
50
|
+
@coupling_strength = DEFAULT_COUPLING_STRENGTH
|
|
51
|
+
@execution_count = 0
|
|
52
|
+
@accurate_predictions = 0
|
|
53
|
+
@prediction_accuracy = 0.0
|
|
54
|
+
@created_at = Time.now.utc
|
|
55
|
+
@last_executed_at = nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def execute!(actual_perception:)
|
|
59
|
+
@execution_count += 1
|
|
60
|
+
@last_executed_at = Time.now.utc
|
|
61
|
+
match = actual_perception.to_s == @perception.to_s
|
|
62
|
+
|
|
63
|
+
if match
|
|
64
|
+
@accurate_predictions += 1
|
|
65
|
+
@coupling_strength = (@coupling_strength + REINFORCEMENT_RATE).clamp(COUPLING_FLOOR, COUPLING_CEILING)
|
|
66
|
+
else
|
|
67
|
+
@coupling_strength = (@coupling_strength - DECOUPLING_RATE).clamp(COUPLING_FLOOR, COUPLING_CEILING)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
@prediction_accuracy = @accurate_predictions.to_f / @execution_count
|
|
71
|
+
{ match: match, coupling_strength: @coupling_strength, prediction_accuracy: @prediction_accuracy }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def coupled?
|
|
75
|
+
coupling_strength >= 0.6
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def coupling_label
|
|
79
|
+
COUPLING_LABELS.each do |range, label|
|
|
80
|
+
return label if range.cover?(coupling_strength)
|
|
81
|
+
end
|
|
82
|
+
:decoupled
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def adapt_perception!(new_perception:)
|
|
86
|
+
@perception = new_perception
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def decay!
|
|
90
|
+
@coupling_strength = (@coupling_strength - COUPLING_DECAY).clamp(COUPLING_FLOOR, COUPLING_CEILING)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def stale?
|
|
94
|
+
return false if @last_executed_at.nil?
|
|
95
|
+
|
|
96
|
+
(Time.now.utc - @last_executed_at) > STALE_THRESHOLD
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def to_h
|
|
100
|
+
{
|
|
101
|
+
id: @id,
|
|
102
|
+
action: @action,
|
|
103
|
+
perception: @perception,
|
|
104
|
+
domain: @domain,
|
|
105
|
+
loop_type: @loop_type,
|
|
106
|
+
coupling_strength: @coupling_strength,
|
|
107
|
+
coupling_label: coupling_label,
|
|
108
|
+
prediction_accuracy: @prediction_accuracy,
|
|
109
|
+
execution_count: @execution_count,
|
|
110
|
+
accurate_predictions: @accurate_predictions,
|
|
111
|
+
coupled: coupled?,
|
|
112
|
+
created_at: @created_at,
|
|
113
|
+
last_executed_at: @last_executed_at
|
|
114
|
+
}
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module EnactiveCognition
|
|
8
|
+
module Runners
|
|
9
|
+
module EnactiveCognition
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def create_sensorimotor_coupling(action:, perception:, domain:, loop_type: :sensorimotor, **)
|
|
14
|
+
type = loop_type.is_a?(Symbol) ? loop_type : loop_type.to_sym
|
|
15
|
+
loop = enaction_engine.create_coupling(
|
|
16
|
+
action: action,
|
|
17
|
+
perception: perception,
|
|
18
|
+
domain: domain,
|
|
19
|
+
loop_type: type
|
|
20
|
+
)
|
|
21
|
+
Legion::Logging.debug "[enactive_cognition] created coupling id=#{loop.id[0..7]} " \
|
|
22
|
+
"action=#{action} domain=#{domain} type=#{type}"
|
|
23
|
+
{ success: true, coupling: loop.to_h }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def execute_enactive_action(coupling_id:, actual_perception:, **)
|
|
27
|
+
result = enaction_engine.execute_action(
|
|
28
|
+
coupling_id: coupling_id,
|
|
29
|
+
actual_perception: actual_perception
|
|
30
|
+
)
|
|
31
|
+
unless result[:success]
|
|
32
|
+
Legion::Logging.debug "[enactive_cognition] execute failed: #{coupling_id[0..7]} not found"
|
|
33
|
+
return result
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Legion::Logging.debug "[enactive_cognition] executed #{coupling_id[0..7]} " \
|
|
37
|
+
"match=#{result[:match]} label=#{result[:coupling_label]}"
|
|
38
|
+
result
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def adapt_sensorimotor_coupling(coupling_id:, new_perception:, **)
|
|
42
|
+
result = enaction_engine.adapt_coupling(
|
|
43
|
+
coupling_id: coupling_id,
|
|
44
|
+
new_perception: new_perception
|
|
45
|
+
)
|
|
46
|
+
unless result[:success]
|
|
47
|
+
Legion::Logging.debug "[enactive_cognition] adapt failed: #{coupling_id[0..7]} not found"
|
|
48
|
+
return result
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
Legion::Logging.info "[enactive_cognition] adapted #{coupling_id[0..7]} new_perception=#{new_perception}"
|
|
52
|
+
result
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def find_action_for_perception(perception:, **)
|
|
56
|
+
loop = enaction_engine.find_action_for(perception: perception)
|
|
57
|
+
unless loop
|
|
58
|
+
Legion::Logging.debug "[enactive_cognition] no coupled action found for perception=#{perception}"
|
|
59
|
+
return { found: false, perception: perception }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
Legion::Logging.debug "[enactive_cognition] found action=#{loop.action} for perception=#{perception}"
|
|
63
|
+
{ found: true, action: loop.action, coupling: loop.to_h }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def coupled_sensorimotor_loops(**)
|
|
67
|
+
loops = enaction_engine.coupled_loops
|
|
68
|
+
Legion::Logging.debug "[enactive_cognition] coupled loops count=#{loops.size}"
|
|
69
|
+
{ loops: loops.map(&:to_h), count: loops.size }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def domain_couplings(domain:, **)
|
|
73
|
+
loops = enaction_engine.by_domain(domain: domain)
|
|
74
|
+
Legion::Logging.debug "[enactive_cognition] domain=#{domain} loops=#{loops.size}"
|
|
75
|
+
{ domain: domain, loops: loops.map(&:to_h), count: loops.size }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def strongest_couplings(limit: 5, **)
|
|
79
|
+
loops = enaction_engine.strongest_couplings(limit: limit)
|
|
80
|
+
Legion::Logging.debug "[enactive_cognition] strongest couplings limit=#{limit} found=#{loops.size}"
|
|
81
|
+
{ loops: loops.map(&:to_h), count: loops.size }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def overall_enactive_coupling(**)
|
|
85
|
+
strength = enaction_engine.overall_coupling
|
|
86
|
+
label = coupling_label_for(strength)
|
|
87
|
+
Legion::Logging.debug "[enactive_cognition] overall_coupling=#{strength.round(3)} label=#{label}"
|
|
88
|
+
{ overall_coupling: strength, coupling_label: label }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def update_enactive_cognition(decay: false, prune: false, **)
|
|
92
|
+
enaction_engine.decay_all if decay
|
|
93
|
+
enaction_engine.prune_decoupled if prune
|
|
94
|
+
Legion::Logging.debug "[enactive_cognition] update decay=#{decay} prune=#{prune} " \
|
|
95
|
+
"remaining=#{enaction_engine.count}"
|
|
96
|
+
{ success: true, decay: decay, prune: prune, coupling_count: enaction_engine.count }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def enactive_cognition_stats(**)
|
|
100
|
+
stats = enaction_engine.to_h
|
|
101
|
+
Legion::Logging.debug "[enactive_cognition] stats couplings=#{stats[:coupling_count]} " \
|
|
102
|
+
"coupled=#{stats[:coupled_count]}"
|
|
103
|
+
{ success: true, stats: stats }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
def enaction_engine
|
|
109
|
+
@enaction_engine ||= Helpers::EnactionEngine.new
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def coupling_label_for(strength)
|
|
113
|
+
Helpers::SensorimotorLoop::COUPLING_LABELS.each do |range, label|
|
|
114
|
+
return label if range.cover?(strength)
|
|
115
|
+
end
|
|
116
|
+
:decoupled
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/inference/enactive_cognition/version'
|
|
4
|
+
require 'legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop'
|
|
5
|
+
require 'legion/extensions/agentic/inference/enactive_cognition/helpers/enaction_engine'
|
|
6
|
+
require 'legion/extensions/agentic/inference/enactive_cognition/runners/enactive_cognition'
|
|
7
|
+
require 'legion/extensions/agentic/inference/enactive_cognition/client'
|
|
8
|
+
|
|
9
|
+
module Legion
|
|
10
|
+
module Extensions
|
|
11
|
+
module Agentic
|
|
12
|
+
module Inference
|
|
13
|
+
module EnactiveCognition
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module ExpectationViolation
|
|
8
|
+
module Helpers
|
|
9
|
+
class Client
|
|
10
|
+
include Runners::ExpectationViolation
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module ExpectationViolation
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
VIOLATION_TYPES = %i[positive negative neutral].freeze
|
|
11
|
+
|
|
12
|
+
VIOLATION_LABELS = {
|
|
13
|
+
(0.8..) => :extreme_positive,
|
|
14
|
+
(0.3...0.8) => :positive,
|
|
15
|
+
(-0.3...0.3) => :neutral,
|
|
16
|
+
(-0.8...-0.3) => :negative,
|
|
17
|
+
(..-0.8) => :extreme_negative
|
|
18
|
+
}.freeze
|
|
19
|
+
|
|
20
|
+
AROUSAL_LABELS = {
|
|
21
|
+
(0.8..) => :highly_aroused,
|
|
22
|
+
(0.6...0.8) => :aroused,
|
|
23
|
+
(0.4...0.6) => :moderate,
|
|
24
|
+
(0.2...0.4) => :calm,
|
|
25
|
+
(..0.2) => :unaffected
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
MAX_EXPECTATIONS = 200
|
|
29
|
+
MAX_VIOLATIONS = 500
|
|
30
|
+
MAX_HISTORY = 500
|
|
31
|
+
|
|
32
|
+
DEFAULT_EXPECTATION = 0.5
|
|
33
|
+
EXPECTATION_FLOOR = 0.0
|
|
34
|
+
EXPECTATION_CEILING = 1.0
|
|
35
|
+
|
|
36
|
+
AROUSAL_BASE = 0.3
|
|
37
|
+
AROUSAL_MULTIPLIER = 0.7
|
|
38
|
+
|
|
39
|
+
ADAPTATION_RATE = 0.1
|
|
40
|
+
DECAY_RATE = 0.02
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Inference
|
|
9
|
+
module ExpectationViolation
|
|
10
|
+
module Helpers
|
|
11
|
+
class Expectation
|
|
12
|
+
include Constants
|
|
13
|
+
|
|
14
|
+
attr_reader :id, :context, :domain, :expected_value, :tolerance,
|
|
15
|
+
:violation_count, :adaptation_count, :created_at
|
|
16
|
+
|
|
17
|
+
def initialize(context:, domain:, expected_value: DEFAULT_EXPECTATION, tolerance: 0.2)
|
|
18
|
+
@id = SecureRandom.uuid
|
|
19
|
+
@context = context
|
|
20
|
+
@domain = domain
|
|
21
|
+
@expected_value = expected_value.clamp(EXPECTATION_FLOOR, EXPECTATION_CEILING)
|
|
22
|
+
@tolerance = tolerance.clamp(0.01, 0.5)
|
|
23
|
+
@violation_count = 0
|
|
24
|
+
@adaptation_count = 0
|
|
25
|
+
@created_at = Time.now.utc
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def evaluate(actual_value:)
|
|
29
|
+
deviation = actual_value - @expected_value
|
|
30
|
+
violated = deviation.abs > @tolerance
|
|
31
|
+
|
|
32
|
+
if violated
|
|
33
|
+
@violation_count += 1
|
|
34
|
+
arousal = compute_arousal(deviation)
|
|
35
|
+
violation_type = deviation.positive? ? :positive : :negative
|
|
36
|
+
{ violated: true, deviation: deviation.round(3), type: violation_type,
|
|
37
|
+
arousal: arousal.round(3), arousal_label: arousal_label(arousal) }
|
|
38
|
+
else
|
|
39
|
+
{ violated: false, deviation: deviation.round(3), type: :neutral,
|
|
40
|
+
arousal: 0.0, arousal_label: :unaffected }
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def adapt!(actual_value:)
|
|
45
|
+
@expected_value += (actual_value - @expected_value) * ADAPTATION_RATE
|
|
46
|
+
@expected_value = @expected_value.clamp(EXPECTATION_FLOOR, EXPECTATION_CEILING)
|
|
47
|
+
@adaptation_count += 1
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def violation_label(deviation)
|
|
51
|
+
VIOLATION_LABELS.find { |range, _| range.cover?(deviation) }&.last || :neutral
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def to_h
|
|
55
|
+
{
|
|
56
|
+
id: @id,
|
|
57
|
+
context: @context,
|
|
58
|
+
domain: @domain,
|
|
59
|
+
expected_value: @expected_value.round(3),
|
|
60
|
+
tolerance: @tolerance,
|
|
61
|
+
violation_count: @violation_count,
|
|
62
|
+
adaptation_count: @adaptation_count,
|
|
63
|
+
created_at: @created_at
|
|
64
|
+
}
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def compute_arousal(deviation)
|
|
70
|
+
(AROUSAL_BASE + (deviation.abs * AROUSAL_MULTIPLIER)).clamp(0.0, 1.0)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def arousal_label(arousal)
|
|
74
|
+
AROUSAL_LABELS.find { |range, _| range.cover?(arousal) }&.last || :moderate
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/violation_engine.rb
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module ExpectationViolation
|
|
8
|
+
module Helpers
|
|
9
|
+
class ViolationEngine
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
attr_reader :history
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@expectations = {}
|
|
16
|
+
@violations = []
|
|
17
|
+
@history = []
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def create_expectation(context:, domain:, expected_value: DEFAULT_EXPECTATION, tolerance: 0.2)
|
|
21
|
+
evict_oldest if @expectations.size >= MAX_EXPECTATIONS
|
|
22
|
+
|
|
23
|
+
expectation = Expectation.new(
|
|
24
|
+
context: context,
|
|
25
|
+
domain: domain,
|
|
26
|
+
expected_value: expected_value,
|
|
27
|
+
tolerance: tolerance
|
|
28
|
+
)
|
|
29
|
+
@expectations[expectation.id] = expectation
|
|
30
|
+
record_history(:created, expectation.id)
|
|
31
|
+
expectation
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def evaluate_against(expectation_id:, actual_value:)
|
|
35
|
+
expectation = @expectations[expectation_id]
|
|
36
|
+
return { success: false, reason: :not_found } unless expectation
|
|
37
|
+
|
|
38
|
+
result = expectation.evaluate(actual_value: actual_value)
|
|
39
|
+
|
|
40
|
+
if result[:violated]
|
|
41
|
+
@violations << {
|
|
42
|
+
expectation_id: expectation_id,
|
|
43
|
+
type: result[:type],
|
|
44
|
+
deviation: result[:deviation],
|
|
45
|
+
arousal: result[:arousal],
|
|
46
|
+
at: Time.now.utc
|
|
47
|
+
}
|
|
48
|
+
trim_violations
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
record_history(:evaluated, expectation_id)
|
|
52
|
+
{ success: true }.merge(result)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def adapt_expectation(expectation_id:, actual_value:)
|
|
56
|
+
expectation = @expectations[expectation_id]
|
|
57
|
+
return { success: false, reason: :not_found } unless expectation
|
|
58
|
+
|
|
59
|
+
expectation.adapt!(actual_value: actual_value)
|
|
60
|
+
record_history(:adapted, expectation_id)
|
|
61
|
+
{ success: true, new_expected: expectation.expected_value.round(3) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def recent_violations(limit: 10)
|
|
65
|
+
@violations.last(limit)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def violations_by_type(type:)
|
|
69
|
+
@violations.select { |vio| vio[:type] == type }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def expectations_by_domain(domain:)
|
|
73
|
+
@expectations.values.select { |exp| exp.domain == domain }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def most_violated(limit: 5)
|
|
77
|
+
@expectations.values.sort_by { |exp| -exp.violation_count }.first(limit)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def violation_rate
|
|
81
|
+
total = @expectations.values.sum(&:violation_count)
|
|
82
|
+
evals = @history.count { |entry| entry[:event] == :evaluated }
|
|
83
|
+
return 0.0 if evals.zero?
|
|
84
|
+
|
|
85
|
+
(total.to_f / evals).round(3)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def positive_violation_ratio
|
|
89
|
+
return 0.0 if @violations.empty?
|
|
90
|
+
|
|
91
|
+
positive = @violations.count { |vio| vio[:type] == :positive }
|
|
92
|
+
(positive.to_f / @violations.size).round(3)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def to_h
|
|
96
|
+
{
|
|
97
|
+
total_expectations: @expectations.size,
|
|
98
|
+
total_violations: @violations.size,
|
|
99
|
+
violation_rate: violation_rate,
|
|
100
|
+
positive_ratio: positive_violation_ratio,
|
|
101
|
+
history_count: @history.size
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def evict_oldest
|
|
108
|
+
oldest_id = @expectations.min_by { |_id, exp| exp.created_at }&.first
|
|
109
|
+
@expectations.delete(oldest_id) if oldest_id
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def trim_violations
|
|
113
|
+
@violations.shift while @violations.size > MAX_VIOLATIONS
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def record_history(event, expectation_id)
|
|
117
|
+
@history << { event: event, expectation_id: expectation_id, at: Time.now.utc }
|
|
118
|
+
@history.shift while @history.size > MAX_HISTORY
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
data/lib/legion/extensions/agentic/inference/expectation_violation/runners/expectation_violation.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module ExpectationViolation
|
|
8
|
+
module Runners
|
|
9
|
+
module ExpectationViolation
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def create_expectation(context:, domain:, expected_value: nil, tolerance: 0.2, **)
|
|
14
|
+
exp = engine.create_expectation(
|
|
15
|
+
context: context,
|
|
16
|
+
domain: domain.to_sym,
|
|
17
|
+
expected_value: expected_value || Helpers::Constants::DEFAULT_EXPECTATION,
|
|
18
|
+
tolerance: tolerance
|
|
19
|
+
)
|
|
20
|
+
Legion::Logging.debug "[expectation_violation] create id=#{exp.id[0..7]} " \
|
|
21
|
+
"ctx=#{context} expected=#{exp.expected_value}"
|
|
22
|
+
{ success: true, expectation: exp.to_h }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def evaluate_expectation(expectation_id:, actual_value:, **)
|
|
26
|
+
result = engine.evaluate_against(expectation_id: expectation_id, actual_value: actual_value)
|
|
27
|
+
Legion::Logging.debug "[expectation_violation] evaluate id=#{expectation_id[0..7]} " \
|
|
28
|
+
"violated=#{result[:violated]} type=#{result[:type]}"
|
|
29
|
+
result
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def adapt_expectation_value(expectation_id:, actual_value:, **)
|
|
33
|
+
result = engine.adapt_expectation(expectation_id: expectation_id, actual_value: actual_value)
|
|
34
|
+
Legion::Logging.debug "[expectation_violation] adapt id=#{expectation_id[0..7]} " \
|
|
35
|
+
"new=#{result[:new_expected]}"
|
|
36
|
+
result
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def recent_violations_report(limit: 10, **)
|
|
40
|
+
violations = engine.recent_violations(limit: limit)
|
|
41
|
+
Legion::Logging.debug "[expectation_violation] recent count=#{violations.size}"
|
|
42
|
+
{ success: true, violations: violations, count: violations.size }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def violations_by_type_report(type:, **)
|
|
46
|
+
violations = engine.violations_by_type(type: type.to_sym)
|
|
47
|
+
Legion::Logging.debug '[expectation_violation] by_type ' \
|
|
48
|
+
"type=#{type} count=#{violations.size}"
|
|
49
|
+
{ success: true, violations: violations, count: violations.size }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def most_violated_expectations(limit: 5, **)
|
|
53
|
+
exps = engine.most_violated(limit: limit)
|
|
54
|
+
Legion::Logging.debug "[expectation_violation] most_violated count=#{exps.size}"
|
|
55
|
+
{ success: true, expectations: exps.map(&:to_h), count: exps.size }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def expectation_violation_stats(**)
|
|
59
|
+
stats = engine.to_h
|
|
60
|
+
Legion::Logging.debug '[expectation_violation] stats ' \
|
|
61
|
+
"total=#{stats[:total_expectations]}"
|
|
62
|
+
{ success: true }.merge(stats)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def engine
|
|
68
|
+
@engine ||= Helpers::ViolationEngine.new
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'expectation_violation/version'
|
|
4
|
+
require_relative 'expectation_violation/helpers/constants'
|
|
5
|
+
require_relative 'expectation_violation/helpers/expectation'
|
|
6
|
+
require_relative 'expectation_violation/helpers/violation_engine'
|
|
7
|
+
require_relative 'expectation_violation/runners/expectation_violation'
|
|
8
|
+
require_relative 'expectation_violation/helpers/client'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module Agentic
|
|
13
|
+
module Inference
|
|
14
|
+
module ExpectationViolation
|
|
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 FreeEnergy
|
|
8
|
+
class Client
|
|
9
|
+
include Runners::FreeEnergy
|
|
10
|
+
|
|
11
|
+
def initialize(engine: nil)
|
|
12
|
+
@engine = engine
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|