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
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
//! Canonical file-record construction shared by every faculty.
//!
//! A file entity is an immutable description of one named file value. Its
//! intrinsic identity is derived from exactly four facts:
//!
//! - [`metadata::tag`] = [`KIND_FILE`]
//! - [`file::content`] = the content-addressed raw-bytes handle
//! - [`file::name`] = the leaf name (never a containing path)
//! - [`file::media_type`] = an intrinsic [`KIND_MEDIA_TYPE`] entity
//!
//! Paths, source-system identifiers, timestamps, tags, embeddings, and other
//! provenance are deliberately absent from that identity. New callers attach
//! those facts to imports or source-specific occurrence entities; complete
//! catalogs also preserve historical path/timestamp provenance on file ids.

pub mod cli;
pub mod mcp;
pub mod operations;
pub mod presentation;

pub use operations::Files;

use anyhow::{anyhow, Context, Result};
use ed25519_dalek::SigningKey;
use hifitime::Epoch;
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use triblespace::core::collection::{CollectionCommit, CollectionSnapshotExt, CollectionStoreExt};
use triblespace::core::metadata;
use triblespace::core::query::TriblePattern;
use triblespace::core::repo::pile::{Pile, PileSnapshot};
use triblespace::core::repo::BlobStoreGet;
use triblespace::prelude::blobencodings::{RawBytes, UTF8String};
use triblespace::prelude::inlineencodings::{Handle, NsTAIInterval, ShortString};
use triblespace::prelude::*;
use triblespace_search::schemas::Embedding;

use crate::collection_names::open_configured;
use crate::schemas::embeddings;
use crate::schemas::files::{file, KIND_DIRECTORY, KIND_FILE, KIND_IMPORT, KIND_MEDIA_TYPE};

pub type ContentHandle = Inline<Handle<RawBytes>>;
pub type NameHandle = Inline<Handle<UTF8String>>;
pub type ImportTime = Inline<NsTAIInterval>;
pub const DEFAULT_MEDIA_TYPE: &str = "application/octet-stream";

/// The two canonical targets carried by a `files:` reference token.
///
/// Unlike an entity selector, a content reference identifies bytes directly:
/// several named file entities may intentionally share one content handle.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum FileReference {
    Entity(Id),
    Content(ContentHandle),
}

impl FileReference {
    pub fn hex(self) -> String {
        match self {
            Self::Entity(entity) => format!("{entity:x}"),
            Self::Content(content) => content_hash_hex(content),
        }
    }
}

/// One structurally complete file in a validated Files snapshot.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FileRecord {
    pub id: Id,
    pub name: String,
    pub content: ContentHandle,
    pub media_type: Id,
}

/// One structurally complete directory in a validated Files snapshot.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DirectoryRecord {
    pub id: Id,
    pub name: String,
    pub children: Vec<Id>,
}

/// One structurally complete import in a validated Files snapshot.
///
/// The writer derives an import id from exactly one source path, timestamp,
/// and root. Repeated values are therefore corruption or conflicting additive
/// evidence, not alternate candidates from which a reader may choose.
#[derive(Clone, Debug, PartialEq)]
pub struct ImportRecord {
    pub id: Id,
    pub imported_at: Epoch,
    pub source_path: String,
    pub root: Id,
    pub tags: Vec<String>,
}

/// A validated file-system node.
#[derive(Clone, Copy, Debug)]
pub enum NodeRecord<'a> {
    File(&'a FileRecord),
    Directory(&'a DirectoryRecord),
}

/// A byte reference together with every catalog name that describes it.
///
/// Entity references always have one name because [`FilesCatalog`] rejects
/// malformed scalar fields. Content references may intentionally be shared by
/// several differently named files, so callers must not pick one name as a
/// winner. [`ResolvedFile::unique_name`] is `None` in that case.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolvedFile {
    pub content: ContentHandle,
    pub names: Vec<String>,
}

impl ResolvedFile {
    pub fn unique_name(&self) -> Option<&str> {
        (self.names.len() == 1).then(|| self.names[0].as_str())
    }
}

/// Canonical read model for one complete Files collection value.
///
/// Scalar writer fields are validated as exactly-one values. Directory edges
/// and tags remain set-valued and are sorted for deterministic presentation.
/// This type is the shared boundary for the Files and Wiki widgets so neither
/// can accidentally reinstate `Iterator::next()` winner semantics.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FilesCatalog {
    files: BTreeMap<Id, FileRecord>,
    directories: BTreeMap<Id, DirectoryRecord>,
    imports: BTreeMap<Id, ImportRecord>,
}

impl FilesCatalog {
    pub fn files(&self) -> impl ExactSizeIterator<Item = &FileRecord> {
        self.files.values()
    }

    pub fn directories(&self) -> impl ExactSizeIterator<Item = &DirectoryRecord> {
        self.directories.values()
    }

    pub fn imports(&self) -> impl ExactSizeIterator<Item = &ImportRecord> {
        self.imports.values()
    }

    pub fn file(&self, id: Id) -> Option<&FileRecord> {
        self.files.get(&id)
    }

    pub fn directory(&self, id: Id) -> Option<&DirectoryRecord> {
        self.directories.get(&id)
    }

    pub fn import(&self, id: Id) -> Option<&ImportRecord> {
        self.imports.get(&id)
    }

    pub fn node(&self, id: Id) -> Option<NodeRecord<'_>> {
        self.file(id)
            .map(NodeRecord::File)
            .or_else(|| self.directory(id).map(NodeRecord::Directory))
    }

    /// Resolve the canonical selector language using only validated records.
    pub fn resolve_reference(&self, input: &str) -> Result<FileReference> {
        let selector = normalize_selector(input)?;

        if selector.len() == 32 {
            return Id::from_hex(&selector)
                .map(FileReference::Entity)
                .ok_or_else(|| anyhow!("invalid entity id '{selector}'"));
        }
        if selector.len() == 64 {
            let hash = inlineencodings::Hash::<inlineencodings::Blake3>::from_hex(&selector)
                .map_err(|_| anyhow!("invalid content hash '{selector}'"))?;
            return Ok(FileReference::Content(inlineencodings::Handle::from_hash(
                hash,
            )));
        }

        let mut matches = BTreeSet::new();
        for file in self.files.values() {
            if format!("{:x}", file.id).starts_with(&selector) {
                matches.insert(FileReference::Entity(file.id));
            }
            if content_hash_hex(file.content).starts_with(&selector) {
                matches.insert(FileReference::Content(file.content));
            }
        }
        for id in self.directories.keys().chain(self.imports.keys()).copied() {
            if format!("{id:x}").starts_with(&selector) {
                matches.insert(FileReference::Entity(id));
            }
        }
        exactly_one(&selector, matches, "file reference", |reference| {
            format!("files:{}", reference.hex())
        })
    }

    /// Resolve bytes without manufacturing a display-name winner.
    pub fn resolve_file(&self, input: &str) -> Result<ResolvedFile> {
        match self.resolve_reference(input)? {
            FileReference::Entity(id) => {
                let file = self
                    .file(id)
                    .ok_or_else(|| anyhow!("files entity {id:x} is not a file"))?;
                Ok(ResolvedFile {
                    content: file.content,
                    names: vec![file.name.clone()],
                })
            }
            FileReference::Content(content) => {
                let names = self
                    .files
                    .values()
                    .filter(|file| file.content == content)
                    .map(|file| file.name.clone())
                    .collect::<BTreeSet<_>>()
                    .into_iter()
                    .collect();
                Ok(ResolvedFile { content, names })
            }
        }
    }
}

/// Return the lowercase Blake3 digest addressed by a canonical file handle.
pub fn content_hash_hex(handle: ContentHandle) -> String {
    let hash: Inline<inlineencodings::Hash<inlineencodings::Blake3>> =
        inlineencodings::Handle::to_hash(handle);
    inlineencodings::Hash::<inlineencodings::Blake3>::to_hex(&hash).to_ascii_lowercase()
}

fn normalize_selector(input: &str) -> Result<String> {
    let trimmed = input.trim();
    let raw = trimmed.strip_prefix("files:").unwrap_or(trimmed);
    let selector = raw.to_ascii_lowercase();
    if selector.is_empty()
        || selector.len() > 64
        || !selector.bytes().all(|byte| byte.is_ascii_hexdigit())
    {
        anyhow::bail!(
            "invalid file selector '{selector}': expected 1 to 64 hexadecimal characters, optionally prefixed by 'files:'"
        );
    }
    Ok(selector)
}

fn exactly_one<T>(
    selector: &str,
    matches: BTreeSet<T>,
    kind: &str,
    render: impl Fn(T) -> String,
) -> Result<T>
where
    T: Copy + Ord,
{
    match matches.len() {
        0 => anyhow::bail!("no {kind} matches selector '{selector}'"),
        1 => Ok(*matches.first().expect("one selector match")),
        _ => {
            let candidates = matches
                .into_iter()
                .map(render)
                .collect::<Vec<_>>()
                .join(", ");
            anyhow::bail!("{kind} selector '{selector}' is ambiguous; candidates: {candidates}")
        }
    }
}

#[derive(Default)]
struct SelectorMatches {
    entities: BTreeSet<Id>,
    references: BTreeSet<FileReference>,
}

fn selector_matches<P: TriblePattern>(space: &P, selector: &str) -> SelectorMatches {
    let mut matches = SelectorMatches::default();
    for (entity, content) in find!(
        (entity: Id, content: ContentHandle),
        pattern!(space, [{
            ?entity @ metadata::tag: &KIND_FILE,
            file::content: ?content,
        }])
    ) {
        if format!("{entity:x}").starts_with(selector) {
            matches.entities.insert(entity);
            matches.references.insert(FileReference::Entity(entity));
        }
        if content_hash_hex(content).starts_with(selector) {
            matches.entities.insert(entity);
            matches.references.insert(FileReference::Content(content));
        }
    }
    for entity in find!(
        entity: Id,
        pattern!(space, [{ ?entity @ metadata::tag: &KIND_DIRECTORY }])
    )
    .chain(find!(
        entity: Id,
        pattern!(space, [{ ?entity @ metadata::tag: &KIND_IMPORT }])
    )) {
        if format!("{entity:x}").starts_with(selector) {
            matches.entities.insert(entity);
            matches.references.insert(FileReference::Entity(entity));
        }
    }
    matches
}

/// Resolve a file-faculty selector to exactly one entity.
///
/// Selectors are case-insensitive hexadecimal with an optional `files:`
/// prefix. A complete 32-character entity id is direct and intentionally does
/// not scan the catalog. A complete 64-character digest resolves through
/// [`file::content`] and must identify exactly one canonical file entity. Any
/// other shorter selector is matched against eligible file, directory, and
/// import entity ids as well as file content digests. Matches are deduplicated
/// by entity, so one file matching through both its id and digest remains one
/// candidate.
pub fn resolve_selector<P: TriblePattern>(space: &P, input: &str) -> Result<Id> {
    let selector = normalize_selector(input)?;

    if selector.len() == 32 {
        return Id::from_hex(&selector).ok_or_else(|| anyhow!("invalid entity id '{selector}'"));
    }

    if selector.len() == 64 {
        let hash = inlineencodings::Hash::<inlineencodings::Blake3>::from_hex(&selector)
            .map_err(|_| anyhow!("invalid content hash '{selector}'"))?;
        let handle: ContentHandle = inlineencodings::Handle::from_hash(hash);
        let matches: BTreeSet<Id> = find!(
            entity: Id,
            pattern!(space, [{
                ?entity @ metadata::tag: &KIND_FILE,
                file::content: &handle,
            }])
        )
        .collect();
        return exactly_one(&selector, matches, "file entity", |entity| {
            format!("{entity:x}")
        });
    }

    exactly_one(
        &selector,
        selector_matches(space, &selector).entities,
        "file entity",
        |entity| format!("{entity:x}"),
    )
}

/// Expand a selector to one canonical `files:` reference token.
///
/// This is deliberately distinct from [`resolve_selector`]. Entity selectors
/// collapse matches by entity and therefore reject one content hash shared by
/// multiple named files. Reference selectors collapse by reference token: the
/// shared content handle is one valid content reference, while entity ids are
/// separate reference tokens. Complete 32- and 64-character tokens are direct
/// and need no catalog evidence; shorter prefixes are expanded from canonical
/// file, directory, and import records.
pub fn resolve_reference<P: TriblePattern>(space: &P, input: &str) -> Result<FileReference> {
    let selector = normalize_selector(input)?;

    if selector.len() == 32 {
        return Id::from_hex(&selector)
            .map(FileReference::Entity)
            .ok_or_else(|| anyhow!("invalid entity id '{selector}'"));
    }

    if selector.len() == 64 {
        let hash = inlineencodings::Hash::<inlineencodings::Blake3>::from_hex(&selector)
            .map_err(|_| anyhow!("invalid content hash '{selector}'"))?;
        return Ok(FileReference::Content(inlineencodings::Handle::from_hash(
            hash,
        )));
    }

    exactly_one(
        &selector,
        selector_matches(space, &selector).references,
        "file reference",
        |reference| format!("files:{}", reference.hex()),
    )
}

/// Best-effort media type inferred from a filename extension.
///
/// This table is shared because media type participates in canonical file
/// identity: two faculties must not classify the same named bytes differently
/// merely because they each grew their own extension mapping. Extensions are
/// ASCII-case-insensitive.
pub fn infer_media_type(path: &Path) -> &'static str {
    let extension = path
        .extension()
        .and_then(|extension| extension.to_str())
        .unwrap_or("")
        .to_ascii_lowercase();
    match extension.as_str() {
        "pdf" => "application/pdf",
        "json" => "application/json",
        "toml" => "application/toml",
        "yaml" | "yml" => "application/yaml",
        "xml" => "application/xml",
        "csv" => "text/csv",
        "txt" => "text/plain",
        "md" | "markdown" => "text/markdown",
        "rs" => "text/x-rust",
        "py" => "text/x-python",
        "js" => "application/javascript",
        "ts" => "application/typescript",
        "html" | "htm" => "text/html",
        "css" => "text/css",
        "png" => "image/png",
        "jpg" | "jpeg" => "image/jpeg",
        "gif" => "image/gif",
        "svg" => "image/svg+xml",
        "webp" => "image/webp",
        "bmp" => "image/bmp",
        "tif" | "tiff" => "image/tiff",
        "tar" => "application/x-tar",
        "gz" | "gzip" => "application/gzip",
        "zip" => "application/zip",
        "wasm" => "application/wasm",
        "doc" => "application/msword",
        "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "xls" => "application/vnd.ms-excel",
        "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "ppt" => "application/vnd.ms-powerpoint",
        "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        _ => DEFAULT_MEDIA_TYPE,
    }
}

/// Reduce a source-provided name to the path-independent leaf used by file
/// identity. Both separator styles are recognized so records converge across
/// Unix, Windows, mail clients, and remote APIs.
pub fn leaf_name(name: &str) -> String {
    name.rsplit(['/', '\\'])
        .find(|part| !part.is_empty() && *part != "." && *part != "..")
        .unwrap_or("unnamed")
        .to_owned()
}

/// Parse a source-provided content type and return its canonical media-type
/// essence (parameters removed, type/subtype lower-cased).
///
/// An absent content type is represented by `application/octet-stream`.
/// Invalid non-empty values are rejected rather than becoming distinct bogus
/// media-type entities.
pub fn normalize_media_type(media_type: &str) -> Result<String> {
    let media_type = media_type.trim();
    if media_type.is_empty() {
        return Ok(DEFAULT_MEDIA_TYPE.to_owned());
    }
    let parsed = media_type
        .parse::<mime::Mime>()
        .map_err(|err| anyhow!("invalid file media type {media_type:?}: {err}"))?;
    Ok(parsed.essence_str().to_ascii_lowercase())
}

/// Normalize an untrusted protocol-provided content type, degrading malformed
/// values to the generic binary type. Source adapters use this when rejecting
/// one bad header would prevent an otherwise replayable sync cursor from
/// advancing; explicit user-facing MIME overrides should stay on the strict
/// [`normalize_media_type`] / [`stage`] path.
pub fn normalize_media_type_or_default(media_type: &str) -> String {
    normalize_media_type(media_type).unwrap_or_else(|_| DEFAULT_MEDIA_TYPE.to_owned())
}

/// Resolve the full media-type name handle for a canonical file record.
///
/// The join deliberately verifies the target's kind as well as following the
/// relation. A dangling or wrongly-typed target is not a valid media type.
pub fn media_type_name_handle<P: TriblePattern>(space: &P, file_id: Id) -> Option<NameHandle> {
    find!(
        (name: NameHandle),
        pattern!(space, [
            { file_id @ file::media_type: _?media_type },
            { _?media_type @ metadata::tag: &KIND_MEDIA_TYPE, metadata::name: ?name }
        ])
    )
    .next()
    .map(|(name,)| name)
}

fn one_file_value<T: Ord>(values: BTreeSet<T>, field: &str, file_id: Id) -> Result<Option<T>> {
    match values.len() {
        0 => Ok(None),
        1 => Ok(values.into_iter().next()),
        count => anyhow::bail!("file {file_id:x} has {count} values for {field}"),
    }
}

/// Resolve the unique content handle of one file entity, if present.
pub fn content_handle<P: TriblePattern>(space: &P, file_id: Id) -> Result<Option<ContentHandle>> {
    one_file_value(
        find!(
            content: ContentHandle,
            pattern!(space, [{ file_id @ file::content: ?content }])
        )
        .collect(),
        "content",
        file_id,
    )
}

/// Resolve the unique leaf-name handle of one file entity, if present.
pub fn name_handle<P: TriblePattern>(space: &P, file_id: Id) -> Result<Option<NameHandle>> {
    one_file_value(
        find!(
            name: NameHandle,
            pattern!(space, [{ file_id @ file::name: ?name }])
        )
        .collect(),
        "name",
        file_id,
    )
}

/// Resolve a file's unique media-type name while checking the referenced
/// entity's canonical kind.
pub fn media_type_name_handle_strict<P: TriblePattern>(
    space: &P,
    file_id: Id,
) -> Result<Option<NameHandle>> {
    one_file_value(
        find!(
            name: NameHandle,
            pattern!(space, [
                { file_id @ file::media_type: _?media_type },
                { _?media_type @ metadata::tag: &KIND_MEDIA_TYPE, metadata::name: ?name }
            ])
        )
        .collect(),
        "media type name",
        file_id,
    )
}

fn required_catalog_value<T: Ord>(values: BTreeSet<T>, field: &str, id: Id) -> Result<T> {
    match values.len() {
        1 => Ok(values.into_iter().next().expect("one catalog value")),
        count => anyhow::bail!(
            "Files entity {id:x} has {count} values for {field}; expected exactly one"
        ),
    }
}

fn read_catalog_text<R: BlobStoreGet>(
    reader: &R,
    handle: NameHandle,
    field: &str,
    id: Id,
) -> Result<String> {
    let value: anybytes::View<str> = reader
        .get(handle)
        .map_err(|error| anyhow!("read Files {field} for {id:x}: {error:?}"))?;
    Ok(value.to_string())
}

fn decode_point_imported_at(value: ImportTime, owner: &str, id: Id) -> Result<Epoch> {
    let (start, end): (Epoch, Epoch) = value
        .try_from_inline()
        .map_err(|error| anyhow!("decode Files {owner} imported_at for {id:x}: {error:?}"))?;
    if start != end {
        anyhow::bail!("Files {owner} {id:x} has a non-point imported_at interval");
    }
    Ok(start)
}

fn entities_with_attributes(facts: &TribleSet, attributes: &[Id]) -> BTreeSet<Id> {
    facts
        .iter()
        .filter(|fact| attributes.contains(fact.a()))
        .map(|fact| *fact.e())
        .collect()
}

fn validate_directory_acyclic(
    id: Id,
    directories: &BTreeMap<Id, DirectoryRecord>,
    visiting: &mut BTreeSet<Id>,
    visited: &mut BTreeSet<Id>,
) -> Result<()> {
    if visited.contains(&id) {
        return Ok(());
    }
    if !visiting.insert(id) {
        anyhow::bail!("Files directory graph contains a cycle through {id:x}");
    }
    let directory = directories
        .get(&id)
        .expect("cycle walk starts from a directory");
    for child in &directory.children {
        if directories.contains_key(child) {
            validate_directory_acyclic(*child, directories, visiting, visited)?;
        }
    }
    visiting.remove(&id);
    visited.insert(id);
    Ok(())
}

/// Strictly read every payload whose encoding is known to Files.
///
/// Structural validation lives in [`load_catalog`]; this narrower function is
/// also used while replaying stopped legacy commits, where preserving an exact
/// authored delta must happen before the union-level structure can be checked.
pub fn validate_known_payloads<R: BlobStoreGet>(reader: &R, facts: &TribleSet) -> Result<()> {
    for fact in facts {
        if fact.a() == &file::content.id() {
            let handle = *fact.v::<Handle<RawBytes>>();
            let _: anybytes::Bytes = reader.get(handle).map_err(|error| {
                anyhow!(
                    "strictly read file content {}: {error:?}",
                    hex::encode(handle.raw)
                )
            })?;
        } else if fact.a() == &file::name.id()
            || fact.a() == &file::source_path.id()
            || fact.a() == &metadata::name.id()
            || fact.a() == &metadata::description.id()
        {
            let handle = *fact.v::<Handle<UTF8String>>();
            let _: anybytes::View<str> = reader.get(handle).map_err(|error| {
                anyhow!(
                    "strictly read Files text {}: {error:?}",
                    hex::encode(handle.raw)
                )
            })?;
        } else if fact.a() == &file::embedding.id() {
            let handle = *fact.v::<Handle<Embedding>>();
            let _: anybytes::View<[f32]> = reader.get(handle).map_err(|error| {
                anyhow!(
                    "strictly read legacy Files embedding (CLIP era) {}: {error:?}",
                    hex::encode(handle.raw)
                )
            })?;
        } else if fact.a() == &embeddings::attr::embedding.id() {
            let handle = *fact.v::<Handle<embeddings::Embedding768>>();
            let _: anybytes::View<[f32]> = reader.get(handle).map_err(|error| {
                anyhow!(
                    "strictly read Files 768-d embedding {}: {error:?}",
                    hex::encode(handle.raw)
                )
            })?;
        } else if fact.a() == &embeddings::attr_mm7b::embedding.id() {
            let handle = *fact.v::<Handle<embeddings::Embedding3584>>();
            let _: anybytes::View<[f32]> = reader.get(handle).map_err(|error| {
                anyhow!(
                    "strictly read Files 3584-d embedding {}: {error:?}",
                    hex::encode(handle.raw)
                )
            })?;
        }
    }
    Ok(())
}

/// Project and validate one complete canonical Files collection value.
///
/// Every scalar used by a reader is checked for exact cardinality before any
/// record is returned. Imports cannot be partial, conflicting file names or
/// content cannot acquire an implicit winner, and directory extraction cannot
/// overwrite one same-named child with another.
pub fn load_catalog<R: BlobStoreGet>(reader: &R, facts: &TribleSet) -> Result<FilesCatalog> {
    validate_known_payloads(reader, facts)?;

    let file_ids = find!(
        id: Id,
        pattern!(facts, [{ ?id @ metadata::tag: &KIND_FILE }])
    )
    .collect::<BTreeSet<_>>();
    let directory_ids = find!(
        id: Id,
        pattern!(facts, [{ ?id @ metadata::tag: &KIND_DIRECTORY }])
    )
    .collect::<BTreeSet<_>>();
    let import_ids = find!(
        id: Id,
        pattern!(facts, [{ ?id @ metadata::tag: &KIND_IMPORT }])
    )
    .collect::<BTreeSet<_>>();

    for id in file_ids
        .intersection(&directory_ids)
        .chain(file_ids.intersection(&import_ids))
        .chain(directory_ids.intersection(&import_ids))
    {
        anyhow::bail!("Files entity {id:x} carries competing file/directory/import kinds");
    }

    let file_candidates =
        entities_with_attributes(facts, &[file::content.id(), file::media_type.id()]);
    if let Some(id) = file_candidates.difference(&file_ids).next() {
        anyhow::bail!("Files entity {id:x} carries file fields without KIND_FILE");
    }
    let directory_candidates = entities_with_attributes(facts, &[file::children.id()]);
    if let Some(id) = directory_candidates.difference(&directory_ids).next() {
        anyhow::bail!("Files entity {id:x} carries directory children without KIND_DIRECTORY");
    }
    let root_candidates = entities_with_attributes(facts, &[file::root.id()]);
    if let Some(id) = root_candidates.difference(&import_ids).next() {
        anyhow::bail!("Files entity {id:x} carries an import root without KIND_IMPORT");
    }
    let provenance_candidates =
        entities_with_attributes(facts, &[file::imported_at.id(), file::source_path.id()]);
    let provenance_owners = file_ids
        .union(&import_ids)
        .copied()
        .collect::<BTreeSet<_>>();
    if let Some(id) = provenance_candidates.difference(&provenance_owners).next() {
        anyhow::bail!(
            "Files entity {id:x} carries source_path/imported_at provenance without KIND_FILE or KIND_IMPORT"
        );
    }
    // Historical producers attached path/time provenance directly to some
    // file ids. It remains set-valued and outside canonical file identity,
    // but every timestamp still has to be a decodable point interval.
    for fact in facts
        .iter()
        .filter(|fact| fact.a() == &file::imported_at.id() && file_ids.contains(fact.e()))
    {
        decode_point_imported_at(*fact.v::<NsTAIInterval>(), "file", *fact.e())?;
    }
    let named_candidates = entities_with_attributes(facts, &[file::name.id()]);
    let named_nodes = file_ids
        .union(&directory_ids)
        .copied()
        .collect::<BTreeSet<_>>();
    if let Some(id) = named_candidates.difference(&named_nodes).next() {
        anyhow::bail!("Files entity {id:x} carries a file name without a file or directory kind");
    }

    let media_type_ids = find!(
        id: Id,
        pattern!(facts, [{ ?id @ metadata::tag: &KIND_MEDIA_TYPE }])
    )
    .collect::<BTreeSet<_>>();
    let mut media_type_names = BTreeMap::new();
    for &id in &media_type_ids {
        let handle = required_catalog_value(
            find!(
                name: NameHandle,
                pattern!(facts, [{ id @ metadata::name: ?name }])
            )
            .collect(),
            "media-type name",
            id,
        )?;
        let name = read_catalog_text(reader, handle, "media-type name", id)?;
        let normalized = normalize_media_type(&name)?;
        if normalized != name {
            anyhow::bail!("Files media-type entity {id:x} stores non-normalized name {name:?}");
        }
        media_type_names.insert(id, name);
    }

    let mut files = BTreeMap::new();
    for &id in &file_ids {
        let name_handle = required_catalog_value(
            find!(
                name: NameHandle,
                pattern!(facts, [{ id @ file::name: ?name }])
            )
            .collect(),
            "file name",
            id,
        )?;
        let content = required_catalog_value(
            find!(
                content: ContentHandle,
                pattern!(facts, [{ id @ file::content: ?content }])
            )
            .collect(),
            "file content",
            id,
        )?;
        let media_type = required_catalog_value(
            find!(
                media_type: Id,
                pattern!(facts, [{ id @ file::media_type: ?media_type }])
            )
            .collect(),
            "file media type",
            id,
        )?;
        if !media_type_names.contains_key(&media_type) {
            anyhow::bail!("Files file {id:x} points at unknown media type {media_type:x}");
        }
        files.insert(
            id,
            FileRecord {
                id,
                name: read_catalog_text(reader, name_handle, "file name", id)?,
                content,
                media_type,
            },
        );
    }

    let mut directories = BTreeMap::new();
    for &id in &directory_ids {
        let name_handle = required_catalog_value(
            find!(
                name: NameHandle,
                pattern!(facts, [{ id @ file::name: ?name }])
            )
            .collect(),
            "directory name",
            id,
        )?;
        let children = find!(
            child: Id,
            pattern!(facts, [{ id @ file::children: ?child }])
        )
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect();
        directories.insert(
            id,
            DirectoryRecord {
                id,
                name: read_catalog_text(reader, name_handle, "directory name", id)?,
                children,
            },
        );
    }

    for directory in directories.values() {
        let mut child_names = BTreeMap::<&str, Id>::new();
        for child in &directory.children {
            let name = if let Some(file) = files.get(child) {
                file.name.as_str()
            } else if let Some(child_directory) = directories.get(child) {
                child_directory.name.as_str()
            } else {
                anyhow::bail!(
                    "Files directory {:x} names unknown child {child:x}",
                    directory.id
                );
            };
            if let Some(previous) = child_names.insert(name, *child) {
                anyhow::bail!(
                    "Files directory {:x} has same-named children {previous:x} and {child:x} ({name:?})",
                    directory.id
                );
            }
        }
    }
    let mut visiting = BTreeSet::new();
    let mut visited = BTreeSet::new();
    for id in directories.keys().copied() {
        validate_directory_acyclic(id, &directories, &mut visiting, &mut visited)?;
    }

    let mut imports = BTreeMap::new();
    for &id in &import_ids {
        let imported_at = required_catalog_value(
            find!(
                value: ImportTime,
                pattern!(facts, [{ id @ file::imported_at: ?value }])
            )
            .collect(),
            "imported_at",
            id,
        )?;
        let imported_at = decode_point_imported_at(imported_at, "import", id)?;
        let source_path = required_catalog_value(
            find!(
                value: NameHandle,
                pattern!(facts, [{ id @ file::source_path: ?value }])
            )
            .collect(),
            "source_path",
            id,
        )?;
        let root = required_catalog_value(
            find!(
                value: Id,
                pattern!(facts, [{ id @ file::root: ?value }])
            )
            .collect(),
            "root",
            id,
        )?;
        if !files.contains_key(&root) && !directories.contains_key(&root) {
            anyhow::bail!("Files import {id:x} points at unknown root {root:x}");
        }
        let tags = find!(
            value: Inline<ShortString>,
            pattern!(facts, [{ id @ file::tag: ?value }])
        )
        .map(|value| {
            String::try_from_inline(&value)
                .map_err(|error| anyhow!("decode Files tag for {id:x}: {error:?}"))
        })
        .collect::<Result<BTreeSet<_>>>()?
        .into_iter()
        .collect();
        imports.insert(
            id,
            ImportRecord {
                id,
                imported_at,
                source_path: read_catalog_text(reader, source_path, "source_path", id)?,
                root,
                tags,
            },
        );
    }

    Ok(FilesCatalog {
        files,
        directories,
        imports,
    })
}

/// Validate one complete Files value without retaining its projection.
pub fn validate_catalog<R: BlobStoreGet>(reader: &R, facts: &TribleSet) -> Result<()> {
    load_catalog(reader, facts).map(drop)
}

/// Construct the canonical intrinsic entity for one normalized IANA media
/// type. Other faculties use this when describing bytes which are not Files
/// records while still sharing Files' media-type vocabulary.
pub fn media_type_fragment(media_type: &str) -> Result<Fragment> {
    let media_type = normalize_media_type(media_type)?;
    let mut fragment = Fragment::empty();
    let name = fragment.put::<UTF8String, _>(media_type);
    fragment += entity! {
        metadata::tag: &KIND_MEDIA_TYPE,
        metadata::name: name,
    };
    Ok(fragment)
}

/// Build one self-contained canonical file fragment.
///
/// `name` is a leaf name supplied by the caller, not a filesystem path. The
/// returned [`Fragment`] owns the content, name, and media-type-name blobs, so
/// callers can safely compose it into a larger fragment and publish that one
/// ownership unit through the native collection API.
pub fn stage<T>(bytes: T, name: impl Into<String>, media_type: &str) -> Result<Fragment>
where
    T: triblespace::core::blob::IntoBlob<RawBytes>,
{
    let media_type = normalize_media_type(media_type)?;
    let mut fragment = Fragment::empty();
    let content = fragment.put::<RawBytes, _>(bytes);
    let name = fragment.put::<UTF8String, _>(leaf_name(&name.into()));
    let media_type_name = fragment.put::<UTF8String, _>(media_type);
    let media_type = entity! {
        metadata::tag: &KIND_MEDIA_TYPE,
        metadata::name: media_type_name,
    };

    // Spreading the child fragment consumes its exported id into the relation
    // while folding its facts into the returned fragment. The file remains the
    // fragment's sole exported root.
    fragment += entity! {
        metadata::tag: &KIND_FILE,
        file::content: content,
        file::name: name,
        file::media_type*: media_type,
    };
    Ok(fragment)
}

/// Materialize the complete WRITE-authorized Files collection through an already
/// open pile.
///
/// Opening the scope introduces no second pile handle or lifecycle policy.
/// The returned immutable reader is the same coherent snapshot which admitted
/// the materialized facts.
pub fn materialize_collection(
    pile: &mut Pile,
    signer: &SigningKey,
) -> Result<(TribleSet, PileSnapshot)> {
    let collection = open_configured(
        pile,
        crate::schemas::files::DEFAULT_SCOPE_ID,
        signer.verifying_key(),
    )?;
    let store_snapshot = pile.snapshot().context("freeze Files store snapshot")?;
    let facts = store_snapshot
        .collection(collection)
        .context("attach Files collection")?
        .view::<TribleSet>()
        .context("read Files collection")?;
    Ok((facts, store_snapshot))
}

/// Publish one complete Files fragment through an already open pile.
///
/// This is the Files-first boundary used by attachment producers. The signed
/// record is appended before this returns; the caller retains ownership of the
/// pile's flush/close boundary.
pub fn commit_collection(
    pile: &mut Pile,
    signer: &SigningKey,
    fragment: Fragment,
) -> Result<CollectionCommit> {
    let collection = open_configured(
        pile,
        crate::schemas::files::DEFAULT_SCOPE_ID,
        signer.verifying_key(),
    )?;
    pile.commit(collection, signer, fragment)
        .map_err(|error| anyhow!("commit Files collection fragment: {error}"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use ed25519_dalek::SigningKey;
    use rand_core::OsRng;
    use triblespace::core::repo::memoryrepo::MemoryRepo;
    use triblespace::core::repo::BlobStoreGet;

    fn content_of(fragment: &Fragment) -> ContentHandle {
        find!(
            (content: ContentHandle),
            pattern!(fragment, [{ _?file @ file::content: ?content }])
        )
        .next()
        .expect("file content")
        .0
    }

    fn media_type_of(fragment: &Fragment) -> Id {
        find!(
            (media_type: Id),
            pattern!(fragment, [{ _?file @ file::media_type: ?media_type }])
        )
        .next()
        .expect("file media type")
        .0
    }

    fn facts_of(fragments: impl IntoIterator<Item = Fragment>) -> TribleSet {
        let mut facts = TribleSet::new();
        for fragment in fragments {
            facts += fragment;
        }
        facts
    }

    fn imported_file_fragment(path: &str) -> (Fragment, Id, Id) {
        let mut fragment = stage(b"imported bytes".to_vec(), "report.txt", "text/plain").unwrap();
        let root = fragment.root().unwrap();
        let source_path = fragment.put::<UTF8String, _>(path.to_owned());
        let instant = Epoch::from_tai_seconds(42.0);
        let imported_at: ImportTime = (instant, instant).try_to_inline().unwrap();
        let import = entity! {
            metadata::tag: &KIND_IMPORT,
            file::root: &root,
            file::imported_at: imported_at,
            file::source_path: source_path,
        };
        let import_id = import.root().unwrap();
        fragment += import;
        (fragment, root, import_id)
    }

    #[test]
    fn catalog_projects_complete_imports_without_scalar_arbitration() {
        let (fragment, root, import_id) = imported_file_fragment("/tmp/report.txt");
        let mut blobs = fragment.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let catalog = load_catalog(&reader, fragment.facts()).unwrap();
        let import = catalog.import(import_id).unwrap();

        assert_eq!(import.root, root);
        assert_eq!(import.source_path, "/tmp/report.txt");
        assert_eq!(catalog.files().len(), 1);
        assert_eq!(catalog.imports().len(), 1);
    }

    #[test]
    fn catalog_preserves_set_valued_legacy_file_provenance() {
        let mut fragment = stage(b"legacy bytes".to_vec(), "legacy.txt", "text/plain").unwrap();
        let file_id = fragment.root().unwrap();
        let first_path = fragment.put::<UTF8String, _>("mail:first".to_owned());
        let second_path = fragment.put::<UTF8String, _>("mail:second".to_owned());
        let first_time = Epoch::from_tai_seconds(41.0);
        let first_time: ImportTime = (first_time, first_time).try_to_inline().unwrap();
        let second_time = Epoch::from_tai_seconds(42.0);
        let second_time: ImportTime = (second_time, second_time).try_to_inline().unwrap();
        fragment += entity! { ExclusiveId::force_ref(&file_id) @
            file::source_path: first_path,
            file::imported_at: first_time,
        };
        fragment += entity! { ExclusiveId::force_ref(&file_id) @
            file::source_path: second_path,
            file::imported_at: second_time,
        };
        let mut blobs = fragment.blobs().clone();
        let reader = blobs.snapshot().unwrap();

        let catalog = load_catalog(&reader, fragment.facts()).unwrap();

        assert!(catalog.file(file_id).is_some());
        assert_eq!(catalog.files().len(), 1);
        assert_eq!(catalog.imports().len(), 0);
    }

    #[test]
    fn catalog_rejects_non_point_file_provenance_timestamps() {
        let mut fragment = stage(b"legacy bytes".to_vec(), "legacy.txt", "text/plain").unwrap();
        let file_id = fragment.root().unwrap();
        let start = Epoch::from_tai_seconds(41.0);
        let end = Epoch::from_tai_seconds(42.0);
        let interval: ImportTime = (start, end).try_to_inline().unwrap();
        fragment += entity! { ExclusiveId::force_ref(&file_id) @ file::imported_at: interval };
        let mut blobs = fragment.blobs().clone();
        let reader = blobs.snapshot().unwrap();

        let error = load_catalog(&reader, fragment.facts()).unwrap_err();

        assert!(format!("{error:#}").contains("non-point imported_at interval"));
    }

    #[test]
    fn catalog_rejects_provenance_or_roots_on_untyped_entities() {
        let mut with_path = stage(b"typed".to_vec(), "typed.txt", "text/plain").unwrap();
        let unknown = Id::new([0x53; 16]).unwrap();
        let path = with_path.put::<UTF8String, _>("unknown:path".to_owned());
        with_path += entity! { ExclusiveId::force_ref(&unknown) @ file::source_path: path };
        let mut blobs = with_path.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, with_path.facts()).unwrap_err();
        assert!(format!("{error:#}")
            .contains("source_path/imported_at provenance without KIND_FILE or KIND_IMPORT"));

        let mut with_time = stage(b"typed".to_vec(), "typed.txt", "text/plain").unwrap();
        let instant = Epoch::from_tai_seconds(43.0);
        let instant: ImportTime = (instant, instant).try_to_inline().unwrap();
        with_time += entity! { ExclusiveId::force_ref(&unknown) @ file::imported_at: instant };
        let mut blobs = with_time.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, with_time.facts()).unwrap_err();
        assert!(format!("{error:#}")
            .contains("source_path/imported_at provenance without KIND_FILE or KIND_IMPORT"));

        let mut with_root = stage(b"typed".to_vec(), "typed.txt", "text/plain").unwrap();
        let root = with_root.root().unwrap();
        with_root += entity! { ExclusiveId::force_ref(&unknown) @ file::root: &root };
        let mut blobs = with_root.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, with_root.facts()).unwrap_err();
        assert!(format!("{error:#}").contains("import root without KIND_IMPORT"));
    }

    #[test]
    fn catalog_rejects_partial_and_conflicting_imports() {
        let (mut conflicting, _, import_id) = imported_file_fragment("/tmp/report.txt");
        let alternate = Epoch::from_tai_seconds(43.0);
        let alternate: ImportTime = (alternate, alternate).try_to_inline().unwrap();
        conflicting +=
            entity! { ExclusiveId::force_ref(&import_id) @ file::imported_at: alternate };
        let mut blobs = conflicting.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, conflicting.facts()).unwrap_err();
        assert!(format!("{error:#}").contains("2 values for imported_at"));

        let (mut conflicting, _, import_id) = imported_file_fragment("/tmp/report.txt");
        let alternate = conflicting.put::<UTF8String, _>("/tmp/alternate.txt".to_owned());
        conflicting +=
            entity! { ExclusiveId::force_ref(&import_id) @ file::source_path: alternate };
        let mut blobs = conflicting.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, conflicting.facts()).unwrap_err();
        assert!(format!("{error:#}").contains("2 values for source_path"));

        let (mut conflicting, _, import_id) = imported_file_fragment("/tmp/report.txt");
        let alternate_root = Id::new([0x51; 16]).unwrap();
        conflicting += entity! { ExclusiveId::force_ref(&import_id) @ file::root: &alternate_root };
        let mut blobs = conflicting.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, conflicting.facts()).unwrap_err();
        assert!(format!("{error:#}").contains("2 values for root"));

        let file = stage(b"partial bytes".to_vec(), "partial.txt", "text/plain").unwrap();
        let root = file.root().unwrap();
        let partial_id = Id::new([0x52; 16]).unwrap();
        let mut partial = file;
        partial += entity! { ExclusiveId::force_ref(&partial_id) @
            metadata::tag: &KIND_IMPORT,
            file::root: &root,
        };
        let mut blobs = partial.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, partial.facts()).unwrap_err();
        assert!(format!("{error:#}").contains("0 values for imported_at"));
    }

    #[test]
    fn catalog_rejects_competing_file_name_and_content() {
        let mut fragment = stage(b"named".to_vec(), "record.txt", "text/plain").unwrap();
        let file_id = fragment.root().unwrap();
        let competing = fragment.put::<UTF8String, _>("alternate.txt".to_owned());
        fragment += entity! { ExclusiveId::force_ref(&file_id) @ file::name: competing };
        let mut blobs = fragment.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let error = load_catalog(&reader, fragment.facts()).unwrap_err();
        assert!(format!("{error:#}").contains("2 values for file name"));

        let mut fragment = stage(b"first".to_vec(), "record.txt", "text/plain").unwrap();
        let file_id = fragment.root().unwrap();
        let competing = fragment.put::<RawBytes, _>(b"second".to_vec());
        fragment += entity! { ExclusiveId::force_ref(&file_id) @ file::content: competing };
        let mut blobs = fragment.blobs().clone();
        let reader = blobs.snapshot().unwrap();

        let error = load_catalog(&reader, fragment.facts()).unwrap_err();
        assert!(format!("{error:#}").contains("2 values for file content"));
    }

    #[test]
    fn shared_content_keeps_all_names_and_has_no_name_winner() {
        let first = stage(b"shared".to_vec(), "alpha.txt", "text/plain").unwrap();
        let content = content_of(&first);
        let second = stage(b"shared".to_vec(), "beta.txt", "text/plain").unwrap();
        let mut fragment = first;
        fragment += second;
        let mut blobs = fragment.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let catalog = load_catalog(&reader, fragment.facts()).unwrap();

        let resolved = catalog.resolve_file(&content_hash_hex(content)).unwrap();
        assert_eq!(resolved.names, ["alpha.txt", "beta.txt"]);
        assert_eq!(resolved.unique_name(), None);
    }

    #[test]
    fn complete_entity_ids_are_direct_and_case_insensitive() {
        let expected = Id::from_hex("abcdef0123456789abcdef0123456789").unwrap();
        assert_eq!(
            resolve_selector(&TribleSet::new(), "files:ABCDEF0123456789ABCDEF0123456789").unwrap(),
            expected
        );
        assert_eq!(
            resolve_reference(&TribleSet::new(), "files:ABCDEF0123456789ABCDEF0123456789").unwrap(),
            FileReference::Entity(expected)
        );
    }

    #[test]
    fn entity_and_content_prefixes_resolve_without_a_minimum_length() {
        let file = stage(b"selector bytes".to_vec(), "selector.txt", "text/plain").unwrap();
        let file_id = file.root().unwrap();
        let content = content_of(&file);
        let hash = content_hash_hex(content);
        let facts = facts_of([file]);
        let id_hex = format!("{file_id:x}");
        assert_eq!(hash, hash.to_ascii_lowercase());

        assert_eq!(
            resolve_selector(&facts, &id_hex[..1].to_ascii_uppercase()).unwrap(),
            file_id
        );
        assert_eq!(
            resolve_selector(&facts, &id_hex[..12].to_ascii_uppercase()).unwrap(),
            file_id
        );
        assert_eq!(
            resolve_selector(
                &facts,
                &format!("files:{}", hash[..20].to_ascii_uppercase())
            )
            .unwrap(),
            file_id
        );
        assert_eq!(
            resolve_selector(&facts, &hash.to_ascii_uppercase()).unwrap(),
            file_id
        );

        let id_reference_prefix = (1..32)
            .map(|len| &id_hex[..len])
            .find(|prefix| {
                resolve_reference(&facts, prefix).ok() == Some(FileReference::Entity(file_id))
            })
            .expect("entity reference has an unambiguous prefix");
        assert_eq!(
            resolve_reference(&facts, &id_reference_prefix.to_ascii_uppercase()).unwrap(),
            FileReference::Entity(file_id)
        );
        assert_eq!(
            resolve_reference(&facts, &hash[..40].to_ascii_uppercase()).unwrap(),
            FileReference::Content(content)
        );
    }

    #[test]
    fn duplicate_content_is_ambiguous_by_entity_even_for_a_complete_hash() {
        let first = stage(b"shared bytes".to_vec(), "first.txt", "text/plain").unwrap();
        let second = stage(b"shared bytes".to_vec(), "second.txt", "text/plain").unwrap();
        let content = content_of(&first);
        let hash = content_hash_hex(content);
        let mut expected = vec![first.root().unwrap(), second.root().unwrap()];
        expected.sort();
        let facts = facts_of([first, second]);

        let message = resolve_selector(&facts, &hash).unwrap_err().to_string();
        assert!(message.contains("ambiguous"));
        for candidate in expected {
            assert!(message.contains(&format!("{candidate:x}")));
        }

        assert_eq!(
            resolve_reference(&facts, &hash).unwrap(),
            FileReference::Content(content)
        );
        assert_eq!(
            resolve_reference(&facts, &hash[..40]).unwrap(),
            FileReference::Content(content)
        );
        assert_eq!(
            resolve_reference(&TribleSet::new(), &hash).unwrap(),
            FileReference::Content(content)
        );
    }

    #[test]
    fn directory_and_import_ids_are_prefix_eligible() {
        let directory_id = *fucid();
        let import_id = *fucid();
        let directory = entity! { ExclusiveId::force_ref(&directory_id) @
            metadata::tag: &KIND_DIRECTORY,
        };
        let import = entity! { ExclusiveId::force_ref(&import_id) @
            metadata::tag: &KIND_IMPORT,
        };

        let directory_facts = facts_of([directory]);
        let directory_hex = format!("{directory_id:x}");
        assert_eq!(
            resolve_selector(&directory_facts, &directory_hex[..1]).unwrap(),
            directory_id
        );

        let import_facts = facts_of([import]);
        let import_hex = format!("{import_id:x}");
        assert_eq!(
            resolve_selector(&import_facts, &import_hex[..1]).unwrap(),
            import_id
        );
    }

    #[test]
    fn identical_file_records_have_identical_intrinsic_ids() {
        let left = stage(b"same bytes".to_vec(), "report.txt", "text/plain").unwrap();
        let right = stage(b"same bytes".to_vec(), "report.txt", "text/plain").unwrap();

        assert_eq!(left.root(), right.root());
        assert_eq!(left, right);
    }

    #[test]
    fn name_and_media_type_are_identity_but_bytes_still_deduplicate() {
        let text = stage(b"same bytes".to_vec(), "report.txt", "text/plain").unwrap();
        let renamed = stage(b"same bytes".to_vec(), "renamed.txt", "text/plain").unwrap();
        let retyped = stage(
            b"same bytes".to_vec(),
            "report.txt",
            "application/octet-stream",
        )
        .unwrap();

        assert_ne!(text.root(), renamed.root());
        assert_ne!(text.root(), retyped.root());
        assert_eq!(content_of(&text), content_of(&renamed));
        assert_eq!(content_of(&text), content_of(&retyped));
        assert_eq!(media_type_of(&text), media_type_of(&renamed));
        assert_ne!(media_type_of(&text), media_type_of(&retyped));
    }

    #[test]
    fn containing_paths_never_enter_file_identity() {
        let unix = stage(
            b"same bytes".to_vec(),
            "/tmp/archive/report.txt",
            "text/plain",
        )
        .unwrap();
        let windows = stage(
            b"same bytes".to_vec(),
            r"C:\archive\report.txt",
            "text/plain",
        )
        .unwrap();
        let bare = stage(b"same bytes".to_vec(), "report.txt", "text/plain").unwrap();

        assert_eq!(unix.root(), bare.root());
        assert_eq!(windows.root(), bare.root());
    }

    #[test]
    fn invalid_media_type_is_reported_instead_of_panicking() {
        let error = stage(
            Vec::<u8>::new(),
            "document.bin",
            "application/invalid\0mime",
        )
        .unwrap_err();
        assert!(error.to_string().contains("invalid file media type"));
    }

    #[test]
    fn media_type_normalization_is_lossless_and_deterministic() {
        assert_eq!(normalize_media_type("text/plain").unwrap(), "text/plain");
        assert_eq!(
            normalize_media_type(" TEXT/PLAIN; charset=UTF-8").unwrap(),
            "text/plain"
        );
        let vendor = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
        assert_eq!(normalize_media_type(vendor).unwrap(), vendor);
        assert_eq!(
            normalize_media_type_or_default("not a media type"),
            DEFAULT_MEDIA_TYPE
        );
    }

    #[test]
    fn media_type_inference_is_case_insensitive_and_shared() {
        assert_eq!(infer_media_type(Path::new("README.MD")), "text/markdown");
        assert_eq!(
            infer_media_type(Path::new("deck.PPTX")),
            "application/vnd.openxmlformats-officedocument.presentationml.presentation"
        );
        assert_eq!(infer_media_type(Path::new("unknown")), DEFAULT_MEDIA_TYPE);
    }

    #[test]
    fn normalized_variants_converge_and_long_suffixes_remain_identity() {
        let plain = stage(b"same bytes".to_vec(), "document.bin", "text/plain").unwrap();
        let decorated = stage(
            b"same bytes".to_vec(),
            "document.bin",
            " TEXT/PLAIN; charset=UTF-8",
        )
        .unwrap();
        assert_eq!(plain.root(), decorated.root());
        assert_eq!(media_type_of(&plain), media_type_of(&decorated));

        let document = stage(
            b"same bytes".to_vec(),
            "document.bin",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        )
        .unwrap();
        let sheet = stage(
            b"same bytes".to_vec(),
            "document.bin",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        )
        .unwrap();
        assert_ne!(document.root(), sheet.root());
        assert_ne!(media_type_of(&document), media_type_of(&sheet));
    }

    #[test]
    fn staged_fragment_owns_content_name_and_joinable_media_type_name() {
        let file = stage(
            b"slides".to_vec(),
            "deck.pptx",
            "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        )
        .unwrap();
        let file_id = file.root().expect("file root");
        let content = content_of(&file);
        let file_name = find!(
            name: NameHandle,
            pattern!(&file, [{ file_id @ file::name: ?name }])
        )
        .next()
        .expect("file name");
        let media_type_id = find!(
            (media_type: Id),
            pattern!(&file, [{ file_id @ file::media_type: ?media_type }])
        )
        .next()
        .expect("media type relation")
        .0;

        assert!(exists!(
            (name: NameHandle),
            pattern!(&file, [{ media_type_id @ metadata::tag: &KIND_MEDIA_TYPE, metadata::name: ?name }])
        ));
        let name_handle = media_type_name_handle(file.facts(), file_id).expect("media type name");
        assert_eq!(
            Some(name_handle),
            find!(
                (name: NameHandle),
                pattern!(&file, [{ media_type_id @ metadata::name: ?name }])
            )
            .next()
            .map(|(name,)| name)
        );
        let mut blobs = file.blobs().clone();
        let reader = blobs.snapshot().expect("fragment blob reader");
        let name: anybytes::View<str> = reader
            .get::<anybytes::View<str>, UTF8String>(name_handle)
            .expect("staged media type name");
        assert_eq!(
            name.as_ref(),
            "application/vnd.openxmlformats-officedocument.presentationml.presentation"
        );
        let content: anybytes::Bytes = reader.get(content).expect("staged content");
        assert_eq!(content.as_ref(), b"slides");
        let file_name: anybytes::View<str> = reader.get(file_name).expect("staged name");
        assert_eq!(file_name.as_ref(), "deck.pptx");
    }

    #[test]
    fn complete_fragment_survives_native_collection_commit_and_materialization() {
        let signer = SigningKey::generate(&mut OsRng);
        let mut store = MemoryRepo::default();
        let collection = crate::collection_names::open(
            &mut store,
            crate::schemas::files::DEFAULT_SCOPE_ID,
            signer.verifying_key(),
        )
        .unwrap();
        let file = stage(
            b"slides".to_vec(),
            "deck.pptx",
            "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        )
        .unwrap();
        let file_id = file.root().expect("file root");

        // The Fragment is the ownership unit: collapsing it into a TribleSet
        // would discard its attachment store before commit can persist the
        // closure.
        store
            .commit(collection, &signer, file)
            .expect("commit canonical file");
        let reader = store.snapshot().expect("freeze files store snapshot");
        let (catalog, _) =
            crate::storage::read_fact_collection(collection, &reader).expect("materialize files");
        let content_handle = find!(
            content: ContentHandle,
            pattern!(&catalog, [{ file_id @ file::content: ?content }])
        )
        .next()
        .expect("persisted content handle");
        let file_name_handle = find!(
            name: NameHandle,
            pattern!(&catalog, [{ file_id @ file::name: ?name }])
        )
        .next()
        .expect("persisted file name handle");
        let name_handle = media_type_name_handle(&catalog, file_id).expect("canonical media type");
        let name: anybytes::View<str> = reader
            .get::<anybytes::View<str>, UTF8String>(name_handle)
            .expect("persisted media type name");
        assert_eq!(
            name.as_ref(),
            "application/vnd.openxmlformats-officedocument.presentationml.presentation"
        );
        let content: anybytes::Bytes = reader.get(content_handle).expect("persisted content");
        assert_eq!(content.as_ref(), b"slides");
        let file_name: anybytes::View<str> =
            reader.get(file_name_handle).expect("persisted file name");
        assert_eq!(file_name.as_ref(), "deck.pptx");
    }
}