# Add an extension Use this guide to add capabilities through an extension. Read the [extension reference](../robot-docs/extensions.md), [plugin contracts](../robot-docs/plugins.md), and [shared contribution rules](../robot-docs/contributing.md) before choosing an owner. ## Choose the contributions Declare the extension's name, description, requirements, plugins, and initializer with [`extension.Extension`](../src/albedo/harness/extension.gleam). Keep declarations free of I/O and environment capture. A sealed build caches them. Choose plugin types from the current `Plugin` definition instead of copying an old inventory. Use `ToolPlugin` for static instructions, tools, Python modules, and host routes. Use `ManagedPlugin` when a contribution must be prepared before use. Use `OwnedManagedPlugin` when allocated resources must survive preparation handoff safely. If a route needs only the values on `host.Context`, keep the route in `ToolPlugin` and do not add a managed contribution that only returns routes. Keep schema, data migrations, deletion cleanup, and transfer behavior with the extension that owns the data. See [migrations](../robot-docs/migrations.md) and [extension lifecycle rules](../robot-docs/contributing.md#preserve-extension-lifecycle-boundaries). ## Connect host routes Use [`host.Route`](../src/albedo/harness/host.gleam) for Python-to-host calls. A route receives `host.Context`, a method string, and dynamically typed arguments. It returns JSON or an error pair. The context supplies the store, the session ID, the loaded workspace, and the providers from the active composition. Declare routes that need only context values in `ToolPlugin`. Follow the [work route](../src/albedo/harness/extensions/work/extension.gleam), which passes `context.store` and `context.workspace` to its handler. [Paperclips](../src/albedo/harness/extensions/paperclips/extension.gleam) also passes `context.session`. Use this loaded workspace instead of querying session settings or reading the process working directory. See the [context snapshot contract](../robot-docs/plugins.md#host-route-context). Resolve providers from the supplied context. Do not capture the installation registry as a substitute for the active session composition. Keep HTTP service admission separate from session host routes. The [extension HTTP contract](http-extensions.md) describes caller permissions, conditional edits, and notifications. ## Declare client operations Use `ClientPlugin` for human commands that read or change an HTTP resource. Start with [`client_api.operation_defaults`](../src/albedo/harness/client_api.gleam), then override the bindings and result constraints the operation needs. Reuse `text_field`, `integer_field`, `choice_field`, and `form_body` when their defaults fit. Preserve custom labels, defaults, validators, and success statuses. Use a `FieldKind` variant when constructing a field directly. Build resource-page actions with `client_api.actions` and propagate its error before encoding the page. See [client declaration checks](../robot-docs/plugins.md#client-declarations). Follow the [work declaration](../src/albedo/harness/extensions/work/extension.gleam) and its [page actions](../src/albedo/harness/extensions/work/service.gleam). Keep model host routes separate from HTTP operations. Read [client API ownership](client-api.md) for transport, recovery, and authorization boundaries. ## Prepare session contributions Follow the [schedule extension](../src/albedo/harness/extensions/schedule/extension.gleam) for a managed contribution that builds a command from the supplied store and session ID. Start from `extension.empty()` and fill the contribution fields you need. For an `OwnedManagedPlugin` whose worker only observes session events, use [`worker.observer`](../src/albedo/harness/extension/worker.gleam). Pass the preparation owner, the worker loop, the message constructor that wraps a session handle and event, and the startup wait in milliseconds. Wrap its owned result in `Some` and map its `Error(Nil)` to the extension startup message: ```gleam worker.observer(owner, run, Observed, 5000) |> result.map(Some) |> result.replace_error("the worker did not start") ``` `observer` builds the managed contribution from `extension.empty()`. Use `worker.start` when other callbacks need the worker inbox, then attach its lifecycle with `extension.owned(contribution, started.adopt, started.close)`. Return `Ok(None)` only when preparation retains no resource or contribution. See the [owned worker helper reference](../robot-docs/plugins.md#owned-worker-helper). Keep event observers short. They run inside the session actor, so send work to its owner and return instead of blocking. Run command handlers outside the actor whose state operations they call. See [plugin contracts](../robot-docs/plugins.md) and [commands](../robot-docs/commands.md). Give allocated resources an explicit cleanup owner. Make close callbacks idempotent. For owned preparation, establish lifecycle monitors before readiness and complete adoption before publication. Preserve saved selection when applying it fails. The [shared lifecycle rules](../robot-docs/contributing.md#preserve-extension-lifecycle-boundaries) describe the required failure boundaries. ## Report hooks If the contribution steers turns, add a hook to its managed value. Use [`hook.Event` and `hook.Verdict`](../src/albedo/harness/hook.gleam) and the [hook reference](../robot-docs/hooks.md) for events, merge rules, refusal behavior, and output visibility. A hook that only observes work must send the event to its owner and return `hook.proceed` promptly. For calls inside Python cells, use [`albedo_plugins.hook`](../priv/python/albedo_plugins/hook.py). Use `reported(name, fields)` when method parameters map to hook arguments, or `around(name, arguments, run)` for an asynchronous callback. Execute with the rewritten arguments, not the original arguments. Use `before` and `after` when completion must be reported separately. For command-hook configuration, inspect the parser in [`albedo_hook_command.py`](../priv/python/albedo_hook_command.py). The [hook reference](../robot-docs/hooks.md#command-hooks) describes command execution and failure behavior. Command hooks are not a fail-closed security boundary. ## Expose a JSON service For a buffered JSON API authenticated with the daemon token, import [`harness/extension/service`](../src/albedo/harness/extension/service.gleam) as `http_service`. Add its result to the extension's plugin list: ```gleam extension.ServicePlugin(http_service.json(65_536, service.dispatch)) ``` Choose a body limit for the service's operations. Implement `service.dispatch(daemon, path, request)` with a `Result(Response(mist.ResponseData), http_api.Failure)` return value. Keep route matching, query and body decoding, and domain error mapping there. Follow the [work service](../src/albedo/harness/extensions/work/service.gleam) and reuse the decoders in [`http_api`](../src/albedo/daemon/http_api.gleam). Read the [HTTP service contract](../robot-docs/plugins.md#http-services) for negotiation and admission behavior. Use `extension.Service` directly when routes need streaming, non-JSON responses, different authentication, or different body limits per route. ## Register and check the extension Add the constructor to [`extensions.declared`](../src/albedo/harness/extensions.gleam). Set its default selection there only if the feature should be enabled by default. Keep discovery read-only and ensure failed preparation leaves no callable contributions from the rejected bundle. Exercise the externally visible change through the [shared daemon harness](../robot-docs/testing.md). Cover the relevant activation, failure, and cleanup paths. Update the canonical feature page with the behavior, limits, and guidance callers need, then follow the [repository checks](../AGENTS.md#use-the-project-tools).