hud-python icon indicating copy to clipboard operation
hud-python copied to clipboard

daytona-rl `fix_calc`: reward 1.0 while `calc.py` stays broken (test file is editable, pass count is read from stdout)

Open jimmy-holdout opened this issue 1 month ago • 0 comments

What happens

fix_calc in cookbooks/daytona-rl/env.py returns 1.0 when run_pytest() reports passed == TOTAL_TESTS (L60-L61). There are two ways to reach that without fixing any bug:

  1. Edit the tests. seed_bug writes test_calc.py into the agent's workspace, and nothing restores or checks it before grading. "Don't edit test_calc.py" appears only in the prompt. Replacing the file with ten empty tests scores 1.0. So does keeping the original failing tests and appending ten passing ones, because only the passed count is compared: 4 failed, 10 passed still scores 1.0.
  2. Print the number. run_pytest merges stdout and stderr, then takes the first match of (\d+) passed (L50). pytest prints a failing test's captured stdout before its summary line. So if calc.py contains print("10 passed"), the grader reads 10, while pytest itself reports 4 failed, 6 passed and exits with code 1. test_calc.py is never touched.

Both work on all 24 variants in bugs.py. That covers the 16 training variants (train.py#L41) and the 8 held-out ones.

Reproduce

Save the script as repro_fix_calc.py in the repo root and run it inside the repo's environment. It needs pytest, and it makes no network or model calls. It uses the real template lifecycle (TaskRunner.start → workspace edit → TaskRunner.grade).

repro_fix_calc.py
# Save as repro_fix_calc.py in the repo root, then: python repro_fix_calc.py
import asyncio
import sys

sys.path.insert(0, "cookbooks/daytona-rl")
import bugs
import env as smoke
from hud.environment.server import TaskRunner

WS = smoke.WORKSPACE


def noop():
    pass


def correct_fix():  # reference implementations from bugs.py
    body = "".join("\n" + bugs.FUNCS[n]["correct"].strip("\n") + "\n" for n in bugs.NAMES)
    (WS / "calc.py").write_text(bugs.HEADER + body)


def rewrite_tests():  # calc.py left buggy
    (WS / "test_calc.py").write_text("".join(f"def test_{i}():\n    pass\n" for i in range(10)))


def print_passed():  # calc.py left buggy, test_calc.py untouched
    lines = []
    for line in (WS / "calc.py").read_text().splitlines():
        lines.append(line)
        if line.startswith("def "):
            lines.append('    print("10 passed")')
    (WS / "calc.py").write_text("\n".join(lines) + "\n")


async def reward(variant, edit):
    task = smoke.fix_calc(variant=variant)
    runner = TaskRunner(smoke.env.templates[task.id], task.args)
    await runner.start()  # seeds calc.py / test_calc.py, returns the prompt
    edit()  # what the agent does in the workspace
    return (await runner.grade({"answer": "done"}))["score"]


async def main():
    for edit in (noop, correct_fix, rewrite_tests, print_passed):
        scores = [await reward(v, edit) for v in range(24)]
        print(f"{edit.__name__:14s} reward 1.0 on {scores.count(1.0):2d}/24 variants")

    # what pytest itself says in the print_passed case (variant 0)
    smoke.seed_bug(0)
    print_passed()
    proc = await asyncio.create_subprocess_exec(
        sys.executable, "-m", "pytest", "-q", "-p", "no:cacheprovider",
        cwd=str(WS), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT,
    )
    out = (await proc.communicate())[0].decode().strip().splitlines()
    print("pytest summary:", out[-1], "| exit code:", proc.returncode)
    print("run_pytest() ->", await smoke.run_pytest())


asyncio.run(main())

Output at e95373f on Ubuntu 24.04 LTS (Linux 6.8), Python 3.12.3, pytest 9.1.1:

noop           reward 1.0 on  0/24 variants
correct_fix    reward 1.0 on 24/24 variants
rewrite_tests  reward 1.0 on 24/24 variants
print_passed   reward 1.0 on 24/24 variants
pytest summary: 4 failed, 6 passed in 0.05s | exit code: 1
run_pytest() -> (10, 10)

Impact

In GRPO training, editing the test file or printing 10 passed earns the same reward as a real fix on every variant. A policy can therefore learn test tampering or log spoofing instead of debugging. The held-out pass rate from bench/baseline.py uses the same grader, so it can't tell the two apart either.

Possible fix

  • In the grading half of fix_calc, rewrite test_calc.py from bugs.build(variant) before running pytest, or run a copy kept outside the workspace. Also delete any conftest.py, pytest.ini, pyproject.toml, setup.cfg or tox.ini the agent may have added.
  • Stop scraping stdout. Run pytest with --junitxml=<path outside the workspace>, delete that file first, and pass only if it exists and lists all 10 expected tests as passed. The exit code alone isn't enough: I checked locally that import os; os._exit(0) at the top of calc.py makes pytest exit with code 0 without running any tests.

Happy to help test a fix. Found while auditing public RL environments.

jimmy-holdout avatar Oct 22 '26 07:10 jimmy-holdout