Class: RailsConsoleAi::Executor
- Inherits:
-
Object
- Object
- RailsConsoleAi::Executor
- Defined in:
- lib/rails_console_ai/executor.rb
Constant Summary collapse
- CODE_REGEX =
/```ruby\s*\n(.*?)```/m- ANY_CODE_FENCE_REGEX =
Matches any fenced code block (“‘anything … “`)
/```\w*\s*\n.*?```/m
Instance Attribute Summary collapse
-
#binding_context ⇒ Object
readonly
Returns the value of attribute binding_context.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#last_safety_error ⇒ Object
readonly
Returns the value of attribute last_safety_error.
-
#last_safety_exception ⇒ Object
readonly
Returns the value of attribute last_safety_exception.
-
#on_prompt ⇒ Object
Returns the value of attribute on_prompt.
Instance Method Summary collapse
- #activate_skill_bypasses(methods) ⇒ Object
- #confirm_and_execute(code) ⇒ Object
- #display_code_block(code) ⇒ Object
- #display_response(response) ⇒ Object
- #execute(code, display: true) ⇒ Object
- #execute_unsafe(code) ⇒ Object
- #expand_output(id) ⇒ Object
- #extract_code(response) ⇒ Object
-
#initialize(binding_context, channel: nil) ⇒ Executor
constructor
A new instance of Executor.
- #last_answer ⇒ Object
- #last_cancelled? ⇒ Boolean
- #last_output ⇒ Object
- #offer_danger_retry(code) ⇒ Object
- #recall_output(id) ⇒ Object
- #store_output(content) ⇒ Object
Constructor Details
#initialize(binding_context, channel: nil) ⇒ Executor
Returns a new instance of Executor.
50 51 52 53 54 55 56 57 58 |
# File 'lib/rails_console_ai/executor.rb', line 50 def initialize(binding_context, channel: nil) @binding_context = binding_context @channel = channel @omitted_outputs = {} @omitted_counter = 0 @output_store = {} @output_counter = 0 @active_skill_bypass_methods = Set.new end |
Instance Attribute Details
#binding_context ⇒ Object (readonly)
Returns the value of attribute binding_context.
47 48 49 |
# File 'lib/rails_console_ai/executor.rb', line 47 def binding_context @binding_context end |
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
47 48 49 |
# File 'lib/rails_console_ai/executor.rb', line 47 def last_error @last_error end |
#last_safety_error ⇒ Object (readonly)
Returns the value of attribute last_safety_error.
47 48 49 |
# File 'lib/rails_console_ai/executor.rb', line 47 def last_safety_error @last_safety_error end |
#last_safety_exception ⇒ Object (readonly)
Returns the value of attribute last_safety_exception.
47 48 49 |
# File 'lib/rails_console_ai/executor.rb', line 47 def last_safety_exception @last_safety_exception end |
#on_prompt ⇒ Object
Returns the value of attribute on_prompt.
48 49 50 |
# File 'lib/rails_console_ai/executor.rb', line 48 def on_prompt @on_prompt end |
Instance Method Details
#activate_skill_bypasses(methods) ⇒ Object
327 328 329 330 331 332 333 |
# File 'lib/rails_console_ai/executor.rb', line 327 def activate_skill_bypasses(methods) guards = RailsConsoleAi.configuration.safety_guards Array(methods).each do |spec| @active_skill_bypass_methods << spec guards.install_bypass_method!(spec) end end |
#confirm_and_execute(code) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/rails_console_ai/executor.rb', line 209 def confirm_and_execute(code) return nil if code.nil? || code.strip.empty? @last_cancelled = false @last_answer = nil prompt = execute_prompt if @channel answer = @channel.confirm(prompt) else $stdout.print colorize(prompt, :yellow) @on_prompt&.call answer = $stdin.gets.to_s.strip.downcase echo_stdin(answer) end @last_answer = answer loop do case answer when 'a', 'auto' RailsConsoleAi.configuration.auto_execute = true if @channel @channel.display_status("Auto-execute: ON") else $stdout.puts colorize("Auto-execute: ON", :cyan) end result = execute(code) if @last_safety_error return nil unless danger_allowed? return offer_danger_retry(code) end return result when 'y', 'yes' result = execute(code) if @last_safety_error return nil unless danger_allowed? return offer_danger_retry(code) end return result when 'd', 'danger' unless danger_allowed? display_error("Safety guards cannot be disabled in this channel.") return nil end if @channel @channel.display_error("Executing with safety guards disabled.") else $stdout.puts colorize("Executing with safety guards disabled.", :red) end return execute_unsafe(code) when 'n', 'no', '' $stdout.puts colorize("Cancelled.", :yellow) @last_cancelled = true return nil else if @channel answer = @channel.confirm(prompt) else $stdout.print colorize(prompt, :yellow) @on_prompt&.call answer = $stdin.gets.to_s.strip.downcase echo_stdin(answer) end @last_answer = answer end end end |
#display_code_block(code) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rails_console_ai/executor.rb', line 84 def display_code_block(code) if @channel @channel.display_code(code) else $stdout.puts $stdout.puts colorize("# Generated code:", :yellow) $stdout.puts highlight_code(code) $stdout.puts end end |
#display_response(response) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rails_console_ai/executor.rb', line 68 def display_response(response) # Code execution now happens via the execute_code tool, not code-fence extraction. # Just display the full response text as-is. text = response.to_s.strip return '' if text.empty? $stdout.puts if @channel @channel.display(text) else $stdout.puts colorize(text, :cyan) end '' # No code to extract — the LLM uses execute_code tool instead end |
#execute(code, display: true) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/rails_console_ai/executor.rb', line 95 def execute(code, display: true) return nil if code.nil? || code.strip.empty? @last_error = nil @last_safety_error = false @last_safety_exception = nil captured_output = StringIO.new old_stdout = $stdout # Three capture strategies: # 1. Slack mode (PrefixedIO active): thread-local capture to avoid cross-thread pollution # 2. Console mode (channel present): capture-only, channel.display_result_output shows it after # 3. No channel (tests/one-shot): TeeIO so output appears live AND is captured use_thread_local = defined?(RailsConsoleAi::PrefixedIO) && $stdout.is_a?(RailsConsoleAi::PrefixedIO) if use_thread_local Thread.current[:capture_io] = captured_output elsif @channel $stdout = captured_output else $stdout = TeeIO.new(old_stdout, captured_output) end RailsConsoleAi::SafetyError.clear! result = with_safety_guards do binding_context.eval(code, "(rails_console_ai)", 1) end restore_stdout(use_thread_local, old_stdout) # Check if a SafetyError was raised but swallowed by a rescue inside the eval'd code if (swallowed = RailsConsoleAi::SafetyError.last_raised) RailsConsoleAi::SafetyError.clear! @last_error = "SafetyError: #{swallowed.}" @last_safety_error = true @last_safety_exception = swallowed display_error("Blocked: #{swallowed.}") @last_output = captured_output&.string return nil end # Send captured puts output through channel before the return value if display && @channel && !captured_output.string.empty? @channel.display_result_output(captured_output.string) end display_result(result) if display @last_output = captured_output.string result rescue Interrupt restore_stdout(use_thread_local, old_stdout) @last_output = captured_output&.string raise rescue RailsConsoleAi::SafetyError => e restore_stdout(use_thread_local, old_stdout) RailsConsoleAi::SafetyError.clear! @last_error = "SafetyError: #{e.}" @last_safety_error = true @last_safety_exception = e display_error("Blocked: #{e.}") @last_output = captured_output&.string nil rescue SyntaxError => e restore_stdout(use_thread_local, old_stdout) @last_error = "SyntaxError: #{e.}" log_execution_error(@last_error) @last_output = nil nil rescue => e restore_stdout(use_thread_local, old_stdout) # Check if a SafetyError is wrapped (e.g. ActiveRecord::StatementInvalid wrapping our error) if safety_error?(e) safety_exc = extract_safety_exception(e) safety_msg = safety_exc ? safety_exc. : e. @last_error = "SafetyError: #{safety_msg}" @last_safety_error = true @last_safety_exception = safety_exc display_error("Blocked: #{safety_msg}") @last_output = captured_output&.string return nil end @last_error = "#{e.class}: #{e.}" backtrace = e.backtrace.first(3).map { |line| " #{line}" }.join("\n") log_execution_error("Error: #{@last_error}\n#{backtrace}") @last_output = captured_output&.string nil end |
#execute_unsafe(code) ⇒ Object
335 336 337 338 339 340 341 |
# File 'lib/rails_console_ai/executor.rb', line 335 def execute_unsafe(code) guards = RailsConsoleAi.configuration.safety_guards guards.disable! execute(code) ensure guards.enable! end |
#expand_output(id) ⇒ Object
187 188 189 |
# File 'lib/rails_console_ai/executor.rb', line 187 def (id) @omitted_outputs[id] end |
#extract_code(response) ⇒ Object
60 61 62 63 |
# File 'lib/rails_console_ai/executor.rb', line 60 def extract_code(response) match = response.match(CODE_REGEX) match ? match[1].strip : '' end |
#last_answer ⇒ Object
201 202 203 |
# File 'lib/rails_console_ai/executor.rb', line 201 def last_answer @last_answer end |
#last_cancelled? ⇒ Boolean
205 206 207 |
# File 'lib/rails_console_ai/executor.rb', line 205 def last_cancelled? @last_cancelled end |
#last_output ⇒ Object
183 184 185 |
# File 'lib/rails_console_ai/executor.rb', line 183 def last_output @last_output end |
#offer_danger_retry(code) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/rails_console_ai/executor.rb', line 277 def offer_danger_retry(code) return nil unless danger_allowed? exc = @last_safety_exception blocked_key = exc&.blocked_key guard = exc&.guard if blocked_key && guard allow_desc = allow_description(guard, blocked_key) $stdout.puts colorize(" [d] re-run with all safe mode disabled", :yellow) $stdout.puts colorize(" [a] allow #{allow_desc} for this session", :yellow) $stdout.puts colorize(" [N] cancel", :yellow) prompt_text = "Choice: " else prompt_text = "Re-run with safe mode disabled? [y/N] " end if @channel answer = @channel.confirm(prompt_text) else $stdout.print colorize(prompt_text, :yellow) answer = $stdin.gets.to_s.strip.downcase echo_stdin(answer) end case answer when 'a', 'allow' if blocked_key && guard RailsConsoleAi.configuration.safety_guards.allow(guard, blocked_key) allow_desc = allow_description(guard, blocked_key) $stdout.puts colorize("Allowed #{allow_desc} for this session.", :green) return execute(code) else if @channel answer = @channel.confirm("Nothing to allow — re-run with safe mode disabled instead? [y/N] ") else $stdout.puts colorize("Nothing to allow — re-run with safe mode disabled instead? [y/N]", :yellow) answer = $stdin.gets.to_s.strip.downcase echo_stdin(answer) end end when 'd', 'danger', 'y', 'yes' $stdout.puts colorize("Executing with safety guards disabled.", :red) return execute_unsafe(code) end $stdout.puts colorize("Cancelled.", :yellow) nil end |
#recall_output(id) ⇒ Object
197 198 199 |
# File 'lib/rails_console_ai/executor.rb', line 197 def recall_output(id) @output_store[id] end |
#store_output(content) ⇒ Object
191 192 193 194 195 |
# File 'lib/rails_console_ai/executor.rb', line 191 def store_output(content) @output_counter += 1 @output_store[@output_counter] = content @output_counter end |