neosian 3.0.1

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! The policy's macOS profile text (split from `policy.rs`).

use super::policy::{Policy, Rule};

fn filter(rule: &Rule) -> String {
    match rule {
        Rule::Literal(path) => format!("(literal \"{path}\")"),
        Rule::Subpath(path) => format!("(subpath \"{path}\")"),
        Rule::Pattern(pattern) => format!("(regex #\"{pattern}\")"),
    }
}

impl Policy {
    /// The macOS sandbox profile: everything allowed but writes, then the
    /// allowances, then the protected places, which win because a later
    /// rule takes precedence, then data writes to the lock files, then the
    /// places no child reads; preference writes through cfprefsd closed.
    pub fn profile(&self) -> String {
        let mut out = String::from("(version 1)\n(allow default)\n(deny file-write*)\n");
        let mut roots: Vec<Rule> = self.roots.iter().cloned().map(Rule::Subpath).collect();
        roots.extend(self.homes.iter().cloned());
        for (verb, rules) in [
            ("allow", &self.allow),
            ("deny", &roots),
            ("allow", &self.state),
            ("deny", &self.protect),
        ] {
            if rules.is_empty() {
                continue;
            }
            out.push_str(&format!("({verb} file-write*"));
            for rule in rules {
                out.push_str("\n  ");
                out.push_str(&filter(rule));
            }
            out.push_str(")\n");
        }
        for (verb, places, filter) in [
            ("allow file-write-data", &self.locks, "literal"),
            ("deny file-read*", &self.hidden, "subpath"),
        ] {
            if !places.is_empty() {
                out.push_str(&format!("({verb}"));
                for place in places {
                    out.push_str(&format!("\n  ({filter} \"{place}\")"));
                }
                out.push_str(")\n");
            }
        }
        out.push_str(
            "(deny mach-lookup (global-name \"com.apple.cfprefsd.agent\") \
             (global-name \"com.apple.cfprefsd.daemon\"))\n",
        );
        out
    }
}