neosian 3.0.3

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Every private home and a project's `.muse` stay unwritable to every
//! child, wherever the temporary directory lies (X6c U2).

use super::policy::{Rule, text};
use super::tests::{Fixture, unset};
use super::*;

#[test]
fn no_child_writes_a_private_home_or_a_projects_muse_directory() {
    let fixture = Fixture::new("homes");
    let temp = std::env::temp_dir().canonicalize().unwrap();
    let home = |kind: &str| {
        temp.join(format!(
            "neosian-harness-41-{kind}-0123456789abcdef0123456789abcdef"
        ))
    };
    let project = Rule::Subpath(text(&fixture.path("home/work/.muse")).unwrap());
    for agent in Agent::ALL {
        let policy = fixture.build(agent, Mode::Write, &unset).unwrap();
        assert!(policy.protect.contains(&project), "{agent:?}");
        let denied = |path: &std::path::Path| {
            let path = path.display().to_string();
            policy.homes.iter().any(|rule| match rule {
                Rule::Pattern(p) => fancy_regex::Regex::new(p).unwrap().is_match(&path).unwrap(),
                _ => false,
            })
        };
        for kind in ["vibe", "muse"] {
            assert!(
                denied(&home(kind)) && denied(&home(kind).join("muse/settings.json")),
                "{agent:?}"
            );
        }
        let watcher = temp.join("neosian-harness-41-0123456789abcdef0123456789abcdef");
        assert!(!denied(&watcher), "a turn's own private directory");
        // Denied with the roots, before any state allowance.
        let profile = policy.profile();
        let denial = profile.find("(deny file-write*").unwrap();
        let pattern = match &policy.homes[0] {
            Rule::Pattern(p) => p.clone(),
            other => panic!("{other:?}"),
        };
        let at = profile.find(&pattern).unwrap();
        let state = profile.rfind("(allow file-write*").unwrap();
        assert!(
            denial < at && (policy.state.is_empty() || at < state),
            "{agent:?}"
        );
    }
}