rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The agent's tool calls of its own in progress: calls of tools other than
//! the run's, whose count the tools hear
//! ([`super::AgentTools::agent_calls`]). The agent announces each call
//! (`tool_call`) and ends it, completed or failed (`tool_call_update`); a
//! call it never ends counts until the run ends, so its owner errs toward
//! the agent being busy.
//!
//! A call the agent ends while work it started goes on is not counted past
//! its end: a Claude Code Bash or Task call run in the background
//! (`run_in_background`) may report itself completed while its work, a
//! Task subagent's model requests included, goes on. Not verified against
//! the agent; the budget accepts it as an exception (see the daemon's
//! `sessions::budget`).

use std::{
    collections::HashSet,
    sync::{Mutex, PoisonError},
};

use agent_client_protocol::v1::schema::{SessionUpdate, ToolCallId, ToolCallStatus};

/// The agent's own calls in progress, by the agent's id.
#[derive(Default)]
pub(super) struct OwnCalls {
    running: Mutex<HashSet<ToolCallId>>,
}

impl OwnCalls {
    /// The agent's `update`, `served` being the run's tool it announces a
    /// call of, if any: how many calls of its own are in progress, if that
    /// changed.
    pub(super) fn hear(&self, update: &SessionUpdate, served: Option<&str>) -> Option<usize> {
        // Each change is one insert or remove, so a panic mid-change leaves
        // the set whole.
        let mut running = self.running.lock().unwrap_or_else(PoisonError::into_inner);
        let changed = match update {
            SessionUpdate::ToolCall(call) if served.is_none() && !ends(Some(&call.status)) => {
                running.insert(call.tool_call_id.clone())
            }
            // A call announced as it ended, or a served one, which the
            // tools count themselves.
            SessionUpdate::ToolCall(call) => running.remove(&call.tool_call_id),
            SessionUpdate::ToolCallUpdate(update) if ends(update.fields.status.as_ref()) => {
                running.remove(&update.tool_call_id)
            }
            _ => false,
        };
        changed.then_some(running.len())
    }
}

/// Whether `status` ends a call.
fn ends(status: Option<&ToolCallStatus>) -> bool {
    matches!(
        status,
        Some(ToolCallStatus::Completed | ToolCallStatus::Failed)
    )
}

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