#!/usr/bin/env bun /** * failproofai — main entry point. * * Handles: * --hook Hook event from Claude Code (minimal startup latency) * --version / -v Print version and exit * --help / -h Show usage and exit * --no-color Suppress ANSI color output * policies Manage policies (list / install / uninstall) * (default) Launch production dashboard */ import { realpathSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { version } from "../package.json"; // Resolve the real package root early (following any npm bin symlinks) so that // scripts/launch.ts can locate .next/standalone/server.js correctly regardless // of how bun resolves import.meta.url for dynamically-imported modules. if (!process.env.FAILPROOFAI_PACKAGE_ROOT) { process.env.FAILPROOFAI_PACKAGE_ROOT = resolve( dirname(realpathSync(fileURLToPath(import.meta.url))), ".." ); } if (!process.env.FAILPROOFAI_DIST_PATH) { process.env.FAILPROOFAI_DIST_PATH = resolve( dirname(realpathSync(fileURLToPath(import.meta.url))), "..", "dist" ); } const args = process.argv.slice(2); // Global presentation flag: consume it before aliases, help routing, and // subcommand validation so it works in any position without loading the TUI. if (args.includes("--no-color")) { process.env.NO_COLOR = "1"; for (let i = args.length - 1; i >= 0; i--) { if (args[i] === "--no-color") args.splice(i, 1); } } // ── one noun for policies ────────────────────────────────────────────────── // `policies`, `policy` and `pack` were three commands for one idea, two of them // a single letter apart and doing unrelated things. They are now three // spellings of the same command. Rewritten HERE, above SUBCOMMANDS and every // dispatch below, so the rest of this file mentions only the canonical name and // no branch has to remember the aliases. // // Nothing anybody has typed before stops working — the old spellings are // translated, not rejected — which matters because they are printed in shipped // help output, in this repo's docs, and in the release notes of every pack // published so far. if (args[0] === "p" || args[0] === "policy") args[0] = "policies"; if (args[0] === "pack") { args[0] = "policies"; if (args[1] === "list") { // `pack list` was two commands wearing one name: bare it listed what is // installed here, with an argument it previewed a pack that is not. Those // are different questions, so they are different words now — the bare form // and `show`. const hasSource = args[2] && !args[2].startsWith("-"); if (hasSource) args.splice(1, 1, "show"); else args.splice(1, 1); } else if (args[1] === "build") { // `pack build` produced the release assets and stopped. That is exactly // `publish` with nowhere to publish to, so it IS publish — the local half // of it. `publish` with no --repo does the same thing and says so. args.splice(0, 2, "publish"); } } // Normalize 'configure' / 'setup' → 'config' (aliases), so every later check // (SUBCOMMANDS, dispatch) mentions only the canonical name. if (args[0] === "configure" || args[0] === "setup") args[0] = "config"; // Lightweight telemetry helper for CLI lifecycle events. Lazy-loads to avoid // pulling in the hook-telemetry / telemetry-id modules on the fast --hook path. let _telemetry; let lastSubcommand = null; // When `policy add|remove` is mid-execution we stash the action here so the // top-level catch can emit `cli_policy_add_failure` / `cli_policy_remove_failure` // with the right event name. Mirrors the cli_install_failure / cli_uninstall_failure // pattern below for parity. Cleared back to null after the success track. let lastPolicyAction = null; async function track(name, props) { try { if (!_telemetry) { const [t, i] = await Promise.all([ import("../src/hooks/hook-telemetry"), import("../lib/telemetry-id"), ]); _telemetry = { trackHookEvent: t.trackHookEvent, getInstanceId: i.getInstanceId }; } await _telemetry.trackHookEvent(_telemetry.getInstanceId(), name, props); } catch {} } /** * Exits only once stdout/stderr have actually been flushed. * * Under every agent CLI a hook's stdout is a pipe, and pipe writes in Node * are asynchronous — `process.exit()` terminates without draining what's * still buffered. That stdout carries the decision payload, so a truncated * write silently changes the decision the CLI observes; on the fail-closed * path it would drop the deny reason entirely and leave the CLI with a bare * exit code and no explanation. */ async function exitAfterFlush(code) { const drain = (stream) => new Promise((resolveDrain) => { // A zero-length write's callback still queues behind everything // already buffered, so this resolves after the real output lands. if (stream.writableLength === 0 || stream.destroyed) resolveDrain(); else stream.write("", () => resolveDrain()); }); try { await Promise.all([drain(process.stdout), drain(process.stderr)]); } catch { // Never let a flush problem swallow the exit code itself. } process.exit(code); } // --hook [--cli ] — called by an agent CLI hook; fast path, outside // runCli() because it has its own exit code contract with the calling agent. const hookIdx = args.indexOf("--hook"); if (hookIdx >= 0) { if (!args[hookIdx + 1]) { // The notice shape, written out by hand and never coloured: importing the // kit here — the first module this file imports — moves `tui.ts` ahead of // the builtin policies in the bundle, so its top-level `ctx` renames their // `ctx` parameters and changes the audit cache key (`engineVersion`) for // every user. It also keeps the hook path an agent spawns per tool call // loading nothing new. Written, not `console.error`, which Bun paints red. process.stderr.write( "✕ Missing event type after --hook.\n Run failproofai help hook for the events and agent ids.\n", ); await exitAfterFlush(1); } const eventType = args[hookIdx + 1]; const cliIdx = args.indexOf("--cli"); const cliArg = cliIdx >= 0 ? args[cliIdx + 1] : undefined; // Default cli=claude preserves back-compat for hooks installed before // multi-CLI support landed. const cli = cliArg && ( cliArg === "claude" || cliArg === "codex" || cliArg === "copilot" || cliArg === "cursor" || cliArg === "opencode" || cliArg === "pi" || cliArg === "hermes" || cliArg === "openclaw" || cliArg === "factory" || cliArg === "devin" || cliArg === "antigravity" || cliArg === "goose" ) ? cliArg : "claude"; const scopeIdx = args.indexOf("--agent-scope"); const scopeArg = scopeIdx >= 0 ? args[scopeIdx + 1] : undefined; const scopeHint = scopeIdx < 0 ? undefined : scopeArg === "user" || scopeArg === "project" || scopeArg === "local" ? scopeArg : null; try { const { runtimeAgentSettingsPath } = await import("../src/hooks/agent-roster"); // An empty path is deliberate: it means the originating hook could not // prove which installed scope fired. Forward it rather than omitting the // field and letting the warm worker infer a different profile from its own // environment. const agentSettingsPath = (scopeHint === null ? null : runtimeAgentSettingsPath(cli, process.cwd(), undefined, scopeHint)) ?? ""; // Daemon-aware path — inert (and this whole block skipped) on every // machine until `failproofai config` has installed failproofaid AND // written the daemonConfigured marker (Stage 4). Until then this is // byte-for-byte the same handleHookEvent(...) call below. const { isDaemonConfigured, attemptDaemonHook } = await import("../src/hooks/daemon-client"); if (isDaemonConfigured()) { const { readStdinPayload } = await import("../src/hooks/read-stdin"); const { evaluateHookEvent } = await import("../src/hooks/handler"); const stdinRead = await readStdinPayload(); // Devin runs Claude Code's hooks too. Whether Devin is the agent above // this process can only be read here (the worker's parent is the daemon), // so a Claude-path report call made under Devin is marked for the worker. const { selfReportMarkDevinOrigin } = await import("../src/hooks/self-report-devin"); // Likewise which agent process a Claude Code / Codex report call came // from, so its stdio server can tell this call's row from another // session's (self-report-ancestry.ts). const { selfReportMarkAgentPid } = await import("../src/hooks/self-report-ancestry"); const { canonicalizeEventType } = await import("../src/hooks/handler"); // And this process's PATH (the agent's, unlike the daemon worker's), for // the guard that asks where a bare `grep` really resolves (agent-path.ts). const { markAgentPath } = await import("../src/hooks/agent-path"); const attempt = await attemptDaemonHook({ hookEvent: eventType, cli, stdin: markAgentPath( selfReportMarkAgentPid(cli, canonicalizeEventType(eventType, cli), selfReportMarkDevinOrigin(cli, stdinRead.payload)), ), // The client's own cwd IS the originating CLI session's cwd — this // process is spawned fresh, at that location, by the calling agent // CLI's own hook mechanism. See daemon-client.ts / PROTOCOL.md. cwd: process.cwd(), agentSettingsPath, }); // On a daemon-configured machine the daemon is the ONLY evaluator. Every // way of not getting an answer from it denies, and in-process evaluation // is never reached from this branch — that is what "all enforcement is // routed through the daemon" means, and a fallback here would be a second // policy engine reachable by breaking the first. // // The two failures still differ in what the USER has to do, so they are // told apart in the message and nowhere else: // // protocol-mismatch: a daemon answered, so it is alive; the CLI and the // daemon are different versions, which is what an `npm update` that // has not been followed by `failproofai config` looks like. The remedy // is an upgrade, and naming it is the difference between a one-command // fix and a support ticket. `daemonVersionSkew()` has already been // hinting this on every CLI command. // // unreachable: nothing answered. A stopped service, a deleted socket and // deliberate tampering are indistinguishable from here — and a machine // where stopping one service silently disables every guardrail is not // a guarded machine. let result; if (attempt.ok) { result = attempt.response; } else { const reason = attempt.failure === "protocol-mismatch" ? "failproofaid is running a different protocol version than this CLI, so it " + "cannot evaluate this call. Run `failproofai update` to update the daemon." : "failproofaid could not be reached. This machine is configured to run hooks through it " + "— check the daemon (see `failproofai config`) rather than retrying blindly."; result = await evaluateHookEvent(eventType, cli, stdinRead.payload, { forceDecision: { decision: "deny", reason }, }); } if (result.stdout) process.stdout.write(result.stdout); if (result.stderr) process.stderr.write(result.stderr); await exitAfterFlush(result.exitCode); } const { handleHookEvent } = await import("../src/hooks/handler"); const exitCode = await handleHookEvent(eventType, cli, agentSettingsPath); // handleHookEvent already flushes its own telemetry before returning; this // is the normal, reliable exit. await exitAfterFlush(exitCode); } catch (err) { const msg = err instanceof Error ? err.message : String(err); // The outer fail-closed boundary. This wrote NOTHING to stdout and exited 2, // which is a deny for Claude and Factory's non-Stop events and a silent // ALLOW for the eight CLIs that read their verdict from stdout JSON — // Cursor, Pi, Hermes, OpenClaw, Devin, Antigravity, Goose, and Factory's // Stop. Everything above can land here, INCLUDING the forced-deny call that // handles an unreachable daemon, so the one path whose entire job is to fail // closed failed open instead. // // Emitted BEFORE any telemetry: `flushHookTelemetry` loops until its queue // drains and is not bounded, so draining first meant a stuck send could hold // the verdict back indefinitely — and a verdict that arrives after the agent // has moved on is the same as no verdict. const reason = "failproofai could not evaluate this call and is failing closed. " + `Check the failproofai installation (\`failproofai config\`). Underlying error: ${msg}`; let emitted = false; let denyExitCode = 2; try { // The real evaluator does the shaping, exactly as the unreachable-daemon // path above does — that is what keeps this deny from being inert on some // CLI whose contract nobody remembered here. It can only work if the // handler module is loadable, which is why the fallback below exists. const { evaluateHookEvent } = await import("../src/hooks/handler"); const forced = await evaluateHookEvent(eventType, cli, "", { forceDecision: { decision: "deny", reason }, }); if (forced.stdout) process.stdout.write(forced.stdout); if (forced.stderr) process.stderr.write(forced.stderr); emitted = true; denyExitCode = forced.exitCode; } catch { // Last ditch: the module that knows each CLI's exact deny shape is itself // the thing that just failed, so this emits the union of them — every CLI // reads only the keys it knows and ignores the rest. Deliberately NOT a // per-CLI table: a second copy of those twelve contracts would drift from // the real one, and this runs only when the install is already broken. // Imperfect enforcement beats the zero bytes that were written before. try { process.stdout.write( JSON.stringify({ decision: "block", reason, permission: "deny", followup_message: reason, hookSpecificOutput: { hookEventName: eventType, permissionDecision: "deny", permissionDecisionReason: reason, }, }), ); emitted = true; } catch {} } if (!emitted) console.error(`Unexpected error: ${msg}`); else console.error(`[failproofai] failing closed: ${msg}`); await track("hook_dispatch_error", { event_type: eventType, cli, error_type: err instanceof Error ? err.name : "unknown", }); // handleHookEvent threw before its own flush ran, so any events it fired // with `void trackHookEvent(...)` are still in flight — drain them (plus the // hook_dispatch_error above) before exiting so they aren't dropped. try { const { flushHookTelemetry } = await import("../src/hooks/hook-telemetry"); await flushHookTelemetry(); } catch {} // `exitAfterFlush`, not a bare `process.exit`: this was the one exit in // `--hook` handling that skipped it, so under load `process.exit` could // truncate the very bytes carrying the deny. await exitAfterFlush(denyExitCode); } } // mcp [--agent ] — the Self Reports MCP server over stdio, spawned // by an agent that registered it. Outside runCli() like --hook: stdout is the // JSON-RPC stream, so nothing else (layout notices, onboarding, the install // report) may run or print first, and it must work with no daemon. const SELF_REPORT_MCP_HELP = { name: "mcp", usage: [["failproofai mcp [--agent ]", "Serve the Self Reports tool over stdio MCP"]], options: [ ["--agent ", "The agent this server was registered for"], ["", "claude, codex, copilot, cursor, opencode, pi,"], ["", "hermes, openclaw, factory, devin, antigravity, goose"], ], }; if (args[0] === "mcp") { if (args.includes("--help") || args.includes("-h")) { await printHelp(SELF_REPORT_MCP_HELP); await exitAfterFlush(0); } const agentIdx = args.indexOf("--agent"); const agentArg = agentIdx >= 0 ? args[agentIdx + 1] : undefined; const stray = args.slice(1).find((a, i) => a !== "--agent" && args[i] !== "--agent"); if ((agentIdx >= 0 && (!agentArg || agentArg.startsWith("-"))) || stray) { process.stderr.write( `✕ ${stray ? `Unexpected argument: ${stray}.` : "Missing agent name after --agent."}\n Run failproofai help mcp for usage.\n`, ); await exitAfterFlush(1); } try { const { runSelfReportMcpServer } = await import("../src/hooks/self-report-mcp"); await runSelfReportMcpServer({ agent: agentArg }); await exitAfterFlush(0); } catch (err) { process.stderr.write(`✕ failproofai mcp: ${err instanceof Error ? err.message : String(err)}\n`); await exitAfterFlush(1); } } /** * Centralised error handler for all CLI subcommands. * CliError → clean message, no stack trace, exit exitCode (1 or 2) * Error → unexpected; shows message only, exits 2 */ /** * Every `--help` in this file, and the index itself, drawn by ONE renderer. * * They used to be twelve template literals: `USAGE` on one screen and `Usage:` * on the next, a description column hand-counted per screen, no version on any * of them, and no colour on any of them while the index they were reached from * had all three. The words are still each screen's own — this owns the shape, * so a screen cannot drift out of the family without editing the family. * * Drawn by `screenKit().helpPage` at the terminal's width; descriptions are * prose and may be shortened on a terminal, never in a pipe. */ /** * Lines a module ALREADY laid out with the kit — `harness`, `publish`, * `policies add`, the pack lane — printed with the outer margins every other * screen gets. They used to go out one `console.log` at a time, which is the * one thing `printBlock` exists to own: the block arrived flush against the * prompt above it while every neighbouring command was breathing. */ async function printLines(lines, ok = true) { const { printBlock } = await import("../src/hooks/tui"); printBlock(ok ? process.stdout : process.stderr, lines); } /** * What an action surface prints when it is done: a heading naming the command, * then the lines it produced, indented and margined like every other screen. * * The modules keep returning bare facts — `runFlushCommand` says "2 batches * spooled", not how to draw it — so the presentation lives in exactly one * place instead of five, and a flush report and a `policies` listing stop * looking like output from two different programs. * * Lines that already carry their own leading space are passed through: those * are sub-items a module laid out on purpose, and re-wrapping them would * flatten the structure they were expressing. */ async function printReport(command, lines, opts = {}) { const { printBlock, optsFor, screenKit, paint: palette } = await import("../src/hooks/tui"); const { reportBody } = await import("../src/hooks/report-body"); const ok = opts.ok !== false; const stream = ok ? process.stdout : process.stderr; const o = optsFor(stream); const kit = screenKit(o); const c = palette(o.color); // `\`like this\`` becomes pink, and loses the backticks. These messages name // the command to run next more often than not, and pink is what you type // everywhere. Applied AFTER wrapping, because an escape sequence has no // width and colouring first would make every wrap measure the wrong length. // // A line that opens on a state glyph gets that glyph in its role colour, the // way a notice draws it: `update` reports a daemon that needs root that way. const glyphRoles = { "✓": c.guide, "▲": c.warn, "✕": c.err }; const paint = (line) => { const typed = (line.match(/`/g) || []).length % 2 === 0 ? line.replace(/`([^`]+)`/g, (_, cmd) => c.pink(cmd)) : line; const lead = /^(\s*)([✓▲✕]) /.exec(typed); return lead ? `${lead[1]}${glyphRoles[lead[2]](lead[2])}${typed.slice(lead[1].length + 1)}` : typed; }; // The 2026-10 shape: the header names the command, top-level lines start at // column 0, and lines a module indented on purpose keep their indent. A // failed run opens with ✕, so it reads as the failure it is — unless its // lines already mark their own state; see report-body.ts. const name = command.charAt(0).toUpperCase() + command.slice(1); const body = reportBody(lines, { ok, width: o.cols - 2 }).map(paint); printBlock(stream, [kit.header(opts.meta ? `${name} · ${opts.meta}` : name), "", ...body]); } async function printHelp(spec) { const { screenKit, optsFor, printBlock } = await import("../src/hooks/tui"); // Descriptions are prose and may be shortened to fit a terminal; nothing is // ever cut in a pipe, and a command, flag or path is never cut anywhere. const kit = screenKit({ ...optsFor(process.stdout), fit: !!process.stdout.isTTY }); printBlock(process.stdout, kit.helpPage(spec)); } async function runCli() { // One page for `policies --help` and `policies add|remove|show --help`: it // carries every flag of every policies lane, so `help policies add` lands on // the right page with no routing of its own. Defined here, inside runCli, // so the bundle gains no top-level name. const POLICIES_HELP = { name: "policies", usage: [ ["failproofai policies [--list]", "List every policy and whether it is on"], ["failproofai policies add [name | owner/repo]", "Turn one on, install a pack, or pick from a list"], ["failproofai policies remove [name | pack-id]", "Turn one off, uninstall a pack, or pick from a list"], ["failproofai policies show ", "See what a pack holds"], ["failproofai policies --install [names...]", "Install hooks and turn on any policies named"], ["failproofai policies --uninstall [names...]", "Turn named policies off; with none, remove hooks"], ], options: [ ["-i, -u", "Short for --install and --uninstall"], ["--policy, --only ", "Install only these policies from a pack"], ["--category ", "Install only these categories from a pack"], ["--all", "List every policy; with a pack, install all of it"], ["--cli ", "Apply to these agents only"], ["", "claude, codex, copilot, cursor, opencode, pi,"], ["", "hermes, openclaw, factory, devin, antigravity, goose"], ["--scope ", "user, project, local (default user); all when removing"], ["--beta", "Include beta policies"], ["--no-color", "Plain output, same as NO_COLOR=1, for any command"], ["-c, --custom ", "With --install, add a custom policy file (repeatable)"], ["", "--uninstall -c removes every custom policy file"], ["--releases", "With show: list its releases, and which one is here"], ], examples: [ "failproofai policies add FailproofAI/policies", "failproofai policies add FailproofAI/policies --category git", "failproofai policies remove block-sudo", ], }; // `--cli` in the three policies lanes: one or more space-separated agents, // optionally repeated, read until the next flag or the first word that is // not an agent, since a policy name may follow the list. One wording for // every mistake, built from INTEGRATION_TYPES: the message this replaced // named eight of the twelve agents, and said "Missing" for a typo too. async function readCliFlag(list) { const { INTEGRATION_TYPES } = await import("../src/hooks/types"); const agents = new Set(INTEGRATION_TYPES); const cliFlagValues = []; const cliConsumedIdxs = new Set(); for (let idx = 0; idx < list.length; idx++) { if (list[idx] !== "--cli") continue; let consumed = 0; for (let j = idx + 1; j < list.length && !list[j].startsWith("-") && agents.has(list[j]); j++) { cliFlagValues.push(list[j]); cliConsumedIdxs.add(j); consumed++; } if (consumed > 0) continue; const half = Math.ceil(INTEGRATION_TYPES.length / 2); const everyAgent = `Use any of: ${INTEGRATION_TYPES.slice(0, half).join(", ")},\n` + ` ${INTEGRATION_TYPES.slice(half).join(", ")}`; const given = list[idx + 1]; if (given === undefined || given.startsWith("-")) { throw new CliError(`Missing agent name after --cli.\n${everyAgent}`); } const { nearestName } = await import("../src/hooks/did-you-mean"); const near = nearestName(given, INTEGRATION_TYPES); throw new CliError(`Not an agent: ${given}${near ? ` (did you mean ${near}?)` : "."}\n${everyAgent}`); } return { cliFlagValues, cliConsumedIdxs }; } // --help / -h (only when not inside a subcommand that handles its own --help) // `update` and `migrate` were missing here, so `failproofai update --help` // exited 1 with "Unexpected argument" — both commands had no reachable help // at all, only the paragraph in the top-level dump that this rewrite moved. // `help` and `publish` are new. `policy` and `pack` are canonicalized to // `policies` above and never reach this list. const SUBCOMMANDS = ["policies", "audit", "config", "uninstall", "backfill", "flush", "harness", "otel", "jev", "self-report", "publish", "update", "migrate", "help"]; // ── help ───────────────────────────────────────────────────────────────── // // The index and the reference manual used to be the same document: 152 lines, // six screens at 80x24, with every flag of every command inlined. That is a // help tier collapse — the thing you read to find a command was the thing you // read to use one — and the cost fell on the person who knew least. // // Now: ONE screen of what exists, and `help ` for everything else. // `help ` is literally ` --help`, dispatched below, so there // is exactly one copy of each command's documentation and the two spellings // cannot drift. const helpTopic = args[0] === "help" ? args[1] : undefined; if (args[0] === "help" && helpTopic) { // `--hook` is the entry point an agent CLI spawns per tool call. It is // documented in NO help output — only a module docblock and one error // string — so it gets a topic here rather than a line on the index, where a // machine-facing flag would only take space from the human-facing commands. if (helpTopic === "hook") { await printHelp({ name: "hook", usage: [["failproofai --hook [--cli ]", "Called by your agents on every tool call"]], options: [ ["--hook ", "PreToolUse, PostToolUse, UserPromptSubmit, Stop and more"], ["--cli ", "claude (default), codex, copilot, cursor, opencode, pi,"], ["", "hermes, openclaw, factory, devin, antigravity, goose"], ], }); process.exit(0); process.exit(0); } // `mcp` is machine-facing too (an agent spawns it), so like --hook it is a // help topic rather than a row on the index. if (helpTopic === "mcp") { await printHelp(SELF_REPORT_MCP_HELP); process.exit(0); } // Canonicalize the topic the same way a typed command is canonicalized, so // `help pack` and `help policy` answer instead of erroring. const canonical = helpTopic === "p" || helpTopic === "policy" || helpTopic === "pack" ? "policies" : helpTopic === "configure" || helpTopic === "setup" ? "config" : helpTopic; if (!SUBCOMMANDS.includes(canonical)) { throw new CliError( `There is no help for '${helpTopic}'.\n` + "Run `failproofai help` to see every command.", ); } // `policies add|remove|show` has its own help, distinct from the listing's. args.splice(0, 2, canonical, ...(canonical === "policies" && !args[2] ? [] : []), "--help"); } if ( args[0] === "help" || ((args.includes("--help") || args.includes("-h")) && !SUBCOMMANDS.includes(args[0])) ) { const extraArgs = args.filter((a) => a !== "--help" && a !== "-h" && a !== "help"); if (extraArgs.length > 0) { throw new CliError(`Unexpected argument: ${extraArgs[0]}.\nRun \`failproofai help\` for usage.`); } // The index and every ` --help` page are built from the same kit, // so a row added here cannot end up in a different dialect from the page it // points at. Colour is decoration and never meaning: the column already says // which half is a command, so this reads the same under NO_COLOR, piped to a // file, or on a terminal that has never heard of 24-bit. { // The index, in the design's shape: what to do first, then every command by // what it is for. Composed here from the kit rather than a builder in tui.ts, // so the bundle gains no top-level name (see the audit-cache note there). const { screenKit, optsFor, printBlock } = await import("../src/hooks/tui"); const kit = screenKit({ ...optsFor(process.stdout), fit: !!process.stdout.isTTY }); // The command sits in the SECOND column here, so these rows never go // through rows()' description slot, which is the one shortened to fit. const steps = [ ["Set up this machine", "failproofai config"], ["Turn on policies", "failproofai policies add FailproofAI/policies"], ["See what your agents did", "failproofai"], ]; const section = (heading, entries) => [ "", kit.head(heading), ...kit.rows(entries.map(([name, description]) => [name === "(no args)" ? name : kit.cmd(name), description]), 12), ]; await printBlock(process.stdout, [ kit.header("Guardrails for coding agents on this machine"), "", kit.head("Get started"), ...steps.map(([label, command], i) => ` ${i + 1} ${label.padEnd(27)} ${kit.cmd(command)}`), ...section("Set up", [ ["config", "Connect to cloud or use open source, and pick agents to trace"], ["update", "Finish an npm upgrade: migrate this machine and match the daemon"], ["uninstall", "Remove hooks and the daemon from this machine"], ]), ...section("Enforce", [ ["policies", "List policies, turn them on or off, install packs"], ["publish", "Ship your own policies as a pack anyone can install"], ["jev", "Have Jev judge tool calls on top of the regex policies"], ]), ...section("Observe", [ ["(no args)", "Open the policy dashboard on localhost:8020"], ["audit", "Scan your agents' history for risky and wasteful patterns"], ["otel", "Opt in to OpenTelemetry from agents and apps"], ["self-report", "Let agents report to you when they are stuck (on by default)"], ]), ...section("Less often", [ ["harness", "Collect sessions from extra locations per agent"], ["flush", "Send queued events to cloud now"], ["backfill", "Re-send history the collector already read"], ["migrate", "Bring ~/.failproofai up to this version's layout"], ]), "", kit.head("Examples"), ...kit.rows([ [kit.cmd("failproofai config --status"), "Check the daemon, cloud and pauses"], [kit.cmd("failproofai policies add block-sudo"), "Turn on one policy"], [kit.cmd("failproofai policies show acme/guards"), "Preview a pack before installing it"], ]), "", `Run ${kit.cmd("failproofai help ")} for its options, ${kit.cmd("failproofai -v")} for the version.`, "docs.befailproof.ai · discord.befailproof.ai · --no-color: plain output", ]); } process.exit(0); } // --version / -v if ((args.includes("--version") || args.includes("-v")) && !SUBCOMMANDS.includes(args[0])) { const extraArgs = args.filter((a) => a !== "--version" && a !== "-v"); if (extraArgs.length > 0) { throw new CliError(`Unexpected argument: ${extraArgs[0]}.\nRun \`failproofai help\` for usage.`); } console.log(version); process.exit(0); } // First-run onboarding — before any subcommand runs its own work. // // On a machine that has never been set up, the first thing the user typed is // almost never the thing they need first, so we run the wizard and then let // their original command proceed. Exemptions matter more than the rule: // // --hook never reaches here (it exits above) — it runs on every // tool call, and a wizard on that path would hang an agent // --version/--help answering "what is this" must not require setup // config IS the wizard // policies/policy explicit configuration actions. Intercepting these would // fight the intent the user just stated, and would break // non-interactive scripts that call them to do setup. // // `maybeFirstRunConfigure` is itself a no-op on a configured machine, on a // non-TTY, and under sudo — so this is a cheap check, not a second gate. // Layout check, before onboarding decides anything. A home written by an // older layout is reset here — visibly, in a real command the user typed — // rather than from a hook, which runs unattended once per tool call. A home // written by a NEWER layout stops the command instead: that data is fine and // an upgrade would read it, so deleting it would destroy something // recoverable. // // `audit --scheduled` is the exception, and it takes the HOOK's branch: it is // spawned by failproofaid on a timer, so "visibly, in a real command the user // typed" is exactly what it is not. `checkLayoutForCli()` deletes // config.toml and credentials.toml (see `resettablePaths`), so letting a // background process reach it would silently revoke a user's // `[telemetry] enabled = false`, erase their cloud enrolment, and switch off // `[audit] auto` — the setting that scheduled the run — with the explanation // going only to the service journal. Reachable on every machine at the next // LAYOUT_VERSION bump, when a home carrying `auto = true` is by definition // stale. Verified live: one scheduled tick took a home's whole config. // // `--help` / `--version` take the same exemption `first-run-gate.ts` gives // them, and for a stronger reason. Those two answer "what is this / how do I // use it"; the subcommands that parse their own help (`policies --help`) fall // past the help block above and reach here, so without this a user typing // `failproofai policies --help` had their home reset by a question. The // adjacent, far less destructive first-run gate exempted help from the start. { const isHelpOrVersion = args.includes("--help") || args.includes("-h") || args.includes("--version") || args.includes("-v"); // `self-report sync` takes it too: failproofaid runs it (`--quiet`) at // startup and after every settings change, unattended, exactly like a // scheduled audit. A person running it by hand on a stale home is told to // run `failproofai config`, which migrates visibly. const unattended = (args[0] === "audit" && args.includes("--scheduled")) || (args[0] === "self-report" && args[1] === "sync" && !isHelpOrVersion); if (unattended) { // NOT `layoutWarningForHook` — that one answers the hook's question // ("are policies unenforced?") and deliberately stays quiet when the // global config is readable. This gate is about the LAYOUT, and must // fire whenever completing the run would reset the home. const { layoutBlockerForScheduledRun } = await import("../src/hooks/fp-reset"); const warning = layoutBlockerForScheduledRun(); if (warning) { // Exit 1, not 75: 75 means "another audit holds the lock" and is retried // in fifteen minutes, which here would just re-warn four times an hour // forever. A stale home is a real failure that `failproofai config` // fixes, so it is reported and retried at the ordinary cadence. console.error(warning); process.exit(1); } } else if (!isHelpOrVersion && args[0] !== "migrate" && args[0] !== "update") { // `migrate` and `update` are exempt because they ARE this step, and letting // it run first breaks them both. `migrate --dry-run` promises to change // nothing and print what would happen — with the check ahead of it, the home // was already migrated by the time the subcommand looked, so it reported // "nothing to migrate" on every stale machine it was ever pointed at, which // is the one answer a dry run must never give wrongly. Caught by a smoke // test on a seeded layout-2 home, not by a unit test: nothing below the CLI // entry point can see this ordering. const { checkLayoutForCli } = await import("../src/hooks/fp-reset"); // Setup brings the daemon to this version itself, so it is not told to // run `update` first; its other modes (--status, --pause, …) still are. const setupRun = args[0] === "config" && !["--status", "--pause", "--resume", "--connect", "--disconnect"].some((f) => args.includes(f)) && !(args.includes("--machine-label") && !["--token", "--oss", "--agents"].some((f) => args.includes(f))); const check = await checkLayoutForCli({ daemonUpdatedByCommand: setupRun }); // Written rather than `console.error`, which Bun paints red on a terminal. if (check.lines.length > 0) process.stderr.write(`${check.lines.join("\n")}\n`); if (check.fatal) await exitAfterFlush(1); // `check.didReset` is deliberately not read. It used to force the wizard // below; see the note at `shouldOfferFirstRun` for why a migrated machine // no longer needs setup re-run, and `didReset` in `fp-reset.ts` for what // the field means now. } } // Report a fresh install / upgrade. AFTER the layout check above, never // before: this writes the `last-version` marker, and while that file lived at // the root of the home it was one of the landmarks `detectLayout()` reads as // "layout 1". Running first meant the CLI created the file and then read it // back as evidence of an old layout, so every genuinely fresh machine had its // very first command open with "failproofai reorganised … Removed 1 item(s) // from the old layout." Nothing was lost — there was nothing there — but // training every new user to ignore that banner is expensive given what it // says on a real layout-1 home. The file has also moved under `state/`, so // the two are independent now; the order is kept because it is the correct // one regardless. // // Deliberately not at module scope: everything above the CLI entry is the // --hook fast path, which runs on every tool call. No-ops after the first run // on a given version. try { const { maybeReportInstall } = await import("../lib/install-check"); await maybeReportInstall(version); } catch { // never block a command on reporting } // True once the setup wizard has run in this process. It opens with the // logomark, so the dashboard launch at the end must not draw a second one. let setupWizardRan = false; const { shouldOfferFirstRun } = await import("../src/hooks/first-run-gate"); // `|| layoutWasReset` stood here, with `force: layoutWasReset` below, because a // migration used to delete the home's policy config while leaving the agent // CLIs' settings files alone — so `isConfigured()` read true off // `hasGlobalHooks` and setup was skipped, leaving hooks firing against no // policies, silently and permanently. // // The migration keeps that file now (`HOME_CLASSES` classes it `user-typed`), // along with `config.json` and `credentials.json`, so a migrated machine is // configured in fact and not merely in appearance. Forcing the wizard here // would open an interactive prompt with nothing to answer — and on a fleet box, // a CI runner or a headless gateway, nobody to answer it. A home that never // finished setup still gets here on its own: `isConfigured()` is false for it. // See `didReset` in `fp-reset.ts`. if (shouldOfferFirstRun(args)) { try { const { maybeFirstRunConfigure } = await import("../src/hooks/configure-wizard"); // `audit` runs its own scan immediately after this returns; firing the // post-setup audit too would scan the whole history twice in a row. setupWizardRan = await maybeFirstRunConfigure({}, { postSetupAudit: args[0] !== "audit" }); } catch { // Onboarding is never allowed to block the command the user actually typed. } } // backfill [--since ] [--dry-run] // // Hands off to the daemon rather than doing the work: the cursors it rewinds // are held in memory by the RUNNING collector, which would write them back // over. Every precondition a person can get wrong is still checked HERE, // synchronously, because reporting success and leaving the real failure in the // journal is what already cost twenty minutes on a live machine. // `failproofai self-report sync [--quiet]`: add or remove failproofai's // Self Reports MCP server in every agent with failproofai hooks, from // ~/.failproofai/self-report.json. The daemon runs it with --quiet whenever // that file's `enabled` (or port) changes; it is idempotent. // Dispatched ahead of the status|enable|disable|test branch below, which // lists it on the `self-report --help` page. if (args[0] === "self-report" && args[1] === "sync") { const subArgs = args.slice(2); if (subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "self-report sync", usage: [["failproofai self-report sync [--quiet]", "Register or remove the Self Reports MCP server (failproofai_local) in each hooked agent"]], options: [["--quiet", "Print nothing; the exit code says whether every agent succeeded"]], optionsCol: 12, }); process.exit(0); } const unknown = subArgs.find((a) => a !== "--quiet"); if (unknown) { throw new CliError(`Unexpected argument: ${unknown}.\nRun \`failproofai self-report sync --help\` for usage.`); } lastSubcommand = "self-report"; const { syncSelfReportMcp, selfReportSyncLines } = await import("../src/hooks/self-report-install"); const results = await syncSelfReportMcp({ quiet: true }); const failed = results.some((r) => r.status === "error"); if (!subArgs.includes("--quiet")) { const lines = selfReportSyncLines(results); await printLines(lines.length > 0 ? lines : ["Self Reports MCP registration is already up to date."], !failed); } else { // The daemon runs this with --quiet and logs our stdout and stderr: what // changed (one line), and each agent that failed, so "exit 1" is never // all its journal says. if (process.env.FAILPROOFAI_SELF_REPORT_SYNC_LOG === "stdout") { const { selfReportSyncLogLine } = await import("../src/hooks/self-report-install"); const changed = selfReportSyncLogLine(results.filter((x) => x.status !== "error" && x.status !== "conflict")); if (changed) process.stdout.write(`${changed}\n`); } for (const r of results.filter((x) => x.status === "error")) { process.stderr.write(`${r.agent}: not updated in ${r.path || "its config"}: ${r.detail ?? "error"}\n`); } } lastSubcommand = null; await exitAfterFlush(failed ? 1 : 0); return; } if (args[0] === "flush") { const subArgs = args.slice(1); if (subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "flush", usage: [["failproofai flush [options]", "Send queued events to cloud now"]], options: [ // The queue on THIS machine: a batch set aside after repeated failures // leaves it empty without having been delivered. ["--wait", "Wait until the queue is empty"], ["--timeout ", "How long --wait waits (default 60)"], ], optionsCol: 20, }); process.exit(0); } const KNOWN = new Set(["--wait", "--timeout"]); const unknown = subArgs.find( (a, i) => a.startsWith("-") && !KNOWN.has(a) && subArgs[i - 1] !== "--timeout", ); if (unknown) { throw new CliError(`Unexpected argument: ${unknown}.\nRun \`failproofai flush --help\` for usage.`); } let timeoutSecs; const tIdx = subArgs.indexOf("--timeout"); if (tIdx >= 0) { const raw = subArgs[tIdx + 1]; if (!raw || raw.startsWith("-")) { throw new CliError("Missing value after --timeout.\nFor example: `failproofai flush --wait --timeout 60`"); } const n = Number(raw); // Rejected rather than coerced: NaN would silently become "wait forever // or not at all" depending on the comparison, and neither is what was asked. if (!Number.isFinite(n) || n <= 0) { throw new CliError(`Could not read --timeout ${raw}.\nGive a number of seconds, for example \`--timeout 60\``); } timeoutSecs = n; } lastSubcommand = "flush"; const { runFlushCommand } = await import("../src/hooks/flush-cli"); const result = await runFlushCommand({ wait: subArgs.includes("--wait"), timeoutSecs }); await printReport("flush", result.lines, { ok: result.exitCode === 0 }); await track("cli_flush", { ok: result.exitCode === 0, waited: subArgs.includes("--wait"), pending: result.pending, }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } if (args[0] === "backfill") { const subArgs = args.slice(1); if (subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "backfill", usage: [["failproofai backfill [options]", "Re-send history the collector already read"]], options: [ ["--since ", "How far back: 30d, 6m, 1y or YYYY-MM-DD (default 30d)"], ["--agents ", "Only these traced agents (default: every traced agent)"], ["--dry-run", "Show what would be sent, send nothing"], ], examples: ["failproofai backfill --since 6m", "failproofai backfill --agents claude,codex --dry-run"], optionsCol: 20, }); process.exit(0); } const KNOWN = new Set(["--since", "--dry-run", "--agents"]); const TAKES_VALUE = new Set(["--since", "--agents"]); // Every word is checked, not only the ones that start with "-": a bare // `backfill 6m` used to be ignored, and ran the default 30 days instead of // the six months that were typed. const unknown = subArgs.find((a, i) => !KNOWN.has(a) && !TAKES_VALUE.has(subArgs[i - 1])); if (unknown) { const looksLikeWindow = /^\d+[dmy]$/.test(unknown) || !Number.isNaN(Date.parse(unknown)); throw new CliError( `Unexpected argument: ${unknown}.\n` + (looksLikeWindow ? `Did you mean \`failproofai backfill --since ${unknown}\`?` : "See its options: `failproofai backfill --help`"), ); } let agents; const agentsIdx = subArgs.indexOf("--agents"); if (agentsIdx >= 0) { const raw = subArgs[agentsIdx + 1]; if (!raw || raw.startsWith("-")) throw new CliError("Missing value after --agents."); agents = raw.split(",").map((a) => a.trim()).filter(Boolean); } let sinceMs; const sinceIdx = subArgs.indexOf("--since"); if (sinceIdx >= 0) { const raw = subArgs[sinceIdx + 1]; if (!raw || raw.startsWith("-")) throw new CliError("Missing value after --since."); // `30d` / `6m` / an ISO date. Rejected rather than guessed at: silently // reading an unparseable window as "the default" would send a different // amount of history than was asked for, and nothing would say so. const rel = /^(\d+)([dmy])$/.exec(raw); if (rel) { const n = Number(rel[1]); const days = rel[2] === "d" ? n : rel[2] === "m" ? n * 30 : n * 365; sinceMs = Date.now() - days * 24 * 60 * 60 * 1000; } else { const t = Date.parse(raw); if (Number.isNaN(t)) { throw new CliError(`Could not read --since ${raw}. Use 30d, 6m, or YYYY-MM-DD.`); } sinceMs = t; } } lastSubcommand = "backfill"; const { runBackfillCommand } = await import("../src/hooks/backfill-cli"); const { optsFor } = await import("../src/hooks/tui"); const result = await runBackfillCommand({ sinceMs, since: sinceIdx >= 0 ? subArgs[sinceIdx + 1] : undefined, dryRun: subArgs.includes("--dry-run"), agents, // Drawn for stdout. A refusal goes to stderr, and printBlock drops the // colour there when stderr cannot show it. render: optsFor(process.stdout), }); await printLines(result.lines, result.exitCode === 0); await track("cli_backfill", { ok: result.exitCode === 0, dry_run: subArgs.includes("--dry-run"), explicit_since: sinceIdx >= 0, agents_named: agents ? agents.length : 0, }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } // harness list | add-path | remove-path // // Extra capture paths per harness. Writes ~/.failproofai/config.toml and // nothing else — no root, no daemon call — so it works on a machine whose // daemon is stopped, which is when someone is most likely to be fixing what // it captures. // There is no `pack` branch here on purpose. `pack`, `policy` and `p` are // rewritten to `policies` at the top of this file, above every dispatch, so // `args[0]` can never be "pack" by the time this runs. One lived here anyway // for a while, unreachable, carrying a sixty-line help screen that still // advertised `pack list`, `pack add` and `pack build` — three spellings this // CLI no longer has — in a third heading dialect nothing else used. Nobody // could reach it to notice. The pack lane is entered from // `policies add|remove|show` below, which is the only door it has. if (args[0] === "otel") { const subArgs = args.slice(1); if (!subArgs.length || subArgs.includes("--help") || subArgs.includes("-h")) { const verb = ["status", "enable", "disable", "env"].includes(subArgs[0]) ? subArgs[0] : null; const usages = { status: "failproofai otel status", enable: "failproofai otel enable [--local] [--no-content] [--yes]", disable: "failproofai otel disable ", env: "failproofai otel env [--service ] [--local]", }; const options = { enable: [ ["--local", "Use the loopback OTLP relay. Automatic for Copilot."], ["--no-content", "Do not capture prompt text or tool details."], ["--yes", "Accept switching this agent from transcript upload to OTEL."], ], env: [ ["--service ", "Service name for env (default: my-agent)."], ["--local", "Use the loopback OTLP relay."], ], status: [], disable: [], }; const acceptedOptions = verb ? options[verb] : [...options.enable, options.env[0]]; await printHelp({ name: verb ? `otel ${verb}` : "otel", usage: (verb ? [usages[verb]] : Object.values(usages)).map(usage => [ usage, verb === "enable" || verb === "disable" || !verb ? "Agents: claude, codex, copilot, all" : "", ]), options: acceptedOptions, examples: verb === "status" ? ["failproofai otel status"] : verb === "disable" ? ["failproofai otel disable all"] : verb === "env" ? ["failproofai otel env --service my-agent --local"] : ["failproofai otel enable claude", "failproofai otel enable codex --local --no-content", "failproofai otel disable all"], }); process.exit(0); } lastSubcommand = "otel"; const { runOtelCommand } = await import("../src/hooks/otel-cli"); const { otelReportLines } = await import("../src/hooks/otel-output"); const result = await runOtelCommand(subArgs, { onEvent: track }); // env is shell input and must not be wrapped in a styled report. if (subArgs[0] === "env" && result.exitCode === 0) console.log(result.lines.join("\n")); else await printReport(`otel ${subArgs[0]}`, otelReportLines(subArgs[0], result), { ok: result.exitCode === 0 }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } if (args[0] === "harness") { const subArgs = args.slice(1); if (subArgs.length === 0 || subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "harness", usage: [ ["failproofai harness list [agent]", "Show extra capture paths"], ["failproofai harness add-path [label=]", "Also capture from here"], ["failproofai harness remove-path ", "Stop capturing from here"], ], // For hermes, goose, opencode and devin the extra path is a database file. examples: [ "failproofai harness add-path claude work=/srv/team/.claude/projects", "failproofai harness add-path hermes prod=/srv/hermes-prod/state.db", "failproofai harness remove-path claude work", ], }); process.exit(0); } lastSubcommand = "harness"; const { runHarnessCommand } = await import("../src/hooks/harness-cli"); const result = runHarnessCommand(subArgs); await printLines(result.lines, result.exitCode === 0); await track("cli_harness", { ok: result.exitCode === 0, // The subcommand only — never the harness name, the label or the path. // A path is a value the user typed, and `enterprise-docs/product-analytics.md` // promises we send shape, never value. sub: ["list", "add-path", "remove-path"].includes(subArgs[0]) ? subArgs[0] : "unknown", }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } // self-report status | enable | disable | test // // Self Reports: the `report` tool agents call when they are stuck. On by // default. On a machine managed by FailproofAI Cloud the setting comes from // Cloud and enable/disable refuse; otherwise they write config.json's // `selfReport` and ~/.failproofai/self-report.json, then update the agents' // MCP entries (disable removes them and stops recording). if (args[0] === "self-report") { const subArgs = args.slice(1); if (subArgs.length === 0 || subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "self-report", usage: [ ["failproofai self-report status [--json]", "Whether it is on, the agents it serves, and any that need a fix"], ["failproofai self-report enable [options]", "Turn it back on for this machine, or change its categories"], ["failproofai self-report disable", "Turn it off: removes the MCP entries and stops recording"], ["failproofai self-report test --agent ", "Send one test report end to end"], ["failproofai self-report sync [--quiet]", "Update each hooked agent's failproofai_local MCP entry (the daemon runs this)"], ], options: [ ["--categories-file ", "JSON list of {name, description}; replaces the defaults"], ["--guidance ", "Extra instructions appended to the tool description"], ["--agent ", "With test: the agent to send it as"], ["--verbose", "With status: paths, endpoints, categories and each agent's transport"], ["--json", "With status: everything, as JSON"], ], examples: [ "failproofai self-report status", "failproofai self-report disable", "failproofai self-report test --agent claude", ], }); process.exit(0); } lastSubcommand = "self-report"; const { runSelfReportCommand } = await import("../src/hooks/self-report-cli"); const result = await runSelfReportCommand(subArgs); if (result.json !== undefined) process.stdout.write(`${JSON.stringify(result.json, null, 2)}\n`); else await printReport(`self-report ${subArgs[0]}`, result.lines, { ok: result.exitCode === 0 }); await track("cli_self_report", { ok: result.exitCode === 0, // The subcommand only — never a category, guidance text or a path. sub: ["status", "enable", "disable", "test", "sync"].includes(subArgs[0]) ? subArgs[0] : "unknown", }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } // jev --url | jev setup | status | test | remove // // The customer's own Jev endpoint and key (BYOK), the only opt-in to the // two-tier hook evaluator. Writes ~/.failproofai/jev.json and nothing else — // no root, no daemon call; hooks re-read the file on every event. The key is // taken on stdin or at a masked prompt and never printed, so it stays out of // `ps`, shell history and scrollback; `--token` is the one spelling that does // put it on the command line, and says so every time it is used. if (args[0] === "jev") { const subArgs = args.slice(1); if (subArgs.length === 0 || subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "jev", // One row per subcommand: the design's `status | test | models | remove` // runs as a shell pipeline if pasted, and the four take different flags. usage: [ ["failproofai jev setup --provider --key-stdin", "Turn Jev on"], ["failproofai jev --url --key-stdin", "Turn Jev on from a URL"], ["failproofai jev setup --provider failproofai", "Use FailproofAI Cloud"], ["failproofai jev status [--json]", "Show config and activity"], ["failproofai jev test [--json]", "Send one live request"], ["failproofai jev models [--provider ] [--url ] [--json]"], ["failproofai jev remove", "Delete the config"], ], options: [ ["--provider ", "typesafe openrouter vercel cloudflare custom failproofai"], ["--key-stdin", "Read the API key from stdin, or a prompt on a terminal"], ["--key-from-env", "Store no key; read it from FAILPROOFAI_JEV_API_KEY"], ["--token ", "The API key on the command line; prefer --key-stdin"], ["--url ", "The API base URL; picks the provider from its host"], ["--base-url ", "Override the API base; default clears it"], ["--mode ", "enforce, observe (log Jev, enforce regex) or off"], ["--model ", "Override the model id; default clears it"], ["--account-id ", "Cloudflare account id, 32 hex characters"], ["--timeout-ms ", "Time per call before falling back to regex (default 3000)"], ["--json", "Print JSON (status, test and models)"], ], examples: [ "failproofai jev setup --provider typesafe --key-stdin < key.txt", "failproofai jev setup --mode observe", "failproofai jev test", ], }); process.exit(0); } lastSubcommand = "jev"; const { runJevCommand } = await import("../src/hooks/jev-cli"); const result = await runJevCommand(subArgs); if (subArgs.includes("--json") && result.json !== undefined) { process.stdout.write(`${result.json}\n`); } else { await printLines(result.lines, result.exitCode === 0); } await track("cli_jev", { ok: result.exitCode === 0, // The subcommand only — never the provider, a URL, a model id or the key. // An argv opening with an option is the one-shot form of `setup`, and is // reported as `setup`: the literal, never the option or what follows it. sub: subArgs[0].startsWith("-") ? "setup" : ["setup", "status", "test", "models", "remove"].includes(subArgs[0]) ? subArgs[0] : "unknown", }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } // uninstall [--purge] [--dry-run] [--yes|-y] // // Top-level, and deliberately NOT a flag on `policies`. `policies --uninstall` // disables policies; this removes the product — hook entries across every // agent CLI plus the root-owned daemon service — and the two must not be one // keystroke apart. if (args[0] === "migrate") { const subArgs = args.slice(1); if (subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "migrate", usage: [["failproofai migrate", "Bring ~/.failproofai up to this version's layout"]], options: [["--dry-run", "Show the steps, change nothing"]], optionsCol: 20, }); process.exit(0); } const KNOWN = new Set(["--dry-run"]); const unknown = subArgs.find((a) => a.startsWith("-") && !KNOWN.has(a)); if (unknown) { throw new CliError( `Unexpected argument: ${unknown}.\nRun \`failproofai migrate --help\` for usage.`, ); } lastSubcommand = "migrate"; const { detectLayout } = await import("../src/hooks/fp-config"); const { LAYOUT_VERSION } = await import("../src/hooks/fp-home"); const { describePlan, runMigrations } = await import("../src/hooks/migrations"); const state = detectLayout(); // A NEWER home is refused here exactly as `checkLayoutForCli` refuses it: the // data is fine and an upgrade would read it, so migrating "forward" from it // is not a thing that exists. if (state.kind === "future") { await printReport("migrate", [ `This machine's failproofai directory was written by a newer version ` + `(layout ${state.found}; this build speaks ${LAYOUT_VERSION}). Upgrade rather than migrate:`, "", " npm install -g failproofai@latest", ], { ok: false, meta: `layout ${state.found}` }); await track("cli_migrate", { ok: false, reason: "future_layout" }); lastSubcommand = null; await exitAfterFlush(1); return; } const from = state.kind === "stale" ? state.found : LAYOUT_VERSION; if (subArgs.includes("--dry-run")) { await printReport("migrate", describePlan(from), { meta: "dry run" }); await track("cli_migrate", { ok: true, dry_run: true, from }); lastSubcommand = null; await exitAfterFlush(0); return; } if (state.kind !== "stale") { await printReport("migrate", [`Already at layout ${LAYOUT_VERSION}. Nothing to migrate.`]); await track("cli_migrate", { ok: true, from, steps: 0 }); lastSubcommand = null; await exitAfterFlush(0); return; } const run = runMigrations(from); const report = run.steps.map( (step) => ` ${step.ok ? "migrated" : "FAILED "} layout ${step.from} → ${step.to}`, ); if (run.backedUp.length > 0) report.push("", `Saved first: ${run.backedUp.join(", ")}`); // The failing step is marked where it is: the backup line comes first, and // left to printReport's guess it took the ✕ with the error indented under it. if (run.failed) { report.push( "", `✕ Step ${run.failed.from} → ${run.failed.to} did not finish: ${run.failed.error}`, ` The home is still marked layout ${from} and will be retried.`, ); } await printReport("migrate", report, { ok: !run.failed, meta: `layout ${from} → ${LAYOUT_VERSION}`, }); await track("cli_migrate", { ok: !run.failed, from, steps: run.steps.length, removed: run.outcome.removed.length, }); lastSubcommand = null; await exitAfterFlush(run.failed ? 1 : 0); return; } if (args[0] === "update") { const subArgs = args.slice(1); if (subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "update", usage: [["npm i -g failproofai@latest && failproofai update"]], options: [ ["--no-daemon", "Migrate everything but leave the daemon as is"], ["--no-restart", "Leave running Hermes and OpenClaw gateways as they are"], ], optionsCol: 20, }); process.exit(0); } const KNOWN = new Set(["--no-daemon", "--no-restart"]); const unknown = subArgs.find((a) => a.startsWith("-") && !KNOWN.has(a)); if (unknown) { throw new CliError( `Unexpected argument: ${unknown}.\nRun \`failproofai update --help\` for usage.`, ); } lastSubcommand = "update"; // Runs the migration itself rather than leaning on the check at the top of // this file, which this command is exempt from. Reading the ledger and // inferring "the top-level check must have done it" was the first version, and // it is the kind of indirection that reads fine and reports the wrong thing // the moment either half moves. The steps are shared with `config`, which // runs them after the first setup on a new version (src/hooks/update-run.ts). const { runUpdateSteps, markUpdated } = await import("../src/hooks/update-run"); const noDaemon = subArgs.includes("--no-daemon"); const run = await runUpdateSteps({ layout: true, daemon: !noDaemon, // A daemon left as it is may not serve the new plugin, so --no-daemon // leaves the gateways alone too. restart: !noDaemon && !subArgs.includes("--no-restart"), noteSkippedDaemon: noDaemon, }); if (run.futureLayout) { await printReport("update", [ `This machine's failproofai directory was written by a NEWER version ` + `(layout ${run.futureLayout.found}; this build speaks ${run.futureLayout.speaks}). This CLI is the stale half:`, "", " npm install -g failproofai@latest", ], { ok: false, meta: `layout ${run.futureLayout.found}` }); await track("cli_update", { ok: false, reason: "future_layout" }); lastSubcommand = null; await exitAfterFlush(1); return; } const report = run.lines; const updateOk = run.ok; if (updateOk && !noDaemon) markUpdated(version); // No `v${version}` meta: the header already names the version. await printReport("update", report, { ok: updateOk }); await track("cli_update", { ok: updateOk, migrations: run.migrationsRan, migration_failed: run.migrationFailed, hermes_ok: run.hermesOk, hermes_migrated: run.hermesMigrated, }); lastSubcommand = null; await exitAfterFlush(updateOk ? 0 : 1); return; } if (args[0] === "uninstall") { const subArgs = args.slice(1); if (subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "uninstall", usage: [["failproofai uninstall [options]"]], options: [ ["--purge", "Also delete ~/.failproofai: settings, keys and history"], ["--dry-run", "Show what would be removed, change nothing"], ["--yes, -y", "Skip confirmation, remove the daemon; needed without a TTY"], ], examples: ["failproofai uninstall && npm rm -g failproofai"], optionsCol: 20, }); process.exit(0); } const KNOWN = new Set(["--purge", "--dry-run", "--yes", "-y"]); const unknown = subArgs.find((a) => !KNOWN.has(a)); if (unknown) { throw new CliError( `Unexpected argument: ${unknown}.\nRun \`failproofai uninstall --help\` for usage.`, ); } lastSubcommand = "uninstall_command"; const { runUninstallCommand } = await import("../src/hooks/uninstall-cli"); const { screenKit, optsFor, printBlock, stack } = await import("../src/hooks/tui"); const purge = subArgs.includes("--purge"); const dryRun = subArgs.includes("--dry-run"); const yes = subArgs.includes("--yes") || subArgs.includes("-y"); const render = optsFor(process.stdout); const kit = screenKit(render); // Set only when the question was actually asked, which is the one case // where the plan is already on screen and printing it again would // duplicate it. let planWasShown = false; const result = await runUninstallCommand({ purge, dryRun, yes, cwd: process.cwd(), render, // ONE question (D17): the plan says exactly what goes, and `y` removes all // of it, the daemon service included — the same thing `--yes` does. Only // offered when a person can actually answer: without a TTY the command // requires --yes rather than assuming consent — see the module. confirm: process.stdin.isTTY ? async (planLines) => { planWasShown = true; process.stdout.write(["", kit.header("Uninstall"), "", ...planLines].join("\n") + "\n"); return kit.confirm({ stdin: process.stdin, stdout: process.stdout }); } : undefined, }); // The question already printed the header and the plan; the outcome goes // under it. `planLines` is reported by the command rather than guessed from // the text — see UninstallResult. const outcome = planWasShown ? result.lines.slice(result.planLines) : [kit.header("Uninstall"), "", ...result.lines]; printBlock(result.exitCode === 0 ? process.stdout : process.stderr, stack(outcome)); // NOT after a purge. `track` resolves the instance id, and `getInstanceId()` // lazily WRITES ~/.failproofai/state/telemetry-id — which re-created the // whole directory seconds after the purge deleted it, leaving a machine the // user had just wiped holding a brand-new tracking identifier and making // the command's own "✓ deleted" line false. A purge means gone; nothing // gets to touch the home afterwards, least of all telemetry. if (!result.purged) { await track("cli_uninstall_command", { ok: result.exitCode === 0, purge, dry_run: dryRun, }); } lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } // publish — build the release assets AND put them on a GitHub release. // // Top level rather than under `policies`, because it is a PRODUCER verb: the // four consumer words are what every user reads, and almost none of them will // ever ship a pack. `pack build` is canonicalized into this above. if (args[0] === "publish") { const subArgs = args.slice(1); // NOT `subArgs.length === 0`. A bare `failproofai publish` is the headline // of the help this used to print instead — "TWO COMMANDS, FROM NOTHING: // --init to start, publish to ship it" — and every argument it needs is // worked out from the directory and the git remote. Printing help there // made the one documented command the only one that did nothing. if (subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp({ name: "publish", usage: [ ["failproofai publish --init [file]", "Write a starter policy file"], ["failproofai publish [file]", "Build and release it on GitHub"], ], options: [ ["--repo ", "Where to release, created if missing (default: origin)"], ["--version ", "Name the version (default: HEAD's tag, else short sha)"], ["--id ", "The pack id (default: the repo)"], ["--tag ", "Release tag (default: the version, v prefix ok)"], ["--notes ", "Release notes (default: policy counts and the commit)"], ["--out ", "Where to write the assets (default: dist-pack)"], ["--effect ", "enforce (default), or observe: record without blocking"], ["--min-cli-version ", "Oldest failproofai version that may install it"], ["--dry-run", "Build the assets and publish nothing"], ["--allow-private", "Publish to a private repo anyway; installs will fail"], ["--entry ", "The policy file, same as [file]"], ], examples: [ "failproofai publish --init guards", "failproofai policies -i -c ./guards.mjs", "failproofai publish --repo me/guards", ], optionsCol: 24, }); process.exit(0); } lastSubcommand = "publish"; const { runPublishCommand } = await import("../src/hooks/pack-cli"); const result = await runPublishCommand(subArgs); await printLines(result.lines, result.exitCode === 0); // Shape, never value: whether it published and whether it worked. Never the // repo, the pack id or the entry path — all three are things the user typed. await track("cli_publish", { ok: result.exitCode === 0, dry_run: subArgs.includes("--dry-run") }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } // policies add | remove | show — one noun for policies. // // `policies`, `policy` and `pack` were three commands for one idea. They are // one now, and `add`/`remove` take EITHER a policy name or a pack source, // told apart by a SLASH: a policy name matches /^[A-Za-z0-9._-]+$/, so a // slash is already illegal in one and unambiguous in the other. That is the // rule npm and docker use, and it means nobody has to discover a flag before // they can install somebody else's policies. // // Honors the same --cli / --scope / --beta flags as `policies --install`. if ( args[0] === "policies" && (args[1] === "add" || args[1] === "remove" || args[1] === "show") ) { lastSubcommand = "policy"; const subArgs = args.slice(1); if (subArgs.length === 0 || subArgs.includes("--help") || subArgs.includes("-h")) { await printHelp(POLICIES_HELP); process.exit(0); } const action = subArgs[0]; const rest = subArgs.slice(1); // A policy name matches /^[A-Za-z0-9._-]+$/ (pack-manifest.ts owns that // rule), so a slash can only ever mean a pack. `core` is the one name // without a slash that is still a source — it is the short spelling of our // own pack, and is checked by the pack lane itself rather than duplicated // here. // The first positional that is NOT some flag's value. // // A plain `rest.find(a => !a.startsWith("-"))` reads the first VALUE it // meets instead: `policies add --cli claude codex acme/x` picked "claude", // decided that was not a source because it has no slash, and sent the whole // command down the policy lane — where it died on "Unknown flag: --policy". // The same shape works, and misroutes, for --scope and --custom. // // `--cli` takes SEVERAL values, bounded the way pack-cli.ts bounds them: a // pack source always carries a slash and an agent name never does, which // separates the list from what follows it exactly. const VALUE_TAKING = new Set(["--scope", "--policy", "--category", "--only", "--custom", "-c"]); const skip = new Set(); for (let i = 0; i < rest.length; i += 1) { if (VALUE_TAKING.has(rest[i])) skip.add(i + 1); if (rest[i] !== "--cli") continue; for (let j = i + 1; j < rest.length && !rest[j].startsWith("-") && !rest[j].includes("/"); j += 1) { skip.add(j); } } const firstPositional = rest.find((a, i) => !a.startsWith("-") && !skip.has(i)); let looksLikeSource = !!firstPositional && (firstPositional.includes("/") || firstPositional.startsWith("github:")); if (!looksLikeSource && firstPositional) { // A retired spelling of our own pack — `core` and friends — is still // routed to the pack lane, which is the only layer that can say what to // type instead. Sent anywhere else it reads as an unknown POLICY name and // the reply lists 38 names, none of which is the answer. // // Read from the layer that owns the set rather than restated here, where // the copy would drift. That exact drift already shipped once, when the // dashboard could not resolve a name the CLI could. const { RETIRED_CORE_ALIASES } = await import("../src/hooks/pack-store"); looksLikeSource = RETIRED_CORE_ALIASES.has(firstPositional.toLowerCase()); } if (action === "show" || looksLikeSource) { if (action === "show" && !firstPositional) { throw new CliError( "Name the pack to show, as /.\n" + "To see what is installed here, run `failproofai policies`", ); } // One lane, one implementation. `show` is the pack lane's remote preview, // and add/remove of a source are its add/remove — routed by translating // the words rather than by growing a second copy of either. const packArgs = action === "show" ? ["list", ...rest] : [action, ...rest]; const { runPackCommand } = await import("../src/hooks/pack-cli"); const result = await runPackCommand(packArgs); await printLines(result.lines, result.exitCode === 0); await track("cli_pack", { ok: result.exitCode === 0, // The subcommand only — never the pack id, source or policy names. A // pack source is a value the user typed and a third-party pack name is // a publisher-controlled string; we send shape, never value. sub: action, }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } // `show` left above, so only add and remove reach here. A branch for any // other word stood here, unreachable: this lane is entered for those three. const validScopes = action === "remove" ? ["user", "project", "local", "all"] : ["user", "project", "local"]; const scopeIdx = rest.indexOf("--scope"); const scope = scopeIdx >= 0 ? rest[scopeIdx + 1] : "user"; if (scopeIdx >= 0 && (!scope || scope.startsWith("-"))) { throw new CliError(`Missing value for --scope.\nUse one of: ${validScopes.join(", ")}`); } if (scopeIdx >= 0 && !validScopes.includes(scope)) { throw new CliError(`Invalid scope: ${scope}.\nUse one of: ${validScopes.join(", ")}`); } // --cli accepts one or more space-separated values, optionally repeated. const { cliFlagValues, cliConsumedIdxs } = await readCliFlag(rest); const includeBeta = rest.includes("--beta"); // Reject unknown flags. const knownFlags = new Set(["--scope", "--cli", "--beta"]); const unknownFlag = rest.find((a) => a.startsWith("-") && !knownFlags.has(a)); if (unknownFlag) { // -c is on the shared help page, for --install and --uninstall; pointing // back at that page answered the question with itself. const custom = unknownFlag === "-c" || unknownFlag === "--custom"; throw new CliError( `Unknown flag: ${unknownFlag}.\n` + (custom ? action === "add" ? "Add a custom policy file with `failproofai policies --install -c `" : "Remove custom policy files with `failproofai policies --uninstall -c`" : `Run \`failproofai policies ${action} --help\` for usage.`), ); } // Positional policy names = anything not consumed by --scope / --cli. const consumedIdxs = new Set(); if (scopeIdx >= 0) consumedIdxs.add(scopeIdx + 1); for (const i of cliConsumedIdxs) consumedIdxs.add(i); const positional = rest.filter( (a, idx) => !a.startsWith("-") && !consumedIdxs.has(idx), ); if (positional.length === 0) { // Naming nothing is a question, not a mistake. It used to be an error // telling you to go and read a list somewhere else and come back — so // the answer is to SHOW the list, here, with what is already on ticked. const { runPolicyPicker } = await import("../src/hooks/pack-cli"); const result = await runPolicyPicker(action, { stdin: process.stdin, stdout: process.stdout }); await printLines(result.lines, result.exitCode === 0); await track("cli_policy_picker", { ok: result.exitCode === 0, action }); lastSubcommand = null; await exitAfterFlush(result.exitCode); return; } if (positional.length > 1) { throw new CliError( `policies ${action} takes one policy name, not ${positional.length}.\n` + `For several, run \`failproofai policies --${action === "add" ? "install" : "uninstall"} ${positional.join(" ")}\``, ); } const policyName = positional[0]; const { resolveTargetClis } = await import("../src/hooks/install-prompt"); const cli = await resolveTargetClis( cliFlagValues.length > 0 ? cliFlagValues : undefined, action === "add" ? "install" : "uninstall", ); lastPolicyAction = action; // `syncAgentSelection` on both: an agent hooked here is traced, and one // unhooked at user scope (`policies remove all`) is not, so the next // `failproofai config` neither undoes nor resurrects what this ran. if (action === "add") { const { installHooks } = await import("../src/hooks/manager"); await installHooks( [policyName], scope, undefined, includeBeta, undefined, undefined, false, cli, { syncAgentSelection: true }, ); await track("cli_policy_add_success", { scope, cli, cli_count: cli.length, policy_name: policyName, include_beta: includeBeta, }); } else { // `policy remove ` always removes the named policy regardless // of whether it's beta or not — passing `betaOnly: includeBeta` // here was a mislabel that only affected the telemetry field, not // the actual remove. Drop the `--beta` semantic for remove and // emit beta_only: false unconditionally so dashboards don't see // ghost "beta removal" events. const { removeHooks } = await import("../src/hooks/manager"); await removeHooks( [policyName], scope, undefined, { betaOnly: false, removeCustomHooks: false, cli, syncAgentSelection: true }, ); await track("cli_policy_remove_success", { scope, cli, cli_count: cli.length, policy_name: policyName, beta_only: false, }); } lastPolicyAction = null; process.exit(0); } // policies [--install|-i|--uninstall|-u|--help|-h] [names...] [--scope] [--beta] [--custom|-c ] if (args[0] === "policies") { const subArgs = args.slice(1); const isInstall = subArgs.includes("--install") || subArgs.includes("-i"); const isUninstall = subArgs.includes("--uninstall") || subArgs.includes("-u"); const isHelp = subArgs.includes("--help") || subArgs.includes("-h"); if (isHelp) { await printHelp(POLICIES_HELP); process.exit(0); } if (isInstall) { lastSubcommand = "install"; const { installHooks } = await import("../src/hooks/manager"); const { resolveTargetClis } = await import("../src/hooks/install-prompt"); const scopeIdx = subArgs.indexOf("--scope"); const scope = scopeIdx >= 0 ? subArgs[scopeIdx + 1] : "user"; if (scopeIdx >= 0 && (!scope || scope.startsWith("-"))) { throw new CliError("Missing value for --scope.\nUse one of: user, project, local"); } if (scopeIdx >= 0 && !["user", "project", "local"].includes(scope)) { throw new CliError(`Invalid scope: ${scope}.\nUse one of: user, project, local`); } const customIdxs = subArgs .map((arg, index) => (arg === "--custom" || arg === "-c" ? index : -1)) .filter((index) => index >= 0); const customPoliciesPaths = customIdxs.map((index) => subArgs[index + 1]); if (customPoliciesPaths.some((path) => !path || path.startsWith("-"))) { throw new CliError("Missing path after --custom/-c.\nFor example: `failproofai policies -i -c ./my-policies.js`"); } // --cli accepts one or more space-separated values, optionally repeated: // --cli claude codex copilot // --cli claude --cli codex // Values are consumed greedily until the next flag, or the first word that // is not an agent so a policy name following --cli (e.g. `--cli claude // block-sudo`) is not mis-consumed as one. const { cliFlagValues, cliConsumedIdxs } = await readCliFlag(subArgs); const includeBeta = subArgs.includes("--beta"); // Collect positional policy names — args that don't start with - and aren't // values consumed by --scope, --custom/-c, or --cli (tracked by index, not value, // so a policy named "user" isn't incorrectly dropped by the default scope). const consumedIdxs = new Set(); if (scopeIdx >= 0) consumedIdxs.add(scopeIdx + 1); for (const customIdx of customIdxs) consumedIdxs.add(customIdx + 1); for (const i of cliConsumedIdxs) consumedIdxs.add(i); const flags = new Set(["--install", "-i", "--scope", "--beta", "--custom", "-c", "--cli"]); const unknownInstallFlag = subArgs.find((a) => a.startsWith("-") && !flags.has(a)); if (unknownInstallFlag) { throw new CliError(`Unknown flag: ${unknownInstallFlag}.\nRun \`failproofai policies --help\` for usage.`); } const explicitPolicyNames = subArgs.filter( (a, idx) => !a.startsWith("-") && !consumedIdxs.has(idx) ); // When --custom/-c is present but no explicit policy names, pass [] so // installHooks uses the existing enabled policies and skips the interactive // prompt — validation of the custom file happens inside installHooks. const policyNames = explicitPolicyNames.length > 0 ? explicitPolicyNames : customPoliciesPaths.length > 0 ? [] : undefined; const cli = await resolveTargetClis( cliFlagValues.length > 0 ? cliFlagValues : undefined, "install", ); // An agent hooked here is traced from now on, so the next // `failproofai config` keeps these hooks instead of taking them out. await installHooks( policyNames, scope, undefined, includeBeta, undefined, customPoliciesPaths.length > 0 ? customPoliciesPaths : undefined, false, cli, { syncAgentSelection: true }, ); await track("cli_install_success", { scope, cli, cli_count: cli.length, explicit_policies: explicitPolicyNames.length > 0, include_beta: includeBeta, has_custom_path: customPoliciesPaths.length > 0, }); process.exit(0); } if (isUninstall) { lastSubcommand = "uninstall"; const { removeHooks } = await import("../src/hooks/manager"); const { resolveTargetClis } = await import("../src/hooks/install-prompt"); const scopeIdx = subArgs.indexOf("--scope"); const scope = scopeIdx >= 0 ? subArgs[scopeIdx + 1] : "user"; if (scopeIdx >= 0 && (!scope || scope.startsWith("-"))) { throw new CliError("Missing value for --scope.\nUse one of: user, project, local, all"); } if (scopeIdx >= 0 && !["user", "project", "local", "all"].includes(scope)) { throw new CliError(`Invalid scope: ${scope}.\nUse one of: user, project, local, all`); } // --cli accepts one or more space-separated values; same parser as install. const { cliFlagValues, cliConsumedIdxs } = await readCliFlag(subArgs); const betaOnly = subArgs.includes("--beta"); const removeCustomHooks = subArgs.includes("--custom") || subArgs.includes("-c"); const consumedIdxs = new Set(); if (scopeIdx >= 0) consumedIdxs.add(scopeIdx + 1); for (const i of cliConsumedIdxs) consumedIdxs.add(i); const flags = new Set(["--uninstall", "-u", "--scope", "--beta", "--custom", "-c", "--cli"]); const unknownUninstallFlag = subArgs.find((a) => a.startsWith("-") && !flags.has(a)); if (unknownUninstallFlag) { throw new CliError(`Unknown flag: ${unknownUninstallFlag}.\nRun \`failproofai policies --help\` for usage.`); } const policyNames = subArgs.filter( (a, idx) => !a.startsWith("-") && !consumedIdxs.has(idx) ); const cli = await resolveTargetClis( cliFlagValues.length > 0 ? cliFlagValues : undefined, "uninstall", ); // Hooks taken out at user scope or every scope stop the agent being // traced; a project- or local-only removal leaves the selection alone. await removeHooks( policyNames.length > 0 ? policyNames : undefined, scope, undefined, { betaOnly, removeCustomHooks, cli, syncAgentSelection: true }, ); await track("cli_uninstall_success", { scope, cli, cli_count: cli.length, beta_only: betaOnly, remove_custom_hooks: removeCustomHooks, explicit_policies: policyNames.length > 0, }); process.exit(0); } // Default: list policies // Accept --list as a no-op alias (common intuition), reject all other unknown flags // and unexpected positional args (e.g. "hi"). --all lists every policy instead of // folding the off ones into one line per category. const knownListFlags = new Set(["--install", "-i", "--uninstall", "-u", "--help", "-h", "--list", "--all"]); const unknownListArg = subArgs.find((a) => a.startsWith("-") && !knownListFlags.has(a)); if (unknownListArg) { throw new CliError( `Unknown flag: ${unknownListArg}.\n` + "Run `failproofai policies --help` for usage." ); } const positionalArgs = subArgs.filter((a) => !a.startsWith("-")); if (positionalArgs.length > 0) { throw new CliError( `Unexpected argument: ${positionalArgs[0]}.\n` + "Run `failproofai policies --help` for usage." ); } lastSubcommand = "list"; const { listHooks } = await import("../src/hooks/manager"); await listHooks(undefined, { all: subArgs.includes("--all") }); await track("cli_list_invoked", {}); process.exit(0); } // audit — scan local agent-CLI history, then launch the dashboard at /audit. if (args[0] === "audit") { lastSubcommand = "audit"; const { runAuditCli } = await import("../src/audit/cli"); await runAuditCli(args.slice(1)); // No process.exit(): on the success path runAuditCli calls launch(), which // keeps this process alive running the dashboard. The --help / no-sessions // paths exit inside runAuditCli; failures throw a CliError handled below. return; } // config — the interactive setup launcher (scope, agents, policies). // `configure` and `setup` are canonicalized to "config" up top. Running it // explicitly does NOT run the post-setup audit (that only fires on first-run // onboarding via bare `failproofai`). if (args[0] === "config") { if (args.includes("--help") || args.includes("-h")) { await printHelp({ name: "config", usage: [["failproofai config [options]"]], options: [ ["--token ", "Connect with this key; prefer FAILPROOFAI_CLOUD_TOKEN"], ["--oss", "Use open source; leaves any connection as it is"], ["--agents ", "Trace these agents, or all, without asking"], ["--status", "Show the cloud connection, daemon version and pauses"], ["--pause [time]", "Pause enforcement for a session (default 30m, max 8h)"], ["--resume [--all]", "End a pause early, or every pause with --all"], ["--session ", "Session to pause or resume (default: newest one here)"], ["--connect ", "Connect to cloud only (for a machine already set up)"], ["--disconnect", "Stop pulling policies and sending activity"], ["--no-transcripts", "Send policy decisions only, not session transcripts"], ["--url ", "Use a self-hosted cloud (or FAILPROOFAI_CLOUD_URL)"], ["--machine-label ", "Rename this machine; with --token, set up under it"], ["--machine-id ", "Machine id for --connect (default: a stable id)"], ], // The key never goes on the command line: config itself warns that argv // lands in shell history and the process list. examples: [ "read -rs FAILPROOFAI_CLOUD_TOKEN && export FAILPROOFAI_CLOUD_TOKEN", "failproofai config", "failproofai config --pause 1h", ], optionsCol: 22, }); process.exit(0); process.exit(0); } lastSubcommand = "config"; // Every option config takes. A typo used to fall through to the full setup // run with the option ignored, so `--agent claude` asked every question. // // `--send-transcripts` is retired, not unknown: it was the transcript // opt-in until transcripts became the default (#653), and provisioning // scripts written then still pass it. Accepted, and does nothing. const CONFIG_FLAGS = new Set([ "--token", "--oss", "--agents", "--status", "--pause", "--resume", "--all", "--session", "--connect", "--disconnect", "--no-transcripts", "--url", "--machine-label", "--machine-id", "--send-transcripts", ]); const CONFIG_VALUE_FLAGS = new Set([ "--token", "--agents", "--pause", "--session", "--connect", "--url", "--machine-label", "--machine-id", ]); const configRest = args.slice(1); const strayConfig = configRest.find( (a, i) => !CONFIG_FLAGS.has(a) && !(CONFIG_VALUE_FLAGS.has(configRest[i - 1]) && !a.startsWith("-")), ); if (strayConfig) { const asFlag = `--${strayConfig.replace(/^-+/, "")}`; throw new CliError( `Unexpected argument: ${strayConfig}.\n` + (CONFIG_FLAGS.has(asFlag) && asFlag !== strayConfig ? `Did you mean \`failproofai config ${asFlag}\`?` : "See its options: `failproofai config --help`"), ); } if (args.includes("--send-transcripts") && args.includes("--no-transcripts")) { throw new CliError( "--send-transcripts and --no-transcripts cannot be combined.\n" + "Transcripts are sent by default; --no-transcripts alone turns them off.", ); } // --pause / --resume / --status are non-interactive session actions that // share `config`'s surface but not the wizard. They write session state, // never the config file — a pause that reached policies-config.json would // be committed and outlive the session that asked for it. // Cloud enrolment. Deliberately writes a credential file the daemon reads // rather than an Environment= line in the service unit: that unit is // installed world-readable (0644, /etc/systemd/system), so a token there // would be readable by every local user. Keeping it out also means no // sudo, and lets an already-installed daemon be connected. const connectIdx = args.indexOf("--connect"); const wantsDisconnect = args.includes("--disconnect"); // `--machine-label` ALONE is a rename, not an enrolment. Alongside --connect it // keeps its old meaning (the name to enrol under); on its own it changes the // name of a machine that is already connected, which previously required // re-running enrolment with the url and token again just to fix a display name. // // Alongside a setup option (--token, --oss, --agents) it is the name to set up // under: `config --token K --machine-label L` used to run as a rename, so // setup never ran and the key was ignored. Setup applies it when it connects; // otherwise it is refused up front or applied as a rename afterwards (below). const wantsRename = connectIdx < 0 && !wantsDisconnect && args.includes("--machine-label") && !["--token", "--oss", "--agents"].some((flag) => args.includes(flag)); // A key given as --token is in shell history whatever became of the run. const warnTokenOnArgv = async () => { if (!args.includes("--token")) return; const { CONFIG_TOKEN_HISTORY_WARNING } = await import("../src/hooks/cloud-enrollment-cli"); const { screenKit, colorsEnabled } = await import("../src/hooks/tui"); const kit = screenKit({ color: colorsEnabled(process.stderr) }); process.stderr.write(`\n${kit.notice("caution", CONFIG_TOKEN_HISTORY_WARNING.join("\n")).join("\n")}\n`); }; // The fix for a flag given no value. Never "put the key after --token": the // whole point of the variable is that a key on argv lands in shell history. const missingValue = (flag) => new CliError( `Missing value after ${flag}.\n` + (flag === "--token" ? "Or leave --token out and set FAILPROOFAI_CLOUD_TOKEN." : "Run `failproofai config --help` for what it takes."), ); if (connectIdx >= 0 || wantsDisconnect || wantsRename) { if (connectIdx >= 0 && wantsDisconnect) { throw new CliError("--connect and --disconnect cannot be combined."); } const valueAfter = (flag) => { const i = args.indexOf(flag); if (i < 0) return undefined; const v = args[i + 1]; if (!v || v.startsWith("-")) throw missingValue(flag); return v; }; let result; if (wantsRename) { const { runRenameCommand } = await import("../src/hooks/cloud-enrollment-cli"); result = await runRenameCommand(valueAfter("--machine-label")); } else if (wantsDisconnect) { const { runDisconnectCommand } = await import("../src/hooks/cloud-enrollment-cli"); result = runDisconnectCommand(); } else { const { hostname } = await import("node:os"); const { runConnectCommand } = await import("../src/hooks/cloud-enrollment-cli"); result = await runConnectCommand({ url: valueAfter("--connect"), // The same fallback the headless `config` path has, and the one // `config --help` documents: it keeps the key out of shell history. token: valueAfter("--token") ?? process.env.FAILPROOFAI_CLOUD_TOKEN, machineId: valueAfter("--machine-id"), machineLabel: valueAfter("--machine-label"), defaultMachineId: hostname(), // Transcripts are what connecting is FOR, so they default on and the // disclosure is made at the point of connection rather than hidden // behind an opt-in flag most people never discover — a dashboard // showing only decisions is the empty-dashboard problem in a // different costume. --no-transcripts is the explicit way out, and // `failproofai config --status` always says which is in effect. sessions: !args.includes("--no-transcripts"), }); } // Through the shared presenter: a header naming what ran, and a failure // opening with ✕ and its fix under it, like every other action result. await printReport(wantsRename ? "rename" : wantsDisconnect ? "disconnect" : "connect", result.lines, { ok: result.exitCode === 0, }); await warnTokenOnArgv(); await track("cli_cloud_enrollment", { action: wantsRename ? "rename" : wantsDisconnect ? "disconnect" : "connect", ok: result.exitCode === 0, }); await exitAfterFlush(result.exitCode); return; } const pauseIdx = args.indexOf("--pause"); const wantsResume = args.includes("--resume"); const wantsStatus = args.includes("--status"); if (pauseIdx >= 0 || wantsResume || wantsStatus) { const chosen = [pauseIdx >= 0 && "--pause", wantsResume && "--resume", wantsStatus && "--status"].filter(Boolean); if (chosen.length > 1) { throw new CliError(`${chosen.join(" and ")} cannot be combined.\nRun them one at a time.`); } // `--status` answers "what is this machine's state?": the daemon, the // dashboard and any pauses, the cloud connection and whether data // arrives, and what enforcement is doing — one screen, one column. if (wantsStatus) { const { runStatusCommand } = await import("../src/hooks/config-status"); const status = await runStatusCommand({ cwd: process.cwd() }); await printLines(status.lines); await track("cli_pause_invoked", { action: "status", ok: true, affected: status.paused }); await exitAfterFlush(0); return; } const sessionIdx = args.indexOf("--session"); if (sessionIdx >= 0 && !args[sessionIdx + 1]) { throw new CliError("Missing session id after --session.\nOr leave --session out to use the newest session here."); } // A bare `--pause` takes the default duration, so only treat the next // token as a duration when it isn't another flag. const next = pauseIdx >= 0 ? args[pauseIdx + 1] : undefined; const duration = next && !next.startsWith("-") ? next : undefined; const { runPauseCommand } = await import("../src/hooks/session-pause-cli"); const result = runPauseCommand({ action: pauseIdx >= 0 ? "pause" : "resume", duration, sessionId: sessionIdx >= 0 ? args[sessionIdx + 1] : undefined, all: args.includes("--all"), cwd: process.cwd(), }); await printLines(result.lines, result.exitCode === 0); await track("cli_pause_invoked", { action: pauseIdx >= 0 ? "pause" : "resume", ok: result.exitCode === 0, affected: result.affected, }); await exitAfterFlush(result.exitCode); return; } // Headless setup. `--token` IS the request to connect — there is no other // reason to pass a key — so there is no separate --connect to remember, and // the URL keeps the default the wizard already infers rather than being a // question the flag path alone asks. // // The key is taken from the environment when the flag is absent, because a // secret on argv is readable from `ps` by every user on the box and lands // in shell history and CI logs. FAILPROOFAI_CLOUD_TOKEN is the name the // Rust daemon already reads, so this unifies rather than invents. const valueFor = (flag) => { const i = args.indexOf(flag); if (i < 0) return undefined; const v = args[i + 1]; if (!v || v.startsWith("-")) throw missingValue(flag); return v; }; // --oss skips the connect step and leaves any connection as it is (Tab's // meaning, decision D4); --disconnect is what removes one. It overrides a // key in the environment, and only an explicit --token contradicts it. const oss = args.includes("--oss"); if (oss && args.includes("--token")) { throw new CliError("--oss and --token cannot be combined: one uses open source, the other connects."); } // --agents: any of the twelve, or `all`, validated here so a typo is an // error rather than an agent silently left untraced. let agents; if (args.includes("--agents")) { const raw = valueFor("--agents"); const { INTEGRATION_TYPES } = await import("../src/hooks/types"); const ids = raw === "all" ? [...INTEGRATION_TYPES] : raw.split(",").map((a) => a.trim()).filter(Boolean); const unknown = ids.filter((id) => !INTEGRATION_TYPES.includes(id)); if (ids.length === 0 || unknown.length > 0) { throw new CliError(`Not an agent: ${unknown.join(", ") || raw}.\nAgents: ${INTEGRATION_TYPES.join(", ")}, or all`); } agents = ids; } // `--machine-label` here is the name to connect under. When this run cannot // connect at all it is refused now, before anything is written; on a // machine that stays connected as it was, it is applied after setup. const machineLabel = valueFor("--machine-label"); if (machineLabel !== undefined) { const { machineLabelRefusal } = await import("../src/hooks/cloud-enrollment-cli"); const refusal = machineLabelRefusal({ oss, keyGiven: !oss && Boolean(valueFor("--token") ?? process.env.FAILPROOFAI_CLOUD_TOKEN), interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY), }); if (refusal) throw new CliError(refusal); } const { runConfigureWizard } = await import("../src/hooks/configure-wizard"); const { pluggedProfiles } = await import("../src/hooks/update-run"); const pluggedBefore = await pluggedProfiles().catch(() => []); const result = await runConfigureWizard( {}, { token: oss ? undefined : (valueFor("--token") ?? process.env.FAILPROOFAI_CLOUD_TOKEN), url: valueFor("--url") ?? process.env.FAILPROOFAI_CLOUD_URL, machineId: valueFor("--machine-id"), machineLabel, noTranscripts: args.includes("--no-transcripts"), oss, agents, }, ); // Setup names the machine only when it connects; when it left a // connection as it was, the label is the rename the flag asked for. let labelExit = 0; if (result.applied && machineLabel !== undefined) { const { applyMachineLabelAfterSetup } = await import("../src/hooks/cloud-enrollment-cli"); const renamed = await applyMachineLabelAfterSetup(machineLabel); if (renamed) { await printReport("rename", renamed.lines, { ok: renamed.exitCode === 0 }); labelExit = renamed.exitCode; } } // The first setup on a new version finishes the upgrade too: without this a // running Hermes or OpenClaw gateway kept the plugin it loaded before (or // none) until someone thought to run `update`. Setup has already brought // the daemon to this version and the entry check migrated the home. // Then Self Reports: every hooked agent's MCP entry brought in line with // self-report.json. Both behind one spinner line, which leaves the Self // Reports line and only what needs the user (src/hooks/setup-finish.ts). let updateExit = 0; if (result.applied) { const { finishSetup } = await import("../src/hooks/setup-finish"); updateExit = await finishSetup({ version, pluggedBefore }); } await warnTokenOnArgv(); await track("cli_configure_invoked", { applied: result.applied, // `target` and `scopes`, not `scope`: the wizard rework replaced that // field and nothing caught it, because `.mjs` is outside the tsconfig // include so `tsc --noEmit` never type-checks this file. Every // `cli_configure_invoked` since has reported `scope: null`. target: result.target ?? null, scopes: result.scopes ?? [], cli_count: result.clis?.length ?? 0, abort: result.abort ?? null, oss, agents_named: agents ? agents.length : 0, }); // `abort` is the field `WizardAbort` exists to expose, and exiting 0 // regardless discarded it: a fleet script could not tell "the user pressed // Esc" from "this machine could not install the required daemon and is // unconfigured". Cancelling is not a failure; the other two are. await exitAfterFlush(!result.applied && result.abort && result.abort !== "cancelled" ? 1 : labelExit || updateExit); return; } // Unknown flag guard — must appear after all known-flag branches. // // Both guards below suggest a whole command, runnable as printed, or nothing // at all. The nearest-spelling guess they replaced always answered, so // `status` got `flush`, `install` got `uninstall` and `-i` got `audit`; see // did-you-mean.ts for the table of words people type and the cut-off. const knownFlags = ["--version", "-v", "--help", "-h", "--hook"]; const unknownFlag = args.find(a => a.startsWith("-") && !knownFlags.includes(a)); if (unknownFlag) { const { suggestFlag } = await import("../src/hooks/did-you-mean"); const suggestion = suggestFlag(unknownFlag); throw new CliError( `Unknown flag: ${unknownFlag}.\n` + (suggestion ? `Did you mean \`${suggestion}\`?` : "Run `failproofai help` for usage.") ); } // Unknown subcommand guard (non-flag args that aren't a known subcommand) const unknownSubcommand = args.find(a => !a.startsWith("-") && !SUBCOMMANDS.includes(a)); if (unknownSubcommand) { const { suggestCommand } = await import("../src/hooks/did-you-mean"); const suggestion = suggestCommand(unknownSubcommand, SUBCOMMANDS); throw new CliError( `There is no command called '${unknownSubcommand}'.\n` + (suggestion ? `Did you mean \`${suggestion}\`?` : "Run `failproofai help` to see every command.") ); } // Dashboard launch — always production mode. Runs on every bare `failproofai` // (first-run onboarding, if any, already ran above). const { launch } = await import("../scripts/launch"); launch("start", { logo: !setupWizardRan }); } // ── Import CliError for use in the guard above ──────────────────────────────── const { CliError } = await import("../src/cli-error"); // ── Run ─────────────────────────────────────────────────────────────────────── try { await runCli(); } catch (err) { if (err instanceof CliError) { if (lastSubcommand === "install") { await track("cli_install_failure", { error_type: "cli_error", exit_code: err.exitCode }); } else if (lastSubcommand === "uninstall") { await track("cli_uninstall_failure", { error_type: "cli_error", exit_code: err.exitCode }); } else if (lastSubcommand === "policy" && lastPolicyAction) { // Mid-action failure: `policy add|remove` parsed but installHooks / // removeHooks threw a CliError (e.g. unknown policy name, invalid scope). await track(`cli_policy_${lastPolicyAction}_failure`, { error_type: "cli_error", exit_code: err.exitCode, }); } else { await track("cli_parse_error", { subcommand: lastSubcommand ?? (args[0] ?? null), exit_code: err.exitCode, }); } // `✕ `, then each fix on its own line, indented two, with // the command in pink (see `notice` in tui.ts). The exit code is the error's. // Written rather than `console.error`, which Bun paints red on a terminal — // a colour from outside the palette — and drained before exiting. let failureText = `✕ ${err.message}`; try { const { screenKit, colorsEnabled } = await import("../src/hooks/tui"); failureText = screenKit({ color: colorsEnabled(process.stderr) }).notice("fail", err.message).join("\n"); } catch { // The plain glyph is still the right shape; the kit is decoration. } process.stderr.write(`${failureText}\n`); await exitAfterFlush(err.exitCode); } // Unexpected internal error — show message only, no stack trace const msg = err instanceof Error ? err.message : String(err); if (lastSubcommand === "install") { await track("cli_install_failure", { error_type: err instanceof Error ? err.name : "unknown" }); } else if (lastSubcommand === "uninstall") { await track("cli_uninstall_failure", { error_type: err instanceof Error ? err.name : "unknown" }); } else if (lastSubcommand === "policy" && lastPolicyAction) { await track(`cli_policy_${lastPolicyAction}_failure`, { error_type: err instanceof Error ? err.name : "unknown", }); } else { await track("cli_unexpected_error", { subcommand: lastSubcommand ?? (args[0] ?? null), error_type: err instanceof Error ? err.name : "unknown", }); } // Plain, never through the kit: this is the path for an install so broken // that anything could be the next thing to fail. process.stderr.write(`✕ Unexpected error: ${msg}\n`); await exitAfterFlush(2); }