The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(session_host): finalize deferred terminal on cancelled or failed turns
Steven Enamakel7d96b1ed7c08
diff --git a/crates/openhuman-core/src/agent/session_host/driver.rs b/crates/openhuman-core/src/agent/session_host/driver.rsindex 469c63c22a..85d4d952d1 100644--- a/crates/openhuman-core/src/agent/session_host/driver.rs+++ b/crates/openhuman-core/src/agent/session_host/driver.rs@@ -233,6 +233,14 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {));}};+ // From here onward close, required-output repair, and durable commit+ // can still be cancelled or fail. Keep the native terminal handle on+ // the per-turn sidecar immediately so `on_terminal` can finalize it+ // after Tiny persists any recoverable partial transcript.+ sidecar+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .deferred_terminal = outcome.deferred_terminal.take();if outcome.text.trim().is_empty() && outcome.tool_outcomes.is_empty() {return Err(driver_error(@@ -481,7 +489,6 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {);}observed.resolved_route = outcome.resolved_route.clone();- observed.deferred_terminal = outcome.deferred_terminal.take();};// The turn compacted its context: persist the compacted history, so the// next turn starts from the checkpoint. The runtime session seals thediff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rsindex 4e6c8b2c52..49948a204d 100644--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs@@ -59,6 +59,12 @@ pub(super) struct OpenHumanSessionState {pub(super) reply_language_directive: Option<String>,pub(super) time_zone: Option<String>,prelude: Option<OpenHumanTurnPrelude>,+ /// Deferred native terminal belonging to the turn currently owned by the+ /// runtime. The session's terminal callback drains it after partial+ /// transcript persistence on cancellation or failure.+ active_terminal_sidecar: Option<+ std::sync::Arc<std::sync::Mutex<crate::agent::tinyagents::host::SessionTurnSidecar>>,+ >,/// The previous turn's deferred post-commit work (thread-goal/// accounting). The next turn awaits it before loading the goal/// (`post_commit::await_pending`).@@ -704,11 +710,14 @@ impl OpenHumanSessionHost {.lock().unwrap_or_else(std::sync::PoisonError::into_inner).request_base_len = request_base_len;- let prelude = state- .lock()- .unwrap_or_else(|poisoned| poisoned.into_inner())- .prelude- .clone();+ let (prelude, pending_post_commit) = {+ let mut state = state+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner());+ state.active_terminal_sidecar =+ Some(options.run_context.data.session_sidecar.clone());+ (state.prelude.clone(), state.pending_post_commit.take())+ };let prelude = prelude.ok_or({tinyagents_runtime::RuntimeError::MissingDependency("OpenHumanTurnPrelude",@@ -716,11 +725,6 @@ impl OpenHumanSessionHost {})?;// The previous turn's goal accounting was deferred past// its completion; settle it before this turn reads the goal.- let pending_post_commit = state- .lock()- .unwrap_or_else(|poisoned| poisoned.into_inner())- .pending_post_commit- .take();post_commit::await_pending(pending_post_commit).await;let new_session = !view.resumed && view.history.is_empty();prelude@@ -870,11 +874,38 @@ impl OpenHumanSessionHost {move |terminal| {let state = state.clone();Box::pin(async move {- state- .lock()- .unwrap_or_else(|poisoned| poisoned.into_inner())- .terminals- .push(terminal);+ let sidecar = {+ let mut state = state+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner());+ state.terminals.push(terminal.clone());+ state.active_terminal_sidecar.take()+ };+ if let Some(sidecar) = sidecar {+ let (deferred, stop, turn) = {+ let mut sidecar = sidecar+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ (+ sidecar.deferred_terminal.take(),+ sidecar.stop.clone(),+ (sidecar.model_calls as u32).max(1),+ )+ };+ if let Some(deferred) = deferred {+ match terminal {+ tinyagents_runtime::SessionTerminal::Completed(_) => {+ deferred.finish(stop, turn).await;+ }+ tinyagents_runtime::SessionTerminal::Cancelled => {+ deferred.finish_cancelled().await;+ }+ tinyagents_runtime::SessionTerminal::Failed(_) => {+ deferred.finish_failed().await;+ }+ }+ }+ }Ok(())})}diff --git a/crates/openhuman-core/src/agent/tinyagents/journal.rs b/crates/openhuman-core/src/agent/tinyagents/journal.rsindex ebe41b2214..9ffd7a4f02 100644--- a/crates/openhuman-core/src/agent/tinyagents/journal.rs+++ b/crates/openhuman-core/src/agent/tinyagents/journal.rs@@ -173,6 +173,23 @@ impl TurnJournal {),}}++ /// Best-effort terminal write for a cooperatively cancelled run.+ pub(crate) async fn finish_cancelled(&self, reason: &str) {+ self.event_sink.flush();+ let snapshot = {+ let mut guard = self.status.lock().unwrap();+ guard.mark_cancelled(reason);+ guard.clone()+ };+ match self.status_store.put_status(snapshot).await {+ Ok(()) => log::debug!("[journal] run cancelled run_id={}", self.run_id.as_str()),+ Err(err) => log::debug!(+ "[journal] cancelled status write failed run_id={} err={err}",+ self.run_id.as_str()+ ),+ }+ }}/// Attach a durable event journal + status writer to `events`, *in addition to*diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rs b/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rsindex 76bc820b69..e6747d62f3 100644--- a/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rs+++ b/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rs@@ -41,6 +41,56 @@ impl DeferredTurnTerminal {journal.finish_completed().await;}}++ /// Finalize a host turn that was cancelled after the harness had returned.+ /// The host callback runs after Tiny persists any recoverable partial.+ pub(crate) async fn finish_cancelled(self) {+ use tinyagents_harness::terminal::{TerminalOutcome, TerminalReason};+ self.events.emit(+ tinyagents_harness::events::AgentEvent::RunFailed {+ run_id: self.run_id.clone(),+ error: "turn cancelled after harness completion".into(),+ outcome: Some(TerminalOutcome::new(+ TerminalReason::Cancelled,+ "turn cancelled after harness completion",+ )),+ },+ );+ self.events+ .emit(tinyagents_harness::events::AgentEvent::RunFinalized {+ run_id: self.run_id,+ });+ if let Some(journal) = self.journal {+ journal+ .finish_cancelled("turn cancelled after harness completion")+ .await;+ }+ }++ /// Finalize a host turn that failed after the harness had returned.+ /// Arbitrary runtime error text is kept out of the content-free trace.+ pub(crate) async fn finish_failed(self) {+ use tinyagents_harness::terminal::{TerminalOutcome, TerminalReason};+ self.events.emit(+ tinyagents_harness::events::AgentEvent::RunFailed {+ run_id: self.run_id.clone(),+ error: "host turn failed after harness completion".into(),+ outcome: Some(TerminalOutcome::new(+ TerminalReason::Internal,+ "host turn failed after harness completion",+ )),+ },+ );+ self.events+ .emit(tinyagents_harness::events::AgentEvent::RunFinalized {+ run_id: self.run_id,+ });+ if let Some(journal) = self.journal {+ journal+ .finish_failed("host turn failed after harness completion")+ .await;+ }+ }}/// The outcome of a turn driven on the `tinyagents` harness.