distributed 4.3.0

CQRS/ES framework for Rust using Plain Old Rust Structs — append-only events, replay, snapshots, outbox, service bus, and pluggable infrastructure
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
use super::*;

pub(super) fn decode_change_row<DB>(
    row: &DB::Row,
    topology: &ProjectorTopologyId,
    partition: &ProjectionPartition,
    expected_epoch: &ProjectionEpoch,
) -> Result<ProjectionChange, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    for<'q> i64: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    let change_epoch: String = row
        .try_get("change_epoch")
        .map_err(|error| protocol_storage_error::<DB>("decode projection change epoch", error))?;
    let change_epoch = ProjectionEpoch::new(change_epoch)?;
    if &change_epoch != expected_epoch {
        return Err(corrupt_storage(
            "projection change epoch differs from its partition",
        ));
    }
    let change_position = from_i64::<DB>(
        row.try_get("change_position").map_err(|error| {
            protocol_storage_error::<DB>("decode projection change position", error)
        })?,
        "projection change position",
    )?;
    let kind_value: String = row
        .try_get("change_kind")
        .map_err(|error| protocol_storage_error::<DB>("decode projection change kind", error))?;
    let kind = decode_change_kind(&kind_value)?;
    let causation_id: String = row
        .try_get("causation_id")
        .map_err(|error| protocol_storage_error::<DB>("decode change causation ID", error))?;
    let model_name: Option<String> = row
        .try_get("model_name")
        .map_err(|error| protocol_storage_error::<DB>("decode change model", error))?;
    let scope_kind: Option<String> = row
        .try_get("scope_kind")
        .map_err(|error| protocol_storage_error::<DB>("decode change scope kind", error))?;
    let key_bytes: Option<Vec<u8>> = row
        .try_get("canonical_key_bytes")
        .map_err(|error| protocol_storage_error::<DB>("decode change key bytes", error))?;
    let key_hash: Option<Vec<u8>> = row
        .try_get("canonical_key_hash")
        .map_err(|error| protocol_storage_error::<DB>("decode change key hash", error))?;
    let incarnation: Option<i64> = row
        .try_get("incarnation")
        .map_err(|error| protocol_storage_error::<DB>("decode change record incarnation", error))?;
    let revision_value: Option<i64> = row
        .try_get("revision")
        .map_err(|error| protocol_storage_error::<DB>("decode change record revision", error))?;
    let failure_id: Option<String> = row
        .try_get("failure_id")
        .map_err(|error| protocol_storage_error::<DB>("decode change failure ID", error))?;

    let scope = match (model_name, key_bytes, key_hash) {
        (Some(model), Some(bytes), Some(hash)) => {
            let scope =
                ProjectionRecordScope::new(topology.clone(), partition.clone(), model, bytes)?;
            verify_digest(&hash, scope.key_digest(), "projection change key")?;
            Some(scope)
        }
        (None, None, None) => None,
        _ => {
            return Err(corrupt_storage(
                "projection change record scope has an inconsistent shape",
            ));
        }
    };
    let revision = match (scope.as_ref(), incarnation, revision_value) {
        (Some(scope), Some(incarnation), Some(revision)) => Some(RecordRevision::new(
            scope.clone(),
            from_i64::<DB>(incarnation, "change record incarnation")?,
            from_i64::<DB>(revision, "change record revision")?,
        )?),
        (_, None, None) => None,
        _ => {
            return Err(corrupt_storage(
                "projection change record revision has an inconsistent shape",
            ));
        }
    };
    let observation_kind = scope_kind
        .as_deref()
        .map(decode_observation_kind)
        .transpose()?;
    match kind {
        ProjectionChangeKind::RecordUpsert
        | ProjectionChangeKind::RecordDelete
        | ProjectionChangeKind::RecordRecreate
            if scope.is_some()
                && revision.is_some()
                && observation_kind.is_none()
                && failure_id.is_none() => {}
        ProjectionChangeKind::Observation
            if scope.is_some()
                && observation_kind.is_some()
                && failure_id.is_none()
                && ((observation_kind == Some(ProjectionObservationKind::Record)
                    && revision.is_some())
                    || (observation_kind == Some(ProjectionObservationKind::Dependency)
                        && revision.is_none())) => {}
        ProjectionChangeKind::Checkpoint
            if scope.is_none()
                && revision.is_none()
                && observation_kind.is_none()
                && failure_id.is_none() => {}
        ProjectionChangeKind::Failure
            if scope.is_none()
                && revision.is_none()
                && observation_kind.is_none()
                && failure_id.is_some() => {}
        _ => {
            return Err(corrupt_storage(format!(
                "projection change `{kind_value}` payload has an inconsistent shape"
            )));
        }
    }
    Ok(ProjectionChange {
        cursor: ProjectionChangeCursor::new(
            topology.clone(),
            partition.clone(),
            change_epoch,
            change_position,
        )?,
        kind,
        causation_id,
        observation_kind,
        scope,
        revision,
        failure_id,
    })
}

/// Apply one sealed same-transaction command projection inside its caller's
/// already-open domain/ledger transaction.
///
/// The adapter, rather than the command handler, allocates record revisions
/// and change cursors while holding the authoritative projection partition
/// lock. The returned evidence is attached to the command completion before
/// the caller executes the final ledger fence.
pub(crate) async fn apply_same_transaction_projection_in_tx<DB>(
    tx: &mut Transaction<'_, DB>,
    batch: &SameTransactionProjectionBatch,
    retention: ProjectionChangeRetention,
) -> Result<SameTransactionProjectionEvidence, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    DB::Arguments: IntoArguments<DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    batch.validate()?;
    let write_plan = TableWritePlan::new(
        batch
            .mutations
            .iter()
            .map(|mutation| mutation.mutation.clone())
            .collect(),
    );
    validate_sql_write_plan(&write_plan)?;
    verify_registered_topology_in_tx(tx, &batch.topology).await?;
    let mut state =
        lock_partition_in_tx(tx, &batch.topology, &batch.partition, &batch.change_epoch).await?;
    if let Some(failure_id) = &state.stopped_failure_id {
        return Err(ProjectionProtocolError::PartitionStopped {
            failure_id: failure_id.clone(),
        });
    }
    if state.pending_retry_failure_id.is_some() {
        return Err(ProjectionProtocolError::IncomparableInput);
    }
    ensure_partition_ownership_in_tx(tx, &batch.topology, &batch.partition, &batch.ownership)
        .await?;

    let staged = batch
        .mutations
        .first()
        .expect("same-transaction projection validation requires one mutation");
    let owner = batch
        .ownership
        .first()
        .expect("same-transaction projection validation requires one owner");
    if staged.mutation.table_name() != owner.table
        || table_model_name(&staged.mutation) != staged.scope.model()
    {
        return Err(ProjectionProtocolError::InvalidBatch(format!(
            "direct projection mutation for model `{}` does not target its registered table",
            staged.scope.model()
        )));
    }

    let current = record_in_tx(tx, &staged.scope, &state.change_epoch).await?;
    let physical_exists = physical_row_exists_in_tx(tx, &staged.mutation).await?;
    match current.as_ref().map(|record| &record.metadata) {
        None if physical_exists => {
            return Err(ProjectionProtocolError::RecordAlreadyExists {
                model: staged.scope.model().to_string(),
            });
        }
        Some(metadata) if metadata.tombstone => {
            return Err(ProjectionProtocolError::RecordTombstoned {
                model: staged.scope.model().to_string(),
            });
        }
        Some(_) if !physical_exists => {
            return Err(ProjectionProtocolError::RecordMissing {
                model: staged.scope.model().to_string(),
            });
        }
        _ => {}
    }
    let expectation = current
        .as_ref()
        .map(|record| ProjectionRecordExpectation::Exact(record.metadata.revision.clone()))
        .unwrap_or(ProjectionRecordExpectation::Missing);
    let (revision, tombstone) = next_record(
        &staged.scope,
        &expectation,
        ProjectionMutationKind::Upsert,
        current.as_ref(),
    )?;
    debug_assert!(!tombstone);
    let change = allocate_change(
        &mut state,
        &batch.topology,
        &batch.partition,
        ProjectionChangeKind::RecordUpsert,
        batch.causation_id.clone(),
        None,
        Some(staged.scope.clone()),
        Some(revision.clone()),
        None,
    )?;
    let metadata = ProjectionRecordMetadata {
        revision: revision.clone(),
        tombstone,
        change: change.cursor.clone(),
    };

    let observation_request = batch
        .observations
        .first()
        .expect("same-transaction projection validation requires one observation");
    if observation_in_tx(
        tx,
        &batch.causation_id,
        &staged.scope,
        observation_request.kind,
        &state.change_epoch,
    )
    .await?
    .is_some()
    {
        return Err(ProjectionProtocolError::InvalidBatch(format!(
            "direct projection causation `{}` already has record evidence",
            batch.causation_id
        )));
    }
    let observation = ProjectionObservation {
        causation_id: batch.causation_id.clone(),
        kind: observation_request.kind,
        scope: staged.scope.clone(),
        revision: Some(revision),
        change: change.cursor.clone(),
    };

    apply_read_model_write_plan_in_tx(tx, write_plan).await?;
    insert_change_in_tx(tx, &change).await?;
    upsert_record_in_tx(tx, &metadata).await?;
    insert_observation_in_tx(tx, &observation).await?;
    update_partition_head_in_tx(
        tx,
        &batch.topology,
        &batch.partition,
        state.change_head,
        None,
    )
    .await?;
    retain_projection_change_suffix_in_tx(tx, &batch.topology, &batch.partition, &state, retention)
        .await?;

    Ok(SameTransactionProjectionEvidence {
        records: vec![metadata],
        changes: vec![change],
        observations: vec![observation],
    })
}

/// Execute the entire query-row/evidence read as one SQL statement.
///
/// PostgreSQL's default transaction isolation takes a fresh snapshot for every
/// statement, so merely wrapping independent row and metadata selects in a
/// transaction would still permit mixed evidence. This joined statement gives
/// SQLite and PostgreSQL the same statement-level snapshot and repeats the
/// physical/protocol columns only when multiple explicit checkpoint probes
/// match.
pub(crate) async fn read_projection_query_snapshot_in_executor<'e, DB, E>(
    executor: E,
    request: &ProjectionQuerySnapshotRequest,
) -> Result<ProjectionQuerySnapshot, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    E: Executor<'e, Database = DB>,
    DB::Arguments: IntoArguments<DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    request.validate()?;
    let schema = request.schema.as_ref();
    let physical_version_column = version_column(schema)?;
    let topology_hash = request.scope.topology().digest();
    let partition_hash = request.scope.projection_partition().digest();
    let key_hash = request.scope.key_digest();

    let mut builder = QueryBuilder::<DB>::new("SELECT ");
    for (index, column) in schema.columns.iter().enumerate() {
        if index > 0 {
            builder.push(", ");
        }
        builder.push("snapshot_row.");
        builder.push(quote_identifier(&column.column_name));
        builder.push(" AS ");
        builder.push(quote_identifier(&format!("ps_row_{index}")));
    }
    if !schema.columns.is_empty() {
        builder.push(", ");
    }
    builder.push("snapshot_row.");
    builder.push(quote_identifier(physical_version_column));
    builder.push(
        " AS ps_row_version, \
         registered.topology_bytes AS ps_registered_topology_bytes, \
         registered.table_name AS ps_registered_table, \
         partition.topology_bytes AS ps_partition_topology_bytes, \
         partition.partition_bytes AS ps_partition_bytes, \
         partition.active_generation AS ps_active_generation, \
         partition.change_epoch AS ps_change_epoch, \
         partition.change_head AS ps_change_head, \
         partition.compacted_through AS ps_compacted, \
         record.canonical_key_bytes AS ps_record_key_bytes, \
         record.canonical_key_hash AS ps_record_key_hash, \
         record.incarnation AS ps_record_incarnation, \
         record.revision AS ps_record_revision, \
         record.tombstone AS ps_record_tombstone, \
         record.change_epoch AS ps_record_change_epoch, \
         record.change_position AS ps_record_change_position, \
         checkpoint.source_bytes AS ps_cursor_source_bytes, \
         checkpoint.source_hash AS ps_cursor_source_hash, \
         checkpoint.source_partition_bytes AS ps_cursor_partition_bytes, \
         checkpoint.source_partition_hash AS ps_cursor_partition_hash, \
         checkpoint.source_epoch AS ps_cursor_source_epoch, \
         checkpoint.source_position AS ps_cursor_source_position, \
         checkpoint.gap_free AS ps_cursor_gap_free, \
         checkpoint.generation AS ps_cursor_generation, \
         checkpoint.change_epoch AS ps_cursor_change_epoch, \
         checkpoint.change_position AS ps_cursor_change_position \
         FROM (SELECT 1 AS snapshot_anchor) AS snapshot_anchor \
         LEFT JOIN (SELECT ",
    );
    for (index, column) in schema.columns.iter().enumerate() {
        if index > 0 {
            builder.push(", ");
        }
        DB::push_select_column(&mut builder, column);
    }
    if !schema.columns.is_empty() {
        builder.push(", ");
    }
    builder.push(quote_identifier(physical_version_column));
    builder.push(" FROM ");
    builder.push(quote_identifier(&schema.table_name));
    push_key_predicates(&mut builder, schema, &request.key)?;
    builder.push(") AS snapshot_row ON 1 = 1 ");

    builder.push(
        "LEFT JOIN projection_registered_models AS registered \
         ON registered.topology_hash = ",
    );
    builder.push_bind(topology_hash.as_slice());
    builder.push(" AND registered.model_name = ");
    builder.push_bind(request.scope.model());

    builder.push(
        " LEFT JOIN projection_partitions AS partition \
         ON partition.topology_hash = ",
    );
    builder.push_bind(topology_hash.as_slice());
    builder.push(" AND partition.partition_hash = ");
    builder.push_bind(partition_hash.as_slice());

    builder.push(
        " LEFT JOIN projection_records AS record \
         ON record.topology_hash = partition.topology_hash \
         AND record.partition_hash = partition.partition_hash \
         AND record.model_name = ",
    );
    builder.push_bind(request.scope.model());
    builder.push(" AND record.canonical_key_hash = ");
    builder.push_bind(key_hash.as_slice());

    builder.push(
        " LEFT JOIN projection_input_cursors AS checkpoint \
         ON checkpoint.topology_hash = partition.topology_hash \
         AND checkpoint.partition_hash = partition.partition_hash AND ",
    );
    if request.checkpoint_probes.is_empty() {
        builder.push("1 = 0");
    } else {
        builder.push("(");
        for (index, probe) in request.checkpoint_probes.iter().enumerate() {
            if index > 0 {
                builder.push(" OR ");
            }
            builder.push("(checkpoint.source_hash = ");
            let source_hash = probe.source.digest();
            builder.push_bind(source_hash.as_slice());
            builder.push(" AND checkpoint.source_partition_hash = ");
            let source_partition_hash = probe.source.partition_digest();
            builder.push_bind(source_partition_hash.as_slice());
            builder.push(" AND checkpoint.generation = ");
            builder.push_bind(to_i64::<DB>(
                probe.generation.get(),
                "projection generation",
            )?);
            builder.push(")");
        }
        builder.push(")");
    }
    builder.push(
        " ORDER BY checkpoint.source_hash, checkpoint.source_partition_hash, \
         checkpoint.generation",
    );

    let rows = builder.build().fetch_all(executor).await.map_err(|error| {
        protocol_storage_error::<DB>("read atomic projection query snapshot", error)
    })?;
    let first = rows.first().ok_or_else(|| {
        corrupt_storage("atomic projection query snapshot returned no anchor row")
    })?;

    let row_version: Option<i64> = first.try_get("ps_row_version").map_err(|error| {
        protocol_storage_error::<DB>("decode projection query row presence", error)
    })?;
    let physical_row = if row_version.is_some() {
        let mut values = RowValues::new();
        for (index, column) in schema.columns.iter().enumerate() {
            let mut aliased = column.clone();
            aliased.column_name = format!("ps_row_{index}");
            values.insert(column.column_name.clone(), DB::row_value(first, &aliased)?);
        }
        validate_row_values(schema, &values, true)?;
        validate_values_match_key(schema, &request.key, &values)?;
        Some(values)
    } else {
        None
    };

    let registered_topology: Option<Vec<u8>> = first
        .try_get("ps_registered_topology_bytes")
        .map_err(|error| {
            protocol_storage_error::<DB>("decode projection query registered topology", error)
        })?;
    let Some(registered_topology) = registered_topology else {
        return Err(ProjectionProtocolError::InvalidBatch(format!(
            "projection query model `{}` has no registered topology owner",
            request.scope.model()
        )));
    };
    verify_bytes(
        &registered_topology,
        &request.scope.topology().canonical_bytes(),
        "projection query registered topology",
    )?;
    let registered_table: Option<String> =
        first.try_get("ps_registered_table").map_err(|error| {
            protocol_storage_error::<DB>("decode projection query registered table", error)
        })?;
    if registered_table.as_deref() != Some(schema.table_name.as_str()) {
        return Err(ProjectionProtocolError::InvalidBatch(format!(
            "projection query model `{}` is registered to table `{}`, not `{}`",
            request.scope.model(),
            registered_table.as_deref().unwrap_or("<missing>"),
            schema.table_name
        )));
    }

    let active_generation: Option<i64> =
        first.try_get("ps_active_generation").map_err(|error| {
            protocol_storage_error::<DB>("decode projection query partition presence", error)
        })?;
    let partition = match active_generation {
        Some(active_generation) => {
            let topology_bytes: Option<Vec<u8>> = first
                .try_get("ps_partition_topology_bytes")
                .map_err(|error| {
                    protocol_storage_error::<DB>(
                        "decode projection query partition topology",
                        error,
                    )
                })?;
            let partition_bytes: Option<Vec<u8>> =
                first.try_get("ps_partition_bytes").map_err(|error| {
                    protocol_storage_error::<DB>("decode projection query partition bytes", error)
                })?;
            verify_bytes(
                topology_bytes
                    .as_deref()
                    .ok_or_else(|| corrupt_storage("partition topology bytes are missing"))?,
                &request.scope.topology().canonical_bytes(),
                "projection query partition topology",
            )?;
            verify_bytes(
                partition_bytes
                    .as_deref()
                    .ok_or_else(|| corrupt_storage("partition bytes are missing"))?,
                request.scope.projection_partition().canonical_bytes(),
                "projection query partition",
            )?;
            let change_epoch: Option<String> =
                first.try_get("ps_change_epoch").map_err(|error| {
                    protocol_storage_error::<DB>("decode projection query change epoch", error)
                })?;
            let change_head: Option<i64> = first.try_get("ps_change_head").map_err(|error| {
                protocol_storage_error::<DB>("decode projection query change head", error)
            })?;
            let compacted_through: Option<i64> =
                first.try_get("ps_compacted").map_err(|error| {
                    protocol_storage_error::<DB>(
                        "decode projection query compaction watermark",
                        error,
                    )
                })?;
            let state = PartitionState {
                active_generation: ProjectionGeneration::new(from_i64::<DB>(
                    active_generation,
                    "projection active generation",
                )?)?,
                change_epoch: ProjectionEpoch::new(
                    change_epoch
                        .ok_or_else(|| corrupt_storage("partition change epoch is missing"))?,
                )?,
                change_head: from_i64::<DB>(
                    change_head
                        .ok_or_else(|| corrupt_storage("partition change head is missing"))?,
                    "projection change head",
                )?,
                compacted_through: from_i64::<DB>(
                    compacted_through.ok_or_else(|| {
                        corrupt_storage("partition compaction watermark is missing")
                    })?,
                    "projection compaction watermark",
                )?,
                pending_retry_failure_id: None,
                stopped_failure_id: None,
            };
            if state.compacted_through > state.change_head {
                return Err(corrupt_storage(
                    "projection compaction watermark exceeds change head",
                ));
            }
            Some(state)
        }
        None => None,
    };

    let record_incarnation: Option<i64> =
        first.try_get("ps_record_incarnation").map_err(|error| {
            protocol_storage_error::<DB>("decode projection query record presence", error)
        })?;
    let record = match record_incarnation {
        Some(incarnation) => {
            let Some(partition) = partition.as_ref() else {
                return Err(corrupt_storage(
                    "projection record exists without partition state",
                ));
            };
            let key_bytes: Option<Vec<u8>> =
                first.try_get("ps_record_key_bytes").map_err(|error| {
                    protocol_storage_error::<DB>("decode projection query record key bytes", error)
                })?;
            let stored_key_hash: Option<Vec<u8>> =
                first.try_get("ps_record_key_hash").map_err(|error| {
                    protocol_storage_error::<DB>("decode projection query record key hash", error)
                })?;
            verify_bytes(
                key_bytes
                    .as_deref()
                    .ok_or_else(|| corrupt_storage("projection record key bytes are missing"))?,
                request.scope.canonical_key_bytes(),
                "projection query record key",
            )?;
            verify_digest(
                stored_key_hash
                    .as_deref()
                    .ok_or_else(|| corrupt_storage("projection record key hash is missing"))?,
                request.scope.key_digest(),
                "projection query record key",
            )?;
            let revision: Option<i64> = first.try_get("ps_record_revision").map_err(|error| {
                protocol_storage_error::<DB>("decode projection query record revision", error)
            })?;
            let tombstone: Option<i64> = first.try_get("ps_record_tombstone").map_err(|error| {
                protocol_storage_error::<DB>("decode projection query record tombstone", error)
            })?;
            let tombstone = match tombstone
                .ok_or_else(|| corrupt_storage("projection record tombstone is missing"))?
            {
                0 => false,
                1 => true,
                value => {
                    return Err(corrupt_storage(format!(
                        "record tombstone contains invalid value {value}"
                    )));
                }
            };
            let record_change_epoch: Option<String> =
                first.try_get("ps_record_change_epoch").map_err(|error| {
                    protocol_storage_error::<DB>(
                        "decode projection query record change epoch",
                        error,
                    )
                })?;
            let record_change_epoch = ProjectionEpoch::new(
                record_change_epoch
                    .ok_or_else(|| corrupt_storage("record change epoch is missing"))?,
            )?;
            if record_change_epoch != partition.change_epoch {
                return Err(corrupt_storage(
                    "projection record change epoch differs from its partition",
                ));
            }
            let record_change_position: Option<i64> = first
                .try_get("ps_record_change_position")
                .map_err(|error| {
                    protocol_storage_error::<DB>(
                        "decode projection query record change position",
                        error,
                    )
                })?;
            let record_change_position = from_i64::<DB>(
                record_change_position
                    .ok_or_else(|| corrupt_storage("record change position is missing"))?,
                "projection record change position",
            )?;
            if record_change_position > partition.change_head {
                return Err(corrupt_storage(
                    "projection record change exceeds its partition head",
                ));
            }
            Some(ProjectionRecordMetadata {
                revision: RecordRevision::new(
                    request.scope.clone(),
                    from_i64::<DB>(incarnation, "projection record incarnation")?,
                    from_i64::<DB>(
                        revision.ok_or_else(|| corrupt_storage("record revision is missing"))?,
                        "projection record revision",
                    )?,
                )?,
                tombstone,
                change: ProjectionChangeCursor::new(
                    request.scope.topology().clone(),
                    request.scope.projection_partition().clone(),
                    record_change_epoch,
                    record_change_position,
                )?,
            })
        }
        None => None,
    };

    match (physical_row.is_some(), record.as_ref()) {
        (true, None)
        | (
            true,
            Some(ProjectionRecordMetadata {
                tombstone: true, ..
            }),
        ) => {
            return Err(ProjectionProtocolError::RecordAlreadyExists {
                model: request.scope.model().to_string(),
            });
        }
        (
            false,
            Some(ProjectionRecordMetadata {
                tombstone: false, ..
            }),
        ) => {
            return Err(ProjectionProtocolError::RecordMissing {
                model: request.scope.model().to_string(),
            });
        }
        _ => {}
    }

    let mut checkpoint_values = vec![None; request.checkpoint_probes.len()];
    for row in &rows {
        let stored_generation: Option<i64> =
            row.try_get("ps_cursor_generation").map_err(|error| {
                protocol_storage_error::<DB>("decode projection query checkpoint presence", error)
            })?;
        let Some(stored_generation) = stored_generation else {
            continue;
        };
        let Some(partition) = partition.as_ref() else {
            return Err(corrupt_storage(
                "projection checkpoint exists without partition state",
            ));
        };
        let generation = ProjectionGeneration::new(from_i64::<DB>(
            stored_generation,
            "projection checkpoint generation",
        )?)?;
        let source_bytes: Option<Vec<u8>> =
            row.try_get("ps_cursor_source_bytes").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint source bytes",
                    error,
                )
            })?;
        let source_hash: Option<Vec<u8>> =
            row.try_get("ps_cursor_source_hash").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint source hash",
                    error,
                )
            })?;
        let source_partition_bytes: Option<Vec<u8>> =
            row.try_get("ps_cursor_partition_bytes").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint source partition bytes",
                    error,
                )
            })?;
        let source_partition_hash: Option<Vec<u8>> =
            row.try_get("ps_cursor_partition_hash").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint source partition hash",
                    error,
                )
            })?;
        let source_hash = source_hash
            .as_deref()
            .ok_or_else(|| corrupt_storage("checkpoint source hash is missing"))?;
        let source_partition_hash = source_partition_hash
            .as_deref()
            .ok_or_else(|| corrupt_storage("checkpoint source partition hash is missing"))?;
        let Some(probe_index) = request.checkpoint_probes.iter().position(|probe| {
            probe.generation == generation
                && source_hash == probe.source.digest().as_slice()
                && source_partition_hash == probe.source.partition_digest().as_slice()
        }) else {
            return Err(corrupt_storage(
                "checkpoint row does not match an explicit query probe",
            ));
        };
        let probe = &request.checkpoint_probes[probe_index];
        verify_bytes(
            source_bytes
                .as_deref()
                .ok_or_else(|| corrupt_storage("checkpoint source bytes are missing"))?,
            &probe.source.canonical_name_bytes(),
            "projection query checkpoint source",
        )?;
        verify_digest(
            source_hash,
            probe.source.digest(),
            "projection query checkpoint source",
        )?;
        verify_bytes(
            source_partition_bytes
                .as_deref()
                .ok_or_else(|| corrupt_storage("checkpoint source partition bytes are missing"))?,
            probe.source.canonical_partition_bytes(),
            "projection query checkpoint source partition",
        )?;
        verify_digest(
            source_partition_hash,
            probe.source.partition_digest(),
            "projection query checkpoint source partition",
        )?;
        let source_epoch: Option<String> =
            row.try_get("ps_cursor_source_epoch").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint source epoch",
                    error,
                )
            })?;
        let source_epoch = ProjectionEpoch::new(
            source_epoch.ok_or_else(|| corrupt_storage("checkpoint source epoch is missing"))?,
        )?;
        if source_epoch != probe.epoch {
            return Err(ProjectionProtocolError::IncomparableInput);
        }
        let source_position: Option<i64> =
            row.try_get("ps_cursor_source_position").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint source position",
                    error,
                )
            })?;
        let gap_free: Option<i64> = row.try_get("ps_cursor_gap_free").map_err(|error| {
            protocol_storage_error::<DB>("decode projection query checkpoint gap-free flag", error)
        })?;
        let gap_free =
            match gap_free.ok_or_else(|| corrupt_storage("checkpoint gap-free flag is missing"))? {
                0 => false,
                1 => true,
                value => {
                    return Err(corrupt_storage(format!(
                        "cursor gap-free flag contains invalid value {value}"
                    )));
                }
            };
        let checkpoint_change_epoch: Option<String> =
            row.try_get("ps_cursor_change_epoch").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint change epoch",
                    error,
                )
            })?;
        let checkpoint_change_epoch = ProjectionEpoch::new(
            checkpoint_change_epoch
                .ok_or_else(|| corrupt_storage("checkpoint change epoch is missing"))?,
        )?;
        let checkpoint_change_position: Option<i64> =
            row.try_get("ps_cursor_change_position").map_err(|error| {
                protocol_storage_error::<DB>(
                    "decode projection query checkpoint change position",
                    error,
                )
            })?;
        let checkpoint_change_position = from_i64::<DB>(
            checkpoint_change_position
                .ok_or_else(|| corrupt_storage("checkpoint change position is missing"))?,
            "projection checkpoint change position",
        )?;
        if checkpoint_change_epoch != partition.change_epoch
            || checkpoint_change_position > partition.change_head
        {
            return Err(corrupt_storage(
                "projection checkpoint change lies outside its partition head",
            ));
        }
        let checkpoint = ProjectionCheckpoint::new(
            ProjectionInputCursor::new(
                probe.topology.clone(),
                probe.partition.clone(),
                probe.source.clone(),
                source_epoch,
                from_i64::<DB>(
                    source_position
                        .ok_or_else(|| corrupt_storage("checkpoint source position is missing"))?,
                    "projection checkpoint source position",
                )?,
            )?,
            ProjectionChangeCursor::new(
                probe.topology.clone(),
                probe.partition.clone(),
                checkpoint_change_epoch,
                checkpoint_change_position,
            )?,
            gap_free,
        )?;
        if checkpoint_values[probe_index].replace(checkpoint).is_some() {
            return Err(corrupt_storage(
                "projection query returned duplicate checkpoint rows",
            ));
        }
    }

    let checkpoints = request
        .checkpoint_probes
        .iter()
        .cloned()
        .zip(checkpoint_values)
        .map(
            |(probe, checkpoint)| crate::projection_protocol::ProjectionCheckpointSnapshot {
                probe,
                checkpoint,
            },
        )
        .collect();
    let (change_head, compacted_through) = match partition {
        Some(partition) => {
            let head = if partition.change_head == 0 {
                None
            } else {
                Some(ProjectionChangeCursor::new(
                    request.scope.topology().clone(),
                    request.scope.projection_partition().clone(),
                    partition.change_epoch,
                    partition.change_head,
                )?)
            };
            (head, partition.compacted_through)
        }
        None => (None, 0),
    };

    Ok(ProjectionQuerySnapshot {
        row: physical_row,
        record,
        checkpoints,
        change_head,
        compacted_through,
    })
}

pub(crate) async fn read_projection_execution_snapshot_batch_in_executor<DB>(
    connection: &mut DB::Connection,
    request: &ProjectionExecutionSnapshotBatchRequest,
) -> Result<ProjectionExecutionSnapshotBatch, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    DB::Arguments: IntoArguments<DB>,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    request.validate()?;
    let mut snapshots = Vec::with_capacity(request.requests.len());
    for row_request in &request.requests {
        let snapshot =
            read_projection_query_snapshot_in_executor::<DB, _>(&mut *connection, row_request)
                .await?;
        snapshots.push(ProjectionScopedRowSnapshot {
            scope: row_request.scope.clone(),
            row: snapshot.row,
            record: snapshot.record,
        });
    }
    Ok(ProjectionExecutionSnapshotBatch { snapshots })
}

pub(crate) async fn read_projection_graph_snapshot_in_executor<DB>(
    connection: &mut DB::Connection,
    request: &ProjectionGraphSnapshotRequest,
) -> Result<ProjectionGraphSnapshot, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    DB::Arguments: IntoArguments<DB>,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    validate_projection_graph_snapshot_request(request)?;
    let root_snapshot =
        read_projection_query_snapshot_in_executor::<DB, _>(&mut *connection, &request.root)
            .await?;
    let root = ProjectionScopedRowSnapshot {
        scope: request.root.scope.clone(),
        row: root_snapshot.row,
        record: root_snapshot.record,
    };
    let mut includes = std::collections::BTreeMap::new();
    let mut unique_scopes = std::collections::HashSet::from([root.scope.clone()]);
    let mut materialized_snapshots = 1;
    for (name, include) in &request.includes {
        let mut snapshots = Vec::new();
        if let Some(root_row) = root.row.as_ref() {
            let keys = read_projection_relationship_keys_in_executor::<DB>(
                connection,
                &request.root.schema,
                root_row,
                &include.relationship,
                &include.target_schema,
                request.max_unique_record_scopes,
            )
            .await?;
            let codec = ProjectionScopeCodec::with_models(
                request.root.scope.topology().clone(),
                [(
                    include.target_schema.model_name.as_str(),
                    include.target_schema.as_ref(),
                )],
            )
            .map_err(|error| {
                ProjectionProtocolError::InvalidBatch(format!(
                    "invalid projection graph target schema: {error}"
                ))
            })?;
            for key in keys {
                materialized_snapshots =
                    checked_projection_graph_materialization(materialized_snapshots)?;
                let scope = codec
                    .encode_row_scope_in_partition(
                        &include.target_schema.model_name,
                        request.root.scope.projection_partition().clone(),
                        &key,
                    )
                    .map_err(|error| {
                        ProjectionProtocolError::InvalidBatch(format!(
                            "invalid projection graph included key: {error}"
                        ))
                    })?;
                let is_new_scope = unique_scopes.insert(scope.clone());
                if is_new_scope && unique_scopes.len() > request.max_unique_record_scopes {
                    return Err(projection_graph_budget_error(
                        &request.root.schema,
                        unique_scopes.len(),
                        request.max_unique_record_scopes,
                    ));
                }
                let row_request = ProjectionQuerySnapshotRequest {
                    schema: Arc::clone(&include.target_schema),
                    key,
                    scope: scope.clone(),
                    checkpoint_probes: Vec::new(),
                };
                let snapshot = read_projection_query_snapshot_in_executor::<DB, _>(
                    &mut *connection,
                    &row_request,
                )
                .await?;
                snapshots.push(ProjectionScopedRowSnapshot {
                    scope,
                    row: snapshot.row,
                    record: snapshot.record,
                });
            }
            snapshots.sort_by(|left, right| {
                left.scope
                    .canonical_key_bytes()
                    .cmp(right.scope.canonical_key_bytes())
            });
        }
        includes.insert(
            name.clone(),
            ProjectionGraphIncludeSnapshot {
                relationship: include.relationship.clone(),
                target_schema: include.target_schema.as_ref().clone(),
                rows: snapshots,
            },
        );
    }
    Ok(ProjectionGraphSnapshot { root, includes })
}

#[allow(clippy::too_many_arguments)]
async fn read_projection_relationship_keys_in_executor<DB>(
    connection: &mut DB::Connection,
    root_schema: &TableSchema,
    root_row: &RowValues,
    relationship: &crate::table::RelationshipDef,
    target_schema: &TableSchema,
    max_unique: usize,
) -> Result<Vec<RowKey>, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    DB::Arguments: IntoArguments<DB>,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    let foreign_key = relationship.foreign_key.as_deref().ok_or_else(|| {
        ProjectionProtocolError::InvalidBatch(format!(
            "projection graph relationship `{}` has no foreign key",
            relationship.field_name
        ))
    })?;
    let (target_column, value) = match relationship.kind {
        RelationshipKind::HasMany => {
            let (target_column, root_column) =
                projection_has_many_columns(root_schema, relationship, target_schema)?;
            let value = root_row.get(&root_column).cloned().ok_or_else(|| {
                ProjectionProtocolError::InvalidBatch(format!(
                    "projection graph root `{}` is missing relationship key `{root_column}`",
                    root_schema.model_name
                ))
            })?;
            (target_column, value)
        }
        RelationshipKind::BelongsTo => {
            let source_column = column_name_for(root_schema, foreign_key).ok_or_else(|| {
                ProjectionProtocolError::InvalidBatch(format!(
                    "projection graph relationship `{}` foreign key `{foreign_key}` is not a source column",
                    relationship.field_name
                ))
            })?;
            let [target_column] = target_schema.primary_key.columns.as_slice() else {
                return Err(ProjectionProtocolError::InvalidBatch(format!(
                    "projection graph belongs-to target `{}` must have one primary-key column",
                    target_schema.model_name
                )));
            };
            let value = root_row.get(&source_column).cloned().ok_or_else(|| {
                ProjectionProtocolError::InvalidBatch(format!(
                    "projection graph root `{}` is missing relationship key `{source_column}`",
                    root_schema.model_name
                ))
            })?;
            (target_column.clone(), value)
        }
        RelationshipKind::ManyToMany => {
            return Err(ProjectionProtocolError::InvalidBatch(format!(
                "projection graph relationship `{}` is many-to-many; project an explicit join read model instead",
                relationship.field_name
            )));
        }
    };
    if value == RowValue::Null {
        return Ok(Vec::new());
    }

    let mut builder = QueryBuilder::<DB>::new("SELECT ");
    for (index, primary_key) in target_schema.primary_key.columns.iter().enumerate() {
        if index > 0 {
            builder.push(", ");
        }
        DB::push_select_column(&mut builder, column_by_name(target_schema, primary_key)?);
    }
    builder.push(" FROM ");
    builder.push(quote_identifier(&target_schema.table_name));
    builder.push(" WHERE ");
    builder.push(quote_identifier(&target_column));
    builder.push(" = ");
    DB::push_row_value_bind(
        &mut builder,
        value,
        column_by_name(target_schema, &target_column)?,
    )?;
    push_order_by_primary_key(&mut builder, target_schema);
    builder.push(" LIMIT ");
    builder.push(max_unique.saturating_add(1).to_string());

    let rows = builder
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| {
            protocol_storage_error::<DB>("load projection graph relationship keys", error)
        })?;
    if rows.len() > max_unique {
        return Err(projection_graph_budget_error(
            root_schema,
            max_unique.saturating_add(1),
            max_unique,
        ));
    }
    rows.iter()
        .map(|row| {
            let values = target_schema
                .primary_key
                .columns
                .iter()
                .map(|primary_key| {
                    let column = column_by_name(target_schema, primary_key)?;
                    Ok((primary_key.clone(), DB::row_value(row, column)?))
                })
                .collect::<Result<Vec<_>, TableStoreError>>()?;
            Ok(RowKey::new(values))
        })
        .collect()
}

fn projection_graph_budget_error(
    root_schema: &TableSchema,
    returned: usize,
    maximum: usize,
) -> ProjectionProtocolError {
    ProjectionProtocolError::InvalidBatch(format!(
        "projection graph model `{}` returned {returned} unique record scopes; request budget is {maximum}",
        root_schema.model_name
    ))
}

/// Resolve a bounded command-obligation set from one existing SQL snapshot.
///
/// Observations and failures are read in two set queries on the same borrowed
/// connection. A durable failure is immutable and therefore wins even when an
/// observation for the same causation also exists or its change entry has been
/// compacted.
pub(crate) async fn read_projection_obligation_evidence_batch_in_executor<DB>(
    connection: &mut DB::Connection,
    request: &ProjectionObligationEvidenceBatchRequest,
) -> Result<ProjectionObligationEvidenceBatch, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    DB::Arguments: IntoArguments<DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    request.validate()?;
    if request.requests.is_empty() {
        return Ok(ProjectionObligationEvidenceBatch::default());
    }

    let mut observations = vec![None; request.requests.len()];
    let mut observation_query = QueryBuilder::<DB>::new(
        "SELECT observation.topology_hash AS evidence_topology_hash, \
         observation.partition_hash AS evidence_partition_hash, \
         observation.causation_id AS evidence_causation_id, \
         observation.model_name AS evidence_model_name, \
         observation.scope_kind AS evidence_scope_kind, \
         observation.canonical_key_bytes, observation.canonical_key_hash, \
         observation.incarnation, observation.revision, observation.change_epoch, \
         observation.change_position, partition.topology_bytes AS evidence_topology_bytes, \
         partition.partition_bytes AS evidence_partition_bytes, \
         partition.change_epoch AS evidence_partition_epoch, \
         partition.change_head AS evidence_partition_head \
         FROM projection_observations AS observation \
         INNER JOIN projection_partitions AS partition \
         ON partition.topology_hash = observation.topology_hash \
         AND partition.partition_hash = observation.partition_hash WHERE ",
    );
    for (index, probe) in request.requests.iter().enumerate() {
        if index > 0 {
            observation_query.push(" OR ");
        }
        let topology_hash = probe.scope.topology().digest();
        let partition_hash = probe.scope.projection_partition().digest();
        let key_hash = probe.scope.key_digest();
        observation_query.push("(observation.topology_hash = ");
        observation_query.push_bind(topology_hash.as_slice());
        observation_query.push(" AND observation.partition_hash = ");
        observation_query.push_bind(partition_hash.as_slice());
        observation_query.push(" AND observation.causation_id = ");
        observation_query.push_bind(probe.causation_id.as_str());
        observation_query.push(" AND observation.model_name = ");
        observation_query.push_bind(probe.scope.model());
        observation_query.push(" AND observation.scope_kind = ");
        observation_query.push_bind(probe.kind.as_storage_str());
        observation_query.push(" AND observation.canonical_key_hash = ");
        observation_query.push_bind(key_hash.as_slice());
        observation_query.push(")");
    }
    let observation_rows = observation_query
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| {
            protocol_storage_error::<DB>("read projection obligation observations", error)
        })?;
    for row in observation_rows {
        let topology_hash = decode_digest(
            row.try_get("evidence_topology_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode evidence topology hash", error)
            })?,
            "projection evidence topology",
        )?;
        let partition_hash = decode_digest(
            row.try_get("evidence_partition_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode evidence partition hash", error)
            })?,
            "projection evidence partition",
        )?;
        let causation_id: String = row
            .try_get("evidence_causation_id")
            .map_err(|error| protocol_storage_error::<DB>("decode evidence causation ID", error))?;
        let model: String = row
            .try_get("evidence_model_name")
            .map_err(|error| protocol_storage_error::<DB>("decode evidence model", error))?;
        let kind_value: String = row.try_get("evidence_scope_kind").map_err(|error| {
            protocol_storage_error::<DB>("decode evidence observation kind", error)
        })?;
        let kind = decode_observation_kind(&kind_value)?;
        let key_hash = decode_digest(
            row.try_get("canonical_key_hash")
                .map_err(|error| protocol_storage_error::<DB>("decode evidence key hash", error))?,
            "projection evidence key",
        )?;
        let key_bytes: Vec<u8> = row
            .try_get("canonical_key_bytes")
            .map_err(|error| protocol_storage_error::<DB>("decode evidence key bytes", error))?;
        let digest_candidates = request
            .requests
            .iter()
            .enumerate()
            .filter(|(_, probe)| {
                probe.scope.topology().digest() == topology_hash
                    && probe.scope.projection_partition().digest() == partition_hash
                    && probe.causation_id == causation_id
                    && probe.scope.model() == model
                    && probe.kind == kind
                    && probe.scope.key_digest() == key_hash
            })
            .collect::<Vec<_>>();
        let exact = digest_candidates
            .iter()
            .filter(|(_, probe)| probe.scope.canonical_key_bytes() == key_bytes.as_slice())
            .map(|(index, _)| *index)
            .collect::<Vec<_>>();
        let [index] = exact.as_slice() else {
            return Err(corrupt_storage(if digest_candidates.is_empty() {
                "projection observation escaped its bounded evidence predicate".to_string()
            } else {
                "projection observation canonical key does not match its digest lookup".to_string()
            }));
        };
        let probe = &request.requests[*index];
        let topology_bytes: Vec<u8> = row.try_get("evidence_topology_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode evidence topology bytes", error)
        })?;
        verify_bytes(
            &topology_bytes,
            &probe.scope.topology().canonical_bytes(),
            "projection evidence topology",
        )?;
        let partition_bytes: Vec<u8> =
            row.try_get("evidence_partition_bytes").map_err(|error| {
                protocol_storage_error::<DB>("decode evidence partition bytes", error)
            })?;
        verify_bytes(
            &partition_bytes,
            probe.scope.projection_partition().canonical_bytes(),
            "projection evidence partition",
        )?;
        let partition_epoch = ProjectionEpoch::new(
            row.try_get::<String, _>("evidence_partition_epoch")
                .map_err(|error| {
                    protocol_storage_error::<DB>("decode evidence partition epoch", error)
                })?,
        )?;
        let partition_head = from_i64::<DB>(
            row.try_get("evidence_partition_head").map_err(|error| {
                protocol_storage_error::<DB>("decode evidence partition head", error)
            })?,
            "projection evidence partition head",
        )?;
        let observation = decode_observation_row::<DB>(
            &row,
            &probe.causation_id,
            &probe.scope,
            probe.kind,
            &partition_epoch,
        )?;
        if observation.change.position() > partition_head {
            return Err(corrupt_storage(
                "projection observation change exceeds its partition head",
            ));
        }
        if observations[*index].replace(observation).is_some() {
            return Err(corrupt_storage(
                "projection obligation evidence returned duplicate observations",
            ));
        }
    }

    let mut failures = vec![None; request.requests.len()];
    let mut failure_query = QueryBuilder::<DB>::new(
        "SELECT failure.topology_hash AS evidence_topology_hash, \
         failure.partition_hash AS evidence_partition_hash, failure.failure_id, \
         failure.source_bytes, failure.source_hash, failure.source_partition_bytes, \
         failure.source_partition_hash, failure.source_epoch, failure.source_position, \
         failure.input_hash, failure.message_id, failure.causation_id, failure.gap_free, \
         failure.generation, failure.failure_code, failure.failure_bytes, \
         failure.failure_hash, failure.change_epoch, failure.change_position, \
         partition.topology_bytes AS evidence_topology_bytes, \
         partition.partition_bytes AS evidence_partition_bytes, \
         partition.change_epoch AS evidence_partition_epoch, \
         partition.change_head AS evidence_partition_head \
         FROM projection_failures AS failure \
         INNER JOIN projection_partitions AS partition \
         ON partition.topology_hash = failure.topology_hash \
         AND partition.partition_hash = failure.partition_hash WHERE ",
    );
    for (index, probe) in request.requests.iter().enumerate() {
        if index > 0 {
            failure_query.push(" OR ");
        }
        let topology_hash = probe.scope.topology().digest();
        let partition_hash = probe.scope.projection_partition().digest();
        failure_query.push("(failure.topology_hash = ");
        failure_query.push_bind(topology_hash.as_slice());
        failure_query.push(" AND failure.partition_hash = ");
        failure_query.push_bind(partition_hash.as_slice());
        failure_query.push(" AND failure.causation_id = ");
        failure_query.push_bind(probe.causation_id.as_str());
        failure_query.push(")");
    }
    failure_query.push(
        " ORDER BY failure.topology_hash, failure.partition_hash, \
         failure.causation_id, failure.change_position, failure.failure_id",
    );
    let failure_rows = failure_query
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| {
            protocol_storage_error::<DB>("read projection obligation failures", error)
        })?;
    for row in failure_rows {
        let topology_hash = decode_digest(
            row.try_get("evidence_topology_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode failure evidence topology hash", error)
            })?,
            "projection failure evidence topology",
        )?;
        let partition_hash = decode_digest(
            row.try_get("evidence_partition_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode failure evidence partition hash", error)
            })?,
            "projection failure evidence partition",
        )?;
        let causation_id: String = row.try_get("causation_id").map_err(|error| {
            protocol_storage_error::<DB>("decode failure evidence causation ID", error)
        })?;
        let matching = request
            .requests
            .iter()
            .enumerate()
            .filter(|(_, probe)| {
                probe.scope.topology().digest() == topology_hash
                    && probe.scope.projection_partition().digest() == partition_hash
                    && probe.causation_id == causation_id
            })
            .map(|(index, _)| index)
            .collect::<Vec<_>>();
        let Some(first_index) = matching.first().copied() else {
            return Err(corrupt_storage(
                "projection failure escaped its bounded evidence predicate",
            ));
        };
        let first = &request.requests[first_index];
        let topology_bytes: Vec<u8> = row.try_get("evidence_topology_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode failure evidence topology bytes", error)
        })?;
        verify_bytes(
            &topology_bytes,
            &first.scope.topology().canonical_bytes(),
            "projection failure evidence topology",
        )?;
        let partition_bytes: Vec<u8> =
            row.try_get("evidence_partition_bytes").map_err(|error| {
                protocol_storage_error::<DB>("decode failure evidence partition bytes", error)
            })?;
        verify_bytes(
            &partition_bytes,
            first.scope.projection_partition().canonical_bytes(),
            "projection failure evidence partition",
        )?;
        let partition_epoch = ProjectionEpoch::new(
            row.try_get::<String, _>("evidence_partition_epoch")
                .map_err(|error| {
                    protocol_storage_error::<DB>("decode failure evidence partition epoch", error)
                })?,
        )?;
        let partition_head = from_i64::<DB>(
            row.try_get("evidence_partition_head").map_err(|error| {
                protocol_storage_error::<DB>("decode failure evidence partition head", error)
            })?,
            "projection failure evidence partition head",
        )?;
        let failure = decode_failure_row::<DB>(
            &row,
            first.scope.topology(),
            first.scope.projection_partition(),
            &partition_epoch,
        )?
        .failure;
        if failure.causation_id != causation_id || failure.change.position() > partition_head {
            return Err(corrupt_storage(
                "projection failure evidence lies outside its exact partition/causation",
            ));
        }
        // Rows are ordered by change position, so retaining the first durable
        // failure preserves the command promise's original terminal outcome.
        for index in matching {
            if failures[index].is_none() {
                failures[index] = Some(failure.clone());
            }
        }
    }

    let evidence = failures
        .into_iter()
        .zip(observations)
        .map(|(failure, observation)| match (failure, observation) {
            (Some(failure), _) => ProjectionObligationEvidence::TerminalFailure(failure),
            (None, Some(observation)) => ProjectionObligationEvidence::Observed(observation),
            (None, None) => ProjectionObligationEvidence::Pending,
        })
        .collect();
    Ok(ProjectionObligationEvidenceBatch { evidence })
}

/// Discover the bounded durable evidence for one modeled command causation.
///
/// The command ledger stores only opaque scope tokens. This read returns
/// server-internal candidates from one SQL snapshot; the authenticated GraphQL
/// authority later remints each token and accepts exact byte equality only.
pub(crate) async fn read_projection_causation_evidence_in_executor<DB>(
    connection: &mut DB::Connection,
    request: &ProjectionCausationEvidenceRequest,
) -> Result<ProjectionCausationEvidenceBatch, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    DB::Arguments: IntoArguments<DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    request.validate()?;
    let overflow_limit = MAX_PROJECTION_EVIDENCE_BATCH_ITEMS + 1;
    let mut observation_query = QueryBuilder::<DB>::new(
        "SELECT observation.topology_hash, observation.partition_hash, \
         observation.causation_id, observation.model_name, observation.scope_kind, \
         observation.canonical_key_bytes, observation.canonical_key_hash, \
         observation.incarnation, observation.revision, observation.change_epoch, \
         observation.change_position, partition.topology_bytes, \
         partition.partition_bytes, partition.change_epoch AS partition_change_epoch, \
         partition.change_head AS partition_change_head \
         FROM projection_observations observation \
         INNER JOIN projection_partitions partition \
         ON partition.topology_hash = observation.topology_hash \
         AND partition.partition_hash = observation.partition_hash \
         WHERE observation.causation_id = ",
    );
    observation_query.push_bind(request.causation_id.as_str());
    observation_query.push(" AND (");
    for (index, topology) in request.topologies.iter().enumerate() {
        if index > 0 {
            observation_query.push(" OR ");
        }
        let topology_hash = topology.digest();
        observation_query.push("observation.topology_hash = ");
        observation_query.push_bind(topology_hash.as_slice());
    }
    observation_query.push(")");
    observation_query.push(
        " ORDER BY observation.topology_hash, observation.partition_hash, \
         observation.model_name, observation.scope_kind, observation.canonical_key_hash \
         LIMIT ",
    );
    observation_query.push(overflow_limit.to_string());
    let rows = observation_query
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| {
            protocol_storage_error::<DB>("read modeled projection causation observations", error)
        })?;
    if rows.len() > MAX_PROJECTION_EVIDENCE_BATCH_ITEMS {
        return Err(ProjectionProtocolError::InvalidBatch(format!(
            "projection causation has more than {} observations",
            MAX_PROJECTION_EVIDENCE_BATCH_ITEMS
        )));
    }

    let mut observations = Vec::with_capacity(rows.len());
    for row in rows {
        let stored_causation_id: String = row.try_get("causation_id").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence causation ID", error)
        })?;
        if stored_causation_id != request.causation_id {
            return Err(corrupt_storage(
                "modeled projection observation escaped its causation predicate",
            ));
        }
        let topology_bytes: Vec<u8> = row.try_get("topology_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence topology bytes", error)
        })?;
        let topology = ProjectorTopologyId::from_canonical_bytes(&topology_bytes)?;
        if !request
            .topologies
            .iter()
            .any(|allowed| allowed == &topology)
        {
            return Err(corrupt_storage(
                "modeled projection observation escaped its topology predicate",
            ));
        }
        let topology_hash: Vec<u8> = row.try_get("topology_hash").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence topology hash", error)
        })?;
        verify_digest(
            &topology_hash,
            topology.digest(),
            "modeled projection evidence topology",
        )?;
        let partition_bytes: Vec<u8> = row.try_get("partition_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence partition bytes", error)
        })?;
        let partition = ProjectionPartition::new(partition_bytes)?;
        let partition_hash: Vec<u8> = row.try_get("partition_hash").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence partition hash", error)
        })?;
        verify_digest(
            &partition_hash,
            partition.digest(),
            "modeled projection evidence partition",
        )?;
        let model: String = row.try_get("model_name").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence model", error)
        })?;
        let key_bytes: Vec<u8> = row.try_get("canonical_key_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence key bytes", error)
        })?;
        let scope = ProjectionRecordScope::new(topology, partition, model, key_bytes)?;
        let key_hash: Vec<u8> = row.try_get("canonical_key_hash").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence key hash", error)
        })?;
        verify_digest(
            &key_hash,
            scope.key_digest(),
            "modeled projection evidence key",
        )?;
        let kind_value: String = row.try_get("scope_kind").map_err(|error| {
            protocol_storage_error::<DB>("decode modeled evidence observation kind", error)
        })?;
        let kind = decode_observation_kind(&kind_value)?;
        let partition_epoch = ProjectionEpoch::new(
            row.try_get::<String, _>("partition_change_epoch")
                .map_err(|error| {
                    protocol_storage_error::<DB>("decode modeled evidence partition epoch", error)
                })?,
        )?;
        let partition_head = from_i64::<DB>(
            row.try_get("partition_change_head").map_err(|error| {
                protocol_storage_error::<DB>("decode modeled evidence partition head", error)
            })?,
            "modeled projection evidence partition head",
        )?;
        let observation = decode_observation_row::<DB>(
            &row,
            &request.causation_id,
            &scope,
            kind,
            &partition_epoch,
        )?;
        if observation.change.position() > partition_head
            || observations.iter().any(|existing: &ProjectionObservation| {
                existing.kind == observation.kind && existing.scope == observation.scope
            })
        {
            return Err(corrupt_storage(
                "modeled projection observation is duplicated or exceeds its partition head",
            ));
        }
        observations.push(observation);
    }

    let mut failure_query = QueryBuilder::<DB>::new(
        "SELECT DISTINCT partition.topology_bytes, partition.topology_hash \
         FROM projection_failures failure \
         INNER JOIN projection_partitions partition \
         ON partition.topology_hash = failure.topology_hash \
         AND partition.partition_hash = failure.partition_hash \
         AND partition.stopped_failure_id = failure.failure_id \
         WHERE failure.causation_id = ",
    );
    failure_query.push_bind(request.causation_id.as_str());
    failure_query.push(" AND (");
    for (index, topology) in request.topologies.iter().enumerate() {
        if index > 0 {
            failure_query.push(" OR ");
        }
        let topology_hash = topology.digest();
        failure_query.push("failure.topology_hash = ");
        failure_query.push_bind(topology_hash.as_slice());
    }
    failure_query.push(")");
    failure_query.push(" ORDER BY partition.topology_hash LIMIT ");
    failure_query.push(overflow_limit.to_string());
    let rows = failure_query
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| {
            protocol_storage_error::<DB>(
                "read modeled projection causation terminal failures",
                error,
            )
        })?;
    if rows.len() > MAX_PROJECTION_EVIDENCE_BATCH_ITEMS {
        return Err(ProjectionProtocolError::InvalidBatch(format!(
            "projection causation has more than {} stopped topologies",
            MAX_PROJECTION_EVIDENCE_BATCH_ITEMS
        )));
    }
    let mut terminal_failure_topologies = Vec::with_capacity(rows.len());
    for row in rows {
        let topology_bytes: Vec<u8> = row.try_get("topology_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode stopped evidence topology bytes", error)
        })?;
        let topology = ProjectorTopologyId::from_canonical_bytes(&topology_bytes)?;
        if !request
            .topologies
            .iter()
            .any(|allowed| allowed == &topology)
        {
            return Err(corrupt_storage(
                "stopped modeled projection escaped its topology predicate",
            ));
        }
        let topology_hash: Vec<u8> = row.try_get("topology_hash").map_err(|error| {
            protocol_storage_error::<DB>("decode stopped evidence topology hash", error)
        })?;
        verify_digest(
            &topology_hash,
            topology.digest(),
            "stopped modeled projection topology",
        )?;
        if terminal_failure_topologies
            .iter()
            .any(|existing| existing == &topology)
        {
            return Err(corrupt_storage(
                "modeled projection causation returned duplicate stopped topologies",
            ));
        }
        terminal_failure_topologies.push(topology);
    }
    Ok(ProjectionCausationEvidenceBatch {
        observations,
        terminal_failure_topologies,
    })
}

/// Recover exact live record scopes for typed physical-row keys without
/// requiring the caller to know hidden projection partitions.
pub(crate) async fn read_projection_live_record_batch_in_executor<DB>(
    connection: &mut DB::Connection,
    request: &ProjectionLiveRecordBatchRequest,
) -> Result<ProjectionLiveRecordBatch, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    DB::Arguments: IntoArguments<DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    request.validate()?;
    if request.requests.is_empty() {
        return Ok(ProjectionLiveRecordBatch::default());
    }

    let mut registered = vec![false; request.requests.len()];
    let mut registration_query = QueryBuilder::<DB>::new(
        "SELECT topology_bytes, topology_hash, model_name, table_name \
         FROM projection_registered_models WHERE ",
    );
    for (index, probe) in request.requests.iter().enumerate() {
        if index > 0 {
            registration_query.push(" OR ");
        }
        let topology_hash = probe.topology.digest();
        registration_query.push("(topology_hash = ");
        registration_query.push_bind(topology_hash.as_slice());
        registration_query.push(" AND model_name = ");
        registration_query.push_bind(probe.model());
        registration_query.push(")");
    }
    let registration_rows = registration_query
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| {
            protocol_storage_error::<DB>("read projection live-record registrations", error)
        })?;
    for row in registration_rows {
        let topology_hash = decode_digest(
            row.try_get("topology_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record topology hash", error)
            })?,
            "projection live-record topology",
        )?;
        let model: String = row.try_get("model_name").map_err(|error| {
            protocol_storage_error::<DB>("decode live-record registered model", error)
        })?;
        let topology_bytes: Vec<u8> = row.try_get("topology_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode live-record topology bytes", error)
        })?;
        let table: String = row.try_get("table_name").map_err(|error| {
            protocol_storage_error::<DB>("decode live-record registered table", error)
        })?;
        let matching = request
            .requests
            .iter()
            .enumerate()
            .filter(|(_, probe)| probe.topology.digest() == topology_hash && probe.model() == model)
            .map(|(index, _)| index)
            .collect::<Vec<_>>();
        if matching.is_empty() {
            return Err(corrupt_storage(
                "projection registration escaped its bounded live-record predicate",
            ));
        }
        for index in matching {
            let probe = &request.requests[index];
            verify_bytes(
                &topology_bytes,
                &probe.topology.canonical_bytes(),
                "projection live-record topology",
            )?;
            if table != probe.schema.table_name {
                return Err(ProjectionProtocolError::InvalidBatch(format!(
                    "projection live-record model `{}` is registered to table `{table}`, not `{}`",
                    probe.model(),
                    probe.schema.table_name
                )));
            }
            if std::mem::replace(&mut registered[index], true) {
                return Err(corrupt_storage(
                    "projection live-record model has duplicate registrations",
                ));
            }
        }
    }
    for (index, is_registered) in registered.into_iter().enumerate() {
        if !is_registered {
            return Err(ProjectionProtocolError::InvalidBatch(format!(
                "projection live-record model `{}` has no registered topology owner",
                request.requests[index].model()
            )));
        }
    }

    let mut record_query = QueryBuilder::<DB>::new(
        "SELECT record.topology_hash AS live_topology_hash, record.partition_hash AS \
         live_partition_hash, record.model_name AS live_model_name, \
         record.canonical_key_bytes, record.canonical_key_hash, record.incarnation, \
         record.revision, record.tombstone, record.change_epoch, record.change_position, \
         partition.topology_bytes AS live_topology_bytes, \
         partition.partition_bytes AS live_partition_bytes, \
         partition.change_epoch AS live_partition_epoch, \
         partition.change_head AS live_partition_head \
         FROM projection_records AS record \
         INNER JOIN projection_partitions AS partition \
         ON partition.topology_hash = record.topology_hash \
         AND partition.partition_hash = record.partition_hash \
         WHERE record.tombstone = 0 AND (",
    );
    for (index, probe) in request.requests.iter().enumerate() {
        if index > 0 {
            record_query.push(" OR ");
        }
        let topology_hash = probe.topology.digest();
        record_query.push("(record.topology_hash = ");
        record_query.push_bind(topology_hash.as_slice());
        record_query.push(" AND record.model_name = ");
        record_query.push_bind(probe.model());
        record_query.push(" AND record.canonical_key_hash = ");
        record_query.push_bind(probe.canonical_key_hash.as_slice());
        record_query.push(")");
    }
    record_query.push(")");
    let rows = record_query
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| {
            protocol_storage_error::<DB>("read projection live-record evidence", error)
        })?;
    let mut records = vec![None; request.requests.len()];
    for row in rows {
        let topology_hash = decode_digest(
            row.try_get("live_topology_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record topology hash", error)
            })?,
            "projection live-record topology",
        )?;
        let model: String = row
            .try_get("live_model_name")
            .map_err(|error| protocol_storage_error::<DB>("decode live-record model", error))?;
        let key_hash = decode_digest(
            row.try_get("canonical_key_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record key hash", error)
            })?,
            "projection live-record key",
        )?;
        let key_bytes: Vec<u8> = row
            .try_get("canonical_key_bytes")
            .map_err(|error| protocol_storage_error::<DB>("decode live-record key bytes", error))?;
        let digest_candidates = request
            .requests
            .iter()
            .enumerate()
            .filter(|(_, probe)| {
                probe.topology.digest() == topology_hash
                    && probe.model() == model
                    && probe.canonical_key_hash == key_hash
            })
            .collect::<Vec<_>>();
        let exact = digest_candidates
            .iter()
            .filter(|(_, probe)| probe.canonical_key_bytes == key_bytes)
            .map(|(index, _)| *index)
            .collect::<Vec<_>>();
        let [index] = exact.as_slice() else {
            return Err(corrupt_storage(if digest_candidates.is_empty() {
                "projection record escaped its bounded live-record predicate".to_string()
            } else {
                "projection live-record canonical key does not match its digest lookup".to_string()
            }));
        };
        let probe = &request.requests[*index];
        let topology_bytes: Vec<u8> = row.try_get("live_topology_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode live-record topology bytes", error)
        })?;
        verify_bytes(
            &topology_bytes,
            &probe.topology.canonical_bytes(),
            "projection live-record topology",
        )?;
        let partition_bytes: Vec<u8> = row.try_get("live_partition_bytes").map_err(|error| {
            protocol_storage_error::<DB>("decode live-record partition bytes", error)
        })?;
        let partition = ProjectionPartition::new(partition_bytes)?;
        let stored_partition_hash: Vec<u8> =
            row.try_get("live_partition_hash").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record partition hash", error)
            })?;
        verify_digest(
            &stored_partition_hash,
            partition.digest(),
            "projection live-record partition",
        )?;
        let scope = ProjectionRecordScope::new(
            probe.topology.clone(),
            partition,
            probe.model().to_string(),
            key_bytes,
        )?;
        verify_digest(&key_hash, scope.key_digest(), "projection live-record key")?;
        let incarnation = from_i64::<DB>(
            row.try_get("incarnation").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record incarnation", error)
            })?,
            "projection live-record incarnation",
        )?;
        let revision = from_i64::<DB>(
            row.try_get("revision").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record revision", error)
            })?,
            "projection live-record revision",
        )?;
        let tombstone: i64 = row
            .try_get("tombstone")
            .map_err(|error| protocol_storage_error::<DB>("decode live-record tombstone", error))?;
        if tombstone != 0 {
            return Err(corrupt_storage(
                "projection live-record lookup returned a tombstone",
            ));
        }
        let partition_epoch =
            ProjectionEpoch::new(row.try_get::<String, _>("live_partition_epoch").map_err(
                |error| protocol_storage_error::<DB>("decode live-record partition epoch", error),
            )?)?;
        let change_epoch =
            ProjectionEpoch::new(row.try_get::<String, _>("change_epoch").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record change epoch", error)
            })?)?;
        if change_epoch != partition_epoch {
            return Err(corrupt_storage(
                "projection live-record change epoch differs from its partition",
            ));
        }
        let change_position = from_i64::<DB>(
            row.try_get("change_position").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record change position", error)
            })?,
            "projection live-record change position",
        )?;
        let partition_head = from_i64::<DB>(
            row.try_get("live_partition_head").map_err(|error| {
                protocol_storage_error::<DB>("decode live-record partition head", error)
            })?,
            "projection live-record partition head",
        )?;
        if change_position > partition_head {
            return Err(corrupt_storage(
                "projection live-record change exceeds its partition head",
            ));
        }
        let metadata = ProjectionRecordMetadata {
            revision: RecordRevision::new(scope.clone(), incarnation, revision)?,
            tombstone: false,
            change: ProjectionChangeCursor::new(
                scope.topology().clone(),
                scope.projection_partition().clone(),
                change_epoch,
                change_position,
            )?,
        };
        if records[*index].replace(metadata).is_some() {
            return Err(corrupt_storage(format!(
                "projection live-record identity for model `{}` is ambiguous across partitions",
                probe.model()
            )));
        }
    }
    Ok(ProjectionLiveRecordBatch { records })
}

/// Boxed operation run inside one framework-owned projection read snapshot.
///
/// The boxed lifetime prevents callers from leaking the borrowed connection
/// beyond the transaction boundary.
pub(crate) type ProjectionReadSnapshotFuture<'a, T> =
    Pin<Box<dyn Future<Output = Result<T, ProjectionProtocolError>> + Send + 'a>>;

/// Run a physical query plan and all of its causal-evidence probes in one
/// repeatable database snapshot.
///
/// PostgreSQL uses `REPEATABLE READ READ ONLY`; its default READ COMMITTED
/// isolation would let each metadata statement observe a newer commit than the
/// physical GraphQL statement. SQLite holds one ordinary read transaction,
/// whose first read establishes the snapshot for the remaining plan.
pub(crate) async fn with_projection_read_snapshot<DB, T, F>(
    pool: &Pool<DB>,
    operation: F,
) -> Result<T, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    T: Send,
    F: for<'connection> FnOnce(
            &'connection mut DB::Connection,
        ) -> ProjectionReadSnapshotFuture<'connection, T>
        + Send,
    DB::Arguments: IntoArguments<DB>,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
{
    let mut tx = pool
        .begin()
        .await
        .map_err(|error| protocol_storage_error::<DB>("begin projection read snapshot", error))?;
    if DB::BACKEND == "postgres" {
        sqlx::query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY")
            .execute(&mut *tx)
            .await
            .map_err(|error| {
                protocol_storage_error::<DB>("configure projection read snapshot", error)
            })?;
    }

    let result = operation(&mut *tx).await;
    match result {
        Ok(value) => {
            tx.commit().await.map_err(|error| {
                protocol_storage_error::<DB>("commit projection read snapshot", error)
            })?;
            Ok(value)
        }
        Err(error) => {
            tx.rollback().await.map_err(|rollback_error| {
                protocol_storage_error::<DB>(
                    "roll back failed projection read snapshot",
                    rollback_error,
                )
            })?;
            Err(error)
        }
    }
}

pub(super) async fn read_projection_changes_in_executor_after_state<DB, AfterState>(
    connection: &mut DB::Connection,
    topology: &ProjectorTopologyId,
    partition: &ProjectionPartition,
    after: Option<&ProjectionChangeCursor>,
    limit: usize,
    after_state: AfterState,
) -> Result<ProjectionChangeRead, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    AfterState: Future<Output = ()> + Send,
    DB::Arguments: IntoArguments<DB>,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    let state = load_partition_in_connection(connection, topology, partition).await?;
    after_state.await;
    let Some(state) = state else {
        return Ok(match after {
            Some(_) => ProjectionChangeRead::ResetRequired {
                head: None,
                compacted_through: 0,
            },
            None => ProjectionChangeRead::Changes {
                head: None,
                compacted_through: 0,
                changes: Vec::new(),
            },
        });
    };
    let head = if state.change_head == 0 {
        None
    } else {
        Some(ProjectionChangeCursor::new(
            topology.clone(),
            partition.clone(),
            state.change_epoch.clone(),
            state.change_head,
        )?)
    };
    if after.is_none() && state.compacted_through > 0 {
        return Ok(ProjectionChangeRead::ResetRequired {
            head,
            compacted_through: state.compacted_through,
        });
    }
    let start = match after {
        Some(cursor)
            if cursor.topology() != topology
                || cursor.projection_partition() != partition
                || cursor.epoch() != &state.change_epoch
                || cursor.position() > state.change_head
                || cursor.position() < state.compacted_through =>
        {
            return Ok(ProjectionChangeRead::ResetRequired {
                head,
                compacted_through: state.compacted_through,
            });
        }
        Some(cursor) => cursor.position(),
        None => state.compacted_through,
    };
    if limit == 0 || start == state.change_head {
        return Ok(ProjectionChangeRead::Changes {
            head,
            compacted_through: state.compacted_through,
            changes: Vec::new(),
        });
    }
    let topology_hash = topology.digest();
    let partition_hash = partition.digest();
    let mut builder = QueryBuilder::<DB>::new(
        "SELECT change_epoch, change_position, change_kind, causation_id, model_name, \
         scope_kind, canonical_key_bytes, canonical_key_hash, incarnation, revision, \
         failure_id FROM projection_changes WHERE topology_hash = ",
    );
    builder.push_bind(topology_hash.as_slice());
    builder.push(" AND partition_hash = ");
    builder.push_bind(partition_hash.as_slice());
    builder.push(" AND change_epoch = ");
    builder.push_bind(state.change_epoch.as_str());
    builder.push(" AND change_position > ");
    builder.push_bind(to_i64::<DB>(start, "projection change read position")?);
    builder.push(" ORDER BY change_position ASC LIMIT ");
    builder.push_bind(i64::try_from(limit).unwrap_or(i64::MAX));
    let rows = builder
        .build()
        .fetch_all(&mut *connection)
        .await
        .map_err(|error| protocol_storage_error::<DB>("read projection changes", error))?;
    let mut changes = Vec::with_capacity(rows.len());
    let mut expected = checked_next(start, "projection change read")?;
    for row in rows {
        let change = decode_change_row::<DB>(&row, topology, partition, &state.change_epoch)?;
        if change.cursor.position() != expected {
            return Err(corrupt_storage(format!(
                "projection change log expected position {expected} but found {}",
                change.cursor.position()
            )));
        }
        expected = if change.cursor.position() == state.change_head {
            state.change_head
        } else {
            checked_next(change.cursor.position(), "projection change read")?
        };
        changes.push(change);
    }
    if changes.is_empty() {
        return Err(corrupt_storage(format!(
            "projection change log is missing retained position {}",
            checked_next(start, "projection change read")?
        )));
    }
    Ok(ProjectionChangeRead::Changes {
        head,
        compacted_through: state.compacted_through,
        changes,
    })
}

/// Read one durable resumable projection-change page using an existing
/// database executor and therefore the caller's already-established snapshot.
pub(crate) async fn read_projection_changes_in_executor<DB>(
    connection: &mut DB::Connection,
    topology: &ProjectorTopologyId,
    partition: &ProjectionPartition,
    after: Option<&ProjectionChangeCursor>,
    limit: usize,
) -> Result<ProjectionChangeRead, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    DB::Arguments: IntoArguments<DB>,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    read_projection_changes_in_executor_after_state::<DB, _>(
        connection,
        topology,
        partition,
        after,
        limit,
        std::future::ready(()),
    )
    .await
}

/// Read one resumable projection-change page from a new database snapshot.
///
/// `after_state` is normally an immediately-ready future. Tests use it to
/// commit compaction after the partition watermark has been observed but
/// before retained rows are read, proving both statements remain one view.
#[allow(dead_code)]
pub(super) async fn read_projection_changes_in_snapshot<DB, AfterState>(
    pool: &Pool<DB>,
    topology: ProjectorTopologyId,
    partition: ProjectionPartition,
    after: Option<ProjectionChangeCursor>,
    limit: usize,
    after_state: AfterState,
) -> Result<ProjectionChangeRead, ProjectionProtocolError>
where
    DB: SqlxRepoBackend,
    AfterState: Future<Output = ()> + Send + 'static,
    DB::Arguments: IntoArguments<DB>,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
    for<'q> i64: Encode<'q, DB> + Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> String: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> Vec<u8>: Type<DB> + sqlx::Decode<'q, DB>,
    for<'q> &'q str: Encode<'q, DB> + Type<DB>,
    for<'q> &'q [u8]: Encode<'q, DB> + Type<DB>,
    for<'r> &'r str: sqlx::ColumnIndex<DB::Row>,
{
    with_projection_read_snapshot(pool, move |connection| {
        Box::pin(async move {
            read_projection_changes_in_executor_after_state::<DB, _>(
                connection,
                &topology,
                &partition,
                after.as_ref(),
                limit,
                after_state,
            )
            .await
        })
    })
    .await
}