lex-agentic-social 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +5 -0
- data/LICENSE +21 -0
- data/README.md +13 -0
- data/lex-agentic-social.gemspec +30 -0
- data/lib/legion/extensions/agentic/social/apprenticeship/client.rb +28 -0
- data/lib/legion/extensions/agentic/social/apprenticeship/helpers/apprenticeship.rb +90 -0
- data/lib/legion/extensions/agentic/social/apprenticeship/helpers/apprenticeship_engine.rb +109 -0
- data/lib/legion/extensions/agentic/social/apprenticeship/helpers/apprenticeship_model.rb +93 -0
- data/lib/legion/extensions/agentic/social/apprenticeship/runners/cognitive_apprenticeship.rb +112 -0
- data/lib/legion/extensions/agentic/social/apprenticeship/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/apprenticeship.rb +19 -0
- data/lib/legion/extensions/agentic/social/conflict/actors/stale_check.rb +45 -0
- data/lib/legion/extensions/agentic/social/conflict/client.rb +27 -0
- data/lib/legion/extensions/agentic/social/conflict/helpers/conflict_log.rb +70 -0
- data/lib/legion/extensions/agentic/social/conflict/helpers/llm_enhancer.rb +130 -0
- data/lib/legion/extensions/agentic/social/conflict/helpers/severity.rb +47 -0
- data/lib/legion/extensions/agentic/social/conflict/runners/conflict.rb +112 -0
- data/lib/legion/extensions/agentic/social/conflict/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/conflict.rb +19 -0
- data/lib/legion/extensions/agentic/social/conscience/client.rb +26 -0
- data/lib/legion/extensions/agentic/social/conscience/helpers/constants.rb +53 -0
- data/lib/legion/extensions/agentic/social/conscience/helpers/moral_evaluator.rb +178 -0
- data/lib/legion/extensions/agentic/social/conscience/helpers/moral_store.rb +116 -0
- data/lib/legion/extensions/agentic/social/conscience/runners/conscience.rb +117 -0
- data/lib/legion/extensions/agentic/social/conscience/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/conscience.rb +19 -0
- data/lib/legion/extensions/agentic/social/consent/actors/tier_evaluation.rb +45 -0
- data/lib/legion/extensions/agentic/social/consent/client.rb +27 -0
- data/lib/legion/extensions/agentic/social/consent/helpers/consent_map.rb +199 -0
- data/lib/legion/extensions/agentic/social/consent/helpers/tiers.rb +54 -0
- data/lib/legion/extensions/agentic/social/consent/local_migrations/20260316000010_create_consent_domains.rb +16 -0
- data/lib/legion/extensions/agentic/social/consent/runners/consent.rb +228 -0
- data/lib/legion/extensions/agentic/social/consent/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/consent.rb +25 -0
- data/lib/legion/extensions/agentic/social/entrainment/client.rb +19 -0
- data/lib/legion/extensions/agentic/social/entrainment/helpers/constants.rb +40 -0
- data/lib/legion/extensions/agentic/social/entrainment/helpers/entrainment_engine.rb +120 -0
- data/lib/legion/extensions/agentic/social/entrainment/helpers/pairing.rb +86 -0
- data/lib/legion/extensions/agentic/social/entrainment/runners/cognitive_entrainment.rb +89 -0
- data/lib/legion/extensions/agentic/social/entrainment/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/entrainment.rb +19 -0
- data/lib/legion/extensions/agentic/social/governance/actors/shadow_ai_scan.rb +19 -0
- data/lib/legion/extensions/agentic/social/governance/actors/vote_timeout.rb +45 -0
- data/lib/legion/extensions/agentic/social/governance/client.rb +27 -0
- data/lib/legion/extensions/agentic/social/governance/helpers/layers.rb +40 -0
- data/lib/legion/extensions/agentic/social/governance/helpers/proposal.rb +94 -0
- data/lib/legion/extensions/agentic/social/governance/runners/governance.rb +87 -0
- data/lib/legion/extensions/agentic/social/governance/runners/shadow_ai.rb +93 -0
- data/lib/legion/extensions/agentic/social/governance/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/governance.rb +20 -0
- data/lib/legion/extensions/agentic/social/joint_attention/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/social/joint_attention/client.rb +28 -0
- data/lib/legion/extensions/agentic/social/joint_attention/helpers/attention_target.rb +124 -0
- data/lib/legion/extensions/agentic/social/joint_attention/helpers/constants.rb +34 -0
- data/lib/legion/extensions/agentic/social/joint_attention/helpers/joint_focus_manager.rb +157 -0
- data/lib/legion/extensions/agentic/social/joint_attention/runners/joint_attention.rb +88 -0
- data/lib/legion/extensions/agentic/social/joint_attention/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/joint_attention.rb +20 -0
- data/lib/legion/extensions/agentic/social/mentalizing/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/social/mentalizing/client.rb +28 -0
- data/lib/legion/extensions/agentic/social/mentalizing/helpers/belief_attribution.rb +58 -0
- data/lib/legion/extensions/agentic/social/mentalizing/helpers/constants.rb +33 -0
- data/lib/legion/extensions/agentic/social/mentalizing/helpers/mental_model.rb +137 -0
- data/lib/legion/extensions/agentic/social/mentalizing/runners/mentalizing.rb +93 -0
- data/lib/legion/extensions/agentic/social/mentalizing/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/mentalizing.rb +20 -0
- data/lib/legion/extensions/agentic/social/mirror/client.rb +33 -0
- data/lib/legion/extensions/agentic/social/mirror/helpers/constants.rb +65 -0
- data/lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb +132 -0
- data/lib/legion/extensions/agentic/social/mirror/helpers/mirror_event.rb +46 -0
- data/lib/legion/extensions/agentic/social/mirror/helpers/simulation.rb +43 -0
- data/lib/legion/extensions/agentic/social/mirror/runners/observe.rb +52 -0
- data/lib/legion/extensions/agentic/social/mirror/runners/resonance.rb +79 -0
- data/lib/legion/extensions/agentic/social/mirror/runners/simulate.rb +63 -0
- data/lib/legion/extensions/agentic/social/mirror/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/mirror.rb +22 -0
- data/lib/legion/extensions/agentic/social/mirror_system/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/social/mirror_system/client.rb +28 -0
- data/lib/legion/extensions/agentic/social/mirror_system/helpers/constants.rb +62 -0
- data/lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb +162 -0
- data/lib/legion/extensions/agentic/social/mirror_system/helpers/observed_behavior.rb +67 -0
- data/lib/legion/extensions/agentic/social/mirror_system/runners/mirror.rb +99 -0
- data/lib/legion/extensions/agentic/social/mirror_system/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/mirror_system.rb +20 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/client.rb +19 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/helpers/constants.rb +49 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/helpers/dilemma.rb +68 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/helpers/llm_enhancer.rb +140 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/helpers/moral_engine.rb +239 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/helpers/moral_foundation.rb +45 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/runners/moral_reasoning.rb +121 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/moral_reasoning.rb +21 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting/client.rb +29 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting/helpers/constants.rb +67 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting/helpers/perspective.rb +45 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting/helpers/perspective_view.rb +57 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting/helpers/shifting_engine.rb +166 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting/runners/perspective_shifting.rb +167 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/perspective_shifting.rb +20 -0
- data/lib/legion/extensions/agentic/social/social/client.rb +25 -0
- data/lib/legion/extensions/agentic/social/social/helpers/constants.rb +84 -0
- data/lib/legion/extensions/agentic/social/social/helpers/social_graph.rb +172 -0
- data/lib/legion/extensions/agentic/social/social/runners/social.rb +146 -0
- data/lib/legion/extensions/agentic/social/social/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/social.rb +18 -0
- data/lib/legion/extensions/agentic/social/social_learning/client.rb +25 -0
- data/lib/legion/extensions/agentic/social/social_learning/helpers/constants.rb +42 -0
- data/lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb +82 -0
- data/lib/legion/extensions/agentic/social/social_learning/helpers/observed_behavior.rb +61 -0
- data/lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb +134 -0
- data/lib/legion/extensions/agentic/social/social_learning/runners/social_learning.rb +105 -0
- data/lib/legion/extensions/agentic/social/social_learning/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/social_learning.rb +20 -0
- data/lib/legion/extensions/agentic/social/symbiosis/client.rb +23 -0
- data/lib/legion/extensions/agentic/social/symbiosis/helpers/constants.rb +50 -0
- data/lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb +113 -0
- data/lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb +104 -0
- data/lib/legion/extensions/agentic/social/symbiosis/helpers/symbiotic_bond.rb +112 -0
- data/lib/legion/extensions/agentic/social/symbiosis/runners/cognitive_symbiosis.rb +101 -0
- data/lib/legion/extensions/agentic/social/symbiosis/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/symbiosis.rb +22 -0
- data/lib/legion/extensions/agentic/social/theory_of_mind/client.rb +26 -0
- data/lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb +173 -0
- data/lib/legion/extensions/agentic/social/theory_of_mind/helpers/constants.rb +70 -0
- data/lib/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker.rb +169 -0
- data/lib/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind.rb +159 -0
- data/lib/legion/extensions/agentic/social/theory_of_mind/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/theory_of_mind.rb +19 -0
- data/lib/legion/extensions/agentic/social/trust/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/social/trust/client.rb +27 -0
- data/lib/legion/extensions/agentic/social/trust/helpers/trust_map.rb +160 -0
- data/lib/legion/extensions/agentic/social/trust/helpers/trust_model.rb +52 -0
- data/lib/legion/extensions/agentic/social/trust/local_migrations/20260316000020_create_trust_entries.rb +23 -0
- data/lib/legion/extensions/agentic/social/trust/runners/trust.rb +80 -0
- data/lib/legion/extensions/agentic/social/trust/version.rb +13 -0
- data/lib/legion/extensions/agentic/social/trust.rb +25 -0
- data/lib/legion/extensions/agentic/social/version.rb +11 -0
- data/lib/legion/extensions/agentic/social.rb +34 -0
- data/spec/legion/extensions/agentic/social/apprenticeship/client_spec.rb +20 -0
- data/spec/legion/extensions/agentic/social/apprenticeship/cognitive_apprenticeship_spec.rb +11 -0
- data/spec/legion/extensions/agentic/social/apprenticeship/helpers/apprenticeship_engine_spec.rb +146 -0
- data/spec/legion/extensions/agentic/social/apprenticeship/helpers/apprenticeship_model_spec.rb +124 -0
- data/spec/legion/extensions/agentic/social/apprenticeship/helpers/apprenticeship_spec.rb +136 -0
- data/spec/legion/extensions/agentic/social/apprenticeship/runners/cognitive_apprenticeship_spec.rb +154 -0
- data/spec/legion/extensions/agentic/social/conflict/actors/stale_check_spec.rb +45 -0
- data/spec/legion/extensions/agentic/social/conflict/client_spec.rb +15 -0
- data/spec/legion/extensions/agentic/social/conflict/helpers/conflict_log_spec.rb +232 -0
- data/spec/legion/extensions/agentic/social/conflict/helpers/llm_enhancer_spec.rb +189 -0
- data/spec/legion/extensions/agentic/social/conflict/helpers/severity_spec.rb +215 -0
- data/spec/legion/extensions/agentic/social/conflict/runners/conflict_spec.rb +151 -0
- data/spec/legion/extensions/agentic/social/conscience/client_spec.rb +58 -0
- data/spec/legion/extensions/agentic/social/conscience/helpers/constants_spec.rb +124 -0
- data/spec/legion/extensions/agentic/social/conscience/helpers/moral_evaluator_spec.rb +253 -0
- data/spec/legion/extensions/agentic/social/conscience/helpers/moral_store_spec.rb +230 -0
- data/spec/legion/extensions/agentic/social/conscience/runners/conscience_spec.rb +239 -0
- data/spec/legion/extensions/agentic/social/consent/actors/tier_evaluation_spec.rb +46 -0
- data/spec/legion/extensions/agentic/social/consent/client_spec.rb +33 -0
- data/spec/legion/extensions/agentic/social/consent/helpers/tiers_spec.rb +49 -0
- data/spec/legion/extensions/agentic/social/consent/local_persistence_spec.rb +234 -0
- data/spec/legion/extensions/agentic/social/consent/runners/consent_spec.rb +224 -0
- data/spec/legion/extensions/agentic/social/entrainment/client_spec.rb +21 -0
- data/spec/legion/extensions/agentic/social/entrainment/helpers/entrainment_engine_spec.rb +116 -0
- data/spec/legion/extensions/agentic/social/entrainment/helpers/pairing_spec.rb +103 -0
- data/spec/legion/extensions/agentic/social/entrainment/runners/cognitive_entrainment_spec.rb +87 -0
- data/spec/legion/extensions/agentic/social/governance/actors/vote_timeout_spec.rb +45 -0
- data/spec/legion/extensions/agentic/social/governance/client_spec.rb +14 -0
- data/spec/legion/extensions/agentic/social/governance/helpers/layers_spec.rb +190 -0
- data/spec/legion/extensions/agentic/social/governance/helpers/proposal_spec.rb +188 -0
- data/spec/legion/extensions/agentic/social/governance/runners/governance_spec.rb +101 -0
- data/spec/legion/extensions/agentic/social/governance/runners/shadow_ai_spec.rb +65 -0
- data/spec/legion/extensions/agentic/social/joint_attention/client_spec.rb +36 -0
- data/spec/legion/extensions/agentic/social/joint_attention/helpers/attention_target_spec.rb +258 -0
- data/spec/legion/extensions/agentic/social/joint_attention/helpers/joint_focus_manager_spec.rb +238 -0
- data/spec/legion/extensions/agentic/social/joint_attention/runners/joint_attention_spec.rb +228 -0
- data/spec/legion/extensions/agentic/social/mentalizing/client_spec.rb +19 -0
- data/spec/legion/extensions/agentic/social/mentalizing/helpers/belief_attribution_spec.rb +108 -0
- data/spec/legion/extensions/agentic/social/mentalizing/helpers/mental_model_spec.rb +179 -0
- data/spec/legion/extensions/agentic/social/mentalizing/runners/mentalizing_spec.rb +162 -0
- data/spec/legion/extensions/agentic/social/mirror/client_spec.rb +92 -0
- data/spec/legion/extensions/agentic/social/mirror/helpers/constants_spec.rb +123 -0
- data/spec/legion/extensions/agentic/social/mirror/helpers/mirror_engine_spec.rb +217 -0
- data/spec/legion/extensions/agentic/social/mirror/helpers/mirror_event_spec.rb +102 -0
- data/spec/legion/extensions/agentic/social/mirror/helpers/simulation_spec.rb +100 -0
- data/spec/legion/extensions/agentic/social/mirror/runners/observe_spec.rb +77 -0
- data/spec/legion/extensions/agentic/social/mirror/runners/resonance_spec.rb +123 -0
- data/spec/legion/extensions/agentic/social/mirror/runners/simulate_spec.rb +103 -0
- data/spec/legion/extensions/agentic/social/mirror_system/client_spec.rb +40 -0
- data/spec/legion/extensions/agentic/social/mirror_system/helpers/mirror_system_spec.rb +144 -0
- data/spec/legion/extensions/agentic/social/mirror_system/helpers/observed_behavior_spec.rb +98 -0
- data/spec/legion/extensions/agentic/social/mirror_system/runners/mirror_spec.rb +122 -0
- data/spec/legion/extensions/agentic/social/moral_reasoning/client_spec.rb +34 -0
- data/spec/legion/extensions/agentic/social/moral_reasoning/helpers/dilemma_spec.rb +108 -0
- data/spec/legion/extensions/agentic/social/moral_reasoning/helpers/llm_enhancer_spec.rb +232 -0
- data/spec/legion/extensions/agentic/social/moral_reasoning/helpers/moral_engine_spec.rb +266 -0
- data/spec/legion/extensions/agentic/social/moral_reasoning/helpers/moral_foundation_spec.rb +70 -0
- data/spec/legion/extensions/agentic/social/moral_reasoning/runners/moral_reasoning_spec.rb +275 -0
- data/spec/legion/extensions/agentic/social/perspective_shifting/client_spec.rb +30 -0
- data/spec/legion/extensions/agentic/social/perspective_shifting/helpers/constants_spec.rb +99 -0
- data/spec/legion/extensions/agentic/social/perspective_shifting/helpers/perspective_spec.rb +77 -0
- data/spec/legion/extensions/agentic/social/perspective_shifting/helpers/perspective_view_spec.rb +105 -0
- data/spec/legion/extensions/agentic/social/perspective_shifting/helpers/shifting_engine_spec.rb +246 -0
- data/spec/legion/extensions/agentic/social/perspective_shifting/runners/perspective_shifting_spec.rb +277 -0
- data/spec/legion/extensions/agentic/social/social/client_spec.rb +72 -0
- data/spec/legion/extensions/agentic/social/social/helpers/constants_spec.rb +99 -0
- data/spec/legion/extensions/agentic/social/social/helpers/social_graph_spec.rb +322 -0
- data/spec/legion/extensions/agentic/social/social/runners/social_spec.rb +220 -0
- data/spec/legion/extensions/agentic/social/social_learning/client_spec.rb +25 -0
- data/spec/legion/extensions/agentic/social/social_learning/helpers/constants_spec.rb +44 -0
- data/spec/legion/extensions/agentic/social/social_learning/helpers/model_agent_spec.rb +120 -0
- data/spec/legion/extensions/agentic/social/social_learning/helpers/observed_behavior_spec.rb +81 -0
- data/spec/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine_spec.rb +196 -0
- data/spec/legion/extensions/agentic/social/social_learning/runners/social_learning_spec.rb +150 -0
- data/spec/legion/extensions/agentic/social/symbiosis/client_spec.rb +45 -0
- data/spec/legion/extensions/agentic/social/symbiosis/helpers/constants_spec.rb +73 -0
- data/spec/legion/extensions/agentic/social/symbiosis/helpers/ecosystem_spec.rb +185 -0
- data/spec/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine_spec.rb +182 -0
- data/spec/legion/extensions/agentic/social/symbiosis/helpers/symbiotic_bond_spec.rb +209 -0
- data/spec/legion/extensions/agentic/social/symbiosis/runners/cognitive_symbiosis_spec.rb +182 -0
- data/spec/legion/extensions/agentic/social/theory_of_mind/client_spec.rb +63 -0
- data/spec/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model_spec.rb +244 -0
- data/spec/legion/extensions/agentic/social/theory_of_mind/helpers/constants_spec.rb +71 -0
- data/spec/legion/extensions/agentic/social/theory_of_mind/helpers/mental_state_tracker_spec.rb +228 -0
- data/spec/legion/extensions/agentic/social/theory_of_mind/runners/theory_of_mind_spec.rb +221 -0
- data/spec/legion/extensions/agentic/social/trust/actors/decay_spec.rb +62 -0
- data/spec/legion/extensions/agentic/social/trust/client_spec.rb +17 -0
- data/spec/legion/extensions/agentic/social/trust/helpers/trust_map_spec.rb +299 -0
- data/spec/legion/extensions/agentic/social/trust/helpers/trust_model_spec.rb +179 -0
- data/spec/legion/extensions/agentic/social/trust/local_persistence_spec.rb +359 -0
- data/spec/legion/extensions/agentic/social/trust/runners/trust_spec.rb +84 -0
- data/spec/spec_helper.rb +54 -0
- metadata +319 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Social
|
|
7
|
+
module SocialLearning
|
|
8
|
+
module Runners
|
|
9
|
+
module SocialLearning
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def register_model_agent(agent_id:, domain:, prestige: nil, **)
|
|
14
|
+
init_prestige = prestige || Helpers::Constants::DEFAULT_PRESTIGE
|
|
15
|
+
Legion::Logging.debug "[social_learning] register_model agent=#{agent_id} domain=#{domain} prestige=#{init_prestige}"
|
|
16
|
+
model = engine.register_model(agent_id: agent_id, domain: domain, prestige: init_prestige)
|
|
17
|
+
{ success: true, model: model.to_h }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def observe_agent_behavior(model_id:, action:, domain:, outcome:, context: {}, **)
|
|
21
|
+
Legion::Logging.debug "[social_learning] observe model=#{model_id} action=#{action} domain=#{domain} outcome=#{outcome}"
|
|
22
|
+
behavior = engine.observe_behavior(
|
|
23
|
+
model_id: model_id,
|
|
24
|
+
action: action,
|
|
25
|
+
domain: domain,
|
|
26
|
+
outcome: outcome,
|
|
27
|
+
context: context
|
|
28
|
+
)
|
|
29
|
+
if behavior
|
|
30
|
+
{ success: true, behavior: behavior.to_h }
|
|
31
|
+
else
|
|
32
|
+
{ success: false, reason: 'model not found or below attention threshold' }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def retained_behaviors(domain: nil, **)
|
|
37
|
+
Legion::Logging.debug "[social_learning] retained_behaviors domain=#{domain.inspect}"
|
|
38
|
+
behaviors = engine.retained_behaviors(domain: domain)
|
|
39
|
+
{ success: true, behaviors: behaviors.map(&:to_h), count: behaviors.size }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def reproducible_behaviors(domain: nil, **)
|
|
43
|
+
Legion::Logging.debug "[social_learning] reproducible_behaviors domain=#{domain.inspect}"
|
|
44
|
+
behaviors = engine.reproducible_behaviors(domain: domain)
|
|
45
|
+
{ success: true, behaviors: behaviors.map(&:to_h), count: behaviors.size }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def reproduce_observed_behavior(behavior_id:, **)
|
|
49
|
+
Legion::Logging.debug "[social_learning] reproduce behavior_id=#{behavior_id}"
|
|
50
|
+
behavior = engine.reproduce_behavior(behavior_id: behavior_id)
|
|
51
|
+
if behavior
|
|
52
|
+
{ success: true, behavior: behavior.to_h }
|
|
53
|
+
else
|
|
54
|
+
{ success: false, reason: 'behavior not found or retention too low' }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def reinforce_reproduction(behavior_id:, outcome:, **)
|
|
59
|
+
Legion::Logging.debug "[social_learning] reinforce behavior_id=#{behavior_id} outcome=#{outcome}"
|
|
60
|
+
result = engine.reinforce_reproduction(behavior_id: behavior_id, outcome: outcome)
|
|
61
|
+
if result
|
|
62
|
+
{ success: true }.merge(result)
|
|
63
|
+
else
|
|
64
|
+
{ success: false, reason: 'behavior or model not found' }
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def best_model_agents(limit: 5, **)
|
|
69
|
+
lim = limit.to_i
|
|
70
|
+
Legion::Logging.debug "[social_learning] best_model_agents limit=#{lim}"
|
|
71
|
+
models = engine.best_models(limit: lim)
|
|
72
|
+
{ success: true, models: models.map(&:to_h), count: models.size }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def domain_models(domain:, **)
|
|
76
|
+
Legion::Logging.debug "[social_learning] domain_models domain=#{domain}"
|
|
77
|
+
models = engine.by_domain(domain: domain)
|
|
78
|
+
{ success: true, models: models.map(&:to_h), count: models.size }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def update_social_learning(**)
|
|
82
|
+
Legion::Logging.debug '[social_learning] update_social_learning decay+prune cycle'
|
|
83
|
+
engine.decay_all
|
|
84
|
+
engine.prune_forgotten
|
|
85
|
+
stats = engine.to_h
|
|
86
|
+
{ success: true }.merge(stats)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def social_learning_stats(**)
|
|
90
|
+
Legion::Logging.debug '[social_learning] social_learning_stats'
|
|
91
|
+
{ success: true }.merge(engine.to_h)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def engine
|
|
97
|
+
@engine ||= Helpers::SocialLearningEngine.new
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/social/social_learning/version'
|
|
4
|
+
require 'legion/extensions/agentic/social/social_learning/helpers/constants'
|
|
5
|
+
require 'legion/extensions/agentic/social/social_learning/helpers/observed_behavior'
|
|
6
|
+
require 'legion/extensions/agentic/social/social_learning/helpers/model_agent'
|
|
7
|
+
require 'legion/extensions/agentic/social/social_learning/helpers/social_learning_engine'
|
|
8
|
+
require 'legion/extensions/agentic/social/social_learning/runners/social_learning'
|
|
9
|
+
require 'legion/extensions/agentic/social/social_learning/client'
|
|
10
|
+
|
|
11
|
+
module Legion
|
|
12
|
+
module Extensions
|
|
13
|
+
module Agentic
|
|
14
|
+
module Social
|
|
15
|
+
module SocialLearning
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Social
|
|
7
|
+
module Symbiosis
|
|
8
|
+
class Client
|
|
9
|
+
include Runners::CognitiveSymbiosis
|
|
10
|
+
|
|
11
|
+
def initialize(**)
|
|
12
|
+
@default_engine = Helpers::SymbiosisEngine.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
attr_reader :default_engine
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Social
|
|
7
|
+
module Symbiosis
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
RELATIONSHIP_TYPES = %i[mutualistic parasitic commensal].freeze
|
|
11
|
+
|
|
12
|
+
INTERACTION_STRENGTHS = {
|
|
13
|
+
0.0..0.2 => :dormant,
|
|
14
|
+
0.2..0.4 => :weak,
|
|
15
|
+
0.4..0.6 => :moderate,
|
|
16
|
+
0.6..0.8 => :strong,
|
|
17
|
+
0.8..1.0 => :dominant
|
|
18
|
+
}.freeze
|
|
19
|
+
|
|
20
|
+
MAX_BONDS = 200
|
|
21
|
+
BOND_DECAY = 0.02
|
|
22
|
+
STRONG_THRESHOLD = 0.6
|
|
23
|
+
DORMANT_THRESHOLD = 0.05
|
|
24
|
+
MIN_STRENGTH = 0.0
|
|
25
|
+
MAX_STRENGTH = 1.0
|
|
26
|
+
DEFAULT_STRENGTH = 0.3
|
|
27
|
+
|
|
28
|
+
# Mutualism: both gain, benefit_ratio > 0
|
|
29
|
+
# Parasitism: one drains the other, benefit_ratio < 0
|
|
30
|
+
# Commensalism: one benefits, other unaffected, benefit_ratio ~0 for host
|
|
31
|
+
BENEFIT_RATIO_RANGES = {
|
|
32
|
+
mutualistic: (0.1..1.0),
|
|
33
|
+
parasitic: (-1.0..-0.1),
|
|
34
|
+
commensal: (-0.05..0.05)
|
|
35
|
+
}.freeze
|
|
36
|
+
|
|
37
|
+
ECOSYSTEM_HEALTH_LABELS = {
|
|
38
|
+
0.0..0.2 => :critical,
|
|
39
|
+
0.2..0.4 => :stressed,
|
|
40
|
+
0.4..0.6 => :balanced,
|
|
41
|
+
0.6..0.8 => :thriving,
|
|
42
|
+
0.8..1.0 => :flourishing
|
|
43
|
+
}.freeze
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Social
|
|
7
|
+
module Symbiosis
|
|
8
|
+
module Helpers
|
|
9
|
+
class Ecosystem
|
|
10
|
+
def initialize
|
|
11
|
+
@bonds = {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def register_bond(bond)
|
|
15
|
+
raise ArgumentError, 'bond must be a SymbioticBond' unless bond.is_a?(SymbioticBond)
|
|
16
|
+
raise ArgumentError, "MAX_BONDS (#{Constants::MAX_BONDS}) exceeded" if @bonds.size >= Constants::MAX_BONDS
|
|
17
|
+
|
|
18
|
+
@bonds[bond.bond_id] = bond
|
|
19
|
+
bond
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def activate_bond(bond_id, amount: 0.05)
|
|
23
|
+
bond = @bonds[bond_id]
|
|
24
|
+
return { found: false, bond_id: bond_id } unless bond
|
|
25
|
+
|
|
26
|
+
bond.activate!(amount: amount)
|
|
27
|
+
{ found: true, bond_id: bond_id, strength: bond.strength, activation_count: bond.activation_count }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def measure_health
|
|
31
|
+
return 0.0 if @bonds.empty?
|
|
32
|
+
|
|
33
|
+
active = @bonds.values.reject(&:dormant?)
|
|
34
|
+
return 0.0 if active.empty?
|
|
35
|
+
|
|
36
|
+
mutualistic = active.count { |b| b.relationship_type == :mutualistic }
|
|
37
|
+
parasitic = active.count { |b| b.relationship_type == :parasitic }
|
|
38
|
+
total_active = active.size
|
|
39
|
+
|
|
40
|
+
mutualistic_ratio = mutualistic.to_f / total_active
|
|
41
|
+
parasite_penalty = parasitic.to_f / total_active * 0.5
|
|
42
|
+
|
|
43
|
+
(mutualistic_ratio - parasite_penalty).clamp(0.0, 1.0).round(10)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def health_label
|
|
47
|
+
score = measure_health
|
|
48
|
+
Constants::ECOSYSTEM_HEALTH_LABELS.each do |range, label|
|
|
49
|
+
return label if range.cover?(score)
|
|
50
|
+
end
|
|
51
|
+
:critical
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def most_beneficial
|
|
55
|
+
@bonds.values
|
|
56
|
+
.select { |b| b.relationship_type == :mutualistic && !b.dormant? }
|
|
57
|
+
.max_by { |b| b.benefit_ratio * b.strength }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def most_parasitic
|
|
61
|
+
@bonds.values
|
|
62
|
+
.select { |b| b.relationship_type == :parasitic && !b.dormant? }
|
|
63
|
+
.min_by { |b| b.benefit_ratio * b.strength }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def decay_all!
|
|
67
|
+
decayed = 0
|
|
68
|
+
@bonds.each_value do |bond|
|
|
69
|
+
bond.decay!
|
|
70
|
+
decayed += 1
|
|
71
|
+
end
|
|
72
|
+
decayed
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def network_density
|
|
76
|
+
return 0.0 if @bonds.empty?
|
|
77
|
+
|
|
78
|
+
active = @bonds.values.reject(&:dormant?)
|
|
79
|
+
return 0.0 if active.empty?
|
|
80
|
+
|
|
81
|
+
avg_strength = active.sum(&:strength) / active.size.to_f
|
|
82
|
+
avg_strength.clamp(0.0, 1.0).round(10)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def symbiotic_web(subsystem_id)
|
|
86
|
+
@bonds.values.select { |b| b.involves?(subsystem_id) }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def find_bond(subsystem_a, subsystem_b)
|
|
90
|
+
@bonds.values.find do |b|
|
|
91
|
+
(b.subsystem_a == subsystem_a && b.subsystem_b == subsystem_b) ||
|
|
92
|
+
(b.subsystem_a == subsystem_b && b.subsystem_b == subsystem_a)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def all_bonds
|
|
97
|
+
@bonds.values
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def bond_count
|
|
101
|
+
@bonds.size
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def active_bonds
|
|
105
|
+
@bonds.values.reject(&:dormant?)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Social
|
|
7
|
+
module Symbiosis
|
|
8
|
+
module Helpers
|
|
9
|
+
class SymbiosisEngine
|
|
10
|
+
def initialize
|
|
11
|
+
@ecosystem = Ecosystem.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def create_bond(subsystem_a:, subsystem_b:, relationship_type:, strength: nil, benefit_ratio: nil)
|
|
15
|
+
existing = @ecosystem.find_bond(subsystem_a, subsystem_b)
|
|
16
|
+
return { created: false, reason: :already_exists, bond: existing.to_h } if existing
|
|
17
|
+
|
|
18
|
+
bond = SymbioticBond.new(
|
|
19
|
+
subsystem_a: subsystem_a,
|
|
20
|
+
subsystem_b: subsystem_b,
|
|
21
|
+
relationship_type: relationship_type,
|
|
22
|
+
strength: strength,
|
|
23
|
+
benefit_ratio: benefit_ratio
|
|
24
|
+
)
|
|
25
|
+
@ecosystem.register_bond(bond)
|
|
26
|
+
{ created: true, bond: bond.to_h }
|
|
27
|
+
rescue ArgumentError => e
|
|
28
|
+
{ created: false, reason: :invalid_arguments, error: e.message }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def activate_interaction(subsystem_a:, subsystem_b:, amount: 0.05)
|
|
32
|
+
bond = @ecosystem.find_bond(subsystem_a, subsystem_b)
|
|
33
|
+
return { found: false, subsystem_a: subsystem_a, subsystem_b: subsystem_b } unless bond
|
|
34
|
+
|
|
35
|
+
result = @ecosystem.activate_bond(bond.bond_id, amount: amount)
|
|
36
|
+
result.merge(
|
|
37
|
+
relationship_type: bond.relationship_type,
|
|
38
|
+
benefit_ratio: bond.benefit_ratio
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def measure_ecosystem_health
|
|
43
|
+
score = @ecosystem.measure_health
|
|
44
|
+
{
|
|
45
|
+
score: score,
|
|
46
|
+
label: @ecosystem.health_label,
|
|
47
|
+
bond_count: @ecosystem.bond_count,
|
|
48
|
+
active_bonds: @ecosystem.active_bonds.size,
|
|
49
|
+
network_density: @ecosystem.network_density
|
|
50
|
+
}
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def find_partners(subsystem_id, min_benefit_ratio: 0.0)
|
|
54
|
+
@ecosystem.symbiotic_web(subsystem_id)
|
|
55
|
+
.reject(&:dormant?)
|
|
56
|
+
.select { |b| b.benefit_ratio >= min_benefit_ratio }
|
|
57
|
+
.sort_by { |b| -b.strength }
|
|
58
|
+
.map do |b|
|
|
59
|
+
b.to_h.merge(partner: b.partner_of(subsystem_id))
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def detect_parasites(strength_threshold: 0.0)
|
|
64
|
+
@ecosystem.all_bonds
|
|
65
|
+
.select { |b| b.relationship_type == :parasitic }
|
|
66
|
+
.reject(&:dormant?)
|
|
67
|
+
.select { |b| b.strength >= strength_threshold }
|
|
68
|
+
.sort_by(&:benefit_ratio)
|
|
69
|
+
.map(&:to_h)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def ecosystem_report
|
|
73
|
+
bonds_by_type = Constants::RELATIONSHIP_TYPES.to_h do |type|
|
|
74
|
+
count = @ecosystem.all_bonds.count { |b| b.relationship_type == type && !b.dormant? }
|
|
75
|
+
[type, count]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
health = measure_ecosystem_health
|
|
79
|
+
strongest = @ecosystem.most_beneficial&.to_h
|
|
80
|
+
weakest = @ecosystem.most_parasitic&.to_h
|
|
81
|
+
|
|
82
|
+
{
|
|
83
|
+
health: health,
|
|
84
|
+
bonds_by_type: bonds_by_type,
|
|
85
|
+
most_beneficial: strongest,
|
|
86
|
+
most_parasitic: weakest,
|
|
87
|
+
total_bonds: @ecosystem.bond_count,
|
|
88
|
+
dormant_bonds: @ecosystem.all_bonds.count(&:dormant?)
|
|
89
|
+
}
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def decay_all
|
|
93
|
+
decayed = @ecosystem.decay_all!
|
|
94
|
+
{ decayed: decayed, health_after: measure_ecosystem_health }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
attr_reader :ecosystem
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
require 'time'
|
|
5
|
+
|
|
6
|
+
module Legion
|
|
7
|
+
module Extensions
|
|
8
|
+
module Agentic
|
|
9
|
+
module Social
|
|
10
|
+
module Symbiosis
|
|
11
|
+
module Helpers
|
|
12
|
+
class SymbioticBond
|
|
13
|
+
attr_reader :bond_id, :subsystem_a, :subsystem_b, :relationship_type,
|
|
14
|
+
:benefit_ratio, :activation_count, :created_at, :last_activated_at
|
|
15
|
+
|
|
16
|
+
def initialize(subsystem_a:, subsystem_b:, relationship_type:, strength: nil, benefit_ratio: nil)
|
|
17
|
+
validate_relationship_type!(relationship_type)
|
|
18
|
+
|
|
19
|
+
@bond_id = SecureRandom.uuid
|
|
20
|
+
@subsystem_a = subsystem_a
|
|
21
|
+
@subsystem_b = subsystem_b
|
|
22
|
+
@relationship_type = relationship_type
|
|
23
|
+
@strength = (strength || Constants::DEFAULT_STRENGTH).clamp(
|
|
24
|
+
Constants::MIN_STRENGTH, Constants::MAX_STRENGTH
|
|
25
|
+
)
|
|
26
|
+
@benefit_ratio = benefit_ratio || default_benefit_ratio(relationship_type)
|
|
27
|
+
@activation_count = 0
|
|
28
|
+
@created_at = Time.now.utc
|
|
29
|
+
@last_activated_at = nil
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def strength
|
|
33
|
+
@strength.round(10)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def activate!(amount: 0.05)
|
|
37
|
+
delta = amount.clamp(0.0, 1.0)
|
|
38
|
+
@strength = (@strength + delta).clamp(Constants::MIN_STRENGTH, Constants::MAX_STRENGTH)
|
|
39
|
+
@activation_count += 1
|
|
40
|
+
@last_activated_at = Time.now.utc
|
|
41
|
+
self
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def decay!(rate: Constants::BOND_DECAY)
|
|
45
|
+
@strength = (@strength - rate).clamp(Constants::MIN_STRENGTH, Constants::MAX_STRENGTH)
|
|
46
|
+
self
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def dormant?
|
|
50
|
+
@strength <= Constants::DORMANT_THRESHOLD
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def strong?
|
|
54
|
+
@strength >= Constants::STRONG_THRESHOLD
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def strength_label
|
|
58
|
+
Constants::INTERACTION_STRENGTHS.each do |range, label|
|
|
59
|
+
return label if range.cover?(@strength)
|
|
60
|
+
end
|
|
61
|
+
:dormant
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def involves?(subsystem_id)
|
|
65
|
+
[@subsystem_a, @subsystem_b].include?(subsystem_id)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def partner_of(subsystem_id)
|
|
69
|
+
return @subsystem_b if @subsystem_a == subsystem_id
|
|
70
|
+
return @subsystem_a if @subsystem_b == subsystem_id
|
|
71
|
+
|
|
72
|
+
nil
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def to_h
|
|
76
|
+
{
|
|
77
|
+
bond_id: @bond_id,
|
|
78
|
+
subsystem_a: @subsystem_a,
|
|
79
|
+
subsystem_b: @subsystem_b,
|
|
80
|
+
relationship_type: @relationship_type,
|
|
81
|
+
strength: strength,
|
|
82
|
+
strength_label: strength_label,
|
|
83
|
+
benefit_ratio: @benefit_ratio,
|
|
84
|
+
activation_count: @activation_count,
|
|
85
|
+
dormant: dormant?,
|
|
86
|
+
strong: strong?,
|
|
87
|
+
created_at: @created_at.iso8601,
|
|
88
|
+
last_activated_at: @last_activated_at&.iso8601
|
|
89
|
+
}
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
def validate_relationship_type!(type)
|
|
95
|
+
return if Constants::RELATIONSHIP_TYPES.include?(type)
|
|
96
|
+
|
|
97
|
+
raise ArgumentError, "unknown relationship_type: #{type.inspect}. " \
|
|
98
|
+
"Must be one of #{Constants::RELATIONSHIP_TYPES.inspect}"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def default_benefit_ratio(type)
|
|
102
|
+
range = Constants::BENEFIT_RATIO_RANGES.fetch(type)
|
|
103
|
+
mid = (range.min + range.max) / 2.0
|
|
104
|
+
mid.round(10)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Social
|
|
7
|
+
module Symbiosis
|
|
8
|
+
module Runners
|
|
9
|
+
module CognitiveSymbiosis
|
|
10
|
+
extend self
|
|
11
|
+
|
|
12
|
+
include Legion::Extensions::Helpers::Lex if defined?(Legion::Extensions::Helpers::Lex)
|
|
13
|
+
|
|
14
|
+
def create_bond(subsystem_a:, subsystem_b:, relationship_type:, strength: nil, benefit_ratio: nil, engine: nil, **)
|
|
15
|
+
eng = engine || default_engine
|
|
16
|
+
result = eng.create_bond(
|
|
17
|
+
subsystem_a: subsystem_a,
|
|
18
|
+
subsystem_b: subsystem_b,
|
|
19
|
+
relationship_type: relationship_type.to_sym,
|
|
20
|
+
strength: strength,
|
|
21
|
+
benefit_ratio: benefit_ratio
|
|
22
|
+
)
|
|
23
|
+
msg = if result[:created]
|
|
24
|
+
"[symbiosis] bond created: #{subsystem_a}<->#{subsystem_b} type=#{relationship_type}"
|
|
25
|
+
else
|
|
26
|
+
"[symbiosis] bond skipped: #{result[:reason]}"
|
|
27
|
+
end
|
|
28
|
+
Legion::Logging.debug msg if defined?(Legion::Logging)
|
|
29
|
+
{ success: result[:created], **result }
|
|
30
|
+
rescue ArgumentError => e
|
|
31
|
+
{ success: false, error: e.message }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def activate(subsystem_a:, subsystem_b:, amount: 0.05, engine: nil, **)
|
|
35
|
+
eng = engine || default_engine
|
|
36
|
+
result = eng.activate_interaction(
|
|
37
|
+
subsystem_a: subsystem_a,
|
|
38
|
+
subsystem_b: subsystem_b,
|
|
39
|
+
amount: amount.clamp(0.0, 1.0)
|
|
40
|
+
)
|
|
41
|
+
found = result.fetch(:found, false)
|
|
42
|
+
Legion::Logging.debug "[symbiosis] activate #{subsystem_a}<->#{subsystem_b} found=#{found}" if defined?(Legion::Logging)
|
|
43
|
+
{ success: found, **result }
|
|
44
|
+
rescue ArgumentError => e
|
|
45
|
+
{ success: false, error: e.message }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def health_status(engine: nil, **)
|
|
49
|
+
eng = engine || default_engine
|
|
50
|
+
health = eng.measure_ecosystem_health
|
|
51
|
+
Legion::Logging.debug "[symbiosis] health score=#{health[:score]} label=#{health[:label]}" if defined?(Legion::Logging)
|
|
52
|
+
{ success: true, **health }
|
|
53
|
+
rescue ArgumentError => e
|
|
54
|
+
{ success: false, error: e.message }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def list_bonds(subsystem_id: nil, relationship_type: nil, engine: nil, **)
|
|
58
|
+
eng = engine || default_engine
|
|
59
|
+
bonds = if subsystem_id
|
|
60
|
+
eng.ecosystem.symbiotic_web(subsystem_id).map(&:to_h)
|
|
61
|
+
else
|
|
62
|
+
eng.ecosystem.all_bonds.map(&:to_h)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
bonds = bonds.select { |b| b[:relationship_type] == relationship_type.to_sym } if relationship_type
|
|
66
|
+
Legion::Logging.debug "[symbiosis] list_bonds count=#{bonds.size}" if defined?(Legion::Logging)
|
|
67
|
+
{ success: true, bonds: bonds, count: bonds.size }
|
|
68
|
+
rescue ArgumentError => e
|
|
69
|
+
{ success: false, error: e.message }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def detect_parasites(strength_threshold: 0.0, engine: nil, **)
|
|
73
|
+
eng = engine || default_engine
|
|
74
|
+
parasites = eng.detect_parasites(strength_threshold: strength_threshold.clamp(0.0, 1.0))
|
|
75
|
+
Legion::Logging.debug "[symbiosis] detect_parasites count=#{parasites.size}" if defined?(Legion::Logging)
|
|
76
|
+
{ success: true, parasites: parasites, count: parasites.size }
|
|
77
|
+
rescue ArgumentError => e
|
|
78
|
+
{ success: false, error: e.message }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def ecosystem_report(engine: nil, **)
|
|
82
|
+
eng = engine || default_engine
|
|
83
|
+
report = eng.ecosystem_report
|
|
84
|
+
Legion::Logging.debug "[symbiosis] ecosystem_report health=#{report.dig(:health, :label)}" if defined?(Legion::Logging)
|
|
85
|
+
{ success: true, **report }
|
|
86
|
+
rescue ArgumentError => e
|
|
87
|
+
{ success: false, error: e.message }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def default_engine
|
|
93
|
+
@default_engine ||= Helpers::SymbiosisEngine.new
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
require 'legion/extensions/agentic/social/symbiosis/version'
|
|
6
|
+
require 'legion/extensions/agentic/social/symbiosis/helpers/constants'
|
|
7
|
+
require 'legion/extensions/agentic/social/symbiosis/helpers/symbiotic_bond'
|
|
8
|
+
require 'legion/extensions/agentic/social/symbiosis/helpers/ecosystem'
|
|
9
|
+
require 'legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine'
|
|
10
|
+
require 'legion/extensions/agentic/social/symbiosis/runners/cognitive_symbiosis'
|
|
11
|
+
require 'legion/extensions/agentic/social/symbiosis/client'
|
|
12
|
+
|
|
13
|
+
module Legion
|
|
14
|
+
module Extensions
|
|
15
|
+
module Agentic
|
|
16
|
+
module Social
|
|
17
|
+
module Symbiosis
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|