The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(channels): cover typing task context isolation
Steven Enamakel1d56963811d0
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/helpers_tests.rs b/crates/openhuman-core/src/channels/runtime/dispatch/helpers_tests.rsindex 2e8987c603..afa84eb92b 100644--- a/crates/openhuman-core/src/channels/runtime/dispatch/helpers_tests.rs+++ b/crates/openhuman-core/src/channels/runtime/dispatch/helpers_tests.rs@@ -43,8 +43,12 @@ async fn canceled_waiter_closes_handle_returned_by_in_flight_typing_start() {stopped: std::sync::Mutex::new(Some(sender)),});let cancellation = tokio_util::sync::CancellationToken::new();- let waiter =- spawn_typing_task(&DispatchCtx::unbound(), channel.clone(), "recipient".into(), cancellation.clone());+ let waiter = spawn_typing_task(+ &DispatchCtx::unbound(),+ channel.clone(),+ "recipient".into(),+ cancellation.clone(),+ );channel.started.notified().await;waiter.abort();cancellation.cancel();@@ -57,3 +61,60 @@ async fn canceled_waiter_closes_handle_returned_by_in_flight_typing_start() {"module-handle");}++struct ProbeTyping(std::sync::Mutex<Option<oneshot::Sender<Option<String>>>>);+#[async_trait::async_trait]+impl crate::channels::Channel for ProbeTyping {+ fn name(&self) -> &str {+ "probe"+ }+ async fn send(&self, _: &crate::channels::SendMessage) -> anyhow::Result<()> {+ Ok(())+ }+ async fn listen(+ &self,+ _: tokio::sync::mpsc::Sender<traits::ChannelMessage>,+ ) -> anyhow::Result<()> {+ Ok(())+ }+ async fn begin_typing_refresh(&self, _: &str, _: u64) -> anyhow::Result<Option<String>> {+ let agent = crate::core::runtime::CoreContext::current()+ .and_then(|core| core.session_agent().map(str::to_owned));+ if let Some(sender) = self.0.lock().unwrap().take() {+ let _ = sender.send(agent);+ }+ Ok(None)+ }+}++#[tokio::test]+async fn typing_task_runs_under_the_handed_context_not_the_active_one() {+ use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};+ let agent_ctx = |agent: &str| {+ CoreContext::for_test(DomainSet::full(), None).derive_with(+ ContextOverlay::new(+ crate::config::Config::default(),+ DomainSet::full(),+ Default::default(),+ )+ .session_agent(agent),+ )+ };+ let asha = DispatchCtx::new(agent_ctx("u-asha"));+ let ravi = agent_ctx("u-ravi");+ let (sender, receiver) = oneshot::channel();+ let channel = Arc::new(ProbeTyping(std::sync::Mutex::new(Some(sender))));+ let cancellation = tokio_util::sync::CancellationToken::new();+ // The caller's own scope is Ravi's; the typing task was handed Asha's.+ let waiter = CoreContext::scope(ravi, async {+ spawn_typing_task(&asha, channel, "recipient".into(), cancellation.clone())+ })+ .await;+ let seen = tokio::time::timeout(std::time::Duration::from_secs(2), receiver)+ .await+ .unwrap()+ .unwrap();+ cancellation.cancel();+ let _ = waiter.await;+ assert_eq!(seen.as_deref(), Some("u-asha"));+}diff --git a/crates/openhuman-core/src/channels/runtime/session_tests.rs b/crates/openhuman-core/src/channels/runtime/session_tests.rsindex 6a96c2bdf5..f5c2a1c76a 100644--- a/crates/openhuman-core/src/channels/runtime/session_tests.rs+++ b/crates/openhuman-core/src/channels/runtime/session_tests.rs@@ -103,21 +103,25 @@ async fn channel_sign_in_resumes_listener_service_after_logout() {let observed_cleanups = Arc::clone(&cleanups);let (started_tx, mut started_rx) = tokio::sync::mpsc::unbounded_channel();let (cleaned_tx, mut cleaned_rx) = tokio::sync::mpsc::unbounded_channel();- let service = spawn_channel_service_with(&DispatchCtx::unbound(), controller.clone(), move |session| {- let starts = Arc::clone(&observed_starts);- let cleanups = Arc::clone(&observed_cleanups);- let started_tx = started_tx.clone();- let cleaned_tx = cleaned_tx.clone();- async move {- let generation = starts.fetch_add(1, Ordering::SeqCst) + 1;- let cleanups_before_start = cleanups.load(Ordering::SeqCst);- let _ = started_tx.send((session.clone(), cleanups_before_start));- session.cancelled().await;- let completed_cleanup = cleanups.fetch_add(1, Ordering::SeqCst) + 1;- let _ = cleaned_tx.send((generation, completed_cleanup));- Ok(())- }- });+ let service = spawn_channel_service_with(+ &DispatchCtx::unbound(),+ controller.clone(),+ move |session| {+ let starts = Arc::clone(&observed_starts);+ let cleanups = Arc::clone(&observed_cleanups);+ let started_tx = started_tx.clone();+ let cleaned_tx = cleaned_tx.clone();+ async move {+ let generation = starts.fetch_add(1, Ordering::SeqCst) + 1;+ let cleanups_before_start = cleanups.load(Ordering::SeqCst);+ let _ = started_tx.send((session.clone(), cleanups_before_start));+ session.cancelled().await;+ let completed_cleanup = cleanups.fetch_add(1, Ordering::SeqCst) + 1;+ let _ = cleaned_tx.send((generation, completed_cleanup));+ Ok(())+ }+ },+ );let (old, initial_cleanups) = started_rx.recv().await.expect("initial channel generation");assert_eq!(initial_cleanups, 0);@@ -168,16 +172,20 @@ async fn channel_service_created_during_logout_waits_for_sign_in() {let starts = Arc::new(AtomicUsize::new(0));let observed_starts = Arc::clone(&starts);let (started_tx, mut started_rx) = tokio::sync::mpsc::unbounded_channel();- let service = spawn_channel_service_with(&DispatchCtx::unbound(), controller.clone(), move |session| {- let starts = Arc::clone(&observed_starts);- let started_tx = started_tx.clone();- async move {- starts.fetch_add(1, Ordering::SeqCst);- let _ = started_tx.send(session.clone());- session.cancelled().await;- Ok(())- }- });+ let service = spawn_channel_service_with(+ &DispatchCtx::unbound(),+ controller.clone(),+ move |session| {+ let starts = Arc::clone(&observed_starts);+ let started_tx = started_tx.clone();+ async move {+ starts.fetch_add(1, Ordering::SeqCst);+ let _ = started_tx.send(session.clone());+ session.cancelled().await;+ Ok(())+ }+ },+ );assert!(tokio::time::timeout(std::time::Duration::from_millis(20), started_rx.recv())diff --git a/crates/openhuman-core/src/storage/import/mod.rs b/crates/openhuman-core/src/storage/import/mod.rsindex f68e0dd981..b4ec117a68 100644--- a/crates/openhuman-core/src/storage/import/mod.rs+++ b/crates/openhuman-core/src/storage/import/mod.rs@@ -190,9 +190,7 @@ pub async fn run(ctx: &crate::core::runtime::DispatchCtx, config: &Config) -> Vereturn Vec::new();}let config = config.clone();- let joined = ctx- .spawn_blocking(move || run_all(&backend, &config))- .await;+ let joined = ctx.spawn_blocking(move || run_all(&backend, &config)).await;match joined {Ok(reports) => reports,Err(error) => {