Class: Legion::Extensions::Agentic::Self::Architecture::Helpers::ArchitectureEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb

Constant Summary

Constants included from Constants

Constants::BOTTLENECK_THRESHOLD, Constants::CONNECTION_TYPES, Constants::DEFAULT_HEALTH, Constants::HEALTH_BOOST, Constants::HEALTH_DECAY, Constants::HEALTH_LABELS, Constants::MAX_CONNECTIONS, Constants::MAX_HISTORY, Constants::MAX_SUBSYSTEMS, Constants::STATUS_LEVELS, Constants::SUBSYSTEM_TYPES

Instance Method Summary collapse

Constructor Details

#initializeArchitectureEngine

Returns a new instance of ArchitectureEngine.



12
13
14
15
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 12

def initialize
  @subsystems  = {}
  @connections = {}
end

Instance Method Details

#activate_subsystem(name:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 50

def activate_subsystem(name:)
  subsystem = find_subsystem!(name)
  subsystem.activate!

  excitatory_connections_from(subsystem.id).each do |conn|
    target = subsystem_by_id(conn.target_id)
    target&.activate!
  end

  { name: subsystem.name, status: subsystem.status, health: subsystem.health }
end

#active_subsystemsObject



78
79
80
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 78

def active_subsystems
  @subsystems.values.select { |s| s.status == :active }
end

#architecture_graphObject



106
107
108
109
110
111
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 106

def architecture_graph
  {
    nodes: @subsystems.values.map(&:to_h),
    edges: @connections.values.map(&:to_h)
  }
end

#architecture_healthObject



99
100
101
102
103
104
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 99

def architecture_health
  return 0.0 if @subsystems.empty?

  total = @subsystems.values.sum(&:health)
  total / @subsystems.size
end

#bottlenecked_subsystemsObject



82
83
84
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 82

def bottlenecked_subsystems
  @subsystems.values.select(&:bottlenecked?)
end

#connections_for(name:) ⇒ Object



86
87
88
89
90
91
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 86

def connections_for(name:)
  subsystem = find_subsystem!(name)
  @connections.values.select do |c|
    c.source_id == subsystem.id || c.target_id == subsystem.id
  end
end

#create_connection(source_name:, target_name:, connection_type: :informational, weight: 0.5) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 30

def create_connection(source_name:, target_name:, connection_type: :informational, weight: 0.5)
  unless Constants::CONNECTION_TYPES.include?(connection_type.to_sym)
    raise ArgumentError,
          "invalid connection_type: #{connection_type.inspect}, must be one of #{Constants::CONNECTION_TYPES}"
  end
  raise ArgumentError, "max connections (#{Constants::MAX_CONNECTIONS}) reached" if @connections.size >= Constants::MAX_CONNECTIONS

  source = find_subsystem!(source_name)
  target = find_subsystem!(target_name)

  conn = Connection.new(
    source_id:       source.id,
    target_id:       target.id,
    connection_type: connection_type,
    weight:          weight
  )
  @connections[conn.id] = conn
  conn
end

#decay_allObject



113
114
115
116
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 113

def decay_all
  @subsystems.each_value { |s| s.degrade!(Constants::HEALTH_DECAY) }
  { decayed: @subsystems.size }
end

#degrade_subsystem(name:) ⇒ Object



62
63
64
65
66
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 62

def degrade_subsystem(name:)
  subsystem = find_subsystem!(name)
  subsystem.degrade!
  { name: subsystem.name, status: subsystem.status, health: subsystem.health }
end

#downstream(name:, max_depth: 5) ⇒ Object



93
94
95
96
97
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 93

def downstream(name:, max_depth: 5)
  start   = find_subsystem!(name)
  visited = bfs_visited(start.id, max_depth)
  visited.keys.filter_map { |sid| subsystem_by_id(sid) }.reject { |s| s.id == start.id }
end

#recover_subsystem(name:) ⇒ Object



68
69
70
71
72
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 68

def recover_subsystem(name:)
  subsystem = find_subsystem!(name)
  subsystem.recover!
  { name: subsystem.name, status: subsystem.status, health: subsystem.health }
end

#register_subsystem(name:, subsystem_type:, health: Constants::DEFAULT_HEALTH) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 17

def register_subsystem(name:, subsystem_type:, health: Constants::DEFAULT_HEALTH)
  unless Constants::SUBSYSTEM_TYPES.include?(subsystem_type.to_sym)
    raise ArgumentError,
          "invalid subsystem_type: #{subsystem_type.inspect}, must be one of #{Constants::SUBSYSTEM_TYPES}"
  end
  raise ArgumentError, "subsystem '#{name}' already registered" if @subsystems.key?(name.to_sym)
  raise ArgumentError, "max subsystems (#{Constants::MAX_SUBSYSTEMS}) reached" if @subsystems.size >= Constants::MAX_SUBSYSTEMS

  subsystem = Subsystem.new(name: name, subsystem_type: subsystem_type, health: health)
  @subsystems[subsystem.name] = subsystem
  subsystem
end

#subsystem_status(name:) ⇒ Object



74
75
76
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 74

def subsystem_status(name:)
  find_subsystem!(name).to_h
end

#to_hObject



118
119
120
121
122
123
124
125
126
# File 'lib/legion/extensions/agentic/self/architecture/helpers/architecture_engine.rb', line 118

def to_h
  {
    subsystem_count:     @subsystems.size,
    connection_count:    @connections.size,
    active_count:        active_subsystems.size,
    bottlenecked_count:  bottlenecked_subsystems.size,
    architecture_health: architecture_health
  }
end