rho-coding-agent 2.26.0

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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use agent_client_protocol::schema::v1::{
    CreateElicitationResponse, ElicitationAcceptAction, ElicitationAction, SessionId,
};
use pretty_assertions::assert_eq;
use rho_sdk::{
    HostChoice, HostInputRefusal, HostInputRequest, HostInputResponse, HostQuestion, SelectionMode,
    ToolCallId,
};
use serde_json::json;

use super::{elicitation_request, host_answer, FormDialect};

/// One question of each form control.
fn request() -> HostInputRequest {
    let questions = vec![
        HostQuestion::new(
            "scope",
            "Which scope should the change cover?",
            vec![
                HostChoice::new("crate", "One crate").description("Only the touched crate"),
                HostChoice::new("workspace", "Workspace"),
            ],
            SelectionMode::One,
        )
        .unwrap()
        .header("Scope")
        .help("Pick the narrowest that works.")
        .default_value(json!("workspace")),
        HostQuestion::new(
            "checks",
            "Which checks?",
            vec![
                HostChoice::new("fmt", "Format"),
                HostChoice::new("test", "Test"),
            ],
            SelectionMode::Many,
        )
        .unwrap()
        .optional(),
        HostQuestion::new(
            "notes",
            "Anything else?",
            vec![HostChoice::new("other", "Other")],
            SelectionMode::One,
        )
        .unwrap()
        .allow_other()
        .optional(),
        HostQuestion::new(
            "ship",
            "Open a PR?",
            vec![HostChoice::new("yes", "Yes"), HostChoice::new("no", "No")],
            SelectionMode::One,
        )
        .unwrap(),
        HostQuestion::new(
            "name",
            "Branch name?",
            vec![HostChoice::new("main", "main")],
            SelectionMode::One,
        )
        .unwrap()
        .allow_other(),
    ];
    HostInputRequest::questionnaire("Before I start", questions).unwrap()
}

// Covers: the standard form carries every question in the ACP schema (titled
// `oneOf`, string arrays, a free-text companion), scoped to the asking tool
// call, with selected defaults
// Owner: ACP elicitation mapping
#[test]
fn questions_become_one_form_scoped_to_the_tool_call() {
    let form = elicitation_request(
        &request(),
        &SessionId::new("session-1"),
        Some(&ToolCallId::from_string("call-1").unwrap()),
        FormDialect::Standard,
    );

    assert_eq!(
        serde_json::to_value(form).unwrap(),
        json!({
            "mode": "form",
            "sessionId": "session-1",
            "toolCallId": "call-1",
            "message": "Before I start",
            "requestedSchema": {
                "type": "object",
                "properties": {
                    "checks": {
                        "type": "array",
                        "title": "Which checks?",
                        "items": {"anyOf": [
                            {"const": "fmt", "title": "Format"},
                            {"const": "test", "title": "Test"},
                        ]},
                    },
                    "name": {
                        "type": "string",
                        "title": "Branch name?",
                        "enum": ["main"],
                        "oneOf": [{"const": "main", "title": "main"}],
                    },
                    "name.other": {
                        "type": "string",
                        "title": "Branch name? (other)",
                        "description": "Your own answer instead of a listed choice",
                    },
                    "notes": {"type": "string", "title": "Anything else?"},
                    "scope": {
                        "type": "string",
                        "title": "Scope",
                        "description": "Which scope should the change cover?\n\nPick the narrowest that works.",
                        "default": "workspace",
                        "enum": ["crate", "workspace"],
                        "oneOf": [
                            {"const": "crate", "title": "One crate", "description": "Only the touched crate"},
                            {"const": "workspace", "title": "Workspace"},
                        ],
                    },
                    "ship": {"type": "boolean", "title": "Open a PR?"},
                },
                "required": ["scope", "ship"],
            },
        })
    );
}

// Covers: T3 Code reads only `enum`, `title`, and `description` and lets any
// question take free text, so each question is one string property listing
// choice labels, with no free-text companion
// Owner: ACP elicitation mapping
#[test]
fn t3_code_gets_one_labelled_string_per_question() {
    let form = elicitation_request(
        &request(),
        &SessionId::new("session-1"),
        None,
        FormDialect::T3Code,
    );

    assert_eq!(
        serde_json::to_value(form).unwrap()["requestedSchema"],
        json!({
            "type": "object",
            "properties": {
                "checks": {
                    "type": "string",
                    "title": "Which checks?",
                    "description": "Pick one or more; separate several with commas.",
                    "enum": ["Format", "Test"],
                },
                "name": {"type": "string", "title": "Branch name?", "enum": ["main"]},
                "notes": {"type": "string", "title": "Anything else?"},
                "scope": {
                    "type": "string",
                    "title": "Scope",
                    "description": "Which scope should the change cover?\n\nPick the narrowest that works.",
                    "enum": ["One crate", "Workspace"],
                },
                "ship": {"type": "boolean", "title": "Open a PR?"},
            },
            "required": ["scope", "ship", "name"],
        })
    );
}

fn accept(content: serde_json::Value) -> CreateElicitationResponse {
    let content = serde_json::from_value(content).unwrap();
    CreateElicitationResponse::new(ElicitationAction::Accept(
        ElicitationAcceptAction::new().content(Some(content)),
    ))
}

// Covers: accepted content becomes answers in each control's encoding (T3
// Code answers a checkbox with its text and every field with strings);
// decline and cancel are refusals; content that does not answer the request
// is treated as declined rather than handed to the tool
// Owner: ACP elicitation mapping
#[test]
fn host_responses_become_answers_or_refusals() {
    let answered = HostInputResponse::new()
        .answer("scope", ["crate"])
        .answer("checks", ["fmt", "test"])
        .answer("notes", ["ship it"])
        .answer("ship", ["yes"])
        .answer("name", ["feat/forms"]);
    let cases = [
        (
            "every control",
            FormDialect::Standard,
            accept(json!({
                "scope": "crate",
                "checks": ["fmt", "test"],
                "notes": "ship it",
                "ship": true,
                "name.other": "feat/forms",
            })),
            Ok(answered.clone()),
        ),
        (
            "text answers and blanks",
            FormDialect::Standard,
            accept(json!({
                "scope": "workspace",
                "notes": "  ",
                "ship": "false",
                "name": "main",
                "name.other": "",
            })),
            Ok(HostInputResponse::new()
                .answer("scope", ["workspace"])
                .answer("ship", ["no"])
                .answer("name", ["main"])),
        ),
        (
            "a typed answer replaces a selection the host cannot clear",
            FormDialect::Standard,
            accept(
                json!({"scope": "crate", "ship": true, "name": "main", "name.other": "feat/forms"}),
            ),
            Ok(HostInputResponse::new()
                .answer("scope", ["crate"])
                .answer("ship", ["yes"])
                .answer("name", ["feat/forms"])),
        ),
        (
            "t3 code answers with labels, comma lists, and free text",
            FormDialect::T3Code,
            accept(json!({
                "scope": "One crate",
                "checks": "Format, test",
                "notes": "ship it",
                "ship": "true",
                "name": "feat/forms",
            })),
            Ok(answered),
        ),
        (
            "t3 code free text for a closed choice",
            FormDialect::T3Code,
            accept(
                json!({"scope": "galaxy", "checks": "fmt", "notes": "x", "ship": "true", "name": "main"}),
            ),
            Err(HostInputRefusal::Declined),
        ),
        (
            "missing required answer",
            FormDialect::Standard,
            accept(json!({"scope": "crate", "ship": true})),
            Err(HostInputRefusal::Declined),
        ),
        (
            "unknown choice",
            FormDialect::Standard,
            accept(json!({"scope": "galaxy", "ship": true, "name": "main"})),
            Err(HostInputRefusal::Declined),
        ),
        (
            "decline",
            FormDialect::Standard,
            CreateElicitationResponse::new(ElicitationAction::Decline),
            Err(HostInputRefusal::Declined),
        ),
        (
            "cancel",
            FormDialect::T3Code,
            CreateElicitationResponse::new(ElicitationAction::Cancel),
            Err(HostInputRefusal::Dismissed),
        ),
    ];
    for (label, dialect, response, expected) in cases {
        assert_eq!(
            host_answer(&request(), response, dialect),
            expected,
            "{label}"
        );
    }
}

// Covers: MCP property names may contain `.`, so a free-text companion key
// must not collide with another question's property
// Owner: ACP elicitation mapping
#[test]
fn free_text_companions_do_not_collide_with_question_ids() {
    let request = HostInputRequest::questionnaire(
        "Server form",
        vec![
            HostQuestion::new(
                "x",
                "X?",
                vec![HostChoice::new("a", "A")],
                SelectionMode::One,
            )
            .unwrap()
            .allow_other(),
            HostQuestion::new(
                "x.other",
                "Note?",
                vec![HostChoice::new("other", "Other")],
                SelectionMode::One,
            )
            .unwrap()
            .allow_other(),
        ],
    )
    .unwrap();
    let form = elicitation_request(&request, &SessionId::new("s"), None, FormDialect::Standard);
    let schema = serde_json::to_value(form).unwrap()["requestedSchema"]["properties"].clone();
    let mut keys = schema
        .as_object()
        .unwrap()
        .keys()
        .cloned()
        .collect::<Vec<_>>();
    keys.sort();

    assert_eq!(keys, ["x", "x.other", "x.other_"]);
    assert_eq!(
        host_answer(
            &request,
            accept(json!({"x.other_": "mine", "x.other": "note"})),
            FormDialect::Standard,
        ),
        Ok(HostInputResponse::new()
            .answer("x", ["mine"])
            .answer("x.other", ["note"]))
    );
}

// Covers: T3 Code answers with displayed labels, several joined by a
// separator no label contains; an exact label wins over another choice's
// value or a case-folded label, and a list splits into exactly the choices
// listed even when a label contains commas
// Owner: ACP elicitation mapping
#[test]
fn t3_code_labels_resolve_to_their_own_choices() {
    let choices = |selection, pairs: &[(&str, &str)]| {
        HostQuestion::new(
            "q",
            "Pick",
            pairs
                .iter()
                .map(|(value, label)| HostChoice::new(*value, *label))
                .collect(),
            selection,
        )
        .unwrap()
    };
    let cases = [
        (
            "exact label over a case-folded one",
            choices(SelectionMode::One, &[("lower", "foo"), ("upper", "FOO")]),
            "FOO",
            vec!["upper"],
        ),
        (
            "exact label over another choice's value",
            choices(SelectionMode::One, &[("X", "Y"), ("Z", "X")]),
            "X",
            vec!["Z"],
        ),
        (
            "a comma label switches the list to semicolons",
            choices(
                SelectionMode::Many,
                &[("a", "A"), ("b", "B"), ("c", "C"), ("ab", "A, B")],
            ),
            "A; B; C",
            vec!["a", "b", "c"],
        ),
        (
            "labels with every common separator and a new line",
            choices(
                SelectionMode::Many,
                &[
                    ("a", "A"),
                    ("b", "B"),
                    ("ab", "A\nB"),
                    ("mix", "A, B; C | D / E + F"),
                ],
            ),
            "A\nB • A",
            vec!["ab", "a"],
        ),
        (
            "the comma label in a semicolon list",
            choices(
                SelectionMode::Many,
                &[("a", "A"), ("b", "B"), ("c", "C"), ("ab", "A, B")],
            ),
            "A, B; C",
            vec!["ab", "c"],
        ),
    ];
    for (label, question, typed, expected) in cases {
        let request = HostInputRequest::questionnaire("Pick", vec![question]).unwrap();
        assert_eq!(
            host_answer(&request, accept(json!({"q": typed})), FormDialect::T3Code),
            Ok(HostInputResponse::new().answer("q", expected)),
            "{label}"
        );
    }
}

// Covers: the form's hint names the separator a typed multiple choice must
// use, so the user can type a list that splits back unambiguously; labels
// that leave no separator get a single choice instead of a panic
// Owner: ACP elicitation mapping
#[test]
fn t3_code_multiple_choice_hint_names_a_separator_no_label_contains() {
    let hint = |labels: &[&str]| {
        let question = HostQuestion::new(
            "q",
            "Pick",
            labels
                .iter()
                .enumerate()
                .map(|(index, label)| HostChoice::new(format!("v{index}"), *label))
                .collect(),
            SelectionMode::Many,
        )
        .unwrap();
        let request = HostInputRequest::questionnaire("Pick", vec![question]).unwrap();
        let form = elicitation_request(&request, &SessionId::new("s"), None, FormDialect::T3Code);
        serde_json::to_value(form).unwrap()["requestedSchema"]["properties"]["q"]["description"]
            .clone()
    };
    for (labels, expected) in [
        (&["A", "B"][..], "commas"),
        (&["A, B", "C"][..], "semicolons"),
        (&["A, B", "C; D"][..], "\"|\""),
        (&["A, B", "C; D | E / F + G"][..], "\"•\""),
    ] {
        assert_eq!(
            hint(labels),
            json!(format!(
                "Pick one or more; separate several with {expected}."
            )),
            "{labels:?}"
        );
    }
    // A label using every candidate leaves no separator: one typed choice,
    // never a split, and no panic.
    let every = ",;|/+•"
        .chars()
        .chain('\u{2190}'..='\u{2BFF}')
        .collect::<String>();
    assert_eq!(hint(&[every.as_str(), "B"]), json!("Pick one."));
    let request = HostInputRequest::questionnaire(
        "Pick",
        vec![HostQuestion::new(
            "q",
            "Pick",
            vec![
                HostChoice::new("all", every.as_str()),
                HostChoice::new("b", "B"),
            ],
            SelectionMode::Many,
        )
        .unwrap()],
    )
    .unwrap();
    assert_eq!(
        host_answer(&request, accept(json!({"q": every})), FormDialect::T3Code),
        Ok(HostInputResponse::new().answer("q", ["all"]))
    );
}

// Covers: a default names its choice by value even when that value is
// another choice's label; a default given as a label still selects it
// Owner: ACP elicitation mapping
#[test]
fn defaults_prefer_a_choice_value_over_another_label() {
    let default_for = |default: &str| {
        let question = HostQuestion::new(
            "q",
            "Pick",
            vec![
                HostChoice::new("first", "second"),
                HostChoice::new("second", "Other"),
            ],
            SelectionMode::One,
        )
        .unwrap()
        .default_value(json!(default));
        let request = HostInputRequest::questionnaire("Pick", vec![question]).unwrap();
        let form = elicitation_request(&request, &SessionId::new("s"), None, FormDialect::Standard);
        serde_json::to_value(form).unwrap()["requestedSchema"]["properties"]["q"]["default"].clone()
    };
    assert_eq!(
        [default_for("second"), default_for("other")],
        [json!("second"), json!("second")]
    );
}