Skip to content

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

feat(modules): add linked module registry

Steven Enamakela6ab90572494
diff --git a/crates/openhuman-core/src/modules/linked.rs b/crates/openhuman-core/src/modules/linked.rs
new file mode 100644
index 0000000000..f15e123e01
--- /dev/null
+++ b/crates/openhuman-core/src/modules/linked.rs
@@ -0,0 +1,72 @@
+//! The registry of modules linked into the host executable.
+//!
+//! A `linked` source (`[modules] source = "linked"`, see [`super::source`])
+//! serves a module from code compiled into the running binary instead of a
+//! downloaded `cdylib`. The core cannot link module implementation crates, so
+//! the code that does (`openhuman-modules-linked`, reachable only from the CLI
+//! in local-development builds) hands the core a *factory* per module id with
+//! [`register_linked`]. The factory returns tinybus' [`LinkedModule`]; the
+//! resolver attaches it through `ModuleHost::attach_linked_with_config` with
+//! the same private configuration a released module gets, so reinitialization
+//! and config behave identically.
+//!
+//! The registry is empty in every binary that never calls [`register_linked`],
+//! which is every shipped build; a `linked` request then falls back to the
+//! release with a warning (see [`super::source::plan`]).
+//!
+//! # Process-global on purpose
+//!
+//! tinybus never unloads a module, and the module host itself is a process
+//! singleton ([`super::host`]), so a process serves a given id from exactly one
+//! source. The registry is likewise process-wide: it records what this binary
+//! *can* link, not what any one configuration asked for.
+
+use std::collections::BTreeMap;
+use std::sync::{Mutex, MutexGuard};
+
+use tinybus::module::LinkedModule;
+
+/// Builds the linked module for one id. A plain `fn` so a registration carries
+/// no state: the module crate's generated `linked_module` fits directly.
+pub type LinkedFactory = fn() -> tinybus::Result<LinkedModule>;
+
+static FACTORIES: Mutex<BTreeMap<&'static str, LinkedFactory>> = Mutex::new(BTreeMap::new());
+
+fn factories() -> MutexGuard<'static, BTreeMap<&'static str, LinkedFactory>> {
+ // A poisoned lock only means a panic elsewhere while holding it; the map
+ // itself is never left half-updated by `insert`/`get`.
+ FACTORIES.lock().unwrap_or_else(|poison| poison.into_inner())
+}
+
+/// Register the in-process factory for the module registered as `id`.
+///
+/// Call once per module before the first load, normally from
+/// `openhuman-modules-linked::install`. A repeat registration for the same id
+/// replaces the earlier one (the last installer wins), which keeps `install`
+/// idempotent.
+pub fn register_linked(id: &'static str, factory: LinkedFactory) {
+ let replaced = factories().insert(id, factory).is_some();
+ log::debug!("[modules] linked factory registered id={id} replaced={replaced}");
+}
+
+/// Whether a factory is registered for `id`.
+#[must_use]
+pub fn is_registered(id: &str) -> bool {
+ factories().contains_key(id)
+}
+
+/// The factory registered for `id`, if any.
+#[must_use]
+pub(super) fn factory_for(id: &str) -> Option<LinkedFactory> {
+ factories().get(id).copied()
+}
+
+/// Ids with a registered factory, for diagnostics.
+#[must_use]
+pub fn registered_ids() -> Vec<&'static str> {
+ factories().keys().copied().collect()
+}
+
+#[cfg(test)]
+#[path = "linked_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/modules/linked_tests.rs b/crates/openhuman-core/src/modules/linked_tests.rs
new file mode 100644
index 0000000000..f3883f6d50
--- /dev/null
+++ b/crates/openhuman-core/src/modules/linked_tests.rs
@@ -0,0 +1,22 @@
+use super::*;
+
+fn failing() -> tinybus::Result<LinkedModule> {
+ Err(tinybus::Error::failed("test factory"))
+}
+
+#[test]
+fn registration_is_visible_and_idempotent() {
+ assert!(!is_registered("linked-registry-test"));
+ register_linked("linked-registry-test", failing);
+ register_linked("linked-registry-test", failing);
+ assert!(is_registered("linked-registry-test"));
+ assert_eq!(
+ registered_ids()
+ .iter()
+ .filter(|id| **id == "linked-registry-test")
+ .count(),
+ 1
+ );
+ assert!(factory_for("linked-registry-test").is_some());
+ assert!(factory_for("linked-registry-absent").is_none());
+}
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index 6f242b6cae..5e3330a31c 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -35,6 +35,7 @@
//! and claims it for a browser about to start.
//! - [`wallet`] — the host half of the `tinywallet` module, which builds and
//! assembles transactions while the signing key stays in this process.
+//! - [`linked`] — the registry of modules linked into the executable (local development).
//! - [`host`] — the module broker, connection and loader.
//! - [`ops`] — resolving, loading, and reporting status.
//! - [`schemas`] — the `modules` RPC surface.
@@ -74,6 +75,8 @@ mod failure;
#[cfg(feature = "modules")]
pub mod host;
#[cfg(feature = "modules")]
+pub mod linked;
+#[cfg(feature = "modules")]
pub mod ops;
pub mod registry;
mod source;
diff --git a/crates/openhuman-core/src/modules/source.rs b/crates/openhuman-core/src/modules/source.rs
index a2bcb1fd84..fda56958d8 100644
--- a/crates/openhuman-core/src/modules/source.rs
+++ b/crates/openhuman-core/src/modules/source.rs
@@ -29,11 +29,19 @@ pub(super) struct SourcePlan {
/// 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.
+/// Only a binary whose host called [`super::linked::register_linked`] (the CLI
+/// built with `linked-modules`) has one; every other binary falls back.
#[must_use]
-pub(super) fn linked_factory_available(_id: &str) -> bool {
- false
+pub(super) fn linked_factory_available(id: &str) -> bool {
+ #[cfg(feature = "modules")]
+ {
+ super::linked::is_registered(id)
+ }
+ #[cfg(not(feature = "modules"))]
+ {
+ let _ = id;
+ false
+ }
}
/// The locally built artifact for `id`, if one exists.