Skip to content

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

fix(chat): gate the local-offline refusal on the whole failure

obchain2d07e4549bf2
diff --git a/crates/openhuman-core/src/web_chat/web_errors/classify.rs b/crates/openhuman-core/src/web_chat/web_errors/classify.rs
index cfc1bccfe2..e279262a16 100644
--- a/crates/openhuman-core/src/web_chat/web_errors/classify.rs
+++ b/crates/openhuman-core/src/web_chat/web_errors/classify.rs
@@ -150,6 +150,31 @@ pub(super) fn classified_plain(
)
}
+/// Whether `err` says nothing except that the offline local session cannot
+/// reach managed inference (#6932).
+///
+/// A fallback aggregate concatenates every attempt, so the sentinel being
+/// present does not mean it is the whole story: one leg can be this refusal
+/// while another is a BYO provider failing on its own key. Only an error whose
+/// every attempt is the refusal is explained by the local profile.
+fn only_local_session_refused(err: &str) -> bool {
+ const AGGREGATE: &str = "All providers/models failed";
+ let sentinel =
+ crate::security::credentials::session_support::LOCAL_SESSION_MANAGED_INFERENCE_UNAVAILABLE;
+ if !err.contains(sentinel) {
+ return false;
+ }
+ match err.split_once(AGGREGATE) {
+ // Not a chain aggregate: the refusal is the only failure there is.
+ None => true,
+ Some((_, attempts)) => attempts
+ .split(';')
+ .map(str::trim)
+ .filter(|attempt| !attempt.is_empty())
+ .all(|attempt| attempt.contains(sentinel)),
+ }
+}
+
pub(crate) fn classify_inference_error(err: &str) -> ClassifiedError {
use FailureClass as C;
let lower = err.to_lowercase();
@@ -184,16 +209,25 @@ pub(crate) fn classify_inference_error(err: &str) -> ClassifiedError {
// "iteration", so they MUST be checked before the generic provider-429
// branch — otherwise users see a confusing "your AI provider is
// rate-limiting you" message for limits OpenHuman itself enforced (#2364).
- let classified = if err.contains(
- crate::security::credentials::session_support::LOCAL_SESSION_MANAGED_INFERENCE_UNAVAILABLE,
- ) {
- // #6932: the offline local profile cannot run the managed model, and
- // `resolve_bearer` now refuses before the request instead of letting
- // the backend's `401 "Invalid token"` come back — that envelope is
- // claimed by `is_session_expired_message` below, which told a locally
- // signed-in user their session had expired. An exact-sentinel match,
- // so it leads the ladder.
- classified_plain(C::LocalSessionManagedUnavailable, None, None)
+ let classified = if only_local_session_refused(err) {
+ // #6932: `resolve_bearer` refuses the offline local session before the
+ // request, so the backend `401 "Invalid token"` that
+ // `is_session_expired_message` claims — and that told a locally
+ // signed-in user their session had expired — never comes back.
+ //
+ // Leads the ladder, because the aggregate wrapper's own words defeat
+ // the arms below: "All providers/models failed" supplies "models" and
+ // the sentinel supplies "unavailable", which is exactly
+ // `is_model_unavailable_text`, so a refusal reaching the lower arms
+ // gets told to check its model settings. The gate is what keeps that
+ // lead honest — a chain that refused managed and then failed a BYO
+ // provider on its own key is NOT claimed here and reports that key
+ // instead.
+ classified_plain(
+ C::LocalSessionManagedUnavailable,
+ provider,
+ fallback_available,
+ )
} else if is_codex_token_expired_text(&err.to_ascii_lowercase()) {
// Codex OAuth refresh failed (#5869): the user must reconnect Codex in
// Settings → Integrations, NOT sign into OpenHuman. Checked before
diff --git a/crates/openhuman-core/src/web_chat/web_tests_error_code_classification_tests.rs b/crates/openhuman-core/src/web_chat/web_tests_error_code_classification_tests.rs
index 31b83cb19b..2f7df610c6 100644
--- a/crates/openhuman-core/src/web_chat/web_tests_error_code_classification_tests.rs
+++ b/crates/openhuman-core/src/web_chat/web_tests_error_code_classification_tests.rs
@@ -610,3 +610,41 @@ fn classify_inference_error_local_offline_profile_is_not_an_expired_session() {
classified.message
);
}
+
+#[test]
+fn classify_inference_error_a_mixed_fallback_chain_reports_the_provider_that_failed() {
+ // #6932 review: a fallback aggregate carries every attempt's text. A chain
+ // that refused managed for the local profile and then failed a BYO
+ // provider on its own key must report the key, not blame the profile for
+ // someone else's failure — so the sentinel sits with the catch-alls rather
+ // than at the head of the ladder.
+ let raw = format!(
+ "All providers/models failed. Attempts: {}; openai API error (401 Unauthorized): invalid api key",
+ crate::security::credentials::session_support::LOCAL_SESSION_MANAGED_INFERENCE_UNAVAILABLE
+ );
+ let classified = classify_inference_error(&raw);
+
+ assert_eq!(classified.error_type, "auth_error");
+ assert!(
+ !classified.message.contains("local offline profile"),
+ "must not blame the local profile for the BYO provider's key: {}",
+ classified.message
+ );
+}
+
+#[test]
+fn classify_inference_error_the_refusal_alone_keeps_the_exhausted_chain_flag() {
+ // The same refusal with nothing else in the chain is still ours, and the
+ // aggregate metadata rides along instead of being dropped.
+ let raw = format!(
+ "All providers/models failed. Attempts: {}",
+ crate::security::credentials::session_support::LOCAL_SESSION_MANAGED_INFERENCE_UNAVAILABLE
+ );
+ let classified = classify_inference_error(&raw);
+
+ assert_eq!(
+ classified.copy_key,
+ "chat_error.local_session_managed_unavailable"
+ );
+ assert_eq!(classified.fallback_available, Some(false));
+}