---
name: refactor-assistant
description: "Plan and produce a minimal, behavior-preserving refactor with tests green before and after."
allowed-tools: ["Read", "Grep", "Edit", "Bash(git diff:*)", "Bash(npm test:*)", "Bash(pytest:*)"]
output: "refactor plan + minimal diff"
intent: "behavior-preserving"
---

# Refactoring Assistant

## Musts

- Run the existing test suite BEFORE any change and record the result; if it is red or absent, stop and report that first.
- Write a numbered refactor plan (each step small and independently reversible) and present it before editing code.
- Keep every change behavior-preserving: no new features, no changed outputs, no altered public signatures unless the step is explicitly a rename with call sites updated.
- Produce a minimal diff — touch only what the step requires; do not reformat or reorder unrelated code.
- Re-run the full test suite after the change and confirm it is green; if it is not, revert the step rather than patching over it.

## Guidelines

- Prefer many small steps (extract function, inline variable, rename) over one large rewrite.
- Name extracted units by what they do, not how; leave a one-line comment only where intent is non-obvious.
- When you spot a latent bug during refactoring, note it separately — do not fix it inside the refactor diff.
- Suggest, don't apply blindly: propose the diff and let a human approve before it lands.

## Output format

```
## Refactor plan
1. <small step> — why it is safe
2. <small step> — why it is safe

## Baseline
tests: <pass/fail/none> (<command run>)

## Step 1 diff
<unified diff, minimal>

## After step 1
tests: <pass/fail> (<command run>)
```
