# Agents The agents extension exposes session relationships and operations to Python code. Its routes are implemented by [`agents/extension.gleam`](../extensions/agents/src/albedo_ext_agents/extension.gleam), with daemon operations reached through [`sdk/session_control`](../packages/albedo_sdk/src/albedo_sdk/session_control.gleam). The host passes the daemon's coordinator as a `session_control.Control` in `host.Context.control` for kernel routes and `extension.Daemon.control` for the agents service. Pass that value to the typed functions, such as `spawn` and `stop`, and do not build `Op` values. Do not keep it in process-wide state. The daemon's session registry installs the coordinator's handler in [`session/session_control`](../apps/albedo/src/albedo/session/session_control.gleam) when it starts, and the host binds `session_control.call` into both contexts. A call runs in the caller's process, outside every actor. Before the handler is installed it answers `agents are unavailable`. A handler that crashes mid-call answers `agents are unavailable; outcome unknown`. The daemon's own `/kernel restart ` command calls the same function. Each agent is a session. See [session attachment](sessions.md). ## Starting agents The [Python binding](../python/albedo_plugins/agents.py) exposes `agents.start` for a root peer and `agents.self.spawn` for a child. Both return an `Agent` handle. Spawn also accepts a verifier and cyber access choice. The host rejects an empty task or model, validates the name, and limits the verifier to 4096 Unicode scalars. [`dispatch`](../extensions/agents/src/albedo_ext_agents/extension.gleam) passes the request to the daemon. [Conversation storage](../apps/albedo/src/albedo/history/conversation.gleam) commits a peer's session and origin together before the registry posts its task. A task that cannot be posted leaves the peer started and idle. [`with_worktree`](../extensions/agents/src/albedo_ext_agents/extension.gleam) handles the requested workspace before starting an agent. A workspace and its files are shared resources unless the chosen worktree gives the agent a separate directory. ## Relationships and operations `agents.self` is the caller's handle. `agents.get` resolves another handle. A handle's `children` and `siblings` methods report family state, while `switch`, `close`, and `cancel` operate on an owned target. A deferred `switch` is stored on `sessions` (`pending_switch_*`), survives a restart, and clears once applied or replaced. `agents.models` asks for available models. `agents.cancel_all` can include descendants. See the [Python binding](../python/albedo_plugins/agents.py) and [`service.act` and `stop_all`](../extensions/agents/src/albedo_ext_agents/service.gleam). `agents.set_role` permits changing the caller's role or that of an owned child or peer. Only a child's parent can call `child.set_verifier`. `agents.progress` publishes a nonempty status of at most 512 Unicode scalars. These checks are in [`dispatch`](../extensions/agents/src/albedo_ext_agents/extension.gleam). ## Messages and resources An `Agent` handle's `messages` method reads its transcript with sequence and pagination arguments. `search_messages` searches that transcript. `agents.sessions` searches sessions and clamps its result limit to 1 through 100. The default `open_only=True` excludes closed sessions while retaining idle ones; set `open_only=False` to include history. `top_level_only=True` keeps sessions with no parent and defaults to false. Both filters compose with `query` and `cwd`, and apply before `offset` and `limit` without changing activity order. See the [Python binding](../python/albedo_plugins/agents.py) and [`agents/sessions.gleam`](../extensions/agents/src/albedo_ext_agents/sessions.gleam). A query reads the messages only of sessions that may hold it. The [text index](../apps/albedo/src/albedo/storage/transcript_text.gleam) keeps the trigrams of each row's text, lowercased, in an FTS5 table that stores no text and no positions. `search.candidates` asks it for the rows holding every trigram of the query and adds the rows it has not read yet, which wait in `transcript_text_pending`. The [`transcript_rows` store](../apps/albedo/src/albedo/storage/transcript_rows.gleam) finds sessions by rowid for small candidate sets and scans the covering session index for larger sets. Above the candidate limit, search walks sessions directly. Waiting rows are indexed newest first, in store turns bounded by rows and payload bytes: all of them by a drain the registry starts when it opens, then those written since by each maintenance sweep, for up to two seconds. Triggers on `transcript` send a rewritten row back to wait and drop a deleted one, under any build, so the index only narrows a search and its results are those of reading every message. A query shorter than three code points has no trigram and reads every session. `resources.query` reports process and session resource observations through the [Python binding](../python/albedo_plugins/agents.py). Its host implementation is [`agents/resources.gleam`](../extensions/agents/src/albedo_ext_agents/resources.gleam). An observation does not guarantee that a process remains alive after the read. ## Agent workflow Read `agents.models()` before choosing a model for a child. `agents.self.spawn` returns a handle, not the child's answer. Use [mail](../python/albedo_plugins/mail.py) for the result rather than treating an idle snapshot as proof of completion. Use `child.set_verifier(command)` for an observable completion check. The [agents hook](../extensions/agents/src/albedo_ext_agents/extension.gleam) checks normal parent submissions and stops while a reply is owed, unless a live non-service background job or backgrounded cell has a completion wake pending (`kernel.awaits_wake`). Ask questions with `mail.request("parent", ...)`. A child that ends its turn without answering its parent and with no pending wake is kept going up to three times, and each reminder says that a submission means the task is finished. Only after the third does its last message reach the parent marked unreviewed; while a wake is pending, both reminders and unreviewed forwarding are deferred (`session.answer_parent`), though turn failures (`Stopped`) still report immediately. Outside a child's reply to its parent, `Stopping` honors explicit `end_turn()` without emitting `"You owe a reply to"`, and requests crossed in flight by a non-dropped reply (`mail.crossed`) neither trigger `"You owe a reply to"` nor forward unreviewed. Report a blocker with `mail.submit("parent", ..., blocker=True)` when the verifier cannot pass. Clear or replace a verifier whose condition is no longer relevant. ## Limits The `subagents` settings section defaults to 12 open children per parent and three generations below a root. `-1` removes either cap. The declarations are in [`family.limits_section`](../apps/albedo/src/albedo/session/family.gleam), and `family.link_in` enforces them when it records a child. The family tables, their schema, and the statements on them belong to [`session/family`](../apps/albedo/src/albedo/session/family.gleam), which the daemon initialises at startup. The reads and writes extensions use, with their capability checks, are in [`sdk/family`](../packages/albedo_sdk/src/albedo_sdk/family.gleam), which calls core's operations ([core table operations](extensions.md#core-table-operations)). Remote lookups check the family's own `family_remote` stubs first. Correspondents, sessions on another daemon that wrote to the caller, come from mail storage, so the daemon supplies that lookup as `family.Correspondents` ([`family.correspondents`](../apps/albedo/src/albedo/session/family.gleam)). Use mail by session ID to reach another session rather than adding nesting solely for communication. ## The order of agents Clients rank working agents before idle agents, then closed agents. More recent activity ranks first within a state. A branch uses its best descendant rank so a working child can bring its ancestors forward. Equal branches use creation order and ID. See [TUI ordering](../cli/internal/tui/agents/order.go) and [web ordering](../web/src/lib/agentTree.ts). Check both clients when changing this policy. ## Deletion boundary `agents.delete` refuses the request. Its error directs the user to delete through the session browser because deletion loses the child's work. `agents.close` is a separate operation. See [`dispatch`](../extensions/agents/src/albedo_ext_agents/extension.gleam) and [session lifecycle](sessions.md). `Agent.close()` marks the family member closed before cleaning up, so new mail cannot enter during shutdown (subsequent sends are rejected with `MailError` naming the closed session) and any queued `task`, `request`, or `message` letters are dropped with a notice letter back to each local open sender. It returns `True` after the kernel and its jobs stop. The session saves its namespace before shutdown and keeps the transcript, files, and worktree. Saving or shutdown failures raise an error while the family member remains closed. A timeout can leave cleanup pending. Repeat close to reconcile cleanup, including recorded kernels with no session worker. See [`registry.run_agent_plan`](../apps/albedo/src/albedo/session/registry.gleam) and [`session.close_agent`](../apps/albedo/src/albedo/session.gleam). ## Related pages - [All topics](README.md) - [Change guidance](contributing.md) - [Session attachment](sessions.md) - [Inbox](inbox.md) - [Worktrees](worktrees.md)