openmoat 0.28.0

Policy checks for AI coding agents: decides allow, defer, review or deny for each tool call the hook of Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot CLI and seven more agents reports, configures Claude Code's, Codex's and Cursor's own OS sandboxes from the same policy, and keeps a local audit log.
//! `moat`: command-line entry point for OpenMoat.
//!
//! The binary owns all I/O (files, environment, terminal); decisions are made
//! by `openmoat-core`. Exit codes are a stable contract used by host hooks:
//! see [`exit::Code`].

/// The agent account of `openmoat run` on Windows (ADR-129); its pure parts
/// are tested on every system, and every system's run names whom it starts
/// the agent as ([`agent_account::RunAs`]) and picks the proxy's port.
#[cfg_attr(
    not(windows),
    expect(
        dead_code,
        reason = "the Windows tier uses all of it; elsewhere only its pure parts"
    )
)]
mod agent_account;
mod anchor;
mod approvals;
mod arguments;
mod audit_key;
mod audit_signing;
mod canary;
mod cli;
mod commands;
mod compliance;
mod context;
mod delivery;
mod disturbance;
mod environment;
mod exit;
mod gate;
mod guard_relay;
mod hangs;
mod heartbeat;
mod home;
mod hook_ledger;
mod install;
mod integrity;
mod json5;
mod key_file;
mod managed;
mod mcp;
mod notice;
mod opt_in;
mod os_version;
mod packs;
mod plugin_record;
mod project;
mod project_packages;
mod project_settings;
mod protection;
mod provenance;
mod proxy_ca;
mod proxy_session;
mod push;
mod realpath;
mod remote;
mod render;
mod repo;
mod report;
mod sandbox;
mod secrets;
mod session_code;
mod signing;
mod snapshot;
mod start;
// Read and set only on Windows; its parsing is tested on every system.
#[cfg_attr(
    all(not(windows), not(test)),
    expect(dead_code, reason = "only Windows has the access lists it reads")
)]
mod state_acl;
mod taint;
mod team;
mod terminal;
mod time;
mod trust;
mod upgrade;
mod widen;

use std::process::ExitCode;

/// This build's version, as `openmoat --version` prints it.
const VERSION: &str = env!("CARGO_PKG_VERSION");

use clap::Parser;

fn main() -> ExitCode {
    // Started as the shell an `openmoat run` gives its agent's tool commands
    // (ADR-075): never a command of its own.
    if let Some(code) = commands::tool_shell_entry() {
        return code.into();
    }
    // A host runs `moat guard` and blocks the tool call only on exit 2; any other
    // failure lets the call through. So a `guard` that cannot even parse its
    // arguments denies (ADR-015), and every other command reports usage errors
    // with the dedicated code, never 2.
    let failed = if invoked_as_hook() {
        exit::Code::Deny
    } else {
        exit::Code::Usage
    };
    let cli = match cli::Cli::try_parse() {
        Ok(cli) => cli,
        Err(error) => {
            let informational = !error.use_stderr();
            let _ = error.print();
            return if informational {
                exit::Code::Ok
            } else {
                failed
            }
            .into();
        }
    };
    match commands::run(cli) {
        Ok(code) => code.into(),
        // Never for `guard`: a hook that cannot answer must not exit 0 (ADR-015).
        Err(error) if !invoked_as_hook() && broken_pipe(&error) => exit::Code::Ok.into(),
        Err(error) => {
            eprintln!("moat: {error:#}");
            failed.into()
        }
    }
}

/// True when the first argument is `guard`, the subcommand host hooks run.
fn invoked_as_hook() -> bool {
    std::env::args_os().nth(1).is_some_and(|arg| arg == "guard")
}

/// A reader that stops early (`moat show | head`) closes the pipe. Like other
/// command-line tools, stop quietly instead of reporting it as an error.
fn broken_pipe(error: &anyhow::Error) -> bool {
    error.chain().any(|cause| {
        let kind = cause
            .downcast_ref::<std::io::Error>()
            .map(std::io::Error::kind)
            .or_else(|| cause.downcast_ref::<serde_json::Error>()?.io_error_kind());
        kind == Some(std::io::ErrorKind::BrokenPipe)
    })
}