chaos_to_the_rescue 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,502 @@
1
+ # ChaosToTheRescue
2
+
3
+ **Safe-by-default LLM-powered method generation and Rails error rescue suggestions**
4
+
5
+ ChaosToTheRescue is a Ruby gem that uses Large Language Models (LLMs) to:
6
+ 1. Generate missing methods on-the-fly via `method_missing`
7
+ 2. Suggest fixes for Rails exceptions in development
8
+
9
+ ## ⚠️ Safety First
10
+
11
+ This gem is **DISABLED BY DEFAULT** and includes multiple safety features:
12
+
13
+ - ✅ **Disabled by default** - Must be explicitly enabled
14
+ - ✅ **No auto-execution** - Generated code returned as strings only (unless explicitly enabled)
15
+ - ✅ **Allowlist-only** - Methods must match patterns or be explicitly allowed
16
+ - ✅ **Secret redaction** - ENV vars, API keys, tokens filtered before LLM prompts
17
+ - ✅ **No file writes** - Suggestions only logged, no automatic code edits
18
+ - ✅ **Development-only** - Intended for development/test environments only
19
+
20
+ **DO NOT USE IN PRODUCTION** - This gem is designed for development and debugging workflows only.
21
+
22
+ ## Installation
23
+
24
+ Add to your Gemfile:
25
+
26
+ ```ruby
27
+ gem "chaos_to_the_rescue"
28
+ ```
29
+
30
+ For LLM features, also add:
31
+
32
+ ```ruby
33
+ gem "ruby_llm", "~> 0.1" # Required for LLM integration
34
+ ```
35
+
36
+ Then run:
37
+
38
+ ```bash
39
+ bundle install
40
+ ```
41
+
42
+ For Rails applications, generate the initializer:
43
+
44
+ ```bash
45
+ rails generate chaos_to_the_rescue:install
46
+ ```
47
+
48
+ ### Configure API Keys
49
+
50
+ ChaosToTheRescue uses RubyLLM under the hood, which supports multiple LLM providers. You need to configure at least one API key:
51
+
52
+ **Option 1: Environment Variables (Recommended)**
53
+ ```bash
54
+ export OPENAI_API_KEY="sk-..."
55
+ export ANTHROPIC_API_KEY="sk-ant-..."
56
+ ```
57
+
58
+ **Option 2: Configuration Block**
59
+ ```ruby
60
+ ChaosToTheRescue.configure do |config|
61
+ config.openai_api_key = "sk-..."
62
+ # or
63
+ config.anthropic_api_key = "sk-ant-..."
64
+ end
65
+ ```
66
+
67
+ **Supported Models:**
68
+ - OpenAI: `gpt-5.2`, `gpt-5.2-pro`, `gpt-5`, `gpt-5-mini`, `gpt-5-nano`, `gpt-4.1`
69
+ - Anthropic: `claude-sonnet-4-5`, `claude-opus-4-5`, `claude-sonnet-4`, `claude-opus-4`
70
+ - Google: `gemini-2.0-flash-exp`, `gemini-1.5-pro`
71
+ - And many more via RubyLLM (xAI, DeepSeek, local models, etc.)
72
+
73
+ ## Features
74
+
75
+ ### 1. Method Generation with `ChaosRescue`
76
+
77
+ Generate missing methods on-the-fly using LLMs. When a method is called that doesn't exist, ChaosToTheRescue can generate and define it automatically.
78
+
79
+ **Supports both instance methods and class methods:**
80
+
81
+ ```ruby
82
+ require "chaos_to_the_rescue"
83
+
84
+ ChaosToTheRescue.configure do |config|
85
+ config.enabled = true
86
+ config.auto_define_methods = true
87
+ config.allowed_method_name_patterns = [/^calc_/]
88
+ end
89
+
90
+ class Calculator
91
+ include ChaosToTheRescue::ChaosRescue
92
+ chaos_rescue_enabled!
93
+ end
94
+
95
+ # Instance method generation
96
+ calc = Calculator.new
97
+ result = calc.calc_fibonacci(10) # Method generated via LLM
98
+
99
+ # Class method generation
100
+ result = Calculator.calc_sum(1, 2, 3) # Class method generated via LLM
101
+ ```
102
+
103
+ ### 2. Method Guidance and Verification
104
+
105
+ Provide guidance to the LLM for better method generation and verify method outputs:
106
+
107
+ #### Adding Guidance for Method Generation
108
+
109
+ ```ruby
110
+ class Calculator
111
+ include ChaosToTheRescue::ChaosRescue
112
+ chaos_rescue_enabled!
113
+
114
+ # Provide guidance BEFORE the method is generated
115
+ chaos_guidance :pow, <<~GUIDANCE
116
+ This method should correctly handle negative bases:
117
+ - Negative base with even exponent returns positive: (-5)^2 = 25
118
+ - Negative base with odd exponent returns negative: (-5)^3 = -125
119
+ Common mistake: Using base.abs which always returns positive.
120
+ GUIDANCE
121
+ end
122
+
123
+ # Global guidance (applies to all classes)
124
+ ChaosToTheRescue.configure do |config|
125
+ config.method_guidance[:divide] = "Handle division by zero gracefully"
126
+ end
127
+ ```
128
+
129
+ #### Adding Guidance to Existing Methods
130
+
131
+ ```ruby
132
+ class Calculator
133
+ def pow(base, exponent)
134
+ base ** exponent # Method already defined
135
+ end
136
+
137
+ # Add guidance AFTER definition for verification purposes
138
+ additional_chaos_guidance :pow, "Should handle negative bases correctly"
139
+ end
140
+ ```
141
+
142
+ #### Verifying Method Outputs
143
+
144
+ **Verify with class method:**
145
+ ```ruby
146
+ result = Calculator.verify_chaos(:pow, -5, 2)
147
+
148
+ if result.failed?
149
+ puts "Issues found:"
150
+ puts result.diagnosis
151
+ puts result.suggestion
152
+ end
153
+ ```
154
+
155
+ **Auto-fix incorrect implementations:**
156
+ ```ruby
157
+ # Automatically applies fix if verification fails
158
+ Calculator.verify_chaos(:pow, -5, 2, auto_apply: true)
159
+ ```
160
+
161
+ **Verify using block syntax:**
162
+ ```ruby
163
+ # Simple verification
164
+ result = ChaosToTheRescue.verify do
165
+ Calculator.new.pow(-5, 2)
166
+ end
167
+
168
+ # With auto-fix
169
+ result = ChaosToTheRescue.verify(auto_apply: true) do
170
+ Calculator.new.pow(-5, 2)
171
+ end
172
+
173
+ if result.failed?
174
+ puts "Method was incorrect:"
175
+ puts result.diagnosis
176
+ puts "Actual result: #{result.actual_result}"
177
+ end
178
+ ```
179
+
180
+ See `examples/guidance_and_verification.rb` for comprehensive examples.
181
+
182
+ ### 3. Rails Exception Rescue Suggestions
183
+
184
+ Get LLM-powered suggestions for fixing Rails exceptions:
185
+
186
+ ```ruby
187
+ class ApplicationController < ActionController::Base
188
+ include ChaosToTheRescue::RescueFrom
189
+
190
+ rescue_from StandardError do |exception|
191
+ suggestion = chaos_suggest_fix(
192
+ exception,
193
+ guidance: "This is a payment processing error"
194
+ )
195
+
196
+ Rails.logger.info("Fix suggestion: #{suggestion[:title]}")
197
+ Rails.logger.info("Diagnosis: #{suggestion[:diagnosis]}")
198
+ Rails.logger.info("Proposed changes: #{suggestion[:proposed_changes]}")
199
+ Rails.logger.info("Files to edit: #{suggestion[:files_to_edit].join(', ')}")
200
+
201
+ render "errors/500", status: :internal_server_error
202
+ end
203
+ end
204
+ ```
205
+
206
+ ## Configuration
207
+
208
+ All configuration is done via the `ChaosToTheRescue.configure` block:
209
+
210
+ ```ruby
211
+ ChaosToTheRescue.configure do |config|
212
+ # === CORE SETTINGS ===
213
+
214
+ # Enable/disable globally (default: false)
215
+ config.enabled = Rails.env.development? || Rails.env.test?
216
+
217
+ # Auto-define generated methods (default: false)
218
+ config.auto_define_methods = false
219
+
220
+ # === METHOD GENERATION ALLOWLIST ===
221
+
222
+ # Regex patterns for allowed method names
223
+ config.allowed_method_name_patterns = [/^calc_/, /^compute_/]
224
+
225
+ # Explicit allowlist of method names
226
+ config.allowlist = [:my_dynamic_method, "another_method"]
227
+
228
+ # Or allow all method names (use with caution!)
229
+ config.allow_everything!
230
+
231
+ # === LLM SETTINGS ===
232
+
233
+ # LLM model to use (default: "gpt-5-mini")
234
+ config.model = "gpt-5-mini"
235
+
236
+ # Maximum characters in prompts (default: 8000)
237
+ config.max_prompt_chars = 8000
238
+
239
+ # === LOGGING SETTINGS ===
240
+
241
+ # Log level (default: :info)
242
+ config.log_level = :info
243
+
244
+ # Log suggestions to file (default: true)
245
+ config.log_suggestions = true
246
+
247
+ # Suggestion log path (default: "tmp/chaos_to_the_rescue_suggestions.log")
248
+ config.suggestions_log_path = "tmp/chaos_to_the_rescue_suggestions.log"
249
+
250
+ # === SECURITY SETTINGS ===
251
+
252
+ # Add custom redaction patterns (already includes common secrets)
253
+ config.redact_patterns << /CUSTOM_SECRET[=:]\s*['"]?([^'"\s]+)['"]?/i
254
+ end
255
+ ```
256
+
257
+ ### Default Redaction Patterns
258
+
259
+ The gem automatically redacts:
260
+ - Environment variable references (`ENV["KEY"]`, `ENV.fetch`)
261
+ - AWS credentials (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AKIA...`)
262
+ - OpenAI tokens (`sk-...`, `OPENAI_API_KEY`)
263
+ - GitHub tokens (`ghp_...`, `gho_...`, `ghu_...`, `ghs_...`, `ghr_...`, `GITHUB_TOKEN`)
264
+ - Rails credentials (`Rails.application.credentials.*`)
265
+ - Generic patterns (`API_KEY`, `SECRET`, `TOKEN`, `PASSWORD`)
266
+ - Private keys (`-----BEGIN PRIVATE KEY-----`)
267
+
268
+ ## Usage Examples
269
+
270
+ ### Example 1: Calculator with Dynamic Methods
271
+
272
+ ```ruby
273
+ ChaosToTheRescue.configure do |config|
274
+ config.enabled = true
275
+ config.auto_define_methods = true
276
+ config.allowed_method_name_patterns = [/^calc_/]
277
+ end
278
+
279
+ class Calculator
280
+ include ChaosToTheRescue::ChaosRescue
281
+ chaos_rescue_enabled!
282
+ end
283
+
284
+ # Instance methods
285
+ calc = Calculator.new
286
+ puts calc.calc_sum(1, 2, 3, 4, 5) # LLM generates sum method
287
+ puts calc.calc_average(10, 20, 30) # LLM generates average method
288
+ puts calc.calc_fibonacci(10) # LLM generates fibonacci method
289
+
290
+ # Class methods
291
+ puts Calculator.calc_factorial(5) # LLM generates class method
292
+ puts Calculator.calc_gcd(48, 18) # LLM generates class method
293
+ ```
294
+
295
+ ### Example 2: Per-Class Enable/Disable
296
+
297
+ ```ruby
298
+ class SafeClass
299
+ include ChaosToTheRescue::ChaosRescue
300
+ # Not enabled - will not generate methods
301
+ end
302
+
303
+ class ExperimentalClass
304
+ include ChaosToTheRescue::ChaosRescue
305
+ chaos_rescue_enabled! # Explicitly enabled for this class
306
+ end
307
+ ```
308
+
309
+ ### Example 3: Rails Controller Error Handling
310
+
311
+ ```ruby
312
+ class OrdersController < ApplicationController
313
+ include ChaosToTheRescue::RescueFrom
314
+
315
+ rescue_from StandardError do |exception|
316
+ if ChaosToTheRescue.configuration.enabled
317
+ suggestion = chaos_suggest_fix(
318
+ exception,
319
+ guidance: "User was trying to complete checkout"
320
+ )
321
+
322
+ # Log the suggestion
323
+ Rails.logger.error("Exception: #{exception.class.name}: #{exception.message}")
324
+ Rails.logger.info("Suggestion: #{suggestion[:title]}")
325
+ Rails.logger.info("Diagnosis: #{suggestion[:diagnosis]}")
326
+
327
+ # Check suggestion log file at tmp/chaos_to_the_rescue_suggestions.log
328
+ end
329
+
330
+ render "errors/500", status: :internal_server_error
331
+ end
332
+ end
333
+ ```
334
+
335
+ ### Example 4: Allowlist Specific Methods
336
+
337
+ ```ruby
338
+ ChaosToTheRescue.configure do |config|
339
+ config.enabled = true
340
+ config.allowlist = [:format_currency, :parse_date, :generate_slug]
341
+ end
342
+
343
+ class Helper
344
+ include ChaosToTheRescue::ChaosRescue
345
+ chaos_rescue_enabled!
346
+ end
347
+
348
+ helper = Helper.new
349
+ helper.format_currency(1234.56) # Allowed - will generate
350
+ helper.dangerous_method # Not allowed - raises NoMethodError
351
+ ```
352
+
353
+ ### Example 5: Allow All Methods (Quick Testing)
354
+
355
+ ```ruby
356
+ # WARNING: Use with caution - allows any method to be generated
357
+ ChaosToTheRescue.configure do |config|
358
+ config.enabled = true
359
+ config.allow_everything! # Convenience method to allow all method names
360
+ end
361
+
362
+ class Calculator
363
+ include ChaosToTheRescue::ChaosRescue
364
+ chaos_rescue_enabled!
365
+ end
366
+
367
+ calc = Calculator.new
368
+ calc.divide(10, 2) # Will work - all methods allowed
369
+ calc.anything_you_want(1, 2) # Will work - all methods allowed
370
+ ```
371
+
372
+ ## How It Works
373
+
374
+ ### Method Generation Flow
375
+
376
+ 1. You call a missing method (instance or class method) on a class that includes `ChaosRescue`
377
+ 2. ChaosToTheRescue checks if the gem is enabled (globally and per-class)
378
+ 3. Checks if the method name matches allowlist or patterns
379
+ 4. Builds context (class name, method name, arguments, method type)
380
+ 5. Redacts any sensitive information from the context
381
+ 6. Sends prompt to LLM to generate method code
382
+ 7. Optionally defines the method on the object or class (if `auto_define_methods` enabled)
383
+ 8. Returns the result
384
+
385
+ ### Exception Suggestion Flow
386
+
387
+ 1. An exception occurs in a Rails controller
388
+ 2. You call `chaos_suggest_fix` in your `rescue_from` block
389
+ 3. ChaosToTheRescue builds exception context (class, message, backtrace)
390
+ 4. Sanitizes request data and redacts secrets
391
+ 5. Sends prompt to LLM to analyze the error
392
+ 6. Returns structured suggestion with diagnosis and proposed changes
393
+ 7. Logs suggestion to file if enabled
394
+
395
+ ## Security Considerations
396
+
397
+ ### What Gets Redacted
398
+
399
+ Before sending any data to an LLM, ChaosToTheRescue redacts:
400
+ - All environment variable references
401
+ - API keys, secrets, tokens, passwords
402
+ - AWS, OpenAI, GitHub credentials
403
+ - Rails credentials
404
+ - Private keys
405
+
406
+ ### What Doesn't Get Sent
407
+
408
+ - File contents (only file paths from backtraces)
409
+ - User session data (unless in exception context)
410
+ - Database credentials
411
+
412
+ ### Recommendations
413
+
414
+ 1. **Never enable in production** - Only use in development/test
415
+ 2. **Review prompts** - Set `log_level: :debug` to see what's sent to LLM
416
+ 3. **Use allowlists** - Don't allow all methods, use specific patterns
417
+ 4. **Don't auto-define** - Keep `auto_define_methods: false` and review generated code
418
+ 5. **Custom redaction** - Add your own patterns for domain-specific secrets
419
+
420
+ ## Development
421
+
422
+ After checking out the repo:
423
+
424
+ ```bash
425
+ bin/setup # Install dependencies
426
+ bundle exec rspec # Run tests
427
+ bin/console # Interactive prompt
428
+ bundle exec standardrb # Lint code
429
+ ```
430
+
431
+ ## Testing
432
+
433
+ Run the full test suite:
434
+
435
+ ```bash
436
+ bundle exec rspec
437
+ ```
438
+
439
+ Run specific test files:
440
+
441
+ ```bash
442
+ bundle exec rspec spec/configuration_spec.rb
443
+ bundle exec rspec spec/chaos_rescue_spec.rb
444
+ ```
445
+
446
+ ## Troubleshooting
447
+
448
+ ### "RubyLLM is not available"
449
+
450
+ You need to add `ruby_llm` to your Gemfile:
451
+
452
+ ```ruby
453
+ gem "ruby_llm", "~> 0.1"
454
+ ```
455
+
456
+ Then run `bundle install`.
457
+
458
+ ### Methods Not Being Generated
459
+
460
+ Check that:
461
+ 1. `config.enabled = true` is set
462
+ 2. Class has `chaos_rescue_enabled!` called
463
+ 3. Method name matches patterns or is in allowlist
464
+ 4. RubyLLM is installed and configured
465
+
466
+ ### No Suggestions Logged
467
+
468
+ Check that:
469
+ 1. `config.log_suggestions = true` is set
470
+ 2. The `tmp/` directory exists and is writable
471
+ 3. ChaosToTheRescue is enabled
472
+
473
+ ## Roadmap
474
+
475
+ Potential future features:
476
+ - Multiple LLM provider support (Anthropic, Cohere, local models)
477
+ - Caching of generated methods
478
+ - Web UI for reviewing suggestions
479
+ - Type signature validation
480
+ - Integration with CI/CD for automated error analysis
481
+
482
+ ## Contributing
483
+
484
+ Bug reports and pull requests are welcome on GitHub at https://github.com/codenamev/chaos_to_the_rescue.
485
+
486
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/codenamev/chaos_to_the_rescue/blob/main/CODE_OF_CONDUCT.md).
487
+
488
+ ## License
489
+
490
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
491
+
492
+ ## Code of Conduct
493
+
494
+ Everyone interacting in the ChaosToTheRescue project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/codenamev/chaos_to_the_rescue/blob/main/CODE_OF_CONDUCT.md).
495
+
496
+ ## Credits
497
+
498
+ Created by [Valentino Stoll](https://github.com/codenamev)
499
+
500
+ ## Disclaimer
501
+
502
+ This gem is experimental and should only be used in development environments. The authors are not responsible for any issues arising from using generated code in production. Always review generated code before using it in any capacity.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]