rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! How rho starts its browser: the manager's options from `[browser]`, and
//! what to tell the user when the browser cannot start.

use super::config::BrowserConfig;
use rho_browser::diagnose::Problem;
use rho_browser::install::Install;
use rho_browser::manager::{Executable, ManagerError, ManagerOptions};
use rho_browser::Sandbox;
use std::path::Path;
use std::time::Duration;

/// How long the browser may go unused before it closes, unless
/// `[browser] unload_idle_after` says otherwise. Across this machine's 1,135
/// saved rho sessions, 71.8% of the gaps between consecutive web tool calls
/// (n = 177) were within 5 minutes, and only 2.8 points more within 10.
/// Starting the browser again takes about 0.1 s, so the limit only decides
/// how often idle tabs are lost.
pub(crate) const UNLOAD_IDLE_AFTER: Duration = Duration::from_secs(5 * 60);

/// How long a download of chrome-headless-shell may wait to connect, and
/// then go without receiving a byte, before it fails. GitHub's runners and
/// this machine fetched the 132 MB Linux archive in 4-7 s, so a healthy
/// connection never pauses for anywhere near this long.
const DOWNLOAD_STALL: Duration = Duration::from_secs(60);

/// The client that downloads chrome-headless-shell.
pub(crate) fn download_client() -> reqwest::Result<reqwest::Client> {
    crate::reqwest_client_builder()
        .connect_timeout(DOWNLOAD_STALL)
        .read_timeout(DOWNLOAD_STALL)
        .build()
}

/// The browser `config` names, or the pinned build, which `client`
/// downloads under `rho_home`.
pub(crate) fn executable(
    config: &BrowserConfig,
    rho_home: &Path,
    client: reqwest::Client,
) -> Executable {
    match &config.executable {
        Some(path) => Executable::Path(path.clone()),
        None => Executable::Pinned {
            install: Install::new(rho_home.join("browser")),
            client,
        },
    }
}

/// The manager's options for `config`, running `executable`. Profiles go
/// under `runtime_dir`, which only this user can read. Fails when the user's
/// proxy settings name a proxy the browser cannot connect through.
pub(crate) fn manager_options(
    config: &BrowserConfig,
    runtime_dir: &Path,
    executable: Executable,
) -> Result<ManagerOptions, String> {
    let sandbox = match config.sandbox {
        Some(false) => Sandbox::Disabled,
        Some(true) | None => Sandbox::Enabled,
    };
    Ok(ManagerOptions {
        executable,
        profiles_dir: runtime_dir.join("browser"),
        sandbox,
        unload_idle_after: config.unload_idle_after.unwrap_or(UNLOAD_IDLE_AFTER),
        navigation: super::navigation::check(),
        connect: super::connect::connect()?,
    })
}

/// What to tell the user when the browser cannot start: what went wrong,
/// each cause rho found, and how to fix it. rho never retries without the
/// sandbox on its own.
pub(crate) fn failure_text(error: &ManagerError) -> String {
    let mut text = error.to_string();
    match error {
        ManagerError::UnsupportedPlatform => {
            text.push_str("; set `executable` under `[browser]` in config.toml to a Chrome");
        }
        ManagerError::Launch {
            executable,
            problems,
            ..
        } => {
            for problem in problems {
                let fix = match problem {
                    Problem::UserNamespacesRestricted => format!(
                        "allow user namespaces for {} with an AppArmor profile \
                         ({DOCS}#browser), or set `sandbox = false` under `[browser]` \
                         in config.toml to run it without its sandbox",
                        executable.display()
                    ),
                    Problem::MissingLibraries(_) => {
                        let deb_deps = executable.with_file_name("deb.deps");
                        if deb_deps.is_file() {
                            format!(
                                "install them; on Debian and Ubuntu, {} lists the packages",
                                deb_deps.display()
                            )
                        } else {
                            "install them".to_owned()
                        }
                    }
                };
                text.push_str(&format!("\n{problem}\nfix: {fix}"));
            }
        }
        ManagerError::Install(_)
        | ManagerError::Proxy(_)
        | ManagerError::Profile { .. }
        | ManagerError::Context(_)
        | ManagerError::Guard(_)
        | ManagerError::Hung { .. } => {}
    }
    text
}

const DOCS: &str = "https://matthewyjiang.github.io/rho/configuration";