Harbor shared grading keeps files left in /tests, so a planted conftest.py makes a wrong answer pass
What happens
In shared Harbor grading, the task's tests are extracted on top of whatever is already in /tests in the agent's box. For tasks whose test.sh runs pytest on files under /tests, a /tests/conftest.py planted by the agent is imported, and it can make a wrong answer score 1.0.
HarborTask.solved stages the tests with self.stage_tests(runtime). With the default wipe=False, the staging command is mkdir -p /tests && tar -xzf /tmp/tests.tgz -C /tests. tar overwrites the archive's own files but leaves every other file in place. rm -rf /tests runs only when the task has a separate verifier (L391). Tasks without one grade in shared mode (L839).
When agent_user is unset, the agent runs as the image's default user. If that user can write to /tests, or can create it (root, which is the default in many task images), it can leave a conftest.py there before it finishes. pytest loads a conftest.py that sits next to the test file, so a pytest_runtest_makereport hook can mark every test as passed, and test.sh then writes reward 1.
Reproduce
The repro runs end to end on main at 16cc409. It uses the real Docker runtime, goes through run_episodes, and grades with the real HarborTask shared path. The agent is a scripted harness that runs a shell script in the container, so no model is called. The task is a minimal Harbor task: a python:3.11-slim image with pytest, a tests/test_outputs.py that asserts /app/answer.txt == "42", and a tests/test.sh that runs pytest and writes /logs/verifier/reward.txt from its exit code.
| What the agent ran | reward |
|---|---|
echo 42 > /app/answer.txt |
1.0 |
echo 41 > /app/answer.txt |
0.0 |
echo 41 > /app/answer.txt, then writes /tests/conftest.py (below) |
1.0 (pytest: 1 passed) |
| same, with the fix below | 0.0 (pytest: assert '41' == ... fails) |
The same planted file also scores 1.0 when the image's default user is a non-root appuser who owns /tests.
The planted file:
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
rep.outcome = "passed"
rep.longrepr = None
Relation to #3134 / #3140
Draft #3140 adds vf.untrusted and, when a distinct agent_user is configured, stops that user's processes before the reward is read. It does not empty /tests. A planted conftest.py needs no surviving process, because the grader's own pytest imports it, so this vector stays open.
Suggested fix
Empty /tests before unpacking the packaged tests. The unpack step runs as the box's default user, which need not be root, and /tests may be a mount point or something the image ships. So I'd do the clearing in stage_tests' existing setup step, which runs as root when the backend supports user switching:
- Empty an existing directory in place, keeping its owner, mode and any mount.
- Replace anything else (a symlink, a file, or nothing) with a directory owned like
/tmp/tests.tgz, so the staging user can still unpack into it.
if [ -d /tests ] && [ ! -L /tests ]; then find /tests -mindepth 1 -delete;
else rm -f /tests && mkdir /tests && chown "$(stat -c %u:%g /tmp/tests.tgz)" /tests; fi
Results with that change, in the same Docker setup:
- The planted-
conftest.pycase scores 0.0, both with a root agent and with the non-rootappuserimage. The honest cases are unchanged. - One side effect: with a non-root default user and no
/testsin the image,maincurrently fails staging (mkdir: cannot create directory '/tests': Permission denied). With the change, that case grades normally.
I have a PR with a regression test that runs the real staging commands against a /tests holding a planted conftest.py, a /tests that is a regular file, and a /tests symlink. I can open it if this direction works for you.
Limits
- A process the agent leaves running in the shared box could write a new
conftest.pyafter the cleanup. That's the surviving-process problem #3140 addresses when a distinctagent_useris configured. This change only removes what's already there when grading starts. - Without user switching, the cleanup runs as the default user and fails closed (staging error) if it can't remove something.
- Fresh isolated verifier boxes don't carry agent-planted
/testsstate, so they're not affected. - This doesn't address files planted elsewhere in a writable shared box, such as a
sitecustomize.pyin site-packages. - Upstream Harbor's single-step verifier (
harbor0.24.0,verifier/verifier.py) also uploads the tests into/testswithout clearing it first. I've only read that code, not run it.