eidetic-engine 0.15.1

Durable, local-first, explainable memory for coding agents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
//! Accepted-side responder broker for authenticated mesh sessions.
//!
//! This T2.2 production path (`bd-tc-epic-qzk7o.3.3`) owns the complete
//! LocalAPI-reported Tailscale address set on one port, revalidates and rebinds
//! it, verifies every kernel-observed peer with WhoIs, resolves peer identity,
//! consent generation, and pair-key generation from durable local stores, and
//! hands the socket to T2.1's public source-coupled session acceptor.
//!
//! Unsigned bootstrap hello is answered on the same listener before
//! session_open. After hello or an authenticated session, the broker serves
//! one `ee.mesh.sync_round.v1` event-header batch from the origin store.
//! A same-user control channel lets another local workspace register or
//! unregister exact team routes without binding a second TCP port: Unix
//! UDS on Unix, loopback TCP plus an owner-only endpoint file on Windows.
//! Durable lifecycle audit persistence and application dispatch remain
//! later T2.2 slices.

use std::collections::{BTreeMap, VecDeque};
use std::fmt;
#[cfg(any(unix, windows))]
use std::fs;
use std::future::Future;
use std::io;
#[cfg(any(unix, windows))]
use std::io::{Read, Write};
use std::net::Shutdown;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
#[cfg(unix)]
use std::os::unix::fs::{DirBuilderExt, FileTypeExt, MetadataExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use asupersync::Cx;
use asupersync::io::{AsyncReadExt, AsyncWriteExt};
#[cfg(unix)]
use asupersync::net::unix::{UnixListener, UnixStream};
use asupersync::net::{TcpListener, TcpStream};
use asupersync::time::BudgetTimeExt;
use asupersync::time::{sleep as asupersync_sleep, timeout, wall_now};
use serde::{Deserialize, Serialize};

use crate::config::{EnvVar, parse_env_bool_flag, read_env_var};
use crate::db::{
    DatabaseLocation, DbConnection, MeshPeerTransportIdentityError,
    ObserveMeshPeerTransportIdentityInput,
};
use crate::mesh::bootstrap_envelope::{
    BODY_FETCH_REQUEST_SCHEMA_V1, BODY_FETCH_RESPONSE_SCHEMA_V1, BOOTSTRAP_DECLINE_SCHEMA_V1,
    BOOTSTRAP_MAX_ENVELOPE_BYTES, BodyFetchRequest, BodyFetchResponse, BootstrapAdmission,
    BootstrapCapability, BootstrapDeclineV1, SYNC_ROUND_SCHEMA_V1, SyncRoundEvent,
    SyncRoundResponse, SyncRoundTip, decode_envelope, encode_envelope, parse_sync_round_request,
};
use crate::mesh::discovery_policy::{DiscoveryMode, EE_MESH_SERVICE_TAG, load_workspace_lists};
use crate::mesh::hello::{
    HelloOutcome, ResponderContext, decide_hello_response, parse_hello_request,
};
pub use crate::mesh::key_store::MESH_KEY_STORE_UNAVAILABLE_CODE;
use crate::mesh::key_store::{KeyStoreError, MeshKeyStore, PairKeyClass};
use crate::mesh::peer::{MeshPeerRecord, MeshPeerState};
use crate::mesh::transport_session::{
    AcceptedSessionConfig, AcceptedSourceAttestation, AuthenticatedTransportSession,
    FrameCapability, HandshakeObservations, ResolvedAcceptedRoute, ResponderExpectations,
    SessionBinding, SessionCapabilities, SessionChannelError, SessionChannelLimits, SessionMessage,
    UntrustedRouteSelectors, accept_authenticated_session_with_open_bytes,
};

pub const RESPONDER_BROKER_STATUS_SCHEMA_V1: &str = "ee.mesh.responder_broker.status.v1";
pub const RESPONDER_BROKER_AUDIT_SCHEMA_V1: &str = "ee.mesh.responder_broker.audit.v1";
pub const MESH_RESPONDER_ROUTE_UNAVAILABLE_CODE: &str = "mesh_responder_route_unavailable";
pub const MESH_BOOTSTRAP_IDENTITY_UNVERIFIED_CODE: &str = "mesh_bootstrap_identity_unverified";
pub const MESH_RESPONDER_PORT_CONFLICT_CODE: &str = "mesh_responder_port_conflict";
pub const MESH_RESPONDER_IDENTITY_UPGRADE_REQUIRED_CODE: &str =
    "mesh_responder_identity_upgrade_required";
pub const RESPONDER_CONTROL_SCHEMA_V1: &str = "ee.mesh.responder_control.v1";
pub const RESPONDER_CONTROL_MAX_BYTES: usize = 8 * 1024;
const CONTROL_NONCE_HEX_LEN: usize = 16;

#[cfg(unix)]
const LOCAL_API_MAX_RESPONSE_BYTES: usize = 64 * 1024;
#[cfg(unix)]
const LOCAL_API_MAX_HEADER_BYTES: usize = 16 * 1024;
const MAX_RECENT_BROKER_AUDIT_EVENTS: usize = 128;
const MIN_OWNER_REVALIDATE_INTERVAL: Duration = Duration::from_millis(100);
const MAX_OWNER_REVALIDATE_INTERVAL: Duration = Duration::from_secs(60);

pub type TailscaleLocalApiFuture<'a, T> =
    Pin<Box<dyn Future<Output = Result<T, ResponderBrokerError>> + 'a>>;

/// Minimal verified local identity needed to validate one listener bind.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LocalTailscaleIdentity {
    pub stable_id: String,
    pub current_node_pubkey: String,
    pub tailnet_id: String,
}

/// Authoritative LocalAPI status snapshot used for full-address-set binding.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LocalTailscaleStatus {
    pub identity: LocalTailscaleIdentity,
    pub addresses: Vec<IpAddr>,
}

/// Minimal accepted-peer identity returned by LocalAPI WhoIs.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WhoIsIdentity {
    pub stable_id: String,
    pub current_node_pubkey: String,
    pub login_name: Option<String>,
    pub display_name: Option<String>,
    pub user_id: Option<String>,
}

/// Narrow conformance seam around the two read-only LocalAPI calls the broker
/// needs. Production uses [`TailscaleLocalApiClient`]; tests may host a fake
/// LocalAPI socket while exercising the same broker path.
pub trait TailscaleLocalApi: Send + Sync {
    fn local_status<'a>(
        &'a self,
        _cx: &'a Cx,
    ) -> TailscaleLocalApiFuture<'a, LocalTailscaleStatus> {
        Box::pin(async { Err(ResponderBrokerError::InvalidConfiguration) })
    }

    fn verify_local_address<'a>(
        &'a self,
        cx: &'a Cx,
        address: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, LocalTailscaleIdentity>;

    fn who_is<'a>(
        &'a self,
        cx: &'a Cx,
        source: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, WhoIsIdentity>;

    /// Team-join TCP may bind loopback when tailscaled is not present.
    fn allows_loopback_bind(&self) -> bool {
        false
    }
}

impl<T: TailscaleLocalApi + ?Sized> TailscaleLocalApi for Arc<T> {
    fn local_status<'a>(&'a self, cx: &'a Cx) -> TailscaleLocalApiFuture<'a, LocalTailscaleStatus> {
        (**self).local_status(cx)
    }

    fn verify_local_address<'a>(
        &'a self,
        cx: &'a Cx,
        address: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, LocalTailscaleIdentity> {
        (**self).verify_local_address(cx, address)
    }

    fn who_is<'a>(
        &'a self,
        cx: &'a Cx,
        source: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, WhoIsIdentity> {
        (**self).who_is(cx, source)
    }

    fn allows_loopback_bind(&self) -> bool {
        (**self).allows_loopback_bind()
    }
}

/// Real Tailscale LocalAPI client over tailscaled's Unix-domain socket.
#[derive(Clone, Debug)]
pub struct TailscaleLocalApiClient {
    socket_path: PathBuf,
    io_timeout: Duration,
}

impl TailscaleLocalApiClient {
    #[must_use]
    pub fn new(socket_path: impl Into<PathBuf>, io_timeout: Duration) -> Self {
        Self {
            socket_path: socket_path.into(),
            io_timeout,
        }
    }

    #[must_use]
    pub fn socket_path(&self) -> &Path {
        &self.socket_path
    }

    /// Select the first real tailscaled LocalAPI Unix socket present on this
    /// host. No CLI fallback is used for responder authority.
    #[must_use]
    pub fn discover(io_timeout: Duration) -> Option<Self> {
        let mut candidates = vec![
            PathBuf::from("/var/run/tailscale/tailscaled.sock"),
            PathBuf::from("/run/tailscale/tailscaled.sock"),
            PathBuf::from("/var/run/tailscaled.socket"),
        ];
        if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
            candidates.push(
                home.join("Library/Containers/io.tailscale.ipn.macsys/Data/IPN/tailscaled.sock"),
            );
            candidates.push(
                home.join(
                    "Library/Group Containers/io.tailscale.ipn.macos/Data/IPN/tailscaled.sock",
                ),
            );
        }
        candidates
            .into_iter()
            .find(|candidate| local_api_socket_exists(candidate))
            .map(|socket_path| Self::new(socket_path, io_timeout))
    }

    #[cfg(unix)]
    async fn request_json(&self, cx: &Cx, endpoint: &str) -> Result<Vec<u8>, ResponderBrokerError> {
        checkpoint(cx, "tailscale localapi")?;
        if self.io_timeout.is_zero()
            || !endpoint.starts_with("/localapi/v0/")
            || endpoint.bytes().any(|byte| byte == b'\r' || byte == b'\n')
        {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let mut stream =
            await_local_api_io(cx, self.io_timeout, UnixStream::connect(&self.socket_path)).await?;
        let request = format!(
            "GET {endpoint} HTTP/1.1\r\nHost: local-tailscaled.sock\r\nConnection: close\r\n\r\n"
        );
        await_local_api_io(
            cx,
            self.io_timeout,
            AsyncWriteExt::write_all(&mut stream, request.as_bytes()),
        )
        .await?;
        await_local_api_io(cx, self.io_timeout, AsyncWriteExt::shutdown(&mut stream)).await?;

        let mut response = Vec::new();
        let mut chunk = [0_u8; 4096];
        loop {
            let read = await_local_api_io(
                cx,
                self.io_timeout,
                AsyncReadExt::read(&mut stream, &mut chunk),
            )
            .await?;
            if read == 0 {
                break;
            }
            if response.len().saturating_add(read) > LOCAL_API_MAX_RESPONSE_BYTES {
                return Err(ResponderBrokerError::WhoIsUnverified);
            }
            response.extend_from_slice(&chunk[..read]);
        }
        parse_local_api_response(&response)
    }
}

impl TailscaleLocalApi for TailscaleLocalApiClient {
    fn local_status<'a>(&'a self, cx: &'a Cx) -> TailscaleLocalApiFuture<'a, LocalTailscaleStatus> {
        Box::pin(async move {
            #[cfg(not(unix))]
            {
                let _ = (cx, &self.socket_path, self.io_timeout);
                return Err(ResponderBrokerError::PlatformUnsupported);
            }
            #[cfg(unix)]
            {
                let body = self.request_json(cx, "/localapi/v0/status").await?;
                parse_local_status(&body)
            }
        })
    }

    fn verify_local_address<'a>(
        &'a self,
        cx: &'a Cx,
        address: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, LocalTailscaleIdentity> {
        Box::pin(async move {
            #[cfg(not(unix))]
            {
                let _ = (cx, address, &self.socket_path, self.io_timeout);
                return Err(ResponderBrokerError::PlatformUnsupported);
            }
            #[cfg(unix)]
            {
                let status = self.local_status(cx).await?;
                if !status.addresses.contains(&address.ip()) {
                    return Err(ResponderBrokerError::WhoIsUnverified);
                }
                Ok(status.identity)
            }
        })
    }

    fn who_is<'a>(
        &'a self,
        cx: &'a Cx,
        source: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, WhoIsIdentity> {
        Box::pin(async move {
            #[cfg(not(unix))]
            {
                let _ = (cx, source, &self.socket_path, self.io_timeout);
                return Err(ResponderBrokerError::PlatformUnsupported);
            }
            #[cfg(unix)]
            {
                if source.ip().is_unspecified() {
                    return Err(ResponderBrokerError::WhoIsUnverified);
                }
                let encoded_source = percent_encode_query(&source.to_string());
                let endpoint = format!("/localapi/v0/whois?addr={encoded_source}&proto=tcp");
                let body = self.request_json(cx, &endpoint).await?;
                let response: LocalApiWhoIsResponse = serde_json::from_slice(&body)
                    .map_err(|_| ResponderBrokerError::WhoIsUnverified)?;
                let node = response.node.ok_or(ResponderBrokerError::WhoIsUnverified)?;
                let source_matches_node = node
                    .addresses
                    .iter()
                    .filter_map(|prefix| prefix.split('/').next())
                    .filter_map(|ip| ip.parse::<IpAddr>().ok())
                    .any(|ip| ip == source.ip());
                if !source_matches_node
                    || !valid_identity(&node.stable_id)
                    || !valid_node_key(&node.current_node_pubkey)
                {
                    return Err(ResponderBrokerError::WhoIsUnverified);
                }
                Ok(WhoIsIdentity {
                    stable_id: node.stable_id,
                    current_node_pubkey: node.current_node_pubkey,
                    login_name: response
                        .user_profile
                        .as_ref()
                        .and_then(|profile| profile.login_name.clone())
                        .filter(|login| !login.trim().is_empty()),
                    display_name: response
                        .user_profile
                        .as_ref()
                        .and_then(|profile| profile.display_name.clone())
                        .filter(|name| !name.trim().is_empty()),
                    user_id: response.user_profile.as_ref().and_then(|profile| {
                        profile.id.as_ref().and_then(|value| {
                            value.as_str().map(str::to_owned).or_else(|| {
                                value
                                    .as_i64()
                                    .map(|id| id.to_string())
                                    .or_else(|| value.as_u64().map(|id| id.to_string()))
                            })
                        })
                    }),
                })
            }
        })
    }
}

/// WhoIs row for one team-join enrolled peer endpoint.
#[derive(Clone, Debug, Eq, PartialEq)]
struct TeamJoinWhoIsPeer {
    ip: IpAddr,
    stable_id: String,
    current_node_pubkey: String,
}

/// LocalAPI stand-in for team-join TCP when tailscaled is absent.
#[derive(Clone, Debug)]
pub struct TeamJoinLocalApi {
    identity: LocalTailscaleIdentity,
    addresses: Vec<IpAddr>,
    peers: Vec<TeamJoinWhoIsPeer>,
}

impl TeamJoinLocalApi {
    /// Build from enrolled team-join peers only. Mixed tailnet peers fail.
    pub fn from_registrations(
        connection: &DbConnection,
        registrations: &[DurableResponderRegistration],
    ) -> Result<Self, ResponderBrokerError> {
        if registrations.is_empty() {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let responder_node_id = registrations[0].responder_node_id.clone();
        if registrations
            .iter()
            .any(|registration| registration.responder_node_id != responder_node_id)
        {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let mut peers = Vec::new();
        for registration in registrations {
            let peer = connection
                .get_mesh_peer(&registration.workspace_id, &registration.peer_handle)
                .map_err(|_| ResponderBrokerError::RouteUnavailable)?
                .filter(|peer| peer.enabled)
                .ok_or(ResponderBrokerError::RouteUnavailable)?;
            let policy = peer
                .policy_summary_json
                .as_deref()
                .ok_or(ResponderBrokerError::IdentityUpgradeRequired)
                .and_then(|json| {
                    serde_json::from_str::<MeshPeerRecord>(json)
                        .map_err(|_| ResponderBrokerError::IdentityUpgradeRequired)
                })?;
            if !crate::mesh::team::team_join_allows_ungranted_route(&policy) {
                return Err(ResponderBrokerError::RouteUnavailable);
            }
            let endpoint = peer_endpoint_for_whois(&policy.endpoint.endpoint)?;
            let current_node_pubkey = if policy.endpoint.tailscale_node_key.starts_with("nodekey:")
            {
                policy.endpoint.tailscale_node_key.clone()
            } else {
                format!("nodekey:{}", policy.endpoint.tailscale_node_key)
            };
            peers.push(TeamJoinWhoIsPeer {
                ip: endpoint.ip(),
                stable_id: format!("team-join-{}", peer.origin_node_id),
                current_node_pubkey,
            });
        }
        Ok(Self {
            identity: LocalTailscaleIdentity {
                stable_id: format!("team-join-{responder_node_id}"),
                current_node_pubkey: format!("nodekey:{responder_node_id}"),
                tailnet_id: crate::mesh::team::TEAM_JOIN_TAILNET_ID.to_owned(),
            },
            addresses: team_join_listen_ips(&peers),
            peers,
        })
    }

    #[must_use]
    pub fn all_loopback(&self) -> bool {
        !self.peers.is_empty() && self.peers.iter().all(|peer| peer.ip.is_loopback())
    }

    #[must_use]
    pub fn identity_for_source(&self, source: SocketAddr) -> Option<WhoIsIdentity> {
        self.peers
            .iter()
            .find(|peer| peer.ip == source.ip())
            .map(|peer| WhoIsIdentity {
                stable_id: peer.stable_id.clone(),
                current_node_pubkey: peer.current_node_pubkey.clone(),
                login_name: None,
                display_name: None,
                user_id: None,
            })
    }
}

fn team_join_listen_ips(peers: &[TeamJoinWhoIsPeer]) -> Vec<IpAddr> {
    if peers.is_empty() || peers.iter().all(|peer| peer.ip.is_loopback()) {
        return vec![IpAddr::V4(Ipv4Addr::LOCALHOST)];
    }
    let mut ips = Vec::new();
    for peer in peers {
        if let Some(local) = routed_local_ip(SocketAddr::new(peer.ip, 9))
            && !ips.contains(&local)
        {
            ips.push(local);
        }
    }
    if ips.is_empty() {
        vec![IpAddr::V4(Ipv4Addr::LOCALHOST)]
    } else {
        ips
    }
}

fn routed_local_ip(remote: SocketAddr) -> Option<IpAddr> {
    if remote.ip().is_unspecified() {
        return None;
    }
    if remote.ip().is_loopback() {
        return Some(if remote.is_ipv4() {
            IpAddr::V4(Ipv4Addr::LOCALHOST)
        } else {
            IpAddr::V6(Ipv6Addr::LOCALHOST)
        });
    }
    let bind = if remote.is_ipv4() {
        SocketAddr::from(([0, 0, 0, 0], 0))
    } else {
        SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 0], 0))
    };
    let socket = UdpSocket::bind(bind).ok()?;
    socket.connect(remote).ok()?;
    let local = socket.local_addr().ok()?;
    if local.ip().is_unspecified() || local.is_ipv4() != remote.is_ipv4() {
        return None;
    }
    Some(local.ip())
}

impl TailscaleLocalApi for TeamJoinLocalApi {
    fn local_status<'a>(
        &'a self,
        _cx: &'a Cx,
    ) -> TailscaleLocalApiFuture<'a, LocalTailscaleStatus> {
        Box::pin(async move {
            Ok(LocalTailscaleStatus {
                identity: self.identity.clone(),
                addresses: self.addresses.clone(),
            })
        })
    }

    fn verify_local_address<'a>(
        &'a self,
        _cx: &'a Cx,
        address: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, LocalTailscaleIdentity> {
        Box::pin(async move {
            if address.ip().is_loopback() || self.addresses.contains(&address.ip()) {
                Ok(self.identity.clone())
            } else {
                Err(ResponderBrokerError::WhoIsUnverified)
            }
        })
    }

    fn who_is<'a>(
        &'a self,
        _cx: &'a Cx,
        source: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, WhoIsIdentity> {
        Box::pin(async move {
            self.identity_for_source(source)
                .ok_or(ResponderBrokerError::WhoIsUnverified)
        })
    }

    fn allows_loopback_bind(&self) -> bool {
        true
    }
}

/// Production LocalAPI or the team-join loopback stand-in.
#[derive(Clone, Debug)]
pub enum InboundLocalApi {
    Tailscale(TailscaleLocalApiClient),
    TeamJoin(TeamJoinLocalApi),
}

impl InboundLocalApi {
    #[must_use]
    pub fn prefer(
        connection: &DbConnection,
        registrations: &[DurableResponderRegistration],
        localapi_socket: Option<&Path>,
    ) -> Option<Self> {
        if let Some(path) = localapi_socket {
            return Some(Self::Tailscale(TailscaleLocalApiClient::new(
                path,
                Duration::from_secs(2),
            )));
        }
        // Team-join enrollments identify peers by accepted source IP.
        // Prefer that stand-in even when tailscaled is installed: Windows
        // LocalAPI WhoIs is not the inbound path, and a hanging WhoIs
        // would leave hello unanswered.
        if let Ok(api) = TeamJoinLocalApi::from_registrations(connection, registrations) {
            return Some(Self::TeamJoin(api));
        }
        TailscaleLocalApiClient::discover(Duration::from_secs(2)).map(Self::Tailscale)
    }

    #[must_use]
    pub const fn is_team_join(&self) -> bool {
        matches!(self, Self::TeamJoin(_))
    }
}

impl TailscaleLocalApi for InboundLocalApi {
    fn local_status<'a>(&'a self, cx: &'a Cx) -> TailscaleLocalApiFuture<'a, LocalTailscaleStatus> {
        match self {
            Self::Tailscale(client) => client.local_status(cx),
            Self::TeamJoin(api) => api.local_status(cx),
        }
    }

    fn verify_local_address<'a>(
        &'a self,
        cx: &'a Cx,
        address: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, LocalTailscaleIdentity> {
        match self {
            Self::Tailscale(client) => client.verify_local_address(cx, address),
            Self::TeamJoin(api) => api.verify_local_address(cx, address),
        }
    }

    fn who_is<'a>(
        &'a self,
        cx: &'a Cx,
        source: SocketAddr,
    ) -> TailscaleLocalApiFuture<'a, WhoIsIdentity> {
        match self {
            Self::Tailscale(client) => client.who_is(cx, source),
            Self::TeamJoin(api) => api.who_is(cx, source),
        }
    }

    fn allows_loopback_bind(&self) -> bool {
        match self {
            Self::Tailscale(_) => false,
            Self::TeamJoin(api) => api.allows_loopback_bind(),
        }
    }
}

#[cfg(unix)]
#[derive(Debug, Deserialize)]
struct LocalApiStatus {
    #[serde(rename = "BackendState")]
    backend_state: Option<String>,
    #[serde(rename = "TailscaleIPs", default)]
    tailscale_ips: Vec<String>,
    #[serde(rename = "Self")]
    local: Option<LocalApiStatusNode>,
    #[serde(rename = "CurrentTailnet")]
    current_tailnet: Option<LocalApiCurrentTailnet>,
    #[serde(rename = "MagicDNSSuffix")]
    magic_dns_suffix: Option<String>,
}

#[cfg(unix)]
#[derive(Debug, Deserialize)]
struct LocalApiStatusNode {
    #[serde(rename = "ID")]
    stable_id: String,
    #[serde(rename = "PublicKey")]
    current_node_pubkey: String,
    #[serde(rename = "TailscaleIPs", default)]
    tailscale_ips: Vec<String>,
    #[serde(rename = "Tailnet")]
    tailnet_id: Option<String>,
}

#[cfg(unix)]
#[derive(Debug, Deserialize)]
struct LocalApiCurrentTailnet {
    #[serde(rename = "MagicDNSSuffix")]
    magic_dns_suffix: Option<String>,
}

#[cfg(unix)]
#[derive(Debug, Deserialize)]
struct LocalApiWhoIsResponse {
    #[serde(rename = "Node")]
    node: Option<LocalApiWhoIsNode>,
    #[serde(rename = "UserProfile")]
    user_profile: Option<LocalApiWhoIsUserProfile>,
}

#[cfg(unix)]
#[derive(Debug, Deserialize)]
struct LocalApiWhoIsUserProfile {
    #[serde(rename = "ID")]
    id: Option<serde_json::Value>,
    #[serde(rename = "LoginName")]
    login_name: Option<String>,
    #[serde(rename = "DisplayName")]
    display_name: Option<String>,
}

#[cfg(unix)]
#[derive(Debug, Deserialize)]
struct LocalApiWhoIsNode {
    #[serde(rename = "StableID")]
    stable_id: String,
    #[serde(rename = "Key")]
    current_node_pubkey: String,
    #[serde(rename = "Addresses", default)]
    addresses: Vec<String>,
}

#[cfg(unix)]
fn parse_local_status(body: &[u8]) -> Result<LocalTailscaleStatus, ResponderBrokerError> {
    let status: LocalApiStatus =
        serde_json::from_slice(body).map_err(|_| ResponderBrokerError::WhoIsUnverified)?;
    if status.backend_state.as_deref() != Some("Running") {
        return Err(ResponderBrokerError::TransportUnavailable);
    }
    let local = status.local.ok_or(ResponderBrokerError::WhoIsUnverified)?;
    let tailnet_id = local
        .tailnet_id
        .or_else(|| {
            status
                .current_tailnet
                .and_then(|tailnet| tailnet.magic_dns_suffix)
        })
        .or(status.magic_dns_suffix)
        .filter(|value| valid_identity(value))
        .ok_or(ResponderBrokerError::WhoIsUnverified)?;
    let mut addresses = status
        .tailscale_ips
        .iter()
        .chain(&local.tailscale_ips)
        .filter_map(|value| value.parse::<IpAddr>().ok())
        .filter(|ip| !ip.is_unspecified())
        .collect::<Vec<_>>();
    addresses.sort();
    addresses.dedup();
    if addresses.is_empty() {
        return Err(ResponderBrokerError::TransportUnavailable);
    }
    if !valid_identity(&local.stable_id) || !valid_node_key(&local.current_node_pubkey) {
        return Err(ResponderBrokerError::WhoIsUnverified);
    }
    Ok(LocalTailscaleStatus {
        identity: LocalTailscaleIdentity {
            stable_id: local.stable_id,
            current_node_pubkey: local.current_node_pubkey,
            tailnet_id,
        },
        addresses,
    })
}

#[cfg(unix)]
fn local_api_socket_exists(path: &Path) -> bool {
    use std::os::unix::fs::FileTypeExt;

    std::fs::symlink_metadata(path)
        .map(|metadata| metadata.file_type().is_socket())
        .unwrap_or(false)
}

#[cfg(not(unix))]
fn local_api_socket_exists(_path: &Path) -> bool {
    false
}

/// One locally registered responder route. No network message can introduce
/// or modify the workspace path or peer handle.
#[derive(Clone, Debug)]
pub struct RegisteredResponderRoute {
    pub workspace_path: PathBuf,
    /// Durable authority store used by production owner registrations. `None`
    /// is retained only for the lower-level transport conformance seam.
    pub database_path: Option<PathBuf>,
    pub peer_handle: String,
    pub committed_port: u16,
    pub expectations: ResponderExpectations,
    pub responder_node_pubkey: String,
    pub peer_transport_key_generation: u64,
    pub grant_generation: u64,
    pub capabilities: SessionCapabilities,
    pub limits: SessionChannelLimits,
}

/// Secret-free local registration request. Remote frames cannot supply any
/// of these fields; peer identity, grants, and pair-key generation are loaded
/// from their durable stores before the route enters the registry.
#[derive(Clone, Debug)]
pub struct DurableResponderRegistration {
    pub workspace_path: PathBuf,
    pub database_path: PathBuf,
    pub workspace_id: String,
    pub team_id: String,
    pub responder_node_id: String,
    pub peer_handle: String,
    pub committed_port: u16,
    pub capabilities: SessionCapabilities,
    pub limits: SessionChannelLimits,
}

/// Enrolled team pair-key peers become durable inbound routes.
pub fn plan_team_responder_registrations(
    connection: &DbConnection,
    workspace_id: &str,
    workspace_path: &Path,
    database_path: &Path,
    committed_port: u16,
) -> Vec<DurableResponderRegistration> {
    let Ok(teams) = crate::mesh::team::load_local_teams(connection) else {
        return Vec::new();
    };
    let workspace_id = crate::core::workspace::bound_workspace_id_or_hash(
        connection,
        workspace_id,
        &[workspace_path],
    )
    .unwrap_or_else(|_| workspace_id.to_owned());
    let Ok(peers) = connection.list_mesh_peers(&workspace_id) else {
        return Vec::new();
    };
    let mut registrations = Vec::new();
    for team in &teams {
        for peer in peers.iter().filter(|peer| peer.enabled) {
            if crate::mesh::team::team_pair_peer_handle(&team.team_id, &peer.origin_node_id)
                != peer.peer_id
            {
                continue;
            }
            registrations.push(DurableResponderRegistration {
                workspace_path: workspace_path.to_path_buf(),
                database_path: database_path.to_path_buf(),
                workspace_id: workspace_id.to_owned(),
                team_id: team.team_id.clone(),
                responder_node_id: team.origin_node_id.clone(),
                peer_handle: peer.peer_id.clone(),
                committed_port,
                capabilities: SessionCapabilities::base(),
                limits: SessionChannelLimits::default(),
            });
        }
    }
    registrations
}

/// Same-EUID control-channel operation. Network frames never carry these.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResponderControlOp {
    Register,
    Unregister,
    Status,
}

/// Bounded local registration request. Paths are revalidated against the
/// caller's filesystem; they are never taken from a network peer.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ResponderControlRequest {
    pub schema: String,
    pub op: ResponderControlOp,
    pub nonce: String,
    pub workspace_id: String,
    pub team_id: String,
    pub responder_node_id: String,
    pub workspace_path: PathBuf,
    pub database_path: PathBuf,
    pub peer_handles: Vec<String>,
    pub committed_port: u16,
}

/// Secret-free control-channel reply. Echoes the request nonce only.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponderControlResponse {
    pub schema: String,
    pub ok: bool,
    pub nonce: String,
    pub code: Option<String>,
    pub message: Option<String>,
    pub bound_addresses: Vec<String>,
    pub registered_routes: usize,
}

/// Authoritative LocalAPI snapshot plus the exact durable route it resolved.
#[derive(Clone, Debug)]
pub struct ResolvedResponderRegistration {
    pub local_status: LocalTailscaleStatus,
    pub route: RegisteredResponderRoute,
}

/// Resolve one route from LocalAPI authority and durable local stores.
///
/// The only caller inputs are local registration scope. Stable node ids,
/// current node keys, key generations, and grant generations are never
/// accepted from the network or command line.
pub async fn resolve_durable_registration<A: TailscaleLocalApi>(
    cx: &Cx,
    local_api: &A,
    connection: &DbConnection,
    registration: &DurableResponderRegistration,
) -> Result<ResolvedResponderRegistration, ResponderBrokerError> {
    let registration = bind_durable_registration_workspace(connection, registration)?;
    validate_durable_registration(connection, &registration)?;
    let local_status = local_api.local_status(cx).await.map_err(|error| {
        if matches!(
            error,
            ResponderBrokerError::WhoIsUnavailable | ResponderBrokerError::TransportUnavailable
        ) {
            ResponderBrokerError::TransportUnavailable
        } else {
            error
        }
    })?;
    let peer = connection
        .get_mesh_peer(&registration.workspace_id, &registration.peer_handle)
        .map_err(|_| ResponderBrokerError::RouteUnavailable)?
        .filter(|peer| peer.enabled)
        .ok_or(ResponderBrokerError::RouteUnavailable)?;
    let policy = peer
        .policy_summary_json
        .as_deref()
        .ok_or(ResponderBrokerError::IdentityUpgradeRequired)
        .and_then(|json| {
            serde_json::from_str::<MeshPeerRecord>(json)
                .map_err(|_| ResponderBrokerError::IdentityUpgradeRequired)
        })?;
    if policy.peer_id != registration.peer_handle
        || policy.workspace_id != registration.workspace_id
        || policy.state != MeshPeerState::Active
        || !policy.handshake.granted
        || !policy.handshake.discovery_consent
        || !crate::mesh::team::team_join_tailnet_matches(
            &policy.endpoint.tailnet_id,
            &local_status.identity.tailnet_id,
        )
    {
        return Err(ResponderBrokerError::RouteUnavailable);
    }

    let endpoint = peer_endpoint_for_whois(&policy.endpoint.endpoint)?;
    let who_is = local_api.who_is(cx, endpoint).await?;
    let expected_key = &policy.endpoint.tailscale_node_key;
    let stored_pubkey = if expected_key.starts_with("nodekey:") {
        expected_key.clone()
    } else {
        format!("nodekey:{expected_key}")
    };
    let who_matches_key =
        who_is.current_node_pubkey == *expected_key || who_is.current_node_pubkey == stored_pubkey;
    if peer.transport_identity.is_none()
        && !who_matches_key
        && !crate::mesh::team::team_join_node_pubkey_matches(
            &stored_pubkey,
            &who_is.current_node_pubkey,
        )
    {
        return Err(ResponderBrokerError::WhoIsUnverified);
    }
    let peer = connection
        .observe_mesh_peer_transport_identity(&ObserveMeshPeerTransportIdentityInput {
            workspace_id: registration.workspace_id.clone(),
            peer_id: registration.peer_handle.clone(),
            tailnet_id: local_status.identity.tailnet_id.clone(),
            stable_node_id: who_is.stable_id,
            current_node_pubkey: who_is.current_node_pubkey,
            observed_at: None,
        })
        .map_err(map_peer_identity_error)?;
    let transport_identity = peer
        .transport_identity
        .ok_or(ResponderBrokerError::IdentityUpgradeRequired)?;
    let grant_generation = connection
        .get_mesh_lane_grant_state(&registration.workspace_id, &registration.peer_handle)
        .map_err(|_| ResponderBrokerError::RouteUnavailable)?
        .filter(|grant| grant.target_matches_current_peer && grant.grant_generation > 0)
        .map(|grant| grant.grant_generation)
        .or_else(|| crate::mesh::team::team_join_allows_ungranted_route(&policy).then_some(0))
        .ok_or(ResponderBrokerError::RouteUnavailable)?;
    let pair_record = MeshKeyStore::open_existing(&registration.workspace_path)
        .map_err(map_key_store_error)?
        .ok_or(ResponderBrokerError::KeyStoreUnavailable)?
        .load_pair_key(&registration.peer_handle, PairKeyClass::Current)
        .map_err(map_key_store_error)?
        .ok_or(ResponderBrokerError::PairingRequired)?;

    let route = RegisteredResponderRoute {
        workspace_path: registration.workspace_path.clone(),
        database_path: Some(registration.database_path.clone()),
        peer_handle: registration.peer_handle.clone(),
        committed_port: registration.committed_port,
        expectations: ResponderExpectations {
            team_id: registration.team_id.clone(),
            tailnet_id: local_status.identity.tailnet_id.clone(),
            responder_node_id: registration.responder_node_id.clone(),
            responder_workspace_id: registration.workspace_id.clone(),
            responder_stable_id: local_status.identity.stable_id.clone(),
            initiator_node_id: peer.origin_node_id,
            initiator_stable_id: transport_identity.stable_node_id,
            pair_key_generation: pair_record.generation.get(),
        },
        responder_node_pubkey: local_status.identity.current_node_pubkey.clone(),
        peer_transport_key_generation: transport_identity.key_generation,
        grant_generation,
        capabilities: registration.capabilities.clone(),
        limits: registration.limits,
    };
    route.validate()?;
    Ok(ResolvedResponderRegistration {
        local_status,
        route,
    })
}

fn bind_durable_registration_workspace(
    connection: &DbConnection,
    registration: &DurableResponderRegistration,
) -> Result<DurableResponderRegistration, ResponderBrokerError> {
    let workspace_id = crate::core::workspace::bound_workspace_id_or_hash(
        connection,
        &registration.workspace_id,
        &[registration.workspace_path.as_path()],
    )
    .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    let mut bound = registration.clone();
    bound.workspace_id = workspace_id;
    Ok(bound)
}

fn validate_durable_registration(
    connection: &DbConnection,
    registration: &DurableResponderRegistration,
) -> Result<(), ResponderBrokerError> {
    if registration.committed_port < 1024
        || !valid_identity(&registration.team_id)
        || !valid_identity(&registration.workspace_id)
        || !valid_identity(&registration.responder_node_id)
        || !valid_opaque_peer_handle(&registration.peer_handle)
        || !registration.workspace_path.is_absolute()
        || !registration.database_path.is_absolute()
        || !path_matches_canonical_form(&registration.workspace_path)
        || !path_matches_canonical_form(&registration.database_path)
    {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    let connection_path = match connection.location() {
        DatabaseLocation::File(path) => path,
        DatabaseLocation::Memory => return Err(ResponderBrokerError::InvalidConfiguration),
    };
    if !path_matches_canonical_location(&connection_path, &registration.database_path) {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    let workspace = connection
        .get_workspace(&registration.workspace_id)
        .map_err(|_| ResponderBrokerError::RouteUnavailable)?
        .ok_or(ResponderBrokerError::InvalidConfiguration)?;
    let stored_workspace_path = PathBuf::from(workspace.path);
    if !stored_workspace_path.is_absolute()
        || !path_matches_canonical_location(&stored_workspace_path, &registration.workspace_path)
    {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    Ok(())
}

fn peer_endpoint_for_whois(endpoint: &str) -> Result<SocketAddr, ResponderBrokerError> {
    endpoint
        .parse::<SocketAddr>()
        .or_else(|_| endpoint.parse::<IpAddr>().map(|ip| SocketAddr::new(ip, 1)))
        .map_err(|_| ResponderBrokerError::IdentityUpgradeRequired)
}

fn map_peer_identity_error(error: MeshPeerTransportIdentityError) -> ResponderBrokerError {
    match error {
        MeshPeerTransportIdentityError::StableIdentityMismatch
        | MeshPeerTransportIdentityError::AmbiguousStableIdentity => {
            ResponderBrokerError::WhoIsUnverified
        }
        MeshPeerTransportIdentityError::InvalidObservation => ResponderBrokerError::WhoIsUnverified,
        MeshPeerTransportIdentityError::PeerUnavailable
        | MeshPeerTransportIdentityError::AmbiguousGrantTarget
        | MeshPeerTransportIdentityError::RandomnessUnavailable
        | MeshPeerTransportIdentityError::GenerationExhausted
        | MeshPeerTransportIdentityError::Storage(_) => ResponderBrokerError::RouteUnavailable,
    }
}

impl RegisteredResponderRoute {
    fn validate(&self) -> Result<(), ResponderBrokerError> {
        if !self.workspace_path.is_absolute()
            || self
                .database_path
                .as_ref()
                .is_some_and(|path| !path.is_absolute())
            || !valid_opaque_peer_handle(&self.peer_handle)
            || self.committed_port < 1024
            || !valid_identity(&self.expectations.team_id)
            || !valid_identity(&self.expectations.tailnet_id)
            || !valid_identity(&self.expectations.responder_workspace_id)
            || !valid_identity(&self.expectations.responder_stable_id)
            || !valid_durable_node_principal(&self.expectations.initiator_node_id)
            || !valid_identity(&self.expectations.initiator_stable_id)
            || !valid_node_key(&self.responder_node_pubkey)
            || self.expectations.pair_key_generation == 0
            || self.peer_transport_key_generation == 0
        {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        Ok(())
    }
}

/// Exact in-memory route table supplied by the local owner. Construction
/// rejects duplicates and mixed listener identity/port posture.
#[derive(Clone, Debug)]
pub struct ResponderRouteRegistry {
    routes: BTreeMap<RouteKey, RegisteredResponderRoute>,
    committed_port: u16,
    tailnet_id: String,
    responder_stable_id: String,
    responder_node_pubkey: String,
    limits: SessionChannelLimits,
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
struct RouteKey {
    team_id: String,
    target_workspace_id: String,
    initiator_stable_id: String,
    pair_key_generation: u64,
}

impl ResponderRouteRegistry {
    pub fn new(
        routes: impl IntoIterator<Item = RegisteredResponderRoute>,
    ) -> Result<Self, ResponderBrokerError> {
        let mut iter = routes.into_iter();
        let first = iter
            .next()
            .ok_or(ResponderBrokerError::InvalidConfiguration)?;
        first.validate()?;
        let committed_port = first.committed_port;
        let tailnet_id = first.expectations.tailnet_id.clone();
        let responder_stable_id = first.expectations.responder_stable_id.clone();
        let responder_node_pubkey = first.responder_node_pubkey.clone();
        let limits = first.limits;
        let mut registry = Self {
            routes: BTreeMap::new(),
            committed_port,
            tailnet_id,
            responder_stable_id,
            responder_node_pubkey,
            limits,
        };
        registry.insert_checked(first)?;
        for route in iter {
            route.validate()?;
            if route.committed_port != registry.committed_port
                || route.expectations.tailnet_id != registry.tailnet_id
                || route.expectations.responder_stable_id != registry.responder_stable_id
                || route.responder_node_pubkey != registry.responder_node_pubkey
                || route.limits != registry.limits
            {
                return Err(ResponderBrokerError::InvalidConfiguration);
            }
            registry.insert_checked(route)?;
        }
        Ok(registry)
    }

    fn insert_checked(
        &mut self,
        route: RegisteredResponderRoute,
    ) -> Result<(), ResponderBrokerError> {
        let key = RouteKey {
            team_id: route.expectations.team_id.clone(),
            target_workspace_id: route.expectations.responder_workspace_id.clone(),
            initiator_stable_id: route.expectations.initiator_stable_id.clone(),
            pair_key_generation: route.expectations.pair_key_generation,
        };
        if self.routes.insert(key, route).is_some() {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        Ok(())
    }

    fn resolve(&self, selectors: &UntrustedRouteSelectors) -> Option<&RegisteredResponderRoute> {
        self.routes.get(&RouteKey {
            team_id: selectors.team_id.clone(),
            target_workspace_id: selectors.responder_workspace_id.clone(),
            initiator_stable_id: selectors.initiator_stable_id.clone(),
            pair_key_generation: selectors.pair_key_generation,
        })
    }

    /// Resolve the one local route authenticated into an established session.
    /// Pair-key generation is intentionally absent from [`SessionBinding`], so
    /// multiple generations with the same authenticated identity tuple are
    /// ambiguous and fail closed rather than selecting one arbitrarily.
    fn resolve_authenticated_binding(
        &self,
        binding: &SessionBinding,
    ) -> Option<&RegisteredResponderRoute> {
        let mut matches = self.routes.values().filter(|route| {
            let expected = &route.expectations;
            expected.team_id == binding.team_id
                && expected.tailnet_id == binding.tailnet_id
                && expected.initiator_node_id == binding.initiator_node_id
                && expected.responder_node_id == binding.responder_node_id
                && expected.responder_workspace_id == binding.responder_workspace_id
                && expected.initiator_stable_id == binding.initiator_stable_id
                && expected.responder_stable_id == binding.responder_stable_id
        });
        let route = matches.next()?;
        matches.next().is_none().then_some(route)
    }

    #[must_use]
    pub fn route_count(&self) -> usize {
        self.routes.len()
    }

    fn workspace_ids(&self) -> Vec<String> {
        let mut ids = self
            .routes
            .values()
            .map(|route| route.expectations.responder_workspace_id.clone())
            .collect::<Vec<_>>();
        ids.sort();
        ids.dedup();
        ids
    }

    fn first_store_route(&self) -> Option<&RegisteredResponderRoute> {
        self.routes
            .values()
            .find(|route| route.database_path.is_some())
            .or_else(|| self.routes.values().next())
    }

    fn first_workspace_path(&self) -> Option<&Path> {
        self.first_store_route()
            .map(|route| route.workspace_path.as_path())
    }
}

/// Pre-authentication concurrency and fixed-window rate bounds.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PreAuthAdmissionLimits {
    pub max_global_inflight: usize,
    pub max_source_inflight: usize,
    pub window_ms: u64,
    pub max_source_per_window: u32,
    pub max_global_per_window: u32,
    pub max_tracked_sources: usize,
}

impl Default for PreAuthAdmissionLimits {
    fn default() -> Self {
        Self {
            max_global_inflight: 64,
            max_source_inflight: 8,
            window_ms: 60_000,
            max_source_per_window: 8,
            max_global_per_window: 64,
            max_tracked_sources: 1024,
        }
    }
}

impl PreAuthAdmissionLimits {
    fn validate(self) -> Result<Self, ResponderBrokerError> {
        if self.max_global_inflight == 0
            || self.max_source_inflight == 0
            || self.max_source_inflight > self.max_global_inflight
            || self.window_ms == 0
            || self.max_source_per_window == 0
            || self.max_global_per_window == 0
            || self.max_tracked_sources == 0
        {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        Ok(self)
    }
}

#[derive(Debug)]
struct AdmissionState {
    limits: PreAuthAdmissionLimits,
    started_at: Instant,
    inflight_global: usize,
    inflight_by_source: BTreeMap<IpAddr, usize>,
    rate: BootstrapAdmission,
    peers: BTreeMap<String, crate::mesh::admission::MeshPeerAdmissionState>,
}

impl AdmissionState {
    fn new(limits: PreAuthAdmissionLimits) -> Self {
        Self {
            limits,
            started_at: Instant::now(),
            inflight_global: 0,
            inflight_by_source: BTreeMap::new(),
            rate: BootstrapAdmission::with_limits(
                limits.window_ms,
                limits.max_source_per_window,
                limits.max_global_per_window,
                limits.max_tracked_sources,
            ),
            peers: BTreeMap::new(),
        }
    }
}

#[derive(Debug)]
struct PreAuthPermit {
    state: Arc<Mutex<AdmissionState>>,
    source: IpAddr,
}

impl Drop for PreAuthPermit {
    fn drop(&mut self) {
        let Ok(mut state) = self.state.lock() else {
            return;
        };
        state.inflight_global = state.inflight_global.saturating_sub(1);
        if let Some(count) = state.inflight_by_source.get_mut(&self.source) {
            *count = count.saturating_sub(1);
            if *count == 0 {
                state.inflight_by_source.remove(&self.source);
            }
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResponderBrokerState {
    Listening,
    Shutdown,
    Unsupported,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponderBrokerStatus {
    pub schema: &'static str,
    pub state: ResponderBrokerState,
    pub bound_address: Option<String>,
    pub registered_routes: usize,
    pub preauth_inflight: usize,
    pub accepted_connections: u64,
    pub authenticated_sessions: u64,
    pub rejected_connections: u64,
    pub last_error_code: Option<&'static str>,
    pub application_hello_performed: bool,
    pub anti_entropy_performed: bool,
    pub synchronized: bool,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponderBrokerAuditEvent {
    pub schema: &'static str,
    pub action: &'static str,
    pub outcome: &'static str,
    pub code: Option<&'static str>,
    pub route_selected: bool,
    pub authenticated: bool,
}

#[derive(Debug)]
struct RuntimeStatus {
    state: ResponderBrokerState,
    accepted_connections: u64,
    authenticated_sessions: u64,
    rejected_connections: u64,
    last_error_code: Option<&'static str>,
    application_hello_performed: bool,
    recent_audit: VecDeque<ResponderBrokerAuditEvent>,
}

impl RuntimeStatus {
    fn listening() -> Self {
        Self {
            state: ResponderBrokerState::Listening,
            accepted_connections: 0,
            authenticated_sessions: 0,
            rejected_connections: 0,
            last_error_code: None,
            application_hello_performed: false,
            recent_audit: VecDeque::new(),
        }
    }

    fn record(
        &mut self,
        outcome: &'static str,
        code: Option<&'static str>,
        route_selected: bool,
        authenticated: bool,
        rejected_connection: bool,
    ) {
        if authenticated {
            self.authenticated_sessions = self.authenticated_sessions.saturating_add(1);
        } else if rejected_connection {
            self.rejected_connections = self.rejected_connections.saturating_add(1);
        }
        if code.is_some() {
            self.last_error_code = code;
        }
        if self.recent_audit.len() == MAX_RECENT_BROKER_AUDIT_EVENTS {
            self.recent_audit.pop_front();
        }
        self.recent_audit.push_back(ResponderBrokerAuditEvent {
            schema: RESPONDER_BROKER_AUDIT_SCHEMA_V1,
            action: "mesh.responder.accept",
            outcome,
            code,
            route_selected,
            authenticated,
        });
    }
}

/// Structured, metadata-safe accepted-side error surface.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ResponderBrokerError {
    PlatformUnsupported,
    InvalidConfiguration,
    PortConflict,
    TransportUnavailable,
    Cancelled,
    AdmissionLimited,
    WhoIsUnavailable,
    WhoIsUnverified,
    IdentityUpgradeRequired,
    RouteUnavailable,
    KeyStoreUnavailable,
    PairingRequired,
    Session(SessionChannelError),
    BootstrapHelloAnswered,
}

impl ResponderBrokerError {
    #[must_use]
    pub const fn code(&self) -> &'static str {
        match self {
            Self::PortConflict => MESH_RESPONDER_PORT_CONFLICT_CODE,
            Self::WhoIsUnavailable | Self::WhoIsUnverified => {
                MESH_BOOTSTRAP_IDENTITY_UNVERIFIED_CODE
            }
            Self::IdentityUpgradeRequired => MESH_RESPONDER_IDENTITY_UPGRADE_REQUIRED_CODE,
            Self::RouteUnavailable => MESH_RESPONDER_ROUTE_UNAVAILABLE_CODE,
            Self::KeyStoreUnavailable => MESH_KEY_STORE_UNAVAILABLE_CODE,
            Self::PairingRequired => "mesh_frame_auth_failed",
            Self::Session(error) => error.degraded_code(),
            Self::BootstrapHelloAnswered => "mesh_transport_unreachable",
            Self::PlatformUnsupported
            | Self::InvalidConfiguration
            | Self::TransportUnavailable
            | Self::Cancelled
            | Self::AdmissionLimited => "mesh_transport_unreachable",
        }
    }

    #[must_use]
    pub fn severity(&self) -> &'static str {
        match self {
            Self::WhoIsUnavailable
            | Self::WhoIsUnverified
            | Self::IdentityUpgradeRequired
            | Self::RouteUnavailable
            | Self::KeyStoreUnavailable
            | Self::PairingRequired => "high",
            Self::Session(error)
                if matches!(
                    error.degraded_code(),
                    "mesh_frame_auth_failed"
                        | "mesh_frame_target_mismatch"
                        | "mesh_frame_replay_rejected"
                ) =>
            {
                "high"
            }
            Self::PortConflict => "high",
            Self::PlatformUnsupported
            | Self::InvalidConfiguration
            | Self::TransportUnavailable
            | Self::Cancelled
            | Self::AdmissionLimited
            | Self::BootstrapHelloAnswered
            | Self::Session(_) => "warning",
        }
    }

    #[must_use]
    pub fn message(&self) -> String {
        match self {
            Self::PlatformUnsupported => {
                "Inbound mesh responder transport is unsupported on this platform".to_owned()
            }
            Self::InvalidConfiguration => {
                "Mesh responder refused invalid local listener or route configuration".to_owned()
            }
            Self::PortConflict => {
                "Mesh responder could not acquire the committed listener port".to_owned()
            }
            Self::TransportUnavailable => {
                "Mesh responder transport is unavailable on the verified local address".to_owned()
            }
            Self::Cancelled => "Mesh responder accept operation was cancelled".to_owned(),
            Self::AdmissionLimited => {
                "Mesh responder declined pre-authentication work at its bounded admission limit"
                    .to_owned()
            }
            Self::WhoIsUnavailable => {
                "Mesh responder could not verify the accepted source identity through Tailscale WhoIs"
                    .to_owned()
            }
            Self::WhoIsUnverified => {
                "Mesh responder could not verify the accepted source identity through Tailscale WhoIs"
                    .to_owned()
            }
            Self::IdentityUpgradeRequired => {
                "Mesh responder peer requires an authoritative LocalAPI identity observation"
                    .to_owned()
            }
            Self::RouteUnavailable => {
                "Mesh responder has no exact validated route for the authenticated target"
                    .to_owned()
            }
            Self::KeyStoreUnavailable => {
                "Mesh responder could not open the existing hardened pair-key store".to_owned()
            }
            Self::PairingRequired => {
                "Mesh responder route is not paired for authenticated transport".to_owned()
            }
            Self::Session(error) => error.message(),
            Self::BootstrapHelloAnswered => {
                "Mesh responder answered an unsigned bootstrap hello".to_owned()
            }
        }
    }

    #[must_use]
    pub const fn repair(&self) -> &'static str {
        match self {
            Self::PlatformUnsupported => {
                "Use TeamJoin inbound (`ee mesh hello-responder run`) on this host, or Tailscale LocalAPI on Unix."
            }
            Self::PortConflict => {
                "Stop the conflicting responder owner or align the user-scoped broker on the committed port."
            }
            Self::WhoIsUnavailable | Self::WhoIsUnverified => {
                "Restore the local Tailscale daemon and verify the peer remains enrolled on the expected tailnet."
            }
            Self::IdentityUpgradeRequired => {
                "Start the responder while the enrolled peer endpoint is reachable through Tailscale LocalAPI."
            }
            Self::RouteUnavailable | Self::InvalidConfiguration => {
                "Re-register the exact local team/workspace route with the user-scoped responder owner."
            }
            Self::KeyStoreUnavailable => {
                "Repair owner-only mesh key-store permissions or re-pair without creating inbound credentials."
            }
            Self::PairingRequired => {
                "Pair this enrolled peer before retrying authenticated mesh transport."
            }
            Self::TransportUnavailable
            | Self::Cancelled
            | Self::AdmissionLimited
            | Self::BootstrapHelloAnswered
            | Self::Session(_) => {
                "Verify the enrolled peer endpoint and retry under a live bounded mesh operation."
            }
        }
    }
}

impl fmt::Display for ResponderBrokerError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message())
    }
}

impl std::error::Error for ResponderBrokerError {}

/// Single listener owner for the bounded accepted-side production path.
pub struct ResponderBroker<A> {
    listener: Option<TcpListener>,
    local_api: A,
    routes: Arc<ResponderRouteRegistry>,
    admission: Arc<Mutex<AdmissionState>>,
    runtime: Arc<Mutex<RuntimeStatus>>,
    bound_address: SocketAddr,
}

impl<A: TailscaleLocalApi> ResponderBroker<A> {
    pub async fn bind(
        cx: &Cx,
        address: SocketAddr,
        local_api: A,
        routes: ResponderRouteRegistry,
        admission_limits: PreAuthAdmissionLimits,
    ) -> Result<Self, ResponderBrokerError> {
        checkpoint(cx, "responder bind")?;
        refuse_if_transport_disabled()?;
        let admission_limits = admission_limits.validate()?;
        if address.ip().is_unspecified()
            || address.port() < 1024
            || address.port() != routes.committed_port
        {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let local_identity = local_api.verify_local_address(cx, address).await?;
        if !crate::mesh::team::team_join_stable_id_matches(
            &routes.responder_stable_id,
            &local_identity.stable_id,
        ) || !crate::mesh::team::team_join_node_pubkey_matches(
            &routes.responder_node_pubkey,
            &local_identity.current_node_pubkey,
        ) || !crate::mesh::team::team_join_tailnet_matches(
            &routes.tailnet_id,
            &local_identity.tailnet_id,
        ) {
            return Err(ResponderBrokerError::WhoIsUnverified);
        }
        let _ambient = Cx::set_current(Some(cx.clone()));
        let listener = TcpListener::bind(address).await.map_err(|error| {
            if error.kind() == io::ErrorKind::AddrInUse {
                ResponderBrokerError::PortConflict
            } else {
                ResponderBrokerError::TransportUnavailable
            }
        })?;
        let bound_address = listener
            .local_addr()
            .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
        Ok(Self {
            listener: Some(listener),
            local_api,
            routes: Arc::new(routes),
            admission: Arc::new(Mutex::new(AdmissionState::new(admission_limits))),
            runtime: Arc::new(Mutex::new(RuntimeStatus::listening())),
            bound_address,
        })
    }

    #[must_use]
    pub const fn local_addr(&self) -> SocketAddr {
        self.bound_address
    }

    /// Accept and authenticate exactly one session. The kernel source is
    /// admitted before reading `session_open`, then passed unchanged to the
    /// LocalAPI WhoIs query and T2.1's source-coupling check.
    pub async fn accept_authenticated(
        &self,
        cx: &Cx,
    ) -> Result<AuthenticatedTransportSession, ResponderBrokerError> {
        checkpoint(cx, "responder accept")?;
        let listener = self
            .listener
            .as_ref()
            .ok_or(ResponderBrokerError::TransportUnavailable)?;
        let _ambient = Cx::set_current(Some(cx.clone()));
        let (stream, kernel_source) = match listener.accept().await {
            Ok(accepted) => accepted,
            Err(error) => {
                let broker_error = if error.kind() == io::ErrorKind::Interrupted {
                    ResponderBrokerError::Cancelled
                } else {
                    ResponderBrokerError::TransportUnavailable
                };
                self.record_error(&broker_error, false, false);
                return Err(broker_error);
            }
        };
        {
            let mut runtime = self
                .runtime
                .lock()
                .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
            runtime.accepted_connections = runtime.accepted_connections.saturating_add(1);
        }
        let permit = match self.admit(kernel_source.ip()) {
            Ok(permit) => permit,
            Err(error) => {
                let _ = stream.shutdown(Shutdown::Both);
                self.record_error(&error, false, true);
                return Err(error);
            }
        };
        let routes = Arc::clone(&self.routes);
        let local_api = &self.local_api;
        let resolution_error = Arc::new(Mutex::new(None));
        let error_slot = Arc::clone(&resolution_error);
        let route_selected = Arc::new(AtomicBool::new(false));
        let selected_slot = Arc::clone(&route_selected);
        let limits = self.routes.limits;
        let mut stream = stream;
        let first_packet = match read_asupersync_framed(cx, &mut stream, limits.io_timeout).await {
            Ok(bytes) => bytes,
            Err(error) => {
                let _ = stream.shutdown(Shutdown::Both);
                self.record_error(&error, false, true);
                return Err(error);
            }
        };
        if decode_envelope(&first_packet)
            .ok()
            .is_some_and(|envelope| envelope.capability == BootstrapCapability::Join)
        {
            if let Err(error) =
                answer_bootstrap_join(cx, &mut stream, &self.routes, &first_packet, kernel_source)
                    .await
            {
                let _ = stream.shutdown(Shutdown::Write);
                self.record_error(&error, false, true);
                return Err(error);
            }
            let _ = stream.shutdown(Shutdown::Write);
            if let Ok(mut runtime) = self.runtime.lock() {
                runtime.application_hello_performed = true;
                runtime.record("bootstrap_join", None, false, false, false);
            }
            return Err(ResponderBrokerError::BootstrapHelloAnswered);
        }
        if decode_envelope(&first_packet)
            .ok()
            .is_some_and(|envelope| envelope.capability == BootstrapCapability::Hello)
        {
            if let Err(error) =
                answer_bootstrap_hello(cx, &mut stream, &self.routes, &first_packet).await
            {
                let _ = stream.shutdown(Shutdown::Both);
                self.record_error(&error, false, true);
                return Err(error);
            }
            let _ = answer_sync_round(cx, &mut stream, &self.routes).await;
            let _ = stream.shutdown(Shutdown::Both);
            if let Ok(mut runtime) = self.runtime.lock() {
                runtime.application_hello_performed = true;
                runtime.record("bootstrap_hello", None, false, false, false);
            }
            return Err(ResponderBrokerError::BootstrapHelloAnswered);
        }
        let accepted = accept_authenticated_session_with_open_bytes(
            cx,
            stream,
            limits,
            first_packet,
            move |route_cx, observed_source, selectors| {
                let error_slot = Arc::clone(&error_slot);
                async move {
                    match resolve_route(
                        &route_cx,
                        local_api,
                        &routes,
                        observed_source,
                        &selectors,
                        permit,
                        &selected_slot,
                    )
                    .await
                    {
                        Ok(route) => Ok(route),
                        Err(error) => {
                            if let Ok(mut slot) = error_slot.lock() {
                                *slot = Some(error);
                            }
                            Err(SessionChannelError::Authentication {
                                message: "accepted responder route could not be verified"
                                    .to_owned(),
                            })
                        }
                    }
                }
            },
        )
        .await;
        match accepted {
            Ok(session) => {
                self.record_success();
                Ok(session)
            }
            Err(session_error) => {
                let broker_error = resolution_error
                    .lock()
                    .ok()
                    .and_then(|mut slot| slot.take())
                    .unwrap_or_else(|| ResponderBrokerError::Session(session_error));
                self.record_error(&broker_error, route_selected.load(Ordering::Acquire), true);
                Err(broker_error)
            }
        }
    }

    /// Accept one authenticated session, serve one EventFetch/Summary
    /// sync-round from the origin store, then close the session.
    pub async fn accept_authenticated_and_serve(
        &self,
        cx: &Cx,
    ) -> Result<(), ResponderBrokerError> {
        let mut session = self.accept_authenticated(cx).await?;
        let result =
            serve_authenticated_sync_round(cx, &mut session, &self.routes, &self.admission).await;
        session.close();
        result
    }

    fn admit(&self, source: IpAddr) -> Result<PreAuthPermit, ResponderBrokerError> {
        let mut state = self
            .admission
            .lock()
            .map_err(|_| ResponderBrokerError::AdmissionLimited)?;
        if state.inflight_global >= state.limits.max_global_inflight {
            return Err(ResponderBrokerError::AdmissionLimited);
        }
        let source_inflight = state.inflight_by_source.get(&source).copied().unwrap_or(0);
        if source_inflight >= state.limits.max_source_inflight {
            return Err(ResponderBrokerError::AdmissionLimited);
        }
        if source_inflight == 0
            && state.inflight_by_source.len() >= state.limits.max_tracked_sources
        {
            return Err(ResponderBrokerError::AdmissionLimited);
        }
        let now_ms = u64::try_from(state.started_at.elapsed().as_millis()).unwrap_or(u64::MAX);
        state
            .rate
            .admit(&source.to_string(), now_ms)
            .map_err(|_| ResponderBrokerError::AdmissionLimited)?;
        state.inflight_global = state.inflight_global.saturating_add(1);
        state
            .inflight_by_source
            .entry(source)
            .and_modify(|count| *count = count.saturating_add(1))
            .or_insert(1);
        drop(state);
        Ok(PreAuthPermit {
            state: Arc::clone(&self.admission),
            source,
        })
    }

    fn record_success(&self) {
        if let Ok(mut runtime) = self.runtime.lock() {
            runtime.record("accepted", None, true, true, false);
        }
    }

    fn record_error(
        &self,
        error: &ResponderBrokerError,
        route_selected: bool,
        rejected_connection: bool,
    ) {
        if let Ok(mut runtime) = self.runtime.lock() {
            let outcome = if matches!(error, ResponderBrokerError::Cancelled) {
                "cancelled"
            } else {
                "rejected"
            };
            runtime.record(
                outcome,
                Some(error.code()),
                route_selected,
                false,
                rejected_connection,
            );
        }
    }

    #[must_use]
    pub fn status(&self) -> ResponderBrokerStatus {
        let (state, accepted, authenticated, rejected, last_error, hello_performed) = self
            .runtime
            .lock()
            .map(|runtime| {
                (
                    runtime.state,
                    runtime.accepted_connections,
                    runtime.authenticated_sessions,
                    runtime.rejected_connections,
                    runtime.last_error_code,
                    runtime.application_hello_performed,
                )
            })
            .unwrap_or((
                ResponderBrokerState::Shutdown,
                0,
                0,
                0,
                Some("mesh_transport_unreachable"),
                false,
            ));
        let preauth_inflight = self
            .admission
            .lock()
            .map(|admission| admission.inflight_global)
            .unwrap_or(0);
        ResponderBrokerStatus {
            schema: RESPONDER_BROKER_STATUS_SCHEMA_V1,
            state,
            bound_address: (state == ResponderBrokerState::Listening)
                .then(|| self.bound_address.to_string()),
            registered_routes: self.routes.route_count(),
            preauth_inflight,
            accepted_connections: accepted,
            authenticated_sessions: authenticated,
            rejected_connections: rejected,
            last_error_code: last_error,
            application_hello_performed: hello_performed,
            anti_entropy_performed: false,
            synchronized: false,
        }
    }

    #[must_use]
    pub fn recent_audit_events(&self) -> Vec<ResponderBrokerAuditEvent> {
        self.runtime
            .lock()
            .map(|runtime| runtime.recent_audit.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Stop future accepts and drop the listener. An in-flight accept is
    /// stopped by cancelling its `Cx`; callers then invoke this after join.
    pub fn shutdown(&mut self) {
        self.listener.take();
        if let Ok(mut runtime) = self.runtime.lock() {
            runtime.state = ResponderBrokerState::Shutdown;
        }
    }
}

/// One user-scoped owner for the complete LocalAPI address set.
///
/// The owner periodically re-reads status while serving. Address loss closes
/// every stale listener before retry; a changed address set or local node-key
/// rotation is rebound atomically from the owner's perspective. It never
/// binds loopback, wildcard, or a non-LocalAPI address.
pub struct ResponderBrokerOwner<A> {
    local_api: Arc<A>,
    routes: ResponderRouteRegistry,
    durable_registrations: Option<Vec<DurableResponderRegistration>>,
    admission_limits: PreAuthAdmissionLimits,
    admission: Arc<Mutex<AdmissionState>>,
    brokers: Vec<ResponderBroker<Arc<A>>>,
    bound_addresses: Vec<SocketAddr>,
    revalidate_interval: Duration,
    last_revalidated_at: Instant,
    #[cfg(any(unix, windows))]
    control: Option<ResponderControlListener>,
}

impl<A: TailscaleLocalApi> ResponderBrokerOwner<A> {
    pub async fn start(
        cx: &Cx,
        local_api: A,
        routes: ResponderRouteRegistry,
        admission_limits: PreAuthAdmissionLimits,
        revalidate_interval: Duration,
    ) -> Result<Self, ResponderBrokerError> {
        if !(MIN_OWNER_REVALIDATE_INTERVAL..=MAX_OWNER_REVALIDATE_INTERVAL)
            .contains(&revalidate_interval)
        {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let admission_limits = admission_limits.validate()?;
        let mut owner = Self {
            local_api: Arc::new(local_api),
            routes,
            durable_registrations: None,
            admission_limits,
            admission: Arc::new(Mutex::new(AdmissionState::new(admission_limits))),
            brokers: Vec::new(),
            bound_addresses: Vec::new(),
            revalidate_interval,
            last_revalidated_at: Instant::now(),
            #[cfg(any(unix, windows))]
            control: None,
        };
        owner.reconcile(cx).await?;
        Ok(owner)
    }

    /// Start the production owner from durable local registration scope.
    /// Every reconciliation re-resolves LocalAPI identity, the peer principal
    /// and key generation, the lane-grant generation, and T2.1's public
    /// pair-key seam before retaining or rebinding listeners.
    pub async fn start_durable(
        cx: &Cx,
        local_api: A,
        registrations: Vec<DurableResponderRegistration>,
        admission_limits: PreAuthAdmissionLimits,
        revalidate_interval: Duration,
    ) -> Result<Self, ResponderBrokerError> {
        if registrations.is_empty() {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let local_api = Arc::new(local_api);
        let routes = resolve_durable_route_registry(cx, local_api.as_ref(), &registrations).await?;
        let mut owner =
            Self::start_with_arc(cx, local_api, routes, admission_limits, revalidate_interval)
                .await?;
        owner.durable_registrations = Some(registrations);
        Ok(owner)
    }

    async fn start_with_arc(
        cx: &Cx,
        local_api: Arc<A>,
        routes: ResponderRouteRegistry,
        admission_limits: PreAuthAdmissionLimits,
        revalidate_interval: Duration,
    ) -> Result<Self, ResponderBrokerError> {
        if !(MIN_OWNER_REVALIDATE_INTERVAL..=MAX_OWNER_REVALIDATE_INTERVAL)
            .contains(&revalidate_interval)
        {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let admission_limits = admission_limits.validate()?;
        let mut owner = Self {
            local_api,
            routes,
            durable_registrations: None,
            admission_limits,
            admission: Arc::new(Mutex::new(AdmissionState::new(admission_limits))),
            brokers: Vec::new(),
            bound_addresses: Vec::new(),
            revalidate_interval,
            last_revalidated_at: Instant::now(),
            #[cfg(any(unix, windows))]
            control: None,
        };
        owner.reconcile(cx).await?;
        Ok(owner)
    }

    #[must_use]
    pub fn bound_addresses(&self) -> &[SocketAddr] {
        &self.bound_addresses
    }

    #[must_use]
    pub fn route_count(&self) -> usize {
        self.routes.route_count()
    }

    /// Return the currently bound pair, transport-key, and grant generations
    /// for one opaque peer handle. This is a secret-free owner diagnostic and
    /// proves reconciliation actually replaced stale durable authority.
    #[must_use]
    pub fn route_generations(&self, peer_handle: &str) -> Option<(u64, u64, u64)> {
        self.routes
            .routes
            .values()
            .find(|route| route.peer_handle == peer_handle)
            .map(|route| {
                (
                    route.expectations.pair_key_generation,
                    route.peer_transport_key_generation,
                    route.grant_generation,
                )
            })
    }

    /// Publish the same-user control channel used by another local workspace
    /// to register exact routes with this owner.
    #[cfg(any(unix, windows))]
    pub fn listen_control(
        &mut self,
        socket_path: impl Into<PathBuf>,
    ) -> Result<(), ResponderBrokerError> {
        self.control = Some(ResponderControlListener::publish(socket_path.into())?);
        Ok(())
    }

    #[cfg(any(unix, windows))]
    #[must_use]
    pub fn control_socket_path(&self) -> Option<&Path> {
        self.control
            .as_ref()
            .map(|control| control.socket_path.as_path())
    }

    /// Accept and answer one inbound connection, then return.
    ///
    /// Unsigned hello/join is a successful one-shot: the reply is written
    /// before `BootstrapHelloAnswered`. Authenticated EventFetch/Summary
    /// runs the same serve path as [`Self::serve_until_cancelled`].
    pub async fn serve_one(&self, cx: &Cx) -> Result<(), ResponderBrokerError> {
        let broker = self
            .brokers
            .first()
            .ok_or(ResponderBrokerError::TransportUnavailable)?;
        match broker.accept_authenticated_and_serve(cx).await {
            Ok(()) => Ok(()),
            Err(ResponderBrokerError::BootstrapHelloAnswered) => Ok(()),
            Err(error) => Err(error),
        }
    }

    /// Serve authenticated transport sessions until the caller context is
    /// cancelled. Each accepted session answers one EventFetch/Summary
    /// sync-round from the registered origin store, then closes.
    pub async fn serve_until_cancelled(&mut self, cx: &Cx) -> Result<(), ResponderBrokerError> {
        let mut listener_index = 0_usize;
        loop {
            checkpoint(cx, "responder owner")?;
            if self.brokers.is_empty() {
                match self.reconcile(cx).await {
                    Ok(()) => {}
                    Err(ResponderBrokerError::Cancelled) => {
                        self.shutdown();
                        return Err(ResponderBrokerError::Cancelled);
                    }
                    Err(_) => {
                        asupersync_sleep(cx.now(), self.revalidate_interval).await;
                        continue;
                    }
                }
            }
            #[cfg(any(unix, windows))]
            self.poll_control(cx).await;
            if self.brokers.is_empty() {
                asupersync_sleep(cx.now(), self.revalidate_interval).await;
                continue;
            }
            listener_index %= self.brokers.len();
            let broker = &self.brokers[listener_index];
            let now = wall_now();
            // When a same-user control listener is published, poll it often
            // enough that status/register do not race the hello accept budget.
            #[cfg(any(unix, windows))]
            let accept_budget = if self.control.is_some() {
                Duration::from_millis(50)
            } else {
                self.revalidate_interval
            };
            #[cfg(not(any(unix, windows)))]
            let accept_budget = self.revalidate_interval;
            let accept_timed_out = match timeout(
                now,
                accept_budget,
                broker.accept_authenticated_and_serve(cx),
            )
            .await
            {
                Ok(Ok(())) => false,
                Ok(Err(ResponderBrokerError::Cancelled)) => {
                    self.shutdown();
                    return Err(ResponderBrokerError::Cancelled);
                }
                Ok(Err(_rejected)) => false,
                Err(_) => true,
            };
            if (accept_timed_out || self.last_revalidated_at.elapsed() >= self.revalidate_interval)
                && let Err(error) = self.reconcile(cx).await
            {
                self.shutdown();
                if matches!(error, ResponderBrokerError::Cancelled) {
                    return Err(error);
                }
                asupersync_sleep(cx.now(), self.revalidate_interval).await;
            }
            listener_index = listener_index.saturating_add(1);
        }
    }

    pub async fn reconcile(&mut self, cx: &Cx) -> Result<(), ResponderBrokerError> {
        let status = match self.local_api.local_status(cx).await {
            Ok(status) => status,
            Err(error) => {
                self.shutdown_listeners();
                return Err(
                    if matches!(
                        error,
                        ResponderBrokerError::WhoIsUnavailable
                            | ResponderBrokerError::TransportUnavailable
                    ) {
                        ResponderBrokerError::TransportUnavailable
                    } else {
                        error
                    },
                );
            }
        };
        let refreshed_routes = if let Some(registrations) = &self.durable_registrations {
            match resolve_durable_route_registry(cx, self.local_api.as_ref(), registrations).await {
                Ok(routes) => Some(routes),
                Err(error) => {
                    self.shutdown_listeners();
                    return Err(error);
                }
            }
        } else {
            None
        };
        let routes_changed = refreshed_routes
            .as_ref()
            .is_some_and(|routes| !same_route_authority(&self.routes, routes));
        if let Some(routes) = refreshed_routes {
            self.routes = routes;
        }
        if !crate::mesh::team::team_join_stable_id_matches(
            &self.routes.responder_stable_id,
            &status.identity.stable_id,
        ) || !crate::mesh::team::team_join_tailnet_matches(
            &self.routes.tailnet_id,
            &status.identity.tailnet_id,
        ) {
            self.shutdown_listeners();
            return Err(ResponderBrokerError::WhoIsUnverified);
        }
        let mut desired = status
            .addresses
            .iter()
            .copied()
            .map(|ip| SocketAddr::new(ip, self.routes.committed_port))
            .collect::<Vec<_>>();
        desired.sort();
        desired.dedup();
        if desired.is_empty()
            || desired.iter().any(|address| {
                address.ip().is_unspecified()
                    || (address.ip().is_loopback() && !self.local_api.allows_loopback_bind())
            })
        {
            self.shutdown_listeners();
            return Err(ResponderBrokerError::TransportUnavailable);
        }
        let key_changed = status.identity.current_node_pubkey != self.routes.responder_node_pubkey;
        if desired == self.bound_addresses && !key_changed && !routes_changed {
            self.last_revalidated_at = Instant::now();
            return Ok(());
        }

        self.shutdown_listeners();
        let mut routes = self.routes.clone();
        if key_changed {
            routes.responder_node_pubkey = status.identity.current_node_pubkey.clone();
            for route in routes.routes.values_mut() {
                route.responder_node_pubkey = status.identity.current_node_pubkey.clone();
            }
        }
        let mut brokers = Vec::with_capacity(desired.len());
        for address in &desired {
            match ResponderBroker::bind(
                cx,
                *address,
                Arc::clone(&self.local_api),
                routes.clone(),
                self.admission_limits,
            )
            .await
            {
                Ok(mut broker) => {
                    broker.admission = Arc::clone(&self.admission);
                    brokers.push(broker);
                }
                Err(error) => {
                    for broker in &mut brokers {
                        broker.shutdown();
                    }
                    return Err(error);
                }
            }
        }
        self.routes = routes;
        self.bound_addresses = desired;
        self.brokers = brokers;
        self.last_revalidated_at = Instant::now();
        Ok(())
    }

    pub fn shutdown(&mut self) {
        self.shutdown_listeners();
    }

    fn shutdown_listeners(&mut self) {
        for broker in &mut self.brokers {
            broker.shutdown();
        }
        self.brokers.clear();
        self.bound_addresses.clear();
    }

    #[cfg(unix)]
    async fn poll_control(&mut self, cx: &Cx) {
        let Some(control) = self.control.as_ref() else {
            return;
        };
        let accepted = match timeout(
            wall_now(),
            Duration::from_millis(50),
            control.listener.accept(),
        )
        .await
        {
            Ok(Ok((stream, _))) => stream,
            Ok(Err(_)) | Err(_) => return,
        };
        let (mut stream, request) = match read_control_request(cx, accepted).await {
            Ok(read) => read,
            Err(_) => return,
        };
        let response = self.dispatch_control(cx, request).await;
        let _ = write_control_response(&mut stream, &response).await;
    }

    #[cfg(windows)]
    async fn poll_control(&mut self, cx: &Cx) {
        let Some(control) = self.control.as_ref() else {
            return;
        };
        let (mut stream, peer) = match control.listener.accept() {
            Ok(accepted) => accepted,
            Err(_) => return,
        };
        if !peer.ip().is_loopback() {
            return;
        }
        let _ = stream.set_nonblocking(false);
        let _ = stream.set_read_timeout(Some(Duration::from_secs(2)));
        let _ = stream.set_write_timeout(Some(Duration::from_secs(2)));
        let request = match read_control_frame(&mut stream) {
            Ok(request) => request,
            Err(_) => return,
        };
        if validate_control_request(&request).is_err() {
            return;
        }
        let response = self.dispatch_control(cx, request).await;
        let _ = write_control_frame(&mut stream, &response);
    }

    #[cfg(any(unix, windows))]
    async fn dispatch_control(
        &mut self,
        cx: &Cx,
        request: ResponderControlRequest,
    ) -> ResponderControlResponse {
        let nonce = request.nonce.clone();
        let outcome = match request.op {
            ResponderControlOp::Status => Ok(()),
            ResponderControlOp::Register => self.apply_control_register(cx, &request).await,
            ResponderControlOp::Unregister => self.apply_control_unregister(cx, &request).await,
        };
        match outcome {
            Ok(()) => ResponderControlResponse {
                schema: RESPONDER_CONTROL_SCHEMA_V1.to_owned(),
                ok: true,
                nonce,
                code: None,
                message: None,
                bound_addresses: self
                    .bound_addresses
                    .iter()
                    .map(ToString::to_string)
                    .collect(),
                registered_routes: self.route_count(),
            },
            Err(error) => ResponderControlResponse {
                schema: RESPONDER_CONTROL_SCHEMA_V1.to_owned(),
                ok: false,
                nonce,
                code: Some(error.code().to_owned()),
                message: Some(error.message()),
                bound_addresses: self
                    .bound_addresses
                    .iter()
                    .map(ToString::to_string)
                    .collect(),
                registered_routes: self.route_count(),
            },
        }
    }

    #[cfg(any(unix, windows))]
    async fn apply_control_register(
        &mut self,
        cx: &Cx,
        request: &ResponderControlRequest,
    ) -> Result<(), ResponderBrokerError> {
        let incoming = materialize_control_registrations(request)?;
        let mut next = self.durable_registrations.clone().unwrap_or_default();
        if let Some(existing) = next.first()
            && existing.committed_port != request.committed_port
        {
            return Err(ResponderBrokerError::PortConflict);
        }
        for registration in incoming {
            next.retain(|row| {
                !(row.workspace_id == registration.workspace_id
                    && row.team_id == registration.team_id
                    && row.peer_handle == registration.peer_handle)
            });
            next.push(registration);
        }
        let routes = resolve_durable_route_registry(cx, self.local_api.as_ref(), &next).await?;
        self.durable_registrations = Some(next);
        self.routes = routes;
        self.reconcile(cx).await
    }

    #[cfg(any(unix, windows))]
    async fn apply_control_unregister(
        &mut self,
        cx: &Cx,
        request: &ResponderControlRequest,
    ) -> Result<(), ResponderBrokerError> {
        let Some(mut next) = self.durable_registrations.clone() else {
            return Err(ResponderBrokerError::RouteUnavailable);
        };
        let before = next.len();
        next.retain(|row| {
            !(row.workspace_id == request.workspace_id
                && row.team_id == request.team_id
                && request.peer_handles.contains(&row.peer_handle))
        });
        if next.len() == before {
            return Err(ResponderBrokerError::RouteUnavailable);
        }
        if next.is_empty() {
            self.durable_registrations = Some(next);
            self.shutdown_listeners();
            return Ok(());
        }
        let routes = resolve_durable_route_registry(cx, self.local_api.as_ref(), &next).await?;
        self.durable_registrations = Some(next);
        self.routes = routes;
        self.reconcile(cx).await
    }
}

async fn resolve_durable_route_registry<A: TailscaleLocalApi>(
    cx: &Cx,
    local_api: &A,
    registrations: &[DurableResponderRegistration],
) -> Result<ResponderRouteRegistry, ResponderBrokerError> {
    let mut routes = Vec::with_capacity(registrations.len());
    for registration in registrations {
        let connection = DbConnection::open_file(&registration.database_path)
            .map_err(|_| ResponderBrokerError::RouteUnavailable)?;
        routes.push(
            resolve_durable_registration(cx, local_api, &connection, registration)
                .await?
                .route,
        );
    }
    ResponderRouteRegistry::new(routes)
}

fn same_route_authority(
    current: &ResponderRouteRegistry,
    refreshed: &ResponderRouteRegistry,
) -> bool {
    if current.committed_port != refreshed.committed_port
        || current.tailnet_id != refreshed.tailnet_id
        || current.responder_stable_id != refreshed.responder_stable_id
        || current.responder_node_pubkey != refreshed.responder_node_pubkey
        || current.routes.len() != refreshed.routes.len()
    {
        return false;
    }
    current.routes.iter().all(|(selectors, route)| {
        refreshed.routes.get(selectors).is_some_and(|candidate| {
            route.workspace_path == candidate.workspace_path
                && route.database_path == candidate.database_path
                && route.peer_handle == candidate.peer_handle
                && route.committed_port == candidate.committed_port
                && route.expectations == candidate.expectations
                && route.responder_node_pubkey == candidate.responder_node_pubkey
                && route.peer_transport_key_generation == candidate.peer_transport_key_generation
                && route.grant_generation == candidate.grant_generation
                && route.capabilities == candidate.capabilities
                && route.limits == candidate.limits
        })
    })
}

async fn resolve_route<A: TailscaleLocalApi>(
    cx: &Cx,
    local_api: &A,
    routes: &ResponderRouteRegistry,
    source: SocketAddr,
    selectors: &UntrustedRouteSelectors,
    permit: PreAuthPermit,
    route_selected: &AtomicBool,
) -> Result<ResolvedAcceptedRoute<PreAuthPermit>, ResponderBrokerError> {
    let route = routes
        .resolve(selectors)
        .ok_or(ResponderBrokerError::RouteUnavailable)?;
    route_selected.store(true, Ordering::Release);
    let who_is = local_api.who_is(cx, source).await?;
    if !crate::mesh::team::team_join_stable_id_matches(
        &route.expectations.initiator_stable_id,
        &who_is.stable_id,
    ) {
        return Err(ResponderBrokerError::WhoIsUnverified);
    }
    refresh_durable_route_authority(route, &who_is)?;
    let pair_key = load_route_pair_key(route)?;
    let source_attestation = AcceptedSourceAttestation::from_local_whois(
        source.ip(),
        route.expectations.tailnet_id.clone(),
        who_is.stable_id,
        who_is.current_node_pubkey.clone(),
    )
    .map_err(ResponderBrokerError::Session)?;
    Ok(ResolvedAcceptedRoute::new(
        AcceptedSessionConfig {
            expectations: route.expectations.clone(),
            pair_key,
            observations: HandshakeObservations {
                initiator_node_pubkey: who_is.current_node_pubkey,
                responder_node_pubkey: route.responder_node_pubkey.clone(),
            },
            capabilities: route.capabilities.clone(),
            limits: route.limits,
        },
        source_attestation,
        permit,
    ))
}

fn refresh_durable_route_authority(
    route: &RegisteredResponderRoute,
    who_is: &WhoIsIdentity,
) -> Result<(), ResponderBrokerError> {
    let Some(database_path) = route.database_path.as_ref() else {
        return Ok(());
    };
    if !database_path.is_file() {
        return Err(ResponderBrokerError::RouteUnavailable);
    }
    let connection = DbConnection::open_file(database_path)
        .map_err(|_| ResponderBrokerError::RouteUnavailable)?;
    let peer = connection
        .observe_mesh_peer_transport_identity(&ObserveMeshPeerTransportIdentityInput {
            workspace_id: route.expectations.responder_workspace_id.clone(),
            peer_id: route.peer_handle.clone(),
            tailnet_id: route.expectations.tailnet_id.clone(),
            stable_node_id: who_is.stable_id.clone(),
            current_node_pubkey: who_is.current_node_pubkey.clone(),
            observed_at: None,
        })
        .map_err(map_peer_identity_error)?;
    if peer
        .transport_identity
        .as_ref()
        .is_none_or(|identity| identity.key_generation != route.peer_transport_key_generation)
    {
        return Err(ResponderBrokerError::WhoIsUnverified);
    }
    let grant = connection
        .get_mesh_lane_grant_state(
            &route.expectations.responder_workspace_id,
            &route.peer_handle,
        )
        .map_err(|_| ResponderBrokerError::RouteUnavailable)?;
    if route.grant_generation == 0 {
        if grant.is_some_and(|grant| grant.grant_generation > 0) {
            return Err(ResponderBrokerError::RouteUnavailable);
        }
        return Ok(());
    }
    let Some(grant) = grant.filter(|grant| {
        grant.target_matches_current_peer && grant.grant_generation == route.grant_generation
    }) else {
        return Err(ResponderBrokerError::RouteUnavailable);
    };
    if grant.target_adapter.peer_id != route.peer_handle
        || grant.target_adapter.origin_node_id != route.expectations.initiator_node_id
    {
        return Err(ResponderBrokerError::RouteUnavailable);
    }
    Ok(())
}

fn load_route_pair_key(
    route: &RegisteredResponderRoute,
) -> Result<crate::mesh::key_store::SecretBytes, ResponderBrokerError> {
    let store = MeshKeyStore::open_existing(&route.workspace_path)
        .map_err(map_key_store_error)?
        .ok_or(ResponderBrokerError::KeyStoreUnavailable)?;
    let pair_record = store
        .load_pair_key(&route.peer_handle, PairKeyClass::Current)
        .map_err(map_key_store_error)?
        .ok_or(ResponderBrokerError::PairingRequired)?;
    if pair_record.generation.get() != route.expectations.pair_key_generation {
        return Err(ResponderBrokerError::PairingRequired);
    }
    Ok(pair_record.key)
}

fn map_key_store_error(_error: KeyStoreError) -> ResponderBrokerError {
    ResponderBrokerError::KeyStoreUnavailable
}

fn valid_identity(value: &str) -> bool {
    let trimmed = value.trim();
    !trimmed.is_empty()
        && trimmed.len() <= 256
        && trimmed == value
        && !value.chars().any(char::is_control)
}

fn valid_node_key(value: &str) -> bool {
    valid_identity(value)
        && value
            .strip_prefix("nodekey:")
            .is_some_and(|key| !key.is_empty() && !key.chars().any(char::is_whitespace))
}

fn valid_opaque_peer_handle(value: &str) -> bool {
    value.strip_prefix("peer_").is_some_and(|opaque| {
        opaque.len() == 32
            && opaque
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    })
}

fn valid_durable_node_principal(value: &str) -> bool {
    value.strip_prefix("node_").is_some_and(|opaque| {
        opaque.len() == 32
            && opaque
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    })
}

/// User-scoped control socket path. Mirrors the daemon partition:
/// private `XDG_RUNTIME_DIR/ee/mesh-responder.sock`, otherwise
/// `${TMPDIR:-/tmp}/ee-${uid}/mesh-responder.sock`.
#[must_use]
pub fn default_responder_control_socket_path() -> PathBuf {
    default_responder_control_socket_path_with(
        |key| std::env::var_os(key),
        current_responder_euid(),
    )
}

fn default_responder_control_socket_path_with(
    mut env_var: impl FnMut(&str) -> Option<std::ffi::OsString>,
    uid: u32,
) -> PathBuf {
    if cfg!(windows) {
        if let Some(local) = env_var("LOCALAPPDATA").filter(|value| !value.is_empty()) {
            return Path::new(&local)
                .join("eidetic-engine")
                .join("mesh-responder.control");
        }
        if let Some(profile) = env_var("USERPROFILE").filter(|value| !value.is_empty()) {
            return Path::new(&profile)
                .join("AppData")
                .join("Local")
                .join("eidetic-engine")
                .join("mesh-responder.control");
        }
    }
    let tmp = env_var("TMPDIR").unwrap_or_else(|| "/tmp".into());
    if let Some(runtime_dir) = env_var("XDG_RUNTIME_DIR") {
        let runtime = Path::new(&runtime_dir);
        if !runtime.as_os_str().is_empty()
            && runtime != Path::new("/tmp")
            && runtime != Path::new("/var/tmp")
            && runtime != Path::new("/private/tmp")
        {
            return runtime.join("ee").join("mesh-responder.sock");
        }
    }
    Path::new(&tmp)
        .join(format!("ee-{uid}"))
        .join("mesh-responder.sock")
}

#[cfg(unix)]
fn current_responder_euid() -> u32 {
    rustix::process::geteuid().as_raw()
}

#[cfg(not(unix))]
fn current_responder_euid() -> u32 {
    0
}

/// Submit one same-EUID control request to a published owner socket.
#[cfg(unix)]
pub fn submit_responder_control_request(
    socket_path: &Path,
    request: &ResponderControlRequest,
) -> Result<ResponderControlResponse, ResponderBrokerError> {
    validate_control_request(request)?;
    refuse_insecure_socket_path(socket_path)?;
    let mut stream = std::os::unix::net::UnixStream::connect(socket_path)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    let peer = control_std_peer_uid(&stream)?;
    if peer != current_responder_euid() {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    write_control_frame(&mut stream, request)?;
    read_control_frame(&mut stream)
}

#[cfg(unix)]
struct ResponderControlListener {
    listener: UnixListener,
    socket_path: PathBuf,
}

#[cfg(unix)]
impl ResponderControlListener {
    fn publish(socket_path: PathBuf) -> Result<Self, ResponderBrokerError> {
        publish_control_socket(&socket_path)?;
        let std_listener = std::os::unix::net::UnixListener::bind(&socket_path)
            .map_err(|_| ResponderBrokerError::PortConflict)?;
        if let Err(error) = fs::set_permissions(&socket_path, fs::Permissions::from_mode(0o600)) {
            let _ = remove_owned_socket(&socket_path);
            let _ = error;
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        // Bind-then-chmod is the same contract as the daemon: parent is 0700,
        // so no other UID can connect during the brief window.
        let listener = UnixListener::from_std(std_listener)
            .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
        Ok(Self {
            listener,
            socket_path,
        })
    }
}

#[cfg(unix)]
impl Drop for ResponderControlListener {
    fn drop(&mut self) {
        let _ = remove_owned_socket(&self.socket_path);
    }
}

/// Loopback TCP control listener published through an owner-only endpoint
/// file. Named-pipe listen is still a later slice; this is the same-user
/// Windows control transport leftover from `bd-tc-followup-oo7d2.3`.
#[cfg(windows)]
struct ResponderControlListener {
    listener: std::net::TcpListener,
    socket_path: PathBuf,
}

#[cfg(windows)]
impl ResponderControlListener {
    fn publish(socket_path: PathBuf) -> Result<Self, ResponderBrokerError> {
        let listener = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
            .map_err(|_| ResponderBrokerError::PortConflict)?;
        listener
            .set_nonblocking(true)
            .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
        let port = listener
            .local_addr()
            .map_err(|_| ResponderBrokerError::InvalidConfiguration)?
            .port();
        write_windows_control_endpoint(&socket_path, port)?;
        Ok(Self {
            listener,
            socket_path,
        })
    }
}

#[cfg(windows)]
impl Drop for ResponderControlListener {
    fn drop(&mut self) {
        quarantine_windows_control_endpoint(&self.socket_path);
    }
}

#[cfg(windows)]
const WINDOWS_CONTROL_ENDPOINT_SCHEMA: &str = "ee.mesh.responder.control.endpoint.v1";

#[cfg(windows)]
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct WindowsControlEndpoint {
    schema: String,
    transport: String,
    host: String,
    port: u16,
}

#[cfg(windows)]
fn windows_control_dir_parts(
    socket_path: &Path,
) -> Result<(PathBuf, PathBuf, String), ResponderBrokerError> {
    let file_name = socket_path
        .file_name()
        .and_then(|name| name.to_str())
        .ok_or(ResponderBrokerError::InvalidConfiguration)?
        .to_owned();
    let dir = socket_path
        .parent()
        .ok_or(ResponderBrokerError::InvalidConfiguration)?;
    let boundary = dir
        .parent()
        .ok_or(ResponderBrokerError::InvalidConfiguration)?;
    Ok((boundary.to_path_buf(), dir.to_path_buf(), file_name))
}

#[cfg(windows)]
fn write_windows_control_endpoint(
    socket_path: &Path,
    port: u16,
) -> Result<(), ResponderBrokerError> {
    let (boundary, dir, file_name) = windows_control_dir_parts(socket_path)?;
    let secure = crate::mesh::key_store::SecureLocalDir::open_or_create(boundary, dir)
        .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    let endpoint = WindowsControlEndpoint {
        schema: WINDOWS_CONTROL_ENDPOINT_SCHEMA.to_owned(),
        transport: "loopback_tcp".to_owned(),
        host: Ipv4Addr::LOCALHOST.to_string(),
        port,
    };
    let bytes = serde_json::to_vec_pretty(&endpoint)
        .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    secure
        .write_replace_capped(&file_name, &bytes, crate::mesh::key_store::MAX_RECORD_BYTES)
        .map_err(|_| ResponderBrokerError::InvalidConfiguration)
}

#[cfg(windows)]
fn read_windows_control_endpoint(
    socket_path: &Path,
) -> Result<WindowsControlEndpoint, ResponderBrokerError> {
    let (boundary, dir, file_name) = windows_control_dir_parts(socket_path)?;
    let secure = crate::mesh::key_store::SecureLocalDir::open_existing(boundary, dir)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?
        .ok_or(ResponderBrokerError::TransportUnavailable)?;
    let bytes = secure
        .read(&file_name)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?
        .ok_or(ResponderBrokerError::TransportUnavailable)?;
    let endpoint: WindowsControlEndpoint =
        serde_json::from_slice(&bytes).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    if endpoint.schema != WINDOWS_CONTROL_ENDPOINT_SCHEMA
        || endpoint.transport != "loopback_tcp"
        || endpoint.host != Ipv4Addr::LOCALHOST.to_string()
        || endpoint.port < 1024
    {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    Ok(endpoint)
}

#[cfg(windows)]
fn quarantine_windows_control_endpoint(socket_path: &Path) {
    let Ok((boundary, dir, file_name)) = windows_control_dir_parts(socket_path) else {
        return;
    };
    let Ok(Some(secure)) = crate::mesh::key_store::SecureLocalDir::open_existing(boundary, dir)
    else {
        return;
    };
    let stamp = chrono::Utc::now().format("%Y%m%dT%H%M%SZ");
    let retired = format!("{file_name}.quarantined-{stamp}");
    let _ = secure.rename(&file_name, &retired);
}

/// Submit one same-user control request to a published owner endpoint.
#[cfg(windows)]
pub fn submit_responder_control_request(
    socket_path: &Path,
    request: &ResponderControlRequest,
) -> Result<ResponderControlResponse, ResponderBrokerError> {
    validate_control_request(request)?;
    let endpoint = read_windows_control_endpoint(socket_path)?;
    let mut stream = std::net::TcpStream::connect((Ipv4Addr::LOCALHOST, endpoint.port))
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
    let _ = stream.set_write_timeout(Some(Duration::from_secs(5)));
    write_control_frame(&mut stream, request)?;
    read_control_frame(&mut stream)
}

#[must_use]
pub fn responder_control_status_request() -> ResponderControlRequest {
    ResponderControlRequest {
        schema: RESPONDER_CONTROL_SCHEMA_V1.to_owned(),
        op: ResponderControlOp::Status,
        nonce: "0".repeat(CONTROL_NONCE_HEX_LEN),
        workspace_id: String::new(),
        team_id: String::new(),
        responder_node_id: String::new(),
        workspace_path: PathBuf::new(),
        database_path: PathBuf::new(),
        peer_handles: Vec::new(),
        committed_port: 0,
    }
}

fn validate_control_request(request: &ResponderControlRequest) -> Result<(), ResponderBrokerError> {
    if request.schema != RESPONDER_CONTROL_SCHEMA_V1
        || request.nonce.len() < CONTROL_NONCE_HEX_LEN
        || !request.nonce.bytes().all(|byte| byte.is_ascii_hexdigit())
    {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    if request.op == ResponderControlOp::Status {
        return Ok(());
    }
    if !valid_identity(&request.workspace_id)
        || !valid_identity(&request.team_id)
        || !valid_identity(&request.responder_node_id)
        || request.committed_port < 1024
        || request.peer_handles.is_empty()
        || request.peer_handles.len() > 32
        || !request
            .peer_handles
            .iter()
            .all(|handle| valid_opaque_peer_handle(handle))
    {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    revalidate_control_paths(request)
}

fn revalidate_control_paths(request: &ResponderControlRequest) -> Result<(), ResponderBrokerError> {
    let workspace = owner_safe_canonical_path(&request.workspace_path)?;
    let database = owner_safe_canonical_path(&request.database_path)?;
    if !paths_are_same_location(&database, &workspace.join(".ee").join("ee.db")) {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    Ok(())
}

fn path_matches_canonical_form(path: &Path) -> bool {
    path.canonicalize()
        .ok()
        .is_some_and(|canonical| paths_are_same_location(&canonical, path))
}

fn path_matches_canonical_location(observed: &Path, expected: &Path) -> bool {
    observed
        .canonicalize()
        .ok()
        .is_some_and(|canonical| paths_are_same_location(&canonical, expected))
}

/// Compare two paths for pointing at the same location.
///
/// This is deliberately more than `left == right`. On Windows
/// `Path::canonicalize` returns the verbatim extended-length form
/// (`\\?\C:\...`), so a canonicalized path never compares equal to the
/// plain path a caller supplied, and NTFS is case-insensitive besides.
/// Both callers here compare a canonicalized path against a non-canonical
/// one, so dropping the normalization makes every Windows comparison false
/// and turns `owner_safe_canonical_path` into an unconditional
/// `InvalidConfiguration`. Keep the platform branch.
#[cfg(not(windows))]
fn paths_are_same_location(left: &Path, right: &Path) -> bool {
    left == right
}

#[cfg(windows)]
fn paths_are_same_location(left: &Path, right: &Path) -> bool {
    left == right
        || normalize_windows_path(left).eq_ignore_ascii_case(&normalize_windows_path(right))
}

#[cfg(windows)]
fn normalize_windows_path(path: &Path) -> String {
    let rendered = path.to_string_lossy();
    let stripped = if let Some(rest) = rendered.strip_prefix(r"\\?\UNC\") {
        format!(r"\\{rest}")
    } else if let Some(rest) = rendered.strip_prefix(r"\\?\") {
        rest.to_owned()
    } else {
        rendered.into_owned()
    };
    stripped.replace('/', r"\")
}

fn owner_safe_canonical_path(path: &Path) -> Result<PathBuf, ResponderBrokerError> {
    if !path.is_absolute() {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    #[cfg(unix)]
    {
        let metadata =
            fs::symlink_metadata(path).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
        if metadata.file_type().is_symlink() || metadata.uid() != current_responder_euid() {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        let canonical = path
            .canonicalize()
            .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
        if canonical != path {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        return Ok(canonical);
    }
    #[cfg(windows)]
    {
        // Walk the stripped form so `\\?\C:\...` from Path::canonicalize
        // does not fail metadata on the verbatim prefix.
        let walk_root = PathBuf::from(normalize_windows_path(path));
        let mut current = PathBuf::new();
        for component in walk_root.components() {
            current.push(component);
            if windows_path_component_is_reparse(&current)? {
                return Err(ResponderBrokerError::InvalidConfiguration);
            }
        }
        let canonical = path
            .canonicalize()
            .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
        if !paths_are_same_location(&canonical, path) {
            return Err(ResponderBrokerError::InvalidConfiguration);
        }
        return Ok(canonical);
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = path;
        Err(ResponderBrokerError::PlatformUnsupported)
    }
}

#[cfg(windows)]
fn windows_path_component_is_reparse(path: &Path) -> Result<bool, ResponderBrokerError> {
    use std::os::windows::fs::MetadataExt;
    const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0000_0400;
    let metadata =
        fs::symlink_metadata(path).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    Ok(metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0)
}

fn materialize_control_registrations(
    request: &ResponderControlRequest,
) -> Result<Vec<DurableResponderRegistration>, ResponderBrokerError> {
    validate_control_request(request)?;
    let workspace_path = owner_safe_canonical_path(&request.workspace_path)?;
    let database_path = owner_safe_canonical_path(&request.database_path)?;
    Ok(request
        .peer_handles
        .iter()
        .map(|peer_handle| DurableResponderRegistration {
            workspace_path: workspace_path.clone(),
            database_path: database_path.clone(),
            workspace_id: request.workspace_id.clone(),
            team_id: request.team_id.clone(),
            responder_node_id: request.responder_node_id.clone(),
            peer_handle: peer_handle.clone(),
            committed_port: request.committed_port,
            capabilities: SessionCapabilities::base(),
            limits: SessionChannelLimits::default(),
        })
        .collect())
}

#[cfg(unix)]
fn publish_control_socket(socket_path: &Path) -> Result<(), ResponderBrokerError> {
    let parent = socket_path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .ok_or(ResponderBrokerError::InvalidConfiguration)?;
    fs::DirBuilder::new()
        .recursive(true)
        .mode(0o700)
        .create(parent)
        .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    let parent_meta =
        fs::symlink_metadata(parent).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    if parent_meta.file_type().is_symlink()
        || !parent_meta.file_type().is_dir()
        || parent_meta.uid() != current_responder_euid()
        || parent_meta.mode() & 0o077 != 0
    {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    match fs::symlink_metadata(socket_path) {
        Ok(metadata) if metadata.file_type().is_socket() => {
            let _ = remove_owned_socket(socket_path);
        }
        Ok(_) => return Err(ResponderBrokerError::PortConflict),
        Err(error) if error.kind() == io::ErrorKind::NotFound => {}
        Err(_) => return Err(ResponderBrokerError::InvalidConfiguration),
    }
    Ok(())
}

#[cfg(unix)]
fn remove_owned_socket(path: &Path) -> io::Result<()> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_socket() => fs::remove_file(path),
        Ok(_) => Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "refusing to remove a non-socket control path",
        )),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error),
    }
}

#[cfg(unix)]
fn refuse_insecure_socket_path(path: &Path) -> Result<(), ResponderBrokerError> {
    let metadata =
        fs::symlink_metadata(path).map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    if !metadata.file_type().is_socket() || metadata.uid() != current_responder_euid() {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    Ok(())
}

#[cfg(unix)]
fn control_std_peer_uid(
    stream: &std::os::unix::net::UnixStream,
) -> Result<u32, ResponderBrokerError> {
    #[cfg(target_os = "linux")]
    {
        rustix::net::sockopt::socket_peercred(stream)
            .map(|credentials| credentials.uid.as_raw())
            .map_err(|_| ResponderBrokerError::InvalidConfiguration)
    }
    #[cfg(target_vendor = "apple")]
    {
        stream
            .peer_cred()
            .map(|credentials| credentials.uid)
            .map_err(|_| ResponderBrokerError::InvalidConfiguration)
    }
    #[cfg(all(not(target_os = "linux"), not(target_vendor = "apple")))]
    {
        let _ = stream;
        Err(ResponderBrokerError::PlatformUnsupported)
    }
}

#[cfg(unix)]
async fn read_control_request(
    cx: &Cx,
    mut stream: UnixStream,
) -> Result<(UnixStream, ResponderControlRequest), ResponderBrokerError> {
    checkpoint(cx, "responder control")?;
    let peer = stream
        .peer_cred()
        .map(|credentials| credentials.uid)
        .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    if peer != current_responder_euid() {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    let request = read_control_frame_async(cx, &mut stream).await?;
    validate_control_request(&request)?;
    Ok((stream, request))
}

#[cfg(unix)]
async fn write_control_response(
    stream: &mut UnixStream,
    response: &ResponderControlResponse,
) -> Result<(), ResponderBrokerError> {
    write_control_frame_async(stream, response).await
}

#[cfg(any(unix, windows))]
fn write_control_frame<W: Write, T: Serialize>(
    stream: &mut W,
    value: &T,
) -> Result<(), ResponderBrokerError> {
    let bytes =
        serde_json::to_vec(value).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    if bytes.len() > RESPONDER_CONTROL_MAX_BYTES {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    let len = u32::try_from(bytes.len()).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    stream
        .write_all(&len.to_le_bytes())
        .and_then(|()| stream.write_all(&bytes))
        .map_err(|_| ResponderBrokerError::TransportUnavailable)
}

#[cfg(any(unix, windows))]
fn read_control_frame<R: Read, T: for<'de> Deserialize<'de>>(
    stream: &mut R,
) -> Result<T, ResponderBrokerError> {
    let mut len_buf = [0_u8; 4];
    stream
        .read_exact(&mut len_buf)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    let len = usize::try_from(u32::from_le_bytes(len_buf))
        .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    if len == 0 || len > RESPONDER_CONTROL_MAX_BYTES {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    let mut bytes = vec![0_u8; len];
    stream
        .read_exact(&mut bytes)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    serde_json::from_slice(&bytes).map_err(|_| ResponderBrokerError::InvalidConfiguration)
}

#[cfg(unix)]
async fn write_control_frame_async<T: Serialize>(
    stream: &mut UnixStream,
    value: &T,
) -> Result<(), ResponderBrokerError> {
    let bytes =
        serde_json::to_vec(value).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    if bytes.len() > RESPONDER_CONTROL_MAX_BYTES {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    let len = u32::try_from(bytes.len()).map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    AsyncWriteExt::write_all(stream, &len.to_le_bytes())
        .await
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    AsyncWriteExt::write_all(stream, &bytes)
        .await
        .map_err(|_| ResponderBrokerError::TransportUnavailable)
}

#[cfg(unix)]
async fn read_control_frame_async<T: for<'de> Deserialize<'de>>(
    cx: &Cx,
    stream: &mut UnixStream,
) -> Result<T, ResponderBrokerError> {
    checkpoint(cx, "responder control frame")?;
    let mut len_buf = [0_u8; 4];
    AsyncReadExt::read_exact(stream, &mut len_buf)
        .await
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    let len = usize::try_from(u32::from_le_bytes(len_buf))
        .map_err(|_| ResponderBrokerError::InvalidConfiguration)?;
    if len == 0 || len > RESPONDER_CONTROL_MAX_BYTES {
        return Err(ResponderBrokerError::InvalidConfiguration);
    }
    let mut bytes = vec![0_u8; len];
    AsyncReadExt::read_exact(stream, &mut bytes)
        .await
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    serde_json::from_slice(&bytes).map_err(|_| ResponderBrokerError::InvalidConfiguration)
}

#[cfg(unix)]
fn percent_encode_query(value: &str) -> String {
    let mut encoded = String::with_capacity(value.len());
    for byte in value.bytes() {
        if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
            encoded.push(char::from(byte));
        } else {
            use std::fmt::Write as _;
            let _ = write!(&mut encoded, "%{byte:02X}");
        }
    }
    encoded
}

#[cfg(unix)]
fn parse_local_api_response(response: &[u8]) -> Result<Vec<u8>, ResponderBrokerError> {
    let header_end = response
        .windows(4)
        .position(|window| window == b"\r\n\r\n")
        .ok_or(ResponderBrokerError::WhoIsUnverified)?;
    if header_end > LOCAL_API_MAX_HEADER_BYTES {
        return Err(ResponderBrokerError::WhoIsUnverified);
    }
    let header = std::str::from_utf8(&response[..header_end])
        .map_err(|_| ResponderBrokerError::WhoIsUnverified)?;
    let mut lines = header.split("\r\n");
    let status = lines.next().ok_or(ResponderBrokerError::WhoIsUnverified)?;
    if !status.starts_with("HTTP/1.1 200 ") && status != "HTTP/1.1 200" {
        return Err(ResponderBrokerError::WhoIsUnavailable);
    }
    let mut content_length = None;
    let mut chunked = false;
    for line in lines {
        let Some((name, value)) = line.split_once(':') else {
            return Err(ResponderBrokerError::WhoIsUnverified);
        };
        if name.eq_ignore_ascii_case("transfer-encoding") {
            let tokens = value
                .split(',')
                .map(str::trim)
                .filter(|token| !token.is_empty())
                .collect::<Vec<_>>();
            if tokens.len() != 1 || !tokens[0].eq_ignore_ascii_case("chunked") {
                return Err(ResponderBrokerError::WhoIsUnverified);
            }
            chunked = true;
        }
        if name.eq_ignore_ascii_case("content-length") {
            let parsed = value
                .trim()
                .parse::<usize>()
                .map_err(|_| ResponderBrokerError::WhoIsUnverified)?;
            if content_length.replace(parsed).is_some() {
                return Err(ResponderBrokerError::WhoIsUnverified);
            }
        }
    }
    let body = &response[header_end + 4..];
    if chunked {
        if content_length.is_some() {
            return Err(ResponderBrokerError::WhoIsUnverified);
        }
        return decode_local_api_chunked_body(body);
    }
    if content_length.is_some_and(|expected| expected != body.len()) {
        return Err(ResponderBrokerError::WhoIsUnverified);
    }
    Ok(body.to_vec())
}

#[cfg(unix)]
fn decode_local_api_chunked_body(body: &[u8]) -> Result<Vec<u8>, ResponderBrokerError> {
    let mut decoded = Vec::new();
    let mut cursor = 0_usize;
    loop {
        let remaining = body
            .get(cursor..)
            .ok_or(ResponderBrokerError::WhoIsUnverified)?;
        let line_end = remaining
            .windows(2)
            .position(|window| window == b"\r\n")
            .ok_or(ResponderBrokerError::WhoIsUnverified)?;
        let size_line = std::str::from_utf8(&remaining[..line_end])
            .map_err(|_| ResponderBrokerError::WhoIsUnverified)?;
        let size_token = size_line.split(';').next().unwrap_or_default().trim();
        let chunk_size = usize::from_str_radix(size_token, 16)
            .map_err(|_| ResponderBrokerError::WhoIsUnverified)?;
        cursor = cursor
            .checked_add(line_end + 2)
            .ok_or(ResponderBrokerError::WhoIsUnverified)?;
        if chunk_size == 0 {
            return Ok(decoded);
        }
        let chunk_end = cursor
            .checked_add(chunk_size)
            .ok_or(ResponderBrokerError::WhoIsUnverified)?;
        let chunk = body
            .get(cursor..chunk_end)
            .ok_or(ResponderBrokerError::WhoIsUnverified)?;
        if decoded.len().saturating_add(chunk.len()) > LOCAL_API_MAX_RESPONSE_BYTES {
            return Err(ResponderBrokerError::WhoIsUnverified);
        }
        decoded.extend_from_slice(chunk);
        if body.get(chunk_end..chunk_end + 2) != Some(b"\r\n") {
            return Err(ResponderBrokerError::WhoIsUnverified);
        }
        cursor = chunk_end + 2;
    }
}

#[cfg(unix)]
async fn await_local_api_io<T, F>(
    cx: &Cx,
    duration: Duration,
    future: F,
) -> Result<T, ResponderBrokerError>
where
    F: Future<Output = io::Result<T>>,
{
    checkpoint(cx, "tailscale localapi")?;
    let now = wall_now();
    let effective = cx
        .budget()
        .remaining_duration(now)
        .map_or(duration, |remaining| remaining.min(duration));
    if effective.is_zero() {
        return Err(ResponderBrokerError::Cancelled);
    }
    let _ambient = Cx::set_current(Some(cx.clone()));
    match timeout(now, effective, future).await {
        Ok(Ok(value)) => {
            checkpoint(cx, "tailscale localapi")?;
            Ok(value)
        }
        Ok(Err(error)) if error.kind() == io::ErrorKind::Interrupted => {
            Err(ResponderBrokerError::Cancelled)
        }
        Ok(Err(_)) => Err(ResponderBrokerError::WhoIsUnavailable),
        Err(_) => Err(ResponderBrokerError::WhoIsUnavailable),
    }
}

async fn read_asupersync_framed(
    cx: &Cx,
    stream: &mut TcpStream,
    duration: Duration,
) -> Result<Vec<u8>, ResponderBrokerError> {
    checkpoint(cx, "bootstrap frame read")?;
    let now = wall_now();
    let effective = cx
        .budget()
        .remaining_duration(now)
        .map_or(duration, |remaining| remaining.min(duration));
    if effective.is_zero() {
        return Err(ResponderBrokerError::Cancelled);
    }
    let _ambient = Cx::set_current(Some(cx.clone()));
    let bytes = match timeout(now, effective, async {
        let mut prefix = [0_u8; 4];
        AsyncReadExt::read_exact(stream, &mut prefix).await?;
        let length = usize::try_from(u32::from_be_bytes(prefix)).map_err(|_| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "bootstrap length does not fit usize",
            )
        })?;
        if length == 0 || length > BOOTSTRAP_MAX_ENVELOPE_BYTES {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!(
                    "bootstrap length {length} exceeds {BOOTSTRAP_MAX_ENVELOPE_BYTES}-byte cap"
                ),
            ));
        }
        let mut body = vec![0_u8; length];
        AsyncReadExt::read_exact(stream, &mut body).await?;
        Ok(body)
    })
    .await
    {
        Ok(Ok(body)) => body,
        Ok(Err(error)) if error.kind() == io::ErrorKind::Interrupted => {
            return Err(ResponderBrokerError::Cancelled);
        }
        Ok(Err(_)) | Err(_) => return Err(ResponderBrokerError::TransportUnavailable),
    };
    checkpoint(cx, "bootstrap frame read")?;
    Ok(bytes)
}

async fn write_asupersync_framed(
    cx: &Cx,
    stream: &mut TcpStream,
    duration: Duration,
    bytes: &[u8],
) -> Result<(), ResponderBrokerError> {
    if bytes.len() > BOOTSTRAP_MAX_ENVELOPE_BYTES {
        return Err(ResponderBrokerError::TransportUnavailable);
    }
    checkpoint(cx, "bootstrap frame write")?;
    let prefix = u32::try_from(bytes.len())
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?
        .to_be_bytes();
    let now = wall_now();
    let effective = cx
        .budget()
        .remaining_duration(now)
        .map_or(duration, |remaining| remaining.min(duration));
    if effective.is_zero() {
        return Err(ResponderBrokerError::Cancelled);
    }
    let _ambient = Cx::set_current(Some(cx.clone()));
    match timeout(now, effective, async {
        AsyncWriteExt::write_all(stream, &prefix).await?;
        AsyncWriteExt::write_all(stream, bytes).await?;
        AsyncWriteExt::flush(stream).await
    })
    .await
    {
        Ok(Ok(())) => {}
        Ok(Err(error)) if error.kind() == io::ErrorKind::Interrupted => {
            return Err(ResponderBrokerError::Cancelled);
        }
        Ok(Err(_)) | Err(_) => return Err(ResponderBrokerError::TransportUnavailable),
    }
    checkpoint(cx, "bootstrap frame write")?;
    Ok(())
}

async fn write_bootstrap_decline(
    cx: &Cx,
    stream: &mut TcpStream,
    duration: Duration,
    code: &str,
) -> Result<(), ResponderBrokerError> {
    let decline = BootstrapDeclineV1 {
        schema: BOOTSTRAP_DECLINE_SCHEMA_V1.to_owned(),
        code: code.to_owned(),
    };
    let bytes =
        serde_json::to_vec(&decline).map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    write_asupersync_framed(cx, stream, duration, &bytes).await
}

async fn answer_bootstrap_hello(
    cx: &Cx,
    stream: &mut TcpStream,
    routes: &ResponderRouteRegistry,
    first_packet: &[u8],
) -> Result<(), ResponderBrokerError> {
    checkpoint(cx, "bootstrap hello")?;
    let io_timeout = routes.limits.io_timeout;
    let envelope = match decode_envelope(first_packet) {
        Ok(envelope) => envelope,
        Err(error) => {
            return write_bootstrap_decline(cx, stream, io_timeout, error.decline_code()).await;
        }
    };
    if envelope.capability != BootstrapCapability::Hello {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_unsupported_capability")
            .await;
    }
    let Some(request) = parse_hello_request(&envelope.payload) else {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
    };
    let workspace_ids = routes.workspace_ids();
    let lists = routes
        .first_workspace_path()
        .and_then(|path| load_workspace_lists(path).ok())
        .unwrap_or_default();
    let advertised_tags = vec![EE_MESH_SERVICE_TAG.to_owned()];
    let capabilities = vec!["hello".to_owned()];
    let context = ResponderContext {
        mesh_enabled: true,
        tailscale_authenticated: true,
        shields_up: false,
        respond_mode: DiscoveryMode::from_env_respond(|_| {}),
        responder_node_key: &routes.responder_node_pubkey,
        responder_ee_version: env!("CARGO_PKG_VERSION"),
        responder_workspace_ids: &workspace_ids,
        responder_capabilities: &capabilities,
        responder_advertised_tags: &advertised_tags,
        respond_allowlist: &lists.respond_allowlist,
        denylist: &lists.denylist,
        rate_limited: false,
        elapsed_micros: 0,
    };
    let payload = match decide_hello_response(&request, &context) {
        HelloOutcome::Granted(response) => serde_json::to_value(response),
        HelloOutcome::Declined(error) => serde_json::to_value(error),
    }
    .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    let reply = encode_envelope(BootstrapCapability::Hello, payload)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    write_asupersync_framed(cx, stream, io_timeout, &reply).await
}

async fn answer_bootstrap_join(
    cx: &Cx,
    stream: &mut TcpStream,
    routes: &ResponderRouteRegistry,
    first_packet: &[u8],
    joiner_addr: SocketAddr,
) -> Result<(), ResponderBrokerError> {
    checkpoint(cx, "bootstrap join")?;
    let io_timeout = routes.limits.io_timeout;
    let envelope = match decode_envelope(first_packet) {
        Ok(envelope) => envelope,
        Err(error) => {
            return write_bootstrap_decline(cx, stream, io_timeout, error.decline_code()).await;
        }
    };
    if envelope.capability != BootstrapCapability::Join {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_unsupported_capability")
            .await;
    }
    let hello = match serde_json::from_value::<crate::mesh::team::TeamJoinHelloV1>(envelope.payload)
    {
        Ok(hello) if hello.schema == crate::mesh::team::TEAM_JOIN_HELLO_SCHEMA_V1 => hello,
        _ => {
            return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
        }
    };
    let Some(route) = routes.routes.values().next() else {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
    };
    let Some(database_path) = route.database_path.clone() else {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
    };
    let Ok(connection) = DbConnection::open_file(database_path) else {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
    };
    let Ok(Some(invite)) = connection.get_team_pending_invite(&hello.invite_id) else {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
    };
    let signer = match crate::mesh::origin_stream::Ed25519OriginSigner::load_or_create(
        &route.workspace_path,
        &invite.origin_node_id,
        &invite.created_at,
    ) {
        Ok(signer) => signer,
        Err(_) => {
            return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
        }
    };
    let challenge = match crate::mesh::team::sign_join_challenge(&signer, &invite, &hello) {
        Ok(challenge) => challenge,
        Err(_) => {
            return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
        }
    };
    let challenge_value =
        serde_json::to_value(&challenge).map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    let challenge_bytes = encode_envelope(BootstrapCapability::Join, challenge_value)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    write_asupersync_framed(cx, stream, io_timeout, &challenge_bytes).await?;
    let prove_bytes = match read_asupersync_framed(cx, stream, io_timeout).await {
        Ok(bytes) => bytes,
        Err(_) => {
            return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
        }
    };
    let prove_envelope = match decode_envelope(&prove_bytes) {
        Ok(envelope) => envelope,
        Err(_) => {
            return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
        }
    };
    let prove = match serde_json::from_value::<crate::mesh::team::TeamJoinProveV1>(
        prove_envelope.payload,
    ) {
        Ok(prove) if prove.schema == crate::mesh::team::TEAM_JOIN_PROVE_SCHEMA_V1 => prove,
        _ => {
            return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
        }
    };
    if prove.invite_id != hello.invite_id
        || prove.joiner_nonce != hello.joiner_nonce
        || prove.inviter_nonce != challenge.inviter_nonce
    {
        return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
    }
    let redeemed_at = chrono::Utc::now().to_rfc3339();
    let mut granted = match crate::mesh::team::redeem_team_invite(
        &connection,
        &prove.invite_id,
        &prove.secret,
        &redeemed_at,
    ) {
        Ok(granted) => granted,
        Err(_) => {
            return write_bootstrap_decline(cx, stream, io_timeout, "bootstrap_malformed").await;
        }
    };
    let pair = crate::mesh::team::derive_team_pair_key(
        &prove.secret,
        &granted.team_id,
        &prove.invite_id,
        &prove.joiner_node_id,
        &granted.origin_node_id,
        &prove.joiner_nonce,
        &prove.inviter_nonce,
    );
    granted.pair_confirmation = crate::mesh::team::pair_confirmation(&pair);
    let requested = crate::core::workspace::stable_workspace_id(&route.workspace_path);
    if let Ok(workspace_id) = crate::core::workspace::bound_workspace_id_or_hash(
        &connection,
        &requested,
        &[route.workspace_path.as_path()],
    ) {
        let _ = crate::mesh::team::record_inviter_side_join_member(
            &connection,
            &workspace_id,
            &granted,
            &prove.joiner_node_id,
            &prove.joiner_display_name,
            &redeemed_at,
            Some(hello.joiner_verifying_key.as_str()).filter(|key| key.len() == 64),
        );
        let _ = crate::mesh::team::enroll_joiner_from_accept(
            &connection,
            &workspace_id,
            &granted.team_id,
            &prove.joiner_node_id,
            &prove.joiner_display_name,
            joiner_addr,
            &hello.joiner_workspace_id,
            hello.joiner_hello_port,
            &redeemed_at,
        );
    }
    let _ = crate::mesh::team::persist_pair_key(
        &route.workspace_path,
        &granted.team_id,
        &prove.joiner_node_id,
        &pair,
        &redeemed_at,
    );
    let payload =
        serde_json::to_value(&granted).map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    let reply = encode_envelope(BootstrapCapability::Join, payload)
        .map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    write_asupersync_framed(cx, stream, io_timeout, &reply).await
}

async fn answer_sync_round(
    cx: &Cx,
    stream: &mut TcpStream,
    routes: &ResponderRouteRegistry,
) -> Result<(), ResponderBrokerError> {
    let io_timeout = routes.limits.io_timeout;
    let Ok(bytes) = read_asupersync_framed(cx, stream, io_timeout).await else {
        return Ok(());
    };
    let Some(request) = parse_sync_round_request(&bytes) else {
        return Ok(());
    };
    let Some(route) = routes.first_store_route() else {
        return Ok(());
    };
    let response = load_sync_round_response(route, request.range_start_seq, request.max_events);
    let reply =
        serde_json::to_vec(&response).map_err(|_| ResponderBrokerError::TransportUnavailable)?;
    write_asupersync_framed(cx, stream, io_timeout, &reply).await
}

fn admit_authenticated_capability(
    _peer_id: &str,
    capability: &FrameCapability,
    payload: &serde_json::Value,
    peer_state: &mut crate::mesh::admission::MeshPeerAdmissionState,
) -> Result<crate::mesh::admission::MeshAdmissionDecision, ResponderBrokerError> {
    use crate::mesh::admission::{
        MeshAdmissionRequestKind, admit_authenticated_mesh_capability_with_state,
        record_authenticated_admission,
    };
    let kind = match capability {
        FrameCapability::BodyFetch => MeshAdmissionRequestKind::BodyFetch,
        FrameCapability::EventFetch => MeshAdmissionRequestKind::EventBatch,
        FrameCapability::Summary => MeshAdmissionRequestKind::TipAdvertise,
        FrameCapability::Extension(name) if name == "identity_attest" => {
            MeshAdmissionRequestKind::Hello
        }
        _ => MeshAdmissionRequestKind::Hello,
    };
    let payload_bytes = u64::try_from(payload.to_string().len()).unwrap_or(u64::MAX);
    let now_epoch_ms = u64::try_from(
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis(),
    )
    .unwrap_or(u64::MAX);
    let event_count = if kind == MeshAdmissionRequestKind::EventBatch {
        serde_json::to_vec(payload)
            .ok()
            .and_then(|bytes| parse_sync_round_request(&bytes))
            .map(|parsed| parsed.max_events)
            .unwrap_or(0)
    } else {
        0
    };
    let body_fetch_bytes = if kind == MeshAdmissionRequestKind::BodyFetch {
        payload_bytes
    } else {
        0
    };
    let decision = admit_authenticated_mesh_capability_with_state(
        peer_state,
        kind,
        payload_bytes,
        event_count,
        body_fetch_bytes,
        now_epoch_ms,
    );
    record_authenticated_admission(peer_state, &decision, now_epoch_ms);
    if !decision.allowed() {
        return Err(ResponderBrokerError::AdmissionLimited);
    }
    Ok(decision)
}

fn authenticated_session_route(
    routes: &ResponderRouteRegistry,
    binding: &SessionBinding,
) -> Result<RegisteredResponderRoute, ResponderBrokerError> {
    routes
        .resolve_authenticated_binding(binding)
        .cloned()
        .ok_or(ResponderBrokerError::RouteUnavailable)
}

async fn serve_authenticated_sync_round(
    cx: &Cx,
    session: &mut AuthenticatedTransportSession,
    routes: &ResponderRouteRegistry,
    admission: &Arc<Mutex<AdmissionState>>,
) -> Result<(), ResponderBrokerError> {
    let authenticated_route = authenticated_session_route(routes, session.binding())?;
    let request = loop {
        let Some(request) = session
            .receive_request(cx)
            .await
            .map_err(ResponderBrokerError::Session)?
        else {
            return Ok(());
        };
        if matches!(
            request.capability,
            FrameCapability::Summary | FrameCapability::EventFetch | FrameCapability::BodyFetch
        ) || matches!(
            &request.capability,
            FrameCapability::Extension(name) if name == "identity_attest"
        ) {
            break request;
        }
    };
    let peer_id = session.binding().initiator_node_id.clone();
    {
        let mut state = admission
            .lock()
            .map_err(|_| ResponderBrokerError::AdmissionLimited)?;
        let peer = state
            .peers
            .entry(peer_id.clone())
            .or_insert_with(|| crate::mesh::admission::MeshPeerAdmissionState::new(&peer_id));
        admit_authenticated_capability(&peer_id, &request.capability, &request.payload, peer)?;
    }
    let payload = if matches!(
        &request.capability,
        FrameCapability::Extension(name) if name == "identity_attest"
    ) {
        let processed = session
            .process_request(cx, &request, async {
                load_identity_attest_response(&authenticated_route, &request.payload)
            })
            .await
            .map_err(ResponderBrokerError::Session)?;
        serde_json::to_value(&processed).map_err(|_| ResponderBrokerError::TransportUnavailable)?
    } else if request.capability == FrameCapability::BodyFetch {
        let processed = session
            .process_request(cx, &request, async {
                load_body_fetch_response(&authenticated_route, &request.payload)
            })
            .await
            .map_err(ResponderBrokerError::Session)?;
        serde_json::to_value(&processed).map_err(|_| ResponderBrokerError::TransportUnavailable)?
    } else {
        let (range_start_seq, max_events) = serde_json::to_vec(&request.payload)
            .ok()
            .and_then(|bytes| parse_sync_round_request(&bytes))
            .map(|parsed| (parsed.range_start_seq, parsed.max_events))
            .unwrap_or((0, 512));
        let processed = session
            .process_request(cx, &request, async {
                load_sync_round_response(&authenticated_route, range_start_seq, max_events)
            })
            .await
            .map_err(ResponderBrokerError::Session)?;
        serde_json::to_value(&processed).map_err(|_| ResponderBrokerError::TransportUnavailable)?
    };
    let result = session
        .send_response(
            cx,
            SessionMessage {
                correlation_id: request.correlation_id,
                capability: request.capability,
                requested_budget_ms: request.requested_budget_ms,
                payload,
            },
        )
        .await
        .map_err(ResponderBrokerError::Session);
    if let Ok(mut state) = admission.lock()
        && let Some(peer) = state.peers.get_mut(&peer_id)
    {
        crate::mesh::admission::release_authenticated_admission(peer);
    }
    persist_authenticated_admission_snapshot(&authenticated_route, admission);
    result
}

fn persist_authenticated_admission_snapshot(
    route: &RegisteredResponderRoute,
    admission: &Arc<Mutex<AdmissionState>>,
) {
    let Some(database_path) = route.database_path.clone() else {
        return;
    };
    let requested = route.expectations.responder_workspace_id.clone();
    let workspace_path = route.workspace_path.clone();
    let Ok(state) = admission.lock() else {
        return;
    };
    let peers = state.peers.values().cloned().collect::<Vec<_>>();
    drop(state);
    let Ok(connection) = DbConnection::open_file(database_path) else {
        return;
    };
    let workspace_id = crate::core::workspace::bound_workspace_id_or_hash(
        &connection,
        &requested,
        &[workspace_path.as_path()],
    )
    .unwrap_or(requested);
    let _ = crate::mesh::team::persist_team_admission_states(
        &connection,
        &workspace_id,
        &peers,
        &chrono::Utc::now().to_rfc3339(),
    );
}

fn load_sync_round_response(
    route: &RegisteredResponderRoute,
    range_start_seq: u64,
    max_events: u32,
) -> SyncRoundResponse {
    let workspace_id = route.expectations.responder_workspace_id.clone();
    let team_id = route.expectations.team_id.clone();
    let origin_node_id = route.expectations.responder_node_id.clone();
    let mut events = Vec::new();
    if let Some(database_path) = route.database_path.clone()
        && let Ok(connection) = DbConnection::open_file_read_only(database_path)
    {
        let limit = max_events.clamp(1, 512);
        if let Ok(rows) =
            connection.list_mesh_origin_events(&team_id, &origin_node_id, range_start_seq, limit)
        {
            events = rows
                .into_iter()
                .map(|row| {
                    let payload_json = crate::mesh::origin_stream::inbound_from_stored(&row)
                        .ok()
                        .and_then(|event| serde_json::to_string(&event).ok())
                        .unwrap_or(row.payload_json);
                    SyncRoundEvent {
                        origin_node_id: row.origin_node_id,
                        origin_workspace_id: workspace_id.clone(),
                        seq: row.seq,
                        event_hash: row.event_hash,
                        payload_json,
                    }
                })
                .collect();
        }
    }
    let last_seq = events.last().map_or(0, |event| event.seq);
    let tip_hash = events.last().map(|event| event.event_hash.clone());
    SyncRoundResponse {
        schema: SYNC_ROUND_SCHEMA_V1.to_owned(),
        tips: vec![SyncRoundTip {
            origin_node_id,
            origin_workspace_id: workspace_id,
            last_seq,
            tip_event_hash: tip_hash,
        }],
        events,
    }
}

fn load_identity_attest_response(
    route: &RegisteredResponderRoute,
    payload: &serde_json::Value,
) -> crate::mesh::idp::IdentityAttestFrameV1 {
    let rejected = crate::mesh::idp::IdentityAttestFrameV1 {
        schema: crate::mesh::idp::IDENTITY_ATTEST_FRAME_SCHEMA_V1.to_owned(),
        team_id: String::new(),
        member_id: String::new(),
        subject: "rejected".to_owned(),
        email: None,
        matched_groups: Vec::new(),
        token_hash: "blake3:0000000000000000000000000000000000000000000000000000000000000000"
            .to_owned(),
        checked_at: String::new(),
    };
    let Some(database_path) = route.database_path.clone() else {
        return rejected;
    };
    let Ok(connection) = DbConnection::open_file(database_path) else {
        return rejected;
    };
    crate::mesh::team::apply_identity_attest_frame(
        &connection,
        payload,
        &route.expectations.team_id,
        &route.expectations.initiator_node_id,
    )
    .unwrap_or(rejected)
}

fn load_body_fetch_response(
    route: &RegisteredResponderRoute,
    payload: &serde_json::Value,
) -> BodyFetchResponse {
    let request = serde_json::from_value::<BodyFetchRequest>(payload.clone()).ok();
    let key = request
        .as_ref()
        .map(|parsed| parsed.body_cache_key.as_str())
        .unwrap_or_default();
    if key.is_empty()
        || request
            .as_ref()
            .is_none_or(|parsed| parsed.schema != BODY_FETCH_REQUEST_SCHEMA_V1)
    {
        return BodyFetchResponse {
            schema: BODY_FETCH_RESPONSE_SCHEMA_V1.to_owned(),
            body_cache_key: key.to_owned(),
            cache_status: "metadata_only".to_owned(),
            size_bytes: 0,
            body_hex: None,
            nonce_hex: None,
        };
    }
    let requested_workspace_id = route.expectations.responder_workspace_id.clone();
    let workspace_path = route.workspace_path.clone();
    let peer_id = route.peer_handle.clone();
    let Some(database_path) = route.database_path.clone() else {
        return BodyFetchResponse {
            schema: BODY_FETCH_RESPONSE_SCHEMA_V1.to_owned(),
            body_cache_key: key.to_owned(),
            cache_status: "metadata_only".to_owned(),
            size_bytes: 0,
            body_hex: None,
            nonce_hex: None,
        };
    };
    let Ok(connection) = DbConnection::open_file_read_only(database_path) else {
        return BodyFetchResponse {
            schema: BODY_FETCH_RESPONSE_SCHEMA_V1.to_owned(),
            body_cache_key: key.to_owned(),
            cache_status: "metadata_only".to_owned(),
            size_bytes: 0,
            body_hex: None,
            nonce_hex: None,
        };
    };
    let workspace_id = crate::core::workspace::bound_workspace_id_or_hash(
        &connection,
        &requested_workspace_id,
        &[workspace_path.as_path()],
    )
    .unwrap_or(requested_workspace_id);
    if !crate::mesh::team::body_lane_allows_fetch(
        &connection,
        &workspace_id,
        &peer_id,
        &workspace_path,
    ) {
        return BodyFetchResponse {
            schema: BODY_FETCH_RESPONSE_SCHEMA_V1.to_owned(),
            body_cache_key: key.to_owned(),
            cache_status: "metadata_only".to_owned(),
            size_bytes: 0,
            body_hex: None,
            nonce_hex: None,
        };
    }
    crate::mesh::team::fetch_local_team_body(&connection, &workspace_id, &workspace_path, key)
        .unwrap_or_else(|_| BodyFetchResponse {
            schema: BODY_FETCH_RESPONSE_SCHEMA_V1.to_owned(),
            body_cache_key: key.to_owned(),
            cache_status: "metadata_only".to_owned(),
            size_bytes: 0,
            body_hex: None,
            nonce_hex: None,
        })
}

fn checkpoint(cx: &Cx, _phase: &'static str) -> Result<(), ResponderBrokerError> {
    cx.checkpoint().map_err(|_| ResponderBrokerError::Cancelled)
}

fn refuse_if_transport_disabled() -> Result<(), ResponderBrokerError> {
    let Some(raw) = read_env_var(EnvVar::MeshTransportDisabled) else {
        return Ok(());
    };
    match parse_env_bool_flag(&raw) {
        Some(true) => Err(ResponderBrokerError::Session(
            SessionChannelError::TransportDisabled,
        )),
        Some(false) => Ok(()),
        None => Err(ResponderBrokerError::Session(
            SessionChannelError::InvalidConfiguration {
                variable: EnvVar::MeshTransportDisabled.name(),
            },
        )),
    }
}

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

    #[derive(Clone)]
    struct StaticLocalApi {
        status: LocalTailscaleStatus,
    }

    impl TailscaleLocalApi for StaticLocalApi {
        fn local_status<'a>(
            &'a self,
            _cx: &'a Cx,
        ) -> TailscaleLocalApiFuture<'a, LocalTailscaleStatus> {
            Box::pin(async move { Ok(self.status.clone()) })
        }

        fn verify_local_address<'a>(
            &'a self,
            _cx: &'a Cx,
            _address: SocketAddr,
        ) -> TailscaleLocalApiFuture<'a, LocalTailscaleIdentity> {
            Box::pin(async { Err(ResponderBrokerError::WhoIsUnverified) })
        }

        fn who_is<'a>(
            &'a self,
            _cx: &'a Cx,
            _source: SocketAddr,
        ) -> TailscaleLocalApiFuture<'a, WhoIsIdentity> {
            Box::pin(async { Err(ResponderBrokerError::WhoIsUnverified) })
        }
    }

    fn fixture_abs_path(label: &str) -> PathBuf {
        std::env::temp_dir().join(label)
    }

    fn route(path: PathBuf, port: u16) -> RegisteredResponderRoute {
        RegisteredResponderRoute {
            workspace_path: path,
            database_path: None,
            peer_handle: "peer_0123456789abcdef0123456789abcdef".to_owned(),
            committed_port: port,
            expectations: ResponderExpectations {
                team_id: "team-a".to_owned(),
                tailnet_id: "tailnet-a".to_owned(),
                responder_node_id: "node-responder".to_owned(),
                responder_workspace_id: "workspace-responder".to_owned(),
                responder_stable_id: "stable-responder".to_owned(),
                initiator_node_id: "node_0123456789abcdef0123456789abcdef".to_owned(),
                initiator_stable_id: "stable-initiator".to_owned(),
                pair_key_generation: 1,
            },
            responder_node_pubkey: "nodekey:responder-current".to_owned(),
            peer_transport_key_generation: 1,
            grant_generation: 1,
            capabilities: SessionCapabilities::base(),
            limits: SessionChannelLimits::default(),
        }
    }

    fn authenticated_binding_for(route: &RegisteredResponderRoute) -> SessionBinding {
        SessionBinding {
            team_id: route.expectations.team_id.clone(),
            tailnet_id: route.expectations.tailnet_id.clone(),
            initiator_node_id: route.expectations.initiator_node_id.clone(),
            responder_node_id: route.expectations.responder_node_id.clone(),
            initiator_workspace_id: "workspace-initiator".to_owned(),
            responder_workspace_id: route.expectations.responder_workspace_id.clone(),
            initiator_stable_id: route.expectations.initiator_stable_id.clone(),
            responder_stable_id: route.expectations.responder_stable_id.clone(),
            session_id: "session-authenticated".to_owned(),
        }
    }

    #[test]
    fn registry_rejects_duplicate_and_mixed_listener_routes() {
        let path = PathBuf::from("/tmp/ee-responder-broker-unit");
        let first = route(path.clone(), 41888);
        let duplicate = first.clone();
        assert!(matches!(
            ResponderRouteRegistry::new([first, duplicate]),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));

        let first = route(path.clone(), 41888);
        let mut mixed = route(path, 41889);
        mixed.expectations.team_id = "team-b".to_owned();
        assert!(matches!(
            ResponderRouteRegistry::new([first, mixed]),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));

        let mut empty_node_key = route(PathBuf::from("/tmp/ee-responder-broker-unit"), 41888);
        empty_node_key.responder_node_pubkey = "nodekey:".to_owned();
        assert!(matches!(
            ResponderRouteRegistry::new([empty_node_key]),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));

        let mut guessable_handle = route(PathBuf::from("/tmp/ee-responder-broker-unit"), 41888);
        guessable_handle.peer_handle = "peer_7".to_owned();
        assert!(matches!(
            ResponderRouteRegistry::new([guessable_handle]),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));
    }

    #[test]
    fn production_owner_rejects_loopback_status_before_any_listener_bind() {
        let registry = ResponderRouteRegistry::new([route(
            fixture_abs_path("ee-responder-owner-loopback-negative"),
            41888,
        )])
        .expect("valid local route registry");
        let result = crate::core::run_cli_with_cx(Duration::from_secs(5), |cx| async move {
            ResponderBrokerOwner::start(
                &cx,
                StaticLocalApi {
                    status: LocalTailscaleStatus {
                        identity: LocalTailscaleIdentity {
                            stable_id: "stable-responder".to_owned(),
                            current_node_pubkey: "nodekey:responder-current".to_owned(),
                            tailnet_id: "tailnet-a".to_owned(),
                        },
                        addresses: vec![IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)],
                    },
                },
                registry,
                PreAuthAdmissionLimits::default(),
                Duration::from_secs(1),
            )
            .await
        });
        assert!(matches!(
            result,
            Ok(Err(ResponderBrokerError::TransportUnavailable))
        ));
    }

    #[cfg(unix)]
    #[test]
    fn localapi_http_parser_accepts_bounded_chunked_json() {
        let response = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\n{\"ok\r\n7\r\n\":true}\r\n0\r\n\r\n";
        let body = parse_local_api_response(response).expect("decode bounded chunked response");
        assert_eq!(body, br#"{"ok":true}"#);
    }

    #[cfg(unix)]
    #[test]
    fn localapi_status_requires_running_backend_and_uses_complete_address_set() {
        let running = br#"{
            "BackendState":"Running",
            "TailscaleIPs":["100.64.0.1"],
            "Self":{
                "ID":"node-stable",
                "PublicKey":"nodekey:current",
                "TailscaleIPs":["fd7a:115c:a1e0::1"]
            },
            "CurrentTailnet":{"MagicDNSSuffix":"example.ts.net"}
        }"#;
        let status = parse_local_status(running).expect("running status");
        assert_eq!(
            status.addresses,
            vec![
                "100.64.0.1".parse::<IpAddr>().expect("v4"),
                "fd7a:115c:a1e0::1".parse::<IpAddr>().expect("v6"),
            ]
        );

        let stopped = br#"{
            "BackendState":"Stopped",
            "TailscaleIPs":["100.64.0.1"],
            "Self":{
                "ID":"node-stable",
                "PublicKey":"nodekey:current",
                "TailscaleIPs":["100.64.0.1"]
            },
            "CurrentTailnet":{"MagicDNSSuffix":"example.ts.net"}
        }"#;
        assert!(matches!(
            parse_local_status(stopped),
            Err(ResponderBrokerError::TransportUnavailable)
        ));
    }

    #[test]
    fn registry_multiplexes_same_target_for_distinct_initiator_stable_ids() {
        let path = fixture_abs_path("ee-responder-broker-multiplex-unit");
        let first = route(path.clone(), 41888);
        let mut second = route(path, 41888);
        second.peer_handle = "peer_fedcba9876543210fedcba9876543210".to_owned();
        second.expectations.initiator_node_id = "node_fedcba9876543210fedcba9876543210".to_owned();
        second.expectations.initiator_stable_id = "stable-initiator-b".to_owned();

        let registry = ResponderRouteRegistry::new([first, second]).expect(
            "distinct verified initiators may share one team, target, port, and generation",
        );
        assert_eq!(registry.route_count(), 2);
        let selected = registry
            .resolve(&UntrustedRouteSelectors {
                team_id: "team-a".to_owned(),
                responder_workspace_id: "workspace-responder".to_owned(),
                initiator_stable_id: "stable-initiator-b".to_owned(),
                pair_key_generation: 1,
            })
            .expect(
                "untrusted stable-id selector chooses only the local route later verified by WhoIs",
            );
        assert_eq!(
            selected.expectations.initiator_stable_id,
            "stable-initiator-b"
        );
        let binding = authenticated_binding_for(selected);
        let authenticated = registry
            .resolve_authenticated_binding(&binding)
            .expect("authenticated identity tuple must select its own durable route");
        assert_eq!(
            authenticated.peer_handle,
            "peer_fedcba9876543210fedcba9876543210"
        );
    }

    #[test]
    fn authenticated_route_resolution_rejects_generation_ambiguity() {
        let path = fixture_abs_path("ee-responder-broker-generation-ambiguity-unit");
        let first = route(path.clone(), 41888);
        let binding = authenticated_binding_for(&first);
        let mut second = route(path, 41888);
        second.expectations.pair_key_generation = 2;
        let registry = ResponderRouteRegistry::new([first, second])
            .expect("distinct pair-key generations are distinct registered routes");

        assert!(
            registry.resolve_authenticated_binding(&binding).is_none(),
            "SessionBinding carries no pair-key generation, so ambiguity must fail closed"
        );
        assert!(matches!(
            authenticated_session_route(&registry, &binding),
            Err(ResponderBrokerError::RouteUnavailable)
        ));
    }

    #[test]
    fn authenticated_route_b_reads_only_route_b_origin_events() {
        let workspace_a = tempfile::tempdir().expect("workspace a");
        let workspace_b = tempfile::tempdir().expect("workspace b");
        let database_a = workspace_a.path().join("a.db");
        let database_b = workspace_b.path().join("b.db");
        let connection_a = DbConnection::open_file(&database_a).expect("open database a");
        connection_a.migrate().expect("migrate database a");
        connection_a
            .append_mesh_origin_event(&crate::db::CreateMeshOriginEventInput {
                event_id: format!("mesh_oevt_{:026}", 1),
                team_id: "team_route_a".to_owned(),
                origin_node_id: "node_responder_a".to_owned(),
                signing_key_generation: 1,
                seq: 0,
                prev_event_hash: None,
                event_hash:
                    "blake3:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                        .to_owned(),
                signature: "sig-a".to_owned(),
                payload_schema: "ee.mesh.memory_event.v1".to_owned(),
                payload_json: r#"{"route":"a"}"#.to_owned(),
                required_features_json: "[]".to_owned(),
                produced_at: "2026-09-01T00:00:00Z".to_owned(),
                body_nonce_hex: None,
            })
            .expect("append route a event");
        connection_a.close().expect("close database a writer");
        let connection_b = DbConnection::open_file(&database_b).expect("open database b");
        connection_b.migrate().expect("migrate database b");
        connection_b
            .append_mesh_origin_event(&crate::db::CreateMeshOriginEventInput {
                event_id: format!("mesh_oevt_{:026}", 2),
                team_id: "team_route_b".to_owned(),
                origin_node_id: "node_responder_b".to_owned(),
                signing_key_generation: 1,
                seq: 0,
                prev_event_hash: None,
                event_hash:
                    "blake3:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
                        .to_owned(),
                signature: "sig-b".to_owned(),
                payload_schema: "ee.mesh.memory_event.v1".to_owned(),
                payload_json: r#"{"route":"b"}"#.to_owned(),
                required_features_json: "[]".to_owned(),
                produced_at: "2026-09-01T00:00:01Z".to_owned(),
                body_nonce_hex: None,
            })
            .expect("append route b event");
        connection_b.close().expect("close database b writer");

        let mut route_a = route(workspace_a.path().to_path_buf(), 41888);
        route_a.database_path = Some(database_a);
        route_a.expectations.team_id = "team_route_a".to_owned();
        route_a.expectations.responder_node_id = "node_responder_a".to_owned();
        route_a.expectations.responder_workspace_id = "workspace-a".to_owned();
        let mut route_b = route(workspace_b.path().to_path_buf(), 41888);
        route_b.database_path = Some(database_b);
        route_b.expectations.team_id = "team_route_b".to_owned();
        route_b.expectations.responder_node_id = "node_responder_b".to_owned();
        route_b.expectations.responder_workspace_id = "workspace-b".to_owned();
        let binding_b = authenticated_binding_for(&route_b);
        let registry =
            ResponderRouteRegistry::new([route_a, route_b]).expect("valid multi-route registry");
        let authenticated_route =
            authenticated_session_route(&registry, &binding_b).expect("authenticate route b");

        let response = load_sync_round_response(&authenticated_route, 0, 16);
        assert_eq!(response.events.len(), 1);
        assert_eq!(response.events[0].origin_workspace_id, "workspace-b");
        assert_eq!(
            response.events[0].event_hash,
            "blake3:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        );
        assert!(response.events.iter().all(|event| {
            event.event_hash
                != "blake3:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        }));
    }

    #[test]
    fn authenticated_route_b_cannot_apply_identity_attest_to_route_a() {
        let workspace_a = tempfile::tempdir().expect("workspace a");
        let workspace_b = tempfile::tempdir().expect("workspace b");
        let workspace_id_a =
            crate::models::WorkspaceId::from_uuid(uuid::Uuid::from_u128(1)).to_string();
        let workspace_id_b =
            crate::models::WorkspaceId::from_uuid(uuid::Uuid::from_u128(2)).to_string();
        let database_a = workspace_a.path().join("a.db");
        let database_b = workspace_b.path().join("b.db");
        let connection_a = DbConnection::open_file(&database_a).expect("open database a");
        connection_a.migrate().expect("migrate database a");
        connection_a
            .insert_workspace(
                &workspace_id_a,
                &crate::db::CreateWorkspaceInput {
                    path: workspace_a.path().display().to_string(),
                    name: Some("workspace a".to_owned()),
                },
            )
            .expect("insert workspace a");
        let created = crate::mesh::team::create_local_team(
            &connection_a,
            &workspace_id_a,
            "Route A",
            "2026-09-01T00:00:00Z",
        )
        .expect("create route a team");
        let member_a = connection_a
            .list_all_team_members()
            .expect("list route a members")
            .into_iter()
            .next()
            .expect("route a member");
        assert!(
            connection_a
                .get_team_member_identity(&member_a.member_id)
                .expect("load route a identity before attest")
                .is_none()
        );
        connection_a.close().expect("close database a writer");
        let connection_b = DbConnection::open_file(&database_b).expect("open database b");
        connection_b.migrate().expect("migrate database b");
        connection_b.close().expect("close database b writer");

        let mut route_a = route(workspace_a.path().to_path_buf(), 41888);
        route_a.database_path = Some(database_a.clone());
        route_a.expectations.team_id = created.team.team_id.clone();
        route_a.expectations.responder_node_id = created.team.origin_node_id;
        route_a.expectations.responder_workspace_id = workspace_id_a;
        let mut route_b = route(workspace_b.path().to_path_buf(), 41888);
        route_b.database_path = Some(database_b);
        route_b.expectations.team_id = "team_route_b".to_owned();
        route_b.expectations.responder_node_id = "node_responder_b".to_owned();
        route_b.expectations.responder_workspace_id = workspace_id_b;
        let binding_b = authenticated_binding_for(&route_b);
        let registry =
            ResponderRouteRegistry::new([route_a, route_b]).expect("valid multi-route registry");
        let authenticated_route =
            authenticated_session_route(&registry, &binding_b).expect("authenticate route b");
        let frame = crate::mesh::idp::IdentityAttestFrameV1 {
            schema: crate::mesh::idp::IDENTITY_ATTEST_FRAME_SCHEMA_V1.to_owned(),
            team_id: member_a.team_id.clone(),
            member_id: member_a.member_id.clone(),
            subject: "route-a-subject".to_owned(),
            email: Some("route-a@example.com".to_owned()),
            matched_groups: vec!["eng".to_owned()],
            token_hash: "blake3:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
                .to_owned(),
            checked_at: "2026-09-01T00:00:01Z".to_owned(),
        };

        let response = load_identity_attest_response(
            &authenticated_route,
            &serde_json::to_value(frame).expect("serialize attest frame"),
        );
        assert_eq!(response.subject, "rejected");
        let connection_a = DbConnection::open_file(&database_a).expect("reopen database a");
        assert!(
            connection_a
                .get_team_member_identity(&member_a.member_id)
                .expect("load route a identity after attest")
                .is_none(),
            "a session authenticated for route B must not mutate route A"
        );
    }

    #[test]
    fn durable_owner_authority_comparison_detects_generation_refreshes() {
        let path = fixture_abs_path("ee-responder-broker-refresh-unit");
        let current = ResponderRouteRegistry::new([route(path.clone(), 41888)])
            .expect("current route registry");
        let mut refreshed_route = route(path, 41888);
        refreshed_route.expectations.pair_key_generation = 2;
        refreshed_route.peer_transport_key_generation = 2;
        refreshed_route.grant_generation = 2;
        let refreshed =
            ResponderRouteRegistry::new([refreshed_route]).expect("refreshed route registry");
        assert!(!same_route_authority(&current, &refreshed));
        assert!(same_route_authority(&refreshed, &refreshed));
    }

    #[test]
    fn route_pair_key_generation_must_match_durable_record() {
        let workspace = tempfile::tempdir().expect("temp workspace");
        let store = MeshKeyStore::open_or_create(workspace.path()).expect("open key store");
        store
            .store_pair_key(
                "peer_0123456789abcdef0123456789abcdef",
                PairKeyClass::Current,
                std::num::NonZeroU64::new(2).expect("nonzero generation"),
                &crate::mesh::key_store::SecretBytes::new([7; 32]),
                "2026-08-08T00:00:00Z",
                false,
            )
            .expect("store generation-two key");
        let route = route(workspace.path().to_path_buf(), 41888);

        let error = load_route_pair_key(&route)
            .expect_err("route generation one must not consume durable generation two");
        assert!(matches!(error, ResponderBrokerError::PairingRequired));
    }

    #[test]
    fn authenticated_admission_rejects_oversized_body_fetch() {
        let oversize = serde_json::json!({
            "schema": "ee.mesh.body_fetch.request.v1",
            "bodyCacheKey": "x".repeat(600 * 1024),
        });
        let mut noisy = crate::mesh::admission::MeshPeerAdmissionState::new("peer-noisy");
        let error = admit_authenticated_capability(
            "peer-noisy",
            &FrameCapability::BodyFetch,
            &oversize,
            &mut noisy,
        )
        .expect_err("oversized body fetch");
        assert!(matches!(error, ResponderBrokerError::AdmissionLimited));
        assert!(noisy.malformed_frame_count > 0);
        let mut ok_peer = crate::mesh::admission::MeshPeerAdmissionState::new("peer-ok");
        let ok = admit_authenticated_capability(
            "peer-ok",
            &FrameCapability::Summary,
            &serde_json::json!({"schema": "ee.mesh.sync_round.v1"}),
            &mut ok_peer,
        )
        .expect("summary stays inside budget");
        assert!(ok.allowed());
        assert_eq!(ok_peer.in_flight_requests, 1);
    }

    #[test]
    fn errors_are_structured_and_do_not_echo_paths_or_routes() {
        for (error, expected_code, expected_severity) in [
            (
                ResponderBrokerError::WhoIsUnverified,
                MESH_BOOTSTRAP_IDENTITY_UNVERIFIED_CODE,
                "high",
            ),
            (
                ResponderBrokerError::RouteUnavailable,
                MESH_RESPONDER_ROUTE_UNAVAILABLE_CODE,
                "high",
            ),
            (
                ResponderBrokerError::KeyStoreUnavailable,
                MESH_KEY_STORE_UNAVAILABLE_CODE,
                "high",
            ),
            (
                ResponderBrokerError::PortConflict,
                MESH_RESPONDER_PORT_CONFLICT_CODE,
                "high",
            ),
            (
                ResponderBrokerError::PlatformUnsupported,
                "mesh_transport_unreachable",
                "warning",
            ),
        ] {
            let message = error.message();
            assert!(!message.contains('/'));
            assert!(!message.contains("team-a"));
            assert!(!message.contains("workspace-responder"));
            assert!(!error.repair().is_empty());
            assert_eq!(error.code(), expected_code);
            assert_eq!(error.severity(), expected_severity);
        }
    }

    #[test]
    fn status_never_claims_unperformed_protocol_work() {
        let status = ResponderBrokerStatus {
            schema: RESPONDER_BROKER_STATUS_SCHEMA_V1,
            state: ResponderBrokerState::Shutdown,
            bound_address: None,
            registered_routes: 1,
            preauth_inflight: 0,
            accepted_connections: 1,
            authenticated_sessions: 1,
            rejected_connections: 0,
            last_error_code: None,
            application_hello_performed: false,
            anti_entropy_performed: false,
            synchronized: false,
        };
        let value = serde_json::to_value(status).expect("serialize status");
        assert_eq!(value["applicationHelloPerformed"], false);
        assert_eq!(value["antiEntropyPerformed"], false);
        assert_eq!(value["synchronized"], false);
    }

    #[test]
    fn default_control_socket_follows_daemon_partition() {
        assert_eq!(
            default_responder_control_socket_path_with(
                |key| match key {
                    "XDG_RUNTIME_DIR" => Some("/run/user/1000".into()),
                    _ => None,
                },
                1000,
            ),
            PathBuf::from("/run/user/1000/ee/mesh-responder.sock")
        );
        assert_eq!(
            default_responder_control_socket_path_with(
                |key| match key {
                    "XDG_RUNTIME_DIR" => Some("/tmp".into()),
                    "TMPDIR" => Some("/var/tmp".into()),
                    _ => None,
                },
                501,
            ),
            PathBuf::from("/var/tmp/ee-501/mesh-responder.sock")
        );
    }

    #[test]
    #[cfg(windows)]
    fn default_control_socket_on_windows_is_not_under_workspace_ee() {
        let path = default_responder_control_socket_path_with(
            |key| match key {
                "LOCALAPPDATA" => Some(r"C:\Users\dev\AppData\Local".into()),
                _ => None,
            },
            0,
        );
        assert_eq!(
            path,
            PathBuf::from(r"C:\Users\dev\AppData\Local\eidetic-engine\mesh-responder.control")
        );
        assert!(!path.components().any(|part| part.as_os_str() == ".ee"));
    }

    #[test]
    #[cfg(windows)]
    fn windows_control_endpoint_publishes_and_status_submits() {
        let root = tempfile::TempDir::new().expect("temp");
        let path = root
            .path()
            .join("eidetic-engine")
            .join("mesh-responder.control");
        let listener = ResponderControlListener::publish(path.clone()).expect("publish");
        let endpoint = read_windows_control_endpoint(&path).expect("read endpoint");
        assert_eq!(endpoint.transport, "loopback_tcp");
        assert!(endpoint.port >= 1024);

        let server = listener.listener.try_clone().expect("clone listener");
        let server_thread = std::thread::spawn(move || {
            let (mut stream, peer) = loop {
                match server.accept() {
                    Ok(accepted) => break accepted,
                    Err(error) if error.kind() == io::ErrorKind::WouldBlock => {
                        std::thread::sleep(Duration::from_millis(5));
                    }
                    Err(error) => panic!("accept: {error}"),
                }
            };
            assert!(peer.ip().is_loopback());
            let request: ResponderControlRequest =
                read_control_frame(&mut stream).expect("read request");
            assert_eq!(request.op, ResponderControlOp::Status);
            let response = ResponderControlResponse {
                schema: RESPONDER_CONTROL_SCHEMA_V1.to_owned(),
                ok: true,
                nonce: request.nonce,
                code: None,
                message: None,
                bound_addresses: vec!["127.0.0.1:41888".to_owned()],
                registered_routes: 1,
            };
            write_control_frame(&mut stream, &response).expect("write response");
        });

        let response = submit_responder_control_request(&path, &responder_control_status_request())
            .expect("submit status");
        assert!(response.ok);
        assert_eq!(response.registered_routes, 1);
        assert_eq!(response.bound_addresses, vec!["127.0.0.1:41888".to_owned()]);
        server_thread.join().expect("server thread");
        drop(listener);
    }

    #[test]
    #[cfg(windows)]
    fn windows_owner_safe_path_accepts_existing_workspace() {
        let root = tempfile::TempDir::new().expect("temp");
        let workspace = root.path().to_path_buf();
        let ee = workspace.join(".ee");
        std::fs::create_dir(&ee).expect("ee dir");
        let database = ee.join("ee.db");
        std::fs::write(&database, b"").expect("db");

        let accepted_workspace =
            owner_safe_canonical_path(&workspace).expect("workspace must be owner-safe");
        let accepted_database =
            owner_safe_canonical_path(&database).expect("database must be owner-safe");
        assert!(accepted_workspace.is_absolute());
        assert!(paths_are_same_location(
            &accepted_database,
            &accepted_workspace.join(".ee").join("ee.db")
        ));

        let sneaky = workspace.join(".").join(".ee").join("ee.db");
        assert!(matches!(
            owner_safe_canonical_path(&sneaky),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));
        assert!(matches!(
            owner_safe_canonical_path(Path::new(r"relative\workspace")),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));

        let request = ResponderControlRequest {
            schema: RESPONDER_CONTROL_SCHEMA_V1.to_owned(),
            op: ResponderControlOp::Register,
            nonce: "0123456789abcdef".to_owned(),
            workspace_id: "wsp_wincontrol".to_owned(),
            team_id: "team_wincontrol".to_owned(),
            responder_node_id: "node_0123456789abcdef0123456789abcdef".to_owned(),
            workspace_path: workspace.clone(),
            database_path: database.clone(),
            peer_handles: vec!["peer_0123456789abcdef0123456789abcdef".to_owned()],
            committed_port: 41889,
        };
        assert!(validate_control_request(&request).is_ok());

        let verbatim_workspace = workspace.canonicalize().expect("canon workspace");
        let verbatim_database = database.canonicalize().expect("canon database");
        assert!(
            owner_safe_canonical_path(&verbatim_workspace).is_ok(),
            "canonical verbatim workspace must be owner-safe"
        );
        let mut verbatim_request = request;
        verbatim_request.workspace_path = verbatim_workspace;
        verbatim_request.database_path = verbatim_database;
        assert!(validate_control_request(&verbatim_request).is_ok());
    }

    #[test]
    fn status_control_request_skips_path_and_identity_gates() {
        assert!(validate_control_request(&responder_control_status_request()).is_ok());
    }

    #[test]
    fn control_request_rejects_network_shaped_fields_and_relative_paths() {
        let request = ResponderControlRequest {
            schema: RESPONDER_CONTROL_SCHEMA_V1.to_owned(),
            op: ResponderControlOp::Register,
            nonce: "0123456789abcdef".to_owned(),
            workspace_id: "wsp_test".to_owned(),
            team_id: "team_test".to_owned(),
            responder_node_id: "node_0123456789abcdef0123456789abcdef".to_owned(),
            workspace_path: PathBuf::from("relative/workspace"),
            database_path: PathBuf::from("relative/ee.db"),
            peer_handles: vec!["peer_0123456789abcdef0123456789abcdef".to_owned()],
            committed_port: 41888,
        };
        assert!(matches!(
            validate_control_request(&request),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));
        let mut unknown = request.clone();
        unknown.schema = "ee.mesh.event.v1".to_owned();
        unknown.workspace_path = PathBuf::from("/tmp/ee-control-unit");
        unknown.database_path = PathBuf::from("/tmp/ee-control-unit/.ee/ee.db");
        assert!(matches!(
            validate_control_request(&unknown),
            Err(ResponderBrokerError::InvalidConfiguration)
        ));
    }
}