aika 0.2.1

Multi-agent coordination framework in Rust with single and multi-threaded execution engines.
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
//! Single-threaded simulation world supporting multiple actors with message passing capabilities.
//! Provides a `LonePlanet` struct that manages actor execution, event scheduling, and local message
//! delivery in a deterministic single-threaded environment with configurable time bounds.
use bytemuck::{Pod, Zeroable};
use mesocarp::comms::buses::Message;

use crate::{
    actors::{Actor, ActorType, ConnectedActor, Context},
    env::Environment,
    objects::{Event, LocalScheduler, Msg, SchedulingTask, Transfer},
    AikaError,
};

/// A world that can contain multiple actors and run a simulation.
pub struct LonePlanet<
    const CLOCK_SLOTS: usize,
    const CLOCK_HEIGHT: usize,
    MessageType: Pod + Zeroable + Clone,
> {
    pub actors: Vec<ActorType<MessageType>>,
    pub env: Context<MessageType>,
    event_scheduler: LocalScheduler<CLOCK_SLOTS, CLOCK_HEIGHT, Event>,
    mail_scheduler: LocalScheduler<CLOCK_SLOTS, CLOCK_HEIGHT, Msg<MessageType>>,
}

unsafe impl<const CLOCK_SLOTS: usize, const CLOCK_HEIGHT: usize, MessageType: Pod + Zeroable + Clone>
    Send for LonePlanet<CLOCK_SLOTS, CLOCK_HEIGHT, MessageType>
{
}
unsafe impl<const CLOCK_SLOTS: usize, const CLOCK_HEIGHT: usize, MessageType: Pod + Zeroable + Clone>
    Sync for LonePlanet<CLOCK_SLOTS, CLOCK_HEIGHT, MessageType>
{
}

impl<const CLOCK_SLOTS: usize, const CLOCK_HEIGHT: usize, MessageType: Pod + Zeroable + Clone>
    LonePlanet<CLOCK_SLOTS, CLOCK_HEIGHT, MessageType>
{
    /// Initialize a new world with the provided time information and world state arena allocation size
    pub fn init(env: impl Environment + 'static) -> Result<Self, AikaError> {
        let mail_scheduler = LocalScheduler::new()?;
        let event_scheduler = LocalScheduler::new()?;
        Ok(Self {
            actors: Vec::new(),
            env: Context::new(env, false, 0, u64::MAX),
            mail_scheduler,
            event_scheduler,
        })
    }

    pub fn set_terminal_time(&mut self, terminal: u64) {
        self.env.terminal = terminal;
    }

    pub fn spawn_receiver_actor(
        &mut self,
        actor: impl ConnectedActor<MessageType> + 'static,
    ) -> usize {
        let actor = ActorType::Connected(Box::new(actor));
        self.actors.push(actor);
        self.actors.len() - 1
    }

    /// Spawn a new `Agent` to the `LonePlanet`.
    pub fn spawn_actor(&mut self, actor: impl Actor<MessageType> + 'static) -> usize {
        let actor = ActorType::Basic(Box::new(actor));
        self.actors.push(actor);
        self.actors.len() - 1
    }

    fn commit(&mut self, event: Event) {
        self.event_scheduler.insert(event)
    }

    fn commit_mail(&mut self, mail: Msg<MessageType>) {
        self.mail_scheduler.insert(mail);
    }

    /// Get the current time of the simulation.
    #[inline(always)]
    pub fn now(&self) -> u64 {
        self.event_scheduler.now()
    }

    /// Get the time information of the simulation.
    pub fn terminal(&self) -> u64 {
        self.env.terminal
    }

    /// Schedule an event for an actor at a given time.
    pub fn schedule(&mut self, time: u64, actor: usize) -> Result<(), AikaError> {
        if time < self.now() {
            return Err(AikaError::TimeTravel);
        } else if time > self.env.terminal {
            return Err(AikaError::PastTerminal);
        }
        let now = self.now();
        self.commit(Event::new(now, time, actor, SchedulingTask::Wait));
        Ok(())
    }

    /// Run the simulation.
    pub fn run(&mut self) -> Result<(), AikaError> {
        loop {
            if (self.now() + 1) > self.env.terminal {
                break;
            }
            if let Ok(msgs) = self.mail_scheduler.tick() {
                for msg in msgs {
                    let id = msg.to.1;
                    if id == usize::MAX {
                        for i in 0..self.actors.len() {
                            match &mut self.actors[i] {
                                ActorType::Basic(_) => {}
                                ActorType::Connected(connected_actor) => {
                                    connected_actor.read_message(&mut self.env, msg, i)?;
                                }
                            }
                        }
                        continue;
                    }
                    if self.actors.len() <= id {
                        return Err(AikaError::MessagedNonExistent(self.actors.len(), id));
                    }
                    match &mut self.actors[id] {
                        ActorType::Basic(_) => {
                            return Err(AikaError::MessagedNonReceiver);
                        }
                        ActorType::Connected(connected_actor) => {
                            connected_actor.read_message(&mut self.env, msg, id)?;
                        }
                    }
                }
            }
            if let Ok(events) = self.event_scheduler.tick() {
                for event in events {
                    if event.time > self.env.terminal {
                        break;
                    }

                    let env = &mut self.env;
                    let id = event.actor;
                    if self.actors.len() <= id {
                        return Err(AikaError::InvalidActorId(
                            self.actors.len(),
                            self.env.cluster_id,
                            id,
                        ));
                    }
                    let task = self.actors[id].step(env, id)?;
                    match task {
                        SchedulingTask::Timeout(time) => {
                            if (self.now() + time) > self.env.terminal {
                                continue;
                            }

                            self.commit(Event::new(
                                self.now(),
                                self.now() + time,
                                event.actor,
                                SchedulingTask::Wait,
                            ));
                        }
                        SchedulingTask::Schedule(time) => {
                            self.commit(Event::new(
                                self.now(),
                                time,
                                event.actor,
                                SchedulingTask::Wait,
                            ));
                        }
                        SchedulingTask::Trigger { time, idx } => {
                            self.commit(Event::new(self.now(), time, idx, SchedulingTask::Wait));
                        }
                        SchedulingTask::Wait => {}
                        SchedulingTask::Break => {
                            break;
                        }
                    }
                }
            }

            let sends = std::mem::take(&mut self.env.outbox);
            for msg in sends {
                if msg.to() != Some(usize::MAX) {
                    if Some(self.env.cluster_id) == msg.to() {
                        match msg {
                            Transfer::Msg(msg) => self.commit_mail(msg),
                            Transfer::AntiMsg(_) => return Err(AikaError::ThreadPanic),
                        }
                        continue;
                    }
                } else {
                    match msg {
                        Transfer::Msg(msg) => self.commit_mail(msg),
                        Transfer::AntiMsg(_) => return Err(AikaError::ThreadPanic),
                    }
                }
            }

            self.event_scheduler.increment();
            self.mail_scheduler.increment();
            self.env.time += 1;
        }
        Ok(())
    }
}

#[macro_export]
macro_rules! world {
    // Just the message type - use all defaults
    ($msg_type:ty) => {
        $crate::st::LonePlanet::<128, 2, $msg_type>::init
    };

    // With CLOCK_SLOTS only
    ($msg_type:ty, CLOCK_SLOTS = $clock_slots:expr) => {
        $crate::st::LonePlanet::<$clock_slots, 2, $msg_type>::init
    };

    // With CLOCK_HEIGHT only
    ($msg_type:ty, CLOCK_HEIGHT = $clock_height:expr) => {
        $crate::st::LonePlanet::<128, $clock_height, $msg_type>::init
    };

    // With both parameters
    ($msg_type:ty, CLOCK_SLOTS = $clock_slots:expr, CLOCK_HEIGHT = $clock_height:expr) => {
        $crate::st::LonePlanet::<$clock_slots, $clock_height, $msg_type>::init
    };

    // With both parameters (reverse order)
    ($msg_type:ty, CLOCK_HEIGHT = $clock_height:expr, CLOCK_SLOTS = $clock_slots:expr) => {
        $crate::st::LonePlanet::<$clock_slots, $clock_height, $msg_type>::init
    };
}

#[cfg(test)]
mod tests {
    use crate::actors::ConnectedActor;
    use crate::env::Stateless;

    use super::*;
    use std::cell::RefCell;
    use std::rc::Rc;

    #[derive(Debug)]
    // Simple actor that just schedules timeouts
    pub struct TestAgent {
        pub _id: usize,
    }

    impl TestAgent {
        pub fn new(_id: usize) -> Self {
            TestAgent { _id }
        }
    }

    impl Actor<u8> for TestAgent {
        fn step(
            &mut self,
            _supports: &mut Context<u8>,
            _id: usize,
        ) -> Result<SchedulingTask, AikaError> {
            Ok(SchedulingTask::Timeout(1))
        }
    }

    #[derive(Debug)]
    // Agent that sends messages
    pub struct SendingAgent {
        pub id: usize,
        pub target: usize,
        pub message_count: usize,
        pub messages_sent: usize,
    }

    impl SendingAgent {
        pub fn new(id: usize, target: usize, message_count: usize) -> Self {
            SendingAgent {
                id,
                target,
                message_count,
                messages_sent: 0,
            }
        }
    }

    impl Actor<u8> for SendingAgent {
        fn step(&mut self, env: &mut Context<u8>, _id: usize) -> Result<SchedulingTask, AikaError> {
            let time = env.time;

            if self.messages_sent < self.message_count {
                let msg = Msg::new(
                    self.messages_sent as u8,
                    time,
                    time + 10,
                    self.id,
                    self.target,
                );
                env.send_mail(msg, 0)?;
                self.messages_sent += 1;
            }

            if self.messages_sent < self.message_count {
                Ok(SchedulingTask::Timeout(5))
            } else {
                Ok(SchedulingTask::Wait)
            }
        }
    }

    #[derive(Debug)]
    // Agent that receives and counts messages
    pub struct ReceivingAgent {
        pub _id: usize,
        pub messages_received: Rc<RefCell<Vec<Msg<u8>>>>,
    }

    impl ReceivingAgent {
        pub fn new(_id: usize) -> Self {
            ReceivingAgent {
                _id,
                messages_received: Rc::new(RefCell::new(Vec::new())),
            }
        }
    }

    impl Actor<u8> for ReceivingAgent {
        fn step(
            &mut self,
            _context: &mut Context<u8>,
            _id: usize,
        ) -> Result<SchedulingTask, AikaError> {
            Ok(SchedulingTask::Wait)
        }
    }

    impl ConnectedActor<u8> for ReceivingAgent {
        fn read_message(
            &mut self,
            _env: &mut Context<u8>,
            msg: Msg<u8>,
            _actor_id: usize,
        ) -> Result<(), AikaError> {
            self.messages_received.borrow_mut().push(msg);
            Ok(())
        }
    }

    #[derive(Debug)]
    // Agent that broadcasts messages
    pub struct BroadcastingAgent {
        pub id: usize,
        pub broadcast_count: usize,
        pub broadcasts_sent: usize,
    }

    impl BroadcastingAgent {
        pub fn new(id: usize, broadcast_count: usize) -> Self {
            BroadcastingAgent {
                id,
                broadcast_count,
                broadcasts_sent: 0,
            }
        }
    }

    impl Actor<u8> for BroadcastingAgent {
        fn step(
            &mut self,
            context: &mut Context<u8>,
            _id: usize,
        ) -> Result<SchedulingTask, AikaError> {
            let time = context.time;

            if self.broadcasts_sent < self.broadcast_count {
                let msg = Msg::new(
                    (100 + self.broadcasts_sent) as u8,
                    time,
                    time + 5,
                    self.id,
                    usize::MAX, // None means broadcast
                );
                context.send_mail(msg, 0)?;
                self.broadcasts_sent += 1;
            }

            if self.broadcasts_sent < self.broadcast_count {
                Ok(SchedulingTask::Timeout(10))
            } else {
                Ok(SchedulingTask::Wait)
            }
        }
    }

    #[derive(Debug)]
    // Agent that triggers other actors
    pub struct TriggeringAgent {
        pub _id: usize,
        pub target: usize,
        pub trigger_times: Vec<u64>,
        pub trigger_index: usize,
    }

    impl TriggeringAgent {
        pub fn new(_id: usize, target: usize, trigger_times: Vec<u64>) -> Self {
            TriggeringAgent {
                _id,
                target,
                trigger_times,
                trigger_index: 0,
            }
        }
    }

    impl Actor<u8> for TriggeringAgent {
        fn step(
            &mut self,
            _context: &mut Context<u8>,
            _id: usize,
        ) -> Result<SchedulingTask, AikaError> {
            if self.trigger_index < self.trigger_times.len() {
                let trigger_time = self.trigger_times[self.trigger_index];
                self.trigger_index += 1;
                return Ok(SchedulingTask::Trigger {
                    time: trigger_time,
                    idx: self.target,
                });
            }

            Ok(SchedulingTask::Wait)
        }
    }

    #[test]
    fn test_world_macro() {
        world!(u8)(Stateless).unwrap();
        world!(u8, CLOCK_SLOTS = 256)(Stateless).unwrap();
        world!(u8, CLOCK_HEIGHT = 1)(Stateless).unwrap();
        world!(u8, CLOCK_SLOTS = 64, CLOCK_HEIGHT = 4)(Stateless).unwrap();
    }

    #[test]
    fn test_run() {
        let mut world = LonePlanet::<128, 1, u8>::init(Stateless).unwrap();
        world.set_terminal_time(400000);
        let actor_test = TestAgent::new(0);
        world.spawn_actor(actor_test);
        world.schedule(1, 0).unwrap();
        world.run().unwrap();
    }

    #[test]
    fn test_simple_message_passing() {
        let mut world = LonePlanet::<128, 1, u8>::init(Stateless).unwrap();
        world.set_terminal_time(100);

        // Create sender and receiver
        let sender = SendingAgent::new(0, 1, 3);
        let receiver = ReceivingAgent::new(1);
        let received_messages = receiver.messages_received.clone();

        world.spawn_actor(sender);
        world.spawn_receiver_actor(receiver);

        // Schedule both actors to start
        world.schedule(1, 0).unwrap();
        world.schedule(1, 1).unwrap();

        world.run().unwrap();

        // Check that messages were received
        let messages = received_messages.borrow();
        assert_eq!(messages.len(), 3);
        for (i, msg) in messages.iter().enumerate() {
            assert_eq!(msg.data, i as u8);
            assert_eq!(msg.from.1, 0);
            assert_eq!(msg.to.1, 1);
        }
    }

    #[test]
    fn test_broadcast_messages() {
        let mut world = LonePlanet::<128, 1, u8>::init(Stateless).unwrap();
        world.set_terminal_time(100);
        // Create one broadcaster and two receivers
        let broadcaster = BroadcastingAgent::new(0, 2);
        let receiver1 = ReceivingAgent::new(1);
        let receiver2 = ReceivingAgent::new(2);

        let received1 = receiver1.messages_received.clone();
        let received2 = receiver2.messages_received.clone();

        world.spawn_actor(broadcaster);
        world.spawn_receiver_actor(receiver1);
        world.spawn_receiver_actor(receiver2);

        // Schedule all actors
        world.schedule(1, 0).unwrap();
        world.schedule(1, 1).unwrap();
        world.schedule(1, 2).unwrap();

        world.run().unwrap();

        // Both receivers should get the broadcasts
        let messages1 = received1.borrow();
        let messages2 = received2.borrow();

        assert_eq!(messages1.len(), 2);
        assert_eq!(messages2.len(), 2);

        // Verify broadcast content
        for msg in messages1.iter() {
            assert_eq!(msg.from.1, 0);
            assert_eq!(msg.to.1, usize::MAX);
            assert!(msg.data >= 100);
        }
    }

    #[test]
    fn test_actor_triggering() {
        let mut world = LonePlanet::<128, 1, u8>::init(Stateless).unwrap();
        world.set_terminal_time(100);
        // Create a triggering actor that will trigger actor 1 at specific times
        let trigger_times = vec![10, 20, 30];
        let triggerer = TriggeringAgent::new(0, 1, trigger_times);

        // Create a simple actor that will be triggered
        let triggered = TestAgent::new(1);

        world.spawn_actor(triggerer);
        world.spawn_actor(triggered);

        // Only schedule the triggerer initially
        world.schedule(1, 0).unwrap();

        world.run().unwrap();

        // The triggered actor should have run at times 10, 20, and 30
        // We can verify this by checking the clock time advanced past 30
        assert!(world.now() >= 30);
    }

    #[test]
    fn test_multiple_simultaneous_messages() {
        let mut world = LonePlanet::<128, 1, u8>::init(Stateless).unwrap();
        world.set_terminal_time(50);
        // Create multiple senders all targeting the same receiver
        let sender1 = SendingAgent::new(0, 3, 2);
        let sender2 = SendingAgent::new(1, 3, 2);
        let sender3 = SendingAgent::new(2, 3, 2);
        let receiver = ReceivingAgent::new(3);

        let received = receiver.messages_received.clone();

        world.spawn_actor(sender1);
        world.spawn_actor(sender2);
        world.spawn_actor(sender3);
        world.spawn_receiver_actor(receiver);

        // Schedule all actors
        for i in 0..4 {
            world.schedule(1, i as usize).unwrap();
        }
        world.run().unwrap();

        // Should receive 6 messages total (2 from each sender)
        let messages = received.borrow();
        assert_eq!(messages.len(), 6);

        // Count messages from each sender
        let mut from_0 = 0;
        let mut from_1 = 0;
        let mut from_2 = 0;

        for msg in messages.iter() {
            match msg.from.1 {
                0 => from_0 += 1,
                1 => from_1 += 1,
                2 => from_2 += 1,
                _ => panic!("Unexpected sender"),
            }
        }

        assert_eq!(from_0, 2);
        assert_eq!(from_1, 2);
        assert_eq!(from_2, 2);
    }

    #[test]
    fn test_invalid_target_handling() {
        let mut world = LonePlanet::<128, 1, u8>::init(Stateless).unwrap();
        world.set_terminal_time(50);
        // Agent that tries to send to non-existent actor
        #[derive(Debug)]
        pub struct InvalidTargetAgent {
            _id: usize,
            attempted: bool,
        }

        impl Actor<u8> for InvalidTargetAgent {
            fn step(
                &mut self,
                context: &mut Context<u8>,
                id: usize,
            ) -> Result<SchedulingTask, AikaError> {
                let time = context.time;
                self.attempted = true;
                let msg = Msg::new(1, time, time + 5, id, 99);
                context.send_mail(msg, 0)?;

                Ok(SchedulingTask::Wait)
            }
        }

        let sender = InvalidTargetAgent {
            _id: 0,
            attempted: false,
        };

        world.spawn_actor(sender);
        world.schedule(1, 0).unwrap();

        assert_eq!(
            world.run().err().unwrap(),
            AikaError::MessagedNonExistent(1, 99)
        );
    }
}

#[cfg(test)]
mod lets_try_to_break_it {
    use crate::env::Stateless;

    use super::*;

    #[derive(Debug)]
    struct StressActor {
        id: usize,
        send_count: usize,
        max_sends: usize,
        targets: Vec<usize>,
    }

    impl StressActor {
        fn new(id: usize, max_sends: usize, targets: Vec<usize>) -> Self {
            Self {
                id,
                send_count: 0,
                max_sends,
                targets,
            }
        }
    }

    impl Actor<u8> for StressActor {
        fn step(
            &mut self,
            ctx: &mut Context<u8>,
            actor_id: usize,
        ) -> Result<SchedulingTask, AikaError> {
            if self.send_count < self.max_sends {
                // Send to all targets
                for &target in &self.targets {
                    let msg = Msg::new(
                        (self.send_count % 256) as u8,
                        ctx.time,
                        ctx.time + (self.id % 10) as u64 + 1,
                        actor_id,
                        target,
                    );
                    ctx.send_mail(msg, 0)?;
                }
                self.send_count += 1;
                Ok(SchedulingTask::Timeout(1))
            } else {
                Ok(SchedulingTask::Wait)
            }
        }
    }

    impl ConnectedActor<u8> for StressActor {
        fn read_message(
            &mut self,
            ctx: &mut Context<u8>,
            _: Msg<u8>,
            id: usize,
        ) -> Result<(), AikaError> {
            // Trigger more activity on message receipt
            if self.send_count < self.max_sends / 2 {
                self.send_count += 1;
                let _ = self.step(ctx, id)?;
            }
            Ok(())
        }
    }

    #[test]
    fn test_message_storm() {
        let mut world = LonePlanet::<256, 3, u8>::init(Stateless).unwrap();
        world.set_terminal_time(1000);

        // Create fully connected network
        let num_actors = 20;
        for i in 0..num_actors {
            let mut targets = Vec::new();
            for j in 0..num_actors {
                if i != j {
                    targets.push(j);
                }
            }
            world.spawn_receiver_actor(StressActor::new(i, 100, targets));
        }

        // Start all actors
        for i in 0..num_actors {
            world.schedule(1, i).unwrap();
        }

        world.run().unwrap();
    }

    #[test]
    fn test_cascading_triggers() {
        #[derive(Debug)]
        struct CascadeActor {
            triggers_remaining: usize,
        }

        impl Actor<u8> for CascadeActor {
            fn step(
                &mut self,
                ctx: &mut Context<u8>,
                id: usize,
            ) -> Result<SchedulingTask, AikaError> {
                if self.triggers_remaining > 0 {
                    self.triggers_remaining -= 1;
                    let next = (id + 1) % 10;
                    Ok(SchedulingTask::Trigger {
                        time: ctx.time + 1,
                        idx: next,
                    })
                } else {
                    Ok(SchedulingTask::Wait)
                }
            }
        }

        impl ConnectedActor<u8> for CascadeActor {
            fn read_message(
                &mut self,
                _: &mut Context<u8>,
                _: Msg<u8>,
                _: usize,
            ) -> Result<(), AikaError> {
                Ok(())
            }
        }

        let mut world = LonePlanet::<128, 2, u8>::init(Stateless).unwrap();
        world.set_terminal_time(500);

        for _ in 0..10 {
            world.spawn_receiver_actor(CascadeActor {
                triggers_remaining: 50,
            });
        }

        world.schedule(1, 0).unwrap();
        world.run().unwrap();
    }

    #[test]
    fn test_time_boundary_stress() {
        #[derive(Debug)]
        struct BoundaryActor;

        impl Actor<u8> for BoundaryActor {
            fn step(
                &mut self,
                ctx: &mut Context<u8>,
                _id: usize,
            ) -> Result<SchedulingTask, AikaError> {
                // Schedule events at exact time boundaries
                let times = [1, 10, 100, 127, 128, 255, 256, 999, 1000];
                for &t in &times {
                    if t > ctx.time && t <= ctx.terminal {
                        return Ok(SchedulingTask::Schedule(t));
                    }
                }
                Ok(SchedulingTask::Wait)
            }
        }

        impl ConnectedActor<u8> for BoundaryActor {
            fn read_message(
                &mut self,
                _: &mut Context<u8>,
                _: Msg<u8>,
                _: usize,
            ) -> Result<(), AikaError> {
                Ok(())
            }
        }

        let mut world = LonePlanet::<128, 2, u8>::init(Stateless).unwrap();
        world.set_terminal_time(1000);

        for _ in 0..50 {
            world.spawn_receiver_actor(BoundaryActor);
        }

        for i in 0..50 {
            world.schedule(0, i).unwrap();
        }

        world.run().unwrap();
    }

    #[test]
    fn test_broadcast_flood() {
        #[derive(Debug)]
        struct BroadcastActor {
            broadcast_count: usize,
        }

        impl Actor<u8> for BroadcastActor {
            fn step(
                &mut self,
                ctx: &mut Context<u8>,
                id: usize,
            ) -> Result<SchedulingTask, AikaError> {
                if self.broadcast_count < 100 {
                    let msg = Msg::new(
                        self.broadcast_count as u8,
                        ctx.time,
                        ctx.time + 1,
                        id,
                        usize::MAX,
                    );
                    ctx.send_mail(msg, 0)?;
                    self.broadcast_count += 1;
                    Ok(SchedulingTask::Timeout(2))
                } else {
                    Ok(SchedulingTask::Wait)
                }
            }
        }

        impl ConnectedActor<u8> for BroadcastActor {
            fn read_message(
                &mut self,
                _: &mut Context<u8>,
                _: Msg<u8>,
                _: usize,
            ) -> Result<(), AikaError> {
                Ok(())
            }
        }

        let mut world = LonePlanet::<256, 2, u8>::init(Stateless).unwrap();
        world.set_terminal_time(500);

        for _ in 0..30 {
            world.spawn_receiver_actor(BroadcastActor { broadcast_count: 0 });
        }

        for i in 0..30 {
            world.schedule(i as u64, i).unwrap();
        }

        world.run().unwrap();
    }

    #[test]
    fn test_scheduler_overflow() {
        #[derive(Debug)]
        struct OverflowActor;

        impl Actor<u8> for OverflowActor {
            fn step(
                &mut self,
                ctx: &mut Context<u8>,
                _id: usize,
            ) -> Result<SchedulingTask, AikaError> {
                let far_future = ctx.time + 10000;
                if far_future <= ctx.terminal {
                    Ok(SchedulingTask::Schedule(far_future))
                } else {
                    Ok(SchedulingTask::Timeout(1))
                }
            }
        }

        impl ConnectedActor<u8> for OverflowActor {
            fn read_message(
                &mut self,
                _: &mut Context<u8>,
                _: Msg<u8>,
                _: usize,
            ) -> Result<(), AikaError> {
                Ok(())
            }
        }

        let mut world = LonePlanet::<128, 2, u8>::init(Stateless).unwrap();
        world.set_terminal_time(50000);

        for _ in 0..100 {
            world.spawn_receiver_actor(OverflowActor);
        }

        for i in 0..100 {
            world.schedule(1, i).unwrap();
        }

        world.run().unwrap();
    }

    #[test]
    fn test_rapid_self_messaging() {
        #[derive(Debug)]
        struct SelfMessager {
            remaining: usize,
        }

        impl Actor<u8> for SelfMessager {
            fn step(
                &mut self,
                ctx: &mut Context<u8>,
                id: usize,
            ) -> Result<SchedulingTask, AikaError> {
                if self.remaining > 0 {
                    for delay in 1..=5 {
                        let msg = Msg::new(
                            delay as u8,
                            ctx.time,
                            ctx.time + delay,
                            id,
                            id, // Self
                        );
                        ctx.send_mail(msg, 0)?;
                    }
                    self.remaining -= 1;
                }
                Ok(SchedulingTask::Timeout(1))
            }
        }

        impl ConnectedActor<u8> for SelfMessager {
            fn read_message(
                &mut self,
                ctx: &mut Context<u8>,
                _: Msg<u8>,
                id: usize,
            ) -> Result<(), AikaError> {
                // Trigger more self-messages
                if self.remaining > 0 {
                    let _ = self.step(ctx, id)?;
                }
                Ok(())
            }
        }

        let mut world = LonePlanet::<256, 3, u8>::init(Stateless).unwrap();
        world.set_terminal_time(200);

        for _ in 0..10 {
            world.spawn_receiver_actor(SelfMessager { remaining: 50 });
        }

        for i in 0..10 {
            world.schedule(1, i).unwrap();
        }

        world.run().unwrap();
    }

    #[test]
    fn test_zero_delay_messages() {
        #[derive(Debug)]
        struct ZeroDelayActor {
            pings: usize,
        }

        impl Actor<u8> for ZeroDelayActor {
            fn step(
                &mut self,
                ctx: &mut Context<u8>,
                id: usize,
            ) -> Result<SchedulingTask, AikaError> {
                if self.pings < 1000 {
                    // Send zero-delay message
                    let target = (id + 1) % 10;
                    let msg = Msg::new(
                        self.pings as u8,
                        ctx.time,
                        ctx.time, // Zero delay!
                        id,
                        target,
                    );
                    ctx.send_mail(msg, 0)?;
                    self.pings += 1;
                    Ok(SchedulingTask::Timeout(1))
                } else {
                    Ok(SchedulingTask::Wait)
                }
            }
        }

        impl ConnectedActor<u8> for ZeroDelayActor {
            fn read_message(
                &mut self,
                ctx: &mut Context<u8>,
                _: Msg<u8>,
                id: usize,
            ) -> Result<(), AikaError> {
                let _ = self.step(ctx, id)?;
                Ok(())
            }
        }

        let mut world = LonePlanet::<512, 2, u8>::init(Stateless).unwrap();
        world.set_terminal_time(100);

        for _ in 0..10 {
            world.spawn_receiver_actor(ZeroDelayActor { pings: 0 });
        }

        world.schedule(1, 0).unwrap();
        world.run().unwrap();
    }
}