Skip to content

fix(opensandbox): time out background commands at Gym's own deadline - #5925

Merged
AdamRajfer merged 2 commits into
NVIDIA-NeMo:mainfrom
AdamRajfer:arajfer/opensandbox-background-timeout
Nov 10, 2026
Merged

AdamRajfer merged 2 commits into
NVIDIA-NeMo:mainfrom
AdamRajfer:arajfer/opensandbox-background-timeout

Conversation

@AdamRajfer

@AdamRajfer AdamRajfer commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What

With operations.background_exec: true, OpenSandbox background commands now time out at Gym's own deadline. Gym records provider_command_timeout, interrupts a command still running at timeout_s, and returns 124 with error_type="timeout". This is what the session path already does.

Why

execd reports the kill at a command's timeout only as a signal death (-1, signal: killed). Since #5127 the provider has deliberately not inferred a timeout from that. So in background mode a command that runs out its time never comes back as a timeout. Since #5859, opencode_sandboxed_agent treats an unexplained -1 as provider_command_outcome_unknown. An agent that uses its whole budget is therefore rerun as an environment fault instead of going to the verifier. We saw this on an OpenCode SWE task: three attempts, each ended at exactly the 3 h agent budget with the model still working.

This change does not bring back elapsed-time inference. The timeout result is returned only when Gym itself interrupted the command at its own deadline and execd accepted the interrupt. execd's timer moves command_timeout_headroom_s / 2 later and stays as a backstop. If the interrupt is refused, for example because the command finished just before it, the reported exit is kept.

Tests

  • New: a command still running at the deadline is interrupted and returns 124/timeout, and execd gets the backstop timeout. A command that finishes earlier is not interrupted. A refused interrupt keeps the reported -1.
  • tests/unit_tests/sandbox and responses_api_agents/opencode_sandboxed_agent/tests pass locally. The exceptions are two test_script_help_runs_by_direct_path tests and the test_openshell_provider.py collection, which fail the same way on main in my environment.
  • Live provider timeout and early-exit checks passed: partial output was retained and process termination confirmed (provider smoke). The rollout smoke also reached verification without retrying timeouts (rollout smoke).

execd reports the kill at a command's timeout only as a signal death (exit -1,
"signal: killed"), and since NVIDIA-NeMo#5127 the provider no longer infers a timeout from
that. With operations.background_exec a command that runs out its time therefore
never comes back as a timeout. A harness that treats an unexplained -1 as an
unknown outcome (opencode_sandboxed_agent since NVIDIA-NeMo#5859) reruns it as an
environment fault instead of verifying it.

Background exec now enforces timeout_s itself, as the session path already does:
it records provider_command_timeout, interrupts a command still running at the
deadline and returns 124 with error_type="timeout". execd's timer moves
command_timeout_headroom_s / 2 later and only stops a command whose interrupt was
refused; a refused interrupt keeps the reported exit.

Signed-off-by: Adam Rajfer <[email protected]>
@AdamRajfer AdamRajfer self-assigned this Nov 10, 2026
@copy-pr-bot

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

@AdamRajfer

Copy link
Copy Markdown
Contributor Author

/ok to test 37f1266

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

@mpatel31415 mpatel31415 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 — one reproduced reliability issue below to resolve before approval. The explicit Gym-owned timeout approach is consistent with #5127 and #5859.

Validation: all 295 tests in tests/unit_tests/sandbox/test_opensandbox_provider.py pass locally. I also compared the status-outage scenario against the parent implementation and checked the SDK 0.1.15 / execd 1.1.0 interrupt contract.

Before approval, please attach the live OpenSandbox timeout smoke evidence discussed in Slack, including retained partial output, confirmed process termination, and the intended OpenCode timeout reaching verification. CI core shard 2 currently fails in the unchanged test_only_detached_services_survive_harness_group_termination[False-grace]; this needs a clean rerun or documented baseline diagnosis.

Non-blocking timing note: Gym's command deadline starts after submission returns, while execd starts its timer at launch. A submission reply delayed longer than half the headroom can still let the backstop win and preserve -1/unknown (reproduced with timeout=0.1s, headroom=0.2s, submit latency=0.15s). Please consider anchoring the deadline before submission or documenting this remaining limit.

…dline

When status reads failed after the command deadline, the past deadline stayed in
the sleep limits and zeroed the polling and backoff delay until the status
recovery window ran out. Only a future deadline now shortens the sleep. Adds a
regression test for a status outage that spans the deadline.

Signed-off-by: Adam Rajfer <[email protected]>
@AdamRajfer

Copy link
Copy Markdown
Contributor Author

/ok to test 200a23c

@AdamRajfer

Copy link
Copy Markdown
Contributor Author

Live check on a real OpenSandbox deployment at 200a23cd70 (OpenSandbox server 1.1.0, execd 1.1.0). Both commands ran through the provider with operations.background_exec: true in one fresh sandbox:

command timeout_s rc error_type stdout wall
echo started; sleep 30 5 124 timeout started 9.1 s
echo done 5 0 none done 0.2 s
  • On the timeout, the output printed before the deadline is kept, and stderr ends with Command timed out after 5s; Gym interrupted it. Right after the interrupt, ps in the sandbox shows no sleep 30, and it still shows none 35 s later. So the real interrupt stops the process, as the mocks assume.
  • It returns after 9.1 s rather than 5 s because, after the interrupt, the next status read follows the normal poll cadence (background_poll_interval_s: 30, still ramping up from 0.5 s).
  • Provider config: connection has protocol: http, request_timeout_s: 300 and the aiohttp transport. operations has background_exec: true, command_retries: 0, background_poll_initial_s: 0.5, background_poll_interval_s: 30 and status_poll_timeout_s: 10.

An OpenCode rollout smoke with the verifier, 2 tasks with a 300 s agent budget, is queued. I'll add its row outcomes here when it ends.

@AdamRajfer
AdamRajfer marked this pull request as ready for review November 10, 2026 12:01
@AdamRajfer

Copy link
Copy Markdown
Contributor Author

OpenCode rollout smoke at 200a23cd70: SWE task set, 2 tasks, 300 s agent budget, background_exec: true. Both rollouts hit the budget. Each agent command came back with Command timed out after 300s; Gym interrupted it, went on to the verifier and was scored (reward 0, not masked, no failure kind). The coverage file shows 2/2 measured, 0 failed, 0 unscored and 2 attempts in total, so nothing was retried. Before this change, the same timeout came back as provider_command_outcome_unknown and was rerun as an environment fault.

@mpatel31415 mpatel31415 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 — LGTM.

Reviewed 200a23c. The status-polling regression is fixed, and the regression test fails against the previous implementation and passes with the fix.

Validation: all 296 provider tests pass locally; CI passes after rerunning the unchanged checkpoint-worker timing tests, which also passed locally. Live timeout and early-exit checks confirm retained partial output and process cleanup. The rollout smoke reached verification without infrastructure retries.

No remaining blocking findings.

@AdamRajfer
AdamRajfer added this pull request to the merge queue Nov 10, 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/42814235326

@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 — one timeout-bound regression and the required rollout evidence remain; there’s also a documentation correction below.

I reviewed all three changed files and traced the timeout result into OpenCode’s capture and verification paths. The historical busy-poll issue is fixed: expired command deadlines no longer constrain retry sleeps, while polling cadence and Retry-After handling remain active.

RISK — Complete the representative OpenCode timeout smoke

The supplied live check establishes partial-output retention and process termination for sleep, but its comment explicitly leaves the model-driven OpenCode smoke queued. The intended timeout → capture → verifier path therefore still lacks the representative rollout evidence required by AGENTS.md and the review rubric.

Could you finish that smoke against this head and attach the model, task IDs, effective configuration, and row/log outcomes? Include a budget-exhausted agent that returns 124/timeout, retains a usable export, and reaches verification without being retried as provider_command_outcome_unknown, plus a normally completed control. Preserve the existing capture guard and configured grading policy: missing capture and genuinely unknown outcomes must not become gradable merely to complete the smoke. No production-code change is requested for this evidence gap.

Validation was static; I couldn’t execute tests or inspect the external SDK implementation. One delegated report wasn’t available, so I inspected its documentation and consumer scope directly.

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

seen_running = True
if command_deadline is not None and not timed_out and loop.time() >= command_deadline:
timed_out = await self._interrupt_at_deadline(
handle, execution_id, timeout_s=command_timeout_s, request_timeout_s=status_timeout_s

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.

RISK — Bound the interrupt by the remaining client wait.

Could you apply the overall deadline to this new request? Status reads already honor that bound, but the interrupt receives the full status_timeout_s.

Source-derived trigger, not executed: set timeout_s=0.1, command_timeout_headroom_s=0.2, and status_poll_timeout_s=1; keep status Running and make interrupt() take 0.3 seconds. The interrupt starts around 0.1 seconds and returns around 0.4 seconds, beyond the overall client deadline of roughly 0.3 seconds. Only then does the next loop raise provider_command_outcome_unknown. A stalled interrupt can overrun that deadline by almost an entire status-request budget.

In _exec_background, replace this call’s arguments with:

handle,
execution_id,
timeout_s=command_timeout_s,
request_timeout_s=(
    min(status_timeout_s, max(0.0, deadline - loop.time()))
    if deadline is not None
    else status_timeout_s
),

This preserves the invariant that interruption cannot extend the existing client wait. Keep the single attempt and preserve uncertainty when its acknowledgement times out; a request timeout does not prove remote termination.

Proposed regression: use a Running fake whose interrupt() waits on an unset event. Assert cancellation at the remaining client deadline, a final provider_command_outcome_unknown failure, one submission, and one interrupt attempt. Retain the existing deadline-test assertions: prompt acceptance returns 124/timeout with partial output, early completion causes no interrupt, and refusal preserves -1 with error_type=None. Please verify the suggested code before adopting it.

| Other string | Wrap the command with `su -s /bin/sh -c ... <user>`. |

Commands with a reported exit status return `SandboxExecResult`. A missing or contradictory final outcome raises `NgFailureEvent` with `provider_command_outcome_unknown`. A signal death near the configured deadline does not establish that the command timer fired, so its reported exit code is preserved. For session commands, Gym observes its own deadline directly: it records `provider_command_timeout`, performs cleanup and returns the existing timeout result. A client request timeout instead reports `provider_io_timeout`; it does not establish that the remote command stopped.
Commands with a reported exit status return `SandboxExecResult`. A missing or contradictory final outcome raises `NgFailureEvent` with `provider_command_outcome_unknown`. A signal death near the configured deadline does not establish that the command timer fired, so its reported exit code is preserved. Gym observes its own deadline directly. For session commands it records `provider_command_timeout`, performs cleanup and returns the existing timeout result. For background commands (`operations.background_exec: true`) it records `provider_command_timeout`, interrupts a command still running at the deadline and returns code `124` with `error_type="timeout"`; execd's own timer is set `command_timeout_headroom_s / 2` later and only stops a command whose interrupt was refused. A client request timeout instead reports `provider_io_timeout`; it does not establish that the remote command stopped.

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.

NOTE — Suggest qualifying the timeout guarantee.

This says a background command still running at the deadline returns 124/timeout, and execd’s backstop acts only after a refused interrupt. The implementation is narrower: it interrupts only after a successful Running status read, and any interrupt exception leaves the reported outcome unchanged. A status outage can prevent any interrupt attempt, while subsequent status/log failures can still raise instead of returning a timeout. Operators shouldn’t rely on the unconditional wording when interpreting retries.

Replace only the background-command sentence with wording such as:

For background commands (operations.background_exec: true), Gym records provider_command_timeout and makes one interrupt attempt when a successful status read still reports Running at its deadline. An acknowledged interrupt enables the 124 result with error_type="timeout", subject to successful completion and output handling. execd’s timeout is set to timeout_s + command_timeout_headroom_s / 2 as a backstop. Without an acknowledged interrupt, Gym preserves the reported outcome or raises the existing provider failure.

Preserve the neighboring session-timeout explanation and the distinction between a command timeout, a signal exit, and a client I/O timeout. Static acceptance: check the wording against the accepted-interrupt, refused-interrupt, and status-outage branches; none should promise 124 without an acknowledged interrupt. No runtime test is needed for this wording change.

Merged via the queue into NVIDIA-NeMo:main with commit 0a264a3 Nov 10, 2026
107 of 110 checks passed
@AdamRajfer
AdamRajfer deleted the arajfer/opensandbox-background-timeout branch November 10, 2026 12:31

This branch was previously deployed

1 inactive deployment
public — 200a23cd Deployed Nov 10, 2026 by copy-pr-bot[bot] via release / finalize / notify #11399
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants