gunnar-server 3.0.0

The gunnar git server: SSH and smart-HTTP git transports and the gRPC control plane. The appliance half, extracted from gunnar-cli so the server code lives in its own crate.
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
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
//! **The join.** `gunnar-ssh`'s [`GitTransport`] seam, implemented over
//! `gunnar-wire`'s state machines, with `gunnar-wire`'s [`PushGate`] seam
//! implemented over `gunnar-policy`'s engine.
//!
//! Until this file existed, those three crates had each been proven alone:
//! `gunnar-wire` against stock `git` over pipes, `gunnar-ssh` against a
//! `BannerTransport`, `gunnar-policy` against crafted push fixtures. Nothing
//! had joined them, so nothing had proved a real `git clone` over a real SSH
//! connection. `tests/ssh_end_to_end.rs` does.
//!
//! # Async on one side, blocking on the other
//!
//! russh hands over a `tokio` duplex stream; `gunnar-wire` wants
//! `&mut dyn BufRead` and `&mut dyn Write`, deliberately, so it can be driven
//! by a pipe or a subprocess in tests (the git wire protocol). The stream is therefore
//! [`tokio::io::split`] into halves — one object cannot be borrowed mutably as
//! both reader and writer — each wrapped in a [`SyncIoBridge`], and the whole
//! transfer runs inside [`tokio::task::spawn_blocking`]. `SyncIoBridge` blocks
//! on the runtime, so running it on a runtime worker would deadlock the reactor
//! that has to feed it.
//!
//! # The protocol version is the client's to choose
//!
//! A v2 client announces itself by sending `GIT_PROTOCOL=version=2` as an SSH
//! **environment request**. `gunnar-ssh` accepts that one variable and no
//! other (`gunnar_ssh::server::ENV_ALLOWLIST`), and carries the raw value
//! on [`ServiceRequest::git_protocol`]. [`protocol_version`] turns it
//! into a [`ProtocolVersion`] with `gunnar-wire`'s own parser, so there is one
//! implementation of the grammar rather than two.
//!
//! A client that sends nothing gets **v0**, which is what `ProtocolVersion`'s
//! default already means and what `gunnar-wire`'s v0 fallback exists for. That
//! path is not vestigial: it is what an old client, or one behind a transport
//! that drops the variable, actually gets, and `tests/ssh_end_to_end.rs`
//! exercises it explicitly rather than assuming it.
//!
//! **What this unblocked, measured 2026-08-03.** While every session was v0,
//! `gunnar clone` could not clone from `gunnar serve` at all:
//!
//! ```text
//! gunnar clone ssh://git@127.0.0.1:2299/team/project.git
//!   → Could not decode server reply: Currently we require feature
//!     "multi_ack_detailed", which is not supported by the server
//! ```
//!
//! `gunnar-client` fetches through `nornir-git` → `gix`'s blocking transport,
//! which demands `multi_ack_detailed` in v0. `gunnar_wire::caps` used not to
//! advertise it, on the grounds that gunnar negotiated in single-ACK mode and
//! advertising more than is implemented is a bug. **It advertises and
//! implements it now** (smart HTTP): single-ACK cannot complete a stateless-rpc
//! negotiation, so smart HTTP forced the mode to be built properly rather than
//! declined. Delivering `GIT_PROTOCOL` still matters — v2 is the better
//! protocol and `gix` asks for it by itself — but the v0 fallback is no longer
//! a capability gunnar refuses `gix` on paper. That fallback is not exercised
//! here, so treat this paragraph as a statement about the advertisement, which
//! `gunnar-wire/tests/advertisement.rs` asserts, and not as a measurement of
//! `gix`'s v0 client.
//!
//! `receive-pack` is **not** given the negotiated version, and that is not an
//! oversight: protocol v2 has no push side. `git` sends `GIT_PROTOCOL` on a
//! push too and then speaks v0 anyway, so passing it on would be inventing a
//! protocol neither end implements.
//!
//! # This module is the event-stream producer, and the attestation one
//!
//! `Events.Watch` had a bus, a filter, a gap marker and its own tests. For
//! `PushAttestation` it had no producer at all. A stream with nothing behind it
//! connects, delivers nothing and is indistinguishable from a quiet server.
//! [`serve_receive_pack`] is where a push becomes events:
//!
//! ```text
//!   refs applied ── status reported ──┬── RefUpdated / RefDeleted / PushRejected
//!                                     └── PushAttestation ──┬── the event bus
//!                                                           └── attestations.jsonl
//! ```
//!
//! Everything is published **after** the report is on the wire. A push must
//! never fail because nobody was watching, and it must never be *reported* as
//! having done something the client has not yet been told about.
//!
//! ## Why there is no publish callback in `gunnar-wire`
//!
//! There does not need to be one. `ReceivePack::serve` already returns a
//! [`PushOutcome`] carrying every command, its status and the indexed pack,
//! which is the input an event needs. A `Publisher` trait threaded
//! through `gunnar-wire` would be a second path to the same facts, live at a
//! point where the report has *not* yet been written, and a second thing to
//! keep in step (one implementation). The coupling `gunnar-wire` must not grow is a
//! dependency on `gunnar-grpc`, and returning a value cannot create one.
//!
//! ## The dependency edge push attestation forbids
//!
//! gunnar does not link the Build Thing, nornir, skade, arrow or parquet for
//! any of this. The event is a proto message and the log is a file of JSON
//! lines; anything wanting a warehouse row subscribes and writes it itself.

use std::collections::HashMap;
use std::future::Future;
use std::io::{BufRead, BufReader, BufWriter, Write as _};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use gix_hash::ObjectId;
use gunnar_core::observe::{ServerMetrics, TARGET_GIT, TARGET_POLICY, TARGET_STORE};
use gunnar_grpc::EventBus;
use gunnar_policy::{
    KeyDirectory, PolicyConfig, PolicyEngine, PushContext, RefUpdate, RepoId, StaticKeyDirectory,
};
use gunnar_proto::{event, PushAttestation, PushRejected, RefDeleted, RefUpdated, RepoPath};
use gunnar_ssh::seam::{Delivery, DuplexStream, GitTransport, ServiceRequest, TransportError};
use gunnar_ssh::GitService;
use gunnar_store::git_storage_trait::GitServe;
use gunnar_wire::{
    PackReceipt, ProtocolVersion, PushFacts, PushGate, PushOutcome, ReceivePack, RefCommand,
    RefStatus, Session, UploadPack,
};
use tokio_util::io::SyncIoBridge;

use crate::attest::AttestationLog;
use crate::storage::OpenRepo;
use crate::stores::ServerStores;

/// The per-repository policy document, read from the repository's git dir.
///
/// `gunnar-core` has no storage for branch protection or ref-name policy (see
/// the report in the crate docs), so the gate configuration comes from a file
/// beside the repository until it does. When the catalog grows a policy
/// document, this constant is the only thing that changes.
pub const POLICY_FILE: &str = "gunnar-policy.json";

/// Which protocol version to answer a request in.
///
/// The client chooses, by sending `GIT_PROTOCOL` as an SSH environment request;
/// `gunnar-ssh` allowlists that one name and carries the raw value here. The
/// grammar is parsed **once**, by `gunnar-wire`, so a v1 announcement cannot
/// mean one thing to the SSH layer and another to the wire (one implementation).
///
/// A request that carries nothing is v0, and not by a default that happens to
/// land on v0: that is what `from_git_protocol(None)` says.
fn protocol_version(request: &ServiceRequest) -> ProtocolVersion {
    ProtocolVersion::from_git_protocol(request.git_protocol.as_deref())
}

/// **The push's standing in its namespace's byte ledger, as the receive loop
/// asks about it.**
///
/// Four lines, and they are the whole of the third seam this file holds. The
/// other two are [`protocol_version`] and [`wire_session`], and this one exists
/// for the same reason they do: `gunnar-ssh` and `gunnar-wire` deliberately do
/// not depend on each other, and this crate is the only one that holds both.
///
/// The bug it closes is a time-of-check/time-of-use on a quota. Every push was
/// handed `PlanGate::pack_ceiling`'s number — the namespace's remaining room
/// plus slack — and held to the whole of it, so N pushes that arrived together
/// were each admitted against the same measurement and the room was spent N
/// times. `gunnar_lfs::Mark` makes the number a budget the namespace shares:
/// the receive loop charges every chunk before it takes it, against everything
/// admitted since this push measured.
///
/// # What happened to the slack
///
/// Nothing, and that is deliberate. [`crate::entitlement`] still computes
/// `remaining + max(cap / 16, 64 MiB)` and this file still passes that number
/// through untouched; the ledger adds no arithmetic of its own and never
/// re-adds the slack per connection. What changed is only WHO spends it: the
/// 6.25 % (floor 64 MiB) that covers the difference between a thin pack's wire
/// bytes and its stored bytes is now one allowance for the namespace, charged
/// once, instead of one allowance per simultaneous push. That is the direction
/// the slack was argued for in the first place — it exists so the push that is
/// landing is not cut by arithmetic nobody can do in advance, not so that eight
/// pushes may each have one.
///
/// # Why an adapter rather than the trait on `Mark` itself
///
/// Because `gunnar-lfs` would then have to name `gunnar_wire::PackLedger`, and
/// `gunnar-lfs` is a leaf (44 packages) that `gunnar-lfs-serve` ships on its
/// own; `gunnar-wire` cannot name `gunnar_lfs::Mark` either, being bound by the
/// no-OS rule and the dependency budget to plumbing. An `Arc<dyn PackLedger>`
/// built here costs one allocation per push, adds no edge to either crate's
/// graph, and puts the conversion where every other conversion between those two
/// already is.
#[derive(Debug)]
pub(crate) struct PackMark {
    pub(crate) mark: gunnar_lfs::Mark,
}

impl gunnar_wire::PackLedger for PackMark {
    /// Pure forwarding, on purpose: `room` is the receive loop's number (the
    /// ceiling plus the one byte that makes it reachable) and the arithmetic is
    /// the ledger's. Anything computed here would be a third place where a byte
    /// ceiling is decided.
    fn admit_upto(&self, want: u64, room: u64) -> u64 {
        self.mark.admit_upto(want, room)
    }
}

/// How much of a conversation this call carries, as `gunnar-wire` names it.
///
/// [`Delivery`] is the seam's word and [`Session`] is the wire's, and they are
/// two enums rather than one because `gunnar-ssh` deliberately does not depend
/// on `gunnar-wire` (see `ServiceRequest::git_protocol`). This is the whole of
/// the translation, in one place, so the mapping cannot be made differently in
/// two.
///
/// [`Delivery::Advertisement`] has no `Session` at all: it is not a session,
/// it is a `GET` that writes the advertisement and reads nothing, and it is
/// handled before this is consulted.
fn wire_session(request: &ServiceRequest) -> Session {
    match request.delivery {
        Delivery::Session | Delivery::Advertisement => Session::Connected,
        Delivery::StatelessRpc => Session::StatelessRpc,
    }
}

/// **What `gunnar-wire` is configured with, for both directions.**
///
/// `gunnar-wire` has had these knobs since it was written, and a
/// `with_config` on both state machines to receive them. Nothing ever called
/// either: this module said `UploadPack::new(…)` and `ReceivePack::new(…)`, so
/// every deployment ran on `Config::default()` and **an operator could not
/// decline `filter`** — the one capability the type's own doc comment says is
/// "the one capability an operator plausibly wants to decline".
///
/// A configuration surface that cannot affect behaviour is worse than none.
/// It reads as a control that works: an appliance that had to state it does
/// not serve partial clones had `Offered { filter: false }` sitting right
/// there, fully implemented in both protocol versions, and no way at all to
/// reach it. So the knobs are wired rather than deleted, and this struct is
/// the one place the two `gunnar_wire::…::Config` types are held (one implementation) — a
/// second copy of `agent` or `budget` on `ServeConfig` is how a value ends up
/// honoured on one direction and stubbed on the other.
#[derive(Debug, Clone)]
pub struct WireConfig {
    /// What `git-upload-pack` serves under: `offered`, `want_policy`, `budget`
    /// and `limits`.
    pub upload: gunnar_wire::upload_pack::Config,
    /// What `git-receive-pack` serves under: the connectivity budget.
    pub receive: gunnar_wire::receive_pack::Config,
    /// What `git-lfs-transfer` serves under.
    ///
    /// Held here and not beside the store for the same reason the two above
    /// are: the LFS session is a third service on this one listener, and a
    /// second home for its quota is how 8 GiB comes to be enforced by the SSH
    /// path and not by the next caller. `gunnar_lfs::Session::with_limits` and
    /// the three builders behind it had **zero** call sites outside
    /// `tests/quota.rs` — every deployment ran on `Limits::default()` and the
    /// numbers in the doc comments were not reachable from any command line.
    pub lfs: gunnar_lfs::Limits,
    /// Enumerate the objects each push introduced, so the push attestation can
    /// name them. **On by default.**
    ///
    /// The ids are not a by-product of storing a pack — a pack entry does not
    /// carry its oid — so answering this waits for the engine's indexer to
    /// drain, which is real latency on the push path
    /// (`storage::znippy_engine::ingest` has the measured multiples). Before
    /// this flag existed the enumeration happened only when a policy gate
    /// needed it, and an attestation on a repository with no gates therefore
    /// carried an empty object list that was indistinguishable from a push
    /// that introduced nothing.
    ///
    /// Turning it **off** does not restore that silence: the attestation then
    /// records the reason the set is absent (`new_object_oids_unknown`), because an
    /// absence that renders as a value is the defect this flag was added to
    /// remove, not the behaviour it restores.
    pub attest_objects: bool,
}

impl Default for WireConfig {
    /// Hand-written for one field. `#[derive(Default)]` would make
    /// [`WireConfig::attest_objects`] `false`, and the safe default here is the
    /// one that produces a complete attestation — the cheap default is the one
    /// an operator opts into, never the one they get by forgetting.
    fn default() -> Self {
        WireConfig {
            upload: Default::default(),
            receive: Default::default(),
            lfs: Default::default(),
            attest_objects: true,
        }
    }
}

impl WireConfig {
    /// The knobs an operator set on `gunnar serve`, translated once.
    ///
    /// `None` on every flag means "the operator did not say", which takes
    /// `gunnar-wire`'s default — the same convention `--max-concurrent-transfers`
    /// already uses. `0` is a *stated* value and is treated as one:
    ///
    /// * on the two byte ceilings it is the documented way to remove them,
    ///   because `Limits`' fields are `Option` and `None` means unbounded;
    /// * on `--fetch-budget` it is **refused**, because the walk stops when it
    ///   reaches the budget, so `0` would mean a server that answers no fetch
    ///   at all while reading as "no limit". An unbounded-looking flag that
    ///   silently bricks the server is the failure this whole item is about.
    pub fn from_serve_args(args: &gunnar_common::cli::ServeArgs) -> anyhow::Result<Self> {
        let mut wire = WireConfig::default();
        if args.no_attest_objects {
            wire.attest_objects = false;
        }
        if args.no_filter {
            wire.upload.offered.filter = false;
        }
        if let Some(policy) = args.want_policy {
            wire.upload.want_policy = policy.to_wire();
        }
        if let Some(budget) = args.fetch_budget {
            anyhow::ensure!(
                budget > 0,
                "--fetch-budget 0 would refuse every fetch rather than allow every fetch: the \
                 walk stops when it REACHES the budget. Leave the flag off for the default of \
                 {}, or give the number of objects a fetch may visit.",
                gunnar_wire::DEFAULT_BUDGET
            );
            wire.upload.budget = Some(budget);
        }
        if let Some(bytes) = args.max_fetch_request_bytes {
            wire.upload.limits.request_bytes = (bytes > 0).then_some(bytes);
        }
        if let Some(bytes) = args.max_fetch_object_bytes {
            wire.upload.limits.object_bytes = (bytes > 0).then_some(bytes);
        }
        if let Some(bytes) = args.max_fetch_walk_bytes {
            wire.upload.limits.walk_bytes = (bytes > 0).then_some(bytes);
        }
        // ── the ceiling that bounds the BOX, not the request ─────────────────
        //
        // Built here rather than defaulted in `gunnar-wire`, because a pool is
        // only a pool if every connection gets the same `Arc`. A default inside
        // `Config` would give each session a private purse of its own, which
        // reads exactly like a server-wide ceiling and is not one.
        //
        // `for_this_machine` reads `MemTotal`. The alternative — a constant —
        // would be a memory ceiling chosen without knowing how much memory
        // there is, which is the mistake `DEFAULT_BUDGET` made in objects.
        wire.upload.walk_pool = match args.max_walk_bytes_total {
            Some(0) => None,
            Some(bytes) => Some(gunnar_wire::WalkPool::new(bytes)),
            None => Some(gunnar_wire::WalkPool::for_this_machine()),
        };
        // The LFS three are `u64`/`usize` and not `Option`, so `0` is a stated
        // "refuse everything" rather than the "unbounded" the two flags above
        // mean by it. Same call as `--fetch-budget`: refuse it at startup,
        // because a flag that reads as "no limit" and bricks the service is the
        // failure this whole item is about.
        if let Some(bytes) = args.max_lfs_object_bytes {
            anyhow::ensure!(
                bytes > 0,
                "--max-lfs-object-bytes 0 would refuse every upload, not allow every upload. \
                 Leave the flag off for the default of {} bytes.",
                gunnar_lfs::DEFAULT_MAX_OBJECT_BYTES
            );
            wire.lfs = wire.lfs.max_object_bytes(bytes);
        }
        if let Some(objects) = args.max_lfs_batch_objects {
            anyhow::ensure!(
                objects > 0,
                "--max-lfs-batch-objects 0 would refuse every batch. Leave the flag off for the \
                 default of {}.",
                gunnar_lfs::DEFAULT_MAX_BATCH_OBJECTS
            );
            wire.lfs = wire.lfs.max_batch_objects(objects);
        }
        if let Some(bytes) = args.max_lfs_drain_bytes {
            anyhow::ensure!(
                bytes > 0,
                "--max-lfs-drain-bytes 0 would drop the channel on the first refusal instead of \
                 draining to the client's flush-pkt, so the refusal itself would not be \
                 delivered. Leave the flag off for the default."
            );
            wire.lfs = wire.lfs.max_drain_bytes(bytes);
        }
        Ok(wire)
    }

    /// **The one place an LFS session is built.**
    ///
    /// By construction rather than by a guard that watches two call sites
    /// agree (one implementation). The defect this closes was exactly a second construction
    /// path: `Session::new(…).serve(…)` in the SSH transport, with
    /// `with_limits` reachable only from `tests/quota.rs`. A test that built
    /// its own session could be green with the server on `Limits::default()`
    /// forever, so the test drives *this* and there is nothing else to drive.
    pub fn lfs_session<'a>(
        &self,
        store: &'a dyn gunnar_lfs::LfsStore,
        operation: gunnar_lfs::Operation,
    ) -> gunnar_lfs::Session<'a> {
        gunnar_lfs::Session::new(store, operation).with_limits(self.lfs)
    }
}

/// `gunnar-wire`, wired to the SSH listener.
pub struct WireTransport {
    stores: Arc<ServerStores>,
    bus: Option<Arc<EventBus>>,
    attestations: AttestationLog,
    metrics: Arc<ServerMetrics>,
    wire: Arc<WireConfig>,
}

impl WireTransport {
    /// Serve repositories out of `stores`.
    pub fn new(stores: Arc<ServerStores>) -> Self {
        let attestations = AttestationLog::under(stores.root());
        WireTransport {
            stores,
            bus: None,
            attestations,
            metrics: Arc::new(ServerMetrics::new()),
            wire: Arc::new(WireConfig::default()),
        }
    }

    /// Serve under `wire` rather than `gunnar-wire`'s defaults.
    ///
    /// The whole of what makes [`WireConfig`] reachable. Without a call to
    /// this, every knob on it is a comment.
    pub fn with_wire_config(mut self, wire: WireConfig) -> Self {
        self.wire = Arc::new(wire);
        self
    }

    /// Count into the **server's** counters rather than into a private set.
    ///
    /// `gunnar serve` mints one [`ServerMetrics`] and hands the same `Arc` here
    /// and to the SSH listener, so `connections_refused` and `pushes_rejected`
    /// are two readings of one server. A transport built without this still
    /// counts, into a set only it can see, which is what a unit test wants.
    pub fn with_metrics(mut self, metrics: Arc<ServerMetrics>) -> Self {
        self.metrics = metrics;
        self
    }

    /// Publish `RefUpdated` / `RefDeleted` / `PushRejected` / `PushAttestation`
    /// to the event bus.
    ///
    /// **The bus is optional; the attestation log is not.** A server started
    /// with `--no-grpc` has nobody to notify, but the attestation's durable record is not a
    /// notification. It is the thing the event stream explicitly does not
    /// promise to keep (see `EventBus::publish`), so the log is written either
    /// way and only the publish is conditional.
    pub fn with_bus(mut self, bus: Arc<EventBus>) -> Self {
        self.bus = Some(bus);
        self
    }

    /// Where the attestation's JSON lines land.
    pub fn attestation_log(&self) -> &AttestationLog {
        &self.attestations
    }
}

/// **The LFS object store, opened the one way** — for the SSH session and for
/// the HTTP batch API alike (one implementation).
///
/// The two surfaces used to be one call apart from having two answers to *where
/// does this repository keep its LFS objects*: `serve_blocking` reached for
/// `stores.repo_store(...).git_dir()` and anything else would have had to
/// re-derive it. It is `<git_dir>/lfs/`, the symlink check in
/// `ServerStores::repo_dir` is not optional on either path, and the handle the
/// store lock lives on is the same handle, so a repack and an LFS transfer
/// exclude each other the same way on both transports.
impl gunnar_ssh::seam::LfsTransport for WireTransport {
    fn open_lfs(
        &self,
        request: &ServiceRequest,
    ) -> Result<gunnar_ssh::seam::LfsAccess, TransportError> {
        // An upload into a repository that does not exist yet (the
        // first-repository door, 2026-09-28): the repository is made by its
        // first object, see `ProspectiveLfs`.
        if request.prospective {
            return Ok(gunnar_ssh::seam::LfsAccess {
                store: Arc::new(ProspectiveLfs::new(
                    Arc::clone(&self.stores),
                    request.clone(),
                )),
                limits: self.wire.lfs,
                allowance: request.lfs_allowance.clone(),
            });
        }
        let store = open_repo_store(&self.stores, request)?;
        let lfs = open_lfs_store(&self.stores, &store, request)?;
        Ok(gunnar_ssh::seam::LfsAccess {
            store: Arc::new(lfs),
            // **`self.wire.lfs`, not `Limits::default()`.** The one place an
            // operator's `--max-lfs-object-bytes` is held. A separate default
            // here is exactly how a quota comes to be enforced on SSH and not
            // on HTTP; see `WireConfig::lfs`.
            limits: self.wire.lfs,
            // **The tenant's plan**, decided by the gate chain in
            // `authorise_service` and carried on the request beside the pack
            // ceiling. Cloned rather than moved: `open_lfs` takes the request by
            // reference, and one `Allowance` is two small fields.
            allowance: request.lfs_allowance.clone(),
        })
    }
}

/// Open the repository behind an **already authorised** request, or say why not
/// in the sentence the client is owed.
///
/// Shared by `serve_blocking` and [`LfsTransport::open_lfs`]. The distinction
/// between the two failures is the load-bearing part and is why this is one
/// function: a held store lock is a running `gunnar gc` (or a second gunnar)
/// and clears on its own, so the answer is "retry"; every other open failure is
/// a fault an operator has to look at. Answering both with one sentence is how
/// a two-minute wait becomes a support ticket.
///
/// `TransportError::Refused` and not `Protocol`: both call sites reach this
/// before a single byte has been written, which is what that variant *means*.
fn open_repo_store(
    stores: &ServerStores,
    request: &ServiceRequest,
) -> Result<Arc<OpenRepo>, TransportError> {
    stores.repo_store(&request.repo).map_err(|err| {
        // Opening the repository failed *after* the grant check said yes, so
        // this is a storage fault and not a decision. It used to be returned
        // silently: the client saw a dead channel and the server said nothing
        // at all.
        tracing::error!(
            target: TARGET_GIT,
            error = format!("{err:#}"),
            "the repository could not be opened for a transfer that was already authorised"
        );
        TransportError::Refused(match locked_path(&err) {
            Some(_) => format!(
                "the repository {} is locked by maintenance on the server (a repack or a \
                 second gunnar holds its store lock); the transfer was not started, so retry",
                request.repo.key()
            ),
            None => format!(
                "the repository {} could not be opened on the server; the transfer was not \
                 started. The reason is in the server log",
                request.repo.key()
            ),
        })
    })
}

/// The LFS objects under a repository's own git dir. See
/// [`gunnar_lfs::fs_store`].
fn open_lfs_store(
    stores: &ServerStores,
    store: &OpenRepo,
    request: &ServiceRequest,
) -> Result<crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>, TransportError> {
    let repo = format!("{}/{}", request.repo.namespace, request.repo.name);
    gunnar_lfs::FsLfsStore::open_or_init(store.git_dir())
        .map(|fs| {
            crate::txlog_lfs::LoggedLfs::new(fs, Arc::clone(stores.txlog()), repo)
                // Held to the data set's room, whole object at a time (cov-13).
                .with_space(stores.space_ledger())
        })
        .map_err(|err| {
            tracing::error!(
                target: TARGET_GIT,
                error = format!("{err}"),
                "the lfs object store could not be opened for an authorised transfer"
            );
            // The same `Refused`, for the same reason, at the twin of the seam
            // above: this is still before the first byte, and the LFS grammar has
            // no per-object error line to carry it later.
            TransportError::Refused(format!(
                "the lfs object store for {} could not be opened on the server; the \
             transfer was not started. The reason is in the server log",
                request.repo.key()
            ))
        })
}

/// **An LFS upload into a repository that does not exist yet** (2026-09-28):
/// the first-repository door for git-lfs's pre-push hook, which runs after
/// git took receive-pack's advertisement and BEFORE it sends a command.
/// MEASURED 2026-09-28: the first `git push` with an LFS object into a new
/// repository died in that hook — `batch request: exec request failed on
/// channel 0: exit status 255` — and neither the repository nor the ref was
/// created, while a precreated repository worked.
///
/// `Dispatch::authorise_service` admitted the upload under the SAME
/// push-to-create rules as the push (`Control::push_create_spec`: namespace
/// Admin, and server Admin for a namespace that does not exist; everyone
/// else the fused not-found). Nothing is written until an OBJECT arrives:
/// until then every object is absent (so the batch asks for all of them),
/// and a client that only batches, or hangs up, leaves nothing. The first
/// `begin_write` makes the repository through [`create_prospect`] — the
/// same writes a push-create makes, the grant asked again — or takes the one
/// a concurrent upload or push made first. After that it is the ordinary
/// store.
///
/// The repository is the push-create prospect, sha1, for the reason
/// `push_create_spec` gives; LFS objects are sha256 by their own spec
/// whatever the repository's hash is.
pub(crate) struct ProspectiveLfs<S> {
    stores: S,
    request: ServiceRequest,
    made: std::sync::OnceLock<(
        Arc<OpenRepo>,
        crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>,
    )>,
    making: Mutex<()>,
}

impl<S: std::ops::Deref<Target = ServerStores> + Send + Sync> ProspectiveLfs<S> {
    pub(crate) fn new(stores: S, request: ServiceRequest) -> Self {
        ProspectiveLfs {
            stores,
            request,
            made: std::sync::OnceLock::new(),
            making: Mutex::new(()),
        }
    }

    fn made(&self) -> Option<&crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>> {
        self.made.get().map(|(_, lfs)| lfs)
    }

    /// The store, made on first use.
    fn made_or_create(
        &self,
    ) -> gunnar_lfs::Result<&crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>> {
        if let Some(lfs) = self.made() {
            return Ok(lfs);
        }
        let _one = self.making.lock().unwrap_or_else(|p| p.into_inner());
        if let Some(lfs) = self.made() {
            return Ok(lfs);
        }
        let stores: &ServerStores = &self.stores;
        let path = self.request.repo.path();
        let (repo, store) = create_prospect(stores, &self.request).map_err(|sentence| {
            if sentence.starts_with("repository not found") {
                gunnar_lfs::Error::NotFound(sentence)
            } else {
                gunnar_lfs::Error::Store(sentence)
            }
        })?;
        let mut admitted = self.request.clone();
        admitted.repo = repo;
        admitted.prospective = false;
        let lfs = open_lfs_store(stores, &store, &admitted)
            .map_err(|err| gunnar_lfs::Error::Store(err.to_string()))?;
        tracing::info!(
            target: TARGET_GIT,
            event = "git.lfs_transfer.created_repo",
            repo = %path,
            principal = %self.request.principal.id,
            "an LFS upload's first object made the repository (the push-create door)"
        );
        Ok(&self.made.get_or_init(|| (store, lfs)).1)
    }
}

impl<S: std::ops::Deref<Target = ServerStores> + Send + Sync> gunnar_lfs::LfsStore
    for ProspectiveLfs<S>
{
    fn size_of(&self, oid: &gunnar_lfs::Oid) -> gunnar_lfs::Result<Option<u64>> {
        match self.made() {
            Some(lfs) => lfs.size_of(oid),
            None => Ok(None),
        }
    }
    fn open(&self, oid: &gunnar_lfs::Oid) -> gunnar_lfs::Result<Option<gunnar_lfs::LfsObject>> {
        match self.made() {
            Some(lfs) => lfs.open(oid),
            None => Ok(None),
        }
    }
    fn begin_write(
        &self,
        oid: &gunnar_lfs::Oid,
        declared_size: u64,
    ) -> gunnar_lfs::Result<Box<dyn gunnar_lfs::PendingWrite + '_>> {
        self.made_or_create()?.begin_write(oid, declared_size)
    }
    fn delete(&self, oid: &gunnar_lfs::Oid) -> gunnar_lfs::Result<bool> {
        match self.made() {
            Some(lfs) => lfs.delete(oid),
            None => Ok(false),
        }
    }
}

impl GitTransport for WireTransport {
    fn serve<'a>(
        &'a self,
        request: ServiceRequest,
        stream: Box<dyn DuplexStream>,
    ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'a>> {
        let stores = Arc::clone(&self.stores);
        let bus = self.bus.clone();
        let attestations = self.attestations.clone();
        let metrics = Arc::clone(&self.metrics);
        let wire = Arc::clone(&self.wire);
        Box::pin(async move {
            // `spawn_blocking` does NOT carry the current span across (it is a
            // thread hand-off, not a task spawn), so without this the whole
            // transfer — every line `gunnar-wire`, the gate and this module
            // emit — would fall out of `git.transfer` and lose the connection
            // id, the principal and the repository. Captured here, where the
            // future is polled inside `gunnar-ssh`'s instrumented task.
            let parent = tracing::Span::current();
            // One object cannot be borrowed mutably as both `BufRead` and
            // `Write`, so the duplex is split before it is bridged.
            let (read_half, write_half) = tokio::io::split(stream);
            // ★ stinky row 1 (2026-10-10): the git work hears that its client
            // is gone. `gunnar-ssh` sets the flag when the session's grace runs
            // out and then WAITS for this future, so the fan worker is held
            // until the blocking task below has actually ended; the guard sets
            // it too if this future is dropped some other way, because a
            // dropped `JoinHandle` stops nothing.
            let cancel = request.cancel.clone();
            let _stop_if_dropped = CancelOnDrop(cancel.clone());
            let mut reader = Cancellable {
                inner: SyncIoBridge::new(read_half),
                cancel: cancel.clone(),
            };
            let mut writer = Cancellable {
                inner: SyncIoBridge::new(write_half),
                cancel,
            };
            // The halves are dropped here, at the end of the blocking task, and
            // that is now safe: `gunnar-ssh` splits the russh channel instead of
            // consuming it, so no part of this stream closes the channel when it
            // goes. A 250 ms `linger` used to hold them open to win a race
            // against `ChannelCloseOnDrop`; the race is gone and so is the
            // timer.
            tokio::task::spawn_blocking(move || {
                // The blocking thread is this transfer's alone for the whole
                // duration, so entering the span here cannot leak it into
                // another task's lines.
                let _entered = parent.enter();
                serve_blocking(
                    &Serving {
                        stores: &stores,
                        bus: bus.as_deref(),
                        attestations: &attestations,
                        metrics: &metrics,
                        wire: &wire,
                    },
                    request,
                    &mut reader,
                    &mut writer,
                )
            })
            .await
            .map_err(|err| {
                TransportError::Protocol(format!("the git transport task did not finish: {err}"))
            })?
        })
    }
}

/// Sets the transfer's [`Cancel`](gunnar_ssh::channel::Cancel) when dropped:
/// a transport future dropped before its blocking task ended tells the task.
struct CancelOnDrop(gunnar_ssh::channel::Cancel);

impl Drop for CancelOnDrop {
    fn drop(&mut self) {
        self.0.cancel();
    }
}

/// **The git work's safe points**: every read from and write to the client.
///
/// Once the transfer is told to stop, the next read or write fails with
/// `ConnectionAborted` — not `Interrupted`, which `read_exact`/`write_all`
/// retry — and the work unwinds from there. The ref updates of a receive-pack
/// transaction do no stream I/O between them, so a stop never lands inside
/// one: the transaction lands whole, or (stopped while the pack was still
/// arriving) not at all.
struct Cancellable<T> {
    inner: T,
    cancel: gunnar_ssh::channel::Cancel,
}

impl<T> Cancellable<T> {
    fn check(&self) -> std::io::Result<()> {
        if self.cancel.is_cancelled() {
            return Err(told_to_stop());
        }
        Ok(())
    }
}

fn told_to_stop() -> std::io::Error {
    std::io::Error::new(
        std::io::ErrorKind::ConnectionAborted,
        "the transfer was told to stop: its client's session ended and the transfer outlived the grace",
    )
}

impl<T: std::io::Read> std::io::Read for Cancellable<T> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.check()?;
        self.inner.read(buf)
    }
}

impl<T: std::io::Write> std::io::Write for Cancellable<T> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.check()?;
        self.inner.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.check()?;
        self.inner.flush()
    }
}

fn wire_error(err: gunnar_wire::Error) -> TransportError {
    TransportError::Protocol(err.to_string())
}

/// Counts the bytes that actually cross the wire.
///
/// **Measured, not inferred.** "Objects sent" comes out of the negotiation and
/// says nothing about what a client paid for: a fetch that sends 12 objects can
/// move 4 KiB or 40 MiB. `bytes_out` is the number an operator answering
/// *"why was that clone slow"* reaches for first, and the only place it can be
/// counted honestly is at the stream.
///
/// One relaxed atomic add per `read`/`write` call, and both are already
/// buffered (`BufReader`/`BufWriter` sit on top), so this is a handful of adds
/// per transfer rather than one per byte.
struct Metered<T> {
    inner: T,
    total: Arc<AtomicU64>,
}

impl<T> Metered<T> {
    fn new(inner: T) -> (Metered<T>, Arc<AtomicU64>) {
        let total = Arc::new(AtomicU64::new(0));
        (
            Metered {
                inner,
                total: Arc::clone(&total),
            },
            total,
        )
    }
}

impl<T: std::io::Read> std::io::Read for Metered<T> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = self.inner.read(buf)?;
        self.total.fetch_add(n as u64, Ordering::Relaxed);
        Ok(n)
    }
}

impl<T: std::io::Write> std::io::Write for Metered<T> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let n = self.inner.write(buf)?;
        self.total.fetch_add(n as u64, Ordering::Relaxed);
        Ok(n)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}

/// **THE counting point for per-namespace flow, both directions.**
///
/// It is a guard that records when it is DROPPED rather than a call at the end
/// of [`serve_blocking`], and that is the whole design. `serve_blocking` has
/// three service arms, an early return for the abandoned prospect, an early
/// return for the advertisement, and a `?` on every fallible step; a call at
/// "the end" would be five calls, and the sixth exit somebody adds next year
/// would silently stop counting a whole transport. A guard cannot be
/// forgotten, and it counts the failed transfers too — the bytes a client sent
/// before it hung up were still received, and a tenant who is failing is
/// exactly the tenant an operator wants to see.
///
/// It reads the SAME two counters [`Metered`] already keeps for
/// `ServerMetrics`, so a byte is counted once for the box and once for the
/// tenant off one measurement, and the two can never disagree about how much
/// moved. Nothing here counts bytes itself.
///
/// The three `Cell` fields are what the arm that ran decides:
///
/// * `fetched` — an upload-pack SESSION, set after the advertisement's early
///   return, so `git ls-remote` and a monitoring probe are bytes and not a
///   customer's fetch;
/// * `pushed` — a receive-pack session that came back `Ok`;
/// * the LFS pair — the session's own applied figures, which are a different
///   number from the wire bytes about the same transfer.
struct FlowRecording<'a> {
    flow: &'a gunnar_core::FlowLedger,
    namespace: String,
    principal: String,
    bytes_in: Arc<AtomicU64>,
    bytes_out: Arc<AtomicU64>,
    fetched: std::cell::Cell<bool>,
    pushed: std::cell::Cell<bool>,
    lfs_bytes_in: std::cell::Cell<u64>,
    lfs_bytes_out: std::cell::Cell<u64>,
}

impl Drop for FlowRecording<'_> {
    fn drop(&mut self) {
        self.flow.record(
            &self.namespace,
            gunnar_core::now_ms(),
            &gunnar_core::Transfer {
                bytes_in: self.bytes_in.load(Ordering::Relaxed),
                bytes_out: self.bytes_out.load(Ordering::Relaxed),
                pushed: self.pushed.get(),
                fetched: self.fetched.get(),
                lfs_bytes_in: self.lfs_bytes_in.get(),
                lfs_bytes_out: self.lfs_bytes_out.get(),
                principal: self.principal.clone(),
            },
        );
    }
}

/// The lock file, when `err` is a store whose exclusive lock is already held.
///
/// `gunnar gc` opens the repository the same way a transfer does, and the
/// `O_EXCL` lock the Arrow log takes is what makes "one writer" true across
/// processes — so a repack and a transfer on the same repository are mutually
/// exclusive **by that lock and nothing else**. Whichever loses gets
/// [`gunnar_store::Error::StoreLocked`] out of `open_or_init`, wrapped in
/// `anyhow` context by `ServerStores::repo_store`, which is why this walks the
/// chain rather than matching the top-level error.
///
/// Recognising it is not cosmetic: "retry in a moment" and "an operator has to
/// look at this" are opposite instructions, and the caller cannot tell them
/// apart from a formatted string without matching on prose.
fn locked_path(err: &anyhow::Error) -> Option<&std::path::Path> {
    match err.downcast_ref::<gunnar_store::Error>() {
        Some(gunnar_store::Error::StoreLocked { path }) => Some(path.as_path()),
        _ => None,
    }
}

/// Everything a transfer needs that belongs to the **server** rather than to
/// the request.
///
/// One borrow instead of five. `serve_blocking` grew an eighth argument when
/// `WireConfig` was wired through and clippy's `too_many_arguments` caught it,
/// which is the right moment to notice that five of those arguments are the
/// same five fields `WireTransport` holds and are passed together everywhere
/// they are passed at all. Bundling them is what the lint is for; an
/// `#[allow]` would have kept the shape that made a sixth easy to add.
struct Serving<'a> {
    stores: &'a ServerStores,
    bus: Option<&'a EventBus>,
    attestations: &'a AttestationLog,
    metrics: &'a Arc<ServerMetrics>,
    wire: &'a WireConfig,
}

fn serve_blocking(
    serving: &Serving<'_>,
    request: ServiceRequest,
    reader: &mut dyn std::io::Read,
    writer: &mut dyn std::io::Write,
) -> Result<(), TransportError> {
    // `bus` and `attestations` are not destructured here: they are used only by
    // `serve_receive_pack`, which takes the whole `Serving`.
    let Serving {
        stores,
        metrics,
        wire,
        ..
    } = *serving;
    // `Refused`, not `Protocol`: this is the one point in `serve_blocking` at
    // which **nothing has been written** — `Metered` is constructed on the next
    // lines and the state machine has not been entered — so the client can still
    // be handed a pkt-line, and `TransportError::Refused` is exactly the claim
    // "no byte has gone out yet". `open_repo_store` is shared with the HTTP LFS
    // seam so the two transports refuse a locked repository in one sentence and
    // not two (one implementation).
    //
    // A PROSPECTIVE request — the first-repository door — has no store to
    // open and must not get one here: `repo_store` would create the directory
    // for a push the client may abandon at the advertisement. That path goes
    // through `admit_prospective_push` below, once the buffers exist.
    if request.cancel.is_cancelled() {
        return Err(TransportError::Io(told_to_stop()));
    }
    let store = if request.prospective {
        None
    } else {
        Some(open_repo_store(stores, &request)?)
    };
    let (metered_in, bytes_in) = Metered::new(reader);
    let (metered_out, bytes_out) = Metered::new(writer);
    // **The per-namespace flow counting point.** Created here, beside the two
    // counters it reads, and recorded when it is dropped — on every exit from
    // this function, including the early returns and the failures. See
    // `FlowRecording`. It is created BEFORE the first-repository door below, so
    // that a client which took an advertisement of a repository it never
    // created still has its bytes charged to the namespace it asked about.
    let flow = FlowRecording {
        flow: stores.flow().as_ref(),
        namespace: request.repo.namespace.clone(),
        principal: request
            .key_fingerprint
            .as_ref()
            .map(|f| f.to_string())
            .unwrap_or_default(),
        bytes_in: Arc::clone(&bytes_in),
        bytes_out: Arc::clone(&bytes_out),
        fetched: std::cell::Cell::new(false),
        pushed: std::cell::Cell::new(false),
        lfs_bytes_in: std::cell::Cell::new(0),
        lfs_bytes_out: std::cell::Cell::new(0),
    };
    // **`BufWriter::new` is 8 KiB, and a sideband packet is 65 520.** So the
    // default capacity did not buffer the pack at all: every packet flushed the
    // 5-byte header as a write of its own and then passed 65 515 bytes straight
    // through. Two writes per packet, one of them five bytes — and under this
    // transport a write is not a syscall, it is a hand-off across a
    // `tokio::io::duplex` from a `spawn_blocking` thread, which is a `block_on`,
    // a waker and (whenever the reader is parked) a futex pair.
    // `gunnar_wire::TRANSPORT_BUFFER_BYTES` carries the arithmetic and the
    // write-counting guard.
    let mut input = BufReader::with_capacity(gunnar_wire::TRANSPORT_BUFFER_BYTES, metered_in);
    let mut output = BufWriter::with_capacity(gunnar_wire::TRANSPORT_BUFFER_BYTES, metered_out);
    let started = std::time::Instant::now();

    // ── the first-repository door: advertise, read, check, THEN create ──────
    //
    // Nothing has been written to the catalog or the disk for a prospective
    // request. The client is shown the empty repository it would be pushing
    // into; if it hangs up (a sha1 tree that read `object-format=sha256`,
    // `git push` with nothing to do) this returns and nothing exists. Only a
    // command list that passes the hash check turns into a repository — and
    // that create is `ServerStores::create_repo`, the same transactional verb
    // every other create uses.
    //
    // A prospective LFS UPLOAD (git-lfs's pre-push, before receive-pack's
    // commands) goes on with no store at all: `ProspectiveLfs` makes the
    // repository through the same writes when the first object arrives.
    let (request, store, preamble) = match store {
        Some(store) => (request, Some(store), None),
        None if matches!(request.service, GitService::LfsTransfer(_)) => (request, None, None),
        None => match admit_prospective_push(serving, &request, &mut input, &mut output)? {
            Some((request, store, preamble)) => (request, Some(store), Some(preamble)),
            None => {
                output.flush()?;
                metrics.add_bytes_out(bytes_out.load(Ordering::Relaxed));
                metrics.add_bytes_in(bytes_in.load(Ordering::Relaxed));
                tracing::info!(
                    target: TARGET_GIT,
                    event = "git.receive_pack.prospect_abandoned",
                    repo = %request.repo.key(),
                    elapsed_ms = started.elapsed().as_millis() as u64,
                    "the client took the advertisement of a repository that does not exist and \
                     sent no command; nothing was created"
                );
                return Ok(());
            }
        },
    };

    // Only a prospective LFS upload reaches here without a store.
    let git_store = || {
        store.clone().ok_or_else(|| {
            TransportError::Refused(format!(
                "repository not found, or you do not have access: {}",
                request.repo.path()
            ))
        })
    };
    match request.service {
        GitService::UploadPack => {
            let span = tracing::info_span!(target: TARGET_GIT, "git.upload_pack");
            let _entered = span.enter();
            let store = git_store()?;
            let version = protocol_version(&request);
            // ── the znippy engine answers reads ─────────────────────────────────
            //
            // When `GUNNAR_COLD_TIER=znippy` and the repository has an archive,
            // upload-pack serves pack ENTRIES out of that archive rather than
            // out of `objects/pack` — no unfold, no file. `packed()` is `Some`
            // on both arms, so the entry-copy path is the same code; only where
            // the bytes come from changes.
            //
            // receive-pack is deliberately NOT switched: a push lands on `fs`
            // and reaches the archive through the append hook. The archive is
            // immutable, and `ZnippyObjects::write` says so rather than
            // pretending.
            //
            // There is no second read side to choose between any more. The
            // engine *is* the archive on the znippy arm, so `GUNNAR_COLD_TIER`,
            // `cold_objects()` and the `Box<dyn ObjectStore>` that carried a
            // `None` for "this binary has no znippy engine" have all gone with
            // the old znippy engine crate: one store answers reads and writes, and which
            // engine it is was decided at startup.
            //
            // **One handle, not two.** `UploadPack` used to take an object
            // store and a ref store, and an advertisement built from one
            // snapshot could promise a pack built from another. `GitServe`
            // carries both halves, so the ref list and the pack it promises are
            // the same store.
            let git = store.serve();
            // `hash_kind` is a required argument, never a default: it comes
            // from the catalog entry through `OpenRepo`, and answering `sha1`
            // for a sha256 repository does not error — it decodes garbage.
            //
            // `with_config`, not `new`: this call is the whole of what makes
            // `--no-filter`, `--want-policy`, `--fetch-budget` and the byte
            // ceilings anything other than fields nobody reads.
            let upload = UploadPack::new(git, store.hash_kind()).with_config(wire.upload.clone());
            // `Delivery::Advertisement` is `GET /info/refs`: write the ref list
            // (v0) or the capability list (v2) and stop, reading nothing. The
            // same call the connected path makes inline, so the two transports
            // cannot advertise differently.
            if request.delivery == Delivery::Advertisement {
                upload.advertise(version, &mut output).map_err(wire_error)?;
                output.flush()?;
                let out = bytes_out.load(Ordering::Relaxed);
                metrics.add_bytes_out(out);
                tracing::info!(
                    target: TARGET_GIT,
                    event = "git.upload_pack.advertised",
                    protocol = version.number(),
                    bytes_out = out,
                    elapsed_ms = started.elapsed().as_millis() as u64,
                    "served the upload-pack advertisement"
                );
                return Ok(());
            }
            // Past the advertisement's early return above: this is a SESSION,
            // which is what `fetches` counts. A bare `git ls-remote` never
            // reaches here.
            flow.fetched.set(true);
            let summary = upload
                .serve_session(version, wire_session(&request), &mut input, &mut output)
                .map_err(|err| {
                    tracing::warn!(
                        target: TARGET_GIT,
                        event = "git.upload_pack.failed",
                        elapsed_ms = started.elapsed().as_millis() as u64,
                        bytes_out = bytes_out.load(Ordering::Relaxed),
                        %err,
                        "git-upload-pack failed"
                    );
                    wire_error(err)
                })?;
            output.flush()?;
            let out = bytes_out.load(Ordering::Relaxed);
            if summary.objects_sent > 0 {
                metrics.add_packs_served(1);
            }
            metrics.add_objects_sent(summary.objects_sent as u64);
            metrics.add_bytes_out(out);
            metrics.add_bytes_in(bytes_in.load(Ordering::Relaxed));
            // The applied outputs, and the duration. `repo` and `principal` are
            // NOT repeated: they are fields of `ssh.connection`, which this span
            // descends from.
            tracing::info!(
                target: TARGET_GIT,
                event = "git.upload_pack.served",
                // What was actually spoken, taken from the session's own
                // summary rather than from the request that asked for it; a v2
                // request served as v0 would otherwise log as v2.
                protocol = summary.protocol.number(),
                ls_refs = summary.ls_refs,
                rounds = summary.fetch_rounds,
                common = summary.common,
                objects = summary.objects_sent,
                // **WHICH PATH ANSWERED.** `Selection::answered_from_bitmap`
                // exists so a guard can tell a fetch served from the store's
                // reachability index from one served by walking, and until now
                // nothing anywhere read it: not a log line, not a metric, not a
                // test. Its only reader in the constellation was an example
                // binary.
                //
                // That blind spot has already cost something concrete. The
                // `--fetch-budget` control charged the ids a WALKER holds, and
                // on the store the appliance actually runs there is no walker —
                // the index answers whole — so the budget was compared against
                // nothing while a red test sat in front of three separate call
                // sites and each read it as a policy question. A field nobody can
                // observe is a behaviour nobody can assert.
                from_index = summary.answered_from_bitmap,
                bytes_out = out,
                elapsed_ms = started.elapsed().as_millis() as u64,
                "served git-upload-pack"
            );
        }
        GitService::ReceivePack => {
            let span = tracing::info_span!(target: TARGET_GIT, "git.receive_pack");
            let _entered = span.enter();
            let store = git_store()?;
            let outcome =
                serve_receive_pack(serving, &request, &store, preamble, &mut input, &mut output);
            let out = bytes_out.load(Ordering::Relaxed);
            let inn = bytes_in.load(Ordering::Relaxed);
            metrics.add_bytes_out(out);
            metrics.add_bytes_in(inn);
            // A push is a receive-pack session that CAME BACK. The bytes are
            // charged either way — they arrived — but a session that failed is
            // not a push, and counting it as one would make a tenant whose every
            // push is refused look like a tenant who is working.
            flow.pushed.set(outcome.is_ok());
            if let Err(err) = &outcome {
                tracing::warn!(
                    target: TARGET_GIT,
                    event = "git.receive_pack.failed",
                    elapsed_ms = started.elapsed().as_millis() as u64,
                    bytes_in = inn,
                    %err,
                    "git-receive-pack failed"
                );
            }
            outcome?;
        }
        // LFS over SSH, git-lfs 3.0's pure-SSH transfer. The grant was already decided
        // by `gunnar_ssh::command::parse`, from the same key table and the same
        // path sandbox the two verbs above went through: `operation` here is the
        // one that demanded Read or Write, not a second decision. It is passed
        // straight to the session so there is no second authorisation path.
        //
        // The objects live under the repository's own git dir, in `lfs/`. See
        // `gunnar_lfs::fs_store`.
        GitService::LfsTransfer(operation) => {
            let span = tracing::info_span!(target: TARGET_GIT, "git.lfs_transfer");
            let _entered = span.enter();
            let lfs: Box<dyn gunnar_lfs::LfsStore + '_> = match &store {
                Some(store) => Box::new(open_lfs_store(stores, store, &request)?),
                None => Box::new(ProspectiveLfs::new(stores, request.clone())),
            };
            let mut session = wire.lfs_session(lfs.as_ref(), operation);
            // **The tenant's remaining LFS room**, from the gate chain that ran
            // in `authorise_service`. `None` on a community build, on an
            // uncapped namespace and on every download; see
            // `gunnar_ssh::PlanGate::lfs_allowance`.
            if let Some(allowance) = request.lfs_allowance.clone() {
                session = session.with_allowance(allowance);
            }
            // `limits()` and `allowance()` read back off the session, not off
            // `wire` and not off the request: what is logged is what the session
            // will enforce, so a builder that stopped taking would show here
            // rather than in a support ticket.
            let effective = session.limits();
            tracing::debug!(
                target: TARGET_GIT,
                max_object_bytes = effective.max_object_bytes,
                max_batch_objects = effective.max_batch_objects,
                max_drain_bytes = effective.max_drain_bytes,
                lfs_allowance_bytes = session.allowance().map(|a| a.bytes),
                "lfs session limits"
            );
            let outcome = session.serve(&mut input, &mut output);
            output.flush()?;
            let out = bytes_out.load(Ordering::Relaxed);
            let inn = bytes_in.load(Ordering::Relaxed);
            metrics.add_bytes_out(out);
            metrics.add_bytes_in(inn);
            let summary = outcome.map_err(|err| {
                tracing::warn!(
                    target: TARGET_GIT,
                    event = "git.lfs_transfer.failed",
                    operation = %operation,
                    elapsed_ms = started.elapsed().as_millis() as u64,
                    %err,
                    "git-lfs-transfer failed"
                );
                TransportError::Protocol(err.to_string())
            })?;
            // The APPLIED figures, from the session's own count. They are a
            // different number from the wire bytes above about the same
            // transfer — the wire carries framing and the batch conversation —
            // so the flow ledger keeps both rather than one as a subset of the
            // other.
            flow.lfs_bytes_in.set(summary.bytes_received);
            flow.lfs_bytes_out.set(summary.bytes_sent);
            // Applied output: objects and bytes that really moved, taken from
            // the session's own count rather than from what was asked for.
            tracing::info!(
                target: TARGET_GIT,
                event = "git.lfs_transfer.served",
                operation = %operation,
                commands = summary.commands,
                batches = summary.batches,
                batch_objects = summary.batch_objects,
                objects_sent = summary.objects_sent,
                objects_received = summary.objects_received,
                lfs_bytes_out = summary.bytes_sent,
                lfs_bytes_in = summary.bytes_received,
                clean_quit = summary.clean_quit,
                bytes_out = out,
                bytes_in = inn,
                elapsed_ms = started.elapsed().as_millis() as u64,
                "served git-lfs-transfer"
            );
        }
    }
    Ok(())
}

// Eight, because the counters joined the six things a push already needed.
// Bundling them into a context struct would be one more type whose
// only job is to be unpacked on the first line of this function.
#[allow(clippy::too_many_arguments)]
/// The door's commit. Advertise the prospect, read the client's commands,
/// and — only if there are any and they pass the hash check — write the
/// catalog row (the namespace with it, if the admin's push names a new one)
/// and make the store. `None` means the client sent nothing and nothing was
/// made.
///
/// A refusal after the preamble (a stale store at the name, a grant revoked
/// since the exec, a store the engine cannot make) is answered in git's own
/// protocol — an `ERR` packet, which `git push` renders as `remote error:` —
/// and leaves nothing: the row is retracted, the namespace too if this call
/// conjured it, the directory removed.
fn admit_prospective_push(
    serving: &Serving<'_>,
    request: &ServiceRequest,
    input: &mut dyn BufRead,
    output: &mut dyn std::io::Write,
) -> Result<Option<(ServiceRequest, Arc<OpenRepo>, gunnar_wire::Preamble)>, TransportError> {
    let Serving { stores, wire, .. } = *serving;
    if request.service != GitService::ReceivePack {
        // An LFS upload's door is `ProspectiveLfs`, never this one; anything
        // else here is a programming error, refused rather than
        // served against a repository that does not exist.
        return Err(TransportError::Refused(format!(
            "repository not found, or you do not have access: {}",
            request.repo.path()
        )));
    }
    let hash_kind: gix_hash::Kind = request.repo.hash_kind.into();
    if wire_session(request).advertises_inline() || request.delivery == Delivery::Advertisement {
        ReceivePack::write_empty_advertisement(hash_kind, &wire.receive, output)
            .map_err(wire_error)?;
        if request.delivery == Delivery::Advertisement {
            return Ok(None);
        }
    }
    let Some(preamble) = ReceivePack::read_preamble(hash_kind, input).map_err(wire_error)? else {
        return Ok(None);
    };

    // The client means it. Now, and only now, the writes — in the order that
    // leaves nothing behind if any of them fails.
    match create_prospect(stores, request) {
        Ok((repo, store)) => {
            let mut admitted = request.clone();
            admitted.repo = repo;
            admitted.prospective = false;
            Ok(Some((admitted, store, preamble)))
        }
        Err(sentence) => {
            let _ = gunnar_wire::err_packet(output, &sentence);
            let _ = output.flush();
            Err(TransportError::Protocol(sentence))
        }
    }
}

/// **The first-repository door's writes**, for a prospective request whose
/// client has shown it means it — a receive-pack's commands past the hash
/// check, or an LFS upload's first object ([`ProspectiveLfs`]). The stale
/// store check, the catalog row (and the namespace, if the admin's push names
/// a new one) under [`gunnar_ssh::Control::admit_push_create`]'s re-asked
/// grant, then the store — and if the store cannot be made, the row and the
/// namespace are taken back and the directory removed. `Err` is the sentence
/// the client is owed; nothing is left behind by it.
fn create_prospect(
    stores: &ServerStores,
    request: &ServiceRequest,
) -> Result<(gunnar_core::Repo, Arc<OpenRepo>), String> {
    let path = request.repo.path();
    let control = stores.ssh_control();
    // **Made meanwhile, by this same push** (2026-09-28): git takes
    // receive-pack's advertisement (the prospect) BEFORE it runs the pre-push
    // hook, and git-lfs's upload in that hook makes the repository in
    // between. The stale-store check below would answer the push's own
    // commands with an ERR. A repository that now EXISTS is served, as the
    // lost race in `admit_push_create` is, after the same re-asked grant,
    // and only in the hash the client was shown.
    match control.resolve(&path) {
        Ok(Some(existing)) => {
            if existing.hash_kind != request.repo.hash_kind {
                return Err(format!(
                    "{path} was created meanwhile as a {:?} repository, and this push was shown {:?}; nothing was written — push again",
                    existing.hash_kind, request.repo.hash_kind
                ));
            }
            return match control.push_create_spec(&request.principal, &path) {
                Ok(Some(_)) => stores
                    .repo_store(&existing)
                    .map(|store| (existing, store))
                    .map_err(|err| {
                        format!("the repository {path} could not be opened on the server: {err:#}")
                    }),
                Ok(None) => Err(format!(
                    "repository not found, or you do not have access: {path}"
                )),
                Err(err) => Err(format!(
                    "the gunnar control plane could not read {path}: {err}"
                )),
            };
        }
        Ok(None) => {}
        Err(err) => {
            return Err(format!(
                "the gunnar control plane could not read {path}: {err}"
            ))
        }
    }
    if let Err(err) = stores.refuse_stale_store(&path, false, request.repo.hash_kind) {
        return Err(format!("{err:#}"));
    }
    let (repo, conjured) = match control.admit_push_create(&request.principal, &path) {
        Ok(Some(pair)) => pair,
        Ok(None) => {
            return Err(format!(
                "repository not found, or you do not have access: {path}"
            ))
        }
        Err(err) => {
            return Err(format!(
                "the gunnar control plane could not create {path}: {err}"
            ))
        }
    };
    match stores.repo_store(&repo) {
        Ok(store) => Ok((repo, store)),
        Err(err) => {
            stores.close_repo(&path);
            if let Err(undo) = control.retract_push_create(&path, conjured) {
                tracing::error!(
                    target: TARGET_GIT,
                    repo = %path,
                    error = %undo,
                    "retracting the catalog row of a push-create whose store failed"
                );
            }
            if let Ok(dir) = stores.repo_dir(&path) {
                let _ = std::fs::remove_dir_all(dir);
            }
            tracing::error!(
                target: TARGET_GIT,
                repo = %path,
                error = format!("{err:#}"),
                "the push-created repository's store could not be made; nothing was kept"
            );
            Err(format!("the repository {path} could not be created on the server; nothing was kept. The reason is in the server log"))
        }
    }
}

/// **A push into an encrypted repository on a build that cannot check it in quarantine**
/// (wiring tracer B 2026-10-08): the quarantine is the znippy engine's
/// (`encrypted_quarantine`), and without it a refused plaintext pack would be stored and framed
/// for the twin before the ref gate refused it. `Some(the line)`: refuse the push by name,
/// before a byte is read.
pub(crate) fn unchecked_encrypted_push(encrypted: bool, quarantine: bool) -> Option<String> {
    (encrypted && !quarantine).then(|| {
        "this repository is encrypted, and this Gunnar was built without the check that keeps \
         plaintext out of it, so it takes no push into it: push to a Gunnar built with its \
         standard storage engine. Nothing was received"
            .to_string()
    })
}

fn serve_receive_pack(
    serving: &Serving<'_>,
    request: &ServiceRequest,
    store: &OpenRepo,
    preamble: Option<gunnar_wire::Preamble>,
    input: &mut dyn BufRead,
    output: &mut dyn std::io::Write,
) -> Result<(), TransportError> {
    let Serving {
        stores,
        bus,
        attestations,
        metrics,
        wire,
    } = *serving;
    let started = std::time::Instant::now();
    // A build that cannot check an encrypted repository's pack in quarantine takes none.
    if let Some(line) =
        unchecked_encrypted_push(request.repo.encrypted, cfg!(feature = "engine-znippy"))
    {
        return Err(TransportError::Refused(line));
    }
    // **Everything below this line is a validation read.** A receive-pack
    // ingests, indexes and then walks every commit and tree it was sent to
    // prove the push closes — once each — and serves nobody. Those reads are
    // indistinguishable from a client's inside the store, so the caller is the
    // only thing that can say which they are, and this is the caller.
    //
    // Without it the push fills the engine's lazy read-cache for a reader that
    // does not exist: MEASURED 6 778 047 872 B on `torvalds/linux`, 31 % of the
    // finished store (measured 2026-09-03).
    //
    // Thread-scoped and dropped at the end of this function, so a concurrent
    // clone on another connection still warms the cache normally.
    #[cfg(feature = "engine-znippy")]
    let _validation_reads = crate::storage::znippy_engine::validation_reads();
    // **One handle.** `GitServe` carries the reading contract and, through its
    // `GitOps` supertrait, the eleven — so receive-pack advertises, checks
    // connectivity and writes references against a single store rather than
    // against an object handle and a ref handle that could disagree.
    let git = store.serve();
    let hash_kind = store.hash_kind();

    let gate = PolicyGate::open(store.git_dir(), request, stores, Arc::clone(metrics));

    // Opened before the ingest, and that ordering is load-bearing: whether the
    // policy has any gate at all is what decides if the ingest pays for an
    // object-set enumeration. It used to be built after, when nothing needed
    // to know.

    // No `objects/pack` is created. An empty one is what a later reader uses
    // to decide a repository is packfile-shaped, and this server never writes
    // a packfile into it.

    // ── who asks for the object set, and why it is not only the gates ──────
    //
    // `want_objects: !gate.engine.is_empty()` stood here, and it had **one**
    // consumer in mind. The push attestation is a second one, and it was not
    // counted: on a repository with no policy gates the ingest skipped the
    // enumeration and `attestation_for` then wrote `new_object_count: 0` with
    // an empty `new_object_oids` — a push of nothing and a push nobody
    // enumerated rendering as the same record. The log line thirty lines below
    // this already got that distinction right (`objects_unknown`); the
    // attestation, which is the durable artefact and the one a verifier reads,
    // did not. `tests/push_events.rs` saw it as *"the pushed commit is missing
    // from the attestation's object list: []"*.
    //
    // So the attestation asks too, and by default it gets an answer. That is
    // not free — see `znippy_engine::ingest`'s note on why the flag exists at
    // all: the ids are not a by-product of storing a pack, and asking for them
    // waits for the indexer to drain. An operator who would rather have the
    // latency than the object list turns it off with
    // `--no-attest-objects`, and the attestation then records **why** the set
    // is absent instead of implying it was empty.
    let objects = if gate.engine.reads_new_objects() {
        // A gate reads the set. (Not `!is_empty()`: the attribution-trailer
        // gate is live on every repository by default and decides from the
        // references and the object graph, so it must not make every push
        // demand an enumeration — the gix engine refuses `Required` outright.) An engine that cannot produce one must refuse
        // rather than let the gate decide on nothing.
        crate::storage::ObjectSet::Required
    } else if wire.attest_objects {
        // Only the attestation wants it, and the attestation can say it did not
        // get it. An engine that cannot enumerate still serves the push.
        crate::storage::ObjectSet::Wanted
    } else {
        crate::storage::ObjectSet::NotNeeded
    };
    let ingest = PushIngest {
        git_dir: store.git_dir().to_path_buf(),
        hash_kind,
        engine: stores.engine(),
        objects,
        // **A push has nobody watching an object count.** `git push` renders the
        // remote's progress band, and what it wants there is bytes; there is no
        // job record and no console poll on this path. The mirror is the caller
        // that has both, so the mirror is the caller that passes a sink.
        watching: None,
        // The pushed pack is a transaction (the twin): framed before the engine stores it. For a
        // first push `request.repo` is exactly the row `admit_push_create` writes first.
        frame: Some(PackFrame::of(stores, &request.repo)),
        // An encrypted repository's pack is checked before it is kept.
        encrypted: request.repo.encrypted.then_some(git),
        // Held to the data set's room, chunk by chunk (cov-13).
        space: stores.space_ledger(),
    };
    if gate.engine.is_empty() {
        tracing::info!(
            target: TARGET_POLICY,
            event = "policy.gates.none",
            policy = %store.git_dir().join(POLICY_FILE).display(),
            "no policy gates are configured for this repository; every well-formed push is \
             accepted. This is an empty policy, not a passing one."
        );
    } else {
        tracing::info!(
            target: TARGET_POLICY,
            event = "policy.gates.live",
            gates = ?gate.engine.gate_names(),
            "policy gates live for this push"
        );
    }

    // **receive-pack reads from the store it writes into.**
    //
    // This used to be `&objects` unconditionally, under a comment saying
    // receive-pack was "deliberately NOT switched" because a push landed on
    // `fs` and reached the tier through the append hook. That is no longer the
    // route: the pack goes straight into the tier, so checking connectivity
    // against the filesystem would ask an empty repository whether the objects
    // it just stored are present — and the answer is a refused push, which is
    // exactly what it did the first time this ran.
    //
    // A pack the ingest stored but the indexer has not absorbed yet is **not**
    // a stale answer here: the per-pack `indexed` bit makes an index miss fall back to the journal
    // rather than report absent, so the read side sees a push the moment
    // `put_pack` returns, which is before anything asks.
    //
    // `write_objects` and not `cold_objects`: the latter asks whether there is
    // anything to *serve*, and answers `None` for an empty repository — which
    // is every repository's first push.
    // The engine is the read side; there is nothing else to open.

    // No `#[allow(unused_mut)]` any more: it was here because the only
    // assignment below was `cfg(metered)`, so a community build had a `mut`
    // binding it never wrote. The ceiling is assigned on both builds, so the
    // `mut` is now load-bearing on both — and an allow that no longer suppresses
    // anything is a suppression nobody can see expire.
    let mut receive_config = wire.receive.clone();
    // The plan's warning (Grace), decided in `authorise_service` and carried
    // on the request: the wire prints it on the progress band beside the
    // report, so `git push` shows `remote: plan expired …; renew at …`.
    #[cfg(feature = "metered")]
    {
        receive_config.notice = request.plan_notice.clone();
    }
    // **The mid-upload ceiling**, decided by the same gate chain in
    // `authorise_service` and carried on the request beside the notice. Not
    // `cfg(metered)`: the field is on `ServiceRequest` unconditionally and a
    // community build simply carries `None`, so there is one path here rather
    // than a second one that only the paid build compiles (one implementation). The two
    // `PackCeiling`s are the deliberate twin `gunnar_ssh::PackCeiling`
    // documents — `gunnar-ssh` does not depend on `gunnar-wire` — and this is
    // the one seam that holds both and converts.
    //
    // The third field is the one this seam could not have been given by either
    // side alone: `gunnar-ssh` measured the namespace and marked its ledger,
    // `gunnar-wire` counts the bytes, and neither may name the other's type.
    // See `PackMark` below.
    receive_config.pack_ceiling =
        request
            .pack_ceiling
            .as_ref()
            .map(|ceiling| gunnar_wire::PackCeiling {
                bytes: ceiling.bytes,
                refusal: ceiling.refusal.clone(),
                ledger: request
                    .pack_mark
                    .clone()
                    .map(|mark| Arc::new(PackMark { mark }) as Arc<dyn gunnar_wire::PackLedger>),
            });
    let receive = ReceivePack::new(git, hash_kind).with_config(receive_config);
    // `GET /info/refs?service=git-receive-pack`: the ref list and its
    // capabilities, then stop. No command list, no pack, no gate — the push
    // itself arrives as a separate POST.
    if request.delivery == Delivery::Advertisement && preamble.is_none() {
        receive
            .write_ref_advertisement(output)
            .map_err(wire_error)?;
        output.flush()?;
        tracing::info!(
            target: TARGET_GIT,
            event = "git.receive_pack.advertised",
            elapsed_ms = started.elapsed().as_millis() as u64,
            "served the receive-pack advertisement"
        );
        return Ok(());
    }
    // A preamble already read (the first-repository door) skips the
    // advertisement and the command read: that half happened before the
    // repository existed.
    let outcome = match preamble {
        Some(preamble) => receive
            .serve_after_preamble(preamble, input, output, &ingest, &gate)
            .map(Some),
        None => receive.serve_session(wire_session(request), input, output, &ingest, &gate),
    }
    .map_err(wire_error)?;
    output.flush()?;

    let Some(outcome) = outcome else {
        tracing::info!(
            target: TARGET_GIT,
            event = "git.receive_pack.advertisement_only",
            elapsed_ms = started.elapsed().as_millis() as u64,
            "git-receive-pack: the client took the advertisement and had nothing to push"
        );
        return Ok(());
    };

    // The key this session authenticated with. Carried on the request since
    // The authenticating key itself, not recovered from the identity table.
    let fingerprint = session_key_fingerprint(request);
    // The answerable one. See `session_credential` and GUN-014.
    let credential = session_credential(request);
    let gates = gate.gate_that_refused();
    // **THE counting point for a policy refusal**, per namespace and per push.
    //
    // Once per PUSH and not once per refused reference, deliberately: a push
    // that names four protected branches met one door four times, and a console
    // that reported four refusals would make one mistake look like a campaign.
    // The refs themselves are already in the `git.ref.rejected` lines below and
    // in the `PushRejected` events, which is where per-reference detail belongs.
    if !gates.is_empty() {
        serving.stores.flow().refuse(
            &request.repo.namespace,
            gunnar_core::now_ms(),
            gunnar_core::Refusal::Policy,
        );
    }

    let mut refs_ok = 0usize;
    let mut refs_rejected = 0usize;
    for (command, status) in outcome.commands.iter().zip(&outcome.statuses) {
        match status {
            RefStatus::Ok => {
                refs_ok += 1;
                if command.is_delete() {
                    metrics.add_refs_deleted(1);
                } else {
                    metrics.add_refs_updated(1);
                }
                tracing::info!(
                    target: TARGET_GIT,
                    event = if command.is_delete() { "git.ref.deleted" } else { "git.ref.updated" },
                    ref_name = %command.name,
                    old = %command.old,
                    new = %command.new,
                    "ref updated"
                )
            }
            RefStatus::Rejected(reason) => {
                refs_rejected += 1;
                metrics.add_refs_rejected(1);
                tracing::warn!(
                    target: TARGET_GIT,
                    event = "git.ref.rejected",
                    ref_name = %command.name,
                    old = %command.old,
                    new = %command.new,
                    // The gate that said no, when one did. Empty means nothing
                    // named a gate: a connectivity failure, a lost
                    // compare-and-swap, or the collateral refusal every other
                    // reference gets in an `--atomic` push. The event stream publishes the
                    // same distinction; the log must not blur it.
                    gate = gates.get(&command.name.to_string()).map(String::as_str).unwrap_or(""),
                    %reason,
                    "ref rejected"
                )
            }
        }
        if let Some(bus) = bus {
            // Only a reference that actually moved has a descent question to
            // answer, so the walk is skipped for everything else.
            let forced = status.is_ok() && was_forced(git, command);
            publish(
                bus,
                request,
                command,
                status,
                outcome.atomic,
                &fingerprint,
                &credential,
                &gates,
                forced,
            );
        }
    }

    // A push that moved a reference is the pusher's operation (2026-10-21,
    // the admin's user list). In memory, at most once a minute per person.
    if refs_ok > 0 {
        stores
            .activity()
            .operated(&request.principal.id, gunnar_core::now_ms());
    }

    // ── a repository nobody could clone, made clonable ─────────────────────
    //
    // After the references have moved, and only for a repository whose `HEAD`
    // still resolves to nothing. See
    // [`ServerStores::adopt_default_branch_if_unborn`] for why a push is
    // allowed to decide this and for how narrow the condition is. A failure
    // here is logged and not fatal: the push itself succeeded and its
    // references are durable, and refusing it afterwards would be a lie about
    // what happened.
    let created: Vec<String> = outcome
        .commands
        .iter()
        .zip(&outcome.statuses)
        .filter(|(command, status)| status.is_ok() && !command.is_delete())
        .map(|(command, _)| command.name.to_string())
        .collect();
    if !created.is_empty() {
        if let Err(err) = stores.adopt_default_branch_if_unborn(&request.repo.path(), &created) {
            tracing::warn!(
                target: TARGET_GIT,
                event = "git.default_branch.adoption_failed",
                repo = %request.repo.key(),
                error = format!("{err:#}"),
                "the push landed, but this repository's HEAD names a branch that does not \
                 exist and adopting a pushed one failed; it cannot be cloned until \
                 `set-default-branch` is run against it"
            );
        }
    }

    // ── the one line per push an operator reads ────────────────────────────
    //
    // Applied outputs: how many references actually moved, how many were
    // refused, what the pack cost and how long the whole thing took. A push
    // summary that said only "done" would be indistinguishable from one that
    // moved nothing (measured, not asserted).
    //
    // `objects` is what the push INTRODUCED, and an ingest that could not work
    // that out logs the reason rather than a plausible zero, because a push of
    // nothing and a push whose contents could not be enumerated are different
    // facts.
    let objects_pushed: &[ObjectId] = outcome.new_objects.as_deref().unwrap_or(&[]);
    let objects_unknown = outcome.new_objects.as_ref().err().map(String::as_str);
    // Two different numbers, both worth having: `wire_bytes` is what the client
    // sent and is what a `max_pack_bytes` limit is enforced against;
    // `pack_bytes` is what landed, which is larger for a thin push because
    // completing it writes bases the client did not send.
    //
    // Neither may fall back to `0`. `PushFacts::pack_bytes` is `Option` on
    // purpose — "`Some(0)` for a deletions-only push … `None` only if the count
    // is genuinely unavailable" — and `unwrap_or(0)` threw away exactly the
    // distinction the type exists to carry. `stat` failing on a pack whose
    // bytes are then reported as `0` is the same disease: an absence that
    // renders as a value (and the `objects_unknown` field above).
    let (wire_bytes, wire_bytes_unknown) = measured(
        outcome.pack_bytes,
        "the ingest did not report how many packfile bytes were read off the wire",
    );
    let (pack_bytes, pack_bytes_unknown) = measured_pack_bytes(&outcome);
    if refs_rejected == 0 && outcome.unpack.is_ok() {
        metrics.add_pushes_accepted(1);
    } else {
        metrics.add_pushes_rejected(1);
    }
    tracing::info!(
        target: TARGET_GIT,
        event = "git.receive_pack.served",
        refs_ok,
        refs_rejected,
        atomic = outcome.atomic,
        unpack_ok = outcome.unpack.is_ok(),
        objects = objects_pushed.len(),
        objects_unknown,
        wire_bytes,
        wire_bytes_unknown,
        pack_bytes,
        pack_bytes_unknown,
        push_options = outcome.push_options.len(),
        elapsed_ms = started.elapsed().as_millis() as u64,
        "served git-receive-pack"
    );

    // ── the znippy engine took the objects at ingest ───────────────────────────
    //
    // There is no second step here any more, and its absence is the change.
    //
    // What used to sit at this line read the `.pack` and `.idx` that
    // `index_thin_pack` had written and appended them to an archive — so the
    // tier was a COPY of a filesystem write that had already happened, and the
    // packfile it copied from stayed in `objects/pack` for ever. That is what
    // made every push pay for git's on-disk format even on a server whose
    // storage is Arrow. `PushIngest::ingest_cold` now stores the bytes as they
    // arrive and no packfile is written at all.
    //
    // The `refs_ok > 0` guard that used to wrap this is gone with it, and its
    // reasoning does not transfer: it existed because archiving a
    // fully-rejected push would make the tier the only place that push
    // survived. Storage is no longer a place a push can survive *separately* —
    // it is where the push went — so a rejected ref list now leaves exactly
    // what a rejected ref list leaves on any git server: objects nothing names,
    // for `gunnar gc` to collect.

    // ── push attestation ───────────────────────────────────────────────────
    //
    // Built from what the push actually did, not from what it asked for: a
    // refused reference is not in `ref_updates`, so an attestation can never
    // claim a reference moved that did not.
    //
    // The `Result` goes in whole. Handing `objects_pushed` — which is the `Err`
    // arm already flattened to `&[]` for the log line above — is what made the
    // record claim an empty object list for every push into a repository with
    // no policy gates.
    let attested_objects = outcome.new_objects.as_deref().map_err(String::as_str);
    if let Some(attestation) = attestation_for(
        request,
        &outcome,
        attested_objects,
        &fingerprint,
        &credential,
    ) {
        tracing::info!(
            target: TARGET_GIT,
            event = "git.attestation",
            refs = attestation.ref_updates.len(),
            objects = attestation.new_object_count,
            // Absent from the line when the set WAS enumerated (`Option`
            // records nothing for `None`), so the field's presence is the fact.
            oids_unknown = (!attestation.new_object_oids_unknown.is_empty())
                .then_some(attestation.new_object_oids_unknown.as_str()),
            pack = %attestation.pack_hash,
            key_fingerprint = %attestation.key_fingerprint,
            // The field an HTTP push can be attributed by. `key_fingerprint`
            // above is empty on that transport (GUN-014).
            credential = %attestation.credential,
            "push attestation"
        );
        if let Err(err) = attestations.append(&attestation) {
            // the attestation's durable record failed, and the push has already been
            // reported to the client as done. Saying so loudly is the only
            // correct action; failing here would make gunnar and the client
            // disagree about what is in the repository.
            tracing::error!(
                target: TARGET_GIT,
                path = %attestations.path().display(),
                %err,
                "the push attestation could not be appended to the log; the push itself stands"
            );
        }
        if let Some(bus) = bus {
            bus.publish(event::Payload::PushAttestation(attestation));
        }
    }
    Ok(())
}

// ───────────────────────────────────────────────────────────────────────────
// Measurements, and the absence of one
// ───────────────────────────────────────────────────────────────────────────

/// A measurement split into **the number** and **why there isn't one**.
///
/// `tracing`'s `Value` impl for `Option<T>` records nothing at all for `None`,
/// so the numeric field is *absent* from the line when the measurement is,
/// rather than present and zero. That is the whole point. A reader who sees
/// `pack_bytes=0` has to be able to conclude the push carried no bytes, and
/// they cannot if `0` is also what a failed `stat` prints.
///
/// The same shape as `objects` / `objects_unknown` on the line above it:
/// one field for the fact, one for the reason it is missing, never
/// a plausible default standing in for either.
fn measured(value: Option<u64>, why: &str) -> (Option<u64>, Option<String>) {
    match value {
        Some(bytes) => (Some(bytes), None),
        None => (None, Some(why.to_owned())),
    }
}

/// How many bytes the pack that landed occupies, or why that is unknown.
///
/// Three different absences used to fold into one `0`:
///
/// * the push carried no pack at all, because it was deletions only;
/// * the pack was indexed without a directory, so nothing was written;
/// * `stat` failed on a path that *was* written.
///
/// Only the first is a zero, and it is a **measured** one. The other two are
/// unknowns and now say so, with the path in the reason: an operator chasing a
/// pack that is not where the server thinks it is needs the path more than they
/// need a number. This is a server log, not a client-facing status line, so the
/// path belongs here (the reverse mistake — a server path on the wire to
/// `git push` — was fixed separately).
fn measured_pack_bytes(outcome: &PushOutcome) -> (Option<u64>, Option<String>) {
    let Some(pack) = outcome.pack.as_ref() else {
        // Deletions only: nothing was written because nothing was sent.
        return (Some(0), None);
    };
    let Some(path) = pack.data_path.as_deref() else {
        // No file — which is the ordinary case, not a degraded one: the znippy
        // engine appends the pack verbatim into its archive and writes no
        // `.pack`. The store counted what it appended, so the measurement is
        // right here and only an engine that reported neither is an unknown.
        return match pack.stored_bytes {
            Some(bytes) => (Some(bytes), None),
            None => (
                None,
                Some(format!(
                    "pack {} was indexed without a directory and the store reported no size, so \
                     there is nothing to measure",
                    pack.pack_id
                )),
            ),
        };
    };
    match std::fs::metadata(path) {
        Ok(meta) => (Some(meta.len()), None),
        Err(err) => (
            None,
            Some(format!("{} could not be stat'ed: {err}", path.display())),
        ),
    }
}

/// How many objects a push introduced, for the attestation record.
///
/// **The count never depends on the list.** `Ok(ids)` is an enumerated set and
/// its length is the count, empty included — a push that introduced nothing is
/// a measured zero. `Err(_)` is "the set was never worked out", and the count
/// then comes off the stored pack's own 12-byte header, which is known either
/// way. Zero only for a push that carried no pack at all.
fn attested_object_count(
    new_objects: std::result::Result<&[ObjectId], &str>,
    outcome: &PushOutcome,
) -> u64 {
    match new_objects {
        Ok(ids) => ids.len() as u64,
        Err(_) => outcome
            .pack
            .as_ref()
            .map(|pack| u64::from(pack.num_objects))
            .unwrap_or(0),
    }
}

/// The push attestation record for one push, or `None` when nothing moved.
///
/// A push in which every reference was refused produces `PushRejected` events
/// and no attestation: there is no provenance to attest to, and a record with
/// an empty `ref_updates` would be indistinguishable from a bug in this
/// function.
fn attestation_for(
    request: &ServiceRequest,
    outcome: &PushOutcome,
    new_objects: std::result::Result<&[ObjectId], &str>,
    fingerprint: &str,
    credential: &str,
) -> Option<PushAttestation> {
    let ref_updates: Vec<gunnar_proto::RefUpdate> = outcome
        .commands
        .iter()
        .zip(&outcome.statuses)
        .filter(|(_, status)| status.is_ok())
        .map(|(command, _)| gunnar_proto::RefUpdate {
            ref_name: command.name.to_string(),
            old_oid: command.old.to_string(),
            new_oid: command.new.to_string(),
        })
        .collect();
    if ref_updates.is_empty() {
        return None;
    }

    // The pack as gunnar stored it. `pack_hash` is that pack's own trailing
    // checksum — the `<id>` in `pack-<id>.pack` — in the repository's hash
    // kind, so it is verifiable against the file on disk. It is deliberately
    // NOT a hash of the bytes the client sent: `index_thin_pack` completes a
    // thin pack from the store before writing it, so the received bytes and
    // the stored pack are different objects and only one of them still exists.
    //
    // **Through `measured_pack_bytes`, not through a `stat` of its own.** This
    // used to `std::fs::metadata(data_path).ok()…unwrap_or(0)`, which on an
    // engine that writes no packfile is `None.unwrap_or(0)` — so every
    // attestation this server produced said the push stored zero bytes. Same
    // disease as the empty object list below, in the same record, one field
    // along. The znippy store writes no pack FILE (`data_path: None`) but it
    // counts the bytes it appends, and that count is exactly what
    // `measured_pack_bytes` was taught to prefer over an absent file — one
    // function, so the log line and the durable record can never disagree
    // about how big a push was.
    //
    // The last resort is the wire count (`PushOutcome::pack_bytes`), for an
    // engine that reports neither a file nor a size: a byte count that could
    // not be established at all stays `0` here because the proto has no third
    // state for it, and `new_object_oids_unknown` is the only field that can
    // say the record is partial. What is gone is the case where the count was
    // *available* and thrown away for want of a file.
    let (pack_hash, pack_bytes) = match &outcome.pack {
        Some(pack) => {
            let bytes = measured_pack_bytes(outcome)
                .0
                .or(outcome.pack_bytes)
                .unwrap_or(0);
            (pack.pack_id.to_string(), bytes)
        }
        // A deletions-only push carries no pack at all, and that is a fact
        // worth attesting rather than a reason to skip the record.
        None => (String::new(), 0),
    };
    // ── the object set, and the reason there isn't one ──────────────────────
    //
    // The set is not free: on the znippy engine `oids_in_extent` WAITS for the
    // indexer to absorb the pack, and that wait is what the deferred indexer
    // exists to avoid paying on the push path (`znippy_engine::ingest`'s "why
    // this is a flag" note). So `PushIngest` says how badly it wants the ids
    // rather than always asking — `ObjectSet::Required` when a policy gate
    // reads them (`gate.engine` non-empty), `ObjectSet::Wanted` when only this
    // record does, and `ObjectSet::NotNeeded` under `--no-attest-objects`.
    // Where the set is not produced — an operator who bought the latency back,
    // or an engine that cannot enumerate for a `Wanted` — the ingest says so
    // with `Err(reason)` rather than an empty set.
    //
    // That `Err` used to be flattened to `&[]` by the caller. The record then
    // carried an empty list, and an empty list is what a push of zero objects
    // carries too, so the two were indistinguishable — and the fix before this
    // one made it worse in the honest direction only halfway: the COUNT became
    // the pack's, so a reader saw "3 objects" beside a list of none and no word
    // about why. The reason travels now, in its own field, and the record says
    // what it knows and no more (measured, and never a plausible value where an absence belongs).
    let new_object_count = attested_object_count(new_objects, outcome);
    let (new_object_oids, new_object_oids_unknown) = match new_objects {
        Ok(ids) => (ids.iter().map(ObjectId::to_string).collect(), String::new()),
        Err(reason) => (Vec::new(), reason.to_owned()),
    };

    let received_at_unix_ms = gunnar_grpc::bus::now_unix_ms();
    Some(PushAttestation {
        // Unique per push: the stored pack's identity is unique to the bytes,
        // and the millisecond disambiguates the same pack pushed twice (and a
        // deletions-only push, which has no pack at all).
        attestation_id: format!(
            "{}@{received_at_unix_ms}/{}",
            request.repo.key(),
            if pack_hash.is_empty() {
                "no-pack"
            } else {
                &pack_hash
            }
        ),
        repo: Some(RepoPath::new(&request.repo.namespace, &request.repo.name)),
        principal_id: request.principal.id.clone(),
        principal_name: request.principal.name.clone(),
        key_fingerprint: fingerprint.to_owned(),
        // Answerable on both transports, unlike the fingerprint above
        // (GUN-014). A verifier reading an older record sees an empty string,
        // which is what it saw before this field existed.
        credential: credential.to_owned(),
        hash_kind: crate::control_plane::hash_kind(request.repo.hash_kind) as i32,
        ref_updates,
        // The list is never a partial one: it is either the set the store
        // enumerated or empty beside a reason that says why nobody asked.
        // `new_object_oids_unknown` is what a reader honours; a reader that
        // ignores it sees a count that still came from the pack's own header
        // rather than from a list that was never built.
        new_object_count,
        new_object_oids,
        new_object_oids_unknown,
        new_object_bytes: pack_bytes,
        pack_hash,
        received_at_unix_ms,
        server_agent: gunnar_proto::AGENT.to_owned(),
        // The no-OS rule: git traffic has exactly one transport, and this module is the
        // SSH one. Not read from the request, because there is nothing else it
        // could be, and a field that can only ever hold one value should say
        // which one rather than sit empty.
        transport: "ssh".to_owned(),
    })
}

/// The SHA-256 fingerprint of the key this push authenticated with, when that
/// can be known without guessing.
///
/// The key that authenticated this session, for the attestation and the events.
///
/// # This used to be a guess, and the guess was usually wrong
///
/// The cross-crate gap is closed: `gunnar-ssh` computes the fingerprint in
/// `auth_publickey`, looks the principal up by it, and now **carries it** on
/// [`ServiceRequest`] instead of dropping it.
///
/// What this replaced read the identity table back and answered only when the
/// principal held **exactly one key** — so a machine account with a rotation
/// key, or any human with a laptop key and a desktop key, published an empty
/// fingerprint on every `RefUpdated`, every `RefDeleted` and every attestation.
/// "Which key pushed this" was therefore unanswerable for precisely the
/// accounts an audit trail exists for. Reading it back could never have been
/// made correct: the table cannot say which of two keys made a connection.
///
/// # An empty string here does NOT mean "nobody" (GUN-014)
///
/// It used to say so: *"an empty string means no key, and now means it for one
/// reason only — the anonymous, read-only path, which authenticates with no key
/// at all. That path cannot push, so it cannot reach an attestation."* That was
/// false for smart HTTP. HTTP authenticates with a **token**, so there is no
/// key and `ServiceRequest::key_fingerprint` is `None` on every HTTP request
/// including `git-receive-pack` — and the empty string went into `RefUpdated`,
/// `RefDeleted` and the attestation with nothing else on the record naming who
/// pushed. Two HTTP pushes by two different people were the same audit line.
///
/// The field is still exactly what it says: the SSH key, when there was one.
/// The question "which credential" is [`session_credential`]'s.
fn session_key_fingerprint(request: &ServiceRequest) -> String {
    match &request.key_fingerprint {
        Some(fingerprint) => fingerprint.as_str().to_owned(),
        None => {
            tracing::debug!(
                target: TARGET_GIT,
                principal = %request.principal.id,
                "no key fingerprint on this request: the session authenticated anonymously"
            );
            String::new()
        }
    }
}

/// **Which credential made this request**, as the audit trail names it:
/// `key:SHA256:…`, `token:<hash prefix>`, or empty for a caller that presented
/// none.
///
/// The field [`session_key_fingerprint`] could not be (GUN-014). It is derived
/// here rather than spelled by each front door so the two transports cannot
/// come to write the prefix differently: SSH already carries the fingerprint
/// and gets `key:` added to it; HTTP has no key and carries
/// [`ServiceRequest::credential`] instead.
///
/// Empty now genuinely has one meaning — no credential — and the anonymous read
/// path is the only thing that produces it, which is what the old note claimed
/// and this makes true.
fn session_credential(request: &ServiceRequest) -> String {
    match (&request.key_fingerprint, &request.credential) {
        (Some(fingerprint), _) => format!("key:{}", fingerprint.as_str()),
        (None, Some(credential)) => credential.clone(),
        (None, None) => String::new(),
    }
}

// `pack_object_ids` stood here: it opened a written `.idx` with
// `gix_pack::index::File::at` and collected every oid in it. It went with
// `index_thin_pack`, which is the only thing that ever wrote an `.idx` for it
// to read — a push is a verbatim `put_pack` into the engine now, and the ids a
// push introduced come from `PushReceipt::new_oids` off the engine's own index.
//
// It carried the last two live bumps of `gunnar_pack::oidprobe`'s
// `GIX_PACK_INDEX_OPEN` / `GIX_PACK_INDEX_ITER`, which `bench/examples/
// oidlookup-probe.rs` reports. Deleting it changes **no measurement**: the
// function had no caller, so those counters were already zero in every run that
// could be taken. A counter that reads 0 because the path is gone is the truth;
// keeping an uncalled function alive to hold a probe open would be the other
// way round — an instrument attached to code that cannot execute.

/// Taking a pushed pack into `objects/pack`, and saying which objects it
/// **introduced**.
///
/// # The subtraction, and why it is the only honest source
///
/// A gate cannot enforce a quota against a set that includes objects the
/// repository already held. This used to hand the gate every id in the written
/// `.idx`, which for a thin push is the client's objects *plus* the bases
/// `index_thin_pack` fetched from the store to complete it — so a
/// `max_objects` gate refused pushes it need not have, and no amount of
/// re-reading the index afterwards could tell the two apart, because by then
/// they are the same file.
///
/// The distinction exists in exactly one place: the `gix_object::Find` that
/// resolution asks for a missing base. Every id it is asked for and finds is,
/// by definition, an object the repository already had. So the lookup is
/// wrapped, the ids it served are recorded, and
///
/// ```text
///     introduced  =  everything in the .idx  −  the bases we handed back
/// ```
///
/// is exact rather than conservative, and needs no second enumeration of
/// anything (one implementation).
/// **Where a pushed or fetched pack is framed**: the box's transaction log, and the catalog
/// key and hash kind the twin's apply opens the repository by (`gunnar_core::txlog`).
#[derive(Clone)]
pub(crate) struct PackFrame {
    log: Arc<gunnar_core::txlog::Log>,
    repo: String,
    hash: String,
}

impl PackFrame {
    /// The frame for `repo` on `stores`.
    pub(crate) fn of(stores: &crate::stores::ServerStores, repo: &gunnar_core::Repo) -> PackFrame {
        PackFrame {
            log: Arc::clone(stores.txlog()),
            repo: repo.key(),
            hash: repo.hash_kind.to_string(),
        }
    }

    /// These exact bytes, as a `Pack` frame, durable before this returns.
    fn append(&self, bytes: &[u8]) -> anyhow::Result<()> {
        let meta = gunnar_core::txlog::Meta::Pack {
            repo: self.repo.clone(),
            hash: self.hash.clone(),
        };
        self.log
            .append(gunnar_core::txlog::Kind::Pack, &meta, bytes)?;
        Ok(())
    }
}

pub(crate) struct PushIngest<'a> {
    git_dir: PathBuf,
    hash_kind: gix_hash::Kind,
    /// Which engine stores this push. Carried on the value rather than read
    /// from a global, because the process's engine is a field on
    /// [`ServerStores`](crate::stores::ServerStores) now and a push is served
    /// out of one of those.
    engine: crate::storage::Engine,
    /// How badly this push needs the object set it introduced: a policy gate
    /// that reads the objects cannot decide without it, the push attestation
    /// would rather have it than apologise for it, and an operator can decline
    /// it altogether. Asking for it waits on index work the ack path defers on
    /// purpose, which is why this is three answers and not a `bool`.
    objects: crate::storage::ObjectSet,
    /// **Where the running object count goes, or `None`.**
    ///
    /// The numerator of a transfer's progress, and the reason it is on the
    /// *ingest* rather than on `gunnar-wire`'s `FetchProgress` beside the
    /// denominator: the count exists only inside the engine's pack walk, and
    /// there is exactly one of those (one implementation). `fetch_pack` reads the header and
    /// can publish `objects_total` because twelve bytes are all that costs;
    /// counting the objects themselves would be a second decoder, which its own
    /// module doc refuses. So the two halves of one progress pair are published
    /// from two places, and this is the half storage owns.
    watching: Option<&'a dyn git_storage_trait::PackProgress>,
    /// **The transaction log this pack is a frame in**, or `None` (a test, a tool). Handed
    /// to `Engine::ingest_push` as `before_store`: the pack's exact bytes are a `Pack` frame
    /// before the engine stores them — this path never passes the `OpenRepo::logged` tee.
    frame: Option<PackFrame>,
    /// **An encrypted repository's objects, or `None`** (crypt-level2 item 4,
    /// 2026-10-06): when set, the pushed pack is checked in quarantine
    /// ([`crate::encrypted_quarantine`]) BEFORE it is framed or stored — these
    /// are the bases a thin pack deltas against. A refusal keeps nothing.
    encrypted: Option<&'a dyn git_storage_trait::GitServe>,
    /// **The data set's space ledger**, or `None` (a test, a tool, a server
    /// whose doors were never wired): every chunk of the pack is admitted
    /// against it BEFORE it is read, and a pack that does not fit beside what
    /// is already arriving is refused by name — never failed by a full disk
    /// mid-receive (cov-13, [`crate::push_space`]).
    space: Option<Arc<gunnar_ssh::SpaceLedger>>,
}

impl<'a> PushIngest<'a> {
    /// **The ingest a MIRROR lands its fetched pack through** — this one, the
    /// push's own, and not a second sink.
    ///
    /// `fetch_pack`'s module doc states the principle it is built on: *a
    /// fetched pack and a pushed pack are the same bytes*, so both land through
    /// the identical [`gunnar_wire::PackIngest`] and znippy implements it once.
    /// A private constructor rather than a public struct because the fields are
    /// this module's business; what a mirror needs to say is the four facts
    /// below.
    pub(crate) fn new(
        git_dir: PathBuf,
        hash_kind: gix_hash::Kind,
        engine: crate::storage::Engine,
        objects: crate::storage::ObjectSet,
        watching: Option<&'a dyn git_storage_trait::PackProgress>,
        frame: Option<PackFrame>,
    ) -> Self {
        PushIngest {
            git_dir,
            hash_kind,
            engine,
            objects,
            watching,
            frame,
            // A mirror's fetched pack is not checked here: the repository it
            // lands in is the mirror's own (an encrypted repository is not a
            // mirror target in Level 1).
            encrypted: None,
            space: None,
        }
    }

    /// Hold the pack to the data set's room — `stores.space_ledger()`.
    pub(crate) fn with_space(mut self, space: Option<Arc<gunnar_ssh::SpaceLedger>>) -> Self {
        self.space = space;
        self
    }

    /// **The push, stored without a packfile.**
    ///
    /// The bytes go from the wire into the znippy engine's own storage and nothing
    /// is written to `objects/pack` — no `.pack`, no `.idx`, no `.keep`. That is
    /// the whole point of it, and it is why this does not call
    /// [`index_thin_pack`]: `gix_pack::Bundle::write_to_directory` takes a
    /// directory, so every route through it puts a git packfile on disk before
    /// the tier ever sees the bytes, and the tier's copy is then a second one.
    ///
    /// # What is given up, said out loud
    ///
    /// `index_thin_pack` **resolved** the pack: it completed thin deltas against
    /// the store and produced a real `.idx`. This does not. The closure check
    /// inside `put_pack` refuses a pack whose bases are missing, which is the
    /// correctness half, but the pack stays thin and stays verbatim — the whole
    /// claim of *verbatim is the truth*. A reader resolves chains on the way
    /// out instead.
    ///
    /// # The `.keep` that is not there
    ///
    /// `index_thin_pack` wrote one and the caller dropped it once the refs
    /// landed, guarding the new pack from a concurrent `gc` in the window
    /// between. There is no window here: `put_pack` returns only after the
    /// journal row is fsynced, and `gc` reads that journal. The marker's job is
    /// done by the ordering rather than by a file.
    fn ingest_engine(&self, pack: &mut dyn BufRead) -> gunnar_wire::Result<PackReceipt> {
        // ── space admission (cov-13) ─────────────────────────────────────
        //
        // The promise this pack holds on /data: spool + store, + the log's
        // frame when it is framed. Given back copy by copy as each lands (the
        // spool at the reader's `consume`, the frame in the hook below, the
        // store when this function returns and the hold drops).
        let hold = self.space.as_ref().map(|ledger| {
            ledger.hold(if self.frame.is_some() {
                gunnar_ssh::PUSH_COPIES
            } else {
                gunnar_ssh::PUSH_COPIES - 1
            })
        });
        let mut gated = None;
        let reader: &mut dyn BufRead = match &hold {
            Some(hold) => gated.insert(crate::push_space::Gated::new(
                pack,
                hold,
                crate::storage::SPOOL_ABOVE as u64,
            )),
            None => pack,
        };
        let hook = |bytes: &[u8]| -> anyhow::Result<()> {
            // ★ Before the frame and before the store: an encrypted
            // repository's pack is checked in quarantine, and a refusal keeps
            // nothing — not on this disk, not in the log the twin replays.
            #[cfg(feature = "engine-znippy")]
            if let Some(repo) = self.encrypted {
                let checked = crate::encrypted_quarantine::check(bytes, self.hash_kind, repo)?;
                tracing::info!(
                    target: TARGET_STORE,
                    event = "store.encrypted.quarantine_passed",
                    objects = checked,
                    "an encrypted repository's pushed pack passed its check in quarantine; it is framed and stored now"
                );
            }
            match &self.frame {
                Some(frame) => {
                    frame.append(bytes)?;
                    // On the disk now: the frame's promise is `statvfs`'s.
                    if let Some(hold) = &hold {
                        hold.realize(bytes.len() as u64);
                    }
                    Ok(())
                }
                None => Ok(()),
            }
        };
        let hooked = self.frame.is_some() || self.encrypted.is_some();
        // The mutation gate spans frame through store (`Log::mutation`): a snapshot's freeze
        // waits for this pack to be in the engine before it reads the head.
        let _framed = self.frame.as_ref().map(|f| f.log.mutation());
        let stored = self.engine.ingest_push(
            self.git_dir.as_path(),
            self.hash_kind,
            reader,
            self.objects,
            self.watching,
            hooked.then_some(&hook as &dyn Fn(&[u8]) -> anyhow::Result<()>),
        );
        // ★ Cut for room: the ledger's line, verbatim, whatever the ingest made
        // of the cut stream. Its spool is already gone (the failed read dropped
        // it), nothing was framed, nothing stored, and its promise is given back.
        let consumed_by_cut = gated
            .as_ref()
            .map(|g| (g.cut().map(str::to_owned), g.consumed()));
        drop(gated);
        if let Some((Some(line), sent)) = consumed_by_cut.clone() {
            tracing::warn!(
                target: TARGET_STORE,
                event = "store.push.no_room",
                pack_bytes = sent,
                refusal = %line,
                "a pushed pack was refused for room mid-receive: it did not fit beside the \
                 pushes already arriving; its spool is deleted and nothing of it was kept"
            );
            return Err(gunnar_wire::Error::Refused(line));
        }
        let received = stored
            .map_err(|err| {
                // ★ The disk filled ANYWAY (a writer outside the ledger): said in
                // the ledger's words, never `uring: op failed: Operation canceled`.
                if let Some(hold) = &hold {
                    let freed = consumed_by_cut.as_ref().map_or(0, |(_, sent)| *sent);
                    if crate::push_space::is_space_failure(&err, || hold.at_the_floor(freed)) {
                        let line = hold.full_now();
                        tracing::warn!(
                            target: TARGET_STORE,
                            event = "store.push.disk_full",
                            error = %format!("{err:#}"),
                            refusal = %line,
                            "the store failed a pushed pack because the data set filled; told by name"
                        );
                        return gunnar_wire::Error::Refused(line);
                    }
                }
                #[cfg(feature = "engine-znippy")]
                if let Some(refused) = crate::encrypted_quarantine::refusal_in(&err) {
                    tracing::info!(
                        target: TARGET_STORE,
                        event = "store.encrypted.quarantine_refused",
                        reason = %refused,
                        "an encrypted repository's pushed pack was refused in quarantine: nothing was framed or stored"
                    );
                    return gunnar_wire::Error::Refused(refused);
                }
                // `{err:#}`, not `{err}`. `anyhow`'s plain `Display` prints the
                // OUTERMOST context and nothing under it, so a failure three
                // `.context()` calls deep arrived as "storing the pushed pack:
                // enumerating the objects this push introduced" — the two
                // labels this code added, and never the cause the store gave.
                // The alternate form joins the whole chain, which is the only
                // version of this line an operator can act on.
                gunnar_wire::Error::Store(gunnar_store::Error::backend(format!(
                    "storing the pushed pack: {err:#}"
                )))
            })?;

        let pack_id = gix_hash::ObjectId::try_from(received.pack_id.as_slice()).map_err(|_| {
            gunnar_wire::Error::Store(gunnar_store::Error::backend(format!(
                "the stored pack's trailer is {} bytes, which is not a {} object id",
                received.pack_id.len(),
                self.hash_kind
            )))
        })?;

        tracing::info!(
            target: TARGET_STORE,
            // Renamed from `store.cold_tier.ingested` 2026-08-14. The cold tier
            // was purged and this event outlived it, so every push in the log
            // still announced a tier that does not exist. Nothing asserted on
            // the old name — only a doc-comment example, corrected with it.
            event = "store.znippy.ingested",
            objects = received.num_objects,
            bytes = received.pack_bytes,
            elapsed_ms = received.elapsed_ms,
            object_set = received.new_oids.is_some(),
            "the push was stored in the znippy-git store; no packfile was written"
        );

        let new_objects = match received.new_oids {
            Some(oids) => oids
                .iter()
                .map(|raw| {
                    gix_hash::ObjectId::try_from(raw.as_slice())
                        .map_err(|_| format!("the store returned a {}-byte object id", raw.len()))
                })
                .collect::<std::result::Result<Vec<_>, _>>(),
            // Not "no objects" — see `ColdReceipt::new_oids`. A gate reading
            // this refuses, which is the correct answer to "I was never asked
            // to find out".
            None => Err(
                "the policy has no gate over objects, so the object set was not \
                         enumerated"
                    .to_owned(),
            ),
        };

        Ok(PackReceipt {
            indexed: gunnar_wire::pack::IndexedPack {
                num_objects: received.num_objects,
                pack_id,
                // There is no index file, and `ObjectId::null` is this tree's
                // existing spelling for that — the same value the git-store
                // bench server and the store's own end-to-end test use.
                index_id: gix_hash::ObjectId::null(self.hash_kind),
                pack_version: gunnar_wire::pack::PackVersion::from_u32(received.version),
                hash_kind: self.hash_kind,
                // The three paths that no longer exist. `None` is not a
                // degraded answer here: it is the fact, and `cold_tier_append`
                // used to read exactly these two to build its second copy.
                index_path: None,
                data_path: None,
                keep_path: None,
                // …but the SIZE is not missing with them. The store counted the
                // bytes it appended, and until this line nobody carried the
                // number out, so `measured_pack_bytes` fell through to "there
                // is nothing on disk to measure" on every push into the engine
                // the appliance ships. There is no file; there is a
                // measurement. Nothing on disk has to be stat'ed to learn it,
                // and on this engine nothing on disk could be.
                stored_bytes: Some(received.pack_bytes),
            },
            new_objects,
        })
    }
}

impl gunnar_wire::PackIngest for PushIngest<'_> {
    /// Straight into the engine. There is no second arm.
    ///
    /// What stood beside this ran `index_thin_pack` — `gix_pack::Bundle::
    /// write_to_directory` — writing a real `.pack`, `.idx` and `.keep` into
    /// `objects/pack`, reading the `.idx` back to learn which objects arrived,
    /// and dropping the `.keep` afterwards. Every line of it was the packfile
    /// engine's, and the engine is amputated.
    fn ingest(&self, pack: &mut dyn BufRead) -> gunnar_wire::Result<PackReceipt> {
        self.ingest_engine(pack)
    }
}

/// Did this reference move to a tip that does **not** descend from the one it
/// held? The event stream's `RefUpdated.forced`.
///
/// # This is one ancestry walk, not a second one
///
/// [`gunnar_policy::ancestry::is_descendant_of`] is the same function
/// `BranchProtectionGate` decides a non-fast-forward with, and it is the one
/// cross-checked against `git merge-base --is-ancestor` in that crate's tests.
/// Re-deriving the answer here — `old != new`, a hex compare, a first-parent
/// walk — is the mistake that gate's module docs enumerate, and two
/// implementations that disagree would put a UI and a policy on different sides
/// of the same push (one implementation).
///
/// The gate cannot simply be asked, because it only runs where a protection
/// rule matches. The audit trail has to answer for **every** reference on every
/// repository, including the ones nothing protects, which is the case where
/// nobody is watching.
///
/// # An undecidable walk reports `true`
///
/// A missing object or an exhausted budget is not evidence of a fast-forward.
/// `BranchProtectionGate` refuses on undecided rather than allowing; the same
/// judgement here means flagging rather than clearing, with the reason logged at
/// `warn`. Reporting `false` for a walk that never finished is exactly the
/// wrong-answer this field is being fixed for.
fn was_forced(git: &dyn GitServe, command: &RefCommand) -> bool {
    // A create has no previous tip, and a delete has no new one. Neither is a
    // rewrite of anything.
    if command.is_create() || command.is_delete() {
        return false;
    }
    match gunnar_policy::ancestry::is_descendant_of(
        git,
        &command.new,
        &command.old,
        gunnar_policy::ancestry::DEFAULT_ANCESTRY_BUDGET,
    ) {
        Ok(descends) => !descends,
        Err(err) => {
            tracing::warn!(
                target: TARGET_POLICY,
                ref_name = %command.name,
                old = %command.old,
                new = %command.new,
                %err,
                "the ancestry walk behind RefUpdated.forced could not be completed; the update \
                 is published as forced, because `not forced` is a claim this could not make"
            );
            true
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn publish(
    bus: &EventBus,
    request: &ServiceRequest,
    command: &RefCommand,
    status: &RefStatus,
    atomic: bool,
    fingerprint: &str,
    credential: &str,
    gates: &HashMap<String, String>,
    forced: bool,
) {
    let repo = Some(RepoPath::new(&request.repo.namespace, &request.repo.name));
    let payload = match status {
        RefStatus::Ok if command.is_delete() => event::Payload::RefDeleted(RefDeleted {
            repo,
            ref_name: command.name.to_string(),
            old_oid: command.old.to_string(),
            principal_id: request.principal.id.clone(),
            key_fingerprint: fingerprint.to_owned(),
            credential: credential.to_owned(),
        }),
        RefStatus::Ok => event::Payload::RefUpdated(RefUpdated {
            repo,
            ref_name: command.name.to_string(),
            old_oid: command.old.to_string(),
            new_oid: command.new.to_string(),
            principal_id: request.principal.id.clone(),
            key_fingerprint: fingerprint.to_owned(),
            credential: credential.to_owned(),
            // Answered by [`was_forced`], not assumed. This was a hardcoded
            // `false`, which made every force-push on the server
            // indistinguishable from an ordinary one on `Events.Watch`.
            forced,
        }),
        RefStatus::Rejected(reason) => event::Payload::PushRejected(PushRejected {
            repo,
            ref_name: command.name.to_string(),
            // Empty when nothing named a gate: a connectivity failure, a
            // compare-and-swap that lost, or the collateral refusal every other
            // reference gets in an `atomic` push. Those are real refusals and
            // they belong on the stream, but attributing them to a gate that
            // never ran would be a lie a UI would repeat.
            gate: gates
                .get(&command.name.to_string())
                .cloned()
                .unwrap_or_default(),
            reason: reason.clone(),
            principal_id: request.principal.id.clone(),
            whole_push_rejected: atomic,
        }),
    };
    bus.publish(payload);
}

// ───────────────────────────────────────────────────────────────────────────
// the policy seam
// ───────────────────────────────────────────────────────────────────────────

/// `gunnar-wire`'s [`PushGate`] over `gunnar-policy`'s [`PolicyEngine`].
struct PolicyGate {
    engine: PolicyEngine,
    /// Why this repository's policy could not be read, if it could not be.
    ///
    /// `Some` makes [`PolicyGate::check`] refuse **every** reference (GUN-007).
    /// An unreadable policy used to become `PolicyConfig::default()`, and an
    /// empty engine is let through by the `is_empty()` branch below — so a
    /// document with a typo, or a storage fault, quietly turned branch
    /// protection and `require_signature` off for the duration. This is the
    /// field that keeps "no gates are configured" and "the gates could not be
    /// read" from being the same value.
    unreadable: Option<String>,
    principal: gunnar_policy::Principal,
    repo: RepoId,
    keys: Box<dyn KeyDirectory>,
    metrics: Arc<ServerMetrics>,
    policy_path: PathBuf,
    /// Reference name → the `Gate::name()` that refused it.
    ///
    /// `PushGate::check` may only answer `Err(String)`, so the *name* of the
    /// gate that said no is lost at that boundary; it survives only inside the
    /// `Decision`. The event stream's `PushRejected.gate` needs it, so it is recorded here
    /// on the way past rather than parsed back out of the reason string; a
    /// reason is written for a human and its shape is free to change.
    refused_by: Mutex<HashMap<String, String>>,
}

/// `gunnar-core`'s principal kind as `gunnar-policy` names it.
///
/// # The fourth arm, which must never be taken
///
/// A [`PolicyGate`] is built only for a **push**, and a push needs
/// [`Level::Write`](gunnar_core::Level::Write), which
/// `gunnar_core::AuthTable::may` refuses to the anonymous principal
/// unconditionally, before `Dispatch::authorise` returns and long before this
/// runs. So the anonymous arm is unreachable through every route that exists.
///
/// It is loud rather than plausible. `gunnar-policy` has three kinds and none of
/// them means "nobody"; quietly answering `User` would put an anonymous push
/// through gates written about humans, and do it silently. If this line is ever
/// reached, the ACL has stopped being consulted before the gate. That is a
/// security incident, and it reads like one in the log.
pub(crate) fn policy_principal_kind(
    kind: gunnar_core::PrincipalKind,
) -> gunnar_policy::PrincipalKind {
    match kind {
        gunnar_core::PrincipalKind::User => gunnar_policy::PrincipalKind::User,
        gunnar_core::PrincipalKind::Machine => gunnar_policy::PrincipalKind::Machine,
        gunnar_core::PrincipalKind::Deploy => gunnar_policy::PrincipalKind::Deploy,
        // A group holds no keys and is never authenticated as (punkt 9), so a
        // push can never be attributed to one — the same unreachable-by-design
        // shape as the anonymous arm below, and loud for the same reason: a
        // group at the pre-receive gate means a keyless identity was handed a
        // push, which is the ACL being bypassed.
        gunnar_core::PrincipalKind::Group => {
            tracing::error!(
                target: gunnar_core::TARGET_AUDIT,
                event = "policy.gate.group_push",
                "a GROUP principal reached the pre-receive gate. A group holds no keys and \
                 cannot authenticate, so this cannot happen unless the grant check has been \
                 bypassed. Treating it as a machine identity for the gate; investigate \
                 immediately."
            );
            debug_assert!(
                false,
                "a group principal reached the pre-receive gate (punkt 9)"
            );
            gunnar_policy::PrincipalKind::Machine
        }
        gunnar_core::PrincipalKind::Anonymous => {
            tracing::error!(
                target: gunnar_core::TARGET_AUDIT,
                event = "policy.gate.anonymous_push",
                "an ANONYMOUS principal reached the pre-receive gate. Anonymous access is \
                 read-only and the ACL refuses it Write, so this cannot happen unless the \
                 grant check has been bypassed. Treating it as a machine identity for the \
                 gate; investigate immediately."
            );
            debug_assert!(false, "an anonymous principal reached the pre-receive gate");
            gunnar_policy::PrincipalKind::Machine
        }
    }
}

impl PolicyGate {
    fn open(
        git_dir: &Path,
        request: &ServiceRequest,
        stores: &ServerStores,
        metrics: Arc<ServerMetrics>,
    ) -> Self {
        let policy_path = git_dir.join(POLICY_FILE);
        let (config, unreadable) = match load_policy(stores, &request.repo, &policy_path) {
            Ok(config) => (config, None),
            // An empty engine on purpose: nothing should be evaluated against
            // a policy that could not be read, and `check` refuses before it
            // would be.
            Err(why) => (PolicyConfig::default(), Some(why)),
        };
        // An ENCRYPTED repository adds the plaintext check to whatever policy
        // it has: the flag is the account admin's, not the policy file's, so
        // no policy edit can switch it off.
        let mut gates = config.gates();
        if request.repo.encrypted {
            gates.push(Box::new(gunnar_policy::EncryptedGate::new()));
        }
        PolicyGate {
            engine: PolicyEngine::new(gates),
            unreadable,
            principal: gunnar_policy::Principal {
                id: request.principal.id.clone(),
                name: request.principal.name.clone(),
                kind: policy_principal_kind(request.principal.kind),
            },
            repo: RepoId::new(&request.repo.namespace, &request.repo.name),
            keys: Box::new(key_directory(stores)),
            metrics,
            policy_path,
            refused_by: Mutex::new(HashMap::new()),
        }
    }

    /// Which gate refused each reference, for the event-stream `PushRejected` events.
    ///
    /// Read after `ReceivePack::serve` has returned, so every `check` that was
    /// going to happen has happened.
    fn gate_that_refused(&self) -> HashMap<String, String> {
        self.refused_by
            .lock()
            .expect("the refusing-gate lock")
            .clone()
    }
}

impl PushGate for PolicyGate {
    fn check(
        &self,
        command: &RefCommand,
        facts: &PushFacts<'_>,
    ) -> std::result::Result<(), String> {
        // One span per gate evaluation. It is a child of
        // `git.receive_pack`, so the connection, principal and repository come
        // for free; what is new is the reference and how long deciding it took.
        // Branch protection walks ancestry, and an ancestry walk on a large
        // repository is one of the two places a push spends unexplained
        // seconds.
        let span = tracing::debug_span!(
            target: TARGET_POLICY,
            "policy.gate",
            ref_name = %command.name,
        );
        let _entered = span.enter();
        let started = std::time::Instant::now();
        // ── the policy could not be READ, which is not "there is none" ─────
        //
        // First, before `is_empty()`, because `is_empty()` is the branch that
        // lets a push through and an unreadable policy produced exactly that
        // value (GUN-007). A pre-receive refusal costs pushes and nothing
        // else: clone, fetch and browse are untouched, and the operator is
        // told which document and why.
        if let Some(why) = &self.unreadable {
            self.metrics.add_gate_refusals(1);
            tracing::error!(
                target: TARGET_POLICY,
                event = "policy.gate.unreadable",
                ref_name = %command.name,
                %why,
                "the policy for this repository could not be read, so every gate refuses"
            );
            return Err(format!(
                "gunnar could not read the policy for this repository, so it refuses this push \
                 rather than serving it with no gates at all: {why}. Reads are unaffected; fix \
                 the document (or `gunnar control policy set-protection`) and push again."
            ));
        }
        if self.engine.is_empty() {
            // No gates are configured, so there is nothing to decide. Said out
            // loud rather than reached by an engine that happens to return an
            // empty decision, because "allowed by every gate" and "there are no
            // gates" are different facts.
            return Ok(());
        }
        // A gate that cannot see what was pushed cannot decide, and an
        // undecidable gate refuses (the pre-receive gate). This is the branch that keeps a
        // configured limit from being silently satisfied by an empty object set.
        let new_objects = match facts.new_objects {
            Ok(ids) => ids,
            // No live gate reads the set, so its absence decides nothing.
            // The empty slice is never read: `reads_new_objects()` is exactly
            // the question "would any gate look at it".
            Err(_) if !self.engine.reads_new_objects() => &[],
            Err(reason) => {
                self.metrics.add_gate_refusals(1);
                tracing::error!(
                    target: TARGET_POLICY,
                    event = "policy.gate.undecidable",
                    %reason,
                    "the pushed objects could not be enumerated, so every configured gate refuses"
                );
                return Err(format!(
                    "gunnar could not enumerate the pushed objects, so the policy gates in {} \
                     could not decide: {reason}",
                    self.policy_path.display()
                ));
            }
        };
        let updates = [RefUpdate::from_wire(
            command.name.clone(),
            command.old,
            command.new,
        )];
        let mut ctx = PushContext::new(
            &self.principal,
            &self.repo,
            &updates,
            new_objects,
            facts.objects,
            self.keys.as_ref(),
        );
        // The byte count reaches the gate. Without it
        // `LimitsGate` refuses any push at all whenever `max_pack_bytes` is
        // configured, because it will not pretend an unmeasured pack is under a
        // limit.
        if let Some(bytes) = facts.pack_bytes {
            ctx = ctx.with_pack_bytes(bytes);
        }
        // The push options reach the gate. Unconditionally, including the empty
        // slice: `Some(&[])` is "the client sent none" and `None` is "nobody
        // looked", and `PushOptionsGate` refuses the second. Before this line
        // the whole tree's only consumer of a parsed push option was `.len()`
        // in the log field twenty lines below, so `push-options` was advertised
        // on the wire and discarded.
        ctx = ctx.with_push_options(facts.push_options);
        let decision = self.engine.evaluate(&ctx);
        let elapsed_ms = started.elapsed().as_millis() as u64;
        if decision.is_allowed() {
            tracing::debug!(
                target: TARGET_POLICY,
                event = "policy.gate.allowed",
                gates = self.engine.gate_names().len(),
                new_objects = new_objects.len(),
                elapsed_ms,
                "every gate allowed this reference"
            );
            Ok(())
        } else {
            // `evaluate` runs every gate, so a reference can be refused by more
            // than one. The first in gate order is the one named, and the
            // summary leads with the same one, so a UI and the client's `ng`
            // line agree about who said no.
            self.metrics.add_gate_refusals(1);
            if let Some(first) = decision.rejections.first() {
                // **`warn!`, and it NAMES THE GATE.** The audit trail's `Verify` asks for
                // exactly one error line per refused push naming the gate and
                // the reference; before this, a refused push produced a
                // `PushRejected` event with an empty `gate` field and no log
                // line at all that said who refused it.
                tracing::warn!(
                    target: TARGET_POLICY,
                    event = "policy.gate.refused",
                    gate = %first.gate,
                    reason = %first.reason,
                    refused_by_gates = decision.rejections.len(),
                    elapsed_ms,
                    "a policy gate refused this reference"
                );
                self.refused_by
                    .lock()
                    .expect("the refusing-gate lock")
                    .insert(command.name.to_string(), first.gate.clone());
            } else {
                // `is_allowed()` said no and named nobody. That is a bug in the
                // engine, not a refusal, and it must not read as a normal one.
                tracing::error!(
                    target: TARGET_POLICY,
                    event = "policy.gate.refused",
                    gate = "",
                    elapsed_ms,
                    "the policy engine refused a reference without naming a gate"
                );
            }
            Err(decision.summary())
        }
    }
}

/// Read the repository's policy document.
///
/// **`Err` is not an empty policy** (GUN-007). Until 2026-09-11 every failure
/// here — a document that did not parse, a control store that could not be
/// read, a file that could not be opened — returned `PolicyConfig::default()`,
/// and `PolicyGate::check` lets an empty engine through because "no gates are
/// configured" is a real and common answer. So a typo in a policy document, or
/// a transient storage fault, silently switched branch protection and the
/// signature requirement OFF for that push, at `error!` level in a log nobody
/// was reading at the time.
///
/// The old note here said refusing would be worse, because a typo would take
/// the repository offline. It does not: this is the PRE-RECEIVE gate, so a
/// refusal costs pushes and nothing else — clone, fetch, browse and every read
/// are untouched, and the operator is told which document and why. Degraded
/// operation must not be indistinguishable from full operation, which is the
/// same judgement `ancestry` already makes when a walk cannot finish.
///
/// `Ok(PolicyConfig::default())` survives for exactly one case and it is a
/// fact rather than a fault: nothing is stored and no file exists, so nothing
/// has been configured.
/// **The control store first, the file beside the repository second.**
///
/// `POLICY_FILE`'s constant used to carry the note *"When the catalog grows a
/// policy document, this constant is the only thing that changes."* The catalog
/// has grown one — `gunnar_core::PolicyStore`, at `refs/gunnar/policy` — and
/// this is that change. What did **not** change is the file: a repository
/// configured before the store existed keeps working, and an operator who edits
/// the JSON on disk still gets what they edited, until they write a policy over
/// the control plane and the stored one takes over.
///
/// The order is deliberate and is the only one that can be right. The stored
/// document is the one `Policy.SetProtection` writes and the one an operator
/// can see over gRPC; if a stale file could shadow it, turning protection on
/// through the console would appear to work and change nothing.
pub(crate) fn load_policy(
    stores: &ServerStores,
    repo: &gunnar_core::Repo,
    path: &Path,
) -> std::result::Result<PolicyConfig, String> {
    match stores.policies().get(&repo.path()) {
        Ok(Some(document)) => match PolicyConfig::from_json(&document) {
            Ok(config) => return Ok(config),
            Err(err) => {
                tracing::error!(
                    target: TARGET_POLICY,
                    repo = %repo.key(),
                    source = "control-store",
                    %err,
                    "the stored policy document did not parse; pushes to this repository are \
                     refused until it does"
                );
                return Err(format!("the stored policy document did not parse: {err}"));
            }
        },
        // Nothing stored. The file below is the pre-store shape and is still
        // the answer for a repository configured before `PolicyStore` existed.
        Ok(None) => {}
        Err(err) => {
            // NOT a fall-through to the file any more. The file "may be older
            // than what an operator configured" — its own words — so serving
            // it when the authoritative store is unreachable is a silent
            // downgrade to a policy nobody chose.
            tracing::error!(
                target: TARGET_POLICY,
                repo = %repo.key(),
                %err,
                "the policy store could not be read; pushes to this repository are refused \
                 rather than gated by a file that may be older than what an operator \
                 configured"
            );
            return Err(format!("the policy store could not be read: {err}"));
        }
    }
    let text = match std::fs::read_to_string(path) {
        Ok(text) => text,
        // The one `Ok(default)`: nothing stored and no file. Nothing has been
        // configured, which is a fact and not a fault.
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            return Ok(PolicyConfig::default())
        }
        Err(err) => {
            tracing::error!(
                target: TARGET_POLICY,
                path = %path.display(),
                %err,
                "the policy document could not be read; pushes to this repository are refused"
            );
            return Err(format!(
                "the policy document at {} could not be read: {err}",
                path.display()
            ));
        }
    };
    match PolicyConfig::from_json(&text) {
        Ok(config) => Ok(config),
        Err(err) => {
            tracing::error!(
                target: TARGET_POLICY,
                path = %path.display(),
                %err,
                "the policy document did not parse; pushes to this repository are refused until \
                 it does"
            );
            Err(format!(
                "the policy document at {} did not parse: {err}",
                path.display()
            ))
        }
    }
}

/// Every key gunnar knows **that has not expired**, for the ssh-sig gate (the pre-receive gate).
///
/// `signing_keys()` and not `keys()`, and that one word is a defect fixed on
/// 2026-08-05: this function used to load every row in the table, so a key past
/// its `expires` could no longer authenticate an SSH session and could still
/// satisfy `require_signature` on a push. The expiry was enforced on exactly one
/// of the two things a key does.
pub(crate) fn key_directory(stores: &ServerStores) -> StaticKeyDirectory {
    let mut directory = StaticKeyDirectory::new();
    let keys = match stores.auth().signing_keys() {
        Ok(keys) => keys,
        Err(err) => {
            tracing::error!(
                target: TARGET_POLICY,
                %err,
                "the identity table could not be read for signature verification"
            );
            return directory;
        }
    };
    for key in keys {
        // The identity table holds two credential kinds in one hash space
        // (auth.rs, the mTLS plan): an `x509` row is the bootstrap certificate,
        // not an ssh key, and cannot verify an ssh-sig — skipped by name, so
        // the WARN below is kept for a key that IS ssh and DID go bad
        // (measured 2026-08-29: the band warned at every login).
        if !ssh_verifiable_kind(&key.algo) {
            tracing::debug!(target: TARGET_POLICY, algo = %key.algo, fingerprint = %key.fingerprint_sha256, "not an ssh key kind; not a signature verifier");
            continue;
        }
        match gunnar_core::ssh_key::PublicKey::from_openssh(&key.openssh) {
            Ok(parsed) => {
                directory.add(&key.principal_id, parsed);
            }
            Err(err) => tracing::warn!(
                target: TARGET_POLICY,
                fingerprint = %key.fingerprint_sha256,
                %err,
                "a registered key did not parse and cannot verify a signature"
            ),
        }
    }
    directory
}

/// Is a registered key's `algo` (the name as OpenSSH writes it) something
/// the ssh-sig verifier can hold? `ssh-*`, `ecdsa-*`, `sk-*` are; `x509`
/// (the bootstrap certificate stored beside them) is not.
pub fn ssh_verifiable_kind(algo: &str) -> bool {
    algo.starts_with("ssh-") || algo.starts_with("ecdsa-") || algo.starts_with("sk-")
}

// ───────────────────────────────────────────────────────────────────────────
// Guards
// ───────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {

    /// ★ A build without the quarantine refuses an encrypted repository's push by name, and a
    /// plain repository's push is never refused for it (wiring tracer B 2026-10-08).
    #[test]
    fn a_build_without_the_quarantine_refuses_an_encrypted_push_by_name() {
        let line = super::unchecked_encrypted_push(true, false).expect("refused");
        assert!(
            line.contains("encrypted") && line.contains("this Gunnar"),
            "{line}"
        );
        assert_eq!(
            super::unchecked_encrypted_push(true, true),
            None,
            "the quarantine checks it"
        );
        assert_eq!(
            super::unchecked_encrypted_push(false, false),
            None,
            "a plain repository"
        );
    }
    use super::*;
    use gunnar_wire::pack::{IndexedPack, PackVersion};

    #[test]
    fn the_signature_directory_takes_ssh_kinds_and_skips_the_x509_row_by_name() {
        for ssh in [
            "ssh-ed25519",
            "ssh-rsa",
            "ecdsa-sha2-nistp256",
            "sk-ssh-ed25519@openssh.com",
            "sk-ecdsa-sha2-nistp256@openssh.com",
        ] {
            assert!(ssh_verifiable_kind(ssh), "{ssh}");
        }
        assert!(
            !ssh_verifiable_kind("x509"),
            "the bootstrap certificate is not tried as an ssh key"
        );
        assert!(!ssh_verifiable_kind(""));
    }

    /// A `PushOutcome` carrying whatever pack the caller wants to ask about.
    fn outcome_with(pack: Option<IndexedPack>) -> PushOutcome {
        PushOutcome {
            commands: Vec::new(),
            statuses: Vec::new(),
            unnameable: Vec::new(),
            push_options: Vec::new(),
            pack,
            pack_bytes: Some(0),
            new_objects: Ok(Vec::new()),
            atomic: false,
            unpack: Ok(()),
        }
    }

    /// `stored_bytes: None` on purpose: these arms are about what
    /// `measured_pack_bytes` does when the ENGINE reported no count, which is
    /// the only case where stat'ing a path is still the answer.
    fn indexed(data_path: Option<std::path::PathBuf>) -> IndexedPack {
        indexed_with(data_path, None)
    }

    fn indexed_with(
        data_path: Option<std::path::PathBuf>,
        stored_bytes: Option<u64>,
    ) -> IndexedPack {
        IndexedPack {
            num_objects: 1,
            pack_id: ObjectId::empty_blob(gix_hash::Kind::Sha1),
            index_id: ObjectId::empty_blob(gix_hash::Kind::Sha1),
            pack_version: PackVersion::V2,
            hash_kind: gix_hash::Kind::Sha1,
            index_path: None,
            data_path,
            keep_path: None,
            stored_bytes,
        }
    }

    /// **An engine that writes no packfile still knows how big the pack was.**
    ///
    /// The znippy store appends the received bytes verbatim and writes no
    /// `.pack`, so `data_path` is `None` — and every push into the engine the
    /// appliance ships logged `pack_bytes_unknown="pack … was indexed without a
    /// directory, so there is nothing on disk to measure"`. That sentence was
    /// FALSE: the store counted the bytes it appended
    /// (`PushReceipt::pack_bytes`) and nobody carried the number this far. An
    /// absence rendered where a measurement exists is the same defect as the
    /// reverse, and it is the one this whole family of tests is about.
    ///
    /// Both directions, or a `stored_bytes` that were ignored would still pass
    /// the first line: a store that reports NEITHER a file nor a size is still
    /// an honest unknown, and must still say so.
    #[test]
    fn a_pack_with_no_file_is_measured_by_the_store_that_stored_it() {
        let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed_with(None, Some(4096)))));
        assert_eq!(
            bytes,
            Some(4096),
            "the store's own count is the measurement"
        );
        assert_eq!(
            why, None,
            "a measured pack carries no reason for not being measured"
        );

        let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed_with(None, None))));
        assert_eq!(
            bytes, None,
            "neither a file nor a count is genuinely unknown"
        );
        assert!(
            why.as_deref().unwrap_or_default().contains("no size"),
            "and the reason must say the store reported no size, not merely that there is no \
             directory: {why:?}"
        );
    }

    /// **The defect this replaced.** `stat` failing rendered as `pack_bytes=0`,
    /// which is indistinguishable from a push that genuinely carried nothing.
    ///
    /// Seen red: with the body restored to
    /// `.and_then(|path| std::fs::metadata(path).ok()).map(len).unwrap_or(0)`
    /// this fails with *"a pack whose bytes could not be read reported
    /// Some(0)"* — the old code's exact answer.
    #[test]
    fn a_pack_that_cannot_be_stated_reports_no_number_and_names_the_path() {
        let missing = std::path::PathBuf::from("/nonexistent/gunnar/pack-deadbeef.pack");
        let outcome = outcome_with(Some(indexed(Some(missing.clone()))));
        let (bytes, why) = measured_pack_bytes(&outcome);
        assert_eq!(
            bytes, None,
            "a pack whose bytes could not be read reported {bytes:?}; the only honest answer is \
             no number at all, because 0 is a legal pack size for a deletions-only push"
        );
        let why = why.expect("an unreadable pack must say why it is unreadable");
        assert!(
            why.contains(missing.to_str().unwrap()),
            "the reason must name the path an operator has to go and look at, got {why:?}"
        );
    }

    /// The one arm that IS a zero, and must stay one: a deletions-only push
    /// carries no pack, and reporting that as *unknown* would be the mirror
    /// mistake.
    #[test]
    fn a_deletions_only_push_reports_a_measured_zero_and_no_reason() {
        let (bytes, why) = measured_pack_bytes(&outcome_with(None));
        assert_eq!(
            bytes,
            Some(0),
            "a push with no pack carries zero pack bytes"
        );
        assert_eq!(why, None, "zero bytes is a measurement, not an absence");
    }

    /// **Three different answers over the same empty list**, which is the whole
    /// reason the count takes a `Result` and not a slice.
    ///
    /// * not enumerated, 3-object pack → 3. Seen red on the rig's SSE feed
    ///   ("pushed 0 objects (0 B)" for a push that carried a commit).
    /// * not enumerated, no pack at all → 0, because there is nothing to count.
    /// * ENUMERATED and empty → 0 even beside a 3-object pack. That is a
    ///   measured zero: the store said which objects the push introduced and
    ///   the answer was none. Under the old `is_empty()` fallback this case
    ///   reported 3, i.e. the record disagreed with the store that had been
    ///   asked. It is the arm that makes this test unable to pass against the
    ///   code it replaces.
    #[test]
    fn the_object_count_reads_the_reason_not_the_length() {
        let mut pack = indexed(None);
        pack.num_objects = 3;
        let three = outcome_with(Some(pack));
        let none = outcome_with(None);
        let unknown = Err("the policy has no gate over objects");
        assert_eq!(attested_object_count(unknown, &three), 3);
        assert_eq!(attested_object_count(unknown, &none), 0);
        assert_eq!(
            attested_object_count(Ok(&[]), &three),
            0,
            "an enumerated empty set is a MEASURED zero and must not be overruled by the pack"
        );
        let oid = gix_hash::ObjectId::null(gix_hash::Kind::Sha1);
        assert_eq!(attested_object_count(Ok(&[oid, oid]), &none), 2);
    }

    /// A pack indexed with no directory wrote nothing, so there is nothing to
    /// measure — and that is not zero either.
    #[test]
    fn a_pack_never_written_to_disk_reports_no_number() {
        let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed(None))));
        assert_eq!(bytes, None);
        assert!(
            why.expect("must say why").contains("without a directory"),
            "the reason must distinguish 'never written' from 'written and unreadable'"
        );
    }

    /// The number is the file's real length, not a plausible one. Seen red by
    /// writing 7 bytes and asserting 6.
    #[test]
    fn a_pack_on_disk_reports_the_bytes_the_file_actually_holds() {
        let dir = tempfile::tempdir().expect("a temp dir");
        let path = dir.path().join("pack-x.pack");
        std::fs::write(&path, b"1234567").expect("write");
        let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed(Some(path)))));
        assert_eq!(bytes, Some(7), "the length must come off the filesystem");
        assert_eq!(why, None);
    }

    /// `wire_bytes` had the same `unwrap_or(0)`. `PushFacts::pack_bytes`
    /// documents `None` as "genuinely unavailable" and `Some(0)` as a
    /// deletions-only push; the log collapsed both onto `0`.
    #[test]
    fn an_uncounted_wire_is_not_a_wire_that_carried_nothing() {
        let (bytes, why) = measured(None, "the ingest did not report");
        assert_eq!(bytes, None, "an uncounted wire must not render as 0 bytes");
        assert_eq!(why.as_deref(), Some("the ingest did not report"));

        let (bytes, why) = measured(Some(0), "unused");
        assert_eq!(bytes, Some(0), "a counted zero stays a zero");
        assert_eq!(why, None, "a counted zero carries no reason");
    }

    /// The ssh-sig gate is handed **only unexpired** keys.
    ///
    /// The defect, found 2026-08-05: `key_directory` loaded `AuthTable::keys()`,
    /// which filters nothing, so a key past its `expires` could no longer open an
    /// SSH session and could still satisfy `require_signature` on a push. One
    /// credential with two lifetimes.
    ///
    /// `gunnar-core`'s `an_expired_key_signs_nothing_and_is_still_listed` guards
    /// the *predicate*. This guards the **wiring**, which is the half that was
    /// actually wrong: the predicate already existed and worked, and this call
    /// site simply did not ask it. A guard on the predicate alone would have been
    /// green over the whole bug.
    ///
    /// # Seen red
    ///
    /// With `signing_keys()` changed back to `keys()`, `all_keys()` has length 1
    /// and the first assertion fails. The valid-key half is the control: it fails
    /// if `key_directory` returns an empty directory for any other reason —
    /// an unreadable table, a key that will not parse — which would otherwise
    /// make the first assertion pass for the wrong reason.
    /// **Un-ignored 2026-08-10.** The reason it was skipped — *"this crate
    /// deliberately carries no storage engine"* — stopped being true when the
    /// engine became a feature of this crate with znippy as the default. There
    /// is no engine-free build of `gunnar-server` any longer to skip it for.
    #[test]
    fn the_signature_gate_is_handed_no_expired_key() {
        // The known-answer key `gunnar-core`'s own tests use, and a second one, so
        // the two principals below hold different key material.
        const EXPIRED_KEY: &str = "ssh-ed25519 \
            AAAAC3NzaC1lZDI1NTE5AAAAIMW4aqVr75w+3DWorOCbfwl5XCDCq1yCQ6puzU4qzol2 gunnar-kat";
        let minted = gunnar_core::mint_ed25519().expect("mint a key");
        let live_key = gunnar_core::ssh_key::PrivateKey::from_openssh(&minted)
            .expect("parse the minted key")
            .public_key()
            .to_openssh()
            .expect("render the public half");

        let dir = tempfile::tempdir().expect("a temp dir");
        let stores = ServerStores::open(dir.path()).expect("open the stores");
        for (id, key, expires) in [
            // Already expired: the unix epoch.
            ("stale", EXPIRED_KEY, Some(1_000)),
            ("live", live_key.as_str(), None),
        ] {
            stores
                .auth()
                .add_principal(
                    gunnar_core::Principal::new(id, id, gunnar_core::PrincipalKind::Machine)
                        .expect("a valid id"),
                )
                .expect("add the principal");
            stores
                .auth()
                .register_key(id, key, expires)
                .expect("register the key");
        }

        let directory = key_directory(&stores);
        assert!(
            directory.keys_for("stale").is_empty(),
            "an expired key must not be offered to the signature gate"
        );
        // The control. Without it an always-empty directory — an unreadable
        // table, a parse failure, a `key_directory` that returns early — passes
        // the line above while proving nothing.
        assert_eq!(
            directory.keys_for("live").len(),
            1,
            "an unexpired key must still be offered, or this test proves nothing"
        );
        assert_eq!(
            directory.all_keys().len(),
            1,
            "exactly the unexpired key, and nothing else, reaches the gate"
        );
    }
}