edbeeching HF Staff
Port 270 AgentEscapeBench tasks to Harbor with OpenEnv integration (part 13)
1049942 verified Download tests/openenv_smoke.py from HuggingFaceSwarms/AgentEscapeBench: direct link, hf CLI and curl.
- Browser
- Download file 5 kB
-
https://huggingface.co/datasets/HuggingFaceSwarms/AgentEscapeBench/resolve/refs%2Fpr%2F3/tests/openenv_smoke.py
- Command line
-
hf download hf://datasets/HuggingFaceSwarms/AgentEscapeBench@refs/pr/3/tests/openenv_smoke.py
-
curl -L -o openenv_smoke.py https://huggingface.co/datasets/HuggingFaceSwarms/AgentEscapeBench/resolve/refs%2Fpr%2F3/tests/openenv_smoke.py
5 kB
| """Actual OpenEnv rollout/capture smoke test with scripted endpoint and oracle. | |
| No paid model, model capability score, or training token/logprob validation. | |
| """ | |
| import argparse | |
| import asyncio | |
| import json | |
| import socket | |
| import threading | |
| import uuid | |
| from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | |
| from pathlib import Path | |
| from harbor.agents.factory import AgentFactory | |
| from harbor.models.agent.name import AgentName | |
| from openenv.core.harness.capture import CaptureServer | |
| from openenv.harbor import seams | |
| from openenv.harbor.rollout import run_rollout | |
| from openenv.harbor.tasks import resolve_task_dirs | |
| class ScriptedEndpoint(BaseHTTPRequestHandler): | |
| def do_POST(self): | |
| json.loads(self.rfile.read(int(self.headers["Content-Length"]))) | |
| result = { | |
| "id": "chatcmpl-validation", | |
| "object": "chat.completion", | |
| "created": 1, | |
| "model": "validation-oracle", | |
| "choices": [ | |
| { | |
| "index": 0, | |
| "message": { | |
| "role": "assistant", | |
| "content": "Validation acknowledged.", | |
| }, | |
| "finish_reason": "stop", | |
| } | |
| ], | |
| "usage": {"prompt_tokens": 10, "completion_tokens": 3, "total_tokens": 13}, | |
| } | |
| body = json.dumps(result).encode() | |
| self.send_response(200) | |
| self.send_header("Content-Type", "application/json") | |
| self.send_header("Content-Length", str(len(body))) | |
| self.end_headers() | |
| self.wfile.write(body) | |
| def log_message(self, *_): | |
| pass | |
| async def main(args): | |
| root = Path(__file__).resolve().parents[1] | |
| paths = resolve_task_dirs(str(args.dataset)) | |
| if args.task_ids: | |
| paths = [path for path in paths if path.name in args.task_ids] | |
| assert len(paths) == len(args.task_ids) | |
| scripted = ThreadingHTTPServer(("127.0.0.1", 0), ScriptedEndpoint) | |
| thread = threading.Thread(target=scripted.serve_forever, daemon=True) | |
| thread.start() | |
| with socket.socket() as reservation: | |
| reservation.bind(("127.0.0.1", 0)) | |
| port = reservation.getsockname()[1] | |
| capture = CaptureServer( | |
| llm_url=f"http://127.0.0.1:{scripted.server_port}/v1", | |
| model="validation-oracle", | |
| port=port, | |
| capture_level="text", | |
| admin_key=uuid.uuid4().hex, | |
| ) | |
| seams.SEAMS["oracle"] = seams.Seam( | |
| name="oracle", | |
| dialect="openai_chat", | |
| kwargs=lambda base, session, model: { | |
| "validation_base_url": base, | |
| "validation_session": session, | |
| }, | |
| notes="Validation only: scripted capture call followed by oracle", | |
| ) | |
| original = AgentFactory._AGENT_MAP[AgentName.ORACLE] | |
| AgentFactory._AGENT_MAP[AgentName.ORACLE] = ( | |
| "agentescape_port.testing:CaptureOracleAgent" | |
| ) | |
| results = [] | |
| capture.start() | |
| try: | |
| for path in paths: | |
| result = await run_rollout( | |
| task_dir=path, | |
| harness="oracle", | |
| sandbox="docker", | |
| registry=capture.registry, | |
| intercept_url=f"http://127.0.0.1:{port}", | |
| model="openai/validation-oracle", | |
| trials_dir=root / "jobs/openenv-smoke", | |
| dataset=str(args.dataset), | |
| capture_level="text", | |
| purpose="eval", | |
| ) | |
| results.append(result.model_dump(mode="json")) | |
| finally: | |
| capture.stop() | |
| scripted.shutdown() | |
| scripted.server_close() | |
| AgentFactory._AGENT_MAP[AgentName.ORACLE] = original | |
| args.report.write_text( | |
| json.dumps( | |
| { | |
| "scope": "Real OpenEnv task discovery, run_rollout and text capture using a scripted localhost endpoint, followed by public-action oracle. No paid model or token/logprob validation.", | |
| "results": results, | |
| }, | |
| indent=2, | |
| ) | |
| ) | |
| for result in results: | |
| print( | |
| json.dumps( | |
| { | |
| key: result.get(key) | |
| for key in ( | |
| "task_name", | |
| "reward", | |
| "ok", | |
| "error", | |
| "n_turns", | |
| "findings", | |
| ) | |
| } | |
| ) | |
| ) | |
| assert results and all( | |
| result["ok"] and result["reward"] == 1.0 and result["n_turns"] >= 1 | |
| for result in results | |
| ) | |
| if __name__ == "__main__": | |
| root = Path(__file__).resolve().parents[1] | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--dataset", type=Path, default=root / "pilot-tasks") | |
| parser.add_argument("--task-ids", nargs="*") | |
| parser.add_argument( | |
| "--report", type=Path, default=root / "reports/openenv-smoke.json" | |
| ) | |
| asyncio.run(main(parser.parse_args())) | |