rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! CLI arguments for `rho daemon`.

use std::{net::SocketAddr, time::Duration};

use clap::Subcommand;

use crate::app::automation_protocol::parse_duration;

/// The shortest `rho daemon run --unload-idle-after`. Shorter, a session
/// could unload between one client's consecutive requests, as a reopen
/// takes up to 41 ms measured (`UNLOAD_IDLE_AFTER` has the receipt), and
/// the sweep, which runs once per period, would spin.
const MIN_UNLOAD_IDLE_AFTER: Duration = Duration::from_millis(100);

fn parse_unload_idle_after(value: &str) -> Result<Duration, String> {
    let duration = parse_duration(value)?;
    if duration < MIN_UNLOAD_IDLE_AFTER {
        return Err(format!(
            "invalid duration '{value}': must be at least {}",
            humantime::format_duration(MIN_UNLOAD_IDLE_AFTER)
        ));
    }
    Ok(duration)
}

#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonCommand {
    /// Run the daemon in the foreground until stopped. The default.
    Run {
        /// Unload a session once it has sat idle this long: no turn under
        /// way and no client attached. Unloading closes its MCP servers and
        /// lets other rho processes open it; the next prompt or attach
        /// reopens it. A session that saved nothing is discarded instead.
        /// Defaults to 60s; at least 100ms.
        #[arg(long, value_name = "DURATION", value_parser = parse_unload_idle_after)]
        unload_idle_after: Option<Duration>,
        /// The lock and socket descriptors a daemon that re-executes itself
        /// in place hands the new binary. Only the daemon passes it.
        #[arg(long, value_name = "LOCK:SOCKET", hide = true)]
        exec_handoff: Option<String>,
    },
    /// Start the daemon in the background unless it is already running,
    /// and wait until it answers.
    ///
    /// Exits 0 once a daemon runs, including one that already did. Waits
    /// for a daemon that is stopping to exit before starting a new one.
    Start {
        /// Print one JSON document instead of text: the daemon's pid and
        /// version, or, for a daemon that refused this rho over its
        /// protocol version, the refusal, which says which side to change
        /// and what restarting the daemon would cost.
        #[arg(long)]
        json: bool,
    },
    /// Show the running daemon's pid, version, uptime, socket, and any
    /// upgrade it waits to run.
    ///
    /// Never starts a daemon. Exits with status 1 when none is running.
    Status {
        /// Print one JSON document instead of text.
        #[arg(long)]
        json: bool,
    },
    /// Stop the running daemon and wait for it to exit.
    ///
    /// Exits with status 1 when none is running.
    Stop,
    /// Restart the running daemon in place and wait until it answers again.
    ///
    /// The daemon keeps its pid, socket, and lock, so clients reconnect to
    /// it rather than start another, and re-executes its executable, which
    /// runs an installed upgrade at once. Turns under way are interrupted;
    /// a TUI session's turn with no time limit continues once the TUI
    /// attaches to its session again. Starts the daemon when none is
    /// running.
    Restart,
    /// Expose the local daemon's `rho-protocol` over HTTP WebSockets (`GET /ws`)
    /// so clients on hosts that cannot use SSH can reach it.
    ///
    /// Binds a loopback or private/overlay network address; every upgrade
    /// requires a bearer token from `rho daemon token rotate`.
    Serve(DaemonServeArgs),
    /// Manage the bearer tokens `rho daemon serve --http` accepts.
    Token {
        #[command(subcommand)]
        command: DaemonTokenCommand,
    },
    /// Set this machine up for rho-ui to reach over the WebSocket bridge, or
    /// undo it.
    Bridge {
        #[command(subcommand)]
        command: DaemonBridgeCommand,
    },
}

#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonBridgeCommand {
    /// Run `rho daemon serve --http` as a service that survives reboots
    /// (systemd on Linux, launchd on macOS) and print a pairing string for
    /// rho-ui, once.
    ///
    /// Listens on this machine's Tailscale or NetBird address, port 8766,
    /// unless --http or --url says otherwise. Running it again rotates the
    /// token and prints a new pairing string; the old one stops working.
    Setup(BridgeSetupArgs),
    /// Pair another device with the bridge setup installed: give it token
    /// NAME and print a pairing string for it, once. Leaves the service as
    /// it is.
    ///
    /// Use one name per device. Pairing a name again rotates its token, so
    /// the device it paired before stops reaching the bridge.
    Pair {
        /// The device's token name, which `rho daemon token list` shows and
        /// `rho daemon token revoke` unpairs.
        #[arg(value_name = "NAME")]
        name: String,
    },
    /// Stop and delete the bridge's service, and revoke its token.
    Remove {
        /// The token to revoke.
        #[arg(long, value_name = "NAME", default_value = BRIDGE_TOKEN)]
        token: String,
    },
}

/// The token `rho daemon bridge` pairs and revokes unless told another.
const BRIDGE_TOKEN: &str = "rho-ui";

#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
pub struct BridgeSetupArgs {
    /// Listen on this address instead of the overlay's: loopback or this
    /// machine's private address, such as 192.168.1.20:8766.
    #[arg(long, value_name = "ADDR")]
    pub http: Option<SocketAddr>,
    /// Pair through a TLS proxy at this wss:// address, which forwards to the
    /// bridge on loopback (127.0.0.1:8766 unless --http names another).
    #[arg(long, value_name = "URL")]
    pub url: Option<String>,
    /// Also accept this `Host` header. May be repeated.
    #[arg(long, value_name = "HOST")]
    pub allow_host: Vec<String>,
    /// The token the pairing string carries, which `rho daemon token list`
    /// shows. Pair more devices with `rho daemon bridge pair`.
    #[arg(long, value_name = "NAME", default_value = BRIDGE_TOKEN)]
    pub token: String,
}

#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
pub struct DaemonServeArgs {
    /// Serve `rho-protocol` over HTTP WebSocket (`GET /ws`) on this address:
    /// a loopback address, such as 127.0.0.1:8766, or this machine's address
    /// on a private network, such as a NetBird or Tailscale peer's
    /// 100.x.y.z:8766, where traffic travels as plain HTTP/WS.
    #[arg(long, value_name = "ADDR")]
    pub http: SocketAddr,
    /// Also accept this `Host` header, such as a tunnel's public hostname,
    /// on any port unless it names one (rho.example.com:443). May be
    /// repeated.
    #[arg(long, value_name = "HOST")]
    pub allow_host: Vec<String>,
    /// Accept WebSocket upgrades from this browser origin, such as
    /// https://app.example.com. Any other `Origin` is refused. Browsers
    /// cannot send the bearer token themselves, so they need a proxy that
    /// adds `Authorization`. May be repeated.
    #[arg(long, value_name = "ORIGIN")]
    pub allow_origin: Vec<String>,
    /// Internal: the listening socket a server that upgraded in place
    /// handed this one.
    #[arg(long, value_name = "FD", hide = true)]
    pub listener_fd: Option<i32>,
    /// Internal: on Windows, the server that upgraded in place to this one,
    /// whose exit this one waits for before it listens.
    #[arg(long, value_name = "PID", hide = true)]
    pub predecessor: Option<u32>,
}

#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum DaemonTokenCommand {
    /// Create token NAME, or give it a new secret, and print the secret once.
    /// The old secret stops working at once: a running bridge closes its
    /// open WebSockets within 5 seconds.
    ///
    /// A daemon HTTP token grants full user-level daemon protocol access
    /// (opening sessions in any workspace, answering approvals, and editing
    /// config). Treat it like SSH access.
    Rotate {
        /// The token's name, which `list` and logs show.
        #[arg(value_name = "NAME")]
        name: String,
    },
    /// List daemon HTTP tokens by name.
    List {
        /// Print one JSON document instead of text rows.
        #[arg(long)]
        json: bool,
    },
    /// Delete token NAME; new connections presenting it are refused at once.
    /// A running bridge closes its open WebSockets within 5 seconds.
    Revoke {
        #[arg(value_name = "NAME")]
        name: String,
    },
}