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
test: add tests for middleware tool output handling
Steven Enamakel6fb74effbcd6
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rsindex 772efaa326..c841589c08 100644--- a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs@@ -48,23 +48,23 @@ fn estimate_text_tokens_charges_each_image_marker_once() {#[tokio::test]async fn unavailable_summarization_is_disclosed_in_the_payload() {- let mw = summarizer_mw(StubSummarizer::ok(SummarizeOutcome::Unavailable(- UnavailableReason::Failed,- )));+ use crate::inference::tokenjuice::module_stub::FAILED_NOTICE;+ let mw = summarizer_mw(StubSummarizer::replying(Err(anyhow::anyhow!("model offline"))));let mut ctx = ctx();let mut result = tool_result("test_tool", "RAW-TOOL-OUTPUT");- mw.after_tool(+ with_module(mw.after_tool(&mut ctx,&(),&invocation("test-1", "test_tool"),&mut result,- )+ )).await+ .0.expect("after_tool should not fail");assert!(- result_text(&result).starts_with(UnavailableReason::Failed.notice()),+ result_text(&result).starts_with(FAILED_NOTICE),"the notice must be a PREFIX — the downstream per-tool cap keeps the \head, so an appended notice is the first thing truncated away; got: {}",result_text(&result)@@ -79,64 +79,64 @@ async fn unavailable_summarization_is_disclosed_in_the_payload() {#[tokio::test]async fn a_payload_that_needed_nothing_is_left_completely_alone() {// The other half of the contract. If every result carried a notice the- // marker would be noise and the model would learn to ignore it.- let mw = summarizer_mw(StubSummarizer::ok(SummarizeOutcome::NotNeeded));+ // marker would be noise and the model would learn to ignore it. Below the+ // summary threshold no call is even prepared.+ let stub = StubSummarizer::replying(Ok("unused".into()));+ let mut mw = summarizer_mw(stub.clone());+ mw.runtime_config = Some(Arc::new(crate::config::Config::default()));let mut ctx = ctx();let mut result = tool_result("test_tool", "RAW-TOOL-OUTPUT");- mw.after_tool(+ let (outcome, requests) = with_module(mw.after_tool(&mut ctx,&(),&invocation("test-2", "test_tool"),&mut result,- )- .await- .expect("after_tool should not fail");+ ))+ .await;+ outcome.expect("after_tool should not fail");assert_eq!(result_text(&result),"RAW-TOOL-OUTPUT","a below-threshold payload must be byte-identical");+ assert!(!stub.was_prepared());+ assert!(+ requests.is_empty(),+ "with compaction off and no summary call there is nothing to ask the module"+ );}#[tokio::test]-async fn a_summarizer_error_is_disclosed_rather_than_swallowed() {- // `Err` used to be discarded by the same `if let Ok(Some(..))` that- // discarded `None`, so a fatal misconfiguration was indistinguishable- // from "nothing to do".- struct ErroringSummarizer;- #[async_trait]- impl PayloadSummarizer for ErroringSummarizer {- async fn maybe_summarize_in_parent(+async fn a_summary_model_that_cannot_prepare_leaves_the_payload_intact() {+ // A host misconfiguration (no parent turn to bind to) must never break+ // the tool call; the module is simply not offered a summary call.+ struct Unpreparable;+ impl PayloadSummarizer for Unpreparable {+ fn prepare(&self,_parent_ctx: &RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,- _tool_name: &str,- _parent_task_hint: Option<&str>,- _raw: &str,- ) -> anyhow::Result<SummarizeOutcome> {+ ) -> anyhow::Result<crate::inference::tokenjuice::generate::PreparedGenerate> {Err(anyhow::anyhow!("summarizer misconfigured"))}}- let mw = summarizer_mw(Arc::new(ErroringSummarizer));+ let mw = summarizer_mw(Arc::new(Unpreparable));let mut ctx = ctx();let mut result = tool_result("test_tool", "RAW-TOOL-OUTPUT");- mw.after_tool(+ with_module(mw.after_tool(&mut ctx,&(),&invocation("test-3", "test_tool"),&mut result,- )+ )).await+ .0.expect("a summarizer error must never break the tool call");- assert!(- result_text(&result).starts_with(UnavailableReason::Failed.notice()),- "an errored summarizer must be disclosed too; got: {}",- result_text(&result)- );+ assert_eq!(result_text(&result), "RAW-TOOL-OUTPUT");}#[tokio::test]@@ -570,33 +570,22 @@ async fn a_tool_that_caps_itself_is_never_sent_to_the_summarizer() {replay: Default::default(),}),);- let stub = StubSummarizer::ok(SummarizeOutcome::Unavailable(UnavailableReason::Failed));- let mw = ToolOutputMiddleware {- // Large enough that the shared backstop never fires, so this- // observes the per-tool cap alone.- budget_bytes: 10_000_000,- payload_summarizer: Some(stub.clone()),- artifact_store: None,- tokenjuice_compaction_enabled: false,- tokenjuice_compression: crate::inference::tokenjuice::AgentTokenjuiceCompression::Off,- runtime_config: None,- tool_policies,- artifact_reads: Default::default(),- focus_by_call: Default::default(),- };+ let stub = StubSummarizer::replying(Ok("note".into()));+ let mut mw = summarizer_mw(stub.clone());+ mw.tool_policies = tool_policies;let mut result = tool_result("terse", &"payload ".repeat(200));- mw.after_tool(+ let (outcome, requests) = with_module(mw.after_tool(&mut ctx(),&(),&invocation("terse-notice", "terse"),&mut result,- )- .await- .unwrap();+ ))+ .await;+ outcome.unwrap();assert!(- !stub.was_called(),+ !stub.was_prepared() && requests.is_empty(),"a tool with its own cap must not be dispatched to the summarizer");assert!(@@ -606,6 +595,48 @@ async fn a_tool_that_caps_itself_is_never_sent_to_the_summarizer() {);}+/// The exception to the rule above: a caller that said what it needs from a+/// self-capped tool (`web_fetch` with `summary_focus`) gets a summary written+/// for that, because paging the raw page cannot answer the question.+#[tokio::test]+async fn a_tool_that_caps_itself_is_summarized_when_the_caller_gives_a_focus() {+ let mut tool_policies = HashMap::new();+ tool_policies.insert(+ "terse".to_string(),+ TaToolPolicy::classified().with_runtime(ToolRuntime {+ timeout_ms: None,+ timeout: ToolTimeout::Inherit,+ max_retries: None,+ idempotent: false,+ cancelable: true,+ sandbox: tinytools::SandboxMode::Inherit,+ max_result_bytes: Some(64),+ streaming: false,+ replay: Default::default(),+ }),+ );+ let stub = StubSummarizer::replying(Ok("focused".into()));+ let mut mw = summarizer_mw(stub.clone());+ mw.tool_policies = tool_policies;+ let mut call = TaToolCall::new("terse-focus", "terse", json!({"summary_focus": "errors"}));+ let mut ctx = ctx();+ mw.before_tool(&mut ctx, &(), &mut call).await.unwrap();++ let mut result = tool_result("terse", &"payload ".repeat(200));+ let (outcome, requests) = with_module(mw.after_tool(+ &mut ctx,+ &(),+ &invocation("terse-focus", "terse"),+ &mut result,+ ))+ .await;+ outcome.unwrap();++ assert!(stub.was_prepared());+ assert_eq!(requests[0].focus.as_deref(), Some("errors"));+ assert!(result_text(&result).contains("focused"));+}+#[tokio::test]async fn tool_output_honors_a_tools_own_cap() {let mut tool_policies = HashMap::new();