Module: Legion::Extensions::Tick::Runners::Orchestrator

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/tick/runners/orchestrator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.remote_invocable?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/legion/extensions/tick/runners/orchestrator.rb', line 11

def self.remote_invocable?
  false
end

Instance Method Details

#evaluate_mode_transition(signals: [], emergency: nil, dream_complete: false) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/legion/extensions/tick/runners/orchestrator.rb', line 28

def evaluate_mode_transition(signals: [], emergency: nil, dream_complete: false, **) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  state = tick_state
  previous_mode = state.mode

  # Emergency promotion
  if emergency && Helpers::Constants::EMERGENCY_TRIGGERS.include?(emergency)
    log.warn "[tick] emergency promotion triggered: #{emergency}"
    state.transition_to(:full_active)
    return { transitioned: true, new_mode: :full_active, previous_mode: previous_mode, reason: :emergency }
  end

  # Check signal-based promotions
  max_salience = signals.map { |s| s.is_a?(Hash) ? (s[:salience] || 0.0) : 0.0 }.max || 0.0
  has_human = signals.any? { |s| s.is_a?(Hash) && s[:source_type] == :human_direct }

  new_mode = case state.mode
             when :dormant
               if signals.any?
                 :sentinel
               elsif state.seconds_since_signal >= Helpers::Constants::DREAM_IDLE_THRESHOLD
                 :dormant_active
               else
                 :dormant
               end
             when :dormant_active
               if max_salience >= Helpers::Constants::HIGH_SALIENCE_THRESHOLD || has_human
                 :sentinel
               elsif dream_complete
                 :dormant
               else
                 :dormant_active
               end
             when :sentinel
               if has_human || max_salience >= Helpers::Constants::HIGH_SALIENCE_THRESHOLD
                 :full_active
               elsif state.seconds_since_signal >= Helpers::Constants::SENTINEL_TO_DREAM_THRESHOLD
                 :dormant_active
               elsif state.seconds_since_signal >= Helpers::Constants::SENTINEL_TIMEOUT
                 :dormant
               else
                 :sentinel
               end
             when :full_active
               if full_active_cooldown_elapsed(state) >= Helpers::Constants::ACTIVE_TIMEOUT
                 :sentinel
               else
                 :full_active
               end
             end

  if new_mode == state.mode
    { transitioned: false, current_mode: state.mode }
  else
    state.transition_to(new_mode)
    log.info "[tick] mode transition: #{previous_mode} -> #{new_mode} (threshold)"
    { transitioned: true, new_mode: new_mode, previous_mode: previous_mode, reason: :threshold }
  end
end

#execute_tick(signals: [], phase_handlers: {}, **context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/tick/runners/orchestrator.rb', line 15

def execute_tick(signals: [], phase_handlers: {}, **context)
  if defined?(Legion::Telemetry::OpenInference)
    state = tick_state
    Legion::Telemetry::OpenInference.agent_span(
      name: "tick-#{state.tick_count + 1}", mode: state.mode,
      phase_count: Helpers::Constants.phases_for_mode(state.mode).size,
      budget_ms: (Helpers::Constants.tick_budget(state.mode) * 1000).round
    ) { |_span| execute_tick_impl(signals: signals, phase_handlers: phase_handlers, **context) }
  else
    execute_tick_impl(signals: signals, phase_handlers: phase_handlers, **context)
  end
end

#set_mode(mode:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/legion/extensions/tick/runners/orchestrator.rb', line 93

def set_mode(mode:, **)
  unless Helpers::Constants::MODES.include?(mode)
    log.warn "[tick] invalid mode requested: #{mode}"
    return { error: :invalid_mode, valid_modes: Helpers::Constants::MODES }
  end

  previous = tick_state.mode
  tick_state.transition_to(mode)
  log.info "[tick] mode forced: #{previous} -> #{mode}"
  { mode: mode }
end

#tick_statusObject



87
88
89
90
91
# File 'lib/legion/extensions/tick/runners/orchestrator.rb', line 87

def tick_status(**)
  status = tick_state.to_h
  log.debug "[tick] status query: mode=#{status[:mode]} tick_count=#{status[:tick_count]}"
  status
end