rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What a session's workspace has checked out, asked of `git` when a
//! client lists or shows the session: the branch whose pull request the
//! session shows. Only a branch whose `origin` is on github.com is looked
//! up: a lookup runs `gh` against the origin's host, and `gh` would send
//! that host the user's sign-in for it.

use std::{
    path::{Path, PathBuf},
    process::{Output, Stdio},
};

use futures_util::{stream, StreamExt as _};
use tokio::process::Command;

use super::model::{BranchId, RepoId, GITHUB};
use crate::git::output_within;

/// The most workspaces [`resolve_each`] asks `git` about at once.
///
/// Receipt: each workspace takes two `git` commands of 3 to 4 ms
/// (2026-11-23), so a sidebar of 30 sessions resolves in about 30 ms at 8 at
/// a time, against 240 ms one at a time, without starting 60 processes at
/// once. Chosen; a larger list only takes longer.
const RESOLVES_AT_ONCE: usize = 8;

/// What a workspace has checked out.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Checkout {
    /// Not in a git repository, or `git` did not answer.
    NotGit,
    /// No branch: a commit, abbreviated as `git` abbreviates it.
    Detached { commit: String },
    /// A branch of a repository with no `origin`, or one whose `origin` is
    /// not on github.com.
    NoForge { branch: String },
    /// A branch of the github.com repository `origin` names, checked out in
    /// `worktree`.
    Branch {
        branch: BranchId,
        worktree: Worktree,
    },
}

/// Whose worktree a session's workspace is: whether the branch it has
/// checked out, and so that branch's pull request, is the session's own.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Worktree {
    /// One the daemon added for the session alone (#1673) at `added_at`
    /// (unix seconds), whose branch only the session's own work moves.
    Own { added_at: u64 },
    /// Any other, such as a clone other sessions or a person also work in
    /// and switch branches in.
    Shared,
}

impl Checkout {
    /// The branch whose pull request is looked up; `None` when the checkout
    /// is on no branch of a github.com repository.
    pub(super) fn branch(&self) -> Option<&BranchId> {
        match self {
            Self::Branch { branch, .. } => Some(branch),
            Self::NotGit | Self::Detached { .. } | Self::NoForge { .. } => None,
        }
    }
}

/// What `workspace`, which is `worktree`, has checked out: `git
/// symbolic-ref --short -q HEAD` names the branch (exit 1 means none, and
/// `rev-parse --short HEAD` names the commit; anything else, no
/// repository), and `origin` its repository, when that is on github.com.
pub(crate) async fn resolve(workspace: &Path, worktree: Worktree) -> Checkout {
    let Some(head) = git(workspace, &["symbolic-ref", "--short", "-q", "HEAD"]).await else {
        return Checkout::NotGit;
    };
    match head.status.code() {
        Some(0) => {}
        Some(1) => {
            let commit = git(workspace, &["rev-parse", "--short", "HEAD"]).await;
            return match commit {
                Some(commit) if commit.status.success() => Checkout::Detached {
                    commit: stdout(&commit),
                },
                Some(_) | None => Checkout::NotGit,
            };
        }
        Some(_) | None => return Checkout::NotGit,
    }
    let branch = stdout(&head);
    match origin(workspace).await {
        Some(repo) if repo.host == GITHUB => Checkout::Branch {
            branch: BranchId { repo, branch },
            worktree,
        },
        Some(_) | None => Checkout::NoForge { branch },
    }
}

/// What each workspace, which is its worktree, has checked out, in order,
/// [`RESOLVES_AT_ONCE`] at a time.
pub(crate) async fn resolve_each(workspaces: Vec<(PathBuf, Worktree)>) -> Vec<Checkout> {
    stream::iter(workspaces)
        .map(|(workspace, worktree)| async move { resolve(&workspace, worktree).await })
        .buffered(RESOLVES_AT_ONCE)
        .collect()
        .await
}

/// The repository `workspace`'s `origin` remote names on any forge, if
/// git answers in time.
pub(super) async fn origin(workspace: &Path) -> Option<RepoId> {
    let output = git(workspace, &["remote", "get-url", "origin"]).await?;
    if !output.status.success() {
        return None;
    }
    RepoId::from_url(std::str::from_utf8(&output.stdout).ok()?)
}

/// `git -C workspace args`, run with the daemon's own environment within
/// [`crate::git::GIT_DEADLINE`] ([`output_within`]); `None` when it could
/// not start or did not finish in time. Past the deadline a warning names
/// the command and the deadline, and the workspace reads as no repository,
/// or as one with no origin.
async fn git(workspace: &Path, args: &[&str]) -> Option<Output> {
    let mut command = Command::new("git");
    for name in crate::git::LOCATION_ENV {
        command.env_remove(name);
    }
    command
        .arg("-C")
        .arg(workspace)
        .args(args)
        .stdin(Stdio::null())
        .kill_on_drop(true);
    output_within(&mut command, workspace).await.ok()
}

/// `output`'s stdout, trimmed.
fn stdout(output: &Output) -> String {
    String::from_utf8_lossy(&output.stdout).trim().to_owned()
}

#[cfg(test)]
#[path = "checkout_tests.rs"]
mod tests;