Skip to content

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

refactor(tinyagents): extract turn model resolver into its own module

Steven Enamakel2f1bc7300b1f
diff --git a/crates/openhuman-core/src/agent/tinyagents/mod.rs b/crates/openhuman-core/src/agent/tinyagents/mod.rs
index 64352e909e..c5fd8b1451 100644
--- a/crates/openhuman-core/src/agent/tinyagents/mod.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/mod.rs
@@ -57,6 +57,7 @@ mod steering_forwarder;
pub(crate) mod stop_hooks;
pub mod todos;
pub(crate) mod tool_registration;
+mod turn_model_resolver;
mod turn_models;
mod turn_outcome;
mod turn_policy;
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_model_resolver.rs b/crates/openhuman-core/src/agent/tinyagents/turn_model_resolver.rs
new file mode 100644
index 0000000000..4be2affc3c
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_model_resolver.rs
@@ -0,0 +1,90 @@
+//! [`TurnModelResolver`] — the host resolver over a turn's pre-built models.
+
+use std::sync::Arc;
+
+use async_trait::async_trait;
+
+use super::model::TurnChatModel;
+use super::turn_models::TurnModels;
+use tinyagents_harness::host::{ModelResolveRequest, ModelResolver};
+use tinyinference_llm::model::ErrorSlotModel;
+
+/// Host resolver for one live invocation. It exposes the exact pre-built
+/// primary and fallback route models already selected by OpenHuman, rather
+/// than constructing a fresh config-routed model during hosted preparation.
+///
+/// # Who wins: the turn's selection or the definition's pin
+///
+/// The **primary** is the model OpenHuman already chose for this turn — the
+/// user's per-thread `model_override`, else `config.default_model` — and for
+/// the turn's lead (the depth-0 agent, `is_team_lead`) it always wins. The
+/// harness forwards exact model pins and typed routing hints separately.
+/// Both are advisory, so neither may replace the lead's stronger selection.
+/// Sub-agents resolve an exact pin first, otherwise `hint:<name>`, against
+/// prebuilt tier routes; an unavailable route falls back to the primary.
+///
+/// Only the lead's model records into the turn's error slot (#6724). Sub-agents
+/// resolve through this same resolver, possibly in parallel, so they get the
+/// unwrapped models: a child's attempt must neither clear the lead's recorded
+/// failure nor leave its own failure to be re-surfaced as the lead's.
+pub(crate) struct TurnModelResolver {
+ lead: TurnChatModel,
+ primary: TurnChatModel,
+ routes: std::collections::HashMap<String, TurnChatModel>,
+}
+
+impl TurnModelResolver {
+ pub(crate) fn from_turn_models(models: &TurnModels) -> Self {
+ let mut resolver = Self::new(
+ models.primary.clone(),
+ models.routes.iter().cloned().collect(),
+ );
+ resolver.lead = Arc::new(ErrorSlotModel::new(
+ models.primary.clone(),
+ models.error_slot.clone(),
+ ));
+ resolver
+ }
+
+ pub(crate) fn new(
+ primary: TurnChatModel,
+ routes: std::collections::HashMap<String, TurnChatModel>,
+ ) -> Self {
+ Self {
+ lead: primary.clone(),
+ primary,
+ routes,
+ }
+ }
+}
+
+#[async_trait]
+impl ModelResolver<()> for TurnModelResolver {
+ async fn resolve(
+ &self,
+ request: &ModelResolveRequest,
+ ) -> tinyagents_harness::Result<TurnChatModel> {
+ let hint_route = request
+ .model_hint
+ .as_deref()
+ .map(str::trim)
+ .filter(|hint| !hint.is_empty())
+ .map(|hint| format!("hint:{hint}"));
+ let pin = request.model_pin().or(hint_route.as_deref());
+ if request.is_team_lead {
+ if let Some(pin) = pin.filter(|pin| self.routes.contains_key(*pin)) {
+ tracing::debug!(
+ target: "tinyagents",
+ agent_id = %request.agent_id,
+ pin,
+ "[models][resolver] lead keeps the turn's selected primary; definition model pin ignored"
+ );
+ }
+ return Ok(self.lead.clone());
+ }
+ Ok(pin
+ .and_then(|name| self.routes.get(name))
+ .cloned()
+ .unwrap_or_else(|| self.primary.clone()))
+ }
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_models.rs b/crates/openhuman-core/src/agent/tinyagents/turn_models.rs
index 4acd9241b0..8a9740ed6f 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_models.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_models.rs
@@ -3,6 +3,9 @@
use std::sync::Arc;
+use crate::core::runtime::DispatchCtx;
+pub(crate) use super::turn_model_resolver::TurnModelResolver;
+
use async_trait::async_trait;
use crate::agent::tinyagents::model::{BuiltTurnModels, TierRoutes, TurnChatModel};
@@ -83,86 +86,6 @@ impl TurnModels {
}
}
-/// Host resolver for one live invocation. It exposes the exact pre-built
-/// primary and fallback route models already selected by OpenHuman, rather
-/// than constructing a fresh config-routed model during hosted preparation.
-///
-/// # Who wins: the turn's selection or the definition's pin
-///
-/// The **primary** is the model OpenHuman already chose for this turn — the
-/// user's per-thread `model_override`, else `config.default_model` — and for
-/// the turn's lead (the depth-0 agent, `is_team_lead`) it always wins. The
-/// harness forwards exact model pins and typed routing hints separately.
-/// Both are advisory, so neither may replace the lead's stronger selection.
-/// Sub-agents resolve an exact pin first, otherwise `hint:<name>`, against
-/// prebuilt tier routes; an unavailable route falls back to the primary.
-///
-/// Only the lead's model records into the turn's error slot (#6724). Sub-agents
-/// resolve through this same resolver, possibly in parallel, so they get the
-/// unwrapped models: a child's attempt must neither clear the lead's recorded
-/// failure nor leave its own failure to be re-surfaced as the lead's.
-pub(crate) struct TurnModelResolver {
- lead: TurnChatModel,
- primary: TurnChatModel,
- routes: std::collections::HashMap<String, TurnChatModel>,
-}
-
-impl TurnModelResolver {
- pub(crate) fn from_turn_models(models: &TurnModels) -> Self {
- let mut resolver = Self::new(
- models.primary.clone(),
- models.routes.iter().cloned().collect(),
- );
- resolver.lead = Arc::new(ErrorSlotModel::new(
- models.primary.clone(),
- models.error_slot.clone(),
- ));
- resolver
- }
-
- pub(crate) fn new(
- primary: TurnChatModel,
- routes: std::collections::HashMap<String, TurnChatModel>,
- ) -> Self {
- Self {
- lead: primary.clone(),
- primary,
- routes,
- }
- }
-}
-
-#[async_trait]
-impl ModelResolver<()> for TurnModelResolver {
- async fn resolve(
- &self,
- request: &ModelResolveRequest,
- ) -> tinyagents_harness::Result<TurnChatModel> {
- let hint_route = request
- .model_hint
- .as_deref()
- .map(str::trim)
- .filter(|hint| !hint.is_empty())
- .map(|hint| format!("hint:{hint}"));
- let pin = request.model_pin().or(hint_route.as_deref());
- if request.is_team_lead {
- if let Some(pin) = pin.filter(|pin| self.routes.contains_key(*pin)) {
- tracing::debug!(
- target: "tinyagents",
- agent_id = %request.agent_id,
- pin,
- "[models][resolver] lead keeps the turn's selected primary; definition model pin ignored"
- );
- }
- return Ok(self.lead.clone());
- }
- Ok(pin
- .and_then(|name| self.routes.get(name))
- .cloned()
- .unwrap_or_else(|| self.primary.clone()))
- }
-}
-
/// Build the per-turn [`TurnModels`] **crate-natively** from `(role, config)` —
/// the Phase 3 P3-B cutover of [`build_turn_models`]: instead of wrapping one host
/// `Provider` per tier in a [`native model adapter`], each tier is built as a crate-native
@@ -400,8 +323,6 @@ pub(crate) struct CrateNativeSource {
primary_override: Option<String>,
}
-use crate::core::runtime::DispatchCtx;
-
impl CrateNativeSource {
fn ctx(&self) -> DispatchCtx {
DispatchCtx::from_weak(self.core.as_ref())