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,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/inference/schema/helpers/constants'
|
|
4
|
+
require 'legion/extensions/agentic/inference/schema/helpers/causal_relation'
|
|
5
|
+
require 'legion/extensions/agentic/inference/schema/helpers/world_model'
|
|
6
|
+
require 'legion/extensions/agentic/inference/schema/runners/schema'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module Agentic
|
|
11
|
+
module Inference
|
|
12
|
+
module Schema
|
|
13
|
+
class Client
|
|
14
|
+
include Runners::Schema
|
|
15
|
+
|
|
16
|
+
attr_reader :world_model
|
|
17
|
+
|
|
18
|
+
def initialize(world_model: nil, **)
|
|
19
|
+
@world_model = world_model || Helpers::WorldModel.new
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Inference
|
|
9
|
+
module Schema
|
|
10
|
+
module Helpers
|
|
11
|
+
class CausalRelation
|
|
12
|
+
attr_reader :id, :cause, :effect, :relation_type, :confidence,
|
|
13
|
+
:evidence_count, :created_at, :updated_at
|
|
14
|
+
|
|
15
|
+
def initialize(cause:, effect:, relation_type:, confidence: 0.5)
|
|
16
|
+
@id = SecureRandom.uuid
|
|
17
|
+
@cause = cause
|
|
18
|
+
@effect = effect
|
|
19
|
+
@relation_type = relation_type
|
|
20
|
+
@confidence = confidence.clamp(0.0, 1.0)
|
|
21
|
+
@evidence_count = 1
|
|
22
|
+
@created_at = Time.now.utc
|
|
23
|
+
@updated_at = Time.now.utc
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def reinforce(amount = Constants::REINFORCEMENT_BONUS)
|
|
27
|
+
@confidence = [@confidence + amount, 1.0].min
|
|
28
|
+
@evidence_count += 1
|
|
29
|
+
@updated_at = Time.now.utc
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def weaken(amount = Constants::CONTRADICTION_PENALTY)
|
|
33
|
+
@confidence = [@confidence - amount, 0.0].max
|
|
34
|
+
@updated_at = Time.now.utc
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def decay
|
|
38
|
+
@confidence = [@confidence - Constants::DECAY_RATE, 0.0].max
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def established?
|
|
42
|
+
@confidence >= Constants::CONFIDENCE_LEVELS[:established]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def speculative?
|
|
46
|
+
@confidence <= Constants::CONFIDENCE_LEVELS[:speculative]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def prunable?
|
|
50
|
+
@confidence < Constants::PRUNE_THRESHOLD
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def to_h
|
|
54
|
+
{
|
|
55
|
+
id: @id,
|
|
56
|
+
cause: @cause,
|
|
57
|
+
effect: @effect,
|
|
58
|
+
relation_type: @relation_type,
|
|
59
|
+
confidence: @confidence.round(4),
|
|
60
|
+
evidence_count: @evidence_count,
|
|
61
|
+
established: established?
|
|
62
|
+
}
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module Schema
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
RELATION_TYPES = %i[
|
|
11
|
+
causes
|
|
12
|
+
prevents
|
|
13
|
+
enables
|
|
14
|
+
requires
|
|
15
|
+
correlates
|
|
16
|
+
contradicts
|
|
17
|
+
].freeze
|
|
18
|
+
|
|
19
|
+
CONFIDENCE_LEVELS = {
|
|
20
|
+
established: 0.9,
|
|
21
|
+
strong: 0.7,
|
|
22
|
+
moderate: 0.5,
|
|
23
|
+
weak: 0.3,
|
|
24
|
+
speculative: 0.1
|
|
25
|
+
}.freeze
|
|
26
|
+
|
|
27
|
+
SCHEMA_ALPHA = 0.12
|
|
28
|
+
|
|
29
|
+
MAX_SCHEMAS = 500
|
|
30
|
+
|
|
31
|
+
MAX_RELATIONS_PER_SCHEMA = 50
|
|
32
|
+
|
|
33
|
+
REINFORCEMENT_BONUS = 0.05
|
|
34
|
+
|
|
35
|
+
CONTRADICTION_PENALTY = 0.15
|
|
36
|
+
|
|
37
|
+
DECAY_RATE = 0.005
|
|
38
|
+
|
|
39
|
+
PRUNE_THRESHOLD = 0.1
|
|
40
|
+
|
|
41
|
+
MAX_COUNTERFACTUAL_DEPTH = 5
|
|
42
|
+
|
|
43
|
+
MAX_EXPLANATION_CHAIN = 10
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module Schema
|
|
8
|
+
module Helpers
|
|
9
|
+
class WorldModel
|
|
10
|
+
attr_reader :relations, :domains
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@relations = {}
|
|
14
|
+
@domains = Hash.new { |h, k| h[k] = [] }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def add_relation(cause:, effect:, relation_type:, confidence: 0.5)
|
|
18
|
+
return nil unless Constants::RELATION_TYPES.include?(relation_type)
|
|
19
|
+
|
|
20
|
+
key = relation_key(cause, effect, relation_type)
|
|
21
|
+
if @relations.key?(key)
|
|
22
|
+
@relations[key].reinforce
|
|
23
|
+
else
|
|
24
|
+
@relations[key] = CausalRelation.new(cause: cause, effect: effect, relation_type: relation_type, confidence: confidence)
|
|
25
|
+
index_domains(cause, effect, key)
|
|
26
|
+
trim_relations
|
|
27
|
+
end
|
|
28
|
+
@relations[key]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def weaken_relation(cause:, effect:, relation_type:)
|
|
32
|
+
key = relation_key(cause, effect, relation_type)
|
|
33
|
+
relation = @relations[key]
|
|
34
|
+
return nil unless relation
|
|
35
|
+
|
|
36
|
+
relation.weaken
|
|
37
|
+
prune_if_needed(key)
|
|
38
|
+
relation
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def find_effects(cause)
|
|
42
|
+
keys = @domains[cause] || []
|
|
43
|
+
keys.filter_map { |k| @relations[k] }.select { |r| r.cause == cause }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def find_causes(effect)
|
|
47
|
+
keys = @domains[effect] || []
|
|
48
|
+
keys.filter_map { |k| @relations[k] }.select { |r| r.effect == effect }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def explain(outcome, max_depth: Constants::MAX_EXPLANATION_CHAIN)
|
|
52
|
+
chain = []
|
|
53
|
+
visited = Set.new
|
|
54
|
+
build_explanation_chain(outcome, chain, visited, max_depth)
|
|
55
|
+
chain
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def counterfactual(cause, max_depth: Constants::MAX_COUNTERFACTUAL_DEPTH)
|
|
59
|
+
affected = []
|
|
60
|
+
visited = Set.new
|
|
61
|
+
propagate_counterfactual(cause, affected, visited, max_depth)
|
|
62
|
+
affected
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def contradictions
|
|
66
|
+
@relations.values.each_with_object([]) do |rel, result|
|
|
67
|
+
next unless rel.relation_type == :causes
|
|
68
|
+
|
|
69
|
+
opposite = @relations[relation_key(rel.cause, rel.effect, :prevents)]
|
|
70
|
+
next unless opposite
|
|
71
|
+
|
|
72
|
+
result << {
|
|
73
|
+
cause: rel.cause,
|
|
74
|
+
effect: rel.effect,
|
|
75
|
+
causes_confidence: rel.confidence.round(4),
|
|
76
|
+
prevents_confidence: opposite.confidence.round(4)
|
|
77
|
+
}
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def decay_all
|
|
82
|
+
@relations.each_value(&:decay)
|
|
83
|
+
prune_weak
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def relation_count
|
|
87
|
+
@relations.size
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def domain_count
|
|
91
|
+
all_entities = @relations.values.flat_map { |r| [r.cause, r.effect] }
|
|
92
|
+
all_entities.uniq.size
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def established_relations
|
|
96
|
+
@relations.values.select(&:established?)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def to_h
|
|
100
|
+
{
|
|
101
|
+
relation_count: relation_count,
|
|
102
|
+
domain_count: domain_count,
|
|
103
|
+
established_count: established_relations.size,
|
|
104
|
+
contradiction_count: contradictions.size
|
|
105
|
+
}
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
private
|
|
109
|
+
|
|
110
|
+
def relation_key(cause, effect, relation_type)
|
|
111
|
+
"#{cause}->#{relation_type}->#{effect}"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def index_domains(cause, effect, key)
|
|
115
|
+
@domains[cause] << key
|
|
116
|
+
@domains[effect] << key
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def build_explanation_chain(outcome, chain, visited, depth)
|
|
120
|
+
return if depth <= 0 || visited.include?(outcome)
|
|
121
|
+
|
|
122
|
+
visited.add(outcome)
|
|
123
|
+
causes = find_causes(outcome).sort_by { |r| -r.confidence }
|
|
124
|
+
causes.first(3).each do |rel|
|
|
125
|
+
chain << rel.to_h
|
|
126
|
+
build_explanation_chain(rel.cause, chain, visited, depth - 1)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def propagate_counterfactual(cause, affected, visited, depth)
|
|
131
|
+
return if depth <= 0 || visited.include?(cause)
|
|
132
|
+
|
|
133
|
+
visited.add(cause)
|
|
134
|
+
effects = find_effects(cause).sort_by { |r| -r.confidence }
|
|
135
|
+
effects.each do |rel|
|
|
136
|
+
affected << { effect: rel.effect, relation: rel.relation_type, confidence: rel.confidence.round(4) }
|
|
137
|
+
propagate_counterfactual(rel.effect, affected, visited, depth - 1)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def prune_if_needed(key)
|
|
142
|
+
relation = @relations[key]
|
|
143
|
+
return unless relation&.prunable?
|
|
144
|
+
|
|
145
|
+
remove_relation(key)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def prune_weak
|
|
149
|
+
prunable = @relations.select { |_, r| r.prunable? }.keys
|
|
150
|
+
prunable.each { |key| remove_relation(key) }
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def remove_relation(key)
|
|
154
|
+
rel = @relations.delete(key)
|
|
155
|
+
return unless rel
|
|
156
|
+
|
|
157
|
+
@domains[rel.cause]&.delete(key)
|
|
158
|
+
@domains[rel.effect]&.delete(key)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def trim_relations
|
|
162
|
+
return if @relations.size <= Constants::MAX_SCHEMAS
|
|
163
|
+
|
|
164
|
+
weakest = @relations.sort_by { |_, r| r.confidence }
|
|
165
|
+
weakest.first(@relations.size - Constants::MAX_SCHEMAS).each { |k, _| remove_relation(k) } # rubocop:disable Style/HashEachMethods
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
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 Schema
|
|
8
|
+
module Runners
|
|
9
|
+
module Schema
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def update_schema(tick_results: {}, **)
|
|
14
|
+
extract_prediction_outcomes(tick_results)
|
|
15
|
+
world_model.decay_all
|
|
16
|
+
|
|
17
|
+
Legion::Logging.debug "[schema] relations=#{world_model.relation_count} " \
|
|
18
|
+
"domains=#{world_model.domain_count} established=#{world_model.established_relations.size}"
|
|
19
|
+
|
|
20
|
+
world_model.to_h
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def learn_relation(cause:, effect:, relation_type:, confidence: 0.5, **)
|
|
24
|
+
relation_sym = relation_type.to_sym
|
|
25
|
+
result = world_model.add_relation(cause: cause, effect: effect, relation_type: relation_sym, confidence: confidence)
|
|
26
|
+
return { success: false, error: 'invalid relation type' } unless result
|
|
27
|
+
|
|
28
|
+
Legion::Logging.info "[schema] learned: #{cause} #{relation_sym} #{effect} (#{result.confidence.round(2)})"
|
|
29
|
+
{ success: true, relation: result.to_h }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def weaken_relation(cause:, effect:, relation_type:, **)
|
|
33
|
+
result = world_model.weaken_relation(cause: cause, effect: effect, relation_type: relation_type.to_sym)
|
|
34
|
+
return { success: false, error: 'relation not found' } unless result
|
|
35
|
+
|
|
36
|
+
Legion::Logging.debug "[schema] weakened: #{cause} #{relation_type} #{effect}"
|
|
37
|
+
{ success: true, relation: result.to_h }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def explain(outcome:, **)
|
|
41
|
+
chain = world_model.explain(outcome)
|
|
42
|
+
Legion::Logging.debug "[schema] explanation for #{outcome}: #{chain.size} links"
|
|
43
|
+
{ outcome: outcome, chain: chain, depth: chain.size }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def counterfactual(cause:, **)
|
|
47
|
+
affected = world_model.counterfactual(cause)
|
|
48
|
+
Legion::Logging.debug "[schema] counterfactual for #{cause}: #{affected.size} effects"
|
|
49
|
+
{ cause: cause, affected: affected, impact: affected.size }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def find_effects(cause:, **)
|
|
53
|
+
effects = world_model.find_effects(cause).map(&:to_h)
|
|
54
|
+
{ cause: cause, effects: effects, count: effects.size }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def find_causes(effect:, **)
|
|
58
|
+
causes = world_model.find_causes(effect).map(&:to_h)
|
|
59
|
+
{ effect: effect, causes: causes, count: causes.size }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def contradictions(**)
|
|
63
|
+
result = world_model.contradictions
|
|
64
|
+
Legion::Logging.debug "[schema] contradictions: #{result.size}"
|
|
65
|
+
{ contradictions: result, count: result.size }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def schema_stats(**)
|
|
69
|
+
Legion::Logging.debug '[schema] stats'
|
|
70
|
+
world_model.to_h.merge(
|
|
71
|
+
top_relations: world_model.established_relations.first(10).map(&:to_h)
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def world_model
|
|
78
|
+
@world_model ||= Helpers::WorldModel.new
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def extract_prediction_outcomes(tick_results)
|
|
82
|
+
predictions = tick_results.dig(:prediction_engine, :resolved)
|
|
83
|
+
return unless predictions.is_a?(Array)
|
|
84
|
+
|
|
85
|
+
predictions.each do |pred|
|
|
86
|
+
next unless pred[:domain] && pred[:outcome_domain]
|
|
87
|
+
|
|
88
|
+
if pred[:accurate]
|
|
89
|
+
world_model.add_relation(cause: pred[:domain], effect: pred[:outcome_domain], relation_type: :causes, confidence: 0.6)
|
|
90
|
+
else
|
|
91
|
+
world_model.weaken_relation(cause: pred[:domain], effect: pred[:outcome_domain], relation_type: :causes)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
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 'legion/extensions/agentic/inference/schema/version'
|
|
4
|
+
require 'legion/extensions/agentic/inference/schema/helpers/constants'
|
|
5
|
+
require 'legion/extensions/agentic/inference/schema/helpers/causal_relation'
|
|
6
|
+
require 'legion/extensions/agentic/inference/schema/helpers/world_model'
|
|
7
|
+
require 'legion/extensions/agentic/inference/schema/runners/schema'
|
|
8
|
+
require 'legion/extensions/agentic/inference/schema/client'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module Agentic
|
|
13
|
+
module Inference
|
|
14
|
+
module Schema
|
|
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/uncertainty_tolerance/helpers/constants'
|
|
4
|
+
require 'legion/extensions/agentic/inference/uncertainty_tolerance/helpers/decision'
|
|
5
|
+
require 'legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine'
|
|
6
|
+
require 'legion/extensions/agentic/inference/uncertainty_tolerance/runners/uncertainty_tolerance'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module Agentic
|
|
11
|
+
module Inference
|
|
12
|
+
module UncertaintyTolerance
|
|
13
|
+
class Client
|
|
14
|
+
include Runners::UncertaintyTolerance
|
|
15
|
+
|
|
16
|
+
def initialize(**)
|
|
17
|
+
@engine = Helpers::ToleranceEngine.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :engine
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Inference
|
|
7
|
+
module UncertaintyTolerance
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
TOLERANCE_LABELS = {
|
|
11
|
+
(0.8..) => :highly_tolerant,
|
|
12
|
+
(0.6...0.8) => :tolerant,
|
|
13
|
+
(0.4...0.6) => :moderate,
|
|
14
|
+
(0.2...0.4) => :intolerant,
|
|
15
|
+
(..0.2) => :highly_intolerant
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
DECISION_TYPES = %i[certain probable uncertain ambiguous unknown].freeze
|
|
19
|
+
|
|
20
|
+
CERTAINTY_THRESHOLDS = {
|
|
21
|
+
certain: 0.9,
|
|
22
|
+
probable: 0.7,
|
|
23
|
+
uncertain: 0.5,
|
|
24
|
+
ambiguous: 0.3,
|
|
25
|
+
unknown: 0.0
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
MAX_DECISIONS = 300
|
|
29
|
+
MAX_HISTORY = 500
|
|
30
|
+
DEFAULT_TOLERANCE = 0.5
|
|
31
|
+
TOLERANCE_FLOOR = 0.0
|
|
32
|
+
TOLERANCE_CEILING = 1.0
|
|
33
|
+
ADAPTATION_RATE = 0.05
|
|
34
|
+
POSITIVE_OUTCOME_BOOST = 0.03
|
|
35
|
+
NEGATIVE_OUTCOME_PENALTY = 0.05
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Inference
|
|
9
|
+
module UncertaintyTolerance
|
|
10
|
+
module Helpers
|
|
11
|
+
class Decision
|
|
12
|
+
attr_reader :id, :description, :domain, :certainty_level,
|
|
13
|
+
:tolerance_at_time, :acted_despite_uncertainty, :created_at
|
|
14
|
+
attr_accessor :actual_outcome
|
|
15
|
+
|
|
16
|
+
def initialize(description:, domain:, certainty_level:, tolerance_at_time:)
|
|
17
|
+
@id = SecureRandom.uuid
|
|
18
|
+
@description = description
|
|
19
|
+
@domain = domain
|
|
20
|
+
@certainty_level = certainty_level.clamp(0.0, 1.0)
|
|
21
|
+
@tolerance_at_time = tolerance_at_time
|
|
22
|
+
@actual_outcome = nil
|
|
23
|
+
@acted_despite_uncertainty = certainty_level < tolerance_at_time
|
|
24
|
+
@created_at = Time.now.utc
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def resolve!(outcome:)
|
|
28
|
+
@actual_outcome = outcome
|
|
29
|
+
self
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def successful?
|
|
33
|
+
@actual_outcome == :success
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def risky?
|
|
37
|
+
@certainty_level < 0.4
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def decision_type
|
|
41
|
+
Constants::CERTAINTY_THRESHOLDS.each do |type, threshold|
|
|
42
|
+
return type if @certainty_level >= threshold
|
|
43
|
+
end
|
|
44
|
+
:unknown
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def to_h
|
|
48
|
+
{
|
|
49
|
+
id: @id,
|
|
50
|
+
description: @description,
|
|
51
|
+
domain: @domain,
|
|
52
|
+
certainty_level: @certainty_level,
|
|
53
|
+
actual_outcome: @actual_outcome,
|
|
54
|
+
tolerance_at_time: @tolerance_at_time,
|
|
55
|
+
decision_type: decision_type,
|
|
56
|
+
acted_despite_uncertainty: @acted_despite_uncertainty,
|
|
57
|
+
created_at: @created_at
|
|
58
|
+
}
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|