Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

docs(channels): document host-agent channel bindings

Steven Enamakel28280f83eda3
diff --git a/crates/openhuman-core/src/channels/README.md b/crates/openhuman-core/src/channels/README.md
index 69a5627ca0..fcdea120ba 100644
--- a/crates/openhuman-core/src/channels/README.md
+++ b/crates/openhuman-core/src/channels/README.md
@@ -32,7 +32,7 @@ Flat files: `bus.rs` (`ChannelInboundSubscriber`, handles `DomainEvent::ChannelI
## Calls into
-- `crates/openhuman-core/src/agent/` — the agent turn is dispatched over `BUS.native()` to the `agent.run_turn` handler registered by `agent::bus::register_agent_handlers`, so `runtime/dispatch/` never imports the harness directly.
+- `crates/openhuman-core/src/agent/` — the agent turn is dispatched over `BUS.native()` to the `agent.run_turn` handler registered by `agent::bus::register_agent_handlers`, so `runtime/dispatch/` never imports the harness directly. The exception is a channel bound to a host-registered agent (`config.agent.channel_agents`, resolved through `agent::host_agents`): `runtime/dispatch/host_agent/` builds that agent's session and runs the turn as it, under the channel's `ExternalChannel` origin capped at read-only, and refuses the message when the bound agent is missing. See [runtime/README.md](runtime/README.md#channels-bound-to-a-host-agent).
- `crates/openhuman-core/src/agent/context/channels_prompt.rs` — channel system prompt rendering, re-exported as `build_system_prompt`.
- `crates/openhuman-core/src/security/credentials/` — `AuthService` lookups for connect/disconnect and for secret hydration at startup (email password, Yuanbao app secret).
- `crates/openhuman-core/src/security/approval/` — `ApprovalGate` for the approval-reply intercept and `ApprovalChatContext` scoping of turns on every provider with the `chat_approvals` capability.
diff --git a/crates/openhuman-core/src/channels/runtime/README.md b/crates/openhuman-core/src/channels/runtime/README.md
index f3e02d9dc5..e12dcd1517 100644
--- a/crates/openhuman-core/src/channels/runtime/README.md
+++ b/crates/openhuman-core/src/channels/runtime/README.md
@@ -19,7 +19,7 @@ Model authentication and account lifetime are separate. A local or CLI model can
1. Each provider listener (`spawn_supervised_listener`, one per channel from `tinychannels::build_channels`) sends `traits::ChannelMessage`s on a bounded `mpsc` (capacity 100). A bridge task in `startup/start_channels.rs` wraps each into `RuntimeChannelMessage` and forwards it to the dispatch queue (also capacity 100). The relay runtime (`start_relay_runtime`) writes `RuntimeChannelMessage`s to that queue directly so relay inbound keeps its original TinyChannels envelope.
2. `run_message_dispatch_loop` (`dispatch/processor.rs`, over `tinychannels::runtime::run_dispatch_loop`) drains the queue under a semaphore sized by `compute_max_in_flight_messages(listener_count)` and spawns a worker per message that calls `process_channel_runtime_message` (`processor/turn.rs`). `process_channel_message` is the `traits::ChannelMessage` wrapper for the same pipeline.
-3. `process_channel_runtime_message` publishes `DomainEvent::ChannelMessageReceived`, then: if `channel_has_approval_surface` and `try_route_approval_reply` claims the message as a yes/no answer to a parked approval, stops there; otherwise starts typing, sends the ACK reaction (`select_acknowledgment_reaction`, threaded messages only), opens a streaming draft when the channel supports it, resolves scoping (`resolve_target_agent` + `build_visible_tool_set`), dispatches the turn over `BUS.native()` to the `agent.run_turn` handler (`agent::bus::register_agent_handlers`), sends the reply, and publishes `DomainEvent::ChannelMessageProcessed`.
+3. `process_channel_runtime_message` publishes `DomainEvent::ChannelMessageReceived`, then: if `channel_has_approval_surface` and `try_route_approval_reply` claims the message as a yes/no answer to a parked approval, stops there; otherwise, when the channel is bound to a host agent (`agent.channel_agents`) whose agent no host answers for, replies `AGENT_UNAVAILABLE_REPLY` and stops there; otherwise starts typing, sends the ACK reaction (`select_acknowledgment_reaction`, threaded messages only), opens a streaming draft when the channel supports it, and runs the turn: a channel bound to a host agent runs as that agent (`host_agent::run_host_agent_turn`, below); any other channel resolves scoping (`resolve_target_agent` + `build_visible_tool_set`) and dispatches the turn over `BUS.native()` to the `agent.run_turn` handler (`agent::bus::register_agent_handlers`, `processor/bus_turn.rs`). It then sends the reply, and publishes `DomainEvent::ChannelMessageProcessed`.
## `dispatch/`
@@ -27,11 +27,20 @@ Model authentication and account lifetime are separate. A local or CLI model can
| --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `helpers.rs` | Stateless helpers: per-turn context block for non-web channels, deterministic ACK-emoji picker, worker join logging, scoped typing task |
| `routing.rs` | `AgentScoping`, `resolve_target_agent`, `build_visible_tool_set`, `connected_with_fallback`: picks the active agent for the channel and its visible/delegation tool surface from `Config`, `AgentDefinitionRegistry`, and the connected-integrations snapshot |
-| `processor.rs` (+ `processor/`: `approval.rs`, `turn.rs`) | `RuntimeChannelMessage` (re-exported from `tinychannels::runtime`), `channel_has_approval_surface`, `try_route_approval_reply`, `process_channel_message`, `process_channel_runtime_message`, `run_message_dispatch_loop` |
+| `processor.rs` (+ `processor/`: `approval.rs`, `turn.rs`, `bus_turn.rs`) | `RuntimeChannelMessage` (re-exported from `tinychannels::runtime`), `channel_has_approval_surface`, `try_route_approval_reply`, `process_channel_message`, `process_channel_runtime_message`, `run_message_dispatch_loop`; `bus_turn.rs` is the orchestrator path over the bus |
+| `host_agent/` (`mod.rs`, `turn.rs`, `posture.rs`, `guard_tool.rs`) | Channels bound to a host-registered agent: `bound_agent_id`, `route` (`HostRoute::{Unbound, Agent, Missing}`), `refuse_missing_agent`, `run_host_agent_turn`, `seed_rows`, and the external-channel posture (`tool_ceiling`, `cap_channel`, `guard_host_tools`, `CeilingGuard`) |
| `mod.rs` | Declares the three submodules and the `#[cfg(test)]` / `#[cfg(any(test, debug_assertions))]` re-exports the test modules reach through `super::*` |
Two policy points here: `channel_has_approval_surface` follows the provider's `chat_approvals` capability (every chat provider; not email, the CLI or webhooks, which keep the "no chat context, silently allow" behaviour); and scoping is per channel (`resolve_target_agent(&msg.channel)`), falling back to `AgentScoping::unscoped()` (every registered tool visible) when the registry is not initialised or the target agent is unknown.
+## Channels bound to a host agent
+
+`config.agent.channel_agents` maps a channel name to an agent id (`telegram = "teeny-chat"`). It is keyed by channel name rather than set on each provider's config because those structs belong to `tinychannels`; which agent answers a channel is host policy. A message on a bound channel is resolved through `agent::host_agents` and runs as that `HostAgent`: its definition (system prompt, tool scope, iteration cap), its host tools, its config (provider model and route) and its `CoreContext` (`HostAgent::scope`). The channel keeps what it owns: the per-chat history in `conversation_histories` seeds the session (`seed_resume_from_messages`, transcript autoload suppressed, so no other chat's transcript is resumed), and typing, drafts (progress is wired with `set_on_progress`), the reply, history compaction on overflow and `ChannelMessageProcessed` work as for the orchestrator. The channel's `/models` route does not apply: the agent's own provider answers.
+
+- **A missing agent is refused, never widened.** When the binding names an agent no host answers for (not created yet, or dropped), the message gets `AGENT_UNAVAILABLE_REPLY`, `ChannelMessageProcessed` records `success: false`, and an error is logged. It is not sent to the orchestrator, which holds the operator's full tool belt.
+- **The turn runs as `ExternalChannel`, capped at read-only.** The origin is passed down as a parameter (`run_single_with_origin`), not read from a task-local. `posture::tool_ceiling` maps it to `PermissionLevel::ReadOnly`, and the turn applies it twice. `cap_channel` sets `agent.channel_permissions[<channel>] = "readonly"` in the turn's config clone (never widening an operator's `none`), so the session's tool policy withholds every tool above read-only, built-in or host, and a call by name is refused at once. `guard_host_tools` wraps each host tool in a `CeilingGuard` that refuses, with a tool error, any call whose `permission_level_with_args` exceeds the ceiling or that has an external effect. It reports no external effect itself, so the approval gate never parks an approval that only the outside sender could answer.
+- An unbound channel takes the orchestrator path unchanged.
+
## Called by
`channels::start_channels` is re-exported from `channels/mod.rs` and spawned by `spawn_channels_service` in `crates/openhuman-core/src/core/runtime/services.rs`, unless `OPENHUMAN_DISABLE_CHANNEL_LISTENERS` is set or `channels_config.has_listening_integrations()` is false. Web-chat-only desktop cores therefore never run it; `bus::ChannelInboundSubscriber` and the web-only proactive subscriber are registered on the always-on boot path in `core/runtime/subscribers.rs` instead.
@@ -41,4 +50,5 @@ Two policy points here: `channel_has_approval_surface` follows the provider's `c
- `startup_tests.rs`, `startup_email_secret_tests_tests.rs`, `startup_yuanbao_secret_tests_tests.rs` (secret hydration precedence).
- `supervision_tests.rs`.
- `dispatch_tests.rs` (helpers: ACK reaction categories, context block shape), `dispatch/mod_scoping_tests_tests.rs` (`build_visible_tool_set` / `AgentScoping`), `dispatch/mod_approval_surface_gating_tests_tests.rs` (`channel_has_approval_surface`), `dispatch/routing_connected_fallback_tests_tests.rs` (`connected_with_fallback`: authoritative snapshot vs cached fallback on unavailable/timeout).
+- `dispatch/host_agent/mod_tests.rs` (binding lookup, `seed_rows`), `posture_tests.rs` (ceiling, `cap_channel`, `CeilingGuard`), `dispatch_tests.rs` (a missing agent is refused without reaching the bus; an unbound channel still reaches the orchestrator; a bound channel runs its host agent against a mock provider with its prompt and read-only host tool, under the channel's origin, with the write tool withheld and refused). The embed crate's `tests/channel_agents.rs` covers the same path end to end through a mocked Telegram Bot API.
- End-to-end dispatch coverage lives in `channels/tests/` (`runtime_dispatch`, `runtime_tool_calls`, `discord_integration`, `telegram_integration`, `health`).
diff --git a/crates/openhuman-embed/README.md b/crates/openhuman-embed/README.md
index 1d2f6d65f5..2b84d006cd 100644
--- a/crates/openhuman-embed/README.md
+++ b/crates/openhuman-embed/README.md
@@ -285,6 +285,19 @@ and stops it with the runtime; `start_services` / `stop_services` control it
explicitly. See [`gitbooks/developing/embedding.md`](../../gitbooks/developing/embedding.md#scheduling)
and `tests/cron_agents.rs`.
+### Channels
+
+`Runtime::channels().telegram(TelegramChannelSpec::new(token, agent_id))`
+starts a Telegram listener whose every message is a turn of that runtime
+agent: its prompt, its host tools and the chat's history. The agent must
+exist first (`ChannelError::UnknownAgent` otherwise), and if it is dropped
+later the bot answers that it is unavailable rather than falling back to the
+orchestrator. Turns run as `ExternalChannel` and are capped at read-only:
+tools that write or reach outside are withheld or refused at once. The
+returned `ChannelListener` stops the bot when it is dropped. Behind the
+`channels` feature (on by default). See the gitbook's "Channels" section and
+`tests/channel_agents.rs`.
+
### Still runtime-wide
These are read from the runtime's boot config by every agent today. They
@@ -398,6 +411,10 @@ turn against a `wiremock` provider with nothing bound;
`tests/runtime_agents.rs` runs three agents with different providers, access
tiers, skills, MCP servers and working directories on one runtime and shows
the API key reaching a mocked managed backend as a bearer;
+`tests/channel_agents.rs` drives a runtime agent from a mocked Telegram Bot
+API: the bound agent answers with its prompt and read-only host tool under the
+`ExternalChannel` origin, its write tool is withheld and refused, and the reply
+is posted back to the chat.
`tests/cron_agents.rs` runs a cron job as a runtime agent with its host tool
under the `TrustedAutomation { Cron }` origin, records a system job handler's
error, and starts and stops the scheduler with the runtime;
diff --git a/gitbooks/developing/embedding.md b/gitbooks/developing/embedding.md
index f789fe7bc9..5a590a076a 100644
--- a/gitbooks/developing/embedding.md
+++ b/gitbooks/developing/embedding.md
@@ -327,6 +327,50 @@ println!("{} {}", run.success, run.output);
- `cron.list()`, `cron.remove(name)` and `cron.runs(name, limit)` cover the
rest. Shell jobs and workflow schedule triggers are not managed here.
+## Channels
+
+A runtime agent can answer a messaging channel. `runtime.channels()` starts a
+channel listener bound to one agent, so every message on it is a turn of that
+agent rather than of the orchestrator:
+
+```rust,no_run
+# async fn demo(runtime: &openhuman_embed::Runtime) -> Result<(), Box<dyn std::error::Error>> {
+use openhuman_embed::{AgentSpec, TelegramChannelSpec};
+
+let agent = runtime.agent(AgentSpec::new("teeny-chat").system_prompt("You are Teeny."))?;
+let telegram = runtime.channels().telegram(
+ TelegramChannelSpec::new("123456:bot-token", "teeny-chat")
+ .allow_everyone() // or .allowed_users(["alice", "4242"])
+ .mention_only(true), // in groups, answer only when mentioned
+)?;
+// The bot answers until the listener is stopped or dropped.
+# let _ = (agent, telegram);
+# Ok(()) }
+```
+
+- **The agent answers as itself.** Each message runs with the agent's system
+ prompt, tool scope, `AgentSpec::tools` belt and attachments, provider and
+ context. History is per chat, kept by the channel the same way it is for the
+ orchestrator. Typing indicators, streamed drafts and the reply work as for
+ any channel. Call `telegram()` from inside the tokio runtime; it spawns the
+ listener there.
+- **Bind to an agent that exists.** `telegram()` refuses an id with no live
+ agent on the runtime (`ChannelError::UnknownAgent`). If the agent is dropped
+ while the bot is running, each message gets a short "not available" reply and
+ an error is logged. The message is never handed to the orchestrator.
+- **Turns are untrusted and read-only.** Anyone who can message the bot writes
+ the turn's text, so it runs as `AgentTurnOrigin::ExternalChannel` and is
+ capped at `PermissionLevel::ReadOnly`. A tool that writes, executes or has an
+ external effect is withheld from the turn's tool list or refused at once with
+ a tool error. It never waits on an approval, because the only person who
+ could answer one is the sender. Give a public agent read-only host tools, and
+ do anything with side effects outside the chat turn (a cron job, host code).
+- **Under the hood** this is the core's channel runtime for that one bot,
+ started under the runtime's context with
+ `agent.channel_agents = { telegram = "<agent id>" }`. A core started from a
+ config file can bind a channel the same way: any channel name works as the
+ key, and the agent is resolved through `agent::host_agents`.
+
## Harness: the one-agent shorthand
`Harness` is a `Runtime` plus exactly one agent, built from one set of