safe-refactor
Skill1 starsChanged 5 months agoMITView on GitHub
Provide a 5-phase protocol for refactoring existing code without breaking behavior. Covers the full lifecycle: characterize → cover → extract → migrate → cleanup. The protocol guarantees that every refactor step is reversible, testable, and produces a measurable diff. Use when touching code that has existing behavior, callers, or tests you cannot break. Use when: - A code smell is identified (long function, deep nesting, leaky abstraction, repeated logic) and the team has agreed to refactor. - An upstream API…
28 other projects use a version of this.
Adopted by
Most recently taken up by
- maximilienGilet/skillstheir own version, first seen Nov 15, 2026
- sudeepgupta90/my-ai-marketplacetheir own version, first seen Nov 8, 2026
- mjlueck-hash/cavemantheir own version, first seen Nov 8, 2026
- dhruvsaxena1998/skillstheir own version, first seen Nov 8, 2026
- MMACABRE/SKILLStheir own version, first seen Nov 8, 2026
- pahlevikun/levistacktheir own version, first seen Nov 1, 2026
Used with
Projects with it also use verify-and-stop, investigate-first and 10 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
JuliusBrusseeSkillverify-and-stop
Prove existing work meets acceptance conditions without expanding scope. Use for validation-only tasks, completion checks, focused gate runs, and last-mile proof.
Also in 8 projects that use this
JuliusBrusseeSkillinvestigate-first
Diagnose ambiguous failures before editing. Use for unknown causes, intermittent behavior, performance regressions, or investigations needing evidence-ranked hypotheses.
Also in 7 projects that use this
- +10 more from JuliusBrussee
Other versions28 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 28
- JuliusBrussee/caveman★ 120ksame name
- Terryc21/sitrep★ 50same name
- JPeetz/agent-skills★ 14same name
- yeaight7/agent-powerups★ 6same name
- dfedoryshchev/weftworks★ 5same name
- Linchi1997yeh/craftsman-suite★ 2same name
- nomdedev/agent-manager-template★ 2same name
- xingtu1996/dsh-xingtu-skills★ 2same name
- dennisrongo/claude-skills★ 1same name
- dhruvsaxena1998/skills★ 1same name
18 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository117 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- advanced-orchestrationSkill
- architect-agentSkill
- implementer-agentSkill
- quality-councilSkill
- reviewer-agentSkill
- tester-agentSkill
- writer-critic-pairSkill
- authoring-skillsSkill
- api-endpoint-qualitySkill
- async-job-qualitySkill
- auth-qualitySkill
- db-migration-qualitySkill
- frontend-state-qualitySkill
- acceptance-criteriaSkill
- adaptive-flowSkill
- adaptive-prompt-selectionSkill
- adversarial-code-reviewSkill
- architecture-decision-recordsSkill
- brainstormingSkill
- brownfield-spec-enhancementSkill
- bug-fix-lifecycleSkill
- checkpoint-validationSkill
- cicd-integrationSkill
- clarify-before-codeSkill
- claude-code-hooks-packSkill
- code-context-packSkill
- code-intelligence-reviewSkill
- context-policySkill
- context-rich-implementationSkill
- crash-proof-planningSkill
- critique-pass-protocolSkill
- dependency-aware-task-orderingSkill
- deprecation-migrationSkill
- disciplined-diagnosisSkill
- docs-authorSkill
- executing-plansSkill
- external-skillSkill
- finishing-a-development-branchSkill
- git-guardrailsSkill
- goal-driven-executionSkill
- grill-user-before-buildingSkill
- guard-bypass-protocolSkill
- improve-codebase-architectureSkill
- incremental-reviewSkill
- issue-slicingSkill
- lessons-learned-dbSkill
- loop-engineeringSkill
- model-aware-configSkill
- model-weakness-memorySkill
- multi-repo-learningSkill
- orchestration-workflowsSkill
- plan-driven-executionSkill
- plan-from-specSkill
- prd-from-contextSkill
- project-constitutionSkill
- prompt-architectureSkill
- prototype-before-commitmentSkill
- quality-engineSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. 0 are common, 2 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 26 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 26
Do NOT use when:
- Distinctive: line 61
Not updating the registry/index — refactored paths become orphans.
“Common” is measured: kept by at least 6 in 10 of the 26 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Skill: Safe Refactor
Purpose
Provide a 5-phase protocol for refactoring existing code without breaking behavior. Covers the full lifecycle: characterize → cover → extract → migrate → cleanup. The protocol guarantees that every refactor step is reversible, testable, and produces a measurable diff. Use when touching code that has existing behavior, callers, or tests you cannot break.
When to use
Use when:
- A code smell is identified (long function, deep nesting, leaky abstraction, repeated logic) and the team has agreed to refactor.
- An upstream API or contract is being changed.
- A "while we're here" rewrite threatens to merge behavioral changes with structural changes.
- Code needs to be split, renamed, moved across modules, or have its dependency direction reversed.
Do NOT use when:
- The change is a bug fix (use
bug-fix-lifecycle). - The change is a new feature (use
write-reusable-skillor feature workflow). - The behavior itself is wrong — fix the behavior first, then refactor.
Inputs
- The code to refactor (file path or diff).
- Existing test coverage (or explicit acknowledgment that coverage is missing).
- The smell or pattern being addressed (long function, deep coupling, etc.).
- The target shape (what "good" looks like).
- Rollback strategy if the refactor must be reverted.
Workflow
- Characterize — capture current behavior with tests, traces, or property assertions. If no tests exist, write characterization tests FIRST. Record observable outputs for representative inputs.
- Cover — confirm the characterization tests pass on the current code. Coverage must be ≥ 80% for the area being refactored before proceeding. If below 80%, stop and add coverage.
- Extract — perform the smallest safe change that moves toward the target shape. One rename, one extract-method, one move-class per commit. Each commit must leave tests green.
- Migrate — update callers, dependents, and contracts to use the new shape. Run the full test suite after each migration step. If any test fails, the refactor is wrong; revert and re-characterize.
- Cleanup — delete the old code path, remove dead branches, remove commented-out code. Final verification: characterization tests still pass + new tests for the new shape pass + no behavioral diff.
Outputs
- A commit chain where each commit is independently revertible.
- A diff summary: lines added, lines removed, files touched, public surface changed.
- A characterization test suite that pins behavior before and after.
- A rollback note: which commit to revert to if the refactor must be abandoned.
Failure modes
- Refactoring without characterization tests — silent behavior change ships.
- Mixing refactor with bug fixes — bug fix and structural change become inseparable; rollback becomes impossible.
- Skipping the "each commit must leave tests green" rule — debugging a multi-step refactor mid-flight is expensive.
- Confusing "the new shape is cleaner" with "the refactor is correct" — cleanliness is not a verification gate.
- Leaving dead code "temporarily" — temporary becomes permanent; cleanup must happen in the same PR.
- Not updating the registry/index — refactored paths become orphans; traceability gate fails.
Verification checklist
- Characterization tests written and passing BEFORE any structural change.
- Coverage of the refactored area is ≥ 80% before the refactor starts.
- Each refactor commit leaves
npm run validate:allgreen. - Each refactor commit leaves the characterization test suite green.
- Public surface (exported names, public methods, route paths, CLI flags) is documented in the diff.
- No behavioral diff: characterization tests pass on both old and new code.
- Registry/index updated: skill paths in
registry/skills.json, command paths inregistry/commands.json, template paths inregistry/templates.jsonall reflect the refactor. npm run validate:traceability -- --strict-newpasses (no new orphans introduced).- PR description lists: smell addressed, target shape, commits in the chain, rollback commit hash.
Related skills
skills/core/bug-fix-lifecycle/SKILL.md— for the "fix bug first" rule (refactor never carries a fix).skills/core/verification-before-done/SKILL.md— 5-axis verification includes refactor-safety axis.skills/core/quality-engine/SKILL.md— quality-engine runs after every refactor commit.templates/refactor-plan.md— refactor PR worksheet.commands/vibe-refactor.md— the canonical command to start a refactor.
Attribution
Pattern adapted from Michael Feathers' Working Effectively with Legacy Code (characterization tests) and Martin Fowler's Refactoring (commit-per-step discipline), in original wording.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

