Skip to content

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

feat(modules): add module search path resolution

Steven Enamakel67ffdc3a4b12
diff --git a/crates/openhuman-core/src/modules/boot.rs b/crates/openhuman-core/src/modules/boot.rs
index c3c655f742..aedd6c07ed 100644
--- a/crates/openhuman-core/src/modules/boot.rs
+++ b/crates/openhuman-core/src/modules/boot.rs
@@ -40,7 +40,7 @@ pub async fn load_declared_modules(config: &Config) {
// Search paths first: an artifact an operator has placed deliberately should
// win over a download, and `ensure_loaded` below then finds it already
// serving rather than fetching a second copy.
- for outcome in runtime.host().load_search_paths() {
+ for outcome in super::search_path::load_enabled(runtime.host(), &ops::disabled_lib_stems(config)) {
match outcome {
Ok(info) => log::info!(
"[modules] loaded '{}' {} from the module search path",
@@ -57,6 +57,13 @@ pub async fn load_declared_modules(config: &Config) {
if record.load != LoadPolicy::Eager {
continue;
}
+ if let Some(component) = ops::disabled_component(config, record.id) {
+ log::info!(
+ "[modules] eager module '{}' skipped: component '{component}' is disabled",
+ record.id
+ );
+ continue;
+ }
log::info!("[modules] eager module '{}' resolving at boot", record.id);
if let Err(reason) = ops::ensure_loaded(config, record.id).await {
log::warn!(
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index 404c1a1079..a5a8d2c989 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -80,6 +80,7 @@ pub mod linked;
pub mod ops;
pub mod registry;
#[cfg(feature = "modules")]
+mod search_path;
pub mod schemas;
#[cfg(feature = "modules")]
pub mod search;
diff --git a/crates/openhuman-core/src/modules/ops.rs b/crates/openhuman-core/src/modules/ops.rs
index 77c2898cd6..cfa7efb9cb 100644
--- a/crates/openhuman-core/src/modules/ops.rs
+++ b/crates/openhuman-core/src/modules/ops.rs
@@ -106,6 +106,9 @@ pub enum LoadError {
/// The module is still being downloaded, verified, or initialised, and the
/// caller's bound passed first. Asking again later can succeed.
StillLoading,
+ /// The module's `[components]` switch is off. Nothing was downloaded,
+ /// mapped or registered. Carries the component name.
+ Disabled(&'static str),
}
impl LoadError {
@@ -115,6 +118,9 @@ impl LoadError {
match self {
Self::Failed(message) => message,
Self::StillLoading => "the module is still loading; try again shortly".to_string(),
+ Self::Disabled(component) => {
+ format!("COMPONENT_DISABLED:{component}")
+ }
}
}
}
@@ -160,6 +166,10 @@ pub async fn ensure_loaded_within(
}
let record =
registry::find(id).ok_or_else(|| LoadError::Failed(format!("unknown module '{id}'")))?;
+ if let Some(component) = disabled_component(config, id) {
+ log::debug!("[modules] '{id}' refused: component '{component}' is disabled");
+ return Err(LoadError::Disabled(component));
+ }
let receiver = match resolution::global().claim(id) {
Claim::Done(Resolution::Ready) => return Ok(()),
@@ -186,6 +196,28 @@ pub async fn ensure_loaded_within(
}
}
+/// The disabled `[components]` switch that owns module `id`, if any.
+#[must_use]
+pub fn disabled_component(config: &Config, id: &str) -> Option<&'static str> {
+ let component = registry::component_for(id)?;
+ let set = crate::core::runtime::ComponentSet::from_config(&config.components);
+ (!set.enabled(component)).then_some(component)
+}
+
+/// Library file stems of every module whose component is off, for skipping
+/// their search-path artifacts. Both `name` and `libname` spellings.
+#[must_use]
+pub fn disabled_lib_stems(config: &Config) -> Vec<String> {
+ registry::ALL
+ .iter()
+ .filter(|record| disabled_component(config, record.id).is_some())
+ .flat_map(|record| {
+ let stem = record.source.lib_name.to_string();
+ [format!("lib{stem}"), stem]
+ })
+ .collect()
+}
+
/// The state of `id` as the resolution table reports it, without touching it.
#[must_use]
pub fn state_of(id: &str) -> ModuleState {
@@ -324,10 +356,9 @@ async fn resolve(config: &Config, record: &'static ModuleRecord) -> Result<(), S
// nothing, and tinybus reports each refusal with a sanitised reason — so the
// errors are dropped here and only a match on the bus name counts.
let bus_name = record.bus_name;
+ let skip = disabled_lib_stems(config);
let found_on_search_path = tokio::task::spawn_blocking(move || {
- runtime
- .host()
- .load_search_paths()
+ super::search_path::load_enabled(runtime.host(), &skip)
.into_iter()
.flatten()
.any(|info| info.manifest.bus_name.as_str() == bus_name)
@@ -553,6 +584,14 @@ fn status_of(config: &Config, record: &ModuleRecord) -> ModuleStatus {
// may remain loaded in this process after a prior request, but callers
// whose configuration disables modules must still be told it is unusable.
let (state, detail) = match () {
+ _ if disabled_component(config, record.id).is_some() => (
+ ModuleState::Disabled,
+ Some(format!(
+ "COMPONENT_DISABLED:{} (set [components] {} = true to enable)",
+ disabled_component(config, record.id).unwrap_or_default(),
+ disabled_component(config, record.id).unwrap_or_default()
+ )),
+ ),
_ if !config.modules.enabled => (
ModuleState::Unsupported,
Some("modules are disabled in configuration".to_string()),
@@ -577,7 +616,7 @@ fn status_of(config: &Config, record: &ModuleRecord) -> ModuleStatus {
},
};
let plan = source::plan(config, record.id);
- let source = (state != ModuleState::Unsupported).then_some(plan.served);
+ let source = (!matches!(state, ModuleState::Unsupported | ModuleState::Disabled)).then_some(plan.served);
let detail = detail.or_else(|| plan.fallback.map(str::to_string));
ModuleStatus {
id: record.id.to_string(),
diff --git a/crates/openhuman-core/src/modules/registry.rs b/crates/openhuman-core/src/modules/registry.rs
index af2ab2b848..143ea3951b 100644
--- a/crates/openhuman-core/src/modules/registry.rs
+++ b/crates/openhuman-core/src/modules/registry.rs
@@ -90,3 +90,30 @@ pub const ALL: &[ModuleRecord] = &[
pub fn find(id: &str) -> Option<&'static ModuleRecord> {
ALL.iter().find(|record| record.id == id)
}
+
+/// Which `[components]` switch owns each module id.
+///
+/// Every record in [`ALL`] has an entry (`registry_tests` enforces it), so a
+/// module is never exempt from a disabled component by omission.
+pub const COMPONENT_OF: &[(&str, &str)] = &[
+ ("tinycomputer", "computer"),
+ ("tinysearch", "search"),
+ ("tinydocs", "documents"),
+ ("tinywallet", "wallet"),
+ ("tinyjuice", "juice"),
+ ("tinyvoice", "voice"),
+ ("tinymcp", "mcp"),
+ ("tinyconnectors", "connectors"),
+ ("tinybox", "sandbox"),
+ ("tinychannels", "channels"),
+ ("tinyhosts", "hosting"),
+];
+
+/// The `[components]` name that owns module `id`, if the table knows it.
+#[must_use]
+pub fn component_for(id: &str) -> Option<&'static str> {
+ COMPONENT_OF
+ .iter()
+ .find(|(module, _)| *module == id)
+ .map(|(_, component)| *component)
+}
diff --git a/crates/openhuman-core/src/modules/search_path.rs b/crates/openhuman-core/src/modules/search_path.rs
new file mode 100644
index 0000000000..37c4b44663
--- /dev/null
+++ b/crates/openhuman-core/src/modules/search_path.rs
@@ -0,0 +1,58 @@
+//! Search-path loading that honours disabled `[components]`.
+//!
+//! tinybus's `load_search_paths` admits every artifact in every directory, so a
+//! disabled component's library would be mapped (and never unloaded) just by
+//! sitting on the path. This walks the same directories but leaves out files
+//! named after a disabled module's library, without opening them.
+
+use std::path::Path;
+
+use tinybus::module::{ModuleHost, ModuleInfo};
+
+/// Whether `path` is a library file whose stem is in `skip`.
+fn is_skipped(path: &Path, skip: &[String]) -> bool {
+ path.file_stem()
+ .and_then(|stem| stem.to_str())
+ .is_some_and(|stem| skip.iter().any(|s| s == stem))
+}
+
+/// [`ModuleHost::load_search_paths`], minus artifacts named in `skip`.
+///
+/// A directory with nothing to skip goes through `load_dir` unchanged, which
+/// keeps tinybus's dependency ordering. Only a directory holding a skipped
+/// artifact is loaded file by file.
+pub fn load_enabled(host: &ModuleHost, skip: &[String]) -> Vec<Result<ModuleInfo, tinybus::Error>> {
+ if skip.is_empty() {
+ return host.load_search_paths();
+ }
+ let mut outcomes = Vec::new();
+ for dir in ModuleHost::search_paths().into_iter().filter(|p| p.is_dir()) {
+ let mut files: Vec<_> = match std::fs::read_dir(&dir) {
+ Ok(entries) => entries.filter_map(|e| e.ok().map(|e| e.path())).collect(),
+ Err(_) => Vec::new(),
+ };
+ files.sort();
+ let has_skipped = files.iter().any(|f| is_skipped(f, skip));
+ if !has_skipped {
+ match host.load_dir(&dir) {
+ Ok(results) => outcomes.extend(results),
+ Err(error) => outcomes.push(Err(error)),
+ }
+ continue;
+ }
+ for file in files {
+ if is_skipped(&file, skip) {
+ log::debug!("[modules] search-path artifact skipped: component disabled");
+ continue;
+ }
+ if file.extension().is_some_and(|e| matches!(e.to_str(), Some("so" | "dylib" | "dll"))) {
+ outcomes.push(host.load_file(&file));
+ }
+ }
+ }
+ outcomes
+}
+
+#[cfg(test)]
+#[path = "search_path_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/modules/search_path_tests.rs b/crates/openhuman-core/src/modules/search_path_tests.rs
new file mode 100644
index 0000000000..e1f98f95bb
--- /dev/null
+++ b/crates/openhuman-core/src/modules/search_path_tests.rs
@@ -0,0 +1,10 @@
+use super::*;
+use std::path::PathBuf;
+
+#[test]
+fn skips_only_files_named_after_a_disabled_library() {
+ let skip = vec!["libtinysearch".to_string(), "tinysearch".to_string()];
+ assert!(is_skipped(&PathBuf::from("/x/libtinysearch.so"), &skip));
+ assert!(is_skipped(&PathBuf::from("/x/tinysearch.dll"), &skip));
+ assert!(!is_skipped(&PathBuf::from("/x/libtinydocs.so"), &skip));
+}
diff --git a/crates/openhuman-core/src/modules/types.rs b/crates/openhuman-core/src/modules/types.rs
index 671a7d72ee..1872082b27 100644
--- a/crates/openhuman-core/src/modules/types.rs
+++ b/crates/openhuman-core/src/modules/types.rs
@@ -140,6 +140,8 @@ pub enum ModuleState {
Failed,
/// No artifact is published for this host.
Unsupported,
+ /// Switched off by `[components]`: never downloaded, loaded or registered.
+ Disabled,
}
/// A module's status as reported over RPC.