Skip to content
Development
Skill

/pdlc-refactor

代码重构(保持外部行为不变,改善内部结构)

From plugin
pdlc
1838 skills2 hooks

Claude Code

$ npx -y skills add kanfu-panda/pdlc-skills --skill pdlc-refactor --agent claude-code

SKILL.md

pdlc-refactor.SKILL.md
name: pdlc-refactor
description: 代码重构(保持外部行为不变,改善内部结构)
argument-hint: "<重构目标 | 文件路径>"
allowed-tools: Read, Write, Edit, Glob, Grep, Bash
layer: 2
stage: refactor
produces: []
requires: []
next_step: pdlc-review
terminal_state: refactor_done

代码重构

<!-- @include templates/prompts/iron-law.md(已内联于下方,无需另读) --> ⛔ **IRON LAW · 不可违反的硬门禁**

以下规则为**不可协商**的执行约束:

1. **文件必须落盘**:所有带编号(功能ID / 缺陷ID)的文档,必须作为实际文件写入磁盘,不可仅在对话中输出。 2. **阶段必须落章**:每个阶段完成后必须在状态机 `docs/.pdlc-state/<feature-id>.json` 追加 history,不可跳过。 3. **测试必须存在**:进入 `/pdlc-implement` 前,对应测试必须存在且处于红灯状态。违反则中止。 4. **自检必须执行**:段二自检为强制步骤,不得以"已经很好了"为由跳过。 5. **防循环**:段三修复为单次,不递归。无法自动修复的问题记录到报告,继续往下走。 6. **状态必推进**:成功执行某 phase 后 `current_stage` 必须变更。收尾时若发现 `current_stage` 未推进,视为失败并报错,**不得静默返回**(防止外层循环拿滞后的状态空转烧额度)。唯一例外:命中人工点主动 block 时,`current_stage` 保持不变但必须写 `last_phase_result.ok=false` + `blocked_reason`。

**违反任一条 = 立即中止当前命令,输出违规详情,等待人工介入。** <!-- @include-end templates/prompts/iron-law.md -->

对指定服务或模块进行安全重构,保证行为不变。

工作流程

1. **确认测试覆盖**: 先检查现有测试是否充分,不充分则先补测试 2. **阅读设计文档**: 阅读 `docs/02_design/` 对应子目录下的相关设计文档 3. **识别坏味道**: 分析代码中的坏味道(Code Smell) 4. **制定重构计划**: 列出重构项,按风险排序 5. **逐步重构**: 每次只做一个小的重构,确保测试通过 6. **运行完整测试**: 确认没有破坏现有功能

常见重构场景

  • **提取方法/类**: 过长的方法、过大的类
  • **消除重复**: DRY 原则,提取公共逻辑
  • **简化条件**: 复杂的 if-else 链、嵌套条件
  • **改善命名**: 不清晰的变量名、方法名
  • **解耦依赖**: 降低模块间耦合度
  • **统一风格**: 对齐项目编码规范

要求

<!-- @include templates/prompts/output-language.md(已内联于下方,无需另读) --> 🌐 **Output language for generated artifacts**

All generated artifacts (PRDs, design docs, code comments, review reports, test plans, deployment manuals, changelog entries, etc.) follow this policy:

1. **Default — match the conversation language exactly**:

  • 用户用中文与 Claude 对话 → 产中文文档、中文代码注释、中文报告
  • User talks to Claude in English → produce English artifacts
  • User talks in another language → produce artifacts in that language
  • **Never silently default to a fixed language regardless of the user's input.**

2. **Explicit override always wins**: when the user specifies a language for an artifact (e.g. "write the PRD in English", "用英文写 API 设计文档", "output the deploy doc in Japanese"), use that language for that artifact, regardless of conversation language.

3. **Mixed-language requirements**: if the user wants some artifacts in one language and others in a different language (common: Chinese PRD + English API docs for partners), honour each per-artifact instruction.

4. **Uncertain**: if you cannot reliably detect the conversation language, ask once before producing the first artifact.

This policy applies to **content** (prose, comments, headings). It does **not** override technical conventions like English variable names, English git commit subjects, or English error codes when the project's conventions require them. <!-- @include-end templates/prompts/output-language.md -->

  • 重构提交信息格式: `refactor: <简要描述>`
  • 严禁在重构中混入新功能
  • 每个重构步骤都要保证测试通过
  • 更新 CHANGELOG.md:条目格式 `- <简要描述>(<功能ID 或缺陷ID>)`;`[未发布]` 里已有同一 ID 的条目就不再追加(`/pdlc-ship` 按 ID 去重)

重构目标: $ARGUMENTS

<!-- pdlc:meta 由 frontmatter 生成(adapters/sync_skills.py),勿手改 --> > **本命令的状态机取值**:阶段短名 `refactor`(写进 `history[].stage` 与 `last_phase_result.stage`);下一跳 `pdlc-review`(写进 `next_step`,交接时提示)。 <!-- pdlc:meta-end --> <!-- @include templates/prompts/state-update.md(已内联于下方,无需另读) -->

状态机更新(段四必须执行)

本命令完成主产出后,必须推进状态机文件 `docs/.pdlc-state/<feature-id>.json`。

用脚本推进,不要手写状态 JSON

bash scripts/pdlc-advance.sh <功能ID> --stage <本阶段短名> --next <下一跳命令名|null> \
  [--produced <产物路径,逗号分隔>] [--autonomous] [--decision "<确认点>=<所选默认>"]... \
  [--name <kebab-case>(状态文件还不存在时)] <项目根>
  • 脚本是本 skill 自带的 `scripts/pdlc-advance.sh`。阶段短名与下一跳就是本命令正文里「本命令的状态机取值」那两个值;带了 `--autonomous` 就照传
  • **检查由脚本自己跑**:它按阶段调用同目录的 `scripts/pdlc-checks.sh`(tdd 验红灯;impl / fix / refactor 跑 unit、coverage、lint;

review 再加 e2e;e2e 只跑 e2e;文档类阶段不跑),据此判 `ok`、整段写入 history 与 `last_phase_result`, 写完调用 `scripts/pdlc-state-lint.sh` 体检。**不要**自己先写 `checks`,也不要挑检查项

  • tdd 阶段的 `--produced` 必须列出写的测试文件:脚本记下它们的指纹,之后 impl / refactor / review 推进时比对
  • 主动交还人类(命中人工点、评审有阻塞项、测试本身有矛盾等)→ 加 `--blocked "<原因>"`:脚本照跑检查、如实记录,但不推进,原因写进 `blocked_reason`
  • `/pdlc-ship` 写 `ship_done`、`/pdlc-deploy` 写 `deploy_done` → 加 `--done`(只有这两个命令能用)

脚本的退出码决定怎么收尾:

| 退出码 | 含义 | 你要做的 | |---|---|---| | `0` | 已推进 | 交接里引用 stdout 那行 JSON(`checks`、`current_stage`、`next_step`) | | `3` | 未推进:检查没过 / 红灯没验证 / 主动 block / tdd 写的测试被改 | 把 stdout 里的 `blocked_reason` 原样写进报告,按阻塞收尾。**不要为了让它推进去改测试或改检查命令** | | `2` | 脚本跑不了(参数错、缺 jq、状态文件损坏) | 参数错就改正重跑;缺 jq 才按下面的格式手写,并在报告里写明「状态机未经脚本写入」 |

> 退出码 `3` 且原因是「tdd 阶段写的测试被改动」:实现阶段不改测试。测试是否真的写错了(与设计 / PRD 矛盾),**只有人能判断**: > - 交互模式:先把矛盾讲清楚、问人;人同意后再带 `--tests-changed "<理由>"` 重跑,理由记进 `auto_decisions` > - `--autonomous`:没有人可问,**不要改测试**,带 `--blocked "<矛盾在哪>"` 收尾。自主模式下带 `--tests-changed` 脚本也不推进 > > 项目开了 PDLC 护栏(`docs/00_standards/pdlc-hooks.yml`)时,用 Write / Edit 直接写状态文件会被拦下。

状态文件格式(读状态时参考;只有脚本跑不了时才照它手写)

{
  "feature_id": "<F/B ID>",
  "feature_name": "<kebab-case>",
  "created_at": "<首次创建时间 ISO 8601>",
  "current_stage": "<当前阶段名>",
  "run_mode": "interactive | autonomous",
  "history": [
    {
      "stage": "<阶段名>",
      "done_at": "<ISO 8601>",
      "produced": ["<相对路径 1>", "<相对路径 2>"],
      "self_audit": { "passed": <N>, "failed": <N>, "manual": <N> },
      "auto_decisions": [
        { "point": "<autonomous 下自动前进的确认点>", "chose": "<所选默认>", "at": "<ISO 8601>" }
      ]
    }
  ],
  "last_phase_result": {
    "stage": "<本次阶段名>",
    "ok": true,
    "advanced_to": "<推进到的下一阶段 | null>",
    "checks": {},
    "self_audit": { "failed": 0 },
    "blocked_reason": null,
    "run_mode": "interactive | autonomous",
    "at": "<ISO 8601>"
  },
  "relations": {
    "extends": [],
    "depends_on": [],
    "supersedes": [],
    "resolves": [],
    "conflicts_with": [],
    "relates_to": [],
    "_updated_at": "<ISO 8601 | 省略>"
  },
  "next_step": "<下一跳命令名,如 pdlc-design;若流程结束则为 null>"
}

> ⛔ 示例里的 `"checks": {}` 是「本阶段没有命令可跑」的样子,**不是键名示范**——键名与取值见下方 §1。

> **`relations` 块(RFC#6,Phase 1

Read more
@kanfu-panda
Ships withpdlc

Author: kanfu-panda Repo: github.com/kanfu-panda/pdlc-skills License: MIT pdlc-skills is built on one rule: an AI saying "done" doesn't count — exit codes do.

Stars on GitHub

Momentum

stars in total
18
Star history of kanfu-panda/pdlc-skills on GitHub, a chart by star-history.com

Other skills on pdlc.

pdlc-add-app

pdlc-add-app

在已有项目里加一个前端应用(先探测布局,跟随现有目录与技术栈);新项目一次性初始化用 /pdlc-bootstrap

@kanfu-panda@kanfu-pandaInstall Skill
pdlc-add-service

pdlc-add-service

在已有项目里加一个后端服务(先探测布局,跟随现有目录与技术栈);新项目一次性初始化用 /pdlc-bootstrap

@kanfu-panda@kanfu-pandaInstall Skill
pdlc-adopt

pdlc-adopt

已有代码、还没有 PDLC 文档的老项目接入——只读扫描后逆向生成基线文档并做健康检查,不改代码;空目录新项目用 /pdlc-bootstrap

@kanfu-panda@kanfu-pandaInstall Skill
pdlc-arch

pdlc-arch

架构分析(生成/更新 docs/ARCHITECTURE.md 系统架构总览 · surface 型)

@kanfu-panda@kanfu-pandaInstall Skill
pdlc-bootstrap

pdlc-bootstrap

空目录从一句话描述初始化新项目(服务 / 应用拆分、技术栈、目录骨架与 PDLC 文档目录);已有代码的项目用 /pdlc-adopt

@kanfu-panda@kanfu-pandaInstall Skill

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills10d309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc11d293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react1d252k
@facebook@facebookInstall Skill

Questions about pdlc-refactor.

Which coding agents can use pdlc-refactor?
pdlc-refactor installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.