Class: Legion::Extensions::Agentic::Attention::Switching::Helpers::SwitchingEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Attention::Switching::Helpers::SwitchingEngine
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb
Constant Summary
Constants included
from Constants
Constants::CONTEXT_RESTORATION_COST, Constants::COST_LABELS, Constants::DEFAULT_SWITCH_COST, Constants::HIGH_COST_THRESHOLD, Constants::LOW_COST_THRESHOLD, Constants::MAX_SWITCH_EVENTS, Constants::MAX_TASK_SETS, Constants::PRACTICE_REDUCTION, Constants::READINESS_LABELS, Constants::READY_THRESHOLD, Constants::RESIDUAL_DECAY_RATE, Constants::RESIDUAL_LABELS, Constants::TASK_SET_TYPES, Constants::WARMUP_RATE
Instance Method Summary
collapse
Constructor Details
Returns a new instance of SwitchingEngine.
12
13
14
15
16
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 12
def initialize
@task_sets = {}
@switch_events = []
@active_task_id = nil
end
|
Instance Method Details
#activate_task(task_id:) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 27
def activate_task(task_id:)
task = @task_sets[task_id]
return nil unless task
task.activate!
@active_task_id = task_id
task
end
|
#active_task ⇒ Object
64
65
66
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 64
def active_task
@task_sets[@active_task_id]
end
|
#average_switch_cost ⇒ Object
76
77
78
79
80
81
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 76
def average_switch_cost
return DEFAULT_SWITCH_COST if @switch_events.empty?
costs = @switch_events.map(&:switch_cost)
(costs.sum / costs.size).round(10)
end
|
#cheap_switches ⇒ Object
87
88
89
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 87
def cheap_switches
@switch_events.select(&:cheap?)
end
|
#costly_switches ⇒ Object
83
84
85
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 83
def costly_switches
@switch_events.select(&:costly?)
end
|
#decay_all_residuals! ⇒ Object
59
60
61
62
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 59
def decay_all_residuals!
@task_sets.each_value(&:decay_residual!)
{ tasks_decayed: @task_sets.size }
end
|
#most_costly_pair ⇒ Object
99
100
101
102
103
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 99
def most_costly_pair
return nil if @switch_events.empty?
@switch_events.max_by(&:switch_cost)
end
|
#recent_switches(limit: 10) ⇒ Object
72
73
74
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 72
def recent_switches(limit: 10)
@switch_events.last(limit)
end
|
#register_task(name:, task_type: :analytical, complexity: 0.5) ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 18
def register_task(name:, task_type: :analytical, complexity: 0.5)
return nil unless TASK_SET_TYPES.include?(task_type.to_sym)
prune_tasks_if_needed
task = TaskSet.new(name: name, task_type: task_type, complexity: complexity)
@task_sets[task.id] = task
task
end
|
#switch_cost_between(from_id:, to_id:) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 91
def switch_cost_between(from_id:, to_id:)
relevant = @switch_events.select { |e| e.from_task_id == from_id && e.to_task_id == to_id }
return nil if relevant.empty?
costs = relevant.map(&:switch_cost)
(costs.sum / costs.size).round(10)
end
|
#switch_to(task_id:) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 36
def switch_to(task_id:)
target = @task_sets[task_id]
return nil unless target
current = @task_sets[@active_task_id]
event = if current && @active_task_id != task_id
perform_switch(current, target)
else
activate_task(task_id: task_id)
nil
end
@active_task_id = task_id
{ task: target.to_h, switch_event: event&.to_h }
end
|
#switching_report ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 105
def switching_report
{
total_tasks: @task_sets.size,
total_switches: @switch_events.size,
active_task: active_task&.to_h,
average_switch_cost: average_switch_cost,
costly_count: costly_switches.size,
cheap_count: cheap_switches.size,
residual_count: tasks_with_residual.size,
recent_switches: recent_switches(limit: 5).map(&:to_h)
}
end
|
#tasks_with_residual ⇒ Object
68
69
70
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 68
def tasks_with_residual
@task_sets.values.select(&:residual?)
end
|
#to_h ⇒ Object
118
119
120
121
122
123
124
125
126
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 118
def to_h
{
total_tasks: @task_sets.size,
total_switches: @switch_events.size,
active_task_id: @active_task_id,
average_switch_cost: average_switch_cost,
residual_count: tasks_with_residual.size
}
end
|
#warmup_active ⇒ Object
52
53
54
55
56
57
|
# File 'lib/legion/extensions/agentic/attention/switching/helpers/switching_engine.rb', line 52
def warmup_active
task = @task_sets[@active_task_id]
return nil unless task
task.warmup!
end
|