rho-coding-agent 3.11.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use pretty_assertions::assert_eq;
use rho_protocol::display::{ToolBody, ToolCard, ToolCardStatus, ToolFact, ToolFamily, ToolHeader};
use serde_json::{json, Value};

use super::{finished_card, preview_card};

const ID: &str = "1a2b3c4d-0000-4000-8000-000000000000";

fn card(status: ToolCardStatus, verb: &str, target: &str) -> ToolCard {
    ToolCard::new(
        status,
        ToolFamily::Default,
        ToolHeader::status_first(verb, target),
    )
}

fn done(verb: &str, target: &str) -> ToolCard {
    card(ToolCardStatus::Ok, verb, target)
}

/// A `new` call's card, as an `agent` call's.
fn child(status: ToolCardStatus, agent: &str, detail: &str, prompt: &[&str]) -> ToolCard {
    ToolCard::new(
        status,
        ToolFamily::Agent,
        ToolHeader::status_first(agent, detail),
    )
    .with_body(lines(prompt))
}

fn meta(text: &str) -> ToolFact {
    ToolFact::Meta { text: text.into() }
}

fn lines(lines: &[&str]) -> ToolBody {
    ToolBody::Lines(lines.iter().map(|line| (*line).to_owned()).collect())
}

fn pretty(value: Value) -> String {
    serde_json::to_string_pretty(&value).unwrap()
}

// Covers: before a result, a card names its target from the arguments
// alone, in the running tense while the call runs or never ended: the
// session shortened, a search's query; a new session's card reads as an
// `agent` call's, by the agent it runs as (the runtime's name for another
// runtime, `session` with the verb alone for rho's default agent, named or
// not) with its prompt's first line as a fact and the whole to expand; a
// history read names no session, since its argument is a search handle, and
// a null `anchor` still means a live read.
// Owner: sessions card presenter.
#[test]
fn preview_names_the_target_in_the_running_tense() {
    let running = ToolCardStatus::Running;
    for (case, arguments, status, want) in [
        (
            "list",
            json!({"action": "list"}),
            running,
            card(running, "listing", "sessions"),
        ),
        (
            "new, on the default agent",
            json!({"action": "new", "workspace": "/w", "prompt": "Fix the fold\nthen test"}),
            running,
            child(
                running,
                "session",
                "starting",
                &["Fix the fold", "then test"],
            )
            .with_facts(vec![meta("\"Fix the fold\"")]),
        ),
        (
            "new, on the default agent named",
            json!({"action": "new", "workspace": "/w", "prompt": "Port it", "agent": "default"}),
            running,
            child(running, "session", "starting", &["Port it"])
                .with_facts(vec![meta("\"Port it\"")]),
        ),
        (
            "new, on another runtime",
            json!({"action": "new", "workspace": "/w", "prompt": "Port it", "runtime": "claude-cli"}),
            running,
            child(running, "Claude Code", "starting session", &["Port it"])
                .with_facts(vec![meta("\"Port it\"")]),
        ),
        (
            "new, as a runtime's own agent",
            json!({"action": "new", "workspace": "/w", "prompt": "Port it", "agent": "antigravity"}),
            running,
            child(running, "Antigravity", "starting session", &["Port it"])
                .with_facts(vec![meta("\"Port it\"")]),
        ),
        (
            "new, on rho's runtime named",
            json!({"action": "new", "workspace": "/w", "prompt": "Port it", "runtime": "rho"}),
            running,
            child(running, "session", "starting", &["Port it"])
                .with_facts(vec![meta("\"Port it\"")]),
        ),
        (
            "wait",
            json!({"action": "wait", "session": ID}),
            running,
            card(running, "waiting on", "1a2b3c4d"),
        ),
        (
            "interrupted wait",
            json!({"action": "wait", "session": ID}),
            ToolCardStatus::Interrupted,
            card(ToolCardStatus::Interrupted, "waiting on", "1a2b3c4d"),
        ),
        (
            "live read with a null anchor",
            json!({"action": "read", "session": ID, "anchor": null}),
            running,
            card(running, "reading", "1a2b3c4d"),
        ),
        (
            "history read",
            json!({"action": "read", "session": "f18996c5aa", "anchor": "m3"}),
            running,
            card(running, "reading", "history"),
        ),
        (
            "search",
            json!({"action": "search", "query": "quartz"}),
            running,
            card(running, "searching", "\"quartz\""),
        ),
        (
            "unknown action",
            json!({"action": "teleport"}),
            running,
            ToolCard::new(
                running,
                ToolFamily::Default,
                ToolHeader::call("sessions", None),
            ),
        ),
    ] {
        assert_eq!(preview_card(&arguments, status), want, "{case}");
    }
}

// Covers: a finished control call names its session by the title its result
// carries, else its short id, and shows what happened without its JSON: a
// list's state tally, filters and table; a new session as an agent call's
// card, with its place and model, or that it could not start; the first
// line of a sent message, with all of it to expand only when that line left
// some out; an answer's picks; a live read's window; a wait's standing with
// its answer, error, or questions. A failure shows its error
// and a result the card cannot read shows as it came.
// Owner: sessions card presenter.
#[test]
fn finished_control_calls_name_the_session_and_the_outcome() {
    let session = |action: &str| json!({"action": action, "session": ID});
    let titled = pretty(json!({"session": ID, "title": "Fix fold jump"}));
    let steered = pretty(json!({"id": "steer-1", "session": ID}));
    let told = format!("{titled}\nrho tells you when this turn ends or asks a question.");
    let list = pretty(json!({
        "sessions": [
            {"session": ID, "workspace": "/src/rho", "state": "running", "opened_by": "agent",
             "title": "Fix fold jump", "updated_at": 1},
            {"session": "9f8e7d6c-1", "workspace": "/src/rho", "state": "waiting_for_input",
             "opened_by": "agent", "updated_at": 1,
             "worktree": {"branch": "rho/fold", "path": "/t/fold", "origin": "/src/rho"}},
            {"session": "5a5a5a5a-2", "workspace": "/src/ui", "state": "queued",
             "queue": {"budget": "agent_concurrency", "limit": 1, "position": 2},
             "opened_by": "person", "title": "Theme pass", "updated_at": 1},
        ],
        "more": true,
    }));
    let new = pretty(json!({
        "session": ID, "workspace": "/src/rho",
        "worktree": {"branch": "rho/fold", "path": "/t/fold", "origin": "/src/rho"},
    }));
    let read = pretty(json!({
        "session": ID, "title": "Fix fold jump", "text": "user: hi\nassistant: hello",
        "start": 0, "end": 26, "total_chars": 90, "next_start": 26,
    }));
    let answered = pretty(json!({
        "session": ID, "title": "Fix fold jump", "state": "completed", "done": true,
        "outcome": {"type": "answered", "text": "\nFixed it.\nTests pass.", "stop": "end_turn"},
    }));
    let asks = pretty(json!({
        "session": ID, "state": "waiting_for_input", "done": true,
        "inputs": [{"id": 7, "ask": {"kind": "question", "title": "Pick a fix",
            "reason": "Two fixes pass the tests.", "questions": [{
            "id": "fix", "prompt": "Which fix?", "header": "Fix", "selection": "one",
            "allow_other": false, "required": true,
            "default": {"values": ["clamp"], "style": "selected"},
            "choices": [{"value": "clamp", "label": "Clamp"}, {"value": "skip", "label": "Skip"}],
        }]}}],
    }));
    let failed = pretty(json!({
        "session": ID, "state": "failed", "done": true,
        "outcome": {"type": "failed", "error": {"kind": "provider", "message": "rate limited\nretry later"}},
    }));
    let running = pretty(json!({"session": ID, "state": "running", "done": false}));
    let queued = pretty(json!({
        "session": ID, "state": "queued", "done": false,
        "queue": {"budget": "agent_concurrency", "limit": 1, "position": 1},
    }));
    let long = "it keeps the jump bounded ".repeat(6);
    let long = long.trim_end().to_owned();
    let wait_for = json!({"action": "wait", "session": ID, "timeout_secs": 50});

    let cases = [
        (
            "list",
            json!({"action": "list", "workspace": "/src/rho", "hide_idle": true}),
            list.as_str(),
            true,
            done("listed", "3 sessions")
                .with_facts(vec![
                    meta("1 running · 1 needs input · 1 queued · more not listed"),
                    meta("in rho · idle hidden"),
                ])
                .with_body(lines(&[
                    "running      1a2b3c4d  Fix fold jump                             rho",
                    "needs input  9f8e7d6c  untitled                                  worktree rho/fold",
                    "queued #2    5a5a5a5a  Theme pass                                ui",
                ])),
        ),
        (
            "empty list",
            json!({"action": "list"}),
            r#"{"sessions": [], "more": false}"#,
            true,
            done("listed", "no sessions"),
        ),
        (
            "new",
            json!({"action": "new", "workspace": "/src/rho", "prompt": "Fix the fold\nthen test",
                   "agent": "worker", "model": "opus", "worktree": true}),
            new.as_str(),
            true,
            child(
                ToolCardStatus::Ok,
                "worker",
                "started session",
                &["Fix the fold", "then test"],
            )
            .with_facts(vec![
                meta("\"Fix the fold\""),
                meta("1a2b3c4d · rho · worktree rho/fold"),
                meta("opus"),
            ]),
        ),
        (
            "new, failed",
            json!({"action": "new", "workspace": "/src/rho", "prompt": "Fix it", "agent": "worker"}),
            "unknown agent: worker",
            false,
            child(ToolCardStatus::Error, "worker", "could not start session", &["Fix it"])
                .with_facts(vec![
                    meta("\"Fix it\""),
                    ToolFact::Error {
                        text: "unknown agent: worker".into(),
                    },
                ]),
        ),
        (
            "prompt, titled, one line with a double space",
            json!({"action": "prompt", "session": ID, "prompt": "Rebase  please"}),
            told.as_str(),
            true,
            done("prompted", "Fix fold jump").with_facts(vec![meta("\"Rebase please\"")]),
        ),
        (
            "steer, untitled, through the text alias",
            json!({"action": "steer", "session": ID, "text": "Stop\nand rebase"}),
            steered.as_str(),
            true,
            done("steered", "1a2b3c4d")
                .with_facts(vec![meta("\"Stop\"")])
                .with_body(lines(&["Stop", "and rebase"])),
        ),
        (
            "cancel",
            session("cancel"),
            titled.as_str(),
            true,
            done("cancelled", "Fix fold jump").with_facts(vec![meta("its turn was asked to stop")]),
        ),
        (
            "answer",
            json!({"action": "answer", "session": ID, "request": 7,
                   "answers": {"fix": ["clamp"], "why": ["it", "fits"]}}),
            titled.as_str(),
            true,
            done("answered", "Fix fold jump").with_facts(vec![meta("fix: clamp · why: it, fits")]),
        ),
        (
            "answer too long for its line",
            json!({"action": "answer", "session": ID, "request": 7,
                   "answers": {"fix": ["clamp"], "why": [long.as_str()]}}),
            titled.as_str(),
            true,
            done("answered", "Fix fold jump")
                .with_facts(vec![meta("fix: clamp · why: it keeps the jump bounded it keeps the jump bounded it keeps the jump bounded it keeps the jump bound…")])
                .with_body(ToolBody::Lines(vec!["fix: clamp".into(), format!("why: {long}")])),
        ),
        (
            "decline",
            json!({"action": "answer", "session": ID, "request": 7, "decline": true}),
            titled.as_str(),
            true,
            done("answered", "Fix fold jump").with_facts(vec![meta("declined")]),
        ),
        (
            "live read",
            session("read"),
            read.as_str(),
            true,
            done("read", "Fix fold jump")
                .with_facts(vec![meta("transcript · chars 0–26 of 90 · more available")])
                .with_body(lines(&["user: hi", "assistant: hello"])),
        ),
        (
            "wait, answered",
            session("wait"),
            answered.as_str(),
            true,
            done("waited", "Fix fold jump · completed")
                .with_facts(vec![meta("Fixed it.")])
                .with_body(lines(&["Fixed it.", "Tests pass."])),
        ),
        (
            "wait, asks a question",
            session("wait"),
            asks.as_str(),
            true,
            done("waited", "1a2b3c4d · needs an answer")
                .with_facts(vec![meta("asks \"Pick a fix\" · 1 question · request 7")])
                .with_body(lines(&[
                    "Two fixes pass the tests.",
                    "Fix: Which fix?",
                    "  • Clamp (default)",
                    "  • Skip",
                ])),
        ),
        (
            "wait, failed",
            session("wait"),
            failed.as_str(),
            true,
            done("waited", "1a2b3c4d · failed")
                .with_facts(vec![ToolFact::Error {
                    text: "rate limited".into(),
                }])
                .with_body(lines(&["rate limited", "retry later"])),
        ),
        (
            "wait, still running",
            wait_for.clone(),
            running.as_str(),
            true,
            done("waited", "1a2b3c4d · still running")
                .with_facts(vec![meta("no answer yet after 50s")]),
        ),
        (
            "wait, queued for the agent budget",
            wait_for.clone(),
            queued.as_str(),
            true,
            done("waited", "1a2b3c4d · queued #1")
                .with_facts(vec![meta("no answer yet after 50s")]),
        ),
        (
            "failure",
            session("prompt"),
            "no session 1a2b3c4d is known to the rho daemon",
            false,
            card(ToolCardStatus::Error, "prompted", "1a2b3c4d").with_facts(vec![
                ToolFact::Error {
                    text: "no session 1a2b3c4d is known to the rho daemon".into(),
                },
            ]),
        ),
        (
            "unreadable result",
            session("cancel"),
            "{}",
            true,
            done("cancelled", "1a2b3c4d").with_body(lines(&["{}"])),
        ),
    ];
    for (case, arguments, content, ok, want) in cases {
        assert_eq!(finished_card(&arguments, content, ok), want, "{case}");
    }
}

// Covers: a history read names the session by the id its result gives, not
// the search handle it took, and a result saved before reads gave one names
// history alone; a recall says which window of the original it shows and
// when that original failed.
// Owner: sessions card presenter.
#[test]
fn finished_history_reads_name_the_session_not_the_handle() {
    let arguments = json!({"action": "read", "session": "f18996c5aa", "anchor": "m3"});
    let read = |id: Option<&str>| {
        let mut read = json!({
            "index": {"skipped_files": 0, "omitted_records": 0},
            "session": "f18996c5aa", "anchor": "m3", "role": "user", "text": "quartz",
            "start": 0, "end": 6, "total_chars": 6, "next_start": null, "next_anchor": null,
            "previous_anchor": null, "omitted_blocks": 0,
        });
        if let Some(id) = id {
            read["id"] = json!(id);
        }
        serde_json::to_string(&read).unwrap()
    };
    let recall = serde_json::to_string(&json!({
        "note": "untrusted", "recall_id": "r00000000000000ab", "tool_call_id": "call-1",
        "ok": false, "text": "boom", "start": 0, "end": 4, "total_chars": 9, "next_start": 4,
    }))
    .unwrap();
    let read_body = |first: &str| lines(&[first, "anchor: m3", "", "quartz"]);
    let cases = [
        (
            "read",
            arguments.clone(),
            read(Some(ID)),
            done("read", "history 1a2b3c4d")
                .with_facts(vec![meta("user · chars 0–6 of 6")])
                .with_body(read_body(&format!("session: {ID}"))),
        ),
        (
            "read saved before reads gave an id",
            arguments,
            read(None),
            done("read", "history")
                .with_facts(vec![meta("user · chars 0–6 of 6")])
                .with_body(read_body("handle: f18996c5aa")),
        ),
        (
            "recall",
            json!({"action": "recall", "recall_id": "r00000000000000ab"}),
            recall,
            done("recalled", "r00000000000000ab")
                .with_facts(vec![meta(
                    "chars 0–4 of 9 · more available · the original call failed",
                )])
                .with_body(lines(&["boom"])),
        ),
    ];
    for (case, arguments, content, want) in cases {
        assert_eq!(finished_card(&arguments, &content, true), want, "{case}");
    }
}