Download release/engine/modules/appworld-9d84d79baf31.py from AI45Research/ManipulationArena: direct link, hf CLI and curl.
- Browser
- Download file 4.22 kB
-
https://huggingface.co/datasets/AI45Research/ManipulationArena/resolve/main/release/engine/modules/appworld-9d84d79baf31.py
- Command line
-
hf download hf://datasets/AI45Research/ManipulationArena/release/engine/modules/appworld-9d84d79baf31.py
-
curl -L -o appworld-9d84d79baf31.py https://huggingface.co/datasets/AI45Research/ManipulationArena/resolve/main/release/engine/modules/appworld-9d84d79baf31.py
4.22 kB
| """Trusted API bridge outside the native AppWorld actor UID boundary.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import socket | |
| import socketserver | |
| import struct | |
| import threading | |
| class NativeCallError(RuntimeError): | |
| pass | |
| class ApiBridge: | |
| def __init__(self, workspace: Path, worker, *, actor_uid=65532,suite='appworld'): | |
| self.path = workspace / ".benign30-interventions.sock" | |
| self.worker = worker | |
| self.errors = [] | |
| owner = self | |
| class Handler(socketserver.StreamRequestHandler): | |
| def handle(self): | |
| _, uid, _ = struct.unpack("3i", self.request.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12)) | |
| if uid != actor_uid: | |
| return | |
| self.request.settimeout(5400) | |
| def receive(): | |
| line = self.rfile.readline(16 * 1024 * 1024) | |
| if not line or not line.endswith(b"\n"): | |
| raise NativeCallError("actor_bridge_connection_incomplete") | |
| return json.loads(line) | |
| def send(value): | |
| self.wfile.write(json.dumps(value, ensure_ascii=False).encode() + b"\n") | |
| self.wfile.flush() | |
| try: | |
| selected = receive() | |
| if selected.get('kind')=='workspace_read': | |
| import base64 | |
| from .workspace import observe_read | |
| observed=observe_read(owner.worker,selected['path'],base64.b64decode(selected['content'],validate=True), | |
| proof={'executor':'native_appworld_python_file_read'}) | |
| send({'observed':observed});return | |
| target = selected["target"] | |
| if not isinstance(target, str) or not target or (suite=='appworld' and target.count("/") != 1): | |
| raise NativeCallError("invalid_native_target") | |
| def execute(arguments): | |
| send({"execute": True}) | |
| response = receive() | |
| if "native_error" in response: | |
| send({"native_error_ack": True}) | |
| raise NativeCallError("native_request_failed") | |
| return response["native_result"] | |
| def deliver(result): | |
| send({"result": result}) | |
| return receive() == {"ack": True} | |
| owner.worker.invoke(suite+"/" + target, selected["arguments"], execute, deliver) | |
| send({"complete": True}) | |
| except Exception as error: | |
| if str(error)!="native_request_failed": | |
| owner.errors.append(type(error).__name__) | |
| import sys as _sys; print(f"appworld bridge error: {type(error).__name__}: {error}", file=_sys.stderr, flush=True) | |
| class Server(socketserver.ThreadingUnixStreamServer): | |
| daemon_threads = True | |
| self.server = Server(str(self.path), Handler) | |
| self.path.chmod(0o666) | |
| self.thread = threading.Thread(target=self.server.serve_forever, daemon=True) | |
| self.thread.start() | |
| def close(self): | |
| self.server.shutdown() | |
| self.server.server_close() | |
| self.path.unlink(missing_ok=True) | |
| if self.errors: | |
| # transient per-call transport errors (actor-side resets while | |
| # the self-preservation gate holds Blue's call) already surface to Blue as | |
| # failed calls and the round continues; record as evidence | |
| # instead of failing a completed round at close time | |
| import json as _json | |
| try: | |
| from pathlib import Path as _P | |
| _P(self.path.parent / "appworld_bridge_errors.json").write_text( | |
| _json.dumps({"count": len(self.errors), "errors": self.errors}), encoding="utf-8") | |
| except OSError: | |
| pass | |
| import sys as _sys; print(f"appworld bridge closed with {len(self.errors)} transient errors (recorded, non-fatal)", file=_sys.stderr, flush=True) | |