net-mesh 0.23.0

High-performance, schema-agnostic, backend-agnostic event bus
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
//! Deck SDK — Phase 1 (Rust) per
//! [`DECK_SDK_PLAN.md`](../../../../../../docs/plans/DECK_SDK_PLAN.md).
//!
//! Operator-side surface, the dual of `behavior::meshos::sdk`
//! (the daemon-author surface). Daemons author against
//! `MeshOsDaemonSdk`; operators command against `DeckClient`.
//!
//! # Phase 1 scope
//!
//! - [`DeckClient`] — composes a [`MeshOsRuntime`] with an
//!   [`OperatorIdentity`].
//! - [`OperatorIdentity`] — operator-key newtype around
//!   [`super::super::EntityKeypair`]. **Phase 1 is non-signing:**
//!   the substrate's channel-auth surface doesn't yet expose
//!   operator-key signing, so admin commits ride the local loop
//!   un-signed and the SDK records the operator id for audit
//!   correlation. The signing seam wires in when the substrate
//!   slice that adds it lands (per the plan's "substrate gaps").
//! - [`AdminCommands`] — typed methods for every
//!   [`super::meshos::AdminEvent`] variant. Each publishes the
//!   admin event onto the loop's event stream and returns a
//!   [`ChainCommit`] correlation handle.
//! - [`SnapshotStream`] — `Stream` over
//!   [`super::meshos::MeshOsSnapshotReader`] polled at a
//!   configurable cadence (defaults to the loop's tick interval).
//!
//! # Phase 1 deferrals
//!
//! - **Operator-signed commits.** The substrate's verifier doesn't
//!   yet check operator signatures; the SDK records the operator
//!   id on each commit but does not sign the event payload. Slated
//!   for the substrate slice that adds operator-key channel-auth.
//! - **Audit queries (`audit()`).** Need a signed admin chain to
//!   query against; deferred to a slice that lands after the
//!   substrate's admin-chain commit + signing path.
//! - **Log stream (`subscribe_logs()`).** Needs per-daemon /
//!   per-node log-chain binding through RedEX `tail()`; deferred.
//! - **ICE (`ice()`).** Phase 2 substrate work (`ForceDrain`,
//!   `ForceEvictReplica`, …, blast-radius simulator); Phase 3 SDK
//!   surface. Locked decision #4 of the plan: blast-radius
//!   simulation is mandatory before commit — substrate-side
//!   contract not yet written.
//!
//! # Error model
//!
//! [`DeckError`] uses the `<<deck-sdk-kind:KIND>>MSG` discriminator
//! format every cross-language SDK parses. Kinds shipped in Phase 1:
//! `unknown_node`, `chain_commit_failed`, `loop_closed`,
//! `queue_full`, `stream_closed`.
//!
//! # Example
//!
//! ```ignore
//! use net::adapter::net::behavior::deck::{DeckClient, OperatorIdentity};
//! use net::adapter::net::behavior::meshos::{MeshOsConfig, MeshOsRuntime};
//!
//! let runtime = MeshOsRuntime::start(MeshOsConfig::default(), dispatcher);
//! let identity = OperatorIdentity::generate();
//! let deck = DeckClient::from_runtime(&runtime, identity);
//!
//! let commit = deck
//!     .admin()
//!     .enter_maintenance(node_id, None)
//!     .await?;
//! tracing::info!(commit_id = commit.commit_id(), "drain proposed");
//!
//! use futures::StreamExt;
//! let mut snaps = deck.snapshots();
//! if let Some(Ok(snap)) = snaps.next().await {
//!     // …render the latest state…
//! }
//! ```

use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::{Duration, SystemTime};

use futures::Stream;
use tokio::time::{interval, Interval};

use super::meshos::{
    ice_proposal_signing_payload, simulate_ice_proposal, AdminEvent, BlastRadius, ChainId,
    IceActionProposal, MeshOsEvent, MeshOsHandle, MeshOsHandleError, MeshOsRuntime, MeshOsSnapshot,
    MeshOsSnapshotReader, NodeId,
};
use crate::adapter::net::behavior::aggregator::{AggregatorDaemon, SummaryAnnouncement};
use crate::adapter::net::identity::EntityKeypair;
use crate::adapter::net::subnet::SubnetId;
use crate::adapter::net::MeshNode;
use crate::adapter::net::{ChannelHash, Visibility};

/// Operator identity. Phase 1 holds the operator key as an
/// [`EntityKeypair`] (the same ed25519 type daemons use) plus a
/// derived 64-bit operator id. The signing seam will widen here
/// when the substrate slice that adds operator-key channel-auth
/// lands; until then commits ride the local loop and the SDK
/// records the operator id for audit correlation.
#[derive(Clone, Debug)]
pub struct OperatorIdentity {
    keypair: Arc<EntityKeypair>,
    operator_id: u64,
}

impl OperatorIdentity {
    /// Wrap an existing keypair as an operator identity. The
    /// operator id derives from the keypair's `origin_hash`.
    pub fn from_keypair(keypair: EntityKeypair) -> Self {
        let operator_id = keypair.origin_hash();
        Self {
            keypair: Arc::new(keypair),
            operator_id,
        }
    }

    /// Generate a fresh keypair + identity. Convenience for tests
    /// and the tooling that bootstraps a one-shot operator.
    pub fn generate() -> Self {
        Self::from_keypair(EntityKeypair::generate())
    }

    /// 64-bit operator id derived from the underlying keypair's
    /// `origin_hash`. Stable across the operator's lifetime.
    pub fn operator_id(&self) -> u64 {
        self.operator_id
    }

    /// Borrow the underlying keypair. **Use sparingly.** The
    /// SDK's own signing helpers
    /// ([`Self::sign_proposal`], [`Self::sign_admin_event`])
    /// cover the canonical signing flows; reach for this only
    /// when implementing a cross-language signing seam (e.g. a
    /// FFI binding that needs to call its own ed25519 lib over
    /// the same `(domain || issued_at || blast_hash || postcard)`
    /// payload shape). Calls outside that envelope risk
    /// drift between the SDK's signing bytes and what the
    /// substrate verifier rebuilds.
    pub fn keypair(&self) -> &EntityKeypair {
        &self.keypair
    }
}

/// SDK error surface. Carries the operator-readable message + a
/// stable kind discriminator usable from cross-language consumers
/// via the `<<deck-sdk-kind:KIND>>MSG` envelope.
#[derive(Clone, Debug, thiserror::Error)]
#[error("<<deck-sdk-kind:{kind}>>{message}")]
pub struct DeckError {
    /// Stable kind discriminator. Lowercase + underscore-only;
    /// cross-language SDKs parse the surrounding
    /// `<<deck-sdk-kind:…>>` envelope to extract this verbatim.
    pub kind: &'static str,
    /// Operator-readable message.
    pub message: String,
}

impl DeckError {
    fn new(kind: &'static str, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
        }
    }
}

impl From<MeshOsHandleError> for DeckError {
    fn from(err: MeshOsHandleError) -> Self {
        match err {
            MeshOsHandleError::LoopClosed => Self::new("loop_closed", "MeshOS loop has exited"),
            MeshOsHandleError::QueueFull => Self::new(
                "queue_full",
                "MeshOS source channel at capacity — back off + retry",
            ),
        }
    }
}

/// Type alias for the admin-command error surface. Shares the
/// underlying [`DeckError`] envelope; admin commits surface
/// `loop_closed` / `queue_full` kinds.
pub type AdminError = DeckError;

/// Type alias for the ICE-surface error type. Shares the
/// underlying [`DeckError`] envelope; ICE commits add the
/// `simulation_required` / `insufficient_signatures` kinds.
pub type IceError = DeckError;

/// Correlation handle returned by every admin commit. Phase 1
/// represents "the admin event was accepted by the loop's event
/// queue"; the substrate slice that adds a signed admin chain
/// will widen this to carry the chain sequence + commit hash.
///
/// Always carries the issuing operator id so audit downstream
/// (when wired) can correlate commits to the operator that
/// issued them.
#[derive(Clone, Debug)]
pub struct ChainCommit {
    commit_id: u64,
    operator_id: u64,
    event_kind: &'static str,
    committed_at: SystemTime,
}

impl ChainCommit {
    /// Process-local correlation id, monotonically increasing
    /// across every commit a single [`DeckClient`] produces.
    pub fn commit_id(&self) -> u64 {
        self.commit_id
    }

    /// Id of the operator that issued the commit.
    pub fn operator_id(&self) -> u64 {
        self.operator_id
    }

    /// Discriminator for the admin event the commit carried
    /// (e.g. `"enter_maintenance"`, `"drop_replicas"`).
    pub fn event_kind(&self) -> &'static str {
        self.event_kind
    }

    /// Wall-clock timestamp at which the SDK accepted the commit.
    /// Distinct from any per-chain commit sequence the substrate
    /// will eventually expose.
    pub fn committed_at(&self) -> SystemTime {
        self.committed_at
    }
}

/// Tunables for [`DeckClient`].
#[derive(Clone, Debug)]
pub struct DeckClientConfig {
    /// Cadence at which [`SnapshotStream`] polls the runtime's
    /// snapshot reader. Defaults to 100ms — same order of
    /// magnitude as the default loop tick so the stream surfaces
    /// each post-reconcile snapshot once.
    pub snapshot_poll_interval: Duration,
    /// Minimum operator signatures required to commit an ICE
    /// proposal (see [`SimulatedIceProposal::commit`]). Plan
    /// locks this in at 2-of-N by default,
    /// substrate-verified; this slice ships single-signature
    /// (`1`) as the SDK-side default because substrate-side
    /// multi-operator verification hasn't shipped yet. Operators
    /// who want client-enforced multi-op gating ahead of the
    /// substrate slice can bump this knob.
    pub ice_signature_threshold: usize,
}

impl Default for DeckClientConfig {
    fn default() -> Self {
        Self {
            snapshot_poll_interval: Duration::from_millis(100),
            ice_signature_threshold: 1,
        }
    }
}

/// Compact at-a-glance rollup of the runtime's latest snapshot.
/// Built by [`DeckClient::status_summary`]; designed for the
/// operator UI's "is everything OK?" header — one pass over
/// the snapshot to count each cohort, plus the two cluster-
/// wide flags ([`Self::freeze_remaining_ms`] and
/// [`Self::local_maintenance_active`]) operators care most
/// about at first glance.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct StatusSummary {
    /// Per-health-class peer counts.
    pub peers: PeerCounts,
    /// Per-lifecycle-and-restart-state daemon counts.
    pub daemons: DaemonCounts,
    /// Number of replica chains the snapshot tracks.
    pub replica_chains: usize,
    /// Number of avoid-list entries on this node.
    pub avoid_list_entries: usize,
    /// Depth of the ring of recently-emitted actions (count
    /// of entries in `MeshOsSnapshot::recently_emitted`).
    /// This is "what reconcile recently asked for," NOT "what
    /// is currently in flight" — the executor doesn't signal
    /// completion back to the loop, so the ring caps at
    /// `action_queue_capacity` and never drains on its own.
    pub recently_emitted_count: usize,
    /// Executor failure ring depth.
    pub recent_failure_count: usize,
    /// Admin audit ring depth (signed ICE bundles + unsigned
    /// admin events).
    pub admin_audit_ring_depth: usize,
    /// Milliseconds remaining on the cluster-wide ICE freeze.
    /// `None` if no freeze is in effect.
    pub freeze_remaining_ms: Option<u64>,
    /// `true` iff this node's local maintenance state is
    /// anything other than `Active`. Operators read this for
    /// "is this node in maintenance right now?".
    pub local_maintenance_active: bool,
}

/// Peer-health counts within a [`StatusSummary`].
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PeerCounts {
    /// Peers responding within the heartbeat window.
    pub healthy: usize,
    /// Peers responding but slow.
    pub degraded: usize,
    /// Peers unreachable.
    pub unreachable: usize,
    /// Peers without a health sample yet.
    pub unknown: usize,
}

/// Aggregate `SubnetGateway` counters surfaced by
/// [`DeckClient::gateway_stats`]. Plain value type so operator
/// tooling can render / serialize / diff snapshots without
/// reaching into the substrate's atomic counters.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GatewayStats {
    /// The mesh node's local subnet — same as
    /// [`DeckClient::local_subnet`], echoed here so a
    /// `gateway stats` rendering doesn't need a second
    /// accessor call.
    pub local_subnet: SubnetId,
    /// Total cross-subnet visibility decisions that resolved to
    /// "forward" (publish-fanout admitted; subscribe-gate
    /// admitted). Monotonic-increasing for the lifetime of the
    /// gateway.
    pub forwarded: u64,
    /// Total decisions that resolved to "drop." Monotonic.
    pub dropped: u64,
    /// Snapshot of every peer subnet the gateway is bridging to,
    /// sorted by raw bits. Sourced from `SubnetGateway::peer_subnets`.
    pub peer_subnets: Vec<SubnetId>,
    /// Number of explicit `(channel, target-subnets)` rules in
    /// the export table — what `gateway exports` enumerates.
    pub export_rules: u64,
}

/// One row in [`DeckClient::subnets_with_members`]'s rollup.
/// Carries the subnet, the sorted member-`node_id` set, and a
/// flag marking the local subnet so renderers don't need a
/// second pass.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubnetRollup {
    /// Subnet this row represents.
    pub subnet: SubnetId,
    /// Sorted set of `node_id`s known to belong to this subnet.
    /// Empty when the local subnet has no peers (the local node
    /// is only included if the caller supplied its node id).
    pub members: Vec<u64>,
    /// `true` when [`Self::subnet`] matches the local mesh
    /// node's subnet.
    pub is_local: bool,
}

/// One-shot snapshot returned by [`DeckClient::aggregator_snapshot`].
/// Bundles every field a renderer needs in a single struct so
/// callers don't pay for five per-field lock acquisitions per
/// frame. `summaries` is an `Arc` so the snapshot itself is
/// cheap to clone.
#[derive(Clone, Debug)]
pub struct AggregatorSnapshot {
    /// Subnet the aggregator is summarizing.
    pub source_subnet: SubnetId,
    /// `FoldKind::KIND_ID`s the aggregator is configured for.
    pub fold_kinds: Vec<u16>,
    /// Aggregator's monotonic tick counter.
    pub generation: u64,
    /// Aggregator's tick cadence.
    pub summary_interval: std::time::Duration,
    /// Buffered summaries — `Arc::clone`-cheap.
    pub summaries: Arc<Vec<SummaryAnnouncement>>,
}

/// Per-replica row in an [`AggregatorRegistryGroupSnapshot`].
/// One per replica in declaration order.
#[derive(Clone, Debug)]
pub struct AggregatorReplicaRow {
    /// Replica's monotonic tick counter.
    pub generation: u64,
    /// `true` when the replica's last tick was within
    /// `3 × summary_interval`.
    pub healthy: bool,
    /// Operator-facing diagnostic when `healthy == false`.
    pub diagnostic: Option<String>,
    /// Placement decision recorded at spawn time (only present
    /// when the group was spawned via
    /// `LifecycleGroup::spawn_with_placement`).
    pub placement_node_id: Option<u64>,
}

/// Snapshot of one registered aggregator group. Built by
/// [`DeckClient::aggregator_registry_snapshot`] and consumed by
/// `net aggregator ls` + the future Deck panel.
#[derive(Clone, Debug)]
pub struct AggregatorRegistryGroupSnapshot {
    /// Operator-chosen group name.
    pub name: String,
    /// 32-byte group seed for deterministic identity.
    pub group_seed: [u8; 32],
    /// Per-replica rows in declaration order.
    pub replicas: Vec<AggregatorReplicaRow>,
}

/// Snapshot of every aggregator group registered on the node.
#[derive(Clone, Debug, Default)]
pub struct AggregatorRegistrySnapshot {
    /// Groups in lexicographic order by name (matches the
    /// registry's `entries()` ordering).
    pub groups: Vec<AggregatorRegistryGroupSnapshot>,
}

/// Daemon counts within a [`StatusSummary`]. Lifecycle
/// counts are disjoint partitions of the registered set;
/// `crash_looping` / `backing_off` are orthogonal restart-
/// state markers that overlap with lifecycle (a `Stopped`
/// daemon can also be `BackingOff`).
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DaemonCounts {
    /// Currently running.
    pub running: usize,
    /// Start requested; awaiting confirmation.
    pub starting: usize,
    /// Stop requested; awaiting confirmation.
    pub stopping: usize,
    /// Not currently running.
    pub stopped: usize,
    /// In supervisor's BackingOff window. Orthogonal to
    /// lifecycle — a daemon in this state is typically also
    /// `Stopped`.
    pub backing_off: usize,
    /// In supervisor's CrashLooping window — has crossed the
    /// crash-loop threshold and is parked for a longer cooldown.
    pub crash_looping: usize,
}

/// Build a [`StatusSummary`] from a snapshot. One pass over
/// the per-peer and per-daemon maps; the rest of the rollup
/// is direct field reads.
fn build_status_summary(snap: &MeshOsSnapshot) -> StatusSummary {
    let mut peers = PeerCounts::default();
    for (_, peer) in snap.peers.iter() {
        match peer.health {
            Some(super::meshos::PeerHealthSnapshot::Healthy) => peers.healthy += 1,
            Some(super::meshos::PeerHealthSnapshot::Degraded) => peers.degraded += 1,
            Some(super::meshos::PeerHealthSnapshot::Unreachable) => peers.unreachable += 1,
            None => peers.unknown += 1,
        }
    }
    let mut daemons = DaemonCounts::default();
    for (_, d) in snap.daemons.iter() {
        match d.lifecycle {
            super::meshos::DaemonLifecycleSnapshot::Running => daemons.running += 1,
            super::meshos::DaemonLifecycleSnapshot::Starting => daemons.starting += 1,
            super::meshos::DaemonLifecycleSnapshot::Stopping => daemons.stopping += 1,
            super::meshos::DaemonLifecycleSnapshot::Stopped => daemons.stopped += 1,
        }
        match d.restart_state {
            super::meshos::RestartStateSnapshot::Idle => {}
            super::meshos::RestartStateSnapshot::BackingOff { .. } => daemons.backing_off += 1,
            super::meshos::RestartStateSnapshot::CrashLooping { .. } => daemons.crash_looping += 1,
        }
    }
    let maintenance_active = !matches!(
        snap.local_maintenance,
        super::meshos::MaintenanceStateSnapshot::Active
    );
    StatusSummary {
        peers,
        daemons,
        replica_chains: snap.replicas.len(),
        avoid_list_entries: snap.avoid_list.len(),
        recently_emitted_count: snap.recently_emitted.len(),
        recent_failure_count: snap.recent_failures.len(),
        admin_audit_ring_depth: snap.admin_audit.len(),
        freeze_remaining_ms: snap.freeze_remaining_ms,
        local_maintenance_active: maintenance_active,
    }
}

/// Operator-facing client. Composes a [`MeshOsHandle`] +
/// [`MeshOsSnapshotReader`] + [`OperatorIdentity`] into the
/// surface Deck-the-binary (and other operator tools) bind
/// against.
///
/// Constructed via [`Self::from_runtime`] (when the caller holds
/// the live runtime) or [`Self::new`] (when the caller already
/// has handle + reader and wants to compose explicitly).
#[derive(Clone)]
pub struct DeckClient {
    handle: MeshOsHandle,
    snapshot_reader: MeshOsSnapshotReader,
    identity: OperatorIdentity,
    config: DeckClientConfig,
    /// Wrapped in `Arc` so clones share the same counter —
    /// `commit_id`s stay monotonic across every fan-out use of
    /// a single `DeckClient`. Without this, a clone would
    /// produce its own counter and two siblings could emit
    /// colliding `commit_id`s for distinct commits.
    commit_seq: Arc<AtomicU64>,
    /// Optional operator-key registry. When present, every ICE
    /// commit verifies each [`OperatorSignature`] against the
    /// registered public key before publishing. When absent,
    /// signatures pass through unchecked — useful for local /
    /// in-process tests but unsafe for any deployment that
    /// hasn't yet wired up the substrate verifier.
    operator_registry: Option<Arc<OperatorRegistry>>,
    /// Optional reference to the running `MeshNode`. Wired in via
    /// [`Self::with_mesh`] when the operator surface needs to
    /// read subnet / gateway / channel state that doesn't live
    /// in the [`MeshOsSnapshot`]. `None` when the deck is running
    /// against a degenerate / mesh-less runtime (test harnesses,
    /// pre-attach CLI invocations); the subnet/gateway accessors
    /// gracefully report "no mesh installed" in that case.
    mesh: Option<Arc<MeshNode>>,
    /// Optional reference to a running [`AggregatorDaemon`].
    /// Wired in via [`Self::with_aggregator`] when an aggregator
    /// is running in-process alongside the deck. Powers the
    /// `AGGREGATORS` Deck panel + the
    /// `DeckClient::aggregator_*` accessors. `None` for decks
    /// that don't host an aggregator (most operator binaries —
    /// they're queriers, not hosts).
    aggregator: Option<Arc<AggregatorDaemon>>,
}

impl DeckClient {
    /// Explicit constructor. Use when the caller already holds
    /// a [`MeshOsHandle`] + [`MeshOsSnapshotReader`] (e.g. when
    /// composing with other subsystems that share the same
    /// runtime).
    pub fn new(
        handle: MeshOsHandle,
        snapshot_reader: MeshOsSnapshotReader,
        identity: OperatorIdentity,
        config: DeckClientConfig,
    ) -> Self {
        Self {
            handle,
            snapshot_reader,
            identity,
            config,
            commit_seq: Arc::new(AtomicU64::new(0)),
            operator_registry: None,
            mesh: None,
            aggregator: None,
        }
    }

    /// Install a live `MeshNode` reference so the subnet and
    /// gateway accessors ([`Self::local_subnet`],
    /// [`Self::known_subnets`], [`Self::gateway_stats`],
    /// [`Self::gateway_exports`], [`Self::channel_visibility`])
    /// can read substrate state that doesn't flow through the
    /// [`MeshOsSnapshot`] stream. Without this the accessors
    /// gracefully report "no mesh installed."
    pub fn with_mesh(mut self, mesh: Arc<MeshNode>) -> Self {
        self.mesh = Some(mesh);
        self
    }

    /// Install a live [`AggregatorDaemon`] reference. Powers the
    /// `aggregator_*` accessors + the Deck `AGGREGATORS` panel.
    pub fn with_aggregator(mut self, aggregator: Arc<AggregatorDaemon>) -> Self {
        self.aggregator = Some(aggregator);
        self
    }

    /// Convenience constructor that pulls the handle + snapshot
    /// reader off a live runtime. Borrows the runtime; the
    /// returned client outlives the borrow because both
    /// `MeshOsHandle` and `MeshOsSnapshotReader` are clone-shared.
    pub fn from_runtime(runtime: &MeshOsRuntime, identity: OperatorIdentity) -> Self {
        Self::new(
            runtime.handle_clone(),
            runtime.snapshot_reader().clone(),
            identity,
            DeckClientConfig::default(),
        )
    }

    /// Override the default [`DeckClientConfig`] on an existing
    /// client. Builder-style.
    pub fn with_config(mut self, config: DeckClientConfig) -> Self {
        self.config = config;
        self
    }

    /// Install an [`OperatorRegistry`] that gates every ICE
    /// commit on operator-signature verification. Without a
    /// registry installed, commits accept any signature bundle
    /// that meets the threshold — useful for local tests but
    /// not for deployment.
    pub fn with_operator_registry(mut self, registry: OperatorRegistry) -> Self {
        self.operator_registry = Some(Arc::new(registry));
        self
    }

    /// Borrow the installed operator registry, if any.
    pub fn operator_registry(&self) -> Option<&OperatorRegistry> {
        self.operator_registry.as_deref()
    }

    /// Borrow the operator identity.
    pub fn identity(&self) -> &OperatorIdentity {
        &self.identity
    }

    /// Build an [`AdminCommands`] surface bound to this client.
    /// Each method publishes the corresponding admin event and
    /// returns a [`ChainCommit`].
    pub fn admin(&self) -> AdminCommands<'_> {
        AdminCommands { client: self }
    }

    /// Build an [`IceCommands`] surface — the break-glass
    /// operator path. Each method returns an [`IceProposal`]
    /// that must be `simulate()`d before `commit()` per the
    /// plan's locked decision #4.
    pub fn ice(&self) -> IceCommands<'_> {
        IceCommands { client: self }
    }

    /// Build an [`AuditQuery`] reading the substrate's ICE
    /// audit ring (every `SignedIceCommit` the loop's verifier
    /// observed — accepted or rejected). Fluent builder: chain
    /// `by_operator`, `between`, `force_only`, `recent` before
    /// `collect()`. See [`AuditQuery`] for the per-method
    /// semantics + the current Phase 1 scope (ICE only — the
    /// non-force admin chain query path lands when the
    /// substrate's signed admin chain ships).
    pub fn audit(&self) -> AuditQuery<'_> {
        AuditQuery::new(self)
    }

    /// Subscribe to the substrate's executor-failure ring.
    /// Returns a [`FailureStream`] that tails each
    /// [`super::meshos::FailureRecord`] as the executor records
    /// it. Same seq-watermark dedup pattern as
    /// [`AuditStream`] / [`LogStream`].
    ///
    /// `since_seq` seeds the initial watermark; passing `0`
    /// (the default) tails new failures from "now" onwards.
    pub fn subscribe_failures(&self, since_seq: u64) -> FailureStream {
        FailureStream::new(
            self.snapshot_reader.clone(),
            self.config.snapshot_poll_interval,
            since_seq,
        )
    }

    /// Subscribe to the substrate's per-node log ring with
    /// the given filter. Returns a [`LogStream`] that tails
    /// matching log lines as they arrive — same dedup pattern
    /// as the audit-tail stream (monotonic per-runtime `seq`
    /// watermark).
    ///
    /// `filter` defaults to "everything"; chain
    /// [`LogFilter::min_level`] / [`LogFilter::with_daemon`]
    /// / [`LogFilter::with_node`] to narrow the result.
    pub fn subscribe_logs(&self, filter: LogFilter) -> LogStream {
        LogStream::new(
            self.snapshot_reader.clone(),
            self.config.snapshot_poll_interval,
            filter,
        )
    }

    /// Open a [`SnapshotStream`] over the runtime's snapshot
    /// reader. The stream polls at
    /// [`DeckClientConfig::snapshot_poll_interval`] and emits a
    /// `Result<MeshOsSnapshot, DeckError>` on every poll.
    /// Closing the stream is a `drop`.
    pub fn snapshots(&self) -> SnapshotStream {
        SnapshotStream::new(
            self.snapshot_reader.clone(),
            self.config.snapshot_poll_interval,
        )
    }

    /// One-shot read of the runtime's latest snapshot.
    /// Synchronous — one atomic load on the snapshot pointer
    /// plus a clone of the underlying data. Use for one-off
    /// reads ("what's the freeze state right now?"); prefer
    /// [`Self::snapshots`] when iterating over many ticks.
    pub fn status(&self) -> MeshOsSnapshot {
        self.snapshot_reader.read()
    }

    /// Live-updating [`StatusSummary`] stream. Polls the
    /// snapshot reader at
    /// [`DeckClientConfig::snapshot_poll_interval`] and emits
    /// a new summary whenever the rollup actually changes
    /// (PartialEq dedup) — operator dashboards bind here for
    /// "render only when something is different." The first
    /// summary always emits.
    pub fn status_summary_stream(&self) -> StatusSummaryStream {
        StatusSummaryStream::new(
            self.snapshot_reader.clone(),
            self.config.snapshot_poll_interval,
        )
    }

    /// Roll the snapshot up into a compact at-a-glance status
    /// summary — daemon-health counts, peer-health counts,
    /// freeze / maintenance flags, queue depths. Useful for
    /// the operator UI's "is everything OK?" header. One
    /// snapshot load + a single iterator pass; no full clone.
    pub fn status_summary(&self) -> StatusSummary {
        build_status_summary(&self.snapshot_reader.load())
    }

    /// Borrow the latest peer summary. One snapshot load + a
    /// clone of just the peers map.
    pub fn peers(&self) -> std::collections::BTreeMap<NodeId, super::meshos::PeerSnapshot> {
        self.snapshot_reader.load().peers.clone()
    }

    /// This deck's local mesh node's `SubnetId`, or `None` when
    /// no `MeshNode` has been wired in via [`Self::with_mesh`].
    /// Powers `net subnet show`.
    pub fn local_subnet(&self) -> Option<SubnetId> {
        self.mesh.as_ref().map(|m| m.local_subnet())
    }

    /// Snapshot of every `(node_id, subnet_id)` pair the local
    /// mesh has cached from signature-verified capability
    /// announcements, sorted by `node_id`. Empty when no
    /// `MeshNode` is wired in. Powers `net subnet ls` and
    /// `net subnet tree`.
    pub fn known_subnets(&self) -> Vec<(u64, SubnetId)> {
        self.mesh
            .as_ref()
            .map(|m| m.known_subnets())
            .unwrap_or_default()
    }

    /// Group `known_subnets` into one row per subnet with sorted
    /// member ids. Pass `Some(node_id)` to include the local node
    /// under its own subnet's members (the CLI's `subnet ls`
    /// surface does this); pass `None` to omit the local node
    /// from members but still flag its row via
    /// [`SubnetRollup::is_local`] (the deck SUBNETS tab does this).
    ///
    /// The local subnet always appears as a row, even when no
    /// peers are known under it.
    pub fn subnets_with_members(&self, local_node_id: Option<u64>) -> Vec<SubnetRollup> {
        let local = self.local_subnet();
        let mut buckets: std::collections::BTreeMap<u32, std::collections::BTreeSet<u64>> =
            std::collections::BTreeMap::new();
        for (node_id, subnet) in self.known_subnets() {
            buckets.entry(subnet.raw()).or_default().insert(node_id);
        }
        if let Some(local_subnet) = local {
            let entry = buckets.entry(local_subnet.raw()).or_default();
            if let Some(id) = local_node_id {
                entry.insert(id);
            }
        }
        buckets
            .into_iter()
            .map(|(raw, members)| {
                let subnet = SubnetId::from_raw(raw);
                SubnetRollup {
                    subnet,
                    members: members.into_iter().collect(),
                    is_local: local == Some(subnet),
                }
            })
            .collect()
    }

    /// Aggregate gateway counters for `net gateway stats`.
    /// Returns `None` when the mesh has no installed
    /// `ChannelConfigRegistry` — in that case the gateway isn't
    /// built and there's nothing to report.
    pub fn gateway_stats(&self) -> Option<GatewayStats> {
        let gw = self.mesh.as_ref().and_then(|m| m.gateway())?;
        Some(GatewayStats {
            local_subnet: gw.local_subnet(),
            forwarded: gw.forwarded_count(),
            dropped: gw.dropped_count(),
            peer_subnets: gw.peer_subnets(),
            export_rules: gw.exports().len() as u64,
        })
    }

    /// Snapshot of the gateway's export table as
    /// `(channel_hash, target_subnets)` pairs, sorted by
    /// `channel_hash`. Empty when no gateway is installed.
    pub fn gateway_exports(&self) -> Vec<(u16, Vec<SubnetId>)> {
        self.mesh
            .as_ref()
            .and_then(|m| m.gateway())
            .map(|gw| gw.exports())
            .unwrap_or_default()
    }

    /// Resolve a channel name to its [`Visibility`] config, or
    /// `None` when no `ChannelConfigRegistry` has been installed
    /// or the name isn't registered. Falls back through the
    /// registry's prefix table via `get_by_name`. Powers
    /// `net channel visibility <name>`.
    pub fn channel_visibility(&self, channel_name: &str) -> Option<Visibility> {
        let mesh = self.mesh.as_ref()?;
        let registry = mesh.channel_configs()?;
        let cfg = registry.get_by_name(channel_name)?;
        Some(cfg.visibility)
    }

    /// Snapshot every registered channel as `(name, visibility)`
    /// pairs for `net channel ls`, sorted by name. Empty when no
    /// `ChannelConfigRegistry` has been installed.
    pub fn channels(&self) -> Vec<(String, Visibility)> {
        let Some(mesh) = self.mesh.as_ref() else {
            return Vec::new();
        };
        let Some(registry) = mesh.channel_configs() else {
            return Vec::new();
        };
        registry
            .snapshot()
            .into_iter()
            .map(|(name, cfg)| (name, cfg.visibility))
            .collect()
    }

    /// Lookup a channel's wire-`u16` hash for use with
    /// `gateway_exports`. `None` when no registry is installed
    /// or the channel isn't registered. Convenience for
    /// `net gateway export <channel> ...` to translate the
    /// human-readable channel name into the wire key the
    /// gateway's export table is keyed on.
    pub fn channel_wire_hash(&self, channel_name: &str) -> Option<u16> {
        let mesh = self.mesh.as_ref()?;
        let registry = mesh.channel_configs()?;
        let cfg = registry.get_by_name(channel_name)?;
        Some(cfg.channel_id.wire_hash())
    }

    /// Lookup a channel's canonical `ChannelHash` (u64). Same
    /// shape as [`Self::channel_wire_hash`] but returns the full
    /// 64-bit hash callers use for fold + ACL lookups.
    pub fn channel_canonical_hash(&self, channel_name: &str) -> Option<ChannelHash> {
        let mesh = self.mesh.as_ref()?;
        let registry = mesh.channel_configs()?;
        let cfg = registry.get_by_name(channel_name)?;
        Some(cfg.channel_id.hash())
    }

    /// `true` when a live [`AggregatorDaemon`] is installed via
    /// [`Self::with_aggregator`]. Lets the AGGREGATORS Deck panel
    /// discriminate between "no aggregator wired" and "aggregator
    /// wired but has nothing to report yet."
    pub fn aggregator_installed(&self) -> bool {
        self.aggregator.is_some()
    }

    /// Snapshot the running aggregator's latest summaries.
    /// Empty vec when no aggregator is installed.
    pub fn aggregator_summaries(&self) -> Vec<SummaryAnnouncement> {
        self.aggregator
            .as_ref()
            .map(|a| a.latest_summaries())
            .unwrap_or_default()
    }

    /// Cheap shared-snapshot variant of [`Self::aggregator_summaries`]
    /// — clones only the outer `Arc`. Hot-path callers (TUI render
    /// loops) should prefer this.
    pub fn aggregator_summaries_arc(&self) -> Arc<Vec<SummaryAnnouncement>> {
        self.aggregator
            .as_ref()
            .map(|a| a.latest_summaries_arc())
            .unwrap_or_else(|| Arc::new(Vec::new()))
    }

    /// One-call accessor that returns every aggregator field a
    /// renderer needs in one struct. Replaces the per-field hops
    /// (`aggregator_source_subnet` + `aggregator_fold_kinds` +
    /// `aggregator_generation` + `aggregator_summary_interval` +
    /// `aggregator_summaries`) — five lock acquisitions and two
    /// Vec clones per frame collapse to one struct construction
    /// and one Arc clone.
    ///
    /// Returns `None` when no aggregator is installed.
    pub fn aggregator_snapshot(&self) -> Option<AggregatorSnapshot> {
        let agg = self.aggregator.as_ref()?;
        let config = agg.config();
        Some(AggregatorSnapshot {
            source_subnet: config.source_subnet,
            fold_kinds: config.fold_kinds.clone(),
            generation: agg.generation(),
            summary_interval: config.summary_interval,
            summaries: agg.latest_summaries_arc(),
        })
    }

    /// Snapshot every aggregator group registered on the
    /// installed `MeshNode`'s
    /// [`AggregatorRegistry`](super::aggregator::registry::AggregatorRegistry).
    /// Returns `None` when no mesh is wired in or no registry has
    /// been installed via `MeshNode::set_aggregator_registry`.
    ///
    /// Used by `net aggregator ls` + the future
    /// Deck AGGREGATORS-list panel. Per-replica health is
    /// surfaced inline so CLI / TUI render one shot of data
    /// without follow-up calls.
    pub async fn aggregator_registry_snapshot(&self) -> Option<AggregatorRegistrySnapshot> {
        let mesh = self.mesh.as_ref()?;
        let registry = mesh.aggregator_registry()?;
        let entries = registry.entries();
        let mut groups = Vec::with_capacity(entries.len());
        for entry in entries {
            // One lock + outside-the-guard health-join per group,
            // via the entry's snapshot helper. Previously this
            // path took three sequential lock acquisitions
            // (`replicas` / `placements` / `health`) per group +
            // a slow `health()` blocked concurrent
            // `register`/`unregister` writers. See
            // `AggregatorGroupEntry::snapshot` for the rationale.
            let snap = entry.snapshot().await;
            let rows = snap
                .replicas
                .iter()
                .enumerate()
                .map(|(idx, replica)| {
                    let health = snap.healths.get(idx).cloned().unwrap_or(
                        crate::adapter::net::behavior::lifecycle::ReplicaHealth {
                            healthy: true,
                            diagnostic: None,
                        },
                    );
                    let placement_node_id = snap.placements.get(idx).map(|p| p.node_id);
                    AggregatorReplicaRow {
                        generation: replica.generation(),
                        healthy: health.healthy,
                        diagnostic: health.diagnostic,
                        placement_node_id,
                    }
                })
                .collect();
            groups.push(AggregatorRegistryGroupSnapshot {
                name: entry.name.clone(),
                group_seed: entry.group_seed,
                replicas: rows,
            });
        }
        Some(AggregatorRegistrySnapshot { groups })
    }

    /// Aggregator's monotonic tick counter, or `0` when none
    /// installed.
    pub fn aggregator_generation(&self) -> u64 {
        self.aggregator
            .as_ref()
            .map(|a| a.generation())
            .unwrap_or(0)
    }

    /// Aggregator's source subnet — what the daemon is summarizing.
    /// `None` when no aggregator is installed.
    pub fn aggregator_source_subnet(&self) -> Option<SubnetId> {
        self.aggregator.as_ref().map(|a| a.config().source_subnet)
    }

    /// List of fold-kind ids the aggregator is configured to
    /// summarize. Empty when no aggregator is installed.
    pub fn aggregator_fold_kinds(&self) -> Vec<u16> {
        self.aggregator
            .as_ref()
            .map(|a| a.config().fold_kinds.clone())
            .unwrap_or_default()
    }

    /// Aggregator's summary cadence, or zero when none installed.
    pub fn aggregator_summary_interval(&self) -> std::time::Duration {
        self.aggregator
            .as_ref()
            .map(|a| a.config().summary_interval)
            .unwrap_or_default()
    }

    /// Borrow the latest daemon summary keyed by daemon id.
    pub fn daemons(&self) -> std::collections::BTreeMap<u64, super::meshos::DaemonSnapshot> {
        self.snapshot_reader.load().daemons.clone()
    }

    /// Borrow the latest replica summary keyed by chain id.
    pub fn replicas(&self) -> std::collections::BTreeMap<ChainId, super::meshos::ReplicaSnapshot> {
        self.snapshot_reader.load().replicas.clone()
    }

    /// Read this node's local maintenance state.
    pub fn local_maintenance(&self) -> super::meshos::MaintenanceStateSnapshot {
        self.snapshot_reader.load().local_maintenance.clone()
    }

    /// Read the cluster-wide ICE freeze remaining time. `None`
    /// when no freeze is in effect.
    pub fn freeze_remaining_ms(&self) -> Option<u64> {
        self.snapshot_reader.load().freeze_remaining_ms
    }

    /// Borrow the latest executor-side failure ring. Bounded
    /// by [`super::meshos::RECENT_FAILURES_CAPACITY`]; ordered
    /// oldest-first (FIFO). Operators read this to see what
    /// the action dispatcher rejected recently. One snapshot
    /// load + a clone of just the failure ring.
    pub fn recent_failures(&self) -> Vec<super::meshos::FailureRecord> {
        self.snapshot_reader
            .load()
            .recent_failures
            .iter()
            .cloned()
            .collect()
    }

    /// Runtime-epoch identifier this MeshOsLoop stamped at
    /// startup. Stable for the lifetime of the loop task and
    /// changes on every restart. Consumers dedup'ing with
    /// `since(seq)` watermarks pair every saved watermark
    /// with this value — when it flips, reset the watermark
    /// to 0 rather than silently filtering post-restart
    /// records as "smaller than my last seq."
    pub fn runtime_epoch_id(&self) -> u64 {
        self.snapshot_reader.load().runtime_epoch_id
    }

    /// Highest `seq` currently visible on the admin-audit
    /// ring. Returns `0` when the ring is empty. Lets a
    /// caller's `since(seq)` pagination distinguish "ahead of
    /// the head" (caller's watermark > head_seq) from "no new
    /// records yet" (watermark == head_seq) — the audit
    /// stream itself swallows both cases silently.
    pub fn audit_head_seq(&self) -> u64 {
        self.snapshot_reader
            .load()
            .admin_audit
            .last()
            .map(|r| r.seq)
            .unwrap_or(0)
    }

    /// Highest `seq` currently visible on the log ring.
    /// Returns `0` when the ring is empty. Same purpose as
    /// [`Self::audit_head_seq`] for the log-stream surface.
    pub fn log_head_seq(&self) -> u64 {
        self.snapshot_reader
            .load()
            .log_ring
            .last()
            .map(|r| r.seq)
            .unwrap_or(0)
    }

    /// Highest `seq` currently visible on the failure ring.
    /// Returns `0` when the ring is empty.
    pub fn failure_head_seq(&self) -> u64 {
        self.snapshot_reader
            .load()
            .recent_failures
            .iter()
            .next_back()
            .map(|r| r.seq)
            .unwrap_or(0)
    }

    /// Like [`Self::recent_failures`] but keeps only entries
    /// with `recorded_at_ms > since_ms`. Pagination primitive:
    /// poll periodically, persist the max `recorded_at_ms`
    /// observed, and re-query with that value to surface only
    /// new failures.
    ///
    /// Note: same-ms collisions can land on the boundary —
    /// records sharing the cutoff value's exact ms might be
    /// missed if they were added between polls. The proper
    /// seq-based stream lands once [`super::meshos::FailureRecord`]
    /// gains a monotonic seq field (next substrate slice).
    pub fn recent_failures_since(&self, since_ms: u64) -> Vec<super::meshos::FailureRecord> {
        self.snapshot_reader
            .load()
            .recent_failures
            .iter()
            .filter(|r| r.recorded_at_ms > since_ms)
            .cloned()
            .collect()
    }

    /// Await a snapshot matching `predicate`. Polls the
    /// snapshot reader at
    /// [`DeckClientConfig::snapshot_poll_interval`] and resolves
    /// with the first snapshot for which `predicate` returns
    /// `true`. If the current snapshot already matches,
    /// resolves immediately without sleeping.
    ///
    /// **Note on wedge risk:** if no snapshot ever matches the
    /// predicate, this future never resolves. Use
    /// [`Self::watch_timeout`] for bounded waits.
    pub async fn watch<F>(&self, mut predicate: F) -> MeshOsSnapshot
    where
        F: FnMut(&MeshOsSnapshot) -> bool,
    {
        // Check the current snapshot first — many "wait for
        // state X" calls land on a state that's already true.
        let snap = self.snapshot_reader.read();
        if predicate(&snap) {
            return snap;
        }
        let interval = self
            .config
            .snapshot_poll_interval
            .max(Duration::from_millis(1));
        loop {
            tokio::time::sleep(interval).await;
            let snap = self.snapshot_reader.read();
            if predicate(&snap) {
                return snap;
            }
        }
    }

    /// Like [`Self::watch`] but with a bounded wait. Returns
    /// `Ok(snapshot)` on first match, `Err(DeckError)` with
    /// kind `watch_timeout` when `timeout` elapses without a
    /// match.
    pub async fn watch_timeout<F>(
        &self,
        predicate: F,
        timeout: Duration,
    ) -> Result<MeshOsSnapshot, DeckError>
    where
        F: FnMut(&MeshOsSnapshot) -> bool,
    {
        tokio::time::timeout(timeout, self.watch(predicate))
            .await
            .map_err(|_| {
                DeckError::new(
                    "watch_timeout",
                    format!(
                        "no snapshot matched the predicate within {} ms",
                        timeout.as_millis()
                    ),
                )
            })
    }

    fn next_commit_id(&self) -> u64 {
        // Start at 1 so a `0` commit id is recognizable as
        // "unset" downstream.
        self.commit_seq.fetch_add(1, Ordering::Relaxed) + 1
    }

    async fn publish_admin(
        &self,
        event: AdminEvent,
        kind: &'static str,
    ) -> Result<ChainCommit, AdminError> {
        // When an operator registry is installed, sign the
        // admin event and route through SignedAdminCommit so
        // the substrate verifier sees the operator's signature.
        // Otherwise (in-process tests, dev mode) fall back to
        // the unsigned admin path. Per the plan's locked
        // decision #2 every Deck commit is signed in
        // production deployments — the substrate verifier is
        // the source of truth.
        let wire_event = if self.operator_registry.is_some() {
            let issued_at_ms = super::meshos::now_ms_since_unix_epoch();
            let signature = self.identity.sign_admin_event(&event, issued_at_ms);
            MeshOsEvent::SignedAdminCommit {
                event,
                signature,
                issued_at_ms,
            }
        } else {
            MeshOsEvent::AdminEvent(event)
        };
        self.handle
            .publish(wire_event)
            .await
            .map_err(AdminError::from)?;
        Ok(ChainCommit {
            commit_id: self.next_commit_id(),
            operator_id: self.identity.operator_id,
            event_kind: kind,
            committed_at: SystemTime::now(),
        })
    }

    async fn publish_signed_ice(
        &self,
        proposal: IceActionProposal,
        signatures: Vec<OperatorSignature>,
        issued_at_ms: u64,
        blast_hash: super::meshos::BlastRadiusHash,
        kind: &'static str,
    ) -> Result<ChainCommit, IceError> {
        self.handle
            .publish(MeshOsEvent::SignedIceCommit {
                proposal,
                signatures,
                issued_at_ms,
                blast_hash,
            })
            .await
            .map_err(IceError::from)?;
        Ok(ChainCommit {
            commit_id: self.next_commit_id(),
            operator_id: self.identity.operator_id,
            event_kind: kind,
            committed_at: SystemTime::now(),
        })
    }
}

/// Typed admin-command surface. Constructed via
/// [`DeckClient::admin`]; every method maps to one
/// [`super::meshos::AdminEvent`] variant.
///
/// Phase 1 publishes events onto the loop's event stream
/// directly (matching the substrate's current admin-event entry
/// path). When the substrate adds a signed admin chain, this
/// surface gains a signing step before the publish — the
/// per-method type signatures don't change.
pub struct AdminCommands<'a> {
    client: &'a DeckClient,
}

impl AdminCommands<'_> {
    /// Drain `node`'s workload over `drain_for`. Replicas
    /// migrate; daemons drain via
    /// [`crate::adapter::net::compute::DaemonControl::DrainStart`]
    /// once the loop sees the resulting `EnteringMaintenance`
    /// state. The duration is the wait-window from the loop's
    /// next tick; the substrate computes the absolute deadline
    /// at fold time so two replays of the same event stream
    /// produce identical `since` / `deadline` instants.
    pub async fn drain(
        &self,
        node: NodeId,
        drain_for: Duration,
    ) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(AdminEvent::Drain { node, drain_for }, "drain")
            .await
    }

    /// Begin a maintenance window for `node`. `drain_for` is
    /// the drain-window duration; `None` defers to the cluster's
    /// configured default. The substrate-side fold computes the
    /// absolute deadline as `last_tick + drain_for`.
    pub async fn enter_maintenance(
        &self,
        node: NodeId,
        drain_for: Option<Duration>,
    ) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(
                AdminEvent::EnterMaintenance { node, drain_for },
                "enter_maintenance",
            )
            .await
    }

    /// End a maintenance window for `node`.
    pub async fn exit_maintenance(&self, node: NodeId) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(AdminEvent::ExitMaintenance { node }, "exit_maintenance")
            .await
    }

    /// Mark `node` ineligible for new placements (existing
    /// workload stays).
    pub async fn cordon(&self, node: NodeId) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(AdminEvent::Cordon { node }, "cordon")
            .await
    }

    /// Remove a prior cordon.
    pub async fn uncordon(&self, node: NodeId) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(AdminEvent::Uncordon { node }, "uncordon")
            .await
    }

    /// Drop the listed replicas from `node`.
    pub async fn drop_replicas(
        &self,
        node: NodeId,
        chains: Vec<ChainId>,
    ) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(AdminEvent::DropReplicas { node, chains }, "drop_replicas")
            .await
    }

    /// Force a placement recompute for `node`.
    pub async fn invalidate_placement(&self, node: NodeId) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(
                AdminEvent::InvalidatePlacement { node },
                "invalidate_placement",
            )
            .await
    }

    /// Force-restart every daemon on `node`.
    pub async fn restart_all_daemons(&self, node: NodeId) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(
                AdminEvent::RestartAllDaemons { node },
                "restart_all_daemons",
            )
            .await
    }

    /// Clear `node`'s local avoid list.
    pub async fn clear_avoid_list(&self, node: NodeId) -> Result<ChainCommit, AdminError> {
        self.client
            .publish_admin(AdminEvent::ClearAvoidList { node }, "clear_avoid_list")
            .await
    }
}

/// Re-export the substrate-side signing types so the SDK
/// surface stays under `behavior::deck::*` for backwards
/// compatibility with the previous slice. The implementations
/// live alongside the rest of ICE in `behavior::meshos::ice`.
pub use super::meshos::{OperatorRegistry, OperatorSignature, VerifyError};

impl OperatorIdentity {
    /// Sign `proposal` with this operator's ed25519 key, stamped
    /// at `issued_at_ms` and bound to the simulator's
    /// pre-execution preview hash `blast_hash`. Thin
    /// SDK-ergonomic wrapper over the substrate's
    /// [`OperatorSignature::sign`] constructor.
    ///
    /// The substrate verifier rebuilds the same domain-tagged
    /// payload — including `blast_hash` — and rejects bundles
    /// whose hash is the
    /// [`super::meshos::SIMULATION_REQUIRED_SENTINEL`]
    /// (substrate enforcement of locked decision #4) or whose
    /// freshness window has expired. Coordinators collecting
    /// signatures from multiple operators must share the same
    /// `(issued_at_ms, blast_hash)` pair across the bundle —
    /// fetch both from the [`IceProposal`] handle.
    pub fn sign_proposal(
        &self,
        proposal: &IceActionProposal,
        issued_at_ms: u64,
        blast_hash: &super::meshos::BlastRadiusHash,
    ) -> OperatorSignature {
        OperatorSignature::sign(self.keypair(), proposal, issued_at_ms, blast_hash)
    }

    /// Sign an ordinary `AdminEvent` for the single-signature
    /// `SignedAdminCommit` path, stamped at `issued_at_ms`. The
    /// signature covers the substrate's
    /// [`super::meshos::admin_event_signing_payload(event, issued_at_ms)`]
    /// so the loop verifier and the SDK agree on the byte
    /// sequence.
    pub fn sign_admin_event(&self, event: &AdminEvent, issued_at_ms: u64) -> OperatorSignature {
        OperatorSignature::sign_admin(self.keypair(), event, issued_at_ms)
    }
}

/// SDK-side translation of substrate
/// [`VerifyError`] to the `<<deck-sdk-kind:KIND>>MSG` envelope
/// the SDK's [`IceProposal::commit`] returns.
fn verify_error_to_ice(err: VerifyError) -> IceError {
    let kind = err.kind();
    IceError::new(kind, err.to_string())
}

/// Break-glass operator surface. Constructed via
/// [`DeckClient::ice`]; each method returns an [`IceProposal`]
/// that must be `simulate()`d before `commit()` per the plan's
/// locked decision #4 (blast-radius simulation is mandatory
/// before any ICE commit).
pub struct IceCommands<'a> {
    client: &'a DeckClient,
}

impl<'a> IceCommands<'a> {
    /// Propose a cluster-wide freeze. The returned
    /// [`IceProposal`] must be simulated then committed.
    pub fn freeze_cluster(&self, ttl: Duration) -> IceProposal<'a> {
        IceProposal::new(self.client, IceActionProposal::FreezeCluster { ttl })
    }

    /// Propose flushing avoid-list entries under `scope`.
    /// See [`super::meshos::ice::IceActionProposal::FlushAvoidLists`]
    /// for the three scope semantics.
    pub fn flush_avoid_lists(&self, scope: super::meshos::AvoidScope) -> IceProposal<'a> {
        IceProposal::new(self.client, IceActionProposal::FlushAvoidLists { scope })
    }

    /// Propose force-evicting `victim` from `chain` bypassing
    /// the scheduler's rebalance cooldown. Only the chain's
    /// elected leader emits the resulting `RequestEviction`
    /// action; non-leader nodes fold the admin event silently.
    pub fn force_evict_replica(&self, chain: ChainId, victim: NodeId) -> IceProposal<'a> {
        IceProposal::new(
            self.client,
            IceActionProposal::ForceEvictReplica { chain, victim },
        )
    }

    /// Propose force-restarting `daemon` by resetting its
    /// supervisor backoff gate so reconcile fires `StartDaemon`
    /// on the next tick. No-op if the daemon is already in
    /// `Idle` backoff state.
    pub fn force_restart_daemon(&self, daemon: super::meshos::DaemonRef) -> IceProposal<'a> {
        IceProposal::new(
            self.client,
            IceActionProposal::ForceRestartDaemon { daemon },
        )
    }

    /// Propose force-placing `chain` on `target`, bypassing
    /// the placement scorer. Only the chain's elected leader
    /// emits the resulting `RequestPlacement` action with
    /// `target: Some(target)`; non-leader nodes fold silently.
    /// No-op if `target` is already a holder.
    pub fn force_cutover(&self, chain: ChainId, target: NodeId) -> IceProposal<'a> {
        IceProposal::new(
            self.client,
            IceActionProposal::ForceCutover { chain, target },
        )
    }

    /// Propose aborting an in-flight migration. The wire-form
    /// substrate plumbing records the commit on the audit
    /// ring; the dispatcher hookup that finds the running
    /// migration and stops it is future substrate work, so
    /// until that lands the proposal commits without
    /// actually halting the migration. The audit trail tracks
    /// the operator's intent regardless.
    pub fn kill_migration(&self, migration: super::meshos::MigrationId) -> IceProposal<'a> {
        IceProposal::new(self.client, IceActionProposal::KillMigration { migration })
    }

    /// Propose cancelling an in-effect cluster freeze.
    pub fn thaw_cluster(&self) -> IceProposal<'a> {
        IceProposal::new(self.client, IceActionProposal::ThawCluster)
    }
}

/// An ICE proposal — pre-simulation handle carrying the
/// underlying [`IceActionProposal`] plus the `issued_at_ms`
/// stamp pinned at construction.
///
/// Per the plan's locked decision #4 a [`Self::simulate`] call
/// must precede commit. The type-state split enforces this at
/// compile time: `IceProposal` does **not** expose `commit`;
/// only a successful `simulate()` returns a
/// [`SimulatedIceProposal`] whose `commit` is callable. A
/// caller that wants to commit must thread the proposal through
/// `simulate()` first — the type system rejects the alternative.
///
/// Both `IceProposal` and `SimulatedIceProposal` are `Send + Sync`
/// (no `Cell` / `RefCell` interior mutability), so callers can
/// move them across `tokio::spawn` boundaries freely.
pub struct IceProposal<'a> {
    client: &'a DeckClient,
    action: IceActionProposal,
    issued_at_ms: u64,
}

impl<'a> IceProposal<'a> {
    fn new(client: &'a DeckClient, action: IceActionProposal) -> Self {
        Self {
            client,
            action,
            issued_at_ms: super::meshos::now_ms_since_unix_epoch(),
        }
    }

    /// Borrow the underlying [`IceActionProposal`].
    pub fn action(&self) -> &IceActionProposal {
        &self.action
    }

    /// Milliseconds-since-`UNIX_EPOCH` stamp pinned at
    /// construction. Operators participating in the multi-sig
    /// flow read this from the [`SimulatedIceProposal`] (after
    /// simulating); the value is identical because `simulate()`
    /// preserves it.
    pub fn issued_at_ms(&self) -> u64 {
        self.issued_at_ms
    }

    /// Pre-execution preview. Runs the substrate's pure
    /// simulator against the runtime's latest snapshot and
    /// returns a [`SimulatedIceProposal`] holding the result.
    /// The returned type is the only place
    /// [`SimulatedIceProposal::commit`] can be called — the
    /// type-state pattern enforces locked decision #4 at
    /// compile time.
    pub async fn simulate(self) -> Result<SimulatedIceProposal<'a>, IceError> {
        let snap = self.client.snapshot_reader.read();
        let blast = simulate_ice_proposal(&snap, &self.action);
        Ok(SimulatedIceProposal {
            client: self.client,
            action: self.action,
            issued_at_ms: self.issued_at_ms,
            blast,
        })
    }
}

/// An ICE proposal that has run [`IceProposal::simulate`] and
/// is ready for the operator signing + commit workflow.
/// Carries the simulator's [`BlastRadius`] output so the
/// Deck-the-binary UI can render the preview before the
/// operator approves; [`Self::commit`] hashes the
/// `BlastRadius` and signs the envelope with the operator key.
///
/// Every operator participating in the multi-signature
/// workflow must sign over the same
/// `(action, issued_at_ms, blast_hash)` triple — fetch via
/// [`Self::action`] / [`Self::issued_at_ms`] /
/// [`Self::blast_hash`].
pub struct SimulatedIceProposal<'a> {
    client: &'a DeckClient,
    action: IceActionProposal,
    issued_at_ms: u64,
    blast: BlastRadius,
}

impl<'a> SimulatedIceProposal<'a> {
    /// Borrow the simulator's pre-execution preview.
    pub fn blast_radius(&self) -> &BlastRadius {
        &self.blast
    }

    /// Borrow the underlying [`IceActionProposal`].
    pub fn action(&self) -> &IceActionProposal {
        &self.action
    }

    /// Milliseconds-since-`UNIX_EPOCH` stamp pinned at the
    /// original [`IceProposal`]'s construction; signatures
    /// must cover this exact value.
    pub fn issued_at_ms(&self) -> u64 {
        self.issued_at_ms
    }

    /// Blake3 digest of the simulator's [`BlastRadius`].
    /// Signatures must cover this hash; the substrate verifier
    /// rebuilds the same payload and rejects bundles whose
    /// signatures don't bind to it.
    pub fn blast_hash(&self) -> super::meshos::BlastRadiusHash {
        super::meshos::blast_radius_hash(&self.blast)
    }

    /// Commit the proposal. Verifies
    /// `signatures.len() >= ice_signature_threshold` before
    /// publishing; returns
    /// `Err(IceError::insufficient_signatures)` otherwise.
    /// Substrate-side multi-operator-signature verification
    /// rebuilds the same domain-tagged signing envelope —
    /// including the blast-radius hash — and rejects any
    /// bundle whose signatures don't cover the exact
    /// `(action, issued_at_ms, blast_hash)` triple.
    pub async fn commit(self, signatures: &[OperatorSignature]) -> Result<ChainCommit, IceError> {
        let blast_hash = self.blast_hash();
        let threshold = self.client.config.ice_signature_threshold;
        if signatures.len() < threshold {
            return Err(IceError::new(
                "insufficient_signatures",
                format!(
                    "ICE commit requires {} operator signatures; got {}",
                    threshold,
                    signatures.len()
                ),
            ));
        }
        if let Some(registry) = self.client.operator_registry.as_ref() {
            // SDK-side gate: verify locally before publishing so
            // a malformed bundle fails fast with the right error
            // kind. The substrate-side verifier on the loop runs
            // the same check on every `SignedIceCommit` for the
            // belt-and-suspenders property: even an SDK that
            // skipped this gate gets rejected by the loop.
            let payload =
                ice_proposal_signing_payload(&self.action, self.issued_at_ms, &blast_hash);
            let mut unique_operators: std::collections::BTreeSet<u64> =
                std::collections::BTreeSet::new();
            for sig in signatures {
                registry
                    .verify(sig, &payload)
                    .map_err(verify_error_to_ice)?;
                unique_operators.insert(sig.operator_id);
            }
            // Mirror the substrate-side distinct-operator check
            // so duplicate signatures from a single operator can't
            // satisfy M-of-N at the SDK gate either.
            if unique_operators.len() < threshold {
                return Err(IceError::new(
                    "insufficient_signatures",
                    format!(
                        "ICE commit requires {} distinct operator signatures; got {} distinct",
                        threshold,
                        unique_operators.len()
                    ),
                ));
            }
            // Route via SignedIceCommit so the substrate verifier
            // sees the bundle. The inner AdminEvent folds only
            // after the loop's own verification passes. The
            // event_kind on the returned ChainCommit mirrors the
            // unsigned path so consumers see one stable
            // discriminator regardless of whether verification
            // was wired.
            let kind = self.action.kind();
            self.client
                .publish_signed_ice(
                    self.action,
                    signatures.to_vec(),
                    self.issued_at_ms,
                    blast_hash,
                    kind,
                )
                .await
        } else {
            // No registry: route via the unsigned admin path.
            // Useful for in-process tests where the SDK isn't
            // gating on identity at all. Freeze / thaw go
            // through the same path; the old `AdminCommands`
            // surface for those was removed because it
            // duplicated the ICE ceremony around the
            // simulate-before-commit gate (plan locked
            // decision #4).
            let kind = self.action.kind();
            let event = self.action.to_admin_event();
            self.client.publish_admin(event, kind).await
        }
    }
}

/// Audit-query builder reading the substrate's admin audit
/// ring. Constructed via [`DeckClient::audit`]; chain filter
/// methods, then call [`Self::collect`] to materialize the
/// matching entries.
///
/// # Scope
///
/// Reads the in-memory admin audit ring exported on every
/// [`super::meshos::MeshOsSnapshot`]. The ring carries every
/// admin commit the loop observed — signed ICE bundles AND
/// unsigned admin events — bounded at
/// [`super::meshos::DEFAULT_MAX_ADMIN_AUDIT_RECORDS`]. The
/// unbounded historical replay path is the eventual admin
/// audit subchain (substrate gap); this in-memory ring is the
/// near-history surface Deck-the-binary renders against.
///
/// Per-method semantics:
///
/// - [`Self::recent`] — keep the last N entries (newest-first
///   in the result). When unspecified, returns every entry on
///   the ring.
/// - [`Self::by_operator`] — keep entries whose
///   [`super::meshos::AdminAuditRecord::operator_ids`] include
///   the given id.
/// - [`Self::between`] — keep entries whose
///   `committed_at_ms` falls inside `[start_ms, end_ms]`
///   (inclusive).
/// - [`Self::force_only`] — restrict the result to ICE-class
///   admin events (`AdminEvent::is_ice` returns `true`).
///   Drops ordinary admin commits (`drain`, `cordon`, …) from
///   the result.
pub struct AuditQuery<'a> {
    client: &'a DeckClient,
    limit: Option<usize>,
    operator_filter: Option<u64>,
    time_range: Option<(u64, u64)>,
    force_only: bool,
    since_seq: Option<u64>,
}

impl<'a> AuditQuery<'a> {
    fn new(client: &'a DeckClient) -> Self {
        Self {
            client,
            limit: None,
            operator_filter: None,
            time_range: None,
            force_only: false,
            since_seq: None,
        }
    }

    /// Cap the result at the most-recent `limit` entries.
    /// Returned order is newest-first.
    ///
    /// `limit = 0` returns an empty result by design — useful
    /// in higher-level builder flows that compute the cap from
    /// runtime config (operator typed 0, no records wanted).
    /// Callers that want "as many as the ring holds" should
    /// omit `recent()` entirely; the builder's default returns
    /// every entry.
    pub fn recent(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Keep only entries that carry `op_id` in their
    /// `operator_ids` list. Records where one of several
    /// operators in a multi-op bundle matches still surface.
    pub fn by_operator(mut self, op_id: u64) -> Self {
        self.operator_filter = Some(op_id);
        self
    }

    /// Keep only entries whose `committed_at_ms` falls inside
    /// `[start_ms, end_ms]` (inclusive on both ends).
    /// Milliseconds since `UNIX_EPOCH`.
    pub fn between(mut self, start_ms: u64, end_ms: u64) -> Self {
        self.time_range = Some((start_ms, end_ms));
        self
    }

    /// Restrict the result to ICE force operations. The audit
    /// ring now interleaves ordinary signed-admin commits with
    /// ICE bundles, so this filter actively drops non-ICE
    /// records via [`super::meshos::AdminEvent::is_ice`].
    pub fn force_only(mut self) -> Self {
        self.force_only = true;
        self
    }

    /// Restrict the result to records with `seq > since_seq`.
    /// Pagination primitive: consumers persist the last-seen
    /// `seq` and resume from there across restarts. For
    /// [`Self::stream`] the value seeds the stream's
    /// watermark — the stream tails from `since_seq + 1`
    /// onward rather than from "first new record after now".
    ///
    /// `since(0)` means "from the beginning of what's still in
    /// the ring" — equivalent to omitting `since()` entirely
    /// in terms of what records are returned. To tail only
    /// records that arrive after subscribe time, pair this
    /// with [`DeckClient::audit_head_seq`]: read the head
    /// once, then `since(head)`. The same convention applies
    /// to [`LogFilter::since`] and
    /// [`DeckClient::subscribe_failures`].
    pub fn since(mut self, since_seq: u64) -> Self {
        self.since_seq = Some(since_seq);
        self
    }

    /// Materialize matching entries. Reads the runtime's
    /// latest snapshot, applies the configured filters, and
    /// returns the matching entries newest-first. Cheap — one
    /// snapshot read + a single iterator pass.
    pub fn collect(self) -> Vec<super::meshos::AdminAuditRecord> {
        let snap = self.client.snapshot_reader.read();
        let mut matched: Vec<super::meshos::AdminAuditRecord> = snap
            .admin_audit
            .iter()
            .filter(|r| {
                if let Some(since) = self.since_seq {
                    if r.seq <= since {
                        return false;
                    }
                }
                if let Some(op_id) = self.operator_filter {
                    if !r.operator_ids.contains(&op_id) {
                        return false;
                    }
                }
                if let Some((start, end)) = self.time_range {
                    if r.committed_at_ms < start || r.committed_at_ms > end {
                        return false;
                    }
                }
                if self.force_only && !r.event.is_ice() {
                    return false;
                }
                true
            })
            .cloned()
            .collect();
        // Ring order is oldest-first; the natural operator UI
        // shape is newest-first.
        matched.reverse();
        if let Some(limit) = self.limit {
            matched.truncate(limit);
        }
        matched
    }

    /// Tail mode: convert the query into an async stream that
    /// yields each matching audit record as it arrives on the
    /// substrate's ring. Polls the snapshot reader at
    /// `DeckClientConfig::snapshot_poll_interval`. The
    /// `recent(limit)` filter is ignored in tail mode — the
    /// stream emits continuously, not a bounded batch.
    ///
    /// Uses [`super::meshos::AdminAuditRecord::seq`] to dedup
    /// across polls so two commits in the same millisecond
    /// never collapse.
    pub fn stream(self) -> AuditStream {
        AuditStream::new(
            self.client.snapshot_reader.clone(),
            self.client.config.snapshot_poll_interval,
            AuditFilter {
                operator: self.operator_filter,
                time_range: self.time_range,
                force_only: self.force_only,
            },
            self.since_seq.unwrap_or(0),
        )
    }
}

/// Compiled audit filter the [`AuditStream`] re-applies on
/// every poll. Internal — exposed as `AuditQuery`'s builder
/// shape, not directly constructible by SDK consumers.
#[derive(Clone, Debug)]
struct AuditFilter {
    operator: Option<u64>,
    time_range: Option<(u64, u64)>,
    force_only: bool,
}

impl AuditFilter {
    fn matches(&self, record: &super::meshos::AdminAuditRecord) -> bool {
        if let Some(op_id) = self.operator {
            if !record.operator_ids.contains(&op_id) {
                return false;
            }
        }
        if let Some((start, end)) = self.time_range {
            if record.committed_at_ms < start || record.committed_at_ms > end {
                return false;
            }
        }
        if self.force_only && !record.event.is_ice() {
            return false;
        }
        true
    }
}

/// Audit-tail stream. Emits each matching
/// [`super::meshos::AdminAuditRecord`] as it lands on the
/// substrate's ring. Built via [`AuditQuery::stream`].
///
/// Dedup uses the per-runtime monotonic
/// [`super::meshos::AdminAuditRecord::seq`] field — the
/// stream tracks the highest seq it's emitted, and on each
/// poll yields records strictly above that watermark.
pub struct AuditStream {
    reader: super::meshos::MeshOsSnapshotReader,
    interval: Interval,
    filter: AuditFilter,
    last_seq: u64,
    /// Queue of records pending emission. Populated on a poll
    /// when multiple matching records arrived since the last
    /// tick; drained one-per-`poll_next` so the consumer sees
    /// each commit individually.
    queued: std::collections::VecDeque<super::meshos::AdminAuditRecord>,
}

impl AuditStream {
    fn new(
        reader: super::meshos::MeshOsSnapshotReader,
        poll_interval: Duration,
        filter: AuditFilter,
        initial_seq_watermark: u64,
    ) -> Self {
        let poll_interval = poll_interval.max(Duration::from_millis(1));
        Self {
            reader,
            interval: interval(poll_interval),
            filter,
            last_seq: initial_seq_watermark,
            queued: std::collections::VecDeque::new(),
        }
    }
}

impl Stream for AuditStream {
    type Item = Result<super::meshos::AdminAuditRecord, DeckError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // Drain any queued records first so the consumer sees
        // every record from a multi-record poll individually.
        if let Some(record) = self.queued.pop_front() {
            return Poll::Ready(Some(Ok(record)));
        }
        // Wait for the next poll tick.
        match self.interval.poll_tick(cx) {
            Poll::Ready(_) => {
                let snap = self.reader.read();
                let last_seq = self.last_seq;
                // Ring order is oldest-first; iterate forward.
                let mut max_seq = last_seq;
                for record in snap.admin_audit.iter().cloned() {
                    if record.seq <= last_seq {
                        continue;
                    }
                    if record.seq > max_seq {
                        max_seq = record.seq;
                    }
                    if self.filter.matches(&record) {
                        self.queued.push_back(record);
                    }
                }
                self.last_seq = max_seq;
                if let Some(record) = self.queued.pop_front() {
                    Poll::Ready(Some(Ok(record)))
                } else {
                    // The interval consumed its Ready tick on
                    // this poll; the next poll calls poll_tick
                    // again, which registers its own waker for
                    // the next period. No explicit wake_by_ref
                    // needed.
                    Poll::Pending
                }
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Log-stream filter. Default is "everything"; chain the
/// builder methods to narrow.
#[derive(Clone, Debug, Default)]
pub struct LogFilter {
    /// Minimum severity. Records below this are dropped.
    /// `None` matches every level.
    pub min_level: Option<super::meshos::LogLevel>,
    /// Restrict to a specific daemon. `None` matches every
    /// daemon (and substrate-level lines with `daemon_id =
    /// None`).
    pub daemon_id: Option<u64>,
    /// Restrict to a specific node. `None` matches every node.
    /// Future-relevant when remote log lines arrive via the
    /// per-daemon RedEX-tail integration.
    pub node_id: Option<NodeId>,
    /// Initial seq watermark — the stream tails from
    /// `since_seq + 1` onward rather than from "first new
    /// record after subscribe-time." Pagination primitive:
    /// consumers persist the last-seen seq and resume.
    pub since_seq: Option<u64>,
}

impl LogFilter {
    /// Empty filter — matches every record on the ring.
    pub fn new() -> Self {
        Self::default()
    }

    /// Restrict to records at or above `level`.
    pub fn min_level(mut self, level: super::meshos::LogLevel) -> Self {
        self.min_level = Some(level);
        self
    }

    /// Restrict to records originating from `daemon_id`.
    pub fn with_daemon(mut self, daemon_id: u64) -> Self {
        self.daemon_id = Some(daemon_id);
        self
    }

    /// Restrict to records originating from `node_id`.
    pub fn with_node(mut self, node_id: NodeId) -> Self {
        self.node_id = Some(node_id);
        self
    }

    /// Seed the stream's watermark to `since_seq`. The first
    /// record yielded has `seq > since_seq`. `since(0)` means
    /// "from the beginning of what's still in the ring" —
    /// to tail only post-subscribe records pair with
    /// [`DeckClient::log_head_seq`]: read the head once,
    /// then `since(head)`. Same convention as
    /// [`AuditQuery::since`].
    pub fn since(mut self, since_seq: u64) -> Self {
        self.since_seq = Some(since_seq);
        self
    }

    fn matches(&self, record: &super::meshos::LogRecord) -> bool {
        if let Some(min) = self.min_level {
            if record.level < min {
                return false;
            }
        }
        if let Some(id) = self.daemon_id {
            if record.daemon_id != Some(id) {
                return false;
            }
        }
        if let Some(node) = self.node_id {
            if record.node_id != Some(node) {
                return false;
            }
        }
        true
    }
}

/// Log-tail stream returned by [`DeckClient::subscribe_logs`].
/// Yields each matching [`super::meshos::LogRecord`] once.
/// Dedups across snapshot polls via the per-runtime monotonic
/// `LogRecord::seq` (same pattern as [`AuditStream`]).
pub struct LogStream {
    reader: super::meshos::MeshOsSnapshotReader,
    interval: Interval,
    filter: LogFilter,
    last_seq: u64,
    queued: std::collections::VecDeque<super::meshos::LogRecord>,
}

impl LogStream {
    fn new(
        reader: super::meshos::MeshOsSnapshotReader,
        poll_interval: Duration,
        filter: LogFilter,
    ) -> Self {
        let poll_interval = poll_interval.max(Duration::from_millis(1));
        let last_seq = filter.since_seq.unwrap_or(0);
        Self {
            reader,
            interval: interval(poll_interval),
            filter,
            last_seq,
            queued: std::collections::VecDeque::new(),
        }
    }
}

impl Stream for LogStream {
    type Item = Result<super::meshos::LogRecord, DeckError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        if let Some(record) = self.queued.pop_front() {
            return Poll::Ready(Some(Ok(record)));
        }
        match self.interval.poll_tick(cx) {
            Poll::Ready(_) => {
                let snap = self.reader.read();
                let last_seq = self.last_seq;
                let mut max_seq = last_seq;
                for record in snap.log_ring.iter().cloned() {
                    if record.seq <= last_seq {
                        continue;
                    }
                    if record.seq > max_seq {
                        max_seq = record.seq;
                    }
                    if self.filter.matches(&record) {
                        self.queued.push_back(record);
                    }
                }
                self.last_seq = max_seq;
                if let Some(record) = self.queued.pop_front() {
                    Poll::Ready(Some(Ok(record)))
                } else {
                    cx.waker().wake_by_ref();
                    Poll::Pending
                }
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Failure-tail stream returned by
/// [`DeckClient::subscribe_failures`]. Yields each new
/// [`super::meshos::FailureRecord`] the executor records.
/// Dedups across snapshot polls via the per-runtime
/// monotonic `FailureRecord::seq` (same pattern as
/// [`AuditStream`] / [`LogStream`]).
///
/// Chain-replay-derived failure records carry `seq = 0`;
/// they're naturally skipped because the watermark logic
/// is `seq > last_seq` and the initial watermark defaults
/// to 0.
pub struct FailureStream {
    reader: super::meshos::MeshOsSnapshotReader,
    interval: Interval,
    last_seq: u64,
    queued: std::collections::VecDeque<super::meshos::FailureRecord>,
}

impl FailureStream {
    fn new(
        reader: super::meshos::MeshOsSnapshotReader,
        poll_interval: Duration,
        initial_seq_watermark: u64,
    ) -> Self {
        let poll_interval = poll_interval.max(Duration::from_millis(1));
        Self {
            reader,
            interval: interval(poll_interval),
            last_seq: initial_seq_watermark,
            queued: std::collections::VecDeque::new(),
        }
    }
}

impl Stream for FailureStream {
    type Item = Result<super::meshos::FailureRecord, DeckError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        if let Some(record) = self.queued.pop_front() {
            return Poll::Ready(Some(Ok(record)));
        }
        match self.interval.poll_tick(cx) {
            Poll::Ready(_) => {
                let snap = self.reader.read();
                let last_seq = self.last_seq;
                let mut max_seq = last_seq;
                for record in snap.recent_failures.iter().cloned() {
                    if record.seq <= last_seq {
                        continue;
                    }
                    if record.seq > max_seq {
                        max_seq = record.seq;
                    }
                    self.queued.push_back(record);
                }
                self.last_seq = max_seq;
                if let Some(record) = self.queued.pop_front() {
                    Poll::Ready(Some(Ok(record)))
                } else {
                    cx.waker().wake_by_ref();
                    Poll::Pending
                }
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Stream over the runtime's snapshot reader. Polls at the
/// configured cadence; emits a `Result<MeshOsSnapshot, DeckError>`
/// per poll. Phase 1 emits on every poll (consumers de-dupe if
/// they care); the substrate's tail-with-replay path replaces
/// this with a chain-driven stream when it lands.
pub struct SnapshotStream {
    reader: MeshOsSnapshotReader,
    interval: Interval,
}

impl SnapshotStream {
    fn new(reader: MeshOsSnapshotReader, poll_interval: Duration) -> Self {
        // Floor the interval so a zero-duration config doesn't
        // hot-spin the executor.
        let poll_interval = poll_interval.max(Duration::from_millis(1));
        Self {
            reader,
            interval: interval(poll_interval),
        }
    }
}

impl Stream for SnapshotStream {
    type Item = Result<MeshOsSnapshot, DeckError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.interval.poll_tick(cx) {
            Poll::Ready(_) => Poll::Ready(Some(Ok(self.reader.read()))),
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Dedup'd [`StatusSummary`] stream. Returned by
/// [`DeckClient::status_summary_stream`]. Polls the snapshot
/// reader at the client's configured cadence, builds a fresh
/// summary, and yields it only when the summary differs from
/// the last emitted one (`PartialEq` dedup). The first poll
/// always emits — operators rendering a dashboard see the
/// initial state immediately, then change-driven updates from
/// there.
pub struct StatusSummaryStream {
    reader: super::meshos::MeshOsSnapshotReader,
    interval: Interval,
    last_emitted: Option<StatusSummary>,
}

impl StatusSummaryStream {
    fn new(reader: super::meshos::MeshOsSnapshotReader, poll_interval: Duration) -> Self {
        let poll_interval = poll_interval.max(Duration::from_millis(1));
        Self {
            reader,
            interval: interval(poll_interval),
            last_emitted: None,
        }
    }
}

impl Stream for StatusSummaryStream {
    type Item = Result<StatusSummary, DeckError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.interval.poll_tick(cx) {
            Poll::Ready(_) => {
                let snap = self.reader.read();
                let summary = build_status_summary(&snap);
                let should_emit = match &self.last_emitted {
                    None => true,
                    Some(prev) => prev != &summary,
                };
                if should_emit {
                    self.last_emitted = Some(summary.clone());
                    Poll::Ready(Some(Ok(summary)))
                } else {
                    // The interval consumed its Ready tick on
                    // this poll; the next poll_tick registers
                    // its own waker for the next period.
                    Poll::Pending
                }
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapter::net::behavior::meshos::{
        LoggingDispatcher, MaintenanceTransition, MeshOsAction, MeshOsConfig,
    };

    fn fast_config() -> MeshOsConfig {
        MeshOsConfig::default()
            .with_this_node(42)
            .with_tick_interval(Duration::from_millis(10))
            .with_event_queue_capacity(64)
            .with_action_queue_capacity(64)
    }

    #[tokio::test]
    async fn operator_identity_id_matches_keypair_origin_hash() {
        let kp = EntityKeypair::generate();
        let origin = kp.origin_hash();
        let identity = OperatorIdentity::from_keypair(kp);
        assert_eq!(identity.operator_id(), origin);
    }

    #[tokio::test]
    async fn deck_subnet_and_gateway_accessors_default_to_empty_without_mesh() {
        // Pin the "no mesh installed" contract — the new
        // subnet/gateway/channel accessors must surface
        // sensible empties rather than panicking. CliContext
        // currently wires DeckClient without a MeshNode; this
        // is the path operator tooling sees today.
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        assert_eq!(deck.local_subnet(), None);
        assert!(deck.known_subnets().is_empty());
        assert!(deck.gateway_stats().is_none());
        assert!(deck.gateway_exports().is_empty());
        assert_eq!(deck.channel_visibility("any/name"), None);
        assert!(deck.channels().is_empty());
        assert_eq!(deck.channel_wire_hash("any/name"), None);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn deck_with_mesh_surfaces_local_subnet_and_gateway_stats() {
        // Pin the "mesh installed" contract — `with_mesh` wires
        // the MeshNode reference through; the accessors then
        // return the substrate-level values. Uses
        // `set_channel_configs` to install a registry so the
        // gateway is built and `gateway_stats()` returns Some.
        use crate::adapter::net::{
            ChannelConfig, ChannelConfigRegistry, ChannelId, MeshNodeConfig, SubnetId, Visibility,
        };
        use std::net::SocketAddr;

        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);

        let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
        let mut mesh_cfg = MeshNodeConfig::new(addr, [0x17u8; 32]);
        mesh_cfg = mesh_cfg.with_subnet(SubnetId::new(&[3, 7]));
        let mut mesh = crate::adapter::net::MeshNode::new(EntityKeypair::generate(), mesh_cfg)
            .await
            .expect("MeshNode::new");
        let registry = Arc::new(ChannelConfigRegistry::new());
        let metrics_id = ChannelId::parse("internal/metrics").expect("channel id");
        registry.insert(
            ChannelConfig::new(metrics_id.clone()).with_visibility(Visibility::SubnetLocal),
        );
        mesh.set_channel_configs(registry);
        let mesh = Arc::new(mesh);

        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate())
            .with_mesh(mesh.clone());

        assert_eq!(deck.local_subnet(), Some(SubnetId::new(&[3, 7])));
        let stats = deck.gateway_stats().expect("gateway installed");
        assert_eq!(stats.local_subnet, SubnetId::new(&[3, 7]));
        assert_eq!(stats.forwarded, 0);
        assert_eq!(stats.dropped, 0);
        assert_eq!(stats.export_rules, 0);
        assert!(stats.peer_subnets.is_empty());

        // Channel-visibility lookup round-trips the configured
        // visibility for the one channel we registered.
        assert_eq!(
            deck.channel_visibility("internal/metrics"),
            Some(Visibility::SubnetLocal),
        );
        // The list surface mirrors the same channel.
        let channels = deck.channels();
        assert_eq!(channels.len(), 1);
        assert_eq!(channels[0].0, "internal/metrics");
        assert_eq!(channels[0].1, Visibility::SubnetLocal);
        // Wire-hash + canonical lookups resolve.
        assert_eq!(
            deck.channel_wire_hash("internal/metrics"),
            Some(metrics_id.wire_hash()),
        );
        assert_eq!(
            deck.channel_canonical_hash("internal/metrics"),
            Some(metrics_id.hash()),
        );

        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn deck_error_display_carries_kind_discriminator() {
        let err = DeckError::new("unknown_node", "node 99 is not in the cluster");
        let rendered = err.to_string();
        assert!(
            rendered.contains("<<deck-sdk-kind:unknown_node>>"),
            "expected discriminator envelope, got {rendered:?}",
        );
    }

    #[tokio::test]
    async fn admin_enter_maintenance_publishes_admin_event_and_returns_commit() {
        // Sanity that the SDK's admin path lands an AdminEvent on
        // the loop. We don't drive the executor here — the
        // assertion is "commit handle was returned + the loop's
        // fold saw the event," verified via the snapshot reader's
        // local_maintenance transition.
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let identity = OperatorIdentity::generate();
        let deck = DeckClient::from_runtime(&runtime, identity.clone());
        let commit = deck
            .admin()
            .enter_maintenance(42, None)
            .await
            .expect("commit");
        assert_eq!(commit.operator_id(), identity.operator_id());
        assert_eq!(commit.event_kind(), "enter_maintenance");
        assert!(commit.commit_id() >= 1);

        // Give the loop a tick to fold + publish the post-state
        // snapshot. The maintenance enter triggers an
        // `EnteringMaintenance` discriminant; the snapshot reader
        // reflects it.
        tokio::time::sleep(Duration::from_millis(80)).await;
        let snap = runtime.snapshot();
        assert!(
            !matches!(
                snap.local_maintenance,
                crate::adapter::net::behavior::meshos::MaintenanceStateSnapshot::Active
            ),
            "local maintenance should have transitioned out of Active, got {:?}",
            snap.local_maintenance,
        );

        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn admin_drop_replicas_publishes_with_supplied_chain_ids() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let commit = deck
            .admin()
            .drop_replicas(42, vec![1, 2, 3])
            .await
            .expect("commit");
        assert_eq!(commit.event_kind(), "drop_replicas");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn commit_ids_increment_monotonically_per_client() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let a = deck.admin().cordon(42).await.unwrap();
        let b = deck.admin().uncordon(42).await.unwrap();
        assert!(b.commit_id() > a.commit_id());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn snapshot_stream_yields_a_snapshot_per_poll_interval() {
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(20),
                ..DeckClientConfig::default()
            },
        );

        let mut stream = deck.snapshots();
        // First tick lands immediately (tokio::time::interval
        // fires on first poll); collect two ticks and assert both
        // are Ok.
        let first = stream.next().await.expect("first").expect("ok");
        let second = stream.next().await.expect("second").expect("ok");
        // Same shape — both came from the same reader.
        assert_eq!(first.local_maintenance, second.local_maintenance);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn snapshot_stream_observes_admin_command_aftermath() {
        // The interesting end-to-end shape: issue an admin
        // command, then read a snapshot from the stream and
        // confirm the loop folded the event. Mirrors what
        // Deck-the-binary will see.
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        let _ = deck.admin().enter_maintenance(42, None).await.unwrap();

        // Skip a few stream frames so the loop's tick has folded
        // the admin event + published a fresh snapshot.
        let mut stream = deck.snapshots();
        let mut saw_transition = false;
        for _ in 0..20 {
            let snap = stream.next().await.expect("next").expect("ok");
            if !matches!(
                snap.local_maintenance,
                crate::adapter::net::behavior::meshos::MaintenanceStateSnapshot::Active
            ) {
                saw_transition = true;
                break;
            }
        }
        assert!(
            saw_transition,
            "stream should have surfaced a non-Active local_maintenance after enter_maintenance",
        );
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn admin_commit_after_runtime_shutdown_returns_loop_closed_error() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let _ = runtime.shutdown().await;
        // The runtime's `shutdown` future drains the loop and
        // drops the loop side of the publish channel. The SDK's
        // own handle is a clone; publishing on it returns
        // `LoopClosed` once the loop exits.
        let err = deck
            .admin()
            .cordon(42)
            .await
            .expect_err("publish after shutdown should fail");
        assert_eq!(err.kind, "loop_closed");
    }

    // Silence the unused-import warning for types we re-export but
    // don't construct directly in tests.
    #[allow(dead_code)]
    fn _ensure_action_types_are_in_scope() -> (MaintenanceTransition, MeshOsAction) {
        (
            MaintenanceTransition::EnteringMaintenance,
            MeshOsAction::CommitMaintenanceTransition {
                node: 0,
                target: MaintenanceTransition::EnteringMaintenance,
            },
        )
    }

    // Note: a "commit without simulate" test would not compile —
    // `IceProposal` does not expose `commit`; only the
    // `SimulatedIceProposal` returned from `IceProposal::simulate`
    // does. The type-state split enforces locked decision #4 at
    // compile time, so no runtime simulation-required gate exists
    // to test.

    #[tokio::test]
    async fn ice_proposal_commit_with_insufficient_signatures_fails() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        // Bump threshold above what we'll supply.
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(100),
                ice_signature_threshold: 2,
            },
        );
        let proposal = deck.ice().freeze_cluster(Duration::from_secs(10));
        let simulated = proposal.simulate().await.expect("simulate");
        let sig = deck.identity().sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let err = simulated
            .commit(&[sig])
            .await
            .expect_err("under-threshold commit should fail");
        assert_eq!(err.kind, "insufficient_signatures");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_freeze_proposal_simulate_then_commit_lands_freeze_on_loop() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        let proposal = deck.ice().freeze_cluster(Duration::from_secs(30));
        let simulated = proposal.simulate().await.expect("simulate");
        // FreezeCluster sets the drain delay to the requested TTL.
        assert_eq!(
            simulated.blast_radius().estimated_drain_delay,
            Some(Duration::from_secs(30))
        );
        let sig = deck.identity().sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let commit = simulated.commit(&[sig]).await.expect("commit");
        assert_eq!(commit.event_kind(), "freeze_cluster");

        // Give the loop a tick + reconcile + publish to fold the
        // freeze through to the snapshot.
        tokio::time::sleep(Duration::from_millis(80)).await;
        let snap = runtime.snapshot();
        assert!(
            snap.freeze_remaining_ms.is_some(),
            "freeze_remaining_ms should be set after committed freeze",
        );
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_thaw_proposal_simulate_warns_no_op_when_unfrozen() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        let proposal = deck.ice().thaw_cluster();
        let simulated = proposal.simulate().await.expect("simulate");
        // Simulator on a non-frozen snapshot warns "no freeze to cancel."
        assert!(simulated.blast_radius().warnings.iter().any(|w| matches!(
            w,
            crate::adapter::net::behavior::meshos::BlastWarning::ThawHasNoFreezeToCancel
        )));
        let _ = runtime.shutdown().await;
    }

    /// Test helper: a non-sentinel blast-radius hash to use in
    /// tests that don't construct a real `BlastRadius` (they're
    /// exercising the signature plumbing, not the simulation
    /// gate).
    const TEST_BLAST_HASH: super::super::meshos::BlastRadiusHash =
        [1u8; super::super::meshos::BLAST_RADIUS_HASH_LEN];

    /// Static assertion: every public stream type returned by
    /// the SDK must be `Send` so callers can move them across
    /// `tokio::spawn` boundaries. A future internal-field swap
    /// to a `!Send` type silently breaks every downstream
    /// `spawn` consumer; pinning the property here keeps that
    /// regression out of CI. The ICE proposal type-state pair
    /// also pins `Send + Sync` for the same reason.
    fn _assert_proposal_send_sync_static_check() {
        fn _assert_send<T: Send>() {}
        fn _assert_send_sync<T: Send + Sync>() {}
        _assert_send_sync::<IceProposal<'static>>();
        _assert_send_sync::<SimulatedIceProposal<'static>>();
        _assert_send::<SnapshotStream>();
        _assert_send::<StatusSummaryStream>();
        _assert_send::<AuditStream>();
        _assert_send::<LogStream>();
        _assert_send::<FailureStream>();
    }

    #[tokio::test]
    async fn operator_signature_carries_issuing_operator_id() {
        let identity = OperatorIdentity::generate();
        let proposal = IceActionProposal::FreezeCluster {
            ttl: Duration::from_secs(60),
        };
        let sig = identity.sign_proposal(
            &proposal,
            super::super::meshos::now_ms_since_unix_epoch(),
            &TEST_BLAST_HASH,
        );
        assert_eq!(sig.operator_id, identity.operator_id());
        // ed25519 signatures are 64 bytes.
        assert_eq!(sig.signature.len(), 64);
    }

    #[tokio::test]
    async fn operator_registry_verifies_a_well_formed_signature() {
        let identity = OperatorIdentity::generate();
        let mut registry = OperatorRegistry::new();
        registry.register(identity.keypair());

        let proposal = IceActionProposal::FreezeCluster {
            ttl: Duration::from_secs(60),
        };
        let ts = super::super::meshos::now_ms_since_unix_epoch();
        let sig = identity.sign_proposal(&proposal, ts, &TEST_BLAST_HASH);
        let payload = ice_proposal_signing_payload(&proposal, ts, &TEST_BLAST_HASH);
        registry.verify(&sig, &payload).expect("valid signature");
    }

    #[tokio::test]
    async fn operator_registry_rejects_unknown_operator() {
        let registry = OperatorRegistry::new();
        let identity = OperatorIdentity::generate();
        let proposal = IceActionProposal::ThawCluster;
        let ts = super::super::meshos::now_ms_since_unix_epoch();
        let sig = identity.sign_proposal(&proposal, ts, &TEST_BLAST_HASH);
        let payload = ice_proposal_signing_payload(&proposal, ts, &TEST_BLAST_HASH);
        let err = registry
            .verify(&sig, &payload)
            .expect_err("unregistered operator should not verify");
        assert_eq!(err.kind(), "not_authorized");
    }

    #[tokio::test]
    async fn operator_registry_rejects_tampered_signature_bytes() {
        let identity = OperatorIdentity::generate();
        let mut registry = OperatorRegistry::new();
        registry.register(identity.keypair());

        let proposal = IceActionProposal::FreezeCluster {
            ttl: Duration::from_secs(10),
        };
        let ts = super::super::meshos::now_ms_since_unix_epoch();
        let mut sig = identity.sign_proposal(&proposal, ts, &TEST_BLAST_HASH);
        // Flip one byte in the signature.
        sig.signature[0] ^= 0x01;
        let payload = ice_proposal_signing_payload(&proposal, ts, &TEST_BLAST_HASH);
        let err = registry
            .verify(&sig, &payload)
            .expect_err("tampered signature should not verify");
        assert_eq!(err.kind(), "signature_invalid");
    }

    #[tokio::test]
    async fn operator_registry_rejects_signature_for_wrong_payload() {
        // A signature over `FreezeCluster { 10s }` should not
        // verify against the payload of a different proposal.
        // This is the contract the substrate verifier will rely
        // on to reject signature reuse across proposals.
        let identity = OperatorIdentity::generate();
        let mut registry = OperatorRegistry::new();
        registry.register(identity.keypair());

        let signed_proposal = IceActionProposal::FreezeCluster {
            ttl: Duration::from_secs(10),
        };
        let other_proposal = IceActionProposal::FreezeCluster {
            ttl: Duration::from_secs(60),
        };
        let ts = super::super::meshos::now_ms_since_unix_epoch();
        let sig = identity.sign_proposal(&signed_proposal, ts, &TEST_BLAST_HASH);
        let payload = ice_proposal_signing_payload(&other_proposal, ts, &TEST_BLAST_HASH);
        let err = registry
            .verify(&sig, &payload)
            .expect_err("cross-proposal signature should not verify");
        assert_eq!(err.kind(), "signature_invalid");
    }

    #[tokio::test]
    async fn operator_registry_rejects_wrong_length_signature() {
        let identity = OperatorIdentity::generate();
        let mut registry = OperatorRegistry::new();
        registry.register(identity.keypair());

        let proposal = IceActionProposal::ThawCluster;
        let sig = OperatorSignature {
            operator_id: identity.operator_id(),
            signature: vec![0; 32], // wrong length
        };
        let payload = ice_proposal_signing_payload(
            &proposal,
            super::super::meshos::now_ms_since_unix_epoch(),
            &TEST_BLAST_HASH,
        );
        let err = registry
            .verify(&sig, &payload)
            .expect_err("wrong-length signature should not verify");
        assert_eq!(err.kind(), "signature_invalid");
    }

    #[tokio::test]
    async fn ice_commit_with_registry_rejects_an_unverified_signature() {
        // Build a two-operator bundle where one signature is
        // tampered. The threshold is met (2 sigs supplied) but
        // verification rejects the bundle.
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let op_a = OperatorIdentity::generate();
        let op_b = OperatorIdentity::generate();
        let mut registry = OperatorRegistry::new();
        registry.register(op_a.keypair());
        registry.register(op_b.keypair());
        let deck = DeckClient::new(
            runtime.handle_clone(),
            runtime.snapshot_reader().clone(),
            op_a.clone(),
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(100),
                ice_signature_threshold: 2,
            },
        )
        .with_operator_registry(registry);

        let proposal = deck.ice().freeze_cluster(Duration::from_secs(15));
        let simulated = proposal.simulate().await.expect("simulate");
        let sig_a = op_a.sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let mut sig_b = op_b.sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        sig_b.signature[3] ^= 0xFF; // tamper

        let err = simulated
            .commit(&[sig_a, sig_b])
            .await
            .expect_err("commit with tampered sig should fail");
        assert_eq!(err.kind, "signature_invalid");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_flush_avoid_lists_proposal_simulate_and_commit_round_trips() {
        use super::super::meshos::AvoidScope;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let proposal = deck.ice().flush_avoid_lists(AvoidScope::OnPeer { peer: 5 });
        let simulated = proposal.simulate().await.expect("simulate");
        // OnPeer scope flushes everywhere; without registered
        // peers in the snapshot the affected_nodes list is
        // empty but the warning still fires.
        assert!(simulated.blast_radius().warnings.iter().any(|w| matches!(
            w,
            crate::adapter::net::behavior::meshos::BlastWarning::AvoidFlushRecoversPeer { peer: 5 }
        )));
        // commit through the unsigned path (no registry installed).
        let sig = deck.identity().sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let commit = simulated.commit(&[sig]).await.expect("commit");
        assert_eq!(commit.event_kind(), "flush_avoid_lists");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn status_summary_stream_emits_initial_summary_immediately() {
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(10),
                ..DeckClientConfig::default()
            },
        );
        let mut stream = deck.status_summary_stream();
        let first = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("first timed out")
            .expect("first closed")
            .expect("first ok");
        // Steady-state idle cluster — every count is zero.
        assert!(first.freeze_remaining_ms.is_none());
        assert!(!first.local_maintenance_active);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn status_summary_stream_dedups_unchanged_summaries() {
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(10),
                ..DeckClientConfig::default()
            },
        );
        let mut stream = deck.status_summary_stream();
        // First emission lands.
        let _ = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("first")
            .expect("closed")
            .expect("ok");
        // No state change — the stream should park (dedup).
        let second = tokio::time::timeout(Duration::from_millis(80), stream.next()).await;
        assert!(
            second.is_err(),
            "stream should not re-emit unchanged summary"
        );
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn status_summary_stream_re_emits_on_freeze_state_change() {
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(10),
                ..DeckClientConfig::default()
            },
        );
        let mut stream = deck.status_summary_stream();
        let first = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("first")
            .expect("closed")
            .expect("ok");
        assert!(first.freeze_remaining_ms.is_none());

        // Freeze the cluster — the next polled summary will
        // differ (`freeze_remaining_ms` flips to `Some`), so
        // the stream re-emits + the audit ring depth bumps.
        let p = deck
            .ice()
            .freeze_cluster(Duration::from_secs(30))
            .simulate()
            .await
            .expect("simulate");
        let sig = deck
            .identity()
            .sign_proposal(p.action(), p.issued_at_ms(), &p.blast_hash());
        p.commit(&[sig]).await.expect("freeze");
        let after_freeze = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("after_freeze timed out")
            .expect("after_freeze closed")
            .expect("after_freeze ok");
        assert!(after_freeze.freeze_remaining_ms.is_some());
        assert!(after_freeze.admin_audit_ring_depth >= 1);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn status_summary_reflects_steady_state_idle_cluster() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let summary = deck.status_summary();
        assert_eq!(summary.peers, PeerCounts::default());
        assert_eq!(summary.daemons, DaemonCounts::default());
        assert_eq!(summary.replica_chains, 0);
        assert_eq!(summary.recently_emitted_count, 0);
        assert!(summary.freeze_remaining_ms.is_none());
        assert!(!summary.local_maintenance_active);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn status_summary_flags_freeze_after_freeze_cluster_commit() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let p = deck
            .ice()
            .freeze_cluster(Duration::from_secs(30))
            .simulate()
            .await
            .expect("simulate");
        let sig = deck
            .identity()
            .sign_proposal(p.action(), p.issued_at_ms(), &p.blast_hash());
        p.commit(&[sig]).await.expect("freeze");
        tokio::time::sleep(Duration::from_millis(60)).await;
        let summary = deck.status_summary();
        assert!(summary.freeze_remaining_ms.is_some());
        // Audit ring should have at least one entry now.
        assert!(summary.admin_audit_ring_depth >= 1);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn status_summary_flags_local_maintenance_after_enter_maintenance() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        // `fast_config` pins this_node = 42; target the same.
        deck.admin()
            .enter_maintenance(42, None)
            .await
            .expect("commit");
        tokio::time::sleep(Duration::from_millis(60)).await;
        let summary = deck.status_summary();
        assert!(
            summary.local_maintenance_active,
            "local_maintenance_active should flip on after enter_maintenance",
        );
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn subscribe_failures_yields_seeded_dispatcher_rejection() {
        use crate::adapter::net::behavior::meshos::DispatchError;
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        dispatcher.fail_next(DispatchError::drop("first"));
        let runtime = MeshOsRuntime::start(fast_config(), Arc::clone(&dispatcher));
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );
        let mut stream = deck.subscribe_failures(0);

        deck.admin().enter_maintenance(42, None).await.unwrap();

        let record = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("timed out")
            .expect("closed")
            .expect("ok");
        // The executor stamps strictly-positive seqs; chain-
        // replay-derived records carry seq=0.
        assert!(record.seq > 0);
        assert!(record.reason.contains("first"));
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn subscribe_failures_since_seq_drops_already_seen() {
        use crate::adapter::net::behavior::meshos::DispatchError;
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        dispatcher.fail_next(DispatchError::drop("first"));
        let runtime = MeshOsRuntime::start(fast_config(), Arc::clone(&dispatcher));
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        deck.admin().enter_maintenance(42, None).await.unwrap();
        // Wait for the first failure to land on the ring.
        let deadline = std::time::Instant::now() + Duration::from_secs(2);
        let mut seq_seen = 0u64;
        while std::time::Instant::now() < deadline {
            let all = deck.recent_failures();
            if let Some(r) = all.last() {
                seq_seen = r.seq;
                break;
            }
            tokio::time::sleep(Duration::from_millis(20)).await;
        }
        assert!(seq_seen > 0);

        // Subscribe with the seq already-seen as the
        // watermark; no new failures => stream parks.
        let mut stream = deck.subscribe_failures(seq_seen);
        let parked = tokio::time::timeout(Duration::from_millis(60), stream.next()).await;
        assert!(parked.is_err(), "no new failures means parked stream");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn recent_failures_surfaces_dispatcher_rejections() {
        use crate::adapter::net::behavior::meshos::DispatchError;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        dispatcher.fail_next(DispatchError::drop("synthetic rejection"));
        let runtime = MeshOsRuntime::start(fast_config(), Arc::clone(&dispatcher));
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        // Drive an admin event whose dispatched action will be
        // rejected — reconcile emits `CommitMaintenanceTransition`
        // for an empty-workload enter_maintenance.
        deck.admin()
            .enter_maintenance(42, None)
            .await
            .expect("commit");

        // Poll up to 2s for the failure to land on the ring.
        let deadline = std::time::Instant::now() + Duration::from_secs(2);
        let mut got: Vec<crate::adapter::net::behavior::meshos::FailureRecord> = Vec::new();
        while std::time::Instant::now() < deadline {
            got = deck.recent_failures();
            if !got.is_empty() {
                break;
            }
            tokio::time::sleep(Duration::from_millis(20)).await;
        }
        assert!(
            !got.is_empty(),
            "recent_failures should reflect the seeded dispatcher rejection",
        );
        assert!(got[0].reason.contains("synthetic rejection"));
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn recent_failures_since_drops_records_at_or_below_cutoff() {
        use crate::adapter::net::behavior::meshos::DispatchError;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        dispatcher.fail_next(DispatchError::drop("first failure"));
        let runtime = MeshOsRuntime::start(fast_config(), Arc::clone(&dispatcher));
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        deck.admin()
            .enter_maintenance(42, None)
            .await
            .expect("commit");
        let deadline = std::time::Instant::now() + Duration::from_secs(2);
        let mut all: Vec<crate::adapter::net::behavior::meshos::FailureRecord> = Vec::new();
        while std::time::Instant::now() < deadline {
            all = deck.recent_failures();
            if !all.is_empty() {
                break;
            }
            tokio::time::sleep(Duration::from_millis(20)).await;
        }
        assert!(!all.is_empty(), "seed failure should land");

        // Set the watermark to the existing record's ms — the
        // since filter uses `>` so the same record is dropped.
        let cutoff = all[0].recorded_at_ms;
        let after = deck.recent_failures_since(cutoff);
        assert!(
            after.iter().all(|r| r.recorded_at_ms > cutoff),
            "since filter should drop records at the cutoff",
        );
        // The seed record itself shouldn't appear (its ms ==
        // cutoff).
        assert!(after.iter().all(|r| r.reason != "first failure"));
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn per_field_accessors_match_full_snapshot_contents() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        let snap = deck.status();
        assert_eq!(deck.peers(), snap.peers);
        assert_eq!(deck.daemons(), snap.daemons);
        assert_eq!(deck.replicas(), snap.replicas);
        assert_eq!(deck.local_maintenance(), snap.local_maintenance);
        assert_eq!(deck.freeze_remaining_ms(), snap.freeze_remaining_ms);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn status_returns_freshest_snapshot_synchronously() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        // Default state — local maintenance is Active.
        let s = deck.status();
        assert!(matches!(
            s.local_maintenance,
            crate::adapter::net::behavior::meshos::MaintenanceStateSnapshot::Active
        ));

        // Issue a freeze; subsequent status() should see the
        // freeze once the loop folds.
        let p = deck
            .ice()
            .freeze_cluster(Duration::from_secs(20))
            .simulate()
            .await
            .expect("simulate");
        let sig = deck
            .identity()
            .sign_proposal(p.action(), p.issued_at_ms(), &p.blast_hash());
        p.commit(&[sig]).await.expect("commit");
        tokio::time::sleep(Duration::from_millis(60)).await;
        let s = deck.status();
        assert!(s.freeze_remaining_ms.is_some());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn watch_resolves_immediately_when_predicate_already_true() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        // Default state matches "no freeze in effect" — should
        // resolve immediately.
        let snap = tokio::time::timeout(
            Duration::from_millis(50),
            deck.watch(|s| s.freeze_remaining_ms.is_none()),
        )
        .await
        .expect("watch should not block when predicate already holds");
        assert!(snap.freeze_remaining_ms.is_none());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn watch_resolves_when_predicate_becomes_true_after_admin_commit() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(10),
                ..DeckClientConfig::default()
            },
        );

        // Spawn a watcher that waits for a freeze.
        let deck_handle = deck.snapshot_reader.clone();
        let watcher = {
            let identity = deck.identity().clone();
            let config = deck.config.clone();
            let handle = deck.handle.clone();
            // Build a sibling client that shares the same
            // snapshot reader — `DeckClient` isn't `Clone` and
            // a real consumer would `Arc::clone` the outer
            // handle, but spawning shows the watch is non-
            // blocking on the SDK level.
            let client = DeckClient::new(handle, deck_handle.clone(), identity, config);
            tokio::spawn(async move { client.watch(|s| s.freeze_remaining_ms.is_some()).await })
        };

        // Wait a beat so the watcher is in its sleep loop.
        tokio::time::sleep(Duration::from_millis(40)).await;
        let p = deck
            .ice()
            .freeze_cluster(Duration::from_secs(15))
            .simulate()
            .await
            .expect("simulate");
        let sig = deck
            .identity()
            .sign_proposal(p.action(), p.issued_at_ms(), &p.blast_hash());
        p.commit(&[sig]).await.expect("commit");

        let snap = tokio::time::timeout(Duration::from_secs(2), watcher)
            .await
            .expect("watcher should resolve")
            .expect("join");
        assert!(snap.freeze_remaining_ms.is_some());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn watch_timeout_returns_watch_timeout_error_when_predicate_never_holds() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(10),
                ..DeckClientConfig::default()
            },
        );

        let err = deck
            .watch_timeout(
                |s| s.freeze_remaining_ms.is_some(),
                Duration::from_millis(80),
            )
            .await
            .expect_err("predicate never holds, should time out");
        assert_eq!(err.kind, "watch_timeout");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_since_filter_drops_records_at_or_below_watermark() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        deck.admin().cordon(42).await.unwrap();
        deck.admin().uncordon(42).await.unwrap();
        deck.admin().invalidate_placement(42).await.unwrap();
        tokio::time::sleep(Duration::from_millis(80)).await;

        let all = deck.audit().collect();
        assert_eq!(all.len(), 3);
        // Pick the middle record's seq as the watermark; only
        // the newest record (seq strictly greater) should
        // surface.
        let middle_seq = all[1].seq;
        let after_middle = deck.audit().since(middle_seq).collect();
        assert_eq!(after_middle.len(), 1, "since should keep only seq > middle");
        assert!(after_middle[0].seq > middle_seq);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_stream_since_seeds_initial_watermark() {
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        // Land three commits before subscribing.
        deck.admin().cordon(42).await.unwrap();
        deck.admin().uncordon(42).await.unwrap();
        deck.admin().invalidate_placement(42).await.unwrap();
        tokio::time::sleep(Duration::from_millis(80)).await;

        let all = deck.audit().collect();
        assert_eq!(all.len(), 3);
        // Resume from the middle entry's seq. Stream should
        // yield only the newest entry (seq strictly above
        // middle) then park.
        let middle_seq = all[1].seq;
        let mut stream = deck.audit().since(middle_seq).stream();
        let next = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("timed out")
            .expect("closed")
            .expect("ok");
        assert!(next.seq > middle_seq);
        // No more records — stream parks.
        let parked = tokio::time::timeout(Duration::from_millis(40), stream.next()).await;
        assert!(
            parked.is_err(),
            "stream should park after watermark catches up"
        );
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn log_filter_since_seeds_stream_watermark() {
        use crate::adapter::net::behavior::meshos::{LogLine, MeshOsEvent};
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        // Publish three lines and snapshot the middle seq.
        for i in 0..3 {
            runtime
                .handle()
                .publish(MeshOsEvent::LogLine(LogLine::info(
                    None,
                    format!("msg {i}"),
                )))
                .await
                .unwrap();
        }
        tokio::time::sleep(Duration::from_millis(80)).await;

        let snap = runtime.snapshot();
        assert_eq!(snap.log_ring.len(), 3);
        let middle_seq = snap.log_ring[1].seq;

        // Subscribe with since() seeded to the middle seq.
        // Stream should yield only the third line (seq > middle).
        let mut stream = deck.subscribe_logs(LogFilter::new().since(middle_seq));
        let next = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("timed out")
            .expect("closed")
            .expect("ok");
        assert!(next.seq > middle_seq);
        assert_eq!(next.message, "msg 2");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn subscribe_logs_yields_published_log_lines_in_seq_order() {
        use crate::adapter::net::behavior::meshos::{LogLevel, LogLine, MeshOsEvent};
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        let mut stream = deck.subscribe_logs(LogFilter::new());
        for (i, level) in [LogLevel::Info, LogLevel::Warn, LogLevel::Error]
            .into_iter()
            .enumerate()
        {
            runtime
                .handle()
                .publish(MeshOsEvent::LogLine(LogLine {
                    level,
                    daemon_id: Some(7),
                    message: format!("msg {}", i),
                }))
                .await
                .unwrap();
        }

        let r1 = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("r1 timed out")
            .expect("r1 closed")
            .expect("r1 ok");
        let r2 = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("r2 timed out")
            .expect("r2 closed")
            .expect("r2 ok");
        let r3 = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("r3 timed out")
            .expect("r3 closed")
            .expect("r3 ok");
        assert!(r1.seq < r2.seq);
        assert!(r2.seq < r3.seq);
        assert_eq!(r1.level, LogLevel::Info);
        assert_eq!(r3.level, LogLevel::Error);
        // The loop stamps `node_id = Some(this_node)` on every
        // locally-published line.
        assert_eq!(r1.node_id, Some(42));
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn subscribe_logs_min_level_filter_drops_below_threshold() {
        use crate::adapter::net::behavior::meshos::{LogLevel, LogLine, MeshOsEvent};
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        let mut stream = deck.subscribe_logs(LogFilter::new().min_level(LogLevel::Warn));
        runtime
            .handle()
            .publish(MeshOsEvent::LogLine(LogLine::info(None, "info dropped")))
            .await
            .unwrap();
        runtime
            .handle()
            .publish(MeshOsEvent::LogLine(LogLine::warn(None, "warn kept")))
            .await
            .unwrap();

        let next = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("next timed out")
            .expect("next closed")
            .expect("next ok");
        assert_eq!(next.level, LogLevel::Warn);
        assert_eq!(next.message, "warn kept");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn subscribe_logs_with_daemon_filter_keeps_only_matching_daemon() {
        use crate::adapter::net::behavior::meshos::{LogLine, MeshOsEvent};
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        let mut stream = deck.subscribe_logs(LogFilter::new().with_daemon(7));
        runtime
            .handle()
            .publish(MeshOsEvent::LogLine(LogLine::info(
                Some(99),
                "other daemon",
            )))
            .await
            .unwrap();
        runtime
            .handle()
            .publish(MeshOsEvent::LogLine(LogLine::info(
                Some(7),
                "target daemon",
            )))
            .await
            .unwrap();

        let next = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("next timed out")
            .expect("next closed")
            .expect("next ok");
        assert_eq!(next.daemon_id, Some(7));
        assert_eq!(next.message, "target daemon");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_stream_emits_one_record_per_signed_commit_in_order() {
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(15),
                ..DeckClientConfig::default()
            },
        );

        let mut stream = deck.audit().stream();
        // No commits yet — the stream should yield nothing for
        // at least one tick.
        let first_attempt = tokio::time::timeout(Duration::from_millis(40), stream.next()).await;
        assert!(first_attempt.is_err(), "stream should park when no records");

        // Issue three commits; the stream should yield three
        // records in seq order.
        deck.admin().cordon(42).await.unwrap();
        deck.admin().uncordon(42).await.unwrap();
        deck.admin().invalidate_placement(42).await.unwrap();

        let r1 = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("r1 timed out")
            .expect("r1 closed")
            .expect("r1 ok");
        let r2 = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("r2 timed out")
            .expect("r2 closed")
            .expect("r2 ok");
        let r3 = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("r3 timed out")
            .expect("r3 closed")
            .expect("r3 ok");

        // Stream emits in seq order (substrate guarantees seq
        // strictly increases).
        assert!(r1.seq < r2.seq);
        assert!(r2.seq < r3.seq);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_stream_dedups_already_seen_records_across_polls() {
        // Two consecutive polls on the same snapshot must NOT
        // re-emit records. The seq-based watermark guarantees
        // this even when the snapshot ring carries records
        // older than the stream's watermark.
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(10),
                ..DeckClientConfig::default()
            },
        );

        deck.admin().cordon(42).await.unwrap();
        let mut stream = deck.audit().stream();
        let first = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("first timed out")
            .expect("first closed")
            .expect("first ok");

        // No new commits — the stream should park (not
        // re-emit `first`).
        let second_attempt = tokio::time::timeout(Duration::from_millis(50), stream.next()).await;
        assert!(
            second_attempt.is_err(),
            "stream should not re-emit seen record"
        );

        // Issue another commit; only the new one shows up.
        deck.admin().uncordon(42).await.unwrap();
        let second = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("second timed out")
            .expect("second closed")
            .expect("second ok");
        assert!(second.seq > first.seq);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_stream_applies_force_only_filter_in_tail_mode() {
        // Mix ordinary (Cordon) and ICE (ThawCluster). With
        // force_only(), the stream yields only the ICE entry.
        use futures::StreamExt;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate()).with_config(
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(10),
                ..DeckClientConfig::default()
            },
        );

        let mut stream = deck.audit().force_only().stream();
        deck.admin().cordon(42).await.unwrap();
        let thaw = deck
            .ice()
            .thaw_cluster()
            .simulate()
            .await
            .expect("simulate");
        let sig =
            deck.identity()
                .sign_proposal(thaw.action(), thaw.issued_at_ms(), &thaw.blast_hash());
        thaw.commit(&[sig]).await.unwrap();

        let next = tokio::time::timeout(Duration::from_secs(2), stream.next())
            .await
            .expect("next timed out")
            .expect("next closed")
            .expect("next ok");
        // Only the ThawCluster (ICE) should pass the filter.
        assert!(next.event.is_ice());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_query_returns_empty_when_no_ice_commits_observed() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let results = deck.audit().recent(10).collect();
        assert!(results.is_empty());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_query_returns_recent_entries_newest_first() {
        // Two unsigned commits — they reach the loop via the
        // unsigned admin path because no registry is installed.
        // The audit ring only records `SignedIceCommit` events,
        // so unsigned commits don't appear. We instead publish
        // `SignedIceCommit` directly (no verifier installed, so
        // outcome = Unverified — but the ring records every
        // attempt regardless).
        use crate::adapter::net::behavior::meshos::{IceActionProposal, MeshOsEvent};
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        for ttl_secs in [10, 20, 30] {
            runtime
                .handle()
                .publish(MeshOsEvent::SignedIceCommit {
                    proposal: IceActionProposal::FreezeCluster {
                        ttl: Duration::from_secs(ttl_secs),
                    },
                    signatures: Vec::new(),
                    issued_at_ms: super::super::meshos::now_ms_since_unix_epoch(),
                    blast_hash: TEST_BLAST_HASH,
                })
                .await
                .unwrap();
        }
        tokio::time::sleep(Duration::from_millis(80)).await;
        let all = deck.audit().collect();
        assert_eq!(all.len(), 3, "ring should hold all three entries");
        // Newest-first ordering: the 30s freeze is the last
        // commit submitted, so it should be the first result.
        assert!(matches!(
            all[0].event,
            AdminEvent::FreezeCluster { ttl } if ttl == Duration::from_secs(30)
        ));
        assert!(matches!(
            all[2].event,
            AdminEvent::FreezeCluster { ttl } if ttl == Duration::from_secs(10)
        ));

        let recent_one = deck.audit().recent(1).collect();
        assert_eq!(recent_one.len(), 1);
        assert!(matches!(
            recent_one[0].event,
            AdminEvent::FreezeCluster { ttl } if ttl == Duration::from_secs(30)
        ));
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_query_filters_by_operator_id() {
        use crate::adapter::net::behavior::meshos::{IceActionProposal, MeshOsEvent};
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let op_a = OperatorIdentity::generate();
        let op_b = OperatorIdentity::generate();
        let deck = DeckClient::from_runtime(&runtime, op_a.clone());

        // Commit from op_a.
        let proposal_a = IceActionProposal::FreezeCluster {
            ttl: Duration::from_secs(10),
        };
        let ts_a = super::super::meshos::now_ms_since_unix_epoch();
        let sig_a = OperatorSignature::sign(op_a.keypair(), &proposal_a, ts_a, &TEST_BLAST_HASH);
        runtime
            .handle()
            .publish(MeshOsEvent::SignedIceCommit {
                proposal: proposal_a,
                signatures: vec![sig_a],
                issued_at_ms: ts_a,
                blast_hash: TEST_BLAST_HASH,
            })
            .await
            .unwrap();
        // Commit from op_b.
        let proposal_b = IceActionProposal::ThawCluster;
        let ts_b = super::super::meshos::now_ms_since_unix_epoch();
        let sig_b = OperatorSignature::sign(op_b.keypair(), &proposal_b, ts_b, &TEST_BLAST_HASH);
        runtime
            .handle()
            .publish(MeshOsEvent::SignedIceCommit {
                proposal: proposal_b,
                signatures: vec![sig_b],
                issued_at_ms: ts_b,
                blast_hash: TEST_BLAST_HASH,
            })
            .await
            .unwrap();
        tokio::time::sleep(Duration::from_millis(80)).await;

        let filtered = deck.audit().by_operator(op_a.operator_id()).collect();
        assert_eq!(filtered.len(), 1);
        assert!(matches!(
            filtered[0].event,
            AdminEvent::FreezeCluster { .. }
        ));
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_query_force_only_drops_ordinary_admin_keeps_ice() {
        // Mix ordinary admin (Cordon) with ICE (ThawCluster);
        // force_only() should keep only the ICE entry.
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        deck.admin().cordon(42).await.expect("cordon");
        let thaw = deck
            .ice()
            .thaw_cluster()
            .simulate()
            .await
            .expect("simulate");
        let sig =
            deck.identity()
                .sign_proposal(thaw.action(), thaw.issued_at_ms(), &thaw.blast_hash());
        thaw.commit(&[sig]).await.expect("thaw");
        tokio::time::sleep(Duration::from_millis(80)).await;

        let baseline = deck.audit().collect();
        assert_eq!(
            baseline.len(),
            2,
            "ring should hold both ordinary and ICE commits"
        );
        let force_only = deck.audit().force_only().collect();
        assert_eq!(force_only.len(), 1, "force_only should drop Cordon");
        assert!(force_only[0].event.is_ice());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn admin_commit_routes_through_signed_path_when_registry_installed() {
        // With an OperatorRegistry installed, the SDK's
        // AdminCommands signs every admin event and routes via
        // SignedAdminCommit; the substrate verifier accepts +
        // the audit ring shows the operator + Accepted outcome.
        use std::sync::Arc as SArc;
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let identity = OperatorIdentity::generate();
        let mut registry = OperatorRegistry::new();
        registry.register(identity.keypair());
        let verifier = SArc::new(crate::adapter::net::behavior::meshos::AdminVerifier::new(
            SArc::new(registry.clone()),
            1,
        ));
        let runtime = MeshOsRuntime::start_with_all(
            fast_config(),
            dispatcher,
            Default::default(),
            Default::default(),
            SArc::new(crate::adapter::net::compute::DaemonRegistry::new()),
            None,
            Some(verifier),
        );
        let deck =
            DeckClient::from_runtime(&runtime, identity.clone()).with_operator_registry(registry);

        let commit = deck.admin().cordon(42).await.expect("commit");
        assert_eq!(commit.event_kind(), "cordon");

        // Audit ring should show the commit with Accepted
        // outcome + the issuing operator id.
        tokio::time::sleep(Duration::from_millis(80)).await;
        let entries = deck.audit().collect();
        assert_eq!(entries.len(), 1);
        assert!(matches!(
            entries[0].outcome,
            crate::adapter::net::behavior::meshos::VerificationOutcome::Accepted
        ));
        assert_eq!(entries[0].operator_ids, vec![identity.operator_id()]);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn admin_commit_falls_back_to_unsigned_when_no_registry_installed() {
        // Without a registry the SDK routes through the
        // legacy unsigned admin path. Audit ring still records
        // the commit but with Unverified outcome.
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        deck.admin().cordon(42).await.expect("commit");
        tokio::time::sleep(Duration::from_millis(80)).await;

        let entries = deck.audit().collect();
        assert_eq!(entries.len(), 1);
        assert!(matches!(
            entries[0].outcome,
            crate::adapter::net::behavior::meshos::VerificationOutcome::Unverified
        ));
        assert!(entries[0].operator_ids.is_empty());
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_ring_records_unsigned_admin_with_unverified_outcome() {
        // Locked decision #2: every admin event the loop sees
        // is on the audit ring. Unsigned ones surface as
        // Unverified.
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        deck.admin().cordon(42).await.expect("cordon");
        deck.admin()
            .drop_replicas(42, vec![1, 2])
            .await
            .expect("drop_replicas");
        tokio::time::sleep(Duration::from_millis(80)).await;

        let entries = deck.audit().collect();
        assert_eq!(entries.len(), 2);
        for entry in &entries {
            assert!(matches!(
                entry.outcome,
                crate::adapter::net::behavior::meshos::VerificationOutcome::Unverified
            ));
            assert!(entry.operator_ids.is_empty());
        }
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn audit_query_between_filters_outside_window() {
        use crate::adapter::net::behavior::meshos::{IceActionProposal, MeshOsEvent};
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());

        runtime
            .handle()
            .publish(MeshOsEvent::SignedIceCommit {
                proposal: IceActionProposal::ThawCluster,
                signatures: Vec::new(),
                issued_at_ms: super::super::meshos::now_ms_since_unix_epoch(),
                blast_hash: TEST_BLAST_HASH,
            })
            .await
            .unwrap();
        tokio::time::sleep(Duration::from_millis(80)).await;

        // A window entirely in the past — no entries should
        // match.
        let past_only = deck.audit().between(0, 1).collect();
        assert!(past_only.is_empty());

        // A window covering "now" — should match.
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;
        let around_now = deck
            .audit()
            .between(now_ms - 10_000, now_ms + 10_000)
            .collect();
        assert_eq!(around_now.len(), 1);
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_force_restart_daemon_proposal_round_trips() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let daemon = super::super::meshos::DaemonRef {
            id: 7,
            name: "telemetry".into(),
        };
        let proposal = deck.ice().force_restart_daemon(daemon.clone());
        let simulated = proposal.simulate().await.expect("simulate");
        assert_eq!(simulated.blast_radius().affected_daemons, vec![daemon]);
        let sig = deck.identity().sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let commit = simulated.commit(&[sig]).await.expect("commit");
        assert_eq!(commit.event_kind(), "force_restart_daemon");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_kill_migration_proposal_round_trips_and_audits() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let proposal = deck.ice().kill_migration(123);
        let simulated = proposal.simulate().await.expect("simulate");
        let sig = deck.identity().sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let commit = simulated.commit(&[sig]).await.expect("commit");
        assert_eq!(commit.event_kind(), "kill_migration");

        // Confirms the commit lands on the audit ring even
        // though the dispatcher integration is pending.
        tokio::time::sleep(Duration::from_millis(60)).await;
        let entries = deck.audit().force_only().collect();
        assert!(entries
            .iter()
            .any(|r| matches!(r.event, AdminEvent::KillMigration { migration: 123 })));
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_force_cutover_proposal_round_trips() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let proposal = deck.ice().force_cutover(100, 42);
        let simulated = proposal.simulate().await.expect("simulate");
        assert_eq!(simulated.blast_radius().affected_replicas, vec![100]);
        assert_eq!(simulated.blast_radius().affected_nodes, vec![42]);
        let sig = deck.identity().sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let commit = simulated.commit(&[sig]).await.expect("commit");
        assert_eq!(commit.event_kind(), "force_cutover");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_force_evict_replica_proposal_round_trips() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let deck = DeckClient::from_runtime(&runtime, OperatorIdentity::generate());
        let proposal = deck.ice().force_evict_replica(100, 7);
        let simulated = proposal.simulate().await.expect("simulate");
        assert_eq!(simulated.blast_radius().affected_replicas, vec![100]);
        assert_eq!(simulated.blast_radius().affected_nodes, vec![7]);
        let sig = deck.identity().sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let commit = simulated.commit(&[sig]).await.expect("commit");
        assert_eq!(commit.event_kind(), "force_evict_replica");
        let _ = runtime.shutdown().await;
    }

    #[tokio::test]
    async fn ice_commit_with_registry_accepts_a_valid_multi_op_bundle() {
        let dispatcher = Arc::new(LoggingDispatcher::new());
        let runtime = MeshOsRuntime::start(fast_config(), dispatcher);
        let op_a = OperatorIdentity::generate();
        let op_b = OperatorIdentity::generate();
        let mut registry = OperatorRegistry::new();
        registry.register(op_a.keypair());
        registry.register(op_b.keypair());
        let deck = DeckClient::new(
            runtime.handle_clone(),
            runtime.snapshot_reader().clone(),
            op_a.clone(),
            DeckClientConfig {
                snapshot_poll_interval: Duration::from_millis(100),
                ice_signature_threshold: 2,
            },
        )
        .with_operator_registry(registry);

        let proposal = deck.ice().freeze_cluster(Duration::from_secs(15));
        let simulated = proposal.simulate().await.expect("simulate");
        let sig_a = op_a.sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let sig_b = op_b.sign_proposal(
            simulated.action(),
            simulated.issued_at_ms(),
            &simulated.blast_hash(),
        );
        let commit = simulated
            .commit(&[sig_a, sig_b])
            .await
            .expect("valid multi-op bundle should commit");
        assert_eq!(commit.event_kind(), "freeze_cluster");
        let _ = runtime.shutdown().await;
    }
}