Skip to content

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

refactor(inference): add request validation for complete_once

Steven Enamakel5a3d3d802fbb
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops.rs b/crates/openhuman-core/src/inference/host_runtime/ops.rs
index bd9ac3d152..3ccfcc0a4b 100644
--- a/crates/openhuman-core/src/inference/host_runtime/ops.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/ops.rs
@@ -18,7 +18,8 @@ pub use agent_chat::{
agent_chat_simple, AgentChatEnv, AgentChatReply, AgentChatTarget,
};
pub use complete_once::{
- complete_once, complete_once_controlled, complete_once_observed, CompletionEndpoint,
+ complete_once, complete_once_controlled, complete_once_observed,
+ validate_complete_once_request, CompletionEndpoint,
};
pub use runtime_ops::{
local_ai_prompt, local_ai_status, local_ai_summarize, local_ai_transcribe,
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
index 797c5e742b..1e586926d7 100644
--- a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
@@ -79,9 +79,47 @@ pub async fn complete_once_observed(
std::sync::Arc<dyn tinyagents_harness::observability::PhysicalDispatchObserver>,
>,
) -> Result<ModelResponse, String> {
+ if let Err(message) = validate_complete_once_request(&request) {
+ return Err(message);
+ }
complete_once_controlled(endpoint, request, budget, observer, None)
.await
- .map_err(|error| error.to_string())
+ .map_err(|error| match error {
+ tinyagents_harness::cost::ControlledDispatchError::Model(source) => {
+ format!("complete_once: {source}")
+ }
+ tinyagents_harness::cost::ControlledDispatchError::Admission(source) => {
+ source.to_string()
+ }
+ })
+}
+
+/// Validate the stateless completion request before dispatching it.
+///
+/// The legacy string facade returns these operation-level messages verbatim;
+/// separating preflight from provider errors preserves that contract without
+/// inspecting formatted error text.
+pub fn validate_complete_once_request(request: &ModelRequest) -> Result<(), String> {
+ if !request.tools.is_empty() {
+ return Err(
+ "complete_once: tools are not supported on a stateless completion; use an agent turn"
+ .to_string(),
+ );
+ }
+ // `provider_options` is forwarded untouched, so a `tools` key there would
+ // reach the provider even though `request.tools` is empty above. The typed
+ // request fields own these keys.
+ if let Some(key) = reserved_provider_option(&request.provider_options) {
+ return Err(format!(
+ "complete_once: provider_options may not set `{key}`; use the typed request fields"
+ ));
+ }
+ request
+ .model
+ .as_deref()
+ .filter(|model| !model.trim().is_empty())
+ .map(|_| ())
+ .ok_or_else(|| "complete_once: request.model is required".to_string())
}
/// Run one completion with optional neutral physical-call admission.
@@ -94,34 +132,13 @@ pub async fn complete_once_controlled(
>,
admission: Option<std::sync::Arc<dyn tinyagents_harness::cost::PhysicalDispatchAdmission>>,
) -> Result<ModelResponse, tinyagents_harness::cost::ControlledDispatchError> {
- if !request.tools.is_empty() {
- return Err(tinyagents_harness::cost::ControlledDispatchError::Model(tinyinference_llm::Error::Validation(
- "complete_once: tools are not supported on a stateless completion; use an agent turn"
- .to_string())));
- }
- // `provider_options` is forwarded untouched, so a `tools` key there would
- // reach the provider even though `request.tools` is empty above. The typed
- // request fields own these keys.
- if let Some(key) = reserved_provider_option(&request.provider_options) {
+ if let Err(message) = validate_complete_once_request(&request) {
return Err(tinyagents_harness::cost::ControlledDispatchError::Model(
- tinyinference_llm::Error::Validation(format!(
- "complete_once: provider_options may not set `{key}`; use the typed request fields"
- )),
+ tinyinference_llm::Error::Validation(message),
));
}
// Blank is refused, but the id itself goes out exactly as the caller named it.
- let model_id = request
- .model
- .as_deref()
- .filter(|model| !model.trim().is_empty())
- .ok_or_else(|| {
- tinyagents_harness::cost::ControlledDispatchError::Model(
- tinyinference_llm::Error::Validation(
- "complete_once: request.model is required".to_string(),
- ),
- )
- })?
- .to_string();
+ let model_id = request.model.as_deref().unwrap().to_string();
let mut model = OpenAiModel::new(endpoint.api_key.clone())
.with_base_url(endpoint.base_url.clone())
diff --git a/crates/openhuman-embed/src/complete.rs b/crates/openhuman-embed/src/complete.rs
index dce8db1fdc..83b762db8f 100644
--- a/crates/openhuman-embed/src/complete.rs
+++ b/crates/openhuman-embed/src/complete.rs
@@ -636,6 +636,17 @@ impl Completer {
let endpoint = self.checked_endpoint(&request)?;
let format = request.response_format.clone();
let model = request.model.clone();
+ let wire_request = request.into_wire();
+ if let Err(message) =
+ openhuman_core::inference::host_runtime::ops::validate_complete_once_request(
+ &wire_request,
+ )
+ {
+ return Err(CoreError::Rpc {
+ method: COMPLETE,
+ message,
+ });
+ }
log::debug!(
"[embed] complete start method={COMPLETE} model={model} messages={} timeout_ms={:?}",
request.messages.len(),
@@ -651,7 +662,7 @@ impl Completer {
// The spend ledger is an explicit argument of the call, not an ambient scope.
let call = openhuman_core::inference::host_runtime::ops::complete_once_controlled(
&endpoint,
- request.into_wire(),
+ wire_request,
budget.as_ref(),
self.observers.physical_dispatch.clone(),
self.dispatch_admission.clone(),
@@ -666,7 +677,9 @@ impl Completer {
usage: None,
};
}
- error => error.to_string(),
+ tinyagents_harness::cost::ControlledDispatchError::Model(source) => {
+ format!("complete_once: {source}")
+ }
};
if let Some(source) = budget.as_ref().and_then(|budget| budget.ledger.refusal()) {
return CoreError::BudgetExceeded {
diff --git a/crates/openhuman-embed/src/complete_tests.rs b/crates/openhuman-embed/src/complete_tests.rs
index 248ed77f9d..7b4d2f8436 100644
--- a/crates/openhuman-embed/src/complete_tests.rs
+++ b/crates/openhuman-embed/src/complete_tests.rs
@@ -797,7 +797,7 @@ async fn provider_failure_keeps_the_complete_once_prefix() {
.expect_err("a provider failure must preserve the legacy completion context");
assert!(
- matches!(error, CoreError::Rpc { message, .. } if message.starts_with("complete_once: model error:")),
+ matches!(&error, CoreError::Rpc { message, .. } if message.starts_with("complete_once: model error:")),
"{error:?}"
);
}