Skip to content

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

fix(session-host): keep deferred memory commits scoped to their profile

Steven Enamakelf588926c9a99
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 7c5f89f437..4e6bbaa534 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
@@ -108,9 +108,8 @@ impl OpenHumanTurnPrelude {
.pending_memory_turn = Some(PendingMemoryTurn {
identity: identity.clone(),
user_index,
- profile_context: crate::core::runtime::CoreContext::scoped()
- .filter(|context| context.profile().is_some())
- .map(|context| Arc::downgrade(&context)),
+ profile_context: crate::profiles::host::host()
+ .and_then(|host| capture_profile_context(&host)),
});
let pack = crate::memory::lifecycle::prefetch::pre_turn(
config.clone(),
@@ -199,18 +198,32 @@ impl OpenHumanTurnPrelude {
)
.await;
};
- crate::core::runtime::spawn_scoped(async move {
- match profile_context {
- Some(context) => {
- // A retired profile cannot fall back to the operator or a
- // configured external engine when its captured context is gone.
- if let Some(context) = context.upgrade() {
- crate::core::runtime::CoreContext::scope(context, work).await;
- }
- }
- None => work.await,
+ crate::core::runtime::spawn_scoped(deferred_memory_work(profile_context, work));
+ }
+}
+
+// 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>> {
+ crate::core::runtime::CoreContext::scoped()
+ .filter(|context| context.profile().is_some())
+ .map(|context| Arc::downgrade(&context))
+}
+
+async fn deferred_memory_work(
+ profile_context: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
+ work: impl std::future::Future<Output = ()>,
+) {
+ match profile_context {
+ Some(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,
}
}
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 ba16df7954..c3216d9f09 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
@@ -63,3 +63,84 @@ fn committed_tool_calls_pair_each_call_with_its_result() {
assert_eq!(calls[0].result.as_deref(), Some("src/main.rs"));
assert_eq!(calls[1].result, None);
}
+
+#[tokio::test]
+async fn deferred_memory_commit_survives_the_turn_scope_without_pinning_the_profile() {
+ use crate::core::runtime::{CoreContext, DomainSet, SaasConfig};
+ use crate::profiles::{ProfileHost, ProfileId};
+ use tinymemory_api::{
+ LearningKind, ListRequest, MemoryEngine, MemoryMeta, MetaFilter, StoreItem,
+ };
+
+ let root = tempfile::tempdir().unwrap();
+ let engine = Arc::new(tinymemory_api::conformance::ReferenceEngine::new());
+ let capabilities = Arc::new(crate::profiles::capabilities::ProfileCapabilities::new(
+ None,
+ None,
+ None,
+ Default::default(),
+ Vec::new(),
+ Some(engine.clone()),
+ ));
+ let host = ProfileHost::new(
+ SaasConfig::new(root.path()),
+ CoreContext::for_test(DomainSet::full(), None),
+ )
+ .with_capabilities(Some(capabilities));
+ let id = ProfileId::parse("deferred-memory").unwrap();
+ 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)
+ })
+ .await;
+ // The entire temporary turn context is gone before this callback is polled.
+ let config = profile.config.clone();
+ let work = async {
+ assert_eq!(
+ crate::core::runtime::current_tenant()
+ .unwrap()
+ .profile
+ .as_deref(),
+ Some(id.as_str())
+ );
+ let binding = crate::memory::engine::resolve(&config).engine().unwrap();
+ binding
+ .engine
+ .store(StoreItem::learning(
+ "delayed committed reply",
+ LearningKind::Fact,
+ 0.9,
+ MemoryMeta {
+ namespace: config.memory.root.as_ref().unwrap().parse().unwrap(),
+ ..Default::default()
+ },
+ ))
+ .await
+ .unwrap();
+ };
+ deferred_memory_work(captured.clone(), work).await;
+ assert_eq!(
+ engine
+ .list(ListRequest::new(MetaFilter::default(), 10))
+ .await
+ .unwrap()
+ .items
+ .len(),
+ 1,
+ "a still-hosted profile must persist its deferred commit after its turn ends"
+ );
+ drop(profile);
+ assert!(
+ host.release(&id).await.unwrap(),
+ "the pending weak capture cannot pin an idle profile"
+ );
+ assert!(
+ captured.as_ref().unwrap().upgrade().is_none(),
+ "the retired owner is not retained"
+ );
+ deferred_memory_work(captured, async {
+ panic!("retired callbacks must fail closed")
+ })
+ .await;
+}