Skip to main content

fallow_engine/baseline/
staleness.rs

1//! Staleness of a saved baseline.
2
3use super::*;
4
5impl BaselineStaleness {
6    /// Entries that matched no current finding on this run.
7    #[must_use]
8    pub const fn stale_entries(&self) -> usize {
9        self.entries.saturating_sub(self.matched)
10    }
11
12    /// The advisory warning this run prints on stderr by default.
13    ///
14    /// Silent for a run narrowed to part of the project, because such a run
15    /// legitimately sees only a slice of a whole-project baseline and re-saving
16    /// from it would drop every entry the run never looked at. Silent as well
17    /// when the run produced no findings: a cleaned project and a rotted
18    /// baseline look identical from here, and the re-save advice is wrong when
19    /// the right move is deleting the file.
20    #[must_use]
21    pub const fn warning(&self) -> BaselineStalenessWarning {
22        if self.change_scoped || self.entries == 0 || self.current_findings == 0 {
23            return BaselineStalenessWarning::None;
24        }
25        if self.matched == 0 {
26            return BaselineStalenessWarning::ZeroOverlap;
27        }
28        if stale_share_warrants_warning(self.entries, self.stale_entries()) {
29            return BaselineStalenessWarning::Partial;
30        }
31        BaselineStalenessWarning::None
32    }
33
34    /// Whether the opt-in `--fail-on-stale-baseline` gate fires.
35    ///
36    /// Deliberately stricter than [`Self::warning`]: any stale entry counts.
37    /// The quarter threshold exists to keep an unasked-for line from training
38    /// people to ignore it, and the empty-run silence exists because an
39    /// advisory cannot tell rot from success; a repository that passes the flag
40    /// has asked for both. Change-scope stays the one shared suppression,
41    /// because a narrowed run still cannot judge a whole-project baseline.
42    #[must_use]
43    pub const fn trips_gate(&self) -> bool {
44        stale_baseline_gate_trips(self.entries, self.matched, self.change_scoped)
45    }
46    /// This run's machine-readable view of the baseline, for the JSON envelope.
47    ///
48    /// Every member is derived here rather than in a consumer, so the advisory
49    /// threshold and the gate rule have exactly one implementation. Only
50    /// `health` can follow a file move; the commands that match entries by
51    /// fingerprint pass `0`.
52    ///
53    /// `scope_reasons` and `unrecognised_format` come from the caller because
54    /// only the command knows which channels it read and which format it read
55    /// the file as; this struct carries the counts the analysis needs and
56    /// nothing more. `change_scoped` and `scope_reasons` must agree, which is
57    /// why every caller derives the boolean from the same reason set it passes
58    /// here.
59    ///
60    /// `unrecognised_format` widens `gate_trips` here, at the single site that
61    /// builds it, so the opt-in gate, the exit code, every CI surface that reads
62    /// the boolean and the MCP sentences move together: a file this command
63    /// cannot read as its own suppresses nothing, which no count can express.
64    /// Unlike the count rule it is not suppressed by `change_scoped`, because
65    /// telling a foreign file from this command's own needs no project-wide run.
66    #[must_use]
67    pub fn to_envelope(
68        &self,
69        moved_entries: usize,
70        scope_reasons: fallow_output::BaselineScopeReasons,
71        unrecognised_format: bool,
72    ) -> fallow_output::BaselineStaleness {
73        debug_assert_eq!(
74            self.change_scoped,
75            !scope_reasons.is_empty(),
76            "change_scoped and scope_reasons must be derived from the same predicate"
77        );
78        let warning = self.warning();
79        fallow_output::BaselineStaleness {
80            baseline_entries: self.entries,
81            matched_entries: self.matched,
82            stale_entries: self.stale_entries(),
83            current_findings: self.current_findings,
84            remaining_findings: None,
85            change_scoped: self.change_scoped,
86            stale: warning != BaselineStalenessWarning::None,
87            warning: match warning {
88                BaselineStalenessWarning::None => fallow_output::BaselineStalenessAdvisory::None,
89                BaselineStalenessWarning::ZeroOverlap => {
90                    fallow_output::BaselineStalenessAdvisory::ZeroOverlap
91                }
92                BaselineStalenessWarning::Partial => {
93                    fallow_output::BaselineStalenessAdvisory::Partial
94                }
95            },
96            gate_trips: self.trips_gate() || unrecognised_format,
97            moved_entries,
98            unrecognised_format,
99            saved_by: None,
100            format: None,
101            scope_reasons,
102        }
103    }
104}