@@ -82,6 +82,12 @@ impl NamespacePath {
8282 NamespacePath ( b"agent" . to_vec ( ) . try_into ( ) . unwrap ( ) )
8383 }
8484
85+ /// Create a new root agent namespace path from the agent name
86+ pub fn new_agent_root ( agent_name : & [ u8 ] ) -> Result < Self , & ' static str > {
87+ let namespace_path: Vec < _ > = [ NAMESPACE_AGENT_PREFIX , agent_name] . concat ( ) ;
88+ Self :: new_agent ( & namespace_path)
89+ }
90+
8591 /// Create a new namespace path from bytes with validation
8692 pub fn new_agent ( bytes : & [ u8 ] ) -> Result < Self , & ' static str > {
8793 if bytes. is_empty ( ) {
@@ -192,6 +198,19 @@ impl NamespacePath {
192198
193199 parents
194200 }
201+
202+ /// Wether the namespace is an agent root path: "agent.<name>".
203+ pub fn is_agent_root ( & self ) -> bool {
204+ if self . depth ( ) != 2 {
205+ return false ;
206+ }
207+
208+ if let Some ( root) = self . segments ( ) . next ( ) {
209+ root == b"agent"
210+ } else {
211+ false
212+ }
213+ }
195214}
196215
197216impl Debug for NamespacePath {
@@ -293,4 +312,47 @@ mod tests {
293312 assert_eq ! ( parents[ 1 ] . as_bytes( ) , b"agent.alice" ) ;
294313 assert_eq ! ( parents[ 2 ] . as_bytes( ) , b"agent" ) ;
295314 }
315+
316+ #[ test]
317+ fn test_is_agent_root ( ) {
318+ let agent_alice = NamespacePath :: new_agent ( b"agent.alice" ) . unwrap ( ) ;
319+ assert ! ( agent_alice. is_agent_root( ) ) ;
320+
321+ let agent_bob = NamespacePath :: new_agent ( b"agent.bob" ) . unwrap ( ) ;
322+ assert ! ( agent_bob. is_agent_root( ) ) ;
323+
324+ let deeper = NamespacePath :: new_agent ( b"agent.alice.memory" ) . unwrap ( ) ;
325+ assert ! ( !deeper. is_agent_root( ) ) ;
326+
327+ let just_agent = NamespacePath :: agent_root ( ) ;
328+ assert ! ( !just_agent. is_agent_root( ) ) ;
329+ }
330+
331+ #[ test]
332+ fn test_new_agent_root ( ) {
333+ let alice_root = NamespacePath :: new_agent_root ( b"alice" ) . unwrap ( ) ;
334+ assert_eq ! ( alice_root. as_bytes( ) , b"agent.alice" ) ;
335+ assert ! ( alice_root. is_agent_root( ) ) ;
336+
337+ let bob_root = NamespacePath :: new_agent_root ( b"bob" ) . unwrap ( ) ;
338+ assert_eq ! ( bob_root. as_bytes( ) , b"agent.bob" ) ;
339+ assert ! ( bob_root. is_agent_root( ) ) ;
340+
341+ assert ! ( NamespacePath :: new_agent_root( b"alice123" ) . is_ok( ) ) ;
342+ assert ! ( NamespacePath :: new_agent_root( b"alice-test" ) . is_ok( ) ) ;
343+ assert ! ( NamespacePath :: new_agent_root( b"alice_test" ) . is_ok( ) ) ;
344+
345+ assert ! (
346+ NamespacePath :: new_agent_root( b"Alice" ) . is_err( ) ,
347+ "uppercase should fail"
348+ ) ;
349+ assert ! (
350+ NamespacePath :: new_agent_root( b"alice!" ) . is_err( ) ,
351+ "special chars should fail"
352+ ) ;
353+ assert ! (
354+ NamespacePath :: new_agent_root( b"" ) . is_err( ) ,
355+ "empty name should fail"
356+ ) ;
357+ }
296358}
0 commit comments