rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The `rho/forge/*` host calls on the daemon's machine (`docs/protocol.md`,
//! "The forge CLI on the daemon's machine"): rho-ui's check of `gh`, its
//! acting on a pull request through it, and its fetching of a forge's
//! pictures, which it ran over ssh as shell scripts, ported from those
//! scripts and the Rust twins it ran on its own machine. Each call runs
//! `gh` with the login environment, never letting it prompt, within
//! [`FORGE_DEADLINE_SECS`] in all, and is logged with the pull request it
//! names, or the scheme, host and path of a URL: never a URL's query, a
//! comment's text or a token.
//!
//! This module answers the calls and keeps how `gh` runs; `status`, `act`
//! and `fetch` do the work.

use std::{future::Future, path::Path, time::Duration};

use rho_protocol::{
    methods::{
        ForgeFetchParams, ForgeFetchResponse, ForgePullRequestActParams,
        ForgePullRequestActResponse, ForgeStatusParams, GhStatus, FORGE_DEADLINE_SECS,
    },
    Caller, ClientEnvironment, ForgeService, RpcError,
};
use tokio::process::Command;

use super::machine::{
    absolute, blocking, failed, host_path, log_call, spawn_stream, Machine, Origin,
    LOGIN_SHELL_DEADLINE,
};
use crate::{
    app::pull_requests::quiet_gh,
    process_tree::{supervised_output, supervised_output_with_input},
};

mod act;
mod fetch;
mod status;

pub(super) use fetch::Web;

/// [`FORGE_DEADLINE_SECS`], which each forge call has in all.
const FORGE_DEADLINE: Duration = Duration::from_secs(FORGE_DEADLINE_SECS);

impl ForgeService for Machine {
    /// Answered in `status.rs`.
    async fn status(
        &self,
        caller: Caller,
        params: ForgeStatusParams,
    ) -> Result<GhStatus, RpcError> {
        status::status(self, caller, params).await
    }

    /// Answered in `act.rs`.
    async fn act(
        &self,
        caller: Caller,
        params: ForgePullRequestActParams,
    ) -> Result<ForgePullRequestActResponse, RpcError> {
        act::act(self, caller, params).await
    }

    /// Answered in `fetch.rs`.
    async fn fetch(
        &self,
        caller: Caller,
        params: ForgeFetchParams,
    ) -> Result<ForgeFetchResponse, RpcError> {
        fetch::fetch(self, caller, params, self.forge_web()).await
    }
}

/// The value of `name` in `environment`, if set.
fn var<'a>(environment: &'a ClientEnvironment, name: &str) -> Option<&'a str> {
    environment
        .iter()
        .find_map(|(set, value)| (set == name).then_some(value))
}

/// `gh` at `program`, to run with only the login's `environment`, quiet,
/// with no input, and killed when dropped ([`quiet_gh`]), by
/// [`supervised_output`], in a process tree of its own that is killed with
/// all in it once it ends or the call stops waiting, as at its deadline.
fn gh_command(program: &Path, environment: &ClientEnvironment) -> Command {
    let mut command = Command::new(program);
    // Before `quiet_gh`, whose variables `env_clear` would forget.
    command.env_clear().envs(environment.iter());
    quiet_gh(&mut command);
    command
}

/// Runs `call`, all of a forge call that runs `gh`, within
/// [`FORGE_DEADLINE`]: past it, the `gh` running is killed and the call
/// fails saying so.
async fn within_deadline<T>(
    call: impl Future<Output = Result<T, RpcError>>,
) -> Result<T, RpcError> {
    match tokio::time::timeout(FORGE_DEADLINE, call).await {
        Ok(answer) => answer,
        Err(_) => {
            tracing::warn!(
                deadline_secs = FORGE_DEADLINE_SECS,
                "gh didn't answer in time, so it was killed"
            );
            Err(failed(format!(
                "gh didn't answer within {FORGE_DEADLINE_SECS} s (FORGE_DEADLINE_SECS)."
            )))
        }
    }
}

#[cfg(all(test, unix))]
#[path = "machine_forge_tests.rs"]
mod tests;