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/FEATURES.md ADDED
@@ -0,0 +1,306 @@
1
+ # Method Guidance and Verification Features
2
+
3
+ ## Overview
4
+
5
+ This document describes the method guidance and verification features added to ChaosToTheRescue. These features help ensure that LLM-generated methods handle edge cases correctly and provide a way to verify and automatically fix incorrect implementations.
6
+
7
+ ## Three Main Features
8
+
9
+ ### 1. chaos_guidance - Provide Guidance Before Generation
10
+
11
+ Add guidance when defining a class to help the LLM generate correct implementations:
12
+
13
+ ```ruby
14
+ class Calculator
15
+ include ChaosToTheRescue::ChaosRescue
16
+ chaos_rescue_enabled!
17
+
18
+ chaos_guidance :pow, <<~GUIDANCE
19
+ Handle negative bases correctly:
20
+ - (-5)^2 = 25 (negative base, even exponent = positive)
21
+ - (-5)^3 = -125 (negative base, odd exponent = negative)
22
+ Common mistake: Using base.abs which always returns positive
23
+ GUIDANCE
24
+ end
25
+ ```
26
+
27
+ When `pow` is called and doesn't exist, the LLM will see this guidance and generate a correct implementation.
28
+
29
+ ### 2. additional_chaos_guidance - Add Guidance to Existing Methods
30
+
31
+ Add guidance to methods that are already defined:
32
+
33
+ ```ruby
34
+ class Calculator
35
+ def pow(base, exponent)
36
+ base ** exponent # Already defined
37
+ end
38
+
39
+ # Add guidance for verification purposes
40
+ additional_chaos_guidance :pow, "Should handle negative bases correctly"
41
+ end
42
+ ```
43
+
44
+ This is an alias for `chaos_guidance` and is useful for documenting expected behavior of existing methods.
45
+
46
+ ### 3. Verification - Verify and Auto-Fix Method Outputs
47
+
48
+ Verify that method outputs are correct:
49
+
50
+ #### Class Method Verification
51
+
52
+ ```ruby
53
+ # Simple verification
54
+ result = Calculator.verify_chaos(:pow, -5, 2)
55
+
56
+ if result.failed?
57
+ puts result.diagnosis
58
+ puts result.suggestion
59
+ puts result.fixed_code
60
+ end
61
+
62
+ # With auto-fix
63
+ Calculator.verify_chaos(:pow, -5, 2, auto_apply: true)
64
+ ```
65
+
66
+ #### Block-based Verification
67
+
68
+ ```ruby
69
+ # Simple verification
70
+ result = ChaosToTheRescue.verify do
71
+ Calculator.new.pow(-5, 2)
72
+ end
73
+
74
+ # With auto-fix
75
+ result = ChaosToTheRescue.verify(auto_apply: true) do
76
+ Calculator.new.pow(-5, 2)
77
+ end
78
+ ```
79
+
80
+ ## How It Works
81
+
82
+ ### Guidance Flow
83
+
84
+ 1. **Class-level guidance** is stored in a class variable when you call `chaos_guidance`
85
+ 2. **Global guidance** can be set in configuration: `config.method_guidance[:method_name] = "guidance"`
86
+ 3. When generating a method, the LLM receives the guidance in its prompt
87
+ 4. The LLM uses the guidance to avoid common mistakes and handle edge cases
88
+
89
+ ### Verification Flow
90
+
91
+ 1. Call the method with specific arguments
92
+ 2. Capture the actual result
93
+ 3. Send method name, arguments, result, and guidance to the LLM
94
+ 4. LLM analyzes whether the result is correct
95
+ 5. If incorrect, LLM provides:
96
+ - Diagnosis of what went wrong
97
+ - Suggestion for correct result
98
+ - Fixed code (if method source is available)
99
+ 6. If `auto_apply: true`, the fixed code is evaluated and replaces the method
100
+
101
+ ## Use Cases
102
+
103
+ ### 1. Handling Edge Cases in Generated Methods
104
+
105
+ **Problem:** `Calculator.new.pow(-5, 2)` returns 25 (incorrect) because the implementation used `base.abs**exponent`
106
+
107
+ **Solution:**
108
+ ```ruby
109
+ class Calculator
110
+ include ChaosToTheRescue::ChaosRescue
111
+ chaos_rescue_enabled!
112
+
113
+ chaos_guidance :pow, "Handle negative bases: (-5)^2 = 25, (-5)^3 = -125"
114
+ end
115
+
116
+ # Now when pow is generated, it will handle negatives correctly
117
+ Calculator.new.pow(-5, 2) # Returns 25 (correct!)
118
+ Calculator.new.pow(-5, 3) # Returns -125 (correct!)
119
+ ```
120
+
121
+ ### 2. Verifying Existing Implementations
122
+
123
+ **Problem:** You have a method but aren't sure if it handles all cases
124
+
125
+ **Solution:**
126
+ ```ruby
127
+ class Calculator
128
+ def pow(base, exponent)
129
+ base.abs ** exponent # Bug!
130
+ end
131
+
132
+ additional_chaos_guidance :pow, "Should handle negative bases correctly"
133
+ end
134
+
135
+ # Verify the implementation
136
+ result = Calculator.verify_chaos(:pow, -5, 2)
137
+ puts result.diagnosis if result.failed?
138
+ # Output: "The implementation uses .abs which always returns positive results..."
139
+ ```
140
+
141
+ ### 3. Auto-fixing Incorrect Methods
142
+
143
+ **Problem:** A method has a bug and you want it automatically fixed
144
+
145
+ **Solution:**
146
+ ```ruby
147
+ # This will verify and auto-apply a fix if needed
148
+ Calculator.verify_chaos(:pow, -5, 2, auto_apply: true)
149
+
150
+ # Method is now fixed!
151
+ Calculator.new.pow(-5, 3) # Returns -125 (correct!)
152
+ ```
153
+
154
+ ## API Reference
155
+
156
+ ### Class Methods
157
+
158
+ #### `chaos_guidance(method_name, guidance)`
159
+
160
+ Add guidance for a method to help LLM generation.
161
+
162
+ **Parameters:**
163
+ - `method_name` (Symbol): The method name
164
+ - `guidance` (String): Guidance text for the LLM
165
+
166
+ **Example:**
167
+ ```ruby
168
+ chaos_guidance :divide, "Handle division by zero by returning Float::INFINITY"
169
+ ```
170
+
171
+ #### `additional_chaos_guidance(method_name, guidance)`
172
+
173
+ Alias for `chaos_guidance`. Use for existing methods.
174
+
175
+ #### `verify_chaos(method_name, *args, auto_apply: false, **kwargs)`
176
+
177
+ Verify a method call on a new instance of the class.
178
+
179
+ **Parameters:**
180
+ - `method_name` (Symbol): The method to verify
181
+ - `*args`: Arguments to pass to the method
182
+ - `auto_apply` (Boolean): Whether to automatically apply fixes (default: false)
183
+ - `**kwargs`: Keyword arguments to pass to the method
184
+
185
+ **Returns:** `Verifier::VerificationResult`
186
+
187
+ **Example:**
188
+ ```ruby
189
+ result = Calculator.verify_chaos(:pow, -5, 2)
190
+ result = Calculator.verify_chaos(:divide, 10, 0, auto_apply: true)
191
+ ```
192
+
193
+ #### `chaos_method_guidance`
194
+
195
+ Get the guidance hash for this class.
196
+
197
+ **Returns:** Hash of method name => guidance string
198
+
199
+ ### Module Methods
200
+
201
+ #### `ChaosToTheRescue.verify(auto_apply: false, &block)`
202
+
203
+ Verify a method call using a block.
204
+
205
+ **Parameters:**
206
+ - `auto_apply` (Boolean): Whether to automatically apply fixes (default: false)
207
+ - `&block`: Block containing the method call to verify
208
+
209
+ **Returns:** `Verifier::VerificationResult`
210
+
211
+ **Example:**
212
+ ```ruby
213
+ result = ChaosToTheRescue.verify { Calculator.new.pow(-5, 2) }
214
+ result = ChaosToTheRescue.verify(auto_apply: true) { Calculator.new.divide(10, 0) }
215
+ ```
216
+
217
+ ### Configuration
218
+
219
+ #### Global Method Guidance
220
+
221
+ Set guidance that applies to all classes:
222
+
223
+ ```ruby
224
+ ChaosToTheRescue.configure do |config|
225
+ config.method_guidance[:pow] = "Handle negative bases correctly"
226
+ config.method_guidance[:divide] = "Handle division by zero"
227
+ end
228
+ ```
229
+
230
+ ## VerificationResult Object
231
+
232
+ The result of a verification has the following attributes:
233
+
234
+ - `success` (Boolean): Whether the verification passed
235
+ - `diagnosis` (String): Explanation of the result
236
+ - `suggestion` (String): Suggested correct result (if failed)
237
+ - `fixed_code` (String): Fixed code (if available and failed)
238
+ - `actual_result` (Object): The actual result from calling the method
239
+
240
+ **Helper Methods:**
241
+ - `passed?` - Returns true if verification succeeded
242
+ - `failed?` - Returns true if verification failed
243
+
244
+ ## Examples
245
+
246
+ See `examples/guidance_and_verification.rb` for comprehensive examples covering:
247
+ - Providing guidance when defining methods
248
+ - Adding guidance to existing methods
249
+ - Simple verification
250
+ - Verification with auto-fix
251
+ - Block-based verification
252
+ - Testing edge cases
253
+ - Comprehensive workflows
254
+
255
+ ## Safety Considerations
256
+
257
+ ### Auto-fix Safety
258
+
259
+ When using `auto_apply: true`:
260
+ - The LLM-generated fix code is evaluated using `eval`
261
+ - Only use in development/test environments
262
+ - Review the `fixed_code` in the verification result before trusting it
263
+ - Set `log_level: :debug` to see the generated code
264
+
265
+ ### Guidance Best Practices
266
+
267
+ 1. **Be specific**: Include concrete examples of expected behavior
268
+ 2. **Mention common mistakes**: Help the LLM avoid known pitfalls
269
+ 3. **Cover edge cases**: Negative numbers, zero, empty strings, nil, etc.
270
+ 4. **Keep it concise**: Focus on the most important requirements
271
+
272
+ ### Verification Best Practices
273
+
274
+ 1. **Test edge cases**: Verify with unusual or boundary inputs
275
+ 2. **Review diagnoses**: Read what the LLM thinks went wrong
276
+ 3. **Manual review**: Check `fixed_code` before applying manually
277
+ 4. **Iterative fixing**: Verify again after auto-fix to ensure correctness
278
+
279
+ ## Implementation Details
280
+
281
+ ### Files Added
282
+
283
+ - `lib/chaos_to_the_rescue/verifier.rb` - Verification logic
284
+ - `spec/verifier_spec.rb` - Verifier tests
285
+ - `spec/chaos_guidance_spec.rb` - Guidance feature tests
286
+ - `examples/guidance_and_verification.rb` - Comprehensive examples
287
+ - `FEATURES.md` - This document
288
+
289
+ ### Files Modified
290
+
291
+ - `lib/chaos_to_the_rescue.rb` - Added verify method and verifier require
292
+ - `lib/chaos_to_the_rescue/chaos_rescue.rb` - Added guidance methods
293
+ - `lib/chaos_to_the_rescue/configuration.rb` - Added method_guidance attribute
294
+ - `lib/chaos_to_the_rescue/llm_client.rb` - Added guidance to prompts
295
+ - `README.md` - Added documentation for new features
296
+
297
+ ## Future Enhancements
298
+
299
+ Possible improvements:
300
+ - Verification result caching
301
+ - Batch verification of multiple methods
302
+ - Verification history tracking
303
+ - Web UI for reviewing verification results
304
+ - Integration with CI/CD for automated verification
305
+ - Support for expected result specifications
306
+ - Verification against test suites
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Valentino Stoll
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.