Spaces:
Sleeping
Sleeping
File size: 7,108 Bytes
532e19f | 1 2 3 4 5 6 7 8 9 10 11 12 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 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 276 277 278 279 | # Two-Phase Bug Injection Refactoring
## Summary
Complete architectural refactor of the bug injection system to guarantee test quality.
**Date**: Current session
**Status**: β
Complete - Ready for testing
---
## The Problem (OLD APPROACH)
### What Was Wrong
Single GPT call asking it to:
1. Inject a bug into a function
2. Generate tests that catch the bug
### Why It Failed
- GPT created bugs independently of tests
- Tests were too generic and passed even with bugs
- Result: "detected by 0 tests" repeatedly
- No validation that tests actually expose the bug
### Example Failure
```
Bug: range(n) β range(n-1)
Test: func([1,2,3,4,5])
- Original: [1,2,3,4,5]
- Buggy: [1,2,3,4]
BUT: Test expected generic "list" result, so both passed β
```
---
## The Solution (NEW APPROACH)
### Revolutionary Idea
**Generate tests FIRST on working code, THEN inject bug**
This guarantees:
1. Tests are valid (run on working code)
2. Tests have concrete expected outputs
3. ANY bug changes outputs β guaranteed detection
### Three-Phase Workflow
#### PHASE 1: Generate Tests on Clean Code
```python
tests = _generate_tests_for_function(working_code)
# Example: 3 public + 3 secret tests
```
**Result**: 6 tests with known-good expected outputs
#### PHASE 2: Execute Tests on Original
```python
original_results = _execute_tests_on_source(original_code, tests)
# Example: {
# "(5,)": (True, "125"),
# "(3,)": (True, "27"),
# ...
# }
```
**Result**: Baseline outputs for all 6 tests
#### PHASE 3: Inject Bug β Validate Detection
```python
buggy_code = _inject_bug_into_function(original_code)
buggy_results = _execute_tests_on_source(buggy_code, tests)
differences = count_differences(original_results, buggy_results)
if differences >= 4: # At least 4/6 tests must differ
SUCCESS! β
```
**Result**: Only accept bugs that 4+ tests reliably catch
---
## Technical Changes
### New Functions
#### `_generate_tests_for_function()`
- **Purpose**: Generate 3+3 tests for WORKING code
- **Input**: Clean function source
- **Output**: `{"test_cases_public": [...], "test_cases_secret": [...]}`
- **Key**: No bug injection in this phase
#### `_inject_bug_into_function()`
- **Purpose**: Inject ONE simple bug
- **Input**: Clean function source, list of attempted bugs
- **Output**: `{"sabotaged_function_code": "...", "bug_description": "..."}`
- **Key**: Avoids repeating failed bugs via `attempted_bugs` list
#### `_execute_tests_on_source()`
- **Purpose**: Run all tests on given source
- **Input**: Source code, tests dict
- **Output**: `{test_args_str: (success, result), ...}`
- **Key**: Captures both success status AND result value
### Refactored Functions
#### `_sabotage_one_helper()` - COMPLETE REWRITE
**Before**:
- Single GPT call: bug + tests together
- ~150 lines of validation
- No output comparison
**After**:
- Phase 1: Generate tests on clean code
- Phase 2: Execute β save outputs
- Phase 3: Inject bug β execute β compare
- Validation: 4+ tests must show different outputs
- ~180 lines but much more robust
---
## Validation Strategy
### Acceptance Criteria
For a bug to be accepted:
1. β
Syntax valid (parses without errors)
2. β
Function name preserved
3. β
No revealing comments
4. β
**NEW**: At least 4/6 tests produce DIFFERENT outputs
### What "Different" Means
```python
orig_success, orig_result = original_results[test]
buggy_success, buggy_result = buggy_results[test]
different = (orig_success != buggy_success) OR (orig_result != buggy_result)
```
Examples of differences:
- Original succeeds, buggy crashes β DIFFERENT β
- Both succeed, different values β DIFFERENT β
- Both succeed, same value β NOT DIFFERENT β
---
## Parameter Simplifications
### Test Count
- **Before**: 5 public + 5 secret = 10 tests per bug
- **After**: 3 public + 3 secret = 6 tests per bug
- **Rationale**: Easier for GPT, still sufficient coverage
### Bug Distribution
- **Before**: N bugs β 2 functions β [3,2] split
- **After**: N bugs β N functions β 1 bug each
- **Rationale**: Simpler validation, clearer test ownership
---
## Expected Benefits
### Cost Savings
- Fewer retries needed (tests guaranteed to work)
- Fewer GPT calls (separate prompts are clearer)
### Quality Improvement
- **Before**: ~30% bugs detected by 0 tests
- **After (expected)**: 100% bugs detected by 4+ tests
### Debug Visibility
New metadata in results:
```python
{
"_debug_tests_catching_bug": "5/6",
...
}
```
---
## Migration Notes
### Breaking Changes
**None** - `_sabotage_one_helper()` signature unchanged
All existing calls remain compatible:
```python
_sabotage_one_helper(
bug_func_name, current_source,
surface_func_name, surface_source,
instructions, llm, indirect_mode,
call_chain=...,
previous_bugs=...,
target_nesting=...,
debug_mode=...
)
```
### Unused Parameters
- `call_chain`: Not used in new implementation (reserved for future)
- `previous_bugs`: Not used (replaced by internal `attempted_bugs` list)
- `target_nesting`: Not used in new implementation
These remain in signature for API compatibility.
---
## Testing Plan
### Quick Test
```bash
python main.py --repo Ugly_Legacy_code --num-bugs 2 --debug
```
**Expected Output**:
```
[saboteur] PHASE 1: Generating tests for 'func1' (working code)...
[saboteur] PHASE 1 SUCCESS: Generated 3 public + 3 secret tests
[saboteur] Executing tests on ORIGINAL code...
[saboteur] Test (5,) -> OK: 125
[saboteur] Test (3,) -> OK: 27
...
[saboteur] PHASE 2: Bug injection attempt 1/3...
[bug_inject] β Bug injected: Changed range(n) to range(n-1)
[saboteur] Executing tests on BUGGY code...
[saboteur] Test (5,) -> OK: 24
[saboteur] DIFFERENCE: (5,)
[saboteur] Original: 125
[saboteur] Buggy: 24
...
[saboteur] Bug caught by 5/6 tests
[saboteur] SUCCESS! Bug reliably detected by 5/6 tests
```
### Metrics to Watch
- **Bug acceptance rate**: Should be higher (~80%+ vs ~40% before)
- **Tests catching bugs**: Should always be 4+/6 (100% vs ~30% before)
- **GPT retries**: Should be lower (clearer prompts)
---
## Files Modified
### architect/saboteur.py
- **Lines 595-673**: Added `_generate_tests_for_function()`
- **Lines 673-748**: Added `_inject_bug_into_function()`
- **Lines 779-803**: Added `_execute_tests_on_source()`
- **Lines 821-1000**: Complete rewrite of `_sabotage_one_helper()`
**Total Changes**: ~400 lines added/modified
---
## Rollback Plan
If issues arise, revert commit:
```bash
git log --oneline # Find commit before refactor
git revert <commit-hash>
```
Old implementation preserved in git history.
---
## Next Steps
1. β
Code refactor complete
2. βοΈ Test with `--num-bugs 2 --debug`
3. βοΈ Verify "X/6 tests caught bug" in logs
4. βοΈ Run full test with 5 bugs
5. βοΈ Compare metrics vs old approach
---
## Credits
**Concept**: User's revolutionary insight - "generate tests first, then inject bug"
**Implementation**: Complete architectural refactor of saboteur.py
**Philosophy**: Guarantee test quality through execution validation, not hope
|