# The inbox

How a message from another process reaches a running conversation: grapevine stage 1 and the read
side of stage 2 ([the design](../design/grapevine.md)). The user's view is in [remote
control](../manual/remote-control.md).

## The table

`pic_inbox` (schema 17), one row per message:

| Column | Meaning |
|---|---|
| `msg_key` | the sender's idempotency key, unique, never forgotten |
| `command_hash` | SHA3-256 of `session_id`, `sender` and `command`: the same key and hash is a retry; any other a conflict |
| `session_id` | the receiving conversation, resolved when sent |
| `sender` | `cli` (`pic send`) or `attach` (`pic attach -i`) |
| `command` | one Pi RPC command line (`steer`, `follow_up`, `abort`); NULL once pruned |
| `hops` | 0 until peers send (stage 4) |
| `target` | an abort's request: the conversation's admitted one when it was sent |
| `created_ms`, `expires_ms` | when it was accepted; after `expires_ms` it is expired, never run |
| `state` | `pending`, `delivered`, `refused` or `expired` |
| `response` | the response line `src/commands.c` gave, or the inbox's own for an abort or expiry |
| `submission_id`, `queued_id` | what delivery made: the `pic_queued` row of a steer or follow-up |
| `settled_ms` | when it left `pending`; retention counts from it |

## Accepting

The sender opens the journal with `pic_open_narrow` (`src/store.c`): the journal lock shared, no
recovery, no schema change, refused unless the schema is this build's and the file is the user's.
`pic_inbox_accept` (`src/inbox.c`) is one `IMMEDIATE` transaction: a known key returns its state
(or is refused as a conflict), the limits (100 pending rows, 64 MB pending per conversation, a
16 MB line) and the conversation are checked, and the row is inserted. An abort sent while nothing
runs is inserted already `refused`. After the commit the sender rings the bell,
`journal.sqlite.pic-leases/ID.bell`: a 20-digit counter rewritten in place.

## Delivering

`pic_run` (`src/loop.c`) calls `pic_inbox_poll` at each step, once it holds the running request's
lease. The first poll starts a thread that waits on the bell through `pic_platform.watch` (kqueue's
`EVFILT_VNODE`, inotify), or stats it every 200 ms where there is none (`PIC_NO_WATCH=1` in tests),
and wakes every 30 s regardless; it writes a byte to a pipe whose read end is `pic_watch_fd`
(`src/net.h`), which every wait polls beside the side descriptor. The thread never touches SQLite.

Each pending row, oldest first, is settled in one transaction begun with `pic_begin_outer`, inside
which `pic_begin` is a savepoint: an expired row becomes `expired`; an abort is `delivered` when its
target is still the running request (the cancel follows at the loop's next step, as Ctrl-C's) and
`refused` otherwise; a steer or follow-up goes through `pic_commands_line` with a response sink
(`pic_commands_sink`), so the `pic_queued` row and the inbox row commit together. The events it
makes (`queue_update`) go to a memory stream while the transaction is open and out only after the
commit, so a slow reader never holds the write lock and a rolled-back delivery shows nothing; a
rollback also puts back `pic_store.running`, which a follow-up's admission may have moved. A steer or
follow-up with no admitted request (and no just-answered one of this process) stays pending, and so
do the rows behind it, keeping commit order.

A crash before the delivery commits leaves the row pending for the conversation's next owner; after
it, the queued message recovers as any does. A delivered abort is itself the record of a cancel to
make: until the loop journals the cancel, the row (`state` delivered, `target` the admitted
request; only aborts have a target) stands for it, and if the owner dies first, recovery
(`settle_session` in `src/loop.c`, reached through `recover_abandoned` at any open or through the
next lease) cancels that request. Ctrl-C keeps its window: it is a signal, journaled only by the
cancel itself, and recording it earlier would mean a write from every wait that notices it; a pic
killed between Ctrl-C and its cancel leaves the request to run on.

## Retention

Hourly with image pruning in `pic_open`, and for its own row in `pic send --wait`: pending rows
past `expires_ms` become `expired`; rows
settled more than 7 days ago lose `command` and `response`, keeping key, hash and state.

## Invariants

`pic check` adds: every row has `expires_ms`; a settled row that is not pruned has its response, at
most one of `submission_id` and `queued_id`, and none for an abort; a pending row has its command
and no response or product; a `queued_id` names a `pic_queued` row of the same conversation.

## Fault points

`inbox_accept` and `inbox_deliver`, each reached twice per message: arrival 1 just before the
commit, arrival 2 just after. `before-cancel` (`src/loop.c`) is where the loop has seen an abort or
Ctrl-C and not yet journaled the cancel. `test/pic/grapevine.sh` crashes at all five.

## pic attach

`src/attach.c` (bundled) collects each pass's output and writes it after the pass's read
transaction ends, so a reader that stops reading never pins the WAL. It reads with the narrow open and writes only through `pic_inbox_accept`;
with `-i` and nothing running it starts a request as a `pic ask --session` child in a session of
its own (`setsid`), which opens the journal fully and takes the lease, and follows it like any. Each
pass is one read transaction, run when `PRAGMA data_version` changes (polled every 100 ms), so a
request's `agent_end` is never seen before its last entry. Events: `agent_start` at a request's
first entry, `message_start` and `message_end` for user entries, `message_end` for replies,
`tool_execution_start` once a call leaves `intent`, `tool_execution_end` at its result entry,
`agent_end` once the request is not admitted. Entry events carry `seq`. Not rebuilt: streamed
deltas, turn events, queue updates, usage, compactions, hook and instruction entries, images.
