geiserx_ts_runtime 0.47.10

tailscale runtime
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
use core::{
    net::{Ipv4Addr, Ipv6Addr},
    time::Duration,
};
use std::{collections::HashMap, sync::Arc, time::Instant};

use futures::StreamExt;
use kameo::{
    actor::{ActorRef, Spawn},
    message::{Context, StreamMessage},
    prelude::Message,
};
use tokio::sync::watch;
use ts_control::{
    AsyncControlClient, Endpoint, EndpointType, Error as ControlError, IdTokenError, LogoutError,
    Node, SetDnsError, SshPolicy, StateUpdate, TkaStatus, TkaSyncError, tka_disable,
    tka_init_begin, tka_init_finish, tka_submit_signature,
};
use ts_magicsock::SelfEndpointType;

use crate::{
    derp_latency::{DerpLatencyMeasurement, DerpLatencyMeasurer},
    direct::EndpointAdvertisement,
};

/// Actor responsible for maintaining the connection to control.
///
/// This actor is responsible for proxying the map response stream onto the message bus.
pub struct ControlRunner {
    client: AsyncControlClient,
    params: Params,

    self_node: watch::Sender<Option<Node>>,
    /// Latest Tailscale SSH policy pushed by control, or `None` until control sends one. The SSH
    /// server reads this to authorize incoming connections; absent policy means deny-all.
    ssh_policy: watch::Sender<Option<SshPolicy>>,
    /// Latest Tailnet Lock status pushed by control, or `None` until control sends one.
    tka: watch::Sender<Option<TkaStatus>>,
    /// The locally-synced Tailnet-Lock state (verified `Authority` + AUM store), or `None` until a
    /// successful bootstrap+sync. Held here because `ControlRunner` owns the netmap stream that
    /// triggers resync. Mutated only on the actor thread (the netmap handler spawns the sync RPC and
    /// the result returns via the [`TkaSynced`] self-message).
    tka_synced: Option<crate::tka_sync::SyncedTka>,
    /// The Tailnet-Lock chain persisted beside the cached netmap for the cold-start replay, and the
    /// single ordered path that writes or removes it. See [`PersistedTkaChain`].
    tka_chain: PersistedTkaChain,
    /// The verified TKA [`Authority`](ts_tka::Authority) the peer tracker **enforces** (Go
    /// `tkaFilterNetmapLocked`). `None` until the first successful sync, and reset to `None` when the
    /// lock is disabled. This is the SOLE delivery channel to the peer tracker (which holds the
    /// matching `Receiver` and reads it on every peer upsert): a `watch` cell, not a bus message, so
    /// the latest value is always readable, never dropped under load, and writes are strictly ordered
    /// by this actor — a disable (`None`) can never be reordered behind or dropped before a stale
    /// `Some`. Written only from [`apply_tka_synced`] (enable) and [`maybe_sync_tka`] (disable), both
    /// on the actor thread. The published `Authority` has always passed `VerifiedAumChain::verify`.
    tka_authority: watch::Sender<Option<Arc<ts_tka::Authority>>>,
    /// In-flight guard: `true` while a sync RPC task is running, so a burst of netmap updates does
    /// not spawn overlapping syncs (Go serializes sync under `b.mu`).
    tka_syncing: bool,
    /// Monotonic generation stamped when a disable (or a fresh sync) supersedes any in-flight sync.
    /// `maybe_sync_tka` bumps this on a disable transition and captures it into each spawned sync;
    /// [`apply_tka_synced`] discards a sync result whose captured generation is stale, so a lock
    /// disabled *while a sync was in flight* is never re-enabled by that sync's late `Ok(Some)`
    /// (the in-flight window the `tka_synced.is_some()` disable guard alone does not cover).
    tka_generation: u64,
    /// Latest cert-domain list from control's netmap DNS config (Go `nm.DNS.CertDomains`), or empty
    /// until control sends a DNS config carrying one. The facade reads this for `Device::cert_domains`.
    cert_domains: watch::Sender<Vec<String>>,
    /// Latest full DNS config from control's netmap (Go `netmap.NetworkMap.DNS`), or `None` until
    /// control sends one. The facade reads this for `Device::dns_config` (the daemon's
    /// `tnet dns status`). A superset of [`cert_domains`](Self::cert_domains), which is kept as its
    /// own cell for the narrower TLS-cert use.
    dns_config: watch::Sender<Option<ts_control::DnsConfig>>,
    /// Latest interactive-login / consent URL control asked this node to open
    /// (`MapResponse.PopBrowserURL`), or `None` until control sends one. The facade reads this for
    /// `Device::pop_browser_url` (a daemon driving a non-authkey login surfaces it to the user), and
    /// [`Runtime::watch_ipn_bus`](crate::Runtime::watch_ipn_bus) subscribes to it for the bus's
    /// `browse_to_url` running-node events.
    ///
    /// **Sticky, not per-update** (Go `controlclient` `sess.lastPopBrowserURL`): control sends
    /// `MapResponse.PopBrowserURL` empty on nearly every netmap tick, so this cell is updated ONLY on
    /// a non-empty URL that differs from its current value (`sticky_update_pop_browser_url`, via
    /// `send_if_modified` — the cell's own value is the "last URL seen", so no separate mirror is
    /// needed). It is never reset to `None` by an empty update — matching Go's `direct.go` guard
    /// `u != "" && u != sess.lastPopBrowserURL`. Updating on every tick would thrash the cell to
    /// `None` and coalesce the URL away for a `watch` subscriber.
    pop_browser_url: watch::Sender<Option<url::Url>>,
    /// Latest network-conditions report (preferred DERP region + per-region latencies), updated each
    /// time the DERP-latency measurer reports in. The facade reads this for `Device::netcheck` (the
    /// daemon's `tnet netcheck`). Empty until the first measurement.
    netcheck: watch::Sender<crate::status::NetcheckReport>,
    /// The DERP home region currently selected, with the latency measured for it at selection time.
    /// `None` until the first home region is chosen. Used to apply selection **hysteresis** (Go
    /// `netcheck.addReportHistoryAndSetPreferredDERP`): the home region is only switched when a new
    /// region is *meaningfully* lower-latency than the current one, so jitter between near-equal
    /// regions does not flap the home relay (which would cause repeated reconnects + brief loss).
    home_region: Option<(ts_derp::RegionId, core::time::Duration)>,
    /// Rolling history of per-cycle DERP-latency reports within the last [`DERP_HISTORY_MAX_AGE`]
    /// (Go `netcheck` `maxAge = 5 * time.Minute`), each stamped with its arrival `Instant`. Feeds the
    /// `bestRecent` smoothing (Go `addReportHistoryAndSetPreferredDERP`): the new home candidate is
    /// chosen by each region's **minimum** latency over this window, not its raw current sample, so a
    /// best region whose latency oscillates across the switch boundary does not flap the home relay.
    /// Aged entries are evicted on each measurement; the buffer is therefore bounded by the netcheck
    /// cadence × the window.
    derp_report_history: Vec<(Instant, Arc<Vec<ts_netcheck::RegionResult>>)>,
    /// Consecutive automatic-reauth attempts that have NOT yet recovered the node to a good (non-
    /// expired) self-node. The circuit breaker for [`expiry_action`]'s `Reauthenticate` path: a
    /// one-shot / already-consumed auth key cannot re-register, so without a bound an expired node
    /// would sit in [`DeviceState::Reauthenticating`] indefinitely (the rotated re-register keeps
    /// failing or control keeps returning a still-expired self-node).
    ///
    /// Incremented once per expired self-node seen while *already* `Reauthenticating` (each such
    /// arrival is evidence the prior reauth did not recover); reset to `0` whenever a good
    /// (non-expired) self-node arrives (the node recovered) — see the [`StreamMessage::Next`] handler.
    /// At [`MAX_REAUTH_ATTEMPTS`] the runner stops re-arming reauth and flips the cell to the terminal
    /// [`DeviceState::Expired`], giving the cell a stable terminal state (a genuinely good self-node
    /// can still recover it later). The one-shot `Command::Reauth` is fired ONLY on the transition
    /// INTO `Reauthenticating`, never re-fired while already reauthenticating, so the node key is
    /// rotated at most once per episode (a second rotation would lose the original `OldNodeKey`
    /// anchor control needs to link the rotation).
    reauth_attempts: u32,
    /// Background task that bridges the control client's mid-session re-auth URL cell onto
    /// [`Self::params`]'s device-state cell (sets [`DeviceState::NeedsLogin`] when control returns
    /// `MachineNotAuthorized` on a live re-register — see [`bridge_reauth_url_to_state`]). Aborted on
    /// [`Drop`] so it cannot outlive the actor (the [`DataplaneActor`](crate::dataplane) pattern).
    reauth_bridge: tokio::task::JoinHandle<()>,
}

/// The number of consecutive failed automatic-reauth attempts after which the runner gives up
/// re-arming reauth and flips the device to the terminal [`DeviceState::Expired`] (the circuit
/// breaker from the design's deferred-question Q1). A one-shot auth key was consumed by the first
/// registration, so an auto re-register with it cannot succeed; this bounds that case to today's
/// terminal behavior instead of an indefinite `Reauthenticating` spell. The first arrival fires the
/// reauth; each subsequent expired self-node while still reauthenticating counts toward this cap.
const MAX_REAUTH_ATTEMPTS: u32 = 3;

impl Drop for ControlRunner {
    fn drop(&mut self) {
        // Stop the re-auth bridge so it does not outlive the actor (mirrors `DataplaneActor`).
        self.reauth_bridge.abort();
    }
}

/// Control runner args.
pub struct Params {
    /// Control config.
    pub(crate) config: ts_control::Config,

    /// Auth key (if needed).
    pub(crate) auth_key: Option<String>,

    /// The [`crate::Env`] for this actor.
    pub(crate) env: crate::Env,

    /// Sender for the device connection-state cell. Created in [`Runtime::spawn`](crate::Runtime)
    /// so it outlives the actor's `on_start` (which may publish [`DeviceState::Failed`] and then
    /// return `Err`, before `Self` exists). The runtime keeps the matching `Receiver` for
    /// [`watch_state`](crate::Runtime::watch_state) / [`wait_until_running`](crate::Runtime::wait_until_running).
    pub(crate) state_tx: watch::Sender<crate::DeviceState>,

    /// Sender for the TKA enforcement-authority cell the peer tracker reads (Go
    /// `tkaFilterNetmapLocked`). Created in [`Runtime::spawn`](crate::Runtime) and threaded into BOTH
    /// the peer tracker (the `Receiver`) and this runner (the `Sender`), so the runner is the sole
    /// writer and the tracker reads the latest verified `Authority` on demand. `None` = no lock /
    /// disabled (admit all).
    pub(crate) tka_authority: watch::Sender<Option<Arc<ts_tka::Authority>>>,

    /// Sender for the selected DERP home region (the **smoothed** `bestRecent` + hysteresis choice,
    /// Go `report.PreferredDERP`). Created in [`Runtime::spawn`](crate::Runtime); the runner is the
    /// sole writer and [`Multiderp`](crate::multiderp) holds the `Receiver`, so the local DERP relay
    /// follows the SAME home the runner advertises to control — not the raw per-cycle latency
    /// minimum (which would flap on jitter and disagree with the advertised home). `None` until the
    /// first home is chosen.
    pub(crate) home_region: watch::Sender<Option<ts_derp::RegionId>>,
}

#[doc(hidden)]
#[derive(Debug, thiserror::Error)]
pub enum ControlRunnerError {
    #[error(transparent)]
    Control(#[from] ControlError),

    #[error(transparent)]
    Crate(#[from] crate::Error),
}

impl kameo::Actor for ControlRunner {
    type Args = Params;
    type Error = ControlRunnerError;

    async fn on_start(params: Params, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
        // Cold start: replay the netmap this node cached the last time control granted it
        // `cache-network-maps` (Go `nodecap.CacheNetworkMaps`, capability version 135 — upstream
        // does the same from `ipnlocal.Start`, feeding the cached map through
        // `setControlClientStatusLocked` before it builds its control client). Publishing it here,
        // ahead of registration, is the whole point: the peer tracker, dataplane and packet filter
        // get the last-known peers, DERP map and rules while this node is still doing its
        // register/poll round trips, so peer connectivity can start coming up before control has
        // said a word. Control's own first netmap lands on the same bus moments later and replaces
        // it, and a netmap that no longer carries the attribute deletes the cache (see
        // `NetmapCache::observe`).
        //
        // Nothing here can fail the start-up: no cache configured, nothing cached, or an
        // undecodable cache all mean "carry on without one".
        replay_cached_netmap(&params).await;

        // The interactive AuthURL, captured on the first unauthorized reply and reused as the
        // `followup` on every subsequent poll so control long-polls ONE stable URL (rather than
        // minting a fresh, racing URL each retry) until the user visits it.
        let mut login_url: Option<url::Url> = None;
        loop {
            match AsyncControlClient::check_auth(
                &params.config,
                &params.env.keys,
                params.auth_key.as_deref(),
                login_url.as_ref(),
            )
            .await
            {
                Ok(()) => break,
                Err(ControlError::MachineNotAuthorized(u)) => {
                    // Capture the FIRST url and keep showing/following-up on it, so the link the
                    // user opened stays valid instead of being superseded by the next poll.
                    let url = login_url.get_or_insert(u).clone();
                    tracing::info!(auth_url = %url, "please authorize this machine or pass an auth key");
                    // Publish `NeedsLogin(url)` only when it actually changes the cell. With `followup`
                    // set, the SAME URL is re-affirmed on every long-poll timeout; a bare `send_replace`
                    // re-notifies `state_rx` each cycle, so the bus re-emits `browse_to_url` for a link
                    // the user already has — reopening it repeatedly. `send_if_modified` dedups the
                    // no-op, mirroring `bridge_reauth_url_to_state` (the mid-session re-auth path).
                    let next = crate::DeviceState::NeedsLogin(url);
                    params.state_tx.send_if_modified(|current| {
                        if *current == next {
                            false
                        } else {
                            *current = next.clone();
                            true
                        }
                    });
                    // With followup set, check_auth long-polls until the URL is visited; this short
                    // sleep only applies if control times the poll out, and we re-followup the SAME url.
                    tokio::time::sleep(Duration::from_secs(2)).await;
                }
                Err(ControlError::NeedsMachineAuth) => {
                    // The node is registered with a valid key but awaiting ADMIN APPROVAL on an
                    // approval-gated tailnet, and control offered NO interactive URL. This is
                    // TRANSIENT (Go's `NeedsMachineAuth`): poll registration until an admin approves,
                    // then `check_auth` returns `Ok(())` → the loop breaks and the node comes up with
                    // no re-registration. Publishing the (no-URL) `NeedsMachineAuth` state — NOT a
                    // terminal `Failed` and NOT `NeedsLogin` (there is no URL to open) — lets a
                    // watcher / `wait_until_running` see "awaiting approval" instead of an opaque
                    // timeout. Same 5s poll cadence as the `MachineNotAuthorized(url)` arm.
                    tracing::info!(
                        "machine awaiting admin approval to join the tailnet; polling until approved"
                    );
                    params
                        .state_tx
                        .send_replace(crate::DeviceState::NeedsMachineAuth);
                    tokio::time::sleep(Duration::from_secs(5)).await;
                }
                Err(ControlError::RateLimited(retry_after)) => {
                    // Control asked us to slow down (HTTP 429). Wait exactly the server-requested
                    // cooldown and retry — this is transient, NOT a terminal `Failed`, so we must
                    // not stop the runner (mirrors Go's `authRoutine` sleeping `rle.retryAfter`).
                    tracing::warn!(
                        ?retry_after,
                        "control rate-limited registration; waiting before retry"
                    );
                    tokio::time::sleep(retry_after).await;
                }
                Err(e) => {
                    // Followup auth path gone: while long-polling a STABLE AuthURL, control returns
                    // an HTTP registration error (410 "auth path not found") once the path is either
                    // VISITED + approved (consumed) or expired. This is NOT terminal — drop the
                    // followup and re-register ONCE: an approved node key comes back `MachineAuthorized`
                    // (`Ok(())` → login completes), an expired one comes back with a fresh AuthURL.
                    // Without this, the user's own approval (which consumes the path → 410) kills the
                    // runner. Bounded: on re-register `login_url` is None, so a repeat HTTP error falls
                    // through to the terminal handling below (no infinite loop).
                    if login_url.is_some()
                        && matches!(
                            e,
                            ControlError::Internal(
                                ts_control::InternalErrorKind::Http,
                                ts_control::Operation::Registration,
                            )
                        )
                    {
                        tracing::info!(
                            "followup auth path gone (approved or expired); re-registering"
                        );
                        login_url = None;
                        tokio::time::sleep(Duration::from_secs(1)).await;
                        continue;
                    }
                    // A hard registration failure (bad/expired/unknown auth key, etc.). Log the
                    // specific reason control gave AND publish it as a typed `Failed` state so
                    // `Device::wait_until_running` returns the actionable reason (tsr-kqj) instead
                    // of the opaque `Internal(Actor)` the caller would otherwise see once the
                    // stopped actor is next asked. Publishing before `return Err` is why the state
                    // sender lives on `Runtime`, not on `Self` (which never gets constructed here).
                    let reason = crate::RegistrationError::from(&e);
                    tracing::error!(error = %e, "registration failed; control runner stopping");
                    params
                        .state_tx
                        .send_replace(crate::DeviceState::Failed(reason));
                    return Err(e.into());
                }
            }
        }
        // check_auth succeeded, but the node is not "up" until the netmap stream is actually
        // attached below. Publish `Running` only AFTER `attach_stream` so `wait_until_running` never
        // resolves `Ok` for a device whose stream connect failed (which would leave a stopped actor
        // behind). If the connect/subscribe steps fail, publish a transient `Failed` first so the
        // waiter sees an actionable reason instead of the opaque post-mortem `Internal(Actor)`.
        // The control client's live map-poll loop publishes a mid-session re-auth URL here (set when
        // a re-register returns `MachineNotAuthorized` because the node key expired/was revoked). The
        // runtime owns the receiver; `connect` takes the sender. Created before `connect` so the
        // sender is in place for the very first poll, and so the receiver outlives `bring_up`.
        let (auth_url_tx, auth_url_rx) = watch::channel::<Option<url::Url>>(None);

        // `connect` issues its own `machine/register` POST (a second one after `check_auth`'s), so
        // it too can hit a 429. Wrap it in the same honor-`Retry-After` retry as the `check_auth`
        // loop above: a rate-limit is transient — sleeping the server-requested cooldown and
        // retrying must NOT stop the runtime (a 429 here previously fell into the `Err(e)` arm →
        // `Failed` → actor stop). `connect` consumes a `watch::Sender` by value (and drops it on a
        // failed register, before it would be moved into the live-poll task), so we keep the original
        // `auth_url_tx` alive here across all attempts and hand `connect` a CLONE each time. That
        // keeps the runtime's `auth_url_rx` (the `reauth_bridge` receiver) paired with a live sender:
        // recreating a fresh sender per attempt instead would orphan the bridge the moment the
        // original dropped, silently killing mid-session re-auth-URL delivery.
        let client = loop {
            let bring_up = async {
                let (client, stream) = AsyncControlClient::connect(
                    &params.config,
                    &params.env.keys,
                    params.auth_key.as_deref(),
                    auth_url_tx.clone(),
                )
                .await?;

                DerpLatencyMeasurer::spawn_link(&slf, params.env.clone()).await;

                params.env.subscribe::<DerpLatencyMeasurement>(&slf).await?;
                params.env.subscribe::<EndpointAdvertisement>(&slf).await?;
                slf.attach_stream(stream.boxed(), (), ());
                Ok::<_, ControlRunnerError>(client)
            };

            match bring_up.await {
                Ok(client) => break client,
                Err(ControlRunnerError::Control(ControlError::RateLimited(retry_after))) => {
                    tracing::warn!(
                        ?retry_after,
                        "control rate-limited the session bring-up; waiting before retry"
                    );
                    tokio::time::sleep(retry_after).await;
                }
                Err(ControlRunnerError::Control(ControlError::NeedsMachineAuth)) => {
                    // `connect` issues its OWN `machine/register` POST (a second one after
                    // `check_auth`'s), so it too can come back "awaiting admin approval, no URL". In
                    // the normal flow the `check_auth` loop above already gated this (it only breaks
                    // once registration returns `Ok`, and approval is monotonic), so this arm is the
                    // defensive twin for a node de-authorized in the race window between the two POSTs:
                    // treat it as TRANSIENT exactly like the `check_auth` arm — publish the (no-URL)
                    // `NeedsMachineAuth` state, poll the same 5s, and retry the bring-up — rather than
                    // collapsing into the terminal `Failed` arm below (which would permanently stop
                    // the runner on a recoverable await-approval). Mirrors Go's `NeedsMachineAuth`.
                    tracing::info!(
                        "machine awaiting admin approval during session bring-up; polling until \
                         approved"
                    );
                    params
                        .state_tx
                        .send_replace(crate::DeviceState::NeedsMachineAuth);
                    tokio::time::sleep(Duration::from_secs(5)).await;
                }
                Err(e) => {
                    tracing::error!(error = %e, "bringing up the control session failed");
                    // The control session never came up; surface it as a transient registration
                    // failure (a retry / fresh `Device::new` may succeed) rather than leaving the
                    // state stuck at `Connecting`.
                    params.state_tx.send_replace(crate::DeviceState::Failed(
                        crate::RegistrationError::NetworkUnreachable,
                    ));
                    return Err(e);
                }
            }
        };

        // The netmap stream is attached: the node is up. The stream `Next` handler keeps this
        // current (and flips to `Expired` if the self-node's key lapses).
        params.state_tx.send_replace(crate::DeviceState::Running);

        // Bridge the control client's mid-session re-auth URL cell onto the device-state cell: a
        // `Some(url)` (control returned `MachineNotAuthorized` on a live re-register) becomes
        // `DeviceState::NeedsLogin(url)` so the IPN bus surfaces `browse_to_url` and the embedder can
        // prompt the user — the live-session analogue of the initial `check_auth` loop above. The
        // recovery to `Running` is the netmap self-node handler's job (next good self-node), so this
        // bridge only forwards `Some`. The task ends when the sender drops (the client's `run` task
        // ended) and is aborted on actor `Drop`, so it cannot leak past the actor.
        let reauth_bridge = {
            let state_tx = params.state_tx.clone();
            let mut auth_url_rx = auth_url_rx;
            tokio::spawn(async move {
                while auth_url_rx.changed().await.is_ok() {
                    let url = auth_url_rx.borrow_and_update().clone();
                    bridge_reauth_url_to_state(&state_tx, url.as_ref());
                }
            })
        };

        // Clone the TKA authority publisher before `params` moves into `Self` below. The matching
        // `Receiver` lives on the peer tracker; this sender is the sole writer (enforce on sync,
        // clear on disable).
        let tka_authority = params.tka_authority.clone();

        // Same reason: the owner of the persisted Tailnet-Lock chain is built from the config before
        // `params` moves. It starts out assuming a chain may already be on disk — an earlier process
        // of this node can have left one there, and this one has synced nothing yet.
        let tka_chain = PersistedTkaChain::new(params.config.netmap_cache_dir.as_ref());

        Ok(Self {
            client,
            params,
            self_node: Default::default(),
            ssh_policy: Default::default(),
            tka: Default::default(),
            tka_synced: None,
            tka_chain,
            tka_authority,
            tka_syncing: false,
            tka_generation: 0,
            cert_domains: Default::default(),
            dns_config: Default::default(),
            pop_browser_url: Default::default(),
            netcheck: Default::default(),
            home_region: None,
            derp_report_history: Vec::new(),
            reauth_attempts: 0,
            reauth_bridge,
        })
    }
}

impl ControlRunner {
    /// Decide whether the latest netmap's Tailnet-Lock status warrants a (re)sync and, if so, spawn
    /// the bootstrap+sync RPC off the actor thread (so the netmap stream never blocks on a control
    /// round-trip). The result returns via the [`TkaSynced`] self-message.
    ///
    /// Triggers when control reports TKA enabled (`is_enabled`) AND we are not already syncing AND
    /// either we hold no `Authority` yet (→ bootstrap) or control's head differs from ours (→ catch
    /// up). When TKA is disabled, clears any synced state (the lock was turned off). Mirrors Go's
    /// `tkaSyncIfNeeded`: a no-op when our head already matches.
    async fn maybe_sync_tka(&mut self, tka: &TkaStatus, self_ref: ActorRef<Self>) {
        if !tka.is_enabled() {
            // Lock disabled (or never enabled): clear enforcement by writing `None` to the authority
            // cell the peer tracker reads — synchronously, so it can never be reordered behind or
            // dropped before a stale `Some` (the failure a best-effort broadcast had). Always bump the
            // generation so ANY sync currently in flight is invalidated: without this, a disable that
            // races an in-flight sync (whose `take()` already cleared `tka_synced`) would be a no-op
            // here, and the sync's late `Ok(Some)` would silently re-enable a lock control just turned
            // off (the in-flight window the `tka_synced.is_some()` guard alone misses). Cheap and
            // idempotent: clearing an already-`None` cell and bumping the generation are harmless.
            self.tka_generation = self.tka_generation.wrapping_add(1);
            if self.tka_synced.is_some() {
                tracing::info!("TKA lock disabled; clearing enforcement (admitting all peers)");
                self.tka_synced = None;
            }
            // Outside that guard, deliberately: the chain on disk is not this process's to have
            // synced. A cold start holds `tka_synced == None` while an earlier process's chain is
            // still in the cache directory, and that chain is exactly the one the *next* cold start
            // would find. So a lock control reports off clears it whether or not we ever enforced it.
            // Dropping it is not what keeps the next cold start honest (the replay only vouches when
            // the cached frame itself says the lock was on, and only with a chain at that frame's
            // head) — it is so a disabled lock leaves nothing of itself on disk.
            self.tka_chain.discard().await;
            self.tka_authority.send_replace(None);
            return;
        }
        if self.tka_syncing {
            return; // a sync is already in flight; the next netmap will re-trigger if still stale
        }
        // Up-to-date check: if we already have an Authority whose head matches control's, nothing to
        // do. A malformed control head is treated as "different" (we'll attempt a sync, which
        // fail-closes harmlessly).
        if let Some(synced) = &self.tka_synced
            && let Some(control_head) = ts_tka::AumHash::from_base32(&tka.head)
            && synced.authority.head_matches(&control_head)
        {
            return;
        }

        // Spawn the sync. Move the current synced state out (the driver takes it by value and returns
        // the advanced state); `tka_synced` stays `None` until the result lands, guarded by
        // `tka_syncing` so we don't spawn a second concurrent sync. Capture the current generation so
        // `apply_tka_synced` can discard this result if a disable bumped the generation while the sync
        // was in flight (H1: don't re-enable a lock that was disabled mid-sync).
        self.tka_syncing = true;
        let generation = self.tka_generation;
        let current = self.tka_synced.take();
        let config = self.params.config.clone();
        let keys = self.params.env.keys.clone();
        tokio::spawn(async move {
            let result = crate::tka_sync::sync_tka(&config, &keys, current).await;
            // Hand the outcome back to the actor thread to apply (mutating actor state off-thread is
            // not allowed). A send failure just means the actor is gone — nothing to do.
            if let Err(e) = self_ref.tell(TkaSynced { result, generation }).await {
                tracing::debug!(error = ?e, "TKA sync result not delivered (actor gone)");
            }
        });
    }

    /// Apply the outcome of a spawned [`maybe_sync_tka`] task on the actor thread: store the advanced
    /// state + publish the `Authority` to the peer tracker's enforcement cell (or, on inert/failed
    /// sync, leave peers unaffected). Always clears the in-flight guard.
    ///
    /// `generation` is the value captured when the sync was spawned. If it no longer matches
    /// `self.tka_generation`, the lock was disabled (or re-synced) while this sync was in flight, so
    /// the result is discarded — never re-enabling an authority control has since turned off.
    async fn apply_tka_synced(
        &mut self,
        result: Result<Option<crate::tka_sync::SyncedTka>, crate::tka_sync::TkaSyncDriverError>,
        generation: u64,
    ) {
        self.tka_syncing = false;

        // H1 guard: a disable (or a superseding sync) bumped the generation while this sync ran. Drop
        // the stale result — `maybe_sync_tka`'s disable branch already cleared enforcement to `None`,
        // and re-applying this `Some` would re-enforce a lock that is no longer active.
        if generation != self.tka_generation {
            tracing::info!(
                "TKA sync result superseded (lock disabled or re-synced mid-flight); discarding"
            );
            return;
        }

        match result {
            Ok(Some(synced)) => {
                tracing::info!(
                    head = %synced.authority.head().to_base32(),
                    "TKA sync succeeded; enforcing verified Authority (Go tkaFilterNetmapLocked)"
                );
                // Deliver the verified Authority to the peer tracker's enforcement cell. The tracker
                // reads it on every peer upsert and drops unauthorized peers. `Some(..)` = enforce; a
                // `None` is written on disable. `watch` is the sole channel (last-write-wins, never
                // dropped, ordered by this actor) — no bus, no re-publish-for-replay needed.
                self.tka_authority
                    .send_replace(Some(synced.authority.clone()));

                // Observability (Go `tkaFilterNetmapLocked`'s self check → `LockedOut` health
                // warning): verify SELF's own node-key signature against the freshly-synced
                // Authority and warn if self is NOT authorized. We never FILTER self (self never
                // enters the peer db, so enforcement can't lock us out of our own netmap), but Go
                // raises an operator-facing warning here because a self that the lock does not
                // authorize means this node's key-signature is missing/invalid for the current lock
                // — it will be unable to prove itself to locked peers. This fork has no health
                // subsystem, so the signal is a `tracing::warn!` (its observability channel).
                //
                // `self_node` is a sticky cell set on every netmap carrying a self-node; if a sync
                // somehow lands before the first self-node ever arrived it is `None`, so we skip the
                // advisory this cycle and re-evaluate on the next sync — fine for observability-only.
                // The `borrow()` ref is scoped to this `if let` and dropped before the `&mut self`
                // write below.
                if let Some(self_node) = self.self_node.borrow().as_ref() {
                    log_self_lockout(self_node, &synced.authority);
                }

                // Persist the verified chain beside the cached netmap so the *next* cold start can
                // run this same filter over the cached peers before control answers — Go's cold
                // start filters its cached map against the authority it re-opens from disk, and this
                // is the disk it re-opens (see `replay_cached_netmap`).
                self.persist_tka_chain(&synced).await;

                self.tka_synced = Some(synced);
            }
            Ok(None) => {
                // Control has no lock for us (no genesis / disabled). Clear any authority we were
                // previously enforcing — symmetric with the disable path — so a transition to
                // "no lock" stops dropping peers. Not an error.
                if self.tka_synced.is_some() {
                    tracing::info!("TKA sync: control reports no lock; clearing enforcement");
                    self.tka_synced = None;
                }
                // Outside that guard for the same reason as the disable path: a chain an earlier
                // process persisted outlives the synced state this one holds.
                self.tka_chain.discard().await;
                self.tka_authority.send_replace(None);
            }
            Err(e) => {
                // Transport or verify failure: log and leave the prior authority in place (a failed
                // sync must not drop enforcement — that would fail OPEN). NEVER errors the netmap.
                // The next netmap update re-triggers a sync attempt.
                tracing::warn!(error = %e, "TKA sync failed; keeping prior enforcement state");
            }
        }
    }

    /// Persist the freshly-synced AUM chain next to the cached netmap, so a cold start can vouch for
    /// the peers in that netmap ([`load_cached_netmap`]).
    ///
    /// Written only where the netmap itself is written: with no
    /// [`netmap_cache_dir`](ts_control::Config::netmap_cache_dir) there is nothing to vouch for, and
    /// with the `cache-network-maps` grant absent or overridden nothing is cached either, so the
    /// chain is removed instead of written — the same withdrawal rule `NetmapCache::observe` applies
    /// to the netmap. The grant is read from the last self node control sent, which is the same
    /// source `observe` reads it from: nothing is ever cached before one arrives, so "no self node
    /// yet" is also "nothing to vouch for". A write failure is logged inside the cache and changes
    /// nothing: the next cold start simply withholds the cached peers.
    async fn persist_tka_chain(&mut self, synced: &crate::tka_sync::SyncedTka) {
        // Scoped so the `watch` borrow is dropped before the awaits below (it is not `Send`).
        let caching_granted = {
            let self_node = self.self_node.borrow();
            self_node
                .as_ref()
                .is_some_and(|node| ts_control::netmap_caching_enabled(&node.cap_map))
        };
        if !caching_granted {
            self.tka_chain.discard().await;
            return;
        }

        match crate::tka_sync::encode_chain(&synced.store, synced.oldest) {
            Some(blob) => self.tka_chain.store(&blob).await,
            None => {
                // The store cannot be walked genesis→head, so there is no chain to persist. Drop any
                // older one rather than leaving a chain that no longer matches what we enforce.
                tracing::warn!("TKA: synced chain is not walkable; not persisting it");
                self.tka_chain.discard().await;
            }
        }
    }

    fn with_self_node<F, R>(&self, f: F) -> impl Future<Output = Option<R>> + use<F, R>
    where
        F: FnOnce(&Node) -> R,
    {
        let mut sub = self.self_node.subscribe();
        let mut shutdown = self.params.env.shutdown.clone();

        async move {
            tokio::select! {
                _ = shutdown.wait_for(|x| *x) => {
                    None
                },
                node = sub.wait_for(Option::is_some) => {
                    Some(f(node.ok()?.as_ref()?))
                },
            }
        }
    }
}

/// Apply Go's sticky `PopBrowserURL` semantics to the consent-URL `watch` cell.
///
/// Control sends `MapResponse.PopBrowserURL` empty on nearly every netmap update, so the cell is
/// updated ONLY when `incoming` is a non-empty URL that differs from the cell's current value —
/// Go's `direct.go` guard `u != "" && u != sess.lastPopBrowserURL`. The cell is **never reset to
/// `None`** by an empty/absent update — the running-node consent URL is sticky for the session.
/// Updating unconditionally would thrash the cell to `None` on every tick and coalesce the URL away
/// for a `watch`/bus subscriber.
///
/// The dedupe is in-place via [`watch::Sender::send_if_modified`] — the cell's own value is the
/// "last URL sent" (this sticky path is its only writer), so no separate mirror field is needed and
/// the watch is woken only on a genuine change (Go's `sess.lastPopBrowserURL` role, for free). This
/// matches the [`send_if_modified`](watch::Sender::send_if_modified) idiom already used for the
/// device-state cell in this handler.
///
/// Factored out of the netmap-update handler so the (easy-to-regress) sticky logic is unit-testable
/// against a plain `watch` channel without standing up the actor.
fn sticky_update_pop_browser_url(
    cell: &watch::Sender<Option<url::Url>>,
    incoming: Option<&url::Url>,
) {
    if let Some(url) = incoming {
        cell.send_if_modified(|current| {
            if current.as_ref() == Some(url) {
                false
            } else {
                *current = Some(url.clone());
                true
            }
        });
    }
}

/// Map a mid-session re-auth URL surfaced by the control client onto the device-state cell.
///
/// The control client's live map-poll loop publishes an `Option<url::Url>` into a `watch` cell when
/// a re-register hits `MachineNotAuthorized` (the node key expired/was revoked mid-session — see
/// [`ts_control::AsyncControlClient::connect`]'s `auth_url_tx`). `ts_control` cannot name
/// [`DeviceState`] (it must not depend on this crate), so this bridge fn does the translation:
/// a `Some(url)` sets [`DeviceState::NeedsLogin`]`(url)` so the IPN bus derives `browse_to_url` and
/// the embedder can prompt the user, exactly like the initial-registration `check_auth` path.
///
/// **Only `Some` drives a transition; `None` is ignored here.** The clear back to
/// [`DeviceState::Running`] is owned by the netmap self-node handler (the next good self-node flips
/// it — see the `StreamMessage::Next` arm), which is the authoritative "we are up again" signal; an
/// independent `None`-clear in this bridge could race that and is unnecessary. The
/// [`send_if_modified`](watch::Sender::send_if_modified) guard fires the watch only on a genuine
/// state change (it is a no-op when the cell already holds `NeedsLogin(url)` for the same URL), so a
/// re-auth URL re-surfaced across retries does not thrash the cell — mirroring the device-state
/// dedupe in the netmap handler.
///
/// Factored out so the (regress-prone) map-and-guard is unit-testable against a plain `watch`
/// channel without standing up the actor (mirrors [`sticky_update_pop_browser_url`]).
/// **Auto-reauth ownership.** While an automatic re-auth is in flight (the cell holds
/// [`DeviceState::Reauthenticating`]), this bridge must NOT downgrade it to `NeedsLogin`. An
/// auto-reauth's rotated re-register surfaces an auth URL through the SAME `auth_url_tx` cell this
/// bridge watches, but on a headless / auth-key node there is no human to visit it — flipping to
/// `NeedsLogin` would surface a misleading `browse_to_url` AND, by moving the cell off
/// `Reauthenticating`, let the next expired self-node re-fire reauth (a second node-key rotation that
/// loses the original `OldNodeKey` anchor). The auto-reauth path owns the cell until it recovers to
/// `Running` (the netmap self-node handler) or its circuit breaker trips it to `Expired`; this bridge
/// stands down for the duration.
pub(crate) fn bridge_reauth_url_to_state(
    state_tx: &watch::Sender<crate::DeviceState>,
    incoming: Option<&url::Url>,
) {
    if let Some(url) = incoming {
        let next = crate::DeviceState::NeedsLogin(url.clone());
        state_tx.send_if_modified(|current| {
            // Do not clobber an in-flight automatic re-auth (see the ownership note above): leave
            // `Reauthenticating` untouched so it can recover to `Running` or trip to `Expired` on its
            // own terms.
            if *current == crate::DeviceState::Reauthenticating || *current == next {
                false
            } else {
                *current = next.clone();
                true
            }
        });
    }
}

/// What to do when control delivers a self-node whose node-key expiry has passed — the decision
/// behind the [`StreamMessage::Next`] handler's expiry branch, factored into a pure function so the
/// full input matrix is unit-testable (mirrors [`bridge_reauth_url_to_state`] being pure).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ExpiryAction {
    /// The key is not expired — the node is up. (`→ DeviceState::Running`.)
    Running,
    /// The key expired and auto-reauth is permitted (auth key retained, reauth enabled, TKA NOT
    /// enforcing): rotate the node key + re-register (`→ DeviceState::Reauthenticating` +
    /// `Command::Reauth`).
    Reauthenticate,
    /// The key expired and auto-reauth is NOT permitted (no auth key, reauth disabled, or TKA
    /// enforcing): fall back to today's terminal behavior (`→ DeviceState::Expired`).
    Expired,
}

/// Decide the action for an expired-or-not self node (pure; the live handler at
/// [`StreamMessage::Next`] applies it). Go's `ipnlocal` runs `doLogin` (rotate the node key +
/// re-register with the stored auth key) when an auth-key node's key expires; this fork does the
/// same, gated by three safety conditions:
///
/// - `key_expired` — control reported the self-node's key expiry is in the past.
/// - `has_auth_key` — a usable auth key is retained for a non-interactive re-register (without one
///   there is nothing to re-register with → fall back to `Expired`, today's behavior).
/// - `reauth_enabled` — the `reauth_on_expiry` config opt-out is on (default true).
/// - `tka_active` — Tailnet Lock enforcement is currently active. **Hard safety gate:** a node-key
///   rotation on a locked tailnet would install an UNSIGNED key, locking the node out of locked
///   peers (the TKA re-sign is a separate follow-up — see `keystate.rs` `rotate_node_key`). So when
///   the lock is enforcing, never rotate — fall back to `Expired`.
///
/// Truth table: not expired → `Running`; expired AND auth-key AND reauth-enabled AND NOT TKA →
/// `Reauthenticate`; otherwise → `Expired`.
pub(crate) fn expiry_action(
    key_expired: bool,
    has_auth_key: bool,
    reauth_enabled: bool,
    tka_active: bool,
) -> ExpiryAction {
    if !key_expired {
        return ExpiryAction::Running;
    }
    if has_auth_key && reauth_enabled && !tka_active {
        ExpiryAction::Reauthenticate
    } else {
        ExpiryAction::Expired
    }
}

/// The outcome of one step of the bounded reauth sub-state-machine: the device state to publish, the
/// updated consecutive-attempt counter, and whether to fire the one-shot `Command::Reauth` this step.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ReauthStep {
    /// The device state to publish for this self-node.
    pub next: ReauthState,
    /// The consecutive-failed-reauth counter after this step (the caller stores it back).
    pub attempts: u32,
    /// Whether to fire the one-shot `Command::Reauth` (rotate + re-register) this step. Set ONLY on
    /// the transition INTO reauthenticating, so the node key rotates at most once per episode.
    pub fire_reauth: bool,
}

/// The device state a [`ReauthStep`] resolves to — the subset of [`DeviceState`](crate::DeviceState)
/// the circuit breaker can produce. Kept as its own enum so the breaker stays pure (no dependency on
/// the URL-carrying `DeviceState` variants) and the truth table is exhaustively testable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ReauthState {
    /// The node is up — `→ DeviceState::Running`.
    Running,
    /// An automatic re-auth is in flight — `→ DeviceState::Reauthenticating`.
    Reauthenticating,
    /// Terminal expiry — `→ DeviceState::Expired` (either the non-reauth path or the breaker tripped).
    Expired,
}

/// One step of the bounded reauth sub-state-machine (pure; the [`StreamMessage::Next`] handler stores
/// the result back into [`ControlRunner::reauth_attempts`] and publishes [`ReauthStep::next`]). This
/// is the circuit breaker for the design's deferred-question Q1: a one-shot / already-consumed auth
/// key cannot re-register, so the `Reauthenticate` path must settle rather than loop forever.
///
/// Inputs:
/// - `action` — the [`expiry_action`] verdict for this self-node.
/// - `already_reauthing` — whether the published state is currently
///   [`DeviceState::Reauthenticating`](crate::DeviceState::Reauthenticating) (the prior reauth has
///   not yet recovered the node).
/// - `attempts` — the current consecutive-failed-reauth counter.
///
/// Rules:
/// - `Running` → reset the counter to `0` (the node recovered) and report `Running`.
/// - `Reauthenticate` while NOT already reauthing → ENTER reauthenticating: counter `= 1`, fire the
///   one-shot reauth.
/// - `Reauthenticate` while ALREADY reauthing → the prior reauth did not recover; COUNT it (counter
///   `+= 1`) and do NOT re-fire (a second rotation would lose the original `OldNodeKey` anchor). At
///   [`MAX_REAUTH_ATTEMPTS`] the breaker trips to terminal `Expired`; below the cap, stay reauthing.
/// - `Expired` → terminal; the counter is irrelevant (this path never armed the reauth machine), so
///   it is reset to `0`.
pub(crate) fn reauth_circuit_step(
    action: ExpiryAction,
    already_reauthing: bool,
    attempts: u32,
) -> ReauthStep {
    match action {
        ExpiryAction::Running => ReauthStep {
            next: ReauthState::Running,
            attempts: 0,
            fire_reauth: false,
        },
        ExpiryAction::Reauthenticate if !already_reauthing => ReauthStep {
            next: ReauthState::Reauthenticating,
            attempts: 1,
            fire_reauth: true,
        },
        ExpiryAction::Reauthenticate => {
            let attempts = attempts.saturating_add(1);
            if attempts >= MAX_REAUTH_ATTEMPTS {
                ReauthStep {
                    next: ReauthState::Expired,
                    attempts,
                    fire_reauth: false,
                }
            } else {
                ReauthStep {
                    next: ReauthState::Reauthenticating,
                    attempts,
                    fire_reauth: false,
                }
            }
        }
        ExpiryAction::Expired => ReauthStep {
            next: ReauthState::Expired,
            attempts: 0,
            fire_reauth: false,
        },
    }
}

/// The classification of SELF against the active network lock — the observability analog of Go
/// `tkaFilterNetmapLocked`'s self check (which raises a `LockedOut` health warning).
#[derive(Debug, Clone, PartialEq, Eq)]
enum SelfLockVerdict {
    /// Self carries no key-signature at all (empty). The common "not signed yet" case: the node
    /// simply has not been signed for this lock — not locked out, just unsigned.
    Unsigned,
    /// Self's key-signature is authorized by the active lock; nothing to warn about.
    Authorized,
    /// Self has a key-signature but the lock does NOT authorize it (the message is the verify
    /// error). The operator-facing `LockedOut` condition: locked peers will reject this node.
    LockedOut(String),
}

/// Classify a node key + its key-signature against `authority` (pure: verify-and-classify, no
/// logging, no I/O). Takes only the two fields it needs — not the whole `Node` — so the decision is
/// unit-testable without constructing a full `Node` or standing up the actor.
fn self_lock_verdict(
    node_key: &ts_keys::NodePublicKey,
    key_signature: &[u8],
    authority: &ts_tka::Authority,
) -> SelfLockVerdict {
    // Mirror the peer path (`peer_tracker` `tka_snapshot_admits`): treat an empty signature as
    // "unsigned" rather than the `LockedOut` bucket Go's `NodeKeyAuthorized` would put a nil sig in
    // (it errors at decode). This is a deliberate, narrow divergence from a literal Go port: it
    // avoids `warn`-spam on a lock that simply has not signed this node yet, and keeps self and peer
    // classification consistent.
    if key_signature.is_empty() {
        return SelfLockVerdict::Unsigned;
    }
    match authority.node_key_authorized(&node_key.to_bytes(), key_signature) {
        Ok(()) => SelfLockVerdict::Authorized,
        Err(e) => SelfLockVerdict::LockedOut(e.to_string()),
    }
}

/// Emit the self-locked-out observability signal (Go `tkaFilterNetmapLocked`'s self check → a
/// `LockedOut` health warning): classify SELF against the freshly-synced `authority` and log.
///
/// This is **observability, not enforcement** — self never enters the peer db, so the lock can never
/// filter our own node out of the netmap. But a self the lock does not authorize means this node's
/// key-signature is absent or invalid for the active lock, so it cannot prove itself to locked peers
/// (they will drop it); surfacing that lets an operator notice and re-sign. A never-signed node
/// (empty signature) logs at `info`, distinct from a present-but-invalid signature (`warn`), so the
/// common unsigned case does not spam a warning. This fork has no health subsystem, so the operator
/// signal is a `tracing` event (its observability channel).
fn log_self_lockout(self_node: &Node, authority: &ts_tka::Authority) {
    match self_lock_verdict(&self_node.node_key, &self_node.key_signature, authority) {
        SelfLockVerdict::Unsigned => tracing::info!(
            "TKA: this node has no key-signature for the active lock; it cannot prove itself to \
             locked peers until control signs it (not locked out, just unsigned)"
        ),
        SelfLockVerdict::Authorized => {
            tracing::debug!("TKA: self node-key is authorized by the active lock")
        }
        SelfLockVerdict::LockedOut(error) => tracing::warn!(
            %error,
            "TKA self locked out: this node's key-signature is not authorized by the active \
             network lock; locked peers will reject it until control re-signs this node \
             (Go LockedOut)"
        ),
    }
}

// The `#[kameo::messages]` macro generates message structs whose fields mirror the method params;
// those generated fields carry no doc and can't take attributes, so wrap in a module where
// missing-docs is allowed (same pattern as PeerTracker's `msg_impl`). The generated message structs
// are re-exported so callers keep referencing them at `control_runner::<Name>`.
pub use msg_impl::*;

#[allow(missing_docs)]
mod msg_impl {
    use kameo::{message::Context, reply::DelegatedReply};

    use super::*;

    #[kameo::messages]
    impl ControlRunner {
        /// Fetch the IPv4 address for this tailscale device.
        #[message(ctx)]
        pub fn ipv4(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Option<Ipv4Addr>>>,
        ) -> DelegatedReply<Option<Ipv4Addr>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let fut = self.with_self_node(|node| node.tailnet_address.ipv4.addr());

                tokio::spawn(async move {
                    let ip = fut.await;
                    replier.send(ip);
                });
            }

            deleg
        }

        /// Fetch the IPv6 address for this tailscale device.
        #[message(ctx)]
        pub fn ipv6(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Option<Ipv6Addr>>>,
        ) -> DelegatedReply<Option<Ipv6Addr>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let fut = self.with_self_node(|node| node.tailnet_address.ipv6.addr());

                tokio::spawn(async move {
                    let ip = fut.await;
                    replier.send(ip);
                });
            }

            deleg
        }

        /// Fetch the self node for this tailscale device.
        #[message(ctx)]
        pub fn self_node(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
        ) -> DelegatedReply<Option<Node>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let node = self.with_self_node(|node| node.clone());

                tokio::spawn(async move {
                    let node = node.await;
                    replier.send(node)
                });
            }

            deleg
        }

        /// Fetch the current Tailscale SSH policy, if control has pushed one.
        ///
        /// Returns `None` when control has not sent an SSH policy (the SSH server treats this as
        /// deny-all — fail-closed). Unlike `self_node` this does not block waiting
        /// for a value: an absent policy is a legitimate, immediate answer.
        #[message]
        pub fn current_ssh_policy(&self) -> Option<SshPolicy> {
            self.ssh_policy.borrow().clone()
        }

        /// Fetch the current Tailnet Lock status, if control has pushed one.
        ///
        /// Returns `None` when control has sent no `TKAInfo` (tailnet lock not in use / no change seen).
        #[message]
        pub fn current_tka_status(&self) -> Option<TkaStatus> {
            self.tka.borrow().clone()
        }

        /// Read up to `limit` entries of the Tailnet-Lock update-chain log, **head-first** (newest →
        /// oldest), from the locally-synced AUM chain (Go `NetworkLockLog`).
        ///
        /// A **pure local read** — no crypto, no mutation, no control round-trip: it walks the
        /// already-verified `SyncedTka` store this actor owns. Returns an empty `Vec` when no lock is
        /// synced (Go's `b.tka == nil`). Synchronous (no spawn), like `current_tka_status` — the
        /// chain is in memory.
        #[message]
        pub fn tka_log(&self, limit: usize) -> Vec<crate::tka_sync::TkaLogEntry> {
            let Some(synced) = self.tka_synced.as_ref() else {
                return Vec::new();
            };
            crate::tka_sync::tka_log_entries(&synced.store, synced.oldest, limit)
        }

        /// Sign `node_key` directly with this node's network-lock key and submit the signature to
        /// control (Go `tka.sign` for the Direct case → `tkaSubmitSignature`).
        ///
        /// Builds a `Direct` [`NodeKeySignature`](ts_tka::NodeKeySignature) via
        /// [`sign_direct`](ts_tka::NodeKeySignature::sign_direct) over this node's inner ed25519
        /// network-lock signing key, serializes it (raw CBOR), and POSTs it to `/machine/tka/sign`.
        /// Mirrors `set_dns`/`get_certificate`: clones the control config + node keys into a spawned
        /// task (delegated reply, so the round-trip doesn't block the mailbox) over a fresh Noise
        /// channel.
        ///
        /// **Posture: this only *submits* a signature to control — it does NOT mutate the local
        /// [`Authority`](ts_tka::Authority).** The local trusted-key state advances solely through the
        /// existing verified-sync path (`sync_tka` → `VerifiedAumChain::verify`); a `tka_sign` success
        /// is acknowledged to the caller, and the resulting AUM is picked up on the next netmap-driven
        /// sync. Verify-and-log is unchanged.
        #[message(ctx)]
        pub fn tka_sign(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Result<(), TkaSyncError>>>,
            node_key: [u8; 32],
        ) -> DelegatedReply<Result<(), TkaSyncError>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    // Sign the node key with our network-lock key, then submit the raw-CBOR NKS.
                    let nks = ts_tka::NodeKeySignature::sign_direct(
                        &node_key,
                        &keys.network_lock_keys.private.signing_key(),
                    );
                    let req = ts_control::TkaSubmitSignatureRequest {
                        // node_key + version are stamped by the RPC client from `keys`.
                        version: Default::default(),
                        node_key: keys.node_keys.public,
                        signature: nks.serialize(),
                    };
                    let result = tka_submit_signature(
                        &config.server_url,
                        &keys,
                        req,
                        config.allow_http_key_fetch,
                    )
                    .await
                    .map(|_response| ());
                    replier.send(result);
                });
            }

            deleg
        }

        /// Disable Tailnet Lock by presenting the disablement secret to control (Go
        /// `tka.disable` → `/machine/tka/disable`).
        ///
        /// Targets the **current** authority head (read from the cached [`TkaStatus`]); the caller
        /// supplies the `disablement_secret` out of band (it is the operator-held capability that
        /// authorizes turning the lock off). Mirrors `tka_sign`: clones config + keys into a spawned
        /// task (delegated reply). Returns [`TkaSyncError::Unsupported`] when there is no known TKA
        /// head (lock not in use / control hasn't pushed a status), since there is nothing to disable.
        ///
        /// **Submit-only, like `tka_sign`:** this POSTs the disablement to control and does NOT mutate
        /// the local [`Authority`](ts_tka::Authority). Control acts on the disablement; this node
        /// observes the result through the existing verified-sync path. Verify-and-log unchanged.
        #[message(ctx)]
        pub fn tka_disable(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Result<(), TkaSyncError>>>,
            disablement_secret: Vec<u8>,
        ) -> DelegatedReply<Result<(), TkaSyncError>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                // Read the current head from the cached status BEFORE the spawn (can't borrow &self
                // across the await). No head ⇒ no lock to disable ⇒ Unsupported.
                let head = self.tka.borrow().as_ref().map(|s| s.head.clone());
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    let result = match head {
                        Some(head) => {
                            let req = ts_control::TkaDisableRequest {
                                // node_key + version are stamped by the RPC client from `keys`.
                                version: Default::default(),
                                node_key: keys.node_keys.public,
                                head,
                                disablement_secret,
                            };
                            tka_disable(&config.server_url, &keys, req, config.allow_http_key_fetch)
                                .await
                                .map(|_response| ())
                        }
                        None => Err(TkaSyncError::Unsupported),
                    };
                    replier.send(result);
                });
            }

            deleg
        }

        /// Initialize Tailnet Lock with this node as the sole initial trusted key, gated by
        /// `disablement_secret` (Go `LocalClient.NetworkLockInit` — the "lock yourself in" case).
        ///
        /// Builds + signs a genesis Checkpoint AUM whose only trusted key is this node's network-lock
        /// public key (votes 1) and whose single DisablementValue is `disablement_value(secret)`, then
        /// drives the two-phase init: `tka/init/begin` (submit the genesis) → if control needs no
        /// further node signatures (`NeedSignatures` empty, the case when this node is the only key) →
        /// `tka/init/finish` carrying the raw `disablement_secret` as `SupportDisablement`. Mirrors
        /// `tka_sign`/`tka_disable`: cloned config + keys into a spawned task (delegated reply).
        ///
        /// If control returns a non-empty `NeedSignatures` (other nodes must be re-signed under the new
        /// lock — a multi-node tailnet), this returns [`TkaSyncError::Unsupported`]: re-signing each
        /// listed node (incl. the Rotation-key case) is a larger flow deferred to a fuller
        /// `tka_init(keys, secrets)` — the single-node lock-init is the shipped subset.
        ///
        /// **Submit-only**, like `tka_sign`/`tka_disable`: this creates the lock at control and does
        /// NOT seed the local [`Authority`](ts_tka::Authority) — the node picks up the new lock through
        /// the existing verified netmap-sync (control pushes a `TKAInfo`, `maybe_sync_tka` bootstraps
        /// the genesis through `VerifiedAumChain::verify`). Verify-and-log posture unchanged.
        #[message(ctx)]
        pub fn tka_init(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Result<(), TkaSyncError>>>,
            disablement_secret: Vec<u8>,
        ) -> DelegatedReply<Result<(), TkaSyncError>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    let result = tka_init_run(&config, &keys, disablement_secret).await;
                    replier.send(result);
                });
            }

            deleg
        }

        /// The cert-eligible DNS names from control's netmap DNS config (Go `nm.DNS.CertDomains`).
        ///
        /// Returns an empty `Vec` when control has sent no DNS config, or one carrying no cert
        /// domains (an empty list is a legitimate, immediate answer — like `current_ssh_policy`, this
        /// does not block waiting for a value).
        #[message]
        pub fn cert_domains(&self) -> Vec<String> {
            self.cert_domains.borrow().clone()
        }

        /// The full DNS config from control's netmap (Go `netmap.NetworkMap.DNS`), or `None` when
        /// control has sent no DNS config yet. An immediate answer (does not block); the facade
        /// surfaces this for `Device::dns_config` (the daemon's `tnet dns status`).
        #[message]
        pub fn dns_config(&self) -> Option<ts_control::DnsConfig> {
            self.dns_config.borrow().clone()
        }

        /// The interactive-login / consent URL control last asked this node to open
        /// (`MapResponse.PopBrowserURL`), or `None` when control has sent none. An immediate answer
        /// (does not block); the facade surfaces this for `Device::pop_browser_url`.
        #[message]
        pub fn pop_browser_url(&self) -> Option<url::Url> {
            self.pop_browser_url.borrow().clone()
        }

        /// Subscribe to the interactive-login / consent URL cell (`MapResponse.PopBrowserURL`).
        ///
        /// Returns a [`watch::Receiver`] whose value is the latest running-node consent URL, used by
        /// [`Runtime::watch_ipn_bus`](crate::Runtime::watch_ipn_bus) to surface `browse_to_url`
        /// events mid-session. The cell is sticky (updated only on a new non-empty URL, never reset
        /// to `None` by an empty update — see the field docs), so a subscriber is not thrashed and a
        /// late subscriber sees the current URL. The initial value is `None` until control sends one.
        #[message(derive(Clone))]
        pub fn watch_browser_url(&self) -> watch::Receiver<Option<url::Url>> {
            self.pop_browser_url.subscribe()
        }

        /// The latest network-conditions report (preferred DERP region + per-region latencies). An
        /// immediate answer (does not block); empty before the first DERP-latency measurement. The
        /// facade surfaces this for `Device::netcheck` (the daemon's `tnet netcheck`).
        #[message]
        pub fn netcheck(&self) -> crate::status::NetcheckReport {
            self.netcheck.borrow().clone()
        }

        /// Request an OIDC ID token from control scoped to `audience` (workload-identity federation).
        ///
        /// Opens a fresh Noise channel and POSTs `/machine/id-token`; returns the signed JWT or an
        /// [`IdTokenError`]. Runs on a spawned task (delegated reply) so the actor mailbox isn't blocked
        /// for the round-trip.
        #[message(ctx)]
        pub fn fetch_id_token(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Result<String, IdTokenError>>>,
            audience: String,
        ) -> DelegatedReply<Result<String, IdTokenError>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    let result = ts_control::fetch_id_token(&config, &keys, &audience).await;
                    replier.send(result);
                });
            }

            deleg
        }

        /// Log this node out of the tailnet: deregister it by expiring its current node key.
        ///
        /// Mirrors `fetch_id_token`: clones the control config + node keys
        /// into a spawned task (delegated reply, so the round-trip doesn't block the mailbox) and
        /// re-POSTs `/machine/register` with a past expiry over a fresh Noise channel. This is a
        /// control-plane state change only — it does NOT stop this actor or tear down the datapath
        /// (the caller follows up with the normal runtime shutdown), and it does not touch the
        /// on-disk node key, so re-registering with the same key is the re-login path.
        #[message(ctx)]
        pub fn logout(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Result<(), LogoutError>>>,
        ) -> DelegatedReply<Result<(), LogoutError>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    let result = ts_control::logout(&config, &keys).await;
                    replier.send(result);
                });
            }

            deleg
        }

        /// Publish a DNS record for this node via control's `/machine/set-dns` (Go
        /// `LocalClient.SetDNS`).
        ///
        /// Mirrors `fetch_id_token`: clones the control config + node keys
        /// into a spawned task (delegated reply, so the round-trip doesn't block the mailbox) and
        /// POSTs the record over a fresh Noise channel. Go's `SetDNS` is `TXT`-only (its sole use is
        /// the ACME DNS-01 `_acme-challenge` record); the record type is fixed to `"TXT"` here to
        /// match, so the surfaced API takes only `name` + `value`.
        #[message(ctx)]
        pub fn set_dns(
            &self,
            ctx: &mut Context<Self, DelegatedReply<Result<(), SetDnsError>>>,
            name: String,
            value: String,
        ) -> DelegatedReply<Result<(), SetDnsError>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    let result = ts_control::set_dns(&config, &keys, &name, "TXT", &value).await;
                    replier.send(result);
                });
            }

            deleg
        }
    }

    /// The reply type of the [`get_cert_pair`](ControlRunner::get_cert_pair) message: the issued
    /// `(cert_chain_pem, key_pem)` PEM pair (the `tnet cert` surface) or a [`ts_control::CertError`].
    /// Aliased so the message's `Context` type stays under clippy's `type_complexity` bar (the
    /// nested `Result<(String, String), _>` trips it inline).
    #[cfg(feature = "acme")]
    pub type CertPairReply = Result<(String, String), ts_control::CertError>;

    // The `acme`-gated cert-issuance message lives in its own `#[kameo::messages]` impl block so the
    // proc-macro never sees it in a non-`acme` build (a `#[cfg]` *inside* a single messages-impl
    // block is not honored by the macro's generated dispatch — it would emit a `GetCertificate`
    // handler calling a `get_certificate` method that the same `#[cfg]` strips). A separate gated
    // block keeps the default build clean.
    #[cfg(feature = "acme")]
    #[kameo::messages]
    impl ControlRunner {
        /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` via the
        /// client-side ACME DNS-01 engine (`acme` feature).
        ///
        /// Mirrors `fetch_id_token`: clones the control config + node keys
        /// into a spawned task (delegated reply, so the round-trip doesn't block the mailbox), loads
        /// or generates the ACME account key, and runs issuance against Let's Encrypt production,
        /// publishing the DNS-01 challenge TXT through the node's `POST /machine/set-dns` RPC.
        ///
        /// The account key is loaded from [`ts_keys::NodeState::acme_account_key`] (PKCS#8 DER) when
        /// present, so the same ACME account persists across renewals; otherwise an ephemeral key is
        /// generated for this call only (a fresh ACME account each issuance — acceptable for v1; LE
        /// allows it). Persisting a generated key back into the key file is the embedder's job (no
        /// write-back path here). SaaS-only: against a self-hosted control plane the set-dns
        /// publish 501s.
        #[message(ctx)]
        pub fn get_certificate(
            &self,
            ctx: &mut Context<
                Self,
                DelegatedReply<Result<ts_control::tls::CertifiedKey, ts_control::CertError>>,
            >,
            name: String,
        ) -> DelegatedReply<Result<ts_control::tls::CertifiedKey, ts_control::CertError>> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    let result = issue_certificate(&config, &keys, &name).await;
                    replier.send(result);
                });
            }

            deleg
        }

        /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` and return the
        /// **PEM pair** — `(cert_chain_pem, key_pem)` — for writing the on-disk `.crt` + `.key`
        /// (the daemon's `tnet cert`, Go's `LocalClient.CertPair`). `acme` feature.
        ///
        /// Identical issuance to [`get_certificate`](Self::get_certificate) (same client-side ACME
        /// DNS-01 flow, same set-dns publish, same account-key handling), only the *shape* of the
        /// result differs: this surfaces the raw chain + leaf-key PEMs instead of the opaque
        /// [`CertifiedKey`](ts_control::tls::CertifiedKey). The leaf **private key** PEM is the
        /// second tuple element and is NEVER logged — the spawned task sends it straight back to the
        /// replier. SaaS-only: against a self-hosted control plane the set-dns publish 501s.
        #[message(ctx)]
        pub fn get_cert_pair(
            &self,
            ctx: &mut Context<Self, DelegatedReply<CertPairReply>>,
            name: String,
        ) -> DelegatedReply<CertPairReply> {
            let (deleg, replier) = ctx.reply_sender();

            if let Some(replier) = replier {
                let config = self.params.config.clone();
                let keys = self.params.env.keys.clone();
                tokio::spawn(async move {
                    let result = issue_cert_pair(&config, &keys, &name).await;
                    replier.send(result);
                });
            }

            deleg
        }
    }
}

/// The `tka_init` body (the genesis-build + two-phase init/begin→init/finish choreography),
/// factored out of the actor handler so it runs in the spawned task. See [`ControlRunner::tka_init`].
///
/// "Lock yourself in": the genesis trusts only this node's network-lock key (votes 1) and stores one
/// DisablementValue = `disablement_value(secret)`. On a non-empty `NeedSignatures` (multi-node
/// tailnet needing re-signs) it returns [`TkaSyncError::Unsupported`] — the single-node subset.
async fn tka_init_run(
    config: &ts_control::Config,
    keys: &ts_keys::NodeState,
    disablement_secret: Vec<u8>,
) -> Result<(), TkaSyncError> {
    // Build the genesis: this node's NL public key as the sole trusted key, one disablement value.
    let nl_public = keys.network_lock_keys.public.to_bytes().to_vec();
    let genesis_key = ts_tka::AumKey {
        kind: ts_tka::KeyKind::Ed25519,
        votes: 1,
        public: nl_public,
        meta: Vec::new(),
    };
    let dvalue = ts_tka::disablement_value(&disablement_secret).to_vec();
    let mut genesis = ts_tka::Aum::new_genesis_checkpoint(vec![genesis_key], vec![dvalue])
        // A malformed genesis is a local construction bug, not a transient RPC failure — surface it as a
        // coarse internal error rather than NetworkError (which would invite a pointless retry).
        .map_err(|_| TkaSyncError::Internal(ts_control::TkaSyncInternalErrorKind::SerDe))?;
    genesis.sign(&keys.network_lock_keys.private.signing_key());

    // Phase 1: submit the genesis. node_key + version are stamped by the RPC client from `keys`.
    let begin_req = ts_control::TkaInitBeginRequest {
        version: Default::default(),
        node_key: keys.node_keys.public,
        genesis_aum: genesis.serialize(),
    };
    let begin_resp = tka_init_begin(
        &config.server_url,
        keys,
        begin_req,
        config.allow_http_key_fetch,
    )
    .await?;

    // Single-node case only: control must need no further node signatures. A non-empty
    // NeedSignatures means other nodes must be re-signed under the new lock — deferred.
    if !begin_resp.need_signatures.is_empty() {
        tracing::warn!(
            need = begin_resp.need_signatures.len(),
            "tka_init: control requires re-signing other nodes; the multi-node init is not yet \
             implemented (single-node lock-init only)"
        );
        return Err(TkaSyncError::Unsupported);
    }

    // Phase 2: finish, carrying the raw disablement secret as SupportDisablement (Go sends the raw
    // secret here; only the genesis stores its Argon2i hash).
    let finish_req = ts_control::TkaInitFinishRequest {
        version: Default::default(),
        node_key: keys.node_keys.public,
        signatures: std::collections::BTreeMap::new(),
        support_disablement: disablement_secret,
    };
    tka_init_finish(
        &config.server_url,
        keys,
        finish_req,
        config.allow_http_key_fetch,
    )
    .await
    .map(|_response| ())
}

/// Load or generate the ACME account key, then issue a cert for `name` via set-dns DNS-01,
/// returning just the ready-to-serve [`CertifiedKey`](ts_control::tls::CertifiedKey) (the
/// `get_certificate` / `ListenTLS` path).
///
/// Thin wrapper over [`issue_cert_pair`] that drops the PEMs — one issuance, this caller just
/// doesn't need the on-disk pair. See [`issue_cert_pair`] for the account-key handling.
#[cfg(feature = "acme")]
async fn issue_certificate(
    config: &ts_control::Config,
    keys: &ts_keys::NodeState,
    name: &str,
) -> Result<ts_control::tls::CertifiedKey, ts_control::CertError> {
    issue_cert_pair_inner(config, keys, name)
        .await
        .map(|issued| issued.certified)
}

/// Load or generate the ACME account key, then issue a cert for `name` via set-dns DNS-01,
/// returning the **PEM pair** `(cert_chain_pem, key_pem)` for the daemon's on-disk `.crt`/`.key`
/// (`tnet cert`, Go `LocalClient.CertPair`).
///
/// Same single issuance as [`issue_certificate`]; only the result shape differs. The leaf
/// **private key** PEM is the second element and is NEVER logged here.
#[cfg(feature = "acme")]
async fn issue_cert_pair(
    config: &ts_control::Config,
    keys: &ts_keys::NodeState,
    name: &str,
) -> Result<(String, String), ts_control::CertError> {
    issue_cert_pair_inner(config, keys, name)
        .await
        .map(|issued| (issued.cert_chain_pem, issued.key_pem))
}

/// Shared issuance core for [`issue_certificate`] and [`issue_cert_pair`]: load (or generate) the
/// ACME account key, target Let's Encrypt production, and run one DNS-01 issuance, returning the
/// full [`IssuedCert`](ts_control::acme::IssuedCert) so each caller projects out what it needs (one
/// ACME order, two consumers).
///
/// Reuses the persisted [`ts_keys::NodeState::acme_account_key`] (PKCS#8 DER) when present so the
/// same Let's Encrypt account survives renewals; otherwise generates an ephemeral per-call key
/// (logged at debug — a new ACME account each issuance, with no write-back). Always targets Let's
/// Encrypt production ([`ts_control::acme::LETS_ENCRYPT_PRODUCTION_DIRECTORY`]). Never logs the leaf
/// private key.
#[cfg(feature = "acme")]
async fn issue_cert_pair_inner(
    config: &ts_control::Config,
    keys: &ts_keys::NodeState,
    name: &str,
) -> Result<ts_control::acme::IssuedCert, ts_control::CertError> {
    let account_key = match keys.acme_account_key.as_deref() {
        Some(der) => ts_control::acme::AcmeAccountKey::from_pkcs8(der)?,
        None => {
            tracing::debug!(
                "no persisted ACME account key in key state; generating an ephemeral per-call key \
                 (a new ACME account this issuance — not persisted back)"
            );
            ts_control::acme::AcmeAccountKey::generate()?.0
        }
    };
    let directory = ts_control::acme::LETS_ENCRYPT_PRODUCTION_DIRECTORY
        .parse()
        .map_err(|e| {
            ts_control::CertError::Acme(format!("parsing Let's Encrypt directory URL: {e}"))
        })?;
    ts_control::issue_cert_pair_via_setdns(config, keys, name, &account_key, &directory).await
}

/// Publish the cached netmap, if this node has one, onto the netmap bus.
///
/// The cold-start half of the netmap cache (Go `nodecap.CacheNetworkMaps`). Reads
/// [`Config::netmap_cache_dir`](ts_control::Config::netmap_cache_dir) — `None` means the embedder
/// configured no storage, so there is nothing to replay — and decodes whatever is there with the
/// same decoder the live map poll uses.
///
/// The read is deliberately **not** gated on the node attributes. Nothing is ever written without
/// the grant, so the presence of a cache is itself the record that control asked for one (Go makes
/// the same argument: at this point in start-up the client has not spoken to control yet, so the
/// grant is not knowable). If the grant has since been withdrawn, the first netmap of this session
/// says so and the cache is discarded then.
///
/// **Peers cached under Tailnet Lock are filtered, not withheld.** Go replays its cached map through
/// `tkaFilterNetmapLocked`, which it can do at cold start because its TKA authority is persisted on
/// disk; the peers that hold a valid signature survive and are dialed. This port persists the same
/// authority next to the cached netmap (see [`load_cached_netmap`]) and runs the same filter over the
/// cached peers. With no persisted authority to run it with — a cache written before this node ever
/// completed a TKA sync — the peers are withheld and control's first netmap brings them back moments
/// later, behind a synced authority.
///
/// The bus has no replay, so this reaches only subscribers already registered. Every netmap
/// subscriber is spawned by `Runtime::spawn` before the control runner and registers from its own
/// `on_start` with no I/O in the way, while this path awaits a file read first — so in practice the
/// subscribers are there. A subscriber that is not simply misses the head start and is brought
/// current by control's first netmap, which is exactly the behaviour of a node with no cache.
async fn replay_cached_netmap(params: &Params) {
    let Some(dir) = params.config.netmap_cache_dir.as_ref() else {
        return;
    };

    let Some(update) = load_cached_netmap(&ts_control::NetmapCache::new(dir)).await else {
        return;
    };

    let peers = match update.peer_update.as_ref() {
        Some(ts_control::PeerUpdate::Full(peers)) => peers.len(),
        _ => 0,
    };
    tracing::info!(peers, "replaying cached netmap on cold start");

    if let Err(e) = params.env.publish(Arc::new(update)).await {
        tracing::warn!(error = %e, "publishing the cached netmap");
    }
}

/// Read the cached netmap, vouching for its peers with the Tailnet-Lock authority persisted beside
/// it — the whole of the cold-start decision, with no actor state in it so it can be tested directly.
///
/// A netmap cached while the lock was **off** replays whole; there is nothing to enforce (Go's
/// `tkaFilterNetmapLocked` returns early on `b.tka == nil`). A netmap cached while the lock was
/// **on** replays exactly the peers
/// [`PeerTracker::tka_keep_verdicts`](crate::peer_tracker::PeerTracker::tka_keep_verdicts) admits —
/// the same pass the live netmap path runs, so a peer that is dialed from the cache is one the
/// authority authorized, and an unsigned peer, a peer whose signature fails, or a peer a newer
/// rotation obsoletes is dropped.
///
/// Three things make the persisted authority safe to enforce with:
///
/// * it is re-verified from genesis on load ([`crate::tka_sync::authority_from_encoded_chain`]), so a
///   blob that is not a signed chain yields no authority;
/// * it is read back out of the same directory the netmap is, vetted as private to this user, so a
///   local attacker cannot choose which chain we start from any more than they can choose the netmap;
/// * its head must equal the head the **cached frame** recorded (`MapResponse.TKAInfo.Head`) — see
///   [`vouch_cached_peers`]. Go does not need that check because it has no second file to reconcile,
///   only the chonk.
pub(crate) async fn load_cached_netmap(cache: &ts_control::NetmapCache) -> Option<StateUpdate> {
    let authority = match cache.load_tka_chain().await {
        None => None,
        Some(blob) => match crate::tka_sync::authority_from_encoded_chain(&blob) {
            Ok(authority) => Some(authority),
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "persisted tailnet-lock chain did not verify; replaying the cached netmap \
                     without its peers"
                );
                None
            }
        },
    };

    cache
        .load_state_update_vouched(|tka, peers| vouch_cached_peers(authority.as_ref(), tka, peers))
        .await
}

/// The peers of a netmap cached under an **active** Tailnet Lock that may be replayed on this cold
/// start: those the persisted `authority` authorizes, by exactly the pass a live netmap's peers go
/// through (Go's `tkaFilterNetmapLocked`, via
/// [`PeerTracker::tka_keep_verdicts`](crate::peer_tracker::PeerTracker::tka_keep_verdicts) — the
/// per-peer signature verdict plus the cross-peer rotation filter).
///
/// Nothing is replayed unless the authority can be held to the netmap:
///
/// * **no persisted authority** (this node has never completed a TKA sync, or the chain did not
///   verify) ⇒ no peers. There is nothing to check a `key_signature` against, and admitting them
///   unchecked would dial a peer the lock may have revoked while this node was off.
/// * **the chain is not at the head the cached frame recorded** (`MapResponse.TKAInfo.Head`) ⇒ no
///   peers. The netmap and the chain are written on separate events, so they can disagree — a crash
///   between them, or a lock disabled and re-enabled under a fresh genesis — and a chain that
///   describes a different lock says nothing about these peers.
///
/// Separated from the I/O in [`load_cached_netmap`] so the decision itself is directly testable with
/// a real chain-derived [`Authority`](ts_tka::Authority) and real signatures.
pub(crate) fn vouch_cached_peers(
    authority: Option<&ts_tka::Authority>,
    tka: &TkaStatus,
    peers: Vec<Node>,
) -> Vec<Node> {
    let Some(authority) = authority else {
        tracing::info!(
            "no persisted tailnet-lock authority to verify the cached peers against; withholding \
             them until control's first netmap"
        );
        return Vec::new();
    };

    if !ts_tka::AumHash::from_base32(&tka.head).is_some_and(|head| authority.head_matches(&head)) {
        tracing::warn!(
            head = %tka.head,
            "persisted tailnet-lock chain is not at the head the cached netmap recorded; \
             withholding its peers"
        );
        return Vec::new();
    }

    let keep = {
        let refs: Vec<&Node> = peers.iter().collect();
        crate::peer_tracker::PeerTracker::tka_keep_verdicts(Some(authority), &refs)
    };
    peers
        .into_iter()
        .zip(keep)
        .filter_map(|(peer, keep)| keep.then_some(peer))
        .collect()
}

/// The Tailnet-Lock chain persisted beside the cached netmap ([`load_cached_netmap`]), and the only
/// path that writes or removes it.
///
/// It is one owner because the two operations have to be ordered **against each other**. They used
/// not to be: the write was awaited on the actor thread while the removal was `tokio::spawn`ed, so a
/// removal scheduled by a lock *disable* could still be sitting in the runtime's queue when a sync
/// that followed it persisted a fresh chain, and then delete that fresh chain. Nothing tells the node
/// afterwards — the loss shows up one restart later, as a cold start that withholds peers it was
/// entitled to dial. Every operation here is awaited by its caller instead, and both callers
/// ([`ControlRunner::persist_tka_chain`] and the two lock-off paths) run on the actor thread, so the
/// actor's mailbox is the order.
///
/// This owns no policy about *which* chain may be replayed: the cold-start replay judges whatever it
/// finds on disk against the head the cached netmap frame itself recorded, so nothing here can admit
/// a peer, and a failed removal is a chain the replay refuses rather than one it trusts.
struct PersistedTkaChain {
    /// The cache directory the chain lives in, or `None` when the embedder configured no
    /// [`netmap_cache_dir`](ts_control::Config::netmap_cache_dir) — then no netmap is cached either,
    /// so there is nothing to vouch for and nothing to remove.
    cache: Option<ts_control::NetmapCache>,
    /// Whether a chain may be on disk right now.
    ///
    /// **Starts `true`.** A fresh process has synced nothing, but an earlier process of this node can
    /// have left a chain in the cache directory, and that chain is precisely what the next cold start
    /// would load. So "this process never synced a lock" is not evidence that there is nothing to
    /// remove, and the lock-off paths must not treat it as such.
    ///
    /// Its only job is to keep the repeated removal free: a tailnet with no lock reports the lock off
    /// on *every* netmap, and one `remove_file` per netmap tick is a syscall for nothing. It is never
    /// trusted in the other direction — a stale `false` cannot admit a peer, because admitting is the
    /// replay's decision and the replay reads the disk.
    maybe_on_disk: bool,
}

impl PersistedTkaChain {
    /// The chain owner for a node configured with `dir` as its netmap cache directory.
    fn new(dir: Option<&std::path::PathBuf>) -> Self {
        Self {
            cache: dir.map(ts_control::NetmapCache::new),
            maybe_on_disk: true,
        }
    }

    /// Persist `blob` as the chain the cached netmap's peers were admitted under. A write failure is
    /// logged inside the cache and swallowed; the flag stays set, so the next lock-off still tries to
    /// remove whatever did land.
    async fn store(&mut self, blob: &[u8]) {
        let Some(cache) = self.cache.as_ref() else {
            return;
        };
        cache.store_tka_chain(blob).await;
        self.maybe_on_disk = true;
    }

    /// Remove the persisted chain. Awaited, so it has happened by the time this returns and cannot
    /// overtake a later [`store`](Self::store).
    async fn discard(&mut self) {
        if !self.maybe_on_disk {
            return;
        }
        let Some(cache) = self.cache.as_ref() else {
            // No directory was ever configured, so nothing can be on disk and nothing ever will be.
            // Settle the flag rather than re-deciding this on every netmap.
            self.maybe_on_disk = false;
            return;
        };
        cache.discard_tka_chain().await;
        self.maybe_on_disk = false;
    }
}

impl Message<StreamMessage<Arc<StateUpdate>, (), ()>> for ControlRunner {
    type Reply = ();

    async fn handle(
        &mut self,
        msg: StreamMessage<Arc<StateUpdate>, (), ()>,
        ctx: &mut Context<Self, Self::Reply>,
    ) {
        match msg {
            StreamMessage::Started(_) => {
                tracing::trace!("started listening to state updates");
            }

            StreamMessage::Next(msg) => {
                if let Some(node) = msg.node.as_ref() {
                    // Reflect node-key expiry into the device state. Control delivering a self-node
                    // whose key is in the past means the node must re-authenticate; the arrival of a
                    // fresh (non-expired) self-node confirms we are Running (recovering the state if a
                    // prior update had flipped it to Expired/Reauthenticating). On expiry, decide
                    // between an automatic re-auth (Go `doLogin`: rotate key + re-register with the
                    // stored auth key) and the terminal Expired state via the pure `expiry_action`:
                    //   - auth key retained, reauth enabled, and TKA NOT enforcing → Reauthenticate.
                    //   - otherwise → Expired (no auth key / reauth disabled / TKA-locked).
                    // The TKA gate is a hard safety constraint: rotating on a locked tailnet would
                    // install an unsigned key and lock this node out of locked peers (the TKA re-sign
                    // is a separate follow-up). Recovery from Reauthenticating is automatic — the next
                    // good self-node flips back to Running at this same handler.
                    let now_unix = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .map(|d| d.as_secs() as i64)
                        .unwrap_or(0);
                    let action = expiry_action(
                        node.key_expired_at_unix(now_unix),
                        self.params.auth_key.is_some(),
                        self.params.config.reauth_on_expiry,
                        self.tka_authority.borrow().is_some(),
                    );

                    // Bounded reauth sub-state-machine (circuit breaker), evaluated by the pure
                    // `reauth_circuit_step`. The `Reauthenticate` path must settle — a one-shot /
                    // already-consumed auth key cannot re-register, so an unbounded reauth would sit in
                    // `Reauthenticating` forever. The step takes the `expiry_action` verdict, whether we
                    // are ALREADY reauthenticating (i.e. the prior reauth has not recovered), and the
                    // current attempt counter, and returns the target state, the new counter, and
                    // whether to fire the one-shot reauth (fired ONLY on entry, so the node key rotates
                    // at most once per episode — a second rotation would lose the original `OldNodeKey`
                    // anchor). At `MAX_REAUTH_ATTEMPTS` it trips to terminal `Expired`.
                    //
                    // NOTE: we may act (count, and eventually flip to `Expired`) even when the published
                    // state does NOT change — a repeated `Reauthenticating` self-node is exactly the
                    // "prior reauth didn't recover" signal we must tally, so the counting reads the
                    // pre-step state directly here and `send_if_modified`'s `changed` only gates the
                    // log/firing below, never the counting.
                    let already_reauthing = matches!(
                        &*self.params.state_tx.borrow(),
                        crate::DeviceState::Reauthenticating
                    );
                    let step = reauth_circuit_step(action, already_reauthing, self.reauth_attempts);
                    self.reauth_attempts = step.attempts;
                    if step.next == ReauthState::Expired
                        && action == ExpiryAction::Reauthenticate
                        && already_reauthing
                    {
                        tracing::warn!(
                            attempts = step.attempts,
                            "automatic re-auth did not recover the node after {MAX_REAUTH_ATTEMPTS} \
                             attempts (auth key likely one-shot / consumed); falling back to terminal \
                             Expired"
                        );
                    }
                    let next = match step.next {
                        ReauthState::Running => crate::DeviceState::Running,
                        ReauthState::Reauthenticating => crate::DeviceState::Reauthenticating,
                        ReauthState::Expired => crate::DeviceState::Expired,
                    };

                    // `send_if_modified` avoids waking watchers when the state is unchanged (a fresh
                    // self-node arrives on every netmap update). Returns whether the state changed.
                    let changed = self.params.state_tx.send_if_modified(|s| {
                        if *s != next {
                            *s = next.clone();
                            true
                        } else {
                            false
                        }
                    });

                    if changed && step.fire_reauth {
                        tracing::info!(
                            "self node-key expired; starting automatic re-auth (rotate node key + \
                             re-register with stored auth key)"
                        );
                        self.client.reauth().await;
                    }

                    self.self_node.send_replace(Some(node.clone()));
                }

                if let Some(policy) = msg.ssh_policy.as_ref() {
                    self.ssh_policy.send_replace(Some(policy.clone()));
                }

                if let Some(tka) = msg.tka.as_ref() {
                    self.tka.send_replace(Some(tka.clone()));
                    self.maybe_sync_tka(tka, ctx.actor_ref().clone()).await;
                }

                // Track the cert-domain list from the netmap DNS config (Go `nm.DNS.CertDomains`).
                // An update with no DNS config, or one carrying no cert domains, means "none" — Go
                // reads an empty slice off an absent config too, so mirror that as an empty `Vec`.
                let cert_domains = msg
                    .dns_config
                    .as_ref()
                    .map(|d| d.cert_domains.clone())
                    .unwrap_or_default();
                self.cert_domains.send_replace(cert_domains);

                // Track the full DNS config for `Device::dns_config` (the daemon's `tnet dns status`).
                // `None` when control sent no DNS config on this update — distinct from a present but
                // empty config (Go `netmap.NetworkMap.DNS`).
                self.dns_config.send_replace(msg.dns_config.clone());

                // Track the interactive-login URL for `Device::pop_browser_url` /
                // `Runtime::watch_ipn_bus`. See `sticky_update_pop_browser_url` for the Go-faithful
                // sticky semantics (update only on a new non-empty URL; never reset to `None`).
                sticky_update_pop_browser_url(&self.pop_browser_url, msg.pop_browser_url.as_ref());

                if let Err(e) = self.params.env.publish(msg).await {
                    tracing::error!(error = %e, "publishing netmap update");
                }
            }

            StreamMessage::Finished(_) => {
                tracing::error!("state update stream terminated")
            }
        }
    }
}

/// The outcome of a spawned TKA bootstrap+sync task, delivered back to the actor thread so the
/// result can be applied to actor state (which a spawned task cannot touch directly). Sent by
/// [`ControlRunner::maybe_sync_tka`]; handled by applying via
/// [`ControlRunner::apply_tka_synced`](ControlRunner).
#[doc(hidden)]
pub struct TkaSynced {
    pub(crate) result:
        Result<Option<crate::tka_sync::SyncedTka>, crate::tka_sync::TkaSyncDriverError>,
    /// The [`ControlRunner::tka_generation`] captured when this sync was spawned; the handler
    /// discards the result if it no longer matches (the lock was disabled/re-synced mid-flight).
    pub(crate) generation: u64,
}

impl Message<TkaSynced> for ControlRunner {
    type Reply = ();

    async fn handle(&mut self, msg: TkaSynced, _ctx: &mut Context<Self, Self::Reply>) {
        self.apply_tka_synced(msg.result, msg.generation).await;
    }
}

impl Message<DerpLatencyMeasurement> for ControlRunner {
    type Reply = ();

    async fn handle(&mut self, msg: DerpLatencyMeasurement, _ctx: &mut Context<Self, Self::Reply>) {
        let measurements = msg.measurement.as_ref().clone();

        // Publish the net-report snapshot for `Device::netcheck` (the daemon's `tnet netcheck`) from
        // the same measurements, before the home-region short-circuit below — an empty set still
        // yields a (default/empty) report rather than a stale one.
        self.netcheck
            .send_replace(crate::status::NetcheckReport::from_region_results(
                &measurements,
            ));

        if measurements.is_empty() {
            tracing::debug!("derp latency measurements empty");
            return;
        };

        // Record this cycle into the rolling history and evict reports older than the smoothing
        // window, then compute each region's `bestRecent` (5-min min). `Instant::now()` is the
        // arrival stamp; `best_recent` takes it as a param so the decision stays unit-testable.
        let now = Instant::now();
        self.derp_report_history
            .push((now, msg.measurement.clone()));
        self.derp_report_history
            .retain(|(stamp, _)| now.saturating_duration_since(*stamp) <= DERP_HISTORY_MAX_AGE);
        let best_recent = best_recent(&self.derp_report_history, now, DERP_HISTORY_MAX_AGE);

        // Apply selection hysteresis (the pure decision lives in `select_home_region` for testability)
        // so jitter between near-equal regions does not flap the home relay. Go's asymmetric
        // smoothed-best vs raw-old comparison lives in `select_home_region`; here we just resolve the
        // chosen id back to its current-cycle latency for the home-region record + control update.
        let selected_id = select_home_region(
            self.home_region.map(|(id, _)| id),
            &measurements,
            &best_recent,
        )
        .expect("non-empty measurements always yield a selection");
        // `select_home_region` only ever returns an id drawn from `measurements`, so this lookup
        // always succeeds (same invariant the prior impl relied on when it returned the result by
        // reference). We record the current-cycle (raw) latency for the chosen region.
        let selected_latency = measurements
            .iter()
            .find(|m| m.id == selected_id)
            .expect("the selected region id is always one of the measurements")
            .latency;

        let iter = measurements.iter().map(|result| {
            (
                result.latency_map_key.as_str(),
                result.latency.as_secs_f64(),
            )
        });

        if self.home_region.map(|(id, _)| id) != Some(selected_id) {
            tracing::debug!(selected_region_id = ?selected_id, "updating home region");
        }
        self.home_region = Some((selected_id, selected_latency));
        // Advertise the smoothed home to control AND drive the local DERP relay to the same region
        // (Go `report.PreferredDERP` feeds both). `send_replace` wakes the watch on every send (it
        // does NOT coalesce same-value writes), so Multiderp's bridge sees a `SetHomeRegion` each
        // cycle; the de-dup is one layer down — `home_transition` returns `Unchanged` for a
        // re-selection of the current home, so the relay only churns on an actual home change.
        self.client.set_home_region(selected_id, iter).await;
        self.params.home_region.send_replace(Some(selected_id));
    }
}

/// The window over which `best_recent` smooths per-region DERP latency (Go `netcheck` `maxAge`).
const DERP_HISTORY_MAX_AGE: Duration = Duration::from_secs(5 * 60);

/// Compute each region's `bestRecent` — its **minimum** latency over the reports within
/// `max_age` of `now` (Go `addReportHistoryAndSetPreferredDERP`'s `bestRecent` map). Reports older
/// than the window are ignored. `now` and `max_age` are parameters (not clock-read) so this is
/// deterministically unit-testable. A region absent from every in-window report is absent from the
/// result.
fn best_recent(
    history: &[(Instant, Arc<Vec<ts_netcheck::RegionResult>>)],
    now: Instant,
    max_age: Duration,
) -> HashMap<ts_derp::RegionId, Duration> {
    let mut best: HashMap<ts_derp::RegionId, Duration> = HashMap::new();
    for (stamp, report) in history {
        // Skip reports outside the window. `saturating_duration_since` guards a `stamp` that is
        // somehow after `now` (clock skew): age 0, always in-window.
        if now.saturating_duration_since(*stamp) > max_age {
            continue;
        }
        for r in report.iter() {
            best.entry(r.id)
                .and_modify(|d| {
                    if r.latency < *d {
                        *d = r.latency;
                    }
                })
                .or_insert(r.latency);
        }
    }
    best
}

/// Choose the DERP home region id, applying Go's selection hysteresis
/// (`netcheck.addReportHistoryAndSetPreferredDERP`). Pure so the decision is unit-testable.
///
/// `measurements` is the current cycle sorted by latency ascending (so `measurements[0]` is the
/// raw-current best). `best_recent` is each region's smoothed (5-min-min) latency. Matching Go's
/// **asymmetric** comparison exactly: the new best candidate is chosen by the *smoothed* `best_recent`
/// latency (`bestAny`), while the old/home region is compared using its *current-cycle* (raw)
/// latency (`oldRegionCurLatency`). Smoothing the best damps oscillation of the best region across
/// the switch boundary that the raw-vs-raw comparison (the prior impl) would still flap on.
///
/// Keeps the `current` home region unless the new best is *meaningfully* lower-latency — switching
/// only when BOTH the current region's raw latency exceeds the smoothed-best by at least
/// `PREFERRED_DERP_ABSOLUTE_DIFF` (10ms) AND the smoothed-best is at most two-thirds of the current
/// region's raw latency (a >~33% improvement). On the first selection (`current` is `None`), when the
/// smoothed-best already IS the current region, or when the current region dropped out of the
/// measurements, returns the best directly. `None` only if `measurements` is empty.
fn select_home_region(
    current: Option<ts_derp::RegionId>,
    measurements: &[ts_netcheck::RegionResult],
    best_recent: &HashMap<ts_derp::RegionId, Duration>,
) -> Option<ts_derp::RegionId> {
    /// Go `netcheck.preferredDERPAbsoluteDiff`.
    const PREFERRED_DERP_ABSOLUTE_DIFF: Duration = Duration::from_millis(10);

    // The smoothed latency for a region: its `best_recent` if present, else its current sample (a
    // region seen only this cycle has a 1-sample history, so its min == its current latency anyway).
    let smoothed = |m: &ts_netcheck::RegionResult| -> Duration {
        best_recent.get(&m.id).copied().unwrap_or(m.latency)
    };

    // Pick the best candidate by SMOOTHED latency (Go `bestAny = min over regions of bestRecent`).
    // `measurements` is sorted by raw latency, but smoothing can reorder, so scan for the smoothed
    // minimum explicitly rather than trusting `measurements[0]`.
    let best = measurements.iter().min_by_key(|m| smoothed(m))?;
    let best_any = smoothed(best);

    let Some(old_id) = current.filter(|id| *id != best.id) else {
        // First selection, or the smoothed-best already is the current home region.
        return Some(best.id);
    };

    // Compare against the old region's CURRENT (raw) latency this cycle, if it is still present —
    // Go's `oldRegionCurLatency`, deliberately unsmoothed (the asymmetry).
    match measurements.iter().find(|m| m.id == old_id) {
        Some(old) => {
            // Byte-faithful to Go: `oldRegionCurLatency - bestAny < 10ms || bestAny >
            // oldRegionCurLatency/3*2`. `saturating_sub` matches Go's signed subtraction for the
            // `< 10ms` test (when `old < best_any` Go is negative → `< 10ms` true; saturating_sub
            // floors to 0 → also true). The two-thirds rule uses INTEGER `Duration` division
            // `(old/3)*2` — NOT float `* 2.0/3.0`: Go computes the threshold in integer nanoseconds
            // (`oldNs/3` truncates), and float arithmetic diverges from it at the exact 2/3 boundary
            // with whole-millisecond inputs (e.g. old=36ms, best=24ms: Go's `24ms > 24ms` is false →
            // switch, but float `0.024 > 0.0239999997` is true → keep). `Duration / u32` truncates
            // nanos exactly like Go and `* u32` is exact, reproducing `oldRegionCurLatency/3*2`.
            let keep_old = old.latency.saturating_sub(best_any) < PREFERRED_DERP_ABSOLUTE_DIFF
                || best_any > (old.latency / 3) * 2;
            Some(if keep_old { old.id } else { best.id })
        }
        // The current region is no longer reachable this cycle: take the new best.
        None => Some(best.id),
    }
}

impl Message<EndpointAdvertisement> for ControlRunner {
    type Reply = ();

    async fn handle(&mut self, msg: EndpointAdvertisement, _ctx: &mut Context<Self, Self::Reply>) {
        let endpoints: Vec<Endpoint> = msg
            .endpoints
            .iter()
            .map(|ep| Endpoint {
                endpoint: ep.addr,
                ty: match ep.ty {
                    SelfEndpointType::Local => EndpointType::Local,
                    SelfEndpointType::Stun => EndpointType::Stun,
                    SelfEndpointType::Stun4LocalPort => EndpointType::Stun4LocalPort,
                },
            })
            .collect();

        tracing::debug!(
            n_endpoints = endpoints.len(),
            "advertising endpoints to control"
        );

        self.client.set_endpoints(endpoints).await;
    }
}

/// Re-advertise this node's routable IP prefixes (`Hostinfo.RoutableIPs`) to control — the wire
/// half of a runtime [`Runtime::set_advertise_routes`](crate::Runtime::set_advertise_routes). Sent
/// as a direct `ask` from the runtime (not over the bus), so the route change reaches the live
/// map-poll client. `routes` is the final advertised set the caller wants control to grant.
#[derive(Debug)]
pub struct SetAdvertiseRoutes {
    /// The prefixes to advertise to control (already filtered to the final set).
    pub routes: Vec<ipnet::IpNet>,
}

impl Message<SetAdvertiseRoutes> for ControlRunner {
    type Reply = ();

    async fn handle(&mut self, msg: SetAdvertiseRoutes, _ctx: &mut Context<Self, Self::Reply>) {
        tracing::debug!(n_routes = msg.routes.len(), "advertising routes to control");
        self.client.set_routable_ips(msg.routes).await;
    }
}

/// Update this node's `Hostinfo.Hostname` at control — the wire half of a runtime
/// [`Runtime::set_hostname`](crate::Runtime::set_hostname). A direct `ask` from the runtime, so the
/// change reaches the live map-poll client.
#[derive(Debug)]
pub struct SetHostname {
    /// The new hostname to report to control.
    pub hostname: String,
}

impl Message<SetHostname> for ControlRunner {
    type Reply = ();

    async fn handle(&mut self, msg: SetHostname, _ctx: &mut Context<Self, Self::Reply>) {
        tracing::debug!("updating hostname at control");
        self.client.set_hostname(msg.hostname).await;
    }
}

#[cfg(test)]
mod reauth_bridge_tests {
    use tokio::sync::watch;

    use super::bridge_reauth_url_to_state;
    use crate::DeviceState;

    fn url(s: &str) -> url::Url {
        s.parse().unwrap()
    }

    /// The bridge maps a surfaced re-auth URL onto `DeviceState::NeedsLogin(url)` — the fix's core:
    /// a mid-session `MachineNotAuthorized` (forwarded by the control client as `Some(url)`) becomes
    /// the "needs login" state the IPN bus turns into `browse_to_url`.
    #[test]
    fn bridge_maps_auth_url_to_needs_login() {
        let u = url("https://login.example/auth");
        let (tx, rx) = watch::channel(DeviceState::Running);

        bridge_reauth_url_to_state(&tx, Some(&u));

        assert_eq!(*rx.borrow(), DeviceState::NeedsLogin(u));
    }

    /// `None` never drives a transition — the recovery to `Running` is the netmap self-node
    /// handler's job, so the bridge ignores a `None` and leaves the state untouched.
    #[test]
    fn bridge_none_leaves_state_unchanged() {
        let (tx, rx) = watch::channel(DeviceState::Running);

        bridge_reauth_url_to_state(&tx, None);

        assert_eq!(*rx.borrow(), DeviceState::Running);
    }

    /// Re-surfacing the same URL across retries does not re-fire the watch (`send_if_modified`
    /// dedupe against the cell's current value), so a stuck re-auth does not thrash subscribers.
    #[test]
    fn bridge_same_url_does_not_refire() {
        let u = url("https://login.example/auth");
        let (tx, mut rx) = watch::channel(DeviceState::Running);

        bridge_reauth_url_to_state(&tx, Some(&u)); // first: fires
        assert!(rx.has_changed().unwrap(), "first NeedsLogin fires");
        rx.mark_unchanged();
        bridge_reauth_url_to_state(&tx, Some(&u)); // same URL: deduped
        assert!(
            !rx.has_changed().unwrap(),
            "the same re-auth URL must not re-fire the state watch"
        );
    }

    /// A genuinely different re-auth URL after a prior one fires again (the dedupe tracks changes,
    /// it does not pin the first URL forever).
    #[test]
    fn bridge_new_url_after_prior_fires() {
        let a = url("https://login.example/a");
        let b = url("https://login.example/b");
        let (tx, rx) = watch::channel(DeviceState::Running);

        bridge_reauth_url_to_state(&tx, Some(&a));
        bridge_reauth_url_to_state(&tx, Some(&b));

        assert_eq!(*rx.borrow(), DeviceState::NeedsLogin(b));
    }

    /// End-to-end of the *clear* contract: after the bridge sets `NeedsLogin`, the netmap self-node
    /// path (modeled here as a direct `send_replace(Running)`, the exact transition the
    /// `StreamMessage::Next` handler performs on the next good self-node) flips back to `Running`.
    /// This pins that the bridge does NOT need a `None`-clear arm — recovery is owned elsewhere.
    #[test]
    fn running_netmap_clears_needs_login() {
        let u = url("https://login.example/auth");
        let (tx, rx) = watch::channel(DeviceState::Running);

        bridge_reauth_url_to_state(&tx, Some(&u));
        assert_eq!(*rx.borrow(), DeviceState::NeedsLogin(u));

        // The self-node handler's recovery transition (next good netmap self-node → Running).
        tx.send_replace(DeviceState::Running);
        assert_eq!(*rx.borrow(), DeviceState::Running);
    }

    /// Fix 2 — the bridge must NOT clobber an in-flight automatic re-auth. While the cell holds
    /// `Reauthenticating`, an auth URL surfaced by the rotated re-register (over the same
    /// `auth_url_tx` cell) must leave the state UNTOUCHED: flipping to `NeedsLogin` would surface a
    /// misleading `browse_to_url` on a headless node and, by moving the cell off `Reauthenticating`,
    /// re-arm a second node-key rotation that loses the original `OldNodeKey` anchor. The auto-reauth
    /// path owns the cell until it recovers to `Running` or trips to `Expired`.
    #[test]
    fn bridge_does_not_clobber_reauthenticating() {
        let u = url("https://login.example/auth");
        let (tx, rx) = watch::channel(DeviceState::Reauthenticating);

        bridge_reauth_url_to_state(&tx, Some(&u));

        assert_eq!(
            *rx.borrow(),
            DeviceState::Reauthenticating,
            "a surfaced auth URL must not downgrade an in-flight auto-reauth to NeedsLogin"
        );
    }

    /// The no-clobber guard is scoped to `Reauthenticating` only — it does not change the bridge's
    /// behavior in any other state. From `Running` (and likewise `Connecting`/`NeedsLogin`) a
    /// surfaced URL still drives `NeedsLogin` as before, so an ordinary interactive re-auth is
    /// unaffected.
    #[test]
    fn bridge_still_sets_needs_login_from_non_reauthenticating() {
        let u = url("https://login.example/auth");
        for start in [
            DeviceState::Running,
            DeviceState::Connecting,
            DeviceState::Expired,
        ] {
            let (tx, rx) = watch::channel(start);
            bridge_reauth_url_to_state(&tx, Some(&u));
            assert_eq!(*rx.borrow(), DeviceState::NeedsLogin(u.clone()));
        }
    }
}

#[cfg(test)]
mod sticky_pop_browser_url_tests {
    use tokio::sync::watch;

    use super::sticky_update_pop_browser_url;

    fn url(s: &str) -> url::Url {
        s.parse().unwrap()
    }

    /// A non-empty URL publishes to the cell.
    #[test]
    fn non_empty_url_publishes() {
        let (tx, rx) = watch::channel(None);
        let u = url("https://login.example/consent");
        sticky_update_pop_browser_url(&tx, Some(&u));
        assert_eq!(*rx.borrow(), Some(u));
    }

    /// An absent (`None`) update — the common netmap tick — must NOT reset the cell. This is the
    /// regression guard for the thrash bug (a reset-every-tick would coalesce the URL away on the bus).
    #[test]
    fn absent_update_does_not_reset() {
        let u = url("https://login.example/consent");
        let (tx, rx) = watch::channel(Some(u.clone()));
        // Simulate many empty netmap updates.
        for _ in 0..5 {
            sticky_update_pop_browser_url(&tx, None);
        }
        assert_eq!(
            *rx.borrow(),
            Some(u),
            "empty updates must not clear the URL"
        );
    }

    /// The same URL repeated does not re-fire the watch (in-place dedupe via `send_if_modified`), so
    /// a subscriber isn't woken spuriously. Proven by the borrow not having been marked changed.
    #[test]
    fn repeated_same_url_does_not_refire() {
        let u = url("https://login.example/consent");
        let (tx, mut rx) = watch::channel(None);
        sticky_update_pop_browser_url(&tx, Some(&u)); // first: fires
        assert!(rx.has_changed().unwrap(), "first non-empty URL fires");
        rx.mark_unchanged();
        sticky_update_pop_browser_url(&tx, Some(&u)); // same: deduped
        assert!(
            !rx.has_changed().unwrap(),
            "repeating the same URL must not re-fire the watch"
        );
    }

    /// A genuinely new URL after a prior one fires again (sticky but tracks changes).
    #[test]
    fn new_url_after_prior_fires() {
        let a = url("https://login.example/a");
        let b = url("https://login.example/b");
        let (tx, rx) = watch::channel(None);
        sticky_update_pop_browser_url(&tx, Some(&a));
        sticky_update_pop_browser_url(&tx, Some(&b));
        assert_eq!(*rx.borrow(), Some(b));
    }

    /// The realistic session sequence: a URL stays sticky through a run of `None` ticks, and a
    /// *different* URL after that gap still fires. Chains the legs the other tests cover in isolation
    /// (the actual control cadence is "URL, then many empty updates, then maybe a new URL").
    #[test]
    fn sticky_through_none_gap_then_new_url_fires() {
        let a = url("https://login.example/a");
        let b = url("https://login.example/b");
        let (tx, rx) = watch::channel(None);
        sticky_update_pop_browser_url(&tx, Some(&a));
        for _ in 0..3 {
            sticky_update_pop_browser_url(&tx, None);
        }
        assert_eq!(*rx.borrow(), Some(a), "stayed sticky through the None gap");
        sticky_update_pop_browser_url(&tx, Some(&b));
        assert_eq!(
            *rx.borrow(),
            Some(b),
            "a new URL after a None gap still fires"
        );
    }

    /// Returning to a previously-seen URL (A → B → A) re-fires: the dedupe is against the cell's
    /// *current* value, not a full history, so A after B is a genuine change.
    #[test]
    fn returning_to_prior_url_refires() {
        let a = url("https://login.example/a");
        let b = url("https://login.example/b");
        let (tx, mut rx) = watch::channel(None);
        sticky_update_pop_browser_url(&tx, Some(&a));
        sticky_update_pop_browser_url(&tx, Some(&b));
        rx.mark_unchanged();
        sticky_update_pop_browser_url(&tx, Some(&a)); // back to A: differs from current (B) → fires
        assert!(
            rx.has_changed().unwrap(),
            "returning to a prior URL re-fires"
        );
        assert_eq!(*rx.borrow(), Some(a));
    }

    /// End-to-end de-thrash: feed a realistic netmap cadence (empty, empty, URL, empty, empty)
    /// through the producer into a cell, and count the changes a `run_bus`-style subscriber would
    /// observe via `changed()`. The whole point of the fix is that exactly ONE change survives the
    /// surrounding `None` thrash — the pre-fix code (`send_replace` every tick) would have woken the
    /// subscriber on every empty tick and coalesced the URL away. This exercises the producer + the
    /// watch-subscribe path together (the two halves the unit tests cover in isolation).
    #[tokio::test]
    async fn end_to_end_one_change_survives_none_thrash() {
        let u = url("https://login.example/consent");
        let (tx, mut rx) = watch::channel(None);
        // The cadence control actually sends: mostly-empty MapResponses with one carrying the URL.
        let cadence = [None, None, Some(&u), None, None];
        for incoming in cadence {
            sticky_update_pop_browser_url(&tx, incoming);
        }
        // A subscriber sees exactly one change, and it carries the URL (not a coalesced `None`).
        let mut changes = 0;
        while rx.has_changed().unwrap() {
            let v = rx.borrow_and_update().clone();
            changes += 1;
            assert_eq!(v, Some(u.clone()), "the surviving change carries the URL");
        }
        assert_eq!(changes, 1, "exactly one change survives the None thrash");
    }
}

#[cfg(test)]
mod home_region_hysteresis_tests {
    use core::time::Duration;
    use std::{collections::HashMap, sync::Arc, time::Instant};

    use ts_derp::RegionId;
    use ts_netcheck::RegionResult;

    use super::{DERP_HISTORY_MAX_AGE, best_recent, select_home_region};

    fn region(id: u32, latency_ms: u64) -> RegionResult {
        RegionResult {
            latency: Duration::from_millis(latency_ms),
            id: RegionId(core::num::NonZeroU32::new(id).unwrap()),
            latency_map_key: format!("region-{id}"),
            connected_remote: "127.0.0.1:0".parse().unwrap(),
        }
    }

    fn rid(id: u32) -> RegionId {
        RegionId(core::num::NonZeroU32::new(id).unwrap())
    }

    /// Call `select_home_region` with NO smoothing history — `best_recent` empty, so each region's
    /// smoothed latency falls back to its current sample, reproducing the original raw-vs-raw
    /// hysteresis these tests pin. (The smoothing-specific tests below pass a populated map.)
    fn sel(current: Option<RegionId>, m: &[RegionResult]) -> Option<RegionId> {
        select_home_region(current, m, &HashMap::new())
    }

    /// Empty measurements yield no selection.
    #[test]
    fn empty_measurements_select_none() {
        assert!(sel(Some(rid(1)), &[]).is_none());
        assert!(sel(None, &[]).is_none());
    }

    /// First selection (no current home region) takes the best (lowest-latency) region directly.
    #[test]
    fn first_selection_takes_best() {
        let m = [region(1, 20), region(2, 50)];
        assert_eq!(sel(None, &m).unwrap(), rid(1));
    }

    /// Jitter within the 10ms absolute-diff band keeps the current region (no flap). Current=region 2
    /// at 25ms; new best=region 1 at 20ms (only 5ms better) -> keep region 2.
    #[test]
    fn keeps_current_when_within_absolute_diff() {
        let m = [region(1, 20), region(2, 25)];
        assert_eq!(
            sel(Some(rid(2)), &m).unwrap(),
            rid(2),
            "a 5ms improvement (< 10ms) must not flap the home region"
        );
    }

    /// A meaningful improvement (>10ms AND best <= 2/3 of current) switches. Current=region 2 at
    /// 100ms; new best=region 1 at 20ms -> switch to region 1.
    #[test]
    fn switches_on_meaningful_improvement() {
        let m = [region(1, 20), region(2, 100)];
        assert_eq!(
            sel(Some(rid(2)), &m).unwrap(),
            rid(1),
            "a large improvement must switch the home region"
        );
    }

    /// The two-thirds rule: even past the 10ms absolute diff, an improvement that does not beat 2/3
    /// of the current latency keeps the current region. current=60ms, best=45ms: diff=15ms (>10ms,
    /// so the absolute test alone would switch), but 45 > 60*2/3=40, so keep.
    #[test]
    fn keeps_current_when_two_thirds_rule_not_met() {
        let m = [region(1, 45), region(2, 60)];
        assert_eq!(
            sel(Some(rid(2)), &m).unwrap(),
            rid(2),
            "best (45ms) is not <= 2/3 of current (40ms), so keep current despite >10ms diff"
        );
    }

    /// When the current home region is no longer present in the measurements, take the new best.
    #[test]
    fn switches_when_current_region_absent() {
        let m = [region(1, 20), region(3, 25)];
        assert_eq!(
            sel(Some(rid(2)), &m).unwrap(),
            rid(1),
            "a current region absent from the measurements falls through to the best"
        );
    }

    /// When the best already IS the current home region, it is kept (no spurious change).
    #[test]
    fn keeps_current_when_it_is_already_best() {
        let m = [region(2, 20), region(1, 50)];
        assert_eq!(sel(Some(rid(2)), &m).unwrap(), rid(2));
    }

    /// `best_recent` is each region's MINIMUM latency over the in-window reports; a report older than
    /// `max_age` is excluded.
    #[test]
    fn best_recent_is_min_over_window_and_evicts_aged() {
        let now = Instant::now();
        // Two in-window reports for region 1 (50ms then 20ms) → min 20ms; region 2 once at 30ms.
        // One aged report (region 1 at 5ms) outside the window must be ignored.
        let history = vec![
            (
                now - Duration::from_secs(10 * 60), // aged out (> 5min)
                Arc::new(vec![region(1, 5)]),
            ),
            (
                now - Duration::from_secs(60),
                Arc::new(vec![region(1, 50), region(2, 30)]),
            ),
            (now, Arc::new(vec![region(1, 20)])),
        ];
        let br = best_recent(&history, now, DERP_HISTORY_MAX_AGE);
        assert_eq!(
            br.get(&rid(1)).copied(),
            Some(Duration::from_millis(20)),
            "region 1 min over the window is 20ms (the aged 5ms is excluded)"
        );
        assert_eq!(br.get(&rid(2)).copied(), Some(Duration::from_millis(30)));
    }

    /// The asymmetric comparison: the new best is chosen by its SMOOTHED (best_recent) latency while
    /// the old region is compared on its RAW current latency. A best region whose CURRENT sample
    /// looks much better but whose 5-min MIN is only marginally better must NOT flap the home region
    /// — exactly the oscillation the raw-vs-raw comparison would have switched on.
    #[test]
    fn smoothed_best_damps_oscillation_across_boundary() {
        // Current home = region 2, raw 60ms this cycle. Region 1's CURRENT sample is 20ms (a >2/3,
        // >10ms improvement → raw-vs-raw would SWITCH), but its 5-min MIN (best_recent) is 50ms
        // (it oscillates). Smoothed-best 50ms vs raw-old 60ms: diff 10ms is NOT < 10ms, but
        // 50 > 60*2/3=40 → keepOld. So we KEEP region 2, where the raw comparison would have flapped.
        let m = [region(1, 20), region(2, 60)];
        let mut br = HashMap::new();
        br.insert(rid(1), Duration::from_millis(50)); // smoothed best is worse than its raw sample
        br.insert(rid(2), Duration::from_millis(60));
        assert_eq!(
            select_home_region(Some(rid(2)), &m, &br).unwrap(),
            rid(2),
            "a best region whose 5-min min is only marginally better must not flap the home region"
        );

        // Sanity: with NO smoothing (raw 20ms best), the same inputs WOULD switch — proving the
        // smoothing is what holds it.
        assert_eq!(
            select_home_region(Some(rid(2)), &m, &HashMap::new()).unwrap(),
            rid(1),
            "raw-vs-raw (no smoothing) switches on the 20ms-vs-60ms current samples"
        );
    }

    /// Smoothing can reorder which region is "best": `measurements` is sorted by raw latency, but the
    /// smoothed minimum may favor a different region. `select_home_region` must pick by smoothed
    /// latency, not blindly trust `measurements[0]`.
    #[test]
    fn smoothed_best_may_differ_from_raw_first() {
        // Raw order: region 1 (10ms) is first. But region 2's 5-min min is 5ms while region 1's is
        // 40ms (region 1's 10ms was a lucky low sample). Smoothed-best is region 2. First selection.
        let m = [region(1, 10), region(2, 12)];
        let mut br = HashMap::new();
        br.insert(rid(1), Duration::from_millis(40));
        br.insert(rid(2), Duration::from_millis(5));
        assert_eq!(
            select_home_region(None, &m, &br).unwrap(),
            rid(2),
            "the smoothed-best region wins even when it is not the raw-latency first"
        );
    }

    /// Byte-faithful integer two-thirds boundary (the float-vs-integer divergence): at exactly
    /// `best == old * 2/3` (old=36ms, best=24ms), Go's integer `bestAny > old/3*2` = `24ms > 24ms`
    /// is FALSE, so it does NOT keep on the 2/3 arm; and `cond_a` `36-24=12ms < 10ms` is also false,
    /// so Go SWITCHES. A float `0.024 > 0.036*2.0/3.0 = 0.0239999997` would wrongly KEEP. This test
    /// pins the integer math: it must switch to the best.
    #[test]
    fn two_thirds_boundary_is_integer_not_float() {
        let m = [region(1, 24), region(2, 36)];
        // No smoothing (raw == smoothed): isolates the 2/3 arithmetic at the exact boundary.
        assert_eq!(
            sel(Some(rid(2)), &m).unwrap(),
            rid(1),
            "at best == old*2/3 the integer rule does NOT keep (Go switches); a float rule would keep"
        );
    }

    /// The `cond_a` (absolute-diff) arm via `saturating_sub`: when the old region's RAW current
    /// latency is FASTER than the smoothed-best (old=20ms raw, smoothed-best=50ms), `old - best_any`
    /// underflows. Go's signed subtraction is negative (`< 10ms` → keepOld); `saturating_sub` floors
    /// to 0 (`< 10ms` → keepOld) — same outcome. The old region is kept.
    #[test]
    fn old_faster_than_smoothed_best_keeps_via_absolute_diff() {
        // Current home = region 2, raw 20ms. Region 1 is the raw-best at 15ms but its smoothed min is
        // 50ms (it oscillates badly). smoothed-best candidate by min = region 2 (raw 20 == smoothed
        // 20, since br[2]=20) vs region 1 smoothed 50 → best is region 2 itself → already-best path.
        // To exercise the old<best_any underflow we need best != old: make region 1 the smoothed best
        // at 18ms but the OLD region's raw 20ms... use: old=region2 raw 20, best=region1 smoothed 18.
        let m = [region(1, 15), region(2, 20)];
        let mut br = HashMap::new();
        br.insert(rid(1), Duration::from_millis(18)); // smoothed-best = region 1 at 18ms
        br.insert(rid(2), Duration::from_millis(25)); // region 2 smoothed worse than its raw 20ms
        // best_any = 18ms (region 1). old (region 2) RAW = 20ms. 20 - 18 = 2ms < 10ms → keepOld.
        assert_eq!(
            select_home_region(Some(rid(2)), &m, &br).unwrap(),
            rid(2),
            "old raw (20ms) within 10ms of smoothed-best (18ms) keeps via the absolute-diff arm"
        );
    }
}

#[cfg(test)]
mod expiry_action_tests {
    use super::{ExpiryAction, expiry_action};

    /// Not expired → `Running`, regardless of the other inputs (the gate fields are only consulted
    /// once the key is expired).
    #[test]
    fn not_expired_is_always_running() {
        for has_auth_key in [false, true] {
            for reauth_enabled in [false, true] {
                for tka_active in [false, true] {
                    assert_eq!(
                        expiry_action(false, has_auth_key, reauth_enabled, tka_active),
                        ExpiryAction::Running,
                        "a non-expired key is Running for any gate combination"
                    );
                }
            }
        }
    }

    /// The ONLY input combination that auto-reauths: expired AND auth key retained AND reauth enabled
    /// AND TKA not enforcing.
    #[test]
    fn expired_with_authkey_reauth_enabled_and_no_tka_reauthenticates() {
        assert_eq!(
            expiry_action(true, true, true, false),
            ExpiryAction::Reauthenticate
        );
    }

    /// Every other expired combination falls back to the terminal `Expired` (today's behavior, no
    /// regression): no auth key, reauth disabled, or TKA enforcing each independently forces Expired.
    #[test]
    fn expired_falls_back_to_expired_for_every_other_combination() {
        // The full expired-input matrix minus the single Reauthenticate cell above.
        for has_auth_key in [false, true] {
            for reauth_enabled in [false, true] {
                for tka_active in [false, true] {
                    let action = expiry_action(true, has_auth_key, reauth_enabled, tka_active);
                    if has_auth_key && reauth_enabled && !tka_active {
                        // The one Reauthenticate cell, asserted above.
                        assert_eq!(action, ExpiryAction::Reauthenticate);
                    } else {
                        assert_eq!(
                            action,
                            ExpiryAction::Expired,
                            "expired with has_auth_key={has_auth_key}, \
                             reauth_enabled={reauth_enabled}, tka_active={tka_active} must be Expired"
                        );
                    }
                }
            }
        }
    }

    /// The TKA safety gate in isolation: even with an auth key and reauth enabled, an ACTIVE lock
    /// forces `Expired` (never rotate an unsigned key on a locked tailnet). This is the hard
    /// constraint from the design — pinned as its own test so a regression that drops the `!tka_active`
    /// term is caught explicitly.
    #[test]
    fn tka_active_forces_expired_even_when_reauth_would_otherwise_fire() {
        assert_eq!(
            expiry_action(true, true, true, true),
            ExpiryAction::Expired,
            "an enforcing Tailnet Lock must veto auto-reauth (unsigned-key lockout safety gate)"
        );
    }

    /// No auth key forces `Expired`: there is nothing to non-interactively re-register with, so even
    /// with reauth enabled and no lock the node goes terminal (unchanged from today).
    #[test]
    fn no_auth_key_forces_expired() {
        assert_eq!(
            expiry_action(true, false, true, false),
            ExpiryAction::Expired
        );
    }

    /// The config opt-out: `reauth_on_expiry=false` forces `Expired` even with an auth key and no
    /// lock (the conservative posture / historical behavior).
    #[test]
    fn reauth_disabled_forces_expired() {
        assert_eq!(
            expiry_action(true, true, false, false),
            ExpiryAction::Expired
        );
    }
}

#[cfg(test)]
mod reauth_circuit_tests {
    use super::{ExpiryAction, MAX_REAUTH_ATTEMPTS, ReauthState, reauth_circuit_step};

    /// Entering reauth (a `Reauthenticate` verdict while NOT already reauthenticating) fires the
    /// one-shot reauth and starts the counter at 1.
    #[test]
    fn enter_reauth_fires_once_and_starts_counter() {
        let step = reauth_circuit_step(ExpiryAction::Reauthenticate, false, 0);
        assert_eq!(step.next, ReauthState::Reauthenticating);
        assert_eq!(
            step.attempts, 1,
            "the entering attempt starts the counter at 1"
        );
        assert!(step.fire_reauth, "entry fires the one-shot Command::Reauth");
    }

    /// A subsequent expired self-node while ALREADY reauthenticating (prior reauth not recovered)
    /// counts up but does NOT re-fire — re-firing would rotate the node key a second time and lose the
    /// original `OldNodeKey` anchor.
    #[test]
    fn still_reauthing_counts_but_does_not_refire() {
        let step = reauth_circuit_step(ExpiryAction::Reauthenticate, true, 1);
        assert_eq!(step.next, ReauthState::Reauthenticating);
        assert_eq!(
            step.attempts, 2,
            "a non-recovering reauth increments the counter"
        );
        assert!(
            !step.fire_reauth,
            "must NOT re-fire reauth while already reauthenticating (no second rotation)"
        );
    }

    /// At `MAX_REAUTH_ATTEMPTS` consecutive non-recovering reauths, the breaker trips to the terminal
    /// `Expired` and stops re-arming reauth (the one-shot auth-key case settles instead of looping).
    #[test]
    fn trips_to_expired_at_cap() {
        // One step below the cap still stays reauthenticating.
        let below =
            reauth_circuit_step(ExpiryAction::Reauthenticate, true, MAX_REAUTH_ATTEMPTS - 2);
        assert_eq!(below.next, ReauthState::Reauthenticating);
        assert!(!below.fire_reauth);

        // The step that reaches the cap flips to terminal Expired and does not fire.
        let at_cap =
            reauth_circuit_step(ExpiryAction::Reauthenticate, true, MAX_REAUTH_ATTEMPTS - 1);
        assert_eq!(at_cap.attempts, MAX_REAUTH_ATTEMPTS);
        assert_eq!(
            at_cap.next,
            ReauthState::Expired,
            "at the cap the circuit breaker trips to terminal Expired"
        );
        assert!(
            !at_cap.fire_reauth,
            "a tripped breaker never fires another reauth"
        );
    }

    /// A good (non-expired) self-node resets the counter to 0 (the node recovered) — so a later,
    /// genuine expiry cycle gets a fresh full budget of attempts, never a stale leftover count.
    #[test]
    fn running_resets_counter() {
        let step = reauth_circuit_step(ExpiryAction::Running, true, MAX_REAUTH_ATTEMPTS);
        assert_eq!(step.next, ReauthState::Running);
        assert_eq!(
            step.attempts, 0,
            "recovery to Running resets the attempt counter"
        );
        assert!(!step.fire_reauth);
    }

    /// The non-reauth terminal path (`expiry_action` → `Expired`: no auth key / reauth disabled /
    /// TKA-locked) reports `Expired` and never fires reauth; the counter is irrelevant (this path
    /// never armed the machine), so it resets to 0.
    #[test]
    fn expired_action_is_terminal_without_firing() {
        let step = reauth_circuit_step(ExpiryAction::Expired, false, 0);
        assert_eq!(step.next, ReauthState::Expired);
        assert!(!step.fire_reauth);
        assert_eq!(step.attempts, 0);
    }

    /// The full episode as the handler drives it, feeding each step's `attempts` into the next: a
    /// one-shot auth key that never recovers fires reauth exactly ONCE, counts each repeated expired
    /// self-node, and settles on terminal `Expired` at the cap — never an indefinite `Reauthenticating`
    /// spell and never a second rotation.
    #[test]
    fn full_episode_one_shot_key_settles_at_expired_after_one_fire() {
        // Step 1: first expired self-node (state was Running → not already reauthing): enter + fire.
        let s1 = reauth_circuit_step(ExpiryAction::Reauthenticate, false, 0);
        assert_eq!(s1.next, ReauthState::Reauthenticating);
        assert!(s1.fire_reauth);
        let mut attempts = s1.attempts;
        let mut fires = 1; // counted the one fire

        // Steps 2..: still expired while reauthenticating — count only, never re-fire — until the cap.
        loop {
            let s = reauth_circuit_step(ExpiryAction::Reauthenticate, true, attempts);
            if s.fire_reauth {
                fires += 1;
            }
            attempts = s.attempts;
            if s.next == ReauthState::Expired {
                break;
            }
            assert_eq!(s.next, ReauthState::Reauthenticating);
            assert!(attempts < MAX_REAUTH_ATTEMPTS);
        }

        assert_eq!(attempts, MAX_REAUTH_ATTEMPTS, "settles exactly at the cap");
        assert_eq!(
            fires, 1,
            "reauth (and thus a node-key rotation) fires exactly once across the whole episode"
        );
    }

    /// Recovery then a fresh failing episode: `Reauthenticating → Running` (reset) → a later expiry
    /// re-enters and re-fires. Proves the reset gives the next episode a full attempt budget rather
    /// than carrying a stale count that would trip the breaker early.
    #[test]
    fn recovery_then_new_episode_re_fires() {
        // Episode 1 entry.
        let e1 = reauth_circuit_step(ExpiryAction::Reauthenticate, false, 0);
        assert!(e1.fire_reauth);
        // A non-recovering step, then recovery to Running resets.
        let mid = reauth_circuit_step(ExpiryAction::Reauthenticate, true, e1.attempts);
        assert_eq!(mid.attempts, 2);
        let recovered = reauth_circuit_step(ExpiryAction::Running, true, mid.attempts);
        assert_eq!(recovered.attempts, 0);

        // Episode 2: a fresh expiry (state is Running again → not already reauthing) re-enters + fires.
        let e2 = reauth_circuit_step(ExpiryAction::Reauthenticate, false, recovered.attempts);
        assert_eq!(e2.next, ReauthState::Reauthenticating);
        assert_eq!(e2.attempts, 1, "the new episode starts fresh at 1");
        assert!(
            e2.fire_reauth,
            "a new episode after recovery fires reauth again"
        );
    }
}

#[cfg(test)]
mod self_lockout_tests {
    use ts_tka::{AumHash, Authority, State};

    use super::{SelfLockVerdict, self_lock_verdict};

    fn node_key() -> ts_keys::NodePublicKey {
        ts_keys::NodePrivateKey::random().public_key()
    }

    /// An empty key-signature is the "not signed yet" case: `Unsigned`, never a lockout warning —
    /// so a tailnet that simply has not signed this node does not spam a `warn`.
    #[test]
    fn empty_signature_is_unsigned_not_locked_out() {
        let authority = Authority::from_state(AumHash([0; 32]), State::default());
        assert_eq!(
            self_lock_verdict(&node_key(), &[], &authority),
            SelfLockVerdict::Unsigned
        );
    }

    /// A non-empty key-signature that does not authorize self classifies as `LockedOut` — the
    /// operator-facing condition — and the verdict carries the verify error string for the log. Here
    /// the blob is non-empty (so we attempt verification rather than short-circuiting to `Unsigned`)
    /// but is not a valid NodeKeySignature CBOR (`0x01` decodes as a bare uint with trailing bytes),
    /// so `node_key_authorized` returns a `Decode` error → `LockedOut`. The cryptographic-rejection
    /// arms (`UntrustedKey` / `BadSignature` for a well-formed-but-untrusted NKS) are covered by
    /// `ts_tka`'s own `node_key_authorized` tests; this only needs to prove the runtime classifier
    /// routes a verify `Err` to `LockedOut`.
    #[test]
    fn unverifiable_signature_is_locked_out() {
        let authority = Authority::from_state(AumHash([0; 32]), State::default());
        let verdict = self_lock_verdict(&node_key(), &[0x01, 0x02, 0x03], &authority);
        assert!(
            matches!(verdict, SelfLockVerdict::LockedOut(_)),
            "a signature the lock cannot authorize must classify as LockedOut, got {verdict:?}"
        );
    }
}

#[cfg(test)]
mod cached_replay_tests {
    //! The cold-start replay of a netmap cached under Tailnet Lock: which of its peers this node
    //! dials before control has answered.
    //!
    //! Go replays its cached map through `setNetMapLocked`, so `tkaFilterNetmapLocked`
    //! (`ipn/ipnlocal/tailnet-lock.go`, upstream `9ea7cba44591e0cd840c6c94d23274dd222059bf`) runs
    //! over it against the authority it re-opened from disk, and the peers holding a valid signature
    //! survive. These cover the same outcome here, over a chain persisted beside the netmap.

    use ed25519_dalek::SigningKey;
    use ts_tka::{Aum, AumHash, AumKey, Authority, KeyKind, MemAumStore, NodeKeySignature};

    use super::{TkaStatus, vouch_cached_peers};
    use crate::peer_tracker::tka_tests::peer_node;

    /// The node key of the peer the lock authorizes in these tests.
    const SIGNED_PEER_KEY: [u8; 32] = [9u8; 32];
    /// The node key of the peer that presents nothing.
    const UNSIGNED_PEER_KEY: [u8; 32] = [10u8; 32];

    /// A locked tailnet: a genesis checkpoint trusting `signer` (signed by it, so the chain verifies),
    /// plus the store and the [`Authority`] a completed sync would hold.
    fn locked_tailnet(signer: &SigningKey) -> (MemAumStore, AumHash, Authority) {
        let key = AumKey {
            kind: KeyKind::Ed25519,
            votes: 1,
            public: signer.verifying_key().to_bytes().to_vec(),
            meta: Vec::new(),
        };
        let mut genesis = Aum::new_genesis_checkpoint(vec![key], vec![vec![0x11; 32]])
            .expect("a well-formed genesis checkpoint");
        genesis.sign(signer);
        let oldest = genesis.hash();
        let store = MemAumStore::from_aums([genesis]);
        let authority = crate::tka_sync::authority_from_encoded_chain(
            &crate::tka_sync::encode_chain(&store, oldest).expect("encode"),
        )
        .expect("the chain verifies");
        (store, oldest, authority)
    }

    /// The status control stamped on the netmap that was cached under `authority`.
    fn cached_lock(authority: &Authority) -> TkaStatus {
        TkaStatus {
            head: authority.head().to_base32(),
            disabled: false,
        }
    }

    /// The headline: with the authority those peers were cached under, the cold start replays the
    /// peer that authority authorizes and drops the one it does not.
    ///
    /// Without the persisted authority this replays **nothing** — which is what made a locked
    /// tailnet lose the cache entirely, while Go kept dialing its authorized peers.
    #[test]
    fn a_persisted_authority_replays_the_peers_it_authorizes() {
        let signer = SigningKey::from_bytes(&[42u8; 32]);
        let (_store, _oldest, authority) = locked_tailnet(&signer);

        let signed = peer_node(
            "signed-peer",
            SIGNED_PEER_KEY,
            NodeKeySignature::sign_direct(&SIGNED_PEER_KEY, &signer).serialize(),
        );
        let unsigned = peer_node("unsigned-peer", UNSIGNED_PEER_KEY, Vec::new());

        let replayed = vouch_cached_peers(
            Some(&authority),
            &cached_lock(&authority),
            vec![signed, unsigned],
        );

        assert_eq!(
            replayed
                .iter()
                .map(|p| p.stable_id.0.as_str())
                .collect::<Vec<_>>(),
            vec!["signed-peer"],
            "the peer the lock authorizes is dialed at cold start; the unsigned one is dropped"
        );
    }

    /// No persisted authority (this node has never completed a sync, or the chain did not verify) ⇒
    /// no peers. There is nothing to check a signature against, and a peer the lock revoked while
    /// this node was off must not be dialed on the strength of a cache entry.
    #[test]
    fn without_an_authority_no_cached_peer_is_replayed() {
        let signer = SigningKey::from_bytes(&[42u8; 32]);
        let (_store, _oldest, authority) = locked_tailnet(&signer);
        let signed = peer_node(
            "signed-peer",
            SIGNED_PEER_KEY,
            NodeKeySignature::sign_direct(&SIGNED_PEER_KEY, &signer).serialize(),
        );

        assert!(
            vouch_cached_peers(None, &cached_lock(&authority), vec![signed]).is_empty(),
            "a peer nothing can vouch for waits for control's first netmap"
        );
    }

    /// A netmap cached on disk (raw `MapResponse` JSON, exactly what `NetmapCache` persists), in a
    /// directory private to this user so the cache's own vetting accepts it.
    #[cfg(unix)]
    fn cache_dir_with(label: &str, tka_info: &str, peers: &str) -> std::path::PathBuf {
        use std::os::unix::fs::{OpenOptionsExt as _, PermissionsExt as _};

        let dir = std::env::temp_dir().join(format!(
            "ts-rs-cached-replay-{}-{label}",
            std::process::id()
        ));
        std::fs::remove_dir_all(&dir).ok();
        std::fs::create_dir_all(&dir).expect("scratch dir");
        std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).expect("chmod");

        let body = format!(
            r#"{{
                {tka_info}
                "Node": {{
                    "ID": 1,
                    "StableID": "self-1",
                    "Name": "self.example.ts.net.",
                    "Addresses": ["100.64.0.1/32"],
                    "CapMap": {{"cache-network-maps": null}}
                }},
                "Peers": [{peers}],
                "DERPMap": {{ "Regions": {{ "3": {{
                    "RegionID": 3, "RegionCode": "tst", "RegionName": "Test", "Nodes": []
                }} }} }}
            }}"#
        );
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(dir.join(ts_control::NETMAP_CACHE_FILE))
            .expect("cache file");
        std::io::Write::write_all(&mut file, body.as_bytes()).expect("write cached netmap");
        dir
    }

    /// One cached peer, with no `KeySignature` — an unsigned peer, which a lock drops.
    #[cfg(unix)]
    const UNSIGNED_CACHED_PEER: &str = r#"{
        "ID": 2,
        "StableID": "peer-2",
        "Name": "peer.example.ts.net.",
        "Addresses": ["100.64.0.2/32"],
        "Endpoints": ["192.0.2.7:41641"],
        "HomeDERP": 3
    }"#;

    /// End to end over the files a cold start actually finds: the persisted chain is read from the
    /// cache directory, re-verified, and used to judge the cached peers — an unsigned one is dropped
    /// while the rest of the netmap replays.
    ///
    /// The *admit* side is proven on [`vouch_cached_peers`] directly rather than here: a cached
    /// frame's `KeySignature` decodes to the raw bytes of its JSON string, so a real signature (64
    /// arbitrary bytes inside a CBOR structure) cannot be written into a JSON fixture at all.
    #[cfg(unix)]
    #[tokio::test]
    async fn a_cold_start_judges_the_cached_peers_against_the_persisted_chain() {
        let signer = SigningKey::from_bytes(&[42u8; 32]);
        let (store, oldest, authority) = locked_tailnet(&signer);
        let dir = cache_dir_with(
            "with-chain",
            &format!(
                r#""TKAInfo": {{ "Head": "{}", "Disabled": false }},"#,
                authority.head().to_base32()
            ),
            UNSIGNED_CACHED_PEER,
        );
        let cache = ts_control::NetmapCache::new(&dir);
        cache
            .store_tka_chain(&crate::tka_sync::encode_chain(&store, oldest).expect("encode"))
            .await;

        let replayed = super::load_cached_netmap(&cache)
            .await
            .expect("the cached netmap replays");

        assert!(
            replayed.peer_update.is_none(),
            "an unsigned peer is dropped by the same filter the live netmap runs, got {:?}",
            replayed.peer_update
        );
        assert!(
            replayed.node.is_some() && replayed.derp.is_some(),
            "the rest of the netmap carries no peer identity and still replays"
        );

        std::fs::remove_dir_all(&dir).ok();
    }

    /// A netmap cached while the lock was **off** replays whole: there is no authority to consult and
    /// nothing to enforce (Go's filter returns early on `b.tka == nil`).
    #[cfg(unix)]
    #[tokio::test]
    async fn a_cold_start_without_a_lock_replays_every_cached_peer() {
        let dir = cache_dir_with("no-lock", "", UNSIGNED_CACHED_PEER);

        let replayed = super::load_cached_netmap(&ts_control::NetmapCache::new(&dir))
            .await
            .expect("the cached netmap replays");

        assert!(
            matches!(replayed.peer_update, Some(ts_control::PeerUpdate::Full(ref p)) if p.len() == 1),
            "an unlocked tailnet replays its cached peers untouched: {:?}",
            replayed.peer_update
        );

        std::fs::remove_dir_all(&dir).ok();
    }

    /// A chain that does not verify is no authority at all, so the cached peers are withheld — the
    /// same outcome as never having persisted one. The netmap around them still replays.
    #[cfg(unix)]
    #[tokio::test]
    async fn a_cold_start_withholds_peers_when_the_persisted_chain_does_not_verify() {
        let signer = SigningKey::from_bytes(&[42u8; 32]);
        let (_store, _oldest, authority) = locked_tailnet(&signer);
        let tka_info = format!(
            r#""TKAInfo": {{ "Head": "{}", "Disabled": false }},"#,
            authority.head().to_base32()
        );

        // No chain at all.
        let dir = cache_dir_with("no-chain", &tka_info, UNSIGNED_CACHED_PEER);
        let replayed = super::load_cached_netmap(&ts_control::NetmapCache::new(&dir))
            .await
            .expect("the cached netmap replays");
        assert!(replayed.peer_update.is_none());
        assert!(replayed.node.is_some());
        std::fs::remove_dir_all(&dir).ok();

        // A chain blob that is not a verified chain.
        let dir = cache_dir_with("bad-chain", &tka_info, UNSIGNED_CACHED_PEER);
        let cache = ts_control::NetmapCache::new(&dir);
        cache.store_tka_chain(b"ts-tka-chain-v1\nAQID").await;
        let replayed = super::load_cached_netmap(&cache)
            .await
            .expect("the cached netmap replays");
        assert!(replayed.peer_update.is_none());
        assert!(replayed.node.is_some());
        std::fs::remove_dir_all(&dir).ok();
    }

    /// A chain that is not at the head the cached frame recorded describes a different lock — a
    /// disable-and-re-enable under a fresh genesis, or a crash between writing the netmap and syncing
    /// the chain that followed it. It vouches for nothing.
    #[test]
    fn a_chain_at_another_head_vouches_for_nothing() {
        let signer = SigningKey::from_bytes(&[42u8; 32]);
        let (_store, _oldest, authority) = locked_tailnet(&signer);
        let signed = peer_node(
            "signed-peer",
            SIGNED_PEER_KEY,
            NodeKeySignature::sign_direct(&SIGNED_PEER_KEY, &signer).serialize(),
        );

        // Another lock's head: valid base32, not ours.
        let other = TkaStatus {
            head: AumHash([0x5a; 32]).to_base32(),
            disabled: false,
        };
        assert!(vouch_cached_peers(Some(&authority), &other, vec![signed.clone()]).is_empty());

        // A head control did not send, or one this node cannot parse, is not a match either.
        for head in ["", "not base32"] {
            let malformed = TkaStatus {
                head: head.to_string(),
                disabled: false,
            };
            assert!(
                vouch_cached_peers(Some(&authority), &malformed, vec![signed.clone()]).is_empty(),
                "a head that does not parse must not be treated as matching ({head:?})"
            );
        }
    }
}

#[cfg(test)]
mod persisted_tka_chain_tests {
    //! The lifecycle of the Tailnet-Lock chain persisted for the cold-start replay: when it is
    //! removed, and that a removal can never outlive the write that follows it.

    use super::PersistedTkaChain;

    /// A cache directory this test owns, not yet created — [`PersistedTkaChain::store`] creates it
    /// private to this user, which is the same path the runtime takes.
    fn scratch_dir(label: &str) -> std::path::PathBuf {
        let dir =
            std::env::temp_dir().join(format!("ts-rs-tka-chain-{}-{label}", std::process::id()));
        std::fs::remove_dir_all(&dir).ok();
        dir
    }

    /// An opaque chain blob. The cache stores and returns bytes; only the runtime decodes them, and
    /// nothing in this module does.
    const CHAIN: &[u8] = b"ts-tka-chain-v1\nAQID";

    /// A process that has synced nothing still clears a chain an **earlier** process left behind.
    ///
    /// This is the state a cold start is in: `tka_synced` is `None` because this process has not
    /// spoken to control yet, while the chain the last run persisted is sitting in the cache
    /// directory — and that chain is exactly the one the *next* cold start would load. Gating the
    /// removal on live synced state (as the lock-off paths once did) leaves it there for good on a
    /// node whose lock was turned off between runs.
    #[tokio::test]
    async fn a_chain_an_earlier_process_left_is_cleared_by_one_that_synced_nothing() {
        let dir = scratch_dir("stale");
        let cache = ts_control::NetmapCache::new(&dir);
        cache.store_tka_chain(CHAIN).await;
        assert!(
            cache.load_tka_chain().await.is_some(),
            "the earlier process's chain is on disk"
        );

        // Freshly constructed, exactly as `on_start` builds it: no sync has happened.
        let mut chain = PersistedTkaChain::new(Some(&dir));
        chain.discard().await;

        assert!(
            cache.load_tka_chain().await.is_none(),
            "a lock control reports off clears the chain whether or not this process enforced it"
        );

        std::fs::remove_dir_all(&dir).ok();
    }

    /// A removal is complete when it returns, so a chain synced *after* it survives.
    ///
    /// The removal used to be detached (`tokio::spawn`), so a lock disable could leave one queued
    /// while a later sync persisted a fresh chain, and the queued removal would then delete it. The
    /// yields below are where such a task would get to run.
    #[tokio::test]
    async fn a_removal_never_outlives_the_chain_synced_after_it() {
        let dir = scratch_dir("ordering");
        let cache = ts_control::NetmapCache::new(&dir);
        let mut chain = PersistedTkaChain::new(Some(&dir));

        chain.store(CHAIN).await;
        chain.discard().await;
        assert!(
            cache.load_tka_chain().await.is_none(),
            "the removal has happened by the time it returns; nothing is left pending"
        );

        // The lock comes back on and a sync persists a new chain.
        let resynced = b"ts-tka-chain-v1\nBBBB".as_slice();
        chain.store(resynced).await;
        for _ in 0..8 {
            tokio::task::yield_now().await;
        }

        assert_eq!(
            cache.load_tka_chain().await.as_deref(),
            Some(resynced),
            "the freshly synced chain is what the next cold start finds"
        );

        std::fs::remove_dir_all(&dir).ok();
    }

    /// Repeated lock-off netmaps stay cheap: the removal is idempotent and the chain stays gone.
    #[tokio::test]
    async fn repeating_the_removal_is_a_no_op() {
        let dir = scratch_dir("idempotent");
        let cache = ts_control::NetmapCache::new(&dir);
        let mut chain = PersistedTkaChain::new(Some(&dir));

        chain.store(CHAIN).await;
        for _ in 0..3 {
            chain.discard().await;
            assert!(cache.load_tka_chain().await.is_none());
        }

        std::fs::remove_dir_all(&dir).ok();
    }

    /// An embedder that configured no netmap cache directory writes nothing and removes nothing —
    /// there is no cache, so there is never a chain to vouch with.
    #[tokio::test]
    async fn no_cache_directory_means_no_chain_at_all() {
        let mut chain = PersistedTkaChain::new(None);
        chain.store(CHAIN).await;
        chain.discard().await;
    }
}