vta-service 0.62.0

Service for Verifiable Trust Agents operating in Verifiable Trust Communities
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
//! A member's MLS group for each room they belong to.
//!
//! The custody half of the room oracle. [`crate::operations::room_oracle`] mints
//! presentations from credentials; this holds the *keys*, which is what
//! `rooms/keys/open` needs and what nothing before this stored.
//!
//! # Three steps, and the third is the one people forget
//!
//! A group arrives once and then has to be kept current:
//!
//! 1. [`mint_key_package`] — the joining side produces something the owner can add.
//! 2. [`join`] — the owner's Welcome arrives and the group exists.
//! 3. [`apply_commit`] — every membership change after that. **This is not optional.** A
//!    member who misses one is stuck at their last epoch and can open nothing sealed after
//!    it, and the symptom is "this record does not open", which reads like corruption
//!    rather than a missed message.
//!
//! # What authorizes each
//!
//! Deliberately three different answers, because they are three different acts:
//!
//! - **Minting** is gated on holding an invitation, because minting retains a private key
//!   against a Welcome that may never come. A key-holder that minted for any room on any
//!   request is one anyone can fill.
//! - **Joining** is gated on that same invitation, *consumed*. This is where the design's
//!   "joining is consent" stops being ceremonial: a Welcome carries a group's secrets, and
//!   a recipient that accepts an uninvited one has made the invitation decorative.
//! - **Committing** is gated *inside the group* — MLS authenticates the committer as a
//!   member of the group we already hold. Never from an ACL of our own, which is the same
//!   rule the rest of the room family follows.

use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use vti_common::error::AppError;
use vti_common::store::{KeyspaceHandle, MoveOutcome};
use vti_rooms::error::RoomKeyError;
use vti_rooms::mls::{GroupSnapshot, IdentitySnapshot, RoomGroup};
use vti_rooms::sealed::SealedRoom;
use vti_rooms::wire::EpochLink;

/// One member's place in one room: the key everything here is stored under.
///
/// # Per (room, persona), never per room
///
/// One VTA serves several principals, and one person may hold several personas, so two
/// members of one room can share a key holder — a family on one VTA, a team on a company
/// VTA. Each is a different leaf in the room's MLS group with different secrets, so each
/// has its own group state, pending KeyPackage, pending advance, epoch chain and root
/// memory. Keyed by room alone, the second member's Welcome was refused as a second join.
///
/// Constructed by [`Member::new`], which refuses identifiers that would make the storage
/// key ambiguous. *Which* persona a request acts as is not decided here — that is
/// [`crate::operations::room_member`], where the caller's contexts are known.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Member {
    room_id: String,
    persona: String,
}

impl Member {
    /// `room_id` and `persona` are DIDs, and a DID has no `/` — which is what makes
    /// `<kind>/<room>/<persona>` unambiguous, so anything carrying one is refused rather
    /// than stored under a key another pair could also produce.
    pub fn new(room_id: &str, persona: &str) -> Result<Self, AppError> {
        for (what, v) in [("room id", room_id), ("member DID", persona)] {
            if v.is_empty() || v.contains('/') {
                return Err(AppError::Validation(format!(
                    "`{v}` is not a usable {what}: a DID is non-empty and has no `/`"
                )));
            }
        }
        Ok(Self {
            room_id: room_id.to_string(),
            persona: persona.to_string(),
        })
    }

    pub fn room_id(&self) -> &str {
        &self.room_id
    }

    /// The persona DID: the member's identity in the room, and their MLS leaf.
    pub fn persona(&self) -> &str {
        &self.persona
    }

    fn key(&self, kind: &str) -> String {
        format!("{kind}/{}/{}", self.room_id, self.persona)
    }
}

/// Group state, per member: `room-group/<roomId>/<persona>`.
const GROUP: &str = "room-group";
/// A pending KeyPackage, per member: `room-kp/<roomId>/<persona>`.
const KEY_PACKAGE: &str = "room-kp";
/// The one pending epoch advance, per member: `room-advance/<roomId>/<persona>`.
///
/// **One key, so one slot.** `rooms/owner/group/{add,remove}/0.1` item 3: a key holder
/// holds at most one pending advance per room — per group it holds, which is per member.
/// Two would be two candidate successors of the same live group, of which the host can
/// accept at most one; keeping both under one key makes the second unrepresentable rather
/// than merely unlikely.
const ADVANCE: &str = "room-advance";
/// What hosts have said about the room's tree, per member: `room-roots/<roomId>/<persona>`.
const ROOTS: &str = "room-roots";
/// That a commit removed this member, per member: `room-removed/<roomId>/<persona>`.
///
/// Written when [`apply_commit`] processes the commit that removes the member's own leaf.
/// The group state stays as it was (what was readable before the removal still is), so
/// without this row the group alone says only "behind", and every record sealed after the
/// removal read as a commit somebody had not delivered — whose remedy, applying commits,
/// can never work.
const REMOVED: &str = "room-removed";
/// The last admission this owner applied, per owner: `room-admitted/<roomId>/<persona>`.
///
/// `rooms/owner/group/add/0.2` item 8: kept so that a caller who lost the answer and sends
/// the identical request again gets the Welcome it needs, rather than `alreadyMember`. Short
/// on purpose — until this owner builds its next advance of the room, or
/// [`ADMISSION_RETENTION_SECS`] after it was applied, whichever is first — because the
/// Welcome is secret-bearing ciphertext and a request after either is no longer a retry of
/// anything.
///
/// In [`ROOM_GROUPS`](crate::keyspaces::ROOM_GROUPS), beside the pending advance it is promoted
/// from, which already holds the same Welcome: under the same storage encryption, carried by
/// a backup with the group it belongs to, and gone with the member's DID. A separate keyspace
/// excluded from backup would buy nothing — the pending advance it is made from is backed up
/// — and a restored row is still bounded by its `appliedAt`.
const ADMITTED: &str = "room-admitted";

/// How long a retained admission is kept at most — `rooms/owner/group/add/0.2` item 8's
/// 24 hours.
pub const ADMISSION_RETENTION_SECS: u64 = 24 * 60 * 60;

/// The [`ADMITTED`] row: `rooms/owner/group/add/0.2` item 8's retained admission.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RetainedAdmission {
    /// The DID admitted, compared with a repeat's `member` as a string — never normalised,
    /// never resolved.
    pub member: String,
    /// The admitted KeyPackage's `KeyPackageRef` (RFC 9420 §5.2), base64url.
    pub key_package_ref: String,
    /// The room epoch the admission produced.
    pub epoch: u32,
    /// The Welcome, base64url.
    pub welcome: String,
    /// The ratchet tree, base64url, where the admission returned one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ratchet_tree: Option<String>,
    /// Unix seconds when it was applied here.
    pub applied_at: u64,
}

impl RetainedAdmission {
    /// Whether it is still kept at `now` (item 8's 24 hours).
    pub fn kept_at(&self, now: u64) -> bool {
        now < self.applied_at.saturating_add(ADMISSION_RETENTION_SECS)
    }

    /// Whether a request for `member` with a KeyPackage whose reference is
    /// `key_package_ref` repeats this admission — the exact match item 8 asks for.
    pub fn repeated_by(&self, member: &str, key_package_ref: &[u8]) -> bool {
        self.member == member && self.key_package_ref == B64.encode(key_package_ref)
    }
}

/// The [`REMOVED`] row.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct RemovalRecord {
    /// The first room epoch this member cannot open.
    epoch: u32,
    /// Unix seconds when the removing commit was processed here.
    removed_at: u64,
}

/// The rows a build keyed by room alone wrote, read only to migrate them
/// ([`migrate_legacy`]). `room-add:` is older still: the admission-only advance slot.
mod legacy {
    pub(super) const GROUP_PREFIX: &str = "room-group:";
    pub(super) fn group(room_id: &str) -> String {
        format!("{GROUP_PREFIX}{room_id}")
    }
    pub(super) fn key_package(room_id: &str) -> String {
        format!("room-kp:{room_id}")
    }
    pub(super) fn advance(room_id: &str) -> String {
        format!("room-advance:{room_id}")
    }
    pub(super) fn add(room_id: &str) -> String {
        format!("room-add:{room_id}")
    }
    pub(super) fn roots(room_id: &str) -> String {
        format!("room-roots:{room_id}")
    }
}

/// Storage key for a consumed invitation.
///
/// Keyed by the invitation's credential id, which already names one subject — so it is
/// per member without saying so, and stays as it was.
pub(super) fn invitation_key(credential_id: &str) -> String {
    format!("room-vic:{credential_id}")
}

/// A minted key package, awaiting the Welcome that consumes it.
///
/// The private half lives in the snapshot; this row *is* the retained key material, which is
/// why it is bounded rather than kept forever.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PendingKeyPackage {
    /// The identity holding the package's private half.
    pub snapshot: IdentitySnapshot,
    /// The KeyPackage itself, base64url — public, and what travels to the owner.
    pub key_package: String,
    /// Unix seconds after which this is discarded unused.
    pub expires_at: u64,
}

/// What this VTA holds for one room.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RoomGroupRecord {
    /// The group, at whatever epoch it last reached.
    pub snapshot: GroupSnapshot,
    /// The member this group is for.
    pub member_did: String,
    /// The room's epoch key chain, as far as this VTA holds it.
    ///
    /// Retained here rather than fetched per open because this is the custody point: a link
    /// is key material, wrapped, and the VTA is where the room's key material lives. Every
    /// commit this VTA applies appends one, so a member who has kept up holds the chain back
    /// to the epoch they joined at without asking anyone.
    ///
    /// **Empty for a room joined before the chain existed**, and for one whose owner has not
    /// sent the history. `open` then reads only from the joining epoch forward and says so
    /// — see `RoomKeyError::EpochUnreachable`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub links: Vec<EpochLink>,
    /// Unix seconds of the last change.
    pub updated_at: u64,
}

/// Mint an MLS KeyPackage for `room_id` and retain its private half.
///
/// **Per room, never reused across rooms.** A KeyPackage is a stable public identifier, so
/// the same one offered to two rooms tells anyone who sees both that one party is in both —
/// the correlation a `private` room exists to deny, arriving through the door rather than
/// the wall.
///
/// The package's leaf identifies `member`'s persona, and the private half is kept under
/// that member, so two personas on one VTA invited to one room each mint their own.
pub async fn mint_key_package(
    groups: &KeyspaceHandle,
    member: &Member,
    lifetime_secs: u64,
    now: u64,
) -> Result<PendingKeyPackage, AppError> {
    let (snapshot, package) = IdentitySnapshot::mint(member.persona())
        .map_err(|e| AppError::Internal(format!("mint a key package: {e}")))?;

    let pending = PendingKeyPackage {
        snapshot,
        key_package: B64.encode(&package),
        expires_at: now + lifetime_secs,
    };
    groups
        .insert(member.key(KEY_PACKAGE), &pending)
        .await
        .map_err(|e| AppError::Internal(format!("store the pending key package: {e}")))?;
    Ok(pending)
}

/// Whether `key_package` is one this VTA minted for `member` with
/// [`mint_key_package`] and still holds the private half of — not yet used (a Welcome
/// consumes it, [`join`]) and not expired. `rooms/keys/act/0.3` recipient item 10.
///
/// **Compared by `KeyPackageRef`, never by the leaf credential.** A leaf credential is
/// signed by the leaf's own key, so anybody can make one naming the member; only the
/// reference names the exact KeyPackage — and so the init and signature keys — this VTA
/// minted. A KeyPackage that does not parse, or that this VTA never minted for this room
/// and member, is not held.
pub async fn holds_key_package(
    groups: &KeyspaceHandle,
    member: &Member,
    key_package: &[u8],
    now: u64,
) -> Result<bool, AppError> {
    let Ok(asked) = vti_rooms::mls::key_package_ref(key_package) else {
        return Ok(false);
    };
    let pending: Option<PendingKeyPackage> = groups
        .get(member.key(KEY_PACKAGE))
        .await
        .map_err(|e| AppError::Internal(format!("read the pending key package: {e}")))?;
    let Some(pending) = pending else {
        return Ok(false);
    };
    if pending.expires_at <= now || pending.snapshot.member_did() != member.persona() {
        return Ok(false);
    }
    let Ok(minted) = B64.decode(pending.key_package.as_bytes()) else {
        return Ok(false);
    };
    Ok(vti_rooms::mls::key_package_ref(&minted).is_ok_and(|r| r == asked))
}

/// A KeyPackage the per-room build minted for `member` before an upgrade, moved under
/// them — so a Welcome arriving after the upgrade still joins. Only when its leaf
/// identifies `member`: a package minted for anybody else is not theirs to join with.
async fn legacy_key_package(
    groups: &KeyspaceHandle,
    member: &Member,
) -> Result<Option<PendingKeyPackage>, AppError> {
    let old = legacy::key_package(member.room_id());
    let Some(raw) = groups.get_raw(old.clone()).await? else {
        return Ok(None);
    };
    let Ok(kp) = serde_json::from_slice::<PendingKeyPackage>(&raw) else {
        return Ok(None);
    };
    if kp.snapshot.member_did() != member.persona() {
        return Ok(None);
    }
    move_row(groups, old, raw, member.key(KEY_PACKAGE), &kp).await?;
    Ok(Some(kp))
}

/// Why a Welcome could not be accepted.
#[derive(Debug)]
pub enum JoinError {
    /// This member already holds group state for the room — `alreadyJoined`.
    AlreadyJoined,
    App(AppError),
}

impl From<AppError> for JoinError {
    fn from(e: AppError) -> Self {
        Self::App(e)
    }
}

/// Join `member`'s room's group from a Welcome.
///
/// Refuses a second join **by the same member** rather than merging: two group states for
/// one member in one room is a condition nothing downstream can resolve — [`open_record`]
/// has no way to choose, and choosing wrong returns "did not open" for a record the member
/// can plainly see. Another persona on this VTA holding the room is a different member with
/// a different leaf, and is no reason to refuse this one.
pub async fn join(
    groups: &KeyspaceHandle,
    member: &Member,
    welcome: &[u8],
    now: u64,
) -> Result<u64, JoinError> {
    let room_id = member.room_id();
    if load(groups, member).await?.is_some() {
        return Err(JoinError::AlreadyJoined);
    }

    // The KeyPackage the owner added is the one we minted, so the private half is in the
    // pending record — joining with a fresh identity would produce a group whose leaf
    // nobody added.
    let pending: Option<PendingKeyPackage> = groups
        .get(member.key(KEY_PACKAGE))
        .await
        .map_err(|e| AppError::Internal(format!("read the pending key package: {e}")))?;
    let pending = match pending {
        Some(p) => Some(p),
        None => legacy_key_package(groups, member).await?,
    };
    let pending = pending.ok_or_else(|| {
        AppError::Validation(format!(
            "no key package was minted for room `{room_id}`; a welcome can only be accepted \
             against the package the owner was given"
        ))
    })?;

    let group = RoomGroup::join_from_identity(&pending.snapshot, welcome)
        .map_err(|e| AppError::Validation(format!("the welcome did not process: {e}")))?;
    let epoch = group.epoch();

    store(groups, member, &group, Vec::new(), now).await?;
    // A member admitted again is a member again: an earlier removal no longer describes
    // the group this VTA now holds.
    groups
        .remove(member.key(REMOVED))
        .await
        .map_err(|e| AppError::Internal(format!("clear an earlier removal record: {e}")))?;
    // The package is consumed. MLS consumes it on add, and a retained private half for a
    // used package is key material kept for nothing.
    groups
        .remove(member.key(KEY_PACKAGE))
        .await
        .map_err(|e| AppError::Internal(format!("clear the pending key package: {e}")))?;
    Ok(epoch)
}

/// Apply a commit, advancing one epoch.
///
/// Ordering is the whole contract. A replay is a no-op success — a retry that failed would
/// make every unreliable transport a liveness problem — and a gap is refused with the epoch
/// we actually hold, so the sender resumes rather than guesses.
pub async fn apply_commit(
    groups: &KeyspaceHandle,
    member: &Member,
    commit: &[u8],
    claimed_epoch: u64,
    now: u64,
) -> Result<u64, AppError> {
    let room_id = member.room_id();
    let record = load(groups, member)
        .await?
        .ok_or_else(|| no_group(member))?;
    // A `SealedRoom` rather than a bare group: the epoch link this commit produces is bound
    // to the room, so minting one needs the room's identifier and the group does not carry
    // it. Restoring through the room type is what makes the link mintable at all.
    let mut room = SealedRoom::new(
        room_id,
        RoomGroup::restore(&record.snapshot)
            .map_err(|e| AppError::Internal(format!("restore the group: {e}")))?,
    );
    let current = room.group().epoch();

    if claimed_epoch == current {
        // Already applied. Reporting success with the unchanged epoch is what makes
        // delivery retryable.
        return Ok(current);
    }
    if claimed_epoch != current + 1 {
        return Err(AppError::Conflict(format!(
            "commit produces epoch {claimed_epoch} but room `{room_id}` is at {current}; \
             resume from {}",
            current + 1
        )));
    }

    // The link is what keeps everything already in the room readable across this commit.
    // Dropping it here would advance the epoch and silently sever the history — the defect
    // the chain exists to fix, so it is retained in the same write that advances the group.
    let (_, link) = match room.apply_commit(commit) {
        Ok(applied) => applied,
        Err(e) => {
            // Remembered, so a later open of a record sealed after the removal says
            // "removed" rather than "a commit has not been delivered". Replaying the same
            // commit lands here again and rewrites the same row.
            if let RoomKeyError::Removed { epoch } = e {
                record_removal(groups, member, epoch, now).await?;
            }
            return Err(AppError::Validation(format!(
                "the commit did not process: {e}"
            )));
        }
    };
    let epoch = room.group().epoch();

    let mut links = record.links;
    if let Some(link) = link {
        links.push(link);
    }
    store(groups, member, room.group(), links, now).await?;
    Ok(epoch)
}

/// Seal a record body with the room's current epoch key.
///
/// The mirror of [`open_record`], and the reason a client can write to a sealed room at all:
/// the key never leaves, so the caller sends plaintext and receives ciphertext.
///
/// # It does not write
///
/// The caller takes the result to a host and presents its own authority there. Sealing and
/// being allowed to store are different questions asked of different parties — this VTA
/// knows the key and nothing about the room's ACL; the host knows the credentials and cannot
/// read a byte. Doing both here would make this VTA the party that decides what goes into a
/// room, which is the one thing the design keeps it out of.
pub async fn seal_record(
    groups: &KeyspaceHandle,
    member: &Member,
    key: &str,
    version: u64,
    plaintext: &[u8],
) -> Result<vti_rooms::wire::SealedContent, AppError> {
    let room_id = member.room_id();
    let record = load(groups, member)
        .await?
        .ok_or_else(|| no_group(member))?;
    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| AppError::Internal(format!("restore the group: {e}")))?;

    SealedRoom::new(room_id, group)
        .seal_record(key, version, plaintext)
        .map_err(|e| AppError::Validation(e.to_string()))
}

/// Why a file key could not be derived.
///
/// `#[non_exhaustive]` because it has already grown once ([`Self::Removed`]) and the
/// epoch cases are exactly where a new remedy would add another.
#[derive(Debug)]
#[non_exhaustive]
pub enum FileKeyError {
    /// This VTA holds no group for the room.
    NoGroup,
    /// Anything else that is not an unreachable epoch: a store error, a group that does not
    /// restore.
    App(AppError),
    /// The file is sealed under an epoch later than the one this VTA holds: a commit has not
    /// been delivered.
    NotDelivered { epoch: u32, held: u32 },
    /// The epoch chain reaches back only to `earliest`.
    BeyondChain { epoch: u32, earliest: u32 },
    /// A commit this VTA applied removed the member from the group at `removed_at`, and
    /// the file is sealed under `epoch`, at or after it: its key will never be derivable
    /// here. Apart from [`Self::NotDelivered`] for the reason [`OpenError::Removed`] is:
    /// that one's remedy, applying commits, cannot work for this one.
    Removed { epoch: u32, removed_at: u32 },
}

/// One file's key — `rooms/keys/file-key/0.1` and `0.2`.
///
/// `open_epoch` is `None` to seal (the current epoch, which is returned) and the file's
/// epoch to open, walked out of the retained chain exactly as [`open_record`] walks it.
///
/// # A key, unlike every other answer here
///
/// `rooms/keys/open` keeps the key and returns plaintext; this returns a key, because file
/// bytes must not pass through this VTA. What it releases opens one file and nothing else:
/// the room's storage key does not leave, so no sibling can be derived from it.
pub async fn file_key(
    groups: &KeyspaceHandle,
    member: &Member,
    file_id: &vti_rooms::files::FileId,
    open_epoch: Option<u32>,
) -> Result<(vti_rooms::files::FileKey, u32), FileKeyError> {
    let room_id = member.room_id();
    let record = load(groups, member)
        .await
        .map_err(FileKeyError::App)?
        .ok_or(FileKeyError::NoGroup)?;
    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| FileKeyError::App(AppError::Internal(format!("restore the group: {e}"))))?;
    let mut room = SealedRoom::new(room_id, group);

    let Some(epoch) = open_epoch else {
        return room
            .file_key_for_seal(file_id)
            .map_err(|e| FileKeyError::App(AppError::Internal(e.to_string())));
    };
    room.add_links(record.links);
    match room.file_key_for_open(file_id, epoch) {
        Ok(key) => Ok((key, epoch)),
        Err(RoomKeyError::EpochAhead { sealed, held }) => {
            // A removal leaves the group where it was, so a file sealed after it is also
            // "ahead of the group". The row says which — as `open_record_detailed` reads it.
            if let Some(removed_at) = removed_at(groups, member)
                .await
                .map_err(FileKeyError::App)?
                && sealed >= removed_at
            {
                return Err(FileKeyError::Removed {
                    epoch: sealed,
                    removed_at,
                });
            }
            Err(FileKeyError::NotDelivered {
                epoch: sealed,
                held,
            })
        }
        Err(RoomKeyError::EpochUnreachable { sealed, earliest }) => {
            Err(FileKeyError::BeyondChain {
                epoch: sealed,
                earliest,
            })
        }
        Err(e) => Err(FileKeyError::App(AppError::Validation(e.to_string()))),
    }
}

/// Every member this VTA holds group state for, by room — keys only, nothing decrypted.
///
/// **Not an answer to give a caller.** It names every persona of every principal this VTA
/// serves; [`crate::operations::room_member`] filters it to the personas the caller may act
/// as before anything leaves.
pub async fn held_members(groups: &KeyspaceHandle) -> Result<Vec<Member>, AppError> {
    members_under(groups, &format!("{GROUP}/")).await
}

/// The members holding group state for `room_id`.
pub async fn holders(groups: &KeyspaceHandle, room_id: &str) -> Result<Vec<Member>, AppError> {
    members_under(groups, &format!("{GROUP}/{room_id}/")).await
}

async fn members_under(groups: &KeyspaceHandle, prefix: &str) -> Result<Vec<Member>, AppError> {
    let keys = groups.prefix_keys(prefix.as_bytes().to_vec()).await?;
    let mut out = Vec::with_capacity(keys.len());
    for k in keys {
        let k = String::from_utf8_lossy(&k);
        let Some(rest) = k.strip_prefix(&format!("{GROUP}/")) else {
            continue;
        };
        // `<room>/<persona>`, neither carrying a `/` (see [`Member::new`]).
        if let Some((room, persona)) = rest.split_once('/')
            && let Ok(m) = Member::new(room, persona)
        {
            out.push(m);
        }
    }
    Ok(out)
}

/// How far `member`'s group reads, as `rooms/keys/list` reports it.
///
/// # Custody, not membership
///
/// This answers "what can I open", never "what am I a member of". A principal may hold a
/// perfectly good membership credential for a room whose Welcome never arrived — absent
/// here, correctly. And a VTA not yet told of a removal still holds keys for a room its
/// principal has left: it cannot write there, because the host checks credentials that no
/// longer verify, and it can still open what it already had. That is what removal has always
/// meant, and a caller MUST NOT read this list as authority to act.
pub async fn held_room(groups: &KeyspaceHandle, member: &Member) -> Result<HeldRoom, AppError> {
    let room_id = member.room_id();
    let record = load(groups, member)
        .await?
        .ok_or_else(|| no_group(member))?;

    // Restoring to answer is deliberate. The epoch could be read from the snapshot, but
    // `earliestReadableEpoch` is only knowable by *walking* the chain — and a number
    // derived two different ways is a number that will eventually disagree with itself.
    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| AppError::Internal(format!("restore the group for `{room_id}`: {e}")))?;
    let mut room = SealedRoom::new(room_id, group);
    room.add_links(record.links);

    let epoch = room.room_epoch();
    let earliest = room
        .earliest_readable_epoch()
        .map_err(|e| AppError::Internal(format!("walk the chain for `{room_id}`: {e}")))?;

    Ok(HeldRoom {
        room_id: room_id.to_string(),
        epoch,
        earliest_readable_epoch: earliest,
    })
}

/// One room this VTA can open, as `rooms/keys/list` reports it.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HeldRoom {
    pub room_id: String,
    pub epoch: u32,
    pub earliest_readable_epoch: u32,
}

/// Retain epoch links this VTA's principal fetched from the room's host.
///
/// The last leg of a joining member's backfill. A key holder accrues a rung for every
/// membership change it lives through — [`apply_commit`] keeps one from each — so what
/// arrives here is the history it did **not** live through.
///
/// Returns `(earliest_readable_epoch, stored)`.
///
/// # Why the answer is not "how many arrived"
///
/// A rung extends reach only if every rung above it is present too, so counting what was
/// delivered says nothing about what can now be read. The number worth returning is the one
/// this VTA can only get by *walking* what it holds — which is why the specification puts it
/// in the response and why a host serving the same rungs could not have answered it.
///
/// # A rung already held is never replaced
///
/// Same rule the hosts follow. A second rung for an epoch is either a replay — which must be
/// a no-op rather than an error, or a retried delivery becomes a failure — or an attempt to
/// re-point this member's history at key material of somebody else's choosing. Keeping the
/// first is the only reading that is safe under both.
pub async fn store_links(
    groups: &KeyspaceHandle,
    member: &Member,
    incoming: Vec<EpochLink>,
    now: u64,
) -> Result<(u32, usize), AppError> {
    let room_id = member.room_id();
    let record = load(groups, member)
        .await?
        .ok_or_else(|| no_group(member))?;

    let mut links = record.links;
    let mut stored = 0;
    for link in incoming {
        if !links.iter().any(|held| held.epoch == link.epoch) {
            links.push(link);
            stored += 1;
        }
    }
    links.sort_by_key(|l| l.epoch);

    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| AppError::Internal(format!("restore the group: {e}")))?;
    let mut room = SealedRoom::new(room_id, group);
    room.add_links(links.clone());
    let earliest = room
        .earliest_readable_epoch()
        .map_err(|e| AppError::Internal(format!("walk the chain: {e}")))?;

    store(groups, member, room.group(), links, now).await?;
    Ok((earliest, stored))
}

/// Open a sealed record with the room's group key.
///
/// The key never leaves. That is the whole design: the caller sends ciphertext and gets
/// plaintext, and an oracle that returned the key would be a key-release call wearing a
/// different name.
pub async fn open_record(
    groups: &KeyspaceHandle,
    member: &Member,
    key: &str,
    version: u64,
    ciphertext: &str,
    nonce: &str,
    epoch: u32,
) -> Result<Vec<u8>, AppError> {
    open_record_detailed(groups, member, key, version, ciphertext, nonce, epoch)
        .await
        .map_err(AppError::from)
}

/// Why [`open_record_detailed`] did not open a record.
///
/// The epoch cases are apart from [`Self::App`] because their remedies differ, and
/// `rooms/keys/open/0.2` names each as an `unknownEpoch` reason: a member who is behind
/// applies the commits it missed, a member whose chain does not reach back needs it
/// extended (or cannot open the record at all), and a member who was removed can apply
/// nothing that will ever open the record.
#[derive(Debug)]
#[non_exhaustive]
pub enum OpenError {
    /// The record is sealed under a later epoch than this VTA's group: a commit has not
    /// been delivered. `held` is the room epoch the group is at.
    NotDelivered { epoch: u32, held: u64 },
    /// A commit this VTA applied removed the member from the group at `removed_at`, and
    /// the record is sealed under `epoch`, at or after it: it will never open here.
    Removed { epoch: u32, removed_at: u32 },
    /// The record is sealed under `epoch`, earlier than this VTA's group, and the retained
    /// epoch chain reaches back only to `earliest` (`rooms/keys/open/0.2`'s `beyondChain`).
    BeyondChain { epoch: u32, earliest: u32 },
    /// Anything else: no group, a group that does not restore, a record that did not open.
    App(AppError),
}

impl From<AppError> for OpenError {
    fn from(e: AppError) -> Self {
        Self::App(e)
    }
}

impl std::fmt::Display for OpenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotDelivered { epoch, held } => write!(
                f,
                "record is sealed under epoch {epoch} and this VTA holds the room at epoch \
                 {held}; a commit has not been delivered"
            ),
            Self::Removed { epoch, removed_at } => write!(
                f,
                "record is sealed under epoch {epoch}, and this member was removed from the \
                 room's group at epoch {removed_at}: nothing sealed from then on opens here"
            ),
            // `RoomKeyError::EpochUnreachable`'s words, which is what this case said before
            // it had a variant of its own.
            Self::BeyondChain { epoch, earliest } => write!(
                f,
                "{}",
                RoomKeyError::EpochUnreachable {
                    sealed: *epoch,
                    earliest: *earliest,
                }
            ),
            Self::App(e) => write!(f, "{e}"),
        }
    }
}

impl From<OpenError> for AppError {
    fn from(e: OpenError) -> Self {
        match e {
            OpenError::App(e) => e,
            other => AppError::Validation(other.to_string()),
        }
    }
}

/// [`open_record`], saying which epoch case refused it.
pub async fn open_record_detailed(
    groups: &KeyspaceHandle,
    member: &Member,
    key: &str,
    version: u64,
    ciphertext: &str,
    nonce: &str,
    epoch: u32,
) -> Result<Vec<u8>, OpenError> {
    let room_id = member.room_id();
    let record = load(groups, member)
        .await?
        .ok_or_else(|| no_group(member))?;
    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| AppError::Internal(format!("restore the group: {e}")))?;

    let held = group.epoch() + 1;
    if u64::from(epoch) > held {
        // A removal leaves the group where it was, so "ahead of the group" is also what a
        // record sealed after it looks like. The row says which.
        if let Some(removed_at) = removed_at(groups, member).await?
            && epoch >= removed_at
        {
            return Err(OpenError::Removed { epoch, removed_at });
        }
        // Saying which epoch we hold turns "it does not open" — which reads like corruption
        // — into "you are behind", which an operator can act on.
        return Err(OpenError::NotDelivered { epoch, held });
    }

    // A record older than the current epoch is opened with *its own* epoch's key, walked out
    // of the retained chain. Without the links this VTA reaches only the epoch it is at, and
    // the error says which — an unreachable epoch is history that was severed or never
    // delivered, not a record that failed to decrypt.
    let mut room = SealedRoom::new(room_id, group);
    room.add_links(record.links);

    room.open_record(
        key,
        version,
        &vti_rooms::wire::SealedContent {
            ciphertext: ciphertext.to_string(),
            nonce: nonce.to_string(),
            epoch,
        },
    )
    .map_err(|e| match e {
        RoomKeyError::EpochUnreachable { sealed, earliest } => OpenError::BeyondChain {
            epoch: sealed,
            earliest,
        },
        e => OpenError::App(AppError::Validation(e.to_string())),
    })
}

/// The first room epoch `member` cannot open because a commit removed them, if one did.
pub async fn removed_at(groups: &KeyspaceHandle, member: &Member) -> Result<Option<u32>, AppError> {
    let row: Option<RemovalRecord> = groups.get(member.key(REMOVED)).await.map_err(|e| {
        AppError::Internal(format!(
            "read the removal record for `{}`: {e}",
            member.room_id()
        ))
    })?;
    Ok(row.map(|r| r.epoch))
}

async fn record_removal(
    groups: &KeyspaceHandle,
    member: &Member,
    epoch: u32,
    now: u64,
) -> Result<(), AppError> {
    groups
        .insert(
            member.key(REMOVED),
            &RemovalRecord {
                epoch,
                removed_at: now,
            },
        )
        .await
        .map_err(|e| {
            AppError::Internal(format!(
                "record the removal from `{}`: {e}",
                member.room_id()
            ))
        })
}

/// Record an invitation as consumed, refusing a second use.
///
/// Single use means single use, and this record is the only thing that remembers. It
/// deliberately outlives the group it admitted: a member who leaves a room discards the
/// group, and without this row the same invitation would let them be re-added with no fresh
/// consent from the owner.
pub async fn consume_invitation(
    invitations: &KeyspaceHandle,
    credential_id: &str,
    room_id: &str,
    now: u64,
) -> Result<(), AppError> {
    let key = invitation_key(credential_id);
    if invitations
        .get_raw(key.clone())
        .await
        .map_err(|e| AppError::Internal(format!("read the invitation record: {e}")))?
        .is_some()
    {
        return Err(AppError::Conflict(format!(
            "invitation `{credential_id}` has already been used"
        )));
    }
    invitations
        .insert(
            key,
            &serde_json::json!({ "roomId": room_id, "consumedAt": now }),
        )
        .await
        .map_err(|e| AppError::Internal(format!("record the invitation: {e}")))
}

/// The group this VTA holds for `member`, if any.
pub async fn load(
    groups: &KeyspaceHandle,
    member: &Member,
) -> Result<Option<RoomGroupRecord>, AppError> {
    groups.get(member.key(GROUP)).await.map_err(|e| {
        AppError::Internal(format!("read group state for `{}`: {e}", member.room_id()))
    })
}

/// The refusal for a member with no group state. Says nothing about whether another persona
/// on this VTA holds the room — that is not the caller's to learn.
fn no_group(member: &Member) -> AppError {
    AppError::NotFound(format!(
        "this VTA holds no group state for room `{}`",
        member.room_id()
    ))
}

async fn store(
    groups: &KeyspaceHandle,
    member: &Member,
    group: &RoomGroup,
    links: Vec<EpochLink>,
    now: u64,
) -> Result<(), AppError> {
    let record = RoomGroupRecord {
        snapshot: group
            .snapshot()
            .map_err(|e| AppError::Internal(format!("snapshot the group: {e}")))?,
        member_did: member.persona().to_string(),
        links,
        updated_at: now,
    };
    groups
        .insert(member.key(GROUP), &record)
        .await
        .map_err(|e| {
            AppError::Internal(format!("store group state for `{}`: {e}", member.room_id()))
        })
}

/// Why a group could not be founded.
#[derive(Debug)]
pub enum CreateGroupError {
    /// This VTA already holds group state for the room, at this room epoch.
    AlreadyExists {
        epoch: u32,
    },
    App(AppError),
}

/// Found `room_id`'s group with `member_did` as its only leaf —
/// `rooms/owner/group/create/0.1`.
///
/// Returns the room epoch, 1: MLS counts from 0 and a room from 1, so a group
/// founded here agrees with the epoch a host records at registration without
/// anything being minted.
///
/// **Never replaces a group.** A group this VTA joined by a Welcome, or founded
/// earlier, holds the key schedule every sealed record of the room depends on;
/// founding over it would leave those records unopenable by this member, and
/// the member's commits unreadable by everyone else.
pub async fn create_group(
    groups: &KeyspaceHandle,
    member: &Member,
    now: u64,
) -> Result<u32, CreateGroupError> {
    let room_id = member.room_id();
    if let Some(existing) = load(groups, member).await.map_err(CreateGroupError::App)? {
        let group = RoomGroup::restore(&existing.snapshot).map_err(|e| {
            CreateGroupError::App(AppError::Internal(format!("restore the group: {e}")))
        })?;
        return Err(CreateGroupError::AlreadyExists {
            epoch: SealedRoom::new(room_id, group).room_epoch(),
        });
    }
    let group = RoomGroup::create(member.persona())
        .map_err(|e| CreateGroupError::App(AppError::Internal(format!("found the group: {e}"))))?;
    // The room epoch is the MLS epoch plus one, as `SealedRoom::room_epoch` has it.
    let epoch = u32::try_from(group.epoch() + 1).unwrap_or(u32::MAX);
    store(groups, member, &group, Vec::new(), now)
        .await
        .map_err(CreateGroupError::App)?;
    Ok(epoch)
}

/// Which epoch advance a pending advance makes — a membership change, or none.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum AdvanceKind {
    /// `rooms/owner/group/add`. The default because every row written before
    /// the field existed was one.
    #[default]
    Admission,
    /// `rooms/owner/group/remove`.
    Removal,
    /// `rooms/owner/group/renew`: a commit that adds and removes nobody.
    Renewal,
}

impl AdvanceKind {
    /// The noun an operator reads.
    pub fn noun(self) -> &'static str {
        match self {
            Self::Admission => "admission",
            Self::Removal => "removal",
            Self::Renewal => "renewal",
        }
    }
}

/// An epoch advance the host has not yet confirmed — items 3 to 6 of
/// `rooms/owner/group/add/0.1`, `rooms/owner/group/remove/0.1` and
/// `rooms/owner/group/renew/0.1`.
///
/// # Why the whole advanced group, and not just the commit
///
/// MLS lets a member process other members' commits from the wire, never its
/// own: the committer's path secrets exist only in the state that produced the
/// commit. So once the change is built, the *advanced group* is the only way to
/// reach the next epoch. It is kept here, beside the live group it would
/// replace, until the host's answer says which of the two is true.
///
/// The live group is not touched until then. A record sealed while an advance
/// is pending is sealed at the epoch the room is still at — which, for a
/// removal, the member being removed can still read. That is the cost of
/// remote-first, and it lasts only until the host answers.
///
/// Serialises as the admission-only row it generalises did, plus `kind`, so a
/// row written before removal existed reads as an admission.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PendingAdvance {
    /// Admission, removal or renewal.
    #[serde(default)]
    pub kind: AdvanceKind,
    /// The group after the change.
    pub snapshot: GroupSnapshot,
    /// The live group's links plus the one this advance produced.
    pub links: Vec<EpochLink>,
    /// Who is being admitted or removed. A renewal changes nobody's membership, and
    /// names the committer — the owner persona whose leaf it updates.
    pub member: String,
    /// The host the epoch advance goes to, so a later request can reconcile
    /// against the same one.
    pub host: String,
    /// The room epoch the change produces.
    pub epoch: u32,
    /// The rung this advance produced, for `rooms/epoch/mint`.
    pub link: Option<EpochLink>,
    /// The commit, base64url, for `rooms/epoch/mint`.
    pub commit: String,
    /// The Welcome, base64url, for the newcomer's key holder. An admission's
    /// only; a removal welcomes nobody.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub welcome: Option<String>,
    /// The admitted KeyPackage's `KeyPackageRef`, base64url. An admission's only — what
    /// the admission is remembered by once applied (`rooms/owner/group/add/0.2` item 8). An
    /// admission retained before the field existed has none, and is applied without being
    /// retained: a repeat of it is `alreadyMember`, as it was.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key_package_ref: Option<String>,
    /// Unix seconds it was built.
    pub created_at: u64,
}

/// Why an admission could not be built.
#[derive(Debug)]
pub enum PrepareAddError {
    NoGroup,
    AlreadyMember,
    KeyPackageInvalid(String),
    App(AppError),
}

/// Retain `pending` as the room's one pending advance, durably — the step
/// every advance takes **before** anything is sent to a host (item 3).
///
/// Refuses when the slot is taken: the caller settles what it finds first
/// (item 6), so an occupied slot here is a request racing another for the same
/// room, and overwriting it would lose an advance the host may have accepted.
/// The claim is one atomic insert-if-absent, so of two racing requests exactly
/// one holds the slot.
async fn retain(
    groups: &KeyspaceHandle,
    owner: &Member,
    pending: &PendingAdvance,
) -> Result<(), AppError> {
    let room_id = owner.room_id();
    let taken = |found: &PendingAdvance| {
        AppError::Conflict(format!(
            "room `{room_id}` already has a pending {} to epoch {} waiting on host `{}`; \
             it is settled before another advance is built",
            found.kind.noun(),
            found.epoch,
            found.host
        ))
    };
    let claimed = groups
        .insert_if_absent(owner.key(ADVANCE), pending)
        .await
        .map_err(|e| {
            AppError::Internal(format!("store the pending {}: {e}", pending.kind.noun()))
        })?;
    if !claimed {
        let found = pending_advance(groups, owner)
            .await?
            .unwrap_or_else(|| pending.clone());
        return Err(taken(&found));
    }
    // `rooms/owner/group/add/0.2` item 8: the last admission is kept until the owner builds
    // its next advance of the room. This is that moment — a request after it is no longer a
    // retry of anything (the advance may itself remove the member).
    discard_retained_admission(groups, owner).await
}

/// Build the admission of `member` to `owner`'s group from their KeyPackage and retain it,
/// durably, as a [`PendingAdvance`] — **before** anything is sent to a host
/// (items 2 and 3). The live group is unchanged.
pub async fn prepare_add(
    groups: &KeyspaceHandle,
    owner: &Member,
    host: &str,
    member: &str,
    key_package: &[u8],
    now: u64,
) -> Result<PendingAdvance, PrepareAddError> {
    let room_id = owner.room_id();
    let record = load(groups, owner)
        .await
        .map_err(PrepareAddError::App)?
        .ok_or(PrepareAddError::NoGroup)?;
    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| PrepareAddError::App(AppError::Internal(format!("restore the group: {e}"))))?;
    if group.leaf_of(member).is_some() {
        return Err(PrepareAddError::AlreadyMember);
    }
    let mut room = SealedRoom::new(room_id, group);
    room.add_links(record.links);

    let (change, link) = room
        .add_member(key_package)
        .map_err(|e| PrepareAddError::KeyPackageInvalid(e.to_string()))?;
    // The leaf must be the party the owner named. A KeyPackage is a stranger's
    // bytes; one carrying another identity would admit somebody the owner
    // never meant, under the name they did.
    if room.group().leaf_of(member).is_none() {
        return Err(PrepareAddError::KeyPackageInvalid(format!(
            "the KeyPackage's leaf does not identify `{member}`"
        )));
    }
    let welcome = change.welcome.ok_or_else(|| {
        PrepareAddError::App(AppError::Internal("an add produced no Welcome".into()))
    })?;
    // What the admission is remembered by once applied (`rooms/owner/group/add/0.2` item 8).
    // The add above already validated the KeyPackage, so this cannot fail on its bytes.
    let key_package_ref = vti_rooms::mls::key_package_ref(key_package)
        .map_err(|e| PrepareAddError::KeyPackageInvalid(e.to_string()))?;

    let pending = PendingAdvance {
        kind: AdvanceKind::Admission,
        snapshot: room.group().snapshot().map_err(|e| {
            PrepareAddError::App(AppError::Internal(format!("snapshot the group: {e}")))
        })?,
        links: room.links(),
        member: member.to_string(),
        host: host.to_string(),
        epoch: room.room_epoch(),
        link,
        commit: B64.encode(&change.commit),
        welcome: Some(B64.encode(&welcome)),
        key_package_ref: Some(B64.encode(&key_package_ref)),
        created_at: now,
    };
    retain(groups, owner, &pending)
        .await
        .map_err(PrepareAddError::App)?;
    Ok(pending)
}

/// Why a removal could not be built — `rooms/owner/group/remove/0.1` item 1.
#[derive(Debug)]
pub enum PrepareRemoveError {
    /// This agent holds no group for the room.
    NoGroup,
    /// `member` has no leaf in the group.
    NotMember,
    /// `member` is this agent's own leaf. A key holder does not remove itself.
    OwnLeaf,
    App(AppError),
}

/// Build the removal of `member`'s leaf from `owner`'s group and retain it, durably, as a
/// [`PendingAdvance`] — **before** anything is sent to a host
/// (`rooms/owner/group/remove/0.1` items 1 to 3). The live group is unchanged:
/// the commit and the link are made on a copy, which is what is kept.
///
/// `ownLeaf` is `owner`'s own persona only. Another persona this VTA holds for somebody
/// else is a different member of the room, and removable like any other.
pub async fn prepare_remove(
    groups: &KeyspaceHandle,
    owner: &Member,
    host: &str,
    member: &str,
    now: u64,
) -> Result<PendingAdvance, PrepareRemoveError> {
    let room_id = owner.room_id();
    if owner.persona() == member {
        return Err(PrepareRemoveError::OwnLeaf);
    }
    let record = load(groups, owner)
        .await
        .map_err(PrepareRemoveError::App)?
        .ok_or(PrepareRemoveError::NoGroup)?;
    if record.member_did == member {
        return Err(PrepareRemoveError::OwnLeaf);
    }
    let group = RoomGroup::restore(&record.snapshot).map_err(|e| {
        PrepareRemoveError::App(AppError::Internal(format!("restore the group: {e}")))
    })?;
    let leaf = group.leaf_of(member).ok_or(PrepareRemoveError::NotMember)?;
    let mut room = SealedRoom::new(room_id, group);
    room.add_links(record.links);

    // Item 2: the commit and the link that seals the outgoing epoch's key under the
    // incoming one — so the members who remain keep reading what came before, and the
    // one removed reads nothing after.
    let (change, link) = room.remove_member(leaf).map_err(|e| {
        PrepareRemoveError::App(AppError::Internal(format!("build the removal: {e}")))
    })?;

    let pending = PendingAdvance {
        kind: AdvanceKind::Removal,
        snapshot: room.group().snapshot().map_err(|e| {
            PrepareRemoveError::App(AppError::Internal(format!("snapshot the group: {e}")))
        })?,
        links: room.links(),
        member: member.to_string(),
        host: host.to_string(),
        epoch: room.room_epoch(),
        link,
        commit: B64.encode(&change.commit),
        welcome: None,
        key_package_ref: None,
        created_at: now,
    };
    retain(groups, owner, &pending)
        .await
        .map_err(PrepareRemoveError::App)?;
    Ok(pending)
}

/// Build the renewal of `owner`'s group — a commit that adds and removes nobody, updating
/// the owner's own leaf — and retain it, durably, as a [`PendingAdvance`] **before**
/// anything is sent to a host (`rooms/owner/group/renew/0.1` items 2 and 3). The live
/// group is unchanged: the commit and the link are made on a copy, which is what is kept.
///
/// `None` when this agent holds no group for the room (item 1, `noGroup`).
pub async fn prepare_renew(
    groups: &KeyspaceHandle,
    owner: &Member,
    host: &str,
    now: u64,
) -> Result<Option<PendingAdvance>, AppError> {
    let Some(record) = load(groups, owner).await? else {
        return Ok(None);
    };
    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| AppError::Internal(format!("restore the group: {e}")))?;
    let mut room = SealedRoom::new(owner.room_id(), group);
    room.add_links(record.links);

    // Item 2 (and item 7): an update of the committer's own leaf, carrying no proposal
    // that adds or removes anybody, and the link sealing the outgoing epoch's key under
    // the incoming one so every member who applies it keeps reading what came before.
    let (change, link) = room
        .renew()
        .map_err(|e| AppError::Internal(format!("build the renewal: {e}")))?;

    let pending = PendingAdvance {
        kind: AdvanceKind::Renewal,
        snapshot: room
            .group()
            .snapshot()
            .map_err(|e| AppError::Internal(format!("snapshot the group: {e}")))?,
        links: room.links(),
        member: owner.persona().to_string(),
        host: host.to_string(),
        epoch: room.room_epoch(),
        link,
        commit: B64.encode(&change.commit),
        welcome: None,
        key_package_ref: None,
        created_at: now,
    };
    retain(groups, owner, &pending).await?;
    Ok(Some(pending))
}

/// The advance `owner`'s group has waiting on its host, if any — admission, removal or
/// renewal.
///
/// The admission-only slot an earlier build wrote (`room-add:<room>`) is moved here by
/// [`migrate_legacy`], so a row left there is settled, not lost.
pub async fn pending_advance(
    groups: &KeyspaceHandle,
    owner: &Member,
) -> Result<Option<PendingAdvance>, AppError> {
    groups
        .get::<PendingAdvance>(owner.key(ADVANCE))
        .await
        .map_err(|e| AppError::Internal(format!("read the pending advance: {e}")))
}

/// The host accepted the advance: the pending group becomes the live one
/// (item 5). One write replaces the group, then the pending row goes; a crash
/// between the two leaves a pending row whose epoch the live group already
/// has, which [`reconcile_pending_advance`] recognises and clears.
pub async fn promote_pending_advance(
    groups: &KeyspaceHandle,
    owner: &Member,
    pending: &PendingAdvance,
    now: u64,
) -> Result<(), AppError> {
    load(groups, owner).await?.ok_or_else(|| no_group(owner))?;
    let group = RoomGroup::restore(&pending.snapshot)
        .map_err(|e| AppError::Internal(format!("restore the pending group: {e}")))?;
    store(groups, owner, &group, pending.links.clone(), now).await?;
    // Before the pending row goes: a crash between the two leaves the pending row, which
    // the next request recognises as promoted and retains again.
    retain_admission(groups, owner, pending, now).await?;
    discard_pending_advance(groups, owner).await
}

/// An applied admission becomes the room's retained admission (`rooms/owner/group/add/0.2`
/// items 6 and 8). A removal or renewal retains nothing — and was preceded, when it was
/// built, by the discard of whatever was retained.
async fn retain_admission(
    groups: &KeyspaceHandle,
    owner: &Member,
    pending: &PendingAdvance,
    now: u64,
) -> Result<(), AppError> {
    let (AdvanceKind::Admission, Some(welcome), Some(key_package_ref)) =
        (pending.kind, &pending.welcome, &pending.key_package_ref)
    else {
        return Ok(());
    };
    groups
        .insert(
            owner.key(ADMITTED),
            &RetainedAdmission {
                member: pending.member.clone(),
                key_package_ref: key_package_ref.clone(),
                epoch: pending.epoch,
                welcome: welcome.clone(),
                ratchet_tree: None,
                applied_at: now,
            },
        )
        .await
        .map_err(|e| AppError::Internal(format!("retain the applied admission: {e}")))
}

/// The admission `owner` last applied in the room, while it is still kept
/// (`rooms/owner/group/add/0.2` item 8). One kept past its 24 hours is discarded here, so
/// it is never served late even before the sweep reaches it.
pub async fn retained_admission(
    groups: &KeyspaceHandle,
    owner: &Member,
    now: u64,
) -> Result<Option<RetainedAdmission>, AppError> {
    let row: Option<RetainedAdmission> = groups
        .get(owner.key(ADMITTED))
        .await
        .map_err(|e| AppError::Internal(format!("read the retained admission: {e}")))?;
    match row {
        Some(r) if r.kept_at(now) => Ok(Some(r)),
        Some(_) => {
            discard_retained_admission(groups, owner).await?;
            Ok(None)
        }
        None => Ok(None),
    }
}

async fn discard_retained_admission(
    groups: &KeyspaceHandle,
    owner: &Member,
) -> Result<(), AppError> {
    groups
        .remove(owner.key(ADMITTED))
        .await
        .map_err(|e| AppError::Internal(format!("discard the retained admission: {e}")))
}

/// Discard every retained admission past its 24 hours (`rooms/owner/group/add/0.2` item 8,
/// "and MUST discard it then"), for the storage thread's sweep. [`retained_admission`]
/// already refuses to serve one late; this is what removes the Welcome from disk when
/// nobody asks again. Returns how many were discarded.
pub async fn sweep_retained_admissions(
    groups: &KeyspaceHandle,
    now: u64,
) -> Result<usize, AppError> {
    let rows = groups
        .prefix_iter_raw(format!("{ADMITTED}/").into_bytes())
        .await
        .map_err(|e| AppError::Internal(format!("scan retained admissions: {e}")))?;
    let mut discarded = 0;
    for (key, raw) in rows {
        // A row that does not read is not one this build can ever serve: discard it too.
        let keep = serde_json::from_slice::<RetainedAdmission>(&raw).is_ok_and(|r| r.kept_at(now));
        if !keep {
            groups
                .remove(key)
                .await
                .map_err(|e| AppError::Internal(format!("discard a retained admission: {e}")))?;
            discarded += 1;
        }
    }
    Ok(discarded)
}

/// The host refused, or the room has moved past it: forget the advance
/// (items 5 and 6). The live group never changed.
pub async fn discard_pending_advance(
    groups: &KeyspaceHandle,
    owner: &Member,
) -> Result<(), AppError> {
    groups
        .remove(owner.key(ADVANCE))
        .await
        .map_err(|e| AppError::Internal(format!("clear the pending advance: {e}")))
}

/// What a found pending advance turned out to be, given the host's epoch.
#[derive(Debug, PartialEq, Eq)]
pub enum Reconciled {
    /// The host is at the pending epoch: it accepted. Applied.
    Applied,
    /// The host is at the live group's epoch: it never did. Discarded.
    Discarded,
    /// The host is somewhere else — another admin advanced the room. Discarded,
    /// and the live group is behind the host.
    Conflict,
}

/// Item 6: settle a pending advance against the epoch the host reports.
pub async fn reconcile_pending_advance(
    groups: &KeyspaceHandle,
    owner: &Member,
    pending: &PendingAdvance,
    host_epoch: u32,
    now: u64,
) -> Result<Reconciled, AppError> {
    let live_epoch = match load(groups, owner).await? {
        Some(r) => SealedRoom::new(
            owner.room_id(),
            RoomGroup::restore(&r.snapshot)
                .map_err(|e| AppError::Internal(format!("restore the group: {e}")))?,
        )
        .room_epoch(),
        None => 0,
    };
    if live_epoch == pending.epoch {
        // Promoted already; only the pending row survived a crash — perhaps before the
        // admission was retained, so it is retained (again) here.
        retain_admission(groups, owner, pending, now).await?;
        discard_pending_advance(groups, owner).await?;
        return Ok(Reconciled::Applied);
    }
    if host_epoch == pending.epoch {
        promote_pending_advance(groups, owner, pending, now).await?;
        return Ok(Reconciled::Applied);
    }
    discard_pending_advance(groups, owner).await?;
    Ok(if host_epoch == live_epoch {
        Reconciled::Discarded
    } else {
        Reconciled::Conflict
    })
}

// ─── Migration from the per-room keying ──────────────────────────────────

/// Rooms with a group row the per-room build wrote (`room-group:<roomId>`), not yet moved.
pub async fn legacy_rooms(groups: &KeyspaceHandle) -> Result<Vec<String>, AppError> {
    let keys = groups
        .prefix_keys(legacy::GROUP_PREFIX.as_bytes().to_vec())
        .await?;
    Ok(keys
        .into_iter()
        .filter_map(|k| {
            String::from_utf8_lossy(&k)
                .strip_prefix(legacy::GROUP_PREFIX)
                .map(str::to_string)
        })
        .collect())
}

/// The group row the per-room build wrote for `room_id`, if it is still there.
pub async fn legacy_group(
    groups: &KeyspaceHandle,
    room_id: &str,
) -> Result<Option<RoomGroupRecord>, AppError> {
    groups
        .get(legacy::group(room_id))
        .await
        .map_err(|e| AppError::Internal(format!("read legacy group state for `{room_id}`: {e}")))
}

/// What [`migrate_legacy`] did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Migrated {
    /// Nothing to move.
    Nothing,
    /// The room's rows now live under the member.
    Moved,
    /// The member already holds group state of its own for the room: the legacy row was
    /// left where it is, unmerged, rather than chosen between.
    Kept,
}

/// Move what a build keyed by room alone holds for `member.room_id()` under `member`.
///
/// The caller decides who the rows belong to ([`crate::operations::room_member`] — the
/// persona the group's own leaf identifies); this only moves them, so it is safe to run on
/// every touch and from any number of requests at once:
///
/// - The pending KeyPackage, advance (either slot) and root memory move first, the group
///   row last. Each is one atomic [`KeyspaceHandle::move_if_unchanged`], so a crash leaves
///   the group row in place and the next touch finishes the job.
/// - A KeyPackage is moved only when its leaf identifies `member`: a package minted for
///   somebody else is not theirs to join with.
/// - Nothing already under `member` is overwritten. A target that exists wins, and the
///   legacy row is left alone rather than merged — two group states are not reconcilable.
pub async fn migrate_legacy(
    groups: &KeyspaceHandle,
    member: &Member,
) -> Result<Migrated, AppError> {
    let room_id = member.room_id();
    let Some(group_raw) = groups.get_raw(legacy::group(room_id)).await? else {
        return Ok(Migrated::Nothing);
    };

    // The KeyPackage, only if it is this member's.
    if let Some(raw) = groups.get_raw(legacy::key_package(room_id)).await?
        && let Ok(kp) = serde_json::from_slice::<PendingKeyPackage>(&raw)
        && kp.snapshot.member_did() == member.persona()
    {
        move_row(
            groups,
            legacy::key_package(room_id),
            raw,
            member.key(KEY_PACKAGE),
            &kp,
        )
        .await?;
    }
    // The advance — the generalised slot, then the admission-only one. One slot per member,
    // so whichever arrives second finds it taken and stays put (and is reported below).
    for old in [legacy::advance(room_id), legacy::add(room_id)] {
        if let Some(raw) = groups.get_raw(old.clone()).await?
            && let Ok(advance) = serde_json::from_slice::<PendingAdvance>(&raw)
        {
            move_row(groups, old, raw, member.key(ADVANCE), &advance).await?;
        }
    }
    if let Some(raw) = groups.get_raw(legacy::roots(room_id)).await?
        && let Ok(roots) = serde_json::from_slice::<RootHistory>(&raw)
    {
        move_row(
            groups,
            legacy::roots(room_id),
            raw,
            member.key(ROOTS),
            &roots,
        )
        .await?;
    }

    let record: RoomGroupRecord = serde_json::from_slice(&group_raw).map_err(|e| {
        AppError::Internal(format!("decode legacy group state for `{room_id}`: {e}"))
    })?;
    Ok(
        match move_row(
            groups,
            legacy::group(room_id),
            group_raw,
            member.key(GROUP),
            &record,
        )
        .await?
        {
            MoveOutcome::Moved | MoveOutcome::SourceMissing | MoveOutcome::SourceChanged => {
                Migrated::Moved
            }
            MoveOutcome::TargetExists => {
                tracing::warn!(
                    room = %room_id,
                    member = %member.persona(),
                    "a group row from before per-member keying was left in place: this \
                     member already holds group state of its own for the room"
                );
                Migrated::Kept
            }
        },
    )
}

async fn move_row<V: serde::Serialize>(
    groups: &KeyspaceHandle,
    old: String,
    expected: Vec<u8>,
    new: String,
    value: &V,
) -> Result<MoveOutcome, AppError> {
    groups
        .move_if_unchanged(old, expected, new, value)
        .await
        .map_err(|e| AppError::Internal(format!("migrate room state: {e}")))
}

/// Write rows as the per-room build did, for tests of an upgraded store.
#[cfg(test)]
pub(crate) mod legacy_for_test {
    use super::*;

    pub(crate) async fn store_group(
        groups: &KeyspaceHandle,
        room_id: &str,
        record: &RoomGroupRecord,
    ) {
        groups.insert(legacy::group(room_id), record).await.unwrap();
    }

    pub(crate) async fn store_pending_add(
        groups: &KeyspaceHandle,
        room_id: &str,
        pending: &serde_json::Value,
    ) {
        groups.insert(legacy::add(room_id), pending).await.unwrap();
    }
}

/// Store a group as `join` would, for tests that need a room held at some epoch.
#[cfg(test)]
pub(crate) async fn store_for_test(
    groups: &KeyspaceHandle,
    member: &Member,
    group: &RoomGroup,
    links: Vec<EpochLink>,
) -> Result<(), AppError> {
    store(groups, member, group, links, 0).await
}

/// Move the retained admission's `appliedAt` `secs` into the past, for tests of item 8's
/// 24 hours.
#[cfg(test)]
pub(crate) async fn age_retained_admission_for_test(
    groups: &KeyspaceHandle,
    owner: &Member,
    secs: u64,
) {
    let mut row: RetainedAdmission = groups.get(owner.key(ADMITTED)).await.unwrap().unwrap();
    row.applied_at = row.applied_at.saturating_sub(secs);
    groups.insert(owner.key(ADMITTED), &row).await.unwrap();
}

/// Whether a retained admission row exists at all, expired or not.
#[cfg(test)]
pub(crate) async fn has_retained_admission_row(groups: &KeyspaceHandle, owner: &Member) -> bool {
    groups.get_raw(owner.key(ADMITTED)).await.unwrap().is_some()
}

/// Record that a commit removed `member` at `epoch`, as [`apply_commit`] does on
/// processing it — for tests of how a removed member's refusals read.
#[cfg(test)]
pub(crate) async fn record_removal_for_test(
    groups: &KeyspaceHandle,
    member: &Member,
    epoch: u32,
) -> Result<(), AppError> {
    record_removal(groups, member, epoch, 0).await
}

/// How many `(headVersion, root)` observations to keep per room.
///
/// One is weaker than it looks in a specific way, and stronger than it looks in
/// another. It *does* catch a host that alternates at one version — `(V, A)`
/// then `(V, B)` differ, and differ again on the way back. What it misses is a
/// head that **advances and then goes backwards**: `(V, A)`, `(V+1, X)`,
/// `(V, B)` leaves the agent holding `V+1` with nothing to compare `V` against.
/// That is the mirror case and the rollback case, which are the two this is for.
///
/// Sixteen versions is more history than a member reads across in a session, and
/// a few hundred bytes.
const KEEP_ROOTS: usize = 16;

/// What a host has said about a room's tree, over time.
///
/// **Keyed by room and member, not by host**, and the host half is the decision worth
/// not undoing. A
/// room's tree at version `V` is a fact about the *room*; who served it is not
/// part of that fact. Keying by `(room, host)` would file two hosts of one room
/// in two drawers and never compare them — and a room may deliberately have
/// several, a mirror serving reads while its primary takes writes.
///
/// Keyed this way, a member who reads the same room from two of its hosts gets
/// the comparison for free: two hosts reporting **different roots at one
/// `headVersion`** is exactly as damning as one host disagreeing with itself.
/// That is a comparison the specification's own list does not include, and it
/// needs no gossip channel, no anchor and no second member.
///
/// The member half is context isolation. It is a record of when one member read one room,
/// and two personas sharing this VTA are two people whose reading is none of each other's
/// business — so each keeps its own, as each keeps its own group.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RootHistory {
    /// Observations, most recent `headVersion` last. At most [`KEEP_ROOTS`].
    pub seen: Vec<RootObservation>,
    /// The highest `headVersion` ever seen for this room.
    ///
    /// Separate from `seen` because it answers a question no single pair can:
    /// **has this room gone backwards?** A pruned observation still leaves this
    /// behind.
    pub highest: u64,
    /// The version at which this room was caught, if it ever was.
    ///
    /// **Remembered rather than recomputed.** A conflict is a fact about the
    /// past, and the pair that revealed it is prunable — an agent that inferred
    /// "caught" from what it still holds would exonerate a host by doing enough
    /// reading. Once set it is never cleared here: it is cleared by
    /// [`forget`], with the membership.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub caught_at: Option<u64>,
}

/// One thing a host said about a room's tree.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RootObservation {
    pub head_version: u64,
    /// The root, as the wire spells it — a `DigestMultibase`.
    pub root: String,
}

/// What comparing a root against what is held came to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RootVerdict {
    /// Seen at this head before, same root.
    Agree,
    /// Seen at this head before, **different root**. A host caught: there is no
    /// write to attribute the difference to, because a write would have moved
    /// the head.
    Conflict,
    /// First observation at this head. Not evidence of anything — a memory of
    /// one is not a comparison.
    NoneHeld,
}

/// Record what a host asserted, and say what it came to.
///
/// The write happens either way, including on a conflict: an agent that stopped
/// recording once it caught a host would forget the evidence at the moment it
/// acquired it.
pub async fn observe_head(
    groups: &KeyspaceHandle,
    member: &Member,
    head_version: u64,
    root: &str,
) -> Result<RootVerdict, AppError> {
    let room_id = member.room_id();
    let key = member.key(ROOTS);
    let mut history = root_history(groups, member).await?;

    let verdict = match history.seen.iter().find(|o| o.head_version == head_version) {
        Some(prior) if prior.root == root => RootVerdict::Agree,
        Some(_) => RootVerdict::Conflict,
        None => RootVerdict::NoneHeld,
    };

    if matches!(verdict, RootVerdict::Conflict) {
        // Earliest wins: the first version at which this host was caught is the
        // one worth naming, and later conflicts do not make the first less true.
        history.caught_at = Some(
            history
                .caught_at
                .map_or(head_version, |v| v.min(head_version)),
        );
    }
    if matches!(verdict, RootVerdict::NoneHeld) {
        history.seen.push(RootObservation {
            head_version,
            root: root.to_string(),
        });
        history.seen.sort_by_key(|o| o.head_version);
        // Drop the OLDEST versions, not the oldest writes: what a member is
        // likely to read again is what the room is at now.
        if history.seen.len() > KEEP_ROOTS {
            let excess = history.seen.len() - KEEP_ROOTS;
            history.seen.drain(0..excess);
        }
    }
    history.highest = history.highest.max(head_version);

    groups
        .insert(key, &history)
        .await
        .map_err(|e| AppError::Internal(format!("record the root history of `{room_id}`: {e}")))?;
    Ok(verdict)
}

/// Everything this agent has been told about a room's tree, for a caller that
/// wants to compare out of band.
///
/// A member with none of its own reads the one the per-room build kept, if any: that
/// memory was this VTA's before members were told apart, and dropping it on upgrade would
/// exonerate a host it had already caught. The member's first observation then writes it
/// forward under the member.
pub async fn root_history(
    groups: &KeyspaceHandle,
    member: &Member,
) -> Result<RootHistory, AppError> {
    let room_id = member.room_id();
    let read =
        |e: AppError| AppError::Internal(format!("read the root history of `{room_id}`: {e}"));
    if let Some(h) = groups.get(member.key(ROOTS)).await.map_err(read)? {
        return Ok(h);
    }
    Ok(groups
        .get(legacy::roots(room_id))
        .await
        .map_err(read)?
        .unwrap_or_default())
}

/// Discard everything this VTA holds for a room.
///
/// Called on removal from the group. A key-holder that kept its state would retain the
/// ability to open everything sealed up to the epoch it was removed at — which is exactly
/// what the removal was for.
///
/// One member's, and only theirs: another persona on this VTA in the same room was not
/// removed.
pub async fn forget(groups: &KeyspaceHandle, member: &Member) -> Result<(), AppError> {
    let room_id = member.room_id();
    groups
        .remove(member.key(GROUP))
        .await
        .map_err(|e| AppError::Internal(format!("discard group state for `{room_id}`: {e}")))?;
    groups
        .remove(member.key(KEY_PACKAGE))
        .await
        .map_err(|e| AppError::Internal(format!("discard a pending key package: {e}")))?;
    // An advance this agent built for a group it no longer holds can never be
    // promoted; leaving it would hand whoever rejoins a stale successor to settle.
    discard_pending_advance(groups, member).await?;
    discard_retained_admission(groups, member).await?;
    // The root history goes with the membership. It carries no record content,
    // but it is a record of when this member read this room, and an agent that
    // kept it afterwards would be keeping a diary of a room its principal can no
    // longer open.
    groups
        .remove(member.key(ROOTS))
        .await
        .map_err(|e| AppError::Internal(format!("discard the root history of `{room_id}`: {e}")))
}

/// The MLS epoch authenticator for a room this agent holds.
///
/// Every member of the group derives this independently and **no host can
/// compute it**, which is the whole reason it is worth anchoring: a member whose
/// own authenticator differs from the anchored one is in a *forked group*.
pub async fn epoch_authenticator(
    groups: &KeyspaceHandle,
    member: &Member,
) -> Result<(u64, Vec<u8>), AppError> {
    let room_id = member.room_id();
    let record = load(groups, member).await?.ok_or_else(|| {
        AppError::Validation(format!(
            "this agent holds no group state for `{room_id}`, so it cannot say what epoch the \
             room is at"
        ))
    })?;
    let group = RoomGroup::restore(&record.snapshot)
        .map_err(|e| AppError::Internal(format!("restore the group for `{room_id}`: {e}")))?;
    // The epoch travels with it: an authenticator names the epoch it was
    // derived at, and one published beside a different number is a statement
    // about a group nobody is in.
    Ok((group.epoch(), group.epoch_authenticator()))
}

#[cfg(test)]
mod root_memory_tests {
    use super::*;
    use vti_common::config::StoreConfig;
    use vti_common::store::Store;

    const ROOM: &str = "did:webvh:example.com:rooms:northwind";

    fn member() -> Member {
        Member::new(ROOM, "did:key:zAlice").unwrap()
    }
    const A: &str = "zQmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR";
    const B: &str = "zQmXo1sV5aJ7bT2kQdF9wRnPzYcH4uMgLtEjV6NrBqWsDpK";

    async fn open() -> (tempfile::TempDir, KeyspaceHandle) {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::open(&StoreConfig {
            data_dir: dir.path().to_path_buf(),
        })
        .unwrap();
        let ks = store.keyspace(crate::keyspaces::ROOM_GROUPS).unwrap();
        (dir, ks)
    }

    /// A memory of one is not a comparison, and the first read must say so.
    #[tokio::test]
    async fn a_first_reading_holds_nothing_to_compare() {
        let (_d, ks) = open().await;
        assert_eq!(
            observe_head(&ks, &member(), 412, A).await.unwrap(),
            RootVerdict::NoneHeld
        );
    }

    /// The whole mechanism: same state, two roots, no write to blame.
    #[tokio::test]
    async fn two_roots_at_one_version_is_a_host_caught() {
        let (_d, ks) = open().await;
        observe_head(&ks, &member(), 412, A).await.unwrap();
        assert_eq!(
            observe_head(&ks, &member(), 412, A).await.unwrap(),
            RootVerdict::Agree
        );
        assert_eq!(
            observe_head(&ks, &member(), 412, B).await.unwrap(),
            RootVerdict::Conflict
        );
    }

    /// A room that moved is not a host that lied, and the version is what tells
    /// them apart. Without it every second read would look like equivocation.
    #[tokio::test]
    async fn a_different_version_is_a_different_moment() {
        let (_d, ks) = open().await;
        observe_head(&ks, &member(), 412, A).await.unwrap();
        assert_eq!(
            observe_head(&ks, &member(), 413, B).await.unwrap(),
            RootVerdict::NoneHeld,
            "a write moved the head, so a different root explains itself"
        );
    }

    /// One slot would miss this, which is why the memory is a map.
    ///
    /// The head advances and then a host serves the older version again — the
    /// mirror case and the rollback case. An agent keeping only the latest pair
    /// is holding `V+1` and has nothing to compare `V` against.
    #[tokio::test]
    async fn a_head_that_goes_backwards_is_still_compared() {
        let (_d, ks) = open().await;
        observe_head(&ks, &member(), 412, A).await.unwrap();
        observe_head(&ks, &member(), 500, B).await.unwrap();
        assert_eq!(
            observe_head(&ks, &member(), 412, B).await.unwrap(),
            RootVerdict::Conflict,
            "the older version is still held, and its root still disagrees"
        );
    }

    /// The bound drops the OLDEST versions and keeps the highest-ever anyway,
    /// because "has this room gone backwards" is a question no single pair can
    /// answer.
    #[tokio::test]
    async fn the_history_is_bounded_and_the_high_water_mark_survives_it() {
        let (_d, ks) = open().await;
        for v in 1..=(KEEP_ROOTS as u64 + 5) {
            observe_head(&ks, &member(), v, A).await.unwrap();
        }
        let history = root_history(&ks, &member()).await.unwrap();
        assert_eq!(history.seen.len(), KEEP_ROOTS);
        assert_eq!(history.highest, KEEP_ROOTS as u64 + 5);
        assert_eq!(
            history.seen.first().unwrap().head_version,
            6,
            "the oldest versions are what gets dropped"
        );
    }

    /// Bounded by membership. An agent that kept this after a removal would keep
    /// a record of when its principal read a room it can no longer open.
    #[tokio::test]
    async fn forgetting_a_room_forgets_what_its_host_said() {
        let (_d, ks) = open().await;
        observe_head(&ks, &member(), 412, A).await.unwrap();
        forget(&ks, &member()).await.unwrap();
        assert_eq!(
            observe_head(&ks, &member(), 412, B).await.unwrap(),
            RootVerdict::NoneHeld,
            "nothing survived the removal to compare against"
        );
    }
}

#[cfg(test)]
mod file_key_tests {
    use super::*;
    use vti_common::config::StoreConfig;
    use vti_common::store::Store;

    const ROOM: &str = "did:webvh:example.com:rooms:northwind";

    fn member() -> Member {
        Member::new(ROOM, "did:key:zAlice").unwrap()
    }

    async fn open() -> (tempfile::TempDir, KeyspaceHandle) {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::open(&StoreConfig {
            data_dir: dir.path().to_path_buf(),
        })
        .unwrap();
        let ks = store.keyspace(crate::keyspaces::ROOM_GROUPS).unwrap();
        (dir, ks)
    }

    /// A room this VTA holds at epoch 2, with the rung back to 1 when `chained`.
    async fn held(ks: &KeyspaceHandle, chained: bool) -> SealedRoom {
        let mut room = SealedRoom::new(ROOM, RoomGroup::create("did:key:zAlice").unwrap());
        let (_bob, kp) = IdentitySnapshot::mint("did:key:zBob").unwrap();
        room.add_member(&kp).unwrap();
        let links = if chained { room.links() } else { Vec::new() };
        store(ks, &member(), room.group(), links, 0).await.unwrap();
        room
    }

    #[tokio::test]
    async fn a_sealing_key_is_the_current_epoch_and_opens_later() {
        let (_d, ks) = open().await;
        let room = held(&ks, true).await;
        let id = [4u8; 32];

        let (seal_key, epoch) = file_key(&ks, &member(), &id, None).await.unwrap();
        assert_eq!(epoch, 2, "a sealing key is always the current epoch");
        assert_eq!(seal_key, room.file_key_for_seal(&id).unwrap().0);

        let (open_key, e) = file_key(&ks, &member(), &id, Some(2)).await.unwrap();
        assert_eq!((open_key, e), (seal_key, 2));
    }

    #[tokio::test]
    async fn an_earlier_epoch_is_reached_through_the_chain() {
        let (_d, ks) = open().await;
        held(&ks, true).await;
        let (k1, e) = file_key(&ks, &member(), &[4u8; 32], Some(1)).await.unwrap();
        assert_eq!(e, 1);
        assert_ne!(
            k1,
            file_key(&ks, &member(), &[4u8; 32], Some(2))
                .await
                .unwrap()
                .0
        );
    }

    #[tokio::test]
    async fn the_two_unreachable_cases_say_which_they_are() {
        let (_d, ks) = open().await;
        held(&ks, false).await;
        assert!(matches!(
            file_key(&ks, &member(), &[4u8; 32], Some(3)).await,
            Err(FileKeyError::NotDelivered { epoch: 3, held: 2 })
        ));
        assert!(matches!(
            file_key(&ks, &member(), &[4u8; 32], Some(1)).await,
            Err(FileKeyError::BeyondChain {
                epoch: 1,
                earliest: 2
            })
        ));
    }

    #[tokio::test]
    async fn no_group_is_not_found() {
        let (_d, ks) = open().await;
        assert!(matches!(
            file_key(&ks, &member(), &[4u8; 32], None).await,
            Err(FileKeyError::NoGroup)
        ));
    }
}

#[cfg(test)]
mod per_member_storage_tests {
    use super::*;
    use vti_common::config::StoreConfig;
    use vti_common::store::Store;

    const ROOM: &str = "did:webvh:example.com:rooms:northwind";
    const ALICE: &str = "did:key:zAlice";
    const BOB: &str = "did:key:zBob";

    async fn open() -> (tempfile::TempDir, KeyspaceHandle) {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::open(&StoreConfig {
            data_dir: dir.path().to_path_buf(),
        })
        .unwrap();
        let ks = store.keyspace(crate::keyspaces::ROOM_GROUPS).unwrap();
        (dir, ks)
    }

    fn m(persona: &str) -> Member {
        Member::new(ROOM, persona).unwrap()
    }

    /// A key that two different (room, persona) pairs could both produce would put one
    /// member's secrets under another's name.
    #[test]
    fn identifiers_that_would_make_the_key_ambiguous_are_refused() {
        assert!(Member::new("did:key:zA/b", ALICE).is_err());
        assert!(Member::new(ROOM, "did:key:zA/b").is_err());
        assert!(Member::new("", ALICE).is_err());
    }

    /// Two members of one room on one VTA are two rows, found by room.
    #[tokio::test]
    async fn two_members_of_one_room_are_two_groups() {
        let (_d, ks) = open().await;
        for who in [ALICE, BOB] {
            create_group(&ks, &m(who), 0).await.unwrap();
        }
        let mut held: Vec<String> = holders(&ks, ROOM)
            .await
            .unwrap()
            .into_iter()
            .map(|m| m.persona().to_string())
            .collect();
        held.sort();
        assert_eq!(held, vec![ALICE.to_string(), BOB.to_string()]);
        assert!(matches!(
            create_group(&ks, &m(ALICE), 0).await,
            Err(CreateGroupError::AlreadyExists { .. })
        ));
    }

    /// A host caught by one member's reading is that member's finding. The other member's
    /// memory is its own — a record of when somebody else read the room is not theirs.
    #[tokio::test]
    async fn root_memory_is_the_members_own() {
        let (_d, ks) = open().await;
        observe_head(&ks, &m(ALICE), 412, "zA").await.unwrap();
        assert_eq!(
            observe_head(&ks, &m(ALICE), 412, "zB").await.unwrap(),
            RootVerdict::Conflict
        );
        assert_eq!(
            observe_head(&ks, &m(BOB), 412, "zB").await.unwrap(),
            RootVerdict::NoneHeld,
            "bob holds no memory of alice's reading"
        );
        assert!(
            root_history(&ks, &m(BOB))
                .await
                .unwrap()
                .caught_at
                .is_none()
        );
    }

    /// Everything the per-room build kept moves with the group, under the member its leaf
    /// identifies; a package minted for somebody else stays put; and a second run is a
    /// no-op rather than a second move.
    #[tokio::test]
    async fn a_legacy_room_moves_whole_under_its_member() {
        let (_d, ks) = open().await;
        let group = RoomGroup::create(ALICE).unwrap();
        let record = RoomGroupRecord {
            snapshot: group.snapshot().unwrap(),
            member_did: ALICE.into(),
            links: Vec::new(),
            updated_at: 7,
        };
        ks.insert(legacy::group(ROOM), &record).await.unwrap();
        let (snapshot, kp) = IdentitySnapshot::mint(BOB).unwrap();
        let bobs_package = PendingKeyPackage {
            snapshot,
            key_package: B64.encode(kp),
            expires_at: 9,
        };
        ks.insert(legacy::key_package(ROOM), &bobs_package)
            .await
            .unwrap();
        ks.insert(
            legacy::roots(ROOM),
            &RootHistory {
                caught_at: Some(3),
                ..Default::default()
            },
        )
        .await
        .unwrap();

        assert_eq!(
            migrate_legacy(&ks, &m(ALICE)).await.unwrap(),
            Migrated::Moved
        );
        assert_eq!(load(&ks, &m(ALICE)).await.unwrap().unwrap().updated_at, 7);
        assert_eq!(
            root_history(&ks, &m(ALICE)).await.unwrap().caught_at,
            Some(3),
            "a host caught before the upgrade stays caught"
        );
        assert!(ks.get_raw(legacy::group(ROOM)).await.unwrap().is_none());
        assert!(ks.get_raw(legacy::roots(ROOM)).await.unwrap().is_none());
        assert!(
            ks.get_raw(legacy::key_package(ROOM))
                .await
                .unwrap()
                .is_some(),
            "bob's package is not alice's to take"
        );
        assert!(
            ks.get_raw(m(ALICE).key(KEY_PACKAGE))
                .await
                .unwrap()
                .is_none()
        );
        assert_eq!(
            migrate_legacy(&ks, &m(ALICE)).await.unwrap(),
            Migrated::Nothing
        );

        // Bob's package moves when bob's Welcome looks for it.
        assert_eq!(
            legacy_key_package(&ks, &m(BOB))
                .await
                .unwrap()
                .map(|p| p.expires_at),
            Some(9)
        );
        assert!(
            ks.get_raw(legacy::key_package(ROOM))
                .await
                .unwrap()
                .is_none()
        );
    }

    /// A member who already holds a group of their own is not given a second one: the
    /// legacy row stays where it is, unmerged.
    #[tokio::test]
    async fn a_legacy_row_never_overwrites_a_members_own_group() {
        let (_d, ks) = open().await;
        create_group(&ks, &m(ALICE), 42).await.unwrap();
        let record = RoomGroupRecord {
            snapshot: RoomGroup::create(ALICE).unwrap().snapshot().unwrap(),
            member_did: ALICE.into(),
            links: Vec::new(),
            updated_at: 7,
        };
        ks.insert(legacy::group(ROOM), &record).await.unwrap();
        assert_eq!(
            migrate_legacy(&ks, &m(ALICE)).await.unwrap(),
            Migrated::Kept
        );
        assert_eq!(load(&ks, &m(ALICE)).await.unwrap().unwrap().updated_at, 42);
        assert!(ks.get_raw(legacy::group(ROOM)).await.unwrap().is_some());
    }
}

#[cfg(test)]
mod removal_tests {
    use super::*;
    use vti_common::config::StoreConfig;
    use vti_common::store::Store;

    const ROOM: &str = "did:webvh:example.com:rooms:northwind";
    const BOB: &str = "did:key:zBob";

    /// After the commit that removes this VTA's member is applied, a record sealed after
    /// the removal says "removed" — not "a commit has not been delivered", whose remedy
    /// (apply the commits) can never open it — and what was sealed before still opens.
    #[tokio::test]
    async fn a_record_sealed_after_the_members_removal_says_removed() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::open(&StoreConfig {
            data_dir: dir.path().to_path_buf(),
        })
        .unwrap();
        let ks = store.keyspace(crate::keyspaces::ROOM_GROUPS).unwrap();
        let bob = Member::new(ROOM, BOB).unwrap();

        let pending = mint_key_package(&ks, &bob, 3600, 0).await.unwrap();
        let mut alice = SealedRoom::new(ROOM, RoomGroup::create("did:key:zAlice").unwrap());
        let (added, _) = alice
            .add_member(&B64.decode(&pending.key_package).unwrap())
            .unwrap();
        join(&ks, &bob, &added.welcome.unwrap(), 0).await.unwrap();

        let before = alice.seal_record("k", 1, b"before").unwrap();
        let leaf = alice.group().leaf_of(BOB).unwrap();
        let (removal, _) = alice.remove_member(leaf).unwrap();
        let after = alice.seal_record("k", 2, b"after").unwrap();

        // Before the removal is applied, this VTA cannot know: it is only behind.
        assert!(matches!(
            open_record_detailed(
                &ks,
                &bob,
                "k",
                2,
                &after.ciphertext,
                &after.nonce,
                after.epoch
            )
            .await,
            Err(OpenError::NotDelivered { .. })
        ));
        let file = [7u8; 32];
        assert!(matches!(
            file_key(&ks, &bob, &file, Some(after.epoch)).await.err(),
            Some(FileKeyError::NotDelivered { .. })
        ));

        let err = apply_commit(&ks, &bob, &removal.commit, removal.epoch, 5)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("removed this holder"), "{err}");
        assert_eq!(removed_at(&ks, &bob).await.unwrap(), Some(after.epoch));

        match open_record_detailed(
            &ks,
            &bob,
            "k",
            2,
            &after.ciphertext,
            &after.nonce,
            after.epoch,
        )
        .await
        {
            Err(OpenError::Removed { epoch, removed_at }) => {
                assert_eq!((epoch, removed_at), (after.epoch, after.epoch))
            }
            other => panic!("expected Removed, got {other:?}"),
        }
        // A file's key is the same question (`rooms/keys/file-key`): sealed at or after
        // the removal is `Removed`, never `NotDelivered`, whose remedy cannot work.
        match file_key(&ks, &bob, &file, Some(after.epoch)).await.err() {
            Some(FileKeyError::Removed { epoch, removed_at }) => {
                assert_eq!((epoch, removed_at), (after.epoch, after.epoch))
            }
            other => panic!("expected FileKeyError::Removed, got {other:?}"),
        }
        assert!(
            file_key(&ks, &bob, &file, Some(before.epoch)).await.is_ok(),
            "a file sealed before the removal still derives"
        );
        assert_eq!(
            open_record(
                &ks,
                &bob,
                "k",
                1,
                &before.ciphertext,
                &before.nonce,
                before.epoch
            )
            .await
            .unwrap(),
            b"before"
        );
    }
}