Safe Refactoringのロゴ

Safe Refactoring

コミュニティ
UnpaidAttention
safe-refactoring

Execute behavior-preserving code restructuring — establish a test safety net first, apply the smallest mechanical transformations in sequence, verify after each, and refuse refactors that lack the coverage to be done safely. Trigger this when the user asks to "refactor", "clean up", "simplify", "restructure", "extract", or "rename" existing working code, or when you initiate a structural improvement yourself. Do NOT trigger for changes meant to alter behavior (that's a feature/fix — use task-planning), or for diagnosing why code is broken (use debugging-methodology) — a refactor by definition starts from green.

概要

公開者UnpaidAttention
リポジトリfable5-methodology
スキル名safe-refactoring
スター
94
フォーク
21
同梱ファイル
指示のみ
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 指示だけで完結

    モデルに必要なものはすべて指示に含まれているため、別途同期するファイルはありません。

  • オープンソース

    UnpaidAttentionがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキル「Safe Refactoring」をTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/UnpaidAttention/fable5-methodology/tree/main/skills/safe-refactoring
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/UnpaidAttention/fable5-methodology.git /tmp/fable5-methodology
mkdir -p .claude/skills
cp -r /tmp/fable5-methodology/skills/safe-refactoring .claude/skills/safe-refactoring
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでSafe Refactoringを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあるとすぐに、モデルが自動的にSafe Refactoringを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Safe Refactoringは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、スキルを使わない間は指示の読み込みにトークンを消費しません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

Safe Refactoring

A refactor changes structure and preserves behavior — both halves are the definition. Without a way to PROVE behavior is preserved, you are not refactoring; you are rewriting and hoping.

Step 0: Define the behavior contract

State in one or two lines what must be true before and after: "all existing tests pass; the public API of billing/ is unchanged; the CLI output is byte-identical." This contract is what "safe" means for this refactor. If any behavior IS allowed to change, that part is not a refactor — split it out (Step 4).

Step 1: Establish the safety net BEFORE touching anything

  1. Run the existing tests over the affected code. Record the baseline (green, or which failures are pre-existing).
  2. Assess whether the tests actually cover the code you'll restructure: run coverage over the affected files, or inspect — do the tests exercise the branches you're about to move?
  3. Insufficient coverage → write characterization tests first. These capture what the code DOES today (not what it should do): feed representative inputs, assert the actual current outputs. For messy code, golden-master style works: capture today's output for a corpus of inputs into fixture files, assert future output matches byte-for-byte.
  4. Characterization tests are committed/verified BEFORE the first structural edit — they are the net; a net installed after the fall is decoration.

Step 2: The refuse/defer rule

Refuse (or defer pending tests) when ALL of: the code has no meaningful coverage, AND its behavior can't be cheaply characterized (nondeterminism, heavy I/O entanglement, no way to run it), AND the restructuring is more than mechanical. Say exactly that:

"This refactor isn't safe yet: dispatch() has zero coverage and side-effects I can't capture in a harness. I can (a) write characterization tests first (~N tests), or (b) limit to mechanical renames only. Which?"

Refusing a risky refactor is a correct deliverable. A "successful" blind rewrite is a defect with a delay on it.

Step 3: Smallest transformations, verified individually

  1. Decompose into named mechanical steps — rename; extract function/module; inline; move; change signature (with all call sites); replace conditional with early return. One step at a time.
  2. After EACH step: run the affected tests (the verification-loop rhythm). Green → next step; red → the step itself is the suspect, fix or revert it before anything else.
  3. Prefer tool-assisted transformations (LSP rename, IDE extract) over hand edits — then grep for what tools miss: string references, docs, templates, reflection, serialized names, SQL, config keys.
  4. Order steps so the tree compiles between every pair — never a step that requires step N+1 to build.

Step 4: No mixed cargo

If you discover an actual bug mid-refactor: do NOT fix it inside the refactor. Note it, finish (or pause) the refactor to a verified state, then fix the bug as its own change with its own regression test. A diff that both moves code and changes behavior cannot be reviewed for either property. Same for improvements: new features never ride along in a refactor diff.

Step 5: Prove the contract

Full affected suite green; baseline failures unchanged; and check the contract from Step 0 directly (API surface diff, byte-compare the golden outputs). Review the diff explicitly asking one question per hunk: "could this hunk change behavior?" Any hunk answering "yes" that isn't a pure signature-propagation gets scrutiny.

Worked example

Task: "Clean up report.py — 600 lines, one god function."

  • Contract: CLI output byte-identical for all supported flags; tests stay green.
  • Safety net: only 2 tests exist, covering ~20%. Write a golden master: run the CLI over 8 representative fixture inputs (incl. empty file, unicode, flag combinations), save outputs to tests/golden/. Now every future step is checked against reality.
  • Steps: (1) extract parse_args → golden green; (2) extract load_rows → green; (3) extract render_table → RED: one golden diff — a .strip() was lost in the move; restore it, green (the net just paid for itself); (4) move helpers to report/lib.py → green; (5) found an actual bug (crash on zero rows) → NOTED in report, not fixed here.
  • Deliver: structure changed, goldens byte-identical, bug reported separately with a suggested regression test.

Done when

The behavior contract from Step 0 is verified by executed tests (including any characterization tests written first, not after); every transformation step ran green before the next began; the diff contains zero intentional behavior changes; and any bugs discovered are reported separately — not silently fixed in, and not silently left unmentioned.

よくある質問

AIスキル「Safe Refactoring」では何ができますか?

Execute behavior-preserving code restructuring — establish a test safety net first, apply the smallest mechanical transformations in sequence, verify after each, and refuse refactors that lack the coverage to be done safely. Trigger this when the user asks to "refactor", "clean up", "simplify", "restructure", "extract", or "rename" existing working code, or when you initiate a structural improvement yourself. Do NOT trigger for changes meant to alter behavior (that's a feature/fix — use task-planning), or for diagnosing why code is broken (use debugging-methodology) — a refactor by definiti...

TypingMindでSafe Refactoringを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Safe Refactoringはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにSafe Refactoringをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/UnpaidAttention/fable5-methodology/tree/main/skills/safe-refactoringを貼り付けてください。TypingMindがSKILL.mdを読み込み、チャットごとに有効にできるスキルとしてインストールします。

Safe Refactoringを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストの内容がこのスキルの用途に合ったときに、このスキルを読み込めます。

Safe Refactoringで使えるAIモデルの数に制限はありますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもSafe Refactoringを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Safe Refactoring」は無料ですか?

UnpaidAttentionがGitHubで公開しています。ライセンス条件はリポジトリでご確認ください。ご利用のAIプロバイダーに支払うのは、使用したトークンの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇