rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What the TUI reads off a daemon job wherever it lists one, `/jobs` and
//! the agent rail: its title, whether it waits on a person, the agent a
//! child job runs as, and the run a subagent's job follows.

use rho_protocol::{AgentRuntime as WireRuntime, ChildInfo, ChildRole, JobKind, JobSummary};

use super::{agent_rail::SubagentAttachTarget, session_picker::short_session_id};
use crate::agent::{agent_display_name, session_agent_runtime, AgentRuntime};

/// The run `job` follows when a parent delegated it, to open read-only in
/// place. Only a delegated subagent keeps a run of its own: a workflow
/// node's job names its node, and a started session is followed instead.
pub(super) fn delegated_run(job: &JobSummary) -> Option<SubagentAttachTarget> {
    let child = job.child.as_ref()?;
    match child.role {
        ChildRole::Delegated => Some(SubagentAttachTarget {
            run_id: job.run.clone()?,
            agent_id: child.agent.clone().unwrap_or_else(|| "agent".into()),
        }),
        ChildRole::Started | ChildRole::WorkflowNode | ChildRole::Runtime | ChildRole::Unknown => {
            None
        }
        // `ChildRole` is non-exhaustive.
        _ => None,
    }
}

/// What `job` shows as: its live title, the newest; else a child's, kept
/// once it ends; else its kind and the id a person names it by.
pub(super) fn title(job: &JobSummary) -> String {
    let titled = |title: &Option<String>| title.clone().filter(|title| !title.is_empty());
    job.live
        .as_ref()
        .and_then(|live| titled(&live.title))
        .or_else(|| job.child.as_ref().and_then(|child| titled(&child.title)))
        .unwrap_or_else(|| untitled(job))
}

/// What a job with no title yet is called: its kind and the id a person
/// names it by.
fn untitled(job: &JobSummary) -> String {
    let named = |kind: &str, id: Option<&str>| match id {
        Some(id) => format!("{kind} {}", short_session_id(id)),
        None => format!("{kind} {}", short_session_id(job.id.as_str())),
    };
    match job.kind {
        JobKind::Session => named("session", job.session.as_ref().map(|id| id.as_str())),
        JobKind::Subagent => named("subagent", job.run.as_deref()),
        JobKind::Workflow => named("workflow", job.workflow_run.as_deref()),
        // `JobKind` is non-exhaustive.
        _ => named("job", None),
    }
}

/// Who a job waits on to answer a request of its own.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Waits {
    /// A person: its turn's request, or a question one of its subagents
    /// asked it ([`JobSummary::waits_on_person`]).
    Person,
    /// Only the session that started it, which the daemon gave each of its
    /// open questions (`told_starter`); a person still may answer.
    Parent,
}

/// Who `job` waits on, if it waits on anyone to answer.
pub(super) fn waits_on(job: &JobSummary) -> Option<Waits> {
    if job.waits_on_person() {
        Some(Waits::Person)
    } else if job
        .live
        .as_ref()
        .is_some_and(|live| !live.inputs.is_empty())
    {
        Some(Waits::Parent)
    } else {
        None
    }
}

/// The agent a child runs as, and what runs it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct ChildAgent {
    id: String,
    engine: Option<WireRuntime>,
}

impl ChildAgent {
    /// The agent of `child`, when the daemon knows it.
    pub(super) fn of(child: &ChildInfo) -> Option<Self> {
        Some(Self {
            id: child.agent.clone()?,
            engine: child.engine,
        })
    }

    /// How a row names the agent: as [`agent_display_name`] shows it, so
    /// not at all for rho's default agent, as a session a client started is
    /// not named, and by its runtime's name for a runtime's own agent; any
    /// other agent with the runtime that runs it after it, unless that is
    /// rho's own loop.
    pub(super) fn label(&self) -> Option<String> {
        let runtime = match self.engine.map(AgentRuntime::try_from) {
            None | Some(Ok(AgentRuntime::Rho)) => None,
            Some(Ok(runtime @ (AgentRuntime::ClaudeCli | AgentRuntime::Antigravity))) => {
                Some(runtime.display_name())
            }
            Some(Err(_)) => Some("another runtime"),
        };
        match runtime {
            Some(runtime) if session_agent_runtime(&self.id).is_none() => {
                Some(format!("{} ({runtime})", self.id))
            }
            Some(_) | None => agent_display_name(&self.id).map(str::to_owned),
        }
    }
}

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