Skip to content

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

refactor(storage): improve scope parsing and testing

Steven Enamakelb87736ef60cb
diff --git a/crates/openhuman-core/src/storage/scope.rs b/crates/openhuman-core/src/storage/scope.rs
index df8b3cbc60..adba6184ba 100644
--- a/crates/openhuman-core/src/storage/scope.rs
+++ b/crates/openhuman-core/src/storage/scope.rs
@@ -41,7 +41,9 @@ impl Scope {
/// Parses an opaque stored scope without interpreting its host grammar.
/// Use [`Scope::parsed`] when a caller needs OpenHuman tenant semantics.
pub fn parse(value: impl AsRef<str>) -> Result<Self, StorageError> {
- Self::new(value)
+ let scope = Self::new(value)?;
+ scope.parsed()?;
+ Ok(scope)
}
/// The host's single-operator tenant scope.
@@ -170,9 +172,8 @@ pub struct ParsedScope {
impl ParsedScope {
/// Parses a string using the host's scope grammar.
pub fn parse(value: &str) -> Result<Self, StorageError> {
- let invalid = || {
- StorageError::invalid_input(format!("{value:?} is not an OpenHuman storage scope"))
- };
+ let invalid =
+ || StorageError::invalid_input(format!("{value:?} is not an OpenHuman storage scope"));
if value == CLUSTER {
return Ok(Self {
tenant: Tenant::Cluster,
@@ -284,10 +285,7 @@ pub fn scope_for_agent(agent_id: &str) -> Scope {
}
/// The scope for module `module_id` under `profile` (`None` for local).
-pub fn scope_for_module(
- profile: Option<&str>,
- module_id: &str,
-) -> Result<Scope, StorageError> {
+pub fn scope_for_module(profile: Option<&str>, module_id: &str) -> Result<Scope, StorageError> {
tenant_scope(profile).module(module_id)
}
@@ -326,7 +324,9 @@ fn encode_segment(id: &str) -> Result<String, StorageError> {
bit_count += 8;
while bit_count >= 5 {
bit_count -= 5;
- encoded.push(char::from(BASE32_LOWER[((bits >> bit_count) & 0x1f) as usize]));
+ encoded.push(char::from(
+ BASE32_LOWER[((bits >> bit_count) & 0x1f) as usize],
+ ));
}
}
encoded.truncate(MAX_HASH_SEGMENT_LEN);
diff --git a/crates/openhuman-core/src/storage/scope_tests.rs b/crates/openhuman-core/src/storage/scope_tests.rs
index 33d9e8a3c1..9dc9f659f9 100644
--- a/crates/openhuman-core/src/storage/scope_tests.rs
+++ b/crates/openhuman-core/src/storage/scope_tests.rs
@@ -31,3 +31,27 @@ fn odd_ids_hash_inside_their_tenant() {
assert!(agent.as_str().starts_with(scope.as_str()));
assert_eq!(agent.tenant_scope(), Some(scope));
}
+
+#[test]
+fn host_parser_rejects_opaque_legacy_values_without_changing_the_value() {
+ let legacy = Scope::new("profile:alice").unwrap();
+ assert_eq!(legacy.as_str(), "profile:alice");
+ assert!(legacy.parsed().is_err());
+ assert!(Scope::parse("profile:alice").is_err());
+
+ let current = Scope::parse("u:alice/agent:writer").unwrap();
+ assert_eq!(current.to_string(), "u:alice/agent:writer");
+ assert_eq!(current.as_str(), "u:alice/agent:writer");
+}
+
+#[test]
+fn hashed_segments_use_the_stable_lowercase_base32_alphabet() {
+ let first = scope_for_profile("alice with spaces");
+ let second = scope_for_profile("alice with spaces");
+ assert_eq!(first, second);
+ let segment = first.as_str().strip_prefix("u:~").unwrap();
+ assert_eq!(segment.len(), 26);
+ assert!(segment
+ .bytes()
+ .all(|byte| byte.is_ascii_lowercase() || (b'2'..=b'7').contains(&byte)));
+}