The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
Allow embedders to narrow tool-call budgets per turn
Jarno de Vries912b26c4c1a3
diff --git a/crates/openhuman-core/src/agent/README.md b/crates/openhuman-core/src/agent/README.mdindex 38f1c69ec4..a97573ee04 100644--- a/crates/openhuman-core/src/agent/README.md+++ b/crates/openhuman-core/src/agent/README.md@@ -85,3 +85,18 @@ Flat files: `bus.rs` (`agent.run_turn` native request handler), `cost.rs` (`pub(- [gitbooks/developing/architecture/agent-harness.md](../../../../gitbooks/developing/architecture/agent-harness.md)- [gitbooks/developing/agent-observability.md](../../../../gitbooks/developing/agent-observability.md)++### Scoped tool-call budgets for embedders++`stop_hooks::with_tool_call_limit(Some(n), turn)` narrows the real TinyAgents+invocation budget for one awaited turn without changing the agent's persistent+configuration. Zero permits no tool invocations. The adapter applies the limit+to both run policy and run configuration, including parallel calls counted by+TinyAgents. `with_stop_hooks_and_tool_limit` combines it with stop hooks.++Nested scopes take the smaller limit; `None` preserves an enclosing limit.+Exiting or dropping the future restores the caller's scope, and concurrent+turns do not share limits. This bounds calls within each run, not a shared+aggregate across child runs. Task-local values do not automatically propagate+through `tokio::spawn`; callers creating a separate task must scope that turn+explicitly. Without a limit, existing iteration-derived limits are unchanged.diff --git a/crates/openhuman-core/src/agent/stop_hooks.rs b/crates/openhuman-core/src/agent/stop_hooks.rsindex 386588a943..b28463f04f 100644--- a/crates/openhuman-core/src/agent/stop_hooks.rs+++ b/crates/openhuman-core/src/agent/stop_hooks.rs@@ -87,6 +87,45 @@ whereCURRENT_STOP_HOOKS.scope(hooks, future).await}+tokio::task_local! {+ static CURRENT_TOOL_CALL_LIMIT: usize;+}++/// Narrow the real tool invocation budget for one turn, including parallel calls.+/// Nested scopes cannot widen their parent's budget. The scope resets on exit.+pub async fn with_tool_call_limit<F: std::future::Future>(+ limit: Option<usize>,+ future: F,+) -> F::Output {+ let inherited = CURRENT_TOOL_CALL_LIMIT.try_with(|n| *n).ok();+ tracing::debug!(+ limit,+ inherited,+ "[tinyagents] installing per-turn tool budget"+ );+ match (limit, inherited) {+ (Some(a), Some(b)) => CURRENT_TOOL_CALL_LIMIT.scope(a.min(b), future).await,+ (Some(n), None) | (None, Some(n)) => CURRENT_TOOL_CALL_LIMIT.scope(n, future).await,+ (None, None) => future.await,+ }+}++/// Install hook policy and a tool budget around the same turn.+pub async fn with_stop_hooks_and_tool_limit<F: std::future::Future>(+ hooks: Vec<Arc<dyn StopHook>>,+ limit: Option<usize>,+ future: F,+) -> F::Output {+ with_tool_call_limit(limit, with_stop_hooks(hooks, future)).await+}++pub(crate) fn tool_call_limit(max_iterations: usize) -> usize {+ let default = max_iterations.saturating_mul(8).max(8);+ CURRENT_TOOL_CALL_LIMIT+ .try_with(|n| default.min(*n))+ .unwrap_or(default)+}+// ─────────────────────────────────────────────────────────────────────────────// Built-in hooks// ─────────────────────────────────────────────────────────────────────────────diff --git a/crates/openhuman-core/src/agent/stop_hooks_tests.rs b/crates/openhuman-core/src/agent/stop_hooks_tests.rsindex 6f6ce3ea77..e389f7b4f8 100644--- a/crates/openhuman-core/src/agent/stop_hooks_tests.rs+++ b/crates/openhuman-core/src/agent/stop_hooks_tests.rs@@ -110,3 +110,57 @@ async fn with_stop_hooks_installs_visible_within_scope() {.await;assert!(current_stop_hooks().is_empty());}++#[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;+}++#[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);+}diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rsindex d9c5743140..ea42a51aa5 100644--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs@@ -136,7 +136,7 @@ pub(super) fn parse_model_call_wall_clock_ms(env_value: Option<&str>) -> Option<pub(crate) fn run_policy_for(max_iterations: usize, response_cache_enabled: bool) -> RunPolicy {let mut policy = RunPolicy::default();policy.limits.max_model_calls = max_iterations;- policy.limits.max_tool_calls = max_iterations.saturating_mul(8).max(8);+ policy.limits.max_tool_calls = crate::agent::stop_hooks::tool_call_limit(max_iterations);policy.limits.max_depth = MAX_SPAWN_DEPTH;// Wall-clock ceiling for the whole turn (issue #4746). The harness bounds// every individual model AND tool call by the run's *remaining* wall-clock@@ -269,3 +269,7 @@ pub(crate) fn is_subagent_spawn_or_delegate_tool(name: &str) -> bool {|| name == "agent_prepare_context"|| name == "spawn_worker_thread"}++#[cfg(test)]+#[path = "turn_policy_budget_tests.rs"]+mod budget_tests;diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy_budget_tests.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy_budget_tests.rsnew file mode 100644index 0000000000..88ffac8157--- /dev/null+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy_budget_tests.rs@@ -0,0 +1,12 @@+use super::*;++#[tokio::test]+async fn per_turn_tool_limit_reaches_the_execution_policy() {+ crate::agent::stop_hooks::with_tool_call_limit(Some(2), async {+ let policy = run_policy_for(10, false);+ assert_eq!(policy.limits.max_tool_calls, 2);+ assert_eq!(policy.limits.max_model_calls, 10);+ })+ .await;+ assert_eq!(run_policy_for(10, false).limits.max_tool_calls, 80);+}diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs b/crates/openhuman-core/src/agent/tinyagents/turn_runner.rsindex dd0594db7c..ef181c6cf3 100644--- a/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs+++ b/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs@@ -24,10 +24,10 @@ use tinyagents_registry::DiagnosticSeverity;use tokio::sync::mpsc::Sender;use crate::agent::harness::tool_result_artifacts::TINYAGENTS_TOOL_RESULT_ARTIFACT_STORE;-use crate::agent::harness::{run_queue::RunQueue, MAX_SPAWN_DEPTH};+use crate::agent::harness::{MAX_SPAWN_DEPTH, run_queue::RunQueue};use crate::agent::messages::ChatMessage;use crate::agent::progress::AgentProgress;-use crate::agent::tinyagents::harness_assembly::{assemble_turn_harness, AssembledTurnHarness};+use crate::agent::tinyagents::harness_assembly::{AssembledTurnHarness, assemble_turn_harness};use crate::agent::tinyagents::middleware::TurnContextMiddleware;use crate::agent::tinyagents::observability::{CapPauser, OpenhumanEventBridge, SubagentScope};use crate::agent::tinyagents::run_cancellation_context::with_run_cancellation;@@ -83,7 +83,7 @@ pub(crate) async fn run_turn_via_tinyagents(// tool calls (the loop also stops when the model stops requesting tools).let config = RunConfig::new("agent_turn").with_max_model_calls(max_iterations)- .with_max_tool_calls(max_iterations.saturating_mul(8).max(8))+ .with_max_tool_calls(crate::agent::stop_hooks::tool_call_limit(max_iterations)).with_max_depth(MAX_SPAWN_DEPTH).with_tag("openhuman").with_tag("scope:root")@@ -318,7 +318,7 @@ pub(crate) async fn run_turn_via_tinyagents_shared(let mut config = RunConfig::new("agent_turn").with_max_model_calls(max_iterations)- .with_max_tool_calls(max_iterations.saturating_mul(8).max(8))+ .with_max_tool_calls(crate::agent::stop_hooks::tool_call_limit(max_iterations)).with_max_depth(MAX_SPAWN_DEPTH).with_tag("openhuman").with_tag(if subagent_scope.is_some() {