rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What rho learned of each ACP adapter it ran, from its `initialize`, for
//! the life of the process: a daemon's, whose sessions start an adapter
//! process every turn. Keyed by the adapter's executable path; the version
//! the adapter reports tells an upgrade in place, such as through a mise
//! shim whose path stays the same.
//!
//! Two things use it:
//! - A run of an adapter that answered v1 to v2 starts on v1, rather than
//!   paying a second process every turn ([`Adapter::start_on`]). When the
//!   adapter then reports another version, the next run tries v2 again.
//! - A daemon session tells its clients whether its adapter takes images,
//!   and steers, before a turn finds out ([`Adapters::known`]).

use std::{
    collections::HashMap,
    path::{Path, PathBuf},
    sync::{LazyLock, Mutex},
};

use super::policy::AcpProtocol;

/// The process's adapters.
pub(crate) static ADAPTERS: LazyLock<Adapters> = LazyLock::new(Adapters::default);

#[derive(Default)]
pub(crate) struct Adapters(Mutex<HashMap<PathBuf, Learned>>);

/// What one adapter said.
#[derive(Clone, Debug, Default)]
struct Learned {
    /// It answered v1 to v2, reporting this version (`agentInfo.version`).
    v1_only_at: Option<Option<String>>,
    /// What its last `initialize` said.
    said: Option<AdapterFacts>,
}

/// What an adapter's last `initialize` said.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct AdapterFacts {
    /// The protocol it answered on.
    pub(crate) protocol: AcpProtocol,
    /// It takes image content blocks in prompts.
    pub(crate) images: bool,
}

impl Adapters {
    /// What the adapter at `path` said, once it answered an `initialize`.
    pub(crate) fn known(&self, path: &Path) -> Option<AdapterFacts> {
        self.lock().get(path)?.said
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, HashMap<PathBuf, Learned>> {
        self.0.lock().expect("ACP adapters")
    }
}

/// One adapter in a cache: what a run of it asks, and tells, of what it
/// said.
#[derive(Clone, Copy)]
pub(super) struct Adapter<'a> {
    pub(super) cache: &'a Adapters,
    pub(super) path: &'a Path,
}

impl Adapter<'_> {
    /// The protocol a run starts on when its policy asks `asked`: v1 for an
    /// adapter known to answer v1 to v2.
    pub(super) fn start_on(self, asked: AcpProtocol) -> AcpProtocol {
        let v1_only = self
            .cache
            .lock()
            .get(self.path)
            .is_some_and(|learned| learned.v1_only_at.is_some());
        match asked {
            AcpProtocol::V2 if v1_only => AcpProtocol::V1,
            asked => asked,
        }
    }

    /// The adapter answered an `initialize` that requested `requested` on
    /// `protocol`, reporting `version`, taking images or not. An answer of
    /// v1 to v2 makes it v1-only at `version`; a v1-only adapter that
    /// answers v2, or reports another version, no longer is.
    pub(super) fn initialized(
        self,
        requested: AcpProtocol,
        protocol: AcpProtocol,
        version: Option<String>,
        images: bool,
    ) {
        let mut adapters = self.cache.lock();
        let learned = adapters.entry(self.path.to_owned()).or_default();
        learned.v1_only_at = match (requested, protocol, learned.v1_only_at.take()) {
            (AcpProtocol::V2, AcpProtocol::V1, _) => Some(version),
            (_, AcpProtocol::V1, Some(at)) if at == version => Some(at),
            _ => None,
        };
        learned.said = Some(AdapterFacts { protocol, images });
    }

    /// A v1 `initialize` of the adapter failed: one known to answer v1 to
    /// v2 may speak only v2 now, as after an upgrade in place, so the next
    /// run asks for v2 again.
    pub(super) fn v1_failed(self) {
        if let Some(learned) = self.cache.lock().get_mut(self.path) {
            learned.v1_only_at = None;
        }
    }
}

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