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
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
//! Canonical constructors for the Archive block DAG.
//!
//! Each constructor returns a self-contained [`Fragment`]. Child fragments
//! are spread into their parent, consuming the child's exports while retaining
//! all facts and blobs. The resulting root therefore follows the data model:
//!
//! ```text
//! content fact -> content part -> block -> source projection
//! ```
//!
//! Kind markers and source annotations are attached only after intrinsic
//! construction. They remain queryable without becoming hidden inputs to a
//! content-derived id.

use std::collections::{BTreeMap, BTreeSet};

use anybytes::Bytes;
use anyhow::{anyhow, bail, Result};
use triblespace::core::blob::encodings::succinctarchive::{OrderedUniverse, UnionArchive};
use triblespace::core::blob::MemoryBlobStoreSnapshot;
use triblespace::core::inline::IntoInline;
use triblespace::core::metadata;
use triblespace::core::query::TriblePattern;
use triblespace::core::repo::pile::PileSnapshot;
use triblespace::core::repo::{BlobStoreGet, BlobStoreList, SnapshotSource};
use triblespace::prelude::blobencodings::{RawBytes, UTF8String};
use triblespace::prelude::inlineencodings::{Handle, NsTAIInterval, U256BE};
use triblespace::prelude::*;

use crate::files;
use crate::schemas::{blockdag as schema, files as files_schema};

/// Source-occurrence evidence that must not affect the projected block or
/// projection-receipt identity.
///
/// Every field is additive. `None` means that the source supplied no usable
/// evidence; it never inserts a sentinel value.
#[derive(Clone, Debug, Default)]
pub struct ProjectionAnnotations {
    /// Source receipts that additively support the projected block's semantic
    /// predecessor classes. This is not exact vendor occurrence adjacency;
    /// that evidence remains in the exact raw record.
    pub semantic_predecessor_support: Vec<Id>,
    /// Genuine source timestamp claim, if independently decodable.
    pub source_timestamp: Option<Inline<NsTAIInterval>>,
    /// Stable Relations entity that produced this occurrence.
    pub author: Option<Id>,
    /// Stable Relations entity whose stream observed or produced it.
    pub experiencer: Option<Id>,
    /// Exact source spelling of the author field.
    pub raw_author: Option<String>,
    /// Exact source spelling of the role field.
    pub raw_role: Option<String>,
    /// Exact source spelling of the model field.
    pub raw_model: Option<String>,
    /// Movable source path. This reuses Files' occurrence-level attribute.
    pub source_path: Option<String>,
}

type TextHandle = Inline<Handle<UTF8String>>;
type RawHandle = Inline<Handle<RawBytes>>;
type IntervalValue = Inline<NsTAIInterval>;
type OrdinalValue = Inline<U256BE>;
type OverlayReader = MemoryBlobStoreSnapshot;

/// Result of validating one exact materialized Archive catalog.
///
/// `Pending` is deliberately limited to attachment residency: every asserted
/// handle already fixes the missing bytes, so those bytes may arrive without
/// changing the catalog's denotation. Missing graph entities and malformed
/// facts are [`Rejected`](Self::Rejected), because repairing them requires a
/// different set of tribles.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CatalogValidation {
    /// Structure, intrinsic identities, closure, and attachment residency are
    /// canonical. Durable payload hashes and decodings remain lazy until the
    /// bytes are consumed; newly staged payloads are validated before publish.
    Accepted,
    /// The graph is canonical, but these content hashes are not resident yet.
    Pending { missing: BTreeSet<[u8; 32]> },
    /// A deterministic structural, hashing, or attachment-format violation.
    Rejected(String),
}

impl CatalogValidation {
    /// True only for a fully resident, canonical catalog.
    pub fn is_accepted(&self) -> bool {
        matches!(self, Self::Accepted)
    }
}

#[derive(Default)]
struct AttachmentPlan {
    texts: BTreeSet<TextHandle>,
    raws: BTreeSet<RawHandle>,
    media_type_names: BTreeSet<TextHandle>,
    source_chunks: BTreeMap<Id, (u128, RawHandle)>,
    source_snapshots: Vec<SourceSnapshotPlan>,
}

struct SourceSnapshotPlan {
    id: Id,
    byte_length: u128,
    chunks: Vec<Id>,
}

fn rooted(fragment: &Fragment, what: &str) -> Result<Id> {
    fragment
        .root()
        .ok_or_else(|| anyhow!("{what} must export exactly one root"))
}

fn attach_kind(mut fragment: Fragment, kind: Id, what: &str) -> Result<Fragment> {
    let root = rooted(&fragment, what)?;
    fragment += entity! { ExclusiveId::force_ref(&root) @ metadata::tag: &kind };
    Ok(fragment)
}

/// Queryable names for the reified Archive modality and direction tags.
///
/// These facts are ordinary collection data rather than renderer knowledge.
/// Repeating them in authored imports is harmless set duplication, and lets a
/// reader display a newly introduced tag without adding another match table.
pub fn vocabulary_fragment() -> Fragment {
    let mut fragment = Fragment::empty();
    for &(id, name) in schema::content_fact::modality::SPECS
        .iter()
        .chain(schema::content_fact::direction::SPECS)
    {
        let name = fragment.put::<UTF8String, _>(name.to_owned());
        fragment += entity! { ExclusiveId::force_ref(&id) @
            metadata::tag: &metadata::KIND_TAG,
            metadata::name: name,
        };
    }
    fragment
}

/// Construct one intrinsic byte range inside a frozen source snapshot.
pub fn source_chunk(offset: u128, bytes: Bytes) -> Result<Fragment> {
    let offset: Inline<U256BE> = offset.to_inline();
    let mut fragment = Fragment::empty();
    let bytes = fragment.put::<RawBytes, _>(bytes);
    fragment += entity! { _ @
        schema::source_chunk::offset: offset,
        schema::source_chunk::bytes: bytes,
    };
    attach_kind(fragment, schema::source_chunk::KIND, "source chunk")
}

/// Construct one exact frozen-source snapshot over intrinsic byte ranges.
///
/// `chunks` exports [`schema::source_chunk`] roots. Their offsets and byte
/// handles determine ordering and reconstruction; the snapshot's total length
/// makes validation independent of import history or collection COMMIT grouping.
pub fn source_snapshot(
    namespace: Id,
    locator: impl Into<String>,
    byte_length: u128,
    chunks: Fragment,
    source_path: Option<String>,
) -> Result<Fragment> {
    let byte_length: Inline<U256BE> = byte_length.to_inline();
    let mut fragment = Fragment::empty();
    let locator = fragment.put::<UTF8String, _>(locator.into());
    fragment += entity! { _ @
        schema::source_projection::source_namespace: &namespace,
        schema::source_projection::source_locator: locator,
        schema::source_snapshot::byte_length: byte_length,
        schema::source_snapshot::contains*: chunks,
    };
    let mut fragment = attach_kind(fragment, schema::source_snapshot::KIND, "source snapshot")?;
    if let Some(source_path) = source_path {
        let snapshot = rooted(&fragment, "source snapshot")?;
        let path = fragment.put::<UTF8String, _>(source_path);
        fragment += entity! { ExclusiveId::force_ref(&snapshot) @
            files_schema::file::source_path: path,
        };
    }
    Ok(fragment)
}

/// Construct one intrinsic textual content fact.
pub fn text_fact(modality: Id, direction: Id, text: impl Into<String>) -> Result<Fragment> {
    text_fact_blob(modality, direction, text.into())
}

/// Construct one intrinsic textual fact while retaining a source-backed view.
pub fn text_fact_view(modality: Id, direction: Id, text: anybytes::View<str>) -> Result<Fragment> {
    text_fact_blob(modality, direction, text)
}

fn text_fact_blob<T>(modality: Id, direction: Id, text: T) -> Result<Fragment>
where
    T: triblespace::core::blob::IntoBlob<UTF8String>,
{
    let mut fragment = Fragment::empty();
    let payload = fragment.put::<UTF8String, _>(text);
    fragment += entity! { _ @
        schema::content_fact::modality: &modality,
        schema::content_fact::direction: &direction,
        schema::content_fact::payload: payload,
    };
    attach_kind(fragment, schema::content_fact::KIND, "content fact")
}

/// Construct one intrinsic resident binary content fact.
///
/// The media type is normalized by Files and represented by the same
/// intrinsic media-type entity used by canonical file records.
pub fn blob_fact<T>(modality: Id, direction: Id, bytes: T, media_type: &str) -> Result<Fragment>
where
    T: triblespace::core::blob::IntoBlob<RawBytes>,
{
    let mut fragment = Fragment::empty();
    let blob = fragment.put::<RawBytes, _>(bytes);
    let media_type = files::media_type_fragment(media_type)?;
    fragment += entity! { _ @
        schema::content_fact::modality: &modality,
        schema::content_fact::direction: &direction,
        schema::content_fact::blob: blob,
        schema::content_fact::media_type*: media_type,
    };
    attach_kind(fragment, schema::content_fact::KIND, "content fact")
}

/// Construct one intrinsic unresolved external-asset fact.
///
/// A pointer is always scoped by its source namespace. Media type and claimed
/// byte size participate only when genuinely known.
pub fn asset_pointer_fact(
    modality: Id,
    direction: Id,
    asset_namespace: Id,
    pointer: impl Into<String>,
    media_type: Option<&str>,
    size: Option<u128>,
) -> Result<Fragment> {
    asset_pointer_fact_blob(
        modality,
        direction,
        asset_namespace,
        pointer.into(),
        media_type,
        size,
    )
}

/// Construct an unresolved asset fact from a source-backed pointer view.
pub fn asset_pointer_fact_view(
    modality: Id,
    direction: Id,
    asset_namespace: Id,
    pointer: anybytes::View<str>,
    media_type: Option<&str>,
    size: Option<u128>,
) -> Result<Fragment> {
    asset_pointer_fact_blob(
        modality,
        direction,
        asset_namespace,
        pointer,
        media_type,
        size,
    )
}

fn asset_pointer_fact_blob<P>(
    modality: Id,
    direction: Id,
    asset_namespace: Id,
    pointer: P,
    media_type: Option<&str>,
    size: Option<u128>,
) -> Result<Fragment>
where
    P: triblespace::core::blob::IntoBlob<UTF8String>,
{
    let mut fragment = Fragment::empty();
    let pointer = fragment.put::<UTF8String, _>(pointer);
    let media_type = media_type
        .map(files::media_type_fragment)
        .transpose()?
        .unwrap_or_else(Fragment::empty);
    fragment += entity! { _ @
        schema::content_fact::modality: &modality,
        schema::content_fact::direction: &direction,
        schema::content_fact::asset_namespace: &asset_namespace,
        schema::content_fact::asset_pointer: pointer,
        schema::content_fact::media_type*: media_type,
        schema::content_fact::asset_size?: size,
    };
    attach_kind(fragment, schema::content_fact::KIND, "content fact")
}

/// Add one recovered byte representation to a pointer-identified content fact.
///
/// The intrinsic fact root remains unchanged. Multiple distinct resolutions
/// are retained as explicit ambiguity for validation rather than resolved by
/// last-write-wins.
pub fn resolve_pointer_fact<T>(mut fact: Fragment, bytes: T) -> Result<Fragment>
where
    T: triblespace::core::blob::IntoBlob<RawBytes>,
{
    let root = rooted(&fact, "content fact")?;
    let resolved = fact.put::<RawBytes, _>(bytes);
    fact += entity! { ExclusiveId::force_ref(&root) @
        schema::content_fact::resolved_to: resolved,
    };
    Ok(fact)
}

/// Wrap one content fact in an ordinal-bearing intrinsic part.
///
/// `responds_to` is a semantic edge to another canonical part. Unresolved
/// vendor correlators stay on the raw source receipt instead.
pub fn content_part(ordinal: u64, fact: Fragment, responds_to: Option<Id>) -> Result<Fragment> {
    let fact_id = rooted(&fact, "content fact")?;
    let resolutions: BTreeSet<RawHandle> = find!(
        resolution: RawHandle,
        pattern!(&fact, [{ fact_id @ schema::content_fact::resolved_to: ?resolution }])
    )
    .collect();
    // A source occurrence carrying one exact recovered body selects it in the
    // part's identity. A genuinely unresolved or already-ambiguous fact makes
    // no such claim; ambiguity remains monotone evidence on the fact itself.
    let resolution =
        (resolutions.len() == 1).then(|| *resolutions.first().expect("one resolution exists"));
    let fragment = entity! { _ @
        schema::content_part::ordinal: ordinal,
        schema::content_part::fact*: fact,
        schema::content_part::responds_to?: responds_to,
        schema::content_part::resolution?: resolution,
    };
    attach_kind(fragment, schema::content_part::KIND, "content part")
}

/// Construct one intrinsic block from structural predecessors, an optional
/// genuine event interval, and zero or more exported content parts.
///
/// The unique predecessor-free, timeless, content-free block is the bottom
/// projection used by exact source receipts that yielded no semantic content.
pub fn block(
    predecessors: impl IntoIterator<Item = Id>,
    timestamp: Option<Inline<NsTAIInterval>>,
    parts: Fragment,
) -> Result<Fragment> {
    let predecessors: Vec<_> = predecessors.into_iter().collect();
    if parts.exports().next().is_none() && (!predecessors.is_empty() || timestamp.is_some()) {
        bail!("a content-free block must be the predecessor-free, timeless canonical bottom");
    }
    // IDENTITY IS `previous` + CONTENT. The timestamp is an ANNOTATION on that
    // identity, not part of it.
    //
    // It used to be in the core, for a good reason: first blocks have no
    // `previous`, so without a timestamp two conversations opening with the
    // same content collide at the root. But that assumed the timestamp is a
    // property of the EVENT. For Codex it is a property of WHEN THE FILE WAS
    // WRITTEN, and it changes on every reload.
    //
    // MEASURED 2026-09-01, two consecutive rollouts of one resumed session:
    // three byte-identical payloads, ZERO with the same timestamp — each
    // restamped to the replay moment (13:12:16.185Z -> 14:03:56.153Z), and two
    // events originally 1 ms apart collapsed onto the SAME new value, so the
    // original timing is destroyed rather than shifted. Identical text
    // therefore hashed to different blocks, and the replayed prefix was
    // re-stored: 872 KB of source added 1238 KB of pile. With ONE 2026-08
    // conversation carrying 622 rollouts, that is one conversation stored
    // several hundred times.
    //
    // Collapsing is also the TRUER statement. A replayed block is the same
    // block observed again; storing it 622 times asserts 622 distinct events.
    // Nothing is lost by collapsing, because multiplicity lives where it is
    // actually true: `source_projection` is identified by source locator and
    // raw record bytes and carries its own `source_timestamp`, so every
    // occurrence stays distinguishable and a query joining on time enumerates
    // them all. JP: "the timestamps unfold them."
    //
    // Root collisions become correct rather than hazardous: two conversations
    // that genuinely open with identical content DO share that opening, and
    // diverge at the first block that differs. This mirrors what Memory
    // already does — `observed_at` is a BTreeSet in `annotate_chunk`, never in
    // `chunk_core` ("genuine creation/import observations, outside intrinsic
    // state").
    let mut fragment = entity! { _ @
        schema::block::previous*: predecessors.iter(),
        schema::block::contains*: parts,
    };
    let root = rooted(&fragment, "block")?;
    fragment += entity! { ExclusiveId::force_ref(&root) @
        schema::block::timestamp?: timestamp,
    };
    debug_assert_eq!(fragment.root(), Some(root));
    attach_kind(fragment, schema::block::KIND, "block")
}

/// Construct one exact source-occurrence projection receipt.
///
/// The identity contains the source namespace, stable locator, exact raw
/// record bytes, and projected canonical block. Replaying the same record is
/// therefore naturally idempotent, while two source occurrences of one shared
/// block stay distinguishable.
pub fn source_projection<T>(
    source_namespace: Id,
    source_locator: impl Into<String>,
    raw_record: T,
    block: Fragment,
) -> Result<Fragment>
where
    T: triblespace::core::blob::IntoBlob<RawBytes>,
{
    source_projection_blob(source_namespace, source_locator.into(), raw_record, block)
}

/// Construct a source projection while retaining a source-backed locator view.
pub fn source_projection_view<T>(
    source_namespace: Id,
    source_locator: anybytes::View<str>,
    raw_record: T,
    block: Fragment,
) -> Result<Fragment>
where
    T: triblespace::core::blob::IntoBlob<RawBytes>,
{
    source_projection_blob(source_namespace, source_locator, raw_record, block)
}

fn source_projection_blob<L, T>(
    source_namespace: Id,
    source_locator: L,
    raw_record: T,
    block: Fragment,
) -> Result<Fragment>
where
    L: triblespace::core::blob::IntoBlob<UTF8String>,
    T: triblespace::core::blob::IntoBlob<RawBytes>,
{
    rooted(&block, "block")?;
    let mut fragment = Fragment::empty();
    let source_locator = fragment.put::<UTF8String, _>(source_locator);
    let raw_record = fragment.put::<RawBytes, _>(raw_record);
    fragment += entity! { _ @
        schema::source_projection::source_namespace: &source_namespace,
        schema::source_projection::source_locator: source_locator,
        schema::source_projection::raw_record: raw_record,
        schema::source_projection::projects_to*: block,
    };
    attach_kind(
        fragment,
        schema::source_projection::KIND,
        "source projection",
    )
}

/// Attach occurrence-scoped evidence without changing a projection receipt's
/// intrinsic root.
pub fn annotate_source_projection(
    mut projection: Fragment,
    annotations: ProjectionAnnotations,
) -> Result<Fragment> {
    let root = rooted(&projection, "source projection")?;
    let raw_author = annotations
        .raw_author
        .map(|value| projection.put::<UTF8String, _>(value));
    let raw_role = annotations
        .raw_role
        .map(|value| projection.put::<UTF8String, _>(value));
    let raw_model = annotations
        .raw_model
        .map(|value| projection.put::<UTF8String, _>(value));
    let source_path = annotations
        .source_path
        .map(|value| projection.put::<UTF8String, _>(value));

    projection += entity! { ExclusiveId::force_ref(&root) @
        schema::source_projection::semantic_predecessor_support*:
            annotations.semantic_predecessor_support.iter(),
        schema::source_projection::source_timestamp?: annotations.source_timestamp,
        schema::source_projection::author?: annotations.author,
        schema::source_projection::experiencer?: annotations.experiencer,
        schema::source_projection::raw_author?: raw_author.as_ref(),
        schema::source_projection::raw_role?: raw_role.as_ref(),
        schema::source_projection::raw_model?: raw_model.as_ref(),
        files_schema::file::source_path?: source_path.as_ref(),
    };
    debug_assert_eq!(projection.root(), Some(root));
    Ok(projection)
}

fn values_by_entity<T: Ord>(rows: impl IntoIterator<Item = (Id, T)>) -> BTreeMap<Id, BTreeSet<T>> {
    let mut values = BTreeMap::new();
    for (entity, value) in rows {
        values
            .entry(entity)
            .or_insert_with(BTreeSet::new)
            .insert(value);
    }
    values
}

fn values_for<T: Ord + Copy>(values: &BTreeMap<Id, BTreeSet<T>>, id: Id) -> BTreeSet<T> {
    values.get(&id).cloned().unwrap_or_default()
}

fn one_required<T: Ord + Copy>(
    values: &BTreeMap<Id, BTreeSet<T>>,
    id: Id,
    field: &str,
) -> Result<T> {
    let values = values_for(values, id);
    if values.len() != 1 {
        bail!(
            "entity {id:x} has {} values for {field}; expected exactly one",
            values.len()
        );
    }
    Ok(*values.first().expect("one value"))
}

fn one_optional<T: Ord + Copy>(
    values: &BTreeMap<Id, BTreeSet<T>>,
    id: Id,
    field: &str,
) -> Result<Option<T>> {
    let values = values_for(values, id);
    if values.len() > 1 {
        bail!(
            "entity {id:x} has {} values for optional scalar {field}",
            values.len()
        );
    }
    Ok(values.first().copied())
}

fn ids_of_kind<P>(facts: &P, kind: Id) -> BTreeSet<Id>
where
    P: TriblePattern,
{
    find!(
        id: Id,
        pattern!(facts, [{ ?id @ metadata::tag: &kind }])
    )
    .collect()
}

fn ensure_intrinsic_with_kind(id: Id, core: Fragment, kind: Id, label: &str) -> Result<TribleSet> {
    let expected = core
        .root()
        .ok_or_else(|| anyhow!("{label} core has no unique intrinsic root"))?;
    if expected != id {
        bail!("{label} {id:x} does not match intrinsic root {expected:x}");
    }
    let mut facts = core.into_facts();
    facts += entity! { ExclusiveId::force_ref(&id) @ metadata::tag: &kind }.into_facts();
    Ok(facts)
}

fn known_modality(value: Id) -> bool {
    schema::content_fact::modality::SPECS
        .iter()
        .any(|(id, _)| *id == value)
}

fn textual_modality(value: Id) -> bool {
    [
        schema::content_fact::modality::TEXT,
        schema::content_fact::modality::TOOL_CALL,
        schema::content_fact::modality::TOOL_RESULT,
        schema::content_fact::modality::THINKING,
        schema::content_fact::modality::EVENT,
    ]
    .contains(&value)
}

fn media_modality(value: Id) -> bool {
    [
        schema::content_fact::modality::AUDIO,
        schema::content_fact::modality::IMAGE,
        schema::content_fact::modality::FILE,
        schema::content_fact::modality::VIDEO,
    ]
    .contains(&value)
}

fn known_direction(value: Id) -> bool {
    schema::content_fact::direction::SPECS
        .iter()
        .any(|(id, _)| *id == value)
}

fn require_disjoint(kinds: &[(&str, &BTreeSet<Id>)]) -> Result<()> {
    for (index, (left_name, left)) in kinds.iter().enumerate() {
        for (right_name, right) in &kinds[index + 1..] {
            if let Some(id) = left.intersection(right).next() {
                bail!("entity {id:x} is tagged as both {left_name} and {right_name}");
            }
        }
    }
    Ok(())
}

fn require_coverage(declared: &BTreeSet<Id>, referenced: &BTreeSet<Id>, label: &str) -> Result<()> {
    if let Some(id) = declared.difference(referenced).next() {
        bail!("orphan {label} {id:x} is not reachable from a source projection");
    }
    if let Some(id) = referenced.difference(declared).next() {
        bail!("reference names undeclared {label} {id:x}");
    }
    Ok(())
}

fn validate_dag(nodes: &BTreeMap<Id, BTreeSet<Id>>, label: &str) -> Result<()> {
    let mut remaining: BTreeMap<Id, usize> = nodes
        .iter()
        .map(|(node, predecessors)| (*node, predecessors.len()))
        .collect();
    let mut children = BTreeMap::<Id, Vec<Id>>::new();
    for (&node, predecessors) in nodes {
        for predecessor in predecessors {
            if !nodes.contains_key(predecessor) {
                bail!("{label} {node:x} cites missing predecessor {predecessor:x}");
            }
            children.entry(*predecessor).or_default().push(node);
        }
    }
    let mut ready: Vec<Id> = remaining
        .iter()
        .filter_map(|(node, count)| (*count == 0).then_some(*node))
        .collect();
    let mut visited = 0usize;
    while let Some(node) = ready.pop() {
        visited += 1;
        for child in children.get(&node).into_iter().flatten() {
            let count = remaining
                .get_mut(child)
                .expect("every child belongs to the graph");
            *count -= 1;
            if *count == 0 {
                ready.push(*child);
            }
        }
    }
    if visited != nodes.len() {
        bail!("{label} contains a predecessor cycle");
    }
    Ok(())
}

/// Prove the universal block-DAG denotation without reading any attachment.
///
/// The proof reconstructs every intrinsic id, enforces scalar cardinality and
/// graph closure, and then compares the reconstructed ontology with the input
/// exactly. Vendor parsing, source-locator grammar, role mapping, MIME
/// inference, and raw-record-to-projection equivalence intentionally remain
/// outside this universal layer.
fn validate_structure_with_rows<P>(
    facts: &P,
    observed: impl IntoIterator<Item = Trible>,
) -> Result<AttachmentPlan>
where
    P: TriblePattern,
{
    let content_facts = ids_of_kind(facts, schema::content_fact::KIND);
    let content_parts = ids_of_kind(facts, schema::content_part::KIND);
    let blocks = ids_of_kind(facts, schema::block::KIND);
    let projections = ids_of_kind(facts, schema::source_projection::KIND);
    let source_chunks = ids_of_kind(facts, schema::source_chunk::KIND);
    let source_snapshots = ids_of_kind(facts, schema::source_snapshot::KIND);
    let media_types = ids_of_kind(facts, files_schema::KIND_MEDIA_TYPE);
    require_disjoint(&[
        ("content fact", &content_facts),
        ("content part", &content_parts),
        ("block", &blocks),
        ("source projection", &projections),
        ("source chunk", &source_chunks),
        ("source snapshot", &source_snapshots),
        ("media type", &media_types),
    ])?;

    let media_names = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ metadata::name: ?value }])
    ));
    let vocabulary_ids: BTreeSet<_> = schema::content_fact::modality::SPECS
        .iter()
        .chain(schema::content_fact::direction::SPECS)
        .map(|(id, _)| *id)
        .collect();
    let has_vocabulary = vocabulary_ids.iter().any(|id| media_names.contains_key(id));
    let modalities = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::content_fact::modality: ?value }])
    ));
    let directions = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::content_fact::direction: ?value }])
    ));
    let payloads = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ schema::content_fact::payload: ?value }])
    ));
    let blobs = values_by_entity(find!(
        (entity: Id, value: RawHandle),
        pattern!(facts, [{ ?entity @ schema::content_fact::blob: ?value }])
    ));
    let asset_pointers = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ schema::content_fact::asset_pointer: ?value }])
    ));
    let asset_namespaces = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::content_fact::asset_namespace: ?value }])
    ));
    let fact_media_types = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::content_fact::media_type: ?value }])
    ));
    let asset_sizes = values_by_entity(find!(
        (entity: Id, value: OrdinalValue),
        pattern!(facts, [{ ?entity @ schema::content_fact::asset_size: ?value }])
    ));
    let resolutions = values_by_entity(find!(
        (entity: Id, value: RawHandle),
        pattern!(facts, [{ ?entity @ schema::content_fact::resolved_to: ?value }])
    ));
    let source_chunk_offsets = values_by_entity(find!(
        (entity: Id, value: OrdinalValue),
        pattern!(facts, [{ ?entity @ schema::source_chunk::offset: ?value }])
    ));
    let source_chunk_bytes = values_by_entity(find!(
        (entity: Id, value: RawHandle),
        pattern!(facts, [{ ?entity @ schema::source_chunk::bytes: ?value }])
    ));
    let source_snapshot_lengths = values_by_entity(find!(
        (entity: Id, value: OrdinalValue),
        pattern!(facts, [{ ?entity @ schema::source_snapshot::byte_length: ?value }])
    ));
    let source_snapshot_chunks = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::source_snapshot::contains: ?value }])
    ));

    let ordinals = values_by_entity(find!(
        (entity: Id, value: OrdinalValue),
        pattern!(facts, [{ ?entity @ schema::content_part::ordinal: ?value }])
    ));
    let part_facts = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::content_part::fact: ?value }])
    ));
    let responds_to = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::content_part::responds_to: ?value }])
    ));
    let part_resolutions = values_by_entity(find!(
        (entity: Id, value: RawHandle),
        pattern!(facts, [{ ?entity @ schema::content_part::resolution: ?value }])
    ));

    let block_previous = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::block::previous: ?value }])
    ));
    let block_timestamps = values_by_entity(find!(
        (entity: Id, value: IntervalValue),
        pattern!(facts, [{ ?entity @ schema::block::timestamp: ?value }])
    ));
    let block_parts = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::block::contains: ?value }])
    ));

    let source_namespaces = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::source_projection::source_namespace: ?value }])
    ));
    let source_locators = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ schema::source_projection::source_locator: ?value }])
    ));
    let raw_records = values_by_entity(find!(
        (entity: Id, value: RawHandle),
        pattern!(facts, [{ ?entity @ schema::source_projection::raw_record: ?value }])
    ));
    let projected_blocks = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::source_projection::projects_to: ?value }])
    ));
    let semantic_predecessor_support = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{
            ?entity @ schema::source_projection::semantic_predecessor_support: ?value
        }])
    ));
    let source_timestamps = values_by_entity(find!(
        (entity: Id, value: IntervalValue),
        pattern!(facts, [{ ?entity @ schema::source_projection::source_timestamp: ?value }])
    ));
    let authors = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::source_projection::author: ?value }])
    ));
    let experiencers = values_by_entity(find!(
        (entity: Id, value: Id),
        pattern!(facts, [{ ?entity @ schema::source_projection::experiencer: ?value }])
    ));
    let raw_authors = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ schema::source_projection::raw_author: ?value }])
    ));
    let raw_roles = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ schema::source_projection::raw_role: ?value }])
    ));
    let raw_models = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ schema::source_projection::raw_model: ?value }])
    ));
    let source_paths = values_by_entity(find!(
        (entity: Id, value: TextHandle),
        pattern!(facts, [{ ?entity @ files_schema::file::source_path: ?value }])
    ));

    let mut expected = TribleSet::new();
    let mut attachments = AttachmentPlan::default();
    let mut referenced_media_types = BTreeSet::new();
    for id in &media_types {
        let name = one_required(&media_names, *id, "media type name")?;
        let core = entity! {
            metadata::tag: &files_schema::KIND_MEDIA_TYPE,
            metadata::name: name,
        };
        let expected_id = core.root().expect("media type core has one root");
        if expected_id != *id {
            bail!("media type {id:x} does not match intrinsic root {expected_id:x}");
        }
        expected += core.into_facts();
        attachments.texts.insert(name);
        attachments.media_type_names.insert(name);
    }

    // Once vocabulary annotation is present, require the complete canonical
    // bootstrap vocabulary. Additional names are ordinary additive graph data:
    // readers may display all of them without a last-writer-wins label table.
    // A completely absent vocabulary remains valid so the writer can add the
    // bootstrap monotonically to a pre-vocabulary collection.
    if has_vocabulary {
        for id in &vocabulary_ids {
            let names = values_for(&media_names, *id);
            if names.is_empty() {
                bail!("entity {id:x} has no Archive vocabulary name");
            }
            attachments.texts.extend(names.iter().copied());
            expected += entity! { ExclusiveId::force_ref(id) @
                metadata::name*: names.iter(),
            }
            .into_facts();
        }
        expected += vocabulary_fragment().into_facts();
    }

    let mut referenced_source_chunks = BTreeSet::new();
    for id in &source_chunks {
        let offset = one_required(&source_chunk_offsets, *id, "source chunk offset")?;
        let offset_number = u128::try_from_inline(&offset)
            .map_err(|error| anyhow!("source chunk {id:x} offset does not fit u128: {error:?}"))?;
        let bytes = one_required(&source_chunk_bytes, *id, "source chunk bytes")?;
        let core = entity! { _ @
            schema::source_chunk::offset: offset,
            schema::source_chunk::bytes: bytes,
        };
        expected +=
            ensure_intrinsic_with_kind(*id, core, schema::source_chunk::KIND, "source chunk")?;
        attachments
            .source_chunks
            .insert(*id, (offset_number, bytes));
    }

    for id in &source_snapshots {
        let namespace = one_required(&source_namespaces, *id, "source snapshot namespace")?;
        let locator = one_required(&source_locators, *id, "source snapshot locator")?;
        let byte_length =
            one_required(&source_snapshot_lengths, *id, "source snapshot byte length")?;
        let byte_length_number = u128::try_from_inline(&byte_length).map_err(|error| {
            anyhow!("source snapshot {id:x} length does not fit u128: {error:?}")
        })?;
        let chunks = values_for(&source_snapshot_chunks, *id);
        for chunk in &chunks {
            if !source_chunks.contains(chunk) {
                bail!("source snapshot {id:x} cites missing chunk {chunk:x}");
            }
        }
        referenced_source_chunks.extend(chunks.iter().copied());
        let paths = values_for(&source_paths, *id);
        let core = entity! { _ @
            schema::source_projection::source_namespace: &namespace,
            schema::source_projection::source_locator: locator,
            schema::source_snapshot::byte_length: byte_length,
            schema::source_snapshot::contains*: chunks.iter(),
        };
        let mut canonical = ensure_intrinsic_with_kind(
            *id,
            core,
            schema::source_snapshot::KIND,
            "source snapshot",
        )?;
        canonical += entity! { ExclusiveId::force_ref(id) @
            files_schema::file::source_path*: paths.iter(),
        }
        .into_facts();
        expected += canonical;
        attachments.texts.insert(locator);
        attachments.texts.extend(paths.iter().copied());
        attachments.source_snapshots.push(SourceSnapshotPlan {
            id: *id,
            byte_length: byte_length_number,
            chunks: chunks.into_iter().collect(),
        });
    }
    require_coverage(&source_chunks, &referenced_source_chunks, "source chunk")?;

    for id in &content_facts {
        let modality = one_required(&modalities, *id, "content fact modality")?;
        if !known_modality(modality) {
            bail!("content fact {id:x} has unknown modality {modality:x}");
        }
        let direction = one_required(&directions, *id, "content fact direction")?;
        if !known_direction(direction) {
            bail!("content fact {id:x} has unknown direction {direction:x}");
        }
        let payload = one_optional(&payloads, *id, "content fact payload")?;
        let blob = one_optional(&blobs, *id, "content fact blob")?;
        let pointer = one_optional(&asset_pointers, *id, "content fact asset pointer")?;
        let namespace = one_optional(&asset_namespaces, *id, "content fact asset namespace")?;
        let media_type = one_optional(&fact_media_types, *id, "content fact media type")?;
        let size = one_optional(&asset_sizes, *id, "content fact asset size")?;
        if let Some(size) = size {
            let _: u128 = size.try_from_inline().map_err(|error| {
                anyhow!("content fact {id:x} asset size does not fit u128: {error:?}")
            })?;
        }
        let resolved = values_for(&resolutions, *id);

        let core = match (payload, blob, pointer) {
            (Some(payload), None, None) => {
                if !textual_modality(modality) {
                    bail!("content fact {id:x} uses a text payload with a media modality");
                }
                if namespace.is_some()
                    || media_type.is_some()
                    || size.is_some()
                    || !resolved.is_empty()
                {
                    bail!("text content fact {id:x} carries media-only fields");
                }
                attachments.texts.insert(payload);
                entity! { _ @
                    schema::content_fact::modality: &modality,
                    schema::content_fact::direction: &direction,
                    schema::content_fact::payload: payload,
                }
            }
            (None, Some(blob), None) => {
                if !media_modality(modality) {
                    bail!("content fact {id:x} uses resident bytes with a textual modality");
                }
                if namespace.is_some() || size.is_some() || !resolved.is_empty() {
                    bail!("resident content fact {id:x} carries pointer-only fields");
                }
                let media_type = media_type.ok_or_else(|| {
                    anyhow!("resident content fact {id:x} has no canonical media type")
                })?;
                if !media_types.contains(&media_type) {
                    bail!("content fact {id:x} cites missing media type {media_type:x}");
                }
                referenced_media_types.insert(media_type);
                attachments.raws.insert(blob);
                entity! { _ @
                    schema::content_fact::modality: &modality,
                    schema::content_fact::direction: &direction,
                    schema::content_fact::blob: blob,
                    schema::content_fact::media_type: &media_type,
                }
            }
            (None, None, Some(pointer)) => {
                if !media_modality(modality) {
                    bail!("content fact {id:x} uses an asset pointer with a textual modality");
                }
                let namespace = namespace
                    .ok_or_else(|| anyhow!("pointer content fact {id:x} has no asset namespace"))?;
                if let Some(media_type) = media_type {
                    if !media_types.contains(&media_type) {
                        bail!("content fact {id:x} cites missing media type {media_type:x}");
                    }
                    referenced_media_types.insert(media_type);
                }
                attachments.texts.insert(pointer);
                attachments.raws.extend(resolved.iter().copied());
                entity! { _ @
                    schema::content_fact::modality: &modality,
                    schema::content_fact::direction: &direction,
                    schema::content_fact::asset_pointer: pointer,
                    schema::content_fact::asset_namespace: &namespace,
                    schema::content_fact::media_type?: media_type,
                    schema::content_fact::asset_size?: size,
                }
            }
            _ => bail!(
                "content fact {id:x} must have exactly one of payload, blob, or asset pointer"
            ),
        };
        let mut canonical =
            ensure_intrinsic_with_kind(*id, core, schema::content_fact::KIND, "content fact")?;
        canonical += entity! { ExclusiveId::force_ref(id) @
            schema::content_fact::resolved_to*: resolved.iter(),
        }
        .into_facts();
        expected += canonical;
    }

    let mut ordinal_numbers = BTreeMap::new();
    let mut referenced_content_facts = BTreeSet::new();
    for id in &content_parts {
        let ordinal = one_required(&ordinals, *id, "content part ordinal")?;
        let ordinal_number: u64 = ordinal
            .try_from_inline()
            .map_err(|error| anyhow!("content part {id:x} ordinal does not fit u64: {error:?}"))?;
        let fact = one_required(&part_facts, *id, "content part fact")?;
        if !content_facts.contains(&fact) {
            bail!("content part {id:x} cites missing content fact {fact:x}");
        }
        referenced_content_facts.insert(fact);
        let response = one_optional(&responds_to, *id, "content part responds-to")?;
        if let Some(response) = response {
            if !content_parts.contains(&response) {
                bail!("content part {id:x} responds to missing part {response:x}");
            }
        }
        let resolution = one_optional(&part_resolutions, *id, "content part resolution")?;
        if let Some(resolution) = resolution {
            let available = values_for(&resolutions, fact);
            if !available.contains(&resolution) {
                bail!("content part {id:x} selects resolution absent from content fact {fact:x}");
            }
        }
        let core = entity! { _ @
            schema::content_part::ordinal: ordinal,
            schema::content_part::fact: &fact,
            schema::content_part::responds_to?: response,
            schema::content_part::resolution?: resolution,
        };
        expected +=
            ensure_intrinsic_with_kind(*id, core, schema::content_part::KIND, "content part")?;
        ordinal_numbers.insert(*id, ordinal_number);
    }

    let mut block_graph = BTreeMap::new();
    let mut referenced_parts = BTreeSet::new();
    for id in &blocks {
        let previous = values_for(&block_previous, *id);
        for predecessor in &previous {
            if !blocks.contains(predecessor) {
                bail!("block {id:x} cites missing predecessor {predecessor:x}");
            }
        }
        let timestamps = values_for(&block_timestamps, *id);
        for timestamp in &timestamps {
            let _: (i128, i128) = timestamp.try_from_inline().map_err(|error| {
                anyhow!("block {id:x} has invalid timestamp interval: {error:?}")
            })?;
        }
        let parts = values_for(&block_parts, *id);
        if parts.is_empty() && (!previous.is_empty() || !timestamps.is_empty()) {
            bail!(
                "content-free block {id:x} is not the predecessor-free, timeless canonical bottom"
            );
        }
        let mut by_ordinal = BTreeMap::new();
        for part in &parts {
            if !content_parts.contains(part) {
                bail!("block {id:x} contains missing part {part:x}");
            }
            let ordinal = ordinal_numbers[part];
            if let Some(other) = by_ordinal.insert(ordinal, *part) {
                bail!("block {id:x} contains parts {other:x} and {part:x} at ordinal {ordinal}");
            }
        }
        for (expected_ordinal, actual) in (0u64..).zip(by_ordinal.keys().copied()) {
            if actual != expected_ordinal {
                bail!(
                    "block {id:x} has non-contiguous part ordinals; expected {expected_ordinal}, found {actual}"
                );
            }
        }
        referenced_parts.extend(parts.iter().copied());
        let core = entity! { _ @
            schema::block::previous*: previous.iter(),
            schema::block::contains*: parts.iter(),
        };
        let mut canonical = ensure_intrinsic_with_kind(*id, core, schema::block::KIND, "block")?;
        canonical += entity! { ExclusiveId::force_ref(id) @
            schema::block::timestamp*: timestamps.iter(),
        }
        .into_facts();
        expected += canonical;
        block_graph.insert(*id, previous);
    }
    validate_dag(&block_graph, "block DAG")?;

    let mut referenced_blocks = BTreeSet::new();
    for id in &projections {
        let namespace = one_required(&source_namespaces, *id, "source projection namespace")?;
        let locator = one_required(&source_locators, *id, "source projection locator")?;
        let raw_record = one_required(&raw_records, *id, "source projection raw record")?;
        let projected = one_required(&projected_blocks, *id, "source projection target block")?;
        if !blocks.contains(&projected) {
            bail!("source projection {id:x} cites missing block {projected:x}");
        }
        referenced_blocks.insert(projected);
        let predecessor_support = values_for(&semantic_predecessor_support, *id);
        let direct_predecessors = block_graph
            .get(&projected)
            .expect("every projected block has a validated predecessor set");
        for receipt in &predecessor_support {
            if !projections.contains(receipt) {
                bail!(
                    "source projection {id:x} cites missing semantic-support receipt {receipt:x}"
                );
            }
            let supporting_block = one_required(
                &projected_blocks,
                *receipt,
                "semantic-support receipt target block",
            )?;
            if !direct_predecessors.contains(&supporting_block) {
                bail!(
                    "source projection {id:x} cites receipt {receipt:x} for unrelated block {supporting_block:x}"
                );
            }
        }
        let source_timestamp =
            one_optional(&source_timestamps, *id, "source projection timestamp")?;
        if let Some(timestamp) = source_timestamp {
            let _: (i128, i128) = timestamp.try_from_inline().map_err(|error| {
                anyhow!("source projection {id:x} has invalid timestamp: {error:?}")
            })?;
        }
        let author = one_optional(&authors, *id, "source projection author")?;
        let experiencer = one_optional(&experiencers, *id, "source projection experiencer")?;
        let raw_author = one_optional(&raw_authors, *id, "source projection raw author")?;
        let raw_role = one_optional(&raw_roles, *id, "source projection raw role")?;
        let raw_model = one_optional(&raw_models, *id, "source projection raw model")?;
        let paths = values_for(&source_paths, *id);

        attachments.texts.insert(locator);
        attachments.raws.insert(raw_record);
        attachments.texts.extend(raw_author);
        attachments.texts.extend(raw_role);
        attachments.texts.extend(raw_model);
        attachments.texts.extend(paths.iter().copied());

        let core = entity! { _ @
            schema::source_projection::source_namespace: &namespace,
            schema::source_projection::source_locator: locator,
            schema::source_projection::raw_record: raw_record,
            schema::source_projection::projects_to: &projected,
        };
        let mut canonical = ensure_intrinsic_with_kind(
            *id,
            core,
            schema::source_projection::KIND,
            "source projection",
        )?;
        canonical += entity! { ExclusiveId::force_ref(id) @
            schema::source_projection::semantic_predecessor_support*:
                predecessor_support.iter(),
            schema::source_projection::source_timestamp?: source_timestamp,
            schema::source_projection::author?: author,
            schema::source_projection::experiencer?: experiencer,
            schema::source_projection::raw_author?: raw_author,
            schema::source_projection::raw_role?: raw_role,
            schema::source_projection::raw_model?: raw_model,
            files_schema::file::source_path*: paths.iter(),
        }
        .into_facts();
        expected += canonical;
    }

    require_coverage(&media_types, &referenced_media_types, "media type")?;
    require_coverage(&content_facts, &referenced_content_facts, "content fact")?;
    require_coverage(&content_parts, &referenced_parts, "content part")?;
    require_coverage(&blocks, &referenced_blocks, "block")?;

    let (missing, unexpected) = canonical_difference_counts(&expected, observed);
    if missing != 0 || unexpected != 0 {
        bail!(
            "block-DAG catalog is not an exact canonical ontology ({missing} missing, {unexpected} unexpected facts)"
        );
    }
    Ok(attachments)
}

fn canonical_difference_counts(
    expected: &TribleSet,
    observed: impl IntoIterator<Item = Trible>,
) -> (usize, usize) {
    let mut expected = expected.iter_ordered().copied().peekable();
    let mut observed = observed.into_iter().peekable();
    let mut missing = 0usize;
    let mut unexpected = 0usize;
    loop {
        match (expected.peek(), observed.peek()) {
            (Some(left), Some(right)) => match left.cmp(right) {
                std::cmp::Ordering::Less => {
                    missing += 1;
                    expected.next();
                }
                std::cmp::Ordering::Equal => {
                    expected.next();
                    observed.next();
                }
                std::cmp::Ordering::Greater => {
                    unexpected += 1;
                    observed.next();
                }
            },
            (Some(_), None) => {
                missing += expected.count();
                break;
            }
            (None, Some(_)) => {
                unexpected += observed.count();
                break;
            }
            (None, None) => break,
        }
    }
    (missing, unexpected)
}

fn validate_structure(facts: &TribleSet) -> Result<AttachmentPlan> {
    validate_structure_with_rows(facts, facts.iter_ordered().copied())
}

fn validate_succinct_structure(facts: &UnionArchive<OrderedUniverse>) -> Result<AttachmentPlan> {
    validate_structure_with_rows(facts, facts.iter())
}

/// Validate only the canonical graph, without consulting any blob store.
///
/// This proves intrinsic identities, exact field shapes, graph closure,
/// predecessor acyclicity, ordinal continuity, and reachability coverage. It
/// deliberately does not claim that named attachments are resident or
/// decodable; use [`validate_catalog`] or [`validate_catalog_union`] for that.
pub fn validate_catalog_structure(facts: &TribleSet) -> Result<()> {
    validate_structure(facts).map(drop)
}

fn read_text_attachment(
    reader: &PileSnapshot,
    overlay: Option<&OverlayReader>,
    handle: TextHandle,
) -> std::result::Result<Option<String>, String> {
    if let Some(overlay) = overlay {
        if overlay
            .contains_blob(handle)
            .map_err(|error| format!("inspect staged UTF8String attachment: {error}"))?
        {
            let value: anybytes::View<str> = overlay.get(handle).map_err(|error| {
                format!(
                    "invalid staged UTF8String attachment {}: {error}",
                    hex::encode(handle.raw)
                )
            })?;
            return Ok(Some(value.to_string()));
        }
    }
    if !reader
        .contains_blob(handle)
        .map_err(|error| format!("inspect resident UTF8String attachment: {error}"))?
    {
        return Ok(None);
    }
    let value: anybytes::View<str> = reader.get(handle).map_err(|error| {
        format!(
            "invalid resident UTF8String attachment {}: {error}",
            hex::encode(handle.raw)
        )
    })?;
    Ok(Some(value.to_string()))
}

fn text_attachment_present(
    reader: &PileSnapshot,
    overlay: Option<&OverlayReader>,
    handle: TextHandle,
) -> std::result::Result<bool, String> {
    if let Some(overlay) = overlay {
        if overlay
            .contains_blob(handle)
            .map_err(|error| format!("inspect staged UTF8String attachment: {error}"))?
        {
            let _: anybytes::View<str> = overlay.get(handle).map_err(|error| {
                format!(
                    "invalid staged UTF8String attachment {}: {error}",
                    hex::encode(handle.raw)
                )
            })?;
            return Ok(true);
        }
    }
    reader
        .contains_blob(handle)
        .map_err(|error| format!("inspect resident UTF8String attachment: {error}"))
}

fn raw_attachment_present(
    reader: &PileSnapshot,
    overlay: Option<&OverlayReader>,
    handle: RawHandle,
) -> std::result::Result<bool, String> {
    if let Some(overlay) = overlay {
        if overlay
            .contains_blob(handle)
            .map_err(|error| format!("inspect staged RawBytes attachment: {error}"))?
        {
            let _: Bytes = overlay.get(handle).map_err(|error| {
                format!(
                    "invalid staged RawBytes attachment {}: {error}",
                    hex::encode(handle.raw)
                )
            })?;
            return Ok(true);
        }
    }
    reader
        .contains_blob(handle)
        .map_err(|error| format!("inspect resident RawBytes attachment: {error}"))
}

fn validate_source_snapshots(
    chunks: &BTreeMap<Id, (u128, RawHandle)>,
    snapshots: &[SourceSnapshotPlan],
    stored_lengths: &BTreeMap<RawHandle, u128>,
) -> std::result::Result<(), String> {
    for snapshot in snapshots {
        let mut ordered = snapshot
            .chunks
            .iter()
            .map(|chunk| {
                let (offset, handle) = chunks.get(chunk).ok_or_else(|| {
                    format!(
                        "source snapshot {:x} lost declared chunk {:x}",
                        snapshot.id, chunk
                    )
                })?;
                Ok((*offset, *chunk, *handle))
            })
            .collect::<std::result::Result<Vec<_>, String>>()?;
        ordered.sort_unstable();

        if snapshot.byte_length == 0 && !ordered.is_empty() {
            return Err(format!(
                "empty source snapshot {:x} contains byte chunks",
                snapshot.id
            ));
        }
        if snapshot.byte_length != 0 && ordered.is_empty() {
            return Err(format!(
                "nonempty source snapshot {:x} contains no byte chunks",
                snapshot.id
            ));
        }

        let mut cursor = 0u128;
        for (index, (offset, chunk, handle)) in ordered.iter().copied().enumerate() {
            if offset != cursor {
                return Err(format!(
                    "source snapshot {:x} chunk {:x} starts at {offset}, expected {cursor}",
                    snapshot.id, chunk
                ));
            }
            let is_last = index + 1 == ordered.len();
            let len = if is_last {
                snapshot.byte_length.checked_sub(offset).ok_or_else(|| {
                    format!(
                        "source snapshot {:x} chunk {:x} starts beyond its claimed length",
                        snapshot.id, chunk
                    )
                })?
            } else {
                schema::source_chunk::CANONICAL_BYTES as u128
            };
            if len == 0 || len > schema::source_chunk::CANONICAL_BYTES as u128 {
                return Err(format!(
                    "source snapshot {:x} chunk {:x} has noncanonical length {len}",
                    snapshot.id, chunk
                ));
            }
            if let Some(stored) = stored_lengths.get(&handle) {
                if *stored != len {
                    return Err(format!(
                        "source snapshot {:x} chunk {:x} stores {stored} bytes, expected {len}",
                        snapshot.id, chunk
                    ));
                }
            }
            if !is_last && len != schema::source_chunk::CANONICAL_BYTES as u128 {
                return Err(format!(
                    "source snapshot {:x} nonfinal chunk {:x} has length {len}, expected {}",
                    snapshot.id,
                    chunk,
                    schema::source_chunk::CANONICAL_BYTES
                ));
            }
            cursor = cursor.checked_add(len).ok_or_else(|| {
                format!("source snapshot {:x} length overflows u128", snapshot.id)
            })?;
        }
        if cursor != snapshot.byte_length {
            return Err(format!(
                "source snapshot {:x} reconstructs {cursor} bytes, claims {}",
                snapshot.id, snapshot.byte_length
            ));
        }
    }
    Ok(())
}

fn source_chunk_storage_lengths(
    reader: &PileSnapshot,
    overlay: Option<&OverlayReader>,
    chunks: &BTreeMap<Id, (u128, RawHandle)>,
) -> std::result::Result<BTreeMap<RawHandle, u128>, String> {
    let wanted: BTreeSet<_> = chunks.values().map(|(_, handle)| handle.raw).collect();
    let mut lengths = BTreeMap::new();
    if wanted.is_empty() {
        return Ok(lengths);
    }
    for raw in &wanted {
        let handle = Inline::<Handle<RawBytes>>::new(*raw);
        if let Some(info) = reader
            .blob_info(handle)
            .map_err(|error| format!("inspect resident source chunk: {error}"))?
        {
            lengths.insert(handle, u128::from(info.length));
        }
    }
    if let Some(overlay) = overlay {
        for info in overlay.blobs() {
            let info = info.map_err(|error| format!("enumerate staged source chunks: {error}"))?;
            if wanted.contains(&info.handle.raw) {
                let handle = Inline::<Handle<RawBytes>>::new(info.handle.raw);
                // A caller-supplied overlay can contain a forged handle.
                // Validate staged bytes once; durable Pile headers are trusted
                // only for length/presence and remain hash-lazy until read.
                let bytes: Bytes = overlay.get(handle).map_err(|error| {
                    format!(
                        "invalid staged source chunk {}: {error}",
                        hex::encode(handle.raw)
                    )
                })?;
                if bytes.len() as u64 != info.length {
                    return Err(format!(
                        "staged source chunk {} header length disagrees with its bytes",
                        hex::encode(handle.raw)
                    ));
                }
                lengths.insert(handle, u128::from(info.length));
            }
        }
    }
    Ok(lengths)
}

fn validate_attachments(
    reader: &PileSnapshot,
    overlay: Option<&OverlayReader>,
    plan: AttachmentPlan,
) -> CatalogValidation {
    let AttachmentPlan {
        texts,
        raws,
        media_type_names,
        source_chunks,
        source_snapshots,
    } = plan;
    let mut missing = BTreeSet::new();
    for handle in texts {
        match text_attachment_present(reader, overlay, handle) {
            Ok(true) => {}
            Ok(false) => {
                missing.insert(handle.raw);
            }
            Err(reason) => return CatalogValidation::Rejected(reason),
        }
    }
    for handle in raws {
        match raw_attachment_present(reader, overlay, handle) {
            Ok(true) => {}
            Ok(false) => {
                missing.insert(handle.raw);
            }
            Err(reason) => return CatalogValidation::Rejected(reason),
        }
    }
    let source_chunk_lengths = match source_chunk_storage_lengths(reader, overlay, &source_chunks) {
        Ok(lengths) => lengths,
        Err(reason) => return CatalogValidation::Rejected(reason),
    };
    if let Err(reason) =
        validate_source_snapshots(&source_chunks, &source_snapshots, &source_chunk_lengths)
    {
        return CatalogValidation::Rejected(reason);
    }
    let source_chunk_handles: BTreeSet<_> =
        source_chunks.values().map(|(_, handle)| *handle).collect();
    for handle in source_chunk_handles {
        if !source_chunk_lengths.contains_key(&handle) {
            missing.insert(handle.raw);
        }
    }
    for handle in media_type_names {
        let name = match read_text_attachment(reader, overlay, handle) {
            Ok(Some(name)) => name,
            Ok(None) => {
                missing.insert(handle.raw);
                continue;
            }
            Err(reason) => return CatalogValidation::Rejected(reason),
        };
        let normalized = match files::normalize_media_type(&name) {
            Ok(value) => value,
            Err(error) => {
                return CatalogValidation::Rejected(format!(
                    "invalid canonical media type name {name:?}: {error}"
                ));
            }
        };
        if normalized != name {
            return CatalogValidation::Rejected(format!(
                "media type name {name:?} is not canonical; expected {normalized:?}"
            ));
        }
    }
    if !missing.is_empty() {
        return CatalogValidation::Pending { missing };
    }
    CatalogValidation::Accepted
}

fn validate_catalog_with_overlay(
    reader: &PileSnapshot,
    overlay: Option<&OverlayReader>,
    facts: &TribleSet,
) -> Result<CatalogValidation> {
    let plan = match validate_structure(facts) {
        Ok(plan) => plan,
        Err(error) => return Ok(CatalogValidation::Rejected(format!("{error:#}"))),
    };
    Ok(validate_attachments(reader, overlay, plan))
}

/// Validate one complete materialized Archive catalog.
///
/// The immutable graph is proved before attachment residency is inspected, so
/// malformed data can never be concealed behind a missing blob. A pointer
/// fact with no `resolved_to` edge is a valid unresolved external asset and is
/// therefore accepted rather than reported as pending.
pub fn validate_catalog(reader: &PileSnapshot, facts: &TribleSet) -> Result<CatalogValidation> {
    validate_catalog_with_overlay(reader, None, facts)
}

/// Validate one complete Archive catalog directly over its logical Succinct
/// cover.
///
/// Exact ontology comparison walks the cover's canonical deduplicated EAV
/// stream, so validation does not rebuild the six in-memory PATCH indexes of
/// a [`TribleSet`].
pub fn validate_succinct_catalog(
    reader: &PileSnapshot,
    facts: &UnionArchive<OrderedUniverse>,
) -> Result<CatalogValidation> {
    let plan = match validate_succinct_structure(facts) {
        Ok(plan) => plan,
        Err(error) => return Ok(CatalogValidation::Rejected(format!("{error:#}"))),
    };
    Ok(validate_attachments(reader, None, plan))
}

/// Preflight the exact staged union without writing its attachments.
///
/// Existing handles resolve through `reader`; blobs carried by `fragment`
/// resolve through its immutable in-memory overlay. The returned facts are the
/// exact candidate set that was validated.
pub fn validate_catalog_union(
    reader: &PileSnapshot,
    current: &TribleSet,
    fragment: &Fragment,
) -> Result<(TribleSet, CatalogValidation)> {
    let mut union = current.clone();
    union += fragment.facts().clone();
    let mut staged = fragment.clone();
    let overlay = staged
        .blobs_mut()
        .snapshot()
        .expect("MemoryBlobStore reader creation is infallible");
    let validation = validate_catalog_with_overlay(reader, Some(&overlay), &union)?;
    Ok((union, validation))
}

#[cfg(test)]
mod tests {
    use super::*;
    use hifitime::Epoch;
    use std::fs::File;
    use triblespace::core::blob::encodings::succinctarchive::SuccinctArchive;
    use triblespace::core::repo::pile::Pile;
    use triblespace::prelude::inlineencodings::Handle;

    fn id(byte: u8) -> Id {
        Id::new([byte; 16]).expect("test ids are non-nil")
    }

    fn instant(seconds: f64) -> Inline<NsTAIInterval> {
        let epoch = Epoch::from_unix_seconds(seconds);
        (epoch, epoch).try_to_inline().expect("valid interval")
    }

    fn text(value: &str) -> Fragment {
        text_fact(
            schema::content_fact::modality::TEXT,
            schema::content_fact::direction::IN,
            value,
        )
        .unwrap()
    }

    fn one_part(value: &str) -> Fragment {
        content_part(0, text(value), None).unwrap()
    }

    fn projection(parts: Fragment) -> Fragment {
        source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "session:test",
            br#"{"type":"test"}"#.as_slice(),
            block([], None, parts).unwrap(),
        )
        .unwrap()
    }

    fn empty_reader() -> (tempfile::TempDir, PileSnapshot) {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("empty.pile");
        File::create(&path).unwrap();
        let mut pile = Pile::open(&path).unwrap();
        let reader = pile.snapshot().unwrap();
        pile.close().unwrap();
        (directory, reader)
    }

    fn validate_fragment(fragment: &Fragment) -> CatalogValidation {
        let (_directory, reader) = empty_reader();
        validate_catalog_union(&reader, &TribleSet::new(), fragment)
            .unwrap()
            .1
    }

    fn without_blobs(fragment: Fragment) -> Fragment {
        let (_, facts, metafacts, _) = fragment.into_parts();
        Fragment::from_parts(facts, metafacts, Default::default())
    }

    #[test]
    fn part_ordinals_preserve_order_and_repeated_equal_facts() {
        let a = text("a");
        let b = text("b");

        let mut ab = Fragment::empty();
        ab += content_part(0, a.clone(), None).unwrap();
        ab += content_part(1, b.clone(), None).unwrap();
        let ab = block([], None, ab).unwrap();

        let mut ba = Fragment::empty();
        ba += content_part(0, b, None).unwrap();
        ba += content_part(1, a.clone(), None).unwrap();
        let ba = block([], None, ba).unwrap();
        assert_ne!(ab.root(), ba.root(), "part order is semantic");

        let fact = a.root().unwrap();
        let mut repeated = Fragment::empty();
        repeated += content_part(0, a.clone(), None).unwrap();
        repeated += content_part(1, a, None).unwrap();
        let repeated = block([], None, repeated).unwrap();
        let root = repeated.root().unwrap();
        let parts: Vec<Id> = find!(
            part: Id,
            pattern!(&repeated, [{ root @ schema::block::contains: ?part }])
        )
        .collect();
        assert_eq!(parts.len(), 2, "equal payloads at two positions survive");
        for part in parts {
            assert!(exists!(pattern!(&repeated, [{
                part @ schema::content_part::fact: &fact
            }])));
        }
    }

    #[test]
    fn raw_only_receipt_projects_to_the_canonical_bottom_block() {
        let receipt = projection(Fragment::empty());
        let receipt_id = receipt.root().unwrap();
        let block = find!(
            (block: Id),
            pattern!(&receipt, [{
                receipt_id @ schema::source_projection::projects_to: ?block
            }])
        )
        .next()
        .map(|(block,)| block)
        .unwrap();
        assert!(!exists!(pattern!(&receipt, [{
            block @ schema::block::contains: _?part
        }])));

        let (_directory, reader) = empty_reader();
        let (_, validation) = validate_catalog_union(&reader, &TribleSet::new(), &receipt).unwrap();
        assert_eq!(validation, CatalogValidation::Accepted);
    }

    #[test]
    fn archive_vocabulary_is_queryable_collection_data() {
        let mut vocabulary = vocabulary_fragment();
        let text = schema::content_fact::modality::TEXT;
        let alias = vocabulary.put::<UTF8String, _>("written text".to_owned());
        vocabulary += entity! { ExclusiveId::force_ref(&text) @
            metadata::name: alias,
        };
        let names: BTreeSet<_> = find!(
            value: TextHandle,
            pattern!(&vocabulary, [{
                text @ metadata::tag: &metadata::KIND_TAG,
                metadata::name: ?value,
            }])
        )
        .collect();
        let mut blobs = vocabulary.blobs().clone();
        let reader = blobs.snapshot().unwrap();
        let values: BTreeSet<String> = names
            .into_iter()
            .map(|name| {
                let value: anybytes::View<str> = reader.get(name).unwrap();
                value.to_string()
            })
            .collect();
        assert_eq!(
            values,
            BTreeSet::from(["text".to_owned(), "written text".to_owned()])
        );

        let (_directory, reader) = empty_reader();
        let (_, validation) =
            validate_catalog_union(&reader, &TribleSet::new(), &vocabulary).unwrap();
        assert_eq!(validation, CatalogValidation::Accepted);
    }

    #[test]
    fn source_snapshot_is_exact_path_independent_and_empty_safe() {
        let make = |path: Option<&str>| {
            source_snapshot(
                schema::source_projection::SOURCE_CLAUDE_CODE,
                "snapshot/v1/session:test",
                3,
                source_chunk(0, Bytes::from_source(b"abc".to_vec())).unwrap(),
                path.map(str::to_owned),
            )
            .unwrap()
        };
        let first = make(Some("/old/export.jsonl"));
        let moved = make(Some("/new/export.jsonl"));
        assert_eq!(first.root(), moved.root());
        assert_ne!(first.facts(), moved.facts());
        assert_eq!(validate_fragment(&first), CatalogValidation::Accepted);

        let empty = source_snapshot(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "snapshot/v1/empty",
            0,
            Fragment::empty(),
            None,
        )
        .unwrap();
        assert_eq!(validate_fragment(&empty), CatalogValidation::Accepted);
        assert!(!exists!(pattern!(&empty, [{
            _?chunk @ schema::source_chunk::bytes: _?raw
        }])));
    }

    #[test]
    fn source_snapshot_geometry_uses_store_lengths_without_reading_payloads() {
        let wrong_total = source_snapshot(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "snapshot/v1/wrong-total",
            2,
            source_chunk(0, Bytes::from_source(vec![1u8])).unwrap(),
            None,
        )
        .unwrap();
        let validation = validate_fragment(&wrong_total);
        match validation {
            CatalogValidation::Rejected(reason) => assert!(
                reason.contains("stores 1 bytes, expected 2"),
                "unexpected rejection: {reason}"
            ),
            other => panic!("expected length rejection, got {other:?}"),
        }

        let mut short_nonfinal = Fragment::empty();
        short_nonfinal += source_chunk(0, Bytes::from_source(vec![1u8])).unwrap();
        short_nonfinal += source_chunk(
            schema::source_chunk::CANONICAL_BYTES as u128,
            Bytes::from_source(vec![2u8]),
        )
        .unwrap();
        let short_nonfinal = source_snapshot(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "snapshot/v1/short-nonfinal",
            schema::source_chunk::CANONICAL_BYTES as u128 + 1,
            short_nonfinal,
            None,
        )
        .unwrap();
        let validation = validate_fragment(&short_nonfinal);
        match validation {
            CatalogValidation::Rejected(reason) => assert!(
                reason.contains("stores 1 bytes, expected 8388608"),
                "unexpected rejection: {reason}"
            ),
            other => panic!("expected nonfinal length rejection, got {other:?}"),
        }
    }

    #[test]
    fn source_snapshot_rejects_gaps_duplicates_and_orphan_chunks_before_residency() {
        let canonical = schema::source_chunk::CANONICAL_BYTES as u128;
        for (label, second_offset, expected) in [
            ("gap", canonical + 1, "starts at 8388609, expected 8388608"),
            ("duplicate", 0, "starts at 0, expected 8388608"),
        ] {
            let mut chunks = Fragment::empty();
            chunks += source_chunk(0, Bytes::from_source(vec![1u8])).unwrap();
            chunks += source_chunk(second_offset, Bytes::from_source(vec![2u8])).unwrap();
            let snapshot = source_snapshot(
                schema::source_projection::SOURCE_CLAUDE_CODE,
                format!("snapshot/v1/{label}"),
                canonical + 1,
                chunks,
                None,
            )
            .unwrap();
            let snapshot = without_blobs(snapshot);
            assert!(matches!(
                validate_fragment(&snapshot),
                CatalogValidation::Rejected(reason) if reason.contains(expected)
            ));
        }

        let orphan = source_chunk(0, Bytes::from_source(vec![1u8])).unwrap();
        assert!(matches!(
            validate_fragment(&orphan),
            CatalogValidation::Rejected(reason) if reason.contains("orphan source chunk")
        ));
    }

    #[test]
    fn source_snapshot_with_absent_bytes_is_pending_not_rejected() {
        let snapshot = source_snapshot(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "snapshot/v1/missing",
            1,
            source_chunk(0, Bytes::from_source(vec![7u8])).unwrap(),
            None,
        )
        .unwrap();
        let raw = find!(
            handle: RawHandle,
            pattern!(&snapshot, [{ _?chunk @ schema::source_chunk::bytes: ?handle }])
        )
        .next()
        .unwrap();
        let snapshot = without_blobs(snapshot);
        assert!(matches!(
            validate_fragment(&snapshot),
            CatalogValidation::Pending { missing } if missing.contains(&raw.raw)
        ));
    }

    #[test]
    fn timestamp_is_a_repeatable_annotation_not_intrinsic_state() {
        let without = block([], None, one_part("same")).unwrap();
        let with_42 = block([], Some(instant(42.0)), one_part("same")).unwrap();
        let with_43 = block([], Some(instant(43.0)), one_part("same")).unwrap();
        assert_eq!(without.root(), with_42.root());
        assert_eq!(without.root(), with_43.root());
        let root = without.root().unwrap();
        assert!(!exists!(pattern!(&without, [{
            root @ schema::block::timestamp: _?timestamp
        }])));

        let mut observed_twice = with_42;
        observed_twice += with_43;
        let projected = source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "timestamp-observations",
            b"{}".as_slice(),
            observed_twice,
        )
        .unwrap();
        validate_catalog_structure(&projected.into_facts()).unwrap();
    }

    #[test]
    fn source_projection_is_idempotent_but_preserves_occurrences() {
        let make = |locator: &str| {
            source_projection(
                schema::source_projection::SOURCE_CLAUDE_CODE,
                locator,
                br#"{\"type\":\"user\"}"#.as_slice(),
                block([], None, one_part("hello")).unwrap(),
            )
            .unwrap()
        };
        let first = make("session:7");
        let replay = make("session:7");
        let other_locator = make("session:8");
        let other_raw = source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "session:7",
            b"different raw record".as_slice(),
            block([], None, one_part("hello")).unwrap(),
        )
        .unwrap();
        assert_eq!(first, replay);
        assert_ne!(first.root(), other_locator.root());
        assert_ne!(first.root(), other_raw.root());

        let projected = |projection: &Fragment| {
            let root = projection.root().unwrap();
            find!(
                block: Id,
                pattern!(projection, [{
                    root @ schema::source_projection::projects_to: ?block
                }])
            )
            .next()
            .unwrap()
        };
        assert_eq!(projected(&first), projected(&other_locator));
        assert_eq!(projected(&first), projected(&other_raw));
    }

    #[test]
    fn occurrence_annotations_do_not_change_projection_identity() {
        let projection = source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "session:7",
            b"raw".as_slice(),
            block([], None, one_part("hello")).unwrap(),
        )
        .unwrap();
        let root = projection.root().unwrap();
        let annotated = annotate_source_projection(
            projection,
            ProjectionAnnotations {
                semantic_predecessor_support: vec![id(8)],
                source_timestamp: Some(instant(42.0)),
                author: Some(id(9)),
                experiencer: Some(id(10)),
                raw_author: Some("human".into()),
                raw_role: Some("user".into()),
                raw_model: Some("model-v1".into()),
                source_path: Some("/moved/export.jsonl".into()),
            },
        )
        .unwrap();
        assert_eq!(annotated.root(), Some(root));
        assert!(exists!(pattern!(&annotated, [{
            root @ schema::source_projection::author: &id(9),
            schema::source_projection::experiencer: &id(10),
        }])));
        assert!(exists!(pattern!(&annotated, [{
            root @ files_schema::file::source_path: _?path
        }])));
    }

    #[test]
    fn semantic_predecessor_support_must_project_to_a_direct_predecessor() {
        let parent = block([], None, one_part("parent")).unwrap();
        let parent_id = parent.root().unwrap();
        let unrelated = block([], None, one_part("unrelated")).unwrap();
        let child = block([parent_id], None, one_part("child")).unwrap();

        let parent_receipt = source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "session:parent",
            b"parent".as_slice(),
            parent,
        )
        .unwrap();
        let unrelated_receipt = source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "session:unrelated",
            b"unrelated".as_slice(),
            unrelated,
        )
        .unwrap();
        let unrelated_receipt_id = unrelated_receipt.root().unwrap();
        let child_receipt = source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "session:child",
            b"child".as_slice(),
            child,
        )
        .and_then(|projection| {
            annotate_source_projection(
                projection,
                ProjectionAnnotations {
                    semantic_predecessor_support: vec![unrelated_receipt_id],
                    ..ProjectionAnnotations::default()
                },
            )
        })
        .unwrap();

        let mut fragment = parent_receipt;
        fragment += unrelated_receipt;
        fragment += child_receipt;
        let (_directory, reader) = empty_reader();
        let (_, validation) =
            validate_catalog_union(&reader, &TribleSet::new(), &fragment).unwrap();
        assert!(matches!(
            validation,
            CatalogValidation::Rejected(reason) if reason.contains("for unrelated block")
        ));
    }

    #[test]
    fn binary_facts_share_files_media_type_entities() {
        let fact = blob_fact(
            schema::content_fact::modality::IMAGE,
            schema::content_fact::direction::IN,
            b"png".as_slice(),
            "IMAGE/PNG; charset=binary",
        )
        .unwrap();
        let root = fact.root().unwrap();
        let media_types: Vec<Id> = find!(
            media_type: Id,
            pattern!(&fact, [{ root @ schema::content_fact::media_type: ?media_type }])
        )
        .collect();
        assert_eq!(media_types.len(), 1);
        assert!(exists!(pattern!(&fact, [{
            media_types[0] @ metadata::tag: &files_schema::KIND_MEDIA_TYPE
        }])));
    }

    #[test]
    fn only_the_canonical_bottom_block_may_be_content_free() {
        let bottom = block([], None, Fragment::empty()).unwrap();
        let predecessor = block([], None, one_part("predecessor")).unwrap();
        let predecessor_id = predecessor.root().unwrap();

        assert!(block([predecessor_id], None, Fragment::empty()).is_err());
        assert!(block([], Some(instant(42.0)), Fragment::empty()).is_err());

        let invalid_core = entity! { _ @
            schema::block::previous: &predecessor_id,
        };
        let invalid = attach_kind(invalid_core, schema::block::KIND, "block").unwrap();
        let invalid_id = invalid.root().unwrap();
        let receipt = source_projection(
            schema::source_projection::SOURCE_CLAUDE_CODE,
            "invalid:content-free-child",
            b"{}".as_slice(),
            invalid,
        )
        .unwrap();
        let mut catalog = bottom;
        catalog += predecessor;
        catalog += receipt;
        let (_directory, reader) = empty_reader();
        let (_, validation) = validate_catalog_union(&reader, &TribleSet::new(), &catalog).unwrap();
        assert!(matches!(
            validation,
            CatalogValidation::Rejected(reason)
                if reason.contains("is not the predecessor-free, timeless canonical bottom")
        ));
        assert!(exists!(pattern!(&catalog, [{
            invalid_id @ schema::block::previous: &predecessor_id
        }])));
    }

    #[test]
    fn kind_markers_do_not_participate_in_intrinsic_ids() {
        let fact = text("identity");
        let fact_id = fact.root().unwrap();
        let payload = find!(
            payload: Inline<Handle<UTF8String>>,
            pattern!(&fact, [{
                fact_id @ schema::content_fact::payload: ?payload
            }])
        )
        .next()
        .unwrap();
        let core = entity! { _ @
            schema::content_fact::modality: &schema::content_fact::modality::TEXT,
            schema::content_fact::direction: &schema::content_fact::direction::IN,
            schema::content_fact::payload: payload,
        };
        assert_eq!(core.root(), Some(fact_id));
        assert!(exists!(pattern!(&fact, [{
            fact_id @ metadata::tag: &schema::content_fact::KIND
        }])));
    }

    #[test]
    fn semantic_response_edges_participate_in_part_identity() {
        let target = content_part(0, text("call"), None).unwrap().root().unwrap();
        let unrelated = content_part(1, text("result"), None).unwrap();
        let related = content_part(1, text("result"), Some(target)).unwrap();
        assert_ne!(unrelated.root(), related.root());
    }

    #[test]
    fn resolving_pointer_bytes_keeps_the_intrinsic_root() {
        let fact = asset_pointer_fact(
            schema::content_fact::modality::IMAGE,
            schema::content_fact::direction::IN,
            id(11),
            "asset-42",
            Some("image/png"),
            Some(3),
        )
        .unwrap();
        let root = fact.root().unwrap();
        let resolved = resolve_pointer_fact(fact, b"png".as_slice()).unwrap();
        assert_eq!(resolved.root(), Some(root));
        let handles: Vec<Inline<Handle<RawBytes>>> = find!(
            handle: Inline<Handle<RawBytes>>,
            pattern!(&resolved, [{ root @ schema::content_fact::resolved_to: ?handle }])
        )
        .collect();
        assert_eq!(handles.len(), 1);
    }

    #[test]
    fn a_part_selects_the_unique_pointer_resolution_in_its_identity() {
        let make = |bytes: &'static [u8]| {
            asset_pointer_fact(
                schema::content_fact::modality::FILE,
                schema::content_fact::direction::IN,
                id(11),
                "sandbox:/same",
                Some("application/octet-stream"),
                Some(bytes.len() as u128),
            )
            .and_then(|fact| resolve_pointer_fact(fact, bytes))
            .unwrap()
        };
        let first = make(b"first");
        let replay = make(b"first");
        let second = make(b"other");
        assert_eq!(first, replay);
        assert_eq!(first.root(), second.root());

        let first = content_part(0, first, None).unwrap();
        let replay = content_part(0, replay, None).unwrap();
        let second = content_part(0, second, None).unwrap();
        assert_eq!(first, replay);
        assert_ne!(first.root(), second.root());

        for part in [first, second] {
            let root = part.root().unwrap();
            let resolutions: Vec<Inline<Handle<RawBytes>>> = find!(
                resolution: Inline<Handle<RawBytes>>,
                pattern!(&part, [{
                    root @ schema::content_part::resolution: ?resolution
                }])
            )
            .collect();
            assert_eq!(resolutions.len(), 1);
        }
    }

    #[test]
    fn staged_complete_projection_is_accepted_without_writing() {
        let (_directory, reader) = empty_reader();
        let fragment = projection(one_part("hello from the staged overlay"));
        let (union, validation) =
            validate_catalog_union(&reader, &TribleSet::new(), &fragment).unwrap();
        assert_eq!(union, fragment.facts().clone());
        assert_eq!(validation, CatalogValidation::Accepted);
    }

    #[test]
    fn complete_graph_without_attachments_is_pending() {
        let (_directory, reader) = empty_reader();
        let fragment = projection(one_part("hello without resident bytes"));
        validate_catalog_structure(fragment.facts()).unwrap();
        let validation = validate_catalog(&reader, fragment.facts()).unwrap();
        let CatalogValidation::Pending { missing } = validation else {
            panic!("complete graph with absent blobs should be pending");
        };
        assert_eq!(missing.len(), 3, "payload, locator, and raw source record");
    }

    #[test]
    fn succinct_cover_validates_the_same_exact_logical_catalog() {
        let (_directory, reader) = empty_reader();
        let fragment = projection(one_part("hello from a succinct cover"));
        let segment = SuccinctArchive::<OrderedUniverse>::from(fragment.facts());
        let cover = UnionArchive::new(vec![segment.clone(), segment]);

        let validation = validate_succinct_catalog(&reader, &cover).unwrap();
        let CatalogValidation::Pending { missing } = validation else {
            panic!("complete succinct graph with absent blobs should be pending");
        };
        assert_eq!(missing.len(), 3, "payload, locator, and raw source record");
    }

    #[test]
    fn structural_rejection_dominates_missing_attachments() {
        let (_directory, reader) = empty_reader();
        let malformed = projection(content_part(1, text("ordinal gap"), None).unwrap());
        let validation = validate_catalog(&reader, malformed.facts()).unwrap();
        assert!(matches!(
            validation,
            CatalogValidation::Rejected(reason) if reason.contains("non-contiguous part ordinals")
        ));
    }

    #[test]
    fn unresolved_and_ambiguously_resolved_pointers_are_valid() {
        let (_directory, reader) = empty_reader();
        let unresolved = asset_pointer_fact(
            schema::content_fact::modality::IMAGE,
            schema::content_fact::direction::IN,
            id(11),
            "vendor:asset-42",
            Some("image/png"),
            Some(3),
        )
        .unwrap();
        let unresolved = projection(content_part(0, unresolved, None).unwrap());
        let (_, validation) =
            validate_catalog_union(&reader, &TribleSet::new(), &unresolved).unwrap();
        assert_eq!(validation, CatalogValidation::Accepted);

        let resolved = asset_pointer_fact(
            schema::content_fact::modality::IMAGE,
            schema::content_fact::direction::IN,
            id(11),
            "vendor:asset-42",
            None,
            None,
        )
        .and_then(|fact| resolve_pointer_fact(fact, b"first".as_slice()))
        .and_then(|fact| resolve_pointer_fact(fact, b"second".as_slice()))
        .unwrap();
        let resolved = projection(content_part(0, resolved, None).unwrap());
        let (_, validation) =
            validate_catalog_union(&reader, &TribleSet::new(), &resolved).unwrap();
        assert_eq!(validation, CatalogValidation::Accepted);
    }

    #[test]
    fn exact_coverage_rejects_orphan_content() {
        let (_directory, reader) = empty_reader();
        let mut fragment = projection(one_part("reachable"));
        fragment += text("orphan");
        let (_, validation) =
            validate_catalog_union(&reader, &TribleSet::new(), &fragment).unwrap();
        assert!(matches!(
            validation,
            CatalogValidation::Rejected(reason) if reason.contains("orphan content fact")
        ));
    }

    #[test]
    fn source_paths_are_additive_occurrence_annotations() {
        let (_directory, reader) = empty_reader();
        let projection = projection(one_part("moved export"));
        let root = projection.root().unwrap();
        let projection = annotate_source_projection(
            projection,
            ProjectionAnnotations {
                source_path: Some("/first/export.jsonl".into()),
                ..ProjectionAnnotations::default()
            },
        )
        .and_then(|projection| {
            annotate_source_projection(
                projection,
                ProjectionAnnotations {
                    source_path: Some("/second/export.jsonl".into()),
                    ..ProjectionAnnotations::default()
                },
            )
        })
        .unwrap();
        assert_eq!(projection.root(), Some(root));
        let (_, validation) =
            validate_catalog_union(&reader, &TribleSet::new(), &projection).unwrap();
        assert_eq!(validation, CatalogValidation::Accepted);
    }

    #[test]
    fn closed_modality_vocabulary_is_enforced_before_residency() {
        let (_directory, reader) = empty_reader();
        let unknown = text_fact(id(12), schema::content_fact::direction::IN, "unknown").unwrap();
        let fragment = projection(content_part(0, unknown, None).unwrap());
        let validation = validate_catalog(&reader, fragment.facts()).unwrap();
        assert!(matches!(
            validation,
            CatalogValidation::Rejected(reason) if reason.contains("unknown modality")
        ));
    }
}