Skip to content

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

fix(runtime): reject agent ids with pending cleanup

Steven Enamakel681dcaddbdcc
diff --git a/crates/openhuman-embed/src/runtime/builder_tests.rs b/crates/openhuman-embed/src/runtime/builder_tests.rs
index 0f7a3225fd..72c583d02b 100644
--- a/crates/openhuman-embed/src/runtime/builder_tests.rs
+++ b/crates/openhuman-embed/src/runtime/builder_tests.rs
@@ -14,6 +14,19 @@ fn a_runtime_identifies_as_a_library_host_by_default() {
assert_eq!(RuntimeBuilder::new().host_kind, HostKind::Library);
}
+#[test]
+fn a_retiring_agent_id_is_not_reused_until_cleanup_is_acknowledged() {
+ assert!(matches!(
+ agent_admission_error("agent", false, true),
+ Some(AgentError::CleanupPending)
+ ));
+ assert_eq!(agent_admission_error("agent", false, false), None);
+ assert!(matches!(
+ agent_admission_error("agent", true, true),
+ Some(AgentError::DuplicateId(id)) if id == "agent"
+ ));
+}
+
#[test]
fn inherited_workspace_without_a_host_credential_keeps_installed_session_policy() {
assert_eq!(
diff --git a/crates/openhuman-embed/src/runtime/mod.rs b/crates/openhuman-embed/src/runtime/mod.rs
index 253444024a..332856bb01 100644
--- a/crates/openhuman-embed/src/runtime/mod.rs
+++ b/crates/openhuman-embed/src/runtime/mod.rs
@@ -362,8 +362,21 @@ impl Runtime {
// id as free and both instantiate, with the second `insert`
// silently overwriting the first agent's registry entry.
let mut agents = self.agents.lock().unwrap_or_else(|e| e.into_inner());
- if agents.contains_key(&id) {
- return Err(AgentError::DuplicateId(id));
+ let cleanup_pending = {
+ #[cfg(feature = "mcp")]
+ {
+ openhuman_core::mcp::host::agent_close_pending(
+ &self.base_config.workspace_dir,
+ &id,
+ )
+ }
+ #[cfg(not(feature = "mcp"))]
+ {
+ false
+ }
+ };
+ if let Some(error) = agent_admission_error(&id, agents.contains_key(&id), cleanup_pending) {
+ return Err(error);
}
if agents.len() >= self.max_agents {
log::warn!(
@@ -628,6 +641,20 @@ impl Runtime {
}
}
+fn agent_admission_error(
+ id: &str,
+ already_registered: bool,
+ cleanup_pending: bool,
+) -> Option<AgentError> {
+ if already_registered {
+ Some(AgentError::DuplicateId(id.to_owned()))
+ } else if cleanup_pending {
+ Some(AgentError::CleanupPending)
+ } else {
+ None
+ }
+}
+
impl std::fmt::Debug for Runtime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Resolved paths and a provider bearer are both in here.