rho-coding-agent 3.11.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 crate::cli_runtime::CliPrompt;

use super::PromptSave;

/// Where in the session's folder the unsent prompt is kept.
const UNSENT_PROMPT_FILE: &str = "unsent-prompt.json";
/// Legacy plain-text unsent prompt file written before multimodal prompts.
const LEGACY_UNSENT_PROMPT_FILE: &str = "unsent-prompt.txt";

/// The session's unsent prompt, in its folder.
pub(super) struct UnsentPrompt {
    path: PathBuf,
    legacy_path: PathBuf,
}

impl UnsentPrompt {
    /// The unsent prompt of the session whose folder is `artifacts`.
    pub(super) fn in_folder(artifacts: &Path) -> Self {
        Self {
            path: artifacts.join(UNSENT_PROMPT_FILE),
            legacy_path: artifacts.join(LEGACY_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: &CliPrompt) {
        let written = match save {
            PromptSave::First(_) => serde_json::to_vec(sent)
                .map_err(std::io::Error::other)
                .and_then(|bytes| crate::config_writer::write_bytes_atomically(&self.path, &bytes))
                .and_then(|()| remove_if_present(&self.legacy_path)),
            PromptSave::OnceSent(_) => self.clear(),
            PromptSave::Saved => Ok(()),
        };
        if let Err(error) = written {
            tracing::warn!(
                path = %self.path.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<CliPrompt> {
        if let Ok(bytes) = std::fs::read(&self.path) {
            if let Ok(prompt) = serde_json::from_slice::<CliPrompt>(&bytes) {
                return Some(prompt);
            }
        }
        std::fs::read_to_string(&self.legacy_path)
            .ok()
            .map(CliPrompt::text)
    }

    /// 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.path.display(),
                %error,
                "could not clear the prompt the turn's run sent"
            );
        }
    }

    fn clear(&self) -> std::io::Result<()> {
        remove_if_present(&self.path)?;
        remove_if_present(&self.legacy_path)
    }
}

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