Implement DockerSandbox extending deepagents' BaseSandbox to manage Docker containers via docker-py. This completes Task 1 of Phase 2. Key implementation details: - Extends BaseSandbox which auto-implements file I/O (read/write/ls/grep) by delegating to execute() - Synchronous execute() method called via loop.run_in_executor() by server.py - Container lifecycle management (create/connect/close) - Upload/download file support via tar archives - Configurable resource limits (memory, CPU, PIDs) - Timeout support with proper exit code handling - Environment variable configuration via create_sandbox() factory Tests: - 6 new tests covering container creation, command execution, and cleanup - All 46 tests passing (40 existing + 6 new)
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import os
|
|
|
|
from agent.integrations.docker_sandbox import DockerSandbox
|
|
|
|
|
|
def create_sandbox(sandbox_id: str | None = None) -> DockerSandbox:
|
|
"""Factory function for creating DockerSandbox instances.
|
|
|
|
Args:
|
|
sandbox_id: Optional container ID to connect to existing container.
|
|
If None, creates a new container.
|
|
|
|
Returns:
|
|
DockerSandbox instance configured from environment variables.
|
|
"""
|
|
# Build environment variables for the container
|
|
env = {}
|
|
test_db_url = os.environ.get("TEST_DATABASE_URL", "")
|
|
if test_db_url:
|
|
env["DATABASE_URL"] = test_db_url
|
|
|
|
# Connect to existing container if ID provided
|
|
if sandbox_id:
|
|
return DockerSandbox(container_id=sandbox_id)
|
|
|
|
# Create new container with environment configuration
|
|
return DockerSandbox(
|
|
image=os.environ.get("SANDBOX_IMAGE", "galaxis-sandbox:latest"),
|
|
network=os.environ.get("SANDBOX_NETWORK", "galaxis-net"),
|
|
mem_limit=os.environ.get("SANDBOX_MEM_LIMIT", "4g"),
|
|
cpu_count=int(os.environ.get("SANDBOX_CPU_COUNT", "2")),
|
|
pids_limit=int(os.environ.get("SANDBOX_PIDS_LIMIT", "256")),
|
|
environment=env,
|
|
default_timeout=int(os.environ.get("SANDBOX_TIMEOUT", "300")),
|
|
)
|