> ## Documentation Index
> Fetch the complete documentation index at: https://axe-engine.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Terminals and input

> Run processes on a pseudo-terminal, write to stdin with retry-safe offsets, resize, and attach and detach interactively.

## Start a process on a terminal

Set `terminal` in `Spec` (`axectl -t`) to run the process on a pseudo-terminal instead of pipes. The process becomes the leader of a new session with the terminal as its controlling terminal.

> [tab] axectl
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> axectl run -t -a --name shell -- bash    # start a shell and attach to it
> ```
>
> `axectl` sends your terminal's size, or 80x24 when it is not running on a terminal.

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"spec": {"command": ["bash"], "terminal": {"cols": 120, "rows": 40}}, "name": "shell"}' \
>   localhost:9090 axe.v1.Processes/Start
> ```
>
> A `terminal` with `cols` or `rows` set to 0 leaves the kernel's default size.

On a terminal:

* stdout and stderr are one stream, reported as stdout.
* stdin is always open; you do not need `stdin`.
* axed sets `TERM` to `xterm-256color` when neither the request nor axed's environment sets it.
* `Process.terminal` is `true`.

> [!NOTE]
>
> In the Agent Sandbox API, set `pty` (with `cols` and `rows`) on `process.v1.ProcessService/Start`. Without `pty`, that API always opens stdin.

## Write to stdin

A process without a terminal has stdin only if you start it with `stdin: true` (`axectl run -i`). Otherwise stdin is `/dev/null`, and writes fail with `FAILED_PRECONDITION`.

`Processes.WriteInput` writes bytes and returns the new stdin offset, the total number of bytes accepted so far. `Process.stdin_offset` reports the same number.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
grpcurl -plaintext -d '{"process": {"name": "cat"}, "data": "aGVsbG8K", "offset": 0}' \
  localhost:9090 axe.v1.Processes/WriteInput
# {"offset": "6"}
```

`data` is base64 in grpcurl's JSON. Set `eof: true` to end input after the write. A pipe is closed. A terminal gets its end-of-file character (normally Ctrl-D) once, which a program reading at the start of a line takes as end of input, and accepts no input after it. The end-of-file character does not count toward the stdin offset, and ending input again does nothing, so a retried write with `eof` sends it only once.

### Make retries safe with offsets

Name the stdin offset your bytes start at, and a retry after a lost reply cannot repeat input:

| `offset` compared with bytes accepted | Result |
| - | - |
| Equal | axed writes all bytes. |
| Lower, and the bytes end at or before what was accepted | axed writes nothing and returns the current offset. |
| Lower, and the bytes run past what was accepted | axed writes only the bytes not yet accepted. |
| Higher | Fails with `FAILED_PRECONDITION`, because the write would leave a hole. |

Leave `offset` unset to append without the check. An `offset` so large that adding the data's length overflows fails with `INVALID_ARGUMENT`.

The offset counts every byte the process accepted, even when the write then fails, is cancelled, or is cut short because the process exited. A retry that names its original offset therefore writes only the bytes the process did not accept.

A write can block when the process stops reading its input and the pipe or terminal buffer fills. Such a write stays pending until the process reads again or exits; if it exits first, the write ends with `FAILED_PRECONDITION`.

For a stream of input, `Processes.StreamInput` takes a client stream of `InputChunk`s. The first chunk must name the process; every chunk may carry `data` and `eof`. The call returns the final offset. `StreamInput` does not check offsets, so use `WriteInput` when you need safe retries.

> [!NOTE]
>
> The Agent Sandbox `WriteStdin` takes `input` or `eof`, with no offsets. As in sandboxd, its `eof` on a terminal process fails with `FAILED_PRECONDITION`; write `0x04` instead.

## Resize

`Processes.Resize` sets the terminal's `cols` and `rows`. It fails with `FAILED_PRECONDITION` when the process has no terminal or has exited.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
grpcurl -plaintext -d '{"process": {"name": "shell"}, "cols": 200, "rows": 50}' \
  localhost:9090 axe.v1.Processes/Resize
```

`axectl` resizes for you in `attach -i`, `run -a` and `exec`, when it forwards your input from a terminal to a terminal process. It sends your size right away and again on every window change. A plain `axectl attach` without `-i` only reads output and does not resize. The Agent Sandbox equivalent is `ResizeTTY`.

## Attach interactively

**1. Start a process with input**

Start it with a terminal (`-t`), or with `-i` to keep a pipe stdin open. `-a` attaches right away and implies `-i`.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl run -t --name shell -- bash
```

**2. Attach with input**

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl attach -i shell
```

`attach` replays the retained output, then follows. `-i` forwards your stdin. If both the process and your session are terminals, `axectl` switches your terminal to raw mode, so keys such as Ctrl-C reach the remote process instead of stopping `axectl`.

**3. Detach**

Press **Ctrl-P Ctrl-Q**. `axectl` prints `axectl: detached from process <id>`, exits 0, and the process keeps running. Attach again whenever you like.

`axectl` holds a Ctrl-P until the next key arrives. If that key is not Ctrl-Q, both keys go to the process. The detach keys are fixed; `axectl` has no option to change them. They work only when `axectl` forwards your input, in `run -a` and `attach -i`, and only when the process has a terminal. On a process without a terminal, every byte of input is forwarded unchanged, so binary input passes through intact. A plain `axectl attach` reads no input, so stop it with Ctrl-C; the process keeps running. `axectl exec` cannot detach, because its process dies with the client.

When local stdin reaches end-of-file, `axectl` ends the process's input. It closes the stdin of a process without a terminal, and sends the end-of-file character (Ctrl-D) to a terminal. This lets you pipe input, including to `axectl exec -t`:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl run -i --name upper -- tr a-z A-Z
echo hello | axectl attach -i upper     # prints HELLO, exits with tr's status
```

The terminal acts on Ctrl-D only in its default line mode. A program that switches it to raw mode for line editing, as `bash` and `python3` do, can miss a Ctrl-D that arrives before it reads, and then waits for more input. End piped input to such a program with a command:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
printf 'print(6*7)\nexit()\n' | axectl exec -t -- python3 -q
```

If you run `axectl` through the Docker wrapper, piping input needs `docker run -i` rather than `-it`, because Docker refuses `-t` when its stdin is not a terminal.

### Docker claims Ctrl-P Ctrl-Q

Docker uses Ctrl-P Ctrl-Q as its own default detach sequence for `docker run -it`. If you run `axectl` in a container, Docker sees the keys first and detaches the `axectl` container instead of `axectl` detaching from the process. Give Docker a different sequence with `--detach-keys`, as the `axectl` wrapper in the [quickstart](https://axe-engine.mintlify.site/quickstart.md) does.

## Exit codes

`axectl attach` and `axectl run -a` exit with the process's exit code once it exits, 0 when you detach, 130 when interrupted with Ctrl-C outside raw mode, and 125 when `axectl` itself fails. [Signals, exits and limits](https://axe-engine.mintlify.site/processes/signals.md) explains how axed forms exit codes.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.