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
//! Questions put to the user over ACP: form elicitation for tools that ask,
//! and the plan approval prompt that leaves plan mode.

use std::{
    future::Future,
    pin::Pin,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Mutex,
    },
};

use agent_client_protocol::{
    schema::v1::{
        ContentBlock as AcpContentBlock, CreateElicitationRequest, CreateElicitationResponse,
        ElicitationAcceptAction, ElicitationAction, ElicitationContentValue, PromptRequest,
        RequestPermissionOutcome, RequestPermissionRequest, RequestPermissionResponse,
        SelectedPermissionOutcome, SessionId, SessionModeId, SessionNotification, SessionUpdate,
        StopReason,
    },
    Error as AcpError,
};
use pretty_assertions::assert_eq;
use rho_sdk::{
    model::{ContentBlock, Message, ModelIdentity, ModelResponse, ToolCall, ToolResult, ToolSpec},
    provider::{ModelProvider, ScriptedProvider, ScriptedTurn},
    tool::{Tool, ToolContext, ToolError, ToolErrorKind, ToolFuture, ToolInvocation, ToolOutput},
    HostChoice, HostInputRequest, HostQuestion, SelectionMode, SessionOptions,
};

use super::{mode::SessionPermission, PromptGate, SessionHost};
use crate::app::acp::host_updates::{FormDialect, Forms};
use crate::{
    app::{acp::AcpStartup, session_assembly::BuiltSession},
    config::Config,
    diagnostics::RuntimeDiagnostics,
    permission::PermissionMode,
    session::Session as StoredSession,
    tools::{
        plan_exit::PlanExitHost,
        sdk_registry::{AppToolSet, StaticToolBundle},
    },
};

const ASK: &str = "ask_color";

/// Asks one question through the run's host input and reports the answer.
struct AskColor;

impl Tool for AskColor {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: ASK.into(),
            description: "test tool".into(),
            input_schema: serde_json::json!({"type": "object"}),
        }
    }

    fn call<'a>(&'a self, _invocation: ToolInvocation, context: ToolContext) -> ToolFuture<'a> {
        Box::pin(async move {
            let question = HostQuestion::new(
                "color",
                "Which color?",
                vec![
                    HostChoice::new("red", "Red"),
                    HostChoice::new("blue", "Blue"),
                ],
                SelectionMode::One,
            )
            .expect("valid question");
            let request =
                HostInputRequest::questionnaire("Pick", vec![question]).expect("valid request");
            let response =
                context
                    .request_host_input(request)
                    .await
                    .map_err(|error| match error {
                        rho_sdk::Error::Cancelled => ToolError::cancelled(),
                        error => ToolError::new(ToolErrorKind::Execution, error.to_string()),
                    })?;
            Ok(ToolOutput::text(response.answers()["color"].join(",")))
        })
    }
}

/// How the fake host answers `elicitation/create`.
enum FormReply {
    Respond(ElicitationAction),
    /// Never answers; reports when it is asked and when the request is
    /// withdrawn (its future dropped).
    Hold {
        asked: tokio::sync::mpsc::UnboundedSender<()>,
        withdrawn: Arc<AtomicBool>,
    },
}

/// Host that answers questions from a script, picks `plan_answer` at every
/// plan approval, and records every session update.
struct AskingPort {
    form: FormReply,
    plan_answer: &'static str,
    /// Cancels this prompt at the run's final usage update, sent after the
    /// run settled: the second after the plan prompt, which follows the
    /// proposing step's start.
    cancel_at_final_usage: Option<Arc<PromptGate>>,
    usage_after_plan_prompt: Mutex<usize>,
    updates: Mutex<Vec<SessionUpdate>>,
    plan_prompts: Mutex<usize>,
}

impl AskingPort {
    fn new(form: FormReply) -> Self {
        Self {
            form,
            plan_answer: "keep_planning",
            cancel_at_final_usage: None,
            usage_after_plan_prompt: Mutex::default(),
            updates: Mutex::default(),
            plan_prompts: Mutex::default(),
        }
    }

    fn mode_updates(&self) -> Vec<SessionModeId> {
        self.updates
            .lock()
            .unwrap()
            .iter()
            .filter_map(|update| match update {
                SessionUpdate::CurrentModeUpdate(update) => Some(update.current_mode_id.clone()),
                _ => None,
            })
            .collect()
    }
}

struct DropFlag(Arc<AtomicBool>);

impl Drop for DropFlag {
    fn drop(&mut self) {
        self.0.store(true, Ordering::Release);
    }
}

impl super::AcpClientPort for AskingPort {
    fn send_session_notification(
        &self,
        notification: SessionNotification,
    ) -> Pin<Box<dyn Future<Output = Result<(), AcpError>> + Send + '_>> {
        if let (Some(gate), SessionUpdate::UsageUpdate(_)) =
            (&self.cancel_at_final_usage, &notification.update)
        {
            if *self.plan_prompts.lock().unwrap() > 0 {
                let mut seen = self.usage_after_plan_prompt.lock().unwrap();
                *seen += 1;
                if *seen == 2 {
                    gate.cancel();
                }
            }
        }
        self.updates.lock().unwrap().push(notification.update);
        Box::pin(async { Ok(()) })
    }

    fn request_permission(
        &self,
        _request: RequestPermissionRequest,
    ) -> Pin<Box<dyn Future<Output = Result<RequestPermissionResponse, AcpError>> + Send + '_>>
    {
        *self.plan_prompts.lock().unwrap() += 1;
        let answer = self.plan_answer;
        Box::pin(async move {
            Ok(RequestPermissionResponse::new(
                RequestPermissionOutcome::Selected(SelectedPermissionOutcome::new(answer)),
            ))
        })
    }

    fn create_elicitation(
        &self,
        _request: CreateElicitationRequest,
    ) -> Pin<Box<dyn Future<Output = Result<CreateElicitationResponse, AcpError>> + Send + '_>>
    {
        match &self.form {
            FormReply::Respond(action) => {
                let response = CreateElicitationResponse::new(action.clone());
                Box::pin(async move { Ok(response) })
            }
            FormReply::Hold { asked, withdrawn } => {
                let _ = asked.send(());
                let flag = DropFlag(Arc::clone(withdrawn));
                Box::pin(async move {
                    let _flag = flag;
                    std::future::pending().await
                })
            }
        }
    }
}

fn call(id: &str, name: &str, arguments: serde_json::Value) -> ScriptedTurn {
    ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::ToolCall(
        ToolCall {
            id: id.into(),
            name: name.into(),
            arguments,
        },
    )]))
}

fn text(text: &str) -> ScriptedTurn {
    ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::Text(
        text.into(),
    )]))
}

/// A session host on `turns`, in `mode`, for a host that does or does not
/// draw forms.
const STANDARD_FORMS: Forms = Forms::Available(FormDialect::Standard);

async fn host(
    home: &std::path::Path,
    mode: PermissionMode,
    forms: Forms,
    turns: Vec<ScriptedTurn>,
) -> (SessionHost, AcpStartup, Arc<ScriptedProvider>) {
    let config = Config {
        provider: "test".into(),
        model: "model".into(),
        permission_mode: mode,
        ..Config::default()
    };
    let scripted = Arc::new(ScriptedProvider::new(
        ModelIdentity::new("test", "test", "model"),
        turns,
    ));
    let provider: Arc<dyn ModelProvider> = scripted.clone();
    let mut tools = AppToolSet::disabled();
    tools.add_bundle(StaticToolBundle::new(vec![Arc::new(AskColor)]));
    let tools = tools.with_plan_exit_host(&config, PlanExitHost::Permission);
    let runtime = rho_sdk::Rho::builder()
        .provider_shared(Arc::clone(&provider))
        .build()
        .unwrap();
    let stored = StoredSession::create_in_root(home, home).unwrap();
    let session = runtime
        .session(SessionOptions::new().id(rho_sdk::SessionId::from_string(stored.id()).unwrap()))
        .await
        .unwrap();
    let built = BuiltSession {
        runtime,
        session,
        provider,
        tools,
        hooks: None,
        approval_receiver: None,
        prompt_template: None,
        prompt: Default::default(),
        diagnostics: RuntimeDiagnostics::new(&config),
    };
    let mut startup = AcpStartup::for_tests(config.clone(), home);
    startup.host_updates.forms = forms;
    // Switching into `mode` builds the runtime with the tools, the way
    // `session/set_mode` does, so the host starts in another mode.
    let initial = if mode == PermissionMode::Plan {
        PermissionMode::Bypass
    } else {
        PermissionMode::Plan
    };
    let mut host = SessionHost::from_built(
        SessionId::new(stored.id()),
        built,
        super::persist::HostStorage::saved(stored),
        Default::default(),
        SessionPermission {
            mode: initial,
            writes: Default::default(),
            workspace_root: home.to_path_buf(),
        },
        &startup,
        Vec::new(),
    );
    host.set_permission_mode(mode, &config).await.unwrap();
    (host, startup, scripted)
}

fn prompt(host: &SessionHost, text: &str) -> PromptRequest {
    PromptRequest::new(
        host.acp_session_id.clone(),
        vec![AcpContentBlock::from(text)],
    )
}

/// The result the model received for tool call `id`.
fn tool_result(provider: &ScriptedProvider, id: &str) -> Option<ToolResult> {
    provider
        .recorded_requests()
        .last()?
        .messages
        .iter()
        .find_map(|message| match message {
            Message::ToolResult(result) if result.id == id => Some(result.clone()),
            _ => None,
        })
}

fn accept(content: &[(&str, &str)]) -> ElicitationAction {
    ElicitationAction::Accept(
        ElicitationAcceptAction::new().content(
            content
                .iter()
                .map(|(key, value)| ((*key).to_owned(), ElicitationContentValue::from(*value)))
                .collect::<std::collections::BTreeMap<_, _>>(),
        ),
    )
}

// Covers: a tool's question reaches a form-capable host as elicitation; the
// accepted answer reaches the tool, and decline or cancel fail only that call
// so the model carries on. A host without forms refuses without asking.
// Owner: ACP session host questions
#[tokio::test]
async fn tool_questions_follow_the_hosts_answer() {
    let cases = [
        (
            "accept",
            STANDARD_FORMS,
            accept(&[("color", "blue")]),
            ToolResult {
                id: "ask".into(),
                ok: true,
                content: "blue".into(),
            },
        ),
        (
            "decline",
            STANDARD_FORMS,
            ElicitationAction::Decline,
            ToolResult {
                id: "ask".into(),
                ok: false,
                content: "the user declined to answer".into(),
            },
        ),
        (
            "cancel",
            STANDARD_FORMS,
            ElicitationAction::Cancel,
            ToolResult {
                id: "ask".into(),
                ok: false,
                content: "the user dismissed the question without answering".into(),
            },
        ),
        (
            "no forms",
            Forms::Unavailable,
            accept(&[("color", "blue")]),
            ToolResult {
                id: "ask".into(),
                ok: false,
                content: "the user declined to answer".into(),
            },
        ),
    ];
    for (label, forms, action, expected) in cases {
        let home = tempfile::tempdir().unwrap();
        let turns = vec![call("ask", ASK, serde_json::json!({})), text("done")];
        let (mut host, startup, provider) =
            host(home.path(), PermissionMode::Bypass, forms, turns).await;
        let port = AskingPort::new(FormReply::Respond(action));

        let response = host
            .prompt(prompt(&host, "ask"), &port, &startup)
            .await
            .unwrap();

        assert_eq!(response.stop_reason, StopReason::EndTurn, "{label}");
        assert_eq!(tool_result(&provider, "ask"), Some(expected), "{label}");
        host.shutdown().await;
    }
}

// Covers: cancelling the prompt while a question is open withdraws the form
// from the host and ends the prompt cancelled instead of waiting on the user.
// Owner: ACP session host questions
#[tokio::test]
async fn cancelling_the_prompt_withdraws_an_open_question() {
    let home = tempfile::tempdir().unwrap();
    let turns = vec![call("ask", ASK, serde_json::json!({}))];
    let (mut host, startup, _provider) =
        host(home.path(), PermissionMode::Bypass, STANDARD_FORMS, turns).await;
    let (asked_tx, mut asked) = tokio::sync::mpsc::unbounded_channel();
    let withdrawn = Arc::new(AtomicBool::new(false));
    let port = AskingPort::new(FormReply::Hold {
        asked: asked_tx,
        withdrawn: Arc::clone(&withdrawn),
    });
    let gate = host.cancel_handle();
    let request = prompt(&host, "ask");

    let (response, ()) = tokio::join!(host.prompt(request, &port, &startup), async {
        asked.recv().await.expect("the host is asked");
        gate.cancel();
    });

    assert_eq!(
        (
            response.unwrap().stop_reason,
            withdrawn.load(Ordering::Acquire)
        ),
        (StopReason::Cancelled, true)
    );
    host.shutdown().await;
}

// Covers: keeping planning at the approval prompt must leave the session in
// plan mode and end the prompt without an implementation turn. Approval is
// covered over stdio in `acp_server_cli`.
// Owner: ACP plan exit
#[tokio::test]
async fn keeping_planning_stays_in_plan_mode_without_another_turn() {
    let home = tempfile::tempdir().unwrap();
    let turns = vec![
        call(
            "plan",
            "exit_plan_mode",
            serde_json::json!({"plan": "1. do it"}),
        ),
        text("proposed"),
        text("implemented"),
    ];
    let (mut host, startup, provider) =
        host(home.path(), PermissionMode::Plan, Forms::Unavailable, turns).await;
    let port = AskingPort::new(FormReply::Respond(ElicitationAction::Decline));

    let response = host
        .prompt(prompt(&host, "plan it"), &port, &startup)
        .await
        .unwrap();

    assert_eq!(
        (
            response.stop_reason,
            *port.plan_prompts.lock().unwrap(),
            host.permission.mode,
            port.mode_updates(),
            provider.recorded_requests().len(),
        ),
        (StopReason::EndTurn, 1, PermissionMode::Plan, Vec::new(), 2)
    );
    host.shutdown().await;
}

// Covers: a cancel that lands after the proposing turn settled but before the
// plan handoff (here, during its final usage update) discards the approval:
// the session stays in plan mode, no mode update is sent, the implementation
// turn never runs, and the prompt ends cancelled
// Owner: ACP plan exit
#[tokio::test]
async fn a_cancel_before_the_handoff_keeps_plan_mode() {
    let home = tempfile::tempdir().unwrap();
    let turns = vec![
        call(
            "plan",
            "exit_plan_mode",
            serde_json::json!({"plan": "1. do it"}),
        ),
        text("proposed"),
        text("implemented"),
    ];
    let (mut host, startup, provider) =
        host(home.path(), PermissionMode::Plan, Forms::Unavailable, turns).await;
    let mut port = AskingPort::new(FormReply::Respond(ElicitationAction::Decline));
    port.plan_answer = "allow_edits";
    port.cancel_at_final_usage = Some(host.cancel_handle());

    let response = host
        .prompt(prompt(&host, "plan it"), &port, &startup)
        .await
        .unwrap();

    assert_eq!(
        (
            response.stop_reason,
            host.permission.mode,
            port.mode_updates(),
            provider.recorded_requests().len(),
        ),
        (StopReason::Cancelled, PermissionMode::Plan, Vec::new(), 2)
    );
    host.shutdown().await;
}