Skip to content

feat(checkpoint): ask a long-running agent harness to park when a checkpoint opens - #4222

Open
pthombre wants to merge 2 commits into
pthombre/partial-ckpt-sandbox-resourcesfrom
pthombre/partial-ckpt-sandbox-agent
Open

pthombre wants to merge 2 commits into
pthombre/partial-ckpt-sandbox-resourcesfrom
pthombre/partial-ckpt-sandbox-agent

Conversation

@pthombre

@pthombre pthombre commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

An agent harness such as OpenCode, Codex, or Claude Code runs a whole rollout as one process inside a sandbox. A legacy /run agent that wraps it sees no boundary between the harness's start and its end: to Gym the harness is one wait step, and a checkpoint waits for a wait step to finish. Meanwhile the policy model server holds every new model call until resume. So the harness's next model call waits for the checkpoint, the checkpoint waits for the harness, and prepare can only miss its deadline. Marking the step replay does not help: the harness keeps changing the sandbox while its owner snapshots it, and after a crash the step would run again from the start in a sandbox that already holds half its work. Until now such an agent could only be restart-only and lose its rollout on every crash.

This PR adds a park request: a way for a checkpoint to ask a run to stop a long wait step at a safe point. The run stops the harness, records a boundary that says how to continue it, and parks there, so the checkpoint is taken within its deadline. The run continues the harness after the resume, or the replacement attempt does after a crash.

  • The park request. EpisodeSteps raises the park request when a checkpoint closes admission and withdraws it when admission reopens, on resume or when a prepare that missed its deadline reopens it. A run reads it through LegacyRun.park_requested and waits for it with await LegacyRun.wait_park_requested(), which returns at once if the request is already raised. Neither lets a run raise or withdraw the request. A restart run, which a checkpoint never waits for, is not asked to park.
  • What a run does with it. A wait step that may outlast a checkpoint's deadline waits for its work and for the park request together. On a park request it stops the work at a safe point (for a harness, it signals the harness so that it saves its session and exits), leaves the step, and records a boundary that continues the work, for example {"next": "harness", "continue": true, "sandbox_handle": ...}. A boundary recorded while admission is closed parks the run until resume, so the run no longer blocks prepare, and the commit exports that boundary. After resume the same run continues from it; after a crash the replacement attempt receives it as its continuation. Continuing must depend only on what the checkpoint captures: a harness keeps its session on the sandbox filesystem, which the sandbox's owner snapshots at commit, and is relaunched with its own resume option, such as opencode run --continue.
  • A sandbox restored under a new id. A restore forks the owner's snapshot into a new sandbox. A run that continues a harness therefore asks the owner which sandbox is current before it relaunches, through /sandbox_access (feat(sandbox): sandbox and snapshot lifecycle for partial-rollout checkpoints #4220), instead of reusing the handle in its boundary, which names the sandbox the crashed process used.
  • Process-level evidence. fake_harness_agent is a legacy /run agent shaped like the OpenCode agent: seed (replay), harness (wait), verify. Its harness is run notes <n> in the fake sandbox backend, which appends a line every interval and continues from the lines the file already holds, as opencode run --continue resumes a session; interrupt notes stops it, as SIGINT stops OpenCode. The notes resources server from feat(checkpoint): process-level evidence for resources-owned sandbox checkpoints, their release, and lease expiry #4221 owns and checkpoints the sandbox; it now implements current_sandbox_access, and its seed reply to a legacy /run names the sandbox. The e2e deployment gains the harness topology. Scenarios:
    • a harness interrupted by a checkpoint continues after a crash in the fork of the snapshot, and every line is written once;
    • a harness interrupted by a checkpoint continues after the resume in the same sandbox, with no fork, and a later release deletes the snapshot;
    • negative control: a replacement that relaunches with the handle from its boundary instead of asking the owner fails against the superseded sandbox, and the fork never receives the remaining lines.

The OpenCode agent adopts this protocol in #4274.

How it works

Where this PR sits in the overall flow

The highlighted part is what this PR adds.

flowchart LR
  C["Controller<br/>NeMo RL"]
  CO["Coordination<br/>prepare, commit, restore, resume,<br/>retire, release"]
  K["Control plane on every server"]
  subgraph G["One participant per Gym server"]
    E["Environment server<br/>episode steps"]
    M["Policy model<br/>holds new calls until resume"]
    A["Agent<br/>legacy runs: park request,<br/>boundary that continues the harness"]
    R["Resources server<br/>owns the sandbox: snapshot, fork,<br/>/sandbox_access"]
  end
  H["Harness in the sandbox<br/>OpenCode, Codex, Claude Code"]
  D[("Checkpoint directory")]
  C --> CO --> K
  K --> E & M & A & R
  A -- "launch, interrupt, relaunch with continue" --> H
  R -- "snapshot at commit, fork at restore" --> H
  G --> D
  classDef this fill:#fde68a,stroke:#b45309,stroke-width:2px,color:#1f2937
  class A this
Loading

One checkpoint of a harness run

sequenceDiagram
  participant C as Controller
  participant P as Agent participant
  participant R as Agent /run
  participant H as Harness in the sandbox
  participant S as Resources server
  R->>H: launch, inside a wait step
  C->>P: prepare
  Note over P: admission closes, park request raised
  P-->>R: wait_park_requested returns
  R->>H: SIGINT, then SIGKILL after a grace period
  Note over H: saves its session on the sandbox filesystem and exits
  Note over R: leaves the wait step, records boundary next=harness continue=true,<br/>parks there until resume
  P-->>C: prepared
  C->>P: commit
  Note over P: exports the run's boundary
  C->>S: commit
  Note over S: snapshots the sandbox: the repository and the harness session
  alt checkpoint and continue
    C->>P: resume
    Note over P: park request withdrawn, the run wakes
    R->>S: /sandbox_access
    S-->>R: the same sandbox
    R->>H: relaunch with continue
  else crash, then restore
    C->>S: restore: fork the snapshot under a new id
    C->>P: restore the boundary, then resume
    Note over R: the replacement attempt starts at the boundary
    R->>S: /sandbox_access
    S-->>R: the fork
    R->>H: relaunch with continue, in the fork
  end
Loading

The run's harness step

flowchart TD
  W["wait step: the harness runs<br/>blocks prepare"] -->|harness finishes| V["boundary: verify"]
  W -->|park request| I["stop the harness at a safe point"]
  I --> B["boundary: continue the harness"]
  B --> K["parked until resume<br/>prepare ready, exported at commit"]
  K -->|resume| L["ask the owner for the sandbox<br/>relaunch with continue"]
  K -->|crash, restore| F["replacement attempt<br/>ask the owner for the fork<br/>relaunch with continue"]
  L --> W
  F --> W
Loading

Where this sits in the stack

This PR is one of a series that adds sandbox checkpointing on top of the partial-rollout checkpointing stack, whose top is #4197 (ananthsub/partial-ckpt-fault-tests). Each PR is one commit, and its base is the branch of the PR before it.

  1. feat(sandbox): sandbox and snapshot lifecycle for partial-rollout checkpoints #4220 (pthombre/partial-ckpt-sandbox-core): the sandbox session checkpointer, the release protocol, the snapshot index, the OpenSandbox lease, and /sandbox_access
  2. feat(checkpoint): process-level evidence for resources-owned sandbox checkpoints, their release, and lease expiry #4221 (pthombre/partial-ckpt-sandbox-resources): process-level evidence for resources-owned sandbox checkpoints and their release
  3. feat(checkpoint): ask a long-running agent harness to park when a checkpoint opens #4222 (pthombre/partial-ckpt-sandbox-agent): this PR
  4. feat(sandbox): sandbox owner base for resources servers, with SWE-bench on it #4576 (pthombre/partial-ckpt-sandbox-swebench): the sandbox owner base, with the SWE-bench resources server on it
  5. feat(checkpoint): harness driver for sandboxed agents that pauses them at checkpoints, with OpenCode on it #4274 (pthombre/partial-ckpt-sandbox-opencode): the harness driver, with the OpenCode agent on it
  6. fix(checkpoint): leave the calls a checkpoint abandoned out of the capture ledger #4451 (pthombre/partial-ckpt-sandbox-capture): calls a checkpoint abandoned stay out of the capture ledger

Issue

No tracking issue exists for this series; #4220 describes the design.

Validation

Run in the r3Run nemo-rl+Gym container on Slurm against this head, on ananthsub/partial-ckpt-fault-tests 8947bb2ca (job 1289959 suites, job 1289960 e2e):

  • tests/unit_tests/test_checkpoint_agent.py, including:
    • a long wait step blocks prepare, the park request interrupts it, prepare becomes ready, the commit exports the boundary that continues it, and resume withdraws the request;
    • a prepare that misses its deadline withdraws the request when it reopens admission;
    • wait_park_requested returns at once when the request is already raised;
    • a restart run is not asked to park.
  • tests/e2e/checkpoint with NEMO_GYM_CHECKPOINT_E2E=1: 61 passed, 12 skipped (real-model and scale cases), including the three harness scenarios above.
  • tests/unit_tests/test_checkpoint_*.py 783 passed; sandbox 212 passed; Simple Agent 73, rollout collection and the session-state and single-turn examples 515 passed.
  • ruff check and ruff format --check (ruff 0.9.9) clean on the changed files. Every commit carries a Signed-off-by line.

Rollout evidence

  • Process-level: the harness scenarios above, across real Gym processes and the fake sandbox backend, with a crash in between.
  • A real harness: the OpenCode agent in feat(checkpoint): harness driver for sandboxed agents that pauses them at checkpoints, with OpenCode on it #4274 uses this park request. On the cell-5 OpenSandbox cell with SWE-bench Verified rows, a checkpoint interrupted the running OpenCode rollouts, Gym was killed, the restore forked the snapshots, and the unfinished rollout continued in its fork with opencode run --continue, finished, and verified (job 1172174); later runs added release and the lease (jobs 1241245, 1245200). On this series' tip (job 1253315): prepare interrupted the 2 running OpenCode rollouts in 5.6 s with no blockers, the commit snapshotted their sandboxes in 19.4 s, Gym was killed with one rollout still running, the restore took 6.9 s, the replacement continued in the fork with opencode run --continue and verified, and a final release deleted both snapshots from the cell.

Compatibility

  • New API: LegacyRun.park_requested and LegacyRun.wait_park_requested(). An agent that does not use them is unchanged: a checkpoint waits for its wait steps as before.
  • Nothing changes unless the global checkpoint: block sets enabled: true.
  • Test-only additions: the harness e2e topology, fake_harness_agent, the fake backend's run and interrupt commands, and the notes server's current_sandbox_access and legacy seed sandbox_handle.

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Oct 8, 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.

@pthombre

pthombre commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto the current stack base and appended one commit (3329a26): EpisodeSteps.park_requested, an event set while admission is closed, exposed as LegacyRun.park_requested(), so a legacy run whose step is one long external process (a harness in a sandbox) can stop it at a safe point and reach a boundary within the checkpoint's deadline. Used by the OpenCode agent in #4274.

@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 3329a26 to a66405e Compare October 9, 2026 06:17
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from a66405e to 9cbd3b8 Compare October 9, 2026 06:31
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 9cbd3b8 to 2c0013f Compare October 9, 2026 21:52
@pthombre

pthombre commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Rewritten in place (2026-10-09): an explicit snapshot taken at commit is now the only restore point of a checkpointed sandbox. Pause is no longer a restore point: the lazy thaw, resume hooks, restore_point field and listing-based snapshot lookup are gone; a restore always forks the snapshot. A commit the controller stops after carries stop: true and participants park (kill) the exported sandboxes once the write is durable. Episode end deletes the snapshots; a retired attempt keeps them for the retained checkpoints and the sweep reaps the rest. No compatibility with pause-based checkpoint records. Rebased onto the current ananthsub/partial-ckpt-fault-tests. Offline suites green (sandbox 199, checkpoint 608, pilots 69, e2e 65 passed; Slurm job 1167803). Live validation waits on the cell-3 shared snapshot store (fork currently returns SNAPSHOT::NOT_FOUND).

🤖 Generated with Claude Code

@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 2c0013f to 8030978 Compare October 11, 2026 04:43
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 8030978 to 3741380 Compare October 12, 2026 21:53
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 3741380 to 468341d Compare October 12, 2026 21:57
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 468341d to d2da397 Compare October 13, 2026 16:24
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from d2da397 to 9df42ec Compare October 13, 2026 16:27
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 9df42ec to 634fa5b Compare October 13, 2026 20:27
@pthombre pthombre changed the title feat(checkpoint): agents checkpoint the sandboxes they own feat(checkpoint): agents checkpoint the sandboxes they own, and a borrower re-resolves access after a restore Oct 13, 2026
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 63f4dca to 5d2487d Compare October 17, 2026 01:43
@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:agent Agent harnesses and Responses API agent behavior feature New capabilities, enhancements, or enablement work labels Oct 20, 2026
@pthombre
pthombre marked this pull request as ready for review October 20, 2026 17:13
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Oct 20, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the needs-review PR is ready for code review and waiting on a reviewer label Oct 20, 2026
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from 5d2487d to cbdd2c6 Compare October 21, 2026 03:04
pthombre and others added 2 commits October 23, 2026 16:07
…ckpoint opens

An agent harness such as OpenCode runs a whole rollout as one wait step, which a checkpoint would wait for past
any deadline while the policy model server holds the harness's next call until resume. A checkpoint now raises
a park request when it closes admission and withdraws it when admission reopens, read through
`LegacyRun.park_requested` and awaited with `LegacyRun.wait_park_requested()`. On the request the run stops the
harness at a safe point and records a boundary that continues it, which parks the run until resume; the run
continues from it after the resume, or the replacement attempt after a crash, in the sandbox the owner names
through `/sandbox_access`. A restart is never asked to park.

Process-level evidence: an OpenCode-shaped harness agent over the sandboxed notes server is interrupted by a
checkpoint and continued after the resume and after a crash, with a negative control that relaunches the harness
in the sandbox its boundary names instead of asking the owner.

Signed-off-by: Pranav Prashant Thombre <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…kpoint

A harness waits for the park request from its launch, so a close must wake a waiter already blocked on it. The
earlier tests closed admission before their waiter first ran, so they only covered a request already raised:
dropping the set() in EpisodeSteps.close() left every test passing. Add that case, a close and reopen that the
waiter must not mistake for a request, and a run interrupted by two checkpoints in turn.

Signed-off-by: Pranav Prashant Thombre <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pthombre
pthombre force-pushed the pthombre/partial-ckpt-sandbox-agent branch from cbdd2c6 to 5a2daaa Compare October 25, 2026 02:47
pthombre added a commit that referenced this pull request Nov 7, 2026
Every protocol change the environment-server checkpointing of sandboxed harnesses needs between the four
server types, reviewed together and tested with fakes:

- Control plane: `release(kept)` admitted only while idle, fanned out to every participant best effort
  (failures returned per participant) and forwarded to every worker; export hooks receive the
  `checkpoint_id` they write (keyword-only); `CheckpointSettings.state_dir` for servers that record
  external state such as snapshots; optional `release_external_state(kept)` hooks. In-tree overrides
  (genrm_compare, gymnasium, workplace_assistant, example_session_state_mgmt, simple_agent,
  tool_simulation_agent) take the new keyword.
- Per-instance checkpoint mode: `checkpoint_mode_in_effect()` on agents, resources and environment
  servers, read by participant setup, status/discovery and seed replies; defaults to the ClassVar.
- Park signal for agent sessions: `Activation.park_requested`, `Activation.wait_park_requested()` (set by
  prepare, at activation entry while closed, and by retire/close; cleared by resume; never for an episode
  that restarts) and `Activation.queued()`, so an activation waiting for the agent's own concurrency limit
  counts as parked.
- Agent base: a replacement attempt's seed of a restored, unclaimed session calls
  `_rebind_restored_agent_session_state` instead of answering 409 (the default still refuses);
  `_seed_restarts(body)` marks a session the agent cannot capture as a restart (header on the seed reply,
  never exported or waited for); `rebinds_tool_accesses` is reported with `x-ng-checkpoint-rebinds-tools`.
- single_agent_turn (and the legacy flat-row server, which inherits it): the bookmark records
  `borrowed_sandbox` and `elapsed_s`; a restore at `invoke_agent` or `close_agent` re-seeds both sessions
  in a replay step (`reseed_after_restore`), so the agent re-binds to the forked sandbox; a replacement
  attempt only has the rest of the episode budget; resources MCP grants force a restart only when the
  agent seed reply does not say it re-binds tool grants.

Provenance: the control-plane part of #4220 and #4222, generalized from LegacyRun to agent sessions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
pthombre added a commit that referenced this pull request Nov 7, 2026
Every protocol change the environment-server checkpointing of sandboxed harnesses needs between the four
server types, reviewed together and tested with fakes:

- Control plane: `release(kept)` admitted only while idle, fanned out to every participant best effort
  (failures returned per participant) and forwarded to every worker; export hooks receive the
  `checkpoint_id` they write (keyword-only); `CheckpointSettings.state_dir` for servers that record
  external state such as snapshots; optional `release_external_state(kept)` hooks. In-tree overrides
  (genrm_compare, gymnasium, workplace_assistant, example_session_state_mgmt, simple_agent,
  tool_simulation_agent) take the new keyword.
- Per-instance checkpoint mode: `checkpoint_mode_in_effect()` on agents, resources and environment
  servers, read by participant setup, status/discovery and seed replies; defaults to the ClassVar.
- Park signal for agent sessions: `Activation.park_requested`, `Activation.wait_park_requested()` (set by
  prepare, at activation entry while closed, and by retire/close; cleared by resume; never for an episode
  that restarts) and `Activation.queued()`, so an activation waiting for the agent's own concurrency limit
  counts as parked.
- Agent base: a replacement attempt's seed of a restored, unclaimed session calls
  `_rebind_restored_agent_session_state` instead of answering 409 (the default still refuses);
  `_seed_restarts(body)` marks a session the agent cannot capture as a restart (header on the seed reply,
  never exported or waited for); `rebinds_tool_accesses` is reported with `x-ng-checkpoint-rebinds-tools`.
- single_agent_turn (and the legacy flat-row server, which inherits it): the bookmark records
  `borrowed_sandbox` and `elapsed_s`; a restore at `invoke_agent` or `close_agent` re-seeds both sessions
  in a replay step (`reseed_after_restore`), so the agent re-binds to the forked sandbox; a replacement
  attempt only has the rest of the episode budget; resources MCP grants force a restart only when the
  agent seed reply does not say it re-binds tool grants.

Provenance: the control-plane part of #4220 and #4222, generalized from LegacyRun to agent sessions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
pthombre added a commit that referenced this pull request Nov 9, 2026
Every protocol change the environment-server checkpointing of sandboxed harnesses needs between the four
server types, reviewed together and tested with fakes:

- Control plane: `release(kept)` admitted only while idle, fanned out to every participant best effort
  (failures returned per participant) and forwarded to every worker; export hooks receive the
  `checkpoint_id` they write (keyword-only); `CheckpointSettings.state_dir` for servers that record
  external state such as snapshots; optional `release_external_state(kept)` hooks. In-tree overrides
  (genrm_compare, gymnasium, workplace_assistant, example_session_state_mgmt, simple_agent,
  tool_simulation_agent) take the new keyword.
- Per-instance checkpoint mode: `checkpoint_mode_in_effect()` on agents, resources and environment
  servers, read by participant setup, status/discovery and seed replies; defaults to the ClassVar.
- Park signal for agent sessions: `Activation.park_requested`, `Activation.wait_park_requested()` (set by
  prepare, at activation entry while closed, and by retire/close; cleared by resume; never for an episode
  that restarts) and `Activation.queued()`, so an activation waiting for the agent's own concurrency limit
  counts as parked.
- Agent base: a replacement attempt's seed of a restored, unclaimed session calls
  `_rebind_restored_agent_session_state` instead of answering 409 (the default still refuses);
  `_seed_restarts(body)` marks a session the agent cannot capture as a restart (header on the seed reply,
  never exported or waited for); `rebinds_tool_accesses` is reported with `x-ng-checkpoint-rebinds-tools`.
- single_agent_turn (and the legacy flat-row server, which inherits it): the bookmark records
  `borrowed_sandbox` and `elapsed_s`; a restore at `invoke_agent` or `close_agent` re-seeds both sessions
  in a replay step (`reseed_after_restore`), so the agent re-binds to the forked sandbox; a replacement
  attempt only has the rest of the episode budget; resources MCP grants force a restart only when the
  agent seed reply does not say it re-binds tool grants.

Provenance: the control-plane part of #4220 and #4222, generalized from LegacyRun to agent sessions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
pthombre added a commit that referenced this pull request Nov 9, 2026
Every protocol change the environment-server checkpointing of sandboxed harnesses needs between the four
server types, reviewed together and tested with fakes:

- Control plane: `release(kept)` admitted only while idle, fanned out to every participant best effort
  (failures returned per participant) and forwarded to every worker; export hooks receive the
  `checkpoint_id` they write (keyword-only); `CheckpointSettings.state_dir` for servers that record
  external state such as snapshots; optional `release_external_state(kept)` hooks. In-tree overrides
  (genrm_compare, gymnasium, workplace_assistant, example_session_state_mgmt, simple_agent,
  tool_simulation_agent) take the new keyword.
- Per-instance checkpoint mode: `checkpoint_mode_in_effect()` on agents, resources and environment
  servers, read by participant setup, status/discovery and seed replies; defaults to the ClassVar.
- Park signal for agent sessions: `Activation.park_requested`, `Activation.wait_park_requested()` (set by
  prepare, at activation entry while closed, and by retire/close; cleared by resume; never for an episode
  that restarts) and `Activation.queued()`, so an activation waiting for the agent's own concurrency limit
  counts as parked.
- Agent base: a replacement attempt's seed of a restored, unclaimed session calls
  `_rebind_restored_agent_session_state` instead of answering 409 (the default still refuses);
  `_seed_restarts(body)` marks a session the agent cannot capture as a restart (header on the seed reply,
  never exported or waited for); `rebinds_tool_accesses` is reported with `x-ng-checkpoint-rebinds-tools`.
- single_agent_turn (and the legacy flat-row server, which inherits it): the bookmark records
  `borrowed_sandbox` and `elapsed_s`; a restore at `invoke_agent` or `close_agent` re-seeds both sessions
  in a replay step (`reseed_after_restore`), so the agent re-binds to the forked sandbox; a replacement
  attempt only has the rest of the episode budget; resources MCP grants force a restart only when the
  agent seed reply does not say it re-binds tool grants.

Provenance: the control-plane part of #4220 and #4222, generalized from LegacyRun to agent sessions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
pthombre added a commit that referenced this pull request Nov 12, 2026
Every protocol change the environment-server checkpointing of sandboxed harnesses needs between the four
server types, reviewed together and tested with fakes:

- Control plane: `release(kept)` admitted only while idle, fanned out to every participant best effort
  (failures returned per participant) and forwarded to every worker; export hooks receive the
  `checkpoint_id` they write (keyword-only); `CheckpointSettings.state_dir` for servers that record
  external state such as snapshots; optional `release_external_state(kept)` hooks. In-tree overrides
  (genrm_compare, gymnasium, workplace_assistant, example_session_state_mgmt, simple_agent,
  tool_simulation_agent) take the new keyword.
- Per-instance checkpoint mode: `checkpoint_mode_in_effect()` on agents, resources and environment
  servers, read by participant setup, status/discovery and seed replies; defaults to the ClassVar.
- Park signal for agent sessions: `Activation.park_requested`, `Activation.wait_park_requested()` (set by
  prepare, at activation entry while closed, and by retire/close; cleared by resume; never for an episode
  that restarts) and `Activation.queued()`, so an activation waiting for the agent's own concurrency limit
  counts as parked.
- Agent base: a replacement attempt's seed of a restored, unclaimed session calls
  `_rebind_restored_agent_session_state` instead of answering 409 (the default still refuses);
  `_seed_restarts(body)` marks a session the agent cannot capture as a restart (header on the seed reply,
  never exported or waited for); `rebinds_tool_accesses` is reported with `x-ng-checkpoint-rebinds-tools`.
- single_agent_turn (and the legacy flat-row server, which inherits it): the bookmark records
  `borrowed_sandbox` and `elapsed_s`; a restore at `invoke_agent` or `close_agent` re-seeds both sessions
  in a replay step (`reseed_after_restore`), so the agent re-binds to the forked sandbox; a replacement
  attempt only has the rest of the episode budget; resources MCP grants force a restart only when the
  agent seed reply does not say it re-binds tool grants.

Provenance: the control-plane part of #4220 and #4222, generalized from LegacyRun to agent sessions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
paul-aiyedun pushed a commit to paul-aiyedun/NemoGym that referenced this pull request Nov 12, 2026
…A-NeMo#5264)

* feat(checkpoint): protocol for harnesses that borrow a sandbox

Every protocol change the environment-server checkpointing of sandboxed harnesses needs between the four
server types, reviewed together and tested with fakes:

- Control plane: `release(kept)` admitted only while idle, fanned out to every participant best effort
  (failures returned per participant) and forwarded to every worker; export hooks receive the
  `checkpoint_id` they write (keyword-only); `CheckpointSettings.state_dir` for servers that record
  external state such as snapshots; optional `release_external_state(kept)` hooks. In-tree overrides
  (genrm_compare, gymnasium, workplace_assistant, example_session_state_mgmt, simple_agent,
  tool_simulation_agent) take the new keyword.
- Per-instance checkpoint mode: `checkpoint_mode_in_effect()` on agents, resources and environment
  servers, read by participant setup, status/discovery and seed replies; defaults to the ClassVar.
- Park signal for agent sessions: `Activation.park_requested`, `Activation.wait_park_requested()` (set by
  prepare, at activation entry while closed, and by retire/close; cleared by resume; never for an episode
  that restarts) and `Activation.queued()`, so an activation waiting for the agent's own concurrency limit
  counts as parked.
- Agent base: a replacement attempt's seed of a restored, unclaimed session calls
  `_rebind_restored_agent_session_state` instead of answering 409 (the default still refuses);
  `_seed_restarts(body)` marks a session the agent cannot capture as a restart (header on the seed reply,
  never exported or waited for); `rebinds_tool_accesses` is reported with `x-ng-checkpoint-rebinds-tools`.
- single_agent_turn (and the legacy flat-row server, which inherits it): the bookmark records
  `borrowed_sandbox` and `elapsed_s`; a restore at `invoke_agent` or `close_agent` re-seeds both sessions
  in a replay step (`reseed_after_restore`), so the agent re-binds to the forked sandbox; a replacement
  attempt only has the rest of the episode budget; resources MCP grants force a restart only when the
  agent seed reply does not say it re-binds tool grants.

Provenance: the control-plane part of NVIDIA-NeMo#4220 and NVIDIA-NeMo#4222, generalized from LegacyRun to agent sessions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>

* feat(checkpoint): report a rollout's first seed in flight as a restart instead of waiting for it

A rollout checkpoint lands right after a training step dispatches its rollouts, while hundreds of first seeds are
still running: the resources server creating each sandbox and the agent setting its harness up in it. Prepare
waited for every one, 70-80 s at 16 nodes, while every rollout already running sat parked. Such a seed has done no
work of its episode worth saving.

When admission closes, the agent and resources participants now record the rollouts whose first seed is still in
flight and report their episodes as restarts of that checkpoint: prepare does not wait for them, and the commit leaves
them out, including the sessions they register before resume. After a crash those rollouts start over from their
input; the live run is unchanged, and after resume the next checkpoint captures them like any other. A seed of a
session that exists, such as one re-binding a restored session to its rebuilt sandbox, still blocks prepare, so
restored progress stays in the checkpoint.

At 16 nodes on AgentENV (runs 8639483 and 8641901 against 8634011), steady-state checkpoints park rollouts for
7-36 s instead of 79-107 s, and leave 200-370 fewer sandboxes to snapshot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>

* fix(checkpoint): address the protocol review: restart parks, slots, budgets, stale releases, startup checks

- A restart's activation no longer parks when it gets a slot (`queued()`) or a policy reply (`awaiting_model()`)
  while a checkpoint is open: nothing of it is in the checkpoint, the rule `park_requested` already applies.
- `Activation.slot(semaphore)` takes one of the agent's concurrency slots in the queued scope and gives it back on
  every path, including a retire or close while the activation is parked holding it.
- An activation keeps the restored boundary as the session's latest until it records its own, so a checkpoint taken
  while it waits before its first boundary exports it again instead of nothing.
- The elapsed time budget is sampled when a checkpoint exports an episode (`EpisodeRecord.elapsed_s`) and charged when
  its replacement begins, so an agent step in flight at the cut counts in full. The per-environment
  `checkpoint_elapsed` / `checkpoint_charge_elapsed` helpers and `SessionHandles.elapsed_s` are gone, and so is the
  environment server's `checkpoint_mode_in_effect`, which nothing overrode.
- An agent that grants MCP tools and does not declare `rebinds_tool_accesses` reports the restart on its own seed
  reply, before its session exists. single_agent_turn makes an episode that grants MCP tools without lending a sandbox
  a restart before seeding the agent, since only a borrowed sandbox's re-seed carries the replacement's token; its two
  checks of the agent's seed reply are one.
- Prepare carries an earlier stage's restarts before closing admission, so no worker wakes a restart's harness first.
- A release for a checkpoint older than the latest one a participant resumed is refused as stale.
- Startup refuses an export hook that cannot take `checkpoint_id` and an agent that declares
  `rebinds_tool_accesses` without overriding the re-bind hook.
- Docs: the `release_external_state` contract on both hooks, the MCP and re-seed behaviour in the single_agent_turn
  README, and `release` among the traced checkpoint operations.

Tests cover each, plus the gaps the review found: the borrowed-sandbox flag recorded by a real seed and a checkpoint
not waiting for the re-seed, the real resources release by mode and the export's checkpoint ID (one and two workers),
the retire and close wake-up of a park wait in a task of its own, and the default re-bind gate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>

* fix(checkpoint): address the second protocol review: release contract, stale releases, restart rules, names

- `release` returns only once the external state is deleted, under the control lock and the request deadline. The
  participant docstring no longer allows returning before.
- A release is stale once the participant committed or restored a checkpoint after the one it names. A checkpoint
  aborted there no longer counts, so the release that ends a run is admitted after an abort. Once a process holds a
  checkpoint, a release that names one it never resumed is stale too, since it dates from before a restart. A
  release names the latest checkpoint the controller resumed.
- Status and discovery report `keeps_external_state`, so a controller that never releases can refuse to run or warn.
- A session granted MCP tools the agent cannot re-bind, or lent a sandbox the agent cannot re-bind, restarts
  whatever `_seed_restarts` returns: the hook only adds restarts, and its default adds none.
- With several workers a release fails unless every expected worker answered.
- The participants check that the export hook takes `checkpoint_id` when they are built, so code that builds one
  around its own hooks learns it at startup too.
- `EpisodeRecord` leaves `elapsed_s` out when nothing was spent, so a Gym from before the field restores the record.
- Names: the hook is `delete_external_state` and the docstrings say delete; the participants' first-seed restarts
  are `_first_seed_restarts`; the header is `x-ng-checkpoint-rebinds-tool-accesses` and its reader
  `seed_rebinds_tool_accesses`; "the cut" no longer names the moment a checkpoint exports an episode.
- Docs: `release` is listed among the control operations in the metrics page and the telemetry docstrings.

Tests cover each, plus the resources participant gaps the review found: a release between a restore and its
resume, resume clearing the first-seed restarts, and a seed that claims a restored session blocking prepare.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>

* test(checkpoint): seed without a lent sandbox where an agent that does not re-bind exports the session

An agent that does not re-bind restored sessions now restarts a session lent a sandbox, so the tests of its
restored sessions and of its MCP restart seed a session without one. The test of a seed that claims a restored
session reads the phase of the prepare it holds up, and the test of an overriding restart hook seeds its second
session on its own client.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>

* test(checkpoint): a server with several workers reports keeps_external_state, and discover carries it

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>

---------

Signed-off-by: Pranav Thombre <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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 feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer 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.

2 participants