brake 2026.11.3

A brake on breaking API changes. Checks OpenAPI, protobuf and GraphQL contracts for backward compatibility, hermetically.
Documentation
//! Machine-readable output, one object per finding.
//!
//! Key order is stable because `serde_json::json!` preserves declaration order
//! and every collection reaching here is already sorted. Byte-stability across
//! runs is guarantee G4.

use serde_json::json;

use crate::report::Report;
use crate::rules::{Finding, catalogue};

/// One finding, as every machine-readable surface shows it.
///
/// `--format json` emits this, and every MCP tool starts from it. A field
/// added here reaches both, which is the point: CI and an agent read the same
/// finding. See `design/09-one-core-three-surfaces.md`.
#[must_use]
pub fn finding(finding: &Finding) -> serde_json::Value {
    let span = finding.span.as_ref();
    json!({
        "rule": finding.rule_id,
        "severity": severity_label(finding.severity),
        "contract": finding.contract,
        "method": finding.method,
        "path": finding.path,
        "pointer": finding.pointer,
        "file": span.map(|span| span.file.clone()),
        "line": span.map(|span| span.line),
        "column": span.map(|span| span.column),
        "message": finding.message,
        "subject": finding.subject,
        // Ways to make the same change without breaking a consumer,
        // most direct first. Structured rather than prose so a
        // consumer can act on one without parsing English.
        "remediation": finding
            .remediations()
            .into_iter()
            .map(|remediation| json!({
                "strategy": remediation.strategy,
                "summary": remediation.summary,
                "cost": remediation.cost,
            }))
            .collect::<Vec<_>>(),
        "help_uri": catalogue::lookup(finding.rule_id).map(catalogue::Rule::help_uri),
        // Declared consumers this finding is evidence against, with
        // the interaction that says so. Empty when no consumer is
        // declared *and* when none is affected: `note` is what tells
        // the two apart.
        "affects": finding
            .affects
            .iter()
            .map(|reference| json!({
                "consumer": reference.consumer,
                "source": reference.source,
                "line": reference.span.line,
            }))
            .collect::<Vec<_>>(),
        "note": finding.note,
    })
}

pub fn render(report: &Report) -> String {
    let findings = report.findings.iter().map(finding).collect::<Vec<_>>();

    let unavailable = report
        .unavailable
        .iter()
        .map(|item| {
            json!({
                "contract": item.contract,
                "message": item.message,
            })
        })
        .collect::<Vec<_>>();

    let payload = json!({
        "contracts_checked": report.contracts_checked,
        "findings": findings,
        "unavailable": unavailable,
    });

    format!("{payload}\n")
}

fn severity_label(severity: crate::Severity) -> &'static str {
    match severity {
        crate::Severity::Info => "info",
        crate::Severity::Warning => "warning",
        crate::Severity::Error => "error",
    }
}

#[cfg(test)]
mod tests {
    use crate::Severity;
    use crate::contract::Span;
    use crate::report::{Report, Unavailable};
    use crate::rules::Finding;

    use super::render;

    fn report() -> Report {
        Report::new(
            vec![Finding {
                affects: Vec::new(),
                note: None,
                rule_id: "response-field-removed",
                severity: Severity::Error,
                contract: "payments".to_owned(),
                message: "response field removed: field `customer_id`".to_owned(),
                method: Some("GET".to_owned()),
                path: Some("/payments/{id}".to_owned()),
                pointer: "/paths/~1payments~1{id}/get/responses/200/customer_id".to_owned(),
                subject: Some("customer_id".to_owned()),
                span: Some(Span::new("api/openapi.yaml", 142, 9, "/paths")),
            }],
            vec![Unavailable {
                contract: Some("ledger".to_owned()),
                message: "baseline missing".to_owned(),
            }],
            2,
        )
    }

    #[test]
    fn emits_the_documented_key_set() {
        let value: serde_json::Value =
            serde_json::from_str(&render(&report())).expect("valid JSON");
        let finding = &value["findings"][0];

        for key in [
            "rule", "severity", "contract", "method", "path", "pointer", "file", "line", "message",
        ] {
            assert!(!finding[key].is_null(), "missing documented key `{key}`");
        }
        assert_eq!(finding["rule"], "response-field-removed");
        assert_eq!(finding["contract"], "payments");
        assert_eq!(finding["file"], "api/openapi.yaml");
        assert_eq!(finding["line"], 142);
    }

    #[test]
    fn names_the_contract_so_findings_are_attributable() {
        let value: serde_json::Value =
            serde_json::from_str(&render(&report())).expect("valid JSON");
        assert_eq!(value["findings"][0]["contract"], "payments");
        assert_eq!(value["unavailable"][0]["contract"], "ledger");
        assert_eq!(value["contracts_checked"], 2);
    }

    #[test]
    fn a_breaking_finding_carries_the_ways_out() {
        let value: serde_json::Value =
            serde_json::from_str(&render(&report())).expect("valid JSON");
        let remediation = &value["findings"][0]["remediation"];

        assert!(remediation.is_array(), "remediation must always be present");
        let first = &remediation[0];
        assert_eq!(first["strategy"], "deprecate-then-remove");
        assert!(
            first["summary"]
                .as_str()
                .expect("summary")
                .contains("`customer_id`"),
            "the strategy must name the field it is about: {first}"
        );
        // A list of options with no costs reads as though they are all free.
        assert!(first["cost"].as_str().is_some_and(|cost| !cost.is_empty()));
        assert_eq!(value["findings"][0]["subject"], "customer_id");
    }

    #[test]
    fn output_is_byte_stable() {
        assert_eq!(render(&report()), render(&report()));
    }
}