Skip to content

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

refactor(modules): split module loading into ops and source

Steven Enamakel1ca8d7c64dd2
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index 316d90a628..6f242b6cae 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -76,6 +76,7 @@ pub mod host;
#[cfg(feature = "modules")]
pub mod ops;
pub mod registry;
+mod source;
#[cfg(feature = "modules")]
pub mod schemas;
#[cfg(feature = "modules")]
diff --git a/crates/openhuman-core/src/modules/ops.rs b/crates/openhuman-core/src/modules/ops.rs
index 031c4c2b51..73e515685f 100644
--- a/crates/openhuman-core/src/modules/ops.rs
+++ b/crates/openhuman-core/src/modules/ops.rs
@@ -13,7 +13,8 @@
//!
//! # Where an artifact comes from, and where it stays
//!
-//! Resolution order is cheapest-first: already loaded, a developer's override,
+//! Resolution order is cheapest-first: already loaded, the configured source
+//! (`[modules] source`, per-module overrides; see [`super::source`]),
//! the module search path, then the release cache. The cache is a directory per
//! module version under [`install_dir`], filled by the first load — downloaded,
//! verified against the digest in [`registry`], extracted — and re-verified from
@@ -35,8 +36,8 @@ use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::time::Duration;
-use super::types::{ModuleRecord, ModuleState, ModuleStatus};
-use super::{host, registry};
+use super::types::{ModuleRecord, ModuleSource, ModuleState, ModuleStatus};
+use super::{host, registry, source};
use crate::config::Config;
use tinybus::module::platform::host_candidates;
use tinybus::module::resolution::{self, Claim, Resolution, ResolutionState, Waited};
@@ -277,11 +278,31 @@ async fn resolve(config: &Config, record: &'static ModuleRecord) -> Result<(), S
return Ok(());
}
- // An override points at a developer's own build. Checked before the pinned
- // release so a module can be iterated on against a live core.
- if let Some(path) = local_override(config, record.id) {
- let module_config = module_config(config, record.id);
- return blocking(move || load_local(runtime, &path, record.id, module_config)).await;
+ // The configured source decides first: a developer's own build is checked
+ // before the pinned release so a module can be iterated on against a live
+ // core. `release` (the default) falls straight through to the chain below.
+ let plan = source::plan(config, record.id);
+ log::debug!(
+ "[modules] id={} requested={} served={:?}",
+ record.id,
+ plan.requested.as_str(),
+ plan.served
+ );
+ match plan.served {
+ ModuleSource::LocalFile | ModuleSource::Source => {
+ if let Some(path) = plan.artifact {
+ let module_config = module_config(config, record.id);
+ return blocking(move || load_local(runtime, &path, record.id, module_config))
+ .await;
+ }
+ }
+ ModuleSource::Linked => {
+ return Err(mark_terminal(format!(
+ "module '{}' is unavailable: its linked factory could not be started",
+ record.id
+ )));
+ }
+ ModuleSource::Release => {}
}
// The search path tinybus itself honours, including OPENHUMAN_MODULE_PATH.
@@ -452,44 +473,6 @@ fn module_config(config: &Config, id: &str) -> serde_json::Value {
serde_json::json!({})
}
-/// A configured local artifact for `id`, if one is set.
-///
-/// The test fixture uses the same explicit-override path as a developer build,
-/// so TinyMemory is initialized with the host's real module configuration.
-/// Loading it through `OPENHUMAN_MODULE_PATH` would initialize it during boot
-/// before that configuration and its host callbacks are installed.
-fn local_override(config: &Config, id: &str) -> Option<PathBuf> {
- let configured = config
- .modules
- .overrides
- .iter()
- .find_map(|entry| (entry.id == id).then(|| PathBuf::from(entry.path.clone())));
-
- configured
- .or_else(|| {
- (id == super::search::MODULE_ID)
- .then(|| std::env::var_os("TINYSEARCH_TEST_MODULE"))
- .flatten()
- .map(PathBuf::from)
- })
- .or_else(|| {
- // Integration tests compile the core as a dependency, without
- // cfg(test), so fixture routing belongs in this shared loader.
- (id == "tinyjuice")
- .then(|| std::env::var_os("TINYJUICE_TEST_MODULE"))
- .flatten()
- .map(PathBuf::from)
- })
- .or_else(|| {
- // TinyConnectors exposes its contract to the host, but has no
- // host-side module namespace: it is resolved by its registry ID.
- (id == "tinyconnectors")
- .then(|| std::env::var_os("TINYCONNECTORS_TEST_MODULE"))
- .flatten()
- .map(PathBuf::from)
- })
-}
-
/// Where downloaded artifacts are kept.
///
/// The user cache directory, falling back to the workspace when there is none —
@@ -546,12 +529,17 @@ fn status_of(config: &Config, record: &ModuleRecord) -> ModuleStatus {
}
},
};
+ let plan = source::plan(config, record.id);
+ let source = (state == ModuleState::Ready || state == ModuleState::Available)
+ .then_some(plan.served);
+ let detail = detail.or_else(|| plan.fallback.map(str::to_string));
ModuleStatus {
id: record.id.to_string(),
description: record.description.to_string(),
version: record.version.to_string(),
bus_name: record.bus_name.to_string(),
state,
+ source,
detail,
}
}
diff --git a/crates/openhuman-core/src/modules/source.rs b/crates/openhuman-core/src/modules/source.rs
new file mode 100644
index 0000000000..6484fb3aa4
--- /dev/null
+++ b/crates/openhuman-core/src/modules/source.rs
@@ -0,0 +1,129 @@
+//! Which source serves a module: release, linked, source, or a local file.
+//!
+//! The decision is a pure function of configuration plus two facts about the
+//! build ([`linked_factory_available`], [`source_artifact`]), so every mode and
+//! every fallback is testable without loading anything. `release` is the
+//! default everywhere and the only mode CI and shipped builds use; `linked` and
+//! `source` exist for local development and local tests.
+
+use std::path::PathBuf;
+
+use super::types::ModuleSource;
+use crate::config::schema::ModuleSourceMode;
+use crate::config::Config;
+
+/// The outcome of choosing a source for one module.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(super) struct SourcePlan {
+ /// What the configuration asked for.
+ pub requested: ModuleSourceMode,
+ /// What will actually serve the module.
+ pub served: ModuleSource,
+ /// The local artifact to load, for [`ModuleSource::LocalFile`] and
+ /// [`ModuleSource::Source`].
+ pub artifact: Option<PathBuf>,
+ /// Why `served` differs from `requested`. A static string: it ends up in
+ /// `ModuleStatus.detail`, so it never carries a path, URL or credential.
+ pub fallback: Option<&'static str>,
+}
+
+/// Whether this binary registered an in-process factory for `id`.
+///
+/// The factory registry arrives with `openhuman-modules-linked` (E0.2); until
+/// then no binary has one, so a `linked` request always falls back.
+#[must_use]
+pub(super) fn linked_factory_available(_id: &str) -> bool {
+ false
+}
+
+/// The locally built artifact for `id`, if one exists.
+///
+/// Produced by the source-build step (E0.4); until it lands there is none, so a
+/// `source` request without an explicit path falls back to `release`.
+#[must_use]
+pub(super) fn source_artifact(_id: &str) -> Option<PathBuf> {
+ None
+}
+
+/// Choose the source for `id` under `config`.
+pub(super) fn plan(config: &Config, id: &str) -> SourcePlan {
+ plan_with(
+ config,
+ id,
+ linked_factory_available(id),
+ source_artifact(id),
+ )
+}
+
+/// [`plan`] with the build facts supplied, for tests.
+pub(super) fn plan_with(
+ config: &Config,
+ id: &str,
+ linked_available: bool,
+ built_artifact: Option<PathBuf>,
+) -> SourcePlan {
+ let mut mode = config.modules.source;
+ if let Some(entry) = config.modules.overrides.iter().find(|entry| entry.id == id) {
+ match entry.validate() {
+ Err(reason) => {
+ log::warn!("[modules] ignoring override for '{id}': {reason}");
+ }
+ Ok(()) => {
+ if let Some(path) = &entry.path {
+ return SourcePlan {
+ requested: ModuleSourceMode::Source,
+ served: ModuleSource::LocalFile,
+ artifact: Some(PathBuf::from(path)),
+ fallback: None,
+ };
+ }
+ if let Some(source) = entry.source {
+ mode = source;
+ }
+ }
+ }
+ }
+
+ let released = |fallback: &'static str| {
+ log::warn!(
+ "[modules] id={id} requested={} served=release: {fallback}",
+ mode.as_str()
+ );
+ SourcePlan {
+ requested: mode,
+ served: ModuleSource::Release,
+ artifact: None,
+ fallback: Some(fallback),
+ }
+ };
+ match mode {
+ ModuleSourceMode::Release => SourcePlan {
+ requested: mode,
+ served: ModuleSource::Release,
+ artifact: None,
+ fallback: None,
+ },
+ ModuleSourceMode::Linked if linked_available => SourcePlan {
+ requested: mode,
+ served: ModuleSource::Linked,
+ artifact: None,
+ fallback: None,
+ },
+ ModuleSourceMode::Linked => {
+ released("no linked factory is registered in this binary; serving the release")
+ }
+ ModuleSourceMode::Source => match built_artifact {
+ Some(path) => SourcePlan {
+ requested: mode,
+ served: ModuleSource::Source,
+ artifact: Some(path),
+ fallback: None,
+ },
+ None => released("no source-built artifact is available; serving the release"),
+ },
+ }
+}
+
+#[cfg(test)]
+#[path = "source_tests.rs"]
+mod tests;