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
//! Sandbox — a running MicroVM instance.
use std::path::PathBuf;
use a3s_box_core::error::Result;
use a3s_box_core::exec::{ExecMetrics, ExecOutput, ExecRequest, FileOp, FileRequest};
use a3s_box_runtime::{ExecClient, PtyClient, StreamingExec, VmManager};
use base64::Engine;
/// Result of executing a command in a sandbox.
#[derive(Debug, Clone)]
pub struct ExecResult {
/// Standard output (lossy UTF-8 conversion from raw bytes).
pub stdout: String,
/// Standard error (lossy UTF-8 conversion from raw bytes).
pub stderr: String,
/// Exit code (0 = success).
pub exit_code: i32,
/// Execution metrics (duration, bytes transferred).
pub metrics: ExecMetrics,
}
impl From<ExecOutput> for ExecResult {
fn from(output: ExecOutput) -> Self {
Self {
metrics: ExecMetrics {
stdout_bytes: output.stdout.len() as u64,
stderr_bytes: output.stderr.len() as u64,
..Default::default()
},
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
exit_code: output.exit_code,
}
}
}
/// A running MicroVM sandbox.
///
/// Provides methods to execute commands, stream output, transfer files,
/// open PTY sessions, and manage the sandbox lifecycle.
pub struct Sandbox {
/// Unique sandbox identifier.
id: String,
/// Human-readable name.
name: String,
/// VM manager (owns the VM lifecycle).
vm: VmManager,
/// Path to the exec Unix socket.
exec_socket: PathBuf,
/// Path to the PTY Unix socket.
pty_socket: PathBuf,
}
impl Sandbox {
/// Create a new Sandbox handle (called by BoxSdk::create).
pub(crate) fn new(
id: String,
name: String,
vm: VmManager,
exec_socket: PathBuf,
pty_socket: PathBuf,
) -> Self {
Self {
id,
name,
vm,
exec_socket,
pty_socket,
}
}
/// Get the sandbox ID.
pub fn id(&self) -> &str {
&self.id
}
/// Get the sandbox name.
pub fn name(&self) -> &str {
&self.name
}
/// Get the current sandbox state.
pub async fn state(&self) -> a3s_box_runtime::BoxState {
self.vm.state().await
}
/// Execute a command in the sandbox.
///
/// Waits for the command to complete and returns all output at once.
///
/// # Arguments
/// * `cmd` - Command to execute
/// * `args` - Command arguments
pub async fn exec(&self, cmd: &str, args: &[&str]) -> Result<ExecResult> {
let started = std::time::Instant::now();
let mut cmd_parts = vec![cmd.to_string()];
cmd_parts.extend(args.iter().map(|a| a.to_string()));
let request = ExecRequest {
cmd: cmd_parts,
timeout_ns: 0,
env: Vec::new(),
working_dir: None,
stdin: None,
user: None,
streaming: false,
};
let client = ExecClient::connect(&self.exec_socket).await?;
let output = client.exec_command(&request).await?;
let mut result = ExecResult::from(output);
result.metrics.duration_ms = started.elapsed().as_millis() as u64;
Ok(result)
}
/// Execute a command with environment variables and working directory.
pub async fn exec_with_options(
&self,
cmd: Vec<String>,
env: Vec<String>,
working_dir: Option<String>,
stdin: Option<Vec<u8>>,
) -> Result<ExecResult> {
let started = std::time::Instant::now();
let request = ExecRequest {
cmd,
timeout_ns: 0,
env,
working_dir,
stdin,
user: None,
streaming: false,
};
let client = ExecClient::connect(&self.exec_socket).await?;
let output = client.exec_command(&request).await?;
let mut result = ExecResult::from(output);
result.metrics.duration_ms = started.elapsed().as_millis() as u64;
Ok(result)
}
/// Execute a command in streaming mode.
///
/// Returns a `StreamingExec` handle that yields output chunks as they
/// arrive from the guest. Use this for long-running commands or when
/// you need real-time output.
///
/// # Example
///
/// ```rust,no_run
/// # use a3s_box_sdk::Sandbox;
/// # async fn example(sandbox: &Sandbox) -> Result<(), Box<dyn std::error::Error>> {
/// use a3s_box_core::exec::ExecEvent;
///
/// let mut stream = sandbox.exec_stream("tail", &["-f", "/var/log/syslog"]).await?;
/// while let Some(event) = stream.next_event().await? {
/// match event {
/// ExecEvent::Chunk(chunk) => {
/// print!("{}", String::from_utf8_lossy(&chunk.data));
/// }
/// ExecEvent::Exit(exit) => {
/// println!("Exited with code {}", exit.exit_code);
/// }
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn exec_stream(&self, cmd: &str, args: &[&str]) -> Result<StreamingExec> {
let mut cmd_parts = vec![cmd.to_string()];
cmd_parts.extend(args.iter().map(|a| a.to_string()));
let request = ExecRequest {
cmd: cmd_parts,
timeout_ns: 0,
env: Vec::new(),
working_dir: None,
stdin: None,
user: None,
streaming: true,
};
let client = ExecClient::connect(&self.exec_socket).await?;
client.exec_stream(&request).await
}
/// Upload a file from the host into the sandbox.
///
/// # Arguments
/// * `data` - File contents
/// * `guest_path` - Destination path inside the sandbox
pub async fn upload(&self, data: &[u8], guest_path: &str) -> Result<()> {
let request = FileRequest {
op: FileOp::Upload,
guest_path: guest_path.to_string(),
data: Some(base64::engine::general_purpose::STANDARD.encode(data)),
};
let client = ExecClient::connect(&self.exec_socket).await?;
let response = client.file_transfer(&request).await?;
if !response.success {
return Err(a3s_box_core::error::BoxError::ExecError(format!(
"Upload failed: {}",
response.error.unwrap_or_else(|| "unknown error".into())
)));
}
Ok(())
}
/// Download a file from the sandbox to the host.
///
/// # Arguments
/// * `guest_path` - Path inside the sandbox to download
///
/// # Returns
/// Raw file contents as bytes.
pub async fn download(&self, guest_path: &str) -> Result<Vec<u8>> {
let request = FileRequest {
op: FileOp::Download,
guest_path: guest_path.to_string(),
data: None,
};
let client = ExecClient::connect(&self.exec_socket).await?;
let response = client.file_transfer(&request).await?;
if !response.success {
return Err(a3s_box_core::error::BoxError::ExecError(format!(
"Download failed: {}",
response.error.unwrap_or_else(|| "unknown error".into())
)));
}
let data = response.data.ok_or_else(|| {
a3s_box_core::error::BoxError::ExecError("Download response missing data".to_string())
})?;
base64::engine::general_purpose::STANDARD
.decode(&data)
.map_err(|e| {
a3s_box_core::error::BoxError::ExecError(format!(
"Failed to decode downloaded file: {}",
e
))
})
}
/// Open an interactive PTY session.
///
/// Returns a `PtyClient` for bidirectional terminal I/O.
pub async fn pty(&self, shell: &str, cols: u16, rows: u16) -> Result<PtyClient> {
let mut client = PtyClient::connect(&self.pty_socket).await?;
let request = a3s_box_core::pty::PtyRequest {
cmd: vec![shell.to_string()],
env: Vec::new(),
working_dir: None,
user: None,
cols,
rows,
};
client.send_request(&request).await?;
Ok(client)
}
/// Stop the sandbox and release resources.
pub async fn stop(mut self) -> Result<()> {
tracing::info!(sandbox_id = %self.id, "Stopping sandbox");
self.vm.destroy().await
}
/// Pause the sandbox (SIGSTOP).
///
/// The sandbox remains in memory but all processes are frozen.
/// Use `resume()` to continue execution.
pub async fn pause(&self) -> Result<()> {
tracing::info!(sandbox_id = %self.id, "Pausing sandbox");
self.vm.pause().await
}
/// Resume a paused sandbox (SIGCONT).
pub async fn resume(&self) -> Result<()> {
tracing::info!(sandbox_id = %self.id, "Resuming sandbox");
self.vm.resume().await
}
/// Check if the sandbox is running.
pub async fn is_running(&self) -> bool {
matches!(
self.vm.state().await,
a3s_box_runtime::BoxState::Ready | a3s_box_runtime::BoxState::Busy
)
}
}
impl std::fmt::Debug for Sandbox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sandbox")
.field("id", &self.id)
.field("name", &self.name)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exec_result_from_exec_output() {
let output = ExecOutput {
stdout: b"hello\n".to_vec(),
stderr: Vec::new(),
exit_code: 0,
};
let result = ExecResult::from(output);
assert_eq!(result.stdout, "hello\n");
assert_eq!(result.stderr, "");
assert_eq!(result.exit_code, 0);
assert_eq!(result.metrics.stdout_bytes, 6);
assert_eq!(result.metrics.stderr_bytes, 0);
}
#[test]
fn test_exec_result_nonzero_exit() {
let output = ExecOutput {
stdout: Vec::new(),
stderr: b"not found\n".to_vec(),
exit_code: 127,
};
let result = ExecResult::from(output);
assert_eq!(result.exit_code, 127);
assert_eq!(result.stderr, "not found\n");
assert_eq!(result.metrics.stderr_bytes, 10);
}
#[test]
fn test_exec_result_metrics_byte_counts() {
let output = ExecOutput {
stdout: vec![0u8; 1024],
stderr: vec![0u8; 512],
exit_code: 0,
};
let result = ExecResult::from(output);
assert_eq!(result.metrics.stdout_bytes, 1024);
assert_eq!(result.metrics.stderr_bytes, 512);
}
}