neosian 3.0.0

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! A child as the registry holds it (ยง40.50 items 2, 6): its harness, mode
//! and directory, its harness session, its turns, the private home a Vibe
//! or Muse Code child keeps for its life, removed when the child is
//! forgotten (X6a), and the session directory made for a Muse child
//! (X6c U5).

use serde_json::Value;

use super::adapter::{Agent, Mode};
use super::turn::{End, Turn, lock};

/// A child as the registry holds it.
pub struct Child {
    pub id: String,
    pub agent: Agent,
    pub mode: Mode,
    pub model: Option<String>,
    pub directory: std::path::PathBuf,
    pub parent: Option<String>,
    pub session: Option<String>,
    /// The session the harness announced already existed in this store.
    pub existed: bool,
    /// One of its turns was filed by its own watcher (X4f Q3).
    pub claimed: bool,
    pub turns: u32,
    pub stopped: bool,
    pub current: Option<Turn>,
    pub limit_seconds: u64,
    /// The private home made at its first turn (X6a, X6c U10).
    pub home: Option<std::path::PathBuf>,
    /// A Muse child's session directory, made at its first turn (X6c U5).
    pub session_dir: Option<std::path::PathBuf>,
}

impl Child {
    pub fn running(&self) -> bool {
        self.current.as_ref().is_some_and(|turn| !turn.ended())
    }

    /// The harness session, learned from the current turn when it is new.
    pub fn learn(&mut self) {
        if self.session.is_none()
            && let Some(turn) = &self.current
        {
            let shared = lock(&turn.shared);
            self.session = shared.session.clone();
            self.existed = shared.existed;
        }
    }

    /// The session already had turns here when the announce check or the
    /// watcher's filing looked: it is never continued (X4e P2).
    pub fn collided(&self) -> bool {
        self.existed || self.receipt(&["existing"])
    }

    /// Whether the current turn's receipt says one of `dispositions`.
    fn receipt(&self, dispositions: &[&str]) -> bool {
        self.current
            .as_ref()
            .is_some_and(|turn| filed_as(lock(&turn.shared).filed.as_ref(), dispositions))
    }

    /// The child's own watcher filed a turn of this session: the session is
    /// this child's, whoever writes it later (X4f Q3).
    pub fn claim(&mut self) -> bool {
        self.claimed = self.claimed || self.receipt(&["recorded", "pending"]);
        self.claimed
    }

    pub(super) fn state(&self) -> &'static str {
        match (self.stopped, self.outcome()) {
            (true, _) => "stopped",
            (false, outcome) => outcome,
        }
    }

    /// How the current turn itself went, whatever became of the child.
    pub(super) fn outcome(&self) -> &'static str {
        let Some(turn) = &self.current else {
            return "starting";
        };
        let shared = lock(&turn.shared);
        if !shared.ended() {
            return "running";
        }
        match shared.end {
            Some(End::Exited(0)) if shared.failure.is_none() => "finished",
            Some(End::TimedOut) => "timed_out",
            Some(End::Stopped) => "stopped",
            _ => "failed",
        }
    }
}

pub(super) fn filed_as(filed: Option<&Value>, dispositions: &[&str]) -> bool {
    filed.is_some_and(|filed| dispositions.iter().any(|d| filed["disposition"] == *d))
}

impl Drop for Child {
    /// A forgotten child takes its private home with it (X6a, X6b), and a
    /// Muse session directory no turn wrote (X6c U5).
    fn drop(&mut self) {
        if let Some(home) = &self.home {
            super::home::forget(home);
        }
        if let Some(dir) = &self.session_dir {
            let _ = std::fs::remove_dir(dir);
        }
    }
}