Skip to content

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

refactor(memory): decouple MemoryIdentity from Config

Steven Enamakel111aba443fbe
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index a6fb8ee10e..3f250ec9dd 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -979,10 +979,9 @@ impl OpenHumanSessionHost {
SessionTurnRequest::new(user_message_from_text(user_message)),
options,
);
- let outcome =
- crate::memory::scope::within_agent(&agent_id, Box::pin(turn))
- .await
- .map_err(|error| anyhow::anyhow!(error.to_string()))?;
+ let outcome = crate::memory::scope::within_agent(&agent_id, Box::pin(turn))
+ .await
+ .map_err(|error| anyhow::anyhow!(error.to_string()))?;
Ok(outcome.output.unwrap_or_default())
}
diff --git a/crates/openhuman-core/src/memory/context_tests.rs b/crates/openhuman-core/src/memory/context_tests.rs
index 4114c7948d..edafca3be9 100644
--- a/crates/openhuman-core/src/memory/context_tests.rs
+++ b/crates/openhuman-core/src/memory/context_tests.rs
@@ -212,7 +212,7 @@ async fn each_agent_gets_its_own_context_md_from_its_reach() {
assert_eq!(refresh_all(&config).await, 3);
assert!(context_path_for(&config.workspace_dir, &Namespace::agent("planner")).is_file());
- let injected = crate::memory::scope::within_agent(&config, "researcher", async {
+ let injected = crate::memory::scope::within_agent("researcher", async {
injection_block(&config).unwrap()
})
.await;
@@ -235,8 +235,7 @@ async fn an_agent_without_a_document_falls_back_to_its_ancestor_s() {
.unwrap();
refresh(&config).await.unwrap();
let injected =
- crate::memory::scope::within_agent(&config, "scout", async { injection_block(&config) })
- .await;
+ crate::memory::scope::within_agent("scout", async { injection_block(&config) }).await;
assert!(injected.unwrap().contains("Sam"));
config.memory.agents.insert(
@@ -247,7 +246,6 @@ async fn an_agent_without_a_document_falls_back_to_its_ancestor_s() {
},
);
let none =
- crate::memory::scope::within_agent(&config, "quiet", async { injection_block(&config) })
- .await;
+ crate::memory::scope::within_agent("quiet", async { injection_block(&config) }).await;
assert!(none.is_none(), "an agent can switch its context off");
}
diff --git a/crates/openhuman-core/src/memory/scope.rs b/crates/openhuman-core/src/memory/scope.rs
index c57d1ac871..e95a5f4430 100644
--- a/crates/openhuman-core/src/memory/scope.rs
+++ b/crates/openhuman-core/src/memory/scope.rs
@@ -148,7 +148,11 @@ pub fn namespace_for(config: &Config, agent_id: &str, team: Option<&str>) -> Nam
/// Whether `agent_id` reads and writes the root node.
#[must_use]
pub fn is_root_agent(config: &Config, agent_id: &str) -> bool {
- config.memory.root_agents.iter().any(|root| root == agent_id)
+ config
+ .memory
+ .root_agents
+ .iter()
+ .any(|root| root == agent_id)
}
/// `namespace` with a sanitized `kind:id` segment appended; past the depth
diff --git a/crates/openhuman-core/src/memory/scope_tests.rs b/crates/openhuman-core/src/memory/scope_tests.rs
index ad9569862b..ae4fea80d1 100644
--- a/crates/openhuman-core/src/memory/scope_tests.rs
+++ b/crates/openhuman-core/src/memory/scope_tests.rs
@@ -31,6 +31,7 @@ fn the_main_agent_is_the_root_and_others_get_their_own_node() {
"a root agent in a team writes the team's node"
);
assert_eq!(namespace_for(&config, "", None), Namespace::ROOT);
+ assert_eq!(MemoryIdentity::root().namespace(&config), Namespace::ROOT);
assert_eq!(
namespace_for(&config, "a.b", None).segments()[0].kind(),
SegmentKind::Agent,
@@ -56,11 +57,11 @@ fn config_pins_a_node_and_can_switch_inheritance_off() {
..MemoryAgentConfig::default()
},
);
- let analyst = MemoryIdentity::for_agent(&config, "analyst");
- assert_eq!(analyst.namespace, ns("project:q4"));
- assert_eq!(analyst.reach(), Reach::exact(ns("project:q4")));
+ let analyst = MemoryIdentity::agent("analyst");
+ assert_eq!(analyst.namespace(&config), ns("project:q4"));
+ assert_eq!(analyst.reach(&config), Reach::exact(ns("project:q4")));
assert_eq!(
- MemoryIdentity::for_agent(&config, "broken").namespace,
+ MemoryIdentity::agent("broken").namespace(&config),
ns("agent:broken"),
"an invalid pin falls back to the derived node"
);
@@ -71,29 +72,32 @@ fn config_pins_a_node_and_can_switch_inheritance_off() {
#[test]
fn sub_agents_nest_under_their_parent() {
let config = config();
- let main = MemoryIdentity::for_agent(&config, "orchestrator");
- assert_eq!(main.namespace, Namespace::ROOT);
- assert_eq!(main.agent_id.as_deref(), Some("orchestrator"));
- let researcher = main.child(&config, "researcher");
- assert_eq!(researcher.namespace, ns("agent:researcher"));
- let scout = researcher.child(&config, "scout");
- assert_eq!(scout.namespace, ns("agent:researcher/agent:scout"));
+ let main = MemoryIdentity::agent("orchestrator");
+ assert_eq!(main.namespace(&config), Namespace::ROOT);
+ let researcher = main.child("researcher");
+ assert_eq!(researcher.namespace(&config), ns("agent:researcher"));
+ let scout = researcher.child("scout");
+ assert_eq!(scout.lineage, ["orchestrator", "researcher"]);
+ assert_eq!(scout.namespace(&config), ns("agent:researcher/agent:scout"));
assert_eq!(
- scout.reach().nodes(),
+ scout.reach(&config).nodes(),
vec![
Namespace::ROOT,
ns("agent:researcher"),
- scout.namespace.clone()
+ ns("agent:researcher/agent:scout")
]
);
assert_eq!(
- researcher.child(&config, "orchestrator").namespace,
+ researcher.child("orchestrator").namespace(&config),
ns("agent:researcher"),
"a root agent spawned below stays on its parent's node"
);
- assert_eq!(scout.shared_namespace(), Namespace::ROOT);
- let member = MemoryIdentity::team_member(&config, "acme", "writer");
- assert_eq!(member.shared_namespace(), ns("team:acme"));
+ let member = MemoryIdentity::team_member("acme", "writer");
+ assert_eq!(member.namespace(&config).shared_ancestor(), ns("team:acme"));
+ assert_eq!(
+ member.child("editor").namespace(&config),
+ ns("team:acme/agent:writer/agent:editor")
+ );
}
#[test]
@@ -101,25 +105,30 @@ fn deep_spawn_chains_stop_at_the_depth_limit() {
let config = config();
let mut identity = MemoryIdentity::root();
for depth in 0..20 {
- identity = identity.child(&config, &format!("a{depth}"));
+ identity = identity.child(&format!("a{depth}"));
}
- assert_eq!(identity.namespace.depth(), tinymemory::namespace::MAX_DEPTH);
+ assert_eq!(
+ identity.namespace(&config).depth(),
+ tinymemory::namespace::MAX_DEPTH
+ );
}
#[tokio::test]
async fn turns_scope_the_identity_and_nest_inside_another_agent() {
- let config = config();
assert_eq!(current(), None);
- within_agent(&config, "orchestrator", async {
- assert_eq!(current().unwrap().namespace, Namespace::ROOT);
- within_agent(&config, "orchestrator", async {
- assert_eq!(current().unwrap().namespace, Namespace::ROOT);
+ within_agent("orchestrator", async {
+ assert_eq!(current().unwrap().agent_id.as_deref(), Some("orchestrator"));
+ within_agent("orchestrator", async {
+ assert!(
+ current().unwrap().lineage.is_empty(),
+ "the same agent keeps its identity"
+ );
})
.await;
- within_agent(&config, "researcher", async {
+ within_agent("researcher", async {
let inner = current().unwrap();
assert_eq!(inner.agent_id.as_deref(), Some("researcher"));
- assert_eq!(inner.namespace, ns("agent:researcher"));
+ assert_eq!(inner.lineage, ["orchestrator"]);
})
.await;
})
diff --git a/crates/openhuman-core/src/memory/tools.rs b/crates/openhuman-core/src/memory/tools.rs
index 96f022aa34..1d64b0e855 100644
--- a/crates/openhuman-core/src/memory/tools.rs
+++ b/crates/openhuman-core/src/memory/tools.rs
@@ -118,10 +118,10 @@ 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::harness::fork_context::current_parent()
+ .map_or_else(super::scope::MemoryIdentity::root, |parent| {
+ super::scope::MemoryIdentity::agent(&parent.agent_definition_id)
+ })
});
Self {
workspace: Some(workspace.display().to_string()),
diff --git a/crates/openhuman-core/src/memory/tools_tests.rs b/crates/openhuman-core/src/memory/tools_tests.rs
index 9c7ad58ed1..520a861490 100644
--- a/crates/openhuman-core/src/memory/tools_tests.rs
+++ b/crates/openhuman-core/src/memory/tools_tests.rs
@@ -25,10 +25,7 @@ fn facts() -> CallFacts {
thread_id: Some("thread-tool-1".into()),
agent_id: Some("orchestrator".into()),
tool_call_id: Some("call-9".into()),
- ..CallFacts::of(&MemoryIdentity::for_agent(
- &Config::default(),
- "orchestrator",
- ))
+ ..CallFacts::of(&Config::default(), &MemoryIdentity::agent("orchestrator"))
}
}
@@ -36,7 +33,7 @@ fn facts() -> CallFacts {
fn facts_of(agent_id: &str) -> CallFacts {
CallFacts {
thread_id: Some(format!("thread-{agent_id}")),
- ..CallFacts::of(&MemoryIdentity::for_agent(&Config::default(), agent_id))
+ ..CallFacts::of(&Config::default(), &MemoryIdentity::agent(agent_id))
}
}