Skip to content

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

refactor(agent): inline stop-hook middleware installation

Steven Enamakel7e347d5ef84a
diff --git a/crates/openhuman-core/src/agent/goals/runtime.rs b/crates/openhuman-core/src/agent/goals/runtime.rs
index 145fe30a17..4d14a523f4 100644
--- a/crates/openhuman-core/src/agent/goals/runtime.rs
+++ b/crates/openhuman-core/src/agent/goals/runtime.rs
@@ -1,5 +1,4 @@
-//! OpenHuman runtime adapters for tinyagents thread-goal accounting and the
-//! host-specific mid-turn budget stop hook.
+//! OpenHuman accounting adapters for tinyagents thread goals.
//!
//! These are the pieces that make a stored goal actually steer the agent
//! (Codex parity):
@@ -17,16 +16,13 @@
//! an autonomous run to a small, deterministic overshoot past the ceiling
//! rather than a hard cut at the exact accounting point.
-use std::path::{Path, PathBuf};
-
-use async_trait::async_trait;
+use std::path::Path;
use tinyagents_graph::goals::budget as crate_budget;
-use tinyagents_graph::goals::{BudgetVerdict, GoalBudgetGuard, ThreadGoal, ThreadGoalStatus};
+use tinyagents_graph::goals::{ThreadGoal, ThreadGoalStatus};
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
-use tinyagents_hooks::stop::{StopDecision, StopHook, TurnState};
/// Load the goal for an explicitly supplied chat thread.
pub async fn load_for_thread(workspace_dir: &Path, thread_id: Option<&str>) -> Option<ThreadGoal> {
@@ -228,63 +224,6 @@ fn normalized_thread(thread_id: Option<&str>) -> Option<String> {
.map(ToOwned::to_owned)
}
-/// Mid-turn stop hook that halts an in-flight turn once an **active** goal's
-/// running usage (already-accounted tokens from prior turns + this turn's
-/// tokens so far) would meet or exceed its budget.
-///
-/// The decision is the crate's
-/// [`GoalBudgetGuard`](tinyagents_graph::goals::GoalBudgetGuard); this is the
-/// adapter that votes it into OpenHuman's [`StopHook`] chain. #4469 item 1: the
-/// stop is a graceful *pause*, not an instantaneous abort — the vote fires in
-/// the stop-hook middleware's `after_model`, and the harness drains the pause
-/// at the **top of the next iteration**, so the tool round for the model call
-/// that tripped the budget still runs and the turn's wrap-up summary may spend
-/// one more model call before the partial transcript is returned. It bounds an
-/// autonomous run to a small, deterministic overshoot past the ceiling rather
-/// than a hard cut at the exact accounting point.
-///
-/// The guard only arms for a goal that is `Active` with a configured budget,
-/// and stands down if that goal is completed, replaced, or paused mid-turn —
-/// once a goal is `budget_limited`/`paused`/`complete` the user can still chat
-/// freely (the injected context steers the model to summarise), so a
-/// user-present turn is never hard-stopped by a budget that is no longer live.
-#[derive(Debug, Clone)]
-pub struct GoalBudgetStopHook {
- workspace_dir: PathBuf,
- guard: GoalBudgetGuard,
-}
-
-impl GoalBudgetStopHook {
- /// Build a hook for `goal` if it's active and has a budget; `None` otherwise.
- pub fn for_goal(workspace_dir: &Path, goal: &ThreadGoal) -> Option<Self> {
- Some(Self {
- workspace_dir: workspace_dir.to_path_buf(),
- guard: GoalBudgetGuard::for_goal(goal)?,
- })
- }
-}
-
-#[async_trait]
-impl StopHook<tinyagents_harness::cost::TurnCost> for GoalBudgetStopHook {
- fn name(&self) -> &str {
- "thread_goal_budget"
- }
-
- async fn check(&self, ctx: &TurnState<'_, tinyagents_harness::cost::TurnCost>) -> StopDecision {
- let store = crate::storage::sessions::stores_or_ephemeral(&self.workspace_dir).kv;
- let in_flight = turn_tokens(ctx.cost.input_tokens, ctx.cost.output_tokens);
- match self.guard.check(&store, in_flight).await {
- Ok(BudgetVerdict::Stop { reason }) => StopDecision::Stop { reason },
- Ok(BudgetVerdict::Continue) => StopDecision::Continue,
- Err(e) => {
- // An unreadable goal is not grounds for killing a live turn.
- tracing::debug!(error = %e, "[thread_goals] budget check failed; continuing");
- StopDecision::Continue
- }
- }
- }
-}
-
#[cfg(test)]
#[path = "runtime_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs b/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs
index 3df02fe1a2..550e8835cd 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs
@@ -277,13 +277,29 @@ pub(super) fn install_turn_middleware(
harness.push_middleware(mw.clone());
}
- stop_hooks::install(
- harness,
- handle.as_ref(),
- model,
- stop_hooks,
- halt_summary.clone(),
- );
+ if handle.is_some() && !stop_hooks.is_empty() {
+ let summary = halt_summary.clone();
+ harness.push_middleware(Arc::new(
+ tinyagents_hooks::stop::StopHookMiddleware::new(model, stop_hooks)
+ .with_metrics_seed(crate::agent::cost::turn_cost())
+ .with_stop_observer(move |report| {
+ tracing::warn!(
+ target: "stop_hooks",
+ hook = %report.hook,
+ iteration = report.iteration,
+ model = %report.model,
+ "[stop_hooks] hook voted to stop the turn — pausing run: {}",
+ report.reason
+ );
+ *summary
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(format!(
+ "Stopping after {} model call(s): {}",
+ report.iteration, report.reason
+ ));
+ }),
+ ));
+ }
// SHADOW tool-exposure layer (issue #4249, 01.3 — dynamic exposure). Compose
// the OpenHuman exposure policy as a crate-native selection layer
// (ToolAllowlistMiddleware + a ContextualToolSelectionMiddleware built via