systemprompt-agent 0.8.0

Agent-to-Agent (A2A) protocol for systemprompt.io AI governance: streaming, JSON-RPC models, task lifecycle, .well-known discovery, and governed agent orchestration.
Documentation
//! Build the `Command` used to spawn a detached agent subprocess and rotate its
//! log file.

use anyhow::Result;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::process::Command;
use systemprompt_models::{CliPaths, Config, Secrets};

use crate::services::agent_orchestration::{OrchestrationError, OrchestrationResult};

const MAX_LOG_SIZE: u64 = 10 * 1024 * 1024;

pub(super) fn rotate_log_if_needed(log_path: &Path) -> Result<()> {
    if let Ok(metadata) = fs::metadata(log_path) {
        if metadata.len() > MAX_LOG_SIZE {
            let backup_path = log_path.with_extension("log.old");
            fs::rename(log_path, &backup_path)?;
        }
    }
    Ok(())
}

pub(super) fn prepare_agent_log_file(
    agent_name: &str,
    log_dir: &Path,
) -> OrchestrationResult<File> {
    if let Err(e) = fs::create_dir_all(log_dir) {
        tracing::error!(
            error = %e,
            path = %log_dir.display(),
            "Failed to create agent log directory - agent may fail to start"
        );
    }

    let log_file_path = log_dir.join(format!("agent-{}.log", agent_name));
    if let Err(e) = rotate_log_if_needed(&log_file_path) {
        tracing::warn!(
            error = %e,
            path = %log_file_path.display(),
            "Failed to rotate agent log file"
        );
    }

    fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_file_path)
        .map_err(|e| {
            OrchestrationError::ProcessSpawnFailed(format!(
                "Failed to create log file {}: {}",
                log_file_path.display(),
                e
            ))
        })
}

fn configure_secrets_env(command: &mut Command, secrets: &Secrets) {
    if let Some(ref key) = secrets.gemini {
        command.env("GEMINI_API_KEY", key);
    }
    if let Some(ref key) = secrets.anthropic {
        command.env("ANTHROPIC_API_KEY", key);
    }
    if let Some(ref key) = secrets.openai {
        command.env("OPENAI_API_KEY", key);
    }
    if let Some(ref key) = secrets.github {
        command.env("GITHUB_TOKEN", key);
    }

    if !secrets.custom.is_empty() {
        let uppercase_keys = secrets.custom_env_var_names();
        command.env("SYSTEMPROMPT_CUSTOM_SECRETS", uppercase_keys.join(","));
        for (env_name, value) in secrets.custom_env_vars() {
            command.env(env_name, value);
        }
    }
}

pub(super) struct BuildAgentCommandParams<'a> {
    pub binary_path: &'a PathBuf,
    pub agent_name: &'a str,
    pub port: u16,
    pub profile_path: &'a str,
    pub secrets: &'a Secrets,
    pub config: &'a Config,
    pub log_file: File,
}

pub(super) fn build_agent_command(params: BuildAgentCommandParams<'_>) -> Command {
    let BuildAgentCommandParams {
        binary_path,
        agent_name,
        port,
        profile_path,
        secrets,
        config,
        log_file,
    } = params;
    let mut command = Command::new(binary_path);
    for arg in CliPaths::agent_run_args() {
        command.arg(arg);
    }
    command
        .arg("--agent-name")
        .arg(agent_name)
        .arg("--port")
        .arg(port.to_string())
        .env_clear();
    if let Ok(path) = std::env::var("PATH") {
        command.env("PATH", path);
    }
    if let Ok(home) = std::env::var("HOME") {
        command.env("HOME", home);
    }
    command
        .env("SYSTEMPROMPT_PROFILE", profile_path)
        .env("SYSTEMPROMPT_SUBPROCESS", "1")
        .env("JWT_SECRET", &secrets.jwt_secret)
        .env(
            "MANIFEST_SIGNING_SECRET_SEED",
            secrets
                .manifest_signing_secret_seed
                .as_deref()
                .unwrap_or(""),
        )
        .env("DATABASE_URL", &secrets.database_url)
        .env("AGENT_NAME", agent_name)
        .env("AGENT_PORT", port.to_string())
        .env("DATABASE_TYPE", &config.database_type)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::from(log_file))
        .stdin(std::process::Stdio::null());

    if let Ok(fly_app) = std::env::var("FLY_APP_NAME") {
        command.env("FLY_APP_NAME", fly_app);
    }

    configure_secrets_env(&mut command, secrets);
    command
}