# Sandbox

> Sandboxes: a short-lived Linux box to run code in.

```python
with noclick.Sandbox.create(image="python", timeout_s=3600) as sb:
    sb.files.write("/tmp/in.csv", csv_bytes)
    print(sb.exec("python -c 'print(1 + 1)'").stdout)
    build = sb.exec("make all", background=True, timeout_s=1800)   # an Operation
    print(build.wait().exit_code)
# terminated on exit
```

## noclick.Sandbox

```python
class Sandbox
```

A running sandbox. `with noclick.Sandbox.create() as sb:` terminates it on exit;
otherwise it stops at `expires_at`. Billed per second while it runs, at its
provider's price for the `cpu` and `memory_mb` it reserves.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `str` |  |
| `status` | `str | None` |  |
| `image` | `str | None` |  |
| `timeout_s` | `int | None` |  |
| `cpu` | `float | None` |  |
| `memory_mb` | `int | None` |  |
| `region` | `str | None` | The region it runs in (`noclick.regions()`), or `None`: wherever NoClick had capacity. |
| `user_id` | `str | None` |  |
| `expires_at` | `Any` |  |
| `created_at` | `Any` |  |
| `volumes` | `dict[str, str] | None` | The volumes it mounts, by mount path: `{path: volume id}`. |
| `secret_ids` | `list[str] | None` | The secrets in its environment, in order (a later one's key wins). |
| `network` | `Any` | What it may reach: `False` (nothing), `True` (anywhere) or the hosts it may reach, as sent. |
| `files` | `SandboxFiles` |  |

### Sandbox.create

```python
noclick.Sandbox.create(
    image=None, *, timeout_s=None, cpu=None, memory_mb=None, network=None, region=None,
    volumes=None, secrets=None, user=..., client=None
) -> Sandbox
```

Start a sandbox (`image` `"python"` or `"node"`) that lives
at most `timeout_s` seconds. `network=True` allows outbound
internet access, which is off by default. A list of up to 50 hosts allows
only those, over HTTPS on port 443: `"pypi.org"`, or `"*.example.com"` for a
domain and its subdomains (a scheme, path, port or IP address raises
`noclick.InvalidRequest`).

`region` is where it runs (`noclick.regions()`): `region=agent.region`
puts it beside an agent. Without one it runs in your account's default
region, else wherever NoClick has capacity. Pinned to a region it costs what the
provider charges there: 1.15x in `eu` and `ap`, 1.75x in `us-east` and `us-west`.

`volumes` mounts `Volume`s (or their ids or names) read-write at
absolute paths, `{"/data": vol}`, up to 5: the sandbox sees each as it
was when it started, and what it writes there is saved every few seconds
while it runs, and all of it when it ends. A path can't be `/`,
`/workspace` itself or a system folder.

`secrets` (`Secret`s, ids or names; up to 20) become environment
variables for every command, a later secret's key winning a clash; the
sandbox keeps the values it started with. A volume's or secret's name is
the sandbox's user's own, else the account's (a user session's, its own only).

Parameters:

- `image` (`str | None`, default `None`)
- `timeout_s` (`int | None`, default `None`)
- `cpu` (`float | None`, default `None`)
- `memory_mb` (`int | None`, default `None`)
- `network` (`bool | Sequence[str] | None`, default `None`)
- `region` (`str | None`, default `None`)
- `volumes` (`Mapping[str, Any] | None`, default `None`)
- `secrets` (`Sequence[Any] | None`, default `None`)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Sandbox` The sandbox, `running` and ready for commands. It stops on its own at `expires_at`.

Usage:

Use it as a context manager, so it stops when the block ends:

```python
with noclick.Sandbox.create(image="python", timeout_s=600) as sb:
    print(sb.exec("python -c 'print(21 * 2)'").stdout)   # 42
```

Without `with`, stop it yourself or let it run out:

```python
sb = noclick.Sandbox.create(image="node", timeout_s=300, network=True)
print(sb.id, sb.expires_at)
sb.terminate()
```

Run it in Europe, beside an agent that works there:

```python
with noclick.Sandbox.create(region="eu") as sb:
    print(sb.region)   # eu
```

Reach only the package index:

```python
with noclick.Sandbox.create(image="python", network=["pypi.org", "files.pythonhosted.org"]) as sb:
    sb.exec("pip install requests")
```

Mount a volume and pass a secret, so the job reads its data, calls its API and leaves its
output for the next run:

```python
data = noclick.Volume.from_name("datasets", create_if_missing=True)
openai = noclick.Secret.from_name("openai")
with noclick.Sandbox.create(volumes={"/data": data}, secrets=[openai], network=True) as sb:
    sb.exec("python summarize.py --in /data/raw --out /data/summaries")
```

### Sandbox.from_id

```python
noclick.Sandbox.from_id(sandbox_id, *, client=None, user=...) -> Sandbox
```

A lazy reference to a sandbox by id, for one you started earlier.

Parameters:

- `sandbox_id` (`str`, required)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: `Sandbox` A sandbox that loads the first time you read a field. Nothing is sent yet.

Usage:

```python
sb = noclick.Sandbox.from_id("5e0c7a1d-2b9f-4d3e-8a61-7f2c9b4e1d08")
print(sb.exec("ls -la").stdout)
```

### Sandbox.get

```python
noclick.Sandbox.get(sandbox_id, *, user=..., client=None) -> Sandbox
```

Load a sandbox by id now; an unknown id raises `noclick.NotFound`.

Parameters:

- `sandbox_id` (`str`, required)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Sandbox` The sandbox, loaded. Its `status` is `starting`, `running`, `terminated` or `failed`.

Usage:

```python
sb = noclick.Sandbox.get("5e0c7a1d-2b9f-4d3e-8a61-7f2c9b4e1d08")
print(sb.status, sb.expires_at)
```

### Sandbox.list

```python
noclick.Sandbox.list(
    *, status=None, limit=None, user_filter=None, user=..., client=None
) -> list[Sandbox]
```

List the current user's sandboxes (the account's own outside `as_user`, or
those `user_filter` covers); `status="live"` lists those starting or running.

Parameters:

- `status` (`str | None`, default `None`)
- `limit` (`int | None`, default `None`): Return at most this many.
- `user_filter` (`Any`, default `None`): Which users a read covers: `noclick.ALL_USERS`, `noclick.NO_USER`, a `User`, or your id for one. It filters without acting as that user.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `list[Sandbox]` Every matching sandbox, newest first, fetched page by page.

Usage:

The sandboxes still starting or running:

```python
for sb in noclick.Sandbox.list(status="live"):
    print(sb.id, sb.image, sb.expires_at)
```

Every user's, plus your account's own:

```python
sandboxes = noclick.Sandbox.list(user_filter=noclick.ALL_USERS, limit=50)
```

### Sandbox.list_page

```python
noclick.Sandbox.list_page(
    *, cursor=None, limit=None, status=None, user_filter=None, user=..., client=None
) -> Page
```

One page of sandboxes; pass `next_cursor` back as `cursor`.

Parameters:

- `cursor` (`str | None`, default `None`): The `next_cursor` of the page before; omit it for the first page.
- `limit` (`int | None`, default `None`): Return at most this many.
- `status` (`str | None`, default `None`)
- `user_filter` (`Any`, default `None`): Which users a read covers: `noclick.ALL_USERS`, `noclick.NO_USER`, a `User`, or your id for one. It filters without acting as that user.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Page` A page whose `data` holds the sandboxes and whose `next_cursor` is `None` on the last page.

Usage:

```python
page = noclick.Sandbox.list_page(status="terminated", limit=50)
while True:
    for sb in page.data:
        print(sb.id, sb.created_at)
    if page.next_cursor is None:
        break
    page = noclick.Sandbox.list_page(status="terminated", limit=50, cursor=page.next_cursor)
```

### Sandbox.exec

```python
sandbox.exec(command, *, background=False, timeout_s=None) -> ExecResult | Operation
```

Run a shell command (a string, or an argv list that gets quoted)
and wait for it: `timeout_s` is 60 by default, at most 90. A
non-zero exit is a result, not an exception.

`background=True` is for commands longer than one request allows
(`timeout_s` up to 3600, within the sandbox's own lifetime).

Parameters:

- `command` (`str | Sequence[str]`, required)
- `background` (`bool`, default `False`)
- `timeout_s` (`int | None`, default `None`)

Returns: `ExecResult | Operation` What the command printed and how it exited: `stdout` and `stderr` (the last 64 KiB of each)
and `exit_code`. With `background=True`, an `Operation` at once, whose `wait()` gives that result.

Usage:

Commands run with bash in `/workspace`. Check `ok` or `exit_code`, since a failing command
doesn't raise:

```python
with noclick.Sandbox.create() as sb:
    result = sb.exec("python -c 'print(21 * 2)'")
    print(result.ok, result.stdout)   # True 42
    sb.exec(["python", "script.py", "--input", "data.csv"])   # an argument list is quoted for you
```

Run anything longer in the background, in a sandbox that lives long enough to finish it:

```python
with noclick.Sandbox.create(timeout_s=3600) as sb:
    build = sb.exec("make all", background=True, timeout_s=1800)
    print(build.wait().exit_code)
```

### Sandbox.shell

```python
sandbox.shell() -> CodeSandboxShell
```

Open an interactive shell (`bash -l` in `/workspace`, in a PTY) in this running sandbox.
`noclick shell <sandbox>` opens one in your own terminal.

Returns: `CodeSandboxShell` The websocket's `url` and its single-use `token` (good for two minutes): hand them to a browser
terminal (xterm.js) from your server.

Usage:

```python
sb = noclick.Sandbox.from_id("3b9d6f2e-1c4a-4e8b-9f7d-2a6c5e8b1d40")
opened = sb.shell()
send_to_browser(opened.url, opened.token)
```

### Sandbox.terminate

```python
sandbox.terminate() -> None
```

Stop the sandbox now (billing stops with it).

Usage:

```python
sb = noclick.Sandbox.create()
try:
    print(sb.exec("uname -a").stdout)
finally:
    sb.terminate()
```

## noclick.CodeSandboxShell

```python
class CodeSandboxShell
```

An interactive shell in a sandbox (`sandbox.shell()`): `url` is a websocket, opened once with `token`
within two minutes. Its first message is `{"type": "auth", "token": ..., "cols": ..., "rows": ...}`; then binary
messages carry what you type and what it prints, a text `{"type": "resize", "cols", "rows"}` sizes it, and it
sends `{"type": "ready"}` once and `{"type": "state", "state": "exited", "reason", "exit_code"}` as it closes
(code 4410): bash exited, `idle_limit_s` passed with nothing typed, or the socket closed. It closes 4401 once its
key is revoked or expires and 4404 once the sandbox stops.

| Attribute | Type | Description |
| --- | --- | --- |
| `sandbox_id` | `str` |  |
| `url` | `str` |  |
| `token` | `str` |  |
| `token_expires_at` | `Any` |  |
| `idle_limit_s` | `int` |  |

## noclick.SandboxFiles

```python
class SandboxFiles
```

`sb.files`: read and write files inside the sandbox.

### SandboxFiles.write

```python
sb.files.write(path, data) -> None
```

Write `data` (bytes, or text as UTF-8) to `path`, replacing it.

Parameters:

- `path` (`str`, required)
- `data` (`bytes | str`, required)

Usage:

```python
with noclick.Sandbox.create(image="python") as sb:
    sb.files.write("/workspace/main.py", "print('hello from the sandbox')")
    print(sb.exec("python main.py").stdout)   # hello from the sandbox
```

### SandboxFiles.read

```python
sb.files.read(path) -> bytes
```

Read the file at `path`; a missing file raises `noclick.NotFound`.

Parameters:

- `path` (`str`, required)

Returns: `bytes` The file's bytes, exactly as stored; decode text yourself.

Usage:

```python
with noclick.Sandbox.create() as sb:
    sb.exec("echo done > /workspace/out.txt")
    print(sb.files.read("/workspace/out.txt").decode())   # done
```

## noclick.ExecResult

```python
class ExecResult
```

What a command printed and how it exited (a non-zero `exit_code` is a result, not an error).

| Attribute | Type | Description |
| --- | --- | --- |
| `stdout` | `str` |  |
| `stderr` | `str` |  |
| `exit_code` | `int | None` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |
| `ok` | `bool` | Whether the command exited 0. |
