Skip to content

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

chore: files changed crates/openhuman-core/src/agent/host_agents.rs,crates/openhuman-core/src/agent/

Steven Enamakel807a2a0ad55b
diff --git a/crates/openhuman-core/src/agent/host_agents.rs b/crates/openhuman-core/src/agent/host_agents.rs
index 19f5a9f92b..4affadbece 100644
--- a/crates/openhuman-core/src/agent/host_agents.rs
+++ b/crates/openhuman-core/src/agent/host_agents.rs
@@ -147,4 +147,4 @@ pub fn resolve(agent_id: &str) -> Option<HostAgent> {
#[cfg(test)]
#[path = "host_agents_tests.rs"]
-mod tests;
+pub(crate) mod tests;
diff --git a/crates/openhuman-core/src/agent/host_agents_tests.rs b/crates/openhuman-core/src/agent/host_agents_tests.rs
index c9400fa2e3..52ba853738 100644
--- a/crates/openhuman-core/src/agent/host_agents_tests.rs
+++ b/crates/openhuman-core/src/agent/host_agents_tests.rs
@@ -2,10 +2,11 @@ use super::*;
use crate::agent::harness::definition::AgentDefinition;
use crate::core::runtime::DomainSet;
-/// The resolver slot is process-wide; tests that install one take turns.
+/// The resolver slot is process-wide; every test in the crate that installs
+/// one takes its turn through this lock.
static SLOT: std::sync::Mutex<()> = std::sync::Mutex::new(());
-fn lock() -> std::sync::MutexGuard<'static, ()> {
+pub(crate) fn lock() -> std::sync::MutexGuard<'static, ()> {
SLOT.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
diff --git a/crates/openhuman-core/src/cron/scheduler/agent_run.rs b/crates/openhuman-core/src/cron/scheduler/agent_run.rs
index 95b0f17c4e..5e55a0ce1c 100644
--- a/crates/openhuman-core/src/cron/scheduler/agent_run.rs
+++ b/crates/openhuman-core/src/cron/scheduler/agent_run.rs
@@ -130,7 +130,7 @@ pub(super) async fn run_agent_job(
"[cron] building isolated agent for scheduled job"
);
match build_agent_for_cron_job(&effective, job) {
- Ok(BuiltCronAgent { mut agent }) => {
+ Ok(BuiltCronAgent { mut agent, .. }) => {
// Tag events so downstream subscribers can correlate
// cron-triggered turns. `cron` is the channel so the
// event bus can filter from other flows (`cli`, `web`…).
@@ -279,6 +279,9 @@ pub(super) fn cron_turn_overrides() -> crate::agent::session_host::TurnOverrides
pub(super) struct BuiltCronAgent {
pub(crate) agent: OpenHumanSessionHost,
+ /// The host agent's context the turn must run in, when `job.agent_id`
+ /// named a host-registered agent (`agent::host_agents`).
+ pub(crate) context: Option<std::sync::Arc<crate::core::runtime::CoreContext>>,
}
pub(super) fn build_agent_for_cron_job(
@@ -293,7 +296,7 @@ pub(super) fn build_agent_for_cron_job(
agent_id = %agent_id,
"[cron] built scheduled job agent from definition"
);
- Ok(BuiltCronAgent { agent })
+ Ok(BuiltCronAgent { agent, context: None })
}
Err(e) => {
tracing::warn!(
@@ -303,7 +306,10 @@ pub(super) fn build_agent_for_cron_job(
"[cron] failed to build agent from definition; falling back to canonical orchestrator"
);
OpenHumanSessionHost::from_config_for_agent(config, "orchestrator")
- .map(|agent| BuiltCronAgent { agent })
+ .map(|agent| BuiltCronAgent {
+ agent,
+ context: None,
+ })
}
}
}
diff --git a/crates/openhuman-core/src/cron/scheduler_host_agent_tests.rs b/crates/openhuman-core/src/cron/scheduler_host_agent_tests.rs
new file mode 100644
index 0000000000..b3a4dfd644
--- /dev/null
+++ b/crates/openhuman-core/src/cron/scheduler_host_agent_tests.rs
@@ -0,0 +1,110 @@
+//! A cron agent job whose `agent_id` names a host-registered agent
+//! (`agent::host_agents`) runs as that agent: its definition, its host tools,
+//! its context. Any other id still resolves through the registries.
+
+use super::*;
+use crate::agent::host_agents::{self, HostAgent, HostAgentResolver};
+use crate::core::runtime::{CoreContext, DomainSet};
+use std::sync::atomic::{AtomicUsize, Ordering};
+use tempfile::TempDir;
+
+struct Marker;
+
+#[async_trait::async_trait]
+impl tinytools::Tool for Marker {
+ fn name(&self) -> &str {
+ "cron_host_marker"
+ }
+ fn description(&self) -> &str {
+ "a host tool only the host agent has"
+ }
+ fn parameters_schema(&self) -> serde_json::Value {
+ serde_json::json!({ "type": "object", "properties": {} })
+ }
+ async fn execute(&self, _args: serde_json::Value) -> anyhow::Result<tinytools::ToolResult> {
+ Ok(tinytools::ToolResult::success("ok"))
+ }
+}
+
+struct Host {
+ config: Config,
+ belt_builds: Arc<AtomicUsize>,
+}
+
+impl HostAgentResolver for Host {
+ fn resolve(&self, agent_id: &str) -> Option<HostAgent> {
+ if agent_id != "cron-host-agent" {
+ return None;
+ }
+ let mut definition = crate::agent::harness::definition::AgentDefinitionRegistry::builtins_only()
+ .get("orchestrator")
+ .cloned()
+ .unwrap();
+ definition.id = agent_id.to_string();
+ let builds = Arc::clone(&self.belt_builds);
+ Some(HostAgent {
+ definition,
+ config: self.config.clone(),
+ host_tools: Some(Arc::new(move |turn| {
+ assert_eq!(turn.agent_id(), "cron-host-agent");
+ builds.fetch_add(1, Ordering::SeqCst);
+ crate::agent::HostTurnTools::advertised(vec![Box::new(Marker)])
+ })),
+ context: CoreContext::for_test(DomainSet::full(), Some(self.config.workspace_dir.clone())),
+ })
+ }
+}
+
+fn agent_job(agent_id: &str) -> CronJob {
+ let mut job = test_job("");
+ job.job_type = JobType::Agent;
+ job.agent_id = Some(agent_id.to_string());
+ job.prompt = Some("check in".into());
+ job
+}
+
+#[tokio::test]
+async fn a_cron_job_for_a_host_agent_builds_with_its_host_tools_and_context() {
+ let _slot = crate::agent::host_agents::tests::lock();
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp).await;
+ let belt_builds = Arc::new(AtomicUsize::new(0));
+ let installed: Arc<dyn HostAgentResolver> = Arc::new(Host {
+ config: config.clone(),
+ belt_builds: Arc::clone(&belt_builds),
+ });
+ host_agents::install(Arc::clone(&installed));
+
+ let built = build_agent_for_cron_job(&config, &agent_job("cron-host-agent"));
+ host_agents::clear_if(&installed);
+ let built = built.expect("the host agent builds");
+
+ assert!(built.context.is_some(), "the turn runs in the host agent's context");
+ assert!(belt_builds.load(Ordering::SeqCst) > 0, "the host belt was built");
+ assert!(built
+ .agent
+ .visible_tool_specs_arc()
+ .iter()
+ .any(|spec| spec.name == "cron_host_marker"));
+}
+
+#[tokio::test]
+async fn an_id_the_host_does_not_know_falls_back_to_the_registries() {
+ let _slot = crate::agent::host_agents::tests::lock();
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp).await;
+ let installed: Arc<dyn HostAgentResolver> = Arc::new(Host {
+ config: config.clone(),
+ belt_builds: Arc::new(AtomicUsize::new(0)),
+ });
+ host_agents::install(Arc::clone(&installed));
+ let built = build_agent_for_cron_job(&config, &agent_job("orchestrator"));
+ host_agents::clear_if(&installed);
+ let built = built.expect("the orchestrator builds from the registries");
+ assert!(built.context.is_none());
+ assert!(!built
+ .agent
+ .visible_tool_specs_arc()
+ .iter()
+ .any(|spec| spec.name == "cron_host_marker"));
+}
diff --git a/crates/openhuman-core/src/cron/scheduler_tests.rs b/crates/openhuman-core/src/cron/scheduler_tests.rs
index 843dd1eed6..4ab7f42257 100644
--- a/crates/openhuman-core/src/cron/scheduler_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_tests.rs
@@ -102,6 +102,8 @@ fn next_user_error(
mod classifier_and_delivery_tests;
#[path = "scheduler_dispatch_tests.rs"]
mod dispatch_tests;
+#[path = "scheduler_host_agent_tests.rs"]
+mod host_agent_tests;
#[path = "scheduler_frequency_tests.rs"]
mod frequency_tests;
#[path = "scheduler_halt_and_persist_tests.rs"]