Skip to content

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

refactor(inference): drop managed call, usage and reasoning test modules

Steven Enamakelf9673e5d67d1
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_calls.rs b/crates/openhuman-tinyhumans/src/inference/managed_calls.rs
deleted file mode 100644
index f16c9db35b..0000000000
--- a/crates/openhuman-tinyhumans/src/inference/managed_calls.rs
+++ /dev/null
@@ -1,160 +0,0 @@
-//! The [`ChatModel`] implementation of the managed model: per-call wire model,
-//! reasoning and thread extensions, usage projection and failure observation.
-
-use async_trait::async_trait;
-use tinyinference_llm::model::{
- ChatModel, ModelProfile, ModelRequest, ModelResponse, ModelStream, ModelStreamItem,
- ProviderError,
-};
-use tinyinference_llm::Error as TiError;
-
-use super::managed_model::OpenHumanBackendModel;
-use super::managed_reasoning::{apply_reasoning_hint, with_thread_id};
-use super::managed_session::{
- maybe_publish_provider_session_expired, maybe_publish_session_expired,
-};
-use super::managed_usage::project_managed_usage;
-
-/// Log the raw upstream failure at the managed inference dispatch boundary
-/// (#5503, part d). The managed unavailability path used to surface the true
-/// backend cause only after the web-chat error classifier had already collapsed
-/// it to a user-facing bucket, so an operator investigating "all tiers died
-/// over hours" had no record of what the backend actually returned. This is the
-/// one place every managed `invoke`/`stream` failure passes through, so it's
-/// where the diagnostic belongs. Structured fields (`status`/`code`/`provider`/
-/// `retryable`) are low-cardinality; the message is secret-scrubbed and capped
-/// by [`sanitize_api_error`] before it's logged — no tokens, no full PII.
-pub(super) fn log_managed_dispatch_error(err: &TiError, operation: &str) {
- match err {
- TiError::Provider(pe) => log_managed_provider_error(pe, operation),
- other => {
- log::warn!(
- "[providers][openhuman-backend] managed {operation} failed (non-provider error): {}",
- tinyinference_core::sanitize::sanitize_api_error(&other.to_string()),
- );
- }
- }
-}
-
-/// Logs a structured provider failure; the detail is secret-scrubbed and
-/// truncated because a provider error can echo request content.
-pub(super) fn log_managed_provider_error(pe: &ProviderError, operation: &str) {
- log::warn!(
- "[providers][openhuman-backend] managed {operation} failed: status={:?} code={:?} provider={} retryable={} detail={}",
- pe.status,
- pe.code,
- pe.provider,
- pe.retryable,
- tinyinference_core::sanitize::sanitize_api_error(&pe.message),
- );
-}
-
-/// Gives a failure reported inside a stream the same handling as a failed
-/// `stream()` call: logged, and an expired session starts re-authentication.
-pub(super) fn observe_in_band_failure(item: &ModelStreamItem) {
- if let ModelStreamItem::ProviderFailed(pe) = item {
- log_managed_provider_error(pe, "stream (in-band)");
- maybe_publish_provider_session_expired(pe, "stream");
- }
-}
-
-#[async_trait]
-impl ChatModel<()> for OpenHumanBackendModel {
- fn profile(&self) -> Option<&ModelProfile> {
- Some(&self.profile)
- }
-
- fn supports_input(
- &self,
- modality: tinyinference_llm::model::InputModality,
- mime: &str,
- source: tinyinference_llm::model::InputSource,
- ) -> bool {
- use tinyinference_llm::model::{InputModality, InputSource};
- matches!(
- (modality, source),
- (InputModality::Image, InputSource::Base64 | InputSource::Url)
- ) && matches!(
- mime,
- "image/png" | "image/jpeg" | "image/webp" | "image/gif"
- ) || matches!(
- (modality, source),
- (InputModality::Audio, InputSource::Base64)
- ) && matches!(
- mime,
- "audio/wav" | "audio/x-wav" | "audio/mpeg" | "audio/mp3"
- )
- }
-
- /// Identity for harness response-cache scoping: the backend base URL and
- /// the default tier/model. The session JWT is deliberately absent — it
- /// rotates, and a key derived from it would never hit twice — and the
- /// backend resolves the tier per account anyway, so two accounts sharing
- /// a cache would need their own namespace, not a credential in the key.
- fn cache_identity(&self) -> Option<String> {
- self.base_url()
- .ok()
- .map(|base| format!("openhuman:{base}:{}", self.default_model))
- }
-
- async fn invoke(
- &self,
- state: &(),
- request: ModelRequest,
- ) -> tinyinference_llm::Result<ModelResponse> {
- let model = self.build_wire_model()?;
- let response = match model
- .invoke(
- state,
- with_thread_id(apply_reasoning_hint(request), self.thread_id.as_deref()),
- )
- .await
- {
- Ok(response) => response,
- Err(e) => {
- log_managed_dispatch_error(&e, "invoke");
- maybe_publish_session_expired(&e, "invoke");
- return Err(e);
- }
- };
- Ok(project_managed_usage(response))
- }
-
- async fn stream(
- &self,
- state: &(),
- request: ModelRequest,
- ) -> tinyinference_llm::Result<ModelStream> {
- let model = self.build_wire_model()?;
- // The backend sends its charge on an `openhuman-metadata` frame before
- // `[DONE]`; the crate's SSE parser keeps that envelope on the terminal
- // response's `raw`, so the streamed `Completed` is projected exactly like
- // the `invoke` path above and carries the charged USD.
- match model
- .stream(
- state,
- with_thread_id(apply_reasoning_hint(request), self.thread_id.as_deref()),
- )
- .await
- {
- // A failure can also arrive *inside* an HTTP 200 stream as an SSE
- // `{"error":…}` payload; it never reaches the `Err` arm (#6724).
- Ok(stream) => Ok(stream.map_items(|item| {
- observe_in_band_failure(&item);
- match item {
- tinyinference_llm::model::ModelStreamItem::Completed(response) => {
- tinyinference_llm::model::ModelStreamItem::Completed(project_managed_usage(
- response,
- ))
- }
- other => other,
- }
- })),
- Err(e) => {
- log_managed_dispatch_error(&e, "stream");
- maybe_publish_session_expired(&e, "stream");
- Err(e)
- }
- }
- }
-}
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_model_reasoning_tests.rs b/crates/openhuman-tinyhumans/src/inference/managed_model_reasoning_tests.rs
deleted file mode 100644
index 67de5318fc..0000000000
--- a/crates/openhuman-tinyhumans/src/inference/managed_model_reasoning_tests.rs
+++ /dev/null
@@ -1,159 +0,0 @@
-//! The reasoning-off hint and request reasoning on the managed wire.
-
-use super::*;
-
-#[test]
-fn reasoning_hint_becomes_disabled_reasoning_on_the_managed_wire() {
- let request = apply_reasoning_hint(without_reasoning(ModelRequest::new(vec![Message::user(
- "hi",
- )])));
- assert_eq!(
- request.provider_options["reasoning"],
- serde_json::json!({ "enabled": false })
- );
-}
-
-#[test]
-fn no_hint_leaves_provider_options_untouched() {
- let request = apply_reasoning_hint(ModelRequest::new(vec![Message::user("hi")]));
- assert!(request.provider_options.get("reasoning").is_none());
-}
-
-#[test]
-fn request_reasoning_effort_becomes_the_managed_reasoning_object() {
- use tinyinference_llm::model::{ReasoningConfig, ReasoningEffort};
- let request = apply_reasoning_hint(
- ModelRequest::new(vec![Message::user("hi")])
- .with_reasoning(ReasoningConfig::effort(ReasoningEffort::High)),
- );
- assert_eq!(
- request.provider_options["reasoning"],
- serde_json::json!({ "effort": "high" })
- );
- assert!(
- request.reasoning.is_none(),
- "the neutral field is consumed so the transport sends no second `reasoning_effort`"
- );
-}
-
-#[test]
-fn request_reasoning_none_disables_reasoning_on_the_managed_wire() {
- use tinyinference_llm::model::{ReasoningConfig, ReasoningEffort};
- let request = apply_reasoning_hint(
- ModelRequest::new(vec![Message::user("hi")])
- .with_reasoning(ReasoningConfig::effort(ReasoningEffort::None)),
- );
- assert_eq!(
- request.provider_options["reasoning"],
- serde_json::json!({ "enabled": false })
- );
-}
-
-#[test]
-fn request_reasoning_budget_becomes_max_tokens() {
- use tinyinference_llm::model::{ReasoningConfig, ReasoningEffort};
- let request = apply_reasoning_hint(
- ModelRequest::new(vec![Message::user("hi")]).with_reasoning(ReasoningConfig {
- effort: Some(ReasoningEffort::High),
- budget_tokens: Some(8_000),
- summary: None,
- }),
- );
- assert_eq!(
- request.provider_options["reasoning"],
- serde_json::json!({ "max_tokens": 8000 })
- );
-}
-
-#[test]
-fn suggestion_off_hint_wins_over_request_reasoning() {
- use tinyinference_llm::model::{ReasoningConfig, ReasoningEffort};
- let request = apply_reasoning_hint(
- without_reasoning(ModelRequest::new(vec![Message::user("hi")]))
- .with_reasoning(ReasoningConfig::effort(ReasoningEffort::Low)),
- );
- assert_eq!(
- request.provider_options["reasoning"],
- serde_json::json!({ "enabled": false })
- );
-}
-
-#[test]
-fn explicit_reasoning_option_wins_over_the_hint() {
- let request = without_reasoning(ModelRequest::new(vec![Message::user("hi")]))
- .with_provider_options(serde_json::json!({ "reasoning": { "effort": "high" } }));
- let request = apply_reasoning_hint(request);
- assert_eq!(
- request.provider_options["reasoning"],
- serde_json::json!({ "effort": "high" })
- );
-}
-
-/// Captures the JSON body of every chat-completions request it receives.
-async fn spawn_capturing_chat_server() -> (String, std::sync::Arc<std::sync::Mutex<Vec<Value>>>) {
- let bodies = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
- let seen = bodies.clone();
- let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
- .await
- .expect("bind");
- let addr = listener.local_addr().expect("local_addr").to_string();
- let app = axum::Router::new().route(
- "/openai/v1/chat/completions",
- axum::routing::post(move |axum::Json(body): axum::Json<Value>| {
- let seen = seen.clone();
- async move {
- seen.lock().unwrap().push(body);
- axum::Json(serde_json::json!({
- "id": "chatcmpl-capture",
- "object": "chat.completion",
- "created": 1,
- "model": "reasoning-v1",
- "choices": [{
- "index": 0,
- "message": { "role": "assistant", "content": "[]" },
- "finish_reason": "stop"
- }],
- "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
- }))
- }
- }),
- );
- tokio::spawn(async move {
- axum::serve(listener, app).await.expect("serve");
- });
- (addr, bodies)
-}
-
-#[tokio::test]
-async fn managed_call_sends_reasoning_disabled_only_when_hinted() {
- let tmp = tempfile::TempDir::new().unwrap();
- seed_app_session(tmp.path());
- let (addr, bodies) = spawn_capturing_chat_server().await;
- let backend = backend_pointed_at(&addr, tmp.path());
-
- backend
- .invoke(
- &(),
- without_reasoning(ModelRequest::new(vec![Message::user("suggest")])),
- )
- .await
- .expect("hinted call");
- backend
- .invoke(&(), ModelRequest::new(vec![Message::user("chat")]))
- .await
- .expect("plain call");
-
- let bodies = bodies.lock().unwrap();
- assert_eq!(bodies.len(), 2);
- assert_eq!(
- bodies[0]["reasoning"],
- serde_json::json!({ "enabled": false })
- );
- assert!(
- bodies[1].get("reasoning").is_none(),
- "an unhinted call must not change reasoning: {}",
- bodies[1]
- );
- // The hint itself never reaches the wire.
- assert!(!bodies[0].to_string().contains("openhuman_reasoning_off"));
-}
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_session.rs b/crates/openhuman-tinyhumans/src/inference/managed_session.rs
index 8bf99e051f..7a0da46230 100644
--- a/crates/openhuman-tinyhumans/src/inference/managed_session.rs
+++ b/crates/openhuman-tinyhumans/src/inference/managed_session.rs
@@ -4,7 +4,6 @@
use tinyinference_llm::model::ProviderError;
use tinyinference_llm::providers::tinyhumans::{TinyHumansAuthFailure, TinyHumansAuthFailureKind, TinyHumansOperation};
-use tinyinference_llm::Error as TiError;
/// Resolve the persisted app-session token for a managed inference caller.
/// Both chat and embeddings use this path so signed-out state, expiry checks,
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_session_tests.rs b/crates/openhuman-tinyhumans/src/inference/managed_session_tests.rs
index 32e0fda233..401bdba827 100644
--- a/crates/openhuman-tinyhumans/src/inference/managed_session_tests.rs
+++ b/crates/openhuman-tinyhumans/src/inference/managed_session_tests.rs
@@ -1,9 +1,7 @@
use super::*;
// #6932: the offline local profile is a valid sign-in with no TinyHumans
-// account behind it. `classify_session_token` reports its `exp`-less token
-// `Live`, so managed inference used to send it, collect a backend
-// `401 "Invalid token"` and surface that as an expired session.
+// account behind it. It must never be sent as a managed inference bearer.
#[test]
fn the_offline_local_session_cannot_authenticate_managed_inference() {
use openhuman_embed::__host::security::credentials::session_support::{
@@ -16,28 +14,12 @@ fn the_offline_local_session_cannot_authenticate_managed_inference() {
)
.expect_err("a local session has no managed bearer");
- assert_eq!(
- error.to_string(),
- LOCAL_SESSION_MANAGED_INFERENCE_UNAVAILABLE
- );
-}
-
-#[test]
-fn the_refusal_does_not_read_as_an_expired_session() {
- use openhuman_embed::__host::security::credentials::session_support::SessionTokenCheck;
-
- let error = managed_bearer(
- SessionTokenCheck::Live("header.payload.local".to_string()),
- "openhuman_backend_model.resolve_bearer",
- )
- .expect_err("a local session has no managed bearer");
-
- // The whole point of the fix: this must not reach the sign-out path that
- // the backend's 401 envelope used to trigger.
+ assert_eq!(error.to_string(), LOCAL_SESSION_MANAGED_INFERENCE_UNAVAILABLE);
assert!(
!openhuman_embed::__host::core::observability::is_session_expired_message(
&error.to_string()
- )
+ ),
+ "the offline local session is unavailable, not expired"
);
}
@@ -53,48 +35,3 @@ fn a_signed_in_session_still_authenticates_managed_inference() {
assert_eq!(bearer, "header.payload.signature");
}
-
-use tinyinference_llm::model::{ModelStreamItem, ProviderError};
-
-/// #6724 (review): a 401 reported *inside* a stream must start re-auth just
-/// like a failed `stream()` call does.
-#[tokio::test]
-async fn an_in_band_401_publishes_session_expired() {
- use openhuman_embed::__host::core::events::DomainEvent;
-
- openhuman_embed::__host::core::bus::init()
- .await
- .expect("bus init");
- let mut rx = openhuman_embed::__host::core::bus::BUS
- .get()
- .expect("event bus initialized")
- .receiver();
-
- super::super::managed_calls::observe_in_band_failure(&ModelStreamItem::ProviderFailed(
- ProviderError {
- provider: super::super::managed_model::PROVIDER_LABEL.to_string(),
- status: Some(401),
- message: "TEST_MARKER_IN_BAND token expired".to_string(),
- ..ProviderError::default()
- },
- ));
-
- let mut source_seen = None;
- loop {
- match rx.try_recv() {
- Ok(DomainEvent::SessionExpired { source, reason })
- if reason.contains("TEST_MARKER_IN_BAND") =>
- {
- source_seen = Some(source);
- break;
- }
- Ok(_) | Err(tinybus::TryRecvError::Lagged(_)) => continue,
- Err(_) => break,
- }
- }
- assert_eq!(
- source_seen.as_deref(),
- Some("openhuman_backend_model.stream(401)"),
- "an in-band 401 must publish SessionExpired"
- );
-}
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_usage.rs b/crates/openhuman-tinyhumans/src/inference/managed_usage.rs
deleted file mode 100644
index 333e1a4945..0000000000
--- a/crates/openhuman-tinyhumans/src/inference/managed_usage.rs
+++ /dev/null
@@ -1,78 +0,0 @@
-//! Re-projection of the managed backend's `openhuman` response envelope into
-//! the metadata the host cost bridge reads. Split out of
-//! `openhuman_backend_model.rs` to keep it under the line cap.
-
-use tinyinference_llm::model::ModelResponse;
-
-/// The subset of the managed backend's `openhuman` response envelope the crate
-/// `Usage`/`ModelResponse` can't carry — billing + cache tokens — so it can be
-/// re-projected for the host cost bridge.
-#[derive(Debug, Default, serde::Deserialize)]
-pub(super) struct ManagedEnvelope {
- #[serde(default)]
- usage: Option<ManagedEnvelopeUsage>,
- #[serde(default)]
- billing: Option<ManagedEnvelopeBilling>,
-}
-
-#[derive(Debug, Default, serde::Deserialize)]
-pub(super) struct ManagedEnvelopeUsage {
- #[serde(default)]
- cached_input_tokens: Option<u64>,
- #[serde(default)]
- context_window: Option<u64>,
-}
-
-#[derive(Debug, Default, serde::Deserialize)]
-pub(super) struct ManagedEnvelopeBilling {
- #[serde(default)]
- charged_amount_usd: f64,
-}
-
-/// Re-project the managed `openhuman.{billing,usage}` envelope — which the crate
-/// `OpenAiModel` leaves only on `ModelResponse.raw` — into the metadata the host
-/// cost bridge reads: `openhuman_usage_meta` (charged USD + context window) plus a
-/// crate `Usage.cache_read_tokens` reconciliation when the crate missed the
-/// envelope's cached count. Parity with the legacy model-adapter path's
-/// `usage_info_from_response`; without it the crate-native managed turn reports
-/// `$0` charged and drops backend-reported cached tokens.
-pub(super) fn project_managed_usage(mut response: ModelResponse) -> ModelResponse {
- let envelope: ManagedEnvelope = response
- .raw
- .as_ref()
- .and_then(|raw| raw.get("openhuman"))
- .and_then(|oh| serde_json::from_value(oh.clone()).ok())
- .unwrap_or_default();
-
- // A `billing` block is the backend's statement of what it debited, so it
- // is a known charge even at zero; no block means no charge was reported.
- let charged_amount_usd = envelope.billing.map(|b| b.charged_amount_usd);
- let context_window = envelope
- .usage
- .as_ref()
- .and_then(|u| u.context_window)
- .unwrap_or(0);
-
- // The `openhuman.usage` cached count is authoritative (the legacy `extract_usage`
- // preferred it over the standard block); backfill it when the crate's standard
- // parse produced none.
- if let (Some(usage), Some(cached)) = (
- response.usage.as_mut(),
- envelope.usage.as_ref().and_then(|u| u.cached_input_tokens),
- ) {
- if usage.cache_read_tokens == 0 {
- usage.cache_read_tokens = cached;
- }
- }
-
- response.raw = tinyinference_llm::usage::merge_usage_meta(
- response.raw,
- charged_amount_usd,
- context_window,
- );
- response
-}
-
-#[cfg(test)]
-#[path = "managed_usage_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_usage_tests.rs b/crates/openhuman-tinyhumans/src/inference/managed_usage_tests.rs
deleted file mode 100644
index 748bc0efd8..0000000000
--- a/crates/openhuman-tinyhumans/src/inference/managed_usage_tests.rs
+++ /dev/null
@@ -1,98 +0,0 @@
-use super::*;
-use tinyinference_llm::model::ModelResponse;
-use tinyinference_llm::usage::recover_usage;
-
-/// The managed `openhuman.{billing,usage}` envelope on `raw` must re-project
-/// into the host `BilledUsage` the cost bridge reads — charged USD, cached
-/// tokens, and context window — exactly as the legacy legacy model-adapter path did.
-#[test]
-fn project_managed_usage_recovers_charged_and_cached() {
- use tinyinference_llm::message::AssistantMessage;
- use tinyinference_llm::usage::Usage;
-
- let raw = serde_json::json!({
- "openhuman": {
- "usage": { "cached_input_tokens": 128, "context_window": 200000 },
- "billing": { "charged_amount_usd": 0.0042 }
- }
- });
- let response = ModelResponse {
- message: AssistantMessage {
- id: None,
- content: vec![],
- tool_calls: vec![],
- usage: None,
- origin: None,
- },
- usage: Some(Usage {
- input_tokens: 1000,
- output_tokens: 50,
- ..Usage::default()
- }),
- finish_reason: None,
- raw: Some(raw),
- resolved_model: None,
- continue_turn: None,
- served_from_cache: false,
- correlation: None,
- resolved_route: None,
- };
-
- let projected = project_managed_usage(response);
- let recovered = recover_usage(&projected).expect("usage recovered");
- let charged = recovered.charged_amount_usd.expect("charge recovered");
- assert!((charged - 0.0042).abs() < 1e-9, "charged={charged}");
- assert_eq!(
- recovered.usage.cache_read_tokens, 128,
- "cached tokens backfilled"
- );
- assert_eq!(recovered.context_window, 200_000);
- assert_eq!(recovered.usage.input_tokens, 1000);
- assert_eq!(recovered.usage.output_tokens, 50);
-}
-
-/// A response with no `openhuman` envelope stays untouched — no meta key, no
-/// charged USD — so non-managed/billing-free responses aren't fabricated.
-#[test]
-fn project_managed_usage_is_noop_without_envelope() {
- use tinyinference_llm::message::AssistantMessage;
- use tinyinference_llm::usage::Usage;
-
- let response = ModelResponse {
- message: AssistantMessage {
- id: None,
- content: vec![],
- tool_calls: vec![],
- usage: None,
- origin: None,
- },
- usage: Some(Usage {
- input_tokens: 10,
- output_tokens: 5,
- cache_read_tokens: 3,
- ..Usage::default()
- }),
- finish_reason: None,
- raw: Some(serde_json::json!({ "id": "resp_1" })),
- resolved_model: None,
- continue_turn: None,
- served_from_cache: false,
- correlation: None,
- resolved_route: None,
- };
-
- let projected = project_managed_usage(response);
- // raw keeps only the wire fields — no meta key injected.
- assert!(projected
- .raw
- .as_ref()
- .unwrap()
- .get("openhuman_usage_meta")
- .is_none());
- let recovered = recover_usage(&projected).expect("usage present");
- assert_eq!(recovered.charged_amount_usd, None);
- assert_eq!(
- recovered.usage.cache_read_tokens, 3,
- "crate cached preserved"
- );
-}