axiopera 0.3.2

Orchestrate coding agents (Claude Code, Codex, Grok Build) as sub-agents of each other
//! CLI side: reach the daemon (starting it if needed), send one request, hand back the replies.

use std::fs::File;
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail};
use serde_json::Value;

use crate::ipc::{self, Envelope, ErrorCode, Reply, Request};
use crate::{identity, platform};

const SANDBOX_HINT: &str =
    "re-run this command with escalated permissions outside the agent sandbox";

pub struct Client {
    pub home: PathBuf,
    pub json: bool,
}

/// One line of the daemon's answer.
pub enum Line {
    Out { text: String, data: Value },
    Error(String),
}

impl Client {
    /// Send one request as `caller` (default: the one detected) and print the replies. Returns
    /// the exit code.
    pub async fn run(&self, caller: Option<String>, request: Request) -> Result<i32> {
        let caller = caller.unwrap_or_else(detect_caller);
        let launch = !matches!(request, Request::Stop { .. });
        self.request_as(caller, launch, request, |line| match line {
            Line::Out { text, data } if self.json => {
                // The text carries result bodies that `data` does not repeat.
                let mut line = serde_json::json!({"text": text});
                if !data.is_null() {
                    line["data"] = data;
                }
                println!("{line}");
            }
            Line::Out { text, .. } if !text.is_empty() => println!("{text}"),
            Line::Out { .. } => {}
            Line::Error(message) => eprintln!("axiopera: {message}"),
        })
        .await
    }

    /// Send one request, handing each line of the answer to `sink`. Returns the exit code.
    pub async fn request(&self, request: Request, sink: impl FnMut(Line)) -> Result<i32> {
        let launch = !matches!(request, Request::Stop { .. });
        self.request_as(detect_caller(), launch, request, sink)
            .await
    }

    /// Without `launch`, a daemon that is not running answers nothing.
    pub async fn request_as(
        &self,
        caller: String,
        launch: bool,
        request: Request,
        mut sink: impl FnMut(Line),
    ) -> Result<i32> {
        if !request.allowed_in_sandbox() && platform::self_sandboxed() {
            sink(Line::Error(SANDBOX_HINT.into()));
            return Ok(ipc::exit::ERROR);
        }
        let env = request.needs_env().then(|| std::env::vars().collect());
        let sub_agent = identity::task_of(&caller).is_some();
        let envelope = Envelope {
            protocol: ipc::PROTOCOL,
            version: env!("CARGO_PKG_VERSION").into(),
            caller,
            cwd: std::env::current_dir()?,
            env,
            request,
        };
        // A version mismatch makes an idle daemon exit; then a fresh one is started.
        for _ in 0..3 {
            let stream = self.connect(launch).await?;
            let Some(stream) = stream else {
                return Ok(ipc::exit::OK);
            };
            match self.exchange(stream, &envelope, &mut sink).await? {
                Some(code) => return Ok(code),
                None => tokio::time::sleep(Duration::from_millis(500)).await,
            }
        }
        if sub_agent {
            bail!("daemon version mismatch persists; only the main session or the user can stop it")
        }
        bail!(
            "daemon version mismatch persists; `axiopera daemon stop --force` interrupts active tasks from all sessions and stops the daemon"
        )
    }

    /// Ok(None) only without `may_launch`, when nothing is running.
    async fn connect(&self, may_launch: bool) -> Result<Option<platform::ClientStream>> {
        let stream = match platform::connect(&self.home).await {
            Ok(s) => s,
            // Sandboxes that block sockets fail here rather than with "not found".
            Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => bail!("{SANDBOX_HINT}"),
            Err(_) if !may_launch => return Ok(None),
            Err(_) if platform::self_sandboxed() => bail!("{SANDBOX_HINT}"),
            Err(_) => launch(&self.home).await?,
        };
        platform::check_server(&stream)?;
        Ok(Some(stream))
    }

    /// Returns the exit code, or None when the daemon asked us to retry after a version change.
    async fn exchange(
        &self,
        stream: platform::ClientStream,
        envelope: &Envelope,
        sink: &mut impl FnMut(Line),
    ) -> Result<Option<i32>> {
        let (r, mut w) = tokio::io::split(stream);
        ipc::write(&mut w, envelope).await?;
        let mut reader = ipc::Reader::new(r);
        while let Some(reply) = reader.read::<Reply>().await? {
            match reply {
                Reply::Out { text, data } => sink(Line::Out { text, data }),
                Reply::Done { exit } => return Ok(Some(exit)),
                Reply::Error {
                    code: ErrorCode::Version,
                    ..
                } => return Ok(None),
                Reply::Error { message, .. } => {
                    sink(Line::Error(message));
                    return Ok(Some(ipc::exit::ERROR));
                }
            }
        }
        bail!("daemon closed the connection without finishing")
    }
}

pub fn detect_caller() -> String {
    identity::detect(|k| std::env::var(k).ok(), platform::ancestor_names)
}

/// Start the daemon unless another caller is already doing it, then wait for its endpoint.
async fn launch(home: &Path) -> Result<platform::ClientStream> {
    let dir = home.join("daemon");
    // A sandbox that lets the missing socket through as "not found" shows here instead.
    let unwritable = |e: std::io::Error| match e.kind() {
        std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::ReadOnlyFilesystem => {
            anyhow!(
                "cannot write {}: {e}; in an agent sandbox, {SANDBOX_HINT}",
                dir.display()
            )
        }
        _ => anyhow!(e).context(format!("writing {}", dir.display())),
    };
    std::fs::create_dir_all(&dir).map_err(unwritable)?;
    let lock = File::create(dir.join("launch.lock")).map_err(unwritable)?;
    tokio::task::block_in_place(|| lock.lock()).context("locking launch.lock")?;
    if let Ok(s) = platform::connect(home).await {
        return Ok(s);
    }
    let exe = std::env::current_exe()?;
    let home_arg = home.to_string_lossy();
    // A broken config.toml would only show up as a daemon that never answers.
    crate::config::load(home)?;
    platform::spawn_detached(&exe, &["daemon", "run", "--home", &home_arg])
        .context("starting the daemon")?;
    for _ in 0..100 {
        tokio::time::sleep(Duration::from_millis(50)).await;
        if let Ok(s) = platform::connect(home).await {
            return Ok(s);
        }
    }
    bail!(
        "the daemon did not come up; see {}",
        dir.join("daemon.log").display()
    )
}