Skip to main content

fallow_cli/
impact.rs

1//! Fallow Impact: local, opt-in value reporting.
2
3use std::path::{Path, PathBuf};
4
5use fallow_types::envelope::Meta;
6use fallow_types::results::{ActiveSuppression, AnalysisResults};
7use rustc_hash::{FxHashMap, FxHashSet};
8use serde::{Deserialize, Serialize};
9
10use crate::audit::{AuditSummary, AuditVerdict};
11use crate::report::ci::fingerprint::fingerprint_hash;
12use crate::report::format_display_path;
13
14const STORE_SCHEMA_VERSION: u32 = 5;
15
16const MAX_RECORDS: usize = 200;
17
18const MAX_CONTAINMENT: usize = 200;
19
20const TREND_TOLERANCE: i64 = 0;
21
22const STORE_FILE: &str = "impact.json";
23
24/// Env var: when set to a positive integer N, a recorded run opportunistically
25/// removes per-project store files whose file mtime is older than N days,
26/// reclaiming stores left behind by deleted repos. Unset / `0` / unparseable
27/// disables the sweep (default: keep every store forever).
28const STORE_MAX_AGE_ENV: &str = "FALLOW_IMPACT_STORE_MAX_AGE_DAYS";
29
30const MAX_RECENT_RESOLVED: usize = 50;
31
32const ID_SEP: &str = "\u{1f}";
33
34const CODE_DUPLICATION_KIND: &str = "code-duplication";
35
36const BLANKET_SUPPRESSION: &str = "*";
37
38/// Per-category issue counts captured at a recorded run.
39#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
40#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
41pub struct ImpactCounts {
42    pub total_issues: usize,
43    pub dead_code: usize,
44    pub complexity: usize,
45    pub duplication: usize,
46}
47
48impl ImpactCounts {
49    fn from_summary(summary: &AuditSummary) -> Self {
50        Self {
51            total_issues: summary.dead_code_issues
52                + summary.complexity_findings
53                + summary.duplication_clone_groups,
54            dead_code: summary.dead_code_issues,
55            complexity: summary.complexity_findings,
56            duplication: summary.duplication_clone_groups,
57        }
58    }
59
60    pub(crate) fn from_combined(dead_code: usize, complexity: usize, duplication: usize) -> Self {
61        Self {
62            total_issues: dead_code + complexity + duplication,
63            dead_code,
64            complexity,
65            duplication,
66        }
67    }
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct ImpactRecord {
72    pub timestamp: String,
73    pub version: String,
74    #[serde(default, skip_serializing_if = "Option::is_none")]
75    pub git_sha: Option<String>,
76    pub verdict: String,
77    #[serde(default)]
78    pub gate: bool,
79    pub counts: ImpactCounts,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct PendingContainment {
84    pub blocked_at: String,
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub git_sha: Option<String>,
87    pub blocked_counts: ImpactCounts,
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize)]
91#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
92pub struct ContainmentEvent {
93    pub blocked_at: String,
94    pub cleared_at: String,
95    #[serde(default, skip_serializing_if = "Option::is_none")]
96    pub git_sha: Option<String>,
97    pub blocked_counts: ImpactCounts,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct FrontierFinding {
102    pub id: String,
103    pub kind: String,
104    #[serde(default, skip_serializing_if = "Option::is_none")]
105    pub symbol: Option<String>,
106}
107
108impl FrontierFinding {
109    fn move_key(&self) -> String {
110        match &self.symbol {
111            Some(symbol) => format!("{}{ID_SEP}{symbol}", self.kind),
112            None => self.id.clone(),
113        }
114    }
115}
116
117#[derive(Debug, Clone, Default, Serialize, Deserialize)]
118pub struct FileFrontier {
119    #[serde(default)]
120    pub findings: Vec<FrontierFinding>,
121    #[serde(default)]
122    pub suppressions: Vec<String>,
123}
124
125#[derive(Debug, Clone, Serialize, Deserialize)]
126#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
127pub struct ResolutionEvent {
128    pub kind: String,
129    pub path: String,
130    #[serde(default, skip_serializing_if = "Option::is_none")]
131    pub symbol: Option<String>,
132    #[serde(default, skip_serializing_if = "Option::is_none")]
133    pub git_sha: Option<String>,
134    pub timestamp: String,
135}
136
137#[derive(Debug, Clone, Default, Serialize, Deserialize)]
138pub struct ImpactStore {
139    #[serde(default)]
140    pub schema_version: u32,
141    #[serde(default)]
142    pub enabled: bool,
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    pub first_recorded: Option<String>,
145    #[serde(default)]
146    pub records: Vec<ImpactRecord>,
147    #[serde(default)]
148    pub project_records: Vec<ImpactRecord>,
149    #[serde(default)]
150    pub containment: Vec<ContainmentEvent>,
151    #[serde(default, skip_serializing_if = "Option::is_none")]
152    pub pending_containment: Option<PendingContainment>,
153    /// Per-finding attribution baseline, namespaced by worktree key (schema v4)
154    /// so two worktrees of one repo (collapsed to a single store) do not prune
155    /// each other's per-file frontier. Inner map is rel-path -> findings.
156    #[serde(default)]
157    pub frontier: FxHashMap<String, FxHashMap<String, FileFrontier>>,
158    /// Clone-family attribution baseline, namespaced by worktree key (schema
159    /// v4). Inner map is clone fingerprint -> instance paths.
160    #[serde(default)]
161    pub clone_frontier: FxHashMap<String, FxHashMap<String, Vec<String>>>,
162    #[serde(default)]
163    pub resolved_total: usize,
164    #[serde(default)]
165    pub suppressed_total: usize,
166    #[serde(default)]
167    pub recent_resolved: Vec<ResolutionEvent>,
168    #[serde(default)]
169    pub onboarding_declined: bool,
170    /// Whether the user ever ran an explicit `impact enable` or `impact
171    /// disable`. Distinguishes "deliberately declined" from "never asked" so
172    /// the agent skill asks for the impact opt-in exactly once per project.
173    #[serde(default)]
174    pub explicit_decision: bool,
175    /// Unix epoch seconds when the periodic impact digest was last surfaced
176    /// (the `impact-report` next-step / human one-liner). Internal cadence
177    /// state, never exposed on the report.
178    #[serde(default, skip_serializing_if = "Option::is_none")]
179    pub last_digest_epoch: Option<u64>,
180    /// Repo display name (the git-toplevel BASENAME only, never a full path),
181    /// captured at record time so the cross-repo `fallow impact --all` view can
182    /// label rows legibly without reversing the opaque project-key hash. Absent
183    /// on pre-v5 stores (rows fall back to the short key) and on stores written
184    /// by older builds. v5.
185    #[serde(default, skip_serializing_if = "Option::is_none")]
186    pub label: Option<String>,
187}
188
189/// Deserialize-only view of a pre-relocation in-repo store (schema <= 3), whose
190/// `frontier` / `clone_frontier` were FLAT (not worktree-namespaced). Used once
191/// during migration to import a legacy `.fallow/impact.json` into the user
192/// store. Every field carries `#[serde(default)]` so any of v1/v2/v3 reads.
193#[derive(Debug, Default, Deserialize)]
194struct LegacyFlatStore {
195    #[serde(default)]
196    enabled: bool,
197    #[serde(default)]
198    first_recorded: Option<String>,
199    #[serde(default)]
200    records: Vec<ImpactRecord>,
201    #[serde(default)]
202    project_records: Vec<ImpactRecord>,
203    #[serde(default)]
204    containment: Vec<ContainmentEvent>,
205    #[serde(default)]
206    pending_containment: Option<PendingContainment>,
207    #[serde(default)]
208    frontier: FlatFrontier,
209    #[serde(default)]
210    clone_frontier: FlatCloneFrontier,
211    #[serde(default)]
212    resolved_total: usize,
213    #[serde(default)]
214    suppressed_total: usize,
215    #[serde(default)]
216    recent_resolved: Vec<ResolutionEvent>,
217    #[serde(default)]
218    onboarding_declined: bool,
219    #[serde(default)]
220    explicit_decision: bool,
221    #[serde(default)]
222    last_digest_epoch: Option<u64>,
223}
224
225impl LegacyFlatStore {
226    /// Convert into the current (v4) store, wrapping the flat frontier under the
227    /// importing worktree's key.
228    fn into_store(self, worktree_key: &str) -> ImpactStore {
229        let mut frontier: FxHashMap<String, FlatFrontier> = FxHashMap::default();
230        if !self.frontier.is_empty() {
231            frontier.insert(worktree_key.to_owned(), self.frontier);
232        }
233        let mut clone_frontier: FxHashMap<String, FlatCloneFrontier> = FxHashMap::default();
234        if !self.clone_frontier.is_empty() {
235            clone_frontier.insert(worktree_key.to_owned(), self.clone_frontier);
236        }
237        ImpactStore {
238            schema_version: STORE_SCHEMA_VERSION,
239            enabled: self.enabled,
240            first_recorded: self.first_recorded,
241            records: self.records,
242            project_records: self.project_records,
243            containment: self.containment,
244            pending_containment: self.pending_containment,
245            frontier,
246            clone_frontier,
247            resolved_total: self.resolved_total,
248            suppressed_total: self.suppressed_total,
249            recent_resolved: self.recent_resolved,
250            onboarding_declined: self.onboarding_declined,
251            explicit_decision: self.explicit_decision,
252            last_digest_epoch: self.last_digest_epoch,
253            // Legacy stores carry no label; the next recorded run backfills it.
254            label: None,
255        }
256    }
257}
258
259/// Process-global memo of `(project_key, worktree_key)` per analyzed root, so
260/// the git subprocesses that derive them run at most once per root per run
261/// (`fallow audit` is the perf-priority path and `load` is called several
262/// times per invocation).
263/// `(project_key, worktree_key, display_name)` for a root.
264type ProjectIdentity = (String, String, Option<String>);
265
266static IDENTITY_CACHE: std::sync::OnceLock<std::sync::Mutex<FxHashMap<PathBuf, ProjectIdentity>>> =
267    std::sync::OnceLock::new();
268
269/// Hash a filesystem-path identity into a stable key. On case-insensitive
270/// filesystems (macOS APFS default, Windows) two spellings of one directory map
271/// to the same on-disk location, so fold case before hashing to keep the key
272/// stable across spellings. Linux paths are case-sensitive and left as-is.
273fn hash_path_identity(path: &Path) -> String {
274    let raw = path.to_string_lossy();
275    let normalized = if cfg!(any(target_os = "macos", target_os = "windows")) {
276        raw.to_lowercase()
277    } else {
278        raw.into_owned()
279    };
280    fingerprint_hash(&[normalized.as_str()])
281}
282
283/// Resolve `resolved` to an existing absolute path, falling back to the
284/// canonicalized `root` and finally the raw `root`. Shared by the project key
285/// (git common dir) and the worktree key (git toplevel) so both keep identical
286/// non-git fallback behavior.
287fn resolve_or_root(resolved: Option<PathBuf>, root: &Path) -> PathBuf {
288    resolved
289        .or_else(|| dunce::canonicalize(root).ok())
290        .unwrap_or_else(|| root.to_path_buf())
291}
292
293/// The repo's display name for cross-repo rows: the folder that owns the shared
294/// `.git` (stable across worktrees), else the directory's own basename. This is
295/// a BASENAME only (e.g. `fallow`), never a full path, so persisting it does not
296/// reintroduce the absolute path the store relocation deliberately dropped.
297fn repo_basename(common_or_dir: &Path) -> Option<String> {
298    let dir = if common_or_dir.file_name().is_some_and(|n| n == ".git") {
299        common_or_dir.parent()?
300    } else {
301        common_or_dir
302    };
303    dir.file_name().map(|n| n.to_string_lossy().into_owned())
304}
305
306/// Resolve (and memoize) the `(project_key, worktree_key, display_name)` for
307/// `root`. `project_key` collapses all worktrees of a repo onto one identity via
308/// the git common dir (falling back to the canonicalized root for non-git);
309/// `worktree_key` is the per-tree toplevel (namespaces the attribution frontier
310/// so concurrent worktrees do not prune each other's baseline); `display_name`
311/// is the repo's basename for legible cross-repo rows. All three derive from a
312/// single common-dir + toplevel resolution so the git subprocesses run at most
313/// once per root per run (`fallow audit` is the perf-priority path).
314fn project_identity(root: &Path) -> ProjectIdentity {
315    let cache = IDENTITY_CACHE.get_or_init(|| std::sync::Mutex::new(FxHashMap::default()));
316    if let Ok(map) = cache.lock()
317        && let Some(found) = map.get(root)
318    {
319        return found.clone();
320    }
321    let common = resolve_or_root(
322        fallow_core::changed_files::resolve_git_common_dir(root).ok(),
323        root,
324    );
325    let toplevel = resolve_or_root(
326        fallow_core::changed_files::resolve_git_toplevel(root).ok(),
327        root,
328    );
329    let identity = (
330        hash_path_identity(&common),
331        hash_path_identity(&toplevel),
332        repo_basename(&common),
333    );
334    if let Ok(mut map) = cache.lock() {
335        map.insert(root.to_path_buf(), identity.clone());
336    }
337    identity
338}
339
340#[cfg(test)]
341thread_local! {
342    /// Per-test override of the user config dir, so parallel tests get isolated
343    /// stores (the real config dir is process-global and would collide). Set via
344    /// [`with_test_config_dir`]; unset = fall back to the real config dir.
345    static TEST_CONFIG_DIR: std::cell::RefCell<Option<PathBuf>> =
346        const { std::cell::RefCell::new(None) };
347
348    /// Per-test CI signal for the record gate. Defaults to `false` so the unit
349    /// tests record into their isolated store EVEN when the suite itself runs on
350    /// CI (GitHub Actions sets `CI` / `GITHUB_ACTIONS`, which `telemetry::is_ci`
351    /// reads); without this, every record-dependent test fails on CI because the
352    /// real `is_ci()` short-circuits `record_*` before any store write. A test
353    /// can flip it true to exercise the CI no-op gate. Thread-local, so it is
354    /// parallel-safe and needs no unsafe env mutation.
355    static TEST_FORCE_CI: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
356}
357
358/// Fallow's per-user config dir. Under test it resolves ONLY the per-test
359/// override (or `None` when unset), so a test never reads or writes the real
360/// developer config dir and parallel tests stay isolated.
361fn impact_config_dir() -> Option<PathBuf> {
362    #[cfg(test)]
363    {
364        TEST_CONFIG_DIR.with(|c| c.borrow().clone())
365    }
366    #[cfg(not(test))]
367    {
368        crate::telemetry::config_dir()
369    }
370}
371
372/// Whether this run should be treated as CI for the Impact record gate. In
373/// production it is `telemetry::is_ci()`; under test it reads the per-test
374/// `TEST_FORCE_CI` override (default `false`) so the suite records into its
375/// isolated store regardless of the ambient CI env. The store path is ALWAYS
376/// the per-test temp dir under `#[cfg(test)]` (see [`impact_config_dir`]), so
377/// bypassing the CI gate in tests can never touch a real user store.
378fn record_gate_is_ci() -> bool {
379    #[cfg(test)]
380    {
381        TEST_FORCE_CI.with(std::cell::Cell::get)
382    }
383    #[cfg(not(test))]
384    {
385        crate::telemetry::is_ci()
386    }
387}
388
389/// Path to the per-project store file in the user's private config dir, or
390/// `None` when no config dir is resolvable (e.g. stripped CI env), in which
391/// case Impact is inert (no persistence). NEVER writes into the analyzed repo.
392fn store_path(root: &Path) -> Option<PathBuf> {
393    let (project_key, _, _) = project_identity(root);
394    Some(
395        impact_config_dir()?
396            .join("impact")
397            .join(format!("{project_key}.json")),
398    )
399}
400
401/// Path to a project's legacy in-repo store (`<root>/.fallow/impact.json`),
402/// read ONCE for migration into the user store, never written.
403fn legacy_store_path(root: &Path) -> PathBuf {
404    root.join(".fallow").join(STORE_FILE)
405}
406
407/// Load the store. Missing or unreadable files fall back to defaults; unreadable
408/// files are warned about rather than silently disabling tracking.
409pub fn load(root: &Path) -> ImpactStore {
410    let Some(path) = store_path(root) else {
411        return ImpactStore::default();
412    };
413    match std::fs::read_to_string(&path) {
414        Ok(content) => parse_store(&content, &path),
415        // No user-store file yet: attempt a one-time import of a legacy in-repo
416        // `.fallow/impact.json` (pre-relocation). Returns default if none.
417        Err(_) => migrate_legacy_store(root),
418    }
419}
420
421fn parse_store(content: &str, path: &Path) -> ImpactStore {
422    match serde_json::from_str::<ImpactStore>(content) {
423        Ok(store) => {
424            if store.schema_version > STORE_SCHEMA_VERSION {
425                tracing::warn!(
426                    "fallow impact: store at {} has schema_version {} but this build understands up to {}; reading it as best-effort, fields this build does not know are dropped on the next write. Upgrade fallow to read it fully.",
427                    path.display(),
428                    store.schema_version,
429                    STORE_SCHEMA_VERSION,
430                );
431            }
432            store
433        }
434        Err(err) => {
435            tracing::warn!(
436                "fallow impact: ignoring unreadable store at {} ({err}); run `fallow impact enable` to reset it",
437                path.display()
438            );
439            ImpactStore::default()
440        }
441    }
442}
443
444/// Persist the store best-effort using atomic replace. No-op when no config dir
445/// is resolvable (e.g. stripped CI env). NEVER writes into the analyzed repo.
446fn save(store: &ImpactStore, root: &Path) {
447    let Some(path) = store_path(root) else {
448        return;
449    };
450    if let Some(parent) = path.parent()
451        && std::fs::create_dir_all(parent).is_err()
452    {
453        return;
454    }
455    if let Ok(json) = serde_json::to_string_pretty(store) {
456        let _ = fallow_config::atomic_write(&path, json.as_bytes());
457    }
458}
459
460/// The advisory-lock sidecar path for a store file (`<store>.json.lock`).
461fn lock_path_for(store: &Path) -> PathBuf {
462    let mut raw = store.as_os_str().to_owned();
463    raw.push(".lock");
464    PathBuf::from(raw)
465}
466
467/// Advisory lock serialising the load -> mutate -> save critical section of an
468/// Impact record across concurrent `fallow` processes.
469///
470/// Two worktrees of the same repo collapse to the SAME store key (and SAME
471/// store path), so a pre-commit gate firing in both at once would otherwise
472/// lost-update each other (A loads, B loads, A saves, B saves => A's record is
473/// dropped). Records BLOCK on this lock so a contended run is serialised rather
474/// than dropped. The `.lock` sidecar is intentionally never deleted (an
475/// unlinked-but-locked inode plus a racer's `O_CREAT` would split the lock
476/// across two inodes); the kernel releases the lock when the handle drops,
477/// including at process exit, so an abandoned record never wedges the next run.
478struct ImpactStoreLock {
479    _file: std::fs::File,
480}
481
482impl ImpactStoreLock {
483    /// Block until the per-project store lock for `root` is held. Best-effort:
484    /// returns `None` (proceed unlocked) when no store path resolves or the lock
485    /// file cannot be opened/locked, so a lock-layer failure never drops a
486    /// record (it only loses the cross-worktree serialisation guarantee).
487    fn acquire(root: &Path) -> Option<Self> {
488        let lock_path = lock_path_for(&store_path(root)?);
489        if let Some(parent) = lock_path.parent()
490            && std::fs::create_dir_all(parent).is_err()
491        {
492            return None;
493        }
494        let file = std::fs::OpenOptions::new()
495            .create(true)
496            .truncate(false)
497            .write(true)
498            .open(&lock_path)
499            .ok()?;
500        match file.lock() {
501            Ok(()) => Some(Self { _file: file }),
502            Err(err) => {
503                tracing::debug!(error = %err, "could not acquire impact store lock");
504                None
505            }
506        }
507    }
508}
509
510/// Resolve the store-GC window from [`STORE_MAX_AGE_ENV`]. `None` (no sweep)
511/// when unset, `0`, or unparseable.
512fn resolve_store_max_age() -> Option<std::time::Duration> {
513    let raw = std::env::var(STORE_MAX_AGE_ENV).ok()?;
514    let days: u32 = raw.trim().parse().ok()?;
515    crate::base_worktree::days_to_duration(days)
516}
517
518/// Remove per-project store files older than `max_age`, reclaiming stores left
519/// behind by deleted repos. Age is the store FILE's mtime (any recorded run
520/// rewrites the file via atomic replace, refreshing the mtime), so an
521/// actively-tracked repo never ages out. Best-effort and opportunistic (called
522/// after a successful record), gated entirely on [`STORE_MAX_AGE_ENV`]. Never
523/// deletes `.lock` sidecars (the lock-lifecycle invariant) and never the global
524/// `impact.json` toggle (it is a sibling FILE one level up, not inside the
525/// `impact/` dir). Skips `keep_key`'s own store so the just-written file is
526/// never reclaimed by a stale-mtime race in the same run.
527///
528/// Cross-project GC race: this sweep does NOT take the per-store
529/// [`ImpactStoreLock`] of the OTHER projects it inspects, so in principle it
530/// could delete a store another process is mid-writing. This is a bounded,
531/// best-effort limitation rather than a corruption bug. A store becomes a
532/// deletion candidate only when its file mtime is already older than
533/// `max_age`, and any record refreshes the mtime via an atomic replace, so a
534/// repo with even occasional activity never ages out. The one genuinely lossy
535/// window is sub-millisecond: a concurrent writer that completes its atomic
536/// replace AFTER this fn's `metadata().modified()` read but BEFORE the
537/// `remove_file` would have its just-written (fresh) record deleted. That
538/// window is vanishingly small, the deletion is opt-in (gated on
539/// [`STORE_MAX_AGE_ENV`]), and the store is reconstructed on the project's
540/// next recorded run, so the worst case is the loss of a single just-written
541/// record for an otherwise-dormant project, never partial/corrupt state.
542fn sweep_old_stores(keep_key: &str, max_age: std::time::Duration) {
543    let Some(dir) = store_dir() else {
544        return;
545    };
546    let Ok(entries) = std::fs::read_dir(&dir) else {
547        return;
548    };
549    let now = std::time::SystemTime::now();
550    for entry in entries.flatten() {
551        let path = entry.path();
552        // Only `<key>.json` store files; `.lock` sidecars have a `lock`
553        // extension and are skipped (never deleted).
554        if path.extension().and_then(|e| e.to_str()) != Some("json") {
555            continue;
556        }
557        if path.file_stem().and_then(|s| s.to_str()) == Some(keep_key) {
558            continue;
559        }
560        let aged_out = std::fs::metadata(&path)
561            .and_then(|m| m.modified())
562            .ok()
563            .and_then(|mtime| now.duration_since(mtime).ok())
564            .is_some_and(|age| age >= max_age);
565        if aged_out {
566            let _ = std::fs::remove_file(&path);
567        }
568    }
569}
570
571/// One-time import of a pre-relocation in-repo `.fallow/impact.json` into the
572/// user store. The legacy store had a FLAT frontier (schema <= 3); this reads
573/// it via [`LegacyFlatStore`] and wraps the flat frontier under the current
574/// worktree key. The legacy file is left byte-for-byte untouched (it is no
575/// longer read once the user store exists; re-running finds the user store and
576/// does not re-import). Monorepo note: N subdir stores share one repo key, so
577/// whichever subdir runs first wins (pick-first); the others are not merged.
578fn migrate_legacy_store(root: &Path) -> ImpactStore {
579    let legacy_path = legacy_store_path(root);
580    let Ok(content) = std::fs::read_to_string(&legacy_path) else {
581        return ImpactStore::default();
582    };
583    let Ok(legacy) = serde_json::from_str::<LegacyFlatStore>(&content) else {
584        return ImpactStore::default();
585    };
586    let (_, worktree, display) = project_identity(root);
587    let mut store = legacy.into_store(&worktree);
588    // Backfill the cross-repo display label on migration so the imported repo
589    // is legible in `impact --all` without waiting for its next recorded run.
590    store.label = display;
591    save(&store, root);
592    store
593}
594
595/// Enable Impact tracking for THIS project (an explicit per-repo decision that
596/// overrides the user-global default). Writes nothing into the repo: the store
597/// lives in the user config dir.
598pub fn enable(root: &Path) -> bool {
599    let mut store = load(root);
600    let was_enabled = store.enabled;
601    store.enabled = true;
602    store.explicit_decision = true;
603    if store.schema_version == 0 {
604        store.schema_version = STORE_SCHEMA_VERSION;
605    }
606    save(&store, root);
607    !was_enabled
608}
609
610/// Disable Impact tracking. Retains existing history. Returns whether it was
611/// newly disabled (false if already off). Also records the explicit decision,
612/// so declining the impact opt-in on a never-enabled project (`impact
613/// disable`) persists "asked and said no" for the agent skill.
614pub fn disable(root: &Path) -> bool {
615    let mut store = load(root);
616    let was_enabled = store.enabled;
617    store.enabled = false;
618    store.explicit_decision = true;
619    if store.schema_version == 0 {
620        store.schema_version = STORE_SCHEMA_VERSION;
621    }
622    save(&store, root);
623    was_enabled
624}
625
626/// A due periodic value digest: the headline counters for "what has fallow
627/// done for you here". Returned by [`take_due_digest`] at most once per
628/// `DIGEST_INTERVAL_SECS` per project.
629#[derive(Debug, Clone, Copy)]
630pub struct ImpactDigest {
631    pub containment_count: usize,
632    pub resolved_total: usize,
633}
634
635/// Minimum seconds between periodic digest surfacings (one week).
636const DIGEST_INTERVAL_SECS: u64 = 7 * 24 * 60 * 60;
637
638/// Return the periodic value digest when it is due, stamping the store so the
639/// next one is at least `DIGEST_INTERVAL_SECS` away. Due means: tracking is
640/// enabled, there is non-zero value to report (anti-nag: a zero digest never
641/// surfaces), and the previous digest is older than the interval (or never
642/// happened). Best-effort like the rest of the store: a clean run that drops
643/// the emitted step simply defers the digest to the next interval.
644pub fn take_due_digest(root: &Path) -> Option<ImpactDigest> {
645    let mut store = load(root);
646    if !resolve_enabled(&store).0 {
647        return None;
648    }
649    let containment_count = store.containment.len();
650    if containment_count == 0 && store.resolved_total == 0 {
651        return None;
652    }
653    let now = std::time::SystemTime::now()
654        .duration_since(std::time::UNIX_EPOCH)
655        .ok()?
656        .as_secs();
657    if let Some(last) = store.last_digest_epoch
658        && now.saturating_sub(last) < DIGEST_INTERVAL_SECS
659    {
660        return None;
661    }
662    store.last_digest_epoch = Some(now);
663    save(&store, root);
664    Some(ImpactDigest {
665        containment_count,
666        resolved_total: store.resolved_total,
667    })
668}
669
670/// Persist that the local user declined the agent onboarding prompt. Writes
671/// only to the user store; nothing is written into the repo.
672pub fn decline_onboarding(root: &Path) -> bool {
673    let mut store = load(root);
674    let was_declined = store.onboarding_declined;
675    store.onboarding_declined = true;
676    if store.schema_version == 0 {
677        store.schema_version = STORE_SCHEMA_VERSION;
678    }
679    save(&store, root);
680    !was_declined
681}
682
683/// Why Impact tracking is (or is not) active for a project. `Project` = an
684/// explicit per-repo `enable`; `User` = the user-global default with no per-repo
685/// decision; `Default` = off (no per-repo decision and no global default).
686#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
687#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
688#[serde(rename_all = "lowercase")]
689pub enum EnabledSource {
690    Project,
691    User,
692    Default,
693}
694
695/// The user-global Impact default, stored at `<config-dir>/fallow/impact.json`
696/// (sibling to `telemetry.json`). A single toggle: when on, new projects record
697/// without a per-repo `enable`. A per-repo explicit decision always wins.
698#[derive(Debug, Default, Serialize, Deserialize)]
699struct GlobalImpactConfig {
700    #[serde(default)]
701    default_enabled: bool,
702}
703
704fn global_config_path() -> Option<PathBuf> {
705    Some(impact_config_dir()?.join(STORE_FILE))
706}
707
708/// Whether the user-global default is on. False when unset or unreadable.
709fn load_global_default() -> bool {
710    let Some(path) = global_config_path() else {
711        return false;
712    };
713    std::fs::read_to_string(&path)
714        .ok()
715        .and_then(|c| serde_json::from_str::<GlobalImpactConfig>(&c).ok())
716        .is_some_and(|c| c.default_enabled)
717}
718
719/// Set the user-global default. Returns whether the value changed.
720pub fn set_global_default(on: bool) -> bool {
721    let was = load_global_default();
722    if let Some(path) = global_config_path() {
723        if let Some(parent) = path.parent()
724            && std::fs::create_dir_all(parent).is_err()
725        {
726            return false;
727        }
728        let config = GlobalImpactConfig {
729            default_enabled: on,
730        };
731        if let Ok(json) = serde_json::to_string_pretty(&config) {
732            let _ = fallow_config::atomic_write(&path, json.as_bytes());
733        }
734    }
735    was != on
736}
737
738/// Resolve whether Impact is active for this project and WHY. Precedence:
739/// per-repo decision (enable/disable) > user-global default > off.
740///
741/// `enabled == true` is itself an explicit project opt-in (somebody ran
742/// `enable` here), so it wins even when `explicit_decision` is unset, which is
743/// the case for stores written before the `explicit_decision` field existed. A
744/// store that is off-but-explicitly-decided (`!enabled && explicit_decision`)
745/// stays off as a Project decision (the user disabled it here). Only a truly
746/// never-asked store (`!enabled && !explicit_decision`) consults the global
747/// default.
748fn resolve_enabled(store: &ImpactStore) -> (bool, EnabledSource) {
749    if store.enabled {
750        return (true, EnabledSource::Project);
751    }
752    if store.explicit_decision {
753        return (false, EnabledSource::Project);
754    }
755    if load_global_default() {
756        return (true, EnabledSource::User);
757    }
758    (false, EnabledSource::Default)
759}
760
761/// The resolved per-project store-file path for `root`, for `status` display
762/// (so a wrong key is debuggable). `None` when no config dir is resolvable.
763#[must_use]
764pub fn resolved_store_path(root: &Path) -> Option<PathBuf> {
765    store_path(root)
766}
767
768/// The resolved (worktree-collapsed) project key for `root`, for display.
769#[must_use]
770pub fn resolved_project_key(root: &Path) -> String {
771    project_identity(root).0
772}
773
774/// The per-project store directory (`<config-dir>/fallow/impact/`), for the
775/// `impact --all` human discoverability footer. `None` when no config dir.
776#[must_use]
777pub fn store_dir() -> Option<PathBuf> {
778    impact_config_dir().map(|d| d.join("impact"))
779}
780
781/// Delete THIS project's store file. Returns whether a file was removed.
782pub fn reset(root: &Path) -> bool {
783    store_path(root).is_some_and(|p| std::fs::remove_file(&p).is_ok())
784}
785
786/// Delete the whole per-project impact dir (`<config-dir>/fallow/impact/`).
787/// Does NOT touch the global default toggle (`impact.json`): a data wipe should
788/// not silently re-disable an opt-in the user made. Returns whether the dir was
789/// present and removed.
790pub fn reset_all() -> bool {
791    let Some(dir) = impact_config_dir().map(|d| d.join("impact")) else {
792        return false;
793    };
794    dir.is_dir() && std::fs::remove_dir_all(&dir).is_ok()
795}
796
797/// Record an audit run into the rolling store.
798pub struct AuditRunRecord<'a> {
799    pub verdict: AuditVerdict,
800    pub gate: bool,
801    pub git_sha: Option<&'a str>,
802    pub version: &'a str,
803    pub timestamp: &'a str,
804    pub attribution: Option<&'a AttributionInput<'a>>,
805}
806
807pub fn record_audit_run(root: &Path, summary: &AuditSummary, record: &AuditRunRecord<'_>) {
808    let AuditRunRecord {
809        verdict,
810        gate,
811        git_sha,
812        version,
813        timestamp,
814        attribution,
815    } = record;
816    // Impact is a LOCAL-DEV signal. Never record in CI: a user-global default
817    // baked into a devcontainer/dotfiles image would otherwise start writing
818    // per-project files on every CI run (pre-relocation this was emergent from
819    // a fresh CI checkout having no in-repo store file; now it is explicit).
820    if record_gate_is_ci() {
821        return;
822    }
823    // Serialise the load -> mutate -> save window so two worktrees of the same
824    // repo (same store key) cannot lost-update each other's record.
825    let _lock = ImpactStoreLock::acquire(root);
826    let mut store = load(root);
827    if !resolve_enabled(&store).0 {
828        return;
829    }
830    store.schema_version = STORE_SCHEMA_VERSION;
831    // Capture the repo basename for the cross-repo view (memoized; no extra git
832    // probe). Refreshed each run so a renamed repo folder updates its label.
833    store.label = project_identity(root).2;
834
835    let counts = ImpactCounts::from_summary(summary);
836    let verdict_str = verdict_label(*verdict);
837
838    if store.first_recorded.is_none() {
839        store.first_recorded = Some((*timestamp).to_owned());
840    }
841
842    apply_containment(&mut store, *verdict, *gate, *git_sha, timestamp, &counts);
843
844    store.records.push(ImpactRecord {
845        timestamp: (*timestamp).to_owned(),
846        version: (*version).to_owned(),
847        git_sha: git_sha.map(ToOwned::to_owned),
848        verdict: verdict_str.to_owned(),
849        gate: *gate,
850        counts,
851    });
852    compact(&mut store);
853
854    if let Some(attribution) = attribution {
855        let (_, worktree, _) = project_identity(root);
856        apply_attribution(&mut store, attribution, &worktree, *git_sha, timestamp);
857    }
858
859    save(&store, root);
860    if let Some(max_age) = resolve_store_max_age() {
861        sweep_old_stores(&project_identity(root).0, max_age);
862    }
863}
864
865/// Record a whole-project combined run into the project track.
866pub fn record_combined_run(
867    root: &Path,
868    counts: ImpactCounts,
869    git_sha: Option<&str>,
870    version: &str,
871    timestamp: &str,
872    attribution: Option<&AttributionInput<'_>>,
873) {
874    if record_gate_is_ci() {
875        return;
876    }
877    let _lock = ImpactStoreLock::acquire(root);
878    let mut store = load(root);
879    if !resolve_enabled(&store).0 {
880        return;
881    }
882    store.schema_version = STORE_SCHEMA_VERSION;
883    store.label = project_identity(root).2;
884
885    if store.first_recorded.is_none() {
886        store.first_recorded = Some(timestamp.to_owned());
887    }
888
889    let verdict_str = if counts.total_issues == 0 {
890        "pass"
891    } else {
892        "warn"
893    };
894    store.project_records.push(ImpactRecord {
895        timestamp: timestamp.to_owned(),
896        version: version.to_owned(),
897        git_sha: git_sha.map(ToOwned::to_owned),
898        verdict: verdict_str.to_owned(),
899        gate: false,
900        counts,
901    });
902    if store.project_records.len() > MAX_RECORDS {
903        let overflow = store.project_records.len() - MAX_RECORDS;
904        store.project_records.drain(0..overflow);
905    }
906
907    if let Some(attribution) = attribution {
908        let (_, worktree, _) = project_identity(root);
909        apply_attribution(&mut store, attribution, &worktree, git_sha, timestamp);
910    }
911
912    save(&store, root);
913    if let Some(max_age) = resolve_store_max_age() {
914        sweep_old_stores(&project_identity(root).0, max_age);
915    }
916}
917
918/// Update pending/contained state from a gate run's verdict.
919fn apply_containment(
920    store: &mut ImpactStore,
921    verdict: AuditVerdict,
922    gate: bool,
923    git_sha: Option<&str>,
924    timestamp: &str,
925    counts: &ImpactCounts,
926) {
927    if !gate {
928        return;
929    }
930    if verdict == AuditVerdict::Fail {
931        if store.pending_containment.is_none() {
932            store.pending_containment = Some(PendingContainment {
933                blocked_at: timestamp.to_owned(),
934                git_sha: git_sha.map(ToOwned::to_owned),
935                blocked_counts: counts.clone(),
936            });
937        }
938    } else if let Some(pending) = store.pending_containment.take() {
939        store.containment.push(ContainmentEvent {
940            blocked_at: pending.blocked_at,
941            cleared_at: timestamp.to_owned(),
942            git_sha: pending.git_sha,
943            blocked_counts: pending.blocked_counts,
944        });
945        if store.containment.len() > MAX_CONTAINMENT {
946            let overflow = store.containment.len() - MAX_CONTAINMENT;
947            store.containment.drain(0..overflow);
948        }
949    }
950}
951
952fn compact(store: &mut ImpactStore) {
953    if store.records.len() > MAX_RECORDS {
954        let overflow = store.records.len() - MAX_RECORDS;
955        store.records.drain(0..overflow);
956    }
957}
958
959#[derive(Debug, Clone)]
960pub struct FindingInput {
961    pub path: PathBuf,
962    pub kind: &'static str,
963    pub symbol: Option<String>,
964}
965
966#[derive(Debug, Clone)]
967pub struct CloneInput {
968    pub fingerprint: String,
969    pub instance_paths: Vec<PathBuf>,
970}
971
972pub enum Scope<'a> {
973    ChangedFiles(&'a [PathBuf]),
974    WholeProject,
975}
976
977pub struct AttributionInput<'a> {
978    pub root: &'a Path,
979    pub scope: Scope<'a>,
980    pub findings: Vec<FindingInput>,
981    pub clones: Vec<CloneInput>,
982    pub suppressions: &'a [ActiveSuppression],
983}
984
985fn finding_id(kind: &str, rel_path: &str, symbol: Option<&str>) -> String {
986    fingerprint_hash(&[kind, rel_path, symbol.unwrap_or("")])
987}
988
989fn covered_by(present: &FxHashSet<String>, kind: &str) -> bool {
990    present.contains(BLANKET_SUPPRESSION) || present.contains(kind)
991}
992
993/// A single worktree's flat per-file attribution baseline (rel-path -> findings).
994type FlatFrontier = FxHashMap<String, FileFrontier>;
995/// A single worktree's flat clone baseline (fingerprint -> instance paths).
996type FlatCloneFrontier = FxHashMap<String, Vec<String>>;
997
998fn apply_attribution(
999    store: &mut ImpactStore,
1000    input: &AttributionInput<'_>,
1001    worktree_key: &str,
1002    git_sha: Option<&str>,
1003    timestamp: &str,
1004) {
1005    let root = input.root;
1006    // Pull THIS worktree's baseline out of the (repo-collapsed) store into owned
1007    // flat locals. The helpers mutate these locals plus the shared totals on
1008    // `store`; because the locals are owned (not borrowed from `store`) there is
1009    // no aliasing with the `store.resolved_total` / `recent_resolved` writes.
1010    let mut frontier: FlatFrontier = store.frontier.remove(worktree_key).unwrap_or_default();
1011    let mut clone_frontier: FlatCloneFrontier = store
1012        .clone_frontier
1013        .remove(worktree_key)
1014        .unwrap_or_default();
1015
1016    let changed: FxHashSet<String> = match input.scope {
1017        Scope::ChangedFiles(files) => files.iter().map(|p| format_display_path(p, root)).collect(),
1018        Scope::WholeProject => whole_project_scope(&frontier, &clone_frontier, input, root),
1019    };
1020
1021    let mut current_findings: FxHashMap<String, Vec<FrontierFinding>> = FxHashMap::default();
1022    for f in &input.findings {
1023        let rel = format_display_path(&f.path, root);
1024        if !changed.contains(&rel) {
1025            continue;
1026        }
1027        let id = finding_id(f.kind, &rel, f.symbol.as_deref());
1028        current_findings
1029            .entry(rel)
1030            .or_default()
1031            .push(FrontierFinding {
1032                id,
1033                kind: f.kind.to_owned(),
1034                symbol: f.symbol.clone(),
1035            });
1036    }
1037    let mut current_supps: FxHashMap<String, FxHashSet<String>> = FxHashMap::default();
1038    for s in input.suppressions {
1039        let rel = format_display_path(&s.path, root);
1040        if !changed.contains(&rel) {
1041            continue;
1042        }
1043        let key = s
1044            .kind
1045            .clone()
1046            .unwrap_or_else(|| BLANKET_SUPPRESSION.to_owned());
1047        current_supps.entry(rel).or_default().insert(key);
1048    }
1049
1050    let mut appeared_move_keys: FxHashSet<String> = FxHashSet::default();
1051    for (rel, findings) in &current_findings {
1052        let prior_ids: FxHashSet<&str> = frontier
1053            .get(rel)
1054            .map(|f| f.findings.iter().map(|x| x.id.as_str()).collect())
1055            .unwrap_or_default();
1056        for ff in findings {
1057            if !prior_ids.contains(ff.id.as_str()) {
1058                appeared_move_keys.insert(ff.move_key());
1059            }
1060        }
1061    }
1062
1063    uncredit_cross_run_moves(store, &appeared_move_keys);
1064
1065    let mut disappearance_input = FileDisappearancesInput {
1066        store,
1067        frontier: &frontier,
1068        changed: &changed,
1069        current_findings: &current_findings,
1070        current_supps: &current_supps,
1071        appeared_move_keys: &appeared_move_keys,
1072        git_sha,
1073        timestamp,
1074    };
1075    classify_file_disappearances(&mut disappearance_input);
1076    update_file_frontier(&mut frontier, &changed, current_findings, current_supps);
1077    classify_clone_disappearances(
1078        store,
1079        &frontier,
1080        &mut clone_frontier,
1081        input,
1082        &changed,
1083        git_sha,
1084        timestamp,
1085    );
1086    prune_frontier(&mut frontier, &mut clone_frontier, root);
1087    bound_recent_resolved(store);
1088
1089    // Store this worktree's baseline back; drop the worktree key entirely when
1090    // empty so deleted/abandoned worktrees do not accumulate.
1091    if frontier.is_empty() {
1092        store.frontier.remove(worktree_key);
1093    } else {
1094        store.frontier.insert(worktree_key.to_owned(), frontier);
1095    }
1096    if clone_frontier.is_empty() {
1097        store.clone_frontier.remove(worktree_key);
1098    } else {
1099        store
1100            .clone_frontier
1101            .insert(worktree_key.to_owned(), clone_frontier);
1102    }
1103}
1104
1105fn whole_project_scope(
1106    frontier: &FlatFrontier,
1107    clone_frontier: &FlatCloneFrontier,
1108    input: &AttributionInput<'_>,
1109    root: &Path,
1110) -> FxHashSet<String> {
1111    let mut set: FxHashSet<String> = frontier.keys().cloned().collect();
1112    for paths in clone_frontier.values() {
1113        for p in paths {
1114            set.insert(p.clone());
1115        }
1116    }
1117    for f in &input.findings {
1118        set.insert(format_display_path(&f.path, root));
1119    }
1120    for c in &input.clones {
1121        for p in &c.instance_paths {
1122            set.insert(format_display_path(p, root));
1123        }
1124    }
1125    set
1126}
1127
1128struct FileDisappearancesInput<'a> {
1129    store: &'a mut ImpactStore,
1130    frontier: &'a FlatFrontier,
1131    changed: &'a FxHashSet<String>,
1132    current_findings: &'a FxHashMap<String, Vec<FrontierFinding>>,
1133    current_supps: &'a FxHashMap<String, FxHashSet<String>>,
1134    appeared_move_keys: &'a FxHashSet<String>,
1135    git_sha: Option<&'a str>,
1136    timestamp: &'a str,
1137}
1138
1139fn classify_file_disappearances(input: &mut FileDisappearancesInput<'_>) {
1140    let store = &mut *input.store;
1141    let frontier = input.frontier;
1142    let changed = input.changed;
1143    let current_findings = input.current_findings;
1144    let current_supps = input.current_supps;
1145    let appeared_move_keys = input.appeared_move_keys;
1146    let git_sha = input.git_sha;
1147    let timestamp = input.timestamp;
1148    let empty_supps = FxHashSet::default();
1149    for rel in changed {
1150        let Some(prior) = frontier.get(rel) else {
1151            continue;
1152        };
1153        let now_ids: FxHashSet<&str> = current_findings
1154            .get(rel)
1155            .map(|fs| fs.iter().map(|f| f.id.as_str()).collect())
1156            .unwrap_or_default();
1157        let now_supps = current_supps.get(rel).unwrap_or(&empty_supps);
1158        let prior_supps: FxHashSet<&str> = prior.suppressions.iter().map(String::as_str).collect();
1159        let new_supp_kinds: FxHashSet<String> = now_supps
1160            .iter()
1161            .filter(|k| !prior_supps.contains(k.as_str()))
1162            .cloned()
1163            .collect();
1164
1165        let mut resolved = Vec::new();
1166        let mut suppressed = 0usize;
1167        for pf in &prior.findings {
1168            if now_ids.contains(pf.id.as_str()) {
1169                continue; // still present
1170            }
1171            if appeared_move_keys.contains(&pf.move_key()) {
1172                continue; // moved to another file this run
1173            }
1174            if covered_by(&new_supp_kinds, &pf.kind) {
1175                suppressed += 1; // conservative: a fresh fallow-ignore, never a win
1176            } else {
1177                resolved.push(pf.clone());
1178            }
1179        }
1180        store.suppressed_total += suppressed;
1181        for pf in resolved {
1182            store.resolved_total += 1;
1183            store.recent_resolved.push(ResolutionEvent {
1184                kind: pf.kind,
1185                path: rel.clone(),
1186                symbol: pf.symbol,
1187                git_sha: git_sha.map(ToOwned::to_owned),
1188                timestamp: timestamp.to_owned(),
1189            });
1190        }
1191    }
1192}
1193
1194fn update_file_frontier(
1195    frontier: &mut FlatFrontier,
1196    changed: &FxHashSet<String>,
1197    mut current_findings: FxHashMap<String, Vec<FrontierFinding>>,
1198    mut current_supps: FxHashMap<String, FxHashSet<String>>,
1199) {
1200    for rel in changed {
1201        let findings = current_findings.remove(rel).unwrap_or_default();
1202        let mut suppressions: Vec<String> = current_supps
1203            .remove(rel)
1204            .unwrap_or_default()
1205            .into_iter()
1206            .collect();
1207        suppressions.sort_unstable();
1208        if findings.is_empty() && suppressions.is_empty() {
1209            frontier.remove(rel);
1210        } else {
1211            frontier.insert(
1212                rel.clone(),
1213                FileFrontier {
1214                    findings,
1215                    suppressions,
1216                },
1217            );
1218        }
1219    }
1220}
1221
1222fn classify_clone_disappearances(
1223    store: &mut ImpactStore,
1224    frontier: &FlatFrontier,
1225    clone_frontier: &mut FlatCloneFrontier,
1226    input: &AttributionInput<'_>,
1227    changed: &FxHashSet<String>,
1228    git_sha: Option<&str>,
1229    timestamp: &str,
1230) {
1231    let root = input.root;
1232    let mut current: FxHashMap<String, Vec<String>> = FxHashMap::default();
1233    for c in &input.clones {
1234        let mut paths: Vec<String> = c
1235            .instance_paths
1236            .iter()
1237            .map(|p| format_display_path(p, root))
1238            .collect();
1239        paths.sort_unstable();
1240        paths.dedup();
1241        if paths.iter().any(|p| changed.contains(p)) {
1242            current.insert(c.fingerprint.clone(), paths);
1243        }
1244    }
1245
1246    let dup_suppressed = |paths: &[String]| -> bool {
1247        paths.iter().any(|p| {
1248            changed.contains(p)
1249                && frontier.get(p).is_some_and(|f| {
1250                    f.suppressions
1251                        .iter()
1252                        .any(|k| k == CODE_DUPLICATION_KIND || k == BLANKET_SUPPRESSION)
1253                })
1254        })
1255    };
1256
1257    let still_duplicated: FxHashSet<&String> = current.values().flatten().collect();
1258
1259    let disappeared: Vec<(String, Vec<String>)> = clone_frontier
1260        .iter()
1261        .filter(|(fp, paths)| {
1262            paths.iter().any(|p| changed.contains(p)) && !current.contains_key(*fp)
1263        })
1264        .map(|(fp, paths)| (fp.clone(), paths.clone()))
1265        .collect();
1266
1267    for (fp, paths) in disappeared {
1268        clone_frontier.remove(&fp);
1269        if paths.iter().any(|p| still_duplicated.contains(p)) {
1270            continue;
1271        }
1272        if dup_suppressed(&paths) {
1273            store.suppressed_total += 1;
1274        } else {
1275            store.resolved_total += 1;
1276            let path = paths.first().cloned().unwrap_or_default();
1277            store.recent_resolved.push(ResolutionEvent {
1278                kind: CODE_DUPLICATION_KIND.to_owned(),
1279                path,
1280                symbol: None,
1281                git_sha: git_sha.map(ToOwned::to_owned),
1282                timestamp: timestamp.to_owned(),
1283            });
1284        }
1285    }
1286
1287    for (fp, paths) in current {
1288        clone_frontier.insert(fp, paths);
1289    }
1290}
1291
1292fn prune_frontier(
1293    frontier: &mut FlatFrontier,
1294    clone_frontier: &mut FlatCloneFrontier,
1295    root: &Path,
1296) {
1297    frontier.retain(|rel, _| root.join(rel).exists());
1298    clone_frontier.retain(|_, paths| paths.iter().any(|p| root.join(p).exists()));
1299}
1300
1301fn bound_recent_resolved(store: &mut ImpactStore) {
1302    if store.recent_resolved.len() > MAX_RECENT_RESOLVED {
1303        let overflow = store.recent_resolved.len() - MAX_RECENT_RESOLVED;
1304        store.recent_resolved.drain(0..overflow);
1305    }
1306}
1307
1308fn event_move_key(ev: &ResolutionEvent) -> Option<String> {
1309    ev.symbol
1310        .as_ref()
1311        .map(|symbol| format!("{}{ID_SEP}{symbol}", ev.kind))
1312}
1313
1314fn uncredit_cross_run_moves(store: &mut ImpactStore, appeared_move_keys: &FxHashSet<String>) {
1315    if appeared_move_keys.is_empty() {
1316        return;
1317    }
1318    let mut uncredited = 0usize;
1319    store.recent_resolved.retain(|ev| match event_move_key(ev) {
1320        Some(mk) if appeared_move_keys.contains(&mk) => {
1321            uncredited += 1;
1322            false
1323        }
1324        _ => true,
1325    });
1326    store.resolved_total = store.resolved_total.saturating_sub(uncredited);
1327}
1328
1329#[must_use]
1330pub fn collect_dead_code_findings(results: &AnalysisResults) -> Vec<FindingInput> {
1331    let mut out = Vec::new();
1332    let mut push = |path: &Path, kind: &'static str, symbol: Option<String>| {
1333        out.push(FindingInput {
1334            path: path.to_path_buf(),
1335            kind,
1336            symbol,
1337        });
1338    };
1339    collect_unused_symbol_findings(results, &mut push);
1340    collect_dependency_findings(results, &mut push);
1341    collect_catalog_findings(results, &mut push);
1342    out
1343}
1344
1345fn collect_unused_symbol_findings(
1346    results: &AnalysisResults,
1347    push: &mut impl FnMut(&Path, &'static str, Option<String>),
1348) {
1349    for f in &results.unused_files {
1350        push(&f.file.path, "unused-file", None);
1351    }
1352    for f in &results.unused_exports {
1353        push(
1354            &f.export.path,
1355            "unused-export",
1356            Some(f.export.export_name.clone()),
1357        );
1358    }
1359    for f in &results.unused_types {
1360        push(
1361            &f.export.path,
1362            "unused-type",
1363            Some(f.export.export_name.clone()),
1364        );
1365    }
1366    for f in &results.private_type_leaks {
1367        push(
1368            &f.leak.path,
1369            "private-type-leak",
1370            Some(format!(
1371                "{}{ID_SEP}{}",
1372                f.leak.export_name, f.leak.type_name
1373            )),
1374        );
1375    }
1376    for f in &results.unused_enum_members {
1377        push(
1378            &f.member.path,
1379            "unused-enum-member",
1380            Some(format!(
1381                "{}{ID_SEP}{}",
1382                f.member.parent_name, f.member.member_name
1383            )),
1384        );
1385    }
1386    for f in &results.unused_class_members {
1387        push(
1388            &f.member.path,
1389            "unused-class-member",
1390            Some(format!(
1391                "{}{ID_SEP}{}",
1392                f.member.parent_name, f.member.member_name
1393            )),
1394        );
1395    }
1396    for f in &results.unused_store_members {
1397        push(
1398            &f.member.path,
1399            "unused-store-member",
1400            Some(format!(
1401                "{}{ID_SEP}{}",
1402                f.member.parent_name, f.member.member_name
1403            )),
1404        );
1405    }
1406    for f in &results.unprovided_injects {
1407        push(
1408            &f.inject.path,
1409            "unprovided-inject",
1410            Some(f.inject.key_name.clone()),
1411        );
1412    }
1413    for f in &results.unrendered_components {
1414        push(
1415            &f.component.path,
1416            "unrendered-component",
1417            Some(f.component.component_name.clone()),
1418        );
1419    }
1420    for f in &results.unused_component_props {
1421        push(
1422            &f.prop.path,
1423            "unused-component-prop",
1424            Some(f.prop.prop_name.clone()),
1425        );
1426    }
1427    for f in &results.unused_component_emits {
1428        push(
1429            &f.emit.path,
1430            "unused-component-emit",
1431            Some(f.emit.emit_name.clone()),
1432        );
1433    }
1434    for f in &results.unused_component_inputs {
1435        push(
1436            &f.input.path,
1437            "unused-component-input",
1438            Some(f.input.input_name.clone()),
1439        );
1440    }
1441    for f in &results.unused_component_outputs {
1442        push(
1443            &f.output.path,
1444            "unused-component-output",
1445            Some(f.output.output_name.clone()),
1446        );
1447    }
1448    for f in &results.unresolved_imports {
1449        push(
1450            &f.import.path,
1451            "unresolved-import",
1452            Some(f.import.specifier.clone()),
1453        );
1454    }
1455    for f in &results.boundary_violations {
1456        let to_path = f.violation.to_path.to_string_lossy().replace('\\', "/");
1457        push(
1458            &f.violation.from_path,
1459            "boundary-violation",
1460            Some(format!("{to_path}{ID_SEP}{}", f.violation.import_specifier)),
1461        );
1462    }
1463}
1464
1465fn collect_dependency_findings(
1466    results: &AnalysisResults,
1467    push: &mut impl FnMut(&Path, &'static str, Option<String>),
1468) {
1469    for f in &results.unused_dependencies {
1470        push(
1471            &f.dep.path,
1472            "unused-dependency",
1473            Some(f.dep.package_name.clone()),
1474        );
1475    }
1476    for f in &results.unused_dev_dependencies {
1477        push(
1478            &f.dep.path,
1479            "unused-dev-dependency",
1480            Some(f.dep.package_name.clone()),
1481        );
1482    }
1483    for f in &results.unused_optional_dependencies {
1484        push(
1485            &f.dep.path,
1486            "unused-optional-dependency",
1487            Some(f.dep.package_name.clone()),
1488        );
1489    }
1490    for f in &results.type_only_dependencies {
1491        push(
1492            &f.dep.path,
1493            "type-only-dependency",
1494            Some(f.dep.package_name.clone()),
1495        );
1496    }
1497    for f in &results.test_only_dependencies {
1498        push(
1499            &f.dep.path,
1500            "test-only-dependency",
1501            Some(f.dep.package_name.clone()),
1502        );
1503    }
1504}
1505
1506fn collect_catalog_findings(
1507    results: &AnalysisResults,
1508    push: &mut impl FnMut(&Path, &'static str, Option<String>),
1509) {
1510    for f in &results.unused_catalog_entries {
1511        push(
1512            &f.entry.path,
1513            "unused-catalog-entry",
1514            Some(format!(
1515                "{}{ID_SEP}{}",
1516                f.entry.catalog_name, f.entry.entry_name
1517            )),
1518        );
1519    }
1520    for f in &results.empty_catalog_groups {
1521        push(
1522            &f.group.path,
1523            "empty-catalog-group",
1524            Some(f.group.catalog_name.clone()),
1525        );
1526    }
1527    for f in &results.unresolved_catalog_references {
1528        push(
1529            &f.reference.path,
1530            "unresolved-catalog-reference",
1531            Some(format!(
1532                "{}{ID_SEP}{}",
1533                f.reference.catalog_name, f.reference.entry_name
1534            )),
1535        );
1536    }
1537    for f in &results.unused_dependency_overrides {
1538        push(
1539            &f.entry.path,
1540            "unused-dependency-override",
1541            Some(f.entry.raw_key.clone()),
1542        );
1543    }
1544    for f in &results.misconfigured_dependency_overrides {
1545        push(
1546            &f.entry.path,
1547            "misconfigured-dependency-override",
1548            Some(f.entry.raw_key.clone()),
1549        );
1550    }
1551}
1552
1553/// Collect line-independent complexity finding identities `(path, function name)`
1554/// from a health report. The function name is line-independent, so a function
1555/// moving within its file keeps the same identity.
1556#[must_use]
1557pub fn collect_complexity_findings(
1558    report: &crate::health_types::HealthReport,
1559) -> Vec<FindingInput> {
1560    report
1561        .findings
1562        .iter()
1563        .map(|f| FindingInput {
1564            path: f.path.clone(),
1565            kind: "complexity",
1566            symbol: Some(f.name.clone()),
1567        })
1568        .collect()
1569}
1570
1571/// Collect clone-group identities `(fingerprint, instance paths)` from a
1572/// duplication report. The fingerprint is content-derived (`dup:<hash>`), so it
1573/// is stable across pure relocation.
1574#[must_use]
1575pub fn collect_clone_findings(
1576    report: &fallow_core::duplicates::DuplicationReport,
1577) -> Vec<CloneInput> {
1578    report
1579        .clone_groups
1580        .iter()
1581        .map(|g| CloneInput {
1582            fingerprint: fallow_core::duplicates::clone_fingerprint(&g.instances),
1583            instance_paths: g.instances.iter().map(|i| i.file.clone()).collect(),
1584        })
1585        .collect()
1586}
1587
1588const fn verdict_label(verdict: AuditVerdict) -> &'static str {
1589    match verdict {
1590        AuditVerdict::Pass => "pass",
1591        AuditVerdict::Warn => "warn",
1592        AuditVerdict::Fail => "fail",
1593    }
1594}
1595
1596/// Direction of a count trend between two recorded runs.
1597#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
1598#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1599#[serde(rename_all = "snake_case")]
1600pub enum ImpactTrendDirection {
1601    /// Issue count went down (good).
1602    Improving,
1603    /// Issue count went up.
1604    Declining,
1605    /// Within tolerance.
1606    Stable,
1607}
1608
1609/// A computed trend between the two most recent records.
1610#[derive(Debug, Clone, Serialize)]
1611#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1612pub struct TrendSummary {
1613    pub direction: ImpactTrendDirection,
1614    /// Signed delta in total issues (current minus previous).
1615    pub total_delta: i64,
1616    pub previous_total: usize,
1617    pub current_total: usize,
1618}
1619
1620fn direction_for(delta: i64) -> ImpactTrendDirection {
1621    if delta < -TREND_TOLERANCE {
1622        ImpactTrendDirection::Improving
1623    } else if delta > TREND_TOLERANCE {
1624        ImpactTrendDirection::Declining
1625    } else {
1626        ImpactTrendDirection::Stable
1627    }
1628}
1629
1630/// Wire-version discriminator for [`ImpactReport`]. Independent from the global
1631/// `SchemaVersion` (the impact report versions on its own cadence) and from the
1632/// on-disk `STORE_SCHEMA_VERSION` (the persisted store shape versions
1633/// separately). Serializes as a string `const` so JSON consumers can switch on
1634/// it, matching the other independently-versioned envelopes (e.g.
1635/// `CoverageAnalyzeSchemaVersion`).
1636#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
1637#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1638pub enum ImpactReportSchemaVersion {
1639    /// First release of the `fallow impact --format json` shape.
1640    #[serde(rename = "1")]
1641    V1,
1642}
1643
1644/// The rendered impact report, derived purely from the store (no analysis run).
1645#[derive(Debug, Clone, Serialize)]
1646#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1647#[cfg_attr(feature = "schema", schemars(title = "fallow impact --format json"))]
1648pub struct ImpactReport {
1649    /// Output-shape version for this report, so JSON consumers have a
1650    /// forward-compat signal independent of the on-disk store version. Always
1651    /// present; bumped only on a breaking change to this report's wire shape.
1652    pub schema_version: ImpactReportSchemaVersion,
1653    pub enabled: bool,
1654    /// WHY tracking is on or off: `project` (an explicit per-repo enable/disable
1655    /// decision), `user` (the user-global default with no per-repo decision), or
1656    /// `default` (off, no per-repo decision and no global default). Combine with
1657    /// `explicit_decision` to tell a never-asked off-state (`enabled:false`,
1658    /// `explicit_decision:false`, offer to enable) from a declined-here one
1659    /// (`enabled:false`, `explicit_decision:true`, do not nag).
1660    pub enabled_source: EnabledSource,
1661    pub record_count: usize,
1662    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
1663    pub meta: Option<Meta>,
1664    #[serde(default, skip_serializing_if = "Option::is_none")]
1665    pub first_recorded: Option<String>,
1666    /// Git SHA of the most recent recorded run, so a consumer can tell which
1667    /// commit the `surfacing` counts belong to. This is an ABBREVIATED SHA
1668    /// (`git rev-parse --short`), so it is for display/correlation only and will
1669    /// not match a full 40-character SHA from `$GITHUB_SHA` or the git API
1670    /// without expansion. None when the latest run had no SHA (not a git repo)
1671    /// or there are no records yet.
1672    #[serde(default, skip_serializing_if = "Option::is_none")]
1673    pub latest_git_sha: Option<String>,
1674    /// Counts from the most recent recorded run. These are CHANGED-FILE scoped
1675    /// (each record comes from a `fallow audit` run, whose default `new-only`
1676    /// gate counts only findings in the changed files of that run), NOT a
1677    /// whole-project total.
1678    #[serde(default, skip_serializing_if = "Option::is_none")]
1679    pub surfacing: Option<ImpactCounts>,
1680    /// Trend between the two most recent records. None until two records exist.
1681    #[serde(default, skip_serializing_if = "Option::is_none")]
1682    pub trend: Option<TrendSummary>,
1683    /// Counts from the most recent whole-project `fallow` run. WHOLE-PROJECT
1684    /// scope (not changed-file), so this is the current issue total across the
1685    /// whole repo, context next to the actionable changed-file `surfacing`
1686    /// count. None until a full `fallow` run has been recorded. v1.6.
1687    #[serde(default, skip_serializing_if = "Option::is_none")]
1688    pub project_surfacing: Option<ImpactCounts>,
1689    /// Trend between the two most recent whole-project records. Comparable over
1690    /// time (same whole-project denominator every run), unlike the changed-file
1691    /// `trend`. None until two full `fallow` runs exist. v1.6.
1692    #[serde(default, skip_serializing_if = "Option::is_none")]
1693    pub project_trend: Option<TrendSummary>,
1694    pub containment_count: usize,
1695    /// Most recent containment events (newest last), capped for display.
1696    pub recent_containment: Vec<ContainmentEvent>,
1697    /// Lifetime count of findings fallow credits as genuinely resolved (code
1698    /// removed or refactored, never a `fallow-ignore`). v1.5.
1699    pub resolved_total: usize,
1700    /// Lifetime count of findings silenced by a newly-added `fallow-ignore`.
1701    /// Reported as honest context, never as a win. v1.5.
1702    pub suppressed_total: usize,
1703    /// Most recent resolution events (newest last), capped for display. v1.5.
1704    pub recent_resolved: Vec<ResolutionEvent>,
1705    /// Whether per-finding attribution has a baseline yet. False on a freshly
1706    /// upgraded v1 store (no frontier captured), which the renderer uses to show
1707    /// "resolution tracking starts from your next run" instead of a bare zero.
1708    pub attribution_active: bool,
1709    /// Whether the local agent onboarding prompt has been explicitly declined.
1710    /// Stored in the user config dir (per project) so agents avoid cross-session
1711    /// nags without writing into the repo.
1712    pub onboarding_declined: bool,
1713    /// Whether the user ever made an explicit enable/disable decision for
1714    /// Impact tracking. `enabled: false` with `explicit_decision: false` means
1715    /// "never asked"; with `true` it means "asked and declined". Agents use
1716    /// this to offer the impact opt-in exactly once per project.
1717    pub explicit_decision: bool,
1718}
1719
1720/// Build a report from the store. Defensive: a single record (or none) yields
1721/// no trend rather than a spurious spike, and an empty store yields an empty
1722/// report flagged so the renderer can show the first-run message.
1723/// Trend between the two most recent records in a series. None until two records
1724/// exist; a missing prior record is "unknown" (no trend), never a spike.
1725fn trend_for(records: &[ImpactRecord]) -> Option<TrendSummary> {
1726    if records.len() < 2 {
1727        return None;
1728    }
1729    let current = &records[records.len() - 1];
1730    let previous = &records[records.len() - 2];
1731    let current_total = current.counts.total_issues;
1732    let previous_total = previous.counts.total_issues;
1733    let total_delta = current_total as i64 - previous_total as i64;
1734    Some(TrendSummary {
1735        direction: direction_for(total_delta),
1736        total_delta,
1737        previous_total,
1738        current_total,
1739    })
1740}
1741
1742pub fn build_report(store: &ImpactStore) -> ImpactReport {
1743    let surfacing = store.records.last().map(|r| r.counts.clone());
1744    let trend = trend_for(&store.records);
1745    let project_surfacing = store.project_records.last().map(|r| r.counts.clone());
1746    let project_trend = trend_for(&store.project_records);
1747
1748    let recent_containment = store
1749        .containment
1750        .iter()
1751        .rev()
1752        .take(5)
1753        .rev()
1754        .cloned()
1755        .collect();
1756
1757    let latest_git_sha = store.records.last().and_then(|r| r.git_sha.clone());
1758
1759    let recent_resolved = store
1760        .recent_resolved
1761        .iter()
1762        .rev()
1763        .take(5)
1764        .rev()
1765        .cloned()
1766        .collect();
1767    let attribution_active = !store.frontier.is_empty()
1768        || !store.clone_frontier.is_empty()
1769        || store.resolved_total > 0
1770        || store.suppressed_total > 0;
1771
1772    let (enabled, enabled_source) = resolve_enabled(store);
1773    ImpactReport {
1774        schema_version: ImpactReportSchemaVersion::V1,
1775        enabled,
1776        enabled_source,
1777        record_count: store.records.len(),
1778        meta: None,
1779        first_recorded: store.first_recorded.clone(),
1780        latest_git_sha,
1781        surfacing,
1782        trend,
1783        project_surfacing,
1784        project_trend,
1785        containment_count: store.containment.len(),
1786        recent_containment,
1787        resolved_total: store.resolved_total,
1788        suppressed_total: store.suppressed_total,
1789        recent_resolved,
1790        attribution_active,
1791        onboarding_declined: store.onboarding_declined,
1792        explicit_decision: store.explicit_decision,
1793    }
1794}
1795
1796// ----- Cross-repo aggregate view (`fallow impact --all`) -------------------
1797
1798/// Independent wire-version for the cross-repo report, on its own cadence (it
1799/// versions separately from the per-project `ImpactReportSchemaVersion` and the
1800/// on-disk `STORE_SCHEMA_VERSION`).
1801#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
1802#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1803pub enum CrossRepoImpactSchemaVersion {
1804    /// First release of the `fallow impact --all --format json` shape.
1805    #[serde(rename = "1")]
1806    V1,
1807}
1808
1809/// Grand totals across every tracked project (including repos whose directory no
1810/// longer exists on disk: their past wins still count toward lifetime impact).
1811#[derive(Debug, Clone, Default, Serialize)]
1812#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1813pub struct CrossRepoTotals {
1814    pub resolved_total: usize,
1815    pub suppressed_total: usize,
1816    pub containment_count: usize,
1817    /// Sum of whole-project issue totals across projects that have a full-run
1818    /// baseline, as of EACH project's last full `fallow` run (not a simultaneous
1819    /// snapshot).
1820    pub project_wide_issues: usize,
1821    pub projects_with_baseline: usize,
1822}
1823
1824/// One project's row in the cross-repo roll-up.
1825#[derive(Debug, Clone, Serialize)]
1826#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1827pub struct CrossRepoProjectEntry {
1828    /// Stable, non-reversible project key (the store filename stem); the
1829    /// cross-tool/cross-run JOIN key. NEVER a path.
1830    pub project_key: String,
1831    /// Repo basename for display (never a full path). Absent on pre-v5 stores
1832    /// (the row falls back to the short key).
1833    #[serde(default, skip_serializing_if = "Option::is_none")]
1834    pub label: Option<String>,
1835    /// Timestamp of the project's most recent recorded run (changed-file or
1836    /// whole-project), for the LAST RUN column and the default `recent` sort.
1837    #[serde(default, skip_serializing_if = "Option::is_none")]
1838    pub last_recorded: Option<String>,
1839    /// The full per-project report (identical shape to `fallow impact --format
1840    /// json`), reused verbatim so the per-project wire contract is the sub-shape.
1841    pub report: ImpactReport,
1842}
1843
1844/// The cross-repo aggregate report (`fallow impact --all --format json`).
1845#[derive(Debug, Clone, Serialize)]
1846#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1847#[cfg_attr(
1848    feature = "schema",
1849    schemars(title = "fallow impact --all --format json")
1850)]
1851pub struct CrossRepoImpactReport {
1852    pub schema_version: CrossRepoImpactSchemaVersion,
1853    /// Per-project stores successfully parsed (add `unreadable_count` for the
1854    /// total number of store files found in the user config dir).
1855    pub project_count: usize,
1856    /// Stores with recorded history (the rows in `projects`); excludes
1857    /// enabled-but-empty stores, which are still counted in `project_count`.
1858    pub tracked_count: usize,
1859    /// Stores that failed to parse and were skipped (corrupt or newer-schema).
1860    pub unreadable_count: usize,
1861    pub totals: CrossRepoTotals,
1862    pub projects: Vec<CrossRepoProjectEntry>,
1863}
1864
1865/// Ranking for the cross-repo rows.
1866#[derive(Debug, Clone, Copy)]
1867pub enum CrossRepoSort {
1868    /// Most recently recorded first (the default: active repos float up).
1869    Recent,
1870    /// Most findings resolved first.
1871    Resolved,
1872    /// Most commits contained first.
1873    Contained,
1874    /// Alphabetical by label/key.
1875    Name,
1876}
1877
1878/// The newest record timestamp across the changed-file and whole-project series.
1879fn latest_activity(store: &ImpactStore) -> Option<String> {
1880    let a = store.records.last().map(|r| r.timestamp.clone());
1881    let b = store.project_records.last().map(|r| r.timestamp.clone());
1882    match (a, b) {
1883        (Some(x), Some(y)) => Some(if x >= y { x } else { y }),
1884        (x, y) => x.or(y),
1885    }
1886}
1887
1888/// Enumerate every per-project store in `<config-dir>/fallow/impact/`, returning
1889/// `(project_key, store)` pairs plus the count of files that failed to parse.
1890/// Read-only; never writes. The global `impact.json` toggle is a sibling FILE of
1891/// this dir (one level up), so it is naturally excluded. Corrupt/newer-schema
1892/// files are skipped and counted, never substituted with a default store.
1893#[must_use]
1894pub fn load_all() -> (Vec<(String, ImpactStore)>, usize) {
1895    let Some(dir) = impact_config_dir().map(|d| d.join("impact")) else {
1896        return (Vec::new(), 0);
1897    };
1898    let Ok(read) = std::fs::read_dir(&dir) else {
1899        return (Vec::new(), 0);
1900    };
1901    let mut stores = Vec::new();
1902    let mut unreadable = 0usize;
1903    for entry in read.flatten() {
1904        let path = entry.path();
1905        if path.extension().and_then(|e| e.to_str()) != Some("json") {
1906            continue;
1907        }
1908        let Some(key) = path.file_stem().and_then(|s| s.to_str()).map(str::to_owned) else {
1909            continue;
1910        };
1911        match std::fs::read_to_string(&path)
1912            .ok()
1913            .and_then(|c| serde_json::from_str::<ImpactStore>(&c).ok())
1914        {
1915            Some(store) => stores.push((key, store)),
1916            None => unreadable += 1,
1917        }
1918    }
1919    (stores, unreadable)
1920}
1921
1922/// Build the cross-repo aggregate from enumerated stores. Excludes
1923/// enabled-but-empty projects from the rows (counted in `project_count`), sums
1924/// totals over every tracked project, and sorts the rows by `sort`.
1925#[must_use]
1926pub fn build_aggregate_report(
1927    stores: Vec<(String, ImpactStore)>,
1928    unreadable: usize,
1929    sort: CrossRepoSort,
1930) -> CrossRepoImpactReport {
1931    let project_count = stores.len();
1932    let mut totals = CrossRepoTotals::default();
1933    let mut projects = Vec::new();
1934    for (key, store) in stores {
1935        let report = build_report(&store);
1936        let has_history = report.record_count > 0
1937            || report.project_surfacing.is_some()
1938            || report.resolved_total > 0
1939            || report.containment_count > 0;
1940        if !has_history {
1941            continue;
1942        }
1943        totals.resolved_total += report.resolved_total;
1944        totals.suppressed_total += report.suppressed_total;
1945        totals.containment_count += report.containment_count;
1946        if let Some(ps) = &report.project_surfacing {
1947            totals.project_wide_issues += ps.total_issues;
1948            totals.projects_with_baseline += 1;
1949        }
1950        projects.push(CrossRepoProjectEntry {
1951            project_key: key,
1952            label: store.label.clone(),
1953            last_recorded: latest_activity(&store),
1954            report,
1955        });
1956    }
1957    sort_cross_repo(&mut projects, sort);
1958    CrossRepoImpactReport {
1959        schema_version: CrossRepoImpactSchemaVersion::V1,
1960        project_count,
1961        tracked_count: projects.len(),
1962        unreadable_count: unreadable,
1963        totals,
1964        projects,
1965    }
1966}
1967
1968fn sort_cross_repo(projects: &mut [CrossRepoProjectEntry], sort: CrossRepoSort) {
1969    match sort {
1970        // Newest activity first; missing timestamps sort last. project_key
1971        // tiebreak keeps the order deterministic.
1972        CrossRepoSort::Recent => projects.sort_by(|a, b| {
1973            b.last_recorded
1974                .cmp(&a.last_recorded)
1975                .then_with(|| a.project_key.cmp(&b.project_key))
1976        }),
1977        CrossRepoSort::Resolved => projects.sort_by(|a, b| {
1978            b.report
1979                .resolved_total
1980                .cmp(&a.report.resolved_total)
1981                .then_with(|| a.project_key.cmp(&b.project_key))
1982        }),
1983        CrossRepoSort::Contained => projects.sort_by(|a, b| {
1984            b.report
1985                .containment_count
1986                .cmp(&a.report.containment_count)
1987                .then_with(|| a.project_key.cmp(&b.project_key))
1988        }),
1989        CrossRepoSort::Name => projects.sort_by(|a, b| {
1990            cross_repo_label(a)
1991                .cmp(&cross_repo_label(b))
1992                .then_with(|| a.project_key.cmp(&b.project_key))
1993        }),
1994    }
1995}
1996
1997/// The display label for a row: the basename when present, else the short key.
1998/// Pure (no path access), so JSON/markdown using it can never leak a path.
1999fn cross_repo_label(entry: &CrossRepoProjectEntry) -> String {
2000    entry
2001        .label
2002        .clone()
2003        .unwrap_or_else(|| short_key(&entry.project_key))
2004}
2005
2006/// First 12 hex of a project key, for opaque-but-stable row labels.
2007fn short_key(key: &str) -> String {
2008    key.chars().take(12).collect()
2009}
2010
2011/// Build the cross-repo report by enumerating the config dir.
2012#[must_use]
2013pub fn aggregate(sort: CrossRepoSort) -> CrossRepoImpactReport {
2014    let (stores, unreadable) = load_all();
2015    build_aggregate_report(stores, unreadable, sort)
2016}
2017
2018/// Render the whole-project view for the human report. Deliberately understated
2019/// (one count line, one trend line, one caveat) rather than a co-equal header:
2020/// the project track advances only on local full `fallow` runs, not CI, so it is
2021/// context for the changed-file story above, not the headline. Renders nothing
2022/// when no full `fallow` run has been recorded yet.
2023#[expect(
2024    clippy::format_push_string,
2025    reason = "small report renderer; readability over avoiding the extra allocation"
2026)]
2027fn render_project_section(out: &mut String, report: &ImpactReport) {
2028    let Some(s) = &report.project_surfacing else {
2029        return;
2030    };
2031    out.push_str(&format!(
2032        "  WHOLE PROJECT (whole-repo context, not a to-do)\n    {} issue{} across the whole project at your last full `fallow` run\n",
2033        s.total_issues,
2034        plural(s.total_issues),
2035    ));
2036    if let Some(t) = &report.project_trend {
2037        let arrow = trend_arrow(t.direction);
2038        out.push_str(&format!(
2039            "    {} -> {} ({}) across your last two full runs (comparable over time)\n",
2040            t.previous_total, t.current_total, arrow,
2041        ));
2042    } else {
2043        out.push_str("    project trend starts after your next full `fallow` run\n");
2044    }
2045    out.push_str("      advances only on your local full `fallow` runs, not CI\n\n");
2046}
2047
2048/// Render the report as human-readable text.
2049#[expect(
2050    clippy::format_push_string,
2051    reason = "small report renderer; readability over avoiding the extra allocation"
2052)]
2053pub fn render_human(report: &ImpactReport) -> String {
2054    let mut out = String::new();
2055    out.push_str("FALLOW IMPACT\n\n");
2056
2057    if !report.enabled {
2058        out.push_str(
2059            "Impact tracking is off. Enable it with `fallow impact enable`, then\n\
2060             let your pre-commit gate run a few times to build history.\n",
2061        );
2062        return out;
2063    }
2064
2065    if report.enabled_source == EnabledSource::User {
2066        out.push_str(
2067            "Enabled by your user-global default (`fallow impact default on`). Run\n\
2068             `fallow impact disable` to opt this project out.\n\n",
2069        );
2070    }
2071
2072    if report.record_count == 0 && report.project_surfacing.is_none() {
2073        out.push_str(
2074            "Tracking enabled. No history yet: check back after your next few\n\
2075             commits (Impact records each `fallow audit` / pre-commit gate run,\n\
2076             and each full `fallow` run for the whole-project view).\n",
2077        );
2078        return out;
2079    }
2080
2081    if let Some(s) = &report.surfacing {
2082        out.push_str(&format!(
2083            "  LATEST RUN (changed files, act on these now)\n    {} issue{} flagged in your last `fallow audit` run\n",
2084            s.total_issues,
2085            plural(s.total_issues),
2086        ));
2087        out.push_str(&format!(
2088            "      dead code {}  ·  complexity {}  ·  duplication {}\n\n",
2089            s.dead_code, s.complexity, s.duplication,
2090        ));
2091    }
2092
2093    if let Some(t) = &report.trend {
2094        let arrow = trend_arrow(t.direction);
2095        out.push_str(&format!(
2096            "  TREND\n    {} -> {} issues ({}) across your last two recorded runs\n      each run is changed-file scope, so consecutive runs may cover different changes\n\n",
2097            t.previous_total, t.current_total, arrow,
2098        ));
2099    }
2100
2101    render_project_section(&mut out, report);
2102
2103    out.push_str(&format!(
2104        "  CONTAINED AT COMMIT\n    {} time{} fallow blocked a commit until it was fixed\n",
2105        report.containment_count,
2106        plural(report.containment_count),
2107    ));
2108
2109    if report.resolved_total > 0 {
2110        out.push_str(&format!(
2111            "\n  RESOLVED\n    {} finding{} you cleared since fallow started tracking\n",
2112            report.resolved_total,
2113            plural(report.resolved_total),
2114        ));
2115        for ev in &report.recent_resolved {
2116            match &ev.symbol {
2117                Some(symbol) => {
2118                    out.push_str(&format!("      {} {} in {}\n", ev.kind, symbol, ev.path));
2119                }
2120                None => out.push_str(&format!("      {} in {}\n", ev.kind, ev.path)),
2121            }
2122        }
2123    } else if report.attribution_active {
2124        out.push_str(
2125            "\n  RESOLVED\n    none yet; a finding is credited when fallow re-analyzes the\n      file it left (a fix that reverts a file to its base state\n      may not be individually credited)\n",
2126        );
2127    } else {
2128        out.push_str("\n  RESOLVED\n    resolution tracking starts from your next gate run\n");
2129    }
2130
2131    if report.suppressed_total > 0 {
2132        out.push_str(&format!(
2133            "      {} finding{} you marked intentional (fallow-ignore), not counted as resolved\n",
2134            report.suppressed_total,
2135            plural(report.suppressed_total),
2136        ));
2137    }
2138
2139    out.push('\n');
2140    let since = report
2141        .first_recorded
2142        .as_deref()
2143        .map_or("the first run", date_only);
2144    if report.record_count > 0 {
2145        out.push_str(&format!(
2146            "Based on {} recorded audit run{} since {}. Local-only; never uploaded.\n\
2147             Changed-file scope: each audit run only sees files differing from your base.\n",
2148            report.record_count,
2149            plural(report.record_count),
2150            since,
2151        ));
2152    } else {
2153        out.push_str(&format!(
2154            "Tracking since {since}. Local-only; never uploaded.\n",
2155        ));
2156    }
2157    out.push_str(
2158        "Resolution tracking is a local-developer signal: it accrues on your\n\
2159         machine across runs, not in CI (fallow never records there).\n",
2160    );
2161    out
2162}
2163
2164/// Render the report as JSON.
2165pub fn render_json(report: &ImpactReport) -> String {
2166    let value = crate::output_envelope::serialize_root_output(
2167        crate::output_envelope::FallowOutput::Impact(report.clone()),
2168    )
2169    .unwrap_or_else(|_| serde_json::json!({"error":"failed to serialize impact report"}));
2170    serde_json::to_string_pretty(&value)
2171        .unwrap_or_else(|_| "{\"error\":\"failed to serialize impact report\"}".to_owned())
2172}
2173
2174/// Render the whole-project view for the markdown report. One understated line
2175/// plus a trend line when available, matching the human renderer's framing.
2176/// Renders nothing when no full `fallow` run has been recorded yet.
2177#[expect(
2178    clippy::format_push_string,
2179    reason = "small report renderer; readability over avoiding the extra allocation"
2180)]
2181fn render_project_markdown(out: &mut String, report: &ImpactReport) {
2182    let Some(s) = &report.project_surfacing else {
2183        return;
2184    };
2185    out.push_str(&format!(
2186        "- **Whole project (whole-repo context, last full `fallow` run):** {} issue{} (dead code {}, complexity {}, duplication {})\n",
2187        s.total_issues,
2188        plural(s.total_issues),
2189        s.dead_code,
2190        s.complexity,
2191        s.duplication,
2192    ));
2193    if let Some(t) = &report.project_trend {
2194        let arrow = trend_arrow(t.direction);
2195        out.push_str(&format!(
2196            "- **Project trend (whole project, last two full runs):** {} -> {} ({})\n",
2197            t.previous_total, t.current_total, arrow,
2198        ));
2199    }
2200}
2201
2202/// Render the report as Markdown (paste-ready for a PR description or standup).
2203#[expect(
2204    clippy::format_push_string,
2205    reason = "small report renderer; readability over avoiding the extra allocation"
2206)]
2207pub fn render_markdown(report: &ImpactReport) -> String {
2208    let mut out = String::new();
2209    out.push_str("## Fallow impact\n\n");
2210
2211    if !report.enabled {
2212        out.push_str("Impact tracking is off. Run `fallow impact enable` to start.\n");
2213        return out;
2214    }
2215    if report.record_count == 0 && report.project_surfacing.is_none() {
2216        out.push_str("Tracking enabled. No history yet; check back after a few commits.\n");
2217        return out;
2218    }
2219
2220    if let Some(s) = &report.surfacing {
2221        out.push_str(&format!(
2222            "- **Latest run (changed files):** {} issue{} (dead code {}, complexity {}, duplication {})\n",
2223            s.total_issues,
2224            plural(s.total_issues),
2225            s.dead_code,
2226            s.complexity,
2227            s.duplication,
2228        ));
2229    }
2230    if let Some(t) = &report.trend {
2231        out.push_str(&format!(
2232            "- **Trend (changed-file scope, last two runs):** {} -> {} ({})\n",
2233            t.previous_total,
2234            t.current_total,
2235            trend_arrow(t.direction),
2236        ));
2237    }
2238    render_project_markdown(&mut out, report);
2239    out.push_str(&format!(
2240        "- **Contained at commit:** {} time{}\n",
2241        report.containment_count,
2242        plural(report.containment_count),
2243    ));
2244    if report.resolved_total > 0 {
2245        out.push_str(&format!(
2246            "- **Resolved:** {} finding{} cleared since tracking started\n",
2247            report.resolved_total,
2248            plural(report.resolved_total),
2249        ));
2250    } else if report.attribution_active {
2251        out.push_str("- **Resolved:** none yet; tracking active\n");
2252    } else {
2253        out.push_str("- **Resolved:** resolution tracking starts from your next gate run\n");
2254    }
2255    if report.suppressed_total > 0 {
2256        out.push_str(&format!(
2257            "- **Marked intentional:** {} finding{} (`fallow-ignore`), not counted as resolved\n",
2258            report.suppressed_total,
2259            plural(report.suppressed_total),
2260        ));
2261    }
2262    let since = report
2263        .first_recorded
2264        .as_deref()
2265        .map_or("the first run", date_only);
2266    if report.record_count > 0 {
2267        out.push_str(&format!(
2268            "\n_Based on {} recorded audit run{} since {}. Local-only; resolution is a local-developer signal._\n",
2269            report.record_count,
2270            plural(report.record_count),
2271            since,
2272        ));
2273    } else {
2274        out.push_str(&format!(
2275            "\n_Tracking since {since}. Local-only; resolution is a local-developer signal._\n",
2276        ));
2277    }
2278    out
2279}
2280
2281/// Render the cross-repo report as JSON via the typed `ImpactCrossRepo` envelope.
2282#[must_use]
2283pub fn render_cross_repo_json(report: &CrossRepoImpactReport) -> String {
2284    let value = crate::output_envelope::serialize_root_output(
2285        crate::output_envelope::FallowOutput::ImpactCrossRepo(report.clone()),
2286    )
2287    .unwrap_or_else(
2288        |_| serde_json::json!({"error":"failed to serialize cross-repo impact report"}),
2289    );
2290    serde_json::to_string_pretty(&value).unwrap_or_else(|_| {
2291        "{\"error\":\"failed to serialize cross-repo impact report\"}".to_owned()
2292    })
2293}
2294
2295/// A single row's display label (basename when present, else short key). Pure:
2296/// never touches the filesystem, so it can never leak a path.
2297fn row_label(entry: &CrossRepoProjectEntry) -> String {
2298    cross_repo_label(entry)
2299}
2300
2301fn opt_count(c: Option<&ImpactCounts>) -> String {
2302    c.map_or_else(|| "-".to_owned(), |c| c.total_issues.to_string())
2303}
2304
2305fn row_trend(report: &ImpactReport) -> &'static str {
2306    report
2307        .project_trend
2308        .as_ref()
2309        .or(report.trend.as_ref())
2310        .map_or("-", |t| trend_arrow(t.direction))
2311}
2312
2313/// Render the cross-repo roll-up as human-readable text. `limit` caps the
2314/// printed rows (grand totals always reflect every tracked project). Path-free:
2315/// the CLI adds the single store-dir discoverability line, gated on `!quiet`.
2316#[expect(
2317    clippy::format_push_string,
2318    reason = "small report renderer; readability over avoiding the extra allocation"
2319)]
2320#[must_use]
2321pub fn render_cross_repo_human(report: &CrossRepoImpactReport, limit: Option<usize>) -> String {
2322    let mut out = String::new();
2323    out.push_str("FALLOW IMPACT (ALL PROJECTS)\n\n");
2324
2325    if report.project_count == 0 {
2326        if report.unreadable_count > 0 {
2327            out.push_str(&format!(
2328                "No readable projects: skipped {} unreadable store{} (corrupt, or written by \
2329                 a newer fallow). Upgrade fallow to read them.\n",
2330                report.unreadable_count,
2331                plural(report.unreadable_count),
2332            ));
2333        } else {
2334            out.push_str(
2335                "No projects tracked yet. Enable in a repo with `fallow impact enable`, or for \
2336                 every project with `fallow impact default on`.\n",
2337            );
2338        }
2339        return out;
2340    }
2341
2342    out.push_str(&format!(
2343        "{} project{} tracked, {} with history\n\n",
2344        report.project_count,
2345        plural(report.project_count),
2346        report.tracked_count,
2347    ));
2348
2349    if !report.projects.is_empty() {
2350        out.push_str(&format!(
2351            "{:<24}{:>8}{:>10}{:>11}{:>10}{:>7}  {}\n",
2352            "PROJECT", "LATEST", "REPO-WIDE", "CONTAINED", "RESOLVED", "TREND", "LAST RUN",
2353        ));
2354        let rows = limit.map_or(report.projects.len(), |n| n.min(report.projects.len()));
2355        for entry in report.projects.iter().take(rows) {
2356            let mut label = row_label(entry);
2357            if label.chars().count() > 22 {
2358                label = format!("{}...", label.chars().take(19).collect::<String>());
2359            }
2360            let last = entry
2361                .last_recorded
2362                .as_deref()
2363                .map_or("-", date_only)
2364                .to_owned();
2365            out.push_str(&format!(
2366                "{:<24}{:>8}{:>10}{:>11}{:>10}{:>7}  {}\n",
2367                label,
2368                opt_count(entry.report.surfacing.as_ref()),
2369                opt_count(entry.report.project_surfacing.as_ref()),
2370                entry.report.containment_count,
2371                entry.report.resolved_total,
2372                row_trend(&entry.report),
2373                last,
2374            ));
2375        }
2376        if let Some(n) = limit
2377            && report.projects.len() > n
2378        {
2379            out.push_str(&format!(
2380                "  ... and {} more (raise --limit to show)\n",
2381                report.projects.len() - n,
2382            ));
2383        }
2384    }
2385
2386    let no_history = report.project_count.saturating_sub(report.tracked_count);
2387    if no_history > 0 {
2388        out.push_str(&format!(
2389            "\n{no_history} tracked project{} with no history yet\n",
2390            plural(no_history),
2391        ));
2392    }
2393    if report.unreadable_count > 0 {
2394        out.push_str(&format!(
2395            "skipped {} unreadable store{}\n",
2396            report.unreadable_count,
2397            plural(report.unreadable_count),
2398        ));
2399    }
2400
2401    let t = &report.totals;
2402    out.push_str("\nGRAND TOTALS\n");
2403    out.push_str(&format!(
2404        "  Across {} tracked project{}: {} finding{} resolved, {} commit{} contained, {} marked intentional\n",
2405        report.tracked_count,
2406        plural(report.tracked_count),
2407        t.resolved_total,
2408        plural(t.resolved_total),
2409        t.containment_count,
2410        plural(t.containment_count),
2411        t.suppressed_total,
2412    ));
2413    if t.projects_with_baseline > 0 {
2414        out.push_str(&format!(
2415            "  {} issue{} project-wide across {} project{} with a full-run baseline (as of each project's last full run)\n",
2416            t.project_wide_issues,
2417            plural(t.project_wide_issues),
2418            t.projects_with_baseline,
2419            plural(t.projects_with_baseline),
2420        ));
2421    }
2422    out.push_str("\nLocal-only; never uploaded; accrues on this machine, not CI.\n");
2423    out
2424}
2425
2426/// Render the cross-repo roll-up as Markdown (paste-ready, path-free).
2427#[expect(
2428    clippy::format_push_string,
2429    reason = "small report renderer; readability over avoiding the extra allocation"
2430)]
2431#[must_use]
2432pub fn render_cross_repo_markdown(report: &CrossRepoImpactReport) -> String {
2433    let mut out = String::new();
2434    out.push_str("## Fallow impact (all projects)\n\n");
2435    if report.project_count == 0 {
2436        if report.unreadable_count > 0 {
2437            out.push_str(&format!(
2438                "No readable projects: skipped {} unreadable store{}.\n",
2439                report.unreadable_count,
2440                plural(report.unreadable_count),
2441            ));
2442        } else {
2443            out.push_str("No projects tracked yet.\n");
2444        }
2445        return out;
2446    }
2447    out.push_str(&format!(
2448        "{} project{} tracked, {} with history.\n\n",
2449        report.project_count,
2450        plural(report.project_count),
2451        report.tracked_count,
2452    ));
2453    if !report.projects.is_empty() {
2454        out.push_str("| Project | Latest | Repo-wide | Contained | Resolved | Last run |\n");
2455        out.push_str("|:--------|-------:|----------:|----------:|---------:|:---------|\n");
2456        for entry in &report.projects {
2457            out.push_str(&format!(
2458                "| {} | {} | {} | {} | {} | {} |\n",
2459                row_label(entry),
2460                opt_count(entry.report.surfacing.as_ref()),
2461                opt_count(entry.report.project_surfacing.as_ref()),
2462                entry.report.containment_count,
2463                entry.report.resolved_total,
2464                entry.last_recorded.as_deref().map_or("-", date_only),
2465            ));
2466        }
2467    }
2468    let t = &report.totals;
2469    out.push_str(&format!(
2470        "\n**Grand totals:** {} resolved, {} contained, {} marked intentional across {} tracked project{}",
2471        t.resolved_total,
2472        t.containment_count,
2473        t.suppressed_total,
2474        report.tracked_count,
2475        plural(report.tracked_count),
2476    ));
2477    if t.projects_with_baseline > 0 {
2478        out.push_str(&format!(
2479            "; {} issue{} project-wide across {} project{} with a full-run baseline (as of each project's last full run)",
2480            t.project_wide_issues,
2481            plural(t.project_wide_issues),
2482            t.projects_with_baseline,
2483            plural(t.projects_with_baseline),
2484        ));
2485    }
2486    out.push_str(".\n\n_Local-only; never uploaded; accrues on this machine, not CI._\n");
2487    out
2488}
2489
2490const fn plural(n: usize) -> &'static str {
2491    if n == 1 { "" } else { "s" }
2492}
2493
2494/// Trim a stored ISO-8601 timestamp (`2026-05-29T18:15:23Z`) to its date part
2495/// (`2026-05-29`) for human/markdown footers. The wall-clock time and `Z` add
2496/// noise without meaning when a reader just wants "tracking since when". JSON
2497/// keeps the full `first_recorded` timestamp. Returns the input unchanged if it
2498/// has no `T` separator.
2499fn date_only(ts: &str) -> &str {
2500    ts.split_once('T').map_or(ts, |(date, _)| date)
2501}
2502
2503/// Single human-facing trend vocabulary, shared by the text and markdown
2504/// renderers so the same concept does not read three different ways. The JSON
2505/// wire keeps the `improving`/`declining`/`stable` enum form for machines.
2506const fn trend_arrow(direction: ImpactTrendDirection) -> &'static str {
2507    match direction {
2508        ImpactTrendDirection::Improving => "down",
2509        ImpactTrendDirection::Declining => "up",
2510        ImpactTrendDirection::Stable => "flat",
2511    }
2512}
2513
2514#[cfg(test)]
2515mod tests {
2516    use super::*;
2517
2518    /// Per-test isolation: a fresh user-config dir (so the store never touches
2519    /// the real dir and parallel tests do not collide) plus a fresh project
2520    /// root. Bind BOTH returned `TempDir`s for the test's lifetime. The store
2521    /// for a non-git tempdir root keys on the canonical root, so each test's
2522    /// root is its own store.
2523    fn test_env() -> (tempfile::TempDir, tempfile::TempDir) {
2524        let config = tempfile::tempdir().unwrap();
2525        TEST_CONFIG_DIR.with(|c| *c.borrow_mut() = Some(config.path().to_path_buf()));
2526        let root = tempfile::tempdir().unwrap();
2527        (config, root)
2528    }
2529
2530    /// All frontier rel-paths across every worktree sub-map (tests use one
2531    /// root => one worktree key), for the v4 nested-frontier shape.
2532    fn frontier_paths(store: &ImpactStore) -> FxHashSet<String> {
2533        store
2534            .frontier
2535            .values()
2536            .flat_map(|m| m.keys().cloned())
2537            .collect()
2538    }
2539
2540    /// All clone fingerprints across every worktree sub-map.
2541    fn clone_fingerprints(store: &ImpactStore) -> FxHashSet<String> {
2542        store
2543            .clone_frontier
2544            .values()
2545            .flat_map(|m| m.keys().cloned())
2546            .collect()
2547    }
2548
2549    /// Seed raw bytes at the resolved (user-dir) store path, creating parent
2550    /// dirs, to exercise the load/parse path against hand-authored JSON.
2551    fn seed_store_raw(root: &Path, bytes: &[u8]) {
2552        let path = store_path(root).expect("test config dir set");
2553        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
2554        std::fs::write(&path, bytes).unwrap();
2555    }
2556
2557    fn summary(dead: usize, complexity: usize, dupes: usize) -> AuditSummary {
2558        AuditSummary {
2559            dead_code_issues: dead,
2560            dead_code_has_errors: dead > 0,
2561            complexity_findings: complexity,
2562            max_cyclomatic: None,
2563            duplication_clone_groups: dupes,
2564        }
2565    }
2566
2567    /// Record a run with no per-finding attribution (v1 surfacing/trend/containment only).
2568    fn record_v1(
2569        root: &Path,
2570        summary: &AuditSummary,
2571        verdict: AuditVerdict,
2572        gate: bool,
2573        git_sha: Option<&str>,
2574        version: &str,
2575        timestamp: &str,
2576    ) {
2577        record_audit_run(
2578            root,
2579            summary,
2580            &AuditRunRecord {
2581                verdict,
2582                gate,
2583                git_sha,
2584                version,
2585                timestamp,
2586                attribution: None,
2587            },
2588        );
2589    }
2590
2591    /// Create a real file under `root` (attribution prunes frontier entries for
2592    /// files that no longer exist, so test files must exist on disk).
2593    fn touch(root: &Path, rel: &str) -> PathBuf {
2594        let p = root.join(rel);
2595        if let Some(parent) = p.parent() {
2596            std::fs::create_dir_all(parent).unwrap();
2597        }
2598        std::fs::write(&p, b"x").unwrap();
2599        p
2600    }
2601
2602    fn fi(path: &Path, kind: &'static str, symbol: &str) -> FindingInput {
2603        FindingInput {
2604            path: path.to_path_buf(),
2605            kind,
2606            symbol: Some(symbol.to_owned()),
2607        }
2608    }
2609
2610    fn supp(path: &Path, kind: &str) -> ActiveSuppression {
2611        ActiveSuppression {
2612            path: path.to_path_buf(),
2613            kind: Some(kind.to_owned()),
2614            is_file_level: false,
2615        }
2616    }
2617
2618    /// Record one attribution run against the store.
2619    fn run(
2620        root: &Path,
2621        changed: &[&Path],
2622        findings: Vec<FindingInput>,
2623        clones: Vec<CloneInput>,
2624        supps: &[ActiveSuppression],
2625        ts: &str,
2626    ) {
2627        let changed_files: Vec<PathBuf> = changed.iter().map(|p| p.to_path_buf()).collect();
2628        let input = AttributionInput {
2629            root,
2630            scope: Scope::ChangedFiles(&changed_files),
2631            findings,
2632            clones,
2633            suppressions: supps,
2634        };
2635        record_audit_run(
2636            root,
2637            &summary(0, 0, 0),
2638            &AuditRunRecord {
2639                verdict: AuditVerdict::Pass,
2640                gate: true,
2641                git_sha: Some("sha"),
2642                version: "2.0.0",
2643                timestamp: ts,
2644                attribution: Some(&input),
2645            },
2646        );
2647    }
2648
2649    #[test]
2650    fn disabled_store_does_not_record() {
2651        let (_config, dir) = test_env();
2652        let root = dir.path();
2653        record_v1(
2654            root,
2655            &summary(3, 1, 0),
2656            AuditVerdict::Fail,
2657            true,
2658            Some("abc1234"),
2659            "2.0.0",
2660            "2026-05-29T10:00:00Z",
2661        );
2662        let store = load(root);
2663        assert!(store.records.is_empty());
2664        assert!(!store.enabled);
2665    }
2666
2667    #[test]
2668    fn enable_and_disable_record_the_explicit_decision() {
2669        let (_config, dir) = test_env();
2670        let root = dir.path();
2671        assert!(!load(root).explicit_decision, "fresh store: never asked");
2672
2673        // Declining on a never-enabled project is an explicit decision too.
2674        disable(root);
2675        let store = load(root);
2676        assert!(!store.enabled);
2677        assert!(store.explicit_decision);
2678        assert!(build_report(&store).explicit_decision);
2679    }
2680
2681    #[test]
2682    fn due_digest_stamps_and_respects_interval_and_gates() {
2683        let (_config, dir) = test_env();
2684        let root = dir.path();
2685
2686        // Disabled, or enabled with zero value: never due.
2687        assert!(take_due_digest(root).is_none());
2688        enable(root);
2689        assert!(take_due_digest(root).is_none(), "zero counters never nag");
2690
2691        let mut store = load(root);
2692        store.resolved_total = 3;
2693        store.containment.push(ContainmentEvent {
2694            blocked_at: "2026-06-11T00:00:00Z".to_string(),
2695            cleared_at: "2026-06-11T00:05:00Z".to_string(),
2696            git_sha: None,
2697            blocked_counts: ImpactCounts::default(),
2698        });
2699        save(&store, root);
2700
2701        let digest = take_due_digest(root).expect("first digest is due");
2702        assert_eq!(digest.containment_count, 1);
2703        assert_eq!(digest.resolved_total, 3);
2704        assert!(
2705            take_due_digest(root).is_none(),
2706            "stamped: not due again within the interval"
2707        );
2708
2709        // An expired stamp makes it due again.
2710        let mut store = load(root);
2711        store.last_digest_epoch = Some(0);
2712        save(&store, root);
2713        assert!(take_due_digest(root).is_some());
2714    }
2715
2716    #[test]
2717    fn decline_onboarding_persists_in_existing_store() {
2718        let (_config, dir) = test_env();
2719        let root = dir.path();
2720
2721        assert!(decline_onboarding(root));
2722        assert!(!decline_onboarding(root));
2723
2724        let store = load(root);
2725        assert!(store.onboarding_declined);
2726        assert_eq!(store.schema_version, STORE_SCHEMA_VERSION);
2727        // Decline persists in the user store and writes nothing into the repo.
2728        assert!(!root.join(".gitignore").exists());
2729        let report = build_report(&store);
2730        assert!(report.onboarding_declined);
2731    }
2732
2733    #[test]
2734    fn enable_then_record_accrues_history() {
2735        let (_config, dir) = test_env();
2736        let root = dir.path();
2737        assert!(enable(root));
2738        assert!(!enable(root)); // second enable is a no-op-ish (already on)
2739        record_v1(
2740            root,
2741            &summary(2, 1, 0),
2742            AuditVerdict::Warn,
2743            false,
2744            None,
2745            "2.0.0",
2746            "2026-05-29T10:00:00Z",
2747        );
2748        let store = load(root);
2749        assert_eq!(store.records.len(), 1);
2750        assert_eq!(store.records[0].counts.total_issues, 3);
2751        assert_eq!(
2752            store.first_recorded.as_deref(),
2753            Some("2026-05-29T10:00:00Z")
2754        );
2755    }
2756
2757    #[test]
2758    fn record_is_a_noop_in_ci() {
2759        // Impact is local-dev-only: it must never record on CI. The suite itself
2760        // runs on CI (where `CI` / `GITHUB_ACTIONS` are set), so the gate uses a
2761        // per-test override instead of the ambient env; here we force it true to
2762        // prove the production no-op. Without the gate, an enabled project would
2763        // record on every CI run.
2764        let (_config, dir) = test_env();
2765        let root = dir.path();
2766        assert!(enable(root));
2767        TEST_FORCE_CI.with(|c| c.set(true));
2768        record_v1(
2769            root,
2770            &summary(2, 1, 0),
2771            AuditVerdict::Warn,
2772            false,
2773            None,
2774            "2.0.0",
2775            "2026-05-29T10:00:00Z",
2776        );
2777        TEST_FORCE_CI.with(|c| c.set(false));
2778        let store = load(root);
2779        assert_eq!(store.records.len(), 0, "impact must not record while in CI");
2780    }
2781
2782    #[test]
2783    fn enable_writes_nothing_into_the_repo() {
2784        let (_config, dir) = test_env();
2785        let root = dir.path();
2786        enable(root);
2787        // The user-store relocation means enable never touches the repo: no
2788        // .gitignore mutation and no in-repo .fallow/ dir.
2789        assert!(
2790            !root.join(".gitignore").exists(),
2791            "enable must not create or modify the repo's .gitignore"
2792        );
2793        assert!(
2794            !root.join(".fallow").exists(),
2795            "enable must not create an in-repo .fallow/ dir"
2796        );
2797        // The decision IS persisted, in the user store.
2798        let store = load(root);
2799        assert!(store.enabled);
2800        assert!(store.explicit_decision);
2801        assert!(resolve_enabled(&store).0);
2802    }
2803
2804    #[test]
2805    fn single_record_yields_no_trend_no_spike() {
2806        let mut store = ImpactStore {
2807            enabled: true,
2808            ..Default::default()
2809        };
2810        store.records.push(ImpactRecord {
2811            timestamp: "t0".into(),
2812            version: "2.0.0".into(),
2813            git_sha: None,
2814            verdict: "warn".into(),
2815            gate: false,
2816            counts: ImpactCounts {
2817                total_issues: 5,
2818                dead_code: 5,
2819                complexity: 0,
2820                duplication: 0,
2821            },
2822        });
2823        let report = build_report(&store);
2824        assert!(report.trend.is_none());
2825        assert_eq!(report.surfacing.unwrap().total_issues, 5);
2826    }
2827
2828    #[test]
2829    fn empty_store_report_is_first_run() {
2830        let store = ImpactStore::default();
2831        let report = build_report(&store);
2832        assert_eq!(report.record_count, 0);
2833        assert!(report.trend.is_none());
2834        assert!(report.surfacing.is_none());
2835        let human = render_human(&report);
2836        assert!(human.contains("off")); // default store is disabled
2837    }
2838
2839    #[test]
2840    fn enabled_empty_store_shows_check_back() {
2841        let store = ImpactStore {
2842            enabled: true,
2843            ..Default::default()
2844        };
2845        let report = build_report(&store);
2846        let human = render_human(&report);
2847        assert!(human.contains("No history yet"));
2848        assert!(!human.contains("0 issues"));
2849    }
2850
2851    #[test]
2852    fn trend_improving_when_issues_drop() {
2853        let mut store = ImpactStore {
2854            enabled: true,
2855            ..Default::default()
2856        };
2857        for total in [8usize, 3usize] {
2858            store.records.push(ImpactRecord {
2859                timestamp: format!("t{total}"),
2860                version: "2.0.0".into(),
2861                git_sha: None,
2862                verdict: "warn".into(),
2863                gate: false,
2864                counts: ImpactCounts {
2865                    total_issues: total,
2866                    dead_code: total,
2867                    complexity: 0,
2868                    duplication: 0,
2869                },
2870            });
2871        }
2872        let report = build_report(&store);
2873        let trend = report.trend.unwrap();
2874        assert_eq!(trend.direction, ImpactTrendDirection::Improving);
2875        assert_eq!(trend.total_delta, -5);
2876    }
2877
2878    #[test]
2879    fn containment_blocked_then_cleared_records_one_event() {
2880        let (_config, dir) = test_env();
2881        let root = dir.path();
2882        enable(root);
2883        record_v1(
2884            root,
2885            &summary(2, 0, 0),
2886            AuditVerdict::Fail,
2887            true,
2888            Some("sha1"),
2889            "2.0.0",
2890            "t0",
2891        );
2892        let store = load(root);
2893        assert!(store.pending_containment.is_some());
2894        assert!(store.containment.is_empty());
2895
2896        record_v1(
2897            root,
2898            &summary(0, 0, 0),
2899            AuditVerdict::Pass,
2900            true,
2901            Some("sha2"),
2902            "2.0.0",
2903            "t1",
2904        );
2905        let store = load(root);
2906        assert!(store.pending_containment.is_none());
2907        assert_eq!(store.containment.len(), 1);
2908        assert_eq!(store.containment[0].blocked_at, "t0");
2909        assert_eq!(store.containment[0].cleared_at, "t1");
2910    }
2911
2912    #[test]
2913    fn non_gate_run_never_creates_containment() {
2914        let (_config, dir) = test_env();
2915        let root = dir.path();
2916        enable(root);
2917        record_v1(
2918            root,
2919            &summary(2, 0, 0),
2920            AuditVerdict::Fail,
2921            false,
2922            None,
2923            "2.0.0",
2924            "t0",
2925        );
2926        let store = load(root);
2927        assert!(store.pending_containment.is_none());
2928        assert!(store.containment.is_empty());
2929    }
2930
2931    #[test]
2932    fn corrupt_store_loads_as_default_no_panic() {
2933        let (_config, dir) = test_env();
2934        let root = dir.path();
2935        seed_store_raw(root, b"{ not valid json ][");
2936        let store = load(root);
2937        assert!(!store.enabled);
2938        assert!(store.records.is_empty());
2939        record_v1(
2940            root,
2941            &summary(1, 0, 0),
2942            AuditVerdict::Fail,
2943            true,
2944            None,
2945            "2.0.0",
2946            "t0",
2947        );
2948    }
2949
2950    #[test]
2951    fn records_are_bounded() {
2952        let mut store = ImpactStore {
2953            enabled: true,
2954            ..Default::default()
2955        };
2956        for i in 0..(MAX_RECORDS + 50) {
2957            store.records.push(ImpactRecord {
2958                timestamp: format!("t{i}"),
2959                version: "2.0.0".into(),
2960                git_sha: None,
2961                verdict: "pass".into(),
2962                gate: false,
2963                counts: ImpactCounts::default(),
2964            });
2965        }
2966        compact(&mut store);
2967        assert_eq!(store.records.len(), MAX_RECORDS);
2968        assert_eq!(store.records[0].timestamp, "t50");
2969    }
2970
2971    #[test]
2972    fn report_always_carries_schema_version() {
2973        let empty = build_report(&ImpactStore::default());
2974        assert_eq!(empty.schema_version, ImpactReportSchemaVersion::V1);
2975        let json = render_json(&empty);
2976        assert!(
2977            json.contains("\"schema_version\": \"1\""),
2978            "schema_version must be present (as the \"1\" const) even when disabled: {json}"
2979        );
2980
2981        let mut store = ImpactStore {
2982            enabled: true,
2983            ..Default::default()
2984        };
2985        store.records.push(ImpactRecord {
2986            timestamp: "2026-05-29T10:00:00Z".into(),
2987            version: "2.0.0".into(),
2988            git_sha: None,
2989            verdict: "pass".into(),
2990            gate: false,
2991            counts: ImpactCounts::default(),
2992        });
2993        assert_eq!(
2994            build_report(&store).schema_version,
2995            ImpactReportSchemaVersion::V1
2996        );
2997    }
2998
2999    #[test]
3000    fn date_only_trims_iso_timestamp() {
3001        assert_eq!(date_only("2026-05-29T18:15:23Z"), "2026-05-29");
3002        assert_eq!(date_only("2026-05-29"), "2026-05-29");
3003        assert_eq!(date_only("the first run"), "the first run");
3004    }
3005
3006    #[test]
3007    fn human_footer_shows_date_only() {
3008        let mut store = ImpactStore {
3009            enabled: true,
3010            ..Default::default()
3011        };
3012        store.first_recorded = Some("2026-05-29T18:15:23Z".into());
3013        store.records.push(ImpactRecord {
3014            timestamp: "2026-05-29T18:15:23Z".into(),
3015            version: "2.0.0".into(),
3016            git_sha: None,
3017            verdict: "pass".into(),
3018            gate: false,
3019            counts: ImpactCounts::default(),
3020        });
3021        let report = build_report(&store);
3022        let human = render_human(&report);
3023        assert!(
3024            human.contains("since 2026-05-29.") && !human.contains("18:15:23"),
3025            "human footer must show date-only: {human}"
3026        );
3027        let md = render_markdown(&report);
3028        assert!(
3029            md.contains("since 2026-05-29.") && !md.contains("18:15:23"),
3030            "markdown footer must show date-only: {md}"
3031        );
3032    }
3033
3034    #[test]
3035    fn future_schema_version_store_loads_without_panic_or_loss() {
3036        let (_config, dir) = test_env();
3037        let root = dir.path();
3038        let future = format!(
3039            "{{\"schema_version\":{},\"enabled\":true,\"records\":[],\"containment\":[]}}",
3040            STORE_SCHEMA_VERSION + 1
3041        );
3042        seed_store_raw(root, future.as_bytes());
3043        let store = load(root);
3044        assert_eq!(store.schema_version, STORE_SCHEMA_VERSION + 1);
3045        assert!(
3046            store.enabled,
3047            "future-version store must not degrade to default"
3048        );
3049    }
3050
3051    #[test]
3052    fn removed_finding_is_credited_as_resolved() {
3053        let (_config, dir) = test_env();
3054        let root = dir.path();
3055        enable(root);
3056        let a = touch(root, "src/a.ts");
3057        run(
3058            root,
3059            &[&a],
3060            vec![fi(&a, "unused-export", "foo")],
3061            vec![],
3062            &[],
3063            "t0",
3064        );
3065        assert_eq!(
3066            load(root).resolved_total,
3067            0,
3068            "first run only establishes a baseline"
3069        );
3070        run(root, &[&a], vec![], vec![], &[], "t1");
3071        let store = load(root);
3072        assert_eq!(store.resolved_total, 1);
3073        assert_eq!(store.suppressed_total, 0);
3074        assert_eq!(store.recent_resolved.len(), 1);
3075        assert_eq!(store.recent_resolved[0].kind, "unused-export");
3076        assert_eq!(store.recent_resolved[0].symbol.as_deref(), Some("foo"));
3077        assert_eq!(store.recent_resolved[0].path, "src/a.ts");
3078    }
3079
3080    #[test]
3081    fn suppressed_finding_is_not_a_win() {
3082        let (_config, dir) = test_env();
3083        let root = dir.path();
3084        enable(root);
3085        let a = touch(root, "src/a.ts");
3086        run(
3087            root,
3088            &[&a],
3089            vec![fi(&a, "unused-export", "foo")],
3090            vec![],
3091            &[],
3092            "t0",
3093        );
3094        run(
3095            root,
3096            &[&a],
3097            vec![],
3098            vec![],
3099            &[supp(&a, "unused-export")],
3100            "t1",
3101        );
3102        let store = load(root);
3103        assert_eq!(
3104            store.resolved_total, 0,
3105            "a suppression must never count as a win"
3106        );
3107        assert_eq!(store.suppressed_total, 1);
3108    }
3109
3110    #[test]
3111    fn fix_and_suppress_same_kind_credits_zero_resolved() {
3112        let (_config, dir) = test_env();
3113        let root = dir.path();
3114        enable(root);
3115        let a = touch(root, "src/a.ts");
3116        run(
3117            root,
3118            &[&a],
3119            vec![
3120                fi(&a, "unused-export", "foo"),
3121                fi(&a, "unused-export", "bar"),
3122            ],
3123            vec![],
3124            &[],
3125            "t0",
3126        );
3127        run(
3128            root,
3129            &[&a],
3130            vec![],
3131            vec![],
3132            &[supp(&a, "unused-export")],
3133            "t1",
3134        );
3135        let store = load(root);
3136        assert_eq!(store.resolved_total, 0);
3137        assert_eq!(store.suppressed_total, 2);
3138    }
3139
3140    #[test]
3141    fn within_file_move_is_not_resolved() {
3142        let (_config, dir) = test_env();
3143        let root = dir.path();
3144        enable(root);
3145        let a = touch(root, "src/a.ts");
3146        run(
3147            root,
3148            &[&a],
3149            vec![fi(&a, "unused-export", "foo")],
3150            vec![],
3151            &[],
3152            "t0",
3153        );
3154        run(
3155            root,
3156            &[&a],
3157            vec![fi(&a, "unused-export", "foo")],
3158            vec![],
3159            &[],
3160            "t1",
3161        );
3162        let store = load(root);
3163        assert_eq!(store.resolved_total, 0);
3164        assert_eq!(store.suppressed_total, 0);
3165    }
3166
3167    #[test]
3168    fn cross_file_move_in_same_run_is_not_resolved() {
3169        let (_config, dir) = test_env();
3170        let root = dir.path();
3171        enable(root);
3172        let a = touch(root, "src/a.ts");
3173        let b = touch(root, "src/b.ts");
3174        run(
3175            root,
3176            &[&a],
3177            vec![fi(&a, "unused-export", "foo")],
3178            vec![],
3179            &[],
3180            "t0",
3181        );
3182        run(
3183            root,
3184            &[&a, &b],
3185            vec![fi(&b, "unused-export", "foo")],
3186            vec![],
3187            &[],
3188            "t1",
3189        );
3190        assert_eq!(
3191            load(root).resolved_total,
3192            0,
3193            "a cross-file move is not a resolution"
3194        );
3195    }
3196
3197    #[test]
3198    fn cross_run_move_uncredits_the_prior_resolution() {
3199        let (_config, dir) = test_env();
3200        let root = dir.path();
3201        enable(root);
3202        let a = touch(root, "src/a.ts");
3203        let b = touch(root, "src/b.ts");
3204        run(
3205            root,
3206            &[&a],
3207            vec![fi(&a, "unused-export", "foo")],
3208            vec![],
3209            &[],
3210            "t0",
3211        );
3212        run(root, &[&a], vec![], vec![], &[], "t1");
3213        assert_eq!(
3214            load(root).resolved_total,
3215            1,
3216            "source disappearance credited in run A"
3217        );
3218        run(
3219            root,
3220            &[&b],
3221            vec![fi(&b, "unused-export", "foo")],
3222            vec![],
3223            &[],
3224            "t2",
3225        );
3226        let store = load(root);
3227        assert_eq!(
3228            store.resolved_total, 0,
3229            "cross-run move must un-credit the phantom win"
3230        );
3231        assert!(
3232            store.recent_resolved.is_empty(),
3233            "the stale resolution event is dropped"
3234        );
3235    }
3236
3237    #[test]
3238    fn resolved_complexity_finding_and_suppressed_complexity() {
3239        let (_config, dir) = test_env();
3240        let root = dir.path();
3241        enable(root);
3242        let a = touch(root, "src/a.ts");
3243        run(
3244            root,
3245            &[&a],
3246            vec![fi(&a, "complexity", "bigFn")],
3247            vec![],
3248            &[],
3249            "t0",
3250        );
3251        run(root, &[&a], vec![], vec![], &[supp(&a, "complexity")], "t1");
3252        let store = load(root);
3253        assert_eq!(store.resolved_total, 0);
3254        assert_eq!(store.suppressed_total, 1);
3255
3256        let b = touch(root, "src/b.ts");
3257        run(
3258            root,
3259            &[&b],
3260            vec![fi(&b, "complexity", "huge")],
3261            vec![],
3262            &[],
3263            "t2",
3264        );
3265        run(root, &[&b], vec![], vec![], &[], "t3");
3266        assert_eq!(load(root).resolved_total, 1);
3267    }
3268
3269    #[test]
3270    fn resolved_duplication_clone_group() {
3271        let (_config, dir) = test_env();
3272        let root = dir.path();
3273        enable(root);
3274        let a = touch(root, "src/a.ts");
3275        let b = touch(root, "src/b.ts");
3276        let clone = CloneInput {
3277            fingerprint: "dup:abc12345".to_owned(),
3278            instance_paths: vec![a.clone(), b],
3279        };
3280        run(root, &[&a], vec![], vec![clone], &[], "t0");
3281        run(root, &[&a], vec![], vec![], &[], "t1");
3282        let store = load(root);
3283        assert_eq!(store.resolved_total, 1);
3284        assert_eq!(store.recent_resolved[0].kind, "code-duplication");
3285    }
3286
3287    #[test]
3288    fn blanket_suppression_covers_any_kind() {
3289        let (_config, dir) = test_env();
3290        let root = dir.path();
3291        enable(root);
3292        let a = touch(root, "src/a.ts");
3293        run(
3294            root,
3295            &[&a],
3296            vec![fi(&a, "unused-export", "foo")],
3297            vec![],
3298            &[],
3299            "t0",
3300        );
3301        let blanket = ActiveSuppression {
3302            path: a.clone(),
3303            kind: None,
3304            is_file_level: true,
3305        };
3306        run(root, &[&a], vec![], vec![], &[blanket], "t1");
3307        let store = load(root);
3308        assert_eq!(store.resolved_total, 0);
3309        assert_eq!(store.suppressed_total, 1);
3310    }
3311
3312    #[test]
3313    fn v1_store_loads_and_upgrades_to_v2() {
3314        let (_config, dir) = test_env();
3315        let root = dir.path();
3316        let v1 = r#"{"schema_version":1,"enabled":true,"first_recorded":"t0","records":[{"timestamp":"t0","version":"2.0.0","verdict":"warn","gate":false,"counts":{"total_issues":1,"dead_code":1,"complexity":0,"duplication":0}}],"containment":[]}"#;
3317        seed_store_raw(root, v1.as_bytes());
3318        let store = load(root);
3319        assert_eq!(store.schema_version, 1);
3320        assert!(store.frontier.is_empty());
3321        assert_eq!(store.resolved_total, 0);
3322        let a = touch(root, "src/a.ts");
3323        run(
3324            root,
3325            &[&a],
3326            vec![fi(&a, "unused-export", "foo")],
3327            vec![],
3328            &[],
3329            "t1",
3330        );
3331        let store = load(root);
3332        assert_eq!(store.schema_version, STORE_SCHEMA_VERSION);
3333        assert!(frontier_paths(&store).contains("src/a.ts"));
3334    }
3335
3336    #[test]
3337    fn recent_resolved_is_bounded() {
3338        let mut store = ImpactStore {
3339            enabled: true,
3340            ..Default::default()
3341        };
3342        for i in 0..(MAX_RECENT_RESOLVED + 25) {
3343            store.recent_resolved.push(ResolutionEvent {
3344                kind: "unused-export".into(),
3345                path: format!("src/f{i}.ts"),
3346                symbol: Some(format!("s{i}")),
3347                git_sha: None,
3348                timestamp: format!("t{i}"),
3349            });
3350        }
3351        bound_recent_resolved(&mut store);
3352        assert_eq!(store.recent_resolved.len(), MAX_RECENT_RESOLVED);
3353        assert_eq!(store.recent_resolved[0].path, "src/f25.ts");
3354    }
3355
3356    #[test]
3357    fn frontier_prunes_deleted_files() {
3358        let (_config, dir) = test_env();
3359        let root = dir.path();
3360        enable(root);
3361        let a = touch(root, "src/a.ts");
3362        run(
3363            root,
3364            &[&a],
3365            vec![fi(&a, "unused-export", "foo")],
3366            vec![],
3367            &[],
3368            "t0",
3369        );
3370        assert!(frontier_paths(&load(root)).contains("src/a.ts"));
3371        std::fs::remove_file(&a).unwrap();
3372        let b = touch(root, "src/b.ts");
3373        run(root, &[&b], vec![], vec![], &[], "t1");
3374        assert!(!frontier_paths(&load(root)).contains("src/a.ts"));
3375    }
3376
3377    #[test]
3378    fn honest_empty_state_before_attribution_baseline() {
3379        let store = ImpactStore {
3380            enabled: true,
3381            records: vec![ImpactRecord {
3382                timestamp: "t0".into(),
3383                version: "2.0.0".into(),
3384                git_sha: None,
3385                verdict: "warn".into(),
3386                gate: false,
3387                counts: ImpactCounts::default(),
3388            }],
3389            ..Default::default()
3390        };
3391        let report = build_report(&store);
3392        assert!(!report.attribution_active);
3393        let human = render_human(&report);
3394        assert!(human.contains("resolution tracking starts from your next gate run"));
3395        assert!(!human.contains("0 finding"));
3396    }
3397
3398    #[test]
3399    fn suppression_only_state_renders_under_a_resolved_header() {
3400        let report = ImpactReport {
3401            schema_version: ImpactReportSchemaVersion::V1,
3402            enabled: true,
3403            enabled_source: EnabledSource::Project,
3404            record_count: 2,
3405            meta: None,
3406            first_recorded: Some("2026-05-29T10:00:00Z".into()),
3407            latest_git_sha: None,
3408            surfacing: Some(ImpactCounts::default()),
3409            trend: None,
3410            project_surfacing: None,
3411            project_trend: None,
3412            containment_count: 0,
3413            recent_containment: vec![],
3414            resolved_total: 0,
3415            suppressed_total: 2,
3416            recent_resolved: vec![],
3417            attribution_active: true,
3418            onboarding_declined: false,
3419            explicit_decision: false,
3420        };
3421        let human = render_human(&report);
3422        let resolved_idx = human.find("  RESOLVED").expect("RESOLVED header present");
3423        let supp_idx = human
3424            .find("2 findings you marked intentional")
3425            .expect("suppression line present");
3426        assert!(
3427            resolved_idx < supp_idx,
3428            "suppression must render under RESOLVED"
3429        );
3430        assert!(human.contains("none yet"));
3431
3432        let md = render_markdown(&report);
3433        assert!(
3434            md.contains("- **Resolved:**"),
3435            "markdown always has a Resolved bullet"
3436        );
3437        assert!(md.contains("- **Marked intentional:** 2 finding"));
3438    }
3439
3440    /// Build a `CloneInput` over real absolute paths (built from `root`).
3441    fn clone_at(fingerprint: &str, paths: &[&Path]) -> CloneInput {
3442        CloneInput {
3443            fingerprint: fingerprint.to_owned(),
3444            instance_paths: paths.iter().map(|p| p.to_path_buf()).collect(),
3445        }
3446    }
3447
3448    /// Record a WHOLE-PROJECT run via the real combined-track recorder
3449    /// (`record_combined_run` with `Scope::WholeProject`), exercising the same
3450    /// path `combined.rs` uses on a full `fallow` run.
3451    fn run_wp(
3452        root: &Path,
3453        findings: Vec<FindingInput>,
3454        clones: Vec<CloneInput>,
3455        supps: &[ActiveSuppression],
3456        ts: &str,
3457    ) {
3458        let input = AttributionInput {
3459            root,
3460            scope: Scope::WholeProject,
3461            findings,
3462            clones,
3463            suppressions: supps,
3464        };
3465        record_combined_run(
3466            root,
3467            ImpactCounts::default(),
3468            Some("sha"),
3469            "2.0.0",
3470            ts,
3471            Some(&input),
3472        );
3473    }
3474
3475    #[test]
3476    fn whole_project_run_does_not_double_credit_after_audit() {
3477        let (_config, dir) = test_env();
3478        let root = dir.path();
3479        enable(root);
3480        let a = touch(root, "src/a.ts");
3481        let b = touch(root, "src/b.ts");
3482        run(
3483            root,
3484            &[&a, &b],
3485            vec![],
3486            vec![clone_at("dup:abc", &[&a, &b])],
3487            &[],
3488            "t1",
3489        );
3490        assert_eq!(clone_fingerprints(&load(root)).len(), 1);
3491
3492        run(root, &[&a, &b], vec![], vec![], &[], "t2");
3493        assert_eq!(load(root).resolved_total, 1);
3494        assert!(load(root).clone_frontier.is_empty());
3495
3496        run_wp(root, vec![], vec![], &[], "t3");
3497        assert_eq!(
3498            load(root).resolved_total,
3499            1,
3500            "whole-project run re-credited a resolution"
3501        );
3502    }
3503
3504    #[test]
3505    fn whole_project_run_credits_suppressed_not_resolved() {
3506        let (_config, dir) = test_env();
3507        let root = dir.path();
3508        enable(root);
3509        let util = touch(root, "src/util.ts");
3510        run(
3511            root,
3512            &[&util],
3513            vec![fi(&util, "unused-export", "dead")],
3514            vec![],
3515            &[],
3516            "t1",
3517        );
3518        assert_eq!(frontier_paths(&load(root)).len(), 1);
3519
3520        run_wp(root, vec![], vec![], &[supp(&util, "unused-export")], "t2");
3521        let store = load(root);
3522        assert_eq!(
3523            store.suppressed_total, 1,
3524            "suppressed finding not counted suppressed"
3525        );
3526        assert_eq!(
3527            store.resolved_total, 0,
3528            "suppressed finding wrongly counted resolved"
3529        );
3530    }
3531
3532    #[test]
3533    fn clone_reshape_three_to_two_not_credited_as_resolved() {
3534        let (_config, dir) = test_env();
3535        let root = dir.path();
3536        enable(root);
3537        let a = touch(root, "src/a.ts");
3538        let b = touch(root, "src/b.ts");
3539        let c = touch(root, "src/c.ts");
3540        run(
3541            root,
3542            &[&a, &b, &c],
3543            vec![],
3544            vec![clone_at("dup:aaa", &[&a, &b, &c])],
3545            &[],
3546            "t1",
3547        );
3548        assert_eq!(clone_fingerprints(&load(root)).len(), 1);
3549
3550        run_wp(
3551            root,
3552            vec![],
3553            vec![clone_at("dup:bbb", &[&a, &b])],
3554            &[],
3555            "t2",
3556        );
3557        let store = load(root);
3558        assert_eq!(
3559            store.resolved_total, 0,
3560            "clone reshape miscredited as resolved"
3561        );
3562        assert!(clone_fingerprints(&store).contains("dup:bbb"));
3563        assert!(!clone_fingerprints(&store).contains("dup:aaa"));
3564    }
3565
3566    fn rcounts(total: usize, dead: usize, complexity: usize, dup: usize) -> ImpactCounts {
3567        ImpactCounts {
3568            total_issues: total,
3569            dead_code: dead,
3570            complexity,
3571            duplication: dup,
3572        }
3573    }
3574
3575    fn rtrend(prev: usize, cur: usize) -> TrendSummary {
3576        TrendSummary {
3577            direction: direction_for(cur as i64 - prev as i64),
3578            total_delta: cur as i64 - prev as i64,
3579            previous_total: prev,
3580            current_total: cur,
3581        }
3582    }
3583
3584    /// Build a report literal for render-state tests.
3585    fn rreport(
3586        record_count: usize,
3587        first_recorded: Option<&str>,
3588        surfacing: Option<ImpactCounts>,
3589        trend: Option<TrendSummary>,
3590        project_surfacing: Option<ImpactCounts>,
3591        project_trend: Option<TrendSummary>,
3592        attribution_active: bool,
3593    ) -> ImpactReport {
3594        ImpactReport {
3595            schema_version: ImpactReportSchemaVersion::V1,
3596            enabled: true,
3597            enabled_source: EnabledSource::Project,
3598            record_count,
3599            meta: None,
3600            first_recorded: first_recorded.map(ToOwned::to_owned),
3601            latest_git_sha: None,
3602            surfacing,
3603            trend,
3604            project_surfacing,
3605            project_trend,
3606            containment_count: 0,
3607            recent_containment: vec![],
3608            resolved_total: 0,
3609            suppressed_total: 0,
3610            recent_resolved: vec![],
3611            attribution_active,
3612            onboarding_declined: false,
3613            explicit_decision: false,
3614        }
3615    }
3616
3617    #[test]
3618    fn render_human_project_only_store_shows_whole_project_not_empty_state() {
3619        let r = rreport(
3620            0,
3621            Some("2026-05-30T10:00:00Z"),
3622            None,
3623            None,
3624            Some(rcounts(1, 1, 0, 0)),
3625            None,
3626            true,
3627        );
3628        let human = render_human(&r);
3629        assert!(
3630            human.contains("WHOLE PROJECT (whole-repo context, not a to-do)"),
3631            "project-only must render the labeled section"
3632        );
3633        assert!(human.contains("1 issue across the whole project"));
3634        assert!(
3635            human.contains("project trend starts after your next full `fallow` run"),
3636            "single project record => no trend line, shows the next-run hint"
3637        );
3638        assert!(human.contains("Tracking since 2026-05-30"));
3639        assert!(
3640            !human.contains("No history yet"),
3641            "must not show the empty-state copy"
3642        );
3643        assert!(
3644            !human.contains("LATEST RUN"),
3645            "no changed-file track recorded"
3646        );
3647        assert!(
3648            !human.contains("recorded audit run"),
3649            "no audit runs => no changed-file footer"
3650        );
3651    }
3652
3653    #[test]
3654    fn render_human_both_tracks_label_actionable_vs_context() {
3655        let r = rreport(
3656            3,
3657            Some("2026-05-29T10:00:00Z"),
3658            Some(rcounts(4, 4, 0, 0)),
3659            Some(rtrend(6, 4)),
3660            Some(rcounts(40, 30, 5, 5)),
3661            Some(rtrend(45, 40)),
3662            true,
3663        );
3664        let human = render_human(&r);
3665        let latest = human
3666            .find("LATEST RUN (changed files, act on these now)")
3667            .expect("LATEST RUN labeled actionable");
3668        let whole = human
3669            .find("WHOLE PROJECT (whole-repo context, not a to-do)")
3670            .expect("WHOLE PROJECT labeled context");
3671        assert!(
3672            latest < whole,
3673            "changed-file section renders before whole-project"
3674        );
3675        assert!(human.contains("45 -> 40 (down) across your last two full runs"));
3676        assert!(human.contains("advances only on your local full `fallow` runs, not CI"));
3677    }
3678
3679    #[test]
3680    fn render_markdown_project_only_store_shows_whole_project_not_empty_state() {
3681        let r = rreport(
3682            0,
3683            Some("2026-05-30T10:00:00Z"),
3684            None,
3685            None,
3686            Some(rcounts(1, 1, 0, 0)),
3687            None,
3688            true,
3689        );
3690        let md = render_markdown(&r);
3691        assert!(
3692            md.contains(
3693                "- **Whole project (whole-repo context, last full `fallow` run):** 1 issue"
3694            ),
3695            "project-only md must render the labeled whole-project line"
3696        );
3697        assert!(
3698            !md.contains("No history yet"),
3699            "project-only md must not show empty state"
3700        );
3701        assert!(md.contains("Tracking since 2026-05-30"));
3702    }
3703
3704    #[test]
3705    fn resolve_enabled_precedence_table() {
3706        let (_config, _dir) = test_env();
3707        // enabled-true is an explicit project opt-in regardless of the flag.
3708        let on = ImpactStore {
3709            enabled: true,
3710            ..Default::default()
3711        };
3712        assert_eq!(resolve_enabled(&on), (true, EnabledSource::Project));
3713
3714        // explicitly disabled here stays off as a Project decision.
3715        let off_explicit = ImpactStore {
3716            enabled: false,
3717            explicit_decision: true,
3718            ..Default::default()
3719        };
3720        assert_eq!(
3721            resolve_enabled(&off_explicit),
3722            (false, EnabledSource::Project)
3723        );
3724
3725        // never-asked + no global default => off (Default).
3726        let never = ImpactStore::default();
3727        assert_eq!(resolve_enabled(&never), (false, EnabledSource::Default));
3728
3729        // never-asked + global default on => on (User).
3730        assert!(set_global_default(true));
3731        assert_eq!(resolve_enabled(&never), (true, EnabledSource::User));
3732        // a per-repo disable still wins over the global default.
3733        assert_eq!(
3734            resolve_enabled(&off_explicit),
3735            (false, EnabledSource::Project)
3736        );
3737    }
3738
3739    #[test]
3740    fn human_report_explains_user_global_default() {
3741        let (_config, _dir) = test_env();
3742        set_global_default(true);
3743        // A never-asked store resolved on a project: enabled via the global default.
3744        let report = build_report(&ImpactStore::default());
3745        assert_eq!(report.enabled_source, EnabledSource::User);
3746        let human = render_human(&report);
3747        assert!(
3748            human.contains("Enabled by your user-global default"),
3749            "human report must explain a global-default enable: {human}"
3750        );
3751        // A project-enabled report does NOT show the global-default note.
3752        let project = build_report(&ImpactStore {
3753            enabled: true,
3754            explicit_decision: true,
3755            ..Default::default()
3756        });
3757        assert_eq!(project.enabled_source, EnabledSource::Project);
3758        assert!(!render_human(&project).contains("user-global default"));
3759    }
3760
3761    #[test]
3762    fn global_default_round_trips() {
3763        let (_config, _dir) = test_env();
3764        assert!(!load_global_default());
3765        assert!(set_global_default(true));
3766        assert!(load_global_default());
3767        assert!(!set_global_default(true)); // unchanged
3768        assert!(set_global_default(false));
3769        assert!(!load_global_default());
3770    }
3771
3772    #[test]
3773    fn global_default_records_without_per_repo_enable() {
3774        let (_config, dir) = test_env();
3775        let root = dir.path();
3776        set_global_default(true);
3777        // No `enable(root)` call: the global default alone should activate.
3778        record_v1(
3779            root,
3780            &summary(2, 0, 0),
3781            AuditVerdict::Warn,
3782            false,
3783            None,
3784            "2.0.0",
3785            "t0",
3786        );
3787        let report = build_report(&load(root));
3788        assert!(report.enabled);
3789        assert_eq!(report.enabled_source, EnabledSource::User);
3790        assert_eq!(report.record_count, 1);
3791    }
3792
3793    #[test]
3794    fn legacy_in_repo_store_is_migrated_on_first_load() {
3795        let (_config, dir) = test_env();
3796        let root = dir.path();
3797        // Seed a pre-relocation v3 store with a FLAT frontier in the repo.
3798        let legacy = r#"{"schema_version":3,"enabled":true,"explicit_decision":true,
3799            "records":[{"timestamp":"t0","version":"2.0.0","verdict":"warn","gate":false,
3800            "counts":{"total_issues":1,"dead_code":1,"complexity":0,"duplication":0}}],
3801            "resolved_total":2,
3802            "frontier":{"src/a.ts":{"findings":[{"id":"x","kind":"unused-export","symbol":"foo"}],"suppressions":[]}},
3803            "containment":[]}"#;
3804        std::fs::create_dir_all(root.join(".fallow")).unwrap();
3805        std::fs::write(legacy_store_path(root), legacy).unwrap();
3806
3807        let store = load(root);
3808        assert!(store.enabled);
3809        assert_eq!(store.schema_version, STORE_SCHEMA_VERSION);
3810        assert_eq!(store.records.len(), 1);
3811        assert_eq!(store.resolved_total, 2);
3812        // The flat frontier was wrapped under the worktree key (nested v4 shape).
3813        assert!(frontier_paths(&store).contains("src/a.ts"));
3814        // The user store now exists, so a second load does NOT re-import (it
3815        // reads the user store directly).
3816        assert!(store_path(root).is_some_and(|p| p.exists()));
3817        let again = load(root);
3818        assert_eq!(again.records.len(), 1);
3819    }
3820
3821    #[test]
3822    fn reset_removes_only_this_project() {
3823        let (_config, dir) = test_env();
3824        let root = dir.path();
3825        enable(root);
3826        record_v1(
3827            root,
3828            &summary(1, 0, 0),
3829            AuditVerdict::Warn,
3830            false,
3831            None,
3832            "2.0.0",
3833            "t0",
3834        );
3835        assert_eq!(load(root).records.len(), 1);
3836        assert!(reset(root));
3837        assert!(load(root).records.is_empty());
3838        assert!(!reset(root)); // already gone
3839    }
3840
3841    #[test]
3842    fn reset_all_clears_dir_but_keeps_global_default() {
3843        let (_config, dir) = test_env();
3844        let root = dir.path();
3845        set_global_default(true);
3846        enable(root);
3847        assert!(load(root).enabled);
3848        assert!(reset_all());
3849        // The global default toggle survives a data wipe.
3850        assert!(load_global_default());
3851    }
3852
3853    // ----- cross-repo aggregate (`impact --all`) tests --------------------
3854
3855    /// Set an isolated config dir (no project root needed) and return its guard.
3856    fn aggregate_env() -> tempfile::TempDir {
3857        let config = tempfile::tempdir().unwrap();
3858        TEST_CONFIG_DIR.with(|c| *c.borrow_mut() = Some(config.path().to_path_buf()));
3859        config
3860    }
3861
3862    /// Write a store file directly under `<config>/impact/<key>.json`.
3863    fn seed_store(key: &str, store: &ImpactStore) {
3864        let dir = impact_config_dir().unwrap().join("impact");
3865        std::fs::create_dir_all(&dir).unwrap();
3866        std::fs::write(
3867            dir.join(format!("{key}.json")),
3868            serde_json::to_string_pretty(store).unwrap(),
3869        )
3870        .unwrap();
3871    }
3872
3873    fn store_with(
3874        label: &str,
3875        resolved: usize,
3876        contained: usize,
3877        latest_ts: &str,
3878        latest_issues: usize,
3879    ) -> ImpactStore {
3880        let mut s = ImpactStore {
3881            enabled: true,
3882            explicit_decision: true,
3883            resolved_total: resolved,
3884            label: Some(label.to_owned()),
3885            ..Default::default()
3886        };
3887        s.records.push(ImpactRecord {
3888            timestamp: latest_ts.to_owned(),
3889            version: "2.0.0".to_owned(),
3890            git_sha: None,
3891            verdict: "warn".to_owned(),
3892            gate: false,
3893            counts: ImpactCounts::from_combined(latest_issues, 0, 0),
3894        });
3895        for _ in 0..contained {
3896            s.containment.push(ContainmentEvent {
3897                blocked_at: "t0".to_owned(),
3898                cleared_at: "t1".to_owned(),
3899                git_sha: None,
3900                blocked_counts: ImpactCounts::default(),
3901            });
3902        }
3903        s
3904    }
3905
3906    #[test]
3907    fn repo_basename_returns_last_component_only() {
3908        assert_eq!(
3909            repo_basename(Path::new("/a/b/myrepo/.git")).as_deref(),
3910            Some("myrepo")
3911        );
3912        assert_eq!(
3913            repo_basename(Path::new("/a/b/proj")).as_deref(),
3914            Some("proj")
3915        );
3916        // Never a separator in the result.
3917        let name = repo_basename(Path::new("/x/y/z/.git")).unwrap();
3918        assert!(!name.contains('/') && !name.contains('\\'));
3919    }
3920
3921    #[test]
3922    fn aggregate_rolls_up_totals_and_excludes_empty() {
3923        let _cfg = aggregate_env();
3924        seed_store(
3925            "aaa",
3926            &store_with("alpha", 10, 2, "2026-06-10T00:00:00Z", 3),
3927        );
3928        seed_store("bbb", &store_with("beta", 5, 1, "2026-06-11T00:00:00Z", 0));
3929        // enabled-but-empty: no records, no resolved, no containment.
3930        seed_store(
3931            "ccc",
3932            &ImpactStore {
3933                enabled: true,
3934                explicit_decision: true,
3935                label: Some("gamma".into()),
3936                ..Default::default()
3937            },
3938        );
3939        let report = aggregate(CrossRepoSort::Recent);
3940        assert_eq!(report.project_count, 3, "all three stores enumerated");
3941        assert_eq!(report.tracked_count, 2, "empty store excluded from rows");
3942        assert_eq!(report.totals.resolved_total, 15);
3943        assert_eq!(report.totals.containment_count, 3);
3944        assert_eq!(report.unreadable_count, 0);
3945    }
3946
3947    #[test]
3948    fn aggregate_sort_recent_orders_by_last_activity() {
3949        let _cfg = aggregate_env();
3950        seed_store("old", &store_with("older", 1, 0, "2026-06-01T00:00:00Z", 1));
3951        seed_store("new", &store_with("newer", 1, 0, "2026-06-12T00:00:00Z", 1));
3952        let report = aggregate(CrossRepoSort::Recent);
3953        assert_eq!(report.projects[0].label.as_deref(), Some("newer"));
3954        assert_eq!(report.projects[1].label.as_deref(), Some("older"));
3955    }
3956
3957    #[test]
3958    fn cross_repo_json_carries_kind_and_leaks_no_path() {
3959        let _cfg = aggregate_env();
3960        seed_store("aaa", &store_with("alpha", 4, 1, "2026-06-10T00:00:00Z", 2));
3961        let report = aggregate(CrossRepoSort::Recent);
3962        let json = render_cross_repo_json(&report);
3963        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
3964        assert_eq!(value["kind"], "impact-cross-repo");
3965        // No label (or any string) may contain a path separator.
3966        for entry in value["projects"].as_array().unwrap() {
3967            if let Some(label) = entry["label"].as_str() {
3968                assert!(
3969                    !label.contains('/') && !label.contains('\\'),
3970                    "label must be a basename, got {label}"
3971                );
3972            }
3973        }
3974        assert!(
3975            !json.contains('/') || !json.contains("Users"),
3976            "json must not leak an absolute home path"
3977        );
3978    }
3979
3980    #[test]
3981    fn cross_repo_markdown_pluralizes_single_project() {
3982        let _cfg = aggregate_env();
3983        seed_store("solo", &store_with("solo", 3, 1, "2026-06-10T00:00:00Z", 2));
3984        let report = aggregate(CrossRepoSort::Recent);
3985        assert_eq!(report.project_count, 1);
3986        assert_eq!(report.tracked_count, 1);
3987        let md = render_cross_repo_markdown(&report);
3988        assert!(
3989            md.contains("1 project tracked"),
3990            "single project must read 'project', got:\n{md}"
3991        );
3992        assert!(
3993            !md.contains("1 projects tracked"),
3994            "must not pluralize a single project, got:\n{md}"
3995        );
3996        assert!(
3997            md.contains("across 1 tracked project"),
3998            "grand totals must read 'tracked project' (singular), got:\n{md}"
3999        );
4000        assert!(
4001            !md.contains("tracked projects"),
4002            "must not pluralize a single tracked project, got:\n{md}"
4003        );
4004    }
4005
4006    #[test]
4007    fn cross_repo_corrupt_file_is_skipped_and_counted() {
4008        let _cfg = aggregate_env();
4009        seed_store("good", &store_with("good", 3, 0, "2026-06-10T00:00:00Z", 1));
4010        let dir = impact_config_dir().unwrap().join("impact");
4011        std::fs::write(dir.join("bad.json"), b"{ not valid json ][").unwrap();
4012        let report = aggregate(CrossRepoSort::Recent);
4013        assert_eq!(report.tracked_count, 1, "good store still aggregated");
4014        assert_eq!(
4015            report.unreadable_count, 1,
4016            "corrupt file counted, not crashed"
4017        );
4018    }
4019
4020    #[test]
4021    fn cross_repo_empty_dir_is_first_run() {
4022        let _cfg = aggregate_env();
4023        let report = aggregate(CrossRepoSort::Recent);
4024        assert_eq!(report.project_count, 0);
4025        let human = render_cross_repo_human(&report, None);
4026        assert!(human.contains("No projects tracked yet"));
4027    }
4028
4029    #[test]
4030    fn cross_repo_all_corrupt_reports_unreadable_not_first_run() {
4031        let _cfg = aggregate_env();
4032        let dir = impact_config_dir().unwrap().join("impact");
4033        std::fs::create_dir_all(&dir).unwrap();
4034        std::fs::write(dir.join("bad.json"), b"{ broken ][").unwrap();
4035        let report = aggregate(CrossRepoSort::Recent);
4036        assert_eq!(report.project_count, 0);
4037        assert_eq!(report.unreadable_count, 1);
4038        let human = render_cross_repo_human(&report, None);
4039        assert!(
4040            human.contains("unreadable store") && !human.contains("No projects tracked yet"),
4041            "all-corrupt must report unreadable, not a misleading first-run hint: {human}"
4042        );
4043    }
4044
4045    #[test]
4046    fn record_audit_run_captures_basename_label() {
4047        let (_config, dir) = test_env();
4048        let root = dir.path();
4049        enable(root);
4050        record_v1(
4051            root,
4052            &summary(1, 0, 0),
4053            AuditVerdict::Warn,
4054            false,
4055            None,
4056            "2.0.0",
4057            "t0",
4058        );
4059        let label = load(root).label.expect("label captured on record");
4060        assert!(
4061            !label.contains('/') && !label.contains('\\'),
4062            "label must be a basename, got {label}"
4063        );
4064    }
4065
4066    // ----- store advisory lock + age-based GC --------------------------------
4067
4068    #[test]
4069    fn lock_path_appends_lock_suffix() {
4070        assert_eq!(
4071            lock_path_for(Path::new("/c/fallow/impact/abc.json")),
4072            PathBuf::from("/c/fallow/impact/abc.json.lock")
4073        );
4074    }
4075
4076    #[test]
4077    fn store_lock_acquire_drop_then_record_roundtrips() {
4078        let (_config, dir) = test_env();
4079        let root = dir.path();
4080        enable(root);
4081        // Acquiring + dropping the lock around a record must not deadlock and
4082        // the record must persist.
4083        {
4084            let _lock = ImpactStoreLock::acquire(root).expect("lock acquires");
4085        }
4086        record_v1(
4087            root,
4088            &summary(1, 0, 0),
4089            AuditVerdict::Warn,
4090            false,
4091            None,
4092            "2.0.0",
4093            "t0",
4094        );
4095        assert_eq!(load(root).records.len(), 1, "record persisted under lock");
4096        // The lock sidecar lives next to the store and is never the store itself.
4097        let store = store_path(root).unwrap();
4098        assert!(lock_path_for(&store).exists(), "lock sidecar created");
4099        assert!(store.exists(), "store file is distinct from its lock");
4100    }
4101
4102    #[test]
4103    fn sweep_keeps_fresh_and_self_deletes_aged_out() {
4104        let _cfg = aggregate_env();
4105        seed_store("keepme", &store_with("keep", 1, 0, "t0", 1));
4106        seed_store("oldone", &store_with("old", 1, 0, "t0", 1));
4107        // A `.lock` sidecar must survive the sweep (lock-lifecycle invariant).
4108        let lock = impact_config_dir()
4109            .unwrap()
4110            .join("impact")
4111            .join("oldone.json.lock");
4112        std::fs::write(&lock, b"").unwrap();
4113
4114        // max_age = 0 ages out every non-kept store regardless of mtime.
4115        sweep_old_stores("keepme", std::time::Duration::ZERO);
4116
4117        let dir = impact_config_dir().unwrap().join("impact");
4118        assert!(dir.join("keepme.json").exists(), "kept store survives");
4119        assert!(
4120            !dir.join("oldone.json").exists(),
4121            "aged-out store reclaimed"
4122        );
4123        assert!(lock.exists(), "lock sidecar never deleted by the sweep");
4124    }
4125
4126    #[test]
4127    fn sweep_keeps_everything_under_a_large_window() {
4128        let _cfg = aggregate_env();
4129        seed_store("a", &store_with("a", 1, 0, "t0", 1));
4130        seed_store("b", &store_with("b", 1, 0, "t0", 1));
4131        // 10-year window: freshly-written stores are never aged out.
4132        sweep_old_stores("a", std::time::Duration::from_hours(10 * 365 * 24));
4133        let dir = impact_config_dir().unwrap().join("impact");
4134        assert!(dir.join("a.json").exists());
4135        assert!(dir.join("b.json").exists());
4136    }
4137}