Skip to content

fix(agent): enforce monotonic deadlines on the event loop's clock - #6324

Merged
adil-a merged 6 commits into
mainfrom
cwing/fix-uvloop-deadline
Nov 27, 2026
Merged

adil-a merged 6 commits into
mainfrom
cwing/fix-uvloop-deadline

Conversation

@cwing-nvidia

@cwing-nvidia cwing-nvidia commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Deadlines built as time.monotonic() + timeout were passed to asyncio.timeout_at, which compares them with loop.time(). Under uvloop (which uvicorn picks automatically when installed), loop.time() also counts time the machine slept, so on macOS it can run days ahead of time.monotonic(). On the machine where this was found the gap was about 927,750 s, so every such deadline was already in the past and expired at once. A hello_world OpenCode rollout with a Docker sandbox failed every time with a TimeoutError about 6 s in, inside SandboxSession.execute. Linux CI does not catch it because the two clocks agree there.

This PR adds nemo_gym.utils.deadlines.monotonic_timeout(deadline), which turns a monotonic deadline into a remaining duration for asyncio.timeout, and uses it at every site that passed a monotonic deadline to timeout_at:

  • nemo_gym/agents/utils/sandbox_session.py (execute)
  • responses_api_agents/opencode_agent/app.py (activation preparation and the session response)
  • responses_api_agents/miniswe_sandboxed_agent/app.py (agent setup, two sites)
  • environment_servers/swe_together/app.py (turn collection)
  • nemo_gym/server_runtime/_checkpoint/agent.py (Activation.slot): its only caller, OpenCode, passes a monotonic deadline, but slot treated it as loop time.
  • responses_api_agents/legal_agent_bench_native/app.py: built its deadline from loop.time() and passed it to SandboxSession.execute, which expects a monotonic one. It now uses monotonic() throughout.

Deadlines stay monotonic everywhere else, since they are used in arithmetic with monotonic(). Sites that build and consume their deadline on loop.time() alone (opencode_agent/sandbox.py, terminal_bench_4/verifier.py) are unchanged. The SandboxSession.execute and Activation.slot docstrings and checkpointable-harness.mdx now say the deadline is a time.monotonic() time. The helper is exported from nemo_gym.utils for servers.

Issue

None filed. The bug was found and root-caused during a local smoke run, and this PR is the complete fix, so a separate issue would only duplicate this description.

Validation

  • tests/helpers/event_loop_clock.py (new): an event loop whose clock runs 10^6 s ahead of time.monotonic(), as uvloop's does on macOS.
  • tests/unit_tests/utils/test_deadlines.py (new): on that loop, monotonic_timeout does not fire a future deadline early, fires a past one, fires a near one after its remaining time, and never expires for None.
  • Every changed call site has a test on that loop (sandbox session, checkpoint slot, OpenCode slot and preparation, miniswe setup, swe_together turn collection, legal agent bench activation and turn). Each one fails when its site is changed back to read the deadline on the loop clock.
  • pytest tests/unit_tests/utils tests/unit_tests/agents tests/unit_tests/test_*.py: all pass except three test_outcome_policy.py::test_upload_evidence_roundtrips_in_reports_and_process_projection cases, which fail the same way on unmodified main.
  • responses_api_agents/opencode_agent/tests: pass apart from intermittent failures in test_sandbox_sessions.py / test_opencode2_session.py that also occur on unmodified main and pass when rerun alone.
  • responses_api_agents/miniswe_sandboxed_agent/tests (in a venv with the server's dependencies): 108 passed, 1 skipped. test_server.py was not collected because of a local venv dependency issue (pathspec import via terminal_bench_4), unrelated to this change.
  • environment_servers/swe_together/tests: 282 passed.
  • tests/unit_tests/server_runtime/test_checkpoint_agent.py and responses_api_agents/legal_agent_bench_native/tests: pass.
  • pre-commit run --files <changed files> and lint-imports: passed.

Rollout evidence

macOS (Apple silicon), Docker via Colima, uvloop 0.21.0 installed in the OpenCode agent venv:

gym eval run environments/hello_world --agent opencode_agent --sandbox docker --model-type openai_model --model <model> --model-url <url> --model-api-key <key> +default_host=<LAN IP> ++harbor_hello_5fworld_agent.responses_api_agents.opencode_agent.context_window=131072 ++harbor_hello_5fworld_agent.responses_api_agents.opencode_agent.max_output_tokens=16384

With this fix the rollout reached the model and scored reward 1.0 with 0 failures. Without the fix, the same command on unmodified main (71c58dc6a3), same machine and same uvloop, dropped the rollout from the score: the agent returned 500 with TimeoutError raised from asyncio.timeout_at(deadline) in SandboxSession.execute (sandbox_session.py:231), before the model was reached.

Compatibility

No behavior change where loop.time() equals time.monotonic() (the default asyncio loop, and uvloop on Linux). One new public helper, nemo_gym.utils.monotonic_timeout. Activation.slot(semaphore, deadline=...) now takes a time.monotonic() deadline instead of an event loop time; a checkpointable harness that passed loop.time() there must pass monotonic() instead. No config, data or benchmark result changes.

@copy-pr-bot

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

@cwing-nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test bed436d

@cwing-nvidia
cwing-nvidia marked this pull request as ready for review November 18, 2026 03:32
@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 18, 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

LGTM. No concerns found. I inspected all eight changed files and traced the affected deadline producers, timeout handling, and session cleanup paths. The converted call sites use monotonic deadlines, and the added regressions exercise the event-loop clock offset.

Static review only. I did not run tests or rollouts.

Note: formal approval is not available for this request, so this review is posted as a comment only.

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

adil-a
adil-a previously approved these changes Nov 26, 2026
…line

Signed-off-by: Chris Wing <[email protected]>

# Conflicts:
#	nemo_gym/agents/utils/sandbox_session.py
#	nemo_gym/utils/__init__.py
#	responses_api_agents/opencode_agent/app.py
…ent bench

Activation.slot() waited until its deadline on the event loop's clock while
OpenCode passed it a time.monotonic() deadline, so under uvloop on macOS the
slot wait expired at once. It now uses monotonic_timeout.

legal_agent_bench_native computed its deadline from loop.time() and passed it
to SandboxSession.execute, which expects a time.monotonic() deadline. It now
uses time.monotonic() throughout.

Signed-off-by: Chris Wing <[email protected]>
Move the miniswe monotonic_timeout import into the import block, state in the
checkpointable harness guide that Activation.slot takes a time.monotonic()
deadline, and describe the uvloop clock by its observed behavior.

Signed-off-by: Chris Wing <[email protected]>
Add tests that run the swe_together collection deadline, the miniswe setup
deadlines and the OpenCode slot and preparation deadlines on an event loop
whose clock runs ahead of time.monotonic(); each fails if its call site goes
back to asyncio.timeout_at. Share the clock-ahead loop through tests/helpers,
drop the test that only exercised asyncio.timeout_at, and state in
SandboxSession.execute that its deadline is a time.monotonic() time.

Signed-off-by: Chris Wing <[email protected]>
…s on a fast-running loop clock

The clock test now lets the activation wait yield and asserts that staging started, so it fails when either deadline is read on the event loop clock. The shared helper cleans up its loop the way asyncio.run does.

Signed-off-by: Chris Wing <[email protected]>
@cwing-nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test cf83ea1

@nemo-pr-automation
nemo-pr-automation Bot requested review from a team November 27, 2026 04:21
@github-actions

Copy link
Copy Markdown
Contributor

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the risk:high Autobot PR risk: high label Nov 27, 2026
@adil-a
adil-a added this pull request to the merge queue Nov 27, 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/46035394024

Merged via the queue into main with commit a01145d Nov 27, 2026
81 of 86 checks passed
@adil-a
adil-a deleted the cwing/fix-uvloop-deadline branch November 27, 2026 16:56
adil-a added a commit that referenced this pull request Nov 27, 2026
Main fixed the macOS clock mismatch itself (#6324): a SandboxSession or
checkpoint-slot deadline is now a time.monotonic() time, enforced with
monotonic_timeout. This branch had fixed the same bug the other way, with
deadlines on the event loop's clock, and the two cannot be mixed: a loop-clock
deadline handed to main's execute would expire at once under uvloop on macOS
again. So this branch's clock changes are dropped in favour of main's, in
sandbox_session, OpenCode, Pi, Codex, OpenClaw and mini-SWE, together with
their loop-clock tests; mini-SWE keeps its setup-timeout warning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Adil Asif <[email protected]>
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:high Autobot PR risk: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants