neosian 3.0.2

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! The common filesystem policy neosian applies to every harness child on
//! macOS (X4b, ledger #433): one policy for Claude Code, Codex and
//! OpenCode, built from validated canonical paths, with writes denied by
//! default. A `read-only` child may write only its private temporary
//! directory, its harness's own explicit state and a few devices; a `write`
//! child may also write its validated directory, which is the one
//! allowance between the modes. The watcher applies the policy at launch
//! with the operating system's sandbox (`macos.rs`) and stays outside it;
//! when the policy cannot be built or applied, the child is not started.
//! The server prepares the same policy before any client command runs, and
//! runs the client's metadata commands under it (X4e P4). Other platforms
//! keep each harness's own preset and say so; a Pi child there still loses
//! the store's credential variables, and is refused when the store is
//! reached with a managed login whose state it could read (X5a); a Cursor
//! child is refused there, since nothing but this policy keeps its
//! integrations from loading (X5b).

mod admission;
#[cfg(test)]
mod children_tests;
pub mod cursor;
mod cursor_run;
#[cfg(test)]
mod cursor_tests;
// A Cursor child exists on macOS alone, and these tests rest on its paths.
#[cfg(all(test, target_os = "macos"))]
mod cursor_worker_tests;
#[cfg(test)]
mod hardening_tests;
#[cfg(test)]
mod homes_tests;
#[cfg(test)]
mod launch_tests;
#[cfg(test)]
mod live_tests;
mod macos;
pub mod muse;
pub mod pi;
#[cfg(test)]
mod pi_tests;
mod policy;
mod private;
mod profile;
mod state;
mod store;
#[cfg(test)]
mod tests;
pub mod vibe;

use std::ffi::OsStr;
use std::path::Path;
use std::process::Command;

use super::adapter::{Agent, Mode};
use super::text::say;
use admission::presets;
pub use admission::ready;
pub use cursor_run::uid;
pub use policy::{Inputs, Policy};
pub use private::{Private, SHORT_PARENT};

/// How a platform's children are bounded.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Platform {
    /// macOS: neosian's common policy, applied by the operating system.
    Common,
    /// Elsewhere (Linux): each harness's own preset, as before X4.
    Presets,
}

/// The decision for an operating system named as `std::env::consts::OS`.
pub fn platform_for(os: &str) -> Platform {
    match os {
        "macos" => Platform::Common,
        _ => Platform::Presets,
    }
}

pub fn platform() -> Platform {
    platform_for(std::env::consts::OS)
}

/// The note a child's answer carries where the common policy is not
/// applied, so the parent knows which boundary holds.
pub fn note(platform: Platform, agent: Agent, mode: Mode) -> Option<String> {
    (platform == Platform::Presets).then(|| {
        say(
            "policy_presets",
            &[("agent", agent.name()), ("mode", mode.name())],
        )
    })
}

/// Whether the parent's store is reached without a credential or a managed
/// login, so a child's own neosian server could reach it (X7b).
pub fn local_store(selection: &[String], directory: &Path) -> bool {
    store::check(selection, directory).is_ok_and(|stores| !stores.credential && !stores.login)
}

/// The server's admission, before any client command runs (X4e P4): on
/// macOS the turn's policy prepared and proved in a private directory of
/// its own, which lives as long as the admission, so the client's metadata
/// commands run under it; and the notes the child's answers carry.
pub struct Admitted {
    pub launch: Launch,
    pub notes: Vec<String>,
    _private: Option<Private>,
}

pub fn admit(
    agent: Agent,
    mode: Mode,
    selection: &[String],
    directory: &Path,
    session: Option<&str>,
    (child_home, session_dir): (Option<&Path>, Option<&Path>),
) -> Result<Admitted, String> {
    if platform() == Platform::Presets {
        return Ok(Admitted {
            launch: presets(agent, selection, directory)?,
            notes: Vec::new(),
            _private: None,
        });
    }
    let private = Private::make_for(agent)
        .map_err(|error| say("sandbox_refused", &[("error", &error.to_string())]))?;
    let turn = Turn {
        agent,
        mode,
        directory,
        private: private.path(),
        selection,
        session,
        child_home,
        session_dir,
    };
    let (launch, credential) = confine(&turn, Path::new(macos::FACILITY))?;
    // A child of these kinds has no MCP at all: it never had the store to lose.
    let lost = credential && !matches!(agent, Agent::Pi | Agent::Cursor | Agent::Muse);
    Ok(Admitted {
        launch,
        notes: lost
            .then(|| say("sandbox_no_store", &[]))
            .into_iter()
            .collect(),
        _private: Some(private),
    })
}

/// One turn, as the watcher checked it.
pub struct Turn<'a> {
    pub agent: Agent,
    pub mode: Mode,
    /// The validated, canonical directory the child works in.
    pub directory: &'a Path,
    /// The turn's private directory (mode 0700, holding the child marker).
    pub private: &'a Path,
    /// The parent's store selection flags.
    pub selection: &'a [String],
    /// The session the turn starts or continues, which names Pi's state.
    pub session: Option<&'a str>,
    /// A Vibe or Muse child's private home (X6a).
    pub child_home: Option<&'a Path>,
    /// A Muse child's session directory, its only state (X6c U5).
    pub session_dir: Option<&'a Path>,
}

/// How one turn's harness is started.
#[derive(Debug)]
pub enum Launch {
    /// Under the common policy, which a probe showed refuses a write.
    Confined {
        profile: String,
        policy: Box<Policy>,
    },
    /// With the harness's own preset only, less the variables named.
    Presets { unset: Vec<String> },
}

/// The turn's launch: on macOS the policy built, applied to a probe write
/// that it must refuse, and only then returned; any failure refuses the
/// turn with a sentence, and nothing is started.
pub fn prepare(turn: &Turn) -> Result<Launch, String> {
    match platform() {
        Platform::Presets => presets(turn.agent, turn.selection, turn.directory),
        Platform::Common => confine(turn, Path::new(macos::FACILITY)).map(|(launch, _)| launch),
    }
}

/// The confined launch, and whether the parent's store is reached with a
/// credential the child does not get.
fn confine(turn: &Turn, facility: &Path) -> Result<(Launch, bool), String> {
    let stores = store::check(turn.selection, turn.directory)?;
    let home = super::adapter::home().ok_or_else(|| say("sandbox_home", &[]))?;
    let var = |name: &str| std::env::var_os(name);
    store::ready(&stores);
    let policy = policy::build(&Inputs {
        agent: turn.agent,
        mode: turn.mode,
        directory: turn.directory,
        private: turn.private,
        home: &home,
        stores: &stores,
        var: &var,
        session: turn.session,
        child_home: turn.child_home,
        session_dir: turn.session_dir,
        persist: &cursor_run::root(),
    })?;
    let profile = policy.profile();
    let target = macos::target(&policy, turn.private, &home)
        .ok_or_else(|| say("sandbox_unenforced", &[]))?;
    macos::probe(facility, &profile, &target)?;
    let policy = Box::new(policy);
    Ok((Launch::Confined { profile, policy }, stores.credential))
}

impl Launch {
    pub fn confined(&self) -> bool {
        matches!(self, Self::Confined { .. })
    }

    /// The command that starts `program` with `args`: the adapter's
    /// variables, then the policy's, then the policy's removals.
    pub fn command(
        &self,
        program: &Path,
        args: &[impl AsRef<OsStr>],
        env: &[(String, String)],
    ) -> Command {
        let mut command = match self {
            Self::Presets { .. } => Command::new(program),
            Self::Confined { profile, .. } => {
                macos::command(Path::new(macos::FACILITY), profile, program)
            }
        };
        command.args(args).envs(env.iter().map(|(k, v)| (k, v)));
        if let Self::Confined { policy, .. } = self {
            command.envs(policy.env.iter().map(|(k, v)| (k, v)));
        }
        self.strip(&mut command);
        command
    }

    /// Whether the child could write `path`, or replace it through its
    /// directory: the path, where it is and where it leads lie where an
    /// allowance reaches (X4f Q5). Protected places are not subtracted.
    pub fn writable(&self, path: &Path) -> bool {
        let Self::Confined { policy, .. } = self else {
            return false;
        };
        let placed = path
            .parent()
            .and_then(|dir| dir.canonicalize().ok())
            .zip(path.file_name())
            .map(|(dir, name)| dir.join(name));
        [Some(path.to_path_buf()), placed, path.canonicalize().ok()]
            .into_iter()
            .flatten()
            .any(|place| policy.allow.iter().any(|rule| macos::covers(rule, &place)))
    }

    /// The child's environment restrictions on a command that runs outside
    /// the policy, such as the client's sign-in status (X4e P4).
    pub fn strip(&self, command: &mut Command) {
        let unset = match self {
            Self::Confined { policy, .. } => &policy.unset,
            Self::Presets { unset } => unset,
        };
        for name in unset {
            command.env_remove(name);
        }
    }
}