Skip to content

Sandbox server to share sandbox states needed for rollouts #2082

Open

Description

@ananthsub

Purpose

A sandbox is a resource that runs outside the Gym process: a container, a VM, or a remote service. Inside the Gym process is the handle to it, a SandboxHandle. Today that handle only works in the process that created the sandbox, so even though the box itself runs separately, only that one process can operate it. When scoring happens in a different process, it has no way to reach the box.

The sandbox server removes that limit. One long-lived server process holds the handle and exposes the sandbox's operations over HTTP, so any Gym server can operate the same box, and the box can outlive the single call that created it. The other servers hold only a small, serializable reference (a SandboxRef) that travels in a normal request body, for example from an agent server that runs a rollout to the resources server that scores it.

Requirements: what verification must be able to do

A rollout runs in a sandbox and produces a result. A resources server then scores it. There are four relationships between the box the rollout used and how the verifier gets what it needs:

No verifier box. The verifier scores a copied-out file or the model's response. No sharing needed.
Fresh box from an artifact. The rollout copies out an artifact such as a patch. The verifier creates its own clean box, applies the artifact, and runs a test. Example: SWE-bench.
Same box. The verifier operates the box the rollout used and runs its own checks. Example: terminal-bench.
Fresh box from the response. The verifier creates its own box and runs code taken from the model's response. Example: code generation.

Today the only thing a rollout can give the verifier is copied-out results: it reads a fixed set of files and runs a fixed set of commands in its own box, and sends the bytes to the verifier. That covers case 1. It cannot cover the other three, for two reasons:

In cases 2 and 4, the verifier has to run scoring itself in a clean box. Copied-out results are only bytes; the verifier has nowhere to apply a patch or run response code, and the rollout's own box is already modified and may expose the hidden tests.
In case 3 the verifier has to run its own checks against the box the rollout used, such as whether a service is listening or a file landed in the right place. These checks can be secret or depend on what the verifier finds, so the rollout cannot run them in advance, and results the rollout reports about itself cannot be trusted.

Cases 2, 3, and 4 all require the verifier to hold a working sandbox: either the box the rollout used, or a fresh box the verifier creates.

Background: Why the in-process API can't do this

AsyncSandbox wraps a provider, and provider.create(spec) returns a SandboxHandle whose raw field is provider state that only works in the process that made it: a live SDK client (OpenSandbox, Fargate), a container the local Docker daemon knows about (Docker), or a subprocess (Apptainer, enroot). Another process cannot operate a box from that handle.

What can travel is a descriptor: a sandbox id plus enough information to reach the backend that owns the box. Whether a descriptor is enough to rebuild a working handle elsewhere depends on the provider.

External control plane (OpenSandbox, ECS Fargate): The box lives in a service reachable over the network by id, so any process with the id and credentials can reconnect. The OpenSandbox provider already does this internally after create, calling Sandbox.connect(id). The backend also enforces its own timeout and quota.

Node-local (Docker, Apptainer, enroot, local subprocess): The box is tied to one daemon or one node and has no network identity of its own. Only a process on that node can operate it, and nothing outside the creating process reaps it or limits how many exist.

Proposal

New provider capability: ConnectableProvider

The base SandboxProvider protocol (nemo_gym/sandbox/providers/base.py) is unchanged: every provider still has create, exec, upload_file, download_file, status, close, and aclose. Reattach is added as a separate, optional protocol in the same file, so the base interface does not fill up with methods most providers cannot implement:

@runtime_checkable
class ConnectableProvider(Protocol):
    # live handle -> JSON descriptor; `scope` is used by providers that mint
    # leases (the remote provider) and ignored by the rest (e.g. OpenSandbox).
    async def serialize_handle(self, handle: SandboxHandle, *, scope: str | None = None) -> dict: ...
    # JSON descriptor -> live handle, rebuilt in this process.
    async def connect(self, descriptor: Mapping[str, Any]) -> SandboxHandle: ...
  • A consumer tests membership with isinstance(provider, ConnectableProvider). Future capabilities (snapshots, provider-side pools) each get their own protocol the same way. This protocol is explicit to avoid getattr checks, which are neither discoverable nor type-checked.
  • This protocol can be Implemented by RemoteSandboxProvider (the sandbox server client) and OpenSandboxProvider (direct reattach through Sandbox.connect(id)). Docker, Apptainer, and enroot do not implement it, so they can only be shared by fronting them with a sandbox server.

Sandbox facade: serialize and connect

AsyncSandbox (nemo_gym/sandbox/api.py) gains two methods that delegate to the capability:

async def serialize(self, *, scope: str | None = None) -> dict:
    # raises unless the provider is a ConnectableProvider; returns a descriptor
    ...

@classmethod
async def connect(cls, descriptor: Mapping[str, Any], *, provider: SandboxProvider) -> "AsyncSandbox":
    # raises unless `provider` is a ConnectableProvider; rebuilds a started sandbox
    ...
  • For the sandbox server case, callers do not build the remote provider by hand. nemo_gym.sandbox_client provides make_remote_provider(server_url) and connect_sandbox(ref); the latter reads ref.server_url, builds the remote provider with Gym's injected transport, and calls AsyncSandbox.connect.

Mapping onto the sandbox server HTTP API

Facade call | Provider method | Sandbox server route
AsyncSandbox(make_remote_provider(url), spec).start() | create | POST /sandboxes returns an owner SandboxRef
exec / upload / download / status | exec / upload_file / download_file / status | POST .../exec, POST .../upload, GET .../download, GET .../status
serialize() (owner, no scope change) | serialize_handle | none; returns the ref the handle already holds

For a directly-reattachable provider such as OpenSandbox, the same serialize/connect calls use the provider SDK instead of a sandbox server: serialize_handle returns {"sandbox_id": id}, and connect calls Sandbox.connect(id) with the connecting process's own connection config. No sandbox server routes are involved, and there is no extra network hop.

Which shape you get follows from the provider, not a per-call flag

  • If the provider can reattach on its own (external control plane), serialize() returns a descriptor the verifier passes to connect() directly. No sandbox server, and no extra network hop.
  • If it cannot (node-local), point the provider at a sandbox server. The server holds the live handle and returns a SandboxRef that any process can connect() to over HTTP. The sandbox server is itself a provider whose connect always works, which is how it covers the node-local case.

So the only choice a user makes is which provider to use, which they already make. Whether a sandbox server has to be running is a consequence of that choice: required for node-local providers, optional for the rest.

Client SDK

Operating a sandbox is the same AsyncSandbox API whether the box is in-process or server-owned: exec, upload, download, status, stop. Sharing adds serialize(scope=...) to get a descriptor and the classmethod connect(descriptor) to rebuild a handle from one.

Callers do not build RemoteSandboxProvider directly. They use two helpers from nemo_gym.sandbox_client, which wire in Gym's HTTP client:

from nemo_gym.sandbox_client import make_remote_provider, attach_sandbox

# Agent: create a server-owned box, run the harness, then share a co-lease.
sandbox = await AsyncSandbox(make_remote_provider(sandbox_server_url), spec).start()
ref = await sandbox.grant(scope="operate")   # pass ref to the verifier in /verify

# Verifier: attach to the same box, or create its own clean box.
box_a = await attach_sandbox(ref)
await box_a.exec("curl -s localhost:8080/health")

box_b = await AsyncSandbox(make_remote_provider(sandbox_server_url), eval_spec).start()
await box_b.upload(patch_path, "/testbed/fix.patch")
await box_b.exec("git -C /testbed apply fix.patch && pytest")

For a provider that can reattach on its own, the same calling code works with no sandbox server: build the provider directly, serialize() returns that provider's descriptor, and connect_sandbox() rebuilds it with no HTTP hop.

Ownership, leases, and authorization

Ownership is split and declared:

The server that creates a sandbox holds the owner lease and may close it. In a rollout that is the agent.
A server that attaches or is granted a co-lease holds an operate lease. It may exec, upload, and download, but not close. Closing an operate lease releases the co-lease; the box lives until the owner closes it or the TTL expires.
ttl_s is the backstop. The server reaps any sandbox past its deadline regardless of leases, so a crashed agent cannot strand a box.

The lease token is signed by the server and carries the sandbox id, the rollout id, and the scope. It is checked on every request: the signature must be valid, the sandbox id must match the path, the rollout id must match the sandbox, and DELETE requires an owner scope.

Binding to the rollout id means a ref leaked from one rollout cannot operate another rollout's box; the server refuses to create a lease whose rollout id does not match the sandbox.

SandboxRef

@dataclass(frozen=True)
class SandboxRef:
    server_url: str            # the owning sandbox server
    sandbox_id: str            # provider-neutral id
    lease_token: str           # signed {sandbox_id, rollout_id, scope}
    provider_name: str = ""    # informational
    scope: str = "operate"     # "owner" | "operate"
    workdir: str | None = None # the working directory, so a verifier lands in the right place

SandboxRef is what serialize() returns for a server-owned box, and what connect_sandbox() takes. It holds no live state, so it is plain JSON and goes straight into a verify payload. A directly-reattachable provider returns its own descriptor shape from serialize() instead, and connect() consumes either the same way.

Configuration

The sandbox server is composed like any other server and referenced by name. The external-harness agent gains one optional field, settings.sandbox_server: the name of a sandbox server to create the box through. When it is set, the agent creates the box on that server and passes a ref and the server URL to /verify. When it is unset, the agent uses an in-process provider and harvests, as before.

pool_sandbox_server:
  sandbox_servers:
    sandbox_server:
      entrypoint: app.py
      sandbox_provider:
        docker:
          create:
            network: host
      max_concurrent: 16
      default_ttl_s: 1800

swe_agent:
  responses_api_agents:
    external_harness:
      resources_server: { type: resources_servers, name: swe_env }
      model_server: { type: responses_api_models, name: policy_model }
      settings:
        harness: claude_code
        builder: prefix_merging
        sandbox_server: pool_sandbox_server
        sandbox_spec: { image: <agent image> }

A Gym server that wants to own or share a box takes the sandbox server's name in its own config, resolves the server's URL from the Gym server registry, and builds a provider with make_remote_provider(url), or reattaches to an existing box with connect_sandbox(ref).

Whether a given agent or resources server offers such a field, and how it decides between copying results out and sharing a box, is that server's own concern and is out of scope here. The sandbox server does not know which server type calls it; it only checks the lease.

Activity

  1. self-assigned this
    on Jul 20, 2026
  2. added
    waiting-on-maintainers
    on Jul 23, 2026
  3. moved this from Dev WIP to Review WIP in NeMo Gym 0.5.0 - Aug 5on Jul 28, 2026
  4. 5 remaining items

  5. added a commit that references this issue on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Platformwaiting-on-maintainers

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions