Agent Skillsgnomeria/usbtree › refactor

refactor

GitHub

指导行为不变的重构流程,强调安全网、小步验证及无功能变更。适用于清理代码、提取函数、重命名或拆分模块等场景。

.agents/skills/refactor/SKILL.md gnomeria/usbtree

触发场景

重构代码结构 清理重复代码 提取或重命名函数/模块 使代码更易于测试

安装

npx skills add gnomeria/usbtree --skill refactor -g -y
更多选项

非标准路径

npx skills add https://github.com/gnomeria/usbtree/tree/main/.agents/skills/refactor -g -y

不安装直接使用

npx skills use gnomeria/usbtree@refactor

指定 Agent (Claude Code)

npx skills add gnomeria/usbtree --skill refactor -a claude-code -g -y

安装 repo 全部 skill

npx skills add gnomeria/usbtree --all -g -y

预览 repo 内 skill

npx skills add gnomeria/usbtree --list

SKILL.md

Frontmatter
{
    "name": "refactor",
    "description": "Behavior-preserving restructuring done safely — test net first, small verified steps, no mixed-in feature changes. Use when the user says \"refactor\", \"clean up\", \"restructure\", \"extract\", \"rename\", \"split this file\/function\", \"reduce duplication\", or \"make this testable\"."
}

Refactor

Structure changes, behavior never. Behavior changes = feature or bug fix — separate change.

Step 0 — Worth it?

  • Purpose required: enables concrete upcoming change, kills duplication that bit someone, or makes untestable testable. "Could be cleaner" alone = say so, stop.
  • Name target shape in one or two sentences before touching code. No target = wandering diff.

Step 1 — Safety net

  • Baseline: cargo test green, cargo clippy -- -D warnings clean. Note it.
  • Uncovered code: characterization tests first — pin what it does now (odd behavior included). Extra check: --demo --dump output before vs after must be byte-identical.
  • Working tree clean before starting; git checkout . is the escape hatch.

Step 2 — Small steps, verify each

  • One named refactoring at a time: extract fn, inline, rename, move, split module. Finish, verify, next.
  • Verify each step: cargo test + clippy. Red → undo last step only. Never push forward on red planning to "fix at the end".
  • Rename via rust-analyzer / cargo tooling where available — tool can't miss call site, regex can.
  • Commit-sized steps. Long refactor → stop at green checkpoints. 40-file big-bang diff "should work" = failure.

Step 3 — Hard rules

  • No behavior changes. Bug found mid-refactor → note it, checkpoint, fix separately (see debug skill).
  • No fmt churn. Repo bans blind cargo fmt — hand-aligned blocks (theme, match tables). Diff only what the refactor touches.
  • Respect // ponytail: comments — known deliberate tradeoffs. Don't "clean up" without reason.
  • No test rewrites to fit refactor. Tests fail after "behavior-preserving" change → change wasn't behavior-preserving. Exception: tests coupled to now-gone internals; update minimally, say why.
  • No scope creep. Step 0 shape is boundary. New ideas → list at end, don't do.

Step 4 — Finish

  • Full cargo test + clippy green. --demo --dump unchanged. Dead code deleted, not commented out.
  • Report: shape achieved, steps taken, things found-not-done.

Direction

  • Extract functions over introducing traits/structs; trait only at genuine seam (multiple impls exist or test needs one).
  • Rule of three: two similar blocks may stand; third occurrence earns abstraction — right shape now visible.
  • Don't abstract for testability when passing plain value/fn does it.

版本历史

  • 171c884 当前 2026-07-19 10:05

    将语言特定检查从Go/TS更新为Rust (cargo test/clippy),增加demo输出一致性校验,细化无行为变更规则,移除旧语言工具引用。

  • 9a316d8 2026-07-11 17:01

同 Skill 集合

.agents/skills/ci-github-actions/SKILL.md
.agents/skills/debug/SKILL.md
.agents/skills/knowledge-graph/SKILL.md
.agents/skills/rust-patterns/SKILL.md
.agents/skills/write-tests/SKILL.md

元信息

文件数
0
版本
7573ea3
Hash
e3085a8e
收录时间
2026-07-11 17:01

首页 - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-04 02:40
浙ICP备14020137号-1 $访客地图$