tak-cli 0.0.18

Pre-v1 command-line benchmarking and performance tracking
Documentation
//! Declared, intentional regressions: which benchmarks a change says it made
//! more expensive on purpose, and who said so.
//!
//! The only other way past a failing gate is `--no-gate`, which stops gating
//! every benchmark at once. A team with one deliberate cost increase then either
//! loosens the threshold for everything or stops requiring the check — both of
//! which leave the next, unintended regression unguarded. An acceptance names
//! one benchmark, and it is always reported with where it came from, so the
//! override is as visible in the report as the regression it covers.
//!
//! Two sources, deliberately no more:
//!
//! - `tak compare --accept`, for a CI integration that maps something outside
//!   the commits — a pull-request label only maintainers can apply, say — onto
//!   an acceptance. This is the one honoured by default, because whoever
//!   decides it is not necessarily whoever wrote the change; and
//! - a `Tak-Accept:` trailer on a commit in the compared range, which is how a
//!   change carries its own justification into history. Opt-in through the
//!   `accept_trailers` setting: those commits are the change being gated, and
//!   by default a change must not be able to waive its own gate.
//!
//! Names only, no bound such as `startup<=5%`. A bound invites arguing about
//! the number in the trailer rather than looking at the change, and a second
//! syntax is a second thing to get wrong in a line nobody tests. An accepted
//! benchmark still shows its full change in the report.

use anyhow::{Result, bail};
use std::collections::{BTreeMap, BTreeSet};

/// The trailer key. git matches trailer keys case-insensitively, so
/// `tak-accept:` works too; this is the spelling the docs and report use.
pub const TRAILER: &str = "Tak-Accept";

/// Where an acceptance came from. Always shown beside it: an override nobody
/// can trace is exactly the kind of quiet gate-weakening this exists to avoid.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Source {
    /// `tak compare --accept`.
    Flag,
    /// A trailer on this commit, by full SHA.
    Trailer(String),
}

impl Source {
    fn describe(&self) -> String {
        match self {
            Source::Flag => "`--accept`".to_string(),
            // Twelve characters, matching how `tak history` names commits.
            Source::Trailer(sha) => {
                format!("`{TRAILER}` in `{}`", &sha[..sha.len().min(12)])
            }
        }
    }
}

/// Accepted benchmark names, each with every source that accepted it.
///
/// Keyed on the benchmark name alone, so an acceptance covers every tool and
/// runner that benchmark was measured with. The name is what a user declares
/// in `tak.toml` and sees in the report; the runner in particular is something
/// a commit author cannot know ahead of time.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Acceptances(BTreeMap<String, BTreeSet<Source>>);

impl Acceptances {
    /// Add one exact name, as `--accept` gives it.
    ///
    /// Not split on commas and not trimmed. Benchmark names are unrestricted,
    /// so `" startup "` is a different benchmark from `startup`, and trimming
    /// would accept the wrong one's regression — waiving a gate nobody asked
    /// to waive. The flag is repeatable, which covers every list without a
    /// separator. An empty value is an error rather than skipped: it is
    /// almost always an unset variable in a CI script, and silently accepting
    /// nothing hides that.
    pub fn add_name(&mut self, name: &str, source: Source) -> Result<()> {
        if name.is_empty() {
            bail!("--accept needs a benchmark name, and was given an empty one");
        }
        self.insert(name, source);
        Ok(())
    }

    fn insert(&mut self, name: &str, source: Source) {
        self.0.entry(name.to_string()).or_default().insert(source);
    }

    /// Parse the output of [`crate::notes::trailers`]: one line per commit,
    /// the SHA and then each trailer value, NUL-separated.
    ///
    /// One trailer names one benchmark, the whole value, exactly as `--accept`
    /// takes one name per flag. It used to be split on commas, which made a
    /// benchmark named `a,b` impossible to accept and `Tak-Accept: a,b` accept
    /// two unrelated ones instead — waiving gates nobody named. Benchmark
    /// names are unrestricted, so no separator inside a value is safe; several
    /// benchmarks take several trailers.
    ///
    /// Nothing else is stripped: `Tak-Accept: startup (new plugin loader)`
    /// names a benchmark that does not exist and is reported as such, which
    /// fails closed — the real `startup` regression still gates — rather than
    /// guessing which word was meant. git has already trimmed the value, so a
    /// trailer cannot name a benchmark with leading or trailing spaces; that
    /// takes `--accept`.
    pub fn add_trailer_log(&mut self, log: &str) {
        for line in log.lines() {
            let mut fields = line.split('\0');
            let Some(sha) = fields.next() else {
                continue;
            };
            // Empty for a commit with no such trailer, and for a bare
            // `Tak-Accept:` — neither names anything.
            for name in fields.filter(|n| !n.is_empty()) {
                self.insert(name, Source::Trailer(sha.to_string()));
            }
        }
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn covers(&self, bench: &str) -> bool {
        self.0.contains_key(bench)
    }

    /// Every accepted name with its sources, in a stable order.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &BTreeSet<Source>)> {
        self.0.iter().map(|(k, v)| (k.as_str(), v))
    }

    /// Where `bench` was accepted from, for the report.
    pub fn describe(&self, bench: &str) -> String {
        self.0
            .get(bench)
            .map(|sources| {
                sources
                    .iter()
                    .map(Source::describe)
                    .collect::<Vec<_>>()
                    .join(", ")
            })
            .unwrap_or_default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const A: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    const B: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";

    /// One trailer, one name. A comma is part of the name: splitting on it
    /// accepted `a` and `b` for a trailer written for the benchmark `a,b`.
    #[test]
    fn a_trailer_value_is_one_name_commas_included() {
        let mut a = Acceptances::default();
        a.add_trailer_log(&format!("{A}\0a,b\0resolve\n"));
        let names: Vec<_> = a.iter().map(|(n, _)| n).collect();
        assert_eq!(names, ["a,b", "resolve"]);
        assert!(!a.covers("a"));
        assert!(!a.covers("b"));
    }

    /// A commit with no trailer still prints its SHA; it must add nothing
    /// rather than an empty name that could never match and would be reported.
    #[test]
    fn a_commit_without_the_trailer_accepts_nothing() {
        let mut a = Acceptances::default();
        a.add_trailer_log(&format!("{A}\0\n{B}\0startup\n"));
        assert!(a.covers("startup"));
        assert_eq!(a.iter().count(), 1);
        assert_eq!(a.describe("startup"), "`Tak-Accept` in `bbbbbbbbbbbb`");
    }

    /// Every source is kept, not just the first: the report has to name all
    /// of them, or removing one trailer could silently leave another in force.
    #[test]
    fn every_source_of_an_acceptance_is_kept() {
        let mut a = Acceptances::default();
        a.add_trailer_log(&format!("{A}\0startup\0resolve\n{B}\0startup\n"));
        a.add_name("startup", Source::Flag).unwrap();
        assert_eq!(
            a.describe("startup"),
            "`--accept`, `Tak-Accept` in `aaaaaaaaaaaa`, `Tak-Accept` in `bbbbbbbbbbbb`"
        );
        assert_eq!(a.describe("resolve"), "`Tak-Accept` in `aaaaaaaaaaaa`");
    }

    /// The flag takes a name as given, so a benchmark whose name contains a
    /// comma can still be accepted.
    #[test]
    fn a_flag_value_is_one_name() {
        let mut a = Acceptances::default();
        a.add_name("parse a,b", Source::Flag).unwrap();
        assert!(a.covers("parse a,b"));
        assert_eq!(a.iter().count(), 1);
    }

    /// Exact means exact: surrounding spaces are part of the name, and
    /// dropping them would accept a different benchmark.
    #[test]
    fn a_flag_value_is_not_trimmed() {
        let mut a = Acceptances::default();
        a.add_name(" startup ", Source::Flag).unwrap();
        assert!(a.covers(" startup "));
        assert!(!a.covers("startup"));
    }

    #[test]
    fn an_empty_flag_value_is_an_error() {
        let mut a = Acceptances::default();
        assert!(a.add_name("", Source::Flag).is_err());
        assert!(a.is_empty());
    }

    /// A reason written after the name is not quietly dropped. The whole value
    /// is the name, which matches nothing, so the regression still gates and
    /// the report says the acceptance named no benchmark.
    #[test]
    fn a_reason_in_the_value_is_not_guessed_at() {
        let mut a = Acceptances::default();
        a.add_trailer_log(&format!("{A}\0startup (new loader)\n"));
        assert!(!a.covers("startup"));
        assert!(a.covers("startup (new loader)"));
    }
}