Skip to main content

borg/
notes.rs

1//! Reasoning travels with the code: a redacted excerpt per landed commit, as a git note.
2//!
3//! `borg notes attach [<commit>]` writes, for each commit a drone's session landed (or the one
4//! named), the session's task, the drone's last proposal and the session's episode ids to
5//! `refs/notes/borg` in that repo. Notes are local refs: borg never pushes them (an engineer who
6//! wants them on a remote pushes `refs/notes/borg` like any ref). The hook's `end` event runs it in
7//! the background when the session started in a git repo (`<state>/<session>.head` holds the repo
8//! and its HEAD at the session's first prompt).
9//!
10//! **Only the session's own commits** (`landed`): a commit gets this session's note only if HEAD's
11//! reflog says `commit` (or `commit (amend)`, `commit (initial)`, `revert`, `cherry-pick`) created it while the session was
12//! working (between one of its prompts and the stop that followed, `<state>/<session>.active`) and
13//! it is still on HEAD. A `git pull`, a merge, a checkout of another branch, or a commit made while
14//! this session was idle (the engineer, or another session in the same repo) gets nothing from it.
15//! Two sessions working in the same worktree at the same second cannot be told apart.
16//!
17//! **Redaction first** (`redact`): tokens, keys, passwords and `.env`-style values are replaced
18//! before an excerpt is written anywhere, by a conservative regex list (it rather blanks a harmless
19//! value than lets a secret through).
20//!
21//! **Replication** (PLAN.md "Availability"): the same excerpt is appended to the collective's
22//! `reasoning.jsonl`, which the mesh (borg-mesh, sync.rs) hands to two member replicas, so the "why"
23//! survives the author being offline. Only when `collective::may_sync` allows it: a local (private)
24//! collective's excerpts stay in that repo's notes, and an off repo gets nothing at all.
25//! `borg notes fetch [<commit>...]` reads it back: it asks the known members (borg-mesh `fetch`,
26//! author or replica) and writes what they hold as notes (restore.rs).
27//!
28//! **Links** (regress.rs): after `attach`, the landed commits are matched against the earlier
29//! decisions whose lines they changed; a note then lists them under `borg fixed in:` (on the
30//! decision) and `borg fixes:` (on the fix), links.rs.
31
32use std::path::{Path, PathBuf};
33use std::process::{Command, Stdio};
34use std::sync::LazyLock;
35
36use regex::{Captures, Regex};
37use serde::{Deserialize, Serialize};
38use serde_json::Value;
39
40use crate::{collective, learn};
41
42pub mod links;
43pub mod reasoning;
44pub mod regress;
45pub mod regress_cli;
46pub mod regress_cosmetic;
47pub mod regress_diff;
48pub mod regress_git;
49pub mod regress_learn;
50pub mod regress_signals;
51pub mod regret;
52pub mod restore;
53
54use links::LinkRef;
55pub use restore::{restore, wanted};
56
57pub const NOTES_REF: &str = "refs/notes/borg";
58/// At most this many commits get a note per run (a session that landed more is unusual; the
59/// newest are the ones whose reasoning is still in the session). Guard: a session that committed
60/// many times (a scripted series) writes at most this many notes per run.
61const MAX_COMMITS: usize = 20;
62
63const REDACTED: &str = "[REDACTED]";
64
65/// (pattern, replacement): applied in order.
66static SECRETS: LazyLock<Vec<(Regex, &'static str)>> = LazyLock::new(|| {
67    [
68        // PEM private keys, also when cut off before their END line.
69        (r"-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----(?s:.*?)(?:-----END [A-Z0-9 ]*PRIVATE KEY-----|\z)", "[REDACTED PRIVATE KEY]"),
70        // ... and when the text starts inside one (a window cut before its BEGIN line): an END
71        // left over blanks everything before it.
72        (r"(?s)\A.*?-----END [A-Z0-9 ]*PRIVATE KEY-----", "[REDACTED PRIVATE KEY]"),
73        // Provider tokens with a recognisable shape.
74        (r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b", REDACTED),
75        (r"\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}|\bgithub_pat_[A-Za-z0-9_]{20,}", REDACTED),
76        (r"\bglpat-[A-Za-z0-9_-]{20,}", REDACTED),
77        (r"\bxox[abposr]-[A-Za-z0-9-]{10,}", REDACTED),
78        (r"\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{20,}", REDACTED),
79        (r"\bAIza[0-9A-Za-z_-]{35}", REDACTED),
80        (r"\bnpm_[A-Za-z0-9]{36}\b", REDACTED),
81        (r"\bhf_[A-Za-z0-9]{30,}", REDACTED),
82        (r"\b(?:sk|rk|pk)_(?:live|test)_[A-Za-z0-9]{16,}", REDACTED),
83        (r"\bya29\.[A-Za-z0-9_-]{20,}", REDACTED),
84        (r"\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}", REDACTED),
85        // Credentials in URLs and auth headers.
86        (r"(?i)\b([a-z][a-z0-9+.-]*://[^\s:/@]+):[^\s@/]+@", "$1:[REDACTED]@"),
87        (r"(?i)\b(bearer|basic|token)(\s+)[A-Za-z0-9._~+/=-]{12,}", "$1$2[REDACTED]"),
88        // A password glued to its flag on a database client's command line (`mysql -pS3cret`).
89        (r"(?i)\b((?:mysql|mysqldump|mysqladmin|mariadb|psql)\b[^\n]*?\s-p)[^\s-]\S*", "${1}[REDACTED]"),
90        // key = value / "key": "value" where the key names a secret: also `pass`, `pw` and any name
91        // ending in `key` (`signing_key`, `secretKey`), whatever the value looks like (lowercase hex too).
92        (
93            r#"(?i)\b([A-Za-z0-9_.-]*?(?:password|passwd|passphrase|pass|pwd|pw|secret|token|api[_-]?key|apikey|access[_-]?key|private[_-]?key|key|credentials?|authorization|auth)(?:[_.-][A-Za-z0-9_.-]*|(?-i:[A-Z])[A-Za-z0-9_.-]*)?)(["']?\s*[:=]\s*)("[^"\n]*"|'[^'\n]*'|[^\s,;}]+)"#,
94            "$1$2[REDACTED]",
95        ),
96        // .env lines: any upper-case name assigned at the start of a line.
97        (r"(?m)^(\s*(?:export\s+)?[A-Z][A-Z0-9_]*\s*=\s*)\S.*$", "$1[REDACTED]"),
98    ]
99    .into_iter()
100    .map(|(p, r)| (Regex::new(p).expect("secret pattern"), r))
101    .collect()
102});
103
104/// Candidates for an unlabelled random token (checked by `looks_random`).
105static TOKENISH: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[A-Za-z0-9+/_=-]{32,}").unwrap());
106
107/// A long run of mixed case and digits that is not a path or an identifier: some `/`-free part of
108/// at least 24 characters with upper, lower and at least two digits; or, since base64 secrets
109/// contain `/` too, a whole run of 40 or more with upper, lower, a digit and at least 30 % of its
110/// letters upper case (paths and CamelCase names have far fewer). Lowercase hex (commit ids,
111/// hashes) and UUIDs are kept: they identify, they do not authorise.
112fn looks_random(s: &str) -> bool {
113    let mixed = |p: &str, digits: usize| {
114        p.chars().any(|c| c.is_ascii_uppercase()) && p.chars().any(|c| c.is_ascii_lowercase()) && p.chars().filter(|c| c.is_ascii_digit()).count() >= digits
115    };
116    let upper = s.chars().filter(|c| c.is_ascii_uppercase()).count();
117    let letters = s.chars().filter(|c| c.is_ascii_alphabetic()).count();
118    (s.len() >= 40 && mixed(s, 1) && upper * 10 >= letters * 3) || s.split('/').any(|p| p.len() >= 24 && mixed(p, 2))
119}
120
121/// `text` with every secret the list recognises replaced (module doc).
122pub fn redact(text: &str) -> String {
123    let mut out = text.to_string();
124    for (re, with) in SECRETS.iter() {
125        out = re.replace_all(&out, *with).into_owned();
126    }
127    TOKENISH.replace_all(&out, |c: &Captures| if looks_random(&c[0]) { REDACTED.to_string() } else { c[0].to_string() }).into_owned()
128}
129
130/// The per-session scratch directory (as the hook uses it).
131pub fn state_dir() -> PathBuf {
132    PathBuf::from(std::env::var("BORG_STATE").unwrap_or_else(|_| "/tmp/borg".into()))
133}
134
135/// `session` as a file name in the state dir.
136pub(crate) fn safe(session: &str) -> String {
137    session.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '-').collect()
138}
139
140fn state(dir: &Path, session: &str, ext: &str) -> PathBuf {
141    dir.join(format!("{}.{ext}", safe(session)))
142}
143
144pub(crate) fn git(repo: &Path, args: &[&str]) -> Option<String> {
145    Command::new("git")
146        .arg("-C")
147        .arg(repo)
148        .args(args)
149        .stdin(Stdio::null())
150        .stderr(Stdio::null())
151        .output()
152        .ok()
153        .filter(|o| o.status.success())
154        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
155}
156
157/// Where a session started: the repo's top level and its HEAD then.
158#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
159pub struct Start {
160    pub repo: String,
161    pub head: String,
162    pub ms: i64,
163}
164
165/// Records where `session` started, once (hook, first prompt in a git repo).
166pub fn remember_start(dir: &Path, session: &str, cwd: &str) {
167    let p = state(dir, session, "head");
168    if cwd.is_empty() || p.exists() {
169        return;
170    }
171    let start = git(Path::new(cwd), &["rev-parse", "--show-toplevel"])
172        .and_then(|repo| Some(Start { head: git(Path::new(&repo), &["rev-parse", "HEAD"])?, repo, ms: learn::now_ms() }));
173    let _ = std::fs::create_dir_all(dir);
174    // Outside a git repo (or before its first commit) a `null` marker keeps later prompts from
175    // asking git again; `end` then has nothing to do.
176    let _ = std::fs::write(p, serde_json::to_string(&start).unwrap());
177}
178
179/// Marks the session working (`prompt`) or idle again (`stop`), for `landed` (hook).
180pub fn mark(dir: &Path, session: &str, what: &str) {
181    use std::io::Write;
182    let _ = std::fs::create_dir_all(dir);
183    if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(state(dir, session, "active")) {
184        let _ = writeln!(f, "{what} {}", learn::now_ms());
185    }
186}
187
188/// When the session was working, as `[from, to]` epoch ms: from each prompt to the stop after it
189/// (open-ended while it works). Without marks, everything since the session started.
190pub fn windows(dir: &Path, session: &str, start: &Start) -> Vec<(i64, i64)> {
191    let text = std::fs::read_to_string(state(dir, session, "active")).unwrap_or_default();
192    let mut out = Vec::new();
193    let mut open: Option<i64> = None;
194    for (what, ms) in text.lines().filter_map(|l| l.split_once(' ')).filter_map(|(w, m)| Some((w, m.trim().parse::<i64>().ok()?))) {
195        match what {
196            "prompt" => {
197                open.get_or_insert(ms);
198            }
199            "stop" => {
200                if let Some(from) = open.take() {
201                    out.push((from, ms));
202                }
203            }
204            _ => {}
205        }
206    }
207    if let Some(from) = open {
208        out.push((from, i64::MAX));
209    }
210    if out.is_empty() {
211        out.push((start.ms, i64::MAX));
212    }
213    out
214}
215
216/// Keeps the drone's latest proposal for the excerpt (hook, at stop and end): redacted whole, then
217/// cut to its last `learn::PROPOSAL_CHARS`, so a cut never lands inside a secret that was not redacted.
218pub fn remember_proposal(dir: &Path, session: &str, text: &str) {
219    if text.trim().is_empty() {
220        return;
221    }
222    let text = redact(text);
223    let tail: String = text.chars().rev().take(crate::learn::PROPOSAL_CHARS).collect::<Vec<_>>().into_iter().rev().collect();
224    let _ = std::fs::create_dir_all(dir);
225    let _ = std::fs::write(state(dir, session, "proposal"), tail);
226}
227
228/// The redacted reasoning of one landed commit.
229#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
230pub struct Excerpt {
231    pub kind: String,
232    pub commit: String,
233    pub repo: String,
234    pub session: String,
235    pub task: String,
236    pub proposal: String,
237    pub episodes: Vec<String>,
238    pub ms: i64,
239    /// The collective the session worked in (`borg why` shows a note only inside it); empty in
240    /// excerpts from before it was recorded.
241    #[serde(default, skip_serializing_if = "String::is_empty")]
242    pub collective: String,
243    /// Later commits that fixed (or revisited) this commit's lines (regress.rs).
244    #[serde(default, skip_serializing_if = "Vec::is_empty")]
245    pub fixed_by: Vec<LinkRef>,
246    /// The earlier decisions this commit fixed.
247    #[serde(default, skip_serializing_if = "Vec::is_empty")]
248    pub fixes: Vec<LinkRef>,
249}
250
251impl Excerpt {
252    /// What `git notes show` prints.
253    pub fn note(&self) -> String {
254        let within = if self.collective.is_empty() { String::new() } else { format!(", collective {}", self.collective) };
255        let mut s = format!("borg reasoning (session {}{within})\n\nTask:\n{}\n\nLast proposal:\n{}\n", self.session, self.task.trim(), self.proposal.trim());
256        if !self.episodes.is_empty() {
257            s.push_str(&format!("\nCorrections (episodes): {}\n", self.episodes.join(", ")));
258        }
259        s.push_str(&links::sections(&self.fixed_by, &self.fixes));
260        s
261    }
262
263    /// The excerpt a borg note holds (the inverse of `note`), for `commit`; `None` if the text is
264    /// not a borg reasoning note.
265    pub fn from_note(commit: &str, text: &str) -> Option<Excerpt> {
266        let (text, fixed_by, fixes) = links::split(text);
267        let (head, rest) = text.split_once('\n')?;
268        let inner = head.trim().strip_prefix("borg reasoning (session ")?.strip_suffix(')')?;
269        let (session, collective) = inner.split_once(", collective ").unwrap_or((inner, ""));
270        // git drops the blank lines an empty task or proposal leaves, and `git` trims the end.
271        let rest = rest.trim_start().strip_prefix("Task:")?;
272        let (task, rest) = rest.split_once("\nLast proposal:").map_or((rest, ""), |(t, p)| (t, p.strip_prefix('\n').unwrap_or(p)));
273        let (proposal, episodes) = match rest.rsplit_once("\nCorrections (episodes): ") {
274            Some((p, e)) => (p, e.trim().split(", ").filter(|e| !e.is_empty()).map(str::to_string).collect()),
275            None => (rest, Vec::new()),
276        };
277        Some(Excerpt {
278            kind: "reasoning".into(),
279            commit: commit.into(),
280            repo: String::new(),
281            session: session.into(),
282            task: task.trim().into(),
283            proposal: proposal.trim().into(),
284            episodes,
285            ms: 0,
286            collective: collective.into(),
287            fixed_by,
288            fixes,
289        })
290    }
291}
292
293/// The session's task, last proposal and episode ids, redacted, for `commit` of `repo`.
294pub fn excerpt(dir: &Path, session: &str, repo: &str, commit: &str, now: i64) -> Excerpt {
295    let read = |ext| std::fs::read_to_string(state(dir, session, ext)).unwrap_or_default();
296    let episodes = read("ctx").lines().filter_map(|l| l.strip_prefix("#borg-episode ")).map(|s| s.trim().to_string()).collect();
297    Excerpt {
298        kind: "reasoning".into(),
299        commit: commit.into(),
300        repo: repo.into(),
301        session: session.into(),
302        task: redact(&read("task").chars().take(crate::learn::TASK_CHARS).collect::<String>()),
303        proposal: redact(&read("proposal")),
304        episodes,
305        ms: now,
306        ..Excerpt::default()
307    }
308}
309
310/// The commits the session itself made (module doc), newest first, at most `MAX_COMMITS`: HEAD's
311/// reflog entries of a `commit` within one of `windows`, still reachable from HEAD but not from
312/// the session's starting HEAD.
313pub fn landed(repo: &Path, start: &Start, windows: &[(i64, i64)]) -> Vec<String> {
314    let on_head: std::collections::HashSet<String> =
315        git(repo, &["rev-list", "--max-count=1000", &format!("{}..HEAD", start.head)]).map(|t| t.lines().map(str::to_string).collect()).unwrap_or_default();
316    // `HEAD@{<unix seconds>}` with --date=unix; the reflog keeps seconds, the marks milliseconds.
317    let within = |secs: i64| windows.iter().any(|&(from, to)| secs >= from.div_euclid(1000) && secs <= to.saturating_add(999) / 1000);
318    let reflog = git(repo, &["log", "-g", "-n", "1000", "--date=unix", "--format=%H%x09%gd%x09%gs", "HEAD"]).unwrap_or_default();
319    let mut out: Vec<String> = Vec::new();
320    for l in reflog.lines() {
321        let mut f = l.splitn(3, '\t');
322        let (Some(c), Some(at), Some(subject)) = (f.next(), f.next(), f.next()) else { continue };
323        let own = ["commit:", "commit (amend):", "commit (initial):", "revert:", "cherry-pick:"].iter().any(|p| subject.starts_with(p));
324        let secs = at.rsplit_once('{').and_then(|(_, t)| t.trim_end_matches('}').parse::<i64>().ok());
325        if own && secs.is_some_and(within) && on_head.contains(c) && !out.iter().any(|o| o == c) {
326            out.push(c.to_string());
327        }
328        if out.len() >= MAX_COMMITS {
329            break;
330        }
331    }
332    out
333}
334
335/// The commits `session` landed so far (`landed`, from its start recorded in `dir`); empty when it
336/// started outside git or left no record here.
337pub fn landed_by(dir: &Path, session: &str) -> Vec<String> {
338    start_of(dir, session).map(|s| landed(Path::new(&s.repo), &s, &windows(dir, session, &s))).unwrap_or_default()
339}
340
341/// Where `session` started (`remember_start`, recorded in `dir`); None outside git or without a record.
342pub fn start_of(dir: &Path, session: &str) -> Option<Start> {
343    std::fs::read_to_string(state(dir, session, "head")).ok().and_then(|t| serde_json::from_str::<Start>(&t).ok())
344}
345
346/// The top level of the git repo `dir` is in.
347pub fn toplevel(dir: &str) -> Option<PathBuf> {
348    git(Path::new(dir), &["rev-parse", "--show-toplevel"]).map(PathBuf::from)
349}
350
351/// The borg note of `commit`, parsed; `None` without one.
352#[must_use]
353pub fn note_of(repo: &Path, commit: &str) -> Option<Excerpt> {
354    Excerpt::from_note(commit, &git(repo, &["notes", &format!("--ref={NOTES_REF}"), "show", commit])?)
355}
356
357pub(crate) fn has_note(repo: &Path, commit: &str) -> bool {
358    git(repo, &["notes", &format!("--ref={NOTES_REF}"), "list", commit]).is_some()
359}
360
361/// Writes `e` as the borg note of its commit (replacing an older borg note). Never pushes.
362pub fn write_note(repo: &Path, e: &Excerpt) -> Result<(), String> {
363    use std::io::Write;
364    let mut child = Command::new("git")
365        .arg("-C")
366        .arg(repo)
367        .args(["notes", &format!("--ref={NOTES_REF}"), "add", "-f", "-F", "-", &e.commit])
368        .stdin(Stdio::piped())
369        .stdout(Stdio::null())
370        .stderr(Stdio::piped())
371        .spawn()
372        .map_err(|err| format!("git notes: {err}"))?;
373    child.stdin.take().ok_or("git notes: no stdin")?.write_all(e.note().as_bytes()).map_err(|err| format!("git notes: {err}"))?;
374    let out = child.wait_with_output().map_err(|err| format!("git notes: {err}"))?;
375    if !out.status.success() {
376        return Err(format!("git notes add {}: {}", e.commit, String::from_utf8_lossy(&out.stderr).trim()));
377    }
378    Ok(())
379}
380
381/// What one run did.
382#[derive(Debug, Default, PartialEq, Eq)]
383pub struct Attached {
384    pub notes: Vec<String>,
385    /// Excerpts queued for the mesh's replicas (0 for a local collective).
386    pub replicated: usize,
387}
388
389/// Notes for `l.commits` from `l.session` in the collective `l.route` names (an off route attaches
390/// nothing); excerpts go to `l.store`'s reasoning.jsonl only if the route may sync. `force`
391/// replaces an existing borg note (an explicitly named commit), keeping the links it lists.
392///
393/// # Errors
394/// A note git would not write, or a store that cannot be appended to.
395pub fn attach(l: &Landed, force: bool) -> Result<Attached, String> {
396    let collective::Route::Collective { name: within, .. } = l.route else { return Ok(Attached::default()) };
397    let may_sync = collective::may_sync(l.route);
398    let name = l.repo.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
399    let mut done = Attached::default();
400    for c in l.commits {
401        if !force && has_note(l.repo, c) {
402            continue;
403        }
404        let mut e = Excerpt { collective: within.clone(), ..excerpt(l.dir, l.session, &name, c, learn::now_ms()) };
405        if let Some(old) = note_of(l.repo, c).filter(|_| force) {
406            (e.fixed_by, e.fixes) = (old.fixed_by, old.fixes);
407        }
408        write_note(l.repo, &e)?;
409        if may_sync {
410            append_reasoning(l.store, &e).map_err(|err| format!("{}: {err}", l.store.display()))?;
411            done.replicated += 1;
412        }
413        done.notes.push(c.clone());
414    }
415    Ok(done)
416}
417
418pub(crate) fn append_reasoning(store: &Path, e: &Excerpt) -> std::io::Result<()> {
419    use std::io::Write;
420    std::fs::create_dir_all(store)?;
421    let mut f = std::fs::OpenOptions::new().create(true).append(true).open(store.join("reasoning.jsonl"))?;
422    writeln!(f, "{}", serde_json::to_string(e).unwrap())
423}
424
425/// The session that last started in `repo`, from the `.head` files in `dir`.
426#[must_use]
427pub fn latest_session(dir: &Path, repo: &str) -> Option<(String, Start)> {
428    std::fs::read_dir(dir)
429        .ok()?
430        .flatten()
431        .filter_map(|f| {
432            let p = f.path();
433            let session = p.file_name()?.to_str()?.strip_suffix(".head")?.to_string();
434            let start: Start = serde_json::from_str(&std::fs::read_to_string(&p).ok()?).ok()?;
435            (start.repo == repo).then_some((session, start))
436        })
437        .max_by_key(|(_, s)| s.ms)
438}
439
440/// Hook, `end`: if the session started in a git repo, attach notes in the background (never delays
441/// the drone; any failure is silent), and `borg learn --session` after them (`--then-learn`), so
442/// learning sees the regret episodes the links made. False when no job was started: the caller
443/// then starts `borg learn` itself.
444#[must_use]
445pub fn on_end(session: &str, input: &Value) -> bool {
446    let dir = state_dir();
447    let started = std::fs::read_to_string(state(&dir, session, "head")).ok().and_then(|t| serde_json::from_str::<Start>(&t).ok());
448    if started.is_none() {
449        return false;
450    }
451    let cwd = input.get("cwd").and_then(Value::as_str).unwrap_or_default();
452    let (Ok(exe), Some(store)) = (std::env::current_exe(), std::env::var_os("BORG_HOME")) else { return false };
453    Command::new(exe)
454        .args(["notes", "attach", "--session", session, "--dir", cwd, "--then-learn", "--store"])
455        .arg(store)
456        .stdin(Stdio::null())
457        .stdout(Stdio::null())
458        .stderr(Stdio::null())
459        .spawn()
460        .is_ok()
461}
462
463/// `--then-learn`: `borg learn [--session ID]` in the background once attach is done, whatever it
464/// did, on the store the run used (`--store`, else the caller's `BORG_HOME`).
465fn then_learn(args: &[&str]) {
466    let flag = |f: &str| args.iter().position(|a| *a == f).and_then(|i| args.get(i + 1)).copied();
467    let Ok(exe) = std::env::current_exe() else { return };
468    let mut c = Command::new(exe);
469    c.arg("learn").args(flag("--session").map(|s| ["--session", s]).into_iter().flatten());
470    if let Some(store) = flag("--store") {
471        c.env("BORG_HOME", store);
472    }
473    let _ = c.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).spawn();
474}
475
476/// A run's commits and where they go: what `attach` notes, and what runs after it (main.rs passes
477/// `regress_learn.rs`, which links them to the earlier decisions they fixed).
478pub struct Landed<'a> {
479    pub dir: &'a Path,
480    pub session: &'a str,
481    pub repo: &'a Path,
482    pub store: &'a Path,
483    pub route: &'a collective::Route,
484    pub commits: &'a [String],
485    /// Where the session started (its work windows come from it).
486    pub start: Option<&'a Start>,
487}
488
489/// Runs after `attach` for the session's landed commits (not for a named one); its text ends the
490/// run's message.
491pub type After = fn(&Landed) -> String;
492
493/// `borg notes attach [<commit>] [--session ID] [--dir DIR] [--store DIR] [--then-learn]`, or
494/// `borg notes fetch [<commit>...] [--dir DIR]` (runs `borg-mesh fetch`: the network is there).
495pub fn main(args: &[&str], after: Option<After>) -> i32 {
496    if args.first() == Some(&"fetch") {
497        let beside = std::env::current_exe().ok().map(|e| e.with_file_name("borg-mesh")).filter(|p| p.exists());
498        return match Command::new(beside.unwrap_or_else(|| "borg-mesh".into())).args(args).status() {
499            Ok(s) => s.code().unwrap_or(1),
500            Err(e) => {
501                eprintln!("borg notes: cannot run borg-mesh ({e}); cargo install borg-mesh --locked");
502                1
503            }
504        };
505    }
506    // A panic in what runs after attach must not keep `--then-learn` from starting learning.
507    let ran = std::panic::catch_unwind(|| run(args, after)).unwrap_or_else(|_| Err("internal error; the notes written so far are kept".into()));
508    let code = match ran {
509        Ok(msg) => {
510            println!("{msg}");
511            0
512        }
513        Err(e) => {
514            eprintln!("borg notes: {e}");
515            1
516        }
517    };
518    if args.contains(&"--then-learn") {
519        then_learn(args);
520    }
521    code
522}
523
524const USAGE: &str = "usage: borg notes attach [<commit>] [--session ID] [--dir DIR] | borg notes fetch [<commit>...] [--dir DIR]";
525
526/// Where an attach run works: the collective `cwd` routes to, its store and state dir, the repo.
527struct Place {
528    route: collective::Route,
529    store: PathBuf,
530    dir: PathBuf,
531    repo: String,
532}
533
534/// The place of `--dir` (else the cwd), with `--store` in place of the collective's home.
535fn place(dir: Option<&str>, store: Option<&str>) -> Result<Place, String> {
536    let cwd = dir.map(str::to_string).unwrap_or_else(|| std::env::current_dir().unwrap_or_default().display().to_string());
537    // The gate is the config as it is now: the share decides replication whatever the caller says.
538    let cfg = collective::load().map_err(|e| format!("invalid config, nothing is attached: {e}"))?;
539    let route = collective::route(cfg.as_ref(), &cwd, &learn::home());
540    let collective::Route::Collective { home, .. } = &route else {
541        return Err(format!("{cwd} belongs to no collective (off): nothing is attached"));
542    };
543    let store = store.map_or_else(|| home.clone(), PathBuf::from);
544    let dir = std::env::var_os("BORG_STATE").map_or_else(|| store.join("state"), PathBuf::from);
545    let repo = git(Path::new(&cwd), &["rev-parse", "--show-toplevel"]).ok_or(format!("{cwd} is not in a git repo"))?;
546    Ok(Place { route, store, dir, repo })
547}
548
549/// The session named (`--session`), else the latest one that started in the repo, and its start.
550fn session_of(named: Option<&str>, p: &Place) -> Result<(String, Option<Start>), String> {
551    match named {
552        Some(s) => Ok((s.to_string(), std::fs::read_to_string(state(&p.dir, s, "head")).ok().and_then(|t| serde_json::from_str::<Start>(&t).ok()))),
553        None => latest_session(&p.dir, &p.repo).map(|(s, st)| (s, Some(st))).ok_or_else(|| "no drone session started in this repo; pass --session ID".into()),
554    }
555}
556
557/// The commit named, else the session's landed commits.
558fn commits_of(named: Option<&str>, start: Option<&Start>, p: &Place, session: &str) -> Result<Vec<String>, String> {
559    match (named, start) {
560        (Some(c), _) => Ok(vec![git(Path::new(&p.repo), &["rev-parse", "--verify", &format!("{c}^{{commit}}")]).ok_or(format!("{c} is not a commit"))?]),
561        (None, Some(st)) if st.repo == p.repo => Ok(landed(Path::new(&p.repo), st, &windows(&p.dir, session, st))),
562        _ => Err("the session did not start in this repo; name a commit".into()),
563    }
564}
565
566/// The run's message: the notes written, where the excerpts went, and what ran after (`then`).
567fn message(done: &Attached, route: &collective::Route, then: &str) -> String {
568    let queued = if collective::may_sync(route) {
569        format!("{} excerpt(s) queued for the mesh's replicas", done.replicated)
570    } else {
571        "local collective: nothing leaves this machine".into()
572    };
573    format!("{} note(s) in {NOTES_REF} (not pushed); {queued}{then}", done.notes.len())
574}
575
576fn run(args: &[&str], after: Option<After>) -> Result<String, String> {
577    let flag = |f: &str| args.iter().position(|a| *a == f).and_then(|i| args.get(i + 1)).copied();
578    if args.first() != Some(&"attach") {
579        return Err(USAGE.into());
580    }
581    let commit = args.get(1).filter(|a| !a.starts_with("--")).copied();
582    let p = place(flag("--dir"), flag("--store"))?;
583    let (session, start) = session_of(flag("--session"), &p)?;
584    let commits = commits_of(commit, start.as_ref(), &p, &session)?;
585    let repo = Path::new(&p.repo);
586    let landed = Landed { dir: &p.dir, session: &session, repo, store: &p.store, route: &p.route, commits: &commits, start: start.as_ref() };
587    let done = attach(&landed, commit.is_some())?;
588    // What runs after (regress.rs) is for the session's landed commits, not for a named one.
589    let then = after.filter(|_| commit.is_none()).map(|f| f(&landed)).unwrap_or_default();
590    Ok(message(&done, &p.route, &then))
591}
592
593#[cfg(test)]
594mod tests {
595    use super::*;
596    use links::Link;
597
598    #[test]
599    fn redacts_secrets_and_keeps_the_reasoning() {
600        let cases = [
601            ("export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "wJalr"),
602            ("use AKIAIOSFODNN7EXAMPLE for s3", "AKIAIOSFODNN7EXAMPLE"),
603            ("token ghp_abcdefghijklmnopqrstuvwxyz0123456789 in CI", "ghp_abc"),
604            ("set ANTHROPIC key sk-ant-api03-abcdefghijklmnopqrstuvwx", "sk-ant-api03"),
605            ("curl -H 'Authorization: Bearer abc.def.ghijklmnopqrstu'", "abc.def"),
606            ("postgres://admin:hunter2@db:5432/app", "hunter2"),
607            (r#"{"password": "correct horse battery"}"#, "correct horse"),
608            ("db_password = s3cr3t!", "s3cr3t"),
609            ("API_KEY: 12345abcdef", "12345abcdef"),
610            ("DATABASE_URL=mysql://u@h/db\nNODE_ENV=production", "production"),
611            ("-----BEGIN RSA PRIVATE KEY-----\nMIIEow\n-----END RSA PRIVATE KEY-----", "MIIEow"),
612            ("-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXk (truncated", "b3Blbn"),
613            ("jwt eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N", "eyJhbGci"),
614            ("random Zx9Qk2LmPw8RtYv3NbHs6JdFgKa1Bc4D blob", "Zx9Qk2"),
615            ("slack xoxb-123456789012-abcdefghij", "xoxb-1234"),
616            // Labels the first list missed.
617            ("const config = { secretKey: 'hunter2hunter2' }", "hunter2"),
618            (r#"{"user": "ann", "pass": "hunter2"}"#, "hunter2"),
619            ("db_pass=hunter2", "hunter2"),
620            ("then set DB_PASS=hunter2 and rerun", "hunter2"),
621            ("mysql -u root -pS3cretPass app", "S3cretPass"),
622            ("signing_key = 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "9f86d0"),
623            ("pw: tr0ub4dor", "tr0ub4dor"),
624            // Unlabelled shapes.
625            ("the secret is wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY ok", "wJalrXUtnFEMI"),
626            ("HF hf_abcdefghijklmnopqrstuvwxyzABCDEFGH works", "hf_abc"),
627            ("stripe sk_live_abcdefghijklmnopqrstuvwx and rk_live_abcdefghijklmnopqrstuvwx", "_live_abc"),
628            ("publishable pk_test_abcdefghijklmnopqrstuvwx", "pk_test_abc"),
629            ("google ya29.a0AfH6SMBabcdefghijklmnopqrstuv", "ya29.a0Af"),
630            // A window that starts inside a key: no BEGIN line left, only its body and END.
631            ("bnBlbnNzaC1rZXktdjEAAAAABG5vbmU\nAAAAAAAAAAEAAAAzAAAAC3NzaC1lZDI1\n-----END OPENSSH PRIVATE KEY-----\nthen I rotated it", "bnBlbn"),
632        ];
633        for (text, secret) in cases {
634            let r = redact(text);
635            assert!(!r.contains(secret), "{text:?} -> {r:?}");
636            assert!(r.contains("[REDACTED"), "{text:?} -> {r:?}");
637        }
638        let plain = "Take the lock before reading the flag (commit 3f2a9c1e5b7d4f6a8c0e2b4d6f8a0c2e4b6d8f0a, \
639                     file /home/user/src/borg-ai/crates/borg-mesh/src/sync.rs, author: Ann, 12 tokens: fine). \
640                     id 123e4567-e89b-12d3-a456-426614174000; HashMapWithLongCamelCaseName stays.";
641        assert_eq!(redact(plain), plain, "reasoning without secrets is untouched");
642        assert!(redact("-----END RSA PRIVATE KEY-----\nthen I rotated it").ends_with("then I rotated it"), "what follows the key stays");
643        let paths = "see /Users/Ann/Projects/MyApp/src/main.rs and crates/borg-mesh/src/sync.rs for Lock2Order";
644        assert_eq!(redact(paths), paths, "paths are not secrets");
645    }
646
647    /// The proposal is redacted whole before it is cut to its last 2000 characters: a cut inside a
648    /// PEM block (BEGIN gone) never leaves the key body behind.
649    #[test]
650    fn a_long_proposal_is_redacted_before_it_is_cut() {
651        let dir = std::env::temp_dir().join(format!("borg-notes-cut-{}", std::process::id()));
652        let _ = std::fs::remove_dir_all(&dir);
653        let body: String = (0..60).map(|i| format!("b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAAB{i:02}\n")).collect();
654        let text = format!("-----BEGIN OPENSSH PRIVATE KEY-----\n{body}-----END OPENSSH PRIVATE KEY-----\nI will use the deploy key above.");
655        assert!(text.len() > 2000 && !text[text.len() - 2000..].contains("BEGIN"), "the cut lands inside the key");
656        remember_proposal(&dir, "s-cut", &text);
657        let kept = std::fs::read_to_string(dir.join("s-cut.proposal")).unwrap();
658        assert!(!kept.contains("b3BlbnNzaC1rZXkt"), "{kept}");
659        assert!(kept.contains("I will use the deploy key above."));
660        let _ = std::fs::remove_dir_all(&dir);
661    }
662
663    fn route(name: &str, share: collective::Share) -> collective::Route {
664        collective::Route::Collective { name: name.into(), home: PathBuf::from("/unused"), share }
665    }
666
667    fn sh(repo: &Path, args: &[&str]) -> String {
668        let o = Command::new("git").arg("-C").arg(repo).args(args).env("GIT_CONFIG_GLOBAL", "/dev/null").env("GIT_CONFIG_NOSYSTEM", "1").output().unwrap();
669        assert!(o.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&o.stderr));
670        String::from_utf8_lossy(&o.stdout).trim().to_string()
671    }
672
673    fn repo(tag: &str) -> (PathBuf, PathBuf, PathBuf) {
674        let base = std::env::temp_dir().join(format!("borg-notes-{tag}-{}", std::process::id()));
675        let _ = std::fs::remove_dir_all(&base);
676        let (repo, state, store) = (base.join("repo"), base.join("state"), base.join("store"));
677        std::fs::create_dir_all(&repo).unwrap();
678        sh(&repo, &["init", "-q"]);
679        sh(&repo, &["config", "user.email", "ann@example.com"]);
680        sh(&repo, &["config", "user.name", "Ann"]);
681        sh(&repo, &["commit", "-q", "--allow-empty", "-m", "base"]);
682        (repo, state, store)
683    }
684
685    #[test]
686    fn notes_attach_writes_a_redacted_note_per_landed_commit() {
687        let (repo, dir, store) = repo("attach");
688        remember_start(&dir, "s-1", &repo.display().to_string());
689        let start: Start = serde_json::from_str(&std::fs::read_to_string(dir.join("s-1.head")).unwrap()).unwrap();
690        std::fs::write(dir.join("s-1.task"), "fix the login; the staging password=hunter22 must not leak").unwrap();
691        std::fs::write(dir.join("s-1.ctx"), "src/login.rs\n#borg-episode s-1-100\n#borg-asked L1\n#borg-episode s-1-200\n").unwrap();
692        remember_proposal(&dir, "s-1", "I will rotate ghp_abcdefghijklmnopqrstuvwxyz0123456789 and take the lock first.");
693        sh(&repo, &["commit", "-q", "--allow-empty", "-m", "one"]);
694        sh(&repo, &["commit", "-q", "--allow-empty", "-m", "two"]);
695        let commits = landed(&repo, &start, &windows(&dir, "s-1", &start));
696        assert_eq!(commits.len(), 2, "only the session's commits");
697
698        let corp = route("corp", collective::Share::Team);
699        let l = Landed { dir: &dir, session: "s-1", repo: &repo, store: &store, route: &corp, commits: &commits, start: None };
700        let done = attach(&l, false).unwrap();
701        assert_eq!((done.notes.len(), done.replicated), (2, 2));
702        let note = sh(&repo, &["notes", "--ref=refs/notes/borg", "show", &commits[0]]);
703        assert!(note.contains("fix the login") && note.contains("take the lock first") && note.contains("s-1-100, s-1-200"), "{note}");
704        let back = Excerpt::from_note(&commits[0], &note).unwrap();
705        assert_eq!((back.session.as_str(), back.collective.as_str(), back.episodes.len()), ("s-1", "corp", 2));
706        assert!(back.task.starts_with("fix the login") && back.proposal.ends_with("take the lock first."), "{back:?}");
707        assert!(!note.contains("hunter22") && !note.contains("ghp_abc"), "{note}");
708        let lines = std::fs::read_to_string(store.join("reasoning.jsonl")).unwrap();
709        assert_eq!(lines.lines().count(), 2);
710        assert!(!lines.contains("hunter22"));
711        assert_eq!(sh(&repo, &["remote"]), "", "nothing to push to, nothing pushed");
712
713        // Idempotent: a second run (another `end`) adds nothing.
714        assert_eq!(attach(&l, false).unwrap(), Attached::default());
715        let _ = std::fs::remove_dir_all(repo.parent().unwrap());
716    }
717
718    /// The veto's "after merge" (escalation.rs `merged`): a session that landed a commit of its own.
719    #[test]
720    fn landed_by_reads_the_sessions_recorded_start() {
721        let (repo, dir, _) = repo("landed-by");
722        assert!(landed_by(&dir, "s-3").is_empty(), "no start recorded");
723        remember_start(&dir, "s-3", &repo.display().to_string());
724        assert!(landed_by(&dir, "s-3").is_empty(), "nothing landed yet");
725        assert!(!crate::escalation::merged(&dir, "s-3"));
726        sh(&repo, &["commit", "-q", "--allow-empty", "-m", "landed"]);
727        assert_eq!(landed_by(&dir, "s-3"), vec![sh(&repo, &["rev-parse", "HEAD"])]);
728        assert!(crate::escalation::merged(&dir, "s-3"));
729        let _ = std::fs::remove_dir_all(repo.parent().unwrap());
730    }
731
732    #[test]
733    fn a_private_collective_keeps_its_notes_local() {
734        let (repo, dir, store) = repo("private");
735        remember_start(&dir, "s-2", &repo.display().to_string());
736        std::fs::write(dir.join("s-2.task"), "private refactor").unwrap();
737        sh(&repo, &["commit", "-q", "--allow-empty", "-m", "private work"]);
738        let head = sh(&repo, &["rev-parse", "HEAD"]);
739        let private = route("private", collective::Share::Local);
740        let l = Landed { dir: &dir, session: "s-2", repo: &repo, store: &store, route: &private, commits: std::slice::from_ref(&head), start: None };
741        let done = attach(&l, false).unwrap();
742        assert_eq!((done.notes.len(), done.replicated), (1, 0));
743        assert!(sh(&repo, &["notes", "--ref=refs/notes/borg", "show", &head]).contains("private refactor"), "the note itself is written");
744        assert!(!store.join("reasoning.jsonl").exists(), "nothing is queued for any peer");
745        let _ = std::fs::remove_dir_all(repo.parent().unwrap());
746    }
747
748    /// The CLI decides replication from the config, not from its caller.
749    #[test]
750    fn share_comes_from_the_config() {
751        let cfg: collective::Config =
752            serde_json::from_str(r#"{"collectives": [{"name": "private", "paths": ["/p"], "share": "local"}, {"name": "corp", "paths": ["/c"]}]}"#).unwrap();
753        let base = Path::new("/irrelevant");
754        assert!(!collective::may_sync(&collective::route(Some(&cfg), "/p/repo", base)));
755        assert!(collective::may_sync(&collective::route(Some(&cfg), "/c/repo", base)));
756    }
757
758    /// git as `sh`, at a fixed time (the reflog records the committer date).
759    fn sh_at(repo: &Path, secs: i64, args: &[&str]) {
760        let date = format!("@{secs} +0000");
761        let o = Command::new("git")
762            .arg("-C")
763            .arg(repo)
764            .args(args)
765            .env("GIT_CONFIG_GLOBAL", "/dev/null")
766            .env("GIT_CONFIG_NOSYSTEM", "1")
767            .env("GIT_COMMITTER_DATE", &date)
768            .env("GIT_AUTHOR_DATE", &date)
769            .output()
770            .unwrap();
771        assert!(o.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&o.stderr));
772    }
773
774    /// Only commits this session made get its note: not a colleague's commits a fast-forward
775    /// brought in, not a commit made while the session was idle (another session, the engineer),
776    /// not a commit on a branch checked out and left.
777    #[test]
778    fn only_the_sessions_own_commits_land() {
779        let (repo, dir, _) = repo("own");
780        let t0 = 1_700_000_000i64;
781        let main = sh(&repo, &["rev-parse", "--abbrev-ref", "HEAD"]);
782        sh_at(&repo, t0 + 10, &["checkout", "-q", "-b", "colleague"]);
783        sh_at(&repo, t0 + 20, &["commit", "-q", "--allow-empty", "-m", "colleague's work"]);
784        let theirs = sh(&repo, &["rev-parse", "HEAD"]);
785        sh_at(&repo, t0 + 30, &["checkout", "-q", &main]);
786        let start = Start { repo: repo.display().to_string(), head: sh(&repo, &["rev-parse", "HEAD"]), ms: (t0 + 100) * 1000 };
787        std::fs::create_dir_all(&dir).unwrap();
788        std::fs::write(dir.join("s-3.active"), format!("prompt {}\nstop {}\nprompt {}\n", (t0 + 100) * 1000, (t0 + 200) * 1000 + 500, (t0 + 300) * 1000))
789            .unwrap();
790        let w = windows(&dir, "s-3", &start);
791        assert_eq!(w, vec![((t0 + 100) * 1000, (t0 + 200) * 1000 + 500), ((t0 + 300) * 1000, i64::MAX)]);
792
793        sh_at(&repo, t0 + 150, &["merge", "-q", "--ff-only", "colleague"]); // git pull, in effect
794        sh_at(&repo, t0 + 160, &["commit", "-q", "--allow-empty", "-m", "one"]);
795        let one = sh(&repo, &["rev-parse", "HEAD"]);
796        sh_at(&repo, t0 + 250, &["commit", "-q", "--allow-empty", "-m", "another session's"]);
797        sh_at(&repo, t0 + 310, &["checkout", "-q", "-b", "side"]);
798        sh_at(&repo, t0 + 320, &["commit", "-q", "--allow-empty", "-m", "abandoned"]);
799        sh_at(&repo, t0 + 330, &["checkout", "-q", &main]);
800        sh_at(&repo, t0 + 340, &["commit", "-q", "--allow-empty", "-m", "three"]);
801        let three = sh(&repo, &["rev-parse", "HEAD"]);
802        sh_at(&repo, t0 + 350, &["commit", "-q", "--amend", "--allow-empty", "-m", "three, amended"]);
803        let amended = sh(&repo, &["rev-parse", "HEAD"]);
804
805        let got = landed(&repo, &start, &w);
806        assert_eq!(got, vec![amended.clone(), one.clone()], "only the session's commits still on HEAD");
807        assert!(!got.contains(&theirs) && !got.contains(&three));
808        // Without marks: everything the session committed since it started, still not the pull.
809        let all = landed(&repo, &start, &[(start.ms, i64::MAX)]);
810        assert_eq!(all.len(), 3, "{all:?}");
811        assert!(!all.contains(&theirs));
812        let _ = std::fs::remove_dir_all(repo.parent().unwrap());
813    }
814
815    /// The link sections survive a note's round trip; a note without them parses as before, and a
816    /// proposal that merely says "fixed in" is left alone.
817    #[test]
818    fn link_sections_round_trip() {
819        let r =
820            |c: &str, s: &str| LinkRef { commit: c.into(), session: "s-7".into(), task: "fix the race".into(), date: "2026-10-12".into(), strength: s.into() };
821        let e = Excerpt {
822            kind: "reasoning".into(),
823            commit: "c".into(),
824            session: "s-1".into(),
825            task: "take the lock".into(),
826            proposal: "Fixed in the next commit, maybe.".into(),
827            episodes: vec!["s-1-1".into()],
828            collective: "work".into(),
829            fixed_by: vec![r("f1", links::FIX), r("f2", links::REVISITED)],
830            fixes: vec![r("d0", links::FIX)],
831            ..Excerpt::default()
832        };
833        assert_eq!(Excerpt::from_note("c", &e.note()), Some(e.clone()));
834        let old = Excerpt { fixed_by: vec![], fixes: vec![], ..e.clone() };
835        assert_eq!(Excerpt::from_note("c", &old.note()), Some(old.clone()));
836        assert_eq!(Excerpt::from_note("c", old.note().trim_end()).map(|x| x.episodes), Some(vec!["s-1-1".to_string()]));
837        // As git hands it back: an empty proposal (or task) loses its blank lines, the end is trimmed.
838        let empty = Excerpt { proposal: String::new(), episodes: vec![], ..old.clone() };
839        let stored = empty.note().replace("\n\n\n", "\n\n");
840        assert_eq!(Excerpt::from_note("c", stored.trim_end()), Some(empty.clone()), "{stored:?}");
841        let linked = Excerpt { fixes: vec![r("d0", links::FIX)], ..empty.clone() };
842        assert_eq!(Excerpt::from_note("c", linked.note().replace("\n\n\n", "\n\n").trim_end()), Some(linked));
843        let no_task = Excerpt { task: String::new(), ..empty };
844        assert_eq!(Excerpt::from_note("c", no_task.note().replace("\n\n\n", "\n\n").trim_end()), Some(no_task));
845    }
846
847    /// A forced re-attach (a named commit) rewrites the reasoning and keeps the links the note lists;
848    /// a `git revert` the session made is one of its commits.
849    #[test]
850    fn a_forced_attach_keeps_the_links_and_a_revert_lands() {
851        let (repo, dir, store) = repo("force");
852        remember_start(&dir, "s-1", &repo.display().to_string());
853        let start: Start = serde_json::from_str(&std::fs::read_to_string(dir.join("s-1.head")).unwrap()).unwrap();
854        std::fs::write(dir.join("s-1.task"), "use a ttl cache").unwrap();
855        std::fs::write(repo.join("cache.rs"), "ttl\n").unwrap();
856        sh(&repo, &["add", "cache.rs"]);
857        sh(&repo, &["commit", "-q", "-m", "use a ttl cache"]);
858        sh(&repo, &["revert", "--no-edit", "HEAD"]);
859        let head = sh(&repo, &["rev-parse", "HEAD"]);
860        assert_eq!(landed(&repo, &start, &windows(&dir, "s-1", &start)).first(), Some(&head), "the revert is the session's");
861        let r = |c: &str| LinkRef { commit: c.into(), session: "s-7".into(), task: "fix it".into(), date: "2026-10-12".into(), strength: links::FIX.into() };
862        let linked = Excerpt {
863            kind: "reasoning".into(),
864            commit: head.clone(),
865            session: "s-0".into(),
866            fixed_by: vec![r("f1")],
867            fixes: vec![r("d0")],
868            ..Excerpt::default()
869        };
870        write_note(&repo, &linked).unwrap();
871        let route = route("private", collective::Share::Local);
872        let l = Landed { dir: &dir, session: "s-1", repo: &repo, store: &store, route: &route, commits: std::slice::from_ref(&head), start: None };
873        attach(&l, true).unwrap();
874        let e = note_of(&repo, &head).unwrap();
875        assert_eq!((e.session.as_str(), e.task.as_str()), ("s-1", "use a ttl cache"));
876        assert_eq!((e.fixed_by, e.fixes), (vec![r("f1")], vec![r("d0")]));
877        let _ = std::fs::remove_dir_all(repo.parent().unwrap());
878    }
879
880    /// `restore` writes the excerpts the store holds for the asked commits as notes, once.
881    #[test]
882    fn restore_writes_held_excerpts_as_notes() {
883        let (repo, _, store) = repo("restore");
884        let head = sh(&repo, &["rev-parse", "HEAD"]);
885        let e = Excerpt {
886            kind: "reasoning".into(),
887            commit: head.clone(),
888            repo: "repo".into(),
889            session: "s-4".into(),
890            task: "why the lock".into(),
891            proposal: "take it first".into(),
892            episodes: vec![],
893            ms: 1,
894            ..Excerpt::default()
895        };
896        let other = Excerpt { commit: "0".repeat(40), ..e.clone() };
897        append_reasoning(&store, &other).unwrap();
898        append_reasoning(&store, &e).unwrap();
899        assert_eq!(wanted(&repo, &[]).unwrap(), vec![head.clone()]);
900        assert_eq!(restore(&repo, &store, std::slice::from_ref(&head)).unwrap(), vec![head.clone()]);
901        assert!(sh(&repo, &["notes", "--ref=refs/notes/borg", "show", &head]).contains("take it first"));
902        assert!(restore(&repo, &store, std::slice::from_ref(&head)).unwrap().is_empty(), "an existing note is kept");
903        assert_eq!(wanted(&repo, &[]).unwrap(), vec![head.clone()], "noted commits are asked too: links to them come later");
904        let _ = std::fs::remove_dir_all(repo.parent().unwrap());
905    }
906
907    #[test]
908    fn restore_brings_the_fetched_links_into_both_notes() {
909        let (repo, _, store) = repo("relink");
910        let mut shas = Vec::new();
911        for msg in ["decide", "fix", "other"] {
912            sh(&repo, &["commit", "-q", "--allow-empty", "-m", msg]);
913            shas.push(sh(&repo, &["rev-parse", "HEAD"]));
914        }
915        let (dec, fix, other) = (&shas[0], &shas[1], &shas[2]);
916        let e = |c: &str, session: &str| Excerpt {
917            kind: "reasoning".into(),
918            commit: c.into(),
919            session: session.into(),
920            task: "take the lock".into(),
921            collective: "work".into(),
922            ..Excerpt::default()
923        };
924        // What `borg notes fetch` received: the decision's excerpt and its links, one of another collective.
925        append_reasoning(&store, &e(dec, "s-1")).unwrap();
926        for (to, collective) in [(fix, "work"), (other, "private")] {
927            let side = links::Side { session: "s-7".into(), task: "fix it".into(), date: "2026-10-12".into(), ..links::Side::default() };
928            let l = Link {
929                kind: links::KIND.into(),
930                commit: dec.clone(),
931                fix: to.clone(),
932                collective: collective.into(),
933                strength: links::FIX.into(),
934                fixed: side,
935                ..Link::default()
936            };
937            learn::append_jsonl(&store.join("reasoning.jsonl"), &l).unwrap();
938        }
939        // The fix's note is there already (this machine wrote it); the decision's is not.
940        write_note(&repo, &e(fix, "s-7")).unwrap();
941        write_note(&repo, &e(other, "s-8")).unwrap();
942        assert_eq!(restore(&repo, &store, std::slice::from_ref(dec)).unwrap(), [dec.clone(), fix.clone()]);
943        let decision = note_of(&repo, dec).unwrap();
944        assert_eq!((decision.fixed_by.len(), decision.fixed_by[0].commit.as_str(), decision.fixed_by[0].task.as_str()), (1, fix.as_str(), "fix it"));
945        assert_eq!(note_of(&repo, fix).unwrap().fixes[0].commit, *dec);
946        assert!(note_of(&repo, other).unwrap().fixes.is_empty(), "a record of another collective changes no note here");
947        assert!(restore(&repo, &store, std::slice::from_ref(dec)).unwrap().is_empty(), "once");
948        let _ = std::fs::remove_dir_all(repo.parent().unwrap());
949    }
950}