neosian 2.12.0

The state layer for LLM agents, as one static binary: the record, memory and the state process
//! The argv grammar (DESIGN ยง14.1): stdout carries the artifact, stderr the
//! guidance; a bad invocation exits 2, the operation's own failure 1.

use std::ffi::OsString;
use std::io::{self, IsTerminal, Write};
use std::process::ExitCode;

use clap::{CommandFactory, Parser, Subcommand};
use serde_json::json;

use crate::conversation::{cli_continue, cli_search};
use crate::{clients, docs, mcp, memory, messaging, operator, record, serve, update, version};

#[derive(Parser)]
#[command(
    name = "neosian",
    version = version::VERSION,
    about = "The state layer for LLM agents",
    disable_help_subcommand = true
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Capture explicitly registered native memory files into local memory.
    NativeMemory {
        #[command(subcommand)]
        command: crate::native_memory::cli::Command,
    },
    /// Exchange current-memory snapshots and deltas with a local replica.
    Exchange {
        #[command(subcommand)]
        command: crate::exchange::cli::Command,
    },
    /// Read durable scope changes or explicitly prune their retained events.
    Changes {
        #[command(subcommand)]
        command: crate::changes::cli::Command,
    },
    /// Review memory quality and apply explicitly approved, guarded changes.
    Upkeep {
        #[command(subcommand)]
        command: crate::upkeep::cli::Command,
    },
    /// Manage reusable skills, supporting files and profile revision guards.
    Skill {
        #[command(subcommand)]
        command: crate::skill::cli::Command,
    },
    /// Import, structure, review, retrieve and export scoped knowledge.
    Knowledge {
        #[command(subcommand)]
        command: crate::knowledge::cli::Command,
    },
    /// Create, exchange and bind portable workspace profiles.
    Profile {
        #[command(subcommand)]
        command: crate::profile::cli::Command,
    },
    /// Wire the installed agents to this store: MCP and the hooks.
    Setup(clients::setup::Setup),
    /// Is this machine set up? The home and the clients.
    Status(clients::status::Status),
    /// Display cache information from a client's statusline JSON on stdin.
    Statusline(clients::statusline::Statusline),
    /// Install a newer binary release, or preview it with --check.
    Update(update::Update),
    /// Display version information.
    Version,
    /// Print a machine-readable contract or operator-owned Postgres DDL.
    Schema(crate::schema::Schema),
    /// Print a shipped docs page, or list the topics.
    Docs {
        /// Topic to print (omit to list the topics)
        topic: Option<String>,
        /// One JSON object on stdout
        #[arg(long = "json")]
        json: bool,
    },
    /// Read and write agent memory from the shell.
    Memory {
        #[command(subcommand)]
        command: memory::grammar::Command,
    },
    /// Read a scope's ledger, optionally merged with one conversation.
    Audit(operator::audit::Audit),
    /// Export selected scopes and conversations to a FileStore archive.
    Export(operator::transfer::Transfer),
    /// Restore a FileStore archive into empty target units.
    Import(operator::transfer::Transfer),
    /// Blank recorded turns and, with --all, their sessions documents.
    Redact(operator::erasure::Redact),
    /// Erase conversations whose newest turn predates a cutoff.
    Prune(operator::erasure::Prune),
    /// Find the turns holding every term, newest first (DESIGN ยง32).
    Search(cli_search::Search),
    /// Print a recorded conversation as continue_session delivers it (ยง33).
    Continue(cli_continue::Continue),
    /// Durable messages and reminders.
    Messages(messaging::cli::Messages),
    /// Record a foreign agent's session from its hooks: one payload on stdin per call (ยง20.9).
    Record(record::cli::Record),
    /// Serve neosian memory to MCP clients on stdio (ยง39.3).
    Mcp(mcp::cli::Mcp),
    /// Serve neosian memory and conversations over HTTP: the state process (ยง18).
    Serve(serve::cli::Serve),
}

/// A grammar-tier refusal (exit 2): the message on stderr, and where
/// `--json` asked, one `{"error": "usage", "hint"}` object on stdout.
pub fn usage(
    json: bool,
    message: &str,
    out: &mut impl Write,
    err: &mut impl Write,
) -> io::Result<u8> {
    if json {
        writeln!(out, "{}", json!({"error": "usage", "hint": message}))?;
    }
    writeln!(err, "error: {message}")?;
    Ok(2)
}

/// Parse and run; the exit code is the verb's.
pub fn run<I>(args: I) -> ExitCode
where
    I: IntoIterator<Item = OsString>,
{
    let args: Vec<OsString> = args.into_iter().collect();
    let stdout = io::stdout();
    let stderr = io::stderr();
    let (mut out, mut err) = (stdout.lock(), stderr.lock());
    if args.len() == 1 {
        return finish(
            writeln!(out, "{}", Cli::command().render_help()).map(|()| 0),
            &mut err,
        );
    }
    let cli = match Cli::try_parse_from(&args) {
        Ok(cli) => cli,
        Err(error) => {
            // The grammar's own text stays on stderr; a caller that asked
            // for JSON also reads the refusal as JSON (DESIGN ยง30.1).
            if error.use_stderr() && args.iter().any(|arg| arg == "--json") {
                let text = error.to_string();
                let hint = text.lines().next().unwrap_or_default();
                let hint = hint.strip_prefix("error: ").unwrap_or(hint);
                let _ = writeln!(out, "{}", json!({"error": "usage", "hint": hint}));
            }
            error.exit()
        }
    };
    let verb = match &cli.command {
        Command::Status(_) => "status",
        _ => "",
    };
    if matches!(
        &cli.command,
        Command::Audit(_)
            | Command::Export(_)
            | Command::Import(_)
            | Command::Redact(_)
            | Command::Prune(_)
            | Command::Serve(_)
    ) {
        let selection = std::env::current_dir()
            .map_err(|e| e.to_string())
            .and_then(|cwd| crate::profile::resolve::selected(None, &cwd));
        let issue = match selection {
            Ok(None) => None,
            Ok(Some(_)) => Some("this operator command requires an explicit legacy store outside a selected profile".into()),
            Err(message) => Some(message),
        };
        if let Some(message) = issue {
            return finish(
                usage(
                    args.iter().any(|a| a == "--json"),
                    &message,
                    &mut out,
                    &mut err,
                ),
                &mut err,
            );
        }
    }
    if update::human_door(
        verb,
        stdout.is_terminal(),
        args.iter().any(|s| s == "--json"),
    ) {
        match update::human_check(&mut err) {
            Ok(Some(code)) => return ExitCode::from(code),
            Ok(None) => {}
            Err(error) => {
                let _ = writeln!(err, "update: {error}");
            }
        }
    }
    let code = match cli.command {
        Command::NativeMemory { command } => {
            crate::native_memory::cli::run(&command, &mut out, &mut err)
        }
        Command::Exchange { command } => crate::exchange::cli::run(&command, &mut out, &mut err),
        Command::Changes { command } => crate::changes::cli::run(&command, &mut out, &mut err),
        Command::Upkeep { command } => crate::upkeep::cli::run(&command, &mut out, &mut err),
        Command::Skill { command } => crate::skill::cli::run(&command, &mut out, &mut err),
        Command::Knowledge { command } => crate::knowledge::cli::run(&command, &mut out, &mut err),
        Command::Profile { command } => crate::profile::cli::run(&command, &mut out, &mut err),
        Command::Setup(args) => clients::setup::run(&args, &mut out, &mut err),
        Command::Status(args) => clients::status::run(&args, &mut out, &mut err),
        Command::Statusline(args) => clients::statusline::run(&args, &mut out, &mut err),
        Command::Update(args) => update::run(&args, &mut out, &mut err),
        Command::Version => writeln!(out, "{}", version::line()).map(|()| 0),
        Command::Schema(args) => crate::schema::run(&args, &mut out, &mut err),
        Command::Docs { topic, json } => docs::run(topic.as_deref(), json, &mut out, &mut err),
        Command::Memory { command } => memory::cli::run(&command, &mut out, &mut err),
        Command::Audit(args) => operator::audit::run(&args, &mut out, &mut err),
        Command::Export(args) => operator::transfer::run(&args, false, &mut out, &mut err),
        Command::Import(args) => operator::transfer::run(&args, true, &mut out, &mut err),
        Command::Redact(args) => operator::erasure::redact(&args, &mut out, &mut err),
        Command::Prune(args) => operator::erasure::prune(&args, &mut out, &mut err),
        Command::Search(args) => cli_search::run(&args, &mut out, &mut err),
        Command::Continue(args) => cli_continue::run(&args, &mut out, &mut err),
        Command::Messages(args) => messaging::cli::run(&args, &mut out, &mut err),
        Command::Record(args) => record::cli::run(&args, &mut out, &mut err),
        Command::Mcp(args) => mcp::cli::run(&args, &mut out, &mut err),
        Command::Serve(args) => serve::cli::run(&args, &mut out, &mut err),
    };
    finish(code, &mut err)
}

fn finish(code: io::Result<u8>, err: &mut impl Write) -> ExitCode {
    match code {
        Ok(code) => ExitCode::from(code),
        // A closed pipe is the reader's choice, not an error of ours.
        Err(error) if error.kind() == io::ErrorKind::BrokenPipe => ExitCode::SUCCESS,
        Err(error) => {
            let _ = writeln!(err, "error: {error}");
            ExitCode::from(1)
        }
    }
}