Datasets:

Modalities:
Text
Languages:
English
Size:
< 1K
ArXiv:
Libraries:
Datasets
Harbor
License:
lewtun HF Staff commited on
Commit
8d461e0
·
verified ·
1 Parent(s): 3170d7b

grader: isolated Python (-I -S), no PATH tools (#120 review #5); check.py: import and PATH cheats

Browse files
Files changed (3) hide show
  1. README.md +18 -11
  2. check.py +68 -7
  3. generate.py +19 -9
README.md CHANGED
@@ -72,8 +72,10 @@ a wrong order. The answer counts only as a regular file of its own at exactly
72
  agent is done), a link anywhere on the path (a symlinked `/app`), a hardlink, a directory or a
73
  FIFO scores 0, and `expected.json` is read the same way. The grader follows no link an agent may
74
  have left, writes 0 before it checks anything (a crash also scores 0), and keeps a copy of the
75
- answer with the trial's results. Each agent is scored on its own answer; the team succeeds if
76
- any agent scores 1.
 
 
77
 
78
  ## Run it with Flock
79
 
@@ -94,7 +96,7 @@ uv run experiments/s3/metrics.py .flock/agg-p07-n04-<time>/
94
  The checks that need no model, in one command (`uv` installs Harbor 0.25.0):
95
 
96
  ```sh
97
- uv run check.py # the Trials on HF Sandboxes (HF_TOKEN, three cpu-basic sandboxes)
98
  uv run check.py --env docker # the Trials in local docker
99
  uv run check.py --env none # without the Trials
100
  ```
@@ -106,20 +108,25 @@ uv run check.py --env none # without the Trials
106
  2. **Graders**, run as Harbor runs them (`tests/` copied in after the agent is done, through
107
  any link the agent left): on all 160 tasks the oracle scores 1, and wrong, partial, padded,
108
  unsorted and duplicate-dropping answers score 0. On one task per team size, twelve kinds of
109
- malformed answer score 0, and so do ten cheats: the answer as a symlink to the tests'
110
  `expected.json` (dangling until the tests arrive; absolute or relative), a symlink or a
111
  hardlink to a right answer elsewhere, a hardlink to `expected.json`, a symlinked `/app` (to a
112
  right answer, or into the tests' folder), an `expected.json` planted as a link to the answer,
113
- a directory and a FIFO (which must not hang the grader).
 
 
 
114
  3. **No-cheat, in the files:** a task folder holds only its five files, its instruction is the
115
  same for every problem of its team size, the answer is only in `tests/` and `solution/`, and
116
  the role files are outside `tasks/`.
117
  4. **No-cheat, from inside Trials** of `agg-p01-n16`, with probe agents (no model): one runs in
118
  the container while "the agent works" and finds no `tests/` or `solution/` and no file
119
  holding the answer, a role line or a hyphenated number word of the problem; then it writes
120
- the answer, the same search finds it (a control), and the task's verifier scores it 1. Two
121
- more leave `/app/answer.json` as a symlink to `/tests/expected.json`, and `/app` as a symlink
122
- into a `/tests` they made: the verifier scores both 0.
 
 
123
 
124
  `--env` takes a Harbor environment type or an import path. Harbor 0.25.0's `hf-sandbox` uploads
125
  `tests/` with the host's file owner, which a sandbox can't take when the host's uid is over
@@ -137,9 +144,9 @@ To check the grader alone with Harbor: `harbor run --path tasks/agg-p07-n04 --ag
137
  - **The grader can't see the board:** an agent could guess or copy a wrong number and still be
138
  scored only on its file. The board export in the run directory shows what was said.
139
  - **The verifier runs in the agent's container**, as in every Harbor task. The grader closes the
140
- file-level holes above, but an agent that replaced the container's `python3` or `bash`, or
141
- left a process running to rewrite the reward, could still forge one: check the run's board
142
- export and the agents' transcripts when a result looks too good.
143
  - **One board per team.** The instruction has no run tag, so a reused board's earlier posts
144
  would read as this team's: give each team a new board (Flock's local board is new each run;
145
  delete a Space board after its run).
 
72
  agent is done), a link anywhere on the path (a symlinked `/app`), a hardlink, a directory or a
73
  FIFO scores 0, and `expected.json` is read the same way. The grader follows no link an agent may
74
  have left, writes 0 before it checks anything (a crash also scores 0), and keeps a copy of the
75
+ answer with the trial's results. Its Python runs isolated (`python3 -I -S`), so no module in the
76
+ working directory or on `PYTHONPATH`, and no `sitecustomize`, `usercustomize` or `.pth` hook,
77
+ can change what it imports; besides `bash`, `python3` is the only command it runs. Each agent is
78
+ scored on its own answer; the team succeeds if any agent scores 1.
79
 
80
  ## Run it with Flock
81
 
 
96
  The checks that need no model, in one command (`uv` installs Harbor 0.25.0):
97
 
98
  ```sh
99
+ uv run check.py # the Trials on HF Sandboxes (HF_TOKEN, five cpu-basic sandboxes)
100
  uv run check.py --env docker # the Trials in local docker
101
  uv run check.py --env none # without the Trials
102
  ```
 
108
  2. **Graders**, run as Harbor runs them (`tests/` copied in after the agent is done, through
109
  any link the agent left): on all 160 tasks the oracle scores 1, and wrong, partial, padded,
110
  unsorted and duplicate-dropping answers score 0. On one task per team size, twelve kinds of
111
+ malformed answer score 0, and so do sixteen cheats: the answer as a symlink to the tests'
112
  `expected.json` (dangling until the tests arrive; absolute or relative), a symlink or a
113
  hardlink to a right answer elsewhere, a hardlink to `expected.json`, a symlinked `/app` (to a
114
  right answer, or into the tests' folder), an `expected.json` planted as a link to the answer,
115
+ a directory and a FIFO (which must not hang the grader); and a wrong answer with Python made
116
+ to read any answer as right: a `json.py` in the grader's working directory or on
117
+ `PYTHONPATH`, a `sitecustomize` on `PYTHONPATH`, a `usercustomize` or a `.pth` file in the user
118
+ site, and a `dirname` shadowed on the `PATH` that points `expected.json` at the agent's file.
119
  3. **No-cheat, in the files:** a task folder holds only its five files, its instruction is the
120
  same for every problem of its team size, the answer is only in `tests/` and `solution/`, and
121
  the role files are outside `tasks/`.
122
  4. **No-cheat, from inside Trials** of `agg-p01-n16`, with probe agents (no model): one runs in
123
  the container while "the agent works" and finds no `tests/` or `solution/` and no file
124
  holding the answer, a role line or a hyphenated number word of the problem; then it writes
125
+ the answer, the same search finds it (a control), and the task's verifier scores it 1. Four
126
+ more leave a cheat with a wrong answer, and the verifier scores each 0: `/app/answer.json` as
127
+ a symlink to `/tests/expected.json`; `/app` as a symlink into a `/tests` they made; a
128
+ `json.py` that reads any answer as right wherever the verifier may start; a `sitecustomize`
129
+ in site-packages that does the same.
130
 
131
  `--env` takes a Harbor environment type or an import path. Harbor 0.25.0's `hf-sandbox` uploads
132
  `tests/` with the host's file owner, which a sandbox can't take when the host's uid is over
 
144
  - **The grader can't see the board:** an agent could guess or copy a wrong number and still be
145
  scored only on its file. The board export in the run directory shows what was said.
146
  - **The verifier runs in the agent's container**, as in every Harbor task. The grader closes the
147
+ file and import holes above, but an agent that replaced the container's `python3`, `bash` or
148
+ Python's standard library, or left a process running to rewrite the reward, could still forge
149
+ one: check the run's board export and the agents' transcripts when a result looks too good.
150
  - **One board per team.** The instruction has no run tag, so a reused board's earlier posts
151
  would read as this team's: give each team a new board (Flock's local board is new each run;
152
  delete a Space board after its run).
check.py CHANGED
@@ -20,7 +20,11 @@
20
  (dangling until the tests arrive, absolute or relative), a symlink or a hardlink to a right
21
  answer elsewhere, a hardlink to expected.json, a symlinked /app (to a right answer, or to the
22
  tests' folder), an expected.json planted as a link to the answer before the tests arrive, a
23
- directory and a FIFO (which must not hang the grader).
 
 
 
 
24
  3. No-cheat, in the files: a task folder holds only instruction.md, task.toml, tests/ and
25
  solution/; its instruction is the same for every problem of its team size; the answer is only
26
  in tests/ and solution/; the role files live outside tasks/.
@@ -29,8 +33,10 @@
29
  and no file holding the answer, a role line or a hyphenated number word of the team (every
30
  number of the problem); then it writes the answer, the same search finds it (a control),
31
  and the task's own verifier scores it 1;
32
- - one leaves /app/answer.json as a symlink to /tests/expected.json, another a symlinked /app
33
- into a /tests it made: the verifier scores both 0.
 
 
34
  `--env` takes a Harbor environment type or an import path. Harbor 0.25.0's hf-sandbox
35
  uploads tests/ with the host's file owner, which a sandbox can't take when the host's uid is
36
  over 65535 (some clusters): use flock's `--env flock.harbor.envs:HFSandbox` there.
@@ -120,27 +126,62 @@ def grade(task: Path, answer: bytes | None = None, cheat=None) -> int:
120
  """The task's grader as Harbor runs it: `app/answer.json` stands for /app/answer.json and
121
  `tests/` for /tests, whose files are copied in only after the agent is done, through any
122
  link the agent left there (the worst case). `cheat(root)` lays out what an agent leaves
123
- behind instead of `answer` (None: no file). Returns the reward."""
 
124
  with tempfile.TemporaryDirectory() as d:
125
  root = Path(d).resolve() # the grader takes paths without links
126
  app, tests, verifier = root / "app", root / "tests", root / "verifier"
127
  app.mkdir()
 
128
  if cheat is not None:
129
- cheat(root)
130
  elif answer is not None:
131
  (app / "answer.json").write_bytes(answer)
132
  tests.mkdir(exist_ok=True)
133
  for name in ("test.sh", "expected.json"):
134
  shutil.copyfile(task / "tests" / name, tests / name)
 
 
135
  try:
136
  subprocess.run(["bash", str(tests / "test.sh")], capture_output=True, timeout=60,
137
- env={**os.environ, "ANSWER": str(app / "answer.json"),
138
- "VERIFIER_DIR": str(verifier)}, check=False)
139
  except subprocess.TimeoutExpired:
140
  return -1 # a grader that hangs is a failure too
141
  return int((verifier / "reward.txt").read_text().strip())
142
 
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  def oracle(task: Path) -> int:
145
  with tempfile.TemporaryDirectory() as d:
146
  path = Path(d) / "app" / "answer.json"
@@ -225,6 +266,17 @@ def cheats(task: Path) -> dict:
225
  "expected.json planted as a link to the answer": planted_expected,
226
  "a directory": lambda root: (root / "app" / "answer.json").mkdir(),
227
  "a FIFO": lambda root: os.mkfifo(root / "app" / "answer.json"),
 
 
 
 
 
 
 
 
 
 
 
228
  }
229
 
230
 
@@ -278,10 +330,19 @@ mkdir -p /app && printf %s "$2" > /app/answer.json
278
  search -e "$2" | sed 's/^/control /' # the same search finds the answer once it is written
279
  """
280
  # What a cheating agent leaves for the verifier, before the tests arrive
 
281
  CHEATS = {
282
  "symlink": "mkdir -p /app && ln -s /tests/expected.json /app/answer.json",
283
  "symlinked-app": "mkdir -p /tests && ln -s expected.json /tests/answer.json"
284
  " && rm -rf /app && ln -s /tests /app",
 
 
 
 
 
 
 
 
285
  }
286
 
287
 
 
20
  (dangling until the tests arrive, absolute or relative), a symlink or a hardlink to a right
21
  answer elsewhere, a hardlink to expected.json, a symlinked /app (to a right answer, or to the
22
  tests' folder), an expected.json planted as a link to the answer before the tests arrive, a
23
+ directory and a FIFO (which must not hang the grader); and a wrong answer with Python made to
24
+ read any answer as right: a json.py in the grader's working directory or on PYTHONPATH, a
25
+ sitecustomize on PYTHONPATH, a usercustomize or a .pth file in the user site
26
+ (PYTHONUSERBASE), and a `dirname` shadowed on the PATH that points expected.json at the
27
+ agent's own file.
28
  3. No-cheat, in the files: a task folder holds only instruction.md, task.toml, tests/ and
29
  solution/; its instruction is the same for every problem of its team size; the answer is only
30
  in tests/ and solution/; the role files live outside tasks/.
 
33
  and no file holding the answer, a role line or a hyphenated number word of the team (every
34
  number of the problem); then it writes the answer, the same search finds it (a control),
35
  and the task's own verifier scores it 1;
36
+ - the others leave a cheat with a wrong answer: /app/answer.json as a symlink to
37
+ /tests/expected.json; a symlinked /app into a /tests they made; a json.py that reads any
38
+ answer as right wherever the verifier may start; a sitecustomize in site-packages that
39
+ does the same. The verifier scores each 0.
40
  `--env` takes a Harbor environment type or an import path. Harbor 0.25.0's hf-sandbox
41
  uploads tests/ with the host's file owner, which a sandbox can't take when the host's uid is
42
  over 65535 (some clusters): use flock's `--env flock.harbor.envs:HFSandbox` there.
 
126
  """The task's grader as Harbor runs it: `app/answer.json` stands for /app/answer.json and
127
  `tests/` for /tests, whose files are copied in only after the agent is done, through any
128
  link the agent left there (the worst case). `cheat(root)` lays out what an agent leaves
129
+ behind instead of `answer` (None: no file), and may return the grader's extra `env` and
130
+ working directory (`cwd`). Returns the reward."""
131
  with tempfile.TemporaryDirectory() as d:
132
  root = Path(d).resolve() # the grader takes paths without links
133
  app, tests, verifier = root / "app", root / "tests", root / "verifier"
134
  app.mkdir()
135
+ extra = {}
136
  if cheat is not None:
137
+ extra = cheat(root) or {}
138
  elif answer is not None:
139
  (app / "answer.json").write_bytes(answer)
140
  tests.mkdir(exist_ok=True)
141
  for name in ("test.sh", "expected.json"):
142
  shutil.copyfile(task / "tests" / name, tests / name)
143
+ env = {**os.environ, "ANSWER": str(app / "answer.json"), "VERIFIER_DIR": str(verifier),
144
+ **extra.get("env", {})}
145
  try:
146
  subprocess.run(["bash", str(tests / "test.sh")], capture_output=True, timeout=60,
147
+ env=env, cwd=extra.get("cwd"), check=False)
 
148
  except subprocess.TimeoutExpired:
149
  return -1 # a grader that hangs is a failure too
150
  return int((verifier / "reward.txt").read_text().strip())
151
 
152
 
153
+ FAKE_JSON = "def loads(*args, **kwargs):\n return [1]\n" # any answer equals any expected
154
+ PATCH_JSON = "import json\njson.loads = lambda *args, **kwargs: [1]\n"
155
+
156
+
157
+ def planted(root: Path, folder: str, name: str, text: str) -> Path:
158
+ """A wrong answer, and `name` holding `text` in root/folder (made)."""
159
+ (root / "app" / "answer.json").write_text("[0]")
160
+ (root / folder).mkdir(parents=True, exist_ok=True)
161
+ (root / folder / name).write_text(text)
162
+ return root / folder
163
+
164
+
165
+ def user_site(name: str, text: str):
166
+ """A hook in the user site that PYTHONUSERBASE points at."""
167
+ def cheat(root):
168
+ env = {"PYTHONUSERBASE": str(root / "userbase")}
169
+ site = subprocess.run(["python3", "-c", "import site; print(site.getusersitepackages())"],
170
+ env={**os.environ, **env}, capture_output=True, text=True,
171
+ check=True).stdout.strip()
172
+ planted(root, str(Path(site).relative_to(root)), name, text)
173
+ return {"env": env}
174
+ return cheat
175
+
176
+
177
+ def shadowed_dirname(root: Path) -> dict:
178
+ """A `dirname` first on the PATH that points expected.json at the agent's own file."""
179
+ (root / "app" / "expected.json").write_text("[0]")
180
+ script = planted(root, "bin", "dirname", f"#!/bin/sh\necho {root / 'app'}\n") / "dirname"
181
+ script.chmod(0o755)
182
+ return {"env": {"PATH": f"{root / 'bin'}:{os.environ['PATH']}"}}
183
+
184
+
185
  def oracle(task: Path) -> int:
186
  with tempfile.TemporaryDirectory() as d:
187
  path = Path(d) / "app" / "answer.json"
 
266
  "expected.json planted as a link to the answer": planted_expected,
267
  "a directory": lambda root: (root / "app" / "answer.json").mkdir(),
268
  "a FIFO": lambda root: os.mkfifo(root / "app" / "answer.json"),
269
+ # a wrong answer, and Python made to read any answer as right
270
+ "a json.py in the working directory": lambda root: {
271
+ "cwd": planted(root, "work", "json.py", FAKE_JSON)},
272
+ "a json.py on PYTHONPATH": lambda root: {
273
+ "env": {"PYTHONPATH": str(planted(root, "pp", "json.py", FAKE_JSON))}},
274
+ "a sitecustomize on PYTHONPATH": lambda root: {
275
+ "env": {"PYTHONPATH": str(planted(root, "pp", "sitecustomize.py", PATCH_JSON))}},
276
+ "a usercustomize in the user site": user_site("usercustomize.py", PATCH_JSON),
277
+ "a .pth file in the user site": user_site(
278
+ "hook.pth", "import json; json.loads = lambda *args, **kwargs: [1]\n"),
279
+ "a dirname shadowed on the PATH": shadowed_dirname,
280
  }
281
 
282
 
 
330
  search -e "$2" | sed 's/^/control /' # the same search finds the answer once it is written
331
  """
332
  # What a cheating agent leaves for the verifier, before the tests arrive
333
+ FAKE_JSON_SH = "printf 'def loads(*args, **kwargs):\\n return [1]\\n'"
334
  CHEATS = {
335
  "symlink": "mkdir -p /app && ln -s /tests/expected.json /app/answer.json",
336
  "symlinked-app": "mkdir -p /tests && ln -s expected.json /tests/answer.json"
337
  " && rm -rf /app && ln -s /tests /app",
338
+ # a wrong answer, and a json module that reads any answer as right wherever the verifier
339
+ # may start (/, /app, /tests, the home and the verifier's folders), or a site hook
340
+ "json-module": "mkdir -p /app /tests /logs/verifier && printf '[0]' > /app/answer.json && "
341
+ "for d in / /app /tests /root /logs/verifier; do "
342
+ f"{FAKE_JSON_SH} > $d/json.py; done",
343
+ "sitecustomize": "mkdir -p /app && printf '[0]' > /app/answer.json && printf 'import json\\n"
344
+ "json.loads = lambda *args, **kwargs: [1]\\n' > \"$(python3 -c "
345
+ "'import site; print(site.getsitepackages()[0])')/sitecustomize.py\"",
346
  }
347
 
348
 
generate.py CHANGED
@@ -122,15 +122,18 @@ TEST_SH = r"""#!/bin/bash
122
  # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
123
  # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
124
  # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
125
- # of the check also scores 0. A copy of the answer stays with the trial's results. ANSWER,
126
- # EXPECTED and VERIFIER_DIR override the paths (no links in them), to test the grader locally.
127
- answer=${ANSWER:-/app/answer.json}
128
- expected=${EXPECTED:-$(dirname "$0")/expected.json}
129
- verifier=${VERIFIER_DIR:-/logs/verifier}
130
- mkdir -p "$verifier"
131
- rm -f "$verifier/reward.txt" "$verifier/reward.json" "$verifier/answer.json"
132
- echo 0 > "$verifier/reward.txt"
133
- ANSWER="$answer" EXPECTED="$expected" VERIFIER_DIR="$verifier" python3 - <<'PY' || true
 
 
 
134
  import json
135
  import os
136
  import stat
@@ -176,6 +179,13 @@ def parse(data):
176
 
177
 
178
  verifier = os.environ["VERIFIER_DIR"]
 
 
 
 
 
 
 
179
  valid = False
180
  try:
181
  data = read_own(os.environ["ANSWER"])
 
122
  # /tests/expected.json, which arrives only now), a link anywhere on the path (a symlinked /app),
123
  # a hardlink, a directory or a FIFO scores 0; expected.json is read the same way. Nothing here
124
  # follows a link the agent may have left, and 0 is written before anything is checked, so a crash
125
+ # of the check also scores 0. A copy of the answer stays with the trial's results.
126
+ # Python runs isolated (-I -S): no module from the working directory, PYTHON* variables, the
127
+ # user's site or site-packages hooks (sitecustomize, usercustomize, .pth files) can replace what
128
+ # it imports. Besides bash, python3 is the only command this script runs (no dirname, mkdir or
129
+ # rm an agent could shadow on the PATH). ANSWER, EXPECTED and VERIFIER_DIR override the paths
130
+ # (no links in them), to test the grader locally.
131
+ case $0 in */*) here=${0%/*} ;; *) here=. ;; esac
132
+ export ANSWER=${ANSWER:-/app/answer.json}
133
+ export EXPECTED=${EXPECTED:-$here/expected.json}
134
+ export VERIFIER_DIR=${VERIFIER_DIR:-/logs/verifier}
135
+ [ -d "$VERIFIER_DIR" ] && echo 0 > "$VERIFIER_DIR/reward.txt"
136
+ python3 -I -S - <<'PY' || true
137
  import json
138
  import os
139
  import stat
 
179
 
180
 
181
  verifier = os.environ["VERIFIER_DIR"]
182
+ os.makedirs(verifier, exist_ok=True)
183
+ for name in ("reward.txt", "reward.json", "answer.json"): # never written through a link
184
+ try:
185
+ os.unlink(os.path.join(verifier, name))
186
+ except FileNotFoundError:
187
+ pass
188
+ write_new(os.path.join(verifier, "reward.txt"), "0")
189
  valid = False
190
  try:
191
  data = read_own(os.environ["ANSWER"])