# Protocol Reference

Every stdin line is one JSON object tagged by a required `code`. Every emitted
line is an Agent-First Data envelope tagged by a top-level `kind`; see
[Output](#output) for the envelope shape.

- pipe mode: full protocol with `id` correlation
- CLI mode: same event schema, `id` may be omitted in display output
- a finite query splits by kind: `result` to stdout, `error`/`log`/`progress`
  to stderr, so a shell capture never mistakes a failure for data
- pipe mode and `--stream-rows` are ordered event streams: every event goes to
  stdout, because splitting a stream across two process streams would lose the
  ordering that makes it a stream
- `--output-to split|stdout|stderr` overrides the destination; `split` is
  rejected for a stream rather than silently reordering it

## Interface Boundary

This protocol is the only runtime interface.

- the CLI is a closed `cli-spec-v1` registry: an invocation runs only when it
  matches exactly one registered shape, there are no short flags, and a command
  path comes before its arguments (`afpsql --docs` renders the whole registry)
- `psql mode` is CLI argument translation only; runtime protocol is unchanged
- no legacy text interpolation
- no table/text output contract

Agent-facing reliability guarantees:

- recoverable runtime conditions are structured events, not ad-hoc prose
- native CLI and pipe writes require explicit permission
- pipe named sessions execute FIFO and are intended to preserve PostgreSQL
  backend session state until invalidation or shutdown
- database failures preserve PostgreSQL `SQLSTATE`
- validation and permission failures include actionable hints when possible

## `afpsql-readonly` capability boundary

`afpsql-readonly` is a separately installed database-write guard. It shares the
read/query, inspect, file/config, and typed transport implementation with
`afpsql`. It is not a replacement for PostgreSQL role authorization and is not
a general local-process sandbox.

| Path | readonly behavior |
|---|---|
| direct / SSH / container read permission | allowed |
| `write`, `ssh-write`, `container-write` | `invalid_request` |
| pipe `begin` read-only | allowed |
| pipe read-write `begin` or write query | `invalid_request` |
| `--mode psql`, `psql status/install/uninstall` | `invalid_request`; use `afpsql psql ...` |
| `skill status/install/uninstall` | allowed on the ordinary entrypoint |
| `--stdout-file`, `--stderr-file`, local `--sql-file` | allowed on the ordinary entrypoint |
| `--dsn env:NAME`, `--conninfo env:NAME`, `--password env:NAME` | allowed on the ordinary entrypoint |
| `--dsn file:PATH#DOT_PATH`, `--conninfo file:PATH#DOT_PATH`, `--password file:PATH#DOT_PATH` | allowed on the ordinary entrypoint |
| explicit SSH options | only `ConnectionAttempts`, `ConnectTimeout`, `Port`, `ProxyJump`, `ServerAliveCountMax`, `ServerAliveInterval`, `TCPKeepAlive`; other settings belong in `~/.ssh/config` |
| custom container runtime | `invalid_request` on the ordinary entrypoint; administrator-locked profiles may select one |
| transaction control sent as query SQL | `invalid_request`; use typed pipe transaction requests |

Every readonly rejection is a structured error event with
`error.code: "invalid_request"` and a hint directing write work to `afpsql`.
The client transaction is a safety belt, not an adversarial SQL sandbox. A
whitelist for the ordinary executable grants caller-selected local file and env
reads, process-spawning transport options, network access to caller-selected
direct/SSH/container targets, and every row visible to the database role. For a
server-enforced baseline, provision a non-owner login role:

```sql
CREATE ROLE app_reader LOGIN
  NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;
GRANT CONNECT ON DATABASE app TO app_reader;
GRANT USAGE ON SCHEMA app TO app_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA app TO app_reader;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA app
  GRANT SELECT ON TABLES TO app_reader;
```

Keep the role out of writer/owner memberships and audit `PUBLIC`, function
`EXECUTE`, `SECURITY DEFINER`, extensions, predefined roles, sequences, and RLS
visibility separately.

Output redaction covers connection, configuration and control metadata only. It
is not a guard on what a query returns: rows, column values, SQL text and the
parameters shown to a human approver are all emitted exactly as the database
gave them, including a column named `api_key_secret`. Redacting them would make
afpsql report something other than what is in the database, which is the one
thing this tool must never do — so a caller that must not see a value has to
avoid selecting it. Redaction of connection metadata also does not mean
connection secrets never leave the process.

PostgreSQL READ ONLY rejects temporary-object creation, sequence `nextval`, and
writes performed inside SECURITY DEFINER functions. PostgreSQL still permits
`NOTIFY` and advisory locks, and, for a role with the privilege, statements
that change no table rows but are not harmless: `CLUSTER`, `REINDEX` and
`ANALYZE` (which rewrite or lock a table), `LOCK TABLE` (held until the
transaction ends, including an idle pipe transaction), `COPY … TO` a server
file or program and `lo_export`, `pg_terminate_backend`, and `dblink_exec`,
which writes over a connection of its own. Read permission is a transaction
mode, not a privilege boundary: give a reading agent a role that lacks these
privileges, and constrain the rest through role/function audit, timeouts,
concurrency limits, and lock/resource governance. Readonly rejects transaction-control SQL before execution because
PostgreSQL can accept nested `BEGIN`/`COMMIT`; pipe clients must use the typed
`begin`, `commit`, and `rollback` requests so afpsql owns transaction state.

### Administrator-locked readonly profiles

On Unix, an executable basename `afpsql-readonly-NAME` selects exactly
`/etc/afpsql/readonly-profiles/NAME.json`. `NAME` is limited to ASCII letters,
digits, `-`, and `_`; the profile must be a regular root-owned file, no larger
than 64 KiB, and not writable by group or others. For example:

```json
{"host":"db.internal","port":5432,"user":"app_reader","dbname":"app","password_secret":"..."}
```

The file uses the flat session fields documented for pipe config. Because this
is administrator-controlled configuration, it may contain custom container
runtimes. Explicit SSH options retain the same allowlist as the ordinary
entrypoint; put settings such as `IdentityFile`/`ProxyCommand` in `~/.ssh/config`.
A locked profile executable
rejects all connection/transport flags and all pipe `sessions` patches before
they can replace the profile.

A locked profile is also the whole connection: it reads none of the connection
environment variables that the ordinary readonly executable falls back to —
`AFPSQL_DSN_SECRET`, `AFPSQL_CONNINFO_SECRET`, `AFPSQL_HOST`/`PGHOST`,
`AFPSQL_PORT`/`PGPORT`, `AFPSQL_USER`/`PGUSER`, `AFPSQL_DBNAME`/`PGDATABASE`,
`AFPSQL_PASSWORD_SECRET`/`PGPASSWORD`, and `PGSSLMODE`. Otherwise a caller who
may only choose SQL could still move the endpoint, or downgrade its TLS, through
the environment. Put every connection field the profile needs — including the
password and any `sslmode` — in the profile file itself; use `dsn_secret` there
when you need `sslmode`. Hosts needing a single target should authorize
this distinct executable name, not infer a target by parsing shell arguments.

Locked profiles currently require Unix owner/mode checks. On other platforms,
use OS-native ACLs plus an equivalent wrapper policy; the generic readonly
executable continues to allow caller-selected targets as documented above.

## Connection secret sources

Each secret slot has three mutually exclusive explicit sources:

| Slot | Direct | Environment | Config file |
|---|---|---|---|
| DSN URI | `--dsn` | `--dsn env:NAME` | `--dsn file:FILE#DOT_PATH` |
| libpq conninfo | `--conninfo` | `--conninfo env:NAME` | `--conninfo file:FILE#DOT_PATH` |
| password | `--password` | `--password env:NAME` | `--password file:FILE#DOT_PATH` |

Config files may be JSON (`.json`), TOML (`.toml`), YAML (`.yaml`/`.yml`),
dotenv (`.env`, `.env.*`, `*.env`), or INI (`.ini`). A config source is one
typed argument containing both the file and dot path.

The grammar is AFDATA's, declared on each of the three arguments, so the
canonical registry refuses a malformed or unaccepted source while it resolves
argv — `cli_invalid_argument_value` at exit 2, before any file is opened — and
the psql-compatible translator, which parses its own argv, refuses the same set
through the same parser. Stream sources (`stdin`, `fd:N`, `prompt`) are
deliberately not accepted here: `afpsql` reads SQL on stdin, and blocking on a
terminal is a hang for an agent. The path must resolve to a non-empty
string; the value is returned verbatim — never trimmed, coerced from another
type, or URL-decoded, so percent-encode reserved characters in a DSN (`%40` for
`@`). A dot path resolving to a number or a boolean is refused rather than
coerced: a credential is text. Note that a double-quoted dotenv value or a TOML basic `"..."` string still
undergoes that format's own escape processing (`\t`→tab, `\\`→`\`); use an
unquoted or single-quoted `'...'` value for raw bytes.

The file is read once during startup, before database or transport work. An
explicit config source overrides `AFPSQL_*` and libpq `PG*` fallbacks for that
slot. Pipe reconnects reuse the resolved value and do not observe file changes.
Pipe config requests cannot submit file/path references in protocol v1.

`Output::Config` serializes configured `dsn_secret`, `conninfo_secret`, and
`password_secret` as `***` for JSON, YAML, and plain output regardless of whether
the original source was direct, env, or config. Startup logs may report safe
source metadata (kind and, for config, file/dot-path), never the resolved value.

## Input (stdin)

### `query`

Execute one SQL statement.

| Field | Required | Description |
|---|---|---|
| `code` | yes | `"query"` |
| `id` | yes | client correlation id |
| `session` | no | session id; default session if omitted |
| `sql` | yes | SQL text |
| `params` | no | positional bind values |
| `options` | no | query behavior |

`options` fields:

| Field | Default | Description |
|---|---|---|
| `stream_rows` | false | stream rows as `result_rows` events |
| `batch_rows` | 1000 | max rows per `result_rows` event |
| `batch_bytes` | 262144 | soft byte target per streamed batch |
| `statement_timeout_ms` | config default | per-query statement timeout |
| `lock_timeout_ms` | config default | per-query lock timeout |
| `permission` | native/pipe transport default | `read`, `write`, `ssh-read`, `ssh-write`, `container-read`, or `container-write` |
| `inline_max_rows` | config default | inline row cap for non-streaming |
| `inline_max_bytes` | config default | inline payload bytes cap for non-streaming |

In native CLI and pipe mode, permission defaults to `read` for direct sessions,
`ssh-read` for sessions using afpsql SSH transport, and `container-read` for
sessions using afpsql container transport. Read permissions run in PostgreSQL
read-only transactions. Direct writes require `write`; SSH writes require
`ssh-write`; container writes require `container-write`.

### Parameter Binding Rules

1. Dynamic values should be passed via `params` with `$1..$N` placeholders.
2. Placeholder count must equal `params` length (validated from prepared-statement metadata, not SQL text scanning).
3. Client-side count/shape/local binding conversion failures return `error.code: "invalid_params"`.
4. PostgreSQL server conversion/execution failures return `code: "sql_error"` with the original SQLSTATE.

Driver-side type mapping (prepared statement parameter OIDs):

- `bool` -> JSON bool or `"true"/"false"`
- `int2/int4/int8` -> JSON integer or numeric string
- `float4/float8/numeric` -> JSON number or numeric string
- `json/jsonb` -> JSON object/array/scalar
- others -> text form (`string` preferred)

Unsupported:

- `:name` interpolation
- SQL string template expansion by client-side substitutions

CLI mapping notes:

- `--param N=value` maps to this `params` array. Bare `null`, `true`, and
  `false` bind JSON primitives; prefix with `text:` to bind those spellings as
  literal strings (for example `--param 1=text:null`).
- in `psql mode`, numeric `-v N=value` may be translated to `params[N]`

### `config`

Partial runtime config update. Echoes full config afterward.

| Field | Required | Description |
|---|---|---|
| `code` | yes | `"config"` |
| `default_session` | no | default session name |
| `sessions` | no | session connection definitions |
| `inline_max_rows` | no | global inline row limit |
| `inline_max_bytes` | no | global inline payload bytes limit |
| `statement_timeout_ms` | no | global statement timeout |
| `lock_timeout_ms` | no | global lock timeout |
| `log` | no | enabled log categories |

Session connection shape supports:

- `dsn_secret`
- `conninfo_secret`
- `host`
- `port`
- `user`
- `dbname`
- `password_secret`
- `ssh`
- `ssh_via`
- `ssh_options`
- `ssh_remote_socket`
- `ssh_sudo_user`
- `docker_name`
- `docker_user`
- `docker_context`
- `docker_runtime`
- `apple_name`
- `apple_user`
- `apple_runtime`
- `podman_name`
- `podman_user`
- `podman_runtime`
- `nerdctl_name`
- `nerdctl_user`
- `nerdctl_runtime`
- `compose_service`
- `compose_user`
- `compose_files`
- `compose_project`
- `compose_runtime`
- `kubectl_pod`
- `kubectl_container`
- `kubectl_namespace`
- `kubectl_context`
- `kubectl_runtime`

`session_info` is ordered through the named session's FIFO. While an explicit
transaction is open it reports configured defaults without probing PostgreSQL,
so identity fields may be null; this avoids adding savepoints or otherwise
mutating the caller's transaction.

Supported TLS settings supplied in `dsn_secret` or `conninfo_secret` are
honored. afpsql currently accepts `sslmode=disable/prefer/require`; unsupported
libpq TLS modes/options such as `verify-ca`, `verify-full`, `sslrootcert`,
`sslcert`, and `sslkey` fail with structured errors and hints.

SSH transport fields start an OpenSSH stdio bridge before
connecting. SSH accepts `dsn_secret`, `conninfo_secret`, or discrete connection
fields. afpsql parses DSN/conninfo locally, interprets its PostgreSQL endpoint
from the final SSH host, and preserves authentication, database, startup-option,
and supported TLS settings when connecting through the bridge. The source must
resolve to one PostgreSQL host and port; multi-host failover lists return a
structured non-retryable connection error.

Options that act on a socket the driver never opens are rejected by name rather
than accepted as no-ops: `connect_timeout`, `tcp_user_timeout`, `keepalives`,
`keepalives_idle`, `keepalives_interval`, `keepalives_retries`,
`target_session_attrs`, and `load_balance_hosts` fail with an error naming the
option that has to be dropped.

The bridge is carried over the `ssh` child process's stdin/stdout, so afpsql
opens no local listening port and no other process on the workstation can reach
the connection. In exchange, the host running the bridge needs `sh` plus one of
`python3`, `python`, or `perl` — any one is enough, and they are tried in that
order. A host with none of them fails with exit 127 and a message naming all
three. For `--ssh-via` chains the bridge runs on the final `--ssh` host, and
only that host needs an interpreter.

Container transport fields start a no-TTY exec bridge through one driver
(`apple`, `docker`, `podman`, `nerdctl`, `compose`, or `kubectl`) and run a
small stdio bridge inside the container. The `apple` family invokes the Apple
`container` CLI. The PostgreSQL host/port or Unix socket is interpreted inside
the container. Container transport can use `dsn_secret`, `conninfo_secret`, or
discrete connection fields and likewise requires one PostgreSQL endpoint.

The driver is not named separately: it is the family of fields used. Each field
is scoped to one driver by its own name, so a driver cannot be asked for an
option it does not have — there is no `kubectl_user` because `kubectl exec` has
no exec-as-user option, and no `podman_context` because Podman selects no
context. `kubectl_container` picks one container in a multi-container pod and is
emitted as `-c CTR` before `--`. `AFPSQL_CONTAINER_COMPOSE_FILE` may supply
colon-separated Compose files when no `compose_files` are configured.

Fields from two families cannot be combined: two families name two drivers, so
the session is rejected with an error naming both offending fields.

When a driver family is combined with `ssh`, afpsql uses SSH to run the
container exec command on the remote host, then bridges from inside the
container. In this combined mode, only `ssh` and `ssh_options` apply; SSH socket
and sudo bridge fields are for non-container SSH transport. The permission family
remains container (`container-read` / `container-write`).

CLI translation notes:

- agent-first mode uses direct agent-first flags (`--dsn`, `--host`, ...)
- `psql mode` may translate legacy flags (`-h`, `-p`, `-U`, `-d`, `-c`, `-f`)
  into these same canonical fields
- `psql mode` does not expose afpsql permission flags and preserves psql's
  writable default for script compatibility; use native afpsql for agent-safe
  permissions and transport-specific agent behavior

### `cancel`

Cancel a queued or running query by id.

```json
{"code":"cancel","id":"q-123"}
```

When the database connection is already executing the query, `afpsql` sends a
PostgreSQL server-side cancel request. When the query is still queued, `afpsql`
removes it before execution. Cancellation is still race-prone: a query may
finish normally before the cancel request is processed.

### `ping`

Health check.

```json
{"code":"ping"}
```

### `close`

Graceful shutdown.

EOF and `close` share a five-second budget for draining all session workers and
closing database clients. Work still waiting at that deadline is cancelled
locally. The `close` acknowledgement does not confirm an unfinished write or
prove a transaction rolled back; verify an operation without its terminal
result before retrying it.

```json
{"code":"close"}
```

### `session_info`

Pipe-mode introspection request. Returns the named session's resolved transport,
permission default, and runtime limits so an agent can discover what it is
connected to without probing via failing queries.

| Field | Required | Description |
|---|---|---|
| `code` | yes | `"session_info"` |
| `id` | no | client correlation id |
| `session` | no | session id; default session if omitted |

Unknown session names return `kind:"error"` with `error.code:"invalid_request"`
and a hint pointing to `config`.

### `begin` / `commit` / `rollback`

Pipe-mode explicit transactions. Without these, every `query` is wrapped in
its own implicit `BEGIN..COMMIT`, so multi-statement atomicity requires
jamming everything into one SQL string. After `begin`, subsequent `query`
events on the same session run inside the open transaction until a matching
`commit` or `rollback`.

```json
{"code":"begin","id":"b1","session":"default","read_only":false,"permission":"write"}
{"code":"commit","id":"c1","session":"default"}
{"code":"rollback","id":"rb1","session":"default"}
```

| Field | Required | Description |
|---|---|---|
| `code` | yes | `"begin"`, `"commit"`, or `"rollback"` |
| `id` | no | client correlation id, echoed on the response |
| `session` | no | session id; default session if omitted |
| `read_only` | no, `begin` only | defaults to `true`; set explicitly to `false` for a read-write transaction |
| `permission` | no, `begin` only | required when `read_only:false`; use the matching `write` / `ssh-write` / `container-write` permission |

The response is a `code:"result"` event with `command_tag` set to `"BEGIN"`,
`"COMMIT"`, or `"ROLLBACK"`. Failures (e.g. `begin` while already in a tx,
`commit` with no open tx, or PostgreSQL errors) surface as `error` or
`sql_error`.

Per-query failures inside an explicit transaction are wrapped in a savepoint
and rolled back individually, so the user's outer transaction is NOT
aborted by a single bad query — the agent can retry or move on without
losing prior progress. Send `rollback` to discard the whole transaction or
`commit` to persist the work done so far.

Every query inside a read-write explicit transaction must repeat the matching
write permission. A query that declares a read permission is rejected before
SQL execution. A read-only explicit transaction remains enforced by
PostgreSQL, including when a query supplies a write permission.

Tx control runs through the same session FIFO as `query`, so the order an
agent writes events to stdin is the order PostgreSQL sees them.

A `begin` that fails, or an explicit transaction whose connection is lost,
leaves the session in a failed transaction state rather than back in
autocommit: every `query` on that session is rejected with `invalid_request`
without reaching PostgreSQL, and another `begin` is rejected as already in an
explicit transaction. Send `rollback` to settle it (a `commit` settles it too,
reporting the transaction as rolled back); the session then accepts `begin`
and queries again. A caller that queues a transaction's statements ahead of
their responses should wait for the successful `begin` result before sending
the write statements.

Each session worker queue has 64 slots. A full queue rejects a new query or
control operation immediately with a retryable `invalid_request`, so stdin can
still accept cancellation and shutdown. A configuration patch reserves room
for every affected session's invalidation before changing any configuration;
if one queue cannot accept it, the entire patch is rejected. An accepted
`begin` keeps the endpoint and transport configuration used for its permission
check, even if a later patch changes that session before it executes.

## Output

Every output line is an Agent-First Data envelope: a top-level `kind` with the
event payload nested under the matching key, and `trace` as a top-level sibling.
The selected `--output-to` policy determines which process stream carries it.

```json
{"kind": "result", "result": { ...payload... }, "trace": { ...timing... }}
```

`kind` is one of `result`, `progress`, `error`, or `log`. The business event
name is the payload's own `code` field (except `log`, which drops `code` and adds
`timestamp_epoch_ms`). The per-event tables below list **payload** fields — the
object nested under the envelope key — so `code`/`id`/`columns`/… arrive as
`result.code`/`result.id`/… on the wire. Each table also lists `trace` for
reference, but it is the top-level sibling shown above, not nested in the payload.

| Event (`payload.code`) | Envelope `kind` | Payload key |
|---|---|---|
| `result`, `result_end`, `dry_run`, `session_info`, `config`, `pong`, `close` | `result` | `result` |
| `result_start`, `result_rows` | `progress` | `progress` |
| `sql_error`, `error` | `error` | `error` |
| `log` | `log` | `log` |

### `result`

Small result returned inline.

| Field | Description |
|---|---|
| `code` | `"result"` |
| `id` | query id |
| `session` | session used |
| `command_tag` | Normalized command tag (`ROWS N` / `EXECUTE N`) |
| `columns` | column metadata array |
| `rows` | result rows |
| `row_count` | row count actually emitted (the prefix size when `truncated`) |
| `truncated` | optional; `true` when `rows` is a prefix of the full result |
| `truncated_at_rows` | optional; inline row cap that fired |
| `truncated_at_bytes` | optional; inline byte cap that fired |
| `trace` | timing and counters |

When `truncated: true`, the underlying SQL still executed in full. For
`UPDATE ... RETURNING`, this means the writes happened and the RETURNING
projection delivered to the agent is the first N rows. To collect the
full result of a read-only query, narrow it with `WHERE` or rerun with
`--stream-rows`. Never rerun a successful write just to recover truncated
RETURNING output. Choose `--stream-rows` before the first write when its full
result is needed; after truncation, use a separate `SELECT` with stable
business keys to inspect the current rows. That SELECT cannot necessarily
reconstruct old RETURNING values.

Bounded row results are produced through a CTE/JSON wrapper. PostgreSQL does
not guarantee that an outer read preserves ordering established only inside
that CTE, so callers must not treat the returned prefix as a contractual
ordered prefix—even when the submitted query contains `ORDER BY`. If exact
ordering is part of correctness, return an explicit ordinal/key and sort or
verify the rows in the caller.

### `result_start`

Start of streamed result.

| Field | Description |
|---|---|
| `code` | `"result_start"` |
| `id` | query id |
| `session` | session used |
| `columns` | column metadata |

### `result_rows`

One streamed row batch.

| Field | Description |
|---|---|
| `code` | `"result_rows"` |
| `id` | query id |
| `rows` | row objects for this batch |
| `rows_batch_count` | rows in batch |

### `result_end`

End of streamed result.

| Field | Description |
|---|---|
| `code` | `"result_end"` |
| `id` | query id |
| `session` | session used |
| `command_tag` | Normalized command tag (`ROWS N` / `EXECUTE N`) |
| `trace` | includes `duration_ms`, `row_count`, `payload_bytes` |

### `dry_run`

Emitted instead of executing the SQL when `--dry-run` is passed. The server
prepares the statement inside a transaction that is rolled back, so this also
validates table/column existence and placeholder counts without side effects.

| Field | Description |
|---|---|
| `code` | `"dry_run"` |
| `id` | optional client correlation id |
| `sql` | the SQL that would have been executed |
| `params` | the params that would have been bound, in JSON-encoded form |
| `session` | session that would have been used |
| `param_types` | inferred PostgreSQL types for `$1`, `$2`, ... in placeholder order |
| `columns` | output column metadata (empty for non-SELECT statements) |
| `trace` | timing and counters |

If preparation fails, `afpsql` emits `sql_error` (PostgreSQL diagnostic) or
`error` (placeholder-count mismatch / connect failure) with the same shape as
a normal query, and exits non-zero.

### `sql_error`

Database execution error.

| Field | Description |
|---|---|
| `code` | `"sql_error"` |
| `id` | query id |
| `session` | session used |
| `sqlstate` | SQLSTATE (`23505`, `42P01`, ...) |
| `message` | primary error message |
| `detail` | optional detail |
| `hint` | optional hint |
| `position` | optional SQL character position |
| `trace` | timing and counters |

### `error`

Client/runtime/protocol error.

| Field | Description |
|---|---|
| `code` | machine-readable code |
| `message` | human-readable detail |
| `sqlstate` | optional SQLSTATE when PostgreSQL rejects connection setup |
| `detail` | optional PostgreSQL detail for connection setup failures |
| `hint` | optional remediation hint |
| `retryable` | whether retry may succeed |
| `trace` | timing and counters (top-level sibling) |

Canonical `error.code` values:

- `invalid_request`
- `invalid_params`
- `connect_failed`
- `cancelled`
- `internal_error`
- `transaction_lost_rolled_back`
- `commit_outcome_unknown`

`internal_error` means the failure is `afpsql`'s own, not the request's and not
PostgreSQL's; the hint asks the caller to retry and then restart the session.
Treat it as a bug report, not as something to work around.

An invocation rejected before anything ran carries the parser's own
classification instead, always on stderr and always exiting 2:
`cli_unknown_command`, `cli_unknown_argument`, `cli_unregistered_combination`,
`cli_missing_argument_value`, `cli_invalid_argument_value`,
`cli_duplicate_argument`, `cli_unexpected_positional`, `cli_invalid_utf8`.

For connection setup failures, `kind` remains `"error"` and `error.code` remains
`"connect_failed"`. If PostgreSQL returns a server diagnostic during startup
(for example password auth failure, missing role/database, too many connections,
or cannot-connect-now), `afpsql` also includes `sqlstate` plus PostgreSQL
diagnostic fields and a SQLSTATE-specific `hint`.

### `session_info`

Response to a `session_info` request.

| Field | Description |
|---|---|
| `code` | `"session_info"` |
| `id` | optional client correlation id |
| `session` | resolved session name |
| `transport_kind` | `"direct"`, `"ssh"`, or `"container"` |
| `permission_default` | transport-default permission (`"read"`, `"ssh-read"`, or `"container-read"`) |
| `stream_rows_default` | session's default `stream_rows` value |
| `batch_rows` | resolved `batch_rows` default |
| `batch_bytes` | resolved `batch_bytes` default |
| `inline_max_rows` | resolved inline row cap |
| `inline_max_bytes` | resolved inline payload byte cap |
| `statement_timeout_ms` | resolved statement timeout |
| `lock_timeout_ms` | resolved lock timeout |
| `database` | optional PostgreSQL database name (from probe or config) |
| `user` | optional PostgreSQL role (from probe or config) |
| `host` | optional server host (from probe or config) |
| `port` | optional server port (from probe or config) |
| `server_version` | optional PostgreSQL server version (from probe) |
| `trace` | timing and counters |

If the probe SELECT succeeds during `session_info`, `database`/`user`/`host`/
`port`/`server_version` reflect what the PostgreSQL server itself reports.
If the probe fails (typically because connection setup itself fails), the
fields fall back to the resolved session config and `server_version` is omitted.
Probe failures do not cause `session_info` to error.

### Other output codes

| `code` | Meaning |
|---|---|
| `config` | full runtime config echo |
| `pong` | ping response with counters |
| `close` | shutdown acknowledgement |
| `log` | optional runtime diagnostic event (enabled by `log` config/categories) |

`log` event fields:

- `event` (e.g. `query.result`, `query.error`, `query.sql_error`,
  `transport.selected`, `mode.permission_default_changed`,
  `connect.libpq_env_fallback`)
- `request_id` (optional)
- `session` (optional)
- `error.code` (optional)
- `command_tag` (optional)
- `chain` (optional transport summary)
- `trace`

Startup `log` events include `version`, parsed/summarized `args`, and selected
environment fallback presence metadata (`key` plus `present`). They intentionally
omit raw `argv`, raw environment values, and config snapshots. Bind values are
summarized as `param_count`, not logged as plaintext.

`log` category matching (from `config.log` / `--log`):

- empty list disables `log` events
- `all` or `*` enables all categories
- exact match (`query.result`)
- group prefix match (`query` -> `query.*`)

`transport.selected` is emitted once when a new session connection is opened
and the `transport` log category (or `all` / `*`) is enabled. Its `chain`
summarizes the selected boundary, for example
`ssh:user@server -> docker exec pg -> tcp 127.0.0.1:5432`.

`mode.permission_default_changed` is emitted under the `mode` log category
whenever `--mode psql` bypasses the native read-only default, so agents can see
when psql-compat translation has dropped the write boundary.

`connect.libpq_env_fallback` is emitted under the `connect` log category when
libpq `PG*` environment variables (`PGHOST`, `PGPORT`, `PGUSER`, `PGDATABASE`,
`PGPASSWORD`, `PGSSLMODE`) fill connection fields that were not provided via
flags or secrets, listing which variables were used.

## Panel delivery (`--mode`)

Every `afpsql ui` panel — `schema`, `activity`, `plan` —
takes an optional `--mode window|session`. `window` opens an isolated browser
window on this machine; `session` registers the panel only, for a person who
is not here to reach with `afui host serve`. When `--mode` is absent,
AFUI selects `session` if the launch file names a registry and `window`
otherwise.

`--mode` on a panel is a different axis from `afpsql --mode cli|pipe|psql`:
the two never appear on the same command line, since `ui` has no runtime mode
of its own, and each is scoped to its own command in the registry.

The kit's third delivery, `link` — a URL another device can open directly,
with no `afui` binary involved in reaching it — is not offered here, and does
not appear in a panel's `--help`. `link` binds every interface and advertises
the result on the network with no tunnel; for rows read out of a database
that is a wider, unencrypted exit than `--stdout-file`, which an
administrator-locked readonly profile already refuses (see above) for the
same reason it refuses every panel outright, regardless of `--mode`. `window`
never leaves this screen; `session` stays on loopback and is reached only
through afui's own registry, so neither carries that exposure.

The `ui_ready` progress event every panel emits carries AFUI's own delivery
facts beside the panel's: `mode`, naming the delivery with the same word
`--mode` accepts, and `session_id`, the identity that panel
is listed under in `afui host list`. Which delivery reached a person
changes nothing about what `ui plan` accepts as an answer: only pressing the
approve control runs the statement, and a closed window, a remotely-refused
session, and a lapsed credential are all still a refusal. The `ending` field on a `ui plan` result reports `approved`
or `refused` for a decision and AFUI's own `closed` or `expired` for every
other way a panel stops.

`afpsql ui schema [--schema NAME] [--table NAME]` reads one atomic snapshot of
a schema: storage (table, index and TOAST bytes, live and dead rows, last vacuum
and analyze), foreign keys, and index health from the usage counters. The
panel is one page, top to bottom:

- **The schema's totals**: storage split and object counts.
- **Needs attention**, shown only when there is something to say: invalid or
  not-ready indexes, tables with at least 10,000 dead rows amounting to 20% of
  their live rows, tables of 1,000 rows or more that were never analyzed, and
  unused indexes of 1 MB or more, largest first.
- **Tables**, largest first: rows, size, index size and foreign keys out and in.
  This is the only place a table is chosen.
- **The opened table**, below the list once one is selected: its kind, owner,
  rows, storage split, last vacuum and analyze, a view's definition, and tabs
  for its columns, its keys and references (constraints, the tables it
  references, the tables that reference it), its indexes (each marked
  `unused`, `invalid`, `primary` or `unique`) and its triggers. Opening a
  table runs no further query.
- **Extensions and functions**, collapsed, when the schema has any.

`--table` opens the panel with that table already selected.

`afpsql ui activity [--all] [--refresh SECONDS]` is one live surface over a
single `pg_stat_activity` snapshot. It shows connection and wait counts, a filter
(`all`, `active`, `blocked`, `idle_in_tx`) and a table of sessions ordered by
how much they need attention: lock holders and waiters first, then idle
transactions past half of `idle_in_transaction_session_timeout` (five minutes
when the server sets none), then active sessions, then the rest. Selecting a
session shows its full statement and context. An idle-in-transaction
statement is labelled as the transaction's last statement, not a running one.
`Explain` plans the selected statement on demand with plain `EXPLAIN (FORMAT
TEXT)` on a separate read-only connection and never requests `ANALYZE`. A
statement with `$n` placeholders uses `EXPLAIN (GENERIC_PLAN)`, which needs
PostgreSQL 16 or newer and fails with `explain_generic_plan_unsupported` on an
older server; no parameter value is guessed. A statement PostgreSQL cut at
`track_activity_query_size` is refused with `explain_statement_truncated`.

`Cancel query` and `Terminate backend` signal the selected session through
`pg_cancel_backend` and `pg_terminate_backend`. Each needs two steps: the first
click only shows the target (`pid`, `user@db`, `state` and a statement
preview), and a second, matching confirmation runs it. The result PostgreSQL
returns (`true`, `false` or its permission error) is shown in the detail, sent
to the agent as a `ui_control` progress event, and followed by an immediate
refresh. The Provider enforces every rule: the panel's own connection is
refused with `invalid_request`, and a locked readonly profile, `--safe-mode` or
an `afpsql-readonly` binary refuses with `control_not_permitted` (the buttons
are also not drawn).

The activity panel queries PostgreSQL once before opening. While an AFUI viewer
is looking at the panel, it refreshes on the configured interval. Polling
pauses when all viewers are hidden or disconnected, or after `Pause`, and
resumes with an immediate refresh when someone returns or presses `Resume`.
`Refresh` fetches once even while paused. Connected clients without a
visibility report count as observing. The closing result reports successful
refreshes as `update_count`. A failed refresh writes only the error strip
(`/refresh_error`); the last good summary and rows stay visible.

`afpsql ui plan` also offers `Preview plan` for statements `EXPLAIN` can
describe (`select`, `with`, `values`, `table`, `insert`, `update`, `delete`,
`merge`). The read-only `EXPLAIN (FORMAT JSON)` preview shows operations,
estimated rows and costs as sortable values. It runs nothing, so it has no
actual rows or time. The statement still runs only on `approve`.

Approving does not close the window. The statement runs, its events go to the
agent at once, and the window replaces the decision buttons with the result:
the command tag, row count and duration, or the error, and the first 100 rows
drawn with the columns PostgreSQL described when the statement was prepared.
The window stays open until the person presses `Done` or closes it, and the
command exits then, with the same exit code `afpsql --sql` would have.

For interactive development, `scripts/test.sh panels window` opens all three
installed panels against a disposable PostgreSQL instance. Use
`scripts/test.sh panels session` inside a native host to register them there.
The test instance is removed on exit.

## Replacing a panel (`afui frontend`)

All built-in panels use the AFUI VIEW 1.0 catalog with `Table`, `CodeBlock` and `Disclosure`. A replacement projection writes view messages with no version and no catalog. AFUI resolves the trust
fingerprint, parses the frontend manifest, renders each JSON projection and
serves its own shell and renderer. afpsql builds typed PostgreSQL documents and
interprets the interactive panels' actions and the selections a person makes.

| `ui_kind` | Panel |
| --- | --- |
| `schema` | `afpsql ui schema` |
| `activity` | `afpsql ui activity` |
| `plan` | `afpsql ui plan` |

Initialize with `afui frontend init afpsql <KIND> --frontend-id custom --ui-api-version 5`.
Enable the workspace fingerprint with `afui frontend enable` after editing.
`--safe-mode` uses the built-in files. Read `ui_frontend_id` from `ui_ready`
to tell which frontend served the panel.

| Override | Purpose |
| --- | --- |
| `templates/surface.json.j2` | Project the panel document to an VIEW message array |
| `templates/*.json.j2` | Additional named JSON partials |
| `templates/page.html.j2` | AFUI shell with `#app` and `<!-- afui:trusted-runtime -->`. Supports label attributes and named Surface slots |
| `style.css` | Renderer style overrides. The default shell layers these over the built-in styles |
| `assets/app-icon.svg` | Application icon |

Each projection receives its panel document directly as `afui.document`.
Interpolate fields in JSON value positions, as in
`"text": {{ afui.document.subject }}`. Each expression emits a complete JSON
literal. The schema and activity panels use the document as the surface's data
model (`"dataModel": {{ afui.document }}`) and bind components to it with
`@path`; SQL NULL is JSON null. The panels ask the page to report edits a person
makes to the data model, and answer them by writing paths, so a projection that
binds a `Table` `selection` to the documented path gets its detail filled in.

Keep counts, seconds, bytes, and timestamps in their model fields. Use the
column's `text` to format values for the viewer. Built-in tables sort the
underlying values, keep the selection on the same row, and show exact
values on hover when the visible quantity is shortened. The activity
table reapplies the current sort after a refresh.

The schema panel's surface is `schema`. A `Table` with `rowKey: "name"` and
`selection: {"@path": "/selected_table"}` opens a table; the Provider writes
`/detail`.

| Path | Value |
| --- | --- |
| `/subject`, `/schema`, `/notice`, `/empty` | Endpoint, schema name, a requested table that was not found, and the sentence for an empty schema |
| `/overview` | `total_bytes`, `table_bytes`, `index_bytes`, `toast_bytes`, and the object summary |
| `/tables` | `name`, `kind`, `live_rows`, `total_bytes`, `index_bytes`, `fks_out`, `fks_in` |
| `/attention` | Finding code, index name and size, live and dead row counts, and last vacuum timestamp |
| `/extensions`, `/routines` | Installed extensions, and functions with `name`, `summary`, `definition` |
| `/selected_table`, `/detail` | The selected table, and its storage, columns, constraints, `fks_out`, `fks_in`, indexes and triggers |

The activity panel's surface is `activity`. A `ChoicePicker` bound to
`/filter` chooses what the table keeps, and a `Table` with `rowKey: "pid"` and
`selection: {"@path": "/selected_pid"}` selects a session. A refresh writes only
the paths it owns, never `/filter` or `/selected_pid`.

| Path | Value |
| --- | --- |
| `/subject`, `/summary`, `/snapshot_rfc3339`, `/refresh_seconds` | Endpoint, connection and wait counts, snapshot time, and refresh cadence |
| `/refresh_error`, `/paused` | The last refresh failure, and whether automatic refresh is paused |
| `/filter`, `/selected_pid`, `/can_control` | Chosen filter, the selected session, and whether Cancel and Terminate may be offered |
| `/rows` | Sessions with `pid`, `state`, `blocked_by`, `blocking`, `wait`, `tx_age_s`, `query_age_s`, `user_db`, `app`, `query_preview`, `query_full`, `client_address`, `backend_type`, `is_self` and `severity` |
| `/waits` | Wait event objects with `label` and `count` |
| `/detail` | The selected session: context fields, `query_full`, `idle_in_tx`, `is_self`, `ended`, `plan`, `result` and `confirm` |

Actions on surface `activity`: `refresh`, `toggle_pause`, `explain` (with
`context.pid`), `control_request` (with `context.kind` of `cancel` or
`terminate` and `context.pid`), `control_confirm` (same context) and
`control_abort`. The Provider reads the statement and the target from its own
snapshot, never from the action payload.

The plan projection displays the frozen statement, transaction mode and
parameters. Put `refuse` and `approve` buttons on surface `decision`, and
optionally a `preview` button whose plan arrives at `/preview` (offered only
when `/previewable` is true). Plan rows contain `depth`, `operation`, `relation`,
`startup_cost_units`, `total_cost_units` (both in planner cost units) and
`estimated_rows`. Only
the `approve` action can authorize execution. Any SQL or parameters in its
action payload are ignored, since afpsql already holds the exact prepared
request. An unknown action is rejected. Refusing, closing, or losing the
session before approval never authorizes the statement.

Once the statement has run, `/result` holds `command_tag`, `row_count`,
`duration_ms` and `rows` (at most 100), or `error`, and a `note` when rows were
left out. `/result_columns` in the initial data model names the columns those
rows carry. A `done` action ends the window. Any `approve`, `refuse` or
`preview` after the run is rejected with `ui_plan_already_ran`; nothing runs
twice.

A frontend with invalid JSON or catalog messages fails before opening.
A surface has to fit in a runtime frame, including its replayable state. Use
the corresponding `afpsql inspect` command for results larger than the panel
can display.

## Runtime Safety Limits

Pipe mode applies hard protocol limits before executing a request:

- max JSONL line: 1 MiB
- max SQL text: 1 MiB
- max params per query: 65,535
- max queued/running query ids: 64

## Environment Fallback

Optional runtime fallback variables:

- `AFPSQL_DSN_SECRET`
- `AFPSQL_CONNINFO_SECRET`
- `AFPSQL_HOST`
- `AFPSQL_PORT`
- `AFPSQL_USER`
- `AFPSQL_DBNAME`
- `AFPSQL_PASSWORD_SECRET`
- `AFPSQL_SSH`
- `AFPSQL_SSH_VIA`
- `AFPSQL_SSH_REMOTE_SOCKET`
- `AFPSQL_SSH_SUDO_USER`
- `AFPSQL_CONTAINER_DOCKER_NAME`
- `AFPSQL_CONTAINER_DOCKER_USER`
- `AFPSQL_CONTAINER_DOCKER_CONTEXT`
- `AFPSQL_CONTAINER_DOCKER_RUNTIME`
- `AFPSQL_CONTAINER_APPLE_NAME`
- `AFPSQL_CONTAINER_APPLE_USER`
- `AFPSQL_CONTAINER_APPLE_RUNTIME`
- `AFPSQL_CONTAINER_PODMAN_NAME`
- `AFPSQL_CONTAINER_PODMAN_USER`
- `AFPSQL_CONTAINER_PODMAN_RUNTIME`
- `AFPSQL_CONTAINER_NERDCTL_NAME`
- `AFPSQL_CONTAINER_NERDCTL_USER`
- `AFPSQL_CONTAINER_NERDCTL_RUNTIME`
- `AFPSQL_CONTAINER_COMPOSE_SERVICE`
- `AFPSQL_CONTAINER_COMPOSE_USER`
- `AFPSQL_CONTAINER_COMPOSE_FILE` (colon-separated)
- `AFPSQL_CONTAINER_COMPOSE_PROJECT`
- `AFPSQL_CONTAINER_COMPOSE_RUNTIME`
- `AFPSQL_CONTAINER_KUBECTL_POD`
- `AFPSQL_CONTAINER_KUBECTL_CONTAINER`
- `AFPSQL_CONTAINER_KUBECTL_NAMESPACE`
- `AFPSQL_CONTAINER_KUBECTL_CONTEXT`
- `AFPSQL_CONTAINER_KUBECTL_RUNTIME`

Variables from two driver families are rejected the same way flags from two
families are: they name two drivers.

An explicit DSN, conninfo, host, port, user, or database means the caller chose
the target, so `AFPSQL_DSN_SECRET` and `AFPSQL_CONNINFO_SECRET` are then not
read; the per-field fallbacks below still fill fields left unset.

Administrator-locked readonly profiles ignore every connection, SSH, and
container environment fallback.

Standard PostgreSQL environment fallback (lower precedence):

- `PGHOST`
- `PGPORT`
- `PGUSER`
- `PGDATABASE`
- `PGPASSWORD`
- `PGSSLMODE` (`disable`, `prefer`, or `require`)

## Example: Small Result

Input:

```json
{"code":"query","id":"q1","sql":"select 1 as n"}
```

Output:

```json
{"kind":"result","result":{"code":"result","id":"q1","command_tag":"ROWS 1","columns":[{"name":"n","type":"int4"}],"rows":[{"n":1}],"row_count":1},"trace":{"duration_ms":2}}
```

## Example: Streamed Result

Input:

```json
{"code":"query","id":"q2","sql":"select * from big_table where id > $1","params":[100],"options":{"stream_rows":true,"batch_rows":1000}}
```

Output:

```json
{"kind":"progress","progress":{"code":"result_start","id":"q2","columns":[{"name":"id","type":"int8"},{"name":"name","type":"text"}],"message":"query result stream started"},"trace":{}}
{"kind":"progress","progress":{"code":"result_rows","id":"q2","rows":[{"id":101,"name":"a"},{"id":102,"name":"b"}],"rows_batch_count":2,"message":"query result rows"},"trace":{}}
{"kind":"result","result":{"code":"result_end","id":"q2","command_tag":"ROWS 200000"},"trace":{"duration_ms":443,"row_count":200000,"payload_bytes":34199211}}
```
