# Embedding and extension boundaries

zelk supports both a managed agent and host-owned execution. The public root API
owns convenient composition; the component packages beside it in `core/` are independently usable
and live in the same module. Neither requires access to private agent implementation. This
is the API ownership map.

## Public component audit

| Package | Host use and ownership |
|---|---|
| `provider` | Implement models or select protocol adapters; the host owns provider lifetimes and HTTP wiring. |
| `tool` | Define actions and their properties; custom executors can construct calls with recorded session facts. |
| `env` | Supply filesystem/process capabilities and required enforcement; adapters report what they enforce. |
| `store` | Supply durable logs and blobs; implementations run `storetest`. |
| `lease` | Supply exclusive fenced authority; implementations run `leasetest`. |
| `fetch` | Supply fetching/extraction or configure the provided clients. |
| `search` | Supply a search backend using portable query/result types. |
| `permission` | Select modes, authorized switches and narrowed tool ceilings. |
| `skill` | Define or load explicit sources, parse bundles and inspect resolved skill sets. Discovery takes explicit roots. |
| `subagent` | Define child configurations and choose built-in definitions; the agent owns child lifetimes. |
| `blob` | Construct and validate portable content-addressed references. |
| `history` | Construct portable messages and provider-native payload envelopes. |
| `event` | Encode/decode durable records and construct commands/configuration snapshots. |
| `loop` | Fold/recover serializable state and prepare atomic derived records for custom executors. |
| `driver` | Reuse supervised authority, initialization, commands and effect executors, or run the supplied driver. |
| `mcp` | Configure MCP servers (stdio through a host environment, or HTTP through a host client); the agent owns the connections it makes and closes them with itself. |
| `observe` | Implement an observer over the host's own telemetry; zelk never starts or stops it. The OpenTelemetry adapter is the separate `otel` module. |
| `acp` | Serve an agent on a host-owned connection; executions outlive the connection and belong to the agent. |

The audit retains these exports. Reducer/request helpers, tool-call construction,
provider reservation helpers and codecs support custom executors and adapters;
their low-level nature is not a reason to hide them. `examples/apicheck` exercises
public component construction, and `examples/external-driver` implements fenced
execution and crash recovery through public APIs. No existing export is renamed
or removed in this pass. `Driver.Ensure` is an additive initialization operation.

Persisted event layouts, reducer-version behavior, tool schemas and atomic record
groups remain protocol constraints, including when a host writes its own driver.
Use `loop.Records` before an append; dispatch only after the group is durable.
See [durability](/internals/durability/) and [the external driver](https://github.com/barevoidco/zelk/blob/main/examples/external-driver/README.md).

## Agent and driver responsibilities

The agent resolves the configuration it offers for a new session, constructs
host-visible handles, owns child/process cleanup and releases the execution's
lease. The driver owns supervised adoption, lazy initialization, persistence,
recovery and effect dispatch. `Ensure` calls the supplied configuration resolver
only for an empty log; reopening keeps recorded configuration. Callers retain
ownership of authority even when initialization fails. `Begin` remains the
explicit initialization primitive for hosts managing the durable protocol.

The Agent passes its injected model-visible root to `driver.WithRoot`. `Ensure`
checks the latest recorded configuration before Agent start preparation, and
`Run` checks it before provider resolution, command admission or recovery.
`Driver.CheckRoot` gives custom hosts the same predicate for workspace operations
outside `Run`. A mismatch is `driver.ErrIncompatible` and names both roots;
omitting a root permits only rootless sessions. Comparison is exact on clean
absolute slash-separated paths, not host-filesystem identities or symlink targets:
remote Environments and the reducer's lexical path checks share the model-visible
namespace. Inspection and pure replay are not execution and remain unrestricted.
See [the root guard](https://github.com/barevoidco/zelk/blob/main/core/driver/README.md#workspace-root-guard).

Execution subscriptions receive only an event reader. The log adapter used by the
driver receives its log port and an append notification callback. The agent's
execution registry owns the lookup and notification of the active execution;
these adapters do not retain the whole agent or access unrelated configuration.

The agent's result projection and wake query are read-only uses of reducer state.
They do not repeat transition policy or own scheduling. The agent, session and
execution types are declared in the root package, so their methods and fields
are documented where hosts import them ([architecture](/internals/architecture/)).

## Enforced dependency boundaries

- Public components do not import the root package or
  built-in tool adapters. Hosts can use components without pulling in the agent.
- `blob`, `history` and `event` form the data layer, in that order. Their local
  dependencies stay within that layer rather than acquiring runtime services.
- Shared private policies/codecs remain valid implementation dependencies of
  public components: for example, the reducer uses accounting/compaction policy
  and provider adapters use schema rendering. `internal/` does not imply that all
  public components must duplicate their private machinery.
- Test infrastructure is confined to tests and testkit implementations, including
  the separate evaluation and examples modules. Platform-specific files are
  inspected even when they do not build on the current machine.

`core/internal/contract` checks these boundaries alongside the existing provider/env,
process, tool-family and pure-policy rules. Targeted import-rule cases distinguish
forbidden dependencies from permitted shared implementation dependencies. Run
`make check` to validate all source modules and the external embedding proofs.

## Observer sources

Hosts send `zelk.CreateSource(event.Source)` and `EndSource` as ordinary
commands, and inspect `Session.Sources`. Observers call
`Session.RecordOccurrence(ctx, source, event.OccurrencePayload)` to append without
a lease and poke admission. A successful call acknowledges the inbox append,
not model execution. Admission failures surface through execution results and
the injected logger; returning them as recording failures would invite duplicate
appends. `Session.AdmitOccurrences` explicitly pokes an existing session. It
coalesces with a local execution or starts an Agent-owned execution; a foreign
lease defers admission. Neither method starts a timer or resumes unrelated sessions.
Custom drivers inject `driver.WithInbox` and use `Commands.Poke`; custom stores
implement `Inbox.Head` as a read without sealing or persistence.

Custom drivers wire `driver.WithInteractiveWakes` (whether a person can
answer a joining follow-up).
