rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What the agent of another runtime that runs a session is doing with the
//! tools rho serves it, which two readers act on: the agent budget, which
//! lends a turn's slot only while the agent waits on those tools alone (see
//! `budget`), and the background delivery, which sends nothing into a turn
//! while they have the agent's attention (see `acp_background`). It counts,
//! as the served tools and the budget tell it (see `acp_tools` and
//! `budget`), and holds the two predicates its readers act on:
//! [`Activity::waits_on_served_tools`] for the budget, and
//! [`Activity::served_tools_busy`] for the delivery.

use std::sync::Arc;

use tokio::sync::watch;

/// The session's agent activity, as it changes. One per session, whatever
/// turn its calls came from: a call can outlive its turn.
#[derive(Clone)]
pub(super) struct AgentActivity(Arc<watch::Sender<Activity>>);

/// One look at the agent's activity.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct Activity {
    /// The served calls running, a call the bridge parked included
    /// ([`AgentActivity::call_running`]).
    calls: usize,
    /// The agent's requests to the served tools whose answers are not yet
    /// ready: the agent is blocked on each
    /// ([`AgentActivity::request_outstanding`]).
    requests: usize,
    /// The agent's requests to the served tools whose answers have yet to
    /// go back: from the request until its answer is readied, or given up
    /// on, a wait for the turn's slot included, which `requests` no longer
    /// counts ([`AgentActivity::answer_pending`]).
    answers: usize,
    /// The agent's own tool calls in progress, outside the served tools, as
    /// its run last told ([`AgentActivity::set_agent_calls`]).
    agent_calls: usize,
}

impl Activity {
    /// Whether the agent waits on the served tools alone: blocked on a
    /// request to them, so it makes no model request until it has the
    /// answer, with no tool call of its own in progress that could, as a
    /// Claude Code Task subagent makes model requests while its agent
    /// waits.
    pub(super) fn waits_on_served_tools(self) -> bool {
        self.requests > 0 && self.agent_calls == 0
    }

    /// Whether the served tools have the agent's attention: a call runs, a
    /// parked one included, or a request's answer has yet to go back, as
    /// while it waits for the turn's slot.
    pub(super) fn served_tools_busy(self) -> bool {
        self.calls > 0 || self.answers > 0
    }
}

impl Default for AgentActivity {
    fn default() -> Self {
        Self(Arc::new(watch::Sender::new(Activity::default())))
    }
}

impl AgentActivity {
    /// Counts a served call running until the guard it returns drops.
    pub(super) fn call_running(&self) -> CallRunning {
        self.0.send_modify(|activity| activity.calls += 1);
        CallRunning(self.clone())
    }

    /// Counts one of the agent's requests to the served tools outstanding
    /// until the guard it returns drops.
    pub(super) fn request_outstanding(&self) -> RequestOutstanding {
        self.0.send_modify(|activity| activity.requests += 1);
        RequestOutstanding(self.clone())
    }

    /// Counts one of the agent's requests to the served tools whose answer
    /// has yet to go back until the guard it returns drops.
    pub(super) fn answer_pending(&self) -> AnswerPending {
        self.0.send_modify(|activity| activity.answers += 1);
        AnswerPending(self.clone())
    }

    /// The agent's own tool calls in progress, outside the served tools,
    /// now number `running`.
    pub(super) fn set_agent_calls(&self, running: usize) {
        self.0.send_if_modified(|activity| {
            let changed = activity.agent_calls != running;
            activity.agent_calls = running;
            changed
        });
    }

    /// The activity now.
    pub(super) fn now(&self) -> Activity {
        *self.0.borrow()
    }

    /// Follows the activity as it changes. The receiver never closes while
    /// `self` lives.
    pub(super) fn watch(&self) -> watch::Receiver<Activity> {
        self.0.subscribe()
    }

    /// Resolves once `done` holds of the activity: at once when it does.
    pub(super) async fn until(&self, done: impl FnMut(&Activity) -> bool) {
        let mut activity = self.0.subscribe();
        // `self` holds the sender, so the wait ends only once `done` holds.
        let _ = activity.wait_for(done).await;
    }
}

/// A served call [`AgentActivity`] counts running, until it drops.
pub(super) struct CallRunning(AgentActivity);

impl Drop for CallRunning {
    fn drop(&mut self) {
        let Self(AgentActivity(activity)) = self;
        activity.send_modify(|activity| activity.calls -= 1);
    }
}

/// A request of the agent's [`AgentActivity`] counts outstanding, until it
/// drops.
pub(super) struct RequestOutstanding(AgentActivity);

impl Drop for RequestOutstanding {
    fn drop(&mut self) {
        let Self(AgentActivity(activity)) = self;
        activity.send_modify(|activity| activity.requests -= 1);
    }
}

/// A request of the agent's whose answer [`AgentActivity`] counts as yet to
/// go back, until it drops.
pub(super) struct AnswerPending(AgentActivity);

impl Drop for AnswerPending {
    fn drop(&mut self) {
        let Self(AgentActivity(activity)) = self;
        activity.send_modify(|activity| activity.answers -= 1);
    }
}