Artifact ffc487e627a940622680ecee956f20883cfe64cd50d58f9dbf288c742ec276a9:
- File
docs/dev/architecture.md
— part of check-in
[8f2d30d39e]
at
2026-11-22 20:18:02
on branch keel-rpc
— Cut the RPC command registry seam
Components register RPC commands (name, help, flags, answer) and main.c wires them; rpc.c dispatches by name without including catalog.h, compact.h, prompts.h or skills.h. get_available_models and get_commands move to src/listings.c; rewind uses the new call shape. Whether compaction is automatic and the token estimate come through the context policy (automatic, tokens). Allowance 29 -> 25, stubs 29 -> 20. (user: pi-c-agent, size: 6682) [annotate] [blame] [check-ins using] [more...]
# Architecture ## State pic's state lives in SQLite: the journal per workspace (`WORKSPACE/.pic/journal.sqlite`, `pic_*` tables) and `~/.pic/pic.sqlite` (`picu_*` tables) for settings, the model catalog, credentials, trust decisions, process records and the conversation lines that ctx archives. Several pic processes share a journal, one conversation each; [durability](durability.md) says what is committed when and who may work a conversation. ## The operating system seams What touches the operating system sits behind two interfaces: `pic_platform` (clocks, entropy, the locks, input files, the SQLite VFS) and `pic_executor` (file operations and running commands). Host-only code stays behind them, which is what makes the targets in [where pic can run](../manual/platforms.md) reachable. ## The terminal interface The terminal interface is a module in the pic binary (`src/tui/`). It runs each request as a child `pic --json` process and knows the run only through Pi's JSON events, so a fault in the interface cannot touch a run. ## The RPC command registry `pic --mode rpc` (`src/rpc.c`, core) answers Pi's session commands itself and hands the rest to components by name, without including their headers. A component declares a `pic_rpc_command` (`src/rpc.h`): the command's name, a one-line help, flags, and an `answer` function over a `pic_rpc_call`. `PIC_RPC_JOURNAL_FREE` answers with no journal open; `PIC_RPC_IMMEDIATE` answers at once while a request runs instead of after it. The call carries the journal (or NULL), the conversation id, the model, the workspace and the command line; `answer` returns 0 with `data` (the response's JSON, or NULL), or -1 with `err`, and sets `switched` and `next` to move to another conversation. The response, its `id` and the errors are core's, so a command keeps Pi's shape whoever answers it. To add one: declare the entry in the component (`pic_rewind_rpc` in `src/rewind.c`, `pic_models_rpc` and `pic_commands_rpc` in `src/listings.c`), add it to `rpc_commands` in `src/main.c`, and give the command its row in docs/reference/rpc-commands.md; `make test-docs` finds the entry by its `pic_rpc_command NAME = {"type"` declaration. A name core answers itself cannot be taken. Core asks the registry too: `set_model`, `cycle_model` and `get_state` read the model list from whatever answers `get_available_models`, and with no component the list is empty. ## Where the manual's facts are kept - Lapse compaction: `pic_generation.reason` (`lapse`), `idle_seconds` and `context_tokens`. Every cold cache with lapse compaction on is the context policy's PreRequest answer, a `pic_hook_run` row (reason `lapse`, `ref` the turn, always 0: a lapse is only answered before a submission's first request) whose context carries its arm (`compact`, `holdout` or `small`, under the floor), mode (`trim` or `summary`) and calibration. The first answer for a turn stands. `pic usage --lapse` sums each answer's window: the done requests from it to the next in the session, by mode. Journals from before the seam keep their `pic_lapse` rows, read as summary mode. - Imported usage (`pic import --claude`): the `usage` of the reply's `pic_generation` row, as a native reply's (`input_tokens`, `cache_read_input_tokens`, `cache_creation_input_tokens`, `output_tokens`) plus `model` and `"imported":true`, with no `pic_tokens` or `pic_cost`. Lapse calibration and the context estimate read it like any reply's; `pic usage --by-trial` skips rows with `imported` (its token sums fall back to the raw fields, so the marker is what keeps them out of spend), and cost sums read only `pic_cost`. A message id Claude Code wrote several times gives one row, each field the largest seen; absent or non-integer fields are left out, and a reply with none has no usage. It is written after the import's own chars/4 estimate, so that figure stays a pure estimate. No schema change: `imported` is a key of the usage JSON. - Trial labels: `pic_submission.trial`. - The Pi lines hooks read: `picu_pi_session_line` in `~/.pic/pic.sqlite`. - Process records (`pic ps`): `picu_proc` in `~/.pic/pic.sqlite`. - Skill use, for ranking the skill list under its budget: `picu_skill_use(name, path, uses, last_ms)` in `~/.pic/pic.sqlite`, keyed by the skill's name and the path it was discovered at. skills.c adds a row on `/skill:NAME` and, through the tools' read observer (`pic_tool_read_observer`, wired in main.c), on a read of a skill's file. The write waits 250 ms at most and its failure is ignored. The score is `uses x max(0.5^(days since last_ms / 7), 0.1)`. The listing is built by skills.c, which reads `skillBudget` itself; core hands `pic_system_prompt` only the model's context window. ## The manual in the binary `src/manual.inc` holds docs/manual and docs/reference as C strings, built by `tools/manual-inc.sh`; `src/help.c` serves them to `pic help` and the terminal's `/help TOPIC`. They cost about 65 KB of the binary. The text is a constant in the image, so it works with no filesystem. ## The credential store Each stored key is sealed with ChaCha20-Poly1305 (the provider's name bound as associated data), so no plaintext key reaches the database, its WAL, a backup or a log. The sealing key comes from a passphrase, stretched with PBKDF2-HMAC-SHA256 (600,000 iterations, a random salt; both kept in `picu_credential_key` with a verifier, so a wrong passphrase is refused cleanly). What it protects against is in [credentials](../manual/credentials.md). A subscription sign-in is a row of kind `oauth`: a JSON object (`access`, `refresh`, `expires` in ms, and whatever else its flow keeps, such as OpenAI's issued `clientId`), sealed whole like a key. Core (`cred.c`) knows only that shape; the flows are bundled files (`oauth_*.c`, over `oauth.c`) that the composition root registers through `pic_cred_oauth_register`, so core names no provider and links without them. A refresh, before expiry (within a minute) or after a 401 (once per request), runs under an exclusive lock on `~/.pic/credential.lock`, not a database transaction, so no writer of `pic.sqlite` waits on the network. Inside the lock the row is read again: when another process already replaced the refused token, its token is used and the refresh token, which may rotate, is not spent twice. A `picu_credential` made before the `oauth` kind is rebuilt once with the wider CHECK. ## The behavioural reference `reference/pi.lock.json` pins the Pi revision used as the behavioural reference. Pi's source is not part of this repository. The decisions in force are in [the implementation plan](../design/implementation-plan.md).