Module: Legion::LLM::EscalationTracker

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/escalation_tracker.rb

Constant Summary collapse

MAX_HISTORY =
200

Class Method Summary collapse

Class Method Details

.clearObject



34
35
36
37
# File 'lib/legion/llm/escalation_tracker.rb', line 34

def clear
  @history = []
  log.debug('[llm][escalation] history_cleared')
end

.escalation_rate(window_seconds: 3600) ⇒ Object



52
53
54
55
56
# File 'lib/legion/llm/escalation_tracker.rb', line 52

def escalation_rate(window_seconds: 3600)
  cutoff = Time.now.utc - window_seconds
  recent = history.count { |e| e[:recorded_at] >= cutoff }
  { count: recent, window_seconds: window_seconds }
end

.historyObject



30
31
32
# File 'lib/legion/llm/escalation_tracker.rb', line 30

def history
  @history ||= []
end

.record(from_model:, to_model:, reason:, tier_from: nil, tier_to: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/llm/escalation_tracker.rb', line 12

def record(from_model:, to_model:, reason:, tier_from: nil, tier_to: nil)
  entry = {
    from_model:  from_model.to_s,
    to_model:    to_model.to_s,
    reason:      reason.to_s,
    tier_from:   tier_from,
    tier_to:     tier_to,
    recorded_at: Time.now.utc
  }
  history << entry
  history.shift while history.size > MAX_HISTORY
  log.info(
    "[llm][escalation] recorded from_model=#{from_model} to_model=#{to_model} " \
    "reason=#{reason} tier_from=#{tier_from || 'none'} tier_to=#{tier_to || 'none'}"
  )
  entry
end

.summaryObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/llm/escalation_tracker.rb', line 39

def summary
  entries = history.dup
  return empty_summary if entries.empty?

  {
    total_escalations: entries.size,
    by_reason:         count_by(entries, :reason),
    by_target_model:   count_by(entries, :to_model),
    by_source_model:   count_by(entries, :from_model),
    recent:            entries.last(5).reverse
  }
end