Skip to content

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

fix(embed): preserve control reasons across cancellation races

Steven Enamakel2b1b2325b45f
diff --git a/crates/openhuman-embed/src/agent/lifecycle.rs b/crates/openhuman-embed/src/agent/lifecycle.rs
index 7d89b3c869..5bc8e22a2e 100644
--- a/crates/openhuman-embed/src/agent/lifecycle.rs
+++ b/crates/openhuman-embed/src/agent/lifecycle.rs
@@ -31,9 +31,15 @@ impl Lifecycle {
/// Stops admitting turns and ends the ones in flight; returns whether this
/// caller claimed removal before another caller.
pub(crate) fn mark_removed(&self) -> bool {
+ self.mark_removed_with(|| {})
+ }
+
+ /// Claim removal and settle owned approvals before waking in-flight turns.
+ pub(crate) fn mark_removed_with(&self, before_notify: impl FnOnce()) -> bool {
if self.removal_claimed.swap(true, Ordering::SeqCst) {
return false;
}
+ before_notify();
// A cancelled turn can retain the watch read while its future drops.
// Only the first claimant writes; repeated teardown never waits on it.
self.removed.send_replace(true);
diff --git a/crates/openhuman-embed/src/runtime/lifecycle.rs b/crates/openhuman-embed/src/runtime/lifecycle.rs
index 7df17da08a..c216451c61 100644
--- a/crates/openhuman-embed/src/runtime/lifecycle.rs
+++ b/crates/openhuman-embed/src/runtime/lifecycle.rs
@@ -113,7 +113,10 @@ impl Runtime {
.and_then(|weak| weak.upgrade())
.ok_or_else(|| AgentError::UnknownId(id.to_string()))?
};
- if !inner.lifecycle.mark_removed() {
+ if !inner
+ .lifecycle
+ .mark_removed_with(|| inner.deny_approvals("agent_removed"))
+ {
return Err(AgentError::UnknownId(id.to_string()));
}
let mut removal = RemovalSlot {
@@ -123,7 +126,6 @@ impl Runtime {
};
let inner = &removal.inner;
log::debug!("[embed][runtime] removing agent id={id} purge={purge}");
- inner.deny_approvals("agent_removed");
if !inner.lifecycle.wait_idle(REMOVE_IDLE_WAIT).await {
log::warn!(
"[embed][runtime] agent id={id} still had turns unwinding after {}s",
diff --git a/crates/openhuman-embed/src/turn_cancellation.rs b/crates/openhuman-embed/src/turn_cancellation.rs
index 2b8f97ccef..d8da22f817 100644
--- a/crates/openhuman-embed/src/turn_cancellation.rs
+++ b/crates/openhuman-embed/src/turn_cancellation.rs
@@ -51,6 +51,10 @@ impl TurnCancellation {
self.0.cleanup.wait().await;
}
+ pub(crate) fn is_cancelled(&self) -> bool {
+ *self.0.requested.borrow()
+ }
+
pub(crate) async fn cancelled(&self) {
let mut requested = self.0.requested.subscribe();
let _ = requested.wait_for(|requested| *requested).await;
diff --git a/crates/openhuman-embed/src/turn_control.rs b/crates/openhuman-embed/src/turn_control.rs
index caed7bb622..6976449cd1 100644
--- a/crates/openhuman-embed/src/turn_control.rs
+++ b/crates/openhuman-embed/src/turn_control.rs
@@ -228,7 +228,20 @@ impl Turn {
native.cancel();
Err(CoreError::DeadlineExceeded { method: AGENT_CHAT })
},
- outcome = Box::pin(self.send_inner(&meter.usage)) => outcome,
+ outcome = Box::pin(self.send_inner(&meter.usage)) => {
+ // The relay can interrupt a long dispatch poll before
+ // this select polls its control branches again. Keep
+ // the public error tied to the triggering control.
+ if token.as_ref().is_some_and(|token| token.is_cancelled())
+ || cancellation.is_cancelled()
+ {
+ Err(CoreError::TurnCancelled { method: AGENT_CHAT })
+ } else if deadline.is_some_and(|deadline| tokio::time::Instant::now() >= deadline) {
+ Err(CoreError::DeadlineExceeded { method: AGENT_CHAT })
+ } else {
+ outcome
+ }
+ },
}
})
.await;
diff --git a/crates/openhuman-embed/tests/turn_cancellation.rs b/crates/openhuman-embed/tests/turn_cancellation.rs
index e0ca914b68..9277b3c64b 100644
--- a/crates/openhuman-embed/tests/turn_cancellation.rs
+++ b/crates/openhuman-embed/tests/turn_cancellation.rs
@@ -115,15 +115,16 @@ async fn scenario() {
let observed = Arc::new(Outcomes::default());
// A whole-turn deadline drops inference and leaves the same agent reusable.
- assert!(matches!(
- blocked
- .turn("deadline")
- .timeout(Duration::from_millis(100))
- .observer(observed.clone())
- .send()
- .await,
- Err(CoreError::DeadlineExceeded { .. })
- ));
+ let deadline = blocked
+ .turn("deadline")
+ .timeout(Duration::from_millis(100))
+ .observer(observed.clone())
+ .send()
+ .await;
+ assert!(
+ matches!(deadline, Err(CoreError::DeadlineExceeded { .. })),
+ "{deadline:?}"
+ );
assert_eq!(agent.run("after deadline").await.unwrap().reply, "finished");
assert_eq!(
*observed.0.lock().unwrap(),