agentleFS
yaohuangguanyaohuangguan/agent-playbook/skills/refactor-safely/SKILL.md

refactor-safely

Skill1 starsChanged 2 days agoMITView on GitHub

Restructure, rename, split or move code without changing behaviour, and prove that nothing changed. Use this for any refactor, cleanup, file move, module split or dead code removal, including when it is only one step inside a larger task.

We split this skill into the rules it gives an agent

6 rules. 0 are common, 5 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 15 edited versions we read keep these.

  • 5

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Record the baseline: Run the tests, type check and build, and save the results.

    line 18
  2. Very specific:

    Check that tests protect this code: If the code has no tests, write tests that record its current behaviour first, using write-tests.

    line 21
  3. Distinctive:

    Move in small mechanical steps: Use the editor or language tools to rename and move where you can.

    line 24
  4. Distinctive:

    Update everything that names the old path: Search for it in imports, tests, docs, build config, CI, scripts, and strings built at runtime.

    line 30
  5. Distinctive:

    Prove that behaviour is unchanged: Use the strongest check you can:

    line 39
  6. Distinctive:

    Run the type checker with unused-name checks on.

    line 44

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

Refactor safely

A refactor promises that behaviour stays the same, so it needs evidence. Mixing behaviour changes into a move makes that impossible to review.

When to use

The goal is better structure with the same behaviour. If behaviour must change too, do it in a separate commit.

Steps

  1. Record the baseline. Run the tests, type check and build, and save the results. Stop and fix the baseline first if it is failing in this area.
  2. Check that tests protect this code. If the code has no tests, write tests that record its current behaviour first, using write-tests.
  3. Move in small mechanical steps. Use the editor or language tools to rename and move where you can. Prefer git mv, so that history follows the file.
  4. Commit structure apart from behaviour. A reviewer should be able to read a move commit as a move.
  5. Update everything that names the old path. Search for it in imports, tests, docs, build config, CI, scripts, and strings built at runtime. Strings are easy to miss, because neither the compiler nor search-and-replace on imports finds them.
  6. Check that tests still test something. A test that looks through a folder or matches a path can pass with nothing to check after a move. Confirm that test counts did not fall, and that each path-based check still finds its files.
  7. Prove that behaviour is unchanged. Use the strongest check you can:

    • Compare the built output before and after, with paths normalized.
    • Compare the test count and results with the baseline.
    • Compare the public API or exported names.
    • Run the type checker with unused-name checks on.
  8. Finish. Follow verify-and-report.

Stop and ask when

  • You find a bug. Mention it, and do not fix it inside the refactor.
  • A clean move needs a public API change.
  • The change is too large to review, and it could be split.

Common mistakes

Tempting moveWhy it failsDo this instead
Fix a bug during the moveReviewers cannot tell what changedUse a separate commit
Reformat everythingBuries the real changeKeep formatting out
Say tests still passFewer tests may be runningCompare the counts
Delete code that looks unusedIt may be reached dynamicallySearch for it, including strings
Add a new abstractionThat is a design change, not a refactorKeep to the agreed goal

Report

Structure changed: <what moved>
Behaviour changed: none
Equivalence proof: <what you compared, and the result>
Test count: <before> -> <after>
References updated: <imports, docs, CI, scripts>

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.