specfold-core 0.12.0

Library behind specfold: typed Markdown documents, owners, a derived graph and checks.
Documentation
//! D1: in a staged or ranged run, code changed under an active document's `governs` while the
//! document, and every change note pointing at it, stayed as they were.

use crate::check::context::Ctx;
use crate::check::finding::Finding;
use crate::check::rule::skipped;
use crate::check::rule::{Rule, RuleInfo};
use crate::corpus::graph::query::GoverningIndex;
use crate::model::Tier;
use crate::workspace::vcs;
use std::collections::{BTreeMap, BTreeSet};

/// How D1 names a moved gitlink in place of the files inside it.
fn label(m: &vcs::GitlinkMove) -> String {
    let short = |c: &str| c.chars().take(7).collect::<String>();
    match &m.old {
        Some(old) => {
            format!("{} (pin {}..{}, {} file(s) changed inside)", m.path, short(old), short(&m.new), m.changed.len())
        }
        None => format!(
            "{} (new submodule at {}: {} file(s) moved in unchanged, {} new or changed)",
            m.path,
            short(&m.new),
            m.moved.len(),
            m.changed.len()
        ),
    }
}

pub struct GovernedCodeDrift;

/// How many changed paths a finding names before it says "+N more".
const SHOWN: usize = 5;

impl Rule for GovernedCodeDrift {
    fn info(&self) -> &RuleInfo {
        &RuleInfo {
            id: "D1",
            title: "changed code moves its governing document",
            explain: "In a staged or ranged run (`spec check --staged`, `--changed`), each changed file that is not a document is matched against the explicit `rel.governs` globs of active, committed documents. A document whose governed code changed is expected to change in the same range, or to be named by a `change` note that changes in the range, or to cover a path that such a note governs (a note on the source file speaks for its test beside it); otherwise this warning lists the changed paths. Documents that govern only through their owner directory are not asked, and a full run never reports D1. Deleted files and the old paths of renamed files count as changed. A submodule pointer the change moves (a pin bump) is one change, named once per document as `<path> (pin <old>..<new>, N file(s) changed inside)` and asked of the documents that govern the files changed inside it. A submodule the change adds where the repository held files (a split) is one change too: files that moved into it with the same content are neither removed nor changed, and only the files new or different there are asked about, under the same one name. Inside a pin bump only documents whose `governs` names a path at or inside the submodule are asked; one governing a wider tree (`governs: [vendor/**]`) is not, since a bump keeps where the submodule sits. A `change` note is asked only in the range that adds or edits it: once committed, its change has landed. With `[check] d1 = \"specific\"` only the most specific of the documents governing a file are asked, so an overview governing a whole tree (`governs: [services]`) is not asked about a file a narrower document governs. Update the document (or its `verified_at`), link a change note already in the range (`spec set <note> rel.depends_on+=<id> --write`, the fix D1 prints when there is one), or write a change note that links it: `spec new change --title … --depends-on <id> --write` (a `[[<id>]]` link in its body works too); where the change type is disabled, the fix pins the document instead: `spec set <id> verified_at=HEAD --write`. A package manifest or lockfile (package.json, bun.lock, Cargo.toml, Cargo.lock) whose change only moves version values, with the integrity hashes that follow them, asks only the documents that name one of the moved packages in their text (`@scope/tool`, as a whole name); a version bump does not ask a document that governs the manifest as a whole. See it with `spec check --staged --rule D1`.",
            on_commit: true,
        }
    }

    fn run(&self, ctx: &Ctx, out: &mut Vec<Finding>) {
        // Named paths are not a change: nothing says what they were before.
        if ctx.scope.is_all() || matches!(ctx.scope.range, crate::check::scope::Range::Paths(_)) {
            return;
        }
        for g in &ctx.scope.submodules_missing {
            out.push(Finding::warn(
                "D1",
                g,
                1,
                format!(
                    "{g}: submodule objects missing; the whole directory counts as changed; next: git submodule update"
                ),
            ));
        }
        let c = ctx.corpus;
        let own: BTreeSet<&String> =
            c.docs.iter().flat_map(|d| std::iter::once(&d.path).chain(&d.chapters).chain(&d.bundle_files)).collect();
        let mut drift: BTreeMap<usize, Vec<&str>> = BTreeMap::new();
        let gone: Vec<String> = ctx.scope.gone.iter().map(|p| format!("{p} (removed)")).collect();
        // A moved gitlink is one change: its files are named by one label, not one by one.
        let labels: Vec<(&vcs::GitlinkMove, String)> = ctx.scope.gitlinks.iter().map(|m| (m, label(m))).collect();
        let shown = |p: &str| labels.iter().find(|(m, _)| crate::paths::is_below(p, &m.path)).map(|(_, l)| l.as_str());
        let changed = ctx.scope.paths.iter().map(|p| (p.as_str(), shown(p).unwrap_or(p.as_str())));
        let removed = ctx.scope.gone.iter().zip(&gone).map(|(p, shown)| (p.as_str(), shown.as_str()));
        let specific = c.cfg.raw.check.d1_specific();
        // Compiled once: a gitlink conversion stages thousands of removed files.
        let index = GoverningIndex::filtered(c, |i| {
            let d = &c.docs[i];
            let explicit = d.rel().get("governs").is_some_and(|v| !v.is_empty());
            // A change note records one change: once committed, later code changes are another
            // change's, so a note outside this range is not asked about them.
            let landed_note = d.id.ty == "change" && !ctx.scope.touched.contains(&i);
            explicit
                && !landed_note
                && d.tier == Tier::Committed
                && d.status().as_deref() == Some("active")
                && !skipped(d, "D1")
        });
        let bumped = |p: &str| {
            ctx.scope.gitlinks.iter().find(|m| m.old.is_some() && crate::paths::is_below(p, &m.path)).map(|m| &m.path)
        };
        let mut reach: BTreeMap<(usize, &str), bool> = BTreeMap::new();
        let quiet = super::bumps::Quiet::new(ctx);
        for (p, shown) in changed.chain(removed).filter(|(p, _)| !own.contains(&p.to_string())) {
            let mut asked = index.governing(c, p);
            asked.retain(|g| !quiet.skips(p, g.idx));
            // Inside a pin bump only claims that reach into the submodule are asked: a document
            // governing a wider tree is about where the submodule sits, which a bump keeps.
            if let Some(link) = bumped(p) {
                asked.retain(|g| *reach.entry((g.idx, link.as_str())).or_insert_with(|| reaches_into(c, g.idx, link)));
            }
            // `governing` lists the narrowest claims first.
            let top = asked.first().map(|g| g.specificity).unwrap_or(0);
            for g in asked.into_iter().filter(|g| !specific || g.specificity == top) {
                let named = drift.entry(g.idx).or_default();
                // Paths come sorted, so a gitlink's files reach a document one after another.
                if named.last() != Some(&shown) {
                    named.push(shown);
                }
            }
        }
        let notes: Vec<usize> = ctx.scope.touched.iter().copied().filter(|&i| c.docs[i].id.ty == "change").collect();
        for (idx, paths) in drift {
            let moved = ctx.scope.touched.contains(&idx)
                || ctx.graph.edges_to(idx).any(|e| notes.contains(&e.from))
                || notes.iter().any(|&n| governs_inside(c, n, idx));
            if moved {
                continue;
            }
            let d = &c.docs[idx];
            let mut named = paths.iter().take(SHOWN).copied().collect::<Vec<_>>().join(", ");
            if paths.len() > SHOWN {
                named.push_str(&format!(" (+{} more)", paths.len() - SHOWN));
            }
            // A note already in the change is linked rather than joined by a second one.
            let note = notes.iter().find(|&&n| c.docs[n].id.owner == d.id.owner).or(notes.first());
            let fix = match note {
                Some(&n) => format!(
                    "spec set {} rel.depends_on+={} --write",
                    crate::suggest::shell::word(&c.docs[n].id),
                    crate::suggest::shell::word(&d.id)
                ),
                None if c.cfg.registry.get("change").is_some() => format!(
                    "spec new change --title {} --depends-on {} --write",
                    crate::suggest::shell::quote(&format!("Code change under {}", d.id.owner)),
                    crate::suggest::shell::word(&d.id)
                ),
                // Without change notes the document itself moves: a pin is the smallest change.
                None => format!("spec set {} verified_at=HEAD --write", crate::suggest::shell::word(&d.id)),
            };
            out.push(
                Finding::warn(
                    "D1",
                    &d.path,
                    1,
                    format!("{named} changed under {}'s governs, but the document and its change notes did not", d.id),
                )
                .fix_cmd(fix),
            );
        }
    }
}

/// D1: whether document `doc` names a path at or inside gitlink `link` in its `governs`.
fn reaches_into(c: &crate::corpus::Corpus, doc: usize, link: &str) -> bool {
    let claims = c.docs[doc].rel().remove("governs").unwrap_or_default();
    claims.iter().any(|g| crate::paths::is_under(&crate::workspace::placement::literal_base(g), link))
}

/// D1: whether change note `note` governs a path inside what document `doc` governs, so the note
/// speaks for changes there (a test file beside the source the note names). A note governing a
/// wider tree does not count: it says nothing about this document.
fn governs_inside(c: &crate::corpus::Corpus, note: usize, doc: usize) -> bool {
    let theirs = c.docs[note].rel().remove("governs").unwrap_or_default();
    let mine = c.docs[doc].rel().remove("governs").unwrap_or_default();
    theirs.iter().map(|g| crate::workspace::placement::literal_base(g)).any(|p| {
        mine.iter().any(|s| {
            let base = crate::workspace::placement::literal_base(s);
            crate::workspace::owners::matcher(s).is_some_and(|m| m.is_match(&p))
                || (!base.is_empty() && crate::paths::is_under(&p, &base))
        })
    })
}