Skip to content

fix(opencode_agent): report unknown usage when a step was stopped - #6960

Merged
ffrujeri merged 1 commit into
mainfrom
jseppanen/opencode-unfinished-step-usage
Dec 1, 2026
Merged

ffrujeri merged 1 commit into
mainfrom
jseppanen/opencode-unfinished-step-usage

Conversation

@jseppanen

@jseppanen jseppanen commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

rollout_token_count_mismatch flags OpenCode rollouts in which the agent timeout stopped a step while a tool was running, even though the only tokens missing from the response usage are those of the stopped steps.

OpenCode creates each assistant message with zero token counters and writes the step's usage and finish field at the step's step-finish event, which comes after the step's tools return. When the OpenCode agent's timeout stops a session while a tool is running—for example, a task call in the root session waiting on a subagent—the step's message keeps the zero placeholders. Gym captures the step's model call, but because the agent sums the response usage from the stored messages, the response usage omits that call's tokens.

With this PR, the OpenCode agent reports no response usage when any message's step never reached step-finish and adds a model_call_usage_unavailable gap whose detail contains the message ID. Such a message has no finish field and either has no time.completed (when the process was killed) or has a MessageAbortedError (when OpenCode cleaned up). The zero counters of a message from a failed model call still count as a measurement, as before. The health check is unchanged. The crewai agent already does the same when a run is interrupted during a model call. The OpenCode agent README describes the new behavior.

Validation

  • New test_a_step_stopped_before_step_finish_leaves_the_usage_unknown: a killed step or an aborted step makes the usage None; with a finished step, a failed model call, or an export without step fields, the usage is summed.
  • New test_session_http_reports_a_stopped_step_as_usage_unavailable: for a subagent step stopped while a tool was running, the response has no usage and the agent adds one model_call_usage_unavailable gap containing the message ID.
  • New test_a_rollout_without_response_usage_leaves_the_token_comparison_unobserved: a rollout with that gap, no response usage, and captured calls with token counts gets no rollout_token_count_mismatch finding, and the check is listed as unobserved. This pins the existing health-check behavior that the agent change relies on.
  • pytest responses_api_agents/opencode_agent/tests tests/unit_tests/rollouts: 4063 passed, 4 failed. The 4 failures are in tests/unit_tests/rollouts/test_tool_telemetry.py and also fail on unmodified main in my environment.
  • pre-commit run --files <changed files>: passed.

Rollouts

This analysis uses an existing OfficeQA Pro run with the OpenCode harness (665 rollouts, 7200 s agent timeout), in which 8 rollouts were flagged with rollout_token_count_mismatch. All 8 were stopped by the agent timeout during a tool call: cleanup reports a timeout with return code 143, and the trajectory has tool_completion_unavailable gaps. In each rollout, the captured calls hold more tokens than the response usage. The difference equals the usage of the last captured call in each session that was stopped mid-step:

Rollout Missing prompt / completion tokens Sessions with a stopped model call (tools invoked by that call)
0-0 121,390 / 285 root (task)
3-0 10,526 / 601 root (task, task, bash)
8-2 16,435 / 358 root (task)
8-3 66,348 / 428 root (task)
30-4 29,251 / 376 root (task, task)
62-2 56,108 / 3,893 root (task, task) and subagent (glob)
62-3 48,886 / 512 root (task)
93-2 191,422 / 2,028 root (task) and subagent (bash, bash, bash)

Those rollouts predate this change, so they have no model_call_usage_unavailable gap.

To test the change end to end, I ran OfficeQA Pro on 40 tasks × 1 with the OpenCode agent timeout lowered from 7200 s to 300 s, so that most sessions are stopped mid-step. Both runs used the same SFT checkpoint and differ only in this PR (both include #6622):

Gym commit Timed-out rollouts rollout_token_count_mismatch findings No response usage, with a model_call_usage_unavailable gap rollout_ended_on_failed_model_call
main + #6622 25 5 0 9
main + #6622 + this PR 24 0 (unobserved on all 24) 24 7

A rollout stopped during a model call also gets the gap, because its in-flight message has no finish either.

#6622 handles a related false positive separately: rollout_tool_call_failed on OpenCode searches with a path that does not exist.

Compatibility and benchmark impact

This change does not touch reward or scoring code. OpenCode rollouts stopped in the middle of a step lose their response usage, including the token totals of the steps that finished, and get model_call_usage_unavailable and token_usage_detail_unavailable gaps. The health check reports rollout_token_count_mismatch as unobserved for these rollouts instead of flagging them. The cline, crewai, and droid agents can already return a response without usage.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Documentation added or updated, or N/A with justification. (OpenCode agent README)
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format checks pass). (run with --files on the changed files)
  • New source files include the required Apache-2.0 SPDX header. (N/A: no new files)
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

OpenCode writes a step's token counts and `finish` field at the step's
`step-finish` event, which comes after the step's tools return. When the
agent timeout stops a session while a tool is running (for example, a
`task` call in the root session waiting on a subagent), the step's
message keeps the placeholder zeros that OpenCode created it with. The
agent summed the response usage from the stored messages, so the sum
omitted the tokens of a call that Gym had captured. The
`rollout_token_count_mismatch` check then flagged the rollout.

The agent now reports no response usage when any step was stopped this
way and adds a `model_call_usage_unavailable` gap whose detail contains
the message ID. Without response usage, the health check already reports
the token-count comparison as unobserved; a new test covers this. The
crewai agent already does the same when a run is interrupted during a
model call. The OpenCode agent README describes the new behavior.

In an OfficeQA Pro evaluation run (665 rollouts), all 8 rollouts flagged
with a mismatch had been stopped by the 7200 s agent timeout during a
tool call. In each case, the missing tokens equaled the usage of the
calls in the stopped steps.

Signed-off-by: Jarno Seppänen <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 30, 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.

@jseppanen

Copy link
Copy Markdown
Contributor Author

/ok to test 096d814

@nemo-pr-automation
nemo-pr-automation Bot requested review from a team November 30, 2026 18:29
@github-actions

Copy link
Copy Markdown
Contributor

@svcnvidia-nemo-ci svcnvidia-nemo-ci added risk:mid Autobot PR risk: mid bug Something isn't working area:agent Agent harnesses and Responses API agent behavior needs-review PR is ready for code review and waiting on a reviewer labels Nov 30, 2026

@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

I reviewed all four changed files and traced the SQLite export, session response and rollout-health consumers. I found no confirmed code defect, but the required real-rollout validation is still unresolved.

RISK: Provide representative smoke-rollout evidence from this head

Could you attach a real-model smoke run for 096d8148e33d00e21b73d319dea7b57c27ce46d7, including a step interrupted during a tool or subagent call? The supplied PR description says the 665 analyzed rollouts predate this change and a branch run has not been done. AGENTS.md requires representative model rollouts and inspection of agent and verifier behavior for behavior-changing agent work.

The new usage suppression removes the entire aggregate when a step is classified as stopped. That also makes the token comparison unobserved, so actual interrupted exports need validation beyond mocks. Please record the revision/configuration and agent/verifier outcomes, and show that an interrupted rollout returns usage: null, identifies the stopped message through model_call_usage_unavailable, and lists rollout_token_count_mismatch as unobserved. Include a completed control whose totals stay measured. Preserve grading, partial output and measured completed/failed-call counters. This asks for validation evidence, not a scoring change or a full benchmark rerun.

Validation

  • I relied on the head’s successful Lint check, Unit tests / Test, Unit tests / Server suite, four core shards, eight server shards and OpenCode conformance checks. Build Gym container and Unit tests / Changed server tests were skipped, not passed.
  • CPU probes ran in 766267172432.dkr.ecr.us-east-1.amazonaws.com/gym:228a16b96df8f925220a7edbff2d7d22317c78fb, the default-branch CI image selected because this head’s build was skipped. Its root dependency inputs match the head. SQLite/export, public HTTP and health-control probes confirmed unknown usage for stopped steps, identifying gaps, preserved timeout status and continued detection of genuine measured mismatches. leader/usage_health_matrix.py passed all four cases.
  • The new public HTTP regression test failed against the admitted base at the expected usage is None assertion, confirming fail-before protection. I did not rerun CI-covered head suites or execute a real-model rollout.

I could not verify whether later PR discussion supplies the missing evidence: the comments read timed out, and subsequent read attempts returned HTTP 503. The evidence request above is based on the supplied PR description.

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

@jseppanen

Copy link
Copy Markdown
Contributor Author

Smoke-rollout evidence from a real-model run with this change.

Revision and configuration. Gym main at 5398501 with #6622 and this PR's commit applied on top (the same diff as 096d814). OfficeQA Pro with the OpenCode harness and an SFT checkpoint served by vLLM, 40 tasks × 1. The OpenCode agent timeout was lowered from 7200 s to 300 s so that many sessions would stop mid-step. The control run used the same configuration without this PR (main + #6622). The PR description has the summary table.

Interrupted rollouts (24 of 40 in the run with this PR). Each has a root agent invocation with status incomplete and error type timeout. In all 24:

  • response.usage is null.
  • ng_trajectory.gaps has one model_call_usage_unavailable gap that identifies the stopped message, for example "detail": "msg_22e3295ca001WdOp4by2Yk8FuD: step stopped before OpenCode stored its usage".
  • The verdict lists rollout_token_count_mismatch under unobserved, and none have a rollout_token_count_mismatch finding.
  • The verifier still ran: every row has a reward. The partial output is kept (for example, 131 and 79 output items in rollouts 35-0 and 39-0).

Some interrupted rollouts are still unhealthy because of rollout_ended_on_failed_model_call (7 with this PR, 9 in the control), which comes from the stopped model call itself. This PR doesn't change that check.

Completed rollouts (16 of 40). All 16 have measured usage (for example, rollout 36-0: 220,913 input and 1,646 output tokens), no model_call_usage_unavailable gap, and the token check observed with no mismatch.

Control without this PR. 25 interrupted rollouts, all with summed usage and no gap; 5 have a rollout_token_count_mismatch finding. The 15 completed rollouts match the run with this PR: usage is measured and the token check finds no mismatch.

A full OfficeQA Pro run (133 tasks × 5) on the same commit and checkpoint with the default 7200 s timeout had no interrupted rollouts. All 665 rollouts kept their measured usage, and the token check was observed on all 665 with no findings.

@ffrujeri ffrujeri 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.

Thanks. A stopped step now leaves the usage unknown with a gap that names the message, instead of an undercounted sum, and a failed model call still counts as measured. The OfficeQA Pro smoke answers the bot's request: 24 interrupted rollouts with null usage and the gap, 16 completed controls still measured, and every row still verified. LGTM.

@ffrujeri
ffrujeri added this pull request to the merge queue Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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

Merged via the queue into main with commit 992f16d Dec 1, 2026
74 checks passed
@ffrujeri
ffrujeri deleted the jseppanen/opencode-unfinished-step-usage branch December 1, 2026 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent harnesses and Responses API agent behavior bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer risk:mid Autobot PR risk: mid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants