rho-coding-agent 1.9.0

A lightweight agent harness inspired by Pi
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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use rho_sdk::CancellationToken;

use super::*;

const GOAL_RETRY_DELAY: Duration = Duration::from_secs(3);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum GoalLoopAction {
    /// Turn finished normally; re-enter the evaluate/work cycle.
    Continue,
    /// Transient failure; wait, then keep working while the goal remains active.
    RetryAfterFailure,
    /// User interrupt/cancel or other terminal stop; leave the goal loop.
    Stop,
}

pub(super) fn should_resume_goal_after_turn(
    outcome: TurnOutcomeKind,
    goal_state: Option<goal::GoalLoopState>,
    should_quit: bool,
) -> bool {
    matches!(goal_state, Some(goal::GoalLoopState::Active))
        && !should_quit
        && matches!(
            outcome,
            TurnOutcomeKind::Completed | TurnOutcomeKind::Failed
        )
}

pub(super) fn should_drain_queued_prompts(outcome: TurnOutcomeKind, resume_goal: bool) -> bool {
    matches!(outcome, TurnOutcomeKind::Completed) || resume_goal
}

fn goal_loop_action_after_turn(outcome: TurnOutcomeKind) -> GoalLoopAction {
    match outcome {
        TurnOutcomeKind::Completed => GoalLoopAction::Continue,
        TurnOutcomeKind::Failed => GoalLoopAction::RetryAfterFailure,
        TurnOutcomeKind::Interrupted | TurnOutcomeKind::Cancelled => GoalLoopAction::Stop,
    }
}

impl App {
    pub(super) fn execute_goal_command_during_turn(
        &mut self,
        invocation: CommandInvocation,
    ) -> anyhow::Result<()> {
        if invocation.args.trim().is_empty() {
            self.insert_entry(&Entry::Notice(self.goal_status_message()));
            self.status = self
                .goal
                .as_ref()
                .map(|goal| {
                    if goal.is_blocked() {
                        "goal blocked"
                    } else {
                        "goal active"
                    }
                })
                .unwrap_or("running")
                .into();
        } else if is_goal_clear_alias(&invocation.args) {
            self.clear_goal();
        } else {
            self.insert_entry(&Entry::Notice(
                "/goal can only be inspected or cleared while a model turn is running".into(),
            ));
            self.status = "goal command unavailable while running".into();
        }
        Ok(())
    }

    pub(super) fn goal_status_message(&self) -> String {
        match &self.goal {
            Some(goal) => {
                let state = if goal.is_blocked() {
                    "goal blocked"
                } else {
                    "goal active"
                };
                let reason = goal
                    .last_reason
                    .as_deref()
                    .map(|reason| format!("\nlast evaluation: {reason}"))
                    .unwrap_or_default();
                let pending_steps = if goal.is_blocked() {
                    format!(
                        "\nremaining steps:\n{}\nuse /goal resume after completing them.",
                        format_human_steps(goal.pending_steps())
                    )
                } else {
                    String::new()
                };
                format!(
                    "{state}: {}\n{} turn(s), {} elapsed{reason}{pending_steps}",
                    goal.condition,
                    goal.turns,
                    goal::format_elapsed(goal.elapsed())
                )
            }
            None => "no active goal. use /goal <condition> to start one.".into(),
        }
    }

    pub(super) fn clear_goal(&mut self) {
        if self.goal.take().is_some() {
            self.insert_entry(&Entry::Notice("goal cleared".into()));
            self.status = if self.running {
                "running"
            } else {
                "goal cleared"
            }
            .into();
        } else {
            self.insert_entry(&Entry::Notice("no active goal".into()));
            self.status = if self.running { "running" } else { "ready" }.into();
        }
    }

    pub(super) async fn execute_goal_command(
        &mut self,
        invocation: CommandInvocation,
        terminal: &mut DefaultTerminal,
        agent: &mut InteractiveRuntime,
    ) -> anyhow::Result<()> {
        let condition = invocation.args.trim();
        if condition.is_empty() {
            self.insert_entry(&Entry::Notice(self.goal_status_message()));
            self.status = self
                .goal
                .as_ref()
                .map(|goal| {
                    if goal.is_blocked() {
                        "goal blocked"
                    } else {
                        "goal active"
                    }
                })
                .unwrap_or("ready")
                .into();
            return Ok(());
        }
        if is_goal_clear_alias(condition) {
            self.clear_goal();
            return Ok(());
        }
        if is_goal_resume_alias(condition) {
            self.resume_goal(terminal, agent).await?;
            return Ok(());
        }
        if condition.chars().count() > goal::MAX_GOAL_CHARS {
            self.insert_entry(&Entry::Error(format!(
                "goal conditions cannot exceed {} characters",
                goal::MAX_GOAL_CHARS
            )));
            self.status = "goal not set".into();
            return Ok(());
        }

        self.goal = Some(GoalState::new(condition.to_string()));
        self.insert_entry(&Entry::Notice(format!(
            "goal set: {condition}\nrho will keep working until the goal is met. use /goal clear to cancel."
        )));
        self.status = "goal active".into();
        let images = std::mem::take(&mut self.pending_images);
        let outcome = self
            .run_prompt_turn(
                TurnPrompt::command(initial_goal_prompt(condition), format!("/goal {condition}")),
                images,
                terminal,
                agent,
            )
            .await?;
        let outcome_kind = outcome.kind();
        let pending_retries = match outcome {
            TurnOutcome::Failed(failed_turn) => VecDeque::from([failed_turn]),
            TurnOutcome::Completed | TurnOutcome::Interrupted | TurnOutcome::Cancelled => {
                VecDeque::new()
            }
        };
        if should_resume_goal_after_turn(
            outcome_kind,
            self.goal.as_ref().map(GoalState::loop_state),
            self.should_quit,
        ) {
            self.continue_goal(terminal, agent, pending_retries).await?;
        }
        Ok(())
    }

    async fn resume_goal(
        &mut self,
        terminal: &mut DefaultTerminal,
        agent: &mut InteractiveRuntime,
    ) -> anyhow::Result<()> {
        let Some(goal) = self.goal.as_mut() else {
            self.insert_entry(&Entry::Notice("no active goal to resume".into()));
            self.status = "ready".into();
            return Ok(());
        };

        if goal.is_blocked() {
            let condition = goal.condition.clone();
            let pending_steps = goal.pending_steps().to_vec();
            goal.begin_verification();
            let prompt = blocked_goal_resumption_prompt(&condition, &pending_steps, None);
            self.insert_entry(&Entry::Notice(
                "goal resumed; verifying the previously blocked steps".into(),
            ));
            self.status = "goal active".into();
            let outcome = self
                .run_prompt_turn(
                    TurnPrompt::command(prompt, "/goal resume".into()),
                    Vec::new(),
                    terminal,
                    agent,
                )
                .await?;
            let outcome_kind = outcome.kind();
            self.finish_goal_resumption_turn(outcome_kind);
            let pending_retries = match outcome {
                TurnOutcome::Failed(failed_turn) => VecDeque::from([failed_turn]),
                TurnOutcome::Completed | TurnOutcome::Interrupted | TurnOutcome::Cancelled => {
                    VecDeque::new()
                }
            };
            if should_resume_goal_after_turn(
                outcome_kind,
                self.goal.as_ref().map(GoalState::loop_state),
                self.should_quit,
            ) {
                self.continue_goal(terminal, agent, pending_retries).await?;
            }
        } else {
            self.insert_entry(&Entry::Notice("goal is already active".into()));
            self.continue_goal(terminal, agent, VecDeque::new()).await?;
        }
        Ok(())
    }

    pub(super) fn prepare_goal_resumption_turn(
        &mut self,
        prompt: String,
        display_prompt: String,
    ) -> TurnPrompt {
        let Some(goal) = self.goal.as_mut() else {
            return TurnPrompt::standard(prompt, display_prompt);
        };
        if !goal.is_blocked() {
            return TurnPrompt::standard(prompt, display_prompt);
        }

        let condition = goal.condition.clone();
        let pending_steps = goal.pending_steps().to_vec();
        goal.begin_verification();
        self.insert_entry(&Entry::Notice(
            "goal resumed by user message; verifying the previously blocked steps".into(),
        ));
        self.status = "goal active".into();
        TurnPrompt {
            model: blocked_goal_resumption_prompt(&condition, &pending_steps, Some(&prompt)),
            history: prompt,
            display: display_prompt.clone(),
            persisted_display: Some(display_prompt),
        }
    }

    pub(super) fn finish_goal_resumption_turn(&mut self, outcome: TurnOutcomeKind) {
        let Some(goal) = self.goal.as_mut() else {
            return;
        };
        match outcome {
            TurnOutcomeKind::Completed | TurnOutcomeKind::Failed => {
                goal.complete_verification();
            }
            TurnOutcomeKind::Interrupted | TurnOutcomeKind::Cancelled => {
                goal.interrupt_verification();
            }
        }
    }

    pub(super) async fn continue_goal(
        &mut self,
        terminal: &mut DefaultTerminal,
        agent: &mut InteractiveRuntime,
        mut pending_retries: VecDeque<FailedTurn>,
    ) -> anyhow::Result<()> {
        while !self.should_quit && self.goal.as_ref().is_some_and(|goal| !goal.is_blocked()) {
            while let Some(failed_turn) = pending_retries.pop_front() {
                if !self
                    .retry_failed_goal_turn(failed_turn, terminal, agent)
                    .await?
                {
                    self.report_resting_herdr_state().await;
                    terminal.draw(|frame| self.draw(frame))?;
                    return Ok(());
                }
            }
            self.status = "evaluating goal".into();
            self.loading_spinner.start();
            terminal.draw(|frame| self.draw(frame))?;

            let (condition, provider, model) = {
                let goal = self.goal.as_ref().expect("goal checked above");
                (
                    goal.condition.clone(),
                    self.info
                        .runtime
                        .title_provider
                        .clone()
                        .unwrap_or_else(|| self.info.runtime.provider.clone()),
                    self.info
                        .runtime
                        .title_model
                        .clone()
                        .unwrap_or_else(|| self.info.runtime.model.clone()),
                )
            };
            let history = agent.history();
            let evaluation = {
                let interrupt_requested = AtomicBool::new(false);
                let tool_call_active = AtomicBool::new(false);
                let cancellation = CancellationToken::new();
                let mut evaluation = Box::pin(goal::evaluate(
                    goal::EvaluationRequest {
                        provider_name: &provider,
                        model: &model,
                        condition: &condition,
                        messages: &history,
                        cancellation: cancellation.clone(),
                        session_id: agent.session_id(),
                        workspace_path: agent.workspace_path(),
                    },
                    agent.usage_recording(),
                ));
                let deadline = tokio::time::Instant::now() + goal::EVALUATION_TIMEOUT;
                loop {
                    tokio::select! {
                        result = &mut evaluation => break Some(result),
                        _ = tokio::time::sleep_until(deadline) => {
                            cancellation.cancel();
                            let _ = evaluation.await;
                            break Some(Err(anyhow::anyhow!("goal evaluation timed out")));
                        }
                        terminal_event = self.terminal_events.as_mut().expect("terminal events initialized").next() => {
                            let control = self.handle_running_terminal_events(
                                terminal_event?,
                                terminal,
                                &interrupt_requested,
                                &tool_call_active,
                                RunningInputMode::Turn,
                            )?;
                            if matches!(control, StreamControl::Interrupt) {
                                cancellation.cancel();
                                let _ = evaluation.await;
                                self.clear_goal();
                                break None;
                            }
                            if self.goal.is_none() {
                                cancellation.cancel();
                                let _ = evaluation.await;
                                break None;
                            }
                            terminal.draw(|frame| self.draw(frame))?;
                        }
                        _ = tokio::time::sleep(LoadingSpinner::FRAME_INTERVAL) => {
                            self.flush_due_paste_burst();
                            terminal.draw(|frame| self.draw(frame))?;
                        }
                    }
                    if self.finish_completed_inline_shells().await? {
                        terminal.draw(|frame| self.draw(frame))?;
                    }
                }
            };
            self.finish_all_inline_shells().await?;
            self.insert_deferred_inline_shell_context(agent)?;
            self.loading_spinner.stop();
            let Some(evaluation) = evaluation else {
                break;
            };

            let evaluation = match evaluation {
                Ok(evaluation) => evaluation,
                Err(err) => {
                    self.insert_entry(&Entry::Error(format!(
                        "goal evaluation failed; retrying while goal remains active: {err}"
                    )));
                    self.status = "goal retrying".into();
                    if !self.wait_for_goal_retry(terminal, agent).await? {
                        break;
                    }
                    continue;
                }
            };
            let Some(goal) = self.goal.as_mut() else {
                break;
            };
            let disposition = goal.record_evaluation(&evaluation);
            match disposition {
                goal::GoalDisposition::Complete => {
                    let elapsed = goal::format_elapsed(goal.elapsed());
                    let turns = goal.turns;
                    self.goal = None;
                    self.insert_entry(&Entry::Notice(format!(
                        "goal achieved after {turns} turn(s) and {elapsed}: {}",
                        evaluation.reason()
                    )));
                    self.status = "goal achieved".into();
                    break;
                }
                goal::GoalDisposition::Pause => {
                    self.insert_entry(&Entry::Notice(format!(
                        "goal blocked: remaining steps need you\n{}\nremaining steps:\n{}\nuse /goal resume or send a message after completing them.",
                        evaluation.reason(),
                        format_human_steps(evaluation.pending_steps())
                    )));
                    self.status = "goal blocked".into();
                    break;
                }
                goal::GoalDisposition::Continue => {}
            }

            self.insert_entry(&Entry::Notice(format!(
                "goal not yet met: {}",
                evaluation.reason()
            )));
            let continuation = format!(
                "Continue working toward this goal:\n\n{condition}\n\nThe goal evaluator says it is not yet met: {}\n\nMake concrete progress and verify the completion condition before stopping.",
                evaluation.reason()
            );
            let outcome = self
                .run_prompt_turn(
                    TurnPrompt::standard(continuation, "continuing active goal".into()),
                    Vec::new(),
                    terminal,
                    agent,
                )
                .await?;
            match outcome {
                TurnOutcome::Completed => {}
                TurnOutcome::Failed(failed_turn) => {
                    if !self
                        .retry_failed_goal_turn(failed_turn, terminal, agent)
                        .await?
                    {
                        break;
                    }
                }
                TurnOutcome::Interrupted | TurnOutcome::Cancelled => break,
            }
        }
        self.report_resting_herdr_state().await;
        terminal.draw(|frame| self.draw(frame))?;
        Ok(())
    }

    async fn retry_failed_goal_turn(
        &mut self,
        mut failed_turn: FailedTurn,
        terminal: &mut DefaultTerminal,
        agent: &mut InteractiveRuntime,
    ) -> anyhow::Result<bool> {
        loop {
            self.insert_entry(&Entry::Notice(
                "goal still active; retrying after the run stopped before the goal was met".into(),
            ));
            self.status = "goal retrying".into();
            if !self.wait_for_goal_retry(terminal, agent).await? {
                return Ok(false);
            }

            let outcome = self
                .retry_failed_prompt_turn(failed_turn, terminal, agent)
                .await?;
            match goal_loop_action_after_turn(outcome.kind()) {
                GoalLoopAction::Continue => return Ok(true),
                GoalLoopAction::RetryAfterFailure => {
                    let TurnOutcome::Failed(next_failed_turn) = outcome else {
                        unreachable!("retry action requires a failed turn")
                    };
                    failed_turn = next_failed_turn;
                }
                GoalLoopAction::Stop => return Ok(false),
            }
        }
    }

    async fn wait_for_goal_retry(
        &mut self,
        terminal: &mut DefaultTerminal,
        agent: &mut InteractiveRuntime,
    ) -> anyhow::Result<bool> {
        if self.should_quit || self.goal.is_none() {
            return Ok(false);
        }

        self.loading_spinner.start();
        terminal.draw(|frame| self.draw(frame))?;
        let interrupt_requested = AtomicBool::new(false);
        let tool_call_active = AtomicBool::new(false);
        let deadline = tokio::time::Instant::now() + GOAL_RETRY_DELAY;
        let should_retry = loop {
            if self.should_quit || self.goal.is_none() {
                break false;
            }
            if tokio::time::Instant::now() >= deadline {
                break true;
            }
            tokio::select! {
                _ = tokio::time::sleep_until(deadline) => break true,
                terminal_event = self.terminal_events.as_mut().expect("terminal events initialized").next() => {
                    let control = self.handle_running_terminal_events(
                        terminal_event?,
                        terminal,
                        &interrupt_requested,
                        &tool_call_active,
                        RunningInputMode::Turn,
                    )?;
                    if matches!(control, StreamControl::Interrupt) {
                        self.clear_goal();
                        break false;
                    }
                    if self.goal.is_none() || self.should_quit {
                        break false;
                    }
                    terminal.draw(|frame| self.draw(frame))?;
                }
                _ = tokio::time::sleep(LoadingSpinner::FRAME_INTERVAL) => {
                    self.flush_due_paste_burst();
                    terminal.draw(|frame| self.draw(frame))?;
                }
            }
            if self.finish_completed_inline_shells().await? {
                terminal.draw(|frame| self.draw(frame))?;
            }
        };
        self.finish_all_inline_shells().await?;
        self.insert_deferred_inline_shell_context(agent)?;
        self.loading_spinner.stop();
        Ok(should_retry && self.goal.is_some() && !self.should_quit)
    }
}

fn initial_goal_prompt(condition: &str) -> String {
    format!(
        "The user invoked Rho's `/goal` command to set the following completion goal. Treat this as a goal-setting action, not as an ordinary conversational message or a claim that the goal is already complete.\n\nGoal:\n{condition}\n\nBegin working toward the goal now. Make concrete progress, use tools as needed, and verify the completion condition before stopping."
    )
}

fn blocked_goal_resumption_prompt(
    condition: &str,
    pending_steps: &[goal::HumanStep],
    user_message: Option<&str>,
) -> String {
    let user_message = user_message
        .map(|message| format!("\n\nThe user's new message is:\n{message}"))
        .unwrap_or_default();
    format!(
        "Resume the following goal after it was blocked on steps requiring the user:\n\n{condition}\n\nPreviously blocked steps:\n{}\n\nFirst verify whether each relevant external condition has changed. Do not repeat implementation work unless verification shows that more agent-actionable work is needed.{user_message}",
        format_human_steps(pending_steps)
    )
}

fn format_human_steps(steps: &[goal::HumanStep]) -> String {
    steps
        .iter()
        .map(|step| format!("- {}: {}", step.action, step.reason))
        .collect::<Vec<_>>()
        .join("\n")
}

pub(super) fn is_goal_clear_alias(value: &str) -> bool {
    matches!(
        value.trim().to_ascii_lowercase().as_str(),
        "clear" | "stop" | "off" | "reset" | "none" | "cancel"
    )
}

fn is_goal_resume_alias(value: &str) -> bool {
    value.trim().eq_ignore_ascii_case("resume")
}

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