refactor

A command for carrying out planned code restructuring, such as splitting large methods or classes, renaming code, and correcting violations of SOLID design principles. It is meant for gradual changes that keep the software working the same way.

In plain words
What is it for?
Use it to apply findings from a code review, reduce technical debt, reorganize classes and methods, or carry out a decided architecture change while preserving existing behavior.
Why use it?
It turns identified code-quality problems into small, test-checked changes instead of one risky rewrite. Tests are run after each step so failures can be caught and the last change can be undone.

Command

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/ysicing/code-pilot/refactor
Clone the repo
git clone --depth 1 https://github.com/ysicing/code-pilot
Per session 18 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 633 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00018 $0.00633
Opus 5 $0.00009 $0.00316
Sonnet 5 $0.00004 $0.00127
Haiku 4.5 $0.00002 $0.00063

Measured yesterday against content hash 8e8ed15c76bf, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactor scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

commands/refactor.md · 72 lines

What it actually says

/refactor 命令

专注于安全重构实施的执行专家,与/review和/optimize形成互补,专门负责代码结构改进的具体实施。

命令语法

/refactor <具体文件或类/方法>

核心定位

实施型命令:专门执行已识别的重构需求,而非分析阶段

  • /review → 发现问题和建议
  • /refactor → 实施具体重构
  • /optimize → 性能导向优化

适用场景

  • 基于code review结果的具体重构实施
  • 违反SOLID原则的代码修正
  • 技术债务的系统性清理
  • 已确定的架构调整执行

重构模式库

经典重构模式

  • 提取方法(Extract Method):分解长方法
  • 提取类(Extract Class):分离职责过多的类
  • 移动方法(Move Method):优化方法归属
  • 重命名(Rename):改善可读性
  • 引入参数对象:简化参数列表
  • 替换算法:优化实现方式

SOLID原则修正

  • S:分离混合职责,单一职责化
  • O:增加扩展点,支持开闭原则
  • L:修正继承关系,确保可替换性
  • I:拆分臃肿接口,实现接口隔离
  • D:引入依赖注入,实现依赖倒置

严格执行规则

前置条件

  • 所有测试必须通过
  • 有明确的重构目标(来自/review或需求分析)

执行纪律

  • 每个重构步骤后立即运行测试
  • 测试失败立即回滚到上一稳定状态
  • 一次只做一种类型的重构
  • 保持功能行为完全不变

安全措施

  • Git提交每个安全的重构步骤
  • 保持向后兼容的API
  • 渐进式重构,避免大爆炸式改动

与其他命令的协作

# 典型工作流
/review "UserService类"           # 发现问题
/refactor "UserService.authenticate方法"  # 实施具体重构
/test "UserService重构后验证"     # 验证重构效果

输出交付

  • 重构实施计划(具体步骤)
  • 逐步重构的代码变更
  • 重构前后对比说明
  • 后续监控建议
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 72 lines · 18 tokens per session scan A 8e8ed15c76bf

Subscribe to this mod's changes

refactor is a command published in the GitHub repository ysicing/code-pilot (38 stars, last pushed 5mo ago), licensed Apache-2.0. It adds 18 tokens to every session and 633 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.