Class: Legion::Extensions::Agentic::Memory::Reserve::Helpers::ReserveEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb

Constant Summary

Constants included from Constants

Constants::CAPACITY_CEILING, Constants::CAPACITY_FLOOR, Constants::COMPENSATION_DECAY, Constants::COMPENSATION_EFFICIENCY, Constants::DEFAULT_CAPACITY, Constants::DEGRADED_THRESHOLD, Constants::FAILED_THRESHOLD, Constants::MAX_COMPENSATIONS, Constants::MAX_HISTORY, Constants::MAX_PATHWAYS, Constants::PATHWAY_STATES, Constants::RECOVERY_RATE, Constants::RESERVE_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReserveEngine

Returns a new instance of ReserveEngine.



14
15
16
17
18
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 14

def initialize
  @pathways      = {}
  @pathway_count = 0
  @history       = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



12
13
14
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 12

def history
  @history
end

#pathwaysObject (readonly)

Returns the value of attribute pathways.



12
13
14
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 12

def pathways
  @pathways
end

Instance Method Details

#add_pathway(function:, domain: :general, capacity: DEFAULT_CAPACITY) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 20

def add_pathway(function:, domain: :general, capacity: DEFAULT_CAPACITY)
  return nil if @pathways.size >= MAX_PATHWAYS

  @pathway_count += 1
  pathway = Pathway.new(
    id:       :"path_#{@pathway_count}",
    function: function,
    domain:   domain,
    capacity: capacity
  )
  @pathways[pathway.id] = pathway
  record_event(:add_pathway, pathway_id: pathway.id)
  pathway
end

#damage_pathway(pathway_id:, amount:) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 45

def damage_pathway(pathway_id:, amount:)
  pathway = @pathways[pathway_id]
  return nil unless pathway

  pathway.damage(amount: amount)
  activate_compensation(pathway) if pathway.degraded?
  record_event(:damage, pathway_id: pathway_id, amount: amount, state: pathway.state)
  pathway
end

#degraded_pathwaysObject



87
88
89
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 87

def degraded_pathways
  @pathways.values.select(&:degraded?).map(&:to_h)
end

#domain_reserve(domain:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 99

def domain_reserve(domain:)
  relevant = @pathways.values.select { |p| p.domain == domain }
  return DEFAULT_CAPACITY if relevant.empty?

  caps = relevant.map do |p|
    backup_caps = p.backup_ids.filter_map { |bid| @pathways[bid]&.capacity }
    p.effective_capacity(backup_capacities: backup_caps)
  end
  caps.sum / caps.size.to_f
end

#effective_capacity(pathway_id:) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 64

def effective_capacity(pathway_id:)
  pathway = @pathways[pathway_id]
  return nil unless pathway

  backup_caps = pathway.backup_ids.filter_map { |bid| @pathways[bid]&.capacity }
  pathway.effective_capacity(backup_capacities: backup_caps)
end

#failed_pathwaysObject



91
92
93
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 91

def failed_pathways
  @pathways.values.select(&:failed?).map(&:to_h)
end

#healthy_pathwaysObject



95
96
97
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 95

def healthy_pathways
  @pathways.values.select(&:healthy?).map(&:to_h)
end


35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 35

def link_backup(primary_id:, backup_id:)
  primary = @pathways[primary_id]
  backup  = @pathways[backup_id]
  return nil unless primary && backup

  primary.add_backup(pathway_id: backup_id)
  record_event(:link_backup, primary_id: primary_id, backup_id: backup_id)
  primary
end

#most_redundant(limit: 5) ⇒ Object



117
118
119
120
121
122
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 117

def most_redundant(limit: 5)
  @pathways.values
           .sort_by { |p| -p.redundancy }
           .first(limit)
           .map(&:to_h)
end

#most_vulnerable(limit: 5) ⇒ Object



110
111
112
113
114
115
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 110

def most_vulnerable(limit: 5)
  @pathways.values
           .sort_by(&:capacity)
           .first(limit)
           .map(&:to_h)
end

#overall_reserveObject



72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 72

def overall_reserve
  return DEFAULT_CAPACITY if @pathways.empty?

  caps = @pathways.values.map do |p|
    backup_caps = p.backup_ids.filter_map { |bid| @pathways[bid]&.capacity }
    p.effective_capacity(backup_capacities: backup_caps)
  end
  caps.sum / caps.size.to_f
end

#recover_allObject



124
125
126
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 124

def recover_all
  @pathways.each_value { |p| p.recover unless p.failed? }
end

#recover_pathway(pathway_id:, amount: RECOVERY_RATE) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 55

def recover_pathway(pathway_id:, amount: RECOVERY_RATE)
  pathway = @pathways[pathway_id]
  return nil unless pathway

  pathway.recover(amount: amount)
  record_event(:recover, pathway_id: pathway_id)
  pathway
end

#reserve_labelObject



82
83
84
85
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 82

def reserve_label
  ratio = overall_reserve
  RESERVE_LABELS.find { |range, _| range.cover?(ratio) }&.last || :critical
end

#to_hObject



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/legion/extensions/agentic/memory/reserve/helpers/reserve_engine.rb', line 128

def to_h
  {
    pathway_count:   @pathways.size,
    overall_reserve: overall_reserve.round(4),
    reserve_label:   reserve_label,
    healthy_count:   @pathways.values.count(&:healthy?),
    degraded_count:  @pathways.values.count(&:degraded?),
    failed_count:    @pathways.values.count(&:failed?),
    history_size:    @history.size
  }
end