Refactor Planning
Purpose
Plan a refactor that improves structure or quality without changing external behavior, in small reversible steps backed by tests. Produces a refactor work item in ../../work-items/refactors/.
When to Use
- Request classified as refactor, or cleanup enabling later work.
- Not when behavior should change (that's
feature-planning) or when fixing a defect (bug-investigation).
Inputs
- The target code and its current tests.
- The improvement goal (readability, decoupling, performance-neutral cleanup).
Discovery Questions
- What is the concrete improvement goal, and how is "better" judged?
- Does adequate test coverage exist to prove behavior is preserved?
- What is the blast radius; who depends on this code?
- Can it be done in small, independently verifiable steps?
Responsibilities
- Audit the target and its dependents.
- Confirm/establish test coverage before touching risky areas.
- Plan small, reversible, behavior-preserving steps.
- Define verification that behavior is unchanged at each step.
- Record the refactor work item.
Required Workflow
- Read the target + dependents + tests.
- If coverage is inadequate, plan to add characterization tests first.
- Break the refactor into small reversible steps.
- Define per-step verification (tests green, behavior identical).
- Record the work item (steps, risk, verification).
Decision Rules
- Behavior-preserving by definition — any behavior change reclassifies as a feature.
- Add tests before refactoring under-covered risky code.
- Prefer many small steps over a big-bang rewrite.
- If risk is high and value low, recommend deferring.
Rules
- Do not change observable behavior.
- Keep each step independently verifiable and revertible.
- Preserve public contracts unless the plan explicitly versions them.
Anti-Patterns
- Big-bang rewrite with no rollback path.
- Refactoring untested, risky code without adding tests first.
- Slipping behavior changes into a "refactor."
Validation Checklist
Definition of Done
A recorded refactor work item with a clear goal, sufficient test coverage (or a plan to add it), small reversible steps, and per-step verification proving behavior is unchanged.
Related Skills
existing-project-audit, testing-strategy, task-planning, code-review, performance-review (if perf-motivated), project-orchestrator.
Related Knowledge
../../knowledge/ (module contracts, invariants).
Related References
../../references/<topic>/ for patterns if needed.
Context Loading Guidance
- Requires: target code, dependents, current tests, the goal.
- Does not require: unrelated modules, the full reference tree, unrelated skills.
- May load:
testing-strategy (coverage), performance-review (if perf-driven).
- Stop when: steps + verification are recorded.
Token Efficiency Guidance
Scope reading to the target and its direct dependents. Capture steps tersely; rely on tests as the verification record rather than prose.
1---2name: refactor-planning3description: Use to plan a behavior-preserving structural improvement — audit the target, ensure test coverage exists, plan small reversible steps, and verify behavior is unchanged. Any intended behavior change is a feature, not a refactor.4---56# Refactor Planning78## Purpose910Plan a refactor that improves structure or quality **without changing external behavior**, in small reversible steps backed by tests. Produces a refactor work item in `../../work-items/refactors/`.1112## When to Use1314- Request classified as **refactor**, or cleanup enabling later work.15- **Not** when behavior should change (that's `feature-planning`) or when fixing a defect (`bug-investigation`).1617## Inputs1819- The target code and its current tests.20- The improvement goal (readability, decoupling, performance-neutral cleanup).2122## Discovery Questions2324- What is the concrete improvement goal, and how is "better" judged?25- Does adequate test coverage exist to prove behavior is preserved?26- What is the blast radius; who depends on this code?27- Can it be done in small, independently verifiable steps?2829## Responsibilities3031- **Audit** the target and its dependents.32- **Confirm/establish test coverage** before touching risky areas.33- Plan **small, reversible, behavior-preserving steps**.34- Define **verification** that behavior is unchanged at each step.35- Record the refactor work item.3637## Required Workflow38391. Read the target + dependents + tests.402. If coverage is inadequate, plan to add characterization tests first.413. Break the refactor into small reversible steps.424. Define per-step verification (tests green, behavior identical).435. Record the work item (steps, risk, verification).4445## Decision Rules4647- Behavior-preserving by definition — any behavior change reclassifies as a feature.48- Add tests before refactoring under-covered risky code.49- Prefer many small steps over a big-bang rewrite.50- If risk is high and value low, recommend deferring.5152## Rules5354- Do not change observable behavior.55- Keep each step independently verifiable and revertible.56- Preserve public contracts unless the plan explicitly versions them.5758## Anti-Patterns5960- Big-bang rewrite with no rollback path.61- Refactoring untested, risky code without adding tests first.62- Slipping behavior changes into a "refactor."6364## Validation Checklist6566- [ ] Improvement goal explicit.67- [ ] Coverage confirmed or characterization tests planned.68- [ ] Steps small and reversible.69- [ ] Per-step verification defined.70- [ ] Behavior-preservation guaranteed by tests.7172## Definition of Done7374A recorded refactor work item with a clear goal, sufficient test coverage (or a plan to add it), small reversible steps, and per-step verification proving behavior is unchanged.7576## Related Skills7778`existing-project-audit`, `testing-strategy`, `task-planning`, `code-review`, `performance-review` (if perf-motivated), `project-orchestrator`.7980## Related Knowledge8182`../../knowledge/` (module contracts, invariants).8384## Related References8586`../../references/<topic>/` for patterns if needed.8788## Context Loading Guidance8990- **Requires:** target code, dependents, current tests, the goal.91- **Does not require:** unrelated modules, the full reference tree, unrelated skills.92- **May load:** `testing-strategy` (coverage), `performance-review` (if perf-driven).93- **Stop when:** steps + verification are recorded.9495## Token Efficiency Guidance9697Scope reading to the target and its direct dependents. Capture steps tersely; rely on tests as the verification record rather than prose.