rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Low-level PTY session: spawn, inject, resize, drain, and cleanup.
//!
//! Shared by the Claude `/usage` probe and the unpublished TUI PTY harness.
//! The harness compiles this file by path rather than depending on this crate,
//! so Cargo (and release-please's cargo-workspace plugin) stay acyclic.

/// Terminal size in character cells.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PtySize {
    pub rows: u16,
    pub cols: u16,
}

impl PtySize {
    pub const fn new(rows: u16, cols: u16) -> Self {
        Self { rows, cols }
    }
}

/// A terminal query the PTY answers by itself, from its reader thread, the
/// moment the child writes it: for children that wait for a reply only
/// briefly, which a reply relayed by a busy caller could miss.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct QueryReply {
    pub query: Vec<u8>,
    pub reply: Vec<u8>,
}

/// Finds [`QueryReply`] queries in output that arrives in chunks, including
/// a query split across two chunks.
#[derive(Debug, Default)]
pub struct QueryWatch {
    replies: Vec<QueryReply>,
    /// The end of the output seen so far, as long as a query split across
    /// chunks could reach back.
    tail: Vec<u8>,
}

impl QueryWatch {
    pub fn new(replies: Vec<QueryReply>) -> Self {
        Self {
            replies,
            tail: Vec::new(),
        }
    }

    /// The replies owed for the queries `chunk` completes, in order.
    pub fn replies_for(&mut self, chunk: &[u8]) -> Vec<u8> {
        let longest = self
            .replies
            .iter()
            .map(|r| r.query.len())
            .max()
            .unwrap_or(0);
        if longest == 0 {
            return Vec::new();
        }
        let seen = self.tail.len();
        let mut window = std::mem::take(&mut self.tail);
        window.extend_from_slice(chunk);
        let mut owed = Vec::new();
        for end in seen + 1..=window.len() {
            for QueryReply { query, reply } in &self.replies {
                if window[..end].ends_with(query) {
                    owed.extend_from_slice(reply);
                }
            }
        }
        let keep = window.len().min(longest - 1);
        self.tail = window.split_off(window.len() - keep);
        owed
    }
}

#[cfg(any(unix, windows))]
mod native {
    use std::{
        io::{Read, Write},
        path::Path,
        sync::{mpsc, Arc, Mutex},
        thread,
        time::{Duration, Instant},
    };

    use anyhow::{Context, Result};
    use portable_pty::{native_pty_system, CommandBuilder, MasterPty, PtySize as PortablePtySize};

    use super::{PtySize, QueryReply, QueryWatch};

    type SharedWriter = Arc<Mutex<Box<dyn Write + Send>>>;

    impl From<PtySize> for PortablePtySize {
        fn from(value: PtySize) -> Self {
            Self {
                rows: value.rows,
                cols: value.cols,
                pixel_width: 0,
                pixel_height: 0,
            }
        }
    }

    /// Spawn and control a child process inside a pseudo-terminal.
    pub struct PtyController {
        child: Box<dyn portable_pty::Child + Send + Sync>,
        /// Shared with the reader thread, which writes [`QueryReply`] replies.
        writer: SharedWriter,
        // ClosePseudoConsole can block while flushing output. Drop the master
        // while the reader and its receiver are still alive to drain that output.
        master: Box<dyn MasterPty + Send>,
        reader_rx: mpsc::Receiver<Vec<u8>>,
        size: PtySize,
        killed: bool,
    }

    impl PtyController {
        /// Spawn `binary` with `args` inside a PTY.
        ///
        /// `env` is the complete child environment after clearing the host process
        /// environment. Callers should pass every variable the child needs.
        /// Callers also emulate terminal replies: ConPTY requests the initial
        /// cursor with DSR 6 and waits for its response before producing output.
        pub fn spawn(
            binary: &Path,
            size: PtySize,
            args: &[impl AsRef<str>],
            env: &[(impl AsRef<str>, impl AsRef<str>)],
            cwd: Option<&Path>,
        ) -> Result<Self> {
            Self::spawn_answering(binary, size, args, env, cwd, Vec::new())
        }

        /// [`Self::spawn`], with `replies` answered from the reader thread.
        pub fn spawn_answering(
            binary: &Path,
            size: PtySize,
            args: &[impl AsRef<str>],
            env: &[(impl AsRef<str>, impl AsRef<str>)],
            cwd: Option<&Path>,
            replies: Vec<QueryReply>,
        ) -> Result<Self> {
            let pty_system = native_pty_system();
            let pair = pty_system
                .openpty(size.into())
                .context("failed to open PTY")?;

            let mut cmd = CommandBuilder::new(binary);
            for arg in args {
                cmd.arg(arg.as_ref());
            }
            if let Some(dir) = cwd {
                cmd.cwd(dir);
            }
            apply_child_env(&mut cmd, env);

            let child = pair
                .slave
                .spawn_command(cmd)
                .with_context(|| format!("failed to spawn {}", binary.display()))?;
            drop(pair.slave);

            let reader = pair
                .master
                .try_clone_reader()
                .context("failed to clone PTY reader")?;
            let writer: SharedWriter = Arc::new(Mutex::new(
                pair.master
                    .take_writer()
                    .context("failed to take PTY writer")?,
            ));
            let reader_rx = spawn_reader(reader, Arc::clone(&writer), QueryWatch::new(replies))?;

            Ok(Self {
                child,
                writer,
                reader_rx,
                master: pair.master,
                size,
                killed: false,
            })
        }

        pub fn size(&self) -> PtySize {
            self.size
        }

        pub fn inject_bytes(&mut self, bytes: &[u8]) -> Result<()> {
            write_input(&self.writer, bytes)
        }

        pub fn resize(&mut self, rows: u16, cols: u16) -> Result<()> {
            self.master
                .resize(PortablePtySize {
                    rows,
                    cols,
                    pixel_width: 0,
                    pixel_height: 0,
                })
                .context("failed to resize PTY")?;
            self.size = PtySize::new(rows, cols);
            Ok(())
        }

        /// Receive one output chunk, waiting up to `timeout`.
        pub fn recv_chunk(&self, timeout: Duration) -> Option<Vec<u8>> {
            self.reader_rx.recv_timeout(timeout).ok()
        }

        /// Drain all currently available output for up to `timeout`.
        pub fn drain(&self, timeout: Duration) -> Vec<u8> {
            let mut out = Vec::new();
            let deadline = Instant::now() + timeout;
            loop {
                let remaining = deadline.saturating_duration_since(Instant::now());
                if remaining.is_zero() {
                    break;
                }
                match self.reader_rx.recv_timeout(remaining) {
                    Ok(chunk) => out.extend(chunk),
                    Err(mpsc::RecvTimeoutError::Timeout | mpsc::RecvTimeoutError::Disconnected) => {
                        break
                    }
                }
            }
            out
        }

        pub fn is_running(&mut self) -> bool {
            matches!(self.child.try_wait(), Ok(None))
        }

        /// Wait for the child to exit and return its exit code.
        pub fn wait_exit(&mut self, timeout: Duration) -> Result<Option<u32>> {
            let deadline = Instant::now() + timeout;
            loop {
                match self.child.try_wait() {
                    Ok(Some(status)) => return Ok(Some(status.exit_code())),
                    Ok(None) if Instant::now() >= deadline => return Ok(None),
                    Ok(None) => thread::sleep(Duration::from_millis(20)),
                    Err(error) => return Err(error).context("failed to wait for PTY child"),
                }
            }
        }

        pub fn kill(&mut self) -> Result<()> {
            if self.killed {
                return Ok(());
            }
            self.killed = true;
            // portable-pty `setsid`s the child, so the pid is the group leader.
            #[cfg(unix)]
            if let Some(pid) = self.child.process_id() {
                if let Ok(pid) = i32::try_from(pid) {
                    // SAFETY: pid is the session leader portable-pty created.
                    unsafe {
                        libc::kill(-pid, libc::SIGKILL);
                    }
                }
            }
            // On Windows portable-pty kills the leader; dropping the master
            // subsequently closes ConPTY. This is not a detached-process tree
            // supervisor, so scenarios must use Rho's supervised tool paths.
            let _ = self.child.kill();
            let _ = self.child.wait();
            Ok(())
        }

        pub fn child_pid(&self) -> Option<u32> {
            self.child.process_id()
        }
    }

    impl Drop for PtyController {
        fn drop(&mut self) {
            let _ = self.kill();
        }
    }

    fn apply_child_env(cmd: &mut CommandBuilder, env: &[(impl AsRef<str>, impl AsRef<str>)]) {
        // Isolated runs get only the launch-plan environment. Clearing first keeps
        // host terminal markers and provider credentials out without maintaining a
        // separate deny list that drifts as new secrets are added.
        cmd.env_clear();
        for (key, value) in env {
            cmd.env(key.as_ref(), value.as_ref());
        }
    }

    fn write_input(writer: &SharedWriter, bytes: &[u8]) -> Result<()> {
        // A poisoned lock still holds a usable writer.
        let mut writer = writer
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        writer
            .write_all(bytes)
            .context("failed to write to PTY stdin")?;
        writer.flush().ok();
        Ok(())
    }

    fn spawn_reader(
        mut reader: Box<dyn Read + Send>,
        writer: SharedWriter,
        mut watch: QueryWatch,
    ) -> Result<mpsc::Receiver<Vec<u8>>> {
        let (tx, rx) = mpsc::channel();
        thread::Builder::new()
            .name("rho-pty-reader".into())
            .spawn(move || {
                let mut buf = [0u8; 8192];
                loop {
                    match reader.read(&mut buf) {
                        Ok(0) => break,
                        Ok(n) => {
                            let owed = watch.replies_for(&buf[..n]);
                            // Best effort: a child that exited reads nothing.
                            if !owed.is_empty() {
                                let _ = write_input(&writer, &owed);
                            }
                            // ConPTY shutdown waits for its output pipe to drain,
                            // even after the consumer disappears. Unix can stop.
                            if tx.send(buf[..n].to_vec()).is_err() && !cfg!(windows) {
                                break;
                            }
                        }
                        Err(_) => break,
                    }
                }
            })
            .context("failed to spawn PTY reader thread")?;
        Ok(rx)
    }
}

#[cfg(any(unix, windows))]
pub use native::PtyController;