Skip to content

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

fix(memory): deny deferred memory when profile authority is missing

Steven Enamakelbd95652d82ed
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
index bee1225386..77cd2d08d0 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
@@ -36,7 +36,15 @@ pub(super) struct PendingMemoryTurn {
user_index: u32,
// The canonical runtime can commit on a different task. Restore the
// authenticated profile captured at preparation, never infer it from config.
- profile_context: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
+ profile_context: ProfileMemoryContext,
+}
+
+// Missing profile authority must not be confused with an ordinary desktop turn.
+#[derive(Clone)]
+enum ProfileMemoryContext {
+ NonProfile,
+ BoundWeak(std::sync::Weak<crate::core::runtime::CoreContext>),
+ Denied,
}
/// Where the prompt's verbatim thread starts, as a user turn index, and
@@ -103,6 +111,10 @@ impl OpenHumanTurnPrelude {
if self.omit_memory_context {
identity.recall = false;
}
+ let profile_context = capture_profile_context(crate::profiles::host::host().as_deref());
+ if matches!(profile_context, ProfileMemoryContext::Denied) {
+ return None;
+ }
let user_index = hooks::user_turn_index(committed_turns);
let (in_prompt_from, compacted) = in_prompt_window(history, committed_turns, current);
self.mutable
@@ -111,8 +123,7 @@ impl OpenHumanTurnPrelude {
.pending_memory_turn = Some(PendingMemoryTurn {
identity: identity.clone(),
user_index,
- profile_context: crate::profiles::host::host()
- .and_then(|host| capture_profile_context(&host)),
+ profile_context,
});
let pack = crate::memory::lifecycle::prefetch::pre_turn(
config.clone(),
@@ -207,30 +218,42 @@ impl OpenHumanTurnPrelude {
// Resolve the profile owner at preparation; a canonical commit may run after
// the immutable turn-origin scope has ended.
-fn capture_profile_context(
- host: &crate::profiles::ProfileHost,
-) -> Option<std::sync::Weak<crate::core::runtime::CoreContext>> {
- let context = crate::core::runtime::CoreContext::scoped()?;
- let id = crate::profiles::ProfileId::parse(context.profile()?).ok()?;
+fn capture_profile_context(host: Option<&crate::profiles::ProfileHost>) -> ProfileMemoryContext {
+ let Some(context) = crate::core::runtime::CoreContext::scoped() else {
+ return if crate::core::runtime::is_saas() {
+ ProfileMemoryContext::Denied
+ } else {
+ ProfileMemoryContext::NonProfile
+ };
+ };
+ let Some(profile_id) = context.profile() else {
+ return ProfileMemoryContext::NonProfile;
+ };
+ let Some(profile) = crate::profiles::ProfileId::parse(profile_id)
+ .ok()
+ .and_then(|id| host.and_then(|host| host.get(&id)))
+ else {
+ return ProfileMemoryContext::Denied;
+ };
// The host owns this root for the lease's lifetime. A derived turn-origin
// context ends with the turn, even if its deferred commit is still queued.
// Keeping only a weak root cannot create an agent/context reference cycle.
- host.get(&id)
- .map(|profile| Arc::downgrade(profile.context()))
+ ProfileMemoryContext::BoundWeak(Arc::downgrade(profile.context()))
}
async fn deferred_memory_work(
- profile_context: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
+ profile_context: ProfileMemoryContext,
work: impl std::future::Future<Output = ()>,
) {
match profile_context {
- Some(context) => {
+ ProfileMemoryContext::BoundWeak(context) => {
// A retired profile cannot fall back to a configured external engine.
if let Some(context) = context.upgrade() {
crate::core::runtime::CoreContext::scope(context, work).await;
}
}
- None => work.await,
+ ProfileMemoryContext::NonProfile => work.await,
+ ProfileMemoryContext::Denied => {}
}
}
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest_tests.rs b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest_tests.rs
index 2369d97604..29ee52c29b 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest_tests.rs
@@ -91,7 +91,7 @@ async fn deferred_memory_commit_survives_the_turn_scope_without_pinning_the_prof
host.provision(&id).await.unwrap();
let profile = host.open(&id).await.unwrap();
let captured = CoreContext::scope_with_turn_origin(profile.context().clone(), None, async {
- capture_profile_context(&host)
+ capture_profile_context(Some(&host))
})
.await;
// The entire temporary turn context is gone before this callback is polled.
@@ -136,7 +136,7 @@ async fn deferred_memory_commit_survives_the_turn_scope_without_pinning_the_prof
"the pending weak capture cannot pin an idle profile"
);
assert!(
- captured.as_ref().unwrap().upgrade().is_none(),
+ matches!(&captured, ProfileMemoryContext::BoundWeak(context) if context.upgrade().is_none()),
"the retired owner is not retained"
);
deferred_memory_work(captured, async {
@@ -161,9 +161,44 @@ async fn missing_profile_owner_refuses_deferred_memory_instead_of_running_unscop
)
.profile("missing-owner"),
);
- let captured = CoreContext::scope(missing, async { capture_profile_context(&host) }).await;
+ let captured =
+ CoreContext::scope(missing, async { capture_profile_context(Some(&host)) }).await;
deferred_memory_work(captured, async {
panic!("a failed profile owner capture must not run under the configured external engine")
})
.await;
}
+
+#[tokio::test]
+async fn failed_profile_capture_is_denied_but_ordinary_desktop_work_still_runs() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
+ use crate::tools::toolpacks::ToolGroups;
+ let operator = CoreContext::for_test(DomainSet::full(), None);
+ for id in ["missing-host", "invalid/owner"] {
+ let profile = operator.derive_with(
+ ContextOverlay::new(
+ crate::config::Config::default(),
+ DomainSet::full(),
+ ToolGroups::none(),
+ )
+ .profile(id),
+ );
+ let captured = CoreContext::scope(profile, async { capture_profile_context(None) }).await;
+ assert!(matches!(captured, ProfileMemoryContext::Denied));
+ deferred_memory_work(captured, async {
+ panic!("a profile without its host cannot resolve an external engine")
+ })
+ .await;
+ }
+ let captured = CoreContext::scope(operator, async { capture_profile_context(None) }).await;
+ assert!(matches!(captured, ProfileMemoryContext::NonProfile));
+ let mut ran = false;
+ deferred_memory_work(captured, async {
+ ran = true;
+ })
+ .await;
+ assert!(
+ ran,
+ "ordinary desktop work retains its existing deferred behavior"
+ );
+}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
index 83c01337de..b8fc9193fb 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
@@ -88,9 +88,8 @@ pub(crate) async fn run_host_agent_turn(
session.set_on_progress(progress);
let text = host
- .scope(crate::core::runtime::CoreContext::scope_with_turn_origin(
- host.context.clone(),
- Some(origin.clone()),
+ .scope(crate::agent::turn_origin::with_origin(
+ origin.clone(),
session.run_single_with_origin(&message, Some(origin)),
))
.await?;
diff --git a/crates/openhuman-embed/tests/profile_host_capabilities.rs b/crates/openhuman-embed/tests/profile_host_capabilities.rs
index de55b7c13e..8807d8b780 100644
--- a/crates/openhuman-embed/tests/profile_host_capabilities.rs
+++ b/crates/openhuman-embed/tests/profile_host_capabilities.rs
@@ -167,7 +167,7 @@ impl Respond for CallTools {
#[test]
fn chat_and_relay_use_only_selected_tools_inside_their_profile() {
let _ = tracing_subscriber::fmt()
- .with_env_filter("warn,openhuman_core::memory::lifecycle=debug")
+ .with_env_filter("warn")
.with_test_writer()
.try_init();
runtime_without_master_key().block_on(async {