net-mesh 0.23.0

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

use std::collections::HashSet;
use std::time::Instant;

use crate::adapter::net::behavior::metadata::NodeId;
use crate::adapter::net::behavior::placement::{Artifact, PlacementFilter, TieBreakContext};
use crate::adapter::net::compute::daemon::{DaemonHostConfig, MeshDaemon};
use crate::adapter::net::compute::group_coord::{
    GroupCoordinator, GroupError, GroupHealth, MemberInfo,
};
use crate::adapter::net::compute::host::DaemonHost;
use crate::adapter::net::compute::registry::DaemonRegistry;
use crate::adapter::net::compute::scheduler::Scheduler;
use crate::adapter::net::identity::EntityKeypair;
use crate::adapter::net::state::causal::CausalEvent;

use crate::adapter::net::behavior::loadbalance::Strategy;

// ── Configuration ────────────────────────────────────────────────────────────

/// Configuration for a standby group.
#[derive(Debug, Clone)]
pub struct StandbyGroupConfig {
    /// Total number of members (1 active + N-1 standbys).
    pub member_count: u8,
    /// 32-byte seed for deterministic keypair derivation.
    pub group_seed: [u8; 32],
    /// Daemon host configuration.
    pub host_config: DaemonHostConfig,
}

// ── Standby state ────────────────────────────────────────────────────────────

/// Per-member role in the standby group.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemberRole {
    /// Processing events and producing output.
    Active,
    /// Holding a snapshot, ready to promote.
    Standby,
}

/// Per-member extended state.
#[derive(Debug, Clone)]
struct StandbyInfo {
    /// Index in the group.
    index: u8,
    /// Current role.
    role: MemberRole,
    /// Sequence number the standby's snapshot covers through.
    /// For the active, this is the current chain head sequence.
    synced_through: u64,
    /// When the last snapshot sync completed.
    last_sync: Option<Instant>,
    /// Stored keypair secret for recovery.
    keypair_secret: [u8; 32],
}

// ── StandbyGroup ─────────────────────────────────────────────────────────────

/// Active-passive group: one member processes events, others hold snapshots.
///
/// Events are always routed to the active member. Standbys receive periodic
/// snapshots but do no event processing. On failure, the standby with the
/// most recent snapshot promotes and replays buffered events.
pub struct StandbyGroup {
    /// Group identifier.
    group_id: u32,
    /// Configuration.
    config: StandbyGroupConfig,
    /// Index of the current active member.
    active_index: u8,
    /// Per-member state.
    members: Vec<StandbyInfo>,
    /// Events buffered since the last snapshot sync.
    /// On promotion, these replay on the new active.
    buffered_since_sync: Vec<CausalEvent>,
    /// Shared coordination (member tracking, placement).
    coord: GroupCoordinator,
    /// X-1 epoch — bumped on every `promote` /
    /// `promote_with_placement`. The first active starts at 1; each
    /// subsequent promotion increments. The intended use is
    /// cross-node fencing: routed events should carry the issuing
    /// active's `term`, and a receiver that observes a strictly-
    /// higher `term` than its own demotes to standby. The cross-
    /// node wire integration is a separate change; this counter is
    /// the local scaffolding it builds on, and `term()` exposes it
    /// to operators / future wire layers.
    term: u64,
}

impl StandbyGroup {
    /// Create a standby group with one active and N-1 standbys.
    ///
    /// Member 0 is the initial active. All members get deterministic
    /// keypairs from the group seed.
    pub fn spawn<F>(
        config: StandbyGroupConfig,
        daemon_factory: F,
        scheduler: &Scheduler,
        registry: &DaemonRegistry,
    ) -> Result<Self, GroupError>
    where
        F: Fn() -> Box<dyn MeshDaemon>,
    {
        if config.member_count < 2 {
            return Err(GroupError::InvalidConfig(
                "standby group requires at least 2 members".into(),
            ));
        }

        let group_id = {
            use xxhash_rust::xxh3::xxh3_64;
            xxh3_64(&config.group_seed) as u32
        };

        // Use a dummy LB strategy — routing always goes to the active,
        // not through the LoadBalancer.
        let mut coord = GroupCoordinator::new(Strategy::RoundRobin);
        let mut members = Vec::with_capacity(config.member_count as usize);
        let mut used_nodes: HashSet<u64> = HashSet::new();
        let requirements = daemon_factory().requirements();

        for index in 0..config.member_count {
            let keypair = super::replica_group::derive_replica_keypair(&config.group_seed, index);
            let origin_hash = keypair.origin_hash();
            let entity_id_bytes: NodeId = *keypair.entity_id().as_bytes();
            let keypair_secret = *keypair.secret_bytes();

            let placement =
                GroupCoordinator::place_with_spread(scheduler, &requirements, &used_nodes)?;
            let node_id = placement.node_id;
            used_nodes.insert(node_id);

            let daemon = daemon_factory();
            let host = DaemonHost::new(daemon, keypair, config.host_config.clone());
            registry.register(host)?;

            coord.add_member(MemberInfo {
                index,
                origin_hash,
                node_id,
                entity_id_bytes,
                healthy: true,
            });

            let role = if index == 0 {
                MemberRole::Active
            } else {
                MemberRole::Standby
            };

            members.push(StandbyInfo {
                index,
                role,
                synced_through: 0,
                last_sync: None,
                keypair_secret,
            });
        }

        Ok(Self {
            group_id,
            config,
            active_index: 0,
            members,
            buffered_since_sync: Vec::new(),
            coord,
            // Spawn-time active is term 1; every subsequent
            // `promote` increments.
            term: 1,
        })
    }

    /// Current epoch counter. Bumped on every successful
    /// `promote` / `promote_with_placement`. Cross-node fencing
    /// (X-1) consumes this to reject events from a stale active
    /// after a partition heal; the wire integration is a
    /// separate change.
    pub fn term(&self) -> u64 {
        self.term
    }

    /// Get the origin_hash of the active member.
    ///
    /// Use this with `DaemonRegistry::deliver()` to send events to the
    /// active daemon. Only the active processes events.
    pub fn active_origin(&self) -> u64 {
        self.coord.members()[self.active_index as usize].origin_hash
    }

    /// Deliver an event to the active member and buffer it for replay.
    ///
    /// The caller should pass the outputs from `DaemonRegistry::deliver()`
    /// back. The event is buffered for standby promotion replay.
    pub fn on_event_delivered(&mut self, event: CausalEvent) {
        self.buffered_since_sync.push(event);
    }

    /// Sync state from the active to all standbys.
    ///
    /// Takes a snapshot of the active daemon and pushes it onto each
    /// standby via `registry.restore_from_snapshot`, so each standby's
    /// state matches the active's at snapshot time. Clears the event
    /// buffer — standbys are now caught up to this point.
    ///
    /// Previously this method only updated `synced_through` /
    /// `last_sync` bookkeeping but never copied the snapshot bytes
    /// onto the standby daemons. On `promote()`, the picked standby
    /// was still in its initial default-constructed state, and
    /// `promote()` only replays `buffered_since_sync` (just cleared
    /// at the previous sync). Everything before the most recent sync
    /// was silently lost — a critical correctness bug for any
    /// stateful daemon that needs failover.
    ///
    /// Returns the snapshot's `through_seq` so the caller knows what's synced.
    ///
    /// Partial-failure semantics: if `restore_from_snapshot` fails
    /// for one standby mid-loop, the bookkeeping for the standbys
    /// that DID succeed is recorded before propagating the error.
    /// Pre-fix, an early `?` skipped the entire bookkeeping pass —
    /// previously-restored standbys retained the OLD `synced_through`
    /// value, but their daemon state had already been rewritten to
    /// the new snapshot. A subsequent `promote()` running through
    /// `max_by_key(synced_through)` picked the (stale-by-tracking)
    /// standby and replayed `buffered_since_sync` on top, double-
    /// executing every event between the previous sync and the
    /// just-failed sync's `through_seq`.
    ///
    /// Buffer-clearing also moves: only clear `buffered_since_sync`
    /// if EVERY standby succeeded. A partial sync must keep the
    /// buffer so the failed standby can catch up next cycle.
    pub fn sync_standbys(&mut self, registry: &DaemonRegistry) -> Result<u64, GroupError> {
        let active_origin = self.active_origin();

        // Take snapshot from active
        let snapshot = registry
            .snapshot(active_origin)
            .map_err(|e| GroupError::RegistryFailed(e.to_string()))?
            .ok_or_else(|| GroupError::RegistryFailed("active daemon is stateless".into()))?;

        let through_seq = snapshot.through_seq;
        let now = Instant::now();

        // Push snapshot state onto every standby and record per-
        // standby success. Iterate in two layers: collect origins
        // first (avoids holding a borrow on `self.members` while
        // calling out to the registry), then track success
        // membership so we can update bookkeeping precisely below.
        let standbys: Vec<(usize, u64)> = self
            .members
            .iter()
            .enumerate()
            .filter(|(_, m)| m.role == MemberRole::Standby)
            .map(|(i, m)| (i, self.coord.members()[m.index as usize].origin_hash))
            .collect();
        let total_standbys = standbys.len();
        let mut succeeded: Vec<usize> = Vec::with_capacity(standbys.len());
        let mut first_err: Option<GroupError> = None;
        for (member_idx, standby_origin) in standbys {
            match registry.restore_from_snapshot(standby_origin, &snapshot) {
                Ok(()) => succeeded.push(member_idx),
                Err(e) => {
                    first_err = Some(GroupError::RegistryFailed(e.to_string()));
                    break;
                }
            }
        }

        // Update bookkeeping for each standby that successfully
        // restored. Standbys that failed (or that we never reached
        // after a mid-loop break) keep their previous tracking.
        for &i in &succeeded {
            let member = &mut self.members[i];
            member.synced_through = through_seq;
            member.last_sync = Some(now);
        }
        // The active member's `synced_through` advances to the new
        // floor regardless of standby success — the snapshot was
        // taken from the active's own state, so its tracking
        // matches reality even if no standby received it.
        for member in &mut self.members {
            if member.role == MemberRole::Active {
                member.synced_through = through_seq;
            }
        }

        if let Some(err) = first_err {
            // Partial failure. Don't clear `buffered_since_sync`:
            // the failed standby still owes the buffer's events
            // and the next sync cycle will retry from this point.
            return Err(err);
        }

        // All standbys synced — safe to drop the event buffer.
        if succeeded.len() == total_standbys {
            self.buffered_since_sync.clear();
        }

        Ok(through_seq)
    }

    /// Promote a standby to active after the current active fails.
    ///
    /// Picks the standby with the highest `synced_through` (most recent
    /// snapshot). Replays buffered events on the new active. Returns the
    /// new active's origin_hash.
    pub fn promote(
        &mut self,
        _daemon_factory: impl Fn() -> Box<dyn MeshDaemon>,
        registry: &DaemonRegistry,
        _scheduler: &Scheduler,
    ) -> Result<u64, GroupError> {
        let old_active = self.active_index;

        // The search for a replacement runs FIRST; only if it
        // succeeds do we mutate the old active's state. If we instead
        // demoted `old_active` first and the search then returned
        // `NoHealthyMember`, the function would exit with `Err` but
        // leave `self.active_index` pointing at the now-unhealthy,
        // now-`Standby`-roled `old_active`. A subsequent
        // `on_node_recovery` for that member only marks it healthy —
        // it doesn't restore the `Active` role — so the group would
        // be silently demoted forever.
        // Prefer standbys that have completed at least one
        // `sync_standbys` cycle. A replaced-then-re-placed standby
        // has `synced_through = 0` and `last_sync = None` until the
        // next sync — and `max_by_key(synced_through)` could
        // otherwise pick that fresh-zero standby over a previously-
        // synced sibling whose `synced_through` was also reset to 0
        // by some prior path. Promoting the never-synced standby
        // means the new active has zero pre-buffer state; replaying
        // `buffered_since_sync` only covers events SINCE the last
        // sync, so anything before that sync is permanently lost.
        //
        // Fall back to any healthy standby when NO candidate has
        // ever synced (legitimate during the first promote-before-
        // sync window): the buffer in that case contains every
        // event since spawn, so a fresh standby can correctly
        // catch up via `buffered_since_sync` replay.
        let candidates: Vec<&StandbyInfo> = self
            .members
            .iter()
            .filter(|m| m.role == MemberRole::Standby && m.index != old_active)
            .filter(|m| self.coord.members()[m.index as usize].healthy)
            .collect();
        let synced_pick = candidates
            .iter()
            .filter(|m| m.last_sync.is_some())
            .max_by_key(|m| m.synced_through)
            .map(|m| m.index);
        let best_standby = match synced_pick {
            Some(idx) => idx,
            None => candidates
                .iter()
                .max_by_key(|m| m.synced_through)
                .map(|m| m.index)
                .ok_or(GroupError::NoHealthyMember)?,
        };

        // Now safe to mutate — search succeeded, promotion will
        // complete.
        self.coord.mark_unhealthy(old_active);
        self.members[old_active as usize].role = MemberRole::Standby;

        // Promote
        self.active_index = best_standby;
        self.members[best_standby as usize].role = MemberRole::Active;
        // X-1 epoch bump. Every promotion advances the term; a
        // future wire-fencing layer compares an incoming event's
        // term against the receiver's recorded term and rejects
        // strictly-lower-term events (stale active still
        // emitting after partition heal) or self-demotes on a
        // strictly-higher-term observation. `saturating_add`
        // pins the post-u64::MAX case at the cap rather than
        // wrapping — an astronomical precondition, but cheap to
        // bound.
        self.term = self.term.saturating_add(1);

        let new_active_origin = self.coord.members()[best_standby as usize].origin_hash;

        // Replay buffered events on the new active — but only the
        // ones strictly above the new active's last `synced_through`.
        //
        // `buffered_since_sync` is preserved verbatim across partial
        // sync rounds so a failed standby can catch up next cycle
        // (line ~286); succeeded standbys have their
        // `synced_through` advanced to the snapshot's through_seq
        // but the buffer keeps its older entries. Without the
        // filter, a succeeded-then-promoted standby has already
        // applied events in `[old_synced_through, new_synced_through]`
        // via the snapshot — replaying the full buffer here re-
        // invokes the daemon's `on_event` for each, doubling
        // counters, re-issuing idempotency keys, and re-firing
        // side effects. The new sequence-aware filter restricts
        // the replay to `event.link.sequence > synced_through` so
        // every event lands on the promoted daemon exactly once
        // (covered by the snapshot's pre-state, then extended by
        // the post-snapshot tail). Per-event check is cheap; the
        // buffer is bounded by the sync cadence.
        let synced_through = self.members[best_standby as usize].synced_through;
        for event in &self.buffered_since_sync {
            if event.link.sequence > synced_through {
                let _ = registry.deliver(new_active_origin, event);
            }
        }
        self.buffered_since_sync.clear();

        // Update synced_through for the new active
        if let Ok(Some(snapshot)) = registry.snapshot(new_active_origin) {
            self.members[best_standby as usize].synced_through = snapshot.through_seq;
        }

        Ok(new_active_origin)
    }

    /// Handle failure of a node.
    ///
    /// If the active's node failed, triggers promotion.
    /// If a standby's node failed, attempts re-placement.
    pub fn on_node_failure<F>(
        &mut self,
        failed_node_id: u64,
        daemon_factory: F,
        scheduler: &Scheduler,
        registry: &DaemonRegistry,
    ) -> Result<Option<u64>, GroupError>
    where
        F: Fn() -> Box<dyn MeshDaemon>,
    {
        let affected = self.coord.members_on_node(failed_node_id);
        let active_failed = affected.contains(&self.active_index);

        // Mark all affected as unhealthy
        for &index in &affected {
            self.coord.mark_unhealthy(index);
        }

        // If active failed, promote
        let new_active = if active_failed {
            Some(self.promote(&daemon_factory, registry, scheduler)?)
        } else {
            None
        };

        // Re-place failed standbys
        let requirements = daemon_factory().requirements();
        let mut exclude: HashSet<u64> = HashSet::new();
        exclude.insert(failed_node_id);

        for &index in &affected {
            if index == self.active_index {
                continue; // already promoted or is the new active
            }

            // Place BEFORE touching the registry so a placement
            // failure leaves the slot recoverable. Mirror of
            // replica_group / fork_group fixes.
            let old_origin_hash = self.coord.members()[index as usize].origin_hash;

            let keypair = EntityKeypair::from_bytes(self.members[index as usize].keypair_secret);
            let entity_id_bytes: NodeId = *keypair.entity_id().as_bytes();

            let placement =
                match GroupCoordinator::place_with_spread(scheduler, &requirements, &exclude) {
                    Ok(p) => p,
                    Err(e) => {
                        tracing::warn!(
                            index,
                            error = %e,
                            "StandbyGroup::on_node_failure: place_with_spread failed; \
                             slot left registered for later recovery (#7)"
                        );
                        continue;
                    }
                };

            // Standby keypairs are stored per-member; the new
            // origin_hash matches the old. Atomic upsert via
            // `replace` — a single map operation that never leaves
            // the slot empty (the older `unregister` → `register`
            // sequence had a small window where the second step
            // could fail and orphan the slot).
            let _ = old_origin_hash;

            let daemon = daemon_factory();
            let host = DaemonHost::new(daemon, keypair, self.config.host_config.clone());
            registry.replace(host);

            self.coord
                .update_member_placement(index, placement.node_id, entity_id_bytes);
            self.members[index as usize].synced_through = 0;
            self.members[index as usize].last_sync = None;
            exclude.insert(placement.node_id);
        }

        Ok(new_active)
    }

    /// Phase G slice 7 — `spawn` with score-based placement. Routes
    /// every member's placement decision through
    /// [`GroupCoordinator::place_member`].
    ///
    /// Member 0 is still the initial active; remaining members are
    /// standbys. Spread invariant preserved (each member on a
    /// distinct node).
    pub fn spawn_with_placement<F>(
        config: StandbyGroupConfig,
        daemon_factory: F,
        scheduler: &Scheduler,
        registry: &DaemonRegistry,
        placement: &dyn PlacementFilter,
        tie_break: &TieBreakContext<'_>,
    ) -> Result<Self, GroupError>
    where
        F: Fn() -> Box<dyn MeshDaemon>,
    {
        if config.member_count < 2 {
            return Err(GroupError::InvalidConfig(
                "standby group requires at least 2 members".into(),
            ));
        }

        let group_id = {
            use xxhash_rust::xxh3::xxh3_64;
            xxh3_64(&config.group_seed) as u32
        };

        let mut coord = GroupCoordinator::new(Strategy::RoundRobin);
        let mut members = Vec::with_capacity(config.member_count as usize);
        let mut used_nodes: HashSet<u64> = HashSet::new();

        // Capture the daemon's capability surface once.
        let prototype = daemon_factory();
        let requirements = prototype.requirements();
        let required = prototype.required_capabilities();
        let optional = prototype.optional_capabilities();
        drop(prototype);

        for index in 0..config.member_count {
            let keypair = super::replica_group::derive_replica_keypair(&config.group_seed, index);
            let origin_hash = keypair.origin_hash();
            let entity_id_bytes: NodeId = *keypair.entity_id().as_bytes();
            let keypair_secret = *keypair.secret_bytes();

            let artifact = Artifact::Daemon {
                daemon_id: entity_id_bytes,
                required: &required,
                optional: &optional,
            };

            let decision = GroupCoordinator::place_member(
                scheduler,
                &artifact,
                &requirements,
                &used_nodes,
                placement,
                tie_break,
            )?;
            let node_id = decision.node_id;
            used_nodes.insert(node_id);

            let daemon = daemon_factory();
            let host = DaemonHost::new(daemon, keypair, config.host_config.clone());
            registry.register(host)?;

            coord.add_member(MemberInfo {
                index,
                origin_hash,
                node_id,
                entity_id_bytes,
                healthy: true,
            });

            let role = if index == 0 {
                MemberRole::Active
            } else {
                MemberRole::Standby
            };

            members.push(StandbyInfo {
                index,
                role,
                synced_through: 0,
                last_sync: None,
                keypair_secret,
            });
        }

        Ok(Self {
            group_id,
            config,
            active_index: 0,
            members,
            buffered_since_sync: Vec::new(),
            coord,
            term: 1,
        })
    }

    /// Phase G slice 7 — `promote` with score-based standby
    /// selection.
    ///
    /// Preserves v1's recovery-correctness contract: standbys with
    /// the most recent sync (`last_sync.is_some()` AND highest
    /// `synced_through`) are still preferred. The placement filter
    /// (with the LOCKED §7 tie-breaker) breaks ties AMONG
    /// equivalently-fresh standbys — does NOT override data
    /// freshness, because promoting a less-fresh standby would
    /// mean replaying the `buffered_since_sync` window over a
    /// stale base and silently losing pre-buffer state.
    ///
    /// Falls back to all healthy standbys (placement-scored) when
    /// no candidate has ever synced (legitimate during the first
    /// promote-before-sync window — `buffered_since_sync` covers
    /// every event since spawn in that case).
    ///
    /// Returns the new active's origin_hash, same as
    /// [`Self::promote`].
    pub fn promote_with_placement<F>(
        &mut self,
        daemon_factory: F,
        registry: &DaemonRegistry,
        scheduler: &Scheduler,
        placement: &dyn PlacementFilter,
        tie_break: &TieBreakContext<'_>,
    ) -> Result<u64, GroupError>
    where
        F: Fn() -> Box<dyn MeshDaemon>,
    {
        let old_active = self.active_index;

        // Search runs FIRST; only on success do we mutate state —
        // same half-mutation safety as v1 `promote`.
        let healthy_standbys: Vec<u8> = self
            .members
            .iter()
            .filter(|m| m.role == MemberRole::Standby && m.index != old_active)
            .filter(|m| self.coord.members()[m.index as usize].healthy)
            .map(|m| m.index)
            .collect();

        if healthy_standbys.is_empty() {
            return Err(GroupError::NoHealthyMember);
        }

        // Synced_through pre-filter: among synced standbys, keep
        // those at the maximum `synced_through`. Fall back to all
        // healthy standbys when no candidate has ever synced.
        let max_synced = healthy_standbys
            .iter()
            .filter(|&&idx| self.members[idx as usize].last_sync.is_some())
            .map(|&idx| self.members[idx as usize].synced_through)
            .max();

        let roster: Vec<u8> = match max_synced {
            Some(max) => healthy_standbys
                .iter()
                .copied()
                .filter(|&idx| {
                    self.members[idx as usize].last_sync.is_some()
                        && self.members[idx as usize].synced_through == max
                })
                .collect(),
            None => healthy_standbys.clone(),
        };

        // Score the roster via placement filter; LOCKED §7 breaks
        // ties (RTT → free-resource → lex-NodeId).
        let prototype = daemon_factory();
        let required = prototype.required_capabilities();
        let optional = prototype.optional_capabilities();
        drop(prototype);

        let artifact = Artifact::Daemon {
            daemon_id: [0u8; 32],
            required: &required,
            optional: &optional,
        };

        let candidate_node_ids: Vec<u64> = roster
            .iter()
            .map(|&idx| self.coord.members()[idx as usize].node_id)
            .collect();

        let chosen_node = scheduler
            .select_promotion_target(candidate_node_ids, &artifact, placement, tie_break)
            .ok_or(GroupError::NoHealthyMember)?;

        // Map node_id back to member index — spread invariant
        // guarantees uniqueness, but use `find` defensively.
        let best_standby = roster
            .iter()
            .copied()
            .find(|&idx| self.coord.members()[idx as usize].node_id == chosen_node)
            .ok_or(GroupError::NoHealthyMember)?;

        // Mutation flow mirrors v1.
        self.coord.mark_unhealthy(old_active);
        self.members[old_active as usize].role = MemberRole::Standby;

        self.active_index = best_standby;
        self.members[best_standby as usize].role = MemberRole::Active;
        // X-1 epoch bump — see promote() above for rationale.
        self.term = self.term.saturating_add(1);

        let new_active_origin = self.coord.members()[best_standby as usize].origin_hash;

        // Filter the replay to events strictly past the new
        // active's `synced_through`. See the equivalent block in
        // `promote` for the partial-sync corruption it prevents:
        // a succeeded-then-promoted standby has already applied
        // events up to `synced_through` via the snapshot, and
        // replaying the full buffer here would double every
        // counter / idempotency key / side effect inside that
        // already-applied range.
        let synced_through = self.members[best_standby as usize].synced_through;
        for event in &self.buffered_since_sync {
            if event.link.sequence > synced_through {
                let _ = registry.deliver(new_active_origin, event);
            }
        }
        self.buffered_since_sync.clear();

        if let Ok(Some(snapshot)) = registry.snapshot(new_active_origin) {
            self.members[best_standby as usize].synced_through = snapshot.through_seq;
        }

        Ok(new_active_origin)
    }

    /// Phase G slice 7 — `on_node_failure` with score-based
    /// placement. Active failure triggers
    /// [`Self::promote_with_placement`]; standby re-placement
    /// routes through [`GroupCoordinator::place_member`].
    pub fn on_node_failure_with_placement<F>(
        &mut self,
        failed_node_id: u64,
        daemon_factory: F,
        scheduler: &Scheduler,
        registry: &DaemonRegistry,
        placement: &dyn PlacementFilter,
        tie_break: &TieBreakContext<'_>,
    ) -> Result<Option<u64>, GroupError>
    where
        F: Fn() -> Box<dyn MeshDaemon>,
    {
        let affected = self.coord.members_on_node(failed_node_id);
        let active_failed = affected.contains(&self.active_index);

        for &index in &affected {
            self.coord.mark_unhealthy(index);
        }

        let new_active = if active_failed {
            Some(self.promote_with_placement(
                &daemon_factory,
                registry,
                scheduler,
                placement,
                tie_break,
            )?)
        } else {
            None
        };

        let prototype = daemon_factory();
        let requirements = prototype.requirements();
        let required = prototype.required_capabilities();
        let optional = prototype.optional_capabilities();
        drop(prototype);

        let mut exclude: HashSet<u64> = HashSet::new();
        exclude.insert(failed_node_id);

        for &index in &affected {
            if index == self.active_index {
                continue;
            }

            let keypair = EntityKeypair::from_bytes(self.members[index as usize].keypair_secret);
            let entity_id_bytes: NodeId = *keypair.entity_id().as_bytes();

            let artifact = Artifact::Daemon {
                daemon_id: entity_id_bytes,
                required: &required,
                optional: &optional,
            };

            let decision = match GroupCoordinator::place_member(
                scheduler,
                &artifact,
                &requirements,
                &exclude,
                placement,
                tie_break,
            ) {
                Ok(p) => p,
                Err(e) => {
                    tracing::warn!(
                        index,
                        error = %e,
                        "StandbyGroup::on_node_failure_with_placement: place_member failed; \
                         slot left registered for later recovery"
                    );
                    continue;
                }
            };

            let daemon = daemon_factory();
            let host = DaemonHost::new(daemon, keypair, self.config.host_config.clone());
            registry.replace(host);

            self.coord
                .update_member_placement(index, decision.node_id, entity_id_bytes);
            self.members[index as usize].synced_through = 0;
            self.members[index as usize].last_sync = None;
            exclude.insert(decision.node_id);
        }

        Ok(new_active)
    }

    /// Handle recovery of a node.
    ///
    /// Only re-marks members healthy if they are still registered in the
    /// `DaemonRegistry`. Prevents routing to origin_hashes that were
    /// unregistered during failure and never replaced.
    pub fn on_node_recovery(&mut self, recovered_node_id: u64, registry: &DaemonRegistry) {
        self.coord.on_node_recovery(recovered_node_id, registry);
    }

    /// Retry placement against the current healthy node pool for
    /// every member slot currently marked unhealthy. Caps at
    /// `MAX_RECOVERIES_PER_TICK` so a pathological "every slot
    /// unhealthy" state makes progress without wedging the caller.
    /// Returns the slot indices that were successfully placed.
    ///
    /// Skips `active_index` even when it is marked unhealthy: a
    /// blank `DaemonHost::new` here would `registry.replace` the
    /// live active and wipe its committed state. An unhealthy
    /// active requires `promote` (which transfers the latest
    /// standby snapshot + replays buffered events), not slot
    /// re-placement.
    fn try_recover_inner<F>(
        &mut self,
        scheduler: &Scheduler,
        registry: &DaemonRegistry,
        daemon_factory: F,
    ) -> Vec<u8>
    where
        F: Fn() -> Box<dyn MeshDaemon>,
    {
        const MAX_RECOVERIES_PER_TICK: usize = 4;
        let active_index = self.active_index;
        let unhealthy: Vec<u8> = self
            .coord
            .members()
            .iter()
            .filter(|m| !m.healthy && m.index != active_index)
            .map(|m| m.index)
            .take(MAX_RECOVERIES_PER_TICK)
            .collect();
        if unhealthy.is_empty() {
            return Vec::new();
        }

        let requirements = daemon_factory().requirements();
        let mut exclude: HashSet<u64> = self
            .coord
            .members()
            .iter()
            .filter(|m| m.healthy)
            .map(|m| m.node_id)
            .collect();
        let mut recovered = Vec::with_capacity(unhealthy.len());

        for index in unhealthy {
            let keypair = EntityKeypair::from_bytes(self.members[index as usize].keypair_secret);
            let entity_id_bytes: NodeId = *keypair.entity_id().as_bytes();

            let placement =
                match GroupCoordinator::place_with_spread(scheduler, &requirements, &exclude) {
                    Ok(p) => p,
                    Err(e) => {
                        tracing::trace!(
                            index,
                            error = %e,
                            "StandbyGroup::try_recover: place_with_spread still failing; \
                             slot remains unhealthy for next tick"
                        );
                        continue;
                    }
                };

            let daemon = daemon_factory();
            let host = DaemonHost::new(daemon, keypair, self.config.host_config.clone());
            registry.replace(host);

            self.coord
                .update_member_placement(index, placement.node_id, entity_id_bytes);
            self.members[index as usize].synced_through = 0;
            self.members[index as usize].last_sync = None;
            exclude.insert(placement.node_id);
            recovered.push(index);
        }

        // X-1 epoch bump on any successful slot re-placement, matching
        // `ForkGroup::try_recover_inner` / `ReplicaGroup::try_recover_inner`.
        // A standby placed on a new node here is a membership change;
        // peers that still believe the slot lives on its old node must
        // observe a higher term so they can refresh routing before the
        // fenced wire-layer rejects their stale-term snapshot syncs.
        // No-op if every placement attempt failed.
        if !recovered.is_empty() {
            self.term = self.term.saturating_add(1);
        }
        recovered
    }

    /// Aggregate health.
    pub fn health(&self) -> GroupHealth {
        self.coord.health()
    }

    /// Whether the active member is healthy.
    pub fn active_healthy(&self) -> bool {
        self.coord.members()[self.active_index as usize].healthy
    }

    /// Get the active member's index.
    pub fn active_index(&self) -> u8 {
        self.active_index
    }

    /// Get the role of a member.
    pub fn member_role(&self, index: u8) -> Option<MemberRole> {
        self.members.get(index as usize).map(|m| m.role)
    }

    /// Get the sync sequence for a member.
    pub fn synced_through(&self, index: u8) -> Option<u64> {
        self.members.get(index as usize).map(|m| m.synced_through)
    }

    /// Number of buffered events since last sync.
    pub fn buffered_event_count(&self) -> usize {
        self.buffered_since_sync.len()
    }

    /// Get the group ID.
    pub fn group_id(&self) -> u32 {
        self.group_id
    }

    /// Get all member info from the coordinator.
    pub fn members(&self) -> &[MemberInfo] {
        self.coord.members()
    }

    /// Total member count.
    pub fn member_count(&self) -> u8 {
        self.coord.member_count()
    }

    /// Number of standbys (total - 1 active).
    pub fn standby_count(&self) -> u8 {
        self.coord.member_count().saturating_sub(1)
    }
}

impl crate::adapter::net::compute::UnhealthySlotRecovery for StandbyGroup {
    fn has_unhealthy_slots(&self) -> bool {
        // Only standby slots are recoverable here; an unhealthy
        // active must go through `promote`, not slot re-placement
        // (see `try_recover_inner`). Returning `true` for an
        // unhealthy-active-only state would make the recovery
        // tick fire repeatedly with nothing to do, since the
        // active slot is skipped inside `try_recover`.
        let active_index = self.active_index;
        self.coord
            .members()
            .iter()
            .any(|m| !m.healthy && m.index != active_index)
    }

    fn try_recover(
        &mut self,
        scheduler: &Scheduler,
        registry: &DaemonRegistry,
        daemon_factory: &dyn Fn() -> Box<dyn MeshDaemon>,
    ) -> Vec<u8> {
        self.try_recover_inner(scheduler, registry, daemon_factory)
    }
}

impl std::fmt::Debug for StandbyGroup {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StandbyGroup")
            .field("group_id", &format!("{:#x}", self.group_id))
            .field("active_index", &self.active_index)
            .field("members", &self.coord.member_count())
            .field("buffered_events", &self.buffered_since_sync.len())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapter::net::behavior::capability::{
        CapabilityAnnouncement, CapabilityFilter, CapabilitySet,
    };
    use crate::adapter::net::compute::DaemonError;
    use crate::adapter::net::state::causal::CausalLink;
    use bytes::Bytes;
    use std::sync::Arc;

    struct StatefulDaemon {
        value: u64,
    }

    impl StatefulDaemon {
        fn new() -> Self {
            Self { value: 0 }
        }
    }

    impl MeshDaemon for StatefulDaemon {
        fn name(&self) -> &str {
            "stateful"
        }
        fn requirements(&self) -> CapabilityFilter {
            CapabilityFilter::default()
        }
        fn process(&mut self, _event: &CausalEvent) -> Result<Vec<Bytes>, DaemonError> {
            self.value += 1;
            Ok(vec![Bytes::from(self.value.to_le_bytes().to_vec())])
        }
        fn snapshot(&self) -> Option<Bytes> {
            Some(Bytes::from(self.value.to_le_bytes().to_vec()))
        }
        fn restore(&mut self, state: Bytes) -> Result<(), DaemonError> {
            if state.len() != 8 {
                return Err(DaemonError::RestoreFailed("bad size".into()));
            }
            self.value = u64::from_le_bytes(state[..8].try_into().unwrap());
            Ok(())
        }
    }

    fn make_event(seq: u64) -> CausalEvent {
        CausalEvent {
            link: CausalLink {
                origin_hash: 0xFFFF,
                horizon_encoded: 0,
                sequence: seq,
                parent_hash: 0,
            },
            payload: Bytes::from(format!("event-{}", seq)),
            received_at: seq * 1000,
        }
    }

    fn make_scheduler() -> Scheduler {
        use crate::adapter::net::behavior::fold::{capability_bridge, CapabilityFold, Fold};
        let fold: Arc<Fold<CapabilityFold>> =
            Arc::new(Fold::with_sweep_interval(std::time::Duration::ZERO));
        // Use a local_node_id NOT in the index so placement spreads
        // across indexed nodes instead of always picking local.
        let eid = crate::adapter::net::identity::EntityId::from_bytes([0u8; 32]);
        for node_id in [0x1111u64, 0x2222, 0x3333] {
            capability_bridge::apply_legacy_announcement(
                &fold,
                CapabilityAnnouncement::new(node_id, eid.clone(), 1, CapabilitySet::new()),
            )
            .expect("apply legacy announcement in fixture");
        }
        Scheduler::new(fold, 0xFFFF, CapabilitySet::new())
    }

    fn test_config(n: u8) -> StandbyGroupConfig {
        StandbyGroupConfig {
            member_count: n,
            group_seed: [55u8; 32],
            host_config: DaemonHostConfig::default(),
        }
    }

    #[test]
    fn test_spawn_standby_group() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        assert_eq!(group.member_count(), 3);
        assert_eq!(group.standby_count(), 2);
        assert_eq!(group.active_index(), 0);
        assert_eq!(group.member_role(0), Some(MemberRole::Active));
        assert_eq!(group.member_role(1), Some(MemberRole::Standby));
        assert_eq!(group.member_role(2), Some(MemberRole::Standby));
        assert_eq!(group.health(), GroupHealth::Healthy);
        assert_eq!(reg.count(), 3);
    }

    #[test]
    fn test_minimum_two_members() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let err = StandbyGroup::spawn(
            test_config(1),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap_err();
        assert_eq!(
            err,
            GroupError::InvalidConfig("standby group requires at least 2 members".into())
        );
    }

    #[test]
    fn test_active_origin_delivers() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let active = group.active_origin();

        // Deliver events to active
        for seq in 1..=5 {
            let event = make_event(seq);
            let outputs = reg.deliver(active, &event).unwrap();
            assert_eq!(outputs.len(), 1);
            let val = u64::from_le_bytes(outputs[0].payload[..8].try_into().unwrap());
            assert_eq!(val, seq);
            group.on_event_delivered(event);
        }

        assert_eq!(group.buffered_event_count(), 5);
    }

    #[test]
    fn test_sync_standbys() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let active = group.active_origin();

        // Process some events
        for seq in 1..=10 {
            let event = make_event(seq);
            reg.deliver(active, &event).unwrap();
            group.on_event_delivered(event);
        }

        // Sync
        let through = group.sync_standbys(&reg).unwrap();
        assert_eq!(through, 10);
        assert_eq!(group.buffered_event_count(), 0);
        assert_eq!(group.synced_through(1), Some(10));
        assert_eq!(group.synced_through(2), Some(10));
    }

    /// Regression: a freshly-replaced standby has `synced_through = 0`
    /// and `last_sync = None` until the next sync_standbys cycle.
    /// Pre-fix, `promote()`'s `max_by_key(synced_through)` could
    /// pick that fresh standby over a previously-synced sibling
    /// whose `synced_through` was also reset to 0 by some earlier
    /// path — promoting a daemon with zero pre-buffer state and
    /// silently losing every event before the most recent sync.
    /// Fix prefers candidates with `last_sync.is_some()`; falls back
    /// to any healthy candidate only when nobody has ever synced.
    #[test]
    fn promote_prefers_synced_standby_over_freshly_replaced_one() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let active = group.active_origin();

        // Drive some events through, then sync — both standbys
        // now have last_sync = Some(_) and synced_through > 0.
        for seq in 1..=10 {
            let event = make_event(seq);
            reg.deliver(active, &event).unwrap();
            group.on_event_delivered(event);
        }
        group.sync_standbys(&reg).unwrap();

        // Identify the two standbys by index (everything that's
        // not the active).
        let standby_indices: Vec<u8> = (0..3u8).filter(|&i| i != group.active_index).collect();
        assert_eq!(standby_indices.len(), 2);

        // Force one of the standbys back to "freshly replaced"
        // shape by zeroing its tracking. This mirrors what
        // `on_node_failure`'s replacement path does at line 430-431.
        let idx_replaced = standby_indices[0];
        let idx_synced = standby_indices[1];
        group.members[idx_replaced as usize].synced_through = 0;
        group.members[idx_replaced as usize].last_sync = None;

        // Now drive new events and buffer them — the synced
        // standby's `synced_through` is still 10 from above. The
        // replaced standby's is 0.
        for seq in 11..=15 {
            let event = make_event(seq);
            reg.deliver(group.active_origin(), &event).unwrap();
            group.on_event_delivered(event);
        }
        // Skip the next sync to keep the replaced standby at
        // `last_sync = None`.

        // Active fails. Promote must pick the SYNCED standby, not
        // the freshly-replaced one — even if `synced_through`
        // were equal, the synced one has pre-buffer state we'd
        // otherwise lose. With our state, the synced one's
        // `synced_through = 10` exceeds the replaced one's `0`,
        // so `max_by_key` should already favor it; the test pins
        // the `last_sync.is_some()` filter for the case where
        // both have identical synced_through.
        group.coord.mark_unhealthy(group.active_index);
        let new_active = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .unwrap();
        assert_eq!(
            group.active_index, idx_synced,
            "promote must pick the synced standby (idx={}), not the freshly-replaced one (idx={})",
            idx_synced, idx_replaced,
        );
        let _ = new_active;
    }

    /// Regression: pre-fix, a partial-failure mid-loop in
    /// `sync_standbys` skipped the entire bookkeeping pass via `?`.
    /// Standbys that DID succeed had their daemon state rewritten
    /// to the new snapshot, but `synced_through` still pointed at
    /// the prior cycle's value AND `buffered_since_sync` was not
    /// cleared. A subsequent `promote()` could pick a (stale-by-
    /// tracking) successfully-restored standby and replay
    /// `buffered_since_sync` on top, double-executing every event
    /// between the previous sync and the just-failed sync's
    /// `through_seq`. The fix records bookkeeping per-standby on
    /// success and only clears the buffer when ALL standbys synced.
    #[test]
    fn sync_standbys_partial_failure_records_per_standby_progress() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        // First clean sync — establishes a baseline `synced_through`.
        for seq in 1..=5 {
            let event = make_event(seq);
            reg.deliver(group.active_origin(), &event).unwrap();
            group.on_event_delivered(event);
        }
        group.sync_standbys(&reg).unwrap();
        assert_eq!(group.buffered_event_count(), 0);
        let pre_synced_1 = group.synced_through(1).unwrap();
        let pre_synced_2 = group.synced_through(2).unwrap();
        assert_eq!(pre_synced_1, 5);
        assert_eq!(pre_synced_2, 5);

        // Buffer some new events, then unregister the SECOND standby
        // so its restore_from_snapshot returns NotFound mid-loop.
        for seq in 6..=10 {
            let event = make_event(seq);
            reg.deliver(group.active_origin(), &event).unwrap();
            group.on_event_delivered(event);
        }
        let standby_origins: Vec<u64> = group
            .members
            .iter()
            .filter(|m| m.role == MemberRole::Standby)
            .map(|m| group.coord.members()[m.index as usize].origin_hash)
            .collect();
        assert_eq!(standby_origins.len(), 2);
        // Drop the 2nd standby so restore fails for it.
        reg.unregister(standby_origins[1]).unwrap();

        // Second sync — should fail mid-loop. The first standby's
        // restore succeeded so its bookkeeping must reflect the
        // new snapshot; the second standby's bookkeeping must NOT
        // advance.
        let result = group.sync_standbys(&reg);
        assert!(
            result.is_err(),
            "sync_standbys must surface the standby restore failure",
        );

        // First standby's tracking advanced (state and bookkeeping
        // are coherent). Pre-fix the bookkeeping was skipped, so
        // promote would treat this standby as still synced through
        // the old `pre_synced_1` and replay the buffered range on
        // top of an already-restored state.
        assert_eq!(
            group.synced_through(1),
            Some(10),
            "first standby successfully restored — tracking must reflect the new snapshot",
        );
        // Second standby's tracking did NOT advance — its state is
        // still at the prior sync.
        assert_eq!(
            group.synced_through(2),
            Some(pre_synced_2),
            "failed-standby tracking must NOT advance",
        );
        // Buffer must be RETAINED on partial failure so the next
        // sync cycle has the events the failed standby still owes.
        assert!(
            group.buffered_event_count() > 0,
            "buffered_since_sync must be retained on partial failure",
        );
    }

    /// X-1 epoch scaffolding: every successful `promote` bumps
    /// the `term` counter. A future cross-node fencing layer uses
    /// this to reject events from a stale active after a
    /// partition heal; this test pins the local bump semantic.
    #[test]
    fn promote_bumps_term_counter() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();
        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        // Spawn-time active is term 1.
        assert_eq!(group.term(), 1);

        // First promote → term 2.
        let _ = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .expect("first promote");
        assert_eq!(group.term(), 2);

        // Second promote → term 3. Drives the term advancement
        // documented for partition-heal fencing.
        let _ = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .expect("second promote");
        assert_eq!(group.term(), 3);
    }

    /// X-19 regression: a `promote` that picks a succeeded standby
    /// after a partial `sync_standbys` must NOT replay buffered
    /// events that the chosen standby already received via the
    /// snapshot. The pre-fix path replayed the entire buffer
    /// unconditionally, doubling every side-effect inside the
    /// `[old_synced_through, new_synced_through]` range — silent
    /// state corruption on the promoted daemon.
    #[test]
    fn promote_does_not_double_apply_events_within_synced_range() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        // Baseline: drive 5 events through the active and sync
        // both standbys cleanly. Each successful event bumps
        // StatefulDaemon::value by 1, so the active's value is 5
        // and both standbys' snapshots restore to value=5.
        for seq in 1..=5 {
            let event = make_event(seq);
            reg.deliver(group.active_origin(), &event).unwrap();
            group.on_event_delivered(event);
        }
        group.sync_standbys(&reg).unwrap();

        // Buffer events 6..=10 on the active, then drop standby 2
        // so its restore fails inside the next sync — exactly the
        // partial-failure shape the audit calls out.
        for seq in 6..=10 {
            let event = make_event(seq);
            reg.deliver(group.active_origin(), &event).unwrap();
            group.on_event_delivered(event);
        }
        let standby_2_origin = group
            .members
            .iter()
            .find(|m| m.role == MemberRole::Standby && m.index == 2)
            .map(|m| group.coord.members()[m.index as usize].origin_hash)
            .expect("standby 2");
        reg.unregister(standby_2_origin).unwrap();

        // Partial sync: standby 1 receives the snapshot (value=10
        // post-restore), standby 2 errors out mid-loop. Buffer is
        // retained so a future cycle can still recover standby 2.
        let _ = group.sync_standbys(&reg);
        assert_eq!(group.synced_through(1), Some(10));
        assert!(group.buffered_event_count() > 0);

        // Promote — must pick standby 1 (highest synced_through).
        let new_active = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .expect("promote should pick the succeeded standby");

        // With the fix: every buffered event has sequence ≤ 10
        // (the new active's synced_through), so all are filtered.
        // StatefulDaemon::value stays at 10. Pre-fix the replay
        // unconditionally fired `process` for each of events
        // 6..=10, pushing value to 15.
        let value = reg
            .with_host(new_active, |host| {
                let snap = host.take_snapshot().expect("snapshot");
                u64::from_le_bytes(snap.state[..8].try_into().expect("8 bytes"))
            })
            .expect("with_host");
        assert_eq!(
            value, 10,
            "promote must not double-apply events already in the new active's snapshot"
        );
    }

    /// Regression for BUG_AUDIT_2026_04_30_CORE.md #103: pre-fix
    /// `promote` mutated `active`'s health and role to
    /// `Unhealthy`/`Standby` BEFORE searching for a replacement.
    /// If the search returned `NoHealthyMember`, the function
    /// exited with `Err` but left `self.active_index` pointing
    /// at the now-unhealthy, now-`Standby`-roled `old_active`.
    /// `on_node_recovery` only marks healthy — it doesn't
    /// restore the `Active` role — so the group was silently
    /// demoted forever.
    ///
    /// We pin the fix by:
    ///   1. Building a 3-member group with one active and two standbys.
    ///   2. Marking BOTH standbys unhealthy so promote will fail.
    ///   3. Calling `promote()` and asserting `Err(NoHealthyMember)`.
    ///   4. Asserting the group is still in its pre-promote state:
    ///      `active_origin`, `active_index`, and the active role
    ///      are unchanged. Pre-fix this would have flipped the
    ///      active to `Standby` + `Unhealthy`.
    #[test]
    fn promote_does_not_half_mutate_on_no_healthy_member() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let pre_active_origin = group.active_origin();
        let pre_active_index = group.active_index();

        // Mark every non-active member unhealthy so promote can't
        // find a replacement.
        let total = group.coord.member_count();
        for idx in 0..total {
            if idx != pre_active_index {
                group.coord.mark_unhealthy(idx);
            }
        }

        // Promotion must fail.
        let err = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .expect_err("promote must fail when no healthy standby exists");
        assert!(matches!(err, GroupError::NoHealthyMember));

        // Group state must NOT have been mutated. Pre-fix this
        // would have flipped the active to Standby + Unhealthy
        // before discovering there was no replacement.
        assert_eq!(
            group.active_origin(),
            pre_active_origin,
            "active_origin must be unchanged when promote fails"
        );
        assert_eq!(
            group.active_index(),
            pre_active_index,
            "active_index must be unchanged when promote fails"
        );
        assert_eq!(
            group.members[pre_active_index as usize].role,
            MemberRole::Active,
            "old active's role must NOT have been demoted to Standby"
        );
        assert!(
            group.active_healthy(),
            "old active's health must NOT have been flipped to Unhealthy"
        );
    }

    #[test]
    fn test_promote_on_active_failure() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let active = group.active_origin();

        // Process events and sync
        for seq in 1..=5 {
            let event = make_event(seq);
            reg.deliver(active, &event).unwrap();
            group.on_event_delivered(event);
        }
        group.sync_standbys(&reg).unwrap();

        // Process more events after sync (these buffer for replay)
        for seq in 6..=8 {
            let event = make_event(seq);
            reg.deliver(active, &event).unwrap();
            group.on_event_delivered(event);
        }
        assert_eq!(group.buffered_event_count(), 3);

        // Promote (simulating active failure)
        let new_active = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .unwrap();

        // New active should be different from old
        assert_ne!(new_active, active);
        assert_eq!(group.active_origin(), new_active);
        assert_ne!(group.active_index(), 0);

        // Buffered events should have been replayed (buffer cleared)
        assert_eq!(group.buffered_event_count(), 0);

        // New active should be healthy
        assert!(group.active_healthy());
    }

    #[test]
    fn test_on_node_failure_active() {
        let reg = DaemonRegistry::new();
        // Place members on different nodes by using separate scheduler
        // queries per member. Since our scheduler always returns the same
        // first match, we test the promote logic directly.
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        // Process events and sync so standbys have synced_through > 0
        let active = group.active_origin();
        for seq in 1..=3 {
            let event = make_event(seq);
            reg.deliver(active, &event).unwrap();
            group.on_event_delivered(event);
        }
        group.sync_standbys(&reg).unwrap();

        // Directly test promotion (bypasses node-level failure)
        let old_active = group.active_origin();
        let new_active = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .unwrap();

        assert_ne!(new_active, old_active);
        assert_eq!(group.active_origin(), new_active);
        assert!(group.active_healthy());
    }

    #[test]
    fn test_on_node_failure_standby_only() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let active_before = group.active_origin();

        // Mark a standby unhealthy (simulating its node failing
        // without affecting the active)
        group.coord.mark_unhealthy(1);

        assert_eq!(
            group.health(),
            GroupHealth::Degraded {
                healthy: 2,
                total: 3
            }
        );

        // Active should NOT have changed
        assert_eq!(group.active_origin(), active_before);
        assert!(group.active_healthy());
    }

    #[test]
    fn test_node_recovery() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let standby_node = group.members()[1].node_id;
        group.coord.mark_unhealthy(1);
        assert_eq!(
            group.health(),
            GroupHealth::Degraded {
                healthy: 2,
                total: 3
            }
        );

        group.on_node_recovery(standby_node, &reg);
        assert_eq!(group.health(), GroupHealth::Healthy);
    }

    #[test]
    fn test_deterministic_identity() {
        let reg1 = DaemonRegistry::new();
        let reg2 = DaemonRegistry::new();
        let sched = make_scheduler();

        let g1 = StandbyGroup::spawn(
            test_config(2),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg1,
        )
        .unwrap();
        let g2 = StandbyGroup::spawn(
            test_config(2),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg2,
        )
        .unwrap();

        assert_eq!(g1.group_id(), g2.group_id());
        assert_eq!(g1.active_origin(), g2.active_origin());
    }

    /// Regression: `sync_standbys` previously only updated
    /// bookkeeping (`synced_through`, `last_sync`); it never copied
    /// the active's snapshot bytes onto the standby daemons. On
    /// `promote()`, the picked standby was still in its initial
    /// default-constructed state (`StatefulDaemon::value == 0`),
    /// and `promote()` only replays `buffered_since_sync` (just
    /// cleared at the previous sync). Everything before the most
    /// recent sync was silently lost.
    ///
    /// This test:
    ///   1. Spawns a standby group with one stateful active.
    ///   2. Drives the active to `value = 5` (5 events).
    ///   3. Calls `sync_standbys` to push state to standbys.
    ///   4. Drives the active to `value = 8` (3 more events,
    ///      buffered as `buffered_since_sync`).
    ///   5. Promotes a standby.
    ///   6. Asserts the new active's `value == 8` (snapshot of 5 +
    ///      buffered 3).
    ///
    /// Pre-fix: the new active reads value=3 (only the buffered
    /// events; pre-sync value of 5 lost). Post-fix: value=8.
    #[test]
    fn sync_standbys_actually_restores_state_onto_standbys() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();
        let mut group = StandbyGroup::spawn(
            test_config(2),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        let active_origin = group.active_origin();

        // Drive active to value=5.
        for seq in 1..=5 {
            let ev = make_event(seq);
            // Deliver via registry (advances daemon state).
            reg.deliver(active_origin, &ev).unwrap();
            // Buffer for replay-on-promote (per sync contract).
            group.on_event_delivered(ev);
        }
        // Sanity: active is at 5.
        let snap = reg.snapshot(active_origin).unwrap().unwrap();
        let pre_sync_value = u64::from_le_bytes(snap.state[..8].try_into().unwrap());
        assert_eq!(pre_sync_value, 5);

        // Sync state onto the standby. After this, the standby's
        // daemon must hold value=5 too.
        group.sync_standbys(&reg).unwrap();

        // Verify: the standby's snapshot returns value=5 (the bug
        // would leave the standby at its default value=0).
        let standby_origin = group
            .members
            .iter()
            .find(|m| m.role == MemberRole::Standby)
            .map(|m| group.coord.members()[m.index as usize].origin_hash)
            .unwrap();
        let standby_snap = reg.snapshot(standby_origin).unwrap().unwrap();
        let standby_value = u64::from_le_bytes(standby_snap.state[..8].try_into().unwrap());
        assert_eq!(
            standby_value, 5,
            "standby must hold the active's pre-sync state after sync_standbys; \
             pre-fix this would be 0 because sync only updated bookkeeping"
        );

        // Drive active to value=8 (3 buffered events).
        for seq in 6..=8 {
            let ev = make_event(seq);
            reg.deliver(active_origin, &ev).unwrap();
            group.on_event_delivered(ev);
        }

        // Promote the standby. It replays buffered_since_sync (3
        // events) on top of the synced state (value=5), landing at 8.
        let new_active = group
            .promote(|| Box::new(StatefulDaemon::new()), &reg, &sched)
            .unwrap();

        let new_active_snap = reg.snapshot(new_active).unwrap().unwrap();
        let new_active_value = u64::from_le_bytes(new_active_snap.state[..8].try_into().unwrap());
        assert_eq!(
            new_active_value, 8,
            "promoted active must hold sync-state (5) + buffered events (3) = 8; \
             pre-fix this would be 3 because the standby's pre-sync state was 0"
        );
    }

    // ──────────────────────────────────────────────────────────────────
    // Phase G slice 7 — `*_with_placement` v2 wiring tests for
    // StandbyGroup. Mirror slice 5 / 6 coverage plus the
    // promote-specific contract: data freshness still wins over
    // placement score (recovery correctness preserved).
    // ──────────────────────────────────────────────────────────────────

    use crate::adapter::net::behavior::placement::{NodeId as PlacementNodeId, ResourceAxis};

    fn make_scheduler_and_index(node_ids: &[u64]) -> Scheduler {
        use crate::adapter::net::behavior::fold::{capability_bridge, CapabilityFold, Fold};
        let fold: Arc<Fold<CapabilityFold>> =
            Arc::new(Fold::with_sweep_interval(std::time::Duration::ZERO));
        let eid = crate::adapter::net::identity::EntityId::from_bytes([0u8; 32]);
        for &id in node_ids {
            capability_bridge::apply_legacy_announcement(
                &fold,
                CapabilityAnnouncement::new(id, eid.clone(), 1, CapabilitySet::new()),
            )
            .expect("apply legacy announcement in fixture");
        }
        // Use a local_node_id NOT in the index so placement spreads
        // across indexed nodes instead of always picking local.
        Scheduler::new(fold, 0xFFFF, CapabilitySet::new())
    }

    /// Permissive placement filter — every candidate scores 1.0.
    struct AllowAll;
    impl PlacementFilter for AllowAll {
        fn placement_score(&self, _: &PlacementNodeId, _: &Artifact<'_>) -> Option<f32> {
            Some(1.0)
        }
    }

    /// `spawn_with_placement` produces N members across distinct
    /// nodes when placement is permissive.
    #[test]
    fn spawn_with_placement_spreads_across_nodes() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler_and_index(&[0x1111, 0x2222, 0x3333]);
        let tb = TieBreakContext {
            rtt_lookup: None,
            resource_axis: ResourceAxis::Compute,
        };

        let group = StandbyGroup::spawn_with_placement(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
            &AllowAll,
            &tb,
        )
        .expect("spawn_with_placement should succeed with 3 candidate nodes");

        assert_eq!(group.member_count(), 3);
        assert_eq!(group.standby_count(), 2);
        assert_eq!(group.active_index(), 0);
        assert_eq!(group.health(), GroupHealth::Healthy);
        let node_ids: HashSet<u64> = group.coord.members().iter().map(|m| m.node_id).collect();
        assert_eq!(
            node_ids.len(),
            3,
            "spread invariant: all 3 members on distinct nodes"
        );
    }

    /// `spawn_with_placement` rejects vetoed-everywhere filter.
    #[test]
    fn spawn_with_placement_returns_placement_failed_when_all_vetoed() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler_and_index(&[0x1111, 0x2222]);
        let tb = TieBreakContext {
            rtt_lookup: None,
            resource_axis: ResourceAxis::Compute,
        };

        struct VetoAll;
        impl PlacementFilter for VetoAll {
            fn placement_score(&self, _: &PlacementNodeId, _: &Artifact<'_>) -> Option<f32> {
                None
            }
        }

        let err = StandbyGroup::spawn_with_placement(
            test_config(2),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
            &VetoAll,
            &tb,
        )
        .expect_err("VetoAll filter should make placement fail");

        assert!(matches!(err, GroupError::PlacementFailed(_)));
    }

    /// `promote_with_placement` STILL prefers the most-synced
    /// standby — placement score does NOT override data freshness.
    /// Pin the recovery-correctness invariant: even with a filter
    /// that pegs the LESS-synced standby's node to 1.0 and the
    /// most-synced standby's node to 0.0, promote_with_placement
    /// MUST pick the most-synced standby.
    #[test]
    fn promote_with_placement_prefers_synced_standby_over_higher_score() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler_and_index(&[0x1111, 0x2222, 0x3333]);
        let tb = TieBreakContext {
            rtt_lookup: None,
            resource_axis: ResourceAxis::Compute,
        };

        let mut group = StandbyGroup::spawn_with_placement(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
            &AllowAll,
            &tb,
        )
        .unwrap();

        // Sync standbys so synced_through > 0 for all.
        group
            .sync_standbys(&reg)
            .expect("sync standbys before promote");

        // Manually mark only standby index 1 as freshly synced;
        // standby index 2 is reset to "never synced" — so the
        // promote roster is just standby 1.
        group.members[2].last_sync = None;
        group.members[2].synced_through = 0;

        let standby_1_node = group.coord.members()[1].node_id;
        let standby_2_node = group.coord.members()[2].node_id;

        // Filter prefers standby 2's node (the never-synced one).
        // Even so, promote_with_placement MUST pick standby 1 —
        // the synced one — because data freshness is the primary
        // signal, not placement score.
        struct PreferUnsynced {
            preferred_node: u64,
        }
        impl PlacementFilter for PreferUnsynced {
            fn placement_score(&self, t: &PlacementNodeId, _: &Artifact<'_>) -> Option<f32> {
                Some(if *t == self.preferred_node { 1.0 } else { 0.1 })
            }
        }
        let filter = PreferUnsynced {
            preferred_node: standby_2_node,
        };

        let new_active_origin = group
            .promote_with_placement(
                || Box::new(StatefulDaemon::new()),
                &reg,
                &sched,
                &filter,
                &tb,
            )
            .unwrap();

        // Standby 1 (synced) is now the active.
        assert_eq!(group.active_index(), 1);
        assert_eq!(group.member_role(1), Some(MemberRole::Active));
        let new_active_node = group.coord.members()[1].node_id;
        assert_eq!(
            new_active_node, standby_1_node,
            "synced standby on node {standby_1_node:#x} promoted, NOT unsynced standby on node {standby_2_node:#x}"
        );
        assert_eq!(new_active_origin, group.coord.members()[1].origin_hash);
    }

    /// `promote_with_placement` uses the placement filter to break
    /// ties AMONG equivalently-synced standbys. With two standbys
    /// at the same synced_through, the placement filter picks the
    /// higher-scoring one.
    #[test]
    fn promote_with_placement_breaks_ties_by_score_among_equivalently_synced() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler_and_index(&[0x1111, 0x2222, 0x3333]);
        let tb = TieBreakContext {
            rtt_lookup: None,
            resource_axis: ResourceAxis::Compute,
        };

        let mut group = StandbyGroup::spawn_with_placement(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
            &AllowAll,
            &tb,
        )
        .unwrap();

        // Sync all standbys → both standbys (idx 1, 2) at the
        // same synced_through.
        group.sync_standbys(&reg).expect("sync standbys");

        let standby_1_node = group.coord.members()[1].node_id;
        let standby_2_node = group.coord.members()[2].node_id;
        assert_ne!(standby_1_node, standby_2_node);

        // Filter prefers standby 2's node — should be picked
        // because the two standbys are equivalently fresh, so
        // placement score is the deciding signal.
        struct PreferNode {
            preferred_node: u64,
        }
        impl PlacementFilter for PreferNode {
            fn placement_score(&self, t: &PlacementNodeId, _: &Artifact<'_>) -> Option<f32> {
                Some(if *t == self.preferred_node { 1.0 } else { 0.1 })
            }
        }
        let filter = PreferNode {
            preferred_node: standby_2_node,
        };

        group
            .promote_with_placement(
                || Box::new(StatefulDaemon::new()),
                &reg,
                &sched,
                &filter,
                &tb,
            )
            .unwrap();

        assert_eq!(group.active_index(), 2);
        let new_active_node = group.coord.members()[2].node_id;
        assert_eq!(
            new_active_node, standby_2_node,
            "when standbys are equivalently fresh, placement filter picks the highest scorer"
        );
    }

    /// `promote_with_placement` returns `NoHealthyMember` when no
    /// healthy standby exists, and DOES NOT mutate the group's
    /// active state — same half-mutation safety as v1 promote.
    #[test]
    fn promote_with_placement_does_not_half_mutate_on_no_healthy_member() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler_and_index(&[0x1111, 0x2222, 0x3333]);
        let tb = TieBreakContext {
            rtt_lookup: None,
            resource_axis: ResourceAxis::Compute,
        };

        let mut group = StandbyGroup::spawn_with_placement(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
            &AllowAll,
            &tb,
        )
        .unwrap();

        let active_origin_pre = group.active_origin();
        let active_index_pre = group.active_index();

        // Mark every standby unhealthy.
        for idx in 1..group.member_count() {
            group.coord.mark_unhealthy(idx);
        }

        let err = group
            .promote_with_placement(
                || Box::new(StatefulDaemon::new()),
                &reg,
                &sched,
                &AllowAll,
                &tb,
            )
            .expect_err("no healthy standby → NoHealthyMember");
        assert_eq!(err, GroupError::NoHealthyMember);

        assert_eq!(
            group.active_origin(),
            active_origin_pre,
            "active_origin must be unchanged when promote_with_placement fails"
        );
        assert_eq!(
            group.active_index(),
            active_index_pre,
            "active_index must be unchanged when promote_with_placement fails"
        );
        assert_eq!(
            group.member_role(active_index_pre),
            Some(MemberRole::Active)
        );
    }

    /// `on_node_failure_with_placement` triggers
    /// `promote_with_placement` on active failure and re-places
    /// failed standbys via `place_member`.
    #[test]
    fn on_node_failure_with_placement_promotes_active_and_replaces_standby() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler_and_index(&[0x1111, 0x2222, 0x3333]);
        let tb = TieBreakContext {
            rtt_lookup: None,
            resource_axis: ResourceAxis::Compute,
        };

        let mut group = StandbyGroup::spawn_with_placement(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
            &AllowAll,
            &tb,
        )
        .unwrap();
        let active_node = group.coord.members()[group.active_index() as usize].node_id;

        let new_active = group
            .on_node_failure_with_placement(
                active_node,
                || Box::new(StatefulDaemon::new()),
                &sched,
                &reg,
                &AllowAll,
                &tb,
            )
            .unwrap();

        assert!(
            new_active.is_some(),
            "active failure → promote returns Some"
        );
        assert_ne!(group.active_index(), 0, "active is no longer index 0");
    }

    /// Regression: `try_recover` must skip the active slot even when
    /// `coord.mark_unhealthy(active_index)` is set. Pre-fix the
    /// recovery path constructed a fresh `DaemonHost::new` and
    /// `registry.replace`d the live active, wiping committed state
    /// and resetting `synced_through` / `last_sync`. Active recovery
    /// belongs to `promote`, not slot re-placement; the recovery
    /// trait must also report `has_unhealthy_slots() == false` when
    /// only the active is unhealthy, so the meshos tick doesn't busy-
    /// loop calling `try_recover` with nothing to do.
    #[test]
    fn try_recover_skips_unhealthy_active_and_preserves_state() {
        use crate::adapter::net::compute::UnhealthySlotRecovery;

        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();

        // Advance the active's state so a clobber is detectable.
        let active_origin_before = group.active_origin();
        for seq in 1..=5 {
            let event = make_event(seq);
            reg.deliver(active_origin_before, &event).unwrap();
            group.on_event_delivered(event);
        }
        group.sync_standbys(&reg).unwrap();
        let snapshot_before = reg.snapshot(active_origin_before).unwrap().unwrap();

        // Simulate active node being briefly flagged unhealthy.
        let active_index = group.active_index();
        group.coord.mark_unhealthy(active_index);

        // Recovery probe must NOT advertise work when only the active
        // is unhealthy — promote, not recover, is the right tool.
        assert!(
            !group.has_unhealthy_slots(),
            "has_unhealthy_slots must skip the active slot; only standby slots are recoverable",
        );

        let recovered = group.try_recover(&sched, &reg, &|| Box::new(StatefulDaemon::new()));
        assert!(
            !recovered.contains(&active_index),
            "try_recover must NOT include the active index in the recovered set",
        );

        // Active's daemon-host state is untouched: same origin, same
        // serialized snapshot bytes.
        assert_eq!(
            group.active_origin(),
            active_origin_before,
            "active origin must be unchanged after a try_recover on the active slot",
        );
        let snapshot_after = reg.snapshot(active_origin_before).unwrap().unwrap();
        assert_eq!(
            snapshot_before.state, snapshot_after.state,
            "active's daemon state must be preserved; try_recover must not replace the live active",
        );

        // The standby slots remain healthy too, so no spurious work.
        assert_eq!(
            group.members().iter().filter(|m| m.healthy).count(),
            2,
            "two standbys still healthy",
        );
    }

    /// Regression: every successful standby-slot re-placement in
    /// `try_recover` must bump `term`, matching
    /// `ForkGroup::try_recover_inner` / `ReplicaGroup::try_recover_inner`.
    /// Pre-fix the bump was missing — a sibling that still routed
    /// snapshot syncs to the slot's old node would never observe a
    /// higher term and would keep targeting the wrong node.
    #[test]
    fn try_recover_bumps_term_on_standby_replacement() {
        use crate::adapter::net::compute::UnhealthySlotRecovery;

        let reg = DaemonRegistry::new();
        let sched = make_scheduler();
        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();
        let term_before = group.term();

        // Mark a standby unhealthy so try_recover has work to do.
        let active_index = group.active_index();
        let standby_index: u8 = (0u8..group.member_count())
            .find(|i| *i != active_index)
            .unwrap();
        group.coord.mark_unhealthy(standby_index);

        let recovered = group.try_recover(&sched, &reg, &|| Box::new(StatefulDaemon::new()));
        assert!(
            !recovered.is_empty(),
            "test fixture's scheduler must be able to re-place the standby",
        );
        assert_eq!(
            group.term(),
            term_before.saturating_add(1),
            "term must advance once on successful slot re-placement",
        );

        // Idle try_recover (no unhealthy slots) does NOT bump.
        let term_after_first = group.term();
        let _ = group.try_recover(&sched, &reg, &|| Box::new(StatefulDaemon::new()));
        assert_eq!(
            group.term(),
            term_after_first,
            "no-op try_recover must not advance term",
        );
    }

    /// Companion: when a standby IS unhealthy alongside an unhealthy
    /// active, `try_recover` repairs the standby and leaves the active
    /// alone. `has_unhealthy_slots` returns true on account of the
    /// standby, not the active.
    #[test]
    fn try_recover_repairs_standby_even_when_active_also_unhealthy() {
        use crate::adapter::net::compute::UnhealthySlotRecovery;

        let reg = DaemonRegistry::new();
        let sched = make_scheduler();

        let mut group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();
        let active_index = group.active_index();
        let active_origin_before = group.active_origin();

        // Mark the active AND a standby unhealthy.
        group.coord.mark_unhealthy(active_index);
        let standby_index: u8 = (0u8..group.member_count())
            .find(|i| *i != active_index)
            .unwrap();
        group.coord.mark_unhealthy(standby_index);

        assert!(group.has_unhealthy_slots(), "standby slot is recoverable");
        let recovered = group.try_recover(&sched, &reg, &|| Box::new(StatefulDaemon::new()));
        assert!(!recovered.contains(&active_index), "active still skipped",);
        assert_eq!(
            group.active_origin(),
            active_origin_before,
            "active origin preserved",
        );
    }

    /// `spawn` (v1) is unchanged after the v2 surface lands.
    #[test]
    fn spawn_v1_path_unchanged_after_v2_added() {
        let reg = DaemonRegistry::new();
        let sched = make_scheduler();
        let group = StandbyGroup::spawn(
            test_config(3),
            || Box::new(StatefulDaemon::new()),
            &sched,
            &reg,
        )
        .unwrap();
        assert_eq!(group.member_count(), 3);
        assert_eq!(group.active_index(), 0);
        assert_eq!(group.health(), GroupHealth::Healthy);
    }
}