Skip to content

fix(terminal_bench_2_1): grade failing pytest runs 0 when test.sh exits before writing reward.txt - #6174

Merged
yaoyu-33 merged 3 commits into
mainfrom
ananthsub/tb21-grade-missing-reward
Nov 14, 2026
Merged

yaoyu-33 merged 3 commits into
mainfrom
ananthsub/tb21-grade-missing-reward

Conversation

@ananthsub

@ananthsub ananthsub commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Terminal Bench 2.1 now records a completed evaluation with reward 0 when tests/test.sh exits non-zero before writing /logs/verifier/reward.txt and its output ends in a pytest summary that counts failed tests or errors.

  • pytest_reported_failure() reads the last pytest summary line and reports whether it counts failures or errors. It accepts:

    • the bordered summary (==== 2 failed, 1 passed in 0.10s ====);
    • the borderless summary that pytest -q prints (1 failed, 2 passed in 0.10s);
    • colored output (terminal color codes are stripped);
    • output with CRLF line endings.

    A borderless line must start with pytest's counts, so tool output such as Prepared 2 packages in 1.20s is not read as a summary.

  • In that case the verify response has reward=0, evaluation_completed=true, mask_sample=false, and a new reward_from_pytest_summary=true field that marks the grade as coming from the summary instead of reward.txt.

  • A new config key, score_pytest_failures_without_reward (default true), turns this off.

Unchanged:

  • An exit without a failing pytest summary (a failed uv or curl download, a setup crash) stays ungraded, as before.
  • A timed-out test run stays ungraded. It did not exit, so its partial output says nothing about the test run.
  • A verifier infrastructure failure, such as a failed download of a reward.txt that exists, stays masked by the outcome policy. The new grading applies only when no infrastructure failure was recorded.
  • The reward stays 0 on every path. This change cannot raise a reward.

Why

Many task test.sh scripts run pytest under set -euo pipefail and write reward.txt only on the following lines. A failing pytest ends the script first, so the rollout has no reward.txt. The server returned reward 0 with evaluation_completed=false, which labels the model's own test failure as an evaluation that did not finish.

A failing pytest is a verdict on the model, so recovery and observability should record it as a completed 0, not as an incomplete or unmeasured evaluation. The set -euo pipefail pattern is common in large generated Terminal Bench-style task sets: in one such set, about a fifth of the task scripts use it. None of the 91 pinned public Terminal Bench 2.1 tasks (harbor-framework/terminal-bench-2-1 at 7131e437) use it: each test.sh checks pytest's exit code and writes reward.txt itself. Public Terminal Bench 2.1 runs therefore never reach the new path.

No issue is linked: this ports a fix from a downstream training fork, where rows with evaluation_completed=false were masked, so failed rollouts on these tasks were dropped from training and only successes trained. That masking is not in Gym or NeMo RL main.

Benchmark-result impact

No change to rewards, reward profiles, or the training signal on main. These rows already had mask_sample=false and reward 0, and both reward profiling and NeMo RL masking read mask_sample.

What changes is how these rows are classified:

  • Verifier observability reports the run as completed instead of incomplete.
  • Recovery treats the row as graded instead of masked, so it is no longer filed as retained but unmeasured.
  • The harness conformance checker no longer counts the row as failed.

Public Terminal Bench 2.1 results do not change at all, because none of its pinned tasks use set -e (see Why).

Set score_pytest_failures_without_reward: false to keep the previous classification.

Validation

Tests cover:

  • A set -euo pipefail script whose pytest step fails, run through verify with a real bash subprocess:

    • graded 0 and completed with the switch on;
    • ungraded with the switch off;
    • ungraded when the script fails with no pytest summary;
    • ungraded when the summary reports failures but the step exits 0 and the script still ends without reward.txt.

    None of these cases is masked.

  • A timed-out test run whose partial output holds a failing summary: still ungraded and not masked.

  • The pytest summary parser on:

    • failed, error, passed and "no tests ran" summaries;
    • bordered, -q, colored and CRLF forms;
    • output with several summaries (the last one wins);
    • non-pytest output.
  • A failed download of a reward.txt that exists, after a failing pytest: still masked as a verifier error, and not graded from the summary.

Each new test fails when the behavior it covers is removed:

  • the grading and parser tests fail on main;
  • the zero-exit case fails when the exit-code guard is removed;
  • the timed-out case fails when the timeout guard is removed;
  • the masked-download test fails when the guard against a recorded infrastructure failure is removed.

The parser was also checked against real output from pytest, pytest -q, pytest --color=yes and pytest -rA on a failing test file.

Commands:

PYTHONPATH=$PWD .venv/bin/python -m pytest resources_servers/terminal_bench_2_1/tests/test_app.py -q
pre-commit run --files resources_servers/terminal_bench_2_1/app.py resources_servers/terminal_bench_2_1/tests/test_app.py

Rollout evidence

Run on the local workstation. The Gym servers ran there, sandboxes ran in OpenSandbox, and the policy model was served by the NVIDIA inference API. Agent: Terminus 2 through benchmarks/terminal_bench_2_1/terminus_2.yaml, so verification ran through this resources server. Settings: max_turns: 40, evaluation_timeout: 900, one repeat per task.

The public tasks do not use set -e, so the smoke tasks are copies of public tasks with set -euo pipefail inserted before the pytest step of test.sh. That reproduces the pattern in the generated task sets.

Model Task test.sh Pytest summary Result
nvidia/openai/eccn-gpt-oss-20b (Chat Completions) prove-plus-comm set -e copy 2 failed, 2 passed reward 0, completed, from summary, not masked
nvidia/openai/eccn-gpt-oss-20b fix-git set -e copy 2 failed reward 0, completed, from summary, not masked
nvidia/openai/eccn-gpt-oss-20b feal-linear-cryptanalysis set -e copy 1 failed reward 0, completed, from summary, not masked
nvidia/openai/eccn-gpt-oss-20b gpt2-codegolf set -e copy 1 failed reward 0, completed, from summary, not masked
openai/openai/eccn-gpt-6-luna (Responses API) prove-plus-comm set -e copy 4 passed reward 1 from reward.txt, not from summary
openai/openai/eccn-gpt-6-luna fix-git set -e copy 2 passed reward 1 from reward.txt, not from summary
openai/openai/eccn-gpt-6-luna prove-plus-comm public 4 passed reward 1 from reward.txt, not from summary
openai/openai/eccn-gpt-6-luna fix-git public 2 passed reward 1 from reward.txt, not from summary

Before this change, each failing set -e row would have come back with evaluation_completed=false. Here each has reward 0, evaluation_completed=true, reward_from_pytest_summary=true, mask_sample=false and no failure kind. Passing rollouts still write reward.txt and score 1, with or without set -e.

For each rollout I checked the agent output, the verifier test_output (the pytest summary above), the grading fields, the collector's coverage report (all completed and measured, none masked or failed), and that no sandbox was left behind after the run.

…ts before writing reward.txt

Many task test.sh scripts run pytest under `set -euo pipefail` and write
reward.txt only on the following lines. A failing pytest ends the script
first, so a failed rollout produced no reward.txt and was returned with
evaluation_completed false, which training masks as an incomplete
evaluation, while a passing one scored 1: those tasks trained on
successes only.

When test.sh exits non-zero without a readable reward.txt and its output
ends in a pytest summary that counts failures or errors, the rollout is
now graded 0 with evaluation_completed true and
reward_from_pytest_summary true. Exits without such a summary (a failed
download, a setup crash) stay ungraded, and a verifier infrastructure
failure is still masked by the outcome policy. The
score_pytest_failures_without_reward config key (default true) turns the
grading off.

Co-authored-by: Jiaqi Zeng <[email protected]>
Signed-off-by: Ananth Subramaniam <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 13, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

…ve timed-out runs ungraded

The summary parser now accepts the borderless summary that pytest -q
prints, and strips terminal color codes and carriage returns before
matching. Bare lines must start with pytest's counts, so tool output such
as "Prepared 2 packages in 1.20s" is not read as a summary.

A test run that timed out did not exit, so its partial output says
nothing about the test run: it stays ungraded even when a failing pytest
summary was printed before the timeout.

Tests now cover a failing summary with a zero exit, a timed-out run, and
the -q, color and CRLF summary forms.

Signed-off-by: Ananth Subramaniam <[email protected]>

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHIP WITH CARE

RISK — Add the required before/after reward-profile evidence

This changes verification logic for a fixed evaluation benchmark. The pinned review rubric requires a measured before/after reward-profile comparison for that change. The description supplies representative real-model smoke results, but no baseline profiling result or measured comparison. The trusted contribution guidance also distinguishes this requirement from training-environment smoke validation.

Could you record profiles on base and head using matching representative inputs and run settings? Include the missing-reward pytest failure and ordinary reward-file success/failure cases, and report sample count, mean reward, zero/one counts, masked count, and incomplete count. This is a validation gap, not a demonstrated reward regression: the new fallback branch changes completion and provenance without assigning a new reward.

Acceptance: the comparison should show unchanged rewards and masking. The affected missing-reward failure should remain reward 0 and unmasked, changing only from incomplete to completed with summary provenance. Valid reward files should retain their 0/1 grades without summary provenance; timeouts should remain incomplete, and infrastructure failures should retain their configured outcome policy. No broader implementation change is requested.

I inspected both changed files and traced the affected sandbox, response, observability, recovery, and profiling contracts. I found no additional qualifying code defect. This was a read-only review; I didn't execute tests or independently reproduce the reported rollouts.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@yaoyu-33 yaoyu-33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Team review at 2fec54b1ef. Verdict: SHIP WITH CARE. The grading logic is sound. The asks are inline: Agentic IF's evidence and impact, corrections to the PR body, and two test and comment fixes. One more inline item predates this PR and suits a follow-up issue.

What we checked about the new branch (app.py#L746-L757):

  • It cannot raise a reward. reward is still 0.0 whenever the branch runs, and it requires failure_cause is None, so mask_sample is the same as on main for every input.
  • Infrastructure failures stay where they were:
    • A sandbox or exec error, or a typed provider error, sets failure_cause before the branch is reached and stays masked.
    • A timeout stays ungraded.
    • A failed download of a reward.txt that exists stays masked.
    • A failed uv or curl step under set -e exits before pytest prints a summary, so it stays ungraded.
    • Each of these guards has a test that fails when the guard is removed. We checked this by removing each guard and re-running the tests on Linux.
  • Pytest collection and fixture errors ("1 error") are graded 0. We think that is right. The public TB2.1 and TBLite test.sh already write 0 from pytest's exit code in the same case. It is also hard to tell such an error apart from a task the model broke.
  • The parser holds up on real output. We compared it with pytest 8.4/9 output in bordered, -q, --color=yes, -rA, xdist and collection-error form. Its misses all fall back to main's behaviour (ungraded): pytest 9 subtests lines such as 2 failed, 2 subtests passed, -qq, and pre-5.1 in 0.10 seconds. Text the model prints can only push a row back to ungraded.

Reward profile. We don't think a before/after gym eval profile is needed for TB2.1 or TBLite.

  • The benchmark guide accepts before/after numbers or proof that the distribution did not move, and the code gives that proof.
  • Reward and mask_sample don't change on this path, and select_measured splits only on mask_sample.
  • No pinned TB2.1 task (89) and no pinned TBLite task (100) reaches the branch; we checked each test.sh.

The one consumer whose distribution does move is Agentic IF, so the evidence ask is there, inline.

CI is green at this head, including the changed-server tests. The new tests also pass on Linux (88 passed, 13 skipped). Not run: real sandboxes or models, or Agentic IF with its private terminal tests.

and getattr(eval_result, "error_type", None) is None
and pytest_reported_failure(test_output)
):
evaluation_completed = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.py:756

This flips a row that Agentic IF reads.

  • Agentic IF uses this server as its tb21 task backend (agentic_if.yaml).
  • It masks any backend reply with evaluation_completed: false as verifier_error (outcome.py).
  • So for a set -e terminal task whose failing pytest ends test.sh, main masks the Agentic IF row, and this PR scores it as an unmasked 0.

That direction looks right to us: a model failure was slipping out of the score. But it does change Agentic IF's measured set and mean reward, so "No change to rewards, reward profiles, or the training signal on main" doesn't hold there. Its public example rows are pinned TB2.1 tasks. The full set's terminal tests come from the private terminal-bench-tests.jsonl artifact (prepare.py), which is the kind of generated task set this PR targets.

Could you name Agentic IF under "Benchmark-result impact" and add one of these?

  1. A static count over terminal-bench-tests.jsonl 2.2.1: how many test.sh run pytest under set -e without a guard (no set +e, no || X=$?, no reward written first). Zero is proof enough.
  2. If the count isn't zero, base vs head numbers for those tasks through Agentic IF. A fresh run works, or reclassify an existing run's saved episodes. The numbers we'd like:
    • total rows;
    • rows masked as verifier_error;
    • rows with reward_from_pytest_summary=true;
    • mean reward over measured rows.

A TB2.1 reward profile isn't needed for this: reward and mask_sample don't change on this path.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Settled from our side.

We read the pinned agentic_if 2.2.1 terminal tests artifact and classified all 242 test.sh. None can reach this branch:

  • 162 guard pytest;
  • 75 don't use set -e;
  • 2 write reward 0 from an EXIT trap;
  • 3 don't run pytest at the top level.

So Agentic IF's pinned distribution is unchanged too. 72e4c902a2 records the Agentic IF interaction next to the switch (L113-L116).

One thing is left for you: "Benchmark-result impact" still says there is no change to the training signal on main. Could you add a line saying that Agentic IF turns these rows from masked into scored zeros on set -e task sets, and that none of its pinned 2.2.1 tasks reach the branch?

task_name: str
test_output: str
# True when the reward came from pytest's summary because test.sh exited before writing reward.txt.
reward_from_pytest_summary: bool = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.py:187

A few corrections to the "Why" and "Benchmark-result impact" sections, so readers can repeat the scope check:

  • Task count. The TB2.1 checkout has 89 tasks, not 91 (NUM_TASKS), and prepare.py clones upstream unpinned (L22-L25). We checked all 89 test.sh at 7131e437: none of them sets -e.
  • TBLite. OpenThoughts-TBLite is also graded by this server (manifest). At f075e463, 10 of its 100 test.sh use set -e. All 10 guard pytest or write reward.txt first, so none reaches this path. Worth saying in the body.
  • Profile columns. The profiler turns every top-level bool into a metric (reward_profile.py). So every TB2.1 and TBLite profile gains */reward_from_pytest_summary columns, and mean/evaluation_completed rises on set -e task sets. Reward and masking are unchanged.
  • Recovery. On main these rows are MASKED. That makes gym eval resubmit --recover stop with "the run is not complete" (recovery.py), and it counts them as trial failures (L5526-L5534). Naming this is the clearest statement of what the PR fixes on main.
  • Wording. "its output ends in a pytest summary": the parser takes the last summary anywhere in the output (L171). "the last pytest summary in the output" would match the code.


task_name: str
test_output: str
# True when the reward came from pytest's summary because test.sh exited before writing reward.txt.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.py:186

This comment, the config comment (L113-L115) and the PR body all say the summary is used when test.sh exits without writing reward.txt. The branch also fires when reward.txt exists but float() fails on it (L740), for example an empty or garbage file. With a non-zero exit and a failing summary, that row is now graded 0 instead of left ungraded.

Grading it looks right to us: the failing pytest is still the verdict. Could you say "no readable reward.txt" here, at L113-L115 and in the body? The test asks are on the test file.

Suggested change
# True when the reward came from pytest's summary because test.sh exited before writing reward.txt.
# True when the reward came from pytest's summary because test.sh exited without a readable reward.txt.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 72e4c902a2: L113-L116 and L187 now say "no readable reward.txt".

script.write_text(
"set -euo pipefail\n"
f"bash -c {quote(failing_step)}\n"
"_EXIT_CODE=$?\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_app.py:427

Under set -euo pipefail the _EXIT_CODE=$? line and the echo ... > reward.txt line never run. A non-zero step ends the script, and a zero step hits || exit 0. So assert not reward.txt.exists() passes in every case, and the one test that runs a real script has no passing control.

Could you add these cases?

  • A passing step that reaches the reward line, for example echo '=== 3 passed in 0.10s ==='; true followed by echo 1 > .../reward.txt. It should be graded 1 from reward.txt with reward_from_pytest_summary False.
  • A failing summary and a non-zero exit when reward.txt exists.
    • A valid 0 or 1 keeps the file's grade, and the flag stays False. This is the TBLite shape: write the reward, then exit $code.
    • Empty or invalid text is graded 0 from the summary (see the comment on app.py:186).

We ran versions of both at this head on Linux and they pass.

except Exception:
if self.config.debug:
print(f"Hit an exception downloading and converting reward: {format_exc()}", file=stderr)
# test.sh exited without a readable reward.txt, typically because a failing pytest ended it under

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.py:746

This one isn't introduced here, but the PR's task population is where it matters. Nothing clears /logs/verifier before test.sh: there is only the mkdir -p at L695. Verification also runs in the agent's own sandbox.

So on a set -e task whose pytest fails before the script writes reward.txt, a /logs/verifier/reward.txt holding 1 that the agent wrote is downloaded as the grade. We reproduced it through verify with a real bash test.sh: reward 1.0, completed, not masked. The Harbor Environment Server empties that directory before running test.sh (verifier.py).

A follow-up issue is fine for this, rather than this PR. Running rm -f /logs/verifier/reward.txt in the same exec as the mkdir would close it.

…ng-reward pytest failures

Agentic IF uses this server as its tb21 task backend and masks any result whose
evaluation is not completed. With score_pytest_failures_without_reward on, a set -e
task whose failing pytest ends test.sh is therefore scored 0 there instead of masked.
Say so next to the switch, and say "no readable reward.txt": the branch also grades a
reward.txt that exists but cannot be parsed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 14, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33

Copy link
Copy Markdown
Contributor

/ok to test 72e4c90

@yaoyu-33 yaoyu-33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 72e4c902a2.

The Agentic IF risk from the earlier review is settled.

  • Evidence. We read the pinned agentic_if 2.2.1 terminal-bench-tests.jsonl artifact (SHA-256 matches prepare.py) and classified all 242 terminal test.sh. None can reach the new branch:

    • 75 run pytest without set -e;
    • 162 guard pytest with if, || or set +e;
    • 2 install an EXIT trap that writes reward 0 when set -e aborts;
    • 3 run pytest only inside an if, or not at all.

    Every script the classifier flagged was then read by hand. So the Agentic IF distribution on its pinned data does not move either, just as for TB2.1 (89 tasks) and OpenThoughts-TBLite (100 tasks). No reward profile is needed.

  • What 72e4c902a2 changes. It touches comments only. The switch's comment (L113-L116) now says that Agentic IF masks a tb21 result that is not completed, so on a set -e task set such a row becomes a scored 0 there. It and the field comment (L187) now also say "no readable reward.txt", since the branch also grades a reward.txt that exists but cannot be parsed.

  • Validation. Pre-commit passes on the changed file, and CI is restarted at the new head. Earlier, at 2fec54b1ef: CI was green, and on Linux the server tests passed (88 passed, 13 skipped) and each guard's test failed once its guard was removed.

The remaining inline notes don't block merging:

  • the PR-body corrections;
  • the test that has no passing control;
  • the pre-existing /logs/verifier cleanup, which suits a follow-up.

Not run: real-model rollouts at the new head (the change is comments only), and a live Agentic IF run.

@yaoyu-33 yaoyu-33 added the ready-to-merge PR is approved, current, and waiting for final CI or merge label Nov 14, 2026
@yaoyu-33
yaoyu-33 added this pull request to the merge queue Nov 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/43569628145

Merged via the queue into main with commit 8f0ad9d Nov 14, 2026
61 checks passed
@yaoyu-33
yaoyu-33 deleted the ananthsub/tb21-grade-missing-reward branch November 14, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge PR is approved, current, and waiting for final CI or merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants