Spaces:
Runtime error
Runtime error
File size: 6,850 Bytes
cd601a6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | ---
title: Self-Healing DevOps Sandbox
emoji: 🔧
colorFrom: red
colorTo: green
sdk: docker
pinned: false
app_port: 8000
base_path: /web
tags:
- openenv
---
# Self-Healing DevOps Sandbox
An OpenEnv RL environment where an AI agent is dropped into a **broken Node.js backend** inside a Docker container. The agent must use **bash commands only** to diagnose bugs, edit files, and fix the app -- just like a real DevOps engineer would.
Built for the **Meta PyTorch OpenEnv Hackathon**.
---
## What Is This?
A 3-task challenge of increasing difficulty. The agent starts in a Docker container with a broken Express.js app in `/app` and must make all endpoints healthy.
| # | Difficulty | Bug | What's Wrong |
|---|-----------|-----------------|---------------------------------------|
| 1 | Easy | `config.json` | Port set to `9999` instead of `3000` |
| 2 | Medium | `routes/users.js`| Missing `)` causes SyntaxError crash |
| 3 | Hard | `routes/data.js` | Missing `await` causes HTTP 500 |
**Goal:** Fix all bugs so these endpoints return HTTP 200:
- `GET /health` returns `{"status": "ok"}`
- `GET /api/users` returns `{"users": [...]}`
- `GET /api/data` returns `{"records": [...]}`
---
## Scoring (Partial Rewards)
The grader runs **after every command** and awards cumulative points:
| Milestone | Points | Total |
|----------------------------------|--------|----------|
| App starts on port 3000 | +0.35 | 0.35 |
| `/health` returns 200 | +0.10 | 0.45 |
| `/api/users` returns valid JSON | +0.15 | 0.60 |
| `/api/data` returns valid JSON | +0.25 | 0.85 |
| All endpoints correct | +0.15 | **1.00** |
---
## Getting Started
### Prerequisites
- **Python 3.10+**
- **Docker Desktop** (running)
- **uv** package manager (`pip install uv`)
### 1. Install Dependencies
```bash
cd devops_sandbox
uv sync
```
### 2. Build the Sandbox Docker Image
```bash
docker build -t devops-sandbox-node:latest -f simulated_app/Dockerfile simulated_app/
```
### 3. Start the Environment Server
```bash
uv run server
```
The server starts at `http://localhost:8000`.
### 4. Run the Baseline Agent
In a **separate terminal**:
```bash
# Set your OpenAI API key
export OPENAI_API_KEY="sk-..." # Linux/Mac
$env:OPENAI_API_KEY = "sk-..." # PowerShell
# Run the baseline
uv run python baseline.py
```
---
## Test Your Own Agent
### Option A: Use the Python Client
```python
from devops_sandbox import BashAction, DevopsSandboxEnv
with DevopsSandboxEnv(base_url="http://localhost:8000").sync() as env:
# Reset creates a fresh Docker container
result = env.reset()
print(result.observation.stdout) # Task description
print(result.observation.grader_score) # 0.0
# Send bash commands
result = env.step(BashAction(command="cat /app/config.json"))
print(result.observation.stdout) # File contents
print(result.observation.grader_score) # Score after grading
# Fix a bug
result = env.step(BashAction(command="sed -i 's/9999/3000/' /app/config.json"))
print(result.observation.grader_score) # Partial score
# Check if done
if result.done:
print("Episode complete!")
```
### Option B: Use the REST API Directly
```bash
# Reset the environment
curl -X POST http://localhost:8000/reset
# Send a command
curl -X POST http://localhost:8000/step \
-H "Content-Type: application/json" \
-d '{"action": {"command": "ls -la /app"}}'
```
### Option C: Use the WebSocket Endpoint
Connect to `ws://localhost:8000/ws` for persistent sessions.
---
## Project Structure
```
devops_sandbox/
|-- openenv.yaml # OpenEnv manifest
|-- pyproject.toml # Python dependencies
|-- README.md # This file
|-- baseline.py # LLM-powered baseline agent
|-- models.py # BashAction & TerminalObservation schemas
|-- client.py # Python client for the environment
|
|-- server/
| |-- app.py # FastAPI server (entry point)
| +-- devops_sandbox_environment.py # Environment logic + grader
|
+-- simulated_app/ # The broken Node.js app (Docker context)
|-- Dockerfile # node:20-slim sandbox container
|-- package.json # Express.js project
|-- server.js # Main entry point
|-- config.json # Bug 1: wrong port
+-- routes/
|-- users.js # Bug 2: syntax error
+-- data.js # Bug 3: missing await
```
---
## How It Works
```
+-----------+ BashAction +------------+ docker exec +--------------+
| Agent | --------------> | OpenEnv | --------------> | Docker |
| (LLM/RL) | | Server | | Container |
| | <-------------- | (8000) | <-------------- | (broken app)|
+-----------+ Observation +-----+------+ stdout/stderr +--------------+
+ grader_score |
+-----+------+
| Grader |
| (curl test |
| endpoints)|
+------------+
```
1. **Agent** sends a `BashAction` (e.g., `cat /app/config.json`)
2. **Server** runs it inside the Docker container via `docker exec`
3. **Grader** restarts the Node app and curls all endpoints
4. **Observation** returns: stdout, stderr, score (0.0-1.0), feedback
---
## Configuration
| Env Variable | Default | Description |
|--------------------|--------------------------|------------------------------------|
| `OPENAI_API_KEY` | *(required)* | OpenAI API key for baseline |
| `OPENAI_MODEL` | `gpt-4o-mini` | LLM model to use |
| `OPENAI_BASE_URL` | *(OpenAI default)* | Custom endpoint (Ollama, vLLM) |
| `MAX_TURNS` | `30` | Max steps per episode |
| `DEVOPS_SANDBOX_URL`| `http://localhost:8000` | Environment server URL |
### Use with Local LLMs (Ollama, vLLM)
```bash
export OPENAI_BASE_URL="http://localhost:11434/v1"
export OPENAI_MODEL="llama3"
export OPENAI_API_KEY="dummy"
uv run python baseline.py
```
---
## Validation
```bash
uv run openenv validate
# Expected: [OK] devops_sandbox: Ready for multi-mode deployment
```
---
## License
BSD-style license. See LICENSE for details.
|