rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! How a compaction under way shows to clients: its start, and its end
//! under the id it started with.

use rho_protocol::{CompactionId, CompactionStatus, CompactionTrigger};
use rho_sdk::CompactionOutcome;

use super::LiveEvents;
use crate::app::session_events::WireEvent;

/// How a compaction ended, as [`LiveEvents::compaction_ended`] reports it.
pub(crate) enum CompactionEnd<'a> {
    Completed(&'a CompactionOutcome),
    Cancelled,
    /// Failed with this error, which clients see: never a provider's raw
    /// text, which may echo a credential.
    Failed(String),
}

impl LiveEvents {
    /// A compaction starting: the run's automatic one, or one a host runs
    /// between turns, such as `/compact`, which emits no run events.
    pub(crate) fn compaction_started(&mut self, trigger: CompactionTrigger) -> Vec<WireEvent> {
        let id = fresh_compaction_id();
        self.compaction = Some(id.clone());
        // Called outside [`Self::map`] too: a compaction ends any message.
        self.messages.interrupt();
        vec![WireEvent::Compaction {
            id,
            status: CompactionStatus::Started {
                trigger: Some(trigger),
            },
        }]
    }

    /// The compaction under way ending as `end`, under the id it started
    /// with; a cancel or failure with no compaction under way reports
    /// nothing.
    pub(crate) fn compaction_ended(&mut self, end: CompactionEnd<'_>) -> Vec<WireEvent> {
        let (id, status, usage) = match end {
            CompactionEnd::Completed(outcome) => (
                // A completion whose start was not seen still gets an id.
                Some(self.compaction.take().unwrap_or_else(fresh_compaction_id)),
                CompactionStatus::Completed {
                    counts: crate::compaction::outcome_counts(outcome),
                },
                // The compacted context's fill, as ACP reports it.
                Some(outcome.current_tokens()),
            ),
            CompactionEnd::Cancelled => (self.compaction.take(), CompactionStatus::Cancelled, None),
            CompactionEnd::Failed(error) => (
                self.compaction.take(),
                CompactionStatus::Failed { error },
                None,
            ),
        };
        let Some(id) = id else {
            return Vec::new();
        };
        // Called outside [`Self::map`] too: a compaction ends any message.
        self.messages.interrupt();
        std::iter::once(WireEvent::Compaction { id, status })
            .chain(usage.map(|used| self.fill(used)))
            .collect()
    }
}

/// Each live compaction gets a fresh random id: clients may keep a
/// session's events across daemon restarts, and an id that restarts per
/// process would update an earlier card. A replayed compaction's derives
/// from its saved node instead.
fn fresh_compaction_id() -> CompactionId {
    CompactionId::new(format!("compaction-{}", uuid::Uuid::new_v4()))
}