rho-coding-agent 3.13.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
use pretty_assertions::assert_eq;

use super::{
    candidate_detail, candidate_item, merge_live_candidates, output_blocks, picker,
    retire_departed_live_runs, run_target, visible_candidates, AttachCandidate, WorkspaceRunFilter,
    LATEST_EXCERPT_ROWS,
};
use crate::{
    subagent::{RunState, RunStatus},
    tui::{
        agent_rail::{RailState, StartedSession, SubagentAttachTarget},
        picker::{DetailBlock, DetailField, DetailTone, ExcerptAnchor},
        PickerBadge, PickerBadgeTone,
    },
};
use rho_protocol::SessionId;

fn candidate(run_id: &str, state: RunState) -> AttachCandidate {
    AttachCandidate {
        run_id: run_id.into(),
        agent_id: "worker".into(),
        elapsed_seconds: 1,
        status: RunStatus {
            state,
            title: Some(run_id.into()),
            ..RunStatus::default()
        },
        prompt: None,
    }
}

fn heading(label: &str) -> DetailBlock {
    DetailBlock::Heading {
        label: label.into(),
        status: String::new(),
    }
}

fn facts(candidate: &AttachCandidate, now: u64) -> Vec<DetailField> {
    candidate_detail(candidate, now)
        .blocks
        .into_iter()
        .find_map(|block| match block {
            DetailBlock::Fields(fields) => Some(fields),
            _ => None,
        })
        .expect("run card has a facts block")
}

// Covers: attach rows name the run by role and title, and the nav badge
// shows live activity or the outcome in its own tone, never the run id.
// Owner: attach picker
#[test]
fn attach_row_shows_role_title_and_outcome_badge() {
    let mut running = candidate("abc123", RunState::Running);
    running.status.title = Some("Review the auth path".into());
    running.status.last_activity = Some("tool: read".into());
    let mut starting = candidate("def456", RunState::Starting);
    starting.agent_id = "explorer".into();
    starting.status.title = None;

    let cases = [
        (
            running,
            "worker",
            "Review the auth path",
            "read",
            PickerBadgeTone::Internal,
        ),
        (
            starting,
            "explorer",
            "untitled",
            "starting",
            PickerBadgeTone::Muted,
        ),
        (
            candidate("ok0001", RunState::Ok),
            "worker",
            "ok0001",
            "✓ done",
            PickerBadgeTone::Healthy,
        ),
        (
            candidate("err001", RunState::Error),
            "worker",
            "err001",
            "✗ error",
            PickerBadgeTone::Error,
        ),
    ];

    for (candidate, role, label, badge, tone) in cases {
        let item = candidate_item(&candidate);
        assert_eq!(
            (
                item.section.as_deref(),
                item.label.as_str(),
                item.value.as_str(),
                item.badge
            ),
            (
                Some(role),
                label,
                candidate.run_id.as_str(),
                Some(PickerBadge {
                    text: badge.into(),
                    tone
                }),
            )
        );
    }
}

// Covers: the card's closing section follows the run's outcome, so a live run
// shows its newest output, a finished one its answer, and a failure its error.
// Owner: attach picker run card
#[test]
fn run_card_output_section_follows_state() {
    let status = |state| RunStatus {
        state,
        last_text: Some("streaming tail".into()),
        result: Some("final answer".into()),
        error: Some("provider exploded".into()),
        ..RunStatus::default()
    };
    let cases = [
        (
            status(RunState::Running),
            vec![
                heading("LATEST"),
                DetailBlock::Excerpt {
                    text: "streaming tail".into(),
                    rows: LATEST_EXCERPT_ROWS,
                    anchor: ExcerptAnchor::End,
                },
            ],
        ),
        (
            status(RunState::Ok),
            vec![
                heading("RESULT"),
                DetailBlock::Paragraph("final answer".into()),
            ],
        ),
        (
            status(RunState::Error),
            vec![
                heading("ERROR"),
                DetailBlock::Error("provider exploded".into()),
            ],
        ),
        (
            RunStatus {
                state: RunState::Running,
                ..RunStatus::default()
            },
            vec![
                heading("LATEST"),
                DetailBlock::Muted("(no output yet)".into()),
            ],
        ),
    ];
    for (status, expected) in cases {
        assert_eq!(output_blocks(&status), expected, "{:?}", status.state);
    }
}

// Covers: facts prefer what the runtime actually bound and recede when the
// run never reported them, instead of printing blanks or zeros.
// Owner: attach picker run card
#[test]
fn run_card_facts_use_reported_values_and_mute_unknowns() {
    let mut finished = candidate("aaaaaa", RunState::Ok);
    finished.elapsed_seconds = 75;
    finished.status = RunStatus {
        state: RunState::Ok,
        model: Some("opus".into()),
        runtime_model: Some("claude-opus-5-5".into()),
        reasoning: Some(rho_sdk::ReasoningLevel::High),
        runtime: Some(crate::agent::AgentRuntime::ClaudeCli.into()),
        finished_at: Some(1_000),
        input_tokens: Some(48_200),
        output_tokens: Some(3_100),
        total_cost_usd: Some(0.14),
        ..RunStatus::default()
    };
    let unknown = candidate("bbbbbb", RunState::Running);

    assert_eq!(
        facts(&finished, 1_120),
        vec![
            DetailField::new("Model", "claude-opus-5-5 · high", DetailTone::Normal)
                .with_note("claude-cli"),
            DetailField::new("Elapsed", "1m 15s", DetailTone::Normal).with_note("finished 2m ago"),
            DetailField::new("Tokens", "48.2K in · 3.1K out", DetailTone::Normal)
                .with_note("$0.14"),
        ]
    );
    assert_eq!(
        facts(&unknown, 0),
        vec![
            DetailField::new("Activity", "working", DetailTone::Normal),
            DetailField::new("Model", "not reported", DetailTone::Muted),
            DetailField::new("Elapsed", "1s", DetailTone::Normal),
            DetailField::new("Tokens", "not reported", DetailTone::Muted),
        ]
    );
}

// Covers: a live refresh must not drop a prompt already read from disk, and a
// newly listed live run reads its prompt once.
// Owner: attach picker
#[test]
fn live_merge_keeps_known_prompts_and_reads_new_ones() {
    let mut disk = candidate("aaaaaa", RunState::Running);
    disk.prompt = Some("known task".into());
    let live = vec![
        candidate("aaaaaa", RunState::Running),
        candidate("bbbbbb", RunState::Starting),
    ];

    let merged = merge_live_candidates(vec![disk], live, |run_id| Some(format!("read {run_id}")));

    assert_eq!(
        merged
            .iter()
            .map(|run| (run.run_id.as_str(), run.prompt.as_deref()))
            .collect::<Vec<_>>(),
        [
            ("bbbbbb", Some("read bbbbbb")),
            ("aaaaaa", Some("known task"))
        ]
    );
}

// Covers: running-only must hide finished transcripts until the user toggles.
// Owner: attach picker
#[test]
fn running_only_hides_terminal_runs() {
    let candidates = [
        candidate("aaaaaa", RunState::Running),
        candidate("bbbbbb", RunState::Ok),
        candidate("cccccc", RunState::Error),
    ];

    let running_ids = visible_candidates(&candidates, WorkspaceRunFilter::RunningOnly)
        .into_iter()
        .map(|run| run.run_id.as_str())
        .collect::<Vec<_>>();
    let all_ids = visible_candidates(&candidates, WorkspaceRunFilter::All)
        .into_iter()
        .map(|run| run.run_id.as_str())
        .collect::<Vec<_>>();

    assert_eq!(running_ids, ["aaaaaa"]);
    assert_eq!(all_ids, ["aaaaaa", "bbbbbb", "cccccc"]);
}

// Covers: the sessions the followed session started list after its runs,
// under the agent each runs as (`session` for the default agent), by title
// and with what the rail says it is doing, valued by session id; running
// only keeps those whose turn is under way or that wait on a person.
// Owner: attach picker
#[test]
fn started_sessions_list_under_their_agent_by_state() {
    let started = |id: &str, agent: Option<&str>, state| StartedSession {
        session: SessionId::new(id),
        agent: agent.map(str::to_owned),
        title: format!("{id} title"),
        state,
        elapsed: None,
    };
    let sessions = [
        started("s-working", None, RailState::Working),
        started("s-asking", Some("worker"), RailState::NeedsInput),
        started("s-idle", Some("worker"), RailState::Idle),
    ];
    let runs = [candidate("aaaaaa", RunState::Running)];
    #[derive(Debug, PartialEq)]
    struct Row {
        section: Option<String>,
        title: String,
        value: String,
        tone: Option<PickerBadgeTone>,
    }
    let rows = |filter| {
        picker(&runs, &sessions, filter)
            .items
            .into_iter()
            .map(|item| Row {
                section: item.section,
                title: item.label,
                value: item.value,
                tone: item.badge.map(|badge| badge.tone),
            })
            .collect::<Vec<_>>()
    };
    let row = |section: &str, value: &str, title: &str, tone| Row {
        section: Some(section.to_owned()),
        title: title.to_owned(),
        value: value.to_owned(),
        tone: Some(tone),
    };
    let all = || {
        vec![
            row("worker", "aaaaaa", "aaaaaa", PickerBadgeTone::Internal),
            row(
                "session",
                "s-working",
                "s-working title",
                PickerBadgeTone::Internal,
            ),
            row(
                "worker",
                "s-asking",
                "s-asking title",
                PickerBadgeTone::Warning,
            ),
            row("worker", "s-idle", "s-idle title", PickerBadgeTone::Healthy),
        ]
    };
    let mut running = all();
    running.pop();
    assert_eq!(rows(WorkspaceRunFilter::RunningOnly), running);
    assert_eq!(rows(WorkspaceRunFilter::All), all());
}

// Covers: an empty inventory must still build an attach overlay.
// Owner: attach picker
#[test]
fn empty_inventory_still_builds_a_picker() {
    let empty = picker(&[], &[], WorkspaceRunFilter::RunningOnly);
    assert!(empty.items.is_empty());
    assert_eq!(empty.selected_item().map(|item| item.value.as_str()), None);
}

// Covers: live panel rows overlay matching disk rows and keep panel order for new lives.
// Owner: attach picker
#[test]
fn live_candidates_replace_matching_disk_rows() {
    let disk = vec![
        candidate("aaaaaa", RunState::Ok),
        candidate("bbbbbb", RunState::Running),
    ];
    let mut live = candidate("bbbbbb", RunState::Running);
    live.status.title = Some("updated".into());
    live.elapsed_seconds = 9;
    let first_missing = candidate("cccccc", RunState::Starting);
    let second_missing = candidate("dddddd", RunState::Starting);

    let merged = merge_live_candidates(disk, vec![live, first_missing, second_missing], |_| None);

    assert_eq!(
        merged
            .iter()
            .map(|run| (
                run.run_id.as_str(),
                run.status.title.as_deref(),
                run.elapsed_seconds
            ))
            .collect::<Vec<_>>(),
        [
            ("cccccc", Some("cccccc"), 1),
            ("dddddd", Some("dddddd"), 1),
            ("aaaaaa", Some("aaaaaa"), 1),
            ("bbbbbb", Some("updated"), 9),
        ]
    );
}

// Covers: finished transcripts keep their role after the picker closes.
// Owner: attach picker
#[test]
fn finished_run_agent_id_comes_from_candidates() {
    let mut finished = candidate("aaaaaa", RunState::Ok);
    finished.agent_id = "explorer".into();

    assert_eq!(
        run_target(&[finished], "aaaaaa"),
        Some(SubagentAttachTarget {
            run_id: "aaaaaa".into(),
            agent_id: "explorer".into(),
        })
    );
    assert_eq!(run_target(&[], "aaaaaa"), None);
}

// Covers: a run that leaves the live panel while /attach is open must show its
// finished result, not a stale running snapshot.
// Owner: attach picker
#[test]
fn departed_live_run_takes_finished_status() {
    let mut candidates = vec![
        candidate("aaaaaa", RunState::Running),
        candidate("bbbbbb", RunState::Running),
        candidate("cccccc", RunState::Running),
    ];
    candidates[0].prompt = Some("do the thing".into());
    let previously_live = ["aaaaaa".into(), "bbbbbb".into(), "cccccc".into()].into();
    let live_ids = ["cccccc".into()].into();
    let finished = RunStatus {
        state: RunState::Ok,
        title: Some("aaaaaa".into()),
        result: Some("done".into()),
        finished_at: Some(42),
        ..RunStatus::default()
    };

    retire_departed_live_runs(&mut candidates, &live_ids, &previously_live, |run_id| {
        (run_id == "aaaaaa").then(|| finished.clone())
    });

    let mut expected = vec![
        candidate("aaaaaa", RunState::Ok),
        candidate("bbbbbb", RunState::Stopped),
        candidate("cccccc", RunState::Running),
    ];
    expected[0].status = finished;
    expected[0].prompt = Some("do the thing".into());
    assert_eq!(candidates, expected);
}