Skip to content

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

feat(connectivity): add OccupiedByCore policy for port selection

Steven Enamakelea7333ace525
diff --git a/crates/openhuman-core/src/platform/connectivity/rpc.rs b/crates/openhuman-core/src/platform/connectivity/rpc.rs
index 7170b8c336..3a1c325b66 100644
--- a/crates/openhuman-core/src/platform/connectivity/rpc.rs
+++ b/crates/openhuman-core/src/platform/connectivity/rpc.rs
@@ -159,11 +159,34 @@ pub async fn pick_listen_port(preferred: u16) -> Result<PickListenPortResult, Pi
pick_listen_port_for_host("127.0.0.1", preferred).await
}
+/// What to do when the preferred port is held by another live OpenHuman core.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum OccupiedByCore {
+ /// Return [`PickListenPortError::WouldTakeOver`] so the caller can run the
+ /// stale-listener takeover (#1130). The desktop shell's embedded core: a
+ /// second copy of the app's own core on its port is a leftover to replace.
+ Takeover,
+ /// Treat it like any other occupant and move to a fallback port. Headless
+ /// `openhuman-core serve`: another checkout's or the desktop app's core is
+ /// a neighbour to coexist with, not something to replace.
+ Fallback,
+}
+
/// Same as [`pick_listen_port`] but allows an explicit host (used by the core
/// server bootstrap so CLI host overrides remain respected).
pub async fn pick_listen_port_for_host(
host: &str,
preferred: u16,
+) -> Result<PickListenPortResult, PickListenPortError> {
+ pick_listen_port_for_host_with(host, preferred, OccupiedByCore::Takeover).await
+}
+
+/// [`pick_listen_port_for_host`] with an explicit policy for a port that
+/// another OpenHuman core already holds.
+pub async fn pick_listen_port_for_host_with(
+ host: &str,
+ preferred: u16,
+ occupied_by_core: OccupiedByCore,
) -> Result<PickListenPortResult, PickListenPortError> {
let fallbacks: Vec<u16> = if preferred == DEFAULT_CORE_PORT {
(DEFAULT_FALLBACK_START..=DEFAULT_FALLBACK_END).collect()
@@ -172,7 +195,14 @@ pub async fn pick_listen_port_for_host(
.filter_map(|delta| preferred.checked_add(delta))
.collect()
};
- pick_listen_port_with_policy(host, preferred, &fallbacks, RetryPolicy::DEFAULT).await
+ pick_listen_port_with_policy(
+ host,
+ preferred,
+ &fallbacks,
+ RetryPolicy::DEFAULT,
+ occupied_by_core,
+ )
+ .await
}
async fn pick_listen_port_with_policy(
@@ -180,6 +210,7 @@ async fn pick_listen_port_with_policy(
preferred: u16,
fallback_ports: &[u16],
retry_policy: RetryPolicy,
+ occupied_by_core: OccupiedByCore,
) -> Result<PickListenPortResult, PickListenPortError> {
// `None` → preferred port is occupied (AddrInUse): probe for a stale
// OpenHuman listener to take over before falling back.
@@ -255,7 +286,9 @@ async fn pick_listen_port_with_policy(
let fingerprint_label = match excluded_reason {
None => {
let fingerprint = identify_listener(host, preferred).await;
- if matches!(fingerprint, ListenerFingerprint::OpenHumanCore) {
+ if matches!(fingerprint, ListenerFingerprint::OpenHumanCore)
+ && occupied_by_core == OccupiedByCore::Takeover
+ {
return Err(PickListenPortError::WouldTakeOver {
preferred,
fingerprint: fingerprint.as_human_readable(),
diff --git a/crates/openhuman-core/src/platform/connectivity/rpc_tests.rs b/crates/openhuman-core/src/platform/connectivity/rpc_tests.rs
index a884931646..2ca9fbe6af 100644
--- a/crates/openhuman-core/src/platform/connectivity/rpc_tests.rs
+++ b/crates/openhuman-core/src/platform/connectivity/rpc_tests.rs
@@ -64,6 +64,7 @@ async fn pick_listen_port_preferred_free() {
attempts: 0,
backoff: Duration::from_millis(1),
},
+ OccupiedByCore::Takeover,
)
.await
.expect("preferred bind should succeed");
@@ -88,6 +89,7 @@ async fn pick_listen_port_openhuman_listener_requests_takeover() {
attempts: 1,
backoff: Duration::from_millis(10),
},
+ OccupiedByCore::Takeover,
)
.await;
@@ -128,6 +130,7 @@ async fn pick_listen_port_other_listener_falls_back() {
attempts: 1,
backoff: Duration::from_millis(10),
},
+ OccupiedByCore::Takeover,
)
.await
.expect("fallback bind should succeed");
@@ -162,6 +165,7 @@ async fn pick_listen_port_all_candidates_busy_errors() {
attempts: 1,
backoff: Duration::from_millis(10),
},
+ OccupiedByCore::Takeover,
)
.await;
@@ -192,6 +196,7 @@ async fn pick_listen_port_retries_transient_addr_in_use() {
attempts: 6,
backoff: Duration::from_millis(10),
},
+ OccupiedByCore::Takeover,
)
.await
.expect("transient in-use should recover to preferred port");