rho-coding-agent 2.3.1

A fast Rust agent harness with a small footprint and opinionated defaults
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::sync::Arc;

use pretty_assertions::assert_eq;
use rho_sdk::SystemPrompt;

use crate::{
    agent::{
        AgentDefinition, AgentId, AgentRuntimeSpec, ModelPolicy, PromptPolicy, ToolCapability,
        ToolPolicy, ADVISOR_AGENT_ID,
    },
    app::agent_binding::{AgentBinder, AgentInvocation, AgentRole},
    config::Config,
    diagnostics::RuntimeDiagnostics,
    tools::agent::BackgroundSubagents,
};

use super::{assemble_tools_and_prompt, ToolsAndPromptOptions};

fn advisor_config(advisor_mode: bool, with_model: bool) -> Config {
    let mut config = Config {
        advisor_mode,
        ..Config::default()
    };
    if with_model {
        config.set_internal_agent_model(
            ADVISOR_AGENT_ID,
            "anthropic".into(),
            "claude-test".into(),
            "api-key".into(),
        );
    }
    config
}

fn bound_agent(config: &Config) -> crate::app::agent_binding::BoundAgent {
    AgentBinder::bind(
        Arc::new(AgentDefinition {
            id: AgentId::new("test").unwrap(),
            description: "test".into(),
            prompt: PromptPolicy::Extend(String::new()),
            runtime: AgentRuntimeSpec::Rho {
                tools: ToolPolicy::Allow(
                    [ToolCapability::Advisor, ToolCapability::ReadFile]
                        .into_iter()
                        .collect(),
                ),
                model: ModelPolicy::Inherit,
                reasoning: None,
            },
        }),
        AgentInvocation {
            role: AgentRole::InteractiveRoot,
            available_tools: crate::agent::AgentCapabilities::all_host_tools(),
        },
        config,
    )
    .unwrap()
}

async fn assemble(config: &Config, cwd: &std::path::Path) -> (bool, String) {
    assemble_awaiting_catalog(config, cwd, /*await_catalog_names*/ false).await
}

async fn assemble_awaiting_catalog(
    config: &Config,
    cwd: &std::path::Path,
    await_catalog_names: bool,
) -> (bool, String) {
    let diagnostics = RuntimeDiagnostics::new(config);
    let agent = bound_agent(config);
    let assembled = assemble_tools_and_prompt(ToolsAndPromptOptions {
        catalog: None,
        config,
        config_path: cwd.join("config.toml"),
        cwd,
        no_system_prompt: false,
        no_tools: false,
        no_subagents: true,
        questionnaire_enabled: false,
        mcp_elicitation: crate::tools::mcp::McpElicitationSupport::Unavailable,
        mcp_sampling: super::McpSamplingSupport::Unavailable,
        mcp_attach: super::McpAttach::Connect,
        await_catalog_names,
        defer_mcp_connect: false,
        background_subagents: BackgroundSubagents::Disabled,
        diagnostics: &diagnostics,
        agent: &agent,
    })
    .await
    .unwrap();
    let tools = assembled.tools;
    let prompt = assembled.system_prompt;
    let registered = tools.advisor_registered();
    let text = match prompt {
        SystemPrompt::Custom(text) => text,
        SystemPrompt::None => String::new(),
        _ => String::new(),
    };
    (registered, text)
}

// Covers: the advisor tool must appear only when advisor mode is on and an
// advisor model is configured. Steering stays off the system prompt.
// Owner: root tool/prompt assembly.
#[tokio::test]
async fn the_advisor_tool_needs_both_the_mode_and_a_model() {
    let cwd = tempfile::tempdir().unwrap();
    let cases = [
        (false, false, false),
        (true, false, false),
        (false, true, false),
        (true, true, true),
    ];

    for (advisor_mode, with_model, expected) in cases {
        let config = advisor_config(advisor_mode, with_model);

        let (registered, prompt) = assemble(&config, cwd.path()).await;

        assert_eq!(
            registered, expected,
            "advisor_mode={advisor_mode} with_model={with_model}"
        );
        assert!(
            !prompt.contains("Do not call advisor as your first action"),
            "system prompt must stay advisor-agnostic; advisor_mode={advisor_mode} with_model={with_model}"
        );
    }
}

// Covers: the advisor must review the prompt the executor actually runs with.
// Owner: root tool/prompt assembly.
#[tokio::test]
async fn the_advisor_receives_the_executor_system_prompt() {
    let cwd = tempfile::tempdir().unwrap();
    let config = advisor_config(true, true);
    let diagnostics = RuntimeDiagnostics::new(&config);
    let agent = bound_agent(&config);

    let assembled = assemble_tools_and_prompt(ToolsAndPromptOptions {
        catalog: None,
        config: &config,
        config_path: cwd.path().join("config.toml"),
        cwd: cwd.path(),
        no_system_prompt: false,
        no_tools: false,
        no_subagents: true,
        questionnaire_enabled: false,
        mcp_elicitation: crate::tools::mcp::McpElicitationSupport::Unavailable,
        mcp_sampling: super::McpSamplingSupport::Unavailable,
        mcp_attach: super::McpAttach::Connect,
        await_catalog_names: false,
        defer_mcp_connect: false,
        background_subagents: BackgroundSubagents::Disabled,
        diagnostics: &diagnostics,
        agent: &agent,
    })
    .await
    .unwrap();
    let tools = assembled.tools;
    let prompt = assembled.system_prompt;

    let SystemPrompt::Custom(text) = prompt else {
        panic!("expected a custom system prompt");
    };
    let store = tools.advisor().expect("advisor store");
    assert_eq!(store.system_prompt(), Some(text));
}

// Covers: the executor system prompt is a single form that does not encode
// advisor registration. Mid-session toggles must not rely on swapping prompts.
// Owner: root tool/prompt assembly.
#[tokio::test]
async fn system_prompt_stays_advisor_agnostic() {
    let cwd = tempfile::tempdir().unwrap();

    for advisor_mode in [false, true] {
        let config = advisor_config(advisor_mode, /*with_model*/ true);
        let diagnostics = RuntimeDiagnostics::new(&config);
        let agent = bound_agent(&config);

        let prompt = assemble_tools_and_prompt(ToolsAndPromptOptions {
            catalog: None,
            config: &config,
            config_path: cwd.path().join("config.toml"),
            cwd: cwd.path(),
            no_system_prompt: false,
            no_tools: false,
            no_subagents: true,
            questionnaire_enabled: false,
            mcp_elicitation: crate::tools::mcp::McpElicitationSupport::Unavailable,
            mcp_sampling: super::McpSamplingSupport::Unavailable,
            mcp_attach: super::McpAttach::Connect,
            await_catalog_names: false,
            defer_mcp_connect: false,
            background_subagents: BackgroundSubagents::Disabled,
            diagnostics: &diagnostics,
            agent: &agent,
        })
        .await
        .unwrap()
        .system_prompt;

        let text = match prompt {
            SystemPrompt::Custom(text) => text,
            SystemPrompt::None => String::new(),
            _ => String::new(),
        };
        assert!(
            !text.contains("Do not call advisor as your first action"),
            "advisor_mode={advisor_mode}"
        );
        assert!(
            !text.contains("You have access to an `advisor` tool"),
            "advisor_mode={advisor_mode}"
        );
    }
}

// Covers: the assembled system prompt names the model this run actually bound,
// so an agent that pins its own model is told that model, not the host's.
// Owner: root tool/prompt assembly.
#[tokio::test]
async fn the_assembled_prompt_names_the_bound_model() {
    let cwd = tempfile::tempdir().unwrap();
    let config = Config {
        provider: "openai".into(),
        model: "gpt-5.6-sol".into(),
        ..Config::default()
    };

    let (_, prompt) = assemble(&config, cwd.path()).await;

    // The seam, not the wording: the bound model reaches the assembled prompt.
    assert!(prompt.contains("openai/gpt-5.6-sol"), "{prompt}");
}

// Covers: `await_catalog_names` must decide whether assembly blocks on a stuck
// models.dev hydrate. Interactive passes false to keep the first frame free;
// this pins the flag itself, so making it a no-op fails here.
// Owner: root tool/prompt assembly
#[tokio::test(flavor = "current_thread")]
async fn await_catalog_names_decides_whether_assembly_waits_for_a_hydrate() {
    let catalog = tempfile::tempdir().unwrap();
    let cwd = tempfile::tempdir().unwrap();
    let _cache =
        rho_providers::model::models_dev::ModelsDevCacheDirGuard::new(catalog.path().to_path_buf());
    // Held for the whole test, so the hydrate every case would await never lands.
    let _lock = rho_providers::model::models_dev::catalog_hydrate_lock_for_tests()
        .lock()
        .await;
    let config = Config::default();

    for (await_catalog_names, finishes) in [(false, true), (true, false)] {
        let assembled = tokio::time::timeout(
            std::time::Duration::from_millis(500),
            assemble_awaiting_catalog(&config, cwd.path(), await_catalog_names),
        )
        .await;

        assert_eq!(
            assembled.is_ok(),
            finishes,
            "await_catalog_names = {await_catalog_names}"
        );
    }
}

// Covers: interactive MCP connect must return a pending inventory instead of
// waiting on a slow stdio handshake.
// Owner: root tool/prompt assembly
#[tokio::test]
async fn deferred_mcp_connect_returns_pending_inventory_without_waiting() {
    use std::collections::BTreeMap;

    use crate::tools::mcp::{
        config::{McpConfig, McpSamplingPolicy, McpServerConfig, McpToolFilter, McpTransport},
        McpServerStatus,
    };

    let cwd = tempfile::tempdir().unwrap();
    let config = Config {
        mcp: McpConfig {
            servers: BTreeMap::from([(
                "slow".into(),
                McpServerConfig {
                    enabled: true,
                    tools: McpToolFilter::default(),
                    log_level: None,
                    sampling: McpSamplingPolicy::Deny,
                    transport: McpTransport::Stdio {
                        command: "sleep".into(),
                        args: vec!["120".into()],
                        cwd: None,
                        env: BTreeMap::new(),
                        env_from_env: BTreeMap::new(),
                    },
                    filesystem: None,
                },
            )]),
            invalid_servers: Vec::new(),
        },
        ..Config::default()
    };
    let diagnostics = RuntimeDiagnostics::new(&config);
    let agent = bound_agent(&config);
    let assembled = tokio::time::timeout(
        std::time::Duration::from_millis(500),
        assemble_tools_and_prompt(ToolsAndPromptOptions {
            catalog: None,
            config: &config,
            config_path: cwd.path().join("config.toml"),
            cwd: cwd.path(),
            no_system_prompt: false,
            no_tools: false,
            no_subagents: true,
            questionnaire_enabled: false,
            mcp_elicitation: crate::tools::mcp::McpElicitationSupport::Unavailable,
            mcp_sampling: super::McpSamplingSupport::Unavailable,
            mcp_attach: super::McpAttach::Connect,
            await_catalog_names: false,
            defer_mcp_connect: true,
            background_subagents: BackgroundSubagents::Disabled,
            diagnostics: &diagnostics,
            agent: &agent,
        }),
    )
    .await
    .expect("deferred MCP connect awaited the handshake")
    .unwrap();
    assert_eq!(
        assembled
            .inventory
            .mcp
            .find("slow")
            .map(|server| server.status()),
        Some(McpServerStatus::Connecting)
    );
    let handle = assembled
        .pending_mcp
        .expect("deferred connect should leave a join handle");
    handle.abort();
}

fn stdio_server(command: &str, args: Vec<String>) -> crate::tools::mcp::config::McpServerConfig {
    use crate::tools::mcp::config::{
        McpSamplingPolicy, McpServerConfig, McpToolFilter, McpTransport,
    };
    use std::collections::BTreeMap;

    McpServerConfig {
        enabled: true,
        tools: McpToolFilter::default(),
        log_level: None,
        sampling: McpSamplingPolicy::Deny,
        transport: McpTransport::Stdio {
            command: command.into(),
            args,
            cwd: None,
            env: BTreeMap::new(),
            env_from_env: BTreeMap::new(),
        },
        filesystem: None,
    }
}

// Covers: McpAttach::None must drop user and plugin MCP so an aside cannot
// inherit Agent Plugin servers through assemble_tools_and_prompt.
// Owner: root tool/prompt assembly
#[test]
fn mcp_attach_none_drops_user_and_plugin_servers() {
    use std::collections::BTreeMap;

    use crate::tools::mcp::config::McpConfig;

    use super::{mcp_config_for_attach, McpAttach};

    let config = Config {
        mcp: McpConfig {
            servers: BTreeMap::from([("user".into(), stdio_server("sleep", vec!["120".into()]))]),
            invalid_servers: Vec::new(),
        },
        ..Config::default()
    };
    let plugin = McpConfig {
        servers: BTreeMap::from([("plugin".into(), stdio_server("sleep", vec!["120".into()]))]),
        invalid_servers: Vec::new(),
    };

    let connected = mcp_config_for_attach(&config, plugin.clone(), McpAttach::Connect);
    pretty_assertions::assert_eq!(
        connected.servers.keys().cloned().collect::<Vec<_>>(),
        vec!["plugin".to_string(), "user".to_string()]
    );

    let none = mcp_config_for_attach(&config, plugin, McpAttach::None);
    pretty_assertions::assert_eq!(none.servers.is_empty(), true);
    pretty_assertions::assert_eq!(none.invalid_servers.is_empty(), true);
}

// Covers: McpAttach::None must not start transports even when config lists
// enabled servers.
// Owner: root tool/prompt assembly
#[tokio::test]
async fn mcp_attach_none_does_not_connect_configured_servers() {
    use std::collections::BTreeMap;

    use crate::tools::mcp::{config::McpConfig, McpLoadMode};

    use super::McpAttach;

    let cwd = tempfile::tempdir().unwrap();
    let config = Config {
        mcp: McpConfig {
            servers: BTreeMap::from([("slow".into(), stdio_server("sleep", vec!["120".into()]))]),
            invalid_servers: Vec::new(),
        },
        ..Config::default()
    };
    let diagnostics = RuntimeDiagnostics::new(&config);
    let agent = bound_agent(&config);
    let assembled = tokio::time::timeout(
        std::time::Duration::from_millis(500),
        assemble_tools_and_prompt(ToolsAndPromptOptions {
            catalog: None,
            config: &config,
            config_path: cwd.path().join("config.toml"),
            cwd: cwd.path(),
            no_system_prompt: false,
            no_tools: false,
            no_subagents: true,
            questionnaire_enabled: false,
            mcp_elicitation: crate::tools::mcp::McpElicitationSupport::Unavailable,
            mcp_sampling: super::McpSamplingSupport::Unavailable,
            mcp_attach: McpAttach::None,
            await_catalog_names: false,
            defer_mcp_connect: false,
            background_subagents: BackgroundSubagents::Disabled,
            diagnostics: &diagnostics,
            agent: &agent,
        }),
    )
    .await
    .expect("McpAttach::None awaited an MCP handshake")
    .unwrap();

    pretty_assertions::assert_eq!(assembled.inventory.mcp.mode, McpLoadMode::ToolsDisabled);
    pretty_assertions::assert_eq!(assembled.inventory.mcp.servers.is_empty(), true);
    pretty_assertions::assert_eq!(assembled.pending_mcp.is_none(), true);
    pretty_assertions::assert_eq!(assembled.inventory.mcp.find("slow").is_none(), true);
}