neosian 3.0.0

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! What a turn needs ready before the server starts its watcher: a
//! continued Pi session's exact file, a Cursor chat and workspace trust, and
//! a child's private home (X5a to X6b).

use std::path::PathBuf;

use super::adapter;
use super::render::Child;
use super::text::say;

/// The file of the Pi session the child continues, found as the watcher
/// will find it.
pub(super) fn pi_file(session: &str, directory: &std::path::Path) -> Result<(), String> {
    use super::sandbox::pi;
    let home = adapter::home().and_then(|home| home.canonicalize().ok());
    let home = home.ok_or_else(|| say("sandbox_home", &[]))?;
    let var = |name: &str| std::env::var_os(name);
    let dir = pi::session_dir(&home, &var, session)?;
    pi::session_file(&dir, session, directory).map(drop)
}

/// A Cursor child continues only a chat that is still Cursor's own (X5d),
/// and starts only in a workspace Cursor already trusts: the policy keeps
/// Cursor's trust marker unwritable, so `--trust` could not record it (X5b).
pub(super) fn cursor(child: &Child) -> Result<(), String> {
    let home = adapter::home().and_then(|home| home.canonicalize().ok());
    let home = home.ok_or_else(|| say("sandbox_home", &[]))?;
    let var = |name: &str| std::env::var_os(name);
    if let Some(chat) = &child.session {
        super::sandbox::cursor::chat_ready(&home, &var, &child.directory, chat)?;
    }
    if !super::sandbox::cursor::trusted(&home, &var, &child.directory) {
        let shown = child.directory.display().to_string();
        return Err(say("cursor_untrusted", &[("directory", &shown)]));
    }
    Ok(())
}

/// A Muse child's session directory (X6c U5): made at its first turn,
/// outside the child, where Muse would put it; the one made then, checked
/// again, for a later turn.
pub(super) fn session_dir(child: &mut Child, session: &str) -> Result<Option<PathBuf>, String> {
    use super::sandbox::muse;
    if child.agent != adapter::Agent::Muse {
        return Ok(None);
    }
    let home = adapter::home().and_then(|home| home.canonicalize().ok());
    let home = home.ok_or_else(|| say("sandbox_home", &[]))?;
    let var = |name: &str| std::env::var_os(name);
    let dir = match &child.session_dir {
        Some(dir) => muse::validate(&home, &var, dir, session)?,
        None => muse::make(&home, &var, session)?,
    };
    child.session_dir = Some(dir.clone());
    Ok(Some(dir))
}

/// A Vibe or Muse Code child's private home: made new at its first turn,
/// kept after (X6a, X6b). Before a later turn's sign-in probe, Muse's is
/// made fresh, so the probe never reads what a child wrote (X6c U2).
pub(super) fn home(child: &mut Child) -> Result<Option<PathBuf>, String> {
    match &child.home {
        None if child.turns == 0 => {
            child.home = super::home::make(child.agent)?;
            Ok(child.home.clone())
        }
        None => Ok(None),
        Some(home) => {
            if child.agent == adapter::Agent::Muse {
                super::home::renew(child.agent, home, None)?;
            }
            Ok(Some(home.clone()))
        }
    }
}