edbeeching HF Staff
Port 270 AgentEscapeBench tasks to Harbor with OpenEnv integration (part 2)
49aa045 verified Download src/agentescape_port/runtime/worker.py from HuggingFaceSwarms/AgentEscapeBench: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/datasets/HuggingFaceSwarms/AgentEscapeBench/resolve/refs%2Fpr%2F3/src/agentescape_port/runtime/worker.py
- Command line
-
hf download hf://datasets/HuggingFaceSwarms/AgentEscapeBench@refs/pr/3/src/agentescape_port/runtime/worker.py
-
curl -L -o worker.py https://huggingface.co/datasets/HuggingFaceSwarms/AgentEscapeBench/resolve/refs%2Fpr%2F3/src/agentescape_port/runtime/worker.py
2.5 kB
| """Stateless Python tool worker. Never receives a scenario or reference values.""" | |
| import json | |
| import os | |
| import signal | |
| import subprocess | |
| import sys | |
| import tempfile | |
| import threading | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel, ConfigDict | |
| app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) | |
| lock = threading.Lock() | |
| class Execution(BaseModel): | |
| model_config = ConfigDict(extra="forbid", strict=True) | |
| code: str | |
| def health(): | |
| return {"ok": True} | |
| def execute(request: Execution): | |
| if len(request.code.encode()) > 65536: | |
| return {"stdout": "Error: code exceeds 64 KiB limit"} | |
| with lock, tempfile.TemporaryDirectory(prefix="escape-code-") as directory: | |
| os.chown(directory, 65534, 65534) | |
| with tempfile.TemporaryFile() as output: | |
| process = subprocess.Popen( | |
| [sys.executable, "-I", "/app/child.py"], | |
| stdin=subprocess.PIPE, | |
| stdout=output, | |
| stderr=subprocess.DEVNULL, | |
| cwd=directory, | |
| user=65534, | |
| group=65534, | |
| extra_groups=[], | |
| start_new_session=True, | |
| env={"PATH": "/usr/local/bin:/usr/bin:/bin"}, | |
| ) | |
| try: | |
| process.communicate( | |
| json.dumps({"code": request.code}).encode(), timeout=5 | |
| ) | |
| except subprocess.TimeoutExpired: | |
| os.killpg(process.pid, signal.SIGKILL) | |
| process.wait() | |
| return {"stdout": "Error: Python execution timed out"} | |
| finally: | |
| # Also remove children still in the process group after normal exit. | |
| try: | |
| os.killpg(process.pid, signal.SIGKILL) | |
| except ProcessLookupError: | |
| pass | |
| output.seek(0) | |
| value = output.read(65537) | |
| if len(value) > 65536 or process.returncode != 0: | |
| return { | |
| "stdout": "Error: Python execution exceeded a resource limit or failed" | |
| } | |
| try: | |
| result = json.loads(value) | |
| if not isinstance(result.get("stdout"), str): | |
| raise ValueError("Invalid worker response") | |
| return result | |
| except (ValueError, AttributeError): | |
| return {"stdout": "Error: Python execution returned invalid output"} | |