Skip to content

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

test(agent): assert host env flows through run contexts

Steven Enamakel78a3e995ddb2
diff --git a/crates/openhuman-core/src/agent/stop_hooks_tests.rs b/crates/openhuman-core/src/agent/stop_hooks_tests.rs
index 887a0c146e..e125a7ab75 100644
--- a/crates/openhuman-core/src/agent/stop_hooks_tests.rs
+++ b/crates/openhuman-core/src/agent/stop_hooks_tests.rs
@@ -1,76 +1,39 @@
use super::*;
+use std::sync::Arc;
-#[tokio::test]
-async fn scoped_tool_limits_narrow_restore_and_keep_zero() {
- assert_eq!(tool_call_limit(10), 80);
- with_tool_call_limit(Some(4), async {
- assert_eq!(tool_call_limit(10), 4);
- with_tool_call_limit(Some(12), async {
- assert_eq!(tool_call_limit(10), 4);
- })
- .await;
- with_tool_call_limit(Some(0), async {
- assert_eq!(tool_call_limit(10), 0);
- })
- .await;
- assert_eq!(tool_call_limit(10), 4);
- with_tool_call_limit(None, async {
- assert_eq!(tool_call_limit(10), 4);
- })
- .await;
- })
- .await;
- assert_eq!(tool_call_limit(10), 80);
- with_tool_call_limit(Some(100), async {
- assert_eq!(tool_call_limit(1), 8);
- })
- .await;
+#[test]
+fn host_tool_cap_only_narrows_the_default_and_keeps_zero() {
+ assert_eq!(tool_call_limit(10, None), 80);
+ assert_eq!(tool_call_limit(10, Some(4)), 4);
+ // A cap wider than the product default never widens it.
+ assert_eq!(tool_call_limit(10, Some(1_000)), 80);
+ assert_eq!(tool_call_limit(1, Some(100)), 8);
+ // Zero is a real cap: no tool may run.
+ assert_eq!(tool_call_limit(10, Some(0)), 0);
}
-#[tokio::test]
-async fn concurrent_turns_do_not_share_tool_limits() {
- let (a, b) = tokio::join!(
- with_tool_call_limit(Some(2), async {
- tokio::task::yield_now().await;
- tool_call_limit(10)
- }),
- with_tool_call_limit(Some(7), async {
- tokio::task::yield_now().await;
- tool_call_limit(10)
- })
- );
- assert_eq!((a, b), (2, 7));
- assert_eq!(tool_call_limit(10), 80);
-}
-
-#[tokio::test]
-async fn cancelled_scope_restores_the_callers_tool_limit() {
- let mut scoped = Box::pin(with_tool_call_limit(Some(0), async {
- assert_eq!(tool_call_limit(10), 0);
- std::future::pending::<()>().await;
- }));
- assert!(futures::poll!(&mut scoped).is_pending());
- drop(scoped);
- assert_eq!(tool_call_limit(10), 80);
-}
-
-#[tokio::test]
-async fn scoped_stop_hooks_are_visible_to_run_contexts() {
+#[test]
+fn host_tool_cap_and_stop_hooks_flow_into_the_run_context_and_its_children() {
let hook: Arc<dyn StopHook<crate::agent::cost::TurnCost>> = Arc::new(BudgetStopHook::new(1.0));
- assert!(current_stop_hooks().is_empty());
- with_stop_hooks(vec![Arc::clone(&hook)], async {
- let active = current_stop_hooks();
- assert_eq!(active.len(), 1);
- assert_eq!(active[0].name(), "budget");
- assert_eq!(
- crate::agent::tinyagents::host::OpenHumanRunContext::new()
- .stop_hooks
- .len(),
- 1
- );
- })
- .await;
- assert!(current_stop_hooks().is_empty());
+ let mut hooks = crate::agent::hooks::HookScope::default();
+ hooks.push_stop(Arc::clone(&hook));
+ let env = crate::agent::turn_env::HostTurnEnv {
+ tool_call_limit: Some(3),
+ hooks,
+ ..Default::default()
+ };
+ let context = crate::agent::tinyagents::host::OpenHumanRunContext::new().with_host_env(&env);
+ assert_eq!(context.tool_call_limit, Some(3));
+ assert_eq!(context.stop_hooks.len(), 1);
+ assert_eq!(context.stop_hooks[0].name(), "budget");
+ // Sub-agents inherit both through the explicit child carrier.
+ let child = context.child();
+ assert_eq!(child.tool_call_limit, Some(3));
+ assert_eq!(child.stop_hooks.len(), 1);
+ // A context built without an env carries nothing ambient.
+ let bare = crate::agent::tinyagents::host::OpenHumanRunContext::new();
+ assert_eq!(bare.tool_call_limit, None);
+ assert!(bare.stop_hooks.is_empty());
}
#[tokio::test]
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_runner_tests.rs b/crates/openhuman-core/src/agent/tinyagents/turn_runner_tests.rs
index 6468e830ed..39f5709672 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_runner_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_runner_tests.rs
@@ -117,9 +117,12 @@ async fn scoped_tool_limit_is_honored_by_the_hosted_runner_inner() {
.expect("scripted turn models build");
let (tx, _rx) = tokio::sync::mpsc::channel(8);
- let _ = crate::agent::stop_hooks::with_tool_call_limit(Some(0), async {
+ let mut limited_context = root_context("limited-runner", "/tmp/limited-runner", tx);
+ // The host's explicit cap rides the run context: no tool may run.
+ limited_context.tool_call_limit = Some(0);
+ let _ = async {
run_root_turn_via_hosted_agent(
- root_context("limited-runner", "/tmp/limited-runner", tx),
+ limited_context,
hosted_base(),
"main".to_string(),
models,
@@ -141,7 +144,7 @@ async fn scoped_tool_limit_is_honored_by_the_hosted_runner_inner() {
true,
)
.await
- })
+ }
.await;
assert!(model_calls.load(std::sync::atomic::Ordering::SeqCst) > 0);
@@ -173,13 +176,15 @@ async fn positive_scoped_tool_limit_caps_hosted_runner_inner() {
.expect("scripted turn models build");
let (tx, _rx) = tokio::sync::mpsc::channel(8);
- let outcome = crate::agent::stop_hooks::with_tool_call_limit(Some(1), async {
+ let mut limited_context = root_context(
+ "limited-runner-positive",
+ "/tmp/limited-runner-positive",
+ tx,
+ );
+ limited_context.tool_call_limit = Some(1);
+ let outcome = async {
run_root_turn_via_hosted_agent(
- root_context(
- "limited-runner-positive",
- "/tmp/limited-runner-positive",
- tx,
- ),
+ limited_context,
hosted_base(),
"main".to_string(),
models,
@@ -201,7 +206,7 @@ async fn positive_scoped_tool_limit_caps_hosted_runner_inner() {
true,
)
.await
- })
+ }
.await;
let outcome = outcome.expect("hosted runner succeeds after its permitted tool call");
diff --git a/crates/openhuman-embed/tests/runtime_agents.rs b/crates/openhuman-embed/tests/runtime_agents.rs
index 64f03137a2..46a6b3bc11 100644
--- a/crates/openhuman-embed/tests/runtime_agents.rs
+++ b/crates/openhuman-embed/tests/runtime_agents.rs
@@ -285,15 +285,15 @@ fn one_runtime_hosts_independently_configured_agents() {
.expect("visible turn runs");
let stop_reason = std::sync::Arc::new(std::sync::Mutex::new(None));
- let budgeted_turn = openhuman_core::agent::stop_hooks::with_stop_hooks(
- vec![std::sync::Arc::new(RecordingBudgetStopHook {
+ let budgeted_turn = budget_agent
+ .turn("report a metered response")
+ .stop_hook(std::sync::Arc::new(RecordingBudgetStopHook {
inner: openhuman_core::agent::stop_hooks::BudgetStopHook::new(1.0),
reason: std::sync::Arc::clone(&stop_reason),
- })],
- budget_agent.run("report a metered response"),
- )
- .await
- .expect("budgeted runtime-owned agent turn returns");
+ }))
+ .send()
+ .await
+ .expect("budgeted runtime-owned agent turn returns");
assert_eq!(common::chat_requests(&budget_provider).await.len(), 1);
assert_eq!(
stop_reason.lock().expect("stop reason lock").as_deref(),
diff --git a/crates/openhuman-embed/tests/scoped_hooks.rs b/crates/openhuman-embed/tests/scoped_hooks.rs
index 3af4900a86..33aaf7143c 100644
--- a/crates/openhuman-embed/tests/scoped_hooks.rs
+++ b/crates/openhuman-embed/tests/scoped_hooks.rs
@@ -158,20 +158,18 @@ async fn scenario() {
.unwrap();
let turn_hook = Observer::new("turn-a", &events, None);
let scheduled = openhuman_core::agent::host_agents::resolve("worker-a").unwrap();
- scheduled
- .scope(async {
- assert_eq!(
- openhuman_core::agent::hooks::turn_tool_hooks().len(),
- 2,
- "scheduled turns lost the agent's tool hooks"
- );
- assert_eq!(
- openhuman_core::agent::hooks::turn_post_turn_hooks().len(),
- 2,
- "scheduled turns lost the agent's post-turn hooks"
- );
- })
- .await;
+ // The agent's callbacks are plain data on the scheduled agent; the session
+ // built from it carries them explicitly (asserted by the counts below).
+ assert_eq!(
+ scheduled.hooks.tool_hooks().len(),
+ 1,
+ "scheduled turns lost the agent's tool hooks"
+ );
+ assert_eq!(
+ scheduled.hooks.post_turn_hooks().len(),
+ 1,
+ "scheduled turns lost the agent's post-turn hooks"
+ );
let (first, second) = tokio::join!(
a.turn("a-first")
.tool_hook(turn_hook.clone())