neosian 3.0.0

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! A Vibe child's state (X6a, X6c U3): the `logs` directory of its private
//! Vibe home and nothing else. Vibe moves everything with `VIBE_HOME`
//! (sessions, logs, caches, trusted folders, hooks, plugins), so the child's
//! home is the one neosian made for it, and the person's own Vibe home
//! stays one of every child's denied roots. In a live run with only `logs/`
//! of the home writable, fresh, write and resumed turns completed (the
//! caches and lock files it could not write failed soft). `hooks.toml`,
//! `config.toml` and the plugin folders are never writable to the child,
//! and before each launch the home is emptied but what Vibe needs to
//! resume the continued session (`home.rs`).

use std::path::{Path, PathBuf};

use super::super::text::say;
use super::policy::{Rule, text};

fn refused(home: Option<&Path>) -> String {
    let shown = home.map_or_else(String::new, |home| home.display().to_string());
    say("vibe_home_refused", &[("directory", &shown)])
}

/// The private home the watcher was handed, checked as the server made it:
/// `neosian-harness-<server>-<kind>-<32 hex>` directly in the temporary
/// directory, the server being this watcher's parent; a real directory, no
/// link, mode 0700, owned as the turn's private directory is, holding the
/// marker.
pub fn validate(kind: &str, home: Option<&Path>, private: &Path) -> Result<PathBuf, String> {
    use std::os::unix::fs::MetadataExt;
    let given = home.ok_or_else(|| refused(home))?;
    let named = format!(
        "{}{}-{kind}-",
        super::super::home::PREFIX,
        std::os::unix::process::parent_id()
    );
    let name = given
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or_default();
    let hex = |b: u8| b.is_ascii_digit() || (b'a'..=b'f').contains(&b);
    let numbered = name
        .strip_prefix(&named)
        .is_some_and(|n| n.len() == 32 && n.bytes().all(hex));
    let temp = std::env::temp_dir().canonicalize().ok();
    let placed = given.parent().map(Path::to_path_buf) == temp;
    let found = given.symlink_metadata().ok().filter(|m| m.is_dir());
    let owner = private.metadata().map(|m| m.uid()).ok();
    let own = found.is_some_and(|m| m.mode() & 0o077 == 0 && Some(m.uid()) == owner);
    let marked = given.join(super::super::MARKER).is_file();
    if numbered && placed && own && marked && given.canonicalize().ok().as_deref() == Some(given) {
        return Ok(given.to_path_buf());
    }
    Err(refused(home))
}

/// The policy's allowance for a Vibe child, its private home's `logs`
/// directory, and the marker, which the child may not remove.
pub fn rules(home: Option<&Path>) -> Result<(Vec<Rule>, Vec<Rule>), String> {
    let home = home.ok_or_else(|| refused(home))?;
    let marker = Rule::Literal(text(&home.join(super::super::MARKER))?);
    Ok((vec![Rule::Subpath(text(&home.join("logs"))?)], vec![marker]))
}