Skip to content

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

feat(rpc): open configured storage for one-shot CLI commands

Steven Enamakelab182ff54fef
diff --git a/crates/openhuman-rpc/src/host.rs b/crates/openhuman-rpc/src/host.rs
index 21ddb06383..2fe2d1f32b 100644
--- a/crates/openhuman-rpc/src/host.rs
+++ b/crates/openhuman-rpc/src/host.rs
@@ -69,7 +69,51 @@ pub fn cli(args: &[String]) -> anyhow::Result<()> {
args.first().map(String::as_str).unwrap_or("<none>"),
args.len()
);
- cli_builder().run_from_args(args)
+ let mut builder = cli_builder();
+ if cli_command_uses_storage(args) {
+ // A one-shot command reads the same backend the server would: the
+ // configured storage URL, opened on the core's storage runtime so the
+ // backend outlives this call. No URL leaves the classic layout alone.
+ let provider = crate::core_host::storage::block_on_anyhow(
+ crate::session_store::provider_if_configured(),
+ )?;
+ if let Some(provider) = provider {
+ log::debug!("[rpc:host] cli: storage-backed session store installed");
+ builder = builder.session_store(provider);
+ }
+ }
+ builder.run_from_args(args)
+}
+
+/// Whether the CLI subcommand in `args` should open the configured storage
+/// backend itself. `run` / `serve` open it in their own server boot, and help,
+/// the moved TUI names and `sentry-test` never touch stored state.
+#[cfg(feature = "server")]
+fn cli_command_uses_storage(args: &[String]) -> bool {
+ let mut args = args.iter().map(String::as_str);
+ let mut command = None;
+ while let Some(arg) = args.next() {
+ match arg {
+ // Launch-wide flags with a separate value.
+ "--model" | "--model-id" | "-m" | "--provider" | "--provider-id" | "-p" => {
+ args.next();
+ }
+ _ if arg.starts_with("--model=")
+ || arg.starts_with("--model-id=")
+ || arg.starts_with("--provider=")
+ || arg.starts_with("--provider-id=") => {}
+ _ => {
+ command = Some(arg);
+ break;
+ }
+ }
+ }
+ !matches!(
+ command,
+ None | Some(
+ "run" | "serve" | "help" | "-h" | "--help" | "tui" | "chat" | "sentry-test"
+ )
+ )
}
/// What the embedded desktop server binds and how it authenticates.
diff --git a/crates/openhuman-rpc/src/host_tests.rs b/crates/openhuman-rpc/src/host_tests.rs
index 947abfada3..b8b28cc4e4 100644
--- a/crates/openhuman-rpc/src/host_tests.rs
+++ b/crates/openhuman-rpc/src/host_tests.rs
@@ -93,3 +93,16 @@ fn tui_builder_runs_every_domain_without_services_on_the_disk_store() {
assert!(summary.has_session_store);
assert!(!summary.has_server_launcher, "the TUI binds no server");
}
+
+#[test]
+#[cfg(feature = "server")]
+fn cli_storage_is_opened_for_one_shot_commands_only() {
+ let args = |parts: &[&str]| parts.iter().map(|p| p.to_string()).collect::<Vec<_>>();
+ assert!(cli_command_uses_storage(&args(&["agent", "list"])));
+ assert!(cli_command_uses_storage(&args(&["cron", "list"])));
+ assert!(cli_command_uses_storage(&args(&["--model", "x", "approvals", "list"])));
+ assert!(!cli_command_uses_storage(&args(&["serve"])));
+ assert!(!cli_command_uses_storage(&args(&["--provider=a", "-m", "b", "run"])));
+ assert!(!cli_command_uses_storage(&args(&[])));
+ assert!(!cli_command_uses_storage(&args(&["--help"])));
+}
diff --git a/crates/openhuman-rpc/src/session_store/mod.rs b/crates/openhuman-rpc/src/session_store/mod.rs
index 820d7940a5..a276da8495 100644
--- a/crates/openhuman-rpc/src/session_store/mod.rs
+++ b/crates/openhuman-rpc/src/session_store/mod.rs
@@ -205,6 +205,21 @@ pub async fn provider_for_host() -> anyhow::Result<Arc<dyn SessionStoreProvider>
provider_for_url(configured_storage_url().await?).await
}
+/// The storage-backed session store, only when the host's configuration
+/// names a storage URL; `None` leaves the classic layout and any process
+/// state untouched. For one-shot CLI commands, which have no reason to
+/// install the classic store but must see the same backend the server does.
+///
+/// # Errors
+///
+/// When the URL cannot be resolved, parsed or opened.
+pub async fn provider_if_configured() -> anyhow::Result<Option<Arc<dyn SessionStoreProvider>>> {
+ match configured_storage_url().await? {
+ Some(url) => provider_for_url(Some(url)).await.map(Some),
+ None => Ok(None),
+ }
+}
+
/// The storage URL the host asks for: `OPENHUMAN_STORAGE_URL`, else
/// `[storage] url` from the config, else `None` (the classic layout).
///