Skip to content

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

feat(core): add runtime controller extension registry

Steven Enamakel00540662111e
diff --git a/crates/openhuman-core/src/core/all.rs b/crates/openhuman-core/src/core/all.rs
index 63546d1773..2e852148d9 100644
--- a/crates/openhuman-core/src/core/all.rs
+++ b/crates/openhuman-core/src/core/all.rs
@@ -354,6 +354,141 @@ fn internal_registry() -> &'static [GroupedController] {
.as_slice()
}
+/// Controllers registered by a crate *above* the core at runtime — today the
+/// hosted TinyHumans proxies (`billing`, `team`, `referral`, `announcements`)
+/// from `openhuman-tinyhumans`, which the core cannot name because the core
+/// carries no backend client.
+///
+/// Append-only and chained into every lookup below alongside [`registry`],
+/// so there is no "register before first registry access" ordering rule: a
+/// host may install an extension before or after the core boots, and the
+/// only observable rule is "install before the first dispatch of one of its
+/// methods". [`DomainSet`](crate::core::runtime::DomainSet) gating applies to
+/// extension controllers exactly as to built-in ones through their group.
+static EXTENSIONS: RwLock<Option<Arc<Vec<GroupedController>>>> = RwLock::new(None);
+static EXTENSION_NAMESPACES: RwLock<Vec<(&'static str, &'static str)>> = RwLock::new(Vec::new());
+
+/// A set of controllers a crate above the core contributes to the registry.
+pub struct ControllerExtension {
+ /// The domain family every controller in `controllers` belongs to; the
+ /// ambient [`DomainSet`](crate::core::runtime::DomainSet) gates them
+ /// through it.
+ pub group: DomainGroup,
+ /// The controllers, built exactly as a core domain builds them.
+ pub controllers: Vec<RegisteredController>,
+ /// `(namespace, description)` pairs for [`namespace_description`].
+ pub namespaces: &'static [(&'static str, &'static str)],
+}
+
+/// Register `ext`'s controllers alongside the built-in registry.
+///
+/// Idempotent for an identical re-registration (same `(namespace, function)`
+/// keys already present): returns `Ok(())` and changes nothing, so a host or
+/// test fixture may call it freely. Any *other* collision with the built-in,
+/// internal or previously extended set is an error, checked with the same
+/// [`validate_registry`] drift guard the built-in set passes at boot.
+pub fn register_controller_extension(ext: ControllerExtension) -> Result<(), String> {
+ let mut slot = EXTENSIONS
+ .write()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ let existing: Vec<GroupedController> = slot.as_deref().cloned().unwrap_or_default();
+
+ let incoming_keys: Vec<String> = ext
+ .controllers
+ .iter()
+ .map(|c| format!("{}.{}", c.schema.namespace, c.schema.function))
+ .collect();
+ let already: std::collections::BTreeSet<String> = existing
+ .iter()
+ .map(|g| format!("{}.{}", g.controller.schema.namespace, g.controller.schema.function))
+ .collect();
+ if !incoming_keys.is_empty() && incoming_keys.iter().all(|k| already.contains(k)) {
+ log::debug!(
+ "[registry] extension for group {:?} already registered ({} controllers); no-op",
+ ext.group,
+ incoming_keys.len()
+ );
+ return Ok(());
+ }
+
+ let mut merged = existing;
+ for controller in ext.controllers {
+ merged.push(GroupedController {
+ group: ext.group,
+ capability: None,
+ controller,
+ });
+ }
+ // Validate the union so an extension cannot shadow a built-in or internal
+ // method, and cannot carry an invalid schema the boot guard would reject.
+ let union: Vec<GroupedController> = registry()
+ .iter()
+ .chain(internal_registry().iter())
+ .chain(merged.iter())
+ .cloned()
+ .collect();
+ validate_registry(&union).map_err(|err| format!("invalid controller extension: {err}"))?;
+
+ log::info!(
+ "[registry] registered controller extension group={:?} controllers={} namespaces={:?}",
+ ext.group,
+ incoming_keys.len(),
+ ext.namespaces.iter().map(|(n, _)| *n).collect::<Vec<_>>()
+ );
+ *slot = Some(Arc::new(merged));
+ let mut names = EXTENSION_NAMESPACES
+ .write()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ for pair in ext.namespaces {
+ if !names.iter().any(|(n, _)| n == &pair.0) {
+ names.push(*pair);
+ }
+ }
+ Ok(())
+}
+
+/// Snapshot of the extension registry: an `Arc` clone per lookup, never a
+/// `Vec` clone.
+fn extension_registry() -> Arc<Vec<GroupedController>> {
+ EXTENSIONS
+ .read()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .clone()
+ .unwrap_or_else(|| Arc::new(Vec::new()))
+}
+
+/// The agent-facing registry: built-in controllers followed by every
+/// registered extension. Every public lookup iterates this, so extension
+/// controllers are first-class for schema, dispatch, capability and CLI
+/// routing.
+struct RegistryView {
+ builtin: &'static [GroupedController],
+ extensions: Arc<Vec<GroupedController>>,
+}
+
+impl RegistryView {
+ fn iter(&self) -> impl Iterator<Item = &GroupedController> + '_ {
+ self.builtin.iter().chain(self.extensions.iter())
+ }
+}
+
+fn registry_view() -> RegistryView {
+ RegistryView {
+ builtin: registry(),
+ extensions: extension_registry(),
+ }
+}
+
+/// Description registered by an extension for `namespace`, if any.
+fn extension_namespace_description(namespace: &str) -> Option<&'static str> {
+ EXTENSION_NAMESPACES
+ .read()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .iter()
+ .find(|(n, _)| *n == namespace)
+ .map(|(_, d)| *d)
+}
+
/// Returns a reference to the global CLI adapter registry.
fn cli_adapters() -> &'static [RegisteredCliAdapter] {
CLI_ADAPTERS.get_or_init(|| {
@@ -1022,8 +1157,7 @@ fn build_internal_only_controllers() -> Vec<GroupedController> {
/// the complete set (byte-identical to pre-#4796).
pub fn all_registered_controllers() -> Vec<RegisteredController> {
let caps = crate::core::runtime::context::CoreContext::current_memory_capabilities();
- registry()
- .iter()
+ { let view = registry_view(); view }.iter()
.filter(|g| group_allowed(g.group) && capability_allowed_in(caps, g.capability))
.map(|g| g.controller.clone())
.collect()
@@ -1038,8 +1172,7 @@ pub fn all_registered_controllers() -> Vec<RegisteredController> {
/// automatically under `harness()`.
pub fn all_controller_schemas() -> Vec<ControllerSchema> {
let caps = crate::core::runtime::context::CoreContext::current_memory_capabilities();
- registry()
- .iter()
+ { let view = registry_view(); view }.iter()
.filter(|g| group_allowed(g.group) && capability_allowed_in(caps, g.capability))
.map(|g| g.controller.schema.clone())
.collect()
@@ -1187,8 +1320,7 @@ pub fn rpc_method_from_parts(namespace: &str, function: &str) -> Option<String>
// and CLI routing, which are harmless for an about-to-be-rejected gated
// method — the DomainSet gate is enforced at dispatch
// (`try_invoke_registered_rpc`), not here. See that fn for the rationale.
- registry()
- .iter()
+ { let view = registry_view(); view }.iter()
.find(|g| {
g.controller.schema.namespace == namespace && g.controller.schema.function == function
})
@@ -1215,8 +1347,7 @@ pub fn rpc_method_from_parts(namespace: &str, function: &str) -> Option<String>
/// CLI-invokable in any configuration, so reporting a capability fact for one
/// would name a cause that is not the reason the command is unavailable.
pub fn capability_for_parts(namespace: &str, function: &str) -> Option<Option<Capability>> {
- registry()
- .iter()
+ { let view = registry_view(); view }.iter()
.find(|g| {
g.controller.schema.namespace == namespace && g.controller.schema.function == function
})
@@ -1231,8 +1362,7 @@ pub fn capability_for_parts(namespace: &str, function: &str) -> Option<Option<Ca
/// must still produce the CLI's configuration-fact diagnostic before it
/// dispatches a capability-gated method.
pub fn capability_for_rpc_method(method: &str) -> Option<Option<Capability>> {
- registry()
- .iter()
+ { let view = registry_view(); view }.iter()
.find(|g| g.controller.rpc_method_name() == method)
.map(|g| g.capability)
}
@@ -1254,8 +1384,7 @@ pub fn capability_for_rpc_method(method: &str) -> Option<Option<Capability>> {
pub fn sole_capability_for_namespace(namespace: &str) -> Option<Capability> {
let mut found: Option<Capability> = None;
let mut any = false;
- for grouped in registry()
- .iter()
+ for grouped in { let view = registry_view(); view }.iter()
.filter(|g| g.controller.schema.namespace == namespace)
{
any = true;
@@ -1292,8 +1421,7 @@ pub fn schema_for_rpc_method(method: &str) -> Option<ControllerSchema> {
// The memory-capability gate (M5.2) rides here for exactly the same reason:
// a `memory_tree.*` method hidden because the bound driver never advertised
// `tree` must not leak back out through a param-validation error.
- registry()
- .iter()
+ { let view = registry_view(); view }.iter()
.chain(internal_registry().iter())
.find(|g| {
g.controller.rpc_method_name() == method
@@ -1497,8 +1625,7 @@ pub async fn try_invoke_registered_rpc(
method: &str,
params: Map<String, Value>,
) -> Option<Result<Value, String>> {
- let grouped = registry()
- .iter()
+ let grouped = { let view = registry_view(); view }.iter()
.chain(internal_registry().iter())
.find(|g| g.controller.rpc_method_name() == method)?;