Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

7.9 KB
Raw Blame
//! Autonomy and security policy configuration.

use super::defaults;
use crate::security::{AutonomyLevel, TrustedRoot};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(default)]
pub struct AutonomyConfig {
    /// Master switch for the autonomy policy. **Defaults to `false`.**
    ///
    /// These agents run inside containers, platform jails and Docker sandboxes
    /// that already provide the isolation this in-process policy was
    /// approximating, and a shell tool that refuses ordinary shell syntax
    /// (`$(…)`, backticks, a `&` inside a heredoc body) is not a usable shell.
    /// So the policy is opt-in rather than opt-out.
    ///
    /// When `false`, every field below is inert: command classification, the
    /// allowlist, the approval gate, the hourly action budget, `workspace_only`,
    /// `forbidden_paths` and the workspace-internal boundary are all skipped.
    ///
    /// `SecurityPolicy::is_always_forbidden` still applies either way —
    /// credential stores (`~/.ssh`, `~/.gnupg`, `~/.aws`) and system roots stay
    /// unreachable. Deliberate floor, documented on
    /// [`crate::security::SecurityPolicy::enabled`].
    ///
    /// Set `[autonomy] enabled = true` to restore the full policy; `level`,
    /// `workspace_only` and the rest then behave exactly as they always have.
    #[serde(default)]
    pub enabled: bool,
    // No field-level override needed — AutonomyLevel's #[default] is Supervised,
    // matching the struct Default. It only takes effect when `enabled` is true.
    pub level: AutonomyLevel,
    #[serde(default = "default_true")]
    pub workspace_only: bool,
    #[serde(default = "default_allowed_commands")]
    pub allowed_commands: Vec<String>,
    #[serde(default = "default_forbidden_paths")]
    pub forbidden_paths: Vec<String>,
    #[serde(default = "default_max_actions_per_hour")]
    pub max_actions_per_hour: u32,
    #[serde(default = "default_max_cost_per_day_cents")]
    pub max_cost_per_day_cents: u32,
    #[serde(default = "default_true")]
    pub require_approval_for_medium_risk: bool,
    #[serde(default = "default_true")]
    pub block_high_risk_commands: bool,
    /// Tool names the user has pre-approved ("Always allow"). The `ApprovalGate`
    /// skips the interactive prompt for any tool listed here. Populated by the
    /// "Always allow" approval decision or hand-edited, and surfaced in
    /// Settings → Agent access. Read live by the gate via `SecurityPolicy`.
    #[serde(default = "default_auto_approve")]
    pub auto_approve: Vec<String>,
    /// When true, the approval gate auto-approves ALL tool calls without
    /// prompting the user — a blanket bypass of the interactive approval
    /// flow, not just the per-tool `auto_approve` allowlist above. An
    /// unlabelled call site is still hard-denied regardless of this flag (see `ApprovalGate::intercept_audited_inner`).
    /// Hard security blocks (`is_always_forbidden`, `is_workspace_internal_path`,
    /// `ToolPolicyMiddleware`) live on independent code paths inside the tool
    /// implementations themselves and are unaffected by this setting.
    ///
    /// It also bypasses **parking**, which is worth knowing before enabling it.
    /// A remote-origin triage dispatch — a Composio or webhook payload reaching
    /// `triage.escalate` — normally parks for a decision and writes a
    /// `pending_approvals` audit row. With this flag on it is allowed
    /// immediately and **no audit row is written**, so those dispatches leave no
    /// approval trail. Accepted deliberately as the cost of a blanket
    /// "approve everything" switch (openhuman#5634); the alternative was to make
    /// this flag mean "approve everything except…", which is harder to reason
    /// about.
    ///
    /// Defaults to `false` — existing users see no behavior change.
    #[serde(default)]
    pub auto_approve_all: bool,
    /// Directories outside the workspace the agent may access. Each entry grants
    /// read (or read+write) to its subtree, taking precedence over `workspace_only`
    /// and `forbidden_paths` — except credential stores (~/.ssh, ~/.gnupg, ~/.aws),
    /// which stay blocked regardless.
    #[serde(default)]
    pub trusted_roots: Vec<TrustedRoot>,
    /// Whether the agent may install OS packages via the `install_tool` tool.
    /// Intended to be enabled only in Full access mode.
    #[serde(default)]
    pub allow_tool_install: bool,
}

fn default_true() -> bool {
    defaults::default_true()
}

fn default_max_actions_per_hour() -> u32 {
    // Effectively unlimited. The rate-limiter check is `count <= max`, so any
    // ceiling above realistic per-hour traffic is functionally infinite;
    // u32::MAX lets the field stay a plain `u32` without a sentinel option.
    u32::MAX
}

fn default_max_cost_per_day_cents() -> u32 {
    500
}

fn default_allowed_commands() -> Vec<String> {
    vec![
        // Version control
        "git".into(),
        // Package managers / build systems. `make` can run arbitrary recipes,
        // but the shell policy still gates execution to this command allow-list
        // and Supervised mode approval remains responsible for risky invocations.
        "npm".into(),
        "pnpm".into(),
        "yarn".into(),
        "cargo".into(),
        "make".into(),
        "cmake".into(),
        // Directory / file inspection (read-only)
        "ls".into(),
        "cat".into(),
        "grep".into(),
        "find".into(),
        "echo".into(),
        "pwd".into(),
        "wc".into(),
        "head".into(),
        "tail".into(),
        "date".into(),
        "sort".into(),
        "uniq".into(),
        "diff".into(),
        "which".into(),
        "uname".into(),
        "basename".into(),
        "dirname".into(),
        "tr".into(),
        "cut".into(),
        "realpath".into(),
        "readlink".into(),
        "stat".into(),
        "file".into(),
        // Filesystem mutations (medium-risk — require approval in Supervised mode)
        "mkdir".into(),
        "touch".into(),
        "cp".into(),
        "mv".into(),
        "ln".into(),
        // Windows read-only equivalents for ls/cat/grep/which
        "dir".into(),
        "type".into(),
        "where".into(),
        "findstr".into(),
        "more".into(),
    ]
}

fn default_forbidden_paths() -> Vec<String> {
    vec![
        "/etc".into(),
        "/root".into(),
        "/home".into(),
        "/usr".into(),
        "/bin".into(),
        "/sbin".into(),
        "/lib".into(),
        "/opt".into(),
        "/boot".into(),
        "/dev".into(),
        "/proc".into(),
        "/sys".into(),
        "/var".into(),
        "/tmp".into(),
        "~/.ssh".into(),
        "~/.gnupg".into(),
        "~/.aws".into(),
        "~/.config".into(),
    ]
}

fn default_auto_approve() -> Vec<String> {
    vec![
        // Read-only tools — always safe to skip the approval prompt
        "file_read".into(),
        "memory_search".into(),
        "memory_list".into(),
        "get_time".into(),
        "list_dir".into(),
        // Workspace-scoped search tools — read-only, no side effects
        "glob".into(),
        "grep".into(),
    ]
}

impl Default for AutonomyConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            level: AutonomyLevel::Supervised,
            workspace_only: default_true(),
            allowed_commands: default_allowed_commands(),
            forbidden_paths: default_forbidden_paths(),
            max_actions_per_hour: default_max_actions_per_hour(),
            max_cost_per_day_cents: default_max_cost_per_day_cents(),
            require_approval_for_medium_risk: default_true(),
            block_high_risk_commands: default_true(),
            auto_approve: default_auto_approve(),
            auto_approve_all: false,
            trusted_roots: Vec::new(),
            allow_tool_install: false,
        }
    }
}