Skip to content

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

feat(embed): hand the builder to the core as a HostBoot

Steven Enamakel61469799e1f2
diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rs
index f9663bba8d..6a55a35e48 100644
--- a/crates/openhuman-embed/src/lib.rs
+++ b/crates/openhuman-embed/src/lib.rs
@@ -171,7 +171,7 @@ pub mod seams {
pub use openhuman_core::agent::hooks::{PostTurnHook, ToolHook};
pub use openhuman_core::agent::hooks::{ToolHookContext, ToolHookDecision, TurnContext};
pub use openhuman_core::core::all::{ControllerExtension, DomainGroup};
- pub use openhuman_core::core::server_launcher::{ServeRequest, ServerLauncher};
+ pub use openhuman_core::core::server_launcher::{HostBoot, ServeRequest, ServerLauncher};
pub use openhuman_core::security::SecurityPolicy;
}
diff --git a/crates/openhuman-embed/src/runtime/run.rs b/crates/openhuman-embed/src/runtime/run.rs
index 94ef82e7ea..73aebdd854 100644
--- a/crates/openhuman-embed/src/runtime/run.rs
+++ b/crates/openhuman-embed/src/runtime/run.rs
@@ -1,63 +1,135 @@
//! The CLI entry: [`RuntimeBuilder::run_from_args`] and [`run_from_args`].
//!
-//! `openhuman_core::run_core_from_args` dispatches a whole command line —
-//! `run`/`serve`, `call`, `agent`, `mcp`, the namespace commands — and each
-//! subcommand boots the core its own way (the server through the installed
-//! [`server_launcher`](RuntimeBuilder::server_launcher), the others
-//! in-process). So this is a thin wrapper rather than a builder-driven boot:
-//! it installs the builder's process-global pieces for the life of the
-//! process, then hands the arguments to the core. The builder's runtime
-//! knobs (domains, services, workspace, token, listen) do not apply; a
-//! server launcher reads its own preset.
+//! `openhuman_core::run_core_from_args_with` dispatches a whole command line —
+//! `run`/`serve`, `call`, `agent`, `mcp`, the namespace commands. Only
+//! `run`/`serve` boot a runtime, and they do it through the installed
+//! [`server_launcher`](RuntimeBuilder::server_launcher). So the entry is two
+//! steps:
+//!
+//! 1. the builder's process-global pieces (backend transport, memory engine,
+//! session store, controller extensions, launcher, hooks) are installed for
+//! the life of the process, which every subcommand needs;
+//! 2. what is left of the builder (host kind, domains, services, token,
+//! listener, workspace, tool groups, ...) is handed to the core as a
+//! [`HostBoot`], which rides the `run`/`serve` [`ServeRequest`] to the
+//! launcher. The launcher boots from it and applies the operator's explicit
+//! flags on top (`--host`, `--port`, `--jsonrpc-only`, `--headless-api`);
+//! see [`ServeRequest`].
+//!
+//! [`ServeRequest`]: openhuman_core::core::server_launcher::ServeRequest
use std::sync::Arc;
+use openhuman_core::agent::session_store::SessionStoreProvider;
+use openhuman_core::backend::transport::BackendTransport;
+use openhuman_core::core::server_launcher::HostBoot;
+
+use super::seams::HostSeams;
use super::{RuntimeBuilder, RuntimeError};
+/// The pieces of a builder that are installed process-wide before the CLI
+/// dispatches, split from the rest, which becomes the [`HostBoot`].
+pub(super) struct CliGlobals {
+ pub(super) transport: Option<Arc<dyn BackendTransport>>,
+ pub(super) memory_engine: Option<Arc<dyn tinymemory_api::MemoryEngine>>,
+ pub(super) session_store: Option<Arc<dyn SessionStoreProvider>>,
+ pub(super) seams: HostSeams,
+}
+
impl RuntimeBuilder {
/// Install this builder's process-global pieces, then run the core's
/// command-line dispatcher on `args` (without the binary name).
///
/// Installed for the rest of the process, never restored: the backend
- /// transport (as the process global), the memory engine, the session
- /// store, controller extensions, the server launcher, the tool ranker and
- /// the embedder hooks. A [`live_policy`](Self::live_policy) is ignored
- /// with a warning — each subcommand's boot installs its own.
+ /// transport (as the process global; the builder keeps it too so the
+ /// server's runtime binds it), the memory engine, the session store,
+ /// controller extensions, the server launcher, the tool ranker and the
+ /// embedder hooks.
+ ///
+ /// For `run` / `serve` the rest of the builder is what the server boots
+ /// with. Precedence, highest first: the operator's explicit flags
+ /// (`--host`, `--port`, `--jsonrpc-only`, `--headless-api`, `--mode`),
+ /// then this builder, then the launcher's own preset. `--mode saas` boots
+ /// the operator's SaaS config instead and takes nothing from the builder.
+ /// Other subcommands run in-process and use only the installed globals.
+ ///
+ /// A [`live_policy`](Self::live_policy) is ignored with a warning — each
+ /// subcommand's boot installs its own.
///
/// # Errors
///
/// A refused controller extension, or whatever the dispatched command
/// returns.
- pub fn run_from_args(mut self, args: &[String]) -> anyhow::Result<()> {
+ pub fn run_from_args(self, args: &[String]) -> anyhow::Result<()> {
let command = args.first().map(String::as_str).unwrap_or("<none>");
log::debug!(
"[embed][cli] run_from_args command={command} argc={}",
args.len()
);
- if let Some(transport) = self.backend_transport.take() {
+ let (builder, globals) = self.split_for_cli();
+ globals
+ .install()
+ .map_err(|error| anyhow::Error::new(RuntimeError::Invalid(error)))?;
+
+ let summary = builder.summary();
+ log::debug!(
+ "[embed][cli] handing builder to the core host_kind={:?} domains={:?} \
+ services={:?} fixed_token={} listen_host={:?} listen_port={:?}",
+ summary.host_kind,
+ summary.domains,
+ summary.services,
+ summary.fixed_token,
+ summary.listen_host,
+ summary.listen_port,
+ );
+ openhuman_core::run_core_from_args_with(args, Some(HostBoot::new(builder)))
+ }
+
+ /// Separate the process-global pieces from the builder that boots the
+ /// server. The transport stays on the builder as well, so the server's
+ /// runtime binds it.
+ pub(super) fn split_for_cli(mut self) -> (RuntimeBuilder, CliGlobals) {
+ if self.seams.live_policy.take().is_some() {
+ log::warn!(
+ "[embed][cli] live_policy is ignored by run_from_args; \
+ each subcommand installs the policy its config describes"
+ );
+ }
+ let globals = CliGlobals {
+ transport: self.backend_transport.clone(),
+ memory_engine: self.memory_engine.take(),
+ session_store: self.session_store.take(),
+ seams: std::mem::take(&mut self.seams),
+ };
+ (self, globals)
+ }
+
+ /// Take back the builder [`run_from_args`](Self::run_from_args) handed
+ /// the core, for a [`ServerLauncher`](crate::seams::ServerLauncher) to
+ /// boot from. `None` when the request carries none (the launcher then
+ /// uses its own preset) or it was already taken.
+ pub fn from_host_boot(boot: &HostBoot) -> Option<RuntimeBuilder> {
+ boot.take::<RuntimeBuilder>()
+ }
+}
+
+impl CliGlobals {
+ fn install(self) -> Result<(), String> {
+ if let Some(transport) = self.transport {
openhuman_core::backend::install_backend_transport(transport);
log::debug!("[embed][cli] backend transport installed (process global)");
}
- if let Some(engine) = self.memory_engine.take() {
+ if let Some(engine) = self.memory_engine {
openhuman_core::memory::engine::install_host_engine(engine);
}
- if let Some(provider) = self.session_store.take() {
- let _previous = openhuman_core::agent::session_store::install(Arc::clone(&provider));
+ if let Some(provider) = self.session_store {
+ let _previous = openhuman_core::agent::session_store::install(provider);
log::debug!("[embed][cli] session store installed (process lifetime)");
}
- if self.seams.live_policy.take().is_some() {
- log::warn!(
- "[embed][cli] live_policy is ignored by run_from_args; \
- each subcommand installs the policy its config describes"
- );
- }
- let seams = std::mem::take(&mut self.seams)
- .install()
- .map_err(|error| anyhow::Error::new(RuntimeError::Invalid(error)))?;
+ let seams = self.seams.install()?;
seams.persist();
-
- openhuman_core::run_core_from_args(args)
+ Ok(())
}
}
@@ -68,3 +140,7 @@ impl RuntimeBuilder {
pub fn run_from_args(args: &[String]) -> anyhow::Result<()> {
RuntimeBuilder::cli().run_from_args(args)
}
+
+#[cfg(test)]
+#[path = "run_tests.rs"]
+mod tests;