Skip to main content

borg/
why.rs

1//! `borg why`: the reasoning behind code, on demand (PLAN.md "Recall mechanics").
2//!
3//! `borg why <path>[:<line>[-<line>]]` blames the span (the whole file without a range) and
4//! `borg why <commit>` takes that commit. For each distinct commit, newest first: short sha, date,
5//! author and subject, then the drone's reasoning (task, last proposal, episode ids) and the lessons
6//! learned from those episodes (question and consequence, one line each).
7//!
8//! **Where the reasoning comes from**: the commit's borg note (`refs/notes/borg`, notes.rs); if the
9//! repo lacks it, the excerpts the collective's store holds (`reasoning.jsonl`: this machine's own
10//! and those the mesh replicated to it from peers). No network and no model call: `borg notes fetch`
11//! is the step that asks peers. Lessons come from the routed collective's `lessons.jsonl` only.
12//!
13//! **Links** (notes/regress.rs): a decision shows the later commits that fixed its lines (`→ fixed
14//! in Y (task: …)`), a fix the decisions it fixed (`← decided in X (task: …) → fixed here`), from
15//! the notes and the store's link records, at most 3 each unless `--full` (why/linked.rs).
16//!
17//! **Privacy**: decided before git is asked anything. A cwd (or target) that routes off, or an
18//! invalid config, prints nothing and exits 0. A note or excerpt that names another collective is
19//! never shown (a private collective's note only where the cwd routes to that same collective);
20//! an excerpt from before notes named their collective is treated as this repo's. Everything shown
21//! is redacted again at display time (`notes::redact`), whatever the note says. Paths are routed
22//! resolved (absolute, symlinks followed, as git sees them), and the repo git reads must route to
23//! the cwd's collective too: a symlink or submodule into an off or other repo prints nothing.
24//!
25//! **Cost**: the blame headers give each commit's time, so the cap is applied before any
26//! per-commit work; then one `git log`, one `git notes list` plus one `cat-file --batch`, and one
27//! read of `reasoning.jsonl` (only if a shown commit lacks a note).
28//!
29//! **Compact by default**: at most `MAX_COMMITS` commits, the task's first line and the proposal's
30//! last `PROPOSAL_CAP` characters, `MAX_LESSONS` lessons per commit. `--full` shows everything,
31//! `--json` prints the same (capped unless `--full`) for scripts. It costs tokens only when a drone
32//! runs it: nothing injects it.
33
34use std::collections::{HashMap, HashSet};
35use std::fmt::Write as _;
36use std::path::{Path, PathBuf};
37
38use serde::Serialize;
39
40use crate::collective::{self, Config, Route};
41use crate::learn;
42use crate::notes::reasoning::{git_in, notes_of, stored_of};
43use crate::notes::{Excerpt, redact, regress};
44
45mod linked;
46
47pub use linked::LinkLine;
48
49/// Commits the compact form shows (module doc; `--full` lifts every bound here). Display: what one
50/// screen of `borg why`, or a drone reading it, takes in.
51const MAX_COMMITS: usize = 8;
52/// Lessons per commit in the compact form. Display, as `MAX_COMMITS`.
53const MAX_LESSONS: usize = 3;
54/// Characters of a task's first line, here and for a link's task (why/linked.rs). Display.
55const TASK_CAP: usize = 240;
56/// Characters kept of the end of a proposal (its conclusion). Display.
57const PROPOSAL_CAP: usize = 400;
58/// Characters of a lesson's question, and of its consequence. Display.
59const LESSON_CAP: usize = 240;
60
61const USAGE: &str = "usage: borg why <path>[:<line>[-<line>]] | <commit> [--full] [--json] [--dir DIR]";
62
63/// What to explain.
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub enum Target {
66    /// A file, optionally a 1-based inclusive line range.
67    Span {
68        path: String,
69        lines: Option<(u32, u32)>,
70    },
71    Commit(String),
72}
73
74/// `path`, `path:12` or `path:12-20` (a commit is told apart later, by what exists).
75pub fn parse_span(arg: &str) -> Result<(String, Option<(u32, u32)>), String> {
76    let Some((path, range)) = arg.rsplit_once(':') else { return Ok((arg.to_string(), None)) };
77    if path.is_empty() || range.is_empty() || !range.chars().all(|c| c.is_ascii_digit() || c == '-') {
78        // Not a line range (`a:b` as a file name, `HEAD:path`): the whole argument.
79        return Ok((arg.to_string(), None));
80    }
81    let num = |s: &str| s.parse::<u32>().map_err(|_| format!("bad line range {range:?} in {arg:?}"));
82    let (a, b) = match range.split_once('-') {
83        Some((a, b)) => (num(a)?, num(b)?),
84        None => (num(range)?, num(range)?),
85    };
86    if a == 0 || b < a {
87        return Err(format!("bad line range {range:?} in {arg:?}: lines start at 1 and the end is not before the start"));
88    }
89    Ok((path.to_string(), Some((a, b))))
90}
91
92fn git(dir: &Path, args: &[&str]) -> Result<String, String> {
93    git_in(dir, args, None).map(|o| String::from_utf8_lossy(&o).into_owned())
94}
95
96/// `p` absolute with symlinks resolved (routing compares path text, git follows the real path);
97/// lexically absolute when it does not exist.
98fn resolve(p: &Path) -> PathBuf {
99    std::fs::canonicalize(p).or_else(|_| std::path::absolute(p)).unwrap_or_else(|_| p.to_path_buf())
100}
101
102fn is_sha(s: &str) -> bool {
103    (s.len() == 40 || s.len() == 64) && s.bytes().all(|b| b.is_ascii_hexdigit())
104}
105
106/// The distinct commits of `git blame --porcelain` output with their author time, in order of
107/// first appearance, and the number of lines not committed yet.
108pub fn blamed(porcelain: &str) -> (Vec<(String, i64)>, usize) {
109    let (mut out, mut uncommitted) = (Vec::<(String, i64)>::new(), 0);
110    let mut seen = HashSet::<String>::new();
111    // The commit whose header lines follow (only its first appearance carries them).
112    let mut current: Option<usize> = None;
113    for l in porcelain.lines().filter(|l| !l.starts_with('\t')) {
114        if let Some(t) = l.strip_prefix("author-time ") {
115            if let Some(i) = current {
116                out[i].1 = t.trim().parse().unwrap_or(0);
117            }
118            continue;
119        }
120        let mut f = l.split(' ');
121        let (Some(sha), Some(_), Some(_)) = (f.next(), f.next(), f.next()) else { continue };
122        if !is_sha(sha) {
123            continue;
124        }
125        if sha.bytes().all(|b| b == b'0') {
126            current = None;
127            // One header per line of an uncommitted group would overcount; a group header has 4 fields.
128            uncommitted += f.next().and_then(|n| n.parse::<usize>().ok()).unwrap_or(0);
129        } else if !seen.insert(sha.to_string()) {
130            current = None;
131        } else {
132            out.push((sha.to_string(), 0));
133            current = Some(out.len() - 1);
134        }
135    }
136    (out, uncommitted)
137}
138
139#[derive(Debug, Clone, Serialize, PartialEq)]
140pub struct LessonLine {
141    pub id: String,
142    pub question: String,
143    pub consequence: String,
144}
145
146#[derive(Debug, Clone, Serialize, PartialEq)]
147pub struct Reasoning {
148    /// "note" (the repo's borg note) or "store" (the collective's reasoning.jsonl, replicated).
149    pub source: String,
150    pub session: String,
151    pub task: String,
152    pub proposal: String,
153    pub episodes: Vec<String>,
154}
155
156#[derive(Debug, Clone, Serialize, PartialEq)]
157pub struct Why {
158    pub sha: String,
159    pub short: String,
160    pub author: String,
161    pub date: String,
162    pub subject: String,
163    pub reasoning: Option<Reasoning>,
164    pub lessons: Vec<LessonLine>,
165    /// Later commits that fixed (or revisited) this one's lines (why/linked.rs).
166    #[serde(skip_serializing_if = "Vec::is_empty")]
167    pub fixed_in: Vec<LinkLine>,
168    /// The earlier decisions this commit fixed.
169    #[serde(skip_serializing_if = "Vec::is_empty")]
170    pub fixes: Vec<LinkLine>,
171}
172
173#[derive(Debug, Clone, Serialize, PartialEq)]
174pub struct Report {
175    pub target: String,
176    pub collective: String,
177    pub commits: Vec<Why>,
178    /// Commits left out by the compact cap.
179    pub more: usize,
180    /// Lines of the span not committed yet.
181    pub uncommitted: usize,
182}
183
184fn first_chars(s: &str, n: usize) -> String {
185    if s.chars().count() <= n { s.to_string() } else { format!("{}…", s.chars().take(n).collect::<String>()) }
186}
187
188fn last_chars(s: &str, n: usize) -> String {
189    let len = s.chars().count();
190    if len <= n { s.to_string() } else { format!("…{}", s.chars().skip(len - n).collect::<String>()) }
191}
192
193fn one_line(s: &str) -> String {
194    s.split_whitespace().collect::<Vec<_>>().join(" ")
195}
196
197/// Defence in depth: whatever wrote the note, it is redacted again before anyone sees it.
198fn shown(e: Excerpt, source: &str) -> Reasoning {
199    Reasoning {
200        source: source.into(),
201        session: e.session,
202        task: redact(&e.task),
203        proposal: redact(&e.proposal),
204        episodes: e.episodes.iter().map(|x| redact(x)).collect(),
205    }
206}
207
208/// The lessons in `lessons` learned from any of `episodes`.
209fn lessons_of(lessons: &[learn::Lesson], episodes: &[String]) -> Vec<LessonLine> {
210    lessons
211        .iter()
212        .filter(|l| l.episodes.iter().any(|e| episodes.contains(e)))
213        .map(|l| LessonLine { id: l.id.clone(), question: redact(&l.question), consequence: redact(&l.consequence) })
214        .collect()
215}
216
217/// Where `borg why` reads: the cwd's collective and store, the repo and the target in it.
218struct Place {
219    name: String,
220    home: PathBuf,
221    repo: PathBuf,
222    /// Where git runs for a span: the file's directory.
223    dir: PathBuf,
224    file: PathBuf,
225    path: String,
226    target: Target,
227}
228
229/// The target as a file span in the cwd's collective, or a commit; `None` when the file routes off.
230fn target_of(cfg: Option<&Config>, base: &Path, cwd: &Path, route: &Route, target: &str) -> Result<Option<(Target, PathBuf, String)>, String> {
231    let (path, lines) = parse_span(target)?;
232    let file = resolve(&cwd.join(&path));
233    if file.is_file() {
234        // The file itself may lie under an off path or in another collective than the cwd.
235        return match collective::route(cfg, &file.to_string_lossy(), base) {
236            Route::Off => Ok(None),
237            r if r != *route => Err(format!("{path} belongs to another collective than {}; run borg why from its repo", cwd.display())),
238            _ => Ok(Some((Target::Span { path: path.clone(), lines }, file, path))),
239        };
240    }
241    if lines.is_some() {
242        return Err(format!("{path}: no such file"));
243    }
244    Ok(Some((Target::Commit(target.to_string()), file, path)))
245}
246
247/// The place `target` is read from `cwd`, or `None` when the cwd, the target or the repo they lie
248/// in routes off or elsewhere (module doc).
249fn place(cfg: Option<&Config>, base: &Path, cwd: &str, target: &str) -> Result<Option<Place>, String> {
250    if cwd.trim().is_empty() {
251        return Ok(None);
252    }
253    let cwd = resolve(Path::new(cwd));
254    let route = collective::route(cfg, &cwd.to_string_lossy(), base);
255    let Route::Collective { name, home, .. } = &route else { return Ok(None) };
256    let Some((target, file, path)) = target_of(cfg, base, &cwd, &route, target)? else { return Ok(None) };
257    let dir = match &target {
258        Target::Span { .. } => file.parent().map_or_else(|| cwd.clone(), Path::to_path_buf),
259        Target::Commit(_) => cwd.clone(),
260    };
261    let repo = git(&dir, &["rev-parse", "--show-toplevel"]).map(|s| PathBuf::from(s.trim())).map_err(|_| format!("{} is not in a git repo", dir.display()))?;
262    // The repo itself must route where the cwd does: a symlinked directory or a submodule may put
263    // the history git reads in an off or another collective's repo.
264    let repo = resolve(&repo);
265    if collective::route(cfg, &repo.to_string_lossy(), base) != route {
266        return Ok(None);
267    }
268    Ok(Some(Place { name: name.clone(), home: home.clone(), repo, dir, file, path, target }))
269}
270
271/// The commits behind the target with their author time, and the span's lines not committed yet.
272fn commits_of(p: &Place) -> Result<(Vec<(String, i64)>, usize), String> {
273    match &p.target {
274        Target::Span { lines, .. } => {
275            let name = p.file.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
276            let mut args = vec!["blame".to_string(), "--porcelain".into()];
277            if let Some((a, b)) = lines {
278                args.push(format!("-L{a},{b}"));
279            }
280            args.extend(["--".into(), name]);
281            let out = git(&p.dir, &args.iter().map(String::as_str).collect::<Vec<_>>()).map_err(|e| format!("git blame {}: {e}", p.path))?;
282            Ok(blamed(&out))
283        }
284        Target::Commit(c) => {
285            let sha = git(&p.repo, &["rev-parse", "--verify", "--quiet", &format!("{c}^{{commit}}")])
286                .map_err(|_| format!("{c} is neither a file nor a commit in {}", p.repo.display()))?;
287            Ok((vec![(sha.trim().to_string(), 0)], 0))
288        }
289    }
290}
291
292/// One `git log` for the shown commits' headers: short sha, author, date and subject by commit.
293fn headers(repo: &Path, shas: &[String]) -> Result<HashMap<String, [String; 4]>, String> {
294    let input: String = shas.iter().map(|s| format!("{s}\n")).collect();
295    let log = git_in(repo, &["log", "--no-walk=unsorted", "--stdin", "--date=short", "--format=%H%x1f%h%x1f%an%x1f%ad%x1f%s"], Some(input))
296        .map_err(|e| format!("git log: {e}"))?;
297    let mut info = HashMap::new();
298    for l in String::from_utf8_lossy(&log).lines() {
299        let f: Vec<&str> = l.splitn(5, '\x1f').collect();
300        if let [sha, short, author, date, subject] = f[..] {
301            info.insert(sha.to_string(), [short.into(), author.into(), date.into(), subject.into()]);
302        }
303    }
304    Ok(info)
305}
306
307/// Each shown commit with its reasoning (note, else store), the lessons learned from its episodes
308/// and its decision → fix links.
309fn describe(p: &Place, shas: &[String]) -> Result<Vec<Why>, String> {
310    let mut info = headers(&p.repo, shas)?;
311    let mut from_notes = notes_of(&p.repo, &p.name, shas);
312    let missing: HashSet<&str> = shas.iter().map(String::as_str).filter(|s| !from_notes.contains_key(*s)).collect();
313    let mut from_store = stored_of(&p.home, &p.name, &missing);
314    let lessons = learn::lessons(&p.home.join("lessons.jsonl"));
315    let stored_links = regress::links_of(&p.home, &p.name, &shas.iter().map(String::as_str).collect::<HashSet<_>>());
316    let mut commits = Vec::new();
317    for sha in shas {
318        let Some([short, author, date, subject]) = info.remove(sha) else { return Err(format!("git log {sha}: unexpected output")) };
319        let (excerpt, source) = match from_notes.remove(sha) {
320            Some(e) => (Some(e), "note"),
321            None => (from_store.remove(sha), "store"),
322        };
323        let (fixed_in, fixes) = linked::of(sha, excerpt.as_ref(), &stored_links);
324        let reasoning = excerpt.map(|e| shown(e, source));
325        let lessons = reasoning.as_ref().map(|r| lessons_of(&lessons, &r.episodes)).unwrap_or_default();
326        commits.push(Why { sha: sha.clone(), short, author, date, subject: redact(&subject), reasoning, lessons, fixed_in, fixes });
327    }
328    Ok(commits)
329}
330
331/// The compact form of a commit: capped lessons and links, the task's first line, the proposal's end.
332fn compact(w: &mut Why) {
333    w.lessons.truncate(MAX_LESSONS);
334    for l in &mut w.lessons {
335        l.question = first_chars(&one_line(&l.question), LESSON_CAP);
336        l.consequence = first_chars(&one_line(&l.consequence), LESSON_CAP);
337    }
338    if let Some(r) = &mut w.reasoning {
339        r.task = first_chars(r.task.lines().find(|l| !l.trim().is_empty()).unwrap_or_default().trim(), TASK_CAP);
340        r.proposal = last_chars(&one_line(&r.proposal), PROPOSAL_CAP);
341    }
342    linked::compact(&mut w.fixed_in);
343    linked::compact(&mut w.fixes);
344}
345
346/// The explanation of `target` (an argument as typed) from `cwd`, or `None` when `cwd`, the
347/// target or the repo they lie in routes off or elsewhere. `base` is the root store (`BORG_HOME`).
348/// Paths are routed resolved (absolute, symlinks followed), as git sees them.
349///
350/// # Errors
351/// A bad line range, a missing file or commit, a file of another collective, or git failing.
352pub fn explain(cfg: Option<&Config>, base: &Path, cwd: &str, target: &str, full: bool) -> Result<Option<Report>, String> {
353    let Some(p) = place(cfg, base, cwd, target)? else { return Ok(None) };
354    let (mut shas, uncommitted) = commits_of(&p)?;
355    // Newest first by author time (from the blame headers), and the cap before any per-commit work.
356    shas.sort_by_key(|(_, ts)| std::cmp::Reverse(*ts));
357    let more = if full { 0 } else { shas.len().saturating_sub(MAX_COMMITS) };
358    shas.truncate(if full { usize::MAX } else { MAX_COMMITS });
359    let shas: Vec<String> = shas.into_iter().map(|(s, _)| s).collect();
360    let mut commits = if shas.is_empty() { Vec::new() } else { describe(&p, &shas)? };
361    if !full {
362        commits.iter_mut().for_each(compact);
363    }
364    Ok(Some(Report { target: target.into(), collective: p.name, commits, more, uncommitted }))
365}
366
367/// The reasoning lines of a commit.
368fn render_reasoning(s: &mut String, r: Option<&Reasoning>) {
369    let Some(x) = r else {
370        s.push_str("  (no borg reasoning recorded)\n");
371        return;
372    };
373    let from = if x.source == "note" { String::new() } else { " (from the collective's store; `borg notes fetch` writes it as a note)".into() };
374    let _ = writeln!(s, "  task: {}{from}", x.task.trim());
375    if !x.proposal.trim().is_empty() {
376        let _ = writeln!(s, "  proposal: {}", x.proposal.trim());
377    }
378    if !x.episodes.is_empty() {
379        let _ = writeln!(s, "  episodes: {}", x.episodes.join(", "));
380    }
381}
382
383/// The report as text.
384#[must_use]
385pub fn render(r: &Report) -> String {
386    let mut s = String::new();
387    for w in &r.commits {
388        let _ = writeln!(s, "{} {} {} {}", w.short, w.date, w.author, w.subject);
389        render_reasoning(&mut s, w.reasoning.as_ref());
390        s.push_str(&linked::render(&w.fixed_in, &w.fixes));
391        for l in &w.lessons {
392            let _ = writeln!(s, "  lesson {}: {} -> {}", l.id, l.question.trim(), l.consequence.trim());
393        }
394    }
395    if r.more > 0 {
396        let _ = writeln!(s, "... {} older commit(s) not shown (--full)", r.more);
397    }
398    if r.uncommitted > 0 {
399        let _ = writeln!(s, "{} line(s) not committed yet", r.uncommitted);
400    }
401    if r.commits.is_empty() {
402        let _ = writeln!(s, "no commits behind {}", r.target);
403    } else if r.commits.iter().all(|w| w.reasoning.is_none()) {
404        let _ = writeln!(s, "no borg reasoning found for {} (in collective {}; `borg notes fetch` asks the mesh)", r.target, r.collective);
405    }
406    s
407}
408
409/// `borg why <target> [--full] [--json] [--dir DIR]`.
410pub fn main(args: &[&str]) -> i32 {
411    let (mut full, mut json, mut dir, mut target) = (false, false, None, None);
412    let mut it = args.iter();
413    while let Some(a) = it.next() {
414        match *a {
415            "--full" => full = true,
416            "--json" => json = true,
417            "--dir" => match it.next() {
418                Some(d) => dir = Some(d.to_string()),
419                None => return usage(),
420            },
421            t if target.is_none() && !t.starts_with("--") => target = Some(t.to_string()),
422            _ => return usage(),
423        }
424    }
425    let Some(target) = target else { return usage() };
426    let cwd = dir.unwrap_or_else(|| std::env::current_dir().unwrap_or_default().display().to_string());
427    // An invalid config routes everything off: nothing is shown.
428    let Ok(cfg) = collective::load() else { return 0 };
429    match explain(cfg.as_ref(), &learn::home(), &cwd, &target, full) {
430        Ok(None) => 0,
431        Ok(Some(r)) => {
432            if json {
433                println!("{}", serde_json::to_string(&r).unwrap());
434            } else {
435                print!("{}", render(&r));
436            }
437            0
438        }
439        Err(e) => {
440            eprintln!("borg why: {e}");
441            1
442        }
443    }
444}
445
446fn usage() -> i32 {
447    eprintln!("{USAGE}");
448    2
449}
450
451#[cfg(test)]
452mod tests {
453    use super::*;
454    use crate::notes;
455    use std::process::Command;
456
457    fn sh(dir: &Path, args: &[&str]) -> String {
458        let o = Command::new("git").arg("-C").arg(dir).args(args).env("GIT_CONFIG_GLOBAL", "/dev/null").env("GIT_CONFIG_NOSYSTEM", "1").output().unwrap();
459        assert!(o.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&o.stderr));
460        String::from_utf8_lossy(&o.stdout).trim().to_string()
461    }
462
463    /// git as `sh`, as `who`, at a fixed time.
464    fn commit_as(dir: &Path, who: &str, secs: i64, msg: &str) -> String {
465        let date = format!("@{secs} +0000");
466        let o = Command::new("git")
467            .arg("-C")
468            .arg(dir)
469            .args(["-c", &format!("user.name={who}"), "-c", &format!("user.email={who}@example.com"), "commit", "-q", "-am", msg])
470            .env("GIT_CONFIG_GLOBAL", "/dev/null")
471            .env("GIT_CONFIG_NOSYSTEM", "1")
472            .env("GIT_COMMITTER_DATE", &date)
473            .env("GIT_AUTHOR_DATE", &date)
474            .output()
475            .unwrap();
476        assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
477        sh(dir, &["rev-parse", "HEAD"])
478    }
479
480    struct Fx {
481        base: PathBuf,
482        work: PathBuf,
483        repo: PathBuf,
484        store: PathBuf,
485        cfg: Config,
486        one: String,
487        two: String,
488    }
489
490    /// `<base>/work/app` (collective "corp", team) with `src/lib.rs`: lines 1-3 by Ann (commit
491    /// `one`), line 2 rewritten by Bob (commit `two`). Also `<base>/private` (local) and
492    /// `<base>/work/secret` (off).
493    fn fixture(tag: &str) -> Fx {
494        let base = std::env::temp_dir().join(format!("borg-why-{tag}-{}", std::process::id()));
495        let _ = std::fs::remove_dir_all(&base);
496        std::fs::create_dir_all(&base).unwrap();
497        // Resolved, as `explain` routes it (the temp dir may be a symlink).
498        let base = std::fs::canonicalize(&base).unwrap();
499        let (work, store_base) = (base.join("work"), base.join("store"));
500        let repo = work.join("app");
501        std::fs::create_dir_all(repo.join("src")).unwrap();
502        sh(&repo, &["init", "-q"]);
503        std::fs::write(repo.join("src/lib.rs"), "fn a() {}\nfn b() {}\nfn c() {}\n").unwrap();
504        sh(&repo, &["add", "."]);
505        let one = commit_as(&repo, "Ann", 1_700_000_000, "add a b c");
506        std::fs::write(repo.join("src/lib.rs"), "fn a() {}\nfn b() { lock(); }\nfn c() {}\n").unwrap();
507        let two = commit_as(&repo, "Bob", 1_700_100_000, "take the lock in b");
508        let cfg: Config = serde_json::from_str(&format!(
509            r#"{{"default": "off", "off": ["{0}/work/secret"], "collectives": [
510                {{"name": "corp", "paths": ["{0}/work"], "share": "team"}},
511                {{"name": "private", "paths": ["{0}/private"], "share": "local"}} ]}}"#,
512            base.display()
513        ))
514        .unwrap();
515        let store = collective::store(&store_base, "corp");
516        Fx { base, work, repo, store, cfg, one, two }
517    }
518
519    impl Fx {
520        fn why(&self, cwd: &Path, target: &str, full: bool) -> Result<Option<Report>, String> {
521            explain(Some(&self.cfg), &self.base.join("store"), &cwd.to_string_lossy(), target, full)
522        }
523        fn note(&self, repo: &Path, commit: &str, collective: &str, episodes: &[&str]) {
524            let e = Excerpt {
525                kind: "reasoning".into(),
526                commit: commit.into(),
527                repo: "app".into(),
528                session: "s-1".into(),
529                task: "make b safe under concurrency\nsecond line".into(),
530                proposal: "Take the lock first; the token is ghp_abcdefghijklmnopqrstuvwxyz0123456789.".into(),
531                episodes: episodes.iter().map(|s| s.to_string()).collect(),
532                ms: 1,
533                collective: collective.into(),
534                ..Excerpt::default()
535            };
536            notes::write_note(repo, &e).unwrap();
537        }
538    }
539
540    impl Drop for Fx {
541        fn drop(&mut self) {
542            let _ = std::fs::remove_dir_all(&self.base);
543        }
544    }
545
546    #[test]
547    fn parses_line_ranges() {
548        assert_eq!(parse_span("src/a.rs").unwrap(), ("src/a.rs".into(), None));
549        assert_eq!(parse_span("src/a.rs:12").unwrap(), ("src/a.rs".into(), Some((12, 12))));
550        assert_eq!(parse_span("src/a.rs:12-20").unwrap(), ("src/a.rs".into(), Some((12, 20))));
551        assert_eq!(parse_span("HEAD~1").unwrap(), ("HEAD~1".into(), None));
552        assert_eq!(parse_span("HEAD:src/a.rs").unwrap(), ("HEAD:src/a.rs".into(), None), "not a range");
553        assert!(parse_span("a.rs:0").is_err());
554        assert!(parse_span("a.rs:20-10").is_err());
555        assert!(parse_span("a.rs:1-").is_err());
556        assert!(parse_span("a.rs:1-2-3").is_err());
557        assert!(parse_span("a.rs:99999999999").is_err());
558    }
559
560    #[test]
561    fn blame_of_a_span_spanning_two_commits() {
562        let fx = fixture("span");
563        fx.note(&fx.repo, &fx.two, "corp", &[]);
564        let r = fx.why(&fx.repo, "src/lib.rs:1-2", false).unwrap().unwrap();
565        let shas: Vec<&str> = r.commits.iter().map(|w| w.sha.as_str()).collect();
566        assert_eq!(shas, [fx.two.as_str(), fx.one.as_str()], "both commits, newest first");
567        assert_eq!((r.commits[0].author.as_str(), r.commits[0].subject.as_str()), ("Bob", "take the lock in b"));
568        let x = r.commits[0].reasoning.as_ref().unwrap();
569        assert_eq!((x.source.as_str(), x.task.as_str()), ("note", "make b safe under concurrency"), "compact: the task's first line");
570        assert!(!x.proposal.contains("ghp_abc") && x.proposal.contains("[REDACTED]"), "redacted again at display: {}", x.proposal);
571        assert!(r.commits[1].reasoning.is_none());
572        let text = render(&r);
573        assert!(text.contains("take the lock in b") && text.contains("add a b c") && text.contains("(no borg reasoning recorded)"), "{text}");
574        // One line only: one commit.
575        let r = fx.why(&fx.repo, "src/lib.rs:3", false).unwrap().unwrap();
576        assert_eq!(r.commits.len(), 1);
577        assert_eq!(r.commits[0].sha, fx.one);
578        // Uncommitted lines are counted, not blamed on a commit.
579        std::fs::write(fx.repo.join("src/lib.rs"), "fn a() {}\nfn b() { lock(); }\nfn c() {}\nfn d() {}\n").unwrap();
580        let r = fx.why(&fx.repo, "src/lib.rs:3-4", false).unwrap().unwrap();
581        assert_eq!((r.commits.len(), r.uncommitted), (1, 1));
582        assert!(fx.why(&fx.repo, "src/lib.rs:9-12", false).is_err(), "past the end of the file");
583    }
584
585    #[test]
586    fn commit_form_and_nothing_found() {
587        let fx = fixture("commit");
588        let r = fx.why(&fx.repo, &fx.one[..10], false).unwrap().unwrap();
589        assert_eq!(r.commits.len(), 1);
590        assert_eq!(r.commits[0].sha, fx.one);
591        assert!(render(&r).contains("no borg reasoning found for"), "{}", render(&r));
592        let r = fx.why(&fx.repo, "HEAD", true).unwrap().unwrap();
593        assert_eq!(r.commits[0].sha, fx.two);
594        let err = fx.why(&fx.repo, "no-such-thing", false).unwrap_err();
595        assert!(err.contains("neither a file nor a commit"), "{err}");
596        assert!(fx.why(&fx.repo, "missing.rs:3", false).unwrap_err().contains("no such file"));
597    }
598
599    #[test]
600    fn falls_back_to_replicated_lines_and_links_lessons_by_episode() {
601        let fx = fixture("replica");
602        std::fs::create_dir_all(&fx.store).unwrap();
603        let line = |commit: &str, collective: &str, task: &str| {
604            serde_json::json!({"kind": "reasoning", "commit": commit, "repo": "app", "session": "s-9", "task": task,
605                "proposal": "lock before the flag; password=hunter22", "episodes": ["s-9-100"], "ms": 5, "collective": collective})
606            .to_string()
607        };
608        let lesson = |id: &str, ep: &str, q: &str| {
609            serde_json::json!({"id": id, "origin_ms": 1, "repo": "app", "question": q, "situations": [], "consequence": "a race corrupts sessions",
610                "support": 1, "episodes": [ep], "history": [], "version": 1})
611            .to_string()
612        };
613        std::fs::write(
614            fx.store.join("reasoning.jsonl"),
615            [line(&fx.two, "corp", "replicated why"), line(&fx.one, "other", "another collective's")].join("\n") + "\n",
616        )
617        .unwrap();
618        std::fs::write(
619            fx.store.join("lessons.jsonl"),
620            [lesson("L1", "s-9-100", "Is the lock taken before the flag is read?"), lesson("L2", "s-0-1", "unrelated")].join("\n") + "\n",
621        )
622        .unwrap();
623        let r = fx.why(&fx.repo, "src/lib.rs", false).unwrap().unwrap();
624        let x = r.commits[0].reasoning.as_ref().unwrap();
625        assert_eq!((x.source.as_str(), x.task.as_str()), ("store", "replicated why"));
626        assert!(!x.proposal.contains("hunter22"));
627        assert_eq!(r.commits[0].lessons.iter().map(|l| l.id.as_str()).collect::<Vec<_>>(), ["L1"], "linked by episode id");
628        assert!(r.commits[1].reasoning.is_none(), "an excerpt tagged with another collective is not shown");
629        let text = render(&r);
630        assert!(text.contains("lesson L1: Is the lock taken before the flag is read? -> a race corrupts sessions"), "{text}");
631        assert!(text.contains("borg notes fetch"), "{text}");
632        // A local note wins over the store.
633        fx.note(&fx.repo, &fx.two, "corp", &["s-9-100"]);
634        let r = fx.why(&fx.repo, &fx.two, false).unwrap().unwrap();
635        assert_eq!(r.commits[0].reasoning.as_ref().unwrap().source, "note");
636        assert_eq!(r.commits[0].lessons.len(), 1);
637        let json = serde_json::to_value(&r).unwrap();
638        assert_eq!(json["commits"][0]["lessons"][0]["id"], "L1");
639        assert!(json["commits"][0].get("ts").is_none());
640    }
641
642    #[test]
643    fn private_notes_stay_in_their_collective_and_off_prints_nothing() {
644        let fx = fixture("private");
645        // The same repo history moved under the private path: its note is tagged "private".
646        let private = fx.base.join("private/app");
647        std::fs::create_dir_all(private.parent().unwrap()).unwrap();
648        sh(&fx.base, &["clone", "-q", &fx.repo.to_string_lossy(), &private.to_string_lossy()]);
649        fx.note(&private, &fx.two, "private", &[]);
650        let r = fx.why(&private, "src/lib.rs:2", false).unwrap().unwrap();
651        assert_eq!(r.collective, "private");
652        assert!(r.commits[0].reasoning.is_some(), "shown where the cwd routes to the same private collective");
653        // A private note that ends up in a team repo (copied refs) is not shown there.
654        fx.note(&fx.repo, &fx.two, "private", &[]);
655        let r = fx.why(&fx.repo, "src/lib.rs:2", false).unwrap().unwrap();
656        assert!(r.commits[0].reasoning.is_none(), "a private note never shows in another collective");
657        // Off: nothing at all, before git is asked (also for a target inside an off path).
658        let secret = fx.work.join("secret");
659        std::fs::create_dir_all(&secret).unwrap();
660        std::fs::write(secret.join("x.rs"), "x").unwrap();
661        assert_eq!(fx.why(&secret, "x.rs", false).unwrap(), None);
662        assert_eq!(fx.why(&fx.repo, "../secret/x.rs", false).unwrap(), None, "a target under an off path");
663        assert_eq!(fx.why(&fx.base.join("elsewhere"), "HEAD", false).unwrap(), None, "default off");
664        // A file in another collective than the cwd is refused.
665        assert!(fx.why(&fx.repo, "../../private/app/src/lib.rs", false).unwrap_err().contains("another collective"));
666    }
667
668    #[test]
669    fn routes_resolved_paths_and_the_repo_itself() {
670        let fx = fixture("resolve");
671        fx.note(&fx.repo, &fx.two, "corp", &[]);
672        // An off repo with a note, reached through symlinks from inside the team tree.
673        let secret = fx.work.join("secret");
674        std::fs::create_dir_all(&secret).unwrap();
675        sh(&fx.base, &["clone", "-q", &fx.repo.to_string_lossy(), &secret.to_string_lossy()]);
676        sh(&fx.repo, &["push", "-q", &secret.to_string_lossy(), "refs/notes/borg:refs/notes/borg"]);
677        std::os::unix::fs::symlink(&secret, fx.repo.join("link")).unwrap();
678        std::os::unix::fs::symlink(&secret, fx.work.join("alias")).unwrap();
679        assert_eq!(fx.why(&fx.repo, "link/src/lib.rs", false).unwrap(), None, "a symlinked file in an off repo");
680        assert_eq!(fx.why(&fx.repo.join("link"), "src/lib.rs:2", false).unwrap(), None, "a symlinked cwd");
681        assert_eq!(fx.why(&fx.work.join("alias"), "HEAD", false).unwrap(), None, "a symlinked cwd, commit form");
682        assert_eq!(fx.why(&fx.repo.join("src/../link"), "HEAD", false).unwrap(), None);
683        // The cwd routes to a collective, but the repo git reads (its toplevel) routes elsewhere.
684        let mut cfg = fx.cfg.clone();
685        cfg.collectives[1].paths.push(fx.repo.join("src").to_string_lossy().into_owned());
686        let r = explain(Some(&cfg), &fx.base.join("store"), &fx.repo.join("src").to_string_lossy(), "lib.rs:2", false).unwrap();
687        assert_eq!(r, None, "a private subdirectory of a team repo");
688        let r = explain(Some(&cfg), &fx.base.join("store"), &fx.repo.join("src").to_string_lossy(), "HEAD", false).unwrap();
689        assert_eq!(r, None);
690        // The real paths still work.
691        assert!(fx.why(&fx.repo, "src/lib.rs:2", false).unwrap().unwrap().commits[0].reasoning.is_some());
692    }
693
694    #[test]
695    fn blame_headers_give_the_order_and_the_cap_comes_first() {
696        let porcelain = "\
697aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 1 1
698author A
699author-time 100
700summary old
701\tx
702bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 2 2 1
703author B
704author-time 300
705summary new
706\ty
707aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 3 3 1
708\tz
7090000000000000000000000000000000000000000 4 4 2
710author Not Committed Yet
711author-time 999
712\tw
713";
714        let (c, u) = blamed(porcelain);
715        assert_eq!(c, [("a".repeat(40), 100), ("b".repeat(40), 300)]);
716        assert_eq!(u, 2);
717
718        // Twelve commits, each owning one line: compact keeps the 8 newest, the store is read once
719        // for those, and an excerpt of a cut commit costs nothing.
720        let fx = fixture("cap");
721        let file = fx.repo.join("src/lib.rs");
722        let mut text = std::fs::read_to_string(&file).unwrap();
723        let mut made = Vec::new();
724        for i in 0..12 {
725            text.push_str(&format!("fn f{i}() {{}}\n"));
726            std::fs::write(&file, &text).unwrap();
727            made.push(commit_as(&fx.repo, "Cy", 1_800_000_000 + i, &format!("f{i}")));
728        }
729        std::fs::create_dir_all(&fx.store).unwrap();
730        let line = |c: &str, ms: i64, task: &str| {
731            serde_json::json!({"kind": "reasoning", "commit": c, "repo": "app", "session": "s", "task": task, "proposal": "", "episodes": [], "ms": ms})
732                .to_string()
733        };
734        let lines = [line(&made[11], 1, "older"), line(&made[11], 2, "newest"), line(&made[0], 1, "cut")];
735        std::fs::write(fx.store.join("reasoning.jsonl"), lines.join("\n") + "\n").unwrap();
736        fx.note(&fx.repo, &made[10], "corp", &[]);
737        let r = fx.why(&fx.repo, "src/lib.rs", false).unwrap().unwrap();
738        let subjects: Vec<&str> = r.commits.iter().map(|w| w.subject.as_str()).collect();
739        assert_eq!(subjects, ["f11", "f10", "f9", "f8", "f7", "f6", "f5", "f4"]);
740        assert_eq!(r.more, 6, "f3..f0, two and one");
741        assert_eq!(r.commits[0].reasoning.as_ref().map(|x| (x.source.as_str(), x.task.as_str())), Some(("store", "newest")));
742        assert_eq!(r.commits[1].reasoning.as_ref().map(|x| x.source.as_str()), Some("note"));
743        assert_eq!((r.commits[0].author.as_str(), r.commits[0].short.len() >= 7), ("Cy", true));
744        let full = fx.why(&fx.repo, "src/lib.rs", true).unwrap().unwrap();
745        assert_eq!((full.commits.len(), full.more), (14, 0));
746        assert_eq!(full.commits[11].reasoning.as_ref().map(|x| x.task.as_str()), Some("cut"));
747        assert_eq!(full.commits[12].sha, fx.two);
748    }
749
750    #[test]
751    fn a_dir_without_git_is_a_clear_error() {
752        let fx = fixture("nogit");
753        let plain = fx.work.join("plain");
754        std::fs::create_dir_all(&plain).unwrap();
755        std::fs::write(plain.join("a.txt"), "x\n").unwrap();
756        // GIT_CEILING_DIRECTORIES is not needed: /tmp/... is not inside a repo.
757        let err = fx.why(&plain, "a.txt:1", false).unwrap_err();
758        assert!(err.contains("is not in a git repo"), "{err}");
759        let err = fx.why(&plain, "HEAD", false).unwrap_err();
760        assert!(err.contains("is not in a git repo"), "{err}");
761    }
762
763    #[test]
764    fn compact_caps_and_full() {
765        let fx = fixture("caps");
766        let long = "x".repeat(2000);
767        let e = Excerpt {
768            kind: "reasoning".into(),
769            commit: fx.two.clone(),
770            repo: "app".into(),
771            session: "s".into(),
772            task: "t".into(),
773            proposal: format!("start {long} end"),
774            episodes: vec![],
775            ms: 1,
776            ..Excerpt::default()
777        };
778        notes::write_note(&fx.repo, &e).unwrap();
779        let compact = fx.why(&fx.repo, "HEAD", false).unwrap().unwrap();
780        let p = &compact.commits[0].reasoning.as_ref().unwrap().proposal;
781        assert!(p.chars().count() <= PROPOSAL_CAP + 1 && p.ends_with("end"), "the proposal's conclusion is kept");
782        let full = fx.why(&fx.repo, "HEAD", true).unwrap().unwrap();
783        assert!(full.commits[0].reasoning.as_ref().unwrap().proposal.starts_with("start"), "an untagged (older) note shows in the repo's collective");
784    }
785}