Skip to content

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

test(scope): add unit tests for memory scope module

Steven Enamakeld1f9a50a4b5e
diff --git a/crates/openhuman-core/src/memory/scope_tests.rs b/crates/openhuman-core/src/memory/scope_tests.rs
new file mode 100644
index 0000000000..9b442ef07e
--- /dev/null
+++ b/crates/openhuman-core/src/memory/scope_tests.rs
@@ -0,0 +1,118 @@
+use super::*;
+
+use crate::config::MemoryAgentConfig;
+
+fn config() -> Config {
+ Config::default()
+}
+
+fn ns(value: &str) -> Namespace {
+ value.parse().unwrap()
+}
+
+#[test]
+fn the_main_agent_is_the_root_and_others_get_their_own_node() {
+ let config = config();
+ assert_eq!(namespace_for(&config, "orchestrator", None), Namespace::ROOT);
+ assert_eq!(namespace_for(&config, "researcher", None), ns("agent:researcher"));
+ assert_eq!(
+ namespace_for(&config, "writer", Some("acme")),
+ ns("team:acme/agent:writer")
+ );
+ assert_eq!(
+ namespace_for(&config, "orchestrator", Some("acme")),
+ ns("team:acme"),
+ "a root agent in a team writes the team's node"
+ );
+ assert_eq!(namespace_for(&config, "", None), Namespace::ROOT);
+ assert_eq!(
+ namespace_for(&config, "a.b", None).segments()[0].kind(),
+ SegmentKind::Agent,
+ "an id outside the charset is sanitized, not refused"
+ );
+}
+
+#[test]
+fn config_pins_a_node_and_can_switch_inheritance_off() {
+ let mut config = config();
+ config.memory.agents.insert(
+ "analyst".into(),
+ MemoryAgentConfig {
+ namespace: Some("project:q4".into()),
+ inherit: false,
+ context: None,
+ },
+ );
+ config.memory.agents.insert(
+ "broken".into(),
+ MemoryAgentConfig {
+ namespace: Some("not a namespace".into()),
+ ..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")));
+ assert_eq!(
+ MemoryIdentity::for_agent(&config, "broken").namespace,
+ ns("agent:broken"),
+ "an invalid pin falls back to the derived node"
+ );
+ config.memory.root_agents.push("assistant".into());
+ assert_eq!(namespace_for(&config, "assistant", None), Namespace::ROOT);
+}
+
+#[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"));
+ assert_eq!(
+ scout.reach().nodes(),
+ vec![Namespace::ROOT, ns("agent:researcher"), scout.namespace.clone()]
+ );
+ assert_eq!(
+ researcher.child(&config, "orchestrator").namespace,
+ 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"));
+}
+
+#[test]
+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}"));
+ }
+ assert_eq!(identity.namespace.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);
+ })
+ .await;
+ within_agent(&config, "researcher", async {
+ let inner = current().unwrap();
+ assert_eq!(inner.agent_id.as_deref(), Some("researcher"));
+ assert_eq!(inner.namespace, ns("agent:researcher"));
+ })
+ .await;
+ })
+ .await;
+ assert_eq!(current(), None);
+}