Sandboxed Code Execution for AI Agents

1.4.0 · active · verified Thu Apr 09

SWE-ReX (SWE-agent Remote Execution Framework) is a Python library designed for sandboxed code execution for AI agents. It provides a flexible runtime interface for interacting with shell environments, supporting execution locally or remotely across various platforms like Docker containers, AWS Fargate, Modal, and Daytona. This enables massively parallel agent runs and disentangles agent logic from infrastructure concerns, making it a core component for projects like SWE-agent. Currently at version 1.4.0, SWE-ReX maintains an active development cycle with regular updates enhancing backend support and overall stability.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `swe-rex` with both local and Docker deployments. It shows how to execute single commands and interact with a persistent bash session. Remember that `LocalDeployment` executes commands directly on your machine without sandboxing. For sandboxed environments, `DockerDeployment` (or cloud deployments like Modal/Fargate/Daytona) are recommended.

import asyncio
from swerex.deployment.local import LocalDeployment
from swerex.deployment.docker import DockerDeployment
from swerex.runtime.abstract import CreateBashSessionRequest, BashAction, Command
import os

async def run_code_with_deployment(deployment_type: str):
    if deployment_type == "local":
        # LocalDeployment runs on your machine WITHOUT sandboxing. Be cautious.
        deployment = LocalDeployment()
        print("Running locally (no sandboxing by default). Be careful with commands!")
    elif deployment_type == "docker":
        # DockerDeployment provides sandboxing
        # Ensure Docker is running. You might need to pull the image first if not cached.
        deployment = DockerDeployment(image="python:3.12")
        print("Running in a Docker sandbox...")
    else:
        raise ValueError("Invalid deployment type")

    try:
        await deployment.start()
        runtime = deployment.runtime

        # Execute one-off commands
        print(f"Executing: echo Hello, world! from {deployment_type}")
        result = await runtime.execute(Command(command=["echo", f"Hello, world! from {deployment_type}"]))
        print(f"Output: {result.stdout.strip()}\n")

        # Create a bash session for persistent state
        await runtime.create_session(CreateBashSessionRequest())

        print(f"Running in session: export MYVAR='test_{deployment_type}'")
        await runtime.run_in_session(BashAction(command=f"export MYVAR='test_{deployment_type}'"))

        print(f"Running in session: echo $MYVAR")
        result_session = await runtime.run_in_session(BashAction(command="echo $MYVAR"))
        print(f"Output from session: {result_session.stdout.strip()}\n")

    finally:
        await deployment.stop()

async def main():
    print("--- Local Deployment Example ---")
    await run_code_with_deployment("local")

    print("\n--- Docker Deployment Example ---")
    await run_code_with_deployment("docker")

if __name__ == "__main__":
    # os.environ['MODAL_TOKEN_ID'] = os.environ.get('MODAL_TOKEN_ID', '') # For ModalDeployment
    # os.environ['MODAL_TOKEN_SECRET'] = os.environ.get('MODAL_TOKEN_SECRET', '') # For ModalDeployment
    # ... similarly for AWS or Daytona credentials if used ...
    asyncio.run(main())

view raw JSON →