Skip to content

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

fix(memory): keep deferred profile memory bound to its captured context

Steven Enamakel515d4991c675
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 d96f0e70fb..7c5f89f437 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
@@ -27,10 +27,13 @@ use crate::memory::lifecycle::hooks::{
use crate::memory::scope::ResolvedIdentity;
/// The memory side of the turn in flight, held from pre-turn to commit.
-#[derive(Debug, Clone)]
+#[derive(Clone)]
pub(super) struct PendingMemoryTurn {
identity: ResolvedIdentity,
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>>,
}
/// Where the prompt's verbatim thread starts, as a user turn index, and
@@ -105,6 +108,9 @@ 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)),
});
let pack = crate::memory::lifecycle::prefetch::pre_turn(
config.clone(),
@@ -175,7 +181,8 @@ impl OpenHumanTurnPrelude {
return;
};
let prelude = self.clone();
- crate::core::runtime::spawn_scoped(async move {
+ let profile_context = pending.profile_context.clone();
+ let work = async move {
let Some(config) = prelude.memory_config().await else {
return;
};
@@ -191,6 +198,18 @@ 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,
+ }
});
}
}
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 a19060abfb..83c01337de 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
@@ -68,14 +68,12 @@ pub(crate) async fn run_host_agent_turn(
"[channels::dispatch::host_agent] running channel turn as host agent"
);
+ let relay_thread = crate::channels::providers::relay::current_thread_id();
let mut session = host.session_host(
&config,
- Some(
- crate::channels::providers::relay::current_thread_id()
- .as_deref()
- .unwrap_or(&history_key),
- ),
+ Some(relay_thread.as_deref().unwrap_or(&history_key)),
)?;
+ session.set_thread_id(relay_thread.as_deref());
// The channel names the session, which also selects the channel's
// permission ceiling in the session's tool policy.
session.set_event_context(history_key, channel);
diff --git a/crates/openhuman-embed/tests/profile_host_capabilities.rs b/crates/openhuman-embed/tests/profile_host_capabilities.rs
index 78b34e9c6d..ddd1b2440c 100644
--- a/crates/openhuman-embed/tests/profile_host_capabilities.rs
+++ b/crates/openhuman-embed/tests/profile_host_capabilities.rs
@@ -37,11 +37,20 @@ impl Tool for TenantTool {
fn parameters_schema(&self) -> Value {
json!({"type":"object","properties":{}})
}
- async fn execute_with_context(&self, args: Value, _: tinytools::ToolCallOptions, context: Option<&dyn tinytools::ToolRunContext>) -> anyhow::Result<ToolResult> {
- let thread = context.and_then(tinytools::ToolRunContext::thread_id).expect("every hosted tool has its durable conversation key");
+ async fn execute_with_context(
+ &self,
+ args: Value,
+ _: tinytools::ToolCallOptions,
+ context: Option<&dyn tinytools::ToolRunContext>,
+ ) -> anyhow::Result<ToolResult> {
+ let thread = context
+ .and_then(tinytools::ToolRunContext::thread_id)
+ .expect("every hosted tool has its durable conversation key");
let expected = match openhuman_embed::current_turn_origin().unwrap() {
openhuman_embed::AgentTurnOrigin::WebChat { .. } => "t1",
- openhuman_embed::AgentTurnOrigin::ExternalChannel { .. } => "channel:telegram/sender/chat",
+ openhuman_embed::AgentTurnOrigin::ExternalChannel { .. } => {
+ "channel:telegram/sender/chat"
+ }
other => panic!("unexpected origin {other:?}"),
};
assert_eq!(thread, expected);
@@ -382,7 +391,15 @@ async fn scenario() {
);
}
}
- assert!(inference.received_requests().await.unwrap().iter().all(|request| !request.url.path().contains("memory/experience")), "deferred profile memory must keep its bound host engine");
+ assert!(
+ inference
+ .received_requests()
+ .await
+ .unwrap()
+ .iter()
+ .all(|request| !request.url.path().contains("memory/experience")),
+ "deferred profile memory must keep its bound host engine"
+ );
// Shared backend contents are independent of the actor: actual contract
// requests, including a malicious global-root filter, must stay confined.
for user in ["alice", "bob"] {