refactor-safely
Skill1 starsChanged 12 days agoMITView on GitHub
Change the structure of existing code without changing its behaviour, in verified steps small enough to abandon. Covers pinning current behaviour with characterisation tests before touching anything, sequencing small reversible transformations, keeping the build green throughout, separating refactoring from behaviour change, and extracting a service from a monolith by strangler fig. Use when improving structure, paying down design debt, preparing code for a feature, splitting a module or service, or when the user says 'refactor', 'clean this up', 'restructure', 'extract', 'strangle', or 'reduce coupling'. This workflow preserves behaviour — to add or change behaviour use implement-service, to audit without changing use review-service.
23 other projects use a version of this.
Adopted by
Most recently taken up by
- JunnDung/cursor-agent-starter-kittheir own version, first seen Nov 29, 2026
- hamakyo/okf-skillstheir own version, first seen Nov 21, 2026
- Kuan-AI-com/agent-skillstheir own version, first seen Nov 15, 2026
- asymmetric-effort/convocatetheir own version, first seen Nov 15, 2026
- tirth8205/code-review-graphtheir own version, first seen Nov 11, 2026
- rumotion/ai-agent-project-templatetheir own version, first seen Nov 1, 2026
Used with
Projects with it also use audit-test-suite, change-data-safely 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.
yaohuangguanSkillaudit-test-suite
Find where an existing test suite is weak and decide what to test next, ranked by risk. Use this when asked to add missing tests, find gaps, raise coverage, assess test quality or see which areas are thin, even if the request only mentions a coverage percentage.
Also in 3 projects that use this
yaohuangguanSkillchange-data-safely
Change a database schema or stored data without breaking running code or losing data. Use this for any migration, new or changed column, backfill, bulk update, delete, index, or change to a stored format, including in small projects. Data mistakes are often impossible to undo.
Also in 3 projects that use this
NVIDIASkilldebug-issue
Use when the user asks to debug, investigate, or fix a GitHub issue or supplies a repository bug report.
Also in 6 projects that use this
MicrosoftSkillfix-flaky-test
Reproduces and fixes flaky or quarantined tests. Tries local reproduction first (fast), then falls back to CI reproduce workflow (reproduce-flaky-tests.yml). Use this when asked to investigate, reproduce, debug, or fix a flaky test, a quarantined test, or an intermittently failing test.
Also in 3 projects that use this
SauravSrivastavSkilldebug-failure
Diagnose a failing test, command, or runtime behavior using evidence and produce a minimal verified fix.
Also in 3 projects that use this
Codagent-AISkillimplement-change
Implements an approved change in one continuous session, with tiered test runs. Use when asked to implement a change from its approved definition, or to invoke codagent:implement-change.
Also in 5 projects that use this
- +6 more from yaohuangguan
Other versions23 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 23
- tirth8205/code-review-graph★ 33ksame name
- techygarg/lattice★ 218same name
- VelimirMueller/ai-development-skills★ 7same name
- VelimirMueller/claude-skills★ 7same name
- yaohuangguan/remote-arc★ 5same name
- asymmetric-effort/convocate★ 4same name
- Arasz/ai-badger★ 3same name
- hamakyo/okf-skills★ 3same name
- imMamdouhaboammar/get-things-done★ 2same name
- contextosai/skills★ 1same name
13 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 repository22 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- api-contractsSkill
- api-protocolsSkill
- clean-architectureSkill
- collaborative-judgmentSkill
- config-and-dependenciesSkill
- contract-testingSkill
- cqrs-and-consistencySkill
- data-accessSkill
- design-firstSkill
- design-serviceSkill
- domain-modellingSkill
- idempotency-and-immutabilitySkill
- implement-serviceSkill
- infrastructure-adaptersSkill
- resilience-patternsSkill
- review-serviceSkill
- secure-serviceSkill
- service-boundariesSkill
- stack-profileSkill
- test-strategySkill
The file
We split this skill into the rules it gives an agent
10 rules. 0 are common, 9 are distinctive, and 1 is very specific to this project.
0
Common
Most of the 21 edited versions we read keep these.
9
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
- Distinctive: line 36
Before any change, state three things and get agreement:
- Distinctive: line 44
If you cannot fill all three, STOP.
- Very specific: line 56
Assess the coverage of the code you are about to move: Read the tests; do not trust a percentage (test-strategy).
- Distinctive: line 66
STOP: if the code cannot be characterised at any level.
- Distinctive: line 83
Never mix behaviour change with structure change: If a fix or improvement is needed, finish the refactor, then make it as a separate change.
- Distinctive: line 106
Add an architecture test that fails the build on a boundary violation.
- Distinctive: line 114
Remove the local implementation: only after the remote one has carried full traffic long enough to trust.
- Distinctive: line 125
Confirm the characterisation tests still pass unchanged: If any needed editing, behaviour changed — say so explicitly rather than quietly …
- Distinctive: line 128
Check the target was actually reached: If the structure is only partly improved, say which part remains and why, rather than implying it is …
- Distinctive: line 137
Revert, do not debug forward: when a step breaks tests.
“Common” is measured: kept by at least 6 in 10 of the 21 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
Refactor Safely
Refactoring is behaviour-preserving by definition. The moment behaviour changes, this stops being a refactor and becomes a change that needs a design and a review.
Two rules make the difference between a refactor and an outage: pin the behaviour before you touch it, and keep every step small enough to throw away. A large refactor that "works" but cannot be reverted is a rewrite wearing a safer name.
Required Skills
Read and apply:
test-strategy— characterisation tests, and the right level to pin behaviour at. (always)clean-architecture— the target structure and why it is better. (always)collaborative-judgment— the scope, the target, and anything that turns out to be a behaviour change. (always)service-boundaries— module or service extraction. (conditional: moving code across a boundary)domain-modelling— restructuring domain types and aggregate boundaries. (conditional)data-access— schema changes, migrations, query restructuring. (conditional)infrastructure-adapters— introducing or reshaping a port to create a seam. (conditional)api-contracts— anything a consumer can observe. (conditional: a public surface is involved)resilience-patterns— moving code across a process boundary turns a call into a network hop. (conditional: extraction)
Workflow
Step 1 — Name the target and the reason
Before any change, state three things and get agreement:
- What is wrong now — the concrete pain, not an aesthetic judgment. "Adding a payment method requires edits in six files" is a reason. "This does not follow SOLID" is not.
- What it should look like — the target structure, in terms of
clean-architecture. - What will be measurably better — fewer files touched per change, a testable unit, a boundary the compiler enforces.
If you cannot fill all three, STOP. Refactoring without a named force is churn: it produces a large diff, a review burden, and a merge conflict for everyone else, in exchange for nothing.
Agree the scope boundary explicitly — which files may change, and which are out of bounds. A refactor that spreads is one nobody can review.
Step 2 — Pin the current behaviour
You cannot preserve behaviour you have not captured.
- Run the existing tests. They must be green before you start. Failing tests → fix or quarantine them first; you cannot tell your breakage from theirs otherwise.
- Assess the coverage of the code you are about to move. Read the tests; do not trust a
percentage (
test-strategy). - Write characterisation tests for what is not covered. These pin what the code does, not
what it should do:
- Drive from the outside — the level where behaviour is observable without knowing the structure you are about to change. Usually a component test.
- Include the ugly cases: nulls, empty collections, boundary values, error paths.
- If a characterisation test reveals a bug, do not fix it now. Pin the current behaviour, note the bug, and raise it separately. Fixing behaviour during a refactor makes the diff impossible to reason about.
- STOP if the code cannot be characterised at any level. That is itself the finding — say so, and propose either adding a seam first or leaving it alone.
Step 3 — Transform in small steps
Each step is one transformation, verified, and independently revertible.
For every step:
- Make one change — rename, extract, inline, move, introduce a parameter object, replace a conditional with polymorphism.
- Run the tests. Green → continue. Red → revert the step; do not debug forward. A revert costs seconds; debugging a half-applied refactor costs an afternoon.
- Commit at each green point, so any step can be abandoned without losing the rest.
Rules that hold throughout:
- Never mix behaviour change with structure change. If a fix or improvement is needed, finish the refactor, then make it as a separate change. If it cannot wait, stop the refactor, make the behaviour change with its own tests, then resume.
- Keep the build green between steps. Long-lived broken states are how refactors get abandoned half-done, leaving the codebase worse than before.
- Prefer parallel-change (expand → migrate → contract) for anything with callers: add the new shape, move callers over one at a time, remove the old one. Each stage ships independently.
- Use the tooling. IDE and language-server refactorings are safer than hand editing for renames and moves — but re-read the diff; automated moves miss reflection, strings, configuration, and documentation.
- Update tests only for structure. A test whose assertions must change means behaviour changed — stop and reconsider.
Apply the clean-architecture checklist after each significant step, and surface any
Ambiguity Signals through collaborative-judgment rather than deciding the target unilaterally.
Step 4 — Extraction (module or service)
Moving code across a process boundary is not a refactor of one codebase; it is a change to the
system's failure model. Follow the strangler-fig sequence in service-boundaries, and stop at each
stage:
- Module first. Move the code inside the existing deployable, behind an explicit interface. Add an architecture test that fails the build on a boundary violation. Ship this. Most of the risk lives here, and it is still cheap to undo.
- Separate the data. Route every access through the interface; nothing else touches those tables. Ship this. If this proves impossible, the boundary was wrong — that is the cheapest possible moment to learn it.
- Add the remote implementation of the same interface, called by nothing yet.
- Shift traffic behind a flag (
config-and-dependencies), comparing results, with an instant rollback. - Remove the local implementation only after the remote one has carried full traffic long enough to trust.
At step 3, every call that was a method call becomes a network call. Apply resilience-patterns in
full — timeout, retry policy, breaker, fallback — and api-contracts to the interface, which is
now a published contract. A "pure refactor" that turns an in-process call into an unguarded remote
one has introduced a new outage mode.
Step 5 — Verify and close
- Run the whole suite, not only the tests near the change.
- Confirm the characterisation tests still pass unchanged. If any needed editing, behaviour changed — say so explicitly rather than quietly adjusting the test.
- Re-read the diff as a reviewer. Anything outside the agreed scope → revert it.
- Check the target was actually reached. If the structure is only partly improved, say which part remains and why, rather than implying it is done.
- Summarise: what moved, what did not change, which steps were taken, what is deliberately left, and any bug found and deferred during characterisation.
Rules
- Behaviour is preserved, always. A behaviour change makes this a different workflow.
- Never refactor on red. Green before, green between, green after.
- Revert, do not debug forward when a step breaks tests.
- A bug found is a bug reported, not a bug fixed mid-refactor.
- Stay inside the agreed scope. Improvements you notice elsewhere go in the close-out list.
- No named force, no refactor. Say so plainly rather than producing churn.
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.

