Skip to content

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

feat(config): load user agent host settings from config

Steven Enamakelbdc65211bd56
diff --git a/crates/openhuman-core/src/config/ops/loader/load.rs b/crates/openhuman-core/src/config/ops/loader/load.rs
index 0f991b6e4e..eb72552e19 100644
--- a/crates/openhuman-core/src/config/ops/loader/load.rs
+++ b/crates/openhuman-core/src/config/ops/loader/load.rs
@@ -28,8 +28,14 @@ const CONFIG_LOAD_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(
/// resolution found. Normalization still runs, because that is a shaping step
/// handlers depend on, not a re-read.
pub async fn load_config_with_timeout() -> Result<Config, String> {
- if let Some(mut config) = crate::core::runtime::context::CoreContext::current_embedder_config()
- {
+ // SaaS reads only the task's own scope, as `Config::load_or_init` does, so
+ // a task that lost its user scope fails rather than getting the operator's.
+ let scoped = if crate::core::runtime::is_saas() {
+ crate::core::runtime::CoreContext::scoped().and_then(|ctx| ctx.embedder_config().cloned())
+ } else {
+ crate::core::runtime::context::CoreContext::current_embedder_config()
+ };
+ if let Some(mut config) = scoped {
normalize_loaded_config(&mut config).await;
return Ok(config);
}
diff --git a/crates/openhuman-core/src/core/runtime/spawn.rs b/crates/openhuman-core/src/core/runtime/spawn.rs
index d8f8af1468..dc87002e2b 100644
--- a/crates/openhuman-core/src/core/runtime/spawn.rs
+++ b/crates/openhuman-core/src/core/runtime/spawn.rs
@@ -27,6 +27,17 @@ where
tokio::spawn(scoped(fut))
}
+/// The context a spawned task inherits. In SaaS only the caller's own task
+/// scope is carried: an unscoped caller's task gets no context (and so fails
+/// closed) rather than the operator's default one.
+fn captured() -> Option<std::sync::Arc<CoreContext>> {
+ if super::is_saas() {
+ CoreContext::scoped()
+ } else {
+ CoreContext::current()
+ }
+}
+
/// `fut`, wrapped to run under the caller's context and memory identity
/// wherever it is eventually polled.
pub fn scoped<F>(fut: F) -> impl Future<Output = F::Output> + Send
@@ -35,7 +46,13 @@ where
F::Output: Send,
{
let identity = crate::memory::scope::current();
- let fut = CoreContext::propagate(fut);
+ let ctx = captured();
+ let fut = async move {
+ match ctx {
+ Some(ctx) => CoreContext::scope(ctx, fut).await,
+ None => fut.await,
+ }
+ };
async move {
match identity {
Some(identity) => crate::memory::scope::within(identity, fut).await,
@@ -51,7 +68,7 @@ where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
- let ctx = CoreContext::current();
+ let ctx = captured();
let identity = crate::memory::scope::current();
let handle = tokio::runtime::Handle::current();
tokio::task::spawn_blocking(move || {
diff --git a/crates/openhuman-core/src/user_agents/host.rs b/crates/openhuman-core/src/user_agents/host.rs
index 4515f685f6..0de34a5119 100644
--- a/crates/openhuman-core/src/user_agents/host.rs
+++ b/crates/openhuman-core/src/user_agents/host.rs
@@ -85,6 +85,9 @@ impl AgentHost {
/// Create agent `id`'s directories. Returns whether it was new.
pub fn provision(&self, id: &UserAgentId) -> Result<bool, String> {
+ // Under the open-agent lock, like `deprovision`, so the two never
+ // interleave on one agent's directory.
+ let _guard = self.lock();
let layout = self.layout(id);
if layout.meta_path.exists() {
log::debug!("[user_agents] provision agent={id}: already provisioned");
@@ -146,6 +149,11 @@ impl AgentHost {
let mut open = self.lock();
if let Some(slot) = open.get_mut(id) {
slot.last_used = now;
+ }
+ // Every open sweeps agents idle past `idle_evict_secs`, so they close
+ // even when no new user arrives.
+ self.sweep_idle_locked(&mut open, now);
+ if let Some(slot) = open.get(id) {
return Ok(Arc::clone(&slot.state));
}
@@ -244,16 +252,22 @@ impl AgentHost {
self.evict_locked(&mut open, Instant::now());
}
- fn evict_locked(&self, open: &mut HashMap<UserAgentId, Slot>, now: Instant) {
+ /// Close agents idle past `idle_evict_secs` that nothing is using.
+ fn sweep_idle_locked(&self, open: &mut HashMap<UserAgentId, Slot>, now: Instant) {
let idle_limit = Duration::from_secs(self.saas.idle_evict_secs);
- let in_use = |slot: &Slot| Arc::strong_count(&slot.state) > 1;
open.retain(|id, slot| {
- let keep = in_use(slot) || now.duration_since(slot.last_used) < idle_limit;
+ let keep = Arc::strong_count(&slot.state) > 1
+ || now.duration_since(slot.last_used) < idle_limit;
if !keep {
log::debug!("[user_agents] evicted idle agent={id}");
}
keep
});
+ }
+
+ fn evict_locked(&self, open: &mut HashMap<UserAgentId, Slot>, now: Instant) {
+ let in_use = |slot: &Slot| Arc::strong_count(&slot.state) > 1;
+ self.sweep_idle_locked(open, now);
// Still full: make room by closing the least recently used idle one.
if open.len() >= self.saas.max_agents_open.max(1) {
let victim = open