Skip to main content

rho_coding_agent/app/
automation.rs

1use std::{
2    fmt,
3    io::{self, Read, Write},
4    num::NonZeroUsize,
5    path::PathBuf,
6    sync::Arc,
7    time::Duration,
8};
9
10use rho_sdk::{SessionOptions, UserInput};
11
12use {
13    crate::cli::{Command, OutputFormat},
14    crate::config::Config,
15    crate::credential_store::AppCredentialStore,
16    crate::diagnostics::RuntimeDiagnostics,
17    crate::herdr::{HerdrReporter, HerdrState},
18    crate::subagent::{RunState, RunStatus},
19    crate::tools::agent::BackgroundSubagents,
20    rho_providers::providers::build_automation_provider,
21};
22
23use super::{
24    agent_binding::BoundAgent,
25    automation_protocol::{write_event, JsonlAdapter, TerminalReason, WireEvent},
26    headless_run::{self, HeadlessRunDeps, HostInputResponder},
27    policy::AppPolicy,
28    runtime_builder::{
29        build_runtime_with_max_steps, configured_context_window, RuntimeBuildOptions,
30    },
31    sdk_config::SdkBootstrapOptions,
32    tools_prompt::{assemble_tools_and_prompt, ToolsAndPromptOptions},
33};
34
35/// Error returned after an automation run has cleaned up and selected a stable exit code.
36#[derive(Debug)]
37pub struct AutomationExit {
38    code: u8,
39    reason: TerminalReason,
40    message: String,
41}
42
43impl AutomationExit {
44    pub(super) fn new(code: u8, reason: TerminalReason, message: impl Into<String>) -> Self {
45        Self {
46            code,
47            reason,
48            message: message.into(),
49        }
50    }
51
52    /// Returns the documented process exit code for this automation result.
53    pub fn exit_code(&self) -> u8 {
54        self.code
55    }
56
57    fn reason(&self) -> TerminalReason {
58        self.reason
59    }
60}
61
62impl fmt::Display for AutomationExit {
63    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
64        formatter.write_str(&self.message)
65    }
66}
67
68impl std::error::Error for AutomationExit {}
69
70/// Error returned after an automation run handles an interrupt and completes cleanup.
71#[derive(Debug)]
72pub struct AutomationInterrupted {
73    signal: ShutdownSignal,
74}
75
76impl AutomationInterrupted {
77    fn new(signal: ShutdownSignal) -> Self {
78        Self { signal }
79    }
80
81    /// Returns the conventional process exit code for the received signal.
82    pub fn exit_code(&self) -> u8 {
83        self.signal.exit_code()
84    }
85}
86
87impl fmt::Display for AutomationInterrupted {
88    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(formatter, "rho run interrupted by {}", self.signal)
90    }
91}
92
93impl std::error::Error for AutomationInterrupted {}
94
95#[derive(Clone, Copy, Debug)]
96enum ShutdownSignal {
97    Interrupt,
98    Terminate,
99}
100
101impl ShutdownSignal {
102    fn exit_code(self) -> u8 {
103        match self {
104            Self::Interrupt => 130,
105            Self::Terminate => 143,
106        }
107    }
108}
109
110impl fmt::Display for ShutdownSignal {
111    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
112        match self {
113            Self::Interrupt => formatter.write_str("SIGINT"),
114            Self::Terminate => formatter.write_str("SIGTERM"),
115        }
116    }
117}
118
119#[derive(Debug)]
120struct SubagentCancelled;
121
122impl fmt::Display for SubagentCancelled {
123    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
124        formatter.write_str("subagent cancellation requested")
125    }
126}
127
128impl std::error::Error for SubagentCancelled {}
129
130pub(super) struct Startup<'a> {
131    pub config: &'a Config,
132    pub config_path: PathBuf,
133    pub cwd: PathBuf,
134    pub no_system_prompt: bool,
135    pub no_tools: bool,
136    pub no_subagents: bool,
137    pub usage_purpose: &'static str,
138    pub parent_session_id: Option<rho_sdk::SessionId>,
139    pub agent: BoundAgent,
140    pub output_file: Option<PathBuf>,
141    pub output: OutputFormat,
142    pub max_steps: Option<NonZeroUsize>,
143    pub timeout: Option<Duration>,
144    pub diagnostics: RuntimeDiagnostics,
145    pub herdr: HerdrReporter,
146    pub host_input: Option<Arc<dyn HostInputResponder>>,
147}
148
149pub(super) fn prompt_for_command(command: &Option<Command>) -> anyhow::Result<Option<String>> {
150    match command {
151        Some(Command::Run { prompt, stdin, .. }) => {
152            prompt_from_stdin(prompt.clone(), *stdin).map(Some)
153        }
154        Some(
155            Command::Attach { .. }
156            | Command::Login { .. }
157            | Command::CredentialStore { .. }
158            | Command::Sessions { .. }
159            | Command::Update,
160        )
161        | None => Ok(None),
162    }
163}
164
165pub(super) fn emit_startup_failure() -> anyhow::Result<()> {
166    let mut adapter = JsonlAdapter::new();
167    let event = adapter.failed(
168        TerminalReason::ConfigurationError,
169        "configuration failed".into(),
170        None,
171    );
172    emit(event)
173}
174
175pub(super) async fn run(prompt_text: String, startup: Startup<'_>) -> anyhow::Result<()> {
176    let mut jsonl = (startup.output == OutputFormat::Jsonl).then(JsonlAdapter::new);
177    let deadline = startup
178        .timeout
179        .map(|timeout| tokio::time::Instant::now() + timeout);
180    // The reporter exists before anything that can fail, so a parent process
181    // watching the output file always sees a terminal state, including startup failures.
182    let reporter_result = startup
183        .output_file
184        .as_ref()
185        .map(|path| {
186            RunReporter::new(
187                path.clone(),
188                RunArtifactIdentity {
189                    agent_id: startup.agent.id().to_string(),
190                    agent_fingerprint: startup.agent.fingerprint().to_string(),
191                    provider: startup.config.provider.clone(),
192                    model: startup.config.model.clone(),
193                },
194                startup.cwd.clone(),
195                &prompt_text,
196                /* stream_output */ startup.output == OutputFormat::Text,
197                None,
198            )
199        })
200        .transpose();
201    let mut reporter = match reporter_result {
202        Ok(reporter) => reporter,
203        Err(error) => {
204            emit_failure(&mut jsonl, TerminalReason::OutputError, &error)?;
205            return Err(
206                AutomationExit::new(1, TerminalReason::OutputError, error.to_string()).into(),
207            );
208        }
209    };
210
211    let cancellation = rho_tools::cancellation::RunCancellation::default();
212    let (result, timed_out) = if let Some(deadline) = deadline {
213        let future = run_session_with_output(
214            prompt_text,
215            &startup,
216            reporter.as_mut(),
217            Some(cancellation.clone()),
218            jsonl.as_mut(),
219        );
220        tokio::pin!(future);
221        tokio::select! {
222            result = &mut future => (result, false),
223            () = tokio::time::sleep_until(deadline) => {
224                cancellation.cancel();
225                (future.await, true)
226            }
227        }
228    } else {
229        (
230            run_session_with_output(
231                prompt_text,
232                &startup,
233                reporter.as_mut(),
234                None,
235                jsonl.as_mut(),
236            )
237            .await,
238            false,
239        )
240    };
241    if let Some(reporter) = reporter.as_mut() {
242        let reached_step_limit = result.as_ref().is_ok_and(|outcome| {
243            outcome.stop_reason() == rho_sdk::StopReason::MaxSteps
244                && (jsonl.is_some() || startup.max_steps.is_some())
245        });
246        if reached_step_limit {
247            let stopped = Err(AutomationExit::new(
248                124,
249                TerminalReason::MaxSteps,
250                "rho run reached its model-step limit",
251            )
252            .into());
253            reporter.finish(&stopped);
254        } else {
255            reporter.finish(&result);
256        }
257    }
258
259    if timed_out {
260        emit_stopped(&mut jsonl, TerminalReason::Timeout)?;
261        return Err(AutomationExit::new(124, TerminalReason::Timeout, "rho run timed out").into());
262    }
263
264    match result {
265        Ok(answer) => {
266            let max_steps = answer.stop_reason() == rho_sdk::StopReason::MaxSteps;
267            if max_steps && (jsonl.is_some() || startup.max_steps.is_some()) {
268                if let Some(adapter) = jsonl.as_mut() {
269                    let text = (!answer.text().is_empty()).then(|| answer.text().into());
270                    let event = adapter.stopped(TerminalReason::MaxSteps, text);
271                    emit(event)?;
272                } else {
273                    write_text_answer(&answer, reporter.is_some())?;
274                }
275                return Err(AutomationExit::new(
276                    124,
277                    TerminalReason::MaxSteps,
278                    "rho run reached its model-step limit",
279                )
280                .into());
281            }
282            if let Some(adapter) = jsonl.as_mut() {
283                let event = adapter.completed(answer.text().into());
284                emit(event)?;
285            } else {
286                write_text_answer(&answer, reporter.is_some())?;
287            }
288            Ok(())
289        }
290        Err(error) => {
291            let (reason, code) = classify_error(&error);
292            if reason == TerminalReason::Interrupted {
293                emit_stopped(&mut jsonl, reason)?;
294            } else if reason != TerminalReason::OutputError {
295                emit_failure(&mut jsonl, reason, &error)?;
296            }
297            let message = terminal_error_message(reason, &error);
298            if error.is::<AutomationInterrupted>() {
299                return Err(error);
300            }
301            Err(AutomationExit::new(code, reason, message).into())
302        }
303    }
304}
305
306fn write_text_answer(answer: &rho_sdk::RunOutcome, has_reporter: bool) -> anyhow::Result<()> {
307    let result = (|| -> io::Result<()> {
308        let mut stdout = io::stdout().lock();
309        if has_reporter {
310            writeln!(stdout, "\n[subagent run complete]")?;
311        } else {
312            writeln!(stdout, "{}", answer.text())?;
313        }
314        stdout.flush()
315    })();
316    result.map_err(|error| {
317        AutomationExit::new(
318            1,
319            TerminalReason::OutputError,
320            format!("could not write output: {error}"),
321        )
322        .into()
323    })
324}
325
326pub(super) fn emit(event: WireEvent) -> anyhow::Result<()> {
327    let mut stdout = io::stdout().lock();
328    write_event(&mut stdout, &event).map_err(|error| {
329        AutomationExit::new(
330            1,
331            TerminalReason::OutputError,
332            format!("could not write JSONL output: {error}"),
333        )
334        .into()
335    })
336}
337
338fn emit_stopped(adapter: &mut Option<JsonlAdapter>, reason: TerminalReason) -> anyhow::Result<()> {
339    if let Some(adapter) = adapter.as_mut() {
340        let text = adapter.partial_text();
341        let event = adapter.stopped(reason, text);
342        emit(event)?;
343    }
344    Ok(())
345}
346
347fn emit_failure(
348    adapter: &mut Option<JsonlAdapter>,
349    reason: TerminalReason,
350    error: &anyhow::Error,
351) -> anyhow::Result<()> {
352    if let Some(adapter) = adapter.as_mut() {
353        let text = adapter.partial_text();
354        let message = terminal_error_message(reason, error);
355        let event = adapter.failed(reason, message, text);
356        emit(event)?;
357    }
358    Ok(())
359}
360
361fn terminal_error_message(reason: TerminalReason, error: &anyhow::Error) -> String {
362    match reason {
363        TerminalReason::Authentication => "authentication failed".to_string(),
364        TerminalReason::ConfigurationError => "configuration failed".to_string(),
365        TerminalReason::OutputError => "output failed".to_string(),
366        TerminalReason::OtherError => "run failed".to_string(),
367        _ => error.to_string(),
368    }
369}
370
371fn classify_error(error: &anyhow::Error) -> (TerminalReason, u8) {
372    if let Some(interrupted) = error.downcast_ref::<AutomationInterrupted>() {
373        return (TerminalReason::Interrupted, interrupted.exit_code());
374    }
375    if let Some(exit) = error.downcast_ref::<AutomationExit>() {
376        return (exit.reason(), exit.exit_code());
377    }
378    for cause in error.chain() {
379        if let Some(error) = cause.downcast_ref::<rho_sdk::Error>() {
380            return match error {
381                rho_sdk::Error::Authentication { .. } => (TerminalReason::Authentication, 1),
382                rho_sdk::Error::Provider(provider)
383                    if provider.kind() == rho_sdk::ProviderErrorKind::Authentication =>
384                {
385                    (TerminalReason::Authentication, 1)
386                }
387                rho_sdk::Error::Provider(_) => (TerminalReason::ProviderError, 1),
388                rho_sdk::Error::Tool(_) => (TerminalReason::ToolHostError, 1),
389                rho_sdk::Error::InvalidConfiguration { .. } => {
390                    (TerminalReason::ConfigurationError, 2)
391                }
392                _ => (TerminalReason::OtherError, 1),
393            };
394        }
395        if let Some(error) = cause.downcast_ref::<rho_providers::model::ModelError>() {
396            use rho_providers::model::ModelError;
397            return match error {
398                ModelError::MissingCredentials(_) | ModelError::Credentials(_) => {
399                    (TerminalReason::Authentication, 1)
400                }
401                ModelError::UnsupportedReasoning { .. } | ModelError::UnsupportedProvider(_) => {
402                    (TerminalReason::ConfigurationError, 2)
403                }
404                _ => (TerminalReason::ProviderError, 1),
405            };
406        }
407    }
408    (TerminalReason::OtherError, 1)
409}
410
411pub(crate) async fn run_session(
412    prompt_text: String,
413    startup: &Startup<'_>,
414    reporter: Option<&mut RunReporter>,
415    cancellation: Option<rho_tools::cancellation::RunCancellation>,
416) -> anyhow::Result<rho_sdk::RunOutcome> {
417    run_session_with_output(prompt_text, startup, reporter, cancellation, None).await
418}
419
420async fn run_session_with_output(
421    prompt_text: String,
422    startup: &Startup<'_>,
423    reporter: Option<&mut RunReporter>,
424    cancellation: Option<rho_tools::cancellation::RunCancellation>,
425    mut jsonl: Option<&mut JsonlAdapter>,
426) -> anyhow::Result<rho_sdk::RunOutcome> {
427    let sdk_options = SdkBootstrapOptions::from_config(startup.config, &startup.cwd)?;
428    let credentials = rho_providers::auth::provider_credentials::ApplicationCredentialSource::new(
429        Arc::new(AppCredentialStore),
430    );
431    let provider = build_automation_provider(sdk_options.provider, &credentials)?;
432    let (tool_set, system_prompt) = assemble_tools_and_prompt(ToolsAndPromptOptions {
433        config: startup.config,
434        config_path: startup.config_path.clone(),
435        cwd: &startup.cwd,
436        no_system_prompt: startup.no_system_prompt,
437        no_tools: startup.no_tools,
438        no_subagents: startup.no_subagents,
439        // Automation keeps questionnaire capability when the agent exposes it.
440        questionnaire_enabled: true,
441        background_subagents: BackgroundSubagents::Disabled,
442        diagnostics: &startup.diagnostics,
443        agent: &startup.agent,
444    })?;
445
446    let workspace_root = sdk_options.workspace.root.clone();
447    let workspace = sdk_options.workspace.build_workspace()?;
448    let context_window = configured_context_window(startup.config);
449    let compaction = sdk_options.runtime.compaction.clone();
450    startup.diagnostics.update_compaction_config(&compaction);
451    let usage_recording = crate::usage::default_recording().await;
452    let runtime = build_runtime_with_max_steps(
453        RuntimeBuildOptions {
454            provider,
455            tools: tool_set.tools(),
456            workspace,
457            workspace_policy: AppPolicy::for_mode(startup.config.permission_mode),
458            approval_handler: None,
459            system_prompt,
460            reasoning: sdk_options.runtime.reasoning,
461            service_tier: sdk_options.runtime.service_tier,
462            compaction,
463            context_window,
464            usage_purpose: startup.usage_purpose,
465            usage_parent_session_id: startup.parent_session_id.clone(),
466            usage_recording,
467        },
468        startup.max_steps,
469    )?;
470    let session = runtime.session(SessionOptions::default()).await?;
471    if let Some(adapter) = jsonl.as_deref_mut() {
472        adapter.set_run_context(session.id(), &workspace_root);
473    }
474    startup
475        .herdr
476        .report_state(HerdrState::Working, None, None)
477        .await;
478    let result = complete_run(
479        &session,
480        prompt_text,
481        HeadlessRunDeps {
482            reporter,
483            external_cancellation: cancellation,
484            jsonl,
485            host_input: startup.host_input.as_deref(),
486        },
487    )
488    .await;
489
490    runtime.shutdown();
491    tool_set.shutdown().await;
492    startup
493        .herdr
494        .report_state(HerdrState::Idle, None, None)
495        .await;
496    startup.herdr.release().await;
497
498    result
499}
500
501async fn complete_run(
502    session: &rho_sdk::Session,
503    prompt_text: String,
504    dependencies: HeadlessRunDeps<'_>,
505) -> anyhow::Result<rho_sdk::RunOutcome> {
506    let HeadlessRunDeps {
507        reporter,
508        external_cancellation,
509        jsonl,
510        host_input,
511    } = dependencies;
512    let mut run = session.start(UserInput::text(prompt_text)).await?;
513    let cancellation = run.cancellation_handle();
514    let external_cancellation = external_cancellation.unwrap_or_default();
515    tokio::select! {
516        outcome = headless_run::drive(&mut run, reporter, jsonl, host_input) => outcome,
517        signal = shutdown_signal() => {
518            let signal = signal?;
519            cancellation.cancel();
520            let _ = run.outcome().await;
521            Err(AutomationInterrupted::new(signal).into())
522        }
523        () = external_cancellation.cancelled() => {
524            cancellation.cancel();
525            let _ = run.outcome().await;
526            Err(SubagentCancelled.into())
527        }
528    }
529}
530
531pub(crate) use crate::run_artifacts::RunArtifactIdentity;
532
533/// Maintains the `--output-file` status contract for subagent runs and
534/// streams progress to stdout so a watching pane shows live activity.
535pub(crate) struct RunReporter {
536    sink: crate::run_artifacts::RunArtifactSink,
537    adapter: crate::tui::event_adapter::SdkEventAdapter,
538    stream_output: bool,
539}
540
541impl RunReporter {
542    pub(crate) fn new(
543        path: PathBuf,
544        identity: RunArtifactIdentity,
545        cwd: PathBuf,
546        prompt: &str,
547        stream_output: bool,
548        status_tx: Option<tokio::sync::watch::Sender<RunStatus>>,
549    ) -> anyhow::Result<Self> {
550        let sink = crate::run_artifacts::RunArtifactSink::open(path, &identity, prompt, status_tx)?;
551        Ok(Self {
552            sink,
553            adapter: crate::tui::event_adapter::SdkEventAdapter::new(cwd),
554            stream_output,
555        })
556    }
557
558    /// Resume after the executor already wrote the Starting boundary.
559    pub(crate) fn continue_from(
560        path: PathBuf,
561        started_status: RunStatus,
562        cwd: PathBuf,
563        prompt: &str,
564        stream_output: bool,
565        status_tx: Option<tokio::sync::watch::Sender<RunStatus>>,
566    ) -> anyhow::Result<Self> {
567        let sink = crate::run_artifacts::RunArtifactSink::continue_from(
568            path,
569            started_status,
570            prompt,
571            status_tx,
572        )?;
573        Ok(Self {
574            sink,
575            adapter: crate::tui::event_adapter::SdkEventAdapter::new(cwd),
576            stream_output,
577        })
578    }
579
580    pub(super) fn on_event(&mut self, event: &rho_sdk::RunEvent) {
581        use rho_sdk::RunEvent;
582
583        let attachments = crate::tui::translate_run_event(&mut self.adapter, event);
584        if !attachments.is_empty() {
585            let mut saw_text_delta = false;
586            let mut needs_immediate_publish = false;
587            for attachment in attachments {
588                match &attachment {
589                    crate::run_artifacts::AttachmentEvent::AssistantTextDelta(text)
590                        if !text.is_empty() =>
591                    {
592                        self.sink.append_last_text(text);
593                        saw_text_delta = true;
594                        self.sink.write_attachment(attachment);
595                    }
596                    _ => {
597                        needs_immediate_publish = true;
598                        self.sink.write_attachment(attachment);
599                    }
600                }
601            }
602            if needs_immediate_publish {
603                self.sink.publish();
604            } else if saw_text_delta {
605                self.sink.publish_throttled();
606            }
607        }
608        match event {
609            RunEvent::StepStarted { step } => {
610                self.sink.status.state = RunState::Running;
611                self.sink.status.turns = *step as u64;
612                self.sink.publish();
613            }
614            RunEvent::ToolStarted { name, .. } => {
615                self.sink.status.last_activity = Some(format!("tool: {name}"));
616                self.stream(&format!("\n[tool] {name}\n"));
617                self.sink.publish();
618            }
619            RunEvent::HostInputRequested { request }
620            | RunEvent::ToolHostInputRequested { request, .. } => {
621                self.sink.status.last_activity =
622                    Some(format!("waiting for questionnaire: {}", request.title()));
623                self.sink.publish();
624            }
625            RunEvent::AssistantTextDelta { text } => {
626                self.sink.status.last_activity = Some("assistant text".into());
627                self.stream(text);
628                // Attachment path already published throttled when translated.
629            }
630            RunEvent::ProviderStreamReset { .. } => {
631                self.sink.status.last_activity = Some("retrying provider response".into());
632                self.sink.status.last_text = None;
633                self.stream("\n[provider response discarded; retrying]\n");
634                self.sink.publish();
635            }
636            RunEvent::UsageUpdated { usage } => {
637                self.sink.status.input_tokens = usage.total_input_tokens();
638                self.sink.status.output_tokens = usage.output_tokens;
639            }
640            _ => {}
641        }
642    }
643
644    #[cfg(test)]
645    pub(crate) fn status(&self) -> &RunStatus {
646        &self.sink.status
647    }
648
649    pub(super) fn write(&mut self) {
650        self.sink.publish();
651    }
652
653    pub(crate) fn finish(&mut self, result: &anyhow::Result<rho_sdk::RunOutcome>) {
654        match result {
655            Ok(outcome) => {
656                let usage = outcome.usage();
657                self.sink.status.input_tokens = usage.total_input_tokens();
658                self.sink.status.output_tokens = usage.output_tokens;
659                self.sink.finish_ok(Some(outcome.text().to_string()));
660            }
661            Err(error)
662                if error.is::<AutomationInterrupted>()
663                    || error.downcast_ref::<AutomationExit>().is_some_and(|exit| {
664                        matches!(
665                            exit.reason(),
666                            TerminalReason::MaxSteps | TerminalReason::Timeout
667                        )
668                    })
669                    || error.is::<SubagentCancelled>() =>
670            {
671                self.sink.finish_stopped("stopped");
672            }
673            Err(error) => {
674                self.sink.finish_error(format!("{error:#}"));
675            }
676        }
677    }
678
679    fn stream(&self, text: &str) {
680        if !self.stream_output {
681            return;
682        }
683        let mut stdout = io::stdout().lock();
684        let _ = stdout.write_all(text.as_bytes());
685        let _ = stdout.flush();
686    }
687}
688
689#[cfg(unix)]
690async fn shutdown_signal() -> io::Result<ShutdownSignal> {
691    use tokio::signal::unix::{signal, SignalKind};
692
693    let mut interrupt = signal(SignalKind::interrupt())?;
694    let mut terminate = signal(SignalKind::terminate())?;
695    tokio::select! {
696        _ = interrupt.recv() => Ok(ShutdownSignal::Interrupt),
697        _ = terminate.recv() => Ok(ShutdownSignal::Terminate),
698    }
699}
700
701#[cfg(not(unix))]
702async fn shutdown_signal() -> io::Result<ShutdownSignal> {
703    tokio::signal::ctrl_c().await?;
704    Ok(ShutdownSignal::Interrupt)
705}
706
707fn prompt_from_stdin(parts: Vec<String>, read_stdin: bool) -> anyhow::Result<String> {
708    prompt_from_reader(parts, read_stdin, &mut io::stdin())
709}
710
711fn prompt_from_reader(
712    parts: Vec<String>,
713    read_stdin: bool,
714    stdin: &mut impl Read,
715) -> anyhow::Result<String> {
716    let mut chunks = Vec::new();
717    let inline = parts.join(" ").trim().to_string();
718    if !inline.is_empty() {
719        chunks.push(inline);
720    }
721    if read_stdin {
722        let mut buffer = String::new();
723        stdin.read_to_string(&mut buffer)?;
724        let buffer = buffer.trim().to_string();
725        if !buffer.is_empty() {
726            chunks.push(buffer);
727        }
728    }
729
730    let prompt = chunks.join("\n\n");
731    if prompt.is_empty() {
732        anyhow::bail!("rho run requires a prompt argument or --stdin");
733    }
734    Ok(prompt)
735}
736
737#[cfg(test)]
738#[path = "automation_tests.rs"]
739mod tests;