Skip to content

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

fix(runtime): stop background delivery from blocking tenant shutdown

Steven Enamakel0441029b80cf
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index aaaa2c9f03..95d85305d6 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -167,7 +167,9 @@ fn schedule_delivery(thread_id: String, delay: Duration) {
.lock()
.expect("scheduled")
.push((thread_id.clone(), delay));
- tokio::spawn(async move {
+ // Scoped: the thread -> workspace table is keyed per profile, so the
+ // delayed task must run under the profile that scheduled it.
+ crate::core::runtime::spawn_scoped(async move {
tokio::time::sleep(delay).await;
try_deliver(thread_id).await;
});
diff --git a/crates/openhuman-core/src/core/runtime/spawn.rs b/crates/openhuman-core/src/core/runtime/spawn.rs
index 91a56a2a34..ce3225f267 100644
--- a/crates/openhuman-core/src/core/runtime/spawn.rs
+++ b/crates/openhuman-core/src/core/runtime/spawn.rs
@@ -64,19 +64,14 @@ where
/// What [`CoreContext::propagate`] wraps a future in: the caller's context
/// captured now and re-entered wherever the future is polled.
///
-/// Single-user processes carry [`CoreContext::current`] and nothing else, as
-/// before. In SaaS only the caller's own task scope is carried (an unscoped
-/// caller's future runs with no context and so fails closed), together with
-/// the memory identity, exactly as [`scoped`] does — but without requiring
+/// Single-user processes carry [`CoreContext::current`]. In SaaS only the
+/// caller's own task scope is carried (an unscoped caller's future runs with
+/// no context and so fails closed). Both carry the memory identity, exactly
+/// as [`scoped`] does — but without requiring
/// the future to be `Send`.
pub(crate) fn carry<F: Future>(fut: F) -> impl Future<Output = F::Output> {
- let saas = super::is_saas();
let ctx = captured();
- let identity = if saas {
- crate::memory::scope::current()
- } else {
- None
- };
+ let identity = crate::memory::scope::current();
async move {
let fut = async move {
match ctx {
diff --git a/crates/openhuman-core/src/core/runtime/tenant.rs b/crates/openhuman-core/src/core/runtime/tenant.rs
index e43abb3a58..6fcd9bced4 100644
--- a/crates/openhuman-core/src/core/runtime/tenant.rs
+++ b/crates/openhuman-core/src/core/runtime/tenant.rs
@@ -194,8 +194,9 @@ fn take_counted(raw: &str) -> Option<(&str, &str)> {
///
/// Without a profile it is the agent, or
/// [`DEFAULT_AGENT`](crate::agent::session_store::DEFAULT_AGENT) — today's
-/// keys. With one it is `<profile>~<agent|default>`, the profile
-/// percent-escaped (`%` and `~`) so the first `~` always ends it. An agent
+/// keys (percent-escaping `%` and `~`, so it never holds a `~`). With a
+/// profile it is `<profile>~<agent|default>`, the profile escaped the same way
+/// so the first `~` always ends it. An agent
/// named `default` is the default agent.
pub fn session_key(tenant: &Tenant) -> String {
let agent = tenant
@@ -204,7 +205,10 @@ pub fn session_key(tenant: &Tenant) -> String {
.unwrap_or(crate::agent::session_store::DEFAULT_AGENT);
match &tenant.profile {
Some(profile) => format!("{}~{agent}", escape_profile(profile)),
- None => agent.to_string(),
+ // Escaped like a profile so an unprofiled key never contains `~` and
+ // so cannot equal `<profile>~<agent>`; ids without `%` or `~` are
+ // unchanged.
+ None => escape_profile(agent),
}
}
diff --git a/crates/openhuman-core/src/platform/cost/global.rs b/crates/openhuman-core/src/platform/cost/global.rs
index 8d23a15561..2d83bb84e1 100644
--- a/crates/openhuman-core/src/platform/cost/global.rs
+++ b/crates/openhuman-core/src/platform/cost/global.rs
@@ -158,10 +158,14 @@ pub fn seed_tenant_tracker(
config.workspace_dir.display()
);
}
- Err(err) => log::warn!(
- "[cost] could not seed the tenant tracker at {}: {err}",
- config.workspace_dir.display()
- ),
+ Err(err) => {
+ // Never keep a tracker over a previous workspace.
+ *ctx.agent_state().slot::<TenantTracker>().0.write() = None;
+ log::warn!(
+ "[cost] could not seed the tenant tracker at {}: {err}",
+ config.workspace_dir.display()
+ );
+ }
}
}