rho-coding-agent 3.5.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Chooses T3's `delegate_task` target for a child launched with a per-call model.

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

/// The parts of T3's `orchestrator_capabilities` reply that decide a target.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Capabilities {
    inherited_provider_instance_id: String,
    /// Required, not `#[serde(default)]`: a reply missing this field must fail
    /// to parse rather than read as "no options", which would send an empty
    /// `options` and make T3 drop the parent's mode and thought level.
    inherited_options: Vec<SelectedOption>,
    providers: Vec<Provider>,
}

/// One setting T3 applies to a thread, such as its permission mode.
#[derive(Deserialize, Serialize)]
struct SelectedOption {
    id: String,
    value: Value,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Provider {
    provider_instance_id: String,
    #[serde(default)]
    models: Vec<Model>,
}

#[derive(Deserialize)]
struct Model {
    id: String,
    #[serde(default)]
    options: Vec<ModelOption>,
}

#[derive(Deserialize)]
struct ModelOption {
    id: String,
    #[serde(default)]
    values: Vec<Value>,
}

/// T3's launch selection for a child whose `/agent:` rebind will select
/// `model`, so the child's thread shows the model it runs on.
///
/// `None` leaves T3 on the parent's selection. That is always safe, because the
/// rebind selects the model either way. A target is returned only when it
/// changes nothing else about the launch:
/// - T3 fails a launch whose model the parent's provider instance does not
///   offer (`model_unavailable`), so the model must be offered there.
/// - T3 keeps the parent's options, such as its permission mode and thought
///   level, only for an unchanged model. The target repeats them, and the
///   model must accept every one.
pub(super) fn launch_target(capabilities: &Value, model: &str) -> Option<Value> {
    let capabilities = match Capabilities::deserialize(capabilities) {
        Ok(capabilities) => capabilities,
        Err(error) => {
            // A shape we cannot read means T3's contract changed, not a decline.
            tracing::debug!(%error, "could not read T3 orchestrator_capabilities; labelling the child's thread with the parent's model");
            return None;
        }
    };
    let offered = capabilities
        .providers
        .iter()
        .find(|provider| {
            provider.provider_instance_id == capabilities.inherited_provider_instance_id
        })?
        .models
        .iter()
        .find(|entry| entry.id == model)?;
    let accepts = |inherited: &SelectedOption| {
        offered
            .options
            .iter()
            .any(|option| option.id == inherited.id && option.values.contains(&inherited.value))
    };
    capabilities
        .inherited_options
        .iter()
        .all(accepts)
        .then(|| json!({"model": model, "options": capabilities.inherited_options}))
}

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