relayrl_framework 0.5.0-beta

A system-oriented multi-agent reinforcement learning framework.
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
//! Runtime actor implementation.
//!
//! Actors own local inference state, trajectory assembly, and the message-handling loop for the
//! client runtime. Transport-backed server inference paths remain experimental in `0.5.0-beta`.

use crate::network::client::agent::{ActorInferenceMode, ClientModes};
#[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
use crate::network::client::runtime::coordination::lifecycle_manager::SharedTransportAddresses;
use crate::network::client::runtime::coordination::state_manager::ActorUuid;
#[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
use crate::network::client::runtime::data::transport_sink::TransportError;
#[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
use crate::network::client::runtime::data::transport_sink::transport_dispatcher::{
    InferenceDispatcher, TrainingDispatcher,
};
use crate::network::client::runtime::router::{
    InferenceRequest, RoutedMessage, RoutedPayload, RoutingProtocol,
};
#[cfg(feature = "metrics")]
use crate::utilities::observability::metrics::MetricsManager;

use relayrl_types::data::action::RelayRLAction;
use relayrl_types::data::tensor::{BackendMatcher, DeviceType};
use relayrl_types::data::trajectory::RelayRLTrajectory;
use relayrl_types::model::utils::{deserialize_model_module, validate_module};
use relayrl_types::model::{HotReloadableModel, ModelError, ModelModule};
use relayrl_types::prelude::tensor::relayrl::AnyBurnTensor;

use std::path::PathBuf;
use std::sync::Arc;
#[cfg(feature = "metrics")]
use std::time::Instant;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::oneshot;

use burn_tensor::backend::Backend;
use thiserror::Error;

/// Shared handle to a hot-reloadable model.
///
/// The outer `Arc<RwLock<Option<...>>>` enables two ownership modes:
/// - **Independent**: each actor holds its own `Arc`, wrapping its own model.
/// - **Shared**: all actors on the same device hold a clone of the *same* `Arc`, so
///   a write through any one actor (handshake / model update) is immediately visible
///   to every other actor that shares it.
pub(crate) type LocalModelHandle<B> = Arc<RwLock<Option<HotReloadableModel<B>>>>;

#[derive(Debug, Error)]
#[allow(clippy::enum_variant_names)]
pub enum ActorError {
    #[error(transparent)]
    ModelError(#[from] ModelError),
    #[error("Trajectory send failed: {0}")]
    TrajectorySendError(String),
    #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
    #[error("Inference request failed: {0}")]
    InferenceRequestError(String),
    #[error("Message handling failed: {0}")]
    MessageHandlingError(String),
    #[error("Type conversion failed: {0}")]
    TypeConversionError(String),
    #[error("System error: {0}")]
    SystemError(String),
    #[error(transparent)]
    UuidPoolError(#[from] active_uuid_registry::UuidPoolError),
    #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
    #[error(transparent)]
    TransportError(#[from] TransportError),
}

pub trait ActorEntity<B: Backend + BackendMatcher<Backend = B>>: Send + Sync + 'static {
    #[allow(clippy::too_many_arguments)]
    async fn new(
        client_namespace: Arc<str>,
        actor_id: ActorUuid,
        device: DeviceType,
        model_handle: LocalModelHandle<B>,
        shared_local_model_path: Arc<RwLock<PathBuf>>,
        shared_max_traj_length: Arc<RwLock<usize>>,
        #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
        shared_inference_dispatcher: Option<Arc<InferenceDispatcher<B>>>,
        #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
        shared_training_dispatcher: Option<Arc<TrainingDispatcher<B>>>,
        #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
        shared_transport_addresses: Option<Arc<RwLock<SharedTransportAddresses>>>,
        rx_from_router: Receiver<RoutedMessage>,
        shared_tx_to_buffer: Sender<RoutedMessage>,
        shared_client_modes: Arc<ClientModes>,
        #[cfg(feature = "metrics")] metrics: MetricsManager,
    ) -> Self
    where
        Self: Sized;
    async fn spawn_loop(&mut self) -> Result<(), ActorError>;
    async fn initial_model_handshake(&mut self, msg: RoutedMessage) -> Result<(), ActorError>;
    async fn get_model_version(&self, msg: RoutedMessage) -> Result<(), ActorError>;
    async fn refresh_model(&self, msg: RoutedMessage) -> Result<(), ActorError>;
    async fn handle_shutdown(&mut self, _msg: RoutedMessage) -> Result<(), ActorError>;
}

/// Responsible for performing inference with an in-memory model
pub(crate) struct Actor<
    B: Backend + BackendMatcher<Backend = B>,
    const D_IN: usize,
    const D_OUT: usize,
> {
    #[allow(dead_code)]
    client_namespace: Arc<str>,
    actor_id: ActorUuid,
    reloadable_model: LocalModelHandle<B>,
    shared_local_model_path: Arc<RwLock<PathBuf>>,
    shared_max_traj_length: Arc<RwLock<usize>>,
    #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
    shared_inference_dispatcher: Option<Arc<InferenceDispatcher<B>>>,
    #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
    shared_training_dispatcher: Option<Arc<TrainingDispatcher<B>>>,
    #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
    shared_transport_addresses: Option<Arc<RwLock<SharedTransportAddresses>>>,
    model_device: DeviceType,
    current_traj: RelayRLTrajectory,
    rx_from_router: Receiver<RoutedMessage>,
    shared_tx_to_buffer: Sender<RoutedMessage>,
    shared_client_modes: Arc<ClientModes>,
    #[cfg(feature = "metrics")]
    metrics: MetricsManager,
}

impl<B: Backend + BackendMatcher<Backend = B>, const D_IN: usize, const D_OUT: usize>
    Actor<B, D_IN, D_OUT>
{
    #[inline(always)]
    #[allow(clippy::type_complexity)]
    fn extract_inference_request(
        msg: RoutedMessage,
    ) -> Result<
        (
            Arc<AnyBurnTensor<B, D_IN>>,
            Option<Arc<AnyBurnTensor<B, D_OUT>>>,
            f32,
            oneshot::Sender<Arc<RelayRLAction>>,
        ),
        ActorError,
    > {
        let RoutedPayload::RequestInference(req) = msg.payload else {
            return Err(ActorError::MessageHandlingError(
                "Expected RequestInference payload".to_string(),
            ));
        };

        let InferenceRequest {
            observation,
            mask,
            reward,
            reply_to,
        } = *req;

        let obs: Arc<AnyBurnTensor<B, D_IN>> = *observation
            .downcast::<Arc<AnyBurnTensor<B, D_IN>>>()
            .map_err(|_| {
                ActorError::TypeConversionError("Failed to downcast observation".into())
            })?;

        let mask: Option<Arc<AnyBurnTensor<B, D_OUT>>> = *mask
            .downcast::<Option<Arc<AnyBurnTensor<B, D_OUT>>>>()
            .map_err(|_| ActorError::TypeConversionError("Failed to downcast mask".into()))?;

        Ok((obs, mask, reward, reply_to))
    }

    #[inline(always)]
    async fn handle_inference_kind(&mut self, msg: RoutedMessage) -> Result<(), ActorError> {
        match self.shared_client_modes.actor_inference_mode {
            ActorInferenceMode::Local(_) => self.perform_local_inference(msg).await,
            #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
            ActorInferenceMode::Server(_) => self.request_server_inference(msg).await,
        }
    }

    async fn perform_local_inference(&mut self, msg: RoutedMessage) -> Result<(), ActorError> {
        #[cfg(feature = "metrics")]
        let start_time = Instant::now();

        // Clone the Arc so the closure owns an independent reference count.
        // In Shared mode all actors clone the same underlying Arc; in Independent mode
        // each actor has its own Arc.
        let model_handle = self.reloadable_model.clone();
        let (obs, mask, reward, reply_to) = Self::extract_inference_request(msg)?;
        let actor_id = self.actor_id;

        let result = async {
            let r4sa = tokio::task::spawn_blocking(move || -> Result<RelayRLAction, ModelError> {
                let guard = model_handle.blocking_read();
                let reloadable_model = guard.as_ref().ok_or_else(|| {
                    ModelError::IoError(
                        "Model not loaded/available for actor inference".to_string(),
                    )
                })?;
                reloadable_model.forward::<D_IN, D_OUT>(obs, mask, reward, actor_id)
            })
            .await
            .map_err(|e| ActorError::SystemError(format!("spawn_blocking join error: {e}")))?
            .map_err(ActorError::from)?;

            self.current_traj.add_action(r4sa.clone());
            reply_to.send(Arc::new(r4sa)).map_err(|e| {
                ActorError::MessageHandlingError(format!("reply_to send failed: {e:?}"))
            })?;

            Ok(())
        }
        .await;

        #[cfg(feature = "metrics")]
        match &result {
            Ok(()) => {
                let duration = start_time.elapsed().as_secs_f64();
                self.metrics
                    .record_histogram("actor_local_inference_latency", duration, &[])
                    .await;
                self.metrics
                    .record_counter("actor_local_inferences", 1, &[])
                    .await;
            }
            Err(_) => {
                self.metrics
                    .record_counter("actor_local_inference_failures", 1, &[])
                    .await;
            }
        }

        result
    }

    /// Server inference: serialize observation (and optionally mask) and send to server.
    /// Note: if obs/mask live on GPU, you will pay a device->host copy during serialization.
    ///
    /// This path is still experimental in `0.5.0-beta`.
    #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
    async fn request_server_inference(&mut self, msg: RoutedMessage) -> Result<(), ActorError> {
        // Both the inference_kind and inference_dispatcher initializations are based on
        // the client_capabilities.server_inference flag. Thus, if server_inference is true, the inference_dispatcher will be Some
        // and inference_kind will be InferenceKind::Server. The opposite is true: see request_local_inference for the opposite case.
        // If the inference_dispatcher is None, we will use the local model.
        if let Some(inference_dispatcher) = &self.shared_inference_dispatcher {
            // we assume that the transport_addresses are available if the inference_dispatcher is Some
            let shared_transport_addresses = self
                .shared_transport_addresses
                .as_ref()
                .ok_or_else(|| ActorError::SystemError("Server addresses not available".into()))?
                .clone();

            let (obs, _mask, _reward, reply_to) = Self::extract_inference_request(msg)?;

            let obs_bytes: Vec<u8> = Vec::new();
            let _ = obs; // Experimental server inference serialization is not implemented yet.

            let actor_entry = (
                self.client_namespace.to_string(),
                crate::network::ACTOR_CONTEXT.to_string(),
                self.actor_id,
            );
            let r4sa = inference_dispatcher
                .send_inference_request(actor_entry, obs_bytes, shared_transport_addresses)
                .await?;

            self.current_traj.add_action(r4sa.clone());
            reply_to.send(Arc::new(r4sa)).map_err(|e| {
                ActorError::MessageHandlingError(format!("reply_to send failed: {e:?}"))
            })?;
        } else {
            // Fall back to local inference if a server dispatcher is not available.
            return self.perform_local_inference(msg).await;
        }

        Ok(())
    }

    async fn perform_flag_last_action(&mut self, msg: RoutedMessage) -> Result<(), ActorError> {
        if let RoutedPayload::FlagLastInference { reward } = msg.payload {
            #[cfg(feature = "metrics")]
            let start_time = Instant::now();

            let result = async {
                {
                    let actor_id = self.actor_id;
                    let mut last_action =
                        RelayRLAction::new(None, None, None, reward, true, None, Some(actor_id));
                    last_action.update_reward(reward);
                    self.current_traj.add_action(last_action);
                }

                let traj_to_send: RelayRLTrajectory = {
                    let max_traj_length: usize = *self.shared_max_traj_length.read().await;
                    std::mem::replace(
                        &mut self.current_traj,
                        RelayRLTrajectory::new(max_traj_length),
                    )
                };

                let (duration_ms, duration_ns) = {
                    let now: SystemTime = SystemTime::now();
                    let duration = now
                        .duration_since(UNIX_EPOCH)
                        .map_err(|e| ActorError::SystemError(format!("Clock skew: {e}")))?;
                    (duration.as_millis(), duration.as_nanos())
                };

                let send_traj_msg = RoutedMessage {
                    actor_id: self.actor_id,
                    protocol: RoutingProtocol::SendTrajectory,
                    payload: RoutedPayload::SendTrajectory {
                        timestamp: (duration_ms, duration_ns),
                        trajectory: traj_to_send,
                    },
                };

                self.shared_tx_to_buffer
                    .send(send_traj_msg)
                    .await
                    .map_err(|e| ActorError::TrajectorySendError(format!("{e:?}")))?;

                Ok(())
            }
            .await;

            #[cfg(feature = "metrics")]
            match &result {
                Ok(()) => {
                    let duration = start_time.elapsed().as_secs_f64();
                    self.metrics
                        .record_histogram("actor_flag_last_action_latency", duration, &[])
                        .await;
                    self.metrics
                        .record_counter("actor_flag_last_actions", 1, &[])
                        .await;
                }
                Err(_) => {
                    self.metrics
                        .record_counter("actor_flag_last_action_failures", 1, &[])
                        .await;
                }
            }

            return result;
        }
        Ok(())
    }
}

impl<B: Backend + BackendMatcher<Backend = B>, const D_IN: usize, const D_OUT: usize> ActorEntity<B>
    for Actor<B, D_IN, D_OUT>
{
    async fn new(
        client_namespace: Arc<str>,
        actor_id: ActorUuid,
        device: DeviceType,
        model_handle: LocalModelHandle<B>,
        shared_local_model_path: Arc<RwLock<PathBuf>>,
        shared_max_traj_length: Arc<RwLock<usize>>,
        #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
        shared_inference_dispatcher: Option<Arc<InferenceDispatcher<B>>>,
        #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
        shared_training_dispatcher: Option<Arc<TrainingDispatcher<B>>>,
        #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
        shared_transport_addresses: Option<Arc<RwLock<SharedTransportAddresses>>>,
        rx_from_router: Receiver<RoutedMessage>,
        shared_tx_to_buffer: Sender<RoutedMessage>,
        shared_client_modes: Arc<ClientModes>,
        #[cfg(feature = "metrics")] metrics: MetricsManager,
    ) -> Self
    where
        Self: Sized,
    {
        let max_traj_length: usize = *shared_max_traj_length.read().await;

        let model_init_flag = model_handle.read().await.is_none();
        if model_init_flag {
            log::warn!(
                "[ActorEntity] Startup model is None, initial model handshake necessitated..."
            );
        }

        let actor: Actor<B, D_IN, D_OUT> = Self {
            client_namespace,
            actor_id,
            reloadable_model: model_handle,
            shared_local_model_path,
            shared_max_traj_length,
            #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
            shared_inference_dispatcher,
            #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
            shared_training_dispatcher,
            #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
            shared_transport_addresses,
            model_device: device,
            current_traj: RelayRLTrajectory::new(max_traj_length),
            rx_from_router,
            shared_tx_to_buffer,
            shared_client_modes,
            #[cfg(feature = "metrics")]
            metrics,
        };

        actor
    }

    async fn spawn_loop(&mut self) -> Result<(), ActorError> {
        while let Some(msg) = self.rx_from_router.recv().await {
            match msg.protocol {
                RoutingProtocol::ModelHandshake => {
                    self.initial_model_handshake(msg).await?;
                }
                RoutingProtocol::RequestInference => {
                    self.handle_inference_kind(msg).await?;
                }
                RoutingProtocol::FlagLastInference => {
                    self.perform_flag_last_action(msg).await?;
                }
                RoutingProtocol::ModelVersion => {
                    self.get_model_version(msg).await?;
                }
                RoutingProtocol::ModelUpdate => {
                    self.refresh_model(msg).await?;
                }
                RoutingProtocol::Shutdown => {
                    self.handle_shutdown(msg).await?;
                    break;
                }
                _ => {}
            }
        }
        Ok(())
    }

    async fn initial_model_handshake(&mut self, msg: RoutedMessage) -> Result<(), ActorError> {
        if let RoutedPayload::ModelHandshake = msg.payload {
            // Fast path: skip the handshake when a model is already available locally.
            {
                let model_guard = self.reloadable_model.read().await;
                if model_guard.is_some() {
                    log::warn!(
                        "[Actor {:?}] Model already available, handshake not needed",
                        self.actor_id
                    );
                    return Ok(());
                }
            }

            #[cfg(feature = "metrics")]
            let start_time = Instant::now();

            let result: Result<bool, ActorError> = async {
                #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
                {
                    if let Some(training_dispatcher) = &self.shared_training_dispatcher {
                        log::info!(
                            "[Actor {:?}] Starting training model handshake",
                            self.actor_id
                        );

                        let shared_transport_addresses = self
                            .shared_transport_addresses
                            .as_ref()
                            .ok_or_else(|| {
                                ActorError::SystemError("Server addresses not available".into())
                            })?
                            .clone();

                        let actor_entry = (
                            self.client_namespace.to_string(),
                            crate::network::ACTOR_CONTEXT.to_string(),
                            self.actor_id,
                        );

                        match training_dispatcher
                            .initial_model_handshake(actor_entry, shared_transport_addresses)
                            .await
                        {
                            Ok(Some(model)) => {
                                log::info!(
                                    "[Actor {:?}] Model handshake successful, received model data",
                                    self.actor_id
                                );

                                if let Err(e) =
                                    model.save(self.shared_local_model_path.read().await.clone())
                                {
                                    log::error!(
                                        "[Actor {:?}] Failed to save model: {:?}",
                                        self.actor_id,
                                        e
                                    );
                                }

                                let model_path = self.shared_local_model_path.clone();
                                let model_device = self.model_device.clone();
                                let actor_id = self.actor_id;

                                let mut model_guard = self.reloadable_model.write().await;
                                match model_guard.as_ref() {
                                    Some(existing_model) => {
                                        let version = existing_model.version() + 1;
                                        existing_model
                                            .reload_from_path(
                                                model_path.read().await.clone(),
                                                version,
                                            )
                                            .await
                                            .map_err(|e| {
                                                log::error!(
                                                    "[Actor {:?}] Failed to reload model: {:?}",
                                                    actor_id,
                                                    e
                                                );
                                                ActorError::from(e)
                                            })?;
                                    }
                                    None => {
                                        *model_guard = Some(
                                            HotReloadableModel::<B>::new_from_module(
                                                model,
                                                model_device,
                                            )
                                            .await
                                            .map_err(ActorError::from)?,
                                        );
                                    }
                                }

                                Ok(true)
                            }
                            _ => {
                                log::error!(
                                    "[Actor {:?}] Model handshake failed or no model update needed",
                                    self.actor_id
                                );
                                Ok(false)
                            }
                        }
                    } else {
                        log::error!(
                            "[Actor {:?}] No transport dispatcher configured for model handshake",
                            self.actor_id
                        );
                        Ok(false)
                    }
                }

                #[cfg(not(any(feature = "nats-transport", feature = "zmq-transport")))]
                {
                    log::error!(
                        "[Actor {:?}] No transport dispatcher configured for model handshake",
                        self.actor_id
                    );
                    Ok(false)
                }
            }
            .await;

            #[cfg(feature = "metrics")]
            match &result {
                Ok(true) => {
                    let duration = start_time.elapsed().as_secs_f64();
                    self.metrics
                        .record_histogram("actor_model_handshake_latency", duration, &[])
                        .await;
                    self.metrics
                        .record_counter("actor_model_handshakes", 1, &[])
                        .await;
                }
                Ok(false) | Err(_) => {
                    self.metrics
                        .record_counter("actor_model_handshake_failures", 1, &[])
                        .await;
                }
            }

            return result.map(|_| ());
        }

        Ok(())
    }

    async fn get_model_version(&self, msg: RoutedMessage) -> Result<(), ActorError> {
        if let RoutedPayload::ModelVersion { reply_to } = msg.payload {
            let version = {
                let model_guard = self.reloadable_model.read().await;
                match model_guard.as_ref() {
                    Some(model) => model.version(),
                    None => -1,
                }
            };
            reply_to
                .send(version)
                .map_err(|e| ActorError::MessageHandlingError(format!("{:?}", e)))?;
        }

        Ok(())
    }

    async fn refresh_model(&self, msg: RoutedMessage) -> Result<(), ActorError> {
        if let RoutedPayload::ModelUpdate {
            model_bytes,
            version,
        } = msg.payload
        {
            #[cfg(feature = "metrics")]
            let start_time = Instant::now();

            let result: Result<bool, ActorError> = async {
                let model: Result<ModelModule<B>, ModelError> =
                    deserialize_model_module::<B>(model_bytes, self.model_device.clone());
                let model_path: PathBuf = self.shared_local_model_path.read().await.clone();

                if let Ok(ok_model) = model {
                    if let Err(e) = validate_module::<B>(&ok_model).map_err(ActorError::from) {
                        log::error!(
                            "[ActorEntity {:?}] Failed to validate model: {:?}",
                            self.actor_id,
                            e
                        );
                        return Err(e);
                    }

                    if let Err(e) = ok_model.save(&model_path).map_err(ActorError::from) {
                        log::error!(
                            "[ActorEntity {:?}] Failed to save model: {:?}",
                            self.actor_id,
                            e
                        );
                        return Err(e);
                    }

                    // Acquire the outer write lock; in Shared mode this also blocks other actors
                    // from running inference until the swap is complete.
                    let model_device = self.model_device.clone();
                    let mut model_guard = self.reloadable_model.write().await;
                    match model_guard.as_ref() {
                        Some(existing_model) => {
                            existing_model
                                .reload_from_module(ok_model, version)
                                .await
                                .map_err(ActorError::from)?;
                        }
                        None => {
                            // Model handle is empty; initialise it now so the actor can run.
                            *model_guard = Some(
                                HotReloadableModel::<B>::new_from_module(ok_model, model_device)
                                    .await
                                    .map_err(ActorError::from)?,
                            );
                        }
                    }

                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            .await;

            #[cfg(feature = "metrics")]
            match &result {
                Ok(true) => {
                    let duration = start_time.elapsed().as_secs_f64();
                    self.metrics
                        .record_histogram("actor_model_refresh_latency", duration, &[])
                        .await;
                    self.metrics
                        .record_counter("actor_model_refreshes", 1, &[])
                        .await;
                }
                Ok(false) | Err(_) => {
                    self.metrics
                        .record_counter("actor_model_refresh_failures", 1, &[])
                        .await;
                }
            }

            return result.map(|_| ());
        }

        Ok(())
    }

    async fn handle_shutdown(&mut self, _msg: RoutedMessage) -> Result<(), ActorError> {
        if !self.current_traj.actions.is_empty() {
            let send_traj_msg = {
                let traj_to_send: RelayRLTrajectory = {
                    let max_traj_length: usize = *self.shared_max_traj_length.read().await;
                    std::mem::replace(
                        &mut self.current_traj,
                        RelayRLTrajectory::new(max_traj_length),
                    )
                };

                let (duration_ms, duration_ns) = {
                    let now: SystemTime = SystemTime::now();
                    let duration = now
                        .duration_since(UNIX_EPOCH)
                        .map_err(|e| ActorError::SystemError(format!("Clock skew: {}", e)))?;
                    (duration.as_millis(), duration.as_nanos())
                };

                RoutedMessage {
                    actor_id: self.actor_id,
                    protocol: RoutingProtocol::SendTrajectory,
                    payload: RoutedPayload::SendTrajectory {
                        timestamp: (duration_ms, duration_ns),
                        trajectory: traj_to_send,
                    },
                }
            };

            let _ = self.shared_tx_to_buffer.send(send_traj_msg).await;
        }

        active_uuid_registry::interface::remove_id(
            self.client_namespace.as_ref(),
            crate::network::ACTOR_CONTEXT,
            self.actor_id,
        )
        .map_err(ActorError::from)?;

        Ok(())
    }
}

#[cfg(test)]
mod unit_tests {
    use super::*;

    use crate::network::client::agent::{
        ActorInferenceMode, ActorTrainingDataMode, ClientModes, ModelMode,
    };
    use crate::network::client::runtime::coordination::coordinator::CHANNEL_THROUGHPUT;

    use active_uuid_registry::registry_uuid::Uuid;
    use relayrl_types::data::tensor::DeviceType;
    use relayrl_types::data::tensor::NdArrayDType;
    use relayrl_types::prelude::tensor::relayrl::DType;
    use relayrl_types::prelude::tensor::relayrl::FloatBurnTensor;

    use std::path::PathBuf;
    use std::sync::Arc;
    use tokio::sync::{RwLock, mpsc, oneshot};

    use burn_ndarray::NdArray;
    use burn_ndarray::NdArrayDevice;
    use burn_tensor::Tensor;

    type NdArrayBackend = NdArray<f32>;

    const D_IN: usize = 4;
    const D_OUT: usize = 1;

    fn disabled_data_mode() -> Arc<ClientModes> {
        Arc::new(ClientModes {
            actor_inference_mode: ActorInferenceMode::Local(ModelMode::Independent),
            actor_training_data_mode: ActorTrainingDataMode::Disabled,
        })
    }

    fn empty_onnx_model_handle() -> LocalModelHandle<NdArrayBackend> {
        Arc::new(RwLock::new(None))
    }

    async fn create_ndarray_actor(
        max_traj_length: usize,
        device: DeviceType,
    ) -> (
        Actor<NdArrayBackend, D_IN, D_OUT>,
        mpsc::Sender<RoutedMessage>,
        mpsc::Receiver<RoutedMessage>,
    ) {
        let actor_id = Uuid::new_v4();
        let (tx_to_actor, rx_from_router) = mpsc::channel::<RoutedMessage>(CHANNEL_THROUGHPUT);
        let (tx_to_buffer, rx_from_buffer) = mpsc::channel::<RoutedMessage>(CHANNEL_THROUGHPUT);

        let actor = Actor::<NdArrayBackend, D_IN, D_OUT>::new(
            Arc::from("test-actor-namespace"),
            actor_id,
            device,
            empty_onnx_model_handle(),
            Arc::new(RwLock::new(PathBuf::new())),
            Arc::new(RwLock::new(max_traj_length)),
            #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
            None,
            #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
            None,
            #[cfg(any(feature = "nats-transport", feature = "zmq-transport"))]
            None,
            rx_from_router,
            tx_to_buffer,
            disabled_data_mode(),
            #[cfg(feature = "metrics")]
            test_metrics(),
        )
        .await;

        (actor, tx_to_actor, rx_from_buffer)
    }

    fn build_msg(
        actor_id: ActorUuid,
        protocol: RoutingProtocol,
        payload: RoutedPayload,
    ) -> RoutedMessage {
        RoutedMessage {
            actor_id,
            protocol,
            payload,
        }
    }

    #[cfg(feature = "metrics")]
    fn test_metrics() -> MetricsManager {
        MetricsManager::new(
            Arc::new(RwLock::new(("test-actor".to_string(), String::new()))),
            ("test-actor".to_string(), String::new()),
            None,
        )
    }

    #[tokio::test]
    async fn spawn_loop_exits_on_channel_close() {
        let (mut actor, tx, _rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let handle = tokio::spawn(async move { actor.spawn_loop().await });

        drop(tx); // closed channel, loop exits

        let result = tokio::time::timeout(tokio::time::Duration::from_millis(300), handle)
            .await
            .expect("spawn_loop did not exit in time")
            .expect("join error");

        assert!(
            result.is_ok(),
            "spawn_loop did not exit in time on channel close for NdArray, CPU actor"
        );
    }

    #[tokio::test]
    async fn spawn_loop_exits_on_shutdown_message() {
        let (mut actor, tx, _rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let actor_id = actor.actor_id;
        active_uuid_registry::interface::add_id(
            "test-actor-namespace",
            crate::network::ACTOR_CONTEXT,
            actor_id,
        )
        .unwrap();
        let handle = tokio::spawn(async move { actor.spawn_loop().await });

        tx.send(build_msg(
            actor_id,
            RoutingProtocol::Shutdown,
            RoutedPayload::Shutdown,
        ))
        .await
        .unwrap();

        let result = tokio::time::timeout(tokio::time::Duration::from_millis(300), handle)
            .await
            .expect("spawn_loop did not exit on Shutdown")
            .expect("join error");

        assert!(
            result.is_ok(),
            "spawn_loop did not exit on Shutdown for NdArray, CPU actor"
        );
    }

    #[tokio::test]
    async fn get_model_version_returns_minus_one_when_no_model() {
        let (mut actor, tx, _rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let actor_id = actor.actor_id;
        let handle = tokio::spawn(async move { actor.spawn_loop().await });

        let (reply_tx, reply_rx) = oneshot::channel::<i64>();
        tx.send(build_msg(
            actor_id,
            RoutingProtocol::ModelVersion,
            RoutedPayload::ModelVersion { reply_to: reply_tx },
        ))
        .await
        .unwrap();

        let version = tokio::time::timeout(tokio::time::Duration::from_millis(200), reply_rx)
            .await
            .expect("timeout waiting for model version")
            .expect("oneshot cancelled");

        assert_eq!(version, -1, "Unloaded model should report version -1");

        // Shutdown the actor
        tx.send(build_msg(
            actor_id,
            RoutingProtocol::Shutdown,
            RoutedPayload::Shutdown,
        ))
        .await
        .unwrap();
        let _ = handle.await;
    }

    #[tokio::test]
    async fn handle_shutdown_sends_trajectory_when_non_empty() {
        let (mut actor, tx, mut rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let actor_id = actor.actor_id;
        let handle = tokio::spawn(async move { actor.spawn_loop().await });

        // Build trajectory via FlagLastInference (adds a terminal action)
        tx.send(build_msg(
            actor_id,
            RoutingProtocol::FlagLastInference,
            RoutedPayload::FlagLastInference { reward: 1.0 },
        ))
        .await
        .unwrap();

        // Wait for the FlagLastInference to produce a SendTrajectory
        let traj_msg = tokio::time::timeout(tokio::time::Duration::from_millis(200), rx_buf.recv())
            .await
            .expect("timeout waiting for trajectory after FlagLastInference")
            .expect("buffer rx closed");

        assert!(matches!(
            traj_msg.payload,
            RoutedPayload::SendTrajectory { .. }
        ));

        // Now send Shutdown
        tx.send(build_msg(
            actor_id,
            RoutingProtocol::Shutdown,
            RoutedPayload::Shutdown,
        ))
        .await
        .unwrap();
        let _ = handle.await;
    }

    #[tokio::test]
    async fn handle_shutdown_does_not_send_when_empty_traj() {
        let (mut actor, tx, mut rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let actor_id = actor.actor_id;
        let handle = tokio::spawn(async move { actor.spawn_loop().await });

        // Send Shutdown immediately without adding any actions
        tx.send(build_msg(
            actor_id,
            RoutingProtocol::Shutdown,
            RoutedPayload::Shutdown,
        ))
        .await
        .unwrap();
        let _ = handle.await;

        // Buffer should be empty
        assert!(
            rx_buf.try_recv().is_err(),
            "Buffer should be empty after shutdown with no trajectory"
        );
    }

    #[tokio::test]
    async fn flag_last_action_appends_terminal_action_and_sends_traj() {
        let (mut actor, tx, mut rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let actor_id = actor.actor_id;
        let _handle = tokio::spawn(async move { actor.spawn_loop().await });

        tx.send(build_msg(
            actor_id,
            RoutingProtocol::FlagLastInference,
            RoutedPayload::FlagLastInference { reward: 0.0 },
        ))
        .await
        .unwrap();

        let msg = tokio::time::timeout(tokio::time::Duration::from_millis(300), rx_buf.recv())
            .await
            .expect("timeout waiting for trajectory")
            .expect("buffer rx closed");

        assert!(
            matches!(msg.payload, RoutedPayload::SendTrajectory { .. }),
            "FlagLastInference should produce a SendTrajectory message"
        );
    }

    #[tokio::test]
    async fn spawn_loop_can_run_concurrently() {
        let mut handles = Vec::new();

        for _ in 0..3 {
            let (mut actor, tx, _rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
            let actor_id = actor.actor_id;
            active_uuid_registry::interface::add_id(
                "test-actor-namespace",
                crate::network::ACTOR_CONTEXT,
                actor_id,
            )
            .unwrap();
            let h = tokio::spawn(async move { actor.spawn_loop().await });
            // Immediately shut each actor down
            tx.send(build_msg(
                actor_id,
                RoutingProtocol::Shutdown,
                RoutedPayload::Shutdown,
            ))
            .await
            .unwrap();
            handles.push(h);
        }

        for h in handles {
            let result = tokio::time::timeout(tokio::time::Duration::from_millis(500), h)
                .await
                .expect("actor did not shut down in time")
                .expect("join error");
            assert!(result.is_ok());
        }
    }

    #[tokio::test]
    async fn trajectory_send_failure_returns_err() {
        let (mut actor, tx, rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let actor_id = actor.actor_id;

        // Drop buffer receiver so send() fails
        drop(rx_buf);

        let result = actor
            .perform_flag_last_action(build_msg(
                actor_id,
                RoutingProtocol::FlagLastInference,
                RoutedPayload::FlagLastInference { reward: 0.0 },
            ))
            .await;

        assert!(
            matches!(result, Err(ActorError::TrajectorySendError(_))),
            "Expected TrajectorySendError, got {:?}",
            result
        );
        drop(tx);
    }

    #[tokio::test]
    async fn model_version_reply_failure_returns_err() {
        let (actor, _tx, _rx_buf) = create_ndarray_actor(10, DeviceType::Cpu).await;
        let actor_id = actor.actor_id;

        let (reply_tx, reply_rx) = oneshot::channel::<i64>();
        // Drop the receiver side before the actor replies
        drop(reply_rx);

        let result = actor
            .get_model_version(build_msg(
                actor_id,
                RoutingProtocol::ModelVersion,
                RoutedPayload::ModelVersion { reply_to: reply_tx },
            ))
            .await;

        assert!(
            matches!(result, Err(ActorError::MessageHandlingError(_))),
            "Expected MessageHandlingError when oneshot rx is dropped, got {:?}",
            result
        );
    }
}