Class: Legion::Extensions::Agentic::Social::Symbiosis::Helpers::SymbiosisEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSymbiosisEngine

Returns a new instance of SymbiosisEngine.



10
11
12
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 10

def initialize
  @ecosystem = Ecosystem.new
end

Instance Attribute Details

#ecosystemObject (readonly)

Returns the value of attribute ecosystem.



97
98
99
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 97

def ecosystem
  @ecosystem
end

Instance Method Details

#activate_interaction(subsystem_a:, subsystem_b:, amount: 0.05) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 31

def activate_interaction(subsystem_a:, subsystem_b:, amount: 0.05)
  bond = @ecosystem.find_bond(subsystem_a, subsystem_b)
  return { found: false, subsystem_a: subsystem_a, subsystem_b: subsystem_b } unless bond

  result = @ecosystem.activate_bond(bond.bond_id, amount: amount)
  result.merge(
    relationship_type: bond.relationship_type,
    benefit_ratio:     bond.benefit_ratio
  )
end

#create_bond(subsystem_a:, subsystem_b:, relationship_type:, strength: nil, benefit_ratio: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 14

def create_bond(subsystem_a:, subsystem_b:, relationship_type:, strength: nil, benefit_ratio: nil)
  existing = @ecosystem.find_bond(subsystem_a, subsystem_b)
  return { created: false, reason: :already_exists, bond: existing.to_h } if existing

  bond = SymbioticBond.new(
    subsystem_a:       subsystem_a,
    subsystem_b:       subsystem_b,
    relationship_type: relationship_type,
    strength:          strength,
    benefit_ratio:     benefit_ratio
  )
  @ecosystem.register_bond(bond)
  { created: true, bond: bond.to_h }
rescue ArgumentError => e
  { created: false, reason: :invalid_arguments, error: e.message }
end

#decay_allObject



92
93
94
95
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 92

def decay_all
  decayed = @ecosystem.decay_all!
  { decayed: decayed, health_after: measure_ecosystem_health }
end

#detect_parasites(strength_threshold: 0.0) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 63

def detect_parasites(strength_threshold: 0.0)
  @ecosystem.all_bonds
            .select { |b| b.relationship_type == :parasitic }
            .reject(&:dormant?)
            .select { |b| b.strength >= strength_threshold }
            .sort_by(&:benefit_ratio)
            .map(&:to_h)
end

#ecosystem_reportObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 72

def ecosystem_report
  bonds_by_type = Constants::RELATIONSHIP_TYPES.to_h do |type|
    count = @ecosystem.all_bonds.count { |b| b.relationship_type == type && !b.dormant? }
    [type, count]
  end

  health    = measure_ecosystem_health
  strongest = @ecosystem.most_beneficial&.to_h
  weakest   = @ecosystem.most_parasitic&.to_h

  {
    health:          health,
    bonds_by_type:   bonds_by_type,
    most_beneficial: strongest,
    most_parasitic:  weakest,
    total_bonds:     @ecosystem.bond_count,
    dormant_bonds:   @ecosystem.all_bonds.count(&:dormant?)
  }
end

#find_partners(subsystem_id, min_benefit_ratio: 0.0) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 53

def find_partners(subsystem_id, min_benefit_ratio: 0.0)
  @ecosystem.symbiotic_web(subsystem_id)
            .reject(&:dormant?)
            .select { |b| b.benefit_ratio >= min_benefit_ratio }
            .sort_by { |b| -b.strength }
            .map do |b|
              b.to_h.merge(partner: b.partner_of(subsystem_id))
            end
end

#measure_ecosystem_healthObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/symbiosis_engine.rb', line 42

def measure_ecosystem_health
  score = @ecosystem.measure_health
  {
    score:           score,
    label:           @ecosystem.health_label,
    bond_count:      @ecosystem.bond_count,
    active_bonds:    @ecosystem.active_bonds.size,
    network_density: @ecosystem.network_density
  }
end