Skip to content

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

refactor(middleware): consolidate time-note installation into a single function

Steven Enamakel1b27d30345ae
diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
index d11bdb7002..c493819c48 100644
--- a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
@@ -9,7 +9,6 @@ use tinyagents_harness::middleware::{
plan_mode_middleware, ApprovalGateMiddleware, ArgRecoveryMiddleware, BudgetLimits,
BudgetMiddleware, ContextCompressionMiddleware, PromptCacheGuardMiddleware,
RepeatProgressMiddleware, RunModeHandle, ToolPolicyMiddleware as TaToolPolicyMiddleware,
- TurnClockMiddleware,
};
use tinyagents_harness::runtime::AgentHarness;
use tinyagents_harness::steering::SteeringHandle;
@@ -358,14 +357,8 @@ pub(super) fn assemble_turn_harness(
.with_cleared_placeholder(crate::agent::context::CLEARED_PLACEHOLDER),
)
});
- // Time awareness (#6953): turn-budget notes and clamped-shell-timeout notes.
- // Pushed before the repeat guard so their `after_tool` runs last (notes
- // survive output caps and stay out of the guard's fingerprint).
- let wall_clock = harness.policy().limits.max_wall_clock_ms;
- let wall_clock = wall_clock.map(std::time::Duration::from_millis);
- harness.push_middleware(Arc::new(TurnClockMiddleware::new(wall_clock)));
- let shell_turn_budget = middleware::ShellTurnBudget::new(wall_clock);
- harness.push_middleware(Arc::new(shell_turn_budget.notes()));
+ // Time awareness (#6953); before the repeat guard so its notes land last.
+ let shell_turn_budget = middleware::install_time_notes(&mut harness);
if let Some(mw) = &repeat_progress {
harness.push_middleware(mw.clone());
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware.rs b/crates/openhuman-core/src/agent/tinyagents/middleware.rs
index 8c5cfa0bad..709d14820d 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware.rs
@@ -52,7 +52,7 @@ pub use memory_protocol::memory_protocol_middleware;
pub(crate) use packed_tool_route::PackedToolRouteMiddleware;
pub(crate) use repeated_failure::RepeatedToolFailureMiddleware;
pub(crate) use research_budget::ResearchBudgetMiddleware;
-pub(crate) use shell_turn_budget::ShellTurnBudget;
+pub(crate) use shell_turn_budget::install_time_notes;
pub(crate) use tool_exposure::OpenHumanToolExposureShadowMiddleware;
pub(crate) use tool_outcome_capture::ToolOutcomeCaptureMiddleware;
pub(crate) use tool_policy::ToolPolicyMiddleware;
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/shell_turn_budget.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/shell_turn_budget.rs
index ebd3618ade..0be303c2b3 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/shell_turn_budget.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/shell_turn_budget.rs
@@ -29,7 +29,12 @@ use std::time::Duration;
use async_trait::async_trait;
use tinyagents_harness::context::RunContext;
use tinyagents_harness::error::Result as TaResult;
-use tinyagents_harness::middleware::{Middleware, ToolInvocationIdentity, TurnClock};
+use tinyagents_harness::middleware::{
+ Middleware, ToolInvocationIdentity, TurnClock, TurnClockMiddleware,
+};
+use tinyagents_harness::runtime::AgentHarness;
+
+use crate::agent::tinyagents::host::OpenHumanRunContext;
use tinyinference_llm::tool::ToolCall;
use tinytools::ToolResult;
@@ -206,6 +211,33 @@ impl<C: Send + Sync> Middleware<(), C> for ShellTimeoutNoteMiddleware {
}
}
+/// Install the turn's time notes and return the shell budget whose
+/// [`clamp`](ShellTurnBudget::clamp) the caller pushes after argument recovery.
+///
+/// Pushes [`TurnClockMiddleware`] (`[turn budget: …]` once per tenth of the
+/// budget past half) and [`ShellTurnBudget::notes`]. Call it before the
+/// repeat-progress guard: `after_tool` runs in reverse registration order, so
+/// these notes are appended after every output cap (they survive truncation)
+/// and after the guard has fingerprinted the result (a changing note does not
+/// make two identical results look different).
+pub(crate) fn install_time_notes(
+ harness: &mut AgentHarness<(), OpenHumanRunContext>,
+) -> Arc<ShellTurnBudget> {
+ let budget = harness
+ .policy()
+ .limits
+ .max_wall_clock_ms
+ .map(Duration::from_millis);
+ tracing::debug!(
+ budget_ms = ?budget.map(|b| b.as_millis() as u64),
+ "[tinyagents::mw] installing turn clock and shell turn-budget notes"
+ );
+ harness.push_middleware(Arc::new(TurnClockMiddleware::new(budget)));
+ let shell = ShellTurnBudget::new(budget);
+ harness.push_middleware(Arc::new(shell.notes()));
+ shell
+}
+
#[cfg(test)]
#[path = "shell_turn_budget_tests.rs"]
mod tests;