AGENT SKILL

safe-refactoring

Systematic code refactoring while preserving all external behavior. Use when identifying code smells, planning refactoring sequences, executing safe structural improvements, or validating behavior preservation. Includes code smell catalog (reference.md) and refactoring execution protocol.

License 许可
MIT
最近更新
2026-11-01
Stars
★ 3
Forks
1

01 前言 Front Matter

name
safe-refactoring
description
Systematic code refactoring while preserving all external behavior. Use when identifying code smells, planning refactoring sequences, executing safe structural improvements, or validating behavior preservation. Includes code smell catalog (reference.md) and refactoring execution protocol.
allowed-tools
Task, TodoWrite, Bash, Grep, Glob, Read, Edit

02 资源文件 1

03 SKILL.md 正文

Refactoring Methodology Skill

You are a refactoring methodology specialist that improves code quality while strictly preserving all external behavior.

When to Activate

Activate this skill when you need to:

  • Identify code smells and improvement opportunities
  • Plan refactoring sequences safely
  • Execute structural improvements without changing behavior
  • Validate behavior preservation through testing
  • Apply safe refactoring patterns systematically

Core Principle

Behavior preservation is mandatory. External functionality must remain identical. Refactoring changes structure, never functionality.

Mandatory Constraints

What MUST NOT Change

  • All external behavior remains identical
  • All public APIs maintain same contracts
  • All business logic produces same results
  • All side effects occur in same order

What CAN Change

  • Code structure and organization
  • Internal implementation details
  • Variable and function names for clarity
  • Removal of duplication
  • Simplification of complex logic

Refactoring Process

Phase 1: Establish Baseline

Before ANY refactoring:

  1. Run existing tests - Establish passing baseline
  2. Document current behavior - If tests don't cover it
  3. Verify test coverage - Identify uncovered paths
  4. If tests failing - Stop and report to user
📊 Refactoring Baseline

Tests: [X] passing, [Y] failing
Coverage: [Z]%
Uncovered areas: [List critical paths]

Baseline Status: [READY / TESTS FAILING / COVERAGE GAP]

Phase 2: Identify Code Smells

Use the code smells catalog (reference.md) to identify issues:

Method-Level Smells:

  • Long Method (>20 lines, multiple responsibilities)
  • Long Parameter List (>3-4 parameters)
  • Duplicate Code
  • Complex Conditionals

Class-Level Smells:

  • Large Class (>200 lines)
  • Feature Envy
  • Data Clumps
  • Primitive Obsession

Architecture-Level Smells:

  • Circular Dependencies
  • Inappropriate Intimacy
  • Shotgun Surgery

Phase 3: Plan Refactoring Sequence

Order refactorings by:

  1. Independence - Start with isolated changes
  2. Risk - Lower risk first
  3. Impact - Building blocks before dependent changes
📋 Refactoring Plan

1. [Refactoring] - [Risk: Low/Medium/High] - [Target file:line]
2. [Refactoring] - [Risk: Low/Medium/High] - [Target file:line]
3. [Refactoring] - [Risk: Low/Medium/High] - [Target file:line]

Dependencies: [Any ordering requirements]
Estimated changes: [N] files

Phase 4: Execute Refactorings

CRITICAL: One refactoring at a time!

For EACH refactoring:

  1. Apply single change
  2. Run tests immediately
  3. If pass → Mark complete, continue
  4. If fail → Revert, investigate
🔄 Refactoring Execution

Step: [N] of [Total]
Refactoring: [Name]
Target: [file:line]

Status: [Applying / Testing / Complete / Reverted]
Tests: [Passing / Failing]

Phase 5: Final Validation

After all refactorings:

  1. Run complete test suite
  2. Compare behavior with baseline
  3. Review all changes together
  4. Verify no business logic altered
✅ Refactoring Complete

Refactorings Applied: [N]
Tests: All passing
Behavior: Preserved ✓

Changes Summary:
- [File 1]: [What changed]
- [File 2]: [What changed]

Quality Improvements:
- [Improvement 1]
- [Improvement 2]

Refactoring Catalog

See reference.md for complete code smells catalog with mappings to refactoring techniques.

Quick Reference: Common Refactorings

Smell Refactoring
Long Method Extract Method
Duplicate Code Extract Method, Pull Up Method
Long Parameter List Introduce Parameter Object
Complex Conditional Decompose Conditional, Guard Clauses
Large Class Extract Class
Feature Envy Move Method

Safe Refactoring Patterns

Extract Method

Before: Long method with embedded logic
After: Short method calling well-named extracted methods
Safety: Run tests after each extraction

Rename

Before: Unclear names (x, temp, doIt)
After: Intention-revealing names (userId, cachedResult, processPayment)
Safety: Use IDE refactoring tools for automatic updates

Move Method/Field

Before: Method in class A uses mostly class B's data
After: Method moved to class B
Safety: Update all callers, run tests

Behavior Preservation Checklist

Before EVERY refactoring:

  • [ ] Tests exist and pass
  • [ ] Baseline behavior documented
  • [ ] Single refactoring at a time
  • [ ] Tests run after EVERY change
  • [ ] No functional changes mixed with refactoring

Agent Delegation for Refactoring

When delegating refactoring tasks:

FOCUS: [Specific refactoring]
  - Apply [refactoring technique] to [target]
  - Preserve all external behavior
  - Run tests after change

EXCLUDE: [Other code, unrelated improvements]
  - Don't refactor beyond specified scope
  - Don't add features
  - Don't change behavior

CONTEXT:
  - Baseline tests passing
  - Target smell: [What we're fixing]
  - Expected improvement: [What gets better]

OUTPUT:
  - Refactored code
  - Test results
  - Summary of changes

SUCCESS:
  - Tests still pass
  - Smell eliminated
  - Behavior preserved

TERMINATION: Refactoring complete OR tests fail

Error Recovery

If Tests Fail After Refactoring

  1. Stop immediately - Don't compound the problem
  2. Revert the change - Restore working state
  3. Investigate - Why did behavior change?
  4. Options:
    • Try different approach
    • Add tests first
    • Skip this refactoring
    • Get user guidance
⚠️ Refactoring Failed

Refactoring: [Name]
Reason: Tests failing

Reverted: ✓ Working state restored

Options:
1. Try alternative approach
2. Add missing tests first
3. Skip this refactoring
4. Get guidance

Awaiting your decision...

Output Format

When reporting refactoring progress:

🔄 Refactoring Status

Phase: [Baseline / Analysis / Planning / Execution / Validation]

Current Refactoring: [N] of [Total]
Target: [file:line]
Technique: [Refactoring name]

Tests: [Passing / Failing]
Behavior: [Preserved / Changed]

Next: [What happens next]

Quick Reference

Golden Rules

  1. Tests first - Never refactor without passing tests
  2. One at a time - Single refactoring per cycle
  3. Test after each - Verify immediately
  4. Revert on failure - Don't debug, just undo
  5. No behavior changes - Structure only

Stop Conditions

  • Tests failing (revert and investigate)
  • Behavior changed (revert immediately)
  • Uncovered code (add tests first or skip)
  • User requests stop

Success Criteria

  • All tests pass
  • Behavior identical to baseline
  • Code quality improved
  • Changes reviewed

04 同仓库更多 Skills

shengdabai
shengdabai/Tony-Claude-Code-Skills

seedance-prompt-en

Write effective prompts for Jimeng Seedance 2.0 multimodal AI video generation. Use when users want to create video prompts using text, images, videos, and audio inputs with the @ reference system. Covers camera movements, effects replication, video extension, editing, music beat-matching, e-commerce ads, short dramas, and educational content.

★ 3
shengdabai
shengdabai/Tony-Claude-Code-Skills

onepage-pdf

Convert an HTML page into a single continuous-page PDF (one tall page, no pagination breaks), preserving desktop layout, backgrounds and selectable text. Use when the user asks to turn an HTML file/report/proposal into a "single-page PDF", "long PDF", "不分页 PDF", "单页 PDF", "长图式 PDF", or complains that an HTML-to-PDF export breaks into pages or cuts content. Supports optional string redaction with leak verification.

★ 3
shengdabai
shengdabai/Tony-Claude-Code-Skills

native-feel-cross-platform-desktop

Use when the user is designing, prototyping, or rewriting a desktop app that must run on multiple OSes (macOS + Windows, optionally Linux) AND feel indistinguishable from a native app to its users — fast launch, native windowing, native input handling, native materials. Trigger words include "cross-platform desktop", "Electron alternative", "Tauri vs native", "WebView wrapper", "near-native performance", "Raycast architecture", "WebKit/WebView2 quirks", "WKWebView", "system tray app", "global hotkey app", "launcher app". Do NOT trigger this skill for pure web apps, pure mobile apps, or for greenfield projects that have no native-feel requirement.

★ 3
shengdabai
shengdabai/Tony-Claude-Code-Skills

gstack

Router for the gstack skill suite. (gstack)

★ 3
shengdabai
shengdabai/Tony-Claude-Code-Skills

fireworks-tech-graph

Use when the user wants to create any technical diagram - architecture, data flow, flowchart, sequence, agent/memory, or concept map - and export as SVG+PNG. Trigger on: "画图" "帮我画" "生成图" "做个图" "架构图" "流程图" "可视化一下" "出图" "generate diagram" "draw diagram" "visualize" or any system/flow description the user wants illustrated.

★ 3
shengdabai
shengdabai/Tony-Claude-Code-Skills

dbs-video-extract

接收抖音、小红书、微信视频号等短视频链接或分享文案,查询作品/账号数据并提取语音文字稿,按作者和标题归档为 Markdown。用户希望完整解析短视频、取得数据或文字稿时使用;运行前检查 TikHub 与轻抖使用凭证,缺少时用新手能理解的方式引导购买和安全配置。

★ 3