neosian 3.0.2

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! A Pi child's state (X5a), inside a scratch directory only: its session
//! directory, the exact file a continued session opens, and the policy's
//! one Pi allowance.

use std::ffi::OsString;
use std::os::unix::fs::symlink;

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

const FILE: &str = "2026-11-28T21-09-13-378Z_0a1b2c3d-4e5f-4789-abcd-ef0123456789.jsonl";

fn header(id: &str, cwd: &std::path::Path) -> String {
    format!(
        "{{\"type\":\"session\",\"version\":3,\"id\":\"{id}\",\"cwd\":\"{}\"}}\n{{}}\n",
        cwd.display()
    )
}

#[test]
fn the_session_directory_is_the_childs_own_below_the_agent_directory() {
    let fixture = Fixture::new("pi-session-dir");
    let home = fixture.path("home");
    let dir = pi::session_dir(&home, &unset, SESSION).unwrap();
    let wanted = home
        .join(".pi/agent/sessions/neosian-harness")
        .join(SESSION);
    assert_eq!(dir, wanted);
    assert!(pi::session_dir(&home, &unset, "not-a-uuid").is_err());
    let moved_to = fixture.path("home/elsewhere");
    let moved = |name: &str| (name == pi::SESSION_VAR).then(|| OsString::from(&moved_to));
    let dir = pi::session_dir(&home, &moved, SESSION).unwrap();
    assert_eq!(dir, moved_to.join("neosian-harness").join(SESSION));
    let outside = fixture.private();
    let away = |name: &str| (name == pi::SESSION_VAR).then(|| OsString::from(&outside));
    let refused = pi::session_dir(&home, &away, SESSION).unwrap_err();
    assert!(refused.contains("pi's own state"), "{refused}");
    // A link on the way that leads elsewhere is refused, never followed.
    std::fs::create_dir_all(fixture.path("home/.pi/agent")).unwrap();
    symlink(
        fixture.path("home/work"),
        fixture.path("home/.pi/agent/sessions"),
    )
    .unwrap();
    assert!(pi::session_dir(&home, &unset, SESSION).is_err());
}

#[test]
fn a_new_session_gets_a_new_directory_and_a_continued_one_its_exact_file() {
    let fixture = Fixture::new("pi-session-file");
    let (home, work) = (fixture.path("home"), fixture.path("home/work"));
    let made = pi::prepare(&home, &unset, SESSION, false, &work).unwrap();
    assert!(made.session_dir.is_dir() && made.file.is_none());
    assert_eq!(made.agent_dir, home.join(".pi/agent"));
    let again = pi::prepare(&home, &unset, SESSION, false, &work).unwrap_err();
    assert!(again.contains("could not be made"), "{again}");
    let dir = made.session_dir;
    let continued = |dir: &std::path::Path| pi::session_file(dir, SESSION, &work);
    assert!(continued(&dir).unwrap_err().contains("is not in"));
    let file = dir.join(FILE);
    std::fs::write(&file, header(SESSION, &work)).unwrap();
    std::fs::write(dir.join("other.jsonl"), header("another", &work)).unwrap();
    assert_eq!(continued(&dir), Ok(file.clone()));
    let found = pi::prepare(&home, &unset, SESSION, true, &work).unwrap();
    assert_eq!(found.file, Some(file.clone()));
    // Ambiguous: a second file names the session.
    let second = dir.join("second.jsonl");
    std::fs::write(&second, header(SESSION, &work)).unwrap();
    assert!(continued(&dir).unwrap_err().contains("More than one file"));
    std::fs::remove_file(&second).unwrap();
    // Replaced: another directory, another name for the file, a link.
    for (body, link) in [
        (header(SESSION, &home), false),
        (header(SESSION, &work), true),
    ] {
        std::fs::write(&file, body).unwrap();
        if link {
            std::fs::hard_link(&file, fixture.private().join("alias")).unwrap();
        }
        assert!(continued(&dir).unwrap_err().contains("is not the one"));
    }
    std::fs::remove_file(fixture.private().join("alias")).unwrap();
    std::fs::remove_file(&file).unwrap();
    std::fs::write(fixture.private().join("real"), header(SESSION, &work)).unwrap();
    symlink(fixture.private().join("real"), &file).unwrap();
    assert!(continued(&dir).unwrap_err().contains("is not the one"));
    // The directory itself replaced by a link.
    std::fs::remove_dir_all(&dir).unwrap();
    symlink(fixture.private(), &dir).unwrap();
    assert!(continued(&dir).unwrap_err().contains("is not in"));
}

#[test]
fn a_pi_child_may_write_its_session_directory_and_nothing_else_of_pi() {
    let fixture = Fixture::new("pi-policy");
    for mode in [Mode::ReadOnly, Mode::Write] {
        let policy = fixture.build(Agent::Pi, mode, &unset).unwrap();
        let dir = fixture.path(&format!(
            "home/.pi/agent/sessions/neosian-harness/{SESSION}"
        ));
        let own = Rule::Subpath(text(&dir).unwrap());
        // And the lock Pi takes to read its credential store (X7a).
        let lock = Rule::Literal(text(&fixture.path("home/.pi/agent/auth.json.lock")).unwrap());
        assert!(policy.allow.contains(&own) && policy.state == [own.clone(), lock]);
        assert!(
            policy
                .roots
                .contains(&text(&fixture.path("home/.pi/agent")).unwrap())
        );
        let project = Rule::Subpath(text(&fixture.path("home/work/.pi")).unwrap());
        assert!(policy.protect.contains(&project));
    }
    // Every other child keeps Pi's directory and project files unwritable.
    let claude = fixture
        .build(Agent::ClaudeCode, Mode::Write, &unset)
        .unwrap();
    assert!(
        claude
            .roots
            .contains(&text(&fixture.path("home/.pi/agent")).unwrap())
    );
    assert!(
        !claude
            .state
            .iter()
            .any(|rule| matches!(rule, Rule::Subpath(p) if p.contains(".pi")))
    );
}