djogi 0.1.0-alpha.4

Model-first web framework for Rust — web-framework-agnostic core; Axum integration opt-in via the `axum` feature flag
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
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
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
//! `db reset` orchestrator — Phase 7 v3 §8 / T8.
//!
//! `db reset` is the destructive triple-gated path: it drops the
//! application database, recreates it, and replays every committed
//! migration found under `migrations/<database>/<app>/`. The triple
//! gate per the v3 §8 brief:
//!
//! 1. `DATABASE_URL` MUST resolve to localhost (reused
//!    [`super::policy::is_localhost_connection`]).
//! 2. `Djogi.toml::profile` MUST NOT equal `"production"`.
//! 3. The caller MUST supply explicit confirmation (a `--yes` flag in
//!    the CLI; programmatic callers pass [`ResetRequest::confirmed`]
//!    `= true`).
//!
//! All three gates are enforced before any I/O. A refusal returns a
//! typed [`ResetError::Refused`] so the operator-facing message is
//! actionable.
//!
//! # Logging-DB isolation
//!
//! Per CLAUDE.md, the CRUD-log and event-log databases survive every
//! `db reset` invocation. Today the runner is single-context (Phase 4)
//! so this module only operates on the application DB; the seam is
//! documented at [`reset_app_database`] for the day the three-database
//! `DjogiContext::pool_for(database)` API lands.
//!
//! # `DROP DATABASE` connection plumbing
//!
//! Postgres refuses to drop the database the current session is
//! connected to. We follow the standard libpq idiom: connect to the
//! `postgres` maintenance database with the same credentials, issue
//! `DROP DATABASE … WITH (FORCE)` (Postgres 13+; we target 18+ per
//! `docs/spec/decisions.md`), then `CREATE DATABASE …`. The forced
//! variant terminates other sessions to avoid the classic "another
//! session is connected" bounce.
//!
//! After recreation the runner re-points at the fresh database via
//! [`crate::pg::pool::DjogiPool::connect`] and replays each migration
//! file pair in HISTORICAL apply order (Codex umbrella U-4 per
//! `docs/spec/configuration.md`). T7's out-of-order policy allows a
//! hotfix migration to apply AFTER a later one, so lexical version
//! sort is NOT a faithful replay of what the live DB experienced.
//! `db reset` pre-flight reads `djogi_schema_migrations.applied_at`
//! BEFORE the drop, then uses that order during replay; versions
//! absent from the historical order (typically disk files added after
//! the last apply) sort lexically afterwards. Fresh DBs with no
//! ledger fall back to lexical sort safely.
//!
//! # Historical-order capture error policy (Codex umbrella round-2 U-6)
//!
//! The pre-flight capture step has TWO qualitatively different
//! failure modes that pre-U-6 collapsed to the same outcome:
//!
//! - **Ledger genuinely missing** (the `pg_class` probe returns
//!   `false`): legitimate fresh-DB fallback. Reset proceeds with an
//!   empty historical map, and `build_replay_plan` falls back to
//!   lexical version sort.
//! - **Anything else** — connection failure, query failure, decode
//!   failure, permission denied: opaque. Reset propagates as
//!   [`ResetError::HistoricalOrderCaptureFailed`] and refuses to
//!   drop / recreate.
//!
//! Pre-U-6 every failure mode swallowed itself via `unwrap_or_default()`
//! at the call site, so a flaky ledger read on a populated DB still
//! triggered the destructive operation. The fix is the
//! [`HistoricalCaptureError`] split: `LedgerMissing` is the only
//! legitimate fall-back signal; `Transient(DjogiError)` propagates.
//!
//! # No regex
//!
//! URL parsing reuses the byte-level extractor in
//! [`super::policy::extract_host`] for the localhost gate, plus a
//! minimal forward-scan helper to split out the `<host>/<dbname>` parts.
//! No regex engine, no regex notation.

// `ResetError` carries an embedded `RunnerError`, which itself embeds
// boxed and string-rich variants; the resulting `Result` payload
// exceeds clippy's default 128-byte threshold for `result_large_err`.
// Boxing the whole error type would force every caller to indirect
// through a heap allocation just to discriminate among the variants —
// the signal-vs-cost tradeoff favours allowing the lint at file
// scope here, mirroring the same `#[allow(clippy::result_large_err)]`
// pattern that `crate::config` and `crate::migrate::projection` use.
#![allow(clippy::result_large_err)]

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use tokio_postgres::NoTls;

use crate::config::MigrateConfig;
use crate::context::DjogiContext;
use crate::error::{DbError, DjogiError};
use crate::pg::pool::DjogiPool;

use super::compose::{
    DATE_ARRAY_HELPER_PRELUDE, NUMERIC_ARRAY_HELPER_PRELUDE, TSTZ_ARRAY_HELPER_PRELUDE,
    date_array_helper_operation, numeric_array_helper_operation, tstz_array_helper_operation,
};
use super::ledger::compute_checksum;
use super::naming::{down_filename, up_filename};
use super::policy::{OutOfOrderPolicy, is_localhost_connection};
use super::projection::BucketKey;
use super::replay_plan::{
    ReplayPlanLoadStatus, find_non_transactional_statement_shape, load_committed_replay_plan,
};
use super::runner::{RunnerCtx, apply_plan};
use super::segment::{MigrationPlan, Segment, SegmentKind};
use super::sql::OperationSql;
use super::target::{app_dirname, bucket_dir, migrations_root};

// ── Public types ──────────────────────────────────────────────────────────

/// Configuration handed to [`reset_app_database`].
///
/// Constructed by the CLI glue from the resolved workspace, the
/// loaded [`crate::config::DjogiConfig`], and the operator's `--yes`
/// flag.
pub struct ResetRequest<'a> {
    /// Workspace root — `migrations/<database>/<app>/` lives below it.
    pub workspace_root: &'a Path,
    /// The operator's full `DATABASE_URL` — used for the localhost
    /// gate and for deriving the maintenance + reconnection URLs.
    pub database_url: &'a str,
    /// `Djogi.toml::profile` — production refuses unconditionally.
    pub profile: &'a str,
    /// `true` when the operator passed `--yes` (or the programmatic
    /// caller has otherwise confirmed). The default is `false`; the
    /// runner refuses without it.
    pub confirmed: bool,
    /// `true` when the operator explicitly accepts replaying
    /// drifted on-disk SQL after the parity preflight reports that
    /// the current files no longer match the live ledger. Default:
    /// `false` — drift refuses before `DROP DATABASE`.
    pub allow_checksum_drift_reset: bool,
    /// Maintenance database name. Defaults to `"postgres"` when
    /// the caller has nothing more specific (the conventional
    /// administrative DB present on every cluster).
    pub maintenance_database: &'a str,
    /// Migration-engine config the runner consults during the replay
    /// phase. Operators rarely override this; the CLI default is the
    /// loaded `Djogi.toml::migrate` block.
    pub migrate_config: MigrateConfig,
    /// Optional pool pointing at the **audit DB** (`crud_log_url` in
    /// `Djogi.toml`, by default `crud_log` derived from
    /// `database.url`). When `Some`, every replayed migration writes
    /// one `djogi_ddl_audit` row per executed (non-metadata) segment
    /// so the audit trail captures the post-reset apply just as a
    /// regular `apply` would. When `None` the audit write is silently
    /// skipped — appropriate for adopters who have not yet provisioned
    /// the second DB OR for tests that only care about the app-side
    /// replay.
    ///
    /// **Why a raw `deadpool_postgres::Pool`:** mirrors
    /// [`super::runner::RunnerCtx::audit_pool`] so the replay
    /// orchestrator can pass the pool through without re-wrapping.
    /// See the doc on `RunnerCtx::audit_pool` for the rationale (the
    /// audit pool is internal substrate; `DjogiPool`'s wider invariants
    /// such as post-connect callbacks and status reporting are not
    /// needed for the audit-side context the runner builds).
    ///
    /// **Construction.** Production callers build this via
    /// [`super::resolve_audit_url`] + [`super::build_audit_pool`]. The
    /// CLI's `db reset` glue degrades to `None` (with a warn log) if
    /// audit URL resolution or pool construction fails — losing the
    /// audit row is preferable to refusing the destructive operation
    /// over a sibling-DB outage. Tests typically pass `None` unless
    /// they explicitly want to assert the per-segment audit-row
    /// behaviour.
    pub audit_pool: Option<deadpool_postgres::Pool>,
}

/// Successful-reset report. Names every replayed migration so the
/// operator can confirm the post-reset state matches expectation.
#[derive(Debug, Clone)]
pub struct ResetReport {
    /// The application database name that was dropped + recreated.
    pub database: String,
    /// One entry per replayed migration version in apply order.
    pub replayed_versions: Vec<ReplayedMigration>,
}

/// Per-replay record.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplayedMigration {
    /// Bucket — `(database, app)` identity.
    pub bucket: BucketKey,
    /// Version id — `V<ts>__<slug>`.
    pub version: String,
}

/// Which file side a checksum-parity issue concerns.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResetSqlSide {
    Up,
    Down,
}

impl ResetSqlSide {
    const fn as_str(self) -> &'static str {
        match self {
            ResetSqlSide::Up => "up",
            ResetSqlSide::Down => "down",
        }
    }
}

/// Why checksum parity could not be satisfied for one historical row.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResetChecksumParityProblem {
    Drift,
    MissingFile,
    UnsupportedBaseline,
}

/// One checksum-parity issue found before the destructive reset step.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResetChecksumParityIssue {
    /// Bucket whose current on-disk SQL no longer matches the live ledger.
    pub bucket: BucketKey,
    /// Migration version carrying the mismatch.
    pub version: String,
    /// Whether the issue concerns `up.sdjql` or `down.sdjql`.
    pub sql_side: ResetSqlSide,
    /// Checksum recorded on the live ledger before reset.
    pub ledger_checksum: String,
    /// Checksum computed from the current on-disk file, or `None`
    /// when the expected file is missing.
    pub on_disk_checksum: Option<String>,
    /// Why the parity preflight refused this historical row.
    pub problem: ResetChecksumParityProblem,
}

/// Why reset cannot prove faithful replay semantics from the committed
/// migration artifacts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResetReplaySemanticsProblem {
    MissingReplayPlan,
    InvalidReplayPlan,
}

/// One replay-semantics issue found before the destructive reset step.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResetReplaySemanticsIssue {
    /// Bucket carrying the affected migration.
    pub bucket: BucketKey,
    /// Migration version that cannot be replayed safely from disk.
    pub version: String,
    /// Statement class that requires non-transactional replay semantics.
    pub statement_shape: String,
    /// Why reset could not recover a committed replay plan.
    pub problem: ResetReplaySemanticsProblem,
}

/// Errors surfaced by [`reset_app_database`].
#[derive(Debug)]
pub enum ResetError {
    /// One of the three triple-gates rejected the request. The
    /// embedded variant names which gate refused so the operator
    /// message is precise.
    Refused(ResetRefusal),
    /// Connecting to the maintenance database failed (admin URL
    /// unreachable, credentials wrong, ssl handshake failed, …).
    MaintenanceConnectFailed { source: DjogiError },
    /// `DROP DATABASE` or `CREATE DATABASE` returned an error from the
    /// server — typically permission denied (the connecting role lacks
    /// CREATEDB) or another session is still connected.
    MaintenanceSqlFailed { sql: String, source: DjogiError },
    /// Connecting to the freshly-created database failed.
    AppConnectFailed { source: DjogiError },
    /// Walking `migrations/<database>/` failed (I/O error reading the
    /// committed migration tree).
    MigrationScanFailed {
        path: PathBuf,
        source: std::io::Error,
    },
    /// Reading one of the on-disk SQL files failed.
    SqlReadFailed {
        path: PathBuf,
        source: std::io::Error,
    },
    /// Replay of one migration via [`apply_plan`] failed. Carries the
    /// version id and the underlying runner error so the operator can
    /// see which migration broke the replay.
    ReplayFailed {
        version: String,
        source: super::runner::RunnerError,
    },
    /// The supplied `DATABASE_URL` is missing the database-name
    /// component that `db reset` would otherwise drop. Surfaces a
    /// typed error rather than a panic deep inside `replace_db_in_url`.
    DatabaseUrlMalformed { database_url: String },
    /// The decoded database name is not a valid Postgres identifier.
    /// Defence-in-depth against URL-injection — we percent-decode the
    /// path component first, then check the resulting bytes match the
    /// strict grammar (ASCII letter or underscore, followed by ASCII
    /// alphanumerics / underscores, up to 63 bytes). Anything else
    /// refuses BEFORE we splice the value into `DROP DATABASE` /
    /// `CREATE DATABASE` DDL. Surfaced separately from
    /// `DatabaseUrlMalformed` so the operator can tell "no database
    /// component" from "the component decodes to something we won't
    /// quote into DDL".
    InvalidDatabaseName { name: String },
    /// Workspace lock acquisition failed before the replay could run.
    WorkspaceLockFailed { source: super::guard::GuardError },
    /// Codex umbrella round-2 U-6: capturing the live ledger's
    /// historical apply order failed for a reason that is NOT
    /// "ledger table is missing on a fresh DB". Pre-fix every
    /// failure mode of the capture step (connection error, decode
    /// error, generic SQL error, …) collapsed to an empty map via
    /// `unwrap_or_default()`, which silently fell through to the
    /// drop / recreate path on a transient error. That re-opens the
    /// U-4 hazard: a flaky ledger read that swallows itself, then
    /// the destructive operation runs anyway against a database
    /// whose true state we never confirmed.
    ///
    /// Post-fix: the ONLY legitimate fall-back-to-lexical signal is
    /// the `pg_class` probe returning `false` (genuinely fresh DB
    /// or freshly-recreated DB without bootstrap yet). Every other
    /// failure mode propagates through this variant, refusing the
    /// destructive operation. The operator-facing message names the
    /// underlying `DjogiError` so the failure point is unambiguous.
    HistoricalOrderCaptureFailed { source: DjogiError },
}

/// Specific refusal kind for [`ResetError::Refused`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResetRefusal {
    /// `DATABASE_URL` does not resolve to localhost.
    NotLocalhost { database_url: String },
    /// `Djogi.toml::profile = "production"`.
    ProductionProfile { profile: String },
    /// The caller did not supply explicit confirmation.
    NotConfirmed,
    /// The live ledger's checksums do not match the current on-disk
    /// migration files. Reset refuses before `DROP DATABASE` unless
    /// the operator explicitly overrides the drift gate.
    ChecksumParity {
        issues: Vec<ResetChecksumParityIssue>,
    },
    /// Reset cannot prove faithful replay semantics for at least one
    /// committed migration before the destructive drop/recreate.
    ReplaySemantics {
        issues: Vec<ResetReplaySemanticsIssue>,
    },
}

impl std::fmt::Display for ResetError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ResetError::Refused(r) => write!(f, "db reset refused: {r}"),
            ResetError::MaintenanceConnectFailed { source } => write!(
                f,
                "db reset: connect to maintenance database failed: {source}"
            ),
            ResetError::MaintenanceSqlFailed { sql, source } => {
                write!(f, "db reset: maintenance SQL `{sql}` failed: {source}")
            }
            ResetError::AppConnectFailed { source } => {
                write!(
                    f,
                    "db reset: connect to fresh app database failed: {source}"
                )
            }
            ResetError::MigrationScanFailed { path, source } => write!(
                f,
                "db reset: scanning {} for migration files failed: {source}",
                path.display(),
            ),
            ResetError::SqlReadFailed { path, source } => write!(
                f,
                "db reset: reading migration SQL at {} failed: {source}",
                path.display(),
            ),
            ResetError::ReplayFailed { version, source } => write!(
                f,
                "db reset: replay of `{version}` failed: {source}; the database \
                 has been recreated but is now in a partial state — fix the \
                 underlying issue and re-run db reset",
            ),
            ResetError::DatabaseUrlMalformed { database_url } => write!(
                f,
                "db reset: DATABASE_URL `{database_url}` does not contain a \
                 database-name component (no `/` after the host); db reset \
                 cannot derive the database name to drop"
            ),
            ResetError::InvalidDatabaseName { name } => write!(
                f,
                "db reset: decoded database name `{name}` is not a valid \
                 Postgres identifier (expected: ASCII letter or underscore, \
                 followed by ASCII alphanumerics or underscores, up to 63 \
                 bytes); db reset refuses to splice arbitrary bytes into \
                 DROP DATABASE / CREATE DATABASE DDL"
            ),
            ResetError::WorkspaceLockFailed { source } => {
                write!(f, "db reset: workspace lock acquisition failed: {source}")
            }
            ResetError::HistoricalOrderCaptureFailed { source } => write!(
                f,
                "db reset: capturing the live ledger's historical apply order \
                 failed: {source}; refusing to proceed with the destructive \
                 drop / recreate because we cannot confirm the live state — \
                 fix the underlying connection / query failure and re-run, or \
                 (if the database genuinely does not exist yet) create it \
                 first then re-run db reset"
            ),
        }
    }
}

impl std::fmt::Display for ResetRefusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ResetRefusal::NotLocalhost { database_url } => write!(
                f,
                "DATABASE_URL is not localhost (got `{database_url}`); db reset is \
                 a destructive operation and must not be invoked against a remote \
                 database"
            ),
            ResetRefusal::ProductionProfile { profile } => write!(
                f,
                "Djogi.toml::profile = `{profile}`; db reset refuses to run on a \
                 production profile"
            ),
            ResetRefusal::NotConfirmed => f.write_str(
                "db reset requires explicit confirmation — pass `--yes` (or set \
                 ResetRequest::confirmed = true) to acknowledge that the entire \
                 application database will be dropped",
            ),
            ResetRefusal::ChecksumParity { issues } => {
                let rendered = issues
                    .iter()
                    .map(render_checksum_parity_issue)
                    .collect::<Vec<_>>()
                    .join("; ");
                write!(
                    f,
                    "db reset checksum parity preflight found drift against the live ledger: \
                     {rendered}; refusing destructive drop / recreate unless you pass \
                     `--allow-checksum-drift-reset` (or set \
                     `ResetRequest::allow_checksum_drift_reset = true`)"
                )
            }
            ResetRefusal::ReplaySemantics { issues } => {
                let rendered = issues
                    .iter()
                    .map(render_replay_semantics_issue)
                    .collect::<Vec<_>>()
                    .join("; ");
                write!(
                    f,
                    "db reset cannot prove faithful replay semantics for at least one committed \
                     migration: {rendered}; refusing destructive drop / recreate until the migration \
                     has a committed replay manifest or is replay-safe as a single transactional plan"
                )
            }
        }
    }
}

impl std::error::Error for ResetError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ResetError::MaintenanceConnectFailed { source } => Some(source),
            ResetError::MaintenanceSqlFailed { source, .. } => Some(source),
            ResetError::AppConnectFailed { source } => Some(source),
            ResetError::MigrationScanFailed { source, .. } => Some(source),
            ResetError::SqlReadFailed { source, .. } => Some(source),
            ResetError::ReplayFailed { source, .. } => Some(source),
            ResetError::WorkspaceLockFailed { source } => Some(source),
            ResetError::HistoricalOrderCaptureFailed { source } => Some(source),
            _ => None,
        }
    }
}

// ── Public entry point ────────────────────────────────────────────────────

/// Drop, recreate, and replay every committed migration against the
/// application database in `req.database_url`.
///
/// Triple-gated per the module docs. Returns a [`ResetReport`] on
/// success or a [`ResetError`] on any failure mode — including a
/// gate refusal, which is surfaced as `ResetError::Refused` rather
/// than as a successful no-op.
pub async fn reset_app_database(req: ResetRequest<'_>) -> Result<ResetReport, ResetError> {
    // 1. Triple gate — every gate runs BEFORE any I/O so a refusal
    //    leaves zero side effects on the workspace OR the database.
    if !is_localhost_connection(req.database_url) {
        return Err(ResetError::Refused(ResetRefusal::NotLocalhost {
            database_url: req.database_url.to_string(),
        }));
    }
    if req.profile == "production" {
        return Err(ResetError::Refused(ResetRefusal::ProductionProfile {
            profile: req.profile.to_string(),
        }));
    }
    if !req.confirmed {
        return Err(ResetError::Refused(ResetRefusal::NotConfirmed));
    }

    // 2. Derive the app database name and the maintenance URL.
    //
    //    Two-step parse: first extract+percent-decode the path
    //    component, then validate the decoded bytes against the strict
    //    Postgres-identifier grammar. We refuse weird-looking names
    //    BEFORE splicing them into `DROP DATABASE` / `CREATE DATABASE`
    //    DDL — defence-in-depth against URL-injection where a crafted
    //    URL like `postgres://localhost/'; DROP TABLE foo; --` could
    //    otherwise reach the DDL builder. The maintenance database
    //    name flows from operator config (`--maintenance-database`,
    //    default `postgres`) so it's validated separately.
    let database =
        extract_database_from_url(req.database_url).ok_or(ResetError::DatabaseUrlMalformed {
            database_url: req.database_url.to_string(),
        })?;
    if !is_valid_pg_identifier(&database) {
        return Err(ResetError::InvalidDatabaseName { name: database });
    }
    if !is_valid_pg_identifier(req.maintenance_database) {
        return Err(ResetError::InvalidDatabaseName {
            name: req.maintenance_database.to_string(),
        });
    }
    let maintenance_url = replace_db_in_url(req.database_url, req.maintenance_database).ok_or(
        ResetError::DatabaseUrlMalformed {
            database_url: req.database_url.to_string(),
        },
    )?;

    // 3. Acquire workspace lock — replay mutates ledger state on the
    //    fresh DB; concurrent compose / apply / repair operations
    //    against the same workspace must not interleave with reset.
    let lock_path = req.workspace_root.join(super::guard::LOCK_FILE_NAME);
    let _guard = super::guard::acquire(&lock_path, super::guard::DEFAULT_TIMEOUT)
        .map_err(|e| ResetError::WorkspaceLockFailed { source: e })?;

    // 4. Codex umbrella U-4: capture the HISTORICAL apply order from
    //    the live ledger BEFORE the drop. T7's out-of-order policy
    //    allows a hotfix migration to apply AFTER a later one, e.g.
    //    `applied_at` of `0001 < 0003 < 0002`. Lexical version-string
    //    sort would replay them as `0001, 0002, 0003`, which is NOT
    //    the sequence the live database actually experienced. If
    //    `0002` only succeeded historically because `0003` was
    //    already in place, lexical replay would re-apply it
    //    out-of-order on a fresh DB — different state from what we
    //    just dropped.
    //
    //    Strategy: pre-flight a read-only connection to the live DB,
    //    query `djogi_schema_migrations` ordered by `applied_at`, and
    //    capture `(bucket, version) -> rank`. We then use that rank
    //    as the replay sort key. Versions absent from the historical
    //    order (e.g. files added on disk after the last apply) sort
    //    AFTER any historical entry, lexically among themselves.
    //
    //    Codex umbrella round-2 U-6 — error-policy split:
    //    `HistoricalCaptureError::LedgerMissing` is the ONLY legitimate
    //    fall-back-to-lexical signal (`pg_class` probe returned false:
    //    genuinely fresh DB). Every OTHER failure mode (connection
    //    failure, decode failure, generic SQL error, permission
    //    denied) surfaces as `Transient(..)` and propagates through
    //    `ResetError::HistoricalOrderCaptureFailed`. Pre-U-6 every
    //    error collapsed to `()` and the reset proceeded with an
    //    empty map — which re-opened the U-4 hazard for transient
    //    failures (the empty map masquerades as "fresh DB with no
    //    history" and the destructive drop / recreate runs anyway).
    let historical_entries = match capture_historical_replay_entries(req.database_url).await {
        Ok(entries) => entries,
        Err(HistoricalCaptureError::LedgerMissing) => Vec::new(),
        Err(HistoricalCaptureError::Transient(e)) => {
            return Err(ResetError::HistoricalOrderCaptureFailed { source: e });
        }
    };
    preflight_reset_checksum_parity(
        req.workspace_root,
        &database,
        &historical_entries,
        req.allow_checksum_drift_reset,
    )?;
    preflight_reset_replay_semantics(req.workspace_root, &database)?;
    let historical_order = build_historical_order(&historical_entries);

    // 5. Drop + recreate the application database via the maintenance
    //    connection. A fresh tokio_postgres client is opened just for
    //    the two DDLs — the maintenance pool is intentionally NOT
    //    cached because db reset is interactive / one-shot.
    drop_and_create_database(&maintenance_url, &database).await?;

    // 6. Connect to the freshly-created application DB and replay
    //    every committed migration.
    let pool = DjogiPool::connect(req.database_url)
        .await
        .map_err(|e| ResetError::AppConnectFailed { source: e })?;
    let mut ctx = DjogiContext::from_pool(pool);

    let buckets = scan_committed_migrations(req.workspace_root, &database)?;
    // Codex umbrella U-4: replay order = historical apply order
    // (`applied_at` ascending) for versions that have a historical
    // entry; lexical-after-historical for versions that do not.
    let replay_plan = build_replay_plan(&buckets, &historical_order);
    let mut replayed: Vec<ReplayedMigration> = Vec::new();

    for (bucket, version) in replay_plan {
        replay_one_migration(
            &mut ctx,
            req.workspace_root,
            &bucket,
            &version,
            &req.migrate_config,
            &_guard,
            req.audit_pool.as_ref(),
        )
        .await?;
        replayed.push(ReplayedMigration {
            bucket: bucket.clone(),
            version,
        });
    }

    Ok(ResetReport {
        database,
        replayed_versions: replayed,
    })
}

/// Internal error classifier for [`capture_historical_apply_order`]
/// per Codex umbrella round-2 U-6.
///
/// The capture step has two qualitatively different failure modes:
///
/// - **`LedgerMissing`** — the `pg_class` probe came back `false`.
///   The connection succeeded, the catalog query succeeded, and the
///   answer was "no `djogi_schema_migrations` table here". This is
///   the legitimate fresh-DB / freshly-recreated-DB signal. The
///   caller falls back to lexical sort and the destructive drop /
///   recreate proceeds.
/// - **`Transient(DjogiError)`** — anything else: tokio_postgres
///   connect failure (DB unreachable, auth fail, network drop, DB
///   does not exist), `current_database()` query failure, probe
///   query failure, decode error, generic SELECT failure. None of
///   these prove the DB is fresh; they prove we cannot CONFIRM the
///   live state. The caller propagates as
///   `ResetError::HistoricalOrderCaptureFailed` and refuses to
///   drop / recreate.
///
/// Pre-U-6 the helper returned `Result<_, ()>` and `unwrap_or_default()`
/// at the call site collapsed every failure mode to "empty map →
/// proceed with lexical fallback". That re-opened the U-4 hazard
/// under a transient connection / query failure: the destructive
/// path runs against a database whose history we never read.
#[derive(Debug)]
enum HistoricalCaptureError {
    /// `pg_class` probe returned `false` — ledger genuinely absent.
    LedgerMissing,
    /// Connection / query / decode failure — treat as opaque, do
    /// NOT proceed with the destructive operation.
    Transient(DjogiError),
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct HistoricalReplayEntry {
    bucket: BucketKey,
    version: String,
    status: String,
    checksum_up: String,
    checksum_down: Option<String>,
}

/// Codex umbrella U-4 + round-2 U-6 — capture the historical apply
/// order from the live ledger before the drop.
///
/// Connects to the application DB at `database_url`, probes for the
/// presence of `djogi_schema_migrations`, and (when present) queries
/// it ordered by `applied_at ASC, id ASC`. Returns a
/// `(bucket, version) -> rank` map where lower ranks applied first
/// historically.
///
/// Per U-6, the error classification is intentional and load-bearing:
///
/// - Probe says ledger absent → `Err(HistoricalCaptureError::LedgerMissing)`
///   (caller falls back to lexical).
/// - Anything else → `Err(HistoricalCaptureError::Transient(..))`
///   (caller propagates and refuses the destructive drop).
///
/// Only `Applied` / `Faked` / `Baseline` rows participate — `Pending`,
/// `Failed`, `RolledBack` do not represent migrations whose effect
/// the live DB carries forward.
#[cfg(test)]
#[allow(clippy::disallowed_methods)]
async fn capture_historical_apply_order(
    database_url: &str,
) -> Result<BTreeMap<(BucketKey, String), u64>, HistoricalCaptureError> {
    let entries = capture_historical_replay_entries(database_url).await?;
    Ok(build_historical_order(&entries))
}

fn build_historical_order(entries: &[HistoricalReplayEntry]) -> BTreeMap<(BucketKey, String), u64> {
    entries
        .iter()
        .enumerate()
        .map(|(rank, entry)| ((entry.bucket.clone(), entry.version.clone()), rank as u64))
        .collect()
}

#[allow(clippy::disallowed_methods)]
async fn capture_historical_replay_entries(
    database_url: &str,
) -> Result<Vec<HistoricalReplayEntry>, HistoricalCaptureError> {
    let (client, conn) = tokio_postgres::connect(database_url, NoTls)
        .await
        .map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "tokio-postgres connect failed during historical-order capture: {e}"
            ))))
        })?;
    let driver = tokio::spawn(async move {
        if let Err(e) = conn.await {
            tracing::debug!("[db reset] historical-order driver: {e}");
        }
    });

    // Resolve the active database name so the captured map's bucket
    // identity matches what `scan_committed_migrations` produces.
    let db_row = client
        .query_one("SELECT current_database()::text", &[])
        .await
        .map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "current_database() failed during historical-order capture: {e}"
            ))))
        })?;
    let database: String = db_row.try_get(0).map_err(|e| {
        HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
            "decoding current_database() result failed: {e}"
        ))))
    })?;

    // Probe the ledger table. THIS is the canonical fresh-DB decision
    // point: the connection succeeded AND the catalog query returned
    // a typed answer. A `false` here means the ledger has not been
    // bootstrapped yet — legitimate fresh-DB fallback. A failure of
    // the probe itself is opaque and propagates as Transient.
    let probe = client
        .query_one(
            "SELECT EXISTS(SELECT 1 FROM pg_class \
             WHERE relname = 'djogi_schema_migrations' AND relkind = 'r')",
            &[],
        )
        .await
        .map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "pg_class probe for djogi_schema_migrations failed: {e}"
            ))))
        })?;
    let exists: bool = probe.try_get(0).map_err(|e| {
        HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
            "decoding pg_class probe result failed: {e}"
        ))))
    })?;
    if !exists {
        drop(client);
        let _ = driver.await;
        return Err(HistoricalCaptureError::LedgerMissing);
    }

    let rows = client
        .query(
            "SELECT version, app_label, status, checksum_up, checksum_down \
             FROM djogi_schema_migrations \
             WHERE status IN ('applied', 'faked', 'baseline') \
             ORDER BY applied_at ASC, id ASC",
            &[],
        )
        .await
        .map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "SELECT djogi_schema_migrations failed during historical-order capture: {e}"
            ))))
        })?;

    let mut out: Vec<HistoricalReplayEntry> = Vec::with_capacity(rows.len());
    for (rank, row) in rows.iter().enumerate() {
        let version: String = row.try_get("version").map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "decoding ledger version column failed at rank {rank}: {e}"
            ))))
        })?;
        let app_label: String = row.try_get("app_label").map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "decoding ledger app_label column failed at rank {rank}: {e}"
            ))))
        })?;
        let status: String = row.try_get("status").map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "decoding ledger status column failed at rank {rank}: {e}"
            ))))
        })?;
        let checksum_up: String = row.try_get("checksum_up").map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "decoding ledger checksum_up column failed at rank {rank}: {e}"
            ))))
        })?;
        let checksum_down: Option<String> = row.try_get("checksum_down").map_err(|e| {
            HistoricalCaptureError::Transient(DjogiError::Db(DbError::other(format!(
                "decoding ledger checksum_down column failed at rank {rank}: {e}"
            ))))
        })?;
        out.push(HistoricalReplayEntry {
            bucket: BucketKey {
                database: database.clone(),
                app: app_label,
            },
            version,
            status,
            checksum_up,
            checksum_down,
        });
    }
    drop(client);
    let _ = driver.await;
    Ok(out)
}

fn preflight_reset_checksum_parity(
    workspace_root: &Path,
    database: &str,
    historical_entries: &[HistoricalReplayEntry],
    allow_checksum_drift_reset: bool,
) -> Result<(), ResetError> {
    let issues = collect_checksum_parity_issues(workspace_root, database, historical_entries)?;
    if issues.is_empty() || allow_checksum_drift_reset {
        return Ok(());
    }
    Err(ResetError::Refused(ResetRefusal::ChecksumParity { issues }))
}

fn collect_checksum_parity_issues(
    workspace_root: &Path,
    database: &str,
    historical_entries: &[HistoricalReplayEntry],
) -> Result<Vec<ResetChecksumParityIssue>, ResetError> {
    let on_disk = super::target::scan_filesystem_with_files(workspace_root, Some(database))
        .map_err(|err| ResetError::MigrationScanFailed {
            path: migrations_root(workspace_root).join(database),
            source: err,
        })?;
    let mut issues = Vec::new();

    for entry in historical_entries {
        if entry.status == "baseline" {
            issues.push(ResetChecksumParityIssue {
                bucket: entry.bucket.clone(),
                version: entry.version.clone(),
                sql_side: ResetSqlSide::Up,
                ledger_checksum: entry.checksum_up.clone(),
                on_disk_checksum: None,
                problem: ResetChecksumParityProblem::UnsupportedBaseline,
            });
            continue;
        }

        let up_path = on_disk
            .get(&entry.bucket)
            .and_then(|versions| versions.get(&entry.version))
            .cloned()
            .unwrap_or_else(|| {
                bucket_dir(workspace_root, &entry.bucket).join(up_filename(&entry.version))
            });
        push_checksum_issue_if_needed(
            &mut issues,
            entry,
            ResetSqlSide::Up,
            &entry.checksum_up,
            &up_path,
        )?;

        if let Some(ledger_checksum_down) = entry.checksum_down.as_deref() {
            let down_path =
                bucket_dir(workspace_root, &entry.bucket).join(down_filename(&entry.version));
            push_checksum_issue_if_needed(
                &mut issues,
                entry,
                ResetSqlSide::Down,
                ledger_checksum_down,
                &down_path,
            )?;
        }
    }

    Ok(issues)
}

fn push_checksum_issue_if_needed(
    issues: &mut Vec<ResetChecksumParityIssue>,
    entry: &HistoricalReplayEntry,
    sql_side: ResetSqlSide,
    ledger_checksum: &str,
    path: &Path,
) -> Result<(), ResetError> {
    let on_disk_sql = match fs::read_to_string(path) {
        Ok(sql) => sql,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            issues.push(ResetChecksumParityIssue {
                bucket: entry.bucket.clone(),
                version: entry.version.clone(),
                sql_side,
                ledger_checksum: ledger_checksum.to_string(),
                on_disk_checksum: None,
                problem: ResetChecksumParityProblem::MissingFile,
            });
            return Ok(());
        }
        Err(e) => {
            return Err(ResetError::SqlReadFailed {
                path: path.to_path_buf(),
                source: e,
            });
        }
    };
    let on_disk_checksum = compute_committed_sql_checksum(&on_disk_sql, sql_side);
    if on_disk_checksum != ledger_checksum {
        issues.push(ResetChecksumParityIssue {
            bucket: entry.bucket.clone(),
            version: entry.version.clone(),
            sql_side,
            ledger_checksum: ledger_checksum.to_string(),
            on_disk_checksum: Some(on_disk_checksum),
            problem: ResetChecksumParityProblem::Drift,
        });
    }
    Ok(())
}

fn render_checksum_parity_issue(issue: &ResetChecksumParityIssue) -> String {
    let version_path = format!(
        "{}/{}/{}",
        issue.bucket.database,
        app_dirname(&issue.bucket.app),
        issue.version
    );
    match issue.problem {
        ResetChecksumParityProblem::Drift => format!(
            "{version_path} {} checksum drift: ledger `{}` vs on-disk `{}`",
            issue.sql_side.as_str(),
            issue.ledger_checksum,
            issue.on_disk_checksum.as_deref().unwrap_or("<missing>")
        ),
        ResetChecksumParityProblem::MissingFile => format!(
            "{version_path} {} file missing: ledger checksum `{}` has no on-disk peer",
            issue.sql_side.as_str(),
            issue.ledger_checksum
        ),
        ResetChecksumParityProblem::UnsupportedBaseline => format!(
            "{version_path} baseline checksum `{}` cannot be compared to migration file bytes; \
             db reset cannot establish safe parity for a baseline row",
            issue.ledger_checksum
        ),
    }
}

fn preflight_reset_replay_semantics(
    workspace_root: &Path,
    database: &str,
) -> Result<(), ResetError> {
    let buckets = scan_committed_migrations(workspace_root, database)?;
    let mut issues = Vec::new();

    for (bucket, versions) in &buckets {
        for version in versions {
            let replay_sql = read_replay_sql_files(workspace_root, bucket, version)?;
            let plan_status =
                replay_plan_status_for_reset(workspace_root, bucket, version, &replay_sql);
            if let Some(issue) =
                replay_semantics_issue_for_plan_status(bucket, version, &replay_sql, &plan_status)
            {
                issues.push(issue);
            }
        }
    }

    if issues.is_empty() {
        Ok(())
    } else {
        Err(ResetError::Refused(ResetRefusal::ReplaySemantics {
            issues,
        }))
    }
}

fn replay_plan_status_for_reset(
    workspace_root: &Path,
    bucket: &BucketKey,
    version: &str,
    replay_sql: &ReplaySqlFiles,
) -> ReplayPlanLoadStatus {
    load_committed_replay_plan(
        workspace_root,
        bucket,
        version,
        &replay_sql.checksum_up,
        replay_sql.checksum_down.as_deref(),
    )
}

fn load_reset_replay_plan(
    workspace_root: &Path,
    bucket: &BucketKey,
    version: &str,
    replay_sql: &ReplaySqlFiles,
) -> Result<Option<MigrationPlan>, ResetError> {
    let plan_status = replay_plan_status_for_reset(workspace_root, bucket, version, replay_sql);
    if let Some(issue) =
        replay_semantics_issue_for_plan_status(bucket, version, replay_sql, &plan_status)
    {
        return Err(ResetError::Refused(ResetRefusal::ReplaySemantics {
            issues: vec![issue],
        }));
    }
    match plan_status {
        ReplayPlanLoadStatus::Loaded(plan) => Ok(Some(plan)),
        ReplayPlanLoadStatus::Missing | ReplayPlanLoadStatus::Invalid(_) => Ok(None),
    }
}

fn replay_semantics_issue_for_plan_status(
    bucket: &BucketKey,
    version: &str,
    replay_sql: &ReplaySqlFiles,
    plan_status: &ReplayPlanLoadStatus,
) -> Option<ResetReplaySemanticsIssue> {
    let statement_shape = find_non_transactional_statement_shape(&replay_sql.up_sql)?;
    let problem = match plan_status {
        ReplayPlanLoadStatus::Loaded(_) => return None,
        ReplayPlanLoadStatus::Missing => ResetReplaySemanticsProblem::MissingReplayPlan,
        ReplayPlanLoadStatus::Invalid(_) => ResetReplaySemanticsProblem::InvalidReplayPlan,
    };
    Some(ResetReplaySemanticsIssue {
        bucket: bucket.clone(),
        version: version.to_string(),
        statement_shape: statement_shape.to_string(),
        problem,
    })
}

fn render_replay_semantics_issue(issue: &ResetReplaySemanticsIssue) -> String {
    let version_path = format!(
        "{}/{}/{}",
        issue.bucket.database,
        app_dirname(&issue.bucket.app),
        issue.version
    );
    match issue.problem {
        ResetReplaySemanticsProblem::MissingReplayPlan => format!(
            "{version_path} contains `{}` but has no committed replay manifest",
            issue.statement_shape
        ),
        ResetReplaySemanticsProblem::InvalidReplayPlan => format!(
            "{version_path} contains `{}` but its committed replay manifest is missing or stale",
            issue.statement_shape
        ),
    }
}

/// Codex umbrella U-4 — given the on-disk bucket map and the captured
/// historical apply order, produce the deterministic replay plan as a
/// flat `Vec<(BucketKey, String)>` in the order migrations should be
/// re-applied.
///
/// **Sort key** (lower wins): `(historical_rank.unwrap_or(u64::MAX),
/// bucket.database, bucket.app, version)`. Versions WITH a historical
/// rank apply first (in apply-order); versions WITHOUT (typically
/// disk files added after the last historical apply) apply last,
/// sorted lexically among themselves so re-running the reset
/// produces byte-identical output.
///
/// Pulled out as a free function so unit tests can pin every edge
/// case without standing up a live connection.
fn build_replay_plan(
    buckets: &BTreeMap<BucketKey, Vec<String>>,
    historical_order: &BTreeMap<(BucketKey, String), u64>,
) -> Vec<(BucketKey, String)> {
    let mut flat: Vec<(BucketKey, String)> = Vec::new();
    for (bucket, versions) in buckets {
        for v in versions {
            flat.push((bucket.clone(), v.clone()));
        }
    }
    flat.sort_by(|a, b| {
        let ra = historical_order
            .get(&(a.0.clone(), a.1.clone()))
            .copied()
            .unwrap_or(u64::MAX);
        let rb = historical_order
            .get(&(b.0.clone(), b.1.clone()))
            .copied()
            .unwrap_or(u64::MAX);
        ra.cmp(&rb)
            .then_with(|| a.0.database.cmp(&b.0.database))
            .then_with(|| a.0.app.cmp(&b.0.app))
            .then_with(|| a.1.cmp(&b.1))
    });
    flat
}

// ── Internals ─────────────────────────────────────────────────────────────

/// Tokio-postgres-based DROP + CREATE helper. Connects to the
/// maintenance database, issues both statements via `batch_execute`
/// (the simple-query protocol — Postgres refuses to prepare DROP /
/// CREATE DATABASE), and returns once both succeed.
#[allow(clippy::disallowed_methods)]
async fn drop_and_create_database(maintenance_url: &str, database: &str) -> Result<(), ResetError> {
    let (client, conn) = tokio_postgres::connect(maintenance_url, NoTls)
        .await
        .map_err(|e| ResetError::MaintenanceConnectFailed {
            source: DjogiError::Db(DbError::other(format!(
                "tokio-postgres connect to maintenance DB failed: {e}"
            ))),
        })?;
    // The connection task must run for the lifetime of the client.
    let driver = tokio::spawn(async move {
        if let Err(e) = conn.await {
            tracing::error!("[db reset] maintenance connection error: {e}");
        }
    });

    // Quote the database name for safety. Postgres identifier rules
    // allow `"` to appear inside a quoted identifier only as an
    // escaped `""` pair. We replace each `"` byte with `""` so an
    // operator who somehow has a quote in the database name still
    // gets a syntactically-valid identifier; in practice the database
    // grammar (set by the connection URL parser upstream) precludes
    // that, but the defensive escape is free.
    let quoted_db = quote_identifier(database);

    let drop_sql = format!("DROP DATABASE IF EXISTS {quoted_db} WITH (FORCE)");
    client
        .batch_execute(&drop_sql)
        .await
        .map_err(|e| ResetError::MaintenanceSqlFailed {
            sql: drop_sql.clone(),
            source: DjogiError::Db(DbError::other(format!("{e}"))),
        })?;

    let create_sql = format!("CREATE DATABASE {quoted_db}");
    client
        .batch_execute(&create_sql)
        .await
        .map_err(|e| ResetError::MaintenanceSqlFailed {
            sql: create_sql.clone(),
            source: DjogiError::Db(DbError::other(format!("{e}"))),
        })?;

    // Drop the client so the connection task finishes; await the task
    // so the connection close lands deterministically.
    drop(client);
    let _ = driver.await;
    Ok(())
}

/// Walk `migrations/<database>/` and collect every committed
/// `V<ts>__<slug>.sql` migration grouped by `(database, app)` bucket.
///
/// Returns a `BTreeMap` so iteration order is deterministic across
/// runs — key order is `(database, app)` ASCII-sorted; per-bucket
/// migration lists are version-sorted (lexical = chronological per
/// the [`super::naming`] convention).
///
/// Files matching the down-side suffix (`.down.sdjql`) are skipped —
/// the up-side filename serves as the canonical version identifier.
fn scan_committed_migrations(
    workspace_root: &Path,
    database: &str,
) -> Result<BTreeMap<BucketKey, Vec<String>>, ResetError> {
    let with_paths = super::target::scan_filesystem_with_files(workspace_root, Some(database))
        .map_err(|err| ResetError::MigrationScanFailed {
            path: migrations_root(workspace_root).join(database),
            source: err,
        })?;
    Ok(with_paths
        .into_iter()
        .map(|(bucket, vers)| (bucket, vers.into_keys().collect()))
        .collect())
}

struct ReplaySqlFiles {
    up_sql: String,
    down_sql: String,
    checksum_up: String,
    checksum_down: Option<String>,
}

/// Compute the canonical checksum of a committed migration SQL file's
/// contents, in the same domain compose uses when it records the ledger
/// `checksum_up` / `checksum_down` values.
///
/// # Why this exists
///
/// Compose computes checksums over the [`super::OperationSql`] fragments
/// (`label` + `up` / `down` SQL), NOT over the rendered file that those
/// fragments are written into. A composed migration file carries a
/// `-- Djogi composed migration — {up,down}` header, a
/// `-- DO NOT EDIT …` banner, and per-statement `-- <label>` comment
/// lines that are absent from the fragment domain. A naive
/// [`compute_checksum`] over the whole file therefore yields a different
/// digest than the ledger stores. This helper strips the file framing
/// (parsing the composed file back into its canonical fragments) so a
/// recomputed checksum matches what compose persisted — load-bearing for
/// `djogi migrations repair checksum-drift`, which recomputes from disk
/// when the operator omits `--checksum-up` / `--checksum-down`.
///
/// # Behavior
///
/// When `sql` is a recognizable composed file (correct header + banner),
/// the digest is computed over its canonical fragments for `side`.
/// Otherwise — a hand-authored or legacy file with no composed framing —
/// it falls back to the whole-file digest, matching how such files are
/// checksummed elsewhere.
pub fn compute_committed_sql_checksum(sql: &str, side: ResetSqlSide) -> String {
    canonical_composed_sql_fragments(sql, side)
        .map(|fragments| compute_checksum(fragments.iter().map(String::as_str)))
        .unwrap_or_else(|| compute_checksum([sql]))
}

/// Compute the canonical checksum of a committed *down* SQL file, or
/// `None` when the down side carries no real statements.
///
/// Shares the fragment-level domain documented on
/// [`compute_committed_sql_checksum`]. Returns `None` (matching compose's
/// `NULL` `checksum_down` sentinel) when the down file is comment-only —
/// either every composed fragment is a comment, or, for a non-composed
/// file, every non-blank line is a `--` comment. A down side with at
/// least one real statement returns `Some(digest)`.
///
/// Note this takes file *contents* already read from disk; a missing
/// down file (which also maps to a `None` / `NULL` down checksum) is the
/// caller's concern, not handled here.
pub fn compute_committed_down_sql_checksum(sql: &str) -> Option<String> {
    if let Some(fragments) = canonical_composed_sql_fragments(sql, ResetSqlSide::Down) {
        if fragments.iter().all(|fragment| fragment.starts_with("--")) {
            None
        } else {
            Some(compute_checksum(fragments.iter().map(String::as_str)))
        }
    } else if is_comment_only_sql(sql) {
        None
    } else {
        Some(compute_checksum([sql]))
    }
}

fn is_comment_only_sql(sql: &str) -> bool {
    sql.lines()
        .map(str::trim_start)
        .filter(|line| !line.trim().is_empty())
        .all(|line| line.starts_with("--"))
}

fn canonical_composed_sql_fragments(sql: &str, side: ResetSqlSide) -> Option<Vec<String>> {
    let expected_header = match side {
        ResetSqlSide::Up => "-- Djogi composed migration — up\n",
        ResetSqlSide::Down => "-- Djogi composed migration — down\n",
    };
    if !sql.starts_with(expected_header) {
        return None;
    }

    let (_, mut body) =
        sql.split_once("-- DO NOT EDIT — regenerate via `djogi migrations compose`.\n\n")?;

    let mut fragments = Vec::new();
    let helper_pairs: [(&str, String); 3] = match side {
        ResetSqlSide::Up => [
            (
                NUMERIC_ARRAY_HELPER_PRELUDE,
                NUMERIC_ARRAY_HELPER_PRELUDE.to_string(),
            ),
            (
                DATE_ARRAY_HELPER_PRELUDE,
                DATE_ARRAY_HELPER_PRELUDE.to_string(),
            ),
            (
                TSTZ_ARRAY_HELPER_PRELUDE,
                TSTZ_ARRAY_HELPER_PRELUDE.to_string(),
            ),
        ],
        ResetSqlSide::Down => [
            (
                NUMERIC_ARRAY_HELPER_PRELUDE,
                numeric_array_helper_operation().down,
            ),
            (
                DATE_ARRAY_HELPER_PRELUDE,
                date_array_helper_operation().down,
            ),
            (
                TSTZ_ARRAY_HELPER_PRELUDE,
                tstz_array_helper_operation().down,
            ),
        ],
    };
    for (rendered_prelude, checksum_fragment) in helper_pairs {
        if let Some(rest) = body.strip_prefix(rendered_prelude) {
            fragments.push(checksum_fragment);
            body = rest.strip_prefix('\n').unwrap_or(rest);
        }
    }

    let mut operation_fragments = parse_composed_operation_fragments(body, side)?;
    if side == ResetSqlSide::Down {
        operation_fragments.reverse();
    }
    fragments.extend(operation_fragments);
    Some(fragments)
}

fn parse_composed_operation_fragments(body: &str, side: ResetSqlSide) -> Option<Vec<String>> {
    let mut rest = body.trim_end_matches('\n');
    let mut fragments = Vec::new();
    if rest.trim().is_empty() {
        return Some(fragments);
    }

    loop {
        let after_label = rest.strip_prefix("-- ")?;
        let (_, after_label) = after_label.split_once('\n')?;
        let (fragment, next) = match after_label.find("\n\n-- ") {
            Some(next_label) => (
                &after_label[..next_label],
                Some(&after_label[next_label + 2..]),
            ),
            None => (after_label, None),
        };
        let fragment = if side == ResetSqlSide::Down {
            fragment
                .lines()
                .filter(|line| !line.starts_with("-- LOSSY:"))
                .collect::<Vec<_>>()
                .join("\n")
        } else {
            fragment.to_string()
        };
        fragments.push(fragment);
        match next {
            Some(next_rest) => rest = next_rest,
            None => break,
        }
    }
    Some(fragments)
}

fn read_replay_sql_files(
    workspace_root: &Path,
    bucket: &BucketKey,
    version: &str,
) -> Result<ReplaySqlFiles, ResetError> {
    let bucket_dir = super::target::bucket_dir(workspace_root, bucket);
    let up_path = bucket_dir.join(up_filename(version));
    let down_path = bucket_dir.join(down_filename(version));

    let up_sql = fs::read_to_string(&up_path).map_err(|e| ResetError::SqlReadFailed {
        path: up_path.clone(),
        source: e,
    })?;
    let checksum_up = compute_committed_sql_checksum(&up_sql, ResetSqlSide::Up);

    let (down_sql, checksum_down) = match fs::read_to_string(&down_path) {
        Ok(sql) => {
            let checksum_down = compute_committed_down_sql_checksum(&sql);
            (sql, checksum_down)
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => (String::new(), None),
        Err(e) => {
            return Err(ResetError::SqlReadFailed {
                path: down_path,
                source: e,
            });
        }
    };

    Ok(ReplaySqlFiles {
        up_sql,
        down_sql,
        checksum_up,
        checksum_down,
    })
}

/// Read one migration's committed SQL and apply it through the runner.
///
/// Manifest-backed migrations replay the committed segment plan so
/// non-transactional statements keep their original execution shape.
/// Legacy migrations without a manifest fall back to a single
/// transactional segment. In both paths, the runner receives the same
/// canonical operation-fragment checksum domain that compose records
/// in the ledger.
async fn replay_one_migration(
    ctx: &mut DjogiContext,
    workspace_root: &Path,
    bucket: &BucketKey,
    version: &str,
    migrate_config: &MigrateConfig,
    guard: &super::guard::WorkspaceGuard,
    audit_pool: Option<&deadpool_postgres::Pool>,
) -> Result<(), ResetError> {
    let replay_sql = read_replay_sql_files(workspace_root, bucket, version)?;

    let plan = load_reset_replay_plan(workspace_root, bucket, version, &replay_sql)?
        .unwrap_or_else(|| MigrationPlan {
            bucket: bucket.clone(),
            classification: super::diff::Classification::Additive,
            segments: vec![Segment {
                kind: SegmentKind::Transactional,
                statements: vec![OperationSql {
                    label: format!("replay {version}"),
                    up: replay_sql.up_sql.clone(),
                    down: replay_sql.down_sql.clone(),
                    lossy: None,
                }],
            }],
        });

    let runner_ctx = RunnerCtx {
        bucket: bucket.clone(),
        version: version.to_string(),
        description: format!("db reset replay of {version}"),
        checksum_up: replay_sql.checksum_up,
        checksum_down: replay_sql.checksum_down,
        snapshot: None,
        snapshot_path: None,
        // `MigrateConfig` does not derive `Clone` (the type carries
        // a small fixed-size payload but the wider Phase 7 stance is
        // to construct it explicitly per call so future changes
        // surface at every callsite). We mirror the operator's
        // settings into a fresh instance.
        config: MigrateConfig {
            concurrent_warn_relpages: migrate_config.concurrent_warn_relpages,
            strict_concurrent_warnings: migrate_config.strict_concurrent_warnings,
            pk_flip_long_tx_threshold_secs: migrate_config.pk_flip_long_tx_threshold_secs,
            pk_flip_join_table_option: migrate_config.pk_flip_join_table_option,
        },
        // Replay applies in lexical order, so the runner's
        // out-of-order detection should never trip — but supplying
        // `AllowWithDiagnostic` matches the usual dev default and
        // means a bug here surfaces as a warning rather than a hard
        // failure during the time-sensitive reset window.
        out_of_order_policy: OutOfOrderPolicy::AllowWithDiagnostic,
        // Phase 8.5 Cluster 2 issue #118 — production wire-up. When the
        // caller supplied an audit pool on `ResetRequest::audit_pool`
        // we plumb it through to `RunnerCtx` so each replayed
        // migration writes one `djogi_ddl_audit` row per executed
        // segment, exactly as a regular `apply` would. `cloned()`
        // bumps the underlying `Arc` (deadpool pools are Arc-shaped)
        // so the runner's per-segment context can take ownership of
        // its own handle without disturbing the orchestrator's. When
        // the caller passed `None` the runner's audit-write loop
        // gracefully skips — matching the runner's own best-effort
        // stance documented on `record_ddl_audit_for_plan`.
        audit_pool: audit_pool.cloned(),
    };

    apply_plan(ctx, &plan, &runner_ctx, guard)
        .await
        .map_err(|e| ResetError::ReplayFailed {
            version: version.to_string(),
            source: e,
        })?;
    Ok(())
}

// ── URL helpers ───────────────────────────────────────────────────────────

/// Extract the database-name component from a Postgres URL.
///
/// Returns `None` when the URL has no path-component database name
/// (e.g. `postgres://localhost`) — `db reset` cannot derive a database
/// to drop in that case.
///
/// **Percent-decoding.** The path-component bytes are percent-decoded
/// before being returned: a path of `my%2Fdb` decodes to `my/db`.
/// Without this step a URL like `postgres://localhost/my%2Fdb` would
/// make the runner drop the literal identifier `my%2Fdb` (with a
/// `%2F` byte sequence in it) while the post-recreate reconnection
/// would target the correctly-decoded `my/db` — different databases.
/// Decoding produces the SAME byte sequence libpq itself sees when it
/// connects, so the maintenance-DB DROP target matches what the runner
/// re-connects to. Returns `None` on malformed escapes (a `%` not
/// followed by two hex digits) — refusing rather than guessing keeps
/// the destructive path defensive.
///
/// Validation against the Postgres identifier grammar (ASCII letter
/// or underscore followed by ASCII alphanumerics or underscores, up
/// to 63 bytes) is layered on top by [`is_valid_pg_identifier`] —
/// extraction returns the raw decoded string so error messages can
/// surface what the operator actually supplied.
///
/// **No regex.** Walks the URL bytes from the rightmost `/` once and
/// then walks the path bytes once more during the percent-decode.
fn extract_database_from_url(url: &str) -> Option<String> {
    // Confirm the URL has a recognised scheme. We accept both
    // `postgres://` and `postgresql://`. Anything else is treated as
    // libpq parameter form, which db reset doesn't support today
    // (the operator would need the URL form for the libpq path).
    let body = url
        .strip_prefix("postgres://")
        .or_else(|| url.strip_prefix("postgresql://"))?;
    // Skip past the authority — the database name follows the FIRST
    // `/` after the scheme. Authority byte indexing within `body`
    // walks until that slash or end-of-string.
    let mut idx = 0usize;
    let body_bytes = body.as_bytes();
    while idx < body_bytes.len() && body_bytes[idx] != b'/' {
        idx += 1;
    }
    if idx >= body_bytes.len() {
        return None; // no path component
    }
    // Path starts after the slash; database name runs until the next
    // `?` (query parameters) or end-of-string.
    let path_start = idx + 1;
    let mut path_end = path_start;
    while path_end < body_bytes.len() && body_bytes[path_end] != b'?' {
        path_end += 1;
    }
    if path_end == path_start {
        return None; // empty database name
    }
    percent_decode_strict(&body_bytes[path_start..path_end])
}

/// Percent-decode a byte slice strictly. A `%` must be followed by
/// exactly two hex digits (case-insensitive ASCII); any other shape
/// returns `None`. Output is treated as UTF-8 — non-UTF-8 byte
/// sequences also return `None`.
///
/// Kept private to this module so the destructive `db reset` path is
/// the only consumer; if a future caller needs the same primitive we
/// can promote it without churning the public surface.
fn percent_decode_strict(bytes: &[u8]) -> Option<String> {
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0usize;
    while i < bytes.len() {
        let b = bytes[i];
        if b == b'%' {
            // Need exactly two hex digits after the `%`.
            if i + 2 >= bytes.len() {
                return None;
            }
            let hi = hex_digit_value(bytes[i + 1])?;
            let lo = hex_digit_value(bytes[i + 2])?;
            out.push((hi << 4) | lo);
            i += 3;
        } else {
            out.push(b);
            i += 1;
        }
    }
    String::from_utf8(out).ok()
}

/// Map an ASCII hex digit byte to its 0..=15 value. Returns `None`
/// for any non-hex byte — used by [`percent_decode_strict`] to refuse
/// malformed escapes outright.
fn hex_digit_value(b: u8) -> Option<u8> {
    match b {
        b'0'..=b'9' => Some(b - b'0'),
        b'a'..=b'f' => Some(10 + (b - b'a')),
        b'A'..=b'F' => Some(10 + (b - b'A')),
        _ => None,
    }
}

/// Validate a string against the strict Postgres-identifier grammar:
///
/// > ASCII letter or underscore, followed by zero-or-more ASCII
/// > alphanumerics or underscores, up to 63 bytes total.
///
/// **No regex.** Byte-level checks per `docs/spec/decisions.md` —
/// `u8::is_ascii_alphabetic`, `u8::is_ascii_alphanumeric`, and
/// explicit byte equality against `b'_'`.
///
/// Postgres' own grammar is technically more permissive (it accepts
/// any byte sequence inside double-quoted identifiers), but the
/// grammar above is the one every Djogi-emitted identifier obeys.
/// Refusing anything wider keeps the `DROP DATABASE` /
/// `CREATE DATABASE` paths free of operator-supplied bytes that the
/// double-quote escape elsewhere in the codebase wouldn't otherwise
/// surface.
fn is_valid_pg_identifier(name: &str) -> bool {
    let bytes = name.as_bytes();
    // Length: 1..=63 bytes (the standard Postgres `NAMEDATALEN - 1`).
    if bytes.is_empty() || bytes.len() > 63 {
        return false;
    }
    // Leading byte: ASCII letter or underscore.
    let first = bytes[0];
    if !first.is_ascii_alphabetic() && first != b'_' {
        return false;
    }
    // Trailing bytes: ASCII alphanumerics or underscore.
    for &b in &bytes[1..] {
        if !b.is_ascii_alphanumeric() && b != b'_' {
            return false;
        }
    }
    true
}

/// Replace the database-name component in a Postgres URL with a new
/// value. Preserves the scheme, authority, and any trailing query
/// string.
///
/// Returns `None` when the URL has no recognisable database component.
///
/// Visible to the rest of the crate so the seed runner (Codex B-1)
/// can reuse the same splice — `db seed --database <name>` derives
/// the per-database connection URL from the application URL by
/// replacing the path component in place.
pub fn replace_db_in_url(url: &str, new_db: &str) -> Option<String> {
    let body = url
        .strip_prefix("postgres://")
        .or_else(|| url.strip_prefix("postgresql://"))?;
    let scheme = if url.starts_with("postgres://") {
        "postgres://"
    } else {
        "postgresql://"
    };
    // Find the path slash.
    let mut idx = 0usize;
    let body_bytes = body.as_bytes();
    while idx < body_bytes.len() && body_bytes[idx] != b'/' {
        idx += 1;
    }
    if idx >= body_bytes.len() {
        return None;
    }
    let authority = &body[..idx];
    // Capture any trailing `?query` from the original path.
    let path_start = idx + 1;
    let mut path_end = path_start;
    while path_end < body_bytes.len() && body_bytes[path_end] != b'?' {
        path_end += 1;
    }
    let trailing = &body[path_end..]; // includes leading `?` if present, else empty.
    Some(format!("{scheme}{authority}/{new_db}{trailing}"))
}

/// Quote a Postgres identifier for embedding in DDL. Doubles each
/// internal `"` byte and wraps the result in `"`. The byte-level
/// approach matches the rest of the migrate substrate.
fn quote_identifier(name: &str) -> String {
    let mut out = String::with_capacity(name.len() + 2);
    out.push('"');
    for b in name.bytes() {
        if b == b'"' {
            out.push('"');
            out.push('"');
        } else {
            out.push(b as char);
        }
    }
    out.push('"');
    out
}

// ── Tests ─────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn temp_root(tag: &str) -> PathBuf {
        static COUNTER: AtomicUsize = AtomicUsize::new(0);
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let p = std::env::temp_dir().join(format!("djogi-reset-{tag}-{nanos}-{n}"));
        fs::create_dir_all(&p).unwrap();
        p
    }

    fn req<'a>(
        workspace: &'a Path,
        url: &'a str,
        profile: &'a str,
        confirmed: bool,
    ) -> ResetRequest<'a> {
        ResetRequest {
            workspace_root: workspace,
            database_url: url,
            profile,
            confirmed,
            allow_checksum_drift_reset: false,
            maintenance_database: "postgres",
            migrate_config: MigrateConfig::default(),
            // Gate / URL / replay tests do not assert audit-row
            // behaviour — the focused audit-pool wire-up coverage
            // lives in `tests/internal/sources/phase8_5_c2_118_*`.
            audit_pool: None,
        }
    }

    /// Gate 1 — non-localhost URLs must refuse before any I/O.
    #[tokio::test]
    async fn refuses_when_url_is_not_localhost() {
        let work = temp_root("not_localhost");
        let res = reset_app_database(req(
            &work,
            "postgres://prod.example.com:5432/main",
            "development",
            true,
        ))
        .await;
        match res {
            Err(ResetError::Refused(ResetRefusal::NotLocalhost { database_url })) => {
                assert_eq!(database_url, "postgres://prod.example.com:5432/main");
            }
            other => panic!("expected NotLocalhost refusal, got {other:?}"),
        }
        let _ = fs::remove_dir_all(&work);
    }

    /// Gate 2 — production profile refuses even against localhost
    /// (production-looking infra running locally is still production
    /// from a policy perspective).
    #[tokio::test]
    async fn refuses_when_profile_is_production() {
        let work = temp_root("production");
        let res =
            reset_app_database(req(&work, "postgres://localhost/main", "production", true)).await;
        match res {
            Err(ResetError::Refused(ResetRefusal::ProductionProfile { profile })) => {
                assert_eq!(profile, "production");
            }
            other => panic!("expected ProductionProfile refusal, got {other:?}"),
        }
        let _ = fs::remove_dir_all(&work);
    }

    /// Gate 3 — unconfirmed invocation refuses. The default for
    /// `confirmed` (in CLI usage: when `--yes` is absent) is `false`.
    #[tokio::test]
    async fn refuses_when_not_confirmed() {
        let work = temp_root("not_confirmed");
        let res = reset_app_database(req(
            &work,
            "postgres://localhost/main",
            "development",
            false,
        ))
        .await;
        match res {
            Err(ResetError::Refused(ResetRefusal::NotConfirmed)) => {}
            other => panic!("expected NotConfirmed refusal, got {other:?}"),
        }
        let _ = fs::remove_dir_all(&work);
    }

    /// Gate ordering — a request that fails multiple gates should
    /// surface the FIRST gate's refusal (localhost > production >
    /// confirmation). Operators get a single, deterministic refusal
    /// reason rather than a moving target.
    #[tokio::test]
    async fn gates_evaluate_in_documented_order() {
        let work = temp_root("gate_order");
        // Non-localhost + production + unconfirmed — every gate
        // refuses. The localhost gate should fire first.
        let res = reset_app_database(req(
            &work,
            "postgres://prod.example.com/main",
            "production",
            false,
        ))
        .await;
        match res {
            Err(ResetError::Refused(ResetRefusal::NotLocalhost { .. })) => {}
            other => panic!("expected NotLocalhost first, got {other:?}"),
        }
        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn extract_database_from_url_basic() {
        assert_eq!(
            extract_database_from_url("postgres://localhost/main"),
            Some("main".to_string())
        );
        assert_eq!(
            extract_database_from_url("postgres://user:pass@localhost:5432/main"),
            Some("main".to_string())
        );
        assert_eq!(
            extract_database_from_url("postgresql://localhost/main?sslmode=disable"),
            Some("main".to_string())
        );
    }

    #[test]
    fn extract_database_from_url_missing_path() {
        // Authority-only URL (no path component) → None.
        assert_eq!(extract_database_from_url("postgres://localhost"), None);
        // Trailing slash but empty database → None.
        assert_eq!(extract_database_from_url("postgres://localhost/"), None);
        // Non-postgres scheme → None.
        assert_eq!(extract_database_from_url("mysql://localhost/main"), None);
    }

    /// Codex round-1 B-2 — percent-decoding has to happen BEFORE the
    /// runner sees the database name, otherwise the maintenance-DB
    /// drop and the post-recreate reconnect target two different
    /// strings (one literal-percent, one decoded). The extractor MUST
    /// surface the decoded string so the validator can refuse it.
    #[test]
    fn extract_database_from_url_percent_decodes_path_bytes() {
        // `my%2Fdb` decodes to `my/db` — this is exactly what libpq
        // sees when it parses the same URL.
        assert_eq!(
            extract_database_from_url("postgres://localhost/my%2Fdb"),
            Some("my/db".to_string())
        );
        // Mixed case hex digits round-trip identically.
        assert_eq!(
            extract_database_from_url("postgres://localhost/foo%2fbar"),
            Some("foo/bar".to_string())
        );
        // A multi-byte UTF-8 sequence (`é` = `c3 a9`) decodes back to
        // the same sequence the operator would have typed unencoded.
        assert_eq!(
            extract_database_from_url("postgres://localhost/caf%C3%A9"),
            Some("café".to_string())
        );
    }

    /// Codex round-1 B-2 — malformed `%XX` escapes must refuse rather
    /// than silently fall through to a literal `%`. A `%Z9` is not a
    /// valid escape; we don't pretend it is.
    #[test]
    fn extract_database_from_url_rejects_malformed_percent_escapes() {
        // Trailing `%` with no following bytes.
        assert_eq!(
            extract_database_from_url("postgres://localhost/main%"),
            None
        );
        // `%` followed by a single hex digit, then end-of-string.
        assert_eq!(
            extract_database_from_url("postgres://localhost/main%2"),
            None
        );
        // Non-hex bytes after the `%`.
        assert_eq!(
            extract_database_from_url("postgres://localhost/main%ZZ"),
            None
        );
    }

    /// Codex round-1 B-2 — the strict-identifier grammar covers the
    /// happy path (typical names) and refuses anything we won't
    /// emit into DDL.
    #[test]
    fn is_valid_pg_identifier_accepts_typical_names() {
        assert!(is_valid_pg_identifier("main"));
        assert!(is_valid_pg_identifier("crud_log"));
        assert!(is_valid_pg_identifier("event_log"));
        assert!(is_valid_pg_identifier("_underscore_lead"));
        assert!(is_valid_pg_identifier("a"));
        assert!(is_valid_pg_identifier("MyDatabase42"));
        // Boundary — exactly 63 bytes is accepted (the Postgres
        // NAMEDATALEN-1 limit).
        let sixty_three: String = std::iter::repeat_n('a', 63).collect();
        assert!(is_valid_pg_identifier(&sixty_three));
    }

    #[test]
    fn is_valid_pg_identifier_refuses_invalid_inputs() {
        // Empty.
        assert!(!is_valid_pg_identifier(""));
        // 64 bytes — one over the limit.
        let sixty_four: String = std::iter::repeat_n('a', 64).collect();
        assert!(!is_valid_pg_identifier(&sixty_four));
        // Leading digit.
        assert!(!is_valid_pg_identifier("1main"));
        // Internal slash (this is what `my%2Fdb` percent-decodes to).
        assert!(!is_valid_pg_identifier("my/db"));
        // SQL-injection shape — a single quote is not an identifier
        // byte, so the validator rejects the whole string.
        assert!(!is_valid_pg_identifier("'; DROP TABLE foo; --"));
        // Spaces.
        assert!(!is_valid_pg_identifier("my db"));
        // Hyphen.
        assert!(!is_valid_pg_identifier("my-db"));
        // Multi-byte UTF-8 (`café`).
        assert!(!is_valid_pg_identifier("café"));
    }

    /// Codex round-1 B-2 — `reset_app_database` must surface
    /// `InvalidDatabaseName` rather than splicing decoded bytes into
    /// DDL. We exercise three failure shapes plus the maintenance-DB
    /// override path through the public entry.
    #[tokio::test]
    async fn reset_refuses_when_decoded_database_name_is_not_an_identifier() {
        // `my%2Fdb` decodes to `my/db` — gate-passing localhost URL
        // but the decoded name fails the identifier grammar.
        let work = temp_root("invalid_decoded");
        let res = reset_app_database(req(
            &work,
            "postgres://localhost/my%2Fdb",
            "development",
            true,
        ))
        .await;
        match res {
            Err(ResetError::InvalidDatabaseName { name }) => assert_eq!(name, "my/db"),
            other => panic!("expected InvalidDatabaseName for `my/db`, got {other:?}"),
        }

        // The `--maintenance-database` operator-supplied value flows
        // through the same validator; a crafted value must refuse.
        let bogus_maint = ResetRequest {
            workspace_root: &work,
            database_url: "postgres://localhost/main",
            profile: "development",
            confirmed: true,
            allow_checksum_drift_reset: false,
            maintenance_database: "'; DROP DATABASE main; --",
            migrate_config: MigrateConfig::default(),
            audit_pool: None,
        };
        match reset_app_database(bogus_maint).await {
            Err(ResetError::InvalidDatabaseName { name }) => {
                assert_eq!(name, "'; DROP DATABASE main; --");
            }
            other => panic!("expected InvalidDatabaseName for maintenance, got {other:?}"),
        }

        // Boundary — a 64-character all-`a` database refuses (over
        // the 63-byte NAMEDATALEN-1 limit).
        let too_long: String = std::iter::repeat_n('a', 64).collect();
        let url = format!("postgres://localhost/{too_long}");
        match reset_app_database(req(&work, &url, "development", true)).await {
            Err(ResetError::InvalidDatabaseName { name }) => assert_eq!(name, too_long),
            other => panic!("expected InvalidDatabaseName, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn replace_db_in_url_round_trips() {
        assert_eq!(
            replace_db_in_url("postgres://localhost/main", "postgres"),
            Some("postgres://localhost/postgres".to_string())
        );
        assert_eq!(
            replace_db_in_url("postgres://user:pass@localhost:5432/main", "postgres"),
            Some("postgres://user:pass@localhost:5432/postgres".to_string())
        );
        // Query string preserved.
        assert_eq!(
            replace_db_in_url("postgresql://localhost/main?sslmode=disable", "postgres"),
            Some("postgresql://localhost/postgres?sslmode=disable".to_string())
        );
    }

    #[test]
    fn quote_identifier_doubles_internal_quotes() {
        assert_eq!(quote_identifier("main"), "\"main\"");
        // Defensive — Postgres URL parsers don't emit quote bytes in
        // database names, but the escape is still correct.
        assert_eq!(quote_identifier("a\"b"), "\"a\"\"b\"");
    }

    #[test]
    fn scan_committed_migrations_returns_versions_in_lexical_order() {
        use super::super::target::{GLOBAL_BUCKET_DIRNAME, MIGRATIONS_DIR};
        let work = temp_root("scan");
        // Lay down two buckets with two up files each.
        let main_global = work.join(format!("{MIGRATIONS_DIR}/main/{GLOBAL_BUCKET_DIRNAME}"));
        let main_billing = work.join(format!("{MIGRATIONS_DIR}/main/billing"));
        fs::create_dir_all(&main_global).unwrap();
        fs::create_dir_all(&main_billing).unwrap();
        fs::write(
            main_global.join("V20260301000000__init.sdjql"),
            "-- up\nCREATE TABLE foo (id BIGINT PRIMARY KEY);",
        )
        .unwrap();
        fs::write(
            main_global.join("V20260301000000__init.down.sdjql"),
            "-- down\nDROP TABLE foo;",
        )
        .unwrap();
        fs::write(
            main_global.join("V20260201000000__earlier.sdjql"),
            "-- up\nCREATE TABLE bar (id BIGINT PRIMARY KEY);",
        )
        .unwrap();
        fs::write(
            main_billing.join("V20260401000000__widgets.sdjql"),
            "-- up\nCREATE TABLE widgets (id BIGINT PRIMARY KEY);",
        )
        .unwrap();
        // Hand-written `seed.sql` (no `V` prefix) should be skipped.
        fs::write(main_global.join("seed.sql"), "-- not a migration").unwrap();
        // The schema_snapshot.json should be skipped (no `.sdjql`
        // suffix).
        fs::write(main_global.join("schema_snapshot.json"), "{}").unwrap();

        let scanned = scan_committed_migrations(&work, "main").unwrap();
        // Two buckets, in BTreeMap order.
        let mut buckets: Vec<&BucketKey> = scanned.keys().collect();
        buckets.sort();
        assert_eq!(buckets.len(), 2);
        // Global bucket — versions sorted ascending.
        let global_bucket = BucketKey {
            database: "main".to_string(),
            app: String::new(),
        };
        let billing_bucket = BucketKey {
            database: "main".to_string(),
            app: "billing".to_string(),
        };
        assert_eq!(
            scanned[&global_bucket],
            vec![
                "V20260201000000__earlier".to_string(),
                "V20260301000000__init".to_string(),
            ]
        );
        assert_eq!(
            scanned[&billing_bucket],
            vec!["V20260401000000__widgets".to_string()]
        );
        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn scan_committed_migrations_handles_missing_database_dir() {
        let work = temp_root("missing_db");
        // No migrations/ tree at all → empty map, no error.
        let scanned = scan_committed_migrations(&work, "main").unwrap();
        assert!(scanned.is_empty());
        let _ = fs::remove_dir_all(&work);
    }

    fn historical_entry(
        database: &str,
        app: &str,
        version: &str,
        checksum_up: &str,
        checksum_down: Option<&str>,
    ) -> HistoricalReplayEntry {
        HistoricalReplayEntry {
            bucket: bk(database, app),
            version: version.to_string(),
            status: "applied".to_string(),
            checksum_up: checksum_up.to_string(),
            checksum_down: checksum_down.map(str::to_string),
        }
    }

    #[test]
    fn u275_preflight_checksum_parity_refuses_when_up_sql_drifted() {
        let work = temp_root("u275_up_drift");
        let bucket = bk("main", "");
        let version = "V20260301000000__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
        )
        .unwrap();

        let err = preflight_reset_checksum_parity(
            &work,
            "main",
            &[historical_entry(
                "main",
                "",
                version,
                &compute_checksum(["CREATE TABLE widgets (id BIGINT);"]),
                None,
            )],
            false,
        )
        .expect_err("edited up SQL must refuse before destructive reset");

        match err {
            ResetError::Refused(ResetRefusal::ChecksumParity { issues }) => {
                assert_eq!(issues.len(), 1, "expected one drift issue");
                assert_eq!(issues[0].bucket, bucket);
                assert_eq!(issues[0].version, version);
                assert_eq!(issues[0].sql_side, ResetSqlSide::Up);
                assert_eq!(issues[0].problem, ResetChecksumParityProblem::Drift);
                assert_eq!(
                    issues[0].ledger_checksum,
                    compute_checksum(["CREATE TABLE widgets (id BIGINT);"])
                );
                assert_eq!(
                    issues[0].on_disk_checksum.as_deref(),
                    Some(
                        compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"])
                            .as_str()
                    )
                );
            }
            other => panic!("expected checksum-parity refusal, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_preflight_checksum_parity_refuses_when_down_sql_drifted() {
        let work = temp_root("u275_down_drift");
        let bucket = bk("main", "");
        let version = "V20260301000001__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            "DROP TABLE widgets;",
        )
        .unwrap();

        let err = preflight_reset_checksum_parity(
            &work,
            "main",
            &[historical_entry(
                "main",
                "",
                version,
                &compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"]),
                Some(&compute_checksum(["DROP TABLE widgets CASCADE;"])),
            )],
            false,
        )
        .expect_err("edited down SQL must refuse before destructive reset");

        match err {
            ResetError::Refused(ResetRefusal::ChecksumParity { issues }) => {
                assert_eq!(issues.len(), 1, "expected one drift issue");
                assert_eq!(issues[0].bucket, bucket);
                assert_eq!(issues[0].version, version);
                assert_eq!(issues[0].sql_side, ResetSqlSide::Down);
                assert_eq!(issues[0].problem, ResetChecksumParityProblem::Drift);
                assert_eq!(
                    issues[0].ledger_checksum,
                    compute_checksum(["DROP TABLE widgets CASCADE;"])
                );
                assert_eq!(
                    issues[0].on_disk_checksum.as_deref(),
                    Some(compute_checksum(["DROP TABLE widgets;"]).as_str())
                );
            }
            other => panic!("expected checksum-parity refusal, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_preflight_checksum_parity_refuses_when_historical_file_is_missing() {
        let work = temp_root("u275_missing_file");
        let err = preflight_reset_checksum_parity(
            &work,
            "main",
            &[historical_entry(
                "main",
                "",
                "V20260301000002__widgets",
                &compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"]),
                None,
            )],
            false,
        )
        .expect_err("missing historical files must refuse before destructive reset");

        match err {
            ResetError::Refused(ResetRefusal::ChecksumParity { issues }) => {
                assert_eq!(issues.len(), 1, "expected one missing-file issue");
                assert_eq!(issues[0].version, "V20260301000002__widgets");
                assert_eq!(issues[0].sql_side, ResetSqlSide::Up);
                assert_eq!(issues[0].problem, ResetChecksumParityProblem::MissingFile);
                assert!(
                    issues[0].on_disk_checksum.is_none(),
                    "missing file should not claim an on-disk checksum"
                );
            }
            other => panic!("expected checksum-parity refusal, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_preflight_checksum_parity_override_allows_drift() {
        let work = temp_root("u275_override");
        let bucket = bk("main", "");
        let version = "V20260301000003__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
        )
        .unwrap();

        preflight_reset_checksum_parity(
            &work,
            "main",
            &[historical_entry(
                "main",
                "",
                version,
                &compute_checksum(["CREATE TABLE widgets (id BIGINT);"]),
                None,
            )],
            true,
        )
        .expect("explicit override should bypass checksum-parity refusal");

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_preflight_checksum_parity_refuses_when_down_file_is_missing() {
        let work = temp_root("u275_missing_down");
        let bucket = bk("main", "");
        let version = "V20260301000003__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
        )
        .unwrap();

        let err = preflight_reset_checksum_parity(
            &work,
            "main",
            &[historical_entry(
                "main",
                "",
                version,
                &compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"]),
                Some(&compute_checksum(["DROP TABLE widgets;"])),
            )],
            false,
        )
        .expect_err("missing down SQL must refuse before destructive reset");

        match err {
            ResetError::Refused(ResetRefusal::ChecksumParity { issues }) => {
                assert_eq!(issues.len(), 1, "expected one missing-file issue");
                assert_eq!(issues[0].version, version);
                assert_eq!(issues[0].sql_side, ResetSqlSide::Down);
                assert_eq!(issues[0].problem, ResetChecksumParityProblem::MissingFile);
                assert!(issues[0].on_disk_checksum.is_none());
            }
            other => panic!("expected checksum-parity refusal, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    fn composed_up_sql(version: &str, body: &str) -> String {
        format!(
            "-- Djogi composed migration — up\n\
             -- Version: {version}\n\
             -- Bucket:  main/_global_\n\
             -- Classification: Additive\n\
             -- DO NOT EDIT — regenerate via `djogi migrations compose`.\n\n\
             -- AddTable widgets\n\
             {body}\n\n"
        )
    }

    fn composed_down_sql(version: &str, body: &str) -> String {
        format!(
            "-- Djogi composed migration — down\n\
             -- Version: {version}\n\
             -- Bucket:  main/_global_\n\
             -- DO NOT EDIT — regenerate via `djogi migrations compose`.\n\n\
             -- DropTable widgets\n\
             {body}\n\n"
        )
    }

    #[test]
    fn u275_preflight_checksum_parity_accepts_composed_sql_headers() {
        let work = temp_root("u275_composed_headers");
        let bucket = bk("main", "");
        let version = "V20260301000012__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            composed_up_sql(version, "CREATE TABLE widgets (id BIGINT PRIMARY KEY);"),
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            composed_down_sql(version, "DROP TABLE widgets;"),
        )
        .unwrap();

        preflight_reset_checksum_parity(
            &work,
            "main",
            &[historical_entry(
                "main",
                "",
                version,
                &compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"]),
                Some(&compute_checksum(["DROP TABLE widgets;"])),
            )],
            false,
        )
        .expect("composed comments and labels must not count as checksum drift");

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_preflight_checksum_parity_refuses_edited_composed_operation_sql() {
        let work = temp_root("u275_composed_operation_drift");
        let bucket = bk("main", "");
        let version = "V20260301000013__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            composed_up_sql(version, "CREATE TABLE widgets (id BIGINT PRIMARY KEY);"),
        )
        .unwrap();

        let err = preflight_reset_checksum_parity(
            &work,
            "main",
            &[historical_entry(
                "main",
                "",
                version,
                &compute_checksum(["CREATE TABLE widgets (id BIGINT);"]),
                None,
            )],
            false,
        )
        .expect_err("operation SQL drift inside composed file must refuse");

        match err {
            ResetError::Refused(ResetRefusal::ChecksumParity { issues }) => {
                assert_eq!(issues.len(), 1);
                assert_eq!(issues[0].problem, ResetChecksumParityProblem::Drift);
                assert_eq!(
                    issues[0].on_disk_checksum.as_deref(),
                    Some(
                        compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"])
                            .as_str()
                    )
                );
            }
            other => panic!("expected checksum-parity refusal, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_preflight_checksum_parity_refuses_when_baseline_row_cannot_be_compared() {
        let work = temp_root("u275_baseline");
        let bucket = bk("main", "");
        let version = "V20260301000004__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
        )
        .unwrap();

        let err = preflight_reset_checksum_parity(
            &work,
            "main",
            &[HistoricalReplayEntry {
                bucket,
                version: version.to_string(),
                status: "baseline".to_string(),
                checksum_up: "V1:baseline-projection".to_string(),
                checksum_down: None,
            }],
            false,
        )
        .expect_err("baseline rows must refuse when reset cannot establish file parity");

        match err {
            ResetError::Refused(ResetRefusal::ChecksumParity { issues }) => {
                assert_eq!(issues.len(), 1, "expected one baseline issue");
                assert_eq!(issues[0].version, version);
                assert_eq!(
                    issues[0].problem,
                    ResetChecksumParityProblem::UnsupportedBaseline
                );
                assert!(issues[0].on_disk_checksum.is_none());
            }
            other => panic!("expected checksum-parity refusal, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_replay_sql_checksums_include_down_when_down_file_exists() {
        let work = temp_root("u275_replay_checksums");
        let bucket = bk("main", "");
        let version = "V20260301000005__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            "DROP TABLE widgets;",
        )
        .unwrap();

        let replay_sql =
            read_replay_sql_files(&work, &bucket, version).expect("load replay SQL files");
        assert_eq!(
            replay_sql.checksum_up,
            compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"])
        );
        assert_eq!(
            replay_sql.checksum_down.as_deref(),
            Some(compute_checksum(["DROP TABLE widgets;"]).as_str()),
            "reset replay must preserve checksum_down so later resets still enforce down-side parity"
        );

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_replay_sql_checksums_treat_comment_only_down_as_none() {
        let work = temp_root("u275_comment_only_down");
        let bucket = bk("main", "");
        let version = "V20260301000016__phase_zero_like";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE SCHEMA IF NOT EXISTS heeranjid;",
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            "-- no meaningful rollback\n-- framework bootstrap is dependency-only\n",
        )
        .unwrap();

        let replay_sql =
            read_replay_sql_files(&work, &bucket, version).expect("load replay SQL files");
        assert!(
            replay_sql.checksum_down.is_none(),
            "comment-only down files must preserve the no-real-rollback null sentinel"
        );

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u276_preflight_replay_semantics_refuses_non_transactional_sql_without_manifest() {
        let work = temp_root("u276_missing_manifest");
        let bucket = bk("main", "");
        let version = "V20260301000006__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "-- AddTable widgets\n\
             CREATE TABLE widgets (id BIGINT PRIMARY KEY);\n\n\
             -- AddIndex widgets_id_idx\n\
             CREATE INDEX CONCURRENTLY widgets_id_idx ON widgets (id);\n",
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            "DROP INDEX CONCURRENTLY widgets_id_idx;\nDROP TABLE widgets;\n",
        )
        .unwrap();

        let err = preflight_reset_replay_semantics(&work, "main")
            .expect_err("legacy file-only concurrent index replay must refuse before drop");

        match err {
            ResetError::Refused(ResetRefusal::ReplaySemantics { issues }) => {
                assert_eq!(issues.len(), 1, "expected one replay-semantics issue");
                assert_eq!(issues[0].bucket, bucket);
                assert_eq!(issues[0].version, version);
                assert_eq!(
                    issues[0].problem,
                    ResetReplaySemanticsProblem::MissingReplayPlan
                );
                assert_eq!(issues[0].statement_shape, "CREATE INDEX CONCURRENTLY");
            }
            other => panic!("expected replay-semantics refusal, got {other:?}"),
        }

        let _ = fs::remove_dir_all(&work);
    }

    fn assert_single_replay_semantics_issue(
        err: ResetError,
        expected_bucket: &BucketKey,
        expected_version: &str,
        expected_problem: ResetReplaySemanticsProblem,
        expected_shape: &str,
    ) {
        match err {
            ResetError::Refused(ResetRefusal::ReplaySemantics { issues }) => {
                assert_eq!(issues.len(), 1, "expected one replay-semantics issue");
                assert_eq!(issues[0].bucket, *expected_bucket);
                assert_eq!(issues[0].version, expected_version);
                assert_eq!(issues[0].problem, expected_problem);
                assert_eq!(issues[0].statement_shape, expected_shape);
            }
            other => panic!("expected replay-semantics refusal, got {other:?}"),
        }
    }

    #[test]
    fn u276_preflight_replay_semantics_refuses_call_backfill_without_manifest() {
        let work = temp_root("u276_missing_manifest_call");
        let bucket = bk("main", "");
        let version = "V20260301000008__pk_flip_call";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CALL heeranjid_bulk_backfill('widgets', 'id', 'id_desc', 'heer', 10000);\n",
        )
        .unwrap();
        fs::write(bucket_dir.join(down_filename(version)), "SELECT 1;\n").unwrap();

        let err = preflight_reset_replay_semantics(&work, "main")
            .expect_err("CALL backfill replay without manifest must refuse before drop");
        assert_single_replay_semantics_issue(
            err,
            &bucket,
            version,
            ResetReplaySemanticsProblem::MissingReplayPlan,
            "CALL heeranjid_bulk_backfill",
        );

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u276_preflight_replay_semantics_refuses_do_backfill_without_manifest() {
        let work = temp_root("u276_missing_manifest_do");
        let bucket = bk("main", "");
        let version = "V20260301000009__pk_flip_do";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "DO $$\n\
             BEGIN\n\
                 COMMIT;\n\
             END\n\
             $$;\n",
        )
        .unwrap();
        fs::write(bucket_dir.join(down_filename(version)), "SELECT 1;\n").unwrap();

        let err = preflight_reset_replay_semantics(&work, "main")
            .expect_err("DO backfill replay without manifest must refuse before drop");
        assert_single_replay_semantics_issue(
            err,
            &bucket,
            version,
            ResetReplaySemanticsProblem::MissingReplayPlan,
            "DO block with COMMIT",
        );

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u276_preflight_replay_semantics_refuses_partitioned_placeholder_without_manifest() {
        let work = temp_root("u276_missing_manifest_partitioned");
        let bucket = bk("main", "");
        let version = "V20260301000010__pk_flip_partitioned";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        fs::write(
            bucket_dir.join(up_filename(version)),
            "CREATE UNIQUE INDEX events_partition_key_id_desc_idx\n  \
             ON ONLY events (partition_key, id_desc);\n\
             -- Per leaf: CREATE UNIQUE INDEX CONCURRENTLY <leaf>_partition_key_id_desc_idx\n\
             --             ON <leaf> (partition_key, id_desc);\n\
             -- Then ALTER INDEX events_partition_key_id_desc_idx ATTACH PARTITION\n\
             --             <leaf>_partition_key_id_desc_idx;\n",
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            "DROP INDEX IF EXISTS events_partition_key_id_desc_idx;\n",
        )
        .unwrap();

        let err = preflight_reset_replay_semantics(&work, "main")
            .expect_err("partitioned placeholder replay without manifest must refuse before drop");
        assert_single_replay_semantics_issue(
            err,
            &bucket,
            version,
            ResetReplaySemanticsProblem::MissingReplayPlan,
            "PARTITIONED CONCURRENTLY placeholder",
        );

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u276_load_reset_replay_plan_refuses_invalid_call_manifest() {
        let work = temp_root("u276_invalid_manifest_call");
        let bucket = bk("main", "");
        let version = "V20260301000011__pk_flip_call_invalid";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();
        let up_sql = "CALL heeranjid_bulk_backfill('widgets', 'id', 'id_desc', 'heer', 10000);\n";
        fs::write(bucket_dir.join(up_filename(version)), up_sql).unwrap();
        fs::write(bucket_dir.join(down_filename(version)), "SELECT 1;\n").unwrap();
        fs::write(
            bucket_dir.join(format!("{version}.plan.json")),
            "{\n  \"format_version\": \"1\",\n  \"checksum_up\": \"V1:stale\",\n  \"checksum_down\": null,\n  \"classification\": { \"kind\": \"additive\" },\n  \"segments\": []\n}\n",
        )
        .unwrap();

        let replay_sql =
            read_replay_sql_files(&work, &bucket, version).expect("load replay SQL files");
        let err = load_reset_replay_plan(&work, &bucket, version, &replay_sql)
            .expect_err("invalid non-transactional replay manifest must refuse");
        assert_single_replay_semantics_issue(
            err,
            &bucket,
            version,
            ResetReplaySemanticsProblem::InvalidReplayPlan,
            "CALL heeranjid_bulk_backfill",
        );

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u276_load_reset_replay_plan_preserves_committed_segment_kinds() {
        let work = temp_root("u276_manifest_roundtrip");
        let bucket = bk("main", "");
        let version = "V20260301000007__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();

        let up_sql = "-- AddTable widgets\n\
             CREATE TABLE widgets (id BIGINT PRIMARY KEY);\n\n\
             -- AddIndex widgets_id_idx\n\
             CREATE INDEX CONCURRENTLY widgets_id_idx ON widgets (id);\n";
        let down_sql = "DROP INDEX CONCURRENTLY widgets_id_idx;\nDROP TABLE widgets;\n";
        fs::write(bucket_dir.join(up_filename(version)), up_sql).unwrap();
        fs::write(bucket_dir.join(down_filename(version)), down_sql).unwrap();
        fs::write(
            bucket_dir.join(format!("{version}.plan.json")),
            format!(
                "{{\n  \"format_version\": \"1\",\n  \"checksum_up\": \"{}\",\n  \"checksum_down\": \"{}\",\n  \"classification\": {{ \"kind\": \"additive\" }},\n  \"segments\": [\n    {{\n      \"kind\": \"transactional\",\n      \"statements\": [\n        {{ \"label\": \"AddTable widgets\", \"up\": \"CREATE TABLE widgets (id BIGINT PRIMARY KEY);\" }}\n      ]\n    }},\n    {{\n      \"kind\": \"non_transactional\",\n      \"statements\": [\n        {{ \"label\": \"AddIndex widgets_id_idx\", \"up\": \"CREATE INDEX CONCURRENTLY widgets_id_idx ON widgets (id);\" }}\n      ]\n    }}\n  ]\n}}\n",
                compute_checksum([up_sql]),
                compute_checksum([down_sql]),
            ),
        )
        .unwrap();

        let replay_sql =
            read_replay_sql_files(&work, &bucket, version).expect("load replay SQL files");
        let plan = load_reset_replay_plan(&work, &bucket, version, &replay_sql)
            .expect("manifest should load")
            .expect("manifest should produce a replay plan");

        assert_eq!(plan.bucket, bucket);
        assert_eq!(
            plan.classification,
            crate::migrate::Classification::Additive
        );
        assert_eq!(plan.segments.len(), 2);
        assert_eq!(plan.segments[0].kind, SegmentKind::Transactional);
        assert_eq!(plan.segments[0].statements[0].label, "AddTable widgets");
        assert_eq!(plan.segments[1].kind, SegmentKind::NonTransactional);
        assert_eq!(
            plan.segments[1].statements[0].label,
            "AddIndex widgets_id_idx"
        );

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u276_load_reset_replay_plan_accepts_composed_sql_operation_checksums() {
        let work = temp_root("u276_manifest_composed_checksum");
        let bucket = bk("main", "");
        let version = "V20260301000014__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();

        fs::write(
            bucket_dir.join(up_filename(version)),
            composed_up_sql(
                version,
                "CREATE TABLE widgets (id BIGINT PRIMARY KEY);\n\n\
                 -- AddIndex widgets_id_idx\n\
                 CREATE INDEX CONCURRENTLY widgets_id_idx ON widgets (id);",
            ),
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            composed_down_sql(
                version,
                "DROP INDEX CONCURRENTLY widgets_id_idx;\nDROP TABLE widgets;",
            ),
        )
        .unwrap();
        fs::write(
            bucket_dir.join(format!("{version}.plan.json")),
            format!(
                "{{\n  \"format_version\": \"1\",\n  \"checksum_up\": \"{}\",\n  \"checksum_down\": \"{}\",\n  \"classification\": {{ \"kind\": \"additive\" }},\n  \"segments\": [\n    {{\n      \"kind\": \"transactional\",\n      \"statements\": [\n        {{ \"label\": \"AddTable widgets\", \"up\": \"CREATE TABLE widgets (id BIGINT PRIMARY KEY);\" }}\n      ]\n    }},\n    {{\n      \"kind\": \"non_transactional\",\n      \"statements\": [\n        {{ \"label\": \"AddIndex widgets_id_idx\", \"up\": \"CREATE INDEX CONCURRENTLY widgets_id_idx ON widgets (id);\" }}\n      ]\n    }}\n  ]\n}}\n",
                compute_checksum([
                    "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
                    "CREATE INDEX CONCURRENTLY widgets_id_idx ON widgets (id);"
                ]),
                compute_checksum(["DROP INDEX CONCURRENTLY widgets_id_idx;\nDROP TABLE widgets;"]),
            ),
        )
        .unwrap();

        let replay_sql =
            read_replay_sql_files(&work, &bucket, version).expect("load replay SQL files");
        assert_eq!(
            replay_sql.checksum_up,
            compute_checksum([
                "CREATE TABLE widgets (id BIGINT PRIMARY KEY);",
                "CREATE INDEX CONCURRENTLY widgets_id_idx ON widgets (id);"
            ])
        );
        let plan = load_reset_replay_plan(&work, &bucket, version, &replay_sql)
            .expect("manifest should load")
            .expect("manifest should produce a replay plan");
        assert_eq!(plan.segments.len(), 2);

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u276_load_reset_replay_plan_accepts_composed_comment_only_down_manifest() {
        let work = temp_root("u276_manifest_comment_only_down");
        let bucket = bk("main", "");
        let version = "V20260301000015__widgets";
        let bucket_dir = super::super::target::bucket_dir(&work, &bucket);
        fs::create_dir_all(&bucket_dir).unwrap();

        fs::write(
            bucket_dir.join(up_filename(version)),
            composed_up_sql(version, "CREATE TABLE widgets (id BIGINT PRIMARY KEY);"),
        )
        .unwrap();
        fs::write(
            bucket_dir.join(down_filename(version)),
            composed_down_sql(
                version,
                "-- no-op rollback placeholder: lossy operation requires manual rollback",
            ),
        )
        .unwrap();
        fs::write(
            bucket_dir.join(format!("{version}.plan.json")),
            format!(
                "{{\n  \"format_version\": \"1\",\n  \"checksum_up\": \"{}\",\n  \"checksum_down\": null,\n  \"classification\": {{ \"kind\": \"lossy\" }},\n  \"segments\": [\n    {{\n      \"kind\": \"transactional\",\n      \"statements\": [\n        {{ \"label\": \"AddTable widgets\", \"up\": \"CREATE TABLE widgets (id BIGINT PRIMARY KEY);\" }}\n      ]\n    }}\n  ]\n}}\n",
                compute_checksum(["CREATE TABLE widgets (id BIGINT PRIMARY KEY);"]),
            ),
        )
        .unwrap();

        let replay_sql =
            read_replay_sql_files(&work, &bucket, version).expect("load replay SQL files");
        assert!(
            replay_sql.checksum_down.is_none(),
            "composed comment-only down files must preserve compose's checksum_down = null"
        );
        let plan = load_reset_replay_plan(&work, &bucket, version, &replay_sql)
            .expect("manifest should load")
            .expect("manifest should produce a replay plan");
        assert_eq!(plan.segments.len(), 1);

        let _ = fs::remove_dir_all(&work);
    }

    #[test]
    fn u275_checksum_parity_refusal_display_names_bucket_version_and_checksums() {
        let refusal = ResetRefusal::ChecksumParity {
            issues: vec![
                ResetChecksumParityIssue {
                    bucket: bk("main", ""),
                    version: "V20260301000004__widgets".to_string(),
                    sql_side: ResetSqlSide::Up,
                    ledger_checksum: "V1:ledger".to_string(),
                    on_disk_checksum: Some("V1:disk".to_string()),
                    problem: ResetChecksumParityProblem::Drift,
                },
                ResetChecksumParityIssue {
                    bucket: bk("main", "billing"),
                    version: "V20260301000005__billing".to_string(),
                    sql_side: ResetSqlSide::Down,
                    ledger_checksum: "V1:down-ledger".to_string(),
                    on_disk_checksum: None,
                    problem: ResetChecksumParityProblem::MissingFile,
                },
            ],
        };

        let rendered = refusal.to_string();
        assert!(
            rendered.contains("main/_global_/V20260301000004__widgets"),
            "{rendered}"
        );
        assert!(
            rendered.contains("main/billing/V20260301000005__billing"),
            "{rendered}"
        );
        assert!(rendered.contains("V1:ledger"), "{rendered}");
        assert!(rendered.contains("V1:disk"), "{rendered}");
        assert!(rendered.contains("V1:down-ledger"), "{rendered}");
        assert!(
            rendered.contains("--allow-checksum-drift-reset")
                || rendered.contains("allow_checksum_drift_reset"),
            "{rendered}"
        );
    }

    // ── Codex umbrella U-4: historical-order replay plan ────────────────

    fn bk(database: &str, app: &str) -> BucketKey {
        BucketKey {
            database: database.to_string(),
            app: app.to_string(),
        }
    }

    /// `build_replay_plan` honours the historical apply order: when
    /// `0001 → applied_at_rank 0`, `0003 → rank 1`, `0002 → rank 2`,
    /// the replay plan is `[0001, 0003, 0002]` — NOT lexical
    /// `[0001, 0002, 0003]`. This is the load-bearing umbrella U-4
    /// invariant.
    #[test]
    fn u4_replay_plan_honours_historical_apply_order_over_lexical() {
        let bucket = bk("main", "");
        let mut buckets = BTreeMap::new();
        buckets.insert(
            bucket.clone(),
            vec![
                "V20260101000000__a".to_string(),
                "V20260201000000__b".to_string(),
                "V20260301000000__c".to_string(),
            ],
        );
        // Out-of-order historical apply: a (0), c (1), b (2). Lexical
        // would be a, b, c — different order!
        let mut historical = BTreeMap::new();
        historical.insert((bucket.clone(), "V20260101000000__a".to_string()), 0u64);
        historical.insert((bucket.clone(), "V20260301000000__c".to_string()), 1u64);
        historical.insert((bucket.clone(), "V20260201000000__b".to_string()), 2u64);

        let plan = build_replay_plan(&buckets, &historical);
        let versions: Vec<&str> = plan.iter().map(|(_, v)| v.as_str()).collect();
        assert_eq!(
            versions,
            vec![
                "V20260101000000__a",
                "V20260301000000__c",
                "V20260201000000__b",
            ],
            "historical apply order MUST win over lexical version sort"
        );
    }

    /// When NO historical order exists (fresh DB, ledger missing),
    /// the plan falls back to lexical version-string sort. This is
    /// the safe-by-default behaviour that pre-umbrella reset always
    /// used.
    #[test]
    fn u4_replay_plan_falls_back_to_lexical_when_historical_empty() {
        let bucket = bk("main", "");
        let mut buckets = BTreeMap::new();
        buckets.insert(
            bucket.clone(),
            vec![
                "V20260201000000__b".to_string(),
                "V20260101000000__a".to_string(),
                "V20260301000000__c".to_string(),
            ],
        );
        let historical: BTreeMap<(BucketKey, String), u64> = BTreeMap::new();
        let plan = build_replay_plan(&buckets, &historical);
        let versions: Vec<&str> = plan.iter().map(|(_, v)| v.as_str()).collect();
        assert_eq!(
            versions,
            vec![
                "V20260101000000__a",
                "V20260201000000__b",
                "V20260301000000__c",
            ],
            "empty historical map → lexical sort"
        );
    }

    /// Mixed shape: some versions have historical entries, some
    /// don't (typical when files were added on disk after the last
    /// apply). The historical ones come first in apply-order; the
    /// rest sort lexically afterwards.
    #[test]
    fn u4_replay_plan_mixes_historical_and_new_disk_files() {
        let bucket = bk("main", "");
        let mut buckets = BTreeMap::new();
        buckets.insert(
            bucket.clone(),
            vec![
                "V20260101000000__a".to_string(), // historical, rank 0
                "V20260201000000__b".to_string(), // not historical
                "V20260301000000__c".to_string(), // historical, rank 1
                "V20260401000000__d".to_string(), // not historical
            ],
        );
        let mut historical = BTreeMap::new();
        historical.insert((bucket.clone(), "V20260101000000__a".to_string()), 0u64);
        historical.insert((bucket.clone(), "V20260301000000__c".to_string()), 1u64);

        let plan = build_replay_plan(&buckets, &historical);
        let versions: Vec<&str> = plan.iter().map(|(_, v)| v.as_str()).collect();
        assert_eq!(
            versions,
            vec![
                "V20260101000000__a", // rank 0
                "V20260301000000__c", // rank 1
                "V20260201000000__b", // no rank, lexical first among non-historical
                "V20260401000000__d", // no rank, lexical second
            ],
        );
    }

    // ── Codex umbrella round-2 U-6: error-policy classification ─────────

    /// Connecting to a syntactically valid but unreachable URL must
    /// classify as `Transient`, NOT `LedgerMissing`. Pre-U-6 the
    /// connect-failure path collapsed to an empty map and the
    /// destructive operation would proceed; post-U-6 the call surfaces
    /// the failure so the caller refuses to drop / recreate.
    ///
    /// We point at a port nobody listens on (TCP `:1` is the standard
    /// "discard" pseudo-port — kernels reject the connect immediately).
    #[tokio::test]
    async fn u6_capture_failure_unreachable_url_classifies_as_transient() {
        let url = "postgres://djogi:djogi@127.0.0.1:1/nonexistent_db";
        let res = capture_historical_apply_order(url).await;
        match res {
            Err(HistoricalCaptureError::Transient(e)) => {
                let msg = format!("{e}");
                assert!(
                    msg.contains("connect failed")
                        || msg.contains("connect")
                        || msg.contains("Connection")
                        || msg.contains("connection")
                        || msg.contains("refused"),
                    "Transient message must surface the connect failure: {msg}"
                );
            }
            Err(HistoricalCaptureError::LedgerMissing) => {
                panic!(
                    "U-6: connect failure must classify as Transient, NOT LedgerMissing — \
                     pre-fix the unwrap_or_default() collapsed both into the same fallback"
                );
            }
            Ok(_) => panic!("U-6: connect to :1 must fail"),
        }
    }

    /// `ResetError::HistoricalOrderCaptureFailed` is plumbed through
    /// `reset_app_database` end-to-end. We construct a request that
    /// passes every gate (localhost URL with valid identifier name,
    /// development profile, confirmed) but points at an unreachable
    /// port — the historical-order capture's connect step fails and
    /// the variant must propagate.
    ///
    /// CRITICAL invariant: pre-fix this same scenario would have
    /// `unwrap_or_default()` ed the failure and proceeded into the
    /// destructive `drop_and_create_database` call. Post-fix the
    /// request returns `HistoricalOrderCaptureFailed` BEFORE any
    /// destructive operation runs.
    #[tokio::test]
    async fn u6_reset_propagates_capture_failure_before_destructive_op() {
        let work = temp_root("u6_capture_propagate");
        // Localhost with a deliberately-wrong port so the gate passes
        // but the connect fails. The is_localhost_connection helper
        // accepts host=127.0.0.1 / host=localhost regardless of port.
        let url = "postgres://djogi:djogi@127.0.0.1:1/main";
        let res = reset_app_database(req(&work, url, "development", true)).await;
        match res {
            Err(ResetError::HistoricalOrderCaptureFailed { source }) => {
                let msg = format!("{source}");
                assert!(
                    msg.contains("connect")
                        || msg.contains("connection")
                        || msg.contains("refused"),
                    "source message must surface the underlying connect failure: {msg}"
                );
            }
            other => panic!(
                "U-6: expected HistoricalOrderCaptureFailed; got {other:?} \
                 (pre-fix this would have proceeded into the destructive drop)"
            ),
        }
        let _ = fs::remove_dir_all(&work);
    }

    /// The Display impl for `HistoricalOrderCaptureFailed` carries
    /// operator-actionable language: it names the underlying source,
    /// explains why we refused, and tells the operator what to do
    /// next. This is the message a CI script or human will see when
    /// the gate fires.
    #[test]
    fn u6_historical_order_capture_failed_display_is_actionable() {
        let e = ResetError::HistoricalOrderCaptureFailed {
            source: DjogiError::Db(DbError::other("connection refused".to_string())),
        };
        let s = format!("{e}");
        assert!(s.contains("connection refused"), "must echo source: {s}");
        assert!(
            s.contains("refusing to proceed") || s.contains("refusing"),
            "must explain we refused: {s}"
        );
        assert!(
            s.contains("re-run") || s.contains("rerun"),
            "must guide remediation: {s}"
        );
    }

    /// `ResetError::source()` returns the underlying `DjogiError` for
    /// the U-6 variant — the `?` operator and `tracing` style error
    /// chains depend on that. The pre-existing variants in the impl
    /// already do this; the test guards against forgetting the new
    /// arm if someone touches the match block later.
    #[test]
    fn u6_historical_order_capture_failed_carries_source() {
        use std::error::Error;
        let e = ResetError::HistoricalOrderCaptureFailed {
            source: DjogiError::Db(DbError::other("decode failed".to_string())),
        };
        let src = e.source().expect("must have source");
        assert!(
            src.to_string().contains("decode failed"),
            "source must carry inner message: {}",
            src
        );
    }

    /// Cross-bucket: historical apply ranks impose order across
    /// different buckets too. Without that property, two buckets
    /// applied historically as `bucketA/v1, bucketB/v1, bucketA/v2`
    /// would lose the interleaved ordering.
    #[test]
    fn u4_replay_plan_orders_across_buckets_by_historical_rank() {
        let a = bk("main", "users");
        let b = bk("main", "billing");
        let mut buckets = BTreeMap::new();
        buckets.insert(
            a.clone(),
            vec!["V0001__a1".to_string(), "V0003__a2".to_string()],
        );
        buckets.insert(b.clone(), vec!["V0002__b1".to_string()]);

        // Historical apply: a/V0001 (0), b/V0002 (1), a/V0003 (2).
        let mut historical = BTreeMap::new();
        historical.insert((a.clone(), "V0001__a1".to_string()), 0u64);
        historical.insert((b.clone(), "V0002__b1".to_string()), 1u64);
        historical.insert((a.clone(), "V0003__a2".to_string()), 2u64);

        let plan = build_replay_plan(&buckets, &historical);
        let render: Vec<String> = plan
            .iter()
            .map(|(bucket, v)| format!("{}/{}/{}", bucket.database, bucket.app, v))
            .collect();
        assert_eq!(
            render,
            vec![
                "main/users/V0001__a1".to_string(),
                "main/billing/V0002__b1".to_string(),
                "main/users/V0003__a2".to_string(),
            ],
            "interleaved cross-bucket apply order must be preserved"
        );
    }

    #[test]
    fn reset_discovers_sdjql_migration_files() {
        let root = temp_root("sdjql-discovery");
        let bucket = super::super::target::bucket_dir(&root, &bk("main", "myapp"));
        fs::create_dir_all(&bucket).unwrap();

        fs::write(
            bucket.join("V20260501000000__new.sdjql"),
            "-- Djogi composed migration — up\n-- Version: V20260501000000__new\n\
             -- Bucket:  main/myapp\n-- Classification: Additive\n--\n\
             -- Apply via `djogi migrations apply`, not psql...\n-- DO NOT EDIT...\n\nCREATE TABLE items (id bigint PRIMARY KEY);\n"
        ).unwrap();
        fs::write(
            bucket.join("V20260501000000__new.down.sdjql"),
            "-- Djogi composed migration — down\n-- Version: V20260501000000__new\n\
             -- Bucket:  main/myapp\n-- DO NOT EDIT...\n\nDROP TABLE items;\n",
        )
        .unwrap();

        let scanned =
            super::super::target::scan_filesystem_with_files(&root, Some("main")).unwrap();
        let bk_main = bk("main", "myapp");
        assert!(
            scanned.contains_key(&bk_main),
            "scanner must discover .sdjql files"
        );
        assert_eq!(
            scanned[&bk_main].len(),
            1,
            "should find exactly one up-side file"
        );
        assert!(scanned[&bk_main].contains_key("V20260501000000__new"));

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn reset_rejects_legacy_sql_migration_files() {
        let root = temp_root("legacy-sql-reject");
        let bucket = super::super::target::bucket_dir(&root, &bk("main", "myapp"));
        fs::create_dir_all(&bucket).unwrap();

        fs::write(
            bucket.join("V20260301000000__legacy.sql"),
            "-- Djogi composed migration — up\n-- Version: V20260301000000__legacy\n\
             CREATE TABLE users (id bigint PRIMARY KEY);\n",
        )
        .unwrap();

        let result = super::super::target::scan_filesystem_with_files(&root, Some("main"));
        assert!(
            result.is_err(),
            "reset must reject legacy .sql schema migration files"
        );
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("V20260301000000__legacy.sql") || err.contains("legacy"),
            "error must mention the legacy file: {err}"
        );

        let _ = fs::remove_dir_all(&root);
    }
}