Skip to content

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

refactor(profiles): extract registry helpers into dedicated functions

Steven Enamakela4f1d811aa4b
diff --git a/crates/openhuman-core/src/profiles/registry.rs b/crates/openhuman-core/src/profiles/registry.rs
new file mode 100644
index 0000000000..470b8ca96e
--- /dev/null
+++ b/crates/openhuman-core/src/profiles/registry.rs
@@ -0,0 +1,222 @@
+//! [`ProfileRegistry`]: which profiles are provisioned.
+//!
+//! With a storage backend installed, every node of a deployment reads one
+//! registry: collection [`REGISTRY_COLLECTION`] of the shared
+//! [`CLUSTER_SCOPE`](crate::storage::lease::CLUSTER_SCOPE), one
+//! [`ProfileMeta`] document per profile. Without one it is the
+//! `profile.toml` file beside each profile's state
+//! (`<root>/users/<id>/profile.toml`), as before.
+//!
+//! The registry records existence and creation time only. A profile's config
+//! is derived (`layout::profile_config`), never stored, and its state stays
+//! under `<root>/users/<id>/` either way.
+
+use std::path::PathBuf;
+use std::sync::Arc;
+
+use tinystoragedrivers::{CollectionSpec, DocumentStoreExt, ErrorKind, Precondition, Query, Scope};
+
+use super::layout::{self, ProfileLayout};
+use super::types::{ProfileId, ProfileMeta};
+use crate::storage::lease::CLUSTER_SCOPE;
+use crate::storage::{DocumentStore, StorageBackend};
+
+/// The collection the profile records live in, under the cluster scope.
+pub const REGISTRY_COLLECTION: &str = "profiles";
+
+/// Where provisioned profiles are recorded. See the module docs.
+pub enum ProfileRegistry {
+ /// One document per profile in the shared backend.
+ Documents {
+ docs: Arc<dyn DocumentStore>,
+ declared: tokio::sync::OnceCell<()>,
+ },
+ /// `profile.toml` under each profile's directory below `root`.
+ Files { root: PathBuf },
+}
+
+impl std::fmt::Debug for ProfileRegistry {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Self::Documents { .. } => f.write_str("ProfileRegistry::Documents"),
+ Self::Files { root } => f.debug_struct("ProfileRegistry::Files").field("root", root).finish(),
+ }
+ }
+}
+
+impl ProfileRegistry {
+ /// `profile.toml` files under the SaaS `root`.
+ pub fn files(root: impl Into<PathBuf>) -> Self {
+ Self::Files { root: root.into() }
+ }
+
+ /// Documents in `backend`'s cluster scope.
+ ///
+ /// # Errors
+ ///
+ /// When the backend refuses the scope.
+ pub fn cluster(backend: &dyn StorageBackend) -> Result<Self, String> {
+ let scope = Scope::new(CLUSTER_SCOPE).map_err(|e| e.to_string())?;
+ let scoped = backend.for_scope(&scope).map_err(|e| e.to_string())?;
+ Ok(Self::Documents {
+ docs: Arc::clone(scoped.documents()),
+ declared: tokio::sync::OnceCell::new(),
+ })
+ }
+
+ /// Whether this registry is the shared backend.
+ pub fn is_shared(&self) -> bool {
+ matches!(self, Self::Documents { .. })
+ }
+
+ async fn docs(&self) -> Result<Option<&Arc<dyn DocumentStore>>, String> {
+ let Self::Documents { docs, declared } = self else {
+ return Ok(None);
+ };
+ declared
+ .get_or_try_init(|| async {
+ docs.ensure_collection(&CollectionSpec::new(REGISTRY_COLLECTION))
+ .await
+ })
+ .await
+ .map_err(|e| format!("profile registry: {e}"))?;
+ Ok(Some(docs))
+ }
+
+ /// The record of profile `id`, or `None` when it is not provisioned.
+ ///
+ /// # Errors
+ ///
+ /// When the record cannot be read or parsed.
+ pub async fn get(&self, id: &ProfileId) -> Result<Option<ProfileMeta>, String> {
+ if let Some(docs) = self.docs().await? {
+ let Some(stored) = docs
+ .get(REGISTRY_COLLECTION, id.as_str())
+ .await
+ .map_err(|e| format!("reading profile {id}: {e}"))?
+ else {
+ return Ok(None);
+ };
+ return serde_json::from_value(stored.doc)
+ .map(Some)
+ .map_err(|e| format!("parsing profile {id}: {e}"));
+ }
+ let Self::Files { root } = self else {
+ unreachable!("a registry is documents or files");
+ };
+ read_meta(&ProfileLayout::new(root, id))
+ }
+
+ /// Records `meta` unless its profile is already recorded. Returns whether
+ /// it was new.
+ ///
+ /// # Errors
+ ///
+ /// When the record cannot be written.
+ pub async fn create(&self, meta: &ProfileMeta) -> Result<bool, String> {
+ let id = &meta.profile_id;
+ if let Some(docs) = self.docs().await? {
+ let doc = serde_json::to_value(meta).map_err(|e| format!("encoding profile {id}: {e}"))?;
+ return match docs
+ .put(REGISTRY_COLLECTION, id.as_str(), doc, Precondition::Absent)
+ .await
+ {
+ Ok(_) => Ok(true),
+ Err(e) if e.kind() == ErrorKind::Conflict => Ok(false),
+ Err(e) => Err(format!("recording profile {id}: {e}")),
+ };
+ }
+ let Self::Files { root } = self else {
+ unreachable!("a registry is documents or files");
+ };
+ let layout = ProfileLayout::new(root, id);
+ if layout.meta_path.exists() {
+ return Ok(false);
+ }
+ let raw = toml::to_string(meta).map_err(|e| format!("encoding profile meta: {e}"))?;
+ std::fs::write(&layout.meta_path, raw)
+ .map_err(|e| format!("writing {}: {e}", layout.meta_path.display()))?;
+ Ok(true)
+ }
+
+ /// Forgets profile `id`. Returns whether it was recorded. The files
+ /// registry forgets a profile when its directory is archived, so this
+ /// only reports there.
+ ///
+ /// # Errors
+ ///
+ /// When the record cannot be removed.
+ pub async fn remove(&self, id: &ProfileId) -> Result<bool, String> {
+ if let Some(docs) = self.docs().await? {
+ return docs
+ .delete(REGISTRY_COLLECTION, id.as_str(), Precondition::None)
+ .await
+ .map_err(|e| format!("forgetting profile {id}: {e}"));
+ }
+ Ok(self.get(id).await?.is_some())
+ }
+
+ /// Every recorded profile, sorted by id. A record that cannot be read is
+ /// logged and skipped, so one bad profile never hides the rest.
+ ///
+ /// # Errors
+ ///
+ /// When the registry itself cannot be read.
+ pub async fn list(&self) -> Result<Vec<ProfileMeta>, String> {
+ let mut found = Vec::new();
+ if let Some(docs) = self.docs().await? {
+ let stored = docs
+ .query_all(REGISTRY_COLLECTION, &Query::all())
+ .await
+ .map_err(|e| format!("listing profiles: {e}"))?;
+ for doc in stored {
+ match serde_json::from_value::<ProfileMeta>(doc.doc) {
+ Ok(meta) => found.push(meta),
+ Err(error) => {
+ log::warn!("[profiles][registry] skipping record {}: {error}", doc.id);
+ }
+ }
+ }
+ } else {
+ let Self::Files { root } = self else {
+ unreachable!("a registry is documents or files");
+ };
+ let dir = layout::users_dir(root);
+ let entries = match std::fs::read_dir(&dir) {
+ Ok(entries) => entries,
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
+ Err(e) => return Err(format!("reading {}: {e}", dir.display())),
+ };
+ for entry in entries.flatten() {
+ let Some(name) = entry.file_name().to_str().map(str::to_owned) else {
+ continue;
+ };
+ let Ok(id) = ProfileId::parse(&name) else {
+ continue;
+ };
+ match read_meta(&ProfileLayout::new(root, &id)) {
+ Ok(Some(meta)) => found.push(meta),
+ Ok(None) => {}
+ Err(error) => log::warn!("[profiles][registry] skipping profile={id}: {error}"),
+ }
+ }
+ }
+ found.sort_by(|a, b| a.profile_id.cmp(&b.profile_id));
+ Ok(found)
+ }
+}
+
+fn read_meta(layout: &ProfileLayout) -> Result<Option<ProfileMeta>, String> {
+ let raw = match std::fs::read_to_string(&layout.meta_path) {
+ Ok(raw) => raw,
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
+ Err(e) => return Err(format!("reading {}: {e}", layout.meta_path.display())),
+ };
+ toml::from_str(&raw)
+ .map(Some)
+ .map_err(|e| format!("parsing {}: {e}", layout.meta_path.display()))
+}
+
+#[cfg(test)]
+#[path = "registry_tests.rs"]
+mod tests;