Skip to content

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

fix: validate routed turns and scope detached flows

Steven Enamakel1d73ade251f8
diff --git a/src/embed/agent.rs b/src/embed/agent.rs
index dc67e03548..ba058c84dd 100644
--- a/src/embed/agent.rs
+++ b/src/embed/agent.rs
@@ -322,6 +322,8 @@ impl Turn<'_> {
self.request.cwd.is_some(),
);
+ validate_route(&self.request)?;
+
// Never transmit the bearer over a non-TLS channel. The route accepts
// an arbitrary base URL, so guard here — before any request is built —
// rather than trusting every embedder to only name https endpoints. A
@@ -377,6 +379,7 @@ impl Turn<'_> {
crate::embed::error::CoreError::Encode { .. } => "encode",
crate::embed::error::CoreError::Decode { .. } => "decode",
crate::embed::error::CoreError::InsecureRoute { .. } => "insecure_route",
+ crate::embed::error::CoreError::InvalidRoute { .. } => "invalid_route",
};
log::debug!("[embed][agent] turn_failed session={session_id} kind={tag}");
})?;
@@ -390,6 +393,23 @@ impl Turn<'_> {
}
}
+fn validate_route(request: &TurnRequest) -> Result<(), CoreError> {
+ let route_requested = request.inference_url.is_some() || request.api_key.is_some();
+ if route_requested
+ && (request
+ .inference_url
+ .as_deref()
+ .is_none_or(|value| value.trim().is_empty())
+ || request
+ .api_key
+ .as_deref()
+ .is_none_or(|value| value.trim().is_empty()))
+ {
+ return Err(CoreError::InvalidRoute { method: AGENT_CHAT });
+ }
+ Ok(())
+}
+
/// Absolute form of `dir`, for callers assembling a [`Turn::cwd`] from a
/// relative path. The core resolves a relative `cwd` against its own
/// `action_dir`, which is rarely what a library caller means.
diff --git a/src/embed/agent_tests.rs b/src/embed/agent_tests.rs
index 750fe86509..ebdb8164a0 100644
--- a/src/embed/agent_tests.rs
+++ b/src/embed/agent_tests.rs
@@ -86,6 +86,18 @@ fn a_route_sets_both_halves_or_neither() {
assert_eq!(turn_request.api_key.as_deref(), Some("sk-test"));
}
+#[test]
+fn blank_per_turn_route_is_rejected_before_dispatch() {
+ let mut request = TurnRequest::new("hi");
+ request.inference_url = Some("https://api.example/v1".into());
+ request.api_key = Some(" ".into());
+
+ assert!(matches!(
+ validate_route(&request),
+ Err(CoreError::InvalidRoute { method: AGENT_CHAT })
+ ));
+}
+
#[test]
fn absolute_leaves_absolute_paths_alone() {
let already = std::path::Path::new("/tmp/example");
diff --git a/src/embed/error.rs b/src/embed/error.rs
index 6d65dd2c72..d4bebae892 100644
--- a/src/embed/error.rs
+++ b/src/embed/error.rs
@@ -98,6 +98,14 @@ pub enum CoreError {
/// The sanitized endpoint (credentials stripped) that was refused.
endpoint: String,
},
+
+ /// A per-turn route was supplied but would be discarded by the inference
+ /// controller because one of its required halves is blank.
+ #[error("{method}: routed inference requires a non-blank endpoint and api key")]
+ InvalidRoute {
+ /// RPC method the invalid route was attached to.
+ method: &'static str,
+ },
}
impl CoreError {
@@ -147,7 +155,8 @@ impl CoreError {
| CoreError::Rpc { method, .. }
| CoreError::Encode { method, .. }
| CoreError::Decode { method, .. }
- | CoreError::InsecureRoute { method, .. } => method,
+ | CoreError::InsecureRoute { method, .. }
+ | CoreError::InvalidRoute { method } => method,
}
}
diff --git a/src/openhuman/flows/ops_part_06.rs b/src/openhuman/flows/ops_part_06.rs
index 8c0c8705b8..16d8403952 100644
--- a/src/openhuman/flows/ops_part_06.rs
+++ b/src/openhuman/flows/ops_part_06.rs
@@ -289,7 +289,8 @@ pub async fn flows_run_detached(
let flow = prepared.flow;
let flow_id_owned = flow_id.to_string();
let body_thread_id = thread_id.clone();
- tokio::spawn(async move {
+ tokio::spawn(crate::core::runtime::context::CoreContext::propagate(
+ async move {
if let Err(e) = run_flow_body(
config_arc,
flow,
@@ -308,7 +309,8 @@ pub async fn flows_run_detached(
// finalizer — this only logs that the detached run ended in error.
tracing::warn!(target: "flows", error = %e, "[flows] flows_run_detached: background run ended with error (row already reconciled)");
}
- });
+ },
+ ));
let result = json!({
"run_id": thread_id,