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47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python3
"""Grade one openai/math challenge with Comparator.

This runs as root in a separate verifier sandbox that never executed agent code. The only input the
agent controls is /logs/artifacts/submission.tar. Everything else comes from /tests (this task's
trusted challenge and Comparator config) and from the image.

Steps:
  1. Unpack submission.tar, accepting only regular `Submission.lean` / `Submission/**.lean` files.
  2. Lay out a fresh Lake project: the trusted lakefile, Challenge.lean from /tests, the submission,
     and the image's prebuilt Mathlib (root-owned, read-only to the grader).
  3. Run `lake env comparator config.json` as the unprivileged `grader` user, with no network.
     Comparator builds Challenge and Submission inside landrun, checks that every listed theorem
     has exactly the challenge's statement, that only the permitted axioms are used, and replays
     the proof through the Lean kernel.
  4. Kill everything the grader started, then write the verdict.

reward.txt is 1 only when Comparator prints its success line and exits 0. A rejected or
non-compiling submission scores 0. A broken grading setup (the trusted Challenge fails to build,
the network block cannot be installed) writes no reward, so Harbor reports an error instead of a
score.
"""

from __future__ import annotations

import json
import os
import pwd
import re
import shutil
import signal
import socket
import stat
import subprocess
import sys
import tarfile
import tempfile
import threading
import time
from pathlib import Path

TESTS = Path("/tests")
VERIFIER_DIR = Path("/logs/verifier")
SUBMISSION_TAR = Path("/logs/artifacts/submission.tar")
LEAN_ENV = Path("/opt/lean-env")
RUN_ROOT = Path("/var/lib/oai-grade")
GRADER = "grader"
SUCCESS_LINE = "Your solution is okay!"
GRADE_THREADS = "2"
SCRATCH_SWEEP_SEC = 15
# Lake and Comparator wording when a build process is killed for running out of memory.
OOM_MARKERS = ("Lean exited with code 137", "Child exited with 137")

MAX_FILES = 20_000
MAX_FILE_BYTES = 64 << 20
MAX_TOTAL_BYTES = 768 << 20
# Submission.lean, or Submission/A/B.lean where each component is a Lean identifier: it starts with
# a letter or `_` and continues with letters, digits (subscripts like `₁` included), `_`, `'`, `!`, `?`.
# Unicode is allowed because Lean identifiers allow it (openai/math has a module named `Kappa₁`).
_COMPONENT = r"[^\W\d][\w'!?]*"
MEMBER = re.compile(rf"^Submission(?:/{_COMPONENT})*\.lean$")
MEMBER_DIR = re.compile(rf"^Submission(?:/{_COMPONENT})*/?$")

LAKEFILE = """import Lake
open Lake DSL

package openai_math where
  leanOptions := #[⟨`autoImplicit, false⟩]

require mathlib from git
  "https://github.com/leanprover-community/mathlib4.git" @ "d13f23b723b8a846827a245b89c10fc7d3f11612"

lean_lib Challenge

-- `Submission` and every `Submission.*` module it imports.
lean_lib Submission
"""


class Rejected(Exception):
    """The submission is invalid: reward 0."""


class GradingError(Exception):
    """The grading setup is broken: no reward, so the trial shows as errored."""


def toolchain_bin() -> Path:
    bins = sorted(Path("/opt/elan/toolchains").glob("*/bin"))
    if len(bins) != 1:
        raise GradingError(f"expected one Lean toolchain, found {bins}")
    return bins[0]


def unpack_submission(dest: Path) -> dict:
    """Copy the submission's .lean files into ``dest``; reject anything else."""
    try:
        st = SUBMISSION_TAR.lstat()
    except FileNotFoundError:
        raise Rejected("no submission: /workspace/Submission.lean was not found after the agent finished")
    if not SUBMISSION_TAR.is_file() or SUBMISSION_TAR.is_symlink() or st.st_size > MAX_TOTAL_BYTES + (64 << 20):
        raise Rejected("submission archive is not a regular file of acceptable size")

    files, total = [], 0
    try:
        with tarfile.open(SUBMISSION_TAR, "r:") as tar:
            for count, member in enumerate(tar):
                if count > MAX_FILES * 2:
                    raise Rejected(f"submission has more than {MAX_FILES} entries")
                name = member.name.removeprefix("./")
                if member.isdir():
                    if not MEMBER_DIR.match(name):
                        raise Rejected(f"unexpected directory in submission: {name!r}")
                    continue
                if not member.isreg():
                    raise Rejected(f"only regular .lean files are accepted, got {name!r}")
                if not MEMBER.match(name):
                    raise Rejected(f"unexpected file in submission: {name!r} (only Submission.lean and Submission/**.lean)")
                if member.size > MAX_FILE_BYTES:
                    raise Rejected(f"{name} is larger than {MAX_FILE_BYTES >> 20} MB")
                total += member.size
                if total > MAX_TOTAL_BYTES or len(files) >= MAX_FILES:
                    raise Rejected("submission is too large")
                data = tar.extractfile(member).read(MAX_FILE_BYTES + 1)
                try:
                    data.decode("utf-8")
                except UnicodeDecodeError:
                    raise Rejected(f"{name} is not valid UTF-8")
                target = dest / name
                if target.exists():
                    raise Rejected(f"duplicate entry {name!r}")
                target.parent.mkdir(parents=True, exist_ok=True)
                target.write_bytes(data)
                target.chmod(0o644)
                files.append(name)
    except tarfile.TarError as exc:
        raise Rejected(f"submission archive is unreadable: {exc}")
    if "Submission.lean" not in files:
        raise Rejected("no submission: /workspace/Submission.lean was not found after the agent finished")
    for d in (dest / "Submission",):
        if d.exists():
            for p in [d, *d.rglob("*")]:
                if p.is_dir():
                    p.chmod(0o755)
    return {"files": len(files), "bytes": total}


def write_project(run: Path, config: dict) -> None:
    (run / "lakefile.lean").write_text(LAKEFILE)
    shutil.copy(LEAN_ENV / "lake-manifest.json", run / "lake-manifest.json")
    shutil.copy(LEAN_ENV / "lean-toolchain", run / "lean-toolchain")
    shutil.copy(TESTS / "Challenge.lean", run / "Challenge.lean")
    comparator_config = {
        "challenge_module": "Challenge",
        "solution_module": "Submission",
        "theorem_names": config["theorem_names"],
        "definition_names": config["definition_names"],
        "permitted_axioms": config["permitted_axioms"],
        "enable_nanoda": False,
    }
    (run / "config.json").write_text(json.dumps(comparator_config, indent=2))
    lake = run / ".lake"
    lake.mkdir()
    (lake / "packages").symlink_to(LEAN_ENV / ".lake" / "packages")
    for p in run.iterdir():
        if p.name != ".lake":
            os.chmod(p, 0o644 if p.is_file() else 0o755, follow_symlinks=False)
    uid = pwd.getpwnam(GRADER).pw_uid
    os.chown(lake, uid, uid)
    os.chmod(run, 0o755)


def fresh_run(task: dict) -> Path:
    """A new, empty Lake project for one grading attempt."""
    run = Path(tempfile.mkdtemp(prefix="run.", dir=RUN_ROOT))
    write_project(run, task["comparator"])
    return run


def ensure_readable(roots: tuple[str, ...] = ("/opt/elan", str(LEAN_ENV))) -> int:
    """Make the prebuilt toolchain and packages readable by the grader (never writable).

    The image already does this; this only repairs an image built without it. It must touch
    nothing that is already right: these files live in the image's lower overlay layer, and a
    `chmod` on one copies it whole into the sandbox's writable layer. A blanket `chmod -R` took
    535 s and used 8.7 GB of the 10 GB working disk. This walk only reads metadata (lstat).
    Returns the number of entries changed.
    """
    changed = 0
    for root in roots:
        for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
            for name in [*dirnames, *filenames]:
                path = os.path.join(dirpath, name)
                st = os.lstat(path)
                if stat.S_ISDIR(st.st_mode):
                    want = (st.st_mode | 0o555) & ~0o022
                elif stat.S_ISREG(st.st_mode):
                    want = (st.st_mode | 0o444) & ~0o022
                else:
                    continue
                if stat.S_IMODE(st.st_mode) != stat.S_IMODE(want):
                    os.chmod(path, stat.S_IMODE(want))
                    changed += 1
    return changed


def block_grader_network() -> None:
    """Reject every socket the grader user opens, loopback included.

    landrun already denies TCP to the sandboxed build. This rule also covers the grader's own
    processes outside landrun and services listening inside the sandbox.
    """
    uid = str(pwd.getpwnam(GRADER).pw_uid)
    for tool in ("iptables", "ip6tables"):
        rule = ["OUTPUT", "-m", "owner", "--uid-owner", uid, "-j", "REJECT"]
        if subprocess.run([tool, "-C", *rule], capture_output=True).returncode == 0:
            continue
        res = subprocess.run([tool, "-I", *rule], capture_output=True, text=True)
        if res.returncode != 0:
            raise GradingError(f"cannot install the {tool} rule for {GRADER}: {res.stderr.strip()}")
    probe = subprocess.run(
        ["runuser", "-u", GRADER, "--", sys.executable, "-c",
         "import socket,sys\n"
         "s=socket.socket(); s.settimeout(3)\n"
         "sys.exit(0 if s.connect_ex(('127.0.0.1', 2280)) == 0 else 1)"],
        capture_output=True,
    )
    if probe.returncode == 0:
        raise GradingError("the grader user can still open loopback connections")


def kill_grader() -> None:
    for _ in range(20):
        subprocess.run(["pkill", "-9", "-u", GRADER], capture_output=True)
        if subprocess.run(["pgrep", "-u", GRADER], capture_output=True).returncode != 0:
            return
        time.sleep(0.5)
    raise GradingError("could not stop the grader's processes")


def out_of_memory(out: str) -> bool:
    return any(m in out for m in OOM_MARKERS)


def out_of_disk(out: str) -> bool:
    return "no space left on device" in out.lower()


# Run as the grader user by `empty_build_scratch`. argv: the .lake/build/ir directory.
EMPTY_SCRATCH = r"""
import os, sys
for dirpath, _, files in os.walk(sys.argv[1]):
    for name in files:
        if not name.endswith(".setup.json"):
            continue
        base = os.path.join(dirpath, name[:-len(".setup.json")])
        try:
            # Lake writes X.c.hash once Lean, and leanir if the module needs it, are done with X;
            # both read X.setup.json, so it may only be emptied after that.
            if os.stat(base + ".c.hash").st_mtime < os.stat(base + ".setup.json").st_mtime:
                continue
            for path in (base + ".setup.json", base + ".c"):
                if os.path.getsize(path):
                    os.truncate(path, 0)
        except OSError:
            pass
"""


def empty_build_scratch(run: Path, stop: threading.Event) -> None:
    """Until ``stop`` is set, empty files Lake leaves in .lake/build/ir that grading never reads.

    For every module Lake writes a `.setup.json` (its imports with the path of every .olean they
    pull in, about 5.5 MB with Mathlib) and the module compiled to C. Neither is used once the
    module is built, and for proofs of 1,000-2,000 modules they filled the 10 GB disk before the
    build finished. Lake keeps the recorded hashes, so emptied files do not trigger a rebuild.
    This runs as the grader user, so it can touch nothing the build could not.
    """
    ir = str(run / ".lake" / "build" / "ir")
    while not stop.wait(SCRATCH_SWEEP_SEC):
        try:
            subprocess.run(["runuser", "-u", GRADER, "--", sys.executable, "-c", EMPTY_SCRATCH, ir],
                           capture_output=True, timeout=600)
        except subprocess.TimeoutExpired:
            pass


def run_comparator(run: Path, timeout_sec: int, threads: str = GRADE_THREADS) -> tuple[int | None, str]:
    env = {
        "PATH": f"{toolchain_bin()}:/usr/local/bin:/usr/bin:/bin",
        "HOME": str(run),
        "LANG": "C.UTF-8",
        "LEAN_ABORT_ON_PANIC": "1",
        # Lean sees every host CPU, not the sandbox's 4; bound Lake's parallel builds so a large
        # proof fits in 8 GB. Needs the image's Comparator, which passes this into landrun.
        "LEAN_NUM_THREADS": threads,
    }
    cmd = [
        "runuser", "-u", GRADER, "--", "env", "-i", *(f"{k}={v}" for k, v in env.items()),
        "lake", "env", "comparator", "config.json",
    ]
    stop = threading.Event()
    sweeper = threading.Thread(target=empty_build_scratch, args=(run, stop), daemon=True)
    sweeper.start()
    try:
        code, stdout, stderr = run_with_timeout(
            cmd, cwd=run, timeout_sec=timeout_sec,
            kill_extra=lambda: subprocess.run(["pkill", "-9", "-u", GRADER], capture_output=True),
        )
    finally:
        stop.set()
        sweeper.join()
    return code, stdout + "\n--- stderr ---\n" + stderr


def run_with_timeout(cmd: list[str], cwd: Path, timeout_sec: int, kill_extra=None) -> tuple[int | None, str, str]:
    """Run ``cmd``; on timeout kill its whole process tree and return code None.

    `timeout(1)` only signals its direct child, so Lean processes started by Lake under landrun
    kept the output pipe open and the grader waited past Harbor's own limit. Here the command
    gets its own session; on timeout its process group is killed, and ``kill_extra`` (every
    process of the grader user) catches anything that left the group.
    """
    proc = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                            text=True, errors="replace", start_new_session=True)
    try:
        stdout, stderr = proc.communicate(timeout=timeout_sec)
        return proc.returncode, stdout, stderr
    except subprocess.TimeoutExpired:
        try:
            os.killpg(proc.pid, signal.SIGKILL)
        except ProcessLookupError:
            pass
        if kill_extra:
            kill_extra()
        stdout, stderr = proc.communicate()
        return None, stdout, stderr


def classify(code: int | None, out: str) -> tuple[int | None, str]:
    """Map Comparator's result to (reward, reason). reward None means a grading error."""
    if code == 0 and SUCCESS_LINE in out:
        return 1, "accepted: statements match, only permitted axioms, kernel replay passed"
    if "Building Submission" not in out:
        # Comparator stopped before it touched the submission: the trusted challenge or the
        # toolchain is broken (or too slow), which says nothing about the agent's proof.
        return None, ("timed out before the submission was built" if code is None
                      else "the trusted Challenge failed to build or export")
    if code is None:
        return 0, "timed out while building or checking the submission"
    tail = [l for l in out.splitlines() if l.strip() and "has local changes" not in l][-25:]
    if out_of_disk(out):
        return 0, "rejected: the build ran out of disk space (10 GB) | " + " | ".join(tail)[-3000:]
    if out_of_memory(out):
        return 0, "rejected: the build was killed, most likely out of memory (8 GB) | " + " | ".join(tail)[-3000:]
    return 0, "rejected: " + " | ".join(tail)[-4000:]


def main() -> int:
    VERIFIER_DIR.mkdir(parents=True, exist_ok=True)
    os.chmod(VERIFIER_DIR, 0o755)
    for name in ("reward.txt", "reward.json"):
        (VERIFIER_DIR / name).unlink(missing_ok=True)

    task = json.loads((TESTS / "task.json").read_text())
    details: dict = {"task": task["challenge"], "started_at": time.time()}
    reward: int | None = None
    log = ""
    try:
        RUN_ROOT.mkdir(mode=0o711, parents=True, exist_ok=True)
        deadline = time.time() + int(task["grade_timeout_sec"])
        run = fresh_run(task)
        details["permission_fixes"] = ensure_readable()
        block_grader_network()
        details["submission"] = unpack_submission(run)
        code, out = run_comparator(run, int(deadline - time.time()))
        log = out
        kill_grader()
        if code not in (0, None) and out_of_memory(out) and deadline - time.time() > 60:
            # Two Lean processes in parallel can exceed 8 GB on a heavy module. Retry once with one,
            # in a fresh directory: the first attempt's .lake was written by the submission's own
            # build and must not be trusted for the challenge.
            shutil.rmtree(run, ignore_errors=True)
            run = fresh_run(task)
            unpack_submission(run)
            code, out = run_comparator(run, int(deadline - time.time()), threads="1")
            log += "\n\n=== retried with one Lean process after running out of memory ===\n\n" + out
            kill_grader()
            details["retried_single_thread"] = True
        reward, details["reason"] = classify(code, out)
        details["comparator_exit"] = code
    except Rejected as exc:
        reward, details["reason"] = 0, f"rejected: {exc}"
    except GradingError as exc:
        reward, details["reason"] = None, f"grading error: {exc}"
    except Exception as exc:
        reward, details["reason"] = None, f"grading error: {type(exc).__name__}: {exc}"
    finally:
        try:
            kill_grader()
        except GradingError as exc:
            reward, details["reason"] = None, f"grading error: {exc}"
        # Free the build before writing anything. A build that filled the disk would otherwise
        # make these writes fail, and with no space left the sandbox cannot report that the
        # verifier exited, so Harbor waits until its own timeout and records no verdict.
        shutil.rmtree(RUN_ROOT, ignore_errors=True)
    details["reward"] = reward
    details["finished_at"] = time.time()
    (VERIFIER_DIR / "comparator.log").write_text(log)
    (VERIFIER_DIR / "grading.json").write_text(json.dumps(details, indent=2))
    print(json.dumps(details, indent=2))
    if reward is not None:
        (VERIFIER_DIR / "reward.txt").write_text(f"{reward}\n")
    return 0


if __name__ == "__main__":
    sys.exit(main())