openmoat 0.29.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.
//! `openmoat run` refuses to start a program it cannot bring to the
//! protection level the policy requires of it (ADR-064). The run is the OS
//! sandbox; what it cannot give is the hook: a program that is no agent
//! OpenMoat knows has none, and an agent whose hook does not run has none
//! either. A requirement it cannot check stops the run (fail closed).
//!
//! `require.run: full-boundary` (ADR-077) asks more of the run itself: no
//! sandbox refuses here, and a run left with a port-only network refuses once
//! the platform has said so ([`super::refuse_port_only`]).

use anyhow::{Result, bail};
use openmoat_core::{EvalContext, RequiredLevel};
use openmoat_hosts::Host;

use crate::home::Home;
use crate::protection::required::{self, by, level_of};
use crate::sandbox::own_login::Owner;

/// `Ok` when the program `typed` (as the person typed it), started as
/// `owner`, meets what the policy for `ctx` requires of it under `openmoat
/// run`, or nothing is required. The value is which policy requires the full
/// boundary, for the run's later checks; `None` when none does.
pub fn check(home: &Home, ctx: &EvalContext, owner: Owner, typed: &str) -> Result<Option<String>> {
    let policy = required::policy_in(home, ctx)?;
    let strict = policy.run_boundary_required().map(|asks| by(&asks));
    if let Some(asks) = &strict
        && let Some(why) = crate::commands::run_unavailable()
    {
        bail!(
            "{asks} requires `openmoat run` to start with its full boundary \
             (`require.run: full-boundary`), and {why}; `{typed}` does not start"
        );
    }
    let host = match owner {
        Owner::Agent(host) => Some(host),
        Owner::Unknown => None,
    };
    let Some(requirement) = policy.required_for(host.map(Host::id)) else {
        return Ok(strict);
    };
    let level = level_of(requirement.level).label();
    let asks = by(&requirement.by);
    if requirement.level == RequiredLevel::HookAndOsSandbox
        && let Some(why) = crate::commands::run_unavailable()
    {
        bail!("{asks} requires {level}, and {why}; the program does not start");
    }
    let Some(host) = host else {
        bail!(
            "{asks} requires {level} of every agent, and `{typed}` is no agent OpenMoat knows, \
             so no hook of OpenMoat's would check its calls; it does not start"
        );
    };
    let binary = crate::install::hook_binary()?;
    let agent = crate::protection::of_host(home, &binary, host)?;
    if !agent.level.hooked() {
        let name = host.display_name();
        let why = agent.reason.as_deref().unwrap_or(agent.level.label());
        let fix = required::missing(host, agent.level, requirement.level)
            .map_or_else(String::new, |(_, fix)| format!("; to meet it, {fix}"));
        bail!(
            "{asks} requires {level} for {name}, and OpenMoat's hook does not run for it \
             ({why}), so `openmoat run` cannot meet it{fix}; {name} does not start"
        );
    }
    Ok(strict)
}