# Commands

A command is a shell line or a list of arguments. `cmd` runs with `sh -c`. `args` is a program and its arguments, run with no shell: nothing in them is expanded, split or quoted. Send one of the two.

```
curl https://api.groundbox.dev/v1/sandboxes/sbx_.../exec \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"args": ["git", "commit", "-m", "Fix the $PATH bug; ship it"]}'
```

## Input and output

Send `stdin` (text) or `stdin_base64` (any bytes) with the call. The command reads it, then end-of-file. Set `encoding` to `base64` to get `stdout` and `stderr` back as base64, byte for byte.

```
curl https://api.groundbox.dev/v1/sandboxes/sbx_.../exec \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"args": ["python3", "-"], "stdin": "print(6 * 7)\n"}'
```

## Streams and long commands

`POST /exec` waits for the command, for up to 60 minutes. To read output as it is printed, to send input as you go, or to run for longer, use the stream: a WebSocket at `/exec/stream`. The SDK and the CLI speak it for you.

```
const command = groundbox.stream_command(
  sandbox.id,
  ["python3", "-u", "train.py"],
  { resumable: true, timeout_s: 6 * 3600 },
  { onStdout: (text) => process.stdout.write(text) },
);
const { exit_code } = await command.done;
```

A resumable command keeps running when the connection drops. The SDK reconnects and reads on from where it was, so each line arrives once. A resumable command can run for as long as its sandbox does. A command that is not resumable ends with its connection, and can run for 60 minutes. Both kinds are in the sandbox's command history.

A resumable command can take input too. Text and bytes both work. Input written while the connection was down is sent once it is back.

```
const repl = groundbox.stream_command(sandbox.id, "python3 -i", { stdin: true, resumable: true }, {
  onStdout: (text) => process.stdout.write(text),
});
repl.write("print(6 * 7)\n");
repl.write(new Uint8Array([0x65, 0x78, 0x69, 0x74, 0x28, 0x29, 0x0a])); // bytes work too
repl.end();
```

## A terminal

Start a stream command with `tty: true` and it runs on a terminal of its own in the sandbox. Prompts, colours, line editing and full-screen programs like `vim` and `htop` work. Give it the size of your window, send what is typed as input, and send `resize` when the window changes. `groundbox sandbox shell` does all of this for you.

```
const shell = groundbox.stream_command(
  sandbox.id,
  "bash -il",
  { stdin: true, tty: { rows: process.stdout.rows, cols: process.stdout.columns }, resumable: true, hangup_s: 300 },
  { onStdout: (text) => process.stdout.write(text) },
);
process.stdin.setRawMode(true);
process.stdin.on("data", (keys) => shell.write(keys));
process.stdout.on("resize", () => shell.resize(process.stdout.rows, process.stdout.columns));
const { exit_code } = await shell.done;
```

A terminal has one stream, so the command's errors arrive with its output. A resumable terminal is a session: it survives a dropped connection, and the SDK picks it up where it was. `hangup_s` ends the session when nobody has been attached for that many seconds. The sandbox's image needs the `script` program from util-linux, which Debian and Ubuntu images have. Nothing else here needs anything in the image: resumable and background commands work in any image, with or without Node.js. The command history needs Node.js in the sandbox: a sandbox made from a Docker image always has Groundbox's own, and the default image has it.

## Background commands

`POST /commands` starts a command and answers at once with its id, which starts with `ex_`. The command runs on with nobody connected. Use it for servers, watchers and long jobs.

```
curl https://api.groundbox.dev/v1/sandboxes/sbx_.../commands \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"cmd": "npm run dev", "cwd": "/home/runner/app", "timeout_s": 86400}'
```

Its output is kept in the sandbox. Read it from any position, as often as you like. To follow it live, or to send it input, attach to it on the stream.

```
# What it printed so far. "seq" in the answer is where to read on from.
curl "https://api.groundbox.dev/v1/sandboxes/sbx_.../commands/ex_.../output?seq=0" \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY"
```

## List and kill

`GET /commands` lists what the sandbox runs and ran, newest first. `running` is the number of commands running now. Kill sends `SIGKILL` unless you name another signal.

```
# What runs now
curl "https://api.groundbox.dev/v1/sandboxes/sbx_.../commands?state=running" \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY"

# End one, and everything it started
curl -X POST https://api.groundbox.dev/v1/sandboxes/sbx_.../commands/ex_.../kill \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"signal": "SIGTERM"}'
```

```
const server = await groundbox.start_command(sandbox.id, "npm run dev", { cwd: "/home/runner/app" });

// Later, from any process: read what it printed, or follow it live.
const { stdout, seq } = await groundbox.get_command_output(sandbox.id, server.id);
const live = groundbox.attach_command(sandbox.id, server.id, { seq }, { onStdout: (text) => process.stdout.write(text) });

const { data, running } = await groundbox.list_commands(sandbox.id);
await groundbox.kill_command(sandbox.id, server.id);
```

## Command history

Every command run in a sandbox is recorded there: `POST /exec`, the stream (resumable or not), background commands, and the dashboard's Commands tab, whether it came from the CLI, an SDK, MCP or your own code. That is how you see what an agent did. Each command has its id, the command line, its folder, `status` (`running`, `finished` or `failed`), exit code, duration, start time, `kind` (`run` or `background`) and `source` (`dashboard`, `cli`, `sdk`, `mcp` or `api`). Filter by `status` and `kind`, and page with `cursor`. `POST /exec` answers with the command's `exec_id`, so its entry is easy to find.

```
# Everything that ran in the sandbox, newest first: failed runs only, 50 at a time
curl "https://api.groundbox.dev/v1/sandboxes/sbx_.../commands?status=failed&kind=run&limit=50" \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY"

# The next page: the answer's next_cursor
curl "https://api.groundbox.dev/v1/sandboxes/sbx_.../commands?cursor=eyJzdGFydGVkX2F0Ijo..." \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY"
```

```
groundbox sandbox commands sbx_... --status failed --limit 20
groundbox sandbox command sbx_... ex_... --output
```

The sandbox keeps the last 32 MB of each command's output until 60 minutes after it ended, then the last 64 KB of it. It keeps the newest 500 commands that ended. `output_truncated` says only the end of a command's output is kept, and a read of its output says `truncated`. The history lives in the sandbox, not in our database: a fork has its source's, and it is deleted with the sandbox. Recording happens in the same call that runs the command, so it adds no round trip.

The command line you send is kept as you sent it, so do not put secrets in it: pass them in `env` or as team secrets. Values of `env` and of team secrets are never written down. In an image with no Node.js the commands still run, but are not recorded, and the list says why in `unavailable_reason`.
