Skip to content

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

fix(web_chat): signal drop cleanup completion

Steven Enamakel528f81ca0ac0
diff --git a/crates/openhuman-core/src/web_chat/surface.rs b/crates/openhuman-core/src/web_chat/surface.rs
index 6e619894a6..cf45423631 100644
--- a/crates/openhuman-core/src/web_chat/surface.rs
+++ b/crates/openhuman-core/src/web_chat/surface.rs
@@ -34,6 +34,7 @@ struct SurfaceState {
#[derive(Clone)]
pub(crate) struct Surface {
state: Arc<tokio::sync::Mutex<SurfaceState>>,
+ cleanup_done: Arc<tokio::sync::Notify>,
ctx: DispatchCtx,
scope_key: String,
ids: TurnIds,
@@ -66,6 +67,7 @@ impl Surface {
handle: None,
closed: false,
})),
+ cleanup_done: Arc::new(tokio::sync::Notify::new()),
ctx: ctx.clone(),
scope_key,
ids,
@@ -189,8 +191,11 @@ impl Surface {
/// The leading narration of `round`, flushed as an interim bubble.
pub(crate) async fn interim(&self, round: u32, text: String) {
- self.project(ChannelOutputEvent::Clarification { prompt: text }, Some(round))
- .await;
+ self.project(
+ ChannelOutputEvent::Clarification { prompt: text },
+ Some(round),
+ )
+ .await;
}
/// Publish a host-authored event unchanged (queue, goal, todo, or the
@@ -230,28 +235,29 @@ impl Drop for Surface {
return;
};
let state = Arc::clone(&self.state);
+ let cleanup_done = Arc::clone(&self.cleanup_done);
let request_id = self.ids.request_id.clone();
runtime.spawn(async move {
let mut state = state.lock().await;
state.closed = true;
- let Some(handle) = state.handle.take() else {
- return;
- };
- if let Err(error) = state
- .module
- .call_confidential::<()>(
- "tinychannels",
- tinychannels_bus::methods::END_WEB_TURN,
- (EndWebTurn {
- handle: handle.handle,
- },),
- )
- .await
- {
- log::warn!(
- "[web-channel] dropped projection turn cleanup failed request_id={request_id}: {error}"
- );
+ if let Some(handle) = state.handle.take() {
+ if let Err(error) = state
+ .module
+ .call_confidential::<()>(
+ "tinychannels",
+ tinychannels_bus::methods::END_WEB_TURN,
+ (EndWebTurn {
+ handle: handle.handle,
+ },),
+ )
+ .await
+ {
+ log::warn!(
+ "[web-channel] dropped projection turn cleanup failed request_id={request_id}: {error}"
+ );
+ }
}
+ cleanup_done.notify_one();
});
}
}
diff --git a/crates/openhuman-core/src/web_chat/surface_tests.rs b/crates/openhuman-core/src/web_chat/surface_tests.rs
index 54674c3f0d..716ea75937 100644
--- a/crates/openhuman-core/src/web_chat/surface_tests.rs
+++ b/crates/openhuman-core/src/web_chat/surface_tests.rs
@@ -91,6 +91,75 @@ async fn host_authored_events_pass_through_without_a_seq() {
assert_eq!(event.seq, None);
}
+#[tokio::test]
+async fn module_projected_cancellation_closes_the_turn() {
+ let mut rx = crate::web_chat::subscribe_web_channel_events();
+ let surface = Surface::new(
+ &DispatchCtx::ambient(),
+ ids("surface-cancel"),
+ crate::config::Config::default(),
+ )
+ .await;
+ surface
+ .output(ChannelOutputEvent::Cancellation {
+ reason: Some("user_stop".into()),
+ })
+ .await;
+
+ let event = next_for(&mut rx, "surface-cancel").await;
+ assert_eq!(event.event, "chat_cancelled");
+ assert_eq!(event.seq, Some(1));
+ let state = surface.state.lock().await;
+ assert!(state.closed);
+ assert!(state.handle.is_none());
+}
+
+#[tokio::test]
+async fn dropping_a_surface_ends_its_module_owned_turn() {
+ let surface = Surface::new(
+ &DispatchCtx::ambient(),
+ ids("surface-drop-cleanup"),
+ crate::config::Config::default(),
+ )
+ .await;
+ surface
+ .output(ChannelOutputEvent::TextDelta {
+ text: "active".into(),
+ iteration: Some(1),
+ })
+ .await;
+ let (module, handle) = {
+ let state = surface.state.lock().await;
+ (
+ state.module.clone(),
+ state.handle.as_ref().unwrap().handle.clone(),
+ )
+ };
+ let cleanup_done = Arc::clone(&surface.cleanup_done);
+ drop(surface);
+ tokio::time::timeout(std::time::Duration::from_secs(5), cleanup_done.notified())
+ .await
+ .expect("drop cleanup completes");
+
+ let result: Result<tinychannels_bus::module_web_output::WebOutputEvents, _> = module
+ .call_confidential(
+ "tinychannels",
+ tinychannels_bus::methods::PROJECT_WEB_OUTPUT,
+ (ProjectWebOutput {
+ handle,
+ event: ChannelOutputEvent::TextDelta {
+ text: "late".into(),
+ iteration: Some(1),
+ },
+ },),
+ )
+ .await;
+ assert!(
+ result.is_err(),
+ "a dropped turn must reject later projections"
+ );
+}
+
#[tokio::test]
async fn the_addressing_context_names_the_turn() {
let surface = Surface::new(