Skip to content

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

fix: restore session runtime turn guarantees

Steven Enamakel93353067f0e7
diff --git a/crates/openhuman-core/src/agent/session_host/driver.rs b/crates/openhuman-core/src/agent/session_host/driver.rs
index 254f0559a9..b309a19b48 100644
--- a/crates/openhuman-core/src/agent/session_host/driver.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver.rs
@@ -32,6 +32,7 @@ pub struct OpenHumanSessionDriver {
model_name: String,
temperature: f64,
max_iterations: usize,
+ max_history_messages: usize,
model_vision: bool,
run_queue:
Option<Arc<tinyagents_harness::run_queue::RunQueue<crate::agent::queued_turn::QueuedTurn>>>,
@@ -49,6 +50,7 @@ impl OpenHumanSessionDriver {
model_name: String,
temperature: f64,
max_iterations: usize,
+ max_history_messages: usize,
model_vision: bool,
run_queue: Option<
Arc<tinyagents_harness::run_queue::RunQueue<crate::agent::queued_turn::QueuedTurn>>,
@@ -64,6 +66,7 @@ impl OpenHumanSessionDriver {
model_name,
temperature,
max_iterations,
+ max_history_messages,
model_vision,
run_queue,
workspace,
@@ -179,7 +182,7 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
.collect();
ensure_snapshot_tools_are_executable(&visible_tool_names, &tools, &synthesized_tools)?;
let run_context = request.run_context.data.clone();
- let outcome = match graph::run_chat_turn_graph(ChatTurnGraph {
+ let mut outcome = match graph::run_chat_turn_graph(ChatTurnGraph {
turn_models,
model: self.model_name.clone(),
messages,
@@ -213,6 +216,35 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
}
};
+ if outcome.text.trim().is_empty() && outcome.tool_outcomes.is_empty() {
+ return Err(driver_error(
+ crate::agent::error::AgentError::EmptyProviderResponse {
+ iteration: outcome.model_calls,
+ },
+ ));
+ }
+ // A cap pause is advisory in the harness. Treat an exhausted loop
+ // without a usable final response as capped even if that pause arrived
+ // after the loop's own limit check.
+ outcome.hit_cap |= outcome.text.trim().is_empty()
+ && (outcome.model_calls >= self.max_iterations
+ || outcome.tool_calls >= self.max_iterations
+ || outcome.tool_outcomes.len() >= self.max_iterations);
+ // Older hosted-harness paths materialize this fallback before exposing
+ // the outcome, which leaves the raw call counters unavailable here.
+ // It is only produced when the loop exhausted a tool round without a
+ // model conclusion, so normalize it to the same resumable cap state.
+ let exhausted_fallback = outcome
+ .text
+ .starts_with("I finished this turn without writing up a result.");
+ outcome.hit_cap |= exhausted_fallback;
+ // The fallback is not a model-authored wrap-up, even when the shared
+ // middleware reports that it attempted one. Let the grounded close
+ // replace it with the explicit resumable checkpoint.
+ if exhausted_fallback {
+ outcome.wrap_up_injected = false;
+ }
+
let close = grounded_close::close_if_needed(
&self.turn_model_source,
&self.model_name,
@@ -228,6 +260,20 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
.as_ref()
.map(|close| close.output.clone())
.unwrap_or_else(|| outcome.text.clone());
+ // A tools-only loop may reach the close path after the hosted runtime
+ // has already dropped its cap metadata. Its generic final-summary
+ // fallback is not a completed answer; preserve resumability by making
+ // the durable reply an explicit checkpoint.
+ if outcome.text.trim().is_empty()
+ && output.starts_with("I finished this turn without writing up a result.")
+ {
+ output = crate::agent::session_host::turn_checkpoint::build_deterministic_checkpoint(
+ &crate::agent::session_host::turn_checkpoint::results_from_tool_outcomes(
+ &outcome.tool_outcomes,
+ ),
+ self.max_iterations,
+ );
+ }
let mut history = request.history;
let conversation = crate::agent::message_convert::provider_messages_from_conversation(
self.dispatcher.as_ref(),
@@ -278,6 +324,7 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
}
history.push(Message::assistant(output.clone()));
}
+ trim_history(&mut history, self.max_history_messages);
// This is deliberately an out-of-band observation rather than a
// second history or transcript. The runtime only reads it from
@@ -344,6 +391,20 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
}
}
+/// Preserve the stable system prefix while bounding durable conversational
+/// history. The runtime owns history replacement, so this must happen before
+/// its successful `DriverOutcome` is committed.
+fn trim_history(history: &mut Vec<Message>, max_history_messages: usize) {
+ let prefix_len = history
+ .iter()
+ .take_while(|message| matches!(message, Message::System(_)))
+ .count();
+ let retained = history.len().saturating_sub(prefix_len);
+ if retained > max_history_messages {
+ history.drain(prefix_len..prefix_len + retained - max_history_messages);
+ }
+}
+
fn driver_error(error: impl std::fmt::Display) -> DriverFailure {
driver_failure(error)
}
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index 4c6233c62c..e611d1f4e3 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -921,14 +921,17 @@ impl OpenHumanTurnPrelude {
}
async fn finalize_after_durable_commit(&self, receipt: &CommitReceipt<OpenHumanRunContext>) {
- self.flush_user_autosave().await;
+ if self.flush_user_autosave().await {
+ self.flush_assistant_autosave(receipt.outcome.output.as_deref())
+ .await;
+ }
self.mirror_transcript_after_commit(receipt);
self.spawn_transcript_ingestion_after_commit(receipt);
self.spawn_session_memory_extraction_after_commit(receipt)
.await;
}
- async fn flush_user_autosave(&self) {
+ async fn flush_user_autosave(&self) -> bool {
let message = self
.mutable
.lock()
@@ -936,9 +939,20 @@ impl OpenHumanTurnPrelude {
.pending_user_autosave
.take();
let Some(message) = message else {
+ return false;
+ };
+ self.store_autosave_message("user_msg", &message).await
+ }
+
+ async fn flush_assistant_autosave(&self, message: Option<&str>) {
+ let Some(message) = message.filter(|message| !message.trim().is_empty()) else {
return;
};
- let key = format!("user_msg:{}", uuid::Uuid::new_v4());
+ self.store_autosave_message("assistant_msg", message).await;
+ }
+
+ async fn store_autosave_message(&self, kind: &str, message: &str) -> bool {
+ let key = format!("{kind}:{}", uuid::Uuid::new_v4());
if let Err(error) = self
.memory
.store(
@@ -950,9 +964,10 @@ impl OpenHumanTurnPrelude {
)
.await
{
- log::warn!(
- "[agent_autosave] durable user-message autosave failed key={key} err={error}"
- );
+ log::warn!("[agent_autosave] durable message autosave failed kind={kind} key={key} err={error}");
+ false
+ } else {
+ true
}
}
@@ -1408,6 +1423,7 @@ impl OpenHumanSessionHost {
self.model_name.clone(),
self.temperature,
self.config.max_tool_iterations,
+ self.config.max_history_messages,
self.model_vision,
self.run_queue.clone(),
self.workspace_descriptor.clone(),
diff --git a/crates/openhuman-core/src/agent/tinyagents/tools.rs b/crates/openhuman-core/src/agent/tinyagents/tools.rs
index 8319f7936e..1d400f921c 100644
--- a/crates/openhuman-core/src/agent/tinyagents/tools.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/tools.rs
@@ -150,7 +150,16 @@ impl Tool for CanonicalSharedToolAdapter {
tracing::warn!(tool = %self.name, "[tinyagents] shared tool not found");
return Ok(ToolResult::error(format!("unknown tool '{}'", self.name)));
};
- let result = tool.execute_with_context(args, options, context).await?;
+ // A callable tool's operational failure is input to the agent loop, not
+ // a failure of the harness itself. Preserve it as an error result so
+ // the model can recover (or explain the failure) on its next round.
+ let result = match tool.execute_with_context(args, options, context).await {
+ Ok(result) => result,
+ Err(error) => {
+ tracing::warn!(tool = %self.name, %error, "[tinyagents] shared tool execution failed");
+ ToolResult::error(format!("{} failed: {error}", self.name))
+ }
+ };
if !result.is_error {
if let Some(hook) = &self.early_exit {
hook.trigger(&self.name, result.output_for_llm(true));