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
//! How a headless prompt shows: the [`TurnView`] every headless host lends
//! [`SessionRuntime::run_prompt_turn`]. It writes the session's events as
//! the daemon protocol's, to `rho run`'s JSONL stream (#1669) or to the host
//! that follows a child run (#1668), and its run events to the status file,
//! and answers the turn's requests.
//! Nobody is there to approve, so an approval or plan that reaches it
//! anyway (an unattended session sends none) is refused. A delegated child
//! whose parent listens forwards its questions there; any other question
//! was declined by the turn already.

use std::{collections::HashMap, future::Future, pin::Pin};

use futures_util::{stream::FuturesUnordered, StreamExt};
use rho_protocol::{SessionEvent as WireEvent, TurnOutcome};
use rho_sdk::{
    CancellationToken, HostInputId, HostInputRefusal, HostInputRequest, HostInputResponse,
    UserInput,
};
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};

use super::RunReporter;
use crate::{
    app::{
        automation_protocol::JsonlStream,
        session_commands::{CommandSet, Hosting},
        session_events::LiveEvents,
        session_options,
        session_runtime::{
            cancelled_approval, PlanReport, SessionEvent, SessionRuntime, TurnControl, TurnIo,
            TurnView,
        },
        subagent_host_input::{ChildAsker, SubagentHostInputBridge},
        subagent_pool::{ChildEvent, ChildEventSender},
    },
    config::Config,
    tools::plan_exit::PlanExitDecision,
};

/// A child run's prompt is its parent's task, which runs no command.
const CHILD_COMMANDS: CommandSet = CommandSet {
    built_ins: &[],
    workflows: Hosting::Refused,
    agents: Hosting::Refused,
    sessions_label: "subagent runs",
};

/// Keeps the status file fresh while a provider or tool call emits no events.
const REPORT_HEARTBEAT: std::time::Duration = std::time::Duration::from_secs(10);

/// Where one headless prompt's events go.
pub(crate) struct HeadlessView<'a> {
    reporter: Option<&'a mut RunReporter>,
    wire: Option<WireView<'a>>,
    /// The parent that answers a delegated child's questions.
    parent: Option<ParentQuestions>,
}

impl<'a> HeadlessView<'a> {
    /// A view that shows nothing until told where to.
    pub(crate) fn new() -> Self {
        Self {
            reporter: None,
            wire: None,
            parent: None,
        }
    }

    /// Also keeps `reporter`'s status file and event log.
    pub(crate) fn reporting_to(mut self, reporter: &'a mut RunReporter) -> Self {
        self.reporter = Some(reporter);
        self
    }

    /// Also writes the session's events to `stream`.
    pub(crate) fn streaming_jsonl(mut self, stream: &'a mut JsonlStream) -> Self {
        self.wire = Some(WireView::to(WireSink::Jsonl(stream)));
        self
    }

    /// Also sends the session's events to `host`, which follows the run.
    pub(crate) fn streaming_to(mut self, host: ChildEventSender) -> Self {
        self.wire = Some(WireView::to(WireSink::Host(host)));
        self
    }

    /// Forwards the session's questions to `parent`.
    pub(crate) fn with_parent_questions(mut self, parent: Option<ParentQuestions>) -> Self {
        self.parent = parent;
        self
    }

    /// Names `runtime`'s session and tools in what the view writes, and
    /// tells a host that follows the run the options it opened with, which
    /// `process_config` gives their choices. [`super::run_prompt`] binds the
    /// view once the session is open, before its prompt begins; a prompt
    /// that rebinds the session's agent binds it again.
    pub(crate) fn bind(&mut self, runtime: &SessionRuntime, process_config: &Config) {
        if let Some(wire) = &mut self.wire {
            wire.bind(runtime, process_config);
        }
        if let Some(reporter) = self.reporter.as_deref_mut() {
            reporter.set_mcp_tools(runtime.tools().mcp_report().tool_names());
        }
    }

    /// Notes `input`, the display input of the turn about to begin, which
    /// the session's events show as the user message the turn saves.
    pub(crate) fn prompt(&mut self, input: &UserInput) {
        if let Some(wire) = &mut self.wire {
            wire.prompt = Some(input.clone());
        }
    }

    /// Ends the JSONL stream's turn with `outcome`, as the daemon does: the
    /// prompt of every session the stream opened ends with a `turn_ended`.
    /// A host that follows a child run ends the run's turn itself.
    pub(crate) fn end_turn(&mut self, outcome: TurnOutcome) {
        if let Some(WireView {
            sink: WireSink::Jsonl(stream),
            live: Some(_),
            ..
        }) = &mut self.wire
        {
            // The stream holds one turn, which nothing else names.
            let turn = rho_protocol::TurnId::new(uuid::Uuid::new_v4().hyphenated().to_string());
            stream.event(&WireEvent::TurnEnded { turn, outcome });
        }
    }

    /// Why the JSONL stream stopped, once a write to it failed; that
    /// failed the turn.
    pub(crate) fn output_failure(&self) -> Option<String> {
        self.wire.as_ref()?.sink.failure().map(str::to_owned)
    }

    /// Shows one turn's `events` until the turn drops its sender, answering
    /// on `control`.
    async fn show(
        &mut self,
        mut events: UnboundedReceiver<SessionEvent>,
        control: UnboundedSender<TurnControl>,
    ) {
        if let Some(wire) = &mut self.wire {
            wire.open_turn(&control);
        }
        let mut heartbeat = tokio::time::interval(REPORT_HEARTBEAT);
        heartbeat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        let mut asked = ParentAsks::default();
        loop {
            tokio::select! {
                biased;
                Some((id, answer)) = asked.pending.next() => {
                    asked.tokens.remove(&id);
                    let _ = control.send(TurnControl::AnswerQuestion { id, answer });
                }
                event = events.recv() => {
                    let Some(event) = event else {
                        break;
                    };
                    self.show_event(event, &control, &mut asked);
                }
                _ = heartbeat.tick(), if self.reporter.is_some() => {
                    if let Some(reporter) = self.reporter.as_deref_mut() {
                        reporter.write();
                    }
                }
            }
        }
        // The turn withdrew or answered every question before it ended;
        // stop any parent request still in flight.
        for token in asked.tokens.into_values() {
            token.cancel();
        }
    }

    fn show_event(
        &mut self,
        event: SessionEvent,
        control: &UnboundedSender<TurnControl>,
        asked: &mut ParentAsks,
    ) {
        if let Some(wire) = &mut self.wire {
            wire.show(&event, control);
        }
        match event {
            SessionEvent::Run(event) => {
                if let Some(reporter) = self.reporter.as_deref_mut() {
                    reporter.on_event(&event);
                }
            }
            SessionEvent::ApprovalRequested { id, .. } => {
                let _ = control.send(TurnControl::RespondApproval {
                    id,
                    decision: cancelled_approval(),
                });
            }
            SessionEvent::PlanApprovalRequested { id, .. } => {
                let _ = control.send(TurnControl::AnswerPlan {
                    id,
                    answer: PlanExitDecision::keep_planning(),
                });
            }
            SessionEvent::QuestionAsked { id, request, .. } => match &self.parent {
                Some(parent) => asked.ask(parent, id, request),
                None => {
                    let _ = control.send(TurnControl::AnswerQuestion {
                        id,
                        answer: Err(HostInputRefusal::Declined),
                    });
                }
            },
            SessionEvent::QuestionWithdrawn { id } => {
                if let Some(token) = asked.tokens.remove(&id) {
                    token.cancel();
                }
            }
            // A headless run is not named: the job is dropped unrun.
            SessionEvent::TitleRequested(_)
            | SessionEvent::MessageInserted(_)
            | SessionEvent::RunStarted { .. }
            | SessionEvent::BoundaryApplied { .. }
            | SessionEvent::ApprovalWithdrawn { .. }
            | SessionEvent::PlanApprovalWithdrawn { .. }
            | SessionEvent::Notice { .. }
            | SessionEvent::RunClosed
            | SessionEvent::RunFinished { .. } => {}
        }
    }
}

/// Where a view's wire events go.
enum WireSink<'a> {
    /// `rho run`'s JSONL stream. A write that fails it fails the turn.
    Jsonl(&'a mut JsonlStream),
    /// The host that follows a child run. A host that stopped listening
    /// misses the rest; the run goes on.
    Host(ChildEventSender),
}

impl WireSink<'_> {
    fn send(&mut self, event: WireEvent) {
        match self {
            Self::Jsonl(stream) => stream.event(&event),
            Self::Host(host) => {
                let _ = host.send(ChildEvent::Session(event));
            }
        }
    }

    /// Why the sink takes no more events, once a write to it failed.
    fn failure(&self) -> Option<&str> {
        match self {
            Self::Jsonl(stream) => stream.failure(),
            Self::Host(_) => None,
        }
    }
}

/// The sink a view writes the session's events to, and the mapper that
/// turns them into the wire events it carries.
struct WireView<'a> {
    sink: WireSink<'a>,
    /// The session's mapper, once [`Self::bind`] bound the session.
    live: Option<LiveEvents>,
    /// The display input of the turn about to begin.
    prompt: Option<UserInput>,
}

impl<'a> WireView<'a> {
    fn to(sink: WireSink<'a>) -> Self {
        Self {
            sink,
            live: None,
            prompt: None,
        }
    }

    /// Opens the sink on `runtime`'s session the first time; a rebind,
    /// whose agent has tools of its own, maps with them from then on.
    fn bind(&mut self, runtime: &SessionRuntime, process_config: &Config) {
        let workspace = runtime.workspace_root();
        let live = LiveEvents::new(
            workspace,
            runtime.tools(),
            runtime.configured_context_window(),
        );
        if self.live.replace(live).is_some() {
            return;
        }
        match &mut self.sink {
            WireSink::Jsonl(stream) => {
                stream.run_started(&runtime.session_id().to_string(), workspace);
            }
            WireSink::Host(host) => {
                // No auths: a child's model is its agent's, which nobody
                // switches.
                let options = session_options::of(
                    runtime,
                    process_config,
                    /*available_auths*/ &[],
                    CHILD_COMMANDS,
                );
                let _ = host.send(ChildEvent::Opened {
                    options: Box::new(options),
                    agent: runtime.agent_id().to_string(),
                });
            }
        }
    }

    /// Readies the mapper for the turn opening, which a sink that already
    /// failed fails at once.
    fn open_turn(&mut self, control: &UnboundedSender<TurnControl>) {
        if let (Some(live), Some(input)) = (&mut self.live, self.prompt.take()) {
            live.prompt(&input);
        }
        self.fail_turn_if_broken(control);
    }

    /// Writes `event` as its wire events.
    fn show(&mut self, event: &SessionEvent, control: &UnboundedSender<TurnControl>) {
        let Some(live) = &mut self.live else {
            return;
        };
        if self.sink.failure().is_some() {
            return;
        }
        // The headless wire carries no notifications.
        for wire in live.map(event) {
            self.sink.send(wire.event);
        }
        self.fail_turn_if_broken(control);
    }

    fn fail_turn_if_broken(&self, control: &UnboundedSender<TurnControl>) {
        if let Some(message) = self.sink.failure() {
            let _ = control.send(TurnControl::HostFailed {
                message: message.to_owned(),
            });
        }
    }
}

impl<'a> TurnView for HeadlessView<'a> {
    fn open_turn<'v>(
        &'v mut self,
        _runtime: &SessionRuntime,
    ) -> (TurnIo, impl Future<Output = ()> + Send + use<'v, 'a>) {
        let (events, event_receiver) = mpsc::unbounded_channel();
        let (control_sender, control) = mpsc::unbounded_channel();
        (
            TurnIo { events, control },
            self.show(event_receiver, control_sender),
        )
    }

    /// Unattended sessions offer no plan exit, so there is no handoff to
    /// show.
    fn report_plan(
        &mut self,
        _runtime: &SessionRuntime,
        _process_config: &Config,
        _report: PlanReport,
    ) -> impl Future<Output = ()> + Send {
        std::future::ready(())
    }
}

/// The parent session a delegated child's questions go to, through the
/// bridge its TUI listens on.
pub(crate) struct ParentQuestions {
    pub(crate) bridge: SubagentHostInputBridge,
    /// The child, as the parent names who asks.
    pub(crate) asker: ChildAsker,
}

type Answer = Result<HostInputResponse, HostInputRefusal>;
type AskFuture = Pin<Box<dyn Future<Output = (HostInputId, Answer)> + Send>>;

/// The questions waiting on the parent, each with the token that stops its
/// request once the turn withdraws it.
#[derive(Default)]
struct ParentAsks {
    pending: FuturesUnordered<AskFuture>,
    tokens: HashMap<HostInputId, CancellationToken>,
}

impl ParentAsks {
    fn ask(&mut self, parent: &ParentQuestions, id: HostInputId, request: HostInputRequest) {
        let token = CancellationToken::new();
        self.tokens.insert(id.clone(), token.clone());
        let bridge = parent.bridge.clone();
        let asker = parent.asker.clone();
        self.pending.push(Box::pin(async move {
            let answered = bridge.request(asker, request, &token).await;
            let answer = answered.map_err(|error| match error {
                rho_sdk::Error::Cancelled => HostInputRefusal::Dismissed,
                error => {
                    tracing::debug!(%error, "parent did not answer a delegated question");
                    HostInputRefusal::Declined
                }
            });
            (id, answer)
        }));
    }
}

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