neosian 3.0.3

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Vibe's and Muse Code's state paths (X6a, X6b), inside a
//! scratch directory only.

use std::path::Path;

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

fn first(fixture: &Fixture, agent: Agent, home: Option<&Path>) -> Result<Policy, String> {
    policy::build(&Inputs {
        agent,
        mode: Mode::Write,
        directory: &fixture.path("home/work"),
        private: &fixture.private(),
        home: &fixture.path("home"),
        stores: &fixture.stores(),
        var: &unset,
        session: None,
        child_home: home,
        session_dir: None,
        persist: &fixture.path("persist"),
    })
}

#[test]
fn a_vibe_child_writes_only_its_homes_logs() {
    // X6c U3: hooks.toml, config.toml and plugins are never writable.
    let fixture = Fixture::new("vibe-state");
    let home = fixture.path("vibe-home");
    let policy = first(&fixture, Agent::Vibe, Some(&home)).unwrap();
    let own = Rule::Subpath(text(&home.join("logs")).unwrap());
    assert!(policy.allow.contains(&own) && policy.state == [own]);
    assert!(!policy.allow.contains(&Rule::Subpath(text(&home).unwrap())));
    assert!(
        policy
            .roots
            .contains(&text(&fixture.path("home/.vibe")).unwrap())
    );
    assert!(
        first(&fixture, Agent::Vibe, None).is_err(),
        "no private home"
    );
    // The watcher takes only the home the server made, as it made it.
    std::fs::create_dir(&home).unwrap();
    assert!(vibe::validate("vibe", Some(&home), &fixture.private()).is_err());
}

#[test]
fn off_macos_vibe_loses_the_store_credential() {
    let work = Path::new("/");
    let root = vec!["--root".to_string(), "/nonexistent/store".to_string()];
    let Ok(Launch::Presets { unset }) = presets(Agent::Vibe, &root, work) else {
        panic!("a Vibe child off macOS")
    };
    assert!(unset.iter().any(|name| name == "NEOSIAN_CLIENT_TOKEN"));
}

#[test]
fn a_muse_child_writes_only_its_own_session_directory() {
    // X6c U5: the directory is made and checked outside the child, which
    // writes inside it and not in sessions/ or the date directories.
    let fixture = Fixture::new("muse-state");
    let session = "0a1b2c3d-4e5f-4789-abcd-ef0123456789";
    let home = fixture.path("home");
    let made = muse::make(&home, &unset, session).unwrap();
    let sessions = fixture.path("home/.local/share/muse/sessions");
    assert_eq!(
        made.parent()
            .unwrap()
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .parent(),
        Some(sessions.as_path())
    );
    assert!(made.ends_with(session) && made.is_dir());
    assert_eq!(
        muse::validate(&home, &unset, &made, session),
        Ok(made.clone())
    );
    let policy = policy::build(&Inputs {
        agent: Agent::Muse,
        mode: Mode::Write,
        directory: &fixture.path("home/work"),
        private: &fixture.private(),
        home: &home,
        stores: &fixture.stores(),
        var: &unset,
        session: Some(session),
        child_home: Some(&fixture.path("muse-home")),
        session_dir: Some(&made),
        persist: &fixture.path("persist"),
    })
    .unwrap();
    assert_eq!(policy.state, [Rule::Subpath(text(&made).unwrap())]);
    for root in [
        "home/.local/share/muse",
        "home/.config/muse",
        "home/Library/Application Support/Muse",
    ] {
        assert!(
            policy.roots.contains(&text(&fixture.path(root)).unwrap()),
            "{root}"
        );
    }
    assert!(
        first(&fixture, Agent::Muse, None).is_err(),
        "no session directory"
    );
    assert!(muse::make(&home, &unset, session).is_err(), "made once");
    let day = made.parent().unwrap();
    let other = "1a1b2c3d-4e5f-4789-abcd-ef0123456789";
    std::fs::create_dir(day.join(other)).unwrap();
    for (dir, named) in [
        (day.join(other), session),
        (day.to_path_buf(), session),
        (sessions.join(format!("2026/1/01/{session}")), session),
        (day.join("not-a-uuid"), "not-a-uuid"),
    ] {
        assert!(
            muse::validate(&home, &unset, &dir, named).is_err(),
            "{dir:?}"
        );
    }
    let moved = fixture.private().join("elsewhere");
    std::fs::rename(&made, &moved).unwrap();
    std::os::unix::fs::symlink(&moved, &made).unwrap();
    assert!(
        muse::validate(&home, &unset, &made, session).is_err(),
        "a link"
    );
}