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.
Files changed (328) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +12 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE +21 -0
  5. data/README.md +13 -0
  6. data/lex-agentic-inference.gemspec +30 -0
  7. data/lib/legion/extensions/agentic/inference/abductive/client.rb +25 -0
  8. data/lib/legion/extensions/agentic/inference/abductive/helpers/abduction_engine.rb +164 -0
  9. data/lib/legion/extensions/agentic/inference/abductive/helpers/constants.rb +39 -0
  10. data/lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb +106 -0
  11. data/lib/legion/extensions/agentic/inference/abductive/helpers/observation.rb +39 -0
  12. data/lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb +129 -0
  13. data/lib/legion/extensions/agentic/inference/abductive/version.rb +13 -0
  14. data/lib/legion/extensions/agentic/inference/abductive.rb +20 -0
  15. data/lib/legion/extensions/agentic/inference/affordance/actors/scan.rb +31 -0
  16. data/lib/legion/extensions/agentic/inference/affordance/client.rb +28 -0
  17. data/lib/legion/extensions/agentic/inference/affordance/helpers/affordance_field.rb +123 -0
  18. data/lib/legion/extensions/agentic/inference/affordance/helpers/affordance_item.rb +75 -0
  19. data/lib/legion/extensions/agentic/inference/affordance/helpers/constants.rb +42 -0
  20. data/lib/legion/extensions/agentic/inference/affordance/runners/affordance.rb +90 -0
  21. data/lib/legion/extensions/agentic/inference/affordance/version.rb +13 -0
  22. data/lib/legion/extensions/agentic/inference/affordance.rb +19 -0
  23. data/lib/legion/extensions/agentic/inference/analogical/client.rb +28 -0
  24. data/lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb +209 -0
  25. data/lib/legion/extensions/agentic/inference/analogical/helpers/constants.rb +38 -0
  26. data/lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb +113 -0
  27. data/lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb +106 -0
  28. data/lib/legion/extensions/agentic/inference/analogical/version.rb +13 -0
  29. data/lib/legion/extensions/agentic/inference/analogical.rb +19 -0
  30. data/lib/legion/extensions/agentic/inference/argument_mapping/client.rb +19 -0
  31. data/lib/legion/extensions/agentic/inference/argument_mapping/helpers/argument.rb +102 -0
  32. data/lib/legion/extensions/agentic/inference/argument_mapping/helpers/argument_engine.rb +131 -0
  33. data/lib/legion/extensions/agentic/inference/argument_mapping/helpers/constants.rb +44 -0
  34. data/lib/legion/extensions/agentic/inference/argument_mapping/runners/argument_mapping.rb +78 -0
  35. data/lib/legion/extensions/agentic/inference/argument_mapping/version.rb +13 -0
  36. data/lib/legion/extensions/agentic/inference/argument_mapping.rb +19 -0
  37. data/lib/legion/extensions/agentic/inference/bayesian/client.rb +28 -0
  38. data/lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb +96 -0
  39. data/lib/legion/extensions/agentic/inference/bayesian/helpers/belief_network.rb +147 -0
  40. data/lib/legion/extensions/agentic/inference/bayesian/helpers/constants.rb +36 -0
  41. data/lib/legion/extensions/agentic/inference/bayesian/runners/bayesian_belief.rb +125 -0
  42. data/lib/legion/extensions/agentic/inference/bayesian/version.rb +13 -0
  43. data/lib/legion/extensions/agentic/inference/bayesian.rb +19 -0
  44. data/lib/legion/extensions/agentic/inference/belief_revision/client.rb +19 -0
  45. data/lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb +155 -0
  46. data/lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb +193 -0
  47. data/lib/legion/extensions/agentic/inference/belief_revision/helpers/constants.rb +54 -0
  48. data/lib/legion/extensions/agentic/inference/belief_revision/helpers/evidence.rb +49 -0
  49. data/lib/legion/extensions/agentic/inference/belief_revision/runners/belief_revision.rb +89 -0
  50. data/lib/legion/extensions/agentic/inference/belief_revision/version.rb +13 -0
  51. data/lib/legion/extensions/agentic/inference/belief_revision.rb +21 -0
  52. data/lib/legion/extensions/agentic/inference/causal_attribution/client.rb +27 -0
  53. data/lib/legion/extensions/agentic/inference/causal_attribution/helpers/attribution.rb +92 -0
  54. data/lib/legion/extensions/agentic/inference/causal_attribution/helpers/attribution_engine.rb +159 -0
  55. data/lib/legion/extensions/agentic/inference/causal_attribution/runners/causal_attribution.rb +111 -0
  56. data/lib/legion/extensions/agentic/inference/causal_attribution/version.rb +13 -0
  57. data/lib/legion/extensions/agentic/inference/causal_attribution.rb +18 -0
  58. data/lib/legion/extensions/agentic/inference/causal_reasoning/client.rb +24 -0
  59. data/lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb +101 -0
  60. data/lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_graph.rb +184 -0
  61. data/lib/legion/extensions/agentic/inference/causal_reasoning/helpers/constants.rb +40 -0
  62. data/lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb +111 -0
  63. data/lib/legion/extensions/agentic/inference/causal_reasoning/version.rb +13 -0
  64. data/lib/legion/extensions/agentic/inference/causal_reasoning.rb +19 -0
  65. data/lib/legion/extensions/agentic/inference/coherence/client.rb +28 -0
  66. data/lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb +170 -0
  67. data/lib/legion/extensions/agentic/inference/coherence/helpers/constants.rb +35 -0
  68. data/lib/legion/extensions/agentic/inference/coherence/helpers/proposition.rb +100 -0
  69. data/lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb +123 -0
  70. data/lib/legion/extensions/agentic/inference/coherence/version.rb +13 -0
  71. data/lib/legion/extensions/agentic/inference/coherence.rb +19 -0
  72. data/lib/legion/extensions/agentic/inference/counterfactual/client.rb +19 -0
  73. data/lib/legion/extensions/agentic/inference/counterfactual/helpers/constants.rb +40 -0
  74. data/lib/legion/extensions/agentic/inference/counterfactual/helpers/counterfactual_engine.rb +157 -0
  75. data/lib/legion/extensions/agentic/inference/counterfactual/helpers/scenario.rb +98 -0
  76. data/lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb +106 -0
  77. data/lib/legion/extensions/agentic/inference/counterfactual/version.rb +13 -0
  78. data/lib/legion/extensions/agentic/inference/counterfactual.rb +19 -0
  79. data/lib/legion/extensions/agentic/inference/debugging/client.rb +30 -0
  80. data/lib/legion/extensions/agentic/inference/debugging/helpers/causal_trace.rb +51 -0
  81. data/lib/legion/extensions/agentic/inference/debugging/helpers/constants.rb +58 -0
  82. data/lib/legion/extensions/agentic/inference/debugging/helpers/correction.rb +56 -0
  83. data/lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb +156 -0
  84. data/lib/legion/extensions/agentic/inference/debugging/helpers/reasoning_error.rb +97 -0
  85. data/lib/legion/extensions/agentic/inference/debugging/runners/cognitive_debugging.rb +178 -0
  86. data/lib/legion/extensions/agentic/inference/debugging/version.rb +13 -0
  87. data/lib/legion/extensions/agentic/inference/debugging.rb +21 -0
  88. data/lib/legion/extensions/agentic/inference/enactive_cognition/client.rb +27 -0
  89. data/lib/legion/extensions/agentic/inference/enactive_cognition/helpers/enaction_engine.rb +120 -0
  90. data/lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb +122 -0
  91. data/lib/legion/extensions/agentic/inference/enactive_cognition/runners/enactive_cognition.rb +124 -0
  92. data/lib/legion/extensions/agentic/inference/enactive_cognition/version.rb +13 -0
  93. data/lib/legion/extensions/agentic/inference/enactive_cognition.rb +18 -0
  94. data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/client.rb +17 -0
  95. data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/constants.rb +47 -0
  96. data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb +82 -0
  97. data/lib/legion/extensions/agentic/inference/expectation_violation/helpers/violation_engine.rb +126 -0
  98. data/lib/legion/extensions/agentic/inference/expectation_violation/runners/expectation_violation.rb +76 -0
  99. data/lib/legion/extensions/agentic/inference/expectation_violation/version.rb +13 -0
  100. data/lib/legion/extensions/agentic/inference/expectation_violation.rb +19 -0
  101. data/lib/legion/extensions/agentic/inference/free_energy/client.rb +19 -0
  102. data/lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb +127 -0
  103. data/lib/legion/extensions/agentic/inference/free_energy/helpers/constants.rb +58 -0
  104. data/lib/legion/extensions/agentic/inference/free_energy/helpers/free_energy_engine.rb +156 -0
  105. data/lib/legion/extensions/agentic/inference/free_energy/runners/free_energy.rb +97 -0
  106. data/lib/legion/extensions/agentic/inference/free_energy/version.rb +13 -0
  107. data/lib/legion/extensions/agentic/inference/free_energy.rb +19 -0
  108. data/lib/legion/extensions/agentic/inference/gravity/client.rb +29 -0
  109. data/lib/legion/extensions/agentic/inference/gravity/helpers/attractor.rb +77 -0
  110. data/lib/legion/extensions/agentic/inference/gravity/helpers/constants.rb +76 -0
  111. data/lib/legion/extensions/agentic/inference/gravity/helpers/gravity_engine.rb +164 -0
  112. data/lib/legion/extensions/agentic/inference/gravity/helpers/orbiting_thought.rb +64 -0
  113. data/lib/legion/extensions/agentic/inference/gravity/runners/gravity.rb +132 -0
  114. data/lib/legion/extensions/agentic/inference/gravity/version.rb +13 -0
  115. data/lib/legion/extensions/agentic/inference/gravity.rb +20 -0
  116. data/lib/legion/extensions/agentic/inference/horizon/actors/adjust.rb +45 -0
  117. data/lib/legion/extensions/agentic/inference/horizon/client.rb +28 -0
  118. data/lib/legion/extensions/agentic/inference/horizon/helpers/constants.rb +43 -0
  119. data/lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb +110 -0
  120. data/lib/legion/extensions/agentic/inference/horizon/helpers/projection.rb +59 -0
  121. data/lib/legion/extensions/agentic/inference/horizon/runners/cognitive_horizon.rb +107 -0
  122. data/lib/legion/extensions/agentic/inference/horizon/version.rb +13 -0
  123. data/lib/legion/extensions/agentic/inference/horizon.rb +19 -0
  124. data/lib/legion/extensions/agentic/inference/hypothesis_testing/client.rb +28 -0
  125. data/lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/constants.rb +37 -0
  126. data/lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis.rb +83 -0
  127. data/lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb +97 -0
  128. data/lib/legion/extensions/agentic/inference/hypothesis_testing/runners/hypothesis_testing.rb +115 -0
  129. data/lib/legion/extensions/agentic/inference/hypothesis_testing/version.rb +13 -0
  130. data/lib/legion/extensions/agentic/inference/hypothesis_testing.rb +19 -0
  131. data/lib/legion/extensions/agentic/inference/intuition/client.rb +19 -0
  132. data/lib/legion/extensions/agentic/inference/intuition/helpers/constants.rb +60 -0
  133. data/lib/legion/extensions/agentic/inference/intuition/helpers/heuristic.rb +66 -0
  134. data/lib/legion/extensions/agentic/inference/intuition/helpers/intuition_engine.rb +157 -0
  135. data/lib/legion/extensions/agentic/inference/intuition/helpers/pattern.rb +105 -0
  136. data/lib/legion/extensions/agentic/inference/intuition/runners/intuition.rb +87 -0
  137. data/lib/legion/extensions/agentic/inference/intuition/version.rb +13 -0
  138. data/lib/legion/extensions/agentic/inference/intuition.rb +20 -0
  139. data/lib/legion/extensions/agentic/inference/magnet/client.rb +29 -0
  140. data/lib/legion/extensions/agentic/inference/magnet/helpers/constants.rb +57 -0
  141. data/lib/legion/extensions/agentic/inference/magnet/helpers/field.rb +105 -0
  142. data/lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb +179 -0
  143. data/lib/legion/extensions/agentic/inference/magnet/helpers/pole.rb +80 -0
  144. data/lib/legion/extensions/agentic/inference/magnet/runners/cognitive_magnet.rb +124 -0
  145. data/lib/legion/extensions/agentic/inference/magnet/version.rb +13 -0
  146. data/lib/legion/extensions/agentic/inference/magnet.rb +21 -0
  147. data/lib/legion/extensions/agentic/inference/momentum/helpers/client.rb +17 -0
  148. data/lib/legion/extensions/agentic/inference/momentum/helpers/constants.rb +65 -0
  149. data/lib/legion/extensions/agentic/inference/momentum/helpers/idea.rb +112 -0
  150. data/lib/legion/extensions/agentic/inference/momentum/helpers/momentum_engine.rb +127 -0
  151. data/lib/legion/extensions/agentic/inference/momentum/runners/cognitive_momentum.rb +101 -0
  152. data/lib/legion/extensions/agentic/inference/momentum/version.rb +13 -0
  153. data/lib/legion/extensions/agentic/inference/momentum.rb +19 -0
  154. data/lib/legion/extensions/agentic/inference/perceptual_inference/client.rb +28 -0
  155. data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/constants.rb +34 -0
  156. data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_field.rb +154 -0
  157. data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb +100 -0
  158. data/lib/legion/extensions/agentic/inference/perceptual_inference/runners/perceptual_inference.rb +120 -0
  159. data/lib/legion/extensions/agentic/inference/perceptual_inference/version.rb +13 -0
  160. data/lib/legion/extensions/agentic/inference/perceptual_inference.rb +19 -0
  161. data/lib/legion/extensions/agentic/inference/prediction/actors/expire_predictions.rb +45 -0
  162. data/lib/legion/extensions/agentic/inference/prediction/client.rb +27 -0
  163. data/lib/legion/extensions/agentic/inference/prediction/helpers/modes.rb +28 -0
  164. data/lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb +66 -0
  165. data/lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb +146 -0
  166. data/lib/legion/extensions/agentic/inference/prediction/version.rb +13 -0
  167. data/lib/legion/extensions/agentic/inference/prediction.rb +18 -0
  168. data/lib/legion/extensions/agentic/inference/predictive_coding/actors/decay.rb +45 -0
  169. data/lib/legion/extensions/agentic/inference/predictive_coding/client.rb +28 -0
  170. data/lib/legion/extensions/agentic/inference/predictive_coding/helpers/constants.rb +46 -0
  171. data/lib/legion/extensions/agentic/inference/predictive_coding/helpers/generative_model.rb +187 -0
  172. data/lib/legion/extensions/agentic/inference/predictive_coding/helpers/prediction_error.rb +59 -0
  173. data/lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb +171 -0
  174. data/lib/legion/extensions/agentic/inference/predictive_coding/version.rb +13 -0
  175. data/lib/legion/extensions/agentic/inference/predictive_coding.rb +20 -0
  176. data/lib/legion/extensions/agentic/inference/predictive_processing/client.rb +28 -0
  177. data/lib/legion/extensions/agentic/inference/predictive_processing/helpers/constants.rb +35 -0
  178. data/lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb +142 -0
  179. data/lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb +129 -0
  180. data/lib/legion/extensions/agentic/inference/predictive_processing/runners/predictive_processing.rb +104 -0
  181. data/lib/legion/extensions/agentic/inference/predictive_processing/version.rb +13 -0
  182. data/lib/legion/extensions/agentic/inference/predictive_processing.rb +19 -0
  183. data/lib/legion/extensions/agentic/inference/reality_testing/client.rb +28 -0
  184. data/lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb +98 -0
  185. data/lib/legion/extensions/agentic/inference/reality_testing/helpers/constants.rb +41 -0
  186. data/lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb +104 -0
  187. data/lib/legion/extensions/agentic/inference/reality_testing/runners/reality_testing.rb +94 -0
  188. data/lib/legion/extensions/agentic/inference/reality_testing/version.rb +13 -0
  189. data/lib/legion/extensions/agentic/inference/reality_testing.rb +19 -0
  190. data/lib/legion/extensions/agentic/inference/schema/client.rb +26 -0
  191. data/lib/legion/extensions/agentic/inference/schema/helpers/causal_relation.rb +70 -0
  192. data/lib/legion/extensions/agentic/inference/schema/helpers/constants.rb +50 -0
  193. data/lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb +173 -0
  194. data/lib/legion/extensions/agentic/inference/schema/runners/schema.rb +101 -0
  195. data/lib/legion/extensions/agentic/inference/schema/version.rb +13 -0
  196. data/lib/legion/extensions/agentic/inference/schema.rb +19 -0
  197. data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/client.rb +28 -0
  198. data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/constants.rb +42 -0
  199. data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/decision.rb +66 -0
  200. data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb +139 -0
  201. data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/runners/uncertainty_tolerance.rb +129 -0
  202. data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/version.rb +13 -0
  203. data/lib/legion/extensions/agentic/inference/uncertainty_tolerance.rb +19 -0
  204. data/lib/legion/extensions/agentic/inference/version.rb +11 -0
  205. data/lib/legion/extensions/agentic/inference.rb +44 -0
  206. data/spec/legion/extensions/agentic/inference/abductive/client_spec.rb +25 -0
  207. data/spec/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning_spec.rb +349 -0
  208. data/spec/legion/extensions/agentic/inference/affordance/client_spec.rb +26 -0
  209. data/spec/legion/extensions/agentic/inference/affordance/helpers/affordance_field_spec.rb +131 -0
  210. data/spec/legion/extensions/agentic/inference/affordance/helpers/affordance_item_spec.rb +107 -0
  211. data/spec/legion/extensions/agentic/inference/affordance/runners/affordance_spec.rb +78 -0
  212. data/spec/legion/extensions/agentic/inference/analogical/client_spec.rb +31 -0
  213. data/spec/legion/extensions/agentic/inference/analogical/helpers/analogy_engine_spec.rb +276 -0
  214. data/spec/legion/extensions/agentic/inference/analogical/helpers/structure_map_spec.rb +255 -0
  215. data/spec/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning_spec.rb +213 -0
  216. data/spec/legion/extensions/agentic/inference/argument_mapping/client_spec.rb +18 -0
  217. data/spec/legion/extensions/agentic/inference/argument_mapping/helpers/argument_engine_spec.rb +218 -0
  218. data/spec/legion/extensions/agentic/inference/argument_mapping/helpers/argument_spec.rb +231 -0
  219. data/spec/legion/extensions/agentic/inference/argument_mapping/runners/argument_mapping_spec.rb +171 -0
  220. data/spec/legion/extensions/agentic/inference/bayesian/client_spec.rb +20 -0
  221. data/spec/legion/extensions/agentic/inference/bayesian/helpers/belief_network_spec.rb +178 -0
  222. data/spec/legion/extensions/agentic/inference/bayesian/helpers/belief_spec.rb +137 -0
  223. data/spec/legion/extensions/agentic/inference/bayesian/runners/bayesian_belief_spec.rb +176 -0
  224. data/spec/legion/extensions/agentic/inference/belief_revision/client_spec.rb +31 -0
  225. data/spec/legion/extensions/agentic/inference/belief_revision/helpers/belief_network_spec.rb +176 -0
  226. data/spec/legion/extensions/agentic/inference/belief_revision/helpers/belief_spec.rb +153 -0
  227. data/spec/legion/extensions/agentic/inference/belief_revision/helpers/evidence_spec.rb +51 -0
  228. data/spec/legion/extensions/agentic/inference/belief_revision/runners/belief_revision_spec.rb +106 -0
  229. data/spec/legion/extensions/agentic/inference/causal_attribution/client_spec.rb +24 -0
  230. data/spec/legion/extensions/agentic/inference/causal_attribution/helpers/attribution_engine_spec.rb +181 -0
  231. data/spec/legion/extensions/agentic/inference/causal_attribution/helpers/attribution_spec.rb +108 -0
  232. data/spec/legion/extensions/agentic/inference/causal_attribution/runners/causal_attribution_spec.rb +142 -0
  233. data/spec/legion/extensions/agentic/inference/causal_reasoning/client_spec.rb +35 -0
  234. data/spec/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge_spec.rb +158 -0
  235. data/spec/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_graph_spec.rb +259 -0
  236. data/spec/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning_spec.rb +161 -0
  237. data/spec/legion/extensions/agentic/inference/coherence/client_spec.rb +17 -0
  238. data/spec/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence_spec.rb +267 -0
  239. data/spec/legion/extensions/agentic/inference/counterfactual/client_spec.rb +48 -0
  240. data/spec/legion/extensions/agentic/inference/counterfactual/helpers/constants_spec.rb +55 -0
  241. data/spec/legion/extensions/agentic/inference/counterfactual/helpers/counterfactual_engine_spec.rb +234 -0
  242. data/spec/legion/extensions/agentic/inference/counterfactual/helpers/scenario_spec.rb +193 -0
  243. data/spec/legion/extensions/agentic/inference/counterfactual/runners/counterfactual_spec.rb +179 -0
  244. data/spec/legion/extensions/agentic/inference/debugging/client_spec.rb +46 -0
  245. data/spec/legion/extensions/agentic/inference/debugging/helpers/causal_trace_spec.rb +84 -0
  246. data/spec/legion/extensions/agentic/inference/debugging/helpers/constants_spec.rb +97 -0
  247. data/spec/legion/extensions/agentic/inference/debugging/helpers/correction_spec.rb +98 -0
  248. data/spec/legion/extensions/agentic/inference/debugging/helpers/debugging_engine_spec.rb +290 -0
  249. data/spec/legion/extensions/agentic/inference/debugging/helpers/reasoning_error_spec.rb +164 -0
  250. data/spec/legion/extensions/agentic/inference/debugging/runners/cognitive_debugging_spec.rb +301 -0
  251. data/spec/legion/extensions/agentic/inference/enactive_cognition/client_spec.rb +19 -0
  252. data/spec/legion/extensions/agentic/inference/enactive_cognition/helpers/enaction_engine_spec.rb +181 -0
  253. data/spec/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop_spec.rb +184 -0
  254. data/spec/legion/extensions/agentic/inference/enactive_cognition/runners/enactive_cognition_spec.rb +214 -0
  255. data/spec/legion/extensions/agentic/inference/expectation_violation/expectation_violation_spec.rb +11 -0
  256. data/spec/legion/extensions/agentic/inference/expectation_violation/helpers/expectation_spec.rb +102 -0
  257. data/spec/legion/extensions/agentic/inference/expectation_violation/helpers/violation_engine_spec.rb +121 -0
  258. data/spec/legion/extensions/agentic/inference/expectation_violation/runners/expectation_violation_spec.rb +59 -0
  259. data/spec/legion/extensions/agentic/inference/free_energy/client_spec.rb +46 -0
  260. data/spec/legion/extensions/agentic/inference/free_energy/helpers/belief_spec.rb +183 -0
  261. data/spec/legion/extensions/agentic/inference/free_energy/helpers/free_energy_engine_spec.rb +211 -0
  262. data/spec/legion/extensions/agentic/inference/free_energy/runners/free_energy_spec.rb +118 -0
  263. data/spec/legion/extensions/agentic/inference/gravity/client_spec.rb +24 -0
  264. data/spec/legion/extensions/agentic/inference/gravity/helpers/attractor_spec.rb +143 -0
  265. data/spec/legion/extensions/agentic/inference/gravity/helpers/constants_spec.rb +107 -0
  266. data/spec/legion/extensions/agentic/inference/gravity/helpers/gravity_engine_spec.rb +193 -0
  267. data/spec/legion/extensions/agentic/inference/gravity/helpers/orbiting_thought_spec.rb +103 -0
  268. data/spec/legion/extensions/agentic/inference/gravity/runners/gravity_spec.rb +159 -0
  269. data/spec/legion/extensions/agentic/inference/horizon/client_spec.rb +58 -0
  270. data/spec/legion/extensions/agentic/inference/horizon/helpers/constants_spec.rb +98 -0
  271. data/spec/legion/extensions/agentic/inference/horizon/helpers/horizon_engine_spec.rb +325 -0
  272. data/spec/legion/extensions/agentic/inference/horizon/helpers/projection_spec.rb +155 -0
  273. data/spec/legion/extensions/agentic/inference/horizon/runners/cognitive_horizon_spec.rb +269 -0
  274. data/spec/legion/extensions/agentic/inference/hypothesis_testing/helpers/constants_spec.rb +38 -0
  275. data/spec/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine_spec.rb +182 -0
  276. data/spec/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_spec.rb +172 -0
  277. data/spec/legion/extensions/agentic/inference/hypothesis_testing/hypothesis_testing_spec.rb +16 -0
  278. data/spec/legion/extensions/agentic/inference/hypothesis_testing/runners/hypothesis_testing_spec.rb +159 -0
  279. data/spec/legion/extensions/agentic/inference/intuition/client_spec.rb +33 -0
  280. data/spec/legion/extensions/agentic/inference/intuition/helpers/heuristic_spec.rb +82 -0
  281. data/spec/legion/extensions/agentic/inference/intuition/helpers/intuition_engine_spec.rb +163 -0
  282. data/spec/legion/extensions/agentic/inference/intuition/helpers/pattern_spec.rb +160 -0
  283. data/spec/legion/extensions/agentic/inference/intuition/runners/intuition_spec.rb +107 -0
  284. data/spec/legion/extensions/agentic/inference/magnet/client_spec.rb +30 -0
  285. data/spec/legion/extensions/agentic/inference/magnet/helpers/constants_spec.rb +120 -0
  286. data/spec/legion/extensions/agentic/inference/magnet/helpers/field_spec.rb +193 -0
  287. data/spec/legion/extensions/agentic/inference/magnet/helpers/magnet_engine_spec.rb +281 -0
  288. data/spec/legion/extensions/agentic/inference/magnet/helpers/pole_spec.rb +211 -0
  289. data/spec/legion/extensions/agentic/inference/magnet/runners/cognitive_magnet_spec.rb +201 -0
  290. data/spec/legion/extensions/agentic/inference/momentum/cognitive_momentum_spec.rb +11 -0
  291. data/spec/legion/extensions/agentic/inference/momentum/helpers/idea_spec.rb +152 -0
  292. data/spec/legion/extensions/agentic/inference/momentum/helpers/momentum_engine_spec.rb +154 -0
  293. data/spec/legion/extensions/agentic/inference/momentum/runners/cognitive_momentum_spec.rb +97 -0
  294. data/spec/legion/extensions/agentic/inference/perceptual_inference/client_spec.rb +39 -0
  295. data/spec/legion/extensions/agentic/inference/perceptual_inference/helpers/constants_spec.rb +97 -0
  296. data/spec/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_field_spec.rb +270 -0
  297. data/spec/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis_spec.rb +206 -0
  298. data/spec/legion/extensions/agentic/inference/perceptual_inference/runners/perceptual_inference_spec.rb +305 -0
  299. data/spec/legion/extensions/agentic/inference/prediction/actors/expire_predictions_spec.rb +46 -0
  300. data/spec/legion/extensions/agentic/inference/prediction/client_spec.rb +14 -0
  301. data/spec/legion/extensions/agentic/inference/prediction/helpers/modes_spec.rb +118 -0
  302. data/spec/legion/extensions/agentic/inference/prediction/helpers/prediction_store_spec.rb +262 -0
  303. data/spec/legion/extensions/agentic/inference/prediction/runners/prediction_spec.rb +116 -0
  304. data/spec/legion/extensions/agentic/inference/predictive_coding/client_spec.rb +74 -0
  305. data/spec/legion/extensions/agentic/inference/predictive_coding/helpers/generative_model_spec.rb +194 -0
  306. data/spec/legion/extensions/agentic/inference/predictive_coding/helpers/prediction_error_spec.rb +109 -0
  307. data/spec/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding_spec.rb +210 -0
  308. data/spec/legion/extensions/agentic/inference/predictive_processing/client_spec.rb +82 -0
  309. data/spec/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model_spec.rb +220 -0
  310. data/spec/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor_spec.rb +206 -0
  311. data/spec/legion/extensions/agentic/inference/predictive_processing/runners/predictive_processing_spec.rb +213 -0
  312. data/spec/legion/extensions/agentic/inference/reality_testing/client_spec.rb +29 -0
  313. data/spec/legion/extensions/agentic/inference/reality_testing/helpers/belief_spec.rb +197 -0
  314. data/spec/legion/extensions/agentic/inference/reality_testing/helpers/constants_spec.rb +78 -0
  315. data/spec/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine_spec.rb +191 -0
  316. data/spec/legion/extensions/agentic/inference/reality_testing/runners/reality_testing_spec.rb +154 -0
  317. data/spec/legion/extensions/agentic/inference/schema/client_spec.rb +53 -0
  318. data/spec/legion/extensions/agentic/inference/schema/helpers/causal_relation_spec.rb +108 -0
  319. data/spec/legion/extensions/agentic/inference/schema/helpers/constants_spec.rb +54 -0
  320. data/spec/legion/extensions/agentic/inference/schema/helpers/world_model_spec.rb +179 -0
  321. data/spec/legion/extensions/agentic/inference/schema/runners/schema_spec.rb +146 -0
  322. data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/client_spec.rb +18 -0
  323. data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/constants_spec.rb +62 -0
  324. data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/decision_spec.rb +125 -0
  325. data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine_spec.rb +184 -0
  326. data/spec/legion/extensions/agentic/inference/uncertainty_tolerance/runners/uncertainty_tolerance_spec.rb +157 -0
  327. data/spec/spec_helper.rb +46 -0
  328. 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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module Schema
8
+ VERSION = '0.1.0'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ 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