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.
Dependency cycles, leaky modules, misplaced responsibilities — cleaned up within the existing design. Explicitly not an invitation to invent a new architecture.
Architecture refactors attract scope explosions: asked to break one dependency cycle, the model proposes a hexagonal rewrite. This prompt uses the architecture cleanup goal with its built-in fence — straighten boundaries and dependencies within the existing design; do not invent a new architecture, introduce new layers, or redesign the system. The priorities are practical: modules expose purpose not internals, high-level policy stops depending on low-level detail, cycles get broken, misplaced responsibilities move to the module that owns the concept. An aggressive risk profile allows real restructuring — staged, verifiable, and traceable to the goal.
Fence the scope
Cleanup within the existing design — the contract names the redesign temptation and forbids it.
Fix direction and ownership
Dependencies point from policy to detail, cycles break, responsibilities move to their owning module.
Verify at the seams
Module moves change visibility and initialization order — every consumer must still get the same contract.
That's exactly what the built-in fence prevents. The TRANSFORMATION GUIDANCE states this is "cleanup within the existing architecture — do not invent a new architecture, introduce new layers, or redesign the system." So a request to break the reporting-package cycle stays scoped to boundaries and dependency direction, not the hexagonal rewrite these refactors usually attract.
Aggressive applies only to the SIZE of the change, never its safety: no behavioral assumptions, no feature invention, no guessed requirements. The BEHAVIOR PRESERVATION requirements keep outputs, business rules, side-effect ordering, and public contracts identical, and if the goal and preserving behavior ever conflict, behavior wins and the model stops to explain.
The VALIDATION STRATEGY validates at the integration level, because "a module move preserves behavior only if every consumer still gets the same contract." It asks for a before/after verification plan, a behavior comparison per changed unit, and integration checks on every external touchpoint — since moving code across boundaries shifts visibility and initialization order.
"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.