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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6dd00ebbcc35eceb42f311a3fe5a3eb05cd85dfe202b25e990aa9e763c767e9a
4
+ data.tar.gz: e77d26da91244a176b5a784838b5cc39e6e98d7e24db1e8670c632fce8cde8e3
5
+ SHA512:
6
+ metadata.gz: fda03d8791a5668dedc78b0017abb410b75d941d309d594e8cf46ac96b9d6e81ae6324e3fe2b71ebe89de8244771905330e84e20ea914bdec6fa964738fc63e2
7
+ data.tar.gz: 7a971543bc03ec39d07f1165428bcebccb49c9b9d5c938530b9aa6593752f204f6e9a4f83c50b1baed2f422790bd2fffaab50703212f931c3cc81b49c058c7d1
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ## [0.1.0] - 2026-03-18
6
+
7
+ ### Added
8
+ - Initial release as domain consolidation gem
9
+ - Consolidated source extensions into unified domain gem under `Legion::Extensions::Agentic::<Domain>`
10
+ - All sub-modules loaded from single entry point
11
+ - Full spec suite with zero failures
12
+ - RuboCop compliance across all files
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matthew Iverson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # lex-agentic-inference
2
+
3
+ LEX agentic inference domain: causal reasoning, prediction, belief updating
4
+
5
+ ## Sub-modules
6
+
7
+ *(populated as extensions are consolidated)*
8
+
9
+ ## Installation
10
+
11
+ ```ruby
12
+ gem 'lex-agentic-inference'
13
+ ```
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/legion/extensions/agentic/inference/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'lex-agentic-inference'
7
+ spec.version = Legion::Extensions::Agentic::Inference::VERSION
8
+ spec.authors = ['Esity']
9
+ spec.email = ['matthewdiverson@gmail.com']
10
+
11
+ spec.summary = 'LEX Agentic Inference'
12
+ spec.description = 'LEX agentic inference domain: causal reasoning, prediction, belief updating'
13
+ spec.homepage = 'https://github.com/LegionIO/lex-agentic-inference'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 3.4'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
20
+ spec.metadata['rubygems_mfa_required'] = 'true'
21
+
22
+ spec.files = Dir.chdir(__dir__) do
23
+ Dir.glob('{lib,spec}/**/*.rb') + %w[lex-agentic-inference.gemspec Gemfile LICENSE README.md CHANGELOG.md]
24
+ end
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'rspec', '~> 3.13'
28
+ spec.add_development_dependency 'rubocop', '~> 1.60'
29
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.26'
30
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/agentic/inference/abductive/helpers/constants'
4
+ require 'legion/extensions/agentic/inference/abductive/helpers/observation'
5
+ require 'legion/extensions/agentic/inference/abductive/helpers/hypothesis'
6
+ require 'legion/extensions/agentic/inference/abductive/helpers/abduction_engine'
7
+ require 'legion/extensions/agentic/inference/abductive/runners/abductive_reasoning'
8
+
9
+ module Legion
10
+ module Extensions
11
+ module Agentic
12
+ module Inference
13
+ module Abductive
14
+ class Client
15
+ include Runners::AbductiveReasoning
16
+
17
+ def initialize(engine: nil)
18
+ @engine = engine || Helpers::AbductionEngine.new
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module Abductive
8
+ module Helpers
9
+ class AbductionEngine
10
+ include Constants
11
+
12
+ def initialize
13
+ @observations = {}
14
+ @hypotheses = {}
15
+ end
16
+
17
+ def record_observation(content:, domain:, surprise_level: :notable, context: {})
18
+ obs = Observation.new(
19
+ content: content,
20
+ domain: domain,
21
+ surprise_level: surprise_level,
22
+ context: context
23
+ )
24
+ prune_observations if @observations.size >= Constants::MAX_OBSERVATIONS
25
+ @observations[obs.id] = obs
26
+ obs
27
+ end
28
+
29
+ def generate_hypothesis(content:, observation_ids:, domain:, simplicity:,
30
+ explanatory_power:, prior_probability: Constants::DEFAULT_PLAUSIBILITY)
31
+ hyp = Hypothesis.new(
32
+ content: content,
33
+ observation_ids: observation_ids,
34
+ domain: domain,
35
+ simplicity: simplicity,
36
+ explanatory_power: explanatory_power,
37
+ prior_probability: prior_probability
38
+ )
39
+ prune_hypotheses if @hypotheses.size >= Constants::MAX_HYPOTHESES
40
+ @hypotheses[hyp.id] = hyp
41
+ hyp
42
+ end
43
+
44
+ def evaluate_hypothesis(hypothesis_id:)
45
+ hyp = @hypotheses[hypothesis_id]
46
+ return { found: false } unless hyp
47
+
48
+ hyp.instance_variable_set(:@last_evaluated_at, Time.now.utc)
49
+ ranked = ranked_hypotheses_for_observations(hyp.observation_ids)
50
+ rank = ranked.index { |h| h.id == hypothesis_id }.to_i + 1
51
+
52
+ {
53
+ score: hyp.overall_score,
54
+ rank: rank,
55
+ quality_label: hyp.quality_label
56
+ }
57
+ end
58
+
59
+ def add_evidence(hypothesis_id:, supporting:)
60
+ hyp = @hypotheses[hypothesis_id]
61
+ return { found: false } unless hyp
62
+
63
+ hyp.add_evidence(supporting: supporting)
64
+ { found: true, hypothesis_id: hypothesis_id, state: hyp.state, plausibility: hyp.plausibility }
65
+ end
66
+
67
+ def best_explanation(observation_id:)
68
+ candidates = active_hypotheses_for(observation_id)
69
+ return nil if candidates.empty?
70
+
71
+ candidates.max_by(&:overall_score)
72
+ end
73
+
74
+ def competing_hypotheses(observation_id:)
75
+ active_hypotheses_for(observation_id).sort_by { |h| -h.overall_score }
76
+ end
77
+
78
+ def refute_hypothesis(hypothesis_id:)
79
+ hyp = @hypotheses[hypothesis_id]
80
+ return { found: false } unless hyp
81
+
82
+ hyp.refute!
83
+ { found: true, hypothesis_id: hypothesis_id, state: hyp.state }
84
+ end
85
+
86
+ def find_by_domain(domain:)
87
+ @hypotheses.values.select { |h| h.domain == domain && h.state != :refuted }
88
+ end
89
+
90
+ def unexplained_observations
91
+ @observations.values.reject do |obs|
92
+ @hypotheses.values.any? do |h|
93
+ h.state == :supported && h.observation_ids.include?(obs.id)
94
+ end
95
+ end
96
+ end
97
+
98
+ def decay_stale
99
+ cutoff = Time.now.utc - Constants::STALE_THRESHOLD
100
+ decayed = 0
101
+ @hypotheses.each_value do |hyp|
102
+ next if hyp.state == :refuted
103
+ next if hyp.last_evaluated_at >= cutoff
104
+
105
+ hyp.plausibility = (hyp.plausibility - Constants::DECAY_RATE).clamp(
106
+ Constants::PLAUSIBILITY_FLOOR,
107
+ Constants::PLAUSIBILITY_CEILING
108
+ )
109
+ decayed += 1
110
+ end
111
+ decayed
112
+ end
113
+
114
+ def prune_refuted
115
+ before = @hypotheses.size
116
+ @hypotheses.delete_if { |_, h| h.state == :refuted }
117
+ before - @hypotheses.size
118
+ end
119
+
120
+ def to_h
121
+ {
122
+ observation_count: @observations.size,
123
+ hypothesis_count: @hypotheses.size,
124
+ supported_count: @hypotheses.values.count { |h| h.state == :supported },
125
+ refuted_count: @hypotheses.values.count { |h| h.state == :refuted },
126
+ candidate_count: @hypotheses.values.count { |h| h.state == :candidate },
127
+ unexplained_count: unexplained_observations.size
128
+ }
129
+ end
130
+
131
+ private
132
+
133
+ def active_hypotheses_for(observation_id)
134
+ @hypotheses.values.select do |h|
135
+ h.state != :refuted && h.observation_ids.include?(observation_id)
136
+ end
137
+ end
138
+
139
+ def ranked_hypotheses_for_observations(observation_ids)
140
+ obs_set = Array(observation_ids)
141
+ @hypotheses.values
142
+ .select { |h| h.state != :refuted && h.observation_ids.intersect?(obs_set) }
143
+ .sort_by { |h| -h.overall_score }
144
+ end
145
+
146
+ def prune_observations
147
+ sorted = @observations.values.sort_by(&:created_at)
148
+ excess = @observations.size - Constants::MAX_OBSERVATIONS + 1
149
+ sorted.first(excess).each { |obs| @observations.delete(obs.id) }
150
+ end
151
+
152
+ def prune_hypotheses
153
+ candidates = @hypotheses.values.select { |h| h.state == :candidate }
154
+ sorted = candidates.sort_by(&:created_at)
155
+ excess = @hypotheses.size - Constants::MAX_HYPOTHESES + 1
156
+ sorted.first(excess).each { |h| @hypotheses.delete(h.id) }
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module Abductive
8
+ module Helpers
9
+ module Constants
10
+ MAX_OBSERVATIONS = 200
11
+ MAX_HYPOTHESES = 100
12
+ MAX_EXPLANATIONS = 500
13
+ MAX_HISTORY = 300
14
+ DEFAULT_PLAUSIBILITY = 0.5
15
+ PLAUSIBILITY_FLOOR = 0.0
16
+ PLAUSIBILITY_CEILING = 1.0
17
+ SIMPLICITY_WEIGHT = 0.3
18
+ EXPLANATORY_POWER_WEIGHT = 0.4
19
+ PRIOR_WEIGHT = 0.3
20
+ EVIDENCE_BOOST = 0.1
21
+ CONTRADICTION_PENALTY = 0.2
22
+ DECAY_RATE = 0.02
23
+ STALE_THRESHOLD = 120
24
+ SURPRISE_LEVELS = %i[trivial expected notable surprising shocking].freeze
25
+ HYPOTHESIS_STATES = %i[candidate supported refuted].freeze
26
+ QUALITY_LABELS = {
27
+ (0.8..) => :compelling,
28
+ (0.6...0.8) => :plausible,
29
+ (0.4...0.6) => :possible,
30
+ (0.2...0.4) => :weak,
31
+ (..0.2) => :implausible
32
+ }.freeze
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Legion
6
+ module Extensions
7
+ module Agentic
8
+ module Inference
9
+ module Abductive
10
+ module Helpers
11
+ class Hypothesis
12
+ include Constants
13
+
14
+ attr_reader :id, :content, :observation_ids, :domain,
15
+ :simplicity, :explanatory_power, :prior_probability,
16
+ :evidence_for, :evidence_against, :state,
17
+ :created_at, :last_evaluated_at
18
+ attr_accessor :plausibility
19
+
20
+ SUPPORT_EVIDENCE_THRESHOLD = 3
21
+
22
+ def initialize(content:, observation_ids:, domain:, simplicity:, explanatory_power:,
23
+ prior_probability: Constants::DEFAULT_PLAUSIBILITY)
24
+ @id = SecureRandom.uuid
25
+ @content = content
26
+ @observation_ids = Array(observation_ids)
27
+ @domain = domain
28
+ @plausibility = prior_probability
29
+ @simplicity = simplicity
30
+ @explanatory_power = explanatory_power
31
+ @prior_probability = prior_probability
32
+ @evidence_for = 0
33
+ @evidence_against = 0
34
+ @state = :candidate
35
+ @created_at = Time.now.utc
36
+ @last_evaluated_at = Time.now.utc
37
+ end
38
+
39
+ def overall_score
40
+ (Constants::SIMPLICITY_WEIGHT * @simplicity) +
41
+ (Constants::EXPLANATORY_POWER_WEIGHT * @explanatory_power) +
42
+ (Constants::PRIOR_WEIGHT * @prior_probability)
43
+ end
44
+
45
+ def add_evidence(supporting:)
46
+ @last_evaluated_at = Time.now.utc
47
+ if supporting
48
+ @evidence_for += 1
49
+ @plausibility = (@plausibility + Constants::EVIDENCE_BOOST).clamp(
50
+ Constants::PLAUSIBILITY_FLOOR,
51
+ Constants::PLAUSIBILITY_CEILING
52
+ )
53
+ else
54
+ @evidence_against += 1
55
+ @plausibility = (@plausibility - Constants::CONTRADICTION_PENALTY).clamp(
56
+ Constants::PLAUSIBILITY_FLOOR,
57
+ Constants::PLAUSIBILITY_CEILING
58
+ )
59
+ end
60
+ support! if @evidence_for >= SUPPORT_EVIDENCE_THRESHOLD && @state == :candidate
61
+ end
62
+
63
+ def refute!
64
+ @state = :refuted
65
+ @last_evaluated_at = Time.now.utc
66
+ end
67
+
68
+ def support!
69
+ @state = :supported
70
+ @last_evaluated_at = Time.now.utc
71
+ end
72
+
73
+ def quality_label
74
+ score = overall_score
75
+ Constants::QUALITY_LABELS.each do |range, label|
76
+ return label if range.include?(score)
77
+ end
78
+ :implausible
79
+ end
80
+
81
+ def to_h
82
+ {
83
+ id: @id,
84
+ content: @content,
85
+ observation_ids: @observation_ids,
86
+ domain: @domain,
87
+ plausibility: @plausibility,
88
+ simplicity: @simplicity,
89
+ explanatory_power: @explanatory_power,
90
+ prior_probability: @prior_probability,
91
+ evidence_for: @evidence_for,
92
+ evidence_against: @evidence_against,
93
+ state: @state,
94
+ overall_score: overall_score,
95
+ quality_label: quality_label,
96
+ created_at: @created_at,
97
+ last_evaluated_at: @last_evaluated_at
98
+ }
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Legion
6
+ module Extensions
7
+ module Agentic
8
+ module Inference
9
+ module Abductive
10
+ module Helpers
11
+ class Observation
12
+ attr_reader :id, :content, :domain, :surprise_level, :context, :created_at
13
+
14
+ def initialize(content:, domain:, surprise_level: :notable, context: {})
15
+ @id = SecureRandom.uuid
16
+ @content = content
17
+ @domain = domain
18
+ @surprise_level = surprise_level
19
+ @context = context
20
+ @created_at = Time.now.utc
21
+ end
22
+
23
+ def to_h
24
+ {
25
+ id: @id,
26
+ content: @content,
27
+ domain: @domain,
28
+ surprise_level: @surprise_level,
29
+ context: @context,
30
+ created_at: @created_at
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module Abductive
8
+ module Runners
9
+ module AbductiveReasoning
10
+ include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
11
+ Legion::Extensions::Helpers.const_defined?(:Lex)
12
+
13
+ def record_observation(content:, domain:, surprise_level: :notable, context: {}, **)
14
+ unless Helpers::Constants::SURPRISE_LEVELS.include?(surprise_level)
15
+ return { success: false, error: :invalid_surprise_level,
16
+ valid_levels: Helpers::Constants::SURPRISE_LEVELS }
17
+ end
18
+
19
+ obs = engine.record_observation(
20
+ content: content,
21
+ domain: domain,
22
+ surprise_level: surprise_level,
23
+ context: context
24
+ )
25
+ Legion::Logging.debug "[abductive_reasoning] observation recorded: id=#{obs.id[0..7]} " \
26
+ "domain=#{domain} surprise=#{surprise_level}"
27
+ { success: true, observation: obs.to_h }
28
+ end
29
+
30
+ def generate_hypothesis(content:, observation_ids:, domain:, simplicity:,
31
+ explanatory_power:, prior_probability: nil, **)
32
+ prior = prior_probability || Helpers::Constants::DEFAULT_PLAUSIBILITY
33
+ hyp = engine.generate_hypothesis(
34
+ content: content,
35
+ observation_ids: observation_ids,
36
+ domain: domain,
37
+ simplicity: simplicity,
38
+ explanatory_power: explanatory_power,
39
+ prior_probability: prior
40
+ )
41
+ Legion::Logging.debug "[abductive_reasoning] hypothesis generated: id=#{hyp.id[0..7]} " \
42
+ "domain=#{domain} score=#{hyp.overall_score.round(3)}"
43
+ { success: true, hypothesis: hyp.to_h }
44
+ end
45
+
46
+ def evaluate_hypothesis(hypothesis_id:, **)
47
+ result = engine.evaluate_hypothesis(hypothesis_id: hypothesis_id)
48
+ if result[:found] == false
49
+ Legion::Logging.debug "[abductive_reasoning] evaluate: not found id=#{hypothesis_id[0..7]}"
50
+ return { success: false, error: :not_found }
51
+ end
52
+
53
+ Legion::Logging.debug "[abductive_reasoning] evaluate: id=#{hypothesis_id[0..7]} " \
54
+ "score=#{result[:score].round(3)} rank=#{result[:rank]} label=#{result[:quality_label]}"
55
+ { success: true }.merge(result)
56
+ end
57
+
58
+ def add_hypothesis_evidence(hypothesis_id:, supporting:, **)
59
+ result = engine.add_evidence(hypothesis_id: hypothesis_id, supporting: supporting)
60
+ if result[:found] == false
61
+ Legion::Logging.debug "[abductive_reasoning] add_evidence: not found id=#{hypothesis_id[0..7]}"
62
+ return { success: false, error: :not_found }
63
+ end
64
+
65
+ Legion::Logging.debug "[abductive_reasoning] evidence added: id=#{hypothesis_id[0..7]} " \
66
+ "supporting=#{supporting} state=#{result[:state]}"
67
+ { success: true }.merge(result)
68
+ end
69
+
70
+ def best_explanation(observation_id:, **)
71
+ hyp = engine.best_explanation(observation_id: observation_id)
72
+ if hyp
73
+ Legion::Logging.debug "[abductive_reasoning] best_explanation: obs=#{observation_id[0..7]} " \
74
+ "hyp=#{hyp.id[0..7]} score=#{hyp.overall_score.round(3)}"
75
+ { success: true, found: true, hypothesis: hyp.to_h }
76
+ else
77
+ Legion::Logging.debug "[abductive_reasoning] best_explanation: obs=#{observation_id[0..7]} none found"
78
+ { success: true, found: false }
79
+ end
80
+ end
81
+
82
+ def competing_hypotheses(observation_id:, **)
83
+ ranked = engine.competing_hypotheses(observation_id: observation_id)
84
+ Legion::Logging.debug "[abductive_reasoning] competing_hypotheses: obs=#{observation_id[0..7]} count=#{ranked.size}"
85
+ { success: true, hypotheses: ranked.map(&:to_h), count: ranked.size }
86
+ end
87
+
88
+ def refute_hypothesis(hypothesis_id:, **)
89
+ result = engine.refute_hypothesis(hypothesis_id: hypothesis_id)
90
+ if result[:found] == false
91
+ Legion::Logging.debug "[abductive_reasoning] refute: not found id=#{hypothesis_id[0..7]}"
92
+ return { success: false, error: :not_found }
93
+ end
94
+
95
+ Legion::Logging.debug "[abductive_reasoning] hypothesis refuted: id=#{hypothesis_id[0..7]}"
96
+ { success: true }.merge(result)
97
+ end
98
+
99
+ def unexplained_observations(**)
100
+ observations = engine.unexplained_observations
101
+ Legion::Logging.debug "[abductive_reasoning] unexplained_observations: count=#{observations.size}"
102
+ { success: true, observations: observations.map(&:to_h), count: observations.size }
103
+ end
104
+
105
+ def update_abductive_reasoning(**)
106
+ decayed = engine.decay_stale
107
+ pruned = engine.prune_refuted
108
+ Legion::Logging.debug "[abductive_reasoning] update cycle: decayed=#{decayed} pruned=#{pruned}"
109
+ { success: true, decayed: decayed, pruned: pruned }
110
+ end
111
+
112
+ def abductive_reasoning_stats(**)
113
+ stats = engine.to_h
114
+ Legion::Logging.debug "[abductive_reasoning] stats: #{stats.inspect}"
115
+ { success: true }.merge(stats)
116
+ end
117
+
118
+ private
119
+
120
+ def engine
121
+ @engine ||= Helpers::AbductionEngine.new
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ 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 Abductive
8
+ VERSION = '0.1.0'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/agentic/inference/abductive/version'
4
+ require 'legion/extensions/agentic/inference/abductive/helpers/constants'
5
+ require 'legion/extensions/agentic/inference/abductive/helpers/observation'
6
+ require 'legion/extensions/agentic/inference/abductive/helpers/hypothesis'
7
+ require 'legion/extensions/agentic/inference/abductive/helpers/abduction_engine'
8
+ require 'legion/extensions/agentic/inference/abductive/runners/abductive_reasoning'
9
+ require 'legion/extensions/agentic/inference/abductive/client'
10
+
11
+ module Legion
12
+ module Extensions
13
+ module Agentic
14
+ module Inference
15
+ module Abductive
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module Affordance
8
+ module Actor
9
+ class Scan < Legion::Extensions::Actors::Every
10
+ def time
11
+ 30
12
+ end
13
+
14
+ def use_runner?
15
+ false
16
+ end
17
+
18
+ def runner_function
19
+ :update_affordances
20
+ end
21
+
22
+ def runner_class
23
+ Legion::Extensions::Agentic::Inference::Affordance::Runners::Affordance
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end