Skip to content

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

refactor(embed): adjust turn error handling and dispatch logic

Steven Enamakela90ecc96c920
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_run_error.rs b/crates/openhuman-core/src/agent/tinyagents/turn_run_error.rs
index ebf779597e..4b74768e0e 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_run_error.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_run_error.rs
@@ -2,7 +2,7 @@
//! caller expects, for [`TurnSpec::run`](TurnSpec::run).
use crate::agent::tinyagents::journal::TurnJournal;
-use tinyagents_harness::turn_run_error::{classify_run_failure, RunFailureKind};
+use tinyagents_harness::turn_run_error::{RunFailureKind, classify_run_failure};
use tinyinference_llm::model::ModelErrorSlot;
/// Map the harness's run failure `e` onto the typed error the turn caller
diff --git a/crates/openhuman-embed/src/turn.rs b/crates/openhuman-embed/src/turn.rs
index 79494db354..a7e44df83d 100644
--- a/crates/openhuman-embed/src/turn.rs
+++ b/crates/openhuman-embed/src/turn.rs
@@ -49,8 +49,8 @@ use openhuman_core::security::turn_origin::AgentTurnOrigin;
#[path = "turn_types.rs"]
mod types;
-pub(crate) use types::{is_safe_endpoint_for_bearer, sanitize_url_for_display};
pub use types::{Route, TurnOutcome, TurnRequest};
+pub(crate) use types::{is_safe_endpoint_for_bearer, sanitize_url_for_display};
#[path = "turn_control.rs"]
mod control;
@@ -175,11 +175,11 @@ impl Turn {
pub fn tools(
mut self,
factory: impl for<'a> Fn(
- openhuman_core::agent::TurnContext<'a>,
- ) -> openhuman_core::agent::HostTurnTools
- + Send
- + Sync
- + 'static,
+ openhuman_core::agent::TurnContext<'a>,
+ ) -> openhuman_core::agent::HostTurnTools
+ + Send
+ + Sync
+ + 'static,
) -> Self {
self.tools = Some(Arc::new(factory));
self
@@ -617,7 +617,7 @@ async fn dispatch(
let turn: futures_box::BoxFuture<'_, Result<AgentReply, CoreError>> =
Box::pin(async move {
use openhuman_core::inference::host_runtime::ops::{
- agent_chat_reply_for_controlled, AgentChatTarget,
+ AgentChatTarget, agent_chat_reply_for_controlled,
};
let mut config = dispatch
.load_config()
@@ -663,7 +663,7 @@ async fn dispatch(
let turn: futures_box::BoxFuture<'_, Result<AgentReply, CoreError>> =
Box::pin(async move {
use openhuman_core::inference::host_runtime::ops::{
- agent_chat_for, AgentChatTarget,
+ AgentChatTarget, agent_chat_for,
};
let mut config = inner.config.clone();
let route = openhuman_core::config::schema::EphemeralRoute::from_params(
@@ -711,21 +711,18 @@ async fn dispatch(
}
}
if let Err(error) = &outcome
- && let Some(refusal) = error.downcast_ref::<
- crate::budget::DispatchRefusal,
- >()
+ && let Some(refusal) =
+ error.downcast_ref::<crate::budget::DispatchRefusal>()
{
return Err(CoreError::DispatchAdmission {
method: AGENT_CHAT,
source: *refusal,
- usage: report.usage.map(|spent| {
- crate::complete::CompletionUsage {
- input_tokens: spent.input_tokens,
- output_tokens: spent.output_tokens,
- cached_tokens: spent.cached_tokens,
- reasoning_tokens: spent.reasoning_tokens,
- cost_usd: spent.cost_usd,
- }
+ usage: report.usage.map(|spent| crate::complete::CompletionUsage {
+ input_tokens: spent.input_tokens,
+ output_tokens: spent.output_tokens,
+ cached_tokens: spent.cached_tokens,
+ reasoning_tokens: spent.reasoning_tokens,
+ cost_usd: spent.cost_usd,
}),
});
}
diff --git a/crates/openhuman-embed/src/turn_tests.rs b/crates/openhuman-embed/src/turn_tests.rs
index 28f669396a..388b85a58d 100644
--- a/crates/openhuman-embed/src/turn_tests.rs
+++ b/crates/openhuman-embed/src/turn_tests.rs
@@ -70,6 +70,35 @@ fn turn_request_omits_absent_options() {
assert_eq!(encoded, json!({ "message": "hi" }));
}
+#[test]
+fn typed_dispatch_refusal_precedes_structured_error_mapping_and_keeps_usage() {
+ let refusal =
+ crate::budget::DispatchRefusal::new(crate::budget::DispatchRefusalReason::CallLimit);
+ let error = anyhow::Error::new(refusal);
+ let mapped = map_agent_chat_error(
+ error,
+ Some(tinyagents_harness::middleware::ResponseUsage {
+ input_tokens: 13,
+ output_tokens: 5,
+ cached_tokens: 2,
+ reasoning_tokens: 1,
+ cost_usd: Some(0.004),
+ ..Default::default()
+ }),
+ );
+
+ match mapped {
+ CoreError::DispatchAdmission { source, usage, .. } => {
+ assert_eq!(source, refusal);
+ let usage = usage.expect("the completed invalid response remains accounted");
+ assert_eq!(usage.input_tokens, 13);
+ assert_eq!(usage.output_tokens, 5);
+ assert_eq!(usage.cost_usd, Some(0.004));
+ }
+ other => panic!("expected typed dispatch refusal, got {other:?}"),
+ }
+}
+
#[test]
fn a_route_sets_both_halves_or_neither() {
// The core ignores the route unless both arrive non-blank, so the type