Sandbox envs take their reward from a file the agent can write
Summary
terminus_env, jupyter_env, coding_tools_env, opencode_env and pi_env all document the same reward rule: passed_verify / total_verify, unless a verify command writes a float to <HOME>/logs/verifier/reward.txt, which then wins. The file lives in the agent's own sandbox, nothing clears it before the verify commands run, and its contents are parsed with a bare float(). So an agent that writes 1.0 there at any point before it submits gets a reward of 1.0, whatever the verify commands find. Values like nan, inf, -0.5 or 1.5 pass straight through as the reward too.
Every one of these envs gives the agent write access to that path:
| Env | How the agent can write the file |
|---|---|
terminus_env |
terminal(command=...) shell tool |
jupyter_env |
execute_shell_command, or any code cell |
coding_tools_env |
bash, or the write file tool directly |
opencode_env, pi_env |
the coding agent itself runs in the sandbox under $HOME before verify |
Where it lives (main @ 8f2d5e6a)
envs/terminus_env/server/terminus_env_environment.py:235,:261-269envs/jupyter_env/server/jupyter_environment.py:395,:444-452envs/coding_tools_env/server/coding_tools_env_environment.py:392,:407-417envs/opencode_env/server/opencode_environment.py:387,:450-457envs/pi_env/server/pi_environment.py:385,:448-455
tbench2_env already handles the same file safely: only the task's own tests/test.sh writes it, and a missing verdict raises instead of being scored (envs/tbench2_env/server/tbench2_env_environment.py:204-233).
Repro
With the existing FakeSandbox pattern from tests/envs/test_terminus_environment.py, extended so a shell command can write the reward file:
verify_commands = ["true", "exit 1"] # one of two checks passes -> 0.5
agent: terminal(command="echo 1.0 > /home/user/logs/verifier/reward.txt")
agent: terminal(final_answer="done")
state.last_reward # 1.0 on main
The same test shape gives 1.0 in all five envs.
A second verifier bug in jupyter_env
jupyter_env's E2BSandbox.run_shell runs the command through subprocess.run in the kernel and prints its output, but never looks at the return code, so success is only False when the Python cell itself raises (envs/jupyter_env/server/e2b_sandbox.py:91-104, :150). A verify command that exits non-zero is recorded as passed, which puts the pass-rate reward at 1.0 for every submission, and a failing setup command is not detected at reset. Driving the real wrapper with a local kernel: run_shell("exit 1").success is True, while terminus's wrapper, which raises SystemExit on a non-zero code (envs/terminus_env/server/e2b_sandbox.py:52-53), returns False. The existing jupyter test only sees 1/2 passed because its fake returns failure for exit 1.
Proposed fix
Per env, no new core API:
- Delete the reward file right before the verify commands run, and confirm it is gone. If it cannot be removed, ignore the override for that run.
- Accept the override only if it is a finite number in
[0, 1]. Otherwise fall back to the pass rate and record why in a newreward_override_ignoredfield on the env's state (RolloutResultfor opencode/pi). - Make
jupyter_env'srun_shellraiseSystemExiton a non-zero return code, as terminus already does, in a separate commit since it also changes whatexecute_shell_commandshows the agent.
Tasks whose verify commands write the file keep working unchanged, which is what the READMEs already describe. I have this on a branch with tests for each env that fail on main and pass with the fix.
Out of scope, noting for later
- A process the agent leaves running in the background could still write the file while verify runs. Closing that needs verify to run outside the agent's reach, or an option to turn the override off for training. Happy to follow up if you want one of those.
terminusletsfinal_answerbe called repeatedly, andcoding_toolsscores an empty verify list as0.0where the others returnNone. Both change reward values, so I left them alone.
Overlap with #729
#729 reworks terminus_env and touches _read_reward_override. I've kept terminus in the fix because it is the most direct case (a plain shell tool). The overlap is that one function, and I'll rebase whichever of the two lands second.
One more finding while testing the fix, specific to jupyter_env: the agent's code cells and the env's run_shell run in the same Python kernel. A code cell that replaces subprocess.run makes every later shell command report success, including the verify commands, so the agent can get the full pass-rate reward without touching the reward file at all. Checked by driving the real E2BSandbox.run_shell against a kernel shared with a patched cell.
The PR for this issue doesn't try to close that, since it needs verify to run outside the kernel. E2B's commands.run would do it, the way opencode_env's backend already runs commands (envs/opencode_env/sandbox/e2b.py:93).
I can send that as a follow-up .