Sandboxed Code Execution for AI Agents
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
- gotcha When using `LocalDeployment`, commands are executed directly on your host machine without any sandboxing. Exercise extreme caution, especially with administrative commands.
- gotcha Versions prior to v1.2.1 could experience `TemporaryDirectory` cleanup issues on Windows, potentially leaving temporary files or directories behind.
- gotcha In some Docker environments, particularly when integrating with tools like SWE-agent, the `swe-rex` server installed via `pipx` might not be correctly added to the system's `PATH` variable.
- gotcha Older versions (prior to v1.4.0) had a hardcoded Python runtime value which could lead to Docker build failures when running in default configurations or with specific projects like SWE-agent.
- gotcha Users might encounter 'Runtime did not start within timeout' errors, especially in CI/CD environments like GitHub Actions. This often indicates issues with port allocation or insufficient startup time.
Install
-
pip install swe-rex -
pip install 'swe-rex[modal]' -
pip install 'swe-rex[fargate]' -
pip install 'swe-rex[daytona]' -
pip install 'swe-rex[dev]'
Imports
- LocalDeployment
from swerex.deployment.local import LocalDeployment
- DockerDeployment
from swerex.deployment.docker import DockerDeployment
- CreateBashSessionRequest
from swerex.runtime.abstract import CreateBashSessionRequest
- BashAction
from swerex.runtime.abstract import BashAction
- Command
from swerex.runtime.abstract import Command
Quickstart
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())