|
| 1 | +#!/bin/bash |
| 2 | +# Run Metis Ralph in a sandboxed Docker container |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# ./run-sandboxed-ralph.sh <SHORT_CODE> [OPTIONS] |
| 6 | +# |
| 7 | +# Options: |
| 8 | +# --task Run single task (auto-detected from short code) |
| 9 | +# --max-iterations N Maximum iterations (default: unlimited) |
| 10 | +# --attach Attach to container instead of background |
| 11 | +# |
| 12 | +# Uses Docker's official sandbox feature which: |
| 13 | +# - Handles OAuth authentication automatically |
| 14 | +# - Stores credentials in persistent Docker volume |
| 15 | +# - Provides isolated execution environment |
| 16 | +# - Includes Claude Code and common dev tools |
| 17 | + |
| 18 | +set -e |
| 19 | + |
| 20 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 21 | + |
| 22 | +# Parse arguments |
| 23 | +SHORT_CODE="" |
| 24 | +MAX_ITERATIONS="" |
| 25 | +ATTACH_MODE=false |
| 26 | +TASK_MODE=false |
| 27 | + |
| 28 | +while [[ $# -gt 0 ]]; do |
| 29 | + case $1 in |
| 30 | + --max-iterations) |
| 31 | + MAX_ITERATIONS="$2" |
| 32 | + shift 2 |
| 33 | + ;; |
| 34 | + --attach) |
| 35 | + ATTACH_MODE=true |
| 36 | + shift |
| 37 | + ;; |
| 38 | + --task) |
| 39 | + TASK_MODE=true |
| 40 | + shift |
| 41 | + ;; |
| 42 | + -h|--help) |
| 43 | + cat << 'EOF' |
| 44 | +Run Metis Ralph in a sandboxed Docker container |
| 45 | +
|
| 46 | +USAGE: |
| 47 | + ./run-sandboxed-ralph.sh <SHORT_CODE> [OPTIONS] |
| 48 | +
|
| 49 | +ARGUMENTS: |
| 50 | + SHORT_CODE Task (PROJ-T-NNNN) or Initiative (PROJ-I-NNNN) short code |
| 51 | +
|
| 52 | +OPTIONS: |
| 53 | + --task Execute a single task (auto-detected from code format) |
| 54 | + --max-iterations N Maximum iterations before auto-stop |
| 55 | + --attach Attach to container (default: background) |
| 56 | + -h, --help Show this help |
| 57 | +
|
| 58 | +AUTHENTICATION: |
| 59 | + Uses Docker's official sandbox which handles OAuth automatically. |
| 60 | + On first run, you'll be prompted to authenticate via browser. |
| 61 | + Credentials are stored in Docker volume 'docker-claude-sandbox-data'. |
| 62 | +
|
| 63 | +EXAMPLES: |
| 64 | + # Run all tasks under an initiative |
| 65 | + ./run-sandboxed-ralph.sh PROJ-I-0001 |
| 66 | +
|
| 67 | + # Run a single task |
| 68 | + ./run-sandboxed-ralph.sh PROJ-T-0001 |
| 69 | +
|
| 70 | + # Attach to see output in real-time |
| 71 | + ./run-sandboxed-ralph.sh PROJ-T-0001 --attach |
| 72 | +
|
| 73 | +COMPLETION: |
| 74 | + Progress is logged to Metis documents in .metis/ |
| 75 | +EOF |
| 76 | + exit 0 |
| 77 | + ;; |
| 78 | + -*) |
| 79 | + echo "Unknown option: $1" >&2 |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + *) |
| 83 | + if [[ -z "$SHORT_CODE" ]]; then |
| 84 | + SHORT_CODE="$1" |
| 85 | + fi |
| 86 | + shift |
| 87 | + ;; |
| 88 | + esac |
| 89 | +done |
| 90 | + |
| 91 | +# Validate short code |
| 92 | +if [[ -z "$SHORT_CODE" ]]; then |
| 93 | + echo "Error: No short code provided" >&2 |
| 94 | + echo "Usage: ./run-sandboxed-ralph.sh <SHORT_CODE> [OPTIONS]" >&2 |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +# Determine mode from short code if not explicitly set |
| 99 | +if [[ "$SHORT_CODE" =~ ^[A-Z]+-T-[0-9]+$ ]]; then |
| 100 | + TASK_MODE=true |
| 101 | + CONTAINER_PREFIX="ralph-task" |
| 102 | + MODE_NAME="Task" |
| 103 | + RALPH_CMD="/metis-ralph" |
| 104 | +elif [[ "$SHORT_CODE" =~ ^[A-Z]+-I-[0-9]+$ ]]; then |
| 105 | + CONTAINER_PREFIX="ralph-initiative" |
| 106 | + MODE_NAME="Initiative" |
| 107 | + RALPH_CMD="/metis-ralph-initiative" |
| 108 | +else |
| 109 | + echo "Error: Invalid short code format: $SHORT_CODE" >&2 |
| 110 | + echo "Expected: PREFIX-T-NNNN (task) or PREFIX-I-NNNN (initiative)" >&2 |
| 111 | + exit 1 |
| 112 | +fi |
| 113 | + |
| 114 | +# Find project root (look for .metis directory) |
| 115 | +PROJECT_DIR="$(pwd)" |
| 116 | +while [[ "$PROJECT_DIR" != "/" ]]; do |
| 117 | + if [[ -d "$PROJECT_DIR/.metis" ]]; then |
| 118 | + break |
| 119 | + fi |
| 120 | + PROJECT_DIR="$(dirname "$PROJECT_DIR")" |
| 121 | +done |
| 122 | + |
| 123 | +if [[ ! -d "$PROJECT_DIR/.metis" ]]; then |
| 124 | + echo "Error: Could not find .metis directory" >&2 |
| 125 | + echo "Run from within a Metis project directory" >&2 |
| 126 | + exit 1 |
| 127 | +fi |
| 128 | + |
| 129 | +echo "========================================" |
| 130 | +echo " Sandboxed Ralph Launcher" |
| 131 | +echo "========================================" |
| 132 | +echo "" |
| 133 | +echo "Mode: $MODE_NAME" |
| 134 | +echo "Short Code: $SHORT_CODE" |
| 135 | +echo "Project: $PROJECT_DIR" |
| 136 | +echo "" |
| 137 | + |
| 138 | +# Build the prompt for Claude |
| 139 | +RALPH_PROMPT="Execute $RALPH_CMD $SHORT_CODE" |
| 140 | +if [[ -n "$MAX_ITERATIONS" ]]; then |
| 141 | + RALPH_PROMPT="$RALPH_PROMPT --max-iterations $MAX_ITERATIONS" |
| 142 | +fi |
| 143 | + |
| 144 | +# Check if docker sandbox is available |
| 145 | +if ! docker sandbox --help &>/dev/null 2>&1; then |
| 146 | + echo "Error: 'docker sandbox' command not available" >&2 |
| 147 | + echo "" >&2 |
| 148 | + echo "Docker sandbox requires Docker Desktop with AI features enabled." >&2 |
| 149 | + echo "See: https://docs.docker.com/ai/sandboxes/get-started/" >&2 |
| 150 | + exit 1 |
| 151 | +fi |
| 152 | + |
| 153 | +# Check if sandbox is authenticated |
| 154 | +# Credentials are stored inside the sandbox container at /home/agent/.claude/.credentials.json |
| 155 | +check_sandbox_auth() { |
| 156 | + # Find any running claude sandbox for this workspace |
| 157 | + local container_name |
| 158 | + container_name=$(docker ps --filter "label=docker/sandbox=true" --format "{{.Names}}" 2>/dev/null | head -1) |
| 159 | + |
| 160 | + if [[ -n "$container_name" ]]; then |
| 161 | + # Check if credentials exist in the running container |
| 162 | + local has_creds |
| 163 | + has_creds=$(docker exec "$container_name" test -f /home/agent/.claude/.credentials.json 2>/dev/null && echo "yes") |
| 164 | + if [[ "$has_creds" == "yes" ]]; then |
| 165 | + echo "$container_name" # Return container name for reuse |
| 166 | + return 0 |
| 167 | + fi |
| 168 | + fi |
| 169 | + |
| 170 | + return 1 |
| 171 | +} |
| 172 | + |
| 173 | +echo "Checking sandbox authentication..." |
| 174 | +EXISTING_CONTAINER=$(check_sandbox_auth) |
| 175 | +if [[ -z "$EXISTING_CONTAINER" ]]; then |
| 176 | + echo "" |
| 177 | + echo "========================================" |
| 178 | + echo " Authentication Required" |
| 179 | + echo "========================================" |
| 180 | + echo "" |
| 181 | + echo "No authenticated Docker sandbox found." |
| 182 | + echo "" |
| 183 | + echo "Please run this command in your terminal:" |
| 184 | + echo "" |
| 185 | + echo " docker sandbox run -w $PROJECT_DIR claude" |
| 186 | + echo "" |
| 187 | + echo "This will:" |
| 188 | + echo " 1. Open a browser for OAuth login" |
| 189 | + echo " 2. Start an interactive Claude session" |
| 190 | + echo " 3. Keep the sandbox running for Ralph to use" |
| 191 | + echo "" |
| 192 | + echo "IMPORTANT: Keep the sandbox running (don't exit)." |
| 193 | + echo "Then re-run this script in another terminal." |
| 194 | + echo "" |
| 195 | + echo "Alternatively, use an API key:" |
| 196 | + echo "" |
| 197 | + echo " export ANTHROPIC_API_KEY='sk-ant-...'" |
| 198 | + echo " docker sandbox run -e ANTHROPIC_API_KEY -w $PROJECT_DIR claude" |
| 199 | + echo "" |
| 200 | + exit 1 |
| 201 | +fi |
| 202 | +echo "Authentication: OK" |
| 203 | +echo "Using sandbox: $EXISTING_CONTAINER" |
| 204 | +echo "" |
| 205 | + |
| 206 | +echo "Launching Ralph..." |
| 207 | +echo "Prompt: $RALPH_PROMPT" |
| 208 | +echo "" |
| 209 | + |
| 210 | +LOG_FILE="$PROJECT_DIR/.metis/ralph-sandbox-$(date +%Y%m%d-%H%M%S).log" |
| 211 | + |
| 212 | +# Run with docker sandbox |
| 213 | +if [[ "$ATTACH_MODE" == "true" ]]; then |
| 214 | + # Attached mode - run in foreground |
| 215 | + echo "Running in foreground..." |
| 216 | + docker exec -it "$EXISTING_CONTAINER" claude --print "$RALPH_PROMPT" |
| 217 | +else |
| 218 | + # Detached mode - run in background |
| 219 | + echo "Running in background..." |
| 220 | + echo "" |
| 221 | + |
| 222 | + # Run the command inside the existing sandbox container |
| 223 | + nohup docker exec "$EXISTING_CONTAINER" claude --print "$RALPH_PROMPT" > "$LOG_FILE" 2>&1 & |
| 224 | + EXEC_PID=$! |
| 225 | + |
| 226 | + echo "Ralph started!" |
| 227 | + echo "" |
| 228 | + echo " Container: $EXISTING_CONTAINER" |
| 229 | + echo " PID: $EXEC_PID" |
| 230 | + echo " Log: $LOG_FILE" |
| 231 | + echo "" |
| 232 | + echo "MONITORING:" |
| 233 | + echo " tail -f $LOG_FILE" |
| 234 | + echo "" |
| 235 | + echo "PROGRESS:" |
| 236 | + echo " Progress is logged to Metis documents in:" |
| 237 | + echo " $PROJECT_DIR/.metis/" |
| 238 | + echo "" |
| 239 | + echo "STOP:" |
| 240 | + echo " kill $EXEC_PID" |
| 241 | + echo "" |
| 242 | +fi |
0 commit comments