一键导入
plan-refactor
Plan refactoring for existing features. Analyzes current implementation and creates/updates design documents with refactoring plan.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Plan refactoring for existing features. Analyzes current implementation and creates/updates design documents with refactoring plan.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Analyze and extract requirements from use case diagram. Use when extracting UR/FR/NFR from use cases or when called by generate-prd.
Check consistency between implementation code and design documents (design), detecting discrepancies
Generate quality assurance checklists from specifications and plans with structured IDs and categories
Analyze specifications and generate clarification questions to eliminate ambiguity before implementation
Define and manage non-negotiable project principles (Constitution) and verify synchronization with other documents
Automatically executed during document updates or before implementation to check consistency between PRD ↔ *_spec.md ↔ *_design.md. Detects missing requirement ID (UR/FR/NFR) references, data model mismatches, API definition discrepancies, terminology inconsistencies, and ensures traceability between documents.
| name | plan-refactor |
| description | Plan refactoring for existing features. Analyzes current implementation and creates/updates design documents with refactoring plan. |
| argument-hint | <feature-name> [context] [--scope=<dir>] [--ci] |
| arguments | ["feature-name"] |
| license | MIT |
| user-invocable | true |
| allowed-tools | Read, Write, Edit, Glob, Grep, AskUserQuestion, Bash |
Plans refactoring for existing features by analyzing current implementation and creating/updating design documents with a comprehensive refactoring plan.
This skill supports two scenarios:
Read the following prerequisite references before execution:
references/prerequisites_plugin_update.md - Check for plugin updatesreferences/prerequisites_principles.md - Read AI-SDD principles documentreferences/prerequisites_directory_paths.md - Resolve directory paths using SDD_* environment variablesTemplates are located under templates/${SDD_LANG:-en}/ within this skill directory.
The SDD_LANG environment variable determines the language (default: en).
feature-name: $feature-nameFull argument string: $ARGUMENTS
Fallback: If the value above is empty, remains a literal
$placeholder, or starts with--(a flag captured positionally), treat the argument as omitted and interpret the full argument string instead. Ask the user interactively when a required argument is missing.contextis free-form text and flags — extract them from the full argument string (everything afterfeature-name).
| Argument | Required | Description |
|---|---|---|
feature-name | Yes | Target feature name or path (supports flat/hierarchical structure) |
context | No | Refactoring goal or improvement intent (e.g., "無限スクロール化", "依存性注入導入") |
--scope=<dir> | No | Limit implementation file search scope (e.g., src/, lib/) |
--ci | No | CI/non-interactive mode (auto-confirm, no user prompts) |
See examples/cli_usage.md for example invocations.
When generating reverse-engineered spec/design documents (Case B), include YAML front matter. When updating existing design documents (Case A), preserve existing front matter and update relevant fields.
See references/front_matter_spec_design.md for full schema definition, dependency direction rules, and validation checklist.
| Field | Rule |
|---|---|
status | "review" (reverse-engineered documents require review) |
depends-on | PRD ID if PRD exists (e.g., ["prd-auth"]). Empty if no PRD |
tags | Always include "reverse-engineered", plus keywords from code analysis |
| Field | Rule |
|---|---|
status | "review" (reverse-engineered documents require review) |
impl-status | "implemented" (already implemented since reverse-engineered) |
depends-on | Spec ID (e.g., ["spec-auth"]) |
tags | Always include "reverse-engineered", plus keywords from code analysis |
When adding a refactoring plan to an existing design doc:
updated to current date"refactoring-planned" to tags if not presentStep 1.1: Scan for Existing Documents
Run the document scanning script: python3 "${CLAUDE_PLUGIN_ROOT}/skills/plan-refactor/scripts/scan-existing-docs.py" "${FEATURE_NAME}"
This script:
${SDD_ROOT}/.cache/plan-refactor/existing-docs.jsonStep 1.2: Read Scan Results
Read ${CLAUDE_PROJECT_DIR}/${SDD_ROOT}/.cache/plan-refactor/existing-docs.json.
See examples/cache_json_outputs.md for an example of this file's content.
Step 1.3: Determine Processing Case
design_exists is true → Case A (existing documents)design_exists is false → Case B (no documents, reverse-engineering needed)If context argument is provided:
Parse the user's refactoring goal and extract:
Example context parsing:
| Context Input | Extracted Information |
|---|---|
"無限スクロール化してパフォーマンス改善" | Goal: 無限スクロール化 Motivation: パフォーマンス改善 |
"依存性注入を導入してテスト容易性を向上" | Goal: 依存性注入導入 Motivation: テスト容易性向上 |
"Strangler Figパターンで段階的にマイクロサービス化" | Goal: マイクロサービス化 Approach: Strangler Figパターン |
"テスト容易性を上げるため密結合を解消" | Goal: 密結合解消 Motivation: テスト容易性向上 |
"モジュール境界を明確化してメンテナンス性向上" | Goal: モジュール境界明確化 Motivation: メンテナンス性向上 |
"react-windowを使って仮想スクロール化" | Goal: 仮想スクロール化 Approach: react-window使用 |
Use extracted information in Phase 3:
If context is NOT provided:
→ Skip this phase, proceed with automatic analysis only (Phase 2)
Step 2.1: Find Implementation Files
Run the implementation file search script. Set SCOPE_DIR from the --scope argument (or leave empty), then run
python3 "${CLAUDE_PLUGIN_ROOT}/skills/plan-refactor/scripts/find-implementation-files.py" "${FEATURE_NAME}" "${SCOPE_DIR}".
This script:
--scope is providednode_modules/, .git/, dist/, etc.${SDD_ROOT}/.cache/plan-refactor/implementation-files.jsonStep 2.2: Read Implementation File List
Read ${CLAUDE_PROJECT_DIR}/${SDD_ROOT}/.cache/plan-refactor/implementation-files.json.
See examples/cache_json_outputs.md for an example of this file's content.
Step 2.3: Validate File Count
file_count > 20 and NOT in --ci mode:
AskUserQuestion to confirm: "Found {count} files. This may take time to analyze.
Continue? [Yes/No/Adjust Scope]"Step 2.4: Read Implementation Files
Read ${CLAUDE_PROJECT_DIR}/${SDD_ROOT}/.cache/plan-refactor/all-files.txt.
Then read the actual implementation files (prioritize key files):
Step 3A.1: Load Existing Documents
Read the following files (paths from scan results):
{prd_path} (if exists){spec_path} (if exists){design_path} (required for Case A)Step 3A.2: Analyze Implementation vs. Specification
Compare implementation with design document:
Step 3A.3: Identify Refactoring Opportunities
Based on analysis, identify:
If context was provided (from Phase 1.5):
Step 3A.4: Generate Refactoring Plan
Use template: read ${CLAUDE_PLUGIN_ROOT}/skills/plan-refactor/templates/${SDD_LANG}/refactor_plan_section.md.
Fill in the template with:
context provided: Start with user's goal (e.g., "無限スクロール化してパフォーマンス改善")context provided: Align goals with user's intentStep 3A.5: Update Design Document
Edit the existing design document: Edit {design_path}.
Append the "## Refactoring Plan" section at the end of the document.
See references/design_doc_integration.md for guidelines on integration.
Step 3B.1: Reverse-Engineer Specification
Analyze implementation files and extract:
Use template: read ${CLAUDE_PLUGIN_ROOT}/skills/plan-refactor/templates/${SDD_LANG}/reverse_spec_template.md.
Step 3B.2: Write Specification Document
Determine path based on structure:
${SDD_SPECIFICATION_PATH}/{feature-name}_spec.md${SDD_SPECIFICATION_PATH}/{parent-feature}/{child-feature}_spec.mdWrite {spec_path}.
Mark the document as reverse-engineered:
⚠️ Note: This specification was reverse-engineered from existing implementation on {DATE}. It may not reflect the original design intent. Please review and update as needed.
Step 3B.3: Reverse-Engineer Design Document
Analyze implementation files and extract:
Use template: read ${CLAUDE_PLUGIN_ROOT}/skills/plan-refactor/templates/${SDD_LANG}/reverse_design_template.md.
Step 3B.4: Write Design Document
Determine path based on structure:
${SDD_SPECIFICATION_PATH}/{feature-name}_design.md${SDD_SPECIFICATION_PATH}/{parent-feature}/{child-feature}_design.mdWrite {design_path}.
Step 3B.5: Generate Refactoring Plan
Follow the same process as Case A Step 3A.4-3A.5:
Verify the refactoring plan includes all required sections:
Required Sections Checklist:
If any required section is missing, add it before proceeding.
Output a summary and recommend next steps. See templates/${SDD_LANG:-en}/completion_output.md for the "Next Steps
Summary" format.
Output format: see the "Output Format" section in templates/${SDD_LANG:-en}/completion_output.md.
The optional context parameter allows you to explicitly specify your refactoring goal:
Without context (automatic analysis): /plan-refactor user-list
→ Claude analyzes code and design, automatically identifies technical debt and proposes generic refactoring
With context (goal-directed refactoring): /plan-refactor user-list "無限スクロール化してパフォーマンス改善"
→ Claude focuses on infinite scroll implementation, prioritizes performance issues, proposes specific approach
Context examples:
| Use Case | Context Example |
|---|---|
| Performance optimization | "無限スクロール化してパフォーマンス改善" |
| Architecture improvement | "依存性注入を導入してテスト容易性を向上" |
| Gradual migration | "Strangler Figパターンで段階的にマイクロサービス化" |
| Technology upgrade | "react-windowを使って仮想スクロール化" |
| Code quality | "密結合を解消してモジュール境界を明確化" |
Benefits of using context:
Ambiguous Context:
If context is unclear or too vague (e.g., "improve it", "make it better"), use AskUserQuestion to clarify. See
examples/ask_user_question_patterns.md for the question/options pattern.
Unrealistic or Infeasible Context:
If context contains technically unrealistic goals (e.g., "make it 100x faster", "zero latency"):
Example:
Conflicting Requirements:
If context includes conflicting goals (e.g., "maximize performance and minimize code complexity"), use
AskUserQuestion to prioritize. See examples/ask_user_question_patterns.md for the question/options pattern.
Document the prioritization in "Purpose and Background".
references/design_doc_integration.md for detailed integration guidelinesreferences/refactor_patterns.md for common refactoring patternsIMPORTANT: Follow the established naming conventions:
| Directory | File Type | Naming Pattern |
|---|---|---|
requirement/ | PRD | {feature-name}.md (no suffix) |
specification/ | Spec | {feature-name}_spec.md (_spec suffix required) |
specification/ | Design | {feature-name}_design.md (_design suffix required) |
Both flat and hierarchical structures are supported. See the "Hierarchical Structure Support" section in
references/design_doc_integration.md for the flat / hierarchical-parent / hierarchical-child directory layouts.
examples/case_a_existing_docs.md for Case A exampleexamples/case_b_no_docs.md for Case B exampleWhen refactoring affects multiple features:
See references/design_doc_integration.md for guidance.
After refactoring is complete:
task/ directory) after implementation<details> tag) if desiredLast Updated: 2026-02-15 Maintained by: AI-SDD Workflow Plugin