Skip to content

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

fix(caps_host): honor cancellation token in agent runs

Steven Enamakel7fc897fb43dc
diff --git a/crates/openhuman-core/src/flows/caps_host/service.rs b/crates/openhuman-core/src/flows/caps_host/service.rs
index 81a0e876d8..9dd196f584 100644
--- a/crates/openhuman-core/src/flows/caps_host/service.rs
+++ b/crates/openhuman-core/src/flows/caps_host/service.rs
@@ -196,9 +196,10 @@ impl CapsHostService {
state.error = Some("agent request was cancelled before admission".to_owned());
state.done = true;
}
+ let handle = admission.entry.handle.clone();
if admission.start_task {
let task_binding = Arc::clone(&binding);
- let entry = admission.entry;
+ let entry = admission.entry.clone();
tokio::spawn(async move {
let _ = service_agent::run_agent(
task_binding,
@@ -209,9 +210,7 @@ impl CapsHostService {
.await;
});
}
- Ok(runs::AgentRunHandle {
- handle: admission.entry.handle,
- })
+ Ok(runs::AgentRunHandle { handle })
}
async fn agent_cancel(
diff --git a/crates/openhuman-core/src/flows/caps_host/service_agent.rs b/crates/openhuman-core/src/flows/caps_host/service_agent.rs
index b782bc57da..cb16ea5e83 100644
--- a/crates/openhuman-core/src/flows/caps_host/service_agent.rs
+++ b/crates/openhuman-core/src/flows/caps_host/service_agent.rs
@@ -12,7 +12,14 @@ pub(crate) async fn run_agent(
binding: Arc<CapsHostBinding>,
request: runs::AgentRunRequest,
state: Arc<tokio::sync::Mutex<AgentRunState>>,
+ cancellation: tokio_util::sync::CancellationToken,
) -> Result<runs::AgentRunOutcome, String> {
+ if cancellation.is_cancelled() {
+ let mut state = state.lock().await;
+ state.error = Some("agent run was cancelled".to_owned());
+ state.done = true;
+ return Err("agent run was cancelled".to_owned());
+ }
let (progress_tx, mut progress_rx) = tokio::sync::mpsc::channel(128);
let progress_state = Arc::clone(&state);
let progress = tokio::spawn(async move {
@@ -69,13 +76,13 @@ pub(crate) async fn run_agent(
progress: Some(progress_tx),
};
let scope = Arc::clone(&binding);
- let cancellation = binding.cancellation.clone();
let turn = scope.within(async move {
runner
.run_agent(&agent_ref, config, connection.as_deref())
.await
});
let result = tokio::select! {
+ biased;
_ = cancellation.cancelled() => Err("workflow run was cancelled".to_owned()),
result = turn => result.map_err(|error| error.to_string()),
};