openmoat 0.23.1

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.
//! What the Lightweight tier covers inside the project on Linux, and what it
//! leaves open there, as `moat run` says before the agent starts and `moat
//! doctor` says beforehand (ADR-090). Landlock only grants, so a deny rule for
//! a path inside a tree it grants cannot be enforced by Landlock alone:
//! - with a network namespace (bubblewrap installed and allowed), its mounts
//!   cover the project as under `--isolate` ([`NAMESPACED`]);
//! - without one, the project is granted entry by entry, and what lies deeper
//!   in a granted entry stays open, each path named ([`port_only`]).
//!
//! macOS's Seatbelt denies inside the project by path, so nothing is named there.

/// How many of the paths left open a line names before it counts the rest.
const NAMED: usize = 8;

/// What `moat run` prints when the agent gets a network namespace.
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
pub const NAMESPACED: &str = "moat run: in the project, the paths the policy denies are \
     covered for the session as under `--isolate` (`.env` files hidden, `.git`, `.moat` and \
     agents' configuration read-only), but a match created after the start where nothing \
     stood (`.env.local`, a `.env` in a new folder) stays open; the hook still checks the \
     agent's own tool calls. `openmoat run --isolate` also hides the rest of the machine";

/// What `moat run` prints when the agent gets a network namespace but the
/// project could not be walked (`why`): only its top and the folders directly
/// in it are covered.
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
pub fn namespaced_near_top(why: &str) -> String {
    format!(
        "moat run: the project could not be walked ({why}), so only its top and the folders \
         directly in it are covered for the session (`.env` files there hidden, `.git`, `.moat` \
         and agents' configuration there read-only); what the policy denies deeper stays open \
         to commands; the hook still checks the agent's own tool calls"
    )
}

/// `moat doctor`'s row where `moat run` without a network namespace would
/// refuse to start, because the project could not be granted entry by entry
/// (`why`).
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
pub fn doctor_refusal(why: &str) -> String {
    format!(
        "openmoat run     without a network namespace it would refuse to start here: the \
         project cannot be granted entry by entry ({why}). Install bubblewrap \
         (`apt install bubblewrap`)"
    )
}

/// What `moat run` prints when the project is granted entry by entry, with
/// `open`, the paths the policy denies there that stay open (or why they could
/// not be listed).
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
pub fn port_only(open: &Result<Vec<String>, String>) -> String {
    format!(
        "moat run: without bubblewrap the project is granted entry by entry: commands cannot \
         create, rename or remove entries directly in it (create what they need before the \
         run), `.env` files there stay unreadable and `.git` and `.moat` read-only. {} With \
         bubblewrap (`apt install bubblewrap`) `openmoat run` covers these with mounts and \
         needs none of this; or set `sandbox.project_top: writable` in your policy and accept \
         that `.env` and git hooks at the top are open to commands. The hook still checks the \
         agent's own tool calls",
        left_open(open)
    )
}

/// What `moat run` prints when the person's policy opened the project's top
/// (`sandbox.project_top: writable`, ADR-090).
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
pub const PORT_ONLY_TOP_OPEN: &str = "moat run: without bubblewrap, and with \
     `sandbox.project_top: writable` in your policy, the project is granted whole: commands the \
     agent starts can read and write `.env`, write `.git/hooks` and `.moat`, and create \
     `.envrc` there. This run is recorded in the audit log (`run-project-top-open`). Install \
     bubblewrap (`apt install bubblewrap`), or remove the setting, to cover them; the hook \
     still checks the agent's own tool calls";

/// `moat doctor`'s warning where the person's policy opened the project's top
/// and `moat run` would get no network namespace.
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
pub const DOCTOR_TOP_OPEN: &str = "openmoat run     `sandbox.project_top: writable`: without a \
     network namespace the project is granted whole, so commands can read `.env` and write \
     `.git/hooks` and `.moat` there; every such run is in the audit log. Install bubblewrap \
     (`apt install bubblewrap`) or remove the setting";

/// `moat doctor`'s row where `moat run` would grant the project entry by entry.
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
pub fn doctor_line(open: &Result<Vec<String>, String>) -> String {
    format!(
        "openmoat run     without a network namespace the project here is granted entry by \
         entry: commands cannot create entries directly in it. {} Install bubblewrap \
         (`apt install bubblewrap`) to cover them, or set `sandbox.project_top: writable` \
         and accept that `.env` and git hooks at the top are open to commands",
        left_open(open)
    )
}

/// The sentence naming `open`.
#[cfg_attr(
    not(any(target_os = "linux", test)),
    expect(
        dead_code,
        reason = "only Linux has a Lightweight tier with a gap to name"
    )
)]
fn left_open(open: &Result<Vec<String>, String>) -> String {
    match open {
        Ok(paths) if paths.is_empty() => {
            "Nothing else the policy denies in the project is open to commands.".to_owned()
        }
        Ok(paths) => {
            let mut named = paths
                .iter()
                .take(NAMED)
                .cloned()
                .collect::<Vec<_>>()
                .join(", ");
            if paths.len() > NAMED {
                named = format!("{named} and {} more", paths.len() - NAMED);
            }
            format!(
                "These paths the policy denies lie in a folder granted whole and stay open to \
                 commands: {named}."
            )
        }
        Err(why) => format!(
            "Which paths the policy denies deeper in the project stay open could not be \
             listed ({why})."
        ),
    }
}

/// How `moat status`, `moat doctor` and `moat init` name the command that
/// puts an agent without a sandbox of its own in one: `--isolate` on Linux.
#[must_use]
pub fn command(bubblewrap: Option<bool>) -> &'static str {
    match bubblewrap {
        None => "`openmoat run`",
        Some(_) => "`openmoat run --isolate`",
    }
}

/// [`command`] as a person types it, without the backticks.
#[must_use]
pub fn words(bubblewrap: Option<bool>) -> &'static str {
    match bubblewrap {
        None => "openmoat run",
        Some(_) => "openmoat run --isolate",
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn granted_entry_by_entry_names_what_it_covers_the_cost_and_each_open_path() {
        let none = port_only(&Ok(Vec::new()));
        for said in [
            "entry by entry",
            "cannot create, rename or remove entries directly in it",
            "`.env` files there stay unreadable",
            "`.git` and `.moat` read-only",
            "Nothing else the policy denies",
            "apt install bubblewrap",
            "set `sandbox.project_top: writable`",
        ] {
            assert!(none.contains(said), "{said}: {none}");
        }
        // The setting that opens the top says plainly what it opens and that
        // the run is on record; `doctor` warns of it.
        for said in ["`.env`", "`.git/hooks`", "run-project-top-open"] {
            assert!(PORT_ONLY_TOP_OPEN.contains(said), "{said}");
        }
        assert!(DOCTOR_TOP_OPEN.starts_with("openmoat run     "));
        assert!(DOCTOR_TOP_OPEN.contains("`.git/hooks`"));
        assert!(doctor_line(&Ok(Vec::new())).contains("sandbox.project_top: writable"));
        let paths: Vec<String> = (0..10).map(|i| format!("/p/sub{i}/.env")).collect();
        let some = port_only(&Ok(paths));
        assert!(some.contains("/p/sub0/.env, /p/sub1/.env"), "{some}");
        assert!(some.contains("/p/sub7/.env and 2 more"), "{some}");
        assert!(!some.contains("/p/sub8/.env"), "{some}");
        let unknown = port_only(&Err("too many files".into()));
        assert!(
            unknown.contains("could not be listed (too many files)"),
            "{unknown}"
        );
        let doctor = doctor_line(&Ok(vec!["/p/a/.env".into()]));
        assert!(doctor.starts_with("openmoat run     "), "{doctor}");
        assert!(doctor.contains("/p/a/.env"), "{doctor}");
    }

    #[test]
    fn the_namespace_names_what_stays_open_after_the_start() {
        assert!(NAMESPACED.contains("as under `--isolate`"));
        assert!(NAMESPACED.contains("created after the start"));
        let near = namespaced_near_top("more than 1000000 files");
        assert!(
            near.contains("could not be walked (more than 1000000 files)"),
            "{near}"
        );
        assert!(near.contains("deeper stays open"), "{near}");
        let refused = doctor_refusal("permission denied");
        assert!(refused.starts_with("openmoat run     "), "{refused}");
        assert!(refused.contains("would refuse to start here"), "{refused}");
        assert!(refused.contains("(permission denied)"), "{refused}");
    }

    #[test]
    fn the_protection_line_names_isolate_on_linux() {
        assert_eq!(command(None), "`openmoat run`");
        assert_eq!(command(Some(true)), "`openmoat run --isolate`");
    }
}