Skip to main content

rto_graph/
sync.rs

1//! The incremental, content-addressed sync engine.
2//!
3//! `sync` brings a [`Store`] into agreement with the repository's `HEAD` tree.
4//! Extraction is the expensive part and is content-addressed by blob id, so only
5//! blobs whose content changed are re-extracted; the rest load from the
6//! [`ObjectCache`]. If the tree id is unchanged since the last sync, it is a
7//! no-op. The graph itself is reassembled from the (cached) per-blob fact sets
8//! and rebuilt in a single transaction — a deliberately simple DB-write model
9//! for this stage; incremental DB updates can come later.
10
11use std::cell::Cell;
12use std::collections::{BTreeMap, BTreeSet, HashSet};
13
14use crate::cache::{CacheError, ObjectCache, ObjectSweep};
15use crate::extract::Extractor;
16use crate::git::{GitError, Repo};
17use crate::store::StoreError;
18use crate::{Edge, EdgeKind, FactSet, Node, NodeKind, Provenance, Store};
19
20/// Errors raised while syncing.
21#[derive(Debug, thiserror::Error)]
22pub enum SyncError {
23    /// A store operation failed.
24    #[error(transparent)]
25    Store(#[from] StoreError),
26    /// A cache operation failed.
27    #[error(transparent)]
28    Cache(#[from] CacheError),
29    /// A git operation failed.
30    #[error(transparent)]
31    Git(#[from] GitError),
32    /// Reading a working-tree file failed (dirty overlay).
33    #[error("worktree io error: {0}")]
34    Io(#[from] std::io::Error),
35}
36
37/// A summary of the work a [`sync`] performed.
38#[derive(Debug, Clone, Default, serde::Serialize)]
39pub struct SyncReport {
40    /// Hex id of the synced `HEAD` tree.
41    pub tree: String,
42    /// Whether the tree was unchanged and nothing was done.
43    pub no_op: bool,
44    /// Source files reflected in the graph — one `File` node per extracted blob.
45    /// Derived from the assembled graph (not the raw tree walk) so full and
46    /// incremental syncs report the same total for the same tree.
47    pub blobs_total: usize,
48    /// Blobs that were extracted (cache misses).
49    pub blobs_extracted: usize,
50    /// Blobs served from the cache (cache hits).
51    pub blobs_cached: usize,
52    /// Working-tree files whose uncommitted content overrode the committed blob
53    /// (the dirty overlay); always zero for a committed-only [`sync`].
54    pub blobs_dirty: usize,
55    /// Nodes in the store after syncing.
56    pub nodes: u64,
57    /// Edges in the store after syncing.
58    pub edges: u64,
59    /// The working tree this graph previously described, when it was a
60    /// **different** one and the sync therefore rebuilt from scratch rather than
61    /// trusting the recorded state (issue #330).
62    ///
63    /// `None` on every ordinary sync. `Some(path)` is the loud half of the
64    /// guarantee: the answer was corrected rather than served, and the caller can
65    /// say *which* tree the store had been holding, so a stale store is never a
66    /// silent wrong answer nor an unexplained slow one.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub rebuilt_from_foreign_worktree: Option<String>,
69}
70
71/// A stable identity for the working tree a graph is assembled from: the
72/// working-tree root, or the git dir for a bare repository.
73///
74/// The *path* is used rather than an opaque id because its whole job is to appear
75/// in a message naming the tree the store actually holds — an id the reader
76/// cannot act on would defeat the point. Linked worktrees have distinct roots, so
77/// this separates them; a plain branch switch within one tree does not change it,
78/// which is correct (the tree is the same, its content moved).
79#[must_use]
80pub fn worktree_id(repo: &Repo) -> String {
81    repo.workdir()
82        .unwrap_or_else(|| repo.git_dir())
83        .to_string_lossy()
84        .into_owned()
85}
86
87/// Decide whether `store`'s recorded sync state may be trusted for *this* tree.
88///
89/// Returns `Some(previous)` when the store was last assembled from a **different**
90/// working tree: its tree id, dirty-set hash and extraction env all describe
91/// someone else's tree, so no fast path may consult them and the caller must
92/// rebuild in full. `None` when the store belongs here or has never been stamped
93/// (unknown is adopted, not rebuilt — see [`Store::synced_worktree`]).
94///
95/// Rebuilding is cheap relative to being wrong: the object cache is shared across
96/// worktrees and already warm, so the re-extraction mostly hits it.
97fn foreign_worktree(store: &Store, repo: &Repo) -> Result<Option<String>, SyncError> {
98    let here = worktree_id(repo);
99    Ok(store.synced_worktree()?.filter(|prior| *prior != here))
100}
101
102/// Sync `store` to the repository's `HEAD` tree, extracting changed blobs with
103/// `extractor` and caching results in `cache`.
104///
105/// # Errors
106/// Returns a [`SyncError`] if git access, extraction caching, or the store
107/// rebuild fails.
108pub fn sync(
109    store: &mut Store,
110    repo: &Repo,
111    cache: &ObjectCache,
112    extractor: &dyn Extractor,
113) -> Result<SyncReport, SyncError> {
114    let tree = repo.head_tree_id()?;
115
116    // The extraction *identity*: the extractor code version (`EXTRACT_VERSION`,
117    // bumped when extraction output changes) plus its environment (installed image
118    // models + ingestion toggles). Both change what an unchanged file extracts to,
119    // and both are folded into the content-cache key — so this mirrors that key.
120    // Recorded with the tree so the next sync can tell whether reusing the stored
121    // facts (the incremental path) is sound; a binary upgrade that bumps the
122    // version, or a model change, invalidates it and forces a full re-extraction.
123    let env = format!(
124        "v{}-e{:016x}",
125        crate::extract::EXTRACT_VERSION,
126        extractor.env_tag()
127    );
128
129    // Nothing to do only when **both** the tree and the extraction identity are
130    // unchanged.
131    //
132    // The identity half is load-bearing, and its absence was a real hole: an
133    // `EXTRACT_VERSION` bump is supposed to guarantee that no user is served the
134    // previous version's facts, but a store already synced at the current `HEAD`
135    // returned `no_op` here before the identity was ever computed — so the new
136    // binary's facts appeared only once `HEAD` next moved. Enabling a feature that
137    // changes extraction output (`audio-metadata`, `pdf-text`, `image-ocr`) on a
138    // quiet repository therefore looked like it had done nothing at all. Every
139    // *other* consumer of the identity — the content-cache key, the incremental
140    // path below — already agreed on it; this one had simply never been asked.
141    //
142    // A store with no recorded identity (`None`) does not match, which is the safe
143    // direction: it re-extracts once and records one.
144    // …and only when the recorded state describes *this* working tree. A store
145    // assembled from another tree has a tree id, dirty hash and env that are all
146    // someone else's, so neither the no-op below nor the incremental diff may
147    // consult them: that is how a stale store reports "up to date" while holding
148    // a graph nobody is looking at (issue #330).
149    let foreign = foreign_worktree(store, repo)?;
150
151    if foreign.is_none()
152        && store.sync_state()?.as_deref() == Some(tree.as_str())
153        && store.sync_env()?.as_deref() == Some(env.as_str())
154    {
155        return Ok(SyncReport {
156            no_op: true,
157            nodes: store.node_count()?,
158            edges: store.edge_count()?,
159            tree,
160            ..SyncReport::default()
161        });
162    }
163
164    // Fast path: if the last sync was a committed one at a known tree with the
165    // same extraction identity, update only the paths that changed. Falls back to
166    // a full re-extraction on any doubt (no prior tree, identity changed, an
167    // unavailable diff, or a tree that is not ours).
168    if foreign.is_none()
169        && let Some(report) = try_incremental(store, repo, cache, extractor, &tree, &env)?
170    {
171        return Ok(report);
172    }
173
174    let committed = extract_committed(repo, cache, extractor)?;
175    let mut assembled = flatten(committed.by_path);
176    resolve_calls(&mut assembled);
177    append_submodule_nodes(repo.submodules()?, &mut assembled);
178    let total = file_count(&assembled);
179    store.reconcile(&assembled, Some(&tree))?;
180    store.set_sync_env(&env)?;
181    store.set_synced_worktree(&worktree_id(repo))?;
182
183    Ok(SyncReport {
184        no_op: false,
185        blobs_total: total,
186        blobs_extracted: committed.extracted,
187        blobs_cached: committed.cached,
188        blobs_dirty: 0,
189        nodes: store.node_count()?,
190        edges: store.edge_count()?,
191        tree,
192        rebuilt_from_foreign_worktree: foreign,
193    })
194}
195
196/// Attempt an incremental committed sync from the last-synced tree to `head_tree`.
197/// Returns `Ok(Some(report))` when it ran, `Ok(None)` when the fast path is not
198/// eligible (the caller then does a full sync).
199///
200/// It is sound because it produces the exact same **derived-only** graph a full
201/// sync would: it reconstructs the derived subgraph from the store (identified by
202/// the `Derived` provenance tag — unchanged paths' facts are a deterministic
203/// function of their unchanged blob content, so they equal a fresh extraction),
204/// drops the changed/deleted paths, extracts only the changed blobs, re-resolves
205/// cross-file `calls` globally, and feeds the result to the same [`Store::reconcile`]
206/// the full path uses. `check`/`reapply_imports` re-layer the authored/import
207/// facts afterward exactly as before — this only accelerates the derived layer.
208fn try_incremental(
209    store: &mut Store,
210    repo: &Repo,
211    cache: &ObjectCache,
212    extractor: &dyn Extractor,
213    head_tree: &str,
214    env: &str,
215) -> Result<Option<SyncReport>, SyncError> {
216    // Eligibility: a prior committed tree (a plain oid — worktree/index states
217    // carry a `:`-delimited marker), extracted under the same environment.
218    let Some(prior_tree) = store.sync_state()? else {
219        return Ok(None);
220    };
221    if prior_tree.contains(':') || store.sync_env()?.as_deref() != Some(env) {
222        return Ok(None);
223    }
224    // The prior tree object may have been pruned (gc); on any diff failure, fall
225    // back to the full path rather than guessing.
226    let Ok(diff) = repo.diff_trees(&prior_tree, head_tree) else {
227        return Ok(None);
228    };
229
230    // Reconstruct the derived subgraph from the store: every derived node, and
231    // every derived edge except `calls` (globally re-derived below from the full
232    // function set, since a changed file can flip name-resolution elsewhere).
233    let mut nodes: Vec<Node> = store.nodes_by_provenance(Provenance::Derived)?;
234    let mut edges: Vec<Edge> = store
235        .edges_by_provenance(Provenance::Derived)?
236        .into_iter()
237        .filter(|e| e.kind != EdgeKind::Calls)
238        .collect();
239
240    // Drop the changed and deleted paths' derived facts (their nodes, and any edge
241    // incident to them — per-blob derived edges are intra-file, so this is exact).
242    let touched: BTreeSet<&str> = diff
243        .changed
244        .iter()
245        .map(|b| b.path.as_str())
246        .chain(diff.deleted.iter().map(String::as_str))
247        .collect();
248    let dropped: HashSet<String> = nodes
249        .iter()
250        .filter(|n| n.path.as_deref().is_some_and(|p| touched.contains(p)))
251        .map(|n| n.key.clone())
252        .collect();
253    nodes.retain(|n| !dropped.contains(&n.key));
254    edges.retain(|e| !dropped.contains(&e.src) && !dropped.contains(&e.dst));
255
256    // Extract the changed blobs (cache-aware) and add their derived facts.
257    let env_tag = extractor.env_tag();
258    let mut extracted = 0usize;
259    let mut cached = 0usize;
260    for blob in &diff.changed {
261        let key = cache_key(&blob.path, &blob.oid, env_tag);
262        let facts = if let Some(facts) = cache.get(&key)? {
263            cached += 1;
264            facts
265        } else {
266            let bytes = repo.read_blob(&blob.oid)?;
267            let facts = extractor.extract(&blob.path, &blob.oid, &bytes);
268            cache.put(&key, &facts)?;
269            extracted += 1;
270            facts
271        };
272        nodes.extend(facts.nodes);
273        edges.extend(facts.edges);
274    }
275
276    // Prune orphaned import-target nodes — a path-less derived node (e.g.
277    // `import:rust:foo`) that no surviving edge references. A full sync emits it
278    // only while some file imports it, so dropping the now-unreferenced ones keeps
279    // the two paths identical.
280    let referenced: HashSet<&str> = edges
281        .iter()
282        .flat_map(|e| [e.src.as_str(), e.dst.as_str()])
283        .collect();
284    nodes.retain(|n| n.path.is_some() || referenced.contains(n.key.as_str()));
285
286    // Global call resolution over the full (reconstructed + changed) function set,
287    // then reconcile to derived-only — identical to what the full path produces.
288    let mut assembled = FactSet { nodes, edges };
289    resolve_calls(&mut assembled);
290    append_submodule_nodes(repo.submodules()?, &mut assembled);
291    let total = file_count(&assembled);
292    store.reconcile(&assembled, Some(head_tree))?;
293    store.set_sync_env(env)?;
294    // The caller only reaches here for a store that is ours, but it may predate
295    // the stamp — record it, or this path would leave it unstamped forever.
296    store.set_synced_worktree(&worktree_id(repo))?;
297
298    Ok(Some(SyncReport {
299        no_op: false,
300        blobs_total: total,
301        blobs_extracted: extracted,
302        blobs_cached: cached,
303        blobs_dirty: 0,
304        nodes: store.node_count()?,
305        edges: store.edge_count()?,
306        tree: head_tree.to_owned(),
307        // Unreachable with a foreign store: the caller skips this path entirely.
308        rebuilt_from_foreign_worktree: None,
309    }))
310}
311
312/// Sync `store` to the working tree: the committed `HEAD` state with uncommitted
313/// working-tree changes overlaid on top (a pre-commit preview).
314///
315/// Committed blobs come from the content-addressed cache as in [`sync`]; then
316/// each tracked file whose working copy differs from its committed blob is
317/// re-extracted in memory (never cached, since dirty content is not a git
318/// object), deleted files are dropped, and brand-new **untracked** files (found
319/// via a gitignore-aware dirwalk, [`Repo::untracked_files`]) are overlaid in.
320/// The recorded sync state encodes the dirty set, so a later committed [`sync`]
321/// correctly supersedes the overlay.
322///
323/// # Errors
324/// Returns a [`SyncError`] if git access, extraction caching, working-tree I/O,
325/// or the store rebuild fails.
326pub fn sync_worktree(
327    store: &mut Store,
328    repo: &Repo,
329    cache: &ObjectCache,
330    extractor: &dyn Extractor,
331) -> Result<SyncReport, SyncError> {
332    let tree = repo.head_tree_id()?;
333    let committed = extract_committed(repo, cache, extractor)?;
334    let mut by_path = committed.by_path;
335
336    // Overlay uncommitted edits to tracked files. A file is dirty when its
337    // working-copy content hashes to a different git blob id than the committed
338    // one; identical content hashes identically, so clean files are skipped.
339    let mut dirty: BTreeSet<(String, String)> = BTreeSet::new();
340    if let Some(workdir) = repo.workdir() {
341        for blob in &committed.blobs {
342            match std::fs::read(workdir.join(&blob.path)) {
343                Ok(bytes) => {
344                    let woid = repo.blob_oid(&bytes)?;
345                    if woid != blob.oid {
346                        by_path.insert(
347                            blob.path.clone(),
348                            extractor.extract(&blob.path, &woid, &bytes),
349                        );
350                        dirty.insert((blob.path.clone(), woid));
351                    }
352                }
353                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
354                    by_path.remove(&blob.path);
355                    dirty.insert((blob.path.clone(), "\0deleted".to_owned()));
356                }
357                Err(e) => return Err(e.into()),
358            }
359        }
360
361        // Overlay brand-new files: those `HEAD` does not have and git would
362        // nonetheless carry — untracked-but-not-ignored, plus anything staged.
363        //
364        // Two sources, and it needs both (#636). `untracked_files` classifies the
365        // working tree **against the index**, so it stops reporting a file the
366        // moment it is `git add`-ed; the committed blob list above comes from the
367        // `HEAD` tree, where a new file does not exist either. A **staged
368        // addition** is therefore in neither, and used to fall straight through
369        // this overlay — so `git add`, an action that moves a file *closer* to
370        // committed, deleted its node from the graph and dropped the
371        // `+N uncommitted` marker at the exact moment the tree differed most from
372        // `HEAD`. Adding the index entries that `HEAD` lacks closes the gap.
373        //
374        // Content still comes from **disk**, not from the staged blob: this is the
375        // worktree source, and a file edited after being staged must be read as it
376        // now stands.
377        //
378        // `.gitignore` is still honoured, and the union states *how*: an ignored
379        // file is absent from the dirwalk, so it enters only by being in the
380        // index — which takes a deliberate `git add -f`. That is the right
381        // outcome rather than a leak, because force-adding overrides the ignore
382        // and the file will be committed; the graph would see it a moment later
383        // anyway.
384        //
385        // They count as dirty (so the preview re-runs when they change) and add to
386        // the blob total (they are genuinely new blobs, not edits of existing ones).
387        let head_paths: BTreeSet<&str> = committed.blobs.iter().map(|b| b.path.as_str()).collect();
388        for path in repo.added_since_head(&head_paths)? {
389            match std::fs::read(workdir.join(&path)) {
390                Ok(bytes) => {
391                    let woid = repo.blob_oid(&bytes)?;
392                    by_path.insert(path.clone(), extractor.extract(&path, &woid, &bytes));
393                    dirty.insert((path, woid));
394                }
395                // Raced away between the walk and the read — nothing to add.
396                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
397                Err(e) => return Err(e.into()),
398            }
399        }
400    }
401
402    // The blob total is the file count of the *overlaid* graph — committed files,
403    // minus working-tree deletions, plus untracked additions — not the committed
404    // baseline, so it stays consistent whether files were added or removed.
405    let total = by_path.len();
406
407    // Encode the dirty set into the sync state so repeated identical previews
408    // no-op, but any committed change (which alters the plain tree id) does not.
409    let state = if dirty.is_empty() {
410        tree.clone()
411    } else {
412        let mut buf = String::new();
413        for (path, marker) in &dirty {
414            buf.push_str(path);
415            buf.push('\0');
416            buf.push_str(marker);
417            buf.push('\n');
418        }
419        format!("{tree}:dirty:{:016x}", fnv1a64(buf.as_bytes()))
420    };
421    let dirty_count = dirty.len();
422
423    // A dirty-set hash computed for another tree says nothing about this one, so
424    // a foreign store may never no-op here (issue #330).
425    let foreign = foreign_worktree(store, repo)?;
426    if foreign.is_none() && store.sync_state()?.as_deref() == Some(state.as_str()) {
427        return Ok(SyncReport {
428            no_op: true,
429            blobs_total: total,
430            blobs_dirty: dirty_count,
431            nodes: store.node_count()?,
432            edges: store.edge_count()?,
433            tree,
434            ..SyncReport::default()
435        });
436    }
437
438    let mut assembled = flatten(by_path);
439    resolve_calls(&mut assembled);
440    append_submodule_nodes(repo.submodules()?, &mut assembled);
441    store.reconcile(&assembled, Some(&state))?;
442    store.set_synced_worktree(&worktree_id(repo))?;
443
444    Ok(SyncReport {
445        no_op: false,
446        blobs_total: total,
447        blobs_extracted: committed.extracted,
448        blobs_cached: committed.cached,
449        blobs_dirty: dirty_count,
450        nodes: store.node_count()?,
451        edges: store.edge_count()?,
452        tree,
453        rebuilt_from_foreign_worktree: foreign,
454    })
455}
456
457/// Sync `store` to the **git index** — the staged tree that a commit would
458/// record. Unlike [`sync_worktree`] (files on disk) this reads each staged blob
459/// by its index object id, so it validates *exactly what is about to be
460/// committed* (partially-staged changes and all). New staged files are included;
461/// unstaged working-tree edits are not. Backs the index-aware pre-commit gate.
462///
463/// # Errors
464/// Returns a [`SyncError`] if git access, extraction caching, or the store
465/// reconcile fails.
466pub fn sync_index(
467    store: &mut Store,
468    repo: &Repo,
469    cache: &ObjectCache,
470    extractor: &dyn Extractor,
471) -> Result<SyncReport, SyncError> {
472    let staged = repo.index_files()?;
473    // A stable state id over the staged (path, oid) set, in its own `index:`
474    // namespace so it never collides with a committed tree id or a worktree dirty
475    // marker — repeated identical index syncs then no-op, while any staged change
476    // does not.
477    let mut buf = String::new();
478    for blob in &staged {
479        buf.push_str(&blob.path);
480        buf.push('\0');
481        buf.push_str(&blob.oid);
482        buf.push('\n');
483    }
484    let state = format!("index:{:016x}", fnv1a64(buf.as_bytes()));
485
486    // An index hash from another tree describes another index (issue #330).
487    let foreign = foreign_worktree(store, repo)?;
488    if foreign.is_none() && store.sync_state()?.as_deref() == Some(state.as_str()) {
489        return Ok(SyncReport {
490            no_op: true,
491            blobs_total: staged.len(),
492            nodes: store.node_count()?,
493            edges: store.edge_count()?,
494            tree: state,
495            ..SyncReport::default()
496        });
497    }
498
499    let extracted = extract_blobs(repo, cache, extractor, staged)?;
500    let total = extracted.by_path.len();
501    let mut assembled = flatten(extracted.by_path);
502    resolve_calls(&mut assembled);
503    // Index mode is "exactly what a commit would record", so submodule pins come
504    // from the *staged* gitlinks, not `HEAD` — a staged bump is reflected.
505    append_submodule_nodes(repo.index_submodules()?, &mut assembled);
506    store.reconcile(&assembled, Some(&state))?;
507    store.set_synced_worktree(&worktree_id(repo))?;
508
509    Ok(SyncReport {
510        no_op: false,
511        blobs_total: total,
512        blobs_extracted: extracted.extracted,
513        blobs_cached: extracted.cached,
514        blobs_dirty: 0,
515        nodes: store.node_count()?,
516        edges: store.edge_count()?,
517        tree: state,
518        rebuilt_from_foreign_worktree: foreign,
519    })
520}
521
522/// Extract a repo's **derived graph at an arbitrary commit/tree `rev`** into
523/// `store`, replacing its contents — the same content-addressed extraction as
524/// [`sync`], but for a historical point rather than `HEAD`. Because extraction is
525/// keyed by `(path, blob oid, env)`, every blob unchanged versus another synced
526/// point is a cache hit, so resolving an older version only re-does what differs.
527///
528/// This backs **version-pin resolution** (ADR-0009 step 8): to resolve a spoke's
529/// cross-repo reference against the hub *version it deploys* (a submodule sha,
530/// an image tag → commit), extract the hub at that `rev` into an ephemeral store
531/// and resolve there. It populates the derived layer only (config keys, symbols,
532/// calls); authored/import layers are not re-applied, since this is a read-only
533/// resolution snapshot. No sync-state is recorded (`tree` carries `rev` for the
534/// report only).
535///
536/// # Errors
537/// Returns [`SyncError`] on git access, extraction caching, or store failure.
538pub fn sync_tree(
539    store: &mut Store,
540    repo: &Repo,
541    cache: &ObjectCache,
542    extractor: &dyn Extractor,
543    rev: &str,
544) -> Result<SyncReport, SyncError> {
545    let extracted = extract_blobs(repo, cache, extractor, repo.blobs_at(rev)?)?;
546    let mut assembled = flatten(extracted.by_path);
547    resolve_calls(&mut assembled);
548    append_submodule_nodes(repo.submodules_at(rev)?, &mut assembled);
549    let total = file_count(&assembled);
550    store.rebuild(&assembled, None)?;
551    Ok(SyncReport {
552        no_op: false,
553        blobs_total: total,
554        blobs_extracted: extracted.extracted,
555        blobs_cached: extracted.cached,
556        blobs_dirty: 0,
557        nodes: store.node_count()?,
558        edges: store.edge_count()?,
559        tree: rev.to_owned(),
560        // A historical-rev store deliberately records no synced state at all
561        // (`rebuild(.., None)` clears the row), so it is stamped with no tree
562        // either — it is a scratch view of a commit, not of a working tree.
563        rebuilt_from_foreign_worktree: None,
564    })
565}
566
567/// The committed fact sets for the `HEAD` tree, one per path, plus the blob list
568/// (for overlay comparison) and cache-hit/miss counts.
569struct Committed {
570    blobs: Vec<crate::BlobRef>,
571    by_path: BTreeMap<String, FactSet>,
572    extracted: usize,
573    cached: usize,
574}
575
576/// Extract (or load from cache) the fact set for every blob in the `HEAD` tree.
577fn extract_committed(
578    repo: &Repo,
579    cache: &ObjectCache,
580    extractor: &dyn Extractor,
581) -> Result<Committed, SyncError> {
582    extract_blobs(repo, cache, extractor, repo.walk_blobs()?)
583}
584
585/// Extract (or load from cache) the fact set for each blob in `blobs` — the
586/// shared core of [`extract_committed`] and [`sync_index`], differing only in
587/// which tree the blob list comes from (`HEAD` vs the git index).
588fn extract_blobs(
589    repo: &Repo,
590    cache: &ObjectCache,
591    extractor: &dyn Extractor,
592    blobs: Vec<crate::BlobRef>,
593) -> Result<Committed, SyncError> {
594    let mut by_path = BTreeMap::new();
595    let mut extracted = 0usize;
596    let mut cached = 0usize;
597
598    // Extraction output depends on runtime state beyond (path, bytes): which
599    // image models are installed, and the extractor's ingestion toggles. The
600    // extractor folds both into a single tag for the cache key. Computed once
601    // per sync.
602    let env = extractor.env_tag();
603
604    for blob in &blobs {
605        // Extraction is a function of (path, blob bytes) and — with `image-ocr`
606        // — the OCR model environment (`env`), never blob id alone: node keys are
607        // path-scoped (e.g. `file:<path>`), so the same blob content at two
608        // different paths yields different facts. Key the cache by (path, oid,
609        // env) so duplicate-content files (e.g. empty files, which git dedupes to
610        // one oid) never collide, the same path+oid in another branch/worktree
611        // still hits, and installing/upgrading OCR models re-extracts images.
612        let key = cache_key(&blob.path, &blob.oid, env);
613        let facts = if let Some(facts) = cache.get(&key)? {
614            cached += 1;
615            facts
616        } else {
617            let bytes = repo.read_blob(&blob.oid)?;
618            let facts = extractor.extract(&blob.path, &blob.oid, &bytes);
619            cache.put(&key, &facts)?;
620            extracted += 1;
621            facts
622        };
623        by_path.insert(blob.path.clone(), facts);
624    }
625
626    Ok(Committed {
627        blobs,
628        by_path,
629        extracted,
630        cached,
631    })
632}
633
634/// Concatenate per-path fact sets into one assembled fact set.
635fn flatten(by_path: BTreeMap<String, FactSet>) -> FactSet {
636    let mut assembled = FactSet::new();
637    for facts in by_path.into_values() {
638        assembled.nodes.extend(facts.nodes);
639        assembled.edges.extend(facts.edges);
640    }
641    assembled
642}
643
644/// The `NodeKind::Other` token for a submodule-pin node (`submodule:<path>`).
645pub(crate) const SUBMODULE_KIND: &str = "submodule";
646
647/// Append the given submodule-pin nodes to `assembled`, replacing any already
648/// present. `subs` is the caller's source-appropriate list — `repo.submodules()`
649/// (the `HEAD` tree) for committed/worktree syncs, `repo.index_submodules()` (the
650/// staged gitlinks) for the index-aware pre-commit gate. A submodule pin is a
651/// **tree-level** derived fact (a gitlink + its `.gitmodules` URL, ADR-0009), not
652/// a per-blob one, so it is recomputed on every sync rather than cached. Removing
653/// any existing submodule nodes first makes the
654/// incremental path — which reconstructs derived nodes from the store — produce
655/// exactly the full sync's result: an unchanged pin re-adds identically, a bumped
656/// pin's new sha wins, and a removed submodule leaves none behind. The nodes carry
657/// `path = .gitmodules` (so a `.gitmodules` deletion drops them) and stand alone
658/// (no edges — nothing in the graph is their guaranteed endpoint).
659fn append_submodule_nodes(subs: Vec<crate::Submodule>, assembled: &mut FactSet) {
660    let kind = NodeKind::Other(SUBMODULE_KIND.to_owned());
661    assembled.nodes.retain(|n| n.kind != kind);
662    for sm in subs {
663        let key = format!("submodule:{}", sm.path);
664        let mut node = Node::new(key, kind.clone(), sm.path.clone());
665        node.path = Some(".gitmodules".to_owned());
666        node.provenance = Provenance::Derived;
667        node.meta = serde_json::json!({ "path": sm.path, "url": sm.url, "sha": sm.sha });
668        assembled.nodes.push(node);
669    }
670}
671
672/// The number of source files reflected in an assembled fact set (one `File`
673/// node per extracted blob). Both the full and incremental sync paths derive
674/// `SyncReport::blobs_total` from the *assembled graph* this way — not from the
675/// raw blob list — so the two paths report the same total for the same tree (the
676/// graphs are identical; see the equivalence test).
677fn file_count(facts: &FactSet) -> usize {
678    facts
679        .nodes
680        .iter()
681        .filter(|n| n.kind == NodeKind::File)
682        .count()
683}
684
685/// Resolve the per-function call records (`meta.calls`) accumulated during
686/// extraction into `calls` edges, now that every file's symbols are present.
687///
688/// Resolution is deliberately conservative — it links a call only when the target
689/// is **unambiguous** — but scope-aware: a callee descriptor may carry the
690/// immediate qualifier the call site provided (`b::foo`, `Type::assoc`,
691/// `Self::method`; see [`crate::extract`]). A call resolves when either
692///
693/// 1. its simple name is unique across the whole tree (the base case), or
694/// 2. its name is ambiguous but a qualifier picks out **exactly one** matching
695///    function — the one whose immediate scope segment equals that qualifier
696///    (with `Self` bound to the caller's own impl type).
697///
698/// This never links a name it could not before (it is a strict superset), and it
699/// still refuses to guess when a qualifier leaves more than one candidate. Runs at
700/// assembly time — not per blob — since a single blob cannot see other files.
701fn resolve_calls(facts: &mut FactSet) {
702    // Simple function name → the keys of functions with that name.
703    let mut by_name: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
704    for n in &facts.nodes {
705        if n.kind == NodeKind::Fn {
706            by_name
707                .entry(n.name.as_str())
708                .or_default()
709                .push(n.key.as_str());
710        }
711    }
712
713    // Collect (caller, callee) pairs; BTreeSet dedupes and orders them.
714    let mut resolved: BTreeSet<(String, String)> = BTreeSet::new();
715    for n in &facts.nodes {
716        if n.kind != NodeKind::Fn {
717            continue;
718        }
719        let Some(calls) = n.meta.get("calls").and_then(|v| v.as_array()) else {
720            continue;
721        };
722        // The caller's own type (for binding `Self::` calls) is the scope segment
723        // immediately before its name in its key, if it is a method.
724        let caller_self = self_type_of(&n.key);
725        for descriptor in calls.iter().filter_map(|v| v.as_str()) {
726            let (qualifier, name) = split_callee(descriptor);
727            let Some(candidates) = by_name.get(name) else {
728                continue;
729            };
730            let target = if candidates.len() == 1 {
731                // Unambiguous by simple name — the base case (unchanged behaviour).
732                Some(candidates[0])
733            } else if let Some(q) = qualifier {
734                // Ambiguous name; try the qualifier. `Self` binds to the caller's
735                // impl type — a free function has none, so such a call stays open.
736                let want = if q == "Self" { caller_self } else { Some(q) };
737                want.and_then(|want| unique_in_scope(candidates, want, name))
738            } else {
739                None
740            };
741            if let Some(dst) = target {
742                resolved.insert((n.key.clone(), dst.to_owned()));
743            }
744        }
745    }
746
747    for (src, dst) in resolved {
748        facts.edges.push(Edge::derived(src, dst, EdgeKind::Calls));
749    }
750}
751
752/// The qualified suffix of a symbol key (`sym:<lang>:<path>#<qualified>` →
753/// `<qualified>`), i.e. the scope-segment path within its file.
754fn qualified_suffix(key: &str) -> &str {
755    key.rsplit_once('#').map_or(key, |(_, q)| q)
756}
757
758/// The caller's own type for binding a `Self::` call: the scope segment
759/// immediately before the function's name in its key (`Type::method` → `Type`),
760/// or `None` for a free function (no enclosing type).
761fn self_type_of(key: &str) -> Option<&str> {
762    let mut segs = qualified_suffix(key).rsplit("::");
763    segs.next()?; // the function's own name
764    segs.next() // the enclosing scope segment, if any
765}
766
767/// The single candidate whose immediate scope segment is `want` (so its key ends
768/// with the `want::name` segment pair), or `None` when zero or several match —
769/// segment-aware so `T::m` matches `a::T::m` but never `XT::m`.
770fn unique_in_scope<'a>(candidates: &[&'a str], want: &str, name: &str) -> Option<&'a str> {
771    let mut hit = None;
772    for &key in candidates {
773        let mut segs = qualified_suffix(key).rsplit("::");
774        if segs.next() == Some(name) && segs.next() == Some(want) {
775            if hit.is_some() {
776                return None; // more than one match at this scope — refuse to guess
777            }
778            hit = Some(key);
779        }
780    }
781    hit
782}
783
784/// Split a `meta.calls` descriptor into its immediate qualifier and simple name:
785/// `b::foo` → `(Some("b"), "foo")`, `foo` → `(None, "foo")`.
786fn split_callee(descriptor: &str) -> (Option<&str>, &str) {
787    match descriptor.rsplit_once("::") {
788        Some((qualifier, name)) => (Some(qualifier), name),
789        None => (None, descriptor),
790    }
791}
792
793/// Content-addressed cache key for a blob at a given path: the blob oid (kept
794/// as the leading, well-distributed shard) suffixed with a stable 64-bit hash of
795/// the path, the [`crate::extract::EXTRACT_VERSION`], and the extractor
796/// environment tag `env` (the installed media-model — OCR + vision + audio —
797/// identity; `0` when no media model is active — see
798/// [`crate::extract::media_env_tag`]). Sharing across branches/worktrees is
799/// preserved (same path+oid+version+env → same key) while duplicate content at
800/// distinct paths stays distinct; bumping the extractor version *or* changing the
801/// installed media models retires old entries so a re-extraction is forced.
802fn cache_key(path: &str, oid: &str, env: u64) -> String {
803    format!(
804        "{oid}-{:016x}-v{}-e{env:016x}",
805        fnv1a64(path.as_bytes()),
806        crate::extract::EXTRACT_VERSION,
807    )
808}
809
810/// How many superseded extractor generations [`sweep_superseded`] keeps behind
811/// the current one by default: **one**.
812///
813/// Not clutter, and not free — it is a trade against the one workflow this
814/// project actually has. Roteiro is developed *inside* the repository it indexes,
815/// so a branch that bumps [`crate::extract::EXTRACT_VERSION`] and the `main` it
816/// will merge into share one `.git/roteiro` (the cache is under the **common**
817/// git dir). With no retention, one maintenance pass on the branch deletes
818/// `main`'s whole live set, and every switch back pays a full cold extraction;
819/// keeping the previous generation makes that switch free. Rolling a release back
820/// one version gets the same protection as a side effect.
821///
822/// It is bounded, which is the part that matters: the complaint being answered
823/// (#387) is *unbounded* accumulation — four generations resident and counting —
824/// and the steady state here is two, whatever happens next.
825pub const DEFAULT_KEEP_GENERATIONS: u32 = 1;
826
827/// Delete the object-cache entries left behind by **superseded** extractor
828/// generations, keeping the current one and `keep_generations` behind it.
829///
830/// # Why a sweep and not a byte budget
831///
832/// Because a proof is available here and nowhere else. [`cache_key`] writes the
833/// extractor generation into every key, and that generation only ever moves
834/// forward, so an entry tagged with an older one *cannot be asked for* by any
835/// binary at or beyond the current generation — no bookkeeping, no recency, no
836/// guessing. A byte budget (the Stage 25 / `rto-llama` `ModelCache` precedent,
837/// ported to disk by [`crate::Store::sweep_agent_cache`]) would have had to
838/// invent an ordering over live entries and would then evict *reachable* ones by
839/// design: on a cache shared by every worktree that means one worktree silently
840/// paying for another's working set, and it would need a last-used column this
841/// store has no clock to fill (ADR-0013 §3). It buys a bound this does not give —
842/// the live set itself is unbounded, and a repository large enough for that to
843/// hurt still needs one. That is a second policy on top of this one, not an
844/// alternative to it, and nothing has yet measured a need for it.
845///
846/// # What "superseded" is allowed to mean
847///
848/// **Only the generation**, i.e. [`crate::extract::EXTRACT_BASE_VERSION`]. The
849/// other two things folded into a key are deliberately *not* eligible:
850///
851/// - The **feature namespace** ([`crate::extract::FEATURE_NAMESPACE_STRIDE`] and
852///   above). A default build and an `--all-features` build write different
853///   `EXTRACT_VERSION`s at the *same* generation, and both are live at once —
854///   `cargo test --workspace` and `cargo test --all-features` on one repository
855///   are exactly that. Sweeping on the whole version number would have each build
856///   delete the other's cache on sight, and the two would take turns
857///   re-extracting for ever. So the namespace is masked off, and every namespace
858///   at a kept generation is kept.
859/// - The **environment tag** (`-e…`: the installed media-model and ingestion
860///   identity). It is a hash — unordered, so no tag can be shown to supersede
861///   another, and several are legitimately live at once (a build without
862///   `image-ocr` tags `0`; a build with it and a model installed does not).
863///   Reclaiming those would need the ordering the paragraph above rejected. They
864///   are left alone, and the cost of that is stated rather than hidden: env churn
865///   *within* one generation is not reclaimed by this pass.
866///
867/// # Why this is safe while other worktrees are live
868///
869/// The rule reads only the key, never the repository — so it does not need to
870/// know what any other worktree has checked out, and cannot be wrong about it. A
871/// reachability rule phrased over *blob ids* would need exactly that knowledge,
872/// and would be the dangerous version of this function: an oid unreachable from
873/// one worktree's `HEAD` is routinely live in another's. This one never asks.
874///
875/// Its only cross-worktree effect is on a worktree running an **older** binary,
876/// which it can cost a re-extraction and nothing else — the cache is derived, so
877/// a miss is slow, never wrong. The asymmetry runs one way: an entry from a
878/// *newer* generation than the sweeper's is retained, because `generation >=
879/// oldest_kept` holds for anything ahead. Two binaries of different ages can
880/// therefore never take turns deleting each other's work.
881///
882/// # Errors
883/// Returns [`CacheError`] if the cache cannot be listed. See
884/// [`ObjectCache::sweep`] for what a failure to delete an individual entry does
885/// (it is counted, not raised).
886pub fn sweep_superseded(
887    cache: &ObjectCache,
888    keep_generations: u32,
889) -> Result<ReclaimReport, CacheError> {
890    let current = crate::extract::EXTRACT_BASE_VERSION;
891    let oldest_kept = current.saturating_sub(keep_generations);
892
893    // The predicate is the only thing that ever classifies an entry, and it runs
894    // exactly once per scanned entry — so tallying here is the one place the
895    // reason for a retention is known, and it costs nothing extra. Counting it
896    // afterwards would mean a second walk, and reconstructing it in the caller
897    // would mean a second copy of this rule.
898    let current_kept = Cell::new(0);
899    let recent_kept = Cell::new(0);
900    let ahead_kept = Cell::new(0);
901    let unrecognised_kept = Cell::new(0);
902    let tally = |counter: &Cell<usize>| counter.set(counter.get() + 1);
903
904    let sweep = cache.sweep(&|key| match key_generation(key) {
905        // Not a key this module writes — a foreign or future format. Unreadable
906        // is not the same as unreachable, and only one of the two may be deleted.
907        None => {
908            tally(&unrecognised_kept);
909            true
910        }
911        Some(generation) if generation > current => {
912            tally(&ahead_kept);
913            true
914        }
915        Some(generation) if generation == current => {
916            tally(&current_kept);
917            true
918        }
919        Some(generation) if generation >= oldest_kept => {
920            tally(&recent_kept);
921            true
922        }
923        Some(_) => false,
924    })?;
925
926    let report = ReclaimReport {
927        kept_current: current_kept.get(),
928        kept_recent: recent_kept.get(),
929        kept_ahead: ahead_kept.get(),
930        kept_unrecognised: unrecognised_kept.get(),
931        sweep,
932    };
933    debug_assert_eq!(
934        report.kept_total(),
935        report.sweep.retained,
936        "every retained entry is retained for exactly one of the four reasons",
937    );
938    Ok(report)
939}
940
941/// What one [`sweep_superseded`] pass did — and, for everything it kept, **why**.
942///
943/// The four `kept_*` counts exist because the retention rule keeps more than the
944/// obvious class, and a summary that named only the obvious one would describe an
945/// irreversible operation inaccurately. They partition [`ObjectSweep::retained`]:
946/// each retained entry falls into exactly one, and their sum is that total.
947#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize)]
948pub struct ReclaimReport {
949    /// The underlying pass: what was scanned, freed, and left on disk.
950    pub sweep: ObjectSweep,
951    /// Kept at **this build's own generation** — the live set, the thing a sweep
952    /// exists to not touch.
953    pub kept_current: usize,
954    /// Kept at an **older** generation still inside the `keep_generations`
955    /// window. Unreachable by this build; deliberate insurance for the binary a
956    /// generation behind that shares this cache (see
957    /// [`DEFAULT_KEEP_GENERATIONS`]).
958    pub kept_recent: usize,
959    /// Kept because it belongs to a generation **ahead** of this build — another
960    /// worktree, or a colleague, running a newer binary against the same shared
961    /// cache. Never swept, which is what stops two binaries of different ages
962    /// taking turns deleting each other's work.
963    pub kept_ahead: usize,
964    /// Kept because `key_generation` could not read a generation out of the key
965    /// at all. Doubt retains, always — but a non-zero count here is worth
966    /// investigating rather than absorbing into a total, because it is either a
967    /// format this build no longer writes or a bug in the parser, and both are
968    /// things a reader would want to know their cache is holding.
969    pub kept_unrecognised: usize,
970}
971
972impl ReclaimReport {
973    /// The four `kept_*` counts summed — equal to [`ObjectSweep::retained`].
974    #[must_use]
975    pub fn kept_total(&self) -> usize {
976        self.kept_current + self.kept_recent + self.kept_ahead + self.kept_unrecognised
977    }
978}
979
980/// The extractor **generation** encoded in a [`cache_key`] key, or `None` if the
981/// key does not carry one in the exact shape `cache_key` writes.
982///
983/// The parse is strict on purpose: this is the predicate a delete hangs off, so
984/// every doubt has to resolve to `None`, which retains. It therefore requires the
985/// whole `-v<digits>-e<16 hex digits>` tail, rejects a sign that `u32::from_str`
986/// would otherwise accept (`+12`), and rejects an environment tag of the wrong
987/// width — anything merely *shaped like* a key is left alone.
988fn key_generation(key: &str) -> Option<u32> {
989    let (head, env) = key.rsplit_once("-e")?;
990    if env.len() != 16 || !env.bytes().all(|b| b.is_ascii_hexdigit()) {
991        return None;
992    }
993    let (_, version) = head.rsplit_once("-v")?;
994    if version.is_empty() || !version.bytes().all(|b| b.is_ascii_digit()) {
995        return None;
996    }
997    // Mask off the feature namespace; what remains is the generation. Sound while
998    // the base stays below the stride, which `extract.rs` asserts at compile time.
999    Some(version.parse::<u32>().ok()? % crate::extract::FEATURE_NAMESPACE_STRIDE)
1000}
1001
1002/// FNV-1a (64-bit). Dependency-free and deterministic; used only to derive
1003/// cache filenames, so it needs no cryptographic properties.
1004fn fnv1a64(bytes: &[u8]) -> u64 {
1005    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
1006    for &b in bytes {
1007        hash ^= u64::from(b);
1008        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
1009    }
1010    hash
1011}
1012
1013#[cfg(test)]
1014mod tests {
1015    use super::{ObjectCache, cache_key, key_generation, resolve_calls};
1016    use crate::{EdgeKind, FactSet, Node, NodeKind};
1017
1018    fn fn_node(key: &str, name: &str, calls: &[&str]) -> Node {
1019        let mut n = Node::new(key, NodeKind::Fn, name);
1020        if !calls.is_empty() {
1021            n.meta = serde_json::json!({ "calls": calls });
1022        }
1023        n
1024    }
1025
1026    #[test]
1027    fn resolve_calls_links_unique_names_only() {
1028        let mut fs = FactSet::new()
1029            .with_node(fn_node(
1030                "sym:rust:a.rs#caller",
1031                "caller",
1032                &["target", "dup", "missing"],
1033            ))
1034            .with_node(fn_node("sym:rust:a.rs#target", "target", &[]))
1035            // Two functions named `dup` → ambiguous, must not be linked.
1036            .with_node(fn_node("sym:rust:a.rs#dup", "dup", &[]))
1037            .with_node(fn_node("sym:rust:b.rs#dup", "dup", &[]));
1038
1039        resolve_calls(&mut fs);
1040
1041        let calls: Vec<_> = fs
1042            .edges
1043            .iter()
1044            .filter(|e| e.kind == EdgeKind::Calls)
1045            .collect();
1046        assert_eq!(
1047            calls.len(),
1048            1,
1049            "only the unambiguous, known callee is linked"
1050        );
1051        assert_eq!(calls[0].src, "sym:rust:a.rs#caller");
1052        assert_eq!(calls[0].dst, "sym:rust:a.rs#target");
1053    }
1054
1055    #[test]
1056    fn cache_key_separates_paths_but_is_stable() {
1057        let oid = "abc123";
1058        // Same path + oid + env is stable across calls.
1059        assert_eq!(cache_key("src/a.rs", oid, 0), cache_key("src/a.rs", oid, 0));
1060        // Same blob content (oid) at two different paths must not collide.
1061        assert_ne!(cache_key("src/a.rs", oid, 0), cache_key("src/b.rs", oid, 0));
1062        // Different content at the same path differs too.
1063        assert_ne!(
1064            cache_key("src/a.rs", "aaa", 0),
1065            cache_key("src/a.rs", "bbb", 0)
1066        );
1067        // A different extractor environment (e.g. OCR models installed) differs,
1068        // so image facts are re-extracted when the models change.
1069        assert_ne!(
1070            cache_key("src/a.rs", oid, 0),
1071            cache_key("src/a.rs", oid, 42)
1072        );
1073        // Key stays sharded on the oid so the cache's 2-char shard is well spread.
1074        assert!(cache_key("src/a.rs", oid, 0).starts_with("abc123-"));
1075        // The extractor version is folded in, so a bump retires old entries.
1076        assert!(
1077            cache_key("src/a.rs", oid, 0)
1078                .contains(&format!("-v{}", crate::extract::EXTRACT_VERSION))
1079        );
1080    }
1081
1082    /// The sweep predicate's one input. The round trip is what makes the sweep
1083    /// safe: a key this module just wrote must decode to *this* generation, or a
1084    /// pass at the current version would delete its own live entries.
1085    #[test]
1086    fn key_generation_round_trips_the_key_this_module_writes() {
1087        let key = cache_key("src/a.rs", "abc123", 0);
1088        assert_eq!(
1089            key_generation(&key),
1090            Some(crate::extract::EXTRACT_BASE_VERSION),
1091            "a key written now decodes to the current generation: {key}",
1092        );
1093        // …and so does the same generation in another feature build's namespace,
1094        // which is the whole reason the namespace is masked off rather than
1095        // compared. Both are live at once on a machine that runs the default and
1096        // `--all-features` test suites over one repository.
1097        let base = crate::extract::EXTRACT_BASE_VERSION;
1098        for namespace in [100, 200, 300, 400, 500, 600, 700] {
1099            let other = format!(
1100                "abc123-0000000000000000-v{}-e0000000000000000",
1101                base + namespace
1102            );
1103            assert_eq!(
1104                key_generation(&other),
1105                Some(base),
1106                "namespace {namespace} is not a different generation",
1107            );
1108        }
1109    }
1110
1111    /// Every doubt resolves to `None`, and `None` retains. These are the strings
1112    /// that must *not* be read as a generation — each one would otherwise put a
1113    /// file nobody can identify in reach of a delete.
1114    #[test]
1115    fn key_generation_refuses_anything_it_did_not_write() {
1116        for not_a_key in [
1117            "",
1118            "abc123",                                                 // no tail at all
1119            "abc123-0000000000000000-v12",                            // no env tag
1120            "abc123-0000000000000000-e0000000000000000",              // no version tag
1121            "abc123-0000000000000000-v12-e00000000000000",            // env too short
1122            "abc123-0000000000000000-v12-e00000000000000000",         // env too long
1123            "abc123-0000000000000000-v12-egggggggggggggggg",          // env not hex
1124            "abc123-0000000000000000-v+12-e0000000000000000",         // `+12` parses as 12
1125            "abc123-0000000000000000-v-e0000000000000000",            // empty version
1126            "abc123-0000000000000000-v1 2-e0000000000000000",         // not all digits
1127            "abc123-0000000000000000-v99999999999-e0000000000000000", // overflows u32
1128        ] {
1129            assert_eq!(
1130                key_generation(not_a_key),
1131                None,
1132                "`{not_a_key}` must not be read as a generation",
1133            );
1134        }
1135    }
1136
1137    /// The sweep's contract, on a cache holding one entry per generation and
1138    /// namespace: the current generation survives in **every** namespace, the
1139    /// retained generations survive, older ones go, and a *newer* one — written
1140    /// by a binary ahead of this one sharing the same common git dir — is never
1141    /// touched, whatever the retention.
1142    #[test]
1143    fn sweep_superseded_keeps_current_future_and_kept_generations() {
1144        let base = crate::extract::EXTRACT_BASE_VERSION;
1145        let dir = std::env::temp_dir().join(format!("roteiro-gc-{}", std::process::id()));
1146        std::fs::remove_dir_all(&dir).ok();
1147        let cache = ObjectCache::open(&dir).expect("open");
1148
1149        let key = |version: u32| format!("abc123-0000000000000000-v{version}-e0000000000000000");
1150        let ancient = key(base - 2);
1151        let previous = key(base - 1);
1152        let current = key(base);
1153        let current_all_features = key(base + 700);
1154        let future = key(base + 1);
1155        let foreign = "not-a-roteiro-cache-key".to_owned();
1156        for k in [
1157            &ancient,
1158            &previous,
1159            &current,
1160            &current_all_features,
1161            &future,
1162            &foreign,
1163        ] {
1164            cache.put(k, &FactSet::new()).expect("put");
1165        }
1166
1167        // Keeping one generation back: only `base - 2` is unreachable.
1168        let swept =
1169            super::sweep_superseded(&cache, super::DEFAULT_KEEP_GENERATIONS).expect("sweep");
1170        assert_eq!(swept.sweep.removed, 1, "{swept:?}");
1171        // Retention is not one class, and the report says which. Two entries sit
1172        // at this generation (the two namespaces), one behind it, one ahead of
1173        // it, and one key that does not parse — each counted under its own
1174        // reason, because a summary that folded them together would describe an
1175        // irreversible operation inaccurately.
1176        assert_eq!(
1177            (
1178                swept.kept_current,
1179                swept.kept_recent,
1180                swept.kept_ahead,
1181                swept.kept_unrecognised,
1182            ),
1183            (2, 1, 1, 1),
1184            "{swept:?}",
1185        );
1186        assert_eq!(
1187            swept.kept_total(),
1188            swept.sweep.retained,
1189            "the four reasons must partition the retained total: {swept:?}",
1190        );
1191        assert!(!cache.contains(&ancient));
1192        for k in [
1193            &previous,
1194            &current,
1195            &current_all_features,
1196            &future,
1197            &foreign,
1198        ] {
1199            assert!(cache.contains(k), "`{k}` must survive a keep-1 sweep");
1200        }
1201
1202        // Keeping none: the previous generation goes too, and nothing else does.
1203        let swept = super::sweep_superseded(&cache, 0).expect("sweep");
1204        assert_eq!(swept.sweep.removed, 1, "{swept:?}");
1205        assert!(!cache.contains(&previous));
1206        for k in [&current, &current_all_features, &future, &foreign] {
1207            assert!(cache.contains(k), "`{k}` must survive a keep-0 sweep");
1208        }
1209
1210        // A repeat pass is a no-op: nothing reachable is ever swept "eventually".
1211        let swept = super::sweep_superseded(&cache, 0).expect("sweep");
1212        assert_eq!(swept.sweep.removed, 0, "{swept:?}");
1213        assert_eq!(swept.sweep.retained, 4, "{swept:?}");
1214
1215        std::fs::remove_dir_all(&dir).expect("cleanup");
1216    }
1217}