Skip to content

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

feat(agent): add typed TurnTimeoutBound to preserve timeout reason through sanitized errors

Steven Enamakelb4fff814918b
diff --git a/crates/openhuman-core/src/agent/error.rs b/crates/openhuman-core/src/agent/error.rs
index 096986daef..1f54cd2791 100644
--- a/crates/openhuman-core/src/agent/error.rs
+++ b/crates/openhuman-core/src/agent/error.rs
@@ -203,6 +203,47 @@ pub fn is_max_iterations_error(error_msg: &str) -> bool {
error_msg.contains(MAX_ITERATIONS_ERROR_PREFIX)
}
+/// Which wall-clock bound ended a turn, as the host reports it to telemetry.
+///
+/// Carried structurally from the harness (`HostedError::timeout_bound`) into
+/// the turn error, because the hosted error message is sanitized and no longer
+/// names the bound. The phrases below are the single place the host spells the
+/// two bounds: the turn runner composes the error text from them and
+/// `web_chat::ops::timeout_bound_tag` recognises that text through them, so
+/// neither side restates the wording.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum TurnTimeoutBound {
+ /// One model call exceeded the per-model-call ceiling.
+ PerModelCall,
+ /// The run's own remaining wall-clock budget ran out.
+ RunRemaining,
+}
+
+impl TurnTimeoutBound {
+ /// Stable phrase embedded in the flattened turn error.
+ pub const fn phrase(self) -> &'static str {
+ match self {
+ Self::PerModelCall => "per-model-call ceiling",
+ Self::RunRemaining => "remaining wall-clock budget",
+ }
+ }
+
+ /// Sentry `timeout_bound` tag value.
+ pub const fn tag(self) -> &'static str {
+ match self {
+ Self::PerModelCall => "per_model_call",
+ Self::RunRemaining => "run_remaining",
+ }
+ }
+
+ /// Recover the bound from a flattened error string, if it names one.
+ pub fn from_message(message: &str) -> Option<Self> {
+ [Self::PerModelCall, Self::RunRemaining]
+ .into_iter()
+ .find(|bound| message.contains(bound.phrase()))
+ }
+}
+
#[cfg(test)]
#[path = "error_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/agent/tinyagents/hosted_error.rs b/crates/openhuman-core/src/agent/tinyagents/hosted_error.rs
new file mode 100644
index 0000000000..1a49df5b45
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/hosted_error.rs
@@ -0,0 +1,51 @@
+//! Turn-error projection of the harness's typed [`HostedError`].
+//!
+//! `HostedError::message` is a fixed, sanitized string per kind, so the
+//! per-model-call ceiling and an exhausted run budget look identical once the
+//! error is flattened. The bound is carried structurally instead
+//! (`HostedError::timeout_bound`), and this projection writes it into the turn
+//! error with [`TurnTimeoutBound`] so the Sentry `timeout_bound` tag survives
+//! the hosted path. The user-facing class is untouched: the text still reads as
+//! `run timed out: ...`, which `web_errors` classifies as `turn_timeout`.
+
+use tinyagents_harness::runtime::{HostedError, HostedErrorKind, TimeoutBound};
+use tinyagents_harness::TinyAgentsError;
+
+use crate::agent::error::TurnTimeoutBound;
+
+/// The host-side name of a harness [`TimeoutBound`].
+pub(super) fn turn_timeout_bound(bound: TimeoutBound) -> TurnTimeoutBound {
+ match bound {
+ TimeoutBound::PerModelCall => TurnTimeoutBound::PerModelCall,
+ // `TimeoutBound` is non-exhaustive; any other bound is a run-level one.
+ _ => TurnTimeoutBound::RunRemaining,
+ }
+}
+
+/// Convert a hosted failure into the run error the turn pipeline maps.
+///
+/// Identical to `TinyAgentsError::from(error)` except that a timeout names the
+/// bound that fired. It stays a terminal `Timeout`, as before.
+pub(super) fn run_error_from_hosted(error: HostedError) -> TinyAgentsError {
+ let bound = match (error.kind, error.timeout_bound) {
+ (HostedErrorKind::Timeout, Some(bound)) => Some(turn_timeout_bound(bound)),
+ _ => None,
+ };
+ match bound {
+ Some(bound) => {
+ tracing::debug!(
+ timeout_bound = bound.tag(),
+ "[tinyagents] hosted turn timed out; carrying the typed bound"
+ );
+ TinyAgentsError::Timeout(format!(
+ "hosted agent invocation exceeded its {}",
+ bound.phrase()
+ ))
+ }
+ None => TinyAgentsError::from(error),
+ }
+}
+
+#[cfg(test)]
+#[path = "hosted_error_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/agent/tinyagents/hosted_error_tests.rs b/crates/openhuman-core/src/agent/tinyagents/hosted_error_tests.rs
new file mode 100644
index 0000000000..7fd6813198
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/hosted_error_tests.rs
@@ -0,0 +1,42 @@
+use super::*;
+use crate::web_chat::web_errors::is_turn_timeout_error;
+
+fn hosted(kind: HostedErrorKind, bound: Option<TimeoutBound>) -> HostedError {
+ HostedError {
+ kind,
+ message: "hosted agent invocation timed out".to_string(),
+ timeout_bound: bound,
+ run: None,
+ }
+}
+
+#[test]
+fn a_per_call_timeout_names_the_per_model_call_bound() {
+ let err = run_error_from_hosted(hosted(
+ HostedErrorKind::Timeout,
+ Some(TimeoutBound::PerModelCall),
+ ));
+ assert!(matches!(err, TinyAgentsError::Timeout(_)));
+ let text = err.to_string();
+ assert_eq!(
+ crate::web_chat::timeout_bound_tag(&text),
+ "per_model_call",
+ "{text}"
+ );
+ assert!(is_turn_timeout_error(&text), "user-facing class unchanged");
+}
+
+#[test]
+fn a_run_budget_timeout_names_the_run_bound() {
+ let text = run_error_from_hosted(hosted(HostedErrorKind::Timeout, Some(TimeoutBound::Run)))
+ .to_string();
+ assert_eq!(crate::web_chat::timeout_bound_tag(&text), "run_remaining");
+ assert!(is_turn_timeout_error(&text));
+}
+
+#[test]
+fn a_non_timeout_failure_is_converted_as_before() {
+ let err = run_error_from_hosted(hosted(HostedErrorKind::Provider, None));
+ assert!(matches!(err, TinyAgentsError::Model(_)));
+ assert_eq!(crate::web_chat::timeout_bound_tag(&err.to_string()), "none");
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/mod.rs b/crates/openhuman-core/src/agent/tinyagents/mod.rs
index 65befcbf11..359f84d20a 100644
--- a/crates/openhuman-core/src/agent/tinyagents/mod.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/mod.rs
@@ -25,6 +25,7 @@ mod embeddings;
mod harness_assembly;
mod harness_context_ladder;
mod harness_tool_registration;
+mod hosted_error;
pub mod host;
pub(crate) mod journal;
pub(crate) mod middleware;
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs b/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs
index 5c9316b1bb..f56e9fd335 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_runner.rs
@@ -659,7 +659,7 @@ async fn run_turn_via_tinyagents_inner(
streaming,
"[tinyagents] hosted root turn failed; keeping the typed kind"
);
- tinyagents_harness::TinyAgentsError::from(error)
+ super::hosted_error::run_error_from_hosted(error)
})
} else if streaming {
let mut stream = Box::pin(harness.invoke_stream_in_context(&(), ctx, input));
diff --git a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
index cf0aa5cab9..1fe830f4f8 100644
--- a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
+++ b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
@@ -151,12 +151,15 @@ pub(crate) fn sentry_suppression_reason(detailed: &str) -> Option<&'static str>
/// under one tag would rebuild, in the dashboard, exactly the conflation this
/// change removed from the code (#5804).
///
-/// Pure over the formatted error string, for the same reason its neighbour is.
+/// The bound is a typed field on the harness's hosted error
+/// (`HostedError::timeout_bound`); the turn runner turns it into a
+/// [`TurnTimeoutBound`](crate::agent::error::TurnTimeoutBound) phrase in the
+/// flattened error, and this function reads that phrase back through the same
+/// type rather than a second spelling. Pure over the formatted error string,
+/// for the same reason its neighbour is.
pub(crate) fn timeout_bound_tag(detailed: &str) -> &'static str {
- if detailed.contains("per-model-call ceiling") {
- "per_model_call"
- } else if detailed.contains("remaining wall-clock budget") {
- "run_remaining"
+ if let Some(bound) = crate::agent::error::TurnTimeoutBound::from_message(detailed) {
+ bound.tag()
} else if super::super::web_errors::is_turn_timeout_error(detailed) {
"unclassified_timeout"
} else {