Skip to content

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

feat(embed): add physical dispatch admission to completer

Steven Enamakeld16fe4fa2cae
diff --git a/crates/openhuman-core/src/agent/tinyagents/budget.rs b/crates/openhuman-core/src/agent/tinyagents/budget.rs
index 0e8c3dd0a7..73444f2ee8 100644
--- a/crates/openhuman-core/src/agent/tinyagents/budget.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/budget.rs
@@ -10,6 +10,9 @@ pub(crate) const MAX_SPAWN_DEPTH: usize = 3;
pub use tinyinference_llm::model::budget::{
Budget, BudgetExceeded, BudgetSnapshot, CallBudget, Spend, SpendLimits,
};
+pub use tinyagents_harness::cost::{
+ DispatchRefusal, DispatchRefusalReason, PhysicalDispatchAdmission, PhysicalDispatchPermit,
+};
/// OpenHuman's source-compatible carrier for the shared ledger and physical
/// call reservation bounds. The owner harness performs the wrapping engine.
diff --git a/crates/openhuman-embed/src/budget.rs b/crates/openhuman-embed/src/budget.rs
index 5ece0e5fb9..0d1ecd1c98 100644
--- a/crates/openhuman-embed/src/budget.rs
+++ b/crates/openhuman-embed/src/budget.rs
@@ -19,6 +19,9 @@
//! counts conservatively; multimodal requests fail before dispatch. Choose
//! bounds that include model framing and the maximum price on allowed routes.
pub use openhuman_core::agent::tinyagents::budget::ModelBudget;
+pub use openhuman_core::agent::tinyagents::budget::{
+ DispatchRefusal, DispatchRefusalReason, PhysicalDispatchAdmission, PhysicalDispatchPermit,
+};
pub use tinyinference_llm::model::budget::{
Budget, BudgetExceeded, BudgetSnapshot, CallBudget, Spend, SpendLimits,
};
diff --git a/crates/openhuman-embed/src/complete.rs b/crates/openhuman-embed/src/complete.rs
index 9809a1798a..ed70b831e8 100644
--- a/crates/openhuman-embed/src/complete.rs
+++ b/crates/openhuman-embed/src/complete.rs
@@ -448,6 +448,7 @@ pub struct Completer {
observers: Arc<CompleterObservers>,
cancellation: crate::cancellation::Cancellation,
budget: Option<crate::budget::ModelBudget>,
+ dispatch_admission: Option<Arc<dyn crate::budget::PhysicalDispatchAdmission>>,
}
impl std::fmt::Debug for Completer {
@@ -471,6 +472,7 @@ impl Completer {
observers: Arc::new(CompleterObservers::default()),
cancellation: Default::default(),
budget: None,
+ dispatch_admission: None,
}
}
@@ -502,6 +504,15 @@ impl Completer {
self
}
+ /// Attach a host-owned physical-dispatch policy shared with other calls.
+ pub fn dispatch_admission(
+ mut self,
+ admission: Arc<dyn crate::budget::PhysicalDispatchAdmission>,
+ ) -> Self {
+ self.dispatch_admission = Some(admission);
+ self
+ }
+
/// Report every call to `observer`.
pub fn observer(mut self, observer: Arc<dyn CompletionObserver>) -> Self {
Arc::make_mut(&mut self.observers).completion = Some(observer);
diff --git a/crates/openhuman-embed/src/complete_tests.rs b/crates/openhuman-embed/src/complete_tests.rs
index 842d8bfe5f..976f3b8bb3 100644
--- a/crates/openhuman-embed/src/complete_tests.rs
+++ b/crates/openhuman-embed/src/complete_tests.rs
@@ -476,6 +476,63 @@ async fn physical_observer_reports_each_structured_repair_dispatch() {
}));
}
+struct OneCallAdmission(std::sync::atomic::AtomicUsize);
+
+struct TestDispatchPermit;
+
+#[async_trait::async_trait]
+impl crate::budget::PhysicalDispatchAdmission for OneCallAdmission {
+ async fn acquire(
+ &self,
+ ) -> Result<Arc<dyn crate::budget::PhysicalDispatchPermit>, crate::budget::DispatchRefusal>
+ {
+ use std::sync::atomic::Ordering;
+
+ if self.0.fetch_add(1, Ordering::SeqCst) != 0 {
+ return Err(crate::budget::DispatchRefusal::new(
+ crate::budget::DispatchRefusalReason::CallLimit,
+ ));
+ }
+ Ok(Arc::new(TestDispatchPermit))
+ }
+}
+
+impl crate::budget::PhysicalDispatchPermit for TestDispatchPermit {
+ fn mark_dispatched(&self) -> Result<(), crate::budget::DispatchRefusal> {
+ Ok(())
+ }
+}
+
+#[tokio::test]
+async fn repair_cannot_exceed_dispatch_limit() {
+ let server = MockServer::start().await;
+ let replies = Arc::new(Mutex::new(0));
+ let replies_for_handler = Arc::clone(&replies);
+ Mock::given(method("POST"))
+ .and(path("/v1/chat/completions"))
+ .respond_with(move |_request: &wiremock::Request| {
+ let mut replies = replies_for_handler.lock().unwrap();
+ let text = if *replies == 0 { "invalid" } else { "{\"ok\":true}" };
+ *replies += 1;
+ ResponseTemplate::new(200).set_body_json(body(text, "stop"))
+ })
+ .mount(&server)
+ .await;
+ let admission = Arc::new(OneCallAdmission(std::sync::atomic::AtomicUsize::new(0)));
+ let request = CompletionRequest::new("repair-model", vec![ChatMessage::user("private")])
+ .response_format(ResponseFormat::JsonObject)
+ .structured_retries(1);
+
+ let result = completer(&server)
+ .dispatch_admission(admission)
+ .complete(request)
+ .await;
+
+ assert!(result.is_err(), "the refused repair must be terminal");
+ let requests = server.received_requests().await.expect("recording on");
+ assert_eq!(requests.len(), 1, "a refused repair must not reach the server");
+}
+
#[test]
fn json_reply_tolerates_one_code_fence() {
assert_eq!(
diff --git a/crates/openhuman-embed/src/turn.rs b/crates/openhuman-embed/src/turn.rs
index d17710fe34..3891c5fe70 100644
--- a/crates/openhuman-embed/src/turn.rs
+++ b/crates/openhuman-embed/src/turn.rs
@@ -70,6 +70,7 @@ pub(crate) enum TurnTarget {
/// on, so a host can build it in one place and send it from another.
pub struct Turn {
budget: Option<crate::budget::ModelBudget>,
+ dispatch_admission: Option<Arc<dyn crate::budget::PhysicalDispatchAdmission>>,
/// A narrower model-directed delegation depth for this turn (fanout leaves).
spawn_depth_limit: Option<usize>,
/// The native token this turn's run tree observes, minted by `send` and
@@ -111,6 +112,7 @@ impl Turn {
Self {
target,
budget: None,
+ dispatch_admission: None,
spawn_depth_limit: None,
native_cancellation: None,
request: TurnRequest::new(message),