| name | refactor-with-safety |
| description | refactor code without breaking functionality. Trigger when asked to clean up, restructure, or optimize code. Enforces test coverage and quality gates. |
Refactor with Safety
Perform structural changes while preserving behavior and stability.
Workflow
-
Preparation
- Audit: Read
AGENTS.md for domain constraints.
- Baseline: Run tests (
pnpm run test) to ensure everything passes before you start.
- Baseline (script form): Run
pnpm run test.
- Coverage: If tests are missing for the target, add them first (use
golden-path-test-author).
-
Execution
- Small Steps: Refactor one function/component at a time.
- Interface Preservation: Keep function signatures stable if possible.
- Linting: Run
pnpm run lint frequently to catch syntax errors.
-
Verification
- Test: Run
pnpm run test after each change.
- Build: Run
pnpm run build to ensure no circular dependencies or export errors.
- Review: Check if the logic is actually simpler/better.
Rules
- No Behavior Change: Unless explicitly requested, refactoring should not alter logic.
- Fix the Code, Not the Test: If a test fails, the refactor is wrong (unless the test was testing implementation details).
- Commit Often: Use
change-plan-conventional-commits.
Example
Input: "Refactor calculateScore to be more readable."
Action:
- Run
tests/score.test.js. (Passes).
- Break
calculateScore into getBaseScore and getMultipliers.
- Run
tests/score.test.js. (Passes).
- Run
pnpm run lint. (Passes).
Output:
"Extracted helper functions getBaseScore and getMultipliers. Tests pass. Logic is unchanged but clearer."
Skill sync: compatible with React 19.2.6 / Vite 8.0.10 / Tailwind 4.2.4 baseline as of 2026-05-20.