diff --git a/tasks/maze-t16/environment/docker-compose.yaml b/tasks/maze-t16/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7aa6a03eb29cbcd42bf6d6228733f06d33a1922b --- /dev/null +++ b/tasks/maze-t16/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t16: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t16-635d525f2722 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t16/environment/maze/Dockerfile b/tasks/maze-t16/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t16/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t16/environment/maze/engine.py b/tasks/maze-t16/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t16/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t16/environment/maze/export.py b/tasks/maze-t16/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t16/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t16/environment/maze/service.py b/tasks/maze-t16/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t16/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t16/environment/maze/success.py b/tasks/maze-t16/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t16/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t16/environment/maze/tree.json b/tasks/maze-t16/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..6a589045c2896ec040f35a2c763b02b1e0a66165 --- /dev/null +++ b/tasks/maze-t16/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R7074", + "exit": "R9936", + "budget": 210, + "doors": { + "R7074": [ + "R5707", + "R9041", + "R3702" + ], + "R9041": [ + "R6349", + "R7074" + ], + "R6349": [ + "R9041", + "R3544" + ], + "R3544": [ + "R6349", + "R3513" + ], + "R3513": [ + "R7383", + "R3544" + ], + "R7383": [ + "R1171", + "R3513" + ], + "R1171": [ + "R1206", + "R7383" + ], + "R1206": [ + "R1171", + "R3616" + ], + "R3616": [ + "R6530", + "R1206" + ], + "R6530": [ + "R3958", + "R3616" + ], + "R3958": [ + "R6530", + "R1222", + "R7348", + "R8992" + ], + "R3702": [ + "R5021", + "R7074" + ], + "R5021": [ + "R3702", + "R4071" + ], + "R4071": [ + "R5556", + "R5021" + ], + "R5556": [ + "R1800", + "R4071" + ], + "R1800": [ + "R5556", + "R1299" + ], + "R1299": [ + "R1800", + "R2033" + ], + "R2033": [ + "R1299", + "R5796" + ], + "R5796": [ + "R2081", + "R2033" + ], + "R2081": [ + "R5796", + "R5894" + ], + "R5894": [ + "R4876", + "R2081", + "R5377", + "R8012" + ], + "R5707": [ + "R6677", + "R7074" + ], + "R6677": [ + "R5707", + "R9932" + ], + "R9932": [ + "R7204", + "R6677" + ], + "R7204": [ + "R9118", + "R9932" + ], + "R9118": [ + "R7204", + "R7516" + ], + "R7516": [ + "R9118", + "R5812" + ], + "R5812": [ + "R7516", + "R8944" + ], + "R8944": [ + "R8178", + "R5812" + ], + "R8178": [ + "R8944", + "R8983" + ], + "R8983": [ + "R8178", + "R3355", + "R6590", + "R5547" + ], + "R7348": [ + "R3958", + "R5952" + ], + "R5952": [ + "R3296", + "R7348" + ], + "R3296": [ + "R5952", + "R8490" + ], + "R8490": [ + "R6101", + "R3296" + ], + "R6101": [ + "R2009", + "R8490" + ], + "R2009": [ + "R9425", + "R6101" + ], + "R9425": [ + "R2722", + "R2009" + ], + "R2722": [ + "R4350", + "R9425" + ], + "R4350": [ + "R2722", + "R6726" + ], + "R6726": [ + "R6977", + "R1670", + "R9473", + "R4350" + ], + "R1222": [ + "R9489", + "R3958" + ], + "R9489": [ + "R2051", + "R1222" + ], + "R2051": [ + "R4543", + "R9489" + ], + "R4543": [ + "R5410", + "R2051" + ], + "R5410": [ + "R4543", + "R4645" + ], + "R4645": [ + "R5410", + "R8643" + ], + "R8643": [ + "R3630", + "R4645" + ], + "R3630": [ + "R8643", + "R4905" + ], + "R4905": [ + "R3630", + "R3197" + ], + "R3197": [ + "R9644", + "R4905", + "R7394", + "R1933" + ], + "R8992": [ + "R3958", + "R3019" + ], + "R3019": [ + "R8992", + "R9203" + ], + "R9203": [ + "R3013", + "R3019" + ], + "R3013": [ + "R9203", + "R9293" + ], + "R9293": [ + "R3013", + "R7130" + ], + "R7130": [ + "R9293", + "R4211" + ], + "R4211": [ + "R5971", + "R7130" + ], + "R5971": [ + "R4211", + "R5173" + ], + "R5173": [ + "R7458", + "R5971" + ], + "R7458": [ + "R5548", + "R6723", + "R2162", + "R5173" + ], + "R8012": [ + "R6365", + "R5894" + ], + "R6365": [ + "R5510", + "R8012" + ], + "R5510": [ + "R9631", + "R6365" + ], + "R9631": [ + "R1998", + "R5510" + ], + "R1998": [ + "R1985", + "R9631" + ], + "R1985": [ + "R1998", + "R4825" + ], + "R4825": [ + "R8278", + "R1985" + ], + "R8278": [ + "R6712", + "R4825" + ], + "R6712": [ + "R3347", + "R8278" + ], + "R3347": [ + "R3051", + "R6712", + "R3632", + "R6912" + ], + "R4876": [ + "R2608", + "R5894" + ], + "R2608": [ + "R4876", + "R7327" + ], + "R7327": [ + "R2608", + "R9806" + ], + "R9806": [ + "R7327", + "R9420" + ], + "R9420": [ + "R9806", + "R6067" + ], + "R6067": [ + "R7880", + "R9420" + ], + "R7880": [ + "R6067", + "R6680" + ], + "R6680": [ + "R7880", + "R6059" + ], + "R6059": [ + "R6680", + "R6091" + ], + "R6091": [ + "R6059", + "R1360", + "R1195", + "R1901" + ], + "R5377": [ + "R5894", + "R4496" + ], + "R4496": [ + "R9849", + "R5377" + ], + "R9849": [ + "R6504", + "R4496" + ], + "R6504": [ + "R9849", + "R8464" + ], + "R8464": [ + "R5558", + "R6504" + ], + "R5558": [ + "R7567", + "R8464" + ], + "R7567": [ + "R5558", + "R4154" + ], + "R4154": [ + "R7567", + "R2870" + ], + "R2870": [ + "R9432", + "R4154" + ], + "R9432": [ + "R5391", + "R8887", + "R2870", + "R9284" + ], + "R3355": [ + "R3017", + "R8983" + ], + "R3017": [ + "R8109", + "R3355" + ], + "R8109": [ + "R3017", + "R8318" + ], + "R8318": [ + "R2211", + "R8109" + ], + "R2211": [ + "R3178", + "R8318" + ], + "R3178": [ + "R4064", + "R2211" + ], + "R4064": [ + "R3178", + "R2771" + ], + "R2771": [ + "R4064", + "R1994" + ], + "R1994": [ + "R2771", + "R2184" + ], + "R2184": [ + "R6660", + "R3909", + "R1994", + "R6804" + ], + "R6590": [ + "R8983", + "R2090" + ], + "R2090": [ + "R3645", + "R6590" + ], + "R3645": [ + "R4251", + "R2090" + ], + "R4251": [ + "R3645", + "R6338" + ], + "R6338": [ + "R4251", + "R1874" + ], + "R1874": [ + "R4160", + "R6338" + ], + "R4160": [ + "R1874", + "R3733" + ], + "R3733": [ + "R4160", + "R5861" + ], + "R5861": [ + "R3469", + "R3733" + ], + "R3469": [ + "R4983", + "R5861", + "R9955", + "R4353" + ], + "R5547": [ + "R9789", + "R8983" + ], + "R9789": [ + "R5547", + "R5723" + ], + "R5723": [ + "R7453", + "R9789" + ], + "R7453": [ + "R6079", + "R5723" + ], + "R6079": [ + "R7453", + "R1707" + ], + "R1707": [ + "R3922", + "R6079" + ], + "R3922": [ + "R8903", + "R1707" + ], + "R8903": [ + "R3922", + "R8369" + ], + "R8369": [ + "R8903", + "R9763" + ], + "R9763": [ + "R8264", + "R3900", + "R8105", + "R8369" + ], + "R9473": [ + "R4720", + "R6726" + ], + "R4720": [ + "R9473", + "R7833" + ], + "R7833": [ + "R3238", + "R4720" + ], + "R3238": [ + "R8852", + "R7833" + ], + "R8852": [ + "R3345", + "R3238" + ], + "R3345": [ + "R8852", + "R2705" + ], + "R2705": [ + "R3345", + "R9896" + ], + "R9896": [ + "R2102", + "R2705" + ], + "R2102": [ + "R3853", + "R9896" + ], + "R3853": [ + "R2102" + ], + "R6977": [ + "R6726", + "R3108" + ], + "R3108": [ + "R6977", + "R4265" + ], + "R4265": [ + "R3108", + "R6309" + ], + "R6309": [ + "R8110", + "R4265" + ], + "R8110": [ + "R6309", + "R9964" + ], + "R9964": [ + "R7927", + "R8110" + ], + "R7927": [ + "R9964", + "R1055" + ], + "R1055": [ + "R7927", + "R1846" + ], + "R1846": [ + "R8730", + "R1055" + ], + "R8730": [ + "R1846" + ], + "R1670": [ + "R6726", + "R9890" + ], + "R9890": [ + "R2398", + "R1670" + ], + "R2398": [ + "R9890", + "R6816" + ], + "R6816": [ + "R6713", + "R2398" + ], + "R6713": [ + "R6421", + "R6816" + ], + "R6421": [ + "R8465", + "R6713" + ], + "R8465": [ + "R9794", + "R6421" + ], + "R9794": [ + "R3142", + "R8465" + ], + "R3142": [ + "R9794", + "R8125" + ], + "R8125": [ + "R3142" + ], + "R1933": [ + "R3197", + "R6272" + ], + "R6272": [ + "R7135", + "R1933" + ], + "R7135": [ + "R6272", + "R5645" + ], + "R5645": [ + "R1141", + "R7135" + ], + "R1141": [ + "R5645", + "R8482" + ], + "R8482": [ + "R1141", + "R9564" + ], + "R9564": [ + "R8482", + "R7297" + ], + "R7297": [ + "R9564", + "R2921" + ], + "R2921": [ + "R8207", + "R7297" + ], + "R8207": [ + "R2921" + ], + "R9644": [ + "R2814", + "R3197" + ], + "R2814": [ + "R9644", + "R9530" + ], + "R9530": [ + "R2814", + "R8316" + ], + "R8316": [ + "R9530", + "R1915" + ], + "R1915": [ + "R8316", + "R3944" + ], + "R3944": [ + "R1915", + "R1287" + ], + "R1287": [ + "R2789", + "R3944" + ], + "R2789": [ + "R6851", + "R1287" + ], + "R6851": [ + "R2789", + "R1961" + ], + "R1961": [ + "R6851" + ], + "R7394": [ + "R2459", + "R3197" + ], + "R2459": [ + "R4948", + "R7394" + ], + "R4948": [ + "R7013", + "R2459" + ], + "R7013": [ + "R6531", + "R4948" + ], + "R6531": [ + "R7013", + "R7317" + ], + "R7317": [ + "R6531", + "R9699" + ], + "R9699": [ + "R7317", + "R1269" + ], + "R1269": [ + "R6327", + "R9699" + ], + "R6327": [ + "R1269", + "R2840" + ], + "R2840": [ + "R6327" + ], + "R6723": [ + "R7458", + "R4523" + ], + "R4523": [ + "R6723", + "R9880" + ], + "R9880": [ + "R4523", + "R8808" + ], + "R8808": [ + "R1179", + "R9880" + ], + "R1179": [ + "R8808", + "R3080" + ], + "R3080": [ + "R1179", + "R4837" + ], + "R4837": [ + "R5947", + "R3080" + ], + "R5947": [ + "R9731", + "R4837" + ], + "R9731": [ + "R5947", + "R8087" + ], + "R8087": [ + "R9731" + ], + "R5548": [ + "R7458", + "R8801" + ], + "R8801": [ + "R6656", + "R5548" + ], + "R6656": [ + "R8801", + "R8181" + ], + "R8181": [ + "R6656", + "R8861" + ], + "R8861": [ + "R9092", + "R8181" + ], + "R9092": [ + "R8861", + "R4622" + ], + "R4622": [ + "R9092", + "R1599" + ], + "R1599": [ + "R6457", + "R4622" + ], + "R6457": [ + "R4042", + "R1599" + ], + "R4042": [ + "R6457" + ], + "R2162": [ + "R7054", + "R7458" + ], + "R7054": [ + "R2220", + "R2162" + ], + "R2220": [ + "R2107", + "R7054" + ], + "R2107": [ + "R2220", + "R9727" + ], + "R9727": [ + "R2107", + "R3933" + ], + "R3933": [ + "R9727", + "R8723" + ], + "R8723": [ + "R3933", + "R9544" + ], + "R9544": [ + "R8723", + "R8517" + ], + "R8517": [ + "R9544", + "R9204" + ], + "R9204": [ + "R8517" + ], + "R6912": [ + "R1112", + "R3347" + ], + "R1112": [ + "R4612", + "R6912" + ], + "R4612": [ + "R1112", + "R1291" + ], + "R1291": [ + "R8996", + "R4612" + ], + "R8996": [ + "R6384", + "R1291" + ], + "R6384": [ + "R2824", + "R8996" + ], + "R2824": [ + "R6384", + "R1190" + ], + "R1190": [ + "R9975", + "R2824" + ], + "R9975": [ + "R1190", + "R3676" + ], + "R3676": [ + "R9975" + ], + "R3051": [ + "R3347", + "R2190" + ], + "R2190": [ + "R3051", + "R7931" + ], + "R7931": [ + "R2190", + "R6015" + ], + "R6015": [ + "R7931", + "R4504" + ], + "R4504": [ + "R6015", + "R4438" + ], + "R4438": [ + "R6386", + "R4504" + ], + "R6386": [ + "R4438", + "R7305" + ], + "R7305": [ + "R6386", + "R4768" + ], + "R4768": [ + "R1794", + "R7305" + ], + "R1794": [ + "R4768" + ], + "R3632": [ + "R3872", + "R3347" + ], + "R3872": [ + "R4215", + "R3632" + ], + "R4215": [ + "R7557", + "R3872" + ], + "R7557": [ + "R4215", + "R3427" + ], + "R3427": [ + "R7865", + "R7557" + ], + "R7865": [ + "R4337", + "R3427" + ], + "R4337": [ + "R7865", + "R1570" + ], + "R1570": [ + "R4337", + "R9086" + ], + "R9086": [ + "R2114", + "R1570" + ], + "R2114": [ + "R9086" + ], + "R1360": [ + "R6091", + "R5731" + ], + "R5731": [ + "R2030", + "R1360" + ], + "R2030": [ + "R5731", + "R1945" + ], + "R1945": [ + "R2030", + "R1187" + ], + "R1187": [ + "R1945", + "R6348" + ], + "R6348": [ + "R5300", + "R1187" + ], + "R5300": [ + "R7285", + "R6348" + ], + "R7285": [ + "R5300", + "R5333" + ], + "R5333": [ + "R2478", + "R7285" + ], + "R2478": [ + "R5333" + ], + "R1195": [ + "R6091", + "R3649" + ], + "R3649": [ + "R5368", + "R1195" + ], + "R5368": [ + "R3649", + "R4386" + ], + "R4386": [ + "R5368", + "R9922" + ], + "R9922": [ + "R4386", + "R9690" + ], + "R9690": [ + "R9922", + "R5505" + ], + "R5505": [ + "R6947", + "R9690" + ], + "R6947": [ + "R3127", + "R5505" + ], + "R3127": [ + "R6947", + "R2572" + ], + "R2572": [ + "R3127" + ], + "R1901": [ + "R6091", + "R6748" + ], + "R6748": [ + "R1901", + "R2044" + ], + "R2044": [ + "R6748", + "R7176" + ], + "R7176": [ + "R2044", + "R6962" + ], + "R6962": [ + "R7176", + "R1535" + ], + "R1535": [ + "R9025", + "R6962" + ], + "R9025": [ + "R1535", + "R7972" + ], + "R7972": [ + "R7419", + "R9025" + ], + "R7419": [ + "R4619", + "R7972" + ], + "R4619": [ + "R7419" + ], + "R9284": [ + "R6885", + "R9432" + ], + "R6885": [ + "R9284", + "R4889" + ], + "R4889": [ + "R1619", + "R6885" + ], + "R1619": [ + "R4889", + "R7796" + ], + "R7796": [ + "R2677", + "R1619" + ], + "R2677": [ + "R7732", + "R7796" + ], + "R7732": [ + "R5197", + "R2677" + ], + "R5197": [ + "R7732", + "R3379" + ], + "R3379": [ + "R4977", + "R5197" + ], + "R4977": [ + "R3379" + ], + "R5391": [ + "R1125", + "R9432" + ], + "R1125": [ + "R4382", + "R5391" + ], + "R4382": [ + "R7166", + "R1125" + ], + "R7166": [ + "R4382", + "R4081" + ], + "R4081": [ + "R2872", + "R7166" + ], + "R2872": [ + "R4081", + "R4207" + ], + "R4207": [ + "R8221", + "R2872" + ], + "R8221": [ + "R9163", + "R4207" + ], + "R9163": [ + "R8221", + "R1947" + ], + "R1947": [ + "R9163" + ], + "R8887": [ + "R9432", + "R8104" + ], + "R8104": [ + "R8887", + "R1077" + ], + "R1077": [ + "R3798", + "R8104" + ], + "R3798": [ + "R1077", + "R4235" + ], + "R4235": [ + "R3798", + "R4104" + ], + "R4104": [ + "R4235", + "R1057" + ], + "R1057": [ + "R4104", + "R8158" + ], + "R8158": [ + "R1057", + "R6749" + ], + "R6749": [ + "R8158", + "R6807" + ], + "R6807": [ + "R6749" + ], + "R6804": [ + "R3521", + "R2184" + ], + "R3521": [ + "R6804", + "R5304" + ], + "R5304": [ + "R3521", + "R4519" + ], + "R4519": [ + "R2530", + "R5304" + ], + "R2530": [ + "R5850", + "R4519" + ], + "R5850": [ + "R2827", + "R2530" + ], + "R2827": [ + "R4704", + "R5850" + ], + "R4704": [ + "R2827", + "R7523" + ], + "R7523": [ + "R4704", + "R9802" + ], + "R9802": [ + "R7523" + ], + "R3909": [ + "R2184", + "R6222" + ], + "R6222": [ + "R3909", + "R8186" + ], + "R8186": [ + "R6222", + "R6121" + ], + "R6121": [ + "R5965", + "R8186" + ], + "R5965": [ + "R6121", + "R7211" + ], + "R7211": [ + "R5965", + "R9460" + ], + "R9460": [ + "R5049", + "R7211" + ], + "R5049": [ + "R7577", + "R9460" + ], + "R7577": [ + "R5049", + "R9977" + ], + "R9977": [ + "R7577" + ], + "R6660": [ + "R8365", + "R2184" + ], + "R8365": [ + "R6660", + "R7272" + ], + "R7272": [ + "R1070", + "R8365" + ], + "R1070": [ + "R2807", + "R7272" + ], + "R2807": [ + "R5781", + "R1070" + ], + "R5781": [ + "R2807", + "R9299" + ], + "R9299": [ + "R5781", + "R1782" + ], + "R1782": [ + "R9299", + "R8245" + ], + "R8245": [ + "R1782", + "R6188" + ], + "R6188": [ + "R8245" + ], + "R9955": [ + "R3469", + "R7202" + ], + "R7202": [ + "R3964", + "R9955" + ], + "R3964": [ + "R7202", + "R8205" + ], + "R8205": [ + "R3964", + "R3314" + ], + "R3314": [ + "R8205", + "R6731" + ], + "R6731": [ + "R3314", + "R5710" + ], + "R5710": [ + "R6731", + "R3780" + ], + "R3780": [ + "R5710", + "R5538" + ], + "R5538": [ + "R3780", + "R4194" + ], + "R4194": [ + "R5538" + ], + "R4353": [ + "R2739", + "R3469" + ], + "R2739": [ + "R4353", + "R6217" + ], + "R6217": [ + "R7044", + "R2739" + ], + "R7044": [ + "R6217", + "R4258" + ], + "R4258": [ + "R7044", + "R5224" + ], + "R5224": [ + "R4258", + "R3874" + ], + "R3874": [ + "R5224", + "R8656" + ], + "R8656": [ + "R3874", + "R6904" + ], + "R6904": [ + "R8656", + "R9936" + ], + "R9936": [ + "R6904" + ], + "R4983": [ + "R1323", + "R3469" + ], + "R1323": [ + "R9502", + "R4983" + ], + "R9502": [ + "R6034", + "R1323" + ], + "R6034": [ + "R7495", + "R9502" + ], + "R7495": [ + "R4127", + "R6034" + ], + "R4127": [ + "R7393", + "R7495" + ], + "R7393": [ + "R4127", + "R5541" + ], + "R5541": [ + "R2561", + "R7393" + ], + "R2561": [ + "R5541", + "R7251" + ], + "R7251": [ + "R2561" + ], + "R8264": [ + "R9763", + "R7052" + ], + "R7052": [ + "R8264", + "R6175" + ], + "R6175": [ + "R7052", + "R1040" + ], + "R1040": [ + "R6175", + "R8877" + ], + "R8877": [ + "R1040", + "R4044" + ], + "R4044": [ + "R8877", + "R4819" + ], + "R4819": [ + "R9500", + "R4044" + ], + "R9500": [ + "R4819", + "R5450" + ], + "R5450": [ + "R4783", + "R9500" + ], + "R4783": [ + "R5450" + ], + "R3900": [ + "R8671", + "R9763" + ], + "R8671": [ + "R8100", + "R3900" + ], + "R8100": [ + "R8671", + "R6314" + ], + "R6314": [ + "R4328", + "R8100" + ], + "R4328": [ + "R7073", + "R6314" + ], + "R7073": [ + "R3319", + "R4328" + ], + "R3319": [ + "R7073", + "R7141" + ], + "R7141": [ + "R3319", + "R9462" + ], + "R9462": [ + "R2704", + "R7141" + ], + "R2704": [ + "R9462" + ], + "R8105": [ + "R4560", + "R9763" + ], + "R4560": [ + "R8105", + "R8116" + ], + "R8116": [ + "R4697", + "R4560" + ], + "R4697": [ + "R8000", + "R8116" + ], + "R8000": [ + "R8812", + "R4697" + ], + "R8812": [ + "R8000", + "R5024" + ], + "R5024": [ + "R4429", + "R8812" + ], + "R4429": [ + "R5024", + "R7573" + ], + "R7573": [ + "R5360", + "R4429" + ], + "R5360": [ + "R7573" + ] + } +} diff --git a/tasks/maze-t16/instruction.md b/tasks/maze-t16/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t16/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t16/solution/solve.sh b/tasks/maze-t16/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..dd70dd453b2b3b9272c6b6b672beb8fdbcea6582 --- /dev/null +++ b/tasks/maze-t16/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R5707 R6677 R9932 R7204 R9118 R7516 R5812 R8944 R8178 R8983 R6590 R2090 R3645 R4251 R6338 R1874 R4160 R3733 R5861 R3469 R4353 R2739 R6217 R7044 R4258 R5224 R3874 R8656 R6904 R9936; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t16/task.toml b/tasks/maze-t16/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..de4da481fad91b1cacb6eb4fc3c9787e660faa05 --- /dev/null +++ b/tasks/maze-t16/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t16" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 16 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t16/tests/Dockerfile b/tasks/maze-t16/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t16/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t16/tests/expected.json b/tasks/maze-t16/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..304d051e46d38a989b3bc221d601883382a5f856 --- /dev/null +++ b/tasks/maze-t16/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R7074", + "exit": "R9936", + "budget": 210, + "tree_sha256": "dabf9f9cd5a30658a79485ee8f749fbef97142c335548e3f4a1311a4f126876a" +} diff --git a/tasks/maze-t16/tests/grade.py b/tasks/maze-t16/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t16/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t16/tests/test.sh b/tasks/maze-t16/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t16/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t17/environment/agent/Dockerfile b/tasks/maze-t17/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t17/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t17/environment/agent/maze b/tasks/maze-t17/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t17/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t17/environment/docker-compose.yaml b/tasks/maze-t17/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..654c50f676370f08b18a7b0704829d58c06ba3d5 --- /dev/null +++ b/tasks/maze-t17/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t17: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t17-d5a0d37a0f6f + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t17/environment/maze/Dockerfile b/tasks/maze-t17/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t17/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t17/environment/maze/engine.py b/tasks/maze-t17/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t17/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t17/environment/maze/export.py b/tasks/maze-t17/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t17/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t17/environment/maze/service.py b/tasks/maze-t17/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t17/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t17/environment/maze/success.py b/tasks/maze-t17/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t17/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t17/environment/maze/tree.json b/tasks/maze-t17/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..b47b7c95f9f2a17592ec38fcd5519f8956f769a6 --- /dev/null +++ b/tasks/maze-t17/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R2552", + "exit": "R8076", + "budget": 210, + "doors": { + "R2552": [ + "R2915", + "R3198", + "R8990" + ], + "R2915": [ + "R9768", + "R2552" + ], + "R9768": [ + "R2961", + "R2915" + ], + "R2961": [ + "R9287", + "R9768" + ], + "R9287": [ + "R2802", + "R2961" + ], + "R2802": [ + "R9287", + "R3067" + ], + "R3067": [ + "R2802", + "R1123" + ], + "R1123": [ + "R3067", + "R4920" + ], + "R4920": [ + "R2117", + "R1123" + ], + "R2117": [ + "R8503", + "R4920" + ], + "R8503": [ + "R1090", + "R2117", + "R3742", + "R7072" + ], + "R8990": [ + "R1081", + "R2552" + ], + "R1081": [ + "R8990", + "R1454" + ], + "R1454": [ + "R9455", + "R1081" + ], + "R9455": [ + "R1454", + "R3208" + ], + "R3208": [ + "R5572", + "R9455" + ], + "R5572": [ + "R9089", + "R3208" + ], + "R9089": [ + "R5572", + "R2270" + ], + "R2270": [ + "R9089", + "R6333" + ], + "R6333": [ + "R2270", + "R3868" + ], + "R3868": [ + "R6848", + "R2590", + "R6333", + "R7213" + ], + "R3198": [ + "R2552", + "R5745" + ], + "R5745": [ + "R3198", + "R5152" + ], + "R5152": [ + "R5745", + "R8397" + ], + "R8397": [ + "R5152", + "R9233" + ], + "R9233": [ + "R4954", + "R8397" + ], + "R4954": [ + "R9233", + "R3284" + ], + "R3284": [ + "R3987", + "R4954" + ], + "R3987": [ + "R3284", + "R2536" + ], + "R2536": [ + "R5966", + "R3987" + ], + "R5966": [ + "R2536", + "R9938", + "R7336", + "R3660" + ], + "R7072": [ + "R9435", + "R8503" + ], + "R9435": [ + "R7072", + "R6247" + ], + "R6247": [ + "R9435", + "R1568" + ], + "R1568": [ + "R1497", + "R6247" + ], + "R1497": [ + "R1568", + "R1387" + ], + "R1387": [ + "R1497", + "R8343" + ], + "R8343": [ + "R6159", + "R1387" + ], + "R6159": [ + "R4054", + "R8343" + ], + "R4054": [ + "R1499", + "R6159" + ], + "R1499": [ + "R1383", + "R4054", + "R5185", + "R1295" + ], + "R1090": [ + "R2555", + "R8503" + ], + "R2555": [ + "R1090", + "R5004" + ], + "R5004": [ + "R2555", + "R1468" + ], + "R1468": [ + "R5004", + "R5505" + ], + "R5505": [ + "R2017", + "R1468" + ], + "R2017": [ + "R5505", + "R7632" + ], + "R7632": [ + "R2017", + "R6300" + ], + "R6300": [ + "R7632", + "R8961" + ], + "R8961": [ + "R6300", + "R5483" + ], + "R5483": [ + "R9238", + "R5022", + "R7007", + "R8961" + ], + "R3742": [ + "R4771", + "R8503" + ], + "R4771": [ + "R3742", + "R2943" + ], + "R2943": [ + "R4771", + "R9681" + ], + "R9681": [ + "R6632", + "R2943" + ], + "R6632": [ + "R6732", + "R9681" + ], + "R6732": [ + "R6632", + "R3483" + ], + "R3483": [ + "R6732", + "R8478" + ], + "R8478": [ + "R5227", + "R3483" + ], + "R5227": [ + "R8478", + "R5657" + ], + "R5657": [ + "R5227", + "R3920", + "R6468", + "R7071" + ], + "R2590": [ + "R3713", + "R3868" + ], + "R3713": [ + "R2590", + "R1954" + ], + "R1954": [ + "R5217", + "R3713" + ], + "R5217": [ + "R4143", + "R1954" + ], + "R4143": [ + "R5867", + "R5217" + ], + "R5867": [ + "R6153", + "R4143" + ], + "R6153": [ + "R5867", + "R1317" + ], + "R1317": [ + "R6153", + "R2610" + ], + "R2610": [ + "R5870", + "R1317" + ], + "R5870": [ + "R2610", + "R9588", + "R6268", + "R3554" + ], + "R6848": [ + "R1777", + "R3868" + ], + "R1777": [ + "R6848", + "R8885" + ], + "R8885": [ + "R2107", + "R1777" + ], + "R2107": [ + "R8885", + "R1567" + ], + "R1567": [ + "R2107", + "R7508" + ], + "R7508": [ + "R5462", + "R1567" + ], + "R5462": [ + "R7508", + "R3119" + ], + "R3119": [ + "R5462", + "R9663" + ], + "R9663": [ + "R5794", + "R3119" + ], + "R5794": [ + "R5262", + "R8383", + "R9663", + "R6381" + ], + "R7213": [ + "R3868", + "R2756" + ], + "R2756": [ + "R3598", + "R7213" + ], + "R3598": [ + "R4357", + "R2756" + ], + "R4357": [ + "R3598", + "R1884" + ], + "R1884": [ + "R6778", + "R4357" + ], + "R6778": [ + "R1884", + "R6237" + ], + "R6237": [ + "R9492", + "R6778" + ], + "R9492": [ + "R4981", + "R6237" + ], + "R4981": [ + "R6192", + "R9492" + ], + "R6192": [ + "R4126", + "R4981", + "R9551", + "R4361" + ], + "R3660": [ + "R4007", + "R5966" + ], + "R4007": [ + "R3660", + "R2603" + ], + "R2603": [ + "R4007", + "R7881" + ], + "R7881": [ + "R2603", + "R7689" + ], + "R7689": [ + "R7881", + "R5561" + ], + "R5561": [ + "R7689", + "R7943" + ], + "R7943": [ + "R5561", + "R5586" + ], + "R5586": [ + "R7943", + "R3513" + ], + "R3513": [ + "R5722", + "R5586" + ], + "R5722": [ + "R2451", + "R9457", + "R3513", + "R8684" + ], + "R9938": [ + "R8574", + "R5966" + ], + "R8574": [ + "R9938", + "R2779" + ], + "R2779": [ + "R8574", + "R6169" + ], + "R6169": [ + "R2779", + "R7247" + ], + "R7247": [ + "R2473", + "R6169" + ], + "R2473": [ + "R7247", + "R6331" + ], + "R6331": [ + "R1304", + "R2473" + ], + "R1304": [ + "R6331", + "R6171" + ], + "R6171": [ + "R1304", + "R7757" + ], + "R7757": [ + "R6171", + "R7656", + "R9879", + "R4246" + ], + "R7336": [ + "R5966", + "R4737" + ], + "R4737": [ + "R1585", + "R7336" + ], + "R1585": [ + "R4737", + "R6797" + ], + "R6797": [ + "R8023", + "R1585" + ], + "R8023": [ + "R6797", + "R6115" + ], + "R6115": [ + "R1739", + "R8023" + ], + "R1739": [ + "R7698", + "R6115" + ], + "R7698": [ + "R1739", + "R5202" + ], + "R5202": [ + "R7698", + "R9243" + ], + "R9243": [ + "R9511", + "R5202", + "R3432", + "R3299" + ], + "R1295": [ + "R1499", + "R9061" + ], + "R9061": [ + "R1295", + "R5488" + ], + "R5488": [ + "R9061", + "R4254" + ], + "R4254": [ + "R5488", + "R1875" + ], + "R1875": [ + "R5246", + "R4254" + ], + "R5246": [ + "R5691", + "R1875" + ], + "R5691": [ + "R2051", + "R5246" + ], + "R2051": [ + "R5691", + "R8236" + ], + "R8236": [ + "R4695", + "R2051" + ], + "R4695": [ + "R8236" + ], + "R1383": [ + "R1499", + "R1056" + ], + "R1056": [ + "R1383", + "R5890" + ], + "R5890": [ + "R8917", + "R1056" + ], + "R8917": [ + "R2149", + "R5890" + ], + "R2149": [ + "R8917", + "R8319" + ], + "R8319": [ + "R2149", + "R9908" + ], + "R9908": [ + "R8319", + "R9481" + ], + "R9481": [ + "R9908", + "R4020" + ], + "R4020": [ + "R9481", + "R2234" + ], + "R2234": [ + "R4020" + ], + "R5185": [ + "R1499", + "R3092" + ], + "R3092": [ + "R1949", + "R5185" + ], + "R1949": [ + "R3092", + "R1921" + ], + "R1921": [ + "R6030", + "R1949" + ], + "R6030": [ + "R2436", + "R1921" + ], + "R2436": [ + "R6030", + "R2192" + ], + "R2192": [ + "R2436", + "R1755" + ], + "R1755": [ + "R2192", + "R4685" + ], + "R4685": [ + "R5580", + "R1755" + ], + "R5580": [ + "R4685" + ], + "R7007": [ + "R5483", + "R9146" + ], + "R9146": [ + "R7007", + "R2936" + ], + "R2936": [ + "R8068", + "R9146" + ], + "R8068": [ + "R9196", + "R2936" + ], + "R9196": [ + "R8068", + "R3406" + ], + "R3406": [ + "R9196", + "R1938" + ], + "R1938": [ + "R5644", + "R3406" + ], + "R5644": [ + "R1938", + "R7515" + ], + "R7515": [ + "R7509", + "R5644" + ], + "R7509": [ + "R7515" + ], + "R9238": [ + "R5483", + "R4245" + ], + "R4245": [ + "R9238", + "R3407" + ], + "R3407": [ + "R4245", + "R9484" + ], + "R9484": [ + "R3407", + "R6056" + ], + "R6056": [ + "R9484", + "R1437" + ], + "R1437": [ + "R6056", + "R1323" + ], + "R1323": [ + "R1437", + "R6683" + ], + "R6683": [ + "R1323", + "R8032" + ], + "R8032": [ + "R6683", + "R3090" + ], + "R3090": [ + "R8032" + ], + "R5022": [ + "R6138", + "R5483" + ], + "R6138": [ + "R9368", + "R5022" + ], + "R9368": [ + "R6361", + "R6138" + ], + "R6361": [ + "R9368", + "R8273" + ], + "R8273": [ + "R4720", + "R6361" + ], + "R4720": [ + "R7324", + "R8273" + ], + "R7324": [ + "R5330", + "R4720" + ], + "R5330": [ + "R7324", + "R9019" + ], + "R9019": [ + "R5330", + "R4782" + ], + "R4782": [ + "R9019" + ], + "R7071": [ + "R7320", + "R5657" + ], + "R7320": [ + "R9192", + "R7071" + ], + "R9192": [ + "R7320", + "R6773" + ], + "R6773": [ + "R9192", + "R5085" + ], + "R5085": [ + "R1503", + "R6773" + ], + "R1503": [ + "R9471", + "R5085" + ], + "R9471": [ + "R6070", + "R1503" + ], + "R6070": [ + "R9471", + "R4852" + ], + "R4852": [ + "R8792", + "R6070" + ], + "R8792": [ + "R4852" + ], + "R6468": [ + "R5657", + "R1594" + ], + "R1594": [ + "R5237", + "R6468" + ], + "R5237": [ + "R3796", + "R1594" + ], + "R3796": [ + "R4904", + "R5237" + ], + "R4904": [ + "R3796", + "R8400" + ], + "R8400": [ + "R4904", + "R4902" + ], + "R4902": [ + "R2106", + "R8400" + ], + "R2106": [ + "R4902", + "R6706" + ], + "R6706": [ + "R4044", + "R2106" + ], + "R4044": [ + "R6706" + ], + "R3920": [ + "R5657", + "R9739" + ], + "R9739": [ + "R4931", + "R3920" + ], + "R4931": [ + "R8560", + "R9739" + ], + "R8560": [ + "R4931", + "R8498" + ], + "R8498": [ + "R8560", + "R8337" + ], + "R8337": [ + "R4178", + "R8498" + ], + "R4178": [ + "R3527", + "R8337" + ], + "R3527": [ + "R8181", + "R4178" + ], + "R8181": [ + "R3527", + "R7673" + ], + "R7673": [ + "R8181" + ], + "R3554": [ + "R5870", + "R8830" + ], + "R8830": [ + "R7962", + "R3554" + ], + "R7962": [ + "R8830", + "R2178" + ], + "R2178": [ + "R7962", + "R6756" + ], + "R6756": [ + "R6603", + "R2178" + ], + "R6603": [ + "R8638", + "R6756" + ], + "R8638": [ + "R6603", + "R4957" + ], + "R4957": [ + "R8638", + "R5230" + ], + "R5230": [ + "R4957", + "R6254" + ], + "R6254": [ + "R5230" + ], + "R6268": [ + "R2336", + "R5870" + ], + "R2336": [ + "R6268", + "R1064" + ], + "R1064": [ + "R2336", + "R3615" + ], + "R3615": [ + "R4483", + "R1064" + ], + "R4483": [ + "R7380", + "R3615" + ], + "R7380": [ + "R9490", + "R4483" + ], + "R9490": [ + "R2772", + "R7380" + ], + "R2772": [ + "R2173", + "R9490" + ], + "R2173": [ + "R2772", + "R1293" + ], + "R1293": [ + "R2173" + ], + "R9588": [ + "R7266", + "R5870" + ], + "R7266": [ + "R9588", + "R5952" + ], + "R5952": [ + "R9116", + "R7266" + ], + "R9116": [ + "R3300", + "R5952" + ], + "R3300": [ + "R1423", + "R9116" + ], + "R1423": [ + "R4887", + "R3300" + ], + "R4887": [ + "R2038", + "R1423" + ], + "R2038": [ + "R2660", + "R4887" + ], + "R2660": [ + "R2926", + "R2038" + ], + "R2926": [ + "R2660" + ], + "R5262": [ + "R8922", + "R5794" + ], + "R8922": [ + "R2800", + "R5262" + ], + "R2800": [ + "R8263", + "R8922" + ], + "R8263": [ + "R9679", + "R2800" + ], + "R9679": [ + "R8263", + "R4718" + ], + "R4718": [ + "R9679", + "R9548" + ], + "R9548": [ + "R4718", + "R2873" + ], + "R2873": [ + "R1660", + "R9548" + ], + "R1660": [ + "R2873", + "R3200" + ], + "R3200": [ + "R1660" + ], + "R8383": [ + "R5794", + "R8073" + ], + "R8073": [ + "R8383", + "R7115" + ], + "R7115": [ + "R5216", + "R8073" + ], + "R5216": [ + "R7115", + "R9299" + ], + "R9299": [ + "R5216", + "R1041" + ], + "R1041": [ + "R9299", + "R8092" + ], + "R8092": [ + "R1041", + "R4960" + ], + "R4960": [ + "R7241", + "R8092" + ], + "R7241": [ + "R4960", + "R8732" + ], + "R8732": [ + "R7241" + ], + "R6381": [ + "R6177", + "R5794" + ], + "R6177": [ + "R6654", + "R6381" + ], + "R6654": [ + "R6177", + "R4370" + ], + "R4370": [ + "R6654", + "R2732" + ], + "R2732": [ + "R5140", + "R4370" + ], + "R5140": [ + "R2732", + "R8929" + ], + "R8929": [ + "R5140", + "R7859" + ], + "R7859": [ + "R6845", + "R8929" + ], + "R6845": [ + "R7859", + "R2049" + ], + "R2049": [ + "R6845" + ], + "R9551": [ + "R6192", + "R5255" + ], + "R5255": [ + "R9551", + "R8865" + ], + "R8865": [ + "R4941", + "R5255" + ], + "R4941": [ + "R2097", + "R8865" + ], + "R2097": [ + "R2074", + "R4941" + ], + "R2074": [ + "R2097", + "R1484" + ], + "R1484": [ + "R2074", + "R9157" + ], + "R9157": [ + "R1484", + "R6940" + ], + "R6940": [ + "R5732", + "R9157" + ], + "R5732": [ + "R6940" + ], + "R4361": [ + "R6192", + "R2372" + ], + "R2372": [ + "R4361", + "R3396" + ], + "R3396": [ + "R2372", + "R8002" + ], + "R8002": [ + "R3396", + "R8980" + ], + "R8980": [ + "R8002", + "R9830" + ], + "R9830": [ + "R8980", + "R9569" + ], + "R9569": [ + "R9830", + "R3453" + ], + "R3453": [ + "R9569", + "R5699" + ], + "R5699": [ + "R3453", + "R1838" + ], + "R1838": [ + "R5699" + ], + "R4126": [ + "R6192", + "R5337" + ], + "R5337": [ + "R5430", + "R4126" + ], + "R5430": [ + "R5337", + "R1482" + ], + "R1482": [ + "R8316", + "R5430" + ], + "R8316": [ + "R4861", + "R1482" + ], + "R4861": [ + "R6777", + "R8316" + ], + "R6777": [ + "R3266", + "R4861" + ], + "R3266": [ + "R6777", + "R6225" + ], + "R6225": [ + "R3191", + "R3266" + ], + "R3191": [ + "R6225" + ], + "R9457": [ + "R1635", + "R5722" + ], + "R1635": [ + "R4916", + "R9457" + ], + "R4916": [ + "R1635", + "R9652" + ], + "R9652": [ + "R4916", + "R1709" + ], + "R1709": [ + "R4873", + "R9652" + ], + "R4873": [ + "R3484", + "R1709" + ], + "R3484": [ + "R4873", + "R1368" + ], + "R1368": [ + "R8711", + "R3484" + ], + "R8711": [ + "R3282", + "R1368" + ], + "R3282": [ + "R8711" + ], + "R2451": [ + "R5722", + "R3791" + ], + "R3791": [ + "R2451", + "R6262" + ], + "R6262": [ + "R3791", + "R6792" + ], + "R6792": [ + "R6262", + "R2589" + ], + "R2589": [ + "R8840", + "R6792" + ], + "R8840": [ + "R1615", + "R2589" + ], + "R1615": [ + "R9256", + "R8840" + ], + "R9256": [ + "R4892", + "R1615" + ], + "R4892": [ + "R7787", + "R9256" + ], + "R7787": [ + "R4892" + ], + "R8684": [ + "R5722", + "R8939" + ], + "R8939": [ + "R8684", + "R2339" + ], + "R2339": [ + "R5214", + "R8939" + ], + "R5214": [ + "R2334", + "R2339" + ], + "R2334": [ + "R5214", + "R3942" + ], + "R3942": [ + "R2793", + "R2334" + ], + "R2793": [ + "R7074", + "R3942" + ], + "R7074": [ + "R1833", + "R2793" + ], + "R1833": [ + "R3512", + "R7074" + ], + "R3512": [ + "R1833" + ], + "R7656": [ + "R7757", + "R9292" + ], + "R9292": [ + "R7656", + "R1725" + ], + "R1725": [ + "R9292", + "R7904" + ], + "R7904": [ + "R1725", + "R8907" + ], + "R8907": [ + "R7904", + "R2322" + ], + "R2322": [ + "R8907", + "R3967" + ], + "R3967": [ + "R2322", + "R5933" + ], + "R5933": [ + "R3967", + "R2265" + ], + "R2265": [ + "R5933", + "R9188" + ], + "R9188": [ + "R2265" + ], + "R4246": [ + "R8714", + "R7757" + ], + "R8714": [ + "R5281", + "R4246" + ], + "R5281": [ + "R8714", + "R8253" + ], + "R8253": [ + "R4719", + "R5281" + ], + "R4719": [ + "R8253", + "R6332" + ], + "R6332": [ + "R4128", + "R4719" + ], + "R4128": [ + "R9172", + "R6332" + ], + "R9172": [ + "R6326", + "R4128" + ], + "R6326": [ + "R9172", + "R5868" + ], + "R5868": [ + "R6326" + ], + "R9879": [ + "R7150", + "R7757" + ], + "R7150": [ + "R1131", + "R9879" + ], + "R1131": [ + "R5942", + "R7150" + ], + "R5942": [ + "R3866", + "R1131" + ], + "R3866": [ + "R5942", + "R6716" + ], + "R6716": [ + "R9203", + "R3866" + ], + "R9203": [ + "R2832", + "R6716" + ], + "R2832": [ + "R6128", + "R9203" + ], + "R6128": [ + "R2832", + "R6864" + ], + "R6864": [ + "R6128" + ], + "R9511": [ + "R4224", + "R9243" + ], + "R4224": [ + "R9511", + "R6599" + ], + "R6599": [ + "R4059", + "R4224" + ], + "R4059": [ + "R9312", + "R6599" + ], + "R9312": [ + "R9235", + "R4059" + ], + "R9235": [ + "R9666", + "R9312" + ], + "R9666": [ + "R2846", + "R9235" + ], + "R2846": [ + "R9666", + "R5671" + ], + "R5671": [ + "R2846", + "R5445" + ], + "R5445": [ + "R5671" + ], + "R3432": [ + "R9243", + "R3612" + ], + "R3612": [ + "R9947", + "R3432" + ], + "R9947": [ + "R4598", + "R3612" + ], + "R4598": [ + "R6483", + "R9947" + ], + "R6483": [ + "R4598", + "R9815" + ], + "R9815": [ + "R6483", + "R7269" + ], + "R7269": [ + "R9815", + "R2951" + ], + "R2951": [ + "R1655", + "R7269" + ], + "R1655": [ + "R3330", + "R2951" + ], + "R3330": [ + "R1655" + ], + "R3299": [ + "R1747", + "R9243" + ], + "R1747": [ + "R2157", + "R3299" + ], + "R2157": [ + "R5215", + "R1747" + ], + "R5215": [ + "R2157", + "R1230" + ], + "R1230": [ + "R5215", + "R2719" + ], + "R2719": [ + "R7041", + "R1230" + ], + "R7041": [ + "R2719", + "R7404" + ], + "R7404": [ + "R7854", + "R7041" + ], + "R7854": [ + "R7404", + "R8076" + ], + "R8076": [ + "R7854" + ] + } +} diff --git a/tasks/maze-t17/instruction.md b/tasks/maze-t17/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t17/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t17/solution/solve.sh b/tasks/maze-t17/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..39db5f48ca0e26f25f10e789bea01aa5a6aa8369 --- /dev/null +++ b/tasks/maze-t17/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R3198 R5745 R5152 R8397 R9233 R4954 R3284 R3987 R2536 R5966 R7336 R4737 R1585 R6797 R8023 R6115 R1739 R7698 R5202 R9243 R3299 R1747 R2157 R5215 R1230 R2719 R7041 R7404 R7854 R8076; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t17/task.toml b/tasks/maze-t17/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..cf740465ce2c70c9d57c67c6e205d09c533ca8ef --- /dev/null +++ b/tasks/maze-t17/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t17" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 17 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t17/tests/Dockerfile b/tasks/maze-t17/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t17/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t17/tests/expected.json b/tasks/maze-t17/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..306e96b9eb82cdd30757df567fa0262234902505 --- /dev/null +++ b/tasks/maze-t17/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R2552", + "exit": "R8076", + "budget": 210, + "tree_sha256": "37cae8d173d6734baf80b9bd1593cd359ee6fa7b8588a8bb3bfb961a489c49bc" +} diff --git a/tasks/maze-t17/tests/grade.py b/tasks/maze-t17/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t17/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t17/tests/test.sh b/tasks/maze-t17/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t17/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t18/environment/agent/Dockerfile b/tasks/maze-t18/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t18/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t18/environment/agent/maze b/tasks/maze-t18/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t18/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t18/environment/docker-compose.yaml b/tasks/maze-t18/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..32e745f0b12310672478fb66a896b857aa3579c9 --- /dev/null +++ b/tasks/maze-t18/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t18: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t18-6d66998d34f6 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t18/environment/maze/Dockerfile b/tasks/maze-t18/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t18/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t18/environment/maze/engine.py b/tasks/maze-t18/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t18/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t18/environment/maze/export.py b/tasks/maze-t18/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t18/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t18/environment/maze/service.py b/tasks/maze-t18/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t18/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t18/environment/maze/success.py b/tasks/maze-t18/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t18/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t18/environment/maze/tree.json b/tasks/maze-t18/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..10a2caf0f0abffee00c7bfcd61f9532fcef3d9c4 --- /dev/null +++ b/tasks/maze-t18/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R3454", + "exit": "R4907", + "budget": 210, + "doors": { + "R3454": [ + "R2409", + "R2521", + "R7621" + ], + "R7621": [ + "R9833", + "R3454" + ], + "R9833": [ + "R4560", + "R7621" + ], + "R4560": [ + "R9833", + "R6886" + ], + "R6886": [ + "R4560", + "R8842" + ], + "R8842": [ + "R7057", + "R6886" + ], + "R7057": [ + "R2831", + "R8842" + ], + "R2831": [ + "R7057", + "R1198" + ], + "R1198": [ + "R2484", + "R2831" + ], + "R2484": [ + "R1198", + "R4066" + ], + "R4066": [ + "R9072", + "R8334", + "R3208", + "R2484" + ], + "R2409": [ + "R4267", + "R3454" + ], + "R4267": [ + "R5236", + "R2409" + ], + "R5236": [ + "R3332", + "R4267" + ], + "R3332": [ + "R4464", + "R5236" + ], + "R4464": [ + "R1461", + "R3332" + ], + "R1461": [ + "R9881", + "R4464" + ], + "R9881": [ + "R1461", + "R4510" + ], + "R4510": [ + "R9881", + "R9652" + ], + "R9652": [ + "R9338", + "R4510" + ], + "R9338": [ + "R9652", + "R8183", + "R4693", + "R6707" + ], + "R2521": [ + "R3454", + "R5453" + ], + "R5453": [ + "R2521", + "R5551" + ], + "R5551": [ + "R5453", + "R6663" + ], + "R6663": [ + "R3702", + "R5551" + ], + "R3702": [ + "R4860", + "R6663" + ], + "R4860": [ + "R3702", + "R6169" + ], + "R6169": [ + "R3407", + "R4860" + ], + "R3407": [ + "R6485", + "R6169" + ], + "R6485": [ + "R3407", + "R9854" + ], + "R9854": [ + "R6582", + "R8968", + "R6122", + "R6485" + ], + "R3208": [ + "R4066", + "R1972" + ], + "R1972": [ + "R3208", + "R5091" + ], + "R5091": [ + "R5083", + "R1972" + ], + "R5083": [ + "R3779", + "R5091" + ], + "R3779": [ + "R7184", + "R5083" + ], + "R7184": [ + "R1095", + "R3779" + ], + "R1095": [ + "R7210", + "R7184" + ], + "R7210": [ + "R7624", + "R1095" + ], + "R7624": [ + "R3090", + "R7210" + ], + "R3090": [ + "R6182", + "R6761", + "R7624", + "R8755" + ], + "R8334": [ + "R4066", + "R7303" + ], + "R7303": [ + "R7197", + "R8334" + ], + "R7197": [ + "R1808", + "R7303" + ], + "R1808": [ + "R7197", + "R2329" + ], + "R2329": [ + "R1808", + "R7208" + ], + "R7208": [ + "R2325", + "R2329" + ], + "R2325": [ + "R4149", + "R7208" + ], + "R4149": [ + "R2325", + "R8598" + ], + "R8598": [ + "R2174", + "R4149" + ], + "R2174": [ + "R8598", + "R5732", + "R5560", + "R9357" + ], + "R9072": [ + "R2784", + "R4066" + ], + "R2784": [ + "R6219", + "R9072" + ], + "R6219": [ + "R2784", + "R9188" + ], + "R9188": [ + "R6219", + "R9900" + ], + "R9900": [ + "R3817", + "R9188" + ], + "R3817": [ + "R9900", + "R8587" + ], + "R8587": [ + "R2447", + "R3817" + ], + "R2447": [ + "R5549", + "R8587" + ], + "R5549": [ + "R1619", + "R2447" + ], + "R1619": [ + "R1438", + "R4909", + "R5549", + "R4011" + ], + "R6707": [ + "R9004", + "R9338" + ], + "R9004": [ + "R9166", + "R6707" + ], + "R9166": [ + "R9004", + "R5958" + ], + "R5958": [ + "R9166", + "R8423" + ], + "R8423": [ + "R6095", + "R5958" + ], + "R6095": [ + "R4025", + "R8423" + ], + "R4025": [ + "R6095", + "R1818" + ], + "R1818": [ + "R5283", + "R4025" + ], + "R5283": [ + "R4225", + "R1818" + ], + "R4225": [ + "R1532", + "R8305", + "R7333", + "R5283" + ], + "R8183": [ + "R9338", + "R8414" + ], + "R8414": [ + "R8183", + "R8060" + ], + "R8060": [ + "R8414", + "R8402" + ], + "R8402": [ + "R8060", + "R5547" + ], + "R5547": [ + "R6331", + "R8402" + ], + "R6331": [ + "R4293", + "R5547" + ], + "R4293": [ + "R7047", + "R6331" + ], + "R7047": [ + "R8340", + "R4293" + ], + "R8340": [ + "R6914", + "R7047" + ], + "R6914": [ + "R8340", + "R1116", + "R7777", + "R5638" + ], + "R4693": [ + "R9338", + "R4902" + ], + "R4902": [ + "R4693", + "R1399" + ], + "R1399": [ + "R6567", + "R4902" + ], + "R6567": [ + "R3695", + "R1399" + ], + "R3695": [ + "R9168", + "R6567" + ], + "R9168": [ + "R6316", + "R3695" + ], + "R6316": [ + "R9168", + "R4079" + ], + "R4079": [ + "R8366", + "R6316" + ], + "R8366": [ + "R8028", + "R4079" + ], + "R8028": [ + "R3340", + "R7414", + "R8366", + "R7026" + ], + "R6582": [ + "R2533", + "R9854" + ], + "R2533": [ + "R6582", + "R5232" + ], + "R5232": [ + "R6906", + "R2533" + ], + "R6906": [ + "R5232", + "R9398" + ], + "R9398": [ + "R6424", + "R6906" + ], + "R6424": [ + "R4775", + "R9398" + ], + "R4775": [ + "R3370", + "R6424" + ], + "R3370": [ + "R4775", + "R5508" + ], + "R5508": [ + "R9386", + "R3370" + ], + "R9386": [ + "R3895", + "R5508", + "R1610", + "R5460" + ], + "R8968": [ + "R9854", + "R8774" + ], + "R8774": [ + "R9314", + "R8968" + ], + "R9314": [ + "R9784", + "R8774" + ], + "R9784": [ + "R6007", + "R9314" + ], + "R6007": [ + "R4357", + "R9784" + ], + "R4357": [ + "R1832", + "R6007" + ], + "R1832": [ + "R7903", + "R4357" + ], + "R7903": [ + "R7152", + "R1832" + ], + "R7152": [ + "R7903", + "R2208" + ], + "R2208": [ + "R1058", + "R1052", + "R7152", + "R7649" + ], + "R6122": [ + "R9854", + "R7315" + ], + "R7315": [ + "R4498", + "R6122" + ], + "R4498": [ + "R7315", + "R3299" + ], + "R3299": [ + "R4498", + "R2011" + ], + "R2011": [ + "R3299", + "R9321" + ], + "R9321": [ + "R2011", + "R6677" + ], + "R6677": [ + "R9321", + "R9406" + ], + "R9406": [ + "R6677", + "R5309" + ], + "R5309": [ + "R9406", + "R3824" + ], + "R3824": [ + "R4053", + "R7376", + "R5309", + "R5478" + ], + "R6761": [ + "R6641", + "R3090" + ], + "R6641": [ + "R6761", + "R3117" + ], + "R3117": [ + "R6641", + "R3365" + ], + "R3365": [ + "R3117", + "R1130" + ], + "R1130": [ + "R3365", + "R6706" + ], + "R6706": [ + "R1130", + "R6814" + ], + "R6814": [ + "R5238", + "R6706" + ], + "R5238": [ + "R6814", + "R1546" + ], + "R1546": [ + "R4522", + "R5238" + ], + "R4522": [ + "R1546" + ], + "R6182": [ + "R6640", + "R3090" + ], + "R6640": [ + "R6182", + "R8296" + ], + "R8296": [ + "R6221", + "R6640" + ], + "R6221": [ + "R8296", + "R9272" + ], + "R9272": [ + "R6221", + "R9164" + ], + "R9164": [ + "R9272", + "R4195" + ], + "R4195": [ + "R9164", + "R1074" + ], + "R1074": [ + "R4195", + "R9732" + ], + "R9732": [ + "R1074", + "R9684" + ], + "R9684": [ + "R9732" + ], + "R8755": [ + "R3090", + "R4876" + ], + "R4876": [ + "R4468", + "R8755" + ], + "R4468": [ + "R4876", + "R7665" + ], + "R7665": [ + "R4217", + "R4468" + ], + "R4217": [ + "R7665", + "R3760" + ], + "R3760": [ + "R4217", + "R6846" + ], + "R6846": [ + "R2693", + "R3760" + ], + "R2693": [ + "R6846", + "R5683" + ], + "R5683": [ + "R2693", + "R3756" + ], + "R3756": [ + "R5683" + ], + "R5732": [ + "R5490", + "R2174" + ], + "R5490": [ + "R5732", + "R6750" + ], + "R6750": [ + "R8322", + "R5490" + ], + "R8322": [ + "R3413", + "R6750" + ], + "R3413": [ + "R7679", + "R8322" + ], + "R7679": [ + "R6107", + "R3413" + ], + "R6107": [ + "R7679", + "R3800" + ], + "R3800": [ + "R6652", + "R6107" + ], + "R6652": [ + "R8623", + "R3800" + ], + "R8623": [ + "R6652" + ], + "R9357": [ + "R2174", + "R6854" + ], + "R6854": [ + "R7517", + "R9357" + ], + "R7517": [ + "R5604", + "R6854" + ], + "R5604": [ + "R7567", + "R7517" + ], + "R7567": [ + "R8498", + "R5604" + ], + "R8498": [ + "R7567", + "R9739" + ], + "R9739": [ + "R8498", + "R3428" + ], + "R3428": [ + "R9739", + "R2382" + ], + "R2382": [ + "R6502", + "R3428" + ], + "R6502": [ + "R2382" + ], + "R5560": [ + "R4552", + "R2174" + ], + "R4552": [ + "R5560", + "R2558" + ], + "R2558": [ + "R9442", + "R4552" + ], + "R9442": [ + "R1265", + "R2558" + ], + "R1265": [ + "R9442", + "R6381" + ], + "R6381": [ + "R3288", + "R1265" + ], + "R3288": [ + "R6381", + "R2629" + ], + "R2629": [ + "R3288", + "R8900" + ], + "R8900": [ + "R2629", + "R4706" + ], + "R4706": [ + "R8900" + ], + "R1438": [ + "R2913", + "R1619" + ], + "R2913": [ + "R3385", + "R1438" + ], + "R3385": [ + "R2913", + "R9111" + ], + "R9111": [ + "R6179", + "R3385" + ], + "R6179": [ + "R5427", + "R9111" + ], + "R5427": [ + "R8457", + "R6179" + ], + "R8457": [ + "R5427", + "R6141" + ], + "R6141": [ + "R8457", + "R6057" + ], + "R6057": [ + "R6141", + "R9696" + ], + "R9696": [ + "R6057" + ], + "R4011": [ + "R4093", + "R1619" + ], + "R4093": [ + "R3831", + "R4011" + ], + "R3831": [ + "R5627", + "R4093" + ], + "R5627": [ + "R3831", + "R3042" + ], + "R3042": [ + "R2541", + "R5627" + ], + "R2541": [ + "R3042", + "R1946" + ], + "R1946": [ + "R5269", + "R2541" + ], + "R5269": [ + "R1946", + "R9578" + ], + "R9578": [ + "R8333", + "R5269" + ], + "R8333": [ + "R9578" + ], + "R4909": [ + "R9770", + "R1619" + ], + "R9770": [ + "R4909", + "R5584" + ], + "R5584": [ + "R9770", + "R5066" + ], + "R5066": [ + "R5584", + "R9399" + ], + "R9399": [ + "R2936", + "R5066" + ], + "R2936": [ + "R9284", + "R9399" + ], + "R9284": [ + "R2936", + "R8097" + ], + "R8097": [ + "R9284", + "R5110" + ], + "R5110": [ + "R1722", + "R8097" + ], + "R1722": [ + "R5110" + ], + "R7333": [ + "R4277", + "R4225" + ], + "R4277": [ + "R1323", + "R7333" + ], + "R1323": [ + "R4277", + "R6174" + ], + "R6174": [ + "R1323", + "R9407" + ], + "R9407": [ + "R9812", + "R6174" + ], + "R9812": [ + "R9407", + "R2450" + ], + "R2450": [ + "R9700", + "R9812" + ], + "R9700": [ + "R4013", + "R2450" + ], + "R4013": [ + "R9700", + "R7141" + ], + "R7141": [ + "R4013" + ], + "R8305": [ + "R6975", + "R4225" + ], + "R6975": [ + "R8305", + "R5392" + ], + "R5392": [ + "R6975", + "R4597" + ], + "R4597": [ + "R9328", + "R5392" + ], + "R9328": [ + "R9455", + "R4597" + ], + "R9455": [ + "R9328", + "R1635" + ], + "R1635": [ + "R9455", + "R7032" + ], + "R7032": [ + "R1635", + "R8216" + ], + "R8216": [ + "R7032", + "R5692" + ], + "R5692": [ + "R8216" + ], + "R1532": [ + "R1923", + "R4225" + ], + "R1923": [ + "R7213", + "R1532" + ], + "R7213": [ + "R1923", + "R7849" + ], + "R7849": [ + "R2351", + "R7213" + ], + "R2351": [ + "R3075", + "R7849" + ], + "R3075": [ + "R5050", + "R2351" + ], + "R5050": [ + "R3075", + "R5447" + ], + "R5447": [ + "R3461", + "R5050" + ], + "R3461": [ + "R1189", + "R5447" + ], + "R1189": [ + "R3461" + ], + "R5638": [ + "R6914", + "R4187" + ], + "R4187": [ + "R5706", + "R5638" + ], + "R5706": [ + "R6028", + "R4187" + ], + "R6028": [ + "R5706", + "R9423" + ], + "R9423": [ + "R6028", + "R2427" + ], + "R2427": [ + "R9423", + "R5791" + ], + "R5791": [ + "R4805", + "R2427" + ], + "R4805": [ + "R5791", + "R3491" + ], + "R3491": [ + "R6226", + "R4805" + ], + "R6226": [ + "R3491" + ], + "R7777": [ + "R6423", + "R6914" + ], + "R6423": [ + "R7777", + "R2816" + ], + "R2816": [ + "R8493", + "R6423" + ], + "R8493": [ + "R3362", + "R2816" + ], + "R3362": [ + "R8493", + "R8955" + ], + "R8955": [ + "R3362", + "R8961" + ], + "R8961": [ + "R1920", + "R8955" + ], + "R1920": [ + "R3619", + "R8961" + ], + "R3619": [ + "R3146", + "R1920" + ], + "R3146": [ + "R3619" + ], + "R1116": [ + "R6914", + "R3163" + ], + "R3163": [ + "R1116", + "R9462" + ], + "R9462": [ + "R3163", + "R5147" + ], + "R5147": [ + "R9462", + "R4355" + ], + "R4355": [ + "R6183", + "R5147" + ], + "R6183": [ + "R4129", + "R4355" + ], + "R4129": [ + "R6183", + "R6803" + ], + "R6803": [ + "R5421", + "R4129" + ], + "R5421": [ + "R8638", + "R6803" + ], + "R8638": [ + "R5421" + ], + "R3340": [ + "R1785", + "R8028" + ], + "R1785": [ + "R4180", + "R3340" + ], + "R4180": [ + "R8745", + "R1785" + ], + "R8745": [ + "R4180", + "R1717" + ], + "R1717": [ + "R8745", + "R5916" + ], + "R5916": [ + "R6460", + "R1717" + ], + "R6460": [ + "R5916", + "R2892" + ], + "R2892": [ + "R6460", + "R2305" + ], + "R2305": [ + "R2892", + "R9915" + ], + "R9915": [ + "R2305" + ], + "R7414": [ + "R2556", + "R8028" + ], + "R2556": [ + "R7414", + "R5061" + ], + "R5061": [ + "R6318", + "R2556" + ], + "R6318": [ + "R3150", + "R5061" + ], + "R3150": [ + "R3026", + "R6318" + ], + "R3026": [ + "R3167", + "R3150" + ], + "R3167": [ + "R1481", + "R3026" + ], + "R1481": [ + "R6762", + "R3167" + ], + "R6762": [ + "R5059", + "R1481" + ], + "R5059": [ + "R6762" + ], + "R7026": [ + "R8028", + "R9051" + ], + "R9051": [ + "R7026", + "R7533" + ], + "R7533": [ + "R7909", + "R9051" + ], + "R7909": [ + "R7533", + "R3048" + ], + "R3048": [ + "R5464", + "R7909" + ], + "R5464": [ + "R6164", + "R3048" + ], + "R6164": [ + "R3987", + "R5464" + ], + "R3987": [ + "R6097", + "R6164" + ], + "R6097": [ + "R3458", + "R3987" + ], + "R3458": [ + "R6097" + ], + "R5460": [ + "R7784", + "R9386" + ], + "R7784": [ + "R5460", + "R8895" + ], + "R8895": [ + "R7784", + "R9468" + ], + "R9468": [ + "R4536", + "R8895" + ], + "R4536": [ + "R9468", + "R3517" + ], + "R3517": [ + "R8004", + "R4536" + ], + "R8004": [ + "R8459", + "R3517" + ], + "R8459": [ + "R5725", + "R8004" + ], + "R5725": [ + "R8459", + "R5892" + ], + "R5892": [ + "R5725" + ], + "R1610": [ + "R9386", + "R7495" + ], + "R7495": [ + "R1610", + "R5107" + ], + "R5107": [ + "R7671", + "R7495" + ], + "R7671": [ + "R6808", + "R5107" + ], + "R6808": [ + "R7671", + "R5700" + ], + "R5700": [ + "R4734", + "R6808" + ], + "R4734": [ + "R5700", + "R5191" + ], + "R5191": [ + "R4734", + "R2531" + ], + "R2531": [ + "R7237", + "R5191" + ], + "R7237": [ + "R2531" + ], + "R3895": [ + "R9386", + "R7264" + ], + "R7264": [ + "R6434", + "R3895" + ], + "R6434": [ + "R2998", + "R7264" + ], + "R2998": [ + "R6434", + "R8085" + ], + "R8085": [ + "R2998", + "R9334" + ], + "R9334": [ + "R4657", + "R8085" + ], + "R4657": [ + "R1467", + "R9334" + ], + "R1467": [ + "R4657", + "R4173" + ], + "R4173": [ + "R3737", + "R1467" + ], + "R3737": [ + "R4173" + ], + "R7649": [ + "R2208", + "R8507" + ], + "R8507": [ + "R7191", + "R7649" + ], + "R7191": [ + "R8507", + "R3889" + ], + "R3889": [ + "R6045", + "R7191" + ], + "R6045": [ + "R3889", + "R1895" + ], + "R1895": [ + "R6045", + "R6590" + ], + "R6590": [ + "R1411", + "R1895" + ], + "R1411": [ + "R9623", + "R6590" + ], + "R9623": [ + "R1411", + "R4870" + ], + "R4870": [ + "R9623" + ], + "R1058": [ + "R2208", + "R3386" + ], + "R3386": [ + "R3803", + "R1058" + ], + "R3803": [ + "R3386", + "R1390" + ], + "R1390": [ + "R7437", + "R3803" + ], + "R7437": [ + "R1390", + "R2635" + ], + "R2635": [ + "R7070", + "R7437" + ], + "R7070": [ + "R9744", + "R2635" + ], + "R9744": [ + "R7070", + "R8234" + ], + "R8234": [ + "R9744", + "R9966" + ], + "R9966": [ + "R8234" + ], + "R1052": [ + "R2208", + "R8465" + ], + "R8465": [ + "R1052", + "R9205" + ], + "R9205": [ + "R3816", + "R8465" + ], + "R3816": [ + "R1159", + "R9205" + ], + "R1159": [ + "R6522", + "R3816" + ], + "R6522": [ + "R1159", + "R5539" + ], + "R5539": [ + "R7890", + "R6522" + ], + "R7890": [ + "R1333", + "R5539" + ], + "R1333": [ + "R7890", + "R4907" + ], + "R4907": [ + "R1333" + ], + "R7376": [ + "R3824", + "R8500" + ], + "R8500": [ + "R7376", + "R1422" + ], + "R1422": [ + "R3833", + "R8500" + ], + "R3833": [ + "R4739", + "R1422" + ], + "R4739": [ + "R3833", + "R2848" + ], + "R2848": [ + "R4739", + "R1437" + ], + "R1437": [ + "R8844", + "R2848" + ], + "R8844": [ + "R1437", + "R8977" + ], + "R8977": [ + "R8844", + "R2842" + ], + "R2842": [ + "R8977" + ], + "R5478": [ + "R5930", + "R3824" + ], + "R5930": [ + "R5478", + "R5571" + ], + "R5571": [ + "R5930", + "R7325" + ], + "R7325": [ + "R7733", + "R5571" + ], + "R7733": [ + "R8385", + "R7325" + ], + "R8385": [ + "R7733", + "R8869" + ], + "R8869": [ + "R8631", + "R8385" + ], + "R8631": [ + "R9803", + "R8869" + ], + "R9803": [ + "R1303", + "R8631" + ], + "R1303": [ + "R9803" + ], + "R4053": [ + "R3824", + "R6010" + ], + "R6010": [ + "R7680", + "R4053" + ], + "R7680": [ + "R6010", + "R8603" + ], + "R8603": [ + "R7680", + "R3516" + ], + "R3516": [ + "R9291", + "R8603" + ], + "R9291": [ + "R3516", + "R5779" + ], + "R5779": [ + "R6491", + "R9291" + ], + "R6491": [ + "R5779", + "R9410" + ], + "R9410": [ + "R5260", + "R6491" + ], + "R5260": [ + "R9410" + ] + } +} diff --git a/tasks/maze-t18/instruction.md b/tasks/maze-t18/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t18/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t18/solution/solve.sh b/tasks/maze-t18/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..2d2fdb330b904f13a3a088a605ee7c4492bf2dc7 --- /dev/null +++ b/tasks/maze-t18/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R2521 R5453 R5551 R6663 R3702 R4860 R6169 R3407 R6485 R9854 R8968 R8774 R9314 R9784 R6007 R4357 R1832 R7903 R7152 R2208 R1052 R8465 R9205 R3816 R1159 R6522 R5539 R7890 R1333 R4907; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t18/task.toml b/tasks/maze-t18/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..72d14086896ac35a410e1ca65ceb27f12a1cdfdc --- /dev/null +++ b/tasks/maze-t18/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t18" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 18 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t18/tests/Dockerfile b/tasks/maze-t18/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t18/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t18/tests/expected.json b/tasks/maze-t18/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..5ee7c00017ae17f93c4e70fb6cb8f907c2beef27 --- /dev/null +++ b/tasks/maze-t18/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R3454", + "exit": "R4907", + "budget": 210, + "tree_sha256": "2bef5124d3511f568e562ef506846f61a3bafa4cf06af64cefc585d85fe540ea" +} diff --git a/tasks/maze-t18/tests/grade.py b/tasks/maze-t18/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t18/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t18/tests/test.sh b/tasks/maze-t18/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t18/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t19/environment/agent/Dockerfile b/tasks/maze-t19/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t19/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t19/environment/agent/maze b/tasks/maze-t19/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t19/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t19/environment/docker-compose.yaml b/tasks/maze-t19/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0cf0fd949d36506d15c8788b9cf6a443ab320681 --- /dev/null +++ b/tasks/maze-t19/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t19: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t19-57fafc0621f6 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t19/environment/maze/Dockerfile b/tasks/maze-t19/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t19/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t19/environment/maze/engine.py b/tasks/maze-t19/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t19/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t19/environment/maze/export.py b/tasks/maze-t19/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t19/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t19/environment/maze/service.py b/tasks/maze-t19/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t19/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t19/environment/maze/success.py b/tasks/maze-t19/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t19/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t19/environment/maze/tree.json b/tasks/maze-t19/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..3d7c55e1a518fe36848763eaece0812c9a2e0142 --- /dev/null +++ b/tasks/maze-t19/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R6253", + "exit": "R4215", + "budget": 210, + "doors": { + "R6253": [ + "R6076", + "R3799", + "R5669" + ], + "R3799": [ + "R6253", + "R4512" + ], + "R4512": [ + "R3799", + "R3949" + ], + "R3949": [ + "R6014", + "R4512" + ], + "R6014": [ + "R3355", + "R3949" + ], + "R3355": [ + "R6854", + "R6014" + ], + "R6854": [ + "R4702", + "R3355" + ], + "R4702": [ + "R6854", + "R7430" + ], + "R7430": [ + "R4702", + "R2007" + ], + "R2007": [ + "R7430", + "R6783" + ], + "R6783": [ + "R2007", + "R8206", + "R8330", + "R2267" + ], + "R5669": [ + "R2457", + "R6253" + ], + "R2457": [ + "R5669", + "R4326" + ], + "R4326": [ + "R9437", + "R2457" + ], + "R9437": [ + "R4326", + "R8733" + ], + "R8733": [ + "R9437", + "R1549" + ], + "R1549": [ + "R8733", + "R5515" + ], + "R5515": [ + "R3048", + "R1549" + ], + "R3048": [ + "R5515", + "R7974" + ], + "R7974": [ + "R3048", + "R5883" + ], + "R5883": [ + "R7974", + "R8984", + "R5470", + "R6261" + ], + "R6076": [ + "R6646", + "R6253" + ], + "R6646": [ + "R1730", + "R6076" + ], + "R1730": [ + "R6646", + "R4832" + ], + "R4832": [ + "R1730", + "R1931" + ], + "R1931": [ + "R4832", + "R7225" + ], + "R7225": [ + "R1931", + "R6488" + ], + "R6488": [ + "R7225", + "R2414" + ], + "R2414": [ + "R6488", + "R9430" + ], + "R9430": [ + "R8997", + "R2414" + ], + "R8997": [ + "R6442", + "R3158", + "R3875", + "R9430" + ], + "R2267": [ + "R6783", + "R3961" + ], + "R3961": [ + "R2267", + "R3863" + ], + "R3863": [ + "R8401", + "R3961" + ], + "R8401": [ + "R5767", + "R3863" + ], + "R5767": [ + "R6520", + "R8401" + ], + "R6520": [ + "R5767", + "R7115" + ], + "R7115": [ + "R5180", + "R6520" + ], + "R5180": [ + "R5922", + "R7115" + ], + "R5922": [ + "R4679", + "R5180" + ], + "R4679": [ + "R5922", + "R6170", + "R9517", + "R9648" + ], + "R8330": [ + "R6783", + "R2763" + ], + "R2763": [ + "R8330", + "R4628" + ], + "R4628": [ + "R5350", + "R2763" + ], + "R5350": [ + "R8336", + "R4628" + ], + "R8336": [ + "R5350", + "R4466" + ], + "R4466": [ + "R2697", + "R8336" + ], + "R2697": [ + "R4466", + "R2341" + ], + "R2341": [ + "R2697", + "R5174" + ], + "R5174": [ + "R5378", + "R2341" + ], + "R5378": [ + "R5174", + "R1834", + "R1184", + "R2297" + ], + "R8206": [ + "R6192", + "R6783" + ], + "R6192": [ + "R8206", + "R5979" + ], + "R5979": [ + "R3034", + "R6192" + ], + "R3034": [ + "R5979", + "R7118" + ], + "R7118": [ + "R7798", + "R3034" + ], + "R7798": [ + "R1596", + "R7118" + ], + "R1596": [ + "R7798", + "R6670" + ], + "R6670": [ + "R8746", + "R1596" + ], + "R8746": [ + "R9189", + "R6670" + ], + "R9189": [ + "R4954", + "R3874", + "R2014", + "R8746" + ], + "R6261": [ + "R5883", + "R5618" + ], + "R5618": [ + "R6261", + "R7046" + ], + "R7046": [ + "R5618", + "R8079" + ], + "R8079": [ + "R7046", + "R4160" + ], + "R4160": [ + "R8079", + "R1187" + ], + "R1187": [ + "R4304", + "R4160" + ], + "R4304": [ + "R4536", + "R1187" + ], + "R4536": [ + "R6742", + "R4304" + ], + "R6742": [ + "R9922", + "R4536" + ], + "R9922": [ + "R5958", + "R1901", + "R6742", + "R3353" + ], + "R8984": [ + "R9572", + "R5883" + ], + "R9572": [ + "R8984", + "R8569" + ], + "R8569": [ + "R4728", + "R9572" + ], + "R4728": [ + "R8569", + "R6294" + ], + "R6294": [ + "R4728", + "R5119" + ], + "R5119": [ + "R6294", + "R7978" + ], + "R7978": [ + "R4235", + "R5119" + ], + "R4235": [ + "R7978", + "R9017" + ], + "R9017": [ + "R4235", + "R3749" + ], + "R3749": [ + "R6727", + "R9017", + "R6158", + "R2562" + ], + "R5470": [ + "R9852", + "R5883" + ], + "R9852": [ + "R3838", + "R5470" + ], + "R3838": [ + "R9852", + "R8357" + ], + "R8357": [ + "R4625", + "R3838" + ], + "R4625": [ + "R3507", + "R8357" + ], + "R3507": [ + "R8493", + "R4625" + ], + "R8493": [ + "R7431", + "R3507" + ], + "R7431": [ + "R1303", + "R8493" + ], + "R1303": [ + "R7431", + "R1375" + ], + "R1375": [ + "R2523", + "R3987", + "R9108", + "R1303" + ], + "R6442": [ + "R6432", + "R8997" + ], + "R6432": [ + "R5663", + "R6442" + ], + "R5663": [ + "R3572", + "R6432" + ], + "R3572": [ + "R3584", + "R5663" + ], + "R3584": [ + "R2017", + "R3572" + ], + "R2017": [ + "R5269", + "R3584" + ], + "R5269": [ + "R2017", + "R8432" + ], + "R8432": [ + "R3281", + "R5269" + ], + "R3281": [ + "R6886", + "R8432" + ], + "R6886": [ + "R3990", + "R2380", + "R5001", + "R3281" + ], + "R3158": [ + "R8997", + "R8960" + ], + "R8960": [ + "R1139", + "R3158" + ], + "R1139": [ + "R8671", + "R8960" + ], + "R8671": [ + "R6132", + "R1139" + ], + "R6132": [ + "R6951", + "R8671" + ], + "R6951": [ + "R6605", + "R6132" + ], + "R6605": [ + "R5349", + "R6951" + ], + "R5349": [ + "R2527", + "R6605" + ], + "R2527": [ + "R5349", + "R1676" + ], + "R1676": [ + "R5473", + "R2692", + "R4231", + "R2527" + ], + "R3875": [ + "R8997", + "R7329" + ], + "R7329": [ + "R5800", + "R3875" + ], + "R5800": [ + "R3535", + "R7329" + ], + "R3535": [ + "R1893", + "R5800" + ], + "R1893": [ + "R8802", + "R3535" + ], + "R8802": [ + "R1893", + "R9402" + ], + "R9402": [ + "R8802", + "R2164" + ], + "R2164": [ + "R9402", + "R5131" + ], + "R5131": [ + "R2164", + "R6437" + ], + "R6437": [ + "R3721", + "R7693", + "R5131", + "R8659" + ], + "R9517": [ + "R1890", + "R4679" + ], + "R1890": [ + "R9915", + "R9517" + ], + "R9915": [ + "R5011", + "R1890" + ], + "R5011": [ + "R2074", + "R9915" + ], + "R2074": [ + "R5011", + "R7162" + ], + "R7162": [ + "R2074", + "R9670" + ], + "R9670": [ + "R7165", + "R7162" + ], + "R7165": [ + "R9670", + "R6191" + ], + "R6191": [ + "R7165", + "R3593" + ], + "R3593": [ + "R6191" + ], + "R6170": [ + "R4679", + "R3986" + ], + "R3986": [ + "R2475", + "R6170" + ], + "R2475": [ + "R7330", + "R3986" + ], + "R7330": [ + "R2830", + "R2475" + ], + "R2830": [ + "R3131", + "R7330" + ], + "R3131": [ + "R2830", + "R6851" + ], + "R6851": [ + "R3131", + "R5151" + ], + "R5151": [ + "R6851", + "R3885" + ], + "R3885": [ + "R5151", + "R1260" + ], + "R1260": [ + "R3885" + ], + "R9648": [ + "R2012", + "R4679" + ], + "R2012": [ + "R5762", + "R9648" + ], + "R5762": [ + "R2012", + "R5677" + ], + "R5677": [ + "R9908", + "R5762" + ], + "R9908": [ + "R1943", + "R5677" + ], + "R1943": [ + "R9908", + "R9523" + ], + "R9523": [ + "R6025", + "R1943" + ], + "R6025": [ + "R9523", + "R4633" + ], + "R4633": [ + "R5254", + "R6025" + ], + "R5254": [ + "R4633" + ], + "R1834": [ + "R5378", + "R7264" + ], + "R7264": [ + "R5288", + "R1834" + ], + "R5288": [ + "R7264", + "R8320" + ], + "R8320": [ + "R5288", + "R8284" + ], + "R8284": [ + "R9144", + "R8320" + ], + "R9144": [ + "R8284", + "R1548" + ], + "R1548": [ + "R9144", + "R9895" + ], + "R9895": [ + "R1548", + "R2132" + ], + "R2132": [ + "R9895", + "R6753" + ], + "R6753": [ + "R2132" + ], + "R1184": [ + "R6807", + "R5378" + ], + "R6807": [ + "R1184", + "R3255" + ], + "R3255": [ + "R6908", + "R6807" + ], + "R6908": [ + "R3255", + "R4479" + ], + "R4479": [ + "R5057", + "R6908" + ], + "R5057": [ + "R9930", + "R4479" + ], + "R9930": [ + "R8305", + "R5057" + ], + "R8305": [ + "R9930", + "R1186" + ], + "R1186": [ + "R8134", + "R8305" + ], + "R8134": [ + "R1186" + ], + "R2297": [ + "R8931", + "R5378" + ], + "R8931": [ + "R2297", + "R4751" + ], + "R4751": [ + "R7409", + "R8931" + ], + "R7409": [ + "R4751", + "R5825" + ], + "R5825": [ + "R2076", + "R7409" + ], + "R2076": [ + "R9048", + "R5825" + ], + "R9048": [ + "R2076", + "R9412" + ], + "R9412": [ + "R9048", + "R7139" + ], + "R7139": [ + "R9412", + "R1496" + ], + "R1496": [ + "R7139" + ], + "R2014": [ + "R9189", + "R4308" + ], + "R4308": [ + "R2014", + "R5022" + ], + "R5022": [ + "R4308", + "R8217" + ], + "R8217": [ + "R7743", + "R5022" + ], + "R7743": [ + "R8217", + "R8550" + ], + "R8550": [ + "R9488", + "R7743" + ], + "R9488": [ + "R8550", + "R9322" + ], + "R9322": [ + "R2705", + "R9488" + ], + "R2705": [ + "R9322", + "R1337" + ], + "R1337": [ + "R2705" + ], + "R3874": [ + "R9189", + "R5133" + ], + "R5133": [ + "R3347", + "R3874" + ], + "R3347": [ + "R2704", + "R5133" + ], + "R2704": [ + "R3347", + "R8027" + ], + "R8027": [ + "R2704", + "R4982" + ], + "R4982": [ + "R8027", + "R7000" + ], + "R7000": [ + "R3420", + "R4982" + ], + "R3420": [ + "R1535", + "R7000" + ], + "R1535": [ + "R3420", + "R6439" + ], + "R6439": [ + "R1535" + ], + "R4954": [ + "R9189", + "R4845" + ], + "R4845": [ + "R9855", + "R4954" + ], + "R9855": [ + "R9175", + "R4845" + ], + "R9175": [ + "R9855", + "R7580" + ], + "R7580": [ + "R9175", + "R6702" + ], + "R6702": [ + "R3871", + "R7580" + ], + "R3871": [ + "R6702", + "R3335" + ], + "R3335": [ + "R6850", + "R3871" + ], + "R6850": [ + "R4968", + "R3335" + ], + "R4968": [ + "R6850" + ], + "R5958": [ + "R7694", + "R9922" + ], + "R7694": [ + "R5958", + "R3687" + ], + "R3687": [ + "R3366", + "R7694" + ], + "R3366": [ + "R2219", + "R3687" + ], + "R2219": [ + "R3366", + "R1423" + ], + "R1423": [ + "R9823", + "R2219" + ], + "R9823": [ + "R1423", + "R2637" + ], + "R2637": [ + "R6013", + "R9823" + ], + "R6013": [ + "R2637", + "R2737" + ], + "R2737": [ + "R6013" + ], + "R1901": [ + "R9922", + "R4020" + ], + "R4020": [ + "R1703", + "R1901" + ], + "R1703": [ + "R4561", + "R4020" + ], + "R4561": [ + "R4162", + "R1703" + ], + "R4162": [ + "R4561", + "R6876" + ], + "R6876": [ + "R4162", + "R5598" + ], + "R5598": [ + "R6876", + "R2592" + ], + "R2592": [ + "R5598", + "R5209" + ], + "R5209": [ + "R2592", + "R3692" + ], + "R3692": [ + "R5209" + ], + "R3353": [ + "R9586", + "R9922" + ], + "R9586": [ + "R3353", + "R9055" + ], + "R9055": [ + "R9586", + "R6042" + ], + "R6042": [ + "R1622", + "R9055" + ], + "R1622": [ + "R4885", + "R6042" + ], + "R4885": [ + "R1622", + "R9605" + ], + "R9605": [ + "R3398", + "R4885" + ], + "R3398": [ + "R9605", + "R9724" + ], + "R9724": [ + "R3398", + "R4027" + ], + "R4027": [ + "R9724" + ], + "R6727": [ + "R3749", + "R7424" + ], + "R7424": [ + "R5799", + "R6727" + ], + "R5799": [ + "R3399", + "R7424" + ], + "R3399": [ + "R5799", + "R5169" + ], + "R5169": [ + "R3399", + "R7646" + ], + "R7646": [ + "R8644", + "R5169" + ], + "R8644": [ + "R7646", + "R5274" + ], + "R5274": [ + "R5816", + "R8644" + ], + "R5816": [ + "R5274", + "R2040" + ], + "R2040": [ + "R5816" + ], + "R2562": [ + "R1029", + "R3749" + ], + "R1029": [ + "R7641", + "R2562" + ], + "R7641": [ + "R3311", + "R1029" + ], + "R3311": [ + "R1770", + "R7641" + ], + "R1770": [ + "R2753", + "R3311" + ], + "R2753": [ + "R1770", + "R7267" + ], + "R7267": [ + "R2753", + "R2363" + ], + "R2363": [ + "R7267", + "R2443" + ], + "R2443": [ + "R2363", + "R6710" + ], + "R6710": [ + "R2443" + ], + "R6158": [ + "R7520", + "R3749" + ], + "R7520": [ + "R6215", + "R6158" + ], + "R6215": [ + "R1053", + "R7520" + ], + "R1053": [ + "R6215", + "R8035" + ], + "R8035": [ + "R6796", + "R1053" + ], + "R6796": [ + "R6182", + "R8035" + ], + "R6182": [ + "R6796", + "R5556" + ], + "R5556": [ + "R6182", + "R5975" + ], + "R5975": [ + "R3820", + "R5556" + ], + "R3820": [ + "R5975" + ], + "R2523": [ + "R8902", + "R1375" + ], + "R8902": [ + "R7260", + "R2523" + ], + "R7260": [ + "R8902", + "R6843" + ], + "R6843": [ + "R7260", + "R8537" + ], + "R8537": [ + "R6843", + "R1527" + ], + "R1527": [ + "R9974", + "R8537" + ], + "R9974": [ + "R1527", + "R1269" + ], + "R1269": [ + "R7723", + "R9974" + ], + "R7723": [ + "R4223", + "R1269" + ], + "R4223": [ + "R7723" + ], + "R3987": [ + "R5552", + "R1375" + ], + "R5552": [ + "R2103", + "R3987" + ], + "R2103": [ + "R5552", + "R7588" + ], + "R7588": [ + "R2103", + "R2190" + ], + "R2190": [ + "R7588", + "R3282" + ], + "R3282": [ + "R2190", + "R9866" + ], + "R9866": [ + "R3282", + "R6929" + ], + "R6929": [ + "R8828", + "R9866" + ], + "R8828": [ + "R6929", + "R7175" + ], + "R7175": [ + "R8828" + ], + "R9108": [ + "R6582", + "R1375" + ], + "R6582": [ + "R9108", + "R8837" + ], + "R8837": [ + "R3363", + "R6582" + ], + "R3363": [ + "R8837", + "R8410" + ], + "R8410": [ + "R3363", + "R4803" + ], + "R4803": [ + "R1875", + "R8410" + ], + "R1875": [ + "R4283", + "R4803" + ], + "R4283": [ + "R1875", + "R4582" + ], + "R4582": [ + "R8658", + "R4283" + ], + "R8658": [ + "R4582" + ], + "R5001": [ + "R6886", + "R6641" + ], + "R6641": [ + "R7656", + "R5001" + ], + "R7656": [ + "R6641", + "R9784" + ], + "R9784": [ + "R3270", + "R7656" + ], + "R3270": [ + "R9784", + "R6937" + ], + "R6937": [ + "R3270", + "R7279" + ], + "R7279": [ + "R8778", + "R6937" + ], + "R8778": [ + "R7279", + "R8822" + ], + "R8822": [ + "R8778", + "R8007" + ], + "R8007": [ + "R8822" + ], + "R3990": [ + "R6886", + "R9181" + ], + "R9181": [ + "R3990", + "R2962" + ], + "R2962": [ + "R9181", + "R7989" + ], + "R7989": [ + "R2962", + "R8092" + ], + "R8092": [ + "R2533", + "R7989" + ], + "R2533": [ + "R8092", + "R4646" + ], + "R4646": [ + "R3577", + "R2533" + ], + "R3577": [ + "R4646", + "R9484" + ], + "R9484": [ + "R3577", + "R5999" + ], + "R5999": [ + "R9484" + ], + "R2380": [ + "R6886", + "R7858" + ], + "R7858": [ + "R2380", + "R9535" + ], + "R9535": [ + "R7858", + "R8393" + ], + "R8393": [ + "R9535", + "R1257" + ], + "R1257": [ + "R8393", + "R1266" + ], + "R1266": [ + "R1257", + "R5126" + ], + "R5126": [ + "R1266", + "R2571" + ], + "R2571": [ + "R5126", + "R2788" + ], + "R2788": [ + "R2571", + "R1579" + ], + "R1579": [ + "R2788" + ], + "R2692": [ + "R8011", + "R1676" + ], + "R8011": [ + "R2692", + "R6205" + ], + "R6205": [ + "R8011", + "R8054" + ], + "R8054": [ + "R6205", + "R5639" + ], + "R5639": [ + "R3037", + "R8054" + ], + "R3037": [ + "R5218", + "R5639" + ], + "R5218": [ + "R7531", + "R3037" + ], + "R7531": [ + "R3452", + "R5218" + ], + "R3452": [ + "R7531", + "R4520" + ], + "R4520": [ + "R3452" + ], + "R5473": [ + "R3625", + "R1676" + ], + "R3625": [ + "R1954", + "R5473" + ], + "R1954": [ + "R7874", + "R3625" + ], + "R7874": [ + "R6403", + "R1954" + ], + "R6403": [ + "R7874", + "R8627" + ], + "R8627": [ + "R4874", + "R6403" + ], + "R4874": [ + "R9637", + "R8627" + ], + "R9637": [ + "R6260", + "R4874" + ], + "R6260": [ + "R7651", + "R9637" + ], + "R7651": [ + "R6260" + ], + "R4231": [ + "R3440", + "R1676" + ], + "R3440": [ + "R4231", + "R7695" + ], + "R7695": [ + "R3440", + "R6289" + ], + "R6289": [ + "R7695", + "R3982" + ], + "R3982": [ + "R6289", + "R4327" + ], + "R4327": [ + "R3982", + "R7086" + ], + "R7086": [ + "R4327", + "R8302" + ], + "R8302": [ + "R5565", + "R7086" + ], + "R5565": [ + "R4450", + "R8302" + ], + "R4450": [ + "R5565" + ], + "R3721": [ + "R6437", + "R6685" + ], + "R6685": [ + "R3406", + "R3721" + ], + "R3406": [ + "R6685", + "R7530" + ], + "R7530": [ + "R3406", + "R3951" + ], + "R3951": [ + "R1490", + "R7530" + ], + "R1490": [ + "R3951", + "R9080" + ], + "R9080": [ + "R4779", + "R1490" + ], + "R4779": [ + "R9080", + "R9959" + ], + "R9959": [ + "R4779", + "R4215" + ], + "R4215": [ + "R9959" + ], + "R8659": [ + "R6437", + "R8864" + ], + "R8864": [ + "R7355", + "R8659" + ], + "R7355": [ + "R5796", + "R8864" + ], + "R5796": [ + "R7355", + "R8889" + ], + "R8889": [ + "R9631", + "R5796" + ], + "R9631": [ + "R5394", + "R8889" + ], + "R5394": [ + "R9631", + "R5904" + ], + "R5904": [ + "R5394", + "R7901" + ], + "R7901": [ + "R6954", + "R5904" + ], + "R6954": [ + "R7901" + ], + "R7693": [ + "R2400", + "R6437" + ], + "R2400": [ + "R7693", + "R5251" + ], + "R5251": [ + "R2400", + "R7304" + ], + "R7304": [ + "R5251", + "R5362" + ], + "R5362": [ + "R7304", + "R9503" + ], + "R9503": [ + "R5362", + "R7377" + ], + "R7377": [ + "R9503", + "R5695" + ], + "R5695": [ + "R9043", + "R7377" + ], + "R9043": [ + "R5695", + "R2214" + ], + "R2214": [ + "R9043" + ] + } +} diff --git a/tasks/maze-t19/instruction.md b/tasks/maze-t19/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t19/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t19/solution/solve.sh b/tasks/maze-t19/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..8efaa75f27952ff2d8bb30e226a4e78fb5026351 --- /dev/null +++ b/tasks/maze-t19/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R6076 R6646 R1730 R4832 R1931 R7225 R6488 R2414 R9430 R8997 R3875 R7329 R5800 R3535 R1893 R8802 R9402 R2164 R5131 R6437 R3721 R6685 R3406 R7530 R3951 R1490 R9080 R4779 R9959 R4215; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t19/task.toml b/tasks/maze-t19/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..c316b2596b0d4aefaea08d127d227b2f944366cc --- /dev/null +++ b/tasks/maze-t19/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t19" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 19 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t19/tests/Dockerfile b/tasks/maze-t19/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t19/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t19/tests/expected.json b/tasks/maze-t19/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..da2ece3a604d8d94ceb8c285cc48ecb22458ed2e --- /dev/null +++ b/tasks/maze-t19/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R6253", + "exit": "R4215", + "budget": 210, + "tree_sha256": "b480f8fbc4f12c5acadd85ed5bfe2e743924cdd47f8e82e4f9f50048d811e291" +} diff --git a/tasks/maze-t19/tests/grade.py b/tasks/maze-t19/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t19/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t19/tests/test.sh b/tasks/maze-t19/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t19/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t20/environment/agent/Dockerfile b/tasks/maze-t20/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t20/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t20/environment/agent/maze b/tasks/maze-t20/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t20/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t20/environment/docker-compose.yaml b/tasks/maze-t20/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c6f23ed026f0c37d34b5ddb0fece8ca40c3aa519 --- /dev/null +++ b/tasks/maze-t20/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t20: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t20-a3d1cd82f7f9 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t20/environment/maze/Dockerfile b/tasks/maze-t20/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t20/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t20/environment/maze/engine.py b/tasks/maze-t20/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t20/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t20/environment/maze/export.py b/tasks/maze-t20/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t20/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t20/environment/maze/service.py b/tasks/maze-t20/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t20/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t20/environment/maze/success.py b/tasks/maze-t20/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t20/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t20/environment/maze/tree.json b/tasks/maze-t20/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..e08757ed79f9aa6a357ab084593bf0b87f27cde0 --- /dev/null +++ b/tasks/maze-t20/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R8368", + "exit": "R9404", + "budget": 210, + "doors": { + "R8368": [ + "R4490", + "R5528", + "R5337" + ], + "R4490": [ + "R8368", + "R7198" + ], + "R7198": [ + "R9609", + "R4490" + ], + "R9609": [ + "R9455", + "R7198" + ], + "R9455": [ + "R5747", + "R9609" + ], + "R5747": [ + "R7792", + "R9455" + ], + "R7792": [ + "R5747", + "R9459" + ], + "R9459": [ + "R6680", + "R7792" + ], + "R6680": [ + "R9459", + "R2300" + ], + "R2300": [ + "R6680", + "R5237" + ], + "R5237": [ + "R2300", + "R1760", + "R8343", + "R6565" + ], + "R5337": [ + "R3210", + "R8368" + ], + "R3210": [ + "R5337", + "R4363" + ], + "R4363": [ + "R3210", + "R9882" + ], + "R9882": [ + "R5019", + "R4363" + ], + "R5019": [ + "R9882", + "R7992" + ], + "R7992": [ + "R5019", + "R5022" + ], + "R5022": [ + "R7992", + "R2048" + ], + "R2048": [ + "R4139", + "R5022" + ], + "R4139": [ + "R7891", + "R2048" + ], + "R7891": [ + "R5601", + "R4139", + "R2249", + "R9765" + ], + "R5528": [ + "R8740", + "R8368" + ], + "R8740": [ + "R9070", + "R5528" + ], + "R9070": [ + "R8740", + "R8216" + ], + "R8216": [ + "R9070", + "R3535" + ], + "R3535": [ + "R8216", + "R9036" + ], + "R9036": [ + "R3535", + "R3140" + ], + "R3140": [ + "R9036", + "R6904" + ], + "R6904": [ + "R3140", + "R8103" + ], + "R8103": [ + "R7588", + "R6904" + ], + "R7588": [ + "R3242", + "R8103", + "R2361", + "R3459" + ], + "R1760": [ + "R8994", + "R5237" + ], + "R8994": [ + "R4255", + "R1760" + ], + "R4255": [ + "R8994", + "R3475" + ], + "R3475": [ + "R6674", + "R4255" + ], + "R6674": [ + "R3475", + "R9377" + ], + "R9377": [ + "R1468", + "R6674" + ], + "R1468": [ + "R9377", + "R1700" + ], + "R1700": [ + "R1468", + "R1358" + ], + "R1358": [ + "R7309", + "R1700" + ], + "R7309": [ + "R6248", + "R1358", + "R3885", + "R2185" + ], + "R8343": [ + "R7772", + "R5237" + ], + "R7772": [ + "R2987", + "R8343" + ], + "R2987": [ + "R3423", + "R7772" + ], + "R3423": [ + "R2987", + "R7046" + ], + "R7046": [ + "R3423", + "R4561" + ], + "R4561": [ + "R7046", + "R5766" + ], + "R5766": [ + "R5353", + "R4561" + ], + "R5353": [ + "R5766", + "R6590" + ], + "R6590": [ + "R1916", + "R5353" + ], + "R1916": [ + "R2376", + "R7722", + "R9549", + "R6590" + ], + "R6565": [ + "R5237", + "R1086" + ], + "R1086": [ + "R6565", + "R7220" + ], + "R7220": [ + "R1086", + "R5832" + ], + "R5832": [ + "R7220", + "R1132" + ], + "R1132": [ + "R9722", + "R5832" + ], + "R9722": [ + "R5880", + "R1132" + ], + "R5880": [ + "R9722", + "R1718" + ], + "R1718": [ + "R5880", + "R8726" + ], + "R8726": [ + "R1718", + "R8564" + ], + "R8564": [ + "R8726", + "R1022", + "R2208", + "R3463" + ], + "R2249": [ + "R4923", + "R7891" + ], + "R4923": [ + "R1660", + "R2249" + ], + "R1660": [ + "R4923", + "R6322" + ], + "R6322": [ + "R6125", + "R1660" + ], + "R6125": [ + "R6322", + "R4869" + ], + "R4869": [ + "R6125", + "R1184" + ], + "R1184": [ + "R8053", + "R4869" + ], + "R8053": [ + "R1184", + "R2392" + ], + "R2392": [ + "R8053", + "R4395" + ], + "R4395": [ + "R2392", + "R3845", + "R3453", + "R1435" + ], + "R9765": [ + "R7891", + "R5853" + ], + "R5853": [ + "R9765", + "R3186" + ], + "R3186": [ + "R5853", + "R8524" + ], + "R8524": [ + "R5230", + "R3186" + ], + "R5230": [ + "R8524", + "R7664" + ], + "R7664": [ + "R5230", + "R5845" + ], + "R5845": [ + "R9024", + "R7664" + ], + "R9024": [ + "R3925", + "R5845" + ], + "R3925": [ + "R8148", + "R9024" + ], + "R8148": [ + "R9133", + "R3925", + "R2575", + "R5050" + ], + "R5601": [ + "R3790", + "R7891" + ], + "R3790": [ + "R6556", + "R5601" + ], + "R6556": [ + "R3790", + "R5905" + ], + "R5905": [ + "R6556", + "R3851" + ], + "R3851": [ + "R5905", + "R8248" + ], + "R8248": [ + "R3851", + "R1634" + ], + "R1634": [ + "R6912", + "R8248" + ], + "R6912": [ + "R1634", + "R2661" + ], + "R2661": [ + "R9630", + "R6912" + ], + "R9630": [ + "R6326", + "R1855", + "R2661", + "R4476" + ], + "R3459": [ + "R8628", + "R7588" + ], + "R8628": [ + "R3459", + "R4444" + ], + "R4444": [ + "R8628", + "R8205" + ], + "R8205": [ + "R8770", + "R4444" + ], + "R8770": [ + "R6121", + "R8205" + ], + "R6121": [ + "R5081", + "R8770" + ], + "R5081": [ + "R6121", + "R9676" + ], + "R9676": [ + "R1922", + "R5081" + ], + "R1922": [ + "R7682", + "R9676" + ], + "R7682": [ + "R7299", + "R1922", + "R9315", + "R7522" + ], + "R2361": [ + "R3223", + "R7588" + ], + "R3223": [ + "R2361", + "R4887" + ], + "R4887": [ + "R9810", + "R3223" + ], + "R9810": [ + "R4887", + "R6499" + ], + "R6499": [ + "R9810", + "R9045" + ], + "R9045": [ + "R6499", + "R9759" + ], + "R9759": [ + "R3825", + "R9045" + ], + "R3825": [ + "R4552", + "R9759" + ], + "R4552": [ + "R7622", + "R3825" + ], + "R7622": [ + "R4246", + "R2907", + "R4552", + "R8113" + ], + "R3242": [ + "R3706", + "R7588" + ], + "R3706": [ + "R3643", + "R3242" + ], + "R3643": [ + "R3706", + "R2765" + ], + "R2765": [ + "R9497", + "R3643" + ], + "R9497": [ + "R9831", + "R2765" + ], + "R9831": [ + "R9497", + "R6052" + ], + "R6052": [ + "R9831", + "R4987" + ], + "R4987": [ + "R6052", + "R2373" + ], + "R2373": [ + "R4987", + "R3073" + ], + "R3073": [ + "R2373", + "R4111", + "R9338", + "R4955" + ], + "R3885": [ + "R5066", + "R7309" + ], + "R5066": [ + "R3885", + "R6996" + ], + "R6996": [ + "R5066", + "R9769" + ], + "R9769": [ + "R9543", + "R6996" + ], + "R9543": [ + "R9332", + "R9769" + ], + "R9332": [ + "R5976", + "R9543" + ], + "R5976": [ + "R9332", + "R4804" + ], + "R4804": [ + "R7324", + "R5976" + ], + "R7324": [ + "R4804", + "R4286" + ], + "R4286": [ + "R7324" + ], + "R6248": [ + "R7309", + "R9813" + ], + "R9813": [ + "R7658", + "R6248" + ], + "R7658": [ + "R9813", + "R2925" + ], + "R2925": [ + "R7658", + "R2339" + ], + "R2339": [ + "R2281", + "R2925" + ], + "R2281": [ + "R5433", + "R2339" + ], + "R5433": [ + "R2281", + "R3017" + ], + "R3017": [ + "R5433", + "R4570" + ], + "R4570": [ + "R2782", + "R3017" + ], + "R2782": [ + "R4570" + ], + "R2185": [ + "R7309", + "R8165" + ], + "R8165": [ + "R2185", + "R9582" + ], + "R9582": [ + "R8165", + "R7468" + ], + "R7468": [ + "R9922", + "R9582" + ], + "R9922": [ + "R7468", + "R4932" + ], + "R4932": [ + "R9922", + "R6060" + ], + "R6060": [ + "R4932", + "R4232" + ], + "R4232": [ + "R6060", + "R4995" + ], + "R4995": [ + "R4903", + "R4232" + ], + "R4903": [ + "R4995" + ], + "R9549": [ + "R3125", + "R1916" + ], + "R3125": [ + "R9549", + "R3217" + ], + "R3217": [ + "R3125", + "R6745" + ], + "R6745": [ + "R4430", + "R3217" + ], + "R4430": [ + "R6745", + "R8329" + ], + "R8329": [ + "R4430", + "R5450" + ], + "R5450": [ + "R1280", + "R8329" + ], + "R1280": [ + "R5450", + "R2571" + ], + "R2571": [ + "R1801", + "R1280" + ], + "R1801": [ + "R2571" + ], + "R2376": [ + "R4151", + "R1916" + ], + "R4151": [ + "R2376", + "R2142" + ], + "R2142": [ + "R4151", + "R7519" + ], + "R7519": [ + "R3413", + "R2142" + ], + "R3413": [ + "R2576", + "R7519" + ], + "R2576": [ + "R9402", + "R3413" + ], + "R9402": [ + "R2576", + "R3277" + ], + "R3277": [ + "R1286", + "R9402" + ], + "R1286": [ + "R3192", + "R3277" + ], + "R3192": [ + "R1286" + ], + "R7722": [ + "R1916", + "R5262" + ], + "R5262": [ + "R5852", + "R7722" + ], + "R5852": [ + "R8273", + "R5262" + ], + "R8273": [ + "R2139", + "R5852" + ], + "R2139": [ + "R8273", + "R6686" + ], + "R6686": [ + "R2139", + "R1542" + ], + "R1542": [ + "R6686", + "R5773" + ], + "R5773": [ + "R1542", + "R4867" + ], + "R4867": [ + "R5773", + "R8915" + ], + "R8915": [ + "R4867" + ], + "R2208": [ + "R9927", + "R8564" + ], + "R9927": [ + "R2208", + "R6679" + ], + "R6679": [ + "R9927", + "R2182" + ], + "R2182": [ + "R6864", + "R6679" + ], + "R6864": [ + "R2182", + "R9591" + ], + "R9591": [ + "R6864", + "R8710" + ], + "R8710": [ + "R4061", + "R9591" + ], + "R4061": [ + "R5635", + "R8710" + ], + "R5635": [ + "R8174", + "R4061" + ], + "R8174": [ + "R5635" + ], + "R1022": [ + "R5931", + "R8564" + ], + "R5931": [ + "R3674", + "R1022" + ], + "R3674": [ + "R2826", + "R5931" + ], + "R2826": [ + "R7240", + "R3674" + ], + "R7240": [ + "R2826", + "R1632" + ], + "R1632": [ + "R3900", + "R7240" + ], + "R3900": [ + "R1632", + "R7754" + ], + "R7754": [ + "R3900", + "R4465" + ], + "R4465": [ + "R7213", + "R7754" + ], + "R7213": [ + "R4465" + ], + "R3463": [ + "R4930", + "R8564" + ], + "R4930": [ + "R7558", + "R3463" + ], + "R7558": [ + "R4930", + "R1429" + ], + "R1429": [ + "R8518", + "R7558" + ], + "R8518": [ + "R3627", + "R1429" + ], + "R3627": [ + "R8518", + "R6201" + ], + "R6201": [ + "R6152", + "R3627" + ], + "R6152": [ + "R6201", + "R7167" + ], + "R7167": [ + "R6152", + "R1130" + ], + "R1130": [ + "R7167" + ], + "R3453": [ + "R8449", + "R4395" + ], + "R8449": [ + "R3453", + "R1924" + ], + "R1924": [ + "R8443", + "R8449" + ], + "R8443": [ + "R1931", + "R1924" + ], + "R1931": [ + "R8443", + "R2593" + ], + "R2593": [ + "R1931", + "R5546" + ], + "R5546": [ + "R1249", + "R2593" + ], + "R1249": [ + "R5546", + "R5483" + ], + "R5483": [ + "R8849", + "R1249" + ], + "R8849": [ + "R5483" + ], + "R3845": [ + "R4395", + "R8673" + ], + "R8673": [ + "R8924", + "R3845" + ], + "R8924": [ + "R8673", + "R1983" + ], + "R1983": [ + "R3441", + "R8924" + ], + "R3441": [ + "R1983", + "R7093" + ], + "R7093": [ + "R1037", + "R3441" + ], + "R1037": [ + "R7093", + "R8749" + ], + "R8749": [ + "R1849", + "R1037" + ], + "R1849": [ + "R8774", + "R8749" + ], + "R8774": [ + "R1849" + ], + "R1435": [ + "R2807", + "R4395" + ], + "R2807": [ + "R9893", + "R1435" + ], + "R9893": [ + "R1012", + "R2807" + ], + "R1012": [ + "R9893", + "R3063" + ], + "R3063": [ + "R8447", + "R1012" + ], + "R8447": [ + "R9974", + "R3063" + ], + "R9974": [ + "R8040", + "R8447" + ], + "R8040": [ + "R9974", + "R9333" + ], + "R9333": [ + "R8040", + "R4938" + ], + "R4938": [ + "R9333" + ], + "R2575": [ + "R8148", + "R6035" + ], + "R6035": [ + "R5694", + "R2575" + ], + "R5694": [ + "R9243", + "R6035" + ], + "R9243": [ + "R5694", + "R3250" + ], + "R3250": [ + "R5438", + "R9243" + ], + "R5438": [ + "R3250", + "R1314" + ], + "R1314": [ + "R3714", + "R5438" + ], + "R3714": [ + "R1314", + "R8763" + ], + "R8763": [ + "R3714", + "R2727" + ], + "R2727": [ + "R8763" + ], + "R9133": [ + "R1324", + "R8148" + ], + "R1324": [ + "R9133", + "R9547" + ], + "R9547": [ + "R1324", + "R3702" + ], + "R3702": [ + "R3127", + "R9547" + ], + "R3127": [ + "R9951", + "R3702" + ], + "R9951": [ + "R9654", + "R3127" + ], + "R9654": [ + "R9951", + "R6698" + ], + "R6698": [ + "R5999", + "R9654" + ], + "R5999": [ + "R6698", + "R9800" + ], + "R9800": [ + "R5999" + ], + "R5050": [ + "R5922", + "R8148" + ], + "R5922": [ + "R5050", + "R5617" + ], + "R5617": [ + "R5330", + "R5922" + ], + "R5330": [ + "R7639", + "R5617" + ], + "R7639": [ + "R5330", + "R1481" + ], + "R1481": [ + "R7639", + "R6433" + ], + "R6433": [ + "R1481", + "R6765" + ], + "R6765": [ + "R6433", + "R6225" + ], + "R6225": [ + "R6765", + "R3188" + ], + "R3188": [ + "R6225" + ], + "R1855": [ + "R9630", + "R8158" + ], + "R8158": [ + "R1855", + "R1636" + ], + "R1636": [ + "R8158", + "R5570" + ], + "R5570": [ + "R7148", + "R1636" + ], + "R7148": [ + "R5570", + "R2852" + ], + "R2852": [ + "R7148", + "R1574" + ], + "R1574": [ + "R8330", + "R2852" + ], + "R8330": [ + "R1574", + "R6714" + ], + "R6714": [ + "R8330", + "R1241" + ], + "R1241": [ + "R6714" + ], + "R4476": [ + "R9630", + "R6460" + ], + "R6460": [ + "R4271", + "R4476" + ], + "R4271": [ + "R6460", + "R6635" + ], + "R6635": [ + "R7107", + "R4271" + ], + "R7107": [ + "R6635", + "R1095" + ], + "R1095": [ + "R2480", + "R7107" + ], + "R2480": [ + "R1095", + "R4322" + ], + "R4322": [ + "R2480", + "R8819" + ], + "R8819": [ + "R1973", + "R4322" + ], + "R1973": [ + "R8819" + ], + "R6326": [ + "R8999", + "R9630" + ], + "R8999": [ + "R4303", + "R6326" + ], + "R4303": [ + "R1407", + "R8999" + ], + "R1407": [ + "R7298", + "R4303" + ], + "R7298": [ + "R9770", + "R1407" + ], + "R9770": [ + "R4090", + "R7298" + ], + "R4090": [ + "R8587", + "R9770" + ], + "R8587": [ + "R4090", + "R2000" + ], + "R2000": [ + "R8587", + "R3652" + ], + "R3652": [ + "R2000" + ], + "R7522": [ + "R7682", + "R7217" + ], + "R7217": [ + "R9301", + "R7522" + ], + "R9301": [ + "R7217", + "R5219" + ], + "R5219": [ + "R8887", + "R9301" + ], + "R8887": [ + "R5219", + "R6295" + ], + "R6295": [ + "R5347", + "R8887" + ], + "R5347": [ + "R6295", + "R2004" + ], + "R2004": [ + "R5347", + "R3340" + ], + "R3340": [ + "R9751", + "R2004" + ], + "R9751": [ + "R3340" + ], + "R7299": [ + "R7682", + "R9365" + ], + "R9365": [ + "R7299", + "R5549" + ], + "R5549": [ + "R9365", + "R5113" + ], + "R5113": [ + "R9343", + "R5549" + ], + "R9343": [ + "R5113", + "R5677" + ], + "R5677": [ + "R9343", + "R3322" + ], + "R3322": [ + "R5677", + "R2150" + ], + "R2150": [ + "R3322", + "R4541" + ], + "R4541": [ + "R2150", + "R8842" + ], + "R8842": [ + "R4541" + ], + "R9315": [ + "R7682", + "R1052" + ], + "R1052": [ + "R1093", + "R9315" + ], + "R1093": [ + "R1052", + "R4305" + ], + "R4305": [ + "R1093", + "R8677" + ], + "R8677": [ + "R4324", + "R4305" + ], + "R4324": [ + "R8677", + "R3519" + ], + "R3519": [ + "R2003", + "R4324" + ], + "R2003": [ + "R5384", + "R3519" + ], + "R5384": [ + "R4766", + "R2003" + ], + "R4766": [ + "R5384" + ], + "R4246": [ + "R7692", + "R7622" + ], + "R7692": [ + "R2490", + "R4246" + ], + "R2490": [ + "R3863", + "R7692" + ], + "R3863": [ + "R3006", + "R2490" + ], + "R3006": [ + "R8615", + "R3863" + ], + "R8615": [ + "R6972", + "R3006" + ], + "R6972": [ + "R8615", + "R5900" + ], + "R5900": [ + "R6972", + "R9038" + ], + "R9038": [ + "R6692", + "R5900" + ], + "R6692": [ + "R9038" + ], + "R2907": [ + "R7622", + "R4685" + ], + "R4685": [ + "R2907", + "R7564" + ], + "R7564": [ + "R4685", + "R1920" + ], + "R1920": [ + "R7564", + "R7655" + ], + "R7655": [ + "R1920", + "R5167" + ], + "R5167": [ + "R1515", + "R7655" + ], + "R1515": [ + "R6253", + "R5167" + ], + "R6253": [ + "R9744", + "R1515" + ], + "R9744": [ + "R3460", + "R6253" + ], + "R3460": [ + "R9744" + ], + "R8113": [ + "R9502", + "R7622" + ], + "R9502": [ + "R8113", + "R4982" + ], + "R4982": [ + "R9502", + "R1430" + ], + "R1430": [ + "R7562", + "R4982" + ], + "R7562": [ + "R1430", + "R2002" + ], + "R2002": [ + "R7562", + "R4154" + ], + "R4154": [ + "R2002", + "R2976" + ], + "R2976": [ + "R9758", + "R4154" + ], + "R9758": [ + "R2976", + "R9404" + ], + "R9404": [ + "R9758" + ], + "R4111": [ + "R5796", + "R3073" + ], + "R5796": [ + "R3455", + "R4111" + ], + "R3455": [ + "R5796", + "R7460" + ], + "R7460": [ + "R4546", + "R3455" + ], + "R4546": [ + "R7460", + "R5678" + ], + "R5678": [ + "R4546", + "R6936" + ], + "R6936": [ + "R8112", + "R5678" + ], + "R8112": [ + "R3372", + "R6936" + ], + "R3372": [ + "R1538", + "R8112" + ], + "R1538": [ + "R3372" + ], + "R9338": [ + "R2625", + "R3073" + ], + "R2625": [ + "R9338", + "R5145" + ], + "R5145": [ + "R2625", + "R2204" + ], + "R2204": [ + "R5145", + "R9476" + ], + "R9476": [ + "R2204", + "R6469" + ], + "R6469": [ + "R9476", + "R5696" + ], + "R5696": [ + "R1025", + "R6469" + ], + "R1025": [ + "R4633", + "R5696" + ], + "R4633": [ + "R1025", + "R7079" + ], + "R7079": [ + "R4633" + ], + "R4955": [ + "R3073", + "R6438" + ], + "R6438": [ + "R4955", + "R7616" + ], + "R7616": [ + "R6438", + "R8169" + ], + "R8169": [ + "R3558", + "R7616" + ], + "R3558": [ + "R8169", + "R1599" + ], + "R1599": [ + "R3558", + "R4660" + ], + "R4660": [ + "R1599", + "R4029" + ], + "R4029": [ + "R4660", + "R6815" + ], + "R6815": [ + "R4029", + "R1418" + ], + "R1418": [ + "R6815" + ] + } +} diff --git a/tasks/maze-t20/instruction.md b/tasks/maze-t20/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t20/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t20/solution/solve.sh b/tasks/maze-t20/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..505455ff7ec2429c0cbac7930c28425a676b98c1 --- /dev/null +++ b/tasks/maze-t20/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R5528 R8740 R9070 R8216 R3535 R9036 R3140 R6904 R8103 R7588 R2361 R3223 R4887 R9810 R6499 R9045 R9759 R3825 R4552 R7622 R8113 R9502 R4982 R1430 R7562 R2002 R4154 R2976 R9758 R9404; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t20/task.toml b/tasks/maze-t20/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..35bdcd00abb099e044184cd0270e746e1c96efc3 --- /dev/null +++ b/tasks/maze-t20/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t20" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 20 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t20/tests/Dockerfile b/tasks/maze-t20/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t20/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t20/tests/expected.json b/tasks/maze-t20/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..24de73164537d35769470205e8252e9ec93f7e2a --- /dev/null +++ b/tasks/maze-t20/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R8368", + "exit": "R9404", + "budget": 210, + "tree_sha256": "eb3ae3f9387422b1bcb949b3c75178b15f20a3453c87d956120edfaf2ef1ce85" +} diff --git a/tasks/maze-t20/tests/grade.py b/tasks/maze-t20/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t20/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t20/tests/test.sh b/tasks/maze-t20/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t20/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t21/environment/agent/Dockerfile b/tasks/maze-t21/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t21/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t21/environment/agent/maze b/tasks/maze-t21/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t21/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t21/environment/docker-compose.yaml b/tasks/maze-t21/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d5e9565e2e7b8f88fe862d86e67445e6002c0b98 --- /dev/null +++ b/tasks/maze-t21/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t21: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t21-7cf962a3f699 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t21/environment/maze/Dockerfile b/tasks/maze-t21/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t21/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t21/environment/maze/engine.py b/tasks/maze-t21/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t21/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t21/environment/maze/export.py b/tasks/maze-t21/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t21/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t21/environment/maze/service.py b/tasks/maze-t21/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t21/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t21/environment/maze/success.py b/tasks/maze-t21/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t21/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t21/environment/maze/tree.json b/tasks/maze-t21/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..c64bdec4fa99e91b406b745439393c5ab3a3344c --- /dev/null +++ b/tasks/maze-t21/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R4380", + "exit": "R2185", + "budget": 210, + "doors": { + "R4380": [ + "R6749", + "R1749", + "R5905" + ], + "R5905": [ + "R4380", + "R2410" + ], + "R2410": [ + "R5905", + "R9953" + ], + "R9953": [ + "R2410", + "R4455" + ], + "R4455": [ + "R9953", + "R5971" + ], + "R5971": [ + "R4455", + "R5749" + ], + "R5749": [ + "R5971", + "R9048" + ], + "R9048": [ + "R5749", + "R9516" + ], + "R9516": [ + "R9048", + "R5974" + ], + "R5974": [ + "R9516", + "R6475" + ], + "R6475": [ + "R4383", + "R5974", + "R3438", + "R3394" + ], + "R1749": [ + "R8465", + "R4380" + ], + "R8465": [ + "R1749", + "R4513" + ], + "R4513": [ + "R2429", + "R8465" + ], + "R2429": [ + "R4513", + "R7766" + ], + "R7766": [ + "R2429", + "R8898" + ], + "R8898": [ + "R7766", + "R4019" + ], + "R4019": [ + "R8898", + "R5174" + ], + "R5174": [ + "R7284", + "R4019" + ], + "R7284": [ + "R7925", + "R5174" + ], + "R7925": [ + "R6647", + "R3345", + "R4137", + "R7284" + ], + "R6749": [ + "R1691", + "R4380" + ], + "R1691": [ + "R6749", + "R6428" + ], + "R6428": [ + "R1691", + "R4483" + ], + "R4483": [ + "R6428", + "R7892" + ], + "R7892": [ + "R4483", + "R3595" + ], + "R3595": [ + "R8424", + "R7892" + ], + "R8424": [ + "R3595", + "R2563" + ], + "R2563": [ + "R6477", + "R8424" + ], + "R6477": [ + "R2563", + "R1423" + ], + "R1423": [ + "R3445", + "R3230", + "R7831", + "R6477" + ], + "R3394": [ + "R2139", + "R6475" + ], + "R2139": [ + "R3394", + "R9320" + ], + "R9320": [ + "R2139", + "R8216" + ], + "R8216": [ + "R3334", + "R9320" + ], + "R3334": [ + "R8216", + "R3986" + ], + "R3986": [ + "R3892", + "R3334" + ], + "R3892": [ + "R8068", + "R3986" + ], + "R8068": [ + "R5746", + "R3892" + ], + "R5746": [ + "R8799", + "R8068" + ], + "R8799": [ + "R2158", + "R8523", + "R2567", + "R5746" + ], + "R4383": [ + "R3242", + "R6475" + ], + "R3242": [ + "R5476", + "R4383" + ], + "R5476": [ + "R4648", + "R3242" + ], + "R4648": [ + "R5476", + "R6840" + ], + "R6840": [ + "R4648", + "R1857" + ], + "R1857": [ + "R3684", + "R6840" + ], + "R3684": [ + "R1857", + "R8679" + ], + "R8679": [ + "R3684", + "R3528" + ], + "R3528": [ + "R8679", + "R5514" + ], + "R5514": [ + "R3528", + "R1305", + "R3633", + "R9062" + ], + "R3438": [ + "R9622", + "R6475" + ], + "R9622": [ + "R6110", + "R3438" + ], + "R6110": [ + "R9622", + "R7479" + ], + "R7479": [ + "R6110", + "R8639" + ], + "R8639": [ + "R7479", + "R4406" + ], + "R4406": [ + "R8639", + "R4014" + ], + "R4014": [ + "R8378", + "R4406" + ], + "R8378": [ + "R4014", + "R2213" + ], + "R2213": [ + "R8378", + "R9790" + ], + "R9790": [ + "R2213", + "R6166", + "R6112", + "R5691" + ], + "R6647": [ + "R1067", + "R7925" + ], + "R1067": [ + "R6647", + "R9210" + ], + "R9210": [ + "R8229", + "R1067" + ], + "R8229": [ + "R9210", + "R1504" + ], + "R1504": [ + "R7780", + "R8229" + ], + "R7780": [ + "R3980", + "R1504" + ], + "R3980": [ + "R7780", + "R5081" + ], + "R5081": [ + "R3980", + "R1721" + ], + "R1721": [ + "R1903", + "R5081" + ], + "R1903": [ + "R2416", + "R1721", + "R3186", + "R1893" + ], + "R3345": [ + "R7925", + "R3773" + ], + "R3773": [ + "R8218", + "R3345" + ], + "R8218": [ + "R3773", + "R4742" + ], + "R4742": [ + "R8218", + "R8446" + ], + "R8446": [ + "R4742", + "R8734" + ], + "R8734": [ + "R8446", + "R5169" + ], + "R5169": [ + "R8734", + "R8449" + ], + "R8449": [ + "R5169", + "R4230" + ], + "R4230": [ + "R8449", + "R5268" + ], + "R5268": [ + "R4680", + "R4230", + "R1887", + "R4676" + ], + "R4137": [ + "R4369", + "R7925" + ], + "R4369": [ + "R4137", + "R7556" + ], + "R7556": [ + "R4369", + "R1558" + ], + "R1558": [ + "R7556", + "R9064" + ], + "R9064": [ + "R4252", + "R1558" + ], + "R4252": [ + "R7295", + "R9064" + ], + "R7295": [ + "R1399", + "R4252" + ], + "R1399": [ + "R5328", + "R7295" + ], + "R5328": [ + "R1399", + "R9789" + ], + "R9789": [ + "R4611", + "R5328", + "R9919", + "R4402" + ], + "R7831": [ + "R7681", + "R1423" + ], + "R7681": [ + "R7831", + "R2446" + ], + "R2446": [ + "R7681", + "R2936" + ], + "R2936": [ + "R3931", + "R2446" + ], + "R3931": [ + "R2936", + "R8778" + ], + "R8778": [ + "R5447", + "R3931" + ], + "R5447": [ + "R8778", + "R3287" + ], + "R3287": [ + "R1417", + "R5447" + ], + "R1417": [ + "R5649", + "R3287" + ], + "R5649": [ + "R6044", + "R2986", + "R5677", + "R1417" + ], + "R3445": [ + "R8487", + "R1423" + ], + "R8487": [ + "R7319", + "R3445" + ], + "R7319": [ + "R2874", + "R8487" + ], + "R2874": [ + "R5189", + "R7319" + ], + "R5189": [ + "R2874", + "R4225" + ], + "R4225": [ + "R5189", + "R1707" + ], + "R1707": [ + "R9521", + "R4225" + ], + "R9521": [ + "R6476", + "R1707" + ], + "R6476": [ + "R5131", + "R9521" + ], + "R5131": [ + "R6476", + "R7022", + "R1002", + "R8091" + ], + "R3230": [ + "R6998", + "R1423" + ], + "R6998": [ + "R5890", + "R3230" + ], + "R5890": [ + "R6998", + "R7592" + ], + "R7592": [ + "R9750", + "R5890" + ], + "R9750": [ + "R5671", + "R7592" + ], + "R5671": [ + "R3948", + "R9750" + ], + "R3948": [ + "R5321", + "R5671" + ], + "R5321": [ + "R3948", + "R6027" + ], + "R6027": [ + "R5321", + "R5808" + ], + "R5808": [ + "R7993", + "R6027", + "R9509", + "R3638" + ], + "R2567": [ + "R4841", + "R8799" + ], + "R4841": [ + "R2567", + "R8990" + ], + "R8990": [ + "R3590", + "R4841" + ], + "R3590": [ + "R8990", + "R9315" + ], + "R9315": [ + "R3590", + "R3685" + ], + "R3685": [ + "R9651", + "R9315" + ], + "R9651": [ + "R3685", + "R1875" + ], + "R1875": [ + "R9651", + "R5674" + ], + "R5674": [ + "R2271", + "R1875" + ], + "R2271": [ + "R5674" + ], + "R8523": [ + "R8799", + "R6971" + ], + "R6971": [ + "R8523", + "R1409" + ], + "R1409": [ + "R6971", + "R1326" + ], + "R1326": [ + "R1409", + "R6834" + ], + "R6834": [ + "R1326", + "R3666" + ], + "R3666": [ + "R7638", + "R6834" + ], + "R7638": [ + "R3666", + "R1198" + ], + "R1198": [ + "R7638", + "R6020" + ], + "R6020": [ + "R1198", + "R9645" + ], + "R9645": [ + "R6020" + ], + "R2158": [ + "R8799", + "R3197" + ], + "R3197": [ + "R2158", + "R6787" + ], + "R6787": [ + "R4995", + "R3197" + ], + "R4995": [ + "R6768", + "R6787" + ], + "R6768": [ + "R4995", + "R2578" + ], + "R2578": [ + "R7183", + "R6768" + ], + "R7183": [ + "R6355", + "R2578" + ], + "R6355": [ + "R6930", + "R7183" + ], + "R6930": [ + "R2185", + "R6355" + ], + "R2185": [ + "R6930" + ], + "R3633": [ + "R3404", + "R5514" + ], + "R3404": [ + "R8872", + "R3633" + ], + "R8872": [ + "R3404", + "R9215" + ], + "R9215": [ + "R9856", + "R8872" + ], + "R9856": [ + "R3012", + "R9215" + ], + "R3012": [ + "R9856", + "R9163" + ], + "R9163": [ + "R3012", + "R6510" + ], + "R6510": [ + "R9163", + "R5388" + ], + "R5388": [ + "R8045", + "R6510" + ], + "R8045": [ + "R5388" + ], + "R9062": [ + "R6339", + "R5514" + ], + "R6339": [ + "R9062", + "R6449" + ], + "R6449": [ + "R3053", + "R6339" + ], + "R3053": [ + "R8900", + "R6449" + ], + "R8900": [ + "R3035", + "R3053" + ], + "R3035": [ + "R2794", + "R8900" + ], + "R2794": [ + "R5242", + "R3035" + ], + "R5242": [ + "R1420", + "R2794" + ], + "R1420": [ + "R7618", + "R5242" + ], + "R7618": [ + "R1420" + ], + "R1305": [ + "R5514", + "R9620" + ], + "R9620": [ + "R1305", + "R9530" + ], + "R9530": [ + "R7221", + "R9620" + ], + "R7221": [ + "R9530", + "R6904" + ], + "R6904": [ + "R7221", + "R2238" + ], + "R2238": [ + "R6904", + "R8386" + ], + "R8386": [ + "R9452", + "R2238" + ], + "R9452": [ + "R8386", + "R7452" + ], + "R7452": [ + "R1437", + "R9452" + ], + "R1437": [ + "R7452" + ], + "R6112": [ + "R4261", + "R9790" + ], + "R4261": [ + "R2149", + "R6112" + ], + "R2149": [ + "R4955", + "R4261" + ], + "R4955": [ + "R8079", + "R2149" + ], + "R8079": [ + "R4955", + "R9629" + ], + "R9629": [ + "R6962", + "R8079" + ], + "R6962": [ + "R4186", + "R9629" + ], + "R4186": [ + "R4339", + "R6962" + ], + "R4339": [ + "R5121", + "R4186" + ], + "R5121": [ + "R4339" + ], + "R5691": [ + "R2497", + "R9790" + ], + "R2497": [ + "R5691", + "R9989" + ], + "R9989": [ + "R2497", + "R7537" + ], + "R7537": [ + "R9989", + "R8823" + ], + "R8823": [ + "R7537", + "R1865" + ], + "R1865": [ + "R8823", + "R7751" + ], + "R7751": [ + "R1865", + "R6518" + ], + "R6518": [ + "R7751", + "R1982" + ], + "R1982": [ + "R6518", + "R8466" + ], + "R8466": [ + "R1982" + ], + "R6166": [ + "R9790", + "R1956" + ], + "R1956": [ + "R6166", + "R6125" + ], + "R6125": [ + "R8722", + "R1956" + ], + "R8722": [ + "R6125", + "R7870" + ], + "R7870": [ + "R7834", + "R8722" + ], + "R7834": [ + "R7870", + "R5065" + ], + "R5065": [ + "R7834", + "R9042" + ], + "R9042": [ + "R8305", + "R5065" + ], + "R8305": [ + "R9042", + "R7871" + ], + "R7871": [ + "R8305" + ], + "R2416": [ + "R1903", + "R6755" + ], + "R6755": [ + "R2416", + "R2766" + ], + "R2766": [ + "R6755", + "R8348" + ], + "R8348": [ + "R6280", + "R2766" + ], + "R6280": [ + "R8348", + "R7437" + ], + "R7437": [ + "R6280", + "R1447" + ], + "R1447": [ + "R7437", + "R2032" + ], + "R2032": [ + "R1758", + "R1447" + ], + "R1758": [ + "R2032", + "R8193" + ], + "R8193": [ + "R1758" + ], + "R3186": [ + "R5392", + "R1903" + ], + "R5392": [ + "R3186", + "R8266" + ], + "R8266": [ + "R5392", + "R6406" + ], + "R6406": [ + "R2558", + "R8266" + ], + "R2558": [ + "R9510", + "R6406" + ], + "R9510": [ + "R2558", + "R3008" + ], + "R3008": [ + "R4012", + "R9510" + ], + "R4012": [ + "R1389", + "R3008" + ], + "R1389": [ + "R4012", + "R8544" + ], + "R8544": [ + "R1389" + ], + "R1893": [ + "R1903", + "R8265" + ], + "R8265": [ + "R8420", + "R1893" + ], + "R8420": [ + "R9234", + "R8265" + ], + "R9234": [ + "R8420", + "R7616" + ], + "R7616": [ + "R4831", + "R9234" + ], + "R4831": [ + "R7616", + "R7574" + ], + "R7574": [ + "R4831", + "R4656" + ], + "R4656": [ + "R4759", + "R7574" + ], + "R4759": [ + "R4656", + "R6730" + ], + "R6730": [ + "R4759" + ], + "R4680": [ + "R9295", + "R5268" + ], + "R9295": [ + "R4680", + "R1499" + ], + "R1499": [ + "R9295", + "R5852" + ], + "R5852": [ + "R1499", + "R7115" + ], + "R7115": [ + "R5179", + "R5852" + ], + "R5179": [ + "R7115", + "R9308" + ], + "R9308": [ + "R5179", + "R5406" + ], + "R5406": [ + "R9308", + "R2687" + ], + "R2687": [ + "R5406", + "R2771" + ], + "R2771": [ + "R2687" + ], + "R1887": [ + "R9020", + "R5268" + ], + "R9020": [ + "R1800", + "R1887" + ], + "R1800": [ + "R9020", + "R9265" + ], + "R9265": [ + "R8513", + "R1800" + ], + "R8513": [ + "R9265", + "R6063" + ], + "R6063": [ + "R9839", + "R8513" + ], + "R9839": [ + "R9690", + "R6063" + ], + "R9690": [ + "R3290", + "R9839" + ], + "R3290": [ + "R5423", + "R9690" + ], + "R5423": [ + "R3290" + ], + "R4676": [ + "R5268", + "R7851" + ], + "R7851": [ + "R4676", + "R1133" + ], + "R1133": [ + "R7851", + "R1797" + ], + "R1797": [ + "R6405", + "R1133" + ], + "R6405": [ + "R4294", + "R1797" + ], + "R4294": [ + "R6405", + "R5571" + ], + "R5571": [ + "R4294", + "R1750" + ], + "R1750": [ + "R5571", + "R3574" + ], + "R3574": [ + "R2545", + "R1750" + ], + "R2545": [ + "R3574" + ], + "R4611": [ + "R5052", + "R9789" + ], + "R5052": [ + "R4611", + "R5751" + ], + "R5751": [ + "R5052", + "R9838" + ], + "R9838": [ + "R9711", + "R5751" + ], + "R9711": [ + "R9285", + "R9838" + ], + "R9285": [ + "R8918", + "R9711" + ], + "R8918": [ + "R5811", + "R9285" + ], + "R5811": [ + "R1371", + "R8918" + ], + "R1371": [ + "R5811", + "R3847" + ], + "R3847": [ + "R1371" + ], + "R4402": [ + "R9789", + "R2579" + ], + "R2579": [ + "R3517", + "R4402" + ], + "R3517": [ + "R2579", + "R7089" + ], + "R7089": [ + "R2590", + "R3517" + ], + "R2590": [ + "R7089", + "R3912" + ], + "R3912": [ + "R2590", + "R2937" + ], + "R2937": [ + "R3912", + "R8989" + ], + "R8989": [ + "R4002", + "R2937" + ], + "R4002": [ + "R8989", + "R8553" + ], + "R8553": [ + "R4002" + ], + "R9919": [ + "R9789", + "R5403" + ], + "R5403": [ + "R5774", + "R9919" + ], + "R5774": [ + "R9149", + "R5403" + ], + "R9149": [ + "R5774", + "R4049" + ], + "R4049": [ + "R9149", + "R1604" + ], + "R1604": [ + "R3065", + "R4049" + ], + "R3065": [ + "R1604", + "R8975" + ], + "R8975": [ + "R3316", + "R3065" + ], + "R3316": [ + "R1655", + "R8975" + ], + "R1655": [ + "R3316" + ], + "R6044": [ + "R5649", + "R7552" + ], + "R7552": [ + "R7352", + "R6044" + ], + "R7352": [ + "R3196", + "R7552" + ], + "R3196": [ + "R2924", + "R7352" + ], + "R2924": [ + "R3196", + "R3566" + ], + "R3566": [ + "R5789", + "R2924" + ], + "R5789": [ + "R8500", + "R3566" + ], + "R8500": [ + "R5789", + "R4697" + ], + "R4697": [ + "R8500", + "R1445" + ], + "R1445": [ + "R4697" + ], + "R5677": [ + "R5649", + "R4148" + ], + "R4148": [ + "R5677", + "R5813" + ], + "R5813": [ + "R4148", + "R9200" + ], + "R9200": [ + "R4620", + "R5813" + ], + "R4620": [ + "R9200", + "R5405" + ], + "R5405": [ + "R4620", + "R6769" + ], + "R6769": [ + "R8682", + "R5405" + ], + "R8682": [ + "R4421", + "R6769" + ], + "R4421": [ + "R6694", + "R8682" + ], + "R6694": [ + "R4421" + ], + "R2986": [ + "R6276", + "R5649" + ], + "R6276": [ + "R2986", + "R2272" + ], + "R2272": [ + "R6276", + "R7399" + ], + "R7399": [ + "R4140", + "R2272" + ], + "R4140": [ + "R4649", + "R7399" + ], + "R4649": [ + "R4140", + "R2237" + ], + "R2237": [ + "R8241", + "R4649" + ], + "R8241": [ + "R2237", + "R5573" + ], + "R5573": [ + "R5989", + "R8241" + ], + "R5989": [ + "R5573" + ], + "R1002": [ + "R9351", + "R5131" + ], + "R9351": [ + "R1002", + "R2453" + ], + "R2453": [ + "R9351", + "R7065" + ], + "R7065": [ + "R1795", + "R2453" + ], + "R1795": [ + "R7065", + "R9403" + ], + "R9403": [ + "R1795", + "R7180" + ], + "R7180": [ + "R5205", + "R9403" + ], + "R5205": [ + "R7180", + "R2259" + ], + "R2259": [ + "R1233", + "R5205" + ], + "R1233": [ + "R2259" + ], + "R8091": [ + "R5131", + "R4558" + ], + "R4558": [ + "R8091", + "R6401" + ], + "R6401": [ + "R1783", + "R4558" + ], + "R1783": [ + "R3821", + "R6401" + ], + "R3821": [ + "R7267", + "R1783" + ], + "R7267": [ + "R5354", + "R3821" + ], + "R5354": [ + "R7267", + "R4433" + ], + "R4433": [ + "R5354", + "R7579" + ], + "R7579": [ + "R9727", + "R4433" + ], + "R9727": [ + "R7579" + ], + "R7022": [ + "R9103", + "R5131" + ], + "R9103": [ + "R5865", + "R7022" + ], + "R5865": [ + "R4021", + "R9103" + ], + "R4021": [ + "R5865", + "R7887" + ], + "R7887": [ + "R4021", + "R8319" + ], + "R8319": [ + "R7887", + "R5830" + ], + "R5830": [ + "R4914", + "R8319" + ], + "R4914": [ + "R8507", + "R5830" + ], + "R8507": [ + "R4914", + "R2042" + ], + "R2042": [ + "R8507" + ], + "R7993": [ + "R8126", + "R5808" + ], + "R8126": [ + "R7993", + "R5713" + ], + "R5713": [ + "R2565", + "R8126" + ], + "R2565": [ + "R5713", + "R1329" + ], + "R1329": [ + "R9948", + "R2565" + ], + "R9948": [ + "R5659", + "R1329" + ], + "R5659": [ + "R9948", + "R6544" + ], + "R6544": [ + "R5659", + "R4404" + ], + "R4404": [ + "R6544", + "R3719" + ], + "R3719": [ + "R4404" + ], + "R3638": [ + "R1130", + "R5808" + ], + "R1130": [ + "R5371", + "R3638" + ], + "R5371": [ + "R1989", + "R1130" + ], + "R1989": [ + "R5371", + "R1003" + ], + "R1003": [ + "R1989", + "R7210" + ], + "R7210": [ + "R5625", + "R1003" + ], + "R5625": [ + "R7210", + "R9756" + ], + "R9756": [ + "R5625", + "R2077" + ], + "R2077": [ + "R4494", + "R9756" + ], + "R4494": [ + "R2077" + ], + "R9509": [ + "R5808", + "R1530" + ], + "R1530": [ + "R6299", + "R9509" + ], + "R6299": [ + "R1917", + "R1530" + ], + "R1917": [ + "R1319", + "R6299" + ], + "R1319": [ + "R1917", + "R6170" + ], + "R6170": [ + "R8952", + "R1319" + ], + "R8952": [ + "R6170", + "R8072" + ], + "R8072": [ + "R8952", + "R8271" + ], + "R8271": [ + "R8072", + "R2716" + ], + "R2716": [ + "R8271" + ] + } +} diff --git a/tasks/maze-t21/instruction.md b/tasks/maze-t21/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t21/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t21/solution/solve.sh b/tasks/maze-t21/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..bb41e77ae79c81189593f8a4467484199721e1de --- /dev/null +++ b/tasks/maze-t21/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R5905 R2410 R9953 R4455 R5971 R5749 R9048 R9516 R5974 R6475 R3394 R2139 R9320 R8216 R3334 R3986 R3892 R8068 R5746 R8799 R2158 R3197 R6787 R4995 R6768 R2578 R7183 R6355 R6930 R2185; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t21/task.toml b/tasks/maze-t21/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7bf89d72002cdea4bd37387e348ba63e369afeaa --- /dev/null +++ b/tasks/maze-t21/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t21" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 21 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t21/tests/Dockerfile b/tasks/maze-t21/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t21/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t21/tests/expected.json b/tasks/maze-t21/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..af710f4496c083a71c839ca563cd4e2e3a84c8ee --- /dev/null +++ b/tasks/maze-t21/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R4380", + "exit": "R2185", + "budget": 210, + "tree_sha256": "90451f34ac8cbc99c5b0ee18b9b6d7340e732622712f6511d3b3fb5148eeb058" +} diff --git a/tasks/maze-t21/tests/grade.py b/tasks/maze-t21/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t21/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t21/tests/test.sh b/tasks/maze-t21/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t21/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t22/environment/agent/Dockerfile b/tasks/maze-t22/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t22/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t22/environment/agent/maze b/tasks/maze-t22/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t22/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t22/environment/docker-compose.yaml b/tasks/maze-t22/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..52adaecfc363047a314c119ffacf6c539d7811a9 --- /dev/null +++ b/tasks/maze-t22/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t22: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t22-96d382f0c4af + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t22/environment/maze/Dockerfile b/tasks/maze-t22/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t22/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t22/environment/maze/engine.py b/tasks/maze-t22/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t22/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t22/environment/maze/export.py b/tasks/maze-t22/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t22/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t22/environment/maze/service.py b/tasks/maze-t22/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t22/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t22/environment/maze/success.py b/tasks/maze-t22/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t22/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t22/environment/maze/tree.json b/tasks/maze-t22/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..d8a2e56f229015ad4ced839979bac1449bc43fcc --- /dev/null +++ b/tasks/maze-t22/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R5023", + "exit": "R2110", + "budget": 210, + "doors": { + "R5023": [ + "R1275", + "R7224", + "R5241" + ], + "R7224": [ + "R5023", + "R8055" + ], + "R8055": [ + "R7224", + "R7969" + ], + "R7969": [ + "R8055", + "R3938" + ], + "R3938": [ + "R7969", + "R6387" + ], + "R6387": [ + "R6052", + "R3938" + ], + "R6052": [ + "R9823", + "R6387" + ], + "R9823": [ + "R1229", + "R6052" + ], + "R1229": [ + "R1366", + "R9823" + ], + "R1366": [ + "R7970", + "R1229" + ], + "R7970": [ + "R5778", + "R1256", + "R5181", + "R1366" + ], + "R5241": [ + "R5023", + "R9656" + ], + "R9656": [ + "R5241", + "R7204" + ], + "R7204": [ + "R7494", + "R9656" + ], + "R7494": [ + "R3404", + "R7204" + ], + "R3404": [ + "R7494", + "R7821" + ], + "R7821": [ + "R4076", + "R3404" + ], + "R4076": [ + "R7821", + "R2810" + ], + "R2810": [ + "R4076", + "R7441" + ], + "R7441": [ + "R2810", + "R3428" + ], + "R3428": [ + "R4713", + "R1036", + "R7441", + "R9235" + ], + "R1275": [ + "R5023", + "R6115" + ], + "R6115": [ + "R1857", + "R1275" + ], + "R1857": [ + "R8338", + "R6115" + ], + "R8338": [ + "R3068", + "R1857" + ], + "R3068": [ + "R8338", + "R4512" + ], + "R4512": [ + "R3068", + "R4002" + ], + "R4002": [ + "R4512", + "R1172" + ], + "R1172": [ + "R1170", + "R4002" + ], + "R1170": [ + "R1172", + "R8382" + ], + "R8382": [ + "R8527", + "R3472", + "R2410", + "R1170" + ], + "R5181": [ + "R6671", + "R7970" + ], + "R6671": [ + "R5181", + "R3993" + ], + "R3993": [ + "R2044", + "R6671" + ], + "R2044": [ + "R3993", + "R8024" + ], + "R8024": [ + "R6378", + "R2044" + ], + "R6378": [ + "R8024", + "R8773" + ], + "R8773": [ + "R6378", + "R1898" + ], + "R1898": [ + "R9697", + "R8773" + ], + "R9697": [ + "R2675", + "R1898" + ], + "R2675": [ + "R9721", + "R3550", + "R7724", + "R9697" + ], + "R5778": [ + "R7970", + "R6192" + ], + "R6192": [ + "R5778", + "R1526" + ], + "R1526": [ + "R6192", + "R3618" + ], + "R3618": [ + "R1453", + "R1526" + ], + "R1453": [ + "R3618", + "R5232" + ], + "R5232": [ + "R1453", + "R9286" + ], + "R9286": [ + "R5232", + "R3492" + ], + "R3492": [ + "R9286", + "R2058" + ], + "R2058": [ + "R4099", + "R3492" + ], + "R4099": [ + "R5172", + "R8109", + "R2058", + "R1457" + ], + "R1256": [ + "R7970", + "R1517" + ], + "R1517": [ + "R2647", + "R1256" + ], + "R2647": [ + "R1517", + "R5604" + ], + "R5604": [ + "R2647", + "R2972" + ], + "R2972": [ + "R5604", + "R1875" + ], + "R1875": [ + "R2972", + "R6422" + ], + "R6422": [ + "R3467", + "R1875" + ], + "R3467": [ + "R6422", + "R5402" + ], + "R5402": [ + "R3467", + "R6683" + ], + "R6683": [ + "R9617", + "R8834", + "R2427", + "R5402" + ], + "R1036": [ + "R1374", + "R3428" + ], + "R1374": [ + "R4700", + "R1036" + ], + "R4700": [ + "R8516", + "R1374" + ], + "R8516": [ + "R4700", + "R7291" + ], + "R7291": [ + "R7297", + "R8516" + ], + "R7297": [ + "R7291", + "R1688" + ], + "R1688": [ + "R2265", + "R7297" + ], + "R2265": [ + "R6853", + "R1688" + ], + "R6853": [ + "R2265", + "R1746" + ], + "R1746": [ + "R3514", + "R5949", + "R6853", + "R6359" + ], + "R4713": [ + "R3428", + "R3883" + ], + "R3883": [ + "R4713", + "R2718" + ], + "R2718": [ + "R3883", + "R5194" + ], + "R5194": [ + "R8992", + "R2718" + ], + "R8992": [ + "R7409", + "R5194" + ], + "R7409": [ + "R5731", + "R8992" + ], + "R5731": [ + "R7409", + "R5197" + ], + "R5197": [ + "R5731", + "R5254" + ], + "R5254": [ + "R5197", + "R9399" + ], + "R9399": [ + "R6451", + "R1033", + "R5254", + "R1792" + ], + "R9235": [ + "R9043", + "R3428" + ], + "R9043": [ + "R9235", + "R4282" + ], + "R4282": [ + "R9043", + "R3724" + ], + "R3724": [ + "R9319", + "R4282" + ], + "R9319": [ + "R6327", + "R3724" + ], + "R6327": [ + "R1482", + "R9319" + ], + "R1482": [ + "R6327", + "R3195" + ], + "R3195": [ + "R9275", + "R1482" + ], + "R9275": [ + "R3195", + "R1030" + ], + "R1030": [ + "R6778", + "R9141", + "R3306", + "R9275" + ], + "R8527": [ + "R8382", + "R4290" + ], + "R4290": [ + "R8527", + "R7289" + ], + "R7289": [ + "R3776", + "R4290" + ], + "R3776": [ + "R8227", + "R7289" + ], + "R8227": [ + "R3776", + "R1824" + ], + "R1824": [ + "R6531", + "R8227" + ], + "R6531": [ + "R1824", + "R2882" + ], + "R2882": [ + "R6531", + "R5325" + ], + "R5325": [ + "R2882", + "R5642" + ], + "R5642": [ + "R2935", + "R6939", + "R5381", + "R5325" + ], + "R2410": [ + "R9183", + "R8382" + ], + "R9183": [ + "R2410", + "R4297" + ], + "R4297": [ + "R5582", + "R9183" + ], + "R5582": [ + "R8441", + "R4297" + ], + "R8441": [ + "R5582", + "R8052" + ], + "R8052": [ + "R8441", + "R9972" + ], + "R9972": [ + "R4935", + "R8052" + ], + "R4935": [ + "R9972", + "R1882" + ], + "R1882": [ + "R4935", + "R1106" + ], + "R1106": [ + "R2539", + "R1882", + "R4281", + "R8751" + ], + "R3472": [ + "R4675", + "R8382" + ], + "R4675": [ + "R5238", + "R3472" + ], + "R5238": [ + "R3363", + "R4675" + ], + "R3363": [ + "R4116", + "R5238" + ], + "R4116": [ + "R3363", + "R7945" + ], + "R7945": [ + "R9907", + "R4116" + ], + "R9907": [ + "R1339", + "R7945" + ], + "R1339": [ + "R2065", + "R9907" + ], + "R2065": [ + "R1339", + "R1508" + ], + "R1508": [ + "R9781", + "R2065", + "R9981", + "R6124" + ], + "R7724": [ + "R5936", + "R2675" + ], + "R5936": [ + "R7724", + "R1863" + ], + "R1863": [ + "R6989", + "R5936" + ], + "R6989": [ + "R1863", + "R1203" + ], + "R1203": [ + "R7312", + "R6989" + ], + "R7312": [ + "R1203", + "R2131" + ], + "R2131": [ + "R5115", + "R7312" + ], + "R5115": [ + "R2131", + "R5761" + ], + "R5761": [ + "R5115", + "R6784" + ], + "R6784": [ + "R5761" + ], + "R9721": [ + "R9776", + "R2675" + ], + "R9776": [ + "R9721", + "R2343" + ], + "R2343": [ + "R3322", + "R9776" + ], + "R3322": [ + "R7083", + "R2343" + ], + "R7083": [ + "R5745", + "R3322" + ], + "R5745": [ + "R7162", + "R7083" + ], + "R7162": [ + "R7044", + "R5745" + ], + "R7044": [ + "R7162", + "R1860" + ], + "R1860": [ + "R8875", + "R7044" + ], + "R8875": [ + "R1860" + ], + "R3550": [ + "R9429", + "R2675" + ], + "R9429": [ + "R3550", + "R3259" + ], + "R3259": [ + "R4311", + "R9429" + ], + "R4311": [ + "R7189", + "R3259" + ], + "R7189": [ + "R4311", + "R1124" + ], + "R1124": [ + "R3060", + "R7189" + ], + "R3060": [ + "R1124", + "R9463" + ], + "R9463": [ + "R6840", + "R3060" + ], + "R6840": [ + "R1479", + "R9463" + ], + "R1479": [ + "R6840" + ], + "R8109": [ + "R3698", + "R4099" + ], + "R3698": [ + "R9361", + "R8109" + ], + "R9361": [ + "R3847", + "R3698" + ], + "R3847": [ + "R9361", + "R4340" + ], + "R4340": [ + "R1698", + "R3847" + ], + "R1698": [ + "R4340", + "R6856" + ], + "R6856": [ + "R1698", + "R5174" + ], + "R5174": [ + "R6856", + "R8252" + ], + "R8252": [ + "R3719", + "R5174" + ], + "R3719": [ + "R8252" + ], + "R5172": [ + "R9129", + "R4099" + ], + "R9129": [ + "R5773", + "R5172" + ], + "R5773": [ + "R9129", + "R8657" + ], + "R8657": [ + "R6139", + "R5773" + ], + "R6139": [ + "R8657", + "R4206" + ], + "R4206": [ + "R3569", + "R6139" + ], + "R3569": [ + "R4206", + "R7960" + ], + "R7960": [ + "R3569", + "R9457" + ], + "R9457": [ + "R4825", + "R7960" + ], + "R4825": [ + "R9457" + ], + "R1457": [ + "R4099", + "R9180" + ], + "R9180": [ + "R8594", + "R1457" + ], + "R8594": [ + "R9180", + "R5448" + ], + "R5448": [ + "R1573", + "R8594" + ], + "R1573": [ + "R5448", + "R9747" + ], + "R9747": [ + "R1573", + "R9027" + ], + "R9027": [ + "R9747", + "R7939" + ], + "R7939": [ + "R9027", + "R8818" + ], + "R8818": [ + "R7872", + "R7939" + ], + "R7872": [ + "R8818" + ], + "R2427": [ + "R5353", + "R6683" + ], + "R5353": [ + "R5717", + "R2427" + ], + "R5717": [ + "R8318", + "R5353" + ], + "R8318": [ + "R9466", + "R5717" + ], + "R9466": [ + "R8318", + "R8450" + ], + "R8450": [ + "R9466", + "R2529" + ], + "R2529": [ + "R8167", + "R8450" + ], + "R8167": [ + "R2529", + "R2758" + ], + "R2758": [ + "R8167", + "R9175" + ], + "R9175": [ + "R2758" + ], + "R9617": [ + "R8335", + "R6683" + ], + "R8335": [ + "R5579", + "R9617" + ], + "R5579": [ + "R8335", + "R9932" + ], + "R9932": [ + "R5579", + "R8939" + ], + "R8939": [ + "R9932", + "R7751" + ], + "R7751": [ + "R8090", + "R8939" + ], + "R8090": [ + "R9640", + "R7751" + ], + "R9640": [ + "R9806", + "R8090" + ], + "R9806": [ + "R9640", + "R5624" + ], + "R5624": [ + "R9806" + ], + "R8834": [ + "R6683", + "R2101" + ], + "R2101": [ + "R4585", + "R8834" + ], + "R4585": [ + "R8116", + "R2101" + ], + "R8116": [ + "R7961", + "R4585" + ], + "R7961": [ + "R8116", + "R9032" + ], + "R9032": [ + "R9584", + "R7961" + ], + "R9584": [ + "R6980", + "R9032" + ], + "R6980": [ + "R9584", + "R9729" + ], + "R9729": [ + "R5725", + "R6980" + ], + "R5725": [ + "R9729" + ], + "R6359": [ + "R1746", + "R2326" + ], + "R2326": [ + "R6359", + "R4317" + ], + "R4317": [ + "R4838", + "R2326" + ], + "R4838": [ + "R4317", + "R9815" + ], + "R9815": [ + "R1399", + "R4838" + ], + "R1399": [ + "R9815", + "R6414" + ], + "R6414": [ + "R1399", + "R3140" + ], + "R3140": [ + "R1079", + "R6414" + ], + "R1079": [ + "R3140", + "R6966" + ], + "R6966": [ + "R1079" + ], + "R3514": [ + "R1746", + "R2168" + ], + "R2168": [ + "R3514", + "R4272" + ], + "R4272": [ + "R8823", + "R2168" + ], + "R8823": [ + "R3562", + "R4272" + ], + "R3562": [ + "R5718", + "R8823" + ], + "R5718": [ + "R5792", + "R3562" + ], + "R5792": [ + "R9933", + "R5718" + ], + "R9933": [ + "R5792", + "R7214" + ], + "R7214": [ + "R2075", + "R9933" + ], + "R2075": [ + "R7214" + ], + "R5949": [ + "R9282", + "R1746" + ], + "R9282": [ + "R4973", + "R5949" + ], + "R4973": [ + "R9282", + "R4214" + ], + "R4214": [ + "R4979", + "R4973" + ], + "R4979": [ + "R9139", + "R4214" + ], + "R9139": [ + "R7782", + "R4979" + ], + "R7782": [ + "R4279", + "R9139" + ], + "R4279": [ + "R7664", + "R7782" + ], + "R7664": [ + "R8191", + "R4279" + ], + "R8191": [ + "R7664" + ], + "R1792": [ + "R5210", + "R9399" + ], + "R5210": [ + "R1792", + "R3803" + ], + "R3803": [ + "R5210", + "R1788" + ], + "R1788": [ + "R8122", + "R3803" + ], + "R8122": [ + "R4347", + "R1788" + ], + "R4347": [ + "R8122", + "R8372" + ], + "R8372": [ + "R4347", + "R2173" + ], + "R2173": [ + "R9374", + "R8372" + ], + "R9374": [ + "R2173", + "R9624" + ], + "R9624": [ + "R9374" + ], + "R6451": [ + "R2418", + "R9399" + ], + "R2418": [ + "R6451", + "R8843" + ], + "R8843": [ + "R2418", + "R1758" + ], + "R1758": [ + "R8173", + "R8843" + ], + "R8173": [ + "R3141", + "R1758" + ], + "R3141": [ + "R9759", + "R8173" + ], + "R9759": [ + "R3141", + "R3634" + ], + "R3634": [ + "R2047", + "R9759" + ], + "R2047": [ + "R3634", + "R7213" + ], + "R7213": [ + "R2047" + ], + "R1033": [ + "R9399", + "R1095" + ], + "R1095": [ + "R1033", + "R4918" + ], + "R4918": [ + "R1095", + "R9553" + ], + "R9553": [ + "R4918", + "R6229" + ], + "R6229": [ + "R9553", + "R3029" + ], + "R3029": [ + "R6229", + "R2977" + ], + "R2977": [ + "R9090", + "R3029" + ], + "R9090": [ + "R1223", + "R2977" + ], + "R1223": [ + "R7569", + "R9090" + ], + "R7569": [ + "R1223" + ], + "R6778": [ + "R6447", + "R1030" + ], + "R6447": [ + "R6778", + "R9586" + ], + "R9586": [ + "R7299", + "R6447" + ], + "R7299": [ + "R9586", + "R6965" + ], + "R6965": [ + "R1899", + "R7299" + ], + "R1899": [ + "R6965", + "R6577" + ], + "R6577": [ + "R1899", + "R1025" + ], + "R1025": [ + "R7013", + "R6577" + ], + "R7013": [ + "R8560", + "R1025" + ], + "R8560": [ + "R7013" + ], + "R9141": [ + "R1030", + "R3480" + ], + "R3480": [ + "R7296", + "R9141" + ], + "R7296": [ + "R6424", + "R3480" + ], + "R6424": [ + "R7296", + "R2370" + ], + "R2370": [ + "R6424", + "R6349" + ], + "R6349": [ + "R2370", + "R9902" + ], + "R9902": [ + "R6349", + "R7191" + ], + "R7191": [ + "R9902", + "R6291" + ], + "R6291": [ + "R7191", + "R2444" + ], + "R2444": [ + "R6291" + ], + "R3306": [ + "R1030", + "R1659" + ], + "R1659": [ + "R5086", + "R3306" + ], + "R5086": [ + "R9660", + "R1659" + ], + "R9660": [ + "R5086", + "R7230" + ], + "R7230": [ + "R2678", + "R9660" + ], + "R2678": [ + "R7230", + "R9029" + ], + "R9029": [ + "R2678", + "R6456" + ], + "R6456": [ + "R4447", + "R9029" + ], + "R4447": [ + "R9825", + "R6456" + ], + "R9825": [ + "R4447" + ], + "R5381": [ + "R1401", + "R5642" + ], + "R1401": [ + "R8374", + "R5381" + ], + "R8374": [ + "R1756", + "R1401" + ], + "R1756": [ + "R2195", + "R8374" + ], + "R2195": [ + "R1208", + "R1756" + ], + "R1208": [ + "R2195", + "R9273" + ], + "R9273": [ + "R5041", + "R1208" + ], + "R5041": [ + "R9273", + "R8023" + ], + "R8023": [ + "R4730", + "R5041" + ], + "R4730": [ + "R8023" + ], + "R2935": [ + "R5642", + "R1744" + ], + "R1744": [ + "R2935", + "R1378" + ], + "R1378": [ + "R8613", + "R1744" + ], + "R8613": [ + "R1378", + "R7198" + ], + "R7198": [ + "R8613", + "R7435" + ], + "R7435": [ + "R6952", + "R7198" + ], + "R6952": [ + "R7435", + "R3810" + ], + "R3810": [ + "R7323", + "R6952" + ], + "R7323": [ + "R3810", + "R2705" + ], + "R2705": [ + "R7323" + ], + "R6939": [ + "R8857", + "R5642" + ], + "R8857": [ + "R6939", + "R2064" + ], + "R2064": [ + "R8857", + "R3419" + ], + "R3419": [ + "R5622", + "R2064" + ], + "R5622": [ + "R3419", + "R1363" + ], + "R1363": [ + "R5622", + "R8428" + ], + "R8428": [ + "R2194", + "R1363" + ], + "R2194": [ + "R5045", + "R8428" + ], + "R5045": [ + "R2194", + "R2112" + ], + "R2112": [ + "R5045" + ], + "R4281": [ + "R4915", + "R1106" + ], + "R4915": [ + "R4423", + "R4281" + ], + "R4423": [ + "R4915", + "R2494" + ], + "R2494": [ + "R4423", + "R8394" + ], + "R8394": [ + "R2494", + "R6535" + ], + "R6535": [ + "R8394", + "R6440" + ], + "R6440": [ + "R1876", + "R6535" + ], + "R1876": [ + "R6440", + "R5607" + ], + "R5607": [ + "R5538", + "R1876" + ], + "R5538": [ + "R5607" + ], + "R2539": [ + "R1106", + "R2007" + ], + "R2007": [ + "R5616", + "R2539" + ], + "R5616": [ + "R2007", + "R5289" + ], + "R5289": [ + "R3055", + "R5616" + ], + "R3055": [ + "R4761", + "R5289" + ], + "R4761": [ + "R3055", + "R3506" + ], + "R3506": [ + "R4761", + "R4431" + ], + "R4431": [ + "R3506", + "R5979" + ], + "R5979": [ + "R4431", + "R4490" + ], + "R4490": [ + "R5979" + ], + "R8751": [ + "R1844", + "R1106" + ], + "R1844": [ + "R6028", + "R8751" + ], + "R6028": [ + "R1228", + "R1844" + ], + "R1228": [ + "R5351", + "R6028" + ], + "R5351": [ + "R1228", + "R3240" + ], + "R3240": [ + "R1412", + "R5351" + ], + "R1412": [ + "R1470", + "R3240" + ], + "R1470": [ + "R1412", + "R5328" + ], + "R5328": [ + "R1470", + "R2110" + ], + "R2110": [ + "R5328" + ], + "R9781": [ + "R4229", + "R1508" + ], + "R4229": [ + "R9707", + "R9781" + ], + "R9707": [ + "R4229", + "R1971" + ], + "R1971": [ + "R1720", + "R9707" + ], + "R1720": [ + "R3939", + "R1971" + ], + "R3939": [ + "R9999", + "R1720" + ], + "R9999": [ + "R9558", + "R3939" + ], + "R9558": [ + "R9999", + "R6068" + ], + "R6068": [ + "R3246", + "R9558" + ], + "R3246": [ + "R6068" + ], + "R9981": [ + "R7673", + "R1508" + ], + "R7673": [ + "R5271", + "R9981" + ], + "R5271": [ + "R7673", + "R5741" + ], + "R5741": [ + "R1633", + "R5271" + ], + "R1633": [ + "R3239", + "R5741" + ], + "R3239": [ + "R2203", + "R1633" + ], + "R2203": [ + "R3851", + "R3239" + ], + "R3851": [ + "R9219", + "R2203" + ], + "R9219": [ + "R3851", + "R4505" + ], + "R4505": [ + "R9219" + ], + "R6124": [ + "R1508", + "R4766" + ], + "R4766": [ + "R6744", + "R6124" + ], + "R6744": [ + "R4766", + "R9157" + ], + "R9157": [ + "R7928", + "R6744" + ], + "R7928": [ + "R9157", + "R2760" + ], + "R2760": [ + "R7928", + "R2850" + ], + "R2850": [ + "R2760", + "R7811" + ], + "R7811": [ + "R2850", + "R6214" + ], + "R6214": [ + "R7811", + "R3795" + ], + "R3795": [ + "R6214" + ] + } +} diff --git a/tasks/maze-t22/instruction.md b/tasks/maze-t22/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t22/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t22/solution/solve.sh b/tasks/maze-t22/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..4c89eee344ce9116d6ab7207d0e036d3bec05ae1 --- /dev/null +++ b/tasks/maze-t22/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R1275 R6115 R1857 R8338 R3068 R4512 R4002 R1172 R1170 R8382 R2410 R9183 R4297 R5582 R8441 R8052 R9972 R4935 R1882 R1106 R8751 R1844 R6028 R1228 R5351 R3240 R1412 R1470 R5328 R2110; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t22/task.toml b/tasks/maze-t22/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..9d70332b8ed0cb45c0d9cd0e4b3f4c25372a3e30 --- /dev/null +++ b/tasks/maze-t22/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t22" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 22 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t22/tests/Dockerfile b/tasks/maze-t22/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t22/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t22/tests/expected.json b/tasks/maze-t22/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..afb6beaeebc72acd048bac050c52528964d48171 --- /dev/null +++ b/tasks/maze-t22/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R5023", + "exit": "R2110", + "budget": 210, + "tree_sha256": "d2ff90432c2a2c7414d48846d1a2e21473ede7b365b202d8a9980a9230c67c42" +} diff --git a/tasks/maze-t22/tests/grade.py b/tasks/maze-t22/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t22/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t22/tests/test.sh b/tasks/maze-t22/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t22/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t23/environment/agent/Dockerfile b/tasks/maze-t23/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t23/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t23/environment/agent/maze b/tasks/maze-t23/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t23/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t23/environment/docker-compose.yaml b/tasks/maze-t23/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c6f22727d6d58c88f2bb5bf8115da6bf60f8f1d3 --- /dev/null +++ b/tasks/maze-t23/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t23: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t23-47342f540123 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t23/environment/maze/Dockerfile b/tasks/maze-t23/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t23/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t23/environment/maze/engine.py b/tasks/maze-t23/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t23/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t23/environment/maze/export.py b/tasks/maze-t23/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t23/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t23/environment/maze/service.py b/tasks/maze-t23/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t23/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t23/environment/maze/success.py b/tasks/maze-t23/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t23/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t23/environment/maze/tree.json b/tasks/maze-t23/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..33b8f72cac35f1ee19fcd929be3b7ea93f05e787 --- /dev/null +++ b/tasks/maze-t23/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R9442", + "exit": "R5935", + "budget": 210, + "doors": { + "R9442": [ + "R5294", + "R3113", + "R3798" + ], + "R3798": [ + "R9442", + "R2738" + ], + "R2738": [ + "R8677", + "R3798" + ], + "R8677": [ + "R1649", + "R2738" + ], + "R1649": [ + "R3827", + "R8677" + ], + "R3827": [ + "R1649", + "R9707" + ], + "R9707": [ + "R3827", + "R5796" + ], + "R5796": [ + "R9707", + "R2404" + ], + "R2404": [ + "R9849", + "R5796" + ], + "R9849": [ + "R2568", + "R2404" + ], + "R2568": [ + "R5136", + "R9849", + "R7937", + "R7628" + ], + "R5294": [ + "R9442", + "R3724" + ], + "R3724": [ + "R5294", + "R2038" + ], + "R2038": [ + "R7388", + "R3724" + ], + "R7388": [ + "R3425", + "R2038" + ], + "R3425": [ + "R3297", + "R7388" + ], + "R3297": [ + "R3425", + "R9124" + ], + "R9124": [ + "R7070", + "R3297" + ], + "R7070": [ + "R5891", + "R9124" + ], + "R5891": [ + "R7070", + "R8274" + ], + "R8274": [ + "R2377", + "R5891", + "R2872", + "R5823" + ], + "R3113": [ + "R6737", + "R9442" + ], + "R6737": [ + "R2112", + "R3113" + ], + "R2112": [ + "R1303", + "R6737" + ], + "R1303": [ + "R2112", + "R2990" + ], + "R2990": [ + "R4109", + "R1303" + ], + "R4109": [ + "R1524", + "R2990" + ], + "R1524": [ + "R6868", + "R4109" + ], + "R6868": [ + "R7332", + "R1524" + ], + "R7332": [ + "R5753", + "R6868" + ], + "R5753": [ + "R9374", + "R5220", + "R7332", + "R4523" + ], + "R7628": [ + "R9187", + "R2568" + ], + "R9187": [ + "R6277", + "R7628" + ], + "R6277": [ + "R4203", + "R9187" + ], + "R4203": [ + "R3589", + "R6277" + ], + "R3589": [ + "R4203", + "R2932" + ], + "R2932": [ + "R9766", + "R3589" + ], + "R9766": [ + "R4227", + "R2932" + ], + "R4227": [ + "R5662", + "R9766" + ], + "R5662": [ + "R4227", + "R7979" + ], + "R7979": [ + "R4595", + "R6199", + "R5662", + "R1300" + ], + "R5136": [ + "R2568", + "R5508" + ], + "R5508": [ + "R5733", + "R5136" + ], + "R5733": [ + "R3492", + "R5508" + ], + "R3492": [ + "R5865", + "R5733" + ], + "R5865": [ + "R3492", + "R2132" + ], + "R2132": [ + "R5865", + "R9036" + ], + "R9036": [ + "R2132", + "R2603" + ], + "R2603": [ + "R8585", + "R9036" + ], + "R8585": [ + "R5468", + "R2603" + ], + "R5468": [ + "R6118", + "R8585", + "R1124", + "R1793" + ], + "R7937": [ + "R2568", + "R7117" + ], + "R7117": [ + "R7937", + "R6324" + ], + "R6324": [ + "R7117", + "R7881" + ], + "R7881": [ + "R6324", + "R2318" + ], + "R2318": [ + "R8967", + "R7881" + ], + "R8967": [ + "R2318", + "R8031" + ], + "R8031": [ + "R8967", + "R3926" + ], + "R3926": [ + "R8031", + "R3396" + ], + "R3396": [ + "R3036", + "R3926" + ], + "R3036": [ + "R3396", + "R5756", + "R7480", + "R4548" + ], + "R2377": [ + "R8274", + "R2982" + ], + "R2982": [ + "R2330", + "R2377" + ], + "R2330": [ + "R2982", + "R8731" + ], + "R8731": [ + "R2330", + "R9542" + ], + "R9542": [ + "R8731", + "R7959" + ], + "R7959": [ + "R9542", + "R4789" + ], + "R4789": [ + "R7959", + "R8071" + ], + "R8071": [ + "R7663", + "R4789" + ], + "R7663": [ + "R9735", + "R8071" + ], + "R9735": [ + "R7928", + "R7663", + "R2077", + "R8471" + ], + "R2872": [ + "R8274", + "R7768" + ], + "R7768": [ + "R2872", + "R9365" + ], + "R9365": [ + "R7768", + "R3870" + ], + "R3870": [ + "R9365", + "R1250" + ], + "R1250": [ + "R3870", + "R2862" + ], + "R2862": [ + "R1250", + "R2327" + ], + "R2327": [ + "R2862", + "R5114" + ], + "R5114": [ + "R4011", + "R2327" + ], + "R4011": [ + "R5114", + "R3043" + ], + "R3043": [ + "R2246", + "R1670", + "R4011", + "R5349" + ], + "R5823": [ + "R8274", + "R3765" + ], + "R3765": [ + "R5823", + "R9419" + ], + "R9419": [ + "R6394", + "R3765" + ], + "R6394": [ + "R2160", + "R9419" + ], + "R2160": [ + "R9126", + "R6394" + ], + "R9126": [ + "R2160", + "R1555" + ], + "R1555": [ + "R1244", + "R9126" + ], + "R1244": [ + "R6580", + "R1555" + ], + "R6580": [ + "R1244", + "R9236" + ], + "R9236": [ + "R7343", + "R2775", + "R1492", + "R6580" + ], + "R5220": [ + "R5753", + "R8599" + ], + "R8599": [ + "R5220", + "R1233" + ], + "R1233": [ + "R8599", + "R8543" + ], + "R8543": [ + "R2563", + "R1233" + ], + "R2563": [ + "R7603", + "R8543" + ], + "R7603": [ + "R2563", + "R1833" + ], + "R1833": [ + "R7603", + "R7399" + ], + "R7399": [ + "R4310", + "R1833" + ], + "R4310": [ + "R6673", + "R7399" + ], + "R6673": [ + "R1421", + "R4310", + "R9951", + "R3236" + ], + "R9374": [ + "R5753", + "R8047" + ], + "R8047": [ + "R9374", + "R4319" + ], + "R4319": [ + "R8047", + "R8126" + ], + "R8126": [ + "R9626", + "R4319" + ], + "R9626": [ + "R7426", + "R8126" + ], + "R7426": [ + "R9626", + "R5008" + ], + "R5008": [ + "R7426", + "R4570" + ], + "R4570": [ + "R1031", + "R5008" + ], + "R1031": [ + "R5031", + "R4570" + ], + "R5031": [ + "R5016", + "R1031", + "R9153", + "R9428" + ], + "R4523": [ + "R5753", + "R3316" + ], + "R3316": [ + "R4523", + "R1972" + ], + "R1972": [ + "R3316", + "R7849" + ], + "R7849": [ + "R1972", + "R7306" + ], + "R7306": [ + "R8464", + "R7849" + ], + "R8464": [ + "R7069", + "R7306" + ], + "R7069": [ + "R8464", + "R7876" + ], + "R7876": [ + "R9531", + "R7069" + ], + "R9531": [ + "R9001", + "R7876" + ], + "R9001": [ + "R2310", + "R9390", + "R9531", + "R3369" + ], + "R4595": [ + "R7979", + "R4462" + ], + "R4462": [ + "R2838", + "R4595" + ], + "R2838": [ + "R4462", + "R9956" + ], + "R9956": [ + "R2838", + "R5655" + ], + "R5655": [ + "R9956", + "R9897" + ], + "R9897": [ + "R4528", + "R5655" + ], + "R4528": [ + "R3253", + "R9897" + ], + "R3253": [ + "R4528", + "R8212" + ], + "R8212": [ + "R3253", + "R7256" + ], + "R7256": [ + "R8212" + ], + "R1300": [ + "R7979", + "R8495" + ], + "R8495": [ + "R6719", + "R1300" + ], + "R6719": [ + "R7510", + "R8495" + ], + "R7510": [ + "R7822", + "R6719" + ], + "R7822": [ + "R7510", + "R7712" + ], + "R7712": [ + "R7822", + "R5127" + ], + "R5127": [ + "R7408", + "R7712" + ], + "R7408": [ + "R5127", + "R8858" + ], + "R8858": [ + "R4710", + "R7408" + ], + "R4710": [ + "R8858" + ], + "R6199": [ + "R6452", + "R7979" + ], + "R6452": [ + "R6199", + "R1213" + ], + "R1213": [ + "R6452", + "R9607" + ], + "R9607": [ + "R1213", + "R3882" + ], + "R3882": [ + "R9607", + "R1128" + ], + "R1128": [ + "R9219", + "R3882" + ], + "R9219": [ + "R4342", + "R1128" + ], + "R4342": [ + "R9219", + "R7529" + ], + "R7529": [ + "R5073", + "R4342" + ], + "R5073": [ + "R7529" + ], + "R6118": [ + "R3746", + "R5468" + ], + "R3746": [ + "R6118", + "R7307" + ], + "R7307": [ + "R3746", + "R9186" + ], + "R9186": [ + "R3988", + "R7307" + ], + "R3988": [ + "R9186", + "R5053" + ], + "R5053": [ + "R9675", + "R3988" + ], + "R9675": [ + "R5053", + "R6648" + ], + "R6648": [ + "R1130", + "R9675" + ], + "R1130": [ + "R6648", + "R5867" + ], + "R5867": [ + "R1130" + ], + "R1124": [ + "R5468", + "R4633" + ], + "R4633": [ + "R1124", + "R2395" + ], + "R2395": [ + "R4633", + "R2488" + ], + "R2488": [ + "R6013", + "R2395" + ], + "R6013": [ + "R2488", + "R9569" + ], + "R9569": [ + "R6081", + "R6013" + ], + "R6081": [ + "R9569", + "R5843" + ], + "R5843": [ + "R6081", + "R4541" + ], + "R4541": [ + "R5843", + "R5041" + ], + "R5041": [ + "R4541" + ], + "R1793": [ + "R5468", + "R2621" + ], + "R2621": [ + "R7614", + "R1793" + ], + "R7614": [ + "R3436", + "R2621" + ], + "R3436": [ + "R4954", + "R7614" + ], + "R4954": [ + "R3436", + "R4722" + ], + "R4722": [ + "R7695", + "R4954" + ], + "R7695": [ + "R4722", + "R3625" + ], + "R3625": [ + "R3160", + "R7695" + ], + "R3160": [ + "R5246", + "R3625" + ], + "R5246": [ + "R3160" + ], + "R7480": [ + "R2840", + "R3036" + ], + "R2840": [ + "R7480", + "R9404" + ], + "R9404": [ + "R2840", + "R6792" + ], + "R6792": [ + "R9404", + "R9441" + ], + "R9441": [ + "R5960", + "R6792" + ], + "R5960": [ + "R9441", + "R8866" + ], + "R8866": [ + "R5960", + "R3978" + ], + "R3978": [ + "R8866", + "R5604" + ], + "R5604": [ + "R5935", + "R3978" + ], + "R5935": [ + "R5604" + ], + "R4548": [ + "R3036", + "R9420" + ], + "R9420": [ + "R4912", + "R4548" + ], + "R4912": [ + "R9420", + "R7686" + ], + "R7686": [ + "R4912", + "R2631" + ], + "R2631": [ + "R7686", + "R8347" + ], + "R8347": [ + "R2416", + "R2631" + ], + "R2416": [ + "R4085", + "R8347" + ], + "R4085": [ + "R2416", + "R6687" + ], + "R6687": [ + "R4085", + "R6176" + ], + "R6176": [ + "R6687" + ], + "R5756": [ + "R3036", + "R8632" + ], + "R8632": [ + "R5756", + "R7543" + ], + "R7543": [ + "R8632", + "R5931" + ], + "R5931": [ + "R7543", + "R7636" + ], + "R7636": [ + "R8131", + "R5931" + ], + "R8131": [ + "R7636", + "R6079" + ], + "R6079": [ + "R2217", + "R8131" + ], + "R2217": [ + "R6079", + "R3177" + ], + "R3177": [ + "R7668", + "R2217" + ], + "R7668": [ + "R3177" + ], + "R2077": [ + "R3590", + "R9735" + ], + "R3590": [ + "R2077", + "R5759" + ], + "R5759": [ + "R1585", + "R3590" + ], + "R1585": [ + "R9878", + "R5759" + ], + "R9878": [ + "R1585", + "R2642" + ], + "R2642": [ + "R9878", + "R2520" + ], + "R2520": [ + "R2642", + "R6133" + ], + "R6133": [ + "R8749", + "R2520" + ], + "R8749": [ + "R3431", + "R6133" + ], + "R3431": [ + "R8749" + ], + "R8471": [ + "R6186", + "R9735" + ], + "R6186": [ + "R8471", + "R6672" + ], + "R6672": [ + "R6186", + "R8650" + ], + "R8650": [ + "R6672", + "R9691" + ], + "R9691": [ + "R4736", + "R8650" + ], + "R4736": [ + "R9691", + "R6773" + ], + "R6773": [ + "R4736", + "R8263" + ], + "R8263": [ + "R6773", + "R9776" + ], + "R9776": [ + "R8263", + "R4399" + ], + "R4399": [ + "R9776" + ], + "R7928": [ + "R9735", + "R9552" + ], + "R9552": [ + "R4794", + "R7928" + ], + "R4794": [ + "R8935", + "R9552" + ], + "R8935": [ + "R4794", + "R2845" + ], + "R2845": [ + "R8935", + "R9993" + ], + "R9993": [ + "R2845", + "R3128" + ], + "R3128": [ + "R9993", + "R9960" + ], + "R9960": [ + "R1977", + "R3128" + ], + "R1977": [ + "R9960", + "R8857" + ], + "R8857": [ + "R1977" + ], + "R1670": [ + "R4702", + "R3043" + ], + "R4702": [ + "R1670", + "R3806" + ], + "R3806": [ + "R4702", + "R2442" + ], + "R2442": [ + "R4440", + "R3806" + ], + "R4440": [ + "R2442", + "R4634" + ], + "R4634": [ + "R4440", + "R6914" + ], + "R6914": [ + "R7677", + "R4634" + ], + "R7677": [ + "R6914", + "R3895" + ], + "R3895": [ + "R1488", + "R7677" + ], + "R1488": [ + "R3895" + ], + "R5349": [ + "R5630", + "R3043" + ], + "R5630": [ + "R5349", + "R4303" + ], + "R4303": [ + "R4840", + "R5630" + ], + "R4840": [ + "R3145", + "R4303" + ], + "R3145": [ + "R1197", + "R4840" + ], + "R1197": [ + "R3145", + "R6415" + ], + "R6415": [ + "R1338", + "R1197" + ], + "R1338": [ + "R3985", + "R6415" + ], + "R3985": [ + "R1320", + "R1338" + ], + "R1320": [ + "R3985" + ], + "R2246": [ + "R7505", + "R3043" + ], + "R7505": [ + "R1446", + "R2246" + ], + "R1446": [ + "R7505", + "R7887" + ], + "R7887": [ + "R2045", + "R1446" + ], + "R2045": [ + "R7887", + "R4457" + ], + "R4457": [ + "R2045", + "R5205" + ], + "R5205": [ + "R5072", + "R4457" + ], + "R5072": [ + "R6858", + "R5205" + ], + "R6858": [ + "R7604", + "R5072" + ], + "R7604": [ + "R6858" + ], + "R1492": [ + "R9236", + "R8869" + ], + "R8869": [ + "R1492", + "R7703" + ], + "R7703": [ + "R4193", + "R8869" + ], + "R4193": [ + "R7703", + "R5927" + ], + "R5927": [ + "R4193", + "R5621" + ], + "R5621": [ + "R5927", + "R5176" + ], + "R5176": [ + "R6805", + "R5621" + ], + "R6805": [ + "R6995", + "R5176" + ], + "R6995": [ + "R6805", + "R7330" + ], + "R7330": [ + "R6995" + ], + "R2775": [ + "R9236", + "R9344" + ], + "R9344": [ + "R2775", + "R2016" + ], + "R2016": [ + "R5254", + "R9344" + ], + "R5254": [ + "R2016", + "R2703" + ], + "R2703": [ + "R4611", + "R5254" + ], + "R4611": [ + "R9679", + "R2703" + ], + "R9679": [ + "R4611", + "R7783" + ], + "R7783": [ + "R1611", + "R9679" + ], + "R1611": [ + "R4323", + "R7783" + ], + "R4323": [ + "R1611" + ], + "R7343": [ + "R6174", + "R9236" + ], + "R6174": [ + "R3908", + "R7343" + ], + "R3908": [ + "R6174", + "R2881" + ], + "R2881": [ + "R3908", + "R4493" + ], + "R4493": [ + "R2881", + "R7082" + ], + "R7082": [ + "R4493", + "R3491" + ], + "R3491": [ + "R7082", + "R3086" + ], + "R3086": [ + "R1678", + "R3491" + ], + "R1678": [ + "R3086", + "R2717" + ], + "R2717": [ + "R1678" + ], + "R3236": [ + "R3122", + "R6673" + ], + "R3122": [ + "R7492", + "R3236" + ], + "R7492": [ + "R3122", + "R8812" + ], + "R8812": [ + "R7492", + "R5307" + ], + "R5307": [ + "R3634", + "R8812" + ], + "R3634": [ + "R7521", + "R5307" + ], + "R7521": [ + "R3634", + "R1276" + ], + "R1276": [ + "R7521", + "R1041" + ], + "R1041": [ + "R1276", + "R8509" + ], + "R8509": [ + "R1041" + ], + "R1421": [ + "R6673", + "R4607" + ], + "R4607": [ + "R1421", + "R4063" + ], + "R4063": [ + "R4607", + "R8362" + ], + "R8362": [ + "R7445", + "R4063" + ], + "R7445": [ + "R8362", + "R8310" + ], + "R8310": [ + "R7445", + "R5135" + ], + "R5135": [ + "R8310", + "R7107" + ], + "R7107": [ + "R5135", + "R7422" + ], + "R7422": [ + "R7107", + "R4096" + ], + "R4096": [ + "R7422" + ], + "R9951": [ + "R6673", + "R8695" + ], + "R8695": [ + "R9951", + "R8845" + ], + "R8845": [ + "R7734", + "R8695" + ], + "R7734": [ + "R8845", + "R5421" + ], + "R5421": [ + "R6104", + "R7734" + ], + "R6104": [ + "R3844", + "R5421" + ], + "R3844": [ + "R6104", + "R5222" + ], + "R5222": [ + "R5863", + "R3844" + ], + "R5863": [ + "R5222", + "R7162" + ], + "R7162": [ + "R5863" + ], + "R9153": [ + "R5031", + "R4796" + ], + "R4796": [ + "R8887", + "R9153" + ], + "R8887": [ + "R3149", + "R4796" + ], + "R3149": [ + "R8887", + "R5242" + ], + "R5242": [ + "R3149", + "R1089" + ], + "R1089": [ + "R5242", + "R4052" + ], + "R4052": [ + "R1089", + "R6827" + ], + "R6827": [ + "R9272", + "R4052" + ], + "R9272": [ + "R7046", + "R6827" + ], + "R7046": [ + "R9272" + ], + "R9428": [ + "R5031", + "R6050" + ], + "R6050": [ + "R2676", + "R9428" + ], + "R2676": [ + "R6050", + "R5497" + ], + "R5497": [ + "R3161", + "R2676" + ], + "R3161": [ + "R5497", + "R1228" + ], + "R1228": [ + "R2353", + "R3161" + ], + "R2353": [ + "R1228", + "R3661" + ], + "R3661": [ + "R2353", + "R5861" + ], + "R5861": [ + "R8689", + "R3661" + ], + "R8689": [ + "R5861" + ], + "R5016": [ + "R8348", + "R5031" + ], + "R8348": [ + "R5016", + "R2126" + ], + "R2126": [ + "R9181", + "R8348" + ], + "R9181": [ + "R2126", + "R7000" + ], + "R7000": [ + "R9215", + "R9181" + ], + "R9215": [ + "R9551", + "R7000" + ], + "R9551": [ + "R9215", + "R1037" + ], + "R1037": [ + "R9551", + "R3891" + ], + "R3891": [ + "R1037", + "R8925" + ], + "R8925": [ + "R3891" + ], + "R2310": [ + "R9001", + "R4112" + ], + "R4112": [ + "R3866", + "R2310" + ], + "R3866": [ + "R9301", + "R4112" + ], + "R9301": [ + "R1466", + "R3866" + ], + "R1466": [ + "R3018", + "R9301" + ], + "R3018": [ + "R1466", + "R4997" + ], + "R4997": [ + "R3018", + "R7909" + ], + "R7909": [ + "R9988", + "R4997" + ], + "R9988": [ + "R7909", + "R3751" + ], + "R3751": [ + "R9988" + ], + "R9390": [ + "R9155", + "R9001" + ], + "R9155": [ + "R9130", + "R9390" + ], + "R9130": [ + "R9155", + "R3755" + ], + "R3755": [ + "R9130", + "R6711" + ], + "R6711": [ + "R2947", + "R3755" + ], + "R2947": [ + "R6711", + "R4216" + ], + "R4216": [ + "R2947", + "R2544" + ], + "R2544": [ + "R6257", + "R4216" + ], + "R6257": [ + "R2354", + "R2544" + ], + "R2354": [ + "R6257" + ], + "R3369": [ + "R9001", + "R6289" + ], + "R6289": [ + "R3369", + "R8142" + ], + "R8142": [ + "R1547", + "R6289" + ], + "R1547": [ + "R2969", + "R8142" + ], + "R2969": [ + "R7548", + "R1547" + ], + "R7548": [ + "R3721", + "R2969" + ], + "R3721": [ + "R7548", + "R7079" + ], + "R7079": [ + "R3721", + "R6545" + ], + "R6545": [ + "R3032", + "R7079" + ], + "R3032": [ + "R6545" + ] + } +} diff --git a/tasks/maze-t23/instruction.md b/tasks/maze-t23/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t23/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t23/solution/solve.sh b/tasks/maze-t23/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..bdd0d37bc01d5df4b6ddc830da4e790e00af6766 --- /dev/null +++ b/tasks/maze-t23/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R3798 R2738 R8677 R1649 R3827 R9707 R5796 R2404 R9849 R2568 R7937 R7117 R6324 R7881 R2318 R8967 R8031 R3926 R3396 R3036 R7480 R2840 R9404 R6792 R9441 R5960 R8866 R3978 R5604 R5935; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t23/task.toml b/tasks/maze-t23/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..bb945479356d2fb4c0cf8ff27bd9833cbe4c18ad --- /dev/null +++ b/tasks/maze-t23/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t23" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 23 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t23/tests/Dockerfile b/tasks/maze-t23/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t23/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t23/tests/expected.json b/tasks/maze-t23/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..16d3d924e70fb138c407e32ad7658c4e8ecd3380 --- /dev/null +++ b/tasks/maze-t23/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R9442", + "exit": "R5935", + "budget": 210, + "tree_sha256": "edbf790716167993f4001709841578cf520757c4246391094da6f8d27be8d1bb" +} diff --git a/tasks/maze-t23/tests/grade.py b/tasks/maze-t23/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t23/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t23/tests/test.sh b/tasks/maze-t23/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t23/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t24/environment/agent/Dockerfile b/tasks/maze-t24/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t24/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t24/environment/agent/maze b/tasks/maze-t24/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t24/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t24/environment/docker-compose.yaml b/tasks/maze-t24/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..59bd9e60b46a9e4fdc1e484c8bb228c4b8cc36c2 --- /dev/null +++ b/tasks/maze-t24/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t24: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t24-4e2efe849b49 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t24/environment/maze/Dockerfile b/tasks/maze-t24/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t24/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t24/environment/maze/engine.py b/tasks/maze-t24/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t24/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t24/environment/maze/export.py b/tasks/maze-t24/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t24/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t24/environment/maze/service.py b/tasks/maze-t24/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t24/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t24/environment/maze/success.py b/tasks/maze-t24/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t24/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t24/environment/maze/tree.json b/tasks/maze-t24/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..97327d211ea7e84469e945227ca3e9e1d102cd35 --- /dev/null +++ b/tasks/maze-t24/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R2077", + "exit": "R5618", + "budget": 210, + "doors": { + "R2077": [ + "R7129", + "R6990", + "R6480" + ], + "R7129": [ + "R9853", + "R2077" + ], + "R9853": [ + "R2221", + "R7129" + ], + "R2221": [ + "R9853", + "R2254" + ], + "R2254": [ + "R8080", + "R2221" + ], + "R8080": [ + "R2254", + "R8297" + ], + "R8297": [ + "R8080", + "R9395" + ], + "R9395": [ + "R8297", + "R3213" + ], + "R3213": [ + "R9395", + "R5860" + ], + "R5860": [ + "R3213", + "R4581" + ], + "R4581": [ + "R7738", + "R6822", + "R8572", + "R5860" + ], + "R6990": [ + "R4027", + "R2077" + ], + "R4027": [ + "R6990", + "R4147" + ], + "R4147": [ + "R6532", + "R4027" + ], + "R6532": [ + "R5756", + "R4147" + ], + "R5756": [ + "R9913", + "R6532" + ], + "R9913": [ + "R6223", + "R5756" + ], + "R6223": [ + "R9913", + "R4047" + ], + "R4047": [ + "R8044", + "R6223" + ], + "R8044": [ + "R6992", + "R4047" + ], + "R6992": [ + "R4149", + "R5834", + "R2065", + "R8044" + ], + "R6480": [ + "R3100", + "R2077" + ], + "R3100": [ + "R5094", + "R6480" + ], + "R5094": [ + "R6315", + "R3100" + ], + "R6315": [ + "R5094", + "R2279" + ], + "R2279": [ + "R3430", + "R6315" + ], + "R3430": [ + "R2279", + "R9754" + ], + "R9754": [ + "R3430", + "R8286" + ], + "R8286": [ + "R3259", + "R9754" + ], + "R3259": [ + "R5535", + "R8286" + ], + "R5535": [ + "R6037", + "R7284", + "R5714", + "R3259" + ], + "R6822": [ + "R9968", + "R4581" + ], + "R9968": [ + "R6822", + "R3684" + ], + "R3684": [ + "R9968", + "R3423" + ], + "R3423": [ + "R3684", + "R7995" + ], + "R7995": [ + "R3423", + "R6859" + ], + "R6859": [ + "R1390", + "R7995" + ], + "R1390": [ + "R6859", + "R8975" + ], + "R8975": [ + "R1390", + "R4997" + ], + "R4997": [ + "R8975", + "R4095" + ], + "R4095": [ + "R4455", + "R7399", + "R8209", + "R4997" + ], + "R7738": [ + "R4581", + "R8619" + ], + "R8619": [ + "R6936", + "R7738" + ], + "R6936": [ + "R8619", + "R8101" + ], + "R8101": [ + "R6936", + "R2207" + ], + "R2207": [ + "R7158", + "R8101" + ], + "R7158": [ + "R2207", + "R9812" + ], + "R9812": [ + "R7158", + "R1721" + ], + "R1721": [ + "R9812", + "R9007" + ], + "R9007": [ + "R6159", + "R1721" + ], + "R6159": [ + "R3541", + "R8993", + "R9007", + "R4218" + ], + "R8572": [ + "R6251", + "R4581" + ], + "R6251": [ + "R4608", + "R8572" + ], + "R4608": [ + "R4846", + "R6251" + ], + "R4846": [ + "R4608", + "R1677" + ], + "R1677": [ + "R4846", + "R7173" + ], + "R7173": [ + "R2730", + "R1677" + ], + "R2730": [ + "R7173", + "R9695" + ], + "R9695": [ + "R2730", + "R4862" + ], + "R4862": [ + "R9695", + "R1809" + ], + "R1809": [ + "R6381", + "R2720", + "R1044", + "R4862" + ], + "R5834": [ + "R2977", + "R6992" + ], + "R2977": [ + "R5834", + "R2864" + ], + "R2864": [ + "R2977", + "R3842" + ], + "R3842": [ + "R7494", + "R2864" + ], + "R7494": [ + "R8679", + "R3842" + ], + "R8679": [ + "R7494", + "R8507" + ], + "R8507": [ + "R6529", + "R8679" + ], + "R6529": [ + "R8507", + "R4951" + ], + "R4951": [ + "R7418", + "R6529" + ], + "R7418": [ + "R6005", + "R4986", + "R4951", + "R5407" + ], + "R2065": [ + "R2533", + "R6992" + ], + "R2533": [ + "R3044", + "R2065" + ], + "R3044": [ + "R2533", + "R3797" + ], + "R3797": [ + "R3044", + "R5879" + ], + "R5879": [ + "R3797", + "R8697" + ], + "R8697": [ + "R7622", + "R5879" + ], + "R7622": [ + "R7647", + "R8697" + ], + "R7647": [ + "R7622", + "R7260" + ], + "R7260": [ + "R7647", + "R2006" + ], + "R2006": [ + "R5464", + "R2691", + "R7261", + "R7260" + ], + "R4149": [ + "R6992", + "R2001" + ], + "R2001": [ + "R4149", + "R4262" + ], + "R4262": [ + "R2001", + "R2177" + ], + "R2177": [ + "R3706", + "R4262" + ], + "R3706": [ + "R2177", + "R8985" + ], + "R8985": [ + "R3706", + "R3483" + ], + "R3483": [ + "R8985", + "R6310" + ], + "R6310": [ + "R3483", + "R9944" + ], + "R9944": [ + "R6310", + "R4854" + ], + "R4854": [ + "R9944", + "R4786", + "R6042", + "R4885" + ], + "R6037": [ + "R3838", + "R5535" + ], + "R3838": [ + "R2354", + "R6037" + ], + "R2354": [ + "R3289", + "R3838" + ], + "R3289": [ + "R8690", + "R2354" + ], + "R8690": [ + "R3289", + "R9491" + ], + "R9491": [ + "R7064", + "R8690" + ], + "R7064": [ + "R8061", + "R9491" + ], + "R8061": [ + "R7064", + "R4061" + ], + "R4061": [ + "R8061", + "R4830" + ], + "R4830": [ + "R1268", + "R4061", + "R1523", + "R5232" + ], + "R5714": [ + "R5535", + "R5025" + ], + "R5025": [ + "R9264", + "R5714" + ], + "R9264": [ + "R4307", + "R5025" + ], + "R4307": [ + "R8874", + "R9264" + ], + "R8874": [ + "R4485", + "R4307" + ], + "R4485": [ + "R8874", + "R1548" + ], + "R1548": [ + "R4485", + "R6590" + ], + "R6590": [ + "R1548", + "R9104" + ], + "R9104": [ + "R3312", + "R6590" + ], + "R3312": [ + "R4274", + "R4779", + "R5230", + "R9104" + ], + "R7284": [ + "R5535", + "R6555" + ], + "R6555": [ + "R3328", + "R7284" + ], + "R3328": [ + "R6555", + "R3879" + ], + "R3879": [ + "R9830", + "R3328" + ], + "R9830": [ + "R8129", + "R3879" + ], + "R8129": [ + "R9830", + "R8081" + ], + "R8081": [ + "R8984", + "R8129" + ], + "R8984": [ + "R8081", + "R5547" + ], + "R5547": [ + "R8984", + "R1321" + ], + "R1321": [ + "R5509", + "R5671", + "R6117", + "R5547" + ], + "R7399": [ + "R4095", + "R3357" + ], + "R3357": [ + "R7399", + "R1490" + ], + "R1490": [ + "R3357", + "R6894" + ], + "R6894": [ + "R5032", + "R1490" + ], + "R5032": [ + "R9032", + "R6894" + ], + "R9032": [ + "R5032", + "R2644" + ], + "R2644": [ + "R7198", + "R9032" + ], + "R7198": [ + "R6455", + "R2644" + ], + "R6455": [ + "R6686", + "R7198" + ], + "R6686": [ + "R6455" + ], + "R4455": [ + "R4095", + "R1083" + ], + "R1083": [ + "R2867", + "R4455" + ], + "R2867": [ + "R7894", + "R1083" + ], + "R7894": [ + "R5216", + "R2867" + ], + "R5216": [ + "R8356", + "R7894" + ], + "R8356": [ + "R5216", + "R7441" + ], + "R7441": [ + "R8264", + "R8356" + ], + "R8264": [ + "R8585", + "R7441" + ], + "R8585": [ + "R8264", + "R9354" + ], + "R9354": [ + "R8585" + ], + "R8209": [ + "R4095", + "R7982" + ], + "R7982": [ + "R8209", + "R3989" + ], + "R3989": [ + "R7982", + "R7847" + ], + "R7847": [ + "R3989", + "R6313" + ], + "R6313": [ + "R7847", + "R5165" + ], + "R5165": [ + "R6313", + "R6022" + ], + "R6022": [ + "R8926", + "R5165" + ], + "R8926": [ + "R6022", + "R1652" + ], + "R1652": [ + "R1012", + "R8926" + ], + "R1012": [ + "R1652" + ], + "R4218": [ + "R6159", + "R9195" + ], + "R9195": [ + "R4218", + "R4003" + ], + "R4003": [ + "R8350", + "R9195" + ], + "R8350": [ + "R6745", + "R4003" + ], + "R6745": [ + "R8350", + "R4506" + ], + "R4506": [ + "R6745", + "R6048" + ], + "R6048": [ + "R4506", + "R6769" + ], + "R6769": [ + "R6048", + "R3825" + ], + "R3825": [ + "R6769", + "R6586" + ], + "R6586": [ + "R3825" + ], + "R3541": [ + "R6159", + "R2472" + ], + "R2472": [ + "R3402", + "R3541" + ], + "R3402": [ + "R2472", + "R5807" + ], + "R5807": [ + "R4169", + "R3402" + ], + "R4169": [ + "R5807", + "R2463" + ], + "R2463": [ + "R4169", + "R8524" + ], + "R8524": [ + "R8257", + "R2463" + ], + "R8257": [ + "R8524", + "R4821" + ], + "R4821": [ + "R8257", + "R8632" + ], + "R8632": [ + "R4821" + ], + "R8993": [ + "R6159", + "R9084" + ], + "R9084": [ + "R2484", + "R8993" + ], + "R2484": [ + "R2294", + "R9084" + ], + "R2294": [ + "R2484", + "R5712" + ], + "R5712": [ + "R2294", + "R7877" + ], + "R7877": [ + "R5712", + "R3224" + ], + "R3224": [ + "R8893", + "R7877" + ], + "R8893": [ + "R3224", + "R2478" + ], + "R2478": [ + "R8893", + "R3942" + ], + "R3942": [ + "R2478" + ], + "R6381": [ + "R8795", + "R1809" + ], + "R8795": [ + "R6381", + "R5997" + ], + "R5997": [ + "R8795", + "R1104" + ], + "R1104": [ + "R5997", + "R4579" + ], + "R4579": [ + "R1104", + "R6138" + ], + "R6138": [ + "R1307", + "R4579" + ], + "R1307": [ + "R6138", + "R6032" + ], + "R6032": [ + "R1307", + "R9809" + ], + "R9809": [ + "R3939", + "R6032" + ], + "R3939": [ + "R9809" + ], + "R2720": [ + "R5453", + "R1809" + ], + "R5453": [ + "R8368", + "R2720" + ], + "R8368": [ + "R4554", + "R5453" + ], + "R4554": [ + "R1689", + "R8368" + ], + "R1689": [ + "R2457", + "R4554" + ], + "R2457": [ + "R3441", + "R1689" + ], + "R3441": [ + "R4428", + "R2457" + ], + "R4428": [ + "R7259", + "R3441" + ], + "R7259": [ + "R8655", + "R4428" + ], + "R8655": [ + "R7259" + ], + "R1044": [ + "R1809", + "R7032" + ], + "R7032": [ + "R1044", + "R7328" + ], + "R7328": [ + "R7628", + "R7032" + ], + "R7628": [ + "R7328", + "R3646" + ], + "R3646": [ + "R1782", + "R7628" + ], + "R1782": [ + "R3646", + "R7050" + ], + "R7050": [ + "R1782", + "R4719" + ], + "R4719": [ + "R7050", + "R9586" + ], + "R9586": [ + "R4719", + "R1807" + ], + "R1807": [ + "R9586" + ], + "R4986": [ + "R7418", + "R6065" + ], + "R6065": [ + "R4986", + "R4373" + ], + "R4373": [ + "R3959", + "R6065" + ], + "R3959": [ + "R5561", + "R4373" + ], + "R5561": [ + "R6317", + "R3959" + ], + "R6317": [ + "R6156", + "R5561" + ], + "R6156": [ + "R6317", + "R7012" + ], + "R7012": [ + "R6156", + "R1309" + ], + "R1309": [ + "R4388", + "R7012" + ], + "R4388": [ + "R1309" + ], + "R6005": [ + "R7418", + "R6649" + ], + "R6649": [ + "R6005", + "R9103" + ], + "R9103": [ + "R7348", + "R6649" + ], + "R7348": [ + "R3315", + "R9103" + ], + "R3315": [ + "R9927", + "R7348" + ], + "R9927": [ + "R5818", + "R3315" + ], + "R5818": [ + "R2255", + "R9927" + ], + "R2255": [ + "R5818", + "R8292" + ], + "R8292": [ + "R2255", + "R9204" + ], + "R9204": [ + "R8292" + ], + "R5407": [ + "R3925", + "R7418" + ], + "R3925": [ + "R3336", + "R5407" + ], + "R3336": [ + "R3925", + "R5754" + ], + "R5754": [ + "R3619", + "R3336" + ], + "R3619": [ + "R5754", + "R1536" + ], + "R1536": [ + "R3619", + "R4354" + ], + "R4354": [ + "R1536", + "R1445" + ], + "R1445": [ + "R9283", + "R4354" + ], + "R9283": [ + "R8657", + "R1445" + ], + "R8657": [ + "R9283" + ], + "R7261": [ + "R4999", + "R2006" + ], + "R4999": [ + "R3356", + "R7261" + ], + "R3356": [ + "R4999", + "R7059" + ], + "R7059": [ + "R3356", + "R8138" + ], + "R8138": [ + "R7567", + "R7059" + ], + "R7567": [ + "R2081", + "R8138" + ], + "R2081": [ + "R7567", + "R2512" + ], + "R2512": [ + "R7455", + "R2081" + ], + "R7455": [ + "R6021", + "R2512" + ], + "R6021": [ + "R7455" + ], + "R2691": [ + "R2006", + "R1145" + ], + "R1145": [ + "R3252", + "R2691" + ], + "R3252": [ + "R7258", + "R1145" + ], + "R7258": [ + "R1288", + "R3252" + ], + "R1288": [ + "R2905", + "R7258" + ], + "R2905": [ + "R1288", + "R3113" + ], + "R3113": [ + "R2905", + "R7713" + ], + "R7713": [ + "R9883", + "R3113" + ], + "R9883": [ + "R7654", + "R7713" + ], + "R7654": [ + "R9883" + ], + "R5464": [ + "R5922", + "R2006" + ], + "R5922": [ + "R7661", + "R5464" + ], + "R7661": [ + "R8769", + "R5922" + ], + "R8769": [ + "R7661", + "R5188" + ], + "R5188": [ + "R2532", + "R8769" + ], + "R2532": [ + "R3420", + "R5188" + ], + "R3420": [ + "R2532", + "R6931" + ], + "R6931": [ + "R3420", + "R9160" + ], + "R9160": [ + "R5862", + "R6931" + ], + "R5862": [ + "R9160" + ], + "R6042": [ + "R8404", + "R4854" + ], + "R8404": [ + "R6042", + "R9187" + ], + "R9187": [ + "R8404", + "R9496" + ], + "R9496": [ + "R9187", + "R3158" + ], + "R3158": [ + "R9381", + "R9496" + ], + "R9381": [ + "R4198", + "R3158" + ], + "R4198": [ + "R9381", + "R3419" + ], + "R3419": [ + "R2860", + "R4198" + ], + "R2860": [ + "R6688", + "R3419" + ], + "R6688": [ + "R2860" + ], + "R4786": [ + "R8016", + "R4854" + ], + "R8016": [ + "R3398", + "R4786" + ], + "R3398": [ + "R9013", + "R8016" + ], + "R9013": [ + "R3398", + "R2450" + ], + "R2450": [ + "R5229", + "R9013" + ], + "R5229": [ + "R5187", + "R2450" + ], + "R5187": [ + "R5229", + "R2044" + ], + "R2044": [ + "R5187", + "R9480" + ], + "R9480": [ + "R5704", + "R2044" + ], + "R5704": [ + "R9480" + ], + "R4885": [ + "R5254", + "R4854" + ], + "R5254": [ + "R4885", + "R1544" + ], + "R1544": [ + "R6462", + "R5254" + ], + "R6462": [ + "R1544", + "R5001" + ], + "R5001": [ + "R6462", + "R2569" + ], + "R2569": [ + "R7619", + "R5001" + ], + "R7619": [ + "R4963", + "R2569" + ], + "R4963": [ + "R2075", + "R7619" + ], + "R2075": [ + "R2847", + "R4963" + ], + "R2847": [ + "R2075" + ], + "R5232": [ + "R2515", + "R4830" + ], + "R2515": [ + "R5232", + "R4613" + ], + "R4613": [ + "R2515", + "R8758" + ], + "R8758": [ + "R4613", + "R2620" + ], + "R2620": [ + "R1486", + "R8758" + ], + "R1486": [ + "R7435", + "R2620" + ], + "R7435": [ + "R1486", + "R3365" + ], + "R3365": [ + "R1380", + "R7435" + ], + "R1380": [ + "R3365", + "R3396" + ], + "R3396": [ + "R1380" + ], + "R1523": [ + "R5260", + "R4830" + ], + "R5260": [ + "R1523", + "R4591" + ], + "R4591": [ + "R6677", + "R5260" + ], + "R6677": [ + "R3663", + "R4591" + ], + "R3663": [ + "R6677", + "R8699" + ], + "R8699": [ + "R3663", + "R6516" + ], + "R6516": [ + "R8699", + "R3016" + ], + "R3016": [ + "R9924", + "R6516" + ], + "R9924": [ + "R3016", + "R2269" + ], + "R2269": [ + "R9924" + ], + "R1268": [ + "R7400", + "R4830" + ], + "R7400": [ + "R9367", + "R1268" + ], + "R9367": [ + "R7400", + "R1203" + ], + "R1203": [ + "R8718", + "R9367" + ], + "R8718": [ + "R9459", + "R1203" + ], + "R9459": [ + "R8718", + "R4883" + ], + "R4883": [ + "R9459", + "R4156" + ], + "R4156": [ + "R4883", + "R3997" + ], + "R3997": [ + "R4182", + "R4156" + ], + "R4182": [ + "R3997" + ], + "R4779": [ + "R3312", + "R9441" + ], + "R9441": [ + "R1586", + "R4779" + ], + "R1586": [ + "R3032", + "R9441" + ], + "R3032": [ + "R3473", + "R1586" + ], + "R3473": [ + "R5173", + "R3032" + ], + "R5173": [ + "R3473", + "R4653" + ], + "R4653": [ + "R6146", + "R5173" + ], + "R6146": [ + "R4653", + "R5687" + ], + "R5687": [ + "R6146", + "R1796" + ], + "R1796": [ + "R5687" + ], + "R5230": [ + "R3312", + "R3132" + ], + "R3132": [ + "R7253", + "R5230" + ], + "R7253": [ + "R6930", + "R3132" + ], + "R6930": [ + "R7253", + "R8477" + ], + "R8477": [ + "R5825", + "R6930" + ], + "R5825": [ + "R8477", + "R9732" + ], + "R9732": [ + "R5825", + "R3427" + ], + "R3427": [ + "R8560", + "R9732" + ], + "R8560": [ + "R3427", + "R4971" + ], + "R4971": [ + "R8560" + ], + "R4274": [ + "R3312", + "R1451" + ], + "R1451": [ + "R1904", + "R4274" + ], + "R1904": [ + "R9680", + "R1451" + ], + "R9680": [ + "R1904", + "R1455" + ], + "R1455": [ + "R5898", + "R9680" + ], + "R5898": [ + "R1455", + "R4476" + ], + "R4476": [ + "R7925", + "R5898" + ], + "R7925": [ + "R2857", + "R4476" + ], + "R2857": [ + "R7925", + "R8854" + ], + "R8854": [ + "R2857" + ], + "R5509": [ + "R5565", + "R1321" + ], + "R5565": [ + "R5509", + "R8169" + ], + "R8169": [ + "R5117", + "R5565" + ], + "R5117": [ + "R7518", + "R8169" + ], + "R7518": [ + "R9753", + "R5117" + ], + "R9753": [ + "R6780", + "R7518" + ], + "R6780": [ + "R9753", + "R8606" + ], + "R8606": [ + "R6780", + "R6595" + ], + "R6595": [ + "R2190", + "R8606" + ], + "R2190": [ + "R6595" + ], + "R6117": [ + "R8071", + "R1321" + ], + "R8071": [ + "R6117", + "R9030" + ], + "R9030": [ + "R5978", + "R8071" + ], + "R5978": [ + "R9766", + "R9030" + ], + "R9766": [ + "R5978", + "R9324" + ], + "R9324": [ + "R9766", + "R5240" + ], + "R5240": [ + "R8042", + "R9324" + ], + "R8042": [ + "R5240", + "R6046" + ], + "R6046": [ + "R5618", + "R8042" + ], + "R5618": [ + "R6046" + ], + "R5671": [ + "R6085", + "R1321" + ], + "R6085": [ + "R2260", + "R5671" + ], + "R2260": [ + "R6085", + "R3316" + ], + "R3316": [ + "R2114", + "R2260" + ], + "R2114": [ + "R5764", + "R3316" + ], + "R5764": [ + "R2114", + "R8079" + ], + "R8079": [ + "R5764", + "R1829" + ], + "R1829": [ + "R8319", + "R8079" + ], + "R8319": [ + "R9595", + "R1829" + ], + "R9595": [ + "R8319" + ] + } +} diff --git a/tasks/maze-t24/instruction.md b/tasks/maze-t24/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t24/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t24/solution/solve.sh b/tasks/maze-t24/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..fb60ccc1cdf3a03af17fe67bfa5063892e0ca8ec --- /dev/null +++ b/tasks/maze-t24/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R6480 R3100 R5094 R6315 R2279 R3430 R9754 R8286 R3259 R5535 R7284 R6555 R3328 R3879 R9830 R8129 R8081 R8984 R5547 R1321 R6117 R8071 R9030 R5978 R9766 R9324 R5240 R8042 R6046 R5618; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t24/task.toml b/tasks/maze-t24/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..6ea60034db4dc8d6df592f57e5df9fe4a0f14957 --- /dev/null +++ b/tasks/maze-t24/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t24" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 24 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t24/tests/Dockerfile b/tasks/maze-t24/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t24/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t24/tests/expected.json b/tasks/maze-t24/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..0cfaddd0260622cd1592581198e2e9d3041b25dc --- /dev/null +++ b/tasks/maze-t24/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R2077", + "exit": "R5618", + "budget": 210, + "tree_sha256": "2eb89a90cfdcde3419189526c475cab59fd4f9746376552088e4ab1a65986eb2" +} diff --git a/tasks/maze-t24/tests/grade.py b/tasks/maze-t24/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t24/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t24/tests/test.sh b/tasks/maze-t24/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t24/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t25/environment/agent/Dockerfile b/tasks/maze-t25/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t25/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t25/environment/agent/maze b/tasks/maze-t25/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t25/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t25/environment/docker-compose.yaml b/tasks/maze-t25/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dd40a5f77bf317ac8f60ddc16b9e4f2a5de094b0 --- /dev/null +++ b/tasks/maze-t25/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t25: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t25-ad91e9128e69 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t25/environment/maze/Dockerfile b/tasks/maze-t25/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t25/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t25/environment/maze/engine.py b/tasks/maze-t25/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t25/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t25/environment/maze/export.py b/tasks/maze-t25/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t25/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t25/environment/maze/service.py b/tasks/maze-t25/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t25/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t25/environment/maze/success.py b/tasks/maze-t25/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t25/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t25/environment/maze/tree.json b/tasks/maze-t25/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..e492677ac585d6772867b4a1123ed7b8e10da885 --- /dev/null +++ b/tasks/maze-t25/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R5912", + "exit": "R7677", + "budget": 210, + "doors": { + "R5912": [ + "R7460", + "R3649", + "R2340" + ], + "R2340": [ + "R9784", + "R5912" + ], + "R9784": [ + "R2340", + "R7307" + ], + "R7307": [ + "R9784", + "R6661" + ], + "R6661": [ + "R4280", + "R7307" + ], + "R4280": [ + "R6661", + "R7889" + ], + "R7889": [ + "R4280", + "R3613" + ], + "R3613": [ + "R8434", + "R7889" + ], + "R8434": [ + "R9901", + "R3613" + ], + "R9901": [ + "R3380", + "R8434" + ], + "R3380": [ + "R8925", + "R9901", + "R8981", + "R2128" + ], + "R7460": [ + "R5912", + "R6102" + ], + "R6102": [ + "R7460", + "R3017" + ], + "R3017": [ + "R6102", + "R6852" + ], + "R6852": [ + "R2811", + "R3017" + ], + "R2811": [ + "R4127", + "R6852" + ], + "R4127": [ + "R9942", + "R2811" + ], + "R9942": [ + "R4127", + "R3761" + ], + "R3761": [ + "R2935", + "R9942" + ], + "R2935": [ + "R3761", + "R4738" + ], + "R4738": [ + "R1130", + "R2276", + "R6979", + "R2935" + ], + "R3649": [ + "R4704", + "R5912" + ], + "R4704": [ + "R4766", + "R3649" + ], + "R4766": [ + "R4704", + "R3393" + ], + "R3393": [ + "R4766", + "R1358" + ], + "R1358": [ + "R8035", + "R3393" + ], + "R8035": [ + "R6711", + "R1358" + ], + "R6711": [ + "R8691", + "R8035" + ], + "R8691": [ + "R8042", + "R6711" + ], + "R8042": [ + "R4302", + "R8691" + ], + "R4302": [ + "R5779", + "R8042", + "R1940", + "R8037" + ], + "R8981": [ + "R6756", + "R3380" + ], + "R6756": [ + "R8252", + "R8981" + ], + "R8252": [ + "R5597", + "R6756" + ], + "R5597": [ + "R7133", + "R8252" + ], + "R7133": [ + "R2026", + "R5597" + ], + "R2026": [ + "R8866", + "R7133" + ], + "R8866": [ + "R8781", + "R2026" + ], + "R8781": [ + "R2149", + "R8866" + ], + "R2149": [ + "R8781", + "R2733" + ], + "R2733": [ + "R2149", + "R8820", + "R3139", + "R4821" + ], + "R8925": [ + "R3617", + "R3380" + ], + "R3617": [ + "R8925", + "R4700" + ], + "R4700": [ + "R8799", + "R3617" + ], + "R8799": [ + "R4700", + "R9710" + ], + "R9710": [ + "R8799", + "R7024" + ], + "R7024": [ + "R9710", + "R6348" + ], + "R6348": [ + "R7024", + "R6188" + ], + "R6188": [ + "R3206", + "R6348" + ], + "R3206": [ + "R6188", + "R2127" + ], + "R2127": [ + "R6861", + "R3206", + "R7308", + "R2379" + ], + "R2128": [ + "R9674", + "R3380" + ], + "R9674": [ + "R2128", + "R8392" + ], + "R8392": [ + "R1319", + "R9674" + ], + "R1319": [ + "R8392", + "R8523" + ], + "R8523": [ + "R1319", + "R1090" + ], + "R1090": [ + "R8523", + "R8007" + ], + "R8007": [ + "R2173", + "R1090" + ], + "R2173": [ + "R4281", + "R8007" + ], + "R4281": [ + "R2173", + "R8823" + ], + "R8823": [ + "R1504", + "R4281", + "R4315", + "R6360" + ], + "R2276": [ + "R4738", + "R7272" + ], + "R7272": [ + "R2276", + "R3032" + ], + "R3032": [ + "R4780", + "R7272" + ], + "R4780": [ + "R9692", + "R3032" + ], + "R9692": [ + "R8449", + "R4780" + ], + "R8449": [ + "R3523", + "R9692" + ], + "R3523": [ + "R8449", + "R5701" + ], + "R5701": [ + "R3523", + "R6790" + ], + "R6790": [ + "R3960", + "R5701" + ], + "R3960": [ + "R6790", + "R9737", + "R4092", + "R2927" + ], + "R6979": [ + "R2712", + "R4738" + ], + "R2712": [ + "R6979", + "R3561" + ], + "R3561": [ + "R2712", + "R9251" + ], + "R9251": [ + "R3561", + "R7501" + ], + "R7501": [ + "R9251", + "R1862" + ], + "R1862": [ + "R7501", + "R7146" + ], + "R7146": [ + "R1862", + "R9633" + ], + "R9633": [ + "R7146", + "R4346" + ], + "R4346": [ + "R9633", + "R4430" + ], + "R4430": [ + "R4346", + "R5926", + "R8817", + "R7371" + ], + "R1130": [ + "R4738", + "R9917" + ], + "R9917": [ + "R1130", + "R7509" + ], + "R7509": [ + "R9917", + "R2559" + ], + "R2559": [ + "R7509", + "R8884" + ], + "R8884": [ + "R7969", + "R2559" + ], + "R7969": [ + "R8884", + "R5453" + ], + "R5453": [ + "R7969", + "R5935" + ], + "R5935": [ + "R5453", + "R7372" + ], + "R7372": [ + "R5935", + "R2007" + ], + "R2007": [ + "R9062", + "R4193", + "R2263", + "R7372" + ], + "R8037": [ + "R4302", + "R7722" + ], + "R7722": [ + "R8037", + "R1471" + ], + "R1471": [ + "R5398", + "R7722" + ], + "R5398": [ + "R7893", + "R1471" + ], + "R7893": [ + "R9220", + "R5398" + ], + "R9220": [ + "R2652", + "R7893" + ], + "R2652": [ + "R9220", + "R7650" + ], + "R7650": [ + "R9114", + "R2652" + ], + "R9114": [ + "R8953", + "R7650" + ], + "R8953": [ + "R1963", + "R9114", + "R2413", + "R7797" + ], + "R5779": [ + "R6379", + "R4302" + ], + "R6379": [ + "R5779", + "R4035" + ], + "R4035": [ + "R6379", + "R8910" + ], + "R8910": [ + "R2461", + "R4035" + ], + "R2461": [ + "R9328", + "R8910" + ], + "R9328": [ + "R2461", + "R8394" + ], + "R8394": [ + "R2621", + "R9328" + ], + "R2621": [ + "R2527", + "R8394" + ], + "R2527": [ + "R2621", + "R5711" + ], + "R5711": [ + "R2373", + "R7723", + "R3449", + "R2527" + ], + "R1940": [ + "R4302", + "R5153" + ], + "R5153": [ + "R1940", + "R7967" + ], + "R7967": [ + "R5153", + "R2129" + ], + "R2129": [ + "R7967", + "R2651" + ], + "R2651": [ + "R2129", + "R9427" + ], + "R9427": [ + "R1859", + "R2651" + ], + "R1859": [ + "R5078", + "R9427" + ], + "R5078": [ + "R1859", + "R5941" + ], + "R5941": [ + "R7302", + "R5078" + ], + "R7302": [ + "R5941", + "R7204", + "R2406", + "R3002" + ], + "R4821": [ + "R1960", + "R2733" + ], + "R1960": [ + "R4821", + "R3481" + ], + "R3481": [ + "R1960", + "R7153" + ], + "R7153": [ + "R3481", + "R4645" + ], + "R4645": [ + "R5469", + "R7153" + ], + "R5469": [ + "R4645", + "R8624" + ], + "R8624": [ + "R5469", + "R3092" + ], + "R3092": [ + "R5739", + "R8624" + ], + "R5739": [ + "R3092", + "R1220" + ], + "R1220": [ + "R5739" + ], + "R3139": [ + "R2733", + "R7905" + ], + "R7905": [ + "R3139", + "R3347" + ], + "R3347": [ + "R2564", + "R7905" + ], + "R2564": [ + "R5609", + "R3347" + ], + "R5609": [ + "R7570", + "R2564" + ], + "R7570": [ + "R6820", + "R5609" + ], + "R6820": [ + "R7570", + "R3884" + ], + "R3884": [ + "R1020", + "R6820" + ], + "R1020": [ + "R2722", + "R3884" + ], + "R2722": [ + "R1020" + ], + "R8820": [ + "R2733", + "R2017" + ], + "R2017": [ + "R8820", + "R4628" + ], + "R4628": [ + "R2017", + "R4506" + ], + "R4506": [ + "R4628", + "R9993" + ], + "R9993": [ + "R4506", + "R1415" + ], + "R1415": [ + "R2054", + "R9993" + ], + "R2054": [ + "R3477", + "R1415" + ], + "R3477": [ + "R2054", + "R6375" + ], + "R6375": [ + "R3477", + "R7540" + ], + "R7540": [ + "R6375" + ], + "R7308": [ + "R7016", + "R2127" + ], + "R7016": [ + "R7308", + "R5083" + ], + "R5083": [ + "R7016", + "R3076" + ], + "R3076": [ + "R5083", + "R7489" + ], + "R7489": [ + "R3076", + "R1916" + ], + "R1916": [ + "R7489", + "R9937" + ], + "R9937": [ + "R1468", + "R1916" + ], + "R1468": [ + "R9150", + "R9937" + ], + "R9150": [ + "R6028", + "R1468" + ], + "R6028": [ + "R9150" + ], + "R6861": [ + "R8582", + "R2127" + ], + "R8582": [ + "R8858", + "R6861" + ], + "R8858": [ + "R8582", + "R8532" + ], + "R8532": [ + "R8858", + "R5245" + ], + "R5245": [ + "R8532", + "R7019" + ], + "R7019": [ + "R7492", + "R5245" + ], + "R7492": [ + "R7019", + "R8739" + ], + "R8739": [ + "R4461", + "R7492" + ], + "R4461": [ + "R7677", + "R8739" + ], + "R7677": [ + "R4461" + ], + "R2379": [ + "R2127", + "R3409" + ], + "R3409": [ + "R3262", + "R2379" + ], + "R3262": [ + "R3409", + "R5269" + ], + "R5269": [ + "R3262", + "R9297" + ], + "R9297": [ + "R5269", + "R2580" + ], + "R2580": [ + "R7262", + "R9297" + ], + "R7262": [ + "R4202", + "R2580" + ], + "R4202": [ + "R7262", + "R4886" + ], + "R4886": [ + "R8546", + "R4202" + ], + "R8546": [ + "R4886" + ], + "R1504": [ + "R2893", + "R8823" + ], + "R2893": [ + "R4684", + "R1504" + ], + "R4684": [ + "R9825", + "R2893" + ], + "R9825": [ + "R8310", + "R4684" + ], + "R8310": [ + "R9825", + "R2076" + ], + "R2076": [ + "R7315", + "R8310" + ], + "R7315": [ + "R9985", + "R2076" + ], + "R9985": [ + "R7315", + "R1292" + ], + "R1292": [ + "R9985", + "R8145" + ], + "R8145": [ + "R1292" + ], + "R6360": [ + "R9618", + "R8823" + ], + "R9618": [ + "R4066", + "R6360" + ], + "R4066": [ + "R9618", + "R8583" + ], + "R8583": [ + "R9037", + "R4066" + ], + "R9037": [ + "R9747", + "R8583" + ], + "R9747": [ + "R9037", + "R8490" + ], + "R8490": [ + "R6539", + "R9747" + ], + "R6539": [ + "R8490", + "R5409" + ], + "R5409": [ + "R6539", + "R4273" + ], + "R4273": [ + "R5409" + ], + "R4315": [ + "R8823", + "R2462" + ], + "R2462": [ + "R3125", + "R4315" + ], + "R3125": [ + "R5555", + "R2462" + ], + "R5555": [ + "R5950", + "R3125" + ], + "R5950": [ + "R5410", + "R5555" + ], + "R5410": [ + "R5950", + "R3155" + ], + "R3155": [ + "R8203", + "R5410" + ], + "R8203": [ + "R6726", + "R3155" + ], + "R6726": [ + "R8203", + "R2494" + ], + "R2494": [ + "R6726" + ], + "R9737": [ + "R5820", + "R3960" + ], + "R5820": [ + "R5685", + "R9737" + ], + "R5685": [ + "R2675", + "R5820" + ], + "R2675": [ + "R5685", + "R7050" + ], + "R7050": [ + "R2675", + "R7619" + ], + "R7619": [ + "R7050", + "R5582" + ], + "R5582": [ + "R7619", + "R4917" + ], + "R4917": [ + "R5582", + "R7764" + ], + "R7764": [ + "R4917", + "R4556" + ], + "R4556": [ + "R7764" + ], + "R4092": [ + "R3960", + "R4428" + ], + "R4428": [ + "R9814", + "R4092" + ], + "R9814": [ + "R5715", + "R4428" + ], + "R5715": [ + "R6240", + "R9814" + ], + "R6240": [ + "R5715", + "R7076" + ], + "R7076": [ + "R8102", + "R6240" + ], + "R8102": [ + "R4860", + "R7076" + ], + "R4860": [ + "R4112", + "R8102" + ], + "R4112": [ + "R8527", + "R4860" + ], + "R8527": [ + "R4112" + ], + "R2927": [ + "R7139", + "R3960" + ], + "R7139": [ + "R8632", + "R2927" + ], + "R8632": [ + "R7139", + "R6689" + ], + "R6689": [ + "R2807", + "R8632" + ], + "R2807": [ + "R6689", + "R4723" + ], + "R4723": [ + "R2807", + "R4712" + ], + "R4712": [ + "R6093", + "R4723" + ], + "R6093": [ + "R4712", + "R6132" + ], + "R6132": [ + "R6093", + "R8943" + ], + "R8943": [ + "R6132" + ], + "R7371": [ + "R4430", + "R4253" + ], + "R4253": [ + "R7371", + "R8444" + ], + "R8444": [ + "R4253", + "R6615" + ], + "R6615": [ + "R8444", + "R2256" + ], + "R2256": [ + "R7491", + "R6615" + ], + "R7491": [ + "R5246", + "R2256" + ], + "R5246": [ + "R7491", + "R5557" + ], + "R5557": [ + "R4758", + "R5246" + ], + "R4758": [ + "R5557", + "R5156" + ], + "R5156": [ + "R4758" + ], + "R5926": [ + "R8050", + "R4430" + ], + "R8050": [ + "R9518", + "R5926" + ], + "R9518": [ + "R4613", + "R8050" + ], + "R4613": [ + "R9518", + "R7271" + ], + "R7271": [ + "R4613", + "R7639" + ], + "R7639": [ + "R6148", + "R7271" + ], + "R6148": [ + "R9432", + "R7639" + ], + "R9432": [ + "R7885", + "R6148" + ], + "R7885": [ + "R9432", + "R5856" + ], + "R5856": [ + "R7885" + ], + "R8817": [ + "R1659", + "R4430" + ], + "R1659": [ + "R8817", + "R5934" + ], + "R5934": [ + "R1659", + "R1362" + ], + "R1362": [ + "R5934", + "R6010" + ], + "R6010": [ + "R1731", + "R1362" + ], + "R1731": [ + "R6010", + "R2052" + ], + "R2052": [ + "R1731", + "R6306" + ], + "R6306": [ + "R9395", + "R2052" + ], + "R9395": [ + "R6306", + "R9420" + ], + "R9420": [ + "R9395" + ], + "R9062": [ + "R5014", + "R2007" + ], + "R5014": [ + "R9062", + "R5638" + ], + "R5638": [ + "R5014", + "R2265" + ], + "R2265": [ + "R5638", + "R3816" + ], + "R3816": [ + "R2265", + "R5209" + ], + "R5209": [ + "R3816", + "R4631" + ], + "R4631": [ + "R4267", + "R5209" + ], + "R4267": [ + "R8343", + "R4631" + ], + "R8343": [ + "R4267", + "R7615" + ], + "R7615": [ + "R8343" + ], + "R4193": [ + "R1371", + "R2007" + ], + "R1371": [ + "R4193", + "R9753" + ], + "R9753": [ + "R1371", + "R3204" + ], + "R3204": [ + "R9753", + "R6590" + ], + "R6590": [ + "R1046", + "R3204" + ], + "R1046": [ + "R6590", + "R1179" + ], + "R1179": [ + "R3965", + "R1046" + ], + "R3965": [ + "R1179", + "R2865" + ], + "R2865": [ + "R3965", + "R7513" + ], + "R7513": [ + "R2865" + ], + "R2263": [ + "R9837", + "R2007" + ], + "R9837": [ + "R1894", + "R2263" + ], + "R1894": [ + "R9837", + "R4088" + ], + "R4088": [ + "R6418", + "R1894" + ], + "R6418": [ + "R4088", + "R8573" + ], + "R8573": [ + "R6418", + "R9711" + ], + "R9711": [ + "R1018", + "R8573" + ], + "R1018": [ + "R9711", + "R8930" + ], + "R8930": [ + "R1018", + "R4086" + ], + "R4086": [ + "R8930" + ], + "R2413": [ + "R2576", + "R8953" + ], + "R2576": [ + "R7855", + "R2413" + ], + "R7855": [ + "R5630", + "R2576" + ], + "R5630": [ + "R6467", + "R7855" + ], + "R6467": [ + "R6653", + "R5630" + ], + "R6653": [ + "R6467", + "R2122" + ], + "R2122": [ + "R1400", + "R6653" + ], + "R1400": [ + "R1142", + "R2122" + ], + "R1142": [ + "R1400", + "R5835" + ], + "R5835": [ + "R1142" + ], + "R1963": [ + "R8953", + "R4520" + ], + "R4520": [ + "R7172", + "R1963" + ], + "R7172": [ + "R1628", + "R4520" + ], + "R1628": [ + "R7393", + "R7172" + ], + "R7393": [ + "R2485", + "R1628" + ], + "R2485": [ + "R4911", + "R7393" + ], + "R4911": [ + "R3126", + "R2485" + ], + "R3126": [ + "R4911", + "R2493" + ], + "R2493": [ + "R3126", + "R1111" + ], + "R1111": [ + "R2493" + ], + "R7797": [ + "R8953", + "R2183" + ], + "R2183": [ + "R7797", + "R4283" + ], + "R4283": [ + "R2183", + "R3199" + ], + "R3199": [ + "R2261", + "R4283" + ], + "R2261": [ + "R3199", + "R5670" + ], + "R5670": [ + "R2261", + "R6724" + ], + "R6724": [ + "R9171", + "R5670" + ], + "R9171": [ + "R3355", + "R6724" + ], + "R3355": [ + "R4553", + "R9171" + ], + "R4553": [ + "R3355" + ], + "R3449": [ + "R1706", + "R5711" + ], + "R1706": [ + "R3449", + "R7853" + ], + "R7853": [ + "R8545", + "R1706" + ], + "R8545": [ + "R7853", + "R4767" + ], + "R4767": [ + "R1510", + "R8545" + ], + "R1510": [ + "R4767", + "R1926" + ], + "R1926": [ + "R9792", + "R1510" + ], + "R9792": [ + "R6427", + "R1926" + ], + "R6427": [ + "R9792", + "R6593" + ], + "R6593": [ + "R6427" + ], + "R7723": [ + "R1756", + "R5711" + ], + "R1756": [ + "R4876", + "R7723" + ], + "R4876": [ + "R9941", + "R1756" + ], + "R9941": [ + "R7846", + "R4876" + ], + "R7846": [ + "R8836", + "R9941" + ], + "R8836": [ + "R9580", + "R7846" + ], + "R9580": [ + "R8836", + "R3131" + ], + "R3131": [ + "R9197", + "R9580" + ], + "R9197": [ + "R3131", + "R2433" + ], + "R2433": [ + "R9197" + ], + "R2373": [ + "R5711", + "R6142" + ], + "R6142": [ + "R8463", + "R2373" + ], + "R8463": [ + "R8455", + "R6142" + ], + "R8455": [ + "R1977", + "R8463" + ], + "R1977": [ + "R1586", + "R8455" + ], + "R1586": [ + "R3294", + "R1977" + ], + "R3294": [ + "R1586", + "R5662" + ], + "R5662": [ + "R3138", + "R3294" + ], + "R3138": [ + "R2448", + "R5662" + ], + "R2448": [ + "R3138" + ], + "R3002": [ + "R9588", + "R7302" + ], + "R9588": [ + "R3002", + "R1591" + ], + "R1591": [ + "R8440", + "R9588" + ], + "R8440": [ + "R8575", + "R1591" + ], + "R8575": [ + "R1074", + "R8440" + ], + "R1074": [ + "R6978", + "R8575" + ], + "R6978": [ + "R6488", + "R1074" + ], + "R6488": [ + "R6107", + "R6978" + ], + "R6107": [ + "R6488", + "R3525" + ], + "R3525": [ + "R6107" + ], + "R7204": [ + "R7302", + "R5328" + ], + "R5328": [ + "R7204", + "R2097" + ], + "R2097": [ + "R7390", + "R5328" + ], + "R7390": [ + "R3011", + "R2097" + ], + "R3011": [ + "R7390", + "R8457" + ], + "R8457": [ + "R3011", + "R8607" + ], + "R8607": [ + "R2242", + "R8457" + ], + "R2242": [ + "R8607", + "R9590" + ], + "R9590": [ + "R9086", + "R2242" + ], + "R9086": [ + "R9590" + ], + "R2406": [ + "R7302", + "R3759" + ], + "R3759": [ + "R2406", + "R8278" + ], + "R8278": [ + "R3759", + "R2715" + ], + "R2715": [ + "R5239", + "R8278" + ], + "R5239": [ + "R2715", + "R8752" + ], + "R8752": [ + "R2754", + "R5239" + ], + "R2754": [ + "R8752", + "R3034" + ], + "R3034": [ + "R2754", + "R2809" + ], + "R2809": [ + "R3034", + "R2825" + ], + "R2825": [ + "R2809" + ] + } +} diff --git a/tasks/maze-t25/instruction.md b/tasks/maze-t25/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t25/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t25/solution/solve.sh b/tasks/maze-t25/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..5ccb2ce6d3d96b4f236d77331542050b086c6aa2 --- /dev/null +++ b/tasks/maze-t25/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R2340 R9784 R7307 R6661 R4280 R7889 R3613 R8434 R9901 R3380 R8925 R3617 R4700 R8799 R9710 R7024 R6348 R6188 R3206 R2127 R6861 R8582 R8858 R8532 R5245 R7019 R7492 R8739 R4461 R7677; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t25/task.toml b/tasks/maze-t25/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..c3cdc7207f5bb07884aef21ca1c5ab36752e9831 --- /dev/null +++ b/tasks/maze-t25/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t25" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 25 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t25/tests/Dockerfile b/tasks/maze-t25/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t25/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t25/tests/expected.json b/tasks/maze-t25/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..99bd5e163bda130e3b5ac593b0843aa26e4efd49 --- /dev/null +++ b/tasks/maze-t25/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R5912", + "exit": "R7677", + "budget": 210, + "tree_sha256": "df73db0d3d043971ccc7c540226c26562eeb53355ee1bc53e51d027b2bcfa739" +} diff --git a/tasks/maze-t25/tests/grade.py b/tasks/maze-t25/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t25/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t25/tests/test.sh b/tasks/maze-t25/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t25/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t26/environment/agent/Dockerfile b/tasks/maze-t26/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t26/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t26/environment/agent/maze b/tasks/maze-t26/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t26/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t26/environment/docker-compose.yaml b/tasks/maze-t26/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4eb5cc2700bfd9f5d4c368cdf2c2849b5251b6e2 --- /dev/null +++ b/tasks/maze-t26/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t26: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t26-87fa60ee420f + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t26/environment/maze/Dockerfile b/tasks/maze-t26/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t26/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t26/environment/maze/engine.py b/tasks/maze-t26/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t26/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t26/environment/maze/export.py b/tasks/maze-t26/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t26/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t26/environment/maze/service.py b/tasks/maze-t26/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t26/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t26/environment/maze/success.py b/tasks/maze-t26/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t26/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t26/environment/maze/tree.json b/tasks/maze-t26/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..ec62a2da0ee1caa2ccde590a8ab714bdd4f82527 --- /dev/null +++ b/tasks/maze-t26/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R3018", + "exit": "R3490", + "budget": 210, + "doors": { + "R3018": [ + "R3272", + "R7018", + "R9194" + ], + "R7018": [ + "R1912", + "R3018" + ], + "R1912": [ + "R9479", + "R7018" + ], + "R9479": [ + "R3138", + "R1912" + ], + "R3138": [ + "R9479", + "R8939" + ], + "R8939": [ + "R7574", + "R3138" + ], + "R7574": [ + "R1579", + "R8939" + ], + "R1579": [ + "R3768", + "R7574" + ], + "R3768": [ + "R1579", + "R8150" + ], + "R8150": [ + "R3965", + "R3768" + ], + "R3965": [ + "R7312", + "R6447", + "R2064", + "R8150" + ], + "R3272": [ + "R4448", + "R3018" + ], + "R4448": [ + "R9994", + "R3272" + ], + "R9994": [ + "R1141", + "R4448" + ], + "R1141": [ + "R9994", + "R4729" + ], + "R4729": [ + "R6431", + "R1141" + ], + "R6431": [ + "R2364", + "R4729" + ], + "R2364": [ + "R6431", + "R3813" + ], + "R3813": [ + "R2364", + "R1741" + ], + "R1741": [ + "R3123", + "R3813" + ], + "R3123": [ + "R6669", + "R1741", + "R8422", + "R9179" + ], + "R9194": [ + "R3018", + "R5799" + ], + "R5799": [ + "R8569", + "R9194" + ], + "R8569": [ + "R5799", + "R8665" + ], + "R8665": [ + "R8569", + "R8331" + ], + "R8331": [ + "R8165", + "R8665" + ], + "R8165": [ + "R8502", + "R8331" + ], + "R8502": [ + "R8165", + "R4224" + ], + "R4224": [ + "R8502", + "R9141" + ], + "R9141": [ + "R4224", + "R9075" + ], + "R9075": [ + "R7997", + "R7355", + "R9141", + "R5330" + ], + "R7312": [ + "R3965", + "R9174" + ], + "R9174": [ + "R7265", + "R7312" + ], + "R7265": [ + "R3310", + "R9174" + ], + "R3310": [ + "R7265", + "R9089" + ], + "R9089": [ + "R9934", + "R3310" + ], + "R9934": [ + "R8639", + "R9089" + ], + "R8639": [ + "R7786", + "R9934" + ], + "R7786": [ + "R8745", + "R8639" + ], + "R8745": [ + "R7786", + "R9985" + ], + "R9985": [ + "R8745", + "R2905", + "R7448", + "R3672" + ], + "R2064": [ + "R3965", + "R5667" + ], + "R5667": [ + "R2064", + "R3201" + ], + "R3201": [ + "R5351", + "R5667" + ], + "R5351": [ + "R9858", + "R3201" + ], + "R9858": [ + "R4818", + "R5351" + ], + "R4818": [ + "R2264", + "R9858" + ], + "R2264": [ + "R4818", + "R5573" + ], + "R5573": [ + "R2264", + "R1605" + ], + "R1605": [ + "R4732", + "R5573" + ], + "R4732": [ + "R1605", + "R9786", + "R7715", + "R9524" + ], + "R6447": [ + "R9787", + "R3965" + ], + "R9787": [ + "R7084", + "R6447" + ], + "R7084": [ + "R9787", + "R1269" + ], + "R1269": [ + "R7084", + "R3871" + ], + "R3871": [ + "R7180", + "R1269" + ], + "R7180": [ + "R3871", + "R7408" + ], + "R7408": [ + "R4249", + "R7180" + ], + "R4249": [ + "R9990", + "R7408" + ], + "R9990": [ + "R4249", + "R6672" + ], + "R6672": [ + "R2543", + "R9990", + "R1445", + "R6148" + ], + "R9179": [ + "R7897", + "R3123" + ], + "R7897": [ + "R9179", + "R7096" + ], + "R7096": [ + "R7897", + "R9745" + ], + "R9745": [ + "R3604", + "R7096" + ], + "R3604": [ + "R9745", + "R1854" + ], + "R1854": [ + "R9666", + "R3604" + ], + "R9666": [ + "R9445", + "R1854" + ], + "R9445": [ + "R9666", + "R6719" + ], + "R6719": [ + "R9164", + "R9445" + ], + "R9164": [ + "R2782", + "R6719", + "R7057", + "R6520" + ], + "R8422": [ + "R6978", + "R3123" + ], + "R6978": [ + "R8422", + "R9113" + ], + "R9113": [ + "R6978", + "R4630" + ], + "R4630": [ + "R9113", + "R5370" + ], + "R5370": [ + "R3200", + "R4630" + ], + "R3200": [ + "R5370", + "R2221" + ], + "R2221": [ + "R3200", + "R1899" + ], + "R1899": [ + "R1305", + "R2221" + ], + "R1305": [ + "R7443", + "R1899" + ], + "R7443": [ + "R1350", + "R4726", + "R1305", + "R7330" + ], + "R6669": [ + "R4433", + "R3123" + ], + "R4433": [ + "R6669", + "R6799" + ], + "R6799": [ + "R3403", + "R4433" + ], + "R3403": [ + "R6799", + "R6112" + ], + "R6112": [ + "R3403", + "R9098" + ], + "R9098": [ + "R6112", + "R9475" + ], + "R9475": [ + "R9098", + "R4776" + ], + "R4776": [ + "R4208", + "R9475" + ], + "R4208": [ + "R4776", + "R1101" + ], + "R1101": [ + "R3671", + "R4111", + "R7478", + "R4208" + ], + "R5330": [ + "R9075", + "R6280" + ], + "R6280": [ + "R5330", + "R1902" + ], + "R1902": [ + "R1588", + "R6280" + ], + "R1588": [ + "R9737", + "R1902" + ], + "R9737": [ + "R5066", + "R1588" + ], + "R5066": [ + "R1466", + "R9737" + ], + "R1466": [ + "R4474", + "R5066" + ], + "R4474": [ + "R9163", + "R1466" + ], + "R9163": [ + "R4474", + "R5702" + ], + "R5702": [ + "R1974", + "R5115", + "R5156", + "R9163" + ], + "R7355": [ + "R9075", + "R2836" + ], + "R2836": [ + "R3917", + "R7355" + ], + "R3917": [ + "R9928", + "R2836" + ], + "R9928": [ + "R7206", + "R3917" + ], + "R7206": [ + "R4898", + "R9928" + ], + "R4898": [ + "R7206", + "R5314" + ], + "R5314": [ + "R8393", + "R4898" + ], + "R8393": [ + "R3202", + "R5314" + ], + "R3202": [ + "R8393", + "R6110" + ], + "R6110": [ + "R6492", + "R3202", + "R5134", + "R2250" + ], + "R7997": [ + "R9075", + "R2071" + ], + "R2071": [ + "R7997", + "R2439" + ], + "R2439": [ + "R1613", + "R2071" + ], + "R1613": [ + "R9378", + "R2439" + ], + "R9378": [ + "R5773", + "R1613" + ], + "R5773": [ + "R9378", + "R8822" + ], + "R8822": [ + "R4280", + "R5773" + ], + "R4280": [ + "R8356", + "R8822" + ], + "R8356": [ + "R4280", + "R4522" + ], + "R4522": [ + "R4928", + "R3014", + "R1570", + "R8356" + ], + "R2905": [ + "R6691", + "R9985" + ], + "R6691": [ + "R2905", + "R1843" + ], + "R1843": [ + "R6691", + "R4867" + ], + "R4867": [ + "R1843", + "R1036" + ], + "R1036": [ + "R4867", + "R9882" + ], + "R9882": [ + "R1036", + "R2556" + ], + "R2556": [ + "R9882", + "R9942" + ], + "R9942": [ + "R2695", + "R2556" + ], + "R2695": [ + "R6144", + "R9942" + ], + "R6144": [ + "R2695" + ], + "R7448": [ + "R9985", + "R2831" + ], + "R2831": [ + "R4887", + "R7448" + ], + "R4887": [ + "R5053", + "R2831" + ], + "R5053": [ + "R4887", + "R3378" + ], + "R3378": [ + "R5053", + "R7501" + ], + "R7501": [ + "R3378", + "R1091" + ], + "R1091": [ + "R6889", + "R7501" + ], + "R6889": [ + "R8619", + "R1091" + ], + "R8619": [ + "R6889", + "R8946" + ], + "R8946": [ + "R8619" + ], + "R3672": [ + "R8115", + "R9985" + ], + "R8115": [ + "R8289", + "R3672" + ], + "R8289": [ + "R8115", + "R9055" + ], + "R9055": [ + "R8289", + "R2730" + ], + "R2730": [ + "R4941", + "R9055" + ], + "R4941": [ + "R2730", + "R9909" + ], + "R9909": [ + "R5877", + "R4941" + ], + "R5877": [ + "R6851", + "R9909" + ], + "R6851": [ + "R5877", + "R8941" + ], + "R8941": [ + "R6851" + ], + "R9524": [ + "R4732", + "R6932" + ], + "R6932": [ + "R9524", + "R8198" + ], + "R8198": [ + "R6932", + "R4295" + ], + "R4295": [ + "R5107", + "R8198" + ], + "R5107": [ + "R4295", + "R6913" + ], + "R6913": [ + "R5446", + "R5107" + ], + "R5446": [ + "R2884", + "R6913" + ], + "R2884": [ + "R5446", + "R2518" + ], + "R2518": [ + "R2884", + "R5308" + ], + "R5308": [ + "R2518" + ], + "R9786": [ + "R9970", + "R4732" + ], + "R9970": [ + "R9786", + "R7152" + ], + "R7152": [ + "R3391", + "R9970" + ], + "R3391": [ + "R7152", + "R8767" + ], + "R8767": [ + "R3391", + "R9724" + ], + "R9724": [ + "R8767", + "R4659" + ], + "R4659": [ + "R9724", + "R3442" + ], + "R3442": [ + "R4659", + "R6381" + ], + "R6381": [ + "R5863", + "R3442" + ], + "R5863": [ + "R6381" + ], + "R7715": [ + "R3031", + "R4732" + ], + "R3031": [ + "R9620", + "R7715" + ], + "R9620": [ + "R9449", + "R3031" + ], + "R9449": [ + "R9620", + "R3430" + ], + "R3430": [ + "R9449", + "R2662" + ], + "R2662": [ + "R1027", + "R3430" + ], + "R1027": [ + "R7352", + "R2662" + ], + "R7352": [ + "R7191", + "R1027" + ], + "R7191": [ + "R7352", + "R4319" + ], + "R4319": [ + "R7191" + ], + "R2543": [ + "R1039", + "R6672" + ], + "R1039": [ + "R2543", + "R7580" + ], + "R7580": [ + "R4760", + "R1039" + ], + "R4760": [ + "R7580", + "R1685" + ], + "R1685": [ + "R9047", + "R4760" + ], + "R9047": [ + "R1685", + "R7455" + ], + "R7455": [ + "R9047", + "R3993" + ], + "R3993": [ + "R7455", + "R4370" + ], + "R4370": [ + "R3993", + "R7791" + ], + "R7791": [ + "R4370" + ], + "R1445": [ + "R7507", + "R6672" + ], + "R7507": [ + "R1445", + "R1514" + ], + "R1514": [ + "R2844", + "R7507" + ], + "R2844": [ + "R1514", + "R2218" + ], + "R2218": [ + "R3296", + "R2844" + ], + "R3296": [ + "R2218", + "R3257" + ], + "R3257": [ + "R3296", + "R6260" + ], + "R6260": [ + "R3361", + "R3257" + ], + "R3361": [ + "R6260", + "R9840" + ], + "R9840": [ + "R3361" + ], + "R6148": [ + "R6672", + "R4246" + ], + "R4246": [ + "R6148", + "R9812" + ], + "R9812": [ + "R5934", + "R4246" + ], + "R5934": [ + "R3812", + "R9812" + ], + "R3812": [ + "R5934", + "R5430" + ], + "R5430": [ + "R6724", + "R3812" + ], + "R6724": [ + "R5430", + "R4528" + ], + "R4528": [ + "R6724", + "R9608" + ], + "R9608": [ + "R4227", + "R4528" + ], + "R4227": [ + "R9608" + ], + "R6520": [ + "R9164", + "R4321" + ], + "R4321": [ + "R9124", + "R6520" + ], + "R9124": [ + "R4321", + "R9328" + ], + "R9328": [ + "R6969", + "R9124" + ], + "R6969": [ + "R9328", + "R2913" + ], + "R2913": [ + "R8813", + "R6969" + ], + "R8813": [ + "R2913", + "R9526" + ], + "R9526": [ + "R8813", + "R8079" + ], + "R8079": [ + "R5906", + "R9526" + ], + "R5906": [ + "R8079" + ], + "R7057": [ + "R6523", + "R9164" + ], + "R6523": [ + "R7057", + "R4769" + ], + "R4769": [ + "R5514", + "R6523" + ], + "R5514": [ + "R7970", + "R4769" + ], + "R7970": [ + "R4124", + "R5514" + ], + "R4124": [ + "R7970", + "R5498" + ], + "R5498": [ + "R9911", + "R4124" + ], + "R9911": [ + "R5498", + "R4665" + ], + "R4665": [ + "R9911", + "R1611" + ], + "R1611": [ + "R4665" + ], + "R2782": [ + "R9164", + "R1585" + ], + "R1585": [ + "R5767", + "R2782" + ], + "R5767": [ + "R1585", + "R4042" + ], + "R4042": [ + "R7510", + "R5767" + ], + "R7510": [ + "R1909", + "R4042" + ], + "R1909": [ + "R7510", + "R7116" + ], + "R7116": [ + "R9654", + "R1909" + ], + "R9654": [ + "R3314", + "R7116" + ], + "R3314": [ + "R9654", + "R1041" + ], + "R1041": [ + "R3314" + ], + "R4726": [ + "R7443", + "R7167" + ], + "R7167": [ + "R5456", + "R4726" + ], + "R5456": [ + "R7167", + "R1354" + ], + "R1354": [ + "R5456", + "R6198" + ], + "R6198": [ + "R1354", + "R9785" + ], + "R9785": [ + "R9862", + "R6198" + ], + "R9862": [ + "R7430", + "R9785" + ], + "R7430": [ + "R6108", + "R9862" + ], + "R6108": [ + "R7430", + "R3490" + ], + "R3490": [ + "R6108" + ], + "R7330": [ + "R2242", + "R7443" + ], + "R2242": [ + "R7330", + "R7027" + ], + "R7027": [ + "R2242", + "R7596" + ], + "R7596": [ + "R7027", + "R4676" + ], + "R4676": [ + "R7596", + "R4585" + ], + "R4585": [ + "R4676", + "R7499" + ], + "R7499": [ + "R3651", + "R4585" + ], + "R3651": [ + "R6899", + "R7499" + ], + "R6899": [ + "R1692", + "R3651" + ], + "R1692": [ + "R6899" + ], + "R1350": [ + "R7443", + "R9330" + ], + "R9330": [ + "R1350", + "R5598" + ], + "R5598": [ + "R9572", + "R9330" + ], + "R9572": [ + "R2048", + "R5598" + ], + "R2048": [ + "R9572", + "R4831" + ], + "R4831": [ + "R2048", + "R8942" + ], + "R8942": [ + "R4831", + "R1527" + ], + "R1527": [ + "R7806", + "R8942" + ], + "R7806": [ + "R5448", + "R1527" + ], + "R5448": [ + "R7806" + ], + "R3671": [ + "R1101", + "R5802" + ], + "R5802": [ + "R3671", + "R6322" + ], + "R6322": [ + "R5802", + "R1314" + ], + "R1314": [ + "R6322", + "R8495" + ], + "R8495": [ + "R1314", + "R3042" + ], + "R3042": [ + "R4309", + "R8495" + ], + "R4309": [ + "R3042", + "R5649" + ], + "R5649": [ + "R4913", + "R4309" + ], + "R4913": [ + "R5649", + "R6562" + ], + "R6562": [ + "R4913" + ], + "R4111": [ + "R9003", + "R1101" + ], + "R9003": [ + "R4111", + "R7204" + ], + "R7204": [ + "R9003", + "R4641" + ], + "R4641": [ + "R1360", + "R7204" + ], + "R1360": [ + "R1061", + "R4641" + ], + "R1061": [ + "R1360", + "R6850" + ], + "R6850": [ + "R7384", + "R1061" + ], + "R7384": [ + "R2136", + "R6850" + ], + "R2136": [ + "R5688", + "R7384" + ], + "R5688": [ + "R2136" + ], + "R7478": [ + "R1533", + "R1101" + ], + "R1533": [ + "R4682", + "R7478" + ], + "R4682": [ + "R8112", + "R1533" + ], + "R8112": [ + "R1178", + "R4682" + ], + "R1178": [ + "R8112", + "R5039" + ], + "R5039": [ + "R1178", + "R1465" + ], + "R1465": [ + "R5039", + "R1960" + ], + "R1960": [ + "R1465", + "R6344" + ], + "R6344": [ + "R3047", + "R1960" + ], + "R3047": [ + "R6344" + ], + "R5115": [ + "R5155", + "R5702" + ], + "R5155": [ + "R8648", + "R5115" + ], + "R8648": [ + "R5155", + "R1536" + ], + "R1536": [ + "R8648", + "R7341" + ], + "R7341": [ + "R1536", + "R3002" + ], + "R3002": [ + "R7072", + "R7341" + ], + "R7072": [ + "R9450", + "R3002" + ], + "R9450": [ + "R5092", + "R7072" + ], + "R5092": [ + "R4333", + "R9450" + ], + "R4333": [ + "R5092" + ], + "R5156": [ + "R5702", + "R6466" + ], + "R6466": [ + "R5156", + "R4979" + ], + "R4979": [ + "R5674", + "R6466" + ], + "R5674": [ + "R4979", + "R6345" + ], + "R6345": [ + "R7901", + "R5674" + ], + "R7901": [ + "R8219", + "R6345" + ], + "R8219": [ + "R7901", + "R3327" + ], + "R3327": [ + "R8219", + "R3051" + ], + "R3051": [ + "R3327", + "R3505" + ], + "R3505": [ + "R3051" + ], + "R1974": [ + "R7276", + "R5702" + ], + "R7276": [ + "R1974", + "R8790" + ], + "R8790": [ + "R7276", + "R6253" + ], + "R6253": [ + "R9067", + "R8790" + ], + "R9067": [ + "R6253", + "R5080" + ], + "R5080": [ + "R8835", + "R9067" + ], + "R8835": [ + "R6935", + "R5080" + ], + "R6935": [ + "R8835", + "R7762" + ], + "R7762": [ + "R1679", + "R6935" + ], + "R1679": [ + "R7762" + ], + "R2250": [ + "R6158", + "R6110" + ], + "R6158": [ + "R2250", + "R8751" + ], + "R8751": [ + "R6597", + "R6158" + ], + "R6597": [ + "R8751", + "R3370" + ], + "R3370": [ + "R7375", + "R6597" + ], + "R7375": [ + "R3370", + "R8915" + ], + "R8915": [ + "R1371", + "R7375" + ], + "R1371": [ + "R6933", + "R8915" + ], + "R6933": [ + "R1371", + "R3714" + ], + "R3714": [ + "R6933" + ], + "R5134": [ + "R6110", + "R8845" + ], + "R8845": [ + "R7820", + "R5134" + ], + "R7820": [ + "R8845", + "R8457" + ], + "R8457": [ + "R6538", + "R7820" + ], + "R6538": [ + "R8457", + "R6577" + ], + "R6577": [ + "R6538", + "R7735" + ], + "R7735": [ + "R6577", + "R8755" + ], + "R8755": [ + "R7735", + "R5940" + ], + "R5940": [ + "R9670", + "R8755" + ], + "R9670": [ + "R5940" + ], + "R6492": [ + "R1194", + "R6110" + ], + "R1194": [ + "R3146", + "R6492" + ], + "R3146": [ + "R8652", + "R1194" + ], + "R8652": [ + "R8456", + "R3146" + ], + "R8456": [ + "R1256", + "R8652" + ], + "R1256": [ + "R7105", + "R8456" + ], + "R7105": [ + "R1256", + "R6658" + ], + "R6658": [ + "R3688", + "R7105" + ], + "R3688": [ + "R6658", + "R6838" + ], + "R6838": [ + "R3688" + ], + "R4928": [ + "R4522", + "R3019" + ], + "R3019": [ + "R8771", + "R4928" + ], + "R8771": [ + "R3019", + "R5293" + ], + "R5293": [ + "R2293", + "R8771" + ], + "R2293": [ + "R6139", + "R5293" + ], + "R6139": [ + "R2293", + "R8228" + ], + "R8228": [ + "R4162", + "R6139" + ], + "R4162": [ + "R8228", + "R2510" + ], + "R2510": [ + "R9895", + "R4162" + ], + "R9895": [ + "R2510" + ], + "R3014": [ + "R9759", + "R4522" + ], + "R9759": [ + "R3014", + "R8712" + ], + "R8712": [ + "R9759", + "R7619" + ], + "R7619": [ + "R8712", + "R2115" + ], + "R2115": [ + "R5069", + "R7619" + ], + "R5069": [ + "R3366", + "R2115" + ], + "R3366": [ + "R5069", + "R1984" + ], + "R1984": [ + "R3366", + "R4420" + ], + "R4420": [ + "R2068", + "R1984" + ], + "R2068": [ + "R4420" + ], + "R1570": [ + "R4012", + "R4522" + ], + "R4012": [ + "R1570", + "R1972" + ], + "R1972": [ + "R6673", + "R4012" + ], + "R6673": [ + "R1972", + "R9290" + ], + "R9290": [ + "R8240", + "R6673" + ], + "R8240": [ + "R1246", + "R9290" + ], + "R1246": [ + "R7234", + "R8240" + ], + "R7234": [ + "R1246", + "R5529" + ], + "R5529": [ + "R7234", + "R5783" + ], + "R5783": [ + "R5529" + ] + } +} diff --git a/tasks/maze-t26/instruction.md b/tasks/maze-t26/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t26/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t26/solution/solve.sh b/tasks/maze-t26/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..93b312fbcf1872c144952d91029cbc4d5c81b3c2 --- /dev/null +++ b/tasks/maze-t26/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R3272 R4448 R9994 R1141 R4729 R6431 R2364 R3813 R1741 R3123 R8422 R6978 R9113 R4630 R5370 R3200 R2221 R1899 R1305 R7443 R4726 R7167 R5456 R1354 R6198 R9785 R9862 R7430 R6108 R3490; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t26/task.toml b/tasks/maze-t26/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..545b5b9237348b152fcae33cd1cbb6f78f67f315 --- /dev/null +++ b/tasks/maze-t26/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t26" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 26 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t26/tests/Dockerfile b/tasks/maze-t26/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t26/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t26/tests/expected.json b/tasks/maze-t26/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..2a1516a08a138fc32d99f2f6872e3ee9af3ab4f8 --- /dev/null +++ b/tasks/maze-t26/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R3018", + "exit": "R3490", + "budget": 210, + "tree_sha256": "d71e8b0c7cc0db8922ca90935ef719ed3cd1e126d548a286456d77982446ad0e" +} diff --git a/tasks/maze-t26/tests/grade.py b/tasks/maze-t26/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t26/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t26/tests/test.sh b/tasks/maze-t26/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t26/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t27/environment/agent/Dockerfile b/tasks/maze-t27/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t27/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t27/environment/agent/maze b/tasks/maze-t27/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t27/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t27/environment/docker-compose.yaml b/tasks/maze-t27/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c9201ea0264047e237bc214cd68527377753e2e2 --- /dev/null +++ b/tasks/maze-t27/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t27: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t27-f162a935bebd + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t27/environment/maze/Dockerfile b/tasks/maze-t27/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t27/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t27/environment/maze/engine.py b/tasks/maze-t27/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t27/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t27/environment/maze/export.py b/tasks/maze-t27/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t27/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t27/environment/maze/service.py b/tasks/maze-t27/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t27/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t27/environment/maze/success.py b/tasks/maze-t27/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t27/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t27/environment/maze/tree.json b/tasks/maze-t27/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..c6b56696d5e7e5179171435edb370c4f7c64828c --- /dev/null +++ b/tasks/maze-t27/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R7470", + "exit": "R5374", + "budget": 210, + "doors": { + "R7470": [ + "R1374", + "R3225", + "R9780" + ], + "R9780": [ + "R6869", + "R7470" + ], + "R6869": [ + "R9780", + "R8349" + ], + "R8349": [ + "R6869", + "R7348" + ], + "R7348": [ + "R8349", + "R6494" + ], + "R6494": [ + "R4460", + "R7348" + ], + "R4460": [ + "R5676", + "R6494" + ], + "R5676": [ + "R7901", + "R4460" + ], + "R7901": [ + "R5676", + "R7185" + ], + "R7185": [ + "R7901", + "R4939" + ], + "R4939": [ + "R3396", + "R1778", + "R7185", + "R2699" + ], + "R3225": [ + "R4339", + "R7470" + ], + "R4339": [ + "R2035", + "R3225" + ], + "R2035": [ + "R1532", + "R4339" + ], + "R1532": [ + "R2035", + "R4085" + ], + "R4085": [ + "R3582", + "R1532" + ], + "R3582": [ + "R1806", + "R4085" + ], + "R1806": [ + "R3582", + "R2419" + ], + "R2419": [ + "R4581", + "R1806" + ], + "R4581": [ + "R2419", + "R1168" + ], + "R1168": [ + "R4581", + "R4532", + "R7906", + "R6741" + ], + "R1374": [ + "R7470", + "R6861" + ], + "R6861": [ + "R1374", + "R5443" + ], + "R5443": [ + "R5907", + "R6861" + ], + "R5907": [ + "R2920", + "R5443" + ], + "R2920": [ + "R6600", + "R5907" + ], + "R6600": [ + "R2920", + "R6457" + ], + "R6457": [ + "R1459", + "R6600" + ], + "R1459": [ + "R6340", + "R6457" + ], + "R6340": [ + "R1099", + "R1459" + ], + "R1099": [ + "R1331", + "R6340", + "R5261", + "R5433" + ], + "R3396": [ + "R4939", + "R6121" + ], + "R6121": [ + "R3396", + "R1928" + ], + "R1928": [ + "R6121", + "R5757" + ], + "R5757": [ + "R1928", + "R8295" + ], + "R8295": [ + "R5757", + "R8263" + ], + "R8263": [ + "R8295", + "R1205" + ], + "R1205": [ + "R5723", + "R8263" + ], + "R5723": [ + "R1205", + "R3999" + ], + "R3999": [ + "R8192", + "R5723" + ], + "R8192": [ + "R3905", + "R9295", + "R3999", + "R5994" + ], + "R1778": [ + "R9229", + "R4939" + ], + "R9229": [ + "R8510", + "R1778" + ], + "R8510": [ + "R9229", + "R1424" + ], + "R1424": [ + "R3369", + "R8510" + ], + "R3369": [ + "R1424", + "R7385" + ], + "R7385": [ + "R6102", + "R3369" + ], + "R6102": [ + "R7385", + "R7483" + ], + "R7483": [ + "R6102", + "R3877" + ], + "R3877": [ + "R7483", + "R5494" + ], + "R5494": [ + "R5713", + "R1767", + "R3877", + "R7528" + ], + "R2699": [ + "R2164", + "R4939" + ], + "R2164": [ + "R7074", + "R2699" + ], + "R7074": [ + "R4195", + "R2164" + ], + "R4195": [ + "R7074", + "R9179" + ], + "R9179": [ + "R7241", + "R4195" + ], + "R7241": [ + "R9179", + "R3978" + ], + "R3978": [ + "R2742", + "R7241" + ], + "R2742": [ + "R5419", + "R3978" + ], + "R5419": [ + "R2742", + "R1805" + ], + "R1805": [ + "R1286", + "R5419", + "R3658", + "R4012" + ], + "R7906": [ + "R1168", + "R7597" + ], + "R7597": [ + "R7906", + "R3099" + ], + "R3099": [ + "R7597", + "R2075" + ], + "R2075": [ + "R3099", + "R9899" + ], + "R9899": [ + "R2075", + "R8731" + ], + "R8731": [ + "R4382", + "R9899" + ], + "R4382": [ + "R8737", + "R8731" + ], + "R8737": [ + "R4382", + "R6451" + ], + "R6451": [ + "R8737", + "R9100" + ], + "R9100": [ + "R6451", + "R6257", + "R8203", + "R7913" + ], + "R6741": [ + "R6614", + "R1168" + ], + "R6614": [ + "R6741", + "R6957" + ], + "R6957": [ + "R6614", + "R7735" + ], + "R7735": [ + "R6957", + "R6311" + ], + "R6311": [ + "R5529", + "R7735" + ], + "R5529": [ + "R8772", + "R6311" + ], + "R8772": [ + "R6539", + "R5529" + ], + "R6539": [ + "R8772", + "R9807" + ], + "R9807": [ + "R6539", + "R7426" + ], + "R7426": [ + "R9807", + "R4163", + "R8146", + "R9573" + ], + "R4532": [ + "R1168", + "R7690" + ], + "R7690": [ + "R4532", + "R1615" + ], + "R1615": [ + "R7690", + "R6930" + ], + "R6930": [ + "R1615", + "R4556" + ], + "R4556": [ + "R6930", + "R2399" + ], + "R2399": [ + "R4556", + "R6816" + ], + "R6816": [ + "R3955", + "R2399" + ], + "R3955": [ + "R6816", + "R6325" + ], + "R6325": [ + "R3955", + "R6894" + ], + "R6894": [ + "R6833", + "R6325", + "R6631", + "R6192" + ], + "R5261": [ + "R2092", + "R1099" + ], + "R2092": [ + "R5261", + "R8504" + ], + "R8504": [ + "R9464", + "R2092" + ], + "R9464": [ + "R4917", + "R8504" + ], + "R4917": [ + "R9589", + "R9464" + ], + "R9589": [ + "R4917", + "R1635" + ], + "R1635": [ + "R8629", + "R9589" + ], + "R8629": [ + "R1635", + "R6963" + ], + "R6963": [ + "R1737", + "R8629" + ], + "R1737": [ + "R7122", + "R7381", + "R6189", + "R6963" + ], + "R1331": [ + "R9183", + "R1099" + ], + "R9183": [ + "R1381", + "R1331" + ], + "R1381": [ + "R7797", + "R9183" + ], + "R7797": [ + "R6395", + "R1381" + ], + "R6395": [ + "R7797", + "R9141" + ], + "R9141": [ + "R6395", + "R3717" + ], + "R3717": [ + "R3325", + "R9141" + ], + "R3325": [ + "R1932", + "R3717" + ], + "R1932": [ + "R4857", + "R3325" + ], + "R4857": [ + "R3683", + "R3133", + "R1932", + "R9287" + ], + "R5433": [ + "R2015", + "R1099" + ], + "R2015": [ + "R1326", + "R5433" + ], + "R1326": [ + "R8466", + "R2015" + ], + "R8466": [ + "R4626", + "R1326" + ], + "R4626": [ + "R9275", + "R8466" + ], + "R9275": [ + "R5303", + "R4626" + ], + "R5303": [ + "R9275", + "R5238" + ], + "R5238": [ + "R5303", + "R9491" + ], + "R9491": [ + "R7864", + "R5238" + ], + "R7864": [ + "R9491", + "R3947", + "R6203", + "R8424" + ], + "R3905": [ + "R8192", + "R1291" + ], + "R1291": [ + "R3375", + "R3905" + ], + "R3375": [ + "R2596", + "R1291" + ], + "R2596": [ + "R2270", + "R3375" + ], + "R2270": [ + "R2596", + "R5315" + ], + "R5315": [ + "R2270", + "R7206" + ], + "R7206": [ + "R7960", + "R5315" + ], + "R7960": [ + "R2708", + "R7206" + ], + "R2708": [ + "R7960", + "R4616" + ], + "R4616": [ + "R2708" + ], + "R9295": [ + "R8192", + "R9447" + ], + "R9447": [ + "R4961", + "R9295" + ], + "R4961": [ + "R6482", + "R9447" + ], + "R6482": [ + "R3827", + "R4961" + ], + "R3827": [ + "R5584", + "R6482" + ], + "R5584": [ + "R3827", + "R3567" + ], + "R3567": [ + "R8493", + "R5584" + ], + "R8493": [ + "R3567", + "R1436" + ], + "R1436": [ + "R7058", + "R8493" + ], + "R7058": [ + "R1436" + ], + "R5994": [ + "R8192", + "R5668" + ], + "R5668": [ + "R5994", + "R3302" + ], + "R3302": [ + "R1359", + "R5668" + ], + "R1359": [ + "R2118", + "R3302" + ], + "R2118": [ + "R2996", + "R1359" + ], + "R2996": [ + "R2118", + "R4437" + ], + "R4437": [ + "R7227", + "R2996" + ], + "R7227": [ + "R4437", + "R7232" + ], + "R7232": [ + "R8214", + "R7227" + ], + "R8214": [ + "R7232" + ], + "R7528": [ + "R9876", + "R5494" + ], + "R9876": [ + "R7528", + "R5993" + ], + "R5993": [ + "R9876", + "R2406" + ], + "R2406": [ + "R3430", + "R5993" + ], + "R3430": [ + "R9953", + "R2406" + ], + "R9953": [ + "R3430", + "R5222" + ], + "R5222": [ + "R9953", + "R3598" + ], + "R3598": [ + "R5222", + "R6442" + ], + "R6442": [ + "R5888", + "R3598" + ], + "R5888": [ + "R6442" + ], + "R1767": [ + "R5494", + "R4962" + ], + "R4962": [ + "R1767", + "R9667" + ], + "R9667": [ + "R4962", + "R4749" + ], + "R4749": [ + "R9667", + "R7246" + ], + "R7246": [ + "R5311", + "R4749" + ], + "R5311": [ + "R7246", + "R6746" + ], + "R6746": [ + "R8272", + "R5311" + ], + "R8272": [ + "R6746", + "R9220" + ], + "R9220": [ + "R1729", + "R8272" + ], + "R1729": [ + "R9220" + ], + "R5713": [ + "R2995", + "R5494" + ], + "R2995": [ + "R5416", + "R5713" + ], + "R5416": [ + "R2995", + "R7307" + ], + "R7307": [ + "R5416", + "R2094" + ], + "R2094": [ + "R6742", + "R7307" + ], + "R6742": [ + "R8521", + "R2094" + ], + "R8521": [ + "R5165", + "R6742" + ], + "R5165": [ + "R3357", + "R8521" + ], + "R3357": [ + "R8234", + "R5165" + ], + "R8234": [ + "R3357" + ], + "R1286": [ + "R1805", + "R5693" + ], + "R5693": [ + "R4906", + "R1286" + ], + "R4906": [ + "R5693", + "R9212" + ], + "R9212": [ + "R3197", + "R4906" + ], + "R3197": [ + "R9212", + "R2277" + ], + "R2277": [ + "R3197", + "R8863" + ], + "R8863": [ + "R6162", + "R2277" + ], + "R6162": [ + "R2097", + "R8863" + ], + "R2097": [ + "R5109", + "R6162" + ], + "R5109": [ + "R2097" + ], + "R4012": [ + "R1805", + "R2654" + ], + "R2654": [ + "R4012", + "R9391" + ], + "R9391": [ + "R2654", + "R7331" + ], + "R7331": [ + "R8465", + "R9391" + ], + "R8465": [ + "R7331", + "R1978" + ], + "R1978": [ + "R8465", + "R7837" + ], + "R7837": [ + "R1978", + "R6198" + ], + "R6198": [ + "R7837", + "R6921" + ], + "R6921": [ + "R6198", + "R9967" + ], + "R9967": [ + "R6921" + ], + "R3658": [ + "R1805", + "R3560" + ], + "R3560": [ + "R9314", + "R3658" + ], + "R9314": [ + "R6809", + "R3560" + ], + "R6809": [ + "R7560", + "R9314" + ], + "R7560": [ + "R6809", + "R1763" + ], + "R1763": [ + "R7560", + "R6146" + ], + "R6146": [ + "R4997", + "R1763" + ], + "R4997": [ + "R8865", + "R6146" + ], + "R8865": [ + "R2679", + "R4997" + ], + "R2679": [ + "R8865" + ], + "R8203": [ + "R9100", + "R1636" + ], + "R1636": [ + "R7984", + "R8203" + ], + "R7984": [ + "R1636", + "R9283" + ], + "R9283": [ + "R7984", + "R2539" + ], + "R2539": [ + "R6959", + "R9283" + ], + "R6959": [ + "R1632", + "R2539" + ], + "R1632": [ + "R1582", + "R6959" + ], + "R1582": [ + "R3422", + "R1632" + ], + "R3422": [ + "R1582", + "R4956" + ], + "R4956": [ + "R3422" + ], + "R6257": [ + "R8267", + "R9100" + ], + "R8267": [ + "R6715", + "R6257" + ], + "R6715": [ + "R8267", + "R7226" + ], + "R7226": [ + "R3795", + "R6715" + ], + "R3795": [ + "R3374", + "R7226" + ], + "R3374": [ + "R8989", + "R3795" + ], + "R8989": [ + "R3374", + "R5396" + ], + "R5396": [ + "R8403", + "R8989" + ], + "R8403": [ + "R8172", + "R5396" + ], + "R8172": [ + "R8403" + ], + "R7913": [ + "R9100", + "R8515" + ], + "R8515": [ + "R7913", + "R5327" + ], + "R5327": [ + "R9090", + "R8515" + ], + "R9090": [ + "R2338", + "R5327" + ], + "R2338": [ + "R9090", + "R6418" + ], + "R6418": [ + "R4709", + "R2338" + ], + "R4709": [ + "R6418", + "R6919" + ], + "R6919": [ + "R4709", + "R1598" + ], + "R1598": [ + "R6919", + "R3041" + ], + "R3041": [ + "R1598" + ], + "R4163": [ + "R7426", + "R4402" + ], + "R4402": [ + "R4163", + "R9603" + ], + "R9603": [ + "R3922", + "R4402" + ], + "R3922": [ + "R9603", + "R4102" + ], + "R4102": [ + "R8928", + "R3922" + ], + "R8928": [ + "R7233", + "R4102" + ], + "R7233": [ + "R8928", + "R7798" + ], + "R7798": [ + "R1521", + "R7233" + ], + "R1521": [ + "R7798", + "R5374" + ], + "R5374": [ + "R1521" + ], + "R9573": [ + "R7426", + "R7936" + ], + "R7936": [ + "R9573", + "R5101" + ], + "R5101": [ + "R7936", + "R1685" + ], + "R1685": [ + "R5101", + "R5245" + ], + "R5245": [ + "R1685", + "R7127" + ], + "R7127": [ + "R5245", + "R4538" + ], + "R4538": [ + "R7127", + "R5988" + ], + "R5988": [ + "R7979", + "R4538" + ], + "R7979": [ + "R5988", + "R3599" + ], + "R3599": [ + "R7979" + ], + "R8146": [ + "R4621", + "R7426" + ], + "R4621": [ + "R8146", + "R3719" + ], + "R3719": [ + "R4621", + "R1918" + ], + "R1918": [ + "R3719", + "R1779" + ], + "R1779": [ + "R5233", + "R1918" + ], + "R5233": [ + "R1779", + "R9080" + ], + "R9080": [ + "R3128", + "R5233" + ], + "R3128": [ + "R9277", + "R9080" + ], + "R9277": [ + "R3128", + "R1190" + ], + "R1190": [ + "R9277" + ], + "R6833": [ + "R9458", + "R6894" + ], + "R9458": [ + "R6833", + "R1797" + ], + "R1797": [ + "R9458", + "R6988" + ], + "R6988": [ + "R1797", + "R5313" + ], + "R5313": [ + "R6052", + "R6988" + ], + "R6052": [ + "R7367", + "R5313" + ], + "R7367": [ + "R8665", + "R6052" + ], + "R8665": [ + "R7367", + "R9829" + ], + "R9829": [ + "R8665", + "R4665" + ], + "R4665": [ + "R9829" + ], + "R6192": [ + "R9251", + "R6894" + ], + "R9251": [ + "R3976", + "R6192" + ], + "R3976": [ + "R9251", + "R8937" + ], + "R8937": [ + "R3976", + "R9110" + ], + "R9110": [ + "R8937", + "R4346" + ], + "R4346": [ + "R1362", + "R9110" + ], + "R1362": [ + "R4093", + "R4346" + ], + "R4093": [ + "R1362", + "R8078" + ], + "R8078": [ + "R4093", + "R9137" + ], + "R9137": [ + "R8078" + ], + "R6631": [ + "R7969", + "R6894" + ], + "R7969": [ + "R6631", + "R7643" + ], + "R7643": [ + "R7969", + "R2073" + ], + "R2073": [ + "R9952", + "R7643" + ], + "R9952": [ + "R3500", + "R2073" + ], + "R3500": [ + "R9952", + "R1025" + ], + "R1025": [ + "R5201", + "R3500" + ], + "R5201": [ + "R4946", + "R1025" + ], + "R4946": [ + "R1254", + "R5201" + ], + "R1254": [ + "R4946" + ], + "R7381": [ + "R1737", + "R5630" + ], + "R5630": [ + "R7381", + "R7807" + ], + "R7807": [ + "R8171", + "R5630" + ], + "R8171": [ + "R7807", + "R2631" + ], + "R2631": [ + "R8282", + "R8171" + ], + "R8282": [ + "R2631", + "R8882" + ], + "R8882": [ + "R8282", + "R3561" + ], + "R3561": [ + "R2559", + "R8882" + ], + "R2559": [ + "R3561", + "R4701" + ], + "R4701": [ + "R2559" + ], + "R7122": [ + "R7240", + "R1737" + ], + "R7240": [ + "R7122", + "R7087" + ], + "R7087": [ + "R7240", + "R3504" + ], + "R3504": [ + "R7087", + "R6079" + ], + "R6079": [ + "R3504", + "R4515" + ], + "R4515": [ + "R6079", + "R1277" + ], + "R1277": [ + "R4515", + "R2146" + ], + "R2146": [ + "R1277", + "R9535" + ], + "R9535": [ + "R2146", + "R1657" + ], + "R1657": [ + "R9535" + ], + "R6189": [ + "R2205", + "R1737" + ], + "R2205": [ + "R7769", + "R6189" + ], + "R7769": [ + "R2205", + "R3864" + ], + "R3864": [ + "R7769", + "R9988" + ], + "R9988": [ + "R3864", + "R6743" + ], + "R6743": [ + "R9988", + "R5125" + ], + "R5125": [ + "R6743", + "R4231" + ], + "R4231": [ + "R6831", + "R5125" + ], + "R6831": [ + "R1052", + "R4231" + ], + "R1052": [ + "R6831" + ], + "R3683": [ + "R5960", + "R4857" + ], + "R5960": [ + "R9364", + "R3683" + ], + "R9364": [ + "R3942", + "R5960" + ], + "R3942": [ + "R1948", + "R9364" + ], + "R1948": [ + "R3942", + "R4970" + ], + "R4970": [ + "R1948", + "R8621" + ], + "R8621": [ + "R4970", + "R2383" + ], + "R2383": [ + "R8621", + "R8796" + ], + "R8796": [ + "R5856", + "R2383" + ], + "R5856": [ + "R8796" + ], + "R9287": [ + "R8878", + "R4857" + ], + "R8878": [ + "R7346", + "R9287" + ], + "R7346": [ + "R9134", + "R8878" + ], + "R9134": [ + "R7346", + "R7642" + ], + "R7642": [ + "R7375", + "R9134" + ], + "R7375": [ + "R7642", + "R4417" + ], + "R4417": [ + "R7375", + "R7263" + ], + "R7263": [ + "R1733", + "R4417" + ], + "R1733": [ + "R7263", + "R5473" + ], + "R5473": [ + "R1733" + ], + "R3133": [ + "R6473", + "R4857" + ], + "R6473": [ + "R3133", + "R9012" + ], + "R9012": [ + "R6473", + "R9582" + ], + "R9582": [ + "R9012", + "R9803" + ], + "R9803": [ + "R9582", + "R1252" + ], + "R1252": [ + "R9803", + "R3893" + ], + "R3893": [ + "R1252", + "R3429" + ], + "R3429": [ + "R3893", + "R1004" + ], + "R1004": [ + "R8949", + "R3429" + ], + "R8949": [ + "R1004" + ], + "R8424": [ + "R7864", + "R9778" + ], + "R9778": [ + "R1327", + "R8424" + ], + "R1327": [ + "R7456", + "R9778" + ], + "R7456": [ + "R9490", + "R1327" + ], + "R9490": [ + "R7456", + "R4461" + ], + "R4461": [ + "R9490", + "R7839" + ], + "R7839": [ + "R4461", + "R1885" + ], + "R1885": [ + "R7839", + "R5220" + ], + "R5220": [ + "R1885", + "R6181" + ], + "R6181": [ + "R5220" + ], + "R3947": [ + "R8617", + "R7864" + ], + "R8617": [ + "R3947", + "R2481" + ], + "R2481": [ + "R8617", + "R8802" + ], + "R8802": [ + "R2481", + "R2719" + ], + "R2719": [ + "R8802", + "R2667" + ], + "R2667": [ + "R2719", + "R6477" + ], + "R6477": [ + "R2667", + "R2556" + ], + "R2556": [ + "R5193", + "R6477" + ], + "R5193": [ + "R2556", + "R2362" + ], + "R2362": [ + "R5193" + ], + "R6203": [ + "R6900", + "R7864" + ], + "R6900": [ + "R7275", + "R6203" + ], + "R7275": [ + "R6900", + "R2661" + ], + "R2661": [ + "R7275", + "R3838" + ], + "R3838": [ + "R2847", + "R2661" + ], + "R2847": [ + "R9370", + "R3838" + ], + "R9370": [ + "R2847", + "R9937" + ], + "R9937": [ + "R9370", + "R4816" + ], + "R4816": [ + "R9937", + "R5849" + ], + "R5849": [ + "R4816" + ] + } +} diff --git a/tasks/maze-t27/instruction.md b/tasks/maze-t27/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t27/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t27/solution/solve.sh b/tasks/maze-t27/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..7ccebcd45d95b9f25f1db7c3f34879cf98ceb317 --- /dev/null +++ b/tasks/maze-t27/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R3225 R4339 R2035 R1532 R4085 R3582 R1806 R2419 R4581 R1168 R6741 R6614 R6957 R7735 R6311 R5529 R8772 R6539 R9807 R7426 R4163 R4402 R9603 R3922 R4102 R8928 R7233 R7798 R1521 R5374; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t27/task.toml b/tasks/maze-t27/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..55bf769c708c0c859b561ea09e1a9729a13199f6 --- /dev/null +++ b/tasks/maze-t27/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t27" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 27 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t27/tests/Dockerfile b/tasks/maze-t27/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t27/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t27/tests/expected.json b/tasks/maze-t27/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..b19d2e6c1cffffa4d26d379884f48e4106f655a1 --- /dev/null +++ b/tasks/maze-t27/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R7470", + "exit": "R5374", + "budget": 210, + "tree_sha256": "33086d9df274edd17d5c93b708c8ecd749075c7f38ed64541b32ed638fa7afe9" +} diff --git a/tasks/maze-t27/tests/grade.py b/tasks/maze-t27/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t27/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t27/tests/test.sh b/tasks/maze-t27/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t27/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t28/environment/agent/Dockerfile b/tasks/maze-t28/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t28/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t28/environment/agent/maze b/tasks/maze-t28/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t28/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t28/environment/docker-compose.yaml b/tasks/maze-t28/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..18a5f8939885a27602488a44060aa9f462d43b3e --- /dev/null +++ b/tasks/maze-t28/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t28: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t28-8c52424bc718 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t28/environment/maze/Dockerfile b/tasks/maze-t28/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t28/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t28/environment/maze/engine.py b/tasks/maze-t28/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t28/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t28/environment/maze/export.py b/tasks/maze-t28/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t28/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t28/environment/maze/service.py b/tasks/maze-t28/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t28/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t28/environment/maze/success.py b/tasks/maze-t28/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t28/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t28/environment/maze/tree.json b/tasks/maze-t28/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..69e405f03e55a66de82182583d3ede12b21814b6 --- /dev/null +++ b/tasks/maze-t28/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R2874", + "exit": "R8122", + "budget": 210, + "doors": { + "R2874": [ + "R4594", + "R3693", + "R6773" + ], + "R3693": [ + "R8748", + "R2874" + ], + "R8748": [ + "R3693", + "R7411" + ], + "R7411": [ + "R8748", + "R4291" + ], + "R4291": [ + "R7411", + "R5043" + ], + "R5043": [ + "R7523", + "R4291" + ], + "R7523": [ + "R7460", + "R5043" + ], + "R7460": [ + "R3700", + "R7523" + ], + "R3700": [ + "R7460", + "R1362" + ], + "R1362": [ + "R8448", + "R3700" + ], + "R8448": [ + "R3723", + "R1362", + "R9255", + "R5825" + ], + "R6773": [ + "R9847", + "R2874" + ], + "R9847": [ + "R5613", + "R6773" + ], + "R5613": [ + "R9489", + "R9847" + ], + "R9489": [ + "R5613", + "R4714" + ], + "R4714": [ + "R6633", + "R9489" + ], + "R6633": [ + "R4714", + "R6259" + ], + "R6259": [ + "R6633", + "R3662" + ], + "R3662": [ + "R6259", + "R1934" + ], + "R1934": [ + "R7693", + "R3662" + ], + "R7693": [ + "R2020", + "R4293", + "R7416", + "R1934" + ], + "R4594": [ + "R5016", + "R2874" + ], + "R5016": [ + "R4594", + "R2478" + ], + "R2478": [ + "R7348", + "R5016" + ], + "R7348": [ + "R2478", + "R3619" + ], + "R3619": [ + "R7348", + "R8346" + ], + "R8346": [ + "R3619", + "R3475" + ], + "R3475": [ + "R7389", + "R8346" + ], + "R7389": [ + "R7999", + "R3475" + ], + "R7999": [ + "R7389", + "R8104" + ], + "R8104": [ + "R4705", + "R1665", + "R7438", + "R7999" + ], + "R5825": [ + "R9456", + "R8448" + ], + "R9456": [ + "R5825", + "R5485" + ], + "R5485": [ + "R7181", + "R9456" + ], + "R7181": [ + "R5485", + "R2382" + ], + "R2382": [ + "R7644", + "R7181" + ], + "R7644": [ + "R2382", + "R2005" + ], + "R2005": [ + "R7644", + "R5918" + ], + "R5918": [ + "R5652", + "R2005" + ], + "R5652": [ + "R5641", + "R5918" + ], + "R5641": [ + "R3811", + "R3770", + "R1579", + "R5652" + ], + "R9255": [ + "R8093", + "R8448" + ], + "R8093": [ + "R4341", + "R9255" + ], + "R4341": [ + "R8093", + "R2280" + ], + "R2280": [ + "R4341", + "R6718" + ], + "R6718": [ + "R2280", + "R1649" + ], + "R1649": [ + "R6718", + "R7145" + ], + "R7145": [ + "R2728", + "R1649" + ], + "R2728": [ + "R7145", + "R4286" + ], + "R4286": [ + "R8171", + "R2728" + ], + "R8171": [ + "R7808", + "R4286", + "R1144", + "R3234" + ], + "R3723": [ + "R9841", + "R8448" + ], + "R9841": [ + "R3316", + "R3723" + ], + "R3316": [ + "R9841", + "R3774" + ], + "R3774": [ + "R3316", + "R6575" + ], + "R6575": [ + "R3774", + "R7146" + ], + "R7146": [ + "R2715", + "R6575" + ], + "R2715": [ + "R9372", + "R7146" + ], + "R9372": [ + "R7749", + "R2715" + ], + "R7749": [ + "R9446", + "R9372" + ], + "R9446": [ + "R9904", + "R2658", + "R7749", + "R7671" + ], + "R7416": [ + "R7693", + "R1614" + ], + "R1614": [ + "R7928", + "R7416" + ], + "R7928": [ + "R1614", + "R3348" + ], + "R3348": [ + "R4195", + "R7928" + ], + "R4195": [ + "R7215", + "R3348" + ], + "R7215": [ + "R5365", + "R4195" + ], + "R5365": [ + "R3907", + "R7215" + ], + "R3907": [ + "R5365", + "R3549" + ], + "R3549": [ + "R3907", + "R8552" + ], + "R8552": [ + "R7699", + "R3549", + "R1633", + "R4703" + ], + "R2020": [ + "R3200", + "R7693" + ], + "R3200": [ + "R2020", + "R4734" + ], + "R4734": [ + "R3200", + "R4937" + ], + "R4937": [ + "R1616", + "R4734" + ], + "R1616": [ + "R4937", + "R5375" + ], + "R5375": [ + "R1616", + "R9923" + ], + "R9923": [ + "R8260", + "R5375" + ], + "R8260": [ + "R9767", + "R9923" + ], + "R9767": [ + "R8260", + "R8091" + ], + "R8091": [ + "R1435", + "R9767", + "R8958", + "R4518" + ], + "R4293": [ + "R1615", + "R7693" + ], + "R1615": [ + "R4293", + "R8760" + ], + "R8760": [ + "R1615", + "R6353" + ], + "R6353": [ + "R4664", + "R8760" + ], + "R4664": [ + "R6353", + "R5800" + ], + "R5800": [ + "R2460", + "R4664" + ], + "R2460": [ + "R8063", + "R5800" + ], + "R8063": [ + "R2460", + "R1827" + ], + "R1827": [ + "R6954", + "R8063" + ], + "R6954": [ + "R1827", + "R7367", + "R7653", + "R9213" + ], + "R7438": [ + "R1830", + "R8104" + ], + "R1830": [ + "R5953", + "R7438" + ], + "R5953": [ + "R1830", + "R4915" + ], + "R4915": [ + "R5953", + "R1676" + ], + "R1676": [ + "R8347", + "R4915" + ], + "R8347": [ + "R8942", + "R1676" + ], + "R8942": [ + "R8347", + "R1278" + ], + "R1278": [ + "R3800", + "R8942" + ], + "R3800": [ + "R1278", + "R9724" + ], + "R9724": [ + "R6252", + "R3800", + "R4918", + "R4481" + ], + "R1665": [ + "R6556", + "R8104" + ], + "R6556": [ + "R1665", + "R5356" + ], + "R5356": [ + "R6556", + "R2138" + ], + "R2138": [ + "R1780", + "R5356" + ], + "R1780": [ + "R2138", + "R9008" + ], + "R9008": [ + "R1780", + "R6987" + ], + "R6987": [ + "R4844", + "R9008" + ], + "R4844": [ + "R8866", + "R6987" + ], + "R8866": [ + "R7087", + "R4844" + ], + "R7087": [ + "R3310", + "R9741", + "R8866", + "R9174" + ], + "R4705": [ + "R4739", + "R8104" + ], + "R4739": [ + "R4705", + "R3529" + ], + "R3529": [ + "R4739", + "R4627" + ], + "R4627": [ + "R7775", + "R3529" + ], + "R7775": [ + "R3580", + "R4627" + ], + "R3580": [ + "R7775", + "R8857" + ], + "R8857": [ + "R3580", + "R4716" + ], + "R4716": [ + "R2615", + "R8857" + ], + "R2615": [ + "R4716", + "R6149" + ], + "R6149": [ + "R6486", + "R6059", + "R8344", + "R2615" + ], + "R3770": [ + "R5641", + "R6661" + ], + "R6661": [ + "R3770", + "R7478" + ], + "R7478": [ + "R4687", + "R6661" + ], + "R4687": [ + "R9494", + "R7478" + ], + "R9494": [ + "R4687", + "R5661" + ], + "R5661": [ + "R4109", + "R9494" + ], + "R4109": [ + "R5661", + "R5347" + ], + "R5347": [ + "R4109", + "R9067" + ], + "R9067": [ + "R5637", + "R5347" + ], + "R5637": [ + "R9067" + ], + "R1579": [ + "R5641", + "R5110" + ], + "R5110": [ + "R6398", + "R1579" + ], + "R6398": [ + "R3177", + "R5110" + ], + "R3177": [ + "R7139", + "R6398" + ], + "R7139": [ + "R7650", + "R3177" + ], + "R7650": [ + "R7139", + "R3160" + ], + "R3160": [ + "R5812", + "R7650" + ], + "R5812": [ + "R6235", + "R3160" + ], + "R6235": [ + "R5812", + "R8422" + ], + "R8422": [ + "R6235" + ], + "R3811": [ + "R5641", + "R9684" + ], + "R9684": [ + "R9356", + "R3811" + ], + "R9356": [ + "R9684", + "R8186" + ], + "R8186": [ + "R8094", + "R9356" + ], + "R8094": [ + "R4568", + "R8186" + ], + "R4568": [ + "R8094", + "R7031" + ], + "R7031": [ + "R4228", + "R4568" + ], + "R4228": [ + "R7031", + "R6673" + ], + "R6673": [ + "R5227", + "R4228" + ], + "R5227": [ + "R6673" + ], + "R7808": [ + "R9294", + "R8171" + ], + "R9294": [ + "R7808", + "R5869" + ], + "R5869": [ + "R2094", + "R9294" + ], + "R2094": [ + "R5869", + "R7665" + ], + "R7665": [ + "R8176", + "R2094" + ], + "R8176": [ + "R7665", + "R7407" + ], + "R7407": [ + "R8176", + "R2851" + ], + "R2851": [ + "R7407", + "R8337" + ], + "R8337": [ + "R3366", + "R2851" + ], + "R3366": [ + "R8337" + ], + "R1144": [ + "R8171", + "R2449" + ], + "R2449": [ + "R1144", + "R7117" + ], + "R7117": [ + "R2449", + "R5489" + ], + "R5489": [ + "R4358", + "R7117" + ], + "R4358": [ + "R6770", + "R5489" + ], + "R6770": [ + "R4458", + "R4358" + ], + "R4458": [ + "R6770", + "R8042" + ], + "R8042": [ + "R4458", + "R7130" + ], + "R7130": [ + "R8042", + "R6888" + ], + "R6888": [ + "R7130" + ], + "R3234": [ + "R8171", + "R7393" + ], + "R7393": [ + "R3689", + "R3234" + ], + "R3689": [ + "R8324", + "R7393" + ], + "R8324": [ + "R2980", + "R3689" + ], + "R2980": [ + "R5316", + "R8324" + ], + "R5316": [ + "R2980", + "R7068" + ], + "R7068": [ + "R5316", + "R5620" + ], + "R5620": [ + "R7730", + "R7068" + ], + "R7730": [ + "R6749", + "R5620" + ], + "R6749": [ + "R7730" + ], + "R2658": [ + "R9446", + "R3956" + ], + "R3956": [ + "R2658", + "R6819" + ], + "R6819": [ + "R3956", + "R7311" + ], + "R7311": [ + "R8048", + "R6819" + ], + "R8048": [ + "R7311", + "R5908" + ], + "R5908": [ + "R8048", + "R6387" + ], + "R6387": [ + "R1397", + "R5908" + ], + "R1397": [ + "R6387", + "R7551" + ], + "R7551": [ + "R4588", + "R1397" + ], + "R4588": [ + "R7551" + ], + "R7671": [ + "R1215", + "R9446" + ], + "R1215": [ + "R6949", + "R7671" + ], + "R6949": [ + "R2088", + "R1215" + ], + "R2088": [ + "R7675", + "R6949" + ], + "R7675": [ + "R2088", + "R6209" + ], + "R6209": [ + "R2051", + "R7675" + ], + "R2051": [ + "R6209", + "R3411" + ], + "R3411": [ + "R4177", + "R2051" + ], + "R4177": [ + "R6936", + "R3411" + ], + "R6936": [ + "R4177" + ], + "R9904": [ + "R9446", + "R6314" + ], + "R6314": [ + "R9904", + "R7119" + ], + "R7119": [ + "R6314", + "R4274" + ], + "R4274": [ + "R5668", + "R7119" + ], + "R5668": [ + "R4274", + "R4012" + ], + "R4012": [ + "R5668", + "R8262" + ], + "R8262": [ + "R4012", + "R6847" + ], + "R6847": [ + "R1089", + "R8262" + ], + "R1089": [ + "R3465", + "R6847" + ], + "R3465": [ + "R1089" + ], + "R7699": [ + "R9358", + "R8552" + ], + "R9358": [ + "R7699", + "R9414" + ], + "R9414": [ + "R9358", + "R2419" + ], + "R2419": [ + "R9414", + "R3606" + ], + "R3606": [ + "R2419", + "R6337" + ], + "R6337": [ + "R2268", + "R3606" + ], + "R2268": [ + "R4084", + "R6337" + ], + "R4084": [ + "R9274", + "R2268" + ], + "R9274": [ + "R3079", + "R4084" + ], + "R3079": [ + "R9274" + ], + "R4703": [ + "R5294", + "R8552" + ], + "R5294": [ + "R4703", + "R7188" + ], + "R7188": [ + "R5294", + "R2998" + ], + "R2998": [ + "R2458", + "R7188" + ], + "R2458": [ + "R1146", + "R2998" + ], + "R1146": [ + "R2458", + "R4130" + ], + "R4130": [ + "R7375", + "R1146" + ], + "R7375": [ + "R4130", + "R8309" + ], + "R8309": [ + "R6711", + "R7375" + ], + "R6711": [ + "R8309" + ], + "R1633": [ + "R8552", + "R1281" + ], + "R1281": [ + "R1633", + "R7394" + ], + "R7394": [ + "R2290", + "R1281" + ], + "R2290": [ + "R8199", + "R7394" + ], + "R8199": [ + "R5876", + "R2290" + ], + "R5876": [ + "R8199", + "R1976" + ], + "R1976": [ + "R1644", + "R5876" + ], + "R1644": [ + "R1976", + "R7840" + ], + "R7840": [ + "R1644", + "R7509" + ], + "R7509": [ + "R7840" + ], + "R4518": [ + "R8231", + "R8091" + ], + "R8231": [ + "R4518", + "R7285" + ], + "R7285": [ + "R4345", + "R8231" + ], + "R4345": [ + "R8620", + "R7285" + ], + "R8620": [ + "R4345", + "R7156" + ], + "R7156": [ + "R1486", + "R8620" + ], + "R1486": [ + "R9834", + "R7156" + ], + "R9834": [ + "R1486", + "R7446" + ], + "R7446": [ + "R8912", + "R9834" + ], + "R8912": [ + "R7446" + ], + "R8958": [ + "R7485", + "R8091" + ], + "R7485": [ + "R7698", + "R8958" + ], + "R7698": [ + "R2244", + "R7485" + ], + "R2244": [ + "R5344", + "R7698" + ], + "R5344": [ + "R2244", + "R2648" + ], + "R2648": [ + "R5344", + "R3305" + ], + "R3305": [ + "R2648", + "R8035" + ], + "R8035": [ + "R3305", + "R3822" + ], + "R3822": [ + "R3871", + "R8035" + ], + "R3871": [ + "R3822" + ], + "R1435": [ + "R8091", + "R5445" + ], + "R5445": [ + "R4974", + "R1435" + ], + "R4974": [ + "R5445", + "R1772" + ], + "R1772": [ + "R9344", + "R4974" + ], + "R9344": [ + "R7660", + "R1772" + ], + "R7660": [ + "R3347", + "R9344" + ], + "R3347": [ + "R7660", + "R9163" + ], + "R9163": [ + "R7931", + "R3347" + ], + "R7931": [ + "R9163", + "R7614" + ], + "R7614": [ + "R7931" + ], + "R9213": [ + "R2866", + "R6954" + ], + "R2866": [ + "R9213", + "R2049" + ], + "R2049": [ + "R2866", + "R8402" + ], + "R8402": [ + "R2049", + "R7042" + ], + "R7042": [ + "R8402", + "R8947" + ], + "R8947": [ + "R7042", + "R2119" + ], + "R2119": [ + "R2115", + "R8947" + ], + "R2115": [ + "R2119", + "R3555" + ], + "R3555": [ + "R2115", + "R3648" + ], + "R3648": [ + "R3555" + ], + "R7653": [ + "R6954", + "R7640" + ], + "R7640": [ + "R7653", + "R9425" + ], + "R9425": [ + "R4022", + "R7640" + ], + "R4022": [ + "R8292", + "R9425" + ], + "R8292": [ + "R1699", + "R4022" + ], + "R1699": [ + "R6334", + "R8292" + ], + "R6334": [ + "R1699", + "R4386" + ], + "R4386": [ + "R6334", + "R3002" + ], + "R3002": [ + "R4386", + "R2309" + ], + "R2309": [ + "R3002" + ], + "R7367": [ + "R6875", + "R6954" + ], + "R6875": [ + "R7046", + "R7367" + ], + "R7046": [ + "R6875", + "R5721" + ], + "R5721": [ + "R8408", + "R7046" + ], + "R8408": [ + "R5721", + "R8156" + ], + "R8156": [ + "R8408", + "R4980" + ], + "R4980": [ + "R4587", + "R8156" + ], + "R4587": [ + "R4980", + "R3138" + ], + "R3138": [ + "R6606", + "R4587" + ], + "R6606": [ + "R3138" + ], + "R6252": [ + "R4936", + "R9724" + ], + "R4936": [ + "R6252", + "R1016" + ], + "R1016": [ + "R4936", + "R4800" + ], + "R4800": [ + "R1016", + "R3124" + ], + "R3124": [ + "R4800", + "R1559" + ], + "R1559": [ + "R3124", + "R9212" + ], + "R9212": [ + "R9592", + "R1559" + ], + "R9592": [ + "R3078", + "R9212" + ], + "R3078": [ + "R9592", + "R3501" + ], + "R3501": [ + "R3078" + ], + "R4918": [ + "R9724", + "R4503" + ], + "R4503": [ + "R4918", + "R7603" + ], + "R7603": [ + "R6935", + "R4503" + ], + "R6935": [ + "R9896", + "R7603" + ], + "R9896": [ + "R4969", + "R6935" + ], + "R4969": [ + "R7852", + "R9896" + ], + "R7852": [ + "R4969", + "R2608" + ], + "R2608": [ + "R7852", + "R9843" + ], + "R9843": [ + "R2608", + "R9537" + ], + "R9537": [ + "R9843" + ], + "R4481": [ + "R9724", + "R4785" + ], + "R4785": [ + "R1522", + "R4481" + ], + "R1522": [ + "R4785", + "R2764" + ], + "R2764": [ + "R1522", + "R6388" + ], + "R6388": [ + "R2764", + "R7959" + ], + "R7959": [ + "R6478", + "R6388" + ], + "R6478": [ + "R7959", + "R6619" + ], + "R6619": [ + "R6478", + "R2852" + ], + "R2852": [ + "R6619", + "R2599" + ], + "R2599": [ + "R2852" + ], + "R9174": [ + "R7087", + "R6018" + ], + "R6018": [ + "R9174", + "R5858" + ], + "R5858": [ + "R6018", + "R3224" + ], + "R3224": [ + "R3462", + "R5858" + ], + "R3462": [ + "R3224", + "R5611" + ], + "R5611": [ + "R6915", + "R3462" + ], + "R6915": [ + "R5611", + "R7089" + ], + "R7089": [ + "R4365", + "R6915" + ], + "R4365": [ + "R3159", + "R7089" + ], + "R3159": [ + "R4365" + ], + "R9741": [ + "R3094", + "R7087" + ], + "R3094": [ + "R9741", + "R4961" + ], + "R4961": [ + "R3094", + "R4853" + ], + "R4853": [ + "R9977", + "R4961" + ], + "R9977": [ + "R4853", + "R9706" + ], + "R9706": [ + "R9977", + "R5994" + ], + "R5994": [ + "R9706", + "R8793" + ], + "R8793": [ + "R5994", + "R7462" + ], + "R7462": [ + "R8793", + "R4754" + ], + "R4754": [ + "R7462" + ], + "R3310": [ + "R7563", + "R7087" + ], + "R7563": [ + "R5423", + "R3310" + ], + "R5423": [ + "R7563", + "R7963" + ], + "R7963": [ + "R8150", + "R5423" + ], + "R8150": [ + "R9428", + "R7963" + ], + "R9428": [ + "R9639", + "R8150" + ], + "R9639": [ + "R9428", + "R5741" + ], + "R5741": [ + "R6246", + "R9639" + ], + "R6246": [ + "R4699", + "R5741" + ], + "R4699": [ + "R6246" + ], + "R8344": [ + "R6149", + "R6703" + ], + "R6703": [ + "R8344", + "R3768" + ], + "R3768": [ + "R8224", + "R6703" + ], + "R8224": [ + "R3768", + "R7134" + ], + "R7134": [ + "R7696", + "R8224" + ], + "R7696": [ + "R7134", + "R2582" + ], + "R2582": [ + "R5031", + "R7696" + ], + "R5031": [ + "R1419", + "R2582" + ], + "R1419": [ + "R7637", + "R5031" + ], + "R7637": [ + "R1419" + ], + "R6486": [ + "R6149", + "R9624" + ], + "R9624": [ + "R9596", + "R6486" + ], + "R9596": [ + "R9624", + "R7075" + ], + "R7075": [ + "R9596", + "R5959" + ], + "R5959": [ + "R7075", + "R9959" + ], + "R9959": [ + "R5959", + "R4256" + ], + "R4256": [ + "R9959", + "R9957" + ], + "R9957": [ + "R4256", + "R7667" + ], + "R7667": [ + "R8122", + "R9957" + ], + "R8122": [ + "R7667" + ], + "R6059": [ + "R8899", + "R6149" + ], + "R8899": [ + "R2742", + "R6059" + ], + "R2742": [ + "R8899", + "R5819" + ], + "R5819": [ + "R2979", + "R2742" + ], + "R2979": [ + "R5819", + "R1554" + ], + "R1554": [ + "R7457", + "R2979" + ], + "R7457": [ + "R4511", + "R1554" + ], + "R4511": [ + "R7457", + "R3772" + ], + "R3772": [ + "R1721", + "R4511" + ], + "R1721": [ + "R3772" + ] + } +} diff --git a/tasks/maze-t28/instruction.md b/tasks/maze-t28/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t28/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t28/solution/solve.sh b/tasks/maze-t28/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..7ea7115b892eeb1d55cde01ea4dbe218aa6c910c --- /dev/null +++ b/tasks/maze-t28/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R4594 R5016 R2478 R7348 R3619 R8346 R3475 R7389 R7999 R8104 R4705 R4739 R3529 R4627 R7775 R3580 R8857 R4716 R2615 R6149 R6486 R9624 R9596 R7075 R5959 R9959 R4256 R9957 R7667 R8122; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t28/task.toml b/tasks/maze-t28/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7de3abfe2229f1100ca6404393bcfe35a41274ac --- /dev/null +++ b/tasks/maze-t28/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t28" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 28 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t28/tests/Dockerfile b/tasks/maze-t28/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t28/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t28/tests/expected.json b/tasks/maze-t28/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..9b817919453f45e3a76d01264e9a28a3fdfa0b1a --- /dev/null +++ b/tasks/maze-t28/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R2874", + "exit": "R8122", + "budget": 210, + "tree_sha256": "ad4db76af49c0f63d5487f52bdf7d61f6c69d0f18b2a002aa7d9053ad7add5d0" +} diff --git a/tasks/maze-t28/tests/grade.py b/tasks/maze-t28/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t28/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t28/tests/test.sh b/tasks/maze-t28/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t28/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t29/environment/agent/Dockerfile b/tasks/maze-t29/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t29/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t29/environment/agent/maze b/tasks/maze-t29/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t29/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t29/environment/docker-compose.yaml b/tasks/maze-t29/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b887f8465832529b0e29111972a37b775a94241e --- /dev/null +++ b/tasks/maze-t29/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t29: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t29-e3e29e7b62d3 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t29/environment/maze/Dockerfile b/tasks/maze-t29/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t29/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t29/environment/maze/engine.py b/tasks/maze-t29/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t29/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t29/environment/maze/export.py b/tasks/maze-t29/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t29/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t29/environment/maze/service.py b/tasks/maze-t29/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t29/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t29/environment/maze/success.py b/tasks/maze-t29/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t29/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t29/environment/maze/tree.json b/tasks/maze-t29/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..f31e54c04af0507b130f1864a115aeb35496a1ea --- /dev/null +++ b/tasks/maze-t29/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R3284", + "exit": "R3826", + "budget": 210, + "doors": { + "R3284": [ + "R5821", + "R4548", + "R7462" + ], + "R5821": [ + "R7546", + "R3284" + ], + "R7546": [ + "R5821", + "R4861" + ], + "R4861": [ + "R7546", + "R6195" + ], + "R6195": [ + "R9167", + "R4861" + ], + "R9167": [ + "R6195", + "R9216" + ], + "R9216": [ + "R9167", + "R5136" + ], + "R5136": [ + "R9216", + "R5983" + ], + "R5983": [ + "R5136", + "R4145" + ], + "R4145": [ + "R9852", + "R5983" + ], + "R9852": [ + "R8529", + "R7571", + "R4145", + "R6450" + ], + "R4548": [ + "R3284", + "R6528" + ], + "R6528": [ + "R4548", + "R7823" + ], + "R7823": [ + "R6528", + "R3197" + ], + "R3197": [ + "R2831", + "R7823" + ], + "R2831": [ + "R6073", + "R3197" + ], + "R6073": [ + "R9336", + "R2831" + ], + "R9336": [ + "R6073", + "R8925" + ], + "R8925": [ + "R2244", + "R9336" + ], + "R2244": [ + "R3090", + "R8925" + ], + "R3090": [ + "R2244", + "R7197", + "R4141", + "R2297" + ], + "R7462": [ + "R3284", + "R4563" + ], + "R4563": [ + "R7462", + "R8910" + ], + "R8910": [ + "R2094", + "R4563" + ], + "R2094": [ + "R8910", + "R2242" + ], + "R2242": [ + "R1612", + "R2094" + ], + "R1612": [ + "R2242", + "R2708" + ], + "R2708": [ + "R1440", + "R1612" + ], + "R1440": [ + "R2708", + "R6736" + ], + "R6736": [ + "R1440", + "R2964" + ], + "R2964": [ + "R6270", + "R8585", + "R6736", + "R8979" + ], + "R6450": [ + "R9852", + "R9666" + ], + "R9666": [ + "R6450", + "R7729" + ], + "R7729": [ + "R3034", + "R9666" + ], + "R3034": [ + "R7867", + "R7729" + ], + "R7867": [ + "R3034", + "R3323" + ], + "R3323": [ + "R7867", + "R6778" + ], + "R6778": [ + "R4997", + "R3323" + ], + "R4997": [ + "R7211", + "R6778" + ], + "R7211": [ + "R6743", + "R4997" + ], + "R6743": [ + "R7211", + "R1134", + "R7880", + "R7541" + ], + "R7571": [ + "R9249", + "R9852" + ], + "R9249": [ + "R5346", + "R7571" + ], + "R5346": [ + "R9249", + "R4072" + ], + "R4072": [ + "R3390", + "R5346" + ], + "R3390": [ + "R4072", + "R7949" + ], + "R7949": [ + "R1093", + "R3390" + ], + "R1093": [ + "R5921", + "R7949" + ], + "R5921": [ + "R5639", + "R1093" + ], + "R5639": [ + "R5183", + "R5921" + ], + "R5183": [ + "R3760", + "R5980", + "R8101", + "R5639" + ], + "R8529": [ + "R9129", + "R9852" + ], + "R9129": [ + "R8529", + "R1836" + ], + "R1836": [ + "R9129", + "R6442" + ], + "R6442": [ + "R6069", + "R1836" + ], + "R6069": [ + "R1877", + "R6442" + ], + "R1877": [ + "R9672", + "R6069" + ], + "R9672": [ + "R1877", + "R9573" + ], + "R9573": [ + "R5196", + "R9672" + ], + "R5196": [ + "R9112", + "R9573" + ], + "R9112": [ + "R4222", + "R5740", + "R5196", + "R5105" + ], + "R7197": [ + "R3090", + "R8068" + ], + "R8068": [ + "R7197", + "R3055" + ], + "R3055": [ + "R6448", + "R8068" + ], + "R6448": [ + "R1619", + "R3055" + ], + "R1619": [ + "R6448", + "R1933" + ], + "R1933": [ + "R2800", + "R1619" + ], + "R2800": [ + "R7890", + "R1933" + ], + "R7890": [ + "R1543", + "R2800" + ], + "R1543": [ + "R9282", + "R7890" + ], + "R9282": [ + "R8276", + "R1543", + "R9703", + "R1678" + ], + "R4141": [ + "R5652", + "R3090" + ], + "R5652": [ + "R4049", + "R4141" + ], + "R4049": [ + "R8164", + "R5652" + ], + "R8164": [ + "R4049", + "R5106" + ], + "R5106": [ + "R6944", + "R8164" + ], + "R6944": [ + "R4718", + "R5106" + ], + "R4718": [ + "R4396", + "R6944" + ], + "R4396": [ + "R4718", + "R1593" + ], + "R1593": [ + "R2768", + "R4396" + ], + "R2768": [ + "R1986", + "R6113", + "R7574", + "R1593" + ], + "R2297": [ + "R4523", + "R3090" + ], + "R4523": [ + "R2297", + "R6344" + ], + "R6344": [ + "R8028", + "R4523" + ], + "R8028": [ + "R4961", + "R6344" + ], + "R4961": [ + "R5535", + "R8028" + ], + "R5535": [ + "R1833", + "R4961" + ], + "R1833": [ + "R3653", + "R5535" + ], + "R3653": [ + "R1833", + "R2021" + ], + "R2021": [ + "R3653", + "R1026" + ], + "R1026": [ + "R6678", + "R9491", + "R2021", + "R3349" + ], + "R8585": [ + "R2673", + "R2964" + ], + "R2673": [ + "R8585", + "R7188" + ], + "R7188": [ + "R2673", + "R3438" + ], + "R3438": [ + "R3673", + "R7188" + ], + "R3673": [ + "R4217", + "R3438" + ], + "R4217": [ + "R3673", + "R7957" + ], + "R7957": [ + "R4217", + "R3115" + ], + "R3115": [ + "R6494", + "R7957" + ], + "R6494": [ + "R3115", + "R9076" + ], + "R9076": [ + "R9055", + "R1164", + "R4594", + "R6494" + ], + "R8979": [ + "R9426", + "R2964" + ], + "R9426": [ + "R9038", + "R8979" + ], + "R9038": [ + "R7138", + "R9426" + ], + "R7138": [ + "R1727", + "R9038" + ], + "R1727": [ + "R7138", + "R3752" + ], + "R3752": [ + "R1539", + "R1727" + ], + "R1539": [ + "R3752", + "R1418" + ], + "R1418": [ + "R5703", + "R1539" + ], + "R5703": [ + "R1418", + "R1470" + ], + "R1470": [ + "R5703", + "R1013", + "R5025", + "R5504" + ], + "R6270": [ + "R2964", + "R6478" + ], + "R6478": [ + "R6270", + "R4282" + ], + "R4282": [ + "R9117", + "R6478" + ], + "R9117": [ + "R4282", + "R4303" + ], + "R4303": [ + "R9302", + "R9117" + ], + "R9302": [ + "R4303", + "R8006" + ], + "R8006": [ + "R9393", + "R9302" + ], + "R9393": [ + "R4748", + "R8006" + ], + "R4748": [ + "R6774", + "R9393" + ], + "R6774": [ + "R8642", + "R4748", + "R3791", + "R6079" + ], + "R7541": [ + "R9184", + "R6743" + ], + "R9184": [ + "R7541", + "R4888" + ], + "R4888": [ + "R9184", + "R7096" + ], + "R7096": [ + "R5786", + "R4888" + ], + "R5786": [ + "R6202", + "R7096" + ], + "R6202": [ + "R5786", + "R6782" + ], + "R6782": [ + "R1907", + "R6202" + ], + "R1907": [ + "R3348", + "R6782" + ], + "R3348": [ + "R9610", + "R1907" + ], + "R9610": [ + "R3348" + ], + "R7880": [ + "R6743", + "R4626" + ], + "R4626": [ + "R7880", + "R7200" + ], + "R7200": [ + "R4059", + "R4626" + ], + "R4059": [ + "R1148", + "R7200" + ], + "R1148": [ + "R8696", + "R4059" + ], + "R8696": [ + "R8957", + "R1148" + ], + "R8957": [ + "R3373", + "R8696" + ], + "R3373": [ + "R3593", + "R8957" + ], + "R3593": [ + "R5144", + "R3373" + ], + "R5144": [ + "R3593" + ], + "R1134": [ + "R6743", + "R8659" + ], + "R8659": [ + "R1134", + "R9488" + ], + "R9488": [ + "R8659", + "R9922" + ], + "R9922": [ + "R4095", + "R9488" + ], + "R4095": [ + "R9922", + "R3905" + ], + "R3905": [ + "R4699", + "R4095" + ], + "R4699": [ + "R4155", + "R3905" + ], + "R4155": [ + "R4699", + "R2880" + ], + "R2880": [ + "R4155", + "R4500" + ], + "R4500": [ + "R2880" + ], + "R5980": [ + "R9266", + "R5183" + ], + "R9266": [ + "R2105", + "R5980" + ], + "R2105": [ + "R9266", + "R9766" + ], + "R9766": [ + "R8767", + "R2105" + ], + "R8767": [ + "R9102", + "R9766" + ], + "R9102": [ + "R8767", + "R8741" + ], + "R8741": [ + "R2533", + "R9102" + ], + "R2533": [ + "R8741", + "R4132" + ], + "R4132": [ + "R2897", + "R2533" + ], + "R2897": [ + "R4132" + ], + "R3760": [ + "R6060", + "R5183" + ], + "R6060": [ + "R3938", + "R3760" + ], + "R3938": [ + "R9743", + "R6060" + ], + "R9743": [ + "R2159", + "R3938" + ], + "R2159": [ + "R1497", + "R9743" + ], + "R1497": [ + "R7380", + "R2159" + ], + "R7380": [ + "R1497", + "R6785" + ], + "R6785": [ + "R7380", + "R4453" + ], + "R4453": [ + "R6785", + "R9200" + ], + "R9200": [ + "R4453" + ], + "R8101": [ + "R5183", + "R4813" + ], + "R4813": [ + "R8101", + "R6024" + ], + "R6024": [ + "R3669", + "R4813" + ], + "R3669": [ + "R2181", + "R6024" + ], + "R2181": [ + "R3669", + "R8314" + ], + "R8314": [ + "R6431", + "R2181" + ], + "R6431": [ + "R8314", + "R3860" + ], + "R3860": [ + "R6431", + "R1294" + ], + "R1294": [ + "R3860", + "R1300" + ], + "R1300": [ + "R1294" + ], + "R4222": [ + "R9112", + "R9476" + ], + "R9476": [ + "R2633", + "R4222" + ], + "R2633": [ + "R4650", + "R9476" + ], + "R4650": [ + "R2633", + "R1250" + ], + "R1250": [ + "R1024", + "R4650" + ], + "R1024": [ + "R2321", + "R1250" + ], + "R2321": [ + "R2626", + "R1024" + ], + "R2626": [ + "R2321", + "R5342" + ], + "R5342": [ + "R3826", + "R2626" + ], + "R3826": [ + "R5342" + ], + "R5740": [ + "R2627", + "R9112" + ], + "R2627": [ + "R5740", + "R1189" + ], + "R1189": [ + "R6159", + "R2627" + ], + "R6159": [ + "R1189", + "R8523" + ], + "R8523": [ + "R6159", + "R1746" + ], + "R1746": [ + "R8523", + "R1788" + ], + "R1788": [ + "R8836", + "R1746" + ], + "R8836": [ + "R4367", + "R1788" + ], + "R4367": [ + "R8836", + "R2507" + ], + "R2507": [ + "R4367" + ], + "R5105": [ + "R9112", + "R9744" + ], + "R9744": [ + "R5105", + "R1184" + ], + "R1184": [ + "R6152", + "R9744" + ], + "R6152": [ + "R1184", + "R2317" + ], + "R2317": [ + "R6152", + "R3927" + ], + "R3927": [ + "R6082", + "R2317" + ], + "R6082": [ + "R3927", + "R5193" + ], + "R5193": [ + "R6082", + "R6779" + ], + "R6779": [ + "R2703", + "R5193" + ], + "R2703": [ + "R6779" + ], + "R1678": [ + "R9282", + "R1511" + ], + "R1511": [ + "R7298", + "R1678" + ], + "R7298": [ + "R6168", + "R1511" + ], + "R6168": [ + "R2281", + "R7298" + ], + "R2281": [ + "R7322", + "R6168" + ], + "R7322": [ + "R2281", + "R6945" + ], + "R6945": [ + "R4639", + "R7322" + ], + "R4639": [ + "R8389", + "R6945" + ], + "R8389": [ + "R4639", + "R9075" + ], + "R9075": [ + "R8389" + ], + "R9703": [ + "R9282", + "R8055" + ], + "R8055": [ + "R9703", + "R2753" + ], + "R2753": [ + "R8745", + "R8055" + ], + "R8745": [ + "R2753", + "R8617" + ], + "R8617": [ + "R8745", + "R9738" + ], + "R9738": [ + "R8872", + "R8617" + ], + "R8872": [ + "R3936", + "R9738" + ], + "R3936": [ + "R8872", + "R2356" + ], + "R2356": [ + "R7750", + "R3936" + ], + "R7750": [ + "R2356" + ], + "R8276": [ + "R9282", + "R7116" + ], + "R7116": [ + "R8276", + "R9969" + ], + "R9969": [ + "R5040", + "R7116" + ], + "R5040": [ + "R9969", + "R4483" + ], + "R4483": [ + "R2726", + "R5040" + ], + "R2726": [ + "R8095", + "R4483" + ], + "R8095": [ + "R2726", + "R9557" + ], + "R9557": [ + "R8095", + "R1954" + ], + "R1954": [ + "R5261", + "R9557" + ], + "R5261": [ + "R1954" + ], + "R7574": [ + "R9522", + "R2768" + ], + "R9522": [ + "R7574", + "R6303" + ], + "R6303": [ + "R4887", + "R9522" + ], + "R4887": [ + "R2656", + "R6303" + ], + "R2656": [ + "R1070", + "R4887" + ], + "R1070": [ + "R5538", + "R2656" + ], + "R5538": [ + "R6650", + "R1070" + ], + "R6650": [ + "R5538", + "R7791" + ], + "R7791": [ + "R6650", + "R4740" + ], + "R4740": [ + "R7791" + ], + "R6113": [ + "R2768", + "R6210" + ], + "R6210": [ + "R6113", + "R4944" + ], + "R4944": [ + "R6210", + "R2263" + ], + "R2263": [ + "R1305", + "R4944" + ], + "R1305": [ + "R7335", + "R2263" + ], + "R7335": [ + "R4828", + "R1305" + ], + "R4828": [ + "R9149", + "R7335" + ], + "R9149": [ + "R4658", + "R4828" + ], + "R4658": [ + "R9149", + "R7856" + ], + "R7856": [ + "R4658" + ], + "R1986": [ + "R2768", + "R8991" + ], + "R8991": [ + "R5444", + "R1986" + ], + "R5444": [ + "R1736", + "R8991" + ], + "R1736": [ + "R5444", + "R1702" + ], + "R1702": [ + "R9252", + "R1736" + ], + "R9252": [ + "R1702", + "R6283" + ], + "R6283": [ + "R5727", + "R9252" + ], + "R5727": [ + "R2163", + "R6283" + ], + "R2163": [ + "R5882", + "R5727" + ], + "R5882": [ + "R2163" + ], + "R6678": [ + "R1220", + "R1026" + ], + "R1220": [ + "R2153", + "R6678" + ], + "R2153": [ + "R1220", + "R2052" + ], + "R2052": [ + "R5227", + "R2153" + ], + "R5227": [ + "R2052", + "R9757" + ], + "R9757": [ + "R5227", + "R2841" + ], + "R2841": [ + "R9757", + "R3461" + ], + "R3461": [ + "R2841", + "R7253" + ], + "R7253": [ + "R1235", + "R3461" + ], + "R1235": [ + "R7253" + ], + "R9491": [ + "R2513", + "R1026" + ], + "R2513": [ + "R9491", + "R1508" + ], + "R1508": [ + "R5684", + "R2513" + ], + "R5684": [ + "R1614", + "R1508" + ], + "R1614": [ + "R2704", + "R5684" + ], + "R2704": [ + "R6124", + "R1614" + ], + "R6124": [ + "R2704", + "R7066" + ], + "R7066": [ + "R1462", + "R6124" + ], + "R1462": [ + "R7066", + "R4094" + ], + "R4094": [ + "R1462" + ], + "R3349": [ + "R4653", + "R1026" + ], + "R4653": [ + "R3349", + "R8513" + ], + "R8513": [ + "R6237", + "R4653" + ], + "R6237": [ + "R7160", + "R8513" + ], + "R7160": [ + "R2628", + "R6237" + ], + "R2628": [ + "R7160", + "R5415" + ], + "R5415": [ + "R2628", + "R2559" + ], + "R2559": [ + "R5415", + "R3087" + ], + "R3087": [ + "R9099", + "R2559" + ], + "R9099": [ + "R3087" + ], + "R9055": [ + "R1522", + "R9076" + ], + "R1522": [ + "R9055", + "R7402" + ], + "R7402": [ + "R6948", + "R1522" + ], + "R6948": [ + "R7402", + "R1160" + ], + "R1160": [ + "R9552", + "R6948" + ], + "R9552": [ + "R1160", + "R1960" + ], + "R1960": [ + "R9552", + "R1912" + ], + "R1912": [ + "R1960", + "R1606" + ], + "R1606": [ + "R1912", + "R5822" + ], + "R5822": [ + "R1606" + ], + "R1164": [ + "R1406", + "R9076" + ], + "R1406": [ + "R1164", + "R3996" + ], + "R3996": [ + "R6455", + "R1406" + ], + "R6455": [ + "R3996", + "R2523" + ], + "R2523": [ + "R6455", + "R4394" + ], + "R4394": [ + "R2523", + "R9073" + ], + "R9073": [ + "R4917", + "R4394" + ], + "R4917": [ + "R1542", + "R9073" + ], + "R1542": [ + "R4917", + "R7010" + ], + "R7010": [ + "R1542" + ], + "R4594": [ + "R9045", + "R9076" + ], + "R9045": [ + "R8945", + "R4594" + ], + "R8945": [ + "R9045", + "R1873" + ], + "R1873": [ + "R8945", + "R8363" + ], + "R8363": [ + "R6476", + "R1873" + ], + "R6476": [ + "R8363", + "R8626" + ], + "R8626": [ + "R6476", + "R6553" + ], + "R6553": [ + "R9367", + "R8626" + ], + "R9367": [ + "R6553", + "R4831" + ], + "R4831": [ + "R9367" + ], + "R5504": [ + "R8825", + "R1470" + ], + "R8825": [ + "R4030", + "R5504" + ], + "R4030": [ + "R8825", + "R9465" + ], + "R9465": [ + "R4030", + "R4267" + ], + "R4267": [ + "R4908", + "R9465" + ], + "R4908": [ + "R6461", + "R4267" + ], + "R6461": [ + "R1913", + "R4908" + ], + "R1913": [ + "R6461", + "R6687" + ], + "R6687": [ + "R1913", + "R5519" + ], + "R5519": [ + "R6687" + ], + "R5025": [ + "R8902", + "R1470" + ], + "R8902": [ + "R5025", + "R6859" + ], + "R6859": [ + "R8902", + "R9775" + ], + "R9775": [ + "R7814", + "R6859" + ], + "R7814": [ + "R9775", + "R7169" + ], + "R7169": [ + "R1722", + "R7814" + ], + "R1722": [ + "R7169", + "R9785" + ], + "R9785": [ + "R1722", + "R7529" + ], + "R7529": [ + "R3367", + "R9785" + ], + "R3367": [ + "R7529" + ], + "R1013": [ + "R3577", + "R1470" + ], + "R3577": [ + "R6201", + "R1013" + ], + "R6201": [ + "R6035", + "R3577" + ], + "R6035": [ + "R6201", + "R7913" + ], + "R7913": [ + "R6035", + "R5571" + ], + "R5571": [ + "R3143", + "R7913" + ], + "R3143": [ + "R5571", + "R9440" + ], + "R9440": [ + "R3143", + "R6916" + ], + "R6916": [ + "R5627", + "R9440" + ], + "R5627": [ + "R6916" + ], + "R8642": [ + "R6774", + "R8218" + ], + "R8218": [ + "R1566", + "R8642" + ], + "R1566": [ + "R8218", + "R1437" + ], + "R1437": [ + "R5552", + "R1566" + ], + "R5552": [ + "R1248", + "R1437" + ], + "R1248": [ + "R3449", + "R5552" + ], + "R3449": [ + "R1248", + "R1224" + ], + "R1224": [ + "R9640", + "R3449" + ], + "R9640": [ + "R6643", + "R1224" + ], + "R6643": [ + "R9640" + ], + "R6079": [ + "R3979", + "R6774" + ], + "R3979": [ + "R6079", + "R7967" + ], + "R7967": [ + "R3979", + "R8251" + ], + "R8251": [ + "R1623", + "R7967" + ], + "R1623": [ + "R8251", + "R6899" + ], + "R6899": [ + "R1623", + "R4940" + ], + "R4940": [ + "R6899", + "R7524" + ], + "R7524": [ + "R2382", + "R4940" + ], + "R2382": [ + "R3467", + "R7524" + ], + "R3467": [ + "R2382" + ], + "R3791": [ + "R6774", + "R6401" + ], + "R6401": [ + "R3791", + "R6047" + ], + "R6047": [ + "R1095", + "R6401" + ], + "R1095": [ + "R7059", + "R6047" + ], + "R7059": [ + "R8862", + "R1095" + ], + "R8862": [ + "R7059", + "R1819" + ], + "R1819": [ + "R8862", + "R6133" + ], + "R6133": [ + "R3213", + "R1819" + ], + "R3213": [ + "R6133", + "R2890" + ], + "R2890": [ + "R3213" + ] + } +} diff --git a/tasks/maze-t29/instruction.md b/tasks/maze-t29/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t29/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t29/solution/solve.sh b/tasks/maze-t29/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..5a7e28e3fb986728f56e87b10d06990e3413bcf1 --- /dev/null +++ b/tasks/maze-t29/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R5821 R7546 R4861 R6195 R9167 R9216 R5136 R5983 R4145 R9852 R8529 R9129 R1836 R6442 R6069 R1877 R9672 R9573 R5196 R9112 R4222 R9476 R2633 R4650 R1250 R1024 R2321 R2626 R5342 R3826; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t29/task.toml b/tasks/maze-t29/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..54d2740f4aabe413adb58b733792d77735cfd3ca --- /dev/null +++ b/tasks/maze-t29/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t29" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 29 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t29/tests/Dockerfile b/tasks/maze-t29/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t29/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t29/tests/expected.json b/tasks/maze-t29/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..3e8d7fe736080d6c991960a874a588b2f3640527 --- /dev/null +++ b/tasks/maze-t29/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R3284", + "exit": "R3826", + "budget": 210, + "tree_sha256": "30000e1a27689fb6ecd714763d4e85bc32d512ba202679c685462a3a30576035" +} diff --git a/tasks/maze-t29/tests/grade.py b/tasks/maze-t29/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t29/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t29/tests/test.sh b/tasks/maze-t29/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t29/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t30/environment/agent/Dockerfile b/tasks/maze-t30/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t30/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t30/environment/agent/maze b/tasks/maze-t30/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t30/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t30/environment/docker-compose.yaml b/tasks/maze-t30/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ad1424a855c5b34fb06b831c938416aa060c6282 --- /dev/null +++ b/tasks/maze-t30/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t30: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t30-cef7a9596a73 + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t30/environment/maze/Dockerfile b/tasks/maze-t30/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t30/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t30/environment/maze/engine.py b/tasks/maze-t30/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t30/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t30/environment/maze/export.py b/tasks/maze-t30/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t30/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t30/environment/maze/service.py b/tasks/maze-t30/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t30/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t30/environment/maze/success.py b/tasks/maze-t30/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t30/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t30/environment/maze/tree.json b/tasks/maze-t30/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..cc710b8bbe531be2521d67db4ff0235cab5613f8 --- /dev/null +++ b/tasks/maze-t30/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R2545", + "exit": "R6230", + "budget": 210, + "doors": { + "R2545": [ + "R4554", + "R9169", + "R8727" + ], + "R9169": [ + "R2545", + "R3927" + ], + "R3927": [ + "R9169", + "R9770" + ], + "R9770": [ + "R5874", + "R3927" + ], + "R5874": [ + "R9770", + "R7110" + ], + "R7110": [ + "R5874", + "R5470" + ], + "R5470": [ + "R7110", + "R2579" + ], + "R2579": [ + "R5470", + "R4084" + ], + "R4084": [ + "R9870", + "R2579" + ], + "R9870": [ + "R4084", + "R4378" + ], + "R4378": [ + "R2537", + "R5492", + "R9870", + "R6400" + ], + "R4554": [ + "R7015", + "R2545" + ], + "R7015": [ + "R4554", + "R7708" + ], + "R7708": [ + "R7015", + "R7651" + ], + "R7651": [ + "R7708", + "R3624" + ], + "R3624": [ + "R7651", + "R5853" + ], + "R5853": [ + "R5669", + "R3624" + ], + "R5669": [ + "R6825", + "R5853" + ], + "R6825": [ + "R1894", + "R5669" + ], + "R1894": [ + "R6825", + "R4090" + ], + "R4090": [ + "R2988", + "R4446", + "R1894", + "R5624" + ], + "R8727": [ + "R3274", + "R2545" + ], + "R3274": [ + "R2367", + "R8727" + ], + "R2367": [ + "R3274", + "R5121" + ], + "R5121": [ + "R4470", + "R2367" + ], + "R4470": [ + "R5121", + "R3212" + ], + "R3212": [ + "R4470", + "R2992" + ], + "R2992": [ + "R4000", + "R3212" + ], + "R4000": [ + "R2992", + "R9228" + ], + "R9228": [ + "R1066", + "R4000" + ], + "R1066": [ + "R9228", + "R3390", + "R4896", + "R1562" + ], + "R5492": [ + "R4378", + "R9355" + ], + "R9355": [ + "R5492", + "R8363" + ], + "R8363": [ + "R4256", + "R9355" + ], + "R4256": [ + "R3738", + "R8363" + ], + "R3738": [ + "R4256", + "R9738" + ], + "R9738": [ + "R7475", + "R3738" + ], + "R7475": [ + "R9738", + "R8714" + ], + "R8714": [ + "R2906", + "R7475" + ], + "R2906": [ + "R8714", + "R3578" + ], + "R3578": [ + "R9792", + "R7371", + "R2139", + "R2906" + ], + "R2537": [ + "R4378", + "R8302" + ], + "R8302": [ + "R2537", + "R9042" + ], + "R9042": [ + "R8302", + "R8345" + ], + "R8345": [ + "R9042", + "R2623" + ], + "R2623": [ + "R1712", + "R8345" + ], + "R1712": [ + "R2623", + "R7948" + ], + "R7948": [ + "R1712", + "R8871" + ], + "R8871": [ + "R7948", + "R2972" + ], + "R2972": [ + "R1558", + "R8871" + ], + "R1558": [ + "R8370", + "R5616", + "R8855", + "R2972" + ], + "R6400": [ + "R4378", + "R9755" + ], + "R9755": [ + "R6400", + "R7661" + ], + "R7661": [ + "R9755", + "R6031" + ], + "R6031": [ + "R7661", + "R6238" + ], + "R6238": [ + "R6038", + "R6031" + ], + "R6038": [ + "R4737", + "R6238" + ], + "R4737": [ + "R8049", + "R6038" + ], + "R8049": [ + "R4222", + "R4737" + ], + "R4222": [ + "R5050", + "R8049" + ], + "R5050": [ + "R6095", + "R6566", + "R1256", + "R4222" + ], + "R5624": [ + "R7275", + "R4090" + ], + "R7275": [ + "R8090", + "R5624" + ], + "R8090": [ + "R9152", + "R7275" + ], + "R9152": [ + "R8090", + "R7815" + ], + "R7815": [ + "R9152", + "R6213" + ], + "R6213": [ + "R7815", + "R6481" + ], + "R6481": [ + "R5686", + "R6213" + ], + "R5686": [ + "R6703", + "R6481" + ], + "R6703": [ + "R1927", + "R5686" + ], + "R1927": [ + "R4396", + "R6703", + "R2961", + "R6939" + ], + "R2988": [ + "R2976", + "R4090" + ], + "R2976": [ + "R6113", + "R2988" + ], + "R6113": [ + "R6517", + "R2976" + ], + "R6517": [ + "R2254", + "R6113" + ], + "R2254": [ + "R6517", + "R8159" + ], + "R8159": [ + "R2254", + "R5517" + ], + "R5517": [ + "R8159", + "R4752" + ], + "R4752": [ + "R5517", + "R5199" + ], + "R5199": [ + "R4752", + "R5958" + ], + "R5958": [ + "R5235", + "R5199", + "R8405", + "R8880" + ], + "R4446": [ + "R4090", + "R3085" + ], + "R3085": [ + "R3759", + "R4446" + ], + "R3759": [ + "R3085", + "R3851" + ], + "R3851": [ + "R3759", + "R5590" + ], + "R5590": [ + "R3851", + "R7889" + ], + "R7889": [ + "R9184", + "R5590" + ], + "R9184": [ + "R7889", + "R1453" + ], + "R1453": [ + "R8647", + "R9184" + ], + "R8647": [ + "R1453", + "R5151" + ], + "R5151": [ + "R8647", + "R1595", + "R9983", + "R2242" + ], + "R1562": [ + "R7377", + "R1066" + ], + "R7377": [ + "R1562", + "R8716" + ], + "R8716": [ + "R8742", + "R7377" + ], + "R8742": [ + "R8716", + "R1931" + ], + "R1931": [ + "R8742", + "R5987" + ], + "R5987": [ + "R3402", + "R1931" + ], + "R3402": [ + "R5987", + "R3253" + ], + "R3253": [ + "R3402", + "R7913" + ], + "R7913": [ + "R3253", + "R2039" + ], + "R2039": [ + "R5855", + "R6724", + "R6838", + "R7913" + ], + "R4896": [ + "R1066", + "R2856" + ], + "R2856": [ + "R4457", + "R4896" + ], + "R4457": [ + "R3643", + "R2856" + ], + "R3643": [ + "R4457", + "R6376" + ], + "R6376": [ + "R2159", + "R3643" + ], + "R2159": [ + "R6376", + "R6794" + ], + "R6794": [ + "R2159", + "R5734" + ], + "R5734": [ + "R7485", + "R6794" + ], + "R7485": [ + "R7389", + "R5734" + ], + "R7389": [ + "R7288", + "R6846", + "R6774", + "R7485" + ], + "R3390": [ + "R1066", + "R9906" + ], + "R9906": [ + "R3390", + "R4128" + ], + "R4128": [ + "R7429", + "R9906" + ], + "R7429": [ + "R4128", + "R5726" + ], + "R5726": [ + "R7429", + "R4872" + ], + "R4872": [ + "R3201", + "R5726" + ], + "R3201": [ + "R2966", + "R4872" + ], + "R2966": [ + "R9721", + "R3201" + ], + "R9721": [ + "R4845", + "R2966" + ], + "R4845": [ + "R2270", + "R9721", + "R9367", + "R5223" + ], + "R9792": [ + "R3578", + "R2017" + ], + "R2017": [ + "R9792", + "R9750" + ], + "R9750": [ + "R2017", + "R6271" + ], + "R6271": [ + "R3395", + "R9750" + ], + "R3395": [ + "R6271", + "R3100" + ], + "R3100": [ + "R8832", + "R3395" + ], + "R8832": [ + "R3100", + "R3312" + ], + "R3312": [ + "R8832", + "R9907" + ], + "R9907": [ + "R3312", + "R4247" + ], + "R4247": [ + "R9907" + ], + "R2139": [ + "R3587", + "R3578" + ], + "R3587": [ + "R6492", + "R2139" + ], + "R6492": [ + "R3587", + "R1921" + ], + "R1921": [ + "R1647", + "R6492" + ], + "R1647": [ + "R1921", + "R8058" + ], + "R8058": [ + "R3756", + "R1647" + ], + "R3756": [ + "R7186", + "R8058" + ], + "R7186": [ + "R8136", + "R3756" + ], + "R8136": [ + "R6815", + "R7186" + ], + "R6815": [ + "R8136" + ], + "R7371": [ + "R4701", + "R3578" + ], + "R4701": [ + "R7371", + "R6440" + ], + "R6440": [ + "R4166", + "R4701" + ], + "R4166": [ + "R6440", + "R9079" + ], + "R9079": [ + "R9692", + "R4166" + ], + "R9692": [ + "R8546", + "R9079" + ], + "R8546": [ + "R9692", + "R7171" + ], + "R7171": [ + "R8546", + "R2555" + ], + "R2555": [ + "R2300", + "R7171" + ], + "R2300": [ + "R2555" + ], + "R8370": [ + "R1558", + "R3787" + ], + "R3787": [ + "R1590", + "R8370" + ], + "R1590": [ + "R3787", + "R3876" + ], + "R3876": [ + "R1803", + "R1590" + ], + "R1803": [ + "R3876", + "R8800" + ], + "R8800": [ + "R2619", + "R1803" + ], + "R2619": [ + "R8800", + "R9097" + ], + "R9097": [ + "R2619", + "R5409" + ], + "R5409": [ + "R8638", + "R9097" + ], + "R8638": [ + "R5409" + ], + "R8855": [ + "R1558", + "R9253" + ], + "R9253": [ + "R3450", + "R8855" + ], + "R3450": [ + "R9253", + "R4249" + ], + "R4249": [ + "R2271", + "R3450" + ], + "R2271": [ + "R2407", + "R4249" + ], + "R2407": [ + "R2271", + "R1974" + ], + "R1974": [ + "R2407", + "R4451" + ], + "R4451": [ + "R7217", + "R1974" + ], + "R7217": [ + "R4058", + "R4451" + ], + "R4058": [ + "R7217" + ], + "R5616": [ + "R1558", + "R9454" + ], + "R9454": [ + "R4054", + "R5616" + ], + "R4054": [ + "R9454", + "R6575" + ], + "R6575": [ + "R3803", + "R4054" + ], + "R3803": [ + "R6575", + "R9341" + ], + "R9341": [ + "R6867", + "R3803" + ], + "R6867": [ + "R2525", + "R9341" + ], + "R2525": [ + "R7401", + "R6867" + ], + "R7401": [ + "R2525", + "R6663" + ], + "R6663": [ + "R7401" + ], + "R6566": [ + "R9837", + "R5050" + ], + "R9837": [ + "R6566", + "R5245" + ], + "R5245": [ + "R9837", + "R9332" + ], + "R9332": [ + "R5245", + "R7927" + ], + "R7927": [ + "R9332", + "R8276" + ], + "R8276": [ + "R7927", + "R4753" + ], + "R4753": [ + "R8276", + "R5661" + ], + "R5661": [ + "R1420", + "R4753" + ], + "R1420": [ + "R5742", + "R5661" + ], + "R5742": [ + "R1420" + ], + "R6095": [ + "R5050", + "R7625" + ], + "R7625": [ + "R6095", + "R8458" + ], + "R8458": [ + "R2942", + "R7625" + ], + "R2942": [ + "R6110", + "R8458" + ], + "R6110": [ + "R4344", + "R2942" + ], + "R4344": [ + "R5769", + "R6110" + ], + "R5769": [ + "R7163", + "R4344" + ], + "R7163": [ + "R5769", + "R1999" + ], + "R1999": [ + "R1534", + "R7163" + ], + "R1534": [ + "R1999" + ], + "R1256": [ + "R5050", + "R2508" + ], + "R2508": [ + "R6529", + "R1256" + ], + "R6529": [ + "R2508", + "R5596" + ], + "R5596": [ + "R6529", + "R3283" + ], + "R3283": [ + "R2209", + "R5596" + ], + "R2209": [ + "R3283", + "R6135" + ], + "R6135": [ + "R8894", + "R2209" + ], + "R8894": [ + "R6700", + "R6135" + ], + "R6700": [ + "R8894", + "R3588" + ], + "R3588": [ + "R6700" + ], + "R4396": [ + "R2688", + "R1927" + ], + "R2688": [ + "R4396", + "R3254" + ], + "R3254": [ + "R5927", + "R2688" + ], + "R5927": [ + "R1506", + "R3254" + ], + "R1506": [ + "R5249", + "R5927" + ], + "R5249": [ + "R1079", + "R1506" + ], + "R1079": [ + "R5249", + "R5234" + ], + "R5234": [ + "R5765", + "R1079" + ], + "R5765": [ + "R1809", + "R5234" + ], + "R1809": [ + "R5765" + ], + "R2961": [ + "R4866", + "R1927" + ], + "R4866": [ + "R2961", + "R2970" + ], + "R2970": [ + "R4866", + "R5911" + ], + "R5911": [ + "R2970", + "R2131" + ], + "R2131": [ + "R9034", + "R5911" + ], + "R9034": [ + "R2131", + "R7964" + ], + "R7964": [ + "R9034", + "R4289" + ], + "R4289": [ + "R9858", + "R7964" + ], + "R9858": [ + "R3646", + "R4289" + ], + "R3646": [ + "R9858" + ], + "R6939": [ + "R4202", + "R1927" + ], + "R4202": [ + "R4844", + "R6939" + ], + "R4844": [ + "R2714", + "R4202" + ], + "R2714": [ + "R4844", + "R9866" + ], + "R9866": [ + "R2714", + "R8322" + ], + "R8322": [ + "R9866", + "R5505" + ], + "R5505": [ + "R8322", + "R9785" + ], + "R9785": [ + "R5505", + "R8110" + ], + "R8110": [ + "R9785", + "R4162" + ], + "R4162": [ + "R8110" + ], + "R5235": [ + "R7571", + "R5958" + ], + "R7571": [ + "R5235", + "R2560" + ], + "R2560": [ + "R7571", + "R6996" + ], + "R6996": [ + "R8160", + "R2560" + ], + "R8160": [ + "R6996", + "R5876" + ], + "R5876": [ + "R8160", + "R7433" + ], + "R7433": [ + "R8274", + "R5876" + ], + "R8274": [ + "R2709", + "R7433" + ], + "R2709": [ + "R8127", + "R8274" + ], + "R8127": [ + "R2709" + ], + "R8880": [ + "R5958", + "R2722" + ], + "R2722": [ + "R8880", + "R4963" + ], + "R4963": [ + "R6347", + "R2722" + ], + "R6347": [ + "R8537", + "R4963" + ], + "R8537": [ + "R7412", + "R6347" + ], + "R7412": [ + "R8537", + "R8409" + ], + "R8409": [ + "R7412", + "R9652" + ], + "R9652": [ + "R9647", + "R8409" + ], + "R9647": [ + "R2228", + "R9652" + ], + "R2228": [ + "R9647" + ], + "R8405": [ + "R5958", + "R2478" + ], + "R2478": [ + "R5514", + "R8405" + ], + "R5514": [ + "R8383", + "R2478" + ], + "R8383": [ + "R5514", + "R5458" + ], + "R5458": [ + "R8383", + "R9352" + ], + "R9352": [ + "R5588", + "R5458" + ], + "R5588": [ + "R5087", + "R9352" + ], + "R5087": [ + "R5588", + "R7094" + ], + "R7094": [ + "R5087", + "R6230" + ], + "R6230": [ + "R7094" + ], + "R2242": [ + "R5151", + "R2681" + ], + "R2681": [ + "R2242", + "R2878" + ], + "R2878": [ + "R2681", + "R2940" + ], + "R2940": [ + "R2878", + "R2479" + ], + "R2479": [ + "R2940", + "R7052" + ], + "R7052": [ + "R5019", + "R2479" + ], + "R5019": [ + "R9500", + "R7052" + ], + "R9500": [ + "R5019", + "R2852" + ], + "R2852": [ + "R9500", + "R5060" + ], + "R5060": [ + "R2852" + ], + "R9983": [ + "R5151", + "R3298" + ], + "R3298": [ + "R4871", + "R9983" + ], + "R4871": [ + "R7081", + "R3298" + ], + "R7081": [ + "R4871", + "R6665" + ], + "R6665": [ + "R7081", + "R1703" + ], + "R1703": [ + "R7966", + "R6665" + ], + "R7966": [ + "R1703", + "R8123" + ], + "R8123": [ + "R7966", + "R4147" + ], + "R4147": [ + "R8123", + "R5016" + ], + "R5016": [ + "R4147" + ], + "R1595": [ + "R2539", + "R5151" + ], + "R2539": [ + "R1325", + "R1595" + ], + "R1325": [ + "R2539", + "R5618" + ], + "R5618": [ + "R8294", + "R1325" + ], + "R8294": [ + "R1609", + "R5618" + ], + "R1609": [ + "R8294", + "R6626" + ], + "R6626": [ + "R1513", + "R1609" + ], + "R1513": [ + "R6626", + "R7287" + ], + "R7287": [ + "R1513", + "R6905" + ], + "R6905": [ + "R7287" + ], + "R6724": [ + "R4223", + "R2039" + ], + "R4223": [ + "R7030", + "R6724" + ], + "R7030": [ + "R4273", + "R4223" + ], + "R4273": [ + "R7030", + "R5948" + ], + "R5948": [ + "R5601", + "R4273" + ], + "R5601": [ + "R5948", + "R4066" + ], + "R4066": [ + "R5601", + "R9776" + ], + "R9776": [ + "R6758", + "R4066" + ], + "R6758": [ + "R2507", + "R9776" + ], + "R2507": [ + "R6758" + ], + "R6838": [ + "R1307", + "R2039" + ], + "R1307": [ + "R8182", + "R6838" + ], + "R8182": [ + "R1307", + "R8692" + ], + "R8692": [ + "R6033", + "R8182" + ], + "R6033": [ + "R9601", + "R8692" + ], + "R9601": [ + "R5707", + "R6033" + ], + "R5707": [ + "R2437", + "R9601" + ], + "R2437": [ + "R1791", + "R5707" + ], + "R1791": [ + "R8245", + "R2437" + ], + "R8245": [ + "R1791" + ], + "R5855": [ + "R2039", + "R3985" + ], + "R3985": [ + "R5855", + "R2100" + ], + "R2100": [ + "R3985", + "R5764" + ], + "R5764": [ + "R2100", + "R5220" + ], + "R5220": [ + "R5764", + "R4267" + ], + "R4267": [ + "R8095", + "R5220" + ], + "R8095": [ + "R4267", + "R8071" + ], + "R8071": [ + "R4525", + "R8095" + ], + "R4525": [ + "R8071", + "R5699" + ], + "R5699": [ + "R4525" + ], + "R7288": [ + "R5626", + "R7389" + ], + "R5626": [ + "R9802", + "R7288" + ], + "R9802": [ + "R5626", + "R2653" + ], + "R2653": [ + "R6356", + "R9802" + ], + "R6356": [ + "R9177", + "R2653" + ], + "R9177": [ + "R2494", + "R6356" + ], + "R2494": [ + "R9177", + "R3685" + ], + "R3685": [ + "R2494", + "R7638" + ], + "R7638": [ + "R3685", + "R9782" + ], + "R9782": [ + "R7638" + ], + "R6774": [ + "R7389", + "R5212" + ], + "R5212": [ + "R6774", + "R7700" + ], + "R7700": [ + "R8730", + "R5212" + ], + "R8730": [ + "R5165", + "R7700" + ], + "R5165": [ + "R4826", + "R8730" + ], + "R4826": [ + "R5165", + "R3499" + ], + "R3499": [ + "R4826", + "R5438" + ], + "R5438": [ + "R7449", + "R3499" + ], + "R7449": [ + "R8927", + "R5438" + ], + "R8927": [ + "R7449" + ], + "R6846": [ + "R7389", + "R3230" + ], + "R3230": [ + "R6316", + "R6846" + ], + "R6316": [ + "R5568", + "R3230" + ], + "R5568": [ + "R4079", + "R6316" + ], + "R4079": [ + "R8663", + "R5568" + ], + "R8663": [ + "R4079", + "R1296" + ], + "R1296": [ + "R8663", + "R6096" + ], + "R6096": [ + "R2614", + "R1296" + ], + "R2614": [ + "R6096", + "R4092" + ], + "R4092": [ + "R2614" + ], + "R5223": [ + "R6307", + "R4845" + ], + "R6307": [ + "R7859", + "R5223" + ], + "R7859": [ + "R6307", + "R9186" + ], + "R9186": [ + "R1132", + "R7859" + ], + "R1132": [ + "R9186", + "R3736" + ], + "R3736": [ + "R6186", + "R1132" + ], + "R6186": [ + "R3736", + "R1521" + ], + "R1521": [ + "R6280", + "R6186" + ], + "R6280": [ + "R1521", + "R7885" + ], + "R7885": [ + "R6280" + ], + "R2270": [ + "R4845", + "R9520" + ], + "R9520": [ + "R2270", + "R6696" + ], + "R6696": [ + "R8414", + "R9520" + ], + "R8414": [ + "R6696", + "R8778" + ], + "R8778": [ + "R8493", + "R8414" + ], + "R8493": [ + "R6369", + "R8778" + ], + "R6369": [ + "R8493", + "R4630" + ], + "R4630": [ + "R3272", + "R6369" + ], + "R3272": [ + "R1378", + "R4630" + ], + "R1378": [ + "R3272" + ], + "R9367": [ + "R5353", + "R4845" + ], + "R5353": [ + "R9367", + "R6337" + ], + "R6337": [ + "R5353", + "R6742" + ], + "R6742": [ + "R6337", + "R5979" + ], + "R5979": [ + "R6742", + "R2025" + ], + "R2025": [ + "R3218", + "R5979" + ], + "R3218": [ + "R2025", + "R6326" + ], + "R6326": [ + "R3218", + "R6488" + ], + "R6488": [ + "R6326", + "R4933" + ], + "R4933": [ + "R6488" + ] + } +} diff --git a/tasks/maze-t30/instruction.md b/tasks/maze-t30/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t30/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t30/solution/solve.sh b/tasks/maze-t30/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..75e866258e018181fcf704232ccdb0aa9e79ffba --- /dev/null +++ b/tasks/maze-t30/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R4554 R7015 R7708 R7651 R3624 R5853 R5669 R6825 R1894 R4090 R2988 R2976 R6113 R6517 R2254 R8159 R5517 R4752 R5199 R5958 R8405 R2478 R5514 R8383 R5458 R9352 R5588 R5087 R7094 R6230; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t30/task.toml b/tasks/maze-t30/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..53e6ac91235513555794cfc4052751cf6d27151b --- /dev/null +++ b/tasks/maze-t30/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t30" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 30 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30 diff --git a/tasks/maze-t30/tests/Dockerfile b/tasks/maze-t30/tests/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c98bd343642d66d42d2477d4d0f0eb411dc88e2f --- /dev/null +++ b/tasks/maze-t30/tests/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The verifier's own container (environment_mode = "separate"): the grader and the task's +# answer, never in the agent's container. Harbor adds /evidence from the maze service. +COPY test.sh grade.py expected.json /tests/ +RUN chmod 755 /tests/test.sh diff --git a/tasks/maze-t30/tests/expected.json b/tasks/maze-t30/tests/expected.json new file mode 100644 index 0000000000000000000000000000000000000000..32d2b218ea32f7bc39b9f029f6266a1459be323e --- /dev/null +++ b/tasks/maze-t30/tests/expected.json @@ -0,0 +1,6 @@ +{ + "start": "R2545", + "exit": "R6230", + "budget": 210, + "tree_sha256": "1bf28a79f17ff93dd7f08df80b24224571faa76b2c2be27b781603fa1ac5444a" +} diff --git a/tasks/maze-t30/tests/grade.py b/tasks/maze-t30/tests/grade.py new file mode 100644 index 0000000000000000000000000000000000000000..9c9dd2e71b38b2b520b4206b4f7b43515a8c769d --- /dev/null +++ b/tasks/maze-t30/tests/grade.py @@ -0,0 +1,117 @@ +"""s3-maze's grader: reward 1 if the agent's walk reached the exit, from the maze service's own +record. Run by test.sh as `python3 -I -S grade.py`, in a fresh verifier container (the task's +`environment_mode = "separate"`), after the agent's container has stopped. + +It reads only /evidence/final.json and /evidence/moves.jsonl, which the maze service exported +(export.py, Harbor's collect hook) and Harbor carried over, and expected.json (this folder, +never in the agent's container). Reward 1 needs all of: the export is the service's +(`exported_by`), for this task's tree (its sha256), start and move budget; the service says the +exit was reached and the walk ends there; and the move log's accepted moves are one unbroken +walk from the start to the exit, within the budget, as many as the service counted. Anything +else, a missing, unreadable or malformed file included, scores 0. + +Each file counts only as a regular file of its own at exactly its path: no link anywhere on the +path is followed, and a FIFO cannot block the read. The reward file is written 0 first and +never through a link. Copies of both files stay in the verifier's folder, for the metrics. +EVIDENCE_DIR, EXPECTED and VERIFIER_DIR override the paths (no links in them), to test locally. +""" + +import json +import os +import stat + +EVIDENCE = os.environ.get("EVIDENCE_DIR", "/evidence") +EXPECTED = os.environ.get("EXPECTED", "/tests/expected.json") +VERIFIER = os.environ.get("VERIFIER_DIR", "/logs/verifier") +LIMIT = 16 << 20 # bytes + + +def read_own(path: str) -> bytes: + """The bytes of the regular file at exactly `path`: every folder on the way is opened + without following a link, and so is the file (a FIFO cannot block it); one link only.""" + parts = os.path.abspath(path).split("/")[1:] + folder = os.open("/", os.O_RDONLY | os.O_DIRECTORY) + try: + for part in parts[:-1]: + inner = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=folder) + os.close(folder) + folder = inner + fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder) + finally: + os.close(folder) + with os.fdopen(fd, "rb") as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError("not a regular file of its own") + data = stream.read(LIMIT + 1) + if len(data) > LIMIT: + raise ValueError("too large") + return data + + +def write_new(name: str, data: bytes) -> None: + path = os.path.join(VERIFIER, name) + try: + os.unlink(path) # never written through a link + except FileNotFoundError: + pass + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as stream: + stream.write(data) + + +def reject_constant(name): + raise ValueError(f"{name} is not JSON") + + +def parse(data: bytes): + return json.loads(data.decode("utf-8"), parse_constant=reject_constant) + + +def walk_reaches_exit(final: dict, records: list, expected: dict) -> bool: + room, moves = expected["start"], 0 + for record in records: + if not isinstance(record, dict) or record.get("ok") not in (True, False): + return False + if record["ok"] is True: + if record.get("from") != room or not isinstance(record.get("to"), str): + return False + room, moves = record["to"], moves + 1 + return (room == expected["exit"] and moves == final.get("moves") + and 0 < moves <= expected["budget"]) + + +def grade() -> bool: + expected = parse(read_own(EXPECTED)) + final_data = read_own(os.path.join(EVIDENCE, "final.json")) + moves_data = read_own(os.path.join(EVIDENCE, "moves.jsonl")) + write_new("final.json", final_data) + write_new("moves.jsonl", moves_data) + final = parse(final_data) + records = [parse(line) for line in moves_data.splitlines() if line.strip()] + return ( + isinstance(final, dict) + and final.get("exported_by") == "maze" + and final.get("tree_sha256") == expected["tree_sha256"] + and final.get("start") == expected["start"] + and final.get("budget") == expected["budget"] + and final.get("reached") is True + and final.get("room") == expected["exit"] + and walk_reaches_exit(final, records, expected) + ) + + +os.makedirs(VERIFIER, exist_ok=True) +for name in ("reward.txt", "reward.json"): + try: + os.unlink(os.path.join(VERIFIER, name)) + except FileNotFoundError: + pass +write_new("reward.txt", b"0") +try: + valid = grade() +except Exception: # missing, a link, not regular, invalid UTF-8 or JSON, a wrong shape + valid = False +if valid: + write_new("reward.txt", b"1") +print("reward:", 1 if valid else 0) diff --git a/tasks/maze-t30/tests/test.sh b/tasks/maze-t30/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..08f51e62df56fb7a2f279a9a8c80fe7f8358ba74 --- /dev/null +++ b/tasks/maze-t30/tests/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# s3-maze's verifier, in a fresh container built from this folder (environment_mode = +# "separate"): reward 1 if the maze service's own record shows the agent's walk reached the exit +# (grade.py has the rules). Reward 0 is written before anything is checked, so a crash of the +# check also scores 0. Python runs isolated (-I -S): no module from the working directory, +# PYTHON* variables or site hooks can change what it imports. Besides bash, python3 is the only +# command this script runs. +case $0 in */*) here=${0%/*} ;; *) here=. ;; esac +export EXPECTED=${EXPECTED:-$here/expected.json} +export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier} +[ -d "$VERIFIER_DIR" ] && [ ! -L "$VERIFIER_DIR/reward.txt" ] && echo 0 > "$VERIFIER_DIR/reward.txt" +python3 -I -S "$here/grade.py" || true diff --git a/tasks/maze-t31/environment/agent/Dockerfile b/tasks/maze-t31/environment/agent/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b46370bbd66eadfe57265c2a9a15c8c2f0732817 --- /dev/null +++ b/tasks/maze-t31/environment/agent/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The agent's container: only the `maze` command, which talks to the maze service. The tree and +# the exit are never here. +RUN apt-get update && apt-get install -y --no-install-recommends bash curl ca-certificates \ + && rm -rf /var/lib/apt/lists/* +COPY maze /usr/local/bin/maze +RUN chmod 755 /usr/local/bin/maze +WORKDIR /app diff --git a/tasks/maze-t31/environment/agent/maze b/tasks/maze-t31/environment/agent/maze new file mode 100644 index 0000000000000000000000000000000000000000..421e2d0e4d73abfdb1b3204aa0e5249dd7cddcac --- /dev/null +++ b/tasks/maze-t31/environment/agent/maze @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""maze look | maze move ROOM: explore the maze through its service (standard library only). + +Prints the service's JSON for your current room: `room`, its `doors`, whether it is the `exit`, +the room you `arrived_from` and your `moves_left` (a dead end adds a `notice`). A refused move +adds an `error` and exits 1; the service unreachable exits 2. +""" + +import json +import os +import sys +import time +import urllib.error +import urllib.request + +URL = os.environ.get("MAZE_URL", "http://maze:8000").rstrip("/") +USAGE = "usage: maze look | maze move ROOM" + + +def call(request: urllib.request.Request) -> tuple[int, dict]: + deadline = time.monotonic() + 30 # the service may still be starting + while True: + try: + with urllib.request.urlopen(request, timeout=30) as response: + return response.status, json.load(response) + except urllib.error.HTTPError as e: + try: + return e.code, json.load(e) + except ValueError: + return e.code, {"error": f"HTTP {e.code} from the maze service"} + except (urllib.error.URLError, ConnectionError, TimeoutError) as e: + if time.monotonic() > deadline: + return 0, {"error": f"the maze service is unreachable: {e}"} + time.sleep(1) + + +def main(argv: list[str]) -> int: + if argv == ["look"]: + request = urllib.request.Request(URL + "/look") + elif len(argv) == 2 and argv[0] == "move": + request = urllib.request.Request( + URL + "/move", data=json.dumps({"room": argv[1]}).encode(), method="POST", + headers={"Content-Type": "application/json"}) + else: + print(USAGE, file=sys.stderr) + return 2 + status, body = call(request) + print(json.dumps(body)) + return 0 if status == 200 else 1 if status in (400, 409) else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tasks/maze-t31/environment/docker-compose.yaml b/tasks/maze-t31/environment/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5f39bbf562ac1830c96c01c557b3ab6676cdbf74 --- /dev/null +++ b/tasks/maze-t31/environment/docker-compose.yaml @@ -0,0 +1,25 @@ +# maze-t31: the agent's container (main) and its own maze service. The service holds +# the tree and answers only `maze look` and `maze move ROOM`, on a network only main reaches. +services: + main: + image: s3-maze-agent:7ecf229faaac + build: + context: ./agent + networks: [default, maze] + security_opt: [no-new-privileges:true] + depends_on: [maze] + maze: + image: s3-maze-service:maze-t31-7501bcfc934b + build: + context: ./maze + networks: [maze] + environment: + MAZE_AGENT: ${COMPOSE_PROJECT_NAME:-} # the Trial, for the move log + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + pids_limit: 64 + mem_limit: 256m +networks: + default: {} # main's way out (Flock replaces it with the agent's own network) + maze: + internal: true diff --git a/tasks/maze-t31/environment/maze/Dockerfile b/tasks/maze-t31/environment/maze/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7a4c488d88a834b8d56f4f888c523f2ac49a18fd --- /dev/null +++ b/tasks/maze-t31/environment/maze/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.13-slim-bookworm@sha256:281d39ff02e3b389401586134ff9e6572954687822a58055ec07a60ded4e0b7f +# The maze service: the tree (private, never in the agent's container), the walk's state and +# move log, the success check and the collect hook. Standard library only. +COPY engine.py service.py success.py export.py /app/ +COPY tree.json /private/tree.json +RUN mkdir -p /var/lib/maze /evidence && chmod 0700 /private /var/lib/maze +ENV MAZE_TREE=/private/tree.json MAZE_STATE_DIR=/var/lib/maze +WORKDIR /app +CMD ["python3", "-I", "-S", "-B", "/app/service.py"] diff --git a/tasks/maze-t31/environment/maze/engine.py b/tasks/maze-t31/environment/maze/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..130582a42c14747a4592fee2ab4f39342df7b34a --- /dev/null +++ b/tasks/maze-t31/environment/maze/engine.py @@ -0,0 +1,93 @@ +"""The maze: one agent's walk through its own copy of a tree of rooms (s3-maze). + +The service (service.py) holds one Walk per Trial; budget.py simulates searches on this same +code. Python 3.10+, standard library only. + +A tree is `{"start", "exit", "budget", "doors": {room: [neighbour, ...]}}`: each room lists its +doors in the order fixed when the tree was generated, the same for every agent and every visit. +""" + +import json + +DEAD_END = "Dead end: its only door leads back the way you came." + + +class Maze: + def __init__(self, tree: dict): + self.start = tree["start"] + self.exit = tree["exit"] + self.budget = int(tree["budget"]) + self.doors = {room: list(doors) for room, doors in tree["doors"].items()} + + @classmethod + def load(cls, path: str) -> "Maze": + with open(path, encoding="utf-8") as f: + return cls(json.load(f)) + + +class Walk: + """One agent's position and its accepted moves. Looking is free; a refused move costs + nothing; at most `budget` moves are accepted, and none once the exit is reached.""" + + def __init__(self, maze: Maze, room: str | None = None, arrived_from: str | None = None, + moves: int = 0, refused: int = 0): + self.maze = maze + self.room = room or maze.start + self.arrived_from = arrived_from + self.moves = moves + self.refused = refused + + @property + def reached(self) -> bool: + return self.room == self.maze.exit + + def look(self) -> dict: + """The paper's observation (s3, Appendix H), plus the moves left.""" + doors = self.maze.doors[self.room] + out = {"room": self.room, "doors": list(doors), "exit": self.reached, + "arrived_from": self.arrived_from, "moves_left": self.maze.budget - self.moves} + if len(doors) == 1 and not self.reached: + out["notice"] = DEAD_END + return out + + def refusal(self, room) -> str | None: + """Why a move into `room` is refused (a short code), or None.""" + if self.reached: + return "at_exit" + if self.moves >= self.maze.budget: + return "no_moves_left" + if not isinstance(room, str) or room not in self.maze.doors[self.room]: + return "not_a_door" + return None + + def move(self, room) -> tuple[dict, str | None]: + """(the observation after moving into `room`, None), or (an error and the current + observation, the refusal's code).""" + why = self.refusal(room) + if why is not None: + self.refused += 1 + return {"error": MESSAGES[why].format(room=_short(room), here=self.room, + budget=self.maze.budget), **self.look()}, why + self.arrived_from, self.room = self.room, room + self.moves += 1 + return self.look(), None + + def state(self) -> dict: + return {"room": self.room, "arrived_from": self.arrived_from, "moves": self.moves, + "refused": self.refused, "reached": self.reached} + + @classmethod + def from_state(cls, maze: Maze, state: dict) -> "Walk": + return cls(maze, state["room"], state["arrived_from"], state["moves"], state["refused"]) + + +MESSAGES = { + "at_exit": "You are at the exit: the task is solved.", + "no_moves_left": "No moves left: your budget of {budget} moves is used up.", + "not_a_door": "{room} is not a door of {here}: move into one of the doors listed for it.", +} + + +def _short(value) -> str: + text = value if isinstance(value, str) else json.dumps(value) + return text if len(text) <= 40 else text[:40] + "..." diff --git a/tasks/maze-t31/environment/maze/export.py b/tasks/maze-t31/environment/maze/export.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e4bd6affe84470a8242fe6ffff5e0d4a1d5c --- /dev/null +++ b/tasks/maze-t31/environment/maze/export.py @@ -0,0 +1,40 @@ +"""Harbor's [[verifier.collect]] hook for s3-maze, run in this maze service once the agent is done +(its container already stopped): `python3 -I -S /app/export.py`. + +Writes the service's own record of the walk to /evidence, which Harbor collects as artifacts +and hands to the grader in a fresh container: `final.json` (the state file, marked as exported +here) and `moves.jsonl` (the move log). Nothing in /evidence comes from the agent's container. +""" + +import json +import os + +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +EVIDENCE = os.environ.get("MAZE_EVIDENCE_DIR", "/evidence") +LIMIT = 16 << 20 + + +def read(name: str) -> bytes: + fd = os.open(os.path.join(STATE_DIR, name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + return f.read(LIMIT) + + +def write(name: str, data: bytes) -> None: + path = os.path.join(EVIDENCE, name) + temp = path + ".tmp" + fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, "wb") as f: + f.write(data) + os.replace(temp, path) + + +os.makedirs(EVIDENCE, exist_ok=True) +state = json.loads(read("state.json")) +try: + moves = read("moves.jsonl") +except FileNotFoundError: # no move was ever asked for + moves = b"" +write("moves.jsonl", moves) +write("final.json", json.dumps({**state, "exported_by": "maze"}).encode()) +print("exported", state.get("moves"), "moves; exit reached:", state.get("reached")) diff --git a/tasks/maze-t31/environment/maze/service.py b/tasks/maze-t31/environment/maze/service.py new file mode 100644 index 0000000000000000000000000000000000000000..d6269ea1836062e25cdb1fc742337731dfe6584c --- /dev/null +++ b/tasks/maze-t31/environment/maze/service.py @@ -0,0 +1,167 @@ +"""The maze service: one agent's own copy of the maze, behind `maze look` and `maze move ROOM`. + +Two routes answer, `GET /look` and `POST /move` (`{"room": }`), each with the current +room's observation (engine.Walk.look); every other request gets a 404 that names nothing. So the +tree and the exit stay in this container: an agent learns a room's doors only by standing in it, +and that a room is the exit only by reaching it. + +Every move, accepted or refused, is appended to the move log (agent, from, to, time) before it is +answered, and the walk's state file is replaced after it: success.py (flock's success_check) +and export.py (Harbor's collect hook, for the grader) read those two files. A restarted +container resumes from them. Python 3.10+, standard library only. +""" + +import hashlib +import json +import os +import sys +import threading +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.dont_write_bytecode = True # -I ignores PYTHONDONTWRITEBYTECODE +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from engine import Maze, Walk # noqa: E402 + +TREE = os.environ.get("MAZE_TREE", "/private/tree.json") +STATE_DIR = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") +PORT = int(os.environ.get("MAZE_PORT", "8000")) +# The Trial whose maze this is: compose's project name, `--tN__env` +AGENT = os.environ.get("MAZE_AGENT", "").removesuffix("__env") or "agent" +BODY_LIMIT = 4096 + + +def now() -> str: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds") + + +class Service: + def __init__(self, tree_path: str, state_dir: str, agent: str): + with open(tree_path, "rb") as f: + raw = f.read() + self.maze = Maze(json.loads(raw)) + self.tree_sha256 = hashlib.sha256(raw).hexdigest() + self.agent = agent + self.state_path = os.path.join(state_dir, "state.json") + self.log_path = os.path.join(state_dir, "moves.jsonl") + self.lock = threading.Lock() + os.makedirs(state_dir, exist_ok=True) + self.reached_at = None + if os.path.exists(self.state_path): # a restarted container + with open(self.state_path, encoding="utf-8") as f: + saved = json.load(f) + self.walk = Walk.from_state(self.maze, saved) + self.reached_at = saved.get("reached_at") + else: + self.walk = Walk(self.maze) + self.save() + + def save(self) -> None: + state = {"agent": self.agent, "start": self.maze.start, "budget": self.maze.budget, + "tree_sha256": self.tree_sha256, **self.walk.state(), + "reached_at": self.reached_at} + temp = self.state_path + ".tmp" + with open(temp, "w", encoding="utf-8") as f: + json.dump(state, f) + f.flush() + os.fsync(f.fileno()) + os.replace(temp, self.state_path) + + def look(self) -> dict: + with self.lock: + return self.walk.look() + + def move(self, room) -> tuple[dict, str | None]: + with self.lock: + here = self.walk.room + body, why = self.walk.move(room) + if why is None and self.walk.reached: + self.reached_at = now() + self.log(here, _logged(room), why) + return body, why + + def bad_request(self) -> None: + """A move request without a room: refused, and logged like any other.""" + with self.lock: + self.walk.refused += 1 + self.log(self.walk.room, None, "bad_request") + + def log(self, here: str, to, why: str | None) -> None: + record = {"agent": self.agent, "from": here, "to": to, "time": now(), "ok": why is None, + "moves": self.walk.moves} + if why is not None: + record["refused"] = why + with open(self.log_path, "a", encoding="utf-8") as f: + f.write(json.dumps(record) + "\n") + f.flush() + os.fsync(f.fileno()) + self.save() + + +def _logged(room): + """What the agent asked to move into, as the log keeps it (a string, at most 64 chars).""" + text = room if isinstance(room, str) else json.dumps(room) + return text[:64] + + +def handler(service: Service): + class Handler(BaseHTTPRequestHandler): + server_version = "maze" + sys_version = "" + + def reply(self, code: int, body: dict) -> None: + data = json.dumps(body).encode() + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def not_found(self) -> None: + self.reply(404, {"error": "not found: use `maze look` or `maze move ROOM`"}) + + def do_GET(self): # noqa: N802 + if self.path == "/look": + self.reply(200, service.look()) + else: + self.not_found() + + def do_POST(self): # noqa: N802 + if self.path != "/move": + return self.not_found() + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + length = -1 + room = None + if 0 < length <= BODY_LIMIT: + try: + room = json.loads(self.rfile.read(length)).get("room") + except (ValueError, AttributeError): + pass + if room is None: + service.bad_request() + return self.reply(400, {"error": 'send {"room": ""}'}) + body, why = service.move(room) + self.reply(200 if why is None else 409, body) + + def do_PUT(self): # noqa: N802 + self.not_found() + + do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = do_PUT # noqa: N815 + + def log_message(self, *args): # the move log is the record + pass + + return Handler + + +def main() -> None: + service = Service(TREE, STATE_DIR, AGENT) + server = ThreadingHTTPServer(("0.0.0.0", PORT), handler(service)) + server.daemon_threads = True + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tasks/maze-t31/environment/maze/success.py b/tasks/maze-t31/environment/maze/success.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fc1508ca212e92c6abb709cf1870536d29ba25 --- /dev/null +++ b/tasks/maze-t31/environment/maze/success.py @@ -0,0 +1,29 @@ +"""flock's success_check for s3-maze (`stop_on_success`), run in this maze service, never in the +agent's container: `python3 -I -S /app/success.py`. + +Read-only: it prints `{"success": , "evidence": {...}}` from the walk's +state file, which only service.py writes. No state yet, or one that can't be read, is not a +success. +""" + +import json +import os + +STATE = os.environ.get("MAZE_STATE_DIR", "/var/lib/maze") + "/state.json" + + +def read_state() -> dict | None: + try: + fd = os.open(STATE, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, "rb") as f: + state = json.loads(f.read(1 << 16)) + return state if isinstance(state, dict) else None + except (OSError, ValueError): + return None + + +state = read_state() +reached = bool(state) and state.get("reached") is True +evidence = ({"exit_reached": reached, "moves": state.get("moves"), + "reached_at": state.get("reached_at")} if state else {"state": "unreadable"}) +print(json.dumps({"success": reached, "evidence": evidence})) diff --git a/tasks/maze-t31/environment/maze/tree.json b/tasks/maze-t31/environment/maze/tree.json new file mode 100644 index 0000000000000000000000000000000000000000..6c8de86e255049e085a49e78d6cf685f6638bdb6 --- /dev/null +++ b/tasks/maze-t31/environment/maze/tree.json @@ -0,0 +1,1569 @@ +{ + "start": "R2357", + "exit": "R7718", + "budget": 210, + "doors": { + "R2357": [ + "R8101", + "R6432", + "R9982" + ], + "R8101": [ + "R2357", + "R6714" + ], + "R6714": [ + "R3882", + "R8101" + ], + "R3882": [ + "R6714", + "R4976" + ], + "R4976": [ + "R9520", + "R3882" + ], + "R9520": [ + "R9934", + "R4976" + ], + "R9934": [ + "R9520", + "R9823" + ], + "R9823": [ + "R1061", + "R9934" + ], + "R1061": [ + "R8212", + "R9823" + ], + "R8212": [ + "R1061", + "R6152" + ], + "R6152": [ + "R7977", + "R6790", + "R1630", + "R8212" + ], + "R6432": [ + "R4764", + "R2357" + ], + "R4764": [ + "R6432", + "R2424" + ], + "R2424": [ + "R8373", + "R4764" + ], + "R8373": [ + "R2424", + "R4923" + ], + "R4923": [ + "R7283", + "R8373" + ], + "R7283": [ + "R4583", + "R4923" + ], + "R4583": [ + "R7283", + "R9002" + ], + "R9002": [ + "R6609", + "R4583" + ], + "R6609": [ + "R1388", + "R9002" + ], + "R1388": [ + "R9217", + "R7071", + "R1007", + "R6609" + ], + "R9982": [ + "R2357", + "R9273" + ], + "R9273": [ + "R9982", + "R4952" + ], + "R4952": [ + "R9273", + "R4080" + ], + "R4080": [ + "R4952", + "R9844" + ], + "R9844": [ + "R1254", + "R4080" + ], + "R1254": [ + "R4305", + "R9844" + ], + "R4305": [ + "R1254", + "R6111" + ], + "R6111": [ + "R6310", + "R4305" + ], + "R6310": [ + "R6111", + "R1601" + ], + "R1601": [ + "R6310", + "R3706", + "R5722", + "R5360" + ], + "R6790": [ + "R7433", + "R6152" + ], + "R7433": [ + "R3187", + "R6790" + ], + "R3187": [ + "R7433", + "R9814" + ], + "R9814": [ + "R8651", + "R3187" + ], + "R8651": [ + "R2209", + "R9814" + ], + "R2209": [ + "R8651", + "R2536" + ], + "R2536": [ + "R2209", + "R9338" + ], + "R9338": [ + "R2536", + "R8118" + ], + "R8118": [ + "R9338", + "R8103" + ], + "R8103": [ + "R1489", + "R8118", + "R5559", + "R3758" + ], + "R7977": [ + "R4236", + "R6152" + ], + "R4236": [ + "R6114", + "R7977" + ], + "R6114": [ + "R1137", + "R4236" + ], + "R1137": [ + "R6369", + "R6114" + ], + "R6369": [ + "R2767", + "R1137" + ], + "R2767": [ + "R6369", + "R8501" + ], + "R8501": [ + "R4656", + "R2767" + ], + "R4656": [ + "R8501", + "R7445" + ], + "R7445": [ + "R1817", + "R4656" + ], + "R1817": [ + "R7445", + "R2284", + "R6906", + "R1037" + ], + "R1630": [ + "R6152", + "R9452" + ], + "R9452": [ + "R1120", + "R1630" + ], + "R1120": [ + "R9452", + "R1547" + ], + "R1547": [ + "R9498", + "R1120" + ], + "R9498": [ + "R6055", + "R1547" + ], + "R6055": [ + "R4272", + "R9498" + ], + "R4272": [ + "R6055", + "R9092" + ], + "R9092": [ + "R8363", + "R4272" + ], + "R8363": [ + "R3947", + "R9092" + ], + "R3947": [ + "R8363", + "R4511", + "R5272", + "R4531" + ], + "R1007": [ + "R1388", + "R8664" + ], + "R8664": [ + "R3224", + "R1007" + ], + "R3224": [ + "R9077", + "R8664" + ], + "R9077": [ + "R7015", + "R3224" + ], + "R7015": [ + "R9077", + "R6095" + ], + "R6095": [ + "R4989", + "R7015" + ], + "R4989": [ + "R1524", + "R6095" + ], + "R1524": [ + "R9875", + "R4989" + ], + "R9875": [ + "R7686", + "R1524" + ], + "R7686": [ + "R2161", + "R9875", + "R6829", + "R7540" + ], + "R7071": [ + "R2731", + "R1388" + ], + "R2731": [ + "R7071", + "R8945" + ], + "R8945": [ + "R2731", + "R8410" + ], + "R8410": [ + "R8945", + "R6335" + ], + "R6335": [ + "R1583", + "R8410" + ], + "R1583": [ + "R7192", + "R6335" + ], + "R7192": [ + "R5542", + "R1583" + ], + "R5542": [ + "R7192", + "R7787" + ], + "R7787": [ + "R8413", + "R5542" + ], + "R8413": [ + "R7316", + "R6439", + "R9614", + "R7787" + ], + "R9217": [ + "R1388", + "R5594" + ], + "R5594": [ + "R1483", + "R9217" + ], + "R1483": [ + "R5594", + "R4405" + ], + "R4405": [ + "R8632", + "R1483" + ], + "R8632": [ + "R4405", + "R3287" + ], + "R3287": [ + "R5725", + "R8632" + ], + "R5725": [ + "R3287", + "R5030" + ], + "R5030": [ + "R8511", + "R5725" + ], + "R8511": [ + "R5030", + "R6418" + ], + "R6418": [ + "R4757", + "R9701", + "R8511", + "R7421" + ], + "R3706": [ + "R1601", + "R5503" + ], + "R5503": [ + "R9730", + "R3706" + ], + "R9730": [ + "R4083", + "R5503" + ], + "R4083": [ + "R9730", + "R4869" + ], + "R4869": [ + "R1973", + "R4083" + ], + "R1973": [ + "R4159", + "R4869" + ], + "R4159": [ + "R8453", + "R1973" + ], + "R8453": [ + "R2920", + "R4159" + ], + "R2920": [ + "R4837", + "R8453" + ], + "R4837": [ + "R1941", + "R9381", + "R8416", + "R2920" + ], + "R5722": [ + "R1601", + "R3082" + ], + "R3082": [ + "R5722", + "R2976" + ], + "R2976": [ + "R3082", + "R4169" + ], + "R4169": [ + "R4444", + "R2976" + ], + "R4444": [ + "R9449", + "R4169" + ], + "R9449": [ + "R8119", + "R4444" + ], + "R8119": [ + "R9949", + "R9449" + ], + "R9949": [ + "R9686", + "R8119" + ], + "R9686": [ + "R9949", + "R2570" + ], + "R2570": [ + "R9182", + "R9686", + "R7079", + "R1670" + ], + "R5360": [ + "R7345", + "R1601" + ], + "R7345": [ + "R5360", + "R4678" + ], + "R4678": [ + "R7345", + "R2000" + ], + "R2000": [ + "R4678", + "R9734" + ], + "R9734": [ + "R2000", + "R5832" + ], + "R5832": [ + "R5019", + "R9734" + ], + "R5019": [ + "R5832", + "R2983" + ], + "R2983": [ + "R5019", + "R3519" + ], + "R3519": [ + "R1837", + "R2983" + ], + "R1837": [ + "R3238", + "R9545", + "R3519", + "R9390" + ], + "R3758": [ + "R8103", + "R8225" + ], + "R8225": [ + "R5959", + "R3758" + ], + "R5959": [ + "R8225", + "R8560" + ], + "R8560": [ + "R5959", + "R3689" + ], + "R3689": [ + "R4577", + "R8560" + ], + "R4577": [ + "R1875", + "R3689" + ], + "R1875": [ + "R9237", + "R4577" + ], + "R9237": [ + "R1875", + "R3623" + ], + "R3623": [ + "R9237", + "R6268" + ], + "R6268": [ + "R3623" + ], + "R1489": [ + "R8103", + "R8434" + ], + "R8434": [ + "R1489", + "R5059" + ], + "R5059": [ + "R8434", + "R3835" + ], + "R3835": [ + "R5745", + "R5059" + ], + "R5745": [ + "R1017", + "R3835" + ], + "R1017": [ + "R5745", + "R6245" + ], + "R6245": [ + "R1017", + "R9675" + ], + "R9675": [ + "R6245", + "R7596" + ], + "R7596": [ + "R7718", + "R9675" + ], + "R7718": [ + "R7596" + ], + "R5559": [ + "R8103", + "R2392" + ], + "R2392": [ + "R4025", + "R5559" + ], + "R4025": [ + "R2392", + "R4995" + ], + "R4995": [ + "R3400", + "R4025" + ], + "R3400": [ + "R4995", + "R2766" + ], + "R2766": [ + "R3400", + "R8262" + ], + "R8262": [ + "R1775", + "R2766" + ], + "R1775": [ + "R8262", + "R1260" + ], + "R1260": [ + "R5685", + "R1775" + ], + "R5685": [ + "R1260" + ], + "R6906": [ + "R1986", + "R1817" + ], + "R1986": [ + "R4319", + "R6906" + ], + "R4319": [ + "R8625", + "R1986" + ], + "R8625": [ + "R4319", + "R3491" + ], + "R3491": [ + "R2086", + "R8625" + ], + "R2086": [ + "R1276", + "R3491" + ], + "R1276": [ + "R6034", + "R2086" + ], + "R6034": [ + "R5178", + "R1276" + ], + "R5178": [ + "R6034", + "R9621" + ], + "R9621": [ + "R5178" + ], + "R2284": [ + "R1817", + "R2526" + ], + "R2526": [ + "R2284", + "R6281" + ], + "R6281": [ + "R2394", + "R2526" + ], + "R2394": [ + "R5398", + "R6281" + ], + "R5398": [ + "R8428", + "R2394" + ], + "R8428": [ + "R5398", + "R5476" + ], + "R5476": [ + "R8428", + "R1824" + ], + "R1824": [ + "R2537", + "R5476" + ], + "R2537": [ + "R7314", + "R1824" + ], + "R7314": [ + "R2537" + ], + "R1037": [ + "R1817", + "R3761" + ], + "R3761": [ + "R1037", + "R1029" + ], + "R1029": [ + "R3761", + "R9937" + ], + "R9937": [ + "R1029", + "R3881" + ], + "R3881": [ + "R4598", + "R9937" + ], + "R4598": [ + "R3881", + "R9007" + ], + "R9007": [ + "R7996", + "R4598" + ], + "R7996": [ + "R9007", + "R5983" + ], + "R5983": [ + "R5521", + "R7996" + ], + "R5521": [ + "R5983" + ], + "R4511": [ + "R3947", + "R4432" + ], + "R4432": [ + "R1855", + "R4511" + ], + "R1855": [ + "R4432", + "R5801" + ], + "R5801": [ + "R1855", + "R9972" + ], + "R9972": [ + "R5801", + "R2861" + ], + "R2861": [ + "R9972", + "R5555" + ], + "R5555": [ + "R8108", + "R2861" + ], + "R8108": [ + "R5555", + "R2732" + ], + "R2732": [ + "R2594", + "R8108" + ], + "R2594": [ + "R2732" + ], + "R5272": [ + "R2397", + "R3947" + ], + "R2397": [ + "R5272", + "R9427" + ], + "R9427": [ + "R9397", + "R2397" + ], + "R9397": [ + "R9427", + "R1905" + ], + "R1905": [ + "R9203", + "R9397" + ], + "R9203": [ + "R1905", + "R9923" + ], + "R9923": [ + "R9203", + "R2960" + ], + "R2960": [ + "R9923", + "R1836" + ], + "R1836": [ + "R2960", + "R6341" + ], + "R6341": [ + "R1836" + ], + "R4531": [ + "R3947", + "R3084" + ], + "R3084": [ + "R4531", + "R6573" + ], + "R6573": [ + "R9059", + "R3084" + ], + "R9059": [ + "R8376", + "R6573" + ], + "R8376": [ + "R9059", + "R2120" + ], + "R2120": [ + "R8376", + "R6512" + ], + "R6512": [ + "R6022", + "R2120" + ], + "R6022": [ + "R1358", + "R6512" + ], + "R1358": [ + "R6022", + "R8246" + ], + "R8246": [ + "R1358" + ], + "R7540": [ + "R7686", + "R3662" + ], + "R3662": [ + "R8887", + "R7540" + ], + "R8887": [ + "R3662", + "R9551" + ], + "R9551": [ + "R8887", + "R2376" + ], + "R2376": [ + "R4715", + "R9551" + ], + "R4715": [ + "R4858", + "R2376" + ], + "R4858": [ + "R9135", + "R4715" + ], + "R9135": [ + "R1013", + "R4858" + ], + "R1013": [ + "R8880", + "R9135" + ], + "R8880": [ + "R1013" + ], + "R2161": [ + "R7686", + "R3965" + ], + "R3965": [ + "R3360", + "R2161" + ], + "R3360": [ + "R7414", + "R3965" + ], + "R7414": [ + "R5420", + "R3360" + ], + "R5420": [ + "R7414", + "R8335" + ], + "R8335": [ + "R5420", + "R5513" + ], + "R5513": [ + "R8335", + "R9116" + ], + "R9116": [ + "R5513", + "R1492" + ], + "R1492": [ + "R8525", + "R9116" + ], + "R8525": [ + "R1492" + ], + "R6829": [ + "R2837", + "R7686" + ], + "R2837": [ + "R3138", + "R6829" + ], + "R3138": [ + "R2837", + "R1791" + ], + "R1791": [ + "R3138", + "R4852" + ], + "R4852": [ + "R3553", + "R1791" + ], + "R3553": [ + "R2311", + "R4852" + ], + "R2311": [ + "R3553", + "R4933" + ], + "R4933": [ + "R2311", + "R6540" + ], + "R6540": [ + "R4933", + "R7562" + ], + "R7562": [ + "R6540" + ], + "R6439": [ + "R3190", + "R8413" + ], + "R3190": [ + "R6439", + "R5993" + ], + "R5993": [ + "R3190", + "R6983" + ], + "R6983": [ + "R3541", + "R5993" + ], + "R3541": [ + "R4936", + "R6983" + ], + "R4936": [ + "R3541", + "R4258" + ], + "R4258": [ + "R4116", + "R4936" + ], + "R4116": [ + "R4258", + "R4047" + ], + "R4047": [ + "R7198", + "R4116" + ], + "R7198": [ + "R4047" + ], + "R9614": [ + "R8576", + "R8413" + ], + "R8576": [ + "R6065", + "R9614" + ], + "R6065": [ + "R8372", + "R8576" + ], + "R8372": [ + "R6065", + "R1333" + ], + "R1333": [ + "R8372", + "R7340" + ], + "R7340": [ + "R9490", + "R1333" + ], + "R9490": [ + "R7340", + "R3318" + ], + "R3318": [ + "R5181", + "R9490" + ], + "R5181": [ + "R2592", + "R3318" + ], + "R2592": [ + "R5181" + ], + "R7316": [ + "R8413", + "R6723" + ], + "R6723": [ + "R2780", + "R7316" + ], + "R2780": [ + "R2503", + "R6723" + ], + "R2503": [ + "R5606", + "R2780" + ], + "R5606": [ + "R2503", + "R7890" + ], + "R7890": [ + "R2388", + "R5606" + ], + "R2388": [ + "R2817", + "R7890" + ], + "R2817": [ + "R9179", + "R2388" + ], + "R9179": [ + "R2817", + "R1566" + ], + "R1566": [ + "R9179" + ], + "R4757": [ + "R6418", + "R8420" + ], + "R8420": [ + "R9953", + "R4757" + ], + "R9953": [ + "R8420", + "R3312" + ], + "R3312": [ + "R8957", + "R9953" + ], + "R8957": [ + "R9853", + "R3312" + ], + "R9853": [ + "R8957", + "R2699" + ], + "R2699": [ + "R3204", + "R9853" + ], + "R3204": [ + "R8477", + "R2699" + ], + "R8477": [ + "R6961", + "R3204" + ], + "R6961": [ + "R8477" + ], + "R9701": [ + "R9998", + "R6418" + ], + "R9998": [ + "R9701", + "R3796" + ], + "R3796": [ + "R3045", + "R9998" + ], + "R3045": [ + "R3796", + "R2047" + ], + "R2047": [ + "R3045", + "R7116" + ], + "R7116": [ + "R2047", + "R8002" + ], + "R8002": [ + "R7116", + "R3640" + ], + "R3640": [ + "R8002", + "R8215" + ], + "R8215": [ + "R3640", + "R1270" + ], + "R1270": [ + "R8215" + ], + "R7421": [ + "R6036", + "R6418" + ], + "R6036": [ + "R3823", + "R7421" + ], + "R3823": [ + "R3331", + "R6036" + ], + "R3331": [ + "R1736", + "R3823" + ], + "R1736": [ + "R7277", + "R3331" + ], + "R7277": [ + "R8241", + "R1736" + ], + "R8241": [ + "R7277", + "R8516" + ], + "R8516": [ + "R8346", + "R8241" + ], + "R8346": [ + "R5675", + "R8516" + ], + "R5675": [ + "R8346" + ], + "R8416": [ + "R9408", + "R4837" + ], + "R9408": [ + "R9847", + "R8416" + ], + "R9847": [ + "R9408", + "R9613" + ], + "R9613": [ + "R9847", + "R9529" + ], + "R9529": [ + "R8349", + "R9613" + ], + "R8349": [ + "R9529", + "R3322" + ], + "R3322": [ + "R4463", + "R8349" + ], + "R4463": [ + "R2064", + "R3322" + ], + "R2064": [ + "R6207", + "R4463" + ], + "R6207": [ + "R2064" + ], + "R1941": [ + "R4837", + "R5002" + ], + "R5002": [ + "R1941", + "R4058" + ], + "R4058": [ + "R5002", + "R5910" + ], + "R5910": [ + "R4058", + "R8352" + ], + "R8352": [ + "R7722", + "R5910" + ], + "R7722": [ + "R7755", + "R8352" + ], + "R7755": [ + "R7722", + "R3044" + ], + "R3044": [ + "R8297", + "R7755" + ], + "R8297": [ + "R2198", + "R3044" + ], + "R2198": [ + "R8297" + ], + "R9381": [ + "R4433", + "R4837" + ], + "R4433": [ + "R7692", + "R9381" + ], + "R7692": [ + "R1491", + "R4433" + ], + "R1491": [ + "R2820", + "R7692" + ], + "R2820": [ + "R1683", + "R1491" + ], + "R1683": [ + "R2820", + "R9507" + ], + "R9507": [ + "R7530", + "R1683" + ], + "R7530": [ + "R6526", + "R9507" + ], + "R6526": [ + "R7398", + "R7530" + ], + "R7398": [ + "R6526" + ], + "R1670": [ + "R2570", + "R8609" + ], + "R8609": [ + "R5835", + "R1670" + ], + "R5835": [ + "R8443", + "R8609" + ], + "R8443": [ + "R5835", + "R2037" + ], + "R2037": [ + "R9594", + "R8443" + ], + "R9594": [ + "R2037", + "R4158" + ], + "R4158": [ + "R9594", + "R4110" + ], + "R4110": [ + "R4158", + "R9029" + ], + "R9029": [ + "R4110", + "R4291" + ], + "R4291": [ + "R9029" + ], + "R9182": [ + "R2570", + "R9101" + ], + "R9101": [ + "R1404", + "R9182" + ], + "R1404": [ + "R9170", + "R9101" + ], + "R9170": [ + "R1404", + "R1765" + ], + "R1765": [ + "R9170", + "R1945" + ], + "R1945": [ + "R1765", + "R5040" + ], + "R5040": [ + "R1945", + "R1725" + ], + "R1725": [ + "R5040", + "R7728" + ], + "R7728": [ + "R1725", + "R6561" + ], + "R6561": [ + "R7728" + ], + "R7079": [ + "R6456", + "R2570" + ], + "R6456": [ + "R3280", + "R7079" + ], + "R3280": [ + "R2012", + "R6456" + ], + "R2012": [ + "R5128", + "R3280" + ], + "R5128": [ + "R4517", + "R2012" + ], + "R4517": [ + "R5979", + "R5128" + ], + "R5979": [ + "R9750", + "R4517" + ], + "R9750": [ + "R2678", + "R5979" + ], + "R2678": [ + "R1457", + "R9750" + ], + "R1457": [ + "R2678" + ], + "R3238": [ + "R9666", + "R1837" + ], + "R9666": [ + "R3238", + "R2081" + ], + "R2081": [ + "R3683", + "R9666" + ], + "R3683": [ + "R9644", + "R2081" + ], + "R9644": [ + "R1086", + "R3683" + ], + "R1086": [ + "R9204", + "R9644" + ], + "R9204": [ + "R6711", + "R1086" + ], + "R6711": [ + "R9204", + "R1841" + ], + "R1841": [ + "R6711", + "R8643" + ], + "R8643": [ + "R1841" + ], + "R9545": [ + "R1837", + "R9715" + ], + "R9715": [ + "R5248", + "R9545" + ], + "R5248": [ + "R9715", + "R7851" + ], + "R7851": [ + "R5248", + "R8088" + ], + "R8088": [ + "R3161", + "R7851" + ], + "R3161": [ + "R8088", + "R3488" + ], + "R3488": [ + "R2068", + "R3161" + ], + "R2068": [ + "R7818", + "R3488" + ], + "R7818": [ + "R7988", + "R2068" + ], + "R7988": [ + "R7818" + ], + "R9390": [ + "R1837", + "R3292" + ], + "R3292": [ + "R9390", + "R4295" + ], + "R4295": [ + "R4173", + "R3292" + ], + "R4173": [ + "R1682", + "R4295" + ], + "R1682": [ + "R7139", + "R4173" + ], + "R7139": [ + "R9043", + "R1682" + ], + "R9043": [ + "R8091", + "R7139" + ], + "R8091": [ + "R9043", + "R7667" + ], + "R7667": [ + "R8091", + "R2244" + ], + "R2244": [ + "R7667" + ] + } +} diff --git a/tasks/maze-t31/instruction.md b/tasks/maze-t31/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..077ef2282867fb9756aec75184f16040b03b689e --- /dev/null +++ b/tasks/maze-t31/instruction.md @@ -0,0 +1,26 @@ +# Find the exit of the maze + +You are exploring a maze of rooms connected by doors to find the exit. The maze is a tree: from +the start room, corridors of rooms lead to junctions where the way divides, and the exit is one +of the dead ends at the far ends of the corridors. Every agent on your team starts in the same +room, in its own copy of the same maze: the same rooms, with the same doors listed in the same +order. Your position is your own: moving never moves a teammate. + +Explore with the `maze` command in your shell: + +- `maze look` describes your current room again, without moving; +- `maze move ROOM` walks through a door of your current room into the named adjacent room. + +Each prints JSON: the room you are in (`room`), its doors (`doors`), whether it is the exit +(`exit`), the room you arrived from (`arrived_from`) and how many moves you have left +(`moves_left`), with a `notice` at a dead end. Only the doors listed for your current room are +reachable in one move; to go back, move to the room you arrived from. Room names carry no +information about where the exit is. + +You can make at most 210 moves. Looking is free, and a refused move (into a room that is not +one of your doors) costs nothing. + +Reaching the exit solves the task for the whole team and ends the run. If you have teammates +(your instructions name them), they are searching the same maze: what one of you learns about it +holds for everyone. Use the message board to share what you find and to read what they find. +Once you reach the exit, post that you have, then stop. diff --git a/tasks/maze-t31/solution/solve.sh b/tasks/maze-t31/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e3bcb9e5a715805b90304b324b7129edcf915c7c --- /dev/null +++ b/tasks/maze-t31/solution/solve.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Oracle: walk the shortest path to the exit with `maze move`, from its own copy of the path. +set -e +for room in R8101 R6714 R3882 R4976 R9520 R9934 R9823 R1061 R8212 R6152 R6790 R7433 R3187 R9814 R8651 R2209 R2536 R9338 R8118 R8103 R1489 R8434 R5059 R3835 R5745 R1017 R6245 R9675 R7596 R7718; do + maze move "$room" > /dev/null +done +maze look diff --git a/tasks/maze-t31/task.toml b/tasks/maze-t31/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..26a681fad292cf73b038513bc7899e0773b17039 --- /dev/null +++ b/tasks/maze-t31/task.toml @@ -0,0 +1,48 @@ +schema_version = "1.4" + +# The maze service's own record of the walk, exported by its collect hook below once the agent +# is done: the grader's only input besides tests/expected.json. +[[artifacts]] +source = "/evidence/final.json" +service = "maze" + +[[artifacts]] +source = "/evidence/moves.jsonl" +service = "maze" + +[task] +name = "HuggingFaceSwarms/maze-t31" +version = "0.1.0" +description = "s3-maze: agents search their own copies of one maze; the first one out wins." +keywords = ["multi-agent", "message-board", "s3", "maze", "exploration"] + +[metadata] +tree = 31 +seed = 115 +branching = 3 +corridor = 10 +depth = 3 +rooms = 391 +dead_ends = 27 +shortest_path = 30 +full_search = 780 +move_budget = 210 + +[agent] +timeout_sec = 3600 + +[environment] +build_timeout_sec = 900 +cpus = 1 +memory_mb = 1024 + +[verifier] +# The grader runs in its own container (tests/Dockerfile) after the agent's has stopped, on +# what the maze service exported: nothing the agent's container holds reaches it. +environment_mode = "separate" +timeout_sec = 120 + +[[verifier.collect]] +service = "maze" +command = "python3 -I -S /app/export.py" +timeout_sec = 30