Skip to main content

PosixShellSandbox

Trait PosixShellSandbox 

Source
pub trait PosixShellSandbox:
    Send
    + Sync
    + 'static {
    // Required method
    fn execute_streaming(
        &self,
        command: &str,
        options: ExecuteOptions,
    ) -> ExecutionStream;

    // Provided methods
    fn execute_code_streaming(
        &self,
        code: &str,
        language: &str,
        options: ExecuteOptions,
    ) -> ExecutionStream { ... }
    fn read_file<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn write_file<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
        content: &'life2 [u8],
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn remove_file<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn list_files<'life0, 'life1, 'async_trait>(
        &'life0 self,
        path: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<FileInfo>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn get_tools(&self) -> Vec<Arc<dyn Tool>> { ... }
}
Expand description

A sandbox where only a POSIX shell is available, such as a container, an SSH connection or a cloud runtime (PosixShellSandbox).

Implementors supply execute_streaming; code execution and the file operations run as shell commands through it. Override any of them with a native implementation where the backend has one. Every PosixShellSandbox is a Sandbox.

execute_streaming must apply options.env: backends that build a shell command prepend build_shell_env_prefix; backends that pass the environment through process flags call validate_env_keys and pass the values on.

Call the operations through Sandbox (or a dyn Sandbox); with both traits in scope a method call on the implementing type is ambiguous.

use std::process::Stdio;

use strands::sandbox::{
    ExecuteOptions, ExecutionResult, ExecutionStream, ExecutionStreamItem, PosixShellSandbox,
    Sandbox, build_shell_env_prefix, shell_quote,
};
use strands::Error;

/// Runs commands in a running container through `docker exec`.
struct Container {
    name: String,
}

impl PosixShellSandbox for Container {
    fn execute_streaming(&self, command: &str, options: ExecuteOptions) -> ExecutionStream {
        let prefix = build_shell_env_prefix(options.env.as_deref());
        let cwd = options.cwd.unwrap_or_else(|| "/".to_owned());
        let name = self.name.clone();
        let command = command.to_owned();
        Box::pin(futures::stream::once(async move {
            let script = format!("cd {} && {}{command}", shell_quote(&cwd), prefix?);
            let output = tokio::process::Command::new("docker")
                .args(["exec", &name, "sh", "-c", &script])
                .stdin(Stdio::null())
                .output()
                .await?;
            let result = ExecutionResult::new(
                output.status.code().unwrap_or(1),
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr),
            );
            Ok::<_, Error>(ExecutionStreamItem::from(result))
        }))
    }
}

let sandbox = Container { name: "dev".to_owned() };
let _hostname = Sandbox::read_text(&sandbox, "/etc/hostname").await?;

Required Methods§

Source

fn execute_streaming( &self, command: &str, options: ExecuteOptions, ) -> ExecutionStream

Runs a shell command, streaming its output. See Sandbox::execute_streaming.

Provided Methods§

Source

fn execute_code_streaming( &self, code: &str, language: &str, options: ExecuteOptions, ) -> ExecutionStream

Pipes base64-encoded code into the interpreter through execute_streaming. See Sandbox::execute_code_streaming.

Source

fn read_file<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Reads a file through base64, with exit codes that tell a missing path, a directory and a permission failure apart. See Sandbox::read_file.

Source

fn write_file<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, path: &'life1 str, content: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Writes a file through base64 -d, creating parent directories with mkdir -p. See Sandbox::write_file.

Source

fn remove_file<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Removes a file with rm. See Sandbox::remove_file.

Source

fn list_files<'life0, 'life1, 'async_trait>( &'life0 self, path: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<FileInfo>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Lists a directory by parsing ls -1ap. Entries carry is_dir but no size. See Sandbox::list_files.

Source

fn get_tools(&self) -> Vec<Arc<dyn Tool>>

Tools this sandbox vends to an agent. See Sandbox::get_tools.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§