360 Secure SkillHub
360 安全技能中心

refactor

针对指定代码,先明确必须保持的行为并建立测试安全网,再做最小结构改动,分步复跑测试,最后报告改动、行为保持情况、验证结果和剩余技术债。

分类:开发
ryanlhuber
热度 0.00
最近更新 2026-10-27 11:38:59

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add ryanlhuber/depot/refactor
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
refactor
description
Improves code structure while keeping behavior the same, in small steps verified by tests. Use when code is hard to change, understand, or test. Not for behavior changes (`build-feature`) or defects (`fix-bug`).

Refactor

Purpose

Reduce the cost of future change in specific code without changing what it does.

When to use

  • Use when: named code is hard to change, understand, or test and the outcome wanted is structural.
  • Do not use when: behavior is meant to change (use build-feature), the problem is a defect (use fix-bug), or the structure question is system-wide (use review-architecture).

Inputs

  • Required: the target code and the maintainability outcome wanted.
  • Optional: existing tests, constraints such as frozen public contracts, and verification commands.
  • Assumptions: if tests are thin, the safety net must be built before the structure changes.

Process

  1. State the behavior that must remain stable, including public contracts.
  2. Establish a safety net. Run existing tests; where coverage of the target is thin, add tests that pin current behavior first and say so.
  3. Pick the smallest structural improvement with a real payoff.
  4. Change in small steps. Re-run the safety net after each step and stop at the first difference.
  5. Keep public contracts stable unless the task explicitly changes them.
  6. Report what changed, what stayed the same, and the debt that remains.

Rules and heuristics

  • Required: behavior preservation is the default. A needed behavior change stops the refactor and is reported as separate work.
  • Required: no new abstraction without a stated payoff. Prefer readable code over clever code.
  • Required: never report tests as passing unless they ran.
  • Recommended: keep structural and behavioral changes in separate commits or clearly separated steps so a reviewer can tell them apart.

Checks

  • The safety net passed before the first change and after the last.
  • Public contracts are unchanged, or each change is called out.
  • No behavior change is hidden inside the diff.

Output

Return: the refactor, behavior-preservation notes, verification results, and remaining debt.

Completion criteria

  • The target is structurally better by the stated outcome and the safety net is green.
  • A reviewer can confirm from the report that behavior did not change.