Skip to content
pi-c Artifact [00a4e33178]
Login

Artifact [00a4e33178]

Artifact 00a4e33178c98deca35dcc62ed3c5a76a24a509bd6d1a1026918cbd1b6fc368d:


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 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 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

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.

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.

Named keys and their health

A provider's extra keys are rows named provider:label, sealed under key:provider:label; the row named provider stays its default. Which one a request uses (cred.c, cred_choose): the label pic_cred_override set for the process (--key), else the setting activeKey:PROVIDER, else the provider's row, else its oldest named row, else the models file's literal, else the variable; env and file name the last two. A chosen label the provider lacks is an error, never a fall back.

picu_credential_health(name, state, reason, status, until_ms, checked_ms, remaining) keeps, per credential name, the last thing a reply said: ok, invalid, credit or limited (transient and other failures are not written). checked_ms of a failure seen again keeps its first time. A variable's name is env:VAR: and twelve hex digits of HMAC-SHA256 over its value under a random per-install salt (picu_meta.credential_salt); recording one value's state drops the others' of the same variable. No key text is stored.

http.c keeps the anthropic-ratelimit-* and x-ratelimit-* header lines of a response; provider_http.c hands a refusal's status, body, message and those lines to pic_cred_classify, which asks the registered pic_cred_failure_ops (keyhealth.c: vendor wording as data rows) or falls back on the status. A 200 records ok. Only a transient class is retried. The store carries the credential and label each request used (s->credential), and the loop writes it to pic_generation.credential with the attempt's outcome (schema 18).

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.