Skip to content

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

fix(channels): keep typing refresh alive until cleanup completes

Steven Enamakelfe2dba2cea78
diff --git a/crates/openhuman-core/Cargo.toml b/crates/openhuman-core/Cargo.toml
index 8b49c96170..3e6d3bdbf9 100644
--- a/crates/openhuman-core/Cargo.toml
+++ b/crates/openhuman-core/Cargo.toml
@@ -556,6 +556,9 @@ windows-sys = { version = "0.61", features = [
tokio-tungstenite = { version = "0.29", default-features = false, features = ["connect", "handshake", "rustls-tls-webpki-roots"] }
[dev-dependencies]
+# The real linked TinyChannels implementation backs host adapter unit tests;
+# it is test-only and never enters the OpenHuman product dependency graph.
+tinychannels-module = { path = "../../vendor/tinychannels/crates/tinychannels-module", features = ["static-link"] }
# The TinyMemory reference engine (an in-memory `MemoryEngine`) and the
# behavioural suite every engine must pass. Memory unit tests bind it as the
# engine instead of talking to CortexDB.
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
index 6047c8e7b6..3ec2a7d5c1 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
@@ -2,7 +2,6 @@
use crate::channels::context::CHANNEL_TYPING_REFRESH_INTERVAL_SECS;
use crate::channels::traits;
-use std::time::Duration;
/// Maximum characters shown in the debug reply println.
pub(super) const REPLY_LOG_TRUNCATE_CHARS: usize = 200;
@@ -15,48 +14,26 @@ pub(super) fn spawn_scoped_typing_task(
recipient: String,
cancellation_token: tokio_util::sync::CancellationToken,
) -> tokio::task::JoinHandle<()> {
+ let (finished_tx, finished_rx) = tokio::sync::oneshot::channel();
+ // The worker retains ownership across cancellation of the caller while
+ // StartTypingRefresh is in flight, then closes the returned module handle.
tokio::spawn(async move {
- let handle = match channel
- .begin_typing_refresh(&recipient, CHANNEL_TYPING_REFRESH_INTERVAL_SECS)
- .await
- {
- Ok(handle) => handle,
- Err(error) => {
- tracing::debug!(
- channel = channel.name(),
- "typing refresh could not start: {error}"
- );
- let _ = channel.stop_typing(&recipient).await;
- return;
- }
- };
-
- if let Some(handle) = &handle {
- cancellation_token.cancelled().await;
- if let Err(error) = channel.end_typing_refresh(handle).await {
- tracing::debug!(
- channel = channel.name(),
- "typing refresh cleanup failed: {error}"
- );
- }
- } else {
- // Compatibility for non-module test doubles. Production module
- // providers return an opaque handle and own the refresh timer.
- loop {
- tokio::select! {
- () = cancellation_token.cancelled() => break,
- () = tokio::time::sleep(Duration::from_secs(CHANNEL_TYPING_REFRESH_INTERVAL_SECS)) => {
- if let Err(error) = channel.start_typing(&recipient).await {
- tracing::debug!(channel = channel.name(), "typing start failed: {error}");
- }
- }
+ match channel.begin_typing_refresh(&recipient, CHANNEL_TYPING_REFRESH_INTERVAL_SECS).await {
+ Ok(Some(handle)) => {
+ cancellation_token.cancelled().await;
+ if channel.end_typing_refresh(&handle).await.is_err() {
+ tracing::debug!(channel = channel.name(), "typing refresh cleanup failed");
}
}
+ Ok(None) => cancellation_token.cancelled().await,
+ Err(_) => tracing::debug!(channel = channel.name(), "typing refresh unavailable"),
}
- if let Err(error) = channel.stop_typing(&recipient).await {
- tracing::debug!(channel = channel.name(), "typing stop failed: {error}");
+ if channel.stop_typing(&recipient).await.is_err() {
+ tracing::debug!(channel = channel.name(), "typing stop failed");
}
- })
+ let _ = finished_tx.send(());
+ });
+ tokio::spawn(async move { let _ = finished_rx.await; })
}
/// Build the `[Channel context]` block for a channel turn.
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index d5fe028815..e0da3e2924 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -345,6 +345,7 @@ pub(crate) async fn process_channel_runtime_message(
};
let typing_cancellation = target_channel.as_ref().map(|_| CancellationToken::new());
+ let _typing_cancel_on_drop = typing_cancellation.as_ref().map(|token| token.clone().drop_guard());
// Typing was already started early (before memory/provider setup). Here we only
// spawn the background refresh task that keeps the indicator alive during long turns.
let typing_task = match (target_channel.as_ref(), typing_cancellation.as_ref()) {