dpc-tau-cli 0.2.0

A minimal Unix-first coding agent.
Documentation
//! Shared per-agent projection and compact estimated API cost presentation.

use tau_proto::EstimatedApiCost;

/// Complete self and authenticated-creator-subtree costs from one stats
/// snapshot.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct AgentCostSnapshot {
    /// Cost directly incurred by this agent.
    self_cost: EstimatedApiCost,
    /// Inclusive cost incurred by this agent and its authenticated descendants.
    creator_subtree_cost: EstimatedApiCost,
}

impl AgentCostSnapshot {
    /// Builds a complete cost pair from a harness-owned stats snapshot.
    pub(crate) fn new(self_cost: EstimatedApiCost, creator_subtree_cost: EstimatedApiCost) -> Self {
        Self {
            self_cost,
            creator_subtree_cost,
        }
    }
}

/// Thread-safe latest canonical cumulative cost projection keyed by agent.
#[derive(Clone, Default)]
pub(crate) struct AgentCostProjection {
    /// Latest complete harness-authored cost pair keyed by canonical agent id.
    costs: std::sync::Arc<
        std::sync::Mutex<std::collections::HashMap<tau_proto::AgentId, AgentCostSnapshot>>,
    >,
}

impl AgentCostProjection {
    /// Records the latest complete harness-authored cost pair for an agent.
    pub(crate) fn record(&self, agent_id: tau_proto::AgentId, costs: AgentCostSnapshot) {
        crate::locked(&self.costs).insert(agent_id, costs);
    }

    /// Clears all costs when the renderer changes sessions.
    pub(crate) fn clear(&self) {
        crate::locked(&self.costs).clear();
    }

    /// Takes a coherent snapshot for one picker interaction.
    pub(crate) fn snapshot(
        &self,
    ) -> std::collections::HashMap<tau_proto::AgentId, AgentCostSnapshot> {
        crate::locked(&self.costs).clone()
    }
}

const PICODOLLARS_PER_CENT: u64 = 10_000_000_000;
const PICODOLLARS_PER_TENTH_DOLLAR: u64 = 100_000_000_000;
const PICODOLLARS_PER_DOLLAR: u64 = 1_000_000_000_000;
const PICODOLLARS_PER_THOUSAND_DOLLARS: u64 = 1_000 * PICODOLLARS_PER_DOLLAR;
const PICODOLLARS_PER_TENTH_MILLION_DOLLARS: u64 = 100_000 * PICODOLLARS_PER_DOLLAR;
const PICODOLLARS_PER_MILLION_DOLLARS: u64 = 1_000_000 * PICODOLLARS_PER_DOLLAR;

/// Format an estimated cost as `$` plus at most three characters.
///
/// Values round half up at each representation boundary: cents below one
/// dollar, tenths below ten dollars, whole dollars below one thousand, whole
/// thousands below one hundred thousand, tenths of a million below one million,
/// then whole millions. A rounded boundary promotes to the next representation.
#[must_use]
pub(crate) fn format_compact(cost: EstimatedApiCost) -> String {
    let picodollars = cost.as_picodollars();
    let cents = rounded_units(picodollars, PICODOLLARS_PER_CENT);
    if cents < 100 {
        return format!("$.{cents:02}");
    }
    let tenths = rounded_units(picodollars, PICODOLLARS_PER_TENTH_DOLLAR);
    if tenths < 100 {
        return format!("${}.{}", tenths / 10, tenths % 10);
    }
    let dollars = rounded_units(picodollars, PICODOLLARS_PER_DOLLAR);
    if dollars < 1_000 {
        return format!("${dollars}");
    }
    let thousands = rounded_units(picodollars, PICODOLLARS_PER_THOUSAND_DOLLARS);
    if thousands < 100 {
        return format!("${thousands}k");
    }
    let tenths_of_millions = rounded_units(picodollars, PICODOLLARS_PER_TENTH_MILLION_DOLLARS);
    if tenths_of_millions < 10 {
        return format!("$.{tenths_of_millions}m");
    }
    let millions = rounded_units(picodollars, PICODOLLARS_PER_MILLION_DOLLARS);
    format!("${millions}m")
}

/// Renders independently compact-formatted self and inclusive costs.
#[must_use]
pub(crate) fn format_compact_pair(
    self_cost: Option<EstimatedApiCost>,
    creator_subtree_cost: Option<EstimatedApiCost>,
) -> String {
    match (self_cost, creator_subtree_cost) {
        (Some(self_cost), Some(creator_subtree_cost)) => format!(
            "{}/{}",
            format_compact(self_cost),
            format_compact(creator_subtree_cost)
        ),
        (Some(self_cost), None) => format!("{}/-", format_compact(self_cost)),
        (None, _) => "-/-".to_owned(),
    }
}

/// Renders a complete cost snapshot or the unavailable pair.
#[must_use]
pub(crate) fn format_snapshot(costs: Option<AgentCostSnapshot>) -> String {
    let (self_cost, creator_subtree_cost) = costs
        .map(|costs| (Some(costs.self_cost), Some(costs.creator_subtree_cost)))
        .unwrap_or((None, None));
    format_compact_pair(self_cost, creator_subtree_cost)
}

fn rounded_units(value: u64, unit: u64) -> u64 {
    u64::try_from((u128::from(value) + u128::from(unit / 2)) / u128::from(unit)).unwrap_or(u64::MAX)
}

#[cfg(test)]
mod tests;