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): stop turn when budget is exhausted
Steven Enamakel76dbc7e99c13
diff --git a/crates/openhuman-core/src/agent/session_host/turn/graph.rs b/crates/openhuman-core/src/agent/session_host/turn/graph.rsindex 565b9a491d..1802b23ab7 100644--- a/crates/openhuman-core/src/agent/session_host/turn/graph.rs+++ b/crates/openhuman-core/src/agent/session_host/turn/graph.rs@@ -29,9 +29,7 @@ use anyhow::Result;use tokio::sync::mpsc::Sender;use crate::agent::progress::AgentProgress;-use crate::agent::tinyagents::{- TinyagentsTurnOutcome, TurnContextMiddleware,-};+use crate::agent::tinyagents::{TinyagentsTurnOutcome, TurnContextMiddleware, TurnSpec};use crate::inference::provider::AGENT_TURN_MAX_OUTPUT_TOKENS;use tinyagents_definition::SandboxMode;use tinyagents_harness::run_queue::RunQueue;@@ -126,68 +124,49 @@ pub(crate) async fn run_chat_turn_graph(graph: ChatTurnGraph) -> Result<Tinyagenrun_context.sandbox_mode = Some(graph.sandbox_mode);run_context.thread_id = graph.thread_id;- // Bound by name rather than passed as bare `true` literals.- // `run_root_turn_via_hosted_agent` takes eighteen positional arguments,- // and these two `bool`s sat eleven lines apart in the call below. While- // investigating #6541 that pair produced four separate misreadings —- // in both directions, including one that briefly discarded a correct- // finding — because aligning an argument against the signature by- // counting is the only way to tell which flag a `true` belongs to.- // A binding puts the name inside the expression, so a future reordering- // is visible here and not only in the signature.- //// Pause gracefully at the model-call cap so the turn emits a resumable// checkpoint instead of erroring or returning a dangling tool cycle.- let pause_at_cap = true;// The runtime's durable `after_commit` callback emits the terminal// event after any driver-grounded close has become part of the- // candidate history. A seam-level event would arrive before that- // close and duplicate the lifecycle projection.- let defer_turn_completed_to_caller = true;-- run_root_turn_via_hosted_agent(+ // candidate history, so the turn itself must not: a seam-level event+ // would arrive before that close and duplicate the lifecycle projection.+ TurnSpec {run_context,- hosted_base,- graph.agent_id,- graph.turn_models,+ turn_models: graph.turn_models,provider_id,- &graph.model,- graph.messages,+ model: &graph.model,+ history: graph.messages,// Durable set first, synthesised second — the order `tool_specs` and// `OpenHumanSessionHost::all_tool_refs` use, so the name a spec was advertised// under resolves to the same instance here. The two sets are// disjoint by construction (`builder::drop_synthesized_name_collisions`),// so the order never decides a collision; it only keeps every// surface enumerating the tools in one sequence.- vec![graph.tools, graph.synthesized_tools],- visible_tool_names,- graph.max_iterations,- graph.context_window,+ tool_sets: vec![graph.tools, graph.synthesized_tools],+ allowed: visible_tool_names,+ max_iterations: graph.max_iterations,+ subagent_scope: None,+ context_window: graph.context_window,// Mid-flight steering from the session's run queue.- graph.run_queue,- // `ask_user_clarification` pauses this turn: the seam returns the+ run_queue: graph.run_queue,+ // `ask_user_clarification` pauses this turn: the turn returns the// question as `outcome.text`, the caller ends the turn on it, and the// user's next message is the answer. No resume plumbing is needed at// the top level — unlike a delegated child, a chat turn's natural// continuation IS the next user message.- //- // This was `&[]` with a comment claiming the chat turn "surfaces- // clarifying questions inline". It does not, and nothing else did- // either: the tool's output went back to the model as a successful- // result, so the model read its own question as answered and carried- // on. Users watched an "Ask User Clarification" step succeed without- // ever being asked anything.- &["ask_user_clarification"],- pause_at_cap,+ early_exit_tools: &["ask_user_clarification"],+ pause_at_cap: true,// Bound the main agent's per-call output (legacy parity — the engine// capped every turn at `AGENT_TURN_MAX_OUTPUT_TOKENS`).- Some(AGENT_TURN_MAX_OUTPUT_TOKENS),+ max_output_tokens: Some(AGENT_TURN_MAX_OUTPUT_TOKENS),// Context middlewares sourced from the session's ContextManager.- graph.context_mw,+ context_mw: graph.context_mw,// Builder-configured tool policy enforcement (session chat path).- graph.tool_policy,- defer_turn_completed_to_caller,- )+ tool_policy: graph.tool_policy,+ deterministic_cacheable: false,+ defer_turn_completed_to_caller: true,+ }+ .run_hosted(hosted_base, graph.agent_id).await}.awaitdiff --git a/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rs b/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rsindex 018e7aa91c..6de49ecfc7 100644--- a/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rs+++ b/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rs@@ -163,28 +163,29 @@ async fn budget_stop_hook_pauses_the_hosted_turn_inner() {reason: Arc::clone(&stop_reason),}));- let outcome = run_root_turn_via_hosted_agent(- context,- hosted_base(),- "main".to_string(),- models,- "test".to_string(),- "claude-sonnet-4-6",- root_messages("budget-stop"),- vec![Arc::new(vec![+ let outcome = TurnSpec {+ run_context: context,+ turn_models: models,+ provider_id: "test".to_string(),+ model: "claude-sonnet-4-6",+ history: root_messages("budget-stop"),+ tool_sets: vec![Arc::new(vec![Box::new(LimitedTool(tool_calls.clone())) as Box<dyn Tool>])],- None,- 3,- None,- None,- &[],- false,- None,- TurnContextMiddleware::default(),- None,- true,- )+ allowed: None,+ max_iterations: 3,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: false,+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(hosted_base(), "main".to_string()).await.expect("budget pause returns the partial turn");@@ -234,28 +235,29 @@ async fn deadline_wind_down_pauses_the_hosted_turn_inner() {std::time::Duration::from_secs(900),));- let outcome = run_root_turn_via_hosted_agent(- context,- hosted_base(),- "main".to_string(),- models,- "test".to_string(),- "claude-sonnet-4-6",- root_messages("deadline-wind-down"),- vec![Arc::new(vec![+ let outcome = TurnSpec {+ run_context: context,+ turn_models: models,+ provider_id: "test".to_string(),+ model: "claude-sonnet-4-6",+ history: root_messages("deadline-wind-down"),+ tool_sets: vec![Arc::new(vec![Box::new(LimitedTool(tool_calls.clone())) as Box<dyn Tool>])],- None,- 50,- None,- None,- &[],- true,- None,- TurnContextMiddleware::default(),- None,- true,- )+ allowed: None,+ max_iterations: 50,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: true,+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(hosted_base(), "main".to_string()).await.expect("a wound-down turn returns its partial work, not an error");@@ -305,28 +307,29 @@ fn a_turn_before_its_wind_down_point_runs_to_its_final_answer() {std::time::Duration::from_secs(900),),);- let outcome = run_root_turn_via_hosted_agent(- context,- hosted_base(),- "main".to_string(),- models,- "test".to_string(),- "claude-sonnet-4-6",- root_messages("deadline-fresh"),- vec![Arc::new(vec![+ let outcome = TurnSpec {+ run_context: context,+ turn_models: models,+ provider_id: "test".to_string(),+ model: "claude-sonnet-4-6",+ history: root_messages("deadline-fresh"),+ tool_sets: vec![Arc::new(vec![Box::new(LimitedTool(tool_calls.clone())) as Box<dyn Tool>])],- None,- 50,- None,- None,- &[],- true,- None,- TurnContextMiddleware::default(),- None,- true,- )+ allowed: None,+ max_iterations: 50,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: true,+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(hosted_base(), "main".to_string()).await.expect("turn completes");assert_eq!(model_calls.load(std::sync::atomic::Ordering::SeqCst), 2);@@ -371,28 +374,29 @@ async fn golden_turn(label: &str, stop: GoldenStop) -> serde_json::Value {}}let sidecar = context.session_sidecar.clone();- let outcome = run_root_turn_via_hosted_agent(- context,- hosted_base(),- "main".to_string(),- models,- "test".to_string(),- "claude-sonnet-4-6",- root_messages(label),- vec![Arc::new(vec![+ let outcome = TurnSpec {+ run_context: context,+ turn_models: models,+ provider_id: "test".to_string(),+ model: "claude-sonnet-4-6",+ history: root_messages(label),+ tool_sets: vec![Arc::new(vec![Box::new(LimitedTool(tool_calls.clone())) as Box<dyn Tool>])],- None,- 50,- None,- None,- &[],- matches!(stop, GoldenStop::Finishes | GoldenStop::DeadlineWindDown),- None,- TurnContextMiddleware::default(),- None,- true,- )+ allowed: None,+ max_iterations: 50,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: matches!(stop, GoldenStop::Finishes | GoldenStop::DeadlineWindDown),+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(hosted_base(), "main".to_string()).await.expect("scripted turn returns its outcome");let wind_down = sidecardiff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rsindex 39f5709672..bc3c5f91e7 100644--- a/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rs+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec_tests.rs@@ -121,28 +121,29 @@ async fn scoped_tool_limit_is_honored_by_the_hosted_runner_inner() {// 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(- limited_context,- hosted_base(),- "main".to_string(),- models,- "test".to_string(),- "root-test-model",- root_messages("limited-runner"),- vec![Arc::new(vec![+ TurnSpec {+ run_context: limited_context,+ turn_models: models,+ provider_id: "test".to_string(),+ model: "root-test-model",+ history: root_messages("limited-runner"),+ tool_sets: vec![Arc::new(vec![Box::new(LimitedTool(tool_calls.clone())) as Box<dyn Tool>])],- None,- 2,- None,- None,- &[],- false,- None,- TurnContextMiddleware::default(),- None,- true,- )+ allowed: None,+ max_iterations: 2,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: false,+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(hosted_base(), "main".to_string()).await}.await;@@ -183,28 +184,29 @@ async fn positive_scoped_tool_limit_caps_hosted_runner_inner() {);limited_context.tool_call_limit = Some(1);let outcome = async {- run_root_turn_via_hosted_agent(- limited_context,- hosted_base(),- "main".to_string(),- models,- "test".to_string(),- "root-test-model",- root_messages("limited-runner-positive"),- vec![Arc::new(vec![+ TurnSpec {+ run_context: limited_context,+ turn_models: models,+ provider_id: "test".to_string(),+ model: "root-test-model",+ history: root_messages("limited-runner-positive"),+ tool_sets: vec![Arc::new(vec![Box::new(LimitedTool(tool_calls.clone())) as Box<dyn Tool>])],- None,- 3,- None,- None,- &[],- false,- None,- TurnContextMiddleware::default(),- None,- true,- )+ allowed: None,+ max_iterations: 3,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: false,+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(hosted_base(), "main".to_string()).await}.await;@@ -249,26 +251,27 @@ async fn run_root_with(reply: &str,messages: Vec<TranscriptMessage>,) -> anyhow::Result<TinyagentsTurnOutcome> {- run_root_turn_via_hosted_agent(- context,- base,- "main".to_string(),- root_models(reply),- "test".to_string(),- "root-test-model",- messages,- vec![Arc::new(Vec::new())],- Some(Default::default()),- 2,- None,- None,- &[],- false,- None,- TurnContextMiddleware::default(),- None,- true,- )+ TurnSpec {+ run_context: context,+ turn_models: root_models(reply),+ provider_id: "test".to_string(),+ model: "root-test-model",+ history: messages,+ tool_sets: vec![Arc::new(Vec::new())],+ allowed: Some(Default::default()),+ max_iterations: 2,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: false,+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(base, "main".to_string()).await}@@ -506,26 +509,27 @@ async fn an_in_stream_provider_failure_is_re_surfaced_past_the_hosted_sanitizer(.build("root-test-model", 0.0, None, None).expect("turn models build");let (tx, _rx) = tokio::sync::mpsc::channel(8);- let error = run_root_turn_via_hosted_agent(- root_context("in-stream", "/tmp/in-stream", tx),- hosted_base(),- "main".to_string(),- models,- "test".to_string(),- "root-test-model",- root_messages("in-stream"),- vec![Arc::new(Vec::new())],- Some(Default::default()),- 2,- None,- None,- &[],- false,- None,- TurnContextMiddleware::default(),- None,- true,- )+ let error = TurnSpec {+ run_context: root_context("in-stream", "/tmp/in-stream", tx),+ turn_models: models,+ provider_id: "test".to_string(),+ model: "root-test-model",+ history: root_messages("in-stream"),+ tool_sets: vec![Arc::new(Vec::new())],+ allowed: Some(Default::default()),+ max_iterations: 2,+ context_window: None,+ run_queue: None,+ early_exit_tools: &[],+ pause_at_cap: false,+ max_output_tokens: None,+ context_mw: TurnContextMiddleware::default(),+ tool_policy: None,+ defer_turn_completed_to_caller: true,+ subagent_scope: None,+ deterministic_cacheable: false,+ }+ .run_hosted(hosted_base(), "main".to_string()).await.expect_err("the provider rejected the request");let text = error.to_string();