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.
Three near-identical functions, one future bug fixed in only two of them. This prompt consolidates duplication into shared logic — and knows when near-duplicates should stay separate.
Duplicate code removal sounds mechanical until the duplicates turn out to differ on purpose. This prompt uses the maintainability goal — extract the shared logic so a future fix lands in one place — with the warning that saves refactors: near-duplicate code is not always true duplication, and merging two blocks that evolve for different reasons creates coupling, not reuse. The loaded setup carries a real-world constraint of exactly that kind: one of the three export functions uses a subtly different date format, and the contract demands confirmation of whether that is intentional before any merge.
Map the copies
Identical, near-identical, and intentionally-different blocks get separated before anything merges.
Confirm the differences
A subtle divergence — a date format, a rounding rule — is a question for a human, not a casualty of the merge.
Extract once, verify each
The shared logic lands in one place; every original call path is validated against its old behavior.
It's told to ask before merging. The stated constraint is that the CSV export uses a subtly different date format — "confirm whether that is intentional before merging." The TRANSFORMATION GUIDANCE warns that near-duplicate blocks merged when they evolve for different reasons "creates coupling, not reuse." The refactor-prompt-builder outputs this prompt; the divergence becomes a question for you, not a silent casualty.
It's constrained not to. BEHAVIOR PRESERVATION REQUIREMENTS keep formats, ordering, precision, side effects, and public contracts identical, and if goal and behavior conflict, "behavior wins: stop and explain the conflict." Anything skipped for safety lands under "Suggested but not applied." You still validate each original call path — the prompt produces the refactor, not a proof it's behavior-neutral.
"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.
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.
"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.