rsigma 0.24.0

CLI for parsing, validating, linting and evaluating Sigma detection rules
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
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
use std::time::Instant;

use arc_swap::ArcSwap;
use axum::extract::{Query, State};
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use axum::routing::{delete, get, post};
use axum::{Json, Router};
use rsigma_eval::{
    CorrelationConfig, MatchDetailLevel, OnUnknown, Pipeline, ProcessResult, RoutingPlan,
    load_schema_config,
};
use rsigma_runtime::{
    AckToken, AlertPipeline, AlertPipelineState, BatchProcessOutcome, CaptureRing, CaptureRingSink,
    DeliveryConfig, DeliveryFailure, Dispatcher, EnrichmentPipeline, FieldObserver, FileSink,
    FormattedIncidentEnvelope, IncidentEnvelope, IncidentResult, IncludeMode, InputFormat,
    LogProcessor, MetricsHook, OnFull, RawEvent, RiskLayer, RiskState, RoutingSpec, RuntimeEngine,
    SchemaClassifier, SchemaObserver, Silence, SilenceOrigin, SilenceSpec, Sink, SinkFormat,
    StdinSource, StdoutSink, build_alert_pipeline, build_risk_layer, load_alert_pipeline_file,
    load_risk_file, load_schema_signatures, spawn_source, strip_event_payloads,
};

use super::bundle::BundleFormat;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use tower_http::trace::TraceLayer;
#[cfg(feature = "daemon-otlp")]
use tracing::Instrument;

/// A dead-letter queue entry for events that fail processing.
#[derive(Serialize)]
struct DlqEntry {
    original_event: String,
    error: String,
    timestamp: String,
}

/// One evaluated batch waiting for ordered sink/ack dispatch.
struct EvaluatedBatch {
    outcome: BatchProcessOutcome,
    payloads: Vec<String>,
    tokens: Vec<AckToken>,
    pipeline_start: Instant,
}

/// Shared event filter for concurrent detection workers (`spawn_blocking`).
type SharedEventFilter = Arc<dyn Fn(&serde_json::Value) -> Vec<serde_json::Value> + Send + Sync>;

use super::health::HealthState;
use super::listen::ListenAddr;
use super::metrics::Metrics;
use super::reload;
use super::store::{SourcePosition, SqliteStateStore};
use crate::EventFilter;

/// The bound API listener: a TCP socket, or a Unix domain socket on Unix.
enum BoundListener {
    Tcp(tokio::net::TcpListener),
    #[cfg(unix)]
    Unix(tokio::net::UnixListener),
}

/// Effective live event-tap limits, resolved from `daemon.tap.*` plus the
/// `--enable-tap` flag.
#[derive(Debug, Clone, Copy)]
pub struct TapSettings {
    /// Whether the tap accepts sessions. `false` makes `GET /api/v1/tap`
    /// return `503`.
    pub enabled: bool,
    /// Per-session bounded channel capacity.
    pub buffer_events: usize,
    /// Maximum concurrent capture sessions.
    pub max_sessions: usize,
    /// Largest accepted capture window.
    pub max_duration: std::time::Duration,
}

/// Effective live detection-tail limits, resolved from `daemon.tail.*` plus the
/// `--enable-tail` flag.
#[derive(Debug, Clone, Copy)]
pub struct TailSettings {
    /// Whether the tail accepts sessions. `false` makes
    /// `GET /api/v1/detections/stream` return `503`.
    pub enabled: bool,
    /// Per-session bounded channel capacity.
    pub buffer_events: usize,
    /// Maximum concurrent tail sessions.
    pub max_sessions: usize,
}

/// Effective triage-feedback settings, resolved from `daemon.dispositions.*`
/// plus the `--enable-dispositions` flag.
#[derive(Debug, Clone)]
pub struct DispositionSettings {
    /// Whether the disposition endpoints and the per-rule ratio are active.
    pub enabled: bool,
    /// Optional pull source spec (a `--source`-style file). `None` is
    /// endpoint-only ingest.
    pub source: Option<PathBuf>,
    /// The rolling-store configuration (window, numerator, min sample).
    pub config: rsigma_runtime::DispositionConfig,
}

/// Effective verdict-driven capture settings, resolved from `daemon.capture.*`
/// plus `--enable-capture`. Startup-only: the daemon never reloads this block.
#[derive(Debug, Clone)]
pub struct CaptureSettings {
    /// Whether the capture ring and disposition spool are active.
    pub enabled: bool,
    /// Directory that receives completed bundles. Required when enabled.
    pub spool_dir: Option<PathBuf>,
    /// In-memory ring bounds.
    pub ring: rsigma_runtime::CaptureConfig,
    /// Disk budget for completed bundles.
    pub max_spool_bytes: u64,
    /// Bounded spool-job queue capacity.
    pub spool_queue_capacity: usize,
    /// Whether the operator independently requested `--include-event`.
    pub operator_include_event: bool,
}

/// Controls whether correlation state is restored from SQLite on startup.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StateRestoreMode {
    /// Decide automatically. For NATS replay, compare the replay start point
    /// against the stored source position: restore when replaying forward,
    /// skip when replaying backward. For non-NATS and Resume, always restore.
    Auto,
    /// Unconditionally clear state (`--clear-state`).
    ForceClear,
    /// Unconditionally restore state (`--keep-state`).
    ForceKeep,
}

#[derive(Clone)]
struct AppState {
    processor: Arc<LogProcessor>,
    metrics: Arc<Metrics>,
    health: HealthState,
    reload_tx: mpsc::Sender<()>,
    start_time: Instant,
    /// Channel for HTTP event ingestion. Set when --input is http.
    event_tx: Option<mpsc::Sender<RawEvent>>,
    /// Channel for on-demand source resolution triggers.
    sources_trigger_tx: Option<mpsc::Sender<rsigma_runtime::sources::refresh::RefreshTrigger>>,
    /// The instrumented source resolver (provides cache access for invalidation API).
    source_resolver: Option<Arc<super::instrumented_resolver::InstrumentedResolver>>,
    /// Channel for OTLP event ingestion. Always set when daemon-otlp is compiled in.
    #[cfg(feature = "daemon-otlp")]
    otlp_event_tx: mpsc::Sender<RawEvent>,
    /// The daemon-wide source registry for API endpoints.
    source_registry: Arc<rsigma_runtime::sources::registry::DaemonSourceRegistry>,
    /// Opt-in field observer. `Some` when the daemon was started with
    /// `--observe-fields`; the engine task records observed field keys
    /// and the `/api/v1/fields/*` handlers (Phase 4) consume snapshots.
    field_observer: Option<Arc<FieldObserver>>,
    /// Opt-in schema observer. `Some` when the daemon was started with
    /// `--observe-schemas`; the engine task classifies each event and the
    /// `GET /api/v1/schemas` handler serves snapshots.
    schema_observer: Option<Arc<SchemaObserver>>,
    /// Live event-tap state. `Some` when the tap is enabled; the
    /// `GET /api/v1/tap` handler registers capture sessions on it.
    tap: Option<super::tap::TapState>,
    /// Live detection-tail state. `Some` when the tail is enabled; the
    /// `GET /api/v1/detections/stream` handler registers sessions on it.
    tail: Option<super::tail::TailState>,
    /// The alert-pipeline config swap, read by `GET /api/v1/incidents` to
    /// resolve the incident include mode.
    alert_pipeline_swap: Arc<ArcSwap<Option<Arc<AlertPipeline>>>>,
    /// Mutable alert-pipeline state (incidents + silences), shared with the
    /// sink task. Read by `/api/v1/incidents` and read/written by
    /// `/api/v1/silences`.
    alert_state: Arc<std::sync::RwLock<AlertPipelineState>>,
    /// The risk-layer config swap, read by `GET /api/v1/risk` to resolve the
    /// accumulation window for the open-entity view.
    risk_swap: Arc<ArcSwap<Option<Arc<RiskLayer>>>>,
    /// Mutable per-entity risk-accumulator state, shared with the sink task.
    /// Read by `GET /api/v1/risk`.
    risk_state: Arc<std::sync::RwLock<RiskState>>,
    /// Triage feedback loop. `Some` when `--enable-dispositions` (or
    /// `daemon.dispositions.enabled`) is set; the `/api/v1/dispositions`
    /// handlers ingest verdicts and serve the per-rule ratio view.
    disposition_state: Option<super::dispositions::DispositionState>,
    /// Control-plane API audit trail. `Some` when `--state-db` is configured
    /// and audit is not disabled; the `/api/v1/audit` handler reads from it.
    audit: Option<Arc<super::audit::AuditLog>>,
}

#[derive(Clone)]
pub struct DaemonConfig {
    pub rules_path: PathBuf,
    pub pipelines: Vec<Pipeline>,
    pub pipeline_paths: Vec<PathBuf>,
    pub corr_config: CorrelationConfig,
    pub include_event: bool,
    pub pretty: bool,
    pub api_addr: super::listen::ListenAddr,
    pub event_filter: Arc<EventFilter>,
    pub state_db: Option<PathBuf>,
    pub state_save_interval: u64,
    pub input: String,
    pub output: Vec<String>,
    pub buffer_size: usize,
    pub batch_size: usize,
    /// Shared async delivery tuning applied to every sink worker.
    pub delivery_config: DeliveryConfig,
    pub dlq: Option<String>,
    #[cfg(feature = "daemon-nats")]
    pub nats_config: rsigma_runtime::NatsConnectConfig,
    #[cfg(feature = "daemon-nats")]
    pub replay_policy: rsigma_runtime::ReplayPolicy,
    #[cfg(feature = "daemon-nats")]
    pub consumer_group: Option<String>,
    pub state_restore_mode: StateRestoreMode,
    pub drain_timeout: u64,
    pub input_format: InputFormat,
    pub allow_remote_include: bool,
    /// Enable opt-in bloom-filter pre-filtering of positive substring
    /// matchers. Off by default; benefit is workload-dependent.
    pub bloom_prefilter: bool,
    /// Match-detail verbosity forwarded to the inner detection engine.
    /// `Off` by default (historical wire shape).
    pub match_detail: MatchDetailLevel,
    /// Optional override for the bloom memory budget (bytes). `None` means
    /// the crate default (1 MB).
    pub bloom_max_bytes: Option<usize>,
    /// Enable the opt-in field observer that counts every event's field
    /// keys so the `/api/v1/fields/*` endpoints can surface gap and
    /// broken-coverage signals. Off by default; when off the engine
    /// task does not iterate `Event::field_keys` at all.
    pub observe_fields: bool,
    /// Hard ceiling on the number of distinct field names tracked by
    /// the field observer. Once the ceiling is reached, new keys are
    /// dropped (and counted via
    /// `rsigma_fields_observer_overflow_dropped_total`); existing keys
    /// keep incrementing.
    pub observe_fields_max_keys: usize,
    /// Enable the opt-in schema observer that classifies every event so the
    /// `GET /api/v1/schemas` endpoint and the
    /// `rsigma_events_by_schema_total` / `rsigma_events_unknown_schema_total`
    /// counters can surface the schema mix and unknown rate. Off by default.
    pub observe_schemas: bool,
    /// Enable the discovery sampler: record redacted shapes of unrecognized
    /// (no-match or `generic_json`) events so `GET /api/v1/schemas/suggestions`
    /// can mine them into candidate signatures. Implies `observe_schemas`.
    pub discover_schemas: bool,
    /// Optional path to a YAML file of user-defined schema signatures (and,
    /// with `schema_routing`, the routing bindings), merged over the built-ins.
    pub schema_config: Option<PathBuf>,
    /// Enable schema routing: classify each event and route it to the
    /// pipeline-set bound to its schema, feeding a single shared correlation
    /// store. Bindings come from the `routing:` section of `schema_config`.
    pub schema_routing: bool,
    /// Opt-in, gated per-schema rule partitioning: compile each platform-locked
    /// per-schema engine with only the rules whose product can apply, cutting
    /// the N-copies memory cost. Off by default.
    pub schema_partition_rules: bool,
    /// Override for the `on_unknown` policy (`warn`/`drop`/`passthrough`/`error`).
    pub on_unknown: Option<String>,
    /// Enable conflict-based logsource pruning on the detection engine(s).
    pub logsource_routing: bool,
    /// Event field-name map for logsource extraction, as
    /// `product=...,service=...,category=...`. `None` uses the literal names.
    pub logsource_field_map: Option<String>,
    /// Static event logsource applied when the field is absent, as
    /// `product=windows,...`, for a single-source pipeline.
    pub event_logsource: Option<String>,
    /// Enable the cross-rule Aho-Corasick pre-filter. Off by default;
    /// benefit is workload-dependent (large rule sets with shared
    /// substring patterns). Available behind the `daachorse-index`
    /// Cargo feature.
    #[cfg(feature = "daachorse-index")]
    pub cross_rule_ac: bool,
    /// Optional path to the enrichers config (from `--enrichers`). Read
    /// at daemon startup and again on hot-reload (SIGHUP / file watcher
    /// / `POST /api/v1/reload`); failures during reload are logged and
    /// the previous pipeline stays active.
    pub enrichers_path: Option<PathBuf>,
    /// Local STIX store root for `type: stix` enrichers (`stix-enrich` feature).
    #[cfg(feature = "stix-enrich")]
    pub stix_store_path: Option<PathBuf>,
    /// Parse custom STIX types when opening [`stix_store_path`](Self::stix_store_path).
    #[cfg(feature = "stix-enrich")]
    pub stix_store_allow_custom: bool,
    /// Optional path to the alert-pipeline config (from `--alert-pipeline`).
    /// Read at daemon startup and again on hot-reload; failures during
    /// reload are logged and the previous pipeline stays active.
    pub alert_pipeline_path: Option<PathBuf>,
    /// Optional path to the risk-based alerting config (from `--risk`). Read at
    /// daemon startup and again on hot-reload; failures during reload are
    /// logged and the previous config stays active.
    pub risk_path: Option<PathBuf>,
    /// Webhook config file/dir paths (from `--webhook`). Loaded and validated
    /// once at daemon startup, then built into lossy (`on_full=drop`) delivery
    /// leaves. Hot reload is not supported in v1.
    pub webhook_paths: Vec<PathBuf>,
    /// Daemon-scoped source registry built from `--source` flags and
    /// pipeline-embedded `sources:` blocks. Collision-checked at
    /// construction time.
    pub source_registry: rsigma_runtime::sources::registry::DaemonSourceRegistry,
    /// Effective live event-tap limits (`daemon.tap.*` + `--enable-tap`).
    pub tap: TapSettings,
    /// Effective live detection-tail limits (`daemon.tail.*` + `--enable-tail`).
    pub tail: TailSettings,
    /// Effective triage-feedback settings (`daemon.dispositions.*` +
    /// `--enable-dispositions`).
    pub dispositions: DispositionSettings,
    /// Effective verdict-driven capture settings (`daemon.capture.*` +
    /// `--enable-capture`). Startup-only.
    pub capture: CaptureSettings,
    /// Optional API authentication table (`daemon.api.auth` +
    /// `--api-token-env`). `None` mounts the routes without auth, as before.
    pub api_auth: Option<super::auth::ApiAuth>,
    /// Control-plane API audit trail settings (`daemon.api.audit` +
    /// `--state-db`). Auto-enabled when a state database is configured.
    pub audit: super::audit::AuditSettings,
    /// Optional server-side TLS state. `Some` when the operator passed
    /// `--tls-cert`/`--tls-key`; the daemon then terminates TLS on the
    /// API listener for HTTP REST, OTLP/HTTP, and OTLP/gRPC.
    #[cfg(feature = "daemon-tls")]
    pub tls_state: Option<super::tls::TlsState>,
}

fn default_detect_inflight(workers: usize) -> usize {
    match workers {
        0 | 1 => 1,
        2..=3 => 2,
        4..=7 => 3,
        _ => 5,
    }
}

pub async fn run_daemon(config: DaemonConfig) {
    let metrics = Arc::new(Metrics::new());
    let health = HealthState::new();

    // The enrichment pipeline is constructed below, after the dynamic
    // source resolver exists, so `lookup` enrichers can share the
    // resolver's `Arc<SourceCache>`. We hoist the `ArcSwap` here so
    // the sink task closure can capture it directly.
    let enrichment_metrics = metrics.clone() as Arc<dyn rsigma_runtime::MetricsHook>;
    let enrichment_swap: Arc<ArcSwap<Option<Arc<EnrichmentPipeline>>>> =
        Arc::new(ArcSwap::new(Arc::new(None)));

    // The alert pipeline (dedup and, in later stages, grouping / silencing /
    // inhibition) runs in the sink task after enrichment. Hoisted here so the
    // sink task and the reload task can both capture the `ArcSwap`.
    let alert_pipeline_swap: Arc<ArcSwap<Option<Arc<AlertPipeline>>>> =
        Arc::new(ArcSwap::new(Arc::new(None)));

    // Mutable alert-pipeline state (dedup, incidents, silences). Owned by the
    // sink task but shared behind an `RwLock` so the `/api/v1/incidents` and
    // `/api/v1/silences` handlers can read and mutate it.
    let alert_state: Arc<std::sync::RwLock<AlertPipelineState>> =
        Arc::new(std::sync::RwLock::new(AlertPipelineState::default()));

    // The risk layer (annotation and, in the incident layer, the per-entity
    // accumulator) runs in the sink task after enrichment and before the alert
    // pipeline. Hoisted here so the sink task and the reload task can both
    // capture the `ArcSwap`.
    let risk_swap: Arc<ArcSwap<Option<Arc<RiskLayer>>>> = Arc::new(ArcSwap::new(Arc::new(None)));

    // Mutable per-entity risk-accumulator state. Owned by the sink task but
    // shared behind an `RwLock` so the `GET /api/v1/risk` handler can read the
    // open entities.
    let risk_state: Arc<std::sync::RwLock<RiskState>> =
        Arc::new(std::sync::RwLock::new(RiskState::default()));

    // Open SQLite state store if configured
    let state_store = config.state_db.as_ref().map(|path| {
        let store = SqliteStateStore::open(path).unwrap_or_else(|e| {
            tracing::error!(error = %e, path = %path.display(), "Failed to open state database");
            std::process::exit(crate::exit_code::CONFIG_ERROR);
        });
        tracing::info!(path = %path.display(), "State database opened");
        Arc::new(store)
    });

    let mut engine = RuntimeEngine::new(
        config.rules_path.clone(),
        config.pipelines.clone(),
        config.corr_config.clone(),
        config.include_event,
    );
    engine.set_pipeline_paths(config.pipeline_paths.clone());
    engine.set_allow_remote_include(config.allow_remote_include);
    engine.set_match_detail(config.match_detail);
    engine.set_bloom_prefilter(config.bloom_prefilter);
    if let Some(budget) = config.bloom_max_bytes {
        engine.set_bloom_max_bytes(budget);
    }
    #[cfg(feature = "daachorse-index")]
    engine.set_cross_rule_ac(config.cross_rule_ac);

    if config.schema_routing {
        engine.set_routing(Some(build_routing_spec(
            config.schema_config.as_deref(),
            config.on_unknown.as_deref(),
            config.schema_partition_rules,
        )));
        tracing::info!("Schema routing enabled");
    }

    if config.logsource_routing {
        // The daemon ingests streams (no per-input EVTX detection), so the
        // format-derived default never applies here; it is an eval-only path.
        match crate::logsource_opts::build_logsource_extractor(
            true,
            config.logsource_field_map.as_deref(),
            config.event_logsource.as_deref(),
            false,
        ) {
            Ok(extractor) => {
                engine.set_logsource_extractor(extractor);
                tracing::info!("Logsource routing enabled");
            }
            Err(e) => {
                eprintln!("Error in logsource routing options: {e}");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        }
    }

    // Set up dynamic source resolver if the registry has sources or any
    // pipeline references external sources.
    let has_dynamic =
        !config.source_registry.is_empty() || config.pipelines.iter().any(|p| p.is_dynamic());
    let mut sources_trigger_tx_val: Option<
        mpsc::Sender<rsigma_runtime::sources::refresh::RefreshTrigger>,
    > = None;

    let mut source_resolver_val: Option<Arc<super::instrumented_resolver::InstrumentedResolver>> =
        None;

    if has_dynamic {
        let instrumented = Arc::new(super::instrumented_resolver::InstrumentedResolver::new(
            metrics.clone(),
        ));
        source_resolver_val = Some(instrumented.clone());
        let resolver: Arc<dyn rsigma_runtime::sources::SourceResolver> = instrumented;
        engine.set_source_resolver(resolver.clone());

        // Feed the engine the external source declarations so it can resolve
        // and expand `${source.*}` references in the pipelines.
        engine.set_external_sources(
            config
                .source_registry
                .sources()
                .into_iter()
                .cloned()
                .collect(),
        );

        // Resolve dynamic sources at startup (blocks on required sources)
        if let Err(e) = engine.resolve_dynamic_pipelines().await {
            tracing::error!(error = %e, "Failed to resolve required dynamic sources at startup");
            std::process::exit(crate::exit_code::CONFIG_ERROR);
        }

        // Resolve external (registry-only) sources at startup
        let registry_only_sources: Vec<_> = config
            .source_registry
            .entries()
            .iter()
            .filter(|e| {
                matches!(
                    e.origin,
                    rsigma_runtime::sources::registry::SourceOrigin::External(_)
                )
            })
            .map(|e| e.source.clone())
            .collect();
        if !registry_only_sources.is_empty() {
            match rsigma_runtime::sources::resolve_all(&*resolver, &registry_only_sources).await {
                Ok(_) => {
                    tracing::info!(
                        count = registry_only_sources.len(),
                        "External sources resolved at startup"
                    );
                }
                Err(e) => {
                    tracing::error!(error = %e, "Failed to resolve required external sources at startup");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                }
            }
        }

        // Collect all dynamic sources for the refresh scheduler: unified
        // registry instead of iterating pipelines.
        let all_sources: Vec<_> = config
            .source_registry
            .sources()
            .into_iter()
            .cloned()
            .collect();

        if !all_sources.is_empty() {
            let scheduler = rsigma_runtime::sources::refresh::RefreshScheduler::new();
            sources_trigger_tx_val = Some(scheduler.trigger_sender());

            // Spawn NATS control subject listener for remote re-resolution triggers
            #[cfg(feature = "daemon-nats")]
            {
                let nats_url = config.nats_config.url.clone();
                let trigger_tx = scheduler.trigger_sender();
                tokio::spawn(async move {
                    let subject = rsigma_runtime::sources::refresh::NATS_CONTROL_SUBJECT;
                    if let Err(e) = rsigma_runtime::sources::refresh::nats_control_loop(
                        &nats_url, subject, trigger_tx,
                    )
                    .await
                    {
                        tracing::warn!(
                            error = %e,
                            "NATS control subject listener failed"
                        );
                    }
                });
            }

            // Collect optional source IDs for background retry
            let optional_source_ids: Vec<String> = all_sources
                .iter()
                .filter(|s| !s.required)
                .map(|s| s.id.clone())
                .collect();

            let bg_trigger_tx = scheduler.trigger_sender();
            scheduler.run(all_sources, resolver);

            // Spawn background retry for optional sources that may have failed at startup
            if !optional_source_ids.is_empty() {
                tokio::spawn(async move {
                    tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                    for id in optional_source_ids {
                        let _ = bg_trigger_tx
                            .send(rsigma_runtime::sources::refresh::RefreshTrigger::Single(id))
                            .await;
                    }
                });
            }
        }
    }

    let processor = Arc::new(LogProcessor::new(engine, metrics.clone()));

    // Initial rule load
    match processor.reload_rules() {
        Ok(stats) => {
            tracing::info!(
                detection_rules = stats.detection_rules,
                correlation_rules = stats.correlation_rules,
                path = %config.rules_path.display(),
                "Rules loaded"
            );
            metrics
                .detection_rules_loaded
                .set(stats.detection_rules as i64);
            metrics
                .correlation_rules_loaded
                .set(stats.correlation_rules as i64);
            health.set_ready(true);
        }
        Err(e) => {
            tracing::error!(error = %e, "Failed to load initial rules");
            std::process::exit(crate::exit_code::RULE_ERROR);
        }
    }

    // Restore correlation state from SQLite (after rules are loaded).
    //
    // The decision depends on `state_restore_mode`:
    // - ForceClear: always skip (--clear-state).
    // - ForceKeep: always restore (--keep-state).
    // - Auto: for NATS replay, compare the replay start point against the
    //   stored source position to avoid double-counting when replaying
    //   backward, while preserving cross-boundary correlations when
    //   replaying forward. For non-NATS and Resume, always restore.
    // Whether to restore persisted state on boot. `--clear-state` never
    // restores; the correlation block below refines this for the NATS
    // backward-replay case. The alert-pipeline restore reuses the decision.
    let mut restore_state = config.state_restore_mode != StateRestoreMode::ForceClear;
    if let Some(ref store) = state_store {
        match store.load().await {
            Ok(Some((snapshot, stored_position))) => {
                let should_restore = decide_state_restore(
                    config.state_restore_mode,
                    stored_position,
                    #[cfg(feature = "daemon-nats")]
                    &config.replay_policy,
                );
                restore_state = should_restore;
                if should_restore {
                    if processor.import_state(&snapshot) {
                        let entries = snapshot.windows.values().map(|g| g.len()).sum::<usize>();
                        tracing::info!(
                            state_entries = entries,
                            "Correlation state restored from database"
                        );
                    } else {
                        tracing::warn!(
                            snapshot_version = snapshot.version,
                            "Incompatible snapshot version, starting with fresh state"
                        );
                    }
                } else {
                    tracing::info!("Correlation state cleared (not restoring)");
                }
            }
            Ok(None) => {
                tracing::info!("No previous correlation state found in database");
            }
            Err(e) => {
                tracing::warn!(error = %e, "Failed to load state from database, starting fresh");
            }
        }
    }

    // Build the post-evaluation enrichment pipeline now that the
    // dynamic source resolver (if any) has been constructed; `lookup`
    // enrichers share the resolver's `Arc<SourceCache>` so they read
    // from the same cache the resolver writes into. Failures here exit
    // cleanly because no I/O has started yet.
    let initial_source_cache = source_resolver_val.as_ref().map(|r| r.arc_cache());
    #[cfg(feature = "stix-enrich")]
    let stix_fs_store: Option<std::sync::Arc<rstix::store::FsStore>> = config
        .stix_store_path
        .as_ref()
        .map(|path| {
            let parse_options = rstix::model::ParseOptions::default()
                .allow_custom(config.stix_store_allow_custom);
            match rstix::store::FsStore::open_with_parse_options(path, parse_options) {
            Ok(store) => {
                tracing::info!(path = %path.display(), "STIX store opened for enrichment");
                std::sync::Arc::new(store)
            }
            Err(e) => {
                tracing::error!(error = %e, path = %path.display(), "Failed to open STIX store");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
            }
        });
    if let Some(path) = config.enrichers_path.as_ref() {
        match super::enrichment::load_enrichers_file(path).and_then(|file| {
            super::enrichment::build_enrichers_full(
                file,
                super::enrichment::EnricherResources {
                    source_cache: initial_source_cache.clone(),
                    #[cfg(feature = "stix-enrich")]
                    stix_store: stix_fs_store
                        .clone()
                        .map(|store| store as std::sync::Arc<dyn rstix::store::StixStore>),
                },
                enrichment_metrics.clone(),
            )
        }) {
            Ok(p) => {
                tracing::info!(
                    enrichers = p.len(),
                    path = %path.display(),
                    "Enrichment pipeline loaded"
                );
                enrichment_swap.store(Arc::new(Some(Arc::new(p))));
            }
            Err(e) => {
                tracing::error!(error = %e, "Failed to build enrichment pipeline");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        }
    }

    // Build the alert pipeline. Failures exit cleanly because no I/O has
    // started yet.
    if let Some(path) = config.alert_pipeline_path.as_ref() {
        match load_alert_pipeline_file(path).and_then(build_alert_pipeline) {
            Ok(pipeline) => {
                tracing::info!(path = %path.display(), "Alert pipeline loaded");
                if let Ok(mut state) = alert_state.write() {
                    state
                        .silences
                        .set_static(pipeline.static_silences().to_vec());
                }
                // Restore persisted alert-pipeline state (unless --clear-state),
                // pruning entries past their window at boot.
                if restore_state && let Some(ref store) = state_store {
                    match store.load_alert_pipeline().await {
                        Ok(Some(snap)) => {
                            let now = chrono::Utc::now().timestamp();
                            let restored = alert_state
                                .write()
                                .map(|mut state| pipeline.restore(&mut state, snap, now))
                                .unwrap_or(false);
                            if restored {
                                tracing::info!("Alert-pipeline state restored from database");
                            } else {
                                tracing::warn!(
                                    "Incompatible alert-pipeline snapshot version, starting fresh"
                                );
                            }
                        }
                        Ok(None) => {}
                        Err(e) => {
                            tracing::warn!(error = %e, "Failed to load alert-pipeline state");
                        }
                    }
                }
                if config.capture.enabled
                    && let Some(err) = pipeline.capture_incompatibility()
                {
                    tracing::error!(error = %err, "Capture requires a compatible group_by alert pipeline");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                }
                alert_pipeline_swap.store(Arc::new(Some(Arc::new(pipeline))));
            }
            Err(e) => {
                tracing::error!(error = %e, "Failed to build alert pipeline");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        }
    } else if config.capture.enabled {
        tracing::error!("Capture requires --alert-pipeline with group.mode: group_by");
        std::process::exit(crate::exit_code::CONFIG_ERROR);
    }

    let capture_ring: Option<Arc<std::sync::Mutex<CaptureRing>>> =
        config.capture.enabled.then(|| {
            Arc::new(std::sync::Mutex::new(CaptureRing::new(
                config.capture.ring.clone(),
            )))
        });
    let capture_spool = match (
        config.capture.enabled,
        config.capture.spool_dir.clone(),
        capture_ring.clone(),
    ) {
        (true, Some(dir), Some(ring)) => {
            match super::capture::CaptureSpool::start(
                dir,
                config.capture.max_spool_bytes,
                config.capture.spool_queue_capacity,
                ring,
                metrics.clone(),
            ) {
                Ok(spool) => Some(spool),
                Err(e) => {
                    tracing::error!(error = %e, "Failed to initialize capture spool");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                }
            }
        }
        _ => None,
    };

    // Build the risk layer. Failures exit cleanly because no I/O has started yet.
    if let Some(path) = config.risk_path.as_ref() {
        match load_risk_file(path).and_then(build_risk_layer) {
            Ok(layer) => {
                tracing::info!(path = %path.display(), "Risk layer loaded");
                // Restore persisted risk state (unless --clear-state), pruning
                // entries past their window at boot.
                if restore_state && let Some(ref store) = state_store {
                    match store.load_risk().await {
                        Ok(Some(snap)) => {
                            let now = chrono::Utc::now().timestamp();
                            let restored = risk_state
                                .write()
                                .map(|mut state| layer.restore(&mut state, snap, now))
                                .unwrap_or(false);
                            if restored {
                                tracing::info!("Risk state restored from database");
                            } else {
                                tracing::warn!(
                                    "Incompatible risk snapshot version, starting fresh"
                                );
                            }
                        }
                        Ok(None) => {}
                        Err(e) => {
                            tracing::warn!(error = %e, "Failed to load risk state");
                        }
                    }
                }
                risk_swap.store(Arc::new(Some(Arc::new(layer))));
            }
            Err(e) => {
                tracing::error!(error = %e, "Failed to build risk layer");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        }
    }

    // Bounded channel acts as backpressure for reload requests. Capacity 4
    // allows the file watcher, SIGHUP handler, and HTTP endpoint to queue
    // reloads without blocking, while the consumer debounces with a 500ms
    // sleep + try_recv drain, collapsing bursts into a single reload.
    let (reload_tx, mut reload_rx) = mpsc::channel::<()>(4);

    // File watcher for hot-reload (rules + pipeline files)
    let pipeline_watch_paths: Vec<&std::path::Path> =
        config.pipeline_paths.iter().map(|p| p.as_path()).collect();
    let _watcher = if config.rules_path.is_dir() {
        reload::spawn_file_watcher(&config.rules_path, &pipeline_watch_paths, reload_tx.clone())
    } else {
        reload::spawn_file_watcher(
            config.rules_path.parent().unwrap_or(&config.rules_path),
            &pipeline_watch_paths,
            reload_tx.clone(),
        )
    };

    let start_time = Instant::now();

    // Create event channel early so both source and HTTP handler can use it
    let buffer_size = config.buffer_size;
    let (event_tx, mut event_rx) = mpsc::channel::<RawEvent>(buffer_size);

    let http_event_tx = if config.input == "http" {
        Some(event_tx.clone())
    } else {
        None
    };

    #[cfg(feature = "daemon-otlp")]
    let otlp_event_tx = event_tx.clone();

    let field_observer = if config.observe_fields {
        let observer = Arc::new(FieldObserver::new(config.observe_fields_max_keys));
        processor.set_field_observer(Some(observer.clone()));
        tracing::info!(
            max_keys = config.observe_fields_max_keys,
            "Field observer enabled"
        );
        Some(observer)
    } else {
        None
    };

    // `--discover-schemas` implies `--observe-schemas`: the discovery sampler
    // rides on the same observer.
    let schema_observer = if config.observe_schemas || config.discover_schemas {
        let classifier = match &config.schema_config {
            Some(path) => match load_schema_signatures(path) {
                Ok(sigs) => SchemaClassifier::with_user_signatures(sigs),
                Err(e) => {
                    tracing::error!(error = %e, "Failed to load schema signatures; using built-ins only");
                    SchemaClassifier::builtin()
                }
            },
            None => SchemaClassifier::builtin(),
        };
        let observer = Arc::new(SchemaObserver::new_with_discovery(
            classifier,
            config.discover_schemas,
        ));
        processor.set_schema_observer(Some(observer.clone()));
        if config.discover_schemas {
            tracing::info!(
                "Schema observer + discovery sampler enabled (GET /api/v1/schemas, \
                 GET /api/v1/schemas/suggestions)"
            );
        } else {
            tracing::info!("Schema observer enabled (GET /api/v1/schemas)");
        }
        Some(observer)
    } else {
        None
    };

    let tap = if config.tap.enabled {
        let registry = rsigma_runtime::TapRegistry::new(
            config.tap.buffer_events,
            config.tap.max_sessions,
            config.tap.max_duration,
        );
        processor.set_event_tap(Some(registry.clone()));
        tracing::info!(
            buffer_events = config.tap.buffer_events,
            max_sessions = config.tap.max_sessions,
            max_duration_secs = config.tap.max_duration.as_secs(),
            "Live event tap enabled (GET /api/v1/tap)"
        );
        Some(super::tap::TapState::new(registry, metrics.clone()))
    } else {
        None
    };

    let (tail, tail_registry) = if config.tail.enabled {
        let registry =
            super::tail::TailRegistry::new(config.tail.buffer_events, config.tail.max_sessions);
        tracing::info!(
            buffer_events = config.tail.buffer_events,
            max_sessions = config.tail.max_sessions,
            "Live detection tail enabled (GET /api/v1/detections/stream)"
        );
        (
            Some(super::tail::TailState::new(
                registry.clone(),
                metrics.clone(),
            )),
            Some(registry),
        )
    } else {
        (None, None)
    };

    // Triage feedback loop: build the shared disposition store when enabled.
    // Shares the alert-pipeline state so `scope: incident` verdicts resolve to
    // their contributing rules through the live incident map.
    let disposition_state = if config.dispositions.enabled {
        tracing::info!("Triage feedback loop enabled (POST/GET /api/v1/dispositions)");
        let state = super::dispositions::DispositionState::with_capture(
            config.dispositions.config.clone(),
            metrics.clone(),
            alert_state.clone(),
            capture_spool,
        );
        // Roll the window forward for idle rules on a timer.
        state.spawn_pruner();
        // Restore persisted disposition state (unless --clear-state), pruning
        // buckets past the window at boot.
        if restore_state && let Some(ref store) = state_store {
            match store.load_dispositions().await {
                Ok(Some(snap)) => {
                    let now = chrono::Utc::now().timestamp();
                    if state.restore(snap, now) {
                        tracing::info!("Disposition state restored from database");
                    } else {
                        tracing::warn!("Incompatible disposition snapshot version, starting fresh");
                    }
                }
                Ok(None) => {}
                Err(e) => tracing::warn!(error = %e, "Failed to load disposition state"),
            }
        }
        // Optional pull source: load the declared dynamic source(s) and ingest
        // each refreshed payload as dispositions over the Phase 0 seam.
        if let Some(src_path) = config.dispositions.source.as_ref() {
            match rsigma_runtime::sources::registry::load_external_sources(std::slice::from_ref(
                src_path,
            )) {
                Ok(loaded) => {
                    let sources: Vec<_> = loaded.into_iter().map(|(s, _)| s).collect();
                    if sources.is_empty() {
                        tracing::warn!(
                            path = %src_path.display(),
                            "Disposition source declared no sources"
                        );
                    } else {
                        tracing::info!(
                            path = %src_path.display(),
                            count = sources.len(),
                            "Disposition pull source loaded"
                        );
                        state.spawn_source(sources);
                    }
                }
                Err(e) => {
                    tracing::error!(
                        error = %e,
                        path = %src_path.display(),
                        "Failed to load disposition source"
                    );
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                }
            }
        }
        Some(state)
    } else {
        None
    };
    // A handle for the periodic and shutdown state savers (the field itself is
    // moved into `AppState` below).
    let disposition_state_for_save = disposition_state.clone();

    let audit_log: Option<Arc<super::audit::AuditLog>> = if config.audit.enabled {
        let store = state_store
            .as_ref()
            .expect("audit enabled without state db");
        let sink = if let Some(ref spec) = config.audit.sink {
            let spec = super::audit::audit_sink_spec(spec);
            let (sink, _on_full) = build_sink(&spec, config.pretty, SinkRole::Audit, &config).await;
            Some(Arc::new(tokio::sync::Mutex::new(sink)))
        } else {
            None
        };
        let log = Arc::new(super::audit::AuditLog::new(
            store.clone(),
            super::audit::AuditConfig {
                max_entries: config.audit.max_entries,
                max_age: config.audit.max_age,
                max_body_bytes: config.audit.max_body_bytes,
            },
            metrics.clone(),
            sink,
        ));
        log.prune().await;
        tracing::info!("API audit trail enabled");
        Some(log)
    } else {
        None
    };
    let audit_for_save = audit_log.clone();

    let app_state = AppState {
        processor: processor.clone(),
        metrics: metrics.clone(),
        health: health.clone(),
        reload_tx: reload_tx.clone(),
        start_time,
        event_tx: http_event_tx,
        sources_trigger_tx: sources_trigger_tx_val.clone(),
        source_resolver: source_resolver_val,
        #[cfg(feature = "daemon-otlp")]
        otlp_event_tx,
        source_registry: Arc::new(config.source_registry.clone()),
        field_observer: field_observer.clone(),
        schema_observer: schema_observer.clone(),
        tap: tap.clone(),
        tail: tail.clone(),
        alert_pipeline_swap: alert_pipeline_swap.clone(),
        alert_state: alert_state.clone(),
        risk_swap: risk_swap.clone(),
        risk_state: risk_state.clone(),
        disposition_state,
        audit: audit_log,
    };

    let app = Router::new()
        .route("/healthz", get(healthz))
        .route("/readyz", get(readyz))
        .route("/metrics", get(metrics_handler))
        .route("/api/v1/rules", get(list_rules))
        .route("/api/v1/status", get(status))
        .route("/api/v1/correlations", get(list_correlations))
        .route("/api/v1/correlations/state", get(correlations_state))
        .route("/api/v1/incidents", get(list_incidents))
        .route("/api/v1/incidents/{id}", get(get_incident))
        .route("/api/v1/incidents/{id}/bundle", get(get_incident_bundle))
        .route("/api/v1/risk", get(list_risk_entities))
        .route("/api/v1/silences", get(list_silences).post(create_silence))
        .route("/api/v1/silences/{id}", delete(delete_silence))
        .route(
            "/api/v1/dispositions",
            get(list_dispositions).post(ingest_dispositions),
        )
        .route("/api/v1/reload", post(trigger_reload))
        .route("/api/v1/events", post(ingest_events))
        .route("/api/v1/sources", get(list_sources))
        .route("/api/v1/sources/resolve", post(resolve_sources))
        .route(
            "/api/v1/sources/resolve/{source_id}",
            post(resolve_source_by_id),
        )
        .route(
            "/api/v1/sources/cache/{source_id}",
            delete(invalidate_source_cache),
        )
        .route("/api/v1/fields", get(fields_full))
        .route("/api/v1/fields/unknown", get(fields_unknown))
        .route("/api/v1/fields/missing", get(fields_missing))
        .route("/api/v1/fields/observer", delete(fields_observer_reset))
        .route("/api/v1/schemas", get(schemas_full).delete(schemas_reset))
        .route("/api/v1/schemas/suggestions", get(schema_suggestions))
        .route("/api/v1/tap", get(tap_stream))
        .route("/api/v1/detections/stream", get(detections_stream))
        .route("/api/v1/audit", get(list_audit));

    #[cfg(feature = "daemon-otlp")]
    let app = app.route("/v1/logs", post(otlp_http_logs));

    // Control-plane audit trail (opt-in via `--state-db`). Mounted before auth
    // so auth stays outermost and can reject before body buffering.
    let app = match audit_for_save.as_ref() {
        Some(audit) => app.route_layer(axum::middleware::from_fn_with_state(
            audit.clone(),
            super::audit::audit_middleware,
        )),
        None => app,
    };

    // Bearer-token auth (opt-in). Mounted with `Router::route_layer` so it
    // runs only for matched routes (a 404 needs no credentials) and after
    // routing, where `MatchedPath` carries the route pattern into the
    // permission lookup. The TraceLayer added below wraps it, so rejected
    // requests are traced too. OTLP/gRPC does not pass through this layer;
    // the gRPC service checks its own request metadata (see
    // `OtlpLogsGrpcService::export`).
    let app = match config.api_auth.as_ref() {
        Some(auth) => {
            tracing::info!("API authentication enabled");
            let extra_disposition_perm = config
                .capture
                .enabled
                .then_some(super::auth::Permission::required("capture", "write"));
            let state = Arc::new(super::auth::AuthLayerState {
                auth: auth.clone(),
                metrics: metrics.clone(),
                extra_disposition_perm,
            });
            app.route_layer(axum::middleware::from_fn_with_state(
                state,
                super::auth::api_auth_middleware,
            ))
        }
        None => app,
    };

    let app = app.layer(TraceLayer::new_for_http()).with_state(app_state);

    // The UDS guard unlinks the socket file when `run_daemon` returns (after
    // the drain), so a clean shutdown leaves no stale socket behind.
    #[cfg(unix)]
    let mut _uds_guard: Option<rsigma_runtime::UnixSocketGuard> = None;
    let (bound, actual_addr) = match &config.api_addr {
        ListenAddr::Tcp(addr) => {
            let listener = tokio::net::TcpListener::bind(addr)
                .await
                .unwrap_or_else(|e| {
                    tracing::error!(addr = %addr, error = %e, "Failed to bind API server");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                });
            let actual = listener
                .local_addr()
                .map(|a| a.to_string())
                .unwrap_or_else(|_| addr.to_string());
            (BoundListener::Tcp(listener), actual)
        }
        #[cfg(unix)]
        ListenAddr::Unix(path) => {
            let (listener, guard) = rsigma_runtime::bind_unix_listener(path)
                .await
                .unwrap_or_else(|e| {
                    tracing::error!(path = %path.display(), error = %e, "Failed to bind API server unix socket");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                });
            _uds_guard = Some(guard);
            (
                BoundListener::Unix(listener),
                format!("unix://{}", path.display()),
            )
        }
    };

    // Install the OS signal handlers eagerly, before the listener is
    // announced and reachable. The kernel completes the TCP handshake for
    // an incoming connection from the listen backlog the moment the socket
    // is bound, well before the serve task first polls its graceful-shutdown
    // future, so a SIGINT/SIGTERM arriving in that window would otherwise
    // hit the default disposition and kill the process. `signal()` registers
    // the handler at creation time (not on first poll like `ctrl_c()`), so
    // building the shutdown future here guarantees the handler is in place
    // before any client can observe the daemon as ready. The same streams
    // are moved into the serve task below, so a signal delivered during the
    // remaining setup is coalesced and still triggers a clean drain.
    let mut shutdown_fut: Option<std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>> = {
        #[cfg(unix)]
        {
            use tokio::signal::unix::{SignalKind, signal};
            let sigint = signal(SignalKind::interrupt()).expect("install SIGINT handler");
            let sigterm = signal(SignalKind::terminate()).expect("install SIGTERM handler");
            Some(Box::pin(shutdown_signal(sigint, sigterm)))
        }
        #[cfg(not(unix))]
        {
            Some(Box::pin(shutdown_signal()))
        }
    };

    #[cfg(feature = "daemon-otlp")]
    let otlp_routes = {
        let grpc_service = rsigma_runtime::LogsServiceServer::new(OtlpLogsGrpcService {
            event_tx: event_tx.clone(),
            metrics: metrics.clone(),
            auth: config.api_auth.clone(),
        })
        .accept_compressed(tonic::codec::CompressionEncoding::Gzip)
        .send_compressed(tonic::codec::CompressionEncoding::Gzip);
        tonic::service::Routes::from(app).add_service(grpc_service)
    };

    // TLS state is consumed below to build either a plaintext or TLS-wrapped
    // listener; pull it out of `config` here so the borrow ends before the
    // serve task captures the rest of the config by move.
    #[cfg(feature = "daemon-tls")]
    let tls_state = config.tls_state.clone();
    #[cfg(feature = "daemon-tls")]
    let tls_enabled = tls_state.is_some();
    #[cfg(not(feature = "daemon-tls"))]
    let tls_enabled = false;

    #[cfg(feature = "daemon-tls")]
    if let Some(ref state) = tls_state {
        update_tls_metrics(&metrics, state.expiry_unix.load(Ordering::Relaxed));
        warn_if_cert_expiring_soon(state.expiry_unix.load(Ordering::Relaxed));
    }

    #[cfg(feature = "daemon-otlp")]
    if tls_enabled {
        tracing::info!(addr = %actual_addr, "API server listening (HTTPS, HTTP/2 + gRPC)");
    } else {
        tracing::info!(addr = %actual_addr, "API server listening (HTTP/2 + gRPC)");
    }
    #[cfg(not(feature = "daemon-otlp"))]
    if tls_enabled {
        tracing::info!(addr = %actual_addr, "API server listening (HTTPS)");
    } else {
        tracing::info!(addr = %actual_addr, "API server listening");
    }

    // Spawn SIGHUP listener (Unix-only; routes the signal into the
    // same `reload_tx` channel the file watcher and HTTP endpoint
    // use, so every reload trigger funnels through one task).
    let sighup_reload_tx = reload_tx.clone();
    let sighup_sources_tx = sources_trigger_tx_val.clone();
    tokio::spawn(async move {
        reload::sighup_listener(sighup_reload_tx, sighup_sources_tx).await;
    });

    // Spawn reload handler — uses LogProcessor::reload_rules for atomic hot-reload.
    // Also re-reads enricher config and (when `daemon-tls` is built in) the TLS
    // certificate / key so a single `POST /api/v1/reload`, SIGHUP, or file-watcher
    // event rotates every hot-reloadable component in one debounced pass.
    let reload_processor = processor.clone();
    let reload_metrics = metrics.clone();
    let reload_health = health.clone();
    let reload_enrichment_swap = enrichment_swap.clone();
    let reload_enrichers_path = config.enrichers_path.clone();
    let reload_enrichment_metrics = enrichment_metrics.clone();
    let reload_source_cache = initial_source_cache.clone();
    #[cfg(feature = "stix-enrich")]
    let reload_stix_fs_store = stix_fs_store.clone();
    let reload_alert_pipeline_swap = alert_pipeline_swap.clone();
    let reload_alert_pipeline_path = config.alert_pipeline_path.clone();
    let reload_alert_state = alert_state.clone();
    let reload_risk_swap = risk_swap.clone();
    let reload_risk_state = risk_state.clone();
    let reload_risk_path = config.risk_path.clone();
    #[cfg(feature = "daemon-tls")]
    let reload_tls_state = tls_state.clone();
    #[cfg(feature = "daemon-tls")]
    let reload_tls_metrics = metrics.clone();
    tokio::spawn(async move {
        while reload_rx.recv().await.is_some() {
            // Debounce: batch rapid file changes
            tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
            while reload_rx.try_recv().is_ok() {}

            reload_metrics.reloads_total.inc();
            tracing::info!("Reloading rules and pipelines...");

            match reload_processor.reload_rules() {
                Ok(stats) => {
                    tracing::info!(
                        detection_rules = stats.detection_rules,
                        correlation_rules = stats.correlation_rules,
                        path = %reload_processor.rules_path().display(),
                        "Rules and pipelines reloaded"
                    );
                    reload_metrics
                        .detection_rules_loaded
                        .set(stats.detection_rules as i64);
                    reload_metrics
                        .correlation_rules_loaded
                        .set(stats.correlation_rules as i64);
                    reload_health.set_ready(true);
                }
                Err(e) => {
                    tracing::error!(error = %e, "Failed to reload rules");
                    reload_metrics.reloads_failed.inc();
                }
            }

            // Reload enrichers. Failures here log + bump
            // `reloads_failed` and leave the previous pipeline in place
            // so a typo in the enrichers config doesn't take down
            // detection enrichment in production.
            if let Some(path) = reload_enrichers_path.as_deref() {
                #[cfg(feature = "stix-enrich")]
                if let Some(store) = reload_stix_fs_store.as_ref()
                    && let Err(e) = store.reload_from_disk()
                {
                    tracing::error!(
                        error = %e,
                        "Failed to reload STIX store from disk; keeping previous enrichment index"
                    );
                    reload_metrics.reloads_failed.inc();
                }
                match super::enrichment::load_enrichers_file(path).and_then(|file| {
                    super::enrichment::build_enrichers_full(
                        file,
                        super::enrichment::EnricherResources {
                            source_cache: reload_source_cache.clone(),
                            #[cfg(feature = "stix-enrich")]
                            stix_store: reload_stix_fs_store
                                .clone()
                                .map(|store| store as std::sync::Arc<dyn rstix::store::StixStore>),
                        },
                        reload_enrichment_metrics.clone(),
                    )
                }) {
                    Ok(new_pipeline) => {
                        let prev_count = reload_enrichment_swap
                            .load()
                            .as_ref()
                            .as_ref()
                            .map(|p| p.len())
                            .unwrap_or(0);
                        let new_count = new_pipeline.len();
                        reload_enrichment_swap.store(Arc::new(Some(Arc::new(new_pipeline))));
                        tracing::info!(
                            previous = prev_count,
                            current = new_count,
                            path = %path.display(),
                            "Enrichment pipeline reloaded"
                        );
                    }
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            path = %path.display(),
                            "Failed to reload enrichers config; keeping previous pipeline"
                        );
                        reload_metrics.reloads_failed.inc();
                    }
                }
            }

            // Reload the alert pipeline. Failures keep the previous pipeline
            // in place so a typo cannot take down dedup in production. The
            // in-flight active-alert store is owned by the sink task and
            // survives a config swap; stale alerts age out via the new
            // pipeline's resolve_timeout.
            if let Some(path) = reload_alert_pipeline_path.as_deref() {
                match load_alert_pipeline_file(path).and_then(build_alert_pipeline) {
                    Ok(new_pipeline) => {
                        // Re-seed static silences (replacing the previous static
                        // set); API-created silences are preserved.
                        if let Ok(mut state) = reload_alert_state.write() {
                            state
                                .silences
                                .set_static(new_pipeline.static_silences().to_vec());
                        }
                        reload_alert_pipeline_swap.store(Arc::new(Some(Arc::new(new_pipeline))));
                        tracing::info!(path = %path.display(), "Alert pipeline reloaded");
                    }
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            path = %path.display(),
                            "Failed to reload alert pipeline; keeping previous pipeline"
                        );
                        reload_metrics.reloads_failed.inc();
                    }
                }
            }

            // Reload the risk layer. Failures keep the previous config in place
            // so a typo cannot take down risk scoring in production. In-flight
            // per-entity accumulators are owned by the sink task and survive a
            // config swap; stale entries age out via the new window.
            if let Some(path) = reload_risk_path.as_deref() {
                match load_risk_file(path).and_then(build_risk_layer) {
                    Ok(new_layer) => {
                        // If the reloaded config drops the accumulator, clear
                        // in-flight entities: with no `incident` block the tick
                        // no longer prunes them, so they would otherwise linger
                        // forever and keep being snapshotted.
                        if new_layer.incident_config().is_none()
                            && let Ok(mut st) = reload_risk_state.write()
                        {
                            *st = RiskState::default();
                        }
                        reload_risk_swap.store(Arc::new(Some(Arc::new(new_layer))));
                        tracing::info!(path = %path.display(), "Risk layer reloaded");
                    }
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            path = %path.display(),
                            "Failed to reload risk layer; keeping previous config"
                        );
                        reload_metrics.reloads_failed.inc();
                    }
                }
            }

            // Reload TLS certificate / key from disk when daemon-tls is
            // built in and configured. Failures keep the previous
            // certificate active so a typo in the cert path cannot
            // black-hole the listener; the operator sees the error in
            // the daemon log and via `rsigma_reloads_failed_total`.
            #[cfg(feature = "daemon-tls")]
            if let Some(ref state) = reload_tls_state {
                match state.reload() {
                    Ok(new_expiry) => {
                        update_tls_metrics(&reload_tls_metrics, new_expiry);
                        warn_if_cert_expiring_soon(new_expiry);
                        tracing::info!(not_after = new_expiry, "TLS certificate hot-reloaded");
                    }
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            "Failed to reload TLS certificate; keeping previous one active"
                        );
                        reload_metrics.reloads_failed.inc();
                    }
                }
            }
        }
    });

    // High-water mark for the last acked NATS stream position.
    // Updated by the ack task, read by the periodic/shutdown state saver.
    let high_water_seq = Arc::new(AtomicU64::new(0));
    let high_water_ts = Arc::new(AtomicI64::new(0));

    // Spawn periodic state saver
    if let Some(ref store) = state_store {
        let save_processor = processor.clone();
        let save_store = store.clone();
        let save_interval_secs = config.state_save_interval;
        let save_hw_seq = high_water_seq.clone();
        let save_hw_ts = high_water_ts.clone();
        let save_alert_swap = alert_pipeline_swap.clone();
        let save_alert_state = alert_state.clone();
        let save_risk_swap = risk_swap.clone();
        let save_risk_state = risk_state.clone();
        let save_dispositions = disposition_state_for_save.clone();
        let save_audit = audit_for_save.clone();
        tokio::spawn(async move {
            let mut interval =
                tokio::time::interval(tokio::time::Duration::from_secs(save_interval_secs));
            interval.tick().await; // skip first immediate tick
            loop {
                interval.tick().await;
                if let Some(snapshot) = save_processor.export_state() {
                    let position = source_position_from_atomics(&save_hw_seq, &save_hw_ts);
                    let snapshot_size = serde_json::to_vec(&snapshot).map(|v| v.len()).unwrap_or(0);
                    let window_count = snapshot.windows.len();
                    let save_start = std::time::Instant::now();
                    if let Err(e) = save_store.save(&snapshot, position.as_ref()).await {
                        tracing::warn!(
                            error = %e,
                            size_bytes = snapshot_size,
                            windows = window_count,
                            "Failed to save periodic state snapshot",
                        );
                    } else {
                        tracing::debug!(
                            duration_ms = save_start.elapsed().as_millis() as u64,
                            size_bytes = snapshot_size,
                            windows = window_count,
                            "Periodic state snapshot saved",
                        );
                    }
                }
                // Persist the alert-pipeline state when configured.
                if let Some(pipeline) = save_alert_swap.load_full().as_ref() {
                    let snap = {
                        let st = save_alert_state.read().unwrap_or_else(|e| e.into_inner());
                        pipeline.snapshot(&st)
                    };
                    if let Err(e) = save_store.save_alert_pipeline(&snap).await {
                        tracing::warn!(error = %e, "Failed to save alert-pipeline snapshot");
                    }
                }
                // Persist the risk-accumulator state when configured.
                if let Some(risk_layer) = save_risk_swap.load_full().as_ref() {
                    let snap = {
                        let st = save_risk_state.read().unwrap_or_else(|e| e.into_inner());
                        risk_layer.snapshot(&st)
                    };
                    if let Err(e) = save_store.save_risk(&snap).await {
                        tracing::warn!(error = %e, "Failed to save risk snapshot");
                    }
                }
                // Persist the disposition store when the triage loop is enabled.
                if let Some(dispositions) = save_dispositions.as_ref()
                    && let Some(snap) = dispositions.snapshot()
                    && let Err(e) = save_store.save_dispositions(&snap).await
                {
                    tracing::warn!(error = %e, "Failed to save disposition snapshot");
                }
                if let Some(audit) = save_audit.as_ref() {
                    audit.prune().await;
                }
            }
        });
    }

    // --- Streaming pipeline: source -> engine -> sink -> ack ---
    let (sink_tx, mut sink_rx) = mpsc::channel::<(ProcessResult, Vec<AckToken>)>(buffer_size);
    let (ack_tx, mut ack_rx) = mpsc::unbounded_channel::<AckToken>();

    // Select source based on --input flag
    #[cfg_attr(not(feature = "daemon-otlp"), allow(unused_mut))]
    let mut source_handle: Option<tokio::task::JoinHandle<()>> = match config.input.as_str() {
        "stdin" | "stdin://" => {
            let h = spawn_source(
                StdinSource::new(),
                event_tx,
                Some(metrics.clone() as Arc<dyn rsigma_runtime::MetricsHook>),
            );
            tracing::info!(input = "stdin", "Event source started");
            Some(h)
        }
        "http" => {
            drop(event_tx);
            tracing::info!(input = "http", "Event source started (POST /api/v1/events)");
            None
        }
        #[cfg(feature = "daemon-nats")]
        input if input.starts_with("nats://") => {
            let (url, subject) = parse_nats_url(input);
            let mut nats_cfg = config.nats_config.clone();
            nats_cfg.url = url.clone();
            match rsigma_runtime::NatsSource::connect(
                &nats_cfg,
                &subject,
                &config.replay_policy,
                config.consumer_group.as_deref(),
            )
            .await
            {
                Ok(source) => {
                    let h = spawn_source(
                        source,
                        event_tx,
                        Some(metrics.clone() as Arc<dyn rsigma_runtime::MetricsHook>),
                    );
                    tracing::info!(url = url, subject = subject, "NATS source started");
                    Some(h)
                }
                Err(e) => {
                    tracing::error!(error = %e, url = url, "Failed to connect NATS source");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                }
            }
        }
        #[cfg(unix)]
        input if input.starts_with("unix://") => {
            let path = input.strip_prefix("unix://").unwrap_or(input);
            match rsigma_runtime::UnixSocketSource::bind(std::path::Path::new(path)).await {
                Ok(source) => {
                    let h = spawn_source(
                        source,
                        event_tx,
                        Some(metrics.clone() as Arc<dyn rsigma_runtime::MetricsHook>),
                    );
                    tracing::info!(path = path, "Unix socket source started");
                    Some(h)
                }
                Err(e) => {
                    tracing::error!(error = %e, path = path, "Failed to bind unix socket source");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                }
            }
        }
        other => {
            tracing::error!(
                input = other,
                "Unsupported input source (supported: stdin, http, nats://, unix://)"
            );
            std::process::exit(crate::exit_code::CONFIG_ERROR);
        }
    };

    // Build optional DLQ sink from --dlq flag
    let (dlq_tx, mut dlq_rx) = mpsc::channel::<DlqEntry>(buffer_size);
    let dlq_sink = if let Some(ref dlq_spec) = config.dlq {
        let (sink, _) = build_sink(dlq_spec, false, SinkRole::Dlq, &config).await;
        tracing::info!(dlq = dlq_spec, "Dead-letter queue enabled");
        Some(sink)
    } else {
        None
    };

    // When a finite source (stdin/NATS) completes but OTLP handlers still hold
    // event_tx clones, event_rx.recv() would block forever. This notify signals
    // the engine to close its receiver so it drains remaining events and exits.
    #[cfg(feature = "daemon-otlp")]
    let source_done_notify = std::sync::Arc::new(tokio::sync::Notify::new());
    #[cfg(feature = "daemon-otlp")]
    if let Some(h) = source_handle.take() {
        let done = source_done_notify.clone();
        tokio::spawn(async move {
            let _ = h.await;
            done.notify_one();
        });
    }

    // Overlap detection-only batches so one batch's serial merge can run while
    // another occupies rayon. Correlation engines stay at 1 (ordered windows).
    // Override with RSIGMA_DETECT_INFLIGHT; default scales with the worker pool
    // size (RAYON_NUM_THREADS, else available parallelism).
    let detect_inflight = if processor.allows_concurrent_detection() {
        let workers = std::env::var("RAYON_NUM_THREADS")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .filter(|n| *n >= 1)
            .unwrap_or_else(|| {
                std::thread::available_parallelism()
                    .map(|n| n.get())
                    .unwrap_or(1)
            });
        let default = default_detect_inflight(workers);
        std::env::var("RSIGMA_DETECT_INFLIGHT")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .filter(|n| *n >= 1)
            .unwrap_or(default)
            .min(8)
    } else {
        1
    };
    if detect_inflight > 1 {
        tracing::info!(
            detect_inflight,
            "Concurrent detection enabled (no correlation rules)"
        );
    }

    // Engine task evaluates batches; a separate dispatch task drains results so
    // sink/ack work can overlap the next evaluate. When the engine has no
    // correlation rules, multiple detection batches may run concurrently and a
    // sequence-numbered reducer restores output order before dispatch.
    // Channel depth matches in-flight detection so a quiet input queue cannot
    // stall completed workers behind a capacity-1 handoff.
    let (eval_tx, mut eval_rx) = mpsc::channel::<EvaluatedBatch>(detect_inflight);

    let dispatch_metrics = metrics.clone();
    let dispatch_ack_tx = ack_tx.clone();
    let dispatch_dlq_tx = dlq_tx.clone();
    let dispatch_dlq_enabled = config.dlq.is_some();
    let dispatch_handle = tokio::spawn(async move {
        while let Some(batch) = eval_rx.recv().await {
            let dispatch_start = Instant::now();
            let mut parse_errors = batch.outcome.parse_error_indices.into_iter().peekable();
            let mut shutdown = false;
            for (index, ((result, ack_token), payload)) in batch
                .outcome
                .results
                .into_iter()
                .zip(batch.tokens)
                .zip(batch.payloads)
                .enumerate()
            {
                if parse_errors.peek().copied() == Some(index) {
                    parse_errors.next();
                    if dispatch_dlq_enabled {
                        tracing::debug!("Event routed to DLQ: parse error");
                        if dispatch_dlq_tx
                            .send(DlqEntry {
                                original_event: payload,
                                error: "parse error".to_string(),
                                timestamp: chrono::Utc::now().to_rfc3339(),
                            })
                            .await
                            .is_err()
                        {
                            tracing::warn!("DLQ channel closed, parse-error event dropped");
                        }
                    }
                    if dispatch_ack_tx.send(ack_token).is_err() {
                        shutdown = true;
                        break;
                    }
                    continue;
                }
                if result.is_empty() {
                    if dispatch_ack_tx.send(ack_token).is_err() {
                        tracing::debug!("Ack channel closed, engine shutting down");
                        shutdown = true;
                        break;
                    }
                    continue;
                }
                dispatch_metrics.on_output_queue_depth_change(1);
                if sink_tx.send((result, vec![ack_token])).await.is_err() {
                    tracing::debug!("Sink channel closed, engine shutting down");
                    shutdown = true;
                    break;
                }
            }
            dispatch_metrics
                .observe_batch_phase_duration("dispatch", dispatch_start.elapsed().as_secs_f64());
            dispatch_metrics.observe_pipeline_latency(batch.pipeline_start.elapsed().as_secs_f64());
            if shutdown {
                break;
            }
        }
    });

    // Engine task: reads RawEvents, evaluates rules, hands outcomes to dispatch.
    let engine_processor = processor.clone();
    let engine_metrics = metrics.clone();
    let event_filter = config.event_filter.clone();
    let event_filter_enabled = !matches!(event_filter.as_ref(), EventFilter::None);
    let batch_size = config.batch_size;
    let input_format = config.input_format.clone();
    #[cfg(feature = "daemon-otlp")]
    let engine_source_done = source_done_notify.clone();
    let engine_handle = tokio::spawn(async move {
        #[cfg(feature = "daemon-otlp")]
        let source_done = engine_source_done;
        #[cfg(feature = "daemon-otlp")]
        let mut source_finished = false;

        // Arc so spawn_blocking workers can share a 'static event filter.
        let runtime_filter: Option<SharedEventFilter> = if event_filter_enabled {
            let filter = event_filter.clone();
            Some(Arc::new(move |v: &serde_json::Value| {
                crate::apply_event_filter(v, filter.as_ref())
            }))
        } else {
            None
        };

        // Ordered reducer restores batch sequence when detection runs ahead.
        let (seq_tx, mut seq_rx) = mpsc::channel::<(u64, EvaluatedBatch)>(detect_inflight.max(1));
        let reducer_eval_tx = eval_tx;
        let reducer_handle = tokio::spawn(async move {
            let mut next = 0u64;
            let mut pending: BTreeMap<u64, EvaluatedBatch> = BTreeMap::new();
            while let Some((seq, batch)) = seq_rx.recv().await {
                pending.insert(seq, batch);
                while let Some(batch) = pending.remove(&next) {
                    if reducer_eval_tx.send(batch).await.is_err() {
                        return;
                    }
                    next = next.saturating_add(1);
                }
            }
        });

        let mut join_set = tokio::task::JoinSet::new();
        let mut next_seq = 0u64;
        let mut shutdown = false;

        // Forward a finished detection worker to the ordered reducer.
        async fn forward_joined(
            seq_tx: &mpsc::Sender<(u64, EvaluatedBatch)>,
            res: Result<(u64, EvaluatedBatch), tokio::task::JoinError>,
        ) -> bool {
            match res {
                Ok(item) => {
                    if seq_tx.send(item).await.is_err() {
                        tracing::debug!("Dispatch channel closed, engine shutting down");
                        return false;
                    }
                    true
                }
                Err(e) => {
                    tracing::error!(error = %e, "Detection worker panicked");
                    false
                }
            }
        }

        loop {
            let pipeline_start = Instant::now();

            // Wait for the next input event, but always drain completed detection
            // workers while idle. Otherwise a quiet queue leaves finished
            // spawn_blocking tasks stranded until another event arrives.
            let first = loop {
                if detect_inflight > 1 && join_set.len() >= detect_inflight {
                    if let Some(res) = join_set.join_next().await
                        && !forward_joined(&seq_tx, res).await
                    {
                        shutdown = true;
                    }
                    if shutdown {
                        break None;
                    }
                    continue;
                }

                if detect_inflight > 1 && !join_set.is_empty() {
                    #[cfg(feature = "daemon-otlp")]
                    {
                        if source_finished {
                            tokio::select! {
                                biased;
                                res = join_set.join_next() => {
                                    if let Some(res) = res
                                        && !forward_joined(&seq_tx, res).await
                                    {
                                        shutdown = true;
                                        break None;
                                    }
                                    continue;
                                }
                                event = event_rx.recv() => break event,
                            }
                        } else {
                            tokio::select! {
                                biased;
                                res = join_set.join_next() => {
                                    if let Some(res) = res
                                        && !forward_joined(&seq_tx, res).await
                                    {
                                        shutdown = true;
                                        break None;
                                    }
                                    continue;
                                }
                                event = event_rx.recv() => break event,
                                _ = source_done.notified() => {
                                    source_finished = true;
                                    event_rx.close();
                                    continue;
                                }
                            }
                        }
                    }
                    #[cfg(not(feature = "daemon-otlp"))]
                    {
                        tokio::select! {
                            biased;
                            res = join_set.join_next() => {
                                if let Some(res) = res
                                    && !forward_joined(&seq_tx, res).await
                                {
                                    shutdown = true;
                                    break None;
                                }
                                continue;
                            }
                            event = event_rx.recv() => break event,
                        }
                    }
                } else {
                    #[cfg(feature = "daemon-otlp")]
                    {
                        if source_finished {
                            break event_rx.recv().await;
                        }
                        tokio::select! {
                            event = event_rx.recv() => break event,
                            _ = source_done.notified() => {
                                source_finished = true;
                                event_rx.close();
                                break event_rx.recv().await;
                            }
                        }
                    }
                    #[cfg(not(feature = "daemon-otlp"))]
                    {
                        break event_rx.recv().await;
                    }
                }
            };

            if shutdown {
                break;
            }
            let Some(first) = first else {
                break;
            };
            engine_metrics.on_input_queue_depth_change(-1);

            let mut batch = Vec::with_capacity(batch_size.min(64));
            batch.push(first);
            while batch.len() < batch_size {
                match event_rx.try_recv() {
                    Ok(raw_event) => {
                        engine_metrics.on_input_queue_depth_change(-1);
                        batch.push(raw_event);
                    }
                    Err(_) => break,
                }
            }
            let initial_batch_size = batch.len();
            engine_metrics.observe_batch_size(initial_batch_size as u64);

            let mut payloads = Vec::with_capacity(batch.len());
            let mut tokens = Vec::with_capacity(batch.len());
            for raw_event in batch {
                payloads.push(raw_event.payload);
                tokens.push(raw_event.ack_token);
            }

            if payloads.is_empty() {
                continue;
            }

            let seq = next_seq;
            next_seq = next_seq.saturating_add(1);
            let processor = engine_processor.clone();
            let format = input_format.clone();
            let filter = runtime_filter.clone();

            if detect_inflight == 1 {
                // Keep the single-batch path on the engine task: no extra thread
                // hop, same ordering as before concurrent detection.
                let process_start = Instant::now();
                let outcome = match filter.as_ref() {
                    Some(f) => processor.process_batch_with_format_detailed(
                        &payloads,
                        &format,
                        Some(f.as_ref()),
                    ),
                    None => processor.process_batch_with_format_detailed(&payloads, &format, None),
                };
                let process_elapsed_ms = process_start.elapsed().as_millis() as u64;
                let match_count = outcome.results.iter().filter(|r| !r.is_empty()).count();
                tracing::debug!(
                    batch_size = payloads.len(),
                    matches = match_count,
                    elapsed_ms = process_elapsed_ms,
                    "Batch processed",
                );
                if seq_tx
                    .send((
                        seq,
                        EvaluatedBatch {
                            outcome,
                            payloads,
                            tokens,
                            pipeline_start,
                        },
                    ))
                    .await
                    .is_err()
                {
                    tracing::debug!("Dispatch channel closed, engine shutting down");
                    break;
                }
            } else {
                join_set.spawn_blocking(move || {
                    let process_start = Instant::now();
                    let outcome = match filter.as_ref() {
                        Some(f) => processor.process_batch_with_format_detailed(
                            &payloads,
                            &format,
                            Some(f.as_ref()),
                        ),
                        None => {
                            processor.process_batch_with_format_detailed(&payloads, &format, None)
                        }
                    };
                    let process_elapsed_ms = process_start.elapsed().as_millis() as u64;
                    let match_count = outcome.results.iter().filter(|r| !r.is_empty()).count();
                    tracing::debug!(
                        batch_size = payloads.len(),
                        matches = match_count,
                        elapsed_ms = process_elapsed_ms,
                        "Batch processed",
                    );
                    (
                        seq,
                        EvaluatedBatch {
                            outcome,
                            payloads,
                            tokens,
                            pipeline_start,
                        },
                    )
                });
            }
        }

        while let Some(res) = join_set.join_next().await {
            if !forward_joined(&seq_tx, res).await {
                break;
            }
        }
        drop(seq_tx);
        let _ = reducer_handle.await;
        tracing::info!("Event source exhausted, engine shutting down");
    });

    // Build leaf sinks from --output flags. The dispatcher runs one worker per
    // leaf, so fan-out is realized by the dispatcher rather than a FanOut sink.
    let pretty = config.pretty;
    let output_specs = if config.output.is_empty() {
        vec!["stdout".to_string()]
    } else {
        config.output.clone()
    };
    let mut leaves: Vec<(Sink, OnFull, DeliveryConfig)> = Vec::new();
    for spec in &output_specs {
        let (sink, on_full) = build_sink(spec, pretty, SinkRole::Findings, &config).await;
        for leaf in sink.into_leaves() {
            leaves.push((leaf, on_full, config.delivery_config));
        }
    }

    // Webhook sinks are built once at startup and run as lossy (on_full=drop)
    // leaves so a third-party HTTP endpoint never blocks the at-least-once
    // token release for the durable sinks; anything undeliverable lands in the
    // DLQ via the shared worker.
    if !config.webhook_paths.is_empty() {
        let webhook_metrics = metrics.clone() as Arc<dyn MetricsHook>;
        match super::webhook::load_and_build_webhooks(&config.webhook_paths, webhook_metrics) {
            Ok(built) => {
                for bw in built {
                    leaves.push((Sink::Webhook(Box::new(bw.sink)), OnFull::Drop, bw.delivery));
                }
            }
            Err(e) => {
                tracing::error!(error = %e, "Failed to load webhooks");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        }
    }
    // Incidents are serialized centrally, once per format the leaf sinks
    // actually ask for, so an all-NDJSON daemon never pays for an OCSF line.
    let ocsf_incidents = leaves
        .iter()
        .any(|(sink, _, _)| sink.format() == Some(SinkFormat::Ocsf));
    tracing::info!(output = ?output_specs, sinks = leaves.len(), "Sink started");

    // Bridge the delivery layer's terminal failures into the existing DLQ
    // writer so failed deliveries land alongside parse errors.
    let (df_tx, mut df_rx) = mpsc::channel::<DeliveryFailure>(buffer_size);
    let df_handle = tokio::spawn(async move {
        while let Some(failure) = df_rx.recv().await {
            let _ = dlq_tx
                .send(DlqEntry {
                    original_event: failure.serialized,
                    error: failure.error,
                    timestamp: chrono::Utc::now().to_rfc3339(),
                })
                .await;
        }
    });

    // Sink task: drains the engine->sink channel, runs post-evaluation
    // enrichment, then dispatches each result to the per-sink workers. The
    // dispatcher's ack-join releases ack tokens only once every sink has
    // committed the result (delivered or DLQ-parked), preserving at-least-once
    // across fan-out. On shutdown it drains the worker queues.
    if config.capture.enabled {
        tracing::info!(
            spool_dir = %config.capture.spool_dir.as_ref().expect("validated at startup").display(),
            max_spool_bytes = config.capture.max_spool_bytes,
            spool_queue_capacity = config.capture.spool_queue_capacity,
            "Capture ring enabled"
        );
    }
    let operator_include_event = config.capture.operator_include_event;

    let sink_metrics = metrics.clone();
    let enrichment_swap_for_sink = enrichment_swap.clone();
    let alert_pipeline_swap_for_sink = alert_pipeline_swap.clone();
    let alert_state_for_sink = alert_state.clone();
    let capture_ring_for_sink = capture_ring.clone();
    let capture_metrics = metrics.clone();
    let alert_pipeline_metrics = metrics.clone() as Arc<dyn MetricsHook>;
    let risk_swap_for_sink = risk_swap.clone();
    let risk_state_for_sink = risk_state.clone();
    let risk_metrics = metrics.clone() as Arc<dyn MetricsHook>;
    let delivery_metrics = metrics.clone() as Arc<dyn MetricsHook>;
    let dispatch_ack_tx = ack_tx.clone();
    let empty_ack_tx = ack_tx.clone();
    let tail_for_sink = tail_registry.clone();
    drop(ack_tx);
    let sink_handle = tokio::spawn(async move {
        let dispatcher = Dispatcher::spawn(leaves, Some(df_tx), dispatch_ack_tx, delivery_metrics);
        // 1s tick drives the alert pipeline's repeat re-emits, resolved
        // records, incident emissions, and silence GC. A no-op when no alert
        // pipeline is configured.
        let mut alert_tick = tokio::time::interval(tokio::time::Duration::from_secs(1));
        alert_tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            tokio::select! {
                maybe = sink_rx.recv() => {
                    let Some((mut result, ack_tokens)) = maybe else { break };
                    sink_metrics.on_output_queue_depth_change(-1);

                    // Post-evaluation enrichment may suppress every entry; if
                    // so, ack the events and skip sink delivery. `load_full`
                    // keeps the pipeline alive across the await without holding
                    // an `ArcSwap` guard.
                    let pipeline_snapshot = enrichment_swap_for_sink.load_full();
                    if let Some(pipeline) = pipeline_snapshot.as_ref()
                        && !pipeline.is_empty()
                    {
                        pipeline.run(&mut result).await;
                        if result.is_empty() {
                            for token in ack_tokens {
                                let _ = empty_ack_tx.send(token);
                            }
                            continue;
                        }
                    }

                    // Risk layer: annotate each in-scope firing with a risk
                    // score and risk objects, then (opt-in) emit a compact risk
                    // event per (detection, risk object) pair. Runs before the
                    // alert pipeline so risk accrues on every firing, before
                    // dedup, silencing, or inhibition can fold or drop it.
                    let risk_snapshot = risk_swap_for_sink.load_full();
                    if let Some(risk_layer) = risk_snapshot.as_ref() {
                        let now = chrono::Utc::now().timestamp();
                        let out = {
                            let mut st = risk_state_for_sink
                                .write()
                                .unwrap_or_else(|e| e.into_inner());
                            risk_layer.process(result, &mut st, now, risk_metrics.as_ref())
                        };
                        result = out.kept;
                        if !out.risk_events.is_empty() {
                            let subject =
                                risk_layer.risk_event_nats_subject().map(|s| s.to_string());
                            for event in out.risk_events {
                                match serde_json::to_string(&event) {
                                    Ok(json) => {
                                        dispatcher
                                            .dispatch_incident(IncidentEnvelope::new(
                                                json,
                                                subject.clone(),
                                            ))
                                            .await;
                                    }
                                    Err(e) => {
                                        tracing::warn!(error = %e, "Failed to serialize risk event");
                                    }
                                }
                            }
                        }
                        if !out.incidents.is_empty() {
                            let subject =
                                risk_layer.incident_nats_subject().map(|s| s.to_string());
                            for incident in out.incidents {
                                match serde_json::to_string(&incident) {
                                    Ok(json) => {
                                        let mut env = FormattedIncidentEnvelope::new(
                                            IncidentEnvelope::new(json, subject.clone()),
                                        );
                                        if ocsf_incidents {
                                            let finding =
                                                rsigma_runtime::ocsf::risk_incident_finding(
                                                    &incident,
                                                );
                                            match serde_json::to_string(&finding) {
                                                Ok(line) => {
                                                    env = env.with_line(SinkFormat::Ocsf, line);
                                                }
                                                Err(e) => {
                                                    tracing::warn!(error = %e, "Failed to serialize OCSF risk incident");
                                                }
                                            }
                                        }
                                        dispatcher.dispatch_formatted_incident(env).await;
                                    }
                                    Err(e) => {
                                        tracing::warn!(error = %e, "Failed to serialize risk incident");
                                    }
                                }
                            }
                        }
                    }

                    // Alert pipeline: dedup folds duplicates and grouping
                    // assigns survivors to incidents (annotating `incident_id`).
                    // If the whole batch folds away, ack and skip dispatch,
                    // mirroring the enrichment empty-result path. The incident
                    // store is locked only for the synchronous process call.
                    let alert_snapshot = alert_pipeline_swap_for_sink.load_full();
                    if let Some(alert_pipeline) = alert_snapshot.as_ref() {
                        let now = chrono::Utc::now().timestamp();
                        let kept = {
                            let mut st = alert_state_for_sink
                                .write()
                                .unwrap_or_else(|e| e.into_inner());
                            match capture_ring_for_sink.as_ref() {
                                Some(ring) => {
                                    let mut ring = ring.lock().unwrap_or_else(|e| e.into_inner());
                                    let mut sink = CaptureRingSink {
                                        ring: &mut ring,
                                        metrics: capture_metrics.as_ref(),
                                    };
                                    let mut kept = alert_pipeline.process_with_capture(
                                        result,
                                        &mut st,
                                        now,
                                        alert_pipeline_metrics.as_ref(),
                                        Some(&mut sink),
                                    );
                                    if !operator_include_event {
                                        for result in &mut kept {
                                            strip_event_payloads(result);
                                        }
                                    }
                                    kept
                                }
                                None => alert_pipeline.process(
                                    result,
                                    &mut st,
                                    now,
                                    alert_pipeline_metrics.as_ref(),
                                ),
                            }
                        };
                        if kept.is_empty() {
                            for token in ack_tokens {
                                let _ = empty_ack_tx.send(token);
                            }
                            continue;
                        }
                        result = kept;
                    }

                    // Live detection tail: fan the post-enrichment result out to
                    // any active tail sessions before dispatch. Lossy and
                    // non-blocking, so it can never stall the sink task or the
                    // ack-join.
                    if let Some(tail) = &tail_for_sink {
                        tail.capture(&result);
                    }

                    dispatcher.dispatch(result, ack_tokens).await;
                }
                _ = alert_tick.tick() => {
                    // Emit any due dedup repeat/resolved records and incident
                    // emissions. These are synthetic (no input event): dedup
                    // records dispatch with no ack tokens, incidents via the
                    // incident path (optionally to a dedicated NATS subject).
                    let alert_snapshot = alert_pipeline_swap_for_sink.load_full();
                    if let Some(alert_pipeline) = alert_snapshot.as_ref() {
                        let now = chrono::Utc::now().timestamp();
                        let out = {
                            let mut st = alert_state_for_sink
                                .write()
                                .unwrap_or_else(|e| e.into_inner());
                            if let Some(ring) = capture_ring_for_sink.as_ref() {
                                let mut ring = ring.lock().unwrap_or_else(|e| e.into_inner());
                                ring.expire(now, capture_metrics.as_ref());
                            }
                            alert_pipeline.tick(&mut st, now, alert_pipeline_metrics.as_ref())
                        };
                        for line in out.dedup_lines {
                            dispatcher
                                .dispatch_incident(IncidentEnvelope::new(line.to_string(), None))
                                .await;
                        }
                        let subject =
                            alert_pipeline.incident_nats_subject().map(|s| s.to_string());
                        for incident in out.incidents {
                            match serde_json::to_string(&incident) {
                                Ok(json) => {
                                    let mut env = FormattedIncidentEnvelope::new(
                                        IncidentEnvelope::new(json, subject.clone()),
                                    );
                                    if ocsf_incidents {
                                        let finding =
                                            rsigma_runtime::ocsf::incident_finding(&incident);
                                        match serde_json::to_string(&finding) {
                                            Ok(line) => {
                                                env = env.with_line(SinkFormat::Ocsf, line);
                                            }
                                            Err(e) => {
                                                tracing::warn!(error = %e, "Failed to serialize OCSF incident finding");
                                            }
                                        }
                                    }
                                    dispatcher.dispatch_formatted_incident(env).await;
                                }
                                Err(e) => {
                                    tracing::warn!(error = %e, "Failed to serialize incident");
                                }
                            }
                        }
                    }

                    // Age out risk entities whose windows have fully elapsed and
                    // refresh the risk gauges. A no-op when no accumulator is
                    // configured.
                    let risk_snapshot = risk_swap_for_sink.load_full();
                    if let Some(risk_layer) = risk_snapshot.as_ref() {
                        let now = chrono::Utc::now().timestamp();
                        let mut st = risk_state_for_sink
                            .write()
                            .unwrap_or_else(|e| e.into_inner());
                        risk_layer.tick(&mut st, now, risk_metrics.as_ref());
                    }
                }
            }
        }
        dispatcher.shutdown().await;
    });

    // DLQ writer task: writes DLQ entries to the configured DLQ sink.
    let dlq_metrics = metrics.clone();
    let dlq_handle = tokio::spawn(async move {
        tracing::debug!("DLQ task started");
        let mut dlq_sink = dlq_sink;
        let mut no_sink_logged = false;
        while let Some(entry) = dlq_rx.recv().await {
            dlq_metrics.dlq_events.inc();
            if let Some(ref mut sink) = dlq_sink {
                let json = serde_json::to_string(&entry).unwrap_or_default();
                if let Err(e) = sink.send_raw(&json).await {
                    tracing::warn!(error = %e, "Failed to write to DLQ sink");
                }
            } else if !no_sink_logged {
                tracing::debug!("DLQ entry counted but no sink configured (use --dlq to persist)");
                no_sink_logged = true;
            }
        }
        tracing::debug!("DLQ task finished");
    });

    // Ack task: resolves ack tokens after the sink confirms delivery.
    // For NATS tokens, extracts the stream sequence before acking to maintain
    // the high-water mark used by the state saver.
    #[cfg(feature = "daemon-nats")]
    let (ack_hw_seq, ack_hw_ts) = (high_water_seq.clone(), high_water_ts.clone());
    let ack_handle = tokio::spawn(async move {
        while let Some(token) = ack_rx.recv().await {
            #[cfg(feature = "daemon-nats")]
            if let Some((seq, ts)) = token.nats_stream_position() {
                ack_hw_seq.fetch_max(seq, Ordering::Relaxed);
                ack_hw_ts.fetch_max(ts, Ordering::Relaxed);
            }
            token.ack().await;
        }
    });

    let drain_duration = std::time::Duration::from_secs(config.drain_timeout);

    // Build the unified axum router: with `daemon-otlp`, the OTLP/gRPC
    // service is folded into the same axum::Router via Tonic's
    // `Routes::into_axum_router`. axum::serve handles HTTP/1 and HTTP/2
    // (including h2c for plaintext gRPC) via hyper-util's auto::Builder.
    #[cfg(feature = "daemon-otlp")]
    let unified_app: axum::Router = otlp_routes.into_axum_router();
    #[cfg(not(feature = "daemon-otlp"))]
    let unified_app: axum::Router = app;

    let mut serve_handle = match bound {
        BoundListener::Tcp(listener) => {
            #[cfg(feature = "daemon-tls")]
            {
                if let Some(state) = tls_state {
                    let tls_listener = super::tls::RustlsListener::new(
                        listener,
                        state.config.clone(),
                        metrics.tls_active_connections.clone(),
                    );
                    let shutdown_fut = shutdown_fut.take().expect("shutdown future consumed once");
                    tokio::spawn(async move {
                        if let Err(e) = axum::serve(tls_listener, unified_app)
                            .with_graceful_shutdown(shutdown_fut)
                            .await
                        {
                            tracing::error!(error = %e, "server error");
                        }
                    })
                } else {
                    let shutdown_fut = shutdown_fut.take().expect("shutdown future consumed once");
                    tokio::spawn(async move {
                        if let Err(e) = axum::serve(listener, unified_app)
                            .with_graceful_shutdown(shutdown_fut)
                            .await
                        {
                            tracing::error!(error = %e, "server error");
                        }
                    })
                }
            }
            #[cfg(not(feature = "daemon-tls"))]
            {
                let shutdown_fut = shutdown_fut.take().expect("shutdown future consumed once");
                tokio::spawn(async move {
                    if let Err(e) = axum::serve(listener, unified_app)
                        .with_graceful_shutdown(shutdown_fut)
                        .await
                    {
                        tracing::error!(error = %e, "server error");
                    }
                })
            }
        }
        // A unix:// listener never terminates TLS (rejected at config time), so
        // it always serves plaintext over the socket.
        #[cfg(unix)]
        BoundListener::Unix(listener) => {
            let shutdown_fut = shutdown_fut.take().expect("shutdown future consumed once");
            tokio::spawn(async move {
                if let Err(e) = axum::serve(listener, unified_app)
                    .with_graceful_shutdown(shutdown_fut)
                    .await
                {
                    tracing::error!(error = %e, "server error");
                }
            })
        }
    };

    let shutdown_triggered = tokio::select! {
        _ = &mut serve_handle => true,
        _ = async {
            let _ = engine_handle.await;
            let _ = dispatch_handle.await;
        } => {
            tracing::info!("Streaming pipeline complete");
            serve_handle.abort();
            false
        }
    };

    if shutdown_triggered {
        tracing::info!("Shutdown signal received, draining pipeline...");

        if let Some(h) = source_handle {
            h.abort();
            tracing::info!("Source task aborted");
        }

        // Tell the engine to stop pulling new events and drain what is already
        // buffered. The stdin reader cannot be cancelled mid-read, so without
        // this the engine would block on `event_rx.recv()` (keeping the
        // evaluate→dispatch handoff open) until the drain timeout elapses,
        // falsely warning that events were lost. The engine closes its
        // receiver on this signal, drains the buffered events, then exits,
        // which lets the dispatch/sink/ack tasks finish.
        #[cfg(feature = "daemon-otlp")]
        source_done_notify.notify_one();

        let drain = async {
            let _ = sink_handle.await;
            tracing::debug!("Sink task finished");
            let _ = df_handle.await;
            tracing::debug!("DLQ bridge finished");
            let _ = dlq_handle.await;
            tracing::debug!("DLQ task finished");
            let _ = ack_handle.await;
            tracing::debug!("Ack task finished");
        };
        if tokio::time::timeout(drain_duration, drain).await.is_err() {
            tracing::warn!(
                timeout_secs = config.drain_timeout,
                "Drain timeout exceeded, some events may be lost"
            );
        }
    } else {
        let _ = sink_handle.await;
        tracing::debug!("Sink task finished");
        let _ = df_handle.await;
        tracing::debug!("DLQ bridge finished");
        let _ = dlq_handle.await;
        tracing::debug!("DLQ task finished");
        let _ = ack_handle.await;
        tracing::debug!("Ack task finished");
    }

    // Save state on shutdown
    if let Some(ref store) = state_store
        && let Some(snapshot) = processor.export_state()
    {
        let position = source_position_from_atomics(&high_water_seq, &high_water_ts);
        match store.save(&snapshot, position.as_ref()).await {
            Ok(()) => {
                if let Some(ref pos) = position {
                    tracing::info!(
                        source_sequence = pos.sequence,
                        "Correlation state saved to database on shutdown"
                    );
                } else {
                    tracing::info!("Correlation state saved to database on shutdown");
                }
            }
            Err(e) => tracing::error!(error = %e, "Failed to save state on shutdown"),
        }
    }

    // Save the alert-pipeline state on shutdown when configured.
    if let Some(ref store) = state_store
        && let Some(pipeline) = alert_pipeline_swap.load_full().as_ref()
    {
        let snap = {
            let st = alert_state.read().unwrap_or_else(|e| e.into_inner());
            pipeline.snapshot(&st)
        };
        match store.save_alert_pipeline(&snap).await {
            Ok(()) => tracing::info!("Alert-pipeline state saved to database on shutdown"),
            Err(e) => {
                tracing::error!(error = %e, "Failed to save alert-pipeline state on shutdown")
            }
        }
    }

    // Save the risk-accumulator state on shutdown when configured.
    if let Some(ref store) = state_store
        && let Some(risk_layer) = risk_swap.load_full().as_ref()
    {
        let snap = {
            let st = risk_state.read().unwrap_or_else(|e| e.into_inner());
            risk_layer.snapshot(&st)
        };
        match store.save_risk(&snap).await {
            Ok(()) => tracing::info!("Risk state saved to database on shutdown"),
            Err(e) => tracing::error!(error = %e, "Failed to save risk state on shutdown"),
        }
    }

    // Save the disposition state on shutdown when the triage loop is enabled.
    if let Some(ref store) = state_store
        && let Some(dispositions) = disposition_state_for_save.as_ref()
        && let Some(snap) = dispositions.snapshot()
    {
        match store.save_dispositions(&snap).await {
            Ok(()) => tracing::info!("Disposition state saved to database on shutdown"),
            Err(e) => tracing::error!(error = %e, "Failed to save disposition state on shutdown"),
        }
    }
}

/// Wait for either SIGINT (Ctrl+C) or SIGTERM, then log and return.
///
/// The signal streams are created by the caller (before the API listener is
/// announced) and moved in here, so the OS handlers are installed eagerly and
/// a signal that races daemon startup is caught rather than killing the
/// process under the default disposition.
#[cfg(unix)]
async fn shutdown_signal(
    mut sigint: tokio::signal::unix::Signal,
    mut sigterm: tokio::signal::unix::Signal,
) {
    tokio::select! {
        _ = sigint.recv() => {}
        _ = sigterm.recv() => {}
    }
    tracing::info!("Shutdown signal received");
}

/// Wait for Ctrl+C, then log and return (non-Unix platforms).
#[cfg(not(unix))]
async fn shutdown_signal() {
    tokio::signal::ctrl_c().await.ok();
    tracing::info!("Shutdown signal received");
}

/// Split an output spec into its base (scheme + target) and `key=value` query
/// parameters: `file:///p?on_full=drop`, `otlp://host:4317?compression=gzip`.
pub(super) fn split_query(spec: &str) -> (&str, Vec<(&str, &str)>) {
    match spec.split_once('?') {
        Some((base, query)) => {
            let params = query
                .split('&')
                // Preserve a bare key with an empty value so validators can
                // reject `?format` instead of silently defaulting it.
                .map(|kv| kv.split_once('=').unwrap_or((kv, "")))
                .collect();
            (base, params)
        }
        None => (spec, Vec::new()),
    }
}

/// Which daemon channel a sink spec was declared for.
///
/// Only the findings channel carries serialized results, so only it accepts a
/// `format` parameter: the DLQ writes `DlqEntry` records and the audit sink
/// writes API audit records, neither of which is a finding.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum SinkRole {
    /// `--output` / `daemon.output.sinks`: detections, correlations, incidents.
    Findings,
    /// `--dlq` / `daemon.dlq`.
    Dlq,
    /// `daemon.audit.sink`.
    Audit,
}

impl SinkRole {
    /// The flag or config key that declared the spec, for error messages.
    fn label(self) -> &'static str {
        match self {
            SinkRole::Findings => "--output",
            SinkRole::Dlq => "--dlq",
            SinkRole::Audit => "audit.sink",
        }
    }
}

/// Resolve the wire format from query params, exiting on any spec that cannot
/// honor it.
///
/// `format` is accepted only on a line-oriented findings sink: OTLP owns its
/// log-record mapping, and the DLQ and audit channels do not carry findings at
/// all. Unlike the `on_full` warn-and-default path, an unusable `format` is a
/// startup config error, so a daemon never silently emits a format the
/// operator did not ask for.
fn format_from_params(params: &[(&str, &str)], base: &str, role: SinkRole) -> SinkFormat {
    let mut formats = params
        .iter()
        .filter(|(key, _)| *key == "format")
        .map(|(_, value)| *value);
    let Some(value) = formats.next() else {
        return SinkFormat::default();
    };
    if formats.next().is_some() {
        tracing::error!(spec = base, "format= may be specified only once");
        std::process::exit(crate::exit_code::CONFIG_ERROR);
    }

    if role != SinkRole::Findings {
        tracing::error!(
            spec = base,
            channel = role.label(),
            "format= is only supported on findings sinks"
        );
        std::process::exit(crate::exit_code::CONFIG_ERROR);
    }

    if base.starts_with("otlp") {
        tracing::error!(
            spec = base,
            "format= is not supported on OTLP sinks (they emit OTLP log records)"
        );
        std::process::exit(crate::exit_code::CONFIG_ERROR);
    }

    match value {
        "ndjson" => SinkFormat::Ndjson,
        "ocsf" => SinkFormat::Ocsf,
        other => {
            tracing::error!(
                value = other,
                "Unknown sink format (supported: ndjson, ocsf)"
            );
            std::process::exit(crate::exit_code::CONFIG_ERROR);
        }
    }
}

/// Resolve the full-queue policy from query params. Defaults to `Block`
/// (at-least-once); `on_full=drop` opts into best-effort delivery.
fn on_full_from_params(params: &[(&str, &str)]) -> OnFull {
    match params
        .iter()
        .find(|(k, _)| *k == "on_full")
        .map(|(_, v)| *v)
    {
        Some("drop") => OnFull::Drop,
        Some("block") | None => OnFull::Block,
        Some(other) => {
            tracing::warn!(value = other, "Unknown on_full value, using block");
            OnFull::Block
        }
    }
}

/// Read OTLP client TLS material referenced by `ca`, `client_cert`,
/// `client_key`, and `tls_domain` query parameters on an `otlps`/`otlphttps`
/// sink URL. A read failure is a fatal config error.
#[cfg(feature = "daemon-otlp")]
fn build_otlp_tls(params: &[(&str, &str)]) -> rsigma_runtime::OtlpClientTls {
    let read_pem = |key: &str| -> Option<Vec<u8>> {
        params.iter().find(|(k, _)| *k == key).map(|(_, path)| {
            std::fs::read(path).unwrap_or_else(|e| {
                tracing::error!(key, path, error = %e, "Failed to read OTLP TLS material");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            })
        })
    };
    rsigma_runtime::OtlpClientTls {
        ca_pem: read_pem("ca"),
        client_cert_pem: read_pem("client_cert"),
        client_key_pem: read_pem("client_key"),
        domain: params
            .iter()
            .find(|(k, _)| *k == "tls_domain")
            .map(|(_, v)| v.to_string()),
    }
}

/// Build a single Sink from an output spec string, plus its full-queue policy.
async fn build_sink(
    spec: &str,
    pretty: bool,
    role: SinkRole,
    #[cfg_attr(not(feature = "daemon-nats"), allow(unused))] config: &DaemonConfig,
) -> (Sink, OnFull) {
    let (base, params) = split_query(spec);
    let on_full = on_full_from_params(&params);
    let format = format_from_params(&params, base, role);

    if base == "stdout" || base == "stdout://" {
        return (
            Sink::Stdout(StdoutSink::new(pretty).with_format(format)),
            on_full,
        );
    }

    if let Some(path) = base.strip_prefix("file://") {
        let path = std::path::Path::new(path);
        return match FileSink::open(path) {
            Ok(file_sink) => {
                tracing::info!(path = %path.display(), "File sink opened");
                (Sink::File(file_sink.with_format(format)), on_full)
            }
            Err(e) => {
                tracing::error!(path = %path.display(), error = %e, "Failed to open file sink");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        };
    }

    #[cfg(feature = "daemon-nats")]
    if base.starts_with("nats://") {
        let (url, subject) = parse_nats_url(base);
        let mut nats_cfg = config.nats_config.clone();
        nats_cfg.url = url.clone();
        return match rsigma_runtime::NatsSink::connect(&nats_cfg, &subject).await {
            Ok(nats_sink) => {
                tracing::info!(url = url, subject = subject, "NATS sink started");
                (Sink::Nats(Box::new(nats_sink.with_format(format))), on_full)
            }
            Err(e) => {
                tracing::error!(error = %e, url = url, "Failed to connect NATS sink");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        };
    }

    #[cfg(feature = "daemon-otlp")]
    {
        use rsigma_runtime::OtlpProtocol;
        // `otlps`/`otlphttps` select TLS; the plaintext forms do not. Order so
        // the longer TLS prefixes win (they are not prefixes of each other, so
        // this is just for clarity).
        let otlp = base
            .strip_prefix("otlphttps://")
            .map(|e| (OtlpProtocol::Http, e, true))
            .or_else(|| {
                base.strip_prefix("otlphttp://")
                    .map(|e| (OtlpProtocol::Http, e, false))
            })
            .or_else(|| {
                base.strip_prefix("otlps://")
                    .map(|e| (OtlpProtocol::Grpc, e, true))
            })
            .or_else(|| {
                base.strip_prefix("otlp://")
                    .map(|e| (OtlpProtocol::Grpc, e, false))
            });
        if let Some((protocol, endpoint, tls_enabled)) = otlp {
            let gzip = params
                .iter()
                .any(|(k, v)| *k == "compression" && *v == "gzip");
            let tls = tls_enabled.then(|| build_otlp_tls(&params));
            return match rsigma_runtime::OtlpSink::new(protocol, endpoint, gzip, tls) {
                Ok(sink) => {
                    tracing::info!(
                        endpoint,
                        ?protocol,
                        gzip,
                        tls = tls_enabled,
                        "OTLP sink started"
                    );
                    (Sink::Otlp(Box::new(sink)), on_full)
                }
                Err(e) => {
                    tracing::error!(endpoint, error = %e, "Failed to build OTLP sink");
                    std::process::exit(crate::exit_code::CONFIG_ERROR);
                }
            };
        }
    }

    #[cfg(unix)]
    if let Some(path) = base.strip_prefix("unix://") {
        return match rsigma_runtime::UnixSocketSink::connect(std::path::Path::new(path)).await {
            Ok(unix_sink) => {
                tracing::info!(path, "Unix socket sink started");
                (Sink::Unix(Box::new(unix_sink.with_format(format))), on_full)
            }
            Err(e) => {
                tracing::error!(path, error = %e, "Failed to connect unix socket sink");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        };
    }

    tracing::error!(
        output = base,
        "Unsupported output sink (supported: stdout, file://<path>, nats://, otlp://, otlphttp://, unix://)"
    );
    std::process::exit(crate::exit_code::CONFIG_ERROR);
}

/// Parse a nats:// URL into (server_url, subject).
///
/// Input: "nats://host:port/subject.path.>"
/// Output: ("nats://host:port", "subject.path.>")
#[cfg(feature = "daemon-nats")]
fn parse_nats_url(url: &str) -> (String, String) {
    let without_scheme = url.strip_prefix("nats://").unwrap_or(url);
    match without_scheme.find('/') {
        Some(pos) => {
            let server = format!("nats://{}", &without_scheme[..pos]);
            let subject = without_scheme[pos + 1..].to_string();
            (server, subject)
        }
        None => (format!("nats://{without_scheme}"), ">".to_string()),
    }
}

// ---------------------------------------------------------------------------
// State restore decision
// ---------------------------------------------------------------------------

/// Decide whether to restore correlation state from SQLite.
fn decide_state_restore(
    mode: StateRestoreMode,
    stored_position: Option<SourcePosition>,
    #[cfg(feature = "daemon-nats")] replay_policy: &rsigma_runtime::ReplayPolicy,
) -> bool {
    match mode {
        StateRestoreMode::ForceClear => {
            tracing::info!("State restore skipped (--clear-state)");
            false
        }
        StateRestoreMode::ForceKeep => {
            tracing::info!("State restore forced (--keep-state)");
            true
        }
        StateRestoreMode::Auto => {
            #[cfg(feature = "daemon-nats")]
            {
                use rsigma_runtime::ReplayPolicy;
                match replay_policy {
                    ReplayPolicy::Resume => true,
                    ReplayPolicy::Latest => {
                        tracing::info!("State restore skipped (--replay-from-latest starts fresh)");
                        false
                    }
                    ReplayPolicy::FromSequence(replay_seq) => match stored_position {
                        Some(pos) if *replay_seq > pos.sequence => {
                            tracing::info!(
                                replay_from = replay_seq,
                                stored_sequence = pos.sequence,
                                "Restoring state (replay starts after stored position)"
                            );
                            true
                        }
                        Some(pos) => {
                            tracing::info!(
                                replay_from = replay_seq,
                                stored_sequence = pos.sequence,
                                "State restore skipped (replay starts at or before stored position, would double-count)"
                            );
                            false
                        }
                        None => {
                            tracing::info!(
                                "State restore skipped (no stored position to compare against replay)"
                            );
                            false
                        }
                    },
                    ReplayPolicy::FromTime(replay_time) => {
                        let replay_ts = replay_time.unix_timestamp();
                        match stored_position {
                            Some(pos) if replay_ts > pos.timestamp => {
                                tracing::info!(
                                    replay_from_ts = replay_ts,
                                    stored_ts = pos.timestamp,
                                    "Restoring state (replay starts after stored timestamp)"
                                );
                                true
                            }
                            Some(pos) => {
                                tracing::info!(
                                    replay_from_ts = replay_ts,
                                    stored_ts = pos.timestamp,
                                    "State restore skipped (replay starts at or before stored timestamp, would double-count)"
                                );
                                false
                            }
                            None => {
                                tracing::info!(
                                    "State restore skipped (no stored position to compare against replay)"
                                );
                                false
                            }
                        }
                    }
                }
            }
            #[cfg(not(feature = "daemon-nats"))]
            {
                let _ = stored_position;
                true
            }
        }
    }
}

/// Refresh the `rsigma_tls_certificate_expiry_seconds` gauge to the
/// number of seconds between now and `expiry_unix`. Called at startup
/// and after every SIGHUP-triggered cert reload.
#[cfg(feature = "daemon-tls")]
pub(crate) fn update_tls_metrics(metrics: &Metrics, expiry_unix: i64) {
    let now = chrono::Utc::now().timestamp();
    let delta = (expiry_unix - now) as f64;
    metrics.tls_certificate_expiry_seconds.set(delta);
}

/// Emit a single WARN if the active certificate expires within 30 days.
/// Operators wire this into existing log-based alerting; the Prometheus
/// gauge handles the longer-horizon dashboards.
#[cfg(feature = "daemon-tls")]
pub(crate) fn warn_if_cert_expiring_soon(expiry_unix: i64) {
    const WARN_WINDOW_SECS: i64 = 30 * 24 * 3600;
    let now = chrono::Utc::now().timestamp();
    let remaining = expiry_unix - now;
    if remaining < 0 {
        tracing::warn!(
            expiry_unix,
            "TLS server certificate has already expired; clients will reject the handshake"
        );
    } else if remaining < WARN_WINDOW_SECS {
        let days = remaining / 86400;
        tracing::warn!(
            expiry_unix,
            days_remaining = days,
            "TLS server certificate expires in less than 30 days; rotate it soon"
        );
    }
}

/// Build a `SourcePosition` from the high-water mark atomics.
/// Returns `None` if no NATS messages have been acked yet (sequence == 0).
fn source_position_from_atomics(seq: &AtomicU64, ts: &AtomicI64) -> Option<SourcePosition> {
    let s = seq.load(Ordering::Relaxed);
    if s == 0 {
        return None;
    }
    Some(SourcePosition {
        sequence: s,
        timestamp: ts.load(Ordering::Relaxed),
    })
}

// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------

async fn healthz() -> impl IntoResponse {
    Json(serde_json::json!({ "status": "ok" }))
}

async fn readyz(State(state): State<AppState>) -> Response {
    if state.health.is_ready() {
        (
            StatusCode::OK,
            Json(serde_json::json!({ "status": "ready", "rules_loaded": true })),
        )
            .into_response()
    } else {
        (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(serde_json::json!({ "status": "not_ready", "rules_loaded": false })),
        )
            .into_response()
    }
}

async fn metrics_handler(State(state): State<AppState>) -> impl IntoResponse {
    state
        .metrics
        .uptime_seconds
        .set(state.start_time.elapsed().as_secs_f64());

    if let Some(observer) = state.field_observer.as_ref() {
        let snapshot = observer.snapshot();
        state.metrics.update_field_observer_metrics(&snapshot);
    }

    if let Some(observer) = state.schema_observer.as_ref() {
        let snapshot = observer.snapshot();
        state.metrics.update_schema_observer_metrics(&snapshot);
    }

    state.metrics.update_logsource_metrics(
        state.processor.logsource_pruned_total(),
        state.processor.logsource_absent_total(),
    );
    state
        .metrics
        .update_schema_pruning_metrics(&state.processor.schema_pruning_summary());

    (
        [(
            axum::http::header::CONTENT_TYPE,
            "text/plain; version=0.0.4; charset=utf-8",
        )],
        state.metrics.encode(),
    )
}

async fn list_rules(State(state): State<AppState>) -> impl IntoResponse {
    let stats = state.processor.stats();
    Json(serde_json::json!({
        "detection_rules": stats.detection_rules,
        "correlation_rules": stats.correlation_rules,
        "rules_path": state.processor.rules_path().display().to_string(),
    }))
}

#[derive(Serialize)]
struct StatusResponse {
    status: String,
    detection_rules: usize,
    correlation_rules: usize,
    correlation_state_entries: usize,
    events_processed: u64,
    detection_matches: u64,
    correlation_matches: u64,
    uptime_seconds: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    dynamic_sources: Option<DynamicSourcesSummary>,
}

#[derive(Serialize)]
struct DynamicSourcesSummary {
    total: usize,
    resolves_total: u64,
    errors_total: u64,
    cache_hits: u64,
}

/// `GET /api/v1/incidents` — open incidents from the grouping stage.
async fn list_incidents(State(state): State<AppState>) -> impl IntoResponse {
    let include = state
        .alert_pipeline_swap
        .load_full()
        .as_ref()
        .as_ref()
        .and_then(|p| p.incident_include())
        .unwrap_or(IncludeMode::Refs);
    let incidents = {
        let st = state.alert_state.read().unwrap_or_else(|e| e.into_inner());
        st.incidents.snapshot(include)
    };
    let count = incidents.len();
    Json(serde_json::json!({ "incidents": incidents, "count": count }))
}

/// The 503 body returned by the per-incident routes when grouping is off.
///
/// Grouping is an optional stage, so an unconfigured daemon has no incidents to
/// address rather than an incident that is missing. Reserving 404 for the
/// latter keeps "you asked for the wrong id" apart from "this daemon does not
/// do incidents".
fn grouping_disabled() -> Response {
    (
        StatusCode::SERVICE_UNAVAILABLE,
        Json(serde_json::json!({
            "error": "incident grouping disabled",
            "hint": "configure an alert pipeline with a `group:` stage to enable the per-incident routes",
        })),
    )
        .into_response()
}

/// The include mode of the configured grouping stage, or `None` when the alert
/// pipeline has no grouping stage.
fn incident_include(state: &AppState) -> Option<IncludeMode> {
    state
        .alert_pipeline_swap
        .load_full()
        .as_ref()
        .as_ref()
        .and_then(|p| p.incident_include())
}

/// `GET /api/v1/incidents/{id}` — one open incident, in the same shape as a
/// list entry.
async fn get_incident(
    State(state): State<AppState>,
    axum::extract::Path(id): axum::extract::Path<String>,
) -> Response {
    let Some(include) = incident_include(&state) else {
        return grouping_disabled();
    };
    match snapshot_incident(&state, &id, include) {
        Some(incident) => Json(incident).into_response(),
        None => incident_not_found(&id),
    }
}

/// Query parameters for `GET /api/v1/incidents/{id}/bundle`.
#[derive(serde::Deserialize)]
struct BundleQuery {
    /// `json` (default) or `markdown`.
    format: Option<String>,
}

/// `GET /api/v1/incidents/{id}/bundle` — the incident joined to its rules'
/// documentation and the risk entities it overlaps.
async fn get_incident_bundle(
    State(state): State<AppState>,
    axum::extract::Path(id): axum::extract::Path<String>,
    Query(query): Query<BundleQuery>,
) -> Response {
    let format = match query.format.as_deref() {
        None => BundleFormat::Json,
        Some(requested) => match BundleFormat::parse(requested) {
            Some(format) => format,
            None => {
                return (
                    StatusCode::BAD_REQUEST,
                    Json(serde_json::json!({
                        "error": format!("unknown bundle format `{requested}`"),
                        "hint": "supported formats are `json` and `markdown`",
                    })),
                )
                    .into_response();
            }
        },
    };

    let Some(include) = incident_include(&state) else {
        return grouping_disabled();
    };
    let Some(incident) = snapshot_incident(&state, &id, include) else {
        return incident_not_found(&id);
    };
    if incident.bundle_ready == Some(false) {
        return (
            StatusCode::CONFLICT,
            Json(serde_json::json!({
                "error": "incident is still batching",
                "hint": "the incident has not cleared `group_wait`, so its contents can still \
                         change; retry once it has been reported",
                "incident_id": id,
            })),
        )
            .into_response();
    }

    // Each component is read in turn and released before the next is taken, so
    // assembling a bundle can never hold two of the daemon's locks at once.
    let keys: Vec<&str> = incident.rule_counts.keys().map(String::as_str).collect();
    let metadata = state.processor.rule_metadata_batch(keys);

    let risk_snapshot = state.risk_swap.load_full();
    let risk = risk_snapshot
        .as_ref()
        .as_ref()
        .and_then(|l| l.incident_config())
        .map(|cfg| {
            let now = chrono::Utc::now().timestamp();
            let st = state.risk_state.read().unwrap_or_else(|e| e.into_inner());
            st.views(cfg, now)
        });

    let generated_at = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
    let bundle = super::bundle::build(incident, &metadata, risk.as_deref(), generated_at);

    let body = match format {
        BundleFormat::Json => match serde_json::to_string_pretty(&bundle) {
            Ok(json) => json,
            Err(e) => {
                return (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(serde_json::json!({ "error": format!("failed to render bundle: {e}") })),
                )
                    .into_response();
            }
        },
        BundleFormat::Markdown => super::bundle::render_markdown(&bundle),
    };
    ([(header::CONTENT_TYPE, format.content_type())], body).into_response()
}

fn snapshot_incident(state: &AppState, id: &str, include: IncludeMode) -> Option<IncidentResult> {
    let st = state.alert_state.read().unwrap_or_else(|e| e.into_inner());
    st.incidents.snapshot_one(id, include)
}

fn incident_not_found(id: &str) -> Response {
    (
        StatusCode::NOT_FOUND,
        Json(serde_json::json!({
            "error": "no such open incident",
            "hint": "the id is unknown, or the incident has already resolved and been evicted",
            "incident_id": id,
        })),
    )
        .into_response()
}

/// `GET /api/v1/risk` — open entities tracked by the risk accumulator, with
/// their current window score, distinct tactic count, source count, and window
/// bounds. Empty when no risk accumulator is configured.
async fn list_risk_entities(State(state): State<AppState>) -> impl IntoResponse {
    let snapshot = state.risk_swap.load_full();
    let entities = match snapshot.as_ref().as_ref().and_then(|l| l.incident_config()) {
        Some(cfg) => {
            let now = chrono::Utc::now().timestamp();
            let st = state.risk_state.read().unwrap_or_else(|e| e.into_inner());
            st.views(cfg, now)
        }
        None => Vec::new(),
    };
    let count = entities.len();
    Json(serde_json::json!({ "entities": entities, "count": count }))
}

/// `GET /api/v1/correlations` — the compiled correlation list with per-group
/// counts (no window contents). Empty when the engine has no correlation rules.
async fn list_correlations(State(state): State<AppState>) -> impl IntoResponse {
    let correlations = state
        .processor
        .introspect_correlations(None, None)
        .map(|s| s.correlations)
        .unwrap_or_default();
    let count = correlations.len();
    Json(serde_json::json!({ "correlations": correlations, "count": count }))
}

/// Query parameters for `GET /api/v1/correlations/state`.
#[derive(serde::Deserialize)]
struct CorrelationStateQuery {
    /// Keep only the correlation whose id, name, or title equals this value.
    id: Option<String>,
    /// Keep only groups whose rendered key (`field=value, ...`) contains this
    /// substring.
    group: Option<String>,
}

/// `GET /api/v1/correlations/state` — the live per-group window snapshot
/// (current aggregate vs threshold, window entries, last alert, seconds to
/// eviction), optionally filtered by `?id=` and `?group=`. Empty when the
/// engine has no correlation rules.
async fn correlations_state(
    State(state): State<AppState>,
    axum::extract::Query(query): axum::extract::Query<CorrelationStateQuery>,
) -> impl IntoResponse {
    let snapshot = state
        .processor
        .introspect_correlations(query.id.as_deref(), query.group.as_deref());
    match snapshot {
        Some(s) => {
            let count = s.groups.len();
            Json(serde_json::json!({
                "correlations": s.correlations,
                "groups": s.groups,
                "count": count,
            }))
        }
        None => Json(serde_json::json!({ "correlations": [], "groups": [], "count": 0 })),
    }
}

/// Build the `503` body returned by the disposition endpoints when the triage
/// feedback loop is disabled.
fn dispositions_disabled() -> Response {
    (
        StatusCode::SERVICE_UNAVAILABLE,
        Json(serde_json::json!({
            "error": "dispositions disabled",
            "hint": "restart the daemon with --enable-dispositions (or daemon.dispositions.enabled: true)"
        })),
    )
        .into_response()
}

/// Build the `503` body returned by `GET /api/v1/audit` when the audit trail
/// is disabled (no `--state-db`, or explicitly turned off in config).
fn audit_disabled() -> Response {
    (
        StatusCode::SERVICE_UNAVAILABLE,
        Json(serde_json::json!({
            "error": "audit trail disabled",
            "hint": "restart the daemon with --state-db (audit auto-enables; disable with daemon.api.audit.enabled: false)"
        })),
    )
        .into_response()
}

#[derive(Debug, Deserialize)]
struct AuditQuery {
    limit: Option<u64>,
    offset: Option<u64>,
    since: Option<i64>,
    until: Option<i64>,
}

/// `GET /api/v1/audit` — paginated control-plane mutation audit log.
async fn list_audit(
    State(state): State<AppState>,
    axum::extract::Query(query): axum::extract::Query<AuditQuery>,
) -> Response {
    let Some(audit) = state.audit.as_ref() else {
        return audit_disabled();
    };
    let limit = query.limit.unwrap_or(100).min(1000);
    let offset = query.offset.unwrap_or(0);
    match audit.query(limit, offset, query.since, query.until).await {
        Ok((count, entries)) => Json(serde_json::json!({
            "count": count,
            "entries": entries,
        }))
        .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({ "error": e })),
        )
            .into_response(),
    }
}

/// `POST /api/v1/dispositions` — ingest one or more analyst dispositions (a
/// single object, a JSON array, or NDJSON). Returns the ingest summary.
async fn ingest_dispositions(State(state): State<AppState>, body: String) -> Response {
    let Some(dispositions) = state.disposition_state.as_ref() else {
        return dispositions_disabled();
    };
    match dispositions.ingest(&body, "api") {
        Ok(summary) => (StatusCode::OK, Json(summary)).into_response(),
        Err(e) => (
            StatusCode::BAD_REQUEST,
            Json(serde_json::json!({ "error": e })),
        )
            .into_response(),
    }
}

/// `GET /api/v1/dispositions` — the per-rule false-positive ratio view.
async fn list_dispositions(State(state): State<AppState>) -> Response {
    match state.disposition_state.as_ref() {
        Some(dispositions) => Json(dispositions.view()).into_response(),
        None => dispositions_disabled(),
    }
}

/// `GET /api/v1/silences` — operator silences and their state.
async fn list_silences(State(state): State<AppState>) -> impl IntoResponse {
    let now = chrono::Utc::now().timestamp();
    let silences = {
        let st = state.alert_state.read().unwrap_or_else(|e| e.into_inner());
        st.silences.snapshot(now)
    };
    let count = silences.len();
    Json(serde_json::json!({ "silences": silences, "count": count }))
}

/// `POST /api/v1/silences` — create a silence. Returns the assigned id.
async fn create_silence(State(state): State<AppState>, body: String) -> Response {
    let spec: SilenceSpec = match serde_json::from_str(&body) {
        Ok(spec) => spec,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(serde_json::json!({ "error": format!("invalid silence JSON: {e}") })),
            )
                .into_response();
        }
    };
    let max_silences = state
        .alert_pipeline_swap
        .load_full()
        .as_ref()
        .as_ref()
        .map(|p| p.max_dynamic_silences())
        .unwrap_or(rsigma_runtime::DEFAULT_MAX_DYNAMIC_SILENCES);
    match Silence::build(spec, SilenceOrigin::Api) {
        Ok(silence) => {
            let id = silence.id().to_string();
            let added = {
                let mut st = state.alert_state.write().unwrap_or_else(|e| e.into_inner());
                st.silences.try_add(silence, max_silences)
            };
            if added {
                (
                    StatusCode::CREATED,
                    Json(serde_json::json!({ "status": "created", "id": id })),
                )
                    .into_response()
            } else {
                (
                    StatusCode::TOO_MANY_REQUESTS,
                    Json(serde_json::json!({
                        "error": format!(
                            "dynamic silence limit reached ({max_silences}); delete silences or raise max_silences"
                        )
                    })),
                )
                    .into_response()
            }
        }
        Err(e) => (
            StatusCode::BAD_REQUEST,
            Json(serde_json::json!({ "error": e.to_string() })),
        )
            .into_response(),
    }
}

/// `DELETE /api/v1/silences/{id}` — expire (remove) a silence.
async fn delete_silence(
    State(state): State<AppState>,
    axum::extract::Path(id): axum::extract::Path<String>,
) -> impl IntoResponse {
    let removed = {
        let mut st = state.alert_state.write().unwrap_or_else(|e| e.into_inner());
        st.silences.remove(&id)
    };
    if removed {
        (
            StatusCode::OK,
            Json(serde_json::json!({ "status": "deleted", "id": id })),
        )
    } else {
        (
            StatusCode::NOT_FOUND,
            Json(serde_json::json!({ "error": "no such silence", "id": id })),
        )
    }
}

async fn status(State(state): State<AppState>) -> impl IntoResponse {
    let stats = state.processor.stats();

    let dynamic_sources = state.source_resolver.as_ref().map(|_| {
        use prometheus::core::Collector;
        let resolves: u64 = state
            .metrics
            .source_resolves_total
            .collect()
            .first()
            .map(|mf| {
                mf.get_metric()
                    .iter()
                    .map(|m| m.get_counter().get_value() as u64)
                    .sum()
            })
            .unwrap_or(0);
        let errors: u64 = state
            .metrics
            .source_resolve_errors
            .collect()
            .first()
            .map(|mf| {
                mf.get_metric()
                    .iter()
                    .map(|m| m.get_counter().get_value() as u64)
                    .sum()
            })
            .unwrap_or(0);
        let cache_hits = state.metrics.source_cache_hits.get();
        let total = state
            .metrics
            .source_last_resolved
            .collect()
            .first()
            .map(|mf| mf.get_metric().len())
            .unwrap_or(0);

        DynamicSourcesSummary {
            total,
            resolves_total: resolves,
            errors_total: errors,
            cache_hits,
        }
    });

    let resp = StatusResponse {
        status: if state.health.is_ready() {
            "running".to_string()
        } else {
            "loading".to_string()
        },
        detection_rules: stats.detection_rules,
        correlation_rules: stats.correlation_rules,
        correlation_state_entries: stats.state_entries,
        events_processed: state.metrics.events_processed.get(),
        detection_matches: state.metrics.detection_matches.get(),
        correlation_matches: state.metrics.correlation_matches.get(),
        uptime_seconds: state.start_time.elapsed().as_secs_f64(),
        dynamic_sources,
    };
    Json(resp)
}

async fn trigger_reload(State(state): State<AppState>) -> impl IntoResponse {
    match state.reload_tx.try_send(()) {
        Ok(()) => (
            StatusCode::OK,
            Json(serde_json::json!({ "status": "reload_triggered" })),
        ),
        Err(_) => (
            StatusCode::TOO_MANY_REQUESTS,
            Json(serde_json::json!({ "status": "reload_already_pending" })),
        ),
    }
}

async fn list_sources(State(state): State<AppState>) -> impl IntoResponse {
    let mut sources_info = Vec::new();
    for entry in state.source_registry.entries() {
        sources_info.push(serde_json::json!({
            "source_id": entry.source.id,
            "origin": entry.origin.to_string(),
            "type": format!("{:?}", entry.source.source_type).split('{').next().unwrap_or("Unknown").trim(),
            "refresh": format!("{:?}", entry.source.refresh),
            "required": entry.source.required,
        }));
    }

    Json(serde_json::json!({ "sources": sources_info }))
}

async fn resolve_sources(State(state): State<AppState>) -> impl IntoResponse {
    use rsigma_runtime::sources::refresh::RefreshTrigger;

    let Some(tx) = &state.sources_trigger_tx else {
        return (
            StatusCode::NOT_FOUND,
            Json(serde_json::json!({ "error": "no dynamic sources configured" })),
        );
    };

    match tx.try_send(RefreshTrigger::All) {
        Ok(()) => (
            StatusCode::OK,
            Json(serde_json::json!({ "status": "resolve_triggered" })),
        ),
        Err(_) => (
            StatusCode::TOO_MANY_REQUESTS,
            Json(serde_json::json!({ "status": "resolve_already_pending" })),
        ),
    }
}

async fn resolve_source_by_id(
    State(state): State<AppState>,
    axum::extract::Path(source_id): axum::extract::Path<String>,
) -> impl IntoResponse {
    use rsigma_runtime::sources::refresh::RefreshTrigger;

    let Some(tx) = &state.sources_trigger_tx else {
        return (
            StatusCode::NOT_FOUND,
            Json(serde_json::json!({ "error": "no dynamic sources configured" })),
        );
    };

    match tx.try_send(RefreshTrigger::Single(source_id.clone())) {
        Ok(()) => (
            StatusCode::OK,
            Json(serde_json::json!({ "status": "resolve_triggered", "source_id": source_id })),
        ),
        Err(_) => (
            StatusCode::TOO_MANY_REQUESTS,
            Json(serde_json::json!({ "status": "resolve_already_pending" })),
        ),
    }
}

async fn invalidate_source_cache(
    State(state): State<AppState>,
    axum::extract::Path(source_id): axum::extract::Path<String>,
) -> impl IntoResponse {
    let Some(resolver) = &state.source_resolver else {
        return (
            StatusCode::NOT_FOUND,
            Json(serde_json::json!({ "error": "no dynamic sources configured" })),
        );
    };

    resolver.cache().invalidate(&source_id);
    (
        StatusCode::OK,
        Json(serde_json::json!({ "status": "invalidated", "source_id": source_id })),
    )
}

// ---------------------------------------------------------------------------
// Field observability handlers
// ---------------------------------------------------------------------------

/// Default `?limit=` value for the paginated `/api/v1/fields/*` endpoints.
const FIELDS_DEFAULT_LIMIT: usize = 100;
/// Hard upper bound on `?limit=` to keep response payloads bounded even
/// when an operator asks for everything.
const FIELDS_MAX_LIMIT: usize = 1000;
/// Maximum number of rule titles surfaced per missing-field entry. The
/// API also reports `truncated: true` when a field carries more.
const MISSING_RULE_TITLES_CAP: usize = 10;

#[derive(serde::Deserialize, Default)]
struct FieldsQuery {
    limit: Option<usize>,
    offset: Option<usize>,
}

impl FieldsQuery {
    fn limit(&self) -> usize {
        self.limit
            .unwrap_or(FIELDS_DEFAULT_LIMIT)
            .min(FIELDS_MAX_LIMIT)
    }
    fn offset(&self) -> usize {
        self.offset.unwrap_or(0)
    }
}

/// Parse the `--on-unknown` policy string, exiting on an invalid value.
fn parse_on_unknown_policy(s: &str) -> OnUnknown {
    match s.to_ascii_lowercase().as_str() {
        "warn" => OnUnknown::Warn,
        "drop" => OnUnknown::Drop,
        "passthrough" => OnUnknown::Passthrough,
        "error" => OnUnknown::Error,
        other => {
            eprintln!(
                "Invalid --on-unknown policy '{other}' (expected warn, drop, passthrough, or error)"
            );
            std::process::exit(crate::exit_code::CONFIG_ERROR);
        }
    }
}

/// Build a [`RoutingSpec`] from the schema config (signatures + routing
/// bindings) for the daemon's `RuntimeEngine` to (re)build its router from.
fn build_routing_spec(
    schema_config: Option<&Path>,
    on_unknown_override: Option<&str>,
    partition_rules: bool,
) -> RoutingSpec {
    let (signatures, routing) = match schema_config {
        Some(path) => match load_schema_config(path) {
            Ok(v) => v,
            Err(e) => {
                eprintln!("Error loading schema config: {e}");
                std::process::exit(crate::exit_code::CONFIG_ERROR);
            }
        },
        None => (Vec::new(), None),
    };

    let classifier = if signatures.is_empty() {
        SchemaClassifier::builtin()
    } else {
        SchemaClassifier::with_user_signatures(signatures)
    };

    let mut routing = routing.unwrap_or_default();
    if let Some(policy) = on_unknown_override {
        routing.on_unknown = parse_on_unknown_policy(policy);
    }
    if routing.bindings.is_empty() {
        tracing::warn!(
            "schema routing is on but no routing bindings are configured; every event routes to the default pipeline-set"
        );
    }

    let plan = RoutingPlan::from_config(&routing);
    let pipeline_sets: Vec<Vec<Pipeline>> = plan
        .pipeline_sets()
        .iter()
        .map(|names| {
            let paths: Vec<PathBuf> = names.iter().map(PathBuf::from).collect();
            crate::load_pipelines(&paths)
        })
        .collect();

    RoutingSpec {
        classifier,
        plan,
        pipeline_sets,
        partition_rules,
    }
}

fn observation_disabled_response() -> Response {
    (
        StatusCode::SERVICE_UNAVAILABLE,
        Json(serde_json::json!({
            "error": "field observation disabled",
            "hint": "restart the daemon with --observe-fields to enable /api/v1/fields/*",
        })),
    )
        .into_response()
}

/// `GET /api/v1/schemas`: the live per-schema breakdown and unknown rate from
/// the opt-in schema observer. Returns 503 when `--observe-schemas` is off.
async fn schemas_full(State(state): State<AppState>) -> Response {
    let Some(observer) = state.schema_observer.as_ref() else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(serde_json::json!({
                "error": "schema observation disabled",
                "hint": "restart the daemon with --observe-schemas to enable /api/v1/schemas",
            })),
        )
            .into_response();
    };
    let snapshot = observer.snapshot();
    state.metrics.update_schema_observer_metrics(&snapshot);

    let by_schema: Vec<serde_json::Value> = snapshot
        .by_schema
        .iter()
        .map(|e| serde_json::json!({ "schema": e.schema, "count": e.count }))
        .collect();

    let unknown_shapes: Vec<serde_json::Value> = snapshot
        .unknown_shapes
        .iter()
        .map(|s| serde_json::json!({ "keys": s.keys, "count": s.count }))
        .collect();

    let routing_pruning: Vec<serde_json::Value> = state
        .processor
        .schema_pruning_summary()
        .iter()
        .map(|p| {
            serde_json::json!({
                "schema": p.schema,
                "eligible": p.eligible,
                "pruned": p.pruned,
            })
        })
        .collect();

    let body = serde_json::json!({
        "summary": {
            "events_observed": snapshot.events_observed,
            "classified": snapshot.classified,
            "unknown": snapshot.unknown,
            "ambiguous": snapshot.ambiguous,
            "uptime_seconds": snapshot.uptime_seconds,
        },
        "by_schema": by_schema,
        "unknown_shapes": unknown_shapes,
        "routing_pruning": routing_pruning,
    });

    (StatusCode::OK, Json(body)).into_response()
}

/// `GET /api/v1/schemas/suggestions`: mine the redacted unknown-shape sample
/// into candidate schema signatures for review. The sample is keys-only, so
/// proposals use presence predicates (`source: keys-only`); the offline
/// `engine discover-schemas` command mines a raw corpus for value markers.
/// Returns 503 when `--observe-schemas` is off.
async fn schema_suggestions(State(state): State<AppState>) -> Response {
    let Some(observer) = state.schema_observer.as_ref() else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(serde_json::json!({
                "error": "schema observation disabled",
                "hint": "restart the daemon with --discover-schemas to enable \
                         /api/v1/schemas/suggestions",
            })),
        )
            .into_response();
    };
    if !observer.discovery_sampling() {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(serde_json::json!({
                "error": "schema discovery sampling disabled",
                "hint": "restart the daemon with --discover-schemas to sample unrecognized \
                         event shapes for /api/v1/schemas/suggestions",
            })),
        )
            .into_response();
    }
    let snapshot = observer.snapshot();
    // Refreshes the per-schema counters and the unknown-cluster gauge.
    state.metrics.update_schema_observer_metrics(&snapshot);

    let report = rsigma_eval::mine_shapes(
        &snapshot.unrecognized_shapes,
        &rsigma_eval::DiscoveryConfig::default(),
    );
    let candidates: Vec<serde_json::Value> = report
        .candidates
        .iter()
        .map(|c| {
            serde_json::json!({
                "name": c.name,
                "specificity": c.specificity,
                "source": "keys-only",
                "support": c.support,
                "coverage_of_unknown": c.coverage_of_unknown,
                "predicates": c.predicate_descriptions(),
                "sample_field_sets": c.sample_field_sets,
                "overlap_warnings": c.overlap_warnings,
            })
        })
        .collect();

    let body = serde_json::json!({
        "summary": {
            "events_mined": report.stats.events_mined,
            "shapes": report.stats.shapes,
            "clusters": report.stats.clusters,
            "candidates": report.stats.candidates,
        },
        "candidates": candidates,
        "signatures_yaml": report.to_signatures_yaml(),
    });

    (StatusCode::OK, Json(body)).into_response()
}

/// `DELETE /api/v1/schemas`: reset the schema observer's counters and samples
/// (per-schema counts, unknown shapes, and the discovery sample), so a
/// long-running daemon can refresh a stale discovery sample without a restart.
/// Returns 503 when `--observe-schemas` is off.
async fn schemas_reset(State(state): State<AppState>) -> Response {
    let Some(observer) = state.schema_observer.as_ref() else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(serde_json::json!({
                "error": "schema observation disabled",
                "hint": "restart the daemon with --observe-schemas to enable /api/v1/schemas",
            })),
        )
            .into_response();
    };
    let (previous_classified, previous_unknown) = observer.reset();
    let snapshot = observer.snapshot();
    state.metrics.update_schema_observer_metrics(&snapshot);
    (
        StatusCode::OK,
        Json(serde_json::json!({
            "status": "reset",
            "previous_classified": previous_classified,
            "previous_unknown": previous_unknown,
        })),
    )
        .into_response()
}

fn missing_field_payload(field: &str, origin: &rsigma_eval::FieldOrigin) -> serde_json::Value {
    let mut rule_titles: Vec<&str> = origin.rule_titles.iter().map(String::as_str).collect();
    let total = rule_titles.len();
    let truncated = total > MISSING_RULE_TITLES_CAP;
    rule_titles.truncate(MISSING_RULE_TITLES_CAP);
    let sources: Vec<&str> = origin.sources.iter().map(|s| s.as_str()).collect();
    serde_json::json!({
        "field": field,
        "rule_count": total,
        "sources": sources,
        "rule_titles": rule_titles,
        "truncated": truncated,
    })
}

/// Slice a window out of `items` by moving the page elements out of the
/// source `Vec` rather than cloning. Returns the page, the original
/// total (preserved before the move), and the next offset (if any).
///
/// Consumes `items` because all four field endpoints construct the
/// list fresh from a snapshot and then discard the rest; cloning each
/// `serde_json::Value` just to throw the leftovers away wastes work
/// proportional to `total - limit`.
fn paginate<T>(mut items: Vec<T>, offset: usize, limit: usize) -> (Vec<T>, usize, Option<usize>) {
    let total = items.len();
    if offset >= total {
        return (Vec::new(), total, None);
    }
    let end = offset.saturating_add(limit).min(total);
    items.truncate(end);
    let page: Vec<T> = items.drain(offset..).collect();
    let next_offset = if end < total { Some(end) } else { None };
    (page, total, next_offset)
}

async fn fields_full(
    State(state): State<AppState>,
    axum::extract::Query(query): axum::extract::Query<FieldsQuery>,
) -> Response {
    let Some(observer) = state.field_observer.as_ref() else {
        return observation_disabled_response();
    };
    let snapshot = observer.snapshot();
    state.metrics.update_field_observer_metrics(&snapshot);

    let rule_field_set = state.processor.rule_field_set();
    let coverage = snapshot.coverage(&rule_field_set);

    let unknown_entries: Vec<serde_json::Value> = coverage
        .unknown
        .iter()
        .map(|e| {
            let field: &str = &e.field;
            serde_json::json!({ "field": field, "count": e.count })
        })
        .collect();
    let missing_entries: Vec<serde_json::Value> = coverage
        .missing
        .iter()
        .map(|(name, origin)| missing_field_payload(name, origin))
        .collect();
    let intersection_count = coverage.intersection_count;

    let (unknown_page, unknown_total, unknown_next) =
        paginate(unknown_entries, query.offset(), query.limit());
    let (missing_page, missing_total, missing_next) =
        paginate(missing_entries, query.offset(), query.limit());

    let body = serde_json::json!({
        "summary": {
            "events_observed": snapshot.events_observed,
            "unique_keys_observed": snapshot.unique_keys,
            "rule_fields_loaded": rule_field_set.len(),
            "overflow_dropped": snapshot.overflow_dropped,
            "max_keys": snapshot.max_keys,
            "uptime_seconds": snapshot.uptime_seconds,
            "intersection_count": intersection_count,
            "unknown_count": unknown_total,
            "missing_count": missing_total,
        },
        "unknown": {
            "items": unknown_page,
            "total": unknown_total,
            "offset": query.offset(),
            "limit": query.limit(),
            "next_offset": unknown_next,
        },
        "missing": {
            "items": missing_page,
            "total": missing_total,
            "offset": query.offset(),
            "limit": query.limit(),
            "next_offset": missing_next,
        },
    });

    (StatusCode::OK, Json(body)).into_response()
}

async fn fields_unknown(
    State(state): State<AppState>,
    axum::extract::Query(query): axum::extract::Query<FieldsQuery>,
) -> Response {
    let Some(observer) = state.field_observer.as_ref() else {
        return observation_disabled_response();
    };
    let snapshot = observer.snapshot();
    state.metrics.update_field_observer_metrics(&snapshot);

    let rule_field_set = state.processor.rule_field_set();

    let coverage = snapshot.coverage(&rule_field_set);
    let entries: Vec<serde_json::Value> = coverage
        .unknown
        .iter()
        .map(|e| {
            let field: &str = &e.field;
            serde_json::json!({ "field": field, "count": e.count })
        })
        .collect();
    let (page, total, next_offset) = paginate(entries, query.offset(), query.limit());
    (
        StatusCode::OK,
        Json(serde_json::json!({
            "items": page,
            "total": total,
            "offset": query.offset(),
            "limit": query.limit(),
            "next_offset": next_offset,
        })),
    )
        .into_response()
}

async fn fields_missing(
    State(state): State<AppState>,
    axum::extract::Query(query): axum::extract::Query<FieldsQuery>,
) -> Response {
    let Some(observer) = state.field_observer.as_ref() else {
        return observation_disabled_response();
    };
    let snapshot = observer.snapshot();
    state.metrics.update_field_observer_metrics(&snapshot);

    let rule_field_set = state.processor.rule_field_set();

    let coverage = snapshot.coverage(&rule_field_set);
    let entries: Vec<serde_json::Value> = coverage
        .missing
        .iter()
        .map(|(name, origin)| missing_field_payload(name, origin))
        .collect();
    let (page, total, next_offset) = paginate(entries, query.offset(), query.limit());
    (
        StatusCode::OK,
        Json(serde_json::json!({
            "items": page,
            "total": total,
            "offset": query.offset(),
            "limit": query.limit(),
            "next_offset": next_offset,
        })),
    )
        .into_response()
}

async fn fields_observer_reset(State(state): State<AppState>) -> Response {
    let Some(observer) = state.field_observer.as_ref() else {
        return observation_disabled_response();
    };
    let (previous_keys, previous_events) = observer.reset();
    let snapshot = observer.snapshot();
    state.metrics.update_field_observer_metrics(&snapshot);
    (
        StatusCode::OK,
        Json(serde_json::json!({
            "status": "reset",
            "previous_keys": previous_keys,
            "previous_events": previous_events,
        })),
    )
        .into_response()
}

/// Stream a bounded, optionally-redacted window of the live event stream as
/// chunked NDJSON (`GET /api/v1/tap`). The capture ends at `duration` or
/// `limit`, whichever comes first, and a final summary record reports the
/// captured / dropped counts so consumers can detect gaps.
async fn tap_stream(
    State(state): State<AppState>,
    axum::extract::Query(query): axum::extract::Query<super::tap::TapQuery>,
) -> Response {
    let Some(tap) = state.tap.as_ref() else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(serde_json::json!({
                "error": "event tap disabled",
                "hint": "restart the daemon with --enable-tap (or daemon.tap.enabled: true) to enable GET /api/v1/tap",
            })),
        )
            .into_response();
    };

    let params = match super::tap::parse_params(&query, tap.registry.max_duration()) {
        Ok(params) => params,
        Err(message) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(serde_json::json!({ "error": message })),
            )
                .into_response();
        }
    };

    let Some(handle) = tap.registry.register(params.stage) else {
        return (
            StatusCode::CONFLICT,
            Json(serde_json::json!({
                "error": "tap session capacity reached",
                "hint": "wait for an active tap to finish, or raise daemon.tap.max_sessions",
            })),
        )
            .into_response();
    };

    super::tap::stream_response(handle, params, tap.metrics.clone())
}

/// Stream live detections as chunked NDJSON (`GET /api/v1/detections/stream`),
/// one result per line, with optional `level` / `rule` filters. Ends at the
/// duration or limit, whichever comes first, and a final summary record
/// reports the streamed / dropped counts.
async fn detections_stream(
    State(state): State<AppState>,
    axum::extract::Query(query): axum::extract::Query<super::tail::TailQuery>,
) -> Response {
    let Some(tail) = state.tail.as_ref() else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(serde_json::json!({
                "error": "detection tail disabled",
                "hint": "restart the daemon with --enable-tail (or daemon.tail.enabled: true) to enable GET /api/v1/detections/stream",
            })),
        )
            .into_response();
    };

    let params = match super::tail::parse_params(&query) {
        Ok(params) => params,
        Err(message) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(serde_json::json!({ "error": message })),
            )
                .into_response();
        }
    };

    match super::tail::stream_response(&tail.registry, params, tail.metrics.clone()) {
        Some(response) => response,
        None => (
            StatusCode::CONFLICT,
            Json(serde_json::json!({
                "error": "tail session capacity reached",
                "hint": "wait for an active tail to finish, or raise daemon.tail.max_sessions",
            })),
        )
            .into_response(),
    }
}

/// Accept events via HTTP POST for processing.
/// Each non-empty line in the request body is parsed using the configured
/// `--input-format` and forwarded to the engine.
async fn ingest_events(State(state): State<AppState>, body: String) -> Response {
    let event_tx = match &state.event_tx {
        Some(tx) => tx,
        None => {
            return (
                StatusCode::NOT_FOUND,
                Json(serde_json::json!({
                    "error": "event ingestion not enabled (start with --input http)"
                })),
            )
                .into_response();
        }
    };

    const MAX_LINE_BYTES: usize = 1_048_576; // 1 MB

    let mut accepted = 0u64;
    for line in body.lines() {
        if line.trim().is_empty() {
            continue;
        }
        if line.len() > MAX_LINE_BYTES {
            return (
                StatusCode::PAYLOAD_TOO_LARGE,
                Json(serde_json::json!({
                    "error": "line exceeds maximum size",
                    "max_bytes": MAX_LINE_BYTES,
                })),
            )
                .into_response();
        }
        let raw_event = RawEvent {
            payload: line.to_string(),
            ack_token: AckToken::Noop,
        };
        // Mirror `spawn_source`'s accounting so the input-queue-depth and
        // back-pressure metrics are accurate for the HTTP push source too,
        // not just the pull sources (stdin/NATS).
        match event_tx.try_send(raw_event) {
            Ok(()) => state.metrics.on_input_queue_depth_change(1),
            Err(mpsc::error::TrySendError::Full(ev)) => {
                state.metrics.on_back_pressure();
                if event_tx.send(ev).await.is_err() {
                    return (
                        StatusCode::SERVICE_UNAVAILABLE,
                        Json(serde_json::json!({
                            "error": "event channel closed",
                            "accepted": accepted,
                        })),
                    )
                        .into_response();
                }
                state.metrics.on_input_queue_depth_change(1);
            }
            Err(mpsc::error::TrySendError::Closed(_)) => {
                return (
                    StatusCode::SERVICE_UNAVAILABLE,
                    Json(serde_json::json!({
                        "error": "event channel closed",
                        "accepted": accepted,
                    })),
                )
                    .into_response();
            }
        }
        accepted += 1;
    }

    (
        StatusCode::OK,
        Json(serde_json::json!({ "accepted": accepted })),
    )
        .into_response()
}

/// Accept OTLP logs via HTTP POST (protobuf or JSON encoding).
///
/// Decodes `ExportLogsServiceRequest` from the request body, flattens each
/// `LogRecord` into a JSON `RawEvent`, and forwards it to the engine pipeline.
/// Always mounted on `/v1/logs` when the `daemon-otlp` feature is compiled in.
///
/// Per the OTLP/HTTP spec, both `application/x-protobuf` and
/// `application/json` content types are supported. When no Content-Type is
/// provided, protobuf is assumed (spec default). Gzip content encoding is
/// supported and transparently decompressed.
#[cfg(feature = "daemon-otlp")]
async fn otlp_http_logs(
    State(state): State<AppState>,
    headers: axum::http::HeaderMap,
    body: axum::body::Bytes,
) -> Response {
    let content_type = headers
        .get(axum::http::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/x-protobuf")
        .to_string();

    let is_proto = content_type.starts_with("application/x-protobuf")
        || content_type.starts_with("application/protobuf");
    let is_json = content_type.starts_with("application/json");
    let encoding = if is_proto { "protobuf" } else { "json" };

    let span = tracing::debug_span!("otlp_ingest", transport = "http", encoding);
    async move {
        if !is_proto && !is_json {
            state
                .metrics
                .otlp_errors
                .with_label_values(&["http", "unsupported_content_type"])
                .inc();
            return (
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
                Json(serde_json::json!({
                    "error": format!(
                        "unsupported content-type: {content_type} \
                         (expected application/x-protobuf or application/json)"
                    )
                })),
            )
                .into_response();
        }

        let body = match otlp_maybe_decompress(&headers, body) {
            Ok(b) => b,
            Err(e) => {
                state
                    .metrics
                    .otlp_errors
                    .with_label_values(&["http", "decompression"])
                    .inc();
                return (
                    StatusCode::BAD_REQUEST,
                    Json(serde_json::json!({
                        "error": format!("decompression error: {e}")
                    })),
                )
                    .into_response();
            }
        };

        let request = if is_proto {
            use prost::Message;
            match rsigma_runtime::ExportLogsServiceRequest::decode(body) {
                Ok(req) => req,
                Err(e) => {
                    state
                        .metrics
                        .otlp_errors
                        .with_label_values(&["http", "decode"])
                        .inc();
                    return (
                        StatusCode::BAD_REQUEST,
                        Json(serde_json::json!({
                            "error": format!("protobuf decode error: {e}")
                        })),
                    )
                        .into_response();
                }
            }
        } else {
            match serde_json::from_slice::<rsigma_runtime::ExportLogsServiceRequest>(&body) {
                Ok(req) => req,
                Err(e) => {
                    state
                        .metrics
                        .otlp_errors
                        .with_label_values(&["http", "decode"])
                        .inc();
                    return (
                        StatusCode::BAD_REQUEST,
                        Json(serde_json::json!({
                            "error": format!("JSON decode error: {e}")
                        })),
                    )
                        .into_response();
                }
            }
        };

        state
            .metrics
            .otlp_requests
            .with_label_values(&["http", encoding])
            .inc();

        let raw_events = rsigma_runtime::logs_request_to_raw_events(&request);
        let record_count = raw_events.len();
        state.metrics.otlp_log_records.inc_by(record_count as u64);
        tracing::debug!(record_count, "OTLP logs ingested");

        for event in raw_events {
            if state.otlp_event_tx.send(event).await.is_err() {
                state
                    .metrics
                    .otlp_errors
                    .with_label_values(&["http", "channel_closed"])
                    .inc();
                return (
                    StatusCode::SERVICE_UNAVAILABLE,
                    Json(serde_json::json!({
                        "error": "event channel closed"
                    })),
                )
                    .into_response();
            }
            state.metrics.on_input_queue_depth_change(1);
        }

        (
            StatusCode::OK,
            Json(serde_json::json!({
                "partialSuccess": {
                    "rejectedLogRecords": 0,
                    "errorMessage": ""
                }
            })),
        )
            .into_response()
    }
    .instrument(span)
    .await
}

#[cfg(feature = "daemon-otlp")]
fn otlp_maybe_decompress(
    headers: &axum::http::HeaderMap,
    body: axum::body::Bytes,
) -> Result<axum::body::Bytes, std::io::Error> {
    let content_encoding = headers
        .get(axum::http::header::CONTENT_ENCODING)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");

    if content_encoding == "gzip" {
        use std::io::Read;
        let mut decoder = flate2::read::GzDecoder::new(&body[..]);
        let mut decompressed = Vec::new();
        decoder.read_to_end(&mut decompressed)?;
        Ok(axum::body::Bytes::from(decompressed))
    } else {
        Ok(body)
    }
}

#[cfg(feature = "daemon-otlp")]
struct OtlpLogsGrpcService {
    event_tx: mpsc::Sender<RawEvent>,
    metrics: Arc<Metrics>,
    /// API auth table shared with the HTTP layer. The gRPC service is folded
    /// into the router after the axum auth middleware is applied, so it
    /// authenticates its own request metadata and answers with proper gRPC
    /// status codes instead of bare HTTP 401/403.
    auth: Option<super::auth::ApiAuth>,
}

#[cfg(feature = "daemon-otlp")]
#[tonic::async_trait]
impl rsigma_runtime::LogsService for OtlpLogsGrpcService {
    async fn export(
        &self,
        request: tonic::Request<rsigma_runtime::ExportLogsServiceRequest>,
    ) -> Result<tonic::Response<rsigma_runtime::ExportLogsServiceResponse>, tonic::Status> {
        if let Some(auth) = &self.auth {
            let authorization = request
                .metadata()
                .get("authorization")
                .and_then(|v| v.to_str().ok());
            match auth.check(authorization, super::auth::EVENTS_INGEST) {
                super::auth::Decision::Allowed(_) => {}
                super::auth::Decision::Unauthorized => {
                    self.metrics
                        .api_auth_failures
                        .with_label_values(&["unauthorized"])
                        .inc();
                    return Err(tonic::Status::unauthenticated(
                        "missing or invalid bearer token",
                    ));
                }
                super::auth::Decision::Forbidden { token } => {
                    self.metrics
                        .api_auth_failures
                        .with_label_values(&["forbidden"])
                        .inc();
                    tracing::warn!(
                        token = %token,
                        "OTLP gRPC auth failure: token lacks events:ingest"
                    );
                    return Err(tonic::Status::permission_denied(
                        "token lacks required permission 'events:ingest'",
                    ));
                }
            }
        }
        let span = tracing::debug_span!("otlp_ingest", transport = "grpc", encoding = "protobuf");
        async move {
            self.metrics
                .otlp_requests
                .with_label_values(&["grpc", "protobuf"])
                .inc();

            let raw_events = rsigma_runtime::logs_request_to_raw_events(&request.into_inner());
            let record_count = raw_events.len();
            self.metrics.otlp_log_records.inc_by(record_count as u64);
            tracing::debug!(record_count, "OTLP logs ingested");

            for event in raw_events {
                self.event_tx.send(event).await.map_err(|_| {
                    self.metrics
                        .otlp_errors
                        .with_label_values(&["grpc", "channel_closed"])
                        .inc();
                    tonic::Status::unavailable("event channel closed")
                })?;
                self.metrics.on_input_queue_depth_change(1);
            }

            Ok(tonic::Response::new(
                rsigma_runtime::ExportLogsServiceResponse::default(),
            ))
        }
        .instrument(span)
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn detection_inflight_default_scales_to_five() {
        assert_eq!(default_detect_inflight(1), 1);
        assert_eq!(default_detect_inflight(2), 2);
        assert_eq!(default_detect_inflight(4), 3);
        assert_eq!(default_detect_inflight(8), 5);
        assert_eq!(default_detect_inflight(16), 5);
    }

    #[cfg(feature = "daemon-otlp")]
    mod otlp_grpc_auth {
        use super::super::OtlpLogsGrpcService;
        use super::*;
        use crate::daemon::auth::{ApiAuth, TokenSpec};

        fn auth_table() -> ApiAuth {
            let tokens = [
                TokenSpec {
                    name: "shipper".into(),
                    role: Some("ingest".into()),
                    permissions: None,
                    token_env: "SHIPPER".into(),
                },
                TokenSpec {
                    name: "viewer".into(),
                    role: Some("reader".into()),
                    permissions: None,
                    token_env: "VIEWER".into(),
                },
            ];
            ApiAuth::build(&[], &tokens, &[], |key| match key {
                "SHIPPER" => Some("ship-secret".into()),
                "VIEWER" => Some("view-secret".into()),
                _ => None,
            })
            .unwrap()
        }

        fn service(auth: Option<ApiAuth>) -> (OtlpLogsGrpcService, mpsc::Receiver<RawEvent>) {
            let (event_tx, event_rx) = mpsc::channel(8);
            (
                OtlpLogsGrpcService {
                    event_tx,
                    metrics: Arc::new(Metrics::new()),
                    auth,
                },
                event_rx,
            )
        }

        fn request_with_token(
            token: Option<&str>,
        ) -> tonic::Request<rsigma_runtime::ExportLogsServiceRequest> {
            let mut request =
                tonic::Request::new(rsigma_runtime::ExportLogsServiceRequest::default());
            if let Some(token) = token {
                request
                    .metadata_mut()
                    .insert("authorization", format!("Bearer {token}").parse().unwrap());
            }
            request
        }

        #[tokio::test]
        async fn grpc_export_rejects_missing_token_as_unauthenticated() {
            use rsigma_runtime::LogsService;
            let (svc, _rx) = service(Some(auth_table()));
            let status = svc.export(request_with_token(None)).await.unwrap_err();
            assert_eq!(status.code(), tonic::Code::Unauthenticated);
        }

        #[tokio::test]
        async fn grpc_export_rejects_read_token_as_permission_denied() {
            use rsigma_runtime::LogsService;
            let (svc, _rx) = service(Some(auth_table()));
            let status = svc
                .export(request_with_token(Some("view-secret")))
                .await
                .unwrap_err();
            assert_eq!(status.code(), tonic::Code::PermissionDenied);
        }

        #[tokio::test]
        async fn grpc_export_accepts_ingest_token() {
            use rsigma_runtime::LogsService;
            let (svc, _rx) = service(Some(auth_table()));
            let response = svc.export(request_with_token(Some("ship-secret"))).await;
            assert!(response.is_ok(), "{response:?}");
        }

        #[tokio::test]
        async fn grpc_export_open_without_auth_table() {
            use rsigma_runtime::LogsService;
            let (svc, _rx) = service(None);
            let response = svc.export(request_with_token(None)).await;
            assert!(response.is_ok(), "{response:?}");
        }
    }

    #[test]
    fn force_clear_always_skips() {
        let result = decide_state_restore(
            StateRestoreMode::ForceClear,
            Some(SourcePosition {
                sequence: 100,
                timestamp: 1000,
            }),
            #[cfg(feature = "daemon-nats")]
            &rsigma_runtime::ReplayPolicy::Resume,
        );
        assert!(!result);
    }

    #[test]
    fn force_keep_always_restores() {
        let result = decide_state_restore(
            StateRestoreMode::ForceKeep,
            None,
            #[cfg(feature = "daemon-nats")]
            &rsigma_runtime::ReplayPolicy::Latest,
        );
        assert!(result);
    }

    #[cfg(feature = "daemon-nats")]
    mod nats_auto {
        use super::*;
        use rsigma_runtime::ReplayPolicy;

        #[test]
        fn resume_restores() {
            assert!(decide_state_restore(
                StateRestoreMode::Auto,
                None,
                &ReplayPolicy::Resume,
            ));
        }

        #[test]
        fn latest_skips() {
            assert!(!decide_state_restore(
                StateRestoreMode::Auto,
                Some(SourcePosition {
                    sequence: 100,
                    timestamp: 1000,
                }),
                &ReplayPolicy::Latest,
            ));
        }

        #[test]
        fn forward_sequence_restores() {
            assert!(decide_state_restore(
                StateRestoreMode::Auto,
                Some(SourcePosition {
                    sequence: 100,
                    timestamp: 1000,
                }),
                &ReplayPolicy::FromSequence(101),
            ));
        }

        #[test]
        fn backward_sequence_skips() {
            assert!(!decide_state_restore(
                StateRestoreMode::Auto,
                Some(SourcePosition {
                    sequence: 100,
                    timestamp: 1000,
                }),
                &ReplayPolicy::FromSequence(50),
            ));
        }

        #[test]
        fn equal_sequence_skips() {
            assert!(!decide_state_restore(
                StateRestoreMode::Auto,
                Some(SourcePosition {
                    sequence: 100,
                    timestamp: 1000,
                }),
                &ReplayPolicy::FromSequence(100),
            ));
        }

        #[test]
        fn forward_time_restores() {
            let future = time::OffsetDateTime::from_unix_timestamp(2000).unwrap();
            assert!(decide_state_restore(
                StateRestoreMode::Auto,
                Some(SourcePosition {
                    sequence: 100,
                    timestamp: 1000,
                }),
                &ReplayPolicy::FromTime(future),
            ));
        }

        #[test]
        fn backward_time_skips() {
            let past = time::OffsetDateTime::from_unix_timestamp(500).unwrap();
            assert!(!decide_state_restore(
                StateRestoreMode::Auto,
                Some(SourcePosition {
                    sequence: 100,
                    timestamp: 1000,
                }),
                &ReplayPolicy::FromTime(past),
            ));
        }

        #[test]
        fn no_stored_position_skips_on_replay() {
            assert!(!decide_state_restore(
                StateRestoreMode::Auto,
                None,
                &ReplayPolicy::FromSequence(42),
            ));
        }
    }

    #[cfg(not(feature = "daemon-nats"))]
    #[test]
    fn auto_without_nats_restores() {
        assert!(decide_state_restore(StateRestoreMode::Auto, None));
    }
}