rvoip-sip 0.3.8

SIP umbrella for RVoIP: api/* (UnifiedCoordinator, StreamPeer, CallbackPeer, Endpoint), server/* (B2BUA helpers), adapter/* (rvoip-core::ConnectionAdapter impl)
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
//! Incoming call handling — accept, reject, redirect, or defer.
//!
//! Inbound SIP messages reach the application through one of four typed
//! wrappers that all implement
//! [`crate::api::headers::SipHeaderView`]:
//!
//! | Wrapper | What it wraps |
//! |---|---|
//! | [`IncomingCall`] | Inbound INVITE |
//! | [`IncomingRequest`] | In-dialog REFER / NOTIFY / INFO / OPTIONS / UPDATE / MESSAGE |
//! | [`IncomingResponse`] | Every inbound response (1xx / 2xx / 3xx / 4xx-6xx) |
//! | [`IncomingRegister`] | Inbound REGISTER on registrar surfaces |

#![deny(missing_docs)]

use std::collections::HashMap;
use std::sync::{
    atomic::{AtomicBool, AtomicU8, Ordering},
    Arc, OnceLock, Weak,
};
use std::time::{Duration, Instant};

use rvoip_sip_core::types::headers::{HeaderAccess, HeaderName, TypedHeader};
use rvoip_sip_core::{Request, Response};

use crate::api::events::Event;
use crate::api::handle::{CallId, SessionHandle};
use crate::api::headers::view::{
    header_names_slice, header_slice, headers_named_slice, SipHeaderView,
};
use crate::api::lifecycle::{CallLifecycleSnapshot, CallTerminalInfo};
use crate::api::unified::UnifiedCoordinator;
use crate::errors::{Result, SessionError};
use crate::session_lifecycle::{OwnedOperation, OwnedOperationCompletion, SessionOperationKind};
use crate::session_registry::SessionRegistryHandle;
use crate::state_table::EventType;
use crate::types::CallState;

const INCOMING_GUARD_RESPONSE_COMPLETION_GRACE: Duration = Duration::from_secs(2);
const INCOMING_RESOLUTION_OPERATION_TIMEOUT: Duration = Duration::from_secs(10);

async fn wait_for_incoming_guard_cancellation(cancel: &mut tokio::sync::watch::Receiver<bool>) {
    loop {
        if *cancel.borrow_and_update() {
            return;
        }
        if cancel.changed().await.is_err() {
            return;
        }
    }
}

async fn rollback_owned_incoming_guard<T>(
    operation: OwnedOperation,
    value: T,
) -> OwnedOperationCompletion<T> {
    operation
        .rollback(value)
        .await
        .unwrap_or_else(|_| panic!("incoming-call guard exact rollback failed"))
}

async fn commit_owned_incoming_guard<T>(
    operation: OwnedOperation,
    value: T,
) -> OwnedOperationCompletion<T> {
    match operation.commit() {
        Ok(committed) => committed.complete(value),
        Err(failure) => rollback_owned_incoming_guard(failure.into_operation(), value).await,
    }
}

fn spawn_exact_incoming_reject(
    coordinator: Arc<UnifiedCoordinator>,
    lifecycle_handle: Option<SessionRegistryHandle>,
    status: u16,
    reason: String,
) {
    let Some(lifecycle_handle) = lifecycle_handle else {
        tracing::warn!(
            status,
            "incoming-call rejection suppressed because exact lifecycle authority is absent"
        );
        return;
    };
    let authority = Arc::clone(coordinator.helpers.state_machine.store.authority());
    let state_machine = Arc::clone(&coordinator.helpers.state_machine);
    let operation_key = lifecycle_handle.key().clone();
    let log_call_id = lifecycle_handle.session_id().clone();
    let scheduled = authority.spawn_owned_exact(
        &operation_key,
        SessionOperationKind::Signaling,
        INCOMING_RESOLUTION_OPERATION_TIMEOUT,
        move |operation| async move {
            let committed = match operation.commit() {
                Ok(committed) => committed,
                Err(failure) => {
                    return rollback_owned_incoming_guard(failure.into_operation(), ()).await;
                }
            };
            if let Err(error) = state_machine
                .process_event_exact(&lifecycle_handle, EventType::RejectCall { status, reason })
                .await
            {
                tracing::debug!(
                    session_id = %lifecycle_handle.session_id(),
                    %error,
                    "exact incoming-call rejection did not dispatch"
                );
            }
            committed.complete(())
        },
    );
    if let Err(error) = scheduled {
        tracing::debug!(
            session_id = %log_call_id,
            %error,
            "exact incoming-call rejection was not admitted"
        );
    }
}

/// An incoming SIP INVITE that must be handled.
///
/// Obtain one via
/// [`StreamPeer::wait_for_incoming`](crate::api::stream_peer::StreamPeer::wait_for_incoming)
/// or via the `on_incoming_call` method of
/// [`CallHandler`](crate::api::callback_peer::CallHandler).
///
/// The call remains in `Ringing` state until you call one of the resolution
/// methods. Dropping without resolving rejects the call with **486 Busy Here**.
///
/// # Resolution options
///
/// | Method | SIP response | Use for |
/// |--------|-------------|---------|
/// | [`accept()`] | 200 OK | Softphones, immediate answer |
/// | [`reject()`] | 4xx/5xx | Busy, unauthorized, etc. |
/// | [`reject_busy()`] | 486 | Convenience wrapper |
/// | [`reject_decline()`] | 603 | User declined |
/// | [`redirect_to()`] | 302 | Proxy / forward-to-voicemail |
/// | [`redirect_with_contacts()`] | 3xx | Multiple redirect choices |
/// | [`defer()`] | (hold) | Call center queue |
///
/// [`accept()`]: IncomingCall::accept
/// [`reject()`]: IncomingCall::reject
/// [`reject_busy()`]: IncomingCall::reject_busy
/// [`reject_decline()`]: IncomingCall::reject_decline
/// [`redirect_to()`]: IncomingCall::redirect_to
/// [`redirect_with_contacts()`]: IncomingCall::redirect_with_contacts
/// [`defer()`]: IncomingCall::defer
pub struct IncomingCall {
    /// The session / call identifier.
    pub call_id: CallId,
    /// SIP From URI (caller).
    pub from: String,
    /// SIP To URI (called party).
    pub to: String,
    /// Remote SDP offer, if present.
    pub sdp: Option<String>,
    /// Additional SIP headers (lower-cased names).
    ///
    /// **Deprecated:** prefer
    /// [`crate::api::headers::SipHeaderView`] inspection
    /// via [`Self::header`], [`Self::header_str`], or
    /// [`Self::raw_request`]. This field is populated from the parsed
    /// INVITE for back-compat and may carry only a curated subset of
    /// the wire headers. Will be removed in a future breaking release.
    pub headers: HashMap<String, String>,
    /// When the INVITE arrived.
    pub received_at: Instant,
    /// The parsed inbound INVITE request, when available. `None` only
    /// when the call was synthesized (tests, lean-mode feature flag).
    pub(crate) request: Option<Arc<Request>>,
    /// Transport context for auth policy decisions.
    pub(crate) transport: crate::auth::SipTransportSecurityContext,
    /// Internal — coordinator for performing accept/reject.
    pub(crate) coordinator: Arc<UnifiedCoordinator>,
    /// Causal authority for the exact registry lifetime that emitted this
    /// inbound INVITE. It is never reconstructed by delayed work.
    lifecycle_handle: Option<SessionRegistryHandle>,
    /// Whether this call has already been resolved (to catch double-resolve).
    resolved: bool,
}

impl std::fmt::Debug for IncomingCall {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("IncomingCall")
            .field("sdp_present", &self.sdp.is_some())
            .field("sdp_bytes", &self.sdp.as_ref().map_or(0, String::len))
            .field("legacy_header_count", &self.headers.len())
            .field("raw_request_present", &self.request.is_some())
            .field(
                "raw_request_header_count",
                &self
                    .request
                    .as_ref()
                    .map_or(0, |request| request.headers.len()),
            )
            .field(
                "raw_request_body_bytes",
                &self
                    .request
                    .as_ref()
                    .map_or(0, |request| request.body().len()),
            )
            .field("resolved", &self.resolved)
            .finish()
    }
}

impl IncomingCall {
    #[cfg(test)]
    pub(crate) fn new(
        call_id: CallId,
        from: String,
        to: String,
        sdp: Option<String>,
        coordinator: Arc<UnifiedCoordinator>,
    ) -> Self {
        let lifecycle_handle = coordinator
            .helpers
            .state_machine
            .store
            .lifecycle_handle(&call_id);
        Self::new_captured(call_id, from, to, sdp, coordinator, lifecycle_handle)
    }

    pub(crate) fn new_captured(
        call_id: CallId,
        from: String,
        to: String,
        sdp: Option<String>,
        coordinator: Arc<UnifiedCoordinator>,
        lifecycle_handle: Option<SessionRegistryHandle>,
    ) -> Self {
        Self {
            call_id,
            from,
            to,
            sdp,
            headers: HashMap::new(),
            received_at: Instant::now(),
            request: None,
            transport: crate::auth::SipTransportSecurityContext::unknown(),
            coordinator,
            lifecycle_handle,
            resolved: false,
        }
    }

    #[cfg(test)]
    pub(crate) fn new_exact(
        call_id: CallId,
        from: String,
        to: String,
        sdp: Option<String>,
        coordinator: Arc<UnifiedCoordinator>,
        lifecycle_handle: SessionRegistryHandle,
    ) -> Self {
        debug_assert_eq!(&call_id, lifecycle_handle.session_id());
        Self::new_captured(call_id, from, to, sdp, coordinator, Some(lifecycle_handle))
    }

    /// Construct an `IncomingCall` with a parsed inbound INVITE retained.
    /// Populates [`Self::headers`] from the request for compatibility with the
    /// legacy field while preserving the captured exact lifecycle.
    pub(crate) fn with_request_captured(
        call_id: CallId,
        from: String,
        to: String,
        sdp: Option<String>,
        coordinator: Arc<UnifiedCoordinator>,
        request: Arc<Request>,
        lifecycle_handle: Option<SessionRegistryHandle>,
    ) -> Self {
        let mut headers: HashMap<String, String> = HashMap::new();
        for hdr in &request.headers {
            let name = hdr.name();
            let key = legacy_header_key(&name);
            headers.entry(key).or_insert_with(|| hdr.to_string());
        }
        Self {
            call_id,
            from,
            to,
            sdp,
            headers,
            received_at: Instant::now(),
            request: Some(request),
            transport: crate::auth::SipTransportSecurityContext::unknown(),
            coordinator,
            lifecycle_handle,
            resolved: false,
        }
    }

    pub(crate) fn with_transport_context(
        mut self,
        transport: crate::auth::SipTransportSecurityContext,
    ) -> Self {
        self.transport = transport;
        self
    }

    /// Transport context used by auth policy decisions.
    pub fn transport_security_context(&self) -> &crate::auth::SipTransportSecurityContext {
        &self.transport
    }

    /// Underlying parsed [`Request`]. `None` when the call was
    /// synthesized (tests) or under a future lean-mode feature flag.
    pub fn raw_request(&self) -> Option<&Arc<Request>> {
        self.request.as_ref()
    }

    /// Authenticate this inbound INVITE with a UAS SIP auth service.
    ///
    /// Pass [`SipAuthService`](crate::SipAuthService) for Digest, Bearer,
    /// Basic, AKA, or multi-challenge validation. Pass
    /// [`SipDigestAuthService`](crate::SipDigestAuthService) for Digest-only
    /// compatibility.
    pub async fn authenticate_with<A>(
        &self,
        auth: &A,
    ) -> Result<<A as crate::auth::SipIncomingAuthenticator>::Decision>
    where
        A: crate::auth::SipIncomingAuthenticator + Sync,
    {
        let request = self.request.as_ref().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingCall.authenticate_with() requires a parsed inbound INVITE".to_string(),
            )
        })?;
        let (authorization, source) = selected_authorization(request);
        let request_uri = request.uri().to_string();
        let body = if request.body().is_empty() {
            None
        } else {
            Some(request.body())
        };
        let transport = effective_transport_security_context(Some(request), &self.transport);
        auth.authenticate_incoming_with_transport_context(
            authorization.as_deref(),
            "INVITE",
            &request_uri,
            body,
            source,
            &transport,
        )
        .await
    }

    /// Zero-alloc header iteration — preferred over the boxed trait
    /// method on hot paths.
    pub fn headers_named_iter<'a>(
        &'a self,
        name: &'a HeaderName,
    ) -> impl Iterator<Item = &'a TypedHeader> + 'a {
        let slice: &[TypedHeader] = match &self.request {
            Some(r) => &r.headers[..],
            None => &[],
        };
        headers_named_slice(slice, name)
    }

    // ===== Resolution methods =====

    /// Accept the call and return a [`SessionHandle`] for controlling it.
    ///
    /// Completes SDP negotiation and sends 200 OK.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn example(incoming: rvoip_sip::IncomingCall) -> rvoip_sip::Result<()> {
    /// let call = incoming.accept().await?;
    /// # let _ = call;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn accept(self) -> Result<SessionHandle> {
        self.accept_builder().send().await
    }

    /// Accept the call with a custom SDP answer.
    ///
    /// This is intended for B2BUA or gateway flows where the application has
    /// already obtained an answer body from another leg.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn example(incoming: rvoip_sip::IncomingCall, answer_sdp: String) -> rvoip_sip::Result<()> {
    /// let call = incoming.accept_with_sdp(answer_sdp).await?;
    /// # let _ = call;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn accept_with_sdp(self, sdp: String) -> Result<SessionHandle> {
        self.accept_builder().with_sdp(sdp).send().await
    }

    /// Send a reliable 183 Session Progress with early-media SDP (RFC 3262).
    ///
    /// Does NOT consume the call — you still need to call [`accept()`] or
    /// [`reject()`] afterward. Typical sequence:
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn example(incoming: rvoip_sip::IncomingCall) -> rvoip_sip::Result<()> {
    /// incoming.send_early_media(None).await?;
    /// tokio::time::sleep(std::time::Duration::from_secs(2)).await;
    /// let session = incoming.accept().await?;
    /// # let _ = session;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// See [`PeerControl::send_early_media`] for the full semantics and the
    /// 100rel failure mode.
    ///
    /// [`accept()`]: IncomingCall::accept
    /// [`reject()`]: IncomingCall::reject
    /// [`PeerControl::send_early_media`]: crate::api::stream_peer::PeerControl::send_early_media
    pub async fn send_early_media(&self, sdp: Option<String>) -> Result<()> {
        let lifecycle_handle = self.lifecycle_handle.as_ref().ok_or_else(|| {
            SessionError::SessionNotFound(format!(
                "Incoming call {} has no exact lifecycle authority",
                self.call_id
            ))
        })?;
        self.coordinator
            .helpers
            .send_early_media_exact(lifecycle_handle, sdp)
            .await
    }

    /// Send a reliable 183 Session Progress and immediately swap the
    /// session's RTP transmitter to `source`. Lets the UAS play a ringback
    /// tone / "please hold" announcement during the `EarlyMedia` state.
    ///
    /// The source plays until the dialog transitions to `Active` (after
    /// `accept()`), at which point the state machine automatically swaps
    /// back to [`AudioSource::PassThrough`][crate::api::unified::AudioSource::PassThrough]
    /// so bidirectional audio flows without further action. Apps that want
    /// a *different* source during the active call (hold music, continued
    /// announcement) can call
    /// [`coordinator.set_audio_source`][crate::api::unified::UnifiedCoordinator::set_audio_source]
    /// again after observing `Event::CallEstablished`.
    ///
    /// See `docs/AUDIO_MODES.md` for the endpoint-vs-bridge comparison.
    ///
    /// Same 100rel precondition as [`send_early_media`][Self::send_early_media].
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use rvoip_sip::{AudioSource, IncomingCall};
    /// # async fn demo(incoming: IncomingCall) -> rvoip_sip::Result<()> {
    /// incoming.send_early_media_with_source(
    ///     None,
    ///     AudioSource::Tone { frequency: 440.0, amplitude: 0.5 },
    /// ).await?;
    /// // Ringback plays; later call accept() to answer.
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send_early_media_with_source(
        &self,
        sdp: Option<String>,
        source: crate::api::unified::AudioSource,
    ) -> Result<()> {
        let lifecycle_handle = self.lifecycle_handle.as_ref().ok_or_else(|| {
            SessionError::SessionNotFound(format!(
                "Incoming call {} has no exact lifecycle authority",
                self.call_id
            ))
        })?;
        self.coordinator
            .helpers
            .send_early_media_exact(lifecycle_handle, sdp)
            .await?;
        self.coordinator
            .set_audio_source_exact(lifecycle_handle, source)
            .await
    }

    /// Reject the call immediately with an explicit SIP status code and reason.
    ///
    /// This spawns the reject operation and returns immediately.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn example(incoming: rvoip_sip::IncomingCall) {
    /// incoming.reject(486, "Busy Here");
    /// # }
    /// ```
    pub fn reject(mut self, status: u16, reason: &str) {
        self.resolved = true;
        spawn_exact_incoming_reject(
            self.coordinator.clone(),
            self.lifecycle_handle.clone(),
            status,
            reason.to_string(),
        );
    }

    /// Reject with **486 Busy Here**.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn example(incoming: rvoip_sip::IncomingCall) {
    /// incoming.reject_busy();
    /// # }
    /// ```
    pub fn reject_busy(self) {
        self.reject(486, "Busy Here");
    }

    /// Reject with **603 Decline** (user explicitly declined).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn example(incoming: rvoip_sip::IncomingCall) {
    /// incoming.reject_decline();
    /// # }
    /// ```
    pub fn reject_decline(self) {
        self.reject(603, "Decline");
    }

    /// Redirect the caller to another URI with **302 Moved Temporarily**.
    ///
    /// This sends a SIP 3xx response with a `Contact` header. It is a
    /// rvoip-sip primitive; higher-level B2BUA/routing layers decide
    /// whether redirect is the right policy for a particular call.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn example(incoming: rvoip_sip::IncomingCall) -> rvoip_sip::Result<()> {
    /// incoming.redirect_to("sip:voicemail@example.com").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn redirect_to(self, target: impl Into<String>) -> Result<()> {
        self.redirect_with_contacts(302, [target.into()]).await
    }

    /// Redirect the caller with an explicit 3xx status and Contact list.
    ///
    /// `status` must be in `300..=399` and `contacts` must contain at least
    /// one SIP URI string.
    pub async fn redirect_with_contacts<I, S>(mut self, status: u16, contacts: I) -> Result<()>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        if !(300..=399).contains(&status) {
            return Err(SessionError::InvalidInput(format!(
                "redirect status must be 3xx, got {status}"
            )));
        }
        let contacts = contacts.into_iter().map(Into::into).collect::<Vec<_>>();
        if contacts.is_empty() {
            return Err(SessionError::InvalidInput(
                "redirect requires at least one Contact URI".to_string(),
            ));
        }
        self.resolved = true;
        crate::api::respond::RedirectBuilder::new_captured(
            self.coordinator.clone(),
            self.call_id.clone(),
            self.lifecycle_handle.clone(),
        )
        .with_status(status)
        .with_contacts(contacts)
        .send()
        .await
    }

    /// Defer the accept/reject decision, keeping the call in `Ringing` state
    /// until the returned [`IncomingCallGuard`] is resolved or the `timeout`
    /// elapses.
    ///
    /// Use this for call queues: store the guard in a queue data structure and
    /// call `guard.accept()` when an agent becomes available.
    ///
    /// If the timeout elapses while the guard is unresolved, or the guard is
    /// dropped without being resolved, the call is rejected with **503 Service
    /// Unavailable**.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn example(incoming: rvoip_sip::IncomingCall) {
    /// let guard = incoming.defer(std::time::Duration::from_secs(30));
    /// // Store `guard` in a queue and later call guard.accept().await.
    /// # let _ = guard;
    /// # }
    /// ```
    pub fn defer(mut self, timeout: Duration) -> IncomingCallGuard {
        self.resolved = true; // prevent Drop from also rejecting
        if self.coordinator.fast_auto_accept_incoming_calls() {
            return IncomingCallGuard::resolved(
                self.call_id.clone(),
                self.coordinator.clone(),
                self.lifecycle_handle.clone(),
            );
        }
        IncomingCallGuard::new_captured(
            self.call_id.clone(),
            self.coordinator.clone(),
            timeout,
            self.lifecycle_handle.clone(),
        )
    }

    // ─────────────────────────────────────────────────────────────────
    // SIP_API_DESIGN_2 Phase D — builder entry points on inbound INVITE.
    // ─────────────────────────────────────────────────────────────────

    /// Begin an `AcceptBuilder` so additional response headers can be
    /// staged before sending 200 OK.
    pub fn accept_builder(mut self) -> crate::api::respond::AcceptBuilder {
        self.resolved = true;
        crate::api::respond::AcceptBuilder::new_captured(
            self.coordinator.clone(),
            self.call_id.clone(),
            self.lifecycle_handle.clone(),
        )
    }

    /// Begin a `RejectBuilder` for a custom 4xx/5xx/6xx response.
    pub fn reject_builder(mut self) -> crate::api::respond::RejectBuilder {
        self.resolved = true;
        crate::api::respond::RejectBuilder::new_captured(
            self.coordinator.clone(),
            self.call_id.clone(),
            self.lifecycle_handle.clone(),
        )
    }

    /// Begin a `RedirectBuilder` for a custom 3xx response.
    pub fn redirect_builder(mut self) -> crate::api::respond::RedirectBuilder {
        self.resolved = true;
        crate::api::respond::RedirectBuilder::new_captured(
            self.coordinator.clone(),
            self.call_id.clone(),
            self.lifecycle_handle.clone(),
        )
    }

    /// Begin a `ProvisionalBuilder` for a 1xx reliable provisional.
    pub fn send_provisional_builder(&self, code: u16) -> crate::api::respond::ProvisionalBuilder {
        crate::api::respond::ProvisionalBuilder::new_captured(
            self.coordinator.clone(),
            self.call_id.clone(),
            self.lifecycle_handle.clone(),
            code,
        )
    }

    /// Begin an `AuthChallengeBuilder` for a 401/407 response.
    pub fn challenge_builder(
        &self,
        scheme: crate::api::respond::AuthScheme,
    ) -> crate::api::respond::AuthChallengeBuilder {
        crate::api::respond::AuthChallengeBuilder::new_captured(
            self.coordinator.clone(),
            self.call_id.clone(),
            rvoip_sip_core::types::Method::Invite,
            scheme,
            self.lifecycle_handle.clone(),
        )
    }

    /// Begin a `GenericResponseBuilder` for an arbitrary 3xx-6xx
    /// status (e.g. 491 Request Pending with `Retry-After`).
    pub fn respond_builder(
        mut self,
        status: u16,
    ) -> Result<crate::api::respond::GenericResponseBuilder> {
        self.resolved = true;
        crate::api::respond::GenericResponseBuilder::new_captured(
            self.coordinator.clone(),
            self.call_id.clone(),
            self.lifecycle_handle.clone(),
            rvoip_sip_core::types::Method::Invite,
            status,
        )
    }
}

/// Return the historical lowercase map key from the canonical SIP wire name.
///
/// The legacy map is protocol data despite being deprecated, so its keys must
/// never be derived from `Debug`: diagnostic output is deliberately redacted
/// and is free to change independently of RFC header spelling.
fn legacy_header_key(name: &HeaderName) -> String {
    name.canonical_wire_name().as_str().to_ascii_lowercase()
}

impl SipHeaderView for IncomingCall {
    fn header(&self, name: &HeaderName) -> Option<&TypedHeader> {
        self.request
            .as_ref()
            .and_then(|r| header_slice(&r.headers[..], name))
    }

    fn headers_named<'a>(
        &'a self,
        name: &HeaderName,
    ) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.request {
            Some(r) => {
                let name = name.clone();
                Box::new(
                    r.headers.iter().filter(move |h| {
                        crate::api::headers::view::header_name_eq(&h.name(), &name)
                    }),
                )
            }
            None => Box::new(std::iter::empty()),
        }
    }

    fn headers<'a>(&'a self) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.request {
            Some(r) => Box::new(r.headers.iter()),
            None => Box::new(std::iter::empty()),
        }
    }

    fn header_names(&self) -> Vec<HeaderName> {
        match &self.request {
            Some(r) => header_names_slice(&r.headers[..]),
            None => Vec::new(),
        }
    }
}

impl Drop for IncomingCall {
    fn drop(&mut self) {
        // Safety net for panicking handlers only. Normal paths set
        // `resolved = true` via accept/reject/defer, OR rely on the
        // CallbackPeer dispatch to apply the CallHandlerDecision after
        // this IncomingCall is dropped — neither should trigger an
        // auto-reject here.
        //
        // `thread::panicking()` is true while the current thread is
        // unwinding from a panic (which is exactly when destructors run
        // during task panics under tokio). This lets us distinguish the
        // rare "handler crashed" path from ordinary drops.
        if self.resolved || !std::thread::panicking() {
            return;
        }
        let coordinator = self.coordinator.clone();
        let call_id = self.call_id.clone();
        // RFC 3261 §21.5.1: 500 is the correct code for a server-side
        // unexpected failure. Sending it terminates the UAC's INVITE
        // transaction cleanly instead of leaving it hanging until Timer C.
        tracing::warn!(
            "[IncomingCall] handler panicked for call {} — sending 500 Server Internal Error",
            call_id
        );
        spawn_exact_incoming_reject(
            coordinator,
            self.lifecycle_handle.clone(),
            500,
            "Server Internal Error".to_string(),
        );
    }
}

// ===== IncomingCallGuard =====

/// A deferred incoming call held in `Ringing` state.
///
/// Created by [`IncomingCall::defer()`]. Must be resolved by calling
/// [`accept()`] or [`reject()`] before the deadline. If the deadline elapses
/// while the guard is unresolved, or the guard is dropped unresolved, the call
/// is rejected with **503 Service Unavailable**.
///
/// [`accept()`]: IncomingCallGuard::accept
/// [`reject()`]: IncomingCallGuard::reject
pub struct IncomingCallGuard {
    call_id: CallId,
    coordinator: Arc<UnifiedCoordinator>,
    lifecycle_handle: Option<SessionRegistryHandle>,
    deadline: Instant,
    resolved: Arc<AtomicBool>,
}

impl IncomingCallGuard {
    #[cfg(test)]
    fn new(call_id: CallId, coordinator: Arc<UnifiedCoordinator>, timeout: Duration) -> Self {
        let lifecycle_handle = coordinator
            .helpers
            .state_machine
            .store
            .lifecycle_handle(&call_id);
        Self::new_captured(call_id, coordinator, timeout, lifecycle_handle)
    }

    fn new_captured(
        call_id: CallId,
        coordinator: Arc<UnifiedCoordinator>,
        timeout: Duration,
        lifecycle_handle: Option<SessionRegistryHandle>,
    ) -> Self {
        let deadline = Instant::now() + timeout;
        let resolved = Arc::new(AtomicBool::new(false));

        // The watchdog is retained by the exact session lifecycle rather than
        // a detached raw-ID task. Quiesce cancels it, drain joins it, and the
        // retained registry handle prevents a later admission with the same
        // application identifier from receiving this rejection.
        let state_machine = Arc::clone(&coordinator.helpers.state_machine);
        if let Some(lifecycle_handle) = lifecycle_handle.clone() {
            let authority = Arc::clone(state_machine.store.authority());
            let operation_key = lifecycle_handle.key().clone();
            let watchdog_resolved = Arc::clone(&resolved);
            let remaining = deadline.saturating_duration_since(Instant::now());
            let hard_timeout = remaining.saturating_add(INCOMING_GUARD_RESPONSE_COMPLETION_GRACE);
            let scheduled = authority.spawn_owned_exact(
                &operation_key,
                SessionOperationKind::Signaling,
                hard_timeout,
                move |operation| async move {
                    let Some(mut cancellation) = operation.cancellation() else {
                        return rollback_owned_incoming_guard(operation, ()).await;
                    };
                    tokio::select! {
                        _ = tokio::time::sleep(remaining) => {}
                        () = wait_for_incoming_guard_cancellation(&mut cancellation) => {
                            return rollback_owned_incoming_guard(operation, ()).await;
                        }
                    }

                    if watchdog_resolved.swap(true, Ordering::SeqCst) {
                        return rollback_owned_incoming_guard(operation, ()).await;
                    }

                    let dispatch = state_machine
                        .process_event_exact(
                            &lifecycle_handle,
                            EventType::RejectCall {
                                status: 503,
                                reason: "Service Unavailable".to_string(),
                            },
                        )
                        .await;
                    if let Err(error) = dispatch {
                        tracing::debug!(
                            session_id = %lifecycle_handle.session_id(),
                            %error,
                            "incoming-call guard timeout did not target a current exact lifetime"
                        );
                        return rollback_owned_incoming_guard(operation, ()).await;
                    }
                    commit_owned_incoming_guard(operation, ()).await
                },
            );
            if let Err(error) = scheduled {
                tracing::debug!(
                    session_id = %call_id,
                    %error,
                    "incoming-call guard watchdog was not admitted for exact lifecycle ownership"
                );
            }
        }

        Self {
            call_id,
            coordinator,
            lifecycle_handle,
            deadline,
            resolved,
        }
    }

    fn resolved(
        call_id: CallId,
        coordinator: Arc<UnifiedCoordinator>,
        lifecycle_handle: Option<SessionRegistryHandle>,
    ) -> Self {
        Self {
            call_id,
            coordinator,
            lifecycle_handle,
            deadline: Instant::now(),
            resolved: Arc::new(AtomicBool::new(true)),
        }
    }

    /// The call identifier for this deferred call (use as queue key).
    ///
    /// This accessor is trivial and can be used to index a queue or map.
    pub fn call_id(&self) -> &CallId {
        &self.call_id
    }

    /// Mark this deferred call as resolved because rvoip-sip already
    /// observed a terminal event for it. This prevents the drop safety net
    /// from sending a late rejection for a call that no longer exists.
    pub(crate) fn resolve_without_response(&self) {
        self.resolved.store(true, Ordering::SeqCst);
    }

    /// When the guard expires and the call is auto-rejected.
    ///
    /// This accessor is trivial and is useful for queue ordering.
    pub fn deadline(&self) -> Instant {
        self.deadline
    }

    /// Accept the call now. Returns a [`SessionHandle`].
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn example(guard: rvoip_sip::IncomingCallGuard) -> rvoip_sip::Result<()> {
    /// let call = guard.accept().await?;
    /// # let _ = call;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn accept(self) -> Result<SessionHandle> {
        let lifecycle_handle = self.lifecycle_handle.clone().ok_or_else(|| {
            SessionError::SessionNotFound(format!(
                "Deferred incoming call {} has no exact lifecycle authority",
                self.call_id
            ))
        })?;
        if self.coordinator.fast_auto_accept_incoming_calls() {
            self.resolved.store(true, Ordering::SeqCst);
            return Ok(SessionHandle::new_exact(
                self.call_id.clone(),
                self.coordinator.clone(),
                lifecycle_handle,
            ));
        }

        if Instant::now() >= self.deadline {
            return Err(SessionError::Timeout(
                "IncomingCallGuard deadline exceeded before accept".to_string(),
            ));
        }
        if self.resolved.swap(true, Ordering::SeqCst) {
            return Err(SessionError::InvalidTransition(format!(
                "IncomingCallGuard for {} is already resolved",
                self.call_id
            )));
        }
        self.coordinator
            .helpers
            .accept_call_exact(&lifecycle_handle)
            .await?;
        Ok(SessionHandle::new_exact(
            self.call_id.clone(),
            self.coordinator.clone(),
            lifecycle_handle,
        ))
    }

    /// Reject the call now with the given SIP status code and reason phrase.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # fn example(guard: rvoip_sip::IncomingCallGuard) {
    /// guard.reject(503, "Service Unavailable");
    /// # }
    /// ```
    pub fn reject(self, status: u16, reason: &str) {
        if self.resolved.swap(true, Ordering::SeqCst) {
            return;
        }
        if self.coordinator.fast_auto_accept_incoming_calls() {
            return;
        }
        spawn_exact_incoming_reject(
            self.coordinator.clone(),
            self.lifecycle_handle.clone(),
            status,
            reason.to_string(),
        );
    }

    /// Abandon the deferred call locally without sending a SIP response.
    ///
    /// This is an explicit escape hatch for tests or external policy engines
    /// that know the INVITE is already being resolved elsewhere. Normal
    /// applications should prefer [`accept`](Self::accept),
    /// [`reject`](Self::reject), or waiting for a real cancellation event.
    pub fn abandon(self) {
        self.resolved.store(true, Ordering::SeqCst);
    }

    /// Reject the call and wait for the matching terminal event.
    ///
    /// This is the deterministic variant for queues and tests that need to
    /// know when the rejection has been observed by rvoip-sip's event
    /// stream. The event subscription is opened before the reject is sent.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn example(guard: rvoip_sip::IncomingCallGuard) -> rvoip_sip::Result<()> {
    /// let terminal = guard
    ///     .reject_and_wait(503, "Service Unavailable", Some(std::time::Duration::from_secs(3)))
    ///     .await?;
    /// # let _ = terminal;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn reject_and_wait(
        self,
        status: u16,
        reason: &str,
        timeout: Option<Duration>,
    ) -> Result<Event> {
        if self.coordinator.fast_auto_accept_incoming_calls() {
            self.resolved.store(true, Ordering::SeqCst);
            return Ok(Event::CallAnswered {
                call_id: self.call_id.clone(),
                sdp: None,
            });
        }
        if self.resolved.swap(true, Ordering::SeqCst) {
            return Err(SessionError::InvalidTransition(format!(
                "IncomingCallGuard for {} is already resolved",
                self.call_id
            )));
        }

        let mut events = self.coordinator.events_for_session(&self.call_id).await?;
        let lifecycle_handle = self.lifecycle_handle.as_ref().ok_or_else(|| {
            SessionError::SessionNotFound(format!(
                "Deferred incoming call {} has no exact lifecycle authority",
                self.call_id
            ))
        })?;
        self.coordinator
            .helpers
            .reject_call_exact(lifecycle_handle, status, reason)
            .await?;

        let fut = async {
            loop {
                match events.next().await {
                    Some(
                        event @ (Event::CallFailed { .. }
                        | Event::CallEnded { .. }
                        | Event::CallCancelled { .. }),
                    ) => return Ok(event),
                    Some(_) => {}
                    None => {
                        return Err(SessionError::Other(
                            "Event channel closed while waiting for reject".to_string(),
                        ));
                    }
                }
            }
        };

        match timeout {
            Some(duration) => tokio::time::timeout(duration, fut)
                .await
                .map_err(|_| SessionError::Timeout("reject_and_wait timed out".to_string()))?,
            None => fut.await,
        }
    }

    /// Wait for the caller to cancel this deferred ringing call.
    ///
    /// This is useful for ring/cancel tests, queues, and callback-style
    /// applications that intentionally keep an INVITE in ringing state. The
    /// method resolves only on [`Event::CallCancelled`]. It returns an error
    /// if the call is answered, rejected, failed, normally ended, the guard
    /// deadline expires, or the event stream closes.
    ///
    /// A caller-supplied timeout only cancels this wait. It does not mark the
    /// guard resolved, reject the call, abandon the call, or suppress the
    /// drop-time safety rejection.
    pub async fn wait_for_cancelled(&self, timeout: Option<Duration>) -> Result<()> {
        if self.resolved.load(Ordering::SeqCst) {
            return Err(SessionError::InvalidTransition(format!(
                "IncomingCallGuard for {} is already resolved",
                self.call_id
            )));
        }

        let resolved = self.resolved.clone();
        if let Some(result) = cancellation_result_from_snapshot(
            &self.coordinator.lifecycle_snapshot(&self.call_id).await,
        ) {
            resolved.store(true, Ordering::SeqCst);
            return result;
        }

        let remaining = self.deadline.saturating_duration_since(Instant::now());
        if remaining.is_zero() {
            return Err(SessionError::Timeout(
                "IncomingCallGuard deadline elapsed before cancellation".to_string(),
            ));
        }

        let wait_duration = timeout.map_or(remaining, |duration| duration.min(remaining));
        let mut events = self.coordinator.events_for_session(&self.call_id).await?;

        if let Some(result) = cancellation_result_from_snapshot(
            &self.coordinator.lifecycle_snapshot(&self.call_id).await,
        ) {
            resolved.store(true, Ordering::SeqCst);
            return result;
        }

        let fut = async {
            loop {
                match events.next().await {
                    Some(Event::CallCancelled { .. }) => {
                        resolved.store(true, Ordering::SeqCst);
                        return Ok(());
                    }
                    Some(Event::CallAnswered { .. }) => {
                        resolved.store(true, Ordering::SeqCst);
                        return Err(SessionError::Other(
                            "incoming call was answered before cancellation".to_string(),
                        ));
                    }
                    Some(Event::CallFailed {
                        status_code,
                        reason,
                        ..
                    }) => {
                        resolved.store(true, Ordering::SeqCst);
                        return Err(SessionError::Other(format!(
                            "incoming call failed before cancellation: {} {}",
                            status_code, reason
                        )));
                    }
                    Some(Event::CallEnded { reason, .. }) => {
                        resolved.store(true, Ordering::SeqCst);
                        return Err(SessionError::Other(format!(
                            "incoming call ended before cancellation: {}",
                            reason
                        )));
                    }
                    Some(_) => {}
                    None => {
                        return Err(SessionError::Other(
                            "Event channel closed while waiting for cancellation".to_string(),
                        ));
                    }
                }
            }
        };

        match tokio::time::timeout(wait_duration, fut).await {
            Ok(result) => result,
            Err(_) => {
                if Instant::now() >= self.deadline {
                    Err(SessionError::Timeout(
                        "IncomingCallGuard deadline elapsed before cancellation".to_string(),
                    ))
                } else {
                    Err(SessionError::Timeout(
                        "wait_for_cancelled timed out".to_string(),
                    ))
                }
            }
        }
    }
}

fn cancellation_result_from_snapshot(snapshot: &CallLifecycleSnapshot) -> Option<Result<()>> {
    match snapshot.terminal.as_ref() {
        Some(CallTerminalInfo::Cancelled) => return Some(Ok(())),
        Some(CallTerminalInfo::Failed {
            status_code,
            reason,
        }) => {
            return Some(Err(SessionError::Other(format!(
                "incoming call failed before cancellation: {} {}",
                status_code, reason
            ))));
        }
        Some(CallTerminalInfo::Ended { reason }) => {
            return Some(Err(SessionError::Other(format!(
                "incoming call ended before cancellation: {}",
                reason
            ))));
        }
        None => {}
    }

    if snapshot.answered.is_some() {
        return Some(Err(SessionError::Other(
            "incoming call was answered before cancellation".to_string(),
        )));
    }

    match snapshot.state.as_ref()? {
        CallState::Cancelled => Some(Ok(())),
        CallState::Failed(reason) => Some(Err(SessionError::Other(format!(
            "incoming call failed before cancellation: {:?}",
            reason
        )))),
        CallState::Terminated => Some(Err(SessionError::Other(
            "incoming call ended before cancellation".to_string(),
        ))),
        CallState::Answering
        | CallState::AnsweringHangupPending
        | CallState::Active
        | CallState::HoldPending
        | CallState::OnHold
        | CallState::Resuming
        | CallState::Muted
        | CallState::Bridged
        | CallState::Transferring
        | CallState::TransferringCall
        | CallState::ConsultationCall => Some(Err(SessionError::Other(
            "incoming call was answered before cancellation".to_string(),
        ))),
        _ => None,
    }
}

impl Drop for IncomingCallGuard {
    fn drop(&mut self) {
        if !self.resolved.swap(true, Ordering::SeqCst) {
            spawn_exact_incoming_reject(
                self.coordinator.clone(),
                self.lifecycle_handle.clone(),
                503,
                "Service Unavailable".to_string(),
            );
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────
// IncomingRequest / IncomingResponse / IncomingRegister
//
// These three wrappers complete the inbound surface. They are populated
// when rvoip-sip-dialog publishes an in-dialog request, an inbound response,
// or an inbound REGISTER. Today only `IncomingCall` carries a fully
// parsed `Arc<Request>`; the other three retain the same field shape so
// later phases can fill in their typed payloads without breaking the
// public API.
// ─────────────────────────────────────────────────────────────────────────

/// An in-dialog received SIP request (REFER / NOTIFY / INFO /
/// OPTIONS / UPDATE / MESSAGE).
///
/// Implements [`SipHeaderView`] for uniform header inspection.
#[derive(Clone)]
pub struct IncomingRequest {
    /// The session this request belongs to, when known.
    pub call_id: CallId,
    /// SIP From URI on the request.
    pub from: String,
    /// SIP To URI on the request.
    pub to: String,
    /// The SIP method (REFER, NOTIFY, …).
    pub method: rvoip_sip_core::types::Method,
    /// When the request arrived.
    pub received_at: Instant,
    /// The parsed inbound request, when available.
    pub(crate) request: Option<Arc<Request>>,
    /// Transport context for auth policy decisions.
    pub(crate) transport: crate::auth::SipTransportSecurityContext,
    /// Exact inbound server transaction used for an application-authored
    /// response. Legacy/synthetic events may not carry one.
    pub(crate) response_transaction: Option<rvoip_sip_dialog::transaction::TransactionKey>,
    /// Shared single-writer obligation for the exact final response.
    pub(crate) response_obligation: Option<Arc<ExactResponseObligation>>,
    /// Coordinator for sending responses. `None` for bus-constructed
    /// instances; the surface consumer (CallbackPeer / StreamPeer)
    /// repopulates this on dispatch so response builders can resolve
    /// the dialog/transaction.
    pub(crate) coordinator: Option<Arc<UnifiedCoordinator>>,
    /// Exact session generation carried by the causal event envelope. A
    /// missing sidecar stays missing so delayed requests never re-resolve a
    /// reused application Call-ID.
    lifecycle_handle: Option<SessionRegistryHandle>,
}

const EXACT_RESPONSE_AVAILABLE: u8 = 0;
const EXACT_RESPONSE_IN_FLIGHT: u8 = 1;
const EXACT_RESPONSE_COMPLETE: u8 = 2;

/// One cancellation-safe final-response authority retained by the
/// coordinator deadline registry.
///
/// INFO binds this owner to an exact session generation before registration;
/// REGISTER uses a standalone exact server-transaction scope.
pub(crate) struct ExactResponseObligation {
    state: AtomicU8,
    coordinator: OnceLock<Weak<UnifiedCoordinator>>,
    scope: OnceLock<ExactResponseScope>,
    call_id: Option<CallId>,
    transaction: rvoip_sip_dialog::transaction::TransactionKey,
    fallback_status: u16,
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum ExactResponseScope {
    Session(SessionRegistryHandle),
    StandaloneTransaction,
}

impl ExactResponseObligation {
    pub(crate) fn new(
        call_id: CallId,
        transaction: rvoip_sip_dialog::transaction::TransactionKey,
    ) -> Self {
        Self {
            state: AtomicU8::new(EXACT_RESPONSE_AVAILABLE),
            coordinator: OnceLock::new(),
            scope: OnceLock::new(),
            call_id: Some(call_id),
            transaction,
            fallback_status: 501,
        }
    }

    pub(crate) fn new_standalone(
        transaction: rvoip_sip_dialog::transaction::TransactionKey,
        fallback_status: u16,
    ) -> Self {
        debug_assert!((200..=699).contains(&fallback_status));
        let scope = OnceLock::new();
        let _ = scope.set(ExactResponseScope::StandaloneTransaction);
        Self {
            state: AtomicU8::new(EXACT_RESPONSE_AVAILABLE),
            coordinator: OnceLock::new(),
            scope,
            call_id: None,
            transaction,
            fallback_status,
        }
    }

    pub(crate) fn attach_coordinator(&self, coordinator: &Arc<UnifiedCoordinator>) {
        let _ = self.coordinator.set(Arc::downgrade(coordinator));
    }

    pub(crate) fn bind_lifecycle_handle(&self, handle: &SessionRegistryHandle) -> bool {
        if self.call_id.as_ref() != Some(handle.session_id()) {
            return false;
        }
        match self.scope.set(ExactResponseScope::Session(handle.clone())) {
            Ok(()) => true,
            Err(_) => self.scope.get() == Some(&ExactResponseScope::Session(handle.clone())),
        }
    }

    pub(crate) fn lifecycle_handle(&self) -> Option<&SessionRegistryHandle> {
        match self.scope.get() {
            Some(ExactResponseScope::Session(handle)) => Some(handle),
            Some(ExactResponseScope::StandaloneTransaction) | None => None,
        }
    }

    pub(crate) fn has_owner_scope(&self) -> bool {
        self.scope.get().is_some()
    }

    pub(crate) fn transaction(&self) -> &rvoip_sip_dialog::transaction::TransactionKey {
        &self.transaction
    }

    pub(crate) fn fallback_status(&self) -> u16 {
        self.fallback_status
    }

    pub(crate) fn claim(self: &Arc<Self>) -> Result<ExactResponseClaim> {
        self.state
            .compare_exchange(
                EXACT_RESPONSE_AVAILABLE,
                EXACT_RESPONSE_IN_FLIGHT,
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .map(|_| ExactResponseClaim {
                obligation: Arc::clone(self),
                armed: true,
            })
            .map_err(|_| {
                SessionError::InvalidInput(
                    "exact inbound response has already been claimed".to_string(),
                )
            })
    }

    fn complete(&self) {
        self.state.store(EXACT_RESPONSE_COMPLETE, Ordering::Release);
        if let Some(coordinator) = self.coordinator.get().and_then(Weak::upgrade) {
            coordinator.complete_exact_response_obligation(self);
        }
    }

    fn release_after_failure(&self) {
        let _ = self.state.compare_exchange(
            EXACT_RESPONSE_IN_FLIGHT,
            EXACT_RESPONSE_AVAILABLE,
            Ordering::AcqRel,
            Ordering::Acquire,
        );
    }
}

/// Cancellation-safe ownership of one exact final-response attempt.
///
/// A successful write completes the obligation. An ordinary write error may
/// release it for an explicit retry. If the send future is cancelled or
/// panics, dropping the still-armed guard releases the claim. The
/// coordinator-owned deadline registry retains the obligation and authors the
/// stack-owned fallback without requiring a task spawn from `Drop`.
pub(crate) struct ExactResponseClaim {
    obligation: Arc<ExactResponseObligation>,
    armed: bool,
}

impl ExactResponseClaim {
    pub(crate) fn complete(mut self) {
        self.obligation.complete();
        self.armed = false;
    }

    pub(crate) fn release_after_failure(mut self) {
        self.obligation.release_after_failure();
        self.armed = false;
    }
}

impl Drop for ExactResponseClaim {
    fn drop(&mut self) {
        if !self.armed {
            return;
        }
        self.obligation.release_after_failure();
    }
}

pub(crate) fn safe_incoming_method_debug_label(
    method: &rvoip_sip_core::types::Method,
) -> &'static str {
    use rvoip_sip_core::types::Method;

    match method {
        Method::Invite => "INVITE",
        Method::Ack => "ACK",
        Method::Bye => "BYE",
        Method::Cancel => "CANCEL",
        Method::Register => "REGISTER",
        Method::Options => "OPTIONS",
        Method::Subscribe => "SUBSCRIBE",
        Method::Notify => "NOTIFY",
        Method::Update => "UPDATE",
        Method::Refer => "REFER",
        Method::Info => "INFO",
        Method::Message => "MESSAGE",
        Method::Prack => "PRACK",
        Method::Publish => "PUBLISH",
        Method::Extension(_) => "extension",
    }
}

impl std::fmt::Debug for IncomingRequest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("IncomingRequest")
            .field("method", &safe_incoming_method_debug_label(&self.method))
            .field("raw_request_present", &self.request.is_some())
            .field(
                "raw_request_header_count",
                &self
                    .request
                    .as_ref()
                    .map_or(0, |request| request.headers.len()),
            )
            .field(
                "raw_request_body_bytes",
                &self
                    .request
                    .as_ref()
                    .map_or(0, |request| request.body().len()),
            )
            .field("coordinator_present", &self.coordinator.is_some())
            .field(
                "response_transaction_present",
                &self.response_transaction.is_some(),
            )
            .finish()
    }
}

impl IncomingRequest {
    // ─────────────────────────────────────────────────────────────────
    // SIP_API_DESIGN_2 Phase D — response builder entry points.
    // ─────────────────────────────────────────────────────────────────

    /// Borrow a [`SessionHandle`] for the dialog this inbound request
    /// belongs to.
    ///
    /// Returns `Err(SessionError::InvalidInput)` when the event has not
    /// supplied both the coordinator hook and exact lifecycle authority.
    /// A missing generation is never recovered from the raw Call-ID because
    /// that identifier may already belong to a later session lifetime.
    ///
    /// Use this from `on_refer_received` / `on_notify_received` trait
    /// impls when you need to drive in-dialog actions on the session
    /// (e.g. `handle.accept_refer().await`).
    pub fn session_handle(&self) -> Result<crate::api::handle::SessionHandle> {
        let coord = self.coordinator.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.session_handle() requires a coordinator hook; \
                 the bus path has not yet rehydrated it"
                    .to_string(),
            )
        })?;
        let lifecycle_handle = self.lifecycle_handle.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.session_handle() requires exact lifecycle authority".to_string(),
            )
        })?;
        Ok(crate::api::handle::SessionHandle::new_exact(
            self.call_id.clone(),
            coord,
            lifecycle_handle,
        ))
    }

    /// Begin a `GenericResponseBuilder` for the inbound request.
    /// Returns `Err(SessionError::InvalidInput)` when this
    /// `IncomingRequest` lacks the coordinator hook or exact lifecycle
    /// authority carried by its causal event.
    pub fn respond_builder(
        &self,
        status: u16,
    ) -> Result<crate::api::respond::GenericResponseBuilder> {
        if self.response_transaction.is_some() {
            let transaction_id = self.exact_response_transaction()?;
            let coord = self.coordinator.clone().ok_or_else(|| {
                SessionError::InvalidInput(
                    "IncomingRequest.respond_builder() requires a coordinator hook; the bus \
                     path has not yet rehydrated it"
                        .to_string(),
                )
            })?;
            return crate::api::respond::GenericResponseBuilder::new_in_dialog(
                coord,
                self.call_id.clone(),
                self.method.clone(),
                transaction_id,
                status,
                self.exact_response_obligation()?,
            );
        }
        if matches!(
            self.method,
            rvoip_sip_core::Method::Info
                | rvoip_sip_core::Method::Notify
                | rvoip_sip_core::Method::Update
                | rvoip_sip_core::Method::Refer
        ) {
            return Err(SessionError::InvalidInput(
                "IncomingRequest.respond_builder() requires an exact inbound transaction for this in-dialog method"
                    .to_string(),
            ));
        }
        let coord = self.coordinator.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.respond_builder() requires a coordinator hook; the bus \
                 path has not yet rehydrated it"
                    .to_string(),
            )
        })?;
        let lifecycle_handle = self.lifecycle_handle.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.respond_builder() requires exact lifecycle authority".to_string(),
            )
        })?;
        crate::api::respond::GenericResponseBuilder::new_exact(
            coord,
            self.call_id.clone(),
            lifecycle_handle,
            self.method.clone(),
            status,
        )
    }

    /// Begin a final 2xx–6xx response for this exact inbound in-dialog
    /// transaction.
    ///
    /// This does not run INVITE accept/reject call-state transitions. It is
    /// therefore the response API for inbound INFO and similar application
    /// requests. Legacy events that lack exact transaction correlation return
    /// `InvalidInput` rather than guessing from dialog state.
    pub fn respond(&self, status: u16) -> Result<crate::api::respond::InDialogResponseBuilder> {
        let transaction_id = self.exact_response_transaction()?;
        let coord = self.coordinator.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.respond() requires a coordinator hook; the bus path has not yet rehydrated it"
                    .to_string(),
            )
        })?;
        crate::api::respond::InDialogResponseBuilder::new(
            coord,
            self.call_id.clone(),
            transaction_id,
            status,
            self.exact_response_obligation()?,
        )
    }

    fn exact_response_obligation(&self) -> Result<Arc<ExactResponseObligation>> {
        self.response_obligation.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest response requires an exact response obligation".to_string(),
            )
        })
    }

    fn exact_response_transaction(&self) -> Result<rvoip_sip_dialog::transaction::TransactionKey> {
        let transaction = self.response_transaction.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest response requires an exact inbound server transaction".to_string(),
            )
        })?;
        let wire_transaction = self
            .request
            .as_ref()
            .and_then(|request| {
                rvoip_sip_dialog::transaction::TransactionKey::from_request(request)
            })
            .ok_or_else(|| {
                SessionError::InvalidInput(
                    "IncomingRequest response requires an exact transaction on the wire request"
                        .to_string(),
                )
            })?;
        if transaction != wire_transaction || transaction.method() != &self.method {
            return Err(SessionError::InvalidInput(
                "IncomingRequest response transaction does not match the wire request".to_string(),
            ));
        }
        Ok(transaction)
    }

    /// Begin an `AuthChallengeBuilder` for 401/407 on the inbound request.
    pub fn challenge_builder(
        &self,
        scheme: crate::api::respond::AuthScheme,
    ) -> Result<crate::api::respond::AuthChallengeBuilder> {
        let coord = self.coordinator.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.challenge_builder() requires a coordinator hook".to_string(),
            )
        })?;
        let lifecycle_handle = self.lifecycle_handle.clone().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.challenge_builder() requires exact lifecycle authority"
                    .to_string(),
            )
        })?;
        Ok(crate::api::respond::AuthChallengeBuilder::new_exact(
            coord,
            self.call_id.clone(),
            self.method.clone(),
            scheme,
            lifecycle_handle,
        ))
    }

    /// Authenticate this inbound request with a UAS SIP auth service.
    ///
    /// Pass [`SipAuthService`](crate::SipAuthService) for Digest, Bearer,
    /// Basic, AKA, or multi-challenge validation. Pass
    /// [`SipDigestAuthService`](crate::SipDigestAuthService) for Digest-only
    /// compatibility.
    pub async fn authenticate_with<A>(
        &self,
        auth: &A,
    ) -> Result<<A as crate::auth::SipIncomingAuthenticator>::Decision>
    where
        A: crate::auth::SipIncomingAuthenticator + Sync,
    {
        let request = self.request.as_ref().ok_or_else(|| {
            SessionError::InvalidInput(
                "IncomingRequest.authenticate_with() requires a parsed inbound request".to_string(),
            )
        })?;
        let (authorization, source) = selected_authorization(request);
        let request_uri = request.uri().to_string();
        let body = if request.body().is_empty() {
            None
        } else {
            Some(request.body())
        };
        let transport = effective_transport_security_context(Some(request), &self.transport);
        auth.authenticate_incoming_with_transport_context(
            authorization.as_deref(),
            self.method.as_str(),
            &request_uri,
            body,
            source,
            &transport,
        )
        .await
    }

    /// Rehydrate the coordinator hook so response builders can
    /// dispatch. Called by the surface consumer (CallbackPeer /
    /// StreamPeer) on every inbound event before exposing the
    /// `IncomingRequest` to application code.
    pub(crate) fn set_coordinator_captured(
        &mut self,
        coord: Arc<UnifiedCoordinator>,
        lifecycle_handle: Option<SessionRegistryHandle>,
    ) -> crate::api::unified::ExactResponseRegistration {
        if lifecycle_handle.is_some() {
            self.lifecycle_handle = lifecycle_handle;
        }
        if let Some(obligation) = &self.response_obligation {
            let Some(lifecycle_handle) = self.lifecycle_handle.as_ref() else {
                return crate::api::unified::ExactResponseRegistration::Collision;
            };
            if !obligation.bind_lifecycle_handle(lifecycle_handle) {
                return crate::api::unified::ExactResponseRegistration::Collision;
            }
            obligation.attach_coordinator(&coord);
            let registration = coord.register_exact_response_obligation(Arc::clone(obligation));
            self.coordinator = Some(coord);
            if !matches!(
                registration,
                crate::api::unified::ExactResponseRegistration::Registered
            ) {
                return registration;
            }
        } else {
            self.coordinator = Some(coord);
        }
        crate::api::unified::ExactResponseRegistration::Registered
    }

    pub(crate) fn set_response_transaction(
        &mut self,
        transaction: rvoip_sip_dialog::transaction::TransactionKey,
    ) {
        self.response_obligation = Some(Arc::new(ExactResponseObligation::new(
            self.call_id.clone(),
            transaction.clone(),
        )));
        self.response_transaction = Some(transaction);
    }

    pub(crate) fn clear_response_capability(&mut self) {
        self.response_transaction = None;
        self.response_obligation = None;
        self.coordinator = None;
        self.lifecycle_handle = None;
    }

    /// SIP_API_DESIGN_2 Phase E — construct an `IncomingRequest` from
    /// re-parsed bus bytes. Coordinator is `None` until the surface
    /// consumer (CallbackPeer / StreamPeer) rehydrates it via
    /// `set_coordinator` on dispatch.
    pub(crate) fn from_bus_request(
        call_id: CallId,
        from: String,
        to: String,
        method: rvoip_sip_core::types::Method,
        request: Arc<Request>,
    ) -> Self {
        Self {
            call_id,
            from,
            to,
            method,
            received_at: Instant::now(),
            request: Some(request),
            transport: crate::auth::SipTransportSecurityContext::unknown(),
            response_transaction: None,
            response_obligation: None,
            coordinator: None,
            lifecycle_handle: None,
        }
    }

    pub(crate) fn with_transport_context(
        mut self,
        transport: crate::auth::SipTransportSecurityContext,
    ) -> Self {
        self.transport = transport;
        self
    }

    /// Transport context used by auth policy decisions.
    pub fn transport_security_context(&self) -> &crate::auth::SipTransportSecurityContext {
        &self.transport
    }

    /// Underlying parsed [`Request`]. `None` when synthesized.
    pub fn raw_request(&self) -> Option<&Arc<Request>> {
        self.request.as_ref()
    }

    /// Zero-alloc header iteration — preferred over the boxed trait
    /// method on hot paths.
    pub fn headers_named_iter<'a>(
        &'a self,
        name: &'a HeaderName,
    ) -> impl Iterator<Item = &'a TypedHeader> + 'a {
        let slice: &[TypedHeader] = match &self.request {
            Some(r) => &r.headers[..],
            None => &[],
        };
        headers_named_slice(slice, name)
    }
}

impl SipHeaderView for IncomingRequest {
    fn header(&self, name: &HeaderName) -> Option<&TypedHeader> {
        self.request
            .as_ref()
            .and_then(|r| header_slice(&r.headers[..], name))
    }
    fn headers_named<'a>(
        &'a self,
        name: &HeaderName,
    ) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.request {
            Some(r) => {
                let name = name.clone();
                Box::new(
                    r.headers.iter().filter(move |h| {
                        crate::api::headers::view::header_name_eq(&h.name(), &name)
                    }),
                )
            }
            None => Box::new(std::iter::empty()),
        }
    }
    fn headers<'a>(&'a self) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.request {
            Some(r) => Box::new(r.headers.iter()),
            None => Box::new(std::iter::empty()),
        }
    }
    fn header_names(&self) -> Vec<HeaderName> {
        match &self.request {
            Some(r) => header_names_slice(&r.headers[..]),
            None => Vec::new(),
        }
    }
}

/// An inbound SIP response — 1xx provisional, 2xx success, 3xx
/// redirect, 4xx-6xx final. Carries the parsed [`Response`] when
/// available; used by B2BUA flows for downstream carry-through of
/// `Allow` / `Supported` / `Server` / `Session-Expires`, redirect
/// handling, and final-failure inspection (`Retry-After`, `Warning`,
/// `Reason`).
#[derive(Clone)]
pub struct IncomingResponse {
    /// The session the response belongs to.
    pub call_id: CallId,
    /// SIP status code (100, 180, 200, 302, 404, …).
    pub status_code: u16,
    /// Reason phrase from the status line.
    pub reason_phrase: String,
    /// SDP body on the response, if any.
    pub sdp: Option<String>,
    /// When the response arrived.
    pub received_at: Instant,
    /// The parsed inbound response, when available.
    pub(crate) response: Option<Arc<Response>>,
}

impl std::fmt::Debug for IncomingResponse {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("IncomingResponse")
            .field("status_code", &self.status_code)
            .field("reason_phrase_bytes", &self.reason_phrase.len())
            .field("sdp_present", &self.sdp.is_some())
            .field("sdp_bytes", &self.sdp.as_ref().map_or(0, String::len))
            .field("raw_response_present", &self.response.is_some())
            .field(
                "raw_response_header_count",
                &self
                    .response
                    .as_ref()
                    .map_or(0, |response| response.headers.len()),
            )
            .field(
                "raw_response_body_bytes",
                &self
                    .response
                    .as_ref()
                    .map_or(0, |response| response.body().len()),
            )
            .finish()
    }
}

impl IncomingResponse {
    /// Synthesize an `IncomingResponse` without a parsed body. Used by
    /// the legacy bus path until Phase A's bus enrichment fully lands.
    pub(crate) fn synthetic(
        call_id: CallId,
        status_code: u16,
        reason_phrase: String,
        sdp: Option<String>,
    ) -> Self {
        Self {
            call_id,
            status_code,
            reason_phrase,
            sdp,
            received_at: Instant::now(),
            response: None,
        }
    }

    /// Construct an `IncomingResponse` carrying a parsed response.
    pub(crate) fn with_response(
        call_id: CallId,
        status_code: u16,
        reason_phrase: String,
        sdp: Option<String>,
        response: Arc<Response>,
    ) -> Self {
        Self {
            call_id,
            status_code,
            reason_phrase,
            sdp,
            received_at: Instant::now(),
            response: Some(response),
        }
    }

    /// Underlying parsed [`Response`]. `None` until Phase A's bus
    /// enrichment is wired up for this code path.
    pub fn raw_response(&self) -> Option<&Arc<Response>> {
        self.response.as_ref()
    }

    /// Zero-alloc header iteration.
    pub fn headers_named_iter<'a>(
        &'a self,
        name: &'a HeaderName,
    ) -> impl Iterator<Item = &'a TypedHeader> + 'a {
        let slice: &[TypedHeader] = match &self.response {
            Some(r) => &r.headers[..],
            None => &[],
        };
        headers_named_slice(slice, name)
    }

    /// True when this is a 1xx response.
    pub fn is_provisional(&self) -> bool {
        (100..200).contains(&self.status_code)
    }

    /// True when this carries an RFC 3262 reliable provisional marker
    /// (Require: 100rel + RSeq). Inspects the parsed response when
    /// available; falls back to `false` when synthesized.
    pub fn is_reliable_provisional(&self) -> bool {
        if !self.is_provisional() {
            return false;
        }
        let Some(resp) = &self.response else {
            return false;
        };
        let mut has_require_100rel = false;
        let mut has_rseq = false;
        for h in &resp.headers {
            match h {
                TypedHeader::Require(req)
                    if req
                        .option_tags
                        .iter()
                        .any(|s| s.eq_ignore_ascii_case("100rel")) =>
                {
                    has_require_100rel = true;
                }
                TypedHeader::RSeq(_) => has_rseq = true,
                _ => {}
            }
        }
        has_require_100rel && has_rseq
    }
}

impl SipHeaderView for IncomingResponse {
    fn header(&self, name: &HeaderName) -> Option<&TypedHeader> {
        self.response
            .as_ref()
            .and_then(|r| header_slice(&r.headers[..], name))
    }
    fn headers_named<'a>(
        &'a self,
        name: &HeaderName,
    ) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.response {
            Some(r) => {
                let name = name.clone();
                Box::new(
                    r.headers.iter().filter(move |h| {
                        crate::api::headers::view::header_name_eq(&h.name(), &name)
                    }),
                )
            }
            None => Box::new(std::iter::empty()),
        }
    }
    fn headers<'a>(&'a self) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.response {
            Some(r) => Box::new(r.headers.iter()),
            None => Box::new(std::iter::empty()),
        }
    }
    fn header_names(&self) -> Vec<HeaderName> {
        match &self.response {
            Some(r) => header_names_slice(&r.headers[..]),
            None => Vec::new(),
        }
    }
}

/// Inbound SIP REGISTER on registrar surfaces.
///
/// Wraps the parsed REGISTER request and exposes it through
/// [`SipHeaderView`] plus dedicated convenience accessors for AOR /
/// Contact / Expires that registrar code reaches for first.
#[derive(Clone)]
pub struct IncomingRegister {
    // Manual `Debug` impl below skips the `coordinator` field since
    // `UnifiedCoordinator` does not derive `Debug`.
    /// Transaction key for the inbound REGISTER (used for response
    /// authoring).
    pub transaction_id: String,
    /// `From` URI (the registering AOR).
    pub from_uri: String,
    /// `To` URI (the registrar / target AOR).
    pub to_uri: String,
    /// First `Contact` URI on the wire. Multi-Contact lookups go
    /// through [`SipHeaderView`].
    pub contact_uri: String,
    /// Requested `Expires` (header or Contact-param). 0 means unregister.
    pub expires: u32,
    /// Raw `Authorization:` header value if present.
    pub authorization: Option<String>,
    /// `Call-ID` from the request.
    pub call_id_header: String,
    /// When the REGISTER arrived.
    pub received_at: Instant,
    /// The parsed inbound REGISTER request, when available.
    pub(crate) request: Option<Arc<Request>>,
    /// Transport context for auth policy decisions.
    pub(crate) transport: crate::auth::SipTransportSecurityContext,
    /// Optional hook back into the coordinator so
    /// `RegisterResponseBuilder.send()` can answer the exact inbound
    /// REGISTER transaction. `None` when the wrapper was synthesized for
    /// tests or by an external registrar that authors responses directly.
    pub(crate) coordinator: Option<Arc<UnifiedCoordinator>>,
    /// Single-writer deadline owner for an application-authored REGISTER
    /// response. Only the private causal control copy retains it.
    pub(crate) response_obligation: Option<Arc<ExactResponseObligation>>,
    /// Whether an owning API receiver may attach response authority. Public
    /// observational projections clear this bit so cloning or redispatching
    /// an observation cannot recreate a second REGISTER response owner.
    response_capability: bool,
    /// Marks the stripped public copy emitted beside a private control
    /// delivery. The owning receiver suppresses only this copy; ordinary
    /// built-in-registrar observations remain visible to applications.
    control_observation: bool,
}

impl std::fmt::Debug for IncomingRegister {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("IncomingRegister")
            .field("expires", &self.expires)
            .field("authorization_present", &self.authorization.is_some())
            .field(
                "authorization_bytes",
                &self.authorization.as_ref().map_or(0, String::len),
            )
            .field("raw_request_present", &self.request.is_some())
            .field(
                "raw_request_header_count",
                &self
                    .request
                    .as_ref()
                    .map_or(0, |request| request.headers.len()),
            )
            .field(
                "raw_request_body_bytes",
                &self
                    .request
                    .as_ref()
                    .map_or(0, |request| request.body().len()),
            )
            .field("coordinator_present", &self.coordinator.is_some())
            .finish()
    }
}

impl IncomingRegister {
    // ─────────────────────────────────────────────────────────────────
    // SIP_API_DESIGN_2 Phase D — registrar response builder entries.
    // ─────────────────────────────────────────────────────────────────

    /// Begin a `RegisterResponseBuilder` for the inbound REGISTER.
    pub fn accept_builder(&self) -> crate::api::respond::RegisterResponseBuilder {
        crate::api::respond::RegisterResponseBuilder::new(
            self.transaction_id.clone(),
            self.coordinator.clone(),
        )
        .with_response_obligation(self.response_obligation.clone())
    }

    /// Begin a 401 / 407 auth challenge for the inbound REGISTER.
    /// The matching dispatch uses the same exact transaction path as
    /// `accept_builder`, so a registrar can author both 200 OK and 401
    /// through one consistent surface.
    pub fn challenge_builder(
        &self,
        scheme: crate::api::respond::AuthScheme,
    ) -> crate::api::respond::RegisterResponseBuilder {
        crate::api::respond::RegisterResponseBuilder::new_challenge(
            self.transaction_id.clone(),
            self.coordinator.clone(),
            scheme,
        )
        .with_response_obligation(self.response_obligation.clone())
    }

    /// Authenticate this inbound REGISTER with a UAS SIP auth service.
    ///
    /// Pass [`SipAuthService`](crate::SipAuthService) for Digest, Bearer,
    /// Basic, AKA, or multi-challenge validation. Pass
    /// [`SipDigestAuthService`](crate::SipDigestAuthService) for Digest-only
    /// compatibility.
    pub async fn authenticate_with<A>(
        &self,
        auth: &A,
    ) -> Result<<A as crate::auth::SipIncomingAuthenticator>::Decision>
    where
        A: crate::auth::SipIncomingAuthenticator + Sync,
    {
        let request_uri = self
            .request
            .as_ref()
            .map(|request| request.uri().to_string())
            .unwrap_or_else(|| self.to_uri.clone());
        let body = self
            .request
            .as_ref()
            .and_then(|request| (!request.body().is_empty()).then(|| request.body()));
        let (authorization, source) = self
            .request
            .as_ref()
            .map(|request| selected_authorization(request))
            .unwrap_or_else(|| {
                (
                    self.authorization.clone(),
                    crate::auth::SipAuthSource::Origin,
                )
            });
        let transport =
            effective_transport_security_context(self.request.as_deref(), &self.transport);
        auth.authenticate_incoming_with_transport_context(
            authorization.as_deref(),
            "REGISTER",
            &request_uri,
            body,
            source,
            &transport,
        )
        .await
    }

    /// Begin a generic non-2xx REGISTER response (404, 423 Interval
    /// Too Brief with `Min-Expires`, 503 Service Unavailable, …).
    pub fn reject_builder(&self, status: u16) -> crate::api::respond::RegisterResponseBuilder {
        crate::api::respond::RegisterResponseBuilder::new_reject(
            self.transaction_id.clone(),
            self.coordinator.clone(),
            status,
        )
        .with_response_obligation(self.response_obligation.clone())
    }

    /// Synthesize without a parsed request. Used by the legacy bus
    /// path until enrichment fully lands.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn synthetic(
        transaction_id: String,
        from_uri: String,
        to_uri: String,
        contact_uri: String,
        expires: u32,
        authorization: Option<String>,
        call_id_header: String,
    ) -> Self {
        Self {
            transaction_id,
            from_uri,
            to_uri,
            contact_uri,
            expires,
            authorization,
            call_id_header,
            received_at: Instant::now(),
            request: None,
            transport: crate::auth::SipTransportSecurityContext::unknown(),
            coordinator: None,
            response_obligation: None,
            response_capability: true,
            control_observation: false,
        }
    }

    /// Construct from a parsed inbound REGISTER request.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn with_request(
        transaction_id: String,
        from_uri: String,
        to_uri: String,
        contact_uri: String,
        expires: u32,
        authorization: Option<String>,
        call_id_header: String,
        request: Arc<Request>,
    ) -> Self {
        Self {
            transaction_id,
            from_uri,
            to_uri,
            contact_uri,
            expires,
            authorization,
            call_id_header,
            received_at: Instant::now(),
            request: Some(request),
            transport: crate::auth::SipTransportSecurityContext::unknown(),
            coordinator: None,
            response_obligation: None,
            response_capability: true,
            control_observation: false,
        }
    }

    /// Same as [`with_request`] but threads the coordinator handle so the
    /// response builder can answer through rvoip-sip-dialog's exact server
    /// transaction.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn with_request_and_coordinator(
        transaction_id: String,
        from_uri: String,
        to_uri: String,
        contact_uri: String,
        expires: u32,
        authorization: Option<String>,
        call_id_header: String,
        request: Arc<Request>,
        coordinator: Arc<UnifiedCoordinator>,
    ) -> Self {
        Self {
            transaction_id,
            from_uri,
            to_uri,
            contact_uri,
            expires,
            authorization,
            call_id_header,
            received_at: Instant::now(),
            request: Some(request),
            transport: crate::auth::SipTransportSecurityContext::unknown(),
            coordinator: Some(coordinator),
            response_obligation: None,
            response_capability: true,
            control_observation: false,
        }
    }

    pub(crate) fn with_transport_context(
        mut self,
        transport: crate::auth::SipTransportSecurityContext,
    ) -> Self {
        self.transport = transport;
        self
    }

    /// Transport context used by auth policy decisions.
    pub fn transport_security_context(&self) -> &crate::auth::SipTransportSecurityContext {
        &self.transport
    }

    /// Underlying parsed [`Request`].
    pub fn raw_request(&self) -> Option<&Arc<Request>> {
        self.request.as_ref()
    }

    /// Zero-alloc header iteration.
    pub fn headers_named_iter<'a>(
        &'a self,
        name: &'a HeaderName,
    ) -> impl Iterator<Item = &'a TypedHeader> + 'a {
        let slice: &[TypedHeader] = match &self.request {
            Some(r) => &r.headers[..],
            None => &[],
        };
        headers_named_slice(slice, name)
    }

    /// Set the coordinator hook so `accept_builder()` /
    /// `challenge_builder()` / `reject_builder()` can dispatch through the
    /// exact inbound REGISTER transaction. Called immediately before the
    /// `IncomingRegister` reaches the application handler.
    pub fn set_coordinator(&mut self, coordinator: Arc<UnifiedCoordinator>) {
        if self.response_capability {
            self.coordinator = Some(coordinator);
        }
    }

    /// Install the standalone exact-transaction owner before the causal
    /// delivery is positively acknowledged. Its managed deadline authors 503
    /// if an accepted application handler never claims a response builder.
    pub(crate) fn install_response_obligation(
        &mut self,
        coordinator: Arc<UnifiedCoordinator>,
    ) -> Result<crate::api::unified::ExactResponseRegistration> {
        if !self.response_capability {
            return Err(SessionError::InvalidInput(
                "observational REGISTER cannot acquire response authority".to_string(),
            ));
        }
        let transaction = self
            .transaction_id
            .parse::<rvoip_sip_dialog::transaction::TransactionKey>()
            .map_err(|_| {
                SessionError::InvalidInput(
                    "IncomingRegister has an invalid exact transaction identifier".to_string(),
                )
            })?;
        if !transaction.is_server()
            || transaction.method() != &rvoip_sip_core::types::Method::Register
        {
            return Err(SessionError::InvalidInput(
                "IncomingRegister response authority requires a server REGISTER transaction"
                    .to_string(),
            ));
        }
        if let Some(request) = self.request.as_ref() {
            let wire_transaction = rvoip_sip_dialog::transaction::TransactionKey::from_request(
                request,
            )
            .ok_or_else(|| {
                SessionError::InvalidInput(
                    "IncomingRegister wire request has no exact transaction".to_string(),
                )
            })?;
            if wire_transaction != transaction {
                return Err(SessionError::InvalidInput(
                    "IncomingRegister event transaction does not match the wire request"
                        .to_string(),
                ));
            }
        }

        let obligation = Arc::new(ExactResponseObligation::new_standalone(transaction, 503));
        obligation.attach_coordinator(&coordinator);
        let registration = coordinator.register_exact_response_obligation(Arc::clone(&obligation));
        if matches!(
            registration,
            crate::api::unified::ExactResponseRegistration::Registered
        ) {
            self.coordinator = Some(coordinator);
            self.response_obligation = Some(obligation);
        }
        Ok(registration)
    }

    /// Permanently strip response authority from an observational copy.
    pub(crate) fn clear_response_capability(&mut self) {
        self.coordinator = None;
        self.response_obligation = None;
        self.response_capability = false;
    }

    /// Identify the stripped observation paired with a private control event.
    pub(crate) fn mark_control_observation(&mut self) {
        self.clear_response_capability();
        self.control_observation = true;
    }

    #[cfg(test)]
    pub(crate) fn is_control_observation(&self) -> bool {
        self.control_observation
    }
}

impl SipHeaderView for IncomingRegister {
    fn header(&self, name: &HeaderName) -> Option<&TypedHeader> {
        self.request
            .as_ref()
            .and_then(|r| header_slice(&r.headers[..], name))
    }
    fn headers_named<'a>(
        &'a self,
        name: &HeaderName,
    ) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.request {
            Some(r) => {
                let name = name.clone();
                Box::new(
                    r.headers.iter().filter(move |h| {
                        crate::api::headers::view::header_name_eq(&h.name(), &name)
                    }),
                )
            }
            None => Box::new(std::iter::empty()),
        }
    }
    fn headers<'a>(&'a self) -> Box<dyn Iterator<Item = &'a TypedHeader> + 'a> {
        match &self.request {
            Some(r) => Box::new(r.headers.iter()),
            None => Box::new(std::iter::empty()),
        }
    }
    fn header_names(&self) -> Vec<HeaderName> {
        match &self.request {
            Some(r) => header_names_slice(&r.headers[..]),
            None => Vec::new(),
        }
    }
}

fn selected_authorization(request: &Request) -> (Option<String>, crate::auth::SipAuthSource) {
    if let Some(value) = request.raw_header_value(&HeaderName::Authorization) {
        return (Some(value), crate::auth::SipAuthSource::Origin);
    }
    if let Some(value) = request.raw_header_value(&HeaderName::ProxyAuthorization) {
        return (Some(value), crate::auth::SipAuthSource::Proxy);
    }
    (None, crate::auth::SipAuthSource::Origin)
}

fn request_transport_security_context(
    request: &Request,
) -> crate::auth::SipTransportSecurityContext {
    crate::auth::SipTransportSecurityContext::from_request_uri_hint(&request.uri().to_string())
}

fn effective_transport_security_context(
    request: Option<&Request>,
    transport: &crate::auth::SipTransportSecurityContext,
) -> crate::auth::SipTransportSecurityContext {
    if transport.secure
        || transport.transport.is_some()
        || transport.local_addr.is_some()
        || transport.remote_addr.is_some()
    {
        transport.clone()
    } else {
        request
            .map(request_transport_security_context)
            .unwrap_or_default()
    }
}

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

    #[test]
    fn legacy_header_keys_use_canonical_wire_spelling_without_debug() {
        assert_eq!(legacy_header_key(&HeaderName::CallId), "call-id");
        assert_eq!(
            legacy_header_key(&HeaderName::Other("i".into())),
            "call-id",
            "compact standard names must expand before entering the legacy map"
        );
        assert_eq!(
            legacy_header_key(&HeaderName::Other("X-Vendor-Trace".into())),
            "x-vendor-trace"
        );

        let debug = format!("{:?}", HeaderName::CallId);
        assert_ne!(
            legacy_header_key(&HeaderName::CallId),
            debug.to_ascii_lowercase()
        );
    }
    use crate::api::unified::Config;
    use base64::{engine::general_purpose::STANDARD as BASE64_STANDARD, Engine as _};

    async fn publish_synthetic(coordinator: &UnifiedCoordinator, event: Event) {
        coordinator
            .publish_app_event_for_test(event)
            .await
            .expect("publish synthetic event");
    }

    #[test]
    fn incoming_wrapper_debug_never_exposes_signaling_payloads() {
        const SECRET: &str = "incoming-wrapper-secret-canary";
        const METHOD: &str = "X-INCOMING-METHOD-SECRET-CANARY";
        let request = Request::new(
            rvoip_sip_core::types::Method::Refer,
            rvoip_sip_core::types::Uri::sip("incoming-wrapper-secret-canary.example"),
        )
        .with_header(TypedHeader::Other(
            HeaderName::Authorization,
            rvoip_sip_core::types::headers::HeaderValue::Raw(SECRET.as_bytes().to_vec()),
        ))
        .with_body(SECRET);
        let incoming_request = IncomingRequest::from_bus_request(
            crate::state_table::types::SessionId(SECRET.into()),
            SECRET.into(),
            SECRET.into(),
            rvoip_sip_core::types::Method::Extension(METHOD.into()),
            Arc::new(request),
        );
        let incoming_response = IncomingResponse::synthetic(
            crate::state_table::types::SessionId(SECRET.into()),
            401,
            SECRET.into(),
            Some(SECRET.into()),
        );
        let incoming_register = IncomingRegister::synthetic(
            SECRET.into(),
            SECRET.into(),
            SECRET.into(),
            SECRET.into(),
            300,
            Some(SECRET.into()),
            SECRET.into(),
        );

        for rendered in [
            format!("{incoming_request:?}"),
            format!("{incoming_response:?}"),
            format!("{incoming_register:?}"),
        ] {
            assert!(
                !rendered.contains(SECRET),
                "debug leaked secret: {rendered}"
            );
            assert!(
                !rendered.contains(METHOD),
                "debug leaked method: {rendered}"
            );
        }
    }

    #[test]
    fn register_observation_permanently_strips_response_authority() {
        let mut incoming = IncomingRegister::synthetic(
            "z9hG4bK-register-observation:REGISTER:server".into(),
            "sip:alice@example.test".into(),
            "sip:alice@example.test".into(),
            "sip:alice@192.0.2.10".into(),
            300,
            None,
            "register-observation@example.test".into(),
        );
        incoming.response_obligation = Some(Arc::new(ExactResponseObligation::new_standalone(
            rvoip_sip_dialog::transaction::TransactionKey::new(
                "z9hG4bK-register-observation".into(),
                rvoip_sip_core::Method::Register,
                true,
            ),
            503,
        )));

        incoming.mark_control_observation();
        assert!(incoming.response_obligation.is_none());
        assert!(!incoming.response_capability);
        assert!(incoming.is_control_observation());
    }

    #[test]
    fn incoming_response_rejects_wrong_branch_before_dispatch() {
        let raw = b"INFO sip:bob@example.test SIP/2.0\r\n\
                    Via: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bK-wire-branch\r\n\
                    From: <sip:alice@example.test>;tag=from-tag\r\n\
                    To: <sip:bob@example.test>;tag=to-tag\r\n\
                    Call-ID: response-correlation\r\n\
                    CSeq: 2 INFO\r\n\
                    Content-Length: 0\r\n\r\n";
        let request = match rvoip_sip_core::parse_message(raw).expect("parse INFO") {
            rvoip_sip_core::Message::Request(request) => request,
            other => panic!("expected request, got {other:?}"),
        };
        let mut incoming = IncomingRequest::from_bus_request(
            crate::state_table::types::SessionId("response-correlation".into()),
            "sip:alice@example.test".into(),
            "sip:bob@example.test".into(),
            rvoip_sip_core::types::Method::Info,
            Arc::new(request),
        );
        incoming.set_response_transaction(rvoip_sip_dialog::transaction::TransactionKey::new(
            "z9hG4bK-wrong-branch".into(),
            rvoip_sip_core::types::Method::Info,
            true,
        ));

        assert!(matches!(
            incoming.respond(200),
            Err(SessionError::InvalidInput(message))
                if message.contains("does not match the wire request")
        ));
    }

    #[test]
    fn cancelled_exact_response_claim_returns_to_retryable_state() {
        let obligation = Arc::new(ExactResponseObligation::new(
            CallId::new(),
            rvoip_sip_dialog::transaction::TransactionKey::new(
                "z9hG4bK-cancelled-exact-response".into(),
                rvoip_sip_core::types::Method::Info,
                true,
            ),
        ));

        let cancelled = obligation.claim().expect("first exact response claim");
        drop(cancelled);
        let retry = obligation
            .claim()
            .expect("cancelled exact response claim was released by RAII");
        retry.release_after_failure();
    }

    #[test]
    fn incoming_source_keeps_request_response_and_register_on_manual_debug() {
        let source = include_str!("incoming.rs");
        for declaration in [
            "pub struct IncomingRequest",
            "pub struct IncomingResponse",
            "pub struct IncomingRegister",
        ] {
            let declaration_offset = source
                .find(declaration)
                .unwrap_or_else(|| panic!("missing declaration {declaration}"));
            let prefix = &source[..declaration_offset];
            let derive_offset = prefix
                .rfind("#[derive(")
                .unwrap_or_else(|| panic!("missing derive for {declaration}"));
            assert!(
                !prefix[derive_offset..].contains("Debug"),
                "{declaration} regained derived Debug"
            );
        }
    }

    #[tokio::test]
    async fn incoming_request_auth_uses_transport_context_over_uri_hint() {
        let token = BASE64_STANDARD.encode("alice:secret");
        let raw = format!(
            "OPTIONS sip:bob@example.test SIP/2.0\r\n\
             Via: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bK-test\r\n\
             From: <sip:alice@example.test>;tag=from-tag\r\n\
             To: <sip:bob@example.test>\r\n\
             Call-ID: incoming-auth-context\r\n\
             CSeq: 1 OPTIONS\r\n\
             Authorization: Basic {token}\r\n\
             Content-Length: 0\r\n\r\n"
        );
        let request = match rvoip_sip_core::parse_message(raw.as_bytes()).expect("parse request") {
            rvoip_sip_core::Message::Request(request) => request,
            other => panic!("expected request, got {other:?}"),
        };
        let incoming = IncomingRequest::from_bus_request(
            crate::state_table::types::SessionId("incoming-auth".to_string()),
            "sip:alice@example.test".to_string(),
            "sip:bob@example.test".to_string(),
            rvoip_sip_core::types::Method::Options,
            Arc::new(request),
        )
        .with_transport_context(
            crate::auth::SipTransportSecurityContext::from_transport_name("WSS"),
        );

        let mut service = crate::auth::SipAuthService::new().with_basic_realm("legacy");
        service.add_basic_user("alice", "secret");

        let decision = incoming
            .authenticate_with(&service)
            .await
            .expect("incoming auth");

        assert!(matches!(
            decision,
            crate::auth::SipAuthDecision::Authorized(crate::auth::AuthIdentity {
                scheme: crate::auth::SipAuthScheme::Basic,
                ..
            })
        ));
    }

    #[tokio::test]
    async fn deferred_guard_reject_marks_shared_resolution_before_watchdog() {
        let coordinator = UnifiedCoordinator::new(Config::local("guard-test", 35990))
            .await
            .expect("coordinator starts");
        let guard = IncomingCallGuard::new(CallId::new(), coordinator, Duration::from_millis(25));
        let resolved = guard.resolved.clone();

        guard.reject(486, "Busy Here");
        tokio::time::sleep(Duration::from_millis(50)).await;

        assert!(
            resolved.load(Ordering::SeqCst),
            "explicit reject should resolve the guard before the watchdog fires"
        );
    }

    #[tokio::test]
    async fn deferred_guard_accept_attempt_marks_shared_resolution_before_watchdog() {
        let coordinator = UnifiedCoordinator::new(Config::local("guard-test", 35991))
            .await
            .expect("coordinator starts");
        let guard = IncomingCallGuard::new(CallId::new(), coordinator, Duration::from_millis(25));
        let resolved = guard.resolved.clone();

        let result = guard.accept().await;
        assert!(
            result.is_err(),
            "fake guard has no backing session, so accept should surface that error"
        );

        tokio::time::sleep(Duration::from_millis(50)).await;

        assert!(
            resolved.load(Ordering::SeqCst),
            "accept should resolve the guard before the watchdog can auto-reject"
        );
    }

    #[tokio::test]
    async fn deferred_guard_watchdog_is_cancelled_and_cannot_cross_raw_id_reuse() {
        let coordinator = UnifiedCoordinator::new(Config::local("guard-test", 35989))
            .await
            .expect("coordinator starts");
        let call_id = CallId::from("deferred-guard-reuse");
        coordinator
            .helpers
            .create_session(
                call_id.clone(),
                "sip:callee@example.test".to_string(),
                "sip:caller@example.test".to_string(),
                crate::state_table::types::Role::UAS,
            )
            .await
            .expect("create original exact incoming lifetime");
        let store = Arc::clone(&coordinator.helpers.state_machine.store);
        let original = store
            .lifecycle_handle(&call_id)
            .expect("original exact registry handle");
        store
            .update_session_exact_with(&original, None, |session| {
                session.call_state = CallState::Ringing;
            })
            .expect("mark original call ringing");

        let guard = IncomingCallGuard::new(
            call_id.clone(),
            Arc::clone(&coordinator),
            Duration::from_millis(200),
        );
        let resolved = Arc::clone(&guard.resolved);

        store
            .remove_session_exact(&original)
            .await
            .expect("teardown cancels and joins the exact watchdog");
        assert!(
            !resolved.load(Ordering::SeqCst),
            "lifecycle cancellation must not resolve the application guard"
        );
        assert!(
            store.authority().elapse_reuse_horizon_for_test(&call_id),
            "expire the production anti-reuse fence through the test clock seam"
        );

        coordinator
            .helpers
            .create_session(
                call_id.clone(),
                "sip:replacement@example.test".to_string(),
                "sip:caller@example.test".to_string(),
                crate::state_table::types::Role::UAS,
            )
            .await
            .expect("reuse the raw identifier for a later exact lifetime");
        let replacement = store
            .lifecycle_handle(&call_id)
            .expect("replacement exact registry handle");
        assert_ne!(original, replacement);
        store
            .update_session_exact_with(&replacement, None, |session| {
                session.call_state = CallState::Ringing;
            })
            .expect("mark replacement call ringing");

        tokio::time::sleep(Duration::from_millis(250)).await;
        assert!(
            !resolved.load(Ordering::SeqCst),
            "the cancelled watchdog must stay inert after its old deadline"
        );
        assert_eq!(
            store
                .get_session_exact(&replacement)
                .await
                .expect("replacement remains live")
                .call_state,
            CallState::Ringing,
            "the old watchdog must not reject the reused identifier"
        );

        guard.abandon();
        coordinator.shutdown();
    }

    #[tokio::test]
    async fn retired_incoming_objects_cannot_resolve_reused_generation() {
        let coordinator = UnifiedCoordinator::new(Config::local("incoming-reuse-test", 35988))
            .await
            .expect("coordinator starts");
        let call_id = CallId::from("incoming-object-reuse");
        coordinator
            .helpers
            .create_session(
                call_id.clone(),
                "sip:callee@example.test".to_string(),
                "sip:caller@example.test".to_string(),
                crate::state_table::types::Role::UAS,
            )
            .await
            .expect("create generation A");
        let store = Arc::clone(&coordinator.helpers.state_machine.store);
        let generation_a = store
            .lifecycle_handle(&call_id)
            .expect("generation A exact handle");
        store
            .update_session_exact_with(&generation_a, None, |session| {
                session.call_state = CallState::Ringing;
            })
            .expect("mark generation A ringing");

        let make_incoming = || {
            IncomingCall::new_exact(
                call_id.clone(),
                "sip:caller@example.test".to_string(),
                "sip:callee@example.test".to_string(),
                None,
                Arc::clone(&coordinator),
                generation_a.clone(),
            )
        };
        let accept_a = make_incoming();
        let redirect_a = make_incoming();
        let reject_a = make_incoming();
        let panic_drop_a = make_incoming();
        let accept_guard_a = make_incoming().defer(Duration::from_secs(5));
        let reject_guard_a = make_incoming().defer(Duration::from_secs(5));

        store
            .remove_session_exact(&generation_a)
            .await
            .expect("retire generation A and cancel its deferred watchdogs");
        assert!(
            store.authority().elapse_reuse_horizon_for_test(&call_id),
            "expire anti-reuse horizon"
        );
        coordinator
            .helpers
            .create_session(
                call_id.clone(),
                "sip:replacement@example.test".to_string(),
                "sip:caller@example.test".to_string(),
                crate::state_table::types::Role::UAS,
            )
            .await
            .expect("create generation B");
        let generation_b = store
            .lifecycle_handle(&call_id)
            .expect("generation B exact handle");
        assert_ne!(generation_a, generation_b);
        store
            .update_session_exact_with(&generation_b, None, |session| {
                session.call_state = CallState::Ringing;
            })
            .expect("mark generation B ringing");

        assert!(accept_a.accept().await.is_err());
        assert!(redirect_a
            .redirect_to("sip:elsewhere@example.test")
            .await
            .is_err());
        assert!(accept_guard_a.accept().await.is_err());
        reject_a.reject(486, "Busy Here");
        reject_guard_a.reject(503, "Service Unavailable");
        let panic_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _owned = panic_drop_a;
            panic!("exercise IncomingCall panic-drop safety net");
        }));
        assert!(panic_result.is_err());

        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!(
            store
                .get_session_exact(&generation_b)
                .await
                .expect("generation B remains live")
                .call_state,
            CallState::Ringing,
            "generation-A accept/reject/redirect/guard/drop work must not mutate generation B"
        );

        coordinator.shutdown();
    }

    #[tokio::test]
    async fn retired_incoming_request_cannot_resolve_or_reject_reused_generation() {
        let coordinator =
            UnifiedCoordinator::new(Config::local("incoming-request-reuse-test", 35987))
                .await
                .expect("coordinator starts");
        let call_id = CallId::from("incoming-request-reuse");
        coordinator
            .helpers
            .create_session(
                call_id.clone(),
                "sip:callee@example.test".to_string(),
                "sip:caller@example.test".to_string(),
                crate::state_table::types::Role::UAS,
            )
            .await
            .expect("create generation A");
        let store = Arc::clone(&coordinator.helpers.state_machine.store);
        let generation_a = store
            .lifecycle_handle(&call_id)
            .expect("generation A exact handle");
        store
            .update_session_exact_with(&generation_a, None, |session| {
                session.call_state = CallState::Ringing;
            })
            .expect("mark generation A ringing");

        let request = Request::new(
            rvoip_sip_core::types::Method::Options,
            rvoip_sip_core::types::Uri::sip("callee.example.test"),
        );
        let mut incoming = IncomingRequest::from_bus_request(
            call_id.clone(),
            "sip:caller@example.test".to_string(),
            "sip:callee@example.test".to_string(),
            rvoip_sip_core::types::Method::Options,
            Arc::new(request),
        );
        assert!(matches!(
            incoming.set_coordinator_captured(Arc::clone(&coordinator), Some(generation_a.clone())),
            crate::api::unified::ExactResponseRegistration::Registered
        ));

        store
            .remove_session_exact(&generation_a)
            .await
            .expect("retire generation A");
        assert!(
            store.authority().elapse_reuse_horizon_for_test(&call_id),
            "expire anti-reuse horizon"
        );
        coordinator
            .helpers
            .create_session(
                call_id.clone(),
                "sip:replacement@example.test".to_string(),
                "sip:caller@example.test".to_string(),
                crate::state_table::types::Role::UAS,
            )
            .await
            .expect("create generation B");
        let generation_b = store
            .lifecycle_handle(&call_id)
            .expect("generation B exact handle");
        assert_ne!(generation_a, generation_b);
        store
            .update_session_exact_with(&generation_b, None, |session| {
                session.call_state = CallState::Ringing;
            })
            .expect("mark generation B ringing");

        assert!(
            incoming
                .session_handle()
                .expect("request retains generation A authority")
                .state()
                .await
                .is_err(),
            "the request-derived session handle must not re-resolve generation B"
        );
        assert!(
            incoming
                .respond_builder(486)
                .expect("exact generic response builder")
                .send()
                .await
                .is_err(),
            "a delayed generic response must fail against retired generation A"
        );
        assert!(
            incoming
                .challenge_builder(crate::api::respond::AuthScheme::Digest)
                .expect("exact challenge builder")
                .with_realm("example.test")
                .with_nonce("generation-a-nonce")
                .send()
                .await
                .is_err(),
            "a delayed auth challenge must fail against retired generation A"
        );
        assert_eq!(
            store
                .get_session_exact(&generation_b)
                .await
                .expect("generation B remains live")
                .call_state,
            CallState::Ringing,
            "generation-A request objects must not mutate generation B"
        );

        coordinator.shutdown();
    }

    #[tokio::test]
    async fn deferred_guard_wait_for_cancelled_observes_call_cancelled() {
        let coordinator = UnifiedCoordinator::new(Config::local("guard-test", 35992))
            .await
            .expect("coordinator starts");
        let call_id = CallId::new();
        let guard =
            IncomingCallGuard::new(call_id.clone(), coordinator.clone(), Duration::from_secs(5));
        let resolved = guard.resolved.clone();

        let waiter = tokio::spawn({
            let guard = guard;
            async move { guard.wait_for_cancelled(Some(Duration::from_secs(2))).await }
        });
        tokio::time::sleep(Duration::from_millis(50)).await;

        publish_synthetic(&coordinator, Event::CallCancelled { call_id }).await;

        waiter.await.unwrap().unwrap();
        assert!(resolved.load(Ordering::SeqCst));
        coordinator.shutdown();
    }

    #[tokio::test]
    async fn deferred_guard_wait_for_cancelled_errors_on_answer() {
        let coordinator = UnifiedCoordinator::new(Config::local("guard-test", 35993))
            .await
            .expect("coordinator starts");
        let call_id = CallId::new();
        let guard =
            IncomingCallGuard::new(call_id.clone(), coordinator.clone(), Duration::from_secs(5));
        let resolved = guard.resolved.clone();

        let waiter = tokio::spawn({
            let guard = guard;
            async move { guard.wait_for_cancelled(Some(Duration::from_secs(2))).await }
        });
        tokio::time::sleep(Duration::from_millis(50)).await;

        publish_synthetic(&coordinator, Event::CallAnswered { call_id, sdp: None }).await;

        let err = waiter.await.unwrap().unwrap_err();
        assert!(matches!(
            err,
            SessionError::Other(ref detail) if detail.contains("answered before cancellation")
        ));
        assert!(resolved.load(Ordering::SeqCst));
        coordinator.shutdown();
    }

    #[tokio::test]
    async fn deferred_guard_wait_for_cancelled_timeout_does_not_resolve_guard() {
        let coordinator = UnifiedCoordinator::new(Config::local("guard-test", 35994))
            .await
            .expect("coordinator starts");
        let guard =
            IncomingCallGuard::new(CallId::new(), coordinator.clone(), Duration::from_secs(5));
        let resolved = guard.resolved.clone();

        let err = guard
            .wait_for_cancelled(Some(Duration::from_millis(25)))
            .await
            .unwrap_err();

        assert!(
            matches!(err, SessionError::Timeout(_)),
            "caller timeout should surface as a timeout"
        );
        assert!(
            !resolved.load(Ordering::SeqCst),
            "caller timeout must not resolve or mutate the guard"
        );

        guard.abandon();
        assert!(
            resolved.load(Ordering::SeqCst),
            "abandon is the explicit local policy decision"
        );
        coordinator.shutdown();
    }

    #[tokio::test]
    async fn deferred_guard_abandon_resolves_without_sip_response() {
        let coordinator = UnifiedCoordinator::new(Config::local("guard-test", 35995))
            .await
            .expect("coordinator starts");
        let guard =
            IncomingCallGuard::new(CallId::new(), coordinator.clone(), Duration::from_secs(5));
        let resolved = guard.resolved.clone();

        guard.abandon();

        assert!(resolved.load(Ordering::SeqCst));
        coordinator.shutdown();
    }

    #[tokio::test]
    async fn redirect_with_contacts_rejects_non_3xx_status() {
        let coordinator = UnifiedCoordinator::new(Config::local("redirect-test", 35996))
            .await
            .expect("coordinator starts");
        let incoming = IncomingCall::new(
            CallId::new(),
            "sip:a@example.test".into(),
            "sip:b@example.test".into(),
            None,
            coordinator.clone(),
        );

        let err = incoming
            .redirect_with_contacts(486, ["sip:voicemail@example.test"])
            .await
            .unwrap_err();
        assert!(matches!(err, SessionError::InvalidInput(_)));
        coordinator.shutdown();
    }

    #[tokio::test]
    async fn redirect_with_contacts_rejects_empty_contacts() {
        let coordinator = UnifiedCoordinator::new(Config::local("redirect-test", 35997))
            .await
            .expect("coordinator starts");
        let incoming = IncomingCall::new(
            CallId::new(),
            "sip:a@example.test".into(),
            "sip:b@example.test".into(),
            None,
            coordinator.clone(),
        );

        let contacts: Vec<String> = Vec::new();
        let err = incoming
            .redirect_with_contacts(302, contacts)
            .await
            .unwrap_err();
        assert!(matches!(err, SessionError::InvalidInput(_)));
        coordinator.shutdown();
    }
}