Skip to content

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

feat(session_host): capture dispatch refusal in driver

Steven Enamakelf6cfb451281d
diff --git a/crates/openhuman-core/src/agent/session_host/driver.rs b/crates/openhuman-core/src/agent/session_host/driver.rs
index a3dd6383d7..740a7407da 100644
--- a/crates/openhuman-core/src/agent/session_host/driver.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver.rs
@@ -584,6 +584,15 @@ fn driver_error_with_snapshot(
) -> DriverFailure {
// Classify from the typed harness error when the chain carries one, rather
// than matching on its rendered text.
+ let dispatch_refusal = error.chain().find_map(|cause| {
+ cause.downcast_ref::<crate::agent::tinyagents::budget::DispatchRefusal>()
+ });
+ if let Some(refusal) = dispatch_refusal {
+ sidecar
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .dispatch_refusal = Some(*refusal);
+ }
let typed = error
.chain()
.find_map(|cause| cause.downcast_ref::<tinyagents_harness::TinyAgentsError>());
diff --git a/crates/openhuman-core/src/agent/session_host/driver_tests.rs b/crates/openhuman-core/src/agent/session_host/driver_tests.rs
index 0b25d057c1..d3b752b9e7 100644
--- a/crates/openhuman-core/src/agent/session_host/driver_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver_tests.rs
@@ -39,6 +39,41 @@ fn graph_failure_persists_only_accepted_snapshot_history() {
.contains("provider rejected follow-up"));
}
+#[test]
+fn physical_dispatch_refusal_survives_the_neutral_driver_error_boundary() {
+ let sidecar = sidecar();
+ let snapshot = Arc::new(std::sync::Mutex::new(
+ crate::agent::tinyagents::middleware::turn_context::TranscriptSnapshot {
+ model_calls: 1,
+ input_tokens: 8,
+ output_tokens: 3,
+ ..Default::default()
+ },
+ ));
+ let refusal = crate::agent::tinyagents::budget::DispatchRefusal::new(
+ crate::agent::tinyagents::budget::DispatchRefusalReason::CallLimit,
+ );
+
+ let failure = driver_error_with_snapshot(
+ anyhow::Error::new(refusal),
+ &snapshot,
+ &sidecar,
+ std::time::Duration::from_millis(1),
+ );
+
+ assert!(matches!(failure.error, RuntimeError::Driver(_)));
+ let observed = sidecar
+ .lock()
+ .unwrap()
+ .dispatch_refusal
+ .expect("typed refusal is retained per turn");
+ assert_eq!(observed.reason(), refusal.reason());
+ let usage = sidecar.lock().unwrap();
+ assert_eq!(usage.model_calls, 1);
+ assert_eq!(usage.input_tokens, 8);
+ assert_eq!(usage.output_tokens, 3);
+}
+
#[test]
fn stalled_model_stream_reports_completed_evidence_instead_of_its_narration() {
let snapshot = Arc::new(std::sync::Mutex::new(
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
index de62640755..fd3202f4ad 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
@@ -74,6 +74,10 @@ impl OpenHumanSessionHost {
run_context: context.into_tinyagents(root_config),
controls: Default::default(),
};
+ // The runtime's neutral `RuntimeError::Driver(String)` channel cannot
+ // carry OpenHuman's typed dispatch refusal. Keep this per-turn sidecar
+ // so the driver can restore the type after recording partial usage.
+ let session_sidecar = options.run_context.data.session_sidecar.clone();
// Load persisted state before the normal turn lifecycle so the host
// can read native recorded declarations for both bound sessions and
// cold, unthreaded latest-agent resumes.
@@ -141,9 +145,20 @@ impl OpenHumanSessionHost {
SessionTurnRequest::new(user_message_from_text(staged.as_str())),
options,
);
- let outcome = Box::pin(turn)
- .await
- .map_err(|error| anyhow::anyhow!(error.to_string()))?;
+ let outcome = match Box::pin(turn).await {
+ Ok(outcome) => outcome,
+ Err(error) => {
+ let refusal = session_sidecar
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .dispatch_refusal
+ .take();
+ if let Some(refusal) = refusal {
+ return Err(anyhow::Error::new(refusal));
+ }
+ return Err(anyhow::anyhow!(error.to_string()));
+ }
+ };
Ok(outcome.output.unwrap_or_default())
}
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
index 747ce74cc3..2a0285b989 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -130,6 +130,9 @@ pub(crate) struct SessionTurnSidecar {
/// and the post-commit UI use the same complete ledger.
pub subagents: Vec<SubagentUsageEntry>,
pub duration: Option<Duration>,
+ /// Typed host refusal captured before the neutral runtime's string error
+ /// boundary. Read only by the same logical turn that populated it.
+ pub dispatch_refusal: Option<super::budget::DispatchRefusal>,
pub tool_outcomes: Vec<ToolCallOutcome>,
pub hit_cap: bool,
pub wrap_up_injected: bool,
@@ -418,12 +421,12 @@ pub(crate) struct HarnessHostPolicy {
#[cfg(test)]
pub(crate) use super::operation_context::tool_context_with_approval;
+pub use super::operation_context::{approval_for_tool, OpenHumanRunExt};
pub(crate) use super::operation_context::{
- HostOperationContext, command_environment_for_tool, dispatch_ctx_for_tool,
- memory_identity_for_tool, memory_in_prompt_for_tool, parent_agent_id_for_tool,
- sandbox_mode_for_tool, task_recency_window_for_tool, turn_origin_for_tool,
+ command_environment_for_tool, dispatch_ctx_for_tool, memory_identity_for_tool,
+ memory_in_prompt_for_tool, parent_agent_id_for_tool, sandbox_mode_for_tool,
+ task_recency_window_for_tool, turn_origin_for_tool, HostOperationContext,
};
-pub use super::operation_context::{OpenHumanRunExt, approval_for_tool};
impl Default for OpenHumanRunContext {
fn default() -> Self {
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs
index 9ae36f535e..116685fbe5 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs
@@ -109,20 +109,16 @@ fn child_inherits_tree_handles_but_isolates_observations_and_usage() {
assert_eq!(child.spawn_depth, 1);
assert_eq!(child.thread_id.as_deref(), Some("thread-a"));
assert!(child.file_state_agent_id().is_none());
- assert!(
- child
- .resolved_route
- .lock()
- .expect("child route lock")
- .is_none()
- );
- assert!(
- child
- .subagent_usage
- .lock()
- .expect("child usage lock")
- .is_empty()
- );
+ assert!(child
+ .resolved_route
+ .lock()
+ .expect("child route lock")
+ .is_none());
+ assert!(child
+ .subagent_usage
+ .lock()
+ .expect("child usage lock")
+ .is_empty());
parent.cancellation.cancel();
assert!(child.cancellation.is_cancelled());