Ephemeral Sandbox Model
Longshot uses an ephemeral sandbox architecture:- Task Assignment: Orchestrator assigns a task to the worker pool
- Sandbox Creation: A new Modal sandbox is spawned with the task payload
- Execution: The sandbox clones the repo, runs the coding agent, and produces a result
- Cleanup: The sandbox terminates immediately after task completion
- Complete isolation between tasks
- Clean slate for every agent run
- No state pollution from previous tasks
- Automatic resource cleanup
Basic Configuration
Configure sandbox resources in.env:
.env
Resource Limits
- Light
- Standard
- Heavy
Suitable for simple tasks (documentation, config files):
.env
Sandbox Image
The sandbox image is defined ininfra/sandbox_image.py and includes:
Base Tools
- Node.js 22.x LTS - For TypeScript/JavaScript projects
- Python 3.12 - For scripting and Python projects
- Git - Version control operations
- pnpm - Fast package manager
Development Tools
- curl, wget - HTTP requests
- jq - JSON processing
- tree - Directory visualization
- ripgrep (rg) - Fast code search
- build-essential - C/C++ compilation tools
Longshot Components
- @longshot/sandbox - The worker agent runtime
- @longshot/core - Shared utilities
- @mariozechner/pi-coding-agent - Coding agent SDK (v0.52.12)
Git Configuration
Sandboxes are pre-configured with git identity:.env:
.env
Custom Sandbox Images
To add tools or dependencies to the sandbox image:1. Edit infra/sandbox_image.py
Add your customizations to create_agent_image():
infra/sandbox_image.py
2. Install Language Runtimes
Example: Adding Go support:3. Rebuild and Deploy
After modifying the image:Sandbox Lifecycle
1. Creation
When a task is assigned,spawn_sandbox.py creates a new sandbox:
2. Task Setup
The orchestrator writestask.json into the sandbox:
3. Repository Clone
The sandbox clones the target repository:--depth 1) is used so git diff against the start SHA works correctly.
4. Branch Setup
Two modes:- Normal Mode
- Conflict Resolution Mode
Creates a new feature branch:
5. Agent Execution
The sandbox runs the worker agent:- Reads
task.json - Initializes the Pi coding agent
- Executes the task (read code, make changes, run tests)
- Writes
result.jsonwith the outcome
6. Push Changes
If files were modified, the sandbox pushes the branch:7. Termination
The sandbox terminates automatically, releasing all resources.Timeout Configuration
Worker Timeout
Maximum time for a task execution:.env
- The sandbox is terminated
- The task is marked as failed
- The dashboard shows a “TIMEOUT” event
Sandbox Idle Timeout
Modal terminates idle sandboxes after this duration:.env
With the ephemeral model,
SANDBOX_IDLE_TIMEOUT rarely triggers since sandboxes terminate immediately after task completion.Monitoring Sandbox Activity
The dashboard shows real-time sandbox activity:Worker Progress Events
As the agent works, progress events are streamed:- “Cloning repo…”
- “Installing deps…”
- “Reading codebase…”
- “Writing implementation…”
- “Running tests…”
- “Committing changes…”
- The Activity Feed (Tab to switch)
- The Planner Tree (under each running task)
Sandbox Phase Events
Key lifecycle events:Troubleshooting
Sandbox Creation Slow
If sandbox creation takes >10 seconds:- Cold start: First sandbox in a Modal session is slower
- Image size: Large custom images take longer to pull
- Modal capacity: Check Modal dashboard for regional issues
modal.Image layer caching to speed up subsequent starts.
Out of Memory Errors
If tasks fail with OOM errors:- Increase
SANDBOX_MEMORY_MB - Optimize your build process (e.g., use
pnpm install --prod) - Split large tasks into smaller subtasks
Git Push Failures
If the sandbox can’t push branches:- Verify
GIT_TOKENhas push access - Check repository permissions
- Ensure branch name doesn’t conflict with existing branches
Worker Timeout Issues
If tasks frequently timeout:- Increase
WORKER_TIMEOUT - Check if the agent is stuck in a loop (review logs)
- Verify the task is achievable (not too complex)
- Check if tests are hanging
Modal Authentication
Ensure your Modal token is configured:Advanced: Custom Worker Runner
The worker runner (packages/sandbox/src/worker-runner.ts) can be customized:
Adding Pre-Task Hooks
worker-runner.ts
Custom Tool Integration
Extend the agent with custom tools:Environment Variables in Sandboxes
Pass environment variables to sandboxes via the task payload:Best Practices
Resource Allocation
- CPU: 4 cores is sufficient for most TypeScript/JavaScript projects
- Memory: 8GB handles most builds; increase for large monorepos
- Timeout: 30 minutes is generous; most tasks complete in 5-15 minutes
Image Optimization
- Keep base image minimal; add tools only when needed
- Use layer caching for faster sandbox starts
- Pre-install common dependencies in the image (not in sandbox runtime)
Cost Management
- Monitor sandbox hours in Modal dashboard
- Use appropriate resource limits (don’t over-provision)
- Consider
SANDBOX_IDLE_TIMEOUTfor non-ephemeral workflows
Next Steps
Merge Strategies
Configure how branches are merged
Debugging
Debug sandbox execution issues