Skip to content

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

refactor(agent): improve session lease handling and structure

Steven Enamakel95922587e399
diff --git a/crates/openhuman-core/src/agent/session_lease.rs b/crates/openhuman-core/src/agent/session_lease.rs
index 969c0644d4..2d5c57bfa0 100644
--- a/crates/openhuman-core/src/agent/session_lease.rs
+++ b/crates/openhuman-core/src/agent/session_lease.rs
@@ -5,7 +5,10 @@
//! the host boundary, keyed by the complete tenant, agent, and canonical
//! session identity.
-use std::{collections::HashMap, sync::{Arc, LazyLock, Mutex, Weak}};
+use std::{
+ collections::HashMap,
+ sync::{Arc, LazyLock, Mutex, Weak},
+};
use tinyagents_harness::CancellationToken;
use tokio::sync::{Mutex as AsyncMutex, OwnedMutexGuard};
@@ -18,8 +21,16 @@ pub(crate) struct SessionLeaseKey {
}
impl SessionLeaseKey {
- pub(crate) fn new(tenant: impl Into<String>, agent: impl Into<String>, session: impl Into<String>) -> Self {
- Self { tenant: tenant.into(), agent: agent.into(), session: session.into() }
+ pub(crate) fn new(
+ tenant: impl Into<String>,
+ agent: impl Into<String>,
+ session: impl Into<String>,
+ ) -> Self {
+ Self {
+ tenant: tenant.into(),
+ agent: agent.into(),
+ session: session.into(),
+ }
}
}
@@ -38,7 +49,9 @@ impl SessionLease {
cancellation: Option<&CancellationToken>,
) -> Result<Self, ()> {
let lock = {
- let mut leases = LEASES.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
+ let mut leases = LEASES
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
leases.retain(|_, lock| lock.strong_count() > 0);
if let Some(lock) = leases.get(&key).and_then(Weak::upgrade) {
lock
diff --git a/crates/openhuman-core/src/agent/session_lease_tests.rs b/crates/openhuman-core/src/agent/session_lease_tests.rs
index 960804ea9e..b377aae64e 100644
--- a/crates/openhuman-core/src/agent/session_lease_tests.rs
+++ b/crates/openhuman-core/src/agent/session_lease_tests.rs
@@ -13,7 +13,10 @@ async fn independent_runners_serialize_the_same_tenant_agent_session() {
async move { SessionLease::acquire(key, Some(&cancellation)).await }
});
tokio::task::yield_now().await;
- assert!(!waiting.is_finished(), "same identity must wait for checkout");
+ assert!(
+ !waiting.is_finished(),
+ "same identity must wait for checkout"
+ );
drop(first);
let second = tokio::time::timeout(std::time::Duration::from_secs(1), waiting)
diff --git a/crates/openhuman-core/src/web_chat/runner.rs b/crates/openhuman-core/src/web_chat/runner.rs
index df716a49c3..6ac4f08e1c 100644
--- a/crates/openhuman-core/src/web_chat/runner.rs
+++ b/crates/openhuman-core/src/web_chat/runner.rs
@@ -17,7 +17,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Duration;
use tinyagents_harness::turn_runner::{ScopeKey, TurnRunner, TurnRunnerConfig};
-use tokio::sync::{Mutex as AsyncMutex, oneshot};
+use tokio::sync::{oneshot, Mutex as AsyncMutex};
use super::ingress::WebIngress;
use super::ops::StartChatError;
@@ -28,7 +28,7 @@ use super::turn_events::{WebEvents, WebFailures, WebReplies, WebSummarizer};
use super::turn_host::WebHost;
use super::types::WebChatTaskResult;
use crate::agent::queued_turn::QueuedPayload;
-use crate::agent::session_lease::{SessionLease, SessionLeaseKey};
+use crate::agent::session_lease::SessionLease;
use crate::core::runtime::DispatchCtx;
pub(super) type WebRunner = TurnRunner<
diff --git a/crates/openhuman-core/src/web_chat/turn_events.rs b/crates/openhuman-core/src/web_chat/turn_events.rs
index 82d0b4ee8e..9d5a44c447 100644
--- a/crates/openhuman-core/src/web_chat/turn_events.rs
+++ b/crates/openhuman-core/src/web_chat/turn_events.rs
@@ -19,10 +19,10 @@ use tinychannels_bus::web as bus_web;
use super::runner::{CancelNote, Shared};
use super::surface::{Surface, TurnIds};
-use super::turn_close::{StopCause, StoppedTurn, close_reply};
+use super::turn_close::{close_reply, StopCause, StoppedTurn};
use super::web_errors::{
- ClassifiedError, classify_inference_error, is_outer_backstop_timeout, turn_idle_error_message,
- turn_timeout_error_message,
+ classify_inference_error, is_outer_backstop_timeout, turn_idle_error_message,
+ turn_timeout_error_message, ClassifiedError,
};
use crate::agent::queued_turn::QueuedPayload;
@@ -425,7 +425,12 @@ impl EventSink<QueuedPayload> for WebEvents {
// announced as a turn, so its being dropped is silent too.
let never_started = self.shared.was_queued(&request.request_id);
let (projection, done, note, _session_lease) = match slot {
- Some(slot) => (slot.projection, slot.result, slot.cancel, slot.session_lease),
+ Some(slot) => (
+ slot.projection,
+ slot.result,
+ slot.cancel,
+ slot.session_lease,
+ ),
None => (None, None, None, None),
};
if never_started && !matches!(event, TerminalEvent::Completed { .. }) {
diff --git a/crates/openhuman-core/src/web_chat/turn_events_tests.rs b/crates/openhuman-core/src/web_chat/turn_events_tests.rs
index accf65b142..22bf33ab71 100644
--- a/crates/openhuman-core/src/web_chat/turn_events_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_events_tests.rs
@@ -166,10 +166,9 @@ async fn a_stopped_turn_announces_cancelled_to_the_client_that_stopped_it() {
cancel_reason: Some(reason), ..
} if reason == "user_stop")
);
- assert!(
- seen.iter()
- .all(|event| event.context().client_id == "stopper")
- );
+ assert!(seen
+ .iter()
+ .all(|event| event.context().client_id == "stopper"));
}
#[tokio::test]
@@ -264,12 +263,10 @@ async fn a_turn_stopped_before_its_config_resolved_cannot_be_closed() {
let shared = Arc::new(Shared::default());
let summarizer = WebSummarizer::new(shared.clone());
let req = request("close-none", QueueDisposition::Interrupt);
- assert!(
- summarizer
- .grounded_close(&req, RUNNER_DEADLINE)
- .await
- .is_none()
- );
+ assert!(summarizer
+ .grounded_close(&req, RUNNER_DEADLINE)
+ .await
+ .is_none());
assert!(summarizer.grounded_close(&req, RUNNER_IDLE).await.is_none());
}
diff --git a/crates/openhuman-core/src/web_chat/turn_host.rs b/crates/openhuman-core/src/web_chat/turn_host.rs
index 4f188dd679..30fae5f2ef 100644
--- a/crates/openhuman-core/src/web_chat/turn_host.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host.rs
@@ -112,7 +112,8 @@ impl WebHost {
)
.await
.map_err(|()| "agent session lease was cancelled".to_owned())?;
- self.shared.with(id, |slot| slot.session_lease = Some(lease));
+ self.shared
+ .with(id, |slot| slot.session_lease = Some(lease));
// A follow-up the runner dispatches after its predecessor finished was
// admitted when it was queued; one started directly still has its
// admission to answer.