neosian 3.0.3

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! Beginning one turn (ยง40.50 items 4, 6, 7, amended by #430): the
//! harness found on an absolute PATH entry, its sign-in checked with its
//! own status command outside the sandbox (X4c), Codex's keys probed, the plan
//! the watcher will check again, the turn number the record compares with,
//! and, for a new session, the check that the announced session did not
//! exist already.

use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};

use serde_json::json;

use super::adapter::{self, Agent};
use super::link::{self, Naming};
use super::plan::WirePlan;
use super::registry::{Registry, available, shown};
use super::render::Child;
use super::signin::{self, Status};
use super::text::say;
use super::turn::{Announce, Turn};
use super::{opencode, probe, ready, status};
use crate::memory::dispatch::{Memory, Outcome};

/// How long `start` and `send` wait for a new session's ID.
const SESSION_WAIT: Duration = Duration::from_secs(3);

fn spawn_failed(agent: Agent, error: &str) -> Outcome {
    Outcome::fail(say(
        "spawn_failed",
        &[("agent", agent.name()), ("error", error)],
    ))
}

/// The check run when a new session is announced: whether its
/// conversation already had turns or a sessions document here.
fn announce(memory: &Memory, naming: &Naming, agent: Agent) -> Announce {
    let store = Arc::clone(&memory.store);
    let scopes: Vec<String> = memory.mounts.iter().map(|m| m.scope.clone()).collect();
    let naming = naming.clone();
    Box::new(move |native: &str| {
        let conversation = naming.conversation(agent.kind(), native);
        link::existed(&store, &scopes, &conversation)
    })
}

/// The store flags a read-only child's neosian server is started with
/// (#431): the server's own selection, its backend always named (X4f Q1),
/// and, unless a profile fixes them, every mount of this server, which the
/// watcher makes read-only.
fn store(registry: &Registry, memory: &Memory) -> Vec<String> {
    let mut flags = registry.selection.clone();
    if memory.store.profile().is_some() {
        return flags;
    }
    for mount in &memory.mounts {
        let access = match (mount.read_only, mount.edit_only) {
            (true, _) => ",ro",
            (_, true) => ",eo",
            _ => "",
        };
        flags.extend([
            "--mount".to_string(),
            format!("scope={},path={}{access}", mount.scope, mount.path),
        ]);
    }
    flags
}

/// One headless run of the child's harness with `prompt` on its stdin.
pub fn turn(
    registry: &Registry,
    memory: &Memory,
    naming: &Naming,
    child: &mut Child,
    prompt: &str,
) -> Result<(), Outcome> {
    let path = std::env::var_os("PATH");
    let Some(program) = adapter::which(child.agent.program(), path.as_deref()) else {
        let (found, _) = available();
        let list = if found.is_empty() {
            say("none_available", &[])
        } else {
            found.join(", ")
        };
        return Err(Outcome::fail(say(
            "missing_harness",
            &[("agent", child.agent.name()), ("available", &list)],
        )));
    };
    // The destination this server resolved is still the one on disk (X4f Q1).
    super::selection::drifted(
        &registry.selection,
        registry.identity.as_deref(),
        &child.directory,
    )
    .map_err(Outcome::fail)?;
    let store = store(registry, memory);
    let new_session = uuid::Uuid::new_v4().to_string();
    let session = child.session.clone().unwrap_or_else(|| new_session.clone());
    // Admission before any client command runs: on macOS the turn's policy
    // is prepared here, and the client's metadata commands run under it with
    // the child's environment (X4e P4); the watcher prepares its own again.
    // Cursor and Antigravity choose their own first session IDs.
    let announced = matches!(child.agent, Agent::Cursor | Agent::Agy);
    let known = !announced || child.session.is_some();
    // A Vibe child's private home is made at its first turn (X6a), and a
    // Muse child's session directory outside it (X6c U5).
    let child_home = ready::home(child).map_err(Outcome::fail)?;
    let session_dir = ready::session_dir(child, &session).map_err(Outcome::fail)?;
    let admitted = super::sandbox::admit(
        child.agent,
        child.mode,
        &store,
        &child.directory,
        known.then_some(session.as_str()),
        (child_home.as_deref(), session_dir.as_deref()),
    )
    .map_err(Outcome::fail)?;
    // One client executable for the status, the metadata and the turn, and
    // none the child could replace (X4f Q5).
    if admitted.launch.writable(&program) {
        let shown = program.display().to_string();
        return Err(Outcome::fail(say(
            "program_writable",
            &[("program", &shown)],
        )));
    }
    // A signed-out client gets the instruction to sign in, never a child;
    // a status neosian cannot read lets the turn start, with a note (X4c).
    // The status command runs outside the policy, which would refuse the
    // sign-in it may need to write, but without the child's credentials.
    let mut notes = admitted.notes.clone();
    let model = child.model.as_deref();
    let launch = (&admitted.launch, child_home.as_deref());
    match status::check(child.agent, &program, &child.directory, launch, model) {
        Some(Status::SignedOut) => return Err(Outcome::fail(signin::instruction(child.agent))),
        Some(Status::Invalid) => {
            let said = say("signin_invalid", &[("model", model.unwrap_or_default())]);
            return Err(Outcome::fail(said));
        }
        Some(Status::Unknown(reason)) => notes.push(signin::unknown(child.agent, &reason)),
        Some(Status::SignedIn) | None => {}
    }
    // A Pi, Cursor, Vibe, Antigravity or Muse session is continued only after
    // this child's own watcher filed a turn of it (X5b, X5d, X6a).
    let stateful = !matches!(
        child.agent,
        Agent::ClaudeCode | Agent::Codex | Agent::OpenCode
    );
    if stateful && child.session.is_some() && !child.claimed {
        let said = [("child", child.id.as_str()), ("agent", child.agent.name())];
        return Err(Outcome::fail(say("session_unclaimed", &said)));
    }
    if child.agent == Agent::Pi {
        // A continued Pi session is its exact file; checked here so `send`
        // refuses, and by the watcher again (X5a).
        if child.session.is_some() {
            ready::pi_file(&session, &child.directory).map_err(Outcome::fail)?;
        }
        notes.push(say("pi_no_mcp", &[]));
    }
    if child.agent == Agent::Cursor {
        ready::cursor(child).map_err(Outcome::fail)?;
    }
    // A Vibe child registers neosian's own server where the store is
    // reached without a credential (X7b); Cursor and Muse children have none.
    let local = super::sandbox::local_store(&store, &child.directory);
    let no_store = say("sandbox_no_store", &[]);
    match child.agent {
        Agent::Cursor | Agent::Agy => notes.push(say("no_mcp", &[("agent", child.agent.name())])),
        Agent::Muse => notes.push(say("muse_no_mcp", &[])),
        Agent::Vibe if local => notes.push(say("child_memory", &[("agent", "vibe")])),
        Agent::Vibe if !notes.contains(&no_store) => notes.push(no_store),
        _ => {}
    }
    if child.agent == Agent::Codex {
        // Checked here so `start` refuses; the watcher checks again.
        probe::codex(&admitted.launch, &program, &child.directory, child.mode)
            .map_err(Outcome::fail)?;
    }
    if child.agent == Agent::OpenCode {
        let carried = std::env::var(opencode::CONFIG_ENV).ok();
        opencode::config(carried.as_deref())
            .map_err(|_| Outcome::fail(say("bad_opencode_config", &[])))?;
    }
    drop(admitted);
    let nonce = uuid::Uuid::new_v4().simple().to_string();
    let wire = WirePlan {
        agent: child.agent.name().to_string(),
        mode: child.mode.name().to_string(),
        directory: shown(&child.directory),
        prompt_bytes: prompt.len(),
        model: child.model.clone(),
        resume: child.session.clone(),
        new_session: Some(new_session.clone()),
        parent: child.parent.clone(),
        store,
        nonce: Some(nonce.clone()),
        identity: registry.identity.clone(),
        home: super::selection::home().map(|home| home.display().to_string()),
        program: Some(program.display().to_string()),
        child_home: child_home.map(|home| home.display().to_string()),
        session_dir: session_dir.map(|dir| dir.display().to_string()),
    };
    // A new session has no turns; a continued one is read now, so the
    // turns that land during this one can be told apart.
    let before = match &child.session {
        Some(native) => {
            let conversation = naming.conversation(child.agent.kind(), native);
            link::last_turn_bounded(&memory.store, &conversation)
        }
        None => Ok(0),
    };
    let check = child
        .session
        .is_none()
        .then(|| announce(memory, naming, child.agent));
    let number = child.turns + 1;
    let limit = Duration::from_secs(registry.settings.turn_seconds);
    let mut turn = Turn::begin(
        child.agent,
        &json!(wire),
        prompt,
        &child.directory,
        limit,
        check,
        nonce,
    )
    .map_err(|error| spawn_failed(child.agent, &error.to_string()))?;
    turn.before = before;
    turn.notes = notes;
    if child.session.is_none() && child.agent.names_session() {
        child.session = Some(new_session);
    }
    child.turns = number;
    child.current = Some(turn);
    let deadline = Instant::now() + SESSION_WAIT;
    while !child.agent.names_session()
        && child.session.is_none()
        && Instant::now() < deadline
        && child.running()
    {
        child.learn();
        thread::sleep(Duration::from_millis(20));
    }
    Ok(())
}