rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Private local endpoints of the daemon's own besides the one clients
//! reach: a named pipe only this user can open, for what lasts one run
//! (one turn's served tools, see `acp_runtime::agent_tools::bridge`).

use std::{
    ffi::{OsStr, OsString},
    io,
};

use tokio::net::windows::named_pipe::{NamedPipeClient, NamedPipeServer};

use super::pipe::{self, PipeServer};

/// The listener's end of a connection.
pub(crate) type LocalAccepted = NamedPipeServer;
/// The connecting end.
pub(crate) type LocalConnected = NamedPipeClient;

/// A listening endpoint only this user can reach, gone as it drops.
pub(crate) struct LocalListener {
    pipe: PipeServer,
}

impl LocalListener {
    /// Listens on a new endpoint named after `prefix`, and returns it with
    /// the address [`connect_local`] reaches it at.
    pub(crate) fn bind_private(prefix: &str) -> io::Result<(Self, OsString)> {
        let name = OsString::from(format!(
            r"\\.\pipe\{prefix}-{}",
            uuid::Uuid::new_v4().simple()
        ));
        let pipe = PipeServer::create(&name)?;
        Ok((Self { pipe }, name))
    }

    /// The next connection. Only this user can open the pipe. Cancel safe.
    pub(crate) async fn accept(&mut self) -> io::Result<LocalAccepted> {
        self.pipe.accept().await
    }
}

/// Connects to the endpoint of a [`LocalListener`] at `address`, once it
/// proved to be this user's.
pub(crate) async fn connect_local(address: &OsStr) -> io::Result<LocalConnected> {
    pipe::open(address).await?.ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::NotFound,
            format!("nothing listens on {}", address.display()),
        )
    })
}