rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The countdown a questionnaire shows while the daemon would answer it
//! with the agent's fallback at a deadline. The form never submits the
//! fallback itself: an answer it sent would count as the user's own.

use std::time::{Duration, Instant};

use super::QuestionnaireComposer;

/// Once touched, a form stays paused until explicit submit or cancel. There is
/// no idle-resume timer that could submit a user's partially edited answers.
#[derive(Debug)]
pub(super) enum QuestionnaireTimer {
    Running {
        opened: Instant,
        duration: Duration,
        /// What the form continues with: its fallback answers by label.
        continuing: String,
        /// The agent's assumption, when the daemon reports one.
        assumption: Option<String>,
    },
    Paused,
}

impl QuestionnaireComposer {
    /// Counts down `remaining` until the daemon answers with the request's
    /// fallback. Any interaction pauses it.
    pub(in crate::tui) fn count_down_to_fallback(&mut self, remaining: Duration, now: Instant) {
        let continuing = self.request.timeout_fallback().map_or_else(
            || "the agent's answers".to_owned(),
            |fallback| {
                crate::questionnaire::fallback_summary(self.request.questions(), fallback.answers())
            },
        );
        self.timeout = Some(QuestionnaireTimer::Running {
            opened: now,
            duration: remaining,
            continuing,
            assumption: self.request.timeout_reason().map(str::to_owned),
        });
    }

    pub(in crate::tui) fn observe_input(&mut self, event: &crossterm::event::Event) {
        use crossterm::event::Event;
        if matches!(event, Event::Key(_) | Event::Paste(_) | Event::Mouse(_)) {
            self.pause_timeout();
        }
    }

    /// Deferred paste can edit a newly opened form without another terminal
    /// event. Keep the edit guard with the composer, not in shared paste helpers.
    pub(in crate::tui) fn pause_timeout(&mut self) {
        if self.timeout.is_some() {
            self.timeout = Some(QuestionnaireTimer::Paused);
        }
    }

    pub(in crate::tui) fn timeout_running(&self) -> bool {
        matches!(self.timeout, Some(QuestionnaireTimer::Running { .. }))
    }

    /// The countdown line at `now`, with the fallback it continues with,
    /// and the agent's assumption to show under it while it runs; `None`
    /// for a form that does not count down.
    pub(super) fn timeout_notice(&self, now: Instant) -> Option<(String, Option<&str>)> {
        match self.timeout.as_ref()? {
            QuestionnaireTimer::Running {
                opened,
                duration,
                continuing,
                assumption,
            } => {
                let remaining = duration.saturating_sub(now.saturating_duration_since(*opened));
                let seconds = remaining
                    .as_secs()
                    .saturating_add(u64::from(remaining.subsec_nanos() > 0));
                let clock = format!("{}:{:02}", seconds / 60, seconds % 60);
                Some((
                    format!("Continuing with {continuing} in {clock}; any key pauses"),
                    assumption.as_deref(),
                ))
            }
            QuestionnaireTimer::Paused => Some((
                "Countdown paused; submit or cancel to continue".into(),
                None,
            )),
        }
    }
}

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