Refactor Prompt — the Behavior Preservation Contract
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
For code that ships weekly: a refactoring prompt where regression avoidance outranks improvement depth, log lines stay intact, and every step is rollback-friendly.
Safe refactoring is a discipline, not an adjective. This prompt configures the contract for production code with zero regression tolerance: a conservative risk profile (smallest change set, skip anything unverifiable), production context duties (behavior-relevant logging stays intact so a regression would be visible, changes structured for rollout and rollback), and a strict validation plan with before/after verification, behavior comparison, regression validation, and integration checks. The model is told what most refactor prompts never say: a smaller, safer refactor beats a better, riskier one.
Declare production stakes
The production context adds duties: observability intact, rollout-safe structure, regression avoidance over depth.
Keep the contract conservative
The smallest change set that achieves the goal — unverifiable transformations become suggestions, not changes.
Demand the strict plan
Before/after inputs, behavior comparison per unit, regression and integration verification — riskiest first.
It caps how far the model goes on that production code. Conservative tells it to make the smallest change set that achieves the Maintainability goal, prefer many small independently-safe transformations over one restructuring, and skip any transformation whose safety cannot be established from the code in front of it — listing that as a suggestion instead. Public contracts, signatures, and observable behavior stay exactly as they are.
One stated constraint pins it: behavior-relevant log lines are consumed by alerting and must stay intact. Observability is kept untouched so a regression stays visible instead of silent — emitted events, log lines other systems consume, and their relative order are all preserved. Any change touching them must be flagged, never applied quietly.
Four, under VALIDATION STRATEGY. It demands a before/after verification plan (specific inputs to run against both versions and the outputs that must match), a per-unit behavior comparison with evidence each behavior survived, regression validation naming existing tests that must pass plus missing tests that should exist, and integration verification for every external touchpoint — API consumers, database, events, files.
Behavior wins. The prompt is explicit: if achieving the Maintainability goal and preserving observable behavior conflict, the model stops and explains the conflict instead of compromising it. Any knowingly behavior-affecting change gets flagged, and worthwhile transformations skipped for safety land under a 'Suggested but not applied' list with the information that would unlock them.
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
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.
The full path to taming an inherited codebase — understand it, document its architecture, pin its behavior with tests, then refactor, modernize, review, speed up, and ship it without breaking what works.
A complete AI-assisted review pass — not one prompt — that ends with ranked findings, tests guarding behavior, and a refactor plan when one is warranted.
Restructure code you own without breaking it — change only what's worth changing, and prove with tests and a diff that behavior held.
The draft is 90% right and you ask AI to fix the one thing that's wrong — and it hands back a whole new version with the good parts rewritten worse. Here's how to ask for a targeted edit that fixes what's broken and leaves the rest exactly as it was.
You ask for a one-line fix and get a sixty-line diff — a refactor, a fallback, some renames "while it's in there." Here's how to keep AI from building more than you asked: state the exact change, spell out the non-goals, cap the blast radius, and make it show a plan first.
"Refactor this to be cleaner" returns tidier code that quietly changed the null handling and an error type — a silent regression. Here's how to refactor with AI without changing behavior: name a small goal, list what must stay identical, forbid fixes, and verify the diff.