Skip to content

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

feat(memory): add run context to memory tools

Steven Enamakel1f13bc7e0ffa
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
index e0b51d8fe0..671aeec1e4 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -408,6 +408,24 @@ pub(crate) struct HarnessHostPolicy {
pub(crate) struct HostOperationContext {
pub(crate) origin: Option<AgentTurnOrigin>,
pub(crate) sandbox_mode: Option<SandboxMode>,
+ /// The agent definition the run's parent execution context belongs to, for
+ /// tools that act on behalf of the spawning agent when no memory identity
+ /// is scoped (see `memory::tools::CallFacts::gather`).
+ pub(crate) parent_agent_id: Option<String>,
+}
+
+/// Reads the spawning agent's definition id from the explicit tool carrier.
+/// Direct tool dispatch has no parent.
+pub(crate) fn parent_agent_id_for_tool(
+ context: Option<&dyn tinytools::ToolRunContext>,
+) -> Option<String> {
+ context
+ .and_then(tinytools::ToolRunContext::host_extension)
+ .and_then(|extension| {
+ extension.downcast_ref::<tinyagents_harness::tool::ToolExecutionContext>()
+ })
+ .and_then(|context| context.state::<HostOperationContext>())
+ .and_then(|host| host.parent_agent_id.clone())
}
/// Reads the calling agent's sandbox policy from the explicit tool carrier.
@@ -679,6 +697,10 @@ impl OpenHumanRunContext {
let host_operations = Arc::new(HostOperationContext {
origin: self.origin.clone(),
sandbox_mode: self.sandbox_mode,
+ parent_agent_id: self
+ .parent
+ .as_ref()
+ .map(|parent| parent.agent_definition_id.clone()),
});
let context = tinyagents_harness::context::RunContext::new(config, self)
.with_cancellation(cancellation)
diff --git a/crates/openhuman-core/src/memory/tools.rs b/crates/openhuman-core/src/memory/tools.rs
index 86ce64ab93..b632e4fb0a 100644
--- a/crates/openhuman-core/src/memory/tools.rs
+++ b/crates/openhuman-core/src/memory/tools.rs
@@ -147,9 +147,9 @@ impl CallFacts {
let identity = super::scope::current().unwrap_or_else(|| {
// Outside a scoped turn (a legacy driver), fall back to the
// parent context's agent, else the root.
- crate::agent::harness::fork_context::current_parent()
- .map_or_else(super::scope::MemoryIdentity::root, |parent| {
- super::scope::MemoryIdentity::agent(&parent.agent_definition_id)
+ crate::agent::tinyagents::host::parent_agent_id_for_tool(context)
+ .map_or_else(super::scope::MemoryIdentity::root, |parent_agent_id| {
+ super::scope::MemoryIdentity::agent(&parent_agent_id)
})
});
Self {