Skip to content

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

fix(agent): bound the grounded close by the turn deadline

M3gA-Mindeb9725b665fe
diff --git a/crates/openhuman-core/src/agent/session_host/driver.rs b/crates/openhuman-core/src/agent/session_host/driver.rs
index 28ffe38ac0..e38ce678af 100644
--- a/crates/openhuman-core/src/agent/session_host/driver.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver.rs
@@ -260,6 +260,11 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
&request.history,
&user_message,
&outcome,
+ request
+ .run_context
+ .data
+ .turn_deadline
+ .map(|deadline| deadline.close_budget(Instant::now())),
)
.await;
let mut output = close
diff --git a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
index 638f63a5d3..d4e7ce2160 100644
--- a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
@@ -204,6 +204,7 @@ pub(super) async fn close_if_needed(
base_history: &[tinyinference_llm::message::Message],
user_message: &str,
outcome: &TinyagentsTurnOutcome,
+ budget: Option<std::time::Duration>,
) -> Option<GroundedClose> {
let needs_cap_close = outcome.hit_cap && !outcome.wrap_up_injected;
let needs_final_close = outcome.text.trim().is_empty() || outcome.breaker_halt.is_some();
@@ -261,11 +262,45 @@ pub(super) async fn close_if_needed(
}
};
- let (output, usage) =
- close_with_one_repair(model, instruction, stop_reason, ask, verify, fallback).await;
+ let (output, usage) = within_close_budget(
+ budget,
+ model,
+ close_with_one_repair(model, instruction, stop_reason, ask, verify, &fallback),
+ &fallback,
+ )
+ .await;
Some(GroundedClose { output, usage })
}
+/// Run `close` within `budget` (the turn deadline's
+/// [`close_budget`](crate::agent::turn_deadline::TurnDeadline::close_budget)),
+/// or ship `fallback` when it runs out. `None` (no deadline) waits for `close`.
+///
+/// The fallback is the same deterministic summary a rejected close ships, so a
+/// slow or stalled provider costs the user the model's wording, not the answer.
+/// Usage of calls that finished before the timeout is not recorded.
+async fn within_close_budget(
+ budget: Option<std::time::Duration>,
+ model: &str,
+ close: impl std::future::Future<Output = (String, RepairUsage)>,
+ fallback: impl FnOnce() -> String,
+) -> (String, RepairUsage) {
+ let Some(budget) = budget else {
+ return close.await;
+ };
+ match tokio::time::timeout(budget, close).await {
+ Ok(closed) => closed,
+ Err(_) => {
+ let budget_ms = budget.as_millis() as u64;
+ tracing::warn!(
+ budget_ms,
+ "[session-runtime] grounded close ran out of the turn budget; shipping the deterministic summary"
+ );
+ (fallback(), RepairUsage::for_model(model))
+ }
+ }
+}
+
/// Ask for a closing message, screen it, and on a violation ask exactly once
/// more with that violation named, before giving up to `fallback`.
///
diff --git a/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs b/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs
index 76237d3752..56b35e79ef 100644
--- a/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs
@@ -248,3 +248,45 @@ fn a_capped_truncated_turn_gets_the_cap_checkpoint() {
assert!(!out.contains("ran out of output tokens"), "{out}");
assert!(out.contains("<tool_records>"), "{out}");
}
+
+/// The production failure (#7716): the close model never answered, and the
+/// backstop dropped the turn with no summary. Within its budget the close
+/// gives up and ships the deterministic summary instead.
+#[tokio::test]
+async fn stalled_close_model_ships_the_fallback_within_the_budget() {
+ let instruction = final_answer_instruction(None, false, RECORDS);
+ let ask = |_prompt: String| std::future::pending::<(String, Option<BilledUsage>)>();
+ let verify = |_candidate: String| async { (None, None) };
+ let fallback = || FALLBACK.to_string();
+
+ let started = std::time::Instant::now();
+ let (output, usage) = within_close_budget(
+ Some(std::time::Duration::from_millis(20)),
+ "test-model",
+ close_with_one_repair("test-model", instruction, None, ask, verify, &fallback),
+ &fallback,
+ )
+ .await;
+
+ assert_eq!(output, FALLBACK);
+ assert_eq!(usage.model_calls, 0);
+ assert!(
+ started.elapsed() < std::time::Duration::from_secs(5),
+ "the budget, not the stalled call, ended the close"
+ );
+}
+
+#[tokio::test]
+async fn close_that_finishes_in_budget_or_without_one_ships_its_reply() {
+ let closed = || async {
+ (
+ CLEAN_REPLY.to_string(),
+ RepairUsage::for_model("test-model"),
+ )
+ };
+ for budget in [Some(std::time::Duration::from_secs(60)), None] {
+ let (output, _) =
+ within_close_budget(budget, "test-model", closed(), || FALLBACK.to_string()).await;
+ assert_eq!(output, CLEAN_REPLY, "budget {budget:?}");
+ }
+}
diff --git a/crates/openhuman-core/src/agent/turn_deadline.rs b/crates/openhuman-core/src/agent/turn_deadline.rs
index e75e45f081..0c6d0bd587 100644
--- a/crates/openhuman-core/src/agent/turn_deadline.rs
+++ b/crates/openhuman-core/src/agent/turn_deadline.rs
@@ -46,6 +46,10 @@ const WIND_DOWN_MARGIN: Duration = Duration::from_secs(120);
/// Upper bound on the gap between the harness hard stop and the backstop.
const HARD_STOP_MARGIN: Duration = Duration::from_secs(60);
+/// Time kept back from the post-loop grounded close so the turn can commit and
+/// emit its reply before the backstop drops it.
+const CLOSE_RESERVE: Duration = Duration::from_secs(10);
+
/// Pure core of [`configured_backstop`]: an absent or unparseable value falls
/// back to [`DEFAULT_TURN_BACKSTOP_SECS`]. `0` disables the backstop (`None`).
pub fn parse_backstop(env_value: Option<&str>) -> Option<Duration> {
@@ -110,6 +114,16 @@ impl TurnDeadline {
self.elapsed_at(now) >= self.wind_down_after()
}
+ /// How long the post-loop grounded close may run as of `now`: what is left
+ /// of the backstop minus [`CLOSE_RESERVE`], zero once that has passed. The
+ /// close otherwise makes up to four model calls with no deadline, and a
+ /// slow one let the backstop drop the turn with no summary (#7716).
+ pub fn close_budget(&self, now: Instant) -> Duration {
+ self.backstop
+ .saturating_sub(self.elapsed_at(now))
+ .saturating_sub(CLOSE_RESERVE)
+ }
+
/// Time left until the hard stop as of `now` (zero once it has passed).
pub fn hard_stop_remaining(&self, now: Instant) -> Duration {
self.hard_stop_after().saturating_sub(self.elapsed_at(now))
diff --git a/crates/openhuman-core/src/agent/turn_deadline_tests.rs b/crates/openhuman-core/src/agent/turn_deadline_tests.rs
index 7c81f40ecd..dcb90d72f7 100644
--- a/crates/openhuman-core/src/agent/turn_deadline_tests.rs
+++ b/crates/openhuman-core/src/agent/turn_deadline_tests.rs
@@ -107,3 +107,16 @@ async fn task_local_scope_is_visible_inside_and_absent_outside() {
.await
.is_none());
}
+
+/// The grounded close gets what is left of the backstop minus the commit
+/// reserve, and nothing once the reserve itself is reached (#7716).
+#[test]
+fn close_budget_is_backstop_remainder_minus_reserve() {
+ let now = Instant::now();
+ let backstop = Duration::from_secs(900);
+ let at = |elapsed: u64| TurnDeadline::new(now - Duration::from_secs(elapsed), backstop);
+ // The wind-down point of a default turn leaves 120s, 110s of it for the close.
+ assert_eq!(at(780).close_budget(now), Duration::from_secs(110));
+ assert_eq!(at(890).close_budget(now), Duration::ZERO);
+ assert_eq!(at(950).close_budget(now), Duration::ZERO);
+}