agentleFS
roronoazoroshao369roronoazoroshao369/vibe-coding-os/skills/core/safe-refactor/SKILL.md

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

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.
  • JuliusBrusseeSkill

    verify-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

  • JuliusBrusseeSkill

    investigate-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

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
  1. Distinctive:

    Do NOT use when:

    line 26
  2. Distinctive:

    Not updating the registry/index — refactored paths become orphans.

    line 61

“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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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-skill or 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

  1. Characterize — capture current behavior with tests, traces, or property assertions. If no tests exist, write characterization tests FIRST. Record observable outputs for representative inputs.
  2. 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.
  3. 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.
  4. 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.
  5. 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:all green.
  • 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 in registry/commands.json, template paths in registry/templates.json all reflect the refactor.
  • npm run validate:traceability -- --strict-new passes (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.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.