Refactoring
Core principle
Refactoring changes structure while preserving behavior. Improve design in small, reversible steps, with characterization or regression proof when behavior could drift.
Load when
Load for an existing software project when the user asks to improve code structure while preserving an identified behavior contract: extract or inline code, reduce duplication, clarify names or type boundaries, remove code smells, or reorganize ownership.
If the user wants behavior change, use implementation. If a failure cause is unknown, use root-cause-analysis. If the refactor is broad or risky, create a refactor doc before mutation.
At entry, use the full Keystone path when the work changes integrated project code and needs project-specific invariant proof. Handle isolated text cleanup, mechanical formatting, and standalone snippets directly. Explicit invocation selects the full Refactoring behavior.
Outcome contract
A complete refactor reports:
- the behavior invariant that must not change;
- smells or pressures addressed;
- isolation checked before mutation via
../_shared/gates/isolation.md;
- characterization/regression proof used before risky edits;
- files changed and why each changed;
- verification commands/results or explicit proof gaps;
- checkpoint handoff to
change-review when review is needed.
Process
- Classify size and risk.
- Small/local: one area, clear invariant, existing proof likely enough.
- Large/cross-cutting: multiple boundaries, weak coverage, shared contracts, or context-window risk.
- Completion criterion: refactor path is either safe for direct mutation or documented first.
- For large refactors, write a refactor doc under
docs/keystone/refactors/YYYY-MM-DD-<slug>.md before editing.
- Include goal, invariants, smells, affected areas, slices, proof, rollback, and review focus.
- Completion criterion: doc is specific enough for
task-creation or implementation.
- Pass isolation before mutation.
- Load/check
../_shared/gates/isolation.md.
- Respect unrelated dirty files and protected scope.
- Completion criterion: mutation scope is safe.
- Establish behavior proof.
- Prefer existing behavior tests; add characterization coverage when the invariant lacks a tripwire.
- Completion criterion: there is a tripwire for accidental behavior change or a documented proof gap.
- Apply small refactorings.
- Prefer rename, extract, inline, move, split, consolidate, simplify conditionals, remove dead code, and clarify ownership.
- Keep public contracts stable unless explicitly approved.
- Completion criterion: each step is understandable and reversible.
- Use engineering standards.
- Load
../_shared/engineering-standards.md for architecture or ownership decisions.
- Remove abstractions that lack current pressure.
- Completion criterion: the result has clearer ownership, state, naming, or boundaries.
- Verify.
- Load and pass
../_shared/gates/proof.md for the preserved invariant.
- Completion criterion: behavior invariant is supported by observed evidence.
- Checkpoint and hand off.
- Use
../_shared/gates/checkpoint.md.
- Hand off to
change-review for non-trivial refactors or leave an explicit review pointer.
Smell prompts
Investigate duplication, long functions, god objects, feature envy, primitive obsession, data clumps, shotgun surgery, divergent change, hidden control flow, speculative generality, vague managers/helpers, mixed abstraction levels, duplicated state, and unclear ownership.
Hard rules
- Preserve behavior unless the user explicitly approves a behavior change.
- Do not disguise feature work as refactoring.
- Do not perform broad refactors without a refactor doc.
- Do not trust “looks equivalent” without proof or an explicit proof gap.
- Do not introduce patterns for imagined futures.
Output format
## Refactor report
Invariant: ...
Size/risk: small / large
Smells addressed: ...
Files changed: ...
Verification: ...
Risks/gaps: ...
### Checkpoint
Current skill: refactoring
Completed gates: ...
Next required skill: change-review / implementation / none
Next check: ...
Action: continue now / ask user / pending pointer / stop
1---2name: refactoring3description: Program source-code refactoring for an explicit structural code improvement to structure, ownership, types, or duplication that preserves identified executable behavior. Select only when the request requires a structural code change with project-specific invariant proof, or enters through a canonical handoff from another Keystone skill.4---56# Refactoring78## Core principle910Refactoring changes structure while preserving behavior. Improve design in small, reversible steps, with characterization or regression proof when behavior could drift.1112## Load when1314Load for an existing software project when the user asks to improve code structure while preserving an identified behavior contract: extract or inline code, reduce duplication, clarify names or type boundaries, remove code smells, or reorganize ownership.1516If the user wants behavior change, use `implementation`. If a failure cause is unknown, use `root-cause-analysis`. If the refactor is broad or risky, create a refactor doc before mutation.1718At entry, use the full Keystone path when the work changes integrated project code and needs project-specific invariant proof. Handle isolated text cleanup, mechanical formatting, and standalone snippets directly. Explicit invocation selects the full Refactoring behavior.1920## Outcome contract2122A complete refactor reports:2324- the behavior invariant that must not change;25- smells or pressures addressed;26- isolation checked before mutation via `../_shared/gates/isolation.md`;27- characterization/regression proof used before risky edits;28- files changed and why each changed;29- verification commands/results or explicit proof gaps;30- checkpoint handoff to `change-review` when review is needed.3132## Process33341. Classify size and risk.35 - Small/local: one area, clear invariant, existing proof likely enough.36 - Large/cross-cutting: multiple boundaries, weak coverage, shared contracts, or context-window risk.37 - Completion criterion: refactor path is either safe for direct mutation or documented first.382. For large refactors, write a refactor doc under `docs/keystone/refactors/YYYY-MM-DD-<slug>.md` before editing.39 - Include goal, invariants, smells, affected areas, slices, proof, rollback, and review focus.40 - Completion criterion: doc is specific enough for `task-creation` or `implementation`.413. Pass isolation before mutation.42 - Load/check `../_shared/gates/isolation.md`.43 - Respect unrelated dirty files and protected scope.44 - Completion criterion: mutation scope is safe.454. Establish behavior proof.46 - Prefer existing behavior tests; add characterization coverage when the invariant lacks a tripwire.47 - Completion criterion: there is a tripwire for accidental behavior change or a documented proof gap.485. Apply small refactorings.49 - Prefer rename, extract, inline, move, split, consolidate, simplify conditionals, remove dead code, and clarify ownership.50 - Keep public contracts stable unless explicitly approved.51 - Completion criterion: each step is understandable and reversible.526. Use engineering standards.53 - Load `../_shared/engineering-standards.md` for architecture or ownership decisions.54 - Remove abstractions that lack current pressure.55 - Completion criterion: the result has clearer ownership, state, naming, or boundaries.567. Verify.57 - Load and pass `../_shared/gates/proof.md` for the preserved invariant.58 - Completion criterion: behavior invariant is supported by observed evidence.598. Checkpoint and hand off.60 - Use `../_shared/gates/checkpoint.md`.61 - Hand off to `change-review` for non-trivial refactors or leave an explicit review pointer.6263## Smell prompts6465Investigate duplication, long functions, god objects, feature envy, primitive obsession, data clumps, shotgun surgery, divergent change, hidden control flow, speculative generality, vague managers/helpers, mixed abstraction levels, duplicated state, and unclear ownership.6667## Hard rules6869- Preserve behavior unless the user explicitly approves a behavior change.70- Do not disguise feature work as refactoring.71- Do not perform broad refactors without a refactor doc.72- Do not trust “looks equivalent” without proof or an explicit proof gap.73- Do not introduce patterns for imagined futures.7475## Output format7677```markdown78## Refactor report79Invariant: ...80Size/risk: small / large81Smells addressed: ...82Files changed: ...83Verification: ...84Risks/gaps: ...8586### Checkpoint87Current skill: refactoring88Completed gates: ...89Next required skill: change-review / implementation / none90Next check: ...91Action: continue now / ask user / pending pointer / stop92```