feagi-agent 0.0.1

Client library for building FEAGI agents in Rust
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
use crate::command_and_control::agent_embodiment_configuration_message::AgentEmbodimentConfigurationMessage;
use crate::command_and_control::agent_registration_message::{
    AgentRegistrationMessage, DeregistrationResponse, RegistrationResponse,
};
use crate::command_and_control::FeagiMessage;
use crate::server::auth::AgentAuth;
use crate::server::wrappers::{
    CommandControlWrapper, MotorTranslator, SensorTranslator, VisualizationTranslator,
};
use crate::{AgentCapabilities, AgentDescriptor, FeagiAgentError};
use feagi_io::traits_and_enums::server::{
    FeagiServerPublisher, FeagiServerPublisherProperties, FeagiServerPuller,
    FeagiServerPullerProperties, FeagiServerRouterProperties,
};
use feagi_io::traits_and_enums::shared::{
    TransportProtocolEndpoint, TransportProtocolImplementation,
};
use feagi_io::AgentID;
use feagi_serialization::FeagiByteContainer;
use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant};
use tracing::{error, info, warn};

type CommandServerIndex = usize;

/// Server-side liveness configuration for command/control sessions.
///
/// `heartbeat_timeout` defines when a client is considered stale if no
/// command/control messages are received.
/// `stale_check_interval` controls how often stale scans run during polling.
#[derive(Debug, Clone)]
pub struct AgentLivenessConfig {
    pub heartbeat_timeout: Duration,
    pub stale_check_interval: Duration,
}

impl Default for AgentLivenessConfig {
    fn default() -> Self {
        Self {
            heartbeat_timeout: Duration::from_secs(30),
            stale_check_interval: Duration::from_secs(1),
        }
    }
}

pub struct FeagiAgentHandler {
    agent_auth_backend: Box<dyn AgentAuth>,
    available_publishers: Vec<Box<dyn FeagiServerPublisherProperties>>,
    available_pullers: Vec<Box<dyn FeagiServerPullerProperties>>,
    command_control_servers: Vec<CommandControlWrapper>,

    all_registered_agents: HashMap<AgentID, (AgentDescriptor, Vec<AgentCapabilities>)>,
    agent_mapping_to_command_control_server_index: HashMap<AgentID, CommandServerIndex>,
    last_activity_by_agent: HashMap<AgentID, Instant>,
    sensors: HashMap<AgentID, SensorTranslator>,
    motors: HashMap<AgentID, MotorTranslator>,
    visualizations: HashMap<AgentID, VisualizationTranslator>,
    sensor_poll_cursor: usize,
    liveness_config: AgentLivenessConfig,
    last_stale_check_at: Instant,

    // this stuff is likely redundant
    // REST STUFF
    /// Device registrations by AgentDescriptor (REST API configuration storage)
    device_registrations_by_descriptor: HashMap<AgentDescriptor, serde_json::Value>,
    /// Agent ID (base64) by AgentDescriptor (for REST→WebSocket bridging)
    agent_id_by_descriptor: HashMap<AgentDescriptor, String>,
    /// Device registrations by AgentID (active connections)
    device_registrations_by_agent: HashMap<AgentID, serde_json::Value>,
}

impl FeagiAgentHandler {
    #[allow(dead_code)]
    fn capabilities_equivalent(
        existing: &[AgentCapabilities],
        requested: &[AgentCapabilities],
    ) -> bool {
        existing.len() == requested.len()
            && existing
                .iter()
                .all(|capability| requested.contains(capability))
    }

    /// Returns true when an existing descriptor session should be replaced by a new registration.
    ///
    /// This suppresses immediate descriptor-replacement churn caused by duplicate
    /// registration packets that arrive within a very short window for the same
    /// live agent session.
    #[allow(dead_code)]
    fn should_replace_existing_descriptor_session(&self, existing_agent_id: AgentID) -> bool {
        let Some(last_seen) = self.last_activity_by_agent.get(&existing_agent_id) else {
            // Missing liveness state is treated as stale and replaceable.
            return true;
        };

        let duplicate_guard_window = self
            .liveness_config
            .stale_check_interval
            .checked_mul(2)
            .unwrap_or(self.liveness_config.stale_check_interval);

        // If the existing session is still very fresh, treat incoming registration
        // as duplicate/in-flight reconnect noise and keep the existing mapping.
        last_seen.elapsed() > duplicate_guard_window
    }

    pub fn new(agent_auth_backend: Box<dyn AgentAuth>) -> FeagiAgentHandler {
        Self::new_with_liveness_config(agent_auth_backend, AgentLivenessConfig::default())
    }

    /// Create a handler with explicit liveness configuration.
    ///
    /// This constructor is preferred in FEAGI runtime code paths where values
    /// come from centralized configuration (`feagi_configuration.toml`).
    pub fn new_with_liveness_config(
        agent_auth_backend: Box<dyn AgentAuth>,
        liveness_config: AgentLivenessConfig,
    ) -> FeagiAgentHandler {
        FeagiAgentHandler {
            agent_auth_backend,
            available_publishers: Vec::new(),
            available_pullers: Vec::new(),

            command_control_servers: Vec::new(),
            all_registered_agents: HashMap::new(),
            agent_mapping_to_command_control_server_index: HashMap::new(),
            last_activity_by_agent: HashMap::new(),
            sensors: Default::default(),
            motors: Default::default(),
            visualizations: Default::default(),
            sensor_poll_cursor: 0,
            liveness_config,
            last_stale_check_at: Instant::now(),

            device_registrations_by_descriptor: HashMap::new(),
            agent_id_by_descriptor: HashMap::new(),
            device_registrations_by_agent: HashMap::new(),
        }
    }

    //region Get Properties

    pub fn get_all_registered_agents(
        &self,
    ) -> &HashMap<AgentID, (AgentDescriptor, Vec<AgentCapabilities>)> {
        &self.all_registered_agents
    }

    pub fn get_all_registered_sensors(&self) -> HashSet<AgentID> {
        self.sensors.keys().cloned().collect()
    }

    pub fn get_all_registered_motors(&self) -> HashSet<AgentID> {
        self.motors.keys().cloned().collect()
    }

    pub fn get_all_registered_visualizations(&self) -> HashSet<AgentID> {
        self.visualizations.keys().cloned().collect()
    }

    /// Register a logical agent entry without transport allocation.
    ///
    /// This utility is intended for deterministic transition/integration tests
    /// that need an active agent session record without starting network servers.
    pub fn register_logical_agent(
        &mut self,
        agent_id: AgentID,
        descriptor: AgentDescriptor,
        capabilities: Vec<AgentCapabilities>,
    ) {
        self.all_registered_agents
            .insert(agent_id, (descriptor, capabilities));
        self.last_activity_by_agent.insert(agent_id, Instant::now());
    }

    /// Forcefully deregister all currently connected agents.
    ///
    /// Returns the removed session IDs (base64) so callers can clear any
    /// runtime subscriptions keyed by session.
    pub fn force_deregister_all_agents(&mut self, reason: &str) -> Vec<String> {
        let ids: Vec<AgentID> = self.all_registered_agents.keys().copied().collect();
        let mut removed_ids = Vec::with_capacity(ids.len());
        for agent_id in ids {
            removed_ids.push(agent_id.to_base64());
            self.deregister_agent_internal(agent_id, reason);
        }
        removed_ids
    }

    pub fn get_command_control_server_info(&self) -> Vec<Box<dyn FeagiServerRouterProperties>> {
        let mut output: Vec<Box<dyn FeagiServerRouterProperties>> = Vec::new();
        for command_control_server in &self.command_control_servers {
            output.push(command_control_server.get_running_server_properties())
        }
        output
    }

    //region  REST

    /// Get device registrations by AgentID
    pub fn get_device_registrations_by_agent(
        &self,
        agent_id: AgentID,
    ) -> Option<&serde_json::Value> {
        self.device_registrations_by_agent.get(&agent_id)
    }

    /// Store device registrations by AgentDescriptor (REST API - before connection)
    /// Also stores the original agent_id for later WebSocket→REST bridging
    pub fn set_device_registrations_by_descriptor(
        &mut self,
        agent_id_base64: String,
        agent_descriptor: AgentDescriptor,
        device_registrations: serde_json::Value,
    ) {
        self.device_registrations_by_descriptor
            .insert(agent_descriptor.clone(), device_registrations);
        self.agent_id_by_descriptor
            .insert(agent_descriptor, agent_id_base64);
    }

    /// Get device registrations by AgentDescriptor (REST API queries)
    pub fn get_device_registrations_by_descriptor(
        &self,
        agent_descriptor: &AgentDescriptor,
    ) -> Option<&serde_json::Value> {
        self.device_registrations_by_descriptor
            .get(agent_descriptor)
    }

    /// Store device registrations by AgentID (active connection)
    pub fn set_device_registrations_by_agent(
        &mut self,
        agent_id: AgentID,
        device_registrations: serde_json::Value,
    ) {
        self.device_registrations_by_agent
            .insert(agent_id, device_registrations);
    }

    // TODO redudant, you can simply check if a AgentID has the capability hash?
    /// Check if a agent has visualization capability configured
    /// Returns (agent_id_base64, rate_hz) for registration with RuntimeService
    pub fn get_visualization_info_for_agent(&self, agent_id: AgentID) -> Option<(String, f64)> {
        let device_regs = self.device_registrations_by_agent.get(&agent_id)?;
        let viz = device_regs.get("visualization")?;
        let rate_hz = viz.get("rate_hz").and_then(|v| v.as_f64())?;

        if rate_hz > 0.0 {
            let agent_descriptor = self.all_registered_agents.get(&agent_id)?;
            let agent_id = self
                .agent_id_by_descriptor
                .get(&agent_descriptor.0)?
                .clone();
            Some((agent_id, rate_hz))
        } else {
            None
        }
    }

    //endregion

    //endregion

    //region Add Servers

    /// Add a poll-based command/control server (ZMQ/WS). The router is wrapped in a
    /// [`CommandControlWrapper`] that only exposes messages.
    pub fn add_and_start_command_control_server(
        &mut self,
        router_property: Box<dyn FeagiServerRouterProperties>,
    ) -> Result<(), FeagiAgentError> {
        let mut router = router_property.as_boxed_server_router();
        router.request_start()?;
        let translator = CommandControlWrapper::new(router);
        self.command_control_servers.push(translator);
        Ok(())
    }

    pub fn add_publisher_server(&mut self, publisher: Box<dyn FeagiServerPublisherProperties>) {
        // TODO check for collisions
        self.available_publishers.push(publisher);
    }

    pub fn add_puller_server(&mut self, puller: Box<dyn FeagiServerPullerProperties>) {
        // TODO check for collisions
        self.available_pullers.push(puller);
    }

    // TODO talk about forcibly starting servers
    /*
    /// Add and start a broadcast publisher server (e.g., visualization on port 9050)
    /// This creates a running server instance that can be polled and broadcast to
    /// NOTE: This does NOT add to available_publishers - broadcast publishers are shared
    pub fn add_and_start_broadcast_publisher(&mut self, publisher_props: Box<dyn FeagiServerPublisherProperties>) -> Result<(), FeagiAgentError> {
        let mut publisher = publisher_props.as_boxed_server_publisher();
        publisher.request_start()?;
        self.broadcast_publishers.push(publisher);
        Ok(())
    }

     */

    //endregion

    //region Command and Control

    /// Poll all command and control servers. Messages for registration request and heartbeat are
    /// handled internally here. Others are raised for FEAGI to act upon
    pub fn poll_command_and_control(
        &mut self,
    ) -> Result<Option<(AgentID, FeagiMessage)>, FeagiAgentError> {
        self.try_prune_stale_agents();
        for (command_index, translator) in self.command_control_servers.iter_mut().enumerate() {
            // TODO smarter error handling. Many things don't deserve a panic
            let possible_message =
                translator.poll_for_incoming_messages(&self.all_registered_agents)?;

            match possible_message {
                None => {
                    continue;
                }
                Some((agent_id, message, is_new_agent)) => {
                    if is_new_agent {
                        return self.handle_messages_from_unknown_agent_ids(
                            agent_id,
                            &message,
                            command_index,
                        );
                    } else {
                        return self.handle_messages_from_known_agent_ids(agent_id, message);
                    }
                }
            }
        }
        // Nothing to report from anyone!
        Ok(None)
    }

    /// Send a command and control message to a specific agent
    pub fn send_message_to_agent(
        &mut self,
        agent_id: AgentID,
        message: FeagiMessage,
        increment_counter: u16,
    ) -> Result<(), FeagiAgentError> {
        let translator_index = match self
            .agent_mapping_to_command_control_server_index
            .get(&agent_id)
        {
            None => {
                return Err(FeagiAgentError::Other(
                    "No such Agent ID exists!".to_string(),
                ))
            }
            Some(index) => index,
        };

        let command_translator = match self.command_control_servers.get_mut(*translator_index) {
            None => {
                panic!("Missing Index for command control server!") // something went wrong
            }
            Some(translator) => translator,
        };
        command_translator.send_message(agent_id, message, increment_counter)
    }

    /// Send a command/control response via the router that received the request.
    ///
    /// This is used for unknown sessions (pre-registration), where agent-to-router
    /// mapping does not exist yet.
    fn send_message_via_command_server(
        &mut self,
        command_server_index: CommandServerIndex,
        session_id: AgentID,
        message: FeagiMessage,
        increment_counter: u16,
    ) -> Result<(), FeagiAgentError> {
        let command_translator = self
            .command_control_servers
            .get_mut(command_server_index)
            .ok_or_else(|| {
                FeagiAgentError::Other("Missing command control server index".to_string())
            })?;
        command_translator.send_message(session_id, message, increment_counter)
    }

    pub fn send_motor_data_to_agent(
        &mut self,
        agent_id: AgentID,
        data: &FeagiByteContainer,
    ) -> Result<(), FeagiAgentError> {
        let motor_translator = self
            .motors
            .get_mut(&agent_id)
            .ok_or_else(|| FeagiAgentError::Other("No Agent ID exists!".to_string()))?;
        motor_translator.poll_and_send_buffered_motor_data(data)?;
        self.refresh_agent_activity(agent_id);
        Ok(())
    }

    pub fn send_visualization_data_to_agent(
        &mut self,
        agent_id: AgentID,
        data: &FeagiByteContainer,
    ) -> Result<(), FeagiAgentError> {
        let visualization_translator = self
            .visualizations
            .get_mut(&agent_id)
            .ok_or_else(|| FeagiAgentError::Other("No Agent ID exists!".to_string()))?;
        visualization_translator.poll_and_send_visualization_data(data)?;
        self.refresh_agent_activity(agent_id);
        Ok(())
    }

    //endregion

    //region Agents

    pub fn poll_agent_sensors(&mut self) -> Result<Option<FeagiByteContainer>, FeagiAgentError> {
        let mut sensor_ids: Vec<AgentID> = self.sensors.keys().copied().collect();
        if sensor_ids.is_empty() {
            return Ok(None);
        }

        sensor_ids.sort_by_key(|agent_id| agent_id.to_base64());
        let count = sensor_ids.len();
        let start = self.sensor_poll_cursor % count;

        for offset in 0..count {
            let idx = (start + offset) % count;
            let agent_id = sensor_ids[idx];
            let polled_data = if let Some(translator) = self.sensors.get_mut(&agent_id) {
                translator.poll_sensor_server()?.cloned()
            } else {
                None
            };

            if let Some(data) = polled_data {
                self.sensor_poll_cursor = (idx + 1) % count;
                self.refresh_agent_activity(agent_id);
                return Ok(Some(data));
            }
        }

        self.sensor_poll_cursor = (start + 1) % count;
        Ok(None)
    }

    pub fn poll_agent_motors(&mut self) -> Result<(), FeagiAgentError> {
        for (_id, translator) in self.motors.iter_mut() {
            translator.poll_motor_server()?;
        }
        Ok(())
    }

    pub fn poll_agent_visualizers(&mut self) -> Result<(), FeagiAgentError> {
        for (_id, translator) in self.visualizations.iter_mut() {
            translator.poll_visualization_server()?;
        }
        Ok(())
    }

    pub fn send_motor_data(
        &mut self,
        agent_id: AgentID,
        motor_data: &FeagiByteContainer,
    ) -> Result<(), FeagiAgentError> {
        let embodiment_option = self.motors.get_mut(&agent_id);
        match embodiment_option {
            Some(embodiment) => {
                embodiment.poll_and_send_buffered_motor_data(motor_data)?;
                self.refresh_agent_activity(agent_id);
                Ok(())
            }
            None => Err(FeagiAgentError::UnableToSendData(
                "Nonexistant Agent ID!".to_string(),
            )),
        }
    }

    /// Send visualization data to a specific agent via dedicated visualization channel
    pub fn send_visualization_data(
        &mut self,
        agent_id: AgentID,
        viz_data: &FeagiByteContainer,
    ) -> Result<(), FeagiAgentError> {
        let embodiment_option = self.visualizations.get_mut(&agent_id);
        match embodiment_option {
            Some(embodiment) => {
                embodiment.poll_and_send_visualization_data(viz_data)?;
                self.refresh_agent_activity(agent_id);
                Ok(())
            }
            None => Err(FeagiAgentError::UnableToSendData(
                "Nonexistant Agent ID!".to_string(),
            )),
        }
    }

    //endregion

    //region Internal

    //region Get property

    fn try_get_puller_property_index(
        &mut self,
        wanted_protocol: &TransportProtocolImplementation,
    ) -> Result<usize, FeagiAgentError> {
        for i in 0..self.available_pullers.len() {
            let available_puller = &self.available_pullers[i];
            if &available_puller
                .get_bind_point()
                .as_transport_protocol_implementation()
                != wanted_protocol
            {
                // not the protocol we are looking for
                continue;
            } else {
                // found the protocol we want
                return Ok(i);
            }
        }
        Err(FeagiAgentError::InitFail(
            "Missing required protocol puller".to_string(),
        ))
    }

    fn try_get_publisher_property_index(
        &mut self,
        wanted_protocol: &TransportProtocolImplementation,
    ) -> Result<usize, FeagiAgentError> {
        for i in 0..self.available_publishers.len() {
            let available_publisher = &self.available_publishers[i];
            if &available_publisher.get_protocol() != wanted_protocol {
                // not the protocol we are looking for
                continue;
            } else {
                // found the protocol we want
                return Ok(i);
            }
        }
        Err(FeagiAgentError::InitFail(
            "Missing required protocol publisher".to_string(),
        ))
    }

    fn try_get_last_publisher_property_index(
        &mut self,
        wanted_protocol: &TransportProtocolImplementation,
    ) -> Result<usize, FeagiAgentError> {
        for i in (0..self.available_publishers.len()).rev() {
            let available_publisher = &self.available_publishers[i];
            if &available_publisher.get_protocol() != wanted_protocol {
                continue;
            } else {
                return Ok(i);
            }
        }
        Err(FeagiAgentError::InitFail(
            "Missing required protocol publisher".to_string(),
        ))
    }

    //endregion

    //region Message Handling

    fn handle_messages_from_unknown_agent_ids(
        &mut self,
        agent_id: AgentID,
        message: &FeagiMessage,
        command_control_index: CommandServerIndex,
    ) -> Result<Option<(AgentID, FeagiMessage)>, FeagiAgentError> {
        match &message {
            FeagiMessage::AgentRegistration(register_message) => {
                match &register_message {
                    AgentRegistrationMessage::ClientRequestRegistration(registration_request) => {
                        info!(
                            target: "feagi-agent",
                            "WS registration request received: session={} descriptor={:?} caps={:?} protocol={:?}",
                            agent_id.to_base64(),
                            registration_request.agent_descriptor(),
                            registration_request.requested_capabilities(),
                            registration_request.connection_protocol()
                        );
                        let auth_result = self
                            .agent_auth_backend
                            .verify_agent_allowed_to_connect(registration_request);
                        if auth_result.is_err() {
                            warn!(
                                target: "feagi-agent",
                                "WS registration rejected by auth backend: session={} descriptor={:?}",
                                agent_id.to_base64(),
                                registration_request.agent_descriptor()
                            );
                            self.send_message_via_command_server(
                                command_control_index,
                                agent_id,
                                FeagiMessage::AgentRegistration(
                                    AgentRegistrationMessage::ServerRespondsRegistration(
                                        RegistrationResponse::FailedInvalidAuth,
                                    ),
                                ),
                                0,
                            )?;
                            return Ok(None);
                        }
                        // auth passed; if the same descriptor is already connected, replace it
                        // first so reconnects can reclaim resources immediately.
                        //
                        // Important: only replace when capability shape is equivalent. This
                        // prevents unrelated clients that share a descriptor string from
                        // evicting each other (for example, a motor/sensor client removing
                        // a live visualization client).
                        if let Some(existing_agent_id) = self
                            .find_agent_id_by_descriptor(registration_request.agent_descriptor())
                        {
                            if let Some((_, existing_capabilities)) =
                                self.all_registered_agents.get(&existing_agent_id)
                            {
                                if !Self::capabilities_equivalent(
                                    existing_capabilities,
                                    registration_request.requested_capabilities(),
                                ) {
                                    info!(
                                        target: "feagi-agent",
                                        "Rejecting descriptor-collision registration for {:?}: existing session {} has different capabilities",
                                        registration_request.agent_descriptor(),
                                        existing_agent_id.to_base64()
                                    );
                                    self.send_message_via_command_server(
                                        command_control_index,
                                        agent_id,
                                        FeagiMessage::AgentRegistration(
                                            AgentRegistrationMessage::ServerRespondsRegistration(
                                                RegistrationResponse::AlreadyRegistered,
                                            ),
                                        ),
                                        0,
                                    )?;
                                    return Ok(None);
                                }
                            }
                            if !self.should_replace_existing_descriptor_session(existing_agent_id) {
                                info!(
                                    target: "feagi-agent",
                                    "Ignoring duplicate registration for descriptor {:?}: existing session {} remains active",
                                    registration_request.agent_descriptor(),
                                    existing_agent_id.to_base64()
                                );
                                self.send_message_via_command_server(
                                    command_control_index,
                                    agent_id,
                                    FeagiMessage::AgentRegistration(
                                        AgentRegistrationMessage::ServerRespondsRegistration(
                                            RegistrationResponse::AlreadyRegistered,
                                        ),
                                    ),
                                    0,
                                )?;
                                return Ok(None);
                            }
                            let replacement_reason = format!(
                                "descriptor replacement by new registration session={}",
                                agent_id.to_base64()
                            );
                            self.deregister_agent_internal(existing_agent_id, &replacement_reason);
                        }

                        // register and always respond deterministically (avoid client timeouts).
                        let mappings = match self.register_agent(
                            agent_id,
                            *registration_request.connection_protocol(),
                            registration_request.requested_capabilities().to_vec(),
                            registration_request.agent_descriptor().clone(),
                            command_control_index,
                        ) {
                            Ok(mappings) => mappings,
                            Err(_) => {
                                error!(
                                    target: "feagi-agent",
                                    "WS registration failed while creating transport mappings: session={} descriptor={:?}",
                                    agent_id.to_base64(),
                                    registration_request.agent_descriptor()
                                );
                                self.send_message_via_command_server(
                                    command_control_index,
                                    agent_id,
                                    FeagiMessage::AgentRegistration(
                                        AgentRegistrationMessage::ServerRespondsRegistration(
                                            RegistrationResponse::FailedInvalidRequest,
                                        ),
                                    ),
                                    0,
                                )?;
                                return Ok(None);
                            }
                        };

                        let mapped_caps: Vec<_> = mappings.keys().cloned().collect();
                        let response = RegistrationResponse::Success(agent_id, mappings);
                        let response_message = FeagiMessage::AgentRegistration(
                            AgentRegistrationMessage::ServerRespondsRegistration(response),
                        );
                        self.send_message_via_command_server(
                            command_control_index,
                            agent_id,
                            response_message,
                            0,
                        )?;
                        info!(
                            target: "feagi-agent",
                            "WS registration success response sent: session={} descriptor={:?} mapped_caps={:?}",
                            agent_id.to_base64(),
                            registration_request.agent_descriptor(),
                            mapped_caps
                        );
                        Ok(None)
                    }
                    AgentRegistrationMessage::ClientRequestDeregistration(_) => {
                        let response = FeagiMessage::AgentRegistration(
                            AgentRegistrationMessage::ServerRespondsDeregistration(
                                DeregistrationResponse::NotRegistered,
                            ),
                        );
                        self.send_message_via_command_server(
                            command_control_index,
                            agent_id,
                            response,
                            0,
                        )?;
                        Ok(None)
                    }
                    _ => {
                        // If not requesting registration, we dont want to hear it
                        Ok(None)
                    }
                }
            }
            _ => {
                // If the new agent is not registering, we don't want to hear it
                Ok(None)
            }
        }
    }

    fn handle_messages_from_known_agent_ids(
        &mut self,
        agent_id: AgentID,
        message: FeagiMessage,
    ) -> Result<Option<(AgentID, FeagiMessage)>, FeagiAgentError> {
        self.refresh_agent_activity(agent_id);
        match &message {
            FeagiMessage::AgentRegistration(register_message) => {
                match register_message {
                    AgentRegistrationMessage::ClientRequestDeregistration(request) => {
                        // Respond first so REQ/REP clients can complete the in-flight request.
                        self.send_message_to_agent(
                            agent_id,
                            FeagiMessage::AgentRegistration(
                                AgentRegistrationMessage::ServerRespondsDeregistration(
                                    DeregistrationResponse::Success,
                                ),
                            ),
                            0,
                        )?;
                        let dereg_reason = request
                            .reason()
                            .map(|text| format!("client request: {}", text))
                            .unwrap_or_else(|| "client request".to_string());
                        self.deregister_agent_internal(agent_id, &dereg_reason);
                        Ok(None)
                    }
                    _ => {
                        // Already registered? dont dont register again
                        // TODO any special exceptions?
                        Ok(None)
                    }
                }
            }
            FeagiMessage::HeartBeat => {
                // We can handle heartbeat here
                // TODO or maybe we should let the higher levels handle it?
                self.send_message_to_agent(agent_id, FeagiMessage::HeartBeat, 0)?;
                Ok(None)
            }
            FeagiMessage::AgentConfiguration(
                AgentEmbodimentConfigurationMessage::AgentConfigurationDetails(device_def),
            ) => {
                let device_regs = serde_json::to_value(device_def).unwrap_or_else(|_| {
                    tracing::warn!(
                        target: "feagi-agent",
                        "Failed to serialize AgentConfigurationDetails to JSON"
                    );
                    serde_json::Value::Object(serde_json::Map::new())
                });
                self.set_device_registrations_by_agent(agent_id, device_regs.clone());
                if let Some((descriptor, _)) = self.all_registered_agents.get(&agent_id) {
                    self.set_device_registrations_by_descriptor(
                        agent_id.to_base64(),
                        descriptor.clone(),
                        device_regs,
                    );
                }
                info!(
                    target: "feagi-agent",
                    "Stored device registrations for agent {}",
                    agent_id.to_base64()
                );
                // Send acknowledgment so REQ/REP clients can complete the request
                self.send_message_to_agent(agent_id, FeagiMessage::HeartBeat, 0)?;
                Ok(None)
            }
            _ => {
                // Throw up anything else
                Ok(Some((agent_id, message)))
            }
        }
    }

    //endregion

    //region Registration

    fn register_agent(
        &mut self,
        agent_id: AgentID,
        wanted_protocol: TransportProtocolImplementation,
        agent_capabilities: Vec<AgentCapabilities>,
        descriptor: AgentDescriptor,
        command_server_index: CommandServerIndex,
    ) -> Result<HashMap<AgentCapabilities, TransportProtocolEndpoint>, FeagiAgentError> {
        // TODO prevent duplicate registration
        /*
        if self.all_registered_agents.contains_key(&agent_id) {
            return Err(FeagiAgentError::ConnectionFailed(
                "Agent Already registered".to_string(),
            ));
        }

         */

        let mut used_puller_indices: Vec<usize> = Vec::new();
        let mut used_publisher_indices: Vec<usize> = Vec::new();
        let mut sensor_servers: Vec<Box<dyn FeagiServerPuller>> = Vec::new();
        let mut motor_servers: Vec<Box<dyn FeagiServerPublisher>> = Vec::new();
        let mut visualizer_servers: Vec<Box<dyn FeagiServerPublisher>> = Vec::new();
        let mut endpoint_mappings: HashMap<AgentCapabilities, TransportProtocolEndpoint> =
            HashMap::new();

        // We try spawning all the servers first without taking any properties out mof circulation
        for agent_capability in &agent_capabilities {
            match agent_capability {
                AgentCapabilities::SendSensorData => {
                    let puller_property_index =
                        self.try_get_puller_property_index(&wanted_protocol)?;
                    let puller_property = &self.available_pullers[puller_property_index];
                    let mut sensor_server = puller_property.as_boxed_server_puller();
                    sensor_server.request_start()?;
                    sensor_servers.push(sensor_server);
                    endpoint_mappings.insert(
                        AgentCapabilities::SendSensorData,
                        puller_property.get_agent_endpoint(),
                    );
                    used_puller_indices.push(puller_property_index);
                }
                AgentCapabilities::ReceiveMotorData => {
                    let publisher_index =
                        self.try_get_publisher_property_index(&wanted_protocol)?;
                    let publisher_property = &self.available_publishers[publisher_index];
                    let mut publisher_server = publisher_property.as_boxed_server_publisher();
                    publisher_server.request_start()?;
                    motor_servers.push(publisher_server);
                    endpoint_mappings.insert(
                        AgentCapabilities::ReceiveMotorData,
                        publisher_property.get_agent_endpoint(),
                    );
                    used_publisher_indices.push(publisher_index);
                }
                AgentCapabilities::ReceiveNeuronVisualizations => {
                    // Prefer the last matching publisher for visualization so motor/viz publishers
                    // configured in order [motor, visualization] map correctly.
                    let publisher_index =
                        self.try_get_last_publisher_property_index(&wanted_protocol)?;
                    let publisher_property = &self.available_publishers[publisher_index];
                    let mut publisher_server = publisher_property.as_boxed_server_publisher();
                    publisher_server.request_start()?;
                    visualizer_servers.push(publisher_server);
                    endpoint_mappings.insert(
                        AgentCapabilities::ReceiveNeuronVisualizations,
                        publisher_property.get_agent_endpoint(),
                    );
                    used_publisher_indices.push(publisher_index);
                }
                AgentCapabilities::ReceiveSystemMessages => {
                    todo!()
                }
            }
        }

        // everything is good, take used properties out of circulation by exact index
        used_puller_indices.sort_unstable();
        used_puller_indices.dedup();
        for idx in used_puller_indices.into_iter().rev() {
            self.available_pullers.remove(idx);
        }

        used_publisher_indices.sort_unstable();
        used_publisher_indices.dedup();
        for idx in used_publisher_indices.into_iter().rev() {
            self.available_publishers.remove(idx);
        }

        // insert the servers into the cache
        for sensor_server in sensor_servers {
            let sensor_translator: SensorTranslator =
                SensorTranslator::new(agent_id, sensor_server);
            self.sensors.insert(agent_id, sensor_translator);
        }

        for motor_server in motor_servers {
            let motor_translator: MotorTranslator = MotorTranslator::new(agent_id, motor_server);
            self.motors.insert(agent_id, motor_translator);
        }

        for visualizer_server in visualizer_servers {
            let visualizer_translator: VisualizationTranslator =
                VisualizationTranslator::new(agent_id, visualizer_server);
            self.visualizations.insert(agent_id, visualizer_translator);
        }

        self.all_registered_agents
            .insert(agent_id, (descriptor, agent_capabilities));
        self.agent_mapping_to_command_control_server_index
            .insert(agent_id, command_server_index);
        self.last_activity_by_agent.insert(agent_id, Instant::now());

        Ok(endpoint_mappings)
    }

    /// Refresh liveness for a known agent based on command/control activity.
    ///
    /// FEAGI treats any valid command/control message as proof of liveness
    /// (not just explicit heartbeat packets).
    fn refresh_agent_activity(&mut self, agent_id: AgentID) {
        self.last_activity_by_agent.insert(agent_id, Instant::now());
    }

    /// Find currently connected agent by descriptor value.
    fn find_agent_id_by_descriptor(&self, descriptor: &AgentDescriptor) -> Option<AgentID> {
        self.all_registered_agents
            .iter()
            .find_map(|(agent_id, (existing_descriptor, _))| {
                if existing_descriptor == descriptor {
                    Some(*agent_id)
                } else {
                    None
                }
            })
    }

    /// Periodically scan and remove stale agents that have exceeded heartbeat timeout.
    fn try_prune_stale_agents(&mut self) {
        if self.last_stale_check_at.elapsed() < self.liveness_config.stale_check_interval {
            return;
        }
        self.last_stale_check_at = Instant::now();

        let stale_ids: Vec<AgentID> = self
            .last_activity_by_agent
            .iter()
            .filter_map(|(agent_id, last_seen)| {
                if last_seen.elapsed() > self.liveness_config.heartbeat_timeout {
                    Some(*agent_id)
                } else {
                    None
                }
            })
            .collect();

        for stale_id in stale_ids {
            let stale_reason = format!(
                "stale heartbeat timeout exceeded ({:.3}s)",
                self.liveness_config.heartbeat_timeout.as_secs_f64()
            );
            self.deregister_agent_internal(stale_id, &stale_reason);
        }
    }

    /// Fully remove an agent and recycle all transport resources.
    ///
    /// This is the single teardown path used by both voluntary and forced
    /// deregistration.
    fn deregister_agent_internal(&mut self, agent_id: AgentID, reason: &str) {
        self.last_activity_by_agent.remove(&agent_id);
        self.agent_mapping_to_command_control_server_index
            .remove(&agent_id);
        let descriptor = self
            .all_registered_agents
            .remove(&agent_id)
            .map(|(descriptor, _)| descriptor);
        let descriptor_text = descriptor
            .as_ref()
            .map(|item| format!("{:?}", item))
            .unwrap_or_else(|| "<unknown-descriptor>".to_string());
        info!(
            target: "feagi-agent",
            "Agent deregistered: agent_id={} descriptor={} reason={}",
            agent_id.to_base64(),
            descriptor_text,
            reason
        );
        self.device_registrations_by_agent.remove(&agent_id);

        if let Some(sensor) = self.sensors.remove(&agent_id) {
            self.available_pullers.push(sensor.into_puller_properties());
        }
        if let Some(motor) = self.motors.remove(&agent_id) {
            self.available_publishers
                .push(motor.into_publisher_properties());
        }
        if let Some(viz) = self.visualizations.remove(&agent_id) {
            self.available_publishers
                .push(viz.into_publisher_properties());
        }

        if let Some(descriptor) = descriptor {
            self.agent_id_by_descriptor.remove(&descriptor);
            // Preserve descriptor-scoped device registrations across session teardown so
            // reconnecting agents can recover motor/sensory mapping state before they
            // resend AgentConfiguration. Session-scoped registrations are still removed.
        }
    }

    //endregion

    //endregion
}