simulate_refactorΒΆ
Simulate applying a code change and verify that the refactored code preserves the original behavior using symbolic execution.
Quick ReferenceΒΆ
simulate_refactor(
original_code: str, # Original code
new_code: str = None, # New implementation
patch: str = None, # Or provide a patch
strict_mode: bool = False # Strict comparison
) -> RefactorSimulation
User StoriesΒΆ
| Persona | Story | Tool Value |
|---|---|---|
| π€ Sarah (AI User) | "Verify my refactor won't break behavior" | Safe code changes |
| π₯ David (Team Lead) | "Verify refactor is safe before PR approval" | Risk mitigation |
| π§ Chris (OSS Contributor) | "Verify my contribution doesn't break existing behavior" | Safe contributions |
| π‘οΈ Marcus (Security Engineer) | "Ensure refactoring preserves security properties" | Security preservation |
ParametersΒΆ
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
original_code | string | Yes | - | Original code before refactoring |
new_code | string | No* | None | New code after refactoring |
patch | string | No* | None | Unified diff patch |
strict_mode | bool | No | false | Require exact behavior match |
*One of new_code or patch is required.
Response SchemaΒΆ
{
"data": {
"is_safe": "boolean",
"behavior_preserved": "boolean",
"differences": [
{
"type": "string",
"description": "string",
"original_behavior": "string",
"new_behavior": "string",
"inputs": {},
"severity": "string"
}
],
"path_comparison": {
"original_paths": "integer",
"new_paths": "integer",
"matching_paths": "integer",
"divergent_paths": "integer"
},
"warnings": ["string"],
"recommendation": "string"
},
"tier_applied": "string",
"duration_ms": "integer"
}
ExamplesΒΆ
Verify Safe RefactorΒΆ
{
"data": {
"is_safe": true,
"behavior_preserved": true,
"differences": [],
"path_comparison": {
"original_paths": 3,
"new_paths": 3,
"matching_paths": 3,
"divergent_paths": 0
},
"warnings": [],
"recommendation": "β
Refactoring is safe. Behavior is preserved for all analyzed paths."
},
"tier_applied": "community",
"duration_ms": 95
}
Detect Behavior ChangeΒΆ
{
"data": {
"is_safe": false,
"behavior_preserved": false,
"differences": [
{
"type": "EXCEPTION_INTRODUCED",
"description": "New code raises ZeroDivisionError where original returned None",
"original_behavior": "Returns None",
"new_behavior": "Raises ZeroDivisionError",
"inputs": {"a": 10, "b": 0},
"severity": "HIGH"
}
],
"path_comparison": {
"original_paths": 2,
"new_paths": 1,
"matching_paths": 1,
"divergent_paths": 1
},
"recommendation": "β Refactoring changes behavior. The zero-division check was removed."
},
"tier_applied": "community",
"duration_ms": 78
}
Check Return Type ChangeΒΆ
{
"data": {
"is_safe": false,
"behavior_preserved": false,
"differences": [
{
"type": "RETURN_TYPE_CHANGE",
"description": "Return type changed from int to str",
"original_behavior": "Returns int (e.g., 5)",
"new_behavior": "Returns str (e.g., '5')",
"inputs": {"items": [1, 2, 3, 4, 5]},
"severity": "MEDIUM"
}
],
"recommendation": "β Return type changed. This may break callers expecting int."
}
}
Verify with PatchΒΆ
{
"original_code": "def validate(data):\n if data is None:\n return False\n if len(data) == 0:\n return False\n return True",
"patch": "--- a/validators.py\n+++ b/validators.py\n@@ -1,6 +1,3 @@\n def validate(data):\n- if data is None:\n- return False\n- if len(data) == 0:\n- return False\n- return True\n+ return bool(data)"
}
```bash codescalpel simulate-refactor \ --original-code "def validate(data):\n if data is None:\n return False\n if len(data) == 0:\n return False\n return True" \ --patch "--- a/validators.py
+++ b/validators.py @@ -1,6 +1,3 @@ def validate(data): - if data is None: - return False - if len(data) == 0: - return False - return True + return bool(data)" ```
{
"data": {
"is_safe": true,
"behavior_preserved": true,
"differences": [],
"path_comparison": {
"original_paths": 3,
"new_paths": 2,
"matching_paths": 3,
"divergent_paths": 0
},
"warnings": [
"New code has fewer explicit paths but equivalent behavior"
],
"recommendation": "β
Refactoring is safe. `bool(data)` is equivalent to the original checks."
}
}
Complex RefactoringΒΆ
Difference TypesΒΆ
| Type | Description | Severity |
|---|---|---|
EXCEPTION_INTRODUCED | New code raises exception where original didn't | HIGH |
EXCEPTION_REMOVED | Original raised exception, new doesn't | MEDIUM |
RETURN_VALUE_CHANGE | Different return value for same input | HIGH |
RETURN_TYPE_CHANGE | Return type changed | MEDIUM |
PATH_REMOVED | Execution path no longer exists | MEDIUM |
PATH_ADDED | New execution path introduced | LOW |
SIDE_EFFECT_CHANGE | Different side effects | HIGH |
Tier LimitsΒΆ
simulate_refactor capabilities vary by tier:
| Feature | Community | Pro | Enterprise |
|---|---|---|---|
| Max file size | 1 MB | 10 MB | 100 MB |
| Analysis depth | Basic | Advanced | Deep |
| Basic simulation | β | β | β |
| Path comparison | β | β | β |
| Strict mode | β | β | β |
| Max paths analyzed | 20 | 100 | Unlimited |
| Side effect detection | β | β | β Comprehensive |
| Type preservation check | β | β | β Full |
| Performance impact | β | β | β |
Community TierΒΆ
- β Simulate basic refactoring changes
- β Path comparison (original vs new)
- β Strict mode for exact behavior matching
- β Detect exception changes
- β οΈ Max 20 paths - Simple functions only
- β οΈ Limited to 1 MB files - Small files only
- β οΈ Basic analysis - May miss subtle issues
- β No side effect detection
Pro TierΒΆ
- β All Community features
- β Max 100 paths analyzed - Complex functions
- β Max 10 MB files - Larger modules
- β Advanced analysis - Better issue detection
- β Side effect detection - Track state changes
- β Type preservation - Ensure types stay consistent
- β Return value tracking - Verify outputs
Enterprise TierΒΆ
- β All Pro features
- β Unlimited paths - Any function complexity
- β Max 100 MB files - Very large modules
- β Deep analysis - Complete behavior verification
- β Comprehensive side effects - All state changes
- β Full type checking - Complete type preservation
- β Performance impact - Before/after performance comparison
Key Difference: Analysis Depth and Scale - Community: 20 paths, 1 MB, basic - Simple refactorings - Pro: 100 paths, 10 MB, advanced - Production refactorings - Enterprise: Unlimited, 100 MB, deep - Complex refactorings
Best PracticesΒΆ
- Test before and after - Don't rely solely on simulation
- Use strict mode - When exact behavior matters
- Review differences - Understand why behavior changed
- Check edge cases - Simulation may not cover all
- Combine with tests - Run existing tests after refactor
WorkflowΒΆ
graph TD
A[Plan Refactoring] --> B[simulate_refactor]
B -->|Safe| C[Apply Change]
B -->|Unsafe| D[Review Differences]
D --> E[Adjust Refactoring]
E --> B
C --> F[Run Tests]
F -->|Pass| G[Complete]
F -->|Fail| H[Investigate] Related ToolsΒΆ
- symbolic_execute - Analyze paths
- update_symbol - Apply the change
- generate_unit_tests - Create tests