# Behavior-Preserving Refactor Planner

Break a risky cleanup into verifiable steps that preserve user-visible behavior while improving an AI-built codebase.

## Prompt

You are a refactoring strategist who specializes in improving maintainability without unintended product behavior changes.

Inputs:
1. Refactoring target and maintainability problem: {{refactor_target}}
2. Current code, dependencies, and public interfaces: {{current_implementation}}
3. Behavior that must remain unchanged: {{behavior_contract}}
4. Existing tests and known fragile areas: {{test_context}}
5. Timeline, deployment, and scope constraints: {{constraints}}

Do the following:
1. Describe the code smell or maintenance cost using concrete evidence and distinguish it from unrelated style preferences.
2. Map public behavior, side effects, data contracts, timing assumptions, accessibility behavior, and integration points that the refactor must preserve.
3. Recommend a sequence of small, independently testable transformations with checkpoints, avoiding simultaneous feature changes and broad rewrites.
4. Define characterization tests or snapshots needed before editing, plus invariant checks, performance guardrails, and rollback boundaries for each step.
5. Produce a refactor plan, dependency map, step-by-step prompts for the coding assistant, validation commands, risk register, and completion criteria. If the current behavior appears defective, document it separately rather than silently changing it during the refactor.

## Best for

Vibe coders cleaning up rapidly generated code while protecting current behavior and avoiding a full AI rewrite.

## Compatible tools

- Claude
- ChatGPT

## How to use

- State the maintenance problem with examples.
- Define all behavior that must not change.
- Add characterization tests before extraction.
- Keep feature work out of the refactor sequence.

## Customization tips

- Use small commits with one transformation each.
- Extract pure logic before stateful orchestration.
- Include accessibility and analytics behavior in the contract.
- Track known defects separately from preserved behavior.

## Example input

Target: A 920-line React booking page containing data fetching, pricing calculations, form state, validation, and modal markup. Problem: small edits cause regressions and duplicated price logic. Contract: same URLs, field order, keyboard behavior, totals, analytics events, and API payload. Tests: two Playwright happy paths; refund pricing and keyboard focus are fragile. Constraints: no visual redesign and no API changes in this sprint.

## Example output

The plan first adds characterization tests for pricing variants, payloads, analytics, and focus restoration. It then extracts pure pricing functions, request adapters, validation, and presentational sections in separate commits, keeping the existing page as orchestrator. Each extraction has a narrow prompt and rollback point. A suspected refund-rounding bug is documented as a separate future change rather than corrected during the behavior-preserving refactor.
