# Inbox

> The inbox.watch and inbox.read contract for receiving what the user writes to your resident and late answers, and the CLI inbox commands.

Messages the user writes to a bound resident, along with answers to earlier questions that no call was waiting for, sit in the inbox for that resident's `namespace`/`key` (see [Resident binding](/docs/reference/resident-binding/)). Nothing is pushed to your agent. For the walkthrough, see [Give your agent a resident](/docs/connect/own-a-resident/); for retry behavior, see [Effects and retries](/docs/connect/effects/).

```text
User message / late answer / user stow
              │
              ▼
   Inbox (namespace/key)
      │                 │
      │ IDs + kinds     │ body + mark read
      ▼                 ▼
  inbox.watch       inbox.read
  (or inbox wait)
```

`namespace` and `key` are the same correlation keys used by `resident.ensure`, not authentication. You can still read the inbox of a stowed resident.

The user can write to a bound resident at any time, producing a `kind: message` entry. Connecting a receiver for that `namespace`/`key` tells Pixling your session hears messages right away. Without a receiver, the input area shows a line noting that the message will not be read until your session checks, naming the session just like the resident's nameplate. Only the CLI's [`inbox receive`](#inbox-receive) holds a receiver; MCP cannot.

## `pixling_inbox_watch`

| Argument | Required | Value |
| --- | --- | --- |
| `namespace` | Yes | Client namespace bound via `resident.ensure`. |
| `key` | Yes | Client key bound via `resident.ensure`. |
| `waitMs` | No | Milliseconds to wait for unread entries: `0`–`55000` (default `0`). |

```json title="inbox.watch call"
{"namespace": "codex", "key": "session-123", "waitMs": 50000}
```

Returns unread entry IDs and kinds (`message`, `answer`, `closed`), never bodies or previews:

```json title="inbox.watch result"
{"messages": [{"messageID": "…", "entityID": "…", "sequence": 12, "kind": "message"}]}
```

- **Waits once and returns**: An empty array means nothing new arrived before `waitMs` elapsed; `0` returns an immediate snapshot.
- **No cursor or acknowledgment**: Unread IDs reappear on every call until you read them with `inbox.read`.
- **Safe to repeat**: Read-only and idempotent.
- **Waiter limits**: At most 1 pending wait per key and 32 app-wide; exceeding either fails with `rejected` / `tooManyWaiters`.
- **EOF cancellation**: Keep the sending side of the connection open while waiting; closing it (EOF) cancels the wait.
- **Unbound keys fail immediately**: A `namespace`/`key` never bound via `resident.ensure` fails with `rejected` / `notBound` without waiting.
- **Does not wake your host**: Watching only reports unread IDs to the active caller.

## `pixling_inbox_read`

| Argument | Required | Value |
| --- | --- | --- |
| `namespace` | Yes | Client namespace bound via `resident.ensure`. |
| `key` | Yes | Client key bound via `resident.ensure`. |
| `messageID` | Yes | Entry UUID returned by `inbox.watch`. |

```json title="inbox.read call"
{"namespace": "codex", "key": "session-123", "messageID": "…"}
```

Returns one entry and saves its read status:

```json title="inbox.read result"
{"messageID": "…", "entityID": "…", "sequence": 12, "kind": "message", "text": "…", "read": true}
```

### Entry kinds

- **`kind: message`**: Text the user typed to the resident.
- **`kind: answer`**: The user's answer to an earlier question given while no call was waiting.
  - `text` is empty, `messageID` equals `promptID`, and `answer` holds `{promptID, outcome: answered|cancelled, selectedChoiceID?, selectedChoiceIDs?, enteredText?}`.
  - `answer.outcome` is authoritative, including `cancelled`. No reply is required, and these entries do not appear on the desk.
- **`kind: closed`**: Written once when the user puts the resident away from the desk, Studio, or stow all.
  - `text` is empty, and `closed` holds `{reason: userStowed, closedAt}` (ISO 8601).
  - The resident accepts no further messages, while earlier unread entries remain readable. Any relay holding a receiver sees it end at the same moment.
  - Your own `resident.stow` does not create this entry. `inbox.watch` lists only the `closed` entry for the resident the key is currently bound to.

### Read rules and refusals

- A `messageID` belonging to another key, or one no longer retained, fails with `rejected` / `notFound`.
- If saving the read state fails, no body is returned. A response lost in transit may still have marked the entry read; reading the same `messageID` again returns the same body for as long as it is retained.
- Reading an entry does not mark it understood or completed. Reply using `message.send` to `client:<namespace>/<key>`, never to `auto`.

## Limits

| Limit | Value |
| --- | --- |
| A user message | 1–4000 characters, nonblank, at most 16000 UTF-8 bytes; line breaks and tabs allowed. |
| Unread entries per key | 32 (`closed` entries do not count toward this limit). |
| Total retained entries | 256 across the inbox. |
| Read entries | Kept up to 7 days, or dropped earlier when room is needed. |

**Retention eviction**: Unread entries for a resident still on the desk are never dropped. When room is needed at the 256-entry limit, unread entries for off-desk residents drop first, oldest first, with `closed` entries dropped before others.

## CLI

```sh title="Terminal"
pixling inbox watch --namespace codex --key SESSION_KEY --wait-ms 50000 --json
pixling inbox read MESSAGE_ID --namespace codex --key SESSION_KEY --json
```

- **`inbox watch`**: Maps to `inbox.watch`. In the CLI, `--wait-ms` defaults to `50000` (maximum `55000`; `0` for an immediate snapshot). Without `--json`, it prints one ID per line or `no unread messages`.
- **`inbox read MESSAGE_ID`**: Maps to `inbox.read`. Without `--json`, human output depends on the entry kind:
  - `message`: Prints the message body.
  - `answer`: Prints `answer to question PROMPT_ID: answered (CHOICE_ID)` or `answer to question PROMPT_ID: cancelled`, with `text: …` on the next line for a typed answer.
  - `closed`: Prints `closed by the user: userStowed at TIME`.

### `inbox wait`

```sh title="Terminal"
pixling inbox wait --namespace codex --key SESSION_KEY [--since SEQ] [--timeout-ms N] --json
```

Waits until something new arrives, prints the unread IDs, and exits. This is the building block for waking your agent [without the plugin](/docs/connect/wake/#without-the-plugin).

- **Event-driven, no polling**: Subscribes first to the app's payload-free Darwin notification, reads `inbox.watch` with `waitMs` `0`, and re-reads on each notification.
- **No waiter slot usage**: Does not consume the app's 32 `inbox.watch` waiter slots, so any number of sessions can wait simultaneously.
- **`--since SEQ`**: Counts only unread entries with a strictly greater `sequence`. Pass the highest `sequence` you have already handled so it does not return immediately; omit it to return on any unread entry or if the inbox restarts from sequence `0`.
- **Unreachable tolerance**: While Pixling is not running, restarting, or has **CLI connection** turned off, `inbox wait` logs once to stderr and keeps waiting. Other errors, such as an unbound key, return immediately as usual.
- **`--timeout-ms`**: With a positive timeout, exits `0` with the last read document (empty `messages`), or exits `3` with an `unavailable` document if Pixling was unreachable on the final read. Without it, waits indefinitely.
- **Parent lifetime**: Exits automatically when its parent process exits, including after a crash or `SIGKILL`.
- **Output**: Same document shape as `inbox watch`, with `messages` filtered by `--since`. Read bodies with `inbox read`.

<details>
<summary>Darwin notification name</summary>

`inbox wait` subscribes to `user.uid.<uid>.app.pixling.inbox.<SHA256 hex of namespace NUL key>`.

</details>

### `inbox receive`

```sh title="Terminal"
pixling inbox receive --namespace codex --key SESSION_KEY --json
```

Holds a receiver open for the key so Pixling knows your session hears user messages right away, suppressing the "not read until your session checks" note in the input area. Prints nothing while active and exits when the receiver ends.

- **Prerequisites**: The resident must be bound by `resident.ensure` and on the desk, and **CLI connection** must be on.
- **Concurrency**: Multiple receivers may hold the same key (the session counts as hearing while at least one is connected), up to 32 receivers total across the app.
- **Lifetime**: No time limit, heartbeat, or polling. Ends when the process or its parent exits (including crash or `SIGKILL`), and the app removes the receiver; parent exit returns exit code `3`.
- **No auto-reconnect**: Run it again when you want to resume receiving, for example after Pixling restarts.
- **Pair with `inbox wait`**: `inbox receive` neither wakes your host nor reads the inbox. Run `inbox wait` alongside it to react to new entries.

Exit outcomes:

| Exit code | Result | Meaning |
| --- | --- | --- |
| `0` | `{"reason": "userStowed"}` | The user put the resident away. The inbox also gets a `kind: closed` entry. Do not hold it again. |
| `3` | `unavailable`, `detail: disconnected` | Connection ended while holding: Pixling quit or restarted, or **CLI connection** was turned off. |
| `3` | `unavailable`, other `detail` | Pixling was unreachable or not ready yet. |
| `5` | `rejected`, `detail: notBound` | No `resident.ensure` binding uses this `namespace`/`key`. |
| `5` | `rejected`, `detail: notOnDesk` | The resident is not on the desk (for example, the user put it away). |
| `5` | `rejected`, `detail: tooManyWaiters` | 32 receivers are already held. |

Without `--json`, a user stow prints `receiver ended: userStowed`.

Relaying a wake into your host is outside Pixling itself. See [Wake your agent](/docs/connect/wake/) for which hosts support waking and how to wire it up.

## Related

- [Resident binding](/docs/reference/resident-binding/): Create and inspect the namespace/key binding that owns an inbox.
- [Messages and questions](/docs/reference/messages-and-questions/): Reply to user messages and understand how late answers land in the inbox.
