rho-coding-agent 2.33.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
493
494
495
496
497
498
499
500
501
502
503
//! Foreground ACP driver. All terminal writes remain in session::settle.

use super::{
    handshake::{self, error, AgentFeatures, HANDSHAKE_STEP_BUDGET},
    permission::{choose_option, is_allowed},
    policy::AcpAgentPolicy,
    steering,
    turn::{classify_stop, TurnClassification},
    ExtensionAnswer,
};
use crate::cli_runtime::{
    agent_event::{render::EventRenderer, AgentEvent},
    parent_messages::{frame_parent_message, ParentMessageInbox},
    status_sink::StatusSink,
    stream_effect::{StatusPatch, StreamEffect},
};
use crate::{
    presentation::{parent_message_card, NotificationDelivery},
    run_artifacts::AttachmentEvent,
};
use agent_client_protocol::util::MatchDispatch;
use agent_client_protocol::{
    schema::v1::{
        CancelNotification, NewSessionRequest, RequestPermissionOutcome, RequestPermissionRequest,
        RequestPermissionResponse, SessionNotification, SessionUpdate, ToolCallContent, ToolCallId,
        ToolCallStatus,
    },
    ActiveSession, Agent, Client, ConnectTo, Dispatch, Error, Handled, Responder, SessionMessage,
    UntypedMessage,
};
use rho_tools::cancellation::RunCancellation;
use serde_json::Value;
use std::{
    collections::HashSet,
    future::Future,
    path::Path,
    pin::Pin,
    sync::{Arc, Mutex},
    time::Duration,
};
use tokio::sync::{mpsc, oneshot};

// Receipt: OwnedChild's 200 ms termination grace. A cooperative cancel was
// observed at ~4 ms in the spike; share the existing shutdown budget before
// forcing termination, rather than leave a hung agent immortal after cancel.
pub(super) const CANCEL_DRAIN_BUDGET: Duration = Duration::from_millis(200);

pub(super) struct DriverOutcome {
    pub(super) classification: TurnClassification,
    pub(super) session_id: Option<String>,
    pub(super) result_text: String,
    pub(super) turns: u64,
}

impl DriverOutcome {
    fn stopped(renderer: &EventRenderer, session_id: Option<String>) -> Self {
        Self {
            classification: TurnClassification::Stopped,
            session_id,
            result_text: renderer.result_text().to_owned(),
            turns: renderer.turns(),
        }
    }
}

pub(super) fn render(renderer: &mut EventRenderer, sink: &mut StatusSink, event: AgentEvent) {
    for effect in renderer.render(event) {
        sink.apply_effect(effect);
    }
}

fn answer_extension<P: AcpAgentPolicy>(
    policy: &Mutex<P>,
    request: UntypedMessage,
    responder: Responder<serde_json::Value>,
    events: &mpsc::UnboundedSender<AgentEvent>,
) -> Result<(), Error> {
    let answer = policy
        .lock()
        .expect("ACP policy")
        .answer_extension(&request.method, &request.params);
    match answer {
        Some(ExtensionAnswer {
            reply,
            events: presentation,
        }) => {
            responder.respond(reply)?;
            for event in presentation {
                let _ = events.send(event);
            }
            Ok(())
        }
        None => responder.respond_with_error(Error::method_not_found()),
    }
}

pub(super) struct DriverContext<'a> {
    pub(super) prompt: &'a str,
    pub(super) cwd: &'a Path,
    pub(super) cancellation: &'a RunCancellation,
    pub(super) inbox: &'a mut Option<ParentMessageInbox>,
    pub(super) renderer: &'a mut EventRenderer,
    pub(super) sink: &'a mut StatusSink,
}

pub(super) async fn run<P: AcpAgentPolicy>(
    transport: impl ConnectTo<Client> + 'static,
    policy: P,
    context: DriverContext<'_>,
) -> Result<DriverOutcome, String> {
    let DriverContext {
        prompt,
        cwd,
        cancellation,
        inbox,
        renderer,
        sink,
    } = context;
    // Reborrow so a post-connection cancel outcome can still render.
    let (session_renderer, session_sink) = (&mut *renderer, &mut *sink);
    let policy = Arc::new(Mutex::new(policy));
    let extension_policy = Arc::clone(&policy);
    let (events_tx, mut events_rx) = mpsc::unbounded_channel();
    let extension_tx = events_tx.clone();
    let outcome = Client.builder()
        .name("rho")
        .on_receive_request(async move |request: UntypedMessage, responder, _cx| {
            // Static handlers run BEFORE ActiveSession's dynamic handler.
            // Decline all session-scoped requests so permissions and scoped
            // extensions arrive through read_update, never this catch-all.
            if request.params.get("sessionId").and_then(serde_json::Value::as_str).is_some() {
                return Ok(Handled::No { message: (request, responder), retry: false });
            }
            answer_extension(&extension_policy, request, responder, &extension_tx)?;
            Ok(Handled::Yes)
        }, agent_client_protocol::on_receive_request!())
        .connect_with(transport, async move |cx| {
            let (renderer, sink) = (session_renderer, session_sink);
            let features = tokio::select! {
                biased;
                _ = cancellation.cancelled() => return Ok(DriverOutcome::stopped(renderer, None)),
                result = handshake::initialize(&cx, &policy) => result?,
            };
            let (started_tx, started_rx) = oneshot::channel();
            let started = std::sync::atomic::AtomicBool::new(false);
            // run_until preserves session/new's actual JSON-RPC error (unlike
            // start_session, which starts a background request task). Bound
            // only startup: after the closure starts, turns have no deadline.
            let session_meta = policy.lock().expect("ACP policy").session_meta();
            let session_run = cx.build_session_from(NewSessionRequest::new(cwd.to_path_buf()).meta(session_meta))
                .block_task().run_until(async |session| {
                    started.store(true, std::sync::atomic::Ordering::Relaxed);
                    let _ = started_tx.send(());
                    sink.apply_effect(StreamEffect::Status(StatusPatch {
                        claude_session_id: Some(session.session_id().to_string()),
                        ..StatusPatch::default()
                    }));
                    tokio::select! {
                        biased;
                        _ = cancellation.cancelled() => return Ok(DriverOutcome::stopped(renderer, Some(session.session_id().to_string()))),
                        result = handshake::require_mode(&session, &policy) => result?,
                    }
                    tokio::select! {
                        biased;
                        _ = cancellation.cancelled() => return Ok(DriverOutcome::stopped(renderer, Some(session.session_id().to_string()))),
                        result = handshake::require_config(&session, &policy) => result?,
                    }
                    turns(session, &policy, features, DriverContext { prompt, cwd, cancellation, inbox, renderer, sink }, &events_tx, &mut events_rx).await
                });
            tokio::pin!(session_run);
            tokio::select! {
                biased;
                started_result = tokio::time::timeout(HANDSHAKE_STEP_BUDGET, started_rx) => {
                    match started_result {
                        Ok(Ok(())) => session_run.await,
                        Ok(Err(_)) => session_run.await.map_err(|source| error(format!("acp: session/new: {source}"))),
                        Err(_) => Err(error("acp: session/new exceeded handshake step budget 10 s")),
                    }
                }
                _ = cancellation.cancelled() => Ok(DriverOutcome { classification: TurnClassification::Stopped, session_id: None, result_text: String::new(), turns: 0 }),
                result = &mut session_run => result.map_err(|source| if started.load(std::sync::atomic::Ordering::Relaxed) { source } else { error(format!("acp: session/new: {source}")) }),
            }
        }).await;
    match outcome {
        Ok(outcome) => Ok(outcome),
        // Local cancellation is authoritative on every path, not only when a
        // stop reason arrives: an agent that closes stdio, or a failed
        // session/cancel send, still settles as Stopped. Keep the diagnostic.
        Err(source) if cancellation.is_cancelled() => {
            render(
                renderer,
                sink,
                AgentEvent::Notice(format!("acp: after cancellation: {source}")),
            );
            Ok(DriverOutcome::stopped(renderer, None))
        }
        Err(source) => Err(format!("acp: {source}")),
    }
}

/// How long a turn that has ended waits on a steer still in flight. Same
/// 10 s as each ACP setup request; claude-agent-acp answers a steer after one
/// queue check, without waiting on the model.
const STEER_REPLY_BUDGET: std::time::Duration = HANDSHAKE_STEP_BUDGET;

type SteerReply = Pin<Box<dyn Future<Output = Result<Value, Error>> + Send>>;

/// Steering for one session. It takes one parent message at a time from the
/// inbox while a turn runs. A message the agent does not inject waits for the
/// next turn, ahead of anything still queued, and pauses steering to keep the
/// order.
enum Steering {
    /// The agent does not steer, or the inbox closed.
    Off,
    /// Ready for the next parent message.
    Idle,
    /// A `_session/steering` request in flight, with the text it carries.
    InFlight(String, SteerReply),
    /// Declined while Rho's prompt was still running, so the agent had not
    /// queued its turn yet. Steered again on the turn's next output, or sent
    /// as the next turn if the turn ends first.
    Held(String),
    /// The steer failed; sent as the next turn.
    Declined(String),
}

impl Steering {
    fn take_in_flight(&mut self) -> Option<(String, SteerReply)> {
        match std::mem::replace(self, Self::Idle) {
            Self::InFlight(text, reply) => Some((text, reply)),
            other => {
                *self = other;
                None
            }
        }
    }

    fn take_held(&mut self) -> Option<String> {
        match std::mem::replace(self, Self::Idle) {
            Self::Held(text) => Some(text),
            other => {
                *self = other;
                None
            }
        }
    }

    fn take_declined(&mut self) -> Option<String> {
        match std::mem::replace(self, Self::Idle) {
            Self::Held(text) | Self::Declined(text) => Some(text),
            other => {
                *self = other;
                None
            }
        }
    }
}

async fn turns<P: AcpAgentPolicy>(
    mut session: ActiveSession<'_, Agent>,
    policy: &Mutex<P>,
    features: AgentFeatures,
    context: DriverContext<'_>,
    events_tx: &mpsc::UnboundedSender<AgentEvent>,
    events_rx: &mut mpsc::UnboundedReceiver<AgentEvent>,
) -> Result<DriverOutcome, Error> {
    let DriverContext {
        prompt,
        cancellation,
        inbox,
        renderer,
        sink,
        ..
    } = context;
    let session_id = session.session_id().to_string();
    sink.mark_running();
    session.send_prompt(prompt)?;
    let connection = session.connection().clone();
    let mut cancelled_by_us = false;
    let mut cancel_deadline = None;
    let mut tracking = UpdateTracking::default();
    let mut steering = if features.steering && inbox.is_some() {
        Steering::Idle
    } else {
        Steering::Off
    };
    loop {
        tokio::select! {
            biased;
            _ = cancellation.cancelled(), if !cancelled_by_us => {
                connection.send_notification(CancelNotification::new(session.session_id().clone()))?;
                cancelled_by_us = true;
                cancel_deadline = Some(tokio::time::Instant::now() + CANCEL_DRAIN_BUDGET);
                if let Some(inbox) = inbox.as_ref() { inbox.seal(); }
            }
            _ = async { match cancel_deadline { Some(deadline) => tokio::time::sleep_until(deadline).await, None => std::future::pending().await } } => {
                render(renderer, sink, AgentEvent::Notice("acp: session/cancel response exceeded shutdown grace budget 200 ms; terminating agent".into()));
                return Ok(DriverOutcome::stopped(renderer, Some(session_id)));
            }
            reply = async { match &mut steering { Steering::InFlight(_, reply) => reply.as_mut().await, _ => std::future::pending().await } }, if matches!(steering, Steering::InFlight(..)) => {
                if let Some((text, _)) = steering.take_in_flight() {
                    steering = settle_steer(text, reply, renderer, sink);
                    if matches!(steering, Steering::Held(_)) {
                        // Retry only after output that follows the decline.
                        tracking.turn_live = false;
                    }
                }
            }
            text = async { inbox.as_mut().expect("steering needs an inbox").recv().await }, if matches!(steering, Steering::Idle) && tracking.turn_live && !cancelled_by_us => match text {
                Some(text) => {
                    let request = steering::request(session.session_id(), &frame_parent_message(&text))?;
                    steering = Steering::InFlight(text, Box::pin(connection.send_request(request).block_task()));
                }
                None => steering = Steering::Off,
            },
            message = session.read_update() => match message? {
                SessionMessage::SessionMessage(dispatch) => {
                    dispatch_update(dispatch, policy, renderer, sink, &mut tracking, cancelled_by_us, events_tx).await?;
                    if tracking.turn_live && !cancelled_by_us {
                        if let Some(text) = steering.take_held() {
                            let request = steering::request(session.session_id(), &frame_parent_message(&text))?;
                            steering = Steering::InFlight(text, Box::pin(connection.send_request(request).block_task()));
                        }
                    }
                }
                SessionMessage::StopReason(stop) => {
                    let classification = classify_stop(&stop, cancelled_by_us);
                    // A steer racing the end of a successful turn is settled
                    // first, so a declined message still becomes the next
                    // turn. Any other ending sends no next turn, so it does not
                    // wait. A cancel during the wait stops the run instead.
                    let mut cancelled_while_settling = false;
                    if let Some((text, reply)) = steering.take_in_flight() {
                        if classification == TurnClassification::Success {
                            tokio::select! {
                                biased;
                                _ = cancellation.cancelled() => cancelled_while_settling = true,
                                reply = tokio::time::timeout(STEER_REPLY_BUDGET, reply) => {
                                    let reply = reply.unwrap_or_else(|_| Err(error(format!("{} reply exceeded budget {} s", steering::METHOD, STEER_REPLY_BUDGET.as_secs()))));
                                    steering = settle_steer(text, reply, renderer, sink);
                                }
                            }
                        }
                    }
                    while let Ok(event) = events_rx.try_recv() { render(renderer, sink, event); }
                    render(renderer, sink, AgentEvent::TurnEnded(stop));
                    if cancelled_while_settling {
                        if let Some(inbox) = inbox.as_ref() { inbox.seal(); }
                        return Ok(DriverOutcome::stopped(renderer, Some(session_id)));
                    }
                    // classify_stop already reports Stopped once we cancelled.
                    if classification == TurnClassification::Success {
                        // Atomic with sends: a racing message either becomes
                        // the next turn (port stays open) or is rejected.
                        if let Some(text) = steering.take_declined().or_else(|| inbox.as_mut().and_then(ParentMessageInbox::take_next_or_close)) {
                            session.send_prompt(frame_parent_message(&text))?;
                            tracking.turn_live = false;
                            sink.apply_effect(StreamEffect::Attachment(AttachmentEvent::Message(Box::new(parent_message_card(text, NotificationDelivery::Queued, "sent as the next agent turn".into())))));
                            continue;
                        }
                    } else if steering.take_declined().is_some() {
                        // It was waiting for a next turn that will not come.
                        render(renderer, sink, AgentEvent::Notice("acp: a parent message was not delivered: the run ended before its next turn".into()));
                    }
                    if let Some(inbox) = inbox.as_ref() { inbox.seal(); }
                    return Ok(DriverOutcome { classification, session_id: Some(session_id), result_text: renderer.result_text().to_owned(), turns: renderer.turns() });
                }
                _ => return Err(error("acp: unhandled session message")),
            },
            Some(event) = events_rx.recv() => render(renderer, sink, event),
            _ = connection.incoming_closed() => return Err(error("acp: connection closed before session/prompt response")),
        }
    }
}

/// Journal an injected message, or keep it for the next turn.
fn settle_steer(
    text: String,
    reply: Result<Value, Error>,
    renderer: &mut EventRenderer,
    sink: &mut StatusSink,
) -> Steering {
    match reply.as_ref().map(steering::delivery) {
        Ok(steering::Delivery::Injected) => {
            sink.apply_effect(StreamEffect::Attachment(AttachmentEvent::Message(
                Box::new(parent_message_card(
                    text,
                    NotificationDelivery::Received,
                    "steered into the running turn".into(),
                )),
            )));
            Steering::Idle
        }
        Ok(steering::Delivery::NextTurn) => Steering::Held(text),
        Err(source) => {
            render(
                renderer,
                sink,
                AgentEvent::Notice(format!(
                    "acp: {}: {source}; sending the message as the next turn",
                    steering::METHOD
                )),
            );
            Steering::Declined(text)
        }
    }
}

/// What the driver keeps from one session's updates.
#[derive(Default)]
struct UpdateTracking {
    /// Ids only, for the session lifetime: duplicate completed snapshots must
    /// not turn a previously rejected tool back into a successful card.
    rejected: HashSet<ToolCallId>,
    /// The current prompt's turn has produced output since it was sent, or
    /// since the last too-early steer. An agent queues a turn only after its
    /// own setup, which can itself send updates (claude-agent-acp publishes an
    /// open task plan first), and declines a steer that arrives before with
    /// `promptRequired`; [`Steering::Held`] retries it.
    turn_live: bool,
}

/// Output only a running turn produces. Session-level updates (commands,
/// modes, config, session info) can arrive before the agent has queued it.
fn is_turn_content(update: &SessionUpdate) -> bool {
    match update {
        SessionUpdate::UserMessageChunk(_)
        | SessionUpdate::AgentMessageChunk(_)
        | SessionUpdate::AgentThoughtChunk(_)
        | SessionUpdate::ToolCall(_)
        | SessionUpdate::ToolCallUpdate(_)
        | SessionUpdate::Plan(_)
        | SessionUpdate::UsageUpdate(_) => true,
        SessionUpdate::AvailableCommandsUpdate(_)
        | SessionUpdate::CurrentModeUpdate(_)
        | SessionUpdate::ConfigOptionUpdate(_)
        | SessionUpdate::SessionInfoUpdate(_) => false,
        _ => false,
    }
}

async fn dispatch_update<P: AcpAgentPolicy>(
    dispatch: Dispatch,
    policy: &Mutex<P>,
    renderer: &mut EventRenderer,
    sink: &mut StatusSink,
    tracking: &mut UpdateTracking,
    cancelled_by_us: bool,
    events: &mpsc::UnboundedSender<AgentEvent>,
) -> Result<(), Error> {
    MatchDispatch::new(dispatch)
        .if_notification(async |notification: SessionNotification| {
            let (mut update, required_mode, status) = {
                let policy = policy.lock().expect("ACP policy");
                let update = policy.normalize_update(notification.update);
                let status = policy.status_effects(&update);
                (update, policy.session_mode(), status)
            };
            for effect in status {
                sink.apply_effect(effect);
            }
            handshake::check_mode_update(&update, required_mode.as_ref())?;
            tracking.turn_live |= is_turn_content(&update);
            if let SessionUpdate::ToolCallUpdate(call) = &mut update {
                if call.fields.status == Some(ToolCallStatus::Completed)
                    && tracking.rejected.contains(&call.tool_call_id)
                {
                    call.fields.status = Some(ToolCallStatus::Failed);
                    call.fields.content = Some(vec![ToolCallContent::from(
                        "rejected by Rho permission policy",
                    )]);
                }
            }
            render(renderer, sink, AgentEvent::from(update));
            Ok(())
        })
        .await
        .if_request(async |request: RequestPermissionRequest, responder| {
            let outcome = if cancelled_by_us {
                RequestPermissionOutcome::Cancelled
            } else {
                choose_option(
                    policy
                        .lock()
                        .expect("ACP policy")
                        .decide_permission(&request),
                    &request.options,
                )
            };
            if !is_allowed(&outcome, &request.options) {
                tracking
                    .rejected
                    .insert(request.tool_call.tool_call_id.clone());
            }
            responder.respond(RequestPermissionResponse::new(outcome))
        })
        .await
        .if_request(async |request: UntypedMessage, responder| {
            answer_extension(policy, request, responder, events)
        })
        .await
        .otherwise_ignore()
}