sozu 2.1.0

sozu, a fast, reliable, hot reconfigurable HTTP reverse proxy
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
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
//! Master command-verb dispatcher and audit envelope.
//!
//! Receives decoded `Request` protos from connected CLI clients, applies
//! `command_allowed_uids` admission, validates and fans out cluster /
//! listener / certificate / runtime mutations to workers, and emits the
//! audit-log envelope (`sanitize_for_audit`-protected JSON sink) for every
//! mutating verb. Long-form lifecycle: `bin/src/command/LIFECYCLE.md`.

use std::{
    collections::{BTreeMap, HashMap},
    env,
    fs::File,
    io::{ErrorKind, Read},
    path::PathBuf,
    time::Instant,
};

use mio::Token;
use nom::{HexDisplay, Offset};
use prost::Message as _;
use rusty_ulid::Ulid;
use sha2::{Digest, Sha256};
use sozu_command_lib::{
    buffer::fixed::Buffer,
    config::Config,
    logging,
    parser::parse_several_requests,
    proto::command::{
        AggregatedMetrics, AvailableMetrics, CertificatesWithFingerprints, ClusterHashes,
        ClusterInformations, Event, EventKind, FrontendFilters, HardStop, MetricDetail,
        MetricDetailStatus, MetricsConfiguration, QueryCertificatesFilters, QueryHealthChecks,
        QueryMetricsOptions, Request, ResponseContent, ResponseStatus, RunState, SetMetricDetail,
        SoftStop, Status, UpdateHttpListenerConfig, UpdateHttpsListenerConfig,
        UpdateTcpListenerConfig, UpdateUdpListenerConfig, WorkerInfo, WorkerInfos, WorkerRequest,
        WorkerResponses, request::RequestType, response_content::ContentType,
    },
    sd_notify,
};
use sozu_lib::metrics::METRICS;

use crate::command::{
    server::{
        DefaultGatherer, Gatherer, GatheringTask, MessageClient, Server, ServerState, Timeout,
        WorkerId,
    },
    sessions::{ClientSession, OptionalClient, sanitize_for_audit, sanitize_for_audit_kv},
    upgrade::{upgrade_main, upgrade_worker},
};

/// Pair a verb tag with its `config.<verb>` counter key in a single place so
/// the two strings cannot drift. Both must be string literals because the
/// metric drain stores `&'static str` keys.
///
/// Defined at the top of the module so that in-file macros expand before any
/// call site (Rust `macro_rules!` macros are textually scoped — definition
/// must precede use within a module).
macro_rules! audit_verb {
    ($verb:literal) => {
        ($verb, concat!("config.", $verb))
    };
}

/// Render the structured audit log line in the MUX-family layout.
///
/// Expands to a `format!` producing
/// `[session_ulid request_ulid cluster_id|- backend_id|-]\tAUDIT\tCommand(verb=..., actor_uid=..., actor_gid=..., actor_pid=..., actor_comm=..., client_id=..., target=..., result=..., [error_code=..., reason=..., elapsed_ms=..., fanout=..., workers=<ok>/<err>/<expected>,] sozu_version=...)`
/// with ANSI colours when the logger is colour-enabled (empty strings
/// otherwise — see [`sozu_command_lib::logging::ansi_palette`]). Bracketed
/// fields are emitted only when set on [`AuditEntry`] / the caller.
///
/// Bracket layout mirrors `log_context!` in `lib/src/protocol/mux/mod.rs:50`
/// so operators can grep `AUDIT` alongside `MUX` / `RUSTLS` / `PIPE` / `TCP`.
/// Uses the `Command(...)` keyword (vs. `Session(...)` in MUX lines) because
/// the payload describes a control-plane command, not a proxy session. The
/// line is self-contained — no `\t >>>` continuation marker since nothing
/// follows the closing paren.
///
/// Every string field — `verb` is a `&'static str` and therefore trusted,
/// but `target`, `cluster_id`, `backend_id`, `actor_comm`, `reason` can
/// originate from attacker-influenced input (cluster IDs from sozu CLI
/// arguments, hostnames from frontend configs, error messages from
/// `state.dispatch`). All of them go through
/// [`sozu_command_lib::sessions::sanitize_for_audit`] at render time to
/// neutralise `\n`/`\t`/`\x1b` injection that would otherwise forge a
/// second audit line.
macro_rules! audit_log_context {
    ($server:expr, $client:expr, $request_id:expr, $entry:expr, $result:expr) => {{
        use $crate::command::sessions::{sanitize_for_audit, sanitize_for_audit_kv};
        let (open, reset, grey, gray, white) = ::sozu_command_lib::logging::ansi_palette();
        let log_ctx = ::sozu_command_lib::logging::LogContext {
            session_id: $client.session_ulid,
            request_id: Some(*$request_id),
            cluster_id: $entry.cluster_id.as_deref(),
            backend_id: $entry.backend_id.as_deref(),
        };
        let mut extras = String::new();
        if let Some(code) = $entry.extras.error_code {
            extras.push_str(&format!(
                ", {gray}error_code{reset}={white}{code}{reset}",
                gray = gray,
                reset = reset,
                white = white,
                code = code,
            ));
        }
        if let Some(reason) = $entry.extras.reason.as_deref() {
            let sanitized = sanitize_for_audit(reason);
            let truncated = if sanitized.chars().count() > AUDIT_REASON_MAX_CHARS {
                let cut: String = sanitized.chars().take(AUDIT_REASON_MAX_CHARS).collect();
                format!("{cut}…")
            } else {
                sanitized
            };
            extras.push_str(&format!(
                ", {gray}reason{reset}={white}{reason}{reset}",
                gray = gray,
                reset = reset,
                white = white,
                reason = truncated,
            ));
        }
        if let Some(elapsed) = $entry.extras.elapsed_ms {
            extras.push_str(&format!(
                ", {gray}elapsed_ms{reset}={white}{elapsed}{reset}",
                gray = gray,
                reset = reset,
                white = white,
                elapsed = elapsed,
            ));
        }
        if let Some(fanout) = $entry.extras.fanout {
            extras.push_str(&format!(
                ", {gray}fanout{reset}={white}{status}{reset}, {gray}workers{reset}={white}{ok}/{err}/{expected}{reset}",
                gray = gray,
                reset = reset,
                white = white,
                status = fanout.status,
                ok = fanout.workers_ok,
                err = fanout.workers_err,
                expected = fanout.workers_expected,
            ));
        }
        if let Some(hash) = $entry.extras.request_sha256.as_deref() {
            extras.push_str(&format!(
                ", {gray}request_sha256{reset}={white}{hash}{reset}",
                gray = gray,
                reset = reset,
                white = white,
                hash = hash,
            ));
        }
        if let Some(lease_id) = $entry.extras.metric_detail_lease_id.as_deref() {
            let sanitized = sanitize_for_audit_kv(lease_id);
            let truncated = if sanitized.chars().count() > AUDIT_LEASE_ID_MAX_CHARS {
                let cut: String = sanitized.chars().take(AUDIT_LEASE_ID_MAX_CHARS).collect();
                format!("{cut}…")
            } else {
                sanitized
            };
            extras.push_str(&format!(
                ", {gray}lease_id{reset}={white}{value}{reset}",
                gray = gray,
                reset = reset,
                white = white,
                value = truncated,
            ));
        }
        if let Some(detail_reason) = $entry.extras.metric_detail_reason.as_deref() {
            let sanitized = sanitize_for_audit_kv(detail_reason);
            let truncated = if sanitized.chars().count() > AUDIT_REASON_MAX_CHARS {
                let cut: String = sanitized.chars().take(AUDIT_REASON_MAX_CHARS).collect();
                format!("{cut}…")
            } else {
                sanitized
            };
            extras.push_str(&format!(
                ", {gray}metric_detail_reason{reset}={white}{value}{reset}",
                gray = gray,
                reset = reset,
                white = white,
                value = truncated,
            ));
        }
        let now_ts = rfc3339_utc(std::time::SystemTime::now());
        let connect_ts = $client.connect_ts_display();
        format!(
            "{gray}{ctx}{reset}\t{open}AUDIT{reset}\t{grey}Command{reset}({gray}ts{reset}={white}{ts}{reset}, {gray}verb{reset}={white}{verb}{reset}, {gray}actor_uid{reset}={white}{actor_uid}{reset}, {gray}actor_gid{reset}={white}{actor_gid}{reset}, {gray}actor_pid{reset}={white}{actor_pid}{reset}, {gray}actor_role{reset}={white}{actor_role}{reset}, {gray}actor_user{reset}={white}{actor_user}{reset}, {gray}actor_comm{reset}={white}{actor_comm}{reset}, {gray}client_id{reset}={white}{client_id}{reset}, {gray}connect_ts{reset}={white}{connect_ts}{reset}, {gray}socket{reset}={white}{socket_path}{reset}, {gray}target{reset}={white}{target}{reset}, {gray}result{reset}={white}{result}{reset}{extras}, {gray}sozu_version{reset}={white}{sozu_version}{reset}, {gray}build_git_sha{reset}={white}{build_git_sha}{reset}, {gray}boot_generation{reset}={white}{boot_generation}{reset})",
            open = open,
            reset = reset,
            grey = grey,
            gray = gray,
            white = white,
            ctx = log_ctx,
            ts = now_ts,
            verb = $entry.verb,
            actor_uid = $client.actor_uid_display(),
            actor_gid = $client.actor_gid_display(),
            actor_pid = $client.actor_pid_display(),
            actor_role = actor_role($client.actor_uid),
            actor_user = $client.actor_user_display(),
            actor_comm = $client.actor_comm_display(),
            client_id = $client.id,
            connect_ts = connect_ts,
            socket_path = sanitize_for_audit(&$client.socket_path),
            target = sanitize_for_audit(&$entry.target),
            result = $result,
            extras = extras,
            sozu_version = SOZU_VERSION,
            build_git_sha = SOZU_BUILD_GIT_SHA,
            boot_generation = $server.boot_generation,
        )
    }};
}

/// Operator-issued verbs that change cluster / listener / certificate
/// state, the saved state file, or the master/worker fleet topology.
/// `true` here brackets the dispatch with `RELOADING=1` / `READY=1`
/// systemd notifications so unit-state-watching tooling can serialise
/// against the change. Read-only verbs (Status / List* / Query* /
/// CountRequests / SubscribeEvents / QueryMaxConnectionsPerIp) return
/// `false` because they're dashboard polls, not transitions.
///
/// `SetMetricDetail` is deliberately excluded: it is a runtime
/// observability knob, not a state transition, and the `sozu top` TUI
/// renews its lease every `ttl/2` seconds (≈ 30 s by default). Including
/// it in this set would flap the systemd unit through `reloading`
/// every renewal for the whole TUI session lifetime. The audit trail
/// for the verb still flows through the special-case inline emission
/// (`EventKind::MetricDetailChanged`, proto tag 30) so SOC visibility
/// is preserved without flapping the unit state.
fn is_mutating_verb(req: &RequestType) -> bool {
    matches!(
        req,
        RequestType::SaveState(_)
            | RequestType::LoadState(_)
            | RequestType::ReloadConfiguration(_)
            | RequestType::UpgradeMain(_)
            | RequestType::UpgradeWorker(_)
            | RequestType::AddCluster(_)
            | RequestType::ActivateListener(_)
            | RequestType::AddBackend(_)
            | RequestType::AddCertificate(_)
            | RequestType::AddHttpFrontend(_)
            | RequestType::AddHttpListener(_)
            | RequestType::AddHttpsFrontend(_)
            | RequestType::AddHttpsListener(_)
            | RequestType::AddTcpFrontend(_)
            | RequestType::AddTcpListener(_)
            | RequestType::AddUdpFrontend(_)
            | RequestType::AddUdpListener(_)
            | RequestType::ConfigureMetrics(_)
            | RequestType::DeactivateListener(_)
            | RequestType::RemoveBackend(_)
            | RequestType::RemoveCertificate(_)
            | RequestType::RemoveCluster(_)
            | RequestType::RemoveHttpFrontend(_)
            | RequestType::RemoveHttpsFrontend(_)
            | RequestType::RemoveListener(_)
            | RequestType::RemoveTcpFrontend(_)
            | RequestType::RemoveUdpFrontend(_)
            | RequestType::ReplaceCertificate(_)
            | RequestType::UpdateHttpListener(_)
            | RequestType::UpdateHttpsListener(_)
            | RequestType::UpdateTcpListener(_)
            | RequestType::UpdateUdpListener(_)
            | RequestType::SetHealthCheck(_)
            | RequestType::RemoveHealthCheck(_)
            | RequestType::SoftStop(_)
            | RequestType::HardStop(_)
            | RequestType::Logging(_)
            | RequestType::SetMaxConnectionsPerIp(_)
    )
}

impl Server {
    pub fn handle_client_request(&mut self, client: &mut ClientSession, request: Request) {
        let request_type = match request.request_type {
            Some(req) => req,
            None => {
                error!("empty request sent by client {:?}", client);
                return;
            }
        };
        // Optional UID allowlist. `None` preserves the historical
        // behaviour (same-UID local process can do anything).
        // When set, requests from UIDs outside the list are rejected
        // before dispatch — both read and write — and the rejection
        // appears in the audit trail via `client.finish_failure`.
        if let Some(allowed) = self.config.command_allowed_uids.as_ref() {
            let actor_uid = client.actor_uid;
            let permitted = actor_uid.is_some_and(|u| allowed.contains(&u));
            if !permitted {
                warn!(
                    "rejecting command-socket request from non-allowlisted UID: actor_uid={} allowed={:?} verb={:?}",
                    actor_uid
                        .map(|u| u.to_string())
                        .unwrap_or_else(|| "unknown".to_owned()),
                    allowed,
                    std::mem::discriminant(&request_type)
                );
                client.finish_failure(format!(
                    "unauthorized: actor UID {} not in command_allowed_uids",
                    actor_uid
                        .map(|u| u.to_string())
                        .unwrap_or_else(|| "unknown".to_owned())
                ));
                return;
            }
        }

        // #228: bracket every operator-issued command with
        // `RELOADING=1` / `READY=1`. Mutating verbs (LoadState,
        // ReloadConfiguration, AddCluster / Backend / Certificate /
        // Frontend / Listener, Remove*, Replace*, Update*,
        // SetHealthCheck, SetMaxConnectionsPerIp, UpgradeMain,
        // UpgradeWorker) move the master through a brief reload
        // window where downstream tooling watching the unit state
        // can serialise against in-flight changes. Read-only verbs
        // (Status, ListWorkers, ListListeners, ListFrontends,
        // QueryClusters*, QueryMetrics, QueryCertificates*,
        // CountRequests, QueryHealthChecks, SubscribeEvents,
        // QueryMaxConnectionsPerIp) skip the bracketing — those are
        // dashboard polls, not state transitions.
        //
        // Helper is a no-op when `$NOTIFY_SOCKET` is unset, so the
        // cost is one env-var lookup per command in non-systemd
        // deployments.
        let mutating = is_mutating_verb(&request_type);
        // INVARIANT: the systemd bracket is symmetric. `mutating` is the
        // sole gate for BOTH the RELOADING=1 (entry) and READY=1 (exit)
        // notifications below, so it must be captured exactly once into this
        // local and reused — never recomputed against a moved/mutated verb —
        // otherwise a verb that opened the reload window could skip closing
        // it (leaving the unit stuck in `reloading`) or vice versa.
        debug_assert_eq!(
            mutating,
            is_mutating_verb(&request_type),
            "is_mutating_verb must be a pure function of the verb (RELOADING/READY bracket gate)"
        );
        // INVARIANT: read-only verbs are never bracketed. The pure dashboard
        // polls (Status / Query* / List* / Count) are dispatched WITHOUT
        // touching `ConfigState` or the fleet, so flapping the systemd unit
        // through `reloading` for them would be a correctness bug for any
        // unit-state-watching tooling. SetMetricDetail is also excluded by
        // design (runtime lease, not a transition — see the doc on the fn).
        debug_assert!(
            !mutating
                || !matches!(
                    request_type,
                    RequestType::Status(_)
                        | RequestType::ListWorkers(_)
                        | RequestType::ListFrontends(_)
                        | RequestType::ListListeners(_)
                        | RequestType::QueryClustersHashes(_)
                        | RequestType::QueryClustersByDomain(_)
                        | RequestType::QueryClusterById(_)
                        | RequestType::QueryCertificatesFromWorkers(_)
                        | RequestType::QueryCertificatesFromTheState(_)
                        | RequestType::QueryMetrics(_)
                        | RequestType::QueryHealthChecks(_)
                        | RequestType::CountRequests(_)
                        | RequestType::SubscribeEvents(_)
                        | RequestType::QueryMaxConnectionsPerIp(_)
                        | RequestType::SetMetricDetail(_)
                ),
            "read-only / non-transition verbs must not open the systemd reload window"
        );
        if mutating {
            if let Err(e) = sd_notify::notify(sd_notify::STATE_RELOADING) {
                warn!("could not notify systemd RELOADING=1: {}", e);
            }
        }

        match request_type {
            RequestType::SaveState(path) => save_state(self, client, &path),
            RequestType::LoadState(path) => load_state(self, Some(client), &path),
            RequestType::ListWorkers(_) => list_workers(self, client),
            RequestType::ListFrontends(inner) => list_frontend_command(self, client, inner),
            RequestType::ListListeners(_) => list_listeners(self, client),
            RequestType::UpgradeMain(_) => upgrade_main(self, client),
            RequestType::UpgradeWorker(worker_id) => upgrade_worker(self, client, worker_id),
            RequestType::SubscribeEvents(_) => subscribe_client_to_events(self, client),
            RequestType::ReloadConfiguration(path) => {
                load_static_config(self, Some(client), Some(&path))
            }
            RequestType::Status(_) => status(self, client),
            RequestType::AddCluster(_)
            | RequestType::ActivateListener(_)
            | RequestType::AddBackend(_)
            | RequestType::AddCertificate(_)
            | RequestType::AddHttpFrontend(_)
            | RequestType::AddHttpListener(_)
            | RequestType::AddHttpsFrontend(_)
            | RequestType::AddHttpsListener(_)
            | RequestType::AddTcpFrontend(_)
            | RequestType::AddTcpListener(_)
            | RequestType::AddUdpFrontend(_)
            | RequestType::AddUdpListener(_)
            | RequestType::ConfigureMetrics(_)
            | RequestType::DeactivateListener(_)
            | RequestType::RemoveBackend(_)
            | RequestType::RemoveCertificate(_)
            | RequestType::RemoveCluster(_)
            | RequestType::RemoveHttpFrontend(_)
            | RequestType::RemoveHttpsFrontend(_)
            | RequestType::RemoveListener(_)
            | RequestType::RemoveTcpFrontend(_)
            | RequestType::RemoveUdpFrontend(_)
            | RequestType::ReplaceCertificate(_)
            | RequestType::UpdateHttpListener(_)
            | RequestType::UpdateHttpsListener(_)
            | RequestType::UpdateTcpListener(_)
            | RequestType::UpdateUdpListener(_)
            | RequestType::SetHealthCheck(_)
            | RequestType::RemoveHealthCheck(_) => {
                worker_request(self, client, request_type);
            }
            RequestType::QueryClustersHashes(_)
            | RequestType::QueryClustersByDomain(_)
            | RequestType::QueryCertificatesFromWorkers(_)
            | RequestType::QueryClusterById(_) => {
                query_clusters(self, client, request_type);
            }
            RequestType::QueryMetrics(inner) => query_metrics(self, client, inner),
            RequestType::SoftStop(_) => stop(self, client, false),
            RequestType::HardStop(_) => stop(self, client, true),
            RequestType::Logging(logging_filter) => set_logging_level(self, client, logging_filter),
            RequestType::QueryCertificatesFromTheState(filters) => {
                query_certificates_from_main(self, client, filters)
            }
            RequestType::CountRequests(_) => count_requests(self, client),
            RequestType::QueryHealthChecks(query) => list_health_checks(self, client, query),

            RequestType::LaunchWorker(_) => {} // not yet implemented, nor used, anywhere
            RequestType::ReturnListenSockets(_) => {} // This is only implemented by workers,
            // Per-(cluster, source-IP) connection-limit runtime hooks. Both
            // the setter and the query are pure worker-side operations
            // (the live counter lives in `SessionManager`, not in the
            // master's `ConfigState`), so we hand them off to the
            // generic worker fan-out path.
            RequestType::SetMaxConnectionsPerIp(_) | RequestType::QueryMaxConnectionsPerIp(_) => {
                worker_request(self, client, request_type);
            }
            // `sozu top`'s runtime cardinality lease verb. Each worker maintains
            // its own lease table and recomputes the effective `MetricDetail` as
            // `max(configured, max(active leases))`. The master fans the verb out
            // through a dedicated dispatcher that synthesises the aggregate
            // `MetricDetailStatus` reply, captures the master's own
            // configured/effective view, and emits the attempt-time + completion
            // audit rows alongside the per-worker fan-out.
            RequestType::SetMetricDetail(req) => {
                set_metric_detail_request(self, client, req);
            }
        }

        if mutating {
            if let Err(e) = sd_notify::notify(sd_notify::STATE_READY) {
                warn!("could not notify systemd READY=1: {}", e);
            }
        }
    }

    /// get infos from the state of the main process
    fn query_main(&self, request: RequestType) -> Option<ResponseContent> {
        match request {
            RequestType::QueryClusterById(cluster_id) => Some(
                ContentType::Clusters(ClusterInformations {
                    vec: self.state.cluster_state(&cluster_id).into_iter().collect(),
                })
                .into(),
            ),
            RequestType::QueryClustersByDomain(domain) => {
                let cluster_ids = self
                    .state
                    .get_cluster_ids_by_domain(domain.hostname, domain.path);
                let vec: Vec<_> = cluster_ids
                    .iter()
                    .filter_map(|cluster_id| self.state.cluster_state(cluster_id))
                    .collect();
                // INVARIANT: `filter_map` can only drop entries, so the
                // resolved cluster-info vec never exceeds the matched id set.
                // A larger vec would mean we synthesised a cluster the domain
                // index never resolved.
                debug_assert!(
                    vec.len() <= cluster_ids.len(),
                    "QueryClustersByDomain result must not exceed the matched cluster-id set"
                );
                Some(ContentType::Clusters(ClusterInformations { vec }).into())
            }
            RequestType::QueryClustersHashes(_) => Some(
                ContentType::ClusterHashes(ClusterHashes {
                    map: self.state.hash_state(),
                })
                .into(),
            ),
            RequestType::ListFrontends(filters) => {
                Some(ContentType::FrontendList(self.state.list_frontends(filters)).into())
            }
            _ => None,
        }
    }
}

//===============================================
// non-scattered commands

pub fn query_certificates_from_main(
    server: &mut Server,
    client: &mut ClientSession,
    filters: QueryCertificatesFilters,
) {
    debug!(
        "querying certificates in the state with filters {}",
        filters
    );

    let certs = server.state.get_certificates(filters);

    client.finish_ok_with_content(
        ContentType::CertificatesWithFingerprints(CertificatesWithFingerprints { certs }).into(),
        "Successfully queried certificates from the state of main process",
    );
}

fn list_health_checks(server: &mut Server, client: &mut ClientSession, query: QueryHealthChecks) {
    let health_checks = server.state.list_health_checks(query.cluster_id.as_deref());
    client.finish_ok_with_content(
        ContentType::HealthChecksList(health_checks).into(),
        "Successfully listed health check configurations",
    );
}

/// return how many requests were received by Sōzu since startup
fn count_requests(server: &mut Server, client: &mut ClientSession) {
    let request_counts = server.state.get_request_counts();

    client.finish_ok_with_content(
        ContentType::RequestCounts(request_counts).into(),
        "Successfully counted requests received by the state",
    );
}

pub fn list_frontend_command(
    server: &mut Server,
    client: &mut ClientSession,
    filters: FrontendFilters,
) {
    match server.query_main(RequestType::ListFrontends(filters)) {
        Some(response) => client.finish_ok_with_content(response, "Successfully listed frontends"),
        None => client.finish_failure("main process could not list frontends"),
    }
}

fn list_workers(server: &mut Server, client: &mut ClientSession) {
    let vec = server
        .workers
        .values()
        .map(|worker| WorkerInfo {
            id: worker.id,
            pid: worker.pid,
            run_state: worker.run_state as i32,
        })
        .collect();

    debug!("workers: {:?}", vec);
    client.finish_ok_with_content(
        ContentType::Workers(WorkerInfos { vec }).into(),
        "Successfully listed workers",
    );
}

fn list_listeners(server: &mut Server, client: &mut ClientSession) {
    let vec = server.state.list_listeners();
    client.finish_ok_with_content(
        ContentType::ListenersList(vec).into(),
        "Successfully listed listeners",
    );
}

fn save_state(server: &mut Server, client: &mut ClientSession, path: &str) {
    let mut path = PathBuf::from(path);
    if path.is_relative() {
        match std::env::current_dir() {
            Ok(cwd) => path = cwd.join(path),
            Err(error) => {
                let (verb, counter) = audit_verb!("state_saved");
                audit_emit_inline(
                    server,
                    client,
                    EventKind::StateSaved,
                    verb,
                    counter,
                    format!("file:{}", path.display()),
                    AuditResult::Err,
                    AuditExtras {
                        error_code: Some(AuditErrorCode::IoError),
                        ..Default::default()
                    },
                );
                client.finish_failure(format!("Cannot get Sōzu working directory: {error}",));
                return;
            }
        }
    }

    debug!("saving state to file {}", &path.display());
    let mut file = match File::create(&path) {
        Ok(file) => file,
        Err(error) => {
            let (verb, counter) = audit_verb!("state_saved");
            audit_emit_inline(
                server,
                client,
                EventKind::StateSaved,
                verb,
                counter,
                format!("file:{}", path.display()),
                AuditResult::Err,
                AuditExtras {
                    error_code: Some(AuditErrorCode::IoError),
                    ..Default::default()
                },
            );
            client.finish_failure(format!(
                "Cannot create file at path {}: {error}",
                path.display()
            ));
            return;
        }
    };

    match server.state.write_requests_to_file(&mut file) {
        Ok(count) => {
            let (verb, counter) = audit_verb!("state_saved");
            audit_emit_inline(
                server,
                client,
                EventKind::StateSaved,
                verb,
                counter,
                format!("file:{} messages:{count}", path.display()),
                AuditResult::Ok,
                AuditExtras::default(),
            );
            client.finish_ok(format!(
                "Saved {count} config messages to {}",
                &path.display()
            ));
        }
        Err(error) => {
            let (verb, counter) = audit_verb!("state_saved");
            audit_emit_inline(
                server,
                client,
                EventKind::StateSaved,
                verb,
                counter,
                format!("file:{}", path.display()),
                AuditResult::Err,
                AuditExtras {
                    error_code: Some(AuditErrorCode::IoError),
                    ..Default::default()
                },
            );
            client.finish_failure(format!("Failed writing state to file: {error}"));
        }
    }
}

/// change logging level on the main process, and on all workers
fn set_logging_level(server: &mut Server, client: &mut ClientSession, logging_filter: String) {
    debug!("Changing main process log level to {}", logging_filter);
    let (directives, errors) = logging::parse_logging_spec(&logging_filter);
    if !errors.is_empty() {
        let (verb, counter) = audit_verb!("logging_level_changed");
        let reason = errors
            .iter()
            .map(logging::LogSpecParseError::to_string)
            .collect::<Vec<String>>()
            .join("; ");
        audit_emit_inline(
            server,
            client,
            EventKind::LoggingLevelChanged,
            verb,
            counter,
            format!("logging:{logging_filter}"),
            AuditResult::Err,
            AuditExtras {
                error_code: Some(AuditErrorCode::InvalidInput),
                reason: Some(reason.clone()),
                ..Default::default()
            },
        );
        client.finish_failure(format!("Error parsing logging filter:\n- {reason}"));
        return;
    }
    logging::LOGGER.with(|logger| {
        logger.borrow_mut().set_directives(directives);
    });

    // also change / set the content of RUST_LOG so future workers / main thread
    // will have the new logging filter value
    // TODO: Audit that the environment access only happens in single-threaded code.
    // SAFETY: `env::set_var` in Rust 2024 is unsafe because it is not
    // thread-safe. The supervisor that handles `LoggingFilter` requests is
    // single-threaded (mio event loop on the command socket), and workers
    // are separate processes that re-read RUST_LOG after fork-and-exec —
    // so the racy read it would otherwise fight with does not exist here.
    unsafe { env::set_var("RUST_LOG", &logging_filter) };
    debug!(
        "Logging level now: {}",
        env::var("RUST_LOG").unwrap_or("could get RUST_LOG from env".to_string())
    );

    let (verb, counter) = audit_verb!("logging_level_changed");
    audit_emit_inline(
        server,
        client,
        EventKind::LoggingLevelChanged,
        verb,
        counter,
        format!("logging:{logging_filter}"),
        AuditResult::Ok,
        AuditExtras::default(),
    );

    worker_request(server, client, RequestType::Logging(logging_filter));
}

fn subscribe_client_to_events(server: &mut Server, client: &mut ClientSession) {
    info!("Subscribing client {:?} to listen to events", client.token);
    server.event_subscribers.insert(client.token);
    let (verb, counter) = audit_verb!("events_subscribed");
    audit_emit_inline(
        server,
        client,
        EventKind::EventsSubscribed,
        verb,
        counter,
        format!("subscribe:client_id:{}", client.id),
        AuditResult::Ok,
        AuditExtras::default(),
    );
}

//===============================================
// Query clusters

#[derive(Debug)]
pub struct QueryClustersTask {
    pub client_token: Token,
    pub gatherer: DefaultGatherer,
    main_process_response: Option<ResponseContent>,
}

pub fn query_clusters(
    server: &mut Server,
    client: &mut ClientSession,
    request_content: RequestType,
) {
    client.return_processing("Querying cluster...");

    server.scatter(
        request_content.clone().into(),
        Box::new(QueryClustersTask {
            client_token: client.token,
            gatherer: DefaultGatherer::default(),
            main_process_response: server.query_main(request_content.clone()),
        }),
        Timeout::Default,
        None,
    )
}

impl GatheringTask for QueryClustersTask {
    fn client_token(&self) -> Option<Token> {
        Some(self.client_token)
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        self: Box<Self>,
        _server: &mut Server,
        client: &mut OptionalClient,
        _timed_out: bool,
    ) {
        let mut worker_responses: BTreeMap<String, ResponseContent> = self
            .gatherer
            .responses
            .into_iter()
            .filter_map(|(worker_id, proxy_response)| {
                proxy_response
                    .content
                    .map(|response_content| (worker_id.to_string(), response_content))
            })
            .collect();

        if let Some(main_response) = self.main_process_response {
            worker_responses.insert(String::from("main"), main_response);
        }

        client.finish_ok_with_content(
            ContentType::WorkerResponses(WorkerResponses {
                map: worker_responses,
            })
            .into(),
            "Successfully queried clusters",
        );
    }
}

//===============================================
// Load static configuration

#[derive(Debug)]
struct LoadStaticConfigTask {
    gatherer: DefaultGatherer,
    client_token: Option<Token>,
}

pub fn load_static_config(server: &mut Server, mut client: OptionalClient, path: Option<&str>) {
    let task_id = server.new_task(
        Box::new(LoadStaticConfigTask {
            gatherer: DefaultGatherer::default(),
            client_token: client.as_ref().map(|c| c.token),
        }),
        Timeout::None,
    );

    let new_config;

    let config = match path {
        Some(path) if !path.is_empty() => {
            info!("loading static configuration at path {}", path);
            new_config = Config::load_from_path(path)
                .unwrap_or_else(|_| panic!("cannot load configuration from '{path}'"));
            &new_config
        }
        _ => {
            info!("reloading static configuration");
            &server.config
        }
    };

    client.return_processing(format!(
        "Reloading static configuration at path {}",
        config.config_path
    ));

    let audit_target = format!("config:{}", config.config_path);

    let config_messages = match config.generate_config_messages() {
        Ok(messages) => messages,
        Err(config_err) => {
            // Only attribute the audit event when a client triggered the
            // reload — at startup (`client == None`) there is no actor.
            if let Some(client_ref) = client.as_deref() {
                let (verb, counter) = audit_verb!("configuration_reloaded");
                audit_emit_inline(
                    server,
                    client_ref,
                    EventKind::ConfigurationReloaded,
                    verb,
                    counter,
                    audit_target.clone(),
                    AuditResult::Err,
                    AuditExtras::default(),
                );
            }
            client.finish_failure(format!("could not generate new config: {config_err}"));
            return;
        }
    };

    for (request_index, message) in config_messages.into_iter().enumerate() {
        let request = message.content;
        if let Err(error) = server.state.dispatch(&request) {
            client.return_processing(format!("Could not execute request on state: {error:#}"));
            continue;
        }

        if let &Some(RequestType::AddCertificate(_)) = &request.request_type {
            debug!("config generated AddCertificate( ... )");
        } else {
            debug!("config generated {:?}", request);
        }

        server.scatter_on(request, task_id, request_index, None);
    }

    if let Some(client_ref) = client.as_deref() {
        let (verb, counter) = audit_verb!("configuration_reloaded");
        audit_emit_inline(
            server,
            client_ref,
            EventKind::ConfigurationReloaded,
            verb,
            counter,
            audit_target,
            AuditResult::Ok,
            AuditExtras::default(),
        );
    }
}

impl GatheringTask for LoadStaticConfigTask {
    fn client_token(&self) -> Option<Token> {
        self.client_token
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        self: Box<Self>,
        server: &mut Server,
        client: &mut OptionalClient,
        _timed_out: bool,
    ) {
        // PRECONDITION: `load_static_config` scatters with `Timeout::None`,
        // so the task is released only once every expected worker answered.
        debug_assert!(
            self.gatherer.ok + self.gatherer.errors >= self.gatherer.expected_responses,
            "LoadStaticConfigTask::on_finish: every expected worker must have answered (no timeout)"
        );
        // Snapshot the failure tally before the loop consumes `responses`; the
        // failure-message list built below must contain exactly one entry per
        // counted error. Read only inside the post-loop assert (ungated, E0425).
        let errors_before = self.gatherer.errors;
        let mut messages = vec![];
        for (worker_id, response) in self.gatherer.responses {
            match ResponseStatus::try_from(response.status) {
                Ok(ResponseStatus::Failure) => {
                    messages.push(format!("worker {worker_id}: {}", response.message))
                }
                Ok(ResponseStatus::Ok) | Ok(ResponseStatus::Processing) => {}
                Err(e) => warn!("error decoding response status: {}", e),
            }
        }
        // INVARIANT: the gatherer's `errors` counter (incremented in
        // `on_message` for every Failure status) must match the number of
        // failure lines we just collected from the same `Failure` responses.
        // A mismatch means the counter and the response log disagree on how
        // many workers rejected the config.
        debug_assert_eq!(
            messages.len(),
            errors_before,
            "LoadStaticConfig failure-message count must equal the gatherer error tally"
        );

        if self.gatherer.errors > 0 {
            client.finish_failure(format!(
                "\nloading static configuration failed: {} OK, {} errors:\n- {}",
                self.gatherer.ok,
                self.gatherer.errors,
                messages.join("\n- ")
            ));
        } else {
            client.finish_ok(format!(
                "Successfully loaded the config: {} ok, {} errors",
                self.gatherer.ok, self.gatherer.errors,
            ));
        }

        server.update_counts();
    }
}

// =========================================================
// Audit trail (control-plane mutations)

/// Outcome of a control-plane mutation, formatted into the structured
/// audit log line.
#[derive(Clone, Copy)]
pub(crate) enum AuditResult {
    Ok,
    Err,
}

impl AuditResult {
    fn as_str(self) -> &'static str {
        match self {
            AuditResult::Ok => "ok",
            AuditResult::Err => "err",
        }
    }
}

impl std::fmt::Display for AuditResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Structured failure reason. Exists so SIEM alerts can group-by without
/// grepping free-form error strings. Paired with `result=err` in the
/// audit line; omitted for `result=ok`.
///
/// `PeerCredUnavailable` and `Other` are reserved — they fire once the
/// callers that need them land (SO_PEERCRED failure audit, catch-all
/// emit path). Suppressing dead-code warnings keeps the taxonomy stable
/// as we wire them up.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub(crate) enum AuditErrorCode {
    /// `state.dispatch` rejected the request on the main process.
    DispatchError,
    /// One or more workers returned `Failure` during fan-out.
    WorkerFailure,
    /// Fan-out timed out before every worker responded.
    WorkerTimeout,
    /// `SO_PEERCRED` returned no credentials; actor attribution missing.
    PeerCredUnavailable,
    /// Operator supplied invalid input (e.g. malformed logging filter).
    InvalidInput,
    /// I/O error on state save/load (disk full, permission denied, parse).
    IoError,
    /// Generic bucket for anything that doesn't fit the above. Prefer
    /// adding a new variant over reusing this.
    Other,
}

impl AuditErrorCode {
    fn as_str(self) -> &'static str {
        match self {
            AuditErrorCode::DispatchError => "dispatch_error",
            AuditErrorCode::WorkerFailure => "worker_failure",
            AuditErrorCode::WorkerTimeout => "worker_timeout",
            AuditErrorCode::PeerCredUnavailable => "peer_cred_unavailable",
            AuditErrorCode::InvalidInput => "invalid_input",
            AuditErrorCode::IoError => "io_error",
            AuditErrorCode::Other => "other",
        }
    }
}

impl std::fmt::Display for AuditErrorCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Worker fan-out outcome, rendered in the completion audit line.
#[derive(Debug, Clone, Copy)]
pub(crate) enum FanoutStatus {
    /// Every expected worker acknowledged with Ok.
    Ok,
    /// Some workers reported Failure; others were Ok.
    Partial,
    /// Fan-out didn't reach all workers within the deadline.
    Timeout,
    /// No workers expected (local-main-only request).
    LocalOnly,
}

impl FanoutStatus {
    fn as_str(self) -> &'static str {
        match self {
            FanoutStatus::Ok => "ok",
            FanoutStatus::Partial => "partial",
            FanoutStatus::Timeout => "timeout",
            FanoutStatus::LocalOnly => "local_only",
        }
    }
}

impl std::fmt::Display for FanoutStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Worker fan-out summary attached to completion-time audit emissions.
#[derive(Debug, Clone, Copy)]
pub(crate) struct FanoutSummary {
    status: FanoutStatus,
    workers_ok: u32,
    workers_err: u32,
    workers_expected: u32,
}

/// Optional audit-line fields populated at completion time or when a
/// failure reason is known. Defaulted to all-`None` at `AuditEntry`
/// construction so the existing build sites don't all need to set them;
/// emitters that know these values fill them in via helper constructors
/// before calling [`audit_emit`] / [`audit_emit_inline`].
#[derive(Debug, Default, Clone)]
pub(crate) struct AuditExtras {
    /// Wall-clock milliseconds between request acceptance and audit emission.
    pub(crate) elapsed_ms: Option<u64>,
    /// Structured failure reason. Set only on `AuditResult::Err` paths.
    pub(crate) error_code: Option<AuditErrorCode>,
    /// Worker fan-out outcome. Set on completion-time emissions.
    pub(crate) fanout: Option<FanoutSummary>,
    /// Short truncated failure detail — mirrors `finish_failure` message.
    pub(crate) reason: Option<String>,
    /// Truncated hex-encoded SHA-256 fingerprint of the proto `Request`
    /// bytes, for dedupe / replay detection. First 16 hex chars (64 bits).
    /// Set for verbs that flow through `worker_request`; `None` for inline
    /// verbs that don't carry a payload worth hashing.
    pub(crate) request_sha256: Option<String>,
    /// Operator-supplied `SetMetricDetail.client_id` (the lease key). Set
    /// only for the `MetricDetailChanged` audit verb. Distinct from the
    /// connection-scoped `ClientSession.id` rendered in the outer audit
    /// envelope: this one identifies the lease, that one identifies the
    /// command-socket caller. Rendered as a dedicated `lease_id=…` field
    /// so attacker-supplied `:` / `=` cannot smuggle a fake column.
    pub(crate) metric_detail_lease_id: Option<String>,
    /// Operator-supplied `SetMetricDetail.reason`. Free-form human note.
    /// Sanitised via [`sanitize_for_audit_kv`] (control bytes + `,` + `=`
    /// stripped) and truncated to [`AUDIT_REASON_MAX_CHARS`].
    pub(crate) metric_detail_reason: Option<String>,
}

/// A control-plane mutation, broken down into the pieces the audit trail
/// needs (event kind, verb name for the log, the matching counter key, and
/// the optional target identifiers populated on the emitted [Event]).
#[derive(Debug)]
struct AuditEntry {
    kind: EventKind,
    /// Stable verb tag rendered inside the audit `Command(verb=...)` block.
    /// Always a static string.
    verb: &'static str,
    /// Pre-built `config.<verb>` counter key. Stored as a `&'static str` so
    /// `count!` can route it through statsd without a verb→key dispatch
    /// table — the construction sites pair `verb` and `counter` at a single
    /// site, eliminating drift.
    counter: &'static str,
    cluster_id: Option<String>,
    backend_id: Option<String>,
    address: Option<sozu_command_lib::proto::command::SocketAddress>,
    /// Free-form target descriptor for the audit log, e.g. `"address:127.0.0.1:8080"`
    /// or `"cluster:my-cluster"`. Captures whichever identifier is meaningful
    /// for the verb.
    target: String,
    /// Optional timing / error_code / fanout / reason fields. Defaulted at
    /// construction; populated via [`AuditEntry::with_extras`] on the hot
    /// paths that care.
    extras: AuditExtras,
}

/// Truncated SHA-256 request fingerprint for dedupe / correlation.
/// Render-only helper; see [`audit_log_context!`] for inclusion.
const AUDIT_REASON_MAX_CHARS: usize = 256;

/// Hard cap on the rendered length of `lease_id` (operator-supplied
/// `SetMetricDetail.client_id`) in the audit log. The legitimate TUI
/// format is `top:<pid>:<8-hex>` ≤ 24 bytes; 64 leaves headroom for
/// other operator-side scrapers while keeping the audit line bounded.
const AUDIT_LEASE_ID_MAX_CHARS: usize = 64;

/// Compile-time sozu version tag — rendered in every audit line so
/// operators correlate which binary emitted which log during upgrades.
const SOZU_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Build-time short git SHA, embedded by `bin/build.rs`. Falls back to
/// `"unknown"` on builds outside a git tree (vendored tarballs, sysroots).
/// Together with `sozu_version` it pins which exact commit emitted a
/// given audit line — useful when a regression lands between the same
/// semver tag.
const SOZU_BUILD_GIT_SHA: &str = env!("SOZU_BUILD_GIT_SHA");

/// Render an actor role hint for SOC scanning. Rule:
/// - `uid == 0`         → `root`     (super-user, P1 alert)
/// - `1 <= uid < 1000`  → `system`   (service account, expected daemons)
/// - `uid >= 1000`      → `user`     (normal interactive operator)
/// - missing            → `unknown`  (SO_PEERCRED unavailable)
///
/// `1000` is the conventional Linux NSS uid floor for human accounts.
/// Operators on systems with a different convention (BSD, macOS) get the
/// same buckets — the labels are advisory, the raw `actor_uid` is still
/// authoritative.
pub(crate) fn actor_role(uid: Option<u32>) -> &'static str {
    match uid {
        None => "unknown",
        Some(0) => "root",
        Some(u) if u < 1000 => "system",
        Some(_) => "user",
    }
}

/// Render a `SystemTime` as an RFC 3339 / ISO 8601 timestamp at UTC
/// (`YYYY-MM-DDTHH:MM:SS.ffffffZ`). std-only — uses Howard Hinnant's
/// `civil_from_days` algorithm so we don't pull in `chrono` / `time`.
///
/// Six-digit fractional seconds (microseconds) — matches what most SIEM
/// stacks expect and avoids the precision overhead of nanoseconds.
pub(crate) fn rfc3339_utc(t: std::time::SystemTime) -> String {
    let dur = t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default();
    let secs = dur.as_secs() as i64;
    let micros = dur.subsec_micros();

    let days = secs.div_euclid(86_400);
    let sec_of_day = secs.rem_euclid(86_400) as u32;
    let hh = sec_of_day / 3600;
    let mm = (sec_of_day / 60) % 60;
    let ss = sec_of_day % 60;

    // Hinnant's civil_from_days — `days` is days since 1970-01-01.
    let z = days + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = (z - era * 146_097) as u64;
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let y = yoe as i64 + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = y + if m <= 2 { 1 } else { 0 };

    format!("{y:04}-{m:02}-{d:02}T{hh:02}:{mm:02}:{ss:02}.{micros:06}Z")
}

/// Build the [AuditEntry] for a control-plane request, or `None` for
/// non-mutating verbs (the caller skips them — they have no audit footprint).
///
/// `state` is used to snapshot the pre-change listener config for
/// `UpdateHttp/Https/TcpListener` so the audit line can show
/// `field=old→new` pairs. Passing the `ConfigState` by reference stays
/// cheap because only the UpdateListener arms look anything up.
fn audit_entry_for(
    request: &RequestType,
    state: &sozu_command_lib::state::ConfigState,
) -> Option<AuditEntry> {
    use std::net::SocketAddr;
    match request {
        RequestType::AddCluster(cluster) => {
            let (verb, counter) = audit_verb!("cluster_added");
            Some(AuditEntry {
                kind: EventKind::ClusterAdded,
                verb,
                counter,
                target: format!("cluster:{}", cluster.cluster_id),
                cluster_id: Some(cluster.cluster_id.to_owned()),
                backend_id: None,
                address: None,
                extras: AuditExtras::default(),
            })
        }
        RequestType::RemoveCluster(cluster_id) => {
            let (verb, counter) = audit_verb!("cluster_removed");
            Some(AuditEntry {
                kind: EventKind::ClusterRemoved,
                verb,
                counter,
                target: format!("cluster:{cluster_id}"),
                cluster_id: Some(cluster_id.to_owned()),
                backend_id: None,
                address: None,
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddHttpFrontend(frontend) => {
            let (verb, counter) = audit_verb!("http_frontend_added");
            Some(AuditEntry {
                kind: EventKind::FrontendAdded,
                verb,
                counter,
                target: format!("frontend:http:{}:{}", frontend.hostname, frontend.address),
                cluster_id: frontend.cluster_id.clone(),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddHttpsFrontend(frontend) => {
            let (verb, counter) = audit_verb!("https_frontend_added");
            Some(AuditEntry {
                kind: EventKind::FrontendAdded,
                verb,
                counter,
                target: format!("frontend:https:{}:{}", frontend.hostname, frontend.address),
                cluster_id: frontend.cluster_id.clone(),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddTcpFrontend(frontend) => {
            let (verb, counter) = audit_verb!("tcp_frontend_added");
            Some(AuditEntry {
                kind: EventKind::FrontendAdded,
                verb,
                counter,
                target: format!("frontend:tcp:{}:{}", frontend.cluster_id, frontend.address),
                cluster_id: Some(frontend.cluster_id.to_owned()),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::RemoveHttpFrontend(frontend) => {
            let (verb, counter) = audit_verb!("http_frontend_removed");
            Some(AuditEntry {
                kind: EventKind::FrontendRemoved,
                verb,
                counter,
                target: format!("frontend:http:{}:{}", frontend.hostname, frontend.address),
                cluster_id: frontend.cluster_id.clone(),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::RemoveHttpsFrontend(frontend) => {
            let (verb, counter) = audit_verb!("https_frontend_removed");
            Some(AuditEntry {
                kind: EventKind::FrontendRemoved,
                verb,
                counter,
                target: format!("frontend:https:{}:{}", frontend.hostname, frontend.address),
                cluster_id: frontend.cluster_id.clone(),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::RemoveTcpFrontend(frontend) => {
            let (verb, counter) = audit_verb!("tcp_frontend_removed");
            Some(AuditEntry {
                kind: EventKind::FrontendRemoved,
                verb,
                counter,
                target: format!("frontend:tcp:{}:{}", frontend.cluster_id, frontend.address),
                cluster_id: Some(frontend.cluster_id.to_owned()),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddUdpFrontend(frontend) => {
            let (verb, counter) = audit_verb!("udp_frontend_added");
            Some(AuditEntry {
                kind: EventKind::FrontendAdded,
                verb,
                counter,
                target: format!("frontend:udp:{}:{}", frontend.cluster_id, frontend.address),
                cluster_id: Some(frontend.cluster_id.to_owned()),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::RemoveUdpFrontend(frontend) => {
            let (verb, counter) = audit_verb!("udp_frontend_removed");
            Some(AuditEntry {
                kind: EventKind::FrontendRemoved,
                verb,
                counter,
                target: format!("frontend:udp:{}:{}", frontend.cluster_id, frontend.address),
                cluster_id: Some(frontend.cluster_id.to_owned()),
                backend_id: None,
                address: Some(frontend.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddCertificate(add) => {
            let (verb, counter) = audit_verb!("certificate_added");
            Some(AuditEntry {
                kind: EventKind::CertificateAdded,
                verb,
                counter,
                target: format!("certificate:{}", add.address),
                cluster_id: None,
                backend_id: None,
                address: Some(add.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::RemoveCertificate(remove) => {
            let (verb, counter) = audit_verb!("certificate_removed");
            Some(AuditEntry {
                kind: EventKind::CertificateRemoved,
                verb,
                counter,
                target: format!("certificate:{}:{}", remove.address, remove.fingerprint),
                cluster_id: None,
                backend_id: None,
                address: Some(remove.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::ReplaceCertificate(replace) => {
            let (verb, counter) = audit_verb!("certificate_replaced");
            // Compute the new cert's fingerprint from its PEM so the audit
            // trail records both the cert being removed AND the cert
            // replacing it. Forensic value: rotation pattern + detection of
            // substituted-cert attacks. Best-effort: on parse failure the
            // new fingerprint falls back to `"unknown"` rather than
            // aborting the audit emission.
            let new_fp =
                compute_certificate_fingerprint(replace.new_certificate.certificate.as_bytes())
                    .unwrap_or_else(|| "unknown".to_owned());
            Some(AuditEntry {
                kind: EventKind::CertificateReplaced,
                verb,
                counter,
                target: format!(
                    "certificate:{}:old={}:new={}",
                    replace.address, replace.old_fingerprint, new_fp
                ),
                cluster_id: None,
                backend_id: None,
                address: Some(replace.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::ActivateListener(listener) => {
            let (verb, counter) = audit_verb!("listener_activated");
            Some(AuditEntry {
                kind: EventKind::ListenerActivated,
                verb,
                counter,
                target: format!("listener:{:?}:{}", listener.proxy(), listener.address),
                cluster_id: None,
                backend_id: None,
                address: Some(listener.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::DeactivateListener(listener) => {
            let (verb, counter) = audit_verb!("listener_deactivated");
            Some(AuditEntry {
                kind: EventKind::ListenerDeactivated,
                verb,
                counter,
                target: format!("listener:{:?}:{}", listener.proxy(), listener.address),
                cluster_id: None,
                backend_id: None,
                address: Some(listener.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::UpdateHttpListener(patch) => {
            let (verb, counter) = audit_verb!("http_listener_updated");
            let current = state.http_listeners.get(&SocketAddr::from(patch.address));
            Some(AuditEntry {
                kind: EventKind::ListenerUpdated,
                verb,
                counter,
                target: format!(
                    "listener:http:{}:{}",
                    patch.address,
                    format_patch_diff_http(patch, current),
                ),
                cluster_id: None,
                backend_id: None,
                address: Some(patch.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::UpdateHttpsListener(patch) => {
            let (verb, counter) = audit_verb!("https_listener_updated");
            let current = state.https_listeners.get(&SocketAddr::from(patch.address));
            Some(AuditEntry {
                kind: EventKind::ListenerUpdated,
                verb,
                counter,
                target: format!(
                    "listener:https:{}:{}",
                    patch.address,
                    format_patch_diff_https(patch, current),
                ),
                cluster_id: None,
                backend_id: None,
                address: Some(patch.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::UpdateTcpListener(patch) => {
            let (verb, counter) = audit_verb!("tcp_listener_updated");
            let current = state.tcp_listeners.get(&SocketAddr::from(patch.address));
            Some(AuditEntry {
                kind: EventKind::ListenerUpdated,
                verb,
                counter,
                target: format!(
                    "listener:tcp:{}:{}",
                    patch.address,
                    format_patch_diff_tcp(patch, current),
                ),
                cluster_id: None,
                backend_id: None,
                address: Some(patch.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::UpdateUdpListener(patch) => {
            let (verb, counter) = audit_verb!("udp_listener_updated");
            let current = state.udp_listeners.get(&SocketAddr::from(patch.address));
            Some(AuditEntry {
                kind: EventKind::ListenerUpdated,
                verb,
                counter,
                target: format!(
                    "listener:udp:{}:{}",
                    patch.address,
                    format_patch_diff_udp(patch, current),
                ),
                cluster_id: None,
                backend_id: None,
                address: Some(patch.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddHttpListener(listener) => {
            let (verb, counter) = audit_verb!("http_listener_added");
            Some(AuditEntry {
                kind: EventKind::ListenerAdded,
                verb,
                counter,
                target: format!("listener:http:{}", listener.address),
                cluster_id: None,
                backend_id: None,
                address: Some(listener.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddHttpsListener(listener) => {
            let (verb, counter) = audit_verb!("https_listener_added");
            Some(AuditEntry {
                kind: EventKind::ListenerAdded,
                verb,
                counter,
                target: format!("listener:https:{}", listener.address),
                cluster_id: None,
                backend_id: None,
                address: Some(listener.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddTcpListener(listener) => {
            let (verb, counter) = audit_verb!("tcp_listener_added");
            Some(AuditEntry {
                kind: EventKind::ListenerAdded,
                verb,
                counter,
                target: format!("listener:tcp:{}", listener.address),
                cluster_id: None,
                backend_id: None,
                address: Some(listener.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::AddUdpListener(listener) => {
            let (verb, counter) = audit_verb!("udp_listener_added");
            Some(AuditEntry {
                kind: EventKind::ListenerAdded,
                verb,
                counter,
                target: format!("listener:udp:{}", listener.address),
                cluster_id: None,
                backend_id: None,
                address: Some(listener.address),
                extras: AuditExtras::default(),
            })
        }
        RequestType::RemoveListener(remove) => {
            let (verb, counter) = audit_verb!("listener_removed");
            Some(AuditEntry {
                kind: EventKind::ListenerRemoved,
                verb,
                counter,
                target: format!("listener:{:?}:{}", remove.proxy(), remove.address),
                cluster_id: None,
                backend_id: None,
                address: Some(remove.address),
                extras: AuditExtras::default(),
            })
        }
        // AddBackend / RemoveBackend are intentionally not audited via this
        // taxonomy — they are already covered by the BACKEND_DOWN / BACKEND_UP
        // events emitted by the workers when traffic reaches them.
        _ => None,
    }
}

/// Bump the per-verb counter, queue the [Event] for fan-out to subscribed
/// clients, and write the structured audit log line at `info!` level. Used
/// by every control-plane mutation handler.
fn audit_emit(server: &mut Server, client: &ClientSession, entry: AuditEntry, result: AuditResult) {
    // PRECONDITION: the verb tag and its counter key are non-empty. Both come
    // from `audit_verb!` string literals; an empty `counter` would silently
    // collapse every audited verb onto one metric series, and an empty `verb`
    // would render an unattributable audit line. Bumping the counter exactly
    // once per emission is the metric contract dashboards rely on.
    debug_assert!(
        !entry.counter.is_empty() && !entry.verb.is_empty(),
        "audit entry must carry a non-empty verb tag and counter key"
    );
    let request_id = Ulid::generate();
    // `entry.counter` is the pre-built `config.<verb>` static-str key
    // chosen at the construction site (paired with `verb` via
    // `audit_verb!`), so dashboards see one counter per verb without a
    // verb→key dispatch table.
    count!(entry.counter, 1);

    let rendered = audit_log_context!(server, client, &request_id, &entry, result);
    info!("{}", rendered);
    // Mirror to the dedicated tamper-resistant sink when configured
    // (`config.audit_logs_target`). Best-effort; failures fall back to the
    // standard `info!` route above. Strip ANSI escapes before writing so
    // the dedicated file stays ASCII and SIEM-parseable even when colour
    // is enabled on stdout.
    server.append_audit_line(&strip_ansi(&rendered));

    // JSON-structured sink (`config.audit_logs_json_target`) — one
    // self-contained record per line, ready for Wazuh / Elastic / Loki
    // ingest without bespoke parser code.
    if server.audit_log_json_writer.is_some() {
        let json = audit_record_to_json(server, client, &request_id, &entry, result);
        server.append_audit_json(&json);
    }

    // Subscribers only see the proto Event; the verb / actor / request_id are
    // captured in the audit log line above (which is already structured).
    server.push_audit_event(Event {
        kind: entry.kind as i32,
        cluster_id: entry.cluster_id,
        backend_id: entry.backend_id,
        address: entry.address,
        // Master-emitted audit events do not carry the
        // `metric_detail` transition; that field is populated by
        // workers via `WorkerResponse::Event` on lease-tick / worker-
        // arm transitions only. See `command.proto`'s `Event` and
        // `MetricDetailTransition` comments.
        metric_detail: None,
    });
}

/// Audit a worker-local `METRIC_DETAIL_CHANGED` transition. Workers emit
/// these via the `Event` channel when the polled lease janitor retires a
/// lease, or when the worker arm of `SetMetricDetail` applies / clears a
/// lease. The master folds them into the same audit log used for
/// operator-initiated transitions so SOC tooling sees a complete picture
/// of cardinality changes regardless of origin.
///
/// Distinct from [`audit_emit`] / [`audit_emit_inline`] because there is
/// no `ClientSession` behind the event: the actor is the worker itself.
/// The line uses a `worker_id=<id>` field in place of the
/// `actor_uid` / `actor_pid` / `client_id` block; everything else
/// (timestamps, sozu_version, fan-out to subscribers) matches the
/// canonical envelope. Both the text sink and the JSON sink receive a
/// record so SIEM ingest stays unified.
pub fn audit_worker_metric_detail_transition(
    server: &mut Server,
    worker_id: crate::command::server::WorkerId,
    transition: &sozu_command_lib::proto::command::MetricDetailTransition,
) {
    use sozu_command_lib::proto::command::MetricDetail;

    let (verb, counter) = audit_verb!("metric_detail_changed_worker_local");
    count!(counter, 1);

    let prev_label = MetricDetail::try_from(transition.previous_effective)
        .map(|m| format!("{m:?}"))
        .unwrap_or_else(|_| "<invalid>".into());
    let eff_label = MetricDetail::try_from(transition.effective)
        .map(|m| format!("{m:?}"))
        .unwrap_or_else(|_| "<invalid>".into());
    let kind_sanitized = sanitize_for_audit_kv(&transition.transition_kind);
    let target = format!("metric_detail:{prev_label}->{eff_label}");
    let now_ts = rfc3339_utc(std::time::SystemTime::now());

    // Truncate the optional lease client_id with the same cap used in the
    // operator-initiated audit line so SIEM consumers see a consistent
    // upper bound.
    let lease_id = transition.client_id.as_deref().map(|c| {
        let sanitized = sanitize_for_audit_kv(c);
        let truncated: String = sanitized.chars().take(AUDIT_LEASE_ID_MAX_CHARS).collect();
        truncated
    });
    // POSTCONDITION: the audit lease_id never exceeds the SIEM-visible cap.
    // The operator-initiated path applies the same `take(AUDIT_LEASE_ID_MAX_CHARS)`
    // bound; a worker-local line that slipped past it would give SOC tooling
    // an inconsistent upper bound across the two emission sites.
    debug_assert!(
        lease_id
            .as_deref()
            .is_none_or(|id| id.chars().count() <= AUDIT_LEASE_ID_MAX_CHARS),
        "worker-local audit lease_id must respect AUDIT_LEASE_ID_MAX_CHARS"
    );
    // The counter for this verb is bumped exactly once at entry (above).
    debug_assert!(
        !verb.is_empty() && !counter.is_empty(),
        "worker-local metric-detail audit must carry a non-empty verb/counter"
    );

    // Render the text-sink line. Match the operator-initiated envelope's
    // KV shape so a SOC analyst can correlate worker-local and operator
    // lines with a single regex. `worker_id` stands in for the
    // `client_id=<connection_id>` block since the worker is its own
    // actor.
    let mut text = format!(
        "[worker:{worker_id} request:- cluster:- backend:-]\tAUDIT\tCommand(ts={now_ts}, verb={verb}, \
         actor_uid=-, actor_gid=-, actor_pid=-, actor_role=worker, actor_user=sozu-worker, \
         actor_comm=sozu-worker, worker_id={worker_id}, socket=(worker-ipc), \
         target={target}, result=ok, transition_kind={kind_sanitized}",
    );
    if let Some(id) = lease_id.as_deref() {
        text.push_str(&format!(", lease_id={id}"));
    }
    text.push_str(&format!(
        ", sozu_version={SOZU_VERSION}, build_git_sha={SOZU_BUILD_GIT_SHA}, boot_generation={})",
        server.boot_generation,
    ));
    info!("{}", text);
    server.append_audit_line(&text);

    if server.audit_log_json_writer.is_some() {
        let mut record = serde_json::Map::new();
        record.insert("ts".to_owned(), serde_json::Value::String(now_ts.clone()));
        record.insert(
            "boot_generation".to_owned(),
            serde_json::json!(server.boot_generation),
        );
        record.insert(
            "verb".to_owned(),
            serde_json::Value::String(verb.to_owned()),
        );
        record.insert(
            "worker_id".to_owned(),
            serde_json::json!(worker_id.to_string()),
        );
        record.insert(
            "actor".to_owned(),
            serde_json::json!({
                "role": "worker",
                "comm": "sozu-worker",
            }),
        );
        record.insert(
            "target".to_owned(),
            serde_json::Value::String(target.clone()),
        );
        record.insert(
            "result".to_owned(),
            serde_json::Value::String("ok".to_owned()),
        );
        record.insert(
            "transition_kind".to_owned(),
            serde_json::Value::String(kind_sanitized.clone()),
        );
        record.insert(
            "previous_effective".to_owned(),
            serde_json::Value::String(prev_label.clone()),
        );
        record.insert(
            "effective".to_owned(),
            serde_json::Value::String(eff_label.clone()),
        );
        if let Some(id) = lease_id {
            record.insert("lease_id".to_owned(), serde_json::Value::String(id));
        }
        record.insert(
            "sozu_version".to_owned(),
            serde_json::Value::String(SOZU_VERSION.to_owned()),
        );
        record.insert(
            "build_git_sha".to_owned(),
            serde_json::Value::String(SOZU_BUILD_GIT_SHA.to_owned()),
        );
        server.append_audit_json(&serde_json::Value::Object(record).to_string());
    }
}

/// Build a single-line JSON record mirroring the audit line. Schema is
/// stable: every key always present, missing values rendered as JSON
/// `null`. Used by the dedicated JSON sink (`audit_logs_json_target`).
///
/// Schema sketch:
/// ```json
/// {
///   "ts": "<RFC3339 UTC>",
///   "boot_generation": <u32>,
///   "session_ulid": "...",
///   "request_ulid": "...",
///   "actor": {"uid": ..., "gid": ..., "pid": ..., "user": "...", "comm": "...", "role": "..."},
///   "client_id": ...,
///   "connect_ts": "<RFC3339 UTC>",
///   "socket": "...",
///   "verb": "...",
///   "target": "...",
///   "result": "ok|err",
///   "cluster_id": "..." or null,
///   "backend_id": "..." or null,
///   "extras": {...}
/// }
/// ```
fn audit_record_to_json(
    server: &Server,
    client: &ClientSession,
    request_id: &Ulid,
    entry: &AuditEntry,
    result: AuditResult,
) -> String {
    use serde_json::{Value, json};
    let extras = {
        let mut map = serde_json::Map::new();
        if let Some(code) = entry.extras.error_code {
            map.insert("error_code".to_owned(), Value::String(code.to_string()));
        }
        if let Some(reason) = entry.extras.reason.as_deref() {
            // INFO-1: every untrusted free-form field that ships to the
            // JSON sink runs through `sanitize_for_audit` to match the
            // text-sink contract. `serde_json` would JSON-escape control
            // bytes correctly, but a SIEM that re-emits JSON to TSV/CSV
            // can resurrect literal `\t` / `\n` and a SOC analyst
            // grepping the flat egress would see forged columns.
            map.insert(
                "reason".to_owned(),
                Value::String(sanitize_for_audit(reason)),
            );
        }
        if let Some(elapsed) = entry.extras.elapsed_ms {
            map.insert("elapsed_ms".to_owned(), json!(elapsed));
        }
        if let Some(fanout) = entry.extras.fanout {
            map.insert(
                "fanout".to_owned(),
                json!({
                    "status": fanout.status.to_string(),
                    "workers_ok": fanout.workers_ok,
                    "workers_err": fanout.workers_err,
                    "workers_expected": fanout.workers_expected,
                }),
            );
        }
        if let Some(hash) = entry.extras.request_sha256.as_deref() {
            map.insert("request_sha256".to_owned(), Value::String(hash.to_owned()));
        }
        if let Some(lease_id) = entry.extras.metric_detail_lease_id.as_deref() {
            // Operator-controlled. Sanitise with the strict KV helper and
            // truncate so JSON consumers that re-emit flat (TSV/CSV) cannot
            // forge an adjacent column.
            let sanitized = sanitize_for_audit_kv(lease_id);
            let truncated: String = sanitized.chars().take(AUDIT_LEASE_ID_MAX_CHARS).collect();
            map.insert("lease_id".to_owned(), Value::String(truncated));
        }
        if let Some(detail_reason) = entry.extras.metric_detail_reason.as_deref() {
            let sanitized = sanitize_for_audit_kv(detail_reason);
            let truncated: String = sanitized.chars().take(AUDIT_REASON_MAX_CHARS).collect();
            map.insert("metric_detail_reason".to_owned(), Value::String(truncated));
        }
        Value::Object(map)
    };
    // INFO-1: free-form attacker-influenced fields go through
    // `sanitize_for_audit` here even though `serde_json` would already
    // escape control bytes — defense in depth against SIEM pipelines
    // that decode JSON and re-emit flat (TSV/CSV/syslog), which
    // resurrects the literal control byte and re-opens the column-
    // smuggling primitive the text sink already defends against.
    let actor_user_sanitized = client.actor_user.as_deref().map(sanitize_for_audit);
    let actor_comm_sanitized = client.actor_comm.as_deref().map(sanitize_for_audit);
    let socket_sanitized = sanitize_for_audit(client.socket_path.as_ref());
    let target_sanitized = sanitize_for_audit(&entry.target);
    let verb_sanitized = sanitize_for_audit(entry.verb);
    let record = json!({
        "ts": rfc3339_utc(std::time::SystemTime::now()),
        "boot_generation": server.boot_generation,
        "session_ulid": client.session_ulid.to_string(),
        "request_ulid": request_id.to_string(),
        "actor": {
            "uid": client.actor_uid,
            "gid": client.actor_gid,
            "pid": client.actor_pid,
            "user": actor_user_sanitized,
            "comm": actor_comm_sanitized,
            "role": actor_role(client.actor_uid),
        },
        "client_id": client.id,
        "connect_ts": client.connect_ts_display(),
        "socket": socket_sanitized,
        "verb": verb_sanitized,
        "target": target_sanitized,
        "result": result.to_string(),
        "cluster_id": entry.cluster_id,
        "backend_id": entry.backend_id,
        "sozu_version": SOZU_VERSION,
        "build_git_sha": SOZU_BUILD_GIT_SHA,
        "extras": extras,
    });
    record.to_string()
}

/// Strip ANSI CSI escape sequences from `s`. Cheap single-pass parser —
/// recognises `\x1b[ ... m` (colour) and any other `\x1b[ ... <final>`
/// sequence. Returns `s.to_owned()` when no ESC byte is found so the common
/// no-colour path doesn't reallocate.
fn strip_ansi(s: &str) -> String {
    if !s.contains('\x1b') {
        return s.to_owned();
    }
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c != '\x1b' {
            out.push(c);
            continue;
        }
        // Swallow the `[`...final-byte CSI, or drop the lone ESC.
        if let Some('[') = chars.next() {
            for next in chars.by_ref() {
                if ('@'..='~').contains(&next) {
                    break;
                }
            }
        }
    }
    out
}

/// Same as [audit_emit] but synthesises the [AuditEntry] inline for verbs
/// whose request payload does not carry the relevant identifiers (e.g.
/// `Logging`, `ConfigureMetrics`, `ReloadConfiguration`). Caller MUST pair
/// `verb` and `counter` via `audit_verb!` so the two strings cannot drift.
#[allow(clippy::too_many_arguments)]
pub(crate) fn audit_emit_inline(
    server: &mut Server,
    client: &ClientSession,
    kind: EventKind,
    verb: &'static str,
    counter: &'static str,
    target: String,
    result: AuditResult,
    extras: AuditExtras,
) {
    audit_emit(
        server,
        client,
        AuditEntry {
            kind,
            verb,
            counter,
            target,
            cluster_id: None,
            backend_id: None,
            address: None,
            extras,
        },
        result,
    );
}

// =========================================================
// Worker request

#[derive(Debug)]
struct WorkerTask {
    pub client_token: Token,
    pub gatherer: DefaultGatherer,
    /// Wall-clock reference captured at `worker_request` entry. Used by
    /// [`WorkerTask::on_finish`] to compute `elapsed_ms` for the
    /// completion-time audit emission.
    started_at: Instant,
    /// Snapshot of the audit entry built from the request. Carried through
    /// the task so the completion-time audit line can attribute the verb
    /// and target. `None` for non-audited verbs (same filter as
    /// `audit_entry_for`).
    audit: Option<AuditEntry>,
    /// Inline-audit target for verbs whose audit factory does not produce
    /// a full `AuditEntry` (`ConfigureMetrics`, `SetMetricDetail`). The
    /// completion handler emits a second `audit_emit_inline` line with
    /// fan-out outcome attached. `None` for verbs that already carry an
    /// `AuditEntry` in `audit`.
    inline_audit: Option<InlineAuditTarget>,
    /// Operator-controlled SetMetricDetail audit fields (lease_id,
    /// reason) folded into the completion-time `AuditExtras` so the
    /// post-fanout audit row also carries the operator-supplied lease key
    /// and human note in their dedicated columns rather than smuggling
    /// them through `target`. `None` for any verb that is not
    /// `SetMetricDetail`.
    metric_detail_audit: Option<MetricDetailAuditFields>,
    /// `MetricsConfiguration::Clear` deferred-clear flag. When `true`, the
    /// completion handler wipes the master-side `METRICS` aggregator AFTER
    /// the audit row has been emitted. Done post-audit so the
    /// `count!(metrics_configured, 1)` increment driven by the audit row
    /// itself is not what the operator sees in `sozu metrics` immediately
    /// after `sozu metrics clear` — otherwise the master would be wiped,
    /// the audit would repopulate one counter, and the "wipes everything"
    /// contract would silently drift by exactly one row per clear.
    clear_master_metrics_on_finish: bool,
}

/// Carry the per-verb metadata needed to emit a completion-time audit
/// row for verbs that don't go through `audit_entry_for`. Mirrors the
/// shape `audit_emit_inline` expects (both `verb` and `counter` are
/// `&'static str` produced together by the `audit_verb!` macro so they
/// can never drift).
#[derive(Debug)]
struct InlineAuditTarget {
    kind: EventKind,
    verb: &'static str,
    counter: &'static str,
    target: String,
}

/// Captured audit fields for `SetMetricDetail` whose operator-controlled
/// values flow into dedicated audit extras (NOT into `target`) so that
/// `:` / `=` / `,` smuggled by an attacker cannot forge an adjacent
/// audit column. `target` itself is kept master-controlled
/// (`metric_detail:<level>` only).
#[derive(Debug, Clone)]
struct MetricDetailAuditFields {
    /// `metric_detail:<level>` — fully master-controlled (level is an enum).
    target: String,
    /// Operator-supplied `SetMetricDetail.client_id`. Sanitised at render
    /// time via [`sanitize_for_audit_kv`] and truncated to
    /// [`AUDIT_LEASE_ID_MAX_CHARS`].
    lease_id: String,
    /// Operator-supplied `SetMetricDetail.reason` (free-form human note).
    /// Sanitised + truncated at render time.
    reason: Option<String>,
}

impl MetricDetailAuditFields {
    /// Build an `AuditExtras` skeleton carrying the operator fields. The
    /// caller layers `elapsed_ms` / `error_code` / `reason` (failure
    /// reason — distinct from `metric_detail_reason`) on top as needed.
    fn into_extras(self) -> AuditExtras {
        AuditExtras {
            metric_detail_lease_id: Some(self.lease_id),
            metric_detail_reason: self.reason,
            ..Default::default()
        }
    }
}

pub fn worker_request(
    server: &mut Server,
    client: &mut ClientSession,
    mut request_content: RequestType,
) {
    // Master-only enrichment: populate `SetMetricDetail`'s peer binding
    // from the connecting `ClientSession` so the worker can authorise
    // subsequent `clear` requests against the apply-time owner. Clients
    // never set these fields themselves — see the proto comment on
    // `SetMetricDetail.peer_pid` / `peer_session_ulid` for the trust
    // model. A `None` actor_pid (non-Linux build, missing SO_PEERCRED)
    // degrades to "binding unknown" on the worker side, which accepts
    // any clear for backward compat.
    if let RequestType::SetMetricDetail(req) = &mut request_content {
        req.peer_pid = client.actor_pid;
        req.peer_session_ulid = Some(client.session_ulid.to_string());
        // Master-side pre-validation: reject obviously bogus inputs
        // BEFORE fan-out so a malicious or buggy caller cannot fan its
        // mistake across every worker (N rejected fan-outs + N audit
        // lines per request). The worker dispatch path still enforces
        // these limits as defence-in-depth, but failing fast here saves
        // the audit-noise amplifier and gives the operator a single
        // clear error rather than N.
        if req.client_id.len() > sozu_lib::metrics::LEASE_CLIENT_ID_MAX_BYTES {
            client.finish_failure(format!(
                "SetMetricDetail: client_id length {} exceeds {} bytes",
                req.client_id.len(),
                sozu_lib::metrics::LEASE_CLIENT_ID_MAX_BYTES,
            ));
            return;
        }
        if let Some(t) = req.ttl_seconds
            && u64::from(t) > sozu_lib::metrics::LEASE_TTL_MAX.as_secs()
        {
            client.finish_failure(format!(
                "SetMetricDetail: ttl_seconds={t} exceeds LEASE_TTL_MAX={}",
                sozu_lib::metrics::LEASE_TTL_MAX.as_secs(),
            ));
            return;
        }
    }
    // Snapshot the audit entry before consuming `request_content` so we can
    // emit even when `state.dispatch` rejects the request AND so the
    // completion handler can re-emit with fanout + elapsed_ms.
    let audit = audit_entry_for(&request_content, &server.state);
    let started_at = Instant::now();

    // Special-case ConfigureMetrics — the proto payload is an i32 enum (no
    // dedicated message), so we synthesise the audit entry inline. The
    // resolved enum value is reused below to clear the main-process METRICS
    // aggregator on `MetricsConfiguration::Clear` (workers see the clear
    // through the scatter; without this the master's own `main_metrics`
    // returned by `dump_local_proxy_metrics` would survive the operator
    // clear and `sozu metrics` would still report stale values).
    let metrics_configuration = if let RequestType::ConfigureMetrics(value) = &request_content {
        // `try_from` rejects any i32 outside the proto-known set. An
        // unknown variant lands here only if the master has been deployed
        // ahead of a worker schema bump (or a malformed IPC payload slips
        // past the dispatch whitelist). Log loudly and fall back to
        // `Disabled` — silently treating "unknown" as "disabled" would
        // mask future enum drift, e.g. a `Clear` value the master no
        // longer recognises would silently skip the master-side wipe.
        match MetricsConfiguration::try_from(*value) {
            Ok(cfg) => Some(cfg),
            Err(err) => {
                error!(
                    "ConfigureMetrics IPC carries unknown enum value {} ({:?}), \
                     falling back to MetricsConfiguration::Disabled",
                    value, err
                );
                Some(MetricsConfiguration::Disabled)
            }
        }
    } else {
        None
    };
    let metrics_target = metrics_configuration
        .as_ref()
        .map(|cfg| format!("metrics:{cfg:?}"));

    // Special-case SetMetricDetail — the same shape as ConfigureMetrics above
    // (no dedicated audit factory in `audit_entry_for`), so we synthesise the
    // entry inline against the new `EventKind::MetricDetailChanged` variant.
    //
    // The `target` field captures the level only (`metric_detail:Backend` /
    // `metric_detail:clear`); the operator-supplied `client_id` (lease key)
    // and free-form `reason` flow into dedicated audit extras
    // (`metric_detail_lease_id`, `metric_detail_reason`) so attacker-supplied
    // `:` / `=` / `,` cannot smuggle a forged column into the audit log.
    let metric_detail_audit = if let RequestType::SetMetricDetail(req) = &request_content {
        let level = if req.clear.unwrap_or(false) {
            "clear".to_owned()
        } else {
            req.detail
                .and_then(|d| MetricDetail::try_from(d).ok())
                .map(|d| format!("{d:?}"))
                .unwrap_or_else(|| "<invalid>".into())
        };
        Some(MetricDetailAuditFields {
            target: format!("metric_detail:{level}"),
            lease_id: req.client_id.clone(),
            reason: req.reason.clone().filter(|s| !s.is_empty()),
        })
    } else {
        None
    };

    // INVARIANT: a single verb resolves to at most ONE audit channel. The
    // three are derived from disjoint `RequestType` matches: a full
    // `AuditEntry` (audit_entry_for), the inline ConfigureMetrics line
    // (metrics_target), or the inline SetMetricDetail line
    // (metric_detail_audit). If two were ever populated together, the
    // dispatch/error/on_finish arms below — which are `if/else if` chains —
    // would silently drop the second, producing an un-audited mutation. The
    // `as u8` sum counts how many channels fired; it must never exceed one.
    debug_assert!(
        (audit.is_some() as u8)
            + (metrics_target.is_some() as u8)
            + (metric_detail_audit.is_some() as u8)
            <= 1,
        "a verb must map to at most one audit channel (entry / metrics / metric_detail)"
    );

    let request: sozu_command_lib::proto::command::Request = request_content.into();
    let request_sha256 = compute_request_sha256(&request);

    // Snapshot the state hash so the error path can assert the rejected
    // dispatch was a true no-op on `ConfigState` — a partially-applied
    // mutation that then errors would leave the master's persisted state
    // diverged from every worker (which never saw the fan-out). The state
    // handlers guarantee this (see `ConfigState::dispatch` postcondition),
    // and we re-check it here at the request boundary. NOTE: we deliberately
    // do NOT assert the success path *changed* the hash — many "mutating"
    // verbs (ConfigureMetrics, SetMetricDetail, SetMaxConnectionsPerIp,
    // Logging) are runtime/worker-only and `dispatch` is `Ok(())` no-op on
    // ConfigState for them (see state.rs:138). `hash_state()` is a cheap
    // per-cluster map; the snapshot is read ONLY inside the debug_assert
    // below, so it is dead code in release but must stay ungated (E0425).
    let state_hash_before = server.state.hash_state();

    if let Err(error) = server.state.dispatch(&request) {
        // INVARIANT: a rejected dispatch must not mutate persisted state.
        debug_assert_eq!(
            server.state.hash_state(),
            state_hash_before,
            "a dispatch that returns Err must leave ConfigState byte-identical (no partial apply)"
        );
        let reason = error.to_string();
        if let Some(mut entry) = audit {
            entry.extras.error_code = Some(AuditErrorCode::DispatchError);
            entry.extras.reason = Some(reason.clone());
            entry.extras.elapsed_ms = Some(elapsed_ms(started_at));
            entry.extras.request_sha256 = Some(request_sha256.clone());
            audit_emit(server, client, entry, AuditResult::Err);
        } else if let Some(target) = metrics_target {
            let (verb, counter) = audit_verb!("metrics_configured");
            audit_emit_inline(
                server,
                client,
                EventKind::MetricsConfigured,
                verb,
                counter,
                target,
                AuditResult::Err,
                AuditExtras {
                    elapsed_ms: Some(elapsed_ms(started_at)),
                    error_code: Some(AuditErrorCode::DispatchError),
                    reason: Some(reason.clone()),
                    ..Default::default()
                },
            );
        } else if let Some(fields) = metric_detail_audit.clone() {
            let (verb, counter) = audit_verb!("metric_detail_changed");
            let target = fields.target.clone();
            let mut extras = fields.into_extras();
            extras.elapsed_ms = Some(elapsed_ms(started_at));
            extras.error_code = Some(AuditErrorCode::DispatchError);
            extras.reason = Some(reason.clone());
            audit_emit_inline(
                server,
                client,
                EventKind::MetricDetailChanged,
                verb,
                counter,
                target,
                AuditResult::Err,
                extras,
            );
        }
        client.finish_failure(format!(
            "could not dispatch request on the main process state: {error}",
        ));
        return;
    }

    // Attempt-time audit — `result=ok` here only means "accepted by the
    // main process state". The completion-time line (emitted from
    // `WorkerTask::on_finish`) carries the fanout outcome.
    let audit_for_task = audit.as_ref().map(|entry| {
        let mut cloned = clone_entry(entry);
        cloned.extras.request_sha256 = Some(request_sha256.clone());
        cloned
    });
    // Stash an inline-audit target for the completion handler when the
    // verb doesn't carry a full `AuditEntry`. The attempt-time line below
    // emits with `AuditResult::Ok` (state.dispatch accepted); on_finish
    // re-emits with the worker fan-out outcome.
    let inline_audit = if let Some(target) = metrics_target.as_ref() {
        let (verb, counter) = audit_verb!("metrics_configured");
        Some(InlineAuditTarget {
            kind: EventKind::MetricsConfigured,
            verb,
            counter,
            target: target.clone(),
        })
    } else {
        metric_detail_audit.as_ref().map(|fields| {
            let (verb, counter) = audit_verb!("metric_detail_changed");
            InlineAuditTarget {
                kind: EventKind::MetricDetailChanged,
                verb,
                counter,
                target: fields.target.clone(),
            }
        })
    };
    // INVARIANT: the completion-time channel threaded into `WorkerTask`
    // mirrors the attempt-time channel exactly. `audit_for_task` carries the
    // full-entry verbs; `inline_audit` carries the ConfigureMetrics /
    // SetMetricDetail inline verbs. They are disjoint by construction (an
    // entry-bearing verb has no metrics_target / metric_detail_audit) — if
    // both were ever set, `WorkerTask::on_finish` would emit the entry arm
    // and silently drop the inline completion line.
    debug_assert!(
        !(audit_for_task.is_some() && inline_audit.is_some()),
        "WorkerTask carries at most one completion-time audit channel (entry XOR inline)"
    );
    // INVARIANT: the completion channel is present iff its attempt-time
    // source was. `audit_for_task` is `audit.as_ref().map(...)` so it is Some
    // exactly when `audit` is; losing it here would drop the completion line.
    debug_assert_eq!(
        audit_for_task.is_some(),
        audit.is_some(),
        "audit_for_task must be Some iff the attempt-time AuditEntry was Some"
    );

    // Operator-controlled SetMetricDetail fields (lease_id + reason) we
    // need to thread into both the attempt-time Ok line below and the
    // completion-time line in `on_finish`. Cloning once keeps the
    // emission sites symmetric without re-deriving from `request_content`
    // (already moved into `request: sozu_command_lib...::Request` above).
    let metric_detail_audit_completion = metric_detail_audit.clone();

    if let Some(mut entry) = audit {
        entry.extras.request_sha256 = Some(request_sha256);
        audit_emit(server, client, entry, AuditResult::Ok);
    } else if let Some(target) = metrics_target {
        let (verb, counter) = audit_verb!("metrics_configured");
        audit_emit_inline(
            server,
            client,
            EventKind::MetricsConfigured,
            verb,
            counter,
            target,
            AuditResult::Ok,
            AuditExtras::default(),
        );
    } else if let Some(fields) = metric_detail_audit {
        let (verb, counter) = audit_verb!("metric_detail_changed");
        let target = fields.target.clone();
        let extras = fields.into_extras();
        audit_emit_inline(
            server,
            client,
            EventKind::MetricDetailChanged,
            verb,
            counter,
            target,
            AuditResult::Ok,
            extras,
        );
    }

    // Master-side clear: deferred to `WorkerTask::on_finish` AFTER the
    // audit emission so the `count!(metrics_configured, 1)` driven by the
    // completion-time audit row does not immediately repopulate the
    // freshly-cleared `main_metrics`. Without this deferral, the documented
    // "wipes everything" contract drifts by exactly one row per clear and
    // `sozu metrics` snapshot taken right after `sozu metrics clear` would
    // report `config.metrics_configured = 1`.
    let clear_master_metrics_on_finish = metrics_configuration == Some(MetricsConfiguration::Clear);

    client.return_processing("Processing worker request...");

    server.scatter(
        request,
        Box::new(WorkerTask {
            client_token: client.token,
            gatherer: DefaultGatherer::default(),
            started_at,
            audit: audit_for_task,
            inline_audit,
            metric_detail_audit: metric_detail_audit_completion,
            clear_master_metrics_on_finish,
        }),
        Timeout::Default,
        None,
    )
}

impl GatheringTask for WorkerTask {
    fn client_token(&self) -> Option<Token> {
        Some(self.client_token)
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        self: Box<Self>,
        server: &mut Server,
        client: &mut OptionalClient,
        timed_out: bool,
    ) {
        // PRECONDITION: the gatherer ran to completion. Either every expected
        // worker answered (`ok + errors >= expected`, the `has_finished`
        // predicate that drives the task off the in-flight queue) or the task
        // tripped its timeout. A handler that fires with neither would be a
        // dispatcher accounting bug (a response counted twice, or the task
        // released before its workers replied). Read-only snapshots feed the
        // debug_asserts only → dead code in release, but must stay ungated.
        debug_assert!(
            timed_out
                || self.gatherer.ok + self.gatherer.errors >= self.gatherer.expected_responses,
            "WorkerTask::on_finish: must be finished (ok+errors >= expected) unless timed out"
        );
        // INVARIANT: every accounted ok/err corresponds to a stored response.
        // `on_message` pushes to `responses` for every ok/err/processing, so
        // the response log can never be shorter than the ok+err tally.
        debug_assert!(
            self.gatherer.responses.len() >= self.gatherer.ok + self.gatherer.errors,
            "responses log must hold at least one entry per accounted ok/err"
        );

        let mut messages = vec![];

        for (worker_id, response) in self.gatherer.responses {
            match ResponseStatus::try_from(response.status) {
                Ok(ResponseStatus::Ok) => messages.push(format!("{worker_id}: OK")),
                Ok(ResponseStatus::Failure) | Ok(ResponseStatus::Processing) | Err(_) => {
                    // Worker error strings are partially operator-
                    // influenced (request-derived fields, IPC payloads).
                    // Run them through the column-boundary-aware
                    // sanitiser before joining into `extras.reason` so
                    // a `,` / `=` inside one worker's message cannot
                    // forge an additional audit-row column when a SIEM
                    // splits on `, ` / `=`. The strict variant also
                    // strips the bidi class so a Trojan-Source-flavoured
                    // payload cannot visually reorder the reason field.
                    messages.push(format!(
                        "{worker_id}: {}",
                        sanitize_for_audit_kv(&response.message)
                    ))
                }
            }
        }

        let errors = self.gatherer.errors;
        let ok = self.gatherer.ok;
        let expected = self.gatherer.expected_responses;
        let result = if errors > 0 || timed_out {
            AuditResult::Err
        } else {
            AuditResult::Ok
        };

        let fanout_status = if timed_out {
            FanoutStatus::Timeout
        } else if errors > 0 {
            FanoutStatus::Partial
        } else if expected == 0 {
            FanoutStatus::LocalOnly
        } else {
            FanoutStatus::Ok
        };
        let fanout_summary = FanoutSummary {
            status: fanout_status,
            workers_ok: u32::try_from(ok).unwrap_or(u32::MAX),
            workers_err: u32::try_from(errors).unwrap_or(u32::MAX),
            workers_expected: u32::try_from(expected).unwrap_or(u32::MAX),
        };

        // INVARIANT: the audit result mirrors the fanout status. A row tagged
        // `result=ok` must never carry a Timeout/Partial fanout, and an
        // `result=err` row must never claim a clean Ok/LocalOnly fanout —
        // a SIEM correlating the two columns would otherwise see a row that
        // contradicts itself.
        debug_assert_eq!(
            matches!(result, AuditResult::Err),
            matches!(fanout_status, FanoutStatus::Timeout | FanoutStatus::Partial),
            "AuditResult and FanoutStatus must agree on success vs failure"
        );
        // INVARIANT: `LocalOnly` means no worker was scattered to, so there
        // can be no per-worker tallies. If a worker answered while the status
        // says local-only, the expected-count accounting drifted.
        debug_assert!(
            !matches!(fanout_status, FanoutStatus::LocalOnly) || (ok == 0 && errors == 0),
            "LocalOnly fanout must have zero worker ok/err tallies"
        );

        // Completion-time audit: attributes the same verb as the attempt-time
        // line but with fanout / worker counts / elapsed_ms filled in. Skip
        // when the client disconnected or the verb is not audited.
        if let (Some(client_ref), Some(mut entry)) = (client.as_deref(), self.audit) {
            entry.extras.elapsed_ms = Some(elapsed_ms(self.started_at));
            entry.extras.fanout = Some(fanout_summary);
            if matches!(result, AuditResult::Err) {
                entry.extras.error_code = Some(if timed_out {
                    AuditErrorCode::WorkerTimeout
                } else {
                    AuditErrorCode::WorkerFailure
                });
                entry.extras.reason = Some(messages.join(", "));
            }
            audit_emit(server, client_ref, entry, result);
        } else if let (Some(client_ref), Some(inline)) = (client.as_deref(), self.inline_audit) {
            // Completion-time inline audit for ConfigureMetrics +
            // SetMetricDetail. Same shape as the entry-bearing arm above
            // but routed through `audit_emit_inline` since these verbs
            // don't synthesise a full `AuditEntry` at attempt time. The
            // operator-supplied `SetMetricDetail` lease_id / reason live
            // in their own audit columns (see `MetricDetailAuditFields`);
            // pre-fill them when present, then layer the completion
            // metadata on top.
            let mut extras = self
                .metric_detail_audit
                .map(MetricDetailAuditFields::into_extras)
                .unwrap_or_default();
            extras.elapsed_ms = Some(elapsed_ms(self.started_at));
            extras.fanout = Some(fanout_summary);
            if matches!(result, AuditResult::Err) {
                extras.error_code = Some(if timed_out {
                    AuditErrorCode::WorkerTimeout
                } else {
                    AuditErrorCode::WorkerFailure
                });
                extras.reason = Some(messages.join(", "));
            }
            audit_emit_inline(
                server,
                client_ref,
                inline.kind,
                inline.verb,
                inline.counter,
                inline.target,
                result,
                extras,
            );
        }

        // Deferred master-side `MetricsConfiguration::Clear`. Runs AFTER
        // the audit emission above so the `count!(metrics_configured, 1)`
        // increment driven by that audit row is wiped here. Operators
        // running `sozu metrics clear; sozu metrics` see an empty master
        // dump, matching the PR contract.
        if self.clear_master_metrics_on_finish {
            METRICS.with(|metrics| {
                (*metrics.borrow_mut()).clear_local();
            });
        }

        if errors > 0 || timed_out {
            client.finish_failure(messages.join(", "));
        } else {
            client.finish_ok("Successfully applied request to all workers");
        }

        server.update_counts();
    }
}

/// Elapsed milliseconds since `started_at`, saturating on overflow.
fn elapsed_ms(started_at: Instant) -> u64 {
    u64::try_from(started_at.elapsed().as_millis()).unwrap_or(u64::MAX)
}

/// SHA-256 of the proto `Request` wire-encoding, hex-truncated to 16 chars
/// (64 bits) so the audit line stays greppable without blowing up line
/// width. Cheap: single prost `encode_to_vec` + one `Sha256::digest` on
/// a control-plane path (one call per worker request, not per packet).
fn compute_request_sha256(request: &sozu_command_lib::proto::command::Request) -> String {
    let bytes = request.encode_to_vec();
    let digest = Sha256::digest(&bytes);
    // INVARIANT: SHA-256 always yields 32 bytes; we render the first 8 as
    // 2-hex-digit pairs, so the audit prefix is always exactly 16 chars.
    debug_assert_eq!(digest.len(), 32, "SHA-256 digest must be 32 bytes");
    let mut hex = String::with_capacity(16);
    for byte in digest.iter().take(8) {
        use std::fmt::Write as _;
        let _ = write!(&mut hex, "{byte:02x}");
    }
    // POSTCONDITION: greppable fixed-width 16-char (64-bit) audit prefix.
    debug_assert_eq!(
        hex.len(),
        16,
        "request sha256 audit prefix must be 16 hex chars"
    );
    hex
}

/// SHA-256 fingerprint of a PEM certificate, hex-encoded and truncated to
/// 16 hex chars (64 bits) for audit brevity. `None` when the input is not
/// parseable as PEM. Mirrors the full fingerprint workflow in
/// `sozu_command_lib::certificate::calculate_fingerprint` but truncates
/// for log terseness — operators correlate via the 64-bit prefix.
fn compute_certificate_fingerprint(certificate_pem: &[u8]) -> Option<String> {
    let fp = sozu_command_lib::certificate::calculate_fingerprint(certificate_pem).ok()?;
    // INVARIANT: a SHA-256 fingerprint is 32 bytes; we render the first 8.
    debug_assert!(
        fp.len() >= 8,
        "certificate fingerprint must hold at least the 8 bytes we truncate to"
    );
    let mut hex = String::with_capacity(16);
    for byte in fp.iter().take(8) {
        use std::fmt::Write as _;
        let _ = write!(&mut hex, "{byte:02x}");
    }
    // POSTCONDITION: fixed-width 16-char (64-bit) fingerprint prefix.
    debug_assert_eq!(
        hex.len(),
        16,
        "certificate fingerprint prefix must be 16 hex chars"
    );
    Some(hex)
}

/// Shallow clone of [`AuditEntry`] so the completion handler can re-emit a
/// second line with the same taxonomy as the attempt-time line but enriched
/// with fanout + elapsed_ms. Manual implementation because `AuditEntry`
/// does not derive `Clone` by default (it owns `String`s that the
/// attempt-time line consumes by value).
fn clone_entry(entry: &AuditEntry) -> AuditEntry {
    AuditEntry {
        kind: entry.kind,
        verb: entry.verb,
        counter: entry.counter,
        cluster_id: entry.cluster_id.clone(),
        backend_id: entry.backend_id.clone(),
        address: entry.address,
        target: entry.target.clone(),
        extras: entry.extras.clone(),
    }
}

// =========================================================
// SetMetricDetail — dedicated dispatcher.
//
// Performs master-side length / TTL pre-validation that mirrors
// `worker_request`, populates the peer binding from the connecting
// `ClientSession`, emits the attempt-time audit row, and fans the
// request out to every worker via the standard scatter path. Workers
// that pre-date this verb return `WorkerResponse::error("unknown
// request type")` which folds into the standard fan-out error tally
// (`extras.fanout.workers_err`); operators see "succeeded with errors"
// rather than a dedicated capability-skip list. Production keeps
// master + workers in sync via `UpgradeMain`, so the mixed-version
// state is transient.

/// Gathers per-worker `SetMetricDetail` responses, synthesises a
/// `MetricDetailStatus` reply for the client, and audits the
/// completion alongside operator-initiated transitions. Wraps the
/// generic worker-task fields (`audit`, `inline_audit`,
/// `metric_detail_audit`) so the existing audit pipeline keeps
/// emitting the same shape it does for any other audited verb.
#[derive(Debug)]
struct SetMetricDetailTask {
    pub client_token: Token,
    pub gatherer: DefaultGatherer,
    started_at: Instant,
    /// Master-side `(configured, effective_before)` captured pre-apply
    /// so the response can carry the `previous_effective` field that
    /// `MetricDetailStatus` advertises. The master also runs an
    /// `Aggregator`; its `effective` participates in operator-visible
    /// cardinality alongside per-worker leases.
    master_configured: MetricDetail,
    master_previous_effective: MetricDetail,
    /// Completion-time inline-audit target so the post-fanout audit
    /// row carries the same `target` / verb shape as the attempt-time
    /// line. Cloned from the `inline_audit` slot the generic
    /// `worker_request` path uses.
    inline_audit: InlineAuditTarget,
    /// Operator-controlled audit fields (lease_id + reason) carried
    /// into the completion-time `AuditExtras` so the post-fan-out
    /// audit row also surfaces the lease key + free-form note in
    /// their dedicated columns.
    metric_detail_audit: MetricDetailAuditFields,
}

/// Dispatch a `SetMetricDetail` request. Performs the same length /
/// TTL pre-validation that `worker_request` does, populates the peer
/// binding from the connecting `ClientSession`, emits the attempt-time
/// audit row, and fans out unconditionally to every worker through the
/// standard scatter path.
pub fn set_metric_detail_request(
    server: &mut Server,
    client: &mut ClientSession,
    mut req: SetMetricDetail,
) {
    // Master-side enrichment + pre-validation (mirrors `worker_request`).
    req.peer_pid = client.actor_pid;
    req.peer_session_ulid = Some(client.session_ulid.to_string());
    if req.client_id.len() > sozu_lib::metrics::LEASE_CLIENT_ID_MAX_BYTES {
        client.finish_failure(format!(
            "SetMetricDetail: client_id length {} exceeds {} bytes",
            req.client_id.len(),
            sozu_lib::metrics::LEASE_CLIENT_ID_MAX_BYTES,
        ));
        return;
    }
    if let Some(t) = req.ttl_seconds
        && u64::from(t) > sozu_lib::metrics::LEASE_TTL_MAX.as_secs()
    {
        client.finish_failure(format!(
            "SetMetricDetail: ttl_seconds={t} exceeds LEASE_TTL_MAX={}",
            sozu_lib::metrics::LEASE_TTL_MAX.as_secs(),
        ));
        return;
    }

    // Capture master-side cardinality view BEFORE we touch anything.
    let (master_configured, master_previous_effective) = METRICS.with(|m| {
        let m = m.borrow();
        (
            MetricDetail::from(m.detail_configured()),
            MetricDetail::from(m.detail_effective()),
        )
    });

    // Build the audit-field skeleton used by both the attempt-time and
    // completion-time emissions. Mirrors `worker_request`.
    let level_label = if req.clear.unwrap_or(false) {
        "clear".to_owned()
    } else {
        req.detail
            .and_then(|d| MetricDetail::try_from(d).ok())
            .map(|d| format!("{d:?}"))
            .unwrap_or_else(|| "<invalid>".into())
    };
    let metric_detail_audit = MetricDetailAuditFields {
        target: format!("metric_detail:{level_label}"),
        lease_id: req.client_id.clone(),
        reason: req.reason.clone().filter(|s| !s.is_empty()),
    };

    // POSTCONDITION of the master-side pre-validation above: by the time we
    // reach fan-out, the request honours both lease bounds (the two guards
    // returned early otherwise). The worker enforces these again as
    // defence-in-depth, but a request that slipped past here would amplify a
    // bad input across every worker (N rejections + N audit lines).
    debug_assert!(
        req.client_id.len() <= sozu_lib::metrics::LEASE_CLIENT_ID_MAX_BYTES,
        "SetMetricDetail must be length-validated before fan-out"
    );
    debug_assert!(
        req.ttl_seconds
            .is_none_or(|t| u64::from(t) <= sozu_lib::metrics::LEASE_TTL_MAX.as_secs()),
        "SetMetricDetail must be TTL-validated before fan-out"
    );

    let started_at = Instant::now();
    // Snapshot the cluster-hash so we can confirm SetMetricDetail is a
    // ConfigState no-op (it is runtime-only — `dispatch` returns `Ok(())`
    // without touching persisted state; see state.rs:138). Read only inside
    // the post-dispatch assert → ungated for the release build (E0425).
    let state_hash_before = server.state.hash_state();
    let request: Request = RequestType::SetMetricDetail(req).into();

    // Attempt-time dispatch gate (mirrors `state.dispatch` in worker_request).
    if let Err(error) = server.state.dispatch(&request) {
        // INVARIANT: a rejected runtime-only dispatch leaves ConfigState
        // untouched (it never mutates it in the first place).
        debug_assert_eq!(
            server.state.hash_state(),
            state_hash_before,
            "SetMetricDetail dispatch must not mutate ConfigState"
        );
        let reason = error.to_string();
        let (verb, counter) = audit_verb!("metric_detail_changed");
        let target = metric_detail_audit.target.clone();
        let mut extras = metric_detail_audit.into_extras();
        extras.elapsed_ms = Some(elapsed_ms(started_at));
        extras.error_code = Some(AuditErrorCode::DispatchError);
        extras.reason = Some(reason.clone());
        audit_emit_inline(
            server,
            client,
            EventKind::MetricDetailChanged,
            verb,
            counter,
            target,
            AuditResult::Err,
            extras,
        );
        client.finish_failure(format!(
            "could not dispatch request on the main process state: {error}",
        ));
        return;
    }

    // INVARIANT: even on the success path, SetMetricDetail is a ConfigState
    // no-op — the runtime lease lives in each worker's `Aggregator`, never in
    // the master's persisted `ConfigState`. If the hash changed, a future
    // edit accidentally wired a runtime knob into persisted state.
    debug_assert_eq!(
        server.state.hash_state(),
        state_hash_before,
        "SetMetricDetail must not mutate ConfigState even on success"
    );

    // Attempt-time audit Ok.
    let (verb, counter) = audit_verb!("metric_detail_changed");
    {
        let target = metric_detail_audit.target.clone();
        let extras = metric_detail_audit.clone().into_extras();
        audit_emit_inline(
            server,
            client,
            EventKind::MetricDetailChanged,
            verb,
            counter,
            target,
            AuditResult::Ok,
            extras,
        );
    }

    client.return_processing("Processing SetMetricDetail...");

    let inline_audit = InlineAuditTarget {
        kind: EventKind::MetricDetailChanged,
        verb,
        counter,
        target: metric_detail_audit.target.clone(),
    };

    // Fan out unconditionally to every worker through the standard
    // scatter path. Workers that pre-date `SetMetricDetail` reply with
    // `WorkerResponse::error("unknown request type")` which folds into
    // the standard fan-out error tally; `on_finish` surfaces them via
    // the existing fanout summary rather than a dedicated skip list.
    let task = Box::new(SetMetricDetailTask {
        client_token: client.token,
        gatherer: DefaultGatherer::default(),
        started_at,
        master_configured,
        master_previous_effective,
        inline_audit,
        metric_detail_audit,
    });
    server.scatter(request, task, Timeout::Default, None);
}

impl GatheringTask for SetMetricDetailTask {
    fn client_token(&self) -> Option<Token> {
        Some(self.client_token)
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        self: Box<Self>,
        server: &mut Server,
        client: &mut OptionalClient,
        timed_out: bool,
    ) {
        // PRECONDITION: the scatter ran to completion (or timed out). Mirrors
        // the generic `WorkerTask::on_finish` finished-or-timeout guard so a
        // SetMetricDetail task released before its workers replied trips here.
        debug_assert!(
            timed_out
                || self.gatherer.ok + self.gatherer.errors >= self.gatherer.expected_responses,
            "SetMetricDetailTask::on_finish: must be finished (ok+errors >= expected) unless timed out"
        );

        // Per-worker status: each worker now returns its own
        // `WorkerMetricDetailStatus` payload via
        // `ContentType::WorkerMetricDetailStatus` in the SetMetricDetail
        // ok-with-content response (lib/src/server.rs::notify). Pull
        // each worker's actual quartet — workers hold independent
        // `Aggregator`s, so the master's view is NOT a reliable
        // stand-in for the per-worker state. Workers that returned an
        // error (e.g. peer-binding refusal) get skipped; the operator
        // sees `MetricDetailStatus.workers` populated only for the
        // ACK'd subset.
        let mut workers_map = BTreeMap::new();
        for (worker_id, response) in &self.gatherer.responses {
            if !matches!(
                ResponseStatus::try_from(response.status),
                Ok(ResponseStatus::Ok)
            ) {
                continue;
            }
            if let Some(ResponseContent {
                content_type: Some(ContentType::WorkerMetricDetailStatus(status)),
            }) = response.content.as_ref()
            {
                // `WorkerMetricDetailStatus` is `Copy` (four `i32`s +
                // one `u32`); dereferencing avoids the `clippy::clone_on_copy`
                // lint that CI's `-D warnings` rejects.
                workers_map.insert(worker_id.to_string(), *status);
            }
        }

        let master_effective = METRICS.with(|m| MetricDetail::from(m.borrow().detail_effective()));
        let status = MetricDetailStatus {
            configured: self.master_configured as i32,
            effective: master_effective as i32,
            previous_effective: self.master_previous_effective as i32,
            workers: workers_map,
        };

        // Completion-time audit row. Same shape as the generic WorkerTask
        // completion path; reuses `metric_detail_audit` for the
        // `lease_id` / `metric_detail_reason` columns and folds the
        // fan-out summary on top.
        let errors = self.gatherer.errors;
        let ok = self.gatherer.ok;
        let expected = self.gatherer.expected_responses;
        let result = if errors > 0 || timed_out {
            AuditResult::Err
        } else {
            AuditResult::Ok
        };
        let fanout_status = if timed_out {
            FanoutStatus::Timeout
        } else if errors > 0 {
            FanoutStatus::Partial
        } else if expected == 0 {
            FanoutStatus::LocalOnly
        } else {
            FanoutStatus::Ok
        };
        let fanout_summary = FanoutSummary {
            status: fanout_status,
            workers_ok: u32::try_from(ok).unwrap_or(u32::MAX),
            workers_err: u32::try_from(errors).unwrap_or(u32::MAX),
            workers_expected: u32::try_from(expected).unwrap_or(u32::MAX),
        };
        // INVARIANT: the per-worker status map holds only successfully-ACK'd
        // workers (the loop above skips non-Ok responses), so it can never be
        // larger than the OK tally. A larger map would mean a non-Ok worker
        // leaked into the operator-visible `MetricDetailStatus.workers`.
        debug_assert!(
            status.workers.len() <= ok,
            "MetricDetailStatus.workers must not exceed the OK worker tally"
        );
        // INVARIANT: audit result agrees with fanout status (same as the
        // generic WorkerTask path).
        debug_assert_eq!(
            matches!(result, AuditResult::Err),
            matches!(fanout_status, FanoutStatus::Timeout | FanoutStatus::Partial),
            "AuditResult and FanoutStatus must agree on success vs failure"
        );
        if let Some(client_ref) = client.as_deref() {
            let mut extras = self.metric_detail_audit.into_extras();
            extras.elapsed_ms = Some(elapsed_ms(self.started_at));
            extras.fanout = Some(fanout_summary);
            if matches!(result, AuditResult::Err) {
                extras.error_code = Some(if timed_out {
                    AuditErrorCode::WorkerTimeout
                } else {
                    AuditErrorCode::WorkerFailure
                });
                let mut msgs = Vec::new();
                for (worker_id, response) in &self.gatherer.responses {
                    // Same column-boundary sanitisation as
                    // `WorkerTask::on_finish` above.
                    // `SetMetricDetail` is itself the
                    // operator-controlled verb most likely to be probed
                    // for SIEM column smuggling, so this site is the
                    // higher-leverage of the two reason-join paths.
                    msgs.push(format!(
                        "{worker_id}: {}",
                        sanitize_for_audit_kv(&response.message)
                    ));
                }
                extras.reason = Some(msgs.join(", "));
            }
            audit_emit_inline(
                server,
                client_ref,
                self.inline_audit.kind,
                self.inline_audit.verb,
                self.inline_audit.counter,
                self.inline_audit.target,
                result,
                extras,
            );
        }

        client.finish_ok_with_content(
            ContentType::MetricDetailStatus(status).into(),
            if errors > 0 || timed_out {
                "SetMetricDetail completed with worker errors"
            } else {
                "Successfully applied SetMetricDetail to all workers"
            },
        );
    }
}

// =========================================================
// Query Metrics

#[derive(Debug)]
struct QueryMetricsTask {
    pub client_token: Token,
    pub gatherer: DefaultGatherer,
    options: QueryMetricsOptions,
}

fn query_metrics(server: &mut Server, client: &mut ClientSession, options: QueryMetricsOptions) {
    client.return_processing("Querrying metrics...");

    server.scatter(
        RequestType::QueryMetrics(options.clone()).into(),
        Box::new(QueryMetricsTask {
            client_token: client.token,
            gatherer: DefaultGatherer::default(),
            options,
        }),
        Timeout::Default,
        None,
    );
}

impl GatheringTask for QueryMetricsTask {
    fn client_token(&self) -> Option<Token> {
        Some(self.client_token)
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        self: Box<Self>,
        _server: &mut Server,
        client: &mut OptionalClient,
        _timed_out: bool,
    ) {
        let main_metrics =
            METRICS.with(|metrics| (*metrics.borrow_mut()).dump_local_proxy_metrics());

        if self.options.list {
            let mut summed_proxy_metrics = Vec::new();
            let mut summed_cluster_metrics = Vec::new();
            for (_, response) in self.gatherer.responses {
                if let Some(ResponseContent {
                    content_type:
                        Some(ContentType::AvailableMetrics(AvailableMetrics {
                            proxy_metrics: listed_proxy_metrics,
                            cluster_metrics: listed_cluster_metrics,
                        })),
                }) = response.content
                {
                    summed_proxy_metrics.append(&mut listed_proxy_metrics.clone());
                    summed_cluster_metrics.append(&mut listed_cluster_metrics.clone());
                }
            }
            summed_proxy_metrics.sort();
            summed_cluster_metrics.sort();
            summed_proxy_metrics.dedup();
            summed_cluster_metrics.dedup();

            return client.finish_ok_with_content(
                ContentType::AvailableMetrics(AvailableMetrics {
                    proxy_metrics: summed_proxy_metrics,
                    cluster_metrics: summed_cluster_metrics,
                })
                .into(),
                "Successfully listed available metrics",
            );
        }

        let workers_metrics = self
            .gatherer
            .responses
            .into_iter()
            .filter_map(
                |(worker_id, worker_response)| match worker_response.content {
                    Some(ResponseContent {
                        content_type: Some(ContentType::WorkerMetrics(worker_metrics)),
                    }) => Some((worker_id.to_string(), worker_metrics)),
                    _ => None,
                },
            )
            .collect();

        let mut aggregated_metrics = AggregatedMetrics {
            main: main_metrics,
            clusters: BTreeMap::new(),
            workers: workers_metrics,
            proxying: BTreeMap::new(),
        };

        // Always fold when the caller asked for merged data, regardless of
        // worker count. `merge_metrics` relocates each worker's `clusters`
        // and `proxying` into the top-level maps via `std::mem::take`; the
        // previous `> 1` guard left single-worker fleets with empty
        // top-level maps and stranded the per-worker data in `workers`,
        // which silently zeroed every CLI/TUI consumer that reads
        // `m.clusters` / `m.proxying`.
        if !self.options.workers {
            aggregated_metrics.merge_metrics();
        }

        client.finish_ok_with_content(
            ContentType::Metrics(aggregated_metrics).into(),
            "Successfully aggregated all metrics",
        );
    }
}

// =========================================================
// Load state

#[derive(Debug)]
struct LoadStateTask {
    /// this task may be called by the main process, without a client
    pub client_token: Option<Token>,
    pub gatherer: DefaultGatherer,
    path: String,
}

pub fn load_state(server: &mut Server, mut client: OptionalClient, path: &str) {
    info!("loading state at path {}", path);

    let audit_target = format!("file:{path}");

    let mut file = match File::open(path) {
        Ok(file) => file,
        Err(err) if matches!(err.kind(), ErrorKind::NotFound) => {
            if let Some(client_ref) = client.as_deref() {
                let (verb, counter) = audit_verb!("state_loaded");
                audit_emit_inline(
                    server,
                    client_ref,
                    EventKind::StateLoaded,
                    verb,
                    counter,
                    audit_target.clone(),
                    AuditResult::Err,
                    AuditExtras {
                        error_code: Some(AuditErrorCode::IoError),
                        ..Default::default()
                    },
                );
            }
            client.finish_failure(format!("Cannot find file at path {path}"));
            return;
        }
        Err(error) => {
            if let Some(client_ref) = client.as_deref() {
                let (verb, counter) = audit_verb!("state_loaded");
                audit_emit_inline(
                    server,
                    client_ref,
                    EventKind::StateLoaded,
                    verb,
                    counter,
                    audit_target.clone(),
                    AuditResult::Err,
                    AuditExtras {
                        error_code: Some(AuditErrorCode::IoError),
                        ..Default::default()
                    },
                );
            }
            client.finish_failure(format!("Cannot open file at path {path}: {error}"));
            return;
        }
    };

    client.return_processing(format!("Parsing state file from {path}..."));

    let task_id = server.new_task(
        Box::new(LoadStateTask {
            client_token: client.as_ref().map(|c| c.token),
            gatherer: DefaultGatherer::default(),
            path: path.to_owned(),
        }),
        Timeout::None,
    );

    let mut buffer = Buffer::with_capacity(200000);
    let mut scatter_request_counter = 0usize;

    let status = loop {
        let previous = buffer.available_data();

        match file.read(buffer.space()) {
            Ok(bytes_read) => buffer.fill(bytes_read),
            Err(error) => break Err(format!("Error reading the saved state file: {error}")),
        };

        if buffer.available_data() == 0 {
            trace!("load_state: empty buffer");
            break Ok(());
        }

        let mut offset = 0usize;
        match parse_several_requests::<WorkerRequest>(buffer.data()) {
            Ok((i, requests)) => {
                if !i.is_empty() {
                    debug!("load_state: could not parse {} bytes", i.len());
                    if previous == buffer.available_data() {
                        break Err("Error consuming load state message".into());
                    }
                }
                offset = buffer.data().offset(i);

                for request in requests {
                    if server.state.dispatch(&request.content).is_ok() {
                        // INVARIANT: the scatter request_id advances by
                        // exactly one per dispatched request. `scatter_on`
                        // embeds it in the per-worker request id, so a stale
                        // or repeated counter would collide two in-flight ids
                        // and silently drop a worker's response, hanging the
                        // task. Snapshot is read only inside the assert →
                        // ungated for the release (E0425) build.
                        let counter_before = scatter_request_counter;
                        scatter_request_counter += 1;
                        debug_assert_eq!(
                            scatter_request_counter,
                            counter_before + 1,
                            "load_state must advance the scatter request_id by exactly one per dispatch"
                        );
                        server.scatter_on(request.content, task_id, scatter_request_counter, None);
                    }
                }
            }
            Err(nom::Err::Incomplete(_)) => {
                if buffer.available_data() == buffer.capacity() {
                    break Err(format!(
                        "message too big, stopping parsing:\n{}",
                        buffer.data().to_hex(16)
                    ));
                }
            }
            Err(parse_error) => {
                break Err(format!("saved state parse error: {parse_error:?}"));
            }
        }
        buffer.consume(offset);
    };

    match status {
        Ok(()) => {
            client.return_processing("Applying state file...");
            // Success audit is emitted from `LoadStateTask::on_finish` once
            // every worker has acknowledged — that's where we know the final
            // ok/err split.
        }
        Err(message) => {
            if let Some(client_ref) = client.as_deref() {
                let (verb, counter) = audit_verb!("state_loaded");
                audit_emit_inline(
                    server,
                    client_ref,
                    EventKind::StateLoaded,
                    verb,
                    counter,
                    audit_target,
                    AuditResult::Err,
                    AuditExtras {
                        error_code: Some(AuditErrorCode::IoError),
                        ..Default::default()
                    },
                );
            }
            client.finish_failure(message);
            server.cancel_task(task_id);
        }
    }
}

impl GatheringTask for LoadStateTask {
    fn client_token(&self) -> Option<Token> {
        self.client_token
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        self: Box<Self>,
        server: &mut Server,
        client: &mut OptionalClient,
        _timed_out: bool,
    ) {
        let DefaultGatherer {
            ok,
            errors,
            expected_responses,
            ..
        } = self.gatherer;
        // PRECONDITION: `load_state` scatters with `Timeout::None`, so the
        // task is only released once every worker has answered — never on a
        // timeout. The ok/err tally must therefore cover the full expected
        // fan-out.
        debug_assert!(
            ok + errors >= expected_responses,
            "LoadStateTask::on_finish: every expected worker must have answered (no timeout path)"
        );
        server.update_counts();
        let result = if errors == 0 {
            AuditResult::Ok
        } else {
            AuditResult::Err
        };
        // INVARIANT: the audit result matches the error tally — an `ok:N
        // errors:0` line must be tagged Ok, any error tagged Err.
        debug_assert_eq!(
            matches!(result, AuditResult::Ok),
            errors == 0,
            "LoadStateTask audit result must agree with the worker error tally"
        );
        if let Some(client_ref) = client.as_deref() {
            let (verb, counter) = audit_verb!("state_loaded");
            audit_emit_inline(
                server,
                client_ref,
                EventKind::StateLoaded,
                verb,
                counter,
                format!("file:{} ok:{ok} errors:{errors}", self.path),
                result,
                AuditExtras::default(),
            );
        }
        if errors == 0 {
            client.finish_ok(format!(
                "Successfully loaded state from path {}, {} ok messages, {} errors",
                self.path, ok, errors
            ));
            return;
        }
        client.finish_failure(format!("loading state: {ok} ok messages, {errors} errors"));
    }
}

// ==========================================================
// status

#[derive(Debug)]
struct StatusTask {
    pub client_token: Token,
    pub gatherer: DefaultGatherer,
    worker_infos: HashMap<WorkerId, WorkerInfo>,
}

fn status(server: &mut Server, client: &mut ClientSession) {
    client.return_processing("Querying status of workers...");

    let worker_infos = server
        .workers
        .values()
        .map(|worker| (worker.id, worker.querying_info()))
        .collect();

    server.scatter(
        RequestType::Status(Status {}).into(),
        Box::new(StatusTask {
            client_token: client.token,
            gatherer: DefaultGatherer::default(),
            worker_infos,
        }),
        Timeout::Default,
        None,
    );
}

impl GatheringTask for StatusTask {
    fn client_token(&self) -> Option<Token> {
        Some(self.client_token)
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        mut self: Box<Self>,
        _server: &mut Server,
        client: &mut OptionalClient,
        _timed_out: bool,
    ) {
        for (worker_id, response) in self.gatherer.responses {
            let new_run_state = match ResponseStatus::try_from(response.status) {
                Ok(ResponseStatus::Ok) => RunState::Running,
                Ok(ResponseStatus::Processing) => continue,
                Ok(ResponseStatus::Failure) => RunState::NotAnswering,
                Err(e) => {
                    warn!("error decoding response status: {}", e);
                    continue;
                }
            };

            self.worker_infos
                .entry(worker_id)
                .and_modify(|worker_info| worker_info.run_state = new_run_state as i32);
        }

        let worker_info_vec = WorkerInfos {
            vec: self.worker_infos.into_values().collect(),
        };

        client.finish_ok_with_content(
            ContentType::Workers(worker_info_vec).into(),
            "Successfully collected the status of workers",
        );
    }
}

// ==========================================================
// Soft stop and hard stop

#[derive(Debug)]
struct StopTask {
    pub client_token: Token,
    pub gatherer: DefaultGatherer,
    pub hardness: bool,
}

/// stop the main process and workers, true for hard stop
fn stop(server: &mut Server, client: &mut ClientSession, hardness: bool) {
    let (verb, counter) = audit_verb!("sozu_stop_requested");
    audit_emit_inline(
        server,
        client,
        EventKind::SozuStopRequested,
        verb,
        counter,
        format!("stop:{}", if hardness { "hard" } else { "soft" }),
        AuditResult::Ok,
        AuditExtras::default(),
    );

    let task = Box::new(StopTask {
        client_token: client.token,
        gatherer: DefaultGatherer::default(),
        hardness,
    });

    server.run_state = ServerState::WorkersStopping;
    // POSTCONDITION: the stop request has opened the shutdown sequence. The
    // matching `StopTask::on_finish` will later advance to `Stopping`. We do
    // NOT assert the prior state was `Running` — an operator may legitimately
    // re-issue stop while a soft-stop is already draining.
    debug_assert_eq!(
        server.run_state,
        ServerState::WorkersStopping,
        "stop() must move the master into WorkersStopping before fan-out"
    );
    if hardness {
        client.return_processing("Performing hard stop...");
        server.scatter(
            RequestType::HardStop(HardStop {}).into(),
            task,
            Timeout::Default,
            None,
        );
    } else {
        client.return_processing("Performing soft stop...");
        server.scatter(
            RequestType::SoftStop(SoftStop {}).into(),
            task,
            Timeout::None,
            None,
        );
    }
}

impl GatheringTask for StopTask {
    fn client_token(&self) -> Option<Token> {
        Some(self.client_token)
    }

    fn get_gatherer(&mut self) -> &mut dyn Gatherer {
        &mut self.gatherer
    }

    fn on_finish(
        self: Box<Self>,
        server: &mut Server,
        client: &mut OptionalClient,
        timed_out: bool,
    ) {
        // PRECONDITION: a StopTask is only created by `stop()`, which moves
        // the master to `WorkersStopping` BEFORE scattering. The server must
        // therefore never still be `Running` when a stop task finishes —
        // that would mean the run-state transition that brackets shutdown
        // was skipped. (`Stopping` is also acceptable: a prior stop task may
        // already have advanced it.)
        debug_assert_ne!(
            server.run_state,
            ServerState::Running,
            "StopTask::on_finish must observe a shutdown run-state, never Running"
        );
        if timed_out && self.hardness {
            client.finish_failure(format!(
                "Workers take too long to stop ({} ok, {} errors), stopping the main process to sever the link",
                self.gatherer.ok, self.gatherer.errors
            ));
        }
        server.run_state = ServerState::Stopping;
        // POSTCONDITION: shutdown is now committed.
        debug_assert_eq!(
            server.run_state,
            ServerState::Stopping,
            "StopTask::on_finish must leave the master in the Stopping state"
        );
        client.finish_ok(format!(
            "Successfully closed {} workers, {} errors, stopping the main process...",
            self.gatherer.ok, self.gatherer.errors
        ));
    }
}

// =========================================================
// Patch diff formatters — for the audit `target=` field.
// Walk each Option field of the patch and, when a pre-patch listener
// snapshot is available, emit `field=old→new` so operators see both the
// prior and the replacement value. Falls back to `field=new` (no arrow)
// when no current listener is known (e.g. patch arriving before the
// listener is registered) so the audit line never swallows a change.
//
// Helpers live inline as macros instead of functions so `stringify!($field)`
// picks up each field name without runtime formatting.

fn format_patch_diff_http(
    p: &UpdateHttpListenerConfig,
    current: Option<&sozu_command_lib::proto::command::HttpListenerConfig>,
) -> String {
    let mut parts: Vec<String> = Vec::new();
    // Patch field is `Option<T>`, current field is `T` (required on the
    // stored listener): `to_string()` directly.
    macro_rules! diff_req_copy {
        ($field:ident) => {
            if let Some(v) = p.$field {
                let old = current
                    .map(|c| c.$field.to_string())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    macro_rules! diff_req_str {
        ($field:ident) => {
            if let Some(v) = p.$field.as_deref() {
                let old = current
                    .map(|c| c.$field.clone())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    // Patch field is `Option<T>`, current field is `Option<T>` (optional on
    // the stored listener): flatten current via `and_then`.
    macro_rules! diff_opt_copy {
        ($field:ident) => {
            if let Some(v) = p.$field {
                let old = current
                    .and_then(|c| c.$field)
                    .map(|o| o.to_string())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    macro_rules! diff_opt_str {
        ($field:ident) => {
            if let Some(v) = p.$field.as_deref() {
                let old = current
                    .and_then(|c| c.$field.clone())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    if let Some(v) = p.public_address.as_ref() {
        let old = current
            .and_then(|c| c.public_address)
            .map(|o| o.to_string())
            .unwrap_or_else(|| "?".to_owned());
        parts.push(format!("public_address={old}→{v}"));
    }
    diff_req_copy!(expect_proxy);
    diff_req_str!(sticky_name);
    diff_req_copy!(front_timeout);
    diff_req_copy!(back_timeout);
    diff_req_copy!(connect_timeout);
    diff_req_copy!(request_timeout);
    if p.http_answers.is_some() {
        parts.push("http_answers=<patched>".to_owned());
    }
    diff_opt_copy!(h2_max_rst_stream_per_window);
    diff_opt_copy!(h2_max_ping_per_window);
    diff_opt_copy!(h2_max_settings_per_window);
    diff_opt_copy!(h2_max_empty_data_per_window);
    diff_opt_copy!(h2_max_continuation_frames);
    diff_opt_copy!(h2_max_glitch_count);
    diff_opt_copy!(h2_initial_connection_window);
    diff_opt_copy!(h2_max_concurrent_streams);
    diff_opt_copy!(h2_stream_shrink_ratio);
    diff_opt_copy!(h2_max_rst_stream_lifetime);
    diff_opt_copy!(h2_max_rst_stream_abusive_lifetime);
    diff_opt_copy!(h2_max_rst_stream_emitted_lifetime);
    diff_opt_copy!(h2_max_header_list_size);
    diff_opt_copy!(h2_max_header_table_size);
    diff_opt_copy!(h2_max_header_fields);
    diff_opt_copy!(h2_stream_idle_timeout_seconds);
    diff_opt_copy!(h2_graceful_shutdown_deadline_seconds);
    diff_opt_copy!(h2_max_window_update_stream0_per_window);
    diff_opt_str!(sozu_id_header);
    if parts.is_empty() {
        "(no-op)".to_owned()
    } else {
        parts.join(" ")
    }
}

fn format_patch_diff_https(
    p: &UpdateHttpsListenerConfig,
    current: Option<&sozu_command_lib::proto::command::HttpsListenerConfig>,
) -> String {
    let mut parts: Vec<String> = Vec::new();
    macro_rules! diff_req_copy {
        ($field:ident) => {
            if let Some(v) = p.$field {
                let old = current
                    .map(|c| c.$field.to_string())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    macro_rules! diff_req_str {
        ($field:ident) => {
            if let Some(v) = p.$field.as_deref() {
                let old = current
                    .map(|c| c.$field.clone())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    macro_rules! diff_opt_copy {
        ($field:ident) => {
            if let Some(v) = p.$field {
                let old = current
                    .and_then(|c| c.$field)
                    .map(|o| o.to_string())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    macro_rules! diff_opt_str {
        ($field:ident) => {
            if let Some(v) = p.$field.as_deref() {
                let old = current
                    .and_then(|c| c.$field.clone())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    if let Some(v) = p.public_address.as_ref() {
        let old = current
            .and_then(|c| c.public_address)
            .map(|o| o.to_string())
            .unwrap_or_else(|| "?".to_owned());
        parts.push(format!("public_address={old}→{v}"));
    }
    diff_req_copy!(expect_proxy);
    diff_req_str!(sticky_name);
    diff_req_copy!(front_timeout);
    diff_req_copy!(back_timeout);
    diff_req_copy!(connect_timeout);
    diff_req_copy!(request_timeout);
    if p.http_answers.is_some() {
        parts.push("http_answers=<patched>".to_owned());
    }
    if let Some(ref alpn) = p.alpn_protocols {
        let old = current
            .map(|c| c.alpn_protocols.join(","))
            .unwrap_or_else(|| "?".to_owned());
        let new = if alpn.values.is_empty() {
            "<reset>".to_owned()
        } else {
            alpn.values.join(",")
        };
        parts.push(format!("alpn_protocols={old}→{new}"));
    }
    diff_opt_copy!(strict_sni_binding);
    diff_opt_copy!(disable_http11);
    diff_opt_copy!(h2_max_rst_stream_per_window);
    diff_opt_copy!(h2_max_ping_per_window);
    diff_opt_copy!(h2_max_settings_per_window);
    diff_opt_copy!(h2_max_empty_data_per_window);
    diff_opt_copy!(h2_max_continuation_frames);
    diff_opt_copy!(h2_max_glitch_count);
    diff_opt_copy!(h2_initial_connection_window);
    diff_opt_copy!(h2_max_concurrent_streams);
    diff_opt_copy!(h2_stream_shrink_ratio);
    diff_opt_copy!(h2_max_rst_stream_lifetime);
    diff_opt_copy!(h2_max_rst_stream_abusive_lifetime);
    diff_opt_copy!(h2_max_rst_stream_emitted_lifetime);
    diff_opt_copy!(h2_max_header_list_size);
    diff_opt_copy!(h2_max_header_table_size);
    diff_opt_copy!(h2_max_header_fields);
    diff_opt_copy!(h2_stream_idle_timeout_seconds);
    diff_opt_copy!(h2_graceful_shutdown_deadline_seconds);
    diff_opt_copy!(h2_max_window_update_stream0_per_window);
    diff_opt_str!(sozu_id_header);
    if parts.is_empty() {
        "(no-op)".to_owned()
    } else {
        parts.join(" ")
    }
}

fn format_patch_diff_tcp(
    p: &UpdateTcpListenerConfig,
    current: Option<&sozu_command_lib::proto::command::TcpListenerConfig>,
) -> String {
    let mut parts: Vec<String> = Vec::new();
    macro_rules! diff_req_copy {
        ($field:ident) => {
            if let Some(v) = p.$field {
                let old = current
                    .map(|c| c.$field.to_string())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    if let Some(v) = p.public_address.as_ref() {
        let old = current
            .and_then(|c| c.public_address)
            .map(|o| o.to_string())
            .unwrap_or_else(|| "?".to_owned());
        parts.push(format!("public_address={old}→{v}"));
    }
    diff_req_copy!(expect_proxy);
    diff_req_copy!(front_timeout);
    diff_req_copy!(back_timeout);
    diff_req_copy!(connect_timeout);
    if parts.is_empty() {
        "(no-op)".to_owned()
    } else {
        parts.join(" ")
    }
}

/// Render a UDP listener patch as a compact `field=old→new` diff for the audit
/// trail, mirroring [`format_patch_diff_tcp`]. UDP has no `expect_proxy` /
/// `connect_timeout`; its distinguishing knobs are `max_rx_datagram_size` and
/// `max_flows`.
fn format_patch_diff_udp(
    p: &UpdateUdpListenerConfig,
    current: Option<&sozu_command_lib::proto::command::UdpListenerConfig>,
) -> String {
    let mut parts: Vec<String> = Vec::new();
    macro_rules! diff_req_copy {
        ($field:ident) => {
            if let Some(v) = p.$field {
                let old = current
                    .map(|c| c.$field.to_string())
                    .unwrap_or_else(|| "?".to_owned());
                parts.push(format!("{}={old}→{v}", stringify!($field)));
            }
        };
    }
    if let Some(v) = p.public_address.as_ref() {
        let old = current
            .and_then(|c| c.public_address)
            .map(|o| o.to_string())
            .unwrap_or_else(|| "?".to_owned());
        parts.push(format!("public_address={old}→{v}"));
    }
    diff_req_copy!(front_timeout);
    diff_req_copy!(back_timeout);
    diff_req_copy!(max_rx_datagram_size);
    diff_req_copy!(max_flows);
    if parts.is_empty() {
        "(no-op)".to_owned()
    } else {
        parts.join(" ")
    }
}

#[cfg(test)]
mod audit_format_tests {
    //! Drift-guard for the audit log line. Rejects any accidental change to
    //! the MUX `Session(...)` layout that would break downstream grep-based
    //! consumers (SIEM pipelines, operator shell recipes).
    //!
    //! The default thread-local logger reports `is_logger_colored() == false`
    //! (see `command/src/logging/logs.rs:30`), so `ansi_palette()` returns
    //! empty strings and the rendered line is ANSI-free — stable to match
    //! with a plain regex.
    use super::{
        AUDIT_LEASE_ID_MAX_CHARS, AUDIT_REASON_MAX_CHARS, AuditEntry, AuditErrorCode, AuditExtras,
        AuditResult, FanoutStatus, FanoutSummary, SOZU_BUILD_GIT_SHA, SOZU_VERSION, actor_role,
        rfc3339_utc,
    };
    use regex::Regex;
    use rusty_ulid::Ulid;
    use sozu_command_lib::proto::command::EventKind;
    use std::time::SystemTime;

    /// Minimal stand-in exposing only the `ClientSession` fields and methods
    /// that `audit_log_context!` reads. Avoids constructing the full
    /// `Channel<Response, Request>` that `ClientSession::new` requires.
    struct TestClient {
        session_ulid: Ulid,
        id: u32,
        actor_uid: Option<u32>,
        actor_gid: Option<u32>,
        actor_pid: Option<i32>,
        actor_comm: Option<String>,
        actor_user: Option<String>,
        socket_path: std::sync::Arc<str>,
        connect_ts: SystemTime,
    }

    /// Minimal stand-in for `Server` exposing only `boot_generation`, the
    /// only field the macro reads off of `$server`. Avoids the full Server
    /// (Poll, listener, workers map, …) ceremony.
    struct TestServer {
        boot_generation: u32,
    }

    impl TestClient {
        fn actor_uid_display(&self) -> String {
            self.actor_uid
                .map(|v| v.to_string())
                .unwrap_or_else(|| "unknown".to_owned())
        }
        fn actor_gid_display(&self) -> String {
            self.actor_gid
                .map(|v| v.to_string())
                .unwrap_or_else(|| "unknown".to_owned())
        }
        fn actor_pid_display(&self) -> String {
            self.actor_pid
                .map(|v| v.to_string())
                .unwrap_or_else(|| "unknown".to_owned())
        }
        fn actor_comm_display(&self) -> String {
            self.actor_comm
                .clone()
                .unwrap_or_else(|| "unknown".to_owned())
        }
        fn actor_user_display(&self) -> String {
            self.actor_user
                .clone()
                .unwrap_or_else(|| "unknown".to_owned())
        }
        fn connect_ts_display(&self) -> String {
            rfc3339_utc(self.connect_ts)
        }
    }

    fn sample_entry(cluster_id: Option<&str>) -> AuditEntry {
        AuditEntry {
            kind: EventKind::ClusterAdded,
            verb: "cluster_added",
            counter: "config.cluster_added",
            cluster_id: cluster_id.map(str::to_owned),
            backend_id: None,
            address: None,
            target: "cluster:my_app".to_owned(),
            extras: AuditExtras::default(),
        }
    }

    fn sample_client(uid: Option<u32>) -> TestClient {
        TestClient {
            session_ulid: Ulid::generate(),
            id: 42,
            actor_uid: uid,
            actor_gid: uid,
            actor_pid: uid.map(|v| v as i32),
            actor_comm: uid.map(|_| "sozu".to_owned()),
            actor_user: uid.map(|_| "florentin".to_owned()),
            socket_path: std::sync::Arc::from("/run/sozu/sock"),
            connect_ts: SystemTime::now(),
        }
    }

    fn sample_server(boot_generation: u32) -> TestServer {
        TestServer { boot_generation }
    }

    fn pattern() -> Regex {
        // Anchored; covers bracket + `AUDIT\tCommand(...)` + every mandatory
        // field in order. Optional fields (error_code, reason, elapsed_ms,
        // fanout, workers, request_sha256) may appear between `result=...`
        // and `sozu_version=...`. Crockford base32 ULIDs are 26 chars
        // over [0-9A-Z]. Timestamps are RFC 3339 UTC with microsecond
        // precision; build_git_sha is 12 hex chars or `unknown`.
        Regex::new(concat!(
            r"^\[[0-9A-Z]{26} [0-9A-Z]{26} (?:[A-Za-z0-9_:.-]+|-) (?:[A-Za-z0-9_:.-]+|-)\]",
            r"\tAUDIT\tCommand\(",
            r"ts=\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z, ",
            r"verb=[a-z_]+, ",
            r"actor_uid=(?:\d+|unknown), ",
            r"actor_gid=(?:\d+|unknown), ",
            r"actor_pid=(?:\d+|unknown), ",
            r"actor_role=(?:root|system|user|unknown), ",
            r"actor_user=\S+, ",
            r"actor_comm=\S+, ",
            r"client_id=\d+, ",
            r"connect_ts=\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z, ",
            r"socket=\S+, ",
            r"target=[^,]+, ",
            r"result=(?:ok|err)",
            // Optional extras block.
            r"(?:, (?:error_code=\S+|reason=[^,)]+|elapsed_ms=\d+|fanout=\S+|workers=\d+/\d+/\d+|request_sha256=[0-9a-f]+))*",
            r", sozu_version=[^,]+",
            r", build_git_sha=\S+",
            r", boot_generation=\d+",
            r"\)$",
        ))
        .expect("audit-format regex must compile")
    }

    #[test]
    fn layout_with_cluster_id_matches() {
        let server = sample_server(0);
        let client = sample_client(Some(1000));
        let request_id = Ulid::generate();
        let entry = sample_entry(Some("my_app"));
        let rendered = audit_log_context!(server, client, &request_id, &entry, AuditResult::Ok);
        assert!(
            pattern().is_match(&rendered),
            "rendered line did not match audit-log pattern.\nrendered: {rendered:?}"
        );
    }

    #[test]
    fn layout_with_dashed_cluster_and_backend_matches() {
        let server = sample_server(3);
        let client = sample_client(None);
        let request_id = Ulid::generate();
        let entry = sample_entry(None);
        let rendered = audit_log_context!(server, client, &request_id, &entry, AuditResult::Err);
        assert!(
            pattern().is_match(&rendered),
            "rendered line did not match audit-log pattern.\nrendered: {rendered:?}"
        );
    }

    #[test]
    fn layout_with_extras_matches() {
        let server = sample_server(0);
        let client = sample_client(Some(42));
        let request_id = Ulid::generate();
        let mut entry = sample_entry(Some("my_app"));
        entry.extras.elapsed_ms = Some(17);
        entry.extras.error_code = Some(AuditErrorCode::WorkerFailure);
        entry.extras.reason = Some("worker 1: failed".to_owned());
        entry.extras.fanout = Some(FanoutSummary {
            status: FanoutStatus::Partial,
            workers_ok: 1,
            workers_err: 1,
            workers_expected: 2,
        });
        let rendered = audit_log_context!(server, client, &request_id, &entry, AuditResult::Err);
        assert!(
            pattern().is_match(&rendered),
            "rendered line with extras did not match.\nrendered: {rendered:?}"
        );
    }

    #[test]
    fn sanitizer_strips_tab_and_escape_in_target() {
        let server = sample_server(0);
        let client = sample_client(Some(0));
        let request_id = Ulid::generate();
        let mut entry = sample_entry(None);
        entry.target = "cluster:\tforge\x1b[Kghost".to_owned();
        let rendered = audit_log_context!(server, client, &request_id, &entry, AuditResult::Ok);
        assert!(
            !rendered.contains('\t') || rendered.matches('\t').count() == 2,
            "target field must not introduce additional tabs (only the three structural tabs allowed): {rendered:?}"
        );
        assert!(
            !rendered.contains('\x1b'),
            "target field must not carry ANSI escape codes: {rendered:?}"
        );
    }

    #[test]
    fn actor_role_buckets() {
        assert_eq!(actor_role(None), "unknown");
        assert_eq!(actor_role(Some(0)), "root");
        assert_eq!(actor_role(Some(99)), "system");
        assert_eq!(actor_role(Some(999)), "system");
        assert_eq!(actor_role(Some(1000)), "user");
        assert_eq!(actor_role(Some(65534)), "user");
    }

    #[test]
    fn rfc3339_utc_round_numbers() {
        // Unix epoch.
        assert_eq!(
            rfc3339_utc(SystemTime::UNIX_EPOCH),
            "1970-01-01T00:00:00.000000Z"
        );
        // Y2K + 1 day, with microseconds.
        let t = SystemTime::UNIX_EPOCH + std::time::Duration::new(946_771_200, 123_456_000);
        assert_eq!(rfc3339_utc(t), "2000-01-02T00:00:00.123456Z");
    }

    #[test]
    fn build_git_sha_format() {
        // Either 12 hex chars (set by build.rs) or the literal "unknown"
        // fallback for builds outside a git tree.
        let s = SOZU_BUILD_GIT_SHA;
        assert!(
            s == "unknown" || (s.len() == 12 && s.chars().all(|c| c.is_ascii_hexdigit())),
            "unexpected SOZU_BUILD_GIT_SHA: {s:?}"
        );
    }

    #[test]
    fn worker_message_join_sanitises_smuggled_kv_pair() {
        // Both `WorkerTask::on_finish` and `SetMetricDetailTask::on_finish`
        // route each worker's `response.message` through
        // `sanitize_for_audit_kv` before joining into `extras.reason`.
        // Verify the call-site shape catches the canonical SIEM-column-
        // smuggling attempt — `,` and `=` inside the operator-influenced
        // worker payload — without relying on a full Server / Gatherer
        // ceremony to drive the on_finish path end-to-end.
        let worker_id = 7u32;
        let attacker_payload = "x,actor_user=mallory,sozu_version=hijacked";
        let formatted = format!(
            "{worker_id}: {}",
            super::sanitize_for_audit_kv(attacker_payload)
        );
        assert!(
            !formatted.contains("actor_user=mallory"),
            "sanitised worker message must not propagate `=` into the \
             reason column (column-boundary forge defence)"
        );
        assert!(
            !formatted.contains(",actor_user"),
            "sanitised worker message must not propagate `,` into the \
             reason column (column-boundary forge defence)"
        );
        // The replacement character does survive — operators still see
        // SOMETHING in the slot so the failure mode is visible.
        assert!(formatted.contains('?'));
    }
}

#[cfg(test)]
mod mutating_verb_policy_tests {
    //! Regression guard for the systemd `RELOADING=1` bracket policy:
    //! `is_mutating_verb` must NOT include `SetMetricDetail`. The TUI
    //! auto-renews its cardinality lease every `ttl/2` seconds, and a
    //! mutating-verb bracket on each renewal would flap the systemd
    //! unit state every 30 s for the whole TUI session lifetime.
    use super::is_mutating_verb;
    use sozu_command_lib::proto::command::{MetricDetail, SetMetricDetail, request::RequestType};

    #[test]
    fn set_metric_detail_is_not_mutating() {
        let req = RequestType::SetMetricDetail(SetMetricDetail {
            client_id: "top:1:abcdef01".to_owned(),
            detail: Some(MetricDetail::DetailBackend as i32),
            ttl_seconds: Some(60),
            reason: Some("operator dashboard".to_owned()),
            clear: Some(false),
            peer_pid: None,
            peer_session_ulid: None,
        });
        assert!(
            !is_mutating_verb(&req),
            "SetMetricDetail is an observability knob, not a state transition; \
             keeping it out of is_mutating_verb prevents RELOADING flap on lease renewal"
        );
    }
}