Repository navigation
fix(opencode-agent): stable sessions under token capture - #5657
Conversation
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]>
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]>
yaoyu-33
left a comment
There was a problem hiding this comment.
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.
| 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)); | ||
| }); |
There was a problem hiding this comment.
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?
| 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; | |
| } | |
| }); |
There was a problem hiding this comment.
Fixed in b7afc6c. The hook now updates the caller's system array in place and writes the marker only after a replacement.
| async function render(hook, sessionID, system) { | ||
| const output = { system }; | ||
| await hook["experimental.chat.system.transform"]({ sessionID, model: {} }, output); | ||
| return output.system; | ||
| } |
There was a problem hiding this comment.
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:
| 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; | |
| } |
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…e-stability Signed-off-by: bg51717 <[email protected]>
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]>
… by NVIDIA-NeMo#6175 Signed-off-by: bg51717 <[email protected]>
|
#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 |
Signed-off-by: bg51717 <[email protected]>
|
/ok to test 4733a47 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44468055346 |
What does this PR do?
Two fixes to
opencode_sandboxed_agentfor RL training:opencode_context_limitedreports 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 intoopencode_failed.stable-date.jsplugin 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: undersource_historythe next call started a new root, and underfingerprintits 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_agentruns 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.mjsplugin tests run)test_stable_date.mjsfails against the previous plugin and passes with the fix.pre-commit run --all-filesRollouts
No real-model rollout. A cross-date check ran real OpenCode 1.17.11 under libfaketime against the
vllm_modelserver withsource_historycapture over a fake vLLM worker (make_capture_harnessfrom 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:Compatibility and benchmark impact
Adds the
opencode_context_limitedverify field. The date pin applies only with token capture on.Checklist
pre-commit run --all-files) (so CI lint/format checks pass).git commit -s) (so the DCO check passes).