restructure
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-10-22
- Stars
- ★ 1
- Forks
- 0
02 资源文件 2
- 参考文档references/seams.md6.8 KB
- 参考文档references/smells.md6.6 KB
03 SKILL.md 正文
Restructure without changing behavior
A restructure that changes behavior is a rewrite with no tests to catch it. Keep the two apart: structure now, behavior in a separate change.
The rule that makes this safe
One move per commit. Tests green before and after every move.
If the suite is red when you start, stop and say so. Restructuring on a red suite means you cannot tell your breakage from the breakage that was already there. Offer to fix the suite first.
If there is no suite covering what you are about to move, write a characterization test first: call the code, record what it currently returns, assert that. It does not need to assert correct behavior, only current behavior. That test is what makes the move reversible.
Extract by seam, not by line count
The wrong question is "which 200 lines do I move out of this 900-line file". The right question is "where does this code stop being about the domain and start being about the framework".
A seam is a boundary where the logic no longer needs its surroundings: no request object, no ORM session, no widget, no clock, no filesystem. Code on the pure side of a seam can move out and be tested directly. Code that needs its surroundings stays.
To find the seam, read the class or module and mark every line that touches the outside world: IO, network, database, UI, time, randomness, environment. What is left unmarked is one or more cohesive units. Those are the extractions. Splitting a file down the middle by line count produces two files that both still need everything.
See references/seams.md for the extraction recipes, each with the before and after shape.
Find the real problem
Run the detection pass before proposing anything. references/smells.md has the full catalog with detection commands and the fix for each. The ones that account for most real pain:
- God object: one class or module owning unrelated concerns. Count the reasons it would have to change, not its lines.
- Feature envy: a function that reads mostly another object's fields. Move the function to the data.
- Shotgun surgery: one conceptual change requires edits in many files. The concept has no home. Give it one.
- Nested conditionals: three or more levels hiding a single predicate. Name the predicate and invert to early returns.
- Long parameter lists: more than four parameters means one is missing. Pass a typed object.
- Compared string literals: a closed set of values compared as strings. Make it an enum or union type so the compiler finds the missing case.
- One setting with several owners: the same default duplicated across a config class, a client, a UI default, and an environment file. Pick one owner and document precedence at the point where the values merge.
- Dead code: an export nobody imports, a flag always true, a branch unreachable since a type narrowed. Delete it. Do not comment it out; the history holds it.
- Circular imports: two modules importing each other. Extract what they share into a third module, or invert the dependency behind an interface.
Work in this order
- Measure. Report file sizes, the detections you found, and the test coverage over the area. Three numbers, not prose.
- Propose. List the moves in dependency order, smallest first. Say which are mechanical and which need judgment. Get agreement before touching more than a handful of files.
- Move, one at a time. Each move: make it, run the tests, commit. A failure then has a blast radius of one move.
- Delete the bridge. Re-exports and shims added to avoid touching callers are the point of the restructure, not a deliverable. Update the callers and remove the shim in the same change, or the old structure survives next to the new one.
- Verify at the seam. The suite plus one real run of the thing you changed. A suite that passes against a module nobody loads at runtime proves nothing.
Do not
- Rename and move in the same commit. The diff becomes unreadable and the reviewer cannot tell a rename from a rewrite.
- Introduce an abstraction for one caller. Wait for the second.
- Reformat untouched lines. A whitespace change across a file hides the eight lines that matter.
- Fix a bug you find mid-move. Note it, finish the move, fix it in its own commit with its own test.
- Add a dependency. A restructure that needs a library is a redesign.
Report
- What you moved and the seam each move followed.
- Test state before and after, with pass, fail, and skip counts. A skipped test is a failing test nobody can see, so report the skip count every time.
- Behavior you could not prove unchanged, and why.
- Bugs found and left alone, with locations.
04 同仓库更多 Skills
triage-ci
Diagnose a failing CI run by finding the first real failure rather than the last log line, classifying it as defect, flake, infrastructure, or config drift, and fixing the cause without disabling the check. Use when asked to fix the build, fix CI, find out why CI is failing, look at a failing run, or work out what is actually wrong with a red build, and when a pipeline or GitHub Actions run fails, a CI log needs reading, or tests pass locally but fail only in CI.
test-gaps
Find the untested branches that matter, write tests that can actually fail, and report pass, fail, and skip counts honestly. Use when asked to add tests, write tests for this, improve coverage, find test gaps, check what is untested, or when a change needs test coverage before it ships.
ship-pr
Turn a finished change into a commit message and pull request derived from the actual diff, with a pre-submit checklist covering auth gates, secrets, migrations, and breaking changes. Use when asked to commit this, write a commit message, open a PR, raise a pull request, write the PR description, or get this ready to push.
root-cause
Debug by reproducing first, isolating the mechanism, then fixing the cause rather than the symptom, and keeping the reproduction as a regression test. Use when asked to debug this, fix this bug, find out why something happens, investigate a failure, or work out what is going wrong, and when a test fails intermittently or flakily, behavior differs between environments, or someone proposes raising a timeout, adding a retry, or skipping a test to make a problem go away.
review-diff
Review uncommitted or branch-local changes for defects, ranked by severity, with a concrete failure scenario behind every finding. Use when asked to review code, review a diff, review my changes, check this before I commit, look over this branch, or do a pre-PR pass. Reviews the change, not the whole repository.
perf-hunt
Find and fix the real performance bottleneck by measuring first, attacking the dominant cost, then re-measuring, with every constant derived from a number rather than guessed. Use when asked to make this faster, speed this up, optimize this, profile this, reduce latency, or fix something slow, and when an endpoint, query, page, build, or test run takes too long or memory grows without bound.