udb 0.4.0

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

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use sqlx::{PgPool, Row};
use tokio::sync::{Mutex, broadcast};
use tokio_stream::Stream;
use tonic::{Request, Response, Status};
use uuid::Uuid;

use super::native_helpers::{
    admit_on as native_admit_on, emit_payload_event, native_next_page_token,
    native_next_page_token_for_total, native_offset_page_window, native_service_context,
    update_mask_allows, update_mask_path_set, validate_request_tenant,
};
use crate::ir::{
    ComparisonOp, ConflictStrategy, LogicalFilter, LogicalPagination, LogicalProjection,
    LogicalRead, LogicalRecord, LogicalSort, LogicalValue, NullOrder, SortDirection,
};
use crate::metrics::{MetricsRecorder, NoopMetrics};
use crate::proto::udb::core::webrtc::entity::v1 as webrtc_entity_pb;
use crate::proto::udb::core::webrtc::services::v1 as webrtc_pb;
use crate::runtime::DataBrokerRuntime;
use crate::runtime::channels::{ChannelManager, ChannelPermit, OperationChannel};
use crate::runtime::native_catalog::{NativeModel, native_model};

use webrtc_pb::peer_service_server::PeerService;
use webrtc_pb::room_service_server::RoomService;
use webrtc_pb::signaling_service_server::SignalingService;
use webrtc_pb::track_service_server::TrackService;
use webrtc_pb::turn_service_server::TurnService;

pub use webrtc_pb::peer_service_server::PeerServiceServer;
pub use webrtc_pb::room_service_server::RoomServiceServer;
pub use webrtc_pb::signaling_service_server::SignalingServiceServer;
pub use webrtc_pb::track_service_server::TrackServiceServer;
pub use webrtc_pb::turn_service_server::TurnServiceServer;

use super::DataBrokerService;

#[cfg(feature = "webrtc")]
mod sfu;
#[cfg(feature = "webrtc")]
mod sfu_livekit;

#[cfg(feature = "webrtc")]
#[async_trait::async_trait]
pub(crate) trait SfuBridge: Send + Sync {
    async fn accept_offer(
        &self,
        room_id: &str,
        peer_id: &str,
        offer_sdp: &str,
    ) -> Result<String, String>;

    async fn mint_join_token(
        &self,
        tenant_id: &str,
        room_id: &str,
        peer_id: &str,
        ttl_seconds: i64,
        now_unix: i64,
    ) -> Result<Option<SfuJoinToken>, String>;

    async fn register_published_track(
        &self,
        room_id: &str,
        peer_id: &str,
        track_id: &str,
        kind: &str,
    ) -> Result<(), String>;

    async fn unregister_track(&self, track_id: &str) -> Result<(), String>;

    async fn kick_peer(&self, tenant_id: &str, room_id: &str, peer_id: &str) -> Result<(), String>;

    async fn close_room_hook(&self, room_id: &str) -> Result<(), String>;
}

#[cfg(feature = "webrtc")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct SfuJoinToken {
    pub(crate) url: String,
    pub(crate) token: String,
    pub(crate) expires_at: i64,
}

#[cfg(feature = "webrtc")]
#[derive(Clone, Debug)]
struct UnavailableSfuBridge {
    reason: String,
}

#[cfg(feature = "webrtc")]
#[async_trait::async_trait]
impl SfuBridge for UnavailableSfuBridge {
    async fn accept_offer(
        &self,
        _room_id: &str,
        _peer_id: &str,
        _offer_sdp: &str,
    ) -> Result<String, String> {
        Err(self.reason.clone())
    }

    async fn mint_join_token(
        &self,
        _tenant_id: &str,
        _room_id: &str,
        _peer_id: &str,
        _ttl_seconds: i64,
        _now_unix: i64,
    ) -> Result<Option<SfuJoinToken>, String> {
        Err(self.reason.clone())
    }

    async fn register_published_track(
        &self,
        _room_id: &str,
        _peer_id: &str,
        _track_id: &str,
        _kind: &str,
    ) -> Result<(), String> {
        Err(self.reason.clone())
    }

    async fn unregister_track(&self, _track_id: &str) -> Result<(), String> {
        Err(self.reason.clone())
    }

    async fn kick_peer(
        &self,
        _tenant_id: &str,
        _room_id: &str,
        _peer_id: &str,
    ) -> Result<(), String> {
        Err(self.reason.clone())
    }

    async fn close_room_hook(&self, _room_id: &str) -> Result<(), String> {
        Err(self.reason.clone())
    }
}

#[cfg(feature = "webrtc")]
fn resolve_sfu_bridge() -> Option<Arc<dyn SfuBridge>> {
    match sfu_livekit::LiveKitSfuBridge::from_env() {
        Ok(Some(bridge)) => return Some(Arc::new(bridge)),
        Ok(None) => {}
        Err(err) => {
            tracing::warn!(error = %err, "LiveKit SFU bridge mounted degraded by invalid configuration");
            return Some(Arc::new(UnavailableSfuBridge { reason: err }));
        }
    }
    if sfu::EmbeddedSfu::enabled_from_env() {
        Some(Arc::new(sfu::EmbeddedSfu::default()))
    } else {
        None
    }
}

const ROOM_MSG: &str = "udb.core.webrtc.entity.v1.Room";
const PEER_MSG: &str = "udb.core.webrtc.entity.v1.Peer";
const TRACK_MSG: &str = "udb.core.webrtc.entity.v1.Track";

/// Default TURN credential lifetime (1 day) when `UDB_TURN_TTL_SECONDS` is unset.
const DEFAULT_TURN_TTL_SECS: i64 = 86_400;
/// Default ICE server advertised when `UDB_TURN_URLS` is unset.
const DEFAULT_STUN_URL: &str = "stun:stun.l.google.com:19302";
/// Action bound into TURN REST credentials; the credential only authorizes media relay.
const TURN_RELAY_ACTION: &str = "webrtc.turn.relay";
/// Per-room signaling broadcast channel capacity (buffered SDP/ICE fan-out).
const SIGNALING_BROADCAST_CAPACITY: usize = 256;
/// Inbound signaling pump: re-validate room membership and refresh the peer's
/// last-seen heartbeat after this many relayed frames…
const SIGNAL_MEMBERSHIP_RECHECK_FRAMES: u64 = 64;
/// …or after this many seconds since the last check, whichever comes first.
/// Must be smaller than the stale-peer timeout so live peers keep their
/// heartbeat fresh.
const SIGNAL_MEMBERSHIP_RECHECK_SECS: u64 = 20;
/// Default interval between stale-peer reaper sweeps
/// (`UDB_WEBRTC_REAP_INTERVAL_SECS` overrides; 0 disables the reaper).
const STALE_PEER_REAP_INTERVAL_SECS_DEFAULT: u64 = 60;
/// Default heartbeat age after which a CONNECTED peer with no signaling
/// activity is reaped (`UDB_WEBRTC_STALE_PEER_TIMEOUT_SECS` overrides;
/// 0 disables the reaper).
const STALE_PEER_TIMEOUT_SECS_DEFAULT: i64 = 120;
/// Bounded batch per stale-peer reaper sweep.
const STALE_PEER_REAP_BATCH_SIZE: i64 = 500;

// ── Machine-readable failure reasons ─────────────────────────────────────────
// Stable `reason` codes attached to the otherwise-opaque `Status` so SDK callers
// can branch on the failure cause without string-matching the human message.
// These do NOT change the gRPC status code or fail-closed behaviour — they only
// ADD a `error-reason` trailer (the tonic-native way to carry a machine-readable
// reason without pulling in the `tonic-types`/google.rpc.ErrorInfo dependency).
/// Room rejected a join: it is at capacity.
const ROOM_FULL: &str = "ROOM_FULL";
/// Room rejected a join: it is CLOSED (reserved; surfaced on the join path).
#[allow(dead_code)]
const ROOM_CLOSED: &str = "ROOM_CLOSED";
/// Peer is not an active (CONNECTED) member of an ACTIVE room.
const PEER_NOT_ACTIVE: &str = "PEER_NOT_ACTIVE";
/// No TURN secret configured; credentials cannot be minted.
const TURN_NOT_CONFIGURED: &str = "TURN_NOT_CONFIGURED";
const TURN_NOT_CONFIGURED_MESSAGE: &str =
    "TURN secret not configured; set UDB_TURN_SECRET to issue credentials";
/// SFU backend configured but unable to mint/control a join.
#[cfg(feature = "webrtc")]
const SFU_BACKEND_UNAVAILABLE: &str = "SFU_BACKEND_UNAVAILABLE";
/// Egress requested but the operator has not enabled it (`UDB_WEBRTC_EGRESS_ENABLED` unset).
const EGRESS_NOT_ENABLED: &str = "EGRESS_NOT_ENABLED";
const EGRESS_NOT_ENABLED_MESSAGE: &str =
    "webrtc egress is not enabled; set UDB_WEBRTC_EGRESS_ENABLED=1 and wire an egress backend";
/// Egress enabled but no recording/egress backend is wired (mounted but degraded).
const EGRESS_BACKEND_UNAVAILABLE: &str = "EGRESS_BACKEND_UNAVAILABLE";
const EGRESS_BACKEND_UNAVAILABLE_MESSAGE: &str =
    "webrtc egress is enabled but no egress backend is wired (mounted but degraded)";
/// Egress id belongs to a different tenant than the authenticated request.
const EGRESS_TENANT_SCOPE_MISMATCH: &str = "EGRESS_TENANT_SCOPE_MISMATCH";
const EGRESS_TENANT_SCOPE_MISMATCH_MESSAGE: &str =
    "egress_id is not scoped to the authenticated tenant";

// ── Egress (master-plan 5.5) ─────────────────────────────────────────────────
/// Egress outbox topics (dotted form, matching the `emits` contract on the
/// RoomService egress RPCs in webrtc_service.proto). The CDC engine tails the
/// outbox → Kafka under these exact names.
const EGRESS_STARTED_TOPIC: &str = "udb.webrtc.egress.started.v1";
const EGRESS_STOPPED_TOPIC: &str = "udb.webrtc.egress.stopped.v1";
const EGRESS_FAILED_TOPIC: &str = "udb.webrtc.egress.failed.v1";
/// Prefix that binds a server-derived egress id to its owning tenant.
const EGRESS_ID_PREFIX: &str = "eg";

/// Egress capability state, resolved ONCE at service construction so the gate is
/// not re-read from the environment per request.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum EgressCapability {
    /// `UDB_WEBRTC_EGRESS_ENABLED` unset/false — egress is off (fail closed).
    Disabled,
    /// Flag set, but no recording/egress backend is wired yet — the capability
    /// is mounted but degraded; start/stop/list fail closed honestly.
    EnabledNoBackend,
}

/// Resolve the egress capability once from the environment. Default (unset) is
/// [`EgressCapability::Disabled`] — fail closed. A real backend would upgrade the
/// returned state to a future `Ready` variant.
fn resolve_egress_capability() -> EgressCapability {
    let enabled = std::env::var("UDB_WEBRTC_EGRESS_ENABLED")
        .ok()
        .map(|v| {
            matches!(
                v.trim().to_ascii_lowercase().as_str(),
                "1" | "true" | "yes" | "on"
            )
        })
        .unwrap_or(false);
    if enabled {
        EgressCapability::EnabledNoBackend
    } else {
        EgressCapability::Disabled
    }
}

/// Server-derived, tenant-scoped egress id. Encodes the verified tenant so a
/// later Stop/List can reject an id that is not scoped to the caller's tenant
/// (cross-tenant leak guard). The caller NEVER supplies an egress_id on start.
fn new_egress_id(tenant_id: &str) -> String {
    format!("{EGRESS_ID_PREFIX}-{}-{}", tenant_id.trim(), Uuid::new_v4())
}

/// True iff `egress_id` was minted for `tenant_id` (see [`new_egress_id`]): it
/// must carry the `eg-<tenant>-` prefix and a parseable UUID suffix.
fn egress_id_is_tenant_scoped(egress_id: &str, tenant_id: &str) -> bool {
    let want = format!("{EGRESS_ID_PREFIX}-{}-", tenant_id.trim());
    egress_id
        .strip_prefix(&want)
        .map(|rest| Uuid::parse_str(rest).is_ok())
        .unwrap_or(false)
}

fn webrtc_policy_status_with_reason(
    operation: &'static str,
    policy_decision_id: &'static str,
    message: impl Into<String>,
    reason: &'static str,
) -> Status {
    webrtc_policy_status_with_code_and_reason(
        tonic::Code::FailedPrecondition,
        operation,
        policy_decision_id,
        message,
        reason,
    )
}

fn webrtc_policy_status_with_code_and_reason(
    code: tonic::Code,
    operation: &'static str,
    policy_decision_id: &'static str,
    message: impl Into<String>,
    reason: &'static str,
) -> Status {
    let mut status = crate::runtime::executor_utils::policy_status_with_code(
        code,
        operation,
        policy_decision_id,
        message,
    );
    status.metadata_mut().insert(
        "error-reason",
        tonic::metadata::MetadataValue::from_static(reason),
    );
    status
}

fn peer_not_active_status() -> Status {
    webrtc_policy_status_with_reason(
        "require_active_peer_membership",
        "peer_not_active",
        "peer is not an active member of this room",
        PEER_NOT_ACTIVE,
    )
}

fn peer_not_active_permission_status() -> Status {
    webrtc_policy_status_with_code_and_reason(
        tonic::Code::PermissionDenied,
        "signal_peer_membership",
        "peer_not_active",
        "peer is not an active member of this room",
        PEER_NOT_ACTIVE,
    )
}

fn room_not_joinable_status() -> Status {
    webrtc_policy_status_with_reason(
        "join_room",
        "room_not_joinable",
        "room not found, closed, or at capacity",
        ROOM_FULL,
    )
}

fn egress_tenant_scope_mismatch_status() -> Status {
    webrtc_policy_status_with_code_and_reason(
        tonic::Code::PermissionDenied,
        "stop_egress",
        "egress_tenant_scope_mismatch",
        EGRESS_TENANT_SCOPE_MISMATCH_MESSAGE,
        EGRESS_TENANT_SCOPE_MISMATCH,
    )
}

/// TURN (coturn) configuration, read from the environment once at build time.
///
/// Credentials follow the long-term-secret REST scheme (RFC 5766-bis / coturn
/// `use-auth-secret`): the broker and coturn share `secret`; the broker mints
/// short-lived `username = "<expiry-unix>:<principal>"` and
/// `credential = base64(HMAC-SHA1(secret, username))`.
#[derive(Clone, Debug)]
struct TurnConfig {
    /// Shared coturn secret, or `None` when unconfigured in production (fail
    /// closed — `IssueCredentials` then rejects rather than minting with a
    /// well-known dev secret). Resolved via `security::resolve_turn_secret`.
    secret: Option<Vec<u8>>,
    urls: Vec<String>,
    default_ttl: i64,
}

impl TurnConfig {
    fn from_env() -> Self {
        let secret = crate::runtime::security::resolve_turn_secret();
        let urls = std::env::var("UDB_TURN_URLS")
            .ok()
            .filter(|s| !s.trim().is_empty())
            .map(|s| {
                s.split(',')
                    .map(|u| u.trim().to_string())
                    .filter(|u| !u.is_empty())
                    .collect::<Vec<_>>()
            })
            .unwrap_or_else(|| vec![DEFAULT_STUN_URL.to_string()]);
        Self {
            secret,
            urls,
            default_ttl: DEFAULT_TURN_TTL_SECS,
        }
    }
}

/// One broadcast frame on a room's signaling channel: a relayed signaling
/// payload, a targeted peer-closed marker that ends ONLY the addressed peer's
/// subscriber stream (`leave_room`), or the terminal room-closed marker that
/// ends every subscriber stream (`close_room`). There are no proto-level
/// PeerClosed/RoomClosed signal payloads, so termination is hub-internal and
/// surfaces to clients as end-of-stream.
#[derive(Clone)]
enum HubFrame {
    Signal(webrtc_pb::SignalResponse),
    /// End-of-stream for the one peer whose `peer_id` this carries; every
    /// other subscriber skips the frame and keeps relaying.
    PeerClosed(String),
    RoomClosed,
}

/// Per-room live channel: the broadcast sender plus a closed flag the inbound
/// pumps consult before relaying. Once a room is closed, racing senders become
/// no-ops instead of delivering frames into a dead room.
#[derive(Clone)]
struct RoomChannel {
    tx: broadcast::Sender<HubFrame>,
    closed: Arc<AtomicBool>,
}

impl RoomChannel {
    fn send_signal(&self, resp: webrtc_pb::SignalResponse) {
        if !self.closed.load(Ordering::SeqCst) {
            let _ = self.tx.send(HubFrame::Signal(resp));
        }
    }

    fn is_closed(&self) -> bool {
        self.closed.load(Ordering::SeqCst)
    }

    fn subscribe(&self) -> broadcast::Receiver<HubFrame> {
        self.tx.subscribe()
    }
}

/// Transient per-room signaling fan-out. Each room gets a broadcast channel;
/// every connected peer subscribes and publishes SDP/ICE through it. This is
/// live-connection routing state, not canonical data — it is rebuilt on connect
/// and lost on restart by design.
#[derive(Default)]
struct SignalingHub {
    rooms: Mutex<HashMap<String, RoomChannel>>,
}

impl SignalingHub {
    async fn channel(&self, room_id: &str) -> RoomChannel {
        let mut guard = self.rooms.lock().await;
        guard
            .entry(room_id.to_string())
            .or_insert_with(|| RoomChannel {
                tx: broadcast::channel(SIGNALING_BROADCAST_CAPACITY).0,
                closed: Arc::new(AtomicBool::new(false)),
            })
            .clone()
    }

    async fn prune_if_idle(&self, room_id: &str) {
        let mut guard = self.rooms.lock().await;
        let should_remove = guard
            .get(room_id)
            .map(|ch| ch.tx.receiver_count() == 0)
            .unwrap_or(false);
        if should_remove {
            guard.remove(room_id);
        }
    }

    /// Terminate a room's signaling: mark the channel closed (racing senders
    /// become no-ops), broadcast the terminal [`HubFrame::RoomClosed`] so every
    /// subscriber stream ends, and drop the hub entry. Whole-room teardown —
    /// `close_room` only; a single peer leaving uses [`Self::close_peer`].
    async fn close(&self, room_id: &str) {
        let entry = { self.rooms.lock().await.remove(room_id) };
        if let Some(ch) = entry {
            ch.closed.store(true, Ordering::SeqCst);
            let _ = ch.tx.send(HubFrame::RoomClosed);
        }
    }

    /// Terminate ONLY one peer's subscriber stream: broadcast the targeted
    /// [`HubFrame::PeerClosed`] which the outbound pump treats as end-of-stream
    /// for the addressed peer and a no-op for everyone else. The room channel
    /// stays open so the remaining peers keep signaling (`leave_room` path).
    async fn close_peer(&self, room_id: &str, peer_id: &str) {
        let entry = { self.rooms.lock().await.get(room_id).cloned() };
        if let Some(ch) = entry
            && !ch.is_closed()
        {
            let _ = ch.tx.send(HubFrame::PeerClosed(peer_id.to_string()));
        }
    }
}

/// Postgres-backed WebRTC control-plane handler. One struct implements all five
/// tonic services (Room/Peer/Track/Turn/Signaling); it is cloned per server when
/// registered on the control-plane listener.
#[derive(Clone)]
pub struct WebrtcServiceImpl {
    pg_pool: Option<PgPool>,
    /// Runtime handle for the typed native-entity data-plane path
    /// (`native_entity_read_for_service`). Room/peer/track CRUD reads + simple
    /// inserts ride the IR compiler → backend dispatch (extend_udb.md P4); the
    /// genuinely atomic/arithmetic lifecycle writes keep their bespoke SQL on
    /// `pg_pool` (capability-gated escape hatch, P4.D). `None` in bare test
    /// construction — `build_webrtc_service` always wires it in production.
    runtime: Option<Arc<DataBrokerRuntime>>,
    turn: TurnConfig,
    signaling: Arc<SignalingHub>,
    #[cfg(feature = "webrtc")]
    /// Optional SFU bridge. Resolved once from deployment config; `None` keeps
    /// the default mesh signaling path without per-request env reads.
    sfu_bridge: Option<Arc<dyn SfuBridge>>,
    /// Schema-qualified outbox table (`udb_system.outbox_events`) the CDC engine
    /// tails → Apache Kafka → consumers. `None` = no domain-event emission.
    outbox_relation: Option<String>,
    /// Per-tenant fair-admission manager (the SAME one the data plane uses via
    /// `execute_with_channel_scoped`). Join/publish/signal entry points acquire a
    /// per-tenant `Admin` budget through this so one tenant can't starve the
    /// shared signaling/control plane. `None` only in test construction (no
    /// runtime wired) — `build_webrtc_service` always wires it in production.
    channels: Option<ChannelManager>,
    metrics: Arc<dyn MetricsRecorder>,
    /// Recording/egress capability, resolved ONCE at construction from
    /// `UDB_WEBRTC_EGRESS_ENABLED` (master-plan 5.5). Default = disabled.
    egress: EgressCapability,
}

fn webrtc_capability_status(
    operation: &'static str,
    capability_required: &'static str,
    message: impl Into<String>,
) -> Status {
    crate::runtime::executor_utils::capability_status(
        "webrtc",
        operation,
        capability_required,
        message,
    )
}

fn webrtc_internal_status(operation: impl Into<String>, message: impl Into<String>) -> Status {
    crate::runtime::executor_utils::internal_status("webrtc", operation, message)
}

fn webrtc_capability_status_with_reason(
    operation: &'static str,
    capability_required: &'static str,
    message: impl Into<String>,
    reason: &'static str,
) -> Status {
    let mut status = webrtc_capability_status(operation, capability_required, message);
    status.metadata_mut().insert(
        "error-reason",
        tonic::metadata::MetadataValue::from_static(reason),
    );
    status
}

fn webrtc_schema_not_found_status(
    operation: &'static str,
    schema_code: &'static str,
    message: &'static str,
) -> Status {
    crate::runtime::executor_utils::schema_status(
        tonic::Code::NotFound,
        "webrtc",
        operation,
        schema_code,
        message,
    )
}

#[cfg(feature = "webrtc")]
fn sfu_backend_unavailable_status(err: impl std::fmt::Display) -> Status {
    webrtc_capability_status_with_reason(
        "sfu_join_token",
        "sfu_backend",
        format!("SFU backend unavailable: {err}"),
        SFU_BACKEND_UNAVAILABLE,
    )
}

fn turn_secret_not_configured_status(operation: &'static str) -> Status {
    webrtc_capability_status_with_reason(
        operation,
        "turn_secret",
        TURN_NOT_CONFIGURED_MESSAGE,
        TURN_NOT_CONFIGURED,
    )
}

fn egress_not_enabled_status(operation: &'static str) -> Status {
    webrtc_capability_status_with_reason(
        operation,
        "webrtc_egress_enabled",
        EGRESS_NOT_ENABLED_MESSAGE,
        EGRESS_NOT_ENABLED,
    )
}

fn egress_backend_unavailable_status(operation: &'static str) -> Status {
    webrtc_capability_status_with_reason(
        operation,
        "webrtc_egress_backend",
        EGRESS_BACKEND_UNAVAILABLE_MESSAGE,
        EGRESS_BACKEND_UNAVAILABLE,
    )
}

impl WebrtcServiceImpl {
    pub fn new() -> Self {
        Self {
            pg_pool: None,
            runtime: None,
            turn: TurnConfig::from_env(),
            signaling: Arc::new(SignalingHub::default()),
            #[cfg(feature = "webrtc")]
            sfu_bridge: resolve_sfu_bridge(),
            outbox_relation: None,
            channels: None,
            metrics: Arc::new(NoopMetrics),
            egress: resolve_egress_capability(),
        }
    }

    #[cfg(feature = "webrtc")]
    pub(crate) fn with_sfu_bridge(mut self, bridge: Option<Arc<dyn SfuBridge>>) -> Self {
        self.sfu_bridge = bridge;
        self
    }

    /// Wire the shared per-tenant fair-admission manager (same one the data plane
    /// uses) so join/publish/signal entry points are bounded per tenant.
    pub(crate) fn with_channels(mut self, channels: Option<ChannelManager>) -> Self {
        self.channels = channels;
        self
    }

    /// Per-tenant fair admission for a webrtc control-plane mutation. Acquires the
    /// shared `Admin` channel budget SCOPED to the validated tenant so a single
    /// tenant cannot starve the shared signaling/control plane — mirrors the data
    /// plane's `execute_with_channel_scoped`. On exhaustion returns the same
    /// `Status::resource_exhausted` backpressure. The returned [`ChannelPermit`]
    /// is dropped (released) as soon as admission succeeds for the streaming
    /// `Signal` RPC — it gates ENTRY only and is NOT held across the long-lived
    /// stream, so it can never deadlock the relay. `None` channels (test mode)
    /// admit without a permit.
    ///
    /// `tenant` MUST be the VALIDATED tenant (post `validate_request_*`).
    async fn admit(&self, tenant: &str) -> Result<Option<ChannelPermit>, Status> {
        native_admit_on(
            self.channels.as_ref(),
            &self.metrics,
            "webrtc",
            OperationChannel::Admin,
            tenant,
            None,
        )
        .await
    }

    /// Lighter per-tenant fair admission for a READ RPC (get/list room/peer/track).
    /// Acquires the cheap `Read` channel budget scoped to the validated tenant so
    /// one tenant cannot exhaust the shared pool with reads.
    async fn admit_read(&self, tenant: &str) -> Result<Option<ChannelPermit>, Status> {
        native_admit_on(
            self.channels.as_ref(),
            &self.metrics,
            "webrtc",
            OperationChannel::Read,
            tenant,
            None,
        )
        .await
    }

    pub(crate) fn with_metrics(mut self, metrics: Arc<dyn MetricsRecorder>) -> Self {
        self.metrics = metrics;
        self
    }

    pub fn with_postgres(mut self, pool: Option<PgPool>) -> Self {
        self.pg_pool = pool;
        self
    }

    /// Wire the runtime used for the typed native-entity path (room/peer/track
    /// CRUD reads + simple inserts compile through the IR and dispatch to the
    /// proto-bound backend). `build_webrtc_service` always wires it.
    pub(crate) fn with_runtime(mut self, runtime: Option<Arc<DataBrokerRuntime>>) -> Self {
        self.runtime = runtime;
        self
    }

    /// The typed-path runtime; fail closed when absent (bare test construction).
    fn require_runtime(&self) -> Result<&DataBrokerRuntime, Status> {
        self.runtime.as_deref().ok_or_else(|| {
            webrtc_capability_status(
                "native_entity_dispatch",
                "runtime_native_entity_dispatch",
                "webrtc service requires runtime native entity dispatch",
            )
        })
    }

    /// Wire the transactional outbox so room/peer/track mutations publish domain
    /// events to Kafka (via the CDC relay). `relation` is the schema-qualified
    /// table, e.g. `"udb_system"."outbox_events"` (`CdcConfig::outbox_relation`).
    pub fn with_outbox(mut self, relation: Option<String>) -> Self {
        self.outbox_relation = relation;
        self
    }

    /// Room/peer/track CRUD is durable-only: fail closed without a Postgres pool.
    fn require_pool(&self) -> Result<&PgPool, Status> {
        self.pg_pool.as_ref().ok_or_else(|| {
            webrtc_capability_status(
                "postgres_store",
                "postgres_store",
                "webrtc service requires a Postgres-backed store (no PG pool configured)",
            )
        })
    }

    // ── Egress capability (master-plan 5.5) ──────────────────────────────────

    /// Honest egress capability label for health/capabilities reporting:
    /// `"disabled"` (flag unset) or `"degraded"` (flag set but no egress backend
    /// wired — mounted but degraded). Becomes `"ready"` once a backend exists.
    pub(crate) fn egress_capability_label(&self) -> &'static str {
        match self.egress {
            EgressCapability::Disabled => "disabled",
            EgressCapability::EnabledNoBackend => "degraded",
        }
    }

    /// Flag gate: fail closed with `FAILED_PRECONDITION`/`EGRESS_NOT_ENABLED`
    /// when the operator has not enabled egress. NEVER `UNIMPLEMENTED` (the
    /// capability-lie / retry-storm guard).
    fn require_egress_enabled(&self, operation: &'static str) -> Result<(), Status> {
        match self.egress {
            EgressCapability::Disabled => Err(egress_not_enabled_status(operation)),
            EgressCapability::EnabledNoBackend => Ok(()),
        }
    }

    /// `FAILED_PRECONDITION`/`EGRESS_BACKEND_UNAVAILABLE` — egress is enabled but
    /// no recording/egress backend is wired (mounted but degraded).
    fn egress_backend_unavailable(operation: &'static str) -> Status {
        egress_backend_unavailable_status(operation)
    }

    /// Backend seam: start a recording/egress job. Returns the lifecycle status
    /// (`EgressStatus::Starting`) once a real backend is wired; until then it is
    /// the mounted-but-degraded fail-closed path.
    async fn start_egress_backend(
        &self,
        _egress_id: &str,
        _room_id: &str,
        _track_id: Option<&str>,
        operation: &'static str,
        _kind: &str,
    ) -> Result<i32, Status> {
        Err(Self::egress_backend_unavailable(operation))
    }

    /// Backend seam: stop a running egress job. Returns `EgressStatus::Stopping`
    /// once a real backend is wired.
    async fn stop_egress_backend(
        &self,
        _egress_id: &str,
        operation: &'static str,
    ) -> Result<i32, Status> {
        Err(Self::egress_backend_unavailable(operation))
    }

    /// Backend seam: list the tenant's egress jobs. Returns the live jobs once a
    /// real backend is wired; until then it fails closed (degraded).
    async fn list_egress_backend(
        &self,
        _tenant_id: &str,
        _room_id: &str,
        operation: &'static str,
    ) -> Result<Vec<webrtc_pb::EgressInfo>, Status> {
        Err(Self::egress_backend_unavailable(operation))
    }

    /// Emit the `EgressStarted` outbox event (partition by room).
    async fn emit_egress_started(
        &self,
        pool: &PgPool,
        egress_id: &str,
        tenant_id: &str,
        room_id: &str,
        track_id: Option<&str>,
        kind: &str,
    ) {
        emit_payload_event(
            pool,
            self.outbox_relation.as_deref(),
            EGRESS_STARTED_TOPIC,
            room_id,
            serde_json::json!({
                "egress_id": egress_id,
                "tenant_id": tenant_id,
                "room_id": room_id,
                "track_id": track_id.unwrap_or(""),
                "kind": kind,
            }),
            Some(&self.metrics),
        )
        .await;
    }

    /// Emit the `EgressStopped` outbox event (partition by tenant).
    async fn emit_egress_stopped(
        &self,
        pool: &PgPool,
        egress_id: &str,
        tenant_id: &str,
        room_id: &str,
    ) {
        emit_payload_event(
            pool,
            self.outbox_relation.as_deref(),
            EGRESS_STOPPED_TOPIC,
            tenant_id,
            serde_json::json!({
                "egress_id": egress_id,
                "tenant_id": tenant_id,
                "room_id": room_id,
            }),
            Some(&self.metrics),
        )
        .await;
    }

    /// Emit the `EgressFailed` outbox event (partition by tenant). Records a
    /// real failed transition — including the mounted-but-degraded case where the
    /// operator enabled egress but no backend is wired — so the failure is
    /// observable on the event stream, not just a per-request error.
    async fn emit_egress_failed(
        &self,
        pool: &PgPool,
        egress_id: &str,
        tenant_id: &str,
        room_id: &str,
        reason: &str,
    ) {
        emit_payload_event(
            pool,
            self.outbox_relation.as_deref(),
            EGRESS_FAILED_TOPIC,
            tenant_id,
            serde_json::json!({
                "egress_id": egress_id,
                "tenant_id": tenant_id,
                "room_id": room_id,
                "reason": reason,
            }),
            Some(&self.metrics),
        )
        .await;
    }

    async fn require_active_peer_membership(
        &self,
        pool: &PgPool,
        tenant_id: Uuid,
        room_id: Uuid,
        peer_id: Uuid,
    ) -> Result<(), Status> {
        let pm = peer_model();
        let rm = room_model();
        // Transitional P4 gate: membership validation is a cross-entity join with
        // state guards; keep it on the Postgres-native path until typed native
        // reads expose a portable join/capability contract.
        let member: Option<i32> = sqlx::query_scalar(&format!(
            "SELECT 1 FROM {prel} p \
             JOIN {rrel} r ON r.{rrid} = p.{prid} AND r.{rtid} = p.{ptid} \
             WHERE p.{ppid} = $1::UUID AND p.{prid} = $2::UUID AND p.{ptid} = $3::UUID \
               AND p.{pdel} IS NULL AND p.{pstate} = 'CONNECTED' \
               AND r.{rdel} IS NULL AND r.{rstate} = 'ACTIVE' \
             LIMIT 1",
            prel = pm.relation,
            rrel = rm.relation,
            rrid = rm.q("room_id"),
            rtid = rm.q("tenant_id"),
            prid = pm.q("room_id"),
            ptid = pm.q("tenant_id"),
            ppid = pm.q("peer_id"),
            pdel = pm.q("deleted_at"),
            pstate = pm.q("state"),
            rdel = rm.q("deleted_at"),
            rstate = rm.q("state"),
        ))
        .bind(peer_id)
        .bind(room_id)
        .bind(tenant_id)
        .fetch_optional(pool)
        .await
        .map_err(|err| {
            webrtc_internal_status(
                "require_active_peer_membership",
                format!("verify peer membership failed: {err}"),
            )
        })?;
        if member.is_none() {
            return Err(peer_not_active_status());
        }
        Ok(())
    }

    /// Shared join body for `join_room` and `join_session`: atomically claim a
    /// participant slot (capacity-CAS), insert the peer, reload the self + other
    /// peers, fan out the `PeerJoined` signal, and emit the join outbox event.
    /// Returns the freshly-inserted peer, the other room peers, and the new
    /// peer_id. Callers MUST have already validated the tenant and acquired the
    /// admission permit; identity/tenant come only from the validated inputs.
    async fn join_room_inner(
        &self,
        tenant_id_str: &str,
        room_id_str: &str,
        display_name: &str,
        metadata_raw: &str,
        user_agent: &str,
    ) -> Result<(webrtc_entity_pb::Peer, Vec<webrtc_entity_pb::Peer>, String), Status> {
        let tenant_id = parse_uuid("tenant_id", tenant_id_str)?;
        let room_id = parse_uuid("room_id", room_id_str)?;
        let pool = self.require_pool()?;
        let m = peer_model();
        let rel = m.relation.clone();
        let peer_id = Uuid::new_v4().to_string();
        let metadata = non_empty_json(metadata_raw);

        // Atomically claim a participant slot: increment participant_count only if
        // the room exists, belongs to the tenant, is open, and has capacity. 0 rows
        // ⇒ reject — no peer is inserted into a missing/foreign/closed/full room,
        // and capacity is enforced atomically (no TOCTOU).
        // Transitional P4 gate: this compare-and-increment is intentionally not
        // lowered to generic typed CRUD until the native path has a CAS/update
        // capability that every eligible backend can declare honestly.
        let rm = room_model();
        let claimed = sqlx::query(&format!(
            "UPDATE {room_rel} SET {count} = {count} + 1 \
             WHERE {rid} = $1::UUID AND {rtid} = $2::UUID AND {deleted} IS NULL \
               AND {state} <> 'CLOSED' AND ({max} = 0 OR {count} < {max})",
            room_rel = rm.relation,
            count = rm.q("participant_count"),
            rid = rm.q("room_id"),
            rtid = rm.q("tenant_id"),
            deleted = rm.q("deleted_at"),
            state = rm.q("state"),
            max = rm.q("max_participants"),
        ))
        .bind(room_id)
        .bind(tenant_id)
        .execute(pool)
        .await
        .map_err(|err| {
            webrtc_internal_status("join_room", format!("claim room slot failed: {err}"))
        })?;
        if claimed.rows_affected() == 0 {
            return Err(room_not_joinable_status());
        }

        // Insert the peer; release the claimed slot on failure.
        if let Err(err) = sqlx::query(&format!(
            "INSERT INTO {rel} \
             ({peer_id}, {room_id}, {tenant_id}, {display_name}, {state}, {metadata}, {user_agent}, {joined_at}) \
             VALUES ($1::UUID, $2::UUID, $3::UUID, $4, 'CONNECTED', $5::JSONB, $6, CURRENT_TIMESTAMP)",
            peer_id = m.q("peer_id"),
            room_id = m.q("room_id"),
            tenant_id = m.q("tenant_id"),
            display_name = m.q("display_name"),
            state = m.q("state"),
            metadata = m.q("metadata"),
            user_agent = m.q("user_agent"),
            joined_at = m.q("joined_at"),
        ))
        .bind(&peer_id)
        .bind(room_id)
        .bind(tenant_id)
        .bind(display_name)
        .bind(&metadata)
        .bind(user_agent)
        .execute(pool)
        .await
        {
            let _ = sqlx::query(&format!(
                "UPDATE {room_rel} SET {count} = GREATEST({count} - 1, 0) \
                 WHERE {rid} = $1::UUID AND {deleted} IS NULL",
                room_rel = rm.relation,
                count = rm.q("participant_count"),
                rid = rm.q("room_id"),
                deleted = rm.q("deleted_at"),
            ))
            .bind(room_id)
            .execute(pool)
            .await;
            return Err(webrtc_internal_status(
                "join_room",
                format!("join room failed: {err}"),
            ));
        }

        // Fetch the freshly-created peer + the other peers already in the room.
        let projection = peer_select_projection(&m);
        let self_row = sqlx::query(&format!(
            "SELECT {projection} FROM {rel} WHERE {peer_id} = $1::UUID",
            peer_id = m.q("peer_id"),
        ))
        .bind(parse_uuid("peer_id", &peer_id)?)
        .fetch_one(pool)
        .await
        .map_err(|err| webrtc_internal_status("join_room", format!("reload peer failed: {err}")))?;
        let peer = peer_from_row(&self_row)?;

        let other_rows = sqlx::query(&format!(
            "SELECT {projection} FROM {rel} \
             WHERE {room_id} = $1::UUID AND {peer_id} <> $2::UUID \
               AND {deleted} IS NULL AND {state} <> 'CLOSED' \
             ORDER BY {display_name}",
            room_id = m.q("room_id"),
            peer_id = m.q("peer_id"),
            deleted = m.q("deleted_at"),
            state = m.q("state"),
            display_name = m.q("display_name"),
        ))
        .bind(room_id)
        .bind(parse_uuid("peer_id", &peer_id)?)
        .fetch_all(pool)
        .await
        .map_err(|err| {
            webrtc_internal_status("join_room", format!("list existing peers failed: {err}"))
        })?;
        let mut existing_peers = Vec::with_capacity(other_rows.len());
        for row in &other_rows {
            existing_peers.push(peer_from_row(row)?);
        }

        // Notify the room's signaling subscribers that a peer joined.
        let tx = self.signaling.channel(room_id_str).await;
        tx.send_signal(webrtc_pb::SignalResponse {
            payload: Some(webrtc_pb::signal_response::Payload::PeerJoined(
                peer_id.clone(),
            )),
        });

        emit_payload_event(
            pool,
            self.outbox_relation.as_deref(),
            "udb.webrtc.peer.joined.v1",
            room_id_str,
            serde_json::json!({
                "room_id": room_id_str.to_string(),
                "peer_id": peer_id.clone(),
                "tenant_id": tenant_id_str.to_string(),
            }),
            Some(&self.metrics),
        )
        .await;

        Ok((peer, existing_peers, peer_id))
    }

    pub(crate) async fn require_active_signal_membership(
        &self,
        req: &webrtc_pb::SignalRequest,
    ) -> Result<(String, String, String), Status> {
        let bound_room = req.room_id.trim().to_string();
        let bound_peer = req.peer_id.trim().to_string();
        let bound_tenant = req.tenant_id.trim().to_string();
        if bound_room.is_empty() {
            return Err(crate::runtime::executor_utils::invalid_argument_fields(
                "room_id is required on the first signaling message",
                [("room_id", "must be a non-empty signaling room id")],
            ));
        }
        if bound_peer.is_empty() {
            return Err(crate::runtime::executor_utils::invalid_argument_fields(
                "peer_id is required on the first signaling message",
                [("peer_id", "must be a non-empty signaling peer id")],
            ));
        }
        if bound_tenant.is_empty() {
            return Err(crate::runtime::executor_utils::invalid_argument_fields(
                "tenant_id is required on the first signaling message",
                [("tenant_id", "must be a non-empty signaling tenant id")],
            ));
        }
        let tenant_uuid = parse_uuid("tenant_id", &bound_tenant)?;
        let room_uuid = parse_uuid("room_id", &bound_room)?;
        let peer_uuid = parse_uuid("peer_id", &bound_peer)?;
        let pool = self.require_pool()?;
        self.require_active_peer_membership(pool, tenant_uuid, room_uuid, peer_uuid)
            .await
            .map_err(|err| {
                if err.code() == tonic::Code::FailedPrecondition {
                    peer_not_active_permission_status()
                } else {
                    err
                }
            })?;
        Ok((bound_tenant, bound_room, bound_peer))
    }

    #[cfg(feature = "webrtc")]
    async fn sfu_answer_for_offer(
        &self,
        req: &webrtc_pb::SignalRequest,
        offer_sdp: &str,
    ) -> Option<webrtc_pb::SignalResponse> {
        let bridge = self.sfu_bridge.as_ref()?;
        if req.room_id.trim().is_empty() || req.peer_id.trim().is_empty() {
            tracing::warn!("SFU offer ignored: room_id and peer_id are required");
            return None;
        }
        match bridge
            .accept_offer(&req.room_id, &req.peer_id, offer_sdp)
            .await
        {
            Ok(answer_sdp) => Some(webrtc_pb::SignalResponse {
                payload: Some(webrtc_pb::signal_response::Payload::AnswerSdp(answer_sdp)),
            }),
            Err(err) => {
                tracing::warn!(
                    room_id = %req.room_id,
                    peer_id = %req.peer_id,
                    error = %err,
                    "SFU bridge failed to accept offer; falling back to mesh relay"
                );
                None
            }
        }
    }

    #[cfg(feature = "webrtc")]
    async fn sfu_kick_peer(&self, tenant_id: &str, room_id: &str, peer_id: &str) {
        if let Some(bridge) = self.sfu_bridge.as_ref() {
            if let Err(err) = bridge.kick_peer(tenant_id, room_id, peer_id).await {
                tracing::warn!(tenant_id, room_id, peer_id, error = %err, "SFU bridge peer kick failed");
            }
        }
    }

    #[cfg(feature = "webrtc")]
    async fn sfu_close_room(&self, room_id: &str) {
        if let Some(bridge) = self.sfu_bridge.as_ref() {
            if let Err(err) = bridge.close_room_hook(room_id).await {
                tracing::warn!(room_id, error = %err, "SFU bridge room close hook failed");
            }
        }
    }

    #[cfg(feature = "webrtc")]
    async fn sfu_register_track(&self, room_id: &str, peer_id: &str, track_id: &str, kind: &str) {
        if let Some(bridge) = self.sfu_bridge.as_ref() {
            if let Err(err) = bridge
                .register_published_track(room_id, peer_id, track_id, kind)
                .await
            {
                tracing::warn!(room_id, peer_id, track_id, error = %err, "SFU bridge track register failed");
            }
        }
    }

    #[cfg(feature = "webrtc")]
    async fn sfu_unregister_track(&self, track_id: &str) {
        if let Some(bridge) = self.sfu_bridge.as_ref() {
            if let Err(err) = bridge.unregister_track(track_id).await {
                tracing::warn!(track_id, error = %err, "SFU bridge track unregister failed");
            }
        }
    }

    #[cfg(feature = "webrtc")]
    async fn sfu_join_token(
        &self,
        tenant_id: &str,
        room_id: &str,
        peer_id: &str,
        ttl_seconds: i64,
        now_unix: i64,
    ) -> Result<Option<SfuJoinToken>, Status> {
        let Some(bridge) = self.sfu_bridge.as_ref() else {
            return Ok(None);
        };
        bridge
            .mint_join_token(tenant_id, room_id, peer_id, ttl_seconds, now_unix)
            .await
            .map_err(sfu_backend_unavailable_status)
    }

    #[cfg(feature = "webrtc")]
    fn attach_sfu_join_metadata<T>(
        response: &mut Response<T>,
        token: SfuJoinToken,
    ) -> Result<(), Status> {
        fn value(
            v: &str,
        ) -> Result<tonic::metadata::MetadataValue<tonic::metadata::Ascii>, Status> {
            tonic::metadata::MetadataValue::try_from(v).map_err(|err| {
                webrtc_internal_status(
                    "sfu_join_metadata",
                    format!("SFU metadata value is invalid: {err}"),
                )
            })
        }
        response
            .metadata_mut()
            .insert("x-udb-sfu-url", value(&token.url)?);
        response
            .metadata_mut()
            .insert("x-udb-sfu-join-token", value(&token.token)?);
        response.metadata_mut().insert(
            "x-udb-sfu-expires-at",
            value(&token.expires_at.to_string())?,
        );
        Ok(())
    }

    async fn signal_response_for(
        &self,
        req: &webrtc_pb::SignalRequest,
    ) -> Option<webrtc_pb::SignalResponse> {
        #[cfg(feature = "webrtc")]
        if let Some(webrtc_pb::signal_request::Payload::OfferSdp(sdp)) = req.payload.as_ref()
            && let Some(answer) = self.sfu_answer_for_offer(req, sdp).await
        {
            return Some(answer);
        }
        signal_to_response(req)
    }

    /// Idempotently release a live peer from a room after signaling teardown.
    /// The peer update is guarded by `state = 'CONNECTED'`, so concurrent
    /// inbound/outbound stream endings can both call this while only one releases
    /// the participant slot.
    async fn disconnect_bound_peer(
        &self,
        pool: &PgPool,
        tenant_id: Uuid,
        room_id: Uuid,
        peer_id: Uuid,
        reason: &str,
    ) -> Result<bool, Status> {
        let pm = peer_model();
        let rm = room_model();
        let mut tx = pool.begin().await.map_err(|err| {
            webrtc_internal_status(
                "disconnect_bound_peer",
                format!("disconnect peer transaction failed: {err}"),
            )
        })?;
        let result = sqlx::query(&disconnect_peer_sql(&pm))
            .bind(peer_id)
            .bind(room_id)
            .bind(tenant_id)
            .execute(&mut *tx)
            .await
            .map_err(|err| {
                webrtc_internal_status(
                    "disconnect_bound_peer",
                    format!("disconnect peer failed: {err}"),
                )
            })?;
        let disconnected = result.rows_affected() > 0;
        if disconnected {
            sqlx::query(&decrement_participant_count_sql(&rm))
                .bind(room_id)
                .execute(&mut *tx)
                .await
                .map_err(|err| {
                    webrtc_internal_status(
                        "disconnect_bound_peer",
                        format!("release room participant failed: {err}"),
                    )
                })?;
        }
        tx.commit().await.map_err(|err| {
            webrtc_internal_status(
                "disconnect_bound_peer",
                format!("commit disconnect peer failed: {err}"),
            )
        })?;

        if disconnected {
            let tenant_id = tenant_id.to_string();
            let room_id = room_id.to_string();
            let peer_id = peer_id.to_string();
            emit_payload_event(
                pool,
                self.outbox_relation.as_deref(),
                "udb.webrtc.peer.left.v1",
                &room_id,
                serde_json::json!({
                    "room_id": room_id.clone(),
                    "peer_id": peer_id.clone(),
                    "tenant_id": tenant_id.clone(),
                    "reason": reason,
                }),
                Some(&self.metrics),
            )
            .await;
            #[cfg(feature = "webrtc")]
            self.sfu_kick_peer(&tenant_id, &room_id, &peer_id).await;
        }
        Ok(disconnected)
    }

    /// Refresh the signaling heartbeat while also revalidating that the peer is
    /// still a connected member of an active room. Returns `false` when the pump
    /// should stop relaying frames.
    async fn touch_bound_peer_membership(
        &self,
        pool: &PgPool,
        tenant_id: Uuid,
        room_id: Uuid,
        peer_id: Uuid,
    ) -> Result<bool, Status> {
        let pm = peer_model();
        let rm = room_model();
        let result = sqlx::query(&touch_peer_membership_sql(&pm, &rm))
            .bind(peer_id)
            .bind(room_id)
            .bind(tenant_id)
            .execute(pool)
            .await
            .map_err(|err| {
                webrtc_internal_status(
                    "touch_bound_peer_membership",
                    format!("refresh peer heartbeat failed: {err}"),
                )
            })?;
        Ok(result.rows_affected() > 0)
    }

    async fn reap_stale_peers(
        &self,
        stale_after_secs: i64,
        batch_size: i64,
    ) -> Result<usize, Status> {
        if stale_after_secs <= 0 || batch_size <= 0 {
            return Ok(0);
        }
        let pool = self.require_pool()?;
        let pm = peer_model();
        let rm = room_model();
        let mut tx = pool.begin().await.map_err(|err| {
            webrtc_internal_status(
                "reap_stale_peers",
                format!("stale peer reaper transaction failed: {err}"),
            )
        })?;
        let rows = sqlx::query(&stale_peer_reap_sql(&pm))
            .bind(stale_after_secs as f64)
            .bind(batch_size)
            .fetch_all(&mut *tx)
            .await
            .map_err(|err| {
                webrtc_internal_status(
                    "reap_stale_peers",
                    format!("stale peer reaper failed: {err}"),
                )
            })?;
        for row in &rows {
            let room_id = row
                .try_get::<String, _>("room_id")
                .ok()
                .and_then(|id| Uuid::parse_str(&id).ok());
            if let Some(room_id) = room_id {
                sqlx::query(&decrement_participant_count_sql(&rm))
                    .bind(room_id)
                    .execute(&mut *tx)
                    .await
                    .map_err(|err| {
                        webrtc_internal_status(
                            "reap_stale_peers",
                            format!("release stale peer room participant failed: {err}"),
                        )
                    })?;
            }
        }
        tx.commit().await.map_err(|err| {
            webrtc_internal_status(
                "reap_stale_peers",
                format!("commit stale peer reaper failed: {err}"),
            )
        })?;

        for row in &rows {
            let peer_id = row.try_get::<String, _>("peer_id").unwrap_or_default();
            let room_id = row.try_get::<String, _>("room_id").unwrap_or_default();
            let tenant_id = row.try_get::<String, _>("tenant_id").unwrap_or_default();
            if !room_id.is_empty() {
                emit_payload_event(
                    pool,
                    self.outbox_relation.as_deref(),
                    "udb.webrtc.peer.left.v1",
                    &room_id,
                    serde_json::json!({
                        "room_id": room_id.clone(),
                        "peer_id": peer_id.clone(),
                        "tenant_id": tenant_id.clone(),
                        "reason": "stale_heartbeat",
                    }),
                    Some(&self.metrics),
                )
                .await;
                #[cfg(feature = "webrtc")]
                self.sfu_kick_peer(&tenant_id, &room_id, &peer_id).await;
            }
        }

        Ok(rows.len())
    }
}

impl Default for WebrtcServiceImpl {
    fn default() -> Self {
        Self::new()
    }
}

// ── models ────────────────────────────────────────────────────────────────────

fn room_model() -> NativeModel {
    native_model(
        ROOM_MSG,
        &[
            "room_id",
            "tenant_id",
            "name",
            "state",
            "max_participants",
            "participant_count",
            "config",
            "created_by",
            "deleted_at",
        ],
    )
}

fn peer_model() -> NativeModel {
    native_model(
        PEER_MSG,
        &[
            "peer_id",
            "room_id",
            "tenant_id",
            "display_name",
            "state",
            "metadata",
            "user_agent",
            "joined_at",
            "left_at",
            "deleted_at",
            // Auto-injected audit column (Peer has `audit_fields: true`); used as
            // the signaling last-seen heartbeat by the stale-peer reaper, not a
            // proto field on the Peer message.
            "updated_at",
        ],
    )
}

/// Idempotent CONNECTED→DISCONNECTED transition for a peer whose signaling
/// stream died without a LeaveRoom RPC. The `state = 'CONNECTED'` guard means
/// only the FIRST caller (inbound or outbound stream half) wins, so the
/// participant-count decrement runs exactly once.
fn disconnect_peer_sql(pm: &NativeModel) -> String {
    format!(
        "UPDATE {rel} SET {state} = 'DISCONNECTED', \
           {left_at} = COALESCE({left_at}, CURRENT_TIMESTAMP), \
           {updated_at} = CURRENT_TIMESTAMP \
         WHERE {peer_id} = $1::UUID AND {room_id} = $2::UUID AND {tenant_id} = $3::UUID \
           AND {deleted} IS NULL AND {state} = 'CONNECTED'",
        rel = pm.relation,
        state = pm.q("state"),
        left_at = pm.q("left_at"),
        updated_at = pm.q("updated_at"),
        peer_id = pm.q("peer_id"),
        room_id = pm.q("room_id"),
        tenant_id = pm.q("tenant_id"),
        deleted = pm.q("deleted_at"),
    )
}

/// Release one participant slot on a room (same SQL `leave_room` uses).
fn decrement_participant_count_sql(rm: &NativeModel) -> String {
    format!(
        "UPDATE {room_rel} SET {count} = GREATEST({count} - 1, 0) \
         WHERE {rid} = $1::UUID AND {rdeleted} IS NULL",
        room_rel = rm.relation,
        count = rm.q("participant_count"),
        rid = rm.q("room_id"),
        rdeleted = rm.q("deleted_at"),
    )
}

/// Combined heartbeat + membership re-validation for the signaling pump: bump
/// the peer's last-seen (`updated_at`) ONLY while the peer is still a CONNECTED
/// member of an ACTIVE room. 0 rows ⇒ membership is gone (left/kicked peer or
/// closed room) and the pump must stop relaying.
fn touch_peer_membership_sql(pm: &NativeModel, rm: &NativeModel) -> String {
    format!(
        "UPDATE {prel} p SET {pupdated} = CURRENT_TIMESTAMP \
         FROM {rrel} r \
         WHERE p.{ppid} = $1::UUID AND p.{prid} = $2::UUID AND p.{ptid} = $3::UUID \
           AND p.{pdel} IS NULL AND p.{pstate} = 'CONNECTED' \
           AND r.{rrid} = p.{prid} AND r.{rtid} = p.{ptid} \
           AND r.{rdel} IS NULL AND r.{rstate} = 'ACTIVE'",
        prel = pm.relation,
        rrel = rm.relation,
        pupdated = pm.q("updated_at"),
        ppid = pm.q("peer_id"),
        prid = pm.q("room_id"),
        ptid = pm.q("tenant_id"),
        pdel = pm.q("deleted_at"),
        pstate = pm.q("state"),
        rrid = rm.q("room_id"),
        rtid = rm.q("tenant_id"),
        rdel = rm.q("deleted_at"),
        rstate = rm.q("state"),
    )
}

/// Bounded stale-peer sweep: CONNECTED peers whose last-seen heartbeat
/// (`updated_at`) is older than the cutoff transition to DISCONNECTED. Returns
/// the disconnected (peer, room, tenant) tuples so the caller can release the
/// participant slots and emit events.
fn stale_peer_reap_sql(pm: &NativeModel) -> String {
    format!(
        "WITH doomed AS ( \
            SELECT {peer_id} AS doomed_peer_id FROM {rel} \
            WHERE {state} = 'CONNECTED' AND {deleted} IS NULL \
              AND {updated} < NOW() - make_interval(secs => $1::DOUBLE PRECISION) \
            ORDER BY {updated} \
            LIMIT $2 \
         ) \
         UPDATE {rel} p SET {state} = 'DISCONNECTED', \
           {left_at} = COALESCE({left_at}, CURRENT_TIMESTAMP), \
           {updated} = CURRENT_TIMESTAMP \
         FROM doomed d \
         WHERE p.{peer_id} = d.doomed_peer_id AND p.{state} = 'CONNECTED' \
         RETURNING p.{peer_id}::TEXT AS peer_id, p.{room_id}::TEXT AS room_id, \
                   p.{tenant_id}::TEXT AS tenant_id",
        rel = pm.relation,
        peer_id = pm.q("peer_id"),
        room_id = pm.q("room_id"),
        tenant_id = pm.q("tenant_id"),
        state = pm.q("state"),
        left_at = pm.q("left_at"),
        deleted = pm.q("deleted_at"),
        updated = pm.q("updated_at"),
    )
}

fn track_model() -> NativeModel {
    native_model(
        TRACK_MSG,
        &[
            "track_id",
            "room_id",
            "peer_id",
            "tenant_id",
            "kind",
            "label",
            "state",
            "settings",
            "metadata",
        ],
    )
}

use super::native_helpers::{non_empty_json, parse_uuid};

// ── enum<->db (VARCHAR(20), short token via the proto_enum serializer) ─────────

fn webrtc_validation_fields<I, F, D>(message: impl Into<String>, fields: I) -> Status
where
    I: IntoIterator<Item = (F, D)>,
    F: Into<String>,
    D: Into<String>,
{
    crate::runtime::executor_utils::invalid_argument_fields(message, fields)
}

fn room_state_from_db(value: &str) -> i32 {
    use webrtc_entity_pb::RoomState as S;
    match value {
        "ACTIVE" | "ROOM_STATE_ACTIVE" => S::Active as i32,
        "IDLE" | "ROOM_STATE_IDLE" => S::Idle as i32,
        "CLOSED" | "ROOM_STATE_CLOSED" => S::Closed as i32,
        _ => S::Unspecified as i32,
    }
}

fn room_state_to_db(value: &str, default: &str) -> Result<String, Status> {
    let v = value.trim();
    if v.is_empty() {
        return Ok(default.to_string());
    }
    let short = match v.to_ascii_uppercase().as_str() {
        "ACTIVE" | "ROOM_STATE_ACTIVE" => "ACTIVE",
        "IDLE" | "ROOM_STATE_IDLE" => "IDLE",
        "CLOSED" | "ROOM_STATE_CLOSED" => "CLOSED",
        other => {
            return Err(webrtc_validation_fields(
                format!("unknown room state: {other}"),
                [("state", "must be ACTIVE, IDLE, or CLOSED")],
            ));
        }
    };
    Ok(short.to_string())
}

fn peer_state_from_db(value: &str) -> i32 {
    use webrtc_entity_pb::PeerState as S;
    match value {
        "NEW" | "PEER_STATE_NEW" => S::New as i32,
        "CONNECTING" | "PEER_STATE_CONNECTING" => S::Connecting as i32,
        "CONNECTED" | "PEER_STATE_CONNECTED" => S::Connected as i32,
        "DISCONNECTED" | "PEER_STATE_DISCONNECTED" => S::Disconnected as i32,
        "FAILED" | "PEER_STATE_FAILED" => S::Failed as i32,
        "CLOSED" | "PEER_STATE_CLOSED" => S::Closed as i32,
        _ => S::Unspecified as i32,
    }
}

fn peer_state_to_db(value: &str, default: &str) -> Result<String, Status> {
    let v = value.trim();
    if v.is_empty() {
        return Ok(default.to_string());
    }
    let short = match v.to_ascii_uppercase().as_str() {
        "NEW" | "PEER_STATE_NEW" => "NEW",
        "CONNECTING" | "PEER_STATE_CONNECTING" => "CONNECTING",
        "CONNECTED" | "PEER_STATE_CONNECTED" => "CONNECTED",
        "DISCONNECTED" | "PEER_STATE_DISCONNECTED" => "DISCONNECTED",
        "FAILED" | "PEER_STATE_FAILED" => "FAILED",
        "CLOSED" | "PEER_STATE_CLOSED" => "CLOSED",
        other => {
            return Err(webrtc_validation_fields(
                format!("unknown peer state: {other}"),
                [("state", "must be a known peer state")],
            ));
        }
    };
    Ok(short.to_string())
}

fn track_kind_from_db(value: &str) -> i32 {
    use webrtc_entity_pb::TrackKind as K;
    match value {
        "AUDIO" | "TRACK_KIND_AUDIO" => K::Audio as i32,
        "VIDEO" | "TRACK_KIND_VIDEO" => K::Video as i32,
        "SCREEN" | "TRACK_KIND_SCREEN" => K::Screen as i32,
        "DATA" | "TRACK_KIND_DATA" => K::Data as i32,
        _ => K::Unspecified as i32,
    }
}

fn track_kind_to_db(value: &str, default: &str) -> Result<String, Status> {
    let v = value.trim();
    if v.is_empty() {
        return Ok(default.to_string());
    }
    let short = match v.to_ascii_uppercase().as_str() {
        "AUDIO" | "TRACK_KIND_AUDIO" => "AUDIO",
        "VIDEO" | "TRACK_KIND_VIDEO" => "VIDEO",
        "SCREEN" | "TRACK_KIND_SCREEN" => "SCREEN",
        "DATA" | "TRACK_KIND_DATA" => "DATA",
        other => {
            return Err(webrtc_validation_fields(
                format!("unknown track kind: {other}"),
                [("kind", "must be AUDIO, VIDEO, SCREEN, or DATA")],
            ));
        }
    };
    Ok(short.to_string())
}

fn track_state_from_db(value: &str) -> i32 {
    use webrtc_entity_pb::TrackState as S;
    match value {
        "ACTIVE" | "TRACK_STATE_ACTIVE" => S::Active as i32,
        "MUTED" | "TRACK_STATE_MUTED" => S::Muted as i32,
        "ENDED" | "TRACK_STATE_ENDED" => S::Ended as i32,
        _ => S::Unspecified as i32,
    }
}

// ── projections + row mappers ─────────────────────────────────────────────────

fn room_select_projection(m: &NativeModel) -> String {
    [
        m.text("room_id"),
        m.text("tenant_id"),
        m.select("name"),
        m.text_or_empty("state"),
        m.select("max_participants"),
        m.select("participant_count"),
        m.text_or_empty("config"),
        m.text_or_empty("created_by"),
    ]
    .join(", ")
}

fn room_from_row(row: &sqlx::postgres::PgRow) -> Result<webrtc_entity_pb::Room, Status> {
    let map =
        |e: sqlx::Error| webrtc_internal_status("decode_room", format!("decode room failed: {e}"));
    Ok(webrtc_entity_pb::Room {
        room_id: row.try_get("room_id").map_err(map)?,
        tenant_id: row.try_get("tenant_id").map_err(map)?,
        name: row.try_get("name").map_err(map)?,
        state: room_state_from_db(&row.try_get::<String, _>("state").map_err(map)?),
        max_participants: row.try_get::<i32, _>("max_participants").map_err(map)?,
        participant_count: row.try_get::<i32, _>("participant_count").map_err(map)?,
        config: row.try_get("config").map_err(map)?,
        created_by: row.try_get("created_by").map_err(map)?,
        ..Default::default()
    })
}

fn peer_select_projection(m: &NativeModel) -> String {
    [
        m.text("peer_id"),
        m.text("room_id"),
        m.text("tenant_id"),
        m.text_or_empty("display_name"),
        m.text_or_empty("state"),
        m.text_or_empty("metadata"),
        m.text_or_empty("user_agent"),
    ]
    .join(", ")
}

fn peer_from_row(row: &sqlx::postgres::PgRow) -> Result<webrtc_entity_pb::Peer, Status> {
    let map =
        |e: sqlx::Error| webrtc_internal_status("decode_peer", format!("decode peer failed: {e}"));
    Ok(webrtc_entity_pb::Peer {
        peer_id: row.try_get("peer_id").map_err(map)?,
        room_id: row.try_get("room_id").map_err(map)?,
        tenant_id: row.try_get("tenant_id").map_err(map)?,
        display_name: row.try_get("display_name").map_err(map)?,
        state: peer_state_from_db(&row.try_get::<String, _>("state").map_err(map)?),
        metadata: row.try_get("metadata").map_err(map)?,
        user_agent: row.try_get("user_agent").map_err(map)?,
        ..Default::default()
    })
}

// `track_select_projection` / `track_from_row` (the raw PgRow track mappers)
// were removed when ListTracks/GetTrack moved to the typed read path below; the
// track row→proto mapping now lives in `track_from_json`. Room/peer keep their
// PgRow mappers because list_rooms + join_room still issue raw SQL (aggregate /
// atomic CAS — see P4.D).

// ── typed native-entity READ path (extend_udb.md P4) ──────────────────────────
// Room/peer/track READS (get_room/get_peer/list_peers/list_tracks) compile through
// the IR (`native_entity_read_for_service`) and dispatch to the proto-bound
// backend, instead of bespoke Postgres SELECTs — the same path
// `tenant_service::get_tenant` rides. The two STANDALONE inserts (create_room,
// publish_track) likewise ride the typed WRITE path (`native_entity_write_for_service`,
// JSONB via `LogicalValue::Json` + UUID via String, proven by storage/asset).
// Still raw (P4.D — not IR-expressible): the atomic/arithmetic lifecycle (join CAS,
// close-room txn, stale-peer reap, participant_count math, cross-table gates), the
// blind conditional UPDATEs (update_room/mute/unpublish/leave — the IR has no
// UPDATE-only op, only insert/upsert-on-PK), and `list_rooms`' exact `total_count`
// aggregate (precedent: `ListTenants`).

fn wv_eq(field: &str, value: &str) -> LogicalFilter {
    LogicalFilter::Comparison {
        field: field.to_string(),
        op: ComparisonOp::Eq,
        value: LogicalValue::String(value.to_string()),
    }
}

fn wv_asc(field: &str) -> LogicalSort {
    LogicalSort {
        field: field.to_string(),
        direction: SortDirection::Asc,
        nulls: NullOrder::Default,
    }
}

/// Read a string-ish field from a typed-read JSON row. JSONB columns come back
/// as nested JSON (object/array) and are re-stringified, matching the proto
/// `string` shape — the same coercion `tenant_service::json_string_field` uses.
fn webrtc_json_str(row: &serde_json::Value, key: &str) -> String {
    match row.get(key) {
        Some(serde_json::Value::String(value)) => value.clone(),
        Some(serde_json::Value::Number(value)) => value.to_string(),
        Some(serde_json::Value::Bool(value)) => value.to_string(),
        Some(value @ (serde_json::Value::Object(_) | serde_json::Value::Array(_))) => {
            value.to_string()
        }
        _ => String::new(),
    }
}

fn webrtc_json_i32(row: &serde_json::Value, key: &str) -> i32 {
    row.get(key)
        .and_then(serde_json::Value::as_i64)
        .unwrap_or(0) as i32
}

fn room_projection() -> LogicalProjection {
    LogicalProjection::fields(
        [
            "room_id",
            "tenant_id",
            "name",
            "state",
            "max_participants",
            "participant_count",
            "config",
            "created_by",
        ]
        .into_iter()
        .map(String::from),
    )
}

fn room_read_by_id(room_id: &str, tenant_id: &str) -> LogicalRead {
    LogicalRead {
        message_type: ROOM_MSG.to_string(),
        filter: Some(LogicalFilter::And(vec![
            wv_eq("room_id", room_id),
            wv_eq("tenant_id", tenant_id),
            LogicalFilter::IsNull("deleted_at".to_string()),
        ])),
        projection: Some(room_projection()),
        sort: Vec::new(),
        include: Vec::new(),
        pagination: Some(LogicalPagination::limit(1)),
    }
}

fn room_from_json(row: &serde_json::Value) -> webrtc_entity_pb::Room {
    webrtc_entity_pb::Room {
        room_id: webrtc_json_str(row, "room_id"),
        tenant_id: webrtc_json_str(row, "tenant_id"),
        name: webrtc_json_str(row, "name"),
        state: room_state_from_db(&webrtc_json_str(row, "state")),
        max_participants: webrtc_json_i32(row, "max_participants"),
        participant_count: webrtc_json_i32(row, "participant_count"),
        config: webrtc_json_str(row, "config"),
        created_by: webrtc_json_str(row, "created_by"),
        ..Default::default()
    }
}

fn peer_projection() -> LogicalProjection {
    LogicalProjection::fields(
        [
            "peer_id",
            "room_id",
            "tenant_id",
            "display_name",
            "state",
            "metadata",
            "user_agent",
        ]
        .into_iter()
        .map(String::from),
    )
}

fn peer_read_by_id(peer_id: &str, tenant_id: &str) -> LogicalRead {
    LogicalRead {
        message_type: PEER_MSG.to_string(),
        filter: Some(LogicalFilter::And(vec![
            wv_eq("peer_id", peer_id),
            wv_eq("tenant_id", tenant_id),
            LogicalFilter::IsNull("deleted_at".to_string()),
        ])),
        projection: Some(peer_projection()),
        sort: Vec::new(),
        include: Vec::new(),
        pagination: Some(LogicalPagination::limit(1)),
    }
}

fn peer_list_read(
    tenant_id: &str,
    room_id: &str,
    state_filter: &str,
    offset: u64,
    limit: u32,
) -> LogicalRead {
    let mut clauses = vec![
        wv_eq("tenant_id", tenant_id),
        wv_eq("room_id", room_id),
        LogicalFilter::IsNull("deleted_at".to_string()),
    ];
    if !state_filter.trim().is_empty() {
        clauses.push(wv_eq("state", state_filter));
    }
    LogicalRead {
        message_type: PEER_MSG.to_string(),
        filter: Some(LogicalFilter::And(clauses)),
        projection: Some(peer_projection()),
        sort: vec![wv_asc("display_name")],
        include: Vec::new(),
        pagination: Some(LogicalPagination::page(offset, limit)),
    }
}

fn peer_from_json(row: &serde_json::Value) -> webrtc_entity_pb::Peer {
    webrtc_entity_pb::Peer {
        peer_id: webrtc_json_str(row, "peer_id"),
        room_id: webrtc_json_str(row, "room_id"),
        tenant_id: webrtc_json_str(row, "tenant_id"),
        display_name: webrtc_json_str(row, "display_name"),
        state: peer_state_from_db(&webrtc_json_str(row, "state")),
        metadata: webrtc_json_str(row, "metadata"),
        user_agent: webrtc_json_str(row, "user_agent"),
        ..Default::default()
    }
}

fn track_projection() -> LogicalProjection {
    LogicalProjection::fields(
        [
            "track_id",
            "room_id",
            "peer_id",
            "tenant_id",
            "kind",
            "label",
            "state",
            "settings",
            "metadata",
        ]
        .into_iter()
        .map(String::from),
    )
}

fn track_list_read(
    tenant_id: &str,
    room_id: &str,
    kind_filter: &str,
    peer_filter: &str,
    offset: u64,
    limit: u32,
) -> LogicalRead {
    let mut clauses = vec![wv_eq("tenant_id", tenant_id), wv_eq("room_id", room_id)];
    if !kind_filter.trim().is_empty() {
        clauses.push(wv_eq("kind", kind_filter));
    }
    if !peer_filter.trim().is_empty() {
        clauses.push(wv_eq("peer_id", peer_filter));
    }
    LogicalRead {
        message_type: TRACK_MSG.to_string(),
        filter: Some(LogicalFilter::And(clauses)),
        projection: Some(track_projection()),
        sort: vec![wv_asc("label")],
        include: Vec::new(),
        pagination: Some(LogicalPagination::page(offset, limit)),
    }
}

fn track_from_json(row: &serde_json::Value) -> webrtc_entity_pb::Track {
    webrtc_entity_pb::Track {
        track_id: webrtc_json_str(row, "track_id"),
        room_id: webrtc_json_str(row, "room_id"),
        peer_id: webrtc_json_str(row, "peer_id"),
        tenant_id: webrtc_json_str(row, "tenant_id"),
        kind: track_kind_from_db(&webrtc_json_str(row, "kind")),
        label: webrtc_json_str(row, "label"),
        state: track_state_from_db(&webrtc_json_str(row, "state")),
        settings: webrtc_json_str(row, "settings"),
        metadata: webrtc_json_str(row, "metadata"),
        ..Default::default()
    }
}

// ── typed native-entity WRITE records (extend_udb.md P4) ──────────────────────
// Only the two STANDALONE inserts (create_room, publish_track) ride the typed
// write path — the IR's `LogicalWrite` is insert/upsert-keyed-on-PK, so a blind
// conditional `UPDATE … WHERE` (update_room / mute / unpublish / leave's peer
// close) and the atomic lifecycle (join CAS / close-room txn / reap CTE) are NOT
// expressible without an unwanted INSERT or lost atomicity — those stay raw (P4.D).

/// Empty → SQL NULL (UUID columns reject `''::uuid`), else the id as a `String`
/// the per-backend compiler binds/casts to UUID (proven live by `storage_service`).
fn wv_uuid_or_null(value: &str) -> LogicalValue {
    let value = value.trim();
    if value.is_empty() {
        LogicalValue::Null
    } else {
        LogicalValue::String(value.to_string())
    }
}

/// A JSONB column value: parse the (always-valid, `{}`-defaulted) JSON text into
/// `LogicalValue::Json`, which the compiler binds as jsonb — the idiom
/// `asset_service`/`authz` already use. Invalid JSON fails closed exactly as the
/// raw `$n::JSONB` bind did.
fn wv_json(value: &str) -> Result<LogicalValue, Status> {
    serde_json::from_str::<serde_json::Value>(value)
        .map(LogicalValue::Json)
        .map_err(|err| {
            webrtc_validation_fields(
                format!("webrtc JSON field is invalid: {err}"),
                [("json", "must be valid JSON")],
            )
        })
}

fn room_create_record(
    room_id: &str,
    tenant_id: &str,
    req: &webrtc_pb::CreateRoomRequest,
) -> Result<LogicalRecord, Status> {
    let mut record = LogicalRecord::new();
    record.insert(
        "room_id".to_string(),
        LogicalValue::String(room_id.to_string()),
    );
    record.insert(
        "tenant_id".to_string(),
        LogicalValue::String(tenant_id.to_string()),
    );
    record.insert("name".to_string(), LogicalValue::String(req.name.clone()));
    record.insert(
        "state".to_string(),
        LogicalValue::String("ACTIVE".to_string()),
    );
    record.insert(
        "max_participants".to_string(),
        LogicalValue::Int(req.max_participants as i64),
    );
    record.insert("participant_count".to_string(), LogicalValue::Int(0));
    record.insert("config".to_string(), wv_json(&non_empty_json(&req.config))?);
    record.insert("created_by".to_string(), wv_uuid_or_null(&req.created_by));
    // `audit_info` (JSONB) + `deleted_*` omitted → DB defaults ('{}'::jsonb / NULL).
    Ok(record)
}

#[allow(clippy::too_many_arguments)]
fn track_create_record(
    track_id: &str,
    tenant_id: &str,
    room_id: &str,
    peer_id: &str,
    req: &webrtc_pb::PublishTrackRequest,
    kind: &str,
) -> Result<LogicalRecord, Status> {
    let mut record = LogicalRecord::new();
    record.insert(
        "track_id".to_string(),
        LogicalValue::String(track_id.to_string()),
    );
    record.insert(
        "room_id".to_string(),
        LogicalValue::String(room_id.to_string()),
    );
    record.insert(
        "peer_id".to_string(),
        LogicalValue::String(peer_id.to_string()),
    );
    record.insert(
        "tenant_id".to_string(),
        LogicalValue::String(tenant_id.to_string()),
    );
    record.insert("kind".to_string(), LogicalValue::String(kind.to_string()));
    record.insert("label".to_string(), LogicalValue::String(req.label.clone()));
    record.insert(
        "state".to_string(),
        LogicalValue::String("ACTIVE".to_string()),
    );
    record.insert(
        "settings".to_string(),
        wv_json(&non_empty_json(&req.settings))?,
    );
    record.insert(
        "metadata".to_string(),
        wv_json(&non_empty_json(&req.metadata))?,
    );
    // `audit_info` (JSONB, NOT NULL DEFAULT '{}') omitted → DB default applies.
    Ok(record)
}

// ── RoomService ────────────────────────────────────────────────────────────────

#[tonic::async_trait]
impl RoomService for WebrtcServiceImpl {
    async fn create_room(
        &self,
        request: Request<webrtc_pb::CreateRoomRequest>,
    ) -> Result<Response<webrtc_pb::CreateRoomResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        if req.tenant_id.trim().is_empty() || req.name.trim().is_empty() {
            return Err(crate::runtime::executor_utils::invalid_argument_fields(
                "tenant_id and name are required",
                [("name", "must be a non-empty room name")],
            ));
        }
        // Per-tenant fair admission (held for the whole RPC).
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?.to_string();
        let room_id = Uuid::new_v4().to_string();
        let context = native_service_context(&metadata, &tenant_id, "");
        // Typed native-entity insert (extend_udb.md P4): the row (incl. its JSONB
        // `config` + UUID columns) compiles through the IR and dispatches to the
        // proto-bound backend — no bespoke SQL. `Error` conflict = plain INSERT.
        self.require_runtime()?
            .native_entity_write_for_service(
                "webrtc.room",
                &context,
                ROOM_MSG,
                room_create_record(&room_id, &tenant_id, &req)?,
                ConflictStrategy::Error,
            )
            .await?;
        // The outbox event is emitted post-insert on the pool (the raw path did
        // the same — not in the insert txn, so no atomicity change).
        if let Ok(pool) = self.require_pool() {
            emit_payload_event(
                pool,
                self.outbox_relation.as_deref(),
                "udb.webrtc.room.created.v1",
                &room_id,
                serde_json::json!({
                    "room_id": room_id.clone(),
                    "tenant_id": tenant_id.clone(),
                    "name": req.name.clone(),
                }),
                Some(&self.metrics),
            )
            .await;
        }
        Ok(Response::new(webrtc_pb::CreateRoomResponse {
            room_id,
            message: "room created".to_string(),
            error: None,
        }))
    }

    async fn get_room(
        &self,
        request: Request<webrtc_pb::GetRoomRequest>,
    ) -> Result<Response<webrtc_pb::GetRoomResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit_read(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?.to_string();
        let room_id = parse_uuid("room_id", &req.room_id)?.to_string();
        let context = native_service_context(&metadata, &tenant_id, "");
        let room = self
            .require_runtime()?
            .native_entity_read_for_service(
                "webrtc.room",
                &context,
                room_read_by_id(&room_id, &tenant_id),
            )
            .await?
            .first()
            .map(room_from_json)
            .ok_or_else(|| {
                webrtc_schema_not_found_status(
                    "get_room",
                    "webrtc_room_not_found",
                    "room not found",
                )
            })?;
        Ok(Response::new(webrtc_pb::GetRoomResponse {
            room: Some(room),
            error: None,
        }))
    }

    async fn update_room(
        &self,
        request: Request<webrtc_pb::UpdateRoomRequest>,
    ) -> Result<Response<webrtc_pb::UpdateRoomResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let room_id = parse_uuid("room_id", &req.room_id)?;
        let update_mask =
            update_mask_path_set(req.update_mask.as_ref(), &["name", "state", "config"])?;
        let update_name = update_mask_allows(&update_mask, "name", !req.name.trim().is_empty());
        let update_state = update_mask_allows(&update_mask, "state", !req.state.trim().is_empty());
        let update_config =
            update_mask_allows(&update_mask, "config", !req.config.trim().is_empty());
        let state = room_state_to_db(&req.state, "")?;
        let pool = self.require_pool()?;
        let m = room_model();
        let rel = m.relation.clone();
        let result = sqlx::query(&format!(
            "UPDATE {rel} SET \
               {name} = CASE WHEN $3 THEN $4 ELSE {name} END, \
               {state} = CASE WHEN $5 THEN $6 ELSE {state} END, \
               {config} = CASE WHEN $7 THEN $8::JSONB ELSE {config} END \
             WHERE {room_id} = $1::UUID AND {tenant_id} = $2::UUID AND {deleted} IS NULL",
            name = m.q("name"),
            state = m.q("state"),
            config = m.q("config"),
            room_id = m.q("room_id"),
            tenant_id = m.q("tenant_id"),
            deleted = m.q("deleted_at"),
        ))
        .bind(room_id)
        .bind(tenant_id)
        .bind(update_name)
        .bind(&req.name)
        .bind(update_state)
        .bind(&state)
        .bind(update_config)
        .bind(req.config.trim())
        .execute(pool)
        .await
        .map_err(|err| {
            webrtc_internal_status("update_room", format!("update room failed: {err}"))
        })?;
        if result.rows_affected() == 0 {
            return Err(webrtc_schema_not_found_status(
                "update_room",
                "webrtc_room_not_found",
                "room not found",
            ));
        }
        Ok(Response::new(webrtc_pb::UpdateRoomResponse {
            message: "room updated".to_string(),
            error: None,
        }))
    }

    async fn close_room(
        &self,
        request: Request<webrtc_pb::CloseRoomRequest>,
    ) -> Result<Response<webrtc_pb::CloseRoomResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let room_id = parse_uuid("room_id", &req.room_id)?;
        let pool = self.require_pool()?;
        let m = room_model();
        let pm = peer_model();
        let tm = track_model();
        // Transitional P4 gate: close_room is a multi-entity transactional state
        // transition plus event fan-out. It must stay on a backend that can
        // express the room/peer/track updates atomically.
        let mut tx = pool.begin().await.map_err(|err| {
            webrtc_internal_status(
                "close_room",
                format!("close room transaction failed: {err}"),
            )
        })?;
        let peer_rows = sqlx::query(&format!(
            "SELECT {peer_id}::TEXT AS peer_id FROM {rel} \
             WHERE {room_id} = $1::UUID AND {tenant_id} = $2::UUID \
               AND {deleted} IS NULL AND {state} = 'CONNECTED' \
             ORDER BY {peer_id}",
            rel = pm.relation,
            peer_id = pm.q("peer_id"),
            room_id = pm.q("room_id"),
            tenant_id = pm.q("tenant_id"),
            deleted = pm.q("deleted_at"),
            state = pm.q("state"),
        ))
        .bind(room_id)
        .bind(tenant_id)
        .fetch_all(&mut *tx)
        .await
        .map_err(|err| {
            webrtc_internal_status("close_room", format!("list room peers failed: {err}"))
        })?;
        let track_rows = sqlx::query(&format!(
            "SELECT {track_id}::TEXT AS track_id FROM {rel} \
             WHERE {room_id} = $1::UUID AND {tenant_id} = $2::UUID AND {state} <> 'ENDED' \
             ORDER BY {track_id}",
            rel = tm.relation,
            track_id = tm.q("track_id"),
            room_id = tm.q("room_id"),
            tenant_id = tm.q("tenant_id"),
            state = tm.q("state"),
        ))
        .bind(room_id)
        .bind(tenant_id)
        .fetch_all(&mut *tx)
        .await
        .map_err(|err| {
            webrtc_internal_status("close_room", format!("list room tracks failed: {err}"))
        })?;
        let result = sqlx::query(&format!(
            "UPDATE {rel} SET {state} = 'CLOSED', {count} = 0 \
             WHERE {room_id} = $1::UUID AND {tenant_id} = $2::UUID AND {deleted} IS NULL",
            rel = m.relation,
            state = m.q("state"),
            count = m.q("participant_count"),
            room_id = m.q("room_id"),
            tenant_id = m.q("tenant_id"),
            deleted = m.q("deleted_at"),
        ))
        .bind(room_id)
        .bind(tenant_id)
        .execute(&mut *tx)
        .await
        .map_err(|err| webrtc_internal_status("close_room", format!("close room failed: {err}")))?;
        if result.rows_affected() == 0 {
            return Err(webrtc_schema_not_found_status(
                "close_room",
                "webrtc_room_not_found",
                "room not found",
            ));
        }
        sqlx::query(&format!(
            "UPDATE {rel} SET {state} = 'CLOSED', {left_at} = COALESCE({left_at}, CURRENT_TIMESTAMP), \
               {deleted} = COALESCE({deleted}, CURRENT_TIMESTAMP) \
             WHERE {room_id} = $1::UUID AND {tenant_id} = $2::UUID \
               AND {deleted} IS NULL AND {state} <> 'CLOSED'",
            rel = pm.relation,
            state = pm.q("state"),
            left_at = pm.q("left_at"),
            deleted = pm.q("deleted_at"),
            room_id = pm.q("room_id"),
            tenant_id = pm.q("tenant_id"),
        ))
        .bind(room_id)
        .bind(tenant_id)
        .execute(&mut *tx)
        .await
        .map_err(|err| {
            webrtc_internal_status("close_room", format!("close room peers failed: {err}"))
        })?;
        sqlx::query(&format!(
            "UPDATE {rel} SET {state} = 'ENDED' \
             WHERE {room_id} = $1::UUID AND {tenant_id} = $2::UUID AND {state} <> 'ENDED'",
            rel = tm.relation,
            state = tm.q("state"),
            room_id = tm.q("room_id"),
            tenant_id = tm.q("tenant_id"),
        ))
        .bind(room_id)
        .bind(tenant_id)
        .execute(&mut *tx)
        .await
        .map_err(|err| {
            webrtc_internal_status("close_room", format!("end room tracks failed: {err}"))
        })?;
        tx.commit().await.map_err(|err| {
            webrtc_internal_status("close_room", format!("commit close room failed: {err}"))
        })?;
        let peer_ids = peer_rows
            .iter()
            .filter_map(|row| row.try_get::<String, _>("peer_id").ok())
            .collect::<Vec<_>>();
        let track_ids = track_rows
            .iter()
            .filter_map(|row| row.try_get::<String, _>("track_id").ok())
            .collect::<Vec<_>>();
        let tx = self.signaling.channel(&req.room_id).await;
        for peer_id in &peer_ids {
            tx.send_signal(webrtc_pb::SignalResponse {
                payload: Some(webrtc_pb::signal_response::Payload::PeerLeft(
                    peer_id.clone(),
                )),
            });
            emit_payload_event(
                pool,
                self.outbox_relation.as_deref(),
                "udb.webrtc.peer.left.v1",
                &req.room_id,
                serde_json::json!({
                    "room_id": req.room_id.clone(),
                    "peer_id": peer_id,
                    "tenant_id": req.tenant_id.clone(),
                    "reason": "room_closed",
                }),
                Some(&self.metrics),
            )
            .await;
        }
        for track_id in &track_ids {
            emit_payload_event(
                pool,
                self.outbox_relation.as_deref(),
                "udb.webrtc.track.ended.v1",
                &req.room_id,
                serde_json::json!({
                    "room_id": req.room_id.clone(),
                    "track_id": track_id,
                    "tenant_id": req.tenant_id.clone(),
                    "reason": "room_closed",
                }),
                Some(&self.metrics),
            )
            .await;
        }
        #[cfg(feature = "webrtc")]
        self.sfu_close_room(&req.room_id).await;
        emit_payload_event(
            pool,
            self.outbox_relation.as_deref(),
            "udb.webrtc.room.closed.v1",
            &req.room_id,
            serde_json::json!({
                "room_id": req.room_id.clone(),
                "tenant_id": req.tenant_id.clone(),
                "closed_peer_count": peer_ids.len(),
                "ended_track_count": track_ids.len(),
            }),
            Some(&self.metrics),
        )
        .await;
        drop(tx);
        self.signaling.close(&req.room_id).await;
        Ok(Response::new(webrtc_pb::CloseRoomResponse {
            message: "room closed".to_string(),
            error: None,
        }))
    }

    async fn list_rooms(
        &self,
        request: Request<webrtc_pb::ListRoomsRequest>,
    ) -> Result<Response<webrtc_pb::ListRoomsResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit_read(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let pool = self.require_pool()?;
        let m = room_model();
        let rel = m.relation.clone();
        let projection = room_select_projection(&m);
        let state_filter = room_state_to_db(&req.state, "")?;
        let page_window = native_offset_page_window(req.page, req.page_size, &req.page_token, 50);
        let where_clause = format!(
            "WHERE {tenant_id} = $1::UUID AND {deleted} IS NULL AND ($2 = '' OR {state} = $2)",
            tenant_id = m.q("tenant_id"),
            deleted = m.q("deleted_at"),
            state = m.q("state"),
        );
        // P4.D: kept raw. ListRooms returns an exact `total_count`; the typed
        // `LogicalRead` helper has no aggregate-count surface yet, so keep the
        // SQL count+page (precedent: `tenant_service::list_tenants`).
        let total: i64 = sqlx::query_scalar(&format!("SELECT COUNT(*) FROM {rel} {where_clause}"))
            .bind(tenant_id)
            .bind(&state_filter)
            .fetch_one(pool)
            .await
            .map_err(|err| {
                webrtc_internal_status("list_rooms", format!("count rooms failed: {err}"))
            })?;
        let rows = sqlx::query(&format!(
            "SELECT {projection} FROM {rel} {where_clause} ORDER BY {name} LIMIT $3 OFFSET $4",
            name = m.q("name"),
        ))
        .bind(tenant_id)
        .bind(&state_filter)
        .bind(page_window.limit_i64())
        .bind(page_window.offset_i64())
        .fetch_all(pool)
        .await
        .map_err(|err| webrtc_internal_status("list_rooms", format!("list rooms failed: {err}")))?;
        let mut rooms = Vec::with_capacity(rows.len());
        for row in &rows {
            rooms.push(room_from_row(row)?);
        }
        Ok(Response::new(webrtc_pb::ListRoomsResponse {
            rooms,
            total_count: total as i32,
            error: None,
            next_page_token: native_next_page_token_for_total(
                page_window.offset,
                page_window.limit,
                total,
            ),
        }))
    }

    // ── Recording / egress (master-plan 5.5) ─────────────────────────────────
    // Proto-first, fail-closed handlers. Gated ONCE at construction on
    // `UDB_WEBRTC_EGRESS_ENABLED` (default unset = disabled). Disabled →
    // FAILED_PRECONDITION/EGRESS_NOT_ENABLED; enabled-but-no-backend →
    // FAILED_PRECONDITION/EGRESS_BACKEND_UNAVAILABLE (mounted-but-degraded
    // honesty). NEVER UNIMPLEMENTED — the capability-lie / retry-storm guard.
    // The code after each gate is the real wiring point and activates the moment
    // a backend makes the `*_egress_backend` seam return Ok.

    async fn start_room_composite(
        &self,
        request: Request<webrtc_pb::StartRoomCompositeRequest>,
    ) -> Result<Response<webrtc_pb::StartRoomCompositeResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        // Validate the room reference; tenant comes only from the validated input.
        let _room_id = parse_uuid("room_id", &req.room_id)?;
        // Fail closed unless the operator has enabled egress.
        self.require_egress_enabled("start_room_composite")?;
        // egress_id is SERVER-derived and bound to the verified tenant so it can
        // never reference another tenant's session (cross-tenant leak guard).
        let egress_id = new_egress_id(&req.tenant_id);
        let pool = self.require_pool()?;
        match self
            .start_egress_backend(
                &egress_id,
                &req.room_id,
                None,
                "start_room_composite",
                "room_composite",
            )
            .await
        {
            Ok(status) => {
                self.emit_egress_started(
                    pool,
                    &egress_id,
                    &req.tenant_id,
                    &req.room_id,
                    None,
                    "room_composite",
                )
                .await;
                Ok(Response::new(webrtc_pb::StartRoomCompositeResponse {
                    egress_id,
                    status,
                    message: "egress starting".to_string(),
                    error: None,
                }))
            }
            Err(status) => {
                self.emit_egress_failed(
                    pool,
                    &egress_id,
                    &req.tenant_id,
                    &req.room_id,
                    &status.message().to_string(),
                )
                .await;
                Err(status)
            }
        }
    }

    async fn start_track_egress(
        &self,
        request: Request<webrtc_pb::StartTrackEgressRequest>,
    ) -> Result<Response<webrtc_pb::StartTrackEgressResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        let _room_id = parse_uuid("room_id", &req.room_id)?;
        let _track_id = parse_uuid("track_id", &req.track_id)?;
        self.require_egress_enabled("start_track_egress")?;
        let egress_id = new_egress_id(&req.tenant_id);
        let pool = self.require_pool()?;
        match self
            .start_egress_backend(
                &egress_id,
                &req.room_id,
                Some(req.track_id.as_str()),
                "start_track_egress",
                "track",
            )
            .await
        {
            Ok(status) => {
                self.emit_egress_started(
                    pool,
                    &egress_id,
                    &req.tenant_id,
                    &req.room_id,
                    Some(req.track_id.as_str()),
                    "track",
                )
                .await;
                Ok(Response::new(webrtc_pb::StartTrackEgressResponse {
                    egress_id,
                    status,
                    message: "egress starting".to_string(),
                    error: None,
                }))
            }
            Err(status) => {
                self.emit_egress_failed(
                    pool,
                    &egress_id,
                    &req.tenant_id,
                    &req.room_id,
                    &status.message().to_string(),
                )
                .await;
                Err(status)
            }
        }
    }

    async fn stop_egress(
        &self,
        request: Request<webrtc_pb::StopEgressRequest>,
    ) -> Result<Response<webrtc_pb::StopEgressResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        // Cross-tenant guard: the egress_id MUST be one minted for this tenant
        // (see `new_egress_id`). Reject an id scoped to a different tenant before
        // touching any backend — a tenant can never stop another's egress.
        if !egress_id_is_tenant_scoped(&req.egress_id, &req.tenant_id) {
            return Err(egress_tenant_scope_mismatch_status());
        }
        self.require_egress_enabled("stop_egress")?;
        let pool = self.require_pool()?;
        match self
            .stop_egress_backend(&req.egress_id, "stop_egress")
            .await
        {
            Ok(status) => {
                self.emit_egress_stopped(pool, &req.egress_id, &req.tenant_id, "")
                    .await;
                Ok(Response::new(webrtc_pb::StopEgressResponse {
                    egress_id: req.egress_id,
                    status,
                    message: "egress stopped".to_string(),
                    error: None,
                }))
            }
            Err(status) => {
                self.emit_egress_failed(
                    pool,
                    &req.egress_id,
                    &req.tenant_id,
                    "",
                    &status.message().to_string(),
                )
                .await;
                Err(status)
            }
        }
    }

    async fn list_egress(
        &self,
        request: Request<webrtc_pb::ListEgressRequest>,
    ) -> Result<Response<webrtc_pb::ListEgressResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit_read(&req.tenant_id).await?;
        // Fail closed both when egress is disabled (EGRESS_NOT_ENABLED) and when
        // it is enabled but no backend is wired (EGRESS_BACKEND_UNAVAILABLE) —
        // a read must not silently advertise an empty list as "no jobs" while
        // the subsystem is degraded.
        self.require_egress_enabled("list_egress")?;
        let page_window = native_offset_page_window(1, req.page_size, &req.page_token, 50);
        let egresses = self
            .list_egress_backend(&req.tenant_id, &req.room_id, "list_egress")
            .await?;
        let total_count = egresses.len() as i32;
        let egresses = egresses
            .into_iter()
            .skip(page_window.offset)
            .take(page_window.limit)
            .collect::<Vec<_>>();
        Ok(Response::new(webrtc_pb::ListEgressResponse {
            next_page_token: native_next_page_token_for_total(
                page_window.offset,
                page_window.limit,
                total_count as i64,
            ),
            egresses,
            total_count,
            error: None,
        }))
    }
}

// ── PeerService ──────────────────────────────────────────────────────────────

#[tonic::async_trait]
impl PeerService for WebrtcServiceImpl {
    async fn join_room(
        &self,
        request: Request<webrtc_pb::JoinRoomRequest>,
    ) -> Result<Response<webrtc_pb::JoinRoomResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        // Per-tenant fair admission (held for the whole RPC) — one tenant's join
        // flood can't starve the shared control plane.
        let _admit = self.admit(&req.tenant_id).await?;
        let (peer, existing_peers, _peer_id) = self
            .join_room_inner(
                &req.tenant_id,
                &req.room_id,
                &req.display_name,
                &req.metadata,
                &req.user_agent,
            )
            .await?;

        Ok(Response::new(webrtc_pb::JoinRoomResponse {
            peer: Some(peer),
            existing_peers,
            error: None,
        }))
    }

    async fn join_session(
        &self,
        request: Request<webrtc_pb::JoinSessionRequest>,
    ) -> Result<Response<webrtc_pb::JoinSessionResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        // Per-tenant fair admission (held for the whole RPC), same as join_room.
        let _admit = self.admit(&req.tenant_id).await?;
        // Run the SAME atomic join (capacity-CAS + insert + reload + signal +
        // event) as join_room. Over-full/closed/foreign rooms reject here with
        // FailedPrecondition + ROOM_FULL.
        let (peer, existing_peers, peer_id) = self
            .join_room_inner(
                &req.tenant_id,
                &req.room_id,
                &req.display_name,
                &req.metadata,
                &req.user_agent,
            )
            .await?;

        // Mint TURN credentials for the just-inserted peer. The peer was inserted
        // by the join above, so no separate active-membership re-check is needed;
        // use the SAME fail-closed secret check as issue_credentials.
        let ttl = if req.ttl_seconds > 0 {
            (req.ttl_seconds as i64).min(self.turn.default_ttl)
        } else {
            self.turn.default_ttl
        };
        let now = chrono::Utc::now().timestamp();
        let expiry = now + ttl;
        // username = "<expiry>:<principal>" per the coturn long-term-secret REST
        // scheme; bind the principal to the verified tenant/room/peer tuple and
        // the only action this credential authorizes: TURN media relay.
        let principal = format!(
            "{}:{}:{}:{}",
            req.tenant_id.trim(),
            req.room_id.trim(),
            peer_id.trim(),
            TURN_RELAY_ACTION
        );
        // Fail closed when no TURN secret is configured rather than minting
        // against a well-known dev secret.
        let secret = self
            .turn
            .secret
            .as_deref()
            .ok_or_else(|| turn_secret_not_configured_status("join_session"))?;
        let (username, credential) =
            crate::runtime::security::turn_rest_credential(secret, &principal, expiry);
        let ice = webrtc_pb::IceServer {
            urls: self.turn.urls.clone(),
            username,
            credential,
        };

        #[cfg(feature = "webrtc")]
        let sfu_join = self
            .sfu_join_token(&req.tenant_id, &req.room_id, peer_id.trim(), ttl, now)
            .await?;

        let response = Response::new(webrtc_pb::JoinSessionResponse {
            peer: Some(peer),
            existing_peers,
            ice_servers: vec![ice],
            expires_at: Some(prost_types::Timestamp {
                seconds: expiry,
                nanos: 0,
            }),
            error: None,
        });
        #[cfg(feature = "webrtc")]
        {
            let mut response = response;
            if let Some(token) = sfu_join {
                Self::attach_sfu_join_metadata(&mut response, token)?;
            }
            Ok(response)
        }
        #[cfg(not(feature = "webrtc"))]
        {
            Ok(response)
        }
    }

    async fn leave_room(
        &self,
        request: Request<webrtc_pb::LeaveRoomRequest>,
    ) -> Result<Response<webrtc_pb::LeaveRoomResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let room_id = parse_uuid("room_id", &req.room_id)?;
        let peer_id = parse_uuid("peer_id", &req.peer_id)?;
        let pool = self.require_pool()?;
        let m = peer_model();
        let rel = m.relation.clone();
        // Transitional P4 gate: leave_room couples a guarded peer transition with
        // a room counter decrement and signaling teardown; keep the durable part
        // on the current transactional Postgres path.
        let result = sqlx::query(&format!(
            "UPDATE {rel} SET {state} = 'CLOSED', {left_at} = CURRENT_TIMESTAMP, \
               {deleted} = CURRENT_TIMESTAMP \
             WHERE {peer_id} = $1::UUID AND {room_id} = $2::UUID AND {tenant_id} = $3::UUID \
               AND {deleted} IS NULL",
            state = m.q("state"),
            left_at = m.q("left_at"),
            deleted = m.q("deleted_at"),
            peer_id = m.q("peer_id"),
            room_id = m.q("room_id"),
            tenant_id = m.q("tenant_id"),
        ))
        .bind(peer_id)
        .bind(room_id)
        .bind(tenant_id)
        .execute(pool)
        .await
        .map_err(|err| webrtc_internal_status("leave_room", format!("leave room failed: {err}")))?;
        if result.rows_affected() == 0 {
            return Ok(Response::new(webrtc_pb::LeaveRoomResponse {
                success: false,
                error: None,
            }));
        }
        let rm = room_model();
        let _ = sqlx::query(&format!(
            "UPDATE {room_rel} SET {count} = GREATEST({count} - 1, 0) \
             WHERE {rid} = $1::UUID AND {rdeleted} IS NULL",
            room_rel = rm.relation,
            count = rm.q("participant_count"),
            rid = rm.q("room_id"),
            rdeleted = rm.q("deleted_at"),
        ))
        .bind(room_id)
        .execute(pool)
        .await;

        emit_payload_event(
            pool,
            self.outbox_relation.as_deref(),
            "udb.webrtc.peer.left.v1",
            &req.room_id,
            serde_json::json!({
                "room_id": req.room_id.clone(),
                "peer_id": req.peer_id.clone(),
                "tenant_id": req.tenant_id.clone(),
            }),
            Some(&self.metrics),
        )
        .await;

        let tx = self.signaling.channel(&req.room_id).await;
        tx.send_signal(webrtc_pb::SignalResponse {
            payload: Some(webrtc_pb::signal_response::Payload::PeerLeft(
                req.peer_id.clone(),
            )),
        });
        drop(tx);
        // Targeted teardown: end ONLY the leaving peer's subscriber stream.
        // The room's signaling hub stays up for every other connected peer —
        // whole-hub teardown belongs to `close_room` alone.
        self.signaling.close_peer(&req.room_id, &req.peer_id).await;
        self.signaling.prune_if_idle(&req.room_id).await;
        #[cfg(feature = "webrtc")]
        self.sfu_kick_peer(
            &tenant_id.to_string(),
            &room_id.to_string(),
            &peer_id.to_string(),
        )
        .await;

        Ok(Response::new(webrtc_pb::LeaveRoomResponse {
            success: true,
            error: None,
        }))
    }

    async fn get_peer(
        &self,
        request: Request<webrtc_pb::GetPeerRequest>,
    ) -> Result<Response<webrtc_pb::GetPeerResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit_read(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?.to_string();
        let peer_id = parse_uuid("peer_id", &req.peer_id)?.to_string();
        let context = native_service_context(&metadata, &tenant_id, "");
        let peer = self
            .require_runtime()?
            .native_entity_read_for_service(
                "webrtc.room",
                &context,
                peer_read_by_id(&peer_id, &tenant_id),
            )
            .await?
            .first()
            .map(peer_from_json)
            .ok_or_else(|| {
                webrtc_schema_not_found_status(
                    "get_peer",
                    "webrtc_peer_not_found",
                    "peer not found",
                )
            })?;
        Ok(Response::new(webrtc_pb::GetPeerResponse {
            peer: Some(peer),
            error: None,
        }))
    }

    async fn list_peers(
        &self,
        request: Request<webrtc_pb::ListPeersRequest>,
    ) -> Result<Response<webrtc_pb::ListPeersResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit_read(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?.to_string();
        let room_id = parse_uuid("room_id", &req.room_id)?.to_string();
        // Validated/normalized into the short DB token before it reaches the IR
        // filter ("" = no state filter), preserving the raw path's semantics.
        let state_filter = peer_state_to_db(&req.state, "")?;
        let context = native_service_context(&metadata, &tenant_id, "");
        let page_window = native_offset_page_window(1, req.page_size, &req.page_token, 50);
        let peers = self
            .require_runtime()?
            .native_entity_read_for_service(
                "webrtc.room",
                &context,
                peer_list_read(
                    &tenant_id,
                    &room_id,
                    &state_filter,
                    page_window.offset as u64,
                    page_window.limit as u32,
                ),
            )
            .await?
            .iter()
            .map(peer_from_json)
            .collect::<Vec<_>>();
        Ok(Response::new(webrtc_pb::ListPeersResponse {
            next_page_token: native_next_page_token(
                page_window.offset,
                page_window.limit,
                peers.len(),
            ),
            peers,
            error: None,
        }))
    }
}

// ── TrackService ─────────────────────────────────────────────────────────────

#[tonic::async_trait]
impl TrackService for WebrtcServiceImpl {
    async fn publish_track(
        &self,
        request: Request<webrtc_pb::PublishTrackRequest>,
    ) -> Result<Response<webrtc_pb::PublishTrackResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        // Per-tenant fair admission (held for the whole RPC).
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let room_id = parse_uuid("room_id", &req.room_id)?;
        let peer_id = parse_uuid("peer_id", &req.peer_id)?;
        let pool = self.require_pool()?;
        self.require_active_peer_membership(pool, tenant_id, room_id, peer_id)
            .await?;
        let track_id = Uuid::new_v4().to_string();
        let kind = track_kind_to_db(&req.kind, "VIDEO")?;
        let tenant_str = tenant_id.to_string();
        let context = native_service_context(&metadata, &tenant_str, "");
        // Typed native-entity insert (extend_udb.md P4): the track row incl. its
        // JSONB settings/metadata + UUID columns compiles through the IR and
        // dispatches to the proto-bound backend. The peer-membership gate above
        // stays raw (cross-table JOIN, not IR-expressible — P4.D).
        self.require_runtime()?
            .native_entity_write_for_service(
                "webrtc.room",
                &context,
                TRACK_MSG,
                track_create_record(
                    &track_id,
                    &tenant_str,
                    &room_id.to_string(),
                    &peer_id.to_string(),
                    &req,
                    &kind,
                )?,
                ConflictStrategy::Error,
            )
            .await?;

        emit_payload_event(
            pool,
            self.outbox_relation.as_deref(),
            "udb.webrtc.track.published.v1",
            &req.room_id,
            serde_json::json!({
                "room_id": req.room_id.clone(),
                "peer_id": req.peer_id.clone(),
                "track_id": track_id.clone(),
                "kind": kind.clone(),
            }),
            Some(&self.metrics),
        )
        .await;

        let tx = self.signaling.channel(&req.room_id).await;
        tx.send_signal(webrtc_pb::SignalResponse {
            payload: Some(webrtc_pb::signal_response::Payload::TrackPublished(
                track_id.clone(),
            )),
        });
        #[cfg(feature = "webrtc")]
        self.sfu_register_track(&req.room_id, &req.peer_id, &track_id, &kind)
            .await;

        Ok(Response::new(webrtc_pb::PublishTrackResponse {
            track_id,
            message: "track published".to_string(),
            error: None,
        }))
    }

    async fn unpublish_track(
        &self,
        request: Request<webrtc_pb::UnpublishTrackRequest>,
    ) -> Result<Response<webrtc_pb::UnpublishTrackResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let track_id = parse_uuid("track_id", &req.track_id)?;
        let pool = self.require_pool()?;
        let m = track_model();
        let rel = m.relation.clone();
        let result = sqlx::query(&format!(
            "UPDATE {rel} SET {state} = 'ENDED' \
             WHERE {track_id} = $1::UUID AND {tenant_id} = $2::UUID",
            state = m.q("state"),
            track_id = m.q("track_id"),
            tenant_id = m.q("tenant_id"),
        ))
        .bind(track_id)
        .bind(tenant_id)
        .execute(pool)
        .await
        .map_err(|err| {
            webrtc_internal_status("unpublish_track", format!("unpublish track failed: {err}"))
        })?;
        #[cfg(feature = "webrtc")]
        if result.rows_affected() > 0 {
            self.sfu_unregister_track(&req.track_id).await;
        }
        Ok(Response::new(webrtc_pb::UnpublishTrackResponse {
            success: result.rows_affected() > 0,
            error: None,
        }))
    }

    async fn mute_track(
        &self,
        request: Request<webrtc_pb::MuteTrackRequest>,
    ) -> Result<Response<webrtc_pb::MuteTrackResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let track_id = parse_uuid("track_id", &req.track_id)?;
        let pool = self.require_pool()?;
        let m = track_model();
        let rel = m.relation.clone();
        let new_state = if req.muted { "MUTED" } else { "ACTIVE" };
        let result = sqlx::query(&format!(
            "UPDATE {rel} SET {state} = $3 \
             WHERE {track_id} = $1::UUID AND {tenant_id} = $2::UUID AND {state} <> 'ENDED'",
            state = m.q("state"),
            track_id = m.q("track_id"),
            tenant_id = m.q("tenant_id"),
        ))
        .bind(track_id)
        .bind(tenant_id)
        .bind(new_state)
        .execute(pool)
        .await
        .map_err(|err| webrtc_internal_status("mute_track", format!("mute track failed: {err}")))?;
        if result.rows_affected() == 0 {
            return Err(webrtc_schema_not_found_status(
                "mute_track",
                "webrtc_track_not_found_or_ended",
                "track not found or ended",
            ));
        }
        Ok(Response::new(webrtc_pb::MuteTrackResponse {
            message: format!("track {new_state}"),
            error: None,
        }))
    }

    async fn list_tracks(
        &self,
        request: Request<webrtc_pb::ListTracksRequest>,
    ) -> Result<Response<webrtc_pb::ListTracksResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit_read(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?.to_string();
        let room_id = parse_uuid("room_id", &req.room_id)?.to_string();
        // Normalize the optional kind filter to its short DB token ("" = any);
        // the optional peer filter stays the raw id (empty = any), matching the
        // raw path which only applied it when non-empty.
        let kind_filter = track_kind_to_db(&req.kind, "")?;
        let peer_filter = req.peer_id.trim().to_string();
        let context = native_service_context(&metadata, &tenant_id, "");
        let page_window = native_offset_page_window(1, req.page_size, &req.page_token, 50);
        let tracks = self
            .require_runtime()?
            .native_entity_read_for_service(
                "webrtc.room",
                &context,
                track_list_read(
                    &tenant_id,
                    &room_id,
                    &kind_filter,
                    &peer_filter,
                    page_window.offset as u64,
                    page_window.limit as u32,
                ),
            )
            .await?
            .iter()
            .map(track_from_json)
            .collect::<Vec<_>>();
        Ok(Response::new(webrtc_pb::ListTracksResponse {
            next_page_token: native_next_page_token(
                page_window.offset,
                page_window.limit,
                tracks.len(),
            ),
            tracks,
            error: None,
        }))
    }
}

// ── TurnService ──────────────────────────────────────────────────────────────

#[tonic::async_trait]
impl TurnService for WebrtcServiceImpl {
    async fn issue_credentials(
        &self,
        request: Request<webrtc_pb::IssueCredentialsRequest>,
    ) -> Result<Response<webrtc_pb::IssueCredentialsResponse>, Status> {
        let metadata = request.metadata().clone();
        let req = request.into_inner();
        validate_request_tenant(&metadata, &req.tenant_id)?;
        let _admit = self.admit(&req.tenant_id).await?;
        let tenant_id = parse_uuid("tenant_id", &req.tenant_id)?;
        let room_id = parse_uuid("room_id", &req.room_id)?;
        let peer_id = parse_uuid("peer_id", &req.peer_id)?;
        let pool = self.require_pool()?;
        self.require_active_peer_membership(pool, tenant_id, room_id, peer_id)
            .await?;
        let ttl = if req.ttl_seconds > 0 {
            (req.ttl_seconds as i64).min(self.turn.default_ttl)
        } else {
            self.turn.default_ttl
        };
        let now = chrono::Utc::now().timestamp();
        let expiry = now + ttl;
        // username = "<expiry>:<principal>" per the coturn long-term-secret REST
        // scheme; bind the principal to the verified tenant/room/peer tuple and
        // the only action this credential authorizes: TURN media relay.
        let principal = format!(
            "{}:{}:{}:{}",
            req.tenant_id.trim(),
            req.room_id.trim(),
            req.peer_id.trim(),
            TURN_RELAY_ACTION
        );
        // Fail closed when no TURN secret is configured (production with no
        // UDB_TURN_SECRET) rather than minting against a well-known dev secret.
        let secret = self
            .turn
            .secret
            .as_deref()
            .ok_or_else(|| turn_secret_not_configured_status("issue_credentials"))?;
        let (username, credential) =
            crate::runtime::security::turn_rest_credential(secret, &principal, expiry);
        let ice = webrtc_pb::IceServer {
            urls: self.turn.urls.clone(),
            username: username.clone(),
            credential: credential.clone(),
        };
        #[cfg(feature = "webrtc")]
        let sfu_join = self
            .sfu_join_token(&req.tenant_id, &req.room_id, &req.peer_id, ttl, now)
            .await?;

        let response = Response::new(webrtc_pb::IssueCredentialsResponse {
            ice_servers: vec![ice],
            username,
            credential,
            ttl_seconds: ttl as i32,
            expires_at: Some(prost_types::Timestamp {
                seconds: expiry,
                nanos: 0,
            }),
            allowed_action: TURN_RELAY_ACTION.to_string(),
            error: None,
        });
        #[cfg(feature = "webrtc")]
        {
            let mut response = response;
            if let Some(token) = sfu_join {
                Self::attach_sfu_join_metadata(&mut response, token)?;
            }
            Ok(response)
        }
        #[cfg(not(feature = "webrtc"))]
        {
            Ok(response)
        }
    }
}

// ── SignalingService (bidi relay) ──────────────────────────────────────────────

/// Map an inbound signaling request to the response broadcast to the room. A
/// `ping` is answered with `pong`; SDP/ICE payloads are forwarded verbatim so
/// every peer in the room sees them (mesh signaling — clients address the
/// relevant peer at the application layer).
fn signal_to_response(req: &webrtc_pb::SignalRequest) -> Option<webrtc_pb::SignalResponse> {
    use webrtc_pb::signal_request::Payload as In;
    use webrtc_pb::signal_response::Payload as Out;
    let out = match req.payload.as_ref()? {
        In::OfferSdp(s) => Out::OfferSdp(s.clone()),
        In::AnswerSdp(s) => Out::AnswerSdp(s.clone()),
        In::IceCandidate(s) => Out::IceCandidate(s.clone()),
        In::Ping(_) => Out::Pong(true),
    };
    Some(webrtc_pb::SignalResponse { payload: Some(out) })
}

fn empty_signaling_stream_status() -> Status {
    webrtc_validation_fields(
        "empty signaling stream",
        [("stream", "must include an initial signaling message")],
    )
}

/// What the outbound pump does with one received hub frame.
enum FrameDisposition {
    /// Relay the signaling payload to this subscriber.
    Yield(webrtc_pb::SignalResponse),
    /// Frame addressed to a different peer — keep this subscriber's stream alive.
    Skip,
    /// End THIS subscriber's stream.
    End,
}

/// Outbound-pump frame filter: [`HubFrame::PeerClosed`] ends ONLY the stream
/// of the peer it addresses (`leave_room` targeted teardown), while
/// [`HubFrame::RoomClosed`] ends every subscriber (`close_room` whole-hub
/// teardown). `bound_peer` is the peer_id this subscriber stream is bound to.
fn dispose_hub_frame(frame: HubFrame, bound_peer: &str) -> FrameDisposition {
    match frame {
        HubFrame::Signal(resp) => FrameDisposition::Yield(resp),
        HubFrame::PeerClosed(peer_id) if peer_id == bound_peer => FrameDisposition::End,
        HubFrame::PeerClosed(_) => FrameDisposition::Skip,
        HubFrame::RoomClosed => FrameDisposition::End,
    }
}

#[tonic::async_trait]
impl SignalingService for WebrtcServiceImpl {
    type SignalStream =
        Pin<Box<dyn Stream<Item = Result<webrtc_pb::SignalResponse, Status>> + Send>>;

    async fn signal(
        &self,
        request: Request<tonic::Streaming<webrtc_pb::SignalRequest>>,
    ) -> Result<Response<Self::SignalStream>, Status> {
        let metadata = request.metadata().clone();
        let mut inbound = request.into_inner();
        // The first message establishes which room this peer is signaling in.
        let first = inbound
            .message()
            .await
            .map_err(|e| webrtc_internal_status("signal", format!("signaling stream error: {e}")))?
            .ok_or_else(empty_signaling_stream_status)?;
        validate_request_tenant(&metadata, &first.tenant_id)?;
        // Per-tenant fair admission at stream ENTRY only: bound the RATE of new
        // signaling connections a single tenant can open so one tenant can't
        // exhaust the shared signaling/control plane. The permit is dropped
        // immediately below (released before the long-lived relay starts) — it is
        // NOT held across the stream, so it cannot deadlock the Signal RPC.
        let _admit = self.admit(&first.tenant_id).await?;
        // Admission: the connection binds to (tenant_id, room_id, peer_id) from
        // the first frame; the peer must be connected in an active room.
        let (bound_tenant, bound_room, bound_peer) =
            self.require_active_signal_membership(&first).await?;
        let bound_tenant_uuid = parse_uuid("tenant_id", &bound_tenant)?;
        let bound_room_uuid = parse_uuid("room_id", &bound_room)?;
        let bound_peer_uuid = parse_uuid("peer_id", &bound_peer)?;
        let pool = self.require_pool()?.clone();
        if !self
            .touch_bound_peer_membership(&pool, bound_tenant_uuid, bound_room_uuid, bound_peer_uuid)
            .await?
        {
            return Err(peer_not_active_permission_status());
        }
        drop(_admit); // release the entry permit; do NOT hold it across the stream
        let tx = self.signaling.channel(&bound_room).await;
        let rx = tx.subscribe();

        // Publish the first message, then pump the rest of the inbound stream —
        // ignoring frames that try to switch the bound (room_id, peer_id) tuple.
        if let Some(resp) = self.signal_response_for(&first).await {
            tx.send_signal(resp);
        }
        let tx_in = tx.clone();
        let svc = self.clone();
        let hub_in = self.signaling.clone();
        let cleanup_room_in = bound_room.clone();
        let pool_in = pool.clone();
        let bound_tenant_in = bound_tenant.clone();
        let bound_room_in = bound_room.clone();
        let bound_peer_in = bound_peer.clone();
        tokio::spawn(async move {
            let mut frames_since_check = 0_u64;
            let mut heartbeat =
                tokio::time::interval(Duration::from_secs(SIGNAL_MEMBERSHIP_RECHECK_SECS));
            loop {
                if tx_in.is_closed() {
                    break;
                }
                tokio::select! {
                    maybe_msg = inbound.message() => {
                        let msg = match maybe_msg {
                            Ok(Some(msg)) => msg,
                            Ok(None) => break,
                            Err(err) => {
                                tracing::warn!(
                                    error = %err,
                                    room_id = %bound_room_in,
                                    peer_id = %bound_peer_in,
                                    "signaling inbound stream failed"
                                );
                                break;
                            }
                        };
                        let same_room =
                            msg.room_id.trim().is_empty()
                                || msg.room_id.trim() == bound_room_in.as_str();
                        let same_peer =
                            msg.peer_id.trim().is_empty()
                                || msg.peer_id.trim() == bound_peer_in.as_str();
                        let same_tenant = msg.tenant_id.trim().is_empty()
                            || msg.tenant_id.trim() == bound_tenant_in.as_str();
                        if !same_room || !same_peer || !same_tenant {
                            continue; // reject frames that rebind to another room/peer
                        }
                        frames_since_check = frames_since_check.saturating_add(1);
                        if frames_since_check >= SIGNAL_MEMBERSHIP_RECHECK_FRAMES {
                            match svc
                                .touch_bound_peer_membership(
                                    &pool_in,
                                    bound_tenant_uuid,
                                    bound_room_uuid,
                                    bound_peer_uuid,
                                )
                                .await
                            {
                                Ok(true) => {
                                    frames_since_check = 0;
                                }
                                Ok(false) => break,
                                Err(err) => {
                                    tracing::warn!(
                                        error = %err,
                                        room_id = %bound_room_in,
                                        peer_id = %bound_peer_in,
                                        "signaling membership revalidation failed"
                                    );
                                    break;
                                }
                            }
                        }
                        if let Some(resp) = svc.signal_response_for(&msg).await {
                            tx_in.send_signal(resp);
                        }
                    }
                    _ = heartbeat.tick() => {
                        match svc
                            .touch_bound_peer_membership(
                                &pool_in,
                                bound_tenant_uuid,
                                bound_room_uuid,
                                bound_peer_uuid,
                            )
                            .await
                        {
                            Ok(true) => {
                                frames_since_check = 0;
                            }
                            Ok(false) => break,
                            Err(err) => {
                                tracing::warn!(
                                    error = %err,
                                    room_id = %bound_room_in,
                                    peer_id = %bound_peer_in,
                                    "signaling heartbeat failed"
                                );
                                break;
                            }
                        }
                    }
                }
            }
            match svc
                .disconnect_bound_peer(
                    &pool_in,
                    bound_tenant_uuid,
                    bound_room_uuid,
                    bound_peer_uuid,
                    "signal_stream_ended",
                )
                .await
            {
                Ok(true) => tx_in.send_signal(webrtc_pb::SignalResponse {
                    payload: Some(webrtc_pb::signal_response::Payload::PeerLeft(
                        bound_peer_in.clone(),
                    )),
                }),
                Ok(false) => {}
                Err(err) => tracing::warn!(
                    error = %err,
                    room_id = %bound_room_in,
                    peer_id = %bound_peer_in,
                    "signaling inbound cleanup failed"
                ),
            }
            drop(tx_in);
            hub_in.prune_if_idle(&cleanup_room_in).await;
        });

        let hub_out = self.signaling.clone();
        let cleanup_room_out = bound_room.clone();
        let svc_out = self.clone();
        let pool_out = pool.clone();
        let bound_peer_out = bound_peer.clone();
        let out = async_stream::try_stream! {
            let mut rx = rx;
            loop {
                match rx.recv().await {
                    Ok(frame) => match dispose_hub_frame(frame, &bound_peer_out) {
                        FrameDisposition::Yield(resp) => yield resp,
                        FrameDisposition::Skip => continue,
                        FrameDisposition::End => break,
                    },
                    Err(broadcast::error::RecvError::Lagged(_)) => continue,
                    Err(broadcast::error::RecvError::Closed) => break,
                }
            }
            match svc_out
                .disconnect_bound_peer(
                    &pool_out,
                    bound_tenant_uuid,
                    bound_room_uuid,
                    bound_peer_uuid,
                    "signal_stream_outbound_ended",
                )
                .await
            {
                Ok(true) => tx.send_signal(webrtc_pb::SignalResponse {
                    payload: Some(webrtc_pb::signal_response::Payload::PeerLeft(
                        bound_peer_out,
                    )),
                }),
                Ok(false) => {}
                Err(err) => tracing::warn!(
                    error = %err,
                    room_id = %cleanup_room_out,
                    peer_id = %bound_peer_out,
                    "signaling outbound cleanup failed"
                ),
            }
            drop(rx);
            drop(tx);
            hub_out.prune_if_idle(&cleanup_room_out).await;
        };
        Ok(Response::new(Box::pin(out)))
    }
}

#[cfg(test)]
mod tenant_scope_tests {
    use super::*;
    use crate::proto::{ErrorDetail, ErrorKind};
    use crate::runtime::executor_utils::ERROR_DETAIL_METADATA_KEY;
    use prost::Message as _;
    use tonic::metadata::MetadataValue;

    fn decode_detail(status: &Status) -> ErrorDetail {
        let raw = status
            .metadata()
            .get_bin(ERROR_DETAIL_METADATA_KEY)
            .expect("typed detail trailer is present");
        crate::runtime::executor_utils::decode_error_detail_from_raw(&raw)
    }

    fn assert_validation_field(status: &Status, field: &str, description: &str) {
        assert_eq!(status.code(), tonic::Code::InvalidArgument);
        let detail = decode_detail(status);
        assert_eq!(detail.kind, ErrorKind::Validation as i32);
        assert_eq!(detail.field_violations.len(), 1);
        assert_eq!(detail.field_violations[0].field, field);
        assert_eq!(detail.field_violations[0].description, description);
    }

    fn assert_schema_not_found_detail(
        status: &Status,
        operation: &str,
        schema_code: &str,
        message: &str,
    ) {
        assert_eq!(status.code(), tonic::Code::NotFound);
        assert_eq!(status.message(), message);
        let detail = decode_detail(status);
        assert_eq!(detail.kind, ErrorKind::Schema as i32);
        assert_eq!(detail.backend, "webrtc");
        assert_eq!(detail.operation, operation);
        assert_eq!(detail.capability_required, schema_code);
        assert!(!detail.retryable);
        assert_eq!(detail.retry_after_ms, 0);
    }

    fn assert_internal_detail(status: &Status, operation: &str, message: &str) {
        assert_eq!(status.code(), tonic::Code::Internal);
        assert_eq!(status.message(), message);
        let detail = decode_detail(status);
        assert_eq!(detail.kind, ErrorKind::Internal as i32);
        assert_eq!(detail.backend, "webrtc");
        assert_eq!(detail.operation, operation);
        assert!(!detail.retryable);
        assert_eq!(detail.retry_after_ms, 0);
        assert!(detail.field_violations.is_empty());
    }

    #[test]
    fn webrtc_internal_status_carries_typed_detail() {
        assert_internal_detail(
            &webrtc_internal_status("join_room", "claim room slot failed: unavailable"),
            "join_room",
            "claim room slot failed: unavailable",
        );
    }

    #[cfg(feature = "webrtc")]
    #[derive(Default)]
    struct RecordingSfuBridge {
        offers: Mutex<Vec<(String, String, String)>>,
    }

    #[cfg(feature = "webrtc")]
    #[async_trait::async_trait]
    impl SfuBridge for RecordingSfuBridge {
        async fn accept_offer(
            &self,
            room_id: &str,
            peer_id: &str,
            offer_sdp: &str,
        ) -> Result<String, String> {
            self.offers.lock().await.push((
                room_id.to_string(),
                peer_id.to_string(),
                offer_sdp.to_string(),
            ));
            Ok("sfu-answer".to_string())
        }

        async fn register_published_track(
            &self,
            _room_id: &str,
            _peer_id: &str,
            _track_id: &str,
            _kind: &str,
        ) -> Result<(), String> {
            Ok(())
        }

        async fn unregister_track(&self, _track_id: &str) -> Result<(), String> {
            Ok(())
        }

        async fn kick_peer(
            &self,
            _tenant_id: &str,
            _room_id: &str,
            _peer_id: &str,
        ) -> Result<(), String> {
            Ok(())
        }

        async fn close_room_hook(&self, _room_id: &str) -> Result<(), String> {
            Ok(())
        }

        async fn mint_join_token(
            &self,
            _tenant_id: &str,
            _room_id: &str,
            _peer_id: &str,
            _ttl_seconds: i64,
            _now_unix: i64,
        ) -> Result<Option<SfuJoinToken>, String> {
            Ok(None)
        }
    }

    #[cfg(feature = "webrtc")]
    #[tokio::test]
    async fn signal_offer_uses_injected_sfu_bridge() {
        let bridge = Arc::new(RecordingSfuBridge::default());
        let bridge_for_service: Arc<dyn SfuBridge> = bridge.clone();
        let svc = WebrtcServiceImpl::new().with_sfu_bridge(Some(bridge_for_service));
        let response = svc
            .signal_response_for(&webrtc_pb::SignalRequest {
                room_id: "room-a".to_string(),
                peer_id: "peer-a".to_string(),
                payload: Some(webrtc_pb::signal_request::Payload::OfferSdp(
                    "client-offer".to_string(),
                )),
                ..Default::default()
            })
            .await
            .expect("injected SFU bridge should return an answer");

        assert_eq!(
            response.payload,
            Some(webrtc_pb::signal_response::Payload::AnswerSdp(
                "sfu-answer".to_string()
            ))
        );
        assert_eq!(
            bridge.offers.lock().await.as_slice(),
            [(
                "room-a".to_string(),
                "peer-a".to_string(),
                "client-offer".to_string()
            )]
        );
    }

    #[cfg(feature = "webrtc")]
    #[test]
    fn sfu_join_metadata_uses_public_header_contract() {
        let mut response = Response::new(());
        WebrtcServiceImpl::attach_sfu_join_metadata(
            &mut response,
            SfuJoinToken {
                url: "wss://livekit.example".to_string(),
                token: "header.payload.signature".to_string(),
                expires_at: 1_700_000_060,
            },
        )
        .expect("valid SFU metadata");

        assert_eq!(
            response
                .metadata()
                .get("x-udb-sfu-url")
                .and_then(|value| value.to_str().ok()),
            Some("wss://livekit.example")
        );
        assert_eq!(
            response
                .metadata()
                .get("x-udb-sfu-join-token")
                .and_then(|value| value.to_str().ok()),
            Some("header.payload.signature")
        );
        assert_eq!(
            response
                .metadata()
                .get("x-udb-sfu-expires-at")
                .and_then(|value| value.to_str().ok()),
            Some("1700000060")
        );
    }

    #[test]
    fn webrtc_not_found_statuses_carry_schema_detail() {
        for (operation, schema_code, message) in [
            ("get_room", "webrtc_room_not_found", "room not found"),
            ("update_room", "webrtc_room_not_found", "room not found"),
            ("close_room", "webrtc_room_not_found", "room not found"),
            ("get_peer", "webrtc_peer_not_found", "peer not found"),
            (
                "mute_track",
                "webrtc_track_not_found_or_ended",
                "track not found or ended",
            ),
        ] {
            assert_schema_not_found_detail(
                &webrtc_schema_not_found_status(operation, schema_code, message),
                operation,
                schema_code,
                message,
            );
        }
    }

    #[tokio::test]
    async fn signaling_hub_close_broadcasts_terminal_frame_and_removes_room() {
        let hub = SignalingHub::default();
        let channel = hub.channel("room-a").await;
        let mut rx = channel.subscribe();

        hub.close("room-a").await;

        assert!(channel.is_closed());
        match tokio::time::timeout(Duration::from_millis(100), rx.recv())
            .await
            .expect("terminal frame should arrive")
            .expect("broadcast should be open long enough for terminal frame")
        {
            HubFrame::RoomClosed => {}
            HubFrame::Signal(_) | HubFrame::PeerClosed(_) => {
                panic!("expected room-closed terminal frame")
            }
        }
        assert!(!hub.rooms.lock().await.contains_key("room-a"));

        let replacement = hub.channel("room-a").await;
        assert!(!replacement.is_closed());
    }

    /// FIX-43: `leave_room` performs a TARGETED teardown via
    /// `SignalingHub::close_peer` — only the leaving peer's subscriber stream
    /// ends; the other peers' streams keep relaying and the room hub survives.
    #[tokio::test]
    async fn leave_room_targeted_close_ends_only_the_leaving_peers_stream() {
        let hub = SignalingHub::default();
        let channel = hub.channel("room-a").await;
        let mut rx_leaver = channel.subscribe();
        let mut rx_stayer = channel.subscribe();

        // The hub-side call leave_room makes for the leaving peer only.
        hub.close_peer("room-a", "peer-leaving").await;

        // Both subscribers receive the broadcast frame, but the outbound-pump
        // filter ends ONLY the addressed peer's stream…
        let frame_leaver = tokio::time::timeout(Duration::from_millis(100), rx_leaver.recv())
            .await
            .expect("targeted frame should arrive")
            .expect("broadcast should stay open");
        assert!(matches!(
            dispose_hub_frame(frame_leaver, "peer-leaving"),
            FrameDisposition::End
        ));
        // …while the staying peer skips it and keeps its stream alive.
        let frame_stayer = tokio::time::timeout(Duration::from_millis(100), rx_stayer.recv())
            .await
            .expect("targeted frame should arrive")
            .expect("broadcast should stay open");
        assert!(matches!(
            dispose_hub_frame(frame_stayer, "peer-staying"),
            FrameDisposition::Skip
        ));

        // The room hub is NOT torn down: later signals still reach the
        // remaining peer (whole-hub teardown stays with close_room).
        assert!(!channel.is_closed());
        assert!(hub.rooms.lock().await.contains_key("room-a"));
        channel.send_signal(webrtc_pb::SignalResponse {
            payload: Some(webrtc_pb::signal_response::Payload::Pong(true)),
        });
        let frame_next = tokio::time::timeout(Duration::from_millis(100), rx_stayer.recv())
            .await
            .expect("relayed signal should arrive")
            .expect("broadcast should stay open");
        assert!(matches!(
            dispose_hub_frame(frame_next, "peer-staying"),
            FrameDisposition::Yield(_)
        ));

        // RoomClosed (close_room) still ends EVERY subscriber's stream.
        hub.close("room-a").await;
        let frame_closed = tokio::time::timeout(Duration::from_millis(100), rx_stayer.recv())
            .await
            .expect("terminal frame should arrive")
            .expect("broadcast should stay open");
        assert!(matches!(
            dispose_hub_frame(frame_closed, "peer-staying"),
            FrameDisposition::End
        ));
    }

    /// Shared live fixture for WebRTC fault oracles: same DSN env chain as
    /// `live_tests::support::live_pg_dsn`, idempotent proto-generated DDL
    /// (`CREATE … IF NOT EXISTS`), and the served WebRTC service bound to the
    /// same Postgres runtime.
    async fn live_postgres_webrtc_fixture() -> (sqlx::PgPool, WebrtcServiceImpl) {
        let dsn = std::env::var("UDB_LIVE_NATIVE_PG_DSN")
            .or_else(|_| std::env::var("UDB_LIVE_AUTH_PG_DSN"))
            .or_else(|_| std::env::var("UDB_INTEGRATION_PG_DSN"))
            .unwrap_or_else(|_| "postgres://udb:udb@127.0.0.1:55432/udb".to_string());
        let pool = sqlx::postgres::PgPoolOptions::new()
            .max_connections(4)
            .acquire_timeout(Duration::from_secs(10))
            .connect(&dsn)
            .await
            .unwrap_or_else(|err| panic!("connect live webrtc postgres at {dsn}: {err}"));
        for stmt in crate::runtime::native_catalog::native_service_catalog_ddl() {
            sqlx::raw_sql(&stmt)
                .execute(&pool)
                .await
                .unwrap_or_else(|err| panic!("native service DDL failed: {err}\nSQL:\n{stmt}"));
        }
        let mut config = crate::runtime::config::UdbConfig::from_env();
        config.primary.direct_dsn = dsn;
        let svc = DataBrokerService::with_runtime(
            crate::runtime::native_catalog::native_manifest().clone(),
            DataBrokerRuntime::from_config(config).await,
        )
        .build_webrtc_service();
        (pool, svc)
    }

    /// FIX-42: stream-death cleanup — when a signaling pump ends without a
    /// LeaveRoom RPC, `disconnect_bound_peer` (the REAL cleanup function both
    /// pump halves call) must mark the peer DISCONNECTED, release exactly one
    /// participant slot, and stay idempotent when the racing inbound/outbound
    /// halves both invoke it.
    #[tokio::test]
    #[ignore = "requires live Postgres; run with cargo test --lib live_postgres_webrtc_stream_death_cleanup_disconnects_peer -- --ignored --nocapture"]
    async fn live_postgres_webrtc_stream_death_cleanup_disconnects_peer() {
        let (pool, svc) = live_postgres_webrtc_fixture().await;
        let tenant_id = Uuid::new_v4().to_string();

        let room = svc
            .create_room(Request::new(webrtc_pb::CreateRoomRequest {
                tenant_id: tenant_id.clone(),
                name: "stream-death-cleanup".to_string(),
                max_participants: 10,
                ..Default::default()
            }))
            .await
            .expect("create_room")
            .into_inner();
        let join = svc
            .join_room(Request::new(webrtc_pb::JoinRoomRequest {
                tenant_id: tenant_id.clone(),
                room_id: room.room_id.clone(),
                display_name: "Ada".to_string(),
                ..Default::default()
            }))
            .await
            .expect("join_room")
            .into_inner();
        let peer = join.peer.expect("peer");

        let tenant_uuid = Uuid::parse_str(&tenant_id).expect("tenant uuid");
        let room_uuid = Uuid::parse_str(&room.room_id).expect("room uuid");
        let peer_uuid = Uuid::parse_str(&peer.peer_id).expect("peer uuid");

        // The pump-end cleanup path (one stream half wins)…
        let first = svc
            .disconnect_bound_peer(
                &pool,
                tenant_uuid,
                room_uuid,
                peer_uuid,
                "signal_stream_ended",
            )
            .await
            .expect("disconnect bound peer");
        assert!(first, "first cleanup must transition the peer and win");
        // …and the racing other half is an idempotent no-op.
        let second = svc
            .disconnect_bound_peer(
                &pool,
                tenant_uuid,
                room_uuid,
                peer_uuid,
                "signal_stream_outbound_ended",
            )
            .await
            .expect("idempotent disconnect");
        assert!(!second, "second cleanup must not double-release the slot");

        let got_peer = svc
            .get_peer(Request::new(webrtc_pb::GetPeerRequest {
                tenant_id: tenant_id.clone(),
                peer_id: peer.peer_id.clone(),
                ..Default::default()
            }))
            .await
            .expect("get_peer")
            .into_inner()
            .peer
            .expect("peer");
        assert_eq!(
            got_peer.state,
            webrtc_entity_pb::PeerState::Disconnected as i32,
            "stream-death cleanup must mark the peer DISCONNECTED"
        );
        let got_room = svc
            .get_room(Request::new(webrtc_pb::GetRoomRequest {
                tenant_id: tenant_id.clone(),
                room_id: room.room_id.clone(),
            }))
            .await
            .expect("get_room")
            .into_inner();
        assert_eq!(
            got_room.room.expect("room").participant_count,
            0,
            "stream-death cleanup must release exactly one participant slot"
        );
    }

    /// Fault-injection oracle for the leader-owned stale-peer worker: expire more
    /// CONNECTED peers than one batch can sweep, then call the real reaper body
    /// until convergence. The served join/get paths prove room counters and peer
    /// state converge without double-releasing fresh or already-reaped peers.
    #[tokio::test]
    #[ignore = "requires live Postgres; run with cargo test --lib live_postgres_webrtc_stale_peer_reaper_converges -- --ignored --nocapture"]
    async fn live_postgres_webrtc_stale_peer_reaper_converges() {
        let (pool, svc) = live_postgres_webrtc_fixture().await;
        let tenant_id = Uuid::new_v4().to_string();
        let room = svc
            .create_room(Request::new(webrtc_pb::CreateRoomRequest {
                tenant_id: tenant_id.clone(),
                name: "stale-peer-reaper".to_string(),
                max_participants: 10,
                ..Default::default()
            }))
            .await
            .expect("create_room")
            .into_inner();

        let mut peers = Vec::new();
        for display_name in [
            "stale-a", "stale-b", "stale-c", "stale-d", "stale-e", "fresh-f",
        ] {
            let join = svc
                .join_room(Request::new(webrtc_pb::JoinRoomRequest {
                    tenant_id: tenant_id.clone(),
                    room_id: room.room_id.clone(),
                    display_name: display_name.to_string(),
                    ..Default::default()
                }))
                .await
                .expect("join_room")
                .into_inner();
            peers.push(join.peer.expect("peer"));
        }

        let pm = peer_model();
        let age_peer_sql = format!(
            "UPDATE {rel} SET {updated} = NOW() - make_interval(secs => $1::DOUBLE PRECISION) \
             WHERE {peer_id} = $2::UUID",
            rel = pm.relation,
            updated = pm.q("updated_at"),
            peer_id = pm.q("peer_id"),
        );
        for peer in peers.iter().take(5) {
            sqlx::query(&age_peer_sql)
                .bind(60.0)
                .bind(&peer.peer_id)
                .execute(&pool)
                .await
                .unwrap_or_else(|err| panic!("age stale peer {}: {err}", peer.peer_id));
        }

        assert_eq!(svc.reap_stale_peers(10, 2).await.expect("first reap"), 2);
        assert_eq!(svc.reap_stale_peers(10, 2).await.expect("second reap"), 2);
        assert_eq!(svc.reap_stale_peers(10, 2).await.expect("third reap"), 1);
        assert_eq!(
            svc.reap_stale_peers(10, 2).await.expect("idempotent reap"),
            0
        );

        for peer in peers.iter().take(5) {
            let got_peer = svc
                .get_peer(Request::new(webrtc_pb::GetPeerRequest {
                    tenant_id: tenant_id.clone(),
                    peer_id: peer.peer_id.clone(),
                    ..Default::default()
                }))
                .await
                .expect("get stale peer")
                .into_inner()
                .peer
                .expect("stale peer");
            assert_eq!(
                got_peer.state,
                webrtc_entity_pb::PeerState::Disconnected as i32,
                "stale peer {} must be disconnected",
                peer.peer_id
            );
        }

        let fresh_peer = peers.last().expect("fresh peer");
        let got_fresh = svc
            .get_peer(Request::new(webrtc_pb::GetPeerRequest {
                tenant_id: tenant_id.clone(),
                peer_id: fresh_peer.peer_id.clone(),
                ..Default::default()
            }))
            .await
            .expect("get fresh peer")
            .into_inner()
            .peer
            .expect("fresh peer");
        assert_eq!(
            got_fresh.state,
            webrtc_entity_pb::PeerState::Connected as i32,
            "fresh peer must not be reaped"
        );

        let got_room = svc
            .get_room(Request::new(webrtc_pb::GetRoomRequest {
                tenant_id,
                room_id: room.room_id,
            }))
            .await
            .expect("get_room")
            .into_inner()
            .room
            .expect("room");
        assert_eq!(
            got_room.participant_count, 1,
            "batched stale reaper must release exactly the five expired slots"
        );
    }

    #[test]
    fn disconnect_sql_is_connected_only_and_updates_last_seen_fields() {
        let pm = peer_model();
        let rm = room_model();
        let disconnect = disconnect_peer_sql(&pm);
        assert!(disconnect.contains("DISCONNECTED"));
        assert!(disconnect.contains("COALESCE"));
        assert!(disconnect.contains("CURRENT_TIMESTAMP"));
        assert!(disconnect.contains("AND \"state\" = 'CONNECTED'"));

        let decrement = decrement_participant_count_sql(&rm);
        assert!(decrement.contains("GREATEST"));
        assert!(decrement.contains("- 1"));
    }

    #[test]
    fn heartbeat_and_stale_reaper_sql_are_bounded_and_membership_gated() {
        let pm = peer_model();
        let rm = room_model();
        let touch = touch_peer_membership_sql(&pm, &rm);
        assert!(touch.contains("CURRENT_TIMESTAMP"));
        assert!(touch.contains("p.\"state\" = 'CONNECTED'"));
        assert!(touch.contains("r.\"state\" = 'ACTIVE'"));

        let reap = stale_peer_reap_sql(&pm);
        assert!(reap.contains("make_interval"));
        assert!(reap.contains("LIMIT $2"));
        assert!(reap.contains("RETURNING"));
        assert!(reap.contains("DISCONNECTED"));
    }

    /// A caller scoped to tenant-a must not read another tenant's room by putting a
    /// foreign tenant_id in the request BODY; the scope guard rejects this before
    /// any pool/DB access (no Postgres needed).
    #[tokio::test]
    async fn get_room_rejects_cross_tenant_body() {
        let svc = WebrtcServiceImpl::new(); // no pool, no channels (admit no-op)
        let mut request = Request::new(webrtc_pb::GetRoomRequest {
            tenant_id: "tenant-b".to_string(),
            room_id: "00000000-0000-0000-0000-000000000001".to_string(),
            ..Default::default()
        });
        request
            .metadata_mut()
            .insert("x-tenant-id", MetadataValue::from_static("tenant-a"));
        let err = svc
            .get_room(request)
            .await
            .expect_err("cross-tenant body must be rejected");
        assert_eq!(err.code(), tonic::Code::PermissionDenied);
    }

    #[tokio::test]
    async fn create_room_missing_name_carries_field_violation() {
        let svc = WebrtcServiceImpl::new(); // no pool, no channels (admit no-op)
        let err = svc
            .create_room(Request::new(webrtc_pb::CreateRoomRequest {
                tenant_id: "tenant-a".to_string(),
                name: "  ".to_string(),
                ..Default::default()
            }))
            .await
            .expect_err("missing room name must fail before runtime access");

        assert_eq!(err.code(), tonic::Code::InvalidArgument);
        assert_eq!(err.message(), "tenant_id and name are required");
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Validation as i32);
        assert_eq!(detail.field_violations.len(), 1);
        assert_eq!(detail.field_violations[0].field, "name");
        assert_eq!(
            detail.field_violations[0].description,
            "must be a non-empty room name"
        );
    }

    #[tokio::test]
    async fn signaling_first_message_missing_room_id_carries_field_violation() {
        let svc = WebrtcServiceImpl::new();
        let err = svc
            .require_active_signal_membership(&webrtc_pb::SignalRequest {
                tenant_id: "tenant-a".to_string(),
                peer_id: "00000000-0000-0000-0000-000000000002".to_string(),
                ..Default::default()
            })
            .await
            .expect_err("missing first-frame room id must fail before pool access");

        assert_eq!(err.code(), tonic::Code::InvalidArgument);
        assert_eq!(
            err.message(),
            "room_id is required on the first signaling message"
        );
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Validation as i32);
        assert_eq!(detail.field_violations.len(), 1);
        assert_eq!(detail.field_violations[0].field, "room_id");
        assert_eq!(
            detail.field_violations[0].description,
            "must be a non-empty signaling room id"
        );
    }

    #[tokio::test]
    async fn signaling_first_message_missing_peer_id_carries_field_violation() {
        let svc = WebrtcServiceImpl::new();
        let err = svc
            .require_active_signal_membership(&webrtc_pb::SignalRequest {
                tenant_id: "tenant-a".to_string(),
                room_id: "00000000-0000-0000-0000-000000000001".to_string(),
                ..Default::default()
            })
            .await
            .expect_err("missing first-frame peer id must fail before pool access");

        assert_eq!(err.code(), tonic::Code::InvalidArgument);
        assert_eq!(
            err.message(),
            "peer_id is required on the first signaling message"
        );
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Validation as i32);
        assert_eq!(detail.field_violations.len(), 1);
        assert_eq!(detail.field_violations[0].field, "peer_id");
        assert_eq!(
            detail.field_violations[0].description,
            "must be a non-empty signaling peer id"
        );
    }

    #[tokio::test]
    async fn signaling_first_message_missing_tenant_id_carries_field_violation() {
        let svc = WebrtcServiceImpl::new();
        let err = svc
            .require_active_signal_membership(&webrtc_pb::SignalRequest {
                room_id: "00000000-0000-0000-0000-000000000001".to_string(),
                peer_id: "00000000-0000-0000-0000-000000000002".to_string(),
                ..Default::default()
            })
            .await
            .expect_err("missing first-frame tenant id must fail before pool access");

        assert_eq!(err.code(), tonic::Code::InvalidArgument);
        assert_eq!(
            err.message(),
            "tenant_id is required on the first signaling message"
        );
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Validation as i32);
        assert_eq!(detail.field_violations.len(), 1);
        assert_eq!(detail.field_violations[0].field, "tenant_id");
        assert_eq!(
            detail.field_violations[0].description,
            "must be a non-empty signaling tenant id"
        );
    }

    #[test]
    fn webrtc_enum_helpers_carry_field_violations() {
        let room = room_state_to_db("BROKEN", "").expect_err("unknown room state must be rejected");
        assert_eq!(room.message(), "unknown room state: BROKEN");
        assert_validation_field(&room, "state", "must be ACTIVE, IDLE, or CLOSED");

        let peer = peer_state_to_db("BROKEN", "").expect_err("unknown peer state must be rejected");
        assert_eq!(peer.message(), "unknown peer state: BROKEN");
        assert_validation_field(&peer, "state", "must be a known peer state");

        let track =
            track_kind_to_db("BROKEN", "").expect_err("unknown track kind must be rejected");
        assert_eq!(track.message(), "unknown track kind: BROKEN");
        assert_validation_field(&track, "kind", "must be AUDIO, VIDEO, SCREEN, or DATA");
    }

    #[test]
    fn webrtc_json_helper_carries_field_violation() {
        let err = wv_json("{not-json").expect_err("invalid JSON must be rejected");
        assert!(
            err.message().starts_with("webrtc JSON field is invalid:"),
            "human message should preserve parse context"
        );
        assert_validation_field(&err, "json", "must be valid JSON");
    }

    #[test]
    fn empty_signaling_stream_carries_field_violation() {
        let err = empty_signaling_stream_status();
        assert_eq!(err.message(), "empty signaling stream");
        assert_validation_field(&err, "stream", "must include an initial signaling message");
    }

    #[test]
    fn webrtc_missing_setup_capabilities_carry_typed_detail() {
        for (operation, capability, message) in [
            (
                "native_entity_dispatch",
                "runtime_native_entity_dispatch",
                "webrtc service requires runtime native entity dispatch",
            ),
            (
                "postgres_store",
                "postgres_store",
                "webrtc service requires a Postgres-backed store (no PG pool configured)",
            ),
        ] {
            let err = webrtc_capability_status(operation, capability, message);
            assert_eq!(err.code(), tonic::Code::FailedPrecondition);
            assert_eq!(err.message(), message);
            let detail = decode_detail(&err);
            assert_eq!(detail.kind, ErrorKind::Capability as i32);
            assert_eq!(detail.backend, "webrtc");
            assert_eq!(detail.operation, operation);
            assert_eq!(detail.capability_required, capability);
            assert!(!detail.retryable);
        }
    }

    #[test]
    fn webrtc_state_denials_preserve_reason_and_policy_detail() {
        for (err, message, reason, operation, decision) in [
            (
                peer_not_active_status(),
                "peer is not an active member of this room",
                PEER_NOT_ACTIVE,
                "require_active_peer_membership",
                "peer_not_active",
            ),
            (
                room_not_joinable_status(),
                "room not found, closed, or at capacity",
                ROOM_FULL,
                "join_room",
                "room_not_joinable",
            ),
        ] {
            assert_eq!(err.code(), tonic::Code::FailedPrecondition);
            assert_eq!(err.message(), message);
            assert_eq!(
                err.metadata()
                    .get("error-reason")
                    .map(|value| value.to_str().unwrap()),
                Some(reason)
            );
            let detail = decode_detail(&err);
            assert_eq!(detail.kind, ErrorKind::Policy as i32);
            assert_eq!(detail.operation, operation);
            assert_eq!(detail.policy_decision_id, decision);
            assert!(!detail.retryable);
        }
    }

    #[test]
    fn webrtc_permission_denials_preserve_reason_and_policy_detail() {
        for (err, message, reason, operation, decision) in [
            (
                peer_not_active_permission_status(),
                "peer is not an active member of this room",
                PEER_NOT_ACTIVE,
                "signal_peer_membership",
                "peer_not_active",
            ),
            (
                egress_tenant_scope_mismatch_status(),
                EGRESS_TENANT_SCOPE_MISMATCH_MESSAGE,
                EGRESS_TENANT_SCOPE_MISMATCH,
                "stop_egress",
                "egress_tenant_scope_mismatch",
            ),
        ] {
            assert_eq!(err.code(), tonic::Code::PermissionDenied);
            assert_eq!(err.message(), message);
            assert_eq!(
                err.metadata()
                    .get("error-reason")
                    .map(|value| value.to_str().unwrap()),
                Some(reason)
            );
            let detail = decode_detail(&err);
            assert_eq!(detail.kind, ErrorKind::Policy as i32);
            assert_eq!(detail.operation, operation);
            assert_eq!(detail.policy_decision_id, decision);
            assert!(!detail.retryable);
        }
    }

    #[cfg(feature = "webrtc")]
    #[test]
    fn sfu_backend_unavailable_preserves_reason_and_capability_detail() {
        let err = sfu_backend_unavailable_status("join-token mint failed");
        assert_eq!(err.code(), tonic::Code::FailedPrecondition);
        assert_eq!(
            err.message(),
            "SFU backend unavailable: join-token mint failed"
        );
        assert_eq!(
            err.metadata()
                .get("error-reason")
                .map(|value| value.to_str().unwrap()),
            Some(SFU_BACKEND_UNAVAILABLE)
        );
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Capability as i32);
        assert_eq!(detail.backend, "webrtc");
        assert_eq!(detail.operation, "sfu_join_token");
        assert_eq!(detail.capability_required, "sfu_backend");
        assert!(!detail.retryable);
    }

    #[test]
    fn turn_secret_setup_capability_preserves_reason_and_typed_detail() {
        for operation in ["join_session", "issue_credentials"] {
            let err = turn_secret_not_configured_status(operation);
            assert_eq!(err.code(), tonic::Code::FailedPrecondition);
            assert_eq!(err.message(), TURN_NOT_CONFIGURED_MESSAGE);
            assert_eq!(
                err.metadata()
                    .get("error-reason")
                    .map(|value| value.to_str().unwrap()),
                Some(TURN_NOT_CONFIGURED)
            );
            let detail = decode_detail(&err);
            assert_eq!(detail.kind, ErrorKind::Capability as i32);
            assert_eq!(detail.backend, "webrtc");
            assert_eq!(detail.operation, operation);
            assert_eq!(detail.capability_required, "turn_secret");
            assert!(!detail.retryable);
        }
    }

    // ── Egress (master-plan 5.5) ─────────────────────────────────────────────

    /// Disabled flag (the default) ⇒ `FAILED_PRECONDITION`/`EGRESS_NOT_ENABLED`,
    /// NOT `UNIMPLEMENTED` (the capability-lie / retry-storm guard). No pool is
    /// needed because the gate fires before any backend/DB access.
    #[tokio::test]
    async fn egress_disabled_returns_failed_precondition_not_unimplemented() {
        let mut svc = WebrtcServiceImpl::new();
        svc.egress = EgressCapability::Disabled; // pin (don't depend on process env)
        let mut request = Request::new(webrtc_pb::StartRoomCompositeRequest {
            tenant_id: "tenant-a".to_string(),
            room_id: "00000000-0000-0000-0000-000000000001".to_string(),
            ..Default::default()
        });
        request
            .metadata_mut()
            .insert("x-tenant-id", MetadataValue::from_static("tenant-a"));
        let err = svc
            .start_room_composite(request)
            .await
            .expect_err("disabled egress must fail closed");
        assert_eq!(err.code(), tonic::Code::FailedPrecondition);
        assert_ne!(err.code(), tonic::Code::Unimplemented);
        assert_eq!(err.message(), EGRESS_NOT_ENABLED_MESSAGE);
        assert_eq!(
            err.metadata()
                .get("error-reason")
                .map(|v| v.to_str().unwrap()),
            Some(EGRESS_NOT_ENABLED)
        );
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Capability as i32);
        assert_eq!(detail.backend, "webrtc");
        assert_eq!(detail.operation, "start_room_composite");
        assert_eq!(detail.capability_required, "webrtc_egress_enabled");
        assert!(!detail.retryable);
    }

    #[tokio::test]
    async fn egress_enabled_without_backend_carries_capability_detail() {
        let mut svc = WebrtcServiceImpl::new();
        svc.egress = EgressCapability::EnabledNoBackend;
        let mut request = Request::new(webrtc_pb::ListEgressRequest {
            tenant_id: "tenant-a".to_string(),
            room_id: "room-a".to_string(),
            ..Default::default()
        });
        request
            .metadata_mut()
            .insert("x-tenant-id", MetadataValue::from_static("tenant-a"));
        let err = svc
            .list_egress(request)
            .await
            .expect_err("enabled egress without backend must fail closed");
        assert_eq!(err.code(), tonic::Code::FailedPrecondition);
        assert_ne!(err.code(), tonic::Code::Unimplemented);
        assert_eq!(err.message(), EGRESS_BACKEND_UNAVAILABLE_MESSAGE);
        assert_eq!(
            err.metadata()
                .get("error-reason")
                .map(|v| v.to_str().unwrap()),
            Some(EGRESS_BACKEND_UNAVAILABLE)
        );
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Capability as i32);
        assert_eq!(detail.backend, "webrtc");
        assert_eq!(detail.operation, "list_egress");
        assert_eq!(detail.capability_required, "webrtc_egress_backend");
        assert!(!detail.retryable);
    }

    /// A cross-tenant egress BODY (foreign tenant_id vs the scoped metadata
    /// tenant) is rejected by the scope guard before any egress work.
    #[tokio::test]
    async fn start_room_composite_rejects_cross_tenant_body() {
        let svc = WebrtcServiceImpl::new();
        let mut request = Request::new(webrtc_pb::StartRoomCompositeRequest {
            tenant_id: "tenant-b".to_string(),
            room_id: "00000000-0000-0000-0000-000000000001".to_string(),
            ..Default::default()
        });
        request
            .metadata_mut()
            .insert("x-tenant-id", MetadataValue::from_static("tenant-a"));
        let err = svc
            .start_room_composite(request)
            .await
            .expect_err("cross-tenant body must be rejected");
        assert_eq!(err.code(), tonic::Code::PermissionDenied);
    }

    /// StopEgress with an `egress_id` minted for ANOTHER tenant is rejected
    /// (`PermissionDenied`) even when egress is enabled — a tenant can never stop
    /// another tenant's egress (cross-tenant leak guard).
    #[tokio::test]
    async fn stop_egress_rejects_cross_tenant_egress_id() {
        let mut svc = WebrtcServiceImpl::new();
        svc.egress = EgressCapability::EnabledNoBackend;
        let foreign_id = new_egress_id("tenant-b");
        let mut request = Request::new(webrtc_pb::StopEgressRequest {
            tenant_id: "tenant-a".to_string(),
            egress_id: foreign_id,
        });
        request
            .metadata_mut()
            .insert("x-tenant-id", MetadataValue::from_static("tenant-a"));
        let err = svc
            .stop_egress(request)
            .await
            .expect_err("foreign-tenant egress_id must be rejected");
        assert_eq!(err.code(), tonic::Code::PermissionDenied);
        assert_eq!(err.message(), EGRESS_TENANT_SCOPE_MISMATCH_MESSAGE);
        assert_eq!(
            err.metadata()
                .get("error-reason")
                .map(|value| value.to_str().unwrap()),
            Some(EGRESS_TENANT_SCOPE_MISMATCH)
        );
        let detail = decode_detail(&err);
        assert_eq!(detail.kind, ErrorKind::Policy as i32);
        assert_eq!(detail.operation, "stop_egress");
        assert_eq!(detail.policy_decision_id, "egress_tenant_scope_mismatch");
    }

    /// The emit-topic constants must match the dotted `emits` contract declared
    /// on the RoomService egress RPCs in webrtc_service.proto, and a minted
    /// egress_id must be scoped to its owning tenant only.
    #[test]
    fn egress_topics_and_id_scoping_match_contract() {
        assert_eq!(EGRESS_STARTED_TOPIC, "udb.webrtc.egress.started.v1");
        assert_eq!(EGRESS_STOPPED_TOPIC, "udb.webrtc.egress.stopped.v1");
        assert_eq!(EGRESS_FAILED_TOPIC, "udb.webrtc.egress.failed.v1");

        let id = new_egress_id("tenant-a");
        assert!(egress_id_is_tenant_scoped(&id, "tenant-a"));
        assert!(!egress_id_is_tenant_scoped(&id, "tenant-b"));
        assert!(!egress_id_is_tenant_scoped("not-an-egress-id", "tenant-a"));
    }
}

impl DataBrokerService {
    /// Build the native WebRTC service, wired to the broker's Postgres pool.
    pub(crate) fn build_webrtc_service(&self) -> WebrtcServiceImpl {
        let runtime = self.runtime.load_full();
        // Native-service persistence resolves through the discovery seam (extend_udb.md):
        // the backend is read from this service's proto `native_service` binding, then a
        // health/weight-routed instance is chosen — not the process-global pool.
        let pg_pool = runtime
            .native_store_pool_for_service("webrtc.room", true, "")
            .ok();
        let outbox = runtime.config().cdc.outbox_relation();
        let channels = Some(runtime.channels().clone());
        let svc = WebrtcServiceImpl::new()
            .with_postgres(pg_pool)
            .with_runtime(Some(runtime.clone()))
            .with_outbox(Some(outbox))
            .with_channels(channels)
            .with_metrics(self.metrics.clone());

        // Report the recording/egress capability honestly at mount time
        // (master-plan 5.5): "disabled" when the flag is unset, "degraded" when
        // it is set but no egress backend is wired (mounted but degraded).
        tracing::info!(
            egress = svc.egress_capability_label(),
            "webrtc egress capability"
        );

        let interval_secs = std::env::var("UDB_WEBRTC_REAP_INTERVAL_SECS")
            .ok()
            .and_then(|v| v.parse::<u64>().ok())
            .unwrap_or(STALE_PEER_REAP_INTERVAL_SECS_DEFAULT);
        let stale_after_secs = std::env::var("UDB_WEBRTC_STALE_PEER_TIMEOUT_SECS")
            .ok()
            .and_then(|v| v.parse::<i64>().ok())
            .unwrap_or(STALE_PEER_TIMEOUT_SECS_DEFAULT);
        if interval_secs > 0 && stale_after_secs > 0 && svc.pg_pool.is_some() {
            let reaper = svc.clone();
            let singleton_pool = svc.pg_pool.clone().expect("checked above");
            let singleton_relation = runtime.config().cdc.lock_log_relation();
            crate::runtime::service::native_runtime::NativeWorkerHost::spawn_while_leader(
                crate::runtime::singleton::WORKER_WEBRTC_STALE_PEER_REAPER,
                "webrtc stale-peer reaper disconnected peers",
                singleton_pool,
                singleton_relation,
                Duration::from_secs(interval_secs),
                move || {
                    let reaper_once = reaper.clone();
                    async move {
                        reaper_once
                            .reap_stale_peers(stale_after_secs, STALE_PEER_REAP_BATCH_SIZE)
                            .await
                            .map(|n| n as i64)
                    }
                },
            );
        }

        svc
    }
}