refactor
SKILL.md
Skill: Refactor
What This Skill Does
Enforces safe refactoring discipline: tests green before → refactor with minimal diffs → tests green after → update docs. Prevents the common problem where refactoring introduces subtle regressions because tests weren't run at each step.
When to Use
- When the user wants to restructure, rename, or reorganize code
- When
diff-reviewrecommends refactoring - When tech debt needs to be addressed
- When extract/inline/rename operations span multiple files
Do NOT use this for new features — use implement-phase or scaffold for that.
Execution Model
- Always: the primary agent runs this skill directly.
- Rationale: refactoring requires file edits, test runs, and iterative verification.
- Token budget: varies by scope. Simple renames: ~3k. Module extraction: ~10-15k.
Workflow
Step 1: Define Refactoring Scope
Use the question tool to clarify:
- What to refactor? (specific function, module, pattern)
- What's the goal? (readability, performance, modularity)
- What's the constraint? (behavior must not change)
Step 2: Pre-Refactor Baseline
Run all relevant tests first:
npm test # or: pytest, go test, etc.
Record the test results. If tests are already failing, STOP — fix tests first before refactoring.
Step 3: Identify Dependencies
Before changing anything, map what depends on the target:
# Find all files that import/reference the target
grep -r "import.*<target>" --include="*.ts" -l
grep -r "from.*<target>" --include="*.py" -l
This is the blast radius of the refactoring.
Step 4: Apply Changes Incrementally
Refactor in small, verifiable steps:
- Step A: make one atomic change (e.g., rename function)
- Run tests: verify nothing broke
- Step B: make the next atomic change
- Run tests: verify again
- Repeat until refactoring is complete
Never make multiple unrelated changes before running tests.
Step 5: Post-Refactor Verification
After all changes:
- Run the full test suite
- Compare test results with Step 2 baseline — same tests should pass
- Check for new warnings or deprecations
- Run the linter/type checker
Step 6: Update Documentation
If the refactoring changed:
- Module structure → update module docs
- Public API → update feature docs
- File locations → update AGENTS.md module rules
- Architecture → consider creating an ADR (
adr-create)
Step 7: Create Minimal Diff
Review the total changes:
git diff --stat
Ensure:
- No unrelated changes snuck in
- No formatting-only changes mixed with logic changes
- Commit message clearly describes the refactoring
Rules
- Tests must pass before AND after: never start refactoring with failing tests. Never leave with failing tests.
- Behavior preservation: refactoring changes structure, not behavior. If behavior needs to change, that's a feature, not a refactor.
- Incremental changes: make one change at a time, test, repeat. Never refactor multiple things in one big bang.
- Minimal diffs: the diff should show only the refactoring. No formatting changes, no "while I'm here" improvements.
- Document the why: if the refactoring isn't obvious, explain the motivation in the commit message or an ADR.
- No built-in explore agent: do NOT use the built-in
exploresubagent type.
Weekly Installs
1
Repository
flitzrrr/openco…g-skillsGitHub Stars
1
First Seen
Mar 3, 2026
Security Audits
Installed on
amp1
cline1
opencode1
cursor1
kimi-cli1
codex1