jpskill.com
🛠️ 開発・MCP コミュニティ

safe-refactor-code

リポジトリの継続性を保ちながら、エージェント自身のフォルダ内でコードを安全かつ段階的にリファクタリングし、ドキュメントとの整合性を維持することで、コードの改善やモジュール刷新などを効率的に行うSkill。

📜 元の英語説明(参考)

Refactor code safely in small verified slices while keeping repo continuity docs in sync inside the active agent's own folder. Use when an agent is asked to refactor, restructure, clean up, remove or replace code, modernize modules, or do follow-up hygiene in a repo.

🇯🇵 日本人クリエイター向け解説

一言でいうと

リポジトリの継続性を保ちながら、エージェント自身のフォルダ内でコードを安全かつ段階的にリファクタリングし、ドキュメントとの整合性を維持することで、コードの改善やモジュール刷新などを効率的に行うSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o safe-refactor-code.zip https://jpskill.com/download/10041.zip && unzip -o safe-refactor-code.zip && rm safe-refactor-code.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10041.zip -OutFile "$d\safe-refactor-code.zip"; Expand-Archive "$d\safe-refactor-code.zip" -DestinationPath $d -Force; ri "$d\safe-refactor-code.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して safe-refactor-code.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → safe-refactor-code フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

安全なコードのリファクタリング

コードを検証済みの小さな単位でリファクタリングし、リポジトリの継続性ファイルを更新して、将来のエージェントがセッション全体を再読することなく再開できるようにします。

コアなルール

  • リポジトリのルートにある AGENTS.md を、主要な長期的なリポジトリのメモリとして扱います。既存のコンテンツを保持し、盲目的に書き換えるのではなく、慎重に更新します。
  • エージェントローカルの MEMORY.md は短く保ちます。これはリポジトリの作業概要であり、共有またはツール管理されたメモリシステムの代わりではありません。
  • safe-refactor-code.md は、リポジトリのリファクタリングルール、ガードレール、および定期的なクリーンアップワークフローに焦点を当て続けます。
  • 実際のコード変更については、今日のセクション ## [YYYY-MM-DD] を使用して CHANGELOG.md を更新します。
  • ドキュメントへの追加またはスコープを絞った編集を優先します。ユーザーが明示的に要求しない限り、ユーザーが記述した履歴を消去しないでください。
  • リファクタリング後、完了する前に、明らかなデッドコード、未使用のインポート、古いヘルパー、および古いドキュメント参照をスキャンします。

エージェントの互換性

  • このスキルを、1つの製品またはランタイムだけでなく、どのAIエージェントでも実行できるように記述します。
  • 「エージェント」、「現在のリポジトリ」、「利用可能な検証ツール」などの一般的な用語を優先します。
  • ホスト環境に独自のグローバルまたは共有メモリシステムがある場合は、それをリポジトリローカルのドキュメントとは別に扱います。
  • 現在のリポジトリが異なるハンドオフファイルを使用している場合は、慎重に適応させますが、同じ責任を維持します。

ステップ0:アクティブなエージェントとドキュメントフォルダの検出

コードまたはドキュメントを編集する前に、どのアージェントが実行されているかを検出します。

if .codex/ exists    → agent = codex,     doc folder = .codex/memory/
if .claude/ exists   → agent = claude,    doc folder = .claude/memory/
if .cursor/ exists   → agent = cursor,    doc folder = .cursor/memory/
if .windsurf/ exists → agent = windsurf,  doc folder = .windsurf/memory/
if none found        → doc folder = repo root

複数のエージェントフォルダが存在する場合は、現在実行中のエージェントに一致するものを優先します。

<agent-folder>/memory/ がまだ存在しない場合は作成します。

ドキュメントファイルの場所:

ファイル パス
AGENTS.md リポジトリルート(常に)
MEMORY.md <agent-folder>/memory/MEMORY.md
CHANGELOG.md <agent-folder>/memory/CHANGELOG.md
safe-refactor-code.md <agent-folder>/memory/safe-refactor-code.md

ワークフロー

1. 最初に継続性ファイルを読み込む

コードを編集する前に、存在する場合はこれらのファイルを調べます。

  • リポジトリルートの AGENTS.md
  • <agent-folder>/memory/safe-refactor-code.md
  • <agent-folder>/memory/MEMORY.md
  • <agent-folder>/memory/CHANGELOG.md

いずれかが欠落している場合は、リファクタリング作業が役立つ場合にのみ作成します。

欠落している AGENTS.md のフォールバック:

  • ルートにある CLAUDE.mdGEMINI.md、または別のリポジトリハンドオフガイドなど、既存の同等のファイルを優先します。
  • 同等のものが存在する場合は、それを今回の実行の主要な継続性ドキュメントとして扱います。
  • 同等のものが存在しない場合は、リファクタリングによってアーキテクチャまたはハンドオフのリスクが十分に変化し、将来のエージェントが恩恵を受ける場合にのみ AGENTS.md を作成します。

2. 安全な単位でリファクタリングする

  • 一度に1つのサブシステムまたは関心事を優先します。
  • ユーザーが動作の変更を要求しない限り、動作を保持します。
  • 利用可能な最も狭い有用なチェック(lint、型チェック、テスト、ビルド、またはターゲットを絞ったプローブ)を使用して、各単位を検証します。
  • リファクタリングによってデッドコードが公開された場合は、確信度が高い場合に、同じ領域にある明らかな残骸を削除します。
  • クリーンアップのリスクが自明でない場合、またはリポジトリに多くの古いモジュールがある場合は、削除する前に影響範囲をマッピングし、自動削除する代わりに確信度の低い候補にフラグを立てます。

広範囲のデッドコードが、直接的なリファクタリング領域を超えて検出された場合は、リポジトリ全体のデッドコード監査のために codebase-pruner スキルを呼び出します。

3. 完了前にドキュメントを同期する

実際のコード変更後、検出されたドキュメントフォルダ内の継続性ファイルを更新します。

  • AGENTS.md(リポジトリルート)— 現在の状態、主要な決定事項、ブロッカー、ハンドオフノート
  • safe-refactor-code.md — リポジトリ固有のリファクタリング制約と定期的なクリーンアップルール
  • MEMORY.md — 短い現在のスナップショット、アクティブな注意点、重要なパス
  • CHANGELOG.md — 今日付けのセクション(追加、変更、修正、削除)

4. 変更履歴の形状を安定させる

次の構造を使用します。

## [YYYY-MM-DD]

### Added
- ...

### Changed
- ...

### Fixed
- ...

### Removed
- ...
  • 今日のセクションが既に存在する場合は再利用します。
  • 何も属していない場合は、空のサブセクションを省略します。
  • 読み取り専用のレビューまたは分析のターンには、変更履歴エントリを追加しないでください。

5. 衛生的なパスで終了する

終了する前に:

  • リファクタリングによって導入された未使用のインポートを削除します。
  • 確信度が高い場合は、リファクタリングによってデッドになったヘルパー、エクスポート、またはファイルを削除します。
  • ドキュメント内の古いファイル/パス参照を更新します。
  • フラグが立てられたが削除されていないデッドコードを safe-refactor-code.md または AGENTS.md に記録します。

何を書くか

AGENTS.md(リポジトリルート)

リファクタリングの影響を受けるセクションのみを更新します。優先:

  • 現在のリポジトリの状態
  • 重要なアーキテクチャ上の決定
  • 既知のブロッカーまたは注意点
  • 将来のエージェントが開始すべき場所

safe-refactor-code.md

このファイルは、次のようなリポジトリ固有のリファクタリング手順に使用します。

  • 触れても安全な領域
  • 危険なファイルまたは生成された出力
  • 必要な検証コマンド
  • クリーンアップの慣例
  • 定期的な移行または同期の落とし穴

MEMORY.md

これを簡潔に保ちます。良い内容:

  • アクティブなアーキテクチャスナップショット
  • 現在の信頼できる情報源ファイル
  • 既知の一時的な回避策
  • 上位のフォローアップ項目

CHANGELOG.md

意味のあるリポジトリの変更のみを記録します。エントリをユーザー向けで簡潔に保ちます。

トリガー

このスキルをアクティブにする典型的なリクエスト:

  • "safe refactor this repo"
  • "clean up after this refactor"
  • "update agents and changelog too"
  • "remove dead code after changing this flow"
  • "keep repo memory in sync while refactoring"
  • "make this refactor safer for future agents"
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Safe Refactor Code

Refactor code in small verified slices, then update the repo's continuity files so future agents can resume without rereading the whole session.

Core Rules

  • Treat AGENTS.md at repo root as the main long-term repo memory. Preserve existing content and update it carefully instead of rewriting blindly.
  • Keep agent-local MEMORY.md short. It is a working summary for the repo, not a replacement for any shared or tool-managed memory system.
  • Keep safe-refactor-code.md focused on the repo's refactor rules, guardrails, and recurring cleanup workflow.
  • Update CHANGELOG.md on real code changes using today's section: ## [YYYY-MM-DD].
  • Prefer additive or scoped edits to docs. Do not wipe user-written history unless the user explicitly asks.
  • After refactors, scan for obvious dead code, unused imports, stale helpers, and outdated doc references before finishing.

Agent Compatibility

  • Write this skill so any AI agent can follow it, not only one product or runtime.
  • Prefer generic terms such as "agent", "current repo", and "available verification tools".
  • If the host environment has its own global or shared memory system, treat that as separate from repo-local docs.
  • If the current repo uses different handoff files, adapt carefully but preserve the same responsibilities.

Step 0: Detect Active Agent and Doc Folder

Before editing code or docs, detect which agent is running:

if .codex/ exists    → agent = codex,     doc folder = .codex/memory/
if .claude/ exists   → agent = claude,    doc folder = .claude/memory/
if .cursor/ exists   → agent = cursor,    doc folder = .cursor/memory/
if .windsurf/ exists → agent = windsurf,  doc folder = .windsurf/memory/
if none found        → doc folder = repo root

If multiple agent folders exist, prefer the one matching the current running agent.

Create <agent-folder>/memory/ if it does not exist yet.

Doc file locations:

File Path
AGENTS.md repo root (always)
MEMORY.md <agent-folder>/memory/MEMORY.md
CHANGELOG.md <agent-folder>/memory/CHANGELOG.md
safe-refactor-code.md <agent-folder>/memory/safe-refactor-code.md

Workflow

1. Read Continuity Files First

Before editing code, inspect these files when present:

  • AGENTS.md at repo root
  • <agent-folder>/memory/safe-refactor-code.md
  • <agent-folder>/memory/MEMORY.md
  • <agent-folder>/memory/CHANGELOG.md

If one is missing, create it only if the refactor work makes it useful.

Fallback for missing AGENTS.md:

  • Prefer an existing equivalent file such as CLAUDE.md, GEMINI.md, or another repo handoff guide at root.
  • If an equivalent exists, treat it as the main continuity document for this run.
  • If no equivalent exists, create AGENTS.md only when the refactor changes architecture or handoff risk enough that future agents would benefit.

2. Refactor in Safe Slices

  • Prefer one subsystem or concern at a time.
  • Preserve behavior unless the user asked for a behavior change.
  • Verify each slice with the narrowest useful checks available: lint, type-check, tests, build, or targeted probe.
  • If a refactor exposes dead code, remove obvious leftovers in the same area when confidence is high.
  • If cleanup risk is non-trivial or the repo has many stale modules, map blast radius before deleting and flag low-confidence candidates instead of auto-removing.

If widespread dead code is detected beyond the immediate refactor area, invoke the codebase-pruner skill for a full repo dead code audit.

3. Sync Docs Before Finishing

After real code changes, update the continuity files in the detected doc folder:

  • AGENTS.md (repo root) — current state, key decisions, blockers, handoff notes
  • safe-refactor-code.md — repo-specific refactor constraints and recurring cleanup rules
  • MEMORY.md — short current snapshot, active caveats, important paths
  • CHANGELOG.md — today's dated section with Added, Changed, Fixed, Removed

4. Keep Changelog Shape Stable

Use this structure:

## [YYYY-MM-DD]

### Added
- ...

### Changed
- ...

### Fixed
- ...

### Removed
- ...
  • Reuse today's section if it already exists.
  • Omit empty subsections when nothing belongs there.
  • Do not add a changelog entry for read-only review or analysis turns.

5. Finish With a Hygiene Pass

Before closing:

  • Remove unused imports introduced by the refactor.
  • Remove helpers, exports, or files made dead by the refactor when confidence is high.
  • Update stale file/path references in docs.
  • Note any flagged but unremoved dead code in safe-refactor-code.md or AGENTS.md.

What To Write

AGENTS.md (repo root)

Update only the sections affected by the refactor. Prefer:

  • Current repo state
  • Important architectural decisions
  • Known blockers or caveats
  • Where future agents should start

safe-refactor-code.md

Use this file for repo-specific refactor instructions such as:

  • Safe areas to touch
  • Dangerous files or generated outputs
  • Required verification commands
  • Cleanup conventions
  • Recurring migration or sync pitfalls

MEMORY.md

Keep this concise. Good contents:

  • Active architecture snapshot
  • Current source-of-truth files
  • Known temporary workarounds
  • Top follow-up items

CHANGELOG.md

Record only meaningful repo changes. Keep entries user-facing and concise.

Triggers

Typical requests that activate this skill:

  • "safe refactor this repo"
  • "clean up after this refactor"
  • "update agents and changelog too"
  • "remove dead code after changing this flow"
  • "keep repo memory in sync while refactoring"
  • "make this refactor safer for future agents"