Skip to content

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

feat(tinyagents): pin stream idle and first-event timeouts explicitly

Steven Enamakel75dd7c0101ff
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
index ed735b351f..986a215155 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
@@ -103,6 +103,49 @@ pub(super) fn parse_model_call_wall_clock_ms(env_value: Option<&str>) -> Option<
(secs > 0).then(|| secs.saturating_mul(1_000))
}
+/// Default silence allowed between output events of a streaming model call,
+/// in seconds, once the first output has arrived. Matches the harness default
+/// but is set explicitly so a harness bump cannot silently change it.
+pub(super) const DEFAULT_STREAM_IDLE_TIMEOUT_SECS: u64 = 120;
+
+/// Default circuit breaker: consecutive stream idle timeouts on one model
+/// before retrying that model stops.
+pub(super) const DEFAULT_MAX_CONSECUTIVE_STREAM_IDLE_TIMEOUTS: usize = 5;
+
+/// Pure core of the stream idle ceiling: `OPENHUMAN_STREAM_IDLE_TIMEOUT_SECS`
+/// in seconds, `0` disables, absent/unparseable falls back to the default.
+pub(super) fn parse_stream_idle_timeout_ms(env_value: Option<&str>) -> Option<u64> {
+ let secs = env_value
+ .and_then(|v| v.trim().parse::<u64>().ok())
+ .unwrap_or(DEFAULT_STREAM_IDLE_TIMEOUT_SECS);
+ (secs > 0).then(|| secs.saturating_mul(1_000))
+}
+
+/// Pure core of the first-event ceiling: `OPENHUMAN_STREAM_FIRST_EVENT_TIMEOUT_SECS`.
+/// Off (`None`) unless set to a positive value: reasoning-hidden providers and
+/// local models legitimately stay silent for minutes before the first token.
+pub(super) fn parse_stream_first_event_timeout_ms(env_value: Option<&str>) -> Option<u64> {
+ env_value
+ .and_then(|v| v.trim().parse::<u64>().ok())
+ .filter(|secs| *secs > 0)
+ .map(|secs| secs.saturating_mul(1_000))
+}
+
+/// Pure core of the idle-timeout breaker:
+/// `OPENHUMAN_MAX_CONSECUTIVE_STREAM_IDLE_TIMEOUTS`, `0` disables.
+pub(super) fn parse_max_consecutive_stream_idle_timeouts(
+ env_value: Option<&str>,
+) -> Option<usize> {
+ let n = env_value
+ .and_then(|v| v.trim().parse::<usize>().ok())
+ .unwrap_or(DEFAULT_MAX_CONSECUTIVE_STREAM_IDLE_TIMEOUTS);
+ (n > 0).then_some(n)
+}
+
+fn env_str(name: &str) -> Option<String> {
+ std::env::var(name).ok()
+}
+
/// Build the harness [`RunPolicy`] for an openhuman turn.
///
/// The loop enforces limits from `self.policy.limits` (not the per-run
@@ -168,6 +211,19 @@ pub(crate) fn run_policy_for(max_iterations: usize, response_cache_enabled: bool
// delegations) are exempt in the harness and keep the remainder-only
// budget. Env-overridable, `0` disables.
policy.limits.max_model_call_ms = model_call_wall_clock_ms();
+ // Stream silence bounds, explicit so a harness default change cannot
+ // alter them. The idle window applies only after the first output event;
+ // the first-event bound is OFF by default because local and hidden-
+ // reasoning models stay silent for minutes before the first token. The
+ // breaker stops retrying a model that keeps stalling.
+ policy.limits.stream_idle_timeout_ms =
+ parse_stream_idle_timeout_ms(env_str("OPENHUMAN_STREAM_IDLE_TIMEOUT_SECS").as_deref());
+ policy.limits.stream_first_event_timeout_ms = parse_stream_first_event_timeout_ms(
+ env_str("OPENHUMAN_STREAM_FIRST_EVENT_TIMEOUT_SECS").as_deref(),
+ );
+ policy.limits.max_consecutive_stream_idle_timeouts = parse_max_consecutive_stream_idle_timeouts(
+ env_str("OPENHUMAN_MAX_CONSECUTIVE_STREAM_IDLE_TIMEOUTS").as_deref(),
+ );
// Each executed tool row ends with `[took 12.3s]` (#6953). Without it the
// model cannot tell a fifteen-minute command from a fast one, so it cannot
// budget the rest of the turn against the ceiling above.
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy_budget_tests.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy_budget_tests.rs
index c3e70000f9..b3bf9bc763 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy_budget_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy_budget_tests.rs
@@ -17,3 +17,23 @@ async fn per_turn_tool_limit_reaches_the_execution_policy() {
fn tool_results_carry_their_duration() {
assert!(run_policy_for(10, false).tool_result_durations);
}
+
+#[test]
+fn stream_timeouts_are_set_explicitly_with_first_event_off() {
+ let limits = run_policy_for(10, false).limits;
+ assert_eq!(limits.stream_idle_timeout_ms, Some(120_000));
+ assert_eq!(limits.stream_first_event_timeout_ms, None);
+ assert_eq!(limits.max_consecutive_stream_idle_timeouts, Some(5));
+}
+
+#[test]
+fn stream_timeout_env_overrides_parse() {
+ assert_eq!(parse_stream_idle_timeout_ms(Some("30")), Some(30_000));
+ assert_eq!(parse_stream_idle_timeout_ms(Some("0")), None);
+ assert_eq!(parse_stream_idle_timeout_ms(Some("junk")), Some(120_000));
+ assert_eq!(parse_stream_first_event_timeout_ms(Some("45")), Some(45_000));
+ assert_eq!(parse_stream_first_event_timeout_ms(Some("0")), None);
+ assert_eq!(parse_stream_first_event_timeout_ms(None), None);
+ assert_eq!(parse_max_consecutive_stream_idle_timeouts(Some("0")), None);
+ assert_eq!(parse_max_consecutive_stream_idle_timeouts(Some("2")), Some(2));
+}