rho-coding-agent 3.0.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
use agent_client_protocol::v1::schema::{
    AvailableCommand, AvailableCommandsUpdate, CompactionId, CompactionStatus, CompactionUpdate,
    ConfigOptionUpdate, ContentBlock, ContentChunk, Cost, CurrentModeUpdate, Diff, MessageId,
    Notice, NoticeSeverity, Plan, PlanEntry, PlanEntryPriority, PlanEntryStatus, ResourceLink,
    SessionId, SessionInfoUpdate, SessionModeId, SessionUpdate, ToolCall, ToolCallContent,
    ToolCallId, ToolCallLocation, ToolCallStatus, ToolCallUpdate, ToolCallUpdateFields, ToolKind,
    UsageUpdate,
};
use pretty_assertions::assert_eq;
use rho_protocol::{self as wire, SessionEvent};
use serde_json::json;

use super::{notification, options_changed, Delivery, Updates};
use crate::app::acp::{host_features::HostFeatures, options};

fn features(compaction: bool, notices: bool) -> HostFeatures {
    HostFeatures {
        compaction,
        notices,
        ..HostFeatures::default()
    }
}

fn updates(features: HostFeatures) -> Updates {
    Updates::with_nonce(features, "n".to_owned())
}

fn message(id: &str, text: &str) -> ContentChunk {
    ContentChunk::new(ContentBlock::from(text)).message_id(MessageId::new(id))
}

fn tool_update(id: &str) -> wire::ToolCallUpdate {
    wire::ToolCallUpdate {
        id: wire::ToolCallId::new(id),
        title: None,
        kind: None,
        status: None,
        raw_input: None,
        content: None,
        locations: None,
        display: None,
    }
}

// Covers: each transcript event maps to the ACP update of the same meaning,
// field for field: message chunks keep the daemon's message ids, user
// messages drop blocks this rho cannot show, usage reports an unknown window
// as 0 and omits an unpriced cost, and cards and notices fall back to what a
// host without those features shows
// Owner: ACP event mapping
#[test]
fn transcript_events_map_to_acp_updates() {
    let plain = features(/*compaction*/ false, /*notices*/ false);
    let rich = features(/*compaction*/ true, /*notices*/ true);
    let cases = [
        (
            "user message",
            plain,
            SessionEvent::UserMessage {
                message: wire::MessageId::new("u1"),
                content: vec![
                    wire::ContentBlock::Text {
                        text: "read this".into(),
                    },
                    wire::ContentBlock::ResourceLink {
                        uri: "file:///a.md".into(),
                        name: "a.md".into(),
                        description: None,
                    },
                    wire::ContentBlock::Unknown,
                ],
                steer: None,
            },
            vec![
                SessionUpdate::UserMessageChunk(message("u1", "read this")),
                SessionUpdate::UserMessageChunk(
                    ContentChunk::new(ContentBlock::ResourceLink(ResourceLink::new(
                        "a.md",
                        "file:///a.md",
                    )))
                    .message_id(MessageId::new("u1")),
                ),
            ],
        ),
        (
            "agent chunk",
            plain,
            SessionEvent::AgentMessageChunk {
                message: wire::MessageId::new("m1"),
                text: "hi".into(),
            },
            vec![SessionUpdate::AgentMessageChunk(message("m1", "hi"))],
        ),
        (
            "thought chunk",
            plain,
            SessionEvent::ThoughtChunk {
                message: wire::MessageId::new("t1"),
                text: "hmm".into(),
            },
            vec![SessionUpdate::AgentThoughtChunk(message("t1", "hmm"))],
        ),
        (
            "plan",
            plain,
            SessionEvent::Plan {
                entries: vec![
                    wire::PlanEntry {
                        content: "write".into(),
                        status: wire::PlanStatus::InProgress,
                    },
                    wire::PlanEntry {
                        content: "later".into(),
                        status: wire::PlanStatus::Unknown,
                    },
                ],
            },
            vec![SessionUpdate::Plan(Plan::new(vec![
                PlanEntry::new(
                    "write",
                    PlanEntryPriority::Medium,
                    PlanEntryStatus::InProgress,
                ),
                PlanEntry::new("later", PlanEntryPriority::Medium, PlanEntryStatus::Pending),
            ]))],
        ),
        (
            "priced usage",
            plain,
            SessionEvent::Usage {
                used_tokens: 1200,
                window_tokens: Some(200_000),
                cost_usd_micros: Some(1_500_000),
            },
            vec![SessionUpdate::UsageUpdate(
                UsageUpdate::new(1200, 200_000).cost(Cost::new(1.5, "USD")),
            )],
        ),
        (
            "unpriced usage of an unknown window",
            plain,
            SessionEvent::Usage {
                used_tokens: 10,
                window_tokens: None,
                cost_usd_micros: None,
            },
            vec![SessionUpdate::UsageUpdate(UsageUpdate::new(10, 0))],
        ),
        (
            "title",
            plain,
            SessionEvent::TitleChanged {
                title: "Fix the build".into(),
            },
            vec![SessionUpdate::SessionInfoUpdate(
                SessionInfoUpdate::new().title("Fix the build".to_owned()),
            )],
        ),
        (
            "compaction to a host with cards",
            rich,
            SessionEvent::Compaction {
                id: wire::CompactionId::new("compaction-1"),
                status: wire::CompactionStatus::Failed {
                    error: "no provider".into(),
                },
            },
            vec![SessionUpdate::CompactionUpdate(
                CompactionUpdate::new(CompactionId::new("compaction-1"), CompactionStatus::Failed)
                    .error("no provider".to_owned()),
            )],
        ),
        (
            "compaction to a host without cards",
            plain,
            SessionEvent::Compaction {
                id: wire::CompactionId::new("compaction-1"),
                status: wire::CompactionStatus::Started {
                    trigger: Some(wire::CompactionTrigger::Command),
                },
            },
            vec![],
        ),
        (
            "discarded response",
            plain,
            SessionEvent::ResponseDiscarded,
            vec![SessionUpdate::AgentThoughtChunk(message(
                "rho-msg-n-1",
                "[provider response discarded; retrying]",
            ))],
        ),
        (
            "error card",
            plain,
            SessionEvent::Card {
                title: "Job failed".into(),
                markdown: "**exit 1**".into(),
                plain: "exit 1".into(),
                tone: wire::CardTone::Error,
                display: None,
            },
            vec![SessionUpdate::ToolCall(
                ToolCall::new(ToolCallId::new("rho-notice-n-1"), "Job failed")
                    .kind(ToolKind::Other)
                    .status(ToolCallStatus::Failed)
                    .content(vec![ToolCallContent::from("**exit 1**")])
                    .raw_output(json!("exit 1")),
            )],
        ),
        (
            "card without a body",
            plain,
            SessionEvent::Card {
                title: "Job done".into(),
                markdown: String::new(),
                plain: "  ".into(),
                tone: wire::CardTone::Success,
                display: None,
            },
            vec![SessionUpdate::ToolCall(
                ToolCall::new(ToolCallId::new("rho-notice-n-1"), "Job done")
                    .kind(ToolKind::Other)
                    .status(ToolCallStatus::Completed),
            )],
        ),
        (
            "notice to a host with notices",
            rich,
            SessionEvent::Notice {
                text: "plan approved".into(),
            },
            vec![SessionUpdate::Notice(Notice::new(
                NoticeSeverity::Info,
                "plan approved",
            ))],
        ),
        (
            "notice to a host without notices",
            plain,
            SessionEvent::Notice {
                text: "plan approved".into(),
            },
            vec![SessionUpdate::AgentMessageChunk(message(
                "rho-msg-n-1",
                "plan approved",
            ))],
        ),
        (
            "state is the pump's",
            rich,
            SessionEvent::State {
                state: wire::JobState::Running,
                turn: Some(wire::TurnId::new("t1")),
            },
            vec![],
        ),
        ("unknown event", rich, SessionEvent::Unknown, vec![]),
    ];
    for (label, features, event, expected) in cases {
        assert_eq!(updates(features).map(&event), expected, "{label}");
    }
}

// Covers: the first report of a tool call opens its card, defaulting what it
// leaves out; later reports patch only the fields they carry, so a status
// change does not blank the host's title or content
// Owner: ACP event mapping
#[test]
fn tool_calls_open_a_card_then_patch_it() {
    let mut mapping = updates(HostFeatures::default());
    let started = wire::ToolCallUpdate {
        title: Some("Edit src/lib.rs".into()),
        kind: Some(wire::ToolKind::Edit),
        raw_input: Some(json!({"path": "src/lib.rs"})),
        locations: Some(vec![wire::Location {
            path: "/w/src/lib.rs".into(),
            line: Some(0),
        }]),
        ..tool_update("c1")
    };
    let finished = wire::ToolCallUpdate {
        status: Some(wire::ToolStatus::Completed),
        content: Some(vec![
            wire::ToolContent::Diff {
                path: "/w/src/lib.rs".into(),
                old_text: Some("a".into()),
                new_text: "b".into(),
                deleted: false,
            },
            wire::ToolContent::Diff {
                path: "/w/src/gone.rs".into(),
                old_text: Some("c".into()),
                new_text: String::new(),
                deleted: true,
            },
            wire::ToolContent::Unknown,
        ]),
        ..tool_update("c1")
    };
    let unnamed = wire::ToolCallUpdate {
        kind: Some(wire::ToolKind::Unknown),
        status: Some(wire::ToolStatus::Unknown),
        ..tool_update("c2")
    };

    let mapped = [started, finished, unnamed]
        .into_iter()
        .flat_map(|update| mapping.map(&SessionEvent::tool_call(update)))
        .collect::<Vec<_>>();

    assert_eq!(
        mapped,
        [
            SessionUpdate::ToolCall(
                ToolCall::new(ToolCallId::new("c1"), "Edit src/lib.rs")
                    .kind(ToolKind::Edit)
                    .status(ToolCallStatus::Pending)
                    .raw_input(json!({"path": "src/lib.rs"}))
                    .locations(vec![ToolCallLocation::new("/w/src/lib.rs").line(1_u32)]),
            ),
            SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(
                ToolCallId::new("c1"),
                ToolCallUpdateFields::new()
                    .status(ToolCallStatus::Completed)
                    .content(vec![
                        ToolCallContent::Diff(
                            Diff::new("/w/src/lib.rs", "b").old_text("a".to_owned()),
                        ),
                        // v1 cannot say a file is gone; `_meta` does.
                        ToolCallContent::Diff(
                            Diff::new("/w/src/gone.rs", "")
                                .old_text("c".to_owned())
                                .meta(serde_json::Map::from_iter([(
                                    "rho/fileDeleted".to_owned(),
                                    json!(true),
                                )])),
                        ),
                    ]),
            )),
            SessionUpdate::ToolCall(
                ToolCall::new(ToolCallId::new("c2"), "tool")
                    .kind(ToolKind::Other)
                    .status(ToolCallStatus::InProgress),
            ),
        ]
    );
}

fn session_options(mode: wire::PermissionMode, fast: bool) -> wire::SessionOptions {
    let current = wire::ModelRef {
        provider: "openai".into(),
        model: "gpt-5".into(),
    };
    wire::SessionOptions {
        model: wire::ModelOptions {
            auth: "api-key".into(),
            current: current.clone(),
            available: vec![wire::ModelChoice {
                model: current,
                label: "GPT-5".into(),
            }],
        },
        reasoning: None,
        fast_mode: Some(wire::FastModeOptions { enabled: fast }),
        permission_mode: wire::PermissionModeOptions {
            current: mode,
            available: vec![wire::PermissionMode::Plan, wire::PermissionMode::Auto],
        },
        commands: Vec::new(),
        added_dirs: Vec::new(),
    }
}

// Covers: an options change tells the host the whole config when an option
// moved, the mode when it switched, and the menu when commands changed, and
// nothing at all when nothing did, so repeated changes do not spam hosts
// Owner: ACP event mapping
#[test]
fn options_changes_send_only_what_moved() {
    let features = HostFeatures::default();
    let plan = session_options(wire::PermissionMode::Plan, /*fast*/ false);
    let auto = session_options(wire::PermissionMode::Auto, /*fast*/ false);
    let fast = session_options(wire::PermissionMode::Plan, /*fast*/ true);
    let mut with_command = plan.clone();
    with_command.commands.push(wire::SessionCommand {
        name: "init".into(),
        kind: wire::CommandKind::Slash,
        description: "Write AGENTS.md".into(),
        input_hint: None,
    });
    let config = |options: &wire::SessionOptions| {
        SessionUpdate::ConfigOptionUpdate(ConfigOptionUpdate::new(options::config_options(
            options,
            features.toggles,
        )))
    };
    let cases = [
        ("nothing", &plan, vec![]),
        (
            "mode",
            &auto,
            vec![
                config(&auto),
                SessionUpdate::CurrentModeUpdate(CurrentModeUpdate::new(SessionModeId::new(
                    "auto",
                ))),
            ],
        ),
        ("fast mode", &fast, vec![config(&fast)]),
        (
            "commands",
            &with_command,
            vec![SessionUpdate::AvailableCommandsUpdate(
                AvailableCommandsUpdate::new(vec![AvailableCommand::new(
                    "init",
                    "Write AGENTS.md",
                )]),
            )],
        ),
    ];
    for (label, new, expected) in cases {
        assert_eq!(options_changed(&plan, new, features), expected, "{label}");
    }
}

// Covers: replayed history carries `_meta.isReplay` so hosts with their own
// transcript can skip it; live updates carry no marker
// Owner: ACP event mapping
#[test]
fn replayed_updates_are_marked() {
    let update = || SessionUpdate::AgentMessageChunk(message("m1", "hi"));
    let session = SessionId::new("s1");
    let meta = |delivery| {
        serde_json::to_value(notification(&session, update(), delivery)).unwrap()["_meta"].clone()
    };
    assert_eq!(
        [meta(Delivery::Replay), meta(Delivery::Live)],
        [json!({"isReplay": true}), json!(null)]
    );
}