rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The person's prompt a turn's run has yet to send the agent, kept in the
//! session's folder until it goes out, so the turn that continues one a
//! daemon restart interrupted (see `restart_resume`) knows whether the agent
//! ever got it.
//!
//! A person's prompt is saved to the session's file, and shown, before its
//! run starts the agent's process; the agent gets it only once that process
//! took the session up. A restart in between leaves a turn the agent never
//! heard of, which "continue where you left off" would lose. No other fact
//! survives the restart to tell: the run's status ends stopped either way,
//! and keeps the agent's session id across turns, so it names that session
//! before the prompt went out too.
//!
//! Each turn's run says what it has yet to send as it starts: a person's
//! prompt is kept until the run is about to send it
//! ([`crate::cli_runtime::status_sink::RunObserver::prompting`]), and a turn
//! the daemon starts clears it, except the one that resends it. So it only
//! ever names the prompt of the last turn, and only a turn the restart cut
//! short before its run sent it leaves one.

use std::path::{Path, PathBuf};

use super::PromptSave;

/// Where in the session's folder the unsent prompt is kept.
const UNSENT_PROMPT_FILE: &str = "unsent-prompt.txt";

/// The session's unsent prompt, in its folder.
pub(super) struct UnsentPrompt(PathBuf);

impl UnsentPrompt {
    /// The unsent prompt of the session whose folder is `artifacts`.
    pub(super) fn in_folder(artifacts: &Path) -> Self {
        Self(artifacts.join(UNSENT_PROMPT_FILE))
    }

    /// Notes, as a turn's run starts, the prompt it has yet to send: `sent`,
    /// for a person's prompt `save` saves first; nothing for one the daemon
    /// starts, unless it resends the one kept. A note that fails only costs
    /// a restart's continuation the person's prompt, so it is logged, not
    /// fatal.
    pub(super) fn track(&self, save: &PromptSave, sent: &str) {
        let written = match save {
            PromptSave::First(_) => {
                crate::config_writer::write_bytes_atomically(&self.0, sent.as_bytes())
            }
            PromptSave::OnceSent(_) => self.clear(),
            PromptSave::Saved => Ok(()),
        };
        if let Err(error) = written {
            tracing::warn!(
                path = %self.0.display(),
                %error,
                "could not note the prompt the turn's run has yet to send"
            );
        }
    }

    /// The prompt the last turn's run never sent, if any.
    pub(super) fn read(&self) -> Option<String> {
        std::fs::read_to_string(&self.0).ok()
    }

    /// The run is about to send its prompt: none is unsent now.
    pub(super) fn sent(&self) {
        if let Err(error) = self.clear() {
            tracing::warn!(
                path = %self.0.display(),
                %error,
                "could not clear the prompt the turn's run sent"
            );
        }
    }

    fn clear(&self) -> std::io::Result<()> {
        match std::fs::remove_file(&self.0) {
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
            removed => removed,
        }
    }
}