offline-protocol 0.30.0

Main protocol engine for the Offline Protocol SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
//! Type definitions, constants, and shared state for the protocol engine.

use crate::events::{Event, EventCallback, PresenceStatus};
use crate::telemetry::{
    dispatch_protocol_event, dispatch_record, scrub_event, TelemetryContext, TelemetryRecord,
};
use crate::Error;
use chrono::{DateTime, Utc};
use offline_protocol_core::{
    AppId, ContentType, ForwardInfo, MediaMetadata, Message, MessageId, MessagePriority,
    ReplyContext,
};
use offline_protocol_transport::TransportType;
use serde::{Deserialize, Serialize};
use std::collections::{HashSet, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use tracing::warn;

/// A carrier that speaks the gateway contract, and can therefore answer about
/// a third party's reachability.
///
/// Two of the five transports qualify, and the type exists so the other three
/// cannot be passed where a gateway answer is expected. That is not a
/// theoretical tidiness: every effect of a presence answer is carrier-scoped,
/// so a wrong carrier records a fact against a transport that never claimed
/// it and pins an un-park flush to a transport that never proved anything,
/// neither of which fails loudly.
///
/// - **BLE and Wi-Fi Direct** are zone carriers. A neighbour being in range
///   is a fact about this device's own link, not a claim about a third party,
///   and it arrives through discovery rather than through a verb.
/// - **Nostr** structurally cannot answer: a broadcast relay reports no
///   per-recipient delivery, so it is a carrier and never a gateway. See
///   [ADR 0017](../../../docs/adr/0017-nostr-is-a-carrier-not-a-gateway.md).
///   This is a permanent gap, recorded here in the type so it stops being
///   rediscovered as a bug.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GatewayCarrier {
    /// The internet relay, which implemented every verb before the contract
    /// named them.
    Internet,
    /// A gateway daemon reached over local IP, through the transport slot
    /// named `Reticulum` after its reference backbone. The variant names
    /// the carrier, not the backbone: what stands behind the daemon is the
    /// gateway's property, and this device learns of it only as a
    /// capability token it never reads (ADR 0026).
    Reticulum,
}

impl GatewayCarrier {
    /// The transport this gateway's answers are recorded against.
    pub fn transport(self) -> TransportType {
        match self {
            Self::Internet => TransportType::Internet,
            Self::Reticulum => TransportType::Reticulum,
        }
    }

    /// How a presence answer from this gateway is labelled to the app.
    pub fn presence_source(self) -> crate::events::PresenceSource {
        match self {
            Self::Internet => crate::events::PresenceSource::Internet,
            Self::Reticulum => crate::events::PresenceSource::Reticulum,
        }
    }

    /// The gateway behind `transport`, or `None` for a carrier that answers
    /// no verdicts.
    ///
    /// Exhaustive by construction rather than by a wildcard: a sixth
    /// transport must be classified here deliberately, and the wrong default
    /// is the silent one. A carrier that returns `None` is not broken; it
    /// simply never contradicts the blanket "up means reachable" assumption,
    /// which is the residual `docs/mesh.md` records.
    pub fn from_transport(transport: TransportType) -> Option<Self> {
        match transport {
            TransportType::Internet => Some(Self::Internet),
            TransportType::Reticulum => Some(Self::Reticulum),
            TransportType::BLE | TransportType::WiFiDirect | TransportType::Nostr => None,
        }
    }
}

/// Retry interval for persisting session confirmation after a transient storage error.
pub(crate) const CONFIRMATION_RETRY_INTERVAL_SECS: i64 = 5;
/// Probe interval for reconciling pending sessions after restart.
pub(crate) const CONFIRMATION_PROBE_INTERVAL_SECS: i64 = 5;
/// Minimum interval between 1:1 session re-keys for the same peer, triggered by
/// an epoch-desync decrypt failure. A re-key is a full teardown + key-package
/// re-exchange, so it is rate-limited well above the confirmation-probe cadence:
/// one legit desync heals in a single round-trip, and this bounds a peer
/// replaying stale-epoch ciphertext (or an injected wrong-epoch frame) to at
/// most one re-key per this window rather than a storm. The floor is enforced
/// unconditionally — a successful decrypt on the healed session does NOT reset
/// it — so an attacker cannot defeat it by interleaving replays with legit
/// traffic (see `schedule_session_rekey`).
pub(crate) const REKEY_INTERVAL_SECS: i64 = 30;
/// Minimum interval between two pushes of our key package to the same peer
/// while no session with that peer exists.
///
/// The first push is fire-once over a carrier that may lose it without saying
/// so (a superseded peer stream drops its late frames, `docs/spec/stream-framing.md`), so a
/// push that produced no session is re-armed. The floor is what keeps the
/// re-arm from becoming a key-package storm: discovery fires on every inbound
/// body, and a peer that never answers (an old SDK, encryption opted out) gets
/// one advertisement per window rather than one per frame. The window doubles
/// with each repeat up to [`KEY_PACKAGE_RESEND_CAP_SECS`], so that peer is not
/// sent a key package every 30 seconds for as long as it stays in range.
pub(crate) const KEY_PACKAGE_RESEND_INTERVAL_SECS: u64 = 30;
/// Ceiling on the doubling key-package resend window, matching
/// [`WELCOME_UNREACHABLE_RETRY_CAP_SECS`].
pub(crate) const KEY_PACKAGE_RESEND_CAP_SECS: u64 = 600;
// The wait doubles at most five times (`<< min(5)`, which also keeps the
// shift from wrapping the wait to zero); this keeps the cap reachable.
const _: () = assert!(KEY_PACKAGE_RESEND_INTERVAL_SECS << 5 >= KEY_PACKAGE_RESEND_CAP_SECS);
const _: () = assert!(KEY_PACKAGE_RESEND_CAP_SECS as i64 == WELCOME_UNREACHABLE_RETRY_CAP_SECS);

/// Seconds to wait before repeating a key-package push that has already been
/// repeated `repeats` times: the interval, doubled per repeat, capped.
pub(crate) fn key_package_resend_wait_secs(repeats: u32) -> u64 {
    (KEY_PACKAGE_RESEND_INTERVAL_SECS << repeats.min(5)).min(KEY_PACKAGE_RESEND_CAP_SECS)
}
/// Number of welcome retry records processed per tick.
pub(crate) const WELCOME_RETRY_BATCH_SIZE: usize = 20;
/// Hard TTL for outbound welcome lifecycle records.
pub(crate) const WELCOME_LIFECYCLE_TTL_SECS: i64 = 300;
/// Jitter ratio applied to welcome retry backoff delays.
pub(crate) const WELCOME_RETRY_JITTER_RATIO: f64 = 0.2;
/// Timeout waiting for explicit internet send confirmation for welcome.
pub(crate) const WELCOME_INTERNET_CONFIRM_TIMEOUT_SECS: i64 = 10;
/// Timeout waiting for a mesh (BLE / WiFi-Direct) welcome to be confirmed by
/// the peer proving the session (probe / ack / welcome / decrypt). A mesh
/// `send()` returning Ok only means the local stack accepted the bytes, not
/// that the multi-fragment Welcome reassembled on the peer — so the lifecycle
/// stays non-terminal and the retry queue re-sends the whole Welcome after this
/// window, recovering from a lost fragment. Slightly longer than the internet
/// timeout to allow a slow multi-fragment Welcome to assemble plus the probe
/// round-trip before paying to re-fragment and re-send it.
pub(crate) const WELCOME_MESH_CONFIRM_TIMEOUT_SECS: i64 = 15;
/// Retry cadence for a Welcome that has no transport carrier at all (the peer is
/// simply unreachable right now). A send is guaranteed to fail with no carrier,
/// so we do NOT burn the speculative send/transition/event churn on it every
/// retry tick — we park it and re-check on this slow interval instead. The
/// primary recovery is event-driven (`on_neighbor_discovered` → re-arm fires the
/// instant a carrier surfaces the peer); this poll is only a safety net for a
/// carrier returning without a fresh discovery event, so it is deliberately far
/// slower than the data-plane retry interval to keep an offline device quiet.
/// Also the base interval for the plain-DM unreachable reachability probe
/// (`handle_recipient_unreachable_for_message`), which shares this
/// escalation.
pub(crate) const WELCOME_NO_CARRIER_RETRY_SECS: i64 = 15;
/// Cap for the escalating retry interval of a welcome repeatedly parked
/// `PeerUnreachable`, on every carrier (see
/// `apply_recipient_unreachable_failure`). Each consecutive unreachable park
/// doubles the interval from [`WELCOME_NO_CARRIER_RETRY_SECS`] up to this
/// cap: DORS may keep selecting the internet path for the timed retry, and
/// every such round trips another relay `DeliveryError` with the attempt
/// refunded — without escalation that is an unbounded 15s resend loop into
/// the relay for as long as the peer stays offline. At the cap the steady
/// state matches the presence-rescue cadence (one send per 10 min), which is
/// cheap and self-resolving. Shared by the plain-DM unreachable probe
/// (`handle_recipient_unreachable_for_message`, per-peer
/// `dm_unreachable_parks` counter) for the same reason — and there the
/// escalation carries the whole bound, since that probe runs on every carrier
/// (see `park_unreachable_dm`), including internet-only devices.
pub(crate) const WELCOME_UNREACHABLE_RETRY_CAP_SECS: i64 = 600;
/// Only used when `RetryConfig::edge_driven_unreachable_dm` is opted in. How
/// many escalating timed probes a plain DM to an unreachable peer gets before
/// it goes edge-driven (stops being timed-probed and rests in the outbox until
/// the peer next proves reachable). One probe (plus the original send) gives a
/// brief blip tolerance; beyond that, recovery is the reachability edge — which
/// in an opted-in deployment is reliable (peers interact / advertise presence
/// on return), so more blind probes only lengthen the transient after a mass
/// disconnect. Default-off deployments never consult this (they probe forever
/// at the documented 15s->600s cap).
pub(crate) const DM_UNREACHABLE_PROBE_LIMIT: u32 = 1;
/// Only used when `RetryConfig::edge_driven_unreachable_dm` is opted in. On the
/// startup/reconnect re-drive, a still-undelivered outbox message first sent
/// longer ago than this is left edge-driven rather than re-driven, so a restart
/// does not re-ramp a durably-failing backlog. Default-off deployments never
/// consult this.
pub(crate) const OUTBOX_DURABLE_UNREACHABLE_AGE_SECS: i64 = 300;
/// Only used when `RetryConfig::edge_driven_unreachable_dm` is opted in.
/// Sustained rate (per second) and burst budget for the RETRY/PROBE resend path
/// (`process_retry_queue`). Set below the reference relay's per-connection limit
/// (10/s sustained, 30 burst) so resends to a fleet of unreachable peers can
/// never burst the connection into a rate-limit disconnect. Only resends are
/// bounded; first-attempt sends, ACKs, control frames and handshakes are never
/// throttled.
pub(crate) const RETRY_DRAIN_RATE_PER_SEC: f64 = 8.0;
pub(crate) const RETRY_DRAIN_BURST: f64 = 24.0;
/// Age limit for a welcome lifecycle to keep its peer on the presence
/// watchlist (`welcome_pending_peers`). Without it the watch set only ever
/// grows: every offline presence answer re-parks the record and pushes its
/// `expires_at`, so a permanently-dead peer (abandoned install) is watched —
/// and its parked lifecycle persisted — forever, and each such peer occupies
/// rotation slots that delay presence rescue for live peers. Once unwatched,
/// offline answers stop, `expires_at` stops being pushed, and the record
/// ages out through normal expiry; recovery degrades to peer-initiated
/// contact or mesh discovery, both of which rebuild the lifecycle.
pub(crate) const WELCOME_WATCHLIST_MAX_AGE_SECS: i64 = 14 * 24 * 60 * 60;
/// Backoff base for presence-driven welcome rescue. The first rescue for a
/// peer is immediate; each subsequent rescue that still fails to prove the
/// session doubles the wait (40s, 80s, 160s, ...) up to
/// [`WELCOME_PRESENCE_RESCUE_MAX_SECS`]. Bounds the resend loop when a peer
/// is provably online but can never confirm (stale key package after a
/// reinstall, incompatible peer version) — without it the platform's 20s
/// presence watch would re-send the multi-frame MLS welcome forever.
pub(crate) const WELCOME_PRESENCE_RESCUE_BASE_SECS: i64 = 40;
/// Cap for the presence-rescue backoff (10 minutes). Deliberately not a
/// terminal state: a peer that stays online but never confirms keeps getting
/// one rescue per cap interval, forever. That steady state is cheap (one
/// multi-frame welcome per 10 min per such peer) and self-resolving — the
/// lifecycle disappears the moment the session confirms — so a give-up
/// threshold would only add a way to strand a recoverable session.
pub(crate) const WELCOME_PRESENCE_RESCUE_MAX_SECS: i64 = 600;
/// Well-known prefix for transport send-failure reasons meaning "the carrier
/// is up but this recipient is unreachable on it" (e.g. the internet relay
/// answered `DeliveryError` for an offline peer). Classified in
/// `on_transport_send_failed` as authoritative proof the frame was dropped:
/// instead of burning a retry attempt, a welcome refunds it and parks on an
/// escalating reachability probe (`apply_recipient_unreachable_failure`), and
/// a plain DM parks the same way (`park_unreachable_dm`). The escalation, not
/// a carrier guard, is what keeps those probes from becoming a resend loop
/// into the relay.
///
/// Cross-layer contract: the React Native platform bridges
/// (`InternetManager.kt` / `InternetManager.swift`) hardcode this literal when
/// calling `internet_send_failed_with_reason` — keep them in sync.
pub(crate) const SEND_FAIL_REASON_RECIPIENT_UNREACHABLE: &str = "recipient_unreachable";

/// The relay accepted a frame for a recipient with no live socket and handed
/// it to a push notification (`MessageSent { pushed: true }`). Not a failure:
/// the push may deliver it. What it does establish is that the recipient is
/// not on the relay, and that the push is the only thing carrying this frame:
/// a relay whose mailbox also holds a copy says so with `stored: true`, which
/// the bridges report as [`SEND_FAIL_REASON_RELAY_PUSHED_STORED`] instead.
///
/// Deliberately its own token rather than a `recipient_unreachable` tail.
/// That prefix fast-fails connection requests and moves Welcomes to `Failed`,
/// both wrong for a frame that may have arrived. This token parks plain DMs
/// only (`park_relay_pushed_dm`), and connection requests and Welcomes stay
/// on their ordinary path.
///
/// Cross-layer contract: the React Native platform bridges
/// (`InternetManager.kt` / `InternetManager.swift`) and the Python relay
/// client (`internet_manager.py`) pass this exact literal to
/// `internet_send_failed_with_reason`; pinned on the React Native side by
/// `react_native_relay_parks_a_pushed_message_sent` and on the Python side by
/// `TestMessageSentPushed`. That call must not also score the report as a send
/// failure; see `OfflineProtocol::send_report_is_carrier_failure`.
pub(crate) const SEND_FAIL_REASON_RELAY_PUSHED: &str = "relay_pushed";

/// The relay's mailbox holds this frame for the recipient's next connection
/// (`DeliveryError { stored: true }`, on a relay advertising `mailbox_v1`).
///
/// The socket write still missed, so this is the `recipient_unreachable`
/// verdict in every respect but one: the frame is not lost, and the relay
/// re-sends it the moment the recipient connects. A plain DM is therefore
/// parked *edge-driven* — no escalating reachability probe — because a probe
/// would only rewrite the copy the relay already holds. That is what the
/// relay's own contract asks for ("do not retry faster on its account"), and
/// what keeps a fleet whose peers are merely asleep from paying relay traffic
/// for a redelivery the relay is already committed to. Recovery without the
/// probe is the reachability edge, and the relay's drain either way.
///
/// Everything that is not a parkable plain DM is deliberately unchanged: the
/// token is normalized back to [`SEND_FAIL_REASON_RECIPIENT_UNREACHABLE`] in
/// `on_transport_send_failed_via`, so connection requests still fast-fail and
/// Welcomes still park on their own lifecycle. Both carry their own proof of
/// arrival, and a held copy is not a reason to stop waiting for it.
///
/// Cross-layer contract: the React Native platform bridges
/// (`InternetManager.kt` / `InternetManager.swift`) and the Python relay
/// client (`internet_manager.py`) pass this exact literal, and only for the
/// one id the relay named — `stored` is a statement about a single message,
/// never about the recipient's other in-flight frames. Pinned on the React
/// Native side by `react_native_relay_parks_a_stored_delivery_error`.
pub(crate) const SEND_FAIL_REASON_RELAY_STORED: &str = "relay_stored";

/// [`SEND_FAIL_REASON_RELAY_PUSHED`] for a frame the mailbox also holds
/// (`MessageSent { pushed: true, stored: true }`).
///
/// Parks exactly as `relay_pushed` does — plain DMs only, no
/// `MessageUndeliverable`, kept out of the carrier's failure accounting —
/// except that the park schedules no reachability probe, for the reason
/// [`SEND_FAIL_REASON_RELAY_STORED`] gives.
///
/// Two tokens rather than one token and a flag because the two origins differ
/// for everything that is *not* a plain DM: a pushed frame must not fast-fail
/// a connection request, and a missed one must. Collapsing them would trade
/// that distinction for one fewer string.
pub(crate) const SEND_FAIL_REASON_RELAY_PUSHED_STORED: &str = "relay_pushed_stored";

/// Fallback token for a send failure that classifies as nothing more specific.
pub(crate) const SEND_FAIL_REASON_TRANSPORT: &str = "transport_send_failed";
/// A Welcome was written to a carrier that never confirmed it.
pub(crate) const SEND_FAIL_REASON_CONFIRM_TIMEOUT: &str = "send_confirmation_timed_out";

/// Every token [`classify_transport_send_error`] and
/// [`send_failure_token`] can produce.
///
/// Membership is what makes classification idempotent: a value already drawn
/// from this list re-classifies to itself, so a record written by one producer
/// and re-read by the other is not degraded to the fallback. Keep it in sync
/// with the two functions — the round-trip test
/// `every_send_failure_token_classifies_to_itself` fails otherwise.
pub(crate) const SEND_FAIL_REASON_TOKENS: &[&str] = &[
    SEND_FAIL_REASON_RECIPIENT_UNREACHABLE,
    SEND_FAIL_REASON_RELAY_PUSHED,
    SEND_FAIL_REASON_RELAY_STORED,
    SEND_FAIL_REASON_RELAY_PUSHED_STORED,
    SEND_FAIL_REASON_TRANSPORT,
    SEND_FAIL_REASON_CONFIRM_TIMEOUT,
    "transport_not_connected",
    "peer_not_reachable",
    "message_too_large",
    "serialization_failed",
    "crypto_failed",
    "session_not_ready",
    "recipient_blocked",
    "media_transfer_limit",
    "group_not_found",
    "invalid_request",
    "protocol_state_invalid",
];

/// Maps a platform-supplied or previously-stored send-failure string onto a
/// fixed local vocabulary.
///
/// # Why the string cannot be passed through
///
/// The bridges build this string from the relay's `DeliveryError.reason`
/// (`"recipient_unreachable: <relay text>"` — the shape is a cross-layer
/// contract, see [`SEND_FAIL_REASON_RECIPIENT_UNREACHABLE`]), and the Nostr
/// bridge from a relay's `OK` rejection text. Both tails are remote-chosen,
/// unbounded, and not ours. They reach `MessageUndeliverable.reason`,
/// `ConnectionRequestUndeliverable.reason` and — through
/// `WelcomeLifecycleRecord::last_transport_error` —
/// `WelcomeSendFailed.transport_error`, every one of which the telemetry
/// scrubber ships verbatim beside a `recipient`/`peer_id` it *hashes*. An
/// identifier in the tail therefore undoes the hashing in the same record, and
/// even the honest relay writes one: its own wording renders the peer it
/// concerns.
///
/// # Why the prefix survives
///
/// The `recipient_unreachable` token is not decoration — core prefix-matches it
/// to fast-fail connection requests and to park plain DMs, and the bridges
/// hardcode it. So the classification *is* the bare token: everything the
/// protocol reads is kept, and only the prose tail is dropped.
///
/// Matching is prefix-based for that one token (the bridges append to it) and
/// exact otherwise, with a closed fallback — a bridge or relay rewording
/// degrades to [`SEND_FAIL_REASON_TRANSPORT`], never back to shipping its text.
/// The `&'static str` return is load-bearing the same way
/// [`GroupErrorPayload::classify_reason`] and `MlsError::privacy_safe_reason`
/// are: it makes interpolating the input unrepresentable rather than merely
/// discouraged.
///
/// Idempotent by construction, which matters twice: a record persisted before
/// this existed is classified when it is *read* (the stored string may be raw
/// relay prose), and a record written after it is classified again on the way
/// out without changing.
pub(crate) fn classify_transport_send_error(raw: &str) -> &'static str {
    // Prefix, not equality: this is the one token the bridges extend.
    if raw.starts_with(SEND_FAIL_REASON_RECIPIENT_UNREACHABLE) {
        return SEND_FAIL_REASON_RECIPIENT_UNREACHABLE;
    }
    // Already classified: a re-read of a record either producer wrote. Returns
    // the entry from the table rather than `raw`, which is what keeps the
    // signature `&'static str`.
    if let Some(token) = SEND_FAIL_REASON_TOKENS.iter().find(|t| **t == raw) {
        return token;
    }
    match raw {
        // The fixed literals the UniFFI layer substitutes when a bridge reports
        // a failure without a reason of its own. Locally minted, but classified
        // here too so the vocabulary has exactly one source.
        "Internet transport send failed"
        | "Reticulum transport send failed"
        | "Nostr transport send failed" => SEND_FAIL_REASON_TRANSPORT,
        "Welcome send confirmation timed out" => SEND_FAIL_REASON_CONFIRM_TIMEOUT,
        // Everything else, including anything a relay or bridge chose.
        _ => SEND_FAIL_REASON_TRANSPORT,
    }
}

/// The pending-ACK table was full, so the frame was held back instead of
/// transmitted. Not a transport fault: the retry queue sends it as soon as an
/// acknowledgement frees a slot. A link carrying less than the app offers
/// produces this steadily, and it is the signal to slow down.
pub(crate) const SEND_DEFER_REASON_ACK_CAPACITY: &str = "ack_capacity";

/// The peer already had [`PEER_SEND_WINDOW`] frames over a mesh link awaiting
/// its acknowledgement, so the frame waits in the outbox and goes out, oldest
/// first, as acknowledgements come back. Not a fault: it is what a link
/// carrying less than the app offers looks like.
pub(crate) const SEND_DEFER_REASON_PEER_WINDOW: &str = "peer_window";

/// How many frames this device keeps in flight to one peer over a mesh link
/// (Bluetooth LE, Wi-Fi Direct) before it holds the next one back.
///
/// Without a window the sender handed the carrier every new frame, every
/// resend and an acknowledgement per received frame, into one unbounded queue,
/// on a timer that starts at enqueue. In the 2026-10-19 volume test (run 4)
/// the Android side's indication path carried 12 to 16 frames a second against
/// 15 offered: the acknowledgement round trip passed the timeout, every resend
/// cost the receiver another acknowledgement on the saturated path, that
/// path's rate halved, and both phones retried 50,000 times and evicted 6,000
/// messages as "Outbox capacity exceeded". A window bounds the queue, and with
/// it the round trip, the resends and the receiver's ratchet gap, at any link
/// rate. It costs no throughput while it exceeds what the link carries in one
/// unloaded round trip: about six messages on that link, counting the
/// receiver's acknowledgement coalescing (`ACK_COALESCE_WINDOW`) and a
/// processing tick on each side. Sixteen leaves margin, and keeps the
/// round trip under about three seconds there, well inside the timeout.
///
/// Only the mesh carriers: a relay mailbox is store-and-forward, and holding
/// a message back from it would strand it on a device the user may close.
pub(crate) const PEER_SEND_WINDOW: usize = 16;

// Every in-window frame can be re-sealed on each of its attempts, and the
// re-seal budget must never trip under the window: past it resends go out
// verbatim, and a verbatim copy more than the out-of-order tolerance behind
// the receiver never decrypts (run 4: 3,308 such failures).
const _: () = assert!(
    (PEER_SEND_WINDOW as u64)
        * (1 + offline_protocol_reliability::constants::DEFAULT_MAX_RETRIES as u64)
        < RESEAL_GAP_BUDGET
);

/// The smallest forward window any receiver of a 1:1 session has: a phone's
/// OpenMLS group (configured) or a leaf's mls-rs one (fixed by mls-rs, and
/// smaller). The re-seal budget is a fraction of this, not of the phone's
/// window: derived from the phone's 8192, it let a sender burn 2,048
/// generations past a leaf that can fast-forward over 1,024.
pub(crate) const RECEIVER_FORWARD_WINDOW_FLOOR: u64 = {
    let phone = offline_protocol_mls::group::SENDER_RATCHET_MAXIMUM_FORWARD_DISTANCE as u64;
    let leaf = offline_protocol_mls::group::LEAF_SENDER_RATCHET_MAXIMUM_FORWARD_DISTANCE as u64;
    if phone < leaf {
        phone
    } else {
        leaf
    }
};

/// How many frames a sender may seal for one peer past the last one that peer
/// acknowledged before resends stop being re-sealed and go out verbatim.
///
/// A receiver cannot decrypt a frame more than its forward window past the
/// newest it has decrypted. A quarter of the smallest window any receiver has
/// ([`RECEIVER_FORWARD_WINDOW_FLOOR`]) leaves the rest to new messages, which
/// still seal. The per-peer window keeps unacknowledged seals growing only by
/// resends of at most [`PEER_SEND_WINDOW`] frames, so the budget trips only on
/// a link that has carried no acknowledgement for a whole retry ladder; the
/// compile-time assertion above keeps it above that ladder.
pub(crate) const RESEAL_GAP_BUDGET: u64 = RECEIVER_FORWARD_WINDOW_FLOOR / 4;

/// Past [`RESEAL_GAP_BUDGET`], a resend whose bytes the receiver can no
/// longer decrypt is still re-sealed, until the gap reaches this.
///
/// A verbatim copy sealed more than the out-of-order tolerance before the
/// newest frame this device sealed is refused by a receiver that has
/// decrypted anything since, and refused without an acknowledgement on a
/// mesh link, so it was resent until its ladder ran out: in run 4 of the
/// 2026-10-19 volume test, 525 such messages produced 3,308 decrypt
/// failures and were never delivered. Re-sealing one spends a generation,
/// which is what the budget rations, so the exception stops at half the
/// smallest forward window ([`RECEIVER_FORWARD_WINDOW_FLOOR`]): the receiver
/// going deaf is the failure the budget exists for, and a wasted resend is
/// not.
pub(crate) const RESEAL_HARD_LIMIT: u64 = RECEIVER_FORWARD_WINDOW_FLOOR / 2;

/// The longest an acknowledgement timeout is postponed for a frame that is
/// still waiting in a local transport queue, measured from the attempt.
///
/// Bounded so a queue that never drains (a link that died without telling
/// the transport) still reaches the retry ladder, and through it the other
/// carriers and the mesh, within a minute.
pub(crate) const QUEUED_ACK_GRACE_MAX: std::time::Duration = std::time::Duration::from_secs(60);

/// Classifies a locally raised send failure for an event field.
///
/// The sibling of [`classify_transport_send_error`] for errors this device
/// produced rather than received. It exists for the same reason: the transport
/// layer renders the counterparty into its own message —
/// `PeerNotReachable("no mesh transport holds a link to {peer_id}…")`, and the
/// same in the BLE and Wi-Fi Direct backends — so `format!("{}", err)` on a
/// routine send failure ships the peer's address to a telemetry sink. On
/// `MessageDeferred` that was the *only* identity in the record, since the
/// event carried no field for the scrubber to hash.
///
/// The match over [`crate::Error`] is exhaustive with no `_` arm: it lives in
/// the defining crate, so a new variant fails to compile here and forces the
/// privacy decision where the variant is written. The transport arm delegates
/// to `offline_protocol_transport::Error::code`, which is that crate's own
/// exhaustive, documented-stable classifier — its doc names telemetry
/// classification as the intended use. Matching its output needs a fallback
/// arm (a foreign `#[non_exhaustive]` enum can grow a variant), and that
/// fallback is closed: a new transport variant degrades to
/// [`SEND_FAIL_REASON_TRANSPORT`], never to its rendered text.
pub(crate) fn send_failure_token(err: &crate::Error) -> &'static str {
    use crate::Error as E;
    match err {
        E::Transport(inner) => match inner.code() {
            "TRANSPORT_NOT_AVAILABLE" => "transport_not_connected",
            "PEER_NOT_REACHABLE" => "peer_not_reachable",
            "MESSAGE_TOO_LARGE" => "message_too_large",
            "SERIALIZATION_ERROR" => "serialization_failed",
            "CRYPTO_ERROR" => "crypto_failed",
            _ => SEND_FAIL_REASON_TRANSPORT,
        },
        E::NotStarted | E::AlreadyStarted | E::InvalidState(_) => "protocol_state_invalid",
        E::InvalidConfiguration(_)
        | E::InvalidArgument(_)
        | E::PermissionDenied(_)
        | E::TelemetryConfigInvalid(_) => "invalid_request",
        E::Core(_) | E::Serialization(_) => "serialization_failed",
        E::Router(_) | E::Reliability(_) | E::Service(_) => SEND_FAIL_REASON_TRANSPORT,
        E::Mls(_) | E::EncryptFailed(_) => "crypto_failed",
        E::MlsNotInitialized | E::SessionNotReady(_) | E::NoKeyPackage(_) => "session_not_ready",
        E::UserBlocked(_) => "recipient_blocked",
        E::MediaTransferLimit(_) => "media_transfer_limit",
        E::GroupNotFound(_) => "group_not_found",
        // Data-layer failures cannot reach a send path today: nothing in the
        // data layer sends. They are classified anyway because this match is
        // where the privacy decision is forced, and "unreachable" is a claim
        // that stops being true the moment replication lands. Each token is
        // locally minted and carries no identifier: the errors themselves
        // interpolate only byte counts and a decode detail, never a space,
        // document or peer.
        E::DataDisabled | E::DataStorageUnavailable => "data_unavailable",
        E::DocTooLarge { .. } => "data_doc_too_large",
        E::DataCorrupted(_) => "data_corrupt",
        E::Other(_) => SEND_FAIL_REASON_TRANSPORT,
    }
}
/// Minimum interval between session reconciliation scans (list_sessions I/O).
/// Keeps the expensive Keychain/Keystore I/O out of the hot path so that
/// sendMessage() is not blocked by Mutex contention on every process tick.
pub(crate) const RECONCILIATION_THROTTLE_MS: u64 = 2_000;
/// Lamport clock ticks between storage persistence writes. Avoids a
/// Keychain/Keystore write on every sent and received message. On crash
/// recovery, at most this many ticks are lost, which is safe — the clock
/// is only used for causal ordering and the gap is absorbed on the next
/// merge with any peer.
pub(crate) const LAMPORT_PERSIST_INTERVAL: u64 = 64;

/// Most seen ids one `DedupSeenRecord` carries. Matches the default
/// `max_tracked_messages`; a larger configured tracker persists its newest
/// ids only, which are the ones a replay is most likely to repeat.
pub(crate) const MAX_PERSISTED_DEDUP_IDS: usize = 5000;

/// Seen-set changes that force a write before the time cadence elapses. A
/// burst of inbound traffic is exactly when the set is worth having on disk,
/// and 32 changes at ~60 bytes each is well under the cost of one write.
pub(crate) const DEDUP_PERSIST_DIRTY_THRESHOLD: u32 = 32;

/// How long a dirty seen set may wait for the next `process()` tick before it
/// is written. Bounds what a crash loses to a few seconds of receipts.
pub(crate) const DEDUP_PERSIST_INTERVAL: std::time::Duration = std::time::Duration::from_secs(5);

/// The deduplicator's seen set as persisted under
/// [`storage_keys::DEDUP_SEEN_IDS`].
///
/// The deduplicator was in-memory only, so after a restart the socket copy of
/// a message the app had already consumed from a push injection was not
/// recognised as a duplicate: it went to the ratchet, whose generation for
/// that message was already spent, and surfaced as a decryption failure — a
/// spurious signal the app counts toward its split-brain breaker, which then
/// tears down a healthy session. Persisting the ids makes the second copy
/// recognisable across the restart. Ids and timestamps only; nothing here is
/// content.
///
/// `version` is the forward-compatibility hinge: an unknown version is dropped
/// rather than guessed at, and the set simply starts empty.
#[derive(Serialize, Deserialize)]
pub(crate) struct DedupSeenRecord {
    #[serde(default = "dedup_seen_record_version")]
    pub(crate) version: u8,
    pub(crate) entries: Vec<offline_protocol_reliability::SeenId>,
}

/// The only record version this build writes or reads.
pub(crate) const DEDUP_SEEN_RECORD_VERSION: u8 = 1;

fn dedup_seen_record_version() -> u8 {
    DEDUP_SEEN_RECORD_VERSION
}
/// Seconds the Nostr receive watermark must advance before it is written back
/// to protocol-state storage. Same debounce role as
/// [`LAMPORT_PERSIST_INTERVAL`]: every inbound relay event moves the mark, and
/// a storage write per message is exactly the I/O the Lamport debounce exists
/// to avoid.
///
/// What an un-flushed gap costs is bounded and one-directional: a launch that
/// loses up to this much advance re-fetches a slightly wider slice of relay
/// history, which dedup absorbs. It can never *skip* messages, because the
/// stale mark is always behind the live one.
pub(crate) const NOSTR_WATERMARK_PERSIST_INTERVAL_SECS: i64 = 300;
pub(crate) const MEDIA_TRANSFER_STALE_TIMEOUT_SECS: u64 = 300;
/// Maximum number of tracked known peers for service discovery.
pub(crate) const MAX_KNOWN_PEERS: usize = 1000;
/// How long a known peer stays tracked without being re-seen.
///
/// Peers on carriers with no disconnect signal (Internet, Reticulum, Nostr,
/// and WiFi Direct message-path senders) are only ever *added* to
/// `known_peers`; this TTL is their eviction path, swept from the periodic
/// `cleanup_expired_entries` tick. A second layer — least-recently-seen
/// eviction when an insert hits `MAX_KNOWN_PEERS` — guarantees a newly
/// discovered peer is always tracked even between sweeps.
///
/// Deliberately generous: a connected-but-quiet BLE peer refreshes only via
/// platform advertisement sightings (BLE inbound messages do not route
/// through the discovery hook), so a short TTL would evict it while still
/// connected. Eviction is self-healing (the peer is re-tracked on its next
/// advertisement or message), so erring long only delays hygiene.
pub(crate) const KNOWN_PEER_TTL_SECS: u64 = 1800;

/// Battery level below which a device stops carrying traffic for other
/// people even while charging.
///
/// Re-exported from the router crate rather than restated here: message
/// forwarding and the relay role apply the same floor, and a device must not
/// keep carrying traffic at a level that would have stripped it of the role.
/// Two copies of the number would eventually disagree, and nothing would
/// notice.
pub(crate) use offline_protocol_router::CRITICAL_RELAY_BATTERY_LEVEL;

/// The metadata keys a signed control frame carries: the Ed25519 signature
/// over the canonical payload, and the public key it verifies against, both
/// base64.
///
/// Re-exported rather than restated because the payload they accompany is
/// built by `offline_protocol_sealed::control_signing_payload`, which a leaf
/// node calls too. `CTRL_SIGN_DOMAIN` lives there as well and is no longer
/// named in this crate outside its tests: nothing here builds the payload any
/// more. A key or a domain declared in two places is one that can differ in
/// one of them, and the only symptom is a signature that stops verifying.
pub(crate) use offline_protocol_sealed::{CTRL_PK_META_KEY, CTRL_SIG_META_KEY};

/// Maximum number of peers retained in the encryption-capability set that gates
/// inbound plaintext.
///
/// Sized to hold every legitimate source at once — the durable capability
/// records plus the MLS session list — with room to spare, so a real deployment
/// never reaches it. The cap exists because the set is keyed by a wire-claimed
/// sender id and nothing else bounds it.
///
/// Note what does *not* bound it: requiring a signature. The control gate does
/// now reject unsigned control frames before dispatch, so every marking path
/// runs on a verified frame — and that changes nothing here. A signature proves
/// the signer owns the address it claims; it does not prove the address belongs
/// to anyone real, and minting one costs a keygen. An attacker spending keygens
/// produces unlimited distinct well-signed peers, so "written only after a
/// signature verifies" is not by itself a bound on anything.
///
/// It bounds the durable `encryption_capable_peers` storage category as well,
/// because `OfflineProtocol::record_encryption_capable` persists only for a
/// peer this cap admitted.
///
/// Unlike the other maps keyed by a wire-claimed id, this one **refuses** at
/// capacity instead of resetting or evicting: forgetting a peer here is the
/// fail-open direction. See `OfflineProtocol::mark_encryption_capable`.
pub(crate) const MAX_ENCRYPTION_CAPABLE_PEERS: usize = 8192;

/// Maximum number of blocked users to retain.
pub(crate) const MAX_BLOCKED_USERS: usize = 10_000;

/// Maximum number of peers tracked for once-per-peer
/// `PlaintextReceiveRejected` warning suppression.
///
/// The keys are wire-claimed (attacker-controllable) sender ids, so the set
/// resets at capacity instead of growing without bound: a flood of forged
/// senders degrades the throttle to once-per-peer-per-generation while
/// memory stays capped.
pub(crate) const MAX_PLAINTEXT_RECEIVE_WARNED_PEERS: usize = 1000;

/// Maximum number of peers tracked for once-per-peer-per-code control-gate
/// warning suppression.
///
/// Every rejection the control gate reports is reachable from an unauthenticated
/// frame carrying an attacker-chosen sender id — that is what a gate rejection
/// *is* — so without a throttle an off-path injector turns the app's event
/// stream into a flood and desensitizes operators to the one code that has no
/// benign reading (`SENDER_ADDRESS_MISMATCH`). Bounded and reset at capacity for
/// the same reason as [`MAX_PLAINTEXT_RECEIVE_WARNED_PEERS`]: the keys are
/// attacker-controlled, so a forged-sender flood degrades the throttle to
/// once-per-peer-per-generation rather than growing memory without bound.
pub(crate) const MAX_CONTROL_GATE_WARNED_PEERS: usize = 1000;

/// Maximum number of pending (received-but-unused) peer key packages retained
/// in memory and in durable `MlsStorage`.
///
/// Keyed by the wire-claimed `sender`, so — like [`MAX_KNOWN_PEERS`] — an
/// unpinned peer can flood distinct forged ids under the default config. Each
/// entry is also written to durable secure storage (`persist_peer_key_package`,
/// iOS Keychain / Android Keystore) and re-loaded on boot, so without this cap
/// a flood grows durable storage without bound and re-inflates memory on every
/// restart. At capacity a new peer evicts the soonest-to-expire entry and drops
/// its persisted copy; the restore-on-boot loop is capped the same way.
pub(crate) const MAX_PENDING_KEY_PACKAGES: usize = 1000;

/// Maximum outbound messages waiting for one peer's MLS session.
///
/// This queue is durable and accepts application-controlled content, so a peer
/// that never completes session establishment must not grow it without bound.
/// The limit matches the default inbound pending-decryption per-peer bound.
///
/// A count alone does not bound memory or durable storage — message content is
/// application-supplied — so this cap works together with
/// [`MAX_PENDING_MESSAGE_BYTES_PER_PEER`]; whichever binds first wins.
pub(crate) const MAX_PENDING_MESSAGES_PER_PEER: usize = 64;

/// Maximum outbound messages waiting for MLS sessions across all peers.
///
/// At capacity the globally oldest message is settled as failed before the new
/// message is admitted. The limit matches the default inbound pending queue.
/// Paired with [`MAX_PENDING_MESSAGE_BYTES_GLOBAL`], as above.
pub(crate) const MAX_PENDING_MESSAGES_GLOBAL: usize = 4096;

/// Maximum pending entries expired in one `process()` tick.
///
/// Each expiry is a settlement *and* a durable delete, and the two have to stay
/// paired — so what gets bounded is how many entries expire per pass, not how
/// many deletes it may issue. Messages queued in a burst share a `queued_at` and
/// so come due in a burst; without this, one tick of the 100 ms bindings loop
/// could issue up to [`MAX_PENDING_MESSAGES_GLOBAL`] synchronous deletes, each a
/// device barrier on every built-in provider.
///
/// The remainder is not deferred indefinitely: the entries left behind are still
/// past their deadline, so `recompute_next_pending_message_expiry` leaves
/// `next_pending_message_expiry` in the past and the very next tick drains
/// another pass.
pub(crate) const MAX_PENDING_EXPIRIES_PER_PASS: usize = 64;

/// Maximum per-message records one launch writes migrating legacy queues.
///
/// The migration's writes are the one part of the pending restore that is not
/// already bounded by a delete budget, and a `store` is the more expensive of
/// the two: every built-in provider flushes the record *and* its directory. A
/// pre-split install sitting near [`MAX_PENDING_MESSAGES_GLOBAL`] would
/// otherwise pay thousands of device barriers inside `initialize_mls`, on the
/// launch path, where a mobile watchdog is watching.
///
/// Sized to match [`crate::protocol::storage::MAX_RESTORE_PRUNE_DELETES`], so
/// the whole walk's barrier count stays the same order of magnitude whichever
/// half of it does the work. Checked per recipient and before its first write,
/// so a queue is either migrated whole or left entirely on disk for the next
/// launch — never split across the two layouts with nothing to reconcile it.
pub(crate) const MAX_MIGRATED_PENDING_WRITES_PER_LAUNCH: usize = 512;

/// Maximum terminal settlements parked by restore for `start()` to drain.
///
/// The restore caps already bound how many can be produced, but they bound it
/// as a sum across every category, and nothing drains this queue until
/// `start()` — which an application that only calls `initialize_mls`, or that
/// retries it against a store that keeps failing, may never reach. Sized above
/// the largest single-category restore ([`MAX_PENDING_MESSAGES_GLOBAL`], the
/// pending queue's own global cap) so no legitimate restore is ever truncated;
/// past that point the count is reported instead of the events.
pub(crate) const MAX_DEFERRED_RESTORE_SETTLEMENTS: usize = 2 * MAX_PENDING_MESSAGES_GLOBAL;

/// Maximum size of application-supplied message content accepted at the public
/// send boundary, in bytes.
///
/// Enforced at the boundary — not at transmit time — because a message queued
/// behind MLS session establishment never reaches the transport's
/// `DEFAULT_MAX_MESSAGE_SIZE` (1 MiB) check: it sits in the durable pending
/// queue first. Without a boundary cap a handful of very large sends could
/// exhaust mobile memory and protocol-state disk while still being formally
/// "within" the count caps above.
///
/// 256 KiB leaves ample headroom under the 1 MiB transport ceiling for MLS
/// ciphertext expansion, base64, and the JSON wire envelope, so anything this
/// path accepts can actually be delivered. Larger payloads belong on the media
/// path (`send_media`), which chunks.
pub(crate) const MAX_MESSAGE_CONTENT_BYTES: usize = 256 * 1024;

/// Maximum total serialized bytes of one peer's pending-session queue.
///
/// At capacity the peer's oldest entries are settled as failed until the
/// incoming message fits, exactly like the count cap. Deliberately larger than
/// [`MAX_MESSAGE_CONTENT_BYTES`] plus [`MAX_RICH_EXTRAS_BYTES`] so a single
/// boundary-legal message always fits in an empty queue and admission can never
/// livelock (pinned by `pending_queue_byte_budgets_admit_any_boundary_legal_message`).
///
/// # Durable cost
///
/// Pending messages are persisted one record per message
/// ([`storage_keys::PENDING_MESSAGE_ENTRIES`]), so an enqueue writes its own
/// entry and nothing else: filling one peer to this budget costs `budget` bytes
/// written, not `budget × entries / 2`. The per-recipient layout this replaced
/// re-serialized the peer's whole queue on every enqueue, which made the byte
/// cost quadratic in the entry count.
///
/// Bytes were never the expensive half, though, and sizing this budget off them
/// would be reading the wrong meter. Every built-in provider pays two device
/// barriers per `store` (the record's own flush plus its directory's) and one
/// per `delete`, *regardless of record size* — so a 500-byte write and a 2 MiB
/// write cost the same in the resource that actually dominates. What the
/// per-message layout buys is not primarily speed but that a record which will
/// not open still names the message it lost; see
/// [`storage_keys::PENDING_MESSAGE_ENTRIES`].
///
/// So this budget is still sized to bound a pathological queue rather than to
/// describe a normal one — real queues hold a handful of short messages waiting
/// on a handshake. Lowering it below `MAX_MESSAGE_CONTENT_BYTES +
/// MAX_RICH_EXTRAS_BYTES` breaks admission outright.
pub(crate) const MAX_PENDING_MESSAGE_BYTES_PER_PEER: usize = 2 * 1024 * 1024;

/// Maximum total serialized bytes of the pending-session queue across all
/// peers. Bounds the whole feature's footprint on a mobile device regardless of
/// how many peers are mid-establishment.
pub(crate) const MAX_PENDING_MESSAGE_BYTES_GLOBAL: usize = 16 * 1024 * 1024;

/// Maximum size of a single persisted protocol-state record, in bytes.
///
/// Enforced on both sides of [`crate::ProtocolStateStorage`]: the SDK refuses to
/// write a larger record, and refuses to *deserialize* a larger one on restore
/// (dropping it instead), so a corrupted or tampered state file cannot turn
/// into an unbounded allocation during initialization.
///
/// Sized above [`MAX_PENDING_MESSAGE_BYTES_PER_PEER`] with room for JSON and
/// seal overhead. That was once tight rather than generous: under the
/// per-recipient pending layout the largest legitimate record *was* one peer's
/// full queue, so the two constants were one bound apart. Now that pending
/// messages are keyed per id the largest legitimate record is a single message
/// ([`MAX_MESSAGE_CONTENT_BYTES`] plus [`MAX_RICH_EXTRAS_BYTES`]) or one outbox
/// entry, and the headroom is real. Keep the ordering anyway — a legacy
/// per-recipient record still has to be readable for long enough to migrate it.
pub(crate) const MAX_PROTOCOL_STATE_RECORD_BYTES: usize = 4 * 1024 * 1024;

/// Maximum number of peers remembered in `key_package_sent_to` (the peers we
/// have pushed our key package to, with when we last did).
///
/// Wire-claimed ids grow this set in lockstep with a key-package flood, so it
/// resets at capacity like [`MAX_PLAINTEXT_RECEIVE_WARNED_PEERS`]: the only cost
/// of forgetting a peer is a one-time idempotent re-send of our key package.
pub(crate) const MAX_KEY_PACKAGE_SENT_TO: usize = 1000;

/// Maximum number of peers tracked in `rekey_due_at` (the per-peer epoch-desync
/// re-key floor).
///
/// The desync classification that reaches `schedule_session_rekey` is produced
/// by OpenMLS *before* it authenticates anything, so the peer id keying this map
/// is wire-claimed. The envelope/sender binding in `SessionManager::decrypt_message`
/// already confines that id to peers we hold a session with, which bounds the map
/// on its own; this cap is the same defence-in-depth every other wire-keyed map
/// carries. Resets at capacity like [`MAX_KEY_PACKAGE_SENT_TO`] — forgetting a
/// peer only costs one extra re-key being allowed through early.
pub(crate) const MAX_REKEY_TRACKED_PEERS: usize = 1000;

/// Maximum lifetime (in milliseconds) honored for a *received* peer key
/// package's cached expiry.
///
/// `remaining_lifetime_ms` arrives on the wire unauthenticated and becomes the
/// eviction sort key for [`MAX_PENDING_KEY_PACKAGES`] (the soonest-to-expire
/// entry is evicted first). Without a ceiling, a flood of forged senders
/// claiming a maximal lifetime would pin their entries as latest-to-expire and
/// preferentially evict legitimate peers. Key packages are minted with a 30-day
/// lifetime (`DEFAULT_KEY_PACKAGE_LIFETIME_SECS` in `offline-protocol-mls`), so
/// a larger value is never legitimate. This bound is purely cache bookkeeping —
/// OpenMLS enforces real key-package validity at use time — so clamping only
/// affects when we drop the *cached* copy, never crypto correctness.
pub(crate) const MAX_KEY_PACKAGE_LIFETIME_MS: u64 = 30 * 24 * 60 * 60 * 1000;

/// Durable record that a peer has proved it runs MLS.
///
/// Written whenever a control message from that peer verifies — signature
/// valid, and the signing key derives to the address the frame claims — and
/// read back on `initialize_mls` to reseed
/// [`OfflineProtocol::encryption_capable_peers`](crate::OfflineProtocol).
///
/// The timestamp is diagnostic, not a policy input. Its predecessor
/// (`TofuEntry.last_seen_ms`) drove LRU eviction of a bounded pin store; there
/// is no eviction here, because there is no longer any per-peer *secret* whose
/// size needs bounding — only the fact, which the restore walk bounds on its
/// own terms. Keeping the field costs nothing and makes a stale store readable
/// by a human.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct EncryptionCapableEntry {
    /// Milliseconds since epoch (UTC) when this peer last proved it runs MLS.
    pub(crate) last_seen_ms: i64,
    /// Whether this peer has ever presented a control-frame signature over the
    /// freshness-bound payload (`offline-ctrl-v2`).
    ///
    /// A **ratchet**, and the only reason the freshness check cannot be
    /// side-stepped: without it an attacker replays a capture from before the
    /// peer upgraded, the older payload verifies exactly as it always did, and
    /// the whole check is bypassed by choosing an old enough recording. Once
    /// this is set, that peer's v1 control frames are refused.
    ///
    /// It rides here rather than in [`PeerCapabilities`] because the two record
    /// different things. That one holds what a peer *advertised*, is
    /// overwritten wholesale by each fresh key package, and answers "what
    /// should we send them"; this one holds what a peer *proved*, must never be
    /// cleared by anything a network attacker can cause, and answers "what will
    /// we accept from them". Storing it in a record that is routinely
    /// overwritten is how a ratchet silently stops being one.
    ///
    /// `#[serde(default)]` gives `false` for records written before the field
    /// existed, which is correct: an install that has never verified a v2
    /// signature from a peer has not proved anything about them.
    #[serde(default)]
    pub(crate) ctrl_freshness_proved: bool,
    /// The signed timestamp of the most recent session reset acted on from
    /// this peer, or `0` if none.
    ///
    /// A high-water mark, and what turns the freshness *window* into an actual
    /// closure for the one directive that destroys state. The window alone
    /// bounds a captured reset to thirty days; it does not stop the frame
    /// being spent inside them, and the receive deduplicator forgets a message
    /// id after an hour. Without this, a recording tears the pair's session
    /// down once an hour for a month.
    ///
    /// Only ever read from and written by a frame whose timestamp is *inside
    /// the signature*. On a frame signed under the older payload the timestamp
    /// is attacker-rewritable, so honouring one here would let anyone park
    /// this mark at `i64::MAX` and permanently deny that peer the ability to
    /// heal a forked session, which is a worse failure than the one being
    /// fixed.
    #[serde(default)]
    pub(crate) last_reset_ms: i64,
    /// Whether this peer has ever sent us a service frame sealed inside the
    /// 1:1 session, which proves it routes the sealed form.
    ///
    /// A **ratchet**, here rather than in [`PeerCapabilities`] for the reason
    /// `ctrl_freshness_proved` is: that record is what a peer advertised and
    /// every key package overwrites it. Kept there, one replayed key package
    /// from before the peer upgraded would erase the proof and send that
    /// peer's service bodies in signed plaintext again, and a key package
    /// signed under `offline-ctrl-v1` is admitted at any age (the key-package
    /// escape), so no freshness window bounds that replay.
    ///
    /// `#[serde(default)]` gives `false` for records written before the field
    /// existed, which is correct: nothing was proved.
    #[serde(default)]
    pub(crate) svc_sealed_proved: bool,
}

/// Payload for key package exchange, and the compact envelope version it
/// advertises.
///
/// Both live in `offline-protocol-sealed`: a leaf node builds and reads this
/// payload with a different MLS implementation, and two ends that disagree
/// about its shape cannot negotiate anything. Re-exported here so every
/// existing use site is unchanged.
pub(crate) use offline_protocol_sealed::{KeyPackagePayload, MLS_ENVELOPE_COMPACT_V1};

/// Sealed rich-payload version advertised in
/// [`KeyPackagePayload::rich_versions`]: the decrypted MLS plaintext may be
/// `__RICH_V1__` + JSON of [`RichPayloadV1`], carrying reply context, rich
/// media metadata, and forward attribution inside the AEAD boundary instead
/// of on the relay-visible outer message.
pub(crate) const RICH_PAYLOAD_V1: u8 = 1;

/// Upper bound on the serialized size of the rich extras accepted by the
/// rich send surface. Enforced at the API boundary (`send_message_with`),
/// deliberately NOT at seal time: a message queued behind session
/// establishment re-makes the seal decision at flush, and a seal-time
/// failure there would re-queue the message forever. Bounding at the
/// boundary means every queued extras blob is already known to seal.
pub(crate) const MAX_RICH_EXTRAS_BYTES: usize = 32 * 1024;

/// Replicated-document sync version advertised in
/// [`KeyPackagePayload::data_versions`]: the decrypted MLS plaintext may be
/// `__DATA_V1__` + JSON of a sync frame (version-vector exchange, delta
/// push, or snapshot offer).
///
/// One number covers both the frame schema and the document encoding the
/// frames carry. Splitting them would let a peer advertise a frame schema it
/// speaks around an encoding it cannot read, and the failure would surface
/// as an unreadable document rather than as a refused negotiation.
pub(crate) const DATA_SYNC_V1: u8 = 1;

/// Group-space replication, advertised in
/// [`KeyPackagePayload::data_versions`] alongside [`DATA_SYNC_V1`]: the
/// sender intercepts `__DATA_V1__` frames arriving inside a *group*
/// ciphertext, not only inside a 1:1 one.
///
/// A separate entry rather than a bump, and the reason is a compatibility
/// trap rather than tidiness. A build that speaks only [`DATA_SYNC_V1`]
/// advertises it, holds group sessions, and has no group interception at
/// all: every group path emits whatever decrypts as a chat message, so a
/// group sync frame sent to one would surface to its user as literal
/// `__DATA_V1__{...}` text. Gating group sends on this entry is what keeps
/// that frame from ever being sent, and appending rather than replacing is
/// what keeps 1:1 replication working with those same peers.
pub(crate) const DATA_GROUP_V1: u8 = 2;

/// Attachment and snapshot carriage over the media path, advertised in
/// [`KeyPackagePayload::data_versions`] alongside [`DATA_SYNC_V1`]: the
/// sender understands the `need_blob` and `blob_gone` frame kinds, and
/// routes a media transfer marked as data-purposed into the data layer
/// instead of surfacing it to its user as a received file.
///
/// A third entry rather than a bump, for the reason the second one exists.
/// A build speaking only [`DATA_SYNC_V1`] replicates documents correctly and
/// has no arm for either new frame kind; it consumes them (the parser reads
/// the version first and the body second, so an unknown kind is dropped
/// rather than surfaced) but answers nothing, so a fetch aimed at it waits
/// forever. Worse, such a build receiving a data-purposed media transfer
/// emits `FileReceived` and hands a person a CRDT snapshot as a downloaded
/// file. Gating on this entry is what stops both.
///
/// v1 carries blobs on 1:1 sessions only, so this entry has no attested
/// sibling: group members never exchange key packages with each other, and
/// there is nothing about group carriage for an inviter to attest.
pub(crate) const DATA_MEDIA_V1: u8 = 3;

/// Document removals, advertised in [`KeyPackagePayload::data_versions`]
/// alongside [`DATA_SYNC_V1`]: the sender reads the `gone` field of a
/// version offer, and removes what a floor covers instead of offering it
/// back.
///
/// A fourth entry rather than a bump, for the reason the second and third
/// exist, and here the compatibility story is unusually gentle. A build
/// without this entry ignores an unknown field on a frame kind it knows, so
/// it keeps its copy and re-offers it forever; this device refuses each
/// offer and each blob against its own floor, so the removal still holds
/// everywhere it is known. Nothing is surfaced wrongly and nothing loops:
/// the cost is one offer per exchange that is answered with nothing.
///
/// Gating the *field* on the entry is therefore a traffic decision rather
/// than a safety one, which is why a group carries it ungated: one
/// ciphertext reaches the whole roster, so there is no per-member choice to
/// make, and the roster-wide gate on [`DATA_GROUP_V1`] already decides
/// whether the frame may be sent at all.
pub(crate) const DATA_TOMBSTONE_V1: u8 = 4;

/// Interest-scoped replication, advertised in
/// [`KeyPackagePayload::data_versions`] alongside [`DATA_SYNC_V1`]: the
/// sender reads the `want` field of a version offer and answers only the
/// documents it names.
///
/// A fifth entry rather than a bump, and like the fourth it gates traffic
/// rather than correctness. A peer without it ignores the field and answers
/// with everything it holds, and the asking side refuses what it did not ask
/// for on arrival, so the narrowing holds regardless; what it does not do is
/// save the radio. Gating means a narrowed space says so only where saying so
/// changes what comes back.
///
/// The half that does not depend on this entry is the local refusal, and it
/// is the reason interest is safe to ship before any peer speaks it.
pub(crate) const DATA_INTEREST_V1: u8 = 5;

/// Attachment bytes inside a group, advertised in
/// [`KeyPackagePayload::data_versions`] alongside [`DATA_SYNC_V1`]: the
/// sender answers a `need_blob` that arrives inside a group ciphertext, and
/// carries the bytes back as `chunk` frames under the group key.
///
/// A sixth entry rather than a bump, and the first `data_versions` entry
/// since the second with an attested sibling. [`DATA_MEDIA_V1`] has none
/// because carriage was 1:1 and there was nothing about it for a group
/// inviter to attest; this one is about a group, so there is.
///
/// What it gates is a better answer to a question, not safety. A member
/// without it receives a `need_blob` it understands the shape of and
/// refuses, and receives `chunk` frames whose kind it does not know and
/// drops: the version is read before the body, so an unknown kind is
/// consumed rather than surfaced. Nothing is shown to anybody wrongly. What
/// the entry buys is refusing a fetch at the call instead of leaving an
/// application waiting out the silence timeout for an answer that was never
/// coming.
pub(crate) const DATA_GROUP_BLOB_V1: u8 = 6;

/// The custody receipt, advertised in [`KeyPackagePayload::data_versions`]
/// alongside [`DATA_SYNC_V1`]: the peer parses the `__CUSTODY_RECEIPT__`
/// control frame, so a custodian that accepted one of its replication frames
/// may answer with a receipt (`docs/spec/custody.md`).
///
/// It gates one frame in one direction and nothing else. A deposit request is
/// a metadata key an unaware receiver ignores, so a depositor writes it toward
/// any neighbour, and acceptance is decided by the custodian's quotas. The
/// gate exists because a peer without the entry does not know the prefix as a
/// control frame: with encryption on it refuses the receipt as inbound
/// plaintext and records a security refusal, and on a plaintext-only
/// deployment it shows the receipt to its user as a message.
///
/// In the replication family rather than a list of its own because custody
/// carries replication frames only. No attested sibling: the receipt is a 1:1
/// control frame between neighbours, and a group inviter has nothing to say
/// about one.
pub(crate) const DATA_CUSTODY_V1: u8 = 7;

/// Rich fields accepted by the `send_message_with` surface. Only ever
/// delivered inside the sealed [`RichPayloadV1`] body — toward a recipient
/// that did not advertise [`RICH_PAYLOAD_V1`] they are silently dropped,
/// never sent cleartext.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub(crate) struct RichSendExtras {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) reply_context: Option<ReplyContext>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) media_metadata: Option<MediaMetadata>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) forward_info: Option<ForwardInfo>,
}

impl RichSendExtras {
    /// Whether any rich field is present (empty extras never seal).
    pub(crate) fn is_any(&self) -> bool {
        self.reply_context.is_some() || self.media_metadata.is_some() || self.forward_info.is_some()
    }

    /// Enforces [`MAX_RICH_EXTRAS_BYTES`] on the serialized extras. Shared
    /// by every rich send boundary (`send_message_with`, the forward paths,
    /// and the group surface) — enforced there, never at seal time, so a
    /// queued or re-sent message is always known to seal (see the
    /// constant's doc). Empty extras always pass.
    pub(crate) fn check_size(&self) -> crate::Result<()> {
        if !self.is_any() {
            return Ok(());
        }
        let extras_len = serde_json::to_vec(self)
            .map_err(|e| Error::Serialization(e.to_string()))?
            .len();
        if extras_len > MAX_RICH_EXTRAS_BYTES {
            return Err(Error::InvalidArgument(format!(
                "Rich extras too large: {} bytes serialized (max {})",
                extras_len, MAX_RICH_EXTRAS_BYTES
            )));
        }
        Ok(())
    }
}

/// Options for `OfflineProtocol::send_message_with`: priority and reply
/// threading (as on `send_message`), plus the rich fields introduced with
/// the sealed rich payload.
///
/// The rich fields (`reply_context`, `media_metadata`, `forward_info`) only
/// ever travel inside the MLS-sealed `__RICH_V1__` body, and only toward
/// recipients whose key package advertised `rich_versions` support. Toward
/// anyone else they are silently dropped — never sent cleartext — so the
/// message degrades to plain text with `reply_to_msg` threading intact.
#[derive(Debug, Clone, Default)]
pub struct SendMessageOptions {
    /// Message priority (defaults to Medium).
    pub priority: Option<MessagePriority>,
    /// ID of the message this is replying to.
    pub reply_to_msg: Option<String>,
    /// Content type stamped on the outer message (defaults to Text). A
    /// coarse rendering hint — the actual content stays MLS-sealed. Toward
    /// a recipient that advertised the sealed rich payload, a copy travels
    /// inside the sealed body — whenever extras seal, or the hint itself is
    /// non-Text — and the receiver treats that copy as authoritative, so a
    /// relay cannot rewrite the hint. Must not be
    /// [`ContentType::FileChunk`] (an internal transport content type; the
    /// receiver would route the message into its file-transfer manager and
    /// drop it) — rejected as `InvalidArgument`.
    pub content_type: Option<ContentType>,
    /// Quoted-reply context, delivered sealed-only.
    pub reply_context: Option<ReplyContext>,
    /// Rich media metadata (cloud attachments, stickers — including any
    /// `encryption_key`/`iv` secrets), delivered sealed-only.
    pub media_metadata: Option<MediaMetadata>,
    /// Forward attribution, delivered sealed-only.
    pub forward_info: Option<ForwardInfo>,
    /// Send via this specific transport (bypassing DORS selection), like
    /// `send_message_via_transport`.
    pub via_transport: Option<TransportType>,
    /// Application id stamped on this message, in place of the configured
    /// [`crate::ProtocolConfig::app_id`].
    ///
    /// For an instance that serves several local applications behind one
    /// identity: the receiver's `MessageReceived.app_id` reports it, so the
    /// receiving instance can route the message to the right client. It
    /// changes the stamp on this one frame only, never the storage
    /// namespace, the MLS session, the BLE app tag, or the id on the
    /// acknowledgements and control frames the engine sends for it.
    ///
    /// It travels in the clear, outside the sealed body and outside every
    /// signature, so any carrier can read and rewrite it (threat model
    /// R17). Treat it as routing metadata, never as an authorization input.
    /// Held to the same rules as the configured id; an invalid value fails
    /// the call with `InvalidArgument` before anything is queued.
    pub app_id: Option<String>,
}

/// Options for `OfflineProtocol::send_media_with`: the chunk-0 media
/// metadata (as on `send_media`), plus the rich fields introduced with the
/// sealed rich payload and an optional caller-supplied `file_id`.
///
/// The rich fields (`caption`, `reply_to_msg`, `reply_context`,
/// `forward_info`) only ever travel inside the MLS-sealed chunk-0 plaintext
/// (v2 media envelope), and only toward recipients whose key package
/// advertised `rich_versions` support. Toward anyone else — including every
/// plaintext (encryption opt-out) transfer — they are silently dropped,
/// never sent cleartext, so the transfer degrades to what plain
/// `send_media` sends.
#[derive(Debug, Clone, Default)]
pub struct MediaSendOptions {
    /// Media metadata delivered with chunk 0 (as on `send_media`).
    pub media_metadata: Option<MediaMetadata>,
    /// Caption text, delivered sealed-only.
    pub caption: Option<String>,
    /// ID of the message this media replies to, delivered sealed-only.
    pub reply_to_msg: Option<String>,
    /// Quoted-reply context, delivered sealed-only.
    pub reply_context: Option<ReplyContext>,
    /// Forward attribution, delivered sealed-only.
    pub forward_info: Option<ForwardInfo>,
    /// Caller-supplied file id for the transfer (minted when absent). Must
    /// not collide with an active outbound transfer; bounded to the wire
    /// `file_id` field limit.
    pub file_id: Option<String>,
    /// Application id stamped on every chunk of this transfer, in place of
    /// the configured one. Same meaning, same limits and the same cleartext
    /// caveat as [`SendMessageOptions::app_id`].
    pub app_id: Option<String>,
}

/// The sealed rich body: what `__RICH_V1__` + JSON carries inside the MLS
/// plaintext. `text` is the user-visible content; the optional fields are
/// restored onto the inbound message by `apply_decrypted_content` *after*
/// its outer-field strip, making the sealed body the sole trusted carrier
/// for rich data on encrypted messages.
#[derive(Clone, Serialize, Deserialize)]
pub(crate) struct RichPayloadV1 {
    pub(crate) text: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) reply_context: Option<ReplyContext>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) media_metadata: Option<MediaMetadata>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) forward_info: Option<ForwardInfo>,
    /// Sealed copy of the outer `content_type` rendering hint — the last
    /// rich-adjacent field a relay could rewrite in transit. Additive after
    /// the body first shipped: absent from bodies sealed by older senders,
    /// in which case the outer value stands. When present it is
    /// authoritative on receive (except `FileChunk`, refused there like at
    /// the send boundary), so a relay can no longer restamp the rendering
    /// hint — or worse, restamp it `FileChunk` and get the decrypted
    /// message routed into the file-transfer manager and dropped. Fresh
    /// sends with a non-Text hint seal a hint-only body even without
    /// extras. Forwards seal their attribution and media metadata as
    /// extras toward capable recipients; only legacy queued forwards
    /// (persisted by older builds, outer-only) skip the hint-only seal,
    /// since a sealed body would wipe their outer copies on restore.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) content_type: Option<ContentType>,
}

impl RichPayloadV1 {
    /// Parses a sealed `__RICH_V1__` body out of decrypted MLS plaintext.
    ///
    /// Shared by the DM restore (`apply_decrypted_content`) and the group
    /// message inbound paths. Never capability-gated (mirroring envelope
    /// parsing): whatever a peer chose to seal, we try to read. Returns
    /// `None` when the prefix is absent or the body fails to parse (logged;
    /// callers surface the raw text rather than dropping an authenticated
    /// message). A sealed `FileChunk` content-type claim is refused here —
    /// mirroring the send boundary — so a hostile sender can never steer a
    /// decrypted message into the file-transfer manager, which would drop
    /// it.
    pub(crate) fn parse_sealed(plaintext: &str, sender: &str) -> Option<Self> {
        let sealed = plaintext.strip_prefix(super::internal_prefixes::RICH_V1)?;
        match serde_json::from_str::<Self>(sealed) {
            Ok(mut rich) => {
                if rich.content_type == Some(ContentType::FileChunk) {
                    warn!(
                        sender = %sender,
                        "Sealed rich payload claims the internal FileChunk content type, ignoring the hint"
                    );
                    rich.content_type = None;
                }
                Some(rich)
            }
            Err(e) => {
                warn!(
                    sender = %sender,
                    error = %e,
                    "Failed to parse sealed rich payload, surfacing raw text"
                );
                None
            }
        }
    }
}

/// An outbound connection request awaiting a transport outcome (see
/// `OfflineProtocol::pending_connection_requests`).
#[derive(Debug, Clone)]
pub(crate) struct PendingConnectionRequest {
    /// Recipient the request was addressed to.
    pub(crate) recipient: String,
    /// When the request was sent — entries older than
    /// [`PENDING_CONNECTION_REQUEST_TTL`] are pruned on insert.
    pub(crate) sent_at: std::time::Instant,
}

/// How long an outbound connection request stays correlatable to a
/// transport failure. Past this window the entry is pruned: a DeliveryError
/// that stale belongs to a request the app has long stopped showing a
/// spinner for.
///
/// Deliberately wider than the bridges' 60s `RecipientInFlightTracker` TTL:
/// that window anchors at the socket write, while this one anchors at
/// `send_connection_request` — a request can dwell in the internet outbox
/// (device offline, relay reconnecting) before its wire attempt, and this
/// window must cover that dwell plus the bridge's correlation window.
///
/// Also deliberately wider than the worst-case default ACK retry schedule
/// (10 retries, 1s initial delay, x2 backoff capped at 5 min, plus a 10s
/// ACK timeout per attempt — up to ~910s end to end): retry exhaustion is
/// the last settlement point that can still emit a typed undeliverable
/// event, so the window must outlive it with headroom.
pub(crate) const PENDING_CONNECTION_REQUEST_TTL: std::time::Duration =
    std::time::Duration::from_secs(1800);

/// Cap on tracked outbound connection requests (oldest evicted first).
pub(crate) const MAX_PENDING_CONNECTION_REQUESTS: usize = 64;

/// Upper bound (UTF-8 bytes) for `initial_message` on a connection request.
/// The request is a plaintext High-priority control frame; an unbounded
/// first message would fragment heavily over BLE and can exceed relay frame
/// limits after the SDK already returned a message id, so oversized input
/// fails loudly at the API instead.
pub(crate) const MAX_INITIAL_MESSAGE_BYTES: usize = 4096;

/// Payload for a connection request message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ConnectionRequestPayload {
    /// Display name of the sender.
    pub(crate) sender_name: String,
    /// Timestamp of the request (Unix ms).
    pub(crate) timestamp_ms: i64,
    /// Optional MLS key package data for encrypted session setup.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) key_package: Option<Vec<u8>>,
    /// Optional first message sent along with the request (`default` keeps
    /// payloads from pre-initial-message senders parseable).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) initial_message: Option<String>,
}

/// Payload for a connection accepted message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ConnectionAcceptedPayload {
    /// Display name of the accepting party.
    pub(crate) accepted_by_name: String,
    /// Timestamp of the acceptance (Unix ms).
    #[serde(default)]
    pub(crate) timestamp_ms: i64,
    /// Optional MLS key package data for encrypted session setup.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) key_package: Option<Vec<u8>>,
}

// --- Presence, typing, and read receipt payloads ---

/// Maximum number of message IDs allowed in a single read receipt.
pub(crate) const MAX_READ_RECEIPT_IDS: usize = 256;

/// Payload for a presence update message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct PresencePayload {
    /// Presence status.
    pub(crate) status: PresenceStatus,
    /// Timestamp of the update (Unix ms).
    pub(crate) timestamp_ms: i64,
}

/// Payload for a typing indicator message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct TypingIndicatorPayload {
    /// Opaque conversation identifier chosen by the sender (conventionally a
    /// peer address for DMs, the group id for groups). Never parsed here.
    pub(crate) conversation_id: String,
    /// Whether the user is currently typing.
    pub(crate) is_typing: bool,
    /// Timestamp of the indicator (Unix ms).
    pub(crate) timestamp_ms: i64,
}

/// Payload for a read receipt message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ReadReceiptPayload {
    /// IDs of the messages that were read.
    pub(crate) message_ids: Vec<String>,
    /// Timestamp when the messages were read (Unix ms).
    pub(crate) timestamp_ms: i64,
}

// --- Group (relay) payloads ---

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GroupCreatedPayload {
    pub(crate) group_id: String,
    pub(crate) name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GroupMessageReceivedPayload {
    pub(crate) group_id: String,
    pub(crate) sender: String,
    pub(crate) content: String,
    pub(crate) timestamp: String,
    pub(crate) message_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) reply_to_msg: Option<String>,
    /// Forwarding attribution (present when the group message was forwarded).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) forward_info: Option<offline_protocol_core::ForwardInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GroupMemberAddedPayload {
    pub(crate) group_id: String,
    pub(crate) user_id: String,
    pub(crate) added_by: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) group_name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GroupMemberRemovedPayload {
    pub(crate) group_id: String,
    pub(crate) user_id: String,
    pub(crate) removed_by: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GroupInfoMemberPayload {
    pub(crate) user_id: String,
    pub(crate) role: String,
    pub(crate) joined_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GroupInfoPayload {
    pub(crate) group_id: String,
    pub(crate) name: String,
    pub(crate) created_by: String,
    pub(crate) created_at: String,
    pub(crate) members: Vec<GroupInfoMemberPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct UserGroupSummaryPayload {
    pub(crate) group_id: String,
    pub(crate) name: String,
    pub(crate) created_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct UserGroupsPayload {
    pub(crate) groups: Vec<UserGroupSummaryPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GroupErrorPayload {
    /// The relay's own wording. **Never emitted on an event** — see
    /// [`GroupErrorPayload::classify_reason`]. Treat every read of this
    /// field as a read of untrusted, unbounded wire input.
    pub(crate) reason: String,
    /// Group the error concerns, when the relay scoped it (e.g. a
    /// registration sync denial). Used to drop the group from
    /// `relay_synced` so sends fall back to per-member delivery.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) group_id: Option<String>,
}

impl GroupErrorPayload {
    /// Maps the relay's free-text `reason` onto a fixed local vocabulary.
    ///
    /// `__GROUP_ERROR__` is a relay answer: it rides
    /// `RELAY_ANSWER_PREFIXES`, so on the relay ingest shape (Internet
    /// arrival, no transport peer identity) it is accepted **unsigned** —
    /// no key material, no session, no prior contact, just a frame on that
    /// socket. Signed, it is reachable to any peer over any transport, since
    /// the handler is deliberately not gated on the Internet path the way
    /// the `GROUP_CREATED` ack is. Either way `reason` is whatever the
    /// sender wrote: arbitrary content, arbitrary length, not ours.
    ///
    /// So the text does not travel any further. Nothing downstream needs it
    /// — the sync revocation keys off `group_id`, and the platform bridges
    /// already dual-emit the raw relay frame on the server-message channel
    /// for apps that want the relay's exact wording. What an event carries
    /// is this classification instead, which an attacker can at most steer
    /// between three harmless codes.
    ///
    /// Even an *honest* relay makes this necessary: its `Not a member of
    /// group {id}` renders an identifier into prose, smuggling past a
    /// telemetry scrubber that hashes `group_id` fields but ships free text
    /// verbatim by design.
    ///
    /// The `&'static str` return is load-bearing, the same way
    /// `MlsError::privacy_safe_reason` is: it makes interpolating wire input
    /// unrepresentable rather than merely discouraged. Matching is exact and
    /// the fallback is closed, so a relay rewording an error degrades to
    /// `error` — never back to shipping its text.
    pub(crate) fn classify_reason(&self) -> &'static str {
        match self.reason.as_str() {
            // Relay has no such group: invite links and relay fan-out for it
            // are dead, not merely refused.
            "Group not found" => "not_found",
            // Relay refused to register/sync the group for this caller.
            "Only admins can sync this group" => "sync_denied",
            // Everything else, including anything a forged frame chose.
            _ => "error",
        }
    }
}

/// A received key package awaiting use for session creation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ReceivedKeyPackage {
    /// Raw MLS key package bytes.
    pub(crate) key_package_data: Vec<u8>,
    /// Local wall-clock deadline (ms since epoch) computed from the sender's
    /// `remaining_lifetime_ms`, anchored to *our* clock at receive time.
    pub(crate) local_expires_at_ms: u64,
}

/// Delivery ACKs held for one peer until they go out as one frame (see
/// `OfflineProtocol::coalesced_acks_out`).
#[derive(Debug, Clone)]
pub(crate) struct CoalescedAcks {
    /// The carrier the answered frames arrived on, which the ACK goes back
    /// over. A frame from another carrier starts a new batch.
    pub(crate) transport: offline_protocol_transport::TransportType,
    /// When the first held ACK was owed.
    pub(crate) since: std::time::Instant,
    /// The first answered frame's hop count, as a single ACK reports it.
    pub(crate) hop_count: String,
    /// The answered message ids, oldest first.
    pub(crate) ids: Vec<String>,
}

/// Durable record of the end-to-end capability versions a peer last
/// advertised in its key package (`env_versions` / `rich_versions`).
///
/// Persisted separately from [`ReceivedKeyPackage`] because the cached key
/// package is deleted once a session is established, while the capabilities
/// must survive restarts for exactly those peers: mobile apps restart
/// constantly and MLS sessions persist, so without this record a rich send
/// right after relaunch silently degrades to bare text (and the compact
/// envelope to JSON) until the next live key-package exchange.
///
/// Stores the raw advertised versions, not the config-gated subset: the kill
/// switches (`compact_envelope_enabled` / `rich_payload_enabled`) gate use —
/// live recording, restore, and send — not knowledge, so toggling one across
/// restarts behaves the same as toggling it live.
///
/// `wire_versions` is deliberately absent: it is hop-local (which frames a
/// directly-connected peer decodes), and connection setup re-exchanges key
/// packages on discovery anyway, so persisting it would buy nothing.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub(crate) struct PeerCapabilities {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) env_versions: Vec<u8>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) rich_versions: Vec<u8>,
    /// Sync versions the peer advertised, from
    /// [`KeyPackagePayload::data_versions`]. Persisted for the same reason
    /// as the two above: without it a document edited right after relaunch
    /// would not replicate until the next live key-package exchange, and a
    /// sync that silently does not happen looks exactly like a peer with
    /// nothing to say.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) data_versions: Vec<u8>,
    /// Rich-payload versions a *group inviter* attested for this peer
    /// (carried on the group Add commit / Welcome), as opposed to the
    /// direct self-advertised `rich_versions` above. Kept separate because
    /// the trust differs: attestation is third-party and may be stale, so a
    /// direct key-package exchange overwrites the whole record (clearing
    /// this field) — direct knowledge is always authoritative. Consulted
    /// only by the group seal gate, never by DM sealing or envelope
    /// selection.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) attested_rich_versions: Vec<u8>,
    /// Sync versions a *group inviter* attested for this peer, carried on
    /// the group Add commit / Welcome exactly like
    /// [`Self::attested_rich_versions`] and kept separate from the direct
    /// `data_versions` above for the same reason: attestation is
    /// third-party and may be stale, so a direct key-package exchange
    /// overwrites the whole record and clears this field.
    ///
    /// Group members never exchange key packages with each other, so
    /// without this a group would replicate only among the subset of
    /// members that happen to have met directly, and the rest would sit
    /// there editing documents nobody receives. Consulted only by the
    /// group replication gate, never by 1:1 sync (which always has direct
    /// knowledge: session establishment exchanges key packages).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) attested_data_versions: Vec<u8>,

    /// Control-frame signing versions the peer advertised, from
    /// [`KeyPackagePayload::ctrl_versions`]. Persisted for the same reason as
    /// the lists above: without it, a peer met before a restart would be sent
    /// the older signing payload until the next live key-package exchange, and
    /// a peer that has *proved* it signs the newer one refuses that.
    ///
    /// This is the advertised half only. What the peer proved lives in
    /// [`EncryptionCapableEntry::ctrl_freshness_proved`], deliberately in a
    /// record this one's wholesale overwrite cannot reach.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) ctrl_versions: Vec<u8>,

    /// Sealed service frame versions the peer advertised, from
    /// [`KeyPackagePayload::svc_versions`], recorded only from a signed key
    /// package. Persisted for the same reason as the lists above: without it
    /// a peer met before a restart would be sent its service bodies in signed
    /// plaintext until the next live key-package exchange.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) svc_versions: Vec<u8>,

    /// Delivery-ACK statuses the peer reads, from
    /// [`KeyPackagePayload::ack_versions`]. Persisted for the same reason as
    /// the lists above: a peer met before a restart would otherwise get no
    /// `undecryptable` answer until its next key package, and a relay mailbox
    /// would keep redelivering its dead frames meanwhile.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) ack_versions: Vec<u8>,

    /// The peer's Nostr public key, from [`KeyPackagePayload::nostr_pubkey`].
    ///
    /// Persisted for the same reason as the capability lists: the cached key
    /// package is deleted once a session is established, and a peer met before
    /// a restart would otherwise silently fall back to bootstrap-key sealing —
    /// a privacy regression with no symptom — until the next live exchange.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) nostr_pubkey: Option<String>,
}

/// Length of an x-only secp256k1 public key in lowercase hex.
pub(crate) const NOSTR_PUBKEY_HEX_LEN: usize = 64;

/// Validates a wire-supplied Nostr public key before it is persisted or used
/// as a sealing destination.
///
/// Shape only — the value is signature-bound to the sender, so this is not an
/// authenticity check. It exists so a malformed or oversized string cannot
/// enter a durable record, and it normalizes case so the same key never
/// persists under two spellings.
pub(crate) fn normalize_nostr_pubkey(candidate: &str) -> Option<String> {
    if candidate.len() != NOSTR_PUBKEY_HEX_LEN || !candidate.bytes().all(|b| b.is_ascii_hexdigit())
    {
        return None;
    }
    Some(candidate.to_ascii_lowercase())
}

/// Cap on how many advertised version entries persist per capability list
/// in a [`PeerCapabilities`] record. The lists are unauthenticated wire
/// input stored raw (raw so unknown future versions survive a local
/// upgrade), and a hostile advertiser could otherwise bloat each durable
/// record up to the transport message size. Truncation only hurts the
/// advertiser — real senders list a handful of versions.
pub(crate) const MAX_PERSISTED_CAPABILITY_VERSIONS: usize = 8;

impl PeerCapabilities {
    /// Builds a record from the wire-advertised version lists, truncating
    /// each to [`MAX_PERSISTED_CAPABILITY_VERSIONS`].
    pub(crate) fn from_advertised(
        env_versions: &[u8],
        rich_versions: &[u8],
        data_versions: &[u8],
        ctrl_versions: &[u8],
        svc_versions: &[u8],
        ack_versions: &[u8],
        nostr_pubkey: Option<&str>,
    ) -> Self {
        Self {
            env_versions: env_versions
                .iter()
                .copied()
                .take(MAX_PERSISTED_CAPABILITY_VERSIONS)
                .collect(),
            rich_versions: rich_versions
                .iter()
                .copied()
                .take(MAX_PERSISTED_CAPABILITY_VERSIONS)
                .collect(),
            data_versions: data_versions
                .iter()
                .copied()
                .take(MAX_PERSISTED_CAPABILITY_VERSIONS)
                .collect(),
            ctrl_versions: ctrl_versions
                .iter()
                .copied()
                .take(MAX_PERSISTED_CAPABILITY_VERSIONS)
                .collect(),
            svc_versions: svc_versions
                .iter()
                .copied()
                .take(MAX_PERSISTED_CAPABILITY_VERSIONS)
                .collect(),
            ack_versions: ack_versions
                .iter()
                .copied()
                .take(MAX_PERSISTED_CAPABILITY_VERSIONS)
                .collect(),
            attested_rich_versions: Vec::new(),
            attested_data_versions: Vec::new(),
            nostr_pubkey: nostr_pubkey.and_then(normalize_nostr_pubkey),
        }
    }

    /// Whether any capability is advertised. Empty records are deleted
    /// rather than stored — the durable side of the downgrade semantics.
    pub(crate) fn is_any(&self) -> bool {
        !self.env_versions.is_empty()
            || !self.rich_versions.is_empty()
            || !self.data_versions.is_empty()
            || !self.ctrl_versions.is_empty()
            || !self.svc_versions.is_empty()
            || !self.ack_versions.is_empty()
            || !self.attested_rich_versions.is_empty()
            || !self.attested_data_versions.is_empty()
            || self.nostr_pubkey.is_some()
    }
}

/// Which canonical payload a control frame's signature verified against.
///
/// Both are legitimate: [`Self::V1`] is what every peer signed before the
/// freshness-bound payload existed, and refusing it outright would refuse
/// first contact with any peer that has not upgraded. What the distinction
/// buys is that the two can be treated differently once a peer has *proved* it
/// can produce the newer one.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ControlSigVersion {
    /// `offline-ctrl-v1`: sender, id, recipient, content. States no freshness,
    /// so a signature over it never expires.
    V1,
    /// `offline-ctrl-v2`: the four fields above plus the frame's timestamp.
    V2,
}

/// What verifying a control frame's signature established.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ControlVerification {
    /// No signature metadata at all. The caller decides policy; for a
    /// security-gated prefix that policy is refusal.
    Unsigned,
    /// A signature verified, and the key that made it derives to the address
    /// the frame claims to come from.
    Verified(ControlSigVersion),
}

/// Outcome of [`OfflineProtocol::security_gate_control_message`].
///
/// [`OfflineProtocol`]: crate::OfflineProtocol
/// [`OfflineProtocol::security_gate_control_message`]: crate::OfflineProtocol
#[derive(Debug)]
pub(crate) enum ControlGateOutcome {
    /// The message may proceed to dispatch.
    ///
    /// `signed` is `true` **only** when an Ed25519 signature was present,
    /// verified, and produced by the key the sender's address derives from. It
    /// is deliberately `false` for both of the other ways a message reaches
    /// dispatch — a relay-originated answer, which no peer signs
    /// (`RELAY_ANSWER_PREFIXES`), and a prefix the gate does not cover at all —
    /// so a handler that keys on it fails closed without having to know which
    /// case it is in.
    ///
    /// Since unsigned control traffic is now refused outright, `false` no
    /// longer reaches any *gated* handler; the bit survives because those two
    /// ungated paths still produce it, and because a handler that treats a
    /// payload field as authenticated should say so at the point it does.
    ///
    /// `freshness_bound` is the strictly narrower fact: the signature verified
    /// against the payload that covers the frame's timestamp, **and** that
    /// timestamp was inside the window this node allows. It is what a handler
    /// consults before acting on a directive that destroys state, because such
    /// a directive on a frame that could be a recording is a replayable way to
    /// destroy that state (issue 403).
    ///
    /// It is deliberately the *fact* and not the *policy*: it stays `false`
    /// when freshness enforcement is switched off, and the handler applies the
    /// switch itself. A flag that folded the config in would read as "this
    /// frame is fresh" while meaning "nobody looked", which is the reading that
    /// gets copied to the next call site.
    Proceed { signed: bool, freshness_bound: bool },
    /// The gate rejected the message; the caller must return this result
    /// without dispatching.
    Rejected(InternalMessageResult),
}

/// Result of processing an internal protocol message.
#[derive(Debug)]
pub(crate) enum InternalMessageResult {
    /// Message was consumed internally (don't surface to app).
    Consumed,
    /// Message was rejected by the security gate (spoofed sender, bad
    /// signature, unsigned control traffic, or a signing key that does not
    /// derive to the claimed sender address). Like `Consumed`, the message is not
    /// surfaced to the app — but unlike `Consumed`, a delivery ACK must NOT
    /// be sent back, to avoid confirming to the attacker that the target is
    /// online and processing messages.
    SecurityRejected,
    /// Message was not delivered, but the sender can still recover it by
    /// resending. Unlike `Consumed`, a delivery ACK must NOT be sent and the id
    /// must NOT stay dedup-marked: the message is provably not delivered, so
    /// the receiver must leave the sender's retry lever intact. The receive
    /// loop responds by unmarking the id (so a resend re-enters processing
    /// instead of hitting the duplicate re-ACK path) and skipping the ACK.
    ///
    /// Two conditions produce it:
    ///
    /// - **Session not ready**: the MLS session/group is not established yet,
    ///   so the frame is queued for delayed decryption
    ///   (`enqueue_pending_decryption`). The queued copy is surfaced — and the
    ///   id re-marked — once the session confirms and the queue drains
    ///   (`process_pending_decryption`), which also sends the deferred delivery
    ///   ACK directly on the recorded arrival transport (so a sender that gave
    ///   up before the session confirmed still learns of delivery).
    /// - **Envelope parse failure** with `crypto_recovery_enabled`: the
    ///   `__MLS_ENC__` payload did not parse in any envelope form, so there is
    ///   no ciphertext to decrypt. Not queued (an unparseable frame can never
    ///   become parseable) and not answered, since for an injector silence
    ///   reveals less than an answer. Recovery is the sender's resend.
    ///
    /// See `docs/state-machines/delivery-and-acks.md` for the
    /// deferred-acknowledgement atom and the decrypt-failure classification.
    Deferred,
    /// The frame can never decrypt, though the message behind it may: an epoch
    /// desync (sealed to an epoch that is not ours, and a re-key is scheduled)
    /// or a crypto/transport failure with `crypto_recovery_enabled` (OpenMLS
    /// spent the ratchet generation on the failed attempt). Not queued.
    ///
    /// Handled like `Deferred` (id unmarked, no delivery ACK) except that
    /// the frame is answered with an `undecryptable` ACK
    /// ([`crate::constants::ACK_STATUS_UNDECRYPTABLE`]) when it arrived over
    /// the internet relay and its sender advertised that it reads one; any
    /// other frame gets no answer. Silence left a relay
    /// holding the frame to redeliver it on every connect, failing again each
    /// time; the answer lets the relay drop it and tells the sender to resend
    /// the message once, re-sealed under its current session when it can.
    Undecryptable,
    /// Message was decrypted, here's the plaintext.
    Decrypted(String),
}

/// Outcome of routing an inbound file-chunk message through
/// [`OfflineProtocol::handle_incoming_file_chunk`]. Distinguishes chunks that
/// were dealt with terminally (decrypted/assembled, or dropped for a permanent
/// reason) from chunks queued for delayed decryption — so the receive loop can
/// defer the ACK for the latter, exactly like the text `Deferred` path.
///
/// [`OfflineProtocol::handle_incoming_file_chunk`]: crate::OfflineProtocol
///
/// `#[must_use]`: the ACK/defer decision hinges on this outcome. Dropping it on
/// the floor silently reverts to the pre-deferred-ACK behavior (always ACK,
/// leave dedup-marked), reintroducing the queue-path silent-loss bug — so every
/// caller must branch on it.
#[must_use]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ChunkOutcome {
    /// The chunk was decrypted/assembled or dropped for a terminal reason
    /// (parse failure, resource limit, permanent refusal). The sender should
    /// stop retrying either way, so the caller ACKs as before.
    Handled,
    /// The chunk was not delivered but the sender can still recover it by
    /// resending: it could not be decrypted yet (session not ready, so queued
    /// for delayed decryption), or it is undecryptable as it stands (epoch
    /// desync, or a crypto failure while `crypto_recovery_enabled`), or its
    /// media envelope did not decode at all (also `crypto_recovery_enabled`) —
    /// the latter shapes dropped without queueing. The caller must NOT ACK and
    /// must unmark the id, so the sender keeps retrying and the resend re-enters
    /// processing. For an undecryptable or undecodable chunk that recovery is
    /// the media outbox's `MediaResendRequired` path — chunks are re-encoded,
    /// never re-sealed.
    Deferred,
    /// The chunk was refused as illegitimate rather than undeliverable. Two
    /// shapes: an unencrypted chunk refused by the encryption policy, and an
    /// encrypted chunk that failed its identity binding — the envelope named
    /// another pair's session slot, or the MLS credential authenticated a
    /// different sender than the wire envelope claims.
    ///
    /// Like [`InternalMessageResult::SecurityRejected`] for text, the caller
    /// must NOT ACK (don't confirm to an injector that the target processes
    /// their messages) and must unmark the id (so a replay re-enters this gate
    /// instead of the duplicate re-ACK path), matching the plaintext-text
    /// rejection in the receive loop.
    Rejected,
}

/// Pending message waiting for session establishment.
#[derive(Clone, Serialize, Deserialize)]
pub(crate) struct PendingMessage {
    /// Original plaintext content.
    pub(crate) content: String,
    /// Message priority.
    pub(crate) priority: MessagePriority,
    /// Message ID (preserved from initial creation).
    pub(crate) message_id: MessageId,
    /// Reply-to message ID if applicable.
    pub(crate) reply_to_msg: Option<MessageId>,
    /// Forwarding attribution (preserved so it survives the pending queue).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) forwarded_from: Option<ForwardInfo>,
    /// Content type of the original message (preserved for forwarded non-text messages).
    #[serde(default)]
    pub(crate) content_type: ContentType,
    /// Media metadata (preserved for forwarded media messages).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) media_metadata: Option<MediaMetadata>,
    /// Option-borne rich extras from the rich send surface. Kept separate
    /// from the legacy `forwarded_from`/`media_metadata` fields above: those
    /// flush as outer cleartext (shipped forward behavior), while these must
    /// flush inside the sealed rich body or be dropped — never cleartext.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) rich: Option<RichSendExtras>,
    /// Per-send application id from the rich send surface, `None` for the
    /// configured one.
    ///
    /// Kept on the entry because the flush rebuilds the outer message from
    /// this record: without it, a send parked while the session was not
    /// ready would go out under the configured id and be routed to the
    /// wrong application on the receiving instance. Serde-defaulted, so a
    /// record queued before the field existed restores as `None`, which is
    /// the id it was queued with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) app_id: Option<String>,
    /// When the message *first* entered the pending queue.
    ///
    /// Drives `pending_message_max_lifetime_ms`, and is deliberately preserved
    /// across a re-queue (see [`PendingProvenance`]): a flush that finds the
    /// session still unavailable puts the entry back, and stamping a fresh
    /// timestamp there would let repeated reconciliation renew an "absolute"
    /// lifetime forever.
    pub(crate) queued_at: DateTime<Utc>,
    /// Serialized footprint of this entry, for the pending-queue byte budgets
    /// ([`MAX_PENDING_MESSAGE_BYTES_PER_PEER`] /
    /// [`MAX_PENDING_MESSAGE_BYTES_GLOBAL`]).
    ///
    /// Derived, so it is not persisted; it is (re)computed by
    /// [`PendingMessage::measure`] at the two points an entry comes into
    /// existence — admission and load-from-storage — and then simply travels
    /// with the entry as it is moved between the queue, a flush, and a
    /// re-queue.
    #[serde(skip)]
    pub(crate) serialized_bytes: usize,
}

/// One persisted pending-queue record: a single queued message plus the peer
/// it is queued for.
///
/// The recipient rides *inside* the record because the key is the message id,
/// and the in-memory queue is a map keyed by recipient that has to be rebuilt
/// from records read in whatever order the store enumerates them. Same shape as
/// [`OutboxEntry`], whose recipient likewise travels in the record (inside its
/// `Message`) rather than in the key.
///
/// Deliberately a wrapper rather than a `recipient` field on [`PendingMessage`]:
/// in memory the recipient is already the map key, and a second copy there
/// would be free to drift from it.
#[derive(Serialize, Deserialize)]
pub(crate) struct PendingMessageRecord {
    pub(crate) recipient: String,
    pub(crate) message: PendingMessage,
}

/// One inbound encrypted frame parked in the pending-decryption queue, as
/// persisted under [`storage_keys::PENDING_DECRYPT_ENTRIES`].
///
/// The queue itself is in-memory only (`PendingDecryptionQueue`); this record
/// is what lets an entry survive an app restart. Without it a frame that
/// arrived before its session was ready was lost the moment the process died
/// — and with a relay that pushes ciphertext without store-and-forward there
/// is no second copy to ask for, so a restart during a slow handshake was a
/// silently lost message.
///
/// Keyed by message id, like [`PendingMessageRecord`], so a drain, a prune or
/// an overflow drop deletes exactly the record it settled. `peer_id` rides
/// inside the record because the in-memory queue is a per-peer map rebuilt
/// from records the store enumerates in no particular order.
///
/// `first_received_at_ms` is wall-clock, not the `Instant` the in-memory entry
/// carries: an `Instant` does not survive a restart, so on restore the entry is
/// re-stamped with a fresh `Instant::now()` (its in-memory TTL restarts) and
/// this field bounds the *total* time on disk instead —
/// `PENDING_DECRYPT_PERSISTED_MAX_AGE_MS`.
///
/// `version` is a forward-compatibility hinge: a reader that sees a version it
/// does not know treats the record as corrupt and drops it rather than
/// guessing at fields. Records written before the field existed default to 1.
#[derive(Serialize, Deserialize)]
pub(crate) struct PendingDecryptRecord {
    #[serde(default = "pending_decrypt_record_version")]
    pub(crate) version: u8,
    pub(crate) peer_id: String,
    pub(crate) message: Message,
    pub(crate) first_received_at_ms: i64,
    #[serde(default)]
    pub(crate) received_via: Option<TransportType>,
}

/// The only record version this build writes or reads.
pub(crate) const PENDING_DECRYPT_RECORD_VERSION: u8 = 1;

fn pending_decrypt_record_version() -> u8 {
    PENDING_DECRYPT_RECORD_VERSION
}

/// One frame in custody, persisted under [`storage_keys::CUSTODY`].
///
/// The frame as it arrived with the deposit request removed and the hop
/// fields as the forwarding path adjusted them; the depositor, which is both
/// the frame's sender and the peer it arrived from; the wall-clock acceptance
/// time in Unix milliseconds, never a monotonic instant; and the class token.
/// `version` is the same forward-compatibility hinge as
/// [`PendingDecryptRecord::version`].
#[derive(Serialize, Deserialize)]
pub(crate) struct CustodyRecord {
    #[serde(default = "custody_record_version")]
    pub(crate) version: u8,
    pub(crate) depositor: String,
    pub(crate) message: Message,
    pub(crate) accepted_at_ms: i64,
    pub(crate) class: String,
}

/// The only custody record version this build writes or reads.
pub(crate) const CUSTODY_RECORD_VERSION: u8 = 1;

fn custody_record_version() -> u8 {
    CUSTODY_RECORD_VERSION
}

impl PendingMessage {
    /// Recomputes [`Self::serialized_bytes`] from the current field values.
    ///
    /// Measures what persistence actually costs (the serialized entry) rather
    /// than estimating from `content.len()`, so rich extras and forward
    /// attribution are counted. A serialization failure — which would also make
    /// the entry unpersistable — falls back to the content length so the entry
    /// still consumes budget rather than reading as free.
    pub(crate) fn measure(&mut self) {
        self.serialized_bytes = serde_json::to_vec(self)
            .map(|encoded| encoded.len())
            .unwrap_or_else(|_| self.content.len());
    }
}

/// Identity carried by an outbound message that is re-entering the
/// pending-session queue rather than being queued for the first time.
///
/// Both fields exist to keep a re-queue from looking like a fresh send: the id
/// so `MessageSent`/`MessageDelivered`/`MessageFailed` stay correlatable with
/// what `send_message*` returned, and the timestamp so the absolute pending
/// lifetime is measured from first entry.
#[derive(Debug, Clone)]
pub(crate) struct PendingProvenance {
    /// The id the caller already holds.
    pub(crate) message_id: MessageId,
    /// When the message first entered the pending queue, or `None` when the
    /// re-queue does not come *from* that queue — the resend re-seal path
    /// passes an outbox id, and an entry it (all but unreachably) enqueues is
    /// starting its pending lifetime now.
    pub(crate) first_queued_at: Option<DateTime<Utc>>,
}

impl PendingProvenance {
    /// Provenance for a message being put back into the pending queue.
    pub(crate) fn requeued(message: &PendingMessage) -> Self {
        Self {
            message_id: message.message_id.clone(),
            first_queued_at: Some(message.queued_at),
        }
    }

    /// Provenance for a known id with no pending-queue history.
    pub(crate) fn for_id(message_id: MessageId) -> Self {
        Self {
            message_id,
            first_queued_at: None,
        }
    }
}

/// Durable state for a peer MLS session.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum SessionState {
    Pending,
    Confirmed,
}

impl SessionState {
    pub(crate) fn as_str(&self) -> &'static str {
        match self {
            Self::Pending => "Pending",
            Self::Confirmed => "Confirmed",
        }
    }
}

/// Durable lifecycle states for outbound Welcome delivery.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum WelcomeDeliveryState {
    Created,
    SendAttempted,
    Sent,
    Failed,
    Expired,
}

impl WelcomeDeliveryState {
    pub(crate) fn as_str(&self) -> &'static str {
        match self {
            Self::Created => "Created",
            Self::SendAttempted => "SendAttempted",
            Self::Sent => "Sent",
            Self::Failed => "Failed",
            Self::Expired => "Expired",
        }
    }
}

/// Per-peer throttle for presence-driven welcome rescue (see
/// `OfflineProtocol::on_peer_presence`). Deliberately in-memory only: a
/// restart resets the backoff, and the one free rescue that buys is useful
/// after a restart anyway.
#[derive(Debug, Clone)]
pub(crate) struct PresenceRescueThrottle {
    pub(crate) next_allowed_at: DateTime<Utc>,
    /// Consecutive rescues without the session confirming; drives the
    /// exponential backoff exponent.
    pub(crate) rescues: u32,
}

/// Durable metadata for outbound Welcome reliability handling.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct WelcomeLifecycleRecord {
    pub(crate) peer_id: String,
    pub(crate) group_id: String,
    pub(crate) state: WelcomeDeliveryState,
    pub(crate) attempt: u32,
    /// Consecutive `PeerUnreachable` parks (relay `DeliveryError` verdicts)
    /// since the last reachability edge; drives the escalating retry
    /// interval capped at [`WELCOME_UNREACHABLE_RETRY_CAP_SECS`]. Reset on
    /// re-arm (presence online / neighbor discovered). Defaulted for
    /// records persisted before the field existed.
    #[serde(default)]
    pub(crate) unreachable_parks: u32,
    pub(crate) welcome_message: Message,
    pub(crate) next_retry_at: Option<DateTime<Utc>>,
    pub(crate) last_reason_code: Option<crate::events::WelcomeReasonCode>,
    pub(crate) last_transport_error: Option<String>,
    pub(crate) created_at: DateTime<Utc>,
    pub(crate) expires_at: DateTime<Utc>,
}

/// Storage key types for message persistence.
pub(crate) mod storage_keys {
    /// Legacy key type for pending encrypted messages, keyed by *recipient*,
    /// each record holding that peer's whole queue.
    ///
    /// Superseded by [`PENDING_MESSAGE_ENTRIES`]. Nothing writes this category
    /// any more: `restore_pending_messages` reads it, migrates each queue into
    /// the per-message layout, and deletes the record. It is read-only
    /// migration scaffolding, and retires on the same trigger as the
    /// [`ADOPTABLE_STATE_KEY_TYPES`] sweep — an install that skips the
    /// migrating release entirely still needs it.
    pub const PENDING_MESSAGES: &str = "pending_messages";
    /// Key type for pending encrypted messages, keyed by message id — one
    /// record per queued message, like [`OUTBOX`].
    ///
    /// Keying by id rather than by recipient is what makes a lost record
    /// *individually settleable*: the id the application is holding is the key,
    /// so a record that will not open still names the message it destroyed. The
    /// per-recipient layout could only report the loss per peer, because every
    /// id was inside the record that would not open.
    pub const PENDING_MESSAGE_ENTRIES: &str = "pending_message_entries";
    /// Inbound ciphertext parked in the pending-decryption queue, one record
    /// per message keyed by message id — the receive-side mirror of
    /// [`PENDING_MESSAGE_ENTRIES`]. See `PendingDecryptRecord`.
    pub const PENDING_DECRYPT_ENTRIES: &str = "pending_decrypt_entries";

    /// Frames held in custody for a neighbour (`docs/spec/custody.md`), one
    /// record per held frame keyed by its message id. Sealed like the
    /// pending-decrypt records: other people's ciphertext plus routing
    /// metadata about them, outliving the process. Post-split only, and
    /// absent from [`ADOPTABLE_STATE_KEY_TYPES`] for the same reason as
    /// [`PENDING_DECRYPT_ENTRIES`].
    pub const CUSTODY: &str = "custody_entries";
    /// Key type for persisted per-peer MLS session confirmation state.
    pub const SESSION_STATES: &str = "session_states";
    /// Key type for persisted per-peer received key packages (survives restart).
    pub const PEER_KEY_PACKAGES: &str = "peer_key_packages";
    /// Key type for persisted per-peer advertised capability versions
    /// (env/rich), which must outlive the key package entry above — that one
    /// is deleted once a session is established (see
    /// [`super::PeerCapabilities`]).
    pub const PEER_CAPABILITIES: &str = "peer_capabilities";
    /// Key type for persisted per-peer outbound welcome lifecycle state.
    pub const WELCOME_LIFECYCLES: &str = "welcome_lifecycles";
    /// Key type for persisted store-and-forward outbox entries, keyed by
    /// message id. Holds only main-outbox (non-media) entries so undelivered
    /// messages survive app restarts; the media (file-chunk) outbox is
    /// intentionally excluded because file transfers are not persisted and
    /// must be re-initiated by the app after a restart.
    pub const OUTBOX: &str = "outbox";
    /// Key type for persisted outbound media transfer descriptors, keyed by
    /// file id. Descriptor-only (never chunk bytes): a descriptor surviving
    /// into a restore marks a transfer the app must re-initiate, surfaced
    /// via `MediaResendRequired`.
    pub const MEDIA_DESCRIPTORS: &str = "media_descriptors";
    /// Key type for the Lamport clock value.
    pub const LAMPORT_CLOCK: &str = "lamport_clock";
    /// Key ID for the single Lamport clock entry.
    pub const LAMPORT_CLOCK_ID: &str = "current";
    /// The deduplicator's exact-mode seen set, one record for the whole set.
    /// See `DedupSeenRecord`.
    pub const DEDUP_SEEN_IDS: &str = "dedup_seen_ids";
    /// Key ID for the single seen-set record.
    pub const DEDUP_SEEN_IDS_ID: &str = "current";
    /// Key type for the durable record that a peer has proved it runs MLS.
    ///
    /// Successor to the `tofu_keys` category, which stored a pinned public key
    /// per peer. The pin is gone — an address *is* its key's hash, so nothing
    /// needs storing to check one — but the store had a second job the
    /// derivation does not do: it was the half of
    /// `OfflineProtocol::encryption_capable_peers` that survived a restart, and
    /// therefore what keeps the plaintext-downgrade gate shut for a peer whose
    /// session was torn down. That job is all this category still does, so the
    /// value is a timestamp and nothing else.
    ///
    /// A leftover `tofu_keys` record is left where it is, and no migration is
    /// written for it. Records from before the identity change are keyed by a
    /// username, so no live peer id can ever read them back — they are simply
    /// unreachable. Records written in the window *between* the identity
    /// change and the pin store's deletion are keyed by an address and would
    /// still resolve, so a peer marked capable there with no live session
    /// loses that durable mark across the upgrade: it falls back to the
    /// `is_session_confirmed` check until the next key-package exchange
    /// re-marks it. That is the fail-closed direction and it costs one
    /// exchange, which is why migrating them is not worth the code while no
    /// fleet is deployed.
    pub const ENCRYPTION_CAPABLE_PEERS: &str = "encryption_capable_peers";
    /// Key type for persisted blocked user entries.
    pub const BLOCKED_USERS: &str = "blocked_users";
    /// Key type for the persistent per-install telemetry scrub secret.
    pub const SCRUB_SECRET: &str = "scrub_secret";
    /// Key ID for the single scrub-secret entry.
    pub const SCRUB_SECRET_ID: &str = "current";
    /// Key type for the persistent per-install Nostr transport signing secret.
    pub const NOSTR_SIGNING_SECRET: &str = "nostr_signing_secret";
    /// Key ID for the single Nostr signing-secret entry.
    pub const NOSTR_SIGNING_SECRET_ID: &str = "current";
    /// Key type for the Nostr receive watermark: the newest event `created_at`
    /// (unix seconds) this install has accepted from a relay.
    ///
    /// Unlike the signing secret this is *protocol-state*, not secure storage:
    /// it is a coarse timestamp, not key material, and losing it costs one
    /// wider backfill window rather than an identity.
    pub const NOSTR_WATERMARK: &str = "nostr_watermark";
    /// Key ID for the single Nostr receive-watermark entry.
    pub const NOSTR_WATERMARK_ID: &str = "current";
    /// Key type for the Nostr key-package publication slot map: which MLS key
    /// package currently stands in each published addressable slot.
    ///
    /// Protocol state rather than secure storage, on the same reasoning as the
    /// watermark: the values are slot labels and package ids, both of which are
    /// already public in the published record itself. Losing it costs a round
    /// of republication under fresh slot ids, not an identity — the stale
    /// records left at the old slots expire with their key packages.
    pub const NOSTR_KEY_PACKAGE_SLOTS: &str = "nostr_key_package_slots";
    /// Key ID for the single Nostr publication-slot entry.
    pub const NOSTR_KEY_PACKAGE_SLOTS_ID: &str = "current";
    /// Key type for this install's published username discovery claim.
    ///
    /// Records *which* username was last published, so a profile change or a
    /// switched-off feature can retract the previous claim. Without it a
    /// renamed install leaves its old name standing in the directory
    /// indefinitely, pointing at an address that is still live — the one
    /// failure a directory must not have, since retraction is the only control
    /// a claimant holds.
    ///
    /// Protocol state rather than secure storage, on the same reasoning as the
    /// slot map: the value is a username that this install already published
    /// in a public place. Losing it costs an un-retractable stale claim, which
    /// expires only when a resolver's second hop fails.
    pub const NOSTR_DISCOVERY_CLAIM: &str = "nostr_discovery_claim";
    /// Key ID for the single discovery-claim entry.
    pub const NOSTR_DISCOVERY_CLAIM_ID: &str = "current";
    /// Key type for the per-install key that seals sensitive protocol-state
    /// records at rest.
    ///
    /// Lives in *secure* storage — it is the one piece of the protocol-state
    /// domain that must be credential-backed, because it is what gives the
    /// install-scoped container's contents their confidentiality (see
    /// [`crate::protocol::state_crypto`]).
    pub const STATE_RECORD_KEY: &str = "protocol_state_record_key";
    /// Key ID for the single protocol-state record key entry.
    pub const STATE_RECORD_KEY_ID: &str = "current";
    /// Key type for peers we are the both-create "owner" of and are awaiting a
    /// group-aware decrypt from before confirming (see
    /// [`crate::protocol::OfflineProtocol`]'s `both_create_awaiting_decrypt`).
    /// Persisted so an owner restart mid-convergence cannot let a stale plaintext
    /// probe/ack prematurely confirm and strand the peer on a divergent group.
    pub const BOTH_CREATE_AWAITING_DECRYPT: &str = "both_create_awaiting_decrypt";
    /// Key type for the marker recording that pre-split protocol state has been
    /// adopted out of secure storage (see
    /// `OfflineProtocol::adopt_legacy_protocol_state`). Lives in *protocol
    /// state* storage, so a reinstall — which drops that container — correctly
    /// re-runs the sweep against whatever the credential store still holds.
    pub const STATE_ADOPTION: &str = "protocol_state_adoption";
    /// Key ID for the single state-adoption marker entry.
    pub const STATE_ADOPTION_ID: &str = "v1";

    /// Key type for compacted replicated documents, one record per document.
    ///
    /// Key ID is `{space}/{doc}`. The space is validated by
    /// `offline_protocol_data::validate_space_name` and the document by
    /// `validate_name`; neither charset includes the separator, so the
    /// composed key always parses back into the two parts it was built
    /// from. (The space charset additionally allows `:`, so a group space
    /// can be named by its `group:<uuid>` id — inert here, since keys are
    /// parsed by stripping the space prefix and splitting on `/`.)
    ///
    /// Post-split only: deliberately absent from
    /// [`ADOPTABLE_STATE_KEY_TYPES`], which has no pre-split data to inherit
    /// for it.
    pub const DATA_DOCS: &str = "data_docs";
    /// Key type for the uncompacted commit log of a replicated document.
    ///
    /// Key ID is `{space}/{doc}/{seq:016x}`, one record per persisted commit
    /// batch. Append-only by construction: a commit writes a new record and
    /// never rewrites an existing one, which is what keeps flash churn
    /// proportional to what changed rather than to document size.
    ///
    /// Post-split only: deliberately absent from
    /// [`ADOPTABLE_STATE_KEY_TYPES`], which has no pre-split data to inherit
    /// for it.
    pub const DATA_DELTA_LOG: &str = "data_delta_log";
    /// Key type for per-space document indexes and compaction bookkeeping.
    ///
    /// Key ID is the space id. Sync watermarks join this record when
    /// replication lands; today it carries the document list and nothing
    /// else. Sequence numbers and sizes are derived from the delta records
    /// themselves at open, so this record can never disagree with the store.
    ///
    /// Post-split only: deliberately absent from
    /// [`ADOPTABLE_STATE_KEY_TYPES`], which has no pre-split data to inherit
    /// for it.
    pub const DATA_SPACES: &str = "data_spaces";
    /// Key type for per-space replication bookkeeping.
    ///
    /// Key ID is the space id. Holds the containment state the sync path
    /// needs to survive a crash: the digest of the blob currently being
    /// handed to the document engine, and the digests of blobs that were
    /// in flight when a previous run died.
    ///
    /// Kept out of [`DATA_SPACES`] deliberately. That record is a cache
    /// reconciled from `list_keys` at open, so anything in it may be
    /// rebuilt from the store at any time; this one records something the
    /// store cannot show, and rebuilding it from the listing would silently
    /// answer "nothing was in flight" after exactly the crash it exists to
    /// remember.
    ///
    /// Post-split only: deliberately absent from
    /// [`ADOPTABLE_STATE_KEY_TYPES`], which has no pre-split data to inherit
    /// for it.
    pub const DATA_SYNC: &str = "data_sync";
    /// Key type for what a replicated document's name says after a removal.
    ///
    /// Key ID is `{space}/{doc}`, the same composition [`DATA_DOCS`] uses.
    /// The value carries the removal floor (the document's version at the
    /// moment it was removed) and whether the name currently holds a
    /// document again.
    ///
    /// A separate category rather than a field on [`DATA_SPACES`] for the
    /// reason that record is a cache: it is reconciled from `list_keys` at
    /// open, so a removal recorded there would be rebuilt away by exactly
    /// the listing that still shows the records a half-finished removal
    /// left behind. It is written *before* those records are deleted, so a
    /// crash in between leaves a name this category already calls removed,
    /// and the next open finishes the job. The reverse order is the
    /// resurrection the whole mechanism exists to prevent.
    ///
    /// An absent record means a live document that has never been removed,
    /// so a store written before removals existed reads correctly with no
    /// migration.
    ///
    /// Post-split only: deliberately absent from
    /// [`ADOPTABLE_STATE_KEY_TYPES`], which has no pre-split data to inherit
    /// for it.
    pub const DATA_DOC_META: &str = "data_doc_meta";

    /// Every key type that moved from secure storage into protocol-state
    /// storage when the two domains were split.
    ///
    /// Unlike the MLS key-type set — which is open, because OpenMLS contributes
    /// its own labels through `storage_adapter.rs` — this set is closed and
    /// declared right here, which is what makes a one-shot bulk adoption
    /// possible at all (see `OfflineProtocol::adopt_legacy_protocol_state`).
    /// A new protocol-state category added *after* the split must NOT be added
    /// here: there is no pre-split data for it to inherit, and listing it would
    /// only cost a pointless enumeration of the credential store.
    ///
    /// # Removal
    ///
    /// This list, `OfflineProtocol::adopt_legacy_protocol_state`, and the
    /// `STATE_ADOPTION` marker are one-shot migration scaffolding for installs
    /// upgrading *across* the storage split. They stop doing anything once no
    /// supported install can still be running a pre-split build — an install
    /// that skips the split release entirely still needs them, so the trigger
    /// is not "one release later". Delete them only when the oldest supported
    /// upgrade path starts at or after the release that introduced the split,
    /// and delete all three together: leaving the marker behind without the
    /// sweep would make a later re-introduction silently skip itself.
    pub const ADOPTABLE_STATE_KEY_TYPES: &[&str] = &[
        BLOCKED_USERS,
        OUTBOX,
        PENDING_MESSAGES,
        SESSION_STATES,
        WELCOME_LIFECYCLES,
        PEER_KEY_PACKAGES,
        PEER_CAPABILITIES,
        MEDIA_DESCRIPTORS,
        BOTH_CREATE_AWAITING_DECRYPT,
        LAMPORT_CLOCK,
    ];
}

/// Protocol state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProtocolState {
    /// Protocol is not started.
    Stopped,
    /// Protocol is running.
    Running,
    /// Protocol is paused (background mode).
    Paused,
}

/// Shared state protected by mutex.
pub(crate) struct SharedState {
    /// Current protocol state.
    pub(crate) state: ProtocolState,

    /// Event handlers registered by the application.
    pub(crate) event_handlers: Vec<EventCallback>,

    /// Received messages queue.
    pub(crate) received_messages: VecDeque<Message>,

    /// Installed telemetry context. When present, `emit_event` additionally
    /// forwards every protocol event to `ctx.sink` as a
    /// `TelemetryRecord::Protocol`, with identifier scrubbing applied per
    /// `ctx.config`. Set via `OfflineProtocol::install_telemetry_sink`.
    pub(crate) telemetry: Option<Arc<TelemetryContext>>,

    /// Handler calls that panicked and were isolated by [`Self::emit_event`].
    /// Each one is an event at least one handler never saw, after the engine
    /// had already acted on what it reports (#514), so it is counted where a
    /// test can read it rather than only logged.
    pub(crate) handler_panics: std::sync::atomic::AtomicU64,
}

impl SharedState {
    pub(crate) fn new() -> Self {
        Self {
            state: ProtocolState::Stopped,
            event_handlers: Vec::new(),
            received_messages: VecDeque::new(),
            telemetry: None,
            handler_panics: std::sync::atomic::AtomicU64::new(0),
        }
    }

    pub(crate) fn emit_event(&self, event: Event) {
        self.notify_handlers(&event);
        self.forward_to_telemetry(&event);
    }

    /// [`Self::emit_event`] without the telemetry sink: the app's handlers
    /// see the event exactly as they always have, and the pipe never does.
    ///
    /// For engine traffic that reuses an app-facing event type but is not
    /// what that type's telemetry column counts. A delivery acknowledgement
    /// for a media chunk or an internal frame (a connection request, a key
    /// package, a Welcome, a data-sync frame) is one: forwarded, it counted
    /// as a delivered message on the dashboard, once per chunk, against a
    /// "sent" figure that never included any of them.
    pub(crate) fn emit_event_to_app(&self, event: Event) {
        self.notify_handlers(&event);
    }

    /// A message's terminal event (`MessageDelivered` or `MessageFailed`):
    /// always to the app's handlers, and to telemetry only when `telemetry`
    /// is set, which callers derive from [`OutboxEntry::app_send`]. One
    /// method so every settle site applies the rule the same way.
    pub(crate) fn emit_settlement(&self, event: Event, telemetry: bool) {
        self.notify_handlers(&event);
        if telemetry {
            self.forward_to_telemetry(&event);
        }
    }

    fn notify_handlers(&self, event: &Event) {
        // Legacy `EventCallback` handlers run first and receive the raw
        // event. This preserves the pre-telemetry contract — any app that
        // relied on `on_event` sees exactly what it used to.
        //
        // Each handler call is panic-isolated so a faulty handler cannot
        // unwind through this method while a `MutexGuard<SharedState>` is
        // live in the caller's frame — that would poison the shared-state
        // mutex and silently degrade every subsequent SDK operation.
        for handler in &self.event_handlers {
            let event_for_handler = event.clone();
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                handler(event_for_handler);
            }));
            if result.is_err() {
                // Counted, and named by type only: the event's fields carry
                // senders, peers and message content, and an embedder's own
                // subscriber would print them. The event is already settled
                // in the engine (a received message is ACKed, a delivered one
                // has left the outbox), so this is a message the app will not
                // hear about.
                self.handler_panics
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                tracing::error!(
                    event = event.telemetry_name(),
                    "EventCallback panicked; this handler missed the event. Handlers must not panic, see OfflineProtocol::on_event.",
                );
            }
        }
    }

    fn forward_to_telemetry(&self, event: &Event) {
        // Sink fan-out runs after, gated on an installed context. The pipe,
        // the one production sink, consumes every event by reference in
        // `dispatch_protocol_event` and reads no identifier from it, so it
        // never reaches the scrub below. Only a sink that declines that
        // borrowed offer does: `scrub_event` hashes its identifier fields per
        // the installed config, or with
        // `TelemetryConfig::with_scrub_ids(false)` borrows the event as is.
        //
        // Dispatch goes through `dispatch_record` so a panicking sink is
        // caught and logged rather than unwinding through the caller's live
        // `MutexGuard<SharedState>` — see the helper's docstring.
        if let Some(ctx) = &self.telemetry {
            // The gate comes first so a disabled pipe stops the clone too,
            // and the borrowed offer comes next so a sink that collects by
            // name never pays for an event it drops.
            if !ctx.enabled() || dispatch_protocol_event(&ctx.sink, event) {
                return;
            }
            let scrubbed = scrub_event::scrub_event(event, &ctx.scrubber);
            let record = TelemetryRecord::Protocol(Box::new(scrubbed.into_owned()));
            dispatch_record(&ctx.sink, &record);
        }
    }
}

/// Locks the shared state, recovering it if an earlier holder panicked.
///
/// Recovering rather than refusing is what keeps events flowing (#514). The
/// callers that emit `message_received` and `message_delivered` act first
/// (the ACK is sent, the outbox entry removed) and lock to emit afterwards, so
/// a refusal here does not undo anything: it silently drops the one event
/// that reports what was done, and then every later one too, since a std
/// mutex stays poisoned for good. Nothing under this lock is left
/// half-written by a panic that matters: handler and sink calls, the only
/// foreign code run under it, are panic-isolated in
/// [`SharedState::emit_event`], and the remaining writes are single pushes
/// and pops on a queue or a field store.
///
/// Still returns a `Result`, so the call sites' existing handling stays
/// valid, but never `Err`.
pub(crate) fn lock_shared_state(
    state: &Arc<Mutex<SharedState>>,
) -> std::result::Result<std::sync::MutexGuard<'_, SharedState>, Error> {
    Ok(state.lock().unwrap_or_else(|poisoned| {
        tracing::error!("Shared state mutex was poisoned by a panic; recovering it");
        state.clear_poison();
        poisoned.into_inner()
    }))
}

/// Provenance kept on an outbox entry so an encrypted DM can be *re-sealed*
/// against the peer's current MLS session on each resend, instead of replaying
/// the ciphertext bytes sealed at first send (which become permanently
/// undecryptable once the peer re-keys to a new epoch). Mirrors the fields the
/// pre-send [`PendingMessage`] carries, so both re-seal through the same
/// `prepare_outbound_content` chokepoint. Only populated for main-outbox
/// (non-media) encrypted DMs; absent (`None`) means verbatim replay — the
/// fallback for plaintext sends and media chunks.
///
/// **Memory-only by design.** This holds the message *plaintext*, so it is never
/// persisted (see the `#[serde(skip)]` on [`OutboxEntry::reseal`]): persisting it
/// would broaden plaintext-at-rest to every sent-but-unACKed encrypted DM for the
/// full outbox lifetime, weakening forward secrecy in exchange for only a narrow
/// cross-restart reseal benefit. After a restart the restored entry replays
/// verbatim; if that resend hits a desync, Tier 1 (un-ACK + re-key) still keeps
/// delivery honest rather than silently losing the message.
#[derive(Debug, Clone)]
pub(crate) struct OutboxReseal {
    /// Original plaintext content.
    pub(crate) content: String,
    /// Message priority.
    pub(crate) priority: MessagePriority,
    /// Reply-to message ID if applicable.
    pub(crate) reply_to_msg: Option<MessageId>,
    /// Forwarding attribution.
    pub(crate) forwarded_from: Option<ForwardInfo>,
    /// Content type of the original message.
    pub(crate) content_type: ContentType,
    /// Media metadata (cleartext-outer fallback provenance).
    pub(crate) media_metadata: Option<MediaMetadata>,
    /// Sealed-only rich extras (reply context, rich media metadata, forward
    /// info) — re-evaluated against current capability at re-seal time.
    pub(crate) rich: Option<RichSendExtras>,
    /// Where the bytes currently on the ladder sit in the session's sealing
    /// order. An acknowledgement for this message proves the peer decrypted
    /// that far, which is what lets later re-seals go ahead (see
    /// `RESEAL_GAP_BUDGET`). Set when the message is staged and on every
    /// re-seal; `None` when it could not be read.
    pub(crate) seal_mark: Option<offline_protocol_mls::SealMark>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct OutboxEntry {
    pub(crate) message: Message,
    pub(crate) attempt_count: u32,
    pub(crate) first_sent_at: DateTime<Utc>,
    pub(crate) last_sent_at: DateTime<Utc>,
    pub(crate) last_transport: Option<TransportType>,
    /// Re-seal provenance; `None` for verbatim-replay entries (plaintext or
    /// media). **Memory-only** (`#[serde(skip)]`): it carries the message
    /// plaintext, which must never be persisted (see [`OutboxReseal`]). A
    /// restored entry deserializes with `reseal: None` and therefore replays
    /// verbatim.
    #[serde(skip)]
    pub(crate) reseal: Option<OutboxReseal>,
    /// The relay has handed this frame to a device push at least once
    /// (`MessageSent { pushed: true }`, see [`SEND_FAIL_REASON_RELAY_PUSHED`]).
    ///
    /// Sticky for the entry's lifetime, and persisted, because of what a later
    /// relay verdict for the same id means. The relay remembers which
    /// `(sender, recipient, message_id)` triples it has pushed and answers a
    /// retry of one with `DeliveryError` (`already_pushed`) instead of a
    /// second notification, and the bridges cannot tell that verdict from a
    /// plain "recipient offline": both reach the core as
    /// `recipient_unreachable`. So the reachability probe a park schedules
    /// earns a `DeliveryError` fifteen seconds after every push, and without
    /// this flag the app would be told `MessageUndeliverable` about a message
    /// the push may already have delivered. With it, that verdict still
    /// re-parks the entry (the recipient is not on the relay) but emits
    /// nothing app-facing. A legacy record restores as `false`, so the first
    /// probe after upgrading emits once; nothing is lost.
    #[serde(default)]
    pub(crate) relay_pushed: bool,
    /// The application sent this message: it went through the app send path
    /// (`send_message`, `forward_message`, a rich send), which is the path
    /// that reports `MessageSent`. Internal frames share the outbox and the
    /// acknowledgement machinery but are never set.
    ///
    /// Read when the message is acknowledged, to decide whether the delivery
    /// is reported to telemetry. Telemetry's delivered count is compared with
    /// its sent count, so both must count the same messages; counting every
    /// acknowledged frame put connection requests, key packages and Welcomes
    /// in delivered and never in sent.
    ///
    /// Persisted. A legacy record restores as `false`, so a delivery restored
    /// across the upgrade still reaches `on_event` and is only left out of
    /// telemetry.
    #[serde(default)]
    pub(crate) app_send: bool,
}

#[derive(Clone)]
pub(crate) struct PendingMediaMetadataEntry {
    pub(crate) content_type: ContentType,
    pub(crate) media_metadata: Option<MediaMetadata>,
    pub(crate) last_updated_at: Instant,
    /// Sender of the file transfer (used to drain partial transfers on block).
    pub(crate) sender: String,
    /// Rich extras from the sealed chunk-0 plaintext. Never populated from
    /// wire (legacy plaintext) chunks — the sealed body is the only trusted
    /// carrier.
    pub(crate) rich_extras: Option<crate::media_envelope::MediaRichExtras>,
    /// The chunk-0 outer `Message` timestamp (wall-clock ms) — the sender's
    /// send time, surfaced on `FileReceived` for display ordering.
    pub(crate) timestamp_ms: i64,
    /// The chunk-0 outer `Message` app id, surfaced on `FileReceived` so an
    /// instance serving several applications can route the file. Without
    /// it a per-send media app id would reach the wire and stop there.
    pub(crate) app_id: String,
    /// Set when the sealed chunk-0 plaintext said this transfer belongs to
    /// the replicated-document layer. Its presence is what keeps every
    /// file-facing event of this transfer from firing: progress, completion
    /// and failure alike are the data layer's business, not a person's.
    ///
    /// Never populated from a legacy plaintext chunk. The field lives inside
    /// the sealed body precisely so an unauthenticated chunk cannot claim it.
    pub(crate) data_purpose: Option<crate::media_envelope::DataPurpose>,
}

#[derive(Clone)]
pub(crate) struct OutboundMediaTransfer {
    pub(crate) content_type: ContentType,
    pub(crate) recipient: String,
    pub(crate) pinned_transport: TransportType,
    pub(crate) total_chunks: u32,
    pub(crate) delivered_chunks: HashSet<u32>,
    pub(crate) last_updated_at: Instant,
    pub(crate) media_metadata: Option<MediaMetadata>,
    /// Rich extras sealed into chunk 0 (already capability-gated at the
    /// `send_media_with` boundary). Kept on the transfer because chunk
    /// batches are (re-)encoded via `pump_media_transfers` too.
    pub(crate) rich_extras: Option<crate::media_envelope::MediaRichExtras>,
    /// Set when this SDK started the transfer for the replicated-document
    /// layer rather than on an application's behalf, and kept here for the
    /// same reason as the extras above: `pump_media_transfers` re-encodes
    /// later batches and has only the transfer to read from.
    ///
    /// No public send surface can produce one.
    pub(crate) data_purpose: Option<crate::media_envelope::DataPurpose>,
    /// Per-send application id, `None` for the configured one. Kept here
    /// for the same reason as the extras: `pump_media_transfers` builds
    /// later chunks from the transfer alone, and a chunk stamped with the
    /// configured id would be routed to a different application than
    /// chunk 0 on the receiving instance.
    pub(crate) app_id: Option<AppId>,
}

/// Crash-scoped descriptor of an in-flight outbound media transfer.
///
/// Persisted (no chunk bytes — see commit 42d1b86's rationale: resurrected
/// chunks can never complete, and per-chunk secure-storage writes are
/// expensive) when a transfer starts and deleted whenever the in-memory
/// transfer is removed (completed, aborted, or stale-swept). A descriptor
/// found on restore therefore means the app died mid-transfer:
/// [`crate::events::Event::MediaResendRequired`] is emitted so the app can
/// re-supply the bytes via `send_media_with` under the same `file_id`,
/// validated against `file_checksum`.
#[derive(Clone, Serialize, Deserialize)]
pub(crate) struct MediaTransferDescriptor {
    pub(crate) file_id: String,
    pub(crate) recipient: String,
    pub(crate) file_name: String,
    pub(crate) file_size: u64,
    /// SHA-256 hex of the plaintext file bytes (same value every chunk
    /// carries as `FileChunk::file_checksum`).
    pub(crate) file_checksum: String,
    pub(crate) content_type: ContentType,
    /// Wall-clock start of the transfer; restore prunes by
    /// `outbox_max_lifetime_ms` age.
    pub(crate) queued_at: DateTime<Utc>,
    /// Set when the transfer belonged to the replicated-document layer.
    ///
    /// Persisted for one reason: so restore can tell these apart and NOT ask
    /// an application to re-supply them. There is no public API that could
    /// answer such a request without stripping the purpose, and a resend
    /// with the purpose stripped is a document snapshot delivered to the
    /// peer's user as a downloaded file, which is the exact outcome the
    /// capability gate and the envelope version exist to prevent, reached
    /// through this SDK's own recovery path.
    ///
    /// Serde-defaulted, so a descriptor written before this field existed
    /// still parses and reads as an ordinary transfer, which is what it was.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) data_purpose: Option<crate::media_envelope::DataPurpose>,
    /// Per-send application id of the interrupted transfer, `None` for the
    /// configured one. Persisted so `MediaResendRequired` can name the
    /// application that must re-supply the bytes; an instance serving
    /// several has no other way to know. Serde-defaulted like the purpose.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) app_id: Option<String>,
}

pub(crate) enum OutboundSendPreparation {
    Ready(String),
    Queued(MessageId),
}

/// Pins that every signing domain, live or reserved, is mutually non-prefixing.
///
/// # Why this test exists here, of all places
///
/// Four domains are in production and they live in three languages and two
/// repositories: `offline-ctrl-v1` (this crate), `offline-disc-v1` and
/// `offline-invite-v1` (the MLS crate), and `offline-relay-addr-v1` (the relay
/// server, plus hand-mirrored copies in the iOS and Android bridges). Nothing
/// can import all four, so this module is the one place they can be compared at
/// all: it is the highest crate that can see two of them, and the fourth is
/// pinned as a literal.
///
/// A fifth is reserved and covered here too, for the reason reservation exists:
/// non-prefixing is a property of the whole set, so a domain that is specified
/// but not yet emitted still has to be held against every other one. A guard
/// that watched only live domains would happily accept a new live domain that
/// prefixes a reserved one, and the collision would be discovered by the
/// implementation that finally emits it.
#[cfg(test)]
mod signing_domain_tests {
    use offline_protocol_mls::discovery::DISCOVERY_SIGN_DOMAIN;
    use offline_protocol_mls::invite::INVITE_SIGN_DOMAIN;

    use offline_protocol_sealed::{CTRL_SIGN_DOMAIN, CTRL_SIGN_DOMAIN_V2};

    /// The relay's address-proof domain.
    ///
    /// A literal because it is defined in the relay-server repository and
    /// hand-mirrored in `AddressDeclarationPolicy.swift` and
    /// `AddressDeclarationPolicy.kt` — there is no Rust constant in this
    /// workspace to import. If it ever changes there, this test does not
    /// notice, which is the accepted cost of a cross-repository constant and is
    /// why it is spelled out with this comment attached.
    const RELAY_ADDR_SIGN_DOMAIN: &[u8] = b"offline-relay-addr-v1";

    /// The gateway's address-proof domain: **reserved, not yet emitted.**
    ///
    /// Specified by `docs/spec/gateway-contract.md` and registered in the
    /// signing-domain table of `docs/spec/username-discovery.md`. No code signs
    /// under it yet, which is precisely why it is pinned before anything does:
    /// it must be chosen against the whole set, and it must stay distinct from
    /// [`RELAY_ADDR_SIGN_DOMAIN`] so a proof harvested by a hostile gateway
    /// cannot be replayed against the relay, or the reverse.
    const GATEWAY_ADDR_SIGN_DOMAIN: &[u8] = b"offline-gateway-addr-v1";

    /// No domain, live or reserved, may be a prefix of another.
    ///
    /// The canonical payload is `domain ‖ Σ(u32be(len) ‖ field)`, and the
    /// **domain itself is not length-prefixed**. So if one domain were a prefix
    /// of another, a signature made under the shorter domain could be replayed
    /// as one made under the longer: an attacker chooses a first field whose
    /// leading bytes supply the rest of the longer domain, and the two payloads
    /// become byte-identical. Length-prefixing the fields does not prevent it,
    /// because the collision happens before the first length prefix.
    ///
    /// The concrete damage this stops is recorded in the addressing work: a
    /// hostile relay that could make an address-proof signature verify as a
    /// control-frame signature would harvest a replayable control frame from
    /// every device that ever authenticated to it.
    #[test]
    fn signing_domains_are_mutually_non_prefixing() {
        let domains: [(&str, &[u8]); 6] = [
            ("offline-ctrl-v1", CTRL_SIGN_DOMAIN),
            ("offline-ctrl-v2", CTRL_SIGN_DOMAIN_V2),
            ("offline-disc-v1", DISCOVERY_SIGN_DOMAIN),
            ("offline-invite-v1", INVITE_SIGN_DOMAIN),
            ("offline-relay-addr-v1", RELAY_ADDR_SIGN_DOMAIN),
            ("offline-gateway-addr-v1", GATEWAY_ADDR_SIGN_DOMAIN),
        ];

        for (a_name, a) in domains {
            for (b_name, b) in domains {
                if a_name == b_name {
                    continue;
                }
                // Both the constant's name and its *value* are reported: a
                // failure is usually caused by an edited value, and a message
                // naming only the constant sends the reader to the wrong place.
                assert!(
                    !a.starts_with(b),
                    "signing domain {} ({:?}) is a prefix of {} ({:?}), which \
                     lets a signature made under one verify under the other",
                    b_name,
                    String::from_utf8_lossy(b),
                    a_name,
                    String::from_utf8_lossy(a)
                );
            }
        }
    }

    /// The literals are what the rest of the system, and every other
    /// implementation, actually expects. A renamed constant that still passes
    /// the non-prefix test above would silently invalidate every signature in
    /// the field, so the spellings are pinned too.
    #[test]
    fn signing_domains_have_their_published_spellings() {
        assert_eq!(CTRL_SIGN_DOMAIN, b"offline-ctrl-v1");
        assert_eq!(CTRL_SIGN_DOMAIN_V2, b"offline-ctrl-v2");
        assert_eq!(DISCOVERY_SIGN_DOMAIN, b"offline-disc-v1");
        assert_eq!(INVITE_SIGN_DOMAIN, b"offline-invite-v1");
        assert_eq!(RELAY_ADDR_SIGN_DOMAIN, b"offline-relay-addr-v1");
        assert_eq!(GATEWAY_ADDR_SIGN_DOMAIN, b"offline-gateway-addr-v1");
    }

    /// All six must be distinct, which non-prefixing already implies for
    /// unequal strings but not for equal ones: two identical domains are
    /// prefixes of each other, and the loop above skips same-name pairs.
    #[test]
    fn signing_domains_are_distinct() {
        let domains: [&[u8]; 6] = [
            CTRL_SIGN_DOMAIN,
            CTRL_SIGN_DOMAIN_V2,
            DISCOVERY_SIGN_DOMAIN,
            INVITE_SIGN_DOMAIN,
            RELAY_ADDR_SIGN_DOMAIN,
            GATEWAY_ADDR_SIGN_DOMAIN,
        ];
        for (i, a) in domains.iter().enumerate() {
            for b in domains.iter().skip(i + 1) {
                assert_ne!(a, b, "two signing domains are the same string");
            }
        }
    }
}

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

    /// Every token either producer can mint classifies to itself.
    ///
    /// This is what lets the two functions compose. A value written by
    /// `send_failure_token` is later re-read from a persisted welcome
    /// lifecycle and passed through `classify_transport_send_error`; without
    /// idempotence that round trip would degrade a precise token to the
    /// generic fallback on every emit.
    #[test]
    fn every_send_failure_token_classifies_to_itself() {
        for token in SEND_FAIL_REASON_TOKENS {
            assert_eq!(
                classify_transport_send_error(token),
                *token,
                "{token} must survive a re-classification"
            );
        }
    }

    /// The vocabulary table really is the vocabulary.
    ///
    /// `classify_transport_send_error` answers from the table, so it cannot
    /// disagree with it — but `send_failure_token` matches independently, and a
    /// token added there and forgotten here would classify to the fallback on
    /// the next round trip. Walking the transport codes catches that for the
    /// arm most likely to grow.
    #[test]
    fn transport_error_tokens_are_in_the_vocabulary() {
        use offline_protocol_transport::Error as T;
        let cases = [
            T::TransportNotAvailable("ble".into()),
            T::PeerNotReachable("bob".into()),
            T::SendFailed("x".into()),
            T::ReceiveFailed("x".into()),
            T::ConfigurationError("x".into()),
            T::SerializationError("x".into()),
            T::MessageTooLarge(2, 1),
            T::CryptoError("x".into()),
            T::Other("x".into()),
        ];
        for case in cases {
            let token = send_failure_token(&crate::Error::Transport(case.clone()));
            assert!(
                SEND_FAIL_REASON_TOKENS.contains(&token),
                "{case:?} produced {token}, which is not in SEND_FAIL_REASON_TOKENS"
            );
        }
    }

    /// A relay's prose is dropped and its token kept.
    ///
    /// The prefix is a cross-layer contract the bridges hardcode and core
    /// prefix-matches to park a DM, so it must survive verbatim; everything
    /// the relay appended to it must not.
    #[test]
    fn relay_prose_is_dropped_and_the_token_kept() {
        assert_eq!(
            classify_transport_send_error(
                "recipient_unreachable: Recipient is offline and push notification could not be sent"
            ),
            SEND_FAIL_REASON_RECIPIENT_UNREACHABLE
        );
        // The relay's real vocabulary, all of it, collapses to one token.
        for prose in [
            "Recipient connection lost and push notification could not be sent",
            "Push notification already sent for this message; recipient unreachable",
            "Recipient unreachable and the message is too large to deliver by push",
            "Group delivery did not settle for this recipient in time",
        ] {
            assert_eq!(
                classify_transport_send_error(&format!("recipient_unreachable: {prose}")),
                SEND_FAIL_REASON_RECIPIENT_UNREACHABLE
            );
        }
    }

    /// The relay-pushed answer is its own token, not a `recipient_unreachable`
    /// tail. The bridges pass this literal (pinned on their side by
    /// `react_native_relay_parks_a_pushed_message_sent`), and reading it under
    /// the unreachable prefix would fast-fail connection requests and fail
    /// Welcomes that the push may have delivered.
    #[test]
    fn relay_pushed_is_its_own_token() {
        assert_eq!(SEND_FAIL_REASON_RELAY_PUSHED, "relay_pushed");
        assert!(!SEND_FAIL_REASON_RELAY_PUSHED.starts_with(SEND_FAIL_REASON_RECIPIENT_UNREACHABLE));
        assert_eq!(
            classify_transport_send_error("relay_pushed"),
            SEND_FAIL_REASON_RELAY_PUSHED
        );
    }

    /// The mailbox tokens are their own too, for the same reason: neither may
    /// be read under the `recipient_unreachable` prefix, and each must
    /// classify to itself so a persisted record survives a round trip.
    #[test]
    fn stored_answers_are_their_own_tokens() {
        assert_eq!(SEND_FAIL_REASON_RELAY_STORED, "relay_stored");
        assert_eq!(SEND_FAIL_REASON_RELAY_PUSHED_STORED, "relay_pushed_stored");
        for token in [
            SEND_FAIL_REASON_RELAY_STORED,
            SEND_FAIL_REASON_RELAY_PUSHED_STORED,
        ] {
            assert!(!token.starts_with(SEND_FAIL_REASON_RECIPIENT_UNREACHABLE));
            assert_eq!(classify_transport_send_error(token), token);
        }
        // `relay_pushed_stored` must not be read as `relay_pushed` with a
        // tail: classification is exact for these, so the longer token keeps
        // its own meaning rather than degrading to the shorter one.
        assert_ne!(
            classify_transport_send_error(SEND_FAIL_REASON_RELAY_PUSHED_STORED),
            SEND_FAIL_REASON_RELAY_PUSHED
        );
    }

    /// A push is parked in the core but is not a failed send, so the reports
    /// that must stay out of a carrier's failure accounting are the two exact
    /// push tokens. Anything else, including relay text that merely contains
    /// one, still counts — and `relay_stored` counts, because it answers a
    /// socket write that really did miss.
    #[test]
    fn only_a_relay_push_report_is_kept_out_of_carrier_failures() {
        use crate::OfflineProtocol;
        for reason in ["relay_pushed", "relay_pushed_stored"] {
            assert!(
                !OfflineProtocol::send_report_is_carrier_failure(Some(reason)),
                "{reason} is a push, not a failed send"
            );
        }
        for reason in [
            Some("recipient_unreachable: Recipient is offline"),
            Some("Internet transport send failed"),
            Some("relay_pushed: extra"),
            Some("relay_stored"),
            None,
        ] {
            assert!(
                OfflineProtocol::send_report_is_carrier_failure(reason),
                "{reason:?} is a send failure"
            );
        }
    }

    /// Anything unrecognized fails closed to the fallback, never to itself.
    #[test]
    fn unknown_text_falls_back_and_is_never_echoed() {
        for raw in [
            "Relay rejected event: blocked",
            "off1qyvh9kjj4vy8f943a22qvxsct5s9ydew35v2dl2c is not connected",
            "",
        ] {
            let classified = classify_transport_send_error(raw);
            assert_eq!(classified, SEND_FAIL_REASON_TRANSPORT);
            assert!(
                SEND_FAIL_REASON_TOKENS.contains(&classified),
                "the fallback must itself be a token"
            );
        }
    }

    /// The literals the UniFFI layer substitutes are classified too, so the
    /// vocabulary has exactly one source rather than two that drift.
    #[test]
    fn uniffi_substituted_literals_are_classified() {
        for literal in [
            "Internet transport send failed",
            "Reticulum transport send failed",
            "Nostr transport send failed",
        ] {
            assert_eq!(
                classify_transport_send_error(literal),
                SEND_FAIL_REASON_TRANSPORT
            );
        }
        assert_eq!(
            classify_transport_send_error("Welcome send confirmation timed out"),
            SEND_FAIL_REASON_CONFIRM_TIMEOUT
        );
    }
}

/// Holds the spec's conformance-vector index against the files on disk.
///
/// # Why this test exists here, of all places
///
/// `docs/spec/README.md` carries a table naming every vector file and the
/// chapter it pins, and that table is what a second implementer reads to find
/// out what they can test themselves against. It is maintained by hand and
/// nothing compared it to the tree, so it was correct only by attention.
///
/// The failure is quiet in both directions. A vector file added without a row
/// is a conformance surface nobody outside this repository knows exists. A row
/// left behind by a moved or renamed file sends an implementer looking for
/// something that is not there, and the reasonable conclusion is that the
/// protocol has no vectors for that chapter rather than that the index is
/// stale.
///
/// It lives beside the signing-domain guard for the same reason that one is
/// here: this is the cross-cutting registry module, and the check needs no
/// crate's code at all, only the repository tree.
#[cfg(test)]
mod spec_vector_index_tests {
    use std::path::{Path, PathBuf};

    fn repo() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
    }

    /// The one vector file that is not a conformance surface for a chapter of
    /// this specification.
    ///
    /// The NIP-44 vectors are the upstream specification's own, vendored for
    /// the Nostr carrier's sealing and pinned to the checksum that
    /// specification publishes. `spec/README.md` lists them anyway, with that
    /// stated in place of a chapter, because a reader who finds the file in the
    /// tree needs to know why it is different. `spec/conformance.md` does not,
    /// because its table is the set of surfaces this specification defines, and
    /// a row for someone else's vectors would make that table mean something
    /// looser.
    ///
    /// Named here rather than left as a silent one-way check, because an
    /// exemption a test does not state is indistinguishable from a test that
    /// was never finished.
    const NOT_A_CHAPTER_SURFACE: &str = "nip44.vectors.json";

    /// The two documents that index the vectors, and what each is not required
    /// to name.
    ///
    /// `spec/README.md` is the table of contents a reader lands on;
    /// `spec/conformance.md` repeats the list with the direction each file
    /// pins. The duplication is deliberate, because a conformance chapter that
    /// sent the reader elsewhere for the list of what to test against would be
    /// missing its own subject. Both are checked in both directions, so the
    /// duplication cannot become a disagreement.
    const INDEXES: [(&str, &[&str]); 2] = [
        ("docs/spec/README.md", &[]),
        ("docs/spec/conformance.md", &[NOT_A_CHAPTER_SURFACE]),
    ];

    /// One index document, or `None` where the repo tree is absent:
    /// `cargo package` cannot carry `docs/`, so a published crate's tests must
    /// still build.
    fn index(name: &str) -> Option<String> {
        std::fs::read_to_string(repo().join(name)).ok().or_else(|| {
            eprintln!("spec tree not present, skipping the vector index checks");
            None
        })
    }

    /// Every vector file either index names exists.
    #[test]
    fn every_indexed_vector_file_exists() {
        for (doc, _) in INDEXES {
            let Some(text) = index(doc) else { return };

            let mut named = 0;
            for token in text.split('`') {
                if token.ends_with(".vectors.json") && token.starts_with("crates/") {
                    named += 1;
                    assert!(
                        repo().join(token).is_file(),
                        "{doc} names {token}, which is not in the tree"
                    );
                }
            }
            assert!(
                named >= 8,
                "the scan of {doc} found only {named} vector files, so it is no \
                 longer reading the table and would pass against anything"
            );
        }
    }

    /// Every vector file in the tree is named by the index.
    ///
    /// This is the direction that catches a surface shipped without being
    /// published, which is the one a reviewer cannot see: the tests pass, the
    /// file is real, and only an outside implementer ever notices it is
    /// undocumented.
    #[test]
    fn every_vector_file_in_the_tree_is_indexed() {
        let mut found = Vec::new();
        for crate_dir in std::fs::read_dir(repo().join("crates")).expect("crates/") {
            let data = crate_dir.expect("an entry").path().join("tests/data");
            let Ok(entries) = std::fs::read_dir(&data) else {
                continue;
            };
            for entry in entries {
                let path = entry.expect("an entry").path();
                let name = path
                    .file_name()
                    .expect("a name")
                    .to_string_lossy()
                    .to_string();
                if name.ends_with(".vectors.json") {
                    // Compared against a path written in a markdown table, so
                    // the separator has to be the one the document uses, not
                    // the one this platform builds. `Path::join` yields
                    // backslashes on Windows and the table is all forward
                    // slashes, so without the replace, every comparison below
                    // fails there and passes everywhere else.
                    //
                    // Falling back to the bare file name if the prefix will not
                    // strip would be worse than failing: the table holds full
                    // paths that end in that name, so `contains` would match on
                    // any of them and the check would quietly degrade to
                    // asserting the file is mentioned somewhere.
                    let relative = path.strip_prefix(repo()).unwrap_or_else(|_| {
                        panic!("{} is not under the repo root", path.display())
                    });
                    found.push(relative.to_string_lossy().replace('\\', "/"));
                }
            }
        }

        assert!(
            !found.is_empty(),
            "no vector files were found, so this test proves nothing"
        );

        for (doc, exempt) in INDEXES {
            let Some(text) = index(doc) else { return };
            for path in &found {
                if exempt.iter().any(|name| path.ends_with(name)) {
                    continue;
                }
                assert!(
                    text.contains(path),
                    "{path} is a conformance surface that {doc} does not list, \
                     so nobody outside this repository knows it exists"
                );
            }
        }
    }
}