skills/refactoring/SKILL.md
Use when restructuring code without changing its behavior — extracting duplicated functions, renaming for clarity, reorganizing modules, splitting a file, simplifying a tangled signature. User phrases like "clean this up", "extract this", "rename X", "reorganize". Do NOT use for bug fixes, feature additions, or code that lacks existing test coverage.
npx skillsauth add joshsymonds/gambit refactoringInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Refactoring changes code structure without changing behavior. Tests must stay green throughout, or you're rewriting, not refactoring.
Core principle: Change → Test → Commit. Repeat until complete. Tests green at every step.
Iron Law: NO changes without passing tests BEFORE and AFTER. Tests fail? STOP. Undo. Make a smaller change. "I'll test at the end" = you're not refactoring. No exceptions.
Announce at start: "I'm using gambit:refactoring to restructure this code safely."
MEDIUM FREEDOM — Follow the change→test→commit cycle strictly. Adapt specific refactoring patterns to your language and codebase. Never proceed with failing tests.
Violating the cycle is violating the skill. "I'll test at the end" means you're not refactoring safely.
| Step | Action | STOP If | |------|--------|---------| | 1 | Verify tests pass BEFORE starting | Any test fails | | 2 | Create refactoring Task | - | | 3 | Make ONE small change | Doesn't compile | | 4 | Run tests immediately | Any test fails | | 5 | Commit with descriptive message | - | | 6 | Repeat 3-5 until complete | Tests fail → undo | | 7 | Final verification | - | | 8 | Mandatory review | Review fails | | 9 | Close Task | - |
Core cycle: Change → Test → Commit (repeat)
If tests fail: STOP. Undo change. Make smaller change. Try again.
Don't use for:
gambit:executing-plans)gambit:debugging)gambit:test-driven-development)BEFORE any refactoring:
Task
subagent_type: "general-purpose"
description: "Run test suite"
prompt: "Run: [test command for this project]. Report pass/fail counts and any failures."
ALL tests must pass.
Failing tests mean you can't detect if refactoring breaks things.
TaskCreate
subject: "Refactor: [specific goal]"
description: |
## Goal
[What structure change you're making]
## Why
- [Reason: duplication, complexity, etc.]
## Approach
1. [Transformation 1]
2. [Transformation 2]
3. [Transformation 3]
## Success Criteria
- [ ] All existing tests still pass
- [ ] No behavior changes
- [ ] Code is cleaner/simpler
- [ ] Each commit is small and safe
activeForm: "Refactoring code"
Then: TaskUpdate taskId: "[id]" status: "in_progress"
The smallest transformation that compiles.
Examples of "small":
NOT small:
The test: If you can't describe the change in one sentence, it's too big. Split it.
After EVERY small change:
Task
subagent_type: "general-purpose"
description: "Run test suite"
prompt: "Run: [test command for this project]. Report pass/fail counts and any failures."
ALL tests must still pass.
If tests fail:
# Undo the change
git checkout -- .
Then:
Never proceed with failing tests.
Commit each safe transformation:
git add [changed files]
git commit -m "refactor: [one-sentence description of transformation]"
Why commit so often:
Repeat steps 3-5 for each small transformation. Track progress:
1. Extract validateEmail() → test → commit ✓
2. Extract validateName() → test → commit ✓
3. Move validations to new file → test → commit ✓
Pattern: change → test → commit (repeat)
After all transformations complete:
Task
subagent_type: "general-purpose"
description: "Run full test suite and linter"
prompt: "Run: [test command] && [lint command]. Report all results."
Checklist:
Review the changes:
git log --oneline | head -10
git diff [start-sha]..HEAD
After final verification passes, invoke gambit:review:
Skill skill="gambit:review"
Do not skip review for "simple" refactorings. Do not tell the user to run it manually — invoke it and follow its process immediately. Review validates the refactoring didn't introduce regressions, security issues, or quality problems.
After review passes:
TaskUpdate
taskId: "[task-id]"
description: |
## Completed
- [List of transformations made]
- All tests pass (verified)
- No behavior changes
- N small transformations, each tested
- Review: APPROVED
status: "completed"
Rule: If you need to change test assertions (not just add tests), you're rewriting, not refactoring.
If the user explicitly asks to batch changes or skip steps:
All of these mean: STOP. Return to the change→test→commit cycle.
| Excuse | Reality | |--------|---------| | "Small refactoring, don't need tests between steps" | Small changes can break things. Test every step. | | "I'll test at the end" | Can't identify which change broke what | | "Tests are slow, I'll run once at the end" | Slow tests → run targeted tests between steps | | "Just fixing bugs while refactoring" | Bug fixes = behavior changes = not refactoring | | "Easier to do all at once" | Easier to debug one change than ten | | "Tests will fail temporarily but I'll fix them" | Tests must stay green. No exceptions. | | "While I'm here, I'll also..." | Scope creep during refactoring = disaster | | "Just adding docstrings/comments/type hints" | Separate commit. Cosmetic ≠ structural. | | "User said to batch it" | Explain risk, recommend incremental, separate structural from cosmetic |
Before marking refactoring complete:
gambit:review invoked and passedCan't check all boxes? Return to process and fix before closing Task.
See REFERENCE.md for detailed good/bad examples including:
This skill requires:
gambit:test-driven-development to write tests first if none exist)gambit:verification (for final verification)subagent_type: "general-purpose") for running testsCalled by:
Calls:
gambit:test-driven-development (if tests need writing first)gambit:verification (final check)gambit:review (mandatory, after final verification passes)Workflow:
Want to improve code structure
↓
Step 1: Verify tests pass
↓
Step 2: Create Task
↓
Steps 3-6: Change → Test → Commit (repeat)
↓
Step 7: Final verification
↓
Step 8: Mandatory review
↓
Step 9: Close Task
testing
Use when creating a new skill, modifying an existing skill, writing or rewriting a SKILL.md file, auditing a skill's description for discoverability, or when user mentions "create a skill", "write a skill", "new skill", "modify skill", "improve skill", "edit the skill".
development
Use before any completion claim, success statement, or marking a task done. Triggers when about to say "Great!", "Perfect!", "Done", "All set", "Ready to commit", before creating a PR, before moving to the next task, or when code has changed since the last test run.
data-ai
Use when starting an isolated feature branch, when working on multiple features simultaneously, when experimenting without affecting the main workspace, or when a clean workspace is needed before beginning implementation. User phrases like "start a new branch", "set up a worktree", "isolated workspace", "work on feature X separately".
development
Use at the start of every session before any response or action. Also invoke whenever uncertain which gambit skill applies, when about to implement / debug / refactor / test / plan / brainstorm, or when a user request could match any gambit skill even at 1% probability.