跳转到主要内容
适用于常见场景的即用型示例,每个示例均说明安装方式及预期效果。

为 Claude Code 设置 Hook

Failproof AI 通过 hooks 系统与 Claude Code 集成。运行 failproofai policies --install 后,它会在 Claude Code 的 settings.json 中注册 Hook 命令,这些命令将在每次工具调用时触发。
1

安装 failproofai

npm install -g failproofai
2

启用所有内置策略

failproofai policies --install
3

验证 Hook 已注册

cat ~/.claude/settings.json | grep failproofai
你应该能看到 PreToolUsePostToolUseNotificationStop 事件对应的 Hook 条目。
4

运行 Claude Code

claude
策略现在会在每次工具调用时自动执行。可以尝试让 Claude 运行 sudo rm -rf /,该命令将被拦截。

为 Agents SDK 设置 Hook

如果你正在使用 Agents SDK 开发,同样可以通过编程方式使用相同的 Hook 系统。
1

在项目中安装 failproofai

npm install failproofai
2

在 Agent 中配置 Hook

在创建 Agent 进程时传入 Hook 命令。Hook 的触发方式与 Claude Code 中相同——通过 stdin/stdout JSON 进行通信:
failproofai --hook PreToolUse   # 在每次工具调用前触发
failproofai --hook PostToolUse  # 在每次工具调用后触发
3

为 Agent 编写自定义策略

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

拦截破坏性命令

最常见的配置——防止 Agent 执行不可逆的破坏性操作。
failproofai policies --install block-sudo block-rm-rf block-force-push block-curl-pipe-sh
各策略说明:
  • block-sudo - 拦截所有 sudo 命令
  • block-rm-rf - 拦截递归删除文件的操作
  • block-force-push - 拦截 git push --force
  • block-curl-pipe-sh - 拦截将远程脚本通过管道传入 Shell 执行的操作

防止密钥泄露

阻止 Agent 在工具输出中查看或泄露凭证信息。
failproofai policies --install sanitize-api-keys sanitize-jwt sanitize-connection-strings sanitize-bearer-tokens
这些策略在 PostToolUse 时触发——工具执行完毕后,在 Agent 获取输出之前对其进行脱敏处理。

在 Agent 需要关注时发送 Slack 提醒

使用 Notification Hook 将空闲提醒转发至 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 {
      // never block the agent if Slack is unreachable
    }

    return allow();
  },
});
安装方式:
SLACK_WEBHOOK_URL=https://hooks.slack.com/... failproofai policies --install --custom ./slack-alerts.js

将 Agent 限制在指定分支

防止 Agent 切换分支或向受保护分支推送代码。
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();
  },
});

提交前要求运行测试

提醒 Agent 在提交代码前先运行测试。
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基础策略——拦截生产环境写入、强制推送、管道脚本执行
policies-notification.js针对空闲通知和会话结束的 Slack 提醒
policies-advanced/index.js传递式导入、异步 Hook、PostToolUse 输出脱敏、Stop 事件处理