Safe Refactoring Prompt — Zero Regression Tolerance
For code that ships weekly: a refactoring prompt where regression avoidance outranks improvement depth, log lines stay intact, and every step is rollback-friendly.
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
Most refactor prompts hand the model code and an adjective. The refactoring contract structures the transformation instead: a goal with named priorities, a risk profile that sizes the change set, seven safety rules that apply at every risk level, and the flagship — behavior preservation requirements covering observable behavior, business rules, outputs, side effects, and integrations. Uncertain behavior becomes a question, never a silent decision. This setup loads a classic large-method decomposition on business-critical code to show the full contract.
Name the goal, not just the wish
Readability, maintainability, simplification — each goal carries different priorities and different known failure modes.
Size the risk
Conservative demands the smallest change set; Aggressive allows restructuring — but behavior preservation never turns off.
Read the assumptions first
Every transformation that depends on an UNVERIFIED assumption arrives flagged — that list is where regressions hide.
No — the prompt returns refactored code plus a change list and a validation plan, but it never applies, runs, tests, or merges anything. You paste the output into your own AI tool, then review and merge yourself. Under the seven safety rules, uncertain behavior arrives as a flagged question, and the conflict rule 'behavior wins' stops it from silently compromising your business rules.
Paste the code into the 'Code to refactor' block, then fill the EXISTING CODE CONTEXT: whether it's business-critical, the language (or 'infer it'), and stated constraints like 'order state transitions must happen in exactly the current sequence.' Also set the refactoring goal and a RISK PROFILE level — Conservative, Balanced, or Aggressive. That context lets the model size the change set and know what must survive untouched.
Run the VALIDATION STRATEGY the prompt returns: a before/after verification plan naming specific inputs and the outputs that must match, a behavior comparison per changed unit, the existing regression tests that must pass, and integration verification for every external touchpoint. Then read the ASSUMPTIONS list — anything marked UNVERIFIED, or flagged as possibly behavior-affecting, is where a regression can hide and needs your own review before merge.
It returns the complete refactored code — no elided sections or 'rest unchanged' placeholders — then a change list stating, for each change, what changed, why it serves the goal, and the behavior impact (expected: none). Any change that could plausibly alter behavior comes flagged with the check that confirms it doesn't, and safe transformations skipped for caution appear under 'Suggested but not applied' with what would unlock them.
For code that ships weekly: a refactoring prompt where regression avoidance outranks improvement depth, log lines stay intact, and every step is rollback-friendly.
Generated code duplicates instead of extracting, wraps without deciding, and defends against impossible states. This prompt hunts those exact failure modes — without preserving bugs as behavior.
Dependency cycles, leaky modules, misplaced responsibilities — cleaned up within the existing design. Explicitly not an invitation to invent a new architecture.
"Review this code" gets shallow comments. The review contract gets findings with severities, a checklist, and a verdict.
getByRole over CSS chains, auto-wait over sleep, web-first assertions — Playwright tests written the way Playwright wants.
"Fix this error" gets guesses. The investigation contract gets a ten-stage diagnosis: facts separated from assumptions, alternatives weighed, fixes justified.
Build behavior-preserving refactor prompts — goal, risk level, and code context turn into a safe refactoring contract.