Skip to content

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

feat(skills): hold a process SkillRegistry configured from the environment

oxoxDev31bc864ec635
diff --git a/crates/openhuman-core/src/skills/catalog/mod.rs b/crates/openhuman-core/src/skills/catalog/mod.rs
index 813312f361..a4df0ef1b8 100644
--- a/crates/openhuman-core/src/skills/catalog/mod.rs
+++ b/crates/openhuman-core/src/skills/catalog/mod.rs
@@ -18,6 +18,8 @@ mod download;
#[cfg(feature = "skills")]
pub mod ops;
#[cfg(feature = "skills")]
+mod registry;
+#[cfg(feature = "skills")]
pub mod schemas;
#[cfg(feature = "skills")]
pub mod store;
@@ -28,6 +30,8 @@ mod transport;
#[cfg(feature = "skills")]
pub mod types;
+#[cfg(feature = "skills")]
+pub use registry::{skill_registry, HERMES_REGISTRY_ID};
#[cfg(feature = "skills")]
pub use transport::ReqwestTransport;
diff --git a/crates/openhuman-core/src/skills/catalog/registry.rs b/crates/openhuman-core/src/skills/catalog/registry.rs
new file mode 100644
index 0000000000..100d9e61ad
--- /dev/null
+++ b/crates/openhuman-core/src/skills/catalog/registry.rs
@@ -0,0 +1,168 @@
+//! The process-wide [`SkillRegistry`], configured from the environment.
+//!
+//! | Variable | Effect |
+//! | --- | --- |
+//! | `OPENHUMAN_SKILL_REGISTRY_CATALOG_URL` | Read the Hermes index from this URL instead of the public one |
+//! | `OPENHUMAN_SKILL_REGISTRY_DOWNLOAD_BASE_URL` | Serve every `SKILL.md` from `<base>/<name>/SKILL.md` |
+//! | `OPENHUMAN_SKILL_INSTALL_ALLOW_LOCAL_HTTP=1` | Allow plain `http` to loopback (fixtures, local mirrors) |
+//! | `OPENHUMAN_SKILL_REGISTRY_CACHE_DIR` | Catalog store directory; default `~/.openhuman/skill-registry` |
+//!
+//! The handle is rebuilt whenever that configuration changes, so a process
+//! that re-points the environment (tests, a relocated home) gets a registry
+//! for its current settings.
+
+use std::path::{Path, PathBuf};
+use std::sync::{Arc, PoisonError, RwLock};
+
+use tinyskills::{FetchPolicy, FileCatalogStore, HermesIndexSource, RegistryLimits, SkillRegistry};
+
+use super::transport::ReqwestTransport;
+
+const CATALOG_URL_ENV: &str = "OPENHUMAN_SKILL_REGISTRY_CATALOG_URL";
+pub(crate) const DOWNLOAD_BASE_URL_ENV: &str = "OPENHUMAN_SKILL_REGISTRY_DOWNLOAD_BASE_URL";
+const CACHE_DIR_ENV: &str = "OPENHUMAN_SKILL_REGISTRY_CACHE_DIR";
+const DEFAULT_CACHE_DIR: &str = "skill-registry";
+const LEGACY_CACHE_FILE: &str = "cache.json";
+
+/// The registry id of the Hermes index source.
+pub const HERMES_REGISTRY_ID: &str = "hermes";
+
+/// Everything the registry is built from.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(crate) struct RegistryConfig {
+ pub(crate) catalog_url: Option<String>,
+ pub(crate) download_base: Option<String>,
+ pub(crate) allow_loopback_http: bool,
+ pub(crate) cache_dir: Option<PathBuf>,
+}
+
+fn non_empty(value: Option<String>) -> Option<String> {
+ value
+ .map(|value| value.trim().to_owned())
+ .filter(|value| !value.is_empty())
+}
+
+impl RegistryConfig {
+ pub(crate) fn from_env() -> Self {
+ Self::from_lookup(|name| std::env::var(name).ok(), dirs::home_dir())
+ }
+
+ pub(crate) fn from_lookup(
+ lookup: impl Fn(&str) -> Option<String>,
+ home: Option<PathBuf>,
+ ) -> Self {
+ let cache_dir = non_empty(lookup(CACHE_DIR_ENV))
+ .map(PathBuf::from)
+ .or_else(|| home.map(|home| home.join(".openhuman").join(DEFAULT_CACHE_DIR)));
+ Self {
+ catalog_url: non_empty(lookup(CATALOG_URL_ENV)),
+ download_base: non_empty(lookup(DOWNLOAD_BASE_URL_ENV)),
+ allow_loopback_http: crate::skills::ops_install::allow_local_http(lookup(
+ crate::skills::ops_install::ALLOW_LOCAL_HTTP_ENV,
+ )),
+ cache_dir,
+ }
+ }
+
+ pub(crate) fn source(&self) -> HermesIndexSource {
+ let source = match &self.catalog_url {
+ Some(url) => HermesIndexSource::new(HERMES_REGISTRY_ID, url.clone())
+ .with_label("Hermes Skills Hub"),
+ None => HermesIndexSource::hermes(),
+ };
+ match &self.download_base {
+ Some(base) => source.with_download_base(base.clone()),
+ None => source,
+ }
+ }
+
+ pub(crate) fn policy(&self) -> FetchPolicy {
+ let mut policy = FetchPolicy::default();
+ policy.allow_loopback_http = self.allow_loopback_http;
+ policy.user_agent = format!("openhuman-core/{}", env!("CARGO_PKG_VERSION"));
+ policy
+ }
+
+ pub(crate) fn limits() -> RegistryLimits {
+ let mut limits = RegistryLimits::default();
+ limits.max_page_size = limits.max_entries;
+ limits
+ }
+
+ fn build(&self) -> Arc<SkillRegistry> {
+ let builder = SkillRegistry::builder(ReqwestTransport::new())
+ .source(self.source())
+ .policy(self.policy())
+ .limits(Self::limits());
+ match &self.cache_dir {
+ Some(dir) => {
+ remove_legacy_cache(dir);
+ builder.store(FileCatalogStore::new(dir.clone())).build()
+ }
+ None => {
+ tracing::warn!("[skill_registry] no home directory; catalog cached in memory only");
+ builder.build()
+ }
+ }
+ }
+}
+
+fn remove_legacy_cache(dir: &Path) {
+ let legacy = dir.join(LEGACY_CACHE_FILE);
+ match std::fs::remove_file(&legacy) {
+ Ok(()) => tracing::info!(
+ path = %legacy.display(),
+ "[skill_registry] removed legacy catalog cache"
+ ),
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
+ Err(error) => tracing::warn!(
+ path = %legacy.display(),
+ error = %error,
+ "[skill_registry] could not remove legacy catalog cache"
+ ),
+ }
+}
+
+static HANDLE: RwLock<Option<(RegistryConfig, Arc<SkillRegistry>)>> = RwLock::new(None);
+
+/// The registry for the current environment, built on first use.
+pub fn skill_registry() -> Arc<SkillRegistry> {
+ registry_for(RegistryConfig::from_env())
+}
+
+pub(crate) fn registry_for(config: RegistryConfig) -> Arc<SkillRegistry> {
+ if let Some((held, registry)) = HANDLE
+ .read()
+ .unwrap_or_else(PoisonError::into_inner)
+ .as_ref()
+ {
+ if *held == config {
+ return Arc::clone(registry);
+ }
+ }
+ let mut handle = HANDLE.write().unwrap_or_else(PoisonError::into_inner);
+ if let Some((held, registry)) = handle.as_ref() {
+ if *held == config {
+ return Arc::clone(registry);
+ }
+ }
+ tracing::info!(
+ custom_catalog = config.catalog_url.is_some(),
+ download_base = config.download_base.is_some(),
+ allow_loopback_http = config.allow_loopback_http,
+ persisted = config.cache_dir.is_some(),
+ "[skill_registry] building registry"
+ );
+ let registry = config.build();
+ *handle = Some((config, Arc::clone(&registry)));
+ registry
+}
+
+#[cfg(test)]
+pub(crate) fn reset_for_tests() {
+ *HANDLE.write().unwrap_or_else(PoisonError::into_inner) = None;
+}
+
+#[cfg(test)]
+#[path = "registry_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/skills/catalog/registry_tests.rs b/crates/openhuman-core/src/skills/catalog/registry_tests.rs
new file mode 100644
index 0000000000..ee99b18be5
--- /dev/null
+++ b/crates/openhuman-core/src/skills/catalog/registry_tests.rs
@@ -0,0 +1,105 @@
+use super::*;
+
+use std::collections::HashMap;
+
+fn config_from(vars: &[(&str, &str)], home: Option<&Path>) -> RegistryConfig {
+ let vars: HashMap<String, String> = vars
+ .iter()
+ .map(|(key, value)| ((*key).to_owned(), (*value).to_owned()))
+ .collect();
+ RegistryConfig::from_lookup(|name| vars.get(name).cloned(), home.map(Path::to_path_buf))
+}
+
+#[test]
+fn defaults_read_the_public_index_into_the_home_cache() {
+ let home = tempfile::tempdir().unwrap();
+ let config = config_from(&[], Some(home.path()));
+ assert_eq!(config.catalog_url, None);
+ assert_eq!(config.download_base, None);
+ assert!(!config.allow_loopback_http);
+ assert_eq!(
+ config.cache_dir,
+ Some(home.path().join(".openhuman").join("skill-registry"))
+ );
+ assert_eq!(config.source().url(), HermesIndexSource::hermes().url());
+ assert!(!config.policy().allow_loopback_http);
+ assert!(config.policy().user_agent.starts_with("openhuman-core/"));
+}
+
+#[test]
+fn environment_overrides_map_onto_the_registry() {
+ let config = config_from(
+ &[
+ (CATALOG_URL_ENV, " http://127.0.0.1:9/skills.json "),
+ (DOWNLOAD_BASE_URL_ENV, "http://127.0.0.1:9/skills"),
+ ("OPENHUMAN_SKILL_INSTALL_ALLOW_LOCAL_HTTP", "1"),
+ (CACHE_DIR_ENV, "/tmp/registry-cache"),
+ ],
+ None,
+ );
+ assert_eq!(
+ config.catalog_url.as_deref(),
+ Some("http://127.0.0.1:9/skills.json")
+ );
+ assert_eq!(config.source().url(), "http://127.0.0.1:9/skills.json");
+ assert_eq!(
+ config.download_base.as_deref(),
+ Some("http://127.0.0.1:9/skills")
+ );
+ assert!(config.allow_loopback_http);
+ assert!(config.policy().allow_loopback_http);
+ assert_eq!(config.cache_dir, Some(PathBuf::from("/tmp/registry-cache")));
+}
+
+#[test]
+fn blank_overrides_are_ignored_and_only_one_enables_local_http() {
+ let config = config_from(
+ &[
+ (CATALOG_URL_ENV, " "),
+ (DOWNLOAD_BASE_URL_ENV, ""),
+ ("OPENHUMAN_SKILL_INSTALL_ALLOW_LOCAL_HTTP", "true"),
+ (CACHE_DIR_ENV, " "),
+ ],
+ None,
+ );
+ assert_eq!(config.catalog_url, None);
+ assert_eq!(config.download_base, None);
+ assert!(!config.allow_loopback_http, "only `1` enables local http");
+ assert_eq!(config.cache_dir, None, "no home and no override");
+}
+
+#[test]
+fn limits_allow_a_whole_catalog_in_one_page() {
+ let limits = RegistryConfig::limits();
+ assert_eq!(limits.max_page_size, limits.max_entries);
+}
+
+#[test]
+fn the_handle_is_reused_for_one_config_and_rebuilt_for_another() {
+ let _env = crate::skills::catalog::TEST_ENV_LOCK.blocking_lock();
+ reset_for_tests();
+ let first_dir = tempfile::tempdir().unwrap();
+ let second_dir = tempfile::tempdir().unwrap();
+ let first = config_from(&[(CACHE_DIR_ENV, first_dir.path().to_str().unwrap())], None);
+ let second = config_from(
+ &[(CACHE_DIR_ENV, second_dir.path().to_str().unwrap())],
+ None,
+ );
+
+ let a = registry_for(first.clone());
+ let b = registry_for(first);
+ assert!(Arc::ptr_eq(&a, &b), "same config shares one registry");
+ let c = registry_for(second);
+ assert!(!Arc::ptr_eq(&a, &c), "a new config builds a new registry");
+ reset_for_tests();
+}
+
+#[test]
+fn building_removes_the_legacy_cache_file() {
+ let dir = tempfile::tempdir().unwrap();
+ let legacy = dir.path().join(LEGACY_CACHE_FILE);
+ std::fs::write(&legacy, r#"{"entries":[],"fetched_at_epoch":0}"#).unwrap();
+ let config = config_from(&[(CACHE_DIR_ENV, dir.path().to_str().unwrap())], None);
+ let _registry = config.build();
+ assert!(!legacy.exists(), "the pre-registry cache is dropped");
+}
diff --git a/crates/openhuman-core/src/skills/ops_install.rs b/crates/openhuman-core/src/skills/ops_install.rs
index da1de7596e..35a7382f88 100644
--- a/crates/openhuman-core/src/skills/ops_install.rs
+++ b/crates/openhuman-core/src/skills/ops_install.rs
@@ -14,6 +14,7 @@ pub use fetch::{
RATE_LIMITED_ERROR_PREFIX,
};
pub use uninstall::{uninstall_workflow, UninstallWorkflowOutcome, UninstallWorkflowParams};
+pub(crate) use url_validation::{allow_local_http, ALLOW_LOCAL_HTTP_ENV};
pub use url_validation::{validate_install_url, validate_resolved_host, MAX_INSTALL_URL_LEN};
#[cfg(test)]
diff --git a/crates/openhuman-core/src/skills/ops_install/url_validation.rs b/crates/openhuman-core/src/skills/ops_install/url_validation.rs
index 14659ebd63..03ce756fa8 100644
--- a/crates/openhuman-core/src/skills/ops_install/url_validation.rs
+++ b/crates/openhuman-core/src/skills/ops_install/url_validation.rs
@@ -1,7 +1,7 @@
//! OpenHuman configuration boundary around portable install URL guards.
pub const MAX_INSTALL_URL_LEN: usize = tinyskills::MAX_INSTALL_URL_LEN;
-const ALLOW_LOCAL_HTTP_ENV: &str = "OPENHUMAN_SKILL_INSTALL_ALLOW_LOCAL_HTTP";
+pub(crate) const ALLOW_LOCAL_HTTP_ENV: &str = "OPENHUMAN_SKILL_INSTALL_ALLOW_LOCAL_HTTP";
pub(crate) fn normalize_install_url(raw: &str) -> Result<String, String> {
// ClawHub's file API identifies the requested Markdown file in its query
@@ -36,8 +36,12 @@ pub(crate) fn validate_install_url_with_config(
tinyskills::validate_install_url(raw, allow_local_http).map_err(|error| error.to_string())
}
+pub(crate) fn allow_local_http(raw: Option<String>) -> bool {
+ raw.as_deref() == Some("1")
+}
+
pub(super) fn read_allow_local_http_env() -> bool {
- std::env::var(ALLOW_LOCAL_HTTP_ENV).ok().as_deref() == Some("1")
+ allow_local_http(std::env::var(ALLOW_LOCAL_HTTP_ENV).ok())
}
pub(super) fn is_loopback_http_url(raw: &str) -> bool {