Skip to content

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

feat(memory): add tool write tracking to memory tools

Steven Enamakel84e633d0a5a6
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
index 1666236528..903eda75f7 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
@@ -17,6 +17,9 @@ pub(crate) struct HostOperationContext {
/// 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>,
+ /// Who the run acts as on memory; see
+ /// [`OpenHumanRunContext::memory_identity`](super::OpenHumanRunContext::memory_identity).
+ pub(crate) memory_identity: Option<crate::memory::scope::MemoryIdentity>,
}
/// Reads the spawning agent's definition id from the explicit tool carrier.
@@ -33,6 +36,20 @@ pub(crate) fn parent_agent_id_for_tool(
.and_then(|host| host.parent_agent_id.clone())
}
+/// Reads the run's memory identity from the explicit tool carrier. Direct tool
+/// dispatch has none.
+pub(crate) fn memory_identity_for_tool(
+ context: Option<&dyn tinytools::ToolRunContext>,
+) -> Option<crate::memory::scope::MemoryIdentity> {
+ 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.memory_identity.clone())
+}
+
/// Reads the calling agent's sandbox policy from the explicit tool carrier.
/// Direct tool dispatch has no agent-specific mode.
pub(crate) fn sandbox_mode_for_tool(
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 42addc6da4..221b667baa 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -307,6 +307,13 @@ pub struct OpenHumanRunContext {
pub parent: Option<ParentExecutionContext>,
/// Context-preparation sources already consumed in this turn.
pub prepared_context_sources: Arc<Vec<AgentContextPreparedSource>>,
+ /// Who this run acts as on memory: its agent definition and team. Set by
+ /// the entry point that starts the run (`MemoryIdentity::root().child(id)`
+ /// for a top-level turn, `MemoryIdentity::team_member` for a team member)
+ /// and derived with `child(definition id)` for each sub-agent; read by the
+ /// memory tool and the turn's memory hooks. `None` means the host acts for
+ /// itself (the root identity).
+ pub memory_identity: Option<crate::memory::scope::MemoryIdentity>,
/// File-state identity used to detect stale parent reads after child writes.
pub file_state_agent_id: Option<String>,
/// Host-owned artifact index for tool-result references.
@@ -408,7 +415,7 @@ pub(crate) struct HarnessHostPolicy {
}
pub(crate) use super::operation_context::{
- approval_for_tool, parent_agent_id_for_tool, sandbox_mode_for_tool, HostOperationContext,
+ approval_for_tool, memory_identity_for_tool, parent_agent_id_for_tool, sandbox_mode_for_tool, HostOperationContext,
};
#[cfg(test)]
pub(crate) use super::operation_context::tool_context_with_approval;
@@ -468,6 +475,7 @@ impl OpenHumanRunContext {
turn_deadline: None,
parent: None,
prepared_context_sources: Arc::new(Vec::new()),
+ memory_identity: None,
file_state_agent_id: None,
tool_result_artifact_index: None,
attachment_placeholders: Arc::new(Vec::new()),
@@ -678,6 +686,7 @@ impl OpenHumanRunContext {
.parent
.as_ref()
.map(|parent| parent.agent_definition_id.clone()),
+ memory_identity: self.memory_identity.clone(),
});
let context = tinyagents_harness::context::RunContext::new(config, self)
.with_cancellation(cancellation)
diff --git a/crates/openhuman-core/src/memory/tool_writes.rs b/crates/openhuman-core/src/memory/tool_writes.rs
index 95970d0d3d..54adced01b 100644
--- a/crates/openhuman-core/src/memory/tool_writes.rs
+++ b/crates/openhuman-core/src/memory/tool_writes.rs
@@ -452,8 +452,10 @@ pub(crate) async fn fence_and_clear(config: &Config) -> MemoryResult<WriteFence>
}
/// Starts a scoped background drain, retaining config only in memory.
-pub(super) fn schedule(config: Arc<Config>) {
- let file = queue_path(&config, super::scope::resolve_current(&config).root());
+///
+/// `root` is the layout root the write was queued under (`CallFacts::reach`).
+pub(super) fn schedule(config: Arc<Config>, root: &Namespace) {
+ let file = queue_path(&config, root);
schedule_partition(config, file);
}
@@ -509,7 +511,10 @@ fn schedule_partition(config: Arc<Config>, file: PathBuf) {
/// after restart; the persisted owner binding must still match the active user.
#[cfg(test)]
pub(crate) async fn drain(config: &Config) -> usize {
- let file = queue_path(config, super::scope::resolve_current(config).root());
+ let file = queue_path(
+ config,
+ super::scope::MemoryIdentity::root().resolve(config).root(),
+ );
let owner = owner_state(config);
let _owner = owner.barrier.read().await;
let state = drain_state(&file);
diff --git a/crates/openhuman-core/src/memory/tools.rs b/crates/openhuman-core/src/memory/tools.rs
index 2ad3c31772..6f0844d978 100644
--- a/crates/openhuman-core/src/memory/tools.rs
+++ b/crates/openhuman-core/src/memory/tools.rs
@@ -139,7 +139,7 @@ pub struct CallFacts {
}
impl CallFacts {
- /// Gathers the facts from the run context and the turn's task-locals,
+ /// Gathers the facts from the run context,
/// falling back to `config.action_dir` for the workspace.
#[must_use]
pub fn gather(config: &Config, context: Option<&dyn ToolRunContext>) -> Self {
@@ -147,9 +147,12 @@ impl CallFacts {
.and_then(ToolRunContext::workspace_root)
.map(std::path::Path::to_path_buf)
.unwrap_or_else(|| config.action_dir.clone());
- 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.
+ // The acting identity is the run's own; direct dispatch (CLI, tests)
+ // has no run, and falls back to the spawning agent, else the root.
+ let identity = crate::agent::tinyagents::host::run_context::memory_identity_for_tool(
+ context,
+ )
+ .unwrap_or_else(|| {
crate::agent::tinyagents::host::run_context::parent_agent_id_for_tool(context)
.map_or_else(super::scope::MemoryIdentity::root, |parent_agent_id| {
super::scope::MemoryIdentity::agent(&parent_agent_id)
@@ -377,6 +380,7 @@ impl Tool for MemoryTool {
) {
let config = self.config.clone();
let write_args = args.clone();
+ let queued_root = facts.reach.at.clone();
let queued = crate::core::runtime::spawn_blocking_scoped(move || {
super::tool_writes::enqueue(&config, &write_args, &facts)
})
@@ -388,7 +392,7 @@ impl Tool for MemoryTool {
))
}) {
Ok(value) => {
- super::tool_writes::schedule(self.config.clone());
+ super::tool_writes::schedule(self.config.clone(), &queued_root);
ToolResult::success(value.to_string())
}
Err(error) => ToolResult::error(render_error(error)),