Skip to content

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

fix(auth): read each managed auth failure's own credential, not the last one

M3gA-Mind5f03f797220e
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_model.rs b/crates/openhuman-tinyhumans/src/inference/managed_model.rs
index ffb10d5daf..3e898ba9cb 100644
--- a/crates/openhuman-tinyhumans/src/inference/managed_model.rs
+++ b/crates/openhuman-tinyhumans/src/inference/managed_model.rs
@@ -5,7 +5,9 @@
//! supplies OpenHuman's live credential resolver and client policy.
use std::path::PathBuf;
-use tinyinference_llm::providers::tinyhumans::{TinyHumansFailurePoint, TinyHumansModel};
+use tinyinference_llm::providers::tinyhumans::{
+ TinyHumansCredential, TinyHumansCredentialKind, TinyHumansFailurePoint, TinyHumansModel,
+};
use super::managed_session::{publish_managed_auth_failure, resolve_session_bearer};
use crate::backend::url::effective_api_url;
@@ -94,11 +96,10 @@ pub(super) fn build_chat_model(
let endpoint = effective_api_url(&api_url.map(str::to_owned));
let resolver_options = options.clone();
let resolver_endpoint = endpoint.clone();
- // Which credential the last request carried: an API-key 401 is a rejected
- // key, not an expired session.
- let sent_api_key = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
- let resolver_sent_api_key = sent_api_key.clone();
- let mut managed = TinyHumansModel::new(
+ // The owner hands each request's credential kind back on that request's
+ // auth failure, so an API-key 401 (a rejected key) is never read as an
+ // expired session, even while the stored credential changes mid-flight.
+ let mut managed = TinyHumansModel::new_with_credential(
endpoint,
model,
super::tiers::MODEL_MANAGED_DEFAULT,
@@ -106,8 +107,12 @@ pub(super) fn build_chat_model(
let (bearer, api_key) = resolver_options
.resolve_bearer_kind(&resolver_endpoint)
.map_err(|error| tinyinference_llm::Error::Model(error.to_string()))?;
- resolver_sent_api_key.store(api_key, std::sync::atomic::Ordering::Relaxed);
- Ok(bearer)
+ let kind = if api_key {
+ TinyHumansCredentialKind::ApiKey
+ } else {
+ TinyHumansCredentialKind::Session
+ };
+ Ok(TinyHumansCredential { bearer, kind })
},
)?
.with_cache_namespace("openhuman")
@@ -116,10 +121,7 @@ pub(super) fn build_chat_model(
.with_error_observer(log_managed_error);
if observe_auth_failures {
- managed = managed.with_managed_auth_failure_handler(move |error, context| {
- let api_key = sent_api_key.load(std::sync::atomic::Ordering::Relaxed);
- publish_managed_auth_failure(error, context, api_key);
- });
+ managed = managed.with_managed_auth_failure_handler(publish_managed_auth_failure);
}
Ok(managed)
}
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_model_tests.rs b/crates/openhuman-tinyhumans/src/inference/managed_model_tests.rs
index 7b42195861..6381f2af66 100644
--- a/crates/openhuman-tinyhumans/src/inference/managed_model_tests.rs
+++ b/crates/openhuman-tinyhumans/src/inference/managed_model_tests.rs
@@ -397,3 +397,120 @@ async fn managed_auth_failure_expires_the_session_only_on_a_session_401() {
assert_eq!(source.as_deref(), expected, "{name}");
}
}
+
+/// A late API-key 401 must not be read as the session's: the failure carries
+/// the credential its own request was sent with, not whichever credential the
+/// model resolved most recently.
+#[tokio::test]
+async fn a_late_api_key_401_after_a_session_request_keeps_the_session() {
+ use axum::http::{HeaderMap, StatusCode};
+ use axum::response::IntoResponse;
+ use openhuman_embed::__host::core::events::DomainEvent;
+ use openhuman_embed::__host::security::credentials::{
+ api_key::API_KEY_PROVIDER, AuthService, DEFAULT_AUTH_PROFILE_NAME,
+ };
+ use std::sync::Arc;
+ use tokio::sync::Notify;
+
+ openhuman_embed::__host::core::bus::init()
+ .await
+ .expect("initialize event bus");
+ let mut events = openhuman_embed::__host::core::bus::BUS
+ .get()
+ .expect("event bus initialized")
+ .receiver();
+ let tmp = tempfile::TempDir::new().expect("temporary credentials");
+ seed_app_session(tmp.path(), Some("2999-01-01T00:00:00Z"));
+ openhuman_embed::__host::security::credentials::api_key::store_api_key_in(
+ tmp.path(),
+ false,
+ "test-managed-key",
+ )
+ .expect("store test API key");
+
+ // The API-key request is held until the session request has resolved its
+ // own bearer and failed.
+ let key_request_arrived = Arc::new(Notify::new());
+ let release_key_request = Arc::new(Notify::new());
+ let (arrived, release) = (key_request_arrived.clone(), release_key_request.clone());
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
+ .await
+ .expect("bind fixture server");
+ let addr = listener.local_addr().expect("fixture address");
+ let app = axum::Router::new().route(
+ "/openai/v1/chat/completions",
+ axum::routing::post(move |headers: HeaderMap| {
+ let (arrived, release) = (arrived.clone(), release.clone());
+ async move {
+ let api_key = headers
+ .get(axum::http::header::AUTHORIZATION)
+ .and_then(|value| value.to_str().ok())
+ == Some("Bearer test-managed-key");
+ let (status, marker) = if api_key {
+ arrived.notify_one();
+ release.notified().await;
+ (StatusCode::UNAUTHORIZED, "HOST_AUTH_LATE_API_KEY_401")
+ } else {
+ (StatusCode::FORBIDDEN, "HOST_AUTH_SESSION_403")
+ };
+ (
+ status,
+ axum::Json(serde_json::json!({ "error": { "message": marker } })),
+ )
+ .into_response()
+ }
+ }),
+ );
+ let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
+ let model = Arc::new(
+ build_chat_model(
+ Some(&format!("http://{addr}")),
+ &managed_options_at(tmp.path()),
+ "reasoning-v1",
+ true,
+ None,
+ true,
+ )
+ .expect("build owner model"),
+ );
+
+ tokio::time::timeout(std::time::Duration::from_secs(30), async {
+ let key_model = model.clone();
+ let key_request = tokio::spawn(async move {
+ key_model
+ .invoke(&(), ModelRequest::new(vec![Message::user("ping")]))
+ .await
+ .is_err()
+ });
+ key_request_arrived.notified().await;
+
+ // The stored credential switches to the session mid-flight.
+ AuthService::new(tmp.path(), false)
+ .remove_profile(API_KEY_PROVIDER, DEFAULT_AUTH_PROFILE_NAME)
+ .expect("remove test API key");
+ assert!(model
+ .invoke(&(), ModelRequest::new(vec![Message::user("ping")]))
+ .await
+ .is_err());
+
+ release_key_request.notify_one();
+ assert!(key_request.await.expect("API-key request task"));
+ })
+ .await
+ .expect("the held API-key request completes");
+ server.abort();
+
+ loop {
+ match events.try_recv() {
+ Ok(DomainEvent::SessionExpired { source, reason }) => {
+ assert!(
+ !reason.contains("HOST_AUTH_LATE_API_KEY_401")
+ && !reason.contains("HOST_AUTH_SESSION_403"),
+ "a refusal signed the user out: {source}: {reason}"
+ );
+ }
+ Ok(_) | Err(tinybus::TryRecvError::Lagged(_)) => continue,
+ Err(_) => break,
+ }
+ }
+}
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_session.rs b/crates/openhuman-tinyhumans/src/inference/managed_session.rs
index 88caeb3648..97f701ac0a 100644
--- a/crates/openhuman-tinyhumans/src/inference/managed_session.rs
+++ b/crates/openhuman-tinyhumans/src/inference/managed_session.rs
@@ -4,7 +4,7 @@
use tinyinference_llm::model::ProviderError;
use tinyinference_llm::providers::tinyhumans::{
- TinyHumansAuthFailure, TinyHumansAuthFailureKind, TinyHumansOperation,
+ TinyHumansAuthFailure, TinyHumansAuthFailureKind, TinyHumansCredentialKind, TinyHumansOperation,
};
/// Resolve the persisted app-session token for a managed inference caller.
@@ -149,15 +149,14 @@ fn maybe_publish_local_session_expiry(operation: &str) {
}
/// Publish expiry only from TinyInference's explicit managed-endpoint
-/// provenance, and only for a 401 on the session credential. A 403 is a
-/// refusal (plan, tier, WAF) and an API-key 401 is a rejected key; neither
-/// signs the user out. The provider label is a wire diagnostic and is not an
-/// auth ownership signal.
-pub(super) fn publish_managed_auth_failure(
- error: &ProviderError,
- context: TinyHumansAuthFailure,
- api_key: bool,
-) {
+/// provenance, and only for a 401 on the session credential that this failing
+/// request was sent with (not whichever credential the model used last). A
+/// failure without credential provenance is never treated as the session. A
+/// 403 is a refusal (plan, tier, WAF) and an API-key 401 is a rejected key;
+/// neither signs the user out. The provider label is a wire diagnostic and is
+/// not an auth ownership signal.
+pub(super) fn publish_managed_auth_failure(error: &ProviderError, context: TinyHumansAuthFailure) {
+ let api_key = context.credential() != Some(TinyHumansCredentialKind::Session);
let status = match context.kind() {
TinyHumansAuthFailureKind::Unauthorized => 401,
TinyHumansAuthFailureKind::Forbidden => 403,