Artifact b1c710ec0526366e1f344f2b5eab57f6c8f1ef2b7697686de322f6662d33a5aa:
- File docs/design/keel.md — part of check-in [758f7748e3] at 2026-11-23 17:10:22 on branch keel-wrap — Keel complete: the design note and the architecture say what the model and client seams are, what remains (the client's local unlock, the frozen migration's name) and the nine stubs keel did not plan (user: dmestas, size: 11620) [annotate] [blame] [check-ins using] [more...]
Keel: what pic builds and what it hosts
Status: complete 2026-11-23. Adopted 2026-11-21; context policy, RPC command registry and provider registration cut 2026-11-22; the model seam and the client seam cut 2026-11-23. Two allowance lines remain, each for a reason that holds (below), and nine core stubs name three seams keel did not plan: prompt assembly, a tool registry and export.
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.
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 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,lapseModein compact.c). Core never reads them andpic_storedoes 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 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 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 was
cut later; see the fifth seam.
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
- Provider adapters 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 then still included models.h and loop.c priced replies from the registry; the fourth
seam ended both.
The fourth seam: the model source
Status: landed 2026-11-23. Tier: core (src/model.h). Clock: pi. Record: none new; a reply's
price is journaled with its usage as before. Default: src/models.c, registered by the composition
root.
Core owns the model a request uses (pic_model) and Pi's thinking-level rules over it, and asks
the registered source two questions: what a name means (provider, wire API, endpoint, key, limits,
dialect switches, price) and what a reply costs. The built-in providers, the catalog, the models
file, compat detection and the cost formula stay in the source; core names none of them. The wire
API is now a name (Pi's, as models name it) rather than an enumeration, so adapters register under
it and no core header names a vendor. With no source, nothing resolves and replies are journaled
unpriced. pic.h and loop.c no longer include models.h: the allowance fell from 14 to 12 and
the stubs from 16 to 9.
The fifth seam: the client asks commands
Status: landed 2026-11-23. Tier: core (src/rpc.h, src/rpc_core.c). Clock: pi. Record: none; a
command that changes state records what it changes as before. Default: the terminal interface.
The terminal interface reaches components and the user's state only by asking commands: the
composition root hands it ask (pic_tui_options), which answers Pi's RPC JSON in the interface's
process with the same registry pic --mode rpc uses (pic_rpc_answer). Each answer lives with its
owner: help and the changelog in help.c, templates, skills, providers, instruction files and a
folder's files in listings.c, compaction settings in compact.c, settings, trust, the Ctrl+P scope,
bug reports, phones, hooks and process records in core (rpc_core.c). The @file walk moved into
ignore.c behind list_files. Check 8 in tiers links core and the client alone
and finds nothing the client takes from a component. The allowance fell from 12 to 2.
What it enables (not built): every one of these is an RPC command, so another client (an editor
plugin, a phone, a second terminal) can show the same help, settings, file completion, providers
and templates the built-in interface does, and the interface could later run against a pic --mode
rpc child instead of in process.
What remains, and why
src/tui/app.cincludescred.h. The credential store's unlocked key lives in the process that asked for the passphrase and is handed to its children by descriptor, so unlocking stays in the interface's process. The login and key panels also read key health and its words from cred.c directly;get_credentialscarries most of those facts and could carry the rest, which would leave only the unlock.src/store.cnames Anthropic in the frozen version-3 migration, kept until that step is retired.- Nine stubs:
rpc.cexports through export_html.c and export_pi.c andhooks.chands hooks the Pi line (an export seam, or export_pi.c becoming core);loop.cbuilds the system prompt through context.c (a prompt-assembly seam);loop.candtoolpar.crun the search tools directly (a tool registry). None was in keel's plan; each is named inci/tiers.stubs.