Spaces:
Sleeping
Sleeping
File size: 12,111 Bytes
fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd fdce872 83ea4bd | 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | """
simulator.py β Dockerless simulator for SWEbench-IN.
Replaces all Docker container operations with:
- A per-episode temp directory (virtual filesystem)
- Local subprocess execution (sandboxed to work_dir)
- In-process pytest via subprocess
- Local Flask server started as a child process
- requests to localhost for server health checks
"""
import ast
import os
import re
import sys
import time
import shutil
import socket
import tempfile
import subprocess
import threading
import requests as http_requests
from tasks import TASKS
class Simulator:
"""Dockerless executor for the SWEbench-IN environment."""
def __init__(self, container_id: str = None):
# container_id kept for API compatibility β ignored
self.work_dir: str = None
self.output_dir: str = None
self.reply_log: list[str] = []
self._server_proc: subprocess.Popen = None
self._zombie_sock: socket.socket = None
self._server_port: int = 8080
# ------------------------------------------------------------------
# Task setup / reset
# ------------------------------------------------------------------
def setup_task(self, task_id: int) -> str:
"""Reset to a fresh temp directory with the broken task files."""
self._kill_server()
self._kill_zombie()
# Fresh working directory each episode
if self.work_dir and os.path.exists(self.work_dir):
shutil.rmtree(self.work_dir, ignore_errors=True)
self.work_dir = tempfile.mkdtemp(prefix=f"swebench_task{task_id}_")
self.output_dir = os.path.join(self.work_dir, "output")
self.reply_log = []
self._make_dirs()
task = TASKS[task_id]
# Write broken source files
self._write(os.path.join(self.work_dir, "app.py"), task.broken_app_code)
if task.broken_app_code_2:
self._write(os.path.join(self.work_dir, "utils.py"), task.broken_app_code_2)
# Write tests
self._write(
os.path.join(self.work_dir, "tests", "test_app.py"),
task.test_code,
)
# Write message files
for fname, content in [
("slack.txt", task.slack_message),
("email.txt", task.email_message),
("hr.txt", task.hr_message),
]:
self._write(
os.path.join(self.work_dir, "messages", fname),
content or "",
)
# Error log
self._write(
os.path.join(self.work_dir, "logs", "error.log"),
f"Task {task_id}: {task.description}",
)
# Task-specific breakage
if task_id in (4, 5):
# Simulate zombie process blocking port 8080
self._start_zombie()
return f"Task {task_id} ready in {self.work_dir}"
def get_initial_observation(self, task_id: int) -> str:
task = TASKS[task_id]
parts = []
log_path = os.path.join(self.work_dir, "logs", "error.log")
if os.path.exists(log_path):
parts.append(f"=== ERROR LOG ===\n{open(log_path).read()}")
if task.slack_message:
parts.append(f"=== SLACK MESSAGE (from Manager) ===\n{task.slack_message}")
if task.email_message:
parts.append(f"=== EMAIL (from Client) ===\n{task.email_message}")
if task.hr_message:
parts.append(f"=== HR MESSAGE ===\n{task.hr_message}")
parts.append(f"\n--- Task: {task.name} ---")
parts.append(f"Description: {task.description}")
parts.append(f"Max actions: {task.max_actions}")
return "\n\n".join(parts)
# ------------------------------------------------------------------
# Action handlers
# ------------------------------------------------------------------
def run_bash(self, command: str) -> str:
"""Execute a shell command inside work_dir (no Docker)."""
blocked = ["sudo", "rm -rf /", "chmod 777 /"]
for pattern in blocked:
if pattern in command:
return f"BLOCKED: '{pattern}' is forbidden."
# pip install flask β simulate as no-op (flask is available on HF Spaces)
if re.search(r"pip\s+install\s+flask", command):
return "Requirement already satisfied: flask"
# Kill zombie process (tasks 4 & 5)
if any(k in command for k in ["pkill", "fuser -k", "kill"]):
self._kill_zombie()
return "OK: Port 8080 cleared."
# Start Flask server
if re.search(r"python.*app\.py", command) or "flask run" in command:
return self._start_server()
# General command β run locally in work_dir
try:
result = subprocess.run(
command,
shell=True,
cwd=self.work_dir,
capture_output=True,
text=True,
timeout=10,
env={**os.environ, "PYTHONPATH": self.work_dir},
)
output = (result.stdout + result.stderr).strip()
return output or "(no output)"
except subprocess.TimeoutExpired:
return "ERROR: Command timed out after 10 seconds."
except Exception as e:
return f"ERROR: {e}"
def read_file(self, path: str) -> str:
"""Read a file from work_dir. Accepts /home/user2/... or relative paths."""
full = self._resolve(path)
try:
return open(full).read()
except FileNotFoundError:
return f"ERROR: File not found: {path}"
except Exception as e:
return f"ERROR: {e}"
def write_file(self, path: str, content: str) -> str:
"""Write content to a file in work_dir."""
full = self._resolve(path)
os.makedirs(os.path.dirname(full), exist_ok=True)
try:
self._write(full, content)
return f"OK: Written to {path}"
except Exception as e:
return f"ERROR: {e}"
def run_pytest(self) -> dict:
"""Run pytest in work_dir and return pass/fail counts."""
try:
result = subprocess.run(
[sys.executable, "-m", "pytest", "tests/", "--tb=short", "-q"],
cwd=self.work_dir,
capture_output=True,
text=True,
timeout=30,
env={**os.environ, "PYTHONPATH": self.work_dir},
)
output = result.stdout + result.stderr
passed = int(m.group(1)) if (m := re.search(r"(\d+) passed", output)) else 0
failed = int(m.group(1)) if (m := re.search(r"(\d+) failed", output)) else 0
errors = int(m.group(1)) if (m := re.search(r"(\d+) error", output)) else 0
total = passed + failed + errors
return {
"passed": passed,
"failed": failed + errors,
"ratio": passed / total if total > 0 else 0.0,
"output": output,
}
except subprocess.TimeoutExpired:
return {"passed": 0, "failed": 0, "ratio": 0.0, "output": "ERROR: pytest timed out."}
except Exception as e:
return {"passed": 0, "failed": 0, "ratio": 0.0, "output": f"ERROR: {e}"}
def curl_server(self) -> dict:
"""Check if the Flask server is up at localhost:8080."""
try:
r = http_requests.get(
f"http://localhost:{self._server_port}", timeout=3
)
return {"status_code": r.status_code, "success": r.status_code == 200}
except Exception:
return {"status_code": 0, "success": False}
def write_reply(self, recipient: str, content: str) -> str:
"""Append a reply to output/reply.txt."""
formatted = f"[{recipient.upper()}]: {content}\n"
reply_path = os.path.join(self.output_dir, "reply.txt")
os.makedirs(self.output_dir, exist_ok=True)
try:
with open(reply_path, "a") as f:
f.write(formatted)
self.reply_log.append(formatted)
return f"OK: Reply sent to {recipient.upper()}"
except Exception as e:
return f"ERROR: {e}"
# ------------------------------------------------------------------
# Internal helpers
# ------------------------------------------------------------------
def _make_dirs(self):
for sub in ("tests", "logs", "messages", "output"):
os.makedirs(os.path.join(self.work_dir, sub), exist_ok=True)
@staticmethod
def _write(path: str, content: str):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(content)
def _resolve(self, path: str) -> str:
"""Translate /home/user2/... or bare relative path to work_dir path."""
norm = path.replace("/home/user2/", "").lstrip("/")
return os.path.join(self.work_dir, norm)
def _start_server(self) -> str:
"""Launch app.py as a child process on port 8080."""
self._kill_server()
app_path = os.path.join(self.work_dir, "app.py")
if not os.path.exists(app_path):
return "ERROR: app.py not found."
# Syntax check before launching
try:
ast.parse(open(app_path).read())
except SyntaxError as e:
return f"ERROR: Syntax error in app.py β {e}"
# Check if zombie is blocking the port
if self._port_in_use(self._server_port):
return (
f"ERROR: Port {self._server_port} is already in use. "
"Kill the blocking process first."
)
try:
self._server_proc = subprocess.Popen(
[sys.executable, "app.py"],
cwd=self.work_dir,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env={**os.environ, "PYTHONPATH": self.work_dir},
)
except Exception as e:
return f"ERROR: Could not start server β {e}"
# Wait up to 4 s for server to accept connections
for _ in range(8):
time.sleep(0.5)
if self._server_proc.poll() is not None:
return "ERROR: Server crashed on startup."
if not self._port_in_use(self._server_port):
continue
result = self.curl_server()
if result["success"]:
return "OK: Server started on port 8080."
# Server started but hasn't responded yet β return optimistic message
if self._server_proc.poll() is None:
return "OK: Server process started (may need a moment to be ready)."
return "ERROR: Server failed to start."
def _start_zombie(self):
"""Block port 8080 with a socket to simulate a zombie process."""
try:
self._zombie_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._zombie_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._zombie_sock.bind(("0.0.0.0", self._server_port))
self._zombie_sock.listen(1)
except OSError:
self._zombie_sock = None # Port already in use β fine
def _kill_zombie(self):
if self._zombie_sock:
try:
self._zombie_sock.close()
except Exception:
pass
self._zombie_sock = None
time.sleep(0.3) # Brief pause for OS to release the port
def _kill_server(self):
if self._server_proc:
try:
self._server_proc.terminate()
self._server_proc.wait(timeout=3)
except Exception:
try:
self._server_proc.kill()
except Exception:
pass
self._server_proc = None
@staticmethod
def _port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0 |