Skip to content

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

fix(channels): own typing refresh lifecycle in dispatch

Steven Enamakelcbe3cf899340
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
index 4cdfaee016..892bd4c844 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
@@ -15,12 +15,45 @@ pub(super) fn spawn_scoped_typing_task(
recipient: String,
cancellation_token: tokio_util::sync::CancellationToken,
) -> tokio::task::JoinHandle<()> {
- tinychannels::runtime::spawn_scoped_typing_task(
- channel,
- recipient,
- cancellation_token,
- Duration::from_secs(CHANNEL_TYPING_REFRESH_INTERVAL_SECS),
- )
+ 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}");
+ }
+ }
+ }
+ }
+ }
+ if let Err(error) = channel.stop_typing(&recipient).await {
+ tracing::debug!(channel = channel.name(), "typing stop failed: {error}");
+ }
+ })
}
/// Build the `[Channel context]` block for a channel turn.
diff --git a/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs b/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs
index b5bdb3e875..f451439f82 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/module_providers.rs
@@ -283,36 +283,50 @@ impl Channel for ModuleChannel {
self.interact(ChannelInteraction::StartTyping {
recipient: recipient.to_owned(),
})
- .await?;
- if let Err(error) = self
- .interact(ChannelInteraction::StartTypingRefresh {
- recipient: recipient.to_owned(),
- interval_secs: crate::channels::context::CHANNEL_TYPING_REFRESH_INTERVAL_SECS,
- })
+ .await
+ }
+
+ async fn stop_typing(&self, recipient: &str) -> anyhow::Result<()> {
+ self.interact(ChannelInteraction::StopTyping {
+ recipient: recipient.to_owned(),
+ })
+ .await
+ }
+
+ async fn begin_typing_refresh(
+ &self,
+ recipient: &str,
+ interval_secs: u64,
+ ) -> anyhow::Result<Option<String>> {
+ match self
+ .object
+ .call_confidential::<ChannelInteractionResult>(
+ tinychannels_bus::methods::CHANNEL_INTERACTION,
+ tinychannels_bus::ChannelInteractionRequest {
+ channel: self.name.clone(),
+ interaction: ChannelInteraction::StartTypingRefresh {
+ recipient: recipient.to_owned(),
+ interval_secs,
+ },
+ },
+ )
.await
+ .map_err(anyhow::Error::new)?
{
- let _ = self
- .interact(ChannelInteraction::StopTyping {
- recipient: recipient.to_owned(),
- })
- .await;
- return Err(error);
+ ChannelInteractionResult::TypingRefresh { handle }
+ if !handle.is_empty() && handle.len() <= 128 =>
+ {
+ Ok(Some(handle))
+ }
+ _ => anyhow::bail!("channel returned an invalid typing refresh handle"),
}
- Ok(())
}
- async fn stop_typing(&self, recipient: &str) -> anyhow::Result<()> {
- let refresh = self
- .interact(ChannelInteraction::StopTypingRefresh {
- recipient: recipient.to_owned(),
- })
- .await;
- let stopped = self
- .interact(ChannelInteraction::StopTyping {
- recipient: recipient.to_owned(),
- })
- .await;
- refresh.and(stopped)
+ async fn end_typing_refresh(&self, handle: &str) -> anyhow::Result<()> {
+ self.interact(ChannelInteraction::StopTypingRefresh {
+ handle: handle.to_owned(),
+ })
+ .await
}
fn capabilities(&self) -> tinychannels_bus::ChannelCapabilities {