agentd 0.1.2

Agent daemon for secure capability execution with pluggable isolation backends
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
//! AgentdRuntime - Main orchestration layer
//!
//! This module provides the top-level runtime that coordinates all components
//! of agentd into a cohesive execution environment:
//!
//! - Configuration loading and validation
//! - Isolation backend initialization
//! - Ingest adapter management
//! - Authentication chain setup
//! - Output multiplexing
//! - Sandbox lifecycle management
//! - Policy engine integration
//! - Graceful startup and shutdown

use anyhow::{Context, Result};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{error, info, warn};

use crate::config::agentd::{AgentdConfig, ExecutionProfile};
use crate::core::auth::AuthProvider;
use crate::core::ingest::{
    CapabilityInfo, HealthStatus, IngestAdapter, IntentHandler, OutputChunk, RequestContext,
};
use crate::core::intent::{IntentRequest, IntentResponse, IntentStatus, ResponseTiming};
use crate::core::isolation::IsolationBackend;
use crate::core::output::OutputSink;
use crate::core::sandbox::SandboxManager;
use crate::isolation;

/// AgentdRuntime - The main execution coordinator
pub struct AgentdRuntime {
    /// Configuration
    config: Arc<RwLock<AgentdConfig>>,

    /// Isolation backend
    isolation_backend: Arc<dyn IsolationBackend>,

    /// Ingest adapters
    adapters: Vec<Arc<dyn IngestAdapter>>,

    /// Authentication providers
    auth_providers: Vec<Arc<dyn AuthProvider>>,

    /// Output sinks
    output_sinks: Vec<Arc<dyn OutputSink>>,

    /// Sandbox manager
    sandbox_manager: Arc<dyn SandboxManager>,

    /// Runtime state
    running: AtomicBool,

    /// Shutdown signal
    shutdown_tx: RwLock<Option<tokio::sync::broadcast::Sender<()>>>,
}

impl AgentdRuntime {
    /// Create a new runtime with the given configuration
    pub async fn new(config: AgentdConfig) -> Result<Self> {
        info!(
            "Initializing AgentdRuntime with profile: {:?}",
            config.profile
        );

        // Validate configuration
        config
            .validate()
            .context("Configuration validation failed")?;

        // Ensure work_root exists
        std::fs::create_dir_all(&config.work_root)
            .context("Failed to create work root directory")?;

        // Initialize isolation backend from registry-backed selector.
        let requested_backend = config.isolation.selected_backend_name();
        let isolation_backend = isolation::create_backend(&requested_backend, &config.work_root)
            .with_context(|| {
                format!(
                    "Failed to create configured isolation backend '{}'",
                    requested_backend
                )
            })?;

        // Probe backend capabilities
        let backend_caps = isolation_backend
            .probe()
            .await
            .context("Failed to probe isolation backend")?;
        info!("Isolation backend capabilities: {:?}", backend_caps);

        if matches!(
            isolation::canonical_backend_name(&requested_backend).as_deref(),
            Some("host-direct")
        ) {
            warn!("Using host-direct isolation backend (no kernel isolation)");
        }

        if matches!(
            config.profile,
            ExecutionProfile::Server | ExecutionProfile::Paranoid
        ) && backend_caps.is_soft_isolation()
        {
            anyhow::bail!(
                "Profile {:?} requires kernel isolation, but backend '{}' is soft isolation",
                config.profile,
                backend_caps.name
            );
        }

        // Initialize authentication providers
        let auth_providers = Self::setup_auth_providers(&config).await?;

        // Initialize sandbox manager
        let sandbox_manager =
            Self::setup_sandbox_manager(isolation_backend.clone(), &config).await?;

        // Create the runtime (adapters will be added during start)
        let runtime = Self {
            config: Arc::new(RwLock::new(config)),
            isolation_backend,
            adapters: vec![],
            auth_providers,
            output_sinks: vec![],
            sandbox_manager,
            running: AtomicBool::new(false),
            shutdown_tx: RwLock::new(None),
        };

        Ok(runtime)
    }

    /// Create a runtime with a specific profile preset
    pub async fn with_profile(profile: ExecutionProfile) -> Result<Self> {
        let config = match profile {
            ExecutionProfile::Workstation => AgentdConfig::workstation(),
            ExecutionProfile::Server => AgentdConfig::server(),
            ExecutionProfile::Paranoid => AgentdConfig::paranoid(),
            ExecutionProfile::Custom => AgentdConfig::default(),
        };

        Self::new(config).await
    }

    /// Start the runtime
    pub async fn start(&mut self) -> Result<()> {
        if self.running.load(Ordering::SeqCst) {
            return Err(anyhow::anyhow!("Runtime is already running"));
        }

        info!("Starting AgentdRuntime");

        // Create shutdown channel
        let (shutdown_tx, _) = tokio::sync::broadcast::channel(1);
        {
            let mut tx = self.shutdown_tx.write().await;
            *tx = Some(shutdown_tx);
        }

        // Start adapters
        // Clone config to avoid borrow conflict with &mut self
        let config = self.config.read().await.clone();
        self.start_adapters(&config).await?;

        self.running.store(true, Ordering::SeqCst);

        info!("AgentdRuntime started successfully");
        Ok(())
    }

    /// Stop the runtime gracefully
    pub async fn stop(&self) -> Result<()> {
        if !self.running.load(Ordering::SeqCst) {
            return Ok(());
        }

        info!("Stopping AgentdRuntime");

        // Send shutdown signal
        {
            let tx = self.shutdown_tx.read().await;
            if let Some(ref shutdown_tx) = *tx {
                let _ = shutdown_tx.send(());
            }
        }

        // Stop all adapters
        for adapter in &self.adapters {
            if let Err(e) = adapter.stop().await {
                error!("Error stopping adapter {}: {}", adapter.name(), e);
            }
        }

        // Destroy all sandboxes
        if let Err(e) = self.isolation_backend.destroy_all().await {
            error!("Error destroying sandboxes: {}", e);
        }

        info!("AgentdRuntime stopped");
        Ok(())
    }

    /// Reload configuration without full restart
    pub async fn reload(&self, new_config: AgentdConfig) -> Result<()> {
        info!("Reloading AgentdRuntime configuration");

        // Validate new configuration
        new_config
            .validate()
            .context("New configuration validation failed")?;

        // Update stored configuration
        {
            let mut config = self.config.write().await;
            *config = new_config;
        }

        info!("Configuration reloaded successfully");
        Ok(())
    }

    /// Get the current health status
    pub async fn health(&self) -> RuntimeHealth {
        let mut adapter_health = vec![];
        for adapter in &self.adapters {
            let status = adapter.health().await;
            adapter_health.push((adapter.name().to_string(), status));
        }

        let backend_health = self.isolation_backend.health_check().await;

        RuntimeHealth {
            running: self.running.load(Ordering::SeqCst),
            adapter_health,
            backend_healthy: backend_health.is_ok(),
            backend_error: backend_health.err().map(|e| e.to_string()),
        }
    }

    /// Get current configuration (read-only copy)
    pub async fn config(&self) -> AgentdConfig {
        self.config.read().await.clone()
    }

    // Private helper methods

    async fn setup_auth_providers(config: &AgentdConfig) -> Result<Vec<Arc<dyn AuthProvider>>> {
        let mut providers: Vec<Arc<dyn AuthProvider>> = vec![];

        for provider_name in &config.auth.enabled_providers {
            match provider_name.as_str() {
                "allow-all" => {
                    info!("Adding allow-all auth provider (development only)");
                    providers.push(Arc::new(crate::auth::allow_all::AllowAllProvider::new()));
                }
                "jwt" => {
                    info!("Adding JWT auth provider");
                    let jwt_config = crate::auth::jwt::JwtConfig {
                        verification_key: config.auth.jwt.public_key.clone().unwrap_or_default(),
                        issuer: config.auth.jwt.issuer.clone(),
                        audience: config.auth.jwt.audience.clone(),
                        ..Default::default()
                    };
                    providers.push(Arc::new(crate::auth::jwt::JwtProvider::new(jwt_config)));
                }
                "api-key" => {
                    info!("Adding API key auth provider");
                    let provider = crate::auth::api_key::ApiKeyProvider::new();
                    // Register configured static keys
                    for (key, subject) in &config.auth.api_key.static_keys {
                        let entry = crate::auth::api_key::ApiKeyEntry {
                            key_id: format!("static-{}", key.chars().take(8).collect::<String>()),
                            subject: subject.clone(),
                            tenant: None,
                            roles: vec![],
                            trust_level: crate::core::auth::TrustLevel::Standard,
                        };
                        provider.register_key(key, entry).await;
                    }
                    providers.push(Arc::new(provider));
                }
                "signature" => {
                    info!("Adding signature auth provider");
                    providers.push(Arc::new(crate::auth::signature::SignatureProvider::new()));
                }
                "peer-creds" => {
                    info!("Adding peer credentials auth provider");
                    providers.push(Arc::new(crate::auth::peer_creds::PeerCredProvider::new()));
                }
                other => {
                    warn!("Unknown auth provider: {}", other);
                }
            }
        }

        if providers.is_empty() && config.auth.require_auth {
            return Err(anyhow::anyhow!(
                "Authentication required but no providers configured"
            ));
        }

        Ok(providers)
    }

    async fn setup_sandbox_manager(
        backend: Arc<dyn IsolationBackend>,
        config: &AgentdConfig,
    ) -> Result<Arc<dyn SandboxManager>> {
        use crate::core::sandbox::{
            DefaultSandboxManager, PoolConfig, SandboxManagerConfig, SessionTimeouts,
        };

        let manager_config = SandboxManagerConfig {
            max_sandboxes: config.sandbox.pool.max_warm,
            pool: PoolConfig {
                enabled: config.sandbox.pool.enabled,
                min_warm: config.sandbox.pool.min_warm,
                max_warm: config.sandbox.pool.max_warm,
                warm_ttl: std::time::Duration::from_secs(config.sandbox.pool.idle_timeout_secs),
                warm_profiles: vec![match config.profile {
                    ExecutionProfile::Workstation => "workstation",
                    ExecutionProfile::Server => "server",
                    ExecutionProfile::Paranoid => "paranoid",
                    ExecutionProfile::Custom => "custom",
                }
                .to_string()],
            },
            default_timeouts: SessionTimeouts::default(),
            cleanup_interval: std::time::Duration::from_secs(60),
        };

        let manager = DefaultSandboxManager::new(vec![backend], manager_config);

        Ok(Arc::new(manager))
    }

    async fn start_adapters(&mut self, config: &AgentdConfig) -> Result<()> {
        // Create intent handler
        let handler = Arc::new(RuntimeIntentHandler {
            auth_providers: self.auth_providers.clone(),
            sandbox_manager: self.sandbox_manager.clone(),
            config: self.config.clone(),
        });

        // Start gRPC adapter if enabled
        #[cfg(feature = "grpc")]
        if config.adapters.grpc.enabled {
            if let Some(listen_addr) = config.adapters.grpc.listen {
                info!("Starting gRPC adapter on {}", listen_addr);
                let grpc_adapter =
                    Arc::new(crate::adapters::GrpcAdapter::with_address(listen_addr));
                // Wire up the sandbox manager for sandbox lifecycle operations
                grpc_adapter
                    .set_sandbox_manager(self.sandbox_manager.clone())
                    .await;
                grpc_adapter
                    .start(handler.clone())
                    .await
                    .context("Failed to start gRPC adapter")?;
                self.adapters.push(grpc_adapter);
            }
        }

        // TODO: Start NATS adapter if enabled
        // TODO: Start HTTP adapter if enabled
        // TODO: Start Unix socket adapter if enabled

        Ok(())
    }
}

/// Runtime health information
#[derive(Debug, Clone)]
pub struct RuntimeHealth {
    pub running: bool,
    pub adapter_health: Vec<(String, HealthStatus)>,
    pub backend_healthy: bool,
    pub backend_error: Option<String>,
}

impl RuntimeHealth {
    pub fn is_healthy(&self) -> bool {
        self.running
            && self.backend_healthy
            && self.adapter_health.iter().all(|(_, s)| s.is_healthy())
    }
}

/// Intent handler that coordinates auth, sandbox, and execution
struct RuntimeIntentHandler {
    auth_providers: Vec<Arc<dyn AuthProvider>>,
    sandbox_manager: Arc<dyn SandboxManager>,
    config: Arc<RwLock<AgentdConfig>>,
}

#[async_trait::async_trait]
impl IntentHandler for RuntimeIntentHandler {
    async fn handle(&self, request: IntentRequest, ctx: RequestContext) -> Result<IntentResponse> {
        use crate::core::isolation::SandboxSpec;
        use crate::core::sandbox::{RequiredCapabilities, SandboxSelectionOptions};

        let start_time = std::time::Instant::now();
        let received_at = chrono::Utc::now().timestamp_millis() as u64;

        // Authenticate the request
        // TODO: Extract credentials from context and authenticate

        // Build sandbox spec
        let sandbox_spec = SandboxSpec::default();

        // Build sandbox selection options
        let selection_options = SandboxSelectionOptions {
            preferred_id: request
                .sandbox_prefs
                .sandbox_id
                .clone()
                .map(|id| crate::core::sandbox::SandboxId(id)),
            require_fresh: request.sandbox_prefs.require_fresh,
            required_capabilities: RequiredCapabilities::default(),
            preferred_backend: request.sandbox_prefs.backend.clone(),
            required_labels: std::collections::HashMap::new(),
            use_pool: true,
        };

        // Acquire sandbox
        let (_session, sandbox) = self
            .sandbox_manager
            .acquire(&sandbox_spec, &selection_options, &ctx.client_id)
            .await?;

        // Build command from request
        let command = crate::core::intent::Command {
            program: request.capability.clone(),
            args: vec![serde_json::to_string(&request.params)?],
            workdir: None,
            env: std::collections::HashMap::new(),
            inherit_env: false,
            stdin: None,
            timeout: request
                .constraints
                .max_duration_ms
                .map(std::time::Duration::from_millis),
        };

        // Create execution context
        let exec_ctx = crate::core::isolation::ExecContext {
            trace_id: request.metadata.trace_id.clone().unwrap_or_default(),
            request_id: request.id.to_string(),
            workdir: Some(std::path::PathBuf::from("/tmp")),
            extra_env: vec![],
            timeout: request
                .constraints
                .max_duration_ms
                .map(std::time::Duration::from_millis),
            capture_stdout: true,
            capture_stderr: true,
            stream_output: false,
        };

        let started_at = chrono::Utc::now().timestamp_millis() as u64;

        // Execute the command
        let result = sandbox.exec(&command, &exec_ctx).await;
        let completed_at = chrono::Utc::now().timestamp_millis() as u64;
        let elapsed = start_time.elapsed();

        match result {
            Ok(output) => {
                let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
                let stderr_str = String::from_utf8_lossy(&output.stderr).to_string();

                Ok(IntentResponse {
                    request_id: request.id,
                    status: if output.exit_code == 0 {
                        IntentStatus::Ok
                    } else {
                        IntentStatus::Error
                    },
                    code: if output.exit_code == 0 {
                        "OK".to_string()
                    } else {
                        "ERROR".to_string()
                    },
                    message: if output.exit_code == 0 {
                        "Execution completed successfully".to_string()
                    } else {
                        format!("Execution failed with exit code {}", output.exit_code)
                    },
                    result: Some(crate::core::intent::ExecutionResult {
                        exit_code: output.exit_code,
                        stdout: Some(stdout_str),
                        stdout_bytes: Some(output.stdout),
                        stderr: Some(stderr_str),
                        output: None,
                        artifacts: vec![],
                        resource_usage: None,
                    }),
                    error: None,
                    timing: ResponseTiming {
                        received_at_ms: received_at,
                        started_at_ms: started_at,
                        completed_at_ms: completed_at,
                        queue_time_ms: started_at - received_at,
                        setup_time_ms: 0,
                        exec_time_ms: completed_at - started_at,
                        total_time_ms: elapsed.as_millis() as u64,
                    },
                    sandbox_info: None,
                })
            }
            Err(e) => Ok(IntentResponse {
                request_id: request.id,
                status: IntentStatus::Error,
                code: "EXECUTION_ERROR".to_string(),
                message: format!("Execution failed: {}", e),
                result: None,
                error: Some(crate::core::intent::ErrorDetails {
                    code: "EXECUTION_ERROR".to_string(),
                    message: e.to_string(),
                    details: None,
                    retryable: false,
                    retry_after_ms: None,
                }),
                timing: ResponseTiming {
                    received_at_ms: received_at,
                    started_at_ms: started_at,
                    completed_at_ms: completed_at,
                    queue_time_ms: started_at - received_at,
                    setup_time_ms: 0,
                    exec_time_ms: completed_at - started_at,
                    total_time_ms: elapsed.as_millis() as u64,
                },
                sandbox_info: None,
            }),
        }
    }

    async fn handle_streaming(
        &self,
        request: IntentRequest,
        ctx: RequestContext,
        output_tx: tokio::sync::mpsc::Sender<OutputChunk>,
    ) -> Result<IntentResponse> {
        // For now, delegate to non-streaming handler
        // TODO: Implement proper streaming support
        let response = self.handle(request, ctx).await?;
        let _ = output_tx.send(OutputChunk::Done).await;
        Ok(response)
    }

    async fn supports_capability(&self, _capability: &str) -> bool {
        // TODO: Check registered runners
        true
    }

    async fn list_capabilities(&self) -> Vec<CapabilityInfo> {
        // TODO: Return actual registered capabilities
        vec![
            CapabilityInfo {
                name: "fs.read.v1".to_string(),
                description: "Read file contents".to_string(),
                version: 1,
                param_schema: None,
                requires_elevated: false,
                supports_streaming: false,
                tags: vec!["filesystem".to_string()],
            },
            CapabilityInfo {
                name: "shell.exec.v1".to_string(),
                description: "Execute shell command".to_string(),
                version: 1,
                param_schema: None,
                requires_elevated: true,
                supports_streaming: true,
                tags: vec!["shell".to_string()],
            },
        ]
    }

    async fn health(&self) -> HealthStatus {
        HealthStatus::Healthy
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::isolation::{register_backend_factory, HostDirectBackend};
    use uuid::Uuid;

    #[tokio::test]
    async fn test_runtime_creation_workstation() {
        let config = AgentdConfig::workstation();
        let runtime = AgentdRuntime::new(config).await;
        assert!(runtime.is_ok());
    }

    #[tokio::test]
    async fn test_runtime_creation_with_backend_override_alias() {
        let mut config = AgentdConfig::workstation();
        config.isolation.backend_name = Some("host".to_string());
        let runtime = AgentdRuntime::new(config).await;
        assert!(runtime.is_ok());
    }

    #[tokio::test]
    async fn test_runtime_creation_with_registered_custom_backend() {
        let backend_name = format!("runtime-custom-{}", Uuid::new_v4().simple());
        register_backend_factory(&backend_name, &[], |work_root| {
            Ok(Arc::new(HostDirectBackend::new(work_root)))
        })
        .expect("custom runtime backend should register");

        let mut config = AgentdConfig::workstation();
        config.isolation.backend_name = Some(backend_name);
        let runtime = AgentdRuntime::new(config).await;
        assert!(runtime.is_ok());
    }

    #[tokio::test]
    async fn test_runtime_creation_with_unknown_backend_override_fails() {
        let mut config = AgentdConfig::workstation();
        config.isolation.backend_name = Some("missing-provider".to_string());
        let runtime = AgentdRuntime::new(config).await;
        let err = match runtime {
            Ok(_) => panic!("missing backend override should fail"),
            Err(err) => err,
        };
        let msg = err.to_string();
        assert!(msg.contains("Failed to create configured isolation backend"));
        assert!(msg.contains("missing-provider"));
    }

    #[tokio::test]
    async fn test_runtime_server_profile_rejects_soft_backend_override() {
        let mut config = AgentdConfig::workstation();
        config.profile = ExecutionProfile::Server;
        config.isolation.backend_name = Some("host-direct".to_string());
        let runtime = AgentdRuntime::new(config).await;
        let err = match runtime {
            Ok(_) => panic!("soft isolation backend should be rejected"),
            Err(err) => err,
        };
        assert!(err.to_string().contains("requires kernel isolation"));
    }

    #[tokio::test]
    async fn test_runtime_health() {
        let config = AgentdConfig::workstation();
        let runtime = AgentdRuntime::new(config).await.unwrap();
        let health = runtime.health().await;
        assert!(!health.running); // Not started yet
        assert!(health.backend_healthy);
    }

    #[tokio::test]
    async fn test_runtime_with_profile_workstation() {
        let runtime = AgentdRuntime::with_profile(ExecutionProfile::Workstation).await;
        assert!(runtime.is_ok());
    }

    #[tokio::test]
    async fn test_runtime_with_profile_server() {
        // Server profile uses LinuxNative backend which may not be available
        let runtime = AgentdRuntime::with_profile(ExecutionProfile::Server).await;
        // On Linux, it should succeed; on other platforms, it may fail
        #[cfg(target_os = "linux")]
        {
            // Linux native may still fail due to cgroups/landlock requirements
            // Just check that we get a sensible result
            let _ = runtime;
        }
        #[cfg(not(target_os = "linux"))]
        {
            // On non-Linux, expect failure since LinuxNative backend won't work
            assert!(runtime.is_err() || runtime.is_ok());
        }
    }

    #[tokio::test]
    async fn test_runtime_with_profile_paranoid() {
        // Paranoid profile uses LinuxNative backend which may not be available
        let runtime = AgentdRuntime::with_profile(ExecutionProfile::Paranoid).await;
        // On Linux, it should succeed; on other platforms, it may fail
        #[cfg(target_os = "linux")]
        {
            // Linux native may still fail due to cgroups/landlock requirements
            // Just check that we get a sensible result
            let _ = runtime;
        }
        #[cfg(not(target_os = "linux"))]
        {
            // On non-Linux, expect failure since LinuxNative backend won't work
            assert!(runtime.is_err() || runtime.is_ok());
        }
    }

    #[tokio::test]
    async fn test_runtime_with_profile_custom() {
        let runtime = AgentdRuntime::with_profile(ExecutionProfile::Custom).await;
        assert!(runtime.is_ok());
    }

    #[tokio::test]
    async fn test_runtime_start_and_stop() {
        let config = AgentdConfig::workstation();
        let mut runtime = AgentdRuntime::new(config).await.unwrap();

        // Start
        let start_result = runtime.start().await;
        assert!(start_result.is_ok());

        // Health should show running
        let health = runtime.health().await;
        assert!(health.running);

        // Stop
        let stop_result = runtime.stop().await;
        assert!(stop_result.is_ok());
    }

    #[tokio::test]
    async fn test_runtime_double_start_fails() {
        let config = AgentdConfig::workstation();
        let mut runtime = AgentdRuntime::new(config).await.unwrap();

        runtime.start().await.unwrap();
        let second_start = runtime.start().await;
        assert!(second_start.is_err());

        runtime.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_runtime_stop_when_not_running() {
        let config = AgentdConfig::workstation();
        let runtime = AgentdRuntime::new(config).await.unwrap();

        // Stop when not running should be ok
        let stop_result = runtime.stop().await;
        assert!(stop_result.is_ok());
    }

    #[tokio::test]
    async fn test_runtime_config() {
        let config = AgentdConfig::workstation();
        let runtime = AgentdRuntime::new(config.clone()).await.unwrap();

        let retrieved_config = runtime.config().await;
        assert_eq!(retrieved_config.profile, config.profile);
    }

    #[tokio::test]
    async fn test_runtime_reload() {
        let config = AgentdConfig::workstation();
        let runtime = AgentdRuntime::new(config).await.unwrap();

        // Create a valid config for reload (workstation with modified settings)
        // Note: server() config has nats.enabled=true but nats.url=None which fails validation
        let mut new_config = AgentdConfig::workstation();
        new_config.profile = ExecutionProfile::Custom;
        new_config.sandbox.max_duration_ms = 60_000;

        let reload_result = runtime.reload(new_config).await;
        assert!(reload_result.is_ok());

        let retrieved_config = runtime.config().await;
        assert_eq!(retrieved_config.profile, ExecutionProfile::Custom);
        assert_eq!(retrieved_config.sandbox.max_duration_ms, 60_000);
    }

    #[test]
    fn test_runtime_health_is_healthy_when_running() {
        let health = RuntimeHealth {
            running: true,
            adapter_health: vec![],
            backend_healthy: true,
            backend_error: None,
        };
        assert!(health.is_healthy());
    }

    #[test]
    fn test_runtime_health_unhealthy_when_not_running() {
        let health = RuntimeHealth {
            running: false,
            adapter_health: vec![],
            backend_healthy: true,
            backend_error: None,
        };
        assert!(!health.is_healthy());
    }

    #[test]
    fn test_runtime_health_unhealthy_backend() {
        let health = RuntimeHealth {
            running: true,
            adapter_health: vec![],
            backend_healthy: false,
            backend_error: Some("error".to_string()),
        };
        assert!(!health.is_healthy());
    }

    #[test]
    fn test_runtime_health_struct_clone() {
        let health = RuntimeHealth {
            running: true,
            adapter_health: vec![("test".to_string(), HealthStatus::Healthy)],
            backend_healthy: true,
            backend_error: None,
        };
        let cloned = health.clone();
        assert_eq!(cloned.running, health.running);
    }
}