rho-coding-agent 3.7.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
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
//! ACP v2 requests in the v1 form Rho's agent handles, and the v1 answers in
//! v2 form.
//!
//! Like [`super::updates`], shapes v2 only widened cross through
//! [`super::bridge`]; redesigned ones are mapped here.

use agent_client_protocol::Error as AcpError;
use agent_client_protocol::{schema::ProtocolVersion, v1::schema as v1, v2::schema as v2};

use rho_protocol::{BackgroundWork, SessionOptions};

use super::{background, bridge};
use crate::app::acp::{host_features::Toggles, options as acp_options, TurnEnd};

/// `initialize` as v1 would have negotiated it. v2 made notices, compaction
/// cards, and boolean config options baseline, so the v1 markers for them are
/// set.
pub(super) fn initialize_request(
    request: &v2::InitializeRequest,
) -> Result<v1::InitializeRequest, AcpError> {
    let capabilities = &request.capabilities;
    let auth = v1::AuthCapabilities::new().terminal(
        capabilities
            .auth
            .as_ref()
            .is_some_and(|auth| auth.terminal.is_some()),
    );
    let session = v1::ClientSessionCapabilities::new()
        .compaction(v1::CompactionCapabilities::new())
        .notices(v1::NoticeCapabilities::new())
        .config_options(
            v1::SessionConfigOptionsCapabilities::new()
                .boolean(v1::BooleanConfigOptionCapabilities::new()),
        );
    let elicitation = capabilities
        .elicitation
        .as_ref()
        .map(bridge::<_, v1::ElicitationCapabilities>)
        .transpose()?;
    Ok(v1::InitializeRequest::new(ProtocolVersion::V1)
        .client_capabilities(
            v1::ClientCapabilities::new()
                .auth(auth)
                .session(session)
                .elicitation(elicitation),
        )
        .client_info(bridge::<_, v1::Implementation>(&request.info)?))
}

/// The v1 `initialize` answer in v2 form. Rho serves every baseline session
/// method, so `session` is advertised; v1's `loadSession` and its
/// list/resume/close markers have no v2 counterpart.
pub(super) fn initialize_response(
    response: v1::InitializeResponse,
) -> Result<v2::InitializeResponse, AcpError> {
    let agent = response.agent_capabilities;
    let prompt = &agent.prompt_capabilities;
    let mut prompt_v2 = v2::PromptCapabilities::new();
    if prompt.image {
        prompt_v2 = prompt_v2.image(v2::PromptImageCapabilities::new());
    }
    if prompt.audio {
        prompt_v2 = prompt_v2.audio(v2::PromptAudioCapabilities::new());
    }
    if prompt.embedded_context {
        prompt_v2 = prompt_v2.embedded_context(v2::PromptEmbeddedContextCapabilities::new());
    }
    // v1 implied stdio MCP servers; v2 asks for the marker.
    let mut mcp = v2::McpCapabilities::new().stdio(v2::McpStdioCapabilities::new());
    if agent.mcp_capabilities.http {
        mcp = mcp.http(v2::McpHttpCapabilities::new());
    }
    let sessions = &agent.session_capabilities;
    let mut session = v2::SessionCapabilities::new().prompt(prompt_v2).mcp(mcp);
    if sessions.delete.is_some() {
        session = session.delete(v2::SessionDeleteCapabilities::new());
    }
    if sessions.additional_directories.is_some() {
        session =
            session.additional_directories(v2::SessionAdditionalDirectoriesCapabilities::new());
    }
    // v2 alone takes a host's system prompt: `system_prompt` below.
    session = session.system_prompt(
        v2::SessionSystemPromptCapabilities::new().mode(
            v2::SessionSystemPromptModeCapabilities::new()
                .override_(v2::SessionSystemPromptOverrideCapabilities::new()),
        ),
    );
    let info = match &response.agent_info {
        Some(info) => bridge(info)?,
        None => v2::Implementation::new("rho", env!("CARGO_PKG_VERSION")),
    };
    Ok(v2::InitializeResponse::new(ProtocolVersion::V2, info)
        .capabilities(v2::AgentCapabilities::new().session(session))
        .auth_methods(
            response
                .auth_methods
                .iter()
                .map(auth_method)
                .collect::<Result<_, _>>()?,
        ))
}

/// v2 renames `id` to `methodId` and lists terminal environment variables as
/// name/value entries.
fn auth_method(method: &v1::AuthMethod) -> Result<v2::AuthMethod, AcpError> {
    Ok(match method {
        v1::AuthMethod::Agent(agent) => v2::AuthMethod::Agent(
            v2::AuthMethodAgent::new(agent.id.0.clone(), agent.name.clone())
                .description(agent.description.clone())
                .meta(agent.meta.clone()),
        ),
        v1::AuthMethod::Terminal(terminal) => {
            let mut env: Vec<_> = terminal
                .env
                .iter()
                .map(|(name, value)| v2::EnvVariable::new(name.clone(), value.clone()))
                .collect();
            env.sort_by(|a, b| a.name.cmp(&b.name));
            v2::AuthMethod::Terminal(
                v2::AuthMethodTerminal::new(terminal.id.0.clone(), terminal.name.clone())
                    .description(terminal.description.clone())
                    .args(terminal.args.clone())
                    .env(env)
                    .meta(terminal.meta.clone()),
            )
        }
        _ => return Err(AcpError::internal_error().data("no ACP v2 form for this auth method")),
    })
}

/// The lifecycle environment shared by `session/new` and `session/resume`.
struct Environment {
    cwd: std::path::PathBuf,
    additional_directories: Vec<std::path::PathBuf>,
    mcp_servers: Vec<v1::McpServer>,
    system_prompt: Option<rho_protocol::SystemPrompt>,
}

/// Refuses what v2 forbids before the session exists: a `systemPrompt` Rho
/// can't apply, and MCP servers that share a `name`.
fn environment(
    cwd: &v2::AbsolutePath,
    additional_directories: &[v2::AbsolutePath],
    mcp_servers: &[v2::McpServer],
    system_prompt: Option<&v2::SystemPrompt>,
) -> Result<Environment, AcpError> {
    let system_prompt = system_prompt.map(self::system_prompt).transpose()?;
    let mut names = std::collections::HashSet::new();
    if let Some(name) = mcp_servers
        .iter()
        .filter_map(mcp_server_name)
        .find(|name| !names.insert(*name))
    {
        return Err(AcpError::invalid_params()
            .data(format!("more than one MCP server is named \"{name}\"")));
    }
    Ok(Environment {
        cwd: cwd.0.clone(),
        additional_directories: additional_directories
            .iter()
            .map(|path| path.0.clone())
            .collect(),
        mcp_servers: mcp_servers
            .iter()
            .map(mcp_server)
            .collect::<Result<_, _>>()?,
        system_prompt,
    })
}

/// The daemon's form of a host's `systemPrompt`: an advertised mode and at
/// least one text block, whose texts join as paragraphs. Anything else is
/// `-32602`, as v2 requires of a prompt the agent can't apply.
fn system_prompt(prompt: &v2::SystemPrompt) -> Result<rho_protocol::SystemPrompt, AcpError> {
    let mode = match &prompt.mode {
        v2::SystemPromptMode::Append => rho_protocol::SystemPromptMode::Append,
        v2::SystemPromptMode::Override => rho_protocol::SystemPromptMode::Override,
        _ => {
            return Err(AcpError::invalid_params()
                .data("Rho supports the systemPrompt modes append and override"))
        }
    };
    let texts = prompt
        .content
        .iter()
        .map(|block| match block {
            v2::ContentBlock::Text(text) => Ok(text.text.as_str()),
            _ => Err(AcpError::invalid_params()
                .data("Rho supports only text content blocks in systemPrompt")),
        })
        .collect::<Result<Vec<_>, _>>()?;
    let text = texts.join("\n\n");
    if text.trim().is_empty() {
        return Err(AcpError::invalid_params().data("systemPrompt needs text"));
    }
    Ok(rho_protocol::SystemPrompt::new(mode, text))
}

fn mcp_server_name(server: &v2::McpServer) -> Option<&str> {
    match server {
        v2::McpServer::Stdio(stdio) => Some(&stdio.name),
        v2::McpServer::Http(http) => Some(&http.name),
        // `mcp_server` refuses a transport this version doesn't know.
        _ => None,
    }
}

/// v2 tags stdio servers `type: "stdio"`, which v1 leaves untagged, and
/// requires their `command` to be an absolute path.
fn mcp_server(server: &v2::McpServer) -> Result<v1::McpServer, AcpError> {
    Ok(match server {
        v2::McpServer::Stdio(stdio) if !stdio.command.0.is_absolute() => {
            return Err(AcpError::invalid_params().data(format!(
                "the command of MCP server \"{}\" must be an absolute path",
                stdio.name
            )));
        }
        v2::McpServer::Stdio(stdio) => v1::McpServer::Stdio(
            v1::McpServerStdio::new(stdio.name.clone(), stdio.command.0.clone())
                .args(stdio.args.clone())
                .env(bridge(&stdio.env)?)
                .meta(stdio.meta.clone()),
        ),
        v2::McpServer::Http(http) => v1::McpServer::Http(
            v1::McpServerHttp::new(http.name.clone(), http.url.clone())
                .headers(bridge(&http.headers)?)
                .meta(http.meta.clone()),
        ),
        _ => {
            return Err(AcpError::invalid_params()
                .data("Rho supports stdio and http MCP servers over ACP v2"))
        }
    })
}

/// The v1 `session/new` and the host's system prompt, which v1's request
/// doesn't carry for Rho.
pub(super) fn new_session_request(
    request: &v2::NewSessionRequest,
) -> Result<(v1::NewSessionRequest, Option<rho_protocol::SystemPrompt>), AcpError> {
    let env = environment(
        &request.cwd,
        &request.additional_directories,
        &request.mcp_servers,
        request.system_prompt.as_ref(),
    )?;
    let v1 = v1::NewSessionRequest::new(env.cwd)
        .additional_directories(env.additional_directories)
        .mcp_servers(env.mcp_servers)
        .meta(request.meta.clone());
    Ok((v1, env.system_prompt))
}

/// How a v2 `session/resume` reattaches, by its replay cursor.
pub(super) enum Reattach {
    /// `replayFrom: start`: v1 `session/load`, which replays history.
    Replay(v1::LoadSessionRequest),
    /// No cursor: v1 `session/resume`; the client keeps its transcript.
    Resume(v1::ResumeSessionRequest),
}

/// The v1 reattach and the host's system prompt, which v1's requests don't
/// carry for Rho: none keeps the one the session saved.
pub(super) fn resume_session_request(
    request: &v2::ResumeSessionRequest,
) -> Result<(Reattach, Option<rho_protocol::SystemPrompt>), AcpError> {
    let env = environment(
        &request.cwd,
        &request.additional_directories,
        &request.mcp_servers,
        request.system_prompt.as_ref(),
    )?;
    let session_id = bridge::<_, v1::SessionId>(&request.session_id)?;
    let reattach = match &request.replay_from {
        None => Reattach::Resume(
            v1::ResumeSessionRequest::new(session_id, env.cwd)
                .additional_directories(env.additional_directories)
                .mcp_servers(env.mcp_servers)
                .meta(request.meta.clone()),
        ),
        Some(v2::ReplayFrom::Start(_)) => Reattach::Replay(
            v1::LoadSessionRequest::new(session_id, env.cwd)
                .additional_directories(env.additional_directories)
                .mcp_servers(env.mcp_servers)
                .meta(request.meta.clone()),
        ),
        Some(_) => {
            return Err(AcpError::invalid_params().data("Rho replays only from the start"));
        }
    };
    Ok((reattach, env.system_prompt))
}

/// The config options and commands a v2 session setup response carries. v1
/// sends the commands as a notification after the response; v2 puts them in
/// it.
pub(super) struct Setup {
    pub(super) config_options: Vec<v2::SessionConfigOption>,
    pub(super) available_commands: Vec<v2::AvailableCommand>,
}

/// The setup of a session with `options`, with on/off options drawn as
/// `toggles`.
pub(super) fn setup(options: &SessionOptions, toggles: Toggles) -> Result<Setup, AcpError> {
    Ok(Setup {
        config_options: super::updates::config_options(acp_options::config_options(
            options, toggles,
        ))?,
        available_commands: super::updates::available_commands(acp_options::available_commands(
            &options.commands,
        ))?,
    })
}

/// The v1 prompt, once the v2 one passes the checks v1 didn't make: at
/// least one content block, each of a type Rho advertised.
pub(super) fn prompt_request(request: &v2::PromptRequest) -> Result<v1::PromptRequest, AcpError> {
    if request.prompt.is_empty() {
        return Err(AcpError::invalid_params().data("a prompt needs at least one content block"));
    }
    let advertised = crate::app::acp::prompt_capabilities();
    if let Some(kind) = request
        .prompt
        .iter()
        .find_map(|block| unadvertised(block, &advertised))
    {
        return Err(AcpError::invalid_params().data(format!(
            "Rho does not accept prompt content of type \"{kind}\"; see capabilities.session.prompt"
        )));
    }
    Ok(v1::PromptRequest::new(
        bridge::<_, v1::SessionId>(&request.session_id)?,
        bridge(&request.prompt).map_err(|_| {
            AcpError::invalid_params().data("prompt content Rho does not understand")
        })?,
    )
    .meta(request.meta.clone()))
}

/// The type of `block` if `advertised` doesn't cover it. Text and resource
/// links are the baseline every agent accepts.
fn unadvertised(
    block: &v2::ContentBlock,
    advertised: &v1::PromptCapabilities,
) -> Option<&'static str> {
    let (kind, accepted) = match block {
        v2::ContentBlock::Text(_) => ("text", true),
        v2::ContentBlock::ResourceLink(_) => ("resource_link", true),
        v2::ContentBlock::Image(_) => ("image", advertised.image),
        v2::ContentBlock::Audio(_) => ("audio", advertised.audio),
        v2::ContentBlock::Resource(_) => ("resource", advertised.embedded_context),
        // A type this version doesn't know, or an unstable one such as
        // `skill`, which Rho doesn't advertise.
        _ => ("unknown", false),
    };
    (!accepted).then_some(kind)
}

pub(super) fn set_config_option_request(
    request: &v2::SetSessionConfigOptionRequest,
) -> Result<v1::SetSessionConfigOptionRequest, AcpError> {
    let value = match &request.value {
        v2::SessionConfigOptionValue::Id { value } => {
            v1::SessionConfigOptionValue::value_id(value.0.clone())
        }
        v2::SessionConfigOptionValue::Boolean { value } => {
            v1::SessionConfigOptionValue::boolean(*value)
        }
        _ => return Err(AcpError::invalid_params().data("unknown config option value type")),
    };
    Ok(v1::SetSessionConfigOptionRequest::new(
        bridge::<_, v1::SessionId>(&request.session_id)?,
        request.config_id.0.clone(),
        value,
    )
    .meta(request.meta.clone()))
}

/// v1 Rho puts the prompt's copy in the tool call's title; v2 gives it its own
/// `title` field, so the card keeps its real title. Everything else on the
/// v1 tool call is genuine card state and becomes the subject: for a plan
/// approval, the `exit_plan_mode` call's card, holding the plan.
pub(super) fn permission_request(
    request: v1::RequestPermissionRequest,
) -> Result<v2::RequestPermissionRequest, AcpError> {
    let v1::RequestPermissionRequest {
        session_id,
        mut tool_call,
        options,
        meta,
        ..
    } = request;
    let title = tool_call
        .fields
        .title
        .take()
        .unwrap_or_else(|| "Allow this tool call?".to_owned());
    let subject = v2::RequestPermissionSubject::ToolCall(Box::new(
        v2::ToolCallPermissionSubject::new(super::updates::tool_call_update(tool_call)?),
    ));
    Ok(v2::RequestPermissionRequest::new(
        bridge::<_, v2::SessionId>(&session_id)?,
        title,
        bridge(&options)?,
    )
    .subject(subject)
    .meta(meta))
}

/// The answer to a prompt that offered `offered`, in v1 form. v2 hosts
/// answer `cancelled` when the user dismisses the prompt or the host
/// declines it by policy, and may answer with an outcome this version
/// doesn't know. Neither approves, so either is taken as the prompt's
/// rejection: its last reject option, which is where Rho puts it ("Reject
/// once", or "Keep planning" after the modes a plan approval offers).
/// Rho always offers one; without one, the answer stays v1's `cancelled`.
/// v1's `cancelled` answers nothing, since a v1 host sends it only for a
/// turn already cancelled, which would leave the tool waiting here.
pub(super) fn permission_response(
    response: v2::RequestPermissionResponse,
    offered: &[v2::PermissionOption],
) -> v1::RequestPermissionResponse {
    let selected = match response.outcome {
        v2::RequestPermissionOutcome::Selected(selected) => Some(selected.option_id),
        _ => offered
            .iter()
            .rev()
            .find(|option| rejects(&option.kind))
            .map(|option| option.option_id.clone()),
    };
    let outcome = match selected {
        Some(option) => {
            v1::RequestPermissionOutcome::Selected(v1::SelectedPermissionOutcome::new(option.0))
        }
        None => v1::RequestPermissionOutcome::Cancelled,
    };
    v1::RequestPermissionResponse::new(outcome).meta(response.meta)
}

fn rejects(kind: &v2::PermissionOptionKind) -> bool {
    match kind {
        v2::PermissionOptionKind::RejectOnce | v2::PermissionOptionKind::RejectAlways => true,
        v2::PermissionOptionKind::AllowOnce | v2::PermissionOptionKind::AllowAlways => false,
        // Rho offers only the kinds it knows.
        _ => false,
    }
}

/// The `idle` that closes a turn that ended as `end`: with why it stopped,
/// `error` with the failure of one that failed after it started, and no
/// reason for one whose end went unseen or that v2 cannot name. It lists the
/// `background` work the turn left running.
pub(super) fn idle(end: &TurnEnd, background: &[BackgroundWork]) -> v2::SessionUpdate {
    background::idle_state(stop_reason(end), background)
}

/// Why foreground work stopped, as a turn's `end` says, when v2 can say it.
pub(super) fn stop_reason(end: &TurnEnd) -> Option<v2::StopReason> {
    match end {
        TurnEnd::Ended(Ok(reason)) => match reason {
            v1::StopReason::EndTurn => Some(v2::StopReason::EndTurn),
            v1::StopReason::MaxTokens => Some(v2::StopReason::MaxTokens),
            v1::StopReason::MaxTurnRequests => Some(v2::StopReason::MaxTurnRequests),
            v1::StopReason::Refusal => Some(v2::StopReason::Refusal),
            v1::StopReason::Cancelled => Some(v2::StopReason::Cancelled),
            _ => None,
        },
        TurnEnd::Ended(Err(error)) => Some(v2::StopReason::Error(
            v2::ErrorStopReason::new().error(bridge::<_, v2::Error>(error).ok()),
        )),
        TurnEnd::Unseen => None,
    }
}

#[cfg(test)]
#[path = "requests_tests.rs"]
mod tests;