faculties 0.21.0

An office suite for AI agents: kanban, wiki, files, messaging, and a Lissajous-backed viewer — all persisted in a TribleSpace pile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
//! The Code faculty's Rust API: no argv, no transport.
//!
//! Every read here ends by reporting the revisions it read at. Absence is only
//! meaningful with its denominator, and a tool that says "absent" without
//! saying absent *from what* is how the fourth wrong absence claim gets made.

use crate::storage::{AcquiringReader, FacultySnapshot};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};

use anybytes::View;
use anyhow::{anyhow, bail, Context, Result};
use hifitime::Epoch;
use triblespace::core::blob::encodings::succinctarchive::Rank9AcceleratedSuccinctArchiveBlob;
use triblespace::core::collection::AttachedSnapshot;
use triblespace::core::query::TriblePattern;
use triblespace::core::repo::{BlobStoreGet, SnapshotSource};
use triblespace::core::collection::{CollectionSnapshotExt, CollectionStoreExt};
#[allow(unused_imports)]
use triblespace::prelude::blobencodings::RawBytes;
#[allow(unused_imports)]
use triblespace::prelude::blobencodings::UTF8String;
use triblespace::prelude::*;

use crate::code::{self, extract, git, ingest, TextHandle};
use crate::schemas::code::{ItemKind, Language, EXTRACTOR_RUST_SYN_V1_NAME};
use crate::storage::{FactArchive, Storage};

/// Largest file this build will hold in a unit's content blob.
///
/// A generated or vendored monster in a tracked path would otherwise put tens
/// of megabytes into the pile for a file nobody will ever read here. Skipping it
/// is reported, not silent.
const MAX_UNIT_BYTES: usize = 4 * 1024 * 1024;

/// Flush a repository's staged delta if it grows past this, so one enormous
/// repository does not hold a million facts resident before its single commit.
const MAX_STAGED_FACTS: usize = 400_000;

pub struct Code {
    storage: Storage,
}

// ── shared result shapes ────────────────────────────────────────────────

/// One scan, resolved to text.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ScanSummary {
    pub id: Id,
    pub repo: String,
    pub commit: String,
    pub observed_at: Epoch,
}

impl ScanSummary {
    /// `faculties@336a8765`, the way a footer names a revision.
    pub fn label(&self) -> String {
        let commit = if self.commit.starts_with("worktree:") {
            "worktree".to_owned()
        } else {
            self.commit.chars().take(8).collect()
        };
        format!("{}@{}", self.repo, commit)
    }
}

/// What every answer carries so it can be believed.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Provenance {
    pub scans: Vec<ScanSummary>,
    pub units: usize,
    pub placements: usize,
    pub extractor: String,
}

/// One located declaration, as a reader sees it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Hit {
    pub item: Id,
    pub unit: Id,
    pub repo: String,
    pub path: String,
    pub line: u64,
    pub end_line: u64,
    pub kind: Option<String>,
    pub visibility: Option<String>,
    pub name: Option<String>,
    pub signature: Option<String>,
    pub doc: Option<String>,
    pub scan: String,
}

impl Hit {
    pub fn location(&self) -> String {
        format!("{}/{}:{}", self.repo, self.path, self.line)
    }
}

/// An answer to a question about one identifier.
#[derive(Clone, Debug, Default)]
pub struct Answer {
    pub subject: String,
    pub hits: Vec<Hit>,
    /// Name-similar declarations. Explicitly NOT answers; printed below the
    /// verdict and labelled, because a list of near-misses presented as results
    /// is exactly how an absence gets misread as a presence.
    pub near: Vec<Hit>,
    pub caveat: Option<String>,
    pub followup: Option<String>,
    pub provenance: Provenance,
}

/// What one ingest run did to one repository.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct RepoIngest {
    pub repo: String,
    pub commit: String,
    pub files_seen: usize,
    pub files_parsed: usize,
    pub files_skipped: usize,
    pub parse_failures: usize,
    pub items: usize,
    pub commits: usize,
}

#[derive(Clone, Debug, Default)]
pub struct IngestReport {
    pub repos: Vec<RepoIngest>,
    pub dry_run: bool,
}

/// Per-repository counts.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct StatsRow {
    pub scan: String,
    pub units: usize,
    pub placements: usize,
    pub parse_failures: usize,
    pub kinds: Vec<(String, usize)>,
}

#[derive(Clone, Debug, Default)]
pub struct StatsReport {
    pub rows: Vec<StatsRow>,
    pub provenance: Provenance,
}

/// One item placed in more than one location.
#[derive(Clone, Debug)]
pub struct DuplicateGroup {
    pub item: Id,
    pub name: Option<String>,
    pub kind: Option<String>,
    pub lines: u64,
    pub places: Vec<Hit>,
}

#[derive(Clone, Debug, Default)]
pub struct DuplicateReport {
    pub groups: Vec<DuplicateGroup>,
    pub provenance: Provenance,
}

/// What `code show` found.
#[derive(Clone, Debug)]
pub struct ItemDetail {
    pub hit: Hit,
    pub places: Vec<Hit>,
    pub source: Option<String>,
    pub provenance: Provenance,
}

/// One commit a pickaxe search attributes a change to.
#[derive(Clone, Debug)]
pub struct BlameRow {
    pub repo: String,
    pub commit: String,
    pub date: String,
    pub subject: String,
    pub paths: Vec<String>,
}

#[derive(Clone, Debug, Default)]
pub struct BlameReport {
    pub identifier: String,
    pub rows: Vec<BlameRow>,
    pub searched: Vec<String>,
}

/// How a caller selected the revisions to answer at.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum Revision {
    /// The newest scan of each repository.
    #[default]
    Latest,
    /// Every scan of an uncommitted working tree.
    Worktree,
    /// `<repo>@<commit prefix>`, `<repo>@worktree`, or a bare commit prefix.
    Selector(String),
}

impl Revision {
    pub fn parse(raw: Option<&str>) -> Self {
        match raw.map(str::trim) {
            None | Some("") => Self::Latest,
            Some("worktree") => Self::Worktree,
            Some(selector) => Self::Selector(selector.to_owned()),
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct Filter {
    pub kind: Option<String>,
    pub repo: Option<String>,
    pub revision: Revision,
}

#[derive(Clone, Debug, Default)]
pub struct IngestOptions {
    /// Ingest one named revision from git objects instead of the working tree.
    pub commit: Option<String>,
    pub dry_run: bool,
}

// ── reading ─────────────────────────────────────────────────────────────

pub(crate) type CodeReader = AcquiringReader<FacultySnapshot>;
type Observed = AttachedSnapshot<CodeReader, Rank9AcceleratedSuccinctArchiveBlob>;

/// A per-invocation memo of blob text.
///
/// This is the blob reader's cache, not a model of the pile's relations: it
/// holds strings keyed by content handle for the length of one command and is
/// dropped with it. Nothing queries it, nothing joins against it, and it never
/// answers a question the pile was not asked.
pub(crate) struct Texts<'a> {
    reader: &'a CodeReader,
    seen: HashMap<[u8; 32], String>,
}

impl<'a> Texts<'a> {
    pub(crate) fn new(reader: &'a CodeReader) -> Self {
        Self {
            reader,
            seen: HashMap::new(),
        }
    }

    pub(crate) fn get(&mut self, handle: TextHandle) -> Result<String> {
        if let Some(text) = self.seen.get(&handle.raw) {
            return Ok(text.clone());
        }
        let value: View<str> = self
            .reader
            .get(handle)
            .context("read catalogued text from the pile")?;
        let text = value.to_string();
        self.seen.insert(handle.raw, text.clone());
        Ok(text)
    }

    /// The first value, when a relation this reader models happens to hold one.
    /// Absence is ordinary; several values are ordinary too, and neither is an
    /// error, because the schema language cannot express cardinality and
    /// re-imposing it in Rust would be inventing a constraint the substrate
    /// deliberately refuses.
    fn first(&mut self, handles: Vec<TextHandle>) -> Result<Option<String>> {
        let mut texts: Vec<String> = Vec::new();
        for handle in handles {
            texts.push(self.get(handle)?);
        }
        texts.sort();
        Ok(texts.into_iter().next())
    }
}

impl Code {
    pub fn new(pile: PathBuf, key: Option<PathBuf>) -> Self {
        Self::with_storage(Storage::new(pile, key))
    }

    pub fn with_storage(storage: Storage) -> Self {
        Self { storage }
    }

    pub(crate) fn observe(&self) -> Result<Observed> {
        self.storage.with_store(|store, signer, runtime| {
            let source = crate::collection_names::open_configured_acquiring(
                store, crate::schemas::code::DEFAULT_SCOPE_ID, signer.verifying_key(), runtime,
            )?;
            let (succinct, rank9) = crate::storage::fact_pair(store, source)?;
            runtime.block_on(async {
                crate::storage::tolerate_own_lag(store.maintain_attached(succinct, signer).await)?;
                crate::storage::tolerate_own_lag(store.maintain_attached(rank9, signer).await)?;
                Ok::<_, anyhow::Error>(())
            })?;
            AcquiringReader::new(store.snapshot()?, runtime.clone()).attached_acquiring(rank9)
                .context("attach Code facts from one frozen observation")
        })
    }

    pub(crate) fn with_operation<T>(&self, operation: impl FnOnce(&Self) -> Result<T>) -> Result<T> {
        self.storage.scope(|storage| operation(&Self::with_storage(storage.clone())))
    }

    // ── ingest ──────────────────────────────────────────────────────────

    /// Catalogue one or more git working copies.
    ///
    /// Git is the walker: ignore rules, submodules, `target*` and the
    /// tracked/untracked split all come from it rather than from a hand-rolled
    /// filesystem walk with an ignore list that would drift.
    pub fn ingest(&self, dirs: &[PathBuf], options: &IngestOptions) -> Result<IngestReport> {
        let mut plans = Vec::new();
        for dir in dirs {
            let dir = dir
                .canonicalize()
                .with_context(|| format!("resolve {}", dir.display()))?;
            if !git::is_repository(&dir) {
                bail!("{} is not a git working copy", dir.display());
            }
            plans.push(plan_repository(&dir, options)?);
        }

        let mut report = IngestReport {
            dry_run: options.dry_run,
            ..IngestReport::default()
        };
        if options.dry_run {
            for plan in &plans {
                report.repos.push(RepoIngest {
                    repo: plan.repo.clone(),
                    commit: plan.commit.clone(),
                    files_seen: plan.sources.len(),
                    files_skipped: plan.skipped,
                    ..RepoIngest::default()
                });
            }
            return Ok(report);
        }

        report.repos = self.storage.with_store(|store, signer, runtime| {
            let mut writer = ingest::CodeImportWriter::from_store(store, signer, runtime.clone())?;
            let mut rows = Vec::new();
            for plan in plans {
                rows.push(ingest_repository(&mut writer, plan)?);
            }
            // Publish whatever the last repository left staged.
            writer.commit_unit()?;
            Ok(rows)
        })?;
        Ok(report)
    }

    // ── questions ───────────────────────────────────────────────────────

    /// Where is `name` defined — and if nowhere, say so with a denominator.
    pub fn find(&self, name: &str, filter: &Filter) -> Result<Answer> {
        self.with_operation(|code| code.find_scoped(name, filter))
    }

    fn find_scoped(&self, name: &str, filter: &Filter) -> Result<Answer> {
        let observed = self.observe()?;
        let facts = crate::storage::acquire_attached_facts(&observed).context("read Code facts")?;
        let reader = observed.snapshot();
        let mut texts = Texts::new(reader);
        let scans = select_scans(&facts, &mut texts, filter)?;
        let provenance = provenance(&facts, &scans);

        let handle = code::text_handle(name);
        let mut hits = Vec::new();
        for scan in &scans {
            for located in code::definitions_in_scan(&facts, scan.id, handle) {
                hits.push(hit(&facts, &mut texts, scan, located)?);
            }
        }
        hits.retain(|hit| keeps(hit, filter));
        sort_hits(&mut hits);
        hits.dedup_by(|left, right| left.item == right.item && left.location() == right.location());

        let mut near = Vec::new();
        if hits.is_empty() {
            for candidate in near_miss_candidates(name) {
                let candidate_handle = code::text_handle(&candidate);
                for scan in &scans {
                    for located in code::definitions_in_scan(&facts, scan.id, candidate_handle) {
                        near.push(hit(&facts, &mut texts, scan, located)?);
                    }
                }
            }
            near.retain(|hit| keeps(hit, filter));
            sort_hits(&mut near);
            near.dedup_by(|left, right| left.location() == right.location());
            near.truncate(8);
        }

        Ok(Answer {
            subject: name.to_owned(),
            hits,
            near,
            caveat: None,
            followup: Some(format!(
                "`code blame {name}` searches history for a removal."
            )),
            provenance,
        })
    }

    /// What names `identifier` — definitions included, since a definition names
    /// itself.
    pub fn uses(&self, identifier: &str, filter: &Filter) -> Result<Answer> {
        self.with_operation(|code| code.uses_scoped(identifier, filter))
    }

    fn uses_scoped(&self, identifier: &str, filter: &Filter) -> Result<Answer> {
        let observed = self.observe()?;
        let facts = crate::storage::acquire_attached_facts(&observed).context("read Code facts")?;
        let reader = observed.snapshot();
        let mut texts = Texts::new(reader);
        let scans = select_scans(&facts, &mut texts, filter)?;
        let provenance = provenance(&facts, &scans);

        let handle = code::text_handle(identifier);
        let mut hits = Vec::new();
        for scan in &scans {
            let located = match filter.kind.as_deref() {
                Some(kind) => code::items_of_kind_mentioning_in_scan(&facts, scan.id, kind, handle),
                None => code::usages_in_scan(&facts, scan.id, handle),
            };
            for located in located {
                hits.push(hit(&facts, &mut texts, scan, located)?);
            }
        }
        hits.retain(|hit| keeps(hit, filter));
        sort_hits(&mut hits);
        hits.dedup_by(|left, right| left.location() == right.location());

        Ok(Answer {
            subject: identifier.to_owned(),
            caveat: spread_caveat(&hits),
            hits,
            near: Vec::new(),
            followup: None,
            provenance,
        })
    }

    /// Which `use` roots appear in the catalogued corpus, and how often.
    pub fn imports(&self, root: &str, filter: &Filter) -> Result<(usize, Provenance)> {
        self.with_operation(|code| code.imports_scoped(root, filter))
    }

    fn imports_scoped(&self, root: &str, filter: &Filter) -> Result<(usize, Provenance)> {
        let observed = self.observe()?;
        let facts = crate::storage::acquire_attached_facts(&observed).context("read Code facts")?;
        let reader = observed.snapshot();
        let mut texts = Texts::new(reader);
        let scans = select_scans(&facts, &mut texts, filter)?;
        let provenance = provenance(&facts, &scans);
        let count = code::import_root_frequency(&facts, code::text_handle(root));
        Ok((count, provenance))
    }

    /// Everything the catalogue holds about one item.
    pub fn show(&self, selector: &str, with_source: bool) -> Result<ItemDetail> {
        self.with_operation(|code| code.show_scoped(selector, with_source))
    }

    fn show_scoped(&self, selector: &str, with_source: bool) -> Result<ItemDetail> {
        let observed = self.observe()?;
        let facts = crate::storage::acquire_attached_facts(&observed).context("read Code facts")?;
        let reader = observed.snapshot();
        let mut texts = Texts::new(reader);
        let filter = Filter::default();
        let scans = select_scans(&facts, &mut texts, &filter)?;
        let provenance = provenance(&facts, &scans);

        let item = resolve_item(&facts, &mut texts, &scans, selector)?;
        // Placements of exactly this item, in every selected scan.
        let mut places = Vec::new();
        for scan in &scans {
            for located in placements_of_item(&facts, scan.id, item) {
                places.push(hit(&facts, &mut texts, scan, located)?);
            }
        }
        sort_hits(&mut places);
        places.dedup_by(|left, right| left.location() == right.location());

        let head = places
            .first()
            .cloned()
            .ok_or_else(|| anyhow!("item {item:x} has no placement in the selected revisions"))?;

        let source = if with_source {
            item_source(&facts, reader, &head)?
        } else {
            None
        };

        Ok(ItemDetail {
            hit: head,
            places,
            source,
            provenance,
        })
    }

    /// Code that exists in more than one place.
    ///
    /// Byte-identical code is already ONE item, so this is a self-join on the
    /// placement relation, not a comparison pass: the pile's content addressing
    /// IS the clone detector. Near-duplicates are out of scope, and the
    /// renderer says so — `arc_points` being exact is luck, not a guarantee.
    pub fn duplicates(
        &self,
        min_lines: u64,
        cross_repo: bool,
        filter: &Filter,
    ) -> Result<DuplicateReport> {
        self.with_operation(|code| code.duplicates_scoped(min_lines, cross_repo, filter))
    }

    fn duplicates_scoped(
        &self,
        min_lines: u64,
        cross_repo: bool,
        filter: &Filter,
    ) -> Result<DuplicateReport> {
        let observed = self.observe()?;
        let facts = crate::storage::acquire_attached_facts(&observed).context("read Code facts")?;
        let reader = observed.snapshot();
        let mut texts = Texts::new(reader);
        let scans = select_scans(&facts, &mut texts, filter)?;
        let provenance = provenance(&facts, &scans);

        let mut pairs: BTreeSet<(Id, Id, Id)> = BTreeSet::new();
        for scan in &scans {
            for (item, left, right, _, _) in code::duplicate_placements_in_scan(&facts, scan.id) {
                // Bag-to-set presentation: the self-join necessarily yields the
                // reflexive pair and both orderings.
                if left == right {
                    continue;
                }
                let (low, high) = if left < right {
                    (left, right)
                } else {
                    (right, left)
                };
                pairs.insert((item, low, high));
            }
        }

        let mut by_item: BTreeMap<Id, BTreeSet<Id>> = BTreeMap::new();
        for (item, left, right) in pairs {
            let entry = by_item.entry(item).or_default();
            entry.insert(left);
            entry.insert(right);
        }

        let mut groups = Vec::new();
        for (item, placements) in by_item {
            let mut places = Vec::new();
            for scan in &scans {
                for located in placements_of_item(&facts, scan.id, item) {
                    if placements.contains(&located.placement) {
                        places.push(hit(&facts, &mut texts, scan, located)?);
                    }
                }
            }
            sort_hits(&mut places);
            places.dedup_by(|left, right| left.location() == right.location());
            if places.len() < 2 {
                continue;
            }
            let lines = places
                .iter()
                .map(|place| place.end_line.saturating_sub(place.line) + 1)
                .max()
                .unwrap_or(1);
            if lines < min_lines {
                continue;
            }
            if cross_repo {
                let repos: BTreeSet<&str> =
                    places.iter().map(|place| place.repo.as_str()).collect();
                if repos.len() < 2 {
                    continue;
                }
            }
            groups.push(DuplicateGroup {
                item,
                name: places.first().and_then(|place| place.name.clone()),
                kind: places.first().and_then(|place| place.kind.clone()),
                lines,
                places,
            });
        }
        groups.sort_by(|left, right| {
            right
                .places
                .len()
                .cmp(&left.places.len())
                .then(right.lines.cmp(&left.lines))
                .then(left.name.cmp(&right.name))
        });

        Ok(DuplicateReport { groups, provenance })
    }

    /// What the catalogue holds, per revision.
    pub fn stats(&self, filter: &Filter) -> Result<StatsReport> {
        self.with_operation(|code| code.stats_scoped(filter))
    }

    fn stats_scoped(&self, filter: &Filter) -> Result<StatsReport> {
        let observed = self.observe()?;
        let facts = crate::storage::acquire_attached_facts(&observed).context("read Code facts")?;
        let reader = observed.snapshot();
        let mut texts = Texts::new(reader);
        let scans = select_scans(&facts, &mut texts, filter)?;

        // `stats` and the footer want the same two counts, so they are asked
        // for once and the footer is built from them rather than re-running the
        // join over every placement a second time.
        let mut rows = Vec::new();
        let mut total_units = 0;
        let mut total_placements = 0;
        for scan in &scans {
            let units = code::units_in_scan(&facts, scan.id).len();
            let placements = code::placement_count_in_scan(&facts, scan.id);
            total_units += units;
            total_placements += placements;
            let parse_failures = code::parse_failures_in_scan(&facts, scan.id).len();
            let mut kinds = Vec::new();
            for kind in [
                ItemKind::Fn,
                ItemKind::Struct,
                ItemKind::Enum,
                ItemKind::Trait,
                ItemKind::Impl,
                ItemKind::Mod,
                ItemKind::Use,
                ItemKind::Const,
                ItemKind::Static,
                ItemKind::Type,
                ItemKind::Macro,
            ] {
                let count = code::items_of_kind_in_scan(&facts, scan.id, kind.name()).len();
                if count > 0 {
                    kinds.push((kind.name().to_owned(), count));
                }
            }
            kinds.sort_by(|left, right| right.1.cmp(&left.1));
            rows.push(StatsRow {
                scan: scan.label(),
                units,
                placements,
                parse_failures,
                kinds,
            });
        }
        Ok(StatsReport {
            rows,
            provenance: Provenance {
                scans,
                units: total_units,
                placements: total_placements,
                extractor: EXTRACTOR_RUST_SYN_V1_NAME.to_owned(),
            },
        })
    }

    /// Ask git which commits added or removed an occurrence of `identifier`.
    ///
    /// The deliberate seam: the catalogue answers from facts when it holds them
    /// and asks git when it does not. Git shows the DIFF, so unlike a name-set
    /// difference between two scans this cannot misread a rename-with-edit as a
    /// removal.
    pub fn blame(&self, identifier: &str, dirs: &[PathBuf], limit: usize) -> Result<BlameReport> {
        let mut report = BlameReport {
            identifier: identifier.to_owned(),
            ..BlameReport::default()
        };
        for dir in dirs {
            let dir = dir
                .canonicalize()
                .with_context(|| format!("resolve {}", dir.display()))?;
            if !git::is_repository(&dir) {
                continue;
            }
            let repo = repo_name(&dir);
            report.searched.push(repo.clone());
            for change in git::pickaxe(&dir, identifier, limit)? {
                let paths = git::commit_paths(&dir, &change.commit, 6).unwrap_or_default();
                report.rows.push(BlameRow {
                    repo: repo.clone(),
                    commit: change.commit,
                    date: change.date,
                    subject: change.subject,
                    paths,
                });
            }
        }
        report
            .rows
            .sort_by(|left, right| right.date.cmp(&left.date));
        Ok(report)
    }

    pub fn storage(&self) -> &Storage {
        &self.storage
    }
}

// ── ingest internals ────────────────────────────────────────────────────

struct RepoPlan {
    repo: String,
    commit: String,
    mode: String,
    sources: Vec<(String, Vec<u8>)>,
    skipped: usize,
}

fn repo_name(dir: &Path) -> String {
    dir.file_name()
        .map(|name| name.to_string_lossy().into_owned())
        .unwrap_or_else(|| dir.display().to_string())
}

/// Read one repository's catalogued files, and decide what commit value names
/// what was read.
///
/// A dirty tree gets `worktree:<blake3 of the sorted (path, content-handle)
/// list>`, so two agents ingesting the same uncommitted tree converge on one
/// scan and a differing tree is visibly a different one.
fn plan_repository(dir: &Path, options: &IngestOptions) -> Result<RepoPlan> {
    let repo = repo_name(dir);
    let mut sources: Vec<(String, Vec<u8>)> = Vec::new();
    let mut skipped = 0usize;

    let (commit, mode) = match &options.commit {
        Some(revision) => {
            let resolved = git::resolve(dir, revision)?;
            let entries = git::ls_tree(dir, &resolved)?;
            let mut by_object: BTreeMap<String, Vec<String>> = BTreeMap::new();
            for entry in &entries {
                if Language::of_path(&entry.path).is_none() {
                    continue;
                }
                by_object
                    .entry(entry.object.clone())
                    .or_default()
                    .push(entry.path.clone());
            }
            let objects: Vec<String> = by_object.keys().cloned().collect();
            let mut payloads: BTreeMap<String, Vec<u8>> = BTreeMap::new();
            git::cat_objects(dir, &objects, |object, payload| {
                payloads.insert(object.to_owned(), payload);
                Ok(())
            })?;
            for (object, paths) in by_object {
                let Some(payload) = payloads.get(&object) else {
                    continue;
                };
                for path in paths {
                    if payload.len() > MAX_UNIT_BYTES {
                        skipped += 1;
                        continue;
                    }
                    sources.push((path, payload.clone()));
                }
            }
            (resolved, "commit".to_owned())
        }
        None => {
            for path in git::ls_files(dir)? {
                if Language::of_path(&path).is_none() {
                    // An extension this build cannot model is skipped, never
                    // rejected, and never even read.
                    continue;
                }
                let full = dir.join(&path);
                let Ok(bytes) = std::fs::read(&full) else {
                    // A tracked path missing from the working tree is something
                    // this reader cannot establish, not an error to refuse over.
                    skipped += 1;
                    continue;
                };
                if bytes.len() > MAX_UNIT_BYTES {
                    skipped += 1;
                    continue;
                }
                sources.push((path, bytes));
            }
            if git::is_dirty(dir)? {
                (worktree_commit(&sources), "worktree".to_owned())
            } else {
                (git::head(dir)?, "head".to_owned())
            }
        }
    };

    sources.sort_by(|left, right| left.0.cmp(&right.0));
    Ok(RepoPlan {
        repo,
        commit,
        mode,
        sources,
        skipped,
    })
}

/// A commit value for an uncommitted tree: the tree's own content, digested.
pub fn worktree_commit(sources: &[(String, Vec<u8>)]) -> String {
    let mut entries: Vec<(String, String)> = sources
        .iter()
        .map(|(path, bytes)| (path.clone(), hex::encode(code::bytes_handle(bytes).raw)))
        .collect();
    entries.sort();
    let mut hasher = blake3::Hasher::new();
    for (path, handle) in entries {
        hasher.update(path.as_bytes());
        hasher.update(b"\0");
        hasher.update(handle.as_bytes());
        hasher.update(b"\n");
    }
    format!("worktree:{}", hasher.finalize().to_hex())
}

fn ingest_repository<P>(
    writer: &mut ingest::CodeImportWriter<P>,
    plan: RepoPlan,
) -> Result<RepoIngest>
where
    P: triblespace::core::repo::Store
        + triblespace::core::repo::async_store::AsyncBlobStoreAcquire
        + Send,
    P::Snapshot: triblespace::core::repo::async_store::AsyncBlobStoreGet,
{
    let repo_handle = code::text_handle(&plan.repo);
    let commit_handle = code::text_handle(&plan.commit);
    let scan_core = code::scan_core(repo_handle, commit_handle);
    let scan = scan_core.root().expect("scan core has one intrinsic root");

    // A re-observation of an IDENTICAL revision must write nothing at all, so
    // the observation time is recorded once, when this `(repo, commit)` first
    // becomes known. The clock deliberately never enters the core — two
    // machines scanning one commit produce the same scan entity — and stamping
    // a fresh `created_at` on every run would be the one fact that made an
    // otherwise no-op re-ingest grow the pile forever.
    let known = writer.holds_entity(scan);
    let mut scan_fragment = Fragment::empty();
    // The blobs behind both identity values still have to reach the pile.
    let mut carrier = Fragment::empty();
    let _ = carrier.put::<blobencodings::UTF8String, _>(plan.repo.clone());
    let _ = carrier.put::<blobencodings::UTF8String, _>(plan.commit.clone());
    scan_fragment += carrier;
    scan_fragment += scan_core;
    if !known {
        scan_fragment += code::scan_annotation(scan, crate::clock::point_now()?, &plan.mode);
    }
    writer.stage_fragment(scan_fragment)?;

    let mut row = RepoIngest {
        repo: plan.repo.clone(),
        commit: plan.commit.clone(),
        files_seen: plan.sources.len(),
        files_skipped: plan.skipped,
        ..RepoIngest::default()
    };

    for (path, bytes) in &plan.sources {
        let Some(language) = Language::of_path(path) else {
            // Unreachable: the plan only collects paths with a language. Kept
            // as a skip rather than an unwrap, because a reader that cannot
            // model something ignores it.
            row.files_skipped += 1;
            continue;
        };
        let path_handle = code::text_handle(path);
        let content_handle = code::bytes_handle(bytes);
        let unit_core = code::unit_core(repo_handle, path_handle, content_handle);
        let unit = unit_core.root().expect("unit core has one intrinsic root");

        // Always assert membership: it is cheap, it converges, and it is what
        // makes a re-scan of an unchanged tree zero-growth rather than absent.
        writer.stage_fragment(code::scan_holds(scan, unit))?;

        if writer.holds_entity(unit) {
            // These exact bytes at this exact path are already catalogued. No
            // parse at all — one Blake3 and one existence probe per unchanged
            // file is what makes a steady-state ingest cost ten parses.
            continue;
        }

        let extracted = extract::extract(&String::from_utf8_lossy(bytes), language);
        if extracted.parse_error.is_some() {
            row.parse_failures += 1;
        }
        row.files_parsed += 1;
        row.items += extracted.items.len();
        let (fragment, _) = code::unit_fragment(&plan.repo, path, language, bytes, &extracted);
        writer.stage_fragment(fragment)?;

        if writer.delta_len() > MAX_STAGED_FACTS {
            if writer.commit_unit()?.is_some() {
                row.commits += 1;
            }
        }
    }

    if writer.commit_unit()?.is_some() {
        row.commits += 1;
    }
    Ok(row)
}

// ── read internals ──────────────────────────────────────────────────────

pub(crate) fn placements_of_item<P>(facts: &P, scan: Id, item: Id) -> Vec<code::Located>
where
    P: TriblePattern + ?Sized,
{
    find!(
        (placement: Id, unit: Id, range: code::SpanValue),
        pattern!(facts, [
            { scan @ crate::schemas::code::attrs::holds: ?unit },
            { ?placement @
                crate::schemas::code::attrs::unit: ?unit,
                crate::schemas::code::attrs::item: item,
                crate::schemas::code::attrs::source_range: ?range },
        ])
    )
    .map(|(placement, unit, range)| code::Located {
        item,
        placement,
        unit,
        range,
    })
    .collect()
}

/// Which scans answer this question.
///
/// With no selector this is the newest scan per repository, which is the only
/// default that does not silently mix two revisions of the same file.
pub(crate) fn select_scans(
    facts: &FactArchive,
    texts: &mut Texts<'_>,
    filter: &Filter,
) -> Result<Vec<ScanSummary>> {
    let mut all = Vec::new();
    for row in code::projected_scans(facts) {
        let repo = texts.get(row.repo)?;
        let commit = texts.get(row.commit)?;
        let observed_at = interval_start(row.observed_at);
        all.push(ScanSummary {
            id: row.id,
            repo,
            commit,
            observed_at,
        });
    }
    if let Some(repo) = &filter.repo {
        all.retain(|scan| &scan.repo == repo);
    }

    let mut selected = match &filter.revision {
        Revision::Latest => {
            let mut newest: BTreeMap<String, ScanSummary> = BTreeMap::new();
            for scan in all {
                newest
                    .entry(scan.repo.clone())
                    .and_modify(|current| {
                        if scan.observed_at > current.observed_at {
                            *current = scan.clone();
                        }
                    })
                    .or_insert(scan);
            }
            newest.into_values().collect::<Vec<_>>()
        }
        Revision::Worktree => all
            .into_iter()
            .filter(|scan| scan.commit.starts_with("worktree:"))
            .collect(),
        Revision::Selector(selector) => {
            let (repo, revision) = match selector.split_once('@') {
                Some((repo, revision)) => (Some(repo), revision),
                None => (None, selector.as_str()),
            };
            all.into_iter()
                .filter(|scan| repo.is_none_or(|repo| scan.repo == repo))
                .filter(|scan| {
                    if revision == "worktree" {
                        scan.commit.starts_with("worktree:")
                    } else {
                        scan.commit.starts_with(revision)
                    }
                })
                .collect()
        }
    };
    selected.sort_by(|left, right| left.repo.cmp(&right.repo));
    Ok(selected)
}

/// The lower bound of an observation interval.
///
/// A value this reader cannot decode sorts to the epoch rather than failing the
/// command: an undecodable timestamp on someone else's scan is not a reason to
/// refuse to answer a question about ours.
fn interval_start(interval: code::IntervalValue) -> Epoch {
    let bounds: std::result::Result<(i128, i128), _> = interval.try_from_inline();
    let lower = bounds.map(|(lower, _)| lower).unwrap_or(0);
    Epoch::from_tai_duration(hifitime::Duration::from_total_nanoseconds(lower))
}

pub(crate) fn provenance(facts: &FactArchive, scans: &[ScanSummary]) -> Provenance {
    let mut units = 0;
    let mut placements = 0;
    for scan in scans {
        units += code::units_in_scan(facts, scan.id).len();
        placements += code::placement_count_in_scan(facts, scan.id);
    }
    Provenance {
        scans: scans.to_vec(),
        units,
        placements,
        extractor: EXTRACTOR_RUST_SYN_V1_NAME.to_owned(),
    }
}

/// Assemble one display row for one surviving result.
///
/// This is render-time projection: it is built per result that already passed
/// the relation query, it is never cached, and it never outlives the call.
pub(crate) fn hit(
    facts: &FactArchive,
    texts: &mut Texts<'_>,
    scan: &ScanSummary,
    located: code::Located,
) -> Result<Hit> {
    let (repo, path) = match code::unit_location(facts, located.unit).into_iter().next() {
        Some((repo, path)) => (texts.get(repo)?, texts.get(path)?),
        None => (scan.repo.clone(), "?".to_owned()),
    };
    let row = code::item_row(facts, located.item).into_iter().next();
    let (line, _, end_line, _) = code::span_parts(located.range);
    Ok(Hit {
        item: located.item,
        unit: located.unit,
        repo,
        path,
        line,
        end_line,
        kind: row.map(|row| row.kind.name().to_owned()),
        visibility: row.map(|row| row.visibility.name().to_owned()),
        name: texts.first(code::item_names(facts, located.item))?,
        signature: texts.first(code::item_signature(facts, located.item))?,
        doc: texts.first(code::item_doc(facts, located.item))?,
        scan: scan.label(),
    })
}

pub(crate) fn keeps(hit: &Hit, filter: &Filter) -> bool {
    if let Some(kind) = &filter.kind {
        if hit.kind.as_deref() != Some(kind.as_str()) {
            return false;
        }
    }
    if let Some(repo) = &filter.repo {
        if &hit.repo != repo {
            return false;
        }
    }
    true
}

fn sort_hits(hits: &mut [Hit]) {
    hits.sort_by(|left, right| {
        left.repo
            .cmp(&right.repo)
            .then(left.path.cmp(&right.path))
            .then(left.line.cmp(&right.line))
    });
}

/// A spread line for an identifier common enough that a list means little.
///
/// A tool that says "0 hits" beautifully and "4,912 hits" identically has
/// learned nothing. `mentions` is unresolved, so above a threshold the honest
/// output is the shape of the answer plus what it cannot tell you.
fn spread_caveat(hits: &[Hit]) -> Option<String> {
    if hits.len() < 60 {
        return None;
    }
    let units: BTreeSet<(&str, &str)> = hits
        .iter()
        .map(|hit| (hit.repo.as_str(), hit.path.as_str()))
        .collect();
    let repos: BTreeSet<&str> = hits.iter().map(|hit| hit.repo.as_str()).collect();
    Some(format!(
        "{} item(s) across {} file(s) across {} repo(s). `mentions` is UNRESOLVED: \
these are declarations that spell this identifier, not resolved references to one \
definition, so a common name returns a list this catalogue cannot narrow.",
        hits.len(),
        units.len(),
        repos.len(),
    ))
}

/// Exact queries for name-similar declarations, rather than a fuzzy scan.
///
/// `LearnerBuilder` yields `Learner` and `Builder`; `snapshot_at` yields
/// `snapshot` and `at`. Each candidate is looked up exactly, so the near-miss
/// list costs a handful of constant-folded index probes rather than reading
/// thirty thousand names.
pub fn near_miss_candidates(name: &str) -> Vec<String> {
    let mut candidates: BTreeSet<String> = BTreeSet::new();

    let camel = split_camel(name);
    for window in 1..camel.len() {
        candidates.insert(camel[..window].concat());
        candidates.insert(camel[window..].concat());
    }
    for word in &camel {
        candidates.insert((*word).to_owned());
    }

    let snake: Vec<&str> = name.split('_').filter(|part| !part.is_empty()).collect();
    for window in 1..snake.len() {
        candidates.insert(snake[..window].join("_"));
        candidates.insert(snake[window..].join("_"));
    }
    for word in &snake {
        candidates.insert((*word).to_owned());
    }

    candidates.remove(name);
    candidates.retain(|candidate| candidate.len() >= 3);
    candidates.into_iter().collect()
}

fn split_camel(name: &str) -> Vec<String> {
    let mut words = Vec::new();
    let mut current = String::new();
    for character in name.chars() {
        if character.is_uppercase() && !current.is_empty() {
            words.push(std::mem::take(&mut current));
        }
        current.push(character);
    }
    if !current.is_empty() {
        words.push(current);
    }
    words
}

fn resolve_item(
    facts: &FactArchive,
    texts: &mut Texts<'_>,
    scans: &[ScanSummary],
    selector: &str,
) -> Result<Id> {
    let trimmed = selector.trim();
    if trimmed
        .chars()
        .all(|character| character.is_ascii_hexdigit())
        && trimmed.len() >= 4
    {
        if let Ok(id) = crate::resolve_id_prefix(trimmed, code::all_item_ids(facts)) {
            return Ok(id);
        }
    }
    let handle = code::text_handle(trimmed);
    for scan in scans {
        if let Some(located) = code::definitions_in_scan(facts, scan.id, handle)
            .into_iter()
            .next()
        {
            return Ok(located.item);
        }
    }
    let _ = texts;
    bail!("no catalogued item matches {selector:?}")
}

/// The declaration's own lines, sliced out of the exact unit it was placed in.
///
/// Addressing the unit rather than the path matters: two revisions of one file
/// are two units with the same path, and slicing the wrong one would print
/// lines that do not say what the reported line number says they do.
fn item_source(facts: &FactArchive, reader: &CodeReader, hit: &Hit) -> Result<Option<String>> {
    for handle in code::unit_content(facts, hit.unit) {
        let bytes: anybytes::Bytes = match reader.get(handle) {
            Ok(bytes) => bytes,
            Err(_) => continue,
        };
        let text = String::from_utf8_lossy(&bytes).into_owned();
        let start = hit.line.saturating_sub(1) as usize;
        let end = (hit.end_line as usize).min(text.lines().count());
        if start >= end {
            continue;
        }
        return Ok(Some(
            text.lines()
                .skip(start)
                .take(end - start)
                .collect::<Vec<_>>()
                .join("\n"),
        ));
    }
    Ok(None)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_camel_name_yields_its_head_and_tail_as_near_misses() {
        let candidates = near_miss_candidates("LearnerBuilder");
        assert!(candidates.contains(&"Learner".to_owned()), "{candidates:?}");
        assert!(candidates.contains(&"Builder".to_owned()));
        assert!(!candidates.contains(&"LearnerBuilder".to_owned()));
    }

    #[test]
    fn a_snake_name_yields_its_segments_as_near_misses() {
        let candidates = near_miss_candidates("snapshot_at");
        assert!(
            candidates.contains(&"snapshot".to_owned()),
            "{candidates:?}"
        );
    }

    #[test]
    fn a_worktree_commit_is_stable_for_the_same_tree() {
        let tree = vec![
            ("b.rs".to_owned(), b"two".to_vec()),
            ("a.rs".to_owned(), b"one".to_vec()),
        ];
        let reversed = vec![
            ("a.rs".to_owned(), b"one".to_vec()),
            ("b.rs".to_owned(), b"two".to_vec()),
        ];
        assert_eq!(worktree_commit(&tree), worktree_commit(&reversed));
        assert!(worktree_commit(&tree).starts_with("worktree:"));

        let changed = vec![
            ("a.rs".to_owned(), b"one".to_vec()),
            ("b.rs".to_owned(), b"three".to_vec()),
        ];
        assert_ne!(worktree_commit(&tree), worktree_commit(&changed));
    }

    #[test]
    fn a_revision_selector_defaults_to_the_newest_scan_per_repo() {
        assert_eq!(Revision::parse(None), Revision::Latest);
        assert_eq!(Revision::parse(Some("  ")), Revision::Latest);
        assert_eq!(Revision::parse(Some("worktree")), Revision::Worktree);
        assert_eq!(
            Revision::parse(Some("faculties@336a8765")),
            Revision::Selector("faculties@336a8765".to_owned())
        );
    }
}