cbcl-core 0.3.1

CBCL pure core: types, verification, and serialization
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
//! Epidemic gossip protocol for dialect propagation (REQ-120–122).
//!
//! Implements round-based epidemic propagation with configurable transmission
//! probability, infected/susceptible set tracking, and O(log n) expected
//! convergence. Faithfully reproduces `src/cbcl/gossip.scm`.
//!
//! # Purity
//!
//! This module uses a deterministic linear congruential generator (LCG) seeded
//! at construction time, matching the Guile reference. Given the same seed and
//! inputs, the protocol produces identical results — no external RNG or I/O.

#![forbid(unsafe_code)]

use alloc::collections::BTreeMap;
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;

use crate::dialect::Dialect;

// ---------------------------------------------------------------------------
// no_std-compatible ln approximation
// ---------------------------------------------------------------------------

/// Natural logarithm approximation for no_std (used in convergence estimation).
///
/// Uses the identity: ln(x) = 2 * atanh((x-1)/(x+1)) with a truncated series.
/// Accurate enough for O(log n) convergence estimation.
fn ln_approx(x: f64) -> f64 {
    if x <= 0.0 {
        return f64::NEG_INFINITY;
    }
    // Reduce to range [1, 2) by extracting the exponent
    let bits = x.to_bits();
    let exp = ((bits >> 52) & 0x7FF) as i64 - 1023;
    let mantissa = f64::from_bits((bits & 0x000F_FFFF_FFFF_FFFF) | 0x3FF0_0000_0000_0000);

    // ln(x) = exp * ln(2) + ln(mantissa)
    // ln(mantissa) via atanh series: 2 * sum_{k=0}^{N} t^(2k+1)/(2k+1)
    let t = (mantissa - 1.0) / (mantissa + 1.0);
    let t2 = t * t;
    // 7 terms for good accuracy
    let ln_m = 2.0
        * t
        * (1.0
            + t2 * (1.0 / 3.0
                + t2 * (1.0 / 5.0
                    + t2 * (1.0 / 7.0 + t2 * (1.0 / 9.0 + t2 * (1.0 / 11.0 + t2 / 13.0))))));

    (exp as f64) * core::f64::consts::LN_2 + ln_m
}

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Network topology for gossip propagation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Topology {
    /// Every agent is connected to every other agent.
    FullyConnected,
    /// Each agent is connected to its ring neighbors.
    Ring,
    /// Erdős–Rényi random graph with edge probability `p`.
    Random(f64),
}

/// Configuration for the gossip network.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GossipConfig {
    /// Probability of successful transmission per contact (0.0–1.0).
    pub transmission_probability: f64,
    /// Maximum number of rounds before a propagation is considered failed.
    pub max_rounds: u32,
    /// Network topology.
    pub topology: Topology,
}

impl Default for GossipConfig {
    fn default() -> Self {
        Self {
            transmission_probability: 0.8,
            max_rounds: 100,
            topology: Topology::FullyConnected,
        }
    }
}

// ---------------------------------------------------------------------------
// Deterministic RNG (matches Guile simple-random)
// ---------------------------------------------------------------------------

/// Simple linear congruential generator for deterministic gossip randomness.
///
/// Uses the same constants as the Guile reference (`gossip.scm:46–50`):
/// `seed = (seed * 1103515245 + 12345) mod 2^31`
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct Lcg {
    seed: u64,
}

impl Lcg {
    fn new(seed: u64) -> Self {
        Self { seed }
    }

    /// Returns a value in [0.0, 1.0).
    fn next_f64(&mut self) -> f64 {
        let m: u64 = 1 << 31;
        self.seed = (self.seed.wrapping_mul(1_103_515_245).wrapping_add(12345)) % m;
        self.seed as f64 / m as f64
    }
}

// ---------------------------------------------------------------------------
// Propagation state
// ---------------------------------------------------------------------------

/// State of an epidemic propagation for a single dialect.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PropagationState {
    /// The dialect being propagated.
    pub dialect: Dialect,
    /// Agent ids that have the dialect installed.
    pub infected: BTreeSet<String>,
    /// Agent ids that do not yet have the dialect.
    pub susceptible: BTreeSet<String>,
    /// Current round number.
    pub round: u32,
    /// Transmission probability for this propagation.
    pub transmission_prob: f64,
}

impl PropagationState {
    /// Coverage ratio: infected / (infected + susceptible).
    pub fn coverage(&self) -> f64 {
        let total = self.infected.len() + self.susceptible.len();
        if total == 0 {
            1.0
        } else {
            self.infected.len() as f64 / total as f64
        }
    }

    /// Whether propagation is complete (no susceptible agents remain).
    pub fn is_complete(&self) -> bool {
        self.susceptible.is_empty()
    }
}

// ---------------------------------------------------------------------------
// Statistics
// ---------------------------------------------------------------------------

/// Aggregate statistics for a gossip network.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GossipStats {
    /// Total rounds executed across all propagations.
    pub total_rounds: u64,
    /// Number of propagations that reached full coverage.
    pub successful_propagations: u64,
    /// Number of propagations that timed out (exceeded max_rounds).
    pub failed_propagations: u64,
}

// ---------------------------------------------------------------------------
// Simulation result
// ---------------------------------------------------------------------------

/// Result of a complete propagation simulation.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SimulationResult {
    /// Name of the dialect propagated.
    pub dialect_name: String,
    /// Number of rounds to convergence (or timeout).
    pub rounds: u32,
    /// Final coverage ratio.
    pub final_coverage: f64,
    /// Whether the propagation completed successfully.
    pub successful: bool,
}

// ---------------------------------------------------------------------------
// GossipNetwork
// ---------------------------------------------------------------------------

/// Epidemic gossip network for dialect propagation (REQ-120).
///
/// Manages a set of agents connected by a configurable topology.
/// Dialects are propagated epidemically: each round, infected agents
/// attempt to transmit to their susceptible neighbors with a given
/// probability. Expected convergence is O(log n) rounds for a
/// fully-connected network with constant transmission probability.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GossipNetwork {
    /// Agent ids and their installed dialect names.
    agents: BTreeMap<String, BTreeSet<String>>,
    /// Adjacency list: agent_id → set of neighbor ids.
    topology: BTreeMap<String, BTreeSet<String>>,
    /// Active propagations keyed by dialect name.
    active_propagations: BTreeMap<String, PropagationState>,
    /// Aggregate statistics.
    stats: GossipStats,
    /// Network configuration.
    config: GossipConfig,
    /// Deterministic RNG.
    rng: Lcg,
}

impl GossipNetwork {
    /// Create a new gossip network with the given configuration and RNG seed.
    pub fn new(config: GossipConfig, seed: u64) -> Self {
        Self {
            agents: BTreeMap::new(),
            topology: BTreeMap::new(),
            active_propagations: BTreeMap::new(),
            stats: GossipStats::default(),
            config,
            rng: Lcg::new(seed),
        }
    }

    /// Create a new gossip network with default configuration and the
    /// reference seed (1234567, matching the Guile implementation).
    pub fn with_defaults() -> Self {
        Self::new(GossipConfig::default(), 1_234_567)
    }

    // -- Agent management (REQ-121) --

    /// Add an agent to the network.
    ///
    /// The topology is updated automatically based on the configured topology
    /// type. Returns `true` if the agent was newly added, `false` if it was
    /// already present.
    pub fn add_agent(&mut self, agent_id: impl Into<String>) -> bool {
        let id = agent_id.into();
        if self.agents.contains_key(&id) {
            return false;
        }
        self.agents.insert(id.clone(), BTreeSet::new());
        self.topology.insert(id.clone(), BTreeSet::new());
        self.update_topology(&id);
        true
    }

    /// Remove an agent from the network.
    ///
    /// Removes the agent from all neighbor lists and from any active
    /// propagation sets. Returns `true` if the agent was present.
    pub fn remove_agent(&mut self, agent_id: &str) -> bool {
        if self.agents.remove(agent_id).is_none() {
            return false;
        }
        self.topology.remove(agent_id);
        // Remove from all neighbor lists
        for neighbors in self.topology.values_mut() {
            neighbors.remove(agent_id);
        }
        // Remove from active propagations
        for prop in self.active_propagations.values_mut() {
            prop.infected.remove(agent_id);
            prop.susceptible.remove(agent_id);
        }
        true
    }

    /// Returns the set of agent ids in the network.
    pub fn agent_ids(&self) -> Vec<&str> {
        self.agents.keys().map(|s| s.as_str()).collect()
    }

    /// Returns the number of agents in the network.
    pub fn agent_count(&self) -> usize {
        self.agents.len()
    }

    /// Returns neighbors of the given agent.
    pub fn neighbors(&self, agent_id: &str) -> Option<Vec<&str>> {
        self.topology
            .get(agent_id)
            .map(|s| s.iter().map(|n| n.as_str()).collect())
    }

    // -- Topology --

    fn update_topology(&mut self, new_agent_id: &str) {
        match &self.config.topology {
            Topology::FullyConnected => {
                let all_ids: Vec<String> = self.agents.keys().cloned().collect();
                for id in &all_ids {
                    if id != new_agent_id {
                        // Connect new agent to existing
                        self.topology
                            .get_mut(new_agent_id)
                            .unwrap()
                            .insert(id.clone());
                        // Connect existing to new agent
                        self.topology
                            .get_mut(id.as_str())
                            .unwrap()
                            .insert(String::from(new_agent_id));
                    }
                }
            }
            Topology::Ring => {
                let all_ids: Vec<String> = self.agents.keys().cloned().collect();
                if all_ids.len() > 1 {
                    // Connect to up to 2 existing agents
                    let others: Vec<&String> = all_ids
                        .iter()
                        .filter(|id| id.as_str() != new_agent_id)
                        .collect();
                    let count = others.len().min(2);
                    for id in &others[..count] {
                        self.topology
                            .get_mut(new_agent_id)
                            .unwrap()
                            .insert((*id).clone());
                        self.topology
                            .get_mut(id.as_str())
                            .unwrap()
                            .insert(String::from(new_agent_id));
                    }
                }
            }
            Topology::Random(p) => {
                let p = *p;
                let all_ids: Vec<String> = self.agents.keys().cloned().collect();
                for id in &all_ids {
                    if id.as_str() != new_agent_id && self.rng.next_f64() < p {
                        self.topology
                            .get_mut(new_agent_id)
                            .unwrap()
                            .insert(id.clone());
                        self.topology
                            .get_mut(id.as_str())
                            .unwrap()
                            .insert(String::from(new_agent_id));
                    }
                }
            }
        }
    }

    // -- Propagation (REQ-120, REQ-121) --

    /// Start epidemic propagation of a dialect from an initial agent (REQ-121).
    ///
    /// The dialect is considered "installed" on the initial agent. All other
    /// agents in the network are placed in the susceptible set.
    ///
    /// Returns `None` if the initial agent is not in the network, or if a
    /// propagation for this dialect is already active. Otherwise returns the
    /// dialect name as a propagation identifier.
    pub fn announce_dialect(&mut self, dialect: Dialect, initial_agent_id: &str) -> Option<String> {
        if !self.agents.contains_key(initial_agent_id) {
            return None;
        }
        if self.active_propagations.contains_key(&dialect.name) {
            return None;
        }

        let dialect_name = dialect.name.clone();

        // Mark initial agent as having the dialect
        self.agents
            .get_mut(initial_agent_id)
            .unwrap()
            .insert(dialect_name.clone());

        let mut infected = BTreeSet::new();
        infected.insert(String::from(initial_agent_id));

        let susceptible: BTreeSet<String> = self
            .agents
            .keys()
            .filter(|id| id.as_str() != initial_agent_id)
            .cloned()
            .collect();

        let transmission_prob = self.config.transmission_probability;

        self.active_propagations.insert(
            dialect_name.clone(),
            PropagationState {
                dialect,
                infected,
                susceptible,
                round: 0,
                transmission_prob,
            },
        );

        #[cfg(feature = "tracing")]
        tracing::event!(
            tracing::Level::INFO,
            dialect = %dialect_name,
            initial_agent = initial_agent_id,
            network_size = self.agents.len(),
            "gossip_announce_dialect"
        );

        Some(dialect_name)
    }

    /// Execute one round of epidemic propagation for all active dialects.
    ///
    /// Each infected agent attempts to transmit to each of its susceptible
    /// neighbors with the configured transmission probability. Newly infected
    /// agents are added to the infected set. Propagations that complete (empty
    /// susceptible set) or exceed `max_rounds` are cleaned up.
    ///
    /// Returns a list of (dialect_name, newly_infected_agent_ids) for this round.
    pub fn step(&mut self) -> Vec<(String, Vec<String>)> {
        let dialect_names: Vec<String> = self.active_propagations.keys().cloned().collect();
        let mut results = Vec::new();

        for dialect_name in &dialect_names {
            let newly_infected = self.step_propagation(dialect_name);
            if !newly_infected.is_empty() {
                results.push((dialect_name.clone(), newly_infected));
            }
        }

        self.cleanup_completed_propagations();
        self.stats.total_rounds += 1;

        #[cfg(feature = "tracing")]
        for (name, prop) in &self.active_propagations {
            tracing::event!(
                tracing::Level::INFO,
                dialect = %name,
                round = prop.round,
                coverage = prop.coverage(),
                infected = prop.infected.len(),
                susceptible = prop.susceptible.len(),
                "gossip_round_convergence"
            );
        }

        results
    }

    fn step_propagation(&mut self, dialect_name: &str) -> Vec<String> {
        // Collect the data we need to decide transmissions
        let prop = match self.active_propagations.get(dialect_name) {
            Some(p) => p,
            None => return Vec::new(),
        };

        let infected: Vec<String> = prop.infected.iter().cloned().collect();
        let transmission_prob = prop.transmission_prob;

        // For each infected agent, try to infect susceptible neighbors
        let mut newly_infected = Vec::new();

        for infected_id in &infected {
            let neighbor_ids: Vec<String> = self
                .topology
                .get(infected_id.as_str())
                .map(|s| s.iter().cloned().collect())
                .unwrap_or_default();

            let prop = self.active_propagations.get(dialect_name).unwrap();
            for neighbor_id in &neighbor_ids {
                if prop.susceptible.contains(neighbor_id) && self.rng.next_f64() < transmission_prob
                {
                    newly_infected.push(neighbor_id.clone());
                }
            }
        }

        // Update propagation state
        if let Some(prop) = self.active_propagations.get_mut(dialect_name) {
            for id in &newly_infected {
                prop.infected.insert(id.clone());
                prop.susceptible.remove(id);
                // Mark agent as having the dialect
                if let Some(agent_dialects) = self.agents.get_mut(id) {
                    agent_dialects.insert(dialect_name.to_string());
                }
            }
            prop.round += 1;
        }

        newly_infected
    }

    fn cleanup_completed_propagations(&mut self) {
        let max_rounds = self.config.max_rounds;
        let to_remove: Vec<(String, bool)> = self
            .active_propagations
            .iter()
            .filter_map(|(name, prop)| {
                if prop.susceptible.is_empty() {
                    Some((name.clone(), true))
                } else if prop.round >= max_rounds {
                    Some((name.clone(), false))
                } else {
                    None
                }
            })
            .collect();

        for (name, success) in to_remove {
            self.active_propagations.remove(&name);
            if success {
                self.stats.successful_propagations += 1;
            } else {
                self.stats.failed_propagations += 1;
            }
        }
    }

    // -- Gossip reception (REQ-121, REQ-122) --

    /// Receive a gossip message containing a dialect for a specific agent.
    ///
    /// The dialect is validated through the R1+R2+R3 pipeline (REQ-122).
    /// Returns a list of validation violations if the dialect fails checks,
    /// or `Ok(())` if the dialect is accepted.
    ///
    /// Note: actual dialect installation on real Agent structs is the caller's
    /// responsibility. This method tracks the gossip-level state (infected/
    /// susceptible sets) for the propagation simulation.
    pub fn receive_gossip(&mut self, agent_id: &str, dialect: &Dialect) -> Result<(), Vec<String>> {
        // Validate through R1+R2+R3 pipeline (REQ-122)
        let mut violations = Vec::new();

        if !crate::r1::verify_r1_dialect(dialect) {
            violations.extend(crate::r1::r1_violations(dialect));
        }
        if !crate::r2::verify_r2(dialect) {
            violations.push(alloc::format!(
                "R2 violation: invalid resource bounds in '{}'",
                dialect.name
            ));
        }
        if !crate::r3::verify_r3(dialect) {
            violations.extend(
                crate::r3::r3_violations(dialect)
                    .into_iter()
                    .map(|v| alloc::format!("R3 violation: {}", v)),
            );
        }

        if !violations.is_empty() {
            return Err(violations);
        }

        // Mark agent as having the dialect
        if let Some(agent_dialects) = self.agents.get_mut(agent_id) {
            agent_dialects.insert(dialect.name.clone());
        }

        // Update propagation state if this dialect is being actively propagated
        if let Some(prop) = self.active_propagations.get_mut(&dialect.name) {
            prop.susceptible.remove(agent_id);
            prop.infected.insert(String::from(agent_id));
        }

        Ok(())
    }

    // -- Peer selection (REQ-121) --

    /// Select up to `k` peers for gossip fan-out (REQ-121).
    ///
    /// Uses the deterministic LCG to select peers from the agent's neighbors.
    /// Returns the selected peer ids.
    pub fn select_peers(&mut self, agent_id: &str, k: usize) -> Vec<String> {
        let neighbors: Vec<String> = match self.topology.get(agent_id) {
            Some(n) => n.iter().cloned().collect(),
            None => return Vec::new(),
        };

        if neighbors.len() <= k {
            return neighbors;
        }

        // Fisher-Yates shuffle on a copy, then take first k
        let mut shuffled = neighbors;
        let len = shuffled.len();
        for i in (1..len).rev() {
            let r = self.rng.next_f64();
            let j = (r * (i + 1) as f64) as usize;
            let j = j.min(i); // safety bound
            shuffled.swap(i, j);
        }
        shuffled.truncate(k);
        shuffled
    }

    // -- Analysis --

    /// Estimate expected convergence time in rounds (REQ-120).
    ///
    /// For a network of n agents with transmission probability p,
    /// the expected convergence is O(log n) rounds:
    /// `ceil(ln(n) / ln(1 + p))`
    ///
    /// This matches `estimate-convergence-time` in `gossip.scm:275–284`.
    pub fn estimate_convergence_time(&self) -> u32 {
        let n = self.agents.len();
        if n <= 1 {
            return 0;
        }
        let p = self.config.transmission_probability;
        let rounds = ln_approx(n as f64) / ln_approx(1.0 + p);
        rounds as u32 + 1
    }

    /// Check if the network is connected (all agents reachable from any agent).
    pub fn is_connected(&self) -> bool {
        if self.agents.is_empty() {
            return true;
        }

        let start = match self.agents.keys().next() {
            Some(id) => id.clone(),
            None => return true,
        };

        let mut visited = BTreeSet::new();
        let mut queue = alloc::vec![start];

        while let Some(current) = queue.pop() {
            if !visited.insert(current.clone()) {
                continue;
            }
            if let Some(neighbors) = self.topology.get(&current) {
                for neighbor in neighbors {
                    if !visited.contains(neighbor) {
                        queue.push(neighbor.clone());
                    }
                }
            }
        }

        visited.len() == self.agents.len()
    }

    /// Simulate complete epidemic propagation until convergence or timeout.
    ///
    /// Matches `simulate-propagation` in `gossip.scm:302–318`.
    pub fn simulate_propagation(
        &mut self,
        dialect: Dialect,
        initial_agent_id: &str,
    ) -> Option<SimulationResult> {
        let dialect_name = self.announce_dialect(dialect, initial_agent_id)?;
        let mut rounds = 0u32;

        while self.active_propagations.contains_key(&dialect_name) {
            self.step();
            rounds += 1;
        }

        // Determine if it was successful by checking stats
        // (the cleanup in step() already updated stats)
        let successful = self
            .agents
            .values()
            .all(|dialects| dialects.contains(&dialect_name));

        Some(SimulationResult {
            dialect_name,
            rounds,
            final_coverage: if successful { 1.0 } else { 0.0 },
            successful,
        })
    }

    // -- Accessors --

    /// Returns a reference to the network configuration.
    pub fn config(&self) -> &GossipConfig {
        &self.config
    }

    /// Returns a reference to the aggregate statistics.
    pub fn stats(&self) -> &GossipStats {
        &self.stats
    }

    /// Returns a reference to the active propagations.
    pub fn active_propagations(&self) -> &BTreeMap<String, PropagationState> {
        &self.active_propagations
    }

    /// Get propagation statistics for a specific dialect.
    pub fn propagation_stats(&self, dialect_name: &str) -> Option<&PropagationState> {
        self.active_propagations.get(dialect_name)
    }

    /// Check if an agent has a specific dialect installed.
    pub fn agent_has_dialect(&self, agent_id: &str, dialect_name: &str) -> bool {
        self.agents
            .get(agent_id)
            .map(|d| d.contains(dialect_name))
            .unwrap_or(false)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dialect::{PerformativeDef, ResourceBounds};
    use crate::sexpr::{Atom, SExpr};
    use alloc::string::String;

    fn test_dialect(name: &str) -> Dialect {
        Dialect {
            types: Default::default(),
            roles: Vec::new(),
            state: None,
            state_bounds: None,
            causal_locality: Default::default(),
            name: String::from(name),
            extends: Vec::new(),
            author: None,
            performatives: vec![PerformativeDef {
                role: None,
                name: alloc::format!("{}-action", name),
                params: Vec::new(),
                template: SExpr::List(alloc::vec![
                    SExpr::Atom(Atom::Symbol(String::from("effect"))),
                    SExpr::Atom(Atom::Symbol(alloc::format!("{}-effect", name))),
                ]),
            }],
            resources: ResourceBounds {
                max_depth: 8,
                max_expansion_size: 512,
            },
            examples: Vec::new(),
            signature: None,
            hash: None,
            protocol: None,
            causal_protocol: None,
            shapes: Vec::new(),
        }
    }

    // -- Construction --

    #[test]
    fn new_network_is_empty() {
        let net = GossipNetwork::with_defaults();
        assert_eq!(net.agent_count(), 0);
        assert!(net.agent_ids().is_empty());
        assert!(net.active_propagations().is_empty());
    }

    #[test]
    fn default_config() {
        let config = GossipConfig::default();
        assert!((config.transmission_probability - 0.8).abs() < f64::EPSILON);
        assert_eq!(config.max_rounds, 100);
        assert_eq!(config.topology, Topology::FullyConnected);
    }

    // -- Agent management --

    #[test]
    fn add_agent() {
        let mut net = GossipNetwork::with_defaults();
        assert!(net.add_agent("alice"));
        assert!(net.add_agent("bob"));
        assert!(!net.add_agent("alice")); // duplicate
        assert_eq!(net.agent_count(), 2);
    }

    #[test]
    fn remove_agent() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        net.add_agent("bob");
        assert!(net.remove_agent("alice"));
        assert!(!net.remove_agent("alice")); // already removed
        assert_eq!(net.agent_count(), 1);
    }

    #[test]
    fn remove_agent_updates_topology() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        net.add_agent("bob");
        net.add_agent("carol");
        // Fully connected: each has 2 neighbors
        assert_eq!(net.neighbors("alice").unwrap().len(), 2);
        net.remove_agent("bob");
        // Alice should now only have carol as neighbor
        let neighbors = net.neighbors("alice").unwrap();
        assert_eq!(neighbors.len(), 1);
        assert_eq!(neighbors[0], "carol");
    }

    // -- Topology --

    #[test]
    fn fully_connected_topology() {
        let mut net = GossipNetwork::with_defaults();
        for name in &["a", "b", "c", "d"] {
            net.add_agent(*name);
        }
        for name in &["a", "b", "c", "d"] {
            assert_eq!(net.neighbors(name).unwrap().len(), 3);
        }
    }

    #[test]
    fn ring_topology() {
        let config = GossipConfig {
            topology: Topology::Ring,
            ..GossipConfig::default()
        };
        let mut net = GossipNetwork::new(config, 42);
        for name in &["a", "b", "c", "d", "e"] {
            net.add_agent(*name);
        }
        // Each agent should have at most 2 neighbors in a ring
        for name in &["a", "b", "c", "d", "e"] {
            let n = net.neighbors(name).unwrap().len();
            assert!(n <= 4, "ring neighbor count {} > 4 for {}", n, name);
            assert!(n >= 1, "ring neighbor count {} < 1 for {}", n, name);
        }
    }

    #[test]
    fn is_connected_empty_network() {
        let net = GossipNetwork::with_defaults();
        assert!(net.is_connected());
    }

    #[test]
    fn is_connected_fully_connected() {
        let mut net = GossipNetwork::with_defaults();
        for name in &["a", "b", "c"] {
            net.add_agent(*name);
        }
        assert!(net.is_connected());
    }

    // -- Propagation --

    #[test]
    fn announce_dialect_creates_propagation() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        net.add_agent("bob");
        let result = net.announce_dialect(test_dialect("test"), "alice");
        assert_eq!(result, Some(String::from("test")));
        assert_eq!(net.active_propagations().len(), 1);

        let prop = net.propagation_stats("test").unwrap();
        assert_eq!(prop.infected.len(), 1);
        assert!(prop.infected.contains("alice"));
        assert_eq!(prop.susceptible.len(), 1);
        assert!(prop.susceptible.contains("bob"));
        assert_eq!(prop.round, 0);
    }

    #[test]
    fn announce_dialect_unknown_agent() {
        let mut net = GossipNetwork::with_defaults();
        let result = net.announce_dialect(test_dialect("test"), "nonexistent");
        assert!(result.is_none());
    }

    #[test]
    fn announce_dialect_duplicate() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        net.announce_dialect(test_dialect("test"), "alice");
        let result = net.announce_dialect(test_dialect("test"), "alice");
        assert!(result.is_none());
    }

    #[test]
    fn step_propagates_dialect() {
        let config = GossipConfig {
            transmission_probability: 1.0, // guaranteed transmission
            ..GossipConfig::default()
        };
        let mut net = GossipNetwork::new(config, 42);
        for name in &["a", "b", "c"] {
            net.add_agent(*name);
        }
        net.announce_dialect(test_dialect("test"), "a");

        // After one step with p=1.0, all neighbors of "a" should be infected
        let results = net.step();
        assert!(!results.is_empty());

        // In a fully connected network with p=1.0, one round should infect everyone
        assert!(net.active_propagations().is_empty()); // cleaned up as complete
        assert_eq!(net.stats().successful_propagations, 1);
    }

    #[test]
    fn step_respects_max_rounds() {
        let config = GossipConfig {
            transmission_probability: 0.0, // never transmits
            max_rounds: 3,
            ..GossipConfig::default()
        };
        let mut net = GossipNetwork::new(config, 42);
        net.add_agent("a");
        net.add_agent("b");
        net.announce_dialect(test_dialect("test"), "a");

        for _ in 0..4 {
            net.step();
        }
        // Should have timed out
        assert!(net.active_propagations().is_empty());
        assert_eq!(net.stats().failed_propagations, 1);
    }

    // -- Convergence estimation --

    #[test]
    fn estimate_convergence_single_agent() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        assert_eq!(net.estimate_convergence_time(), 0);
    }

    #[test]
    fn estimate_convergence_log_n() {
        let mut net = GossipNetwork::with_defaults();
        for i in 0..100 {
            net.add_agent(alloc::format!("agent-{}", i));
        }
        let estimate = net.estimate_convergence_time();
        // With p=0.8, ln(100)/ln(1.8) ≈ 7.8, so estimate should be 8
        assert!(estimate > 0);
        assert!(
            estimate <= 10,
            "estimate {} too high for 100 agents",
            estimate
        );
    }

    #[test]
    fn estimate_convergence_empty_network() {
        let net = GossipNetwork::with_defaults();
        assert_eq!(net.estimate_convergence_time(), 0);
    }

    // -- Receive gossip (REQ-122) --

    #[test]
    fn receive_gossip_valid_dialect() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        let d = test_dialect("ext");
        assert!(net.receive_gossip("alice", &d).is_ok());
        assert!(net.agent_has_dialect("alice", "ext"));
    }

    #[test]
    fn receive_gossip_rejects_r3_violation() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        let bad = Dialect {
            types: Default::default(),
            roles: Vec::new(),
            state: None,
            state_bounds: None,
            causal_locality: Default::default(),
            name: String::from("bad"),
            extends: Vec::new(),
            author: None,
            performatives: vec![PerformativeDef {
                role: None,
                name: String::from("tell"), // R3 violation
                params: Vec::new(),
                template: SExpr::Atom(Atom::Symbol(String::from("x"))),
            }],
            resources: ResourceBounds {
                max_depth: 8,
                max_expansion_size: 512,
            },
            examples: Vec::new(),
            signature: None,
            hash: None,
            protocol: None,
            causal_protocol: None,
            shapes: Vec::new(),
        };
        let result = net.receive_gossip("alice", &bad);
        assert!(result.is_err());
        assert!(!net.agent_has_dialect("alice", "bad"));
    }

    #[test]
    fn receive_gossip_rejects_r2_violation() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("alice");
        let bad = Dialect {
            types: Default::default(),
            roles: Vec::new(),
            state: None,
            state_bounds: None,
            causal_locality: Default::default(),
            name: String::from("bad-bounds"),
            extends: Vec::new(),
            author: None,
            performatives: Vec::new(),
            resources: ResourceBounds {
                max_depth: 100, // invalid
                max_expansion_size: 512,
            },
            examples: Vec::new(),
            signature: None,
            hash: None,
            protocol: None,
            causal_protocol: None,
            shapes: Vec::new(),
        };
        let result = net.receive_gossip("alice", &bad);
        assert!(result.is_err());
    }

    // -- Select peers (REQ-121) --

    #[test]
    fn select_peers_returns_subset() {
        let mut net = GossipNetwork::with_defaults();
        for name in &["a", "b", "c", "d", "e"] {
            net.add_agent(*name);
        }
        let peers = net.select_peers("a", 2);
        assert_eq!(peers.len(), 2);
        // All selected peers should be neighbors of "a"
        let neighbors = net.neighbors("a").unwrap();
        for peer in &peers {
            assert!(neighbors.contains(&peer.as_str()));
        }
    }

    #[test]
    fn select_peers_all_when_k_large() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("a");
        net.add_agent("b");
        let peers = net.select_peers("a", 10);
        assert_eq!(peers.len(), 1); // only neighbor is "b"
    }

    #[test]
    fn select_peers_unknown_agent() {
        let mut net = GossipNetwork::with_defaults();
        let peers = net.select_peers("nonexistent", 3);
        assert!(peers.is_empty());
    }

    // -- Simulation --

    #[test]
    fn simulate_propagation_converges() {
        let config = GossipConfig {
            transmission_probability: 1.0,
            ..GossipConfig::default()
        };
        let mut net = GossipNetwork::new(config, 42);
        for i in 0..10 {
            net.add_agent(alloc::format!("agent-{}", i));
        }
        let result = net
            .simulate_propagation(test_dialect("test"), "agent-0")
            .unwrap();
        assert!(result.successful);
        assert!((result.final_coverage - 1.0).abs() < f64::EPSILON);
        // With p=1.0 fully connected, should converge in 1 round
        assert_eq!(result.rounds, 1);
    }

    #[test]
    fn simulate_propagation_with_realistic_probability() {
        let mut net = GossipNetwork::new(GossipConfig::default(), 42);
        for i in 0..20 {
            net.add_agent(alloc::format!("agent-{}", i));
        }
        let result = net
            .simulate_propagation(test_dialect("epidemic"), "agent-0")
            .unwrap();
        assert!(result.successful);
        // O(log n) convergence: should complete well under max_rounds
        assert!(
            result.rounds < 20,
            "took {} rounds for 20 agents",
            result.rounds
        );
    }

    #[test]
    fn simulate_propagation_unknown_agent() {
        let mut net = GossipNetwork::with_defaults();
        let result = net.simulate_propagation(test_dialect("test"), "nonexistent");
        assert!(result.is_none());
    }

    // -- Coverage --

    #[test]
    fn propagation_coverage() {
        let mut state = PropagationState {
            dialect: test_dialect("test"),
            infected: BTreeSet::new(),
            susceptible: BTreeSet::new(),
            round: 0,
            transmission_prob: 0.8,
        };
        // Empty network: coverage = 1.0
        assert!((state.coverage() - 1.0).abs() < f64::EPSILON);
        assert!(state.is_complete());

        state.infected.insert(String::from("a"));
        state.susceptible.insert(String::from("b"));
        state.susceptible.insert(String::from("c"));
        assert!((state.coverage() - 1.0 / 3.0).abs() < 0.01);
        assert!(!state.is_complete());
    }

    // -- Determinism --

    #[test]
    fn deterministic_with_same_seed() {
        let run = |seed: u64| -> SimulationResult {
            let mut net = GossipNetwork::new(GossipConfig::default(), seed);
            for i in 0..15 {
                net.add_agent(alloc::format!("agent-{}", i));
            }
            net.simulate_propagation(test_dialect("det-test"), "agent-0")
                .unwrap()
        };

        let r1 = run(9999);
        let r2 = run(9999);
        assert_eq!(r1.rounds, r2.rounds);
        assert_eq!(r1.successful, r2.successful);
    }

    // -- Statistics --

    #[test]
    fn stats_track_success_and_failure() {
        // Successful propagation
        let config = GossipConfig {
            transmission_probability: 1.0,
            max_rounds: 100,
            ..GossipConfig::default()
        };
        let mut net = GossipNetwork::new(config, 42);
        net.add_agent("a");
        net.add_agent("b");
        net.simulate_propagation(test_dialect("good"), "a");
        assert_eq!(net.stats().successful_propagations, 1);

        // Failed propagation (p=0, max_rounds=2)
        let config2 = GossipConfig {
            transmission_probability: 0.0,
            max_rounds: 2,
            ..GossipConfig::default()
        };
        let mut net2 = GossipNetwork::new(config2, 42);
        net2.add_agent("a");
        net2.add_agent("b");
        net2.simulate_propagation(test_dialect("fail"), "a");
        assert_eq!(net2.stats().failed_propagations, 1);
    }

    // -- LCG --

    #[test]
    fn lcg_produces_values_in_range() {
        let mut rng = Lcg::new(12345);
        for _ in 0..1000 {
            let v = rng.next_f64();
            assert!((0.0..1.0).contains(&v), "LCG value {} out of range", v);
        }
    }

    #[test]
    fn lcg_deterministic() {
        let mut rng1 = Lcg::new(42);
        let mut rng2 = Lcg::new(42);
        for _ in 0..100 {
            assert_eq!(rng1.next_f64().to_bits(), rng2.next_f64().to_bits());
        }
    }

    // -- Remove agent from active propagation --

    #[test]
    fn remove_agent_during_propagation() {
        let mut net = GossipNetwork::with_defaults();
        net.add_agent("a");
        net.add_agent("b");
        net.add_agent("c");
        net.announce_dialect(test_dialect("test"), "a");

        // Remove a susceptible agent
        net.remove_agent("b");
        let prop = net.propagation_stats("test").unwrap();
        assert!(!prop.susceptible.contains("b"));
        assert_eq!(prop.susceptible.len(), 1);
    }
}