feat: add Sandbox abstraction for agent code execution environments#1968
Draft
agent-of-mkmeral wants to merge 1 commit intostrands-agents:mainfrom
Draft
feat: add Sandbox abstraction for agent code execution environments#1968agent-of-mkmeral wants to merge 1 commit intostrands-agents:mainfrom
agent-of-mkmeral wants to merge 1 commit intostrands-agents:mainfrom
Conversation
Add the Sandbox interface that decouples tool logic from where code runs. Tools that need to execute code or access a filesystem receive a Sandbox instead of managing their own execution, enabling portability across local, Docker, and cloud environments. Core components: - Sandbox ABC with streaming AsyncGenerator interface (base.py) - LocalSandbox for host-process execution via asyncio subprocesses (local.py) - DockerSandbox for containerized execution via docker exec (docker.py) - Agent integration: sandbox parameter on Agent.__init__, defaults to LocalSandbox Key design decisions: - Only core abstractions in SDK; AgentCoreSandbox and sandbox tools belong in separate packages (external dependencies, different release cycles) - Streaming output via AsyncGenerator[str | ExecutionResult] - yields lines as they arrive, ExecutionResult as the final yield - Security: randomized heredoc delimiters, shlex.quote for all paths, stdin piping in DockerSandbox to prevent injection - Auto-start lifecycle: sandbox starts on first execute() call - Zero external dependencies for core sandbox package Tests: 76 new tests (base: 22, local: 17, docker: 18, agent: 6, + shared) All 76 sandbox tests passing, 1686 existing tests still passing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add the Sandbox interface that decouples tool logic from where code runs. Tools that need to execute code or access a filesystem receive a Sandbox instead of managing their own execution, enabling portability across local, Docker, and cloud environments.
Related: Design Doc PR #681
What Changed
New Files (4 source + 5 test)
src/strands/sandbox/__init__.pysrc/strands/sandbox/base.pySandboxABC with streamingAsyncGeneratorinterface,ExecutionResult, convenience methods (read_file,write_file,list_files,execute_code), auto-start lifecyclesrc/strands/sandbox/local.pyLocalSandbox— native file I/O overrides, asyncio subprocess executionsrc/strands/sandbox/docker.pyDockerSandbox— container lifecycle, stdin piping for safe file writes,docker execstreamingtests/strands/sandbox/test_base.pytests/strands/sandbox/test_local.pytests/strands/sandbox/test_docker.pytests/strands/sandbox/test_agent_sandbox.pyModified Files (2)
src/strands/__init__.pySandbox,LocalSandbox,DockerSandbox,ExecutionResultsrc/strands/agent/agent.pysandboxparameter toAgent.__init__, defaults toLocalSandbox()Design Decisions
✅ IN core SDK
SandboxABC — the interface contractLocalSandbox— zero-dependency host execution (default)DockerSandbox— containerized executionExecutionResultdataclasssandboxparameter)❌ NOT in core SDK (for now)
AgentCoreSandbox— External dependency onbedrock-agentcorepackage. Should live in a separate packagerun_command,python_tool,programmatic_tool_caller) — These are tools, not core abstractions. They belong instrands-agents/toolsCodeActPlugin— P2 in the design doc, not yet readyRationale: The core SDK should provide the minimal, zero-external-dependency abstraction. Tools and cloud-specific sandbox implementations have their own dependency graphs and release cycles.
Key Design Details
Streaming Interface
Agent Integration
Security
secrets.token_hex(8)delimitershlex.quote()for all pathsshlex.quote()for code argumentsTests
What's Left (Outside This PR)
AgentCoreSandbox— needs its own package withbedrock-agentcoredependencystrands-agents/toolsCodeActPlugin— P2 per design doccc @mkmeral