neosian 3.0.1

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! What the live Cursor and Pi children needed beyond their source-derived
//! state (X7a): Cursor's worker socket and its directory as Cursor names
//! it, the claim lock a resumed chat takes, and the lock Pi takes on its
//! credential store to read it.

use std::path::Path;

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

#[test]
fn cursors_worker_directory_is_named_as_cursor_names_it() {
    // Computed with Cursor 2026.11.22's own rule (paths.js `kL`).
    let data = Path::new("/Users/someone/.cursor");
    let long = Path::new(
        "/Users/someone/Documents/projects/neosian/record/x7a-2026-11-29/live/cursor-ro.MaF591",
    );
    let short = Path::new("/Users/someone/w");
    assert_eq!(
        super::cursor_run::worker_dir(data, Path::new("/t"), long),
        Path::new(
            "/Users/someone/.cursor/projects/Users-someone-Documents-projects-neosian-record-x7a-f6bf044"
        )
    );
    assert_eq!(
        super::cursor_run::worker_dir(data, Path::new("/t"), short),
        Path::new("/Users/someone/.cursor/projects/Users-someone-w")
    );
    let deep =
        Path::new("/Users/someone/a-data-directory-whose-projects-path-is-longer-than-83-chars");
    assert_eq!(
        super::cursor_run::worker_dir(deep, Path::new("/t"), short),
        deep.join("Users-someone-w"),
        "past 83 characters the projects directory is left out"
    );
    let deeper = Path::new(
        "/Users/someone/a-data-directory-name-that-is-longer-than-eighty-three-characters-xxxxxx",
    );
    assert_eq!(
        super::cursor_run::worker_dir(deeper, Path::new("/t"), short),
        Path::new("/t/.cursor/Users-someone-w"),
        "then the child's temporary directory"
    );
    let long_tmp = Path::new(
        "/var/folders/ab/cdefghijklmnopqrstuvwxyz0123/T/neosian-harness-41-0123456789abcdef0123456789abcdef",
    );
    assert_eq!(
        super::cursor_run::worker_dir(deeper, long_tmp, short),
        Path::new("/tmp/.cursor/Users-someone-w"),
        "and last /tmp"
    );
}

/// X7c Y7: the fixture owns its persist root and its short private
/// directory, and chooses a data directory too long for Cursor's first two
/// candidates on every platform, so the worker falls in that private
/// directory and the policy names no worker place and nothing in `/tmp`.
#[test]
fn the_persistent_session_state_is_made_closed_and_refused_when_open() {
    use std::os::unix::fs::PermissionsExt;
    let fixture = Fixture::new("cursor-persist");
    let root = fixture.path("persist");
    super::cursor_run::ready(&root).unwrap();
    for dir in [&root, &root.join("claim-locks"), &root.join("bindings")] {
        assert_eq!(dir.metadata().unwrap().permissions().mode() & 0o777, 0o700);
    }
    std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o755)).unwrap();
    assert!(super::cursor_run::ready(&root).is_err(), "open to others");
}

/// X7c Y5: exactly `auth.json.lock` in the agent directory, while
/// `auth.json` is no link and the lock's name holds nothing but a directory.
#[test]
fn pis_lock_allowance_is_refused_for_a_linked_store_or_a_file_at_its_name() {
    let refused = |fixture: &Fixture| {
        let built = fixture.build(Agent::Pi, Mode::ReadOnly, &unset);
        assert!(
            built
                .as_ref()
                .is_err_and(|e| e.contains("auth.json.lock beside")),
            "{:?}",
            built.map(|p| p.state)
        );
    };
    let linked = Fixture::new("pi-lock-linked");
    std::fs::create_dir_all(linked.path("home/.pi/agent")).unwrap();
    std::fs::write(linked.path("outside.json"), "{}").unwrap();
    std::os::unix::fs::symlink(
        linked.path("outside.json"),
        linked.path("home/.pi/agent/auth.json"),
    )
    .unwrap();
    refused(&linked);
    let file = Fixture::new("pi-lock-file");
    std::fs::create_dir_all(file.path("home/.pi/agent")).unwrap();
    std::fs::write(file.path("home/.pi/agent/auth.json.lock"), "").unwrap();
    refused(&file);
    let link = Fixture::new("pi-lock-link");
    std::fs::create_dir_all(link.path("home/.pi/agent/elsewhere")).unwrap();
    std::os::unix::fs::symlink(
        link.path("home/.pi/agent/elsewhere"),
        link.path("home/.pi/agent/auth.json.lock"),
    )
    .unwrap();
    refused(&link);
    let held = Fixture::new("pi-lock-held");
    std::fs::create_dir_all(held.path("home/.pi/agent/auth.json.lock")).unwrap();
    std::fs::write(held.path("home/.pi/agent/auth.json"), "{}").unwrap();
    let policy = held.build(Agent::Pi, Mode::ReadOnly, &unset).unwrap();
    let lock = held.path("home/.pi/agent/auth.json.lock");
    assert!(policy.state.contains(&Rule::Literal(text(&lock).unwrap())));
}

#[test]
fn a_pi_child_may_make_only_its_credential_stores_lock() {
    let fixture = Fixture::new("pi-live");
    let policy = fixture.build(Agent::Pi, Mode::ReadOnly, &unset).unwrap();
    let lock = fixture.path("home/.pi/agent/auth.json.lock");
    assert!(policy.state.contains(&Rule::Literal(text(&lock).unwrap())));
    assert!(
        !policy
            .state
            .iter()
            .any(|r| matches!(r, Rule::Literal(p) if p.ends_with("auth.json")))
    );
}