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
use std::path::Path;
use std::process::{Output, Stdio};
use std::sync::Arc;
use std::time::Instant;

use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::Value;
use smith_protocol::ExecutionStatus;
use tokio::process::Command;
use tokio::sync::oneshot;
use tokio::time::{self, Duration, MissedTickBehavior};
use tracing::{debug, info, warn};

use super::{ExecContext, ExecutionResult, OutputSink, Runner};
use crate::vm::MicroVmManager;

const DEFAULT_TIMEOUT_MS: u64 = 30_000;

#[derive(Debug)]
pub struct ShellExecRunner {
    vm_manager: Option<Arc<MicroVmManager>>,
}

#[derive(Debug, Deserialize)]
struct ShellExecParams {
    command: String,
    #[serde(default = "default_timeout_ms")]
    timeout_ms: u64,
}

const fn default_timeout_ms() -> u64 {
    DEFAULT_TIMEOUT_MS
}

impl ShellExecRunner {
    pub fn new(vm_manager: Option<Arc<MicroVmManager>>) -> Self {
        Self { vm_manager }
    }

    fn parse_params(&self, params: Value) -> Result<ShellExecParams> {
        serde_json::from_value(params).context("Failed to parse shell.exec parameters")
    }

    async fn try_execute_in_vm(
        &self,
        ctx: &ExecContext,
        parsed: &ShellExecParams,
        timeout_ms: u64,
        out: &mut dyn OutputSink,
    ) -> Result<Option<ExecutionResult>> {
        let (manager, session) = match (&self.vm_manager, &ctx.session) {
            (Some(manager), Some(session)) => (manager.clone(), session.clone()),
            _ => return Ok(None),
        };

        let guard = match manager.acquire(session.session_id).await {
            Ok(guard) => guard,
            Err(err) => {
                warn!(
                    session_id = %session.session_id,
                    error = %err,
                    "Failed to acquire micro-VM; falling back to ephemeral sandbox"
                );
                return Ok(None);
            }
        };

        info!(
            session_id = %session.session_id,
            domain = session.domain.as_deref().unwrap_or("unknown"),
            command = %parsed.command,
            "Executing shell command inside persistent VM"
        );

        let mut command = Command::new(guard.shell_path());
        for arg in guard.shell_args() {
            command.arg(arg);
        }
        command.arg(&parsed.command);
        command.current_dir(guard.workdir());
        command.kill_on_drop(true);
        command.stdout(Stdio::piped());
        command.stderr(Stdio::piped());
        for (key, value) in guard.environment() {
            command.env(key, value);
        }
        command.env("SMITH_TRACE_ID", ctx.trace_id.clone());

        let result = self
            .spawn_and_collect(
                command,
                &parsed.command,
                timeout_ms,
                guard.workdir(),
                ctx,
                out,
            )
            .await?;

        Ok(Some(result))
    }

    async fn execute_on_host(
        &self,
        ctx: &ExecContext,
        parsed: &ShellExecParams,
        timeout_ms: u64,
        out: &mut dyn OutputSink,
    ) -> Result<ExecutionResult> {
        let workdir = &ctx.workdir;
        let mut command = Command::new("bash");
        command.arg("-lc").arg(&parsed.command);
        command.current_dir(workdir);
        command.kill_on_drop(true);
        command.stdout(Stdio::piped());
        command.stderr(Stdio::piped());
        command.env("SMITH_TRACE_ID", ctx.trace_id.clone());

        self.spawn_and_collect(command, &parsed.command, timeout_ms, workdir, ctx, out)
            .await
    }

    async fn spawn_and_collect(
        &self,
        mut command: Command,
        command_label: &str,
        timeout_ms: u64,
        workdir: &Path,
        ctx: &ExecContext,
        out: &mut dyn OutputSink,
    ) -> Result<ExecutionResult> {
        debug!(
            command = %command_label,
            timeout_ms,
            workdir = %workdir.display(),
            trace_id = %ctx.trace_id,
            "Starting shell.exec runner"
        );

        let start = Instant::now();
        let timeout = Duration::from_millis(timeout_ms);
        let heartbeat_interval = Duration::from_secs(5);

        let spawn_start = Instant::now();
        debug!(
            command = %command_label,
            timeout_ms,
            workdir = %workdir.display(),
            "Spawning shell command"
        );
        let child = match command.spawn() {
            Ok(child) => {
                debug!(
                    command = %command_label,
                    workdir = %workdir.display(),
                    "Shell command spawn returned Ok"
                );
                child
            }
            Err(err) => {
                let raw_os_error = err.raw_os_error();
                warn!(
                    command = %command_label,
                    workdir = %workdir.display(),
                    error = %err,
                    raw_os_error,
                    "Failed to spawn shell command"
                );
                return Err(err).context("Failed to spawn shell command");
            }
        };
        let child_pid = child.id();
        debug!(
            command = %command_label,
            workdir = %workdir.display(),
            child_pid = ?child_pid,
            "Shell command pid acquired"
        );
        info!(
            command = %command_label,
            child_pid = ?child_pid,
            workdir = %workdir.display(),
            spawn_ms = spawn_start.elapsed().as_millis() as u64,
            "Shell command spawned"
        );

        let (output_tx, output_rx) = oneshot::channel();
        let command_label_cloned = command_label.to_string();
        tokio::spawn(async move {
            let result = child.wait_with_output().await;
            match output_tx.send(result) {
                Ok(()) => {
                    debug!(command = %command_label_cloned, "Shell command wait task signaled completion");
                }
                Err(_) => {
                    debug!(command = %command_label_cloned, "Shell command wait task dropped before signaling completion");
                }
            }
        });

        debug!(
            command = %command_label,
            child_pid = ?child_pid,
            "Shell exec wait loop initialized"
        );

        let mut timeout_future = time::sleep(timeout);
        tokio::pin!(timeout_future);
        let mut output_rx = output_rx;
        tokio::pin!(output_rx);

        let mut heartbeat = time::interval(heartbeat_interval);
        heartbeat.set_missed_tick_behavior(MissedTickBehavior::Delay);

        let mut timed_out = false;
        let mut command_output: Option<Output> = None;

        loop {
            tokio::select! {
                result = &mut output_rx => {
                    match result {
                        Ok(Ok(output)) => {
                            debug!(
                                command = %command_label,
                                child_pid = ?child_pid,
                                elapsed_ms = start.elapsed().as_millis() as u64,
                                "Shell command wait_with_output completed"
                            );
                            command_output = Some(output);
                        }
                        Ok(Err(err)) => {
                            warn!(
                                command = %command_label,
                                child_pid = ?child_pid,
                                error = %err,
                                "Failed to collect shell command output"
                            );
                            return Err(err).context("Failed to collect shell command output");
                        }
                        Err(_) => {
                            return Err(anyhow!(
                                "Shell command wait task was cancelled before completion"
                            ));
                        }
                    }
                    break;
                }
                _ = &mut timeout_future => {
                    debug!(
                        command = %command_label,
                        child_pid = ?child_pid,
                        elapsed_ms = start.elapsed().as_millis() as u64,
                        "Shell exec timeout reached"
                    );
                    timed_out = true;
                    break;
                }
                _ = heartbeat.tick() => {
                    info!(
                        command = %command_label,
                        child_pid = ?child_pid,
                        elapsed_ms = start.elapsed().as_millis() as u64,
                        "Shell command still running"
                    );
                }
            }
        }

        debug!(
            command = %command_label,
            child_pid = ?child_pid,
            timed_out,
            has_output = command_output.is_some(),
            "Shell exec wait loop exiting"
        );

        if timed_out {
            if command_output.is_some() {
                debug!(
                    command = %command_label,
                    child_pid = ?child_pid,
                    "Shell exec timeout fired but output already captured"
                );
                timed_out = false;
            } else {
                out.write_log("error", "Shell command timed out")?;
                warn!(
                    command = %command_label,
                    child_pid = ?child_pid,
                    timeout_ms,
                    elapsed_ms = start.elapsed().as_millis() as u64,
                    "Shell command timed out; attempting to terminate"
                );

                #[cfg(target_os = "linux")]
                {
                    if let Some(pid) = child_pid {
                        let raw_pid = pid as i32;
                        let signal_result = unsafe { libc::kill(raw_pid, libc::SIGKILL) };
                        if signal_result != 0 {
                            let err = std::io::Error::last_os_error();
                            warn!(
                                command = %command_label,
                                child_pid = ?child_pid,
                                error = %err,
                                "Failed to send SIGKILL to shell command"
                            );
                        } else {
                            debug!(
                                command = %command_label,
                                child_pid = ?child_pid,
                                "Sent SIGKILL to shell command after timeout"
                            );
                        }
                    } else {
                        warn!(
                            command = %command_label,
                            "Shell command PID unavailable; unable to signal process"
                        );
                    }
                }

                #[cfg(not(target_os = "linux"))]
                {
                    warn!(
                        command = %command_label,
                        "Shell command timeout handling is not supported on this platform"
                    );
                }

                debug!(
                    command = %command_label,
                    child_pid = ?child_pid,
                    "Awaiting shell command termination after timeout"
                );
                match output_rx.as_mut().await {
                    Ok(Ok(output)) => {
                        debug!(
                            command = %command_label,
                            child_pid = ?child_pid,
                            "Shell exec collected output after timeout"
                        );
                        command_output = Some(output);
                    }
                    Ok(Err(err)) => {
                        warn!(
                            command = %command_label,
                            child_pid = ?child_pid,
                            error = %err,
                            "Failed to collect shell command output after timeout"
                        );
                        return Err(err)
                            .context("Failed to collect shell command output after timeout");
                    }
                    Err(_) => {
                        return Err(anyhow!(
                            "Shell command wait task was cancelled after timeout"
                        ));
                    }
                }
            }
        }

        let output = command_output
            .ok_or_else(|| anyhow!("Shell command finished without producing output"))?;
        let duration_ms = start.elapsed().as_millis() as u64;
        let exit_code = output.status.code();
        let stdout = output.stdout;
        let stderr = output.stderr;
        let stdout_bytes = stdout.len() as u64;
        let stderr_bytes = stderr.len() as u64;

        out.write_stdout(&stdout)?;
        out.write_stderr(&stderr)?;
        if !stderr.is_empty() {
            if let Ok(stderr_str) = std::str::from_utf8(&stderr) {
                out.write_log("warn", stderr_str.trim_end())?;
            }
        }

        if timed_out {
            return Ok(ExecutionResult {
                status: ExecutionStatus::Timeout,
                exit_code,
                artifacts: Vec::new(),
                duration_ms,
                stdout_bytes,
                stderr_bytes,
            });
        }

        info!(
            command = %command_label,
            child_pid = ?child_pid,
            timeout_ms,
            workdir = %workdir.display(),
            exit_code,
            exited_successfully = output.status.success(),
            duration_ms,
            "Shell command completed"
        );

        let exec_status = if output.status.success() {
            ExecutionStatus::Ok
        } else {
            ExecutionStatus::Error
        };

        Ok(ExecutionResult {
            status: exec_status,
            exit_code,
            artifacts: Vec::new(),
            duration_ms,
            stdout_bytes,
            stderr_bytes,
        })
    }
}

#[async_trait]
impl Runner for ShellExecRunner {
    fn digest(&self) -> String {
        format!("shell-exec-runner-{}", env!("CARGO_PKG_VERSION"))
    }

    fn validate_params(&self, params: &Value) -> Result<()> {
        let parsed = self.parse_params(params.clone())?;
        if parsed.command.trim().is_empty() {
            return Err(anyhow!("Shell command must not be empty"));
        }
        Ok(())
    }

    async fn execute(
        &self,
        ctx: &ExecContext,
        params: Value,
        out: &mut dyn OutputSink,
    ) -> Result<ExecutionResult> {
        let parsed = self.parse_params(params)?;
        if parsed.command.trim().is_empty() {
            return Err(anyhow!("Shell command must not be empty"));
        }

        out.write_log(
            "info",
            &format!("Executing shell command: {}", parsed.command),
        )?;
        let timeout_ms = parsed.timeout_ms.max(1);

        if let Some(vm_result) = self
            .try_execute_in_vm(ctx, &parsed, timeout_ms, out)
            .await?
        {
            return Ok(vm_result);
        }

        self.execute_on_host(ctx, &parsed, timeout_ms, out).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runners::{MemoryOutputSink, Scope};
    use serde_json::json;
    use smith_protocol::ExecutionLimits;
    use std::path::PathBuf;
    use tempfile::TempDir;

    // ===== ShellExecRunner Creation Tests =====

    #[test]
    fn test_shell_exec_runner_new_without_vm() {
        let runner = ShellExecRunner::new(None);
        assert!(runner.vm_manager.is_none());
    }

    // ===== Parameter Parsing Tests =====

    #[test]
    fn test_parse_params_valid() {
        let runner = ShellExecRunner::new(None);
        let params = json!({
            "command": "echo hello"
        });

        let parsed = runner.parse_params(params).unwrap();
        assert_eq!(parsed.command, "echo hello");
        assert_eq!(parsed.timeout_ms, DEFAULT_TIMEOUT_MS);
    }

    #[test]
    fn test_parse_params_with_timeout() {
        let runner = ShellExecRunner::new(None);
        let params = json!({
            "command": "sleep 5",
            "timeout_ms": 60000
        });

        let parsed = runner.parse_params(params).unwrap();
        assert_eq!(parsed.command, "sleep 5");
        assert_eq!(parsed.timeout_ms, 60000);
    }

    #[test]
    fn test_parse_params_missing_command() {
        let runner = ShellExecRunner::new(None);
        let params = json!({});

        let result = runner.parse_params(params);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_params_invalid_type() {
        let runner = ShellExecRunner::new(None);
        let params = json!({
            "command": 12345
        });

        let result = runner.parse_params(params);
        assert!(result.is_err());
    }

    // ===== Validation Tests =====

    #[test]
    fn test_validate_params_valid() {
        let runner = ShellExecRunner::new(None);
        let params = json!({
            "command": "ls -la"
        });

        assert!(runner.validate_params(&params).is_ok());
    }

    #[test]
    fn test_validate_params_empty_command() {
        let runner = ShellExecRunner::new(None);
        let params = json!({
            "command": ""
        });

        let result = runner.validate_params(&params);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("empty"));
    }

    #[test]
    fn test_validate_params_whitespace_only() {
        let runner = ShellExecRunner::new(None);
        let params = json!({
            "command": "   \t\n   "
        });

        let result = runner.validate_params(&params);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_params_missing_command() {
        let runner = ShellExecRunner::new(None);
        let params = json!({
            "timeout_ms": 5000
        });

        let result = runner.validate_params(&params);
        assert!(result.is_err());
    }

    // ===== Digest Tests =====

    #[test]
    fn test_digest_format() {
        let runner = ShellExecRunner::new(None);
        let digest = runner.digest();

        assert!(digest.starts_with("shell-exec-runner-"));
        assert!(digest.contains(env!("CARGO_PKG_VERSION")));
    }

    // ===== Default Timeout Tests =====

    #[test]
    fn test_default_timeout_ms() {
        assert_eq!(default_timeout_ms(), DEFAULT_TIMEOUT_MS);
        assert_eq!(DEFAULT_TIMEOUT_MS, 30_000);
    }

    // ===== Integration Tests (require async runtime) =====

    fn create_test_context(workdir: &Path) -> ExecContext {
        ExecContext {
            workdir: workdir.to_path_buf(),
            limits: ExecutionLimits::default(),
            scope: Scope {
                paths: vec![],
                urls: vec![],
            },
            creds: None,
            netns: None,
            trace_id: "test-trace-123".to_string(),
            session: None,
        }
    }

    #[tokio::test]
    async fn test_execute_simple_command() {
        let runner = ShellExecRunner::new(None);
        let temp_dir = TempDir::new().unwrap();
        let ctx = create_test_context(temp_dir.path());
        let mut output = MemoryOutputSink::new();

        let params = json!({
            "command": "echo hello",
            "timeout_ms": 5000
        });

        let result = runner.execute(&ctx, params, &mut output).await.unwrap();

        assert_eq!(result.status, ExecutionStatus::Ok);
        assert_eq!(result.exit_code, Some(0));
        assert!(result.stdout_bytes > 0);
    }

    #[tokio::test]
    async fn test_execute_command_with_exit_code() {
        let runner = ShellExecRunner::new(None);
        let temp_dir = TempDir::new().unwrap();
        let ctx = create_test_context(temp_dir.path());
        let mut output = MemoryOutputSink::new();

        let params = json!({
            "command": "exit 42",
            "timeout_ms": 5000
        });

        let result = runner.execute(&ctx, params, &mut output).await.unwrap();

        assert_eq!(result.status, ExecutionStatus::Error);
        assert_eq!(result.exit_code, Some(42));
    }

    #[tokio::test]
    async fn test_execute_command_stderr() {
        let runner = ShellExecRunner::new(None);
        let temp_dir = TempDir::new().unwrap();
        let ctx = create_test_context(temp_dir.path());
        let mut output = MemoryOutputSink::new();

        let params = json!({
            "command": "echo error >&2",
            "timeout_ms": 5000
        });

        let result = runner.execute(&ctx, params, &mut output).await.unwrap();

        assert_eq!(result.status, ExecutionStatus::Ok);
        assert!(result.stderr_bytes > 0);
    }

    #[tokio::test]
    async fn test_execute_empty_command_fails() {
        let runner = ShellExecRunner::new(None);
        let temp_dir = TempDir::new().unwrap();
        let ctx = create_test_context(temp_dir.path());
        let mut output = MemoryOutputSink::new();

        let params = json!({
            "command": ""
        });

        let result = runner.execute(&ctx, params, &mut output).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_execute_outputs_logs() {
        let runner = ShellExecRunner::new(None);
        let temp_dir = TempDir::new().unwrap();
        let ctx = create_test_context(temp_dir.path());
        let mut output = MemoryOutputSink::new();

        let params = json!({
            "command": "echo test",
            "timeout_ms": 5000
        });

        runner.execute(&ctx, params, &mut output).await.unwrap();

        // Should have info log about executing command
        assert!(!output.logs.is_empty());
        assert!(output
            .logs
            .iter()
            .any(|l| l.contains("Executing shell command")));
    }

    #[tokio::test]
    async fn test_execute_in_workdir() {
        let runner = ShellExecRunner::new(None);
        let temp_dir = TempDir::new().unwrap();

        // Create a file in the temp directory
        std::fs::write(temp_dir.path().join("test.txt"), "content").unwrap();

        let ctx = create_test_context(temp_dir.path());
        let mut output = MemoryOutputSink::new();

        let params = json!({
            "command": "ls test.txt",
            "timeout_ms": 5000
        });

        let result = runner.execute(&ctx, params, &mut output).await.unwrap();

        assert_eq!(result.status, ExecutionStatus::Ok);
        // stdout should contain "test.txt"
        let stdout_str = String::from_utf8_lossy(&output.stdout);
        assert!(stdout_str.contains("test.txt"));
    }

    #[tokio::test]
    async fn test_execute_timeout() {
        let runner = ShellExecRunner::new(None);
        let temp_dir = TempDir::new().unwrap();
        let ctx = create_test_context(temp_dir.path());
        let mut output = MemoryOutputSink::new();

        // Command that will definitely timeout (100ms timeout, 10 second sleep)
        let params = json!({
            "command": "sleep 10",
            "timeout_ms": 100
        });

        let result = runner.execute(&ctx, params, &mut output).await.unwrap();

        // Should either timeout or be killed
        assert!(
            result.status == ExecutionStatus::Timeout || result.status == ExecutionStatus::Error,
            "Expected timeout or error, got {:?}",
            result.status
        );
    }
}