Class: Legion::Extensions::Agentic::Executive::Triage::Helpers::TriageEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb

Constant Summary

Constants included from Constants

Constants::CAPACITY_DEFAULT, Constants::CAPACITY_DRAIN, Constants::CAPACITY_LABELS, Constants::CAPACITY_RESTORE, Constants::MAX_DEMANDS, Constants::MAX_QUEUE_SIZE, Constants::OVERLOAD_THRESHOLD, Constants::QUEUE_LABELS, Constants::SEVERITY_LEVELS, Constants::SEVERITY_WEIGHTS, Constants::TRIAGE_LABELS, Constants::URGENCY_LEVELS, Constants::URGENCY_WEIGHTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initializeTriageEngine

Returns a new instance of TriageEngine.



14
15
16
17
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 14

def initialize
  @demands  = {}
  @capacity = CAPACITY_DEFAULT
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



12
13
14
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 12

def capacity
  @capacity
end

Instance Method Details

#active_demandsObject



69
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 69

def active_demands = @demands.values.select(&:active?)

#add_demand(description:, domain: :general, severity: :moderate, urgency: :soon) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 19

def add_demand(description:, domain: :general, severity: :moderate, urgency: :soon)
  prune_if_needed
  demand = Demand.new(
    description: description, domain: domain, severity: severity, urgency: urgency
  )
  @demands[demand.id] = demand
  drain_capacity!(demand.triage_score * CAPACITY_DRAIN)
  demand.triage!
  demand
end

#capacity_labelObject



93
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 93

def capacity_label = Constants.label_for(CAPACITY_LABELS, @capacity)

#complete_demand(demand_id:) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 38

def complete_demand(demand_id:)
  demand = @demands[demand_id]
  return nil unless demand

  demand.complete!
  restore_capacity!(CAPACITY_RESTORE)
  demand
end

#completed_demandsObject



71
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 71

def completed_demands = @demands.values.select { |d| d.status == :completed }

#defer_demand(demand_id:) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 47

def defer_demand(demand_id:)
  demand = @demands[demand_id]
  return nil unless demand&.active?

  demand.defer!
  restore_capacity!(CAPACITY_RESTORE * 0.5)
  demand
end

#deferred_demandsObject



73
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 73

def deferred_demands = @demands.values.select { |d| d.status == :deferred }

#demands_by_domain(domain:) ⇒ Object



79
80
81
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 79

def demands_by_domain(domain:)
  @demands.values.select { |d| d.domain == domain.to_sym }
end

#demands_by_severity(severity:) ⇒ Object



75
76
77
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 75

def demands_by_severity(severity:)
  @demands.values.select { |d| d.severity == severity.to_sym }
end

#drop_demand(demand_id:) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 56

def drop_demand(demand_id:)
  demand = @demands[demand_id]
  return nil unless demand&.active?

  demand.drop!
  restore_capacity!(CAPACITY_RESTORE)
  demand
end

#dropped_demandsObject



72
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 72

def dropped_demands = @demands.values.select { |d| d.status == :dropped }

#next_demandObject



65
66
67
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 65

def next_demand
  active_demands.max_by(&:triage_score)
end

#overloaded?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 83

def overloaded?
  @capacity <= OVERLOAD_THRESHOLD
end

#process_demand(demand_id:) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 30

def process_demand(demand_id:)
  demand = @demands[demand_id]
  return nil unless demand&.active?

  demand.process!
  demand
end

#queue_labelObject



94
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 94

def queue_label = Constants.label_for(QUEUE_LABELS, queue_pressure)

#queue_pressureObject



87
88
89
90
91
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 87

def queue_pressure
  return 0.0 if @demands.empty?

  (active_demands.size.to_f / MAX_QUEUE_SIZE).clamp(0.0, 1.0).round(10)
end

#red_demandsObject



70
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 70

def red_demands = @demands.values.select(&:red?)

#restore_capacity!(amount = CAPACITY_RESTORE) ⇒ Object



96
97
98
99
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 96

def restore_capacity!(amount = CAPACITY_RESTORE)
  @capacity = (@capacity + amount).clamp(0.0, 1.0).round(10)
  self
end

#to_hObject



118
119
120
121
122
123
124
125
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 118

def to_h
  {
    total_demands: @demands.size,
    active:        active_demands.size,
    capacity:      @capacity,
    overloaded:    overloaded?
  }
end

#triage_reportObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/legion/extensions/agentic/executive/triage/helpers/triage_engine.rb', line 101

def triage_report
  {
    total_demands:  @demands.size,
    active_count:   active_demands.size,
    red_count:      red_demands.size,
    completed:      completed_demands.size,
    dropped:        dropped_demands.size,
    deferred:       deferred_demands.size,
    capacity:       @capacity,
    capacity_label: capacity_label,
    overloaded:     overloaded?,
    queue_pressure: queue_pressure,
    queue_label:    queue_label,
    next_demand:    next_demand&.to_h
  }
end