Vai al contenuto principale
Esempi pronti all’uso per scenari comuni. Ogni esempio mostra come installare e cosa aspettarsi.

Configurazione di hook per Claude Code

Failproof AI si integra con Claude Code tramite il suo sistema di hook. Quando esegui failproofai policies --install, registra comandi hook nel file settings.json di Claude Code che si attivano ad ogni chiamata di tool.
1

Installa failproofai

npm install -g failproofai
2

Abilita tutte le policy integrate

failproofai policies --install
3

Verifica che gli hook siano registrati

cat ~/.claude/settings.json | grep failproofai
Dovresti vedere voci di hook per gli eventi PreToolUse, PostToolUse, Notification e Stop.
4

Esegui Claude Code

claude
Le policy ora vengono eseguite automaticamente ad ogni chiamata di tool. Prova a chiedere a Claude di eseguire sudo rm -rf / - verrà bloccato.

Configurazione di hook per Agents SDK

Se stai costruendo con Agents SDK, puoi utilizzare lo stesso sistema di hook a livello programmatico.
1

Installa failproofai nel tuo progetto

npm install failproofai
2

Configura gli hook nel tuo agent

Passa comandi hook quando crei il tuo processo agent. Gli hook si attivano allo stesso modo che in Claude Code - tramite JSON su stdin/stdout:
failproofai --hook PreToolUse   # called before each tool
failproofai --hook PostToolUse  # called after each tool
3

Scrivi una policy personalizzata per il tuo 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

Installa la policy personalizzata

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

Blocca i comandi distruttivi

La configurazione più comune - impedisci agli agent di causare danni irreversibili.
failproofai policies --install block-sudo block-rm-rf block-force-push block-curl-pipe-sh
Cosa fa:
  • block-sudo - blocca tutti i comandi sudo
  • block-rm-rf - blocca l’eliminazione ricorsiva di file
  • block-force-push - blocca git push --force
  • block-curl-pipe-sh - blocca il piping di script remoti a shell

Previeni la perdita di segreti

Impedisci agli agent di vedere o perdere credenziali nell’output dei tool.
failproofai policies --install sanitize-api-keys sanitize-jwt sanitize-connection-strings sanitize-bearer-tokens
Queste si attivano su PostToolUse - dopo che uno strumento viene eseguito, puliscono l’output prima che l’agent lo veda.

Ricevi avvisi Slack quando gli agent hanno bisogno di attenzione

Utilizza l’hook di notifica per inviare gli avvisi di inattività a 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();
  },
});
Installalo:
SLACK_WEBHOOK_URL=https://hooks.slack.com/... failproofai policies --install --custom ./slack-alerts.js

Mantieni gli agent su un branch

Impedisci agli agent di cambiare branch o fare push su quelli protetti.
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();
  },
});

Richiedi test prima dei commit

Ricorda agli agent di eseguire i test prima di fare commit.
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();
  },
});

Proteggi un repository di produzione

Fai il commit di una config a livello di progetto in modo che ogni sviluppatore del tuo team ottenga le stesse policy. Crea .failproofai/policies-config.json nel tuo repository:
{
  "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"]
    }
  }
}
Poi fai il commit:
git add .failproofai/policies-config.json
git commit -m "Add failproofai team policies"
Ogni membro del team che ha failproofai installato raccoglierà automaticamente queste regole.

Altri esempi

La directory examples/ nel repository contiene:
FileCosa mostra
policies-basic.jsPolicy di base - blocca scritture in produzione, force-push, script pipati
policies-notification.jsAvvisi Slack per notifiche di inattività e fine sessione
policies-advanced/index.jsImport transitivi, hook asincroni, scrubbing dell’output PostToolUse, gestione dell’evento Stop