adk-gateway 1.0.0

Multi-channel AI gateway for adk-rust agents — Telegram, Slack, WhatsApp, Discord, Matrix + control panel
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
//! Central registry for configured coding agents and their runtime state.
//!
//! Provides thread-safe management of registered coding agents, backend definitions,
//! and real-time status broadcasting via WebSocket events. Supports hot-reload of
//! backend definitions from configuration without restart.

use std::time::Instant;

use chrono::{DateTime, Utc};
use dashmap::DashMap;
use tokio::sync::broadcast;
use tracing::{info, warn};

use super::config::{BackendDefinition, CodingAgentInstanceConfig, CodingAgentsConfig};
use super::error::CodingAgentError;
use super::status::{AgentConnectionStatus, AgentStatusEvent};

/// A registered coding agent instance with its runtime state.
#[derive(Debug, Clone)]
pub struct RegisteredAgent {
    /// Unique identifier for this agent instance.
    pub id: String,
    /// The backend type (references a BackendDefinition).
    pub backend_type: String,
    /// Full configuration for this agent instance.
    pub config: CodingAgentInstanceConfig,
    /// Current connection status.
    pub status: AgentConnectionStatus,
    /// When the last health check was performed.
    pub last_health_check: Option<Instant>,
    /// When the last task completed successfully.
    pub last_successful_task: Option<DateTime<Utc>>,
    /// The ACP endpoint URL for this agent (used to create AcpTool on demand).
    pub endpoint: String,
}

/// Central registry for all configured coding agents.
/// Thread-safe via DashMap, supports hot-reload of backend definitions.
pub struct CodingAgentRegistry {
    /// Registered agents indexed by their unique ID.
    agents: DashMap<String, RegisteredAgent>,
    /// Backend definitions loaded from config, indexed by agent_type.
    backends: DashMap<String, BackendDefinition>,
    /// WebSocket broadcast channel for status update events.
    status_tx: broadcast::Sender<AgentStatusEvent>,
}

impl CodingAgentRegistry {
    /// Create a new empty registry with the given broadcast channel capacity.
    ///
    /// The `channel_capacity` determines how many status events can be buffered
    /// before slow receivers start missing events.
    pub fn new(channel_capacity: usize) -> Self {
        let (status_tx, _) = broadcast::channel(channel_capacity);
        Self {
            agents: DashMap::new(),
            backends: DashMap::new(),
            status_tx,
        }
    }

    /// Create a registry pre-populated from a `CodingAgentsConfig`.
    ///
    /// Loads backend definitions and registers all configured agent instances.
    pub fn from_config(config: &CodingAgentsConfig) -> Self {
        let registry = Self::new(64);
        registry.load_backends_from_config(&config.backends);

        for agent_config in &config.agents {
            if let Err(e) = registry.register_agent(agent_config.clone()) {
                warn!(
                    agent_id = %agent_config.id,
                    error = %e,
                    "Failed to register agent from config"
                );
            }
        }

        registry
    }

    /// Register a new coding agent instance.
    ///
    /// Returns an error if an agent with the same ID is already registered.
    pub fn register_agent(
        &self,
        config: CodingAgentInstanceConfig,
    ) -> Result<(), CodingAgentError> {
        let id = config.id.clone();

        if self.agents.contains_key(&id) {
            return Err(CodingAgentError::ConfigValidation(format!(
                "Agent with id '{}' is already registered",
                id
            )));
        }

        let agent = RegisteredAgent {
            id: id.clone(),
            backend_type: config.backend_type.clone(),
            endpoint: config.endpoint.clone(),
            config,
            status: AgentConnectionStatus::Unknown,
            last_health_check: None,
            last_successful_task: None,
        };

        self.agents.insert(id.clone(), agent);
        info!(agent_id = %id, "Registered coding agent");
        Ok(())
    }

    /// Unregister a coding agent by its ID.
    ///
    /// Returns the removed agent, or an error if not found.
    pub fn unregister_agent(&self, agent_id: &str) -> Result<RegisteredAgent, CodingAgentError> {
        match self.agents.remove(agent_id) {
            Some((_, agent)) => {
                info!(agent_id = %agent_id, "Unregistered coding agent");
                Ok(agent)
            }
            None => Err(CodingAgentError::AgentNotFound(agent_id.to_string())),
        }
    }

    /// Get a clone of a registered agent by its ID.
    pub fn get_agent(&self, agent_id: &str) -> Option<RegisteredAgent> {
        self.agents.get(agent_id).map(|entry| entry.value().clone())
    }

    /// List all registered agents.
    pub fn list_agents(&self) -> Vec<RegisteredAgent> {
        self.agents
            .iter()
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Load (or reload) backend definitions from configuration.
    ///
    /// This replaces all existing backend definitions with the new set.
    /// Existing registered agents are not affected — only the available
    /// backend type catalog is updated.
    pub fn load_backends_from_config(&self, backends: &[BackendDefinition]) {
        // Clear existing backends
        self.backends.clear();

        let mut loaded = 0;
        for backend in backends {
            let missing = super::config::validate_backend_definition(backend);
            if missing.is_empty() {
                self.backends
                    .insert(backend.agent_type.clone(), backend.clone());
                loaded += 1;
            } else {
                warn!(
                    agent_type = %backend.agent_type,
                    missing_fields = ?missing,
                    "Skipping invalid backend definition"
                );
            }
        }

        info!(count = loaded, "Loaded backend definitions from config");
    }

    /// Get a backend definition by its agent type identifier.
    pub fn get_backend(&self, agent_type: &str) -> Option<BackendDefinition> {
        self.backends.get(agent_type).map(|entry| entry.value().clone())
    }

    /// List all loaded backend definitions.
    pub fn list_backends(&self) -> Vec<BackendDefinition> {
        self.backends
            .iter()
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Resolve an agent by its shorthand alias.
    ///
    /// Searches all registered agents for one whose `alias` field matches
    /// the given alias (case-insensitive). Returns the agent ID if found.
    pub fn resolve_by_alias(&self, alias: &str) -> Option<String> {
        let alias_lower = alias.to_lowercase();
        self.agents
            .iter()
            .find(|entry| {
                entry
                    .value()
                    .config
                    .alias
                    .as_ref()
                    .map(|a| a.to_lowercase() == alias_lower)
                    .unwrap_or(false)
            })
            .map(|entry| entry.value().id.clone())
    }

    /// Update the connection status of an agent and emit a status event.
    ///
    /// If the new status differs from the current status, an `AgentStatusEvent`
    /// is broadcast to all subscribers (WebSocket clients).
    ///
    /// Returns an error if the agent is not found.
    pub fn update_status(
        &self,
        agent_id: &str,
        new_status: AgentConnectionStatus,
    ) -> Result<(), CodingAgentError> {
        let mut agent = self
            .agents
            .get_mut(agent_id)
            .ok_or_else(|| CodingAgentError::AgentNotFound(agent_id.to_string()))?;

        let previous_status = agent.status.clone();

        // Only emit event if status actually changed
        if previous_status != new_status {
            let status_label = match &new_status {
                AgentConnectionStatus::Connected => "connected".to_string(),
                AgentConnectionStatus::Disconnected { .. } => "disconnected".to_string(),
                AgentConnectionStatus::Error { message, .. } => format!("error: {}", message),
                AgentConnectionStatus::Unknown => "unknown".to_string(),
            };

            agent.status = new_status.clone();

            let event = AgentStatusEvent {
                agent_id: agent_id.to_string(),
                previous_status,
                new_status,
                timestamp: Utc::now(),
            };

            // Broadcast to subscribers; ignore error if no receivers are listening
            let _ = self.status_tx.send(event);

            info!(
                agent_id = %agent_id,
                status = %status_label,
                "Agent status updated"
            );
        }

        Ok(())
    }

    /// Subscribe to agent status events.
    ///
    /// Returns a broadcast receiver that will receive `AgentStatusEvent` messages
    /// whenever any agent's status changes.
    pub fn subscribe_status(&self) -> broadcast::Receiver<AgentStatusEvent> {
        self.status_tx.subscribe()
    }

    /// Get a reference to the status broadcast sender.
    ///
    /// Useful for components that need to forward the sender without
    /// subscribing themselves.
    pub fn status_sender(&self) -> &broadcast::Sender<AgentStatusEvent> {
        &self.status_tx
    }

    /// Record a successful task completion timestamp for an agent.
    pub fn record_successful_task(&self, agent_id: &str) {
        if let Some(mut agent) = self.agents.get_mut(agent_id) {
            agent.last_successful_task = Some(Utc::now());
        }
    }

    /// Record that a health check was performed for an agent.
    pub fn record_health_check(&self, agent_id: &str) {
        if let Some(mut agent) = self.agents.get_mut(agent_id) {
            agent.last_health_check = Some(Instant::now());
        }
    }

    /// Get the number of registered agents.
    pub fn agent_count(&self) -> usize {
        self.agents.len()
    }

    /// Get the number of loaded backend definitions.
    pub fn backend_count(&self) -> usize {
        self.backends.len()
    }

    /// Reload the registry from a new configuration.
    ///
    /// This is the hot-reload entry point called by the config watcher.
    /// It reloads backend definitions and registers any new agents found
    /// in the config that aren't already registered.
    pub fn reload_from_config(&self, config: &CodingAgentsConfig) {
        // Reload backend definitions
        self.load_backends_from_config(&config.backends);

        // Register any new agents (don't touch existing ones)
        for agent_config in &config.agents {
            if !self.agents.contains_key(&agent_config.id) {
                if let Err(e) = self.register_agent(agent_config.clone()) {
                    warn!(
                        agent_id = %agent_config.id,
                        error = %e,
                        "Failed to register agent during config reload"
                    );
                }
            }
        }

        info!("Config reload complete");
    }
}

/// Allow wrapping in Arc for shared ownership across async tasks.
impl std::fmt::Debug for CodingAgentRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CodingAgentRegistry")
            .field("agent_count", &self.agents.len())
            .field("backend_count", &self.backends.len())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    use super::super::config::{
        AgentCapabilities, AuthMethod, BackendDefinition, CodingAgentInstanceConfig,
    };

    fn sample_agent_config(id: &str, alias: Option<&str>) -> CodingAgentInstanceConfig {
        CodingAgentInstanceConfig {
            id: id.to_string(),
            backend_type: "claude-code".to_string(),
            endpoint: format!("http://localhost:3000/{}", id),
            transport: None,
            workspaces: vec![PathBuf::from("/home/user/projects")],
            timeout_secs: Some(900),
            cost_cap_usd: Some(5.0),
            monthly_budget_usd: None,
            alias: alias.map(|a| a.to_string()),
            auth: None,
        }
    }

    fn sample_backend_definition(agent_type: &str) -> BackendDefinition {
        BackendDefinition {
            agent_type: agent_type.to_string(),
            display_name: format!("{} Display", agent_type),
            cli_command: agent_type.to_string(),
            install_check_command: format!("{} --version", agent_type),
            auth_method: AuthMethod::None,
            capabilities: AgentCapabilities::default(),
            install_instructions: "Install instructions here".to_string(),
            install_instructions_windows: None,
            install_instructions_linux: None,
        }
    }

    #[test]
    fn test_new_registry_is_empty() {
        let registry = CodingAgentRegistry::new(16);
        assert_eq!(registry.agent_count(), 0);
        assert_eq!(registry.backend_count(), 0);
    }

    #[test]
    fn test_register_agent_success() {
        let registry = CodingAgentRegistry::new(16);
        let config = sample_agent_config("agent-1", Some("cc"));

        let result = registry.register_agent(config);
        assert!(result.is_ok());
        assert_eq!(registry.agent_count(), 1);
    }

    #[test]
    fn test_register_agent_duplicate_id_fails() {
        let registry = CodingAgentRegistry::new(16);
        let config = sample_agent_config("agent-1", None);

        registry.register_agent(config.clone()).unwrap();
        let result = registry.register_agent(config);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), CodingAgentError::ConfigValidation(_)));
    }

    #[test]
    fn test_unregister_agent_success() {
        let registry = CodingAgentRegistry::new(16);
        let config = sample_agent_config("agent-1", None);
        registry.register_agent(config).unwrap();

        let result = registry.unregister_agent("agent-1");
        assert!(result.is_ok());
        assert_eq!(registry.agent_count(), 0);
    }

    #[test]
    fn test_unregister_agent_not_found() {
        let registry = CodingAgentRegistry::new(16);
        let result = registry.unregister_agent("nonexistent");
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), CodingAgentError::AgentNotFound(_)));
    }

    #[test]
    fn test_get_agent() {
        let registry = CodingAgentRegistry::new(16);
        let config = sample_agent_config("agent-1", Some("cc"));
        registry.register_agent(config).unwrap();

        let agent = registry.get_agent("agent-1");
        assert!(agent.is_some());
        let agent = agent.unwrap();
        assert_eq!(agent.id, "agent-1");
        assert_eq!(agent.backend_type, "claude-code");
        assert!(matches!(agent.status, AgentConnectionStatus::Unknown));
    }

    #[test]
    fn test_get_agent_not_found() {
        let registry = CodingAgentRegistry::new(16);
        assert!(registry.get_agent("nonexistent").is_none());
    }

    #[test]
    fn test_list_agents() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", None))
            .unwrap();
        registry
            .register_agent(sample_agent_config("agent-2", None))
            .unwrap();

        let agents = registry.list_agents();
        assert_eq!(agents.len(), 2);
    }

    #[test]
    fn test_load_backends_from_config() {
        let registry = CodingAgentRegistry::new(16);
        let backends = vec![
            sample_backend_definition("claude-code"),
            sample_backend_definition("kiro-cli"),
        ];

        registry.load_backends_from_config(&backends);
        assert_eq!(registry.backend_count(), 2);
        assert!(registry.get_backend("claude-code").is_some());
        assert!(registry.get_backend("kiro-cli").is_some());
    }

    #[test]
    fn test_load_backends_skips_invalid() {
        let registry = CodingAgentRegistry::new(16);
        let backends = vec![
            sample_backend_definition("valid-agent"),
            BackendDefinition {
                agent_type: "".to_string(), // invalid: empty agent_type
                display_name: "Invalid".to_string(),
                cli_command: "cmd".to_string(),
                install_check_command: "cmd --version".to_string(),
                auth_method: AuthMethod::None,
                capabilities: AgentCapabilities::default(),
                install_instructions: "".to_string(),
            install_instructions_windows: None,
            install_instructions_linux: None,
            },
        ];

        registry.load_backends_from_config(&backends);
        assert_eq!(registry.backend_count(), 1);
        assert!(registry.get_backend("valid-agent").is_some());
    }

    #[test]
    fn test_load_backends_replaces_existing() {
        let registry = CodingAgentRegistry::new(16);

        // Load initial set
        registry.load_backends_from_config(&vec![sample_backend_definition("old-agent")]);
        assert_eq!(registry.backend_count(), 1);

        // Reload with new set
        registry.load_backends_from_config(&vec![
            sample_backend_definition("new-agent-1"),
            sample_backend_definition("new-agent-2"),
        ]);
        assert_eq!(registry.backend_count(), 2);
        assert!(registry.get_backend("old-agent").is_none());
        assert!(registry.get_backend("new-agent-1").is_some());
    }

    #[test]
    fn test_resolve_by_alias() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("claude-code-1", Some("cc")))
            .unwrap();
        registry
            .register_agent(sample_agent_config("kiro-cli-1", Some("kiro")))
            .unwrap();

        assert_eq!(
            registry.resolve_by_alias("cc"),
            Some("claude-code-1".to_string())
        );
        assert_eq!(
            registry.resolve_by_alias("kiro"),
            Some("kiro-cli-1".to_string())
        );
        assert_eq!(registry.resolve_by_alias("nonexistent"), None);
    }

    #[test]
    fn test_resolve_by_alias_case_insensitive() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", Some("CC")))
            .unwrap();

        assert_eq!(
            registry.resolve_by_alias("cc"),
            Some("agent-1".to_string())
        );
        assert_eq!(
            registry.resolve_by_alias("CC"),
            Some("agent-1".to_string())
        );
        assert_eq!(
            registry.resolve_by_alias("Cc"),
            Some("agent-1".to_string())
        );
    }

    #[test]
    fn test_update_status_emits_event() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", None))
            .unwrap();

        let mut rx = registry.subscribe_status();

        // Update status from Unknown to Connected
        registry
            .update_status("agent-1", AgentConnectionStatus::Connected)
            .unwrap();

        let event = rx.try_recv().unwrap();
        assert_eq!(event.agent_id, "agent-1");
        assert!(matches!(event.previous_status, AgentConnectionStatus::Unknown));
        assert!(matches!(event.new_status, AgentConnectionStatus::Connected));
    }

    #[test]
    fn test_update_status_no_event_on_same_status() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", None))
            .unwrap();

        // First set to Connected
        registry
            .update_status("agent-1", AgentConnectionStatus::Connected)
            .unwrap();

        let mut rx = registry.subscribe_status();

        // Update to same status — should not emit
        registry
            .update_status("agent-1", AgentConnectionStatus::Connected)
            .unwrap();

        assert!(rx.try_recv().is_err());
    }

    #[test]
    fn test_update_status_agent_not_found() {
        let registry = CodingAgentRegistry::new(16);
        let result = registry.update_status("nonexistent", AgentConnectionStatus::Connected);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), CodingAgentError::AgentNotFound(_)));
    }

    #[test]
    fn test_record_successful_task() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", None))
            .unwrap();

        assert!(registry.get_agent("agent-1").unwrap().last_successful_task.is_none());

        registry.record_successful_task("agent-1");

        assert!(registry.get_agent("agent-1").unwrap().last_successful_task.is_some());
    }

    #[test]
    fn test_record_health_check() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", None))
            .unwrap();

        assert!(registry.get_agent("agent-1").unwrap().last_health_check.is_none());

        registry.record_health_check("agent-1");

        assert!(registry.get_agent("agent-1").unwrap().last_health_check.is_some());
    }

    #[test]
    fn test_from_config() {
        let config = CodingAgentsConfig {
            enabled: true,
            max_concurrent_tasks: 3,
            default_timeout_secs: 1800,
            progress_interval_secs: 30,
            agents: vec![
                sample_agent_config("agent-1", Some("cc")),
                sample_agent_config("agent-2", Some("kiro")),
            ],
            backends: vec![sample_backend_definition("claude-code")],
        };

        let registry = CodingAgentRegistry::from_config(&config);
        assert_eq!(registry.agent_count(), 2);
        assert_eq!(registry.backend_count(), 1);
    }

    #[test]
    fn test_reload_from_config_adds_new_agents() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("existing-agent", None))
            .unwrap();

        let config = CodingAgentsConfig {
            enabled: true,
            max_concurrent_tasks: 3,
            default_timeout_secs: 1800,
            progress_interval_secs: 30,
            agents: vec![
                sample_agent_config("existing-agent", None),
                sample_agent_config("new-agent", Some("new")),
            ],
            backends: vec![sample_backend_definition("claude-code")],
        };

        registry.reload_from_config(&config);

        // Existing agent preserved, new agent added
        assert_eq!(registry.agent_count(), 2);
        assert!(registry.get_agent("existing-agent").is_some());
        assert!(registry.get_agent("new-agent").is_some());
    }

    #[test]
    fn test_reload_from_config_does_not_duplicate_existing() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", None))
            .unwrap();

        // Set a status to verify it's preserved
        registry
            .update_status("agent-1", AgentConnectionStatus::Connected)
            .unwrap();

        let config = CodingAgentsConfig {
            enabled: true,
            max_concurrent_tasks: 3,
            default_timeout_secs: 1800,
            progress_interval_secs: 30,
            agents: vec![sample_agent_config("agent-1", None)],
            backends: vec![],
        };

        registry.reload_from_config(&config);

        // Agent count unchanged, status preserved
        assert_eq!(registry.agent_count(), 1);
        let agent = registry.get_agent("agent-1").unwrap();
        assert!(matches!(agent.status, AgentConnectionStatus::Connected));
    }

    #[test]
    fn test_list_backends() {
        let registry = CodingAgentRegistry::new(16);
        registry.load_backends_from_config(&vec![
            sample_backend_definition("claude-code"),
            sample_backend_definition("kiro-cli"),
        ]);

        let backends = registry.list_backends();
        assert_eq!(backends.len(), 2);
    }

    #[test]
    fn test_debug_impl() {
        let registry = CodingAgentRegistry::new(16);
        let debug_str = format!("{:?}", registry);
        assert!(debug_str.contains("CodingAgentRegistry"));
        assert!(debug_str.contains("agent_count"));
        assert!(debug_str.contains("backend_count"));
    }

    #[test]
    fn test_subscribe_multiple_receivers() {
        let registry = CodingAgentRegistry::new(16);
        registry
            .register_agent(sample_agent_config("agent-1", None))
            .unwrap();

        let mut rx1 = registry.subscribe_status();
        let mut rx2 = registry.subscribe_status();

        registry
            .update_status("agent-1", AgentConnectionStatus::Connected)
            .unwrap();

        // Both receivers should get the event
        assert!(rx1.try_recv().is_ok());
        assert!(rx2.try_recv().is_ok());
    }

    #[test]
    fn test_status_sender_accessible() {
        let registry = CodingAgentRegistry::new(16);
        let _sender = registry.status_sender();
        // Just verify it's accessible without panic
    }
}