rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::*;
use crate::acp_runtime::person::Attendance;
use agent_client_protocol::v1::schema::UsageUpdate;
use pretty_assertions::assert_eq;
use std::ffi::OsString;

fn shape(system_prompt: PromptPolicy, persistence: SessionPersistence) -> ClaudeSessionShape {
    ClaudeSessionShape {
        system_prompt,
        model: Some("opus".into()),
        reasoning: Some(crate::agent::ReasoningLevel::High),
        tools: vec!["Read".into(), "Bash(git *)".into(), "Task".into()],
        max_turns: 40,
        persistence,
    }
}

// Covers: a delegated run carries identity, the turn cap, tools, the settings fence,
// and the extended system prompt in `session/new` `_meta`, under the mode the
// fence picked; Claude's own subagent tools are on only in a run that records
// the subagents they start, and not where loaded settings could define a
// subagent looser than an unattended Plan run. Owner: Claude ACP policy (the
// whole session contract).
#[test]
fn delegated_session_carries_identity_and_fence_in_meta() {
    let options = |nested: Value| {
        let mut options = json!({
            "tools": ["Read", "Bash"],
            "allowedTools": ["Read", "Bash(git *)"],
            "settingSources": ["project"],
            "strictMcpConfig": true,
            "maxTurns": 40,
            "promptSuggestions": false,
            "model": "opus",
            "effort": "high",
        });
        let options_map = options.as_object_mut().unwrap();
        for (key, value) in nested.as_object().unwrap() {
            options_map.insert(key.clone(), value.clone());
        }
        options
    };
    let off = options(json!({"disallowedTools": ["Task", "Agent"]}));
    let on = options(json!({"tools": ["Read", "Bash", "Task", "Agent"]}));
    let cases = [
        (
            PermissionMode::Bypass,
            NestedAgents::Off,
            "bypassPermissions",
            off.clone(),
        ),
        (
            PermissionMode::Bypass,
            NestedAgents::Recorded,
            "bypassPermissions",
            on,
        ),
        (PermissionMode::Plan, NestedAgents::Recorded, "plan", off),
    ];
    let dir = tempfile::tempdir().unwrap();
    for (mode, nested, claude_mode, options) in cases {
        let policy = ClaudeAcpPolicy::delegated(
            shape(
                PromptPolicy::Extend("be brief".into()),
                SessionPersistence::Keep,
            ),
            mode,
            /*inherit_claude_config*/ false,
            Attendance::Unattended,
            dir.path().into(),
            None,
        );
        assert_eq!(
            (
                policy.session_mode(),
                Value::Object(policy.session_meta(nested).unwrap())
            ),
            (
                Some(SessionModeId::new(claude_mode)),
                json!({
                    "systemPrompt": {"append": "be brief"},
                    "claudeCode": {"options": options},
                })
            ),
            "{mode:?} {nested:?}"
        );
    }
}

// Covers: Rho's own one-shot replaces Claude's system prompt, loads no
// settings, rejects every ask, and leaves no resumable session.
// Owner: Claude ACP policy for internal agents.
#[test]
fn headless_one_shot_replaces_the_prompt_and_discards_the_session() {
    let dir = tempfile::tempdir().unwrap();
    let mut shape = shape(
        PromptPolicy::Replace("advise".into()),
        SessionPersistence::Discard,
    );
    shape.tools = Vec::new();
    shape.max_turns = 1;
    let policy = ClaudeAcpPolicy::headless(shape, dir.path().into());
    assert_eq!(
        (
            policy.session_mode(),
            Value::Object(policy.session_meta(NestedAgents::Off).unwrap())
        ),
        (
            Some(SessionModeId::new("default")),
            json!({
                "systemPrompt": "advise",
                "claudeCode": {"options": {
                    "tools": [],
                    "disallowedTools": ["Task", "Agent"],
                    "settingSources": [],
                    "strictMcpConfig": true,
                    "maxTurns": 1,
                    "promptSuggestions": false,
                    "model": "opus",
                    "effort": "high",
                    "persistSession": false,
                }},
            })
        )
    );
}

// Covers: a usage update's `_meta` yields the rate-limit window (with
// utilization from `unifiedWindows` when the top level omits it) and the
// concrete model an alias resolved to; updates without them yield nothing.
// Owner: Claude ACP policy (feeds `/limits` and the run's resolved model).
#[test]
fn usage_meta_yields_rate_limit_and_resolved_model() {
    let dir = tempfile::tempdir().unwrap();
    let policy = ClaudeAcpPolicy::headless(
        shape(
            PromptPolicy::Extend(String::new()),
            SessionPersistence::Discard,
        ),
        dir.path().into(),
    );
    let with_meta = |meta: Value| {
        SessionUpdate::UsageUpdate(UsageUpdate::new(10, 100).meta(meta.as_object().cloned()))
    };
    let event = with_meta(json!({
        "_claude/model": "claude-opus-5-5",
        "_claude/rateLimit": {
            "status": "allowed",
            "resetsAt": 1_793_866_200,
            "rateLimitType": "five_hour",
            "isUsingOverage": false,
            "unifiedWindows": {
                "five_hour": {"utilization": 0.25, "resetsAt": 1_793_866_200},
                "seven_day": {"utilization": 0.18, "resetsAt": 1_794_063_600},
            },
        },
    }));
    assert_eq!(
        policy.status_effects(&event),
        vec![
            StreamEffect::RateLimit(RateLimitInfo {
                status: Some("allowed".into()),
                rate_limit_type: Some("five_hour".into()),
                resets_at: Some(1_793_866_200),
                utilization: Some(0.25),
                overage_status: None,
                overage_resets_at: None,
                is_using_overage: Some(false),
            }),
            StreamEffect::Status(StatusPatch {
                runtime_model: Some("claude-opus-5-5".into()),
                ..StatusPatch::default()
            }),
        ]
    );
    assert_eq!(
        policy.status_effects(&SessionUpdate::UsageUpdate(UsageUpdate::new(10, 100))),
        vec![]
    );
}

// Covers: a model pin reaches the adapter's highest-priority model source, so
// a `model` in the user's Claude settings cannot replace it; an inherited model
// leaves the user's choice alone. Either way, Claude waits on a call to Rho's
// tools as long as the call takes, not its default 60 s.
// Owner: Claude ACP policy (spawn contract).
#[test]
fn model_pin_outranks_claude_settings() {
    let unbounded_mcp: (OsString, OsString) = ("MCP_TOOL_TIMEOUT".into(), "2147483647".into());
    let dir = tempfile::tempdir().unwrap();
    let env_for = |model: Option<&str>| {
        let mut shape = shape(
            PromptPolicy::Extend(String::new()),
            SessionPersistence::Keep,
        );
        shape.model = model.map(str::to_owned);
        ClaudeAcpPolicy::headless(shape, dir.path().into())
            .spawn_plan()
            .unwrap()
            .env
    };
    assert_eq!(
        (env_for(Some("haiku")), env_for(None)),
        (
            vec![
                ("ANTHROPIC_MODEL".into(), "haiku".into()),
                unbounded_mcp.clone()
            ],
            vec![unbounded_mcp]
        )
    );
}

// Covers: an agent's tool call, on its permission ask or its update, names
// the Rho tool it is for from `_meta.claudeCode.toolName`; a built-in tool
// and a call with no tool name name none. Which names Claude's naming reads
// back as Rho's is `each_naming_reads_back_only_rhos_tools`'.
// Owner: Claude ACP policy (served tools).
#[test]
fn asks_name_only_rho_served_tools() {
    let dir = tempfile::tempdir().unwrap();
    let policy = ClaudeAcpPolicy::headless(
        shape(
            PromptPolicy::Extend(String::new()),
            SessionPersistence::Keep,
        ),
        dir.path().into(),
    );
    let named = [
        json!({"claudeCode": {"toolName": "mcp__rho__questionnaire"}}),
        json!({"claudeCode": {"toolName": "Bash"}}),
        json!({}),
    ]
    .map(|meta| {
        let call = AgentToolCallRef {
            meta: meta.as_object(),
            name: None,
            kind: None,
        };
        policy.agent_tool(call).map(str::to_owned)
    });
    assert_eq!(named, [Some("questionnaire".to_owned()), None, None]);
}