# Change Albedo These project rules guide implementation changes. The linked feature pages describe current behavior and its limits. ## Choose the owning layer Add capabilities through typed [plugins](plugins.md). Keep HTTP admission and wire encoding in daemon adapters, composition and kernel lifecycle in the [runtime](runtime.md), and feature state with its owning extension. The interfaces are defined in [`extension.gleam`](../src/albedo/harness/extension.gleam). Use the command's `Context.state` operations or supplied session callbacks to reach session behavior. Do not mutate a session's state from an extension. Resolve cross-extension dependencies from the applicable active selection, using [`host.Context`](../src/albedo/harness/host.gleam) or `extension.Daemon` rather than a captured installation registry. See [commands](commands.md) and [extensions](extensions.md). Add an actor when ordering, concurrency, or resource lifetime requires one. A plugin declaration alone does not require a process. ## Keep typed and native responsibilities separate Keep process state, ports, monitors, external-term handling, and native binary operations in their Erlang owners. Reuse Gleam functions for validation and text projection. Keep typed values through internal operations and encode JSON at the wire boundary. Do not encode an observation to JSON and parse it again to add fields. See [`command.StateOp`](../src/albedo/harness/command.gleam). Keep unfinished-output projection policy in [`active_output.gleam`](../src/albedo/daemon/active_output.gleam). Its [native helper](../src/albedo/daemon/albedo_active_output.erl) owns spill-file operations. Do not reconstruct output by replaying events when a typed projection is available. ## Reuse runtime helpers Use [`clock`](../src/albedo/clock.gleam) for timestamps and deadlines. Use system time for durable timestamps and monotonic time for elapsed durations. Request an explicit native time unit. Keep a scaled test timer and the deadline it checks consistent. Use [`text_scalars`](../src/albedo/text_scalars.gleam) for scalar-bounded previews and names. Do not allocate a codepoint list for the entire input just to truncate a prefix. Scalar limits and grapheme limits serve different purposes. Use [`shared`](../src/albedo/shared.gleam) for long-lived immutable values shared across processes. Release a handle at the end of its owner's lifetime. Do not turn changing provider caches into repeatedly replaced compound persistent terms. Capture compact arguments in stored callbacks rather than the whole actor state. [`session_namespace.watch_kernel`](../src/albedo/daemon/session_namespace.gleam) demonstrates that pattern. Use [`actor_call.try_call`](../src/albedo/actor_call.gleam) for finite monitored waits. A timeout does not cancel accepted work. Keep inner waits within their callers' deadlines so the inner failure can be returned. Use a lifetime protocol when the caller must wait for a definite outcome or owner death. ## Preserve extension lifecycle boundaries Keep discovery read-only. A catalog read must not boot Python, allocate managed resources, execute commands, or contact providers. [`runtime/catalog.gleam`](../src/albedo/harness/runtime/catalog.gleam) owns discovery, and [`runtime/preparation.gleam`](../src/albedo/harness/runtime/preparation.gleam) owns preparation work. Distinguish installed extensions, saved selection, and loaded composition. Use installation for schema and deletion ownership, and the loaded composition for session capabilities. Do not leave callable contributions from a failed bundle. See [extensions](extensions.md). Give each allocated resource a cleanup owner. Use the owned preparation contract when a resource must survive handoff. A resource must establish its lifecycle monitors before reporting readiness, and adoption must finish before publication. Make close callbacks idempotent. [`extension/worker.gleam`](../src/albedo/harness/extension/worker.gleam) and [`composition.adopt`](../src/albedo/harness/extension/composition.gleam) implement these contracts. Legacy managed callbacks do not acquire them automatically. Keep kernel replacement in the runtime. When a caller owns a live session, use `runtime.apply_change` or `apply_desired` and reconcile the surviving session from either result. Do not add a separate snapshot-and-replace sequence in an HTTP handler. Preserve saved intent on apply failure. A database rollback cannot restore a process that has already stopped. Before changing reload behavior, identify the selection each handler uses, the owner that closes each resource, and the state retained after failure. ## Keep actor handlers responsive Keep lifecycle waits and slow work outside session actors. Command handlers must run outside the actor whose state operations they call. A handler that waits on its own actor deadlocks. See [commands](commands.md) and [`session.gleam`](../src/albedo/daemon/session.gleam). Use [`store.read` or `snapshot`](../src/albedo/daemon/store.gleam) for pure observations. Keep mutations and read-modify-write work on the writer through `query`. Use `query_then` when an actor must remain responsive while a write commits. Publish the result after commit, not after enqueueing. Inside a snapshot, use the supplied connection for related reads. Do not write, retain the connection, or call Store-level APIs from the callback. Do not wait for capture-reader admission inside the session actor. The separate capture connection exists to avoid that ordering problem. Reserve runtime producer storage before sending payloads or creating completion relays. Use the public runtime entrypoints instead of sending request messages directly. Keep a request's reservation through deferred work and shared waiters, and tag completion with the applicable generation. Requester withdrawal must remove its waiter without cancelling another requester's shared work. Count runtime workers from monitored tickets, not session ownership maps. Keep cleanup capacity separate from public preparation and observation. Reserve cleanup bookkeeping before resource allocation. A stale generation still consumes computation capacity until its worker settles; an unconfirmed cleanup retains its resource callback and cleanup credit afterward. Preserve the runtime's failed-stop fencing and result acknowledgment protocol. See the [runtime admission contract](runtime.md#admission-and-worker-lifetime). Register `stop_before_close` for resources that read the store on their own schedule. The stop callback runs on the writer and must not call the store. Keep resource shutdown within the store's lifecycle. See [`store.gleam`](../src/albedo/daemon/store.gleam). For schema changes, preserve the [foreign-key indexing rule](migrations.md#foreign-key-indexes). Unindexed child lookups can make deletion block the writer. ## Preserve client conventions For terminal screens, follow [Add or change a terminal screen](tui-contributing.md). Reuse the shared layout, filtering, confirmation, and request-generation contracts. Check the result visually using the repository's [UI review instructions](../AGENTS.md#inspect-ui-changes). ## Preserve provider and retry boundaries Keep `ModelProviderPlugin`, `ModelContext`, and `Upstream` as the provider boundary. Shared request and replay types belong under [`openai_api`](../src/albedo/openai_api.gleam). Provider identity, signing, file handling, and body repair remain provider-owned. Reuse shared codecs and transport where they fit. Keep credential recovery separate from request encoding. Preserve the error variants, cancellation behavior, usage, and account attribution used by recovery. Do not add another broad retry loop around the existing model-loop and transport retries. See [authentication](auth.md) and [provider requests](provider-requests.md). After an interrupted or uncertain operation, inspect its receipt or effects before retrying. A missing answer does not establish that the operation did nothing. Keep operation identity across retries where the [admission contract](operations.md) requires it. ## Handle uploaded files Treat client-side attachment checks as convenience. The daemon validates names, encoding, counts, and sizes again and decides admission. Build storage paths from daemon-owned identity, such as the session, input ID, and content hash. Do not use the uploaded filename as a path component. Record attachment metadata in the submission that owns the message. Do not infer that a message had an attachment from text that looks like a label. Do not resend changed attachments under an old input ID. A transcript can outlive the saved bytes, so do not promise that a file named in history is still readable. See [saved pastes and uploads](kernel-state.md#saved-pastes-and-uploads) and the [web client's attachment rules](web.md#chat-attachments). ## Test the observable change Use the shared real-daemon harness for externally visible behavior. Do not copy daemon startup plumbing into a test. Keep upgrade scenarios while their old formats remain supported. See [testing](testing.md), [migrations](migrations.md), and the repository's [test policy](../.agents/skills/writing-tests/SKILL.md). Use focused unit tests for cross-cutting invariants or bug-prone logic that an end-to-end scenario cannot exercise reliably. Do not add tests that only restate constants, copy, or record plumbing. Keep live-provider experiments under `test/manual`. Run the relevant checks before submitting a change. Keep generated audit and benchmark reports under `/tmp`. Retain a measurement in documentation only with its fixture, revision, environment, command, and result artifact. See [Gleam checks](gleam-lint.md) and [performance checks](perf.md). ## Related pages - [All topics](README.md) - [Extensions](extensions.md) - [Test execution](testing.md)