Skip to content

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

fix(embed): refuse agents when the discovered config fails to load

Steven Enamakel884046eb1f4e
diff --git a/crates/openhuman-embed/src/runtime/build.rs b/crates/openhuman-embed/src/runtime/build.rs
index 6526fe3f3a..b8c4f88920 100644
--- a/crates/openhuman-embed/src/runtime/build.rs
+++ b/crates/openhuman-embed/src/runtime/build.rs
@@ -126,6 +126,14 @@ impl RuntimeBuilder {
Some(self.resolve_config(&resolved).await?)
};
+ // The seams are the last fallible step before side effects a failed
+ // build cannot undo (the stored API key, the host memory engine), so
+ // a refused controller extension leaves neither behind. Restorable
+ // seams are undone by this guard if the boot below fails.
+ let mut seams = std::mem::take(&mut self.seams)
+ .install()
+ .map_err(RuntimeError::Invalid)?;
+
// Before `CoreBuilder::build()`: the scheduler gate reads the credential
// store exactly once, at boot, to decide whether it is signed in.
let has_api_key = match (self.api_key.as_ref(), config.as_ref()) {
@@ -140,11 +148,15 @@ impl RuntimeBuilder {
// applicator, so host policy follows the effective behaviour: only a
// *usable* runtime-default route exempts an inherited install from
// its session gate. An API key is a credential in its own right.
+ // Discovered has no config yet, but the provider is applied to the
+ // agents' base config after boot (see below), so judge it by the
+ // model it carries itself.
let routed_provider_effective = self.provider.has_usable_route()
- && config
- .as_ref()
- .and_then(|config| config.default_model.as_deref())
- .is_some_and(|model| !model.trim().is_empty());
+ && match config.as_ref() {
+ Some(config) => config.default_model.as_deref(),
+ None => self.provider.model_name(),
+ }
+ .is_some_and(|model| !model.trim().is_empty());
let host_kind = effective_host_kind(
self.host_kind,
inherit,
@@ -170,11 +182,6 @@ impl RuntimeBuilder {
previous: previous_session_store,
};
- // Restorable seams are undone by this guard if the boot below fails.
- let mut seams = std::mem::take(&mut self.seams)
- .install()
- .map_err(RuntimeError::Invalid)?;
-
log::debug!(
"[embed][runtime] building host_kind={host_kind:?} config_source={:?} \
inherit_workspace={inherit} routed_provider={} api_key={has_api_key} \
@@ -213,8 +220,8 @@ impl RuntimeBuilder {
let core = Core::from_runtime(Arc::new(runtime));
// Discovered: agents start from the config the core just loaded.
- let mut base_config = match config.take() {
- Some(config) => config,
+ let (mut base_config, config_unavailable) = match config.take() {
+ Some(config) => (config, None),
None => discovered_base_config().await,
};
if discovered {
@@ -242,6 +249,7 @@ impl RuntimeBuilder {
previous_session_store,
Some(seams),
base_config,
+ config_unavailable,
inherit,
domains,
tool_groups,
@@ -293,15 +301,21 @@ impl RuntimeBuilder {
/// The base config for agents of a runtime whose core discovered its own:
/// the same `load_or_init` the core just ran, so the two agree.
-async fn discovered_base_config() -> Config {
+///
+/// When it fails the core has already booted without its workspace-bound
+/// stores. The runtime still comes up (a host that never creates an agent,
+/// like the desktop shell, keeps working), but the failure is returned with
+/// the placeholder config so [`Runtime::agent`] refuses instead of laying an
+/// agent out under the default root, a different workspace and credential
+/// store from the one the operator selected.
+async fn discovered_base_config() -> (Config, Option<String>) {
match Config::load_or_init().await {
- Ok(config) => config,
+ Ok(config) => (config, None),
Err(error) => {
- // The core logged the same failure and skipped its workspace-bound
- // stores; agents fall back to defaults rather than failing a host
- // that may never create one.
- log::warn!("[embed][runtime] discovered config failed to load: {error:#}");
- Config::default()
+ log::warn!(
+ "[embed][runtime] discovered config failed to load; agents will be refused: {error:#}"
+ );
+ (Config::default(), Some(format!("{error:#}")))
}
}
}
diff --git a/crates/openhuman-embed/src/runtime/mod.rs b/crates/openhuman-embed/src/runtime/mod.rs
index 8e467879cc..24ac626e2b 100644
--- a/crates/openhuman-embed/src/runtime/mod.rs
+++ b/crates/openhuman-embed/src/runtime/mod.rs
@@ -221,6 +221,9 @@ pub struct Runtime {
/// The config every agent starts from. Already carries the runtime-wide
/// defaults (backend URL, access, provider model, supplied overrides).
base_config: Config,
+ /// Why the discovered config could not be loaded, when it could not:
+ /// `base_config` is then a placeholder and agents are refused.
+ config_unavailable: Option<String>,
/// Where `Workspace::Inherit` resolved to, for the per-agent layout rule.
inherited: bool,
domains: DomainSet,
@@ -250,6 +253,13 @@ impl Runtime {
/// agent (any clone of it) is alive.
pub fn agent(&self, spec: AgentSpec) -> Result<Agent, AgentError> {
let id = spec.id().to_string();
+ if let Some(error) = &self.config_unavailable {
+ log::warn!("[embed][runtime] agent refused id={id}: config unavailable");
+ return Err(AgentError::Invalid(format!(
+ "the runtime's config failed to load ({error}); refusing to start an agent \
+ on a default workspace"
+ )));
+ }
// Held across `instantiate` (fs layout only, no turn, no await) so a
// concurrent `agent()` call for the same id cannot pass the duplicate
// check while this one is still being built. Releasing the lock
@@ -296,6 +306,11 @@ impl Runtime {
/// [`MemoryError::InvalidRequest`](crate::memory::MemoryError::InvalidRequest)
/// when `root` is not a valid layout root, or is the store root itself.
pub fn memory(&self, root: &str) -> crate::memory::MemoryResult<crate::memory::Memory> {
+ if let Some(error) = &self.config_unavailable {
+ return Err(crate::memory::MemoryError::InvalidRequest(format!(
+ "the runtime's config failed to load ({error})"
+ )));
+ }
crate::memory::Memory::bind(self.base_config.clone(), root)
}
@@ -389,6 +404,7 @@ impl Runtime {
>,
seams: Option<seams::InstalledSeams>,
base_config: Config,
+ config_unavailable: Option<String>,
inherited: bool,
domains: DomainSet,
tool_groups: ToolGroups,
@@ -405,6 +421,7 @@ impl Runtime {
seams,
}),
base_config,
+ config_unavailable,
inherited,
domains,
tool_groups,