Module: Legion::MCP::SelfGenerate
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/mcp/self_generate.rb
Constant Summary collapse
- MAX_GAPS_PER_CYCLE =
5- COOLDOWN_SECONDS =
300
Class Method Summary collapse
- .cooldown_remaining ⇒ Object
- .cooldown_seconds ⇒ Object
- .cycle_count ⇒ Object
- .cycle_history(limit = 10) ⇒ Object
- .enabled? ⇒ Boolean
- .in_cooldown? ⇒ Boolean
- .last_cycle_at ⇒ Object
-
.max_gaps_per_cycle ⇒ Object
private helpers.
- .mutex ⇒ Object
- .publish_gap(gap) ⇒ Object
- .record_cycle(published_count) ⇒ Object
- .reset! ⇒ Object
- .run_cycle ⇒ Object
- .status ⇒ Object
- .total_published ⇒ Object
Class Method Details
.cooldown_remaining ⇒ Object
114 115 116 117 118 119 |
# File 'lib/legion/mcp/self_generate.rb', line 114 def cooldown_remaining return 0 unless last_cycle_at remaining = cooldown_seconds - (Time.now - last_cycle_at) [remaining, 0].max.round(1) end |
.cooldown_seconds ⇒ Object
132 133 134 135 |
# File 'lib/legion/mcp/self_generate.rb', line 132 def cooldown_seconds val = Legion::Settings.dig(:codegen, :self_generate, :cooldown_seconds) if defined?(Legion::Settings) val || COOLDOWN_SECONDS end |
.cycle_count ⇒ Object
152 153 154 |
# File 'lib/legion/mcp/self_generate.rb', line 152 def cycle_count mutex.synchronize { @cycle_count || 0 } end |
.cycle_history(limit = 10) ⇒ Object
104 105 106 |
# File 'lib/legion/mcp/self_generate.rb', line 104 def cycle_history(limit = 10) mutex.synchronize { (@cycle_history || []).last(limit) } end |
.enabled? ⇒ Boolean
16 17 18 19 20 |
# File 'lib/legion/mcp/self_generate.rb', line 16 def enabled? return false unless defined?(Legion::Settings) Legion::Settings.dig(:codegen, :self_generate, :enabled) == true end |
.in_cooldown? ⇒ Boolean
108 109 110 111 112 |
# File 'lib/legion/mcp/self_generate.rb', line 108 def in_cooldown? return false unless last_cycle_at Time.now - last_cycle_at < cooldown_seconds end |
.last_cycle_at ⇒ Object
148 149 150 |
# File 'lib/legion/mcp/self_generate.rb', line 148 def last_cycle_at mutex.synchronize { @last_cycle_at } end |
.max_gaps_per_cycle ⇒ Object
private helpers
127 128 129 130 |
# File 'lib/legion/mcp/self_generate.rb', line 127 def max_gaps_per_cycle val = Legion::Settings.dig(:codegen, :self_generate, :max_gaps_per_cycle) if defined?(Legion::Settings) val || MAX_GAPS_PER_CYCLE end |
.mutex ⇒ Object
156 157 158 |
# File 'lib/legion/mcp/self_generate.rb', line 156 def mutex @mutex ||= Mutex.new end |
.publish_gap(gap) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/legion/mcp/self_generate.rb', line 58 def publish_gap(gap) return false unless defined?(Legion::Transport::Messages::Dynamic) Legion::Transport::Messages::Dynamic.new( function: 'codegen.gap.detected', data: { gap_id: gap[:id], gap_type: gap[:type], intent: gap[:intent] || gap[:intent_text], occurrence_count: gap[:occurrences] || gap[:observation_count] || gap[:failure_count] || 1, priority: gap[:priority], metadata: gap[:metadata] || {}, detected_at: Time.now.iso8601 } ).publish true rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.self_generate.publish_gap') log.warn("SelfGenerate#publish_gap failed: #{e.}") false end |
.record_cycle(published_count) ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/legion/mcp/self_generate.rb', line 137 def record_cycle(published_count) mutex.synchronize do @last_cycle_at = Time.now @cycle_count = (@cycle_count || 0) + 1 @total_published = (@total_published || 0) + published_count @cycle_history ||= [] @cycle_history << { at: Time.now, published: published_count } @cycle_history.shift if @cycle_history.size > 50 end end |
.reset! ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/legion/mcp/self_generate.rb', line 95 def reset! mutex.synchronize do @last_cycle_at = nil @cycle_count = 0 @total_published = 0 @cycle_history = [] end end |
.run_cycle ⇒ Object
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 49 50 51 52 53 54 55 56 |
# File 'lib/legion/mcp/self_generate.rb', line 22 def run_cycle log.info('Starting legion.mcp.self_generate.run_cycle') return { success: false, reason: :disabled } unless enabled? return { success: false, reason: :cooldown } if in_cooldown? gaps = GapDetector.detect_gaps return { success: true, gaps_found: 0, published: 0 } if gaps.empty? top_gaps = gaps.sort_by { |g| -g[:priority] }.first(max_gaps_per_cycle) published_count = 0 top_gaps.each do |gap| published_count += 1 if publish_gap(gap) end if published_count.zero? reason = defined?(Legion::Transport::Messages::Dynamic) ? :publish_failed : :transport_unavailable return { success: false, reason: reason, gaps_found: gaps.size, processed: top_gaps.size, published: 0 } end record_cycle(published_count) { success: true, gaps_found: gaps.size, processed: top_gaps.size, published: published_count } end |
.status ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/legion/mcp/self_generate.rb', line 80 def status log.info('Starting legion.mcp.self_generate.status') { last_cycle_at: last_cycle_at, total_cycles: cycle_count, total_published: total_published, cooldown_remaining: cooldown_remaining, pending_gaps: GapDetector.detect_gaps.size, enabled: enabled? } rescue StandardError => e handle_exception(e, level: :error, operation: 'legion.mcp.self_generate.status') { error: e. } end |
.total_published ⇒ Object
121 122 123 |
# File 'lib/legion/mcp/self_generate.rb', line 121 def total_published mutex.synchronize { @total_published || 0 } end |