Skip to content

fix(litmus_agent): release the sandbox through the shared session hooks - #5482

Merged
cmunley1 merged 31 commits into
mainfrom
yuya/gymint-177-litmus-agent-close-sessions
Nov 13, 2026
Merged

cmunley1 merged 31 commits into
mainfrom
yuya/gymint-177-litmus-agent-close-sessions

Conversation

@yaoyu-33

@yaoyu-33 yaoyu-33 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Release Litmus sandbox state through the shared resources lifecycle from #5709. Verification uses the base-registered route and its tracing, grading observations, and judge-failure handling, including verifier-only configurations without a sandbox provider. Direct verify(body) callers retain request-only grading.

The shared lifecycle in #5709 and MCP support in #5708 are merged into main. This PR contains only the Litmus migration and related tests/documentation. No separate issue is needed for this focused migration of an existing resources server.

  • Keep sandbox startup in a shielded task so cancellation of a waiting tool request or close does not discard an in-flight allocation. The session lock remains held through startup: verification and close wait for allocation to settle before cleanup.
  • Wait for startup/execution before stopping the sandbox. A failed stop preserves the handle and ownership for a later close or retirement retry. This requires the server/event loop to stay alive and the provider to return a handle; forced process termination and allocation failure before handle publication remain outside this guarantee.
  • Use the public verification context to release identity and sandbox state, preserve close fencing, and retain compact retry evidence. Request-scored retries retain an empty marker (retain(None)) because the request carries the answer and grading data.
  • Reuse the base verification route instead of removing and rebuilding its wrapper chain. HTTP regressions cover scoring records and bindings, judge-failure masking, cleanup, and retry.

Validation

Final Litmus source at ca1315764, on workstation ws, Python 3.13.14, with the validation checkout first on PYTHONPATH and NEMO_GYM_EXTRA_ROOTS:

  • python -m pytest -o addopts='' -o log_cli=false resources_servers/litmus_agent/tests -q --tb=short: 112 passed.
  • pre-commit run --all-files: passed in the combined validation checkout, including Ruff, formatting, guardwords, configuration checks, README generation, and import layering. git diff --check passed. New commits include DCO sign-off.
  • Real-model HTTP smoke with nvidia/nemotron-3.5-lightning-30b-a3b: the model returned ((3)); the base verification route scored 1.0, unmasked, emitted a completed grading record bound to the response and score, and released typed identity. Close succeeded. Both changed Litmus files matched the committed source hashes before and after the run. This smoke exercises the changed verifier-only route; it does not start a sandbox or establish benchmark parity.
  • Earlier real Enroot sandbox smoke at 56c33f3705f5669705d432e1c8e53e885d07ac6a used the same model, produced Python output 385 through two real tools, scored 1.0 unmasked, and released all session/identity state. That evidence is historical; the latest route refactor did not change sandbox execution.

The branch includes main at acb67734c. The earlier combined refresh passed all nine environment suites (904 tests) and shared lifecycle/MCP/process-supervisor checks (213 tests); those counts describe that earlier validation, not the latest focused run. These checks do not replace the current repository CI matrix or review approval.

…sion

With sandbox_provider set, litmus_agent starts a per-rollout sandbox keyed
by the session cookie, and stopped it only in the wrapped /verify or at
shutdown. Under single_agent_turn_legacy, an episode that never reached
/verify kept its sandbox until the provider TTL (OpenSandbox) or server
shutdown (Apptainer), because the inherited close_resources_session kept
no state. _cleanup_session also dropped the session before stopping it
and swallowed stop errors, so a failed stop leaked the sandbox.

- seed_session binds a typed seed's resources_session_id to the cookie and
  records its episode and task; the legacy /run seed body keeps no state.
- close_resources_session stops the sandbox by resources_session_id, fences
  late seeds with ResourcesSessionFence, is idempotent, and raises when the
  stop fails, so the caller can retry.
- A sandbox whose stop fails stays registered so a later cleanup retries
  it; verify logs the failure and keeps its score, and shutdown goes on to
  the remaining sandboxes.
- A tool call for a closed session gets ResourcesSessionLostError (404); a
  call that a close overtook stops the sandbox it started.
- A /verify for a closed session returns lost_session_verification.
- Drop the shared litmus-no-session fallback key.

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

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

@yaoyu-33 yaoyu-33 added area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers bug Something isn't working labels Nov 3, 2026
@yaoyu-33

yaoyu-33 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test a5e35cf

@yaoyu-33
yaoyu-33 marked this pull request as ready for review November 3, 2026 18:33
@yaoyu-33
yaoyu-33 requested a review from a team as a code owner November 3, 2026 18:33
@yaoyu-33 yaoyu-33 added the needs-review PR is ready for code review and waiting on a reviewer label Nov 3, 2026
@yaoyu-33
yaoyu-33 requested a review from a team as a code owner November 3, 2026 18:33
yaoyu-33 and others added 3 commits November 3, 2026 11:05
…ping it

Verify's cleanup ran without the session's lock, so a verify that arrived while the first tool call was still creating its sandbox stopped a wrapper with no handle yet: that only released the client. Startup then created the container, which nothing tracked or stopped. A close could likewise stop a sandbox under a running cell, so the call failed with a 500 instead of finishing.

_cleanup_session now holds the session's lock across the stop, and a tool call that waited behind a cleanup gets 404. The post-exec closed check could no longer fire, so it is gone; its test is replaced by tests that drive a real close during a cell and a real verify during startup, both of which fail on the previous code. The conformance import moves to nemo_gym.server_runtime.testing, and the README notes OpenShell's queued deletes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 5678ae6

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 3, 2026 21:45
@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Nov 4, 2026
yaoyu-33 and others added 5 commits November 4, 2026 15:20
…ycle

- The repo contract test counts a server that implements the state hooks as
  closing through the inherited close_resources_session. newton_bench,
  workplace_assistant, tavily_search and indirect_prompt_injection override
  seed_session but close through the hooks, and failed it.
- Export reject_partial_typed_seed from nemo_gym.server_runtime, so servers
  that override seed_session reuse the shared guard instead of a copy.
- The MCP seed wrapper still recognises a request param annotated as
  Annotated[Request, ...]; keeping Annotated metadata had made it inject a
  second Request and fail with KeyError.
- Name the 409 for a typed seed over a cookie session "cookie-seeded", not
  "legacy", and document that a served seed_session override declares
  request before body.
- Shared tests now cover the MCP-wrapped seed route, close after verify
  (checked against the verified episode), and a cookie seed of a verified
  typed session.
- The add-benchmark skill describes the three state hooks as the default
  for a stateful server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
…nt cookie sessions

- Stop the remaining sandboxes concurrently at shutdown. The server
  supervisor kills the process about a second after SIGINT, so stopping
  them one at a time left most of them to their ttl_s. A failed stop is
  still logged with its session ID and does not keep the others from
  stopping.
- Test that shutdown stops sandboxes concurrently, and that a shutdown
  during a pending sandbox start waits for the start, stops the started
  sandbox, and fails the waiting tool call as a lost session.
- README: shutdown stops what it can within the shutdown grace and leaves
  the rest to ttl_s; after a cookie session's /verify a tool call on that
  cookie gets 404 until the cookie is seeded again; a cookie seed of a
  closed session gets 409.
- Tests import check_resources_session_contract and ResourcesParticipant
  from nemo_gym.server_runtime, which test_public_api_facades requires.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
The docs now describe only the typed Environment Server path. The cookie
seed still works unchanged; the PR description records that compatibility.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 4022766

@kajalj22
kajalj22 removed request for a team November 6, 2026 22:51
Base automatically changed from yuya/shared-resources-lifecycle to main November 8, 2026 06:36
@yaoyu-33

yaoyu-33 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test d9a8df6

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test ca13157

Preserve the shared session cleanup logger and the multiworker FastAPI entrypoint from main.

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

Copy link
Copy Markdown
Contributor Author

/ok to test 2934bed

@cmunley1
cmunley1 enabled auto-merge November 13, 2026 01:12
@cmunley1
cmunley1 added this pull request to the merge queue Nov 13, 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/43343355024

Merged via the queue into main with commit ea8a5a4 Nov 13, 2026
66 checks passed
@cmunley1
cmunley1 deleted the yuya/gymint-177-litmus-agent-close-sessions branch November 13, 2026 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants