Skip to content

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

fix(tinyhumans): bound backend error bodies; use the SDK 404 classifier

M3gA-Mind9e11a86c7fc9
diff --git a/crates/openhuman-tinyhumans/src/client/backend_client_tests.rs b/crates/openhuman-tinyhumans/src/client/backend_client_tests.rs
index 4e524c6658..d69c167cda 100644
--- a/crates/openhuman-tinyhumans/src/client/backend_client_tests.rs
+++ b/crates/openhuman-tinyhumans/src/client/backend_client_tests.rs
@@ -1,5 +1,7 @@
use super::*;
-use crate::client::classify::{backend_api_body_shape, BACKEND_API_BODY_SHAPE_MAX_BYTES};
+use crate::client::classify::{
+ backend_api_body_shape, BACKEND_API_BODY_SHAPE_MAX_BYTES, BACKEND_API_ERROR_BODY_MAX_BYTES,
+};
use crate::client::{flatten_authed_error, HostedApiError};
use axum::extract::State;
use axum::http::HeaderMap;
@@ -361,3 +363,34 @@ fn backend_api_body_shape_bounds_long_safe_key_list() {
#[path = "backend_channel_tests.rs"]
mod channel_tests;
+
+#[tokio::test]
+async fn non_2xx_error_body_is_truncated_in_the_returned_error() {
+ let body = format!("{}SECRET_TAIL", "x".repeat(1024 * 1024));
+ let app = Router::new().route(
+ "/teams/me/usage",
+ get(move || {
+ let body = body.clone();
+ async move { (axum::http::StatusCode::UNPROCESSABLE_ENTITY, body) }
+ }),
+ );
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ tokio::spawn(async move {
+ axum::serve(listener, app).await.unwrap();
+ });
+
+ let client = BackendClient::new(&format!("http://{addr}")).unwrap();
+ let err = client
+ .authed_json("mock-jwt", Method::GET, "/teams/me/usage", None)
+ .await
+ .unwrap_err();
+ let message = flatten_authed_error(err);
+ assert!(message.contains("failed (422"), "{message}");
+ assert!(!message.contains("SECRET_TAIL"));
+ assert!(
+ message.len() < BACKEND_API_ERROR_BODY_MAX_BYTES + 256,
+ "{}",
+ message.len()
+ );
+}
diff --git a/crates/openhuman-tinyhumans/src/client/classify.rs b/crates/openhuman-tinyhumans/src/client/classify.rs
index fd8147612d..b38b3c0ff5 100644
--- a/crates/openhuman-tinyhumans/src/client/classify.rs
+++ b/crates/openhuman-tinyhumans/src/client/classify.rs
@@ -18,6 +18,10 @@ use super::error::HostedApiError;
/// Bounded so a body with pathologically many keys can't bloat the event.
pub(super) const BACKEND_API_BODY_SHAPE_MAX_BYTES: usize = 120;
+/// Max bytes of a non-2xx response body carried in the returned error, which
+/// reaches the RPC error string and the logs.
+pub(super) const BACKEND_API_ERROR_BODY_MAX_BYTES: usize = 512;
+
/// PII-safe classification of a non-2xx response body for telemetry.
///
/// The report message is written to daily logs before any Sentry scrubbing, so
@@ -255,9 +259,10 @@ impl BackendClient {
);
}
anyhow::bail!(
- "{} {} failed ({status}): {text}",
+ "{} {} failed ({status}): {}",
method.as_str(),
- url.path()
+ url.path(),
+ truncate_at_byte_boundary(&text, BACKEND_API_ERROR_BODY_MAX_BYTES)
)
}
}
diff --git a/crates/openhuman-tinyhumans/src/transport/README.md b/crates/openhuman-tinyhumans/src/transport/README.md
index 779fb4d54b..81f8743cbd 100644
--- a/crates/openhuman-tinyhumans/src/transport/README.md
+++ b/crates/openhuman-tinyhumans/src/transport/README.md
@@ -63,8 +63,9 @@ says what it means: a 404 on a channel-message path
```text
404 on .../channels/<p>/messages/<id>
|
- +-- method is PATCH and body contains "Cannot PATCH "
- | (Express unmatched-route page: the backend has no edit route)
+ +-- method is PATCH and the body is not JSON (the SDK's
+ | `is_unmatched_route_404`: Express's unmatched-route page, or a
+ | proxy's HTML 404; no handler answered, so no edit route)
| -> ChannelMessageRouteMissing { provider, message_id }
|
+-- otherwise (handler's JSON 404: the message is gone)
diff --git a/crates/openhuman-tinyhumans/src/transport/channel_404_tests.rs b/crates/openhuman-tinyhumans/src/transport/channel_404_tests.rs
index 5672631aed..fe27cff39e 100644
--- a/crates/openhuman-tinyhumans/src/transport/channel_404_tests.rs
+++ b/crates/openhuman-tinyhumans/src/transport/channel_404_tests.rs
@@ -69,6 +69,33 @@ async fn patch_404_without_a_matching_route_is_route_missing() {
);
}
+#[tokio::test]
+async fn patch_404_with_a_non_json_body_is_route_missing() {
+ // A CDN or proxy HTML 404 has no "Cannot PATCH", but no backend handler
+ // answered it either: the SDK's classifier treats any non-JSON body as an
+ // unmatched route, and the host follows it.
+ let server = server_404(
+ "PATCH",
+ "/channels/telegram/messages/5",
+ ResponseTemplate::new(404).set_body_string("<html>Not Found</html>"),
+ )
+ .await;
+ let err = send(
+ &server.uri(),
+ Method::PATCH,
+ "/channels/telegram/messages/5",
+ )
+ .await
+ .unwrap_err();
+ assert!(
+ matches!(
+ err,
+ BackendTransportError::ChannelMessageRouteMissing { .. }
+ ),
+ "got {err:?}"
+ );
+}
+
#[tokio::test]
async fn patch_404_from_a_real_handler_is_a_missing_message() {
// The world where the edit route exists: a handler's JSON 404 means the
diff --git a/crates/openhuman-tinyhumans/src/transport/error.rs b/crates/openhuman-tinyhumans/src/transport/error.rs
index b29d65a1e2..c6e6587611 100644
--- a/crates/openhuman-tinyhumans/src/transport/error.rs
+++ b/crates/openhuman-tinyhumans/src/transport/error.rs
@@ -5,33 +5,9 @@
use super::port::BackendTransportError;
use reqwest::Method;
+use tinyhumans_sdk::classify::channel_message_path;
use tinyhumans_sdk::Error as SdkError;
-fn channel_message_path(path: &str) -> Option<(&str, &str)> {
- let segments = path
- .split('/')
- .filter(|segment| !segment.is_empty())
- .collect::<Vec<_>>();
- let channels = segments.iter().position(|segment| *segment == "channels")?;
- if segments.get(channels + 2).copied() != Some("messages") {
- return None;
- }
- let provider = *segments.get(channels + 1)?;
- let message_id = *segments.get(channels + 3)?;
- if provider.is_empty() || message_id.is_empty() || segments.len() != channels + 4 {
- return None;
- }
- Some((provider, message_id))
-}
-
-fn is_unmatched_route_404(error: &SdkError) -> bool {
- let SdkError::Status { body, .. } = error else {
- return false;
- };
- body.as_str()
- .is_some_and(|body| body.contains("Cannot PATCH "))
-}
-
/// Map the SDK's error for `method path` onto the core-owned transport error.
///
/// A `404` on a channel-message route becomes a typed variant here, because
@@ -44,7 +20,7 @@ pub fn map_sdk_error(error: SdkError, method: &Method, path: &str) -> BackendTra
if let SdkError::Status { status: 404, .. } = &error {
if let Some((provider, message_id)) = channel_message_path(path) {
let (provider, message_id) = (provider.to_owned(), message_id.to_owned());
- if *method == Method::PATCH && is_unmatched_route_404(&error) {
+ if *method == Method::PATCH && error.is_unmatched_route_404() {
log::debug!(
"[tinyhumans-transport] {method} {path}: 404 with no matching route; \
channel edit route missing"