jmap-chat-server 0.3.0

JMAP Chat extension method handlers — backend-agnostic, plugs into jmap-server Dispatcher
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
//! ChatBackend trait and supporting types for JMAP Chat method handlers.
//!
//! Consumers implement [`ChatBackend`] for their storage system. The method
//! handlers in sibling modules call into the backend through this trait.
//!
//! The read-side operations (`get_objects`, `get_state`, `get_changes`,
//! `query_objects`, `query_changes`) are defined on the [`jmap_server::JmapBackend`]
//! supertrait. Only write operations are here.
//!
//! Marker traits and property selector enums live in `jmap-types` and
//! `jmap-chat-types` respectively; they are re-exported here for convenience.

pub use jmap_chat_types::backend::{
    ChatContactProperty, ChatProperty, MessageProperty, ReadPositionProperty, SpaceProperty,
};
pub use jmap_chat_types::space_set::{SpaceMetadataPatch, SpacePatchOp};
pub use jmap_server::{
    AddedItem, BackendChangesError, BackendQueryError, BackendSetError, ChangesResult, GetObject,
    JmapBackend, JmapObject, QueryChangesResult, QueryObject, QueryResult, SetError, SetErrorType,
    SetObject,
};

// ---------------------------------------------------------------------------
// Space/set structural-mutation result
// ---------------------------------------------------------------------------

/// The outcome of a single [`SpacePatchOp`] applied by
/// [`ChatBackend::apply_space_patch`].
///
/// `op_index` is the zero-based index of the op within the input `Vec`, used
/// by handlers to construct a descriptive error message identifying which
/// per-key entry failed (e.g. `addRoles[2] failed: ...`).
///
/// `outcome` is:
/// - `Ok(Some(id))` — the op produced a new server-assigned id (e.g.
///   [`SpacePatchOp::AddRole`], [`SpacePatchOp::AddChannel`],
///   [`SpacePatchOp::AddCategory`]). The handler reports this id back to
///   the client via the `/set` response.
/// - `Ok(None)` — the op completed but produced no id (every `Remove*` and
///   `Update*` variant).
/// - `Err(SetError)` — the op was rejected (e.g. permission denied, target
///   id not found, role hierarchy violation, count limit exceeded).
///
/// Per RFC 8620 §5.3 `/set`, an update target is per-target atomic on the
/// wire: it appears in exactly one of `updated` or `notUpdated`. If **any**
/// `OpResult` in the returned `Vec` has an `Err`, the handler reports the
/// containing update target in `notUpdated`. The handler is free to choose
/// which `Err` to surface; the reference handler surfaces the first.
///
/// This type lives in `jmap-chat-server` (not `jmap-chat-types`) because
/// [`SetError`] is defined in `jmap-server` and `jmap-chat-types` cannot
/// depend on it (per the workspace dependency rule: types crates depend
/// only on `jmap-types`, `serde`, `serde_json`).
///
/// `#[non_exhaustive]` preserves the ability to add fields without a
/// SemVer break (e.g. an audit-log handle, commit ordering hint,
/// permission-diagnostic annotation). External consumers MUST construct
/// instances via [`OpResult::ok`] or [`OpResult::err`] rather than
/// field-init syntax.
///
/// # Field-addition policy
///
/// Future fields added to `OpResult` are an **additive non-breaking
/// change**: the new field MUST have a `Default` value (typical for
/// `Option` / `Vec` / `HashMap`) and the new field is exposed via a
/// `with_*`-style builder setter rather than being appended as a third
/// positional argument to [`OpResult::ok`] or [`OpResult::err`]. The
/// `ok(op_index, id)` and `err(op_index, error)` constructors therefore
/// stay stable across the crate's pre-1.0 lifetime, matching the
/// [`ChatLimits`]-style canonical workspace pattern for
/// `#[non_exhaustive]` types with builder setters.
#[non_exhaustive]
#[derive(Debug)]
pub struct OpResult {
    /// Zero-based index of the originating op in the input `Vec<SpacePatchOp>`.
    pub op_index: usize,
    /// The outcome of applying that op.
    pub outcome: Result<Option<jmap_types::Id>, SetError>,
}

impl OpResult {
    /// Construct an `Ok` result for a successfully applied op.
    ///
    /// The contained `Id` is the server-assigned id when the op was a
    /// create (the kit's response includes it under `created[create_id].id`);
    /// pass `None` for updates and destroys, which have no id to surface.
    #[must_use]
    pub const fn ok(op_index: usize, id: Option<jmap_types::Id>) -> Self {
        Self {
            op_index,
            outcome: Ok(id),
        }
    }

    /// Construct an `Err` result for a rejected op.
    ///
    /// The kit's `handle_space_set` surfaces this error to the JMAP
    /// `notUpdated` map for the containing update target.
    #[must_use]
    pub const fn err(op_index: usize, error: SetError) -> Self {
        Self {
            op_index,
            outcome: Err(error),
        }
    }

    /// Builder-style setter for [`Self::op_index`].
    ///
    /// Lets callers update the originating op index after construction.
    /// Useful when re-indexing a result produced for one op into the
    /// position of another (e.g. when filtering a batch).
    #[must_use]
    pub const fn with_op_index(mut self, op_index: usize) -> Self {
        self.op_index = op_index;
        self
    }

    /// Builder-style setter for [`Self::outcome`].
    ///
    /// Lets callers replace the outcome after construction. Useful for
    /// retry / fallback flows that re-classify a transient error into a
    /// success or vice versa before surfacing the result to the
    /// handler.
    #[must_use]
    pub fn with_outcome(mut self, outcome: Result<Option<jmap_types::Id>, SetError>) -> Self {
        self.outcome = outcome;
        self
    }
}

// ---------------------------------------------------------------------------
// Per-Space content limits
// ---------------------------------------------------------------------------

/// Implementation-defined per-Space content limits enforced by
/// `Space/set` handlers.
///
/// Per draft-atwood-jmap-chat-00 §Space/set (spec commit `80d5e11`,
/// 2026-05-11), each `add*` op on a Space MUST return an `overQuota`
/// SetError (RFC 8620 §5.3) when the resulting count would exceed a
/// server-defined limit. The spec does not name or normatively define
/// the cap values; they are implementation-defined and may vary per
/// account or per tenant.
///
/// Backends override [`ChatBackend::limits`] to supply their own values.
/// The default impl returns conservative reference-impl-grade values
/// suitable for tests and single-tenant dev servers; production
/// deployments are expected to override.
///
/// Client visibility of these caps (when desired) is via JMAP Quotas
/// (`urn:ietf:params:jmap:quotas`, RFC 9425), not via this struct.
/// The `urn:ietf:params:jmap:chat` session capability does NOT advertise
/// the count caps (the previous cap-advertising fields `maxRolesPerSpace`,
/// `maxSpaceMembers`, `maxChannelsPerSpace`, `maxCategoriesPerSpace`
/// were removed from the draft in spec commit `80d5e11`).
///
/// See the workspace `AGENTS.md` "Backend caps and limits" section for
/// the cross-extension pattern this struct establishes for JMAP Chat.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChatLimits {
    /// Maximum number of roles per Space. Enforced against the
    /// resulting count after applying every `addRoles` entry in a
    /// `Space/set` update patch.
    pub max_roles_per_space: u32,
    /// Maximum number of members per Space. Enforced against the
    /// resulting count after applying every `addMembers` entry in a
    /// `Space/set` update patch.
    pub max_space_members: u32,
    /// Maximum number of channels per Space. Enforced against the
    /// resulting count after applying every `addChannels` entry in a
    /// `Space/set` update patch. The current channel count is the sum
    /// of `space.uncategorizedChannelIds.len()` and every
    /// `space.categories[].channelIds.len()`.
    pub max_channels_per_space: u32,
    /// Maximum number of categories per Space. Enforced against the
    /// resulting count after applying every `addCategories` entry in
    /// a `Space/set` update patch.
    pub max_categories_per_space: u32,
    /// Maximum number of members per group Chat. Enforced against the
    /// resulting count after applying every `addMembers` entry in a
    /// `Chat/set` update patch.
    pub max_group_chat_members: u32,
    /// `bodyType` values the account advertises as `supportedBodyTypes`
    /// and `Message/set` accepts (draft-atwood-jmap-chat-00
    /// §Account-Level Capability Object). Defaults to
    /// [`crate::message::SUPPORTED_BODY_TYPES`]. The list "MUST include
    /// `"text/plain"` unless the server accepts only end-to-end encrypted
    /// body types"; `Message/set` rejects a `bodyType` not listed here,
    /// including the implied `"text/plain"` of an omitted `bodyType`.
    pub supported_body_types: &'static [&'static str],
    /// Listed types that a companion specification reserves for
    /// server-created Messages (e.g. `"application/mimi-fanout"`,
    /// draft-atwood-jmap-chat-mimi-00). `Message/set` create rejects
    /// them with an `invalidProperties` SetError naming `bodyType`;
    /// update does not. Defaults to empty.
    pub server_reserved_body_types: &'static [&'static str],
}

impl Default for ChatLimits {
    fn default() -> Self {
        Self::new(50, 10_000, 500, 100)
    }
}

impl ChatLimits {
    /// Construct a [`ChatLimits`] with the four Space cap fields
    /// specified in declaration order: roles, members, channels,
    /// categories. [`Self::max_group_chat_members`] defaults to 1000;
    /// override it with [`Self::with_max_group_chat_members`].
    ///
    /// The struct is `#[non_exhaustive]` so external callers (tests
    /// using `MemoryBackend::set_limits_for_test`, production backends
    /// overriding [`ChatBackend::limits`]) need a constructor to build
    /// one. Future cap-field additions to [`ChatLimits`] are an
    /// additive non-breaking change because the constructor stays
    /// stable; callers wanting to override the new field combine
    /// `ChatLimits::new(..)` with [`Self::with_max_roles_per_space`]
    /// / [`Self::with_max_space_members`] /
    /// [`Self::with_max_channels_per_space`] /
    /// [`Self::with_max_categories_per_space`] and a future analogous
    /// setter.
    pub const fn new(
        max_roles_per_space: u32,
        max_space_members: u32,
        max_channels_per_space: u32,
        max_categories_per_space: u32,
    ) -> Self {
        Self {
            max_roles_per_space,
            max_space_members,
            max_channels_per_space,
            max_categories_per_space,
            max_group_chat_members: 1_000,
            supported_body_types: crate::message::SUPPORTED_BODY_TYPES,
            server_reserved_body_types: &[],
        }
    }

    /// Builder-style setter for [`Self::max_roles_per_space`].
    #[must_use]
    pub const fn with_max_roles_per_space(mut self, max: u32) -> Self {
        self.max_roles_per_space = max;
        self
    }

    /// Builder-style setter for [`Self::max_space_members`].
    #[must_use]
    pub const fn with_max_space_members(mut self, max: u32) -> Self {
        self.max_space_members = max;
        self
    }

    /// Builder-style setter for [`Self::max_channels_per_space`].
    #[must_use]
    pub const fn with_max_channels_per_space(mut self, max: u32) -> Self {
        self.max_channels_per_space = max;
        self
    }

    /// Builder-style setter for [`Self::max_categories_per_space`].
    #[must_use]
    pub const fn with_max_categories_per_space(mut self, max: u32) -> Self {
        self.max_categories_per_space = max;
        self
    }

    /// Builder-style setter for [`Self::max_group_chat_members`].
    #[must_use]
    pub const fn with_max_group_chat_members(mut self, max: u32) -> Self {
        self.max_group_chat_members = max;
        self
    }

    /// Builder-style setter for [`Self::supported_body_types`].
    #[must_use]
    pub const fn with_supported_body_types(mut self, types: &'static [&'static str]) -> Self {
        self.supported_body_types = types;
        self
    }

    /// Builder-style setter for [`Self::server_reserved_body_types`].
    #[must_use]
    pub const fn with_server_reserved_body_types(mut self, types: &'static [&'static str]) -> Self {
        self.server_reserved_body_types = types;
        self
    }
}

// ---------------------------------------------------------------------------
// Custom emoji authorization gate
// ---------------------------------------------------------------------------

/// Identifies the kind of `CustomEmoji/set` operation being authorized by
/// [`ChatBackend::may_set_custom_emoji`].
///
/// Per draft-atwood-jmap-chat-00 commit `9344aec` (2026-05-11, "refactor:
/// implementation-defined emoji authorization"), authorization for
/// `CustomEmoji/set` is fully implementation-defined for both server-global
/// and Space-scoped emoji. The kit forwards the op kind so the backend can
/// apply different policies per op (e.g. allow create+update but require
/// elevated rights for destroy).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmojiSetOp {
    /// `CustomEmoji/set` `create` — a new emoji is being added.
    Create,
    /// `CustomEmoji/set` `update` — an existing emoji's mutable fields
    /// (currently `name` and `blobId`) are being patched.
    Update,
    /// `CustomEmoji/set` `destroy` — an existing emoji is being removed.
    Destroy,
}

/// Why [`ChatBackend::join_space_with_invite`] refused a redemption.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InviteJoinRefusal {
    /// The invite, or the Space it names, no longer exists.
    NotFound,
    /// The invite's `uses` has reached `maxUses`.
    Exhausted,
    /// The owner is already a member of the Space.
    AlreadyMember,
}

// ---------------------------------------------------------------------------
// Slow-mode rate-limit gate
// ---------------------------------------------------------------------------

/// Outcome of a [`ChatBackend::slow_mode_check`] rejection.
///
/// Per draft-atwood-jmap-chat-00 §Chat `slowModeSeconds` plus spec commit
/// `de60acb` (2026-05-11) which softened the manage-channels exemption to
/// SHOULD: a non-exempt member who sends faster than the configured rate
/// MUST be rejected with a `rateLimited` SetError carrying a
/// `serverRetryAfter` UTCDate that tells the client when it may retry.
///
/// `retry_after` is a fully-formed [`jmap_types::UTCDate`] — the backend
/// has already done the arithmetic (typically "now + remaining slow-mode
/// window"). The handler serialises it verbatim onto the wire as the
/// `serverRetryAfter` SetError extra field.
///
/// `#[non_exhaustive]` preserves the ability to add fields without a
/// SemVer break (e.g. an error code, sender-facing retry hint string,
/// "this is your N-th throttle in M minutes" diagnostic counter).
/// External consumers MUST construct instances via [`SlowModeError::new`]
/// rather than field-init syntax.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct SlowModeError {
    /// When the rate-limited sender may retry.
    pub retry_after: jmap_types::UTCDate,
}

impl SlowModeError {
    /// Construct a [`SlowModeError`] from a precomputed retry-after
    /// [`UTCDate`].
    ///
    /// [`UTCDate`]: jmap_types::UTCDate
    #[must_use]
    pub const fn new(retry_after: jmap_types::UTCDate) -> Self {
        Self { retry_after }
    }
}

impl std::fmt::Display for SlowModeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "slow-mode rate limit; retry after {}",
            self.retry_after.as_ref()
        )
    }
}

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

// ---------------------------------------------------------------------------
// ChatBackend trait
// ---------------------------------------------------------------------------

/// Storage backend for JMAP Chat method handlers.
///
/// Implementors provide the actual data access; the method handler modules
/// in this crate translate between JMAP wire protocol and backend calls.
///
/// Read-side operations (`get_objects`, `get_state`, `get_changes`,
/// `query_objects`, `query_changes`) are defined on the [`JmapBackend`]
/// supertrait. Only write operations and type introspection are here.
///
/// This trait is not object-safe by design (generic methods). Use
/// `Arc<impl ChatBackend>` when sharing across tasks.
///
/// # Async vs sync method convention
///
/// `ChatBackend` carves its methods into two groups:
///
/// 1. **Mutation / hot-path hooks are async.** [`ChatBackend::create_object`],
///    [`ChatBackend::update_object`], [`ChatBackend::destroy_object`],
///    [`ChatBackend::apply_space_patch`],
///    [`ChatBackend::apply_space_metadata_patch`],
///    [`ChatBackend::expire_message`],
///    [`ChatBackend::read_and_burn_message`],
///    [`ChatBackend::slow_mode_check`],
///    [`ChatBackend::is_contact_blocked`], and
///    [`ChatBackend::may_set_custom_emoji`] all return
///    `impl Future + Send`. These methods consult per-account state,
///    may issue I/O, and run on the JMAP request hot path; the async
///    signature lets production backends `.await` storage / cache /
///    policy lookups without runtime-pinning hazards.
///
/// 2. **Policy / capability hooks are sync.** [`ChatBackend::supports_type`],
///    [`ChatBackend::generate_invite_code`], [`ChatBackend::limits`],
///    [`ChatBackend::protect_last_admin`], and
///    [`ChatBackend::retains_edit_history`] are synchronous methods.
///    These hooks answer deployment-policy or capability questions
///    whose answer SHOULD be derivable from in-process state (a
///    startup-loaded config snapshot, a cached per-account record, a
///    CSPRNG handle). The sync shape keeps the trait surface easy to
///    implement for single-process and reference backends, which are
///    the dominant use case for a kit crate.
///
/// The carve-up is intentional and is NOT a "we forgot to async these"
/// regression. Production backends that need to consult an async
/// source for any of the five sync hooks have two options: (a)
/// pre-cache the answer at startup or on a background refresh; or (b)
/// file a follow-up bead proposing the conversion for that specific
/// method, with the production constraint documented. Per-method
/// rustdoc notes ("`# Why sync, not async`" / "`# Fallibility and
/// async — known limitations`") spell out the workaround on each
/// affected method. Flipping any of the sync hooks to async is a
/// major-version breaking change for downstream implementors;
/// converting them all preemptively for hypothetical multi-tenant
/// SaaS deployments would over-engineer the kit-vs-product posture
/// described in workspace `AGENTS.md`.
pub trait ChatBackend: JmapBackend {
    /// Create a new object.
    ///
    /// Returns `(assigned_id, created_object)` on success. `create_id` is the
    /// client-side creation id used in the `/set` request.
    ///
    /// # Sentinel fields the backend MUST replace
    ///
    /// The method handlers in this crate pass partially-constructed objects
    /// with sentinel values that the backend MUST replace with real values
    /// before storing:
    ///
    /// - **`id`**: The `id` field in the input object is always set to
    ///   `"placeholder"`. The backend MUST replace it with a real, unique,
    ///   account-scoped ID and return that ID as the first element of the
    ///   result tuple.
    ///
    /// Failing to replace this sentinel will cause the client to receive an
    /// invalid wire value (`"placeholder"`) as the assigned id of every
    /// created `Space`, `Chat`, `Message`, `SpaceInvite`, `SpaceBan`,
    /// `CustomEmoji`, and `ReadPosition`.
    ///
    /// # Per-type uniqueness contracts
    ///
    /// For most types the (id) primary key is the only uniqueness
    /// invariant the backend must enforce, and the backend's normal
    /// server-assigned-id discipline closes that. A few types carry
    /// additional uniqueness invariants on non-id fields that the
    /// backend MUST enforce atomically with the create — the handler
    /// pre-checks defensively but cannot close the
    /// two-concurrent-requests race window.
    ///
    /// - **`ReadPosition`** — at most one record per
    ///   `(account_id, chat_id)`. Clients cannot create one
    ///   (`ReadPosition/set` create is `forbidden`); the server creates
    ///   it on first message delivery in
    ///   [`crate::message::handle_message_set`], which pre-checks via a
    ///   sequential scan, but two concurrent `Message/set` requests for
    ///   the same `chatId` can both pass the pre-check and then both
    ///   reach `create_object`. The
    ///   backend MUST enforce a unique constraint on
    ///   `(account_id, chat_id)` at the storage layer (typically a
    ///   composite unique index in a database backend) and surface a
    ///   duplicate as `BackendSetError::SetError(SetError::new(
    ///   SetErrorType::AlreadyExists).with_existing_id(canonical))`.
    ///   See draft-atwood-jmap-chat-00 §ReadPosition.
    /// - **`ChatContact`** — at most one record per
    ///   `(account_id, identity)`, compared octet for octet.
    ///   [`crate::contact::handle_contact_set`] does not pre-check: the
    ///   backend MUST reject a duplicate atomically with the create as
    ///   `BackendSetError::SetError(SetError::new(
    ///   SetErrorType::AlreadyExists).with_existing_id(existing))`, where
    ///   `existing` is the id of the ChatContact that holds the
    ///   `identity`. See draft-atwood-jmap-chat-00 §ChatContact/set.
    /// - **`Chat`** with `kind == Direct` — at most one direct chat
    ///   per `(account_id, contact_id)`. The handler at
    ///   [`crate::chat::handle_chat_set`] uses optimistic
    ///   create-then-validate with rollback; backends MAY enforce a
    ///   storage-level unique constraint to eliminate the rollback
    ///   path entirely, but it is not required because the
    ///   handler's rollback closes the race correctly. See
    ///   draft-atwood-jmap-chat-00 §Chat.
    ///
    /// The reference [`crate::memory::MemoryBackend`] does NOT
    /// enforce the `ReadPosition` `(account, chatId)` uniqueness
    /// constraint at the storage layer — it is single-Mutex
    /// per-call so concurrent racing cannot occur in practice, but a
    /// production backend that holds different locks per record (or
    /// no lock at all between read and write) MUST.
    ///
    /// # Errors
    ///
    /// Reject a create that RFC 8620 §5.3 defines an error for with
    /// [`BackendSetError::SetError`] carrying the matching
    /// [`SetErrorType`]; the handler places it verbatim in `notCreated`.
    /// For create these are `forbidden`, `overQuota`, `tooLarge`,
    /// `rateLimit` and `invalidProperties`. An `invalidProperties` error
    /// SHOULD name in `properties` "*all* the properties that were
    /// invalid", for example `chatId` when a Message names a Chat that does
    /// not exist.
    ///
    /// Return [`BackendSetError::Other`] only for unexpected storage
    /// failures. The handler reports it as `serverFail` without the
    /// error's `Display` text, which tells the client to retry; a
    /// deterministic rejection returned as `Other` invites retries that
    /// cannot succeed.
    ///
    /// # State and change log
    ///
    /// RFC 8620 §5.1 defines the state string over "*all* the data of
    /// this type in the account": "If the data changes, this string MUST
    /// change." Every successful `create_object`,
    /// [`update_object`](Self::update_object) and
    /// [`destroy_object`](Self::destroy_object) MUST advance
    /// [`get_state`](JmapBackend::get_state) for `O` in `account_id` and
    /// record the id as created, updated or destroyed so
    /// [`get_changes`](JmapBackend::get_changes) reports it.
    ///
    /// The write, the state advance and the change record MUST be atomic
    /// with respect to other callers: the reference `MemoryBackend` does
    /// all three under one mutex acquisition, and a database backend uses
    /// one transaction. A call that returns `Err` MUST leave the stored
    /// data, the state and the change log unchanged.
    fn create_object<O: SetObject + Send + Sync>(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
        create_id: &str,
        obj: O,
    ) -> impl std::future::Future<Output = Result<(jmap_types::Id, O), BackendSetError<Self::Error>>>
           + Send;

    /// Apply a partial update (patch) to an existing object.
    ///
    /// Returns `Some(updated_object)` if the backend modified any properties
    /// beyond what the client requested (RFC 8620 §5.3 server-set field echo),
    /// or `None` if the patch was applied verbatim.
    ///
    /// # Patch format
    ///
    /// `patch` is an RFC 8620 §5.3 PatchObject, not an RFC 7396 JSON
    /// Merge Patch: "The keys are a path in JSON Pointer format
    /// \[RFC6901\], with an implicit leading "/"". So `{"a/b": null}`
    /// removes member `b` of property `a`; it does not touch a top-level
    /// property literally named `a/b`. A `null` value sets the
    /// property's default if it has one, otherwise removes it. A pointer
    /// into an array, a pointer whose parent does not exist, or two keys
    /// where one pointer is a prefix of the other "MUST be rejected with
    /// an "invalidPatch" error".
    ///
    /// Implementors SHOULD apply `patch` with
    /// [`jmap_server::apply_patch_object`], which enforces these rules
    /// all-or-nothing, and map any [`jmap_server::PatchPathError`] to
    /// [`jmap_server::SetErrorType::InvalidPatch`], as the reference
    /// `MemoryBackend` does.
    ///
    /// # Errors
    ///
    /// As for [`create_object`](Self::create_object), plus these RFC 8620
    /// §5.3 rules for update:
    ///
    /// - "If an id given cannot be found, the update or destroy MUST be
    ///   rejected with a "notFound" set error."
    /// - A patch that breaks the rules under "Patch format" is
    ///   `invalidPatch`.
    /// - A patched record that is invalid is `invalidProperties`. That
    ///   includes changing a server-set property, though "it is not an
    ///   error to include a server-set property in an update as long as
    ///   the value is identical to the current value on the server."
    ///
    /// # Atomicity
    ///
    /// RFC 8620 §5.3: the server "MUST NOT only commit part of an update
    /// to a single record". Apply the whole patch and validate the result
    /// before writing it, so that on any error the stored record is
    /// unchanged. State and change log follow
    /// [`create_object`](Self::create_object) ("State and change log"); an
    /// `Ok(None)` return still advances the state.
    fn update_object<O: SetObject + Send + Sync>(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
        id: &jmap_types::Id,
        patch: O::Patch,
    ) -> impl std::future::Future<Output = Result<Option<O>, BackendSetError<Self::Error>>> + Send;

    /// Destroy an existing object by id.
    ///
    /// # Errors
    ///
    /// As for [`create_object`](Self::create_object), plus these RFC 8620
    /// §5.3 rules for destroy: an unknown id, including one already
    /// destroyed, is `notFound`, and a destroy that "would violate an ACL
    /// or other permissions policy" is `forbidden`.
    ///
    /// # Cascades
    ///
    /// `destroy_object::<Message>` MUST also remove the id from its
    /// Chat's `pinnedMessageIds` (draft-atwood-jmap-chat-00 §Chat:
    /// "When a pinned Message is hard-deleted, the server MUST remove
    /// its id from this list."), as
    /// [`expire_message`](Self::expire_message) does. That change is
    /// a Chat update.
    ///
    /// `destroy_object::<Space>` MUST also destroy every channel Chat
    /// whose `spaceId` is the Space and every Message in those Chats
    /// (draft-atwood-jmap-chat-00 §Destroying a Space: "Cascades to all
    /// channel Chats and their Messages. Hard-deletes all records; no
    /// tombstones are retained."). Those are Chat and Message destroys.
    ///
    /// If the backend itself changes another record because of the
    /// destroy, it MUST advance that record type's state and record the
    /// change in the same atomic step, so `/changes` on that type sees it.
    /// State and change log for `O` follow
    /// [`create_object`](Self::create_object) ("State and change log").
    fn destroy_object<O: SetObject + Send + Sync>(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
        id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<(), BackendSetError<Self::Error>>> + Send;

    /// Returns true if this backend implements the given JMAP object type.
    ///
    /// # Contract
    ///
    /// This is a **global, stateless backend-capability check** — it asks
    /// "did this implementation wire up the methods needed for type
    /// `O`?", not "does this user's account have this type enabled?".
    /// That is why the method is synchronous and takes no `caller` or
    /// `account_id` arguments. Per-account capability variation belongs
    /// in the consumer's session-capability builder (workspace AGENTS.md
    /// library-kit posture), NOT here.
    ///
    /// # Types in scope
    ///
    /// Implementors should answer for the object types this trait
    /// covers (draft-atwood-jmap-chat-00): `Chat`, `Message`, `Space`,
    /// `SpaceInvite`, `SpaceBan`, `ChatContact`, `CustomEmoji`,
    /// `ReadPosition`, and `PresenceStatus`. Backends MAY answer for
    /// object types from sibling extensions if they also implement those
    /// traits. A backend that does not recognise `O` SHOULD return
    /// `false`.
    ///
    /// # Callers
    ///
    /// The handler library in this crate does not call this method. The
    /// server consumer calls it when building the JMAP session capability
    /// response.
    ///
    /// # Per-account semantics
    ///
    /// Capabilities are per account (RFC 8620 §1.6.2, §2
    /// `accountCapabilities`), so the answer may vary with `caller` and
    /// `account_id`. Backends that support every type in this trait for
    /// every account can ignore both arguments and return `true`.
    ///
    /// # Why sync, not async
    ///
    /// Type-support is a deployment capability question answered from
    /// in-process state. A backend whose supported types come from
    /// configuration or a per-account record SHOULD load it ahead of the
    /// request and answer from the in-memory snapshot. See the
    /// trait-level "Async vs sync method convention" section above for
    /// the workspace rationale.
    fn supports_type<O: JmapObject>(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
    ) -> bool;

    /// Generate a cryptographically random invite code.
    ///
    /// Implementations MUST use a CSPRNG seeded from OS entropy. The
    /// recommended choices are [`rand::rngs::OsRng`] or the [`getrandom`]
    /// crate directly. Do NOT use `rand::thread_rng()` for security-relevant
    /// output: although current `rand` versions document `ThreadRng` as
    /// cryptographically secure, its underlying algorithm is
    /// implementation-defined and has changed across releases, and the
    /// `rand` book explicitly routes security-sensitive callers to `OsRng`.
    ///
    /// The returned string must be unguessable — do NOT use timestamps,
    /// sequential counters, or non-CSPRNG sources.
    ///
    /// # Output shape contract
    ///
    /// Implementations MUST satisfy ALL of the following for the kit's
    /// constant-time-compare guarantee (see below) to hold:
    ///
    /// 1. **Fixed length across calls.** Every invocation of
    ///    `generate_invite_code` on a single backend MUST return a
    ///    string of the same byte length. A backend that returns
    ///    variable-length codes leaks a length oracle through the
    ///    constant-time-compare timing channel: `ct_eq` returns
    ///    `Choice(0)` cheaply on length mismatch, so an attacker can
    ///    learn whether their candidate code matches the stored
    ///    length even when the content compare is constant-time.
    /// 2. **Minimum 128 bits of CSPRNG entropy.** The reference
    ///    `MemoryBackend` impl in this crate (in `memory.rs`) emits
    ///    32 lowercase-hex characters (16 bytes = 128 bits) which is
    ///    the workspace floor. Production backends MAY use more; less
    ///    is forbidden. The trait method itself is abstract — no
    ///    default impl — so backends MUST provide one.
    /// 3. **ASCII-safe encoding.** The returned bytes MUST be 7-bit
    ///    ASCII printable, byte-length-equal-to-char-length, and
    ///    safe to use as-is in JMAP wire format and HTTP-style
    ///    URL paths (no `/`, `+`, `=`, or whitespace). Recommended
    ///    encodings: lowercase hex (the default), base32 unpadded,
    ///    base64url unpadded. Plain base64 with `/` is forbidden
    ///    because downstream URL-encoded redemption flows misroute
    ///    the slash. Non-ASCII output is forbidden because
    ///    helpers that take byte-prefix slices (e.g. the
    ///    `iso8601_before` partial-comparison family) panic on
    ///    multi-byte UTF-8 boundaries.
    /// 4. **Sufficient lifespan.** Codes MUST remain
    ///    discriminable from each other across the backend's
    ///    retention window. With 128 bits of entropy and
    ///    well-mixed CSPRNG output, collision probability is
    ///    cryptographically negligible up to 2^64 active codes.
    ///
    /// A backend that cannot meet any of points 1–3 MUST NOT
    /// expose `Space/join`'s invite-code redemption path, or MUST
    /// build its own redemption with a different constant-time
    /// argument. The kit's `handle_space_join` assumes the contract
    /// without re-validating each code's shape.
    ///
    /// # Constant-time comparison contract
    ///
    /// Consumers of the returned code (notably `Space/join` invite-code
    /// lookup) MUST compare it against attacker-supplied values in
    /// constant time using `subtle::ConstantTimeEq::ct_eq` or equivalent.
    /// The reference handler in `space::handle_space_join` already does
    /// this; backends that build their own invite-redemption paths must
    /// preserve the invariant. A plain `String == String` short-circuits
    /// at the first mismatched byte and exposes a byte-by-byte timing
    /// oracle for credential recovery.
    ///
    /// # Fallibility and async — known limitations
    ///
    /// This method is sync and infallible. Backends that mint codes
    /// via a remote source (HSM, KMS, hardened sandbox where
    /// `getrandom` is gated, etc.) cannot surface failure from this
    /// signature and cannot `.await` a network call. Such backends
    /// must pre-fetch a buffer of codes at startup and serve from it,
    /// or panic on entropy starvation. A future revision MAY change
    /// the signature to `async fn generate_invite_code(&self) ->
    /// Result<String, Self::Error>` to support remote / fallible
    /// entropy sources without forcing the pre-fetch workaround.
    ///
    /// [`rand::rngs::OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html
    /// [`getrandom`]: https://docs.rs/getrandom
    fn generate_invite_code(&self) -> String;

    /// Redeem the SpaceInvite `invite_id` for `Space/join`: add the owner
    /// (member entry `account_id`, no roles, `joinedAt` = `joined_at`) to
    /// the invite's Space and increment the invite's `uses`.
    ///
    /// Per draft-atwood-jmap-chat-00 §Space/join: "The `uses` increment
    /// and membership insertion MUST be performed within a single atomic
    /// operation so that concurrent redemptions cannot exceed `maxUses`."
    ///
    /// # Atomicity contract
    ///
    /// The `uses < maxUses` check, the already-member check, the member
    /// append and the `uses` increment MUST all take effect or none, and
    /// MUST be serialized against concurrent redemptions of the invite
    /// and concurrent membership writes to the Space (a transaction or
    /// compare-and-swap). Composing this from `update_object` calls loses
    /// that guarantee: a failed second write leaves a member admitted
    /// without a use counted, and concurrent joins both pass `maxUses`.
    ///
    /// # Account scope
    ///
    /// `account_id` is the request's `accountId`, which names the
    /// account hosting the Space; the handler resolves the invite code
    /// only within that account, and `invite_id` and its Space live
    /// there. Implementations need no server-wide invite-code index.
    ///
    /// # When the handler calls this
    ///
    /// Once, after the invite resolved by code and is unexpired, and
    /// after the ban and `closedAt` checks. The handler maps each
    /// [`InviteJoinRefusal`] to the draft's `invalidArguments` method
    /// error. There is no default: a non-atomic default would reinstate
    /// the race.
    fn join_space_with_invite(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
        invite_id: &jmap_types::Id,
        joined_at: &jmap_types::UTCDate,
    ) -> impl std::future::Future<Output = Result<Result<(), InviteJoinRefusal>, Self::Error>> + Send;

    /// Apply a `readAt`-setting `patch` to the `burnOnRead` Message
    /// `message_id` and hard-delete it, as one atomic operation.
    ///
    /// Per draft-atwood-jmap-chat-00 §Message `burnOnRead`: "the
    /// receiving server MUST permanently hard-delete this message (row
    /// removal, not tombstone) immediately after setting `readAt`."
    ///
    /// # Atomicity contract
    ///
    /// Validate `patch` against the stored Message exactly as
    /// [`Self::update_object`] would, then remove the row, record the id
    /// in the `destroyed` set of the `Message` change log (bumping the
    /// `Message` state once), and unpin it from its Chat (§Chat
    /// `pinnedMessageIds`). All of it takes effect or none: on any error
    /// nothing is committed, so a retried `readAt` patch burns again.
    /// Composing this from `update_object` and [`Self::expire_message`]
    /// loses that guarantee: a failed delete leaves `readAt` stored, and
    /// a retry finds nothing left to set.
    ///
    /// # When the handler calls this
    ///
    /// `Message/set` update calls this instead of `update_object` when
    /// the patch sets a non-null `readAt` and the stored Message has
    /// `burnOnRead: true`. Return `SetError` `notFound` if the Message
    /// does not exist and the `update_object` `SetError` for an invalid
    /// patch. There is no default: a non-atomic default would reinstate
    /// the gap.
    fn read_and_burn_message(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
        message_id: &jmap_types::Id,
        patch: jmap_types::PatchObject,
    ) -> impl std::future::Future<Output = Result<(), BackendSetError<Self::Error>>> + Send;

    /// Implementation-defined per-Space content limits for this caller
    /// and account.
    ///
    /// Called by `handle_space_set` once per request, before
    /// dispatching to [`Self::apply_space_patch`], to enforce
    /// per-aggregate caps on roles, members, channels, and categories
    /// per Space (draft-atwood-jmap-chat-00 §Space/set, spec commit
    /// `80d5e11`), and by `handle_message_set` once per request to
    /// validate `bodyType` against [`ChatLimits::supported_body_types`]
    /// and [`ChatLimits::server_reserved_body_types`].
    ///
    /// The default implementation returns [`ChatLimits::default`],
    /// which carries conservative reference-impl-grade values. The
    /// `caller` and `account_id` arguments are plumbed even though the
    /// default impl ignores them, so production backends can vary caps
    /// per-account (Free vs. Pro tier, multi-tenant SaaS, etc.)
    /// without a future API break. Implementations SHOULD return
    /// quickly (in-process struct construction, possibly off a cached
    /// per-account record) — this method is called on the hot
    /// `Space/set` path.
    ///
    /// # Why sync, not async
    ///
    /// `Space/set` calls this once per request before dispatching the
    /// mutation; an async `.await` here would add a roundtrip to the
    /// hot path of every Space mutation. Production backends with
    /// per-tenant caps SHOULD cache the resolved [`ChatLimits`] for
    /// each account on first use and serve from the cache thereafter.
    /// See the trait-level "Async vs sync method convention" section
    /// above for the workspace rationale.
    ///
    /// Workspace cross-extension pattern: see `AGENTS.md` "Backend
    /// caps and limits".
    fn limits(&self, _caller: &Self::CallerCtx, _account_id: &jmap_types::Id) -> ChatLimits {
        ChatLimits::default()
    }

    /// Apply a sequence of structural mutations to a Space
    /// (draft-atwood-jmap-chat-00 §Space/set).
    ///
    /// `Space/set` `update` operations use semantic mutation keys
    /// (`addRoles`, `removeRoles`, `addMembers`, …) rather than RFC 8620
    /// JSON Pointer patches. The handler in `space::handle_space_set`
    /// parses the wire object, unfolds each array entry into a
    /// [`SpacePatchOp`] value, then calls this method with the resulting
    /// ordered `Vec`.
    ///
    /// # Ordering and atomicity
    ///
    /// Implementations SHOULD apply ops in input order and SHOULD provide
    /// best-effort transactional semantics so that a partial failure does
    /// not leave the Space in a half-updated state. The reference
    /// in-memory implementation locks the entire backend for the duration
    /// of the call. A database-backed implementation should wrap the
    /// sequence in a single transaction.
    ///
    /// # Permission and limit checks
    ///
    /// Permission gates (`manage_space`, `manage_roles`,
    /// `manage_members`, `manage_channels`) are backend-canonical per
    /// workspace AGENTS.md "Caller identity (foundation seam)": the
    /// reference handler does NOT apply gates before calling this
    /// method, and the backend is responsible for rejecting any op the
    /// caller is not authorized to perform.
    ///
    /// Per-aggregate count limits on roles, members, channels, and
    /// categories per Space are **backend-canonical** as of
    /// bd:JMAP-x2gd.44: the backend MUST query its own
    /// [`ChatBackend::limits`], project the post-patch counts for the
    /// target Space, and reject the whole patch with an `overQuota`
    /// SetError (RFC 8620 §5.3) if any aggregate would exceed its
    /// cap. The cap enforcement MUST be atomic with the mutation
    /// itself — typically inside the same database transaction or
    /// the same mutex critical section. Per
    /// draft-atwood-jmap-chat-00 §Space/set (spec commit `80d5e11`,
    /// 2026-05-11), this behavior is normative.
    ///
    /// The handler at [`crate::space::handle_space_set`] additionally
    /// runs a defense-in-depth pre-flight cap check before this
    /// method, but the pre-flight is non-load-bearing: a backend
    /// caller that bypasses the JMAP wire layer (admin tool,
    /// federation receiver, internal batch importer) and calls
    /// `apply_space_patch` directly MUST still see caps enforced.
    /// The pre-flight exists only to surface `overQuota` to the
    /// client as a single SetError without consuming a backend
    /// round-trip; backends MAY rely on the handler having pre-checked
    /// for performance, but MUST NOT rely on it for correctness.
    ///
    /// The role-position hierarchy check (members may only add or modify
    /// roles whose `position` is strictly less than their own
    /// highest-position role — draft §Space/set lines 1096, 1102) MUST
    /// be enforced by the backend because it is atomic with the
    /// mutation and depends on the current Space state.
    ///
    /// # Cross-type cascade contract
    ///
    /// Several `SpacePatchOp` variants have side effects on other JMAP
    /// types in the chat extension (`Chat`, `Message`). The wire-format
    /// `Space/set` response describes only the Space-side change, but
    /// `/changes` subscribers on the cascaded types depend on the
    /// backend bumping the relevant type state tokens. A backend that
    /// performs the cross-type mutations but forgets the state bumps
    /// silently desynchronises every multi-client subscriber — the
    /// failure is invisible to single-tenant smoke tests.
    ///
    /// The required cascades are:
    ///
    /// * `RemoveChannel` — MUST destroy the channel-kind `Chat` record
    ///   and every `Message` whose `chatId` matches the removed
    ///   channel (draft §Space/set line 1117: "Cascades to all
    ///   Messages in those channels."). The `Chat` and `Message` type
    ///   state tokens MUST both bump so `Chat/changes` and
    ///   `Message/changes` subscribers see the destruction.
    ///
    /// * `RemoveCategory` — MUST clear `Chat.categoryId` on every
    ///   channel-kind `Chat` whose `categoryId` named the removed
    ///   category (the channels fall back to uncategorized). The
    ///   `Chat` type state token MUST bump and the affected channel
    ///   ids MUST appear in the next `Chat/changes` entry's `updated`
    ///   list.
    ///
    /// * `AddCategory` / `UpdateCategory` with a non-empty `channelIds`
    ///   array — MUST set `Chat.categoryId` on each named channel
    ///   (relocating it from its previous category or from
    ///   uncategorized). The `Chat` type state token MUST bump and
    ///   the relocated channel ids MUST appear in `Chat/changes`
    ///   `updated`. This pins a regression class where channel
    ///   relocations silently bypass the `Chat/changes` log; a
    ///   backend that mutates `Chat.categoryId` without bumping the
    ///   `Chat` state token desynchronises every multi-client
    ///   subscriber.
    ///
    /// * `RemoveRole` — MUST strip the removed role id from
    ///   `roleIds` on every `SpaceMember` of this Space (draft
    ///   §Space/set line 1099). Members embed directly in `Space`,
    ///   so the cascade is captured by the existing `Space` state
    ///   bump; no separate `Member/changes` rotation is required.
    ///
    /// * `RemoveMember` — MUST remove the member entry and keep
    ///   `Space.memberCount` consistent with `members.len()`. The
    ///   spec does not mandate cascade to the member's `ReadPosition`
    ///   records; the reference impl leaves them in place and treats
    ///   ReadPosition cleanup as implementation-defined. A backend
    ///   that destroys orphaned ReadPositions MUST bump the
    ///   `ReadPosition` type state token; a backend that retains
    ///   them MUST NOT.
    ///
    /// State-token bumps SHOULD be batched per call: one bump per
    /// affected type per `apply_space_patch` invocation, not one bump
    /// per op. The reference impl combines every channel created,
    /// updated, and destroyed by a patch into a single `Chat/changes`
    /// entry; doing the same in production keeps `/changes`
    /// rotation rates proportional to client-visible mutations rather
    /// than to internal op count.
    ///
    /// # Server-set field invariants
    ///
    /// Server-set fields on the target Space (`memberCount`,
    /// `updatedAt`, and any future analogous cached aggregates) MUST
    /// be kept consistent with the post-mutation state. Specifically,
    /// after any combination of `AddMember` and `RemoveMember` ops,
    /// `Space.memberCount` MUST equal `Space.members.len()` as
    /// observed by the next `Space/get`. Backends MAY store these as
    /// cached values that they recompute on every mutation, or MAY
    /// compute them lazily at read time; either is acceptable as
    /// long as `Space/get` returns values consistent with the
    /// returned `members` array. The natural-minimal "persist
    /// memberCount once and trust it" implementation is **not**
    /// correct: subsequent mutations to `members` would let
    /// `memberCount` drift silently.
    ///
    /// `Space.closedAt` is server-set the same way: when the caller
    /// removes themselves as the last member holding `manage_space`
    /// and other members remain, the backend MUST set `closedAt` in
    /// the same mutation (draft §Space/set `removeMembers`). The
    /// handler rejects every other update to a closed Space.
    ///
    /// # Per-op error semantics
    ///
    /// Backend rejections that originate from a single op carry both
    /// a typed [`SetErrorType`] and a human-readable `description`
    /// string. The kit's integration tests pin specific
    /// (`SetErrorType`, `description`-substring) pairs for the
    /// common failure modes; a backend that returns a different
    /// variant or an empty description will fail those tests when
    /// plugged in.
    ///
    /// The following table records the contract for the
    /// failure modes the kit exercises today:
    ///
    /// | Op + failure mode | `SetErrorType` | `description` MUST contain |
    /// |---|---|---|
    /// | Any op, caller lacks the spec-mandated permission | `Forbidden` | the permission identifier as a substring (`"manage_space"`, `"manage_channels"`, `"manage_members"`, `"manage_roles"`, or a deployment-specific permission name) |
    /// | `AddRole` / `UpdateRole` at or above caller's highest role `position` | `Forbidden` | `"hierarchy"` or `"position"` |
    /// | Any op or combination of ops whose post-patch projection leaves the Space with zero `manage_members`-holding members (when [`Self::protect_last_admin`] returns `true`), other than self-removal | `InvalidProperties` (`properties: ["removeMembers"]`) | `"last-admin"` or `"manage_members"` |
    /// | `AddMember` with `userId` already a member | `InvalidProperties` (`properties: ["userId"]`) | implementation-defined; the reference impl names the duplicate userId |
    /// | `AddMember` with a `roleId` that does not name a role on this Space | `InvalidProperties` (`properties: ["roleIds"]`) | implementation-defined; the reference impl names the unknown roleId |
    /// | `RemoveRole` / `RemoveChannel` / `RemoveCategory` with an id that does not exist on this Space | `NotFound` | implementation-defined; the reference impl names the missing id |
    ///
    /// The "MUST contain ... as a substring" rule on permission-denied
    /// failures lets downstream UI / CLI consumers route
    /// permission-request flows by parsing the description, and lets
    /// users see "Permission denied: missing manage_space" rather
    /// than a bare "Permission denied". Deployments that need a
    /// programmatic alternative SHOULD additionally surface the
    /// missing permission via [`SetError::with_extra`] under a
    /// deployment-specific key — substring matching is the floor,
    /// not the ceiling.
    ///
    /// Note on routing: per-op rejections normally surface inside the
    /// returned [`OpResult`] vector, NOT as a top-level
    /// [`BackendSetError`] return. The handler at
    /// [`crate::space::handle_space_set`] applies the kit's per-
    /// update-target atomicity: any failing op in the patch fails the
    /// whole `update` target, so the test fixtures see the first-failing
    /// op's `(SetErrorType, description)` surface on
    /// `notUpdated[<spaceId>]`. The table above describes the
    /// per-op outcome content; the handler does not synthesise new
    /// variants or rewrite descriptions.
    ///
    /// # Return value
    ///
    /// On success, returns a `Vec<OpResult>` of the same length as `ops`,
    /// in input order. Each entry reports the outcome of one op (id
    /// assignment for `Add*` variants, error for rejections). The
    /// handler maps per-op errors back into the `/set` response shape
    /// per [`OpResult`]'s documentation.
    ///
    /// Returns [`BackendSetError::Other`] only for backend-level failures
    /// (the storage layer is unreachable, the account does not exist,
    /// `space_id` is unknown, etc.) — i.e. failures that prevent any op
    /// from being attempted. Per-op rejections (permission denied,
    /// invalid id, role hierarchy violation, etc.) go in the `outcome`
    /// field of the returned [`OpResult`] vector, not in an error return.
    fn apply_space_patch(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
        space_id: &jmap_types::Id,
        ops: Vec<SpacePatchOp>,
    ) -> impl std::future::Future<Output = Result<Vec<OpResult>, BackendSetError<Self::Error>>> + Send;

    /// Apply a JSON Merge Patch to a Space's top-level metadata fields
    /// (draft-atwood-jmap-chat-00 §Space/set, `name`, `description`,
    /// `iconBlobId`, `isPublic`, `isPubliclyPreviewable`).
    ///
    /// `Space/set` `update` operations carrying *only* top-level
    /// metadata fields (and not the semantic-mutation keys
    /// `addRoles` / `addMembers` / `addChannels` / `addCategories`
    /// etc.) route through this method instead of the generic
    /// [`Self::update_object`]. Mixed patches carrying both top-level
    /// metadata AND semantic-mutation keys call
    /// [`Self::apply_space_patch`] first (for the structural ops) and
    /// this method second (for the metadata). The handler at
    /// `space::handle_space_set` is responsible for the split and for
    /// stripping non-metadata keys before calling this method.
    ///
    /// # Permission and atomicity
    ///
    /// Per draft-atwood-jmap-chat-00, every top-level metadata field
    /// carries the marker "Mutable by members with `manage_space`
    /// permission". Implementations MUST gate the mutation on the
    /// caller's effective permissions in the target Space: verify the
    /// caller holds `manage_space` (resolved through the Space's role
    /// hierarchy) and reject the patch with
    /// [`SetErrorType::Forbidden`] if not. The caller is the owner, whose
    /// `members` entry is the `account_id` (draft §Terminology), so look
    /// the caller up by `account_id`, not by the principal, whether or
    /// not [`JmapBackend::principal_id`] returns `Some`; the same holds
    /// for every caller-membership or permission check in this trait.
    ///
    /// The check fires inside this method atomically with the
    /// mutation: snapshot the Space's role/member state, evaluate
    /// the permission gate, and apply the merge patch in one
    /// critical section. Backend canonical per workspace AGENTS.md.
    ///
    /// # `patch` shape
    ///
    /// The handler decodes the wire patch's metadata-key subset
    /// (`name`, `description`, `iconBlobId`, `isPublic`,
    /// `isPubliclyPreviewable`) into a [`SpaceMetadataPatch`],
    /// stripping structural mutation keys (`addRoles` etc.) before
    /// building it. Each field is `Option<_>`; nullable target fields
    /// use `Option<Clearable<T>>` so the backend can distinguish
    /// "wire null" (clear) from "absent" (unchanged). Backends MAY
    /// validate the values further (e.g. reject an `icon_blob_id`
    /// referencing a non-existent blob) and surface those as
    /// [`SetError`] returns. The expected `SetErrorType` for
    /// value-level validation failures is
    /// [`SetErrorType::InvalidProperties`] with the offending field
    /// name in `properties` — see the "Diagnostic content
    /// convention" section below for the blob-not-found shape
    /// specifically. (RFC 8620 §5.3 defines `blobNotFound` only as
    /// a method-level error; at the SetError level it surfaces as
    /// `InvalidProperties` naming the offending blob-id field.)
    ///
    /// # Return value
    ///
    /// The discriminator between `Ok(Some(space))` and `Ok(None)` is
    /// the **wire-visible Space shape**:
    ///
    /// - Return `Ok(None)` if the wire-visible `Space` (the shape
    ///   `Space/get` would serialise after applying this patch) is
    ///   identical to the result of mechanically applying the patch
    ///   keys to the prior state with no additional server-side
    ///   modification. The handler can then build the `/set` `updated`
    ///   entry from the input patch alone with no extra fetch.
    /// - Return `Ok(Some(space))` if the backend modified any
    ///   wire-visible property beyond what the client requested
    ///   (RFC 8620 §5.3 server-set field echo, e.g. a normalised
    ///   `name`, a derived `iconBlobId`, a server-bumped
    ///   `memberCount`, or any spec-defined server-set field that
    ///   `Space/get` would return). The handler echoes the returned
    ///   `Space` to the client.
    ///
    /// Server-internal bookkeeping that is NOT part of the
    /// wire-visible `Space` shape (e.g. private last-touched
    /// timestamps used only for `/changes` cache-keying, internal
    /// sequence numbers) MUST NOT itself drive the `Some` path —
    /// the discriminator is "would `Space/get` return a different
    /// shape?", not "did the backend touch anything internally?".
    /// A backend that bumps an internal timestamp on every metadata
    /// patch correctly returns `Ok(None)` when the wire shape is
    /// otherwise unchanged.
    ///
    /// `Err(BackendSetError::SetError(e))` with `e.kind ==
    /// SetErrorType::Forbidden` when the caller fails the
    /// `manage_space` gate.
    ///
    /// # Diagnostic content convention
    ///
    /// The `SetError.description` on a `manage_space`-gate rejection
    /// MUST contain `"manage_space"` as a substring. The kit's
    /// integration tests assert this (e.g.
    /// `tests/space_metadata_apply.rs`); a backend that returns a
    /// bare `SetError::new(SetErrorType::Forbidden)` with no
    /// `description` will fail those tests. Build the error via
    /// `SetError::new(SetErrorType::Forbidden).with_description(
    /// "manage_space permission required")` (or any other string
    /// that contains the literal `"manage_space"`).
    ///
    /// Future blob-validation rejections (e.g. `iconBlobId`
    /// referencing a non-existent blob, see the `patch` shape
    /// section above) SHOULD surface as
    /// `SetErrorType::InvalidProperties` with
    /// `properties: ["iconBlobId"]` and a description that names
    /// the offending blob id. The reference impl does not validate
    /// blobs today; production backends with a blob store SHOULD.
    ///
    /// Rationale for the separate metadata patch method: routing
    /// top-level metadata through the generic `update_object::<Space>`
    /// would bypass the permission gate, since the generic path is
    /// not permission-aware. This method exists so that top-level
    /// `Space/set` `update` requests carrying only metadata fields
    /// (`name` / `description` / `iconBlobId` / `isPublic` /
    /// `isPubliclyPreviewable`) route through a `manage_space`-gated
    /// path instead.
    fn apply_space_metadata_patch(
        &self,
        caller: &Self::CallerCtx,
        account_id: &jmap_types::Id,
        space_id: &jmap_types::Id,
        patch: jmap_chat_types::SpaceMetadataPatch,
    ) -> impl std::future::Future<
        Output = Result<Option<jmap_chat_types::Space>, BackendSetError<Self::Error>>,
    > + Send;

    /// Whether the backend should reject any [`Self::apply_space_patch`]
    /// that would leave the target Space with zero members holding
    /// either `manage_members` or `manage_space` permission.
    ///
    /// # Invariant (by outcome, not by op type)
    ///
    /// The protection is invariant on the *post-patch* state of the
    /// (member, role, permission) graph, not on any single op variant.
    /// A backend that returns `true` from this method MUST reject every
    /// patch whose post-application projection has zero members holding
    /// `manage_members` or `manage_space`, including (non-exhaustively)
    /// the cases below. Self-removal (a `removeMembers` holding only the
    /// accountId, which references the owner per draft §Terminology) is
    /// exempt: draft §Space/set says
    /// "Last-administrator protection does not apply to self-removal."
    ///
    ///
    /// - `RemoveMember` that removes the last admin member.
    /// - `UpdateMember` that strips a role granting `manage_members` or
    ///   `manage_space` from a member who holds no other such role.
    /// - `UpdateRole` that removes `manage_members` or `manage_space`
    ///   permission from a role currently held by the last admin.
    /// - `RemoveRole` that drops a permission-granting role from the
    ///   graph when no other role grants the same permission to a
    ///   remaining member.
    /// - Any combination of the above within a single `ops` vector
    ///   whose cumulative effect is zero admins.
    ///
    /// Production backends should project the post-patch graph and
    /// reject when the admin count would reach zero, regardless of
    /// which op caused the transition.
    ///
    /// # Why this exists
    ///
    /// The 2026-05-12 design reversal dropped the `Space.ownerId` field:
    /// "who controls a Space" is now fully implementation-defined /
    /// out-of-band per draft-atwood-jmap-chat-00.
    /// Without a normative owner identity, the kit cannot enforce the
    /// previous "owner cannot be removed" rule. Instead the kit exposes
    /// this purely permission-graph-based knob: production backends that
    /// want to prevent the "no admin left" failure mode return `true`
    /// (the trait default); deployments with their own designated
    /// controller principal or external admin-tracking system can opt
    /// out by returning `false`.
    ///
    /// # Default implementation
    ///
    /// Returns `true` — production-safe by default. A third-party
    /// backend that does not override this method gets last-admin
    /// protection automatically.
    ///
    /// # Why the reference `MemoryBackend` overrides this to `false`
    ///
    /// The reference impl flips the default to keep existing
    /// integration tests (which do not seed admin memberships) passing
    /// unchanged. Tests that exercise the protection path opt in via
    /// `MemoryBackend::set_protect_last_admin_for_test(true)`.
    ///
    /// The reference impl's projection is intentionally narrow: it
    /// covers `RemoveMember` only, and does NOT model `UpdateMember`
    /// role-strip, `UpdateRole` permission-strip, or `RemoveRole`
    /// paths to zero-admin state. Production backends with the full
    /// invariant requirement MUST extend the projection.
    ///
    /// # Atomicity
    ///
    /// The check fires inside [`Self::apply_space_patch`] atomically
    /// with the candidate mutation: the backend snapshots the Space's
    /// member/role state, projects the post-patch admin count across
    /// all ops in the patch (member adds/removes, role-id changes,
    /// permission edits), and rejects the whole update target with a
    /// `Forbidden` SetError if zero admins would remain.
    ///
    /// # Why sync, not async
    ///
    /// This predicate is a deployment-policy flag: a backend either
    /// enforces the last-admin invariant or it does not. Backends
    /// that consult an external admin-tracking service to derive the
    /// answer SHOULD cache the per-account boolean and answer from
    /// the cache. See the trait-level "Async vs sync method
    /// convention" section above for the workspace rationale.
    fn protect_last_admin(&self, caller: &Self::CallerCtx, account_id: &jmap_types::Id) -> bool {
        let _ = (caller, account_id);
        true
    }

    /// Whether the backend retains edit history for messages.
    ///
    /// Per draft-atwood-jmap-chat-00 commit `0783fc4` ("condition
    /// MessageRevision push on edit-history retention") +
    /// `§Message editHistory`: servers MAY limit retained
    /// MessageRevisions, and when the server does not retain edit
    /// history, the `editHistory` field MUST be omitted from
    /// `Message/get` and (in principle) `Message/changes` responses.
    /// `Message/changes` per RFC 8620 §5.2 returns only id arrays,
    /// not Message objects, so the spec's "omit from /changes" is
    /// a no-op at the wire level — the kit only enforces the
    /// `Message/get` projection.
    ///
    /// # When the handler calls this
    ///
    /// `handle_message_get` consults this predicate once per call.
    /// When `false`, every returned `Message` has its
    /// `edit_history` field set to `None` before serialization,
    /// which serde's `skip_serializing_if = "Option::is_none"`
    /// collapses to a wire-absent field per spec.
    ///
    /// # Why sync, not async
    ///
    /// This is a static deployment-policy flag, not a per-call
    /// decision — it does not consult per-account state, principal
    /// identity, or per-message bounds. Production backends that
    /// need to vary retention per-account would file a follow-up
    /// bead for a parallel `retains_edit_history_for_account` hook
    /// rather than reshaping this method.
    ///
    /// # Default implementation
    ///
    /// Returns `false` — no retention. This matches the workspace's
    /// "kit defines the hook; consumer enforces the policy" posture.
    /// Production backends that retain edit history MUST override
    /// this to `true`; the reference `MemoryBackend` exposes a
    /// test-only setter for tests that need the retain-on path.
    fn retains_edit_history(&self) -> bool {
        false
    }

    /// Predicate consulted by typing/presence fan-out paths: is the
    /// caller (the sender of the ephemeral event) blocked by the
    /// recipient who owns `account_id`'s contact list?
    ///
    /// Per draft-atwood-jmap-chat-00 commit `d68b4e3` (2026-05-11,
    /// "close blocked-sender suppression gaps for typing/presence"):
    /// when the sender corresponds to a [`ChatContact`] whose
    /// `blocked` is `true` on the recipient's contact list, the
    /// server MUST silently suppress the ephemeral event for that
    /// recipient. The sender is NOT informed.
    ///
    /// [`ChatContact`]: jmap_chat_types::ChatContact
    ///
    /// # Argument semantics
    ///
    /// - `caller`: identity of the sender. Backends with identity
    ///   wired resolve this via
    ///   [`JmapBackend::principal_id`];
    ///   the resolved principal is matched against the
    ///   [`ChatContact`]'s implementation-defined principal binding
    ///   to determine which `ChatContact` (if any) in the recipient's
    ///   list represents the sender.
    /// - `account_id`: the **recipient's** account, i.e. the account
    ///   that owns the [`ChatContact`] list to consult. NOT the
    ///   sender's account.
    /// - `contact_id`: the candidate [`ChatContact`] id within the
    ///   recipient's account. The backend checks whether THIS
    ///   contact's `blocked` field is `true` AND whether THIS
    ///   contact corresponds to the sender (via the principal
    ///   binding resolved from `caller`).
    ///
    /// # Single-user mode (principal_id returns None)
    ///
    /// Backends that have not wired identity cannot match the sender
    /// against a `ChatContact` principal binding. Such backends
    /// SHOULD fail-safe by returning `Ok(false)` (the default impl
    /// does this), which preserves visibility — under-suppression in
    /// a single-user dev deployment is preferable to
    /// over-suppression that would mask a misconfigured backend.
    ///
    /// # When the handler calls this
    ///
    /// `handle_chat_typing` consults this predicate when the target
    /// `Chat` is direct (a single recipient identified by
    /// `Chat.contact_id`). The kit does not implement transport-layer
    /// fan-out (push events to subscribers) — that is the consumer's
    /// responsibility — so the consultation result does not change
    /// the handler's wire response. The call site is the documented
    /// integration point: production transport that fans typing
    /// events out to subscribers SHOULD consult this predicate
    /// before each per-recipient event.
    ///
    /// Group / channel chats are skipped on the kit side. The kit
    /// handler has no way to enumerate fan-out recipients; that work
    /// belongs to the consumer's transport layer, which must call
    /// this predicate per recipient with that recipient's
    /// `account_id`.
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(false)` — no one is blocked. This is appropriate
    /// for backends that have not yet implemented contact lists, and
    /// it matches the workspace's "kit defines the hook; consumer
    /// enforces the policy" posture.
    fn is_contact_blocked(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _contact_id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
        async { Ok(false) }
    }

    /// The owner's identity for `account_id`: the value of the
    /// `ownerUserId` Session field (draft-atwood-jmap-chat-00 §Session
    /// Object Extensions).
    ///
    /// [`crate::contact::handle_contact_set`] rejects a `ChatContact/set`
    /// create whose `identity` equals this value, octet for octet, with
    /// an `invalidProperties` SetError naming `identity`
    /// (draft-atwood-jmap-chat-00 §ChatContact/set: "the owner has no
    /// ChatContact").
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(None)`: the owner's identity is unknown and the check
    /// is skipped. Backends that serve `ownerUserId` MUST override this
    /// to return the same value.
    fn owner_user_id(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send {
        async { Ok(None) }
    }

    /// Authorization gate consulted by `handle_emoji_set` before every
    /// `CustomEmoji/set` create, update, or destroy.
    ///
    /// Per draft-atwood-jmap-chat-00 commit `9344aec` (2026-05-11,
    /// "refactor: implementation-defined emoji authorization"):
    /// authorization for `CustomEmoji/set` is fully
    /// implementation-defined, for both server-global and Space-scoped
    /// emoji. When the caller is not authorized, the handler emits a
    /// `forbidden` SetError (RFC 8620 §5.3).
    ///
    /// # `target_space_id` semantics
    ///
    /// `target_space_id` is the **actual scope of the emoji this op
    /// targets**:
    ///
    /// - `None` — the emoji is server-global (its `spaceId` field is
    ///   absent / null).
    /// - `Some(id)` — the emoji is scoped to that Space (its `spaceId`
    ///   field equals `id`).
    ///
    /// For `Create`, the handler reads the scope from the create
    /// payload. For `Update` and `Destroy`, the handler pre-fetches
    /// the existing emoji via `get_objects` and passes its current
    /// `spaceId`. If the pre-fetch reports the id as not found, the
    /// handler skips this gate entirely and lets `update_object` /
    /// `destroy_object` surface a `notFound` SetError naturally — a
    /// non-existent target consumes no authorization decision.
    ///
    /// # When the handler calls this
    ///
    /// Exactly once per create/update/destroy entry in the request,
    /// AFTER wire-format validation has succeeded and BEFORE the
    /// corresponding `create_object` / `update_object` /
    /// `destroy_object` call. The handler skips the underlying mutation
    /// when the outer `Result` is `Ok(Err(_))` or when the outer is
    /// `Err(_)`. The wire mapping is:
    ///
    /// - `Ok(Ok(()))` — permit; the handler proceeds to the underlying
    ///   `create_object` / `update_object` / `destroy_object`.
    /// - `Ok(Err(set_err))` — deny with reason; the handler serialises
    ///   `set_err` into the appropriate `notCreated` / `notUpdated` /
    ///   `notDestroyed` map verbatim. Backends construct the SetError
    ///   with the type, description, and any `with_extra` fields the
    ///   deployment wants the client to see.
    /// - `Err(e)` — backend infrastructure failure; the handler maps
    ///   to a `serverFail` SetError.
    ///
    /// # Default implementation
    ///
    /// The default returns `Ok(Ok(()))` — every op is permitted. This
    /// matches the workspace's "kit defines the hook; consumer
    /// enforces the policy" posture (see the parallel design of
    /// [`Self::slow_mode_check`]). Production backends SHOULD override
    /// this method to apply the deployment's permission model — e.g.
    /// "only members of `target_space_id` may modify a Space-scoped
    /// emoji" or "only `manage_emoji` permission-holders may modify
    /// server-global emoji."
    ///
    /// # SetError shape recommendation
    ///
    /// Production backends SHOULD return
    /// `Ok(Err(SetError::new(SetErrorType::Forbidden)
    ///     .with_description("…")))` for authorization denials, mirroring
    /// the workspace convention for /set responses. The description
    /// should explain the deployment's denial reason without leaking
    /// internal policy details — e.g. "Space-scoped emoji on this
    /// Space requires `manage_channels` permission" rather than
    /// "principal abc123 is not in role admin on Space xyz789".
    ///
    /// # Foundation seam
    ///
    /// Backends that wish to identify the caller call
    /// [`jmap_server::JmapBackend::principal_id`] on `caller` to learn
    /// whether identity is wired, and compare `account_id` (the owner's
    /// member entry, draft §Terminology) against the Space's `members`
    /// list, or consult ACLs or whatever permission model the
    /// deployment uses. Backends whose
    /// `principal_id` returns `None` (the workspace default, see the
    /// "Caller identity (foundation seam)" section of the workspace
    /// `AGENTS.md`) cannot meaningfully implement identity-scoped
    /// authorization — they should either override `principal_id`
    /// first or return `Ok(Ok(()))` to match the reference posture.
    fn may_set_custom_emoji(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _target_space_id: Option<&jmap_types::Id>,
        _op: EmojiSetOp,
    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
        async { Ok(Ok(())) }
    }

    /// Authorization gate consulted by `handle_message_set` before a
    /// create or update with a non-empty `broadcastMentions` array is
    /// applied.
    ///
    /// Per draft-atwood-jmap-chat-00 §4.4.1: sending a `Message` with
    /// a non-empty `broadcastMentions` array requires the
    /// `"mention_broadcast"` Space permission. The server MUST reject
    /// with `forbidden` when the caller lacks the permission.
    ///
    /// For group/direct chats NOT part of any Space (`spaceId` is
    /// `None`), broadcast mentions are unrestricted per spec; the
    /// handler skips calling this method entirely in that case.
    ///
    /// # `space_id` semantics
    ///
    /// `space_id` is the Space that contains the target Chat (channel).
    /// The handler resolves it by pre-fetching the Chat and reading
    /// its `space_id` field. This method is only called when
    /// `space_id` is `Some` — the handler skips the call for
    /// group/direct chats with no Space.
    ///
    /// # When the handler calls this
    ///
    /// `handle_message_set` invokes `may_broadcast_mention` once per
    /// create entry or edit whose post-patch `broadcastMentions` is
    /// non-empty AND whose target Chat has a `spaceId`; rich-body
    /// `"broadcast"` spans are display-only and never trigger it
    /// (draft-atwood-jmap-chat-00 §Rich Body Format). The call
    /// fires AFTER structural validation (`validate_broadcast_mentions`)
    /// has succeeded and BEFORE `create_object` / `update_object`, so
    /// malformed payloads do not consume an authorization decision and
    /// an unauthorized message never touches storage.
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(Ok(()))` — every op is permitted. This matches the
    /// workspace's "kit defines the hook; consumer enforces the policy"
    /// posture (see the parallel design of [`Self::may_set_custom_emoji`]).
    /// Production backends SHOULD override this method to check the
    /// caller's `mention_broadcast` permission in the target Space's
    /// role hierarchy.
    ///
    /// # SetError shape recommendation
    ///
    /// Production backends SHOULD return
    /// `Ok(Err(SetError::new(SetErrorType::Forbidden)
    ///     .with_description("sender lacks mention_broadcast permission in this Space")))`.
    fn may_broadcast_mention(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _space_id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
        async { Ok(Ok(())) }
    }

    /// Authorization gate consulted by `handle_chat_set` before an update
    /// whose patch touches `pinnedMessageIds` is applied.
    ///
    /// Per draft-atwood-jmap-chat-00 §Chat `pinnedMessageIds`: "For group
    /// chats, only admins may modify this list. For direct chats, the
    /// owner may modify it freely. For channel chats, only members with
    /// the `"pin"` Space permission may modify this list."
    ///
    /// # When the handler calls this
    ///
    /// Once per update entry whose patch sets `pinnedMessageIds` (or a
    /// pointer path beneath it), AFTER wire-format validation and BEFORE
    /// `update_object`. A `chat_id` that does not resolve SHOULD yield
    /// `Ok(Ok(()))` so `update_object` surfaces `notFound`. The return
    /// mapping is the same as [`Self::may_set_custom_emoji`].
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(Ok(()))` — every op is permitted, matching the
    /// parallel hooks above. Production backends SHOULD override this
    /// method and deny with
    /// `Ok(Err(SetError::new(SetErrorType::Forbidden)))`.
    fn may_set_pinned_messages(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _chat_id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
        async { Ok(Ok(())) }
    }

    /// Authorization gate consulted by `handle_ban_set` before each
    /// SpaceBan create, update or destroy.
    ///
    /// Per draft-atwood-jmap-chat-00 §SpaceBan/set: create, update and
    /// destroy each "Requires `"ban"` permission in the Space."
    ///
    /// # When the handler calls this
    ///
    /// Once per entry, AFTER wire-format validation and BEFORE
    /// `create_object` / `update_object` / `destroy_object`. `space_id`
    /// is the create body's `spaceId`, or the stored ban's `spaceId` for
    /// update and destroy; an update or destroy whose ban does not
    /// resolve skips the gate so the backend surfaces `notFound`. The
    /// return mapping is the same as [`Self::may_set_custom_emoji`].
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(Ok(()))` — every op is permitted, matching the
    /// parallel hooks above. Production backends SHOULD override this
    /// method and deny with
    /// `Ok(Err(SetError::new(SetErrorType::Forbidden)))`.
    fn may_set_space_ban(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _space_id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
        async { Ok(Ok(())) }
    }

    /// Authorization gate consulted by `handle_invite_set` before each
    /// SpaceInvite create or destroy.
    ///
    /// Per draft-atwood-jmap-chat-00 §SpaceInvite/set: create "Requires
    /// `"manage_members"` permission"; destroy "Requires
    /// `"manage_members"` permission or ownership of the invite."
    ///
    /// # When the handler calls this
    ///
    /// Once per entry, AFTER wire-format validation and BEFORE
    /// `create_object` / `destroy_object`. `space_id` is the create body's
    /// `spaceId`, or the stored invite's `spaceId` for destroy.
    /// `created_by` is `None` on create and the stored invite's
    /// `createdBy` on destroy. A destroy whose invite does not resolve
    /// skips the gate so the backend surfaces `notFound`. The return
    /// mapping is the same as [`Self::may_set_custom_emoji`].
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(Ok(()))` — every op is permitted, matching the
    /// parallel hooks above. Production backends SHOULD override this
    /// method and deny with
    /// `Ok(Err(SetError::new(SetErrorType::Forbidden)))`.
    fn may_set_space_invite(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _space_id: &jmap_types::Id,
        _created_by: Option<&jmap_types::Id>,
    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
        async { Ok(Ok(())) }
    }

    /// Visibility gate consulted by `handle_invite_get` and
    /// `handle_invite_changes` for each stored SpaceInvite.
    ///
    /// Per draft-atwood-jmap-chat-00 §SpaceInvite/get: "Only members of
    /// the Space may retrieve its invites. Members with
    /// `"manage_members"` permission see all invites; other members see
    /// only invites they created." §SpaceInvite/changes: "The same
    /// visibility rules as `SpaceInvite/get` apply".
    ///
    /// # When the handler calls this
    ///
    /// Once per invite returned by `get_objects`. `Ok(false)` hides the
    /// invite: `/get` reports a requested id as `notFound`, and
    /// `/changes` omits it from `created` and `updated`.
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(true)`, so every invite is visible, matching the
    /// parallel hooks above. Production backends SHOULD override this
    /// method; invite codes are join credentials.
    fn may_get_space_invite(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _invite: &jmap_chat_types::SpaceInvite,
    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
        async { Ok(true) }
    }

    /// Visibility gate consulted by `handle_ban_get` for each stored
    /// SpaceBan.
    ///
    /// Per draft-atwood-jmap-chat-00 §SpaceBan/get: "Members with
    /// `"ban"` permission in the Space see all SpaceBan records for that
    /// Space. A banned user fetching their own account sees only SpaceBan
    /// records where `userId` is the accountId."
    ///
    /// # When the handler calls this
    ///
    /// Once per ban returned by `get_objects`. `Ok(false)` hides the ban,
    /// and a requested id is reported as `notFound`. `SpaceBan/changes`
    /// does not consult this hook, because the spec defines it as plain
    /// "Standard JMAP `/changes`".
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(true)`, so every ban is visible, matching the parallel
    /// hooks above. Production backends SHOULD override this method.
    fn may_get_space_ban(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _ban: &jmap_chat_types::SpaceBan,
    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
        async { Ok(true) }
    }

    /// Referential check consulted by `handle_chat_set` before an update
    /// that adds ids to `pinnedMessageIds` is applied.
    ///
    /// Per draft-atwood-jmap-chat-00 §Chat `pinnedMessageIds`: "Each id
    /// MUST be the id of a Message in this Chat; the server MUST reject a
    /// `Chat/set` update that would add any other id with
    /// `invalidProperties`."
    ///
    /// # When the handler calls this
    ///
    /// After [`Self::may_set_pinned_messages`] permits the update and
    /// BEFORE `update_object`, with `added` holding only the ids in the
    /// new list that are not already in the stored list. Ids already
    /// pinned are not re-checked: the requirement covers adding, and a
    /// stale id left behind by a backend that missed a hard delete must
    /// not make the list impossible to edit. Not called when `added` is
    /// empty or `chat_id` does not resolve.
    ///
    /// Return `Ok(Err(SetError))` with type `invalidProperties` and
    /// properties `["pinnedMessageIds"]` when any id in `added` is not a
    /// Message whose `chatId` is `chat_id`.
    ///
    /// # Default implementation
    ///
    /// Returns `Ok(Ok(()))`, matching the parallel hooks above. Backends
    /// that store Messages SHOULD override it; the spec rule is a MUST.
    fn validate_pinned_message_ids(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _chat_id: &jmap_types::Id,
        _added: &[jmap_types::Id],
    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
        async { Ok(Ok(())) }
    }

    /// Throttle gate consulted before a `Message/set` create lands on
    /// the rate-limited path.
    ///
    /// Per draft-atwood-jmap-chat-00 §Chat `slowModeSeconds` plus spec
    /// commit `de60acb` (2026-05-11, "soften slow-mode exemption to
    /// SHOULD with rationale"): a `Chat` with `slowModeSeconds > 0`
    /// throttles the rate at which a member may post messages. The
    /// spec SHOULD-exempts members holding the `manage_channels`
    /// permission, and servers MAY define additional exempt
    /// principals. The kit does not opine on the exemption set — the
    /// backend implements the policy.
    ///
    /// When the caller is throttled, the backend returns
    /// `Err(`[`SlowModeError`]`)` with `retry_after` set to a UTCDate
    /// the rate-limited sender may use to schedule a retry. The
    /// `Message/set` create handler maps the error onto a
    /// `rateLimited` SetError (RFC 8620 §5.3 `SetError.type`) whose
    /// `serverRetryAfter` extra field carries the UTCDate verbatim.
    ///
    /// # When the handler calls this
    ///
    /// `handle_message_set` invokes `slow_mode_check` once per create
    /// entry, after wire-format validation has succeeded and before
    /// `create_object`. A throttle rejection short-circuits to
    /// `notCreated[create_id] = { type: "rateLimited",
    /// serverRetryAfter: <UTCDate> }` and the create_object call is
    /// never made.
    ///
    /// # Default implementation
    ///
    /// The default returns `Ok(())` — no throttle. This is appropriate
    /// for backends that have not yet implemented rate-tracking and
    /// for single-tenant dev servers where no abuse path exists.
    /// Production backends SHOULD override this method with their own
    /// per-(account, chat, caller) rate tracker.
    ///
    /// # No state on the kit
    ///
    /// The kit deliberately does not provide a reference rate-tracker
    /// — that is deployment territory. The reference `MemoryBackend`
    /// keeps the default no-op behaviour for the same reason.
    fn slow_mode_check(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _chat_id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<(), SlowModeError>> + Send {
        async { Ok(()) }
    }

    /// Hard-delete a message ("expire" it) due to a per-message expiry
    /// event.
    ///
    /// Called by the kit from two places:
    ///
    /// 1. The `Message/set` update handler when a patch sets `readAt` on
    ///    a message whose pre-patch `burnOnRead` is `true`
    ///    (draft-atwood-jmap-chat-00 §Message `burnOnRead`).
    /// 2. A deployment-provided scheduler when a message's
    ///    `senderExpiresAt` fires (draft-atwood-jmap-chat-00 §Message
    ///    `senderExpiresAt`). The kit does NOT run a scheduler;
    ///    consumers wire one in and call this method directly when a
    ///    timer fires.
    ///
    /// # Contract
    ///
    /// Per draft-atwood-jmap-chat-00, an expiry MUST hard-delete the
    /// row (not tombstone) and the message MUST appear in the
    /// `destroyed` set of subsequent `Message/changes` results.
    /// Implementations therefore SHOULD:
    ///
    /// - Remove the underlying record so a subsequent `Message/get`
    ///   does not find it.
    /// - Bump the per-account `Message` state token and record the id
    ///   in the change log so `Message/changes` sees it as destroyed.
    /// - Remove the id from its Chat's `pinnedMessageIds` (§Chat
    ///   `pinnedMessageIds`: "When a pinned Message is hard-deleted, the
    ///   server MUST remove its id from this list."). The same applies to
    ///   `destroy_object::<Message>`.
    ///
    /// # Idempotency
    ///
    /// If the message no longer exists — because a previous
    /// `expire_message` call or [`Self::read_and_burn_message`] has
    /// already deleted it — this method MUST
    /// return `Ok(())`, AND MUST NOT bump the per-account `Message`
    /// state token in that case. The no-op case must be
    /// observationally indistinguishable from "the scheduler never
    /// fired" for `Message/changes` subscribers; a state bump on the
    /// retry path would surface a spurious empty change set on every
    /// re-fire and induce a poll storm. Expiry events are
    /// inherently retry-friendly (a scheduler may re-fire if it
    /// crashed mid-batch). The
    /// return type is `Result<(), Self::Error>` (not
    /// `BackendSetError`) precisely to prevent a `NotFound`
    /// SetError construction on this path: expiry is an internal
    /// scheduler hook, not a `/set`-shaped call, and there is no
    /// JMAP wire surface for per-message expiry rejection.
    ///
    /// Burn-on-read does not use this hook; `Message/set` calls
    /// [`Self::read_and_burn_message`] so the `readAt` write and the
    /// hard-delete are atomic.
    ///
    /// # Default implementation
    ///
    /// The default returns `Ok(())` without doing anything. This is
    /// appropriate for backends that have not yet implemented expiry.
    /// Production backends that need expiry SHOULD override this method.
    fn expire_message(
        &self,
        _caller: &Self::CallerCtx,
        _account_id: &jmap_types::Id,
        _message_id: &jmap_types::Id,
    ) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
        async { Ok(()) }
    }
}

#[cfg(test)]
mod tests {
    use super::SlowModeError;
    use jmap_types::UTCDate;

    #[test]
    fn slow_mode_error_display_contains_retry_after() {
        let err = SlowModeError::new(UTCDate::from("2026-05-16T12:34:56Z"));
        let rendered = format!("{err}");
        assert!(
            rendered.contains("slow-mode rate limit"),
            "Display missing prefix: {rendered}"
        );
        assert!(
            rendered.contains("2026-05-16T12:34:56Z"),
            "Display missing retry_after timestamp: {rendered}"
        );
    }

    #[test]
    fn slow_mode_error_is_std_error() {
        // Compile-time check that SlowModeError implements std::error::Error.
        // If this fails to compile, the trait impl regressed.
        fn assert_is_error<E: std::error::Error>(_: &E) {}
        let err = SlowModeError::new(UTCDate::from("2026-05-16T12:34:56Z"));
        assert_is_error(&err);
    }

    #[test]
    fn slow_mode_error_boxes_as_dyn_error() {
        // Independent oracle: the std error machinery itself. If
        // SlowModeError does not implement std::error::Error this
        // conversion does not compile.
        let err = SlowModeError::new(UTCDate::from("2026-05-16T12:34:56Z"));
        let boxed: Box<dyn std::error::Error> = Box::new(err);
        assert!(boxed.to_string().contains("slow-mode rate limit"));
    }

    use super::OpResult;
    use jmap_server::backend::{SetError, SetErrorType};

    #[test]
    fn op_result_ok_carries_op_index_and_optional_id() {
        let r = OpResult::ok(3, Some(jmap_types::Id::from("space-1")));
        assert_eq!(r.op_index, 3);
        assert_eq!(r.outcome, Ok(Some(jmap_types::Id::from("space-1"))));
    }

    #[test]
    fn op_result_ok_with_none_id_for_update_or_destroy() {
        let r = OpResult::ok(0, None);
        assert_eq!(r.op_index, 0);
        assert_eq!(r.outcome, Ok(None));
    }

    #[test]
    fn op_result_err_carries_op_index_and_set_error() {
        let r = OpResult::err(7, SetError::new(SetErrorType::Forbidden));
        assert_eq!(r.op_index, 7);
        match r.outcome {
            Err(e) => assert_eq!(e.error_type, SetErrorType::Forbidden),
            Ok(_) => panic!("expected Err outcome"),
        }
    }

    #[test]
    fn op_result_with_op_index_replaces_field() {
        let r = OpResult::ok(0, None).with_op_index(42);
        assert_eq!(r.op_index, 42);
        assert_eq!(r.outcome, Ok(None));
    }

    #[test]
    fn op_result_with_outcome_replaces_field() {
        let r =
            OpResult::ok(0, None).with_outcome(Err(SetError::new(SetErrorType::InvalidProperties)));
        assert_eq!(r.op_index, 0);
        match r.outcome {
            Err(e) => assert_eq!(e.error_type, SetErrorType::InvalidProperties),
            Ok(_) => panic!("expected Err outcome after with_outcome"),
        }
    }

    // Const-context smoke tests — lock in the const fn markers so a
    // future refactor that accidentally drops `const` from any of
    // these constructors / builders is caught at compile time
    // (bd:JMAP-x2gd.79).

    use super::ChatLimits;

    /// `OpResult::ok` is callable in const context.
    #[test]
    fn op_result_ok_is_const_fn() {
        const _R: OpResult = OpResult::ok(0, None);
    }

    /// `ChatLimits::new` plus all four `with_*` setters chain in const
    /// context — proves the entire builder pipeline stays const.
    #[test]
    fn chat_limits_builder_is_const_fn() {
        const _LIMITS: ChatLimits = ChatLimits::new(1, 2, 3, 4)
            .with_max_roles_per_space(10)
            .with_max_space_members(20)
            .with_max_channels_per_space(30)
            .with_max_categories_per_space(40);
    }

    // Note: `SlowModeError::new` is `const fn` (clippy::nursery
    // `missing_const_for_fn` would re-fire if the marker were dropped),
    // but a const-context smoke test cannot be authored without a
    // const-constructible `UTCDate`. `UTCDate` wraps a `String`, and
    // `String` is not const-constructible from a non-empty literal in
    // stable Rust today. The clippy lint covers the regression case.
}