Module: Legion::Extensions::Agentic::Homeostasis::Core::Runners::Homeostasis

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/homeostasis/core/runners/homeostasis.rb

Instance Method Summary collapse

Instance Method Details

#allostatic_statusObject



51
52
53
54
55
# File 'lib/legion/extensions/agentic/homeostasis/core/runners/homeostasis.rb', line 51

def allostatic_status(**)
  log.debug("[homeostasis] allostatic load=#{allostatic_load.load.round(2)} (#{allostatic_load.classification})")

  allostatic_load.to_h
end

#homeostasis_statsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/legion/extensions/agentic/homeostasis/core/runners/homeostasis.rb', line 68

def homeostasis_stats(**)
  {
    regulation_count:     regulator.regulation_count,
    regulation_health:    regulator.regulation_health,
    health_label:         regulator.health_label,
    allostatic_load:      allostatic_load.load,
    allostatic_class:     allostatic_load.classification,
    allostatic_peak:      allostatic_load.peak_load,
    allostatic_trend:     allostatic_load.trend,
    subsystems_tracked:   regulator.setpoints.size,
    subsystems_deviating: regulator.setpoints.values.count { |element| !element.within_tolerance? },
    worst_deviation:      regulator.worst_deviation
  }
end

#modulation_for(subsystem:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/homeostasis/core/runners/homeostasis.rb', line 36

def modulation_for(subsystem:, **)
  sym = subsystem.to_sym
  signal = regulator.signals[sym]
  sp_status = regulator.subsystem_status(sym)

  log.debug("[homeostasis] modulation query: #{sym} -> #{signal&.dig(:type) || :unknown}")

  {
    subsystem:       sym,
    signal:          signal || { type: :hold, magnitude: 0.0, direction: 0.0 },
    setpoint:        sp_status&.slice(:target, :tolerance, :current_value, :error, :within_tolerance),
    allostatic_load: allostatic_load.load
  }
end

#regulate(tick_results: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/agentic/homeostasis/core/runners/homeostasis.rb', line 13

def regulate(tick_results: {}, **)
  observations = extract_observations(tick_results)
  signals = regulator.regulate(observations)
  allostatic_load.update(regulator)

  # Adapt setpoints slowly when system is stable
  regulator.adapt_setpoints if allostatic_load.classification == :healthy

  log.debug("[homeostasis] regulated #{signals.size} subsystems, " \
            "health=#{regulator.regulation_health.round(2)}, " \
            "allostatic=#{allostatic_load.load.round(2)}")

  {
    signals:              signals,
    regulation_health:    regulator.regulation_health,
    health_label:         regulator.health_label,
    allostatic_load:      allostatic_load.load,
    allostatic_class:     allostatic_load.classification,
    subsystems_regulated: signals.size,
    worst_deviation:      regulator.worst_deviation
  }
end

#regulation_overviewObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/homeostasis/core/runners/homeostasis.rb', line 57

def regulation_overview(**)
  {
    health:           regulator.regulation_health,
    health_label:     regulator.health_label,
    allostatic_load:  allostatic_load.to_h,
    setpoints:        regulator.setpoints.transform_values(&:to_h),
    signals:          regulator.signals,
    regulation_count: regulator.regulation_count
  }
end