Refactor Safelyのロゴ

Refactor Safely

コミュニティ
0xBakeer
refactor-safely

Changing the structure of existing code without changing its behavior - characterization tests first, small verified steps, renames and extractions, keeping the public API, and proving nothing changed.

概要

公開者0xBakeer
リポジトリTandemLLM
スキル名refactor-safely
スター
75
フォーク
14
同梱ファイル
指示のみ
ライセンスAGPL-3.0
リンク
  • Markdown形式の指示

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

  • どのLLMでも利用可能

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

  • 指示だけで完結

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

  • オープンソース

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

インストール

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

1

TypingMindにインストール

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

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

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

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

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

TypingMindで使用

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

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

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

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

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

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、未使用のスキルにはコストがかかりません。

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

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

スキルの指示

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

Refactoring safely

Rules:

  • Behavior stays the same. If the task also changes behavior, do the refactor first in its own commit, then the behavior change.
  • Before touching anything: run the tests (baseline), and if the area has none, write a characterization test that records the current outputs for 5 to 10 real inputs.
  • One mechanical step at a time: rename, extract function, move file, inline, replace a conditional. Run the tests after each step. Commit each step.
  • Keep the public API: names that other files or users import stay, or get a deprecation shim that forwards.

What goes wrong:

  • A rename missed in a string (a route, a config key, a template) that no type checker sees.
  • The refactor and a bug fix mixed in one diff; the reviewer cannot tell which line changed behavior.
  • Extracting a function that captured a variable by closure and now reads a stale value.

Check: the same test suite passes before and after with the same count, the characterization outputs are byte-identical, and the diff shows only structure (no new conditions, no changed constants).

Done when:

  • Test summary line before and after with equal counts and 0 failed.
  • Characterization outputs (or a checksum of them) equal before and after.
  • git diff --stat and a one-line list of the mechanical steps taken.

Reference

Characterization test:

python
import json, pytest
from mymod import process
CASES = ["", "a", "a,b", "x;y", "ünï", "1,2,3,4,5", "  spaced  ", "a,,b", "\n", "0"]
@pytest.mark.parametrize("inp", CASES)
def test_characterize(inp, snapshot_path=__import__("pathlib").Path("tests/snap_process.json")):
    snaps = json.loads(snapshot_path.read_text()) if snapshot_path.exists() else {}
    out = repr(process(inp))
    if inp not in snaps:
        snaps[inp] = out; snapshot_path.write_text(json.dumps(snaps, indent=1, ensure_ascii=False))
    assert snaps[inp] == out

Run once to record, then it guards. Delete the file only when a behavior change is intended.

Rename across the repo (check first, then apply):

bash
grep -rn "old_name" --include='*.py' --include='*.html' --include='*.yaml' --include='*.md' . | grep -v .venv
grep -rl "old_name" --include='*.py' . | grep -v .venv | xargs sed -i.bak 's/\bold_name\b/new_name/g' && find . -name '*.bak' -delete
pytest -q

Deprecation shim: def old_name(*a, **k): warnings.warn("old_name is deprecated; use new_name", DeprecationWarning, 2); return new_name(*a, **k). Extract function: copy the block into a function with explicit parameters for every variable it reads and a return for every one it writes; replace the block with the call; run; then simplify. Order of steps: rename, extract, move, delete dead code; one commit each, git log --oneline as the record.

More rules:

  • Use the tool for renames when one exists (IDE, rope, ts-morph, gofmt -r); otherwise grep every call site and change them all in one step.
  • Stop when the goal is met. Do not "clean up" neighboring code that nobody asked about.

More pitfalls:

  • Tests rewritten "to match" the new code; they then test nothing.
  • Behavior changed in an edge (None handling, order of a dict) and no test covered it.

よくある質問

AIスキル「Refactor Safely」では何ができますか?

Changing the structure of existing code without changing its behavior - characterization tests first, small verified steps, renames and extractions, keeping the public API, and proving nothing changed.

TypingMindでRefactor Safelyを使うメリットは何ですか?

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

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

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

Refactor Safelyを使えるAIモデルはどれですか?

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

Refactor Safelyは何個のAIモデルで使えますか?

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

AIスキル「Refactor Safely」は無料ですか?

はい。0xBakeerがGitHubでAGPL-3.0ライセンスの下で公開しています。支払いが必要なのは、利用したトークンに対する、ご自身が契約するAIプロバイダーへの料金だけです。

AIスキルとは何ですか?

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

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

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

すべて見る

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

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