Cotton code
101 lines · 3.9 KB · RustRaw
//! What `/activity` reads that no row stores — what a run stopped on and//! spent, and whether an assistant's call was refused — as typed facts, said//! in the database's words (line set `activity`, `rules::shell_lines`).//!//! A run's `task`, `receipt` and `error` are the server's own sentences,//! composed once and stamped on the row; they are carried verbatim and never//! re-composed. What is NOT on the row is the `spent` line (`stopped_reason`//! and `cost_usd` together) and a word for an audit row's `ok` (`outcome`),//! for the generic reads of the `assistant_run` and `assistant_call` tables//! (`cotton_tools::mcp::handlers::tables::activity_tables`).use super::shell_lines::{resolve, LineRow};/// The line set these words live in.pub const SET: &str = "activity";/// The `outcome` of a call that succeeded.pub const CALL_OK: &str = "ok";/// The `outcome` of a call that was refused.pub const CALL_REFUSED: &str = "refused";/// What a run stopped on. `answered` (and nothing) is no fact: a run that/// already says Done needs no stopping word, and a ceiling is not a failure.#[derive(Debug, Clone, PartialEq, Eq)]pub enum Stopped {    TurnCeiling,    TimeCeiling,    NoKey,    ModelRefused,    /// A reason this build has no line for, said as the server wrote it    /// rather than dropped.    Unnamed(String),}impl Stopped {    pub fn of(reason: Option<&str>) -> Option<Stopped> {        let reason = reason.map(str::trim).filter(|reason| !reason.is_empty())?;        Some(match reason {            "answered" => return None,            "turn_ceiling" => Stopped::TurnCeiling,            "time_ceiling" => Stopped::TimeCeiling,            "no_key" => Stopped::NoKey,            "model_refused" => Stopped::ModelRefused,            other => Stopped::Unnamed(other.to_string()),        })    }    /// The fact in the database's words.    pub fn say(&self, rows: &[LineRow]) -> String {        let line = match self {            Stopped::TurnCeiling => "stopped.turn_ceiling",            Stopped::TimeCeiling => "stopped.time_ceiling",            Stopped::NoKey => "stopped.no_key",            Stopped::ModelRefused => "stopped.model_refused",            Stopped::Unnamed(reason) => return reason.clone(),        };        resolve(SET, line, rows, &[]).unwrap_or_default()    }}/// A metered cost in dollars as money: two decimals once there is a cent,/// four below it — "$0.00" beside a key the owner pays with would read as/// free. A null, zero, negative or non-finite cost says nothing: nobody/// costed it, which is not the same as free.pub fn cost_words(cost_usd: Option<f64>) -> Option<String> {    let cost = cost_usd.filter(|cost| cost.is_finite() && *cost > 0.0)?;    Some(if cost < 0.01 {        format!("${cost:.4}")    } else {        format!("${cost:.2}")    })}/// A run's `spent` line in the database's words: what it stopped on, what it/// spent, or both — `None` when neither says anything.pub fn spent_line(stopped_reason: Option<&str>, cost_usd: Option<f64>, rows: &[LineRow]) -> Option<String> {    let stopped = Stopped::of(stopped_reason).map(|stopped| stopped.say(rows));    let cost = cost_words(cost_usd);    match (stopped, cost) {        (None, None) => None,        (Some(stopped), None) => Some(stopped),        (None, Some(cost)) => resolve(SET, "spent.cost", rows, &[("cost", &cost)]),        (Some(stopped), Some(cost)) => {            resolve(SET, "spent.stopped_and_cost", rows, &[("stopped", &stopped), ("cost", &cost)])        }    }}/// An audit row's `ok` as the `outcome` choice. An absent `ok` reads as/// succeeded: the transport writes a real flag on every row, and a value gone/// missing must not accuse a connection.pub fn call_outcome(ok: Option<i64>) -> &'static str {    match ok {        Some(0) => CALL_REFUSED,        _ => CALL_OK,    }}#[cfg(test)]#[path = "activity_words_tests.rs"]mod tests;