Skip to main content

adk_code/
executor.rs

1//! Async executor trait and shared request validation helpers.
2//!
3//! [`CodeExecutor`] is the backend interface that all execution backends implement.
4//! The module also provides [`validate_policy`] and [`validate_request`] helpers
5//! that enforce fail-closed semantics: if a backend cannot enforce a requested
6//! sandbox control, execution is rejected before user code runs.
7//!
8//! # Example
9//!
10//! ```rust
11//! use adk_code::{
12//!     BackendCapabilities, ExecutionIsolation, SandboxPolicy, validate_policy,
13//! };
14//!
15//! let caps = BackendCapabilities {
16//!     isolation: ExecutionIsolation::ContainerEphemeral,
17//!     enforce_network_policy: true,
18//!     enforce_filesystem_policy: true,
19//!     enforce_environment_policy: true,
20//!     enforce_timeout: true,
21//!     supports_structured_output: true,
22//!     supports_process_execution: false,
23//!     supports_persistent_workspace: false,
24//!     supports_interactive_sessions: false,
25//! };
26//!
27//! let policy = SandboxPolicy::strict_rust();
28//! assert!(validate_policy(&caps, &policy).is_ok());
29//! ```
30
31use async_trait::async_trait;
32
33use crate::{
34    BackendCapabilities, EnvironmentPolicy, ExecutionError, ExecutionLanguage, ExecutionPayload,
35    ExecutionRequest, ExecutionResult, FilesystemPolicy, GuestModuleFormat, NetworkPolicy,
36    SandboxPolicy,
37};
38
39/// Async trait for code execution backends.
40///
41/// Backends may optionally implement lifecycle methods ([`start`](Self::start),
42/// [`stop`](Self::stop), [`restart`](Self::restart)) for persistent execution
43/// environments like containers. The default implementations are no-ops, so
44/// simple backends (e.g., host-local `rustc`) work without lifecycle management.
45///
46/// Backends that support persistent environments should override these methods
47/// and report `supports_persistent_workspace: true` in their capabilities.
48#[async_trait]
49pub trait CodeExecutor: Send + Sync {
50    /// Human-readable backend name.
51    fn name(&self) -> &str;
52    /// The capabilities this backend can enforce.
53    fn capabilities(&self) -> BackendCapabilities;
54    /// Whether this backend supports the given language.
55    fn supports_language(&self, lang: &ExecutionLanguage) -> bool;
56    /// Execute a request and return a structured result.
57    async fn execute(&self, request: ExecutionRequest) -> Result<ExecutionResult, ExecutionError>;
58
59    /// Start the execution environment (e.g., create and start a container).
60    ///
61    /// For persistent backends, this creates the underlying environment and
62    /// makes it ready for [`execute`](Self::execute) calls. Calling `execute`
63    /// on a started backend reuses the same environment.
64    ///
65    /// The default implementation is a no-op for backends that don't need
66    /// lifecycle management (e.g., host-local compilation).
67    async fn start(&self) -> Result<(), ExecutionError> {
68        Ok(())
69    }
70
71    /// Stop the execution environment and release resources.
72    ///
73    /// For persistent backends, this stops and removes the underlying
74    /// environment (e.g., stops and removes a Docker container). After
75    /// `stop`, the backend can be restarted with [`start`](Self::start).
76    ///
77    /// The default implementation is a no-op.
78    async fn stop(&self) -> Result<(), ExecutionError> {
79        Ok(())
80    }
81
82    /// Restart the execution environment.
83    ///
84    /// Equivalent to [`stop`](Self::stop) followed by [`start`](Self::start),
85    /// but backends may implement this more efficiently (e.g., `docker restart`).
86    ///
87    /// The default implementation calls `stop` then `start`.
88    async fn restart(&self) -> Result<(), ExecutionError> {
89        self.stop().await?;
90        self.start().await
91    }
92
93    /// Whether the execution environment is currently running.
94    ///
95    /// Returns `true` if [`start`](Self::start) has been called and
96    /// [`stop`](Self::stop) has not. For backends without lifecycle
97    /// management, this always returns `true`.
98    async fn is_running(&self) -> bool {
99        true
100    }
101}
102
103/// Validates that the backend can enforce the requested sandbox policy.
104///
105/// Returns `Err(ExecutionError::UnsupportedPolicy(...))` if any requested
106/// control cannot be enforced by the backend. This implements fail-closed
107/// semantics: execution is rejected before user code runs.
108///
109/// # Checks
110///
111/// - Network policy: if disabled, backend must be able to enforce it
112/// - Filesystem policy: if any access is requested, backend must enforce it
113/// - Environment policy: if any variables are exposed, backend must enforce it
114/// - Timeout: backend must always be able to enforce timeouts
115pub fn validate_policy(
116    capabilities: &BackendCapabilities,
117    policy: &SandboxPolicy,
118) -> Result<(), ExecutionError> {
119    if matches!(policy.network, NetworkPolicy::Disabled) && !capabilities.enforce_network_policy {
120        return Err(ExecutionError::UnsupportedPolicy(
121            "backend cannot enforce network restrictions".to_string(),
122        ));
123    }
124    if !matches!(policy.filesystem, FilesystemPolicy::None)
125        && !capabilities.enforce_filesystem_policy
126    {
127        return Err(ExecutionError::UnsupportedPolicy(
128            "backend cannot enforce filesystem restrictions".to_string(),
129        ));
130    }
131    if !matches!(policy.environment, EnvironmentPolicy::None)
132        && !capabilities.enforce_environment_policy
133    {
134        return Err(ExecutionError::UnsupportedPolicy(
135            "backend cannot enforce environment variable restrictions".to_string(),
136        ));
137    }
138    if !capabilities.enforce_timeout {
139        return Err(ExecutionError::UnsupportedPolicy(
140            "backend cannot enforce execution timeouts".to_string(),
141        ));
142    }
143    Ok(())
144}
145
146/// Validates a full execution request against a backend's capabilities.
147///
148/// Checks that:
149/// 1. The backend supports the requested language
150/// 2. The payload type matches the language (e.g., `GuestModule` only for Wasm)
151/// 3. The sandbox policy is enforceable by the backend
152///
153/// Call this before [`CodeExecutor::execute`] for clear, early errors.
154///
155/// # Example
156///
157/// ```rust
158/// use adk_code::{
159///     BackendCapabilities, ExecutionIsolation, ExecutionLanguage,
160///     ExecutionPayload, ExecutionRequest, SandboxPolicy,
161///     validate_request,
162/// };
163///
164/// let caps = BackendCapabilities {
165///     isolation: ExecutionIsolation::ContainerEphemeral,
166///     enforce_network_policy: true,
167///     enforce_filesystem_policy: true,
168///     enforce_environment_policy: true,
169///     enforce_timeout: true,
170///     supports_structured_output: true,
171///     supports_process_execution: false,
172///     supports_persistent_workspace: false,
173///     supports_interactive_sessions: false,
174/// };
175///
176/// let request = ExecutionRequest {
177///     language: ExecutionLanguage::Rust,
178///     payload: ExecutionPayload::Source {
179///         code: "fn run(input: serde_json::Value) -> serde_json::Value { input }".to_string(),
180///     },
181///     argv: vec![],
182///     stdin: None,
183///     input: None,
184///     sandbox: SandboxPolicy::strict_rust(),
185///     identity: None,
186/// };
187///
188/// let supported = [ExecutionLanguage::Rust];
189/// assert!(validate_request(&caps, &supported, &request).is_ok());
190/// ```
191pub fn validate_request(
192    capabilities: &BackendCapabilities,
193    supported_languages: &[ExecutionLanguage],
194    request: &ExecutionRequest,
195) -> Result<(), ExecutionError> {
196    // 1. Language support check
197    if !supported_languages.contains(&request.language) {
198        return Err(ExecutionError::UnsupportedLanguage(format!("{}", request.language)));
199    }
200
201    // 2. Payload-language compatibility check
202    match (&request.language, &request.payload) {
203        // GuestModule payloads are only valid for Wasm
204        (lang, ExecutionPayload::GuestModule { format, .. }) => match format {
205            GuestModuleFormat::Wasm if *lang != ExecutionLanguage::Wasm => {
206                return Err(ExecutionError::InvalidRequest(format!(
207                    "GuestModule(Wasm) payload requires Wasm language, got {lang}"
208                )));
209            }
210            _ => {}
211        },
212        // Wasm language requires a GuestModule payload
213        (ExecutionLanguage::Wasm, ExecutionPayload::Source { .. }) => {
214            return Err(ExecutionError::InvalidRequest(
215                "Wasm language requires a GuestModule payload, not Source".to_string(),
216            ));
217        }
218        _ => {}
219    }
220
221    // 3. Policy enforcement check
222    validate_policy(capabilities, &request.sandbox)?;
223
224    Ok(())
225}