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,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module FreeEnergy
8
+ module Helpers
9
+ class Belief
10
+ include Constants
11
+
12
+ attr_reader :id, :domain, :content, :precision, :prediction,
13
+ :last_observation, :prediction_error, :created_at, :updated_at
14
+
15
+ def initialize(id:, content:, domain: :general, prediction: {}, precision: DEFAULT_PRECISION)
16
+ @id = id
17
+ @content = content
18
+ @domain = domain
19
+ @prediction = prediction
20
+ @precision = precision.clamp(PRECISION_FLOOR, PRECISION_CEILING)
21
+ @last_observation = nil
22
+ @prediction_error = 0.0
23
+ @created_at = Time.now.utc
24
+ @updated_at = @created_at
25
+ end
26
+
27
+ def observe(observation:)
28
+ @last_observation = observation
29
+ @prediction_error = compute_error(observation)
30
+ update_precision
31
+ @updated_at = Time.now.utc
32
+ self
33
+ end
34
+
35
+ def free_energy
36
+ @prediction_error * @precision
37
+ end
38
+
39
+ def surprising?
40
+ free_energy > FREE_ENERGY_THRESHOLD
41
+ end
42
+
43
+ def surprise_label
44
+ fe = free_energy
45
+ SURPRISE_LABELS.find { |range, _| range.cover?(fe) }&.last || :trivial
46
+ end
47
+
48
+ def revise_prediction(observation:)
49
+ merged = @prediction.dup
50
+ observation.each do |key, value|
51
+ current = merged[key]
52
+ merged[key] = if current.is_a?(Numeric) && value.is_a?(Numeric)
53
+ current + (LEARNING_RATE * (value - current))
54
+ else
55
+ value
56
+ end
57
+ end
58
+ @prediction = merged
59
+ @updated_at = Time.now.utc
60
+ self
61
+ end
62
+
63
+ def stale?(now: Time.now.utc)
64
+ (now - @updated_at) > STALE_THRESHOLD
65
+ end
66
+
67
+ def decay_precision
68
+ diff = @precision - DEFAULT_PRECISION
69
+ @precision -= diff * PRECISION_DECAY
70
+ @precision = @precision.clamp(PRECISION_FLOOR, PRECISION_CEILING)
71
+ end
72
+
73
+ def to_h
74
+ {
75
+ id: @id,
76
+ domain: @domain,
77
+ content: @content,
78
+ prediction: @prediction,
79
+ last_observation: @last_observation,
80
+ prediction_error: @prediction_error.round(4),
81
+ precision: @precision.round(4),
82
+ free_energy: free_energy.round(4),
83
+ surprise_label: surprise_label,
84
+ surprising: surprising?,
85
+ created_at: @created_at,
86
+ updated_at: @updated_at
87
+ }
88
+ end
89
+
90
+ private
91
+
92
+ def compute_error(observation)
93
+ return empty_prediction_error(observation) if @prediction.empty?
94
+
95
+ shared_keys = @prediction.keys & observation.keys
96
+ return 1.0 if shared_keys.empty?
97
+
98
+ errors = shared_keys.map { |k| key_error(@prediction[k], observation[k]) }
99
+ missing_penalty = (@prediction.keys - shared_keys).size * 0.5
100
+ ((errors.sum + missing_penalty) / @prediction.size.to_f).clamp(0.0, 1.0)
101
+ end
102
+
103
+ def empty_prediction_error(observation)
104
+ observation.empty? ? 0.0 : 1.0
105
+ end
106
+
107
+ def key_error(predicted, observed)
108
+ return (predicted - observed).abs.clamp(0.0, 1.0) if predicted.is_a?(Numeric) && observed.is_a?(Numeric)
109
+
110
+ predicted == observed ? 0.0 : 1.0
111
+ end
112
+
113
+ def update_precision
114
+ if @prediction_error < 0.2
115
+ @precision += PRECISION_UPDATE_RATE * (1.0 - @prediction_error)
116
+ else
117
+ @precision -= PRECISION_UPDATE_RATE * @prediction_error
118
+ end
119
+ @precision = @precision.clamp(PRECISION_FLOOR, PRECISION_CEILING)
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module FreeEnergy
8
+ module Helpers
9
+ module Constants
10
+ # Maximum generative-model beliefs
11
+ MAX_BELIEFS = 200
12
+
13
+ # Maximum planned actions
14
+ MAX_ACTIONS = 100
15
+
16
+ # Maximum event history
17
+ MAX_HISTORY = 300
18
+
19
+ # Default precision (inverse variance / confidence in prediction)
20
+ DEFAULT_PRECISION = 0.5
21
+
22
+ # Precision bounds
23
+ PRECISION_FLOOR = 0.05
24
+ PRECISION_CEILING = 0.95
25
+
26
+ # Free energy above this triggers high-surprise state
27
+ FREE_ENERGY_THRESHOLD = 0.5
28
+
29
+ # Learning rate for belief updates (perceptual inference)
30
+ LEARNING_RATE = 0.1
31
+
32
+ # Rate at which precision adapts to prediction errors
33
+ PRECISION_UPDATE_RATE = 0.05
34
+
35
+ # Precision decay toward default for stale beliefs
36
+ PRECISION_DECAY = 0.01
37
+
38
+ # How stale (seconds) before precision decays
39
+ STALE_THRESHOLD = 120
40
+
41
+ # Free energy minimization strategies
42
+ INFERENCE_MODES = %i[perceptual active].freeze
43
+
44
+ # Surprise magnitude labels
45
+ SURPRISE_LABELS = {
46
+ (0.8..) => :shocking,
47
+ (0.6...0.8) => :surprising,
48
+ (0.4...0.6) => :notable,
49
+ (0.2...0.4) => :expected,
50
+ (..0.2) => :trivial
51
+ }.freeze
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module FreeEnergy
8
+ module Helpers
9
+ class FreeEnergyEngine
10
+ include Constants
11
+
12
+ attr_reader :beliefs, :history
13
+
14
+ def initialize
15
+ @beliefs = {}
16
+ @belief_count = 0
17
+ @history = []
18
+ end
19
+
20
+ def add_belief(content:, domain: :general, prediction: {}, precision: DEFAULT_PRECISION)
21
+ return nil if @beliefs.size >= MAX_BELIEFS
22
+
23
+ @belief_count += 1
24
+ belief = Belief.new(
25
+ id: :"belief_#{@belief_count}",
26
+ content: content,
27
+ domain: domain,
28
+ prediction: prediction,
29
+ precision: precision
30
+ )
31
+ @beliefs[belief.id] = belief
32
+ record_event(:add_belief, belief_id: belief.id)
33
+ belief
34
+ end
35
+
36
+ def observe(belief_id:, observation:)
37
+ belief = @beliefs[belief_id]
38
+ return nil unless belief
39
+
40
+ belief.observe(observation: observation)
41
+ record_event(:observe, belief_id: belief_id, error: belief.prediction_error)
42
+ belief
43
+ end
44
+
45
+ def minimize_perceptual(belief_id:)
46
+ belief = @beliefs[belief_id]
47
+ return nil unless belief
48
+ return nil unless belief.last_observation
49
+
50
+ belief.revise_prediction(observation: belief.last_observation)
51
+ record_event(:minimize_perceptual, belief_id: belief_id)
52
+ belief
53
+ end
54
+
55
+ def minimize_active(belief_id:)
56
+ belief = @beliefs[belief_id]
57
+ return nil unless belief
58
+
59
+ action = {
60
+ type: :active_inference,
61
+ belief_id: belief_id,
62
+ target: belief.prediction,
63
+ urgency: belief.free_energy
64
+ }
65
+ record_event(:minimize_active, belief_id: belief_id, action: action)
66
+ action
67
+ end
68
+
69
+ def minimize(belief_id:, mode: :perceptual)
70
+ case mode
71
+ when :perceptual then minimize_perceptual(belief_id: belief_id)
72
+ when :active then minimize_active(belief_id: belief_id)
73
+ end
74
+ end
75
+
76
+ def total_free_energy
77
+ return 0.0 if @beliefs.empty?
78
+
79
+ @beliefs.values.sum(&:free_energy) / @beliefs.size.to_f
80
+ end
81
+
82
+ def surprise_level
83
+ fe = total_free_energy
84
+ SURPRISE_LABELS.find { |range, _| range.cover?(fe) }&.last || :trivial
85
+ end
86
+
87
+ def high_surprise_beliefs
88
+ @beliefs.values.select(&:surprising?).map(&:to_h)
89
+ end
90
+
91
+ def domain_beliefs(domain:)
92
+ @beliefs.values.select { |b| b.domain == domain }
93
+ end
94
+
95
+ def domain_free_energy(domain:)
96
+ relevant = domain_beliefs(domain: domain)
97
+ return 0.0 if relevant.empty?
98
+
99
+ relevant.sum(&:free_energy) / relevant.size.to_f
100
+ end
101
+
102
+ def most_surprising(limit: 5)
103
+ @beliefs.values
104
+ .sort_by { |b| -b.free_energy }
105
+ .first(limit)
106
+ .map(&:to_h)
107
+ end
108
+
109
+ def most_precise(limit: 5)
110
+ @beliefs.values
111
+ .sort_by { |b| -b.precision }
112
+ .first(limit)
113
+ .map(&:to_h)
114
+ end
115
+
116
+ def decay_stale
117
+ now = Time.now.utc
118
+ @beliefs.each_value do |belief|
119
+ belief.decay_precision if belief.stale?(now: now)
120
+ end
121
+ end
122
+
123
+ def remove_belief(belief_id:)
124
+ @beliefs.delete(belief_id)
125
+ end
126
+
127
+ def to_h
128
+ {
129
+ belief_count: @beliefs.size,
130
+ total_free_energy: total_free_energy.round(4),
131
+ surprise_level: surprise_level,
132
+ high_surprise_count: @beliefs.values.count(&:surprising?),
133
+ mean_precision: mean_precision.round(4),
134
+ history_size: @history.size
135
+ }
136
+ end
137
+
138
+ private
139
+
140
+ def mean_precision
141
+ return DEFAULT_PRECISION if @beliefs.empty?
142
+
143
+ @beliefs.values.sum(&:precision) / @beliefs.size.to_f
144
+ end
145
+
146
+ def record_event(type, **details)
147
+ @history << { type: type, at: Time.now.utc }.merge(details)
148
+ @history.shift while @history.size > MAX_HISTORY
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module FreeEnergy
8
+ module Runners
9
+ module FreeEnergy
10
+ include Helpers::Constants
11
+ include Legion::Extensions::Helpers::Lex if defined?(Legion::Extensions::Helpers::Lex)
12
+
13
+ def add_generative_belief(content:, domain: :general, prediction: {}, precision: DEFAULT_PRECISION, **)
14
+ belief = engine.add_belief(content: content, domain: domain, prediction: prediction, precision: precision)
15
+ return { success: false, reason: :limit_reached } unless belief
16
+
17
+ { success: true, belief_id: belief.id, precision: belief.precision }
18
+ end
19
+
20
+ def observe_outcome(belief_id:, observation:, **)
21
+ belief = engine.observe(belief_id: belief_id, observation: observation)
22
+ return { success: false, reason: :not_found } unless belief
23
+
24
+ {
25
+ success: true,
26
+ belief_id: belief_id,
27
+ prediction_error: belief.prediction_error.round(4),
28
+ free_energy: belief.free_energy.round(4),
29
+ surprising: belief.surprising?,
30
+ surprise_label: belief.surprise_label
31
+ }
32
+ end
33
+
34
+ def minimize_free_energy(belief_id:, mode: :perceptual, **)
35
+ result = engine.minimize(belief_id: belief_id, mode: mode)
36
+ return { success: false, reason: :not_found } unless result
37
+
38
+ if mode == :active
39
+ { success: true, mode: :active }.merge(result)
40
+ else
41
+ { success: true, mode: :perceptual, belief_id: result.id, prediction: result.prediction }
42
+ end
43
+ end
44
+
45
+ def compute_free_energy(**)
46
+ {
47
+ success: true,
48
+ total_free_energy: engine.total_free_energy.round(4),
49
+ surprise_level: engine.surprise_level,
50
+ high_surprise_count: engine.high_surprise_beliefs.size
51
+ }
52
+ end
53
+
54
+ def surprise_assessment(**)
55
+ {
56
+ success: true,
57
+ surprise_level: engine.surprise_level,
58
+ most_surprising: engine.most_surprising,
59
+ most_precise: engine.most_precise
60
+ }
61
+ end
62
+
63
+ def high_surprise_beliefs(**)
64
+ beliefs = engine.high_surprise_beliefs
65
+ { success: true, beliefs: beliefs, count: beliefs.size }
66
+ end
67
+
68
+ def domain_free_energy(domain:, **)
69
+ {
70
+ success: true,
71
+ domain: domain,
72
+ free_energy: engine.domain_free_energy(domain: domain).round(4),
73
+ belief_count: engine.domain_beliefs(domain: domain).size
74
+ }
75
+ end
76
+
77
+ def update_free_energy(**)
78
+ engine.decay_stale
79
+ { success: true }.merge(engine.to_h)
80
+ end
81
+
82
+ def free_energy_stats(**)
83
+ { success: true }.merge(engine.to_h)
84
+ end
85
+
86
+ private
87
+
88
+ def engine
89
+ @engine ||= Helpers::FreeEnergyEngine.new
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ 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 FreeEnergy
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_relative 'free_energy/version'
4
+ require_relative 'free_energy/helpers/constants'
5
+ require_relative 'free_energy/helpers/belief'
6
+ require_relative 'free_energy/helpers/free_energy_engine'
7
+ require_relative 'free_energy/runners/free_energy'
8
+ require_relative 'free_energy/client'
9
+
10
+ module Legion
11
+ module Extensions
12
+ module Agentic
13
+ module Inference
14
+ module FreeEnergy
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/agentic/inference/gravity/helpers/constants'
4
+ require 'legion/extensions/agentic/inference/gravity/helpers/attractor'
5
+ require 'legion/extensions/agentic/inference/gravity/helpers/orbiting_thought'
6
+ require 'legion/extensions/agentic/inference/gravity/helpers/gravity_engine'
7
+ require 'legion/extensions/agentic/inference/gravity/runners/gravity'
8
+
9
+ module Legion
10
+ module Extensions
11
+ module Agentic
12
+ module Inference
13
+ module Gravity
14
+ class Client
15
+ include Runners::Gravity
16
+
17
+ def initialize(**)
18
+ @gravity_engine = Helpers::GravityEngine.new
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :gravity_engine
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Legion
6
+ module Extensions
7
+ module Agentic
8
+ module Inference
9
+ module Gravity
10
+ module Helpers
11
+ class Attractor
12
+ attr_reader :id, :content, :domain, :mass, :pull_radius, :decay_rate, :created_at, :reinforcement_count
13
+
14
+ def initialize(content:, domain: :unknown, mass: Constants::DEFAULT_MASS,
15
+ pull_radius: 1.0, decay_rate: 0.01)
16
+ @id = SecureRandom.uuid
17
+ @content = content
18
+ @domain = domain
19
+ @mass = mass.to_f.round(10)
20
+ @pull_radius = pull_radius.clamp(0.01, Float::INFINITY)
21
+ @decay_rate = decay_rate.clamp(0.0, 1.0)
22
+ @created_at = Time.now.utc
23
+ @reinforcement_count = 0
24
+ end
25
+
26
+ def accrete!(amount = Constants::MASS_ACCRETION)
27
+ @mass = (@mass + amount.to_f).round(10)
28
+ @reinforcement_count += 1
29
+ self
30
+ end
31
+
32
+ def erode!(amount = Constants::MASS_EROSION)
33
+ @mass = [(@mass - amount.to_f).round(10), 0.0].max
34
+ self
35
+ end
36
+
37
+ def pull_strength_at(distance:)
38
+ return 0.0 if distance <= 0 || distance > @pull_radius
39
+
40
+ raw = (Constants::PULL_CONSTANT * @mass) / (distance**2)
41
+ raw.clamp(0.0, @mass)
42
+ end
43
+
44
+ def collapsed?
45
+ @mass < Constants::COLLAPSE_THRESHOLD
46
+ end
47
+
48
+ def supermassive?
49
+ @mass >= Constants::SUPERMASSIVE_THRESHOLD
50
+ end
51
+
52
+ def mass_label
53
+ Constants.label_for(Constants::MASS_LABELS, @mass)
54
+ end
55
+
56
+ def to_h
57
+ {
58
+ id: @id,
59
+ content: @content,
60
+ domain: @domain,
61
+ mass: @mass,
62
+ pull_radius: @pull_radius,
63
+ decay_rate: @decay_rate,
64
+ reinforcement_count: @reinforcement_count,
65
+ collapsed: collapsed?,
66
+ supermassive: supermassive?,
67
+ mass_label: mass_label,
68
+ created_at: @created_at
69
+ }
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Agentic
6
+ module Inference
7
+ module Gravity
8
+ module Helpers
9
+ module Constants
10
+ MAX_ATTRACTORS = 200
11
+ MAX_ORBITING = 500
12
+ DEFAULT_MASS = 1.0
13
+ MASS_ACCRETION = 0.15
14
+ MASS_EROSION = 0.05
15
+ CAPTURE_RADIUS = 0.2
16
+ ESCAPE_RADIUS = 1.5
17
+ PULL_CONSTANT = 0.1
18
+ COLLAPSE_THRESHOLD = 0.1
19
+ SUPERMASSIVE_THRESHOLD = 3.0
20
+
21
+ ATTRACTOR_DOMAINS = %i[
22
+ problem
23
+ curiosity
24
+ anxiety
25
+ obsession
26
+ interest
27
+ fear
28
+ desire
29
+ unknown
30
+ ].freeze
31
+
32
+ MASS_LABELS = {
33
+ (0.0..0.1) => :collapsing,
34
+ (0.1..0.5) => :weak,
35
+ (0.5..1.0) => :nascent,
36
+ (1.0..2.0) => :moderate,
37
+ (2.0..3.0) => :strong,
38
+ (3.0..Float::INFINITY) => :supermassive
39
+ }.freeze
40
+
41
+ DENSITY_LABELS = {
42
+ (0..0) => :empty,
43
+ (1..2) => :sparse,
44
+ (3..5) => :light,
45
+ (6..10) => :moderate,
46
+ (11..20) => :dense,
47
+ (21..Float::INFINITY) => :crowded
48
+ }.freeze
49
+
50
+ ORBIT_LABELS = {
51
+ (0.0..0.2) => :captured,
52
+ (0.2..0.5) => :tight,
53
+ (0.5..1.0) => :stable,
54
+ (1.0..1.5) => :loose,
55
+ (1.5..Float::INFINITY) => :escaped
56
+ }.freeze
57
+
58
+ module_function
59
+
60
+ def label_for(labels_hash, value)
61
+ labels_hash.each do |range, label|
62
+ return label if range.cover?(value)
63
+ end
64
+ :unknown
65
+ end
66
+
67
+ def valid_domain?(domain)
68
+ ATTRACTOR_DOMAINS.include?(domain)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end