> This page is for version 0.5.1.
> For other versions, use one of these documentation indexes:
> - Main (default): https://docs.nvidia.com/nemo/gym/main/llms.txt
> - 0.7.0: https://docs.nvidia.com/nemo/gym/v0.7.0/llms.txt
> - 0.6.0: https://docs.nvidia.com/nemo/gym/v0.6.0/llms.txt
> - 0.5.1: https://docs.nvidia.com/nemo/gym/v0.5.1/llms.txt
> - 0.5.0: https://docs.nvidia.com/nemo/gym/v0.5.0/llms.txt
> - 0.4.0: https://docs.nvidia.com/nemo/gym/v0.4.0/llms.txt
> - 0.3.0: https://docs.nvidia.com/nemo/gym/v0.3.0/llms.txt
> - 0.2.1: https://docs.nvidia.com/nemo/gym/v0.2.1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/gym/llms.txt.

# nemo_gym.server_runtime.sandbox_borrow

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

| Name                                                                                   | Description                                                                                           |
| -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [`BorrowedSandbox`](#nemo_gym-server_runtime-sandbox_borrow-BorrowedSandbox)           | A lent sandbox: commands, terminals and file transfers work, the owner's lifecycle operations refuse. |
| [`BorrowedSandboxError`](#nemo_gym-server_runtime-sandbox_borrow-BorrowedSandboxError) | A borrower tried what only the sandbox's owner does: start, stop, pause, resume or snapshot it.       |
| [`_BorrowedPty`](#nemo_gym-server_runtime-sandbox_borrow-_BorrowedPty)                 | Terminal sessions of a borrowed sandbox, with the access's `env` under each session's own.            |

### Functions

| Name                                                       | Description                                             |
| ---------------------------------------------------------- | ------------------------------------------------------- |
| [`borrow`](#nemo_gym-server_runtime-sandbox_borrow-borrow) | Connect to the sandbox `access` lends, as its borrower. |

### Data

[`LOGGER`](#nemo_gym-server_runtime-sandbox_borrow-LOGGER)

### API

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

**Bases:** [AsyncSandbox](/nemo/gym/nemo-gym/nemo_gym/sandbox/api#nemo_gym-sandbox-api-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)`

---

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.__aexit__(
    exc_type: typing.Any,
    exc_val: typing.Any,
    exc_tb: typing.Any
) -> None
```

async

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox._refuse(
    operation: str
) -> typing.NoReturn
```

```python
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.

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.exec(
    command: str | collections.abc.Sequence[str],
    env: dict[str, str] | None = None,
    kwargs: typing.Any = {}
) -> nemo_gym.sandbox.providers.base.SandboxExecResult
```

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.

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.pause() -> None
```

async

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

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.resume() -> None
```

async

Refuse: resuming is the owner's.

```python
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.

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.snapshot(
    name: str | None = None
) -> str
```

async

Refuse: the owner snapshots the sandbox at a checkpoint.

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.start(
    args: typing.Any = (),
    kwargs: typing.Any = {}
) -> nemo_gym.sandbox.api.AsyncSandbox
```

async

Refuse: a borrower never creates a sandbox.

```python
nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox.start_with_setup(
    args: typing.Any = (),
    kwargs: typing.Any = {}
) -> nemo_gym.sandbox.api.AsyncSandbox
```

async

Refuse: a borrower never creates a sandbox.

```python
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`.

```python
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.

```python
class nemo_gym.server_runtime.sandbox_borrow._BorrowedPty(
    sandbox: nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox
)
```

**Bases:** [SandboxPty](/nemo/gym/nemo-gym/nemo_gym/sandbox/api#nemo_gym-sandbox-api-SandboxPty)

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

```python
nemo_gym.server_runtime.sandbox_borrow._BorrowedPty.create(
    command: str | None = None,
    env: dict[str, str] | None = None,
    kwargs: typing.Any = {}
) -> nemo_gym.sandbox.providers.base.SandboxPtySession
```

async

```python
nemo_gym.server_runtime.sandbox_borrow.borrow(
    access: nemo_gym.sandbox.access.SandboxAccess,
    named_configs: collections.abc.Mapping[str, typing.Any] | None = None
) -> nemo_gym.server_runtime.sandbox_borrow.BorrowedSandbox
```

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.

```python
nemo_gym.server_runtime.sandbox_borrow.LOGGER = logging.getLogger(__name__)
```