Artifact 269f35f999ef1ff98c0062d0a42550ba6614904091cd1e321973f3579b8d75af:
- File docs/dev/architecture.md — part of check-in [f29b9f9c7f] at 2026-11-22 14:05:42 on branch import-usage — Record a Claude Code import's per-reply usage, as imported, so lapse calibration works on a resumed conversation (user: dmestas, size: 4329) [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 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.
Where the manual's facts are kept
- Lapse compaction:
pic_generation.reason(lapse),idle_secondsandcontext_tokens. Every cold cache with lapse compaction on is the context policy's PreRequest answer, apic_hook_runrow (reasonlapse,refthe turn) whose context carries its arm (compact,holdoutorsmall, under the floor) and mode (trimorsummary). The first answer for a turn stands.pic usage --lapsesums each answer's window: the done requests from it to the next in the session, by mode. Journals from before the seam keep theirpic_lapserows, read as summary mode. - Imported usage (
pic import --claude): theusageof the reply'spic_generationrow, as a native reply's (input_tokens,cache_read_input_tokens,cache_creation_input_tokens,output_tokens) plusmodeland"imported":true, with nopic_tokensorpic_cost. Lapse calibration and the context estimate read it like any reply's;pic usage --by-trialskips rows withimported(its token sums fall back to the raw fields, so the marker is what keeps them out of spend), and cost sums read onlypic_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:importedis a key of the usage JSON. - Trial labels:
pic_submission.trial. - The Pi lines hooks read:
picu_pi_session_linein~/.pic/pic.sqlite. - Process records (
pic ps):picu_procin~/.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:NAMEand, 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 isuses x max(0.5^(days since last_ms / 7), 0.1). The listing is built by skills.c, which readsskillBudgetitself; core handspic_system_promptonly 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.
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.