pointbreak 0.10.0

Durable terminal code review for changes humans and coding agents collaborate on together
Documentation
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
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
//! Bodyless SQLite semantic facts shared by the dormant product profile and qualification.
#![cfg_attr(not(test), allow(dead_code))]

use std::collections::BTreeSet;

use rusqlite::{OptionalExtension, Transaction, params};
use sha2::{Digest, Sha256};

use super::locator::{SqliteLocator, SqliteLocatorError, read_locator_checkpoint};
use crate::canonical_hash::sha256_bytes_hex;
use crate::session::derived_access::QualificationLocalJournal;
use crate::session::derived_access::cursor::{CursorDelta, TruthCursor};
use crate::session::derived_access::locator::{LocatorRead, LocatorRow};
use crate::session::derived_access::semantic::state::{
    MaterializedSemanticDuplicate, MaterializedSemanticState, SemanticStateSnapshot,
};
use crate::session::derived_access::semantic::{
    AssessmentFact, CommitAssociationFact, CommitWithdrawalFact, InputRequestFact,
    InputResponseFact, MaterializedAttentionSnapshot, RefAssociationFact, RefWithdrawalFact,
    RevisionFact, SemanticFact, SemanticFactKind, SemanticModelError, SemanticSnapshot,
    ValidationFact, decode_enum, decode_string_list, encode_enum, encode_string_list,
};
use crate::session::event::{
    EventSignatureRecordedPayload, EventType, ReviewObservationRecordedPayload, ShoreEvent,
    WorkObjectProposal, WorkObjectProposedPayload,
};
use crate::session::projection::change::{ChangeProjectionFact, project_changes_from_facts};
use crate::session::workflow::tag_completion_key;
use crate::session::{EventStore, parse_event_instant};

const SEMANTIC_PROFILE_ID: &str = "pointbreak.sqlite-derived-access-semantic.v1";
const SEMANTIC_SCHEMA_VERSION: i64 = 7;
const PRODUCT_HISTORY_PROFILE_ID: &str = "pointbreak.sqlite-derived-access-history.v1";
const PRODUCT_HISTORY_SCHEMA_VERSION: i64 = 3;

#[derive(Clone, Debug)]
pub(crate) struct SqliteSemantic {
    locator: SqliteLocator,
}

#[derive(Debug)]
pub(crate) struct HydratedSemanticFact {
    pub(crate) fact: SemanticFact,
    pub(crate) event: ShoreEvent,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SemanticInventory {
    pub(crate) profile_id: String,
    pub(crate) schema_version: u32,
    pub(crate) fact_count: u64,
    pub(crate) tables: Vec<String>,
    pub(crate) columns: Vec<String>,
    pub(crate) indexes: Vec<String>,
    pub(crate) retained_body_object_bytes: u64,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct ProductHistoryFact {
    sequence: u64,
    tag_keys: Vec<String>,
    signature_target_event_id: Option<String>,
    revision: Option<ProductRevisionFact>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct ProductRevisionFact {
    revision_id: String,
    captured_at: String,
    captured_at_millis: i64,
    supersedes: Vec<String>,
}

impl ProductHistoryFact {
    pub(crate) fn from_event(
        sequence: u64,
        event: &ShoreEvent,
    ) -> Result<Self, SemanticModelError> {
        let mut tag_keys = Vec::new();
        let mut signature_target_event_id = None;
        let mut revision = None;
        match event.event_type {
            EventType::WorkObjectProposed => {
                let payload: WorkObjectProposedPayload =
                    serde_json::from_value(event.payload.clone())?;
                if let WorkObjectProposal::Revision {
                    revision: proposed,
                    supersedes,
                    ..
                } = payload.work_object
                {
                    let captured_at_millis =
                        parse_event_instant(&event.occurred_at).ok_or_else(|| {
                            SemanticModelError::InvalidEventInstant(event.occurred_at.clone())
                        })?;
                    revision = Some(ProductRevisionFact {
                        revision_id: proposed.id.as_str().to_owned(),
                        captured_at: event.occurred_at.clone(),
                        captured_at_millis,
                        supersedes: supersedes
                            .iter()
                            .map(|revision| revision.as_str().to_owned())
                            .collect(),
                    });
                }
            }
            EventType::ReviewObservationRecorded => {
                let payload: ReviewObservationRecordedPayload =
                    serde_json::from_value(event.payload.clone())?;
                tag_keys.extend(
                    payload
                        .tags
                        .iter()
                        .filter_map(|tag| tag_completion_key(tag)),
                );
                tag_keys.sort();
                tag_keys.dedup();
            }
            EventType::EventSignatureRecorded => {
                let payload: EventSignatureRecordedPayload =
                    serde_json::from_value(event.payload.clone())?;
                signature_target_event_id = Some(payload.target_event_id.as_str().to_owned());
            }
            _ => {}
        }
        Ok(Self {
            sequence,
            tag_keys,
            signature_target_event_id,
            revision,
        })
    }
}

#[derive(Debug, thiserror::Error)]
pub(crate) enum SqliteSemanticError {
    #[error(transparent)]
    Locator(#[from] SqliteLocatorError),
    #[error(transparent)]
    Model(#[from] SemanticModelError),
    #[error("semantic metadata mismatch: {0}")]
    Metadata(String),
    #[error("derived product history requires rebuild: {0}")]
    ProductHistoryUpgradeRequired(String),
    #[error("semantic projection requires rebuild: {0}")]
    UpgradeRequired(String),
    #[error("semantic delta does not follow its checkpoint: {0}")]
    Delta(String),
    #[error("semantic SQLite failure during {operation}: {message}")]
    Sqlite {
        operation: &'static str,
        message: String,
    },
    #[error("semantic carrier does not match persisted fact at {0:?}")]
    CarrierMismatch(TruthCursor),
}

impl SqliteSemantic {
    pub(crate) fn open(locator: SqliteLocator) -> Result<Self, SqliteSemanticError> {
        let connection = locator.validated_connection()?;
        let locator_checkpoint = read_locator_checkpoint(&connection)?;
        let semantic_schema_exists = connection
            .query_row(
                "SELECT EXISTS(
                     SELECT 1 FROM sqlite_schema
                     WHERE type = 'table' AND name = 'semantic_meta'
                 )",
                [],
                |row| row.get::<_, bool>(0),
            )
            .map_err(|error| sqlite_error("inspect semantic schema", error))?;
        if semantic_schema_exists {
            let schema_version = connection
                .query_row(
                    "SELECT schema_version FROM semantic_meta WHERE singleton = 1",
                    [],
                    |row| row.get::<_, i64>(0),
                )
                .map_err(|error| sqlite_error("inspect semantic version", error))?;
            if schema_version < SEMANTIC_SCHEMA_VERSION {
                return Err(SqliteSemanticError::UpgradeRequired(format!(
                    "existing semantic schema {schema_version} predates version \
                     {SEMANTIC_SCHEMA_VERSION}"
                )));
            }
            if schema_version > SEMANTIC_SCHEMA_VERSION {
                return Err(SqliteSemanticError::Metadata(format!(
                    "existing semantic schema {schema_version} is newer than version \
                     {SEMANTIC_SCHEMA_VERSION}"
                )));
            }
        }
        let product_history_exists = connection
            .query_row(
                "SELECT EXISTS(
                     SELECT 1 FROM sqlite_schema
                     WHERE type = 'table' AND name = 'product_history_meta'
                 )",
                [],
                |row| row.get::<_, bool>(0),
            )
            .map_err(|error| sqlite_error("inspect product history schema", error))?;
        if !product_history_exists && locator_checkpoint.applied.sequence != 0 {
            return Err(SqliteSemanticError::ProductHistoryUpgradeRequired(format!(
                "existing locator cursor {:?} predates the product history schema",
                locator_checkpoint.applied
            )));
        }
        if product_history_exists && locator_checkpoint.applied.sequence != 0 {
            let schema_version = connection
                .query_row(
                    "SELECT schema_version FROM product_history_meta WHERE singleton = 1",
                    [],
                    |row| row.get::<_, i64>(0),
                )
                .map_err(|error| sqlite_error("inspect product history version", error))?;
            if schema_version < PRODUCT_HISTORY_SCHEMA_VERSION {
                return Err(SqliteSemanticError::ProductHistoryUpgradeRequired(format!(
                    "existing product history schema {schema_version} predates version \
                     {PRODUCT_HISTORY_SCHEMA_VERSION}"
                )));
            }
        }
        connection
            .execute_batch(
                "CREATE TABLE IF NOT EXISTS semantic_meta (
                     singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
                     profile_id TEXT NOT NULL,
                     schema_version INTEGER NOT NULL CHECK (schema_version = 7),
                     epoch INTEGER NOT NULL CHECK (epoch > 0),
                     applied_sequence INTEGER NOT NULL CHECK (applied_sequence >= 0)
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_identity_prefix (
                     id INTEGER PRIMARY KEY,
                     value TEXT NOT NULL UNIQUE
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_actor (
                     id INTEGER PRIMARY KEY,
                     value TEXT NOT NULL UNIQUE
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_event_fact (
                     sequence INTEGER PRIMARY KEY CHECK (sequence > 0)
                         REFERENCES locator_event(sequence),
                     revision_prefix_id INTEGER REFERENCES semantic_identity_prefix(id),
                     revision_digest BLOB CHECK (length(revision_digest) = 32),
                     revision_raw TEXT,
                     semantic_prefix_id INTEGER REFERENCES semantic_identity_prefix(id),
                     semantic_digest BLOB CHECK (length(semantic_digest) = 32),
                     semantic_raw TEXT,
                     content_prefix_id INTEGER REFERENCES semantic_identity_prefix(id),
                     content_digest BLOB CHECK (length(content_digest) = 32),
                     content_raw TEXT,
                     occurred_at TEXT NOT NULL,
                     assertion_mode INTEGER NOT NULL CHECK (assertion_mode IN (0, 1)),
                     actor_id INTEGER NOT NULL REFERENCES semantic_actor(id),
                     CHECK (
                         (revision_prefix_id IS NULL AND revision_digest IS NULL)
                         OR (revision_prefix_id IS NOT NULL AND revision_digest IS NOT NULL)
                     ),
                     CHECK (
                         (semantic_prefix_id IS NULL AND semantic_digest IS NULL)
                         OR (semantic_prefix_id IS NOT NULL AND semantic_digest IS NOT NULL)
                     ),
                     CHECK (
                         (content_prefix_id IS NULL AND content_digest IS NULL)
                         OR (content_prefix_id IS NOT NULL AND content_digest IS NOT NULL)
                     )
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_revision_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     object_id TEXT NOT NULL,
                     engagement_id TEXT NOT NULL,
                     supersedes_json TEXT NOT NULL,
                     base_commit_oid TEXT,
                     capture_commit_oid TEXT,
                     capture_tree_oid TEXT
                 ) STRICT;
                 CREATE INDEX IF NOT EXISTS semantic_revision_engagement
                     ON semantic_revision_fact(engagement_id, sequence);
                 CREATE TABLE IF NOT EXISTS semantic_assessment_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     assessment TEXT NOT NULL,
                     replaces_json TEXT NOT NULL,
                     related_observations_json TEXT NOT NULL,
                     related_requests_json TEXT NOT NULL,
                     revision_scoped INTEGER NOT NULL CHECK (revision_scoped IN (0, 1))
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_request_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     reason_code TEXT NOT NULL,
                     title TEXT NOT NULL
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_response_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     request_id TEXT NOT NULL
                 ) STRICT;
                 CREATE INDEX IF NOT EXISTS semantic_response_request
                     ON semantic_response_fact(request_id);
                 CREATE TABLE IF NOT EXISTS semantic_validation_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     check_name TEXT NOT NULL,
                     status TEXT NOT NULL,
                     exit_code INTEGER,
                     completed_at TEXT,
                     log_hashes_json TEXT NOT NULL
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_commit_association_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     commit_oid TEXT NOT NULL,
                     tree_oid TEXT NOT NULL
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_commit_withdrawal_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     association_id TEXT NOT NULL
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_ref_association_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     ref_name TEXT NOT NULL,
                     head_oid TEXT NOT NULL
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_ref_withdrawal_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     association_id TEXT NOT NULL
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_change_fact (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     fact_json TEXT NOT NULL
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_representative (
                     family_id INTEGER NOT NULL CHECK (family_id BETWEEN 1 AND 12),
                     semantic_key_prefix_id INTEGER
                         REFERENCES semantic_identity_prefix(id),
                     semantic_key_digest BLOB CHECK (length(semantic_key_digest) = 32),
                     semantic_key_raw TEXT,
                     semantic_key_hash BLOB NOT NULL CHECK (length(semantic_key_hash) = 32),
                     sequence INTEGER NOT NULL REFERENCES semantic_event_fact(sequence),
                     CHECK (
                         (
                             semantic_key_prefix_id IS NULL
                             AND semantic_key_digest IS NULL
                             AND semantic_key_raw IS NOT NULL
                         )
                         OR (
                             semantic_key_prefix_id IS NOT NULL
                             AND semantic_key_digest IS NOT NULL
                             AND semantic_key_raw IS NULL
                         )
                     ),
                     PRIMARY KEY (family_id, semantic_key_hash)
                 ) STRICT, WITHOUT ROWID;
                 CREATE INDEX IF NOT EXISTS semantic_representative_sequence
                     ON semantic_representative(sequence, family_id);
                 CREATE VIEW IF NOT EXISTS semantic_representative_text AS
                 SELECT representative.family_id,
                        CASE representative.family_id
                            WHEN 1 THEN 'revision'
                            WHEN 2 THEN 'observation'
                            WHEN 3 THEN 'assessment'
                            WHEN 4 THEN 'request'
                            WHEN 5 THEN 'response'
                            WHEN 6 THEN 'validation'
                            WHEN 7 THEN 'commit_association'
                            WHEN 8 THEN 'commit_withdrawal'
                            WHEN 9 THEN 'ref_association'
                            WHEN 10 THEN 'ref_withdrawal'
                            WHEN 11 THEN 'removal'
                            WHEN 12 THEN 'change_record'
                        END AS family,
                        coalesce(
                            representative.semantic_key_raw,
                            prefix.value || lower(hex(representative.semantic_key_digest))
                        ) AS semantic_key,
                        representative.semantic_key_hash,
                        representative.sequence
                 FROM semantic_representative AS representative
                 LEFT JOIN semantic_identity_prefix AS prefix
                   ON prefix.id = representative.semantic_key_prefix_id;
                 CREATE TABLE IF NOT EXISTS semantic_duplicate_projection (
                     family TEXT NOT NULL,
                     semantic_key TEXT NOT NULL,
                     event_count INTEGER NOT NULL CHECK (event_count >= 1),
                     event_ids_json TEXT NOT NULL,
                     PRIMARY KEY (family, semantic_key)
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS semantic_state_projection (
                     singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
                     journal_id TEXT NOT NULL,
                     current_revision_id TEXT,
                     current_object_id TEXT,
                     revision_count INTEGER NOT NULL CHECK (revision_count >= 0),
                     event_count INTEGER NOT NULL CHECK (event_count >= 0),
                     observation_count INTEGER NOT NULL CHECK (observation_count >= 0),
                     assessment_count INTEGER NOT NULL CHECK (assessment_count >= 0),
                     validation_check_count INTEGER NOT NULL
                         CHECK (validation_check_count >= 0),
                     input_request_count INTEGER NOT NULL CHECK (input_request_count >= 0),
                     open_input_request_count INTEGER NOT NULL
                         CHECK (open_input_request_count >= 0),
                     open_operative_input_request_count INTEGER NOT NULL
                         CHECK (open_operative_input_request_count >= 0)
                 ) STRICT;
                 CREATE INDEX IF NOT EXISTS semantic_event_fact_revision
                     ON semantic_event_fact(
                         revision_prefix_id, revision_digest, revision_raw, sequence
                     );
                 CREATE INDEX IF NOT EXISTS semantic_event_fact_content
                     ON semantic_event_fact(
                         content_prefix_id, content_digest, content_raw, sequence
                     );
                 CREATE VIEW IF NOT EXISTS semantic_event_fact_text AS
                 SELECT event.sequence,
                        coalesce(
                            event.revision_raw,
                            revision_prefix.value || lower(hex(event.revision_digest))
                        ) AS revision_id,
                        coalesce(
                            event.semantic_raw,
                            semantic_prefix.value || lower(hex(event.semantic_digest))
                        ) AS semantic_id,
                        coalesce(
                            event.content_raw,
                            content_prefix.value || lower(hex(event.content_digest))
                        ) AS content_hash,
                        event.occurred_at,
                        CASE event.assertion_mode
                            WHEN 0 THEN 'advisory'
                            WHEN 1 THEN 'operative'
                        END AS assertion_mode,
                        actor.value AS actor_id,
                        event.revision_prefix_id,
                        event.revision_digest,
                        event.semantic_prefix_id,
                        event.semantic_digest,
                        event.content_prefix_id,
                        event.content_digest
                 FROM semantic_event_fact AS event
                 LEFT JOIN semantic_identity_prefix AS revision_prefix
                   ON revision_prefix.id = event.revision_prefix_id
                 LEFT JOIN semantic_identity_prefix AS semantic_prefix
                   ON semantic_prefix.id = event.semantic_prefix_id
                 LEFT JOIN semantic_identity_prefix AS content_prefix
                   ON content_prefix.id = event.content_prefix_id
                 JOIN semantic_actor AS actor ON actor.id = event.actor_id;
                 CREATE TABLE IF NOT EXISTS product_history_meta (
                     singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
                     profile_id TEXT NOT NULL,
                     schema_version INTEGER NOT NULL CHECK (schema_version = 3),
                     epoch INTEGER NOT NULL CHECK (epoch > 0),
                     applied_sequence INTEGER NOT NULL CHECK (applied_sequence >= 0)
                 ) STRICT;
                 CREATE TABLE IF NOT EXISTS product_history_tag (
                     sequence INTEGER NOT NULL REFERENCES semantic_event_fact(sequence),
                     tag_key TEXT NOT NULL,
                     PRIMARY KEY (sequence, tag_key)
                 ) STRICT, WITHOUT ROWID;
                 CREATE INDEX IF NOT EXISTS product_history_tag_key
                     ON product_history_tag(tag_key, sequence);
                 CREATE TABLE IF NOT EXISTS product_history_signature (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     target_event_id TEXT NOT NULL
                 ) STRICT;
                 CREATE INDEX IF NOT EXISTS product_history_signature_target
                     ON product_history_signature(target_event_id, sequence);
                 CREATE TABLE IF NOT EXISTS product_revision (
                     sequence INTEGER PRIMARY KEY REFERENCES semantic_event_fact(sequence),
                     revision_id TEXT NOT NULL,
                     captured_at TEXT NOT NULL,
                     captured_at_millis INTEGER NOT NULL
                 ) STRICT;
                 CREATE INDEX IF NOT EXISTS product_revision_identity
                     ON product_revision(revision_id, sequence);
                 CREATE INDEX IF NOT EXISTS product_revision_chronological
                     ON product_revision(captured_at_millis DESC, revision_id DESC, sequence);
                 CREATE TABLE IF NOT EXISTS product_revision_edge (
                     sequence INTEGER NOT NULL REFERENCES product_revision(sequence),
                     superseded_revision_id TEXT NOT NULL,
                     PRIMARY KEY (sequence, superseded_revision_id)
                 ) STRICT, WITHOUT ROWID;
                 CREATE INDEX IF NOT EXISTS product_revision_edge_target
                     ON product_revision_edge(superseded_revision_id, sequence);",
            )
            .map_err(|error| sqlite_error("create semantic schema", error))?;
        let inserted = connection
            .execute(
                "INSERT INTO semantic_meta
                 (singleton, profile_id, schema_version, epoch, applied_sequence)
                 VALUES (1, ?1, ?2, ?3, 0)
                 ON CONFLICT(singleton) DO NOTHING",
                params![
                    SEMANTIC_PROFILE_ID,
                    SEMANTIC_SCHEMA_VERSION,
                    to_i64(locator_checkpoint.applied.epoch, "semantic epoch")?,
                ],
            )
            .map_err(|error| sqlite_error("initialize semantic metadata", error))?;
        if inserted == 1 && locator_checkpoint.applied.sequence != 0 {
            return Err(SqliteSemanticError::Metadata(format!(
                "semantic profile requires deliberate rebuild for existing locator cursor {:?}",
                locator_checkpoint.applied
            )));
        }
        let product_inserted = connection
            .execute(
                "INSERT INTO product_history_meta
                 (singleton, profile_id, schema_version, epoch, applied_sequence)
                 VALUES (1, ?1, ?2, ?3, 0)
                 ON CONFLICT(singleton) DO NOTHING",
                params![
                    PRODUCT_HISTORY_PROFILE_ID,
                    PRODUCT_HISTORY_SCHEMA_VERSION,
                    to_i64(locator_checkpoint.applied.epoch, "product history epoch")?,
                ],
            )
            .map_err(|error| sqlite_error("initialize product history metadata", error))?;
        debug_assert!(product_inserted == 0 || locator_checkpoint.applied.sequence == 0);
        connection
            .execute(
                "INSERT INTO semantic_state_projection
                 (singleton, journal_id, current_revision_id, current_object_id,
                  revision_count, event_count, observation_count, assessment_count,
                  validation_check_count, input_request_count, open_input_request_count,
                  open_operative_input_request_count)
                 VALUES (1, 'journal:default', NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0)
                 ON CONFLICT(singleton) DO NOTHING",
                [],
            )
            .map_err(|error| sqlite_error("initialize semantic state projection", error))?;
        validate_meta(&connection, locator_checkpoint.applied)?;
        validate_product_history_meta(&connection, locator_checkpoint.applied)?;
        Ok(Self { locator })
    }

    pub(crate) fn apply_delta(
        &self,
        delta: &CursorDelta,
        locator_rows: &[LocatorRow],
        semantic_facts: &[SemanticFact],
        product_history_facts: &[ProductHistoryFact],
    ) -> Result<TruthCursor, SqliteSemanticError> {
        self.apply_delta_inner(
            delta,
            locator_rows,
            semantic_facts,
            product_history_facts,
            false,
        )
    }

    pub(crate) fn apply_delta_with_failure(
        &self,
        delta: &CursorDelta,
        locator_rows: &[LocatorRow],
        semantic_facts: &[SemanticFact],
        product_history_facts: &[ProductHistoryFact],
    ) -> Result<TruthCursor, SqliteSemanticError> {
        self.apply_delta_inner(
            delta,
            locator_rows,
            semantic_facts,
            product_history_facts,
            true,
        )
    }

    fn apply_delta_inner(
        &self,
        delta: &CursorDelta,
        locator_rows: &[LocatorRow],
        semantic_facts: &[SemanticFact],
        product_history_facts: &[ProductHistoryFact],
        inject_failure: bool,
    ) -> Result<TruthCursor, SqliteSemanticError> {
        if semantic_facts.len() != delta.receipts.len()
            || semantic_facts.len() != locator_rows.len()
            || semantic_facts.len() != product_history_facts.len()
        {
            return Err(SqliteSemanticError::Delta(format!(
                "{} semantic facts and {} locator rows for {} cursor receipts",
                semantic_facts.len(),
                locator_rows.len(),
                delta.receipts.len()
            )));
        }
        for ((receipt, locator), fact) in
            delta.receipts.iter().zip(locator_rows).zip(semantic_facts)
        {
            if fact.cursor != receipt.cursor
                || fact.logical_reread_key != receipt.logical_reread_key
                || fact.validation_witness != receipt.validation_witness
                || fact.event_id != locator.event_id
            {
                return Err(SqliteSemanticError::Delta(format!(
                    "semantic fact does not match receipt/locator at {:?}",
                    receipt.cursor
                )));
            }
        }
        let applied = delta
            .receipts
            .last()
            .map_or(delta.after, |receipt| receipt.cursor);
        let result = self
            .locator
            .apply_delta_with(delta, locator_rows, |transaction| {
                insert_facts(transaction, semantic_facts)?;
                insert_product_history_facts(transaction, product_history_facts)?;
                if inject_failure {
                    return Err(SqliteLocatorError::Delta(
                        "injected semantic transaction failure".to_owned(),
                    ));
                }
                let updated = transaction
                    .execute(
                        "UPDATE semantic_meta
                         SET applied_sequence = ?1
                         WHERE singleton = 1 AND epoch = ?2 AND applied_sequence = ?3",
                        params![
                            to_i64_locator(applied.sequence, "semantic applied")?,
                            to_i64_locator(applied.epoch, "semantic epoch")?,
                            to_i64_locator(delta.after.sequence, "semantic previous applied")?,
                        ],
                    )
                    .map_err(|error| locator_sqlite_error("advance semantic metadata", error))?;
                if updated != 1 {
                    return Err(SqliteLocatorError::Delta(
                        "semantic checkpoint changed concurrently".to_owned(),
                    ));
                }
                let product_updated = transaction
                    .execute(
                        "UPDATE product_history_meta
                         SET applied_sequence = ?1
                         WHERE singleton = 1 AND epoch = ?2 AND applied_sequence = ?3",
                        params![
                            to_i64_locator(applied.sequence, "product history applied")?,
                            to_i64_locator(applied.epoch, "product history epoch")?,
                            to_i64_locator(
                                delta.after.sequence,
                                "product history previous applied"
                            )?,
                        ],
                    )
                    .map_err(|error| {
                        locator_sqlite_error("advance product history metadata", error)
                    })?;
                if product_updated != 1 {
                    return Err(SqliteLocatorError::Delta(
                        "product history checkpoint changed concurrently".to_owned(),
                    ));
                }
                Ok(())
            });
        result.map_err(SqliteSemanticError::from)?;
        Ok(applied)
    }

    pub(crate) fn audit_snapshot(
        &self,
        observed: TruthCursor,
    ) -> Result<LocatorRead<SemanticSnapshot>, SqliteSemanticError> {
        let connection = self.locator.validated_connection()?;
        let checkpoint = read_locator_checkpoint(&connection)?;
        validate_meta(&connection, checkpoint.applied)?;
        if checkpoint.applied.epoch != observed.epoch
            || checkpoint.applied.sequence < observed.sequence
        {
            return Ok(LocatorRead::CatchUpRequired {
                applied: checkpoint.applied,
                observed,
            });
        }
        let journal = QualificationLocalJournal::new(self.locator.store_root());
        let facts = query_facts(
            &connection,
            &journal,
            "SELECT locator.epoch, event.sequence, receipt.logical_reread_key_hash,
                    locator.replay_key, locator.event_id, locator.event_type,
                    locator.journal_id, event.revision_id, event.semantic_id,
                    event.content_hash, locator.payload_hash,
                    event.occurred_at, event.assertion_mode,
                    locator.track_id, event.actor_id, receipt.validation_witness,
                    receipt.epoch
             FROM semantic_event_fact_text AS event
             JOIN locator_event_text AS locator ON locator.sequence = event.sequence
             JOIN cursor_receipt_text AS receipt ON receipt.sequence = event.sequence
             WHERE locator.epoch = ?1 AND event.sequence <= ?2
             ORDER BY locator.replay_key, receipt.logical_reread_key_hash",
            params![
                to_i64(observed.epoch, "snapshot epoch")?,
                to_i64(observed.sequence, "snapshot cursor")?,
            ],
        )?;
        let facts = hydrated_facts_only(facts);
        Ok(LocatorRead::Ready(SemanticSnapshot::audit_from_facts(
            observed, &facts,
        )?))
    }

    pub(crate) fn materialized_audit_snapshot(
        &self,
        observed: TruthCursor,
    ) -> Result<LocatorRead<SemanticSnapshot>, SqliteSemanticError> {
        let connection = self.locator.validated_connection()?;
        let checkpoint = read_locator_checkpoint(&connection)?;
        validate_meta(&connection, checkpoint.applied)?;
        if checkpoint.applied.epoch != observed.epoch
            || checkpoint.applied.sequence < observed.sequence
        {
            return Ok(LocatorRead::CatchUpRequired {
                applied: checkpoint.applied,
                observed,
            });
        }
        let state = query_materialized_state(&connection)?;
        let journal = QualificationLocalJournal::new(self.locator.store_root());
        let facts = hydrated_facts_only(query_materialized_facts(
            &connection,
            &journal,
            observed.epoch,
            observed.sequence,
            None,
            MaterializedFactFamilies::AllExceptObservations,
        )?);
        #[cfg(any(test, feature = "longitudinal-counting"))]
        {
            crate::bench_support::longitudinal::record_projection_rebuild();
            crate::bench_support::longitudinal::record_event_folds(facts.len());
        }
        Ok(LocatorRead::Ready(SemanticSnapshot::from_materialized(
            observed, state, &facts,
        )?))
    }

    pub(crate) fn materialized_attention_snapshot(
        &self,
        observed: TruthCursor,
    ) -> Result<LocatorRead<MaterializedAttentionSnapshot>, SqliteSemanticError> {
        let connection = self.locator.validated_connection()?;
        let checkpoint = read_locator_checkpoint(&connection)?;
        validate_meta(&connection, checkpoint.applied)?;
        if checkpoint.applied.epoch != observed.epoch
            || checkpoint.applied.sequence < observed.sequence
        {
            return Ok(LocatorRead::CatchUpRequired {
                applied: checkpoint.applied,
                observed,
            });
        }
        let state = query_materialized_state(&connection)?;
        let facts = query_materialized_compact_facts(
            &connection,
            observed.epoch,
            observed.sequence,
            None,
            MaterializedFactFamilies::Attention,
        )?;
        let supersession =
            crate::session::derived_access::semantic::thread::supersession_from_facts(&facts)?;
        let attention = crate::session::derived_access::semantic::attention::AttentionSemanticSnapshot::from_facts_with_supersession(
            &facts,
            &supersession,
        )?;
        Ok(LocatorRead::Ready(MaterializedAttentionSnapshot {
            as_of: observed,
            state,
            supersession,
            attention,
        }))
    }

    pub(crate) fn materialized_engagement_snapshot(
        &self,
        engagement_id: &str,
        observed: TruthCursor,
    ) -> Result<LocatorRead<SemanticSnapshot>, SqliteSemanticError> {
        let connection = self.locator.validated_connection()?;
        let checkpoint = read_locator_checkpoint(&connection)?;
        validate_meta(&connection, checkpoint.applied)?;
        if checkpoint.applied.epoch != observed.epoch
            || checkpoint.applied.sequence < observed.sequence
        {
            return Ok(LocatorRead::CatchUpRequired {
                applied: checkpoint.applied,
                observed,
            });
        }
        let state = query_materialized_state(&connection)?;
        let journal = QualificationLocalJournal::new(self.locator.store_root());
        let facts = hydrated_facts_only(query_materialized_facts(
            &connection,
            &journal,
            observed.epoch,
            observed.sequence,
            Some(engagement_id),
            MaterializedFactFamilies::AllExceptObservations,
        )?);
        let changes =
            query_materialized_change_projection(&connection, observed.epoch, observed.sequence)?;
        Ok(LocatorRead::Ready(
            SemanticSnapshot::from_materialized_with_changes(observed, state, &facts, changes)?,
        ))
    }

    pub(crate) fn facts_for_revision_hydrated(
        &self,
        revision_id: &str,
        observed: TruthCursor,
    ) -> Result<LocatorRead<Vec<HydratedSemanticFact>>, SqliteSemanticError> {
        let connection = self.locator.validated_connection()?;
        let checkpoint = read_locator_checkpoint(&connection)?;
        validate_meta(&connection, checkpoint.applied)?;
        if checkpoint.applied.epoch != observed.epoch
            || checkpoint.applied.sequence < observed.sequence
        {
            return Ok(LocatorRead::CatchUpRequired {
                applied: checkpoint.applied,
                observed,
            });
        }
        let epoch = to_i64(observed.epoch, "detail epoch")?;
        let sequence = to_i64(observed.sequence, "detail cursor")?;
        let journal = QualificationLocalJournal::new(self.locator.store_root());
        let facts = if let Some((prefix, digest)) = split_canonical_digest(revision_id) {
            query_facts(
                &connection,
                &journal,
                &selected_semantic_facts(
                    "physical.revision_prefix_id = (
                         SELECT id FROM semantic_identity_prefix WHERE value = ?1
                     )
                     AND physical.revision_digest = ?2",
                    "semantic_event_fact_revision",
                    3,
                    4,
                ),
                params![prefix, digest.as_slice(), epoch, sequence],
            )?
        } else {
            query_facts(
                &connection,
                &journal,
                &selected_semantic_facts(
                    "physical.revision_prefix_id IS NULL
                     AND physical.revision_digest IS NULL
                     AND physical.revision_raw = ?1",
                    "semantic_event_fact_revision",
                    2,
                    3,
                ),
                params![revision_id, epoch, sequence],
            )?
        };
        Ok(LocatorRead::Ready(facts))
    }

    pub(crate) fn content_is_removed(
        &self,
        content_hash: &str,
        observed: TruthCursor,
    ) -> Result<bool, SqliteSemanticError> {
        let connection = self.locator.validated_connection()?;
        let epoch = to_i64(observed.epoch, "removal epoch")?;
        let sequence = to_i64(observed.sequence, "removal cursor")?;
        let (query, parameters): (String, Vec<rusqlite::types::Value>) =
            if let Some((prefix, digest)) = split_canonical_digest(content_hash) {
                (
                    selected_content_query(
                        "event.content_prefix_id = (
                             SELECT id FROM semantic_identity_prefix WHERE value = ?1
                         )
                         AND event.content_digest = ?2",
                        3,
                        4,
                    ),
                    vec![
                        prefix.to_owned().into(),
                        digest.to_vec().into(),
                        epoch.into(),
                        sequence.into(),
                    ],
                )
            } else {
                (
                    selected_content_query(
                        "event.content_prefix_id IS NULL
                         AND event.content_digest IS NULL
                         AND event.content_raw = ?1",
                        2,
                        3,
                    ),
                    vec![
                        content_hash.to_owned().into(),
                        epoch.into(),
                        sequence.into(),
                    ],
                )
            };
        let count = connection
            .query_row(&query, rusqlite::params_from_iter(parameters), |_| Ok(()))
            .optional()
            .map_err(|error| sqlite_error("query removal fact", error))?;
        Ok(count.is_some())
    }

    pub(crate) fn product_history_connection(
        &self,
        observed: TruthCursor,
    ) -> Result<LocatorRead<(rusqlite::Connection, SemanticStateSnapshot)>, SqliteSemanticError>
    {
        let connection = self.locator.validated_connection()?;
        let checkpoint = read_locator_checkpoint(&connection)?;
        validate_meta(&connection, checkpoint.applied)?;
        validate_product_history_meta(&connection, checkpoint.applied)?;
        if checkpoint.applied.epoch != observed.epoch
            || checkpoint.applied.sequence < observed.sequence
        {
            return Ok(LocatorRead::CatchUpRequired {
                applied: checkpoint.applied,
                observed,
            });
        }
        let state = query_materialized_state(&connection)?;
        Ok(LocatorRead::Ready((connection, state)))
    }

    pub(crate) fn inventory(&self) -> Result<SemanticInventory, SqliteSemanticError> {
        let connection = self.locator.validated_connection()?;
        let (profile_id, schema_version) = connection
            .query_row(
                "SELECT profile_id, schema_version FROM semantic_meta WHERE singleton = 1",
                [],
                |row| Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?)),
            )
            .map_err(|error| sqlite_error("read semantic inventory identity", error))?;
        let fact_count = connection
            .query_row("SELECT count(*) FROM semantic_event_fact", [], |row| {
                row.get::<_, i64>(0)
            })
            .map_err(|error| sqlite_error("count semantic facts", error))?;
        let retained_body_object_bytes = retained_body_object_bytes(&connection)?;
        Ok(SemanticInventory {
            profile_id,
            schema_version: u32::try_from(schema_version)
                .map_err(|_| SqliteSemanticError::Metadata("negative schema version".to_owned()))?,
            fact_count: u64::try_from(fact_count)
                .map_err(|_| SqliteSemanticError::Metadata("negative fact count".to_owned()))?,
            tables: query_names(
                &connection,
                "SELECT name FROM sqlite_schema
                 WHERE type = 'table' AND name LIKE 'semantic_%'
                 ORDER BY name",
                0,
            )?,
            columns: query_names(&connection, "PRAGMA table_info(semantic_event_fact)", 1)?,
            indexes: query_names(&connection, "PRAGMA index_list(semantic_event_fact)", 1)?,
            retained_body_object_bytes,
        })
    }
}

fn retained_body_object_bytes(
    connection: &rusqlite::Connection,
) -> Result<u64, SqliteSemanticError> {
    let tables = query_names(
        connection,
        "SELECT name FROM sqlite_schema
         WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
         ORDER BY name",
        0,
    )?;
    let mut total = 0_u64;
    for table in tables {
        let pragma = format!("PRAGMA table_info({})", quote_identifier(&table));
        let mut statement = connection
            .prepare(&pragma)
            .map_err(|error| sqlite_error("inspect derived-access columns", error))?;
        let columns = statement
            .query_map([], |row| {
                Ok((row.get::<_, String>(1)?, row.get::<_, String>(2)?))
            })
            .map_err(|error| sqlite_error("inspect derived-access columns", error))?
            .collect::<Result<Vec<_>, _>>()
            .map_err(|error| sqlite_error("inspect derived-access columns", error))?;
        for (column, declared_type) in columns {
            if !is_retained_body_object_column(&column, &declared_type) {
                continue;
            }
            let query = format!(
                "SELECT coalesce(sum(length({})), 0) FROM {}",
                quote_identifier(&column),
                quote_identifier(&table)
            );
            let bytes = connection
                .query_row(&query, [], |row| row.get::<_, i64>(0))
                .map_err(|error| sqlite_error("measure retained body/object bytes", error))?;
            total = total.saturating_add(u64::try_from(bytes).map_err(|_| {
                SqliteSemanticError::Metadata("negative retained body/object bytes".to_owned())
            })?);
        }
    }
    Ok(total)
}

fn is_retained_body_object_column(name: &str, declared_type: &str) -> bool {
    let name = name.to_ascii_lowercase();
    if declared_type.eq_ignore_ascii_case("BLOB")
        && !name.ends_with("_digest")
        && !name.ends_with("_hash")
    {
        return true;
    }
    matches!(name.as_str(), "body" | "object" | "payload" | "content")
        || ["body", "object", "payload", "content"]
            .iter()
            .any(|subject| {
                ["bytes", "json", "text", "content"]
                    .iter()
                    .any(|representation| name == format!("{subject}_{representation}"))
            })
}

fn quote_identifier(value: &str) -> String {
    format!("\"{}\"", value.replace('"', "\"\""))
}

fn insert_facts(
    transaction: &Transaction<'_>,
    facts: &[SemanticFact],
) -> Result<(), SqliteLocatorError> {
    for fact in facts {
        let revision = encode_identity(transaction, fact.revision_id.as_deref())?;
        let semantic = encode_identity(transaction, fact.semantic_id.as_deref())?;
        let content = encode_identity(transaction, fact.content_hash.as_deref())?;
        let actor_id = semantic_dimension_id(transaction, "semantic_actor", &fact.actor_id)?;
        let assertion_mode = match fact.assertion_mode {
            crate::session::event::AssertionMode::Advisory => 0_i64,
            crate::session::event::AssertionMode::Operative => 1_i64,
        };
        transaction
            .execute(
                "INSERT INTO semantic_event_fact
                 (sequence,
                  revision_prefix_id, revision_digest, revision_raw,
                  semantic_prefix_id, semantic_digest, semantic_raw,
                  content_prefix_id, content_digest, content_raw,
                  occurred_at,
                  assertion_mode, actor_id)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)",
                params![
                    to_i64_locator(fact.cursor.sequence, "semantic sequence")?,
                    revision.prefix_id,
                    revision.digest.as_deref(),
                    revision.raw,
                    semantic.prefix_id,
                    semantic.digest.as_deref(),
                    semantic.raw,
                    content.prefix_id,
                    content.digest.as_deref(),
                    content.raw,
                    fact.occurred_at,
                    assertion_mode,
                    actor_id,
                ],
            )
            .map_err(|error| locator_sqlite_error("insert semantic fact", error))?;
        insert_family_fact(transaction, fact)?;
        update_materialized_projection(transaction, fact)?;
    }
    Ok(())
}

fn insert_product_history_facts(
    transaction: &Transaction<'_>,
    facts: &[ProductHistoryFact],
) -> Result<(), SqliteLocatorError> {
    for fact in facts {
        let sequence = to_i64_locator(fact.sequence, "product history sequence")?;
        for tag_key in &fact.tag_keys {
            transaction
                .execute(
                    "INSERT INTO product_history_tag (sequence, tag_key) VALUES (?1, ?2)",
                    params![sequence, tag_key],
                )
                .map_err(|error| locator_sqlite_error("insert product history tag", error))?;
        }
        if let Some(target_event_id) = &fact.signature_target_event_id {
            transaction
                .execute(
                    "INSERT INTO product_history_signature (sequence, target_event_id)
                     VALUES (?1, ?2)",
                    params![sequence, target_event_id],
                )
                .map_err(|error| locator_sqlite_error("insert product history signature", error))?;
        }
        if let Some(revision) = &fact.revision {
            transaction
                .execute(
                    "INSERT INTO product_revision
                         (sequence, revision_id, captured_at, captured_at_millis)
                     VALUES (?1, ?2, ?3, ?4)",
                    params![
                        sequence,
                        revision.revision_id,
                        revision.captured_at,
                        revision.captured_at_millis
                    ],
                )
                .map_err(|error| locator_sqlite_error("insert product revision", error))?;
            for superseded_revision_id in &revision.supersedes {
                transaction
                    .execute(
                        "INSERT INTO product_revision_edge
                         (sequence, superseded_revision_id) VALUES (?1, ?2)",
                        params![sequence, superseded_revision_id],
                    )
                    .map_err(|error| locator_sqlite_error("insert product revision edge", error))?;
            }
        }
    }
    Ok(())
}

struct EncodedIdentity {
    prefix_id: Option<i64>,
    digest: Option<Vec<u8>>,
    raw: Option<String>,
}

fn encode_identity(
    transaction: &Transaction<'_>,
    value: Option<&str>,
) -> Result<EncodedIdentity, SqliteLocatorError> {
    let Some(value) = value else {
        return Ok(EncodedIdentity {
            prefix_id: None,
            digest: None,
            raw: None,
        });
    };
    if let Some((prefix, digest)) = split_canonical_digest(value) {
        return Ok(EncodedIdentity {
            prefix_id: Some(semantic_dimension_id(
                transaction,
                "semantic_identity_prefix",
                prefix,
            )?),
            digest: Some(digest.to_vec()),
            raw: None,
        });
    }
    Ok(EncodedIdentity {
        prefix_id: None,
        digest: None,
        raw: Some(value.to_owned()),
    })
}

fn split_canonical_digest(value: &str) -> Option<(&str, [u8; 32])> {
    let split = value.len().checked_sub(64)?;
    let (prefix, hex) = value.split_at(split);
    if prefix.is_empty()
        || !hex
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return None;
    }
    let mut digest = [0_u8; 32];
    for (index, pair) in hex.as_bytes().chunks_exact(2).enumerate() {
        digest[index] = u8::from_str_radix(std::str::from_utf8(pair).ok()?, 16).ok()?;
    }
    Some((prefix, digest))
}

fn semantic_key_digest(value: &str) -> [u8; 32] {
    Sha256::digest(value.as_bytes()).into()
}

fn semantic_dimension_id(
    transaction: &Transaction<'_>,
    table: &'static str,
    value: &str,
) -> Result<i64, SqliteLocatorError> {
    let insert = format!("INSERT INTO {table}(value) VALUES (?1) ON CONFLICT(value) DO NOTHING");
    transaction
        .execute(&insert, [value])
        .map_err(|error| locator_sqlite_error("insert semantic dimension", error))?;
    let select = format!("SELECT id FROM {table} WHERE value = ?1");
    transaction
        .query_row(&select, [value], |row| row.get(0))
        .map_err(|error| locator_sqlite_error("read semantic dimension", error))
}

fn insert_family_fact(
    transaction: &Transaction<'_>,
    fact: &SemanticFact,
) -> Result<(), SqliteLocatorError> {
    let sequence = to_i64_locator(fact.cursor.sequence, "semantic family sequence")?;
    if let Some(change) = &fact.change {
        let fact_json = serde_json::to_string(change)
            .map_err(|error| SqliteLocatorError::Delta(error.to_string()))?;
        transaction
            .execute(
                "INSERT INTO semantic_change_fact (sequence, fact_json) VALUES (?1, ?2)",
                params![sequence, fact_json],
            )
            .map_err(|error| locator_sqlite_error("insert Change semantic fact", error))?;
    }
    match &fact.kind {
        SemanticFactKind::Revision(revision) => transaction.execute(
            "INSERT INTO semantic_revision_fact
                 (sequence, object_id, engagement_id, supersedes_json, base_commit_oid,
                  capture_commit_oid, capture_tree_oid)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            params![
                sequence,
                revision.object_id,
                revision.engagement_id,
                list_text(&revision.supersedes)?,
                revision.base_commit_oid,
                revision.capture_commit_oid,
                revision.capture_tree_oid,
            ],
        ),
        SemanticFactKind::Assessment(assessment) => transaction.execute(
            "INSERT INTO semantic_assessment_fact
             (sequence, assessment, replaces_json, related_observations_json,
              related_requests_json, revision_scoped)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            params![
                sequence,
                enum_text(assessment.assessment)?,
                list_text(&assessment.replaces)?,
                list_text(&assessment.related_observations)?,
                list_text(&assessment.related_requests)?,
                i64::from(assessment.revision_scoped),
            ],
        ),
        SemanticFactKind::InputRequestOpened(request) => transaction.execute(
            "INSERT INTO semantic_request_fact (sequence, reason_code, title)
             VALUES (?1, ?2, ?3)",
            params![sequence, enum_text(request.reason_code)?, request.title],
        ),
        SemanticFactKind::InputRequestResponded(response) => transaction.execute(
            "INSERT INTO semantic_response_fact (sequence, request_id) VALUES (?1, ?2)",
            params![sequence, response.request_id],
        ),
        SemanticFactKind::Validation(validation) => transaction.execute(
            "INSERT INTO semantic_validation_fact
             (sequence, check_name, status, exit_code, completed_at, log_hashes_json)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            params![
                sequence,
                validation.check_name,
                enum_text(validation.status)?,
                validation.exit_code,
                validation.completed_at,
                list_text(&validation.log_artifact_content_hashes)?,
            ],
        ),
        SemanticFactKind::CommitAssociated(association) => transaction.execute(
            "INSERT INTO semantic_commit_association_fact
             (sequence, commit_oid, tree_oid) VALUES (?1, ?2, ?3)",
            params![sequence, association.commit_oid, association.tree_oid],
        ),
        SemanticFactKind::CommitWithdrawn(withdrawal) => transaction.execute(
            "INSERT INTO semantic_commit_withdrawal_fact (sequence, association_id)
             VALUES (?1, ?2)",
            params![sequence, withdrawal.association_id],
        ),
        SemanticFactKind::RefAssociated(association) => transaction.execute(
            "INSERT INTO semantic_ref_association_fact
             (sequence, ref_name, head_oid) VALUES (?1, ?2, ?3)",
            params![sequence, association.ref_name, association.head_oid],
        ),
        SemanticFactKind::RefWithdrawn(withdrawal) => transaction.execute(
            "INSERT INTO semantic_ref_withdrawal_fact (sequence, association_id)
             VALUES (?1, ?2)",
            params![sequence, withdrawal.association_id],
        ),
        SemanticFactKind::Observation
        | SemanticFactKind::ArtifactRemoved
        | SemanticFactKind::Other => return Ok(()),
    }
    .map_err(|error| locator_sqlite_error("insert semantic family fact", error))?;
    Ok(())
}

fn update_materialized_projection(
    transaction: &Transaction<'_>,
    fact: &SemanticFact,
) -> Result<(), SqliteLocatorError> {
    if fact.event_type == "review_initialized" {
        let journal_id = transaction
            .query_row(
                "SELECT locator.journal_id
                 FROM semantic_event_fact_text AS event
                 JOIN locator_event_text AS locator ON locator.sequence = event.sequence
                 WHERE locator.event_type = 'review_initialized'
                   AND event.semantic_id IS NULL
                 ORDER BY locator.replay_key DESC, locator.event_id DESC
                 LIMIT 1",
                [],
                |row| row.get::<_, String>(0),
            )
            .map_err(|error| locator_sqlite_error("select materialized journal", error))?;
        transaction
            .execute(
                "UPDATE semantic_state_projection
                 SET event_count = event_count + 1, journal_id = ?1
                 WHERE singleton = 1",
                [journal_id],
            )
            .map_err(|error| locator_sqlite_error("advance materialized state", error))?;
    } else {
        transaction
            .execute(
                "UPDATE semantic_state_projection
                 SET event_count = event_count + 1,
                     journal_id = CASE
                         WHEN event_count = 0 THEN ?1
                         ELSE journal_id
                     END
                 WHERE singleton = 1",
                [&fact.journal_id],
            )
            .map_err(|error| locator_sqlite_error("advance materialized state", error))?;
    }

    let Some((family, semantic_key)) = materialized_identity(fact) else {
        return Ok(());
    };
    if duplicate_family(family) {
        update_materialized_duplicate(transaction, family, semantic_key, &fact.event_id)?;
    }

    let previous = transaction
        .query_row(
            "SELECT representative.sequence, locator.event_id, representative.semantic_key
             FROM semantic_representative_text AS representative
             JOIN locator_event_text AS locator ON locator.sequence = representative.sequence
             WHERE representative.family_id = ?1 AND representative.semantic_key_hash = ?2",
            params![
                family_code(family)?,
                semantic_key_digest(semantic_key).as_slice()
            ],
            |row| {
                Ok((
                    row.get::<_, i64>(0)?,
                    row.get::<_, String>(1)?,
                    row.get::<_, String>(2)?,
                ))
            },
        )
        .optional()
        .map_err(|error| locator_sqlite_error("read semantic representative", error))?;
    if let Some((_, _, observed_key)) = &previous
        && observed_key != semantic_key
    {
        return Err(SqliteLocatorError::Delta(
            "semantic representative digest resolves to a different key".to_owned(),
        ));
    }
    let replace = previous
        .as_ref()
        .is_none_or(|(_, event_id, _)| fact.event_id < *event_id);
    if !replace {
        return Ok(());
    }

    let mut affected_requests = BTreeSet::new();
    if family == "request" {
        affected_requests.insert(semantic_key.to_owned());
    } else if family == "response" {
        if let Some((sequence, _, _)) = &previous
            && let Some(request_id) = response_request_id(transaction, *sequence)?
        {
            affected_requests.insert(request_id);
        }
        let SemanticFactKind::InputRequestResponded(response) = &fact.kind else {
            return Err(SqliteLocatorError::Delta(
                "response representative has the wrong semantic kind".to_owned(),
            ));
        };
        affected_requests.insert(response.request_id.clone());
    }
    let before_request_states = affected_requests
        .iter()
        .map(|request_id| {
            Ok((
                request_id.clone(),
                request_projection_state(transaction, request_id)?,
            ))
        })
        .collect::<Result<Vec<_>, SqliteLocatorError>>()?;

    let inserted = previous.is_none();
    let encoded_key = encode_identity(transaction, Some(semantic_key))?;
    transaction
        .execute(
            "INSERT INTO semantic_representative
             (family_id, semantic_key_prefix_id, semantic_key_digest, semantic_key_raw,
              semantic_key_hash, sequence)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)
             ON CONFLICT(family_id, semantic_key_hash) DO UPDATE SET
                 semantic_key_prefix_id = excluded.semantic_key_prefix_id,
                 semantic_key_digest = excluded.semantic_key_digest,
                 semantic_key_raw = excluded.semantic_key_raw,
                 sequence = excluded.sequence",
            params![
                family_code(family)?,
                encoded_key.prefix_id,
                encoded_key.digest.as_deref(),
                encoded_key.raw,
                semantic_key_digest(semantic_key).as_slice(),
                to_i64_locator(fact.cursor.sequence, "representative sequence")?,
            ],
        )
        .map_err(|error| locator_sqlite_error("upsert semantic representative", error))?;

    if inserted {
        increment_materialized_family_count(transaction, family, fact)?;
    } else if family == "revision" {
        let SemanticFactKind::Revision(revision) = &fact.kind else {
            return Err(SqliteLocatorError::Delta(
                "revision representative has the wrong semantic kind".to_owned(),
            ));
        };
        transaction
            .execute(
                "UPDATE semantic_state_projection
                 SET current_object_id = CASE
                     WHEN revision_count = 1 AND current_revision_id = ?1 THEN ?2
                     ELSE current_object_id
                 END
                 WHERE singleton = 1",
                params![semantic_key, revision.object_id],
            )
            .map_err(|error| {
                locator_sqlite_error("replace current revision materialization", error)
            })?;
    }

    for (request_id, before) in before_request_states {
        let after = request_projection_state(transaction, &request_id)?;
        adjust_request_state_counts(transaction, before, after)?;
    }
    Ok(())
}

fn materialized_identity(fact: &SemanticFact) -> Option<(&'static str, &str)> {
    match &fact.kind {
        SemanticFactKind::Revision(_) => fact.revision_id.as_deref().map(|key| ("revision", key)),
        SemanticFactKind::Observation => {
            fact.semantic_id.as_deref().map(|key| ("observation", key))
        }
        SemanticFactKind::Assessment(_) => {
            fact.semantic_id.as_deref().map(|key| ("assessment", key))
        }
        SemanticFactKind::InputRequestOpened(_) => {
            fact.semantic_id.as_deref().map(|key| ("request", key))
        }
        SemanticFactKind::InputRequestResponded(_) => {
            fact.semantic_id.as_deref().map(|key| ("response", key))
        }
        SemanticFactKind::Validation(_) => {
            fact.semantic_id.as_deref().map(|key| ("validation", key))
        }
        SemanticFactKind::CommitAssociated(_) => fact
            .semantic_id
            .as_deref()
            .map(|key| ("commit_association", key)),
        SemanticFactKind::CommitWithdrawn(_) => fact
            .semantic_id
            .as_deref()
            .map(|key| ("commit_withdrawal", key)),
        SemanticFactKind::RefAssociated(_) => fact
            .semantic_id
            .as_deref()
            .map(|key| ("ref_association", key)),
        SemanticFactKind::RefWithdrawn(_) => fact
            .semantic_id
            .as_deref()
            .map(|key| ("ref_withdrawal", key)),
        SemanticFactKind::ArtifactRemoved => {
            fact.content_hash.as_deref().map(|key| ("removal", key))
        }
        SemanticFactKind::Other => fact
            .change
            .as_ref()
            .map(|_| ("change_record", fact.event_id.as_str())),
    }
}

fn duplicate_family(family: &str) -> bool {
    matches!(
        family,
        "observation" | "assessment" | "request" | "response" | "validation"
    )
}

fn family_code(family: &str) -> Result<i64, SqliteLocatorError> {
    match family {
        "revision" => Ok(1),
        "observation" => Ok(2),
        "assessment" => Ok(3),
        "request" => Ok(4),
        "response" => Ok(5),
        "validation" => Ok(6),
        "commit_association" => Ok(7),
        "commit_withdrawal" => Ok(8),
        "ref_association" => Ok(9),
        "ref_withdrawal" => Ok(10),
        "removal" => Ok(11),
        "change_record" => Ok(12),
        _ => Err(SqliteLocatorError::Delta(format!(
            "unsupported semantic representative family {family}"
        ))),
    }
}

fn update_materialized_duplicate(
    transaction: &Transaction<'_>,
    family: &str,
    semantic_key: &str,
    event_id: &str,
) -> Result<(), SqliteLocatorError> {
    let current = transaction
        .query_row(
            "SELECT event_count, event_ids_json
             FROM semantic_duplicate_projection
             WHERE family = ?1 AND semantic_key = ?2",
            params![family, semantic_key],
            |row| Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?)),
        )
        .optional()
        .map_err(|error| locator_sqlite_error("read semantic duplicate row", error))?;
    let (event_count, mut event_ids) = match current {
        Some((count, ids)) => (
            count + 1,
            decode_string_list(&ids)
                .map_err(|error| SqliteLocatorError::Delta(error.to_string()))?,
        ),
        None => {
            let representative = transaction
                .query_row(
                    "SELECT locator.event_id, representative.semantic_key
                     FROM semantic_representative_text AS representative
                     JOIN locator_event_text AS locator
                       ON locator.sequence = representative.sequence
                     WHERE representative.family_id = ?1
                       AND representative.semantic_key_hash = ?2",
                    params![
                        family_code(family)?,
                        semantic_key_digest(semantic_key).as_slice()
                    ],
                    |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
                )
                .optional()
                .map_err(|error| {
                    locator_sqlite_error("read first semantic duplicate representative", error)
                })?;
            let Some((representative, observed_key)) = representative else {
                return Ok(());
            };
            if observed_key != semantic_key {
                return Err(SqliteLocatorError::Delta(
                    "semantic duplicate digest resolves to a different key".to_owned(),
                ));
            }
            (2, vec![representative])
        }
    };
    event_ids.push(event_id.to_owned());
    event_ids.sort();
    event_ids.dedup();
    event_ids.truncate(5);
    transaction
        .execute(
            "INSERT INTO semantic_duplicate_projection
             (family, semantic_key, event_count, event_ids_json)
             VALUES (?1, ?2, ?3, ?4)
             ON CONFLICT(family, semantic_key) DO UPDATE SET
                 event_count = excluded.event_count,
                 event_ids_json = excluded.event_ids_json",
            params![family, semantic_key, event_count, list_text(&event_ids)?],
        )
        .map_err(|error| locator_sqlite_error("upsert semantic duplicate row", error))?;
    Ok(())
}

fn increment_materialized_family_count(
    transaction: &Transaction<'_>,
    family: &str,
    fact: &SemanticFact,
) -> Result<(), SqliteLocatorError> {
    match family {
        "revision" => {
            let SemanticFactKind::Revision(revision) = &fact.kind else {
                return Err(SqliteLocatorError::Delta(
                    "revision representative has the wrong semantic kind".to_owned(),
                ));
            };
            transaction
                .execute(
                    "UPDATE semantic_state_projection
                     SET revision_count = revision_count + 1,
                         current_revision_id = CASE
                             WHEN revision_count = 0 THEN ?1
                             ELSE NULL
                         END,
                         current_object_id = CASE
                             WHEN revision_count = 0 THEN ?2
                             ELSE NULL
                         END
                     WHERE singleton = 1",
                    params![fact.revision_id, revision.object_id],
                )
                .map_err(|error| {
                    locator_sqlite_error("increment revision projection count", error)
                })?;
        }
        "observation" => increment_state_column(transaction, "observation_count")?,
        "assessment" => increment_state_column(transaction, "assessment_count")?,
        "validation" => increment_state_column(transaction, "validation_check_count")?,
        "request" => increment_state_column(transaction, "input_request_count")?,
        _ => {}
    }
    Ok(())
}

fn increment_state_column(
    transaction: &Transaction<'_>,
    column: &'static str,
) -> Result<(), SqliteLocatorError> {
    let sql = match column {
        "observation_count" => {
            "UPDATE semantic_state_projection
             SET observation_count = observation_count + 1 WHERE singleton = 1"
        }
        "assessment_count" => {
            "UPDATE semantic_state_projection
             SET assessment_count = assessment_count + 1 WHERE singleton = 1"
        }
        "validation_check_count" => {
            "UPDATE semantic_state_projection
             SET validation_check_count = validation_check_count + 1 WHERE singleton = 1"
        }
        "input_request_count" => {
            "UPDATE semantic_state_projection
             SET input_request_count = input_request_count + 1 WHERE singleton = 1"
        }
        _ => {
            return Err(SqliteLocatorError::Delta(
                "unsupported materialized state counter".to_owned(),
            ));
        }
    };
    transaction
        .execute(sql, [])
        .map_err(|error| locator_sqlite_error("increment materialized state counter", error))?;
    Ok(())
}

fn response_request_id(
    transaction: &Transaction<'_>,
    sequence: i64,
) -> Result<Option<String>, SqliteLocatorError> {
    transaction
        .query_row(
            "SELECT request_id FROM semantic_response_fact WHERE sequence = ?1",
            [sequence],
            |row| row.get(0),
        )
        .optional()
        .map_err(|error| locator_sqlite_error("read response representative target", error))
}

fn request_projection_state(
    transaction: &Transaction<'_>,
    request_id: &str,
) -> Result<(bool, bool), SqliteLocatorError> {
    let mode = transaction
        .query_row(
            "SELECT event.assertion_mode, representative.semantic_key
             FROM semantic_representative_text AS representative
             JOIN semantic_event_fact_text AS event
               ON event.sequence = representative.sequence
             WHERE representative.family_id = 4
               AND representative.semantic_key_hash = ?1",
            [semantic_key_digest(request_id).as_slice()],
            |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
        )
        .optional()
        .map_err(|error| locator_sqlite_error("read request projection state", error))?;
    let Some((mode, observed_key)) = mode else {
        return Ok((false, false));
    };
    if observed_key != request_id {
        return Err(SqliteLocatorError::Delta(
            "request representative digest resolves to a different key".to_owned(),
        ));
    }
    let responded = transaction
        .query_row(
            "SELECT 1
             FROM semantic_response_fact AS response
             JOIN semantic_representative AS representative
               ON representative.family_id = 5
              AND representative.sequence = response.sequence
             WHERE response.request_id = ?1
             LIMIT 1",
            [request_id],
            |_| Ok(()),
        )
        .optional()
        .map_err(|error| locator_sqlite_error("read response projection state", error))?
        .is_some();
    let open = !responded;
    Ok((open, open && mode == "operative"))
}

fn adjust_request_state_counts(
    transaction: &Transaction<'_>,
    before: (bool, bool),
    after: (bool, bool),
) -> Result<(), SqliteLocatorError> {
    let open_delta = i64::from(after.0) - i64::from(before.0);
    let operative_delta = i64::from(after.1) - i64::from(before.1);
    transaction
        .execute(
            "UPDATE semantic_state_projection
             SET open_input_request_count = open_input_request_count + ?1,
                 open_operative_input_request_count =
                     open_operative_input_request_count + ?2
             WHERE singleton = 1",
            params![open_delta, operative_delta],
        )
        .map_err(|error| locator_sqlite_error("adjust open request counts", error))?;
    Ok(())
}

fn query_materialized_state(
    connection: &rusqlite::Connection,
) -> Result<SemanticStateSnapshot, SqliteSemanticError> {
    let row = connection
        .query_row(
            "SELECT journal_id, current_revision_id, current_object_id,
                    revision_count, event_count, observation_count, assessment_count,
                    validation_check_count, input_request_count, open_input_request_count,
                    open_operative_input_request_count
             FROM semantic_state_projection WHERE singleton = 1",
            [],
            |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, Option<String>>(1)?,
                    row.get::<_, Option<String>>(2)?,
                    row.get::<_, i64>(3)?,
                    row.get::<_, i64>(4)?,
                    row.get::<_, i64>(5)?,
                    row.get::<_, i64>(6)?,
                    row.get::<_, i64>(7)?,
                    row.get::<_, i64>(8)?,
                    row.get::<_, i64>(9)?,
                    row.get::<_, i64>(10)?,
                ))
            },
        )
        .map_err(|error| sqlite_error("read materialized semantic state", error))?;
    let state = MaterializedSemanticState {
        journal_id: row.0,
        current_revision_id: row.1,
        current_object_id: row.2,
        revision_count: to_usize(row.3, "revision count")?,
        event_count: to_usize(row.4, "event count")?,
        observation_count: to_usize(row.5, "observation count")?,
        assessment_count: to_usize(row.6, "assessment count")?,
        validation_check_count: to_usize(row.7, "validation count")?,
        input_request_count: to_usize(row.8, "input request count")?,
        open_input_request_count: to_usize(row.9, "open input request count")?,
        open_operative_input_request_count: to_usize(row.10, "open operative input request count")?,
    };
    let mut statement = connection
        .prepare(
            "SELECT family, semantic_key, event_count, event_ids_json
             FROM semantic_duplicate_projection
             WHERE event_count >= 2
             ORDER BY family, semantic_key",
        )
        .map_err(|error| sqlite_error("prepare materialized semantic duplicates", error))?;
    let rows = statement
        .query_map([], |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, i64>(2)?,
                row.get::<_, String>(3)?,
            ))
        })
        .map_err(|error| sqlite_error("query materialized semantic duplicates", error))?;
    let mut duplicates = Vec::new();
    for row in rows {
        let (family, semantic_id, event_count, event_ids) =
            row.map_err(|error| sqlite_error("read materialized semantic duplicate", error))?;
        duplicates.push(MaterializedSemanticDuplicate {
            family,
            semantic_id,
            event_ids: decode_string_list(&event_ids)?,
            event_count: to_usize(event_count, "semantic duplicate count")?,
        });
    }
    Ok(SemanticStateSnapshot::from_materialized(state, &duplicates))
}

#[derive(Clone, Copy)]
enum MaterializedFactFamilies {
    AllExceptObservations,
    Attention,
}

impl MaterializedFactFamilies {
    const fn predicate(self) -> &'static str {
        match self {
            Self::AllExceptObservations => "representative.family_id != 2",
            Self::Attention => "representative.family_id IN (1, 3, 4, 5, 6)",
        }
    }
}

fn query_materialized_facts(
    connection: &rusqlite::Connection,
    journal: &QualificationLocalJournal,
    epoch: u64,
    sequence: u64,
    engagement_id: Option<&str>,
    families: MaterializedFactFamilies,
) -> Result<Vec<HydratedSemanticFact>, SqliteSemanticError> {
    query_materialized_compact_facts(connection, epoch, sequence, engagement_id, families)?
        .into_iter()
        .map(|fact| hydrate_semantic_fact(journal, fact))
        .collect()
}

fn query_materialized_compact_facts(
    connection: &rusqlite::Connection,
    epoch: u64,
    sequence: u64,
    engagement_id: Option<&str>,
    families: MaterializedFactFamilies,
) -> Result<Vec<SemanticFact>, SqliteSemanticError> {
    let sql = format!(
        "SELECT locator.epoch, event.sequence, receipt.logical_reread_key_hash,
                locator.replay_key, locator.event_id, locator.event_type,
                locator.journal_id, event.revision_id, event.semantic_id,
                event.content_hash, locator.payload_hash,
                event.occurred_at, event.assertion_mode,
                locator.track_id, event.actor_id, receipt.validation_witness,
                revision.object_id, revision.engagement_id, revision.supersedes_json,
                revision.base_commit_oid, revision.capture_commit_oid,
                revision.capture_tree_oid,
                assessment.assessment, assessment.replaces_json,
                assessment.related_observations_json,
                assessment.related_requests_json, assessment.revision_scoped,
                request.reason_code, request.title,
                response.request_id,
                validation.check_name, validation.status, validation.exit_code,
                validation.completed_at, validation.log_hashes_json,
                commit_association.commit_oid, commit_association.tree_oid,
                commit_withdrawal.association_id,
                ref_association.ref_name, ref_association.head_oid,
                ref_withdrawal.association_id,
                change_fact.fact_json,
                receipt.epoch
         FROM semantic_representative AS representative
         JOIN semantic_event_fact_text AS event ON event.sequence = representative.sequence
         JOIN locator_event_text AS locator ON locator.sequence = event.sequence
         JOIN cursor_receipt_text AS receipt ON receipt.sequence = event.sequence
         LEFT JOIN semantic_revision_fact AS revision
           ON revision.sequence = event.sequence
         LEFT JOIN semantic_assessment_fact AS assessment
           ON assessment.sequence = event.sequence
         LEFT JOIN semantic_request_fact AS request
           ON request.sequence = event.sequence
         LEFT JOIN semantic_response_fact AS response
           ON response.sequence = event.sequence
         LEFT JOIN semantic_validation_fact AS validation
           ON validation.sequence = event.sequence
         LEFT JOIN semantic_commit_association_fact AS commit_association
           ON commit_association.sequence = event.sequence
         LEFT JOIN semantic_commit_withdrawal_fact AS commit_withdrawal
           ON commit_withdrawal.sequence = event.sequence
         LEFT JOIN semantic_ref_association_fact AS ref_association
           ON ref_association.sequence = event.sequence
         LEFT JOIN semantic_ref_withdrawal_fact AS ref_withdrawal
           ON ref_withdrawal.sequence = event.sequence
         LEFT JOIN semantic_change_fact AS change_fact
           ON change_fact.sequence = event.sequence
         WHERE {}
           AND locator.epoch = ?1 AND event.sequence <= ?2
           AND (
               ?3 IS NULL
               OR event.revision_id IN (
                   SELECT selected_event.revision_id
                   FROM semantic_revision_fact AS selected_revision
                   JOIN semantic_event_fact_text AS selected_event
                     ON selected_event.sequence = selected_revision.sequence
                   JOIN locator_event AS selected_locator
                     ON selected_locator.sequence = selected_event.sequence
                   JOIN semantic_representative AS selected_representative
                     ON selected_representative.family_id = 1
                    AND selected_representative.sequence = selected_event.sequence
                   WHERE selected_revision.engagement_id = ?3
                     AND selected_locator.epoch = ?1
                     AND selected_event.sequence <= ?2
               )
               OR representative.family_id = 12
               OR (
                   representative.family_id = 11
                   AND event.content_hash IN (
                       SELECT selected_event.content_hash
                       FROM semantic_revision_fact AS selected_revision
                       JOIN semantic_event_fact_text AS selected_event
                         ON selected_event.sequence = selected_revision.sequence
                       JOIN locator_event AS selected_locator
                         ON selected_locator.sequence = selected_event.sequence
                       JOIN semantic_representative AS selected_representative
                         ON selected_representative.family_id = 1
                        AND selected_representative.sequence = selected_event.sequence
                       WHERE selected_revision.engagement_id = ?3
                         AND selected_locator.epoch = ?1
                         AND selected_event.sequence <= ?2
                   )
               )
           )
         ORDER BY locator.replay_key, receipt.logical_reread_key_hash",
        families.predicate()
    );
    let mut statement = connection
        .prepare(&sql)
        .map_err(|error| sqlite_error("prepare materialized semantic facts", error))?;
    let mut rows = statement
        .query(params![
            to_i64(epoch, "materialized semantic epoch")?,
            to_i64(sequence, "materialized semantic cursor")?,
            engagement_id,
        ])
        .map_err(|error| sqlite_error("query materialized semantic facts", error))?;
    let mut facts = Vec::new();
    while let Some(row) = rows
        .next()
        .map_err(|error| sqlite_error("advance materialized semantic facts", error))?
    {
        let mut fact = semantic_fact_from_sql(row)
            .map_err(|error| sqlite_error("read materialized semantic fact", error))?;
        fact.kind = materialized_kind_from_sql(&fact, row)?;
        fact.change = row
            .get::<_, Option<String>>(41)
            .map_err(|error| sqlite_error("read materialized Change fact", error))?
            .map(|value| serde_json::from_str::<ChangeProjectionFact>(&value))
            .transpose()
            .map_err(|error| SqliteSemanticError::Model(SemanticModelError::Json(error)))?;
        let receipt_epoch = row
            .get::<_, i64>(42)
            .map_err(|error| sqlite_error("read materialized receipt epoch", error))?;
        if receipt_epoch != to_i64(fact.cursor.epoch, "materialized receipt epoch")? {
            return Err(SqliteSemanticError::Metadata(format!(
                "materialized fact does not match cursor receipt at {:?}",
                fact.cursor
            )));
        }
        facts.push(fact);
    }
    Ok(facts)
}

fn materialized_kind_from_sql(
    fact: &SemanticFact,
    row: &rusqlite::Row<'_>,
) -> Result<SemanticFactKind, SqliteSemanticError> {
    match fact.event_type.as_str() {
        "work_object_proposed" => Ok(SemanticFactKind::Revision(RevisionFact {
            object_id: materialized_text(row, 16, "revision object id")?,
            engagement_id: materialized_text(row, 17, "revision engagement id")?,
            supersedes: decode_string_list(&materialized_text(row, 18, "revision supersedes")?)?,
            base_commit_oid: materialized_optional_text(row, 19, "revision base commit")?,
            capture_commit_oid: materialized_optional_text(row, 20, "revision capture commit")?,
            capture_tree_oid: materialized_optional_text(row, 21, "revision capture tree")?,
        })),
        "review_assessment_recorded" => Ok(SemanticFactKind::Assessment(AssessmentFact {
            assessment: decode_enum(&materialized_text(row, 22, "assessment")?)?,
            replaces: decode_string_list(&materialized_text(row, 23, "assessment replacements")?)?,
            related_observations: decode_string_list(&materialized_text(
                row,
                24,
                "assessment observations",
            )?)?,
            related_requests: decode_string_list(&materialized_text(
                row,
                25,
                "assessment requests",
            )?)?,
            revision_scoped: row
                .get::<_, i64>(26)
                .map_err(|error| sqlite_error("read assessment scope", error))?
                != 0,
        })),
        "input_request_opened" => Ok(SemanticFactKind::InputRequestOpened(InputRequestFact {
            reason_code: decode_enum(&materialized_text(row, 27, "request reason code")?)?,
            title: materialized_text(row, 28, "request title")?,
        })),
        "input_request_responded" => {
            Ok(SemanticFactKind::InputRequestResponded(InputResponseFact {
                request_id: materialized_text(row, 29, "response request id")?,
            }))
        }
        "validation_check_recorded" => Ok(SemanticFactKind::Validation(ValidationFact {
            check_name: materialized_text(row, 30, "validation name")?,
            status: decode_enum(&materialized_text(row, 31, "validation status")?)?,
            exit_code: row
                .get(32)
                .map_err(|error| sqlite_error("read validation exit code", error))?,
            completed_at: materialized_optional_text(row, 33, "validation completed at")?,
            log_artifact_content_hashes: decode_string_list(&materialized_text(
                row,
                34,
                "validation log hashes",
            )?)?,
        })),
        "revision_commit_associated" => {
            Ok(SemanticFactKind::CommitAssociated(CommitAssociationFact {
                commit_oid: materialized_text(row, 35, "commit association oid")?,
                tree_oid: materialized_text(row, 36, "commit association tree")?,
            }))
        }
        "revision_commit_withdrawn" => {
            Ok(SemanticFactKind::CommitWithdrawn(CommitWithdrawalFact {
                association_id: materialized_text(row, 37, "commit withdrawal target")?,
            }))
        }
        "revision_ref_associated" => Ok(SemanticFactKind::RefAssociated(RefAssociationFact {
            ref_name: materialized_text(row, 38, "ref association name")?,
            head_oid: materialized_text(row, 39, "ref association head")?,
        })),
        "revision_ref_withdrawn" => Ok(SemanticFactKind::RefWithdrawn(RefWithdrawalFact {
            association_id: materialized_text(row, 40, "ref withdrawal target")?,
        })),
        "artifact_removed" => Ok(SemanticFactKind::ArtifactRemoved),
        _ => Ok(SemanticFactKind::Other),
    }
}

fn materialized_text(
    row: &rusqlite::Row<'_>,
    column: usize,
    label: &'static str,
) -> Result<String, SqliteSemanticError> {
    row.get::<_, Option<String>>(column)
        .map_err(|error| sqlite_error("read materialized family text", error))?
        .ok_or_else(|| SqliteSemanticError::Metadata(format!("missing {label}")))
}

fn materialized_optional_text(
    row: &rusqlite::Row<'_>,
    column: usize,
    label: &'static str,
) -> Result<Option<String>, SqliteSemanticError> {
    row.get(column)
        .map_err(|error| SqliteSemanticError::Metadata(format!("invalid {label}: {error}")))
}

fn selected_semantic_facts(
    identity_predicate: &str,
    index: &str,
    epoch_parameter: usize,
    sequence_parameter: usize,
) -> String {
    format!(
        "SELECT locator.epoch, event.sequence, receipt.logical_reread_key_hash,
                locator.replay_key, locator.event_id, locator.event_type,
                locator.journal_id, event.revision_id, event.semantic_id,
                event.content_hash, locator.payload_hash,
                event.occurred_at, event.assertion_mode,
                locator.track_id, event.actor_id, receipt.validation_witness,
                receipt.epoch
         FROM semantic_event_fact AS physical INDEXED BY {index}
         JOIN semantic_event_fact_text AS event ON event.sequence = physical.sequence
         JOIN locator_event_text AS locator ON locator.sequence = event.sequence
         JOIN cursor_receipt_text AS receipt ON receipt.sequence = event.sequence
         WHERE {identity_predicate}
           AND locator.epoch = ?{epoch_parameter}
           AND event.sequence <= ?{sequence_parameter}
         ORDER BY locator.replay_key, receipt.logical_reread_key_hash"
    )
}

fn selected_content_query(
    identity_predicate: &str,
    epoch_parameter: usize,
    sequence_parameter: usize,
) -> String {
    format!(
        "SELECT 1
         FROM semantic_event_fact AS event INDEXED BY semantic_event_fact_content
         JOIN locator_event_text AS locator ON locator.sequence = event.sequence
         WHERE {identity_predicate}
           AND locator.event_type = 'artifact_removed'
           AND locator.epoch = ?{epoch_parameter}
           AND event.sequence <= ?{sequence_parameter}
         LIMIT 1"
    )
}

fn query_facts(
    connection: &rusqlite::Connection,
    journal: &QualificationLocalJournal,
    sql: &str,
    parameters: impl rusqlite::Params,
) -> Result<Vec<HydratedSemanticFact>, SqliteSemanticError> {
    let mut statement = connection
        .prepare(sql)
        .map_err(|error| sqlite_error("prepare semantic facts", error))?;
    let rows = statement
        .query_map(parameters, semantic_fact_from_joined_sql)
        .map_err(|error| sqlite_error("query semantic facts", error))?;
    let mut facts = Vec::new();
    for fact in rows {
        let mut fact = fact.map_err(|error| sqlite_error("read semantic fact", error))?;
        fact.kind = query_family_fact(connection, &fact)?;
        fact.change = query_change_fact(connection, fact.cursor.sequence)?;
        facts.push(hydrate_semantic_fact(journal, fact)?);
    }
    Ok(facts)
}

fn hydrate_semantic_fact(
    journal: &QualificationLocalJournal,
    mut stored: SemanticFact,
) -> Result<HydratedSemanticFact, SqliteSemanticError> {
    let bytes = journal
        .read_event_bytes_by_key_digest(&stored.logical_reread_key)
        .map_err(|error| SqliteSemanticError::Metadata(error.to_string()))?
        .ok_or_else(|| {
            SqliteSemanticError::Metadata(format!(
                "semantic carrier is absent for key digest {}",
                stored.logical_reread_key
            ))
        })?;
    if sha256_bytes_hex(&bytes) != stored.validation_witness {
        return Err(SqliteSemanticError::CarrierMismatch(stored.cursor));
    }
    let event = EventStore::decode_qualification_entry(stored.logical_reread_key.clone(), bytes)
        .map_err(|error| SqliteSemanticError::Metadata(error.to_string()))?;
    stored.logical_reread_key = event.idempotency_key.clone();
    let observed =
        SemanticFact::from_event(stored.cursor, &event, stored.validation_witness.clone())?;
    if observed != stored {
        return Err(SqliteSemanticError::CarrierMismatch(stored.cursor));
    }
    Ok(HydratedSemanticFact {
        fact: stored,
        event,
    })
}

fn hydrated_facts_only(facts: Vec<HydratedSemanticFact>) -> Vec<SemanticFact> {
    facts.into_iter().map(|fact| fact.fact).collect()
}

fn semantic_fact_from_sql(row: &rusqlite::Row<'_>) -> rusqlite::Result<SemanticFact> {
    let epoch = row.get::<_, i64>(0)?;
    let sequence = row.get::<_, i64>(1)?;
    let epoch = u64::try_from(epoch).map_err(|error| {
        rusqlite::Error::FromSqlConversionFailure(
            0,
            rusqlite::types::Type::Integer,
            Box::new(error),
        )
    })?;
    let sequence = u64::try_from(sequence).map_err(|error| {
        rusqlite::Error::FromSqlConversionFailure(
            1,
            rusqlite::types::Type::Integer,
            Box::new(error),
        )
    })?;
    let assertion_mode = decode_enum::<crate::session::event::AssertionMode>(
        &row.get::<_, String>(12)?,
    )
    .map_err(|error| {
        rusqlite::Error::FromSqlConversionFailure(12, rusqlite::types::Type::Text, Box::new(error))
    })?;
    Ok(SemanticFact {
        cursor: TruthCursor::new(epoch, sequence),
        logical_reread_key: row.get(2)?,
        replay_key: row.get(3)?,
        event_id: row.get(4)?,
        event_type: row.get(5)?,
        journal_id: row.get(6)?,
        revision_id: row.get(7)?,
        semantic_id: row.get(8)?,
        content_hash: row.get(9)?,
        payload_hash: row.get(10)?,
        occurred_at: row.get(11)?,
        assertion_mode,
        track_id: row.get(13)?,
        actor_id: row.get(14)?,
        validation_witness: row.get(15)?,
        kind: SemanticFactKind::Other,
        change: None,
    })
}

fn query_change_fact(
    connection: &rusqlite::Connection,
    sequence: u64,
) -> Result<Option<ChangeProjectionFact>, SqliteSemanticError> {
    let value = connection
        .query_row(
            "SELECT fact_json FROM semantic_change_fact WHERE sequence = ?1",
            [to_i64(sequence, "Change semantic fact sequence")?],
            |row| row.get::<_, String>(0),
        )
        .optional()
        .map_err(|error| sqlite_error("query Change semantic fact", error))?;
    value
        .map(|value| serde_json::from_str(&value))
        .transpose()
        .map_err(|error| SqliteSemanticError::Model(SemanticModelError::Json(error)))
}

fn query_materialized_change_projection(
    connection: &rusqlite::Connection,
    epoch: u64,
    sequence: u64,
) -> Result<crate::session::ChangeProjection, SqliteSemanticError> {
    let mut statement = connection
        .prepare(
            "SELECT change_fact.fact_json
             FROM semantic_change_fact AS change_fact
             JOIN locator_event_text AS locator ON locator.sequence = change_fact.sequence
             WHERE locator.epoch = ?1 AND change_fact.sequence <= ?2
             ORDER BY locator.replay_key, change_fact.sequence",
        )
        .map_err(|error| sqlite_error("prepare materialized Change projection", error))?;
    let rows = statement
        .query_map(
            params![
                to_i64(epoch, "materialized Change epoch")?,
                to_i64(sequence, "materialized Change sequence")?,
            ],
            |row| row.get::<_, String>(0),
        )
        .map_err(|error| sqlite_error("query materialized Change projection", error))?;
    let mut facts = Vec::new();
    for row in rows {
        let json = row.map_err(|error| sqlite_error("read materialized Change fact", error))?;
        facts.push(
            serde_json::from_str::<ChangeProjectionFact>(&json)
                .map_err(|error| SqliteSemanticError::Model(SemanticModelError::Json(error)))?,
        );
    }
    project_changes_from_facts(&facts)
        .map_err(|error| SqliteSemanticError::Model(SemanticModelError::Product(error)))
}

fn semantic_fact_from_joined_sql(row: &rusqlite::Row<'_>) -> rusqlite::Result<SemanticFact> {
    let fact = semantic_fact_from_sql(row)?;
    let receipt_epoch = row.get::<_, i64>(16)?;
    let receipt_epoch = u64::try_from(receipt_epoch).map_err(|error| {
        rusqlite::Error::FromSqlConversionFailure(
            16,
            rusqlite::types::Type::Integer,
            Box::new(error),
        )
    })?;
    if receipt_epoch != fact.cursor.epoch {
        return Err(rusqlite::Error::InvalidQuery);
    }
    Ok(fact)
}

fn query_family_fact(
    connection: &rusqlite::Connection,
    fact: &SemanticFact,
) -> Result<SemanticFactKind, SqliteSemanticError> {
    let sequence = to_i64(fact.cursor.sequence, "semantic family query sequence")?;
    match fact.event_type.as_str() {
        "work_object_proposed" => connection
            .query_row(
                "SELECT object_id, engagement_id, supersedes_json, base_commit_oid,
                        capture_commit_oid, capture_tree_oid
                 FROM semantic_revision_fact WHERE sequence = ?1",
                [sequence],
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?,
                        row.get::<_, Option<String>>(3)?,
                        row.get::<_, Option<String>>(4)?,
                        row.get::<_, Option<String>>(5)?,
                    ))
                },
            )
            .optional()
            .map_err(|error| sqlite_error("query revision fact", error))?
            .map(|row| {
                Ok(SemanticFactKind::Revision(RevisionFact {
                    object_id: row.0,
                    engagement_id: row.1,
                    supersedes: decode_string_list(&row.2)?,
                    base_commit_oid: row.3,
                    capture_commit_oid: row.4,
                    capture_tree_oid: row.5,
                }))
            })
            .transpose()
            .map(|kind| kind.unwrap_or(SemanticFactKind::Other)),
        "review_observation_recorded" => Ok(SemanticFactKind::Observation),
        "review_assessment_recorded" => {
            let row = connection
                .query_row(
                    "SELECT assessment, replaces_json, related_observations_json,
                            related_requests_json, revision_scoped
                     FROM semantic_assessment_fact WHERE sequence = ?1",
                    [sequence],
                    |row| {
                        Ok((
                            row.get::<_, String>(0)?,
                            row.get::<_, String>(1)?,
                            row.get::<_, String>(2)?,
                            row.get::<_, String>(3)?,
                            row.get::<_, i64>(4)?,
                        ))
                    },
                )
                .map_err(|error| sqlite_error("query assessment fact", error))?;
            Ok(SemanticFactKind::Assessment(AssessmentFact {
                assessment: decode_enum(&row.0)?,
                replaces: decode_string_list(&row.1)?,
                related_observations: decode_string_list(&row.2)?,
                related_requests: decode_string_list(&row.3)?,
                revision_scoped: row.4 != 0,
            }))
        }
        "input_request_opened" => {
            let row = connection
                .query_row(
                    "SELECT reason_code, title FROM semantic_request_fact WHERE sequence = ?1",
                    [sequence],
                    |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
                )
                .map_err(|error| sqlite_error("query request fact", error))?;
            Ok(SemanticFactKind::InputRequestOpened(InputRequestFact {
                reason_code: decode_enum(&row.0)?,
                title: row.1,
            }))
        }
        "input_request_responded" => {
            Ok(SemanticFactKind::InputRequestResponded(InputResponseFact {
                request_id: connection
                    .query_row(
                        "SELECT request_id FROM semantic_response_fact WHERE sequence = ?1",
                        [sequence],
                        |row| row.get(0),
                    )
                    .map_err(|error| sqlite_error("query response fact", error))?,
            }))
        }
        "validation_check_recorded" => {
            let row = connection
                .query_row(
                    "SELECT check_name, status, exit_code, completed_at, log_hashes_json
                     FROM semantic_validation_fact WHERE sequence = ?1",
                    [sequence],
                    |row| {
                        Ok((
                            row.get::<_, String>(0)?,
                            row.get::<_, String>(1)?,
                            row.get::<_, Option<i64>>(2)?,
                            row.get::<_, Option<String>>(3)?,
                            row.get::<_, String>(4)?,
                        ))
                    },
                )
                .map_err(|error| sqlite_error("query validation fact", error))?;
            Ok(SemanticFactKind::Validation(ValidationFact {
                check_name: row.0,
                status: decode_enum(&row.1)?,
                exit_code: row.2,
                completed_at: row.3,
                log_artifact_content_hashes: decode_string_list(&row.4)?,
            }))
        }
        "revision_commit_associated" => query_pair(
            connection,
            "SELECT commit_oid, tree_oid FROM semantic_commit_association_fact WHERE sequence = ?1",
            sequence,
            "query commit association fact",
        )
        .map(|pair| {
            pair.map_or(SemanticFactKind::Other, |(commit_oid, tree_oid)| {
                SemanticFactKind::CommitAssociated(CommitAssociationFact {
                    commit_oid,
                    tree_oid,
                })
            })
        }),
        "revision_commit_withdrawn" => {
            Ok(SemanticFactKind::CommitWithdrawn(CommitWithdrawalFact {
                association_id: query_single(
                    connection,
                    "SELECT association_id FROM semantic_commit_withdrawal_fact WHERE sequence = ?1",
                    sequence,
                    "query commit withdrawal fact",
                )?,
            }))
        }
        "revision_ref_associated" => query_pair(
            connection,
            "SELECT ref_name, head_oid FROM semantic_ref_association_fact WHERE sequence = ?1",
            sequence,
            "query ref association fact",
        )
        .map(|pair| {
            pair.map_or(SemanticFactKind::Other, |(ref_name, head_oid)| {
                SemanticFactKind::RefAssociated(RefAssociationFact { ref_name, head_oid })
            })
        }),
        "revision_ref_withdrawn" => Ok(SemanticFactKind::RefWithdrawn(RefWithdrawalFact {
            association_id: query_single(
                connection,
                "SELECT association_id FROM semantic_ref_withdrawal_fact WHERE sequence = ?1",
                sequence,
                "query ref withdrawal fact",
            )?,
        })),
        "artifact_removed" => Ok(SemanticFactKind::ArtifactRemoved),
        _ => Ok(SemanticFactKind::Other),
    }
}

fn query_pair(
    connection: &rusqlite::Connection,
    sql: &str,
    sequence: i64,
    operation: &'static str,
) -> Result<Option<(String, String)>, SqliteSemanticError> {
    connection
        .query_row(sql, [sequence], |row| Ok((row.get(0)?, row.get(1)?)))
        .optional()
        .map_err(|error| sqlite_error(operation, error))
}

fn query_single(
    connection: &rusqlite::Connection,
    sql: &str,
    sequence: i64,
    operation: &'static str,
) -> Result<String, SqliteSemanticError> {
    connection
        .query_row(sql, [sequence], |row| row.get(0))
        .map_err(|error| sqlite_error(operation, error))
}

fn enum_text<T: serde::Serialize>(value: T) -> Result<String, SqliteLocatorError> {
    encode_enum(value).map_err(|error| SqliteLocatorError::Delta(error.to_string()))
}

fn list_text(values: &[String]) -> Result<String, SqliteLocatorError> {
    encode_string_list(values).map_err(|error| SqliteLocatorError::Delta(error.to_string()))
}

fn validate_meta(
    connection: &rusqlite::Connection,
    expected: TruthCursor,
) -> Result<(), SqliteSemanticError> {
    let (profile, version, epoch, applied) = connection
        .query_row(
            "SELECT profile_id, schema_version, epoch, applied_sequence
             FROM semantic_meta WHERE singleton = 1",
            [],
            |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, i64>(2)?,
                    row.get::<_, i64>(3)?,
                ))
            },
        )
        .map_err(|error| sqlite_error("validate semantic metadata", error))?;
    if profile != SEMANTIC_PROFILE_ID
        || version != SEMANTIC_SCHEMA_VERSION
        || epoch != to_i64(expected.epoch, "expected semantic epoch")?
        || applied != to_i64(expected.sequence, "expected semantic applied")?
    {
        return Err(SqliteSemanticError::Metadata(format!(
            "semantic identity/checkpoint {profile}/{version}/{epoch}/{applied} \
             does not match {SEMANTIC_PROFILE_ID}/{SEMANTIC_SCHEMA_VERSION}/{expected:?}"
        )));
    }
    Ok(())
}

fn validate_product_history_meta(
    connection: &rusqlite::Connection,
    expected: TruthCursor,
) -> Result<(), SqliteSemanticError> {
    let (profile, version, epoch, applied) = connection
        .query_row(
            "SELECT profile_id, schema_version, epoch, applied_sequence
             FROM product_history_meta WHERE singleton = 1",
            [],
            |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, i64>(2)?,
                    row.get::<_, i64>(3)?,
                ))
            },
        )
        .map_err(|error| sqlite_error("validate product history metadata", error))?;
    if profile != PRODUCT_HISTORY_PROFILE_ID
        || version != PRODUCT_HISTORY_SCHEMA_VERSION
        || epoch != to_i64(expected.epoch, "expected product history epoch")?
        || applied != to_i64(expected.sequence, "expected product history applied")?
    {
        return Err(SqliteSemanticError::Metadata(format!(
            "product history identity/checkpoint {profile}/{version}/{epoch}/{applied} \
             does not match {PRODUCT_HISTORY_PROFILE_ID}/{PRODUCT_HISTORY_SCHEMA_VERSION}/{expected:?}"
        )));
    }
    Ok(())
}

fn query_names(
    connection: &rusqlite::Connection,
    sql: &str,
    column: usize,
) -> Result<Vec<String>, SqliteSemanticError> {
    let mut statement = connection
        .prepare(sql)
        .map_err(|error| sqlite_error("prepare semantic names", error))?;
    let rows = statement
        .query_map([], |row| row.get::<_, String>(column))
        .map_err(|error| sqlite_error("query semantic names", error))?;
    let mut names = Vec::new();
    for row in rows {
        names.push(row.map_err(|error| sqlite_error("read semantic name", error))?);
    }
    names.sort();
    Ok(names)
}

fn sqlite_error(operation: &'static str, error: rusqlite::Error) -> SqliteSemanticError {
    SqliteSemanticError::Sqlite {
        operation,
        message: error.to_string(),
    }
}

fn locator_sqlite_error(operation: &'static str, error: rusqlite::Error) -> SqliteLocatorError {
    SqliteLocatorError::Sqlite {
        operation,
        message: error.to_string(),
    }
}

fn to_i64(value: u64, label: &'static str) -> Result<i64, SqliteSemanticError> {
    i64::try_from(value)
        .map_err(|_| SqliteSemanticError::Metadata(format!("{label} does not fit SQLite INTEGER")))
}

fn to_i64_locator(value: u64, label: &'static str) -> Result<i64, SqliteLocatorError> {
    i64::try_from(value)
        .map_err(|_| SqliteLocatorError::Metadata(format!("{label} does not fit SQLite INTEGER")))
}

fn to_usize(value: i64, label: &'static str) -> Result<usize, SqliteSemanticError> {
    usize::try_from(value)
        .map_err(|_| SqliteSemanticError::Metadata(format!("{label} is negative or too large")))
}