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.
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.
AI-generated code has a recognizable signature: repetition where a human would extract, abstraction layers that make no decision, defensive checks for states that cannot occur, and over-general solutions to specific problems. This prompt configures the contract for that material — the AI-generated context hunts each failure mode by name, and adds the rule that makes this cleanup different from other refactors: generated code can look right while being subtly wrong, so the model must verify the code does what it appears to do, and a discovered bug gets flagged rather than silently preserved as behavior. Simplification priorities do the deflating; the safety rules keep it honest.
Hunt the signature
Repetition, over-abstraction, impossible-state checks — the known failure modes of generated code, by name.
Deflate, don't redesign
Simplification priorities remove the bloat; the non-goals keep the cleanup from becoming a rewrite.
Flag what looks wrong
Subtly-wrong generated logic gets flagged as a question — never silently preserved, never silently fixed.
It hunts the failure modes specific to generated code by name: repetition to consolidate, "unnecessary wrappers, interfaces with a single implementation, layers that make no decision," and "defensive checks for impossible states." The rule that sets it apart is the looks-right-but-wrong one — verify the code does what it appears to do. The refactor-prompt-builder configures this contract; your assistant runs the cleanup and you review the change list.
It flags the bug rather than fixing or preserving it. The context rule is explicit: "do not silently preserve a bug as behavior: flag it," and the SAFETY RULES say to flag uncertain behavior "instead of deciding it" — ask, don't guess. Worthwhile-but-unsafe transformations land under "Suggested but not applied" with what information would unlock them. That keeps it a refactor, not an uncontrolled patch.
You don't have to. The line reads "Language: not specified — infer it from the code and stay consistent with it," so it works from what you paste into the "Code to refactor" block. It also preserves public contracts — signatures, return types, error types, serialized shapes — unless you explicitly say otherwise, so inferring the language doesn't put your API surface at risk.
"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.
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.