Skip to content

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

fix(chat): retry empty completed model streams once

Steven Enamakel43660e6ef0b0
diff --git a/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs b/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs
index 1d31c04cfa..a02b5375bb 100644
--- a/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs
@@ -95,6 +95,12 @@ fn run_policy_for_makes_invalid_tool_arguments_recoverable() {
);
}
+#[test]
+fn run_policy_retries_one_nontruncated_empty_completion() {
+ let policy = run_policy_for(10, false);
+ assert_eq!(policy.empty_response_retries, 1);
+}
+
#[test]
fn parse_model_call_wall_clock_defaults_to_fifteen_minutes() {
// Absent and unparseable values both fall back to the 900s default.
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
index 9818625973..25bb5e3d0f 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
@@ -137,6 +137,11 @@ pub(super) fn parse_model_call_wall_clock_ms(env_value: Option<&str>) -> Option<
/// routes (chat→burst, reasoning→agentic, …) the way the harness registry can.
pub(crate) fn run_policy_for(max_iterations: usize, response_cache_enabled: bool) -> RunPolicy {
let mut policy = RunPolicy::default();
+ // A managed streaming response can finish with reasoning but no visible
+ // answer. Reissue that unusable call once; the harness keeps the same
+ // output cap and drops the blank assistant row before retrying. If it
+ // repeats, the existing EmptyProviderResponse path remains actionable.
+ policy.empty_response_retries = 1;
policy.limits.max_model_calls = max_iterations;
policy.limits.max_tool_calls = max_iterations.saturating_mul(8).max(8);
policy.limits.max_depth = MAX_SPAWN_DEPTH;
diff --git a/crates/openhuman-core/src/inference/provider/openhuman_backend_model_stream_tests.rs b/crates/openhuman-core/src/inference/provider/openhuman_backend_model_stream_tests.rs
new file mode 100644
index 0000000000..2c3be04843
--- /dev/null
+++ b/crates/openhuman-core/src/inference/provider/openhuman_backend_model_stream_tests.rs
@@ -0,0 +1,153 @@
+//! Managed-model SSE proxy replay and empty-response recovery.
+
+use super::*;
+
+async fn spawn_sse_chat_server(chunks: Vec<Value>) -> String {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
+ .await
+ .expect("bind SSE proxy");
+ let addr = listener.local_addr().expect("SSE proxy address");
+ let body = chunks
+ .into_iter()
+ .map(|chunk| format!("data: {chunk}\n\n"))
+ .collect::<String>()
+ + "data: [DONE]\n\n";
+ let app = axum::Router::new()
+ .route(
+ "/openai/v1/chat/completions",
+ axum::routing::post(
+ |axum::extract::State(body): axum::extract::State<String>| async move {
+ (
+ [(axum::http::header::CONTENT_TYPE, "text/event-stream")],
+ body,
+ )
+ },
+ ),
+ )
+ .with_state(body);
+ tokio::spawn(async move {
+ axum::serve(listener, app).await.expect("serve SSE proxy");
+ });
+ addr.to_string()
+}
+
+async fn spawn_recovering_sse_proxy(
+ first_delta: Value,
+) -> (String, std::sync::Arc<std::sync::atomic::AtomicUsize>) {
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+ #[derive(Clone)]
+ struct ProxyState {
+ first_delta: Value,
+ calls: std::sync::Arc<AtomicUsize>,
+ }
+
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
+ .await
+ .expect("bind recovering SSE proxy");
+ let addr = listener.local_addr().expect("recovering proxy address");
+ let calls = std::sync::Arc::new(AtomicUsize::new(0));
+ let app = axum::Router::new()
+ .route(
+ "/openai/v1/chat/completions",
+ axum::routing::post(
+ |axum::extract::State(state): axum::extract::State<ProxyState>| async move {
+ let attempt = state.calls.fetch_add(1, Ordering::SeqCst);
+ let delta = if attempt == 0 {
+ state.first_delta
+ } else {
+ serde_json::json!({ "content": "Hello there" })
+ };
+ let body = format!(
+ "data: {}\n\ndata: {}\n\ndata: [DONE]\n\n",
+ serde_json::json!({ "choices": [{ "index": 0, "delta": delta }] }),
+ serde_json::json!({ "choices": [{ "index": 0, "delta": {}, "finish_reason": "stop" }] }),
+ );
+ ([(axum::http::header::CONTENT_TYPE, "text/event-stream")], body)
+ },
+ ),
+ )
+ .with_state(ProxyState {
+ first_delta,
+ calls: calls.clone(),
+ });
+ tokio::spawn(async move {
+ axum::serve(listener, app)
+ .await
+ .expect("serve recovering SSE proxy");
+ });
+ (addr.to_string(), calls)
+}
+
+#[tokio::test]
+async fn managed_stream_proxy_distinguishes_visible_and_reasoning_only_content() {
+ use futures::StreamExt;
+ use tinyinference_llm::model::StreamAccumulator;
+
+ for (delta, expected_text) in [
+ (
+ serde_json::json!({ "content": "Hello there" }),
+ "Hello there",
+ ),
+ (serde_json::json!({ "reasoning": "Hello there" }), ""),
+ (serde_json::json!({ "content": "<think>Hello there" }), ""),
+ ] {
+ let tmp = tempfile::TempDir::new().expect("scratch credentials");
+ seed_app_session(tmp.path());
+ let addr = spawn_sse_chat_server(vec![
+ serde_json::json!({ "choices": [{ "index": 0, "delta": delta }] }),
+ serde_json::json!({ "choices": [{ "index": 0, "delta": {}, "finish_reason": "stop" }] }),
+ ])
+ .await;
+ let backend = backend_pointed_at(&addr, tmp.path());
+ let stream = backend
+ .stream(&(), ModelRequest::new(vec![Message::user("hi")]))
+ .await
+ .expect("mock SSE response");
+ let mut accumulated = StreamAccumulator::new();
+ for item in stream.collect::<Vec<_>>().await {
+ accumulated.push(&item);
+ }
+ let response = accumulated.finish().expect("completed SSE stream");
+ assert_eq!(response.text(), expected_text);
+ assert_eq!(response.finish_reason.as_deref(), Some("stop"));
+ }
+}
+
+#[tokio::test]
+async fn managed_stream_retries_a_proxy_completion_without_visible_text() {
+ use std::sync::atomic::Ordering;
+ use tinyagents_harness::context::RunConfig;
+ use tinyagents_harness::runtime::{AgentHarness, RunPolicy};
+
+ for first_delta in [
+ serde_json::json!({ "reasoning": "Hello there" }),
+ serde_json::json!({ "content": "<think>Hello there" }),
+ ] {
+ let tmp = tempfile::TempDir::new().expect("scratch credentials");
+ seed_app_session(tmp.path());
+ let (addr, calls) = spawn_recovering_sse_proxy(first_delta).await;
+ let mut harness: AgentHarness<()> = AgentHarness::new();
+ harness.register_model(
+ "managed",
+ std::sync::Arc::new(backend_pointed_at(&addr, tmp.path())),
+ );
+ harness.with_policy(RunPolicy {
+ empty_response_retries: 1,
+ ..RunPolicy::default()
+ });
+
+ let run = harness
+ .invoke_streaming(
+ &(),
+ (),
+ RunConfig::new("proxy-retry"),
+ vec![Message::user("hi")],
+ )
+ .await
+ .expect("second proxy completion should produce a visible answer");
+ assert_eq!(run.text(), Some("Hello there".to_string()));
+ assert_eq!(run.model_calls, 2);
+ assert_eq!(calls.load(Ordering::SeqCst), 2);
+ }
+}
diff --git a/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs b/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs
index 0a6a75f8d0..9cd0468cd6 100644
--- a/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs
+++ b/crates/openhuman-core/src/inference/provider/openhuman_backend_model_tests.rs
@@ -317,6 +317,9 @@ async fn spawn_static_chat_server(status: axum::http::StatusCode, body: Value) -
addr.to_string()
}
+#[path = "openhuman_backend_model_stream_tests.rs"]
+mod stream_tests;
+
async fn slow_chat_handler() -> axum::response::Response {
use axum::response::IntoResponse;
// Longer than the probe's 5s timeout — the probe must return before
diff --git a/vendor/tinyagents b/vendor/tinyagents
index c0ea84492e..5fcae3899d 160000
--- a/vendor/tinyagents
+++ b/vendor/tinyagents
@@ -1 +1 @@
-Subproject commit c0ea84492e2a6dd0eb3866f35bd90e96f9381241
+Subproject commit 5fcae3899d91eee6edbc3349cceb79bdd97c4e8a