Skip to content

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

feat(attachments): add provider fallback and history tracking

Steven Enamakel13f8f80299bc
diff --git a/crates/openhuman-core/src/agent/attachments/provider.rs b/crates/openhuman-core/src/agent/attachments/provider.rs
index 10e95108ff..717995c83c 100644
--- a/crates/openhuman-core/src/agent/attachments/provider.rs
+++ b/crates/openhuman-core/src/agent/attachments/provider.rs
@@ -13,6 +13,7 @@ use tinyinference_llm::{
};
pub(crate) fn wrap(
+ ctx: &crate::core::runtime::DispatchCtx,
inner: Arc<dyn ChatModel<()>>,
config: Arc<Config>,
model: &str,
@@ -64,6 +65,7 @@ pub(crate) fn wrap(
Arc::new(AttachmentModel {
inner,
config,
+ core: ctx.downgrade(),
profile,
fallback_cache: std::sync::Mutex::new(std::collections::HashMap::new()),
})
@@ -72,6 +74,7 @@ pub(crate) fn wrap(
/// Injected models supply their own selected-model facts through ModelProfile.
/// Transport support is still required for every native media block.
pub(crate) fn wrap_injected(
+ ctx: &crate::core::runtime::DispatchCtx,
inner: Arc<dyn ChatModel<()>>,
config: Arc<Config>,
) -> Arc<dyn ChatModel<()>> {
@@ -79,6 +82,7 @@ pub(crate) fn wrap_injected(
Arc::new(AttachmentModel {
inner,
config,
+ core: ctx.downgrade(),
profile,
fallback_cache: std::sync::Mutex::new(std::collections::HashMap::new()),
})
@@ -87,6 +91,9 @@ pub(crate) fn wrap_injected(
struct AttachmentModel {
inner: Arc<dyn ChatModel<()>>,
config: Arc<Config>,
+ /// The context the wrapped turn runs under. Weak: a model held by a turn
+ /// must not keep a tenant's context alive.
+ core: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
profile: ModelProfile,
fallback_cache: std::sync::Mutex<std::collections::HashMap<String, String>>,
}
@@ -111,6 +118,11 @@ fn recoverable_historical_media_error(error: &tinyinference_llm::Error) -> bool
}
impl AttachmentModel {
+ /// The dispatch context of the turn this model serves.
+ pub(super) fn dispatch_ctx(&self) -> crate::core::runtime::DispatchCtx {
+ crate::core::runtime::DispatchCtx::from_weak(self.core.as_ref())
+ }
+
fn native(&self, modality: InputModality, mime: &str) -> bool {
let known = match modality {
InputModality::Image => self.profile.modalities.image_in,
diff --git a/crates/openhuman-core/src/agent/attachments/provider_cache.rs b/crates/openhuman-core/src/agent/attachments/provider_cache.rs
index b9d90d2d2f..6851ff6c76 100644
--- a/crates/openhuman-core/src/agent/attachments/provider_cache.rs
+++ b/crates/openhuman-core/src/agent/attachments/provider_cache.rs
@@ -8,7 +8,8 @@ const MAX_CACHE_BYTES: u64 = 1024 * 1024;
impl AttachmentModel {
pub(super) fn fallback_key(&self, path: &str, mime: &str, bytes: &[u8]) -> String {
let route = if mime.starts_with("image/") || mime == "application/pdf" {
- crate::inference::provider::factory::create_chat_model_with_model_id(
+ crate::inference::provider::factory::create_chat_model_with_model_id_in(
+ &self.dispatch_ctx(),
"vision",
&self.config,
0.0,
diff --git a/crates/openhuman-core/src/agent/attachments/provider_fallback.rs b/crates/openhuman-core/src/agent/attachments/provider_fallback.rs
index 72b0f1a80e..f6a82c1f1b 100644
--- a/crates/openhuman-core/src/agent/attachments/provider_fallback.rs
+++ b/crates/openhuman-core/src/agent/attachments/provider_fallback.rs
@@ -228,7 +228,8 @@ impl AttachmentModel {
));
}
let (vision, model_id) =
- crate::inference::provider::factory::create_chat_model_with_model_id(
+ crate::inference::provider::factory::create_chat_model_with_model_id_in(
+ &self.dispatch_ctx(),
"vision",
&self.config,
0.0,
@@ -238,7 +239,7 @@ impl AttachmentModel {
.profile()
.and_then(|p| p.provider.as_deref())
.unwrap_or("unknown");
- let prepared = wrap(vision.clone(), self.config.clone(), &model_id, provider);
+ let prepared = wrap(&self.dispatch_ctx(), vision.clone(), self.config.clone(), &model_id, provider);
let known = prepared.profile().is_some_and(|p| p.modalities.image_in)
|| provider == "injected" && vision.profile().is_some_and(|p| p.modalities.image_in);
if !known || !vision.supports_input(InputModality::Image, mime, InputSource::Base64) {
diff --git a/crates/openhuman-core/src/agent/attachments/provider_history_tests.rs b/crates/openhuman-core/src/agent/attachments/provider_history_tests.rs
index 7603ea6811..aec79bb626 100644
--- a/crates/openhuman-core/src/agent/attachments/provider_history_tests.rs
+++ b/crates/openhuman-core/src/agent/attachments/provider_history_tests.rs
@@ -13,7 +13,7 @@ async fn missing_historical_media_does_not_block_latest_valid_media() {
let mut config = Config::default();
config.action_dir = temp.path().into();
let probe = Arc::new(Probe::default());
- let wrapper = wrap(probe.clone(), Arc::new(config), "gpt-4o", "openai");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config), "gpt-4o", "openai");
let image = |path: &str| {
Message::User(tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Image(ImageRef {
diff --git a/crates/openhuman-core/src/agent/attachments/provider_security_tests.rs b/crates/openhuman-core/src/agent/attachments/provider_security_tests.rs
index 9ce72f2cb6..7fd222e9fe 100644
--- a/crates/openhuman-core/src/agent/attachments/provider_security_tests.rs
+++ b/crates/openhuman-core/src/agent/attachments/provider_security_tests.rs
@@ -112,7 +112,7 @@ async fn fifo_attachment_is_rejected_without_waiting_for_a_writer() {
config.action_dir = temp.path().to_path_buf();
config.workspace_dir = temp.path().join("internal");
let model = Arc::new(NeverCalled::default());
- let wrapped = wrap_injected(model.clone(), Arc::new(config));
+ let wrapped = wrap_injected(&crate::core::runtime::DispatchCtx::host_default(), model.clone(), Arc::new(config));
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Image(ImageRef {
diff --git a/crates/openhuman-core/src/agent/attachments/provider_tests.rs b/crates/openhuman-core/src/agent/attachments/provider_tests.rs
index 81427b95e3..c9f5d49553 100644
--- a/crates/openhuman-core/src/agent/attachments/provider_tests.rs
+++ b/crates/openhuman-core/src/agent/attachments/provider_tests.rs
@@ -34,7 +34,7 @@ async fn local_native_resolution_changes_only_ephemeral_request() {
config.action_dir = temp.path().into();
config.workspace_dir = temp.path().join("internal");
let probe = Arc::new(Probe::default());
- let wrapper = wrap(probe.clone(), Arc::new(config), "gpt-4o", "openai");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config), "gpt-4o", "openai");
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Image(ImageRef {
@@ -67,7 +67,7 @@ async fn blocked_attachment_fails_before_model_call() {
let mut config = Config::default();
config.multimodal_files.max_files = 0;
let probe = Arc::new(Probe::default());
- let wrapper = wrap(probe.clone(), Arc::new(config), "gpt-4o", "openai");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config), "gpt-4o", "openai");
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Image(ImageRef {
@@ -81,7 +81,7 @@ async fn blocked_attachment_fails_before_model_call() {
}
#[test]
fn unknown_model_does_not_inherit_transport_vision() {
- let model = wrap(
+ let model = wrap(&crate::core::runtime::DispatchCtx::host_default(),
Arc::new(Probe::default()),
Arc::new(Config::default()),
"unknown-model",
@@ -99,7 +99,7 @@ async fn persisted_image_without_mime_uses_magic_over_extension() {
let mut config = Config::default();
config.action_dir = temp.path().into();
let probe = Arc::new(Probe::default());
- let wrapper = wrap(probe.clone(), Arc::new(config), "gpt-4o", "openai");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config), "gpt-4o", "openai");
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Image(ImageRef {
@@ -119,7 +119,7 @@ async fn persisted_image_without_mime_uses_magic_over_extension() {
#[tokio::test]
async fn too_many_typed_images_reject_before_read_or_inference() {
let probe = Arc::new(Probe::default());
- let wrapper = wrap(
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(),
probe.clone(),
Arc::new(Config::default()),
"gpt-4o",
@@ -146,7 +146,7 @@ async fn too_many_typed_images_reject_before_read_or_inference() {
async fn external_channel_cannot_rehydrate_a_local_image() {
use crate::agent::turn_origin::AgentTurnOrigin;
let probe = Arc::new(Probe::default());
- let wrapper = wrap(
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(),
probe.clone(),
Arc::new(Config::default()),
"gpt-4o",
@@ -185,7 +185,7 @@ async fn unknown_video_retains_path_as_metadata_without_specialist() {
let mut config = Config::default();
config.action_dir = temp.path().into();
let probe = Arc::new(Probe::default());
- let wrapper = wrap(probe.clone(), Arc::new(config), "unknown", "custom");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config), "unknown", "custom");
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Video(MediaRef::Path {
@@ -314,7 +314,7 @@ fn endpoint_discovery_denial_overrides_offline_vision_facts() {
source: LimitSource::ProviderListing,
}),
);
- let model = wrap(
+ let model = wrap(&crate::core::runtime::DispatchCtx::host_default(),
Arc::new(Probe::default()),
Arc::new(config),
"gpt-4o",
@@ -328,7 +328,7 @@ async fn legacy_inline_source_is_bounded_and_malformed_base64_rejects_before_pri
let probe = Arc::new(Probe::default());
let mut config = Config::default();
config.multimodal.max_image_size_mb = 1;
- let wrapper = wrap(probe.clone(), Arc::new(config), "gpt-4o", "openai");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config), "gpt-4o", "openai");
for source in [
"data:image/png;base64,%%%".to_owned(),
format!("data:image/png;base64,{}", "A".repeat(2 * 1024 * 1024)),
@@ -349,7 +349,7 @@ async fn legacy_inline_source_is_bounded_and_malformed_base64_rejects_before_pri
#[tokio::test]
async fn disabled_remote_replay_is_rejected_before_inference() {
let probe = Arc::new(Probe::default());
- let wrapper = wrap(
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(),
probe.clone(),
Arc::new(Config::default()),
"unknown",
@@ -384,7 +384,7 @@ async fn inline_native_replay_materializes_original_and_keeps_durable_message_un
let mut config = Config::default();
config.action_dir = temp.path().into();
let probe = Arc::new(Probe::default());
- let wrapper = wrap(probe.clone(), Arc::new(config), "gpt-4o", "openai");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config), "gpt-4o", "openai");
let bytes = b"\x89PNG\r\n\x1a\noriginal";
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
@@ -432,7 +432,7 @@ async fn derivative_cache_cannot_overwrite_original_named_like_cache() {
assert_eq!(files.len(), 1);
assert_eq!(files[0].name, ".openhuman-intake.txt");
let probe = Arc::new(Probe::default());
- let wrapper = wrap(probe.clone(), Arc::new(config.clone()), "unknown", "custom");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), probe.clone(), Arc::new(config.clone()), "unknown", "custom");
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Document(MediaRef::Path {
@@ -504,7 +504,7 @@ async fn unknown_model_legacy_inline_replay_uses_specialist_once_across_model_re
)]);
for _ in 0..2 {
let main = Arc::new(Probe::default());
- let wrapper = wrap(main.clone(), config.clone(), "unknown-model", "custom");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), main.clone(), config.clone(), "unknown-model", "custom");
assert!(wrapper.invoke(&(), request.clone()).await.is_err());
let captured = main.requests.lock().unwrap().clone();
assert_eq!(captured.len(), 1);
@@ -527,7 +527,7 @@ async fn legacy_audio_base64_unsupported_transport_becomes_workspace_reference()
let mut config = Config::default();
config.action_dir = temp.path().into();
let main = Arc::new(Probe::default());
- let wrapper = wrap(main.clone(), Arc::new(config), "unknown", "custom");
+ let wrapper = wrap(&crate::core::runtime::DispatchCtx::host_default(), main.clone(), Arc::new(config), "unknown", "custom");
let request = ModelRequest::new(vec![Message::User(
tinyinference_llm::message::UserMessage {
content: vec![ContentBlock::Audio(MediaRef::Base64 {
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs b/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs
index 755577b988..1eae8e910f 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs
@@ -176,7 +176,7 @@ impl OpenHumanHostBundleFactory {
security = security.with_tool_policy(policy);
}
let security = Arc::new(security);
- let models = Arc::new(OpenHumanModelResolver::new(Arc::clone(&inputs.config)));
+ let models = Arc::new(OpenHumanModelResolver::new(Arc::clone(&inputs.config)).with_dispatch(turn.dispatch_ctx()));
let budget = Arc::new(
OpenHumanBudgetGate::new(Arc::clone(&inputs.config)).with_dispatch(turn.dispatch_ctx()),
);
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/model_resolver.rs b/crates/openhuman-core/src/agent/tinyagents/host/model_resolver.rs
index 9b5f4e174c..207a29ee55 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/model_resolver.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/model_resolver.rs
@@ -246,6 +246,8 @@ pub struct OpenHumanModelResolver {
/// Built clients keyed by workload role. Guarded by a `std::sync::Mutex`
/// held across no `.await` — model construction is synchronous.
cache: Mutex<HashMap<&'static str, Arc<dyn ChatModel<()>>>>,
+ /// The context the turn runs under; the host's own when none was bound.
+ dispatch: Option<crate::core::runtime::DispatchCtx>,
}
impl OpenHumanModelResolver {
@@ -257,9 +259,18 @@ impl OpenHumanModelResolver {
config,
temperature,
cache: Mutex::new(HashMap::new()),
+ dispatch: None,
}
}
+ /// Resolve models under `dispatch`, the context of the turn this resolver
+ /// serves (agent-local model overrides, tenant routes).
+ #[must_use]
+ pub fn with_dispatch(mut self, dispatch: crate::core::runtime::DispatchCtx) -> Self {
+ self.dispatch = Some(dispatch);
+ self
+ }
+
/// Builds a resolver that pins every resolved model to `temperature`
/// instead of the config default.
pub fn with_temperature(config: Arc<Config>, temperature: f64) -> Self {
@@ -304,7 +315,13 @@ impl OpenHumanModelResolver {
// The one construction chokepoint: provider resolution, BYOK/local/managed
// selection, and the egress disclosure all happen inside here.
let (model, model_id) =
- create_chat_model_with_model_id(role, &self.config, self.temperature).map_err(
+ create_chat_model_with_model_id_in(
+ &self.dispatch.clone().unwrap_or_else(crate::core::runtime::DispatchCtx::host_default),
+ role,
+ &self.config,
+ self.temperature,
+ )
+ .map_err(
|error| {
TinyAgentsError::Model(format!(
"openhuman: no model for workload role `{role}`: {error}"