# Keel: what pic builds and what it hosts

Status: adopted 2026-11-21; first seam (context policy) cut 2026-11-22; RPC command registry cut
2026-11-22; provider registration cut 2026-11-22

The full study, with the sorting test, the hard cases and the first audit, is at
https://artifacts.craftdesign.group/v/7AcFU37QOBvcGyD3TEMpp#8b97a2dfe17cd12d8d19f2b890bd60dc80e77ed6cfa7b22a13851a27c01d9630.
The manifest and its checks are in [tiers](../dev/tiers.md).

## The principle

Core records and enforces; components decide. Core holds the mechanisms that make an agent
durable, recoverable and observable. It holds no decision whose right answer depends on a model, a
vendor's API or price list, or the current way of prompting. A decision is journaled before it takes
effect, recovery reads the record and never asks the component again, and core runs with no
components.

## Tiers

- **core**: every build has it; changes when durability, SQLite or the operating system require it.
- **bundled**: ships and is on by default, reaches core only through a published seam, can be turned
  off or replaced.
- **external**: a separate tool with its own release schedule, speaking hooks, RPC or the extension
  host.
- **data**: rows or files with no code (catalogs, prices, prompt text, themes, keymaps), updated
  without a release.

`main.c` is the composition root and may name everything. The terminal interface (`src/tui/`) is a
client.

## The component ladder

Lowest rung that works: data, then a hook, then a protocol client, then a C interface, then the
extension host.

## The tier-line rule

A capability is not core by default. Every ticket or design note that adds one carries a line:
`Tier: ... Clock: ... Record: ... Default: ...`. A `Tier: core` line needs a design note in this
directory with a status line. A new seam goes to the owner; a new component on an existing seam
does not. Tiers move both ways, by a recorded decision.

## The first seam: context policy

Status: landed 2026-11-22. Tier: core (`src/policy.h`). Clock: durability. Record: a
`pic_hook_run` row per answer. Default: the built-in compaction component (`src/compact.c`).

Before each reply request core asks one question, PreRequest, through the hook host: the facts it
owns go in (model, context window, cache lifetime, idle seconds, first request of the submission
or not), and the answer is "compact
before this request in MODE, for REASON" or nothing. Core journals the answer before it takes
effect, asks the component for that mode's plan, and runs it: a summary's requests are journaled
and retried as before; a trim commits in one transaction. The first answer for a turn stands, so
recovery and retries never ask again. loop.c no longer includes `compact.h` and decides nothing
about compaction: Pi's threshold, overflow recovery's switch, lapse detection, the floor and the
holdout draw are all the component's. [Durability](../dev/durability.md) has the invariants.

### How settings are scoped

A setting belongs to the tier that decides with it.

- Core reads only settings of mechanism: leases, retries, the request cap, the trial label.
- A component reads its own policy settings from the user database (`compaction.*`,
  `lapseHoldout`, `lapseMode` in compact.c). Core never reads them and `pic_store` does not carry
  them.
- A setting that also has a flag or a variable (`lapseCompaction`, `lapseSeconds`,
  `lapseFloorTokens`) is resolved in the composition root, which may name everything, and handed to
  the component (`pic_lapse_configure`); main.c also rejects a bad value at startup.
- A decision journals what it decided with (the arm, the mode, the idle seconds, the tokens), so a
  changed setting never changes a recorded decision and recovery reads no setting.

### What it implies for the MCP seam (not built)

An out-of-process component could answer the same question: JSON facts in, a reason and a JSON
context out, asked outside any transaction, journaled before it takes effect. Three things stand
in the way today. The in-process answerer gets the journal's handle, which it uses for Pi's token
estimate; an external one would need the estimate among the facts. Planning is in-process C over
the journal, so an external component can only name a mode a built-in can plan, unless the plan
becomes data (the first kept entry and the text, or the prompts), which `pic_compact_plan` nearly
is. And the question blocks every request, so an external answerer needs a deadline whose default
is no answer, as command hooks have.

The check that core stands alone is now real: check 5 in [tiers](../dev/tiers.md) links the core
files with the vendored libraries alone, and `ci/tiers.stubs` lists the bundled symbols core still
takes, each a seam not yet cut.

## The second seam: the RPC command registry

Status: landed 2026-11-22. Tier: core (`src/rpc.h`). Clock: pi. Record: none; a command that
changes the journal records what it changes as before. Default: `src/listings.c` answers
`get_available_models` (the catalog and the models file) and `get_commands` (templates and
skills); `src/rewind.c` answers `rewind`.

A component declares its commands (name, help, flags, an answer function) and the composition root
hands them to the RPC server, which dispatches by name and owns the response, so every command
keeps Pi's shape. rpc.c no longer includes `catalog.h`, `compact.h`, `prompts.h` or `skills.h`.
Compaction did not need a command: `compact` is core's mechanism (`pic_compact_now`), and what
rpc.c read from compact.c, whether compaction is automatic for `get_state` and the token estimate
for `get_session_stats` and `compact`'s result, now comes through the context policy
(`automatic`, `tokens` in `src/policy.h`). The allowance fell from 29 to 25 lines and the stubs
from 29 to 20. [Architecture](../dev/architecture.md) says how a component registers a command.

The same registry is how the terminal interface could reach components later. Most of its
allowance lines (`src/tui/app.c` including `compact.h`, `models.h`, `prompts.h`, `skills.h`,
`procs.h`, `hooks.h`) say "needs an RPC command": each would become a registered command the client
asks over Pi's protocol, as `get_commands` and `get_available_models` already are. That seam is not
cut here.

## The third seam: provider registration

Status: landed 2026-11-22. Tier: core (`src/provider_http.h`). Clock: durability. Record: none new;
a reply is journaled in both forms as before. Default: the four bundled adapters
(`src/provider_anthropic.c`, `provider_chat.c`, `provider_responses.c`, `provider_gemini.c`),
registered by the composition root.

An adapter declares its API, how to open a provider for a model (request building and stream
decoding stay behind `pic_provider`), how long a long-retention cache lives, its tool-call id rule
and the content shapes its requests name. Core looks the adapter up by the model's API, asks it for
the cache lifetime, and the transform layer's `pic_norm_id` and `pic_content` dispatch to its rule
and shapes. What stays core is mechanism: the shared POST and its retries, the key's attachment, the
cacheRetention setting with short (300 seconds), none, and an hour for long where the adapter does
not say, the default id rule, Pi's own shapes, and the image budget. The Messages API's conversion
to the neutral form moved into its adapter; the scripted provider now replies in the neutral form
itself, so core runs with no adapter at all, and the version-3 migration keeps a frozen copy of the
conversion it was written against. The allowance fell from 25 to 14 lines and the stubs from 20 to
16. [Provider adapters](../dev/providers.md) says how to add one.

Refusal classification did not move: its rows are keyed by provider, not by wire API (OpenRouter
and OpenAI share an API and differ in what a 402 means), and they already reach core through the
credential seam (`pic_cred_health_ops`). The adapter keeps a `probe` slot, NULL for now, where the
next landing puts free key checks (a provider's key or balance endpoint, else a models-list call).
`pic.h` still includes `models.h` for `pic_model`, which many core files use through it, and
`loop.c` still prices replies from the registry; both wait for catalog rows.
