Agentic coding RL environments: tasks and unit-test graders that hold up

For agent builders4 min read

A software engineer in Abuja reviewing a failing test run on a laptop at a standing desk, terminal text on screen, late morning light.

Agentic coding RL environments give an agent a code repository and a task, let it edit files and run commands, and then grade the result by running tests. Unit tests are the usual grader because they are fast and objective. This post covers the parts, a grader function you can run, and how to stop the agent from gaming the tests.

The parts of agentic coding RL environments

SWE-bench is the best-known example. Its docs describe it as a benchmark of real software issues from GitHub: a model gets a codebase and an issue and must produce a patch that fixes it. It runs evaluation in a Docker-based runner, which the docs present as the basis for consistent, reproducible results. SWE-bench Verified has 500 problems that engineers confirmed are solvable.

Source: SWE-bench documentation(opens in a new tab)

PartIn a coding environment
TaskAn issue or feature request in plain language
StateA repository at a known commit, in a sandbox
ToolsRead, edit, search and run commands
GraderTests the agent has not seen
ResetA fresh copy of the repository for each attempt

Anthropic's agent eval guide agrees with the grader choice. It says deterministic graders work well for coding agents because the question is simple: does the code run, and do the tests pass? It adds that transcript-level grading can score things tests miss, such as code quality, using static analysis or a clear rubric.

Source: Demystifying evals for AI agents(opens in a new tab)

A grader that runs the tests

The function below takes a clean repository, applies the agent's edited files, copies in tests the agent never saw, and runs them with a timeout. In a real setup, the agent edits a working copy through tools. Here the edits arrive as a dictionary of paths and file contents to keep the example short.

Python
import shutilimport subprocessimport sysimport tempfilefrom pathlib import Path def grade(repo: Path, agent_files: dict, hidden_tests: Path,          timeout: int = 60) -> float:    with tempfile.TemporaryDirectory() as tmp:        work = Path(tmp) / "work"        shutil.copytree(repo, work)               # clean copy per attempt        for rel, text in agent_files.items():     # the agent's edits            target = work / rel            target.parent.mkdir(parents=True, exist_ok=True)            target.write_text(text)        shutil.copytree(hidden_tests, work / "hidden_tests")        try:            run = subprocess.run(                [sys.executable, "-m", "pytest", "-q", "hidden_tests"],                cwd=work, capture_output=True, text=True, timeout=timeout)        except subprocess.TimeoutExpired:            return 0.0        return 1.0 if run.returncode == 0 else 0.0

Three details matter. The hidden tests are copied in after the agent's edits, so an agent that rewrote or deleted a visible test file cannot affect the grade. The work happens in a temporary copy, so attempts cannot affect each other. And a timeout turns an infinite loop into a zero rather than a stuck job.

Try it on a tiny repository

Python
import tempfilefrom pathlib import Path root = Path(tempfile.mkdtemp())(root / "repo").mkdir()(root / "repo" / "mathutil.py").write_text(    "def add(a, b):\n    return a - b\n")          # the bug(root / "tests").mkdir()(root / "tests" / "test_add.py").write_text(    "from mathutil import add\n"    "def test_add():\n    assert add(2, 3) == 5\n"    "    assert add(-1, 1) == 0\n") print(grade(root / "repo", {}, root / "tests"))                # 0.0fixed = {"mathutil.py": "def add(a, b):\n    return a + b\n"}print(grade(root / "repo", fixed, root / "tests"))             # 1.0

Make the tests hold up

  • Write tests for behavior, not for the shape of one solution. If the tests demand a specific helper name, valid fixes fail.
  • Include tests that should keep passing, so a fix cannot break something else.
  • Include cases the issue does not spell out but the correct behavior implies.
  • Check that the task fails before the fix and passes after it, using a reference solution.
  • Pin dependencies and block network access, or a run may depend on the day it was run.

The reference solution step is the one teams skip. A task with no passing solution produces zeros that look like model failures. The eval guide says to provide reference solutions to prove tasks are solvable, and the engineer review that SWE-bench Verified describes serves the same purpose.

Watch the agent, too

Tests grade the result. A short script over the run log can flag edits to test files, network calls, and commands that delete the tests directory. Read a sample of passing runs by eye as well. The post on reward hacking explains why.

Curatrain's experts build realistic tasks, tools and checkers for AI agents, including coding environments, with the reference solutions and tests that go with them.