メインコンテンツへスキップ
よくあるシナリオですぐに使える例を紹介します。各例では、インストール方法と期待される動作を説明します。

Claude Code 向けフックの設定

Failproof AI は Claude Code のフックシステムを通じて統合されます。failproofai policies --install を実行すると、Claude Code の settings.json にフックコマンドが登録され、すべてのツール呼び出し時に実行されます。
1

failproofai をインストールする

npm install -g failproofai
2

組み込みポリシーをすべて有効にする

failproofai policies --install
3

フックが登録されていることを確認する

cat ~/.claude/settings.json | grep failproofai
PreToolUsePostToolUseNotificationStop イベントのフックエントリが表示されるはずです。
4

Claude Code を起動する

claude
これで、すべてのツール呼び出しに対してポリシーが自動的に実行されます。試しに Claude に sudo rm -rf / を実行するよう指示してみてください — ブロックされます。

Agents SDK 向けフックの設定

Agents SDK を使って開発している場合、同じフックシステムをプログラムから利用できます。
1

プロジェクトに failproofai をインストールする

npm install failproofai
2

エージェントにフックを設定する

エージェントプロセスの作成時にフックコマンドを渡します。フックは Claude Code と同じように、stdin/stdout の JSON を介して起動されます。
failproofai --hook PreToolUse   # 各ツールの実行前に呼び出される
failproofai --hook PostToolUse  # 各ツールの実行後に呼び出される
3

エージェント用のカスタムポリシーを作成する

import { customPolicies, allow, deny } from "failproofai";

customPolicies.add({
  name: "limit-to-project-dir",
  description: "Keep the agent inside the project directory",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    const path = String(ctx.toolInput?.file_path ?? "");
    if (path.startsWith("/") && !path.startsWith(ctx.session?.cwd ?? "")) {
      return deny("Agent is restricted to the project directory");
    }
    return allow();
  },
});
4

カスタムポリシーをインストールする

failproofai policies --install --custom ./my-agent-policies.js

破壊的なコマンドをブロックする

最もよく使われる設定 — エージェントが取り消し不可能な操作を行うのを防ぎます。
failproofai policies --install block-sudo block-rm-rf block-force-push block-curl-pipe-sh
この設定の効果:
  • block-sudo — すべての sudo コマンドをブロック
  • block-rm-rf — 再帰的なファイル削除をブロック
  • block-force-pushgit push --force をブロック
  • block-curl-pipe-sh — リモートスクリプトのシェルへのパイプをブロック

シークレットの漏洩を防ぐ

ツールの出力に含まれる認証情報をエージェントが参照・漏洩するのを防ぎます。
failproofai policies --install sanitize-api-keys sanitize-jwt sanitize-connection-strings sanitize-bearer-tokens
これらは PostToolUse で実行されます。ツールの実行後、エージェントが出力を受け取る前にスクラブ処理が行われます。

エージェントの待機時に Slack へアラートを送信する

通知フックを使って、アイドル状態のアラートを Slack に転送します。
import { customPolicies, allow, instruct } from "failproofai";

customPolicies.add({
  name: "slack-on-idle",
  description: "Alert Slack when the agent is waiting for input",
  match: { events: ["Notification"] },
  fn: async (ctx) => {
    const webhookUrl = process.env.SLACK_WEBHOOK_URL;
    if (!webhookUrl) return allow();

    const message = String(ctx.payload?.message ?? "Agent is waiting");
    const project = ctx.session?.cwd ?? "unknown";

    try {
      await fetch(webhookUrl, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          text: `*${message}*\nProject: \`${project}\``,
        }),
        signal: AbortSignal.timeout(5000),
      });
    } catch {
      // Slack に接続できない場合でもエージェントをブロックしない
    }

    return allow();
  },
});
インストール:
SLACK_WEBHOOK_URL=https://hooks.slack.com/... failproofai policies --install --custom ./slack-alerts.js

エージェントをブランチに固定する

エージェントが他のブランチに切り替えたり、保護されたブランチにプッシュしたりするのを防ぎます。
import { customPolicies, allow, deny } from "failproofai";

customPolicies.add({
  name: "stay-on-branch",
  description: "Prevent the agent from checking out other branches",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    if (ctx.toolName !== "Bash") return allow();
    const cmd = String(ctx.toolInput?.command ?? "");
    if (/git\s+checkout\s+(?!-b)/.test(cmd)) {
      return deny("Stay on the current branch. Create a new branch with -b if needed.");
    }
    return allow();
  },
});

コミット前にテストを要求する

コミット前にテストを実行するようエージェントに促します。
import { customPolicies, allow, instruct } from "failproofai";

customPolicies.add({
  name: "test-before-commit",
  description: "Remind the agent to run tests before committing",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    if (ctx.toolName !== "Bash") return allow();
    const cmd = String(ctx.toolInput?.command ?? "");
    if (/git\s+commit/.test(cmd)) {
      return instruct("Run tests before committing. Use `npm test` or `bun test` first.");
    }
    return allow();
  },
});

本番リポジトリをロックダウンする

プロジェクトレベルの設定をコミットして、チーム全員が同じポリシーを適用できるようにします。 リポジトリに .failproofai/policies-config.json を作成します:
{
  "enabledPolicies": [
    "block-sudo",
    "block-rm-rf",
    "block-force-push",
    "block-push-master",
    "block-env-files",
    "sanitize-api-keys",
    "sanitize-jwt"
  ],
  "policyParams": {
    "block-push-master": {
      "protectedBranches": ["main", "release", "production"]
    }
  }
}
コミットします:
git add .failproofai/policies-config.json
git commit -m "Add failproofai team policies"
failproofai をインストール済みのすべてのチームメンバーに、これらのルールが自動的に適用されます。

その他の使用例

リポジトリの examples/ ディレクトリには以下が含まれています:
ファイル内容
policies-basic.js基本ポリシー — 本番環境への書き込み、force-push、パイプスクリプトのブロック
policies-notification.jsアイドル通知とセッション終了時の Slack アラート
policies-advanced/index.jsトランジティブインポート、非同期フック、PostToolUse の出力スクラブ、Stop イベントの処理