Skip to content

fix(opencode-agent): stable sessions under token capture - #5657

Merged
yaoyu-33 merged 8 commits into
NVIDIA-NeMo:mainfrom
bg51717:biguo/opencode-capture-stability
Nov 19, 2026
Merged

yaoyu-33 merged 8 commits into
NVIDIA-NeMo:mainfrom
bg51717:biguo/opencode-capture-stability

Conversation

@bg51717

@bg51717 bg51717 commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Two fixes to opencode_sandboxed_agent for RL training:

  • opencode_context_limited reports whether the session ended on a full context (its last assistant message stopped on length), so a recipe can mask those sessions like overlong rows instead of folding them into opencode_failed.
  • With token capture on, the stable-date.js plugin pins the system prompt's date to the session's first call. OpenCode writes today's date into every call's system prompt, so a session running past 00:00 UTC sent a changed prefix: under source_history the next call started a new root, and under fingerprint its token prefix no longer matched its parent. The plugin goes through the same registration and verified upload as the other plugins. A run that finishes without its marker fails; the marker shows that the plugin loaded and replaced a date at least once.

opencode_agent runs OpenCode 1.17.11 too, but continues a session in separate processes, so it needs the date persisted; that is out of scope here and tracked in #6185.

This PR also carried a fix that lets the sandbox's error type outrank the error in the session's last message; #6175 has since landed the same fix on main, so it is no longer part of this diff.

No separate issue for the fixes above: this PR describes the failures it addresses.

Proposed labels: bug, area:agent.

Validation

  • gym env test +entrypoint=responses_api_agents/opencode_sandboxed_agent +should_validate_data=true (with Node available, so the .mjs plugin tests run)
  • The updated test_stable_date.mjs fails against the previous plugin and passes with the fix.
  • The cross-date check under Rollouts.
  • pre-commit run --all-files

Rollouts

No real-model rollout. A cross-date check ran real OpenCode 1.17.11 under libfaketime against the vllm_model server with source_history capture over a fake vLLM worker (make_capture_harness from its capture tests). The clock moves from 2026-11-01 23:59 to 2026-11-02 00:10 during the first call, which the worker answers with a bash tool call. Two runs each:

Plugin Dates of the two requests Second call's parent
none Nov 01 → Nov 02 none (new root)
previous Nov 01 → Nov 02 none (new root)
this PR Nov 01 → Nov 01 the first call

Compatibility and benchmark impact

Adds the opencode_context_limited verify field. The date pin applies only with token capture on.

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. N/A: no user-facing docs changed.
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format checks pass).
  • New source files include the required Apache-2.0 SPDX header.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

bg51717 and others added 3 commits November 4, 2026 17:21
opencode_failed folds a context-limited session (its last assistant
message stopped on length: the generation hit the context limit, or the
next request no longer fit and the model server answered with a length
stop) together with real failures, so a trainer could not treat the two
differently. Expose it as opencode_context_limited, so a recipe can mask
such sessions like overlong rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…'s last message

When the sandbox kills the OpenCode run (for example at its timeout), the
session's export still ends with the error the kill left in its last
assistant message. opencode_error_type took that message's error name, so
a timeout reported by the sandbox showed up as an OpenCode error and mask
rules keyed on the timeout never matched. Prefer the sandbox's error type.

Based on Jiaqi Zeng's c546efa. The provider side of that change, which
classifies a kill at the deadline as a timeout, is already on main (NVIDIA-NeMo#5157).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…en capture

OpenCode renders today's date into every call's system prompt. Source-history
capture links a call to its parent by exact prompt text, so the first call
after 00:00 UTC in a running session found no parent and the rollout was
discarded.

With token capture on, load stable-date.js, which uses OpenCode's
experimental.chat.system.transform hook to pin the date line to the
session's first call. The plugin writes a marker once it pins a date; a run
that finishes without the marker fails like a required-MCP setup error, so a
hook or date-format change in a future OpenCode cannot silently bring the
discarded rollouts back.

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

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

@bg51717
bg51717 marked this pull request as ready for review November 5, 2026 07:23
@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Nov 6, 2026
Resolve conflicts with NVIDIA-NeMo#5128: clear the stable-date marker in the split
setup command, and report a missing marker as an NgFailureEvent like the
other capture checks. Timed-out test runs now carry the launch marker
that main requires.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added 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 7, 2026

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

Thanks for this. The context-limit flag and the change that lets a sandbox timeout outrank the killed session's abort error both look right on top of main (after #5859, only None or "timeout" reach that branch). The second one also lets main's opencode_error_type == "timeout" health signal see those rows. The branch merges cleanly with current main, and the agent's tests pass both at the head and on the merged tree.

The main issue is the date pin: in OpenCode 1.17.11 it does not reach the request, and the marker check reports success anyway. Details, a suggested fix, a test fix, an evidence ask, and a scope question are inline.

Comment on lines +20 to +29
output.system = output.system.map((text) => {
const line = typeof text === "string" ? text.match(DATE_LINE) : null;
if (!line) return text;
if (!pinned.has(sessionID)) pinned.set(sessionID, line[0]);
if (!marked) {
writeFileSync(MARKER, "ok\n");
marked = true;
}
return text.replace(DATE_LINE, () => pinned.get(sessionID));
});

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.

stable-date.js:20-29

I think the date never actually gets pinned in OpenCode 1.17.11. request.ts builds a local system array, calls the hook with a fresh { system } object, ignores what comes back, and then builds the messages from its own array (L74-L78). Assigning a new array to output.system therefore has no effect. The marker is still written inside the map, so the check at app.py:1537 passes even though the prompt still has the new date. A node repro that holds the caller's array, the way request.ts does, still sends "Mon Nov 02" on the second call. Could you change the array in place, and write the marker only after a replacement was applied?

Suggested change
output.system = output.system.map((text) => {
const line = typeof text === "string" ? text.match(DATE_LINE) : null;
if (!line) return text;
if (!pinned.has(sessionID)) pinned.set(sessionID, line[0]);
if (!marked) {
writeFileSync(MARKER, "ok\n");
marked = true;
}
return text.replace(DATE_LINE, () => pinned.get(sessionID));
});
// OpenCode keeps using the array it passed in, so change it in place.
output.system.forEach((text, i) => {
const line = typeof text === "string" ? text.match(DATE_LINE) : null;
if (!line) return;
if (!pinned.has(sessionID)) pinned.set(sessionID, line[0]);
output.system[i] = text.replace(DATE_LINE, () => pinned.get(sessionID));
if (!marked) {
writeFileSync(MARKER, "ok\n");
marked = true;
}
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in b7afc6c. The hook now updates the caller's system array in place and writes the marker only after a replacement.

Comment on lines +13 to +17
async function render(hook, sessionID, system) {
const output = { system };
await hook["experimental.chat.system.transform"]({ sessionID, model: {} }, output);
return output.system;
}

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.

tests/test_stable_date.mjs:13-17

render returns output.system, the property the hook reassigns, so these tests pass with the current plugin even though OpenCode never sees the change. If the helper asserts on the array the caller passed in, the tests model OpenCode's call site and would have caught the issue above:

Suggested change
async function render(hook, sessionID, system) {
const output = { system };
await hook["experimental.chat.system.transform"]({ sessionID, model: {} }, output);
return output.system;
}
async function render(hook, sessionID, system) {
// OpenCode passes a fresh `{ system }` and keeps reading its own array, so assert on that array.
await hook["experimental.chat.system.transform"]({ sessionID, model: {} }, { system });
return system;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in b7afc6c. The helper now returns the caller's array, as OpenCode's call site reads it. Locally, the date tests fail with the previous plugin and pass with the fix.

Comment on lines +1535 to +1536
if self._token_id_capture_enabled() and result is not None and result.return_code == 0:
# A finished run made model calls; without the marker the date pin did not apply.

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:1535-1536

The marker shows that the hook ran, but not that the pinned date reached the model request (see the stable-date.js comment). Since this changes behavior for every token-capture training run, could you add one real rollout to the PR, replacing "Rollouts: N/A" as AGENTS.md asks for agent changes? A capture run with OpenCode 1.17.11 where the clock moves past a date boundary between calls (for example libfaketime in the sandbox) should show the second call keeping the first date and no unresolved_parent in the capture chain.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ran a cross-date check: real OpenCode 1.17.11 under libfaketime, talking to the vllm_model server with source_history capture over a fake vLLM worker (the make_capture_harness used by Gym's capture tests); the clock moves past midnight during the first call. With the fix, the second request keeps the first date and capture links it to the first call. Without the plugin, or with the previous one, the date changes and the second call starts a new root. No real-model rollout was run; the PR description has the details.

Comment on lines +905 to +907
if self._token_id_capture_enabled():
# Source-history capture links calls by exact prompt text; keep the date stable.
policy_plugins.append("stable-date.js")

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:905-907

The session harness opencode_agent on main runs the same OpenCode 1.17.11 and supports token_id_capture, but it has no date pin. Its capture sessions should hit the same midnight unresolved_parent break. The relay fix makes sense for the setups that still need it (swebench/swe_gym/r2e_gym, HLE search). Could you either port the pin to opencode_agent or say in the PR that it is out of scope, with a follow-up issue? Its continuation path runs separate opencode processes, so an in-memory Map would need the pinned date persisted, for example in the session dir.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Kept this PR scoped to opencode_sandboxed_agent and said so in the description. Tracking the opencode_agent date pin, persisted across its separate opencode processes, in #6185.

@github-actions github-actions Bot removed the sla:triage-overdue Review assignment is over the one-business-day SLA label Nov 12, 2026
OpenCode 1.17.11 builds the request from the array it passes to the
system-transform hook, so assigning a new array had no effect. Update
the array in place, write the marker only after a replacement, and
make the test helper assert on the caller's array.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

#6175 landed the same sandbox-error priority fix on main, so I dropped it from this PR along with its duplicate test; what remains is the stable date pin and opencode_context_limited, and the description is updated to match.

@bg51717
bg51717 requested a review from yaoyu-33 November 17, 2026 07:44
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Nov 18, 2026
Signed-off-by: bg51717 <[email protected]>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the risk:mid Autobot PR risk: mid label Nov 18, 2026
@yaoyu-33
yaoyu-33 enabled auto-merge November 19, 2026 05:34
@yaoyu-33

Copy link
Copy Markdown
Contributor

/ok to test 4733a47

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 19, 2026 05:35
@yaoyu-33
yaoyu-33 added this pull request to the merge queue Nov 19, 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/44468055346

Merged via the queue into NVIDIA-NeMo:main with commit 826c8c1 Nov 19, 2026
68 of 70 checks passed
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 sla:review-overdue Review response is over the one-business-day SLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants