Sandbox Context Management
This skill provides standardized patterns for managing Daytona sandbox environments across agent sessions.
When This Skill Applies
Use this skill when:
- Agent has
"use_sandbox": truein metadata - Agent needs to execute bash, docker, or git operations in an isolated environment
- Agent needs to persist sandbox across conversation turns or resumed sessions
Sandbox Lifecycle
Step 1: Check for Existing Sandbox
On every task start, check if a sandbox already exists for this session:
Use Daytona MCP tool to check:
get_sandbox(session_id=<current_session_id>)
- If sandbox exists → Use that
sandbox_idfor all operations - If no sandbox found → Proceed to Step 2
Step 2: Create Sandbox (If Needed)
Use Daytona MCP tool to create:
create_sandbox(session_id=<current_session_id>)
Store the returned sandbox_id for use in all subsequent operations.
Step 3: Persist Sandbox ID
Save sandbox_id to workspace for session resumption:
Write to .sandbox_id file in current working directory:
echo "<sandbox_id>" > .sandbox_id
Step 4: Validate Workspace
CRITICAL: Workspace directories may not exist. Always create them first:
mkdir -p /workspace
mkdir -p /workspace/{service_name}
Session Resumption
When a session is resumed (same session_id):
- Check if
.sandbox_idfile exists in workspace - Read the sandbox_id:
cat .sandbox_id - Validate sandbox is still active using
get_sandbox(sandbox_id) - If valid → reuse it
- If invalid/gone → create new sandbox and update
.sandbox_id
Sandbox Capabilities
The sandbox supports:
| Capability | Description |
|---|---|
| Bash | All standard Linux commands, file operations, process management |
| Docker | Build images, run containers, docker-compose |
| Git | Clone, checkout, commit (use git-operations MCP for GitHub push/PR) |
File Operations
Reading Files
# Simple read
cat /path/to/file.txt
# Check if exists, then read
[ -f /path/to/file.txt ] && cat /path/to/file.txt
# Read first/last N lines
head -50 /path/to/file.txt
tail -50 /path/to/file.txt
Writing Files
# Step 1: Create directory (ALWAYS do this first)
mkdir -p /path/to/directory
# Step 2: Write file using heredoc (use 'EOF' with quotes to preserve $variables)
cat > /path/to/file.txt << 'EOF'
Your content here
$variables stay as literals
Multiple lines supported
EOF
# Step 3: Verify (optional but recommended)
[ -s /path/to/file.txt ] && echo "Success" || echo "Failed"
Quick Reference
| Operation | Command |
|---|---|
| Create directory | mkdir -p /path/to/dir |
| Write file | cat > file.txt << 'EOF'...EOF |
| Read file | cat file.txt |
| Check exists | [ -f file.txt ] && echo "yes" |
| Check not empty | [ -s file.txt ] && echo "has content" |
| List directory | ls -la /path/to/dir |
| Copy file | cp source.txt dest.txt |
| Move/rename | mv old.txt new.txt |
| Delete file | rm file.txt |
| Delete directory | rm -rf /path/to/dir |
Docker Operations
# Build image
docker build -t my-image .
# Run container
docker run -d --name my-container my-image
# Docker compose
docker-compose up -d
# View logs
docker logs my-container
# Stop/remove
docker stop my-container && docker rm my-container
Error Handling
Return structured JSON on errors:
{
"status": "FAILED",
"phase": "sandbox_creation",
"reason": "Failed to create Daytona sandbox",
"recoverable": true
}
Best Practices
- Always
mkdir -pfirst - directories may not exist - Use quoted heredocs -
<< 'EOF'preserves $variables as literals - Persist sandbox_id - save to
.sandbox_idfor session resumption - Validate on resume - check if stored sandbox still exists
- Use git-operations MCP - for GitHub authenticated operations (push, PR)
Workflow Diagram
Task Starts
│
▼
Check sandbox exists? ──► get_sandbox(session_id)
│
├─► YES: Use existing sandbox_id
│
└─► NO: Create sandbox ──► create_sandbox(session_id)
│
▼
Save to .sandbox_id
│
▼
Create directories (mkdir -p /workspace/...)
│
▼
Execute task operations