agntcy-slim-session 0.3.0

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

use std::{
    collections::{HashMap, VecDeque},
    time::Duration,
};

use display_error_chain::ErrorChainExt;

use slim_auth::traits::{TokenProvider, Verifier};
use slim_datapath::{
    api::{
        CommandPayload, MlsPayload, NameId, Participant, ProtoMessage as Message, ProtoMlsSettings,
        ProtoName, ProtoSessionMessageType, ProtoSessionType,
    },
    messages::utils::{DELETE_GROUP, DISCONNECTION_DETECTED, LEAVING_SESSION, TRUE_VAL},
};
use slim_mls::mls::Mls;
use tokio::sync::oneshot;

use tracing::debug;

use crate::{
    common::{MessageDirection, SessionMessage, SessionOutput},
    errors::SessionError,
    mls_state::{MlsModeratorState, MlsState},
    moderator_task::{
        AddParticipant, ModeratorTask, NotifyParticipants, RemoveParticipant, TaskUpdate,
    },
    runtime::maybe_await,
    session_controller::SessionControllerCommon,
    session_settings::SessionSettings,
    subscription_manager::{SubscriptionManager, SubscriptionOps},
    traits::{MessageHandler, ProcessingState},
};

pub struct SessionModerator<P, V, I, M = SubscriptionManager>
where
    P: TokenProvider + Send + Sync + Clone + 'static,
    V: Verifier + Send + Sync + Clone + 'static,
    I: MessageHandler + Send + Sync + 'static,
    M: SubscriptionOps,
{
    /// Queue of tasks to be performed by the moderator
    /// Each task contains a message and an optional ack channel
    tasks_todo: VecDeque<(Message, Option<oneshot::Sender<Result<(), SessionError>>>)>,

    /// Current task being processed by the moderator
    current_task: Option<ModeratorTask>,

    /// MLS state for the moderator
    mls_state: Option<MlsModeratorState<P, V>>,

    /// List of group participants
    /// The key is the participant name without ID
    /// The value contains the full and name and the participant settings
    group_list: HashMap<ProtoName, Participant>,

    /// Common settings
    common: SessionControllerCommon<P, V, M>,

    /// Postponed message to be sent after current task completion
    postponed_message: Option<Message>,

    /// Subscription status
    subscribed: bool,

    /// connection id to the remote node
    conn_id: Option<u64>,

    /// Inner message handler
    inner: I,
}

impl<P, V, I, M> SessionModerator<P, V, I, M>
where
    P: TokenProvider + Send + Sync + Clone + 'static,
    V: Verifier + Send + Sync + Clone + 'static,
    I: MessageHandler + Send + Sync + 'static,
    M: SubscriptionOps,
{
    pub(crate) fn new(inner: I, settings: SessionSettings<P, V, M>) -> Self {
        let common = SessionControllerCommon::new(settings);

        SessionModerator {
            tasks_todo: vec![].into(),
            current_task: None,
            mls_state: None,
            group_list: HashMap::new(),
            common,
            postponed_message: None,
            subscribed: false,
            conn_id: None,
            inner,
        }
    }
}

/// Implementation of MessageHandler trait for SessionModerator
/// This allows the moderator to be used as a layer in the generic layer system
impl<P, V, I, M> MessageHandler for SessionModerator<P, V, I, M>
where
    P: TokenProvider + Send + Sync + Clone + 'static,
    V: Verifier + Send + Sync + Clone + 'static,
    I: MessageHandler + Send + Sync + 'static,
    M: SubscriptionOps,
{
    async fn init(&mut self) -> Result<(), SessionError> {
        // Initialize MLS
        self.mls_state = if let Some(mls_settings) = &self.common.settings.config.mls_settings {
            let mls_state = MlsState::new(
                Mls::new(
                    self.common.settings.identity_provider.clone(),
                    self.common.settings.identity_verifier.clone(),
                ),
                mls_settings.header_integrity_validation_percent,
            )
            .await
            .expect("failed to create MLS state");
            Some(MlsModeratorState::new(mls_state))
        } else {
            None
        };

        Ok(())
    }

    async fn on_message(&mut self, message: SessionMessage) -> Result<SessionOutput, SessionError> {
        let mut output = SessionOutput::new();

        match message {
            SessionMessage::OnMessage {
                mut message,
                direction,
                ack_tx,
            } => {
                if message.get_session_message_type().is_command_message() {
                    debug!(
                        message = ?message.get_session_message_type(),
                        source = %message.get_source(),
                        "received  message",
                    );
                    output.extend(self.process_control_message(message, ack_tx).await?);
                } else {
                    if direction == MessageDirection::South
                        && self.common.settings.config.session_type
                            == ProtoSessionType::PointToPoint
                    {
                        message
                            .get_slim_header_mut()
                            .set_destination(self.common.settings.destination.clone());
                    }

                    if direction == MessageDirection::North
                        && let Some(mls_state) = &mut self.mls_state
                    {
                        maybe_await!(mls_state.common.process_message(&mut message, direction))?;
                    }

                    let inner_output = self
                        .inner
                        .on_message(SessionMessage::OnMessage {
                            message,
                            direction,
                            ack_tx,
                        })
                        .await?;

                    output.extend(inner_output);
                }
            }
            SessionMessage::MessageError { error } => {
                output.extend(self.handle_message_error(error).await?);
            }
            SessionMessage::TimerTimeout {
                message_id,
                message_type,
                name,
                timeouts,
            } => {
                if message_type.is_command_message() {
                    output.extend(
                        self.common
                            .sender
                            .on_timer_timeout(message_id, message_type)?,
                    );
                } else {
                    let inner_output = self
                        .inner
                        .on_message(SessionMessage::TimerTimeout {
                            message_id,
                            message_type,
                            name,
                            timeouts,
                        })
                        .await?;

                    output.extend(inner_output);
                }
            }
            SessionMessage::TimerFailure {
                message_id,
                message_type,
                name,
                timeouts,
            } => {
                if message_type.is_command_message() {
                    self.handle_failure(
                        message_id,
                        message_type,
                        SessionError::MessageSendRetryFailed { id: message_id },
                    )
                    .await?;
                } else {
                    output.extend(
                        self.inner
                            .on_message(SessionMessage::TimerFailure {
                                message_id,
                                message_type,
                                name,
                                timeouts,
                            })
                            .await?,
                    );
                }
            }
            SessionMessage::StartDrain { grace_period: _ } => {
                debug!("start draining by calling delete_all");
                self.common.processing_state = ProcessingState::Draining;
                let p = CommandPayload::builder().leave_request().as_content();
                let destination = self.common.settings.control.clone();
                let mut leave_msg = self.common.create_control_message(
                    &destination,
                    ProtoSessionMessageType::LeaveRequest,
                    rand::random::<u32>(),
                    p,
                    false,
                )?;
                leave_msg.insert_metadata(DELETE_GROUP.to_string(), TRUE_VAL.to_string());

                output.extend(self.delete_all(None).await?);
            }
            SessionMessage::ParticipantDisconnected {
                name: opt_participant,
            } => {
                let participant =
                    opt_participant.ok_or(SessionError::MissingParticipantNameOnDisconnection)?;
                debug!(
                    %participant,
                    "Participant not anymore connected to the current session",
                );

                let mut msg = self.common.create_control_message(
                    &participant.clone(),
                    ProtoSessionMessageType::LeaveRequest,
                    rand::random::<u32>(),
                    CommandPayload::builder().leave_request().as_content(),
                    false,
                )?;
                msg.insert_metadata(DISCONNECTION_DETECTED.to_string(), TRUE_VAL.to_string());

                output.extend(self.on_disconnection_detected(msg, None).await?);
            }
            _ => {
                return Err(SessionError::SessionMessageInternalUnexpected(Box::new(
                    message,
                )));
            }
        }

        maybe_await!(self.encrypt_output(&mut output))?;

        Ok(output)
    }

    async fn add_endpoint(
        &mut self,
        endpoint: &Participant,
    ) -> Result<SessionOutput, SessionError> {
        self.inner.add_endpoint(endpoint).await
    }

    fn remove_endpoint(&mut self, endpoint: &ProtoName) {
        self.inner.remove_endpoint(endpoint);
    }

    fn needs_drain(&self) -> bool {
        !self.common.sender.drain_completed()
            || self.inner.needs_drain()
            || !self.tasks_todo.is_empty()
    }
    fn processing_state(&self) -> ProcessingState {
        self.common.processing_state
    }

    fn participants_list(&self) -> Vec<ProtoName> {
        self.group_list
            .iter()
            .map(|(name, p)| {
                let id = p
                    .name
                    .as_ref()
                    .map(|n| n.id())
                    .unwrap_or(NameId::NULL_COMPONENT); // the name should always be present
                name.clone().with_id(id)
            })
            .collect()
    }

    async fn on_shutdown(&mut self) -> Result<(), SessionError> {
        // Moderator-specific cleanup
        self.subscribed = false;
        self.common.sender.close();

        // Remove route and subscription for multicast sessions
        if self.common.settings.config.session_type == ProtoSessionType::Multicast
            && let Some(conn) = self.conn_id
        {
            self.common
                .delete_route(self.common.settings.destination.clone(), conn)
                .await?;
            self.common
                .delete_subscription(self.common.settings.destination.clone(), conn)
                .await?;
            self.common
                .delete_route(self.common.settings.control.clone(), conn)
                .await?;
            // Note: No subscription to control channel - moderator only sends to it
        }

        // Shutdown inner layer
        MessageHandler::on_shutdown(&mut self.inner).await?;

        self.send_close_signal().await;

        Ok(())
    }
}

impl<P, V, I, M> SessionModerator<P, V, I, M>
where
    P: TokenProvider + Send + Sync + Clone + 'static,
    V: Verifier + Send + Sync + Clone + 'static,
    I: MessageHandler + Send + Sync + 'static,
    M: SubscriptionOps,
{
    #[maybe_async::maybe_async]
    async fn encrypt_output(&mut self, output: &mut SessionOutput) -> Result<(), SessionError> {
        crate::session_controller::SessionController::apply_identity_to_slim_output(
            output,
            &self.common.settings.identity_provider,
        )?;
        if let Some(mls_state) = &mut self.mls_state {
            mls_state.common.encrypt_output(output).await?;
        }
        Ok(())
    }

    /// Helper method to handle MessageError
    /// Extracts context from the error and routes to appropriate handler
    async fn handle_message_error(
        &mut self,
        error: SessionError,
    ) -> Result<SessionOutput, SessionError> {
        let Some(session_ctx) = error.session_context() else {
            tracing::warn!("Received MessageError without session context");
            return self
                .inner
                .on_message(SessionMessage::MessageError { error })
                .await;
        };

        if error.is_command_message_error() {
            self.handle_failure(
                session_ctx.message_id,
                session_ctx.get_session_message_type(),
                error,
            )
            .await?;
            Ok(SessionOutput::new())
        } else {
            self.inner
                .on_message(SessionMessage::MessageError { error })
                .await
        }
    }

    /// Helper method to handle failures, either from a timer or message error
    async fn handle_failure(
        &mut self,
        message_id: u32,
        message_type: ProtoSessionMessageType,
        error: SessionError,
    ) -> Result<(), SessionError> {
        self.common.sender.on_failure(message_id, message_type);

        // the task should always exist at this point
        if let Some(task) = self.current_task.as_mut()
            && let Some(ack_tx) = task.ack_tx_take()
        {
            let _ = ack_tx.send(Err(task.failure_message(error)));
        }

        // delete current task and pick a new one
        self.current_task = None;
        self.pop_task().await
    }

    /// Helper method to handle errors after task creation
    /// Extracts ack_tx from current_task and sends the error
    fn handle_task_error(&mut self, error: SessionError) -> SessionError {
        if let Some(task) = self.current_task.take() {
            let ack_tx = match task {
                ModeratorTask::Add(t) => t.ack_tx,
                ModeratorTask::Remove(t) => t.ack_tx,
                ModeratorTask::Update(t) => t.ack_tx,
                ModeratorTask::CloseOrDisconnect(t) => t.ack_tx,
            };
            if let Some(tx) = ack_tx {
                let _ = tx.send(Err(SessionError::cleanup_failed(&error)));
            }
        }

        // Remove task
        self.current_task = None;

        error
    }

    /// Helper method to prepare for shutdown by cleaning up state
    /// Sets processing state to draining, removes MLS state, clears tasks and timers,
    /// and signals drain to inner layer and sender
    async fn prepare_shutdown(&mut self) -> Result<(), SessionError> {
        debug!("Preparing for shutdown: cleaning up state");
        // set the processing state to draining
        self.common.processing_state = ProcessingState::Draining;
        // remove mls state
        self.mls_state = None;
        // clear all pending tasks
        self.tasks_todo.clear();
        // clear all pending timers
        self.common.sender.clear_timers();
        // signal start drain everywhere
        self.inner
            .on_message(SessionMessage::StartDrain {
                grace_period: Duration::from_secs(60), // not used in session
            })
            .await?;
        self.common.sender.start_drain();
        Ok(())
    }

    /// Helper method to remove a participant from the group list and compute MLS payload
    /// Returns the list of remaining participants and the MLS payload if MLS is enabled
    async fn remove_participant_and_compute_mls(
        &mut self,
        participant: &ProtoName,
        msg: &Message,
    ) -> Result<(Vec<ProtoName>, Option<MlsPayload>), SessionError> {
        // Build participants list with current participants
        // the group update needs to be received by everybody
        // in the group unless there are only 2 participants
        // (the moderator and a participant)
        let participants_vec: Vec<ProtoName> = self
            .group_list
            .iter()
            .map(|(n, p)| p.get_name().map(|name| n.clone().with_id(name.id())))
            .collect::<Result<Vec<ProtoName>, _>>()?;

        // Remove participant from group list
        let mut participant_no_id = participant.clone();
        participant_no_id.reset_id();
        self.group_list.remove(&participant_no_id);

        // Remove endpoint from local session
        self.remove_endpoint(participant);

        // Compute MLS payload if needed
        let mls_payload = match self.mls_state.as_mut() {
            Some(state) => {
                let mls_content = maybe_await!(state.remove_participant(msg))
                    .map_err(|e| self.handle_task_error(e))?;
                let commit_id = self.mls_state.as_mut().unwrap().get_next_mls_mgs_id();
                Some(MlsPayload {
                    commit_id,
                    mls_content,
                })
            }
            None => None,
        };

        Ok((participants_vec, mls_payload))
    }

    /// Helper method to send a GroupRemove message to notify participants
    /// Returns the message ID of the sent GroupRemove message
    async fn send_group_remove(
        &mut self,
        removed_participant: ProtoName,
        participants: Vec<ProtoName>,
        mls_payload: Option<MlsPayload>,
    ) -> Result<(u32, SessionOutput), SessionError> {
        let update_payload = CommandPayload::builder()
            .group_remove(removed_participant, participants, mls_payload)
            .as_content();
        let msg_id = rand::random::<u32>();

        let output = self.common.send_control_message(
            &self.common.settings.control.clone(),
            ProtoSessionMessageType::GroupRemove,
            msg_id,
            update_payload,
            None,
            true,
        )?;

        Ok((msg_id, output))
    }

    async fn process_control_message(
        &mut self,
        message: Message,
        ack_tx: Option<oneshot::Sender<Result<(), SessionError>>>,
    ) -> Result<SessionOutput, SessionError> {
        match message.get_session_message_type() {
            ProtoSessionMessageType::DiscoveryRequest => {
                self.on_discovery_request(message, ack_tx).await
            }
            ProtoSessionMessageType::DiscoveryReply => self.on_discovery_reply(message).await,
            ProtoSessionMessageType::JoinReply => self.on_join_reply(message).await,
            ProtoSessionMessageType::LeaveRequest => {
                // the LeaveRequest message is also used to signal the disconnection of
                // a remote participant. if the metadata contains the key "DISCONNECTION_DETECTED"
                // or "LEAVING_SESSION" call the function on_disconnection_detected
                if message.contains_metadata(DISCONNECTION_DETECTED)
                    || message.contains_metadata(LEAVING_SESSION)
                {
                    return self.on_disconnection_detected(message, ack_tx).await;
                }

                // otherwise start the leave process
                self.on_leave_request(message, ack_tx).await
            }
            ProtoSessionMessageType::LeaveReply => self.on_leave_reply(message).await,
            ProtoSessionMessageType::GroupAck => self.on_group_ack(message).await,
            ProtoSessionMessageType::Ping => self.common.sender.on_message(&message),
            ProtoSessionMessageType::GroupProposal => todo!(),
            ProtoSessionMessageType::JoinRequest
            | ProtoSessionMessageType::GroupAdd
            | ProtoSessionMessageType::GroupRemove
            | ProtoSessionMessageType::GroupWelcome
            | ProtoSessionMessageType::GroupClose
            | ProtoSessionMessageType::GroupNack => Err(
                SessionError::SessionMessageTypeUnexpected(message.get_session_message_type()),
            ),
            _ => Err(SessionError::SessionMessageTypeUnknown(
                message.get_session_message_type(),
            )),
        }
    }

    /// message processing functions
    async fn on_discovery_request(
        &mut self,
        mut msg: Message,
        ack_tx: Option<oneshot::Sender<Result<(), SessionError>>>,
    ) -> Result<SessionOutput, SessionError> {
        debug!(%self.common.settings.id, "received discovery request");
        // the channel discovery starts a new participant invite.
        // process the request only if not busy
        if self.current_task.is_some() {
            debug!(
                "Moderator is busy. Add invite participant task to the list and process it later"
            );
            // if busy postpone the task and add it to the todo list with its ack_tx
            self.tasks_todo.push_back((msg, ack_tx));
            return Ok(SessionOutput::new());
        }

        // now the moderator is busy - create the task first
        debug!("Create AddParticipant task with ack_tx");
        self.current_task = Some(ModeratorTask::Add(AddParticipant::new(ack_tx)));

        // check if the participant is already part of the group
        let new_participant_name = msg.get_dst();
        if self.group_list.contains_key(&new_participant_name) {
            let err = SessionError::ParticipantAlreadyInGroup(new_participant_name);
            return Err(self.handle_task_error(err));
        }

        // start the current task
        let id = rand::random::<u32>();
        msg.get_session_header_mut().set_message_id(id);
        self.current_task
            .as_mut()
            .unwrap()
            .discovery_start(id)
            .map_err(|e| self.handle_task_error(e))?;

        debug!(
            dst = %msg.get_dst(),
            id = msg.get_id(),
            "send discovery request",
        );
        self.common
            .send_with_timer(msg)
            .map_err(|e| self.handle_task_error(e))
    }

    async fn on_discovery_reply(&mut self, msg: Message) -> Result<SessionOutput, SessionError> {
        debug!(
            source = %msg.get_source(),
            id = msg.get_id(),
            "discovery reply",
        );
        // update sender status to stop timers
        let mut output = self.common.sender.on_message(&msg)?;

        // evolve the current task state
        // the discovery phase is completed
        self.current_task
            .as_mut()
            .unwrap()
            .discovery_complete(msg.get_id())?;

        // join the channel if needed
        self.join(msg.get_source(), msg.get_incoming_conn()).await?;

        // set a route to the remote participant
        self.common
            .add_route(msg.get_source(), msg.get_incoming_conn())
            .await?;

        // if this is a multicast session we need to add a route for the channel
        // on the connection from where we received the message. This has to be done
        // all the times because the messages from the remote endpoints may come from
        // different connections. In case the route exists already it will be just ignored
        if self.common.settings.config.session_type == ProtoSessionType::Multicast {
            self.common
                .add_route(
                    self.common.settings.destination.clone(),
                    msg.get_incoming_conn(),
                )
                .await?;
            self.common
                .add_route(
                    self.common.settings.control.clone(),
                    msg.get_incoming_conn(),
                )
                .await?;
        }

        // an endpoint replied to the discovery message
        // send a join message
        let msg_id = rand::random::<u32>();

        let channel = if self.common.settings.config.session_type == ProtoSessionType::Multicast {
            // using the destination as channel name, the control name can be recreated by the participants
            Some(self.common.settings.destination.clone())
        } else {
            None
        };

        let mls_settings =
            self.common
                .settings
                .config
                .mls_settings
                .as_ref()
                .map(|s| ProtoMlsSettings {
                    header_integrity_validation_percent: s.header_integrity_validation_percent,
                });

        let payload = CommandPayload::builder()
            .join_request(
                self.common.settings.config.max_retries,
                self.common.settings.config.interval,
                channel,
                mls_settings,
            )
            .as_content();

        debug!(
            dst = %msg.get_slim_header().get_source(),
            id = msg_id,
            "send join request",
        );
        output.extend(self.common.send_control_message(
            &msg.get_slim_header().get_source(),
            ProtoSessionMessageType::JoinRequest,
            msg_id,
            payload,
            Some(self.common.settings.config.metadata.clone()),
            false,
        )?);

        // evolve the current task state
        // start the join phase
        self.current_task.as_mut().unwrap().join_start(msg_id)?;

        Ok(output)
    }

    async fn on_join_reply(&mut self, msg: Message) -> Result<SessionOutput, SessionError> {
        debug!(
            source = %msg.get_source(),
            id = msg.get_id(),
            "join reply",
        );
        // stop the timer for the join request
        let mut output = self.common.sender.on_message(&msg)?;

        // evolve the current task state
        // the join phase is completed
        self.current_task
            .as_mut()
            .unwrap()
            .join_complete(msg.get_id())?;

        // at this point the participant is part of the group so we can add it to the list
        let new_participant = msg
            .extract_join_reply()?
            .participant
            .clone()
            .ok_or(SessionError::MissingParticipantSettings)?;
        let mut new_name = new_participant.get_name()?;
        // notify the local session that a new participant was added to the group
        debug!(session_name = %new_name, "add endpoint");
        self.add_endpoint(&new_participant).await?;

        new_name.reset_id();
        self.group_list.insert(new_name, new_participant.clone());

        // get mls data if MLS is enabled
        let (commit, welcome) = if let Some(mls_state) = &mut self.mls_state {
            let (commit_payload, welcome_payload) = maybe_await!(mls_state.add_participant(&msg))?;

            // get the id of the commit, the welcome message has a random id
            let commit_id = self.mls_state.as_mut().unwrap().get_next_mls_mgs_id();

            let commit = MlsPayload {
                commit_id,
                mls_content: commit_payload,
            };
            let welcome = MlsPayload {
                commit_id,
                mls_content: welcome_payload,
            };

            (Some(commit), Some(welcome))
        } else {
            (None, None)
        };

        // Create participants list for the messages to send
        let participants_vec = self.group_list.values().cloned().collect::<Vec<_>>();

        // send the group update
        if participants_vec.len() > 2 {
            debug!("participant len is > 2, send a group update");
            let update_payload = CommandPayload::builder()
                .group_add(new_participant, participants_vec.clone(), commit)
                .as_content();
            let add_msg_id = rand::random::<u32>();
            debug!(id = %add_msg_id, "send add update to channel");
            output.extend(self.common.send_control_message(
                &self.common.settings.control.clone(),
                ProtoSessionMessageType::GroupAdd,
                add_msg_id,
                update_payload,
                None,
                true,
            )?);
            self.current_task
                .as_mut()
                .unwrap()
                .commit_start(add_msg_id)?;
        } else {
            // no commit message will be sent so update the task state to consider the commit as received
            // the timer id is not important here, it just need to be consistent
            debug!("cancel the a group update task");
            self.current_task.as_mut().unwrap().commit_start(12345)?;
            self.current_task
                .as_mut()
                .unwrap()
                .update_phase_completed(12345)?;
        }

        // send welcome message
        let welcome_msg_id = rand::random::<u32>();
        let welcome_payload = CommandPayload::builder()
            .group_welcome(participants_vec, welcome)
            .as_content();
        debug!(
            dst = %msg.get_slim_header().get_source(),
            id = %welcome_msg_id,
            "send welcome message",
        );
        output.extend(self.common.send_control_message(
            &msg.get_slim_header().get_source(),
            ProtoSessionMessageType::GroupWelcome,
            welcome_msg_id,
            welcome_payload,
            None,
            false,
        )?);

        // evolve the current task state
        // welcome start
        self.current_task
            .as_mut()
            .unwrap()
            .welcome_start(welcome_msg_id)?;

        Ok(output)
    }

    async fn on_leave_request(
        &mut self,
        mut msg: Message,
        ack_tx: Option<oneshot::Sender<Result<(), SessionError>>>,
    ) -> Result<SessionOutput, SessionError> {
        if self.current_task.is_some() {
            // if busy postpone the task and add it to the todo list with its ack_tx
            debug!("Moderator is busy. Add leave request task to the list and process it later");
            self.tasks_todo.push_back((msg, ack_tx));
            return Ok(SessionOutput::new());
        }

        debug!("Create RemoveParticipant task");
        self.current_task = Some(ModeratorTask::Remove(RemoveParticipant::new(ack_tx)));

        let dst_without_id = msg.get_dst();
        // Look up participant ID in group list
        let id = match self.group_list.get(&dst_without_id) {
            Some(p) => p.get_name()?.id(),
            None => {
                let err = SessionError::ParticipantNotFound(dst_without_id);
                return Err(self.handle_task_error(err));
            }
        };

        // Set destination with ID and message ID (common to both cases)
        let dst_with_id = dst_without_id.clone().with_id(id);
        msg.get_slim_header_mut().set_destination(dst_with_id);
        msg.set_message_id(rand::random::<u32>());

        let leave_message = msg;

        // Remove the participant from the group list and compute MLS payload
        debug!(
            session_name = %leave_message.get_dst(),
            "remove endpoint from the session",
        );

        let (participants_vec, mls_payload) = self
            .remove_participant_and_compute_mls(&leave_message.get_dst(), &leave_message)
            .await?;

        if participants_vec.len() > 2 {
            // in this case we need to send first the group update and later the leave message
            let (msg_id, output) = self
                .send_group_remove(leave_message.get_dst(), participants_vec, mls_payload)
                .await?;
            self.current_task.as_mut().unwrap().commit_start(msg_id)?;

            // We need to save the leave message and send it after
            // the reception of all the acks for the group update message
            // see on_group_ack for postponed_message handling
            self.postponed_message = Some(leave_message);
            Ok(output)
        } else {
            // no commit message will be sent so update the task state to consider the commit as received
            // the timer id is not important here, it just need to be consistent
            self.current_task.as_mut().unwrap().commit_start(12345)?;
            self.current_task
                .as_mut()
                .unwrap()
                .update_phase_completed(12345)?;

            // just send the leave message in this case
            let output = self.common.sender.on_message(&leave_message)?;

            self.current_task
                .as_mut()
                .unwrap()
                .leave_start(leave_message.get_id())?;
            Ok(output)
        }
    }

    async fn on_disconnection_detected(
        &mut self,
        mut msg: Message,
        ack_tx: Option<oneshot::Sender<Result<(), SessionError>>>,
    ) -> Result<SessionOutput, SessionError> {
        // if the disconnection was detected (no metadata in the message) the leave message is
        // sent toward the participant that was disconnected. Otherwise the leave message is sent
        // from the participant that wants to disconnect
        let disconnected = if msg.contains_metadata(LEAVING_SESSION) {
            msg.get_source()
        } else {
            msg.get_dst()
        };

        let mut disconnected_no_id = disconnected.clone();
        disconnected_no_id.reset_id();

        // check that the participant is actually part of the group
        if !self.group_list.contains_key(&disconnected_no_id) {
            debug!(
                "detected disconnection of participant {} that is not part of the group, ignore the message",
                disconnected
            );
            return Ok(SessionOutput::new());
        }

        debug!(%disconnected, "disconnection detected");

        // Send error notification to the application
        let error = SessionError::ParticipantDisconnected(disconnected.clone());
        let mut output = SessionOutput::to_app(Err(error));

        // if the disconnection was detected nothing to do here,
        // otherwise we need to reply, change the metadata and swap
        // source and destination so that we can process the message
        // as if the disconnection was detected locally
        if msg.contains_metadata(LEAVING_SESSION) {
            // send a reply to the source of the message
            // since this is a leave request message the sender is expecting
            // a leave reply message
            let reply = self.common.create_control_message(
                &disconnected,
                ProtoSessionMessageType::LeaveReply,
                msg.get_id(),
                CommandPayload::builder().leave_reply().as_content(),
                false,
            )?;
            // the participant will be removed from the group so we need to remove
            // it from the local sender.
            self.common.sender.remove_participant(&disconnected);
            output.extend(SessionOutput::to_slim(reply));

            // replace LEAVING_SESSION with DISCONNECTION_DETECTED so that if the process of the
            // message needs to be delayed because the moderator is busy we do not send the reply twice
            msg.remove_metadata(LEAVING_SESSION);
            msg.insert_metadata(DISCONNECTION_DETECTED.to_string(), TRUE_VAL.to_string());
            let header = msg.get_slim_header_mut();
            header.set_destination(disconnected.clone());
            header.set_source(self.common.settings.source.clone());
        }

        // if the session is P2P or no one is left on the session close it
        // if self.group_list.len() == 2 only the moderator and the participant
        // to remove are still in the list
        if self.common.settings.config.session_type == ProtoSessionType::PointToPoint
            || self.group_list.len() == 2
        {
            debug!("no one is left connected connected to the session, close it");
            // if the remote endpoint got disconnected on a P2P session
            // simply notify the app and close the session
            self.prepare_shutdown().await?;
            // remove the last endpoint
            self.remove_endpoint(&msg.get_dst());

            // the control will exit and call the shutdown
            // no need to do it here
            return Ok(output);
        }

        if self.current_task.is_some() {
            // if busy postpone the task and add it to the todo list with its ack_tx
            debug!(
                "Moderator is busy. Add disconnection handling task to the list and process it later"
            );
            self.tasks_todo.push_back((msg, ack_tx));
            return Ok(output);
        }

        debug!("Create disconnected task for the disconnection handling");
        // Reuse the disconnection task here, however we don't need to send the leave message
        // so we can mark it as done immediately
        self.current_task = Some(ModeratorTask::CloseOrDisconnect(NotifyParticipants::new(
            ack_tx,
        )));

        // Remove the participant from the group list and compute MLS payload
        debug!(
            endpoint = %disconnected,
            "remove disconnected endpoint from the session",
        );

        let (participants_vec, mls_payload) = self
            .remove_participant_and_compute_mls(&disconnected, &msg)
            .await?;

        // Notify all the participants left and update the MLS state if needed
        let (msg_id, remove_output) = self
            .send_group_remove(disconnected, participants_vec, mls_payload)
            .await?;
        output.extend(remove_output);
        self.current_task.as_mut().unwrap().commit_start(msg_id)?;

        Ok(output)
    }

    async fn delete_all(
        &mut self,
        ack_tx: Option<oneshot::Sender<Result<(), SessionError>>>,
    ) -> Result<SessionOutput, SessionError> {
        debug!("receive a close channel message, send signals to all participants");
        self.prepare_shutdown().await?;

        // Collect the participants and create the close message
        let participants: Vec<ProtoName> = self
            .group_list
            .iter()
            .map(|(n, p)| p.get_name().map(|name| n.clone().with_id(name.id())))
            .collect::<Result<Vec<ProtoName>, _>>()?;

        if participants.len() == 1 {
            // in this case the moderator is the only one remained
            // in the group so there is nothing to do
            return Ok(SessionOutput::new());
        }

        let destination = self.common.settings.control.clone();
        let close_id = rand::random::<u32>();
        let close = self.common.create_control_message(
            &destination,
            ProtoSessionMessageType::GroupClose,
            close_id,
            CommandPayload::builder()
                .group_close(participants)
                .as_content(),
            true,
        )?;

        // create the close task
        self.current_task = Some(ModeratorTask::CloseOrDisconnect(NotifyParticipants::new(
            ack_tx,
        )));
        self.current_task.as_mut().unwrap().commit_start(close_id)?;

        // sent the message
        self.common.sender.on_message(&close)
    }

    async fn on_leave_reply(&mut self, msg: Message) -> Result<SessionOutput, SessionError> {
        debug!(
            from = %msg.get_source(),
            id = %msg.get_id(),
            "received leave reply",
        );
        let msg_id = msg.get_id();

        // delete the route to the source of the message
        self.common
            .delete_route(msg.get_source(), msg.get_incoming_conn())
            .await?;

        // notify the sender and see if we can pick another task
        let output = self.common.sender.on_message(&msg)?;
        if !self.common.sender.is_still_pending(msg_id) {
            self.current_task.as_mut().unwrap().leave_complete(msg_id)?;
        }

        self.task_done().await?;
        Ok(output)
    }

    async fn on_group_ack(&mut self, msg: Message) -> Result<SessionOutput, SessionError> {
        debug!(
            from = %msg.get_source(),
            id = %msg.get_id(),
            "received group ack",
        );
        // notify the sender
        let mut output = self.common.sender.on_message(&msg)?;

        // check if the timer is done
        let msg_id = msg.get_id();
        if !self.common.sender.is_still_pending(msg_id) {
            debug!(
                id = %msg_id,
                "process group ack. try to close task",
            );
            // `is_still_pending` returns false for any ID that is not actively
            // tracked — including IDs that were already cleaned up when a task
            // completed or failed.  Guard against a late / retransmitted GroupAck
            // arriving after the task has been cleared; such an ACK is harmless
            // and should be silently discarded rather than causing a panic.
            let Some(task) = self.current_task.as_mut() else {
                debug!(
                    id = %msg_id,
                    "received group ack for completed/unknown task, ignoring",
                );
                return Ok(output);
            };
            // we received all the messages related to this timer
            // check if we are done and move on
            task.update_phase_completed(msg_id)?;

            // check if the task is finished.
            if !self.current_task.as_mut().unwrap().task_complete() {
                // if the task is not finished yet we may need to send a leave
                // message that was postponed to send all group update first
                if let Some(leave_message) = &self.postponed_message
                    && matches!(self.current_task, Some(ModeratorTask::Remove(_)))
                {
                    // send the leave message an progress
                    output.extend(self.common.sender.on_message(leave_message)?);
                    self.current_task
                        .as_mut()
                        .unwrap()
                        .leave_start(leave_message.get_id())?;
                    // rest the postponed message
                    self.postponed_message = None;
                }
            }

            // check if we can progress with another task
            self.task_done().await?;
        } else {
            debug!(
                id = %msg_id,
                "timer for message still pending, do not close the task",
            );
        }

        Ok(output)
    }

    /// task handling functions
    async fn task_done(&mut self) -> Result<(), SessionError> {
        if !self.current_task.as_ref().unwrap().task_complete() {
            // the task is not completed so just return
            // and continue with the process
            debug!("Current task is NOT completed");
            return Ok(());
        }

        // here the moderator is not busy anymore
        self.current_task = None;
        self.pop_task().await
    }

    async fn pop_task(&mut self) -> Result<(), SessionError> {
        if self.current_task.is_some() {
            // moderator is busy, nothing else to do
            return Ok(());
        }

        // check if there is a pending task to process
        let (msg, ack_tx) = match self.tasks_todo.pop_front() {
            Some(task) => task,
            None => {
                // nothing else to do
                debug!("No tasks left to perform");

                // No need to close the session here. If we are in
                // closing state the moderator will be closed in
                // the controller loop
                return Ok(());
            }
        };

        debug!("Re-enqueue a task from the todo list onto the processing loop");
        // Send the task back to the processing loop instead of recursing into
        // `on_message`. Recursive async calls would otherwise require a boxed
        // future (and `async_trait` was hiding that cost before).
        // Use South direction: these are deferred control messages that the
        // moderator buffered while busy. They originate from the local app (e.g.
        // invite_participant) and therefore carry no identity token. Marking them
        // as South lets the session-controller loop skip identity verification,
        // which only applies to messages arriving from SLIM (North).
        self.common
            .settings
            .tx_session
            .send(SessionMessage::OnMessage {
                message: msg,
                direction: MessageDirection::South,
                ack_tx,
            })
            .await
            .map_err(|_| SessionError::SlimMessageSendFailed)?;

        Ok(())
    }

    async fn join(&mut self, remote: ProtoName, conn: u64) -> Result<(), SessionError> {
        if self.subscribed {
            return Ok(());
        }

        self.subscribed = true;
        self.conn_id = Some(conn);

        // if this is a point to point connection set the remote name so that we
        // can add also the right id to the message destination name
        if self.common.settings.config.session_type == ProtoSessionType::PointToPoint {
            self.common.settings.destination = remote;
        } else {
            // if this is a multicast session we need to subscribe for the channel name
            let destination = self.common.settings.destination.clone();
            self.common.add_subscription(destination, conn).await?;
        }

        // create mls group if needed
        if let Some(mls) = self.mls_state.as_mut() {
            mls.init_moderator().await?;
        }

        // add ourself to the participants
        let mut local_name = self.common.settings.source.clone();
        let settings = self.common.settings.direction.to_participant_settings();
        let participant = Participant::new(local_name.clone(), settings);
        local_name.reset_id();
        self.group_list.insert(local_name, participant);

        Ok(())
    }

    #[allow(dead_code)]
    async fn ack_msl_proposal(&mut self, _msg: &Message) -> Result<(), SessionError> {
        todo!()
    }

    #[allow(dead_code)]
    async fn on_mls_proposal(&mut self, _msg: Message) -> Result<(), SessionError> {
        todo!()
    }

    async fn send_close_signal(&mut self) {
        debug!("Signal session layer to close the session, all tasks are done");

        // notify the session layer
        let res = self
            .common
            .settings
            .tx_to_session_layer
            .send(Ok(SessionMessage::DeleteSession {
                session_id: self.common.settings.id,
            }))
            .await;

        if let Err(e) = res {
            tracing::error!(error = %e.chain(), "an error occurred while signaling session close");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Direction;
    use crate::common::OutboundMessage;
    use crate::session_config::SessionConfig;
    use crate::session_settings::SessionSettings;
    use crate::test_utils::{MockInnerHandler, MockTokenProvider, MockVerifier};
    use slim_datapath::Status;
    use slim_datapath::api::{CommandPayload, ParticipantSettings, ProtoSessionType};
    use tokio::sync::mpsc;

    // --- Test Helpers -----------------------------------------------------------------------

    /// Drives `fut` to completion while automatically resolving any subscription
    /// ACKs that arrive on `rx_slim` (simulating the SLIM datapath ACK response).
    async fn run_with_acks<F, T>(
        fut: F,
        rx_slim: &mut mpsc::Receiver<Result<Message, Status>>,
        sub_mgr: &crate::subscription_manager::SubscriptionManager,
    ) -> T
    where
        F: std::future::Future<Output = T>,
    {
        let mut pinned = Box::pin(fut);
        loop {
            tokio::select! {
                res = &mut pinned => return res,
                msg = rx_slim.recv() => {
                    if let Some(Ok(msg)) = msg && let Some(ack_id) = msg.get_subscription_id() {
                        let ack = Message::builder().build_subscription_ack(ack_id, true, "");
                        sub_mgr.resolve_ack(ack.get_subscription_ack());
                    }
                }
            }
        }
    }

    fn make_name(parts: &[&str; 3]) -> ProtoName {
        ProtoName::from_strings([parts[0], parts[1], parts[2]]).with_id(0)
    }

    fn setup_moderator() -> (
        SessionModerator<MockTokenProvider, MockVerifier, MockInnerHandler>,
        mpsc::Receiver<Result<Message, Status>>,
        mpsc::Receiver<Result<SessionMessage, SessionError>>,
    ) {
        let source = make_name(&["local", "moderator", "v1"]).with_id(100);
        let destination = make_name(&["channel", "name", "v1"]).with_id(NameId::DATA_CHANNEL_ID);
        let control = make_name(&["channel", "name", "v1"]).with_id(NameId::CONTROL_CHANNEL_ID);

        let identity_provider = MockTokenProvider;
        let identity_verifier = MockVerifier;

        let (tx_slim, rx_slim) = mpsc::channel(16);
        let (tx_app, _rx_app) = mpsc::unbounded_channel();
        let (tx_session, _rx_session) = mpsc::channel(16);
        let (tx_session_layer, rx_session_layer) = mpsc::channel(16);

        let subscription_manager =
            crate::subscription_manager::SubscriptionManager::new(tx_slim.clone());

        let config = SessionConfig {
            session_type: ProtoSessionType::Multicast,
            max_retries: Some(3),
            interval: Some(std::time::Duration::from_secs(1)),
            mls_settings: None,
            initiator: true,
            metadata: Default::default(),
        };

        let settings = SessionSettings {
            id: 1,
            source,
            destination,
            control,
            config,
            direction: Direction::Bidirectional,
            slim_tx: tx_slim,
            app_tx: tx_app,
            tx_session,
            tx_to_session_layer: tx_session_layer,
            identity_provider,
            identity_verifier,
            graceful_shutdown_timeout: None,
            subscription_manager,
            service_id: String::new(),
        };

        let inner = MockInnerHandler::new();
        let moderator = SessionModerator::new(inner, settings);

        (moderator, rx_slim, rx_session_layer)
    }

    #[tokio::test]
    async fn test_moderator_new() {
        let (moderator, _rx_slim, _rx_session_layer) = setup_moderator();

        assert!(moderator.tasks_todo.is_empty());
        assert!(moderator.current_task.is_none());
        assert!(moderator.mls_state.is_none());
        assert!(moderator.group_list.is_empty());
        assert!(moderator.postponed_message.is_none());
        assert!(!moderator.subscribed);
    }

    #[tokio::test]
    async fn test_moderator_init() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();

        let result = moderator.init().await;
        assert!(result.is_ok());
        assert!(moderator.mls_state.is_none()); // MLS is disabled in test setup
    }

    #[tokio::test]
    async fn test_moderator_discovery_request_starts_task() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        let source = make_name(&["requester", "app", "v1"]).with_id(300);
        let destination = moderator.common.settings.source.clone();

        let discovery_msg = Message::builder()
            .source(source.clone())
            .destination(destination)
            .identity("")
            .forward_to(0)
            .incoming_conn(12345)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::DiscoveryRequest)
            .session_id(1)
            .message_id(100)
            .payload(CommandPayload::builder().discovery_request().as_content())
            .build_publish()
            .unwrap();

        let result = moderator.on_discovery_request(discovery_msg, None).await;
        assert!(result.is_ok());

        // Should have created an Add task
        assert!(moderator.current_task.is_some());
        assert!(matches!(
            moderator.current_task,
            Some(ModeratorTask::Add(_))
        ));

        // Should have sent a discovery request
        let output = result.unwrap();
        assert!(!output.is_empty());
        let msg = match &output.messages[0] {
            OutboundMessage::ToSlim(m) => m,
            _ => panic!("Expected ToSlim message"),
        };
        assert_eq!(
            msg.get_session_header().session_message_type(),
            ProtoSessionMessageType::DiscoveryRequest
        );
    }

    #[tokio::test]
    async fn test_moderator_discovery_request_when_busy() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        // Set a current task to make moderator busy
        moderator.current_task = Some(ModeratorTask::Add(AddParticipant::new(None)));

        let source = make_name(&["requester", "app", "v1"]).with_id(300);
        let destination = moderator.common.settings.source.clone();

        let discovery_msg = Message::builder()
            .source(source.clone())
            .destination(destination)
            .identity("")
            .forward_to(0)
            .incoming_conn(12345)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::DiscoveryRequest)
            .session_id(1)
            .message_id(100)
            .payload(CommandPayload::builder().discovery_request().as_content())
            .build_publish()
            .unwrap();

        let result = moderator.on_discovery_request(discovery_msg, None).await;
        assert!(result.is_ok());

        // Should have added task to todo list
        assert_eq!(moderator.tasks_todo.len(), 1);
    }

    #[tokio::test]
    async fn test_moderator_join_request_passthrough() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        let source = make_name(&["requester", "app", "v1"]).with_id(300);
        let destination = moderator.common.settings.source.clone();

        let join_msg = Message::builder()
            .source(source.clone())
            .destination(destination.clone())
            .identity("")
            .forward_to(0)
            .incoming_conn(12345)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::JoinRequest)
            .session_id(1)
            .message_id(100)
            .payload(
                CommandPayload::builder()
                    .join_request(Some(3), Some(std::time::Duration::from_secs(1)), None, None)
                    .as_content(),
            )
            .build_publish()
            .unwrap();
        // JoinRequest is no longer handled by the moderator (moved to channel-manager).
        // It should return an error.
        let result = moderator.process_control_message(join_msg, None).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_moderator_application_message_forwarding() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        let source = moderator.common.settings.source.clone();
        let destination = moderator.common.settings.destination.clone();

        let app_msg = Message::builder()
            .source(source)
            .destination(destination)
            .identity("")
            .forward_to(0)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::Msg)
            .session_id(1)
            .message_id(100)
            .application_payload("application/octet-stream", vec![1, 2, 3, 4])
            .build_publish()
            .unwrap();

        let result = moderator
            .on_message(SessionMessage::OnMessage {
                message: app_msg,
                direction: MessageDirection::South,
                ack_tx: None,
            })
            .await;

        assert!(result.is_ok());

        // Should have forwarded to inner handler
        assert_eq!(moderator.inner.get_messages_count().await, 1);
    }

    #[tokio::test]
    async fn test_moderator_add_and_remove_endpoint() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        let endpoint_name = make_name(&["participant", "app", "v1"]).with_id(400);
        let endpoint =
            Participant::new(endpoint_name.clone(), ParticipantSettings::bidirectional());

        // Add endpoint
        let result = moderator.add_endpoint(&endpoint).await;
        assert!(result.is_ok());
        assert_eq!(moderator.inner.get_endpoints_added_count().await, 1);

        // Remove endpoint
        moderator.remove_endpoint(&endpoint_name);
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        assert_eq!(moderator.inner.get_endpoints_removed_count().await, 1);
    }

    #[tokio::test]
    async fn test_moderator_join_sets_subscribed() {
        let (mut moderator, mut rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        assert!(!moderator.subscribed);

        let sub_mgr = moderator.common.settings.subscription_manager.clone();
        let remote = make_name(&["remote", "app", "v1"]).with_id(200);
        let result = run_with_acks(moderator.join(remote, 12345), &mut rx_slim, &sub_mgr).await;

        assert!(result.is_ok());
        assert!(moderator.subscribed);
        assert!(!moderator.group_list.is_empty());
    }

    #[tokio::test]
    async fn test_moderator_join_only_once() {
        let (mut moderator, mut rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        let sub_mgr = moderator.common.settings.subscription_manager.clone();
        let remote = make_name(&["remote", "app", "v1"]).with_id(200);

        // First join — run_with_acks drains and ACKs the subscribe message
        run_with_acks(
            moderator.join(remote.clone(), 12345),
            &mut rx_slim,
            &sub_mgr,
        )
        .await
        .unwrap();

        // Second join should do nothing (already subscribed)
        moderator.join(remote, 12345).await.unwrap();
        let second_subscribe = rx_slim.try_recv();
        assert!(second_subscribe.is_err()); // No message should be sent
    }

    #[tokio::test]
    async fn test_moderator_on_shutdown() {
        let (mut moderator, _rx_slim, mut _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        let result = moderator.on_shutdown().await;
        assert!(result.is_ok());
        assert!(!moderator.subscribed);

        // TODO(msardara): enable the close signal
        // let close_msg = rx_session_layer.try_recv();
        // assert!(close_msg.is_ok());
        // if let Ok(Ok(SessionMessage::DeleteSession { session_id })) = close_msg {
        //     assert_eq!(session_id, 1);
        // } else {
        //     panic!("Expected DeleteSession message");
        // }
    }

    #[tokio::test]
    async fn test_moderator_delete_all_creates_leave_tasks() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        // Add some participants to group list
        moderator.group_list.insert(
            make_name(&["participant1", "app", "v1"]),
            Participant::new(
                make_name(&["participant1", "app", "v1"]).with_id(401),
                ParticipantSettings::bidirectional(),
            ),
        );
        moderator.group_list.insert(
            make_name(&["participant2", "app", "v1"]),
            Participant::new(
                make_name(&["participant2", "app", "v1"]).with_id(402),
                ParticipantSettings::bidirectional(),
            ),
        );
        moderator.group_list.insert(
            make_name(&["participant3", "app", "v1"]),
            Participant::new(
                make_name(&["participant3", "app", "v1"]).with_id(403),
                ParticipantSettings::bidirectional(),
            ),
        );

        let result = moderator.delete_all(None).await;
        assert!(result.is_ok() || result.is_err()); // May error due to missing routes

        assert!(moderator.mls_state.is_none());
    }

    #[tokio::test]
    async fn test_moderator_timer_timeout_for_control_message() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        // Timer timeout for control messages requires sender to have pending messages
        // Without setup, it will fail. Just verify it processes without panicking.
        let result = moderator
            .on_message(SessionMessage::TimerTimeout {
                message_id: 100,
                message_type: ProtoSessionMessageType::DiscoveryRequest,
                name: None,
                timeouts: 1,
            })
            .await;

        // Result may be error if no pending timer exists, which is expected
        assert!(result.is_ok() || result.is_err());
    }

    #[tokio::test]
    async fn test_moderator_timer_timeout_for_app_message() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        let result = moderator
            .on_message(SessionMessage::TimerTimeout {
                message_id: 100,
                message_type: ProtoSessionMessageType::Msg,
                name: None,
                timeouts: 1,
            })
            .await;

        assert!(result.is_ok());
        // Should have forwarded to inner handler
        assert_eq!(moderator.inner.get_messages_count().await, 1);
    }

    #[tokio::test]
    async fn test_moderator_point_to_point_destination_update() {
        let source = make_name(&["local", "app", "v1"]).with_id(100);
        let destination = make_name(&["remote", "app", "v1"]).with_id(200);

        let identity_provider = MockTokenProvider;
        let identity_verifier = MockVerifier;

        let (tx_slim, _rx_slim) = mpsc::channel(16);
        let (tx_app, _rx_app) = mpsc::unbounded_channel();
        let (tx_session, _rx_session) = mpsc::channel(16);
        let (tx_session_layer, _rx_session_layer) = mpsc::channel(16);

        let subscription_manager =
            crate::subscription_manager::SubscriptionManager::new(tx_slim.clone());

        let config = SessionConfig {
            session_type: ProtoSessionType::PointToPoint,
            max_retries: Some(3),
            interval: Some(std::time::Duration::from_secs(1)),
            mls_settings: None,
            initiator: true,
            metadata: Default::default(),
        };

        let settings = SessionSettings {
            id: 1,
            source: source.clone(),
            destination: destination.clone(),
            control: destination.clone(),
            config,
            direction: Direction::Bidirectional,
            slim_tx: tx_slim,
            app_tx: tx_app,
            tx_session,
            tx_to_session_layer: tx_session_layer,
            identity_provider,
            identity_verifier,
            graceful_shutdown_timeout: None,
            subscription_manager,
            service_id: String::new(),
        };

        let inner = MockInnerHandler::new();
        let mut moderator = SessionModerator::new(inner, settings);
        moderator.init().await.unwrap();

        let app_msg = Message::builder()
            .source(source)
            .destination(destination)
            .identity("")
            .forward_to(0)
            .session_type(ProtoSessionType::PointToPoint)
            .session_message_type(ProtoSessionMessageType::Msg)
            .session_id(1)
            .message_id(100)
            .application_payload("application/octet-stream", vec![1, 2, 3])
            .build_publish()
            .unwrap();

        let _original_dest = app_msg.get_dst();

        let result = moderator
            .on_message(SessionMessage::OnMessage {
                message: app_msg,
                direction: MessageDirection::South,
                ack_tx: None,
            })
            .await;

        assert!(result.is_ok());
        // In P2P mode going South, destination should be updated
    }

    #[tokio::test]
    async fn test_moderator_graceful_leave_with_two_participants() {
        // Test graceful leave when exactly 2 participants remain (moderator + participant)
        // The leave request comes directly from the participant (not through controller)
        // with LEAVING_SESSION metadata to signal graceful departure

        // Create moderator with agntcy/ns/moderator naming
        let source = ProtoName::from_strings(["agntcy", "ns", "moderator"]).with_id(100);
        let destination = ProtoName::from_strings(["agntcy", "ns", "chat"]);
        let control =
            ProtoName::from_strings(["agntcy", "ns", "chat"]).with_id(NameId::CONTROL_CHANNEL_ID);

        let identity_provider = MockTokenProvider;
        let identity_verifier = MockVerifier;

        let (tx_slim, mut rx_slim) = mpsc::channel(16);
        let (tx_app, _rx_app) = mpsc::unbounded_channel();
        let (tx_session, _rx_session) = mpsc::channel(16);
        let (tx_session_layer, _rx_session_layer) = mpsc::channel(16);

        let subscription_manager =
            crate::subscription_manager::SubscriptionManager::new(tx_slim.clone());

        let config = SessionConfig {
            session_type: ProtoSessionType::Multicast,
            max_retries: Some(3),
            interval: Some(std::time::Duration::from_secs(1)),
            mls_settings: None,
            initiator: true,
            metadata: Default::default(),
        };

        let settings = SessionSettings {
            id: 1,
            source: source.clone(),
            destination: destination.clone(),
            control,
            config,
            direction: Direction::Bidirectional,
            slim_tx: tx_slim,
            app_tx: tx_app,
            tx_session,
            tx_to_session_layer: tx_session_layer,
            identity_provider,
            identity_verifier,
            graceful_shutdown_timeout: None,
            subscription_manager,
            service_id: String::new(),
        };

        let inner = MockInnerHandler::new();
        let mut moderator = SessionModerator::new(inner, settings);
        moderator.init().await.unwrap();

        // Set up moderator as joined (this adds moderator to group_list)
        let remote = ProtoName::from_strings(["agntcy", "ns", "participant"]).with_id(200);
        let sub_mgr = moderator.common.settings.subscription_manager.clone();
        run_with_acks(
            moderator.join(remote.clone(), 12345),
            &mut rx_slim,
            &sub_mgr,
        )
        .await
        .unwrap();

        // Add one participant to the group (now we have moderator + participant = 2 total)
        // Use the naming convention requested: agntcy/ns/participant
        let mut participant_name = ProtoName::from_strings(["agntcy", "ns", "participant"]);
        let participant = Participant::new(
            participant_name.clone(),
            ParticipantSettings::bidirectional(),
        );
        // Fill in participant settings as needed
        let participant_id = 401u128;
        participant_name.reset_id(); // Remove ID before inserting into group_list
        moderator
            .group_list
            .insert(participant_name.clone(), participant);

        // Verify we have exactly 2 participants (moderator + participant)
        assert_eq!(
            moderator.group_list.len(),
            2,
            "Should have exactly 2 participants"
        );

        // Verify session is not in draining state
        assert_eq!(moderator.processing_state(), ProcessingState::Active);

        // Create a leave request message coming directly from the participant
        // When coming from participant directly (not controller), the source is the participant
        // and the destination is the moderator, with LEAVING_SESSION metadata set
        let participant_with_id = participant_name.clone().with_id(participant_id);
        let mut leave_msg = Message::builder()
            .source(participant_with_id.clone())
            .destination(source.clone())
            .identity("")
            .forward_to(0)
            .incoming_conn(12345)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::LeaveRequest)
            .session_id(1)
            .message_id(100)
            .payload(
                CommandPayload::builder()
                    .leave_request() // Empty payload when coming from participant
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        // Add LEAVING_SESSION metadata to signal graceful departure
        leave_msg.insert_metadata(LEAVING_SESSION.to_string(), TRUE_VAL.to_string());

        let result = moderator.on_disconnection_detected(leave_msg, None).await;

        // The function should succeed now that app channel is open
        assert!(result.is_ok(), "Should succeed with open app channel");

        // Verify that the ParticipantDisconnected error was sent to the output
        let output = result.unwrap();
        let app_error = output
            .messages
            .iter()
            .find(|m| matches!(m, OutboundMessage::ToApp(_)));
        assert!(
            app_error.is_some(),
            "Expected error to be sent to app output"
        );

        if let Some(OutboundMessage::ToApp(Err(SessionError::ParticipantDisconnected(name)))) =
            app_error
        {
            let name_str = name.to_string();
            assert!(
                name_str.contains("agntcy/ns/participant"),
                "Error message should mention the participant, got: {}",
                name_str
            );
        } else {
            panic!("Expected ParticipantDisconnected error");
        }

        // Verify shutdown was triggered when only 2 participants remained
        assert_eq!(
            moderator.processing_state(),
            ProcessingState::Draining,
            "Session should be in draining state"
        );
    }

    #[tokio::test]
    async fn test_moderator_concurrent_leave_requests() {
        // Test that concurrent leave requests are queued and processed sequentially

        // Create moderator with agntcy/ns/moderator naming
        let source = ProtoName::from_strings(["agntcy", "ns", "moderator"]).with_id(100);
        let destination =
            ProtoName::from_strings(["agntcy", "ns", "chat"]).with_id(NameId::DATA_CHANNEL_ID);
        let control =
            ProtoName::from_strings(["agntcy", "ns", "chat"]).with_id(NameId::CONTROL_CHANNEL_ID);

        let identity_provider = MockTokenProvider;
        let identity_verifier = MockVerifier;

        let (tx_slim, mut rx_slim) = mpsc::channel(16);
        let (tx_app, _rx_app) = mpsc::unbounded_channel();
        let (tx_session, _rx_session) = mpsc::channel(16);
        let (tx_session_layer, _rx_session_layer) = mpsc::channel(16);

        let subscription_manager =
            crate::subscription_manager::SubscriptionManager::new(tx_slim.clone());

        let config = SessionConfig {
            session_type: ProtoSessionType::Multicast,
            max_retries: Some(3),
            interval: Some(std::time::Duration::from_secs(1)),
            mls_settings: None,
            initiator: true,
            metadata: Default::default(),
        };

        let settings = SessionSettings {
            id: 1,
            source: source.clone(),
            destination: destination.clone(),
            control: control.clone(),
            config,
            direction: Direction::Bidirectional,
            slim_tx: tx_slim,
            app_tx: tx_app,
            tx_session,
            tx_to_session_layer: tx_session_layer,
            identity_provider,
            identity_verifier,
            graceful_shutdown_timeout: None,
            subscription_manager,
            service_id: String::new(),
        };

        let inner = MockInnerHandler::new();
        let mut moderator = SessionModerator::new(inner, settings);
        moderator.init().await.unwrap();

        // Set up moderator as joined
        let remote = ProtoName::from_strings(["agntcy", "ns", "participant1"]).with_id(200);
        let sub_mgr = moderator.common.settings.subscription_manager.clone();
        run_with_acks(
            moderator.join(remote.clone(), 12345),
            &mut rx_slim,
            &sub_mgr,
        )
        .await
        .unwrap();

        // Add three participants to the group
        let mut participant1_name = ProtoName::from_strings(["agntcy", "ns", "participant1"]);
        let mut participant2_name = ProtoName::from_strings(["agntcy", "ns", "participant2"]);
        let mut participant3_name = ProtoName::from_strings(["agntcy", "ns", "participant3"]);
        let participant1 = Participant::new(
            participant1_name.clone(),
            ParticipantSettings::bidirectional(),
        );
        let participant2 = Participant::new(
            participant2_name.clone(),
            ParticipantSettings::bidirectional(),
        );
        let participant3 = Participant::new(
            participant3_name.clone(),
            ParticipantSettings::bidirectional(),
        );

        participant1_name.reset_id(); // Remove ID before inserting into group_list
        participant2_name.reset_id();
        participant3_name.reset_id();

        moderator
            .group_list
            .insert(participant1_name.clone(), participant1);
        moderator
            .group_list
            .insert(participant2_name.clone(), participant2);
        moderator
            .group_list
            .insert(participant3_name.clone(), participant3);

        // Create first leave request coming directly from participant1 with LEAVING_SESSION metadata
        let participant1_with_id = participant1_name.clone().with_id(401);
        let mut leave_msg1 = Message::builder()
            .source(participant1_with_id.clone())
            .destination(source.clone()) // sent to moderator
            .identity("")
            .forward_to(0)
            .incoming_conn(12345)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::LeaveRequest)
            .session_id(1)
            .message_id(101)
            .payload(
                CommandPayload::builder()
                    .leave_request() // No destination in payload when coming from participant
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        // Add LEAVING_SESSION metadata to signal graceful departure
        leave_msg1.insert_metadata(LEAVING_SESSION.to_string(), TRUE_VAL.to_string());

        // Create second leave request coming directly from participant2 with LEAVING_SESSION metadata
        let participant2_with_id = participant2_name.clone().with_id(402);
        let mut leave_msg2 = Message::builder()
            .source(participant2_with_id.clone())
            .destination(source.clone()) // sent to moderator
            .identity("")
            .forward_to(0)
            .incoming_conn(12345)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::LeaveRequest)
            .session_id(1)
            .message_id(102)
            .payload(
                CommandPayload::builder()
                    .leave_request() // No destination in payload when coming from participant
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        // Add LEAVING_SESSION metadata to signal graceful departure
        leave_msg2.insert_metadata(LEAVING_SESSION.to_string(), TRUE_VAL.to_string());

        // Process first leave request (should start processing immediately)
        // Since it has LEAVING_SESSION metadata, it will be handled by on_disconnection_detected
        let result1 = moderator.on_disconnection_detected(leave_msg1, None).await;
        assert!(result1.is_ok() || result1.is_err());

        // Verify first task was created
        assert!(
            moderator.current_task.is_some(),
            "First leave should create a task"
        );

        // Process second leave request while first is still processing
        // Since it has LEAVING_SESSION metadata, it will be handled by on_disconnection_detected
        let result2 = moderator.on_disconnection_detected(leave_msg2, None).await;
        assert!(result2.is_ok());

        // Verify second task was queued
        assert_eq!(
            moderator.tasks_todo.len(),
            1,
            "Second leave request should be queued while first is processing"
        );

        // Verify the queued task exists and has DISCONNECTION_DETECTED metadata
        if let Some((queued_msg, _)) = moderator.tasks_todo.front() {
            // Verify DISCONNECTION_DETECTED metadata was set (LEAVING_SESSION was replaced)
            assert!(
                queued_msg.contains_metadata(DISCONNECTION_DETECTED),
                "Queued message should have DISCONNECTION_DETECTED metadata"
            );
        } else {
            panic!("Expected queued task for participant2");
        }

        // Clear the messages
        while rx_slim.try_recv().is_ok() {}

        // Verify participant1 was removed from group (first task processed)
        assert!(
            !moderator.group_list.contains_key(&participant1_name),
            "Participant1 should be removed after first leave request"
        );

        // Verify participant2 is still in group (second task queued, not processed yet)
        assert!(
            moderator.group_list.contains_key(&participant2_name),
            "Participant2 should still be in group (task queued, not processed)"
        );
    }

    /// A late or retransmitted GroupAck arriving when `current_task` is `None`
    /// must be silently discarded instead of panicking.
    #[tokio::test]
    async fn test_group_ack_ignored_when_no_current_task() {
        let (mut moderator, _rx_slim, _rx_session_layer) = setup_moderator();
        moderator.init().await.unwrap();

        // Sanity-check: no task is active.
        assert!(moderator.current_task.is_none());

        let source = make_name(&["participant", "app", "v1"]).with_id(300);
        let destination = moderator.common.settings.source.clone();

        // Build a GroupAck whose message_id was never registered with the sender,
        // so `is_still_pending` returns false and the guard is exercised.
        let group_ack = Message::builder()
            .source(source)
            .destination(destination)
            .identity("")
            .forward_to(0)
            .incoming_conn(12345)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAck)
            .session_id(1)
            .message_id(999)
            .payload(CommandPayload::builder().group_ack().as_content())
            .build_publish()
            .unwrap();

        // Must not panic; the stale ACK is discarded and Ok(()) is returned.
        let result = moderator.process_control_message(group_ack, None).await;
        assert!(result.is_ok());

        // State is unchanged.
        assert!(moderator.current_task.is_none());
    }
}