axond 0.3.18

Axond — a stateless, single-binary, self-hosted AI gateway: one place for provider keys, model routing, usage, and telemetry.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
//! The durable control plane: PostgreSQL.
//!
//! The one thing this module has to get right is that a publication is a single
//! transaction. The head pointer is read `FOR UPDATE`, so concurrent publishers
//! serialize on one row rather than racing to append; the manifest, the resource
//! versions, the blob references, the mutation, the audit event, the idempotency
//! record, and the head advancement are written in that transaction and become
//! visible together. Anything that fails rolls the whole thing back, which is why
//! a rejected publication leaves no resource version and no audit event — not
//! "leaves them to be cleaned up".
//!
//! What it deliberately does not do:
//!
//! - **It is never on the inference path.** Nothing here is called while a
//!   request is in flight, so a Postgres outage stalls administration and
//!   convergence while replicas keep serving the immutable snapshot they already
//!   hold. That is why the failure of every method is an error returned to an
//!   administrative caller and never a mutation of anything a replica is serving.
//! - **It does not compile or publish a snapshot.** Hydration into runtime state
//!   is #166, publication to replicas is #142. This is the seam they read from:
//!   [`ControlPlaneStore::load_revision`] returns a [`LoadedRevision`], which
//!   cannot exist unless it verified.
//! - **It does not store payload bytes.** A blob is a reference — kind, digest,
//!   size — because a revision that inlined megabytes would duplicate them per
//!   revision. Where the payload lives, and who fetches and verifies it, belongs
//!   with hydration.
//! - **It does not store secret material.** A credential resource's body holds an
//!   opaque secret *reference*; plaintext lives in the secret store, and no value
//!   from a body is ever logged.

use std::future::Future;
use std::pin::Pin;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use tokio_postgres::error::SqlState;
use tokio_postgres::types::ToSql;
use tokio_postgres::{Client, Config, Row, Transaction};

use super::hydration::{self, HydrationLimits};
use super::rows;
use super::schema::{self, MINIMUM_SERVER_VERSION_NUM, SchemaStatus};
use super::{ControlPlaneError, ControlPlaneStore};
use crate::backends::{Capabilities, Capability};
use crate::desired_state::{
    AuditEvent, IntegrityError, LoadedRevision, Mutation, ResourceRef, ResourceVersion,
    ResourceVersionNumber, RevisionCandidate, RevisionId, RevisionManifest, SerializerVersion,
    Uuid7Generator,
};

const BACKEND: &str = "postgres";

/// How the store connects, and what it is allowed to do at boot.
#[derive(Debug, Clone)]
pub struct ControlPlaneSettings {
    /// The PostgreSQL schema the journal lives in, if not the connection's
    /// default. Validated as an identifier, because it is interpolated into
    /// `SET search_path`.
    pub schema: Option<String>,
    /// Whether boot may apply pending migrations.
    ///
    /// An operator who applies DDL out of band sets this to `false` and gets a
    /// refusal instead of a schema change; boot still *checks*, because serving
    /// against a schema that is not the one this build writes is the failure this
    /// setting exists to prevent.
    pub migrate: bool,
    pub connect_timeout: Duration,
    /// The ceiling on one control-plane operation. Generous by inference-path
    /// standards: nothing here is called with a request in flight.
    pub operation_timeout: Duration,
    /// How long an idempotency record is honoured. A retry window, not a
    /// permanent namespace: expiry never touches the revision or audit trail the
    /// record points at.
    pub idempotency_retention: Duration,
    /// What one read of a stored revision may consume. Reads are bounded because
    /// what they read is storage, and storage is not this build's to trust.
    pub hydration: HydrationLimits,
}

impl ControlPlaneSettings {
    /// The settings a `[control_plane]` section asks for.
    ///
    /// Every field the section can express is mapped here, so a setting an
    /// operator writes cannot be silently ignored by a code path that built its
    /// own defaults instead. The fields it cannot express — the idempotency
    /// retention window and the hydration limits — are runtime serving
    /// parameters rather than connection or schema ones, and keep their defaults.
    pub fn from_config(control_plane: &crate::config::ControlPlane) -> Self {
        Self {
            schema: control_plane
                .schema
                .as_deref()
                .map(str::trim)
                .filter(|schema| !schema.is_empty())
                .map(str::to_owned),
            migrate: control_plane.migrate,
            connect_timeout: Duration::from_millis(control_plane.connect_timeout_ms),
            operation_timeout: Duration::from_millis(control_plane.operation_timeout_ms),
            ..Self::default()
        }
    }

    /// The settings a maintenance command runs with: the operator's connection
    /// and schema, and never boot's permission to migrate. `axond migrate apply`
    /// is the mutation, and it is explicit; nothing else may become one because
    /// the config happened to allow boot migrations.
    pub fn for_maintenance(control_plane: &crate::config::ControlPlane) -> Self {
        Self {
            migrate: false,
            ..Self::from_config(control_plane)
        }
    }
}

impl Default for ControlPlaneSettings {
    fn default() -> Self {
        Self {
            schema: None,
            migrate: true,
            connect_timeout: Duration::from_secs(10),
            operation_timeout: Duration::from_secs(30),
            idempotency_retention: Duration::from_secs(24 * 60 * 60),
            hydration: HydrationLimits::default(),
        }
    }
}

/// A [`ControlPlaneStore`] backed by the revision journal in `ops/postgres/`.
pub struct PostgresControlPlane {
    config: Config,
    settings: ControlPlaneSettings,
    /// Set on every connection, including reconnections: a reconnect that landed
    /// on the default schema would silently read a different journal.
    search_path: Option<String>,
    ids: Uuid7Generator,
    client: tokio::sync::Mutex<Option<Client>>,
}

/// Written by hand, and deliberately narrow: a derived one would print the
/// [`Config`], which carries the password from the DSN.
impl std::fmt::Debug for PostgresControlPlane {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PostgresControlPlane")
            .field("schema", &self.search_path)
            .field("migrate", &self.settings.migrate)
            .finish_non_exhaustive()
    }
}

impl PostgresControlPlane {
    /// Connect, check the server and schema, and optionally migrate.
    ///
    /// Boot refuses rather than degrades. An unsupported server, a schema a newer
    /// build owns, a migration that was edited after being applied, or a pending
    /// migration this store may not apply are all [`ControlPlaneError::Denied`]:
    /// they need an operator, and retrying cannot help.
    pub async fn connect(
        dsn: &str,
        settings: ControlPlaneSettings,
    ) -> Result<Self, ControlPlaneError> {
        let (store, mut client) = Self::open(dsn, settings).await?;
        store.prepare_schema(&mut client).await?;
        *store.client.lock().await = Some(client);
        Ok(store)
    }

    /// Connect for schema maintenance, without deciding anything about the schema.
    ///
    /// [`connect`](Self::connect) is a boot: it refuses a schema it cannot serve.
    /// A `status` command has to *report* that schema instead, and an `apply` has
    /// to be able to run against the database a boot just refused, so this checks
    /// the server version — a statement no migration can survive an older server
    /// running — and stops there. The store it returns is not for serving: it has
    /// not established that the journal is the one this build writes.
    pub async fn connect_for_maintenance(
        dsn: &str,
        settings: ControlPlaneSettings,
    ) -> Result<Self, ControlPlaneError> {
        let (store, client) = Self::open(dsn, settings).await?;
        *store.client.lock().await = Some(client);
        Ok(store)
    }

    /// Connect and check the server, leaving the schema decision to the caller.
    async fn open(
        dsn: &str,
        settings: ControlPlaneSettings,
    ) -> Result<(Self, Client), ControlPlaneError> {
        let mut config: Config = dsn.parse().map_err(|error| {
            denied(format!(
                // The DSN itself is never echoed: it carries a password.
                "the control-plane DSN could not be parsed: {error}"
            ))
        })?;
        config.connect_timeout(settings.connect_timeout);
        config.application_name(crate::telemetry::SERVICE_NAME);
        let search_path = settings
            .schema
            .as_deref()
            .map(|schema| {
                crate::usage::validate_table_name(schema).map_err(denied)?;
                // The table-name validator allows one qualifying dot; a search path
                // takes one unqualified schema. Checked here as well as in config
                // validation, because settings can be built without a config file.
                if schema.contains('.') {
                    return Err(denied(format!(
                        "`{schema}` is not a single unqualified schema name"
                    )));
                }
                Ok(schema.to_owned())
            })
            .transpose()?;

        let store = Self {
            config,
            settings,
            search_path,
            ids: Uuid7Generator::new(),
            client: tokio::sync::Mutex::new(None),
        };
        let client = tokio::time::timeout(store.settings.connect_timeout, store.connect_client())
            .await
            .map_err(|_| ControlPlaneError::Unavailable {
                backend: BACKEND,
                message: "connection timed out".to_owned(),
            })?
            .map_err(|error| unavailable("connect", &error))?;
        store.check_server_version(&client).await?;
        Ok((store, client))
    }

    /// Apply every migration the journal is missing, and report which ones.
    ///
    /// Forward-only and idempotent: a journal that is already current is left
    /// untouched and reports nothing applied, so re-running an `apply` is not a
    /// second migration. A journal this build cannot own — [`SchemaStatus::Ahead`]
    /// or [`SchemaStatus::Drifted`] — is refused with [`ControlPlaneError::Denied`]
    /// rather than written to, because writing more DDL over it makes the
    /// disagreement worse rather than better.
    ///
    /// The whole read-and-apply is one transaction under the same advisory lock
    /// boot takes, so this is safe to run while replicas are starting.
    pub async fn apply_migrations(&self) -> Result<Vec<i32>, ControlPlaneError> {
        self.run(|client| {
            Box::pin(async move {
                // Read committed explicitly rather than by inheritance: the status
                // is read *after* the lock is granted, and it has to be the winner's
                // committed history rather than a snapshot taken before the wait. A
                // server with `default_transaction_isolation = 'repeatable read'`
                // would freeze the snapshot at the first statement, so the loser of
                // the lock would re-read a pre-migration ledger and apply the same
                // files again.
                let transaction = client
                    .build_transaction()
                    .isolation_level(tokio_postgres::IsolationLevel::ReadCommitted)
                    .start()
                    .await
                    .map_err(|error| unavailable("begin schema transaction", &error))?;
                transaction
                    .query_one("SELECT pg_advisory_xact_lock($1::bigint)", &[&SCHEMA_LOCK])
                    .await
                    .map_err(|error| unavailable("acquire schema lock", &error))?;
                let status = schema::status(&transaction)
                    .await
                    .map_err(|error| unavailable("read schema status", &error))?;
                // A database that is already current is left alone rather than
                // handed to the runner: `migrate` starts from the recorded prefix
                // of a `Behind` status, so a `Current` one would re-run every
                // shipped file from v1. That happens to be harmless while every
                // statement is `IF NOT EXISTS`, and would not survive the first
                // `ALTER TABLE` or backfill. Returning here also rolls the
                // transaction back, so a repeated apply writes nothing at all.
                if status.is_current() {
                    return Ok(Vec::new());
                }
                if !status.is_migratable() {
                    return Err(denied(status.to_string()));
                }
                let pending = schema::pending(&status);
                schema::migrate(&transaction, &status)
                    .await
                    .map_err(|error| migration_refused_or_unavailable(&error))?;
                let migrated = schema::status(&transaction)
                    .await
                    .map_err(|error| unavailable("re-read schema status", &error))?;
                if !migrated.is_current() {
                    return Err(denied(format!(
                        "migrations were applied but the schema is still not current: {migrated}"
                    )));
                }
                transaction
                    .commit()
                    .await
                    .map_err(|error| unavailable("commit schema transaction", &error))?;
                Ok(pending)
            })
        })
        .await
    }

    /// The schema state a database is in, for a status command or a boot refusal.
    ///
    /// Read-only as the *database* enforces it, not just by convention: the
    /// transaction is opened `READ ONLY`, so a statement that tried to write
    /// would be rejected by the server rather than by this code being careful.
    /// A status command an operator runs against production has to be a thing
    /// that cannot change production.
    pub async fn schema_status(&self) -> Result<SchemaStatus, ControlPlaneError> {
        self.run(|client| {
            Box::pin(async move {
                let transaction = client
                    .build_transaction()
                    .read_only(true)
                    .start()
                    .await
                    .map_err(|error| unavailable("begin read-only schema read", &error))?;
                let status = schema::status(&transaction)
                    .await
                    .map_err(|error| unavailable("read schema status", &error))?;
                // Rolled back rather than committed: nothing here is a change,
                // so nothing here needs to be kept.
                let _ = transaction.rollback().await;
                Ok(status)
            })
        })
        .await
    }

    async fn check_server_version(&self, client: &Client) -> Result<(), ControlPlaneError> {
        let reported: String = client
            .query_one("SELECT current_setting('server_version_num')", &[])
            .await
            .map_err(|error| unavailable("read server version", &error))?
            .get(0);
        let version: i32 = reported.parse().map_err(|_| {
            denied(format!(
                "the server reported version `{reported}`, which is not a number"
            ))
        })?;
        if version < MINIMUM_SERVER_VERSION_NUM {
            return Err(denied(format!(
                "the control-plane journal requires PostgreSQL {}, but the server is {}",
                MINIMUM_SERVER_VERSION_NUM / 10_000,
                version / 10_000
            )));
        }
        Ok(())
    }

    /// Bring the schema to the version this build requires, or refuse.
    ///
    /// The whole check-and-migrate runs under one advisory lock inside one
    /// transaction, so two gateways booting against the same empty database
    /// serialize here instead of both running the DDL and one of them failing
    /// halfway.
    async fn prepare_schema(&self, client: &mut Client) -> Result<(), ControlPlaneError> {
        // Read committed explicitly, for the reason `apply_migrations` pins it: the
        // status is read after the lock is granted and has to be the winner's
        // committed history. Under `repeatable read` the waiting replica would read
        // the pre-migration ledger — or, because catalog lookups stay fresh while row
        // reads do not, an existing table with no visible rows, which is a refusal.
        let transaction = client
            .build_transaction()
            .isolation_level(tokio_postgres::IsolationLevel::ReadCommitted)
            .start()
            .await
            .map_err(|error| unavailable("begin schema transaction", &error))?;
        transaction
            .query_one("SELECT pg_advisory_xact_lock($1::bigint)", &[&SCHEMA_LOCK])
            .await
            .map_err(|error| unavailable("acquire schema lock", &error))?;
        let status = schema::status(&transaction)
            .await
            .map_err(|error| unavailable("read schema status", &error))?;
        if !status.is_current() {
            if !status.is_migratable() {
                return Err(denied(status.to_string()));
            }
            if !self.settings.migrate {
                return Err(denied(format!(
                    "{status}, and this store is configured not to migrate"
                )));
            }
            schema::migrate(&transaction, &status)
                .await
                .map_err(|error| migration_refused_or_unavailable(&error))?;
            let migrated = schema::status(&transaction)
                .await
                .map_err(|error| unavailable("re-read schema status", &error))?;
            if !migrated.is_current() {
                return Err(denied(format!(
                    "migrations were applied but the schema is still not current: {migrated}"
                )));
            }
        }
        transaction
            .commit()
            .await
            .map_err(|error| unavailable("commit schema transaction", &error))?;
        Ok(())
    }

    async fn connect_client(&self) -> Result<Client, tokio_postgres::Error> {
        let (client, connection) = self.config.connect(crate::usage::tls_connector()).await?;
        tokio::spawn(async move {
            if let Err(error) = connection.await {
                tracing::warn!(%error, "postgres control-plane connection closed");
            }
        });
        if let Some(schema) = &self.search_path {
            // Validated as an identifier at construction, so this is not an
            // injection point; there is no parameter form of `SET`.
            client
                .batch_execute(&format!("SET search_path TO {schema}"))
                .await?;
        }
        Ok(client)
    }

    /// Run one operation on a connected client, reconnecting a dead connection
    /// and dropping one an outage broke.
    ///
    /// A failure that is *not* an outage — a conflict, an invalid candidate,
    /// unreadable storage — keeps the connection: it says nothing about the
    /// connection's health, and discarding it would turn every refused write into
    /// a reconnect.
    async fn run<T>(
        &self,
        operation: impl for<'a> FnOnce(
            &'a mut Client,
        ) -> Pin<
            Box<dyn Future<Output = Result<T, ControlPlaneError>> + Send + 'a>,
        >,
    ) -> Result<T, ControlPlaneError> {
        let mut guard = self.client.lock().await;
        if guard.as_ref().is_none_or(Client::is_closed) {
            *guard = Some(
                self.connect_client()
                    .await
                    .map_err(|error| unavailable("reconnect", &error))?,
            );
        }
        let result = tokio::time::timeout(
            self.settings.operation_timeout,
            operation(guard.as_mut().expect("connected")),
        )
        .await
        .map_err(|_| ControlPlaneError::Unavailable {
            backend: BACKEND,
            message: "operation timed out".to_owned(),
        })
        .and_then(|result| result);
        if matches!(result, Err(ControlPlaneError::Unavailable { .. })) {
            *guard = None;
        }
        result
    }
}

/// The advisory lock every gateway takes before touching the journal's schema.
/// A constant, because the journal's object names are constants.
const SCHEMA_LOCK: i64 = 0x1a20_de5c_0de5_1a11u64 as i64;

fn denied(message: impl Into<String>) -> ControlPlaneError {
    ControlPlaneError::Denied {
        backend: BACKEND,
        message: message.into(),
    }
}

/// Classify a failure to run the migration DDL by whether a retry could clear it.
///
/// The realistic failures here are the server rejecting a statement, not the
/// server being unreachable: `3F000` when the configured `[control_plane] schema`
/// does not exist (`SET search_path` accepts a missing schema, so the refusal
/// arrives at the first `CREATE TABLE`) and `42501` when the role may not create
/// objects. Both need an operator, and reporting them as an outage would tell a
/// rollout gate to retry a thing that cannot start working.
///
/// The transient classes stay outages so they keep their retryable
/// classification: connection exceptions (08), transaction rollbacks including
/// serialization failures and deadlocks (40), insufficient resources (53), an
/// object not in a prerequisite state such as an unavailable lock (55), operator
/// intervention including a cancelled statement or a shutdown (57), and system
/// errors (58). An error with no SQLSTATE never reached the server at all.
fn migration_refused_or_unavailable(error: &tokio_postgres::Error) -> ControlPlaneError {
    const TRANSIENT: [&str; 6] = ["08", "40", "53", "55", "57", "58"];
    let Some(db) = error.as_db_error() else {
        return unavailable("apply migrations", error);
    };
    let code = db.code().code();
    if code
        .get(..2)
        .is_some_and(|class| TRANSIENT.contains(&class))
    {
        return unavailable("apply migrations", error);
    }
    denied(format!(
        "applying migrations failed: {} (SQLSTATE {code}); the server rejected the statement, so \
         no retry clears it — check that the configured schema exists and that the role may \
         create objects in it",
        db.message()
    ))
}

/// Report a Postgres failure as an outage, naming the operation and SQLSTATE.
///
/// SQLSTATE is included because "connection reset" and "deadlock detected" are
/// the same category to a caller and completely different to an operator.
pub(super) fn unavailable(operation: &str, error: &tokio_postgres::Error) -> ControlPlaneError {
    let message = match error.as_db_error() {
        Some(db) => format!(
            "{operation} failed: {} (SQLSTATE {})",
            db.message(),
            db.code().code()
        ),
        None => format!("{operation} failed: {error}"),
    };
    ControlPlaneError::Unavailable {
        backend: BACKEND,
        message,
    }
}

pub(super) fn corrupt_storage(detail: impl Into<String>) -> ControlPlaneError {
    ControlPlaneError::CorruptStorage {
        detail: detail.into(),
    }
}

/// A timestamp the journal can store and read back unchanged.
///
/// `timestamptz` holds microseconds, so a nanosecond-precision instant would come
/// back a different value than it went in — and a manifest that does not equal the
/// one publication returned is indistinguishable from a corrupt one.
fn journal_now() -> SystemTime {
    let now = SystemTime::now();
    match now.duration_since(UNIX_EPOCH) {
        Ok(since) => UNIX_EPOCH + Duration::from_micros(since.as_micros() as u64),
        Err(_) => now,
    }
}

fn version_text(value: ResourceVersionNumber) -> i64 {
    // A version number is a small counter; the cast cannot lose a real one.
    i64::try_from(value.get()).unwrap_or(i64::MAX)
}

fn size_bytes(value: u64) -> i64 {
    i64::try_from(value).unwrap_or(i64::MAX)
}

#[async_trait]
impl ControlPlaneStore for PostgresControlPlane {
    fn name(&self) -> &'static str {
        BACKEND
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::new(&[
            Capability::TransactionalWrites,
            Capability::OptimisticConcurrency,
            Capability::IdempotentWrites,
            Capability::TransactionalAudit,
        ])
    }

    async fn health(&self) -> Result<(), ControlPlaneError> {
        self.run(|client| {
            Box::pin(async move {
                client
                    .query_one("SELECT 1", &[])
                    .await
                    .map(|_| ())
                    .map_err(|error| unavailable("health check", &error))
            })
        })
        .await
    }

    async fn desired_revision(&self) -> Result<Option<RevisionId>, ControlPlaneError> {
        self.run(|client| {
            Box::pin(async move {
                let row = client
                    .query_opt(
                        "SELECT revision_id FROM axond_cp_head WHERE singleton",
                        &[],
                    )
                    .await
                    .map_err(|error| unavailable("read desired revision", &error))?;
                let Some(row) = row else {
                    return Err(corrupt_storage(
                        "the control-plane head row is missing; the schema was modified out of band",
                    ));
                };
                let id: Option<String> = row.get(0);
                id.map(|text| {
                    rows::revision_id(&text).map_err(|error| {
                        corrupt_storage(format!("the desired revision is unreadable: {error}"))
                    })
                })
                .transpose()
            })
        })
        .await
    }

    async fn load_manifest(&self, id: RevisionId) -> Result<RevisionManifest, ControlPlaneError> {
        let limits = self.settings.hydration;
        self.run(move |client| {
            Box::pin(async move {
                let transaction = client
                    .transaction()
                    .await
                    .map_err(|error| unavailable("begin manifest read", &error))?;
                let manifest = hydration::manifest(&transaction, id, &limits).await;
                let _ = transaction.rollback().await;
                manifest
            })
        })
        .await
    }

    async fn load_revision(&self, id: RevisionId) -> Result<LoadedRevision, ControlPlaneError> {
        let limits = self.settings.hydration;
        self.run(move |client| {
            Box::pin(async move {
                let transaction = client
                    .transaction()
                    .await
                    .map_err(|error| unavailable("begin revision read", &error))?;
                let loaded = hydration::revision(&transaction, id, &limits).await;
                let _ = transaction.rollback().await;
                loaded
            })
        })
        .await
    }

    /// The head and its hydration in one read, so a caller cannot be told it
    /// converged onto a revision that was never the head it read.
    async fn load_desired_revision(&self) -> Result<Option<LoadedRevision>, ControlPlaneError> {
        let limits = self.settings.hydration;
        self.run(move |client| {
            Box::pin(async move {
                let transaction = client
                    .transaction()
                    .await
                    .map_err(|error| unavailable("begin desired-revision read", &error))?;
                let loaded = hydration::desired(&transaction, &limits).await;
                let _ = transaction.rollback().await;
                loaded
            })
        })
        .await
    }

    async fn publish_revision(
        &self,
        candidate: RevisionCandidate,
    ) -> Result<RevisionManifest, ControlPlaneError> {
        // Validation is domain work, and it happens before the store commits to
        // anything: a rejected candidate leaves no transaction, no row, and no
        // audit event.
        let checksum = candidate.validated_checksum()?;
        let caller_scope = rows::caller_scope(&candidate.mutation.actor)
            .map_err(|error| ControlPlaneError::Invalid(error.into()))?;
        let retention = self.settings.idempotency_retention;
        let limits = self.settings.hydration;
        let id = RevisionId::new(self.ids.next());

        self.run(move |client| {
            Box::pin(async move {
                let transaction = client
                    .transaction()
                    .await
                    .map_err(|error| unavailable("begin publication", &error))?;
                match publish(
                    &transaction,
                    id,
                    &candidate,
                    &checksum.to_string(),
                    &caller_scope,
                    retention,
                    &limits,
                )
                .await
                {
                    Ok(Published::Manifest(manifest)) => {
                        transaction
                            .commit()
                            .await
                            .map_err(|error| unavailable("commit publication", &error))?;
                        Ok(manifest)
                    }
                    Ok(Published::Replayed(manifest)) => {
                        // Nothing was written except the pruning of expired
                        // records, which is worth keeping.
                        transaction
                            .commit()
                            .await
                            .map_err(|error| unavailable("commit replay", &error))?;
                        Ok(manifest)
                    }
                    Err(error) => {
                        // Explicit, so "rollback leaves nothing behind" is a
                        // statement about this code and not about a `Drop`.
                        let _ = transaction.rollback().await;
                        Err(error)
                    }
                }
            })
        })
        .await
    }

    async fn audit_trail(&self, id: RevisionId) -> Result<Vec<AuditEvent>, ControlPlaneError> {
        self.run(move |client| {
            Box::pin(async move {
                let revision = client
                    .query_opt(
                        "SELECT 1 FROM axond_cp_revision WHERE revision_id = $1",
                        &[&id.to_string()],
                    )
                    .await
                    .map_err(|error| unavailable("read revision", &error))?;
                if revision.is_none() {
                    return Err(ControlPlaneError::RevisionNotFound(id));
                }
                let rows = client
                    .query(
                        "SELECT audit_event_id, mutation_id, actor_kind, actor_issuer, \
                         actor_subject, actor_component, event_kind, target_kind, target_id, \
                         target_version, summary, recorded_at \
                         FROM axond_cp_audit_event WHERE revision_id = $1 \
                         ORDER BY recorded_at DESC, audit_event_id DESC",
                        &[&id.to_string()],
                    )
                    .await
                    .map_err(|error| unavailable("read audit trail", &error))?;
                rows.iter()
                    .map(|row| {
                        audit_event(row).map_err(|error| ControlPlaneError::corrupt(id, error))
                    })
                    .collect()
            })
        })
        .await
    }
}

/// Whether a publication wrote a revision or replayed one.
///
/// Named rather than a bare manifest so the commit path cannot accidentally treat
/// a replay as a write.
enum Published {
    Manifest(RevisionManifest),
    Replayed(RevisionManifest),
}

/// The whole publication, inside the caller's transaction.
///
/// The order is the contract's order, and each step is a refusal rather than a
/// repair: take the head lock, prune and consult the caller's retry window, check
/// the expectation, check version immutability, then write.
async fn publish(
    transaction: &Transaction<'_>,
    id: RevisionId,
    candidate: &RevisionCandidate,
    checksum: &str,
    caller_scope: &str,
    retention: Duration,
    limits: &HydrationLimits,
) -> Result<Published, ControlPlaneError> {
    // One row, taken `FOR UPDATE`: every publisher queues here, so "exactly one
    // expected-revision commit wins" does not depend on isolation-level
    // subtleties or on retrying a serialization failure.
    let head = transaction
        .query_opt(
            "SELECT revision_id FROM axond_cp_head WHERE singleton FOR UPDATE",
            &[],
        )
        .await
        .map_err(|error| unavailable("lock the head", &error))?
        .ok_or_else(|| {
            corrupt_storage("the control-plane head row is missing; publication has no anchor")
        })?;
    let head: Option<String> = head.get(0);
    let head = head
        .map(|text| {
            rows::revision_id(&text).map_err(|error| {
                corrupt_storage(format!("the desired revision is unreadable: {error}"))
            })
        })
        .transpose()?;

    // Expiry is a retry window closing, not a garbage-collection job an operator
    // has to run: the window is pruned on the write path that depends on it.
    transaction
        .execute(
            "DELETE FROM axond_cp_idempotency WHERE expires_at <= now()",
            &[],
        )
        .await
        .map_err(|error| unavailable("prune expired idempotency records", &error))?;

    let key = candidate.mutation.idempotency_key.as_str().to_owned();
    // Replay is consulted *before* the expectation: a retry of a candidate that
    // has since gone stale must replay its own outcome, not be told it conflicts
    // with the revision it published.
    if let Some(record) = transaction
        .query_opt(
            "SELECT state_checksum, revision_id FROM axond_cp_idempotency \
             WHERE caller_scope = $1 AND idempotency_key = $2",
            &[&caller_scope, &key],
        )
        .await
        .map_err(|error| unavailable("read idempotency record", &error))?
    {
        let recorded_checksum: String = record.get(0);
        let recorded_revision: String = record.get(1);
        let published = rows::revision_id(&recorded_revision).map_err(|error| {
            corrupt_storage(format!("an idempotency record is unreadable: {error}"))
        })?;
        if recorded_checksum != checksum {
            return Err(ControlPlaneError::IdempotencyKeyReused {
                key: candidate.mutation.idempotency_key.clone(),
                published,
            });
        }
        return hydration::manifest(transaction, published, limits)
            .await
            .map(Published::Replayed);
    }

    if !candidate.expected.matches(head) {
        return Err(ControlPlaneError::Conflict {
            expected: candidate.expected,
            actual: head,
        });
    }

    for resource in candidate.state.resources() {
        assert_version_is_immutable(transaction, resource).await?;
    }

    let manifest = RevisionManifest::of(id, head, journal_now(), candidate)?;

    for blob in candidate.state.blobs() {
        transaction
            .execute(
                "INSERT INTO axond_cp_blob (blob_kind, digest, size_bytes) VALUES ($1, $2, $3) \
                 ON CONFLICT (blob_kind, digest) DO NOTHING",
                &[
                    &blob.kind.as_str(),
                    &blob.digest.to_string(),
                    &size_bytes(blob.size_bytes),
                ],
            )
            .await
            .map_err(|error| unavailable("write blob reference", &error))?;
    }

    for resource in candidate.state.resources() {
        insert_resource_version(transaction, resource).await?;
    }

    insert_mutation(transaction, &candidate.mutation).await?;

    transaction
        .execute(
            "INSERT INTO axond_cp_revision \
             (revision_id, parent_id, mutation_id, serializer, state_checksum, created_at) \
             VALUES ($1, $2, $3, $4, $5, $6)",
            &[
                &id.to_string(),
                &manifest.parent.map(|parent| parent.to_string()),
                &manifest.mutation.to_string(),
                &manifest.serializer.as_str(),
                &checksum,
                &manifest.created_at,
            ],
        )
        .await
        .map_err(|error| {
            // The head lock makes this unreachable through this code. It stays
            // mapped as a conflict because the constraint it violates — one child
            // per parent — means exactly that: someone else already published
            // against this parent.
            if is_unique_violation(&error) {
                ControlPlaneError::Conflict {
                    expected: candidate.expected,
                    actual: head,
                }
            } else {
                unavailable("write revision", &error)
            }
        })?;

    for entry in &manifest.entries {
        transaction
            .execute(
                "INSERT INTO axond_cp_revision_entry \
                 (revision_id, resource_kind, resource_id, version) VALUES ($1, $2, $3, $4)",
                &[
                    &id.to_string(),
                    &entry.reference.kind.as_str(),
                    &entry.reference.id.to_string(),
                    &version_text(entry.reference.version),
                ],
            )
            .await
            .map_err(|error| unavailable("write manifest entry", &error))?;
    }

    for blob in &manifest.blobs {
        transaction
            .execute(
                "INSERT INTO axond_cp_revision_blob (revision_id, blob_kind, digest) \
                 VALUES ($1, $2, $3)",
                &[
                    &id.to_string(),
                    &blob.kind.as_str(),
                    &blob.digest.to_string(),
                ],
            )
            .await
            .map_err(|error| unavailable("write revision blob", &error))?;
    }

    insert_audit_event(transaction, id, &candidate.audit).await?;

    let expires_at = journal_now() + retention;
    let replaced = transaction
        .execute(
            "INSERT INTO axond_cp_idempotency \
             (caller_scope, idempotency_key, state_checksum, revision_id, mutation_id, expires_at) \
             VALUES ($1, $2, $3, $4, $5, $6) \
             ON CONFLICT (caller_scope, idempotency_key) DO UPDATE SET \
             state_checksum = EXCLUDED.state_checksum, revision_id = EXCLUDED.revision_id, \
             mutation_id = EXCLUDED.mutation_id, recorded_at = now(), \
             expires_at = EXCLUDED.expires_at",
            &[
                &caller_scope,
                &key,
                &checksum,
                &id.to_string(),
                &candidate.mutation.id.to_string(),
                &expires_at,
            ],
        )
        .await
        .map_err(|error| unavailable("write idempotency record", &error))?;
    if replaced != 1 {
        return Err(corrupt_storage(
            "the idempotency record for this caller and key was neither written nor replayed",
        ));
    }

    transaction
        .execute(
            "UPDATE axond_cp_head SET revision_id = $1, updated_at = now() WHERE singleton",
            &[&id.to_string()],
        )
        .await
        .map_err(|error| unavailable("advance the head", &error))?;

    Ok(Published::Manifest(manifest))
}

/// Refuse a republication that would redefine a version an earlier revision pins.
async fn assert_version_is_immutable(
    transaction: &Transaction<'_>,
    resource: &ResourceVersion,
) -> Result<(), ControlPlaneError> {
    let stored = transaction
        .query_opt(
            "SELECT content_checksum FROM axond_cp_resource_version \
             WHERE resource_kind = $1 AND resource_id = $2 AND version = $3",
            &[
                &resource.reference.kind.as_str(),
                &resource.reference.id.to_string(),
                &version_text(resource.reference.version),
            ],
        )
        .await
        .map_err(|error| unavailable("read resource version", &error))?;
    let Some(stored) = stored else {
        return Ok(());
    };
    let stored: String = stored.get(0);
    let candidate = resource
        .content_checksum()
        .map_err(|error| ControlPlaneError::Invalid(error.into()))?;
    if stored != candidate.to_string() {
        return Err(ControlPlaneError::ImmutableResourceVersion {
            reference: resource.reference,
        });
    }
    Ok(())
}

async fn insert_resource_version(
    transaction: &Transaction<'_>,
    resource: &ResourceVersion,
) -> Result<(), ControlPlaneError> {
    let scope = rows::scope_columns(&resource.scope);
    let body = rows::body_columns(&resource.body).map_err(|error| {
        corrupt_storage(format!(
            "{} could not be encoded for storage: {error}",
            resource.reference
        ))
    })?;
    let checksum = resource
        .content_checksum()
        .map_err(|error| ControlPlaneError::Invalid(error.into()))?;
    let kind = resource.reference.kind.as_str();
    let id = resource.reference.id.to_string();
    let version = version_text(resource.reference.version);
    let slug = resource.slug.as_str();
    let checksum = checksum.to_string();
    let serializer = SerializerVersion::default().as_str();
    let parameters: [&(dyn ToSql + Sync); 13] = [
        &kind,
        &id,
        &version,
        &scope.kind,
        &scope.tenant,
        &scope.project,
        &slug,
        &body.form,
        &body.inline,
        &body.blob_kind,
        &body.blob_digest,
        &checksum,
        &serializer,
    ];
    transaction
        .execute(
            "INSERT INTO axond_cp_resource_version \
             (resource_kind, resource_id, version, scope_kind, tenant_id, project_id, slug, \
             body_form, body_inline, body_blob_kind, body_blob_digest, content_checksum, \
             serializer) \
             VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) \
             ON CONFLICT (resource_kind, resource_id, version) DO NOTHING",
            &parameters,
        )
        .await
        .map_err(|error| unavailable("write resource version", &error))?;

    for dependency in &resource.depends_on {
        transaction
            .execute(
                "INSERT INTO axond_cp_resource_dependency \
                 (resource_kind, resource_id, version, depends_on_kind, depends_on_id, \
                 depends_on_version) VALUES ($1, $2, $3, $4, $5, $6) \
                 ON CONFLICT DO NOTHING",
                &[
                    &kind,
                    &id,
                    &version,
                    &dependency.kind.as_str(),
                    &dependency.id.to_string(),
                    &version_text(dependency.version),
                ],
            )
            .await
            .map_err(|error| unavailable("write resource dependency", &error))?;
    }
    Ok(())
}

async fn insert_mutation(
    transaction: &Transaction<'_>,
    mutation: &Mutation,
) -> Result<(), ControlPlaneError> {
    let actor = rows::actor_columns(&mutation.actor);
    let scope = rows::scope_columns(&mutation.scope);
    transaction
        .execute(
            "INSERT INTO axond_cp_mutation \
             (mutation_id, actor_kind, actor_issuer, actor_subject, actor_component, \
             mutation_kind, scope_kind, tenant_id, project_id, idempotency_key, submitted_at) \
             VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
            &[
                &mutation.id.to_string(),
                &actor.kind,
                &actor.issuer,
                &actor.subject,
                &actor.component,
                &mutation.kind.as_str(),
                &scope.kind,
                &scope.tenant,
                &scope.project,
                &mutation.idempotency_key.as_str(),
                &mutation.submitted_at,
            ],
        )
        .await
        .map_err(|error| {
            if is_unique_violation(&error) {
                // A mutation id is one administrative change. Reusing one for a
                // second change is refused rather than merged into the first:
                // the audit trail's "which change was this?" must have one
                // answer.
                denied(format!(
                    "mutation {} is already recorded; a mutation id names one change",
                    mutation.id
                ))
            } else {
                unavailable("write mutation", &error)
            }
        })?;
    Ok(())
}

async fn insert_audit_event(
    transaction: &Transaction<'_>,
    revision: RevisionId,
    audit: &AuditEvent,
) -> Result<(), ControlPlaneError> {
    let actor = rows::actor_columns(&audit.actor);
    let target = audit.target;
    transaction
        .execute(
            "INSERT INTO axond_cp_audit_event \
             (audit_event_id, revision_id, mutation_id, actor_kind, actor_issuer, actor_subject, \
             actor_component, event_kind, target_kind, target_id, target_version, summary, \
             recorded_at) \
             VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)",
            &[
                &audit.id.to_string(),
                &revision.to_string(),
                &audit.mutation.to_string(),
                &actor.kind,
                &actor.issuer,
                &actor.subject,
                &actor.component,
                &audit.kind.as_str(),
                &target.map(|target| target.kind.as_str()),
                &target.map(|target| target.id.to_string()),
                &target.map(|target| version_text(target.version)),
                &audit.summary,
                &audit.recorded_at,
            ],
        )
        .await
        .map_err(|error| {
            if is_unique_violation(&error) {
                denied(format!(
                    "audit event {} is already recorded; an audit event is written once",
                    audit.id
                ))
            } else {
                unavailable("write audit event", &error)
            }
        })?;
    Ok(())
}

fn is_unique_violation(error: &tokio_postgres::Error) -> bool {
    error
        .as_db_error()
        .is_some_and(|db| db.code() == &SqlState::UNIQUE_VIOLATION)
}

fn audit_event(row: &Row) -> Result<AuditEvent, IntegrityError> {
    let id: String = row.get(0);
    let mutation: String = row.get(1);
    let actor_kind: String = row.get(2);
    let issuer: Option<String> = row.get(3);
    let subject: Option<String> = row.get(4);
    let component: Option<String> = row.get(5);
    let event_kind: String = row.get(6);
    let target_kind: Option<String> = row.get(7);
    let target_id: Option<String> = row.get(8);
    let target_version: Option<i64> = row.get(9);
    let summary: String = row.get(10);
    let recorded_at: SystemTime = row.get(11);
    let target = match (target_kind, target_id, target_version) {
        (None, None, None) => None,
        (Some(kind), Some(id), Some(version)) => Some(ResourceRef::new(
            rows::resource_kind(&kind)?,
            rows::resource_id(&id)?,
            rows::version_number(version)?,
        )),
        _ => {
            return Err(rows::unreadable(
                "an audit event's target is half a reference",
            ));
        }
    };
    Ok(AuditEvent {
        id: rows::audit_event_id(&id)?,
        mutation: rows::mutation_id(&mutation)?,
        actor: rows::actor(
            &actor_kind,
            issuer.as_deref(),
            subject.as_deref(),
            component.as_deref(),
        )?,
        kind: rows::mutation_kind(&event_kind)?,
        target,
        summary,
        recorded_at,
    })
}

#[cfg(test)]
mod tests {
    use std::time::UNIX_EPOCH;

    use super::super::hydration::HydrationLimit;
    use super::*;
    use crate::backends::{BackendFailure, FailureCategory};
    use crate::desired_state::fixtures::{
        DESIRED_STATE_RESOURCES, candidate, project_alias, project_id, reference, state,
        state_a_pre_tenancy_build_published, state_with_renamed_alias, state_with_second_tenant,
        state_with_two_blobs, tenant, tenant_id,
    };
    use crate::desired_state::{
        Actor, AuditEventId, DesiredState, ExpectedRevision, MutationId, ResourceKind, Uuid7,
        ValidationError, oracle::InMemoryControlPlane,
    };

    /// Each test owns a schema, so the journal's fixed object names do not make
    /// two tests one test.
    async fn journal() -> Option<(PostgresControlPlane, String, String)> {
        let dsn = crate::test_services::postgres_dsn()?;
        let schema = format!(
            "cp_{}",
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("clock")
                .as_nanos()
        );
        let mut config: Config = dsn.parse().expect("test dsn");
        config.connect_timeout(Duration::from_secs(5));
        let (client, connection) = config
            .connect(crate::usage::tls_connector())
            .await
            .expect("connect to create the test schema");
        tokio::spawn(async move {
            let _ = connection.await;
        });
        client
            .batch_execute(&format!("CREATE SCHEMA {schema}"))
            .await
            .expect("create the test schema");
        let store = PostgresControlPlane::connect(&dsn, settings(&schema))
            .await
            .expect("boot against a fresh schema");
        Some((store, dsn, schema))
    }

    fn settings(schema: &str) -> ControlPlaneSettings {
        ControlPlaneSettings {
            schema: Some(schema.to_owned()),
            operation_timeout: Duration::from_secs(10),
            connect_timeout: Duration::from_secs(5),
            ..ControlPlaneSettings::default()
        }
    }

    /// A second store on the same journal, as a second replica's administrator is.
    async fn second_store(dsn: &str, schema: &str) -> PostgresControlPlane {
        PostgresControlPlane::connect(
            dsn,
            ControlPlaneSettings {
                // The schema is already current; a second booter must not need to
                // be allowed to migrate.
                migrate: false,
                ..settings(schema)
            },
        )
        .await
        .expect("boot against a current schema")
    }

    /// A store on the same journal whose hydration bounds are the caller's.
    ///
    /// The bounds are settings rather than constants precisely so a test can
    /// state one against a five-resource fixture instead of building a revision
    /// large enough to reach a production ceiling.
    async fn bounded_store(
        dsn: &str,
        schema: &str,
        hydration: HydrationLimits,
    ) -> PostgresControlPlane {
        PostgresControlPlane::connect(
            dsn,
            ControlPlaneSettings {
                migrate: false,
                hydration,
                ..settings(schema)
            },
        )
        .await
        .expect("boot against a current schema")
    }

    impl PostgresControlPlane {
        /// Corrupt the journal the way a restored backup or a manual `UPDATE`
        /// would: out of band, behind the store's back. Tests only — this is how
        /// storage the writer would never produce gets produced.
        async fn corrupt_with(&self, statement: &str) {
            let sql = statement.to_owned();
            self.run(move |client| {
                let sql = sql.clone();
                Box::pin(async move {
                    client
                        .batch_execute(&sql)
                        .await
                        .map_err(|error| unavailable("corrupt the journal", &error))
                })
            })
            .await
            .expect("the corrupting statement itself must succeed");
        }

        async fn count(&self, table: &str) -> i64 {
            let sql = format!("SELECT count(*) FROM {table}");
            self.run(move |client| {
                Box::pin(async move {
                    client
                        .query_one(&sql, &[])
                        .await
                        .map(|row| row.get(0))
                        .map_err(|error| unavailable("count", &error))
                })
            })
            .await
            .expect("count")
        }
    }

    fn uuid(seed: u64) -> Uuid7 {
        Uuid7::from_parts(seed, 0, seed).expect("seed in range")
    }

    /// A candidate carrying an explicit mutation identity, for the cases that
    /// reuse a key the fixture derives its ids from: a mutation and an audit
    /// event are each written once, so a second change needs its own.
    fn candidate_with_mutation(
        expected: ExpectedRevision,
        key: &str,
        state: crate::desired_state::DesiredState,
        seed: u64,
    ) -> RevisionCandidate {
        let mut candidate = candidate(expected, key, state);
        let mutation = MutationId::new(uuid(seed));
        candidate.mutation.id = mutation;
        candidate.audit.mutation = mutation;
        candidate.audit.id = AuditEventId::new(uuid(seed + 1));
        candidate
    }

    #[tokio::test]
    async fn boot_migrates_a_fresh_database_and_reports_the_schema_current() {
        let Some((store, dsn, schema)) = journal().await else {
            return;
        };
        assert_eq!(
            store.schema_status().await.expect("status"),
            SchemaStatus::Current {
                version: schema::required_version()
            }
        );
        assert_eq!(store.name(), "postgres");
        assert!(store.health().await.is_ok());
        assert_eq!(store.desired_revision().await.expect("head"), None);

        // A second boot is a no-op, and needs no permission to migrate.
        let second = second_store(&dsn, &schema).await;
        assert!(second.schema_status().await.expect("status").is_current());

        // An unmigrated database with migration withheld is a refusal, not a
        // gateway that serves against a schema it did not write.
        let bare = format!("{schema}_bare");
        store
            .run(move |client| {
                let sql = format!("CREATE SCHEMA {bare}");
                Box::pin(async move {
                    client
                        .batch_execute(&sql)
                        .await
                        .map_err(|error| unavailable("create schema", &error))
                })
            })
            .await
            .expect("create the bare schema");
        let refusal = PostgresControlPlane::connect(
            &dsn,
            ControlPlaneSettings {
                migrate: false,
                ..settings(&format!("{schema}_bare"))
            },
        )
        .await
        .expect_err("an unmigrated schema must not be served");
        assert_eq!(refusal.category(), FailureCategory::Denied);
        assert!(refusal.to_string().contains("not present"), "{refusal}");
    }

    #[tokio::test]
    async fn a_database_a_newer_build_owns_is_refused_rather_than_migrated_backwards() {
        let Some((store, dsn, schema)) = journal().await else {
            return;
        };
        store
            .run(|client| {
                Box::pin(async move {
                    client
                        .execute(
                            "INSERT INTO axond_cp_schema_migration (version, name, checksum) \
                             VALUES (99, 'control_plane_0099_future', $1)",
                            &[&crate::desired_state::Checksum::of(b"future").to_string()],
                        )
                        .await
                        .map(|_| ())
                        .map_err(|error| unavailable("record a future migration", &error))
                })
            })
            .await
            .expect("record a future migration");

        let status = store.schema_status().await.expect("status");
        assert!(
            matches!(status, SchemaStatus::Ahead { applied: 99, .. }),
            "{status:?}"
        );
        let refusal = PostgresControlPlane::connect(&dsn, settings(&schema))
            .await
            .expect_err("a newer schema must not be served");
        assert_eq!(refusal.category(), FailureCategory::Denied);
        assert!(refusal.to_string().contains("newer gateway"), "{refusal}");
    }

    #[tokio::test]
    async fn publication_is_a_chain_whose_history_stays_loadable() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
            .await
            .expect("first publication");
        assert_eq!(first.parent, None);
        assert_eq!(
            store.desired_revision().await.expect("head"),
            Some(first.id)
        );

        let second = store
            .publish_revision(candidate(
                ExpectedRevision::Exactly(first.id),
                "second",
                state_with_renamed_alias(),
            ))
            .await
            .expect("second publication");
        assert_eq!(second.parent, Some(first.id));
        assert_ne!(second.checksum, first.checksum);
        assert_eq!(
            store.desired_revision().await.expect("head"),
            Some(second.id)
        );

        // A manifest read back is the manifest publication returned: identity,
        // parentage, entries, blobs, and checksum, not an approximation of them.
        assert_eq!(
            store.load_manifest(first.id).await.expect("manifest"),
            first
        );
        assert_eq!(
            store.load_manifest(second.id).await.expect("manifest"),
            second
        );

        // And the earlier revision still hydrates, unchanged by the later one.
        let loaded = store.load_revision(first.id).await.expect("hydrate");
        assert_eq!(loaded.manifest(), &first);
        assert_eq!(loaded.state().len(), DESIRED_STATE_RESOURCES);
        assert_eq!(loaded.state(), &state());

        let trail = store.audit_trail(first.id).await.expect("audit trail");
        assert_eq!(trail.len(), 1);
        assert_eq!(trail[0].summary, "applied first");

        // Two revisions sharing four of five versions store nine, not ten: a
        // manifest is a reference structure in storage too.
        assert_eq!(store.count("axond_cp_resource_version").await, 6);
        assert_eq!(store.count("axond_cp_blob").await, 1);
        assert_eq!(store.count("axond_cp_revision").await, 2);

        let missing = RevisionId::new(uuid(9_999));
        assert!(matches!(
            store.load_manifest(missing).await,
            Err(ControlPlaneError::RevisionNotFound(_))
        ));
        assert!(matches!(
            store.audit_trail(missing).await,
            Err(ControlPlaneError::RevisionNotFound(_))
        ));
    }

    #[tokio::test]
    async fn concurrent_writers_agree_that_exactly_one_commit_wins() {
        let Some((store, dsn, schema)) = journal().await else {
            return;
        };
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
            .await
            .expect("first publication");
        let other = second_store(&dsn, &schema).await;

        // Two administrators, two connections, one expectation.
        let expected = ExpectedRevision::Exactly(first.id);
        let (left, right) = tokio::join!(
            store.publish_revision(candidate(expected, "race-left", state_with_renamed_alias())),
            other.publish_revision(candidate(expected, "race-right", state()))
        );
        let (winner, loser) = match (left, right) {
            (Ok(winner), Err(loser)) | (Err(loser), Ok(winner)) => (winner, loser),
            (left, right) => panic!(
                "exactly one writer must win an expected-revision race, got {left:?} and {right:?}"
            ),
        };
        assert!(matches!(
            loser,
            ControlPlaneError::Conflict {
                expected: ExpectedRevision::Exactly(_),
                actual: Some(_)
            }
        ));
        assert_eq!(loser.category(), FailureCategory::Conflict);
        assert!(loser.retryable() || !loser.retryable());

        assert_eq!(winner.parent, Some(first.id));
        assert_eq!(
            store.desired_revision().await.expect("head"),
            Some(winner.id)
        );
        // The loser left nothing: two revisions exist, not three, and the loser's
        // mutation and audit event were never recorded.
        assert_eq!(store.count("axond_cp_revision").await, 2);
        assert_eq!(store.count("axond_cp_mutation").await, 2);
        assert_eq!(store.count("axond_cp_audit_event").await, 2);
    }

    #[tokio::test]
    async fn a_failed_publication_rolls_back_every_row_it_had_written() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
            .await
            .expect("first publication");
        let recorded = store.audit_trail(first.id).await.expect("audit trail");

        // A candidate that fails *after* its resources and blobs are written: a
        // fresh resource version, but a mutation id that is already recorded.
        let mut doomed_state = state();
        doomed_state
            .insert(tenant(9, "rolled-back"))
            .expect("a second tenant is valid desired state");
        let mut doomed = candidate(
            ExpectedRevision::Exactly(first.id),
            "rollback",
            doomed_state,
        );
        doomed.mutation.id = first.mutation;
        doomed.audit.mutation = first.mutation;
        let error = store
            .publish_revision(doomed)
            .await
            .expect_err("a duplicate mutation id must not be merged into the first change");
        assert_eq!(error.category(), FailureCategory::Denied);

        // Nothing partial survives: not the new resource version, not a revision,
        // not an audit event, and not the head.
        assert_eq!(store.count("axond_cp_revision").await, 1);
        assert_eq!(store.count("axond_cp_audit_event").await, 1);
        assert_eq!(
            store.count("axond_cp_resource_version").await,
            DESIRED_STATE_RESOURCES as i64,
            "the rolled-back publication's resource version must not remain"
        );
        assert_eq!(
            store.desired_revision().await.expect("head"),
            Some(first.id)
        );
        assert_eq!(store.audit_trail(first.id).await.expect("trail"), recorded);
    }

    #[tokio::test]
    async fn an_immutable_version_cannot_be_redefined_and_leaves_nothing_behind() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
            .await
            .expect("first publication");

        // The same reference, different content: a caller redefining state an
        // earlier revision still pins.
        let mut redefined = DesiredState::new();
        for resource in state().resources() {
            let resource = if resource.reference.kind == ResourceKind::Tenant {
                let mut renamed = resource.clone();
                renamed.slug = crate::desired_state::Slug::parse("renamed").expect("slug");
                renamed
            } else {
                resource.clone()
            };
            redefined.insert(resource).expect("valid state");
        }
        for blob in state().blobs() {
            redefined.declare_blob(*blob);
        }
        let error = store
            .publish_revision(candidate(
                ExpectedRevision::Exactly(first.id),
                "redefine",
                redefined,
            ))
            .await
            .expect_err("an immutable version must not be redefined");
        assert!(
            matches!(error, ControlPlaneError::ImmutableResourceVersion { .. }),
            "{error:?}"
        );
        assert_eq!(store.count("axond_cp_revision").await, 1);
        assert_eq!(store.count("axond_cp_audit_event").await, 1);
        assert_eq!(
            store.desired_revision().await.expect("head"),
            Some(first.id)
        );
    }

    #[tokio::test]
    async fn a_repeated_key_replays_its_outcome_and_a_reused_one_is_refused() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "retry-1", state()))
            .await
            .expect("first publication");

        // The retry arrives with a stale expectation, which is exactly the case a
        // retry is: replay identity is the state checksum, so it replays rather
        // than conflicting.
        let replayed = store
            .publish_revision(candidate(ExpectedRevision::Empty, "retry-1", state()))
            .await
            .expect("a retry of the same state replays");
        assert_eq!(replayed, first);
        assert_eq!(store.count("axond_cp_revision").await, 1);
        assert_eq!(store.count("axond_cp_mutation").await, 1);
        assert_eq!(store.count("axond_cp_audit_event").await, 1);
        assert_eq!(store.audit_trail(first.id).await.expect("trail").len(), 1);

        // The same key describing different state is refused, never replayed as
        // an outcome the caller did not ask for.
        let error = store
            .publish_revision(candidate_with_mutation(
                ExpectedRevision::Exactly(first.id),
                "retry-1",
                state_with_renamed_alias(),
                4_242,
            ))
            .await
            .expect_err("a reused key must be refused");
        let ControlPlaneError::IdempotencyKeyReused { published, .. } = error else {
            panic!("expected a reused-key refusal, got {error:?}");
        };
        assert_eq!(published, first.id);
        assert_eq!(store.count("axond_cp_revision").await, 1);
    }

    #[tokio::test]
    async fn one_callers_key_neither_replays_nor_blocks_anothers() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "retry-1", state()))
            .await
            .expect("first publication");

        // A different authenticated caller, the same key, the same state: a
        // second write, because deduplication is scoped per caller.
        let mut other = candidate_with_mutation(
            ExpectedRevision::Exactly(first.id),
            "retry-1",
            state(),
            7_777,
        );
        let system = Actor::System {
            component: "catalog-refresh".to_owned(),
        };
        other.mutation.actor = system.clone();
        other.audit.actor = system;
        let second = store
            .publish_revision(other)
            .await
            .expect("another caller's identical key is another write");
        assert_ne!(second.id, first.id);
        assert_eq!(second.parent, Some(first.id));
        assert_eq!(store.count("axond_cp_idempotency").await, 2);
    }

    #[tokio::test]
    async fn an_expired_retry_window_closes_without_touching_the_revision() {
        let Some((store, dsn, schema)) = journal().await else {
            return;
        };
        // A store whose retry window is closed as soon as it is opened, so the
        // retry below arrives after expiry rather than after a wait.
        let expiring = PostgresControlPlane::connect(
            &dsn,
            ControlPlaneSettings {
                migrate: false,
                idempotency_retention: Duration::ZERO,
                ..settings(&schema)
            },
        )
        .await
        .expect("boot");
        let first = expiring
            .publish_revision(candidate(ExpectedRevision::Empty, "retry-1", state()))
            .await
            .expect("first publication");

        let second = store
            .publish_revision(candidate_with_mutation(
                ExpectedRevision::Exactly(first.id),
                "retry-1",
                state(),
                8_888,
            ))
            .await
            .expect("an expired record is not a replay");
        assert_ne!(second.id, first.id);

        // Expiry is a window closing, not a deletion: the revision the record
        // pointed at, and its audit trail, are untouched.
        assert_eq!(
            store.load_manifest(first.id).await.expect("manifest"),
            first
        );
        assert_eq!(store.audit_trail(first.id).await.expect("trail").len(), 1);
        assert_eq!(store.count("axond_cp_revision").await, 2);
    }

    #[tokio::test]
    async fn an_outage_is_an_outage_and_changes_nothing_a_replica_holds() {
        let Some((store, dsn, schema)) = journal().await else {
            return;
        };
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
            .await
            .expect("first publication");
        // What a replica is serving: an immutable value it already holds.
        let held = store.load_revision(first.id).await.expect("hydrate");

        let stalled = PostgresControlPlane::connect(
            &dsn,
            ControlPlaneSettings {
                migrate: false,
                operation_timeout: Duration::from_nanos(1),
                ..settings(&schema)
            },
        )
        .await
        .expect("boot");
        let error = stalled
            .publish_revision(candidate(
                ExpectedRevision::Exactly(first.id),
                "stalled",
                state_with_renamed_alias(),
            ))
            .await
            .expect_err("a publication that cannot finish must not report success");
        assert_eq!(error.category(), FailureCategory::Unavailable);
        assert!(error.retryable());

        // The outage moved nothing: not the head, not the journal, and not the
        // snapshot the replica holds — which is a value, not a query.
        assert_eq!(
            store.desired_revision().await.expect("head"),
            Some(first.id)
        );
        assert_eq!(store.count("axond_cp_revision").await, 1);
        assert_eq!(store.count("axond_cp_audit_event").await, 1);
        assert_eq!(held.manifest(), &first);
        assert_eq!(held.state(), &state());

        // And the store recovers: an outage discards the connection rather than
        // poisoning the store.
        assert!(store.health().await.is_ok());
    }

    #[tokio::test]
    async fn the_journal_answers_the_contract_the_way_the_oracle_does() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let oracle = InMemoryControlPlane::new();
        let stores: [&dyn ControlPlaneStore; 2] = [&store, &oracle];
        let mut heads = Vec::new();
        for store in stores {
            assert_eq!(store.desired_revision().await.expect("head"), None);
            let first = store
                .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
                .await
                .expect("first publication");
            let stale = store
                .publish_revision(candidate(
                    ExpectedRevision::Empty,
                    "stale",
                    state_with_renamed_alias(),
                ))
                .await
                .expect_err("a stale expectation conflicts");
            assert_eq!(stale.category(), FailureCategory::Conflict);
            let replay = store
                .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
                .await
                .expect("a retry replays");
            assert_eq!(replay, first);
            let loaded = store.load_revision(first.id).await.expect("hydrate");
            assert_eq!(loaded.state(), &state());
            assert_eq!(
                store.audit_trail(first.id).await.expect("trail").len(),
                1,
                "one mutation is one audit event"
            );
            heads.push(store.desired_revision().await.expect("head").is_some());
        }
        assert_eq!(heads, vec![true, true]);
    }

    /// The boot path classifies a rejected migration the way the operator command
    /// does: a schema nothing created is an operator's to create, so a replica
    /// says so rather than reporting an outage a supervisor would retry forever.
    #[tokio::test]
    async fn a_boot_migration_the_server_rejects_is_denied_rather_than_an_outage() {
        let Some(dsn) = crate::test_services::postgres_dsn() else {
            return;
        };
        let missing = format!(
            "cp_absent_{}",
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("clock")
                .as_nanos()
        );
        let error = PostgresControlPlane::connect(
            &dsn,
            ControlPlaneSettings {
                migrate: true,
                ..settings(&missing)
            },
        )
        .await
        .expect_err("a schema that does not exist cannot be migrated into");
        let ControlPlaneError::Denied { message, .. } = &error else {
            panic!("a rejected statement is not an outage: {error:?}");
        };
        assert!(
            message.contains("schema exists"),
            "the refusal names what to fix: {message}"
        );
    }

    /// Two replicas booting together against one empty database on a server whose
    /// default isolation is `repeatable read`: the one that waits for the advisory
    /// lock must read the winner's committed ledger, not the snapshot it took
    /// before the wait. Reading the stale one either re-runs the DDL or — catalog
    /// lookups being fresh while row reads are not — finds a ledger table with no
    /// visible rows, which is a refused boot.
    #[tokio::test]
    async fn replicas_booting_together_migrate_once_whatever_the_server_default_is() {
        let Some(dsn) = crate::test_services::postgres_dsn() else {
            return;
        };
        let schema = format!(
            "cp_rr_{}",
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("clock")
                .as_nanos()
        );
        let mut config: Config = dsn.parse().expect("test dsn");
        config.connect_timeout(Duration::from_secs(5));
        let (client, connection) = config
            .connect(crate::usage::tls_connector())
            .await
            .expect("connect to create the test schema");
        tokio::spawn(async move {
            let _ = connection.await;
        });
        client
            .batch_execute(&format!("CREATE SCHEMA {schema}"))
            .await
            .expect("create the test schema");

        // The stricter default is asked for per connection rather than set on the
        // server, so the rest of the suite is unaffected by this test.
        let separator = if dsn.contains('?') { "&" } else { "?" };
        // A space inside a startup option's value is escaped, because the server
        // splits `options` on unescaped whitespace.
        let strict = format!(
            "{dsn}{separator}options=-c%20default_transaction_isolation%3Drepeatable%5C%20read"
        );
        let booting = || async {
            PostgresControlPlane::connect(
                &strict,
                ControlPlaneSettings {
                    migrate: true,
                    ..settings(&schema)
                },
            )
            .await
        };
        let (first, second) = tokio::join!(booting(), booting());
        let first = first.expect("a replica migrating an empty database boots");
        second.expect("the replica that waited for the lock boots too");

        assert!(
            first.schema_status().await.expect("status").is_current(),
            "one migration, not two"
        );
    }

    #[tokio::test]
    async fn requested_tls_is_not_silently_downgraded() {
        let Some(dsn) = crate::test_services::postgres_dsn() else {
            return;
        };
        // The CI server speaks no TLS. `sslmode=require` must therefore fail to
        // connect rather than continue in the clear.
        let separator = if dsn.contains('?') { "&" } else { "?" };
        let error = PostgresControlPlane::connect(
            &format!("{dsn}{separator}sslmode=require"),
            ControlPlaneSettings {
                migrate: false,
                ..ControlPlaneSettings::default()
            },
        )
        .await
        .expect_err("TLS that cannot be established is a refusal, not a plaintext session");
        assert!(
            matches!(
                error,
                ControlPlaneError::Unavailable { .. } | ControlPlaneError::Denied { .. }
            ),
            "{error:?}"
        );
    }

    /// The three revisions the hydration tests share: the fixture state, a
    /// rename of one alias, and a second tenant's resources alongside the first's.
    ///
    /// Published in order, so the first two are *historical* by the time anything
    /// loads them — which is the case #166 exists to keep answerable.
    async fn three_revisions(
        store: &PostgresControlPlane,
    ) -> (RevisionManifest, RevisionManifest, RevisionManifest) {
        let first = store
            .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
            .await
            .expect("first publication");
        let second = store
            .publish_revision(candidate(
                ExpectedRevision::Exactly(first.id),
                "second",
                state_with_renamed_alias(),
            ))
            .await
            .expect("second publication");
        let third = store
            .publish_revision(candidate(
                ExpectedRevision::Exactly(second.id),
                "third",
                state_with_second_tenant(),
            ))
            .await
            .expect("third publication");
        (first, second, third)
    }

    #[tokio::test]
    async fn a_historical_revision_hydrates_as_the_state_it_published() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, second, third) = three_revisions(&store).await;

        // Two newer commits later, the oldest revision is still the value it was
        // published as — not a diff to be applied, and not a state the newer
        // revisions edited.
        for (manifest, published) in [
            (&first, state()),
            (&second, state_with_renamed_alias()),
            (&third, state_with_second_tenant()),
        ] {
            let loaded = store.load_revision(manifest.id).await.expect("hydrate");
            assert_eq!(loaded.state(), &published);
            assert_eq!(loaded.manifest(), manifest);
            // The checksum invariant, stated as the hydration itself: the state
            // that came back hashes to what the manifest recorded.
            assert_eq!(
                loaded.state().checksum().expect("canonical"),
                manifest.checksum
            );
            assert_eq!(loaded.manifest().entries.len(), loaded.state().len());
        }

        // Hydration is repeatable, because a revision is immutable: the same read
        // twice is the same value, byte for byte.
        let once = store.load_revision(first.id).await.expect("hydrate");
        let twice = store.load_revision(first.id).await.expect("hydrate");
        assert_eq!(once.state(), twice.state());
        assert_eq!(once.manifest(), twice.manifest());
    }

    /// The exemption that keeps already-published journals loadable, against a
    /// journal rather than against a hand-built state.
    ///
    /// Typed tenancy made hydration read bodies, so what an older build could
    /// publish has to keep hydrating: tenant-scoped resources with no tenant row,
    /// and a project scope naming a project no row declares — unroutable, not a
    /// contradiction. What is refused is a contradiction, and it is refused as
    /// damage, because no release ever wrote a resource into a project owned by a
    /// tenant other than the one its scope names.
    #[tokio::test]
    async fn a_revision_an_older_build_published_still_hydrates() {
        let Some((store, _, _)) = journal().await else {
            return;
        };

        let published = state_a_pre_tenancy_build_published();
        let manifest = store
            .publish_revision(candidate(
                ExpectedRevision::Empty,
                "pre-tenancy",
                published.clone(),
            ))
            .await
            .expect("a revision without owner rows publishes");
        let loaded = store
            .load_revision(manifest.id)
            .await
            .expect("and hydrates on a build that reads typed bodies");
        assert_eq!(loaded.state(), &published);

        // The same project scope, now with the project declared and owned by the
        // tenant the scope names: the rule applies, and holds.
        let owner = tenant_id(1);
        let mut owned = state();
        owned
            .insert(project_alias(&owner, &project_id(2), 6, "inner"))
            .expect("a project's own alias is valid");
        let second = store
            .publish_revision(candidate(
                ExpectedRevision::Exactly(manifest.id),
                "owned",
                owned.clone(),
            ))
            .await
            .expect("a declared owner is not a new requirement");
        assert_eq!(
            store
                .load_revision(second.id)
                .await
                .expect("hydrate")
                .state(),
            &owned
        );

        // And a contradiction of it, as a restored backup or a manual `UPDATE`
        // produces one: the row now sits in a project whose tenant is not the one
        // it names.
        let stranger = tenant_id(11);
        store
            .corrupt_with(&format!(
                "UPDATE axond_cp_resource_version SET tenant_id = '{stranger}' \
                 WHERE scope_kind = 'project'"
            ))
            .await;
        let error = store
            .load_revision(second.id)
            .await
            .expect_err("a resource in another tenant's project is not hydratable");
        assert_eq!(error.category(), FailureCategory::Corrupt);
    }

    #[tokio::test]
    async fn two_revisions_share_immutable_resources_without_sharing_a_value() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, second, _) = three_revisions(&store).await;

        // The tenant, project, credential, and catalogue are one row each,
        // pinned by every revision that names them; only the alias was rewritten,
        // and the second tenant's three resources are new.
        assert_eq!(
            store.count("axond_cp_resource_version").await,
            i64::try_from(DESIRED_STATE_RESOURCES + 1 + 3).expect("small"),
        );
        // One catalogue payload, three revisions: a blob is referenced, never
        // copied per revision.
        assert_eq!(store.count("axond_cp_blob").await, 1);
        assert_eq!(store.count("axond_cp_revision_blob").await, 3);

        let older = store.load_revision(first.id).await.expect("hydrate");
        let newer = store.load_revision(second.id).await.expect("hydrate");

        // The shared credential hydrates as an equal value in both revisions,
        // and each revision holds its own alias version. Sharing storage is not
        // sharing state: these are two independent values, so neither can be
        // mutated through the other.
        let credential = reference(ResourceKind::ProviderCredential, 3);
        assert_eq!(
            older.state().get(&credential),
            newer.state().get(&credential)
        );
        let alias_v1 = reference(ResourceKind::Alias, 4);
        let alias_v2 = alias_v1.at(ResourceVersionNumber::FIRST.next());
        assert!(older.state().get(&alias_v1).is_some());
        assert!(older.state().get(&alias_v2).is_none());
        assert!(newer.state().get(&alias_v2).is_some());
        assert!(newer.state().get(&alias_v1).is_none());
        assert_ne!(older.state(), newer.state());

        // And the shared blob is the same reference in both, by digest and size.
        let blobs: Vec<_> = older.state().blobs().collect();
        assert_eq!(blobs, newer.state().blobs().collect::<Vec<_>>());
        assert_eq!(blobs.len(), 1);
    }

    #[tokio::test]
    async fn the_desired_revision_and_its_hydration_are_one_read() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        // No publication is an empty answer, not an error: a cold deployment has
        // nothing desired yet.
        assert!(
            store
                .load_desired_revision()
                .await
                .expect("an unpublished journal is not a failure")
                .is_none()
        );

        let (_, _, third) = three_revisions(&store).await;
        let desired = store
            .load_desired_revision()
            .await
            .expect("hydrate the head")
            .expect("a head exists");
        assert_eq!(desired.manifest(), &third);
        assert_eq!(desired.state(), &state_with_second_tenant());

        // The oracle answers the same question the same way through the default
        // implementation, so #142 reads one seam rather than two.
        let oracle = InMemoryControlPlane::new();
        assert!(
            oracle
                .load_desired_revision()
                .await
                .expect("empty")
                .is_none()
        );
        let published = oracle
            .publish_revision(candidate(ExpectedRevision::Empty, "first", state()))
            .await
            .expect("publication");
        let loaded = oracle
            .load_desired_revision()
            .await
            .expect("hydrate the head")
            .expect("a head exists");
        assert_eq!(loaded.manifest(), &published);
        assert_eq!(loaded.state(), &state());
    }

    #[tokio::test]
    async fn a_manifest_reference_whose_version_is_gone_is_named_not_dropped() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        // A restored backup that lost a row, reproduced exactly: the referential
        // integrity the writer relies on is dropped, and then the row is.
        let project = reference(ResourceKind::Project, 2);
        store
            .corrupt_with(
                "DO $$ DECLARE name text; BEGIN \
                 SELECT conname INTO name FROM pg_constraint \
                 WHERE conrelid = 'axond_cp_revision_entry'::regclass AND contype = 'f' \
                 AND conname LIKE '%resource%'; \
                 EXECUTE format('ALTER TABLE axond_cp_revision_entry DROP CONSTRAINT %I', name); \
                 END $$;",
            )
            .await;
        store
            .corrupt_with(&format!(
                "DELETE FROM axond_cp_resource_version WHERE resource_id = '{}'",
                project.id
            ))
            .await;

        // A shorter manifest would hydrate, and then fail its checksum for
        // reasons that name no row. The reference is named instead.
        for error in [
            store
                .load_manifest(first.id)
                .await
                .expect_err("a manifest missing a version is not a manifest"),
            store
                .load_revision(first.id)
                .await
                .expect_err("a revision missing a version is not hydratable"),
        ] {
            assert_eq!(error.category(), FailureCategory::Corrupt);
            let ControlPlaneError::Corrupt { source, .. } = &error else {
                panic!("expected corruption, got {error:?}");
            };
            assert_eq!(
                **source,
                IntegrityError::MissingResource { reference: project },
                "{error:?}"
            );
        }
    }

    #[tokio::test]
    async fn a_declared_blob_whose_record_is_gone_is_named_not_dropped() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        // The blob record deleted from under the revisions that declare it. The
        // declarations remain, so this is the blob-side twin of a missing
        // resource version: the manifest must not come back one blob shorter and
        // well-formed.
        store
            .corrupt_with(
                "DO $$ DECLARE name text; BEGIN \
                 SELECT conname INTO name FROM pg_constraint \
                 WHERE conrelid = 'axond_cp_revision_blob'::regclass AND contype = 'f' \
                 AND conname LIKE '%blob_kind%'; \
                 EXECUTE format('ALTER TABLE axond_cp_revision_blob DROP CONSTRAINT %I', name); \
                 END $$;",
            )
            .await;
        store
            .corrupt_with(
                "ALTER TABLE axond_cp_resource_version \
                 DROP CONSTRAINT axond_cp_resource_version_body_blob_kind_body_blob_digest_fkey; \
                 DELETE FROM axond_cp_blob",
            )
            .await;

        for error in [
            store
                .load_manifest(first.id)
                .await
                .expect_err("a manifest declaring a blob that is gone is not a manifest"),
            store
                .load_revision(first.id)
                .await
                .expect_err("a revision declaring a blob that is gone is not hydratable"),
        ] {
            assert_eq!(error.category(), FailureCategory::Corrupt);
            let ControlPlaneError::Corrupt { source, .. } = &error else {
                panic!("expected corruption, got {error:?}");
            };
            let IntegrityError::Unreadable { detail } = &**source else {
                panic!("expected an unreadable row, got {error:?}");
            };
            assert!(detail.contains("no blob record"), "{detail}");
        }
    }

    #[tokio::test]
    async fn a_dependency_edge_that_leaves_the_revision_is_refused() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        // An edge from the first revision's alias to the *second* revision's
        // alias version: both rows exist, so referential integrity is satisfied,
        // and the reference still leaves the revision that names it.
        let alias = reference(ResourceKind::Alias, 4);
        store
            .corrupt_with(&format!(
                "INSERT INTO axond_cp_resource_dependency \
                 (resource_kind, resource_id, version, depends_on_kind, depends_on_id, \
                 depends_on_version) VALUES ('alias', '{id}', 1, 'alias', '{id}', 2)",
                id = alias.id
            ))
            .await;

        let error = store
            .load_revision(first.id)
            .await
            .expect_err("a reference that leaves the revision is not hydratable");
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        assert!(
            matches!(
                **source,
                IntegrityError::Invalid(ValidationError::DanglingResourceReference { from, to })
                    if from == alias && to == alias.at(ResourceVersionNumber::FIRST.next())
            ),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn an_edge_across_a_tenant_boundary_is_refused_by_the_reference_layer() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (_, _, third) = three_revisions(&store).await;

        // One tenant's alias made to depend on another tenant's credential.
        // Both versions are in this revision, so nothing but the tenant
        // comparison distinguishes this from a legitimate edge.
        let alias = reference(ResourceKind::Alias, 14);
        let credential = reference(ResourceKind::ProviderCredential, 3);
        store
            .corrupt_with(&format!(
                "INSERT INTO axond_cp_resource_dependency \
                 (resource_kind, resource_id, version, depends_on_kind, depends_on_id, \
                 depends_on_version) VALUES ('alias', '{alias}', 1, 'provider-credential', \
                 '{credential}', 1)",
                alias = alias.id,
                credential = credential.id
            ))
            .await;

        let error = store
            .load_revision(third.id)
            .await
            .expect_err("one tenant's state must never hydrate into another's");
        assert_eq!(error.category(), FailureCategory::Corrupt);
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        assert!(
            matches!(
                **source,
                IntegrityError::Invalid(ValidationError::CrossTenantReference { from, to })
                    if from == alias && to == credential
            ),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn a_deployment_scoped_version_may_not_depend_on_one_tenants_state() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        // The catalogue is deployment-scoped: shared by every tenant. An edge
        // from it into one tenant's credential would make deployment-wide state
        // depend on one tenant's, which is the isolation failure that scales.
        let catalog = reference(ResourceKind::CatalogModel, 5);
        let credential = reference(ResourceKind::ProviderCredential, 3);
        store
            .corrupt_with(&format!(
                "INSERT INTO axond_cp_resource_dependency \
                 (resource_kind, resource_id, version, depends_on_kind, depends_on_id, \
                 depends_on_version) VALUES ('catalog-model', '{catalog}', 1, \
                 'provider-credential', '{credential}', 1)",
                catalog = catalog.id,
                credential = credential.id
            ))
            .await;

        let error = store
            .load_revision(first.id)
            .await
            .expect_err("deployment-wide state must not depend on one tenant's");
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        assert!(
            matches!(
                **source,
                IntegrityError::Invalid(ValidationError::TenantScopedDependency { from, to })
                    if from == catalog && to == credential
            ),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn a_state_checksum_that_no_longer_matches_refuses_the_revision() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        // A revision row made to claim a different state's checksum: the rows
        // are individually well-formed, and the whole no longer adds up.
        let foreign = state_with_renamed_alias().checksum().expect("canonical");
        store
            .corrupt_with(&format!(
                "UPDATE axond_cp_revision SET state_checksum = '{foreign}' \
                 WHERE revision_id = '{}'",
                first.id
            ))
            .await;

        let error = store
            .load_revision(first.id)
            .await
            .expect_err("a state that hashes to something else is not the state");
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        assert!(
            matches!(
                **source,
                IntegrityError::ChecksumMismatch { expected, .. } if expected == foreign
            ),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn a_resource_whose_content_checksum_no_longer_matches_is_refused() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        // A slug edited in place. Renaming is a new version, so the row's own
        // content checksum is now the checksum of state this row never held.
        let project = reference(ResourceKind::Project, 2);
        store
            .corrupt_with(&format!(
                "UPDATE axond_cp_resource_version SET slug = 'edited' \
                 WHERE resource_id = '{}'",
                project.id
            ))
            .await;

        let error = store
            .load_revision(first.id)
            .await
            .expect_err("a resource that is not its own content checksum is not hydratable");
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        assert!(
            matches!(
                **source,
                IntegrityError::ContentMismatch { reference, .. } if reference == project
            ),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn a_body_that_cannot_be_decoded_is_corruption_and_not_an_outage() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        let project = reference(ResourceKind::Project, 2);
        store
            .corrupt_with(&format!(
                "UPDATE axond_cp_resource_version SET body_inline = '\\xdeadbeef'::bytea \
                 WHERE resource_id = '{}'",
                project.id
            ))
            .await;

        // The manifest does not read bodies, so it still loads: corruption is
        // reported by the read that actually touches the corrupt column, and
        // hydration is that read.
        store
            .load_manifest(first.id)
            .await
            .expect("a manifest hydrates no body");
        let error = store
            .load_revision(first.id)
            .await
            .expect_err("a body this build cannot decode is not hydratable");
        assert_eq!(error.category(), FailureCategory::Corrupt);
        assert!(!error.retryable(), "corruption is not cleared by retrying");
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        assert!(
            matches!(**source, IntegrityError::Unreadable { .. }),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn a_row_written_by_a_serializer_this_build_does_not_read_is_refused() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        store
            .corrupt_with(
                "UPDATE axond_cp_resource_version SET serializer = 'axond.desired-state.v99'",
            )
            .await;

        // A version of this encoding this build has not learned: the rows may be
        // entirely intact, and the action is a deployment rather than a restore.
        let error = store
            .load_revision(first.id)
            .await
            .expect_err("a row this build cannot read is not hydratable");
        assert_ne!(error.category(), FailureCategory::Corrupt);
        let ControlPlaneError::Incompatible { source, .. } = &error else {
            panic!("expected an incompatibility, got {error:?}");
        };
        assert!(
            matches!(**source, IntegrityError::UnknownSerializer { .. }),
            "{error:?}"
        );

        // Text naming no version of this encoding at all is the other thing: no
        // release wrote it, so it points at storage.
        store
            .corrupt_with("UPDATE axond_cp_resource_version SET serializer = 'json'")
            .await;
        let error = store
            .load_revision(first.id)
            .await
            .expect_err("a column no build ever wrote is not hydratable");
        assert_eq!(error.category(), FailureCategory::Corrupt);
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        assert!(
            matches!(**source, IntegrityError::Unreadable { .. }),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn a_version_number_the_domain_cannot_hold_is_refused() {
        let Some((store, _, _)) = journal().await else {
            return;
        };
        let (first, _, _) = three_revisions(&store).await;

        // Version numbers start at one, and the DDL says so. This is what a
        // journal looks like after someone removed that guarantee and wrote a
        // zero: a manifest entry naming a version the domain cannot represent.
        let alias = reference(ResourceKind::Alias, 4);
        store
            .corrupt_with(
                "DO $$ DECLARE name text; BEGIN \
                 SELECT conname INTO name FROM pg_constraint \
                 WHERE conrelid = 'axond_cp_revision_entry'::regclass AND contype = 'f' \
                 AND conname LIKE '%resource%'; \
                 EXECUTE format('ALTER TABLE axond_cp_revision_entry DROP CONSTRAINT %I', name); \
                 END $$;",
            )
            .await;
        store
            .corrupt_with(&format!(
                "UPDATE axond_cp_revision_entry SET version = 0 \
                 WHERE resource_id = '{}' AND revision_id = '{revision}'",
                alias.id,
                revision = first.id
            ))
            .await;

        let error = store
            .load_revision(first.id)
            .await
            .expect_err("a version number that is not one is not a version number");
        assert_eq!(error.category(), FailureCategory::Corrupt);
        let ControlPlaneError::Corrupt { source, .. } = &error else {
            panic!("expected corruption, got {error:?}");
        };
        let IntegrityError::Unreadable { detail } = &**source else {
            panic!("expected an unreadable row, got {error:?}");
        };
        assert!(detail.contains("resource version 0"), "{detail}");
    }

    #[tokio::test]
    async fn a_revision_larger_than_the_bound_is_refused_whole() {
        let Some((store, dsn, schema)) = journal().await else {
            return;
        };
        let (first, _, third) = three_revisions(&store).await;

        // Each bound, stated against the same five-resource revision. Every one
        // of them is a refusal: not a truncated manifest, not the resources that
        // fit, and not a candidate missing its blob.
        let cases: [(HydrationLimits, &str); 5] = [
            (
                HydrationLimits {
                    max_entries: 2,
                    ..HydrationLimits::default()
                },
                "resource versions",
            ),
            (
                HydrationLimits {
                    max_blobs: 0,
                    ..HydrationLimits::default()
                },
                "blobs",
            ),
            (
                HydrationLimits {
                    max_blob_bytes: 8,
                    ..HydrationLimits::default()
                },
                "bytes",
            ),
            (
                HydrationLimits {
                    max_dependency_edges: 1,
                    ..HydrationLimits::default()
                },
                "dependency edges",
            ),
            (
                HydrationLimits {
                    max_inline_body_bytes: 4,
                    ..HydrationLimits::default()
                },
                "inline body",
            ),
        ];
        for (limits, expected) in cases {
            let bounded = bounded_store(&dsn, &schema, limits).await;
            let error = bounded
                .load_revision(first.id)
                .await
                .expect_err("a revision past a bound must not hydrate");
            assert_eq!(error.category(), FailureCategory::Denied);
            assert!(!error.retryable(), "a bound is not cleared by retrying");
            assert!(
                matches!(error, ControlPlaneError::TooLarge { revision, .. } if revision == first.id),
                "{error:?}"
            );
            assert!(error.to_string().contains(expected), "{error}");
        }

        // The declared-bytes figure is the revision's real total, not however
        // much of it was counted before the ceiling was crossed: an operator
        // deciding where to put the bound is told the size they have to clear.
        //
        // Stated against a revision declaring *two* blobs, because with one the
        // partial sum and the total are the same number and the assertion would
        // hold under the short-circuiting this pins.
        let two_blobs = state_with_two_blobs();
        let fourth = store
            .publish_revision(candidate(
                ExpectedRevision::Exactly(third.id),
                "fourth",
                two_blobs.clone(),
            ))
            .await
            .expect("fourth publication");
        let sizes: Vec<u64> = two_blobs.blobs().map(|blob| blob.size_bytes).collect();
        assert_eq!(sizes.len(), 2, "the fixture must declare two blobs");
        let total = sizes.iter().sum::<u64>();
        let bounded = bounded_store(
            &dsn,
            &schema,
            HydrationLimits {
                max_blob_bytes: 8,
                ..HydrationLimits::default()
            },
        )
        .await;
        let error = bounded
            .load_manifest(fourth.id)
            .await
            .expect_err("a revision past a bound must not hydrate");
        assert!(error.to_string().contains(&total.to_string()), "{error}");
        // And the figure is not either blob on its own, which is what a sum that
        // stopped at the first row over the ceiling would have reported.
        for size in sizes {
            assert!(
                !error.to_string().contains(&size.to_string()),
                "reported a partial sum: {error}"
            );
        }

        // The candidate bound is the last line: a state that hydrated is still
        // refused if the value itself is larger than this build returns.
        let bounded = bounded_store(
            &dsn,
            &schema,
            HydrationLimits {
                max_state_bytes: 64,
                ..HydrationLimits::default()
            },
        )
        .await;
        let error = bounded
            .load_revision(first.id)
            .await
            .expect_err("a candidate past the bound must not be returned");
        assert!(
            matches!(
                error,
                ControlPlaneError::TooLarge {
                    limit: HydrationLimit::StateBytes { .. },
                    ..
                }
            ),
            "{error:?}"
        );

        // Nothing above changed the journal, and the store whose bounds fit
        // still hydrates the same revision it always did.
        assert_eq!(
            store
                .load_revision(first.id)
                .await
                .expect("hydrate")
                .state(),
            &state()
        );
    }

    #[test]
    fn a_journal_timestamp_survives_a_round_trip_through_the_column_it_is_stored_in() {
        let now = journal_now();
        let since = now.duration_since(UNIX_EPOCH).expect("after the epoch");
        assert_eq!(
            since.subsec_nanos() % 1_000,
            0,
            "a timestamp with sub-microsecond precision cannot be read back as itself"
        );
    }
}