Module: Legion::Extensions::Agentic::Self::Reflection::Runners::Reflection

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/self/reflection/runners/reflection.rb

Instance Method Summary collapse

Instance Method Details

#adapt(reflection_id:) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/self/reflection/runners/reflection.rb', line 91

def adapt(reflection_id:, **)
  reflection = reflection_store.get(reflection_id)
  return { error: :not_found } unless reflection
  return { error: :already_acted } if reflection[:acted_on]

  reflection_store.mark_acted_on(reflection_id)
  log.info "[reflection] adapted: #{reflection[:observation]}"
  { adapted: true, reflection_id: reflection_id, recommendation: reflection[:recommendation] }
end

#cognitive_healthObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/extensions/agentic/self/reflection/runners/reflection.rb', line 68

def cognitive_health(**)
  health = reflection_store.cognitive_health
  log.debug "[reflection] cognitive health: #{health}"
  {
    health:            health,
    category_scores:   Helpers::Constants::CATEGORIES.to_h { |c| [c, reflection_store.category_score(c)] },
    unacted_count:     reflection_store.unacted.size,
    critical_count:    reflection_store.by_severity(:critical).size,
    significant_count: reflection_store.by_severity(:significant).size
  }
end

#recent_reflections(limit: 10) ⇒ Object



80
81
82
83
# File 'lib/legion/extensions/agentic/self/reflection/runners/reflection.rb', line 80

def recent_reflections(limit: 10, **)
  reflections = reflection_store.recent(limit: limit)
  { reflections: reflections.map { |r| format_reflection(r) } }
end

#reflect(tick_results: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/self/reflection/runners/reflection.rb', line 13

def reflect(tick_results: {}, **)
  @metric_history ||= []
  @metric_history << tick_results
  @metric_history = @metric_history.last(Helpers::Constants::METRIC_WINDOW_SIZE)

  new_reflections = Helpers::Monitors.run_all(tick_results, @metric_history)
  new_reflections.each { |r| reflection_store.store(r) }

  update_category_scores(tick_results)

  if Helpers::LlmEnhancer.available? && new_reflections.any?
    health_scores = Helpers::Constants::CATEGORIES.to_h { |c| [c, reflection_store.category_score(c)] }
    llm_result = Helpers::LlmEnhancer.enhance_reflection(
      monitors_data: new_reflections,
      health_scores: health_scores
    )
    if llm_result
      new_reflections.each do |entry|
        enhanced = llm_result[:observations][entry[:category]]
        next unless enhanced

        entry[:observation] = enhanced
        entry[:source]      = :llm
      end
    end
  end

  log.debug "[reflection] generated #{new_reflections.size} reflections, health=#{reflection_store.cognitive_health}"

  {
    reflections_generated: new_reflections.size,
    cognitive_health:      reflection_store.cognitive_health,
    new_reflections:       new_reflections.map { |r| format_reflection(r) },
    total_reflections:     reflection_store.count
  }
end

#reflect_on_dream(dream_results: {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/self/reflection/runners/reflection.rb', line 50

def reflect_on_dream(dream_results: {}, **)
  source     = :mechanical
  reflection = nil

  if Helpers::LlmEnhancer.available?
    llm_result = Helpers::LlmEnhancer.reflect_on_dream(dream_results: dream_results)
    if llm_result&.fetch(:reflection, nil)
      reflection = llm_result[:reflection]
      source     = :llm
    end
  end

  reflection ||= build_mechanical_dream_reflection(dream_results)

  log.debug "[reflection] dream reflection generated source=#{source}"
  { reflection: reflection, source: source }
end

#reflection_statsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/legion/extensions/agentic/self/reflection/runners/reflection.rb', line 101

def reflection_stats(**)
  {
    total_generated:  reflection_store.total_generated,
    current_count:    reflection_store.count,
    cognitive_health: reflection_store.cognitive_health,
    severity_counts:  reflection_store.severity_counts,
    category_counts:  reflection_store.category_counts,
    unacted:          reflection_store.unacted.size
  }
end

#reflections_by_category(category:) ⇒ Object



85
86
87
88
89
# File 'lib/legion/extensions/agentic/self/reflection/runners/reflection.rb', line 85

def reflections_by_category(category:, **)
  cat = category.to_sym
  reflections = reflection_store.by_category(cat)
  { category: cat, reflections: reflections.map { |r| format_reflection(r) } }
end