AGENT SKILL

refactor

Change the structure of code without changing its behavior. Use when the user asks to refactor, restructure, clean up, simplify, or deepen code, or asks what in a codebase is worth refactoring.

License 许可
MIT
所属仓库
dwin/skills
最近更新
2026-11-14
Stars
★ 0
Forks
0

01 前言 Front Matter

name
refactor
description
Change the structure of code without changing its behavior. Use when the user asks to refactor, restructure, clean up, simplify, or deepen code, or asks what in a codebase is worth refactoring.

02 资源文件 3

03 SKILL.md 正文

Refactor

A refactor changes structure and leaves behavior as it was. A change that adds behavior is a feature. A change that corrects behavior is a bug fix. When the work turns up either one, split it out and finish the structural change first.

Use the design skill's vocabulary for structure and CONTEXT.md for the domain's names.

Choose the target

  • When the user asked what is worth refactoring, in the whole codebase or in one part of it, read FIND-CANDIDATES.md. It scans the code, shows the candidates in an HTML report, and interviews the user about the one they pick. That survey ends with a plan. Continue below only when the user asks for the change to be made.
  • When the user asked for a refactor of code they named, start at Pin the behavior.

Pin the behavior

Before any structure moves, capture what the code does now in a check you can rerun: a characterization test, a snapshot, or a script that records the outputs for fixed inputs. A passing type check or lint run is not a pin.

Write the pin at the module's interface, so it survives the restructuring. Run it on the unchanged code and see it pass.

Name the target shape

State what the modules, types, and call graph would be if the code were written today, knowing everything known now. Design that shape as a whole, then deliver it in steps.

The new shape must remove something: a branch, an invalid state, a layer, a duplicate. A reshape that only adds indirection is not worth making, and code whose shape is already clear and local stays as it is.

When the target changes a module's interface, sketch at least two shapes with design before moving anything.

Move in small steps

  1. Subtract first. Delete dead code, collapse a wrapper that has one caller, and remove a check that repeats an earlier one.
  2. Make one structural move, then run the pin. Continue only when it passes.
  3. After a rename, search for the old name in strings, comments, docs, and configuration. A rename tool misses those.
  4. Update every description of the old shape: types, docs, examples, and the comments that explain why.

Change an interface

By default, keep callers working through the change, as engineering-principles requires: add the new interface, move callers to it, and remove the old one once nothing uses it. Every step passes the pin.

Users of the old interface include what a search of this repository does not show: stored data, messages already sent, callers in other repositories, and older instances still running during a staged deployment. While any of them depends on the old interface, keep it, or ask the user to approve the breaking change.

Option: migrate every caller and delete the old interface in one change. This leaves no shim and no period with two ways to do one thing. Use it only when the user asks for it, or agrees after you propose it. Propose it when all of these hold:

  • every caller is in this repository;
  • nothing stored, sent between services, or deployed in stages depends on the old shape;
  • the old interface has no reason to remain.

With this option, change the tests to assert the new contract, and delete the tests that only protected details of the old implementation.

Prove it and decide

  • Run the pin on the final tree. For a larger reshape, also compare old and new outputs with a script over recorded inputs.
  • Keep the change only when the code is easier to read or to change somewhere. When it is not, revert it and say so.
  • Commit the subtraction first, then the reshape, then any cleanup that follows. Each commit passes the pin.

Report

State the structure that changed, the pin and its result, what was removed, what was reverted, and any feature or bug fix that was split out.

04 同仓库更多 Skills

dwin
dwin/skills

visual-explainer

Render interactive visual explanations, architecture simulators, option tradeoff matrices, permission or blast-radius maps, and phased rollout cards on the host's native visual surface. Use when answering a question that compares three or more options across multiple dimensions, traces a multi-gate request or state flow, maps permissions or blast radius, lays out a multi-phase architecture plan, or when the user asks to visualize or sends `viz`.

★ 0
dwin
dwin/skills

writing-for-agents

Writing documents for agents. Use when creating or editing skills, or modifying AGENTS.md or CLAUDE.md.

★ 0
dwin
dwin/skills

wizard

Generate an interactive bash wizard that walks a human through steps only they can perform. Use when provisioning infrastructure, setting up credentials or CI secrets, walking an unfamiliar third-party dashboard, or running a one-off migration or cutover. Don't invoke this for steps the agent can perform itself.

★ 0
dwin
dwin/skills

why

Find out why code is the way it is from its history, reviews, tickets, docs, and other records, and say how sure each answer is. Use when the user asks why something is built, written, or behaves a certain way, why a choice was made, or where a number or limit came from.

★ 0
dwin
dwin/skills

whats-next

Match a task to the skill, flow, or playbook that fits it. Use when the user asks what to do next or which workflow applies, when a task fits no other skill, and for these kinds of work, which have playbooks here - proving a property of concurrent or stateful code with a model checker or verifier, matching a UI pixel for pixel, and diagnosing a live process or a captured profile, trace, or heap snapshot.

★ 0
dwin
dwin/skills

wayfinder

Plan a huge chunk of work — more than one agent session can hold — as a shared map of decision tickets on your issue tracker, and resolve them one at a time until the way to the destination is clear.

★ 0