rho-coding-agent 3.6.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
//! The aside's session: a fork of the followed session in the daemon
//! (`rho/session/fork`), opened at the aside's first prompt.
//!
//! The fork starts from the session's conversation as last saved and never
//! writes back to it. The daemon opens it supervised (plan, when the session
//! was saved in plan) and tells its model it is a side chat that must leave
//! the workspace alone; reads run free. The overlay has no approval prompt,
//! so the aside denies every approval the fork asks for, and keeps a plan
//! in plan mode. Its setup is the session's, less what the overlay cannot
//! show (questions and the `ui` tool), subagents, whose work would outlive
//! a cancelled turn, and idle wake, since nothing would show the turn it
//! starts. The fork is memory-only: the daemon saves nothing of it, and
//! discards it once nothing follows it and it is idle, as when the TUI
//! quits or its connection goes.
//!
//! The worker follows the fork on the session's connection, by its own
//! route, and maps its live events, not the replay of the forked
//! conversation, to [`SideChatEvent`]s. Dropping the handle stops the
//! fork's turn, if one runs, then deletes the fork once the turn ended, as
//! the daemon deletes no session mid-turn.

use std::{collections::HashSet, sync::Arc};

use rho_protocol::{
    methods::{
        SessionAnswer, SessionAttach, SessionCancel, SessionDelete, SessionFork, SessionPrompt,
    },
    Ask, ClientEnvironment, ContentBlock, IdleWake, InputAnswer, InputRequest, SessionAnswerParams,
    SessionAttachParams, SessionCancelParams, SessionDeleteParams, SessionEvent, SessionForkParams,
    SessionId, SessionPart, SessionPromptParams, SessionSetup, Surfaces, ToolCallId, TurnOutcome,
};
use tokio::sync::mpsc;

use crate::{
    app::daemon_client::{Daemon, Part, Routed, StreamPosition},
    tui::approval::ApprovalPrompt,
};

/// Why the aside denies an approval, which the model hears and the overlay
/// shows.
pub(super) const READ_ONLY: &str = "side chats are read-only";

/// What the overlay shows of the fork.
#[derive(Debug)]
pub(super) enum SideChatEvent {
    AssistantDelta(String),
    /// The provider discarded the reply it was streaming.
    AssistantReset,
    ToolStarted(String),
    /// The aside denied an approval the fork asked for; its summary.
    Denied(String),
    /// Something to tell the user that ends no turn, such as a daemon notice.
    Notice(String),
    Finished,
    Failed(String),
    Cancelled,
}

/// What forks the session: its daemon, its id, and its setup.
pub(super) struct ForkLaunch {
    pub(super) daemon: Arc<Daemon>,
    pub(super) parent: SessionId,
    pub(super) setup: SessionSetup,
}

#[derive(Debug)]
enum Command {
    Submit(String),
    Cancel,
}

/// The overlay's end of the worker. Dropping it winds the fork down.
pub(super) struct SideChatHandle {
    commands: mpsc::UnboundedSender<Command>,
    events: mpsc::UnboundedReceiver<SideChatEvent>,
}

impl SideChatHandle {
    pub(super) fn submit(&self, prompt: String) {
        let _ = self.commands.send(Command::Submit(prompt));
    }

    pub(super) fn cancel(&self) {
        let _ = self.commands.send(Command::Cancel);
    }

    pub(super) fn try_recv(&mut self) -> Option<SideChatEvent> {
        self.events.try_recv().ok()
    }
}

/// Starts the worker; the fork opens with the first submit. The overlay
/// submits only while no turn of the aside runs.
pub(super) fn spawn_side_chat(launch: ForkLaunch) -> SideChatHandle {
    let (commands, command_rx) = mpsc::unbounded_channel();
    let (event_tx, events) = mpsc::unbounded_channel();
    tokio::spawn(worker(launch, command_rx, event_tx));
    SideChatHandle { commands, events }
}

/// The fork the worker follows.
struct Fork {
    id: SessionId,
    routed: mpsc::UnboundedReceiver<Routed>,
    position: StreamPosition,
    /// Whether a turn the aside started still runs.
    running: bool,
    /// The tool calls the overlay was told of.
    tools: HashSet<ToolCallId>,
}

async fn worker(
    launch: ForkLaunch,
    mut commands: mpsc::UnboundedReceiver<Command>,
    events: mpsc::UnboundedSender<SideChatEvent>,
) {
    let ForkLaunch {
        daemon,
        parent,
        setup,
    } = launch;
    let setup = fork_setup(setup);
    let mut fork: Option<Fork> = None;
    loop {
        tokio::select! {
            command = commands.recv() => match command {
                None => break,
                Some(Command::Cancel) => {
                    if let Some(fork) = fork.as_ref().filter(|fork| fork.running) {
                        cancel(&daemon, &fork.id).await;
                    }
                }
                Some(Command::Submit(prompt)) => {
                    let open = match fork.as_mut() {
                        Some(open) => open,
                        None => match open(&daemon, &parent, &setup).await {
                            Ok(opened) => fork.insert(opened),
                            Err(error) => {
                                let _ = events.send(SideChatEvent::Failed(format!(
                                    "could not start side chat: {error:#}"
                                )));
                                continue;
                            }
                        },
                    };
                    match send_prompt(&daemon, &open.id, prompt).await {
                        Ok(()) => open.running = true,
                        Err(error) => {
                            let _ = events.send(SideChatEvent::Failed(format!(
                                "could not send side chat message: {error:#}"
                            )));
                        }
                    }
                }
            },
            routed = next_routed(fork.as_mut()) => match routed {
                Some(Routed::Event { seq, event }) => {
                    let Some(open) = fork.as_mut() else {
                        continue;
                    };
                    if open.position.observe(seq) == Part::Live {
                        apply(&daemon, open, event, &events).await;
                    }
                }
                // The daemon ended the attachment, or the connection went,
                // and the memory-only fork with it: the next prompt forks
                // the session again.
                Some(Routed::Detached(_) | Routed::Lost) | None => {
                    if let Some(lost) = fork.take() {
                        daemon.unroute(&lost.id);
                        let _ = events.send(if lost.running {
                            SideChatEvent::Failed(
                                "could not complete side chat: lost its session in the rho daemon"
                                    .into(),
                            )
                        } else {
                            SideChatEvent::Notice(
                                "the side chat lost its session in the rho daemon; the next \
                                 message starts it over"
                                    .into(),
                            )
                        });
                    }
                }
            },
        }
    }
    if let Some(fork) = fork {
        close(&daemon, fork).await;
    }
}

async fn next_routed(fork: Option<&mut Fork>) -> Option<Routed> {
    match fork {
        Some(fork) => fork.routed.recv().await,
        None => std::future::pending().await,
    }
}

/// The setup a fork attaches with: the session's, without what the overlay
/// cannot show or what would run unseen.
fn fork_setup(mut setup: SessionSetup) -> SessionSetup {
    setup.without.insert(SessionPart::Questions);
    setup.without.insert(SessionPart::Subagents);
    // Only a new session takes it; the fork opens with it.
    setup.without.remove(&SessionPart::Saving);
    setup.surfaces = Surfaces::default();
    setup.idle_wake = IdleWake::default();
    setup
}

/// Forks `parent` memory-only and attaches to the fork with `setup`.
async fn open(daemon: &Daemon, parent: &SessionId, setup: &SessionSetup) -> anyhow::Result<Fork> {
    let mut memory_only = setup.clone();
    memory_only.without.insert(SessionPart::Saving);
    let forked = daemon
        .request::<SessionFork>(&SessionForkParams {
            session: parent.clone(),
            environment: ClientEnvironment::of_this_process(),
            setup: memory_only,
        })
        .await?;
    let id = forked.session;
    let routed = daemon.route_channel(&id);
    let attached = daemon
        .request::<SessionAttach>(&SessionAttachParams {
            session: id.clone(),
            after: None,
            environment: ClientEnvironment::of_this_process(),
            setup: setup.clone(),
        })
        .await;
    match attached {
        Ok(response) => Ok(Fork {
            position: StreamPosition::attached(&response),
            id,
            routed,
            running: false,
            tools: HashSet::new(),
        }),
        Err(error) => {
            daemon.unroute(&id);
            delete(daemon, &id).await;
            Err(error.into())
        }
    }
}

async fn send_prompt(daemon: &Daemon, id: &SessionId, prompt: String) -> anyhow::Result<()> {
    daemon
        .request::<SessionPrompt>(&SessionPromptParams {
            session: id.clone(),
            content: vec![ContentBlock::Text { text: prompt }],
            timeout_ms: None,
            environment: ClientEnvironment::of_this_process(),
            grant: None,
            idempotency_key: None,
            author: Default::default(),
        })
        .await?;
    Ok(())
}

async fn cancel(daemon: &Daemon, id: &SessionId) {
    let _ = daemon
        .request::<SessionCancel>(&SessionCancelParams {
            session: id.clone(),
        })
        .await;
}

/// Deletes the fork now. One the delete misses is memory-only all the same:
/// the daemon discards it once it is idle and nothing follows it.
async fn delete(daemon: &Daemon, id: &SessionId) {
    if let Err(error) = daemon
        .request::<SessionDelete>(&SessionDeleteParams {
            session: id.clone(),
            unless_attached: false,
        })
        .await
    {
        tracing::debug!(%error, session = %id, "could not delete side chat fork");
    }
}

/// Shows one live event of the fork, and answers what it asks.
async fn apply(
    daemon: &Daemon,
    fork: &mut Fork,
    event: SessionEvent,
    events: &mpsc::UnboundedSender<SideChatEvent>,
) {
    let shown = match event {
        SessionEvent::AgentMessageChunk { text, .. } => SideChatEvent::AssistantDelta(text),
        SessionEvent::ResponseDiscarded => SideChatEvent::AssistantReset,
        SessionEvent::ToolCall(update) => {
            if !fork.tools.insert(update.id.clone()) {
                return;
            }
            SideChatEvent::ToolStarted(update.title.unwrap_or_else(|| "tool".into()))
        }
        SessionEvent::Notice { text } => SideChatEvent::Notice(text),
        SessionEvent::InputRequested { request } => {
            match refuse(daemon, &fork.id, *request).await {
                Some(denied) => denied,
                None => return,
            }
        }
        SessionEvent::TurnEnded { outcome, .. } => {
            fork.running = false;
            match outcome {
                TurnOutcome::Answered { .. } | TurnOutcome::MaxSteps { .. } => {
                    SideChatEvent::Finished
                }
                TurnOutcome::Cancelled => SideChatEvent::Cancelled,
                TurnOutcome::Failed { error } => SideChatEvent::Failed(format!(
                    "could not complete side chat: {}",
                    error.message
                )),
                TurnOutcome::TimedOut { .. } => {
                    SideChatEvent::Failed("could not complete side chat: it timed out".into())
                }
                // `Unknown`, or an outcome a newer daemon sends.
                _ => SideChatEvent::Finished,
            }
        }
        // The overlay shows the reply and its tools alone: no reasoning,
        // progress, cards, or session state.
        SessionEvent::State { .. }
        | SessionEvent::UserMessage { .. }
        | SessionEvent::ThoughtChunk { .. }
        | SessionEvent::Plan { .. }
        | SessionEvent::Usage { .. }
        | SessionEvent::TitleChanged { .. }
        | SessionEvent::MarkChanged { .. }
        | SessionEvent::Compaction { .. }
        | SessionEvent::ProviderRequest
        | SessionEvent::ProviderRetry { .. }
        | SessionEvent::Card { .. }
        | SessionEvent::InputResolved { .. }
        | SessionEvent::InputHeld { .. }
        | SessionEvent::OptionsChanged { .. }
        | SessionEvent::SteeringDelivered { .. }
        | SessionEvent::CacheWarm { .. }
        | SessionEvent::Background { .. }
        | SessionEvent::Activity { .. }
        | SessionEvent::Agent { .. }
        | SessionEvent::PullRequests { .. } => return,
        // `Unknown`, or an event a newer daemon sends.
        _ => return,
    };
    let _ = events.send(shown);
}

/// Answers an input request of the fork as a read-only aside does: an
/// approval is denied, and a plan stays a plan. Returns what the overlay
/// shows of a denied approval.
async fn refuse(daemon: &Daemon, id: &SessionId, request: InputRequest) -> Option<SideChatEvent> {
    let (answer, shown) = match &request.ask {
        Ask::Approval(ask) => (
            InputAnswer::Deny {
                reason: Some(READ_ONLY.into()),
            },
            Some(SideChatEvent::Denied(
                ApprovalPrompt::of_daemon_ask(ask).summary().join(" ยท "),
            )),
        ),
        Ask::PlanApproval(_) => (
            InputAnswer::KeepPlanning {
                feedback: Some(READ_ONLY.into()),
            },
            None,
        ),
        // The fork opens without questions; one asked all the same goes
        // unanswered by the person.
        Ask::Question(_) => (InputAnswer::Dismiss, None),
        // `Unknown`, or an ask a newer daemon sends: the turn waits on it
        // until the person cancels it.
        _ => return None,
    };
    // A request that closed meanwhile needs no answer.
    let _ = daemon
        .request::<SessionAnswer>(&SessionAnswerParams {
            session: id.clone(),
            request: request.id,
            answer,
        })
        .await;
    shown
}

/// Stops the fork's turn, if one runs, then deletes the fork once it ended.
async fn close(daemon: &Daemon, mut fork: Fork) {
    if fork.running {
        cancel(daemon, &fork.id).await;
        while let Some(routed) = fork.routed.recv().await {
            match routed {
                Routed::Event {
                    event: SessionEvent::TurnEnded { .. },
                    ..
                } => break,
                Routed::Event { .. } => {}
                Routed::Detached(_) | Routed::Lost => break,
            }
        }
    }
    daemon.unroute(&fork.id);
    delete(daemon, &fork.id).await;
}