neosian 3.0.0

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! The harness watcher's own filing (X6c U1). `neosian harness-file`
//! checks the watcher's plan again, takes the session and the parent from
//! it, builds the verb's arguments from it (the agent, the store selection
//! and `--project` the child's directory) and hands one event over here, in
//! the recorder's own vocabulary. No flag sets it and no payload shape
//! selects it, so whatever reaches `neosian record` on its standard input
//! goes through the client's ordinary hook readers, Antigravity's and
//! Vibe's included.

use std::path::PathBuf;

use super::cli::Record;
use super::payload::Payload;

/// One event of a watched turn, as `neosian harness-file` hands it over.
#[derive(Debug, Default)]
pub struct Watched {
    payload: Payload,
    parent: Option<String>,
}

/// The verb's arguments for one event of a watched turn: `agent` the
/// child's kind, `selection` the plan's store flags (`--root`, `--url`,
/// `--schema`, `--profile`, each checked by the plan), `project` the
/// child's directory. A Cursor event comes in Cursor's own hook vocabulary
/// and is read as its hooks' would be; the session is the one given.
pub fn record(
    agent: &str,
    selection: &[String],
    project: PathBuf,
    (mut payload, session): (Payload, &str),
    parent: Option<String>,
) -> Result<Record, String> {
    if agent == "cursor" {
        payload = super::payload::cursor_payload(payload);
    }
    if payload.get("session_id").and_then(|s| s.as_str()) != Some(session) || session.is_empty() {
        return Err("the payload carries no session_id".to_string());
    }
    let flag = |name: &str| {
        selection
            .chunks(2)
            .find(|pair| pair[0] == name)
            .map(|pair| pair[1].clone())
    };
    Ok(Record {
        profile: flag("--profile"),
        command: None,
        root: flag("--root").map(PathBuf::from),
        url: flag("--url"),
        schema: flag("--schema"),
        scope: None,
        mount: Vec::new(),
        agent: agent.to_string(),
        spool: None,
        json: true,
        project: Some(project),
        watched: Some(Watched { payload, parent }),
    })
}

/// The watcher's payload, when the verb was handed one.
pub(super) fn payload(args: &Record) -> Option<Payload> {
    args.watched.as_ref().map(|watched| watched.payload.clone())
}

/// The parent the turn links to: the watcher's plan names it; a hook reads
/// it from the variables a harness child's hooks inherit.
pub(super) fn parent(args: &Record, native: &str) -> Option<String> {
    match &args.watched {
        Some(watched) => watched.parent.clone(),
        None => super::settings::harness_parent(native),
    }
}