rho-coding-agent 1.40.1

A lightweight agent harness inspired by Pi
Documentation
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
use std::collections::BTreeMap;

use pretty_assertions::assert_eq;
use rho_providers::model::catalog::ModelSelection;
use rho_providers::reasoning::ReasoningLevel;

use super::super::InteractiveModelSelection;
use crate::{
    agent::ADVISOR_AGENT_ID, config::InternalAgentModelConfig, model_aliases::ModelAliases,
    tui::tests::test_app,
};

#[test]
fn model_refresh_prefers_the_active_available_auth_mode() {
    let descriptor = rho_providers::provider::provider_descriptor("openrouter").unwrap();
    let available = vec!["openrouter-api-key".into(), "openrouter-oauth".into()];

    assert_eq!(
        super::refresh_auth_for_provider(descriptor, "openrouter-oauth", &available),
        "openrouter-oauth"
    );
}

#[test]
fn model_refresh_falls_back_to_an_available_auth_mode() {
    let descriptor = rho_providers::provider::provider_descriptor("openrouter").unwrap();
    let available = vec!["openrouter-oauth".into()];

    assert_eq!(
        super::refresh_auth_for_provider(descriptor, "openrouter-api-key", &available),
        "openrouter-oauth"
    );
}

fn aliases(entries: &[(&str, &str)]) -> ModelAliases {
    ModelAliases::from_entries(
        entries
            .iter()
            .map(|(name, target)| (name.to_string(), target.to_string()))
            .collect::<BTreeMap<_, _>>(),
    )
    .unwrap()
}

#[test]
fn resolves_alias_before_interactive_model_lookup() {
    let mut app = test_app();
    app.info.runtime.model_aliases = aliases(&[("deep", "openai-codex/gpt-5.5")]);

    let resolved = app
        .resolve_model_selection("@deep", &app.info.runtime.provider, &app.info.runtime.auth)
        .unwrap();

    assert_eq!(
        resolved,
        InteractiveModelSelection {
            selection: ModelSelection {
                provider: "openai-codex".into(),
                model: "gpt-5.5".into(),
                auth: "codex".into(),
                from_catalog: true,
            },
            alias: Some("deep".into()),
        }
    );
}

#[test]
fn bare_alias_keeps_current_provider() {
    let mut app = test_app();
    app.info.runtime.model_aliases = aliases(&[("fast", "gpt-5.5")]);

    let resolved = app
        .resolve_model_selection("@fast", "openai-codex", "codex")
        .unwrap();

    assert_eq!(
        resolved,
        InteractiveModelSelection {
            selection: ModelSelection {
                provider: "openai-codex".into(),
                model: "gpt-5.5".into(),
                auth: "codex".into(),
                from_catalog: true,
            },
            alias: Some("fast".into()),
        }
    );
}

#[test]
fn reports_undefined_alias_in_interactive_model_lookup() {
    let app = test_app();

    let error = app
        .resolve_model_selection(
            "@missing",
            &app.info.runtime.provider,
            &app.info.runtime.auth,
        )
        .unwrap_err();

    assert_eq!(
        error.to_string(),
        "model alias '@missing' is not defined; define it in [model.aliases] or use a concrete model reference"
    );
}

// Covers: first internal-agent model pick does not materialize definition defaults.
// Owner: internal agent model selection
#[test]
fn selecting_an_internal_agent_model_keeps_reasoning_unset_by_default() {
    let mut app = test_app();

    app.select_internal_agent_model(
        ADVISOR_AGENT_ID,
        Some(ModelSelection {
            provider: "openai".into(),
            model: "gpt-5.5".into(),
            auth: "api-key".into(),
            from_catalog: true,
        }),
    )
    .unwrap();

    let stored = app
        .info
        .runtime
        .internal_agents
        .get(ADVISOR_AGENT_ID)
        .expect("advisor model stored");
    assert_eq!(stored.reasoning, None);
    assert_eq!(
        app.info
            .services
            .config_repository
            .load()
            .unwrap()
            .internal_agent_model(ADVISOR_AGENT_ID)
            .and_then(|model| model.reasoning),
        None
    );
}

// Covers: an explicit reasoning override is carried across model switches.
// Owner: internal agent model selection
#[test]
fn selecting_an_internal_agent_model_carries_explicit_reasoning() {
    let mut app = test_app();
    let mut previous =
        InternalAgentModelConfig::new("anthropic".into(), "claude-old".into(), "api-key".into());
    previous.reasoning = Some(ReasoningLevel::High);
    app.info
        .runtime
        .internal_agents
        .insert(ADVISOR_AGENT_ID.into(), previous);

    app.select_internal_agent_model(
        ADVISOR_AGENT_ID,
        Some(ModelSelection {
            provider: "openai".into(),
            model: "gpt-5.5".into(),
            auth: "api-key".into(),
            from_catalog: true,
        }),
    )
    .unwrap();

    assert_eq!(
        app.info
            .runtime
            .internal_agents
            .get(ADVISOR_AGENT_ID)
            .and_then(|model| model.reasoning),
        Some(ReasoningLevel::High)
    );
}

// Covers: select_model_report must apply Auto's provider-preferred edit format
// after a provider change, while a pinned preference stays put.
// Owner: model switch edit-tool handoff
#[tokio::test]
async fn select_model_report_auto_edit_tool_follows_provider_change() {
    use std::sync::Arc;

    use rho_providers::credentials::{save_provider_api_key, MemoryCredentialStore};

    use crate::{
        app::interactive_runtime::test_edit_tool_runtime,
        config::EditTool,
        tui::{tests::test_bootstrap, App, InteractiveRuntime},
    };

    async fn switch_to_anthropic(app: &mut App, agent: &mut InteractiveRuntime) {
        app.select_model_report(
            InteractiveModelSelection {
                selection: ModelSelection {
                    provider: "anthropic".into(),
                    model: "claude-fable-5".into(),
                    auth: "api-key".into(),
                    from_catalog: true,
                },
                alias: None,
            },
            agent,
        )
        .await
        .expect("model switch should succeed")
        .expect("handoff report");
    }

    fn advertised_edit_name(agent: &InteractiveRuntime) -> Option<&'static str> {
        ["edit", "apply_patch", "str_replace"]
            .into_iter()
            .find(|name| agent.has_tool(name))
    }

    // --- Auto follows the provider ---
    let store = Arc::new(MemoryCredentialStore::default());
    save_provider_api_key(store.as_ref(), "openai", "sk-test").unwrap();
    save_provider_api_key(store.as_ref(), "anthropic", "sk-ant-test").unwrap();
    let mut app = App::new_with_credentials(
        test_bootstrap(),
        store,
        crate::herdr::HerdrGraphicsCapability::NotHerdr,
        crate::tools::mcp::McpSessionReport::default(),
        crate::tools::mcp::McpCatalog::default(),
        crate::plugins::PluginLoadReport::default(),
    );
    app.info
        .services
        .config_repository
        .update(|config| {
            config.edit_tool = EditTool::Auto;
            config.provider = "openai".into();
            config.model = "gpt-5.5".into();
            config.auth = "api-key".into();
        })
        .unwrap();

    let mut agent = test_edit_tool_runtime(EditTool::Auto).await;
    assert_eq!(
        advertised_edit_name(&agent),
        Some("edit"),
        "Auto + openai should start on hashline (`edit`)"
    );

    switch_to_anthropic(&mut app, &mut agent).await;
    assert_eq!(app.info.runtime.provider, "anthropic");
    assert_eq!(
        advertised_edit_name(&agent),
        Some("str_replace"),
        "Auto must follow anthropic to str_replace after select_model_report"
    );

    // --- Pinned does not follow ---
    let store = Arc::new(MemoryCredentialStore::default());
    save_provider_api_key(store.as_ref(), "openai", "sk-test").unwrap();
    save_provider_api_key(store.as_ref(), "anthropic", "sk-ant-test").unwrap();
    let mut app = App::new_with_credentials(
        test_bootstrap(),
        store,
        crate::herdr::HerdrGraphicsCapability::NotHerdr,
        crate::tools::mcp::McpSessionReport::default(),
        crate::tools::mcp::McpCatalog::default(),
        crate::plugins::PluginLoadReport::default(),
    );
    app.info
        .services
        .config_repository
        .update(|config| {
            config.edit_tool = EditTool::Pinned(rho_tools::EditFormat::Hashline);
            config.provider = "openai".into();
            config.model = "gpt-5.5".into();
            config.auth = "api-key".into();
        })
        .unwrap();

    let mut agent = test_edit_tool_runtime(EditTool::Pinned(rho_tools::EditFormat::Hashline)).await;
    assert_eq!(advertised_edit_name(&agent), Some("edit"));

    switch_to_anthropic(&mut app, &mut agent).await;
    assert_eq!(app.info.runtime.provider, "anthropic");
    assert_eq!(
        advertised_edit_name(&agent),
        Some("edit"),
        "pinned hashline must not follow the anthropic provider change"
    );
    assert!(!agent.has_tool("str_replace"));
}

// Covers: a mid-session model switch must reach the model as an appended line,
// because the system prompt names the starting model and then stays fixed. The
// line names only the new model, and includes the catalog name when the snapshot
// already has one. A first selection on an empty session is not a switch and
// must stay silent.
// Owner: model switch context notice
#[tokio::test]
async fn select_model_report_tells_the_model_about_a_mid_session_switch() {
    use std::sync::Arc;

    use rho_providers::{
        credentials::{save_provider_api_key, MemoryCredentialStore},
        model::{
            display_name::ModelDisplayNameCacheGuard,
            models_dev::{
                with_models_dev_cache_dir_for_tests, write_cached_model_metadata_for_tests,
                ModelMetadata,
            },
        },
    };

    use crate::{
        app::interactive_runtime::test_edit_tool_runtime,
        config::EditTool,
        model_identity::PromptModel,
        prompt::{model_switch_context, ModelSwitchKind},
        tui::{tests::test_bootstrap, App, InteractiveRuntime},
    };

    async fn switch_to_anthropic(app: &mut App, agent: &mut InteractiveRuntime) {
        app.select_model_report(
            InteractiveModelSelection {
                selection: ModelSelection {
                    provider: "anthropic".into(),
                    model: "claude-fable-5".into(),
                    auth: "api-key".into(),
                    from_catalog: true,
                },
                alias: None,
            },
            agent,
        )
        .await
        .expect("model switch should succeed");
    }

    /// Model-visible history as one string. A provider change also swaps the
    /// Auto edit tool, so the switch notice is not reliably the last message.
    fn history_text(agent: &InteractiveRuntime) -> String {
        agent
            .history()
            .iter()
            .filter_map(|message| match message {
                rho_sdk::model::Message::User(blocks) => Some(
                    blocks
                        .iter()
                        .filter_map(|block| match block {
                            rho_sdk::model::ContentBlock::Text(text) => Some(text.as_str()),
                            _ => None,
                        })
                        .collect::<String>(),
                ),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    fn app_on_openai() -> App {
        let store = Arc::new(MemoryCredentialStore::default());
        save_provider_api_key(store.as_ref(), "openai", "sk-test").unwrap();
        save_provider_api_key(store.as_ref(), "anthropic", "sk-ant-test").unwrap();
        let app = App::new_with_credentials(
            test_bootstrap(),
            store,
            crate::herdr::HerdrGraphicsCapability::NotHerdr,
            crate::tools::mcp::McpSessionReport::default(),
            crate::tools::mcp::McpCatalog::default(),
            crate::plugins::PluginLoadReport::default(),
        );
        app.info
            .services
            .config_repository
            .update(|config| {
                config.provider = "openai".into();
                config.model = "gpt-5.5".into();
                config.auth = "api-key".into();
            })
            .unwrap();
        app
    }

    let catalog = tempfile::tempdir().unwrap();
    with_models_dev_cache_dir_for_tests(catalog.path().to_path_buf(), || {
        let _names = ModelDisplayNameCacheGuard::new();
        write_cached_model_metadata_for_tests(
            "anthropic",
            "claude-fable-5",
            &ModelMetadata {
                display_name: Some("Claude Fable 5".into()),
                reasoning_metadata_complete: true,
                ..ModelMetadata::default()
            },
        );
    });

    // --- A started session is told, naming the new model with its catalog name ---
    let mut app = app_on_openai();
    let mut agent = test_edit_tool_runtime(EditTool::Auto).await;
    agent
        .append_user_context_with_display("first turn".into(), "first turn".into())
        .unwrap();

    // Keep the test cache dir for the switch so describe() reads the seeded name.
    let _cache =
        rho_providers::model::models_dev::ModelsDevCacheDirGuard::new(catalog.path().to_path_buf());
    let _names = ModelDisplayNameCacheGuard::new();
    switch_to_anthropic(&mut app, &mut agent).await;

    let text = history_text(&agent);
    let (stored, _) = model_switch_context(
        ModelSwitchKind::Conversation,
        &PromptModel::Rho {
            provider: "anthropic".into(),
            model: "claude-fable-5".into(),
        },
    );
    assert!(
        text.contains(stored.trim()),
        "expected switch notice {stored:?} in history:\n{text}"
    );
    // The model the session started on stays readable in the system prompt, so
    // the notice does not restate it.
    assert!(!text.contains("openai/gpt-5.5"), "{text}");

    // --- A first selection on an empty session stays silent ---
    let mut app = app_on_openai();
    let mut agent = test_edit_tool_runtime(EditTool::Auto).await;
    assert!(agent.history().is_empty());

    switch_to_anthropic(&mut app, &mut agent).await;

    let text = history_text(&agent);
    assert!(
        !text.contains("conversation model switched"),
        "a first model choice is not a switch: {text}"
    );
}