Skip to content

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

feat(observability): classify API key rejection as session expired error

Steven Enamakelf0b0031d7db3
diff --git a/crates/openhuman-core/src/api/rest.rs b/crates/openhuman-core/src/api/rest.rs
index 52f2a0cc2d..7bfa6d5d4c 100644
--- a/crates/openhuman-core/src/api/rest.rs
+++ b/crates/openhuman-core/src/api/rest.rs
@@ -145,7 +145,10 @@ pub fn flatten_authed_error(err: anyhow::Error) -> String {
// (clear the session, prompt re-sign-in) would be the wrong
// recovery. See `BackendApiError::ApiKeyRejected`.
Some(BackendApiError::ApiKeyRejected { method, path }) => {
- format!("API_KEY_REJECTED: backend rejected api key on {method} {path}")
+ format!(
+ "{} backend rejected api key on {method} {path}",
+ crate::core::observability::API_KEY_REJECTED_PREFIX
+ )
}
Some(BackendApiError::BackendUnavailable { method, path }) => {
format!(
diff --git a/crates/openhuman-core/src/core/observability.rs b/crates/openhuman-core/src/core/observability.rs
index ec0a387b78..b27e7e150a 100644
--- a/crates/openhuman-core/src/core/observability.rs
+++ b/crates/openhuman-core/src/core/observability.rs
@@ -615,6 +615,15 @@ pub fn expected_error_kind(message: &str) -> Option<ExpectedErrorKind> {
if is_session_expired_message(message) {
return Some(ExpectedErrorKind::SessionExpired);
}
+ // The backend rejected the stored TinyHumans API key (401 on an api-key
+ // credential, `BackendApiError::ApiKeyRejected`). User-state — the key was
+ // revoked or mistyped and only a new key recovers it — so it shares the
+ // credential-lapse bucket. Kept out of `is_session_expired_message` on
+ // purpose: that predicate feeds the `SessionExpired` publish, which must
+ // not clear a session because an API key failed.
+ if is_api_key_rejected_message(message) {
+ return Some(ExpectedErrorKind::SessionExpired);
+ }
if is_embedding_backend_auth_failure(&lower) {
return Some(ExpectedErrorKind::SessionExpired);
}
@@ -3177,6 +3186,17 @@ pub const USAGE_PROBE_BACKOFF_PREFIX: &str = "USAGE_PROBE_BACKOFF:";
/// classifies it as [`ExpectedErrorKind::BackendUnavailable`].
pub const BACKEND_UNAVAILABLE_PREFIX: &str = "BACKEND_UNAVAILABLE:";
+/// Sentinel prefix on the error string a backend call returns when the backend
+/// rejects the stored TinyHumans API key (`api::rest::flatten_authed_error`).
+/// [`expected_error_kind`] demotes it: the fix is a new key, not a code change.
+pub const API_KEY_REJECTED_PREFIX: &str = "API_KEY_REJECTED:";
+
+/// Whether `msg` carries the [`API_KEY_REJECTED_PREFIX`] sentinel anywhere in
+/// its chain.
+pub fn is_api_key_rejected_message(msg: &str) -> bool {
+ msg.contains(API_KEY_REJECTED_PREFIX)
+}
+
/// Whether `msg` is the backend-unavailable sentinel (see
/// [`BACKEND_UNAVAILABLE_PREFIX`]). Matched anywhere in the chain because
/// callers wrap it with `anyhow` context before it reaches the reporter.