agentic-sandbox 0.1.0

Sandboxed code execution: Process, Docker, Firecracker, Gateway
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
//! Gateway sandbox that delegates execution to the Agentic Gateway.
//!
//! This sandbox connects to a running gateway instance which manages
//! Firecracker microVMs for secure code execution.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────┐       HTTPS        ┌─────────────────────┐
//! │  agentic-rs     │  ───────────────►  │  Agentic Gateway    │
//! │                 │                    │                     │
//! │  GatewaySandbox │  ◄───────────────  │  ┌───────────────┐  │
//! │                 │      Response      │  │ Firecracker   │  │
//! │  - Session mgmt │                    │  │ Pool (warm)   │  │
//! │  - Auto cleanup │                    │  └───────────────┘  │
//! └─────────────────┘                    └─────────────────────┘
//! ```
//!
//! # Example
//!
//! ```rust,no_run
//! use agentic_sandbox::{GatewaySandbox, Sandbox};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let sandbox = GatewaySandbox::builder("http://localhost:8080")
//!         .runtime("python")
//!         .build()
//!         .await?;
//!
//!     let result = sandbox.execute("print('Hello from Firecracker!')").await?;
//!     println!("Output: {}", result.stdout);
//!
//!     // Sandbox session is automatically cleaned up on drop
//!     Ok(())
//! }
//! ```

use async_trait::async_trait;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, info, instrument, warn};

use crate::{
    error::SandboxError,
    traits::{ExecutionResult, Sandbox, SandboxArtifact},
};

/// Supported runtimes in the gateway.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Runtime {
    #[default]
    Python,
    Node,
    Rust,
    Bash,
}

impl std::fmt::Display for Runtime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Python => write!(f, "python"),
            Self::Node => write!(f, "node"),
            Self::Rust => write!(f, "rust"),
            Self::Bash => write!(f, "bash"),
        }
    }
}

/// Request to create a sandbox session.
#[derive(Debug, Clone, Serialize)]
struct CreateSandboxRequest {
    runtime: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    memory_mb: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    timeout_ms: Option<u64>,
}

/// Response from creating a sandbox.
#[derive(Debug, Clone, Deserialize)]
struct CreateSandboxResponse {
    id: String,
    #[allow(dead_code)]
    status: String,
}

/// Request to execute code in a sandbox.
#[derive(Debug, Clone, Serialize)]
struct ExecuteCodeRequest {
    code: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    timeout_ms: Option<u64>,
}

/// Response from code execution.
#[derive(Debug, Clone, Deserialize)]
struct ExecuteCodeResponse {
    stdout: String,
    stderr: String,
    exit_code: i32,
    duration_ms: u64,
    #[serde(default)]
    artifacts: Vec<ArtifactResponse>,
    error: Option<String>,
}

/// Artifact in the response.
#[derive(Debug, Clone, Deserialize)]
struct ArtifactResponse {
    path: String,
    content: String,
}

/// Health check response.
#[derive(Debug, Clone, Deserialize)]
pub struct GatewayHealthResponse {
    pub status: String,
    #[serde(default)]
    pub sandboxes_ready: u32,
    #[serde(default)]
    pub sandboxes_total: u32,
}

/// Configuration for `GatewaySandbox`.
#[derive(Debug, Clone)]
pub struct GatewaySandboxConfig {
    /// Base URL of the gateway (e.g., `http://localhost:8080`)
    pub base_url: String,
    /// API key for authentication (if required)
    pub api_key: Option<String>,
    /// Runtime to use
    pub runtime: Runtime,
    /// Memory in MB for the sandbox
    pub memory_mb: Option<u32>,
    /// Request timeout in milliseconds
    pub timeout_ms: u64,
    /// Number of retries on failure
    pub max_retries: u32,
}

impl Default for GatewaySandboxConfig {
    fn default() -> Self {
        Self {
            base_url: "http://localhost:8080".into(),
            api_key: None,
            runtime: Runtime::Python,
            memory_mb: None,
            timeout_ms: 30000,
            max_retries: 2,
        }
    }
}

/// Internal state for the sandbox session.
#[derive(Debug)]
struct SandboxSession {
    id: String,
    base_url: String,
    api_key: Option<String>,
    client: Client,
}

impl SandboxSession {
    /// Delete the sandbox session.
    async fn cleanup(&self) -> Result<(), SandboxError> {
        let url = format!("{}/v1/sandboxes/{}", self.base_url, self.id);

        let mut request = self.client.delete(&url);
        if let Some(ref key) = self.api_key {
            request = request.header("Authorization", format!("Bearer {key}"));
        }

        match request.send().await {
            Ok(response) if response.status().is_success() => {
                info!(sandbox_id = %self.id, "Sandbox session cleaned up");
                Ok(())
            }
            Ok(response) => {
                warn!(
                    sandbox_id = %self.id,
                    status = %response.status(),
                    "Failed to cleanup sandbox"
                );
                Ok(()) // Don't fail on cleanup errors
            }
            Err(e) => {
                warn!(sandbox_id = %self.id, error = %e, "Failed to cleanup sandbox");
                Ok(()) // Don't fail on cleanup errors
            }
        }
    }
}

/// Gateway sandbox that uses the Agentic Gateway for Firecracker execution.
///
/// This sandbox manages a session with the gateway, automatically creating
/// and cleaning up sandbox instances.
pub struct GatewaySandbox {
    config: GatewaySandboxConfig,
    client: Client,
    session: Arc<RwLock<Option<SandboxSession>>>,
}

impl GatewaySandbox {
    /// Create a new `GatewaySandbox` builder.
    ///
    /// # Arguments
    ///
    /// * `base_url` - The URL of the gateway server
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agentic_sandbox::GatewaySandbox;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let sandbox = GatewaySandbox::builder("http://localhost:8080")
    ///     .runtime("python")
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn builder(base_url: impl Into<String>) -> GatewaySandboxBuilder {
        GatewaySandboxBuilder::new(base_url.into())
    }

    /// Create with full config (does not create session yet).
    fn with_config(config: GatewaySandboxConfig) -> Self {
        let http_timeout = config.timeout_ms + 30_000;
        let client = Client::builder()
            .timeout(Duration::from_millis(http_timeout))
            .build()
            .expect("Failed to create HTTP client");

        Self {
            config,
            client,
            session: Arc::new(RwLock::new(None)),
        }
    }

    /// Initialize the sandbox session with the gateway.
    pub async fn init(&self) -> Result<(), SandboxError> {
        let mut session = self.session.write().await;
        if session.is_some() {
            return Ok(()); // Already initialized
        }

        let url = format!("{}/v1/sandboxes", self.config.base_url);

        let request_body = CreateSandboxRequest {
            runtime: self.config.runtime.to_string(),
            memory_mb: self.config.memory_mb,
            timeout_ms: Some(self.config.timeout_ms),
        };

        let mut request = self.client.post(&url).json(&request_body);
        if let Some(ref key) = self.config.api_key {
            request = request.header("Authorization", format!("Bearer {key}"));
        }

        let response = request
            .send()
            .await
            .map_err(|e| SandboxError::ConnectionFailed(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(SandboxError::ExecutionFailed(format!(
                "Failed to create sandbox: {status} - {body}"
            )));
        }

        let create_response: CreateSandboxResponse = response
            .json()
            .await
            .map_err(|e| SandboxError::ExecutionFailed(e.to_string()))?;

        info!(
            sandbox_id = %create_response.id,
            runtime = %self.config.runtime,
            "Sandbox session created"
        );

        *session = Some(SandboxSession {
            id: create_response.id,
            base_url: self.config.base_url.clone(),
            api_key: self.config.api_key.clone(),
            client: self.client.clone(),
        });

        Ok(())
    }

    /// Get the sandbox session ID.
    pub async fn session_id(&self) -> Option<String> {
        self.session.read().await.as_ref().map(|s| s.id.clone())
    }

    /// Check gateway health.
    pub async fn health(&self) -> Result<GatewayHealthResponse, SandboxError> {
        let url = format!("{}/health", self.config.base_url);

        let mut request = self.client.get(&url);
        if let Some(ref key) = self.config.api_key {
            request = request.header("Authorization", format!("Bearer {key}"));
        }

        let response = request
            .send()
            .await
            .map_err(|e| SandboxError::ConnectionFailed(e.to_string()))?;

        if !response.status().is_success() {
            return Err(SandboxError::ConnectionFailed(format!(
                "Health check failed: {}",
                response.status()
            )));
        }

        response
            .json::<GatewayHealthResponse>()
            .await
            .map_err(|e| SandboxError::ConnectionFailed(e.to_string()))
    }

    /// Execute code with retry logic.
    async fn execute_with_retry(&self, code: &str) -> Result<ExecutionResult, SandboxError> {
        // Ensure session is initialized
        self.init().await?;

        let mut last_error = None;

        for attempt in 0..=self.config.max_retries {
            if attempt > 0 {
                debug!("Retry attempt {}/{}", attempt, self.config.max_retries);
                tokio::time::sleep(Duration::from_millis(500 * u64::from(attempt))).await;
            }

            match self.execute_once(code).await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    warn!("Execution attempt {} failed: {}", attempt + 1, e);
                    last_error = Some(e);
                }
            }
        }

        Err(last_error.unwrap_or_else(|| SandboxError::ExecutionFailed("Unknown error".into())))
    }

    /// Execute code once (no retry).
    async fn execute_once(&self, code: &str) -> Result<ExecutionResult, SandboxError> {
        let session = self.session.read().await;
        let session = session
            .as_ref()
            .ok_or_else(|| SandboxError::ExecutionFailed("Session not initialized".into()))?;

        let url = format!(
            "{}/v1/sandboxes/{}/execute",
            self.config.base_url, session.id
        );

        let request_body = ExecuteCodeRequest {
            code: code.to_string(),
            timeout_ms: Some(self.config.timeout_ms),
        };

        let mut request = self.client.post(&url).json(&request_body);
        if let Some(ref key) = self.config.api_key {
            request = request.header("Authorization", format!("Bearer {key}"));
        }

        let response = request
            .send()
            .await
            .map_err(|e| SandboxError::ConnectionFailed(e.to_string()))?;

        if response.status() == reqwest::StatusCode::UNAUTHORIZED {
            return Err(SandboxError::ConnectionFailed("Invalid API key".into()));
        }

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(SandboxError::ExecutionFailed(
                "Sandbox session expired or not found".into(),
            ));
        }

        if response.status() == reqwest::StatusCode::REQUEST_TIMEOUT {
            return Err(SandboxError::Timeout);
        }

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(SandboxError::ExecutionFailed(format!(
                "Server error {status}: {body}"
            )));
        }

        let exec_response: ExecuteCodeResponse = response
            .json()
            .await
            .map_err(|e| SandboxError::ExecutionFailed(e.to_string()))?;

        if let Some(error) = exec_response.error {
            return Err(SandboxError::ExecutionFailed(error));
        }

        let artifacts = exec_response
            .artifacts
            .into_iter()
            .map(|a| SandboxArtifact {
                path: a.path,
                content: a.content,
            })
            .collect();

        Ok(ExecutionResult {
            exit_code: exec_response.exit_code,
            stdout: exec_response.stdout,
            stderr: exec_response.stderr,
            execution_time_ms: exec_response.duration_ms,
            artifacts,
        })
    }

    /// Cleanup the sandbox session.
    pub async fn cleanup(&self) -> Result<(), SandboxError> {
        let mut session = self.session.write().await;
        if let Some(s) = session.take() {
            s.cleanup().await?;
        }
        Ok(())
    }
}

impl Drop for GatewaySandbox {
    fn drop(&mut self) {
        // Schedule async cleanup
        let session = self.session.clone();
        tokio::spawn(async move {
            let mut guard = session.write().await;
            if let Some(s) = guard.take() {
                let _ = s.cleanup().await;
            }
        });
    }
}

impl std::fmt::Debug for GatewaySandbox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GatewaySandbox")
            .field("base_url", &self.config.base_url)
            .field("runtime", &self.config.runtime)
            .field("timeout_ms", &self.config.timeout_ms)
            .finish_non_exhaustive()
    }
}

#[async_trait]
impl Sandbox for GatewaySandbox {
    #[instrument(skip(self, code), fields(sandbox = "gateway", url = %self.config.base_url))]
    async fn execute(&self, code: &str) -> Result<ExecutionResult, SandboxError> {
        info!(runtime = %self.config.runtime, "Executing code via gateway");
        debug!("Code: {}...", &code[..code.len().min(100)]);

        self.execute_with_retry(code).await
    }

    async fn is_ready(&self) -> Result<bool, SandboxError> {
        match self.health().await {
            Ok(health) => Ok(health.sandboxes_ready > 0),
            Err(_) => Ok(false),
        }
    }

    async fn stop(&self) -> Result<(), SandboxError> {
        self.cleanup().await
    }
}

/// Builder for `GatewaySandbox`.
pub struct GatewaySandboxBuilder {
    config: GatewaySandboxConfig,
    auto_init: bool,
}

impl GatewaySandboxBuilder {
    fn new(base_url: String) -> Self {
        Self {
            config: GatewaySandboxConfig {
                base_url,
                ..Default::default()
            },
            auto_init: true,
        }
    }

    /// Set API key for authentication.
    #[must_use]
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.config.api_key = Some(key.into());
        self
    }

    /// Set the runtime (python, node, rust, bash).
    #[must_use]
    pub fn runtime(mut self, runtime: impl Into<String>) -> Self {
        let runtime_str = runtime.into();
        self.config.runtime = match runtime_str.to_lowercase().as_str() {
            "python" => Runtime::Python,
            "node" | "nodejs" | "javascript" | "js" => Runtime::Node,
            "rust" => Runtime::Rust,
            "bash" | "shell" | "sh" => Runtime::Bash,
            _ => Runtime::Python, // Default
        };
        self
    }

    /// Set memory in MB.
    #[must_use]
    pub const fn memory_mb(mut self, mb: u32) -> Self {
        self.config.memory_mb = Some(mb);
        self
    }

    /// Set request timeout in milliseconds.
    #[must_use]
    pub const fn timeout_ms(mut self, ms: u64) -> Self {
        self.config.timeout_ms = ms;
        self
    }

    /// Set max retries.
    #[must_use]
    pub const fn max_retries(mut self, n: u32) -> Self {
        self.config.max_retries = n;
        self
    }

    /// Don't auto-initialize the session on build.
    #[must_use]
    pub const fn lazy(mut self) -> Self {
        self.auto_init = false;
        self
    }

    /// Build the `GatewaySandbox`.
    ///
    /// If `lazy()` was not called, this will create the sandbox session.
    ///
    /// # Errors
    ///
    /// Returns an error if auto-init is enabled and session creation fails.
    pub async fn build(self) -> Result<GatewaySandbox, SandboxError> {
        let sandbox = GatewaySandbox::with_config(self.config);

        if self.auto_init {
            sandbox.init().await?;
        }

        Ok(sandbox)
    }

    /// Build without initializing (sync version for lazy init).
    #[must_use]
    pub fn build_lazy(self) -> GatewaySandbox {
        GatewaySandbox::with_config(self.config)
    }
}

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

    #[test]
    fn test_builder_sync() {
        let sandbox = GatewaySandbox::builder("http://localhost:8080")
            .api_key("test-key")
            .runtime("python")
            .timeout_ms(5000)
            .max_retries(3)
            .build_lazy();

        assert_eq!(sandbox.config.base_url, "http://localhost:8080");
        assert_eq!(sandbox.config.api_key, Some("test-key".into()));
        assert_eq!(sandbox.config.timeout_ms, 5000);
        assert_eq!(sandbox.config.max_retries, 3);
    }

    #[test]
    fn test_runtime_parsing() {
        let sandbox = GatewaySandbox::builder("http://localhost:8080")
            .runtime("javascript")
            .build_lazy();

        assert!(matches!(sandbox.config.runtime, Runtime::Node));
    }

    #[test]
    fn test_default_config() {
        let config = GatewaySandboxConfig::default();
        assert_eq!(config.base_url, "http://localhost:8080");
        assert!(config.api_key.is_none());
        assert_eq!(config.timeout_ms, 30000);
    }
}