Download template/tests/grade.py from HuggingFaceSwarms/s3-maze: direct link, hf CLI and curl.
- Browser
- Download file 4.78 kB
-
https://huggingface.co/datasets/HuggingFaceSwarms/s3-maze/resolve/main/template/tests/grade.py
- Command line
-
hf download hf://datasets/HuggingFaceSwarms/s3-maze/template/tests/grade.py
-
curl -L -o grade.py https://huggingface.co/datasets/HuggingFaceSwarms/s3-maze/resolve/main/template/tests/grade.py
4.78 kB
| """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) | |