Skip to content

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

feat(channels): report progressive delivery outcomes and close aborted replies

Steven Enamakel39ef3942d78c
diff --git a/crates/openhuman-core/src/channels/bus/delivery.rs b/crates/openhuman-core/src/channels/bus/delivery.rs
index 51f485d39b..61e938ada2 100644
--- a/crates/openhuman-core/src/channels/bus/delivery.rs
+++ b/crates/openhuman-core/src/channels/bus/delivery.rs
@@ -191,7 +191,10 @@ impl NativeProgressiveReply {
}
}
- pub(crate) async fn feed(&self, events: Vec<ChannelOutputEvent>) -> Result<(), ModuleCallError> {
+ pub(crate) async fn feed(
+ &self,
+ events: Vec<ChannelOutputEvent>,
+ ) -> Result<ProgressiveResult, ModuleCallError> {
let result: ProgressiveResult = self
.object
.call_confidential(
@@ -202,24 +205,65 @@ impl NativeProgressiveReply {
},),
)
.await?;
- if !matches!(result, ProgressiveResult::Updated | ProgressiveResult::DeliveryBatch { .. }) {
+ if !matches!(
+ result,
+ ProgressiveResult::Updated
+ | ProgressiveResult::DeliveryOutcome { .. }
+ | ProgressiveResult::DeliveryOutcomes { .. }
+ | ProgressiveResult::DeliveryUnsupported { .. }
+ | ProgressiveResult::DeliveryBatch { .. }
+ ) {
crate::modules::report::malformed_reply("tinychannels");
return Err(ModuleCallError::ModuleFault);
}
- Ok(())
+ Ok(result)
}
- pub(crate) async fn finish(&self, text: &str) -> Result<(), ModuleCallError> {
+ /// Deliver the canonical final event and return every receipt/failure fact.
+ pub(crate) async fn finish(
+ &self,
+ text: &str,
+ ) -> Result<ProgressiveResult, ModuleCallError> {
let result: ProgressiveResult = self
.object
.call_confidential(
methods::EXECUTE_PROGRESSIVE,
- (ProgressiveCommand::Finish {
+ (ProgressiveCommand::FeedOutput {
handle: self.handle.clone(),
- text: text.to_owned(),
+ events: vec![ChannelOutputEvent::FinalMessage {
+ text: text.to_owned(),
+ parts: Vec::new(),
+ receipt: None,
+ }],
},),
)
.await?;
+ if !matches!(
+ result,
+ ProgressiveResult::DeliveryOutcome { .. }
+ | ProgressiveResult::DeliveryOutcomes { .. }
+ | ProgressiveResult::DeliveryUnsupported { .. }
+ | ProgressiveResult::DeliveryBatch { .. }
+ ) {
+ crate::modules::report::malformed_reply("tinychannels");
+ return Err(ModuleCallError::ModuleFault);
+ }
+ Ok(result)
+ }
+
+ /// Abort an unfinished reply after cancellation or a failed progress call.
+ pub(crate) async fn close(&self) -> Result<(), ModuleCallError> {
+ let result: ProgressiveResult = tokio::time::timeout(
+ std::time::Duration::from_secs(5),
+ self.object.call_confidential(
+ methods::EXECUTE_PROGRESSIVE,
+ (ProgressiveCommand::Close {
+ handle: self.handle.clone(),
+ },),
+ ),
+ )
+ .await
+ .map_err(|_| ModuleCallError::ModuleFault)??;
if result != ProgressiveResult::Closed {
crate::modules::report::malformed_reply("tinychannels");
return Err(ModuleCallError::ModuleFault);
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs
index 4f4e62804e..a21d9400e2 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs
@@ -36,10 +36,25 @@ pub(super) async fn finish_native_reply(
let Some(reply) = reply else {
return false;
};
- if let Err(error) = reply.finish(text).await {
- tracing::error!("[dispatch] native progressive final delivery failed: {error}");
- }
- true
+ let delivered = match reply.finish(text).await {
+ Ok(result) if delivery_succeeded(&result) => true,
+ Ok(result) => {
+ tracing::error!(
+ outcomes = delivery_outcome_count(&result),
+ unsupported_parts = delivery_unsupported_count(&result),
+ "[dispatch] native progressive final delivery was incomplete"
+ );
+ false
+ }
+ Err(error) => {
+ tracing::error!("[dispatch] native progressive final delivery failed: {error}");
+ if let Err(close_error) = reply.close().await {
+ tracing::warn!("[dispatch] native progressive close failed: {close_error}");
+ }
+ false
+ }
+ };
+ delivered
}
pub(super) fn spawn_native_progress(
@@ -47,18 +62,82 @@ pub(super) fn spawn_native_progress(
reply: std::sync::Arc<NativeProgressiveReply>,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
+ let mut failed = false;
while let Some(progress) = events.recv().await {
let Some(event) = output_event(progress) else {
continue;
};
- if let Err(error) = reply.feed(vec![event]).await {
- tracing::warn!("[dispatch] progressive channel update failed: {error}");
- break;
+ match reply.feed(vec![event]).await {
+ Ok(result) if delivery_succeeded(&result) => {}
+ Ok(result) => {
+ tracing::warn!(
+ outcomes = delivery_outcome_count(&result),
+ unsupported_parts = delivery_unsupported_count(&result),
+ "[dispatch] progressive channel update was incomplete"
+ );
+ failed = true;
+ break;
+ }
+ Err(error) => {
+ tracing::warn!("[dispatch] progressive channel update failed: {error}");
+ failed = true;
+ break;
+ }
+ }
+ }
+ if failed {
+ if let Err(error) = reply.close().await {
+ tracing::warn!("[dispatch] native progressive close failed: {error}");
}
}
})
}
+fn delivery_succeeded(result: &tinychannels_bus::module_progressive::ProgressiveResult) -> bool {
+ use tinychannels_bus::module_progressive::ProgressiveResult;
+ match result {
+ ProgressiveResult::Updated => true,
+ ProgressiveResult::DeliveryOutcome { outcome } => outcome.error.is_none(),
+ ProgressiveResult::DeliveryOutcomes { outcomes } => {
+ outcomes.iter().all(|outcome| outcome.error.is_none())
+ }
+ ProgressiveResult::DeliveryBatch {
+ outcomes,
+ unsupported,
+ failure,
+ } => {
+ outcomes.iter().all(|outcome| outcome.error.is_none())
+ && unsupported.is_empty()
+ && failure.is_none()
+ }
+ ProgressiveResult::DeliveryUnsupported { parts } => parts.is_empty(),
+ _ => false,
+ }
+}
+
+fn delivery_outcome_count(result: &tinychannels_bus::module_progressive::ProgressiveResult) -> usize {
+ use tinychannels_bus::module_progressive::ProgressiveResult;
+ match result {
+ ProgressiveResult::DeliveryOutcome { .. } => 1,
+ ProgressiveResult::DeliveryOutcomes { outcomes }
+ | ProgressiveResult::DeliveryBatch { outcomes, .. } => outcomes.len(),
+ _ => 0,
+ }
+}
+
+fn delivery_unsupported_count(
+ result: &tinychannels_bus::module_progressive::ProgressiveResult,
+) -> usize {
+ use tinychannels_bus::module_progressive::ProgressiveResult;
+ match result {
+ ProgressiveResult::DeliveryUnsupported { parts }
+ | ProgressiveResult::DeliveryBatch {
+ unsupported: parts, ..
+ } => parts.len(),
+ _ => 0,
+ }
+}
+
fn output_event(progress: AgentProgress) -> Option<ChannelOutputEvent> {
match progress {
AgentProgress::TextDelta { delta, iteration } => Some(ChannelOutputEvent::TextDelta {
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 6a1930fd85..caf5f6d557 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -542,7 +542,8 @@ pub(crate) async fn process_channel_runtime_message(
started_at.elapsed().as_millis(),
truncate_with_ellipsis(&response_text, REPLY_LOG_TRUNCATE_CHARS)
);
- if !super::progressive::finish_native_reply(&native_reply, &response_text).await {
+ let delivered = super::progressive::finish_native_reply(&native_reply, &response_text).await;
+ if native_reply.is_none() {
if let Some(channel) = target_channel.as_ref() {
if let Some(ref draft_id) = draft_message_id {
if let Err(e) = channel
@@ -573,7 +574,7 @@ pub(crate) async fn process_channel_runtime_message(
}
}
}
- (true, response_text, resolved_provider, resolved_model)
+ (delivered, response_text, resolved_provider, resolved_model)
}
Ok(Err(e)) => {
if is_context_window_overflow_error(&ctx, &e)
@@ -585,7 +586,8 @@ pub(crate) async fn process_channel_runtime_message(
" ⚠️ Context window exceeded after {}ms",
started_at.elapsed().as_millis()
);
- if !super::progressive::finish_native_reply(&native_reply, error_text).await {
+ let _ = super::progressive::finish_native_reply(&native_reply, error_text).await;
+ if native_reply.is_none() {
if let Some(channel) = target_channel.as_ref() {
if let Some(ref draft_id) = draft_message_id {
let _ = channel
@@ -676,7 +678,8 @@ pub(crate) async fn process_channel_runtime_message(
],
);
}
- if !super::progressive::finish_native_reply(&native_reply, &error_response).await {
+ let _ = super::progressive::finish_native_reply(&native_reply, &error_response).await;
+ if native_reply.is_none() {
if let Some(channel) = target_channel.as_ref() {
if let Some(ref draft_id) = draft_message_id {
let _ = channel
@@ -722,7 +725,8 @@ pub(crate) async fn process_channel_runtime_message(
);
let error_text =
"⚠️ Request timed out while waiting for the model. Please try again.".to_string();
- if !super::progressive::finish_native_reply(&native_reply, &error_text).await {
+ let _ = super::progressive::finish_native_reply(&native_reply, &error_text).await;
+ if native_reply.is_none() {
if let Some(channel) = target_channel.as_ref() {
if let Some(ref draft_id) = draft_message_id {
let _ = channel