Class: Legion::Extensions::Agentic::Executive::Autopilot::Helpers::AutopilotEngine

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

Constant Summary

Constants included from Constants

Constants::AUTOPILOT_COST, Constants::AUTOPILOT_THRESHOLD, Constants::DEFAULT_ENERGY, Constants::DELIBERATE_COST, Constants::DELIBERATE_THRESHOLD, Constants::ENERGY_LABELS, Constants::FAMILIARITY_BOOST, Constants::FAMILIARITY_DECAY, Constants::FAMILIARITY_LABELS, Constants::MAX_EVENTS, Constants::MAX_ROUTINES, Constants::MODE_LABELS, Constants::OVERRIDE_COST, Constants::PROCESSING_MODES, Constants::TASK_DOMAINS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initializeAutopilotEngine

Returns a new instance of AutopilotEngine.



14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 14

def initialize
  @routines     = {}
  @events       = {}
  @current_mode = :deliberate
  @energy       = DEFAULT_ENERGY
end

Instance Attribute Details

#current_modeObject (readonly)

Returns the value of attribute current_mode.



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

def current_mode
  @current_mode
end

#energyObject (readonly)

Returns the value of attribute energy.



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

def energy
  @energy
end

Instance Method Details

#autopilot_ratioObject



69
70
71
72
73
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 69

def autopilot_ratio
  return 0.0 if @routines.empty?

  (autopilot_routines.size.to_f / @routines.size).round(10)
end

#autopilot_reportObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 91

def autopilot_report
  {
    current_mode:    @current_mode,
    energy:          @energy,
    energy_label:    energy_label,
    exhausted:       exhausted?,
    total_routines:  @routines.size,
    autopilot_ready: autopilot_routines.size,
    novel_count:     novel_routines.size,
    autopilot_ratio: autopilot_ratio,
    mode_label:      mode_label,
    total_events:    @events.size,
    override_count:  override_count,
    most_familiar:   most_familiar(limit: 3).map(&:to_h)
  }
end

#autopilot_routinesObject



66
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 66

def autopilot_routines = @routines.values.select(&:autopilot_ready?)

#decay_all!Object



61
62
63
64
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 61

def decay_all!
  @routines.each_value(&:decay!)
  { routines_decayed: @routines.size }
end

#energy_labelObject



84
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 84

def energy_label = Constants.label_for(ENERGY_LABELS, @energy)

#execute_routine(routine_id:, success: true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 28

def execute_routine(routine_id:, success: true)
  routine = @routines[routine_id]
  return nil unless routine

  routine.execute!(success: success)
  cost = routine.autopilot_ready? ? AUTOPILOT_COST : DELIBERATE_COST
  drain_energy!(cost)
  auto_switch_mode(routine)
  routine
end

#exhausted?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 87

def exhausted?
  @energy <= 0.2
end

#find_routine(routine_id:) ⇒ Object



80
81
82
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 80

def find_routine(routine_id:)
  @routines[routine_id]
end

#least_familiar(limit: 5) ⇒ Object



78
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 78

def least_familiar(limit: 5) = @routines.values.sort_by(&:familiarity).first(limit)

#mode_labelObject



85
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 85

def mode_label = Constants.label_for(MODE_LABELS, autopilot_ratio)

#most_familiar(limit: 5) ⇒ Object



77
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 77

def most_familiar(limit: 5) = @routines.values.sort_by { |r| -r.familiarity }.first(limit)

#novel_routinesObject



67
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 67

def novel_routines = @routines.values.select(&:novel?)

#override_countObject



75
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 75

def override_count = @events.values.count(&:override?)

#register_routine(pattern:, domain: :routine, familiarity: 0.0) ⇒ Object



21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 21

def register_routine(pattern:, domain: :routine, familiarity: 0.0)
  prune_routines
  routine = Routine.new(pattern: pattern, domain: domain, familiarity: familiarity)
  @routines[routine.id] = routine
  routine
end

#rest!(amount: 0.1) ⇒ Object



56
57
58
59
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 56

def rest!(amount: 0.1)
  @energy = (@energy + amount).clamp(0.0, 1.0).round(10)
  self
end

#switch_to_autopilot!(trigger: 'manual') ⇒ Object



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

def switch_to_autopilot!(trigger: 'manual')
  return nil if @current_mode == :autopilot

  record_event(from: @current_mode, to: :autopilot, trigger: trigger, cost: 0.0)
  @current_mode = :autopilot
  @current_mode
end

#switch_to_deliberate!(trigger: 'manual') ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 39

def switch_to_deliberate!(trigger: 'manual')
  return nil if @current_mode == :deliberate

  drain_energy!(OVERRIDE_COST)
  record_event(from: @current_mode, to: :deliberate, trigger: trigger, cost: OVERRIDE_COST)
  @current_mode = :deliberate
  @current_mode
end

#to_hObject



108
109
110
111
112
113
114
115
# File 'lib/legion/extensions/agentic/executive/autopilot/helpers/autopilot_engine.rb', line 108

def to_h
  {
    current_mode:    @current_mode,
    energy:          @energy,
    total_routines:  @routines.size,
    autopilot_ratio: autopilot_ratio
  }
end