Skip to content

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

docs(agent): add README for the agent module

Steven Enamakel04d80acc0b8d
diff --git a/crates/openhuman-core/src/agent/README.md b/crates/openhuman-core/src/agent/README.md
index 7205ca8861..166ea5a804 100644
--- a/crates/openhuman-core/src/agent/README.md
+++ b/crates/openhuman-core/src/agent/README.md
@@ -1,102 +1,437 @@
-# Agent
+# agent
-Multi-agent orchestration domain. Owns the LLM tool-calling loop, sub-agent dispatch, conversation transcripts, the trigger-triage pipeline that classifies incoming external events, and the bundled prompt assets in `agent/prompts/`. Does NOT own model construction or provider HTTP transport (`crates/openhuman-core/src/inference/provider/`), tool implementations (`tools/`), or memory storage (`memory/`).
+The agent domain is where a user message becomes model calls, tool calls and
+a reply. It composes OpenHuman's product policy (agent definitions, prompts,
+tool visibility, approvals, budgets, progress events) around the vendored
+TinyAgents loop, and it owns sub-agent delegation, trigger triage, and the
+bundled prompt and archetype assets. Web chat, channel runtimes, cron jobs,
+flows, MCP, voice and the `agent.chat` RPC all reach the model through this
+folder.
-## Public surface
+It does not implement the agent loop itself. The model/tool iteration,
+sessions, transcripts, run queues and orchestration state machines live in
+[`vendor/tinyagents`](../../../../vendor/tinyagents/). Most of the code here
+is adapters that hand TinyAgents the things only OpenHuman knows.
-- `pub struct OpenHumanSessionHost` / `pub struct SessionHostBuilder` / `pub struct TurnOverrides` (`session_host/types.rs`, re-exported from `agent`): top-level conversation runtime, the entry point for any chat turn. Constructors live in `session_host/builder/factory.rs`; `run_single` in `session_host/runtime/run_loop.rs`. The `builder/`, `runtime/`, and `turn/` submodules are private.
-- `pub fn run_subagent` / `pub struct SubagentRunOptions` / `pub enum SubagentRunError` (`subagent_host/`): OpenHuman policy adapters around the neutral TinyAgents sub-agent lifecycle.
-- `pub struct AgentDefinition` / `pub struct AgentDefinitionRegistry` / `pub enum SandboxMode` / `pub enum ToolScope` (`harness/definition/`: `agent_definition.rs`, `registry.rs`, `source.rs`, `tier.rs`, `execution_spec.rs`, `prompt_source.rs`, `subagents.rs`): sub-agent archetypes loaded from built-ins and workspace TOML.
-- `pub mod harness::fork_context`: task-local parent context for KV-cache reuse.
-- `tinytools_agent::dialect::ToolDialect` / `tinytools_agent::ParsedToolCall` / `tinytools_agent::dialect::ToolOutcome`: canonical tool-call vocabulary; `message_convert.rs` performs only concrete durable/provider conversions.
-- `pub mod triage` (`run_triage`, `apply_decision`, `TriggerEnvelope`, `TriageDecision`, `TriageAction`, in `triage/mod.rs`): classifies external triggers and escalates to sub-agents.
-- `pub mod prompts::SystemPromptBuilder` (`prompts/`): system-prompt section composer.
-- `messages.rs` (`history_wire`): the `{role, content}` serde adapter for host-owned files that embed message rows (durable sub-agent sessions, pause checkpoints). Rows are `tinyagents_session::transcript::TranscriptMessage`; typed tool-call history is `tinytools_agent::dialect::TranscriptEntry`.
-- `pub fn bus::register_agent_handlers` (`bus.rs`): registers the `agent.run_turn` native request handler (`AgentTurnRequest` -> `AgentTurnResponse`) on `BUS.native()`; called from `channels/runtime/startup/start_channels.rs`.
-- Built-in archetypes live in `crates/openhuman-core/src/agent/registry/agents/`; this module stays focused on harness/runtime behavior.
-- RPC `agent.{chat, chat_simple, server_status, list_definitions, get_definition, reload_definitions, triage_evaluate, graph_topologies, registry_snapshot}`: `schemas.rs`.
-- Read-only replay RPC `agent.{runs_active, run_status, run_events}` (`tinyagents/replay/schemas.rs`): pages a run's durable journal or status without holding the run open.
+## How it works
-## Submodule map
+### Layering
-| Path | Purpose |
+```text
+ callers: web_chat, channels, cron, flows, mcp, voice, agent.chat RPC
+ | |
+ | OpenHumanSessionHost | BUS.native() "agent.run_turn"
+ | (stateful, per thread) | (stateless, caller owns history)
+ v v
+ +-----------------------------------------------------------------+
+ | agent/session_host/ agent/bus.rs + harness/graph.rs |
+ | builder, hooks, codec, driver |
+ +-----------------------------------------------------------------+
+ | |
+ v v
+ +-----------------------------------------------------------------+
+ | agent/tinyagents/ the adapter seam |
+ | run_root_turn_via_hosted_agent / run_turn_via_tinyagents_shared|
+ | TurnModelSource, middleware stack, host/ capability adapters, |
+ | OpenhumanEventBridge, journal |
+ +-----------------------------------------------------------------+
+ |
+ v
+ +-----------------------------------------------------------------+
+ | vendor/tinyagents |
+ | tinyagents-runtime Session: history, resume, commit |
+ | tinyagents-session SessionRef, transcripts, run ledger |
+ | tinyagents-harness AgentHarness loop, middleware, RunQueue |
+ | tinyagents-graph / -orchestration delegation, sub-agents |
+ | tinytools, tinyinference Tool trait, ChatModel, providers |
+ +-----------------------------------------------------------------+
+ | |
+ v v
+ crate::tools (Tool impls) crate::inference (providers, models)
+```
+
+There are two ways into a turn. The stateful one is `OpenHumanSessionHost`
+(re-exported from `crate::agent`), which owns one conversation, resumes it
+from its transcript, and is what web chat, cron, flows, MCP and voice use.
+The stateless one is the `agent.run_turn` native bus handler in `bus.rs`:
+the caller passes the whole history, the tool registry and a
+`TurnModelSource` as owned Rust values in an `AgentTurnRequest` and gets an
+`AgentTurnResponse` back. Channel runtimes
+(`channels/runtime/dispatch/processor/turn.rs`) and the triage evaluator use
+the bus path because they keep their own history.
+
+### A chat turn, end to end
+
+```text
+ web_chat::run_task (or cron, flows, mcp, ...)
+ | build or reuse OpenHumanSessionHost::from_config_for_agent(cfg, id)
+ v
+ run_single_with_origin(msg, origin) session_host/runtime/run_loop.rs
+ | prompt-injection guard, publish AgentTurnStarted
+ v
+ turn_with_origin session_host/runtime_session_turn.rs
+ | stage attachments, build OpenHumanRunContext (origin, progress,
+ | thread, workspace, cancellation), resume transcript if bound
+ v
+ tinyagents_runtime::Session::turn
+ |-- OpenHumanSessionHooks::before_turn prompt, memory pack, tools
+ |-- OpenHumanSessionDriver::execute session_host/driver.rs
+ | v
+ | run_chat_turn_graph session_host/turn/graph.rs
+ | v
+ | run_root_turn_via_hosted_agent tinyagents/turn_runner.rs
+ | | assemble_turn_harness: models + tools + middleware
+ | v
+ | AgentHarness loop <---------------------------------+
+ | model call -> tool calls -> middleware -> results --+
+ | | events -> OpenhumanEventBridge -> AgentProgress -> on_progress
+ | v
+ | grounded close if the loop hit its cap
+ |-- OpenHumanTranscriptCodec durable rows, commit
+ |-- after_commit memory, BUS, dual-write
+ v
+ publish AgentTurnCompleted, return final text
+```
+
+In order:
+
+1. The caller builds a session host. `from_config_for_agent` resolves the
+ `AgentDefinition` for the agent id, picks its model and provider role,
+ assembles the tool registry, and wires the `ContextManager`,
+ `ToolPolicy`, run queue and progress sender. Web chat caches one host per
+ thread (`web_chat/session.rs`); cron builds one per job.
+2. The caller scopes an `AgentTurnOrigin` (`turn_origin.rs`) and calls
+ `run_single_with_origin`. The origin (`WebChat`, `ExternalChannel`,
+ `TrustedAutomation`, `Cli`, `DirectChat`, `Unknown`) is what the approval
+ gate reads to decide trust. `run_single_with_origin` runs the
+ prompt-injection guard from `security::prompt_injection`, publishes
+ `DomainEvent::AgentTurnStarted`, and calls `turn_with_origin`.
+3. `turn_with_origin` stages any attachments, builds the explicit per-turn
+ carrier `OpenHumanRunContext` (`tinyagents/host/run_context.rs`), and
+ resumes the conversation if it is bound to a `SessionRef` with
+ `ResumeMode::Session`. On resume it adopts the recorded tool list so
+ Composio actions the thread used before are rebuilt as deferred executors
+ (`session_host/recorded_tools.rs`).
+4. TinyAgents' `Session::turn` runs the lifecycle. Its `before_turn` hook
+ (`OpenHumanSessionHooks`) prepares the request: system prompt (frozen
+ for a resumed thread), per-turn memory pack, integration, MCP and skill
+ announcements, and the tool surface. The tool surface is rebuilt as one
+ unit (executable tools, visible specs, deferred names, policy) so the
+ schema the model sees and the authority the driver enforces cannot drift.
+5. The session calls `OpenHumanSessionDriver::execute` with the full
+ history and the tool snapshot. The driver builds this turn's tiered
+ `ChatModel` set from `TurnModelSource`, resolves the context window, and
+ calls `run_chat_turn_graph`, which calls
+ `run_root_turn_via_hosted_agent`.
+6. `assemble_turn_harness` (`tinyagents/harness_assembly.rs`) registers the
+ models, the tools (through `CanonicalSharedToolAdapter`) and the
+ middleware stack: approval and security gating, tool policy, cost
+ budget, tool-output capping and summarization, credential scrubbing,
+ repeated-failure circuit breaker, research budget, stop hooks, and the
+ generic context ladder from `tinyagents-harness`. Then the harness loop
+ runs: model call, parse tool calls, run them through middleware, append
+ results, repeat until the model answers or a cap is hit.
+7. Every harness `AgentEvent` goes through `OpenhumanEventBridge`
+ (`tinyagents/observability/event_bridge.rs`), which turns it into an
+ `AgentProgress` value (`progress.rs`) on the session's `on_progress`
+ channel and feeds usage to `platform::cost`. Web chat forwards that
+ stream to the socket in `web_chat/progress_bridge.rs`. A `TurnJournal`
+ (`tinyagents/journal.rs`) writes the same events to a durable JSONL
+ store that the replay RPCs read.
+8. If the loop paused at its model-call cap without a conclusion, the driver
+ runs a tools-disabled grounded close (`session_host/driver/grounded_close.rs`).
+9. The runtime commits the turn. `OpenHumanTranscriptCodec`
+ (`session_host/codec.rs`) converts between durable `TranscriptMessage`
+ rows and model `Message`s. After the durable commit, the host publishes
+ `ConversationTurnCommitted` for memory ingest, optionally dual-writes
+ the legacy session format (`session_import::live`), and accounts usage
+ against any active goal.
+10. `run_single_with_origin` publishes `AgentTurnCompleted` (or
+ `AgentError`) and returns the final text.
+
+The bus path is shorter. `bus.rs` scopes the origin, sandbox mode and file
+state agent id, then calls `harness::run_channel_turn_via_graph`
+(`harness/graph.rs`), which calls `run_turn_via_tinyagents_shared`. There is
+no TinyAgents `Session`: the caller owns the history vector, and
+`ask_user_clarification` is an early-exit tool whose question becomes the
+reply.
+
+### Sessions and transcripts
+
+A conversation's durable identity is
+`tinyagents_session::transcript::SessionRef`, derived from the thread id and
+the agent id with no timestamp, so one thread maps to one transcript file in
+every process. The session host binds it and resumes with
+`ResumeMode::Session`. A resumed session reuses its persisted system prompt
+and tool declarations verbatim, which keeps the provider's prefix cache warm
+across restarts; the cost is that prompt edits and new skills do not reach
+an existing thread. Compaction seals the current generation and opens a new
+one with the sealed one as parent, so nothing is erased.
+
+By default transcripts, journals, goals and todos are files under the
+workspace. A host that keeps them in its own database installs a
+`SessionStoreProvider` once through `session_store/` (re-exported from
+`tinyagents_session::port`), and every agent then reads and writes through
+its `AgentStores`. See [session_host/README.md](session_host/README.md).
+
+### Sub-agents and orchestration
+
+A parent agent delegates by calling a tool. `spawn_subagent`
+(`orchestration/tools/spawn_subagent.rs`) and its siblings
+(`spawn_async_subagent`, `spawn_parallel_agents`, `continue_subagent`,
+`steer_subagent`, `wait_subagent`, `close_subagent`, `delegate`) look up the
+target `AgentDefinition` and call `subagent_host::run_subagent_with_parent`.
+
+```text
+ parent harness loop
+ | tool call: spawn_subagent { agent_id, prompt }
+ v
+ orchestration/tools/spawn_subagent.rs
+ | read ParentExecutionContext (task-local, harness/fork_context.rs)
+ v
+ subagent_host::run_subagent_with_parent
+ | resolve definition, model, narrowed tools, narrowed prompt,
+ | workspace/sandbox, SubagentScope for progress
+ v
+ tinyagents-orchestration::subagent lifecycle
+ v
+ run_turn_via_tinyagents_shared (subagent_host/ops/graph/dispatch.rs)
+ | child events -> Subagent* AgentProgress variants on parent channel
+ v
+ one compact tool_result back to the parent
+```
+
+The child gets a filtered tool list (the definition's `tools`,
+`disallowed_tools`, `skill_filter`), its own system prompt, and often a
+cheaper model. Its progress shows up on the parent's channel as
+`SubagentSpawned`, `SubagentToolCallStarted`, `SubagentTextDelta` and the
+other `Subagent*` variants. Spawn depth is capped by
+`harness::MAX_SPAWN_DEPTH`. Detached runs are tracked through TinyAgents'
+`DetachedTaskRegistry` and can be steered through the shared
+`SteeringRegistry` (`tinyagents/host/steering.rs`).
+
+`orchestration/` is the larger control plane on top of that: agent teams,
+the command center, declarative workflow runs, git worktrees for parallel
+coding workers, and background completion delivery. See
+[orchestration/README.md](orchestration/README.md) and
+[subagent_host/README.md](subagent_host/README.md).
+
+### Agent definitions, registry and prompts
+
+An `AgentDefinition` (`harness/definition/`) is an archetype: id, tier,
+prompt source, model spec, `ToolScope`, `SandboxMode`, iteration policy and
+allowed sub-agents. Built-ins live in `registry/agents/<id>/` as an
+`agent.toml` plus `prompt.md` (orchestrator, planner, critic, summarizer,
+trigger_triage, trigger_reactor, vision_agent and others). Custom
+definitions are TOML files under `<workspace>/agents/*.toml`, with
+`~/.openhuman/agents/*.toml` as a fallback (`harness/definition_loader.rs`).
+`AgentDefinitionRegistry` holds the merged set; `agent.reload_definitions`
+rebuilds it.
+
+`registry/` is the user-facing layer over those definitions: enable and
+disable, custom agents, model pins, tool allow and deny lists, and sub-agent
+policy, persisted at `Config.agent_registry` and exposed as the
+`agent_registry` RPC namespace. `library/` projects definitions into a
+display-safe `AgentDefinitionDisplay` for the UI.
+
+`prompts/` holds the bundled identity files (`SOUL.md`, `IDENTITY.md`,
+`ROLE.md`, `STYLE.md`, `USER.md`) and `SystemPromptBuilder`, which renders
+ordered `PromptSection`s into the system prompt, optionally as a
+`TieredPrompt` with cache breakpoints. `context/` keeps per-session
+`ContextManager` state and the byte-stable channel prompt builder. `debug/`
+renders the exact prompt a live session would send, by building a real
+session host and calling `build_system_prompt`.
+
+### Triage
+
+External events (Composio triggers, incoming webhooks, task-source cards,
+desktop notifications, the `agent.triage_evaluate` RPC) are not chat turns.
+The caller wraps the event in a `TriggerEnvelope`, `run_triage` sends a
+zero-tool `trigger_triage` turn over `agent.run_turn` (cloud first, one
+retry, then a local model arm, else `Deferred`), and `apply_decision` acts
+on the parsed `drop` / `acknowledge` / `react` / `escalate` decision.
+`react` and `escalate` pass the approval gate and then run `trigger_reactor`
+or `orchestrator` through `subagent_host::run_subagent`. Cron jobs do not go
+through triage. See [triage/README.md](triage/README.md).
+
+## Layout
+
+Turn execution:
+
+| Path | What it does |
| --- | --- |
-| `artifacts/` | Agent-generated artifact storage, retrieval, and lifecycle ([README](artifacts/README.md)) |
-| `context/` | System-prompt assembly and per-session `ContextManager` bookkeeping (utilisation stats, budget, session-memory triggers) ([README](context/README.md)) |
-| `debug/` | Renders the exact system prompt a live session would see for a given agent, via `Agent::from_config_for_agent` |
-| `experience/` | Local procedural operating experience capture for self-learning ([README](experience/README.md)) |
-| `file_state/` | Process-wide read/write stamps so parallel sub-agents and worker threads detect stale file contents before writing |
-| `goals/` | Host adapters around `tinyagents_graph::goals`: workspace-store resolution, domain events, turn accounting, and the `goal_*` tools ([README](goals/README.md)) |
-| `harness/` | Legacy/product prompt and definition helpers used by the session host; generic loop mechanics are imported from TinyAgents ([README](harness/README.md)) |
-| `harness_init/` | One-time first-run provisioning (Python/spaCy/Kompress/Node) before the harness can run ([README](harness_init/README.md)) |
-| `learning/` | Reflection, tool-outcome tracking, user-profile inference from transcripts ([README](learning/README.md)) |
-| `library/` | Safe, user-facing projection of agent definitions (`AgentDefinitionDisplay`) |
-| `orchestration/` | Command center, workflow runs, agent teams, worktrees, subagent control, `spawn_subagent` and its sibling tools ([README](orchestration/README.md)) |
-| `plan_review/` | Interactive plan-review gate that parks a live turn on a thread-scoped plan |
-| `progress_tracing.rs` + `progress_tracing/` (`pub(crate)`) | Structured OpenTelemetry/Langfuse-style spans off the `progress::AgentProgress` stream ([README](progress_tracing/README.md)) |
-| `prompts/` | Prompt types, section builders, `SystemPromptBuilder` ([README](prompts/README.md)) |
-| `registry/` | User-facing agent registry: defaults, enablement, custom agents, tool policy; `registry/agents/` holds built-in archetypes ([README](registry/README.md)) |
-| `session_db/` | `run_ledger` RPC controllers over the durable run ledger; the store itself lives in `tinyagents::session::run_ledger` |
-| `session_import/` | One-time import of legacy OpenHuman session JSONL/Markdown into TinyAgents stores ([README](session_import/README.md)) |
-| `subagent_host/` | OpenHuman planner, executor and persistence adapters for `tinyagents-orchestration::subagent`; policy, provider/model selection, tool narrowing, progress, artifacts and durable product projection live here |
-| `tinyagents/` | Integration with the vendored `tinyagents` loop/replay crate: `TurnModelSource`, middleware, journal, `replay/schemas.rs` ([README](tinyagents/README.md)) |
-| `todos/` | Session-scoped todo list adapters around the TinyAgents todo store; falls back to an in-memory scratch list outside a chat session ([README](todos/README.md)) |
-| `tools/` | Agent-loop control tools (`ask_clarification`, `delegate`, `plan_exit`, `remember_preference`, `save_preference`, `run_workflow`, `todo`), re-exported through `crate::tools` ([README](tools/README.md)) |
-| `triage/` | Classifies external `TriggerEnvelope`s and escalates to sub-agents ([README](triage/README.md)) |
-
-Flat files: `bus.rs` (`agent.run_turn` native request handler), `context_breakdown.rs` (RPC `agent.context_breakdown`, a UI-facing system/tools/history breakdown of prompt size, cached per agent), `cost.rs` (`pub(crate)`, per-turn token/cost accounting), `error.rs` (typed retryable/permanent loop errors), `hooks.rs` (post-turn self-learning hooks), `host_runtime.rs` (native shell execution backend), `message_convert.rs` (`pub(crate)`, transcript/provider conversion), `messages.rs` (transcript types), `multimodal.rs` (attachment handling), `platform_shell.rs` (cross-platform shell selection shared with `host_runtime` and `sandbox::ops`), `progress.rs` (`AgentProgress` channel), `progress_sink.rs` (task-local progress sink for in-process embedders), `queued_turn.rs` (host-owned payload for a message queued while a turn is active; TinyAgents' `RunQueue` owns the queue mechanics), `stop_hooks.rs` (mid-turn policy halts), `tool_policy.rs` (pre-execution tool-call policy hook), `turn_origin.rs` (task-local trust/routing label read by the approval gate), `turn_workspace.rs` (task-local per-turn filesystem root).
-
-## RPC namespaces owned by this tree
-
-`agent`, `agent_registry`, `harness_init`, `session_import`, `plan_review`, `run_ledger` (session_db), `agent_experience` (experience), `ai` (artifacts), `learning`, `agent_team`, `agent_work` (orchestration/command_center), `workflow_run`, `worktree`, `subagent` (orchestration/subagent_control): all registered under `DomainGroup::Agent` in `core/all.rs`.
-
-`crate::core::Outcome` is the controller result type; the JSON-RPC protocol, client and server that expose controllers live in the separate `crates/openhuman-rpc` crate, which depends on this one.
-
-## Calls into
-
-- `crates/openhuman-core/src/inference/provider/`: `factory::{provider_for_role, create_chat_model_with_model_id}` build the crate-native `ChatModel`s that `tinyagents::TurnModelSource` runs each turn against; `ChatResponse` / `ToolCall` / `UsageInfo` DTOs cross this boundary. There is no `Provider` trait; the harness names crate model types only.
-- `crates/openhuman-core/src/tools/`: `Tool` / `ToolSpec` execution surface invoked from the tool loop.
-- `crates/openhuman-core/src/memory/`: episodic indexing.
-- `crates/openhuman-core/src/inference/local/`: `agent_chat` / `agent_chat_simple` execution backend.
-- `crates/openhuman-core/src/config/`: runtime config load via `config::rpc::load_config_with_timeout` (`config::rpc` is `pub use ops as rpc`).
-- `crates/openhuman-core/src/core/bus.rs` (`BUS.publish`/`BUS.subscribe`/`BUS.native()`) and `crates/openhuman-core/src/core/events.rs` (`DomainEvent`): emits `AgentTurnStarted` / `AgentTurnCompleted` / `AgentError`, `AgentOrchestration*`, and `TriggerEvaluated`; subscribers live in `orchestration/{background_delivery,run_ledger_finalize}.rs` and `learning/`, not in `agent/bus.rs`.
-
-## Called by
-
-- `crates/openhuman-core/src/channels/runtime/dispatch/` (`processor*.rs`, `routing.rs`): drives chat turns through the `agent.run_turn` native handler; `web_chat/` (`session.rs`, `run_task.rs`) builds `Agent`s directly.
-- `crates/openhuman-core/src/cron/scheduler/agent_run.rs::run_agent_job`: builds an `Agent` directly via `Agent::from_config_for_agent` and delivers output through `scheduler/delivery.rs::deliver_if_configured`; it does not go through triage.
-- `crates/openhuman-core/src/skills/webhooks/{ops,bus}.rs`: webhook ingestion routes through `triage::run_triage` + `apply_decision`.
-- `crates/openhuman-core/src/memory/sync/composio/bus*.rs`: Composio trigger envelopes go through `agent::triage`.
-- `crates/openhuman-core/src/integrations/task_sources/route.rs`: external task-source events go through the same `TriggerEnvelope` → `run_triage` → `apply_decision` path.
-- `crates/openhuman-core/src/desktop/notifications/rpc.rs`: `notification_ingest` kicks off background triage to back-fill the notification score.
-- `crates/openhuman-core/src/agent/schemas.rs::handle_triage_evaluate`: `agent.triage_evaluate`, the dry-run triage entry point exposed over RPC.
-- `crates/openhuman-core/src/agent/learning/{reflection,tool_tracker,user_profile}.rs`: read transcripts + tool outcomes.
-- `crates/openhuman-core/src/agent/orchestration/tools/{dispatch,spawn_subagent}.rs`: `spawn_subagent` tool delegates to `subagent_host`.
-- `crates/openhuman-core/src/core/runtime/services.rs`: starts task-source polling and runs `agent::harness_init::run_harness_init` during core startup.
-- `crates/openhuman-core/src/core/all.rs`: controller registry wires all `agent`, `agent_registry`, `harness_init`, `plan_review`, `artifacts`, `experience`, `learning`, `session_db`, `session_import`, and `orchestration` controllers under `DomainGroup::Agent`.
+| `session_host/` | `OpenHumanSessionHost`: builder, runtime session, driver, hooks, transcript codec, resume and recorded tools ([README](session_host/README.md)) |
+| `tinyagents/` | Adapter seam onto the TinyAgents harness: turn runner, model source, middleware stack, `host/` capability adapters, event bridge, journal, replay RPC ([README](tinyagents/README.md)) |
+| `harness/` | Agent definitions and loader, parent/fork context task-locals, sandbox and spawn-depth context, oversized tool-result artifacts, the channel/CLI turn graph ([README](harness/README.md)) |
+| `bus.rs` | The `agent.run_turn` native request handler (`AgentTurnRequest` to `AgentTurnResponse`) |
+| `progress.rs` | `AgentProgress`, the event enum every turn streams to its observer |
+| `progress_sink.rs` | Task-local progress sink for in-process embedders driving an RPC that returns only text |
+| `progress_tracing.rs`, `progress_tracing/` | OpenTelemetry/Langfuse-style spans built from the progress stream ([README](progress_tracing/README.md)) |
+| `turn_origin.rs` | `AgentTurnOrigin` task-local, the trust and routing label the approval gate reads |
+| `turn_workspace.rs` | Task-local per-turn filesystem root an embedder binds one turn to |
+| `stop_hooks.rs` | `StopHook` mid-turn halts and the scoped `with_tool_call_limit` budget |
+| `tool_policy.rs` | `ToolPolicy`, the pre-execution allow/deny hook installed through the builder |
+| `hooks.rs` | `PostTurnHook` background hooks, including ones registered by embedders |
+| `queued_turn.rs` | `QueuedTurn`, the payload for a message sent while a turn is running |
+| `error.rs` | `AgentError`, typed retryable and permanent loop errors |
+| `cost.rs` | Per-turn token and cost accounting (`TurnCost`) |
+| `message_convert.rs`, `messages.rs` | Conversions between `TranscriptMessage` rows and TinyAgents `Message`s; the `history_wire` adapter for host-owned files |
+| `attachments/`, `multimodal.rs` | Durable user uploads and provider-side attachment resolution; legacy marker compatibility ([README](attachments/README.md)) |
+| `host_runtime.rs`, `platform_shell.rs` | Native and Docker shell `RuntimeAdapter`s and the shared `cmd.exe` vs `sh` selection |
+| `session_store/` | Process-wide `SessionStoreProvider` slot for hosts with their own database |
-## Tests
+Delegation and coordination:
+
+| Path | What it does |
+| --- | --- |
+| `subagent_host/` | Runs a child agent: definition, model and tool narrowing, prompt, checkpoints, progress ([README](subagent_host/README.md)) |
+| `orchestration/` | Spawn/steer/wait tools, teams, command center, workflow runs, worktrees, background delivery ([README](orchestration/README.md)) |
+| `triage/` | Classifies `TriggerEnvelope`s and escalates ([README](triage/README.md)) |
+| `goals/` | Host adapters around `tinyagents_graph::goals` and the `goal_*` tools ([README](goals/README.md)) |
+| `todos/` | Session-scoped todo list over the TinyAgents todo store ([README](todos/README.md)) |
+| `plan_review/` | `request_plan_review` gate that parks a web-chat turn until the user decides |
+| `tools.rs`, `tools/` | Loop control tools: `delegate`, `plan_exit`, `todo`, `run_workflow`/`await_workflow` (with `skills`) ([README](tools/README.md)) |
+
+Definitions, prompts and context:
+
+| Path | What it does |
+| --- | --- |
+| `registry/` | `agent_registry` RPC; built-in archetypes under `registry/agents/` ([README](registry/README.md)) |
+| `prompts/` | Bundled identity files, `SystemPromptBuilder`, prompt sections ([README](prompts/README.md)) |
+| `context/` | `ContextManager` per session and the channel prompt builder ([README](context/README.md)) |
+| `context_breakdown.rs` | `agent.context_breakdown`: system/tools/history split of prompt size for the UI |
+| `debug/` | Dumps the exact system prompt and prompt-size report for an agent |
+| `library/` | Display-safe projection of definitions (`AgentDefinitionDisplay`) |
+
+Persistence, setup and RPC:
+
+| Path | What it does |
+| --- | --- |
+| `artifacts/` | Agent-generated artifact storage and the `ai` RPC namespace ([README](artifacts/README.md)) |
+| `session_db/` | `run_ledger` RPC over `tinyagents_session::run_ledger` |
+| `session_import/` | One-time import of legacy session files and the optional live dual-write ([README](session_import/README.md)) |
+| `harness_init/` | First-run provisioning (Python, spaCy, Kompress, Node) ([README](harness_init/README.md)) |
+| `schemas.rs` | The `agent` namespace controllers |
-- Unit: `agent_tests.rs`, `multimodal_tests.rs`, plus `*_tests.rs` files colocated with `bus.rs`, `cost.rs`, `error.rs`, `hooks.rs`, `host_runtime.rs`, `message_convert.rs`, `platform_shell.rs`, `progress_sink.rs`, `schemas.rs`, `stop_hooks.rs`, `tool_policy.rs`, `turn_origin.rs`, `turn_workspace.rs`, and under `harness/`, `session_host/`, `triage/`.
-- Integration: `tests/in_process/agent_builder_public.rs`, `tests/in_process/agent_harness_public.rs`, `tests/agent_harness_e2e.rs`, `tests/in_process/agent_turn_overrides_e2e.rs`, `tests/in_process/agent_approval_memory_coverage_e2e.rs`.
-- Schema regression: `schemas_tests.rs` (`controller_schema_inventory_is_stable`).
+## Key types and entry points
-## Related docs
+- `OpenHumanSessionHost` (`session_host/types.rs`): one conversation.
+ Construct with `from_config`, `from_config_for_agent` or
+ `from_config_with_definition` (`session_host/builder/factory.rs`), or the
+ `SessionHostBuilder` fluent API. Drive with `run_single` or
+ `run_single_with_origin` (`session_host/runtime/run_loop.rs`).
+ `TurnOverrides` adjusts the next turn only.
+- `AgentTurnRequest` / `AgentTurnResponse` and `AGENT_RUN_TURN_METHOD`
+ (`bus.rs`): the stateless turn over `BUS.native()`.
+ `register_agent_handlers` installs it at startup.
+- `TurnModelSource` (`tinyagents/turn_models.rs`): builds a turn's primary,
+ tier-routed and summarizer `ChatModel`s from `(role, config)`.
+- `run_turn_via_tinyagents_shared` and `run_root_turn_via_hosted_agent`
+ (`tinyagents/turn_runner.rs`): the only places a harness is invoked.
+- `OpenHumanRunContext` (`tinyagents/host/run_context.rs`): the explicit
+ per-turn carrier (origin, progress, thread, workspace, cancellation, stop
+ hooks) passed into TinyAgents and down to recursive tools.
+- `AgentProgress` (`progress.rs`): turn, iteration, tool, text and thinking
+ deltas, cost updates and the `Subagent*` family.
+- `AgentDefinition`, `AgentDefinitionRegistry`, `ToolScope`, `SandboxMode`
+ (`harness/definition/`): what an agent is.
+- `ParentExecutionContext` (`harness/fork_context.rs`): task-local parent
+ state a sub-agent reads; `orchestration::parent_context::build_root_parent`
+ builds one for runs with no parent turn.
+- `run_subagent`, `run_subagent_with_parent`, `SubagentRunOptions`
+ (`subagent_host/lifecycle.rs`, `subagent_host/types.rs`).
+- `run_triage`, `apply_decision`, `TriggerEnvelope` (`triage/`).
+- `SystemPromptBuilder` (`prompts/builder.rs`).
+- `AgentTurnOrigin` and `with_origin` (`turn_origin.rs`).
-- [gitbooks/developing/architecture/agent-harness.md](../../../../gitbooks/developing/architecture/agent-harness.md)
-- [gitbooks/developing/agent-observability.md](../../../../gitbooks/developing/agent-observability.md)
+## RPC surface
+
+All controllers register under `DomainGroup::Agent` in `core/all.rs`.
+
+| Namespace | Owner | Methods |
+| --- | --- | --- |
+| `agent` | `schemas.rs` | `chat`, `chat_simple`, `server_status`, `list_definitions`, `get_definition`, `reload_definitions`, `triage_evaluate`, `graph_topologies`, `registry_snapshot`, `context_breakdown` |
+| `agent` | `tinyagents/replay/` | `runs_active`, `run_status`, `run_events` (read-only journal replay) |
+| `agent` | `tinyagents/run_mode.rs` | `set_run_mode`, `get_run_mode` |
+| `agent_registry` | `registry/` | default and custom agent CRUD, enablement, tool policy |
+| `harness_init` | `harness_init/` | first-run provisioning status and control |
+| `plan_review` | `plan_review/` | `decide` and friends |
+| `ai` | `artifacts/` | artifact listing and retrieval |
+| `run_ledger` | `session_db/` | run ledger queries |
+| `session_import` | `session_import/` | legacy import |
+| `agent_work` | `orchestration/command_center` | command center views |
+| `workflow_run` | `orchestration/workflow_runs` | workflow runs |
+| `agent_team` | `orchestration/agent_teams` | teams |
+| `worktree` | `orchestration/worktree_schemas.rs` | worktrees |
+| `subagent` | `orchestration/subagent_control.rs` | running sub-agent control |
+
+`agent.chat` and `agent.chat_simple` load config and call
+`inference::host_runtime::rpc::agent_chat` / `agent_chat_simple`, which
+build an `OpenHumanSessionHost`. `chat_simple` is a bare provider call with
+no tools. The JSON-RPC server itself is in `crates/openhuman-rpc`.
+
+## Boundaries
+
+What lives elsewhere:
+
+- The agent loop, tool-call parsing and dialects, sessions, transcripts,
+ resume, compaction generations, run queues, steering, the run ledger, the
+ sub-agent lifecycle, goals and todos stores, and generic middleware (context
+ compression, microcompact, final-call wrap-up, arg recovery) belong to
+ `vendor/tinyagents` (`tinyagents-runtime`, `-session`, `-harness`,
+ `-graph`, `-orchestration`). A bug there is fixed upstream, not worked
+ around here.
+- The `Tool` trait and tool types are in `vendor/tinyagents/vendor/tinytools`.
+ `ChatModel`, providers and message types are in `tinyinference`.
+- Tool implementations are in `crate::tools` and the owning domains. This
+ folder only owns the loop-control tools in `tools/` and the orchestration
+ tools.
+- Provider construction and model routing are in `crate::inference::provider`
+ (`factory::provider_for_role`, `create_chat_model_with_model_id`).
+- The approval gate and prompt-injection guard are in `crate::security`; this
+ folder scopes the origin they read and installs the middleware that calls
+ them.
+- Memory storage is in `crate::memory`; the agent publishes
+ `ConversationTurnCommitted` and reads a memory pack, nothing more.
+- The web socket protocol and per-thread session cache are in
+ `crate::web_chat`; channel adapters are in `crate::channels`.
+- The JSON-RPC server is in `crates/openhuman-rpc`.
+
+## Gotchas
+
+- Task-locals do not cross `tokio::spawn`. `AGENT_TURN_ORIGIN`,
+ `ParentExecutionContext`, `with_tool_call_limit` and the turn workspace
+ must be re-scoped in any spawned task. An unscoped turn reads origin
+ `Unknown` and the approval gate fails closed. Recursive execution passes
+ `OpenHumanRunContext` explicitly for this reason.
+- The tool surface is all or nothing. An empty visible-tool set is deny-all,
+ not "every tool"; `run_chat_turn_graph` deliberately keeps `Some(empty)`.
+- A resumed thread's prompt and tools are frozen. Testing a prompt change
+ needs a new thread.
+- `agent.run_turn` must be registered before channels dispatch.
+ `register_agent_handlers` runs from `channels/runtime/startup/start_channels.rs`
+ and `core/runtime/subscribers.rs`; tests that replace the handler restore
+ it through the guard in `bus.rs`.
+- `stop_hooks::with_tool_call_limit(Some(n), turn)` narrows the TinyAgents
+ invocation budget for one awaited turn, including parallel calls. Zero
+ permits no tool calls. Nested scopes take the smaller limit, `None` keeps
+ the enclosing one, and the limit applies per run, not as an aggregate
+ across child runs.
+- `journal.rs` is best-effort. A journal write failure logs `[journal]` and
+ does not fail the turn.
+
+## Tests
-### Scoped tool-call budgets for embedders
+Unit tests sit beside their modules as `*_tests.rs` (for example
+`agent_tests.rs`, `bus_tests.rs`, `schemas_tests.rs` with
+`controller_schema_inventory_is_stable`, and the suites under
+`session_host/`, `tinyagents/`, `subagent_host/`, `triage/`). Run them with
+`cargo test -p openhuman agent::` or `pnpm debug rust agent::`.
-`stop_hooks::with_tool_call_limit(Some(n), turn)` narrows the real TinyAgents
-invocation budget for one awaited turn without changing the agent's persistent
-configuration. Zero permits no tool invocations. The adapter applies the limit
-to both run policy and run configuration, including parallel calls counted by
-TinyAgents.
+Integration coverage is in `tests/agent_harness_e2e.rs`
+(`cargo test -p openhuman-cli --test agent_harness_e2e`) and in
+`tests/in_process/agent_*.rs`, aggregated into the `in_process_all` target.
-Nested scopes take the smaller limit; `None` preserves an enclosing limit.
-Exiting or dropping the future restores the caller's scope, and concurrent
-turns do not share limits. This bounds calls within each run, not a shared
-aggregate across child runs. Task-local values do not automatically propagate
-through `tokio::spawn`; callers creating a separate task must scope that turn
-explicitly. Without a limit, existing iteration-derived limits are unchanged.
+Related docs:
+[agent harness architecture](../../../../gitbooks/developing/architecture/agent-harness.md),
+[agent observability](../../../../gitbooks/developing/agent-observability.md).