mielin-cells 0.1.0-rc.1

Agent SDK providing agent lifecycle management, policy execution, and inter-agent communication
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
//! Agent Discovery Service
//!
//! Provides service registry, capability-based discovery, location-aware routing,
//! and health checking for agents in a distributed system.

use crate::{AgentId, Version};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::net::SocketAddr;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use thiserror::Error;

/// Discovery-related errors
#[derive(Debug, Error)]
pub enum DiscoveryError {
    #[error("Agent not found: {0}")]
    AgentNotFound(AgentId),
    #[error("Service not found: {0}")]
    ServiceNotFound(String),
    #[error("No healthy agents available for service: {0}")]
    NoHealthyAgents(String),
    #[error("Registration failed: {0}")]
    RegistrationFailed(String),
    #[error("Health check failed: {0}")]
    HealthCheckFailed(String),
}

/// Agent capabilities
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Capability {
    pub name: String,
    pub version: Version,
    #[serde(skip)]
    pub parameters: HashMap<String, String>,
}

impl Hash for Capability {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.version.hash(state);
        // Intentionally skip parameters for hashing
    }
}

impl Capability {
    pub fn new(name: impl Into<String>, version: Version) -> Self {
        Self {
            name: name.into(),
            version,
            parameters: HashMap::new(),
        }
    }

    pub fn with_parameter(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.parameters.insert(key.into(), value.into());
        self
    }
}

/// Geographic location
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Location {
    pub latitude: f64,
    pub longitude: f64,
}

impl Location {
    pub fn new(latitude: f64, longitude: f64) -> Self {
        Self {
            latitude,
            longitude,
        }
    }

    /// Calculate distance to another location in kilometers using Haversine formula
    pub fn distance_to(&self, other: &Location) -> f64 {
        const EARTH_RADIUS_KM: f64 = 6371.0;

        let lat1_rad = self.latitude.to_radians();
        let lat2_rad = other.latitude.to_radians();
        let delta_lat = (other.latitude - self.latitude).to_radians();
        let delta_lon = (other.longitude - self.longitude).to_radians();

        let a = (delta_lat / 2.0).sin().powi(2)
            + lat1_rad.cos() * lat2_rad.cos() * (delta_lon / 2.0).sin().powi(2);
        let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());

        EARTH_RADIUS_KM * c
    }
}

/// Health status
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum HealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
    Unknown,
}

/// Health check result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheck {
    pub status: HealthStatus,
    #[serde(skip, default = "Instant::now")]
    pub last_check: Instant,
    pub message: Option<String>,
    pub metrics: HashMap<String, f64>,
}

impl HealthCheck {
    pub fn healthy() -> Self {
        Self {
            status: HealthStatus::Healthy,
            last_check: Instant::now(),
            message: None,
            metrics: HashMap::new(),
        }
    }

    pub fn degraded(message: impl Into<String>) -> Self {
        Self {
            status: HealthStatus::Degraded,
            last_check: Instant::now(),
            message: Some(message.into()),
            metrics: HashMap::new(),
        }
    }

    pub fn unhealthy(message: impl Into<String>) -> Self {
        Self {
            status: HealthStatus::Unhealthy,
            last_check: Instant::now(),
            message: Some(message.into()),
            metrics: HashMap::new(),
        }
    }

    pub fn with_metric(mut self, key: impl Into<String>, value: f64) -> Self {
        self.metrics.insert(key.into(), value);
        self
    }

    pub fn is_stale(&self, max_age: Duration) -> bool {
        self.last_check.elapsed() > max_age
    }
}

/// Service registration information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceRegistration {
    pub agent_id: AgentId,
    pub service_name: String,
    pub version: Version,
    pub capabilities: HashSet<Capability>,
    pub address: SocketAddr,
    pub location: Option<Location>,
    pub metadata: HashMap<String, String>,
    #[serde(skip, default = "Instant::now")]
    pub registered_at: Instant,
}

impl ServiceRegistration {
    pub fn new(
        agent_id: AgentId,
        service_name: impl Into<String>,
        version: Version,
        address: SocketAddr,
    ) -> Self {
        Self {
            agent_id,
            service_name: service_name.into(),
            version,
            capabilities: HashSet::new(),
            address,
            location: None,
            metadata: HashMap::new(),
            registered_at: Instant::now(),
        }
    }

    pub fn with_capability(mut self, capability: Capability) -> Self {
        self.capabilities.insert(capability);
        self
    }

    pub fn with_location(mut self, location: Location) -> Self {
        self.location = Some(location);
        self
    }

    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
}

/// Discovery query for finding agents
#[derive(Debug, Clone, Default)]
pub struct DiscoveryQuery {
    pub service_name: Option<String>,
    pub capabilities: Vec<Capability>,
    pub version: Option<Version>,
    pub location: Option<Location>,
    pub max_distance_km: Option<f64>,
    pub required_health: Option<HealthStatus>,
    pub max_results: Option<usize>,
}

impl DiscoveryQuery {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn service(mut self, name: impl Into<String>) -> Self {
        self.service_name = Some(name.into());
        self
    }

    pub fn capability(mut self, capability: Capability) -> Self {
        self.capabilities.push(capability);
        self
    }

    pub fn version(mut self, version: Version) -> Self {
        self.version = Some(version);
        self
    }

    pub fn near(mut self, location: Location, max_distance_km: f64) -> Self {
        self.location = Some(location);
        self.max_distance_km = Some(max_distance_km);
        self
    }

    pub fn healthy(mut self) -> Self {
        self.required_health = Some(HealthStatus::Healthy);
        self
    }

    pub fn limit(mut self, max: usize) -> Self {
        self.max_results = Some(max);
        self
    }
}

/// Service discovery result
#[derive(Debug, Clone)]
pub struct DiscoveryResult {
    pub registration: ServiceRegistration,
    pub health: HealthCheck,
    pub distance_km: Option<f64>,
    pub score: f64, // Higher is better
}

impl DiscoveryResult {
    pub fn is_healthy(&self) -> bool {
        matches!(self.health.status, HealthStatus::Healthy)
    }
}

/// Service registry for agent discovery
pub struct ServiceRegistry {
    registrations: Arc<RwLock<HashMap<AgentId, ServiceRegistration>>>,
    health_checks: Arc<RwLock<HashMap<AgentId, HealthCheck>>>,
    service_index: Arc<RwLock<HashMap<String, HashSet<AgentId>>>>,
    capability_index: Arc<RwLock<HashMap<String, HashSet<AgentId>>>>,
    health_check_interval: Duration,
    max_health_age: Duration,
}

impl ServiceRegistry {
    pub fn new() -> Self {
        Self {
            registrations: Arc::new(RwLock::new(HashMap::new())),
            health_checks: Arc::new(RwLock::new(HashMap::new())),
            service_index: Arc::new(RwLock::new(HashMap::new())),
            capability_index: Arc::new(RwLock::new(HashMap::new())),
            health_check_interval: Duration::from_secs(30),
            max_health_age: Duration::from_secs(60),
        }
    }

    pub fn with_health_check_interval(mut self, interval: Duration) -> Self {
        self.health_check_interval = interval;
        self
    }

    pub fn with_max_health_age(mut self, max_age: Duration) -> Self {
        self.max_health_age = max_age;
        self
    }

    /// Register a service
    pub fn register(&self, registration: ServiceRegistration) -> Result<(), DiscoveryError> {
        let agent_id = registration.agent_id;
        let service_name = registration.service_name.clone();

        // Update registrations
        {
            let mut registrations = self
                .registrations
                .write()
                .expect("Lock poisoned: registrations");
            registrations.insert(agent_id, registration.clone());
        }

        // Update service index
        {
            let mut service_index = self
                .service_index
                .write()
                .expect("Lock poisoned: service_index");
            service_index
                .entry(service_name)
                .or_default()
                .insert(agent_id);
        }

        // Update capability index
        {
            let mut capability_index = self
                .capability_index
                .write()
                .expect("Lock poisoned: capability_index");
            for capability in &registration.capabilities {
                capability_index
                    .entry(capability.name.clone())
                    .or_default()
                    .insert(agent_id);
            }
        }

        // Initialize health check
        {
            let mut health_checks = self
                .health_checks
                .write()
                .expect("Lock poisoned: health_checks");
            health_checks.insert(agent_id, HealthCheck::healthy());
        }

        Ok(())
    }

    /// Deregister a service
    pub fn deregister(&self, agent_id: &AgentId) -> Result<(), DiscoveryError> {
        let registration = {
            let mut registrations = self
                .registrations
                .write()
                .expect("Lock poisoned: registrations");
            registrations
                .remove(agent_id)
                .ok_or(DiscoveryError::AgentNotFound(*agent_id))?
        };

        // Remove from service index
        {
            let mut service_index = self
                .service_index
                .write()
                .expect("Lock poisoned: service_index");
            if let Some(agents) = service_index.get_mut(&registration.service_name) {
                agents.remove(agent_id);
                if agents.is_empty() {
                    service_index.remove(&registration.service_name);
                }
            }
        }

        // Remove from capability index
        {
            let mut capability_index = self
                .capability_index
                .write()
                .expect("Lock poisoned: capability_index");
            for capability in &registration.capabilities {
                if let Some(agents) = capability_index.get_mut(&capability.name) {
                    agents.remove(agent_id);
                    if agents.is_empty() {
                        capability_index.remove(&capability.name);
                    }
                }
            }
        }

        // Remove health check
        {
            let mut health_checks = self
                .health_checks
                .write()
                .expect("Lock poisoned: health_checks");
            health_checks.remove(agent_id);
        }

        Ok(())
    }

    /// Update health check for an agent
    pub fn update_health(
        &self,
        agent_id: &AgentId,
        health: HealthCheck,
    ) -> Result<(), DiscoveryError> {
        let mut health_checks = self
            .health_checks
            .write()
            .expect("Lock poisoned: health_checks");
        if health_checks.contains_key(agent_id) {
            health_checks.insert(*agent_id, health);
            Ok(())
        } else {
            Err(DiscoveryError::AgentNotFound(*agent_id))
        }
    }

    /// Get health check for an agent
    pub fn get_health(&self, agent_id: &AgentId) -> Result<HealthCheck, DiscoveryError> {
        let health_checks = self
            .health_checks
            .read()
            .expect("Lock poisoned: health_checks");
        health_checks
            .get(agent_id)
            .cloned()
            .ok_or(DiscoveryError::AgentNotFound(*agent_id))
    }

    /// Discover agents matching a query
    pub fn discover(&self, query: DiscoveryQuery) -> Vec<DiscoveryResult> {
        let registrations = self
            .registrations
            .read()
            .expect("Lock poisoned: registrations");
        let health_checks = self
            .health_checks
            .read()
            .expect("Lock poisoned: health_checks");

        let mut results = Vec::new();

        for (agent_id, registration) in registrations.iter() {
            // Filter by service name
            if let Some(ref service_name) = query.service_name {
                if &registration.service_name != service_name {
                    continue;
                }
            }

            // Filter by version
            if let Some(ref version) = query.version {
                if &registration.version != version {
                    continue;
                }
            }

            // Filter by capabilities
            if !query.capabilities.is_empty() {
                let has_all_capabilities = query.capabilities.iter().all(|cap| {
                    registration
                        .capabilities
                        .iter()
                        .any(|reg_cap| reg_cap.name == cap.name && reg_cap.version >= cap.version)
                });

                if !has_all_capabilities {
                    continue;
                }
            }

            // Calculate distance if location is specified
            let distance_km = if let (Some(query_loc), Some(reg_loc)) =
                (&query.location, &registration.location)
            {
                let dist = query_loc.distance_to(reg_loc);
                if let Some(max_dist) = query.max_distance_km {
                    if dist > max_dist {
                        continue;
                    }
                }
                Some(dist)
            } else {
                None
            };

            // Get health check
            let health = health_checks
                .get(agent_id)
                .cloned()
                .unwrap_or_else(|| HealthCheck {
                    status: HealthStatus::Unknown,
                    last_check: Instant::now(),
                    message: None,
                    metrics: HashMap::new(),
                });

            // Filter by health status
            if let Some(required_health) = query.required_health {
                if health.status != required_health {
                    continue;
                }
            }

            // Skip stale health checks
            if health.is_stale(self.max_health_age) && health.status != HealthStatus::Unknown {
                continue;
            }

            // Calculate score (lower distance is better)
            let distance_score = distance_km.map(|d| 1.0 / (1.0 + d)).unwrap_or(1.0);
            let health_score = match health.status {
                HealthStatus::Healthy => 1.0,
                HealthStatus::Degraded => 0.5,
                HealthStatus::Unhealthy => 0.1,
                HealthStatus::Unknown => 0.3,
            };
            let score = distance_score * health_score;

            results.push(DiscoveryResult {
                registration: registration.clone(),
                health,
                distance_km,
                score,
            });
        }

        // Sort by score (highest first)
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Limit results
        if let Some(max_results) = query.max_results {
            results.truncate(max_results);
        }

        results
    }

    /// Get all services by name
    pub fn get_service(&self, service_name: &str) -> Vec<AgentId> {
        let service_index = self
            .service_index
            .read()
            .expect("Lock poisoned: service_index");
        service_index
            .get(service_name)
            .map(|agents| agents.iter().copied().collect())
            .unwrap_or_default()
    }

    /// Get all agents with a capability
    pub fn get_by_capability(&self, capability_name: &str) -> Vec<AgentId> {
        let capability_index = self
            .capability_index
            .read()
            .expect("Lock poisoned: capability_index");
        capability_index
            .get(capability_name)
            .map(|agents| agents.iter().copied().collect())
            .unwrap_or_default()
    }

    /// Get all registered services
    pub fn list_services(&self) -> Vec<String> {
        let service_index = self
            .service_index
            .read()
            .expect("Lock poisoned: service_index");
        service_index.keys().cloned().collect()
    }

    /// Get all registered agents
    pub fn list_agents(&self) -> Vec<AgentId> {
        let registrations = self
            .registrations
            .read()
            .expect("Lock poisoned: registrations");
        registrations.keys().copied().collect()
    }

    /// Get registration for an agent
    pub fn get_registration(
        &self,
        agent_id: &AgentId,
    ) -> Result<ServiceRegistration, DiscoveryError> {
        let registrations = self
            .registrations
            .read()
            .expect("Lock poisoned: registrations");
        registrations
            .get(agent_id)
            .cloned()
            .ok_or(DiscoveryError::AgentNotFound(*agent_id))
    }

    /// Get healthy agent count for a service
    pub fn healthy_count(&self, service_name: &str) -> usize {
        let service_index = self
            .service_index
            .read()
            .expect("Lock poisoned: service_index");
        let health_checks = self
            .health_checks
            .read()
            .expect("Lock poisoned: health_checks");

        service_index
            .get(service_name)
            .map(|agents| {
                agents
                    .iter()
                    .filter(|agent_id| {
                        health_checks
                            .get(agent_id)
                            .map(|h| matches!(h.status, HealthStatus::Healthy))
                            .unwrap_or(false)
                    })
                    .count()
            })
            .unwrap_or(0)
    }
}

impl Default for ServiceRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Load balancer for routing requests to agents
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoadBalancingStrategy {
    RoundRobin,
    Random,
    LeastConnections,
    LocationBased,
}

pub struct LoadBalancer {
    registry: Arc<ServiceRegistry>,
    strategy: LoadBalancingStrategy,
    current_index: Arc<RwLock<HashMap<String, usize>>>,
    connection_counts: Arc<RwLock<HashMap<AgentId, usize>>>,
}

impl LoadBalancer {
    pub fn new(registry: Arc<ServiceRegistry>, strategy: LoadBalancingStrategy) -> Self {
        Self {
            registry,
            strategy,
            current_index: Arc::new(RwLock::new(HashMap::new())),
            connection_counts: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Select an agent for a service
    pub fn select(
        &self,
        service_name: &str,
        location: Option<Location>,
    ) -> Result<AgentId, DiscoveryError> {
        let mut query = DiscoveryQuery::new().service(service_name).healthy();

        if let Some(loc) = location {
            query = query.near(loc, f64::MAX);
        }

        let results = self.registry.discover(query);

        if results.is_empty() {
            return Err(DiscoveryError::NoHealthyAgents(service_name.to_string()));
        }

        let agent_id = match self.strategy {
            LoadBalancingStrategy::RoundRobin => {
                let mut current_index = self
                    .current_index
                    .write()
                    .expect("Lock poisoned: current_index");
                let index = current_index.entry(service_name.to_string()).or_insert(0);
                let selected = results[*index % results.len()].registration.agent_id;
                *index = (*index + 1) % results.len();
                selected
            }
            LoadBalancingStrategy::Random => {
                use rand::Rng;
                let mut rng = rand::rng();
                let index = rng.random_range(0..results.len());
                results[index].registration.agent_id
            }
            LoadBalancingStrategy::LeastConnections => {
                let connection_counts = self
                    .connection_counts
                    .read()
                    .expect("Lock poisoned: connection_counts");
                results
                    .iter()
                    .min_by_key(|r| {
                        connection_counts
                            .get(&r.registration.agent_id)
                            .copied()
                            .unwrap_or(0)
                    })
                    .map(|r| r.registration.agent_id)
                    .expect("Results is not empty")
            }
            LoadBalancingStrategy::LocationBased => {
                // Already sorted by score which includes distance
                results[0].registration.agent_id
            }
        };

        // Increment connection count
        if matches!(self.strategy, LoadBalancingStrategy::LeastConnections) {
            let mut connection_counts = self
                .connection_counts
                .write()
                .expect("Lock poisoned: connection_counts");
            *connection_counts.entry(agent_id).or_insert(0) += 1;
        }

        Ok(agent_id)
    }

    /// Release a connection (for LeastConnections strategy)
    pub fn release(&self, agent_id: &AgentId) {
        let mut connection_counts = self
            .connection_counts
            .write()
            .expect("Lock poisoned: connection_counts");
        if let Some(count) = connection_counts.get_mut(agent_id) {
            *count = count.saturating_sub(1);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr};

    #[test]
    fn test_location_distance() {
        let tokyo = Location::new(35.6762, 139.6503);
        let osaka = Location::new(34.6937, 135.5023);

        let distance = tokyo.distance_to(&osaka);
        // Distance between Tokyo and Osaka is approximately 400 km
        assert!(distance > 390.0 && distance < 410.0);
    }

    #[test]
    fn test_service_registration() {
        let registry = ServiceRegistry::new();
        let agent_id = AgentId::new_v4();
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        let registration =
            ServiceRegistration::new(agent_id, "test-service", Version::new(1, 0, 0), addr);

        registry.register(registration).unwrap();

        let agents = registry.get_service("test-service");
        assert_eq!(agents.len(), 1);
        assert_eq!(agents[0], agent_id);
    }

    #[test]
    fn test_capability_discovery() {
        let registry = ServiceRegistry::new();
        let agent_id = AgentId::new_v4();
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        let capability = Capability::new("image-processing", Version::new(1, 0, 0));
        let registration =
            ServiceRegistration::new(agent_id, "media-service", Version::new(1, 0, 0), addr)
                .with_capability(capability.clone());

        registry.register(registration).unwrap();

        let query = DiscoveryQuery::new().capability(capability);
        let results = registry.discover(query);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].registration.agent_id, agent_id);
    }

    #[test]
    fn test_location_based_discovery() {
        let registry = ServiceRegistry::new();
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        // Register agent in Tokyo
        let agent1 = AgentId::new_v4();
        let tokyo = Location::new(35.6762, 139.6503);
        let reg1 = ServiceRegistration::new(agent1, "geo-service", Version::new(1, 0, 0), addr)
            .with_location(tokyo);
        registry.register(reg1).unwrap();

        // Register agent in Osaka
        let agent2 = AgentId::new_v4();
        let osaka = Location::new(34.6937, 135.5023);
        let reg2 = ServiceRegistration::new(agent2, "geo-service", Version::new(1, 0, 0), addr)
            .with_location(osaka);
        registry.register(reg2).unwrap();

        // Query from near Tokyo (within 100km)
        let query_location = Location::new(35.7, 139.7);
        let query = DiscoveryQuery::new()
            .service("geo-service")
            .near(query_location, 100.0);

        let results = registry.discover(query);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].registration.agent_id, agent1);
    }

    #[test]
    fn test_health_check() {
        let registry = ServiceRegistry::new();
        let agent_id = AgentId::new_v4();
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        let registration =
            ServiceRegistration::new(agent_id, "health-test", Version::new(1, 0, 0), addr);
        registry.register(registration).unwrap();

        // Initially healthy
        let health = registry.get_health(&agent_id).unwrap();
        assert_eq!(health.status, HealthStatus::Healthy);

        // Update to unhealthy
        registry
            .update_health(&agent_id, HealthCheck::unhealthy("Service error"))
            .unwrap();

        let health = registry.get_health(&agent_id).unwrap();
        assert_eq!(health.status, HealthStatus::Unhealthy);

        // Query should not return unhealthy agents
        let query = DiscoveryQuery::new().service("health-test").healthy();
        let results = registry.discover(query);
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_deregistration() {
        let registry = ServiceRegistry::new();
        let agent_id = AgentId::new_v4();
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        let registration =
            ServiceRegistration::new(agent_id, "temp-service", Version::new(1, 0, 0), addr);
        registry.register(registration).unwrap();

        assert_eq!(registry.get_service("temp-service").len(), 1);

        registry.deregister(&agent_id).unwrap();
        assert_eq!(registry.get_service("temp-service").len(), 0);
    }

    #[test]
    fn test_round_robin_load_balancer() {
        let registry = Arc::new(ServiceRegistry::new());
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        let agent1 = AgentId::new_v4();
        let agent2 = AgentId::new_v4();
        let agent3 = AgentId::new_v4();

        registry
            .register(ServiceRegistration::new(
                agent1,
                "lb-test",
                Version::new(1, 0, 0),
                addr,
            ))
            .unwrap();
        registry
            .register(ServiceRegistration::new(
                agent2,
                "lb-test",
                Version::new(1, 0, 0),
                addr,
            ))
            .unwrap();
        registry
            .register(ServiceRegistration::new(
                agent3,
                "lb-test",
                Version::new(1, 0, 0),
                addr,
            ))
            .unwrap();

        let lb = LoadBalancer::new(registry.clone(), LoadBalancingStrategy::RoundRobin);

        let mut selected = HashSet::new();
        for _ in 0..6 {
            let agent = lb.select("lb-test", None).unwrap();
            selected.insert(agent);
        }

        // Should have selected all 3 agents
        assert_eq!(selected.len(), 3);
    }

    #[test]
    fn test_random_load_balancer() {
        let registry = Arc::new(ServiceRegistry::new());
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        for _ in 0..10 {
            let agent = AgentId::new_v4();
            registry
                .register(ServiceRegistration::new(
                    agent,
                    "random-test",
                    Version::new(1, 0, 0),
                    addr,
                ))
                .unwrap();
        }

        let lb = LoadBalancer::new(registry.clone(), LoadBalancingStrategy::Random);

        let mut selected = HashSet::new();
        for _ in 0..20 {
            let agent = lb.select("random-test", None).unwrap();
            selected.insert(agent);
        }

        // Should have selected multiple different agents
        assert!(selected.len() > 1);
    }

    #[test]
    fn test_least_connections_load_balancer() {
        let registry = Arc::new(ServiceRegistry::new());
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

        let agent1 = AgentId::new_v4();
        let agent2 = AgentId::new_v4();

        registry
            .register(ServiceRegistration::new(
                agent1,
                "lc-test",
                Version::new(1, 0, 0),
                addr,
            ))
            .unwrap();
        registry
            .register(ServiceRegistration::new(
                agent2,
                "lc-test",
                Version::new(1, 0, 0),
                addr,
            ))
            .unwrap();

        let lb = LoadBalancer::new(registry.clone(), LoadBalancingStrategy::LeastConnections);

        // First selection
        let first = lb.select("lc-test", None).unwrap();
        // Second selection should pick the other agent
        let second = lb.select("lc-test", None).unwrap();

        assert_ne!(first, second);

        // Release first connection
        lb.release(&first);

        // Next selection should prefer the first agent again
        let third = lb.select("lc-test", None).unwrap();
        assert_eq!(third, first);
    }
}