rho-coding-agent 3.5.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
use pretty_assertions::assert_eq;
use rho_protocol::{ModelChoice, ModelOptions, ModelRef};

use rho_providers::model::favorites::CycleDirection;

use super::{model_choice, next_choice, next_pinned_model};

// Covers: a bare id shared by two providers must not silently select the
// wrong provider; fully qualified choices work without any local catalog.
// Owner: pure daemon option selection
#[test]
fn resolve_only_unambiguous_advertised_models() {
    let first = ModelRef {
        provider: "private-host".into(),
        model: "shared".into(),
    };
    let second = ModelRef {
        provider: "other-host".into(),
        model: "shared".into(),
    };
    let options = ModelOptions {
        auth: "api-key".into(),
        current: first.clone(),
        available: vec![
            ModelChoice {
                model: first.clone(),
                label: "Private model".into(),
            },
            ModelChoice {
                model: second.clone(),
                label: "Other model".into(),
            },
        ],
    };
    for (reference, expected) in [
        ("private-host/shared", Some(first.clone())),
        ("Private model", Some(first)),
        ("other-host/shared", Some(second)),
        ("shared", None),
        ("openai/not-advertised", None),
    ] {
        assert_eq!(
            model_choice(&options, reference).ok(),
            expected,
            "{reference}"
        );
    }
}

// Covers: cycling uses the server's offered order, wraps, and does not invent
// a choice when the current value is unknown to this client.
// Owner: pure daemon option selection
#[test]
fn cycle_advertised_choices_without_catalog_fallbacks() {
    use rho_protocol::ReasoningLevel::{High, Low, Max, Unknown};
    for (current, available, expected) in [
        (Low, vec![Low, Max, High], Some(Max)),
        (High, vec![Low, Max, High], Some(Low)),
        (High, vec![High], Some(High)),
        (Unknown, vec![Low, High], None),
        (Low, vec![], None),
    ] {
        assert_eq!(
            next_choice(current, &available),
            expected,
            "{current:?}, {available:?}"
        );
    }
}

fn options_on(provider: &str, auth: &str) -> rho_protocol::SessionOptions {
    rho_protocol::SessionOptions {
        model: ModelOptions {
            auth: auth.into(),
            current: ModelRef {
                provider: provider.into(),
                model: "model".into(),
            },
            available: Vec::new(),
        },
        reasoning: None,
        fast_mode: None,
        permission_mode: rho_protocol::PermissionModeOptions {
            current: rho_protocol::PermissionMode::Bypass,
            available: Vec::new(),
        },
        commands: Vec::new(),
        added_dirs: Vec::new(),
    }
}

// Covers: the launch found no credentials for the config's provider, but
// the daemon session, a resumed one for instance, runs on another: it is
// not held signed out, from its first report on. One on that same provider
// and auth stays signed out.
// Owner: tui daemon session options
#[test]
fn a_session_on_another_provider_than_the_signed_out_one_is_not_signed_out() {
    for (provider, auth, signed_in) in [
        ("anthropic", "anthropic-api-key", true),
        ("openai", "chatgpt", true),
        ("openai", "api-key", false),
    ] {
        let mut app = crate::tui::tests::test_app();
        app.info.runtime.provider = "openai".into();
        app.info.runtime.auth = "api-key".into();
        app.info.services.auth_unavailable = Some("no credentials for openai".into());

        app.show_daemon_options(options_on(provider, auth));

        assert_eq!(app.setup_state().signed_in, signed_in, "{provider} {auth}");
    }
}

// Covers: a daemon session's first launch opens at sign-in when it is
// signed out and no other auth offers a model. The daemon always lists the
// session's own model, so a list of one is nothing to choose from.
// Owner: tui daemon session options
#[test]
fn a_signed_out_first_launch_has_a_model_to_choose_only_from_another_auth() {
    let choice = |provider: &str| ModelChoice {
        model: ModelRef {
            provider: provider.into(),
            model: "model".into(),
        },
        label: provider.into(),
    };
    for (signed_out, available, can_choose) in [
        (true, vec![choice("openai")], false),
        (true, vec![choice("openai"), choice("anthropic")], true),
        (false, vec![choice("openai")], true),
    ] {
        let mut app = crate::tui::tests::test_app();
        let mut options = options_on("openai", "api-key");
        options.model.available = available;
        app.info.runtime.provider = "openai".into();
        app.info.runtime.auth = "api-key".into();
        app.show_daemon_options(options);
        app.info.services.auth_unavailable = signed_out.then(|| "no credentials".into());

        assert_eq!(app.daemon_setup_can_choose_model(), can_choose);
    }
}

// Covers: a daemon session's model picker lists the models the daemon
// offers under the TUI's own pins and scope, and a picker opened mid-turn
// marks the model already asked for.
// Owner: tui daemon session options
#[test]
fn the_model_picker_lays_offered_models_out_by_pin_and_pending_switch() {
    let choice = |provider: &str, model: &str| ModelChoice {
        model: ModelRef {
            provider: provider.into(),
            model: model.into(),
        },
        label: model.to_uppercase(),
    };
    let rows = |app: &crate::tui::App| {
        let picker = app.daemon_model_picker().expect("daemon options are shown");
        let rows: Vec<_> = picker
            .items
            .iter()
            .map(|item| {
                let badge = item.badge.as_ref().map(|badge| badge.text.clone());
                (item.value.clone(), badge)
            })
            .collect();
        (picker.title.clone(), rows)
    };
    let row = |value: &str, badge: Option<&str>| (value.to_owned(), badge.map(str::to_owned));
    let mut app = crate::tui::tests::test_app();
    let mut options = options_on("openai", "api-key");
    options.model.available = vec![
        choice("openai", "model"),
        choice("xai", "grok"),
        choice("poolside", "laguna"),
    ];
    app.show_daemon_options(options);

    assert_eq!(
        rows(&app),
        (
            "select model · all".to_owned(),
            vec![
                row("openai/model", Some("selected")),
                row("xai/grok", None),
                row("poolside/laguna", None),
            ]
        ),
        "no pins"
    );

    app.info.runtime.favorite_models = vec!["xai/grok".into(), "not/offered".into()];
    assert_eq!(
        rows(&app),
        (
            "select model · pinned".to_owned(),
            vec![row("xai/grok", Some("pinned"))]
        ),
        "a pin the daemon offers"
    );

    app.begin_provider_turn_ui();
    app.daemon_option_requests
        .push_back(rho_protocol::SessionChange::Model {
            model: ModelRef {
                provider: "xai".into(),
                model: "grok".into(),
            },
            auth: None,
            setup: false,
        });
    assert_eq!(
        rows(&app),
        (
            "select model for next turn · pinned".to_owned(),
            vec![row("xai/grok", Some("pinned, pending"))]
        ),
        "a switch queued behind the turn"
    );
}

// Covers: a session on an alias (Mistral's default `mistral-medium-latest`)
// gets the alias in its listed model's row from the daemon: the picker opens
// on that row, marked selected and pinned by a pin of the listed id; pin
// cycling moves on from that pin; and `/model` with the
// listed id names the current model rather than refusing it (#1945).
// Owner: tui daemon session options
#[test]
fn a_current_alias_is_the_row_offered_in_its_listed_models_place() {
    use rho_providers::model::provider_models::{
        replace_cached_aliased_provider_models_for_tests, with_provider_models_cache_dir_for_tests,
    };

    let model = |provider: &str, model: &str| ModelRef {
        provider: provider.into(),
        model: model.into(),
    };
    let alias = model("mistral", "mistral-small-latest");
    let cache = tempfile::tempdir().unwrap();
    let (picker, by_listed_id, cycled) =
        with_provider_models_cache_dir_for_tests(cache.path().to_path_buf(), || {
            replace_cached_aliased_provider_models_for_tests(
                "mistral",
                &[(
                    "mistral-small-2603",
                    "Mistral Small 4",
                    &["mistral-small-latest"],
                )],
            )
            .unwrap();
            let mut app = crate::tui::tests::test_app();
            app.info.runtime.favorite_models = vec![
                "xai/grok".into(),
                "mistral/mistral-small-2603".into(),
                "poolside/laguna".into(),
            ];
            let mut options = options_on("mistral", "api-key");
            options.model.current = alias.clone();
            options.model.available = [
                model("xai", "grok"),
                alias.clone(),
                model("poolside", "laguna"),
            ]
            .map(|model| ModelChoice {
                label: model.model.clone(),
                model,
            })
            .to_vec();
            app.show_daemon_options(options);
            let picker = app.daemon_model_picker().expect("daemon options are shown");
            let by_listed_id = model_choice(
                &app.daemon_options.as_ref().unwrap().model,
                "mistral/mistral-small-2603",
            )
            .ok();
            let options = &app.daemon_options.as_ref().unwrap().model;
            let favorites = rho_providers::model::favorites::normalized_favorite_models(
                &app.info.runtime.favorite_models,
            );
            let cycled = next_pinned_model(
                &options.available,
                &favorites,
                &options.current,
                CycleDirection::Forward,
            )
            .cloned();
            (picker, by_listed_id, cycled)
        });

    let rows: Vec<_> = picker
        .items
        .iter()
        .map(|item| {
            let badge = item.badge.as_ref().map(|badge| badge.text.as_str());
            (item.value.as_str(), badge)
        })
        .collect();
    assert_eq!(
        (rows, picker.selected),
        (
            vec![
                ("xai/grok", Some("pinned")),
                ("mistral/mistral-small-latest", Some("pinned, selected")),
                ("poolside/laguna", Some("pinned")),
            ],
            1
        )
    );
    assert_eq!(by_listed_id, Some(alias));
    assert_eq!(cycled, Some(model("poolside", "laguna")));
}

/// Who asked for the model switch whose options arrive.
#[derive(Clone, Copy)]
enum Switch {
    /// `/login`, after storing credentials.
    Login,
    /// The person, with `/model`.
    Model,
    /// The person, after the daemon refused the same switch from `/login`.
    ModelAfterRefusedLogin,
    /// `/login`, whose request finished before the options showed it.
    LoginFinishedFirst,
}

// Covers: a model switch this TUI asked for says where it landed, except
// the one `/login` sent after storing credentials: its status line already
// names the model and the credentials, and stays, whichever of its request
// and its options the TUI sees first. A login switch the daemon refused
// quiets nothing later (#1818, #1880).
// Owner: tui daemon session options
#[test]
fn a_switch_login_asked_for_keeps_logins_status_line() {
    const LOGIN: &str = "stored credentials for anthropic and selected anthropic/model";
    let kept: Vec<_> = [
        Switch::Login,
        Switch::Model,
        Switch::ModelAfterRefusedLogin,
        Switch::LoginFinishedFirst,
    ]
    .into_iter()
    .map(|switch| {
        let mut app = crate::tui::tests::test_app();
        app.show_daemon_options(options_on("openai", "api-key"));
        let switched = options_on("anthropic", "anthropic-api-key");
        let model = switched.model.current.clone();
        let change = rho_protocol::SessionChange::Model {
            model: model.clone(),
            auth: None,
            setup: false,
        };
        app.daemon_option_requests.push_back(change.clone());
        match switch {
            Switch::Login => app.daemon_login_selected = Some(model),
            Switch::Model => {}
            Switch::ModelAfterRefusedLogin => {
                app.daemon_login_selected = Some(model);
                app.finish_daemon_option_request();
                app.daemon_option_requests.push_back(change);
            }
            Switch::LoginFinishedFirst => {
                app.daemon_login_selected = Some(model);
                app.finish_daemon_option_request();
            }
        }
        app.set_status(LOGIN);

        app.show_daemon_options(switched);

        app.last_status == LOGIN
    })
    .collect();

    assert_eq!(kept, vec![true, false, false, true]);
}

/// When a model switch's request finished, relative to the options report
/// that follows it.
#[derive(Clone, Copy, Debug)]
enum Finished {
    /// After the report showed the switch, as the event stream orders it.
    AfterOptions,
    /// Before any report showed it, as when a reattach's response carries
    /// the options (#1880).
    BeforeOptions,
    /// Refused: the next report is on another model.
    Refused,
}

// Covers: a pinned-model switch says where it landed whether its request
// finished before or after the options showing it arrived, and a refused
// one says nothing when a later report lands elsewhere (#1880).
// Owner: tui daemon session options
#[test]
fn a_switch_says_where_it_landed_whenever_its_request_finished() {
    const BEFORE: &str = "status before the switch";
    let announced: Vec<_> = [
        Finished::AfterOptions,
        Finished::BeforeOptions,
        Finished::Refused,
    ]
    .into_iter()
    .map(|finished| {
        let mut app = crate::tui::tests::test_app();
        app.show_daemon_options(options_on("openai", "api-key"));
        let asked = options_on("anthropic", "anthropic-api-key");
        app.daemon_option_requests
            .push_back(rho_protocol::SessionChange::Model {
                model: asked.model.current.clone(),
                auth: None,
                setup: false,
            });
        app.set_status(BEFORE);
        match finished {
            Finished::AfterOptions => {
                app.show_daemon_options(asked);
                app.finish_daemon_option_request();
            }
            Finished::BeforeOptions => {
                app.finish_daemon_option_request();
                app.show_daemon_options(asked);
            }
            Finished::Refused => {
                app.finish_daemon_option_request();
                app.show_daemon_options(options_on("xai", "api-key"));
            }
        }
        app.last_status != BEFORE
    })
    .collect();

    assert_eq!(announced, vec![true, true, false]);
}