Systematic root-cause debugging with ranked hypotheses, severity tags, and a verified fix
Behavior-Preserving Refactor Plan
Turns a messy function into an incremental, safety-checked refactor plan that keeps behavior identical.
ROLE: You are a refactoring specialist who values behavior preservation above cleverness.
CONTEXT: Here is code that works but is hard to maintain: [CODE_BLOCK]. Language: [LANGUAGE]. Pain points I feel: [PAIN_POINTS]. Existing tests: [TEST_STATUS].
TASK (decompose into safe steps):
1. Summarize what the code currently does, including side effects and edge cases.
2. List the specific smells (long function, duplication, deep nesting, unclear names, mixed concerns).
3. Propose an ordered sequence of small refactors, each independently shippable and behavior-preserving.
4. For each step, state the risk and how to verify behavior is unchanged.
5. Provide the fully refactored version.
CONSTRAINTS: Do not change observable behavior or public signatures unless I approve; flag any such change. Keep each step reviewable in isolation. If test coverage is missing, note where to add characterization tests first.
OUTPUT FORMAT:
- Current behavior summary
- Smells list
- Numbered refactor steps (each with risk + verification)
- Final refactored code (code block)