Skip to content

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

fix(tinyagents): route packed tool calls through the loop guard

Steven Enamakelffa9278775e9
diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
index ed9004b72a..81c0a2bb9c 100644
--- a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
@@ -514,9 +514,7 @@ pub(super) fn assemble_turn_harness(
// it keeps enforcing exactly as before, but ALSO compares its per-run token
// accounting against the observe-only crate `BudgetMiddleware` above at end of
// run and logs `[budget_shadow]` parity/divergence.
- harness.push_middleware(Arc::new(middleware::CostBudgetMiddleware::with_shadow(
- shadow_budget_tracker,
- )));
+ harness.push_middleware(Arc::new(BudgetParityObserver::new(shadow_budget_tracker)));
// The context ladder (compression, microcompact, final-call wrap-up,
// artifact contents list, trim), registered here so each step keeps its
// place in the middleware order. See `context_wiring.rs`.
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/packed_tool_route_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/packed_tool_route_tests.rs
index 9bc4c71e71..8d09df2f46 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/packed_tool_route_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/packed_tool_route_tests.rs
@@ -1,5 +1,11 @@
use super::*;
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
+
+use async_trait::async_trait;
+use serde_json::{json, Value};
+use tinyagents_harness::context::{RunConfig, RunContext};
+use tinyagents_harness::middleware::Middleware;
+use tinyinference_llm::tool::ToolCall as TaToolCall;
use tinytools::ToolPolicyEngine;
use tinytools::{PermissionLevel, Tool, ToolResult};
@@ -53,11 +59,18 @@ fn open_session() -> Option<ToolPolicySession> {
Some(session(PermissionLevel::ReadOnly, None))
}
-fn middleware(
- registered: &[&str],
- session: Option<ToolPolicySession>,
-) -> PackedToolRouteMiddleware {
- PackedToolRouteMiddleware::new(registered.iter().map(|n| n.to_string()), session)
+type Mw = PackedToolRouteMiddleware<OpenHumanRunContext, SessionPackRouter>;
+
+fn middleware(registered: &[&str], session: Option<ToolPolicySession>) -> Mw {
+ packed_tool_route_middleware(registered.iter().map(|n| n.to_string()), session)
+}
+
+/// Run `before_tool`; `true` when the call was rewritten.
+fn route(mw: &Mw, call: &mut TaToolCall) -> bool {
+ let before = call.clone();
+ let mut ctx = RunContext::new(RunConfig::new("route-test"), OpenHumanRunContext::new());
+ futures::executor::block_on(mw.before_tool(&mut ctx, &(), call)).unwrap();
+ *call != before
}
#[test]
@@ -65,7 +78,7 @@ fn a_bare_packed_call_becomes_the_use_skill_call_that_reaches_it() {
let mw = middleware(&["use_skill", "shell"], open_session());
let mut call = TaToolCall::new("c1", "skill_registry_search", json!({ "query": "x" }));
- assert!(mw.route(&mut call));
+ assert!(route(&mw, &mut call));
assert_eq!(call.name, "use_skill");
assert_eq!(
call.arguments,
@@ -121,7 +134,7 @@ fn calls_the_crate_should_answer_are_left_alone() {
];
for (mw, mut call, why) in cases {
let before = call.clone();
- assert!(!mw.route(&mut call), "{why}: must not route");
+ assert!(!route(&mw, &mut call), "{why}: must not route");
assert_eq!(call, before, "{why}: call must be untouched");
}
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_loop_guard_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_loop_guard_tests.rs
index 44786cca64..88a79920ec 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_loop_guard_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_loop_guard_tests.rs
@@ -78,52 +78,6 @@ fn compaction_and_truncation_exempt_sets_are_distinct() {
assert!(!is_truncation_exempt("some_other_tool"));
}
-// ── CostBudgetMiddleware ────────────────────────────────────────────────
-
-#[tokio::test]
-async fn cost_budget_is_a_noop_without_a_global_tracker() {
- // No global CostTracker is installed in the unit-test process, so the
- // gate self-disables and the model call proceeds.
- let mw = CostBudgetMiddleware::new();
- let mut req = ModelRequest::new(vec![TaMessage::user("hi")]);
- assert!(mw.before_model(&mut ctx(), &(), &mut req).await.is_ok());
-}
-
-// ── CostBudgetMiddleware shadow (W2-budget-dedupe) ──────────────────────
-
-/// The shadow comparison at `after_agent` logs parity when the crate
-/// `BudgetMiddleware`'s tracker matches the runtime `AgentRun.usage`, and
-/// never fails the run — in both the matching and diverging cases. It also
-/// must be inert (no panic, `Ok`) when no shadow tracker is installed.
-#[tokio::test]
-async fn cost_budget_shadow_after_agent_never_fails_the_run() {
- use tinyinference_llm::usage::Usage;
-
- // No shadow tracker: after_agent is a silent no-op.
- let plain = CostBudgetMiddleware::new();
- let mut run = AgentRun::new();
- run.usage.record(Usage::new(100, 40));
- assert!(plain.after_agent(&mut ctx(), &(), &mut run).await.is_ok());
-
- // Matching tracker (parity): the crate tracker accumulated the same
- // single call's usage the runtime recorded into `run.usage`.
- let tracker = BudgetTracker::new();
- tracker.record(Usage::new(100, 40), Default::default());
- let shadow = CostBudgetMiddleware::with_shadow(tracker.clone());
- let mut run = AgentRun::new();
- run.usage.record(Usage::new(100, 40));
- assert!(shadow.after_agent(&mut ctx(), &(), &mut run).await.is_ok());
-
- // Diverging tracker (crate missed a call): still only logs, never fails.
- let mut diverged_run = AgentRun::new();
- diverged_run.usage.record(Usage::new(100, 40));
- diverged_run.usage.record(Usage::new(10, 5));
- assert!(shadow
- .after_agent(&mut ctx(), &(), &mut diverged_run)
- .await
- .is_ok());
-}
-
#[tokio::test]
async fn repeated_tool_failure_pauses_only_after_the_threshold() {
let handle = SteeringHandle::allow_all();