Skip to content

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

chore: files changed crates/openhuman-core/src/inference/provider/factory.rs,crates/openhuman-core/s

Steven Enamakelaebf8a585c17
diff --git a/crates/openhuman-core/src/inference/provider/factory.rs b/crates/openhuman-core/src/inference/provider/factory.rs
index addb0c2767..6ed8757f85 100644
--- a/crates/openhuman-core/src/inference/provider/factory.rs
+++ b/crates/openhuman-core/src/inference/provider/factory.rs
@@ -90,10 +90,7 @@ use local_runtime::{
OptionalChatModelResult,
};
pub(crate) use managed_backend::make_openhuman_backend_model_for_thread;
-use managed_backend::{
- registered_managed_chat_model, resolve_managed_backend,
- resolve_managed_backend_with_model_override,
-};
+use managed_backend::{managed_chat_model, resolve_managed_model};
use primary_cloud::{legacy_inference_slug, resolve_primary_cloud_provider_string};
use subprocess_providers::{
prepare_claude_agent_sdk_chat_model, try_create_claude_agent_sdk_chat_model,
@@ -125,7 +122,6 @@ mod factory_tests;
use crate::config::schema::cloud_providers::AuthStyle;
use crate::config::Config;
use crate::inference::provider::openai_codex::resolve_openai_codex_routing;
-use crate::inference::provider::openhuman_backend_model::OpenHumanBackendModel;
use crate::inference::provider::ProviderRuntimeOptions;
use crate::security::credentials::AuthService;
use crate::security::egress::EgressLedger;
diff --git a/crates/openhuman-core/src/inference/provider/factory/chat_model.rs b/crates/openhuman-core/src/inference/provider/factory/chat_model.rs
index 647c8c27b6..6a16a4be1e 100644
--- a/crates/openhuman-core/src/inference/provider/factory/chat_model.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/chat_model.rs
@@ -56,7 +56,7 @@ pub(super) fn create_chat_model_with_model_id_inner(
return Ok((model, "mock-model".to_string()));
}
// Managed OpenHuman backend → crate-native host `ChatModel`
- // ([`OpenHumanBackendModel`], issue #4727 Motion B) instead of a
+ // (issue #4727 Motion B) instead of a
// adapted provider. A native test-model override must still win, so only
// take this path when no
// override is installed. The public wrapper supplies the construction-time
@@ -133,7 +133,7 @@ pub(crate) fn resolves_to_managed_backend(role: &str, config: &Config) -> bool {
/// unknown provider slug, local-only privacy-mode block, …) reused
/// verbatim so this probe never re-implements it.
/// 2. **Managed-backend readiness** — when `role` resolves to the managed
-/// OpenHuman backend, [`OpenHumanBackendModel::probe_readiness`] makes one
+/// OpenHuman backend, the registered provider's readiness probe makes one
/// cheap real completion attempt to catch the "account has no provider API
/// key configured" class of failure that construction alone cannot see
/// (construction only builds the client; it never calls the backend).
@@ -177,15 +177,15 @@ pub async fn probe_inference_readiness(
"[flows][inference-probe] role={role} resolves to the managed OpenHuman backend — \
probing readiness"
);
- if let Some(probe) =
+ let Some(probe) =
crate::inference::registry::chat_model(PROVIDER_OPENHUMAN).and_then(|entry| entry.probe)
- {
- log::debug!("[flows][inference-probe] role={role} probing via the registered chat model");
- return probe(role.to_string(), config.clone()).await;
- }
- let (managed_model, model_id) =
- resolve_managed_backend(role, config, &EgressLedger::none()).map_err(|e| e.to_string())?;
- let result = managed_model.probe_readiness().await;
+ else {
+ // No managed provider is registered: construction above already
+ // answered `BACKEND_UNAVAILABLE:`, so this is unreachable in practice.
+ return Err(super::managed_backend::managed_unavailable_error().to_string());
+ };
+ let model_id = resolve_managed_model(role, config, None);
+ let result = probe(role.to_string(), model_id.clone(), config.clone()).await;
log::debug!(
"[flows][inference-probe] role={role} model={model_id} probe result: {}",
if result.is_ok() { "ready" } else { "not ready" }
diff --git a/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs b/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs
index 52670f19a8..2c1258e477 100644
--- a/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs
@@ -338,7 +338,7 @@ pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools(
}
let model_override =
(!effective_model.trim().is_empty()).then_some(effective_model.as_str());
- if let Some(registered) = registered_managed_chat_model(
+ return Some(managed_chat_model(
role,
config,
model_override,
@@ -346,22 +346,7 @@ pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools(
native_tool_calling,
None,
egress,
- ) {
- return Some(registered);
- }
- let (backend, pinned_model) = match resolve_managed_backend_with_model_override(
- role,
- config,
- model_override,
- egress,
- ) {
- Ok(result) => result,
- Err(error) => return Some(Err(error)),
- };
- return Some(Ok((
- Arc::new(backend.with_native_tool_calling(native_tool_calling)),
- pinned_model,
- )));
+ ));
}
AuthStyle::Bearer => {
// The codex routing may re-target the endpoint (OAuth backend).
diff --git a/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs b/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs
index 2e6769c3d9..ff19ac6228 100644
--- a/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs
@@ -1,143 +1,40 @@
-//! The managed OpenHuman backend as a crate-native `ChatModel`: tier pinning per
-//! workload role, `hint:*` translation, and the single managed egress emission.
+//! The managed backend as a crate-native `ChatModel`: tier pinning per
+//! workload role, `hint:*` translation, the privacy gate and the single managed
+//! egress emission. The model itself is built by the chat-model provider
+//! registered under [`PROVIDER_OPENHUMAN`]
+//! ([`crate::inference::registry::register_chat_model`]); with none registered
+//! the managed provider is unavailable and construction answers with the
+//! `BACKEND_UNAVAILABLE:` sentinel.
use super::*;
-/// The managed provider as a registered chat model
-/// ([`crate::inference::registry::register_chat_model`]), or `None` when
-/// nothing is registered under [`PROVIDER_OPENHUMAN`] and the built-in backend
-/// applies. The privacy gate runs first, exactly as for the built-in path.
-pub(super) fn registered_managed_chat_model(
- role: &str,
- config: &Config,
- model_override: Option<&str>,
- default_model: Option<&str>,
- native_tool_calling: bool,
- thread_id: Option<&str>,
- egress: &EgressLedger,
-) -> Option<
- anyhow::Result<(
- std::sync::Arc<dyn tinyinference_llm::model::ChatModel<()>>,
- String,
- )>,
-> {
- let entry = crate::inference::registry::chat_model(PROVIDER_OPENHUMAN)?;
- log::debug!(
- "[providers][chat-factory] role={role} managed provider served by a registered chat model"
- );
- if let Err(error) = enforce_local_only_inference(role, PROVIDER_OPENHUMAN) {
- return Some(Err(error));
- }
- Some((entry.build)(
- &crate::inference::registry::ChatModelRequest {
- role,
- config,
- model_override,
- default_model,
- native_tool_calling,
- thread_id,
- egress,
- },
- ))
-}
-
-/// Resolve the managed OpenHuman backend for `role` — the model id (tier /
-/// summarization / default, with `hint:<tier>` translation) plus a configured
-/// [`OpenHumanBackendModel`]. Shared by both the `Provider` path
-/// ([`make_openhuman_backend`]) and the crate `ChatModel` path
-/// ([`make_openhuman_backend_model`], issue #4727 Motion B).
-pub(super) fn resolve_managed_backend(
- role: &str,
- config: &Config,
- egress: &EgressLedger,
-) -> anyhow::Result<(OpenHumanBackendModel, String)> {
- resolve_managed_backend_with_model_override(role, config, None, egress)
-}
-
-/// Construct a managed turn model with the explicit OpenHuman thread attached.
-/// This intentionally exposes no generic backend resolver outside the factory.
-pub(crate) fn make_openhuman_backend_model_for_thread(
- role: &str,
- config: &Config,
- model: &str,
- native_tool_calling: bool,
- thread_id: Option<&str>,
- egress: &EgressLedger,
-) -> anyhow::Result<(
+type ManagedChatModel = (
std::sync::Arc<dyn tinyinference_llm::model::ChatModel<()>>,
String,
-)> {
- if let Some(registered) = registered_managed_chat_model(
- role,
- config,
- Some(model),
- None,
- native_tool_calling,
- thread_id,
- egress,
- ) {
- return registered;
- }
- // `model` may be a role alias (`hint:reasoning`) the registry keys the
- // route by; the backend only ever sees the concrete id it resolves to.
- let (backend, resolved_model) =
- resolve_managed_backend_with_model_override(role, config, Some(model), egress)?;
- Ok((
- std::sync::Arc::new(
- backend
- .with_native_tool_calling(native_tool_calling)
- .with_thread_id(thread_id),
- ),
- resolved_model,
- ))
-}
+);
-pub(super) fn resolve_managed_backend_with_model_override(
+/// The concrete model id a managed route runs on: the pinned default (else
+/// `MODEL_MANAGED_DEFAULT`), unless `model_override` names something.
+///
+/// An override that is itself a managed alias (`hint:coding`, `hint:chat`)
+/// means "this role on the managed backend": the default model. A concrete id
+/// (`openrouter/deepseek/deepseek-v4-pro`) is forwarded verbatim; the backend
+/// is authoritative over its validity (issue #4598).
+pub(super) fn resolve_managed_model(
role: &str,
config: &Config,
model_override: Option<&str>,
- egress: &EgressLedger,
-) -> anyhow::Result<(OpenHumanBackendModel, String)> {
- // Managed construction shares the same privacy gate as local and BYOK
- // factories, before constructing a client or announcing any egress.
- enforce_local_only_inference(role, PROVIDER_OPENHUMAN)?;
-
+) -> String {
// Every managed role runs on one concrete model: the pinned default, else
- // `MODEL_MANAGED_DEFAULT`. Roles still matter — they picked the *route*
- // that led here — but there are no per-role tier endpoints any more.
+ // `MODEL_MANAGED_DEFAULT`. Roles still matter (they picked the *route* that
+ // led here) but there are no per-role tier endpoints any more.
let model = managed_default_model(config);
log::debug!(
"[providers][chat-factory] role={} managed resolves to model={}",
role,
model
);
- // Critical: pass the *config's* workspace directory through so the
- // provider's `AuthService` reads `auth-profiles.json` from the
- // same dir login wrote to. Without this, `ProviderRuntimeOptions::default()`
- // leaves `openhuman_dir = None`, the provider falls back to
- // `~/.openhuman`, and reads an unrelated (or empty)
- // profile store — surfacing as "No backend session: store a JWT
- // via auth (app-session)" even though login just succeeded in the
- // user's actual workspace (e.g. test workspaces under OPENHUMAN_WORKSPACE).
- let options = ProviderRuntimeOptions {
- openhuman_dir: config.config_path.parent().map(std::path::PathBuf::from),
- secrets_encrypt: config.secrets.encrypt,
- ..ProviderRuntimeOptions::default()
- };
- log::debug!(
- "[providers][chat-factory] building openhuman backend provider model={} state_dir={:?} secrets_encrypt={}",
- model,
- options.openhuman_dir,
- options.secrets_encrypt
- );
- // `model` is the managed default here, never a hint or a retired tier
- // slug; a caller's own `hint:*` / tier value only ever arrives through
- // `model_override`, translated below.
- // An override that is itself a managed alias (`hint:coding`, `hint:chat`)
- // means "this role on the managed backend" — the default model. A concrete
- // id (`openrouter/deepseek/deepseek-v4-pro`) is forwarded verbatim; the
- // backend is authoritative over its validity (issue #4598).
- let model = match model_override.map(str::trim).filter(|m| !m.is_empty()) {
+ match model_override.map(str::trim).filter(|m| !m.is_empty()) {
Some(raw) if is_known_openhuman_tier(raw) => {
log::debug!(
"[providers][chat-factory] role={} override '{}' is a managed alias; using model={}",
@@ -156,43 +53,82 @@ pub(super) fn resolve_managed_backend_with_model_override(
raw.to_string()
}
None => model,
- };
+ }
+}
+
+/// The error the managed provider answers with when no chat-model provider is
+/// registered for it.
+pub(crate) fn managed_unavailable_error() -> anyhow::Error {
+ anyhow::anyhow!(
+ "{} managed inference needs a hosted provider; none is registered",
+ crate::core::observability::BACKEND_UNAVAILABLE_PREFIX
+ )
+}
- // Egress spine (privacy epic S2, #4436): managed backend resolution is the
- // universal chokepoint for EVERY managed-backend inference construction —
- // the direct ChatModel path and both turn paths
- // (`create_turn_chat_model[_from_string]_with_native_tools`) resolve here.
- // Emitting once here guarantees the default managed chat turn discloses
- // egress exactly once (see `emit_inference_egress`).
+/// Build the managed model for `role` through the registered chat-model
+/// provider. The privacy gate runs first (before a client is constructed or any
+/// egress announced), then the model id is resolved and the egress disclosed
+/// once: managed resolution is the universal chokepoint for every managed
+/// inference construction (privacy epic S2, #4436).
+pub(super) fn managed_chat_model(
+ role: &str,
+ config: &Config,
+ model_override: Option<&str>,
+ default_model: Option<&str>,
+ native_tool_calling: bool,
+ thread_id: Option<&str>,
+ egress: &EgressLedger,
+) -> anyhow::Result<ManagedChatModel> {
+ enforce_local_only_inference(role, PROVIDER_OPENHUMAN)?;
+ let Some(entry) = crate::inference::registry::chat_model(PROVIDER_OPENHUMAN) else {
+ log::debug!("[providers][chat-factory] role={role} managed provider is not registered");
+ return Err(managed_unavailable_error());
+ };
+ let model = resolve_managed_model(role, config, model_override);
+ log::debug!(
+ "[providers][chat-factory] role={role} managed provider served by a registered chat model model={model}"
+ );
crate::security::egress::emit_external_transfer_in(
egress,
crate::security::egress::EgressDescriptor::inference("openhuman", &model, true),
);
- Ok((
- OpenHumanBackendModel::new(config.api_url.as_deref(), &options, model.clone()),
- model,
- ))
+ (entry.build)(&crate::inference::registry::ChatModelRequest {
+ role,
+ config,
+ model_override: Some(&model),
+ default_model,
+ native_tool_calling,
+ thread_id,
+ egress,
+ })
}
-/// The managed OpenHuman backend as a crate-native host `ChatModel`
-/// ([`OpenHumanBackendModel`], issue #4727 Motion B) — the cutover replacement
-/// for the `Provider` path. Same resolution; wraps the backend so the harness
-/// holds a crate `ChatModel` and the dynamic JWT + `thread_id` + billing envelope
-/// are bridged onto the crate wire client per call.
+/// Construct a managed turn model with the explicit OpenHuman thread attached.
+pub(crate) fn make_openhuman_backend_model_for_thread(
+ role: &str,
+ config: &Config,
+ model: &str,
+ native_tool_calling: bool,
+ thread_id: Option<&str>,
+ egress: &EgressLedger,
+) -> anyhow::Result<ManagedChatModel> {
+ // `model` may be a role alias (`hint:reasoning`) the registry keys the
+ // route by; the provider only ever sees the concrete id it resolves to.
+ managed_chat_model(
+ role,
+ config,
+ Some(model),
+ None,
+ native_tool_calling,
+ thread_id,
+ egress,
+ )
+}
+
+/// The managed backend as a crate-native host `ChatModel`.
pub(crate) fn make_openhuman_backend_model(
role: &str,
config: &Config,
-) -> anyhow::Result<(
- std::sync::Arc<dyn tinyinference_llm::model::ChatModel<()>>,
- String,
-)> {
- if let Some(registered) =
- registered_managed_chat_model(role, config, None, None, true, None, &EgressLedger::none())
- {
- return registered;
- }
- let (model_client, model) = resolve_managed_backend(role, config, &EgressLedger::none())?;
- let chat: std::sync::Arc<dyn tinyinference_llm::model::ChatModel<()>> =
- std::sync::Arc::new(model_client);
- Ok((chat, model))
+) -> anyhow::Result<ManagedChatModel> {
+ managed_chat_model(role, config, None, None, true, None, &EgressLedger::none())
}
diff --git a/crates/openhuman-core/src/inference/provider/factory/turn_model.rs b/crates/openhuman-core/src/inference/provider/factory/turn_model.rs
index bdb3cbac61..f5aa1ef371 100644
--- a/crates/openhuman-core/src/inference/provider/factory/turn_model.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/turn_model.rs
@@ -11,7 +11,7 @@ use crate::core::runtime::DispatchCtx;
/// [`TurnModelSource`](crate::agent::tinyagents::TurnModelSource) to construct
/// the primary + each workload-tier route directly.
///
-/// - **Managed** → [`OpenHumanBackendModel`](super::openhuman_backend_model::OpenHumanBackendModel)
+/// - **Managed** → the registered managed chat model
/// pinned to `model`; the backend resolves the tier from `request.model`, so a
/// tier alias / agent-model pin dispatches directly.
/// - **Local / cloud** → the crate builders; the model rides the role's resolved
@@ -111,7 +111,7 @@ pub(super) fn create_turn_chat_model_with_native_tools_and_route_inner(
};
if !test_override_active {
if resolves_to_managed_backend(role, config) {
- if let Some(registered) = registered_managed_chat_model(
+ let (chat, _resolved_model) = managed_chat_model(
role,
config,
None,
@@ -119,20 +119,8 @@ pub(super) fn create_turn_chat_model_with_native_tools_and_route_inner(
native_tool_calling,
None,
egress,
- ) {
- let (chat, _resolved_model) = registered?;
- return Ok((chat, PROVIDER_OPENHUMAN.to_string(), model.to_string()));
- }
- let (backend, _resolved_model) = resolve_managed_backend(role, config, egress)?;
- return Ok((
- Arc::new(
- backend
- .with_default_model(model)
- .with_native_tool_calling(native_tool_calling),
- ),
- PROVIDER_OPENHUMAN.to_string(),
- model.to_string(),
- ));
+ )?;
+ return Ok((chat, PROVIDER_OPENHUMAN.to_string(), model.to_string()));
}
let resolved_provider = provider_for_role(role, config);
let provider_name = resolved_provider
@@ -197,7 +185,7 @@ pub(super) fn create_turn_chat_model_with_native_tools_and_route_inner(
/// would violate by building the local model.
///
/// - `provider_string` empty / `"cloud"` / [`PROVIDER_OPENHUMAN`] → managed
-/// [`OpenHumanBackendModel`] pinned to `model` (the force-managed case).
+/// the registered managed chat model pinned to `model` (the force-managed case).
/// - Otherwise the string equals what the role resolves to (a BYOK cloud slug), so
/// this delegates to [`create_turn_chat_model`] for `role`.
///
@@ -281,7 +269,7 @@ pub(crate) fn create_turn_chat_model_from_string_with_native_tools_and_route(
let p = provider_string.trim();
let is_managed = p.is_empty() || p == "cloud" || p == PROVIDER_OPENHUMAN;
if is_managed && !test_override_active {
- if let Some(registered) = registered_managed_chat_model(
+ let (chat, _resolved_model) = managed_chat_model(
role,
config,
None,
@@ -289,24 +277,9 @@ pub(crate) fn create_turn_chat_model_from_string_with_native_tools_and_route(
native_tool_calling,
None,
egress,
- ) {
- let (chat, _resolved_model) = registered?;
- return Ok((
- with_default_temperature(chat, temperature),
- PROVIDER_OPENHUMAN.to_string(),
- model.to_string(),
- ));
- }
- let (backend, _resolved_model) = resolve_managed_backend(role, config, egress)?;
+ )?;
return Ok((
- with_default_temperature(
- Arc::new(
- backend
- .with_default_model(model)
- .with_native_tool_calling(native_tool_calling),
- ),
- temperature,
- ),
+ with_default_temperature(chat, temperature),
PROVIDER_OPENHUMAN.to_string(),
model.to_string(),
));