Skip to main content

ciab_core/traits/
runtime.rs

1use std::collections::HashMap;
2
3use async_trait::async_trait;
4use tokio::sync::mpsc;
5use uuid::Uuid;
6
7use crate::error::CiabResult;
8use crate::types::sandbox::{
9    ExecRequest, ExecResult, FileInfo, LogOptions, ResourceStats, SandboxInfo, SandboxSpec,
10    SandboxState,
11};
12
13#[async_trait]
14pub trait SandboxRuntime: Send + Sync {
15    /// Create a new sandbox from the given spec.
16    async fn create_sandbox(&self, spec: &SandboxSpec) -> CiabResult<SandboxInfo>;
17
18    /// Get info about a sandbox by ID.
19    async fn get_sandbox(&self, id: &Uuid) -> CiabResult<SandboxInfo>;
20
21    /// List all sandboxes, optionally filtered by state, provider, and labels.
22    async fn list_sandboxes(
23        &self,
24        state: Option<SandboxState>,
25        provider: Option<&str>,
26        labels: &HashMap<String, String>,
27    ) -> CiabResult<Vec<SandboxInfo>>;
28
29    /// Start a sandbox.
30    async fn start_sandbox(&self, id: &Uuid) -> CiabResult<()>;
31
32    /// Stop a sandbox.
33    async fn stop_sandbox(&self, id: &Uuid) -> CiabResult<()>;
34
35    /// Pause a sandbox.
36    async fn pause_sandbox(&self, id: &Uuid) -> CiabResult<()>;
37
38    /// Resume a paused sandbox.
39    async fn resume_sandbox(&self, id: &Uuid) -> CiabResult<()>;
40
41    /// Terminate and remove a sandbox.
42    async fn terminate_sandbox(&self, id: &Uuid) -> CiabResult<()>;
43
44    /// Execute a command inside a sandbox.
45    async fn exec(&self, id: &Uuid, request: &ExecRequest) -> CiabResult<ExecResult>;
46
47    /// Execute a command inside a sandbox, streaming stdout lines as they arrive.
48    /// Returns a receiver of output lines and a join handle for the final ExecResult.
49    ///
50    /// Default implementation falls back to `exec()` and sends all lines at once.
51    async fn exec_streaming(
52        &self,
53        id: &Uuid,
54        request: &ExecRequest,
55    ) -> CiabResult<(
56        mpsc::Receiver<String>,
57        tokio::task::JoinHandle<CiabResult<ExecResult>>,
58    )> {
59        let result = self.exec(id, request).await?;
60        let (tx, rx) = mpsc::channel::<String>(256);
61        let stdout = result.stdout.clone();
62        let handle = tokio::spawn(async move {
63            for line in stdout.lines() {
64                let _ = tx.send(line.to_string()).await;
65            }
66            Ok(result)
67        });
68        Ok((rx, handle))
69    }
70
71    /// Execute a command with bidirectional streaming.
72    /// Returns (stdout lines receiver, stdin sender, join handle).
73    /// Send lines to the stdin sender to write to the process's stdin.
74    async fn exec_streaming_interactive(
75        &self,
76        id: &Uuid,
77        request: &ExecRequest,
78    ) -> CiabResult<(
79        mpsc::Receiver<String>,
80        mpsc::Sender<String>,
81        tokio::task::JoinHandle<CiabResult<ExecResult>>,
82    )> {
83        // Default: fall back to non-interactive exec_streaming, return a dummy stdin sender.
84        let (rx, handle) = self.exec_streaming(id, request).await?;
85        let (stdin_tx, _stdin_rx) = mpsc::channel::<String>(16);
86        Ok((rx, stdin_tx, handle))
87    }
88
89    /// Read a file from a sandbox.
90    async fn read_file(&self, id: &Uuid, path: &str) -> CiabResult<Vec<u8>>;
91
92    /// Write a file to a sandbox.
93    async fn write_file(&self, id: &Uuid, path: &str, content: &[u8]) -> CiabResult<()>;
94
95    /// List files in a directory inside a sandbox.
96    async fn list_files(&self, id: &Uuid, path: &str) -> CiabResult<Vec<FileInfo>>;
97
98    /// Get resource stats for a sandbox.
99    async fn get_stats(&self, id: &Uuid) -> CiabResult<ResourceStats>;
100
101    /// Stream logs from a sandbox.
102    async fn stream_logs(
103        &self,
104        id: &Uuid,
105        options: &LogOptions,
106    ) -> CiabResult<mpsc::Receiver<String>>;
107
108    /// Kill an active exec process for a sandbox.
109    async fn kill_exec(&self, id: &Uuid) -> CiabResult<()> {
110        let _ = id;
111        Ok(())
112    }
113}