agentleFS
caiaffacaiaffa/claude-code-ultimate-engineering-system/skills/safe-refactoring/SKILL.md

safe-refactoring

Skill17 starsChanged 58 days agoMITView on GitHub

Refactor code safely through behavior preservation, protective tests, incremental change sequencing, and risk-aware migration planning.

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 1

    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:

    Hands off to: test-strategy (protective tests), code-review (review the refactor), release-commander (if affects production behavior).

    line 19

“Common” is measured: kept by at least 6 in 10 of the 3 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

Mission

Improve code structure without silently changing system behavior or increasing delivery risk.

When to use

  • Cleaning up legacy code.
  • Extracting modules or services.
  • Reducing coupling.
  • Introducing clearer abstractions.
  • Reorganizing domain boundaries.

Handoff

  • Receives from: code-review (identified tech debt) or backend-platform-engineer (planned improvement).
  • Hands off to: test-strategy (protective tests), code-review (review the refactor), release-commander (if affects production behavior).

The safe refactoring method

1. CHARACTERIZE → Write tests that capture current behavior (before changing anything)
2. IDENTIFY → What exactly needs to change? What must stay the same?
3. PLAN → Break the refactor into small, independently-mergeable steps
4. EXECUTE → One step at a time. Each step: green tests → change → green tests
5. VALIDATE → Run full test suite. Check no behavioral regression.
6. DEPLOY → Ship incrementally. Monitor for unexpected changes.

Refactor vs rewrite decision

Choose refactor whenChoose rewrite when
Core behavior is correct, structure is badCore behavior is wrong
Tests exist or can be writtenSystem is so tangled tests can't be written
Team needs to maintain it during refactorClean break is possible (new service, new module)
Incremental delivery is possibleAll-or-nothing is the only realistic path

Default to refactor. Rewrites are almost always more expensive than estimated.

Common refactoring pitfalls

  • Changing behavior accidentally while restructuring.
  • Removing "unused" code that's actually called via reflection or config.
  • Breaking error semantics (different exception types, different HTTP codes).
  • Changing timing that affects distributed flows.
  • Losing observability (spans, metrics, log lines removed).

Red flags — stop and reassess if

  • You can't write a characterization test for the current behavior.
  • The refactor touches more than 3 modules simultaneously.
  • The refactor requires coordinated changes across services.
  • There's no way to deploy the refactor incrementally.
  • You find yourself saying "while we're at it, let's also..."

Output format

  1. Current problems (what's wrong with the current structure)
  2. Preserved behavior (what must not change)
  3. Refactor strategy (numbered steps, each independently mergeable)
  4. Protective tests (what to write before starting)
  5. Risk assessment (what could go wrong, how to detect it)
  6. Migration plan (if contracts or APIs change)

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.