Skip to content

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

feat(core): replace direct jsonrpc calls with server launcher abstraction

Steven Enamakel7c1bf0dd236c
diff --git a/crates/openhuman-core/src/core/cli.rs b/crates/openhuman-core/src/core/cli.rs
index 5163fcc7dd..19bcbdedfe 100644
--- a/crates/openhuman-core/src/core/cli.rs
+++ b/crates/openhuman-core/src/core/cli.rs
@@ -9,7 +9,7 @@ use serde_json::{Map, Value};
use std::collections::BTreeMap;
use crate::core::all;
-use crate::core::jsonrpc::{default_state, invoke_method};
+use crate::core::invoke::{default_state, invoke_method};
use crate::core::logging::CliLogDefault;
use crate::core::{ControllerSchema, TypeSchema};
use crate::core::params::parse_json_params;
@@ -394,13 +394,18 @@ fn run_server_command(args: &[String]) -> Result<()> {
.thread_stack_size(crate::core::runtime::AGENT_WORKER_STACK_BYTES)
.max_blocking_threads(crate::core::runtime::MAX_BLOCKING_THREADS)
.build()?;
- rt.block_on(async {
- if headless_api {
- crate::core::jsonrpc::run_server_headless(host.as_deref(), port).await
- } else {
- crate::core::jsonrpc::run_server(host.as_deref(), port, socketio_enabled).await
- }
+ let launcher = crate::core::server_launcher::installed_server_launcher().ok_or_else(|| {
+ anyhow::anyhow!(
+ "this binary has no JSON-RPC server linked in; the host must call \
+ openhuman_rpc::server::install_cli_server() before run_core_from_args"
+ )
})?;
+ rt.block_on(launcher(crate::core::server_launcher::ServeRequest {
+ host,
+ port,
+ socketio_enabled,
+ headless_api,
+ }))?;
Ok(())
}
diff --git a/crates/openhuman-core/src/core/mod.rs b/crates/openhuman-core/src/core/mod.rs
index 921940abb8..40c7d01bf3 100644
--- a/crates/openhuman-core/src/core/mod.rs
+++ b/crates/openhuman-core/src/core/mod.rs
@@ -13,8 +13,6 @@ pub mod bus;
pub mod bus_testing;
pub mod cli;
pub mod cli_capability;
-#[cfg(feature = "http-server")]
-pub mod dev_connect;
pub mod dispatch;
pub mod event_bind_tokens;
pub mod events;
@@ -22,7 +20,7 @@ pub mod events;
// shell asserts `HTTP_SERVER_COMPILED_IN` so a listener-less core fails the
// build instead of shipping silently (cf. voice #4901).
pub mod http_server_status;
-pub mod jsonrpc;
+pub mod invoke;
pub mod legacy_aliases;
pub mod log_redaction;
pub mod logging;
@@ -35,7 +33,8 @@ pub mod runtime;
#[cfg(feature = "crash-reporting")]
pub mod sentry_transport;
pub mod shutdown;
-pub mod socketio;
+pub mod server_launcher;
+pub mod session_expiry;
pub mod structured_error;
pub mod subsystem;
pub mod subsystems_cli;
diff --git a/crates/openhuman-core/src/core/server_launcher.rs b/crates/openhuman-core/src/core/server_launcher.rs
new file mode 100644
index 0000000000..3881d06785
--- /dev/null
+++ b/crates/openhuman-core/src/core/server_launcher.rs
@@ -0,0 +1,48 @@
+//! The port through which `openhuman-core run` / `serve` starts a server.
+//!
+//! The JSON-RPC server lives in `openhuman-rpc`, which sits above this crate,
+//! so the CLI cannot call it directly. A host binary installs a launcher once
+//! at startup (`openhuman_rpc::server::install_cli_server()`) before it hands
+//! its arguments to [`run_core_from_args`](crate::run_core_from_args).
+//! Without one, `run` / `serve` fail with an error that says so instead of
+//! starting a core nothing can reach.
+
+use std::future::Future;
+use std::pin::Pin;
+use std::sync::OnceLock;
+
+/// What the `run` / `serve` subcommand asked for.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ServeRequest {
+ /// Bind host, when the operator passed one.
+ pub host: Option<String>,
+ /// Bind port, when the operator passed one.
+ pub port: Option<u16>,
+ /// Serve Socket.IO alongside HTTP JSON-RPC (`--jsonrpc-only` clears it).
+ pub socketio_enabled: bool,
+ /// Request/response API only, with no background services.
+ pub headless_api: bool,
+}
+
+/// Starts a server for a [`ServeRequest`] and resolves when it stops.
+pub type ServerLauncher =
+ fn(ServeRequest) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>>;
+
+static LAUNCHER: OnceLock<ServerLauncher> = OnceLock::new();
+
+/// Install the launcher `run` / `serve` use. The first call wins; later calls
+/// are ignored, so every host path can install without coordinating.
+pub fn install_server_launcher(launcher: ServerLauncher) {
+ if LAUNCHER.set(launcher).is_err() {
+ log::debug!("[cli] server launcher already installed; keeping the first");
+ }
+}
+
+/// The installed launcher, if a host installed one.
+pub fn installed_server_launcher() -> Option<ServerLauncher> {
+ LAUNCHER.get().copied()
+}
+
+#[cfg(test)]
+#[path = "server_launcher_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/core/server_launcher_tests.rs b/crates/openhuman-core/src/core/server_launcher_tests.rs
new file mode 100644
index 0000000000..1b3b2857d0
--- /dev/null
+++ b/crates/openhuman-core/src/core/server_launcher_tests.rs
@@ -0,0 +1,23 @@
+use super::*;
+
+fn ok_launcher(_: ServeRequest) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> {
+ Box::pin(async { Ok(()) })
+}
+
+fn other_launcher(_: ServeRequest) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> {
+ Box::pin(async { anyhow::bail!("second launcher must not replace the first") })
+}
+
+#[tokio::test]
+async fn first_installed_launcher_wins() {
+ install_server_launcher(ok_launcher);
+ install_server_launcher(other_launcher);
+ let launcher = installed_server_launcher().expect("a launcher is installed");
+ let request = ServeRequest {
+ host: None,
+ port: None,
+ socketio_enabled: true,
+ headless_api: false,
+ };
+ launcher(request).await.expect("the first launcher runs");
+}