Skip to content

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

refactor(storage): split storage module into submodules

Steven Enamakela187a0bf47bd
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index cfcac17dec..e78021589b 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -228,10 +228,10 @@ pub fn scope_for_agent(agent_id: &str) -> Scope {
}
/// The storage scope profile `profile_id`'s records live under:
-/// `profile:<id>`, or `sha256:<hex of profile:<id>>` when that is not a valid
-/// scope (too long, whitespace, control characters). Injective: a literal
-/// scope always starts with `profile:`, a hashed one never does, and two
-/// distinct profiles never share either form.
+/// `profile:<id>`, or `profile-sha256:<hex of the id>` when that is not a
+/// valid scope (too long, whitespace, control characters). Injective: a
+/// literal scope always starts with `profile:`, a hashed one never does, and
+/// two distinct profiles never share either form.
pub fn scope_for_profile(profile_id: &str) -> Scope {
const PREFIX: &str = "profile:";
let literal = format!("{PREFIX}{profile_id}");
@@ -239,8 +239,8 @@ pub fn scope_for_profile(profile_id: &str) -> Scope {
return scope;
}
use sha2::Digest;
- let digest = sha2::Sha256::digest(literal.as_bytes());
- Scope::new(format!("sha256:{}", hex::encode(digest)))
+ let digest = sha2::Sha256::digest(profile_id.as_bytes());
+ Scope::new(format!("profile-sha256:{}", hex::encode(digest)))
.unwrap_or_else(|_| unreachable!("a sha256 hex scope is always valid"))
}
diff --git a/crates/openhuman-core/src/storage/mod_tests.rs b/crates/openhuman-core/src/storage/mod_tests.rs
index f6c173b6ef..818ca0d8eb 100644
--- a/crates/openhuman-core/src/storage/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/mod_tests.rs
@@ -66,20 +66,43 @@ fn agent_scopes_match_the_session_store() {
#[test]
fn the_scope_follows_the_acting_agent_and_fails_closed_in_saas() {
assert_eq!(
- scope_from(Some("agent-7"), false).unwrap().as_str(),
+ scope_from(None, Some("agent-7"), false).unwrap().as_str(),
"agent-7"
);
- assert_eq!(
- scope_from(Some("agent-7"), true).unwrap().as_str(),
- "agent-7"
+ assert!(
+ scope_from(None, Some("agent-7"), true).is_err(),
+ "SaaS hands out profile scopes only"
);
- assert!(scope_from(None, false).unwrap().is_local());
+ assert!(scope_from(None, None, false).unwrap().is_local());
assert!(
- scope_from(None, true).is_err(),
+ scope_from(None, None, true).is_err(),
"SaaS never falls back to a shared scope"
);
}
+#[test]
+fn a_profile_scopes_storage_over_its_agents() {
+ for saas in [false, true] {
+ assert_eq!(
+ scope_from(Some("u-alice"), Some("u-alice"), saas)
+ .unwrap()
+ .as_str(),
+ "profile:u-alice"
+ );
+ assert_eq!(
+ scope_from(Some("u-alice"), None, saas).unwrap().as_str(),
+ "profile:u-alice"
+ );
+ }
+ assert_ne!(scope_for_profile("a"), scope_for_profile("b"));
+ let odd = scope_for_profile("has a space");
+ assert!(odd.as_str().starts_with("profile-sha256:"), "{odd}");
+ assert_ne!(odd, scope_for_profile("has a space"));
+ assert!(scope_for_profile(&"x".repeat(400))
+ .as_str()
+ .starts_with("profile-sha256:"));
+}
+
#[test]
fn block_on_runs_a_future_from_sync_code() {
let answer = block_on(async { Ok::<_, StorageError>(42) }).unwrap();