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
feat(agent): add stop hooks to tinyagents turn runner
Steven Enamakel3ed26b1b8c8d
diff --git a/crates/openhuman-core/src/agent/stop_hooks.rs b/crates/openhuman-core/src/agent/stop_hooks.rsindex 5e0b8782fe..5e5734f8bb 100644--- a/crates/openhuman-core/src/agent/stop_hooks.rs+++ b/crates/openhuman-core/src/agent/stop_hooks.rs@@ -10,12 +10,14 @@//! The `tinyagents` adapter installs a `StopHookMiddleware`//! ([`crate::agent::tinyagents::stop_hooks`]) that fires each hook after//! every model call; a hook returning [`StopDecision::Stop`] pauses the run-//! gracefully (via the steering handle) before the next provider call. The+//! gracefully (via the steering handle) before the next provider call. Hooks+//! arrive on the turn's explicit run context+//! (`OpenHumanRunContext::stop_hooks`). The//! goal-budget hook ([`crate::agent::goals::runtime::GoalBudgetStopHook`]) is//! the live implementation.//!-//! The per-turn tool budget ([`with_tool_call_limit`]) also lives here, on a-//! task-local.+//! The per-turn tool budget ([`tool_call_limit`]) also lives here; a host+//! narrows it through `HostTurnEnv::tool_call_limit`.pub use crate::agent::cost::TurnCost;use async_trait::async_trait;@@ -23,25 +25,6 @@ use std::sync::Arc;use tinyagents_harness::middleware::{StopDecision, StopHook, TurnState};-tokio::task_local! {- static CURRENT_STOP_HOOKS: Vec<Arc<dyn StopHook<TurnCost>>>;-}--/// Clone the stop hooks active in the current task, if any.-pub fn current_stop_hooks() -> Vec<Arc<dyn StopHook<TurnCost>>> {- CURRENT_STOP_HOOKS- .try_with(Clone::clone)- .unwrap_or_default()-}--/// Scope a future with per-turn stop hooks.-pub async fn with_stop_hooks<F: std::future::Future>(- hooks: Vec<Arc<dyn StopHook<TurnCost>>>,- future: F,-) -> F::Output {- CURRENT_STOP_HOOKS.scope(hooks, future).await-}-/// Stop a turn when its cumulative cost reaches the configured USD cap.#[derive(Debug, Clone, Copy)]pub struct BudgetStopHook {@@ -81,34 +64,12 @@ impl StopHook<TurnCost> for BudgetStopHook {}}-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,- }-}--pub(crate) fn tool_call_limit(max_iterations: usize) -> usize {+/// The real tool invocation budget for one turn: the default derived from the+/// iteration cap, narrowed by the host's explicit `cap`+/// (`HostTurnEnv::tool_call_limit`) when it set one. A host can only narrow it.+pub(crate) fn tool_call_limit(max_iterations: usize, cap: Option<usize>) -> usize {let default = max_iterations.saturating_mul(8).max(8);- CURRENT_TOOL_CALL_LIMIT- .try_with(|n| default.min(*n))- .unwrap_or(default)+ cap.map_or(default, |cap| default.min(cap))}#[cfg(test)]diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rsindex e20c218ca0..2336c32d73 100644--- a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs+++ b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs@@ -70,6 +70,8 @@ pub(super) fn assemble_turn_harness(// The turn's agent/turn tool callbacks (`OpenHumanRunContext::tool_hooks`),// registered after the runtime's global ones.scoped_tool_hooks: Vec<Arc<dyn crate::agent::hooks::ToolHook>>,+ // The host's cap on real tool invocations (`OpenHumanRunContext::tool_call_limit`).+ tool_call_cap: Option<usize>,tool_policy: Option<ToolPolicyEnforcement>,required_capabilities: Option<CapabilitySet>,deterministic_cacheable: bool,@@ -121,6 +123,7 @@ pub(super) fn assemble_turn_harness(max_iterations,deterministic_cacheable,turn_models.is_local(),+ tool_call_cap,);let route_fallback = routes::route_fallback_policy(model);policy.fallback = route_fallback.clone();diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rsindex 298b9c3874..177af67b42 100644--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs@@ -117,7 +117,7 @@ fn env_overrides() -> TurnPolicyOverrides {/// Build the harness [`RunPolicy`] for an openhuman turn (hosted provider).pub(crate) fn run_policy_for(max_iterations: usize, response_cache_enabled: bool) -> RunPolicy {- run_policy_for_provider(max_iterations, response_cache_enabled, false)+ run_policy_for_provider(max_iterations, response_cache_enabled, false, None)}/// [`run_policy_for`] with the turn's provider locality: a local / self-hosted@@ -130,10 +130,11 @@ pub(crate) fn run_policy_for_provider(max_iterations: usize,response_cache_enabled: bool,local_provider: bool,+ tool_call_cap: Option<usize>,) -> RunPolicy {run_policy(&TurnPolicyOptions {max_model_calls: max_iterations,- max_tool_calls: crate::agent::stop_hooks::tool_call_limit(max_iterations),+ max_tool_calls: crate::agent::stop_hooks::tool_call_limit(max_iterations, tool_call_cap),max_depth: MAX_SPAWN_DEPTH,local_provider,response_cache_enabled,diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs b/crates/openhuman-core/src/agent/tinyagents/turn_runner.rsindex 726e46e194..5857ca66b6 100644--- a/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs+++ b/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs@@ -278,6 +278,7 @@ pub(super) async fn run_turn_via_tinyagents_body(context_mw,run_context.stop_hooks.clone(),run_context.tool_hooks.clone(),+ run_context.tool_call_limit,tool_policy.clone(),routes::turn_required_capabilities(model),deterministic_cacheable,@@ -359,7 +360,10 @@ pub(super) async fn run_turn_via_tinyagents_body("openhuman-agent-turn"}).with_max_model_calls(max_iterations)- .with_max_tool_calls(crate::agent::stop_hooks::tool_call_limit(max_iterations))+ .with_max_tool_calls(crate::agent::stop_hooks::tool_call_limit(+ max_iterations,+ run_context.tool_call_limit,+ )).with_max_depth(super::budget::depth(&run_context)).with_tag("openhuman").with_tag(if subagent_scope.is_some() {