Skip to content

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

refactor(saas): move the SaaS host admission into mode.rs

Steven Enamakel9e14b93e1978
diff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rs
index 021d15f898..ddf07b1612 100644
--- a/crates/openhuman-core/src/core/runtime/context.rs
+++ b/crates/openhuman-core/src/core/runtime/context.rs
@@ -222,20 +222,8 @@ impl CoreContext {
.map(|t| t.name())
.unwrap_or("<process-global>")
);
- // A process locked to SaaS boots nothing but its own SaaS core.
- if crate::core::runtime::mode::is_saas() && host_kind != HostKind::Saas {
- anyhow::bail!("[core-context] this process serves SaaS; refusing a {host_kind:?} core");
- }
- // A SaaS core boots only through `saas::build`, which runs the boot
- // guard and locks the mode first, and only once per process.
- if host_kind == HostKind::Saas {
- if !crate::core::runtime::mode::is_saas() {
- anyhow::bail!("[core-context] a SaaS core boots only through saas::build");
- }
- if DEFAULT_CONTEXT.get().is_some() {
- anyhow::bail!("[core-context] this process already serves its SaaS core");
- }
- }
+ crate::core::runtime::mode::admit_core(host_kind, DEFAULT_CONTEXT.get().is_some())
+ .map_err(|e| anyhow::anyhow!("[core-context] {e}"))?;
// 1. Ensure all controllers are registered before anything dispatches.
let _ = crate::core::all::all_registered_controllers();
diff --git a/crates/openhuman-core/src/core/runtime/mode.rs b/crates/openhuman-core/src/core/runtime/mode.rs
index beeacea9f5..c4333e76ab 100644
--- a/crates/openhuman-core/src/core/runtime/mode.rs
+++ b/crates/openhuman-core/src/core/runtime/mode.rs
@@ -91,6 +91,27 @@ pub(crate) fn reserve_saas_boot() -> Result<(), String> {
Ok(())
}
+/// Whether a core of `host_kind` may boot now. A process locked to SaaS boots
+/// nothing but its own SaaS core; a SaaS core boots only through
+/// `saas::build`, which runs the boot guard and locks the mode first, and only
+/// once per process (`core_running`: a default context already exists).
+pub(crate) fn admit_core(
+ host_kind: crate::core::types::HostKind,
+ core_running: bool,
+) -> Result<(), String> {
+ let saas_host = host_kind == crate::core::types::HostKind::Saas;
+ match (is_saas(), saas_host) {
+ (true, false) => Err(format!(
+ "this process serves SaaS; refusing a {host_kind:?} core"
+ )),
+ (false, true) => Err("a SaaS core boots only through saas::build".to_string()),
+ (true, true) if core_running => {
+ Err("this process already serves its SaaS core".to_string())
+ }
+ _ => Ok(()),
+ }
+}
+
/// Fix the process mode. Locking the mode it already has is a no-op; locking
/// a different one fails, because one process never serves both shapes.
pub(crate) fn lock_mode(mode: Mode) -> Result<(), String> {
diff --git a/crates/openhuman-core/src/core/runtime/mode_tests.rs b/crates/openhuman-core/src/core/runtime/mode_tests.rs
index 9792857305..9165b6bb7b 100644
--- a/crates/openhuman-core/src/core/runtime/mode_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/mode_tests.rs
@@ -47,3 +47,12 @@ fn saas_requests_are_spotted_before_parsing() {
assert!(!requested_in(&args(&["run"]), None));
assert!(!requested_in(&args(&["run", "saas"]), Some("")));
}
+
+#[test]
+fn a_saas_core_needs_the_saas_boot() {
+ use crate::core::types::HostKind;
+ // This test process is never locked to SaaS.
+ assert!(admit_core(HostKind::Cli, false).is_ok());
+ let refused = admit_core(HostKind::Saas, false).unwrap_err();
+ assert!(refused.contains("saas::build"), "{refused}");
+}