agentleFS
Kuan-AI-comKuan-AI-com/agent-skills/skills/refactor-safely/SKILL.md

refactor-safely

Skill0 starsChanged 6 days agoMITView on GitHub

Refactor code without changing what it does (rename, extract, split files, remove duplication, simplify). Use when the user says "clean this up", "refactor", "split this file", "simplify this function", "rename X everywhere", or "remove this duplication".

21 other projects use a version of this.

Adopted by

Most recently taken up by

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

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

  • yaohuangguanSkill

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

  • SauravSrivastavSkill

    debug-failure

    Diagnose a failing test, command, or runtime behavior using evidence and produce a minimal verified fix.

    Also in 3 projects that use this

  • NVIDIASkill

    debug-issue

    Use when the user asks to debug, investigate, or fix a GitHub issue or supplies a repository bug report.

    Also in 5 projects that use this

  • ficaviolaodorata520Skill

    security-pass

    Review an MCP server for common security gaps: LLM-facing surfaces as injection vector (tools, resources, prompts, descriptions), scope blast radius, destructive ops without consent, upstream auth shape, input sinks (URL / path / roots / shell / sampling / schema strictness / ReDoS), tenant isolation, leakage through errors and telemetry, unbounded resources, and HTTP-mode deployment surface. Use before a release, after a batch of handler changes, or when the user asks for a security review, audit, or hardening pass. Produces grouped findings and a numbered options list.

    Also in 3 projects that use this

  • Codagent-AISkill

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

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

    If the code you will touch has no tests, write characterization tests that pin its current output for typical and edge inputs, including …

    line 18
  2. Distinctive:

    Before renaming or moving anything, search for it in all forms:

    line 22
  3. Distinctive:

    After each step, run the fast checks (affected tests and the type checker).

    line 30
  4. Distinctive:

    Prefer tooling for renames and moves (IDE refactor, git mv) over hand edits so history and references follow.

    line 31
  5. Distinctive:

    If the user is using git, suggest a commit per meaningful step so each one can be reverted alone.

    line 32
  6. Distinctive:

    Do not fix bugs you find mid-refactor.

    line 36
  7. Distinctive:

    Do not reformat unrelated code or change style across files you only passed through.

    line 37
  8. Distinctive:

    If the refactor grows past what was agreed (more files, a new abstraction), stop and check with the user.

    line 38
  9. Distinctive:

    Run the full baseline again.

    line 42
  10. Very specific:

    Read your own final diff once, looking for accidental logic changes: flipped conditions, changed defaults, reordered side effects, dropped …

    line 43

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

Safe refactoring

A refactor changes structure, not behavior. Behavior includes return values, thrown errors and their messages, side effects and their order, public names and types, and log or event output that other systems read.

1. Scope it

State in one or two sentences what will change and what will not. If the user's request implies a behavior change ("and make it faster", "and fix the edge case"), split that into a separate step after the refactor.

2. Baseline

  • Find and run the checks the project has: tests, type checker (tsc --noEmit, mypy, go vet), linter. Get the commands from package.json, Makefile, CI config.
  • Record what passes now. Pre-existing failures are not yours to fix; note them.
  • If the code you will touch has no tests, write characterization tests that pin its current output for typical and edge inputs, including current odd behavior. Do not refactor untested code you cannot run.

3. Find every usage

Before renaming or moving anything, search for it in all forms:

  • static references (git grep -n, editor "find references")
  • dynamic ones: string keys, getattr/reflection, dependency-injection names, route names, template and config files, serialized data, SQL column names
  • external consumers: exported package API, HTTP or event contracts, other repos. If it is public, keep the old name as a deprecated alias or ask first.

4. Change in small steps

  • One mechanical move per step: rename, extract function, inline, move file, replace conditional with lookup.
  • After each step, run the fast checks (affected tests and the type checker). If something breaks, fix or revert that step before continuing.
  • Prefer tooling for renames and moves (IDE refactor, git mv) over hand edits so history and references follow.
  • If the user is using git, suggest a commit per meaningful step so each one can be reverted alone.

5. Stay in scope

  • Do not fix bugs you find mid-refactor; list them and fix later as separate changes (a bug fix changes behavior and the tests should show it).
  • Do not reformat unrelated code or change style across files you only passed through.
  • If the refactor grows past what was agreed (more files, a new abstraction), stop and check with the user.

6. Verify and report

  • Run the full baseline again; results must match.
  • Read your own final diff once, looking for accidental logic changes: flipped conditions, changed defaults, reordered side effects, dropped await, changed error types.
  • Report: what moved where, why the new shape is simpler, the checks you ran with results, and any bugs or follow-ups you noticed but did not touch.

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.