# components The component loader that replaces the closed `extension.Plugin` type, `Managed` and per-session composition. It borrows the vocabulary of Cordis ([arXiv 2608.25512](https://arxiv.org/abs/2608.25512)) but is not reactive: a composition is built in one pass and a changed selection builds a new one. The interface is `src/albedo/component.gleam` with `component/key.gleam` and `component/erased.gleam`; the executable specification is `test/harness/component_test.gleam`. ## goal The core is a component loader. Every capability, python included, is a component that owns keys, provides to them and injects from them. An extension owns only what it contributes. The core knows no plugin kind, no kernel and no tool list. ## vocabulary - Component: a name, the keys it owns, the exclusive keys it injects and provides, and an `activate` function. - Key: a typed name `owner/name`. Exclusive: at most one provider bound per scope. Collection: many providers, one entry each. - Scope: what closes a composition. The daemon scope is a root; each session's scope is composed under it. - Fiber: what `activate` is given: the injected bindings it began with and a table its registrations are staged in. - View: what a composition held when it was built, immutable, read through its parent's published view. - Effect: a registration or acquisition made during activation, with its inverse. - Installation: durable, process-independent work (tables, migrations, row cleanup, settings declarations, reaping). It is not an effect. ## keys ```gleam pub opaque type Key(a, shape) // shape is Exclusive or Collection key.exclusive("python", "kernel") // Key(Kernel, Exclusive) key.collection("turn", "tools") // Key(Tool, Collection) ``` The brief asks for `Key(a)`. The design adds a second phantom parameter for the shape, so `get` on a collection and `entries` on an exclusive key do not compile. The runtime sees the shape as a value in `key.Declared`. A key name is `owner/name`. Only the component named `owner` may declare it, in its `keys` field. `compose` refuses a component that declares another owner's key (`ForeignKey`), declares one name twice (`KeyDeclaredTwice`), or names a component already in the scope (`DuplicateComponent`). Because the owner is the namespace, two components cannot collide on a key unless one of them lies about its owner. The single unchecked cast lives in `component/erased.gleam`: a scope stores bindings of many types in one map, `erase` forgets the type on the way in and `restore` recovers it on the way out. It is sound only if one key name always carries one type. The constructors `key.exclusive` and `key.collection` are public, so the type system alone does not hold that: any module could build a `Key(Int, Exclusive)` named like a `Key(Tool, Exclusive)`. The lint rule `albedo_component_keys` (`tools/lint`, run by `test/quick.sh` and `test.sh`) holds it. Every key is built at exactly one call site in `src/` and `test/`, as the result of a function whose return annotation is a concrete `Key(Type, Shape)`, with the owner and name as string literals; a second call site naming the same `owner/name` is reported at both sites. Only modules under `src/albedo/component` may import `albedo/component/erased`. `test/harness/albedo_lint_test.py` feeds the rule a forged second key and an import from outside the component modules, and both are reported. This is the same confinement `shared.gleam` gives `persistent_term`, enforced by the linter instead of by visibility. An exclusive key holds one binding per scope. `component.read(view, key)` returns `Option(a)` from the view of a scope, which includes the nearest ancestor binding. A component reads an injected key with `component.get(fiber, key)`, which returns the binding the activation began with (the committed view, paper 5.1.3). A key it did not inject reads as `None`. A collection key holds entries identified by a string id. `component.add(fiber, key, id, entry)` registers one. An id taken by another component refuses the add at commit and fails the whole activation. `component.entries(view, key)` returns the entries of the ancestors first, then the scope's own, each scope in load order and then registration order. For that reason a collection injection does not gate activation and a collection key cannot appear in `injects` or `provides` (`NotExclusive`). A view never changes; a consumer that needs a different one asks for a new composition. ## effects Every registration an activation makes has an inverse, and the loader records it. `provide` and `add` stage their registration and the loader publishes the whole stage when the activation commits. `acquire(fiber, fn() { Ok(#(value, inverse)) })` runs the function immediately, records the `Inverse` it returns and answers the value, for things outside the scope's maps: a connection, a process, a file. An authored `close` function does not exist. An `acquire` must be atomic: when it returns an error nothing is left, and a resource it creates is tied to the activation (a linked process, not a detached one). A resource that can outlive the daemon process is acquired with a durable inverse, described under durable state. `close` runs the inverses of the whole composition last-in first-out. Each inverse runs at most once, even when `close` is called again or from two processes (an atomic flag). Each runs under `protect`, so an inverse that raises stops nothing: the rest still run and the failure is reported. This keeps today's rule that rejected resources close exactly once in reverse order. An activation that returns `Error`, raises, dies, or whose commit is refused, runs the inverses of everything it staged and acquired. The component ends `Failed(reason)` with nothing behind. One component is one atomic bundle: this is today's whole-extension activation without a separate mechanism. ## composition `component.compose(parent, components)` builds a composition and answers `Composed(scope, view, refused)`. 1. Admission, in declaration order. A component is refused (`refused`, with a `Refusal`) when its name is taken, it declares a key another component owns or a key already declared, it injects or provides a collection key, or its injects and provides close a dependency cycle with the components accepted before it. 2. Activation. A component is activated once every exclusive key it injects is bound by a committed component or by the parent's view; the first such component in declaration order goes first, and each commit can make others ready. The activation runs in a process of its own under `protect`, so a raise or a kill is a failed activation and not the loader's. Its registrations are staged in an ETS table the loader owns. 3. Commit. The staged registrations are checked (a binding the component does not provide, an entry under an undeclared or exclusive key, an id staged twice, an empty id under a dotted key), then against what is committed. An earlier-declared holder of a name wins: the later component fails with a `Collision` naming the key, the id and the holder, and its inverses run. A later-declared holder (one that activated first because the earlier component waited for a key) is rolled back together with everything committed after it, and activates again to find the name taken; the rollbacks are bounded. 4. The view. Entries read in declaration order of the component that registered them, then registration order; every component has a `Member` (`Active`, `Failed(reason)` with its collision, or `Inactive` with the key names it still waits for). A component whose injected keys are never bound stays `Inactive` without error. Nothing activates later: a changed selection is a new `compose`, and the session closes the composition it replaces after the new one is in place. ## scopes A root composition (the daemon's) publishes its view once in a `Shared` and `component.published` reads it without copying it. A session composition is built with `Some(parent)`: its view holds only its own entries and reads through the parent's published view, so a session's view copies none of the daemon's. Closing a session's scope leaves the daemon's keys bound; closing the daemon's releases its published view. ## durable state and installation Tables, migrations and per-session row cleanup are outside the effect boundary (paper 6.1). They belong to installation and survive a component's deactivation. The transcript is never reverted. ```gleam Installation( name, description, requires, initialise: fn(Store) -> Result(Nil, String), // tables migrate: List(Migration), // schema and data clean: Option(Cleaner), // rows deleted with a session transfer: Option(transfer.Adapter), // durable state a move to another daemon carries settings: Option(settings.Declaration), // listed even while disabled reap: fn(Store) -> Nil, // see below component: Component, ) ``` `extension.install` keeps its role (quarantine of a bundle whose initialiser, schema or requirements fail, with the reason in the catalog). It runs `initialise`, schema migrations and `reap` once per installed entry at boot, in registry order, before any scope activates anything. Disabled extensions still upgrade their storage and still clean rows, so these fields never go through activation. An extension that wants only a component and no durable state supplies a plain `Installation` with no-op fields. Reaping is the seam for effects that outlive the process. A detached python kernel is acquired with `effect`, whose in-memory inverse kills it, and the same acquire writes a `kernel_links` row. If the daemon dies, the in-memory inverse never runs, and `reap` at the next boot reads `kernel_links` and kills what it finds, which is the durable inverse. v1 leaves this entirely to the python component, which supplies its own `reap`. The runtime offers the field and the guarantee that `reap` runs before any activation. ## dependencies and bundles An extension is one component. Its `requires` become injects of an exclusive key `/active`, bound to `Nil` by the required extension's own activation as its last registration. A dependent therefore stays inactive until every requirement is active. That is dependency closure, without a separate rule. Whole-bundle activation is the atomic activation above. A bundle whose context fails, whose managed preparation cannot start, or whose entries collide commits nothing and leaves no callable tool or resource. Registry order decides a collision between bundles because components commit in registry order and the loader refuses the later `add` or `provide`. Within one component the order of registrations is the order of the entries. Tests pin both. A second compaction strategy is refused by the exclusive `compaction/strategy` key as a backstop. Selection still guarantees at most one enabled. ## python as a component Python is one component, `python`, with no special position in the runtime. - `python/kernel` is an exclusive key in the session scope. The python component binds it. Its activation boots a kernel (acquire: spawn, record `kernel_links`; inverse: shut down, delete the row). Components that run cells, such as the python tool, inject `python/kernel` and are inactive without it. - `python/bindings` is a collection key. Each entry is a module specification, with the module living next to the component that provides it (today `priv/python/albedo_plugins/*`). A binding module a component contributes no longer appears in a `python_modules` field. Two components may add the same module id only if the module is a shared one; the shared module is its own component both inject. - `python/routes` is a collection key. A host route `namespace.method` is an entry added by the component that owns the namespace. Dispatch is a lookup over the collection, so routes are each component's own API. - The kernel effects (`python/{booting,upgrading,ending}` over the `python/kernels` table, behind `daemon/session_resources`) are the python component's own reconciliation, not the runtime's: when a session reloads it asks the component to rebind, and the component compares the entries and the workspace to the module set the kernel booted with, keeps the kernel when they match, and otherwise snapshots, stages, restores and swaps (the replace sequence in kernel.md). Failure before shutdown keeps the old kernel. This logic is python's and not the runtime's. The session serialises its own kernel work (kernel.md, "the session's kernel phase"); the component only keeps a start and a stop of one session from overlapping. - `Context.kernel` disappears. A tool reads the kernel from its fiber's injected key. ## native extension shape This is the contract the conversion follows. `work` and `warm` are the reference conversions (`harness/extensions/work/extension.gleam`, `warm/extension.gleam`). A session component may bind and read exclusive keys of other components: `extension.session_with(extension, activate, provides, injects)`, or `extension.session(extension, activate)` when it has neither. An exclusive key it `provides` must be bound by its activation (`component.provide`); a key it `injects` is read with `component.get`, and the component stays inactive, without an error, until a provider binds it, and deactivates before the provider unloads. Collections are never provided or injected: an extension registers entries into them. An extension is two things: - Installation: what the daemon keeps whether or not a session runs the extension. Fields of `extension.Extension` (`name`, `description`, `requires`, `initialise`) and the install-time fields `migrations`, `clean`, `transfer`, `startup`, `settings` (and the daemon-scope kinds listed in the key catalog). Durable state is outside the effect boundary: tables, migrations, row cleanup and settings sections belong to installation, never to an activation. - A session component: `extension.session(extension, activate)`. `activate(fiber)` runs once for each session scope that selects the extension, in a worker, and returns `Result(Nil, String)`. It reads the session's facts with `session_keys.facts_of(fiber)` (ledger, session id, workspace), registers entries with `component.add(fiber, key, id, entry)`, and acquires every resource with `component.acquire(fiber, fn() { Ok(#(value, inverse)) })`. There is no `Managed`, no `close` and no one big teardown: the runtime runs the inverses newest first, each at most once, a crashing inverse stopping nothing else, when the extension leaves the session or the scope closes. The composition wraps the activation: it injects the session facts and the `/active` markers of what the extension `requires`, binds the extension's own marker last when another extension requires it, and loads extensions one at a time in registry order. An activation that returns `Error`, crashes or loses a collision leaves nothing behind and shows up as the extension's warning; its dependents stay inactive. An extension never writes `requires` handling, marker keys or collision checks. An entry id is the name the runtime guards: a tool's name, a command's slash name, a route namespace (dotted), a python module (`@` when the module is shared, as `mcp` is). Collections without a natural name use `#`. A second entry with an id an earlier-loaded component holds is refused and the later extension fails with the collision; an id registered twice by one activation fails it too. One example for each kind of contribution, all inside one `activate`: ```gleam fn activate(fiber: component.Fiber) -> Result(Nil, String) { use facts <- result.try(session_keys.facts_of(fiber)) // a model tool, by name use _ <- result.try(component.add(fiber, session_keys.tools(), "notes", notes_tool(facts))) // a session command (its client binding is static metadata: a daemon component // adds it under daemon_keys.client_commands(), see `work`) use _ <- result.try(component.add(fiber, session_keys.commands(), "/notes", notes_command())) // a python module and the host route its binding calls use _ <- result.try(component.add(fiber, session_keys.modules(), "notes", "notes")) use _ <- result.try(component.add(fiber, session_keys.routes(), "notes", #("notes", route(facts)))) // instructions and context for the model use _ <- result.try(component.add(fiber, session_keys.blocks(), "notes", session_keys.Block("notes", context, instructions))) // a sidebar use _ <- result.try(component.add(fiber, session_keys.glances(), "notes", session_keys.Glance("notes", read, url))) // a managed resource with an observer (the warmer of `warm`) use inbox <- result.try(component.acquire(fiber, fn() { Ok(#(start(), stop)) })) use _ <- result.try(component.add(fiber, session_keys.observers(), "notes", session_keys.Observer("notes", fn(handle, event) { send(inbox, handle, event) }))) // a hook asked as each turn goes, and a note shown to the session use _ <- result.try(component.add(fiber, session_keys.hooks(), "notes#0", #("notes", ask))) component.add(fiber, session_keys.notices(), "notes#0", "the notes store is read-only") } ``` ```gleam // provides: the extension is the session's compaction strategy. selection and // `extension.conflict` read this declaration, so two such extensions cannot be // enabled together. extension.SessionComponent(activate, [key.declared(compaction.strategy_key())], []) // injects: a component that needs another's binding (here a kernel key the // python extension provides) is inactive until it exists. extension.SessionComponent(activate, [], [key.declared(python_keys.kernel())]) ``` Closures an entry holds are copied into the component runtime and into every process that reads the view. A closure captures what it serves (a handle, an id), never a catalogue: a value many sessions share goes through `shared_pool.hold` as the skills and mcp extensions do, with the release as the inverse of the acquisition that took the hold. ## key catalog Session scope, declared by the `session` component, built in `harness/session_keys.gleam` (one function per key, owner `session`), read from a `View` with `component.entries`: | key | entry type | id | read by | | --- | --- | --- | --- | | `session/facts` (exclusive) | `Facts(ledger, session, workspace)` | | every activation | | `session/tools` | `extension.Tool` | tool name | turn loop, prompt, `runtime.tools` | | `session/commands` | `command.Command` | slash name | `preparation`, `observation`, kernel routes | | `session/routes` (dotted) | `extension.Route` | namespace | `kernels.kernel_routes` | | `session/modules` | `String` | module, or `@` | `kernels.python_modules` | | `session/blocks` | `Block(extension, context, instructions)` | `#` | `preparation` | | `session/wordings` | `Wording(extension, text)` (instructions worded from the final tool names) | `#` | `preparation` | | `session/tool_fallbacks` | `ToolFallback(extension, rewrite)` (a call naming no tool rewritten into one that does; first answer wins) | `#` | the tool-call path of `runtime` | | `session/notices` | `String` | `#` | `composition.warnings` | | `session/observers` | `Observer(extension, observe)` | `#` | `runtime.observe` | | `session/hooks` | `hook.Hook` | `#` | `runtime.hook`, kernel routes | | `session/glances` | `Glance(extension, read, resource_url)` | `#` | `selection.glances` | | `session/compaction` (exclusive) | `compaction.Strategy` | | `runtime` request-history builder, `reload` | | `session/notes`, `session/folds` | `compaction.Notes`, `compaction.Folds` | extension name | `runtime` request-history builder | Daemon scope, built in `harness/daemon_keys.gleam` (owner `daemon`; `daemon/facts` is bound by the `daemon` component with the ledger and the home). Every installed extension's daemon component loads once at boot, in registry order, after the install (schemas are committed), so each key holds the entries of every installed extension, tagged with their owner. Enabling or disabling an extension never loads or unloads anything here: selection is a read-time filter. | key | entry type | id | read by | | --- | --- | --- | --- | | `daemon/model_catalogs` | `extension.ModelCatalog` | extension name | `providers.model_info`, `provider_model_info`, `provider_model_names`, `reload_catalogs` | | `daemon/providers` | `extension.ModelProvider` | extension name | `providers.upstream`, `fast_mode`, `catalog_provider` | | `daemon/logins` | `oauth.Login` | `#` | `providers.logins` | | `daemon/login_resolvers` | `fn(home, provider) -> Option(Result(Login, String))` | `#` | `providers.resolve_login` | | `daemon/client_commands` | `client_api.Command` (the binding names its command by slash name) | `#` | `http_catalog.native_commands` (before any session is composed), `observation` (joined to the session's commands) | | `daemon/services` | `extension.Service` | extension name (mounted at `/extensions//`) | `providers.service`, `service_names` | | `daemon/searches` | `web_search.Provider` | `#` | `providers.searches` | | `daemon/cyber_access`, `daemon/cyber_catalogs` | the check functions | extension name | `providers.cyber_access`, `cyber_catalog` | Exclusive daemon keys, bound once rather than collected: `daemon/facts` (`daemon_keys.Facts`, bound by the `daemon` component), `daemon/admission` (`admission.Handle`, bound by the `daemon` component, read by the runtime and python), and `daemon/session_resources` (`session_resources.SessionResources`, owner `daemon`, bound by python's daemon component in `python/extension`, read through `runtime.resources`). The last carries the kernel effects with their types forgotten (`session_resources.bind`) and restored by `session_resources.effects`; its helpers (`start`, `stop`, `upgrade`, `rebind`, `observe`) answer for a daemon that binds none. If the key is unbound a session that needs a kernel is refused. The filter rule: a consumer reads a daemon key only through `providers.enabled_entries(view, key, enabled)` (or a function of `providers` built on it), passing the enabled set it already computes: `runtime.global` for the picker, logins, services and global searches; the session's selection (`enabled_for`, or the composition's extensions) for a session's upstream, fast mode, cyber checks, model facts and search providers; the installed set for sign-in resolution. Entries of an extension outside the set contribute nothing. A session scope's view includes its parent's, so `composition.view` reads the same keys. Never read `component.entries` of a daemon key directly. A daemon component whose activation fails is left out with its dependents (`extension.without_failed`, once at boot) and listed as quarantined with the reason. Example of a daemon component (a provider with a catalog; `openai` is the reference): ```gleam extension.daemon(fn(fiber) { use _ <- result.try(component.add(fiber, daemon_keys.model_catalogs(), "openai", catalog)) component.add(fiber, daemon_keys.providers(), "openai", provider) }) ``` One example per other kind, in the same `activate`: a sign-in `component.add(fiber, daemon_keys.logins(), "codex#0", login)`, a resolver `component.add(fiber, daemon_keys.login_resolvers(), "mcp#0", resolve)`, a search `component.add(fiber, daemon_keys.searches(), "exa#0", provider)`, cyber `component.add(fiber, daemon_keys.cyber_access(), "codex", check)`. How a service registers: `component.add(fiber, daemon_keys.services(), "", extension.Service(admission, handle))`. The id is the extension's name because the mount point is `/extensions//`; the daemon serves it only while the extension is enabled globally (`runtime.service(host, name)`; the extension list reports `runtime.mounted_services(host)`). `Admission` and `handle` are unchanged. A worker the daemon runs for an extension (what `StartupPlugin` did) is not a key: acquire the process in the activation and return its stop as the inverse (`component.acquire(fiber, fn() { Ok(#(pid, stop)) })`); the activation runs after the install, so tables exist, and a failure quarantines the extension. Not keys: the extension's own tables and settings (installation), and python module code, which lives next to its extension. ## stored data the extension system reads The conversion changes no persisted key, name or shape: - `session_extensions` (a session's saved selection by extension name), the global defaults and `selection.gleam`'s SQL: untouched; extension names are the same. - Extension tables and their `initialise` and migrations: untouched (work keeps `work.initialise` and `cwd.apply`). - `kernel_links` (`modules` is the JSON list a kernel booted with): the module list is now in registration order, which can differ from the old static-before-managed order. A recorded list is compared as a set (`same_modules`), so a live detached kernel is not marked stale by the reordering. - `extensions.json` sections, `creds.json`, `capabilities.json`: read by the same code; `SettingsPlugin` is unchanged. - `mcp_catalogues`, `skill_loads` and other extension rows: untouched. - Not stored but visible: `extension_summaries` lists `provides` (the exclusive keys an extension's components bind) in place of the old list of plugin kinds. ## extension record ```gleam Extension( name, description, requires, initialise, // creates the extension's tables migrations: List(Migration), clean: Option(Cleaner), // rows kept for a session, deleted with it transfer: Option(transfer.Adapter), // what a move carries of its durable state startup: Option(fn(Store) -> Result(Nil, String)), settings: Option(settings.Declaration), session: Option(SessionPart), daemon: Option(DaemonPart), ) ``` An extension is built with `extension.new(name, description)` and the functions that add what it has: `requires`, `initialise`, `migration`, `clean`, `transfer`, `startup`, `settings`, `session` / `session_with` (activation, provides, injects) and `daemon` / `daemon_with`. The first seven are installation: durable, independent of any process, run whether or not a session enables the extension. The two parts are what activation registers: the session part once per session scope that selects the extension, the daemon part once at boot for every installed extension. The catalog lists what an extension provides (`provides`: the exclusive keys its components bind, such as `session/compaction`) next to its tools and python modules, which are read from the session view. There is no list of plugin kinds. Python is built the same way: `extension.new("python", ..) |> initialise |> migration (twice) |> clean |> startup |> daemon_with(declare, [python/kernels, python/spares, daemon/session_resources], [daemon/admission]) |> session_with(activate, [python/kernel], [])`.