Skip to content

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

feat(agent): deliver background task results to active sessions

Steven Enamakel86bff84f0134
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index 1c72a06472..d6a6b8495d 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -83,25 +83,32 @@ fn session_of<'k>(key: &'k str, me: &Tenant) -> Option<&'k str> {
.map(|(_, session)| session)
}
-/// Mark a user turn on `session_id` as in flight, for the calling profile.
-/// Called by the turn loop itself, inside the turn's scope, because the bus
-/// subscriber runs off-task and could not tell which profile a session
-/// belongs to.
-pub(crate) fn note_turn_started(session_id: &str) {
- let key = tenant::profile_key(session_id);
- busy()
- .lock()
- .expect("background_delivery busy poisoned")
- .insert(key);
+/// A user turn in flight on a session, for the calling profile; the session
+/// counts as busy until this is dropped. Taken by the turn loop itself, inside
+/// the turn's scope, because the bus subscriber runs off-task and could not
+/// tell which profile a session id belongs to. The key is computed once, so
+/// dropping it anywhere (a cancelled turn) clears the right entry.
+pub(crate) struct TurnBusy {
+ key: String,
}
-/// The calling profile's turn on `session_id` ended (completed or failed).
-pub(crate) fn note_turn_ended(session_id: &str) {
- let key = tenant::profile_key(session_id);
- busy()
- .lock()
- .expect("background_delivery busy poisoned")
- .remove(&key);
+impl TurnBusy {
+ pub(crate) fn start(session_id: &str) -> Self {
+ let key = tenant::profile_key(session_id);
+ busy()
+ .lock()
+ .expect("background_delivery busy poisoned")
+ .insert(key.clone());
+ Self { key }
+ }
+}
+
+impl Drop for TurnBusy {
+ fn drop(&mut self) {
+ if let Ok(mut busy) = busy().lock() {
+ busy.remove(&self.key);
+ }
+ }
}
/// Is any in-flight turn of the calling profile running on `thread_id`?
diff --git a/crates/openhuman-core/src/agent/session_host/runtime/run_loop.rs b/crates/openhuman-core/src/agent/session_host/runtime/run_loop.rs
index 7a437b0721..cf9d1bfa04 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime/run_loop.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime/run_loop.rs
@@ -77,7 +77,8 @@ impl OpenHumanSessionHost {
let history_snapshot = self.history();
// Busy state for background-result delivery is marked here, in the
// turn's own (profile) scope; the bus subscriber runs off-task.
- crate::agent::orchestration::background_delivery::note_turn_started(
+ // A guard, so a turn dropped mid-flight does not leave it busy.
+ let busy = crate::agent::orchestration::background_delivery::TurnBusy::start(
self.event_session_id(),
);
BUS.publish(DomainEvent::AgentTurnStarted {
@@ -89,9 +90,7 @@ impl OpenHumanSessionHost {
Ok(response) => {
let history = self.history();
let new_entries = Self::new_entries_for_turn(&history_snapshot, &history);
- crate::agent::orchestration::background_delivery::note_turn_ended(
- self.event_session_id(),
- );
+ drop(busy);
BUS.publish(DomainEvent::AgentTurnCompleted {
session_id: self.event_session_id().to_string(),
text_chars: response.chars().count(),
@@ -145,6 +144,7 @@ impl OpenHumanSessionHost {
],
);
}
+ drop(busy);
BUS.publish(DomainEvent::AgentError {
session_id: self.event_session_id().to_string(),
message: sanitized_message,