Skip to content

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

feat(core): add turn origin tracking to runtime context

Steven Enamakelf83f84add118
diff --git a/crates/openhuman-core/src/config/schema/load/saas_scope.rs b/crates/openhuman-core/src/config/schema/load/saas_scope.rs
index 2310eb2f2e..2d1c9122c4 100644
--- a/crates/openhuman-core/src/config/schema/load/saas_scope.rs
+++ b/crates/openhuman-core/src/config/schema/load/saas_scope.rs
@@ -14,7 +14,9 @@ use super::super::Config;
pub(super) fn saas_scoped_config() -> Option<anyhow::Result<Config>> {
resolve(
crate::core::runtime::is_saas(),
- crate::core::runtime::CoreContext::current_embedder_config(),
+ // The task's own scope only: falling back to the process default
+ // would hand a task that lost its user scope the operator's config.
+ crate::core::runtime::CoreContext::scoped().and_then(|ctx| ctx.embedder_config.clone()),
)
}
diff --git a/crates/openhuman-core/src/core/all.rs b/crates/openhuman-core/src/core/all.rs
index 5a7553ca56..312a23742e 100644
--- a/crates/openhuman-core/src/core/all.rs
+++ b/crates/openhuman-core/src/core/all.rs
@@ -83,7 +83,9 @@ fn active_domain_set() -> Option<crate::core::runtime::DomainSet> {
/// (full, no filter) so pre-boot unit tests and non-context callers see every
/// domain, exactly as before #4796.
fn group_allowed(group: DomainGroup) -> bool {
- active_domain_set().is_none_or(|s| s.allows(group))
+ // No context: everything but the operator plane, which only a SaaS
+ // `DomainSet` opens.
+ active_domain_set().map_or(group != DomainGroup::Operator, |s| s.allows(group))
}
/// The global static registry of all controllers, initialized once on first access.
diff --git a/crates/openhuman-core/src/core/runtime/context_turn_origin.rs b/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
index 781813f7d6..6e1ba81782 100644
--- a/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
+++ b/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
@@ -8,6 +8,13 @@ impl CoreContext {
.or_else(|| DEFAULT_CONTEXT.get().cloned())
}
+ /// The context scoped onto the current task, with no fallback to the
+ /// process default. SaaS uses it: there, a task that lost its scope must
+ /// fail rather than act as the operator.
+ pub fn scoped() -> Option<Arc<CoreContext>> {
+ CURRENT_CONTEXT.try_with(|ctx| ctx.clone()).ok()
+ }
+
/// The process default context (first built), independent of any active
/// scope. Used by the dispatch chokepoint to establish the ambient scope.
pub fn default_context() -> Option<Arc<CoreContext>> {
diff --git a/crates/openhuman-core/src/core/runtime/spawn.rs b/crates/openhuman-core/src/core/runtime/spawn.rs
index e1931fa5e0..d8f8af1468 100644
--- a/crates/openhuman-core/src/core/runtime/spawn.rs
+++ b/crates/openhuman-core/src/core/runtime/spawn.rs
@@ -45,17 +45,26 @@ where
}
/// Like `tokio::task::spawn_blocking`, but the closure runs under the caller's
-/// [`CoreContext`].
+/// [`CoreContext`] and memory identity.
pub fn spawn_blocking_scoped<F, R>(f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let ctx = CoreContext::current();
+ let identity = crate::memory::scope::current();
let handle = tokio::runtime::Handle::current();
- tokio::task::spawn_blocking(move || match ctx {
- Some(ctx) => handle.block_on(CoreContext::scope(ctx, async move { f() })),
- None => f(),
+ tokio::task::spawn_blocking(move || {
+ let run = async move {
+ match identity {
+ Some(identity) => crate::memory::scope::within(identity, async move { f() }).await,
+ None => f(),
+ }
+ };
+ match ctx {
+ Some(ctx) => handle.block_on(CoreContext::scope(ctx, run)),
+ None => handle.block_on(run),
+ }
})
}
diff --git a/crates/openhuman-core/src/user_agents/host.rs b/crates/openhuman-core/src/user_agents/host.rs
index 611e03b26c..4515f685f6 100644
--- a/crates/openhuman-core/src/user_agents/host.rs
+++ b/crates/openhuman-core/src/user_agents/host.rs
@@ -107,8 +107,19 @@ impl AgentHost {
/// Close agent `id` and archive its state under `<root>/deprovisioned/`.
/// Nothing is deleted. Returns whether there was such an agent.
+ ///
+ /// The open-agent lock is held for the whole operation, so no `open` can
+ /// re-open the agent between closing it and moving its directory. An
+ /// agent still in use (a request holds its state) is not archived from
+ /// under it: deprovisioning fails and can be retried.
pub fn deprovision(&self, id: &UserAgentId) -> Result<bool, String> {
- self.lock().remove(id);
+ let mut open = self.lock();
+ if let Some(slot) = open.get(id) {
+ if Arc::strong_count(&slot.state) > 1 {
+ return Err(format!("agent {id} is in use; try again shortly"));
+ }
+ }
+ open.remove(id);
let layout = self.layout(id);
if !layout.dir.exists() {
return Ok(false);
@@ -116,9 +127,15 @@ impl AgentHost {
let archive = layout::archive_dir(&self.saas.root);
std::fs::create_dir_all(&archive)
.map_err(|e| format!("creating {}: {e}", archive.display()))?;
- let dest = archive.join(format!("{id}-{}", unix_now()));
+ // Unique even when one user is deprovisioned twice in a second.
+ let dest = archive.join(format!(
+ "{id}-{}-{}",
+ unix_now(),
+ uuid::Uuid::new_v4().simple()
+ ));
std::fs::rename(&layout.dir, &dest)
.map_err(|e| format!("archiving {}: {e}", layout.dir.display()))?;
+ drop(open);
log::info!("[user_agents] deprovisioned agent={id} (archived)");
Ok(true)
}
diff --git a/crates/openhuman-core/src/user_agents/layout.rs b/crates/openhuman-core/src/user_agents/layout.rs
index 5c158a8e61..0318071c0c 100644
--- a/crates/openhuman-core/src/user_agents/layout.rs
+++ b/crates/openhuman-core/src/user_agents/layout.rs
@@ -76,6 +76,9 @@ pub fn agent_config(layout: &UserAgentLayout, id: &UserAgentId) -> Config {
action_dir: layout.sandbox_dir.clone(),
..Config::default()
};
+ // Artifacts land in the agent's sandbox, never the host's shared
+ // `~/OpenHuman/projects/Files`.
+ config.files_dir_override = Some(layout.sandbox_dir.join("files"));
config.memory.agent_id = Some(id.to_string());
config.memory.root = Some(memory_root(id));