Skip to navigation

nemo_gym.server_runtime.sandbox_borrow

View as MarkdownOpen in Claude

How a borrower uses a sandbox another server lends it: borrow.

Every sandbox has one owner, the server that started it, and only the owner stops it (see “Sandbox owner” in the glossary). A borrower, an agent whose seed carries a sandbox_access or a Resources Server grading in the sandbox, runs commands and transfers files in it, and releases its client when it is done. Lent access is always operate scope.

No provider enforces scope yet, so the refusals are advisory: code that connects to a sandbox some other way, or calls the raw provider a borrowed sandbox still exposes as .provider (its close, pause and snapshot), can still stop it. Enforcement waits on provider leases; until then every borrower should connect through borrow. Agents that call AsyncSandbox.connect themselves move onto it one by one, and a repository check that keeps connections inside borrow and OwnedSandboxes follows once they have.

Module Contents

Classes

NameDescription
BorrowedSandboxA lent sandbox: commands, terminals and file transfers work, the owner’s lifecycle operations refuse.
BorrowedSandboxErrorA borrower tried what only the sandbox’s owner does: start, stop, pause, resume or snapshot it.
_BorrowedPtyTerminal sessions of a borrowed sandbox, with the access’s env under each session’s own.

Functions

NameDescription
borrowConnect to the sandbox access lends, as its borrower.

Data

LOGGER

API

class nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox(
args: typing.Any = (),
kwargs: typing.Any = {}
)

Bases: AsyncSandbox

A lent sandbox: commands, terminals and file transfers work, the owner’s lifecycle operations refuse.

Commands and terminal sessions run as the access’s user in its workdir, with its env under each call’s own env. That includes a harness’s own setup, such as installing its runtime: a command that must run as another user names it. stop, start, pause, resume and snapshot raise BorrowedSandboxError. disconnect releases the client, shielded from cancellation and bounded as AsyncSandbox.disconnect is, and leaving an async with block disconnects instead of stopping. Sidecar commands run in another container and take none of the access’s settings.

_access_env
dict[str, str] = {}
_lent
SandboxAccess | None = None
pty
= _BorrowedPty(self)
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.__aexit__(
exc_type: typing.Any,
exc_val: typing.Any,
exc_tb: typing.Any
) -> None
async
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox._refuse(
operation: str
) -> typing.NoReturn
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.access() -> nemo_gym.sandbox.access.SandboxAccess
async

The access this sandbox was lent with, for a harness that borrows it again, for example in a subprocess.

That borrower calls borrow with it, and with named_configs holding the provider block that access.connection.provider_config_ref names, since a subprocess has no global config.

Raises:

  • RuntimeError: For a sandbox that borrow did not build.
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.exec(
command: str | collections.abc.Sequence[str],
env: dict[str, str] | None = None,
kwargs: typing.Any = {}
async

Run command as AsyncSandbox.exec does, with the access’s env under env.

The access’s user is passed explicitly when the call names none, since a command that keeps background services runs through a provider call that does not read the sandbox’s default user.

nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.pause() -> None
async

Refuse: pausing is the owner’s, for example at a checkpoint.

nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.resume() -> None
async

Refuse: resuming is the owner’s.

nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.serialize(
scope: str | None = None
) -> dict[str, typing.Any]
async

A descriptor with operate scope; access wraps it for a harness that borrows it elsewhere.

Raises:

  • ValueError: If scope asks for more than operate scope.
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.snapshot(
name: str | None = None
) -> str
async

Refuse: the owner snapshots the sandbox at a checkpoint.

nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.start(
args: typing.Any = (),
kwargs: typing.Any = {}
async

Refuse: a borrower never creates a sandbox.

nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.start_with_setup(
args: typing.Any = (),
kwargs: typing.Any = {}
async

Refuse: a borrower never creates a sandbox.

nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.stop(
timeout_s: float | None = STOP_TIMEOUT_S
) -> None
async

Refuse: the owner stops the sandbox. Release this client with disconnect.

class nemo_gym.server_runtime.sandbox_borrow.BorrowedSandboxError()

Bases: RuntimeError

A borrower tried what only the sandbox’s owner does: start, stop, pause, resume or snapshot it.

class nemo_gym.server_runtime.sandbox_borrow._BorrowedPty(
)

Bases: SandboxPty

Terminal sessions of a borrowed sandbox, with the access’s env under each session’s own.

nemo_gym.server_runtime.sandbox_borrow._BorrowedPty.create(
command: str | None = None,
env: dict[str, str] | None = None,
kwargs: typing.Any = {}
async
nemo_gym.server_runtime.sandbox_borrow.borrow(
named_configs: collections.abc.Mapping[str, typing.Any] | None = None
async

Connect to the sandbox access lends, as its borrower.

The provider is resolved from access.connection.provider_config_ref in named_configs, the merged global config by default. The returned sandbox runs commands as access.user in access.workdir with access.env, and its client is released with BorrowedSandbox.disconnect. A connect that fails or is cancelled closes the client it opened, shielded from the cancellation, and raises the connect’s own error.

Raises:

  • ValueError: If the provider config cannot be resolved.
  • RuntimeError: If the provider cannot reconnect to a sandbox.
nemo_gym.server_runtime.sandbox_borrow.LOGGER = logging.getLogger(__name__)