rho-coding-agent 3.1.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
//! Runs a daemon session's turns from the TUI (#1667): sends composer
//! prompts, follows the turn while it runs, and cancels or steers it.
//!
//! The daemon owns the turn. A prompt only asks it to start one: the user
//! row is the daemon's `user_message` echo, and the busy chrome follows the
//! job's state ([`super::daemon_transcript`]). While the job runs, or waits
//! for input, the app loop hands the terminal to [`App::follow_daemon_turn`],
//! which routes keys through `during_turn`, so Esc cancels, Enter queues a
//! steer, which this sends to the daemon, and an open input request's prompt
//! takes its keys (`daemon_answers`). Quitting leaves the turn running in the
//! daemon, which keeps the session.

use std::time::Instant;

use rho_protocol::{PromptAuthor, TurnErrorKind, TurnOutcome};

use super::{
    daemon_prompt,
    during_turn::RunningTerminalError,
    idle_input::{HeldOrigin, HeldTurn},
    pending_input::{AcceptedSteering, PendingInputAction},
    send_confirm::SendSubmission,
    App, ChatMedia, DefaultTerminal, Entry, FrameScheduler, PasteSegment, QueuedPrompt,
    StreamControl, TurnPrompt,
};

impl App {
    /// Whether the followed daemon session runs a turn.
    pub(super) fn daemon_turn_runs(&self) -> bool {
        self.turn.is_provider_turn()
    }

    /// Sends `turn` with `media` to the daemon session as
    /// [`Self::send_daemon_turn`] does, once the person confirmed it where
    /// the session's model cannot be sent some of the conversation
    /// (`send_confirm::daemon`): every prompt that starts a model request
    /// goes through here. One held while the session's MCP servers connect
    /// is confirmed when it is released.
    ///
    /// The daemon starts a turn only once those servers connected, and the
    /// TUI would wait for it meanwhile, so every path to
    /// [`Self::send_daemon_turn`] holds the turn here until they have.
    pub(super) async fn submit_daemon_turn(
        &mut self,
        turn: TurnPrompt,
        media: Vec<ChatMedia>,
        paste_segments: Vec<PasteSegment>,
        terminal: &mut DefaultTerminal,
    ) -> anyhow::Result<()> {
        if !self.open_daemon_draft(terminal).await? {
            self.give_back_turn(turn, media, paste_segments);
            return Ok(());
        }
        if self.daemon_tool_sources_loading() {
            self.hold_turn(turn, media, paste_segments);
            return Ok(());
        }
        let submission = SendSubmission::turn(turn, media, paste_segments);
        self.submit_daemon_submission(submission, terminal).await
    }

    /// Sends a command that is no send to confirm, as `/compact`: held like
    /// a prompt while the session's MCP servers connect, and released the
    /// same way.
    pub(super) async fn send_daemon_command(
        &mut self,
        turn: TurnPrompt,
        media: Vec<ChatMedia>,
        paste_segments: Vec<PasteSegment>,
        terminal: &mut DefaultTerminal,
    ) -> anyhow::Result<()> {
        if !self.open_daemon_draft(terminal).await? {
            self.give_back_turn(turn, media, paste_segments);
            return Ok(());
        }
        if self.daemon_tool_sources_loading() {
            self.hold(HeldTurn {
                turn,
                media,
                paste_segments,
                origin: HeldOrigin::Command,
            });
            return Ok(());
        }
        self.send_daemon_turn(turn, media, paste_segments, terminal)
            .await
    }

    /// Puts a turn that was not sent back in the composer.
    fn give_back_turn(
        &mut self,
        turn: TurnPrompt,
        media: Vec<ChatMedia>,
        paste_segments: Vec<PasteSegment>,
    ) {
        self.restore_pending_prompt(QueuedPrompt {
            prompt: turn.model,
            display_prompt: turn.display,
            paste_segments,
            media,
        });
    }

    pub(super) fn daemon_tool_sources_loading(&self) -> bool {
        self.info.session.daemon.tool_sources_loading()
    }

    /// Opens the new session a first prompt opens, with the settings
    /// chosen before it, ahead of the hold for its MCP servers, which only
    /// an open session connects; `true` once it is open. When it could not
    /// open, the transcript says why, and the caller gives the prompt back.
    pub(super) async fn open_daemon_draft(
        &mut self,
        terminal: &mut DefaultTerminal,
    ) -> anyhow::Result<bool> {
        if !self.info.session.daemon.is_draft() {
            return Ok(true);
        }
        let opened = self.info.session.daemon.open_drafted().await;
        self.show_taken_daemon_views(terminal).await?;
        match opened {
            Ok(()) => Ok(true),
            Err(error) => {
                self.insert_entry(&Entry::Error(format!(
                    "could not open the session: {error:#}"
                )));
                Ok(false)
            }
        }
    }

    /// Asks the daemon to run `turn` with `media`, now and unconfirmed: the
    /// caller already held it while the session's MCP servers connected and
    /// passed the send gate where one applies. On a refusal the transcript
    /// says why and the composer gets the prompt back, so the caller clears
    /// the composer before calling. A turn that ended before
    /// it saved its prompt is not a refusal: the daemon's own rows say how
    /// it ended, so the prompt comes back without another. A command the
    /// daemon refused, as `/init` in plan mode, does not come back either:
    /// it goes to the input history as a sent one does.
    pub(super) async fn send_daemon_turn(
        &mut self,
        turn: TurnPrompt,
        media: Vec<ChatMedia>,
        paste_segments: Vec<PasteSegment>,
        terminal: &mut DefaultTerminal,
    ) -> anyhow::Result<()> {
        if let Err(error) = self.request_daemon_turn(&turn, &media).await {
            // Why the prompt found no attachment, such as a failed reattach
            // after a daemon restart, shows first.
            self.show_taken_daemon_views(terminal).await?;
            let ended = error
                .downcast_ref::<rho_protocol::ClientError>()
                .and_then(rho_protocol::ClientError::turn_ended);
            match ended {
                // The daemon refused the command itself, and its row says
                // why: sent, as far as the composer goes.
                Some(TurnOutcome::Failed { error })
                    if error.kind == TurnErrorKind::Configuration =>
                {
                    if !turn.history.is_empty() {
                        self.push_input_history(&turn.history);
                    }
                    self.reset_input_history_navigation();
                    return Ok(());
                }
                // The daemon's rows say how the turn ended.
                Some(
                    TurnOutcome::Failed { .. }
                    | TurnOutcome::Cancelled
                    | TurnOutcome::TimedOut { .. },
                ) => {}
                // Not sent, or ended in a way this rho cannot show.
                Some(_) | None => self.insert_entry(&Entry::Error(format!(
                    "could not send the prompt: {error:#}"
                ))),
            }
            self.give_back_turn(turn, media, paste_segments);
            return Ok(());
        }
        if !turn.history.is_empty() {
            self.push_input_history(&turn.history);
        }
        self.reset_input_history_navigation();
        self.show_taken_daemon_views(terminal).await
    }

    /// Sends the next queued follow-up once a turn completed armed
    /// `start_follow_ups` and the composer is free. Reports whether anything
    /// changed.
    pub(super) async fn start_next_follow_up(
        &mut self,
        terminal: &mut DefaultTerminal,
    ) -> anyhow::Result<bool> {
        // Another client's turn may run meanwhile; its end arms this again.
        if !self.start_follow_ups
            || self.input_ui.composer().blocks_held_turn_start()
            || self.is_ui_busy()
        {
            return Ok(false);
        }
        self.start_follow_ups = false;
        let Some((prompt, consent)) = self.pending.pop_follow_up_with_consent() else {
            return Ok(false);
        };
        self.pending_input_changed();
        self.select_pending_recall_target();
        let turn = TurnPrompt::standard(prompt.prompt, prompt.display_prompt);
        let submission = SendSubmission::turn(turn, prompt.media, prompt.paste_segments)
            .with_compact_consent(consent);
        // A confirmation takes the prompt, which it sends or returns to the
        // composer; the rest of the queue stays parked.
        let Some(submission) = self.gate_daemon_send(submission).await else {
            return Ok(true);
        };
        let (turn, prompt) = submission.into_daemon_turn();
        if let Err(error) = self.request_daemon_turn(&turn, &prompt.media).await {
            self.show_taken_daemon_views(terminal).await?;
            self.pending.push_follow_up_front(prompt);
            self.pending_input_changed();
            self.insert_entry(&Entry::Error(format!(
                "could not send the queued message: {error:#}"
            )));
            return Ok(true);
        }
        self.show_taken_daemon_views(terminal).await?;
        Ok(true)
    }

    /// Starts a daemon turn of `turn` with `media`. Once this returns, what
    /// the daemon sent before it answered, such as the turn's start, waits
    /// for [`Self::show_taken_daemon_views`], as does, on an error, what
    /// ended the attachment, such as a failed reattach.
    pub(super) async fn request_daemon_turn(
        &mut self,
        turn: &TurnPrompt,
        media: &[ChatMedia],
    ) -> anyhow::Result<()> {
        self.request_daemon_prompt(turn, media, PromptAuthor::Person)
            .await
    }

    /// [`Self::request_daemon_turn`] for a prompt `author` sends: the
    /// daemon keeps no user message for one the TUI sends on the person's
    /// behalf, as the workflow hub's start.
    pub(super) async fn request_daemon_prompt(
        &mut self,
        turn: &TurnPrompt,
        media: &[ChatMedia],
        author: PromptAuthor,
    ) -> anyhow::Result<()> {
        // The person moved on from a report still on its way.
        self.awaited_daemon_report = None;
        self.info
            .session
            .daemon
            .prompt(daemon_prompt::prompt_content(turn, media), author)
            .await
    }

    /// Shows what the daemon sent up to now without waiting, so a turn the
    /// TUI just started shows busy before the next key is read.
    pub(super) async fn show_taken_daemon_views(
        &mut self,
        terminal: &mut DefaultTerminal,
    ) -> anyhow::Result<()> {
        let views = self.info.session.daemon.take_views();
        self.show_daemon_views(views, terminal).await
    }

    /// Follows the daemon session's running turn until it rests, or the
    /// person quits: Esc cancels it, and Enter steers it.
    pub(super) async fn follow_daemon_turn(
        &mut self,
        terminal: &mut DefaultTerminal,
    ) -> anyhow::Result<()> {
        let mut frame_scheduler = FrameScheduler::new(Instant::now());
        self.draw_running_frame(terminal, &mut frame_scheduler)?;
        while self.daemon_turn_runs() && !self.should_quit {
            let mut needs_redraw = self.update_activity_panels()?;
            needs_redraw |= self.reconcile_overlays().await?;
            needs_redraw |= self.poll_sessions_task()?;
            needs_redraw |= self.finish_completed_inline_shells().await?;
            if needs_redraw {
                self.draw_running_frame(terminal, &mut frame_scheduler)?;
            }
            let frame_deadline =
                self.next_running_frame_deadline(frame_scheduler.deferred_deadline());
            tokio::select! {
                biased;
                event = self.terminal_session.as_mut().expect("terminal session initialized").next_event() => {
                    let control = self
                        .handle_running_terminal_events(event?, terminal)
                        .await;
                    // Before any cancel: Esc at an approval denies it, then
                    // cancels the turn, in that order.
                    let cancelling = matches!(control, Ok(StreamControl::Interrupt));
                    self.answer_daemon_input(terminal, cancelling).await?;
                    match control {
                        // Quitting leaves the turn to the daemon, unless
                        // nothing could follow it again: a session with no
                        // file cannot be resumed, so its turn ends here.
                        Ok(StreamControl::Interrupt)
                            if self.should_quit && !self.info.session.no_save => {}
                        Ok(StreamControl::Interrupt) => self.cancel_daemon_turn().await,
                        Ok(
                            StreamControl::Continue
                            | StreamControl::Resize
                            | StreamControl::ApprovalResolved,
                        ) => {}
                        Err(RunningTerminalError::Recoverable(error)) => {
                            self.insert_entry(&Entry::Error(format!(
                                "could not handle input: {error}"
                            )));
                        }
                        Err(RunningTerminalError::Terminal(error)) => return Err(error),
                    }
                    self.send_daemon_pending_input().await;
                    self.pending_input_changed();
                    self.draw_running_frame(terminal, &mut frame_scheduler)?;
                }
                views = self.info.session.daemon.next_views() => {
                    self.show_daemon_views(views, terminal).await?;
                    if frame_scheduler.request_background_frame(Instant::now()) {
                        self.draw_running_frame(terminal, &mut frame_scheduler)?;
                    }
                }
                _ = tokio::time::sleep_until(frame_deadline) => {
                    self.tick_questionnaire_timeout();
                    self.drain_stream_tick(terminal)?;
                    self.flush_due_paste_burst();
                    self.draw_running_frame(terminal, &mut frame_scheduler)?;
                }
            }
        }
        Ok(())
    }

    pub(super) fn draw_running_frame(
        &mut self,
        terminal: &mut DefaultTerminal,
        frame_scheduler: &mut FrameScheduler,
    ) -> anyhow::Result<()> {
        terminal.draw(|frame| self.draw(frame))?;
        frame_scheduler.rendered(Instant::now());
        Ok(())
    }

    /// Asks the daemon to stop its turn; the turn's end shows that it did.
    pub(super) async fn cancel_daemon_turn(&mut self) {
        if let Err(error) = self.info.session.daemon.cancel().await {
            self.insert_entry(&Entry::Error(format!(
                "could not cancel the turn: {error:#}"
            )));
        }
    }

    /// Sends what the pending panel asked for since: a steer to withdraw,
    /// then the steers queued meanwhile. A steer the daemon refuses, and
    /// every one queued after it, becomes a follow-up.
    async fn send_daemon_pending_input(&mut self) {
        if let Some(action) = self.pending.take_input_action() {
            let id = match &action {
                PendingInputAction::EditAccepted { id, .. }
                | PendingInputAction::DiscardAccepted { id } => id.clone(),
            };
            let retraction = match self.info.session.daemon.unsteer(&id).await {
                Ok(outcome) => steering_retraction(outcome),
                Err(error) => Err(format!("{error:#}")),
            };
            self.finish_steering_retraction(action, retraction);
        }
        while let Some(prompt) = self.pending.steering_prompts_mut().pop_front() {
            match self.info.session.daemon.steer(prompt.prompt.clone()).await {
                Ok(id) => {
                    self.pending
                        .accepted_steering_mut()
                        .push_back(AcceptedSteering {
                            id,
                            prompt,
                            delivered: false,
                        });
                    self.select_pending_recall_target();
                }
                Err(error) => {
                    let unsent: Vec<_> = std::iter::once(prompt)
                        .chain(self.pending.steering_prompts_mut().drain(..))
                        .collect();
                    for prompt in unsent.into_iter().rev() {
                        self.pending.push_follow_up_front(prompt);
                    }
                    self.select_pending_recall_target();
                    self.notify_status(format!("steer queued as follow-up: {error:#}"));
                    return;
                }
            }
        }
    }
}

/// What withdrawing a steer did, as the pending panel shows it.
fn steering_retraction(
    outcome: rho_protocol::UnsteerOutcome,
) -> Result<rho_sdk::SteeringRetraction, String> {
    match outcome {
        rho_protocol::UnsteerOutcome::Withdrawn => Ok(rho_sdk::SteeringRetraction::Retracted),
        rho_protocol::UnsteerOutcome::Delivered => Ok(rho_sdk::SteeringRetraction::Delivered),
        rho_protocol::UnsteerOutcome::Applied => Ok(rho_sdk::SteeringRetraction::AlreadyApplied),
        rho_protocol::UnsteerOutcome::NotFound => Ok(rho_sdk::SteeringRetraction::NotFound),
        // `Unknown`, or an outcome a newer daemon sends.
        _ => Err("the rho daemon answered with an outcome this client does not know".into()),
    }
}